2005-06-07 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / c-parser.c
blobabb8b2291320e6132884502060fe00b8c3c91af1
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 Parser actions based on the old Bison parser; structure somewhat
6 influenced by and fragments based on the C++ parser.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA. */
25 /* TODO:
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "toplev.h"
54 #include "ggc.h"
55 #include "c-common.h"
58 /* Miscellaneous data and functions needed for the parser. */
60 int yydebug;
62 /* Objective-C specific parser/lexer information. */
64 static int objc_pq_context = 0;
66 /* The following flag is needed to contextualize Objective-C lexical
67 analysis. In some cases (e.g., 'int NSObject;'), it is undesirable
68 to bind an identifier to an Objective-C class, even if a class with
69 that name exists. */
70 static int objc_need_raw_identifier = 0;
71 #define OBJC_NEED_RAW_IDENTIFIER(VAL) \
72 do { \
73 if (c_dialect_objc ()) \
74 objc_need_raw_identifier = VAL; \
75 } while (0)
77 /* The reserved keyword table. */
78 struct resword
80 const char *word;
81 ENUM_BITFIELD(rid) rid : 16;
82 unsigned int disable : 16;
85 /* Disable mask. Keywords are disabled if (reswords[i].disable &
86 mask) is _true_. */
87 #define D_C89 0x01 /* not in C89 */
88 #define D_EXT 0x02 /* GCC extension */
89 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
90 #define D_OBJC 0x08 /* Objective C only */
92 static const struct resword reswords[] =
94 { "_Bool", RID_BOOL, 0 },
95 { "_Complex", RID_COMPLEX, 0 },
96 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
97 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
98 { "__alignof", RID_ALIGNOF, 0 },
99 { "__alignof__", RID_ALIGNOF, 0 },
100 { "__asm", RID_ASM, 0 },
101 { "__asm__", RID_ASM, 0 },
102 { "__attribute", RID_ATTRIBUTE, 0 },
103 { "__attribute__", RID_ATTRIBUTE, 0 },
104 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
105 { "__builtin_offsetof", RID_OFFSETOF, 0 },
106 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
107 { "__builtin_va_arg", RID_VA_ARG, 0 },
108 { "__complex", RID_COMPLEX, 0 },
109 { "__complex__", RID_COMPLEX, 0 },
110 { "__const", RID_CONST, 0 },
111 { "__const__", RID_CONST, 0 },
112 { "__extension__", RID_EXTENSION, 0 },
113 { "__func__", RID_C99_FUNCTION_NAME, 0 },
114 { "__imag", RID_IMAGPART, 0 },
115 { "__imag__", RID_IMAGPART, 0 },
116 { "__inline", RID_INLINE, 0 },
117 { "__inline__", RID_INLINE, 0 },
118 { "__label__", RID_LABEL, 0 },
119 { "__real", RID_REALPART, 0 },
120 { "__real__", RID_REALPART, 0 },
121 { "__restrict", RID_RESTRICT, 0 },
122 { "__restrict__", RID_RESTRICT, 0 },
123 { "__signed", RID_SIGNED, 0 },
124 { "__signed__", RID_SIGNED, 0 },
125 { "__thread", RID_THREAD, 0 },
126 { "__typeof", RID_TYPEOF, 0 },
127 { "__typeof__", RID_TYPEOF, 0 },
128 { "__volatile", RID_VOLATILE, 0 },
129 { "__volatile__", RID_VOLATILE, 0 },
130 { "asm", RID_ASM, D_EXT },
131 { "auto", RID_AUTO, 0 },
132 { "break", RID_BREAK, 0 },
133 { "case", RID_CASE, 0 },
134 { "char", RID_CHAR, 0 },
135 { "const", RID_CONST, 0 },
136 { "continue", RID_CONTINUE, 0 },
137 { "default", RID_DEFAULT, 0 },
138 { "do", RID_DO, 0 },
139 { "double", RID_DOUBLE, 0 },
140 { "else", RID_ELSE, 0 },
141 { "enum", RID_ENUM, 0 },
142 { "extern", RID_EXTERN, 0 },
143 { "float", RID_FLOAT, 0 },
144 { "for", RID_FOR, 0 },
145 { "goto", RID_GOTO, 0 },
146 { "if", RID_IF, 0 },
147 { "inline", RID_INLINE, D_EXT89 },
148 { "int", RID_INT, 0 },
149 { "long", RID_LONG, 0 },
150 { "register", RID_REGISTER, 0 },
151 { "restrict", RID_RESTRICT, D_C89 },
152 { "return", RID_RETURN, 0 },
153 { "short", RID_SHORT, 0 },
154 { "signed", RID_SIGNED, 0 },
155 { "sizeof", RID_SIZEOF, 0 },
156 { "static", RID_STATIC, 0 },
157 { "struct", RID_STRUCT, 0 },
158 { "switch", RID_SWITCH, 0 },
159 { "typedef", RID_TYPEDEF, 0 },
160 { "typeof", RID_TYPEOF, D_EXT },
161 { "union", RID_UNION, 0 },
162 { "unsigned", RID_UNSIGNED, 0 },
163 { "void", RID_VOID, 0 },
164 { "volatile", RID_VOLATILE, 0 },
165 { "while", RID_WHILE, 0 },
166 /* These Objective-C keywords are recognized only immediately after
167 an '@'. */
168 { "class", RID_AT_CLASS, D_OBJC },
169 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
170 { "defs", RID_AT_DEFS, D_OBJC },
171 { "encode", RID_AT_ENCODE, D_OBJC },
172 { "end", RID_AT_END, D_OBJC },
173 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
174 { "interface", RID_AT_INTERFACE, D_OBJC },
175 { "private", RID_AT_PRIVATE, D_OBJC },
176 { "protected", RID_AT_PROTECTED, D_OBJC },
177 { "protocol", RID_AT_PROTOCOL, D_OBJC },
178 { "public", RID_AT_PUBLIC, D_OBJC },
179 { "selector", RID_AT_SELECTOR, D_OBJC },
180 { "throw", RID_AT_THROW, D_OBJC },
181 { "try", RID_AT_TRY, D_OBJC },
182 { "catch", RID_AT_CATCH, D_OBJC },
183 { "finally", RID_AT_FINALLY, D_OBJC },
184 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
185 /* These are recognized only in protocol-qualifier context
186 (see above) */
187 { "bycopy", RID_BYCOPY, D_OBJC },
188 { "byref", RID_BYREF, D_OBJC },
189 { "in", RID_IN, D_OBJC },
190 { "inout", RID_INOUT, D_OBJC },
191 { "oneway", RID_ONEWAY, D_OBJC },
192 { "out", RID_OUT, D_OBJC },
194 #define N_reswords (sizeof reswords / sizeof (struct resword))
196 /* Initialization routine for this file. */
198 void
199 c_parse_init (void)
201 /* The only initialization required is of the reserved word
202 identifiers. */
203 unsigned int i;
204 tree id;
205 int mask = (flag_isoc99 ? 0 : D_C89)
206 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
208 if (!c_dialect_objc ())
209 mask |= D_OBJC;
211 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
212 for (i = 0; i < N_reswords; i++)
214 /* If a keyword is disabled, do not enter it into the table
215 and so create a canonical spelling that isn't a keyword. */
216 if (reswords[i].disable & mask)
217 continue;
219 id = get_identifier (reswords[i].word);
220 C_RID_CODE (id) = reswords[i].rid;
221 C_IS_RESERVED_WORD (id) = 1;
222 ridpointers [(int) reswords[i].rid] = id;
226 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
227 and the C parser. Unlike the C++ lexer, the parser structure
228 stores the lexer information instead of using a separate structure.
229 Identifiers are separated into ordinary identifiers, type names,
230 keywords and some other Objective-C types of identifiers, and some
231 look-ahead is maintained.
233 ??? It might be a good idea to lex the whole file up front (as for
234 C++). It would then be possible to share more of the C and C++
235 lexer code, if desired. */
237 /* The following local token type is used. */
239 /* A keyword. */
240 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
242 /* More information about the type of a CPP_NAME token. */
243 typedef enum c_id_kind {
244 /* An ordinary identifier. */
245 C_ID_ID,
246 /* An identifier declared as a typedef name. */
247 C_ID_TYPENAME,
248 /* An identifier declared as an Objective-C class name. */
249 C_ID_CLASSNAME,
250 /* Not an identifier. */
251 C_ID_NONE
252 } c_id_kind;
254 /* A single C token after string literal concatenation and conversion
255 of preprocessing tokens to tokens. */
256 typedef struct c_token GTY (())
258 /* The kind of token. */
259 ENUM_BITFIELD (cpp_ttype) type : 8;
260 /* If this token is a CPP_NAME, this value indicates whether also
261 declared as some kind of type. Otherwise, it is C_ID_NONE. */
262 ENUM_BITFIELD (c_id_kind) id_kind : 8;
263 /* If this token is a keyword, this value indicates which keyword.
264 Otherwise, this value is RID_MAX. */
265 ENUM_BITFIELD (rid) keyword : 8;
266 /* True if this token is from a system header. */
267 BOOL_BITFIELD in_system_header : 1;
268 /* The value associated with this token, if any. */
269 tree value;
270 /* The location at which this token was found. */
271 location_t location;
272 } c_token;
274 /* A parser structure recording information about the state and
275 context of parsing. Includes lexer information with up to two
276 tokens of look-ahead; more are not needed for C. */
277 typedef struct c_parser GTY(())
279 /* The look-ahead tokens. */
280 c_token tokens[2];
281 /* How many look-ahead tokens are available (0, 1 or 2). */
282 short tokens_avail;
283 /* True if a syntax error is being recovered from; false otherwise.
284 c_parser_error sets this flag. It should clear this flag when
285 enough tokens have been consumed to recover from the error. */
286 BOOL_BITFIELD error : 1;
287 } c_parser;
289 /* Read in and lex a single token, storing it in *TOKEN. */
291 static void
292 c_lex_one_token (c_token *token)
294 timevar_push (TV_LEX);
295 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
296 token->in_system_header = in_system_header;
297 switch (token->type)
299 case CPP_NAME:
300 token->id_kind = C_ID_NONE;
301 token->keyword = RID_MAX;
303 tree decl;
305 int objc_force_identifier = objc_need_raw_identifier;
306 OBJC_NEED_RAW_IDENTIFIER (0);
308 if (C_IS_RESERVED_WORD (token->value))
310 enum rid rid_code = C_RID_CODE (token->value);
312 if (c_dialect_objc ())
314 if (!OBJC_IS_AT_KEYWORD (rid_code)
315 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
317 /* Return the canonical spelling for this keyword. */
318 token->value = ridpointers[(int) rid_code];
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
324 else
326 /* Return the canonical spelling for this keyword. */
327 token->value = ridpointers[(int) rid_code];
328 token->type = CPP_KEYWORD;
329 token->keyword = rid_code;
330 break;
334 decl = lookup_name (token->value);
335 if (decl)
337 if (TREE_CODE (decl) == TYPE_DECL)
339 token->id_kind = C_ID_TYPENAME;
340 break;
343 else if (c_dialect_objc ())
345 tree objc_interface_decl = objc_is_class_name (token->value);
346 /* Objective-C class names are in the same namespace as
347 variables and typedefs, and hence are shadowed by local
348 declarations. */
349 if (objc_interface_decl
350 && (global_bindings_p ()
351 || (!objc_force_identifier && !decl)))
353 token->value = objc_interface_decl;
354 token->id_kind = C_ID_CLASSNAME;
355 break;
359 token->id_kind = C_ID_ID;
360 break;
361 case CPP_AT_NAME:
362 /* This only happens in Objective-C; it must be a keyword. */
363 token->type = CPP_KEYWORD;
364 token->id_kind = C_ID_NONE;
365 token->keyword = C_RID_CODE (token->value);
366 break;
367 case CPP_COLON:
368 case CPP_COMMA:
369 case CPP_CLOSE_PAREN:
370 case CPP_SEMICOLON:
371 /* These tokens may affect the interpretation of any identifiers
372 following, if doing Objective-C. */
373 OBJC_NEED_RAW_IDENTIFIER (0);
374 token->id_kind = C_ID_NONE;
375 token->keyword = RID_MAX;
376 break;
377 default:
378 token->id_kind = C_ID_NONE;
379 token->keyword = RID_MAX;
380 break;
382 timevar_pop (TV_LEX);
385 /* Return a pointer to the next token from PARSER, reading it in if
386 necessary. */
388 static inline c_token *
389 c_parser_peek_token (c_parser *parser)
391 if (parser->tokens_avail == 0)
393 c_lex_one_token (&parser->tokens[0]);
394 parser->tokens_avail = 1;
396 return &parser->tokens[0];
399 /* Return true if the next token from PARSER has the indicated
400 TYPE. */
402 static inline bool
403 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
405 return c_parser_peek_token (parser)->type == type;
408 /* Return true if the next token from PARSER does not have the
409 indicated TYPE. */
411 static inline bool
412 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
414 return !c_parser_next_token_is (parser, type);
417 /* Return true if the next token from PARSER is the indicated
418 KEYWORD. */
420 static inline bool
421 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
423 c_token *token;
425 /* Peek at the next token. */
426 token = c_parser_peek_token (parser);
427 /* Check to see if it is the indicated keyword. */
428 return token->keyword == keyword;
431 /* Return true if TOKEN can start a type name,
432 false otherwise. */
433 static bool
434 c_token_starts_typename (c_token *token)
436 switch (token->type)
438 case CPP_NAME:
439 switch (token->id_kind)
441 case C_ID_ID:
442 return false;
443 case C_ID_TYPENAME:
444 return true;
445 case C_ID_CLASSNAME:
446 gcc_assert (c_dialect_objc ());
447 return true;
448 default:
449 gcc_unreachable ();
451 case CPP_KEYWORD:
452 switch (token->keyword)
454 case RID_UNSIGNED:
455 case RID_LONG:
456 case RID_SHORT:
457 case RID_SIGNED:
458 case RID_COMPLEX:
459 case RID_INT:
460 case RID_CHAR:
461 case RID_FLOAT:
462 case RID_DOUBLE:
463 case RID_VOID:
464 case RID_BOOL:
465 case RID_ENUM:
466 case RID_STRUCT:
467 case RID_UNION:
468 case RID_TYPEOF:
469 case RID_CONST:
470 case RID_VOLATILE:
471 case RID_RESTRICT:
472 case RID_ATTRIBUTE:
473 return true;
474 default:
475 return false;
477 case CPP_LESS:
478 if (c_dialect_objc ())
479 return true;
480 return false;
481 default:
482 return false;
486 /* Return true if the next token from PARSER can start a type name,
487 false otherwise. */
488 static inline bool
489 c_parser_next_token_starts_typename (c_parser *parser)
491 c_token *token = c_parser_peek_token (parser);
492 return c_token_starts_typename (token);
495 /* Return true if TOKEN can start declaration specifiers, false
496 otherwise. */
497 static bool
498 c_token_starts_declspecs (c_token *token)
500 switch (token->type)
502 case CPP_NAME:
503 switch (token->id_kind)
505 case C_ID_ID:
506 return false;
507 case C_ID_TYPENAME:
508 return true;
509 case C_ID_CLASSNAME:
510 gcc_assert (c_dialect_objc ());
511 return true;
512 default:
513 gcc_unreachable ();
515 case CPP_KEYWORD:
516 switch (token->keyword)
518 case RID_STATIC:
519 case RID_EXTERN:
520 case RID_REGISTER:
521 case RID_TYPEDEF:
522 case RID_INLINE:
523 case RID_AUTO:
524 case RID_THREAD:
525 case RID_UNSIGNED:
526 case RID_LONG:
527 case RID_SHORT:
528 case RID_SIGNED:
529 case RID_COMPLEX:
530 case RID_INT:
531 case RID_CHAR:
532 case RID_FLOAT:
533 case RID_DOUBLE:
534 case RID_VOID:
535 case RID_BOOL:
536 case RID_ENUM:
537 case RID_STRUCT:
538 case RID_UNION:
539 case RID_TYPEOF:
540 case RID_CONST:
541 case RID_VOLATILE:
542 case RID_RESTRICT:
543 case RID_ATTRIBUTE:
544 return true;
545 default:
546 return false;
548 case CPP_LESS:
549 if (c_dialect_objc ())
550 return true;
551 return false;
552 default:
553 return false;
557 /* Return true if the next token from PARSER can start declaration
558 specifiers, false otherwise. */
559 static inline bool
560 c_parser_next_token_starts_declspecs (c_parser *parser)
562 c_token *token = c_parser_peek_token (parser);
563 return c_token_starts_declspecs (token);
566 /* Return a pointer to the next-but-one token from PARSER, reading it
567 in if necessary. The next token is already read in. */
569 static c_token *
570 c_parser_peek_2nd_token (c_parser *parser)
572 if (parser->tokens_avail >= 2)
573 return &parser->tokens[1];
574 gcc_assert (parser->tokens_avail == 1);
575 gcc_assert (parser->tokens[0].type != CPP_EOF);
576 c_lex_one_token (&parser->tokens[1]);
577 parser->tokens_avail = 2;
578 return &parser->tokens[1];
581 /* Consume the next token from PARSER. */
583 static void
584 c_parser_consume_token (c_parser *parser)
586 if (parser->tokens_avail == 2)
587 parser->tokens[0] = parser->tokens[1];
588 else
590 gcc_assert (parser->tokens_avail == 1);
591 gcc_assert (parser->tokens[0].type != CPP_EOF);
593 parser->tokens_avail--;
596 /* Update the globals input_location and in_system_header from
597 TOKEN. */
598 static inline void
599 c_parser_set_source_position_from_token (c_token *token)
601 if (token->type != CPP_EOF)
603 input_location = token->location;
604 in_system_header = token->in_system_header;
608 /* Allocate a new parser. */
610 static c_parser *
611 c_parser_new (void)
613 /* Use local storage to lex the first token because loading a PCH
614 file may cause garbage collection. */
615 c_parser tparser;
616 c_parser *ret;
617 memset (&tparser, 0, sizeof tparser);
618 c_lex_one_token (&tparser.tokens[0]);
619 tparser.tokens_avail = 1;
620 ret = GGC_NEW (c_parser);
621 memcpy (ret, &tparser, sizeof tparser);
622 return ret;
625 /* Issue a diagnostic of the form
626 FILE:LINE: MESSAGE before TOKEN
627 where TOKEN is the next token in the input stream of PARSER.
628 MESSAGE (specified by the caller) is usually of the form "expected
629 OTHER-TOKEN".
631 Do not issue a diagnostic if still recovering from an error.
633 ??? This is taken from the C++ parser, but building up messages in
634 this way is not i18n-friendly and some other approach should be
635 used. */
637 static void
638 c_parser_error (c_parser *parser, const char *gmsgid)
640 c_token *token = c_parser_peek_token (parser);
641 if (parser->error)
642 return;
643 parser->error = true;
644 if (!gmsgid)
645 return;
646 /* This diagnostic makes more sense if it is tagged to the line of
647 the token we just peeked at. */
648 c_parser_set_source_position_from_token (token);
649 c_parse_error (gmsgid,
650 /* Because c_parse_error does not understand
651 CPP_KEYWORD, keywords are treated like
652 identifiers. */
653 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
654 token->value);
657 /* If the next token is of the indicated TYPE, consume it. Otherwise,
658 issue the error MSGID. If MSGID is NULL then a message has already
659 been produced and no message will be produced this time. Returns
660 true if found, false otherwise. */
662 static bool
663 c_parser_require (c_parser *parser,
664 enum cpp_ttype type,
665 const char *msgid)
667 if (c_parser_next_token_is (parser, type))
669 c_parser_consume_token (parser);
670 return true;
672 else
674 c_parser_error (parser, msgid);
675 return false;
679 /* If the next token is the indicated keyword, consume it. Otherwise,
680 issue the error MSGID. Returns true if found, false otherwise. */
682 static bool
683 c_parser_require_keyword (c_parser *parser,
684 enum rid keyword,
685 const char *msgid)
687 if (c_parser_next_token_is_keyword (parser, keyword))
689 c_parser_consume_token (parser);
690 return true;
692 else
694 c_parser_error (parser, msgid);
695 return false;
699 /* Like c_parser_require, except that tokens will be skipped until the
700 desired token is found. An error message is still produced if the
701 next token is not as expected. If MSGID is NULL then a message has
702 already been produced and no message will be produced this
703 time. */
705 static void
706 c_parser_skip_until_found (c_parser *parser,
707 enum cpp_ttype type,
708 const char *msgid)
710 unsigned nesting_depth = 0;
712 if (c_parser_require (parser, type, msgid))
713 return;
715 /* Skip tokens until the desired token is found. */
716 while (true)
718 /* Peek at the next token. */
719 c_token *token = c_parser_peek_token (parser);
720 /* If we've reached the token we want, consume it and stop. */
721 if (token->type == type && !nesting_depth)
723 c_parser_consume_token (parser);
724 break;
726 /* If we've run out of tokens, stop. */
727 if (token->type == CPP_EOF)
728 return;
729 if (token->type == CPP_OPEN_BRACE
730 || token->type == CPP_OPEN_PAREN
731 || token->type == CPP_OPEN_SQUARE)
732 ++nesting_depth;
733 else if (token->type == CPP_CLOSE_BRACE
734 || token->type == CPP_CLOSE_PAREN
735 || token->type == CPP_CLOSE_SQUARE)
737 if (nesting_depth-- == 0)
738 break;
740 /* Consume this token. */
741 c_parser_consume_token (parser);
743 parser->error = false;
746 /* Skip tokens until the end of a parameter is found, but do not
747 consume the comma, semicolon or closing delimiter. */
749 static void
750 c_parser_skip_to_end_of_parameter (c_parser *parser)
752 unsigned nesting_depth = 0;
754 while (true)
756 c_token *token = c_parser_peek_token (parser);
757 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
758 && !nesting_depth)
759 break;
760 /* If we've run out of tokens, stop. */
761 if (token->type == CPP_EOF)
762 return;
763 if (token->type == CPP_OPEN_BRACE
764 || token->type == CPP_OPEN_PAREN
765 || token->type == CPP_OPEN_SQUARE)
766 ++nesting_depth;
767 else if (token->type == CPP_CLOSE_BRACE
768 || token->type == CPP_CLOSE_PAREN
769 || token->type == CPP_CLOSE_SQUARE)
771 if (nesting_depth-- == 0)
772 break;
774 /* Consume this token. */
775 c_parser_consume_token (parser);
777 parser->error = false;
780 /* Skip tokens until we have consumed an entire block, or until we
781 have consumed a non-nested ';'. */
783 static void
784 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
786 unsigned nesting_depth = 0;
788 while (true)
790 c_token *token;
792 /* Peek at the next token. */
793 token = c_parser_peek_token (parser);
794 /* If we've run out of tokens, stop. */
795 if (token->type == CPP_EOF)
796 return;
797 /* If the next token is a ';', we have reached the end of the
798 statement. */
799 if (token->type == CPP_SEMICOLON && !nesting_depth)
801 /* Consume the ';'. */
802 c_parser_consume_token (parser);
803 break;
805 /* If the next token is a non-nested '}', then we have reached
806 the end of the current block. */
807 if (token->type == CPP_CLOSE_BRACE
808 && (nesting_depth == 0 || --nesting_depth == 0))
810 c_parser_consume_token (parser);
811 break;
813 /* If it the next token is a '{', then we are entering a new
814 block. Consume the entire block. */
815 if (token->type == CPP_OPEN_BRACE)
816 ++nesting_depth;
817 c_parser_consume_token (parser);
819 parser->error = false;
823 /* Save the warning flags which are controlled by __extension__. */
825 static inline int
826 disable_extension_diagnostics (void)
828 int ret = (pedantic
829 | (warn_pointer_arith << 1)
830 | (warn_traditional << 2)
831 | (flag_iso << 3));
832 pedantic = 0;
833 warn_pointer_arith = 0;
834 warn_traditional = 0;
835 flag_iso = 0;
836 return ret;
839 /* Restore the warning flags which are controlled by __extension__.
840 FLAGS is the return value from disable_extension_diagnostics. */
842 static inline void
843 restore_extension_diagnostics (int flags)
845 pedantic = flags & 1;
846 warn_pointer_arith = (flags >> 1) & 1;
847 warn_traditional = (flags >> 2) & 1;
848 flag_iso = (flags >> 3) & 1;
851 /* Possibly kinds of declarator to parse. */
852 typedef enum c_dtr_syn {
853 /* A normal declarator with an identifier. */
854 C_DTR_NORMAL,
855 /* An abstract declarator (maybe empty). */
856 C_DTR_ABSTRACT,
857 /* A parameter declarator: may be either, but after a type name does
858 not redeclare a typedef name as an identifier if it can
859 alternatively be interpreted as a typedef name; see DR#009,
860 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
861 following DR#249. For example, given a typedef T, "int T" and
862 "int *T" are valid parameter declarations redeclaring T, while
863 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
864 abstract declarators rather than involving redundant parentheses;
865 the same applies with attributes inside the parentheses before
866 "T". */
867 C_DTR_PARM
868 } c_dtr_syn;
870 static void c_parser_external_declaration (c_parser *);
871 static void c_parser_asm_definition (c_parser *);
872 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
873 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
874 bool);
875 static struct c_typespec c_parser_enum_specifier (c_parser *);
876 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
877 static tree c_parser_struct_declaration (c_parser *);
878 static struct c_typespec c_parser_typeof_specifier (c_parser *);
879 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
880 bool *);
881 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
882 c_dtr_syn, bool *);
883 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
884 bool,
885 struct c_declarator *);
886 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
887 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
888 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
889 static tree c_parser_simple_asm_expr (c_parser *);
890 static tree c_parser_attributes (c_parser *);
891 static struct c_type_name *c_parser_type_name (c_parser *);
892 static struct c_expr c_parser_initializer (c_parser *);
893 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
894 static void c_parser_initelt (c_parser *);
895 static void c_parser_initval (c_parser *, struct c_expr *);
896 static tree c_parser_compound_statement (c_parser *);
897 static void c_parser_compound_statement_nostart (c_parser *);
898 static void c_parser_label (c_parser *);
899 static void c_parser_statement (c_parser *);
900 static void c_parser_statement_after_labels (c_parser *);
901 static void c_parser_if_statement (c_parser *);
902 static void c_parser_switch_statement (c_parser *);
903 static void c_parser_while_statement (c_parser *);
904 static void c_parser_do_statement (c_parser *);
905 static void c_parser_for_statement (c_parser *);
906 static tree c_parser_asm_statement (c_parser *);
907 static tree c_parser_asm_operands (c_parser *);
908 static tree c_parser_asm_clobbers (c_parser *);
909 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
910 static struct c_expr c_parser_conditional_expression (c_parser *,
911 struct c_expr *);
912 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
913 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
914 static struct c_expr c_parser_unary_expression (c_parser *);
915 static struct c_expr c_parser_sizeof_expression (c_parser *);
916 static struct c_expr c_parser_alignof_expression (c_parser *);
917 static struct c_expr c_parser_postfix_expression (c_parser *);
918 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
919 struct c_type_name *);
920 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
921 struct c_expr);
922 static struct c_expr c_parser_expression (c_parser *);
923 static tree c_parser_expr_list (c_parser *);
925 /* These Objective-C parser functions are only ever called when
926 compiling Objective-C. */
927 static void c_parser_objc_class_definition (c_parser *);
928 static void c_parser_objc_class_instance_variables (c_parser *);
929 static void c_parser_objc_class_declaration (c_parser *);
930 static void c_parser_objc_alias_declaration (c_parser *);
931 static void c_parser_objc_protocol_definition (c_parser *);
932 static enum tree_code c_parser_objc_method_type (c_parser *);
933 static void c_parser_objc_method_definition (c_parser *);
934 static void c_parser_objc_methodprotolist (c_parser *);
935 static void c_parser_objc_methodproto (c_parser *);
936 static tree c_parser_objc_method_decl (c_parser *);
937 static tree c_parser_objc_type_name (c_parser *);
938 static tree c_parser_objc_protocol_refs (c_parser *);
939 static void c_parser_objc_try_catch_statement (c_parser *);
940 static void c_parser_objc_synchronized_statement (c_parser *);
941 static tree c_parser_objc_selector (c_parser *);
942 static tree c_parser_objc_selector_arg (c_parser *);
943 static tree c_parser_objc_receiver (c_parser *);
944 static tree c_parser_objc_message_args (c_parser *);
945 static tree c_parser_objc_keywordexpr (c_parser *);
947 /* Parse a translation unit (C90 6.7, C99 6.9).
949 translation-unit:
950 external-declarations
952 external-declarations:
953 external-declaration
954 external-declarations external-declaration
956 GNU extensions:
958 translation-unit:
959 empty
962 static void
963 c_parser_translation_unit (c_parser *parser)
965 if (c_parser_next_token_is (parser, CPP_EOF))
967 if (pedantic)
968 pedwarn ("ISO C forbids an empty source file");
970 else
972 void *obstack_position = obstack_alloc (&parser_obstack, 0);
975 ggc_collect ();
976 c_parser_external_declaration (parser);
977 obstack_free (&parser_obstack, obstack_position);
979 while (c_parser_next_token_is_not (parser, CPP_EOF));
983 /* Parse an external declaration (C90 6.7, C99 6.9).
985 external-declaration:
986 function-definition
987 declaration
989 GNU extensions:
991 external-declaration:
992 asm-definition
994 __extension__ external-declaration
996 Objective-C:
998 external-declaration:
999 objc-class-definition
1000 objc-class-declaration
1001 objc-alias-declaration
1002 objc-protocol-definition
1003 objc-method-definition
1004 @end
1007 static void
1008 c_parser_external_declaration (c_parser *parser)
1010 int ext;
1011 switch (c_parser_peek_token (parser)->type)
1013 case CPP_KEYWORD:
1014 switch (c_parser_peek_token (parser)->keyword)
1016 case RID_EXTENSION:
1017 ext = disable_extension_diagnostics ();
1018 c_parser_consume_token (parser);
1019 c_parser_external_declaration (parser);
1020 restore_extension_diagnostics (ext);
1021 break;
1022 case RID_ASM:
1023 c_parser_asm_definition (parser);
1024 break;
1025 case RID_AT_INTERFACE:
1026 case RID_AT_IMPLEMENTATION:
1027 gcc_assert (c_dialect_objc ());
1028 c_parser_objc_class_definition (parser);
1029 break;
1030 case RID_AT_CLASS:
1031 gcc_assert (c_dialect_objc ());
1032 c_parser_objc_class_declaration (parser);
1033 break;
1034 case RID_AT_ALIAS:
1035 gcc_assert (c_dialect_objc ());
1036 c_parser_objc_alias_declaration (parser);
1037 break;
1038 case RID_AT_PROTOCOL:
1039 gcc_assert (c_dialect_objc ());
1040 c_parser_objc_protocol_definition (parser);
1041 break;
1042 case RID_AT_END:
1043 gcc_assert (c_dialect_objc ());
1044 c_parser_consume_token (parser);
1045 objc_finish_implementation ();
1046 break;
1047 default:
1048 goto decl_or_fndef;
1050 break;
1051 case CPP_SEMICOLON:
1052 if (pedantic)
1053 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1054 c_parser_consume_token (parser);
1055 break;
1056 case CPP_PLUS:
1057 case CPP_MINUS:
1058 if (c_dialect_objc ())
1060 c_parser_objc_method_definition (parser);
1061 break;
1063 /* Else fall through, and yield a syntax error trying to parse
1064 as a declaration or function definition. */
1065 default:
1066 decl_or_fndef:
1067 /* A declaration or a function definition. We can only tell
1068 which after parsing the declaration specifiers, if any, and
1069 the first declarator. */
1070 c_parser_declaration_or_fndef (parser, true, true, false, true);
1071 break;
1075 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1076 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1077 accepted; otherwise (old-style parameter declarations) only other
1078 declarations are accepted. If NESTED is true, we are inside a
1079 function or parsing old-style parameter declarations; any functions
1080 encountered are nested functions and declaration specifiers are
1081 required; otherwise we are at top level and functions are normal
1082 functions and declaration specifiers may be optional. If EMPTY_OK
1083 is true, empty declarations are OK (subject to all other
1084 constraints); otherwise (old-style parameter declarations) they are
1085 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1086 may start with attributes; otherwise they may not.
1088 declaration:
1089 declaration-specifiers init-declarator-list[opt] ;
1091 function-definition:
1092 declaration-specifiers[opt] declarator declaration-list[opt]
1093 compound-statement
1095 declaration-list:
1096 declaration
1097 declaration-list declaration
1099 init-declarator-list:
1100 init-declarator
1101 init-declarator-list , init-declarator
1103 init-declarator:
1104 declarator simple-asm-expr[opt] attributes[opt]
1105 declarator simple-asm-expr[opt] attributes[opt] = initializer
1107 GNU extensions:
1109 nested-function-definition:
1110 declaration-specifiers declarator declaration-list[opt]
1111 compound-statement
1113 The simple-asm-expr and attributes are GNU extensions.
1115 This function does not handle __extension__; that is handled in its
1116 callers. ??? Following the old parser, __extension__ may start
1117 external declarations, declarations in functions and declarations
1118 at the start of "for" loops, but not old-style parameter
1119 declarations.
1121 C99 requires declaration specifiers in a function definition; the
1122 absence is diagnosed through the diagnosis of implicit int. In GNU
1123 C we also allow but diagnose declarations without declaration
1124 specifiers, but only at top level (elsewhere they conflict with
1125 other syntax). */
1127 static void
1128 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1129 bool nested, bool start_attr_ok)
1131 struct c_declspecs *specs;
1132 tree prefix_attrs;
1133 tree all_prefix_attrs;
1134 bool diagnosed_no_specs = false;
1135 specs = build_null_declspecs ();
1136 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1137 if (parser->error)
1139 c_parser_skip_to_end_of_block_or_statement (parser);
1140 return;
1142 if (nested && !specs->declspecs_seen_p)
1144 c_parser_error (parser, "expected declaration specifiers");
1145 c_parser_skip_to_end_of_block_or_statement (parser);
1146 return;
1148 finish_declspecs (specs);
1149 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1151 if (empty_ok)
1152 shadow_tag (specs);
1153 else
1155 shadow_tag_warned (specs, 1);
1156 pedwarn ("empty declaration");
1158 c_parser_consume_token (parser);
1159 return;
1161 pending_xref_error ();
1162 prefix_attrs = specs->attrs;
1163 all_prefix_attrs = prefix_attrs;
1164 specs->attrs = NULL_TREE;
1165 while (true)
1167 struct c_declarator *declarator;
1168 bool dummy = false;
1169 tree fnbody;
1170 /* Declaring either one or more declarators (in which case we
1171 should diagnose if there were no declaration specifiers) or a
1172 function definition (in which case the diagnostic for
1173 implicit int suffices). */
1174 declarator = c_parser_declarator (parser, specs->type_seen_p,
1175 C_DTR_NORMAL, &dummy);
1176 if (declarator == NULL)
1178 c_parser_skip_to_end_of_block_or_statement (parser);
1179 return;
1181 if (c_parser_next_token_is (parser, CPP_EQ)
1182 || c_parser_next_token_is (parser, CPP_COMMA)
1183 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1184 || c_parser_next_token_is_keyword (parser, RID_ASM)
1185 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1187 tree asm_name = NULL_TREE;
1188 tree postfix_attrs = NULL_TREE;
1189 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1191 diagnosed_no_specs = true;
1192 pedwarn ("data definition has no type or storage class");
1194 /* Having seen a data definition, there cannot now be a
1195 function definition. */
1196 fndef_ok = false;
1197 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1198 asm_name = c_parser_simple_asm_expr (parser);
1199 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1200 postfix_attrs = c_parser_attributes (parser);
1201 if (c_parser_next_token_is (parser, CPP_EQ))
1203 tree d;
1204 struct c_expr init;
1205 c_parser_consume_token (parser);
1206 /* The declaration of the variable is in effect while
1207 its initializer is parsed. */
1208 d = start_decl (declarator, specs, true,
1209 chainon (postfix_attrs, all_prefix_attrs));
1210 if (!d)
1211 d = error_mark_node;
1212 start_init (d, asm_name, global_bindings_p ());
1213 init = c_parser_initializer (parser);
1214 finish_init ();
1215 if (d != error_mark_node)
1217 maybe_warn_string_init (TREE_TYPE (d), init);
1218 finish_decl (d, init.value, asm_name);
1221 else
1223 tree d = start_decl (declarator, specs, false,
1224 chainon (postfix_attrs,
1225 all_prefix_attrs));
1226 if (d)
1227 finish_decl (d, NULL_TREE, asm_name);
1229 if (c_parser_next_token_is (parser, CPP_COMMA))
1231 c_parser_consume_token (parser);
1232 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1233 all_prefix_attrs = chainon (c_parser_attributes (parser),
1234 prefix_attrs);
1235 else
1236 all_prefix_attrs = prefix_attrs;
1237 continue;
1239 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1241 c_parser_consume_token (parser);
1242 return;
1244 else
1246 c_parser_error (parser, "expected %<,%> or %<;%>");
1247 c_parser_skip_to_end_of_block_or_statement (parser);
1248 return;
1251 else if (!fndef_ok)
1253 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1254 "%<asm%> or %<__attribute__%>");
1255 c_parser_skip_to_end_of_block_or_statement (parser);
1256 return;
1258 /* Function definition (nested or otherwise). */
1259 if (nested)
1261 if (pedantic)
1262 pedwarn ("ISO C forbids nested functions");
1263 push_function_context ();
1265 if (!start_function (specs, declarator, all_prefix_attrs))
1267 /* This can appear in many cases looking nothing like a
1268 function definition, so we don't give a more specific
1269 error suggesting there was one. */
1270 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1271 "or %<__attribute__%>");
1272 if (nested)
1273 pop_function_context ();
1274 break;
1276 /* Parse old-style parameter declarations. ??? Attributes are
1277 not allowed to start declaration specifiers here because of a
1278 syntax conflict between a function declaration with attribute
1279 suffix and a function definition with an attribute prefix on
1280 first old-style parameter declaration. Following the old
1281 parser, they are not accepted on subsequent old-style
1282 parameter declarations either. However, there is no
1283 ambiguity after the first declaration, nor indeed on the
1284 first as long as we don't allow postfix attributes after a
1285 declarator with a nonempty identifier list in a definition;
1286 and postfix attributes have never been accepted here in
1287 function definitions either. */
1288 while (c_parser_next_token_is_not (parser, CPP_EOF)
1289 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1290 c_parser_declaration_or_fndef (parser, false, false, true, false);
1291 DECL_SOURCE_LOCATION (current_function_decl)
1292 = c_parser_peek_token (parser)->location;
1293 store_parm_decls ();
1294 fnbody = c_parser_compound_statement (parser);
1295 if (nested)
1297 tree decl = current_function_decl;
1298 add_stmt (fnbody);
1299 finish_function ();
1300 pop_function_context ();
1301 add_stmt (build_stmt (DECL_EXPR, decl));
1303 else
1305 add_stmt (fnbody);
1306 finish_function ();
1308 break;
1312 /* Parse an asm-definition (asm() outside a function body). This is a
1313 GNU extension.
1315 asm-definition:
1316 simple-asm-expr ;
1319 static void
1320 c_parser_asm_definition (c_parser *parser)
1322 tree asm_str = c_parser_simple_asm_expr (parser);
1323 /* ??? This only works sensibly in the presence of
1324 -fno-unit-at-a-time; file-scope asms really need to be passed to
1325 cgraph which needs to preserve the order of functions and
1326 file-scope asms. */
1327 if (asm_str)
1328 assemble_asm (asm_str);
1329 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1332 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1333 6.7), adding them to SPECS (which may already include some).
1334 Storage class specifiers are accepted iff SCSPEC_OK; type
1335 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1336 the start iff START_ATTR_OK.
1338 declaration-specifiers:
1339 storage-class-specifier declaration-specifiers[opt]
1340 type-specifier declaration-specifiers[opt]
1341 type-qualifier declaration-specifiers[opt]
1342 function-specifier declaration-specifiers[opt]
1344 Function specifiers (inline) are from C99, and are currently
1345 handled as storage class specifiers, as is __thread.
1347 C90 6.5.1, C99 6.7.1:
1348 storage-class-specifier:
1349 typedef
1350 extern
1351 static
1352 auto
1353 register
1355 C99 6.7.4:
1356 function-specifier:
1357 inline
1359 C90 6.5.2, C99 6.7.2:
1360 type-specifier:
1361 void
1362 char
1363 short
1365 long
1366 float
1367 double
1368 signed
1369 unsigned
1370 _Bool
1371 _Complex
1372 [_Imaginary removed in C99 TC2]
1373 struct-or-union-specifier
1374 enum-specifier
1375 typedef-name
1377 (_Bool and _Complex are new in C99.)
1379 C90 6.5.3, C99 6.7.3:
1381 type-qualifier:
1382 const
1383 restrict
1384 volatile
1386 (restrict is new in C99.)
1388 GNU extensions:
1390 declaration-specifiers:
1391 attributes declaration-specifiers[opt]
1393 storage-class-specifier:
1394 __thread
1396 type-specifier:
1397 typeof-specifier
1399 Objective-C:
1401 type-specifier:
1402 class-name objc-protocol-refs[opt]
1403 typedef-name objc-protocol-refs
1404 objc-protocol-refs
1407 static void
1408 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1409 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1411 bool attrs_ok = start_attr_ok;
1412 bool seen_type = specs->type_seen_p;
1413 while (c_parser_next_token_is (parser, CPP_NAME)
1414 || c_parser_next_token_is (parser, CPP_KEYWORD)
1415 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1417 struct c_typespec t;
1418 tree attrs;
1419 if (c_parser_next_token_is (parser, CPP_NAME))
1421 tree value = c_parser_peek_token (parser)->value;
1422 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1423 /* This finishes the specifiers unless a type name is OK, it
1424 is declared as a type name and a type name hasn't yet
1425 been seen. */
1426 if (!typespec_ok || seen_type
1427 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1428 break;
1429 c_parser_consume_token (parser);
1430 seen_type = true;
1431 attrs_ok = true;
1432 if (kind == C_ID_TYPENAME
1433 && (!c_dialect_objc ()
1434 || c_parser_next_token_is_not (parser, CPP_LESS)))
1436 t.kind = ctsk_typedef;
1437 /* For a typedef name, record the meaning, not the name.
1438 In case of 'foo foo, bar;'. */
1439 t.spec = lookup_name (value);
1441 else
1443 tree proto = NULL_TREE;
1444 gcc_assert (c_dialect_objc ());
1445 t.kind = ctsk_objc;
1446 if (c_parser_next_token_is (parser, CPP_LESS))
1447 proto = c_parser_objc_protocol_refs (parser);
1448 t.spec = objc_get_protocol_qualified_type (value, proto);
1450 declspecs_add_type (specs, t);
1451 continue;
1453 if (c_parser_next_token_is (parser, CPP_LESS))
1455 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1456 nisse@lysator.liu.se. */
1457 tree proto;
1458 gcc_assert (c_dialect_objc ());
1459 if (!typespec_ok || seen_type)
1460 break;
1461 proto = c_parser_objc_protocol_refs (parser);
1462 t.kind = ctsk_objc;
1463 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1464 declspecs_add_type (specs, t);
1465 continue;
1467 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1468 switch (c_parser_peek_token (parser)->keyword)
1470 case RID_STATIC:
1471 case RID_EXTERN:
1472 case RID_REGISTER:
1473 case RID_TYPEDEF:
1474 case RID_INLINE:
1475 case RID_AUTO:
1476 case RID_THREAD:
1477 if (!scspec_ok)
1478 goto out;
1479 attrs_ok = true;
1480 /* TODO: Distinguish between function specifiers (inline)
1481 and storage class specifiers, either here or in
1482 declspecs_add_scspec. */
1483 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1484 c_parser_consume_token (parser);
1485 break;
1486 case RID_UNSIGNED:
1487 case RID_LONG:
1488 case RID_SHORT:
1489 case RID_SIGNED:
1490 case RID_COMPLEX:
1491 case RID_INT:
1492 case RID_CHAR:
1493 case RID_FLOAT:
1494 case RID_DOUBLE:
1495 case RID_VOID:
1496 case RID_BOOL:
1497 if (!typespec_ok)
1498 goto out;
1499 attrs_ok = true;
1500 seen_type = true;
1501 OBJC_NEED_RAW_IDENTIFIER (1);
1502 t.kind = ctsk_resword;
1503 t.spec = c_parser_peek_token (parser)->value;
1504 declspecs_add_type (specs, t);
1505 c_parser_consume_token (parser);
1506 break;
1507 case RID_ENUM:
1508 if (!typespec_ok)
1509 goto out;
1510 attrs_ok = true;
1511 seen_type = true;
1512 t = c_parser_enum_specifier (parser);
1513 declspecs_add_type (specs, t);
1514 break;
1515 case RID_STRUCT:
1516 case RID_UNION:
1517 if (!typespec_ok)
1518 goto out;
1519 attrs_ok = true;
1520 seen_type = true;
1521 t = c_parser_struct_or_union_specifier (parser);
1522 declspecs_add_type (specs, t);
1523 break;
1524 case RID_TYPEOF:
1525 /* ??? The old parser rejected typeof after other type
1526 specifiers, but is a syntax error the best way of
1527 handling this? */
1528 if (!typespec_ok || seen_type)
1529 goto out;
1530 attrs_ok = true;
1531 seen_type = true;
1532 t = c_parser_typeof_specifier (parser);
1533 declspecs_add_type (specs, t);
1534 break;
1535 case RID_CONST:
1536 case RID_VOLATILE:
1537 case RID_RESTRICT:
1538 attrs_ok = true;
1539 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1540 c_parser_consume_token (parser);
1541 break;
1542 case RID_ATTRIBUTE:
1543 if (!attrs_ok)
1544 goto out;
1545 attrs = c_parser_attributes (parser);
1546 declspecs_add_attrs (specs, attrs);
1547 break;
1548 default:
1549 goto out;
1552 out: ;
1555 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1557 enum-specifier:
1558 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1559 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1560 enum attributes[opt] identifier
1562 The form with trailing comma is new in C99. The forms with
1563 attributes are GNU extensions. In GNU C, we accept any expression
1564 without commas in the syntax (assignment expressions, not just
1565 conditional expressions); assignment expressions will be diagnosed
1566 as non-constant.
1568 enumerator-list:
1569 enumerator
1570 enumerator-list , enumerator
1572 enumerator:
1573 enumeration-constant
1574 enumeration-constant = constant-expression
1577 static struct c_typespec
1578 c_parser_enum_specifier (c_parser *parser)
1580 struct c_typespec ret;
1581 tree attrs;
1582 tree ident = NULL_TREE;
1583 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1584 c_parser_consume_token (parser);
1585 attrs = c_parser_attributes (parser);
1586 if (c_parser_next_token_is (parser, CPP_NAME))
1588 ident = c_parser_peek_token (parser)->value;
1589 c_parser_consume_token (parser);
1591 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1593 /* Parse an enum definition. */
1594 tree type = start_enum (ident);
1595 tree postfix_attrs;
1596 /* We chain the enumerators in reverse order, then put them in
1597 forward order at the end. */
1598 tree values = NULL_TREE;
1599 c_parser_consume_token (parser);
1600 while (true)
1602 tree enum_id;
1603 tree enum_value;
1604 tree enum_decl;
1605 bool seen_comma;
1606 if (c_parser_next_token_is_not (parser, CPP_NAME))
1608 c_parser_error (parser, "expected identifier");
1609 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1610 values = error_mark_node;
1611 break;
1613 enum_id = c_parser_peek_token (parser)->value;
1614 c_parser_consume_token (parser);
1615 if (c_parser_next_token_is (parser, CPP_EQ))
1617 c_parser_consume_token (parser);
1618 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1620 else
1621 enum_value = NULL_TREE;
1622 enum_decl = build_enumerator (enum_id, enum_value);
1623 TREE_CHAIN (enum_decl) = values;
1624 values = enum_decl;
1625 seen_comma = false;
1626 if (c_parser_next_token_is (parser, CPP_COMMA))
1628 seen_comma = true;
1629 c_parser_consume_token (parser);
1631 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1633 if (seen_comma && pedantic && !flag_isoc99)
1634 pedwarn ("comma at end of enumerator list");
1635 c_parser_consume_token (parser);
1636 break;
1638 if (!seen_comma)
1640 c_parser_error (parser, "expected %<,%> or %<}%>");
1641 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1642 values = error_mark_node;
1643 break;
1646 postfix_attrs = c_parser_attributes (parser);
1647 ret.spec = finish_enum (type, nreverse (values),
1648 chainon (attrs, postfix_attrs));
1649 ret.kind = ctsk_tagdef;
1650 return ret;
1652 else if (!ident)
1654 c_parser_error (parser, "expected %<{%>");
1655 ret.spec = error_mark_node;
1656 ret.kind = ctsk_tagref;
1657 return ret;
1659 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1660 /* In ISO C, enumerated types can be referred to only if already
1661 defined. */
1662 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1663 pedwarn ("ISO C forbids forward references to %<enum%> types");
1664 return ret;
1667 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1669 struct-or-union-specifier:
1670 struct-or-union attributes[opt] identifier[opt]
1671 { struct-contents } attributes[opt]
1672 struct-or-union attributes[opt] identifier
1674 struct-contents:
1675 struct-declaration-list
1677 struct-declaration-list:
1678 struct-declaration ;
1679 struct-declaration-list struct-declaration ;
1681 GNU extensions:
1683 struct-contents:
1684 empty
1685 struct-declaration
1686 struct-declaration-list struct-declaration
1688 struct-declaration-list:
1689 struct-declaration-list ;
1692 (Note that in the syntax here, unlike that in ISO C, the semicolons
1693 are included here rather than in struct-declaration, in order to
1694 describe the syntax with extra semicolons and missing semicolon at
1695 end.)
1697 Objective-C:
1699 struct-declaration-list:
1700 @defs ( class-name )
1702 (Note this does not include a trailing semicolon, but can be
1703 followed by further declarations, and gets a pedwarn-if-pedantic
1704 when followed by a semicolon.) */
1706 static struct c_typespec
1707 c_parser_struct_or_union_specifier (c_parser *parser)
1709 struct c_typespec ret;
1710 tree attrs;
1711 tree ident = NULL_TREE;
1712 enum tree_code code;
1713 switch (c_parser_peek_token (parser)->keyword)
1715 case RID_STRUCT:
1716 code = RECORD_TYPE;
1717 break;
1718 case RID_UNION:
1719 code = UNION_TYPE;
1720 break;
1721 default:
1722 gcc_unreachable ();
1724 c_parser_consume_token (parser);
1725 attrs = c_parser_attributes (parser);
1726 if (c_parser_next_token_is (parser, CPP_NAME))
1728 ident = c_parser_peek_token (parser)->value;
1729 c_parser_consume_token (parser);
1731 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1733 /* Parse a struct or union definition. Start the scope of the
1734 tag before parsing components. */
1735 tree type = start_struct (code, ident);
1736 tree postfix_attrs;
1737 /* We chain the components in reverse order, then put them in
1738 forward order at the end. Each struct-declaration may
1739 declare multiple components (comma-separated), so we must use
1740 chainon to join them, although when parsing each
1741 struct-declaration we can use TREE_CHAIN directly.
1743 The theory behind all this is that there will be more
1744 semicolon separated fields than comma separated fields, and
1745 so we'll be minimizing the number of node traversals required
1746 by chainon. */
1747 tree contents = NULL_TREE;
1748 c_parser_consume_token (parser);
1749 /* Handle the Objective-C @defs construct,
1750 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1751 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1753 tree name;
1754 gcc_assert (c_dialect_objc ());
1755 c_parser_consume_token (parser);
1756 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1757 goto end_at_defs;
1758 if (c_parser_next_token_is (parser, CPP_NAME)
1759 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1761 name = c_parser_peek_token (parser)->value;
1762 c_parser_consume_token (parser);
1764 else
1766 c_parser_error (parser, "expected class name");
1767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1768 goto end_at_defs;
1770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1771 "expected %<)%>");
1772 contents = nreverse (objc_get_class_ivars (name));
1774 end_at_defs:
1775 /* Parse the struct-declarations and semicolons. Problems with
1776 semicolons are diagnosed here; empty structures are diagnosed
1777 elsewhere. */
1778 while (true)
1780 tree decls;
1781 /* Parse any stray semicolon. */
1782 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1784 if (pedantic)
1785 pedwarn ("extra semicolon in struct or union specified");
1786 c_parser_consume_token (parser);
1787 continue;
1789 /* Stop if at the end of the struct or union contents. */
1790 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1792 c_parser_consume_token (parser);
1793 break;
1795 /* Parse some comma-separated declarations, but not the
1796 trailing semicolon if any. */
1797 decls = c_parser_struct_declaration (parser);
1798 contents = chainon (decls, contents);
1799 /* If no semicolon follows, either we have a parse error or
1800 are at the end of the struct or union and should
1801 pedwarn. */
1802 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1803 c_parser_consume_token (parser);
1804 else
1806 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1807 pedwarn ("no semicolon at end of struct or union");
1808 else
1810 c_parser_error (parser, "expected %<;%>");
1811 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1812 break;
1816 postfix_attrs = c_parser_attributes (parser);
1817 ret.spec = finish_struct (type, nreverse (contents),
1818 chainon (attrs, postfix_attrs));
1819 ret.kind = ctsk_tagdef;
1820 return ret;
1822 else if (!ident)
1824 c_parser_error (parser, "expected %<{%>");
1825 ret.spec = error_mark_node;
1826 ret.kind = ctsk_tagref;
1827 return ret;
1829 ret = parser_xref_tag (code, ident);
1830 return ret;
1833 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1834 the trailing semicolon.
1836 struct-declaration:
1837 specifier-qualifier-list struct-declarator-list
1839 specifier-qualifier-list:
1840 type-specifier specifier-qualifier-list[opt]
1841 type-qualifier specifier-qualifier-list[opt]
1842 attributes specifier-qualifier-list[opt]
1844 struct-declarator-list:
1845 struct-declarator
1846 struct-declarator-list , attributes[opt] struct-declarator
1848 struct-declarator:
1849 declarator attributes[opt]
1850 declarator[opt] : constant-expression attributes[opt]
1852 GNU extensions:
1854 struct-declaration:
1855 __extension__ struct-declaration
1856 specifier-qualifier-list
1858 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1859 of attributes where shown is a GNU extension. In GNU C, we accept
1860 any expression without commas in the syntax (assignment
1861 expressions, not just conditional expressions); assignment
1862 expressions will be diagnosed as non-constant. */
1864 static tree
1865 c_parser_struct_declaration (c_parser *parser)
1867 struct c_declspecs *specs;
1868 tree prefix_attrs;
1869 tree all_prefix_attrs;
1870 tree decls;
1871 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1873 int ext;
1874 tree decl;
1875 ext = disable_extension_diagnostics ();
1876 c_parser_consume_token (parser);
1877 decl = c_parser_struct_declaration (parser);
1878 restore_extension_diagnostics (ext);
1879 return decl;
1881 specs = build_null_declspecs ();
1882 c_parser_declspecs (parser, specs, false, true, true);
1883 if (parser->error)
1884 return NULL_TREE;
1885 if (!specs->declspecs_seen_p)
1887 c_parser_error (parser, "expected specifier-qualifier-list");
1888 return NULL_TREE;
1890 finish_declspecs (specs);
1891 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1893 tree ret;
1894 if (!specs->type_seen_p)
1896 if (pedantic)
1897 pedwarn ("ISO C forbids member declarations with no members");
1898 shadow_tag_warned (specs, pedantic);
1899 ret = NULL_TREE;
1901 else
1903 /* Support for unnamed structs or unions as members of
1904 structs or unions (which is [a] useful and [b] supports
1905 MS P-SDK). */
1906 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1908 return ret;
1910 pending_xref_error ();
1911 prefix_attrs = specs->attrs;
1912 all_prefix_attrs = prefix_attrs;
1913 specs->attrs = NULL_TREE;
1914 decls = NULL_TREE;
1915 while (true)
1917 /* Declaring one or more declarators or un-named bit-fields. */
1918 struct c_declarator *declarator;
1919 bool dummy = false;
1920 if (c_parser_next_token_is (parser, CPP_COLON))
1921 declarator = build_id_declarator (NULL_TREE);
1922 else
1923 declarator = c_parser_declarator (parser, specs->type_seen_p,
1924 C_DTR_NORMAL, &dummy);
1925 if (declarator == NULL)
1927 c_parser_skip_to_end_of_block_or_statement (parser);
1928 break;
1930 if (c_parser_next_token_is (parser, CPP_COLON)
1931 || c_parser_next_token_is (parser, CPP_COMMA)
1932 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1933 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1934 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1936 tree postfix_attrs = NULL_TREE;
1937 tree width = NULL_TREE;
1938 tree d;
1939 if (c_parser_next_token_is (parser, CPP_COLON))
1941 c_parser_consume_token (parser);
1942 width = c_parser_expr_no_commas (parser, NULL).value;
1944 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1945 postfix_attrs = c_parser_attributes (parser);
1946 d = grokfield (declarator, specs, width);
1947 decl_attributes (&d, chainon (postfix_attrs,
1948 all_prefix_attrs), 0);
1949 TREE_CHAIN (d) = decls;
1950 decls = d;
1951 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1952 all_prefix_attrs = chainon (c_parser_attributes (parser),
1953 prefix_attrs);
1954 else
1955 all_prefix_attrs = prefix_attrs;
1956 if (c_parser_next_token_is (parser, CPP_COMMA))
1957 c_parser_consume_token (parser);
1958 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
1959 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1961 /* Semicolon consumed in caller. */
1962 break;
1964 else
1966 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
1967 break;
1970 else
1972 c_parser_error (parser,
1973 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
1974 "%<__attribute__%>");
1975 break;
1978 return decls;
1981 /* Parse a typeof specifier (a GNU extension).
1983 typeof-specifier:
1984 typeof ( expression )
1985 typeof ( type-name )
1988 static struct c_typespec
1989 c_parser_typeof_specifier (c_parser *parser)
1991 struct c_typespec ret;
1992 ret.kind = ctsk_typeof;
1993 ret.spec = error_mark_node;
1994 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
1995 c_parser_consume_token (parser);
1996 skip_evaluation++;
1997 in_typeof++;
1998 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2000 skip_evaluation--;
2001 in_typeof--;
2002 return ret;
2004 if (c_parser_next_token_starts_typename (parser))
2006 struct c_type_name *type = c_parser_type_name (parser);
2007 skip_evaluation--;
2008 in_typeof--;
2009 if (type != NULL)
2011 ret.spec = groktypename (type);
2012 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2015 else
2017 struct c_expr expr = c_parser_expression (parser);
2018 skip_evaluation--;
2019 in_typeof--;
2020 if (TREE_CODE (expr.value) == COMPONENT_REF
2021 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2022 error ("%<typeof%> applied to a bit-field");
2023 ret.spec = TREE_TYPE (expr.value);
2024 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2027 return ret;
2030 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2031 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2032 be redeclared; otherwise it may not. KIND indicates which kind of
2033 declarator is wanted. Returns a valid declarator except in the
2034 case of a syntax error in which case NULL is returned. *SEEN_ID is
2035 set to true if an identifier being declared is seen; this is used
2036 to diagnose bad forms of abstract array declarators and to
2037 determine whether an identifier list is syntactically permitted.
2039 declarator:
2040 pointer[opt] direct-declarator
2042 direct-declarator:
2043 identifier
2044 ( attributes[opt] declarator )
2045 direct-declarator array-declarator
2046 direct-declarator ( parameter-type-list )
2047 direct-declarator ( identifier-list[opt] )
2049 pointer:
2050 * type-qualifier-list[opt]
2051 * type-qualifier-list[opt] pointer
2053 type-qualifier-list:
2054 type-qualifier
2055 attributes
2056 type-qualifier-list type-qualifier
2057 type-qualifier-list attributes
2059 parameter-type-list:
2060 parameter-list
2061 parameter-list , ...
2063 parameter-list:
2064 parameter-declaration
2065 parameter-list , parameter-declaration
2067 parameter-declaration:
2068 declaration-specifiers declarator attributes[opt]
2069 declaration-specifiers abstract-declarator[opt] attributes[opt]
2071 identifier-list:
2072 identifier
2073 identifier-list , identifier
2075 abstract-declarator:
2076 pointer
2077 pointer[opt] direct-abstract-declarator
2079 direct-abstract-declarator:
2080 ( attributes[opt] abstract-declarator )
2081 direct-abstract-declarator[opt] array-declarator
2082 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2084 GNU extensions:
2086 direct-declarator:
2087 direct-declarator ( parameter-forward-declarations
2088 parameter-type-list[opt] )
2090 direct-abstract-declarator:
2091 direct-abstract-declarator[opt] ( parameter-forward-declarations
2092 parameter-type-list[opt] )
2094 parameter-forward-declarations:
2095 parameter-list ;
2096 parameter-forward-declarations parameter-list ;
2098 The uses of attributes shown above are GNU extensions.
2100 Some forms of array declarator are not included in C99 in the
2101 syntax for abstract declarators; these are disallowed elsewhere.
2102 This may be a defect (DR#289).
2104 This function also accepts an omitted abstract declarator as being
2105 an abstract declarator, although not part of the formal syntax. */
2107 static struct c_declarator *
2108 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2109 bool *seen_id)
2111 /* Parse any initial pointer part. */
2112 if (c_parser_next_token_is (parser, CPP_MULT))
2114 struct c_declspecs *quals_attrs = build_null_declspecs ();
2115 struct c_declarator *inner;
2116 c_parser_consume_token (parser);
2117 c_parser_declspecs (parser, quals_attrs, false, false, true);
2118 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2119 if (inner == NULL)
2120 return NULL;
2121 else
2122 return make_pointer_declarator (quals_attrs, inner);
2124 /* Now we have a direct declarator, direct abstract declarator or
2125 nothing (which counts as a direct abstract declarator here). */
2126 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2129 /* Parse a direct declarator or direct abstract declarator; arguments
2130 as c_parser_declarator. */
2132 static struct c_declarator *
2133 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2134 bool *seen_id)
2136 /* The direct declarator must start with an identifier (possibly
2137 omitted) or a parenthesized declarator (possibly abstract). In
2138 an ordinary declarator, initial parentheses must start a
2139 parenthesized declarator. In an abstract declarator or parameter
2140 declarator, they could start a parenthesized declarator or a
2141 parameter list. To tell which, the open parenthesis and any
2142 following attributes must be read. If a declaration specifier
2143 follows, then it is a parameter list; if the specifier is a
2144 typedef name, there might be an ambiguity about redeclaring it,
2145 which is resolved in the direction of treating it as a typedef
2146 name. If a close parenthesis follows, it is also an empty
2147 parameter list, as the syntax does not permit empty abstract
2148 declarators. Otherwise, it is a parenthesized declarator (in
2149 which case the analysis may be repeated inside it, recursively).
2151 ??? There is an ambiguity in a parameter declaration "int
2152 (__attribute__((foo)) x)", where x is not a typedef name: it
2153 could be an abstract declarator for a function, or declare x with
2154 parentheses. The proper resolution of this ambiguity needs
2155 documenting. At present we follow an accident of the old
2156 parser's implementation, whereby the first parameter must have
2157 some declaration specifiers other than just attributes. Thus as
2158 a parameter declaration it is treated as a parenthesized
2159 parameter named x, and as an abstract declarator it is
2160 rejected.
2162 ??? Also following the old parser, attributes inside an empty
2163 parameter list are ignored, making it a list not yielding a
2164 prototype, rather than giving an error or making it have one
2165 parameter with implicit type int.
2167 ??? Also following the old parser, typedef names may be
2168 redeclared in declarators, but not Objective-C class names. */
2170 if (kind != C_DTR_ABSTRACT
2171 && c_parser_next_token_is (parser, CPP_NAME)
2172 && ((type_seen_p
2173 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2174 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2176 struct c_declarator *inner
2177 = build_id_declarator (c_parser_peek_token (parser)->value);
2178 *seen_id = true;
2179 inner->id_loc = c_parser_peek_token (parser)->location;
2180 c_parser_consume_token (parser);
2181 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2184 if (kind != C_DTR_NORMAL
2185 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2187 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2188 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2191 /* Either we are at the end of an abstract declarator, or we have
2192 parentheses. */
2194 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2196 tree attrs;
2197 struct c_declarator *inner;
2198 c_parser_consume_token (parser);
2199 attrs = c_parser_attributes (parser);
2200 if (kind != C_DTR_NORMAL
2201 && (c_parser_next_token_starts_declspecs (parser)
2202 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2204 struct c_arg_info *args
2205 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2206 attrs);
2207 if (args == NULL)
2208 return NULL;
2209 else
2211 inner
2212 = build_function_declarator (args,
2213 build_id_declarator (NULL_TREE));
2214 return c_parser_direct_declarator_inner (parser, *seen_id,
2215 inner);
2218 /* A parenthesized declarator. */
2219 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2220 if (inner != NULL && attrs != NULL)
2221 inner = build_attrs_declarator (attrs, inner);
2222 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2224 c_parser_consume_token (parser);
2225 if (inner == NULL)
2226 return NULL;
2227 else
2228 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2230 else
2232 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2233 "expected %<)%>");
2234 return NULL;
2237 else
2239 if (kind == C_DTR_NORMAL)
2241 c_parser_error (parser, "expected identifier or %<(%>");
2242 return NULL;
2244 else
2245 return build_id_declarator (NULL_TREE);
2249 /* Parse part of a direct declarator or direct abstract declarator,
2250 given that some (in INNER) has already been parsed; ID_PRESENT is
2251 true if an identifier is present, false for an abstract
2252 declarator. */
2254 static struct c_declarator *
2255 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2256 struct c_declarator *inner)
2258 /* Parse a sequence of array declarators and parameter lists. */
2259 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2261 struct c_declarator *declarator;
2262 struct c_declspecs *quals_attrs = build_null_declspecs ();
2263 bool static_seen;
2264 bool star_seen;
2265 tree dimen;
2266 c_parser_consume_token (parser);
2267 c_parser_declspecs (parser, quals_attrs, false, false, true);
2268 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2269 if (static_seen)
2270 c_parser_consume_token (parser);
2271 if (static_seen && !quals_attrs->declspecs_seen_p)
2272 c_parser_declspecs (parser, quals_attrs, false, false, true);
2273 if (!quals_attrs->declspecs_seen_p)
2274 quals_attrs = NULL;
2275 /* If "static" is present, there must be an array dimension.
2276 Otherwise, there may be a dimension, "*", or no
2277 dimension. */
2278 if (static_seen)
2280 star_seen = false;
2281 dimen = c_parser_expr_no_commas (parser, NULL).value;
2283 else
2285 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2287 dimen = NULL_TREE;
2288 star_seen = false;
2290 else if (c_parser_next_token_is (parser, CPP_MULT))
2292 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2294 dimen = NULL_TREE;
2295 star_seen = true;
2296 c_parser_consume_token (parser);
2298 else
2300 star_seen = false;
2301 dimen = c_parser_expr_no_commas (parser, NULL).value;
2304 else
2306 star_seen = false;
2307 dimen = c_parser_expr_no_commas (parser, NULL).value;
2310 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2311 c_parser_consume_token (parser);
2312 else
2314 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2315 "expected %<]%>");
2316 return NULL;
2318 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2319 star_seen);
2320 inner = set_array_declarator_inner (declarator, inner, !id_present);
2321 return c_parser_direct_declarator_inner (parser, id_present, inner);
2323 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2325 tree attrs;
2326 struct c_arg_info *args;
2327 c_parser_consume_token (parser);
2328 attrs = c_parser_attributes (parser);
2329 args = c_parser_parms_declarator (parser, id_present, attrs);
2330 if (args == NULL)
2331 return NULL;
2332 else
2334 inner = build_function_declarator (args, inner);
2335 return c_parser_direct_declarator_inner (parser, id_present, inner);
2338 return inner;
2341 /* Parse a parameter list or identifier list, including the closing
2342 parenthesis but not the opening one. ATTRS are the attributes at
2343 the start of the list. ID_LIST_OK is true if an identifier list is
2344 acceptable; such a list must not have attributes at the start. */
2346 static struct c_arg_info *
2347 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2349 push_scope ();
2350 declare_parm_level ();
2351 /* If the list starts with an identifier, it is an identifier list.
2352 Otherwise, it is either a prototype list or an empty list. */
2353 if (id_list_ok
2354 && !attrs
2355 && c_parser_next_token_is (parser, CPP_NAME)
2356 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2358 tree list = NULL_TREE, *nextp = &list;
2359 while (c_parser_next_token_is (parser, CPP_NAME)
2360 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2362 *nextp = build_tree_list (NULL_TREE,
2363 c_parser_peek_token (parser)->value);
2364 nextp = & TREE_CHAIN (*nextp);
2365 c_parser_consume_token (parser);
2366 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2367 break;
2368 c_parser_consume_token (parser);
2369 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2371 c_parser_error (parser, "expected identifier");
2372 break;
2375 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2377 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2378 ret->parms = 0;
2379 ret->tags = 0;
2380 ret->types = list;
2381 ret->others = 0;
2382 c_parser_consume_token (parser);
2383 pop_scope ();
2384 return ret;
2386 else
2388 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2389 "expected %<)%>");
2390 pop_scope ();
2391 return NULL;
2394 else
2396 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2397 pop_scope ();
2398 return ret;
2402 /* Parse a parameter list (possibly empty), including the closing
2403 parenthesis but not the opening one. ATTRS are the attributes at
2404 the start of the list. */
2406 static struct c_arg_info *
2407 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2409 bool good_parm = false;
2410 /* ??? Following the old parser, forward parameter declarations may
2411 use abstract declarators, and if no real parameter declarations
2412 follow the forward declarations then this is not diagnosed. Also
2413 note as above that attributes are ignored as the only contents of
2414 the parentheses, or as the only contents after forward
2415 declarations. */
2416 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2418 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2419 ret->parms = 0;
2420 ret->tags = 0;
2421 ret->types = 0;
2422 ret->others = 0;
2423 c_parser_consume_token (parser);
2424 return ret;
2426 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2428 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2429 ret->parms = 0;
2430 ret->tags = 0;
2431 ret->others = 0;
2432 /* Suppress -Wold-style-definition for this case. */
2433 ret->types = error_mark_node;
2434 error ("ISO C requires a named argument before %<...%>");
2435 c_parser_consume_token (parser);
2436 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2438 c_parser_consume_token (parser);
2439 return ret;
2441 else
2443 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2444 "expected %<)%>");
2445 return NULL;
2448 /* Nonempty list of parameters, either terminated with semicolon
2449 (forward declarations; recurse) or with close parenthesis (normal
2450 function) or with ", ... )" (variadic function). */
2451 while (true)
2453 /* Parse a parameter. */
2454 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2455 attrs = NULL_TREE;
2456 if (parm != NULL)
2458 good_parm = true;
2459 push_parm_decl (parm);
2461 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2463 tree new_attrs;
2464 c_parser_consume_token (parser);
2465 new_attrs = c_parser_attributes (parser);
2466 return c_parser_parms_list_declarator (parser, new_attrs);
2468 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2470 c_parser_consume_token (parser);
2471 if (good_parm)
2472 return get_parm_info (false);
2473 else
2475 struct c_arg_info *ret
2476 = XOBNEW (&parser_obstack, struct c_arg_info);
2477 ret->parms = 0;
2478 ret->tags = 0;
2479 ret->types = 0;
2480 ret->others = 0;
2481 return ret;
2484 if (!c_parser_require (parser, CPP_COMMA,
2485 "expected %<;%>, %<,%> or %<)%>"))
2487 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2488 return NULL;
2490 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2492 c_parser_consume_token (parser);
2493 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2495 c_parser_consume_token (parser);
2496 if (good_parm)
2497 return get_parm_info (true);
2498 else
2500 struct c_arg_info *ret
2501 = XOBNEW (&parser_obstack, struct c_arg_info);
2502 ret->parms = 0;
2503 ret->tags = 0;
2504 ret->types = 0;
2505 ret->others = 0;
2506 return ret;
2509 else
2511 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2512 "expected %<)%>");
2513 return NULL;
2519 /* Parse a parameter declaration. ATTRS are the attributes at the
2520 start of the declaration if it is the first parameter. */
2522 static struct c_parm *
2523 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2525 struct c_declspecs *specs;
2526 struct c_declarator *declarator;
2527 tree prefix_attrs;
2528 tree postfix_attrs = NULL_TREE;
2529 bool dummy = false;
2530 if (!c_parser_next_token_starts_declspecs (parser))
2532 /* ??? In some Objective-C cases '...' isn't applicable so there
2533 should be a different message. */
2534 c_parser_error (parser,
2535 "expected declaration specifiers or %<...%>");
2536 c_parser_skip_to_end_of_parameter (parser);
2537 return NULL;
2539 specs = build_null_declspecs ();
2540 if (attrs)
2542 declspecs_add_attrs (specs, attrs);
2543 attrs = NULL_TREE;
2545 c_parser_declspecs (parser, specs, true, true, true);
2546 finish_declspecs (specs);
2547 pending_xref_error ();
2548 prefix_attrs = specs->attrs;
2549 specs->attrs = NULL_TREE;
2550 declarator = c_parser_declarator (parser, specs->type_seen_p,
2551 C_DTR_PARM, &dummy);
2552 if (declarator == NULL)
2554 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2555 return NULL;
2557 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2558 postfix_attrs = c_parser_attributes (parser);
2559 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2560 declarator);
2563 /* Parse a string literal in an asm expression. It should not be
2564 translated, and wide string literals are an error although
2565 permitted by the syntax. This is a GNU extension.
2567 asm-string-literal:
2568 string-literal
2570 ??? At present, following the old parser, the caller needs to have
2571 set c_lex_string_translate to 0. It would be better to follow the
2572 C++ parser rather than using the c_lex_string_translate kludge. */
2574 static tree
2575 c_parser_asm_string_literal (c_parser *parser)
2577 tree str;
2578 if (c_parser_next_token_is (parser, CPP_STRING))
2580 str = c_parser_peek_token (parser)->value;
2581 c_parser_consume_token (parser);
2583 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2585 error ("wide string literal in %<asm%>");
2586 str = build_string (1, "");
2587 c_parser_consume_token (parser);
2589 else
2591 c_parser_error (parser, "expected string literal");
2592 str = NULL_TREE;
2594 return str;
2597 /* Parse a simple asm expression. This is used in restricted
2598 contexts, where a full expression with inputs and outputs does not
2599 make sense. This is a GNU extension.
2601 simple-asm-expr:
2602 asm ( asm-string-literal )
2605 static tree
2606 c_parser_simple_asm_expr (c_parser *parser)
2608 tree str;
2609 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2610 /* ??? Follow the C++ parser rather than using the
2611 c_lex_string_translate kludge. */
2612 c_lex_string_translate = 0;
2613 c_parser_consume_token (parser);
2614 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2616 c_lex_string_translate = 1;
2617 return NULL_TREE;
2619 str = c_parser_asm_string_literal (parser);
2620 c_lex_string_translate = 1;
2621 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2623 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2624 return NULL_TREE;
2626 return str;
2629 /* Parse (possibly empty) attributes. This is a GNU extension.
2631 attributes:
2632 empty
2633 attributes attribute
2635 attribute:
2636 __attribute__ ( ( attribute-list ) )
2638 attribute-list:
2639 attrib
2640 attribute_list , attrib
2642 attrib:
2643 empty
2644 any-word
2645 any-word ( identifier )
2646 any-word ( identifier , nonempty-expr-list )
2647 any-word ( expr-list )
2649 where the "identifier" must not be declared as a type, and
2650 "any-word" may be any identifier (including one declared as a
2651 type), a reserved word storage class specifier, type specifier or
2652 type qualifier. ??? This still leaves out most reserved keywords
2653 (following the old parser), shouldn't we include them, and why not
2654 allow identifiers declared as types to start the arguments? */
2656 static tree
2657 c_parser_attributes (c_parser *parser)
2659 tree attrs = NULL_TREE;
2660 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2662 /* ??? Follow the C++ parser rather than using the
2663 c_lex_string_translate kludge. */
2664 c_lex_string_translate = 0;
2665 c_parser_consume_token (parser);
2666 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2668 c_lex_string_translate = 1;
2669 return attrs;
2671 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2673 c_lex_string_translate = 1;
2674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2675 return attrs;
2677 /* Parse the attribute list. */
2678 while (c_parser_next_token_is (parser, CPP_COMMA)
2679 || c_parser_next_token_is (parser, CPP_NAME)
2680 || c_parser_next_token_is (parser, CPP_KEYWORD))
2682 tree attr, attr_name, attr_args;
2683 if (c_parser_next_token_is (parser, CPP_COMMA))
2685 c_parser_consume_token (parser);
2686 continue;
2688 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2690 /* ??? See comment above about what keywords are
2691 accepted here. */
2692 bool ok;
2693 switch (c_parser_peek_token (parser)->keyword)
2695 case RID_STATIC:
2696 case RID_UNSIGNED:
2697 case RID_LONG:
2698 case RID_CONST:
2699 case RID_EXTERN:
2700 case RID_REGISTER:
2701 case RID_TYPEDEF:
2702 case RID_SHORT:
2703 case RID_INLINE:
2704 case RID_VOLATILE:
2705 case RID_SIGNED:
2706 case RID_AUTO:
2707 case RID_RESTRICT:
2708 case RID_COMPLEX:
2709 case RID_THREAD:
2710 case RID_INT:
2711 case RID_CHAR:
2712 case RID_FLOAT:
2713 case RID_DOUBLE:
2714 case RID_VOID:
2715 case RID_BOOL:
2716 ok = true;
2717 break;
2718 default:
2719 ok = false;
2720 break;
2722 if (!ok)
2723 break;
2725 attr_name = c_parser_peek_token (parser)->value;
2726 c_parser_consume_token (parser);
2727 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2729 attr = build_tree_list (attr_name, NULL_TREE);
2730 attrs = chainon (attrs, attr);
2731 continue;
2733 c_parser_consume_token (parser);
2734 /* Parse the attribute contents. If they start with an
2735 identifier which is followed by a comma or close
2736 parenthesis, then the arguments start with that
2737 identifier; otherwise they are an expression list. */
2738 if (c_parser_next_token_is (parser, CPP_NAME)
2739 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2740 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2741 || (c_parser_peek_2nd_token (parser)->type
2742 == CPP_CLOSE_PAREN)))
2744 tree arg1 = c_parser_peek_token (parser)->value;
2745 c_parser_consume_token (parser);
2746 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2747 attr_args = build_tree_list (NULL_TREE, arg1);
2748 else
2750 c_parser_consume_token (parser);
2751 attr_args = tree_cons (NULL_TREE, arg1,
2752 c_parser_expr_list (parser));
2755 else
2757 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2758 attr_args = NULL_TREE;
2759 else
2760 attr_args = c_parser_expr_list (parser);
2762 attr = build_tree_list (attr_name, attr_args);
2763 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2764 c_parser_consume_token (parser);
2765 else
2767 c_lex_string_translate = 1;
2768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2769 "expected %<)%>");
2770 return attrs;
2772 attrs = chainon (attrs, attr);
2774 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2775 c_parser_consume_token (parser);
2776 else
2778 c_lex_string_translate = 1;
2779 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2780 "expected %<)%>");
2781 return attrs;
2783 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2784 c_parser_consume_token (parser);
2785 else
2787 c_lex_string_translate = 1;
2788 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2789 "expected %<)%>");
2790 return attrs;
2792 c_lex_string_translate = 1;
2794 return attrs;
2797 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2799 type-name:
2800 specifier-qualifier-list abstract-declarator[opt]
2803 static struct c_type_name *
2804 c_parser_type_name (c_parser *parser)
2806 struct c_declspecs *specs = build_null_declspecs ();
2807 struct c_declarator *declarator;
2808 struct c_type_name *ret;
2809 bool dummy = false;
2810 c_parser_declspecs (parser, specs, false, true, true);
2811 if (!specs->declspecs_seen_p)
2813 c_parser_error (parser, "expected specifier-qualifier-list");
2814 return NULL;
2816 pending_xref_error ();
2817 finish_declspecs (specs);
2818 declarator = c_parser_declarator (parser, specs->type_seen_p,
2819 C_DTR_ABSTRACT, &dummy);
2820 if (declarator == NULL)
2821 return NULL;
2822 ret = XOBNEW (&parser_obstack, struct c_type_name);
2823 ret->specs = specs;
2824 ret->declarator = declarator;
2825 return ret;
2828 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2830 initializer:
2831 assignment-expression
2832 { initializer-list }
2833 { initializer-list , }
2835 initializer-list:
2836 designation[opt] initializer
2837 initializer-list , designation[opt] initializer
2839 designation:
2840 designator-list =
2842 designator-list:
2843 designator
2844 designator-list designator
2846 designator:
2847 array-designator
2848 . identifier
2850 array-designator:
2851 [ constant-expression ]
2853 GNU extensions:
2855 initializer:
2858 designation:
2859 array-designator
2860 identifier :
2862 array-designator:
2863 [ constant-expression ... constant-expression ]
2865 Any expression without commas is accepted in the syntax for the
2866 constant-expressions, with non-constant expressions rejected later.
2868 This function is only used for top-level initializers; for nested
2869 ones, see c_parser_initval. */
2871 static struct c_expr
2872 c_parser_initializer (c_parser *parser)
2874 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2875 return c_parser_braced_init (parser, NULL_TREE, false);
2876 else
2877 return c_parser_expr_no_commas (parser, NULL);
2880 /* Parse a braced initializer list. TYPE is the type specified for a
2881 compound literal, and NULL_TREE for other initializers and for
2882 nested braced lists. NESTED_P is true for nested braced lists,
2883 false for the list of a compound literal or the list that is the
2884 top-level initializer in a declaration. */
2886 static struct c_expr
2887 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2889 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2890 c_parser_consume_token (parser);
2891 if (nested_p)
2892 push_init_level (0);
2893 else
2894 really_start_incremental_init (type);
2895 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2897 if (pedantic)
2898 pedwarn ("ISO C forbids empty initializer braces");
2900 else
2902 /* Parse a non-empty initializer list, possibly with a trailing
2903 comma. */
2904 while (true)
2906 c_parser_initelt (parser);
2907 if (parser->error)
2908 break;
2909 if (c_parser_next_token_is (parser, CPP_COMMA))
2910 c_parser_consume_token (parser);
2911 else
2912 break;
2913 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2914 break;
2917 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
2919 struct c_expr ret;
2920 ret.value = error_mark_node;
2921 ret.original_code = ERROR_MARK;
2922 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
2923 return ret;
2925 c_parser_consume_token (parser);
2926 return pop_init_level (0);
2929 /* Parse a nested initializer, including designators. */
2931 static void
2932 c_parser_initelt (c_parser *parser)
2934 /* Parse any designator or designator list. A single array
2935 designator may have the subsequent "=" omitted in GNU C, but a
2936 longer list or a structure member designator may not. */
2937 if (c_parser_next_token_is (parser, CPP_NAME)
2938 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
2940 /* Old-style structure member designator. */
2941 set_init_label (c_parser_peek_token (parser)->value);
2942 if (pedantic)
2943 pedwarn ("obsolete use of designated initializer with %<:%>");
2944 c_parser_consume_token (parser);
2945 c_parser_consume_token (parser);
2947 else
2949 /* des_seen is 0 if there have been no designators, 1 if there
2950 has been a single array designator and 2 otherwise. */
2951 int des_seen = 0;
2952 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
2953 || c_parser_next_token_is (parser, CPP_DOT))
2955 int des_prev = des_seen;
2956 if (des_seen < 2)
2957 des_seen++;
2958 if (c_parser_next_token_is (parser, CPP_DOT))
2960 des_seen = 2;
2961 c_parser_consume_token (parser);
2962 if (c_parser_next_token_is (parser, CPP_NAME))
2964 set_init_label (c_parser_peek_token (parser)->value);
2965 c_parser_consume_token (parser);
2967 else
2969 struct c_expr init;
2970 init.value = error_mark_node;
2971 init.original_code = ERROR_MARK;
2972 c_parser_error (parser, "expected identifier");
2973 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2974 process_init_element (init);
2975 return;
2978 else
2980 tree first, second;
2981 /* ??? Following the old parser, [ objc-receiver
2982 objc-message-args ] is accepted as an initializer,
2983 being distinguished from a designator by what follows
2984 the first assignment expression inside the square
2985 brackets, but after a first array designator a
2986 subsequent square bracket is for Objective-C taken to
2987 start an expression, using the obsolete form of
2988 designated initializer without '=', rather than
2989 possibly being a second level of designation: in LALR
2990 terms, the '[' is shifted rather than reducing
2991 designator to designator-list. */
2992 if (des_prev == 1 && c_dialect_objc ())
2994 des_seen = des_prev;
2995 break;
2997 if (des_prev == 0 && c_dialect_objc ())
2999 /* This might be an array designator or an
3000 Objective-C message expression. If the former,
3001 continue parsing here; if the latter, parse the
3002 remainder of the initializer given the starting
3003 primary-expression. ??? It might make sense to
3004 distinguish when des_prev == 1 as well; see
3005 previous comment. */
3006 tree rec, args;
3007 struct c_expr mexpr;
3008 c_parser_consume_token (parser);
3009 if (c_parser_peek_token (parser)->type == CPP_NAME
3010 && ((c_parser_peek_token (parser)->id_kind
3011 == C_ID_TYPENAME)
3012 || (c_parser_peek_token (parser)->id_kind
3013 == C_ID_CLASSNAME)))
3015 /* Type name receiver. */
3016 tree id = c_parser_peek_token (parser)->value;
3017 c_parser_consume_token (parser);
3018 rec = objc_get_class_reference (id);
3019 goto parse_message_args;
3021 first = c_parser_expr_no_commas (parser, NULL).value;
3022 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3023 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3024 goto array_desig_after_first;
3025 /* Expression receiver. So far only one part
3026 without commas has been parsed; there might be
3027 more of the expression. */
3028 rec = first;
3029 while (c_parser_next_token_is (parser, CPP_COMMA))
3031 tree next;
3032 c_parser_consume_token (parser);
3033 next = c_parser_expr_no_commas (parser, NULL).value;
3034 rec = build_compound_expr (rec, next);
3036 parse_message_args:
3037 /* Now parse the objc-message-args. */
3038 args = c_parser_objc_message_args (parser);
3039 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3040 "expected %<]%>");
3041 mexpr.value
3042 = objc_build_message_expr (build_tree_list (rec, args));
3043 mexpr.original_code = ERROR_MARK;
3044 /* Now parse and process the remainder of the
3045 initializer, starting with this message
3046 expression as a primary-expression. */
3047 c_parser_initval (parser, &mexpr);
3048 return;
3050 c_parser_consume_token (parser);
3051 first = c_parser_expr_no_commas (parser, NULL).value;
3052 array_desig_after_first:
3053 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3055 c_parser_consume_token (parser);
3056 second = c_parser_expr_no_commas (parser, NULL).value;
3058 else
3059 second = NULL_TREE;
3060 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3062 c_parser_consume_token (parser);
3063 set_init_index (first, second);
3064 if (pedantic && second)
3065 pedwarn ("ISO C forbids specifying range of "
3066 "elements to initialize");
3068 else
3069 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3070 "expected %<]%>");
3073 if (des_seen >= 1)
3075 if (c_parser_next_token_is (parser, CPP_EQ))
3077 if (pedantic && !flag_isoc99)
3078 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3079 c_parser_consume_token (parser);
3081 else
3083 if (des_seen == 1)
3085 if (pedantic)
3086 pedwarn ("obsolete use of designated initializer "
3087 "without %<=%>");
3089 else
3091 struct c_expr init;
3092 init.value = error_mark_node;
3093 init.original_code = ERROR_MARK;
3094 c_parser_error (parser, "expected %<=%>");
3095 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3096 process_init_element (init);
3097 return;
3102 c_parser_initval (parser, NULL);
3105 /* Parse a nested initializer; as c_parser_initializer but parses
3106 initializers within braced lists, after any designators have been
3107 applied. If AFTER is not NULL then it is an Objective-C message
3108 expression which is the primary-expression starting the
3109 initializer. */
3111 static void
3112 c_parser_initval (c_parser *parser, struct c_expr *after)
3114 struct c_expr init;
3115 gcc_assert (!after || c_dialect_objc ());
3116 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3117 init = c_parser_braced_init (parser, NULL_TREE, true);
3118 else
3119 init = c_parser_expr_no_commas (parser, after);
3120 process_init_element (init);
3123 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3124 C99 6.8.2).
3126 compound-statement:
3127 { block-item-list[opt] }
3128 { label-declarations block-item-list }
3130 block-item-list:
3131 block-item
3132 block-item-list block-item
3134 block-item:
3135 nested-declaration
3136 statement
3138 nested-declaration:
3139 declaration
3141 GNU extensions:
3143 compound-statement:
3144 { label-declarations block-item-list }
3146 nested-declaration:
3147 __extension__ nested-declaration
3148 nested-function-definition
3150 label-declarations:
3151 label-declaration
3152 label-declarations label-declaration
3154 label-declaration:
3155 __label__ identifier-list ;
3157 Allowing the mixing of declarations and code is new in C99. The
3158 GNU syntax also permits (not shown above) labels at the end of
3159 compound statements, which yield an error. We don't allow labels
3160 on declarations; this might seem like a natural extension, but
3161 there would be a conflict between attributes on the label and
3162 prefix attributes on the declaration. ??? The syntax follows the
3163 old parser in requiring something after label declarations.
3164 Although they are erroneous if the labels declared aren't defined,
3165 is it useful for the syntax to be this way? */
3167 static tree
3168 c_parser_compound_statement (c_parser *parser)
3170 tree stmt;
3171 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3172 return error_mark_node;
3173 stmt = c_begin_compound_stmt (true);
3174 c_parser_compound_statement_nostart (parser);
3175 return c_end_compound_stmt (stmt, true);
3178 /* Parse a compound statement except for the opening brace. This is
3179 used for parsing both compound statements and statement expressions
3180 (which follow different paths to handling the opening). */
3182 static void
3183 c_parser_compound_statement_nostart (c_parser *parser)
3185 bool last_stmt = false;
3186 bool last_label = false;
3187 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3189 c_parser_consume_token (parser);
3190 return;
3192 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3194 /* Read zero or more forward-declarations for labels that nested
3195 functions can jump to. */
3196 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3198 c_parser_consume_token (parser);
3199 /* Any identifiers, including those declared as type names,
3200 are OK here. */
3201 while (true)
3203 tree label;
3204 if (c_parser_next_token_is_not (parser, CPP_NAME))
3206 c_parser_error (parser, "expected identifier");
3207 break;
3209 label
3210 = declare_label (c_parser_peek_token (parser)->value);
3211 C_DECLARED_LABEL_FLAG (label) = 1;
3212 add_stmt (build_stmt (DECL_EXPR, label));
3213 c_parser_consume_token (parser);
3214 if (c_parser_next_token_is (parser, CPP_COMMA))
3215 c_parser_consume_token (parser);
3216 else
3217 break;
3219 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3221 /* ??? Locating this diagnostic on the token after the
3222 declarations end follows the old parser, but it might be
3223 better to locate it where the declarations start instead. */
3224 if (pedantic)
3225 pedwarn ("ISO C forbids label declarations");
3227 /* We must now have at least one statement, label or declaration. */
3228 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3230 c_parser_error (parser, "expected declaration or statement");
3231 c_parser_consume_token (parser);
3232 return;
3234 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3236 location_t loc = c_parser_peek_token (parser)->location;
3237 if (c_parser_next_token_is (parser, CPP_EOF))
3239 c_parser_error (parser, "expected declaration or statement");
3240 return;
3242 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3243 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3244 || (c_parser_next_token_is (parser, CPP_NAME)
3245 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3247 last_label = true;
3248 last_stmt = false;
3249 c_parser_label (parser);
3251 else if (!last_label
3252 && c_parser_next_token_starts_declspecs (parser))
3254 last_label = false;
3255 c_parser_declaration_or_fndef (parser, true, true, true, true);
3256 if (last_stmt
3257 && ((pedantic && !flag_isoc99)
3258 || warn_declaration_after_statement))
3259 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3260 &loc);
3261 last_stmt = false;
3263 else if (!last_label
3264 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3266 /* __extension__ can start a declaration, but is also an
3267 unary operator that can start an expression. Consume all
3268 but the last of a possible series of __extension__ to
3269 determine which. */
3270 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3271 && (c_parser_peek_2nd_token (parser)->keyword
3272 == RID_EXTENSION))
3273 c_parser_consume_token (parser);
3274 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3276 int ext;
3277 ext = disable_extension_diagnostics ();
3278 c_parser_consume_token (parser);
3279 last_label = false;
3280 c_parser_declaration_or_fndef (parser, true, true, true, true);
3281 /* Following the old parser, __extension__ does not
3282 disable this diagnostic. */
3283 restore_extension_diagnostics (ext);
3284 if (last_stmt
3285 && ((pedantic && !flag_isoc99)
3286 || warn_declaration_after_statement))
3287 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3288 &loc);
3289 last_stmt = false;
3291 else
3292 goto statement;
3294 else
3296 statement:
3297 last_label = false;
3298 last_stmt = true;
3299 c_parser_statement_after_labels (parser);
3302 if (last_label)
3303 error ("label at end of compound statement");
3304 c_parser_consume_token (parser);
3307 /* Parse a label (C90 6.6.1, C99 6.8.1).
3309 label:
3310 identifier : attributes[opt]
3311 case constant-expression :
3312 default :
3314 GNU extensions:
3316 label:
3317 case constant-expression ... constant-expression :
3319 The use of attributes on labels is a GNU extension. The syntax in
3320 GNU C accepts any expressions without commas, non-constant
3321 expressions being rejected later. */
3323 static void
3324 c_parser_label (c_parser *parser)
3326 location_t loc1 = c_parser_peek_token (parser)->location;
3327 tree label = NULL_TREE;
3328 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3330 tree exp1, exp2;
3331 c_parser_consume_token (parser);
3332 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3333 if (c_parser_next_token_is (parser, CPP_COLON))
3335 c_parser_consume_token (parser);
3336 label = do_case (exp1, NULL_TREE);
3338 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3340 c_parser_consume_token (parser);
3341 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3342 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3343 label = do_case (exp1, exp2);
3345 else
3346 c_parser_error (parser, "expected %<:%> or %<...%>");
3348 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3350 c_parser_consume_token (parser);
3351 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3352 label = do_case (NULL_TREE, NULL_TREE);
3354 else
3356 tree name = c_parser_peek_token (parser)->value;
3357 tree tlab;
3358 location_t loc2;
3359 tree attrs;
3360 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3361 c_parser_consume_token (parser);
3362 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3363 loc2 = c_parser_peek_token (parser)->location;
3364 c_parser_consume_token (parser);
3365 attrs = c_parser_attributes (parser);
3366 tlab = define_label (loc2, name);
3367 if (tlab)
3369 decl_attributes (&tlab, attrs, 0);
3370 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3373 if (label)
3374 SET_EXPR_LOCATION (label, loc1);
3377 /* Parse a statement (C90 6.6, C99 6.8).
3379 statement:
3380 labeled-statement
3381 compound-statement
3382 expression-statement
3383 selection-statement
3384 iteration-statement
3385 jump-statement
3387 labeled-statement:
3388 label statement
3390 expression-statement:
3391 expression[opt] ;
3393 selection-statement:
3394 if-statement
3395 switch-statement
3397 iteration-statement:
3398 while-statement
3399 do-statement
3400 for-statement
3402 jump-statement:
3403 goto identifier ;
3404 continue ;
3405 break ;
3406 return expression[opt] ;
3408 GNU extensions:
3410 statement:
3411 asm-statement
3413 jump-statement:
3414 goto * expression ;
3416 Objective-C:
3418 statement:
3419 objc-throw-statement
3420 objc-try-catch-statement
3421 objc-synchronized-statement
3423 objc-throw-statement:
3424 @throw expression ;
3425 @throw ;
3428 static void
3429 c_parser_statement (c_parser *parser)
3431 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3432 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3433 || (c_parser_next_token_is (parser, CPP_NAME)
3434 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3435 c_parser_label (parser);
3436 c_parser_statement_after_labels (parser);
3439 /* Parse a statement, other than a labeled statement. */
3441 static void
3442 c_parser_statement_after_labels (c_parser *parser)
3444 location_t loc = c_parser_peek_token (parser)->location;
3445 tree stmt = NULL_TREE;
3446 switch (c_parser_peek_token (parser)->type)
3448 case CPP_OPEN_BRACE:
3449 add_stmt (c_parser_compound_statement (parser));
3450 break;
3451 case CPP_KEYWORD:
3452 switch (c_parser_peek_token (parser)->keyword)
3454 case RID_IF:
3455 c_parser_if_statement (parser);
3456 break;
3457 case RID_SWITCH:
3458 c_parser_switch_statement (parser);
3459 break;
3460 case RID_WHILE:
3461 c_parser_while_statement (parser);
3462 break;
3463 case RID_DO:
3464 c_parser_do_statement (parser);
3465 break;
3466 case RID_FOR:
3467 c_parser_for_statement (parser);
3468 break;
3469 case RID_GOTO:
3470 c_parser_consume_token (parser);
3471 if (c_parser_next_token_is (parser, CPP_NAME))
3473 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3474 c_parser_consume_token (parser);
3476 else if (c_parser_next_token_is (parser, CPP_MULT))
3478 c_parser_consume_token (parser);
3479 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3481 else
3482 c_parser_error (parser, "expected identifier or %<*%>");
3483 goto expect_semicolon;
3484 case RID_CONTINUE:
3485 c_parser_consume_token (parser);
3486 stmt = c_finish_bc_stmt (&c_cont_label, false);
3487 goto expect_semicolon;
3488 case RID_BREAK:
3489 c_parser_consume_token (parser);
3490 stmt = c_finish_bc_stmt (&c_break_label, true);
3491 goto expect_semicolon;
3492 case RID_RETURN:
3493 c_parser_consume_token (parser);
3494 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3496 stmt = c_finish_return (NULL_TREE);
3497 c_parser_consume_token (parser);
3499 else
3501 stmt = c_finish_return (c_parser_expression (parser).value);
3502 goto expect_semicolon;
3504 break;
3505 case RID_ASM:
3506 stmt = c_parser_asm_statement (parser);
3507 break;
3508 case RID_AT_THROW:
3509 gcc_assert (c_dialect_objc ());
3510 c_parser_consume_token (parser);
3511 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3513 stmt = objc_build_throw_stmt (NULL_TREE);
3514 c_parser_consume_token (parser);
3516 else
3518 stmt
3519 = objc_build_throw_stmt (c_parser_expression (parser).value);
3520 goto expect_semicolon;
3522 break;
3523 case RID_AT_TRY:
3524 gcc_assert (c_dialect_objc ());
3525 c_parser_objc_try_catch_statement (parser);
3526 break;
3527 case RID_AT_SYNCHRONIZED:
3528 gcc_assert (c_dialect_objc ());
3529 c_parser_objc_synchronized_statement (parser);
3530 break;
3531 default:
3532 goto expr_stmt;
3534 break;
3535 case CPP_SEMICOLON:
3536 c_parser_consume_token (parser);
3537 break;
3538 case CPP_CLOSE_PAREN:
3539 case CPP_CLOSE_SQUARE:
3540 /* Avoid infinite loop in error recovery:
3541 c_parser_skip_until_found stops at a closing nesting
3542 delimiter without consuming it, but here we need to consume
3543 it to proceed further. */
3544 c_parser_error (parser, "expected statement");
3545 c_parser_consume_token (parser);
3546 break;
3547 default:
3548 expr_stmt:
3549 stmt = c_finish_expr_stmt (c_parser_expression (parser).value);
3550 expect_semicolon:
3551 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3552 break;
3554 /* Two cases cannot and do not have line numbers associated: If stmt
3555 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3556 cannot hold line numbers. But that's OK because the statement
3557 will either be changed to a MODIFY_EXPR during gimplification of
3558 the statement expr, or discarded. If stmt was compound, but
3559 without new variables, we will have skipped the creation of a
3560 BIND and will have a bare STATEMENT_LIST. But that's OK because
3561 (recursively) all of the component statements should already have
3562 line numbers assigned. ??? Can we discard no-op statements
3563 earlier? */
3564 if (stmt && EXPR_P (stmt))
3565 SET_EXPR_LOCATION (stmt, loc);
3568 /* Parse a parenthesized condition from an if, do or while statement.
3570 condition:
3571 ( expression )
3573 static tree
3574 c_parser_paren_condition (c_parser *parser)
3576 location_t loc;
3577 tree cond;
3578 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3579 return error_mark_node;
3580 loc = c_parser_peek_token (parser)->location;
3581 cond = c_objc_common_truthvalue_conversion
3582 (c_parser_expression (parser).value);
3583 if (EXPR_P (cond))
3584 SET_EXPR_LOCATION (cond, loc);
3585 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3586 return cond;
3589 /* Parse a statement which is a block in C99. */
3591 static tree
3592 c_parser_c99_block_statement (c_parser *parser)
3594 tree block = c_begin_compound_stmt (flag_isoc99);
3595 c_parser_statement (parser);
3596 return c_end_compound_stmt (block, flag_isoc99);
3599 /* Parse the body of an if statement or the else half thereof. This
3600 is just parsing a statement but (a) it is a block in C99, (b) we
3601 track whether the body is an if statement for the sake of
3602 -Wparentheses warnings, (c) we handle an empty body specially for
3603 the sake of -Wextra warnings. */
3605 static tree
3606 c_parser_if_body (c_parser *parser, bool *if_p)
3608 tree block = c_begin_compound_stmt (flag_isoc99);
3609 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3610 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3611 || (c_parser_next_token_is (parser, CPP_NAME)
3612 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3613 c_parser_label (parser);
3614 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3615 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3616 add_stmt (build (NOP_EXPR, NULL_TREE, NULL_TREE));
3617 c_parser_statement_after_labels (parser);
3618 return c_end_compound_stmt (block, flag_isoc99);
3621 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3623 if-statement:
3624 if ( expression ) statement
3625 if ( expression ) statement else statement
3628 static void
3629 c_parser_if_statement (c_parser *parser)
3631 tree block;
3632 location_t loc;
3633 tree cond;
3634 bool first_if = false, second_if = false;
3635 tree first_body, second_body;
3636 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3637 c_parser_consume_token (parser);
3638 block = c_begin_compound_stmt (flag_isoc99);
3639 loc = c_parser_peek_token (parser)->location;
3640 cond = c_parser_paren_condition (parser);
3641 first_body = c_parser_if_body (parser, &first_if);
3642 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3644 c_parser_consume_token (parser);
3645 second_body = c_parser_if_body (parser, &second_if);
3647 else
3648 second_body = NULL_TREE;
3649 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3650 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3653 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3655 switch-statement:
3656 switch (expression) statement
3659 static void
3660 c_parser_switch_statement (c_parser *parser)
3662 tree block, expr, body, save_break;
3663 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3664 c_parser_consume_token (parser);
3665 block = c_begin_compound_stmt (flag_isoc99);
3666 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3668 expr = c_parser_expression (parser).value;
3669 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3671 else
3672 expr = error_mark_node;
3673 c_start_case (expr);
3674 save_break = c_break_label;
3675 c_break_label = NULL_TREE;
3676 body = c_parser_c99_block_statement (parser);
3677 c_finish_case (body);
3678 if (c_break_label)
3679 add_stmt (build (LABEL_EXPR, void_type_node, c_break_label));
3680 c_break_label = save_break;
3681 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3684 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3686 while-statement:
3687 while (expression) statement
3690 static void
3691 c_parser_while_statement (c_parser *parser)
3693 tree block, cond, body, save_break, save_cont;
3694 location_t loc;
3695 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3696 c_parser_consume_token (parser);
3697 block = c_begin_compound_stmt (flag_isoc99);
3698 loc = c_parser_peek_token (parser)->location;
3699 cond = c_parser_paren_condition (parser);
3700 save_break = c_break_label;
3701 c_break_label = NULL_TREE;
3702 save_cont = c_cont_label;
3703 c_cont_label = NULL_TREE;
3704 body = c_parser_c99_block_statement (parser);
3705 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3706 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3707 c_break_label = save_break;
3708 c_cont_label = save_cont;
3711 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3713 do-statement:
3714 do statement while ( expression ) ;
3717 static void
3718 c_parser_do_statement (c_parser *parser)
3720 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3721 location_t loc;
3722 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3723 c_parser_consume_token (parser);
3724 block = c_begin_compound_stmt (flag_isoc99);
3725 loc = c_parser_peek_token (parser)->location;
3726 save_break = c_break_label;
3727 c_break_label = NULL_TREE;
3728 save_cont = c_cont_label;
3729 c_cont_label = NULL_TREE;
3730 body = c_parser_c99_block_statement (parser);
3731 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3732 new_break = c_break_label;
3733 c_break_label = save_break;
3734 new_cont = c_cont_label;
3735 c_cont_label = save_cont;
3736 cond = c_parser_paren_condition (parser);
3737 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3738 c_parser_skip_to_end_of_block_or_statement (parser);
3739 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3740 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3743 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3745 for-statement:
3746 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3747 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3749 The form with a declaration is new in C99.
3751 ??? In accordance with the old parser, the declaration may be a
3752 nested function, which is then rejected in check_for_loop_decls,
3753 but does it make any sense for this to be included in the grammar?
3754 Note in particular that the nested function does not include a
3755 trailing ';', whereas the "declaration" production includes one.
3756 Also, can we reject bad declarations earlier and cheaper than
3757 check_for_loop_decls? */
3759 static void
3760 c_parser_for_statement (c_parser *parser)
3762 tree block, cond, incr, save_break, save_cont, body;
3763 location_t loc = UNKNOWN_LOCATION;
3764 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3765 c_parser_consume_token (parser);
3766 block = c_begin_compound_stmt (flag_isoc99);
3767 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3769 /* Parse the initialization declaration or expression. */
3770 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3772 c_parser_consume_token (parser);
3773 c_finish_expr_stmt (NULL_TREE);
3775 else if (c_parser_next_token_starts_declspecs (parser))
3777 c_parser_declaration_or_fndef (parser, true, true, true, true);
3778 check_for_loop_decls ();
3780 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3782 /* __extension__ can start a declaration, but is also an
3783 unary operator that can start an expression. Consume all
3784 but the last of a possible series of __extension__ to
3785 determine which. */
3786 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3787 && (c_parser_peek_2nd_token (parser)->keyword
3788 == RID_EXTENSION))
3789 c_parser_consume_token (parser);
3790 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3792 int ext;
3793 ext = disable_extension_diagnostics ();
3794 c_parser_consume_token (parser);
3795 c_parser_declaration_or_fndef (parser, true, true, true, true);
3796 restore_extension_diagnostics (ext);
3797 check_for_loop_decls ();
3799 else
3800 goto init_expr;
3802 else
3804 init_expr:
3805 c_finish_expr_stmt (c_parser_expression (parser).value);
3806 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3808 /* Parse the loop condition. */
3809 loc = c_parser_peek_token (parser)->location;
3810 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3812 c_parser_consume_token (parser);
3813 cond = NULL_TREE;
3815 else
3817 tree ocond = c_parser_expression (parser).value;
3818 cond = c_objc_common_truthvalue_conversion (ocond);
3819 if (EXPR_P (cond))
3820 SET_EXPR_LOCATION (cond, loc);
3821 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3823 /* Parse the increment expression. */
3824 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3825 incr = c_process_expr_stmt (NULL_TREE);
3826 else
3827 incr = c_process_expr_stmt (c_parser_expression (parser).value);
3828 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3830 else
3832 cond = error_mark_node;
3833 incr = error_mark_node;
3835 save_break = c_break_label;
3836 c_break_label = NULL_TREE;
3837 save_cont = c_cont_label;
3838 c_cont_label = NULL_TREE;
3839 body = c_parser_c99_block_statement (parser);
3840 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3841 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3842 c_break_label = save_break;
3843 c_cont_label = save_cont;
3846 /* Parse an asm statement, a GNU extension. This is a full-blown asm
3847 statement with inputs, outputs, clobbers, and volatile tag
3848 allowed.
3850 asm-statement:
3851 asm type-qualifier[opt] ( asm-argument ) ;
3853 asm-argument:
3854 asm-string-literal
3855 asm-string-literal : asm-operands[opt]
3856 asm-string-literal : asm-operands[opt] : asm-operands[opt]
3857 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3859 Qualifiers other than volatile are accepted in the syntax but
3860 warned for. */
3862 static tree
3863 c_parser_asm_statement (c_parser *parser)
3865 tree quals, str, outputs, inputs, clobbers, ret;
3866 bool simple;
3867 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3868 c_parser_consume_token (parser);
3869 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3871 quals = c_parser_peek_token (parser)->value;
3872 c_parser_consume_token (parser);
3874 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3875 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3877 warning (0, "%E qualifier ignored on asm",
3878 c_parser_peek_token (parser)->value);
3879 quals = NULL_TREE;
3880 c_parser_consume_token (parser);
3882 else
3883 quals = NULL_TREE;
3884 /* ??? Follow the C++ parser rather than using the
3885 c_lex_string_translate kludge. */
3886 c_lex_string_translate = 0;
3887 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3889 c_lex_string_translate = 1;
3890 return NULL_TREE;
3892 str = c_parser_asm_string_literal (parser);
3893 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3895 simple = true;
3896 outputs = NULL_TREE;
3897 inputs = NULL_TREE;
3898 clobbers = NULL_TREE;
3899 goto done_asm;
3901 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3903 c_lex_string_translate = 1;
3904 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3905 return NULL_TREE;
3907 simple = false;
3908 /* Parse outputs. */
3909 if (c_parser_next_token_is (parser, CPP_COLON)
3910 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3911 outputs = NULL_TREE;
3912 else
3913 outputs = c_parser_asm_operands (parser);
3914 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3916 inputs = NULL_TREE;
3917 clobbers = NULL_TREE;
3918 goto done_asm;
3920 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3922 c_lex_string_translate = 1;
3923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3924 return NULL_TREE;
3926 /* Parse inputs. */
3927 if (c_parser_next_token_is (parser, CPP_COLON)
3928 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3929 inputs = NULL_TREE;
3930 else
3931 inputs = c_parser_asm_operands (parser);
3932 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3934 clobbers = NULL_TREE;
3935 goto done_asm;
3937 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3939 c_lex_string_translate = 1;
3940 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3941 return NULL_TREE;
3943 /* Parse clobbers. */
3944 clobbers = c_parser_asm_clobbers (parser);
3945 done_asm:
3946 c_lex_string_translate = 1;
3947 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3950 return NULL_TREE;
3952 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3953 c_parser_skip_to_end_of_block_or_statement (parser);
3954 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
3955 clobbers, simple));
3956 return ret;
3959 /* Parse asm operands, a GNU extension.
3961 asm-operands:
3962 asm-operand
3963 asm-operands , asm-operand
3965 asm-operand:
3966 asm-string-literal ( expression )
3967 [ identifier ] asm-string-literal ( expression )
3970 static tree
3971 c_parser_asm_operands (c_parser *parser)
3973 tree list = NULL_TREE;
3974 while (true)
3976 tree name, str, expr;
3977 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3979 c_parser_consume_token (parser);
3980 if (c_parser_next_token_is (parser, CPP_NAME))
3982 tree id = c_parser_peek_token (parser)->value;
3983 c_parser_consume_token (parser);
3984 name = build_string (IDENTIFIER_LENGTH (id),
3985 IDENTIFIER_POINTER (id));
3987 else
3989 c_parser_error (parser, "expected identifier");
3990 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3991 return NULL_TREE;
3993 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3994 "expected %<]%>");
3996 else
3997 name = NULL_TREE;
3998 str = c_parser_asm_string_literal (parser);
3999 if (str == NULL_TREE)
4000 return NULL_TREE;
4001 c_lex_string_translate = 1;
4002 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4004 c_lex_string_translate = 0;
4005 return NULL_TREE;
4007 expr = c_parser_expression (parser).value;
4008 c_lex_string_translate = 0;
4009 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4012 return NULL_TREE;
4014 list = chainon (list, build_tree_list (build_tree_list (name, str),
4015 expr));
4016 if (c_parser_next_token_is (parser, CPP_COMMA))
4017 c_parser_consume_token (parser);
4018 else
4019 break;
4021 return list;
4024 /* Parse asm clobbers, a GNU extension.
4026 asm-clobbers:
4027 asm-string-literal
4028 asm-clobbers , asm-string-literal
4031 static tree
4032 c_parser_asm_clobbers (c_parser *parser)
4034 tree list = NULL_TREE;
4035 while (true)
4037 tree str = c_parser_asm_string_literal (parser);
4038 if (str)
4039 list = tree_cons (NULL_TREE, str, list);
4040 else
4041 return NULL_TREE;
4042 if (c_parser_next_token_is (parser, CPP_COMMA))
4043 c_parser_consume_token (parser);
4044 else
4045 break;
4047 return list;
4050 /* Parse an expression other than a compound expression; that is, an
4051 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4052 NULL then it is an Objective-C message expression which is the
4053 primary-expression starting the expression as an initializer.
4055 assignment-expression:
4056 conditional-expression
4057 unary-expression assignment-operator assignment-expression
4059 assignment-operator: one of
4060 = *= /= %= += -= <<= >>= &= ^= |=
4062 In GNU C we accept any conditional expression on the LHS and
4063 diagnose the invalid lvalue rather than producing a syntax
4064 error. */
4066 static struct c_expr
4067 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4069 struct c_expr lhs, rhs, ret;
4070 enum tree_code code;
4071 gcc_assert (!after || c_dialect_objc ());
4072 lhs = c_parser_conditional_expression (parser, after);
4073 switch (c_parser_peek_token (parser)->type)
4075 case CPP_EQ:
4076 code = NOP_EXPR;
4077 break;
4078 case CPP_MULT_EQ:
4079 code = MULT_EXPR;
4080 break;
4081 case CPP_DIV_EQ:
4082 code = TRUNC_DIV_EXPR;
4083 break;
4084 case CPP_MOD_EQ:
4085 code = TRUNC_MOD_EXPR;
4086 break;
4087 case CPP_PLUS_EQ:
4088 code = PLUS_EXPR;
4089 break;
4090 case CPP_MINUS_EQ:
4091 code = MINUS_EXPR;
4092 break;
4093 case CPP_LSHIFT_EQ:
4094 code = LSHIFT_EXPR;
4095 break;
4096 case CPP_RSHIFT_EQ:
4097 code = RSHIFT_EXPR;
4098 break;
4099 case CPP_AND_EQ:
4100 code = BIT_AND_EXPR;
4101 break;
4102 case CPP_XOR_EQ:
4103 code = BIT_XOR_EXPR;
4104 break;
4105 case CPP_OR_EQ:
4106 code = BIT_IOR_EXPR;
4107 break;
4108 default:
4109 return lhs;
4111 c_parser_consume_token (parser);
4112 rhs = c_parser_expr_no_commas (parser, NULL);
4113 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4114 if (code == NOP_EXPR)
4115 ret.original_code = MODIFY_EXPR;
4116 else
4118 TREE_NO_WARNING (ret.value) = 1;
4119 ret.original_code = ERROR_MARK;
4121 return ret;
4124 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4125 is not NULL then it is an Objective-C message expression which is
4126 the primary-expression starting the expression as an initializer.
4128 conditional-expression:
4129 logical-OR-expression
4130 logical-OR-expression ? expression : conditional-expression
4132 GNU extensions:
4134 conditional-expression:
4135 logical-OR-expression ? : conditional-expression
4138 static struct c_expr
4139 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4141 struct c_expr cond, exp1, exp2, ret;
4142 gcc_assert (!after || c_dialect_objc ());
4143 cond = c_parser_binary_expression (parser, after);
4144 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4145 return cond;
4146 c_parser_consume_token (parser);
4147 if (c_parser_next_token_is (parser, CPP_COLON))
4149 if (pedantic)
4150 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4151 /* Make sure first operand is calculated only once. */
4152 exp1.value = save_expr (default_conversion (cond.value));
4153 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4154 skip_evaluation += cond.value == truthvalue_true_node;
4156 else
4158 cond.value
4159 = c_objc_common_truthvalue_conversion
4160 (default_conversion (cond.value));
4161 skip_evaluation += cond.value == truthvalue_false_node;
4162 exp1 = c_parser_expression (parser);
4163 skip_evaluation += ((cond.value == truthvalue_true_node)
4164 - (cond.value == truthvalue_false_node));
4166 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4168 skip_evaluation -= cond.value == truthvalue_true_node;
4169 ret.value = error_mark_node;
4170 ret.original_code = ERROR_MARK;
4171 return ret;
4173 exp2 = c_parser_conditional_expression (parser, NULL);
4174 skip_evaluation -= cond.value == truthvalue_true_node;
4175 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4176 ret.original_code = ERROR_MARK;
4177 return ret;
4180 /* Parse a binary expression; that is, a logical-OR-expression (C90
4181 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4182 an Objective-C message expression which is the primary-expression
4183 starting the expression as an initializer.
4185 multiplicative-expression:
4186 cast-expression
4187 multiplicative-expression * cast-expression
4188 multiplicative-expression / cast-expression
4189 multiplicative-expression % cast-expression
4191 additive-expression:
4192 multiplicative-expression
4193 additive-expression + multiplicative-expression
4194 additive-expression - multiplicative-expression
4196 shift-expression:
4197 additive-expression
4198 shift-expression << additive-expression
4199 shift-expression >> additive-expression
4201 relational-expression:
4202 shift-expression
4203 relational-expression < shift-expression
4204 relational-expression > shift-expression
4205 relational-expression <= shift-expression
4206 relational-expression >= shift-expression
4208 equality-expression:
4209 relational-expression
4210 equality-expression == relational-expression
4211 equality-expression != relational-expression
4213 AND-expression:
4214 equality-expression
4215 AND-expression & equality-expression
4217 exclusive-OR-expression:
4218 AND-expression
4219 exclusive-OR-expression ^ AND-expression
4221 inclusive-OR-expression:
4222 exclusive-OR-expression
4223 inclusive-OR-expression | exclusive-OR-expression
4225 logical-AND-expression:
4226 inclusive-OR-expression
4227 logical-AND-expression && inclusive-OR-expression
4229 logical-OR-expression:
4230 logical-AND-expression
4231 logical-OR-expression || logical-AND-expression
4234 static struct c_expr
4235 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4237 /* A binary expression is parsed using operator-precedence parsing,
4238 with the operands being cast expressions. All the binary
4239 operators are left-associative. Thus a binary expression is of
4240 form:
4242 E0 op1 E1 op2 E2 ...
4244 which we represent on a stack. On the stack, the precedence
4245 levels are strictly increasing. When a new operator is
4246 encountered of higher precedence than that at the top of the
4247 stack, it is pushed; its LHS is the top expression, and its RHS
4248 is everything parsed until it is popped. When a new operator is
4249 encountered with precedence less than or equal to that at the top
4250 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4251 by the result of the operation until the operator at the top of
4252 the stack has lower precedence than the new operator or there is
4253 only one element on the stack; then the top expression is the LHS
4254 of the new operator. In the case of logical AND and OR
4255 expressions, we also need to adjust skip_evaluation as
4256 appropriate when the operators are pushed and popped. */
4258 /* The precedence levels, where 0 is a dummy lowest level used for
4259 the bottom of the stack. */
4260 enum prec {
4261 PREC_NONE,
4262 PREC_LOGOR,
4263 PREC_LOGAND,
4264 PREC_BITOR,
4265 PREC_BITXOR,
4266 PREC_BITAND,
4267 PREC_EQ,
4268 PREC_REL,
4269 PREC_SHIFT,
4270 PREC_ADD,
4271 PREC_MULT,
4272 NUM_PRECS
4274 struct {
4275 /* The expression at this stack level. */
4276 struct c_expr expr;
4277 /* The precedence of the operator on its left, PREC_NONE at the
4278 bottom of the stack. */
4279 enum prec prec;
4280 /* The operation on its left. */
4281 enum tree_code op;
4282 } stack[NUM_PRECS];
4283 int sp;
4284 #define POP \
4285 do { \
4286 switch (stack[sp].op) \
4288 case TRUTH_ANDIF_EXPR: \
4289 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4290 break; \
4291 case TRUTH_ORIF_EXPR: \
4292 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4293 break; \
4294 default: \
4295 break; \
4297 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4298 stack[sp - 1].expr, \
4299 stack[sp].expr); \
4300 sp--; \
4301 } while (0)
4302 gcc_assert (!after || c_dialect_objc ());
4303 stack[0].expr = c_parser_cast_expression (parser, after);
4304 stack[0].prec = PREC_NONE;
4305 sp = 0;
4306 while (true)
4308 enum prec oprec;
4309 enum tree_code ocode;
4310 if (parser->error)
4311 goto out;
4312 switch (c_parser_peek_token (parser)->type)
4314 case CPP_MULT:
4315 oprec = PREC_MULT;
4316 ocode = MULT_EXPR;
4317 break;
4318 case CPP_DIV:
4319 oprec = PREC_MULT;
4320 ocode = TRUNC_DIV_EXPR;
4321 break;
4322 case CPP_MOD:
4323 oprec = PREC_MULT;
4324 ocode = TRUNC_MOD_EXPR;
4325 break;
4326 case CPP_PLUS:
4327 oprec = PREC_ADD;
4328 ocode = PLUS_EXPR;
4329 break;
4330 case CPP_MINUS:
4331 oprec = PREC_ADD;
4332 ocode = MINUS_EXPR;
4333 break;
4334 case CPP_LSHIFT:
4335 oprec = PREC_SHIFT;
4336 ocode = LSHIFT_EXPR;
4337 break;
4338 case CPP_RSHIFT:
4339 oprec = PREC_SHIFT;
4340 ocode = RSHIFT_EXPR;
4341 break;
4342 case CPP_LESS:
4343 oprec = PREC_REL;
4344 ocode = LT_EXPR;
4345 break;
4346 case CPP_GREATER:
4347 oprec = PREC_REL;
4348 ocode = GT_EXPR;
4349 break;
4350 case CPP_LESS_EQ:
4351 oprec = PREC_REL;
4352 ocode = LE_EXPR;
4353 break;
4354 case CPP_GREATER_EQ:
4355 oprec = PREC_REL;
4356 ocode = GE_EXPR;
4357 break;
4358 case CPP_EQ_EQ:
4359 oprec = PREC_EQ;
4360 ocode = EQ_EXPR;
4361 break;
4362 case CPP_NOT_EQ:
4363 oprec = PREC_EQ;
4364 ocode = NE_EXPR;
4365 break;
4366 case CPP_AND:
4367 oprec = PREC_BITAND;
4368 ocode = BIT_AND_EXPR;
4369 break;
4370 case CPP_XOR:
4371 oprec = PREC_BITXOR;
4372 ocode = BIT_XOR_EXPR;
4373 break;
4374 case CPP_OR:
4375 oprec = PREC_BITOR;
4376 ocode = BIT_IOR_EXPR;
4377 break;
4378 case CPP_AND_AND:
4379 oprec = PREC_LOGAND;
4380 ocode = TRUTH_ANDIF_EXPR;
4381 break;
4382 case CPP_OR_OR:
4383 oprec = PREC_LOGOR;
4384 ocode = TRUTH_ORIF_EXPR;
4385 break;
4386 default:
4387 /* Not a binary operator, so end of the binary
4388 expression. */
4389 goto out;
4391 c_parser_consume_token (parser);
4392 while (oprec <= stack[sp].prec)
4393 POP;
4394 switch (ocode)
4396 case TRUTH_ANDIF_EXPR:
4397 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4398 (default_conversion (stack[sp].expr.value));
4399 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4400 break;
4401 case TRUTH_ORIF_EXPR:
4402 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4403 (default_conversion (stack[sp].expr.value));
4404 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4405 break;
4406 default:
4407 break;
4409 sp++;
4410 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4411 stack[sp].prec = oprec;
4412 stack[sp].op = ocode;
4414 out:
4415 while (sp > 0)
4416 POP;
4417 return stack[0].expr;
4418 #undef POP
4421 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4422 NULL then it is an Objective-C message expression which is the
4423 primary-expression starting the expression as an initializer.
4425 cast-expression:
4426 unary-expression
4427 ( type-name ) unary-expression
4430 static struct c_expr
4431 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4433 gcc_assert (!after || c_dialect_objc ());
4434 if (after)
4435 return c_parser_postfix_expression_after_primary (parser, *after);
4436 /* If the expression begins with a parenthesized type name, it may
4437 be either a cast or a compound literal; we need to see whether
4438 the next character is '{' to tell the difference. If not, it is
4439 an unary expression. */
4440 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4441 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4443 struct c_type_name *type_name;
4444 struct c_expr ret;
4445 tree expr;
4446 c_parser_consume_token (parser);
4447 type_name = c_parser_type_name (parser);
4448 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4449 if (type_name == NULL)
4451 ret.value = error_mark_node;
4452 ret.original_code = ERROR_MARK;
4453 return ret;
4455 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4456 return c_parser_postfix_expression_after_paren_type (parser,
4457 type_name);
4458 expr = c_parser_cast_expression (parser, NULL).value;
4459 ret.value = c_cast_expr (type_name, expr);
4460 ret.original_code = ERROR_MARK;
4461 return ret;
4463 else
4464 return c_parser_unary_expression (parser);
4467 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4469 unary-expression:
4470 postfix-expression
4471 ++ unary-expression
4472 -- unary-expression
4473 unary-operator cast-expression
4474 sizeof unary-expression
4475 sizeof ( type-name )
4477 unary-operator: one of
4478 & * + - ~ !
4480 GNU extensions:
4482 unary-expression:
4483 __alignof__ unary-expression
4484 __alignof__ ( type-name )
4485 && identifier
4487 unary-operator: one of
4488 __extension__ __real__ __imag__
4490 In addition, the GNU syntax treats ++ and -- as unary operators, so
4491 they may be applied to cast expressions with errors for non-lvalues
4492 given later. */
4494 static struct c_expr
4495 c_parser_unary_expression (c_parser *parser)
4497 int ext;
4498 struct c_expr ret;
4499 switch (c_parser_peek_token (parser)->type)
4501 case CPP_PLUS_PLUS:
4502 c_parser_consume_token (parser);
4503 return parser_build_unary_op (PREINCREMENT_EXPR,
4504 c_parser_cast_expression (parser, NULL));
4505 case CPP_MINUS_MINUS:
4506 c_parser_consume_token (parser);
4507 return parser_build_unary_op (PREDECREMENT_EXPR,
4508 c_parser_cast_expression (parser, NULL));
4509 case CPP_AND:
4510 c_parser_consume_token (parser);
4511 return parser_build_unary_op (ADDR_EXPR,
4512 c_parser_cast_expression (parser, NULL));
4513 case CPP_MULT:
4514 c_parser_consume_token (parser);
4515 ret.value
4516 = build_indirect_ref (c_parser_cast_expression (parser, NULL).value,
4517 "unary *");
4518 ret.original_code = ERROR_MARK;
4519 return ret;
4520 case CPP_PLUS:
4521 c_parser_consume_token (parser);
4522 if (!c_dialect_objc () && !in_system_header)
4523 warning (OPT_Wtraditional,
4524 "traditional C rejects the unary plus operator");
4525 return parser_build_unary_op (CONVERT_EXPR,
4526 c_parser_cast_expression (parser, NULL));
4527 case CPP_MINUS:
4528 c_parser_consume_token (parser);
4529 return parser_build_unary_op (NEGATE_EXPR,
4530 c_parser_cast_expression (parser, NULL));
4531 case CPP_COMPL:
4532 c_parser_consume_token (parser);
4533 return parser_build_unary_op (BIT_NOT_EXPR,
4534 c_parser_cast_expression (parser, NULL));
4535 case CPP_NOT:
4536 c_parser_consume_token (parser);
4537 return parser_build_unary_op (TRUTH_NOT_EXPR,
4538 c_parser_cast_expression (parser, NULL));
4539 case CPP_AND_AND:
4540 /* Refer to the address of a label as a pointer. */
4541 c_parser_consume_token (parser);
4542 if (c_parser_next_token_is (parser, CPP_NAME))
4544 ret.value = finish_label_address_expr
4545 (c_parser_peek_token (parser)->value);
4546 c_parser_consume_token (parser);
4548 else
4550 c_parser_error (parser, "expected identifier");
4551 ret.value = error_mark_node;
4553 ret.original_code = ERROR_MARK;
4554 return ret;
4555 case CPP_KEYWORD:
4556 switch (c_parser_peek_token (parser)->keyword)
4558 case RID_SIZEOF:
4559 return c_parser_sizeof_expression (parser);
4560 case RID_ALIGNOF:
4561 return c_parser_alignof_expression (parser);
4562 case RID_EXTENSION:
4563 c_parser_consume_token (parser);
4564 ext = disable_extension_diagnostics ();
4565 ret = c_parser_cast_expression (parser, NULL);
4566 restore_extension_diagnostics (ext);
4567 return ret;
4568 case RID_REALPART:
4569 c_parser_consume_token (parser);
4570 return parser_build_unary_op (REALPART_EXPR,
4571 c_parser_cast_expression (parser,
4572 NULL));
4573 case RID_IMAGPART:
4574 c_parser_consume_token (parser);
4575 return parser_build_unary_op (IMAGPART_EXPR,
4576 c_parser_cast_expression (parser,
4577 NULL));
4578 default:
4579 return c_parser_postfix_expression (parser);
4581 default:
4582 return c_parser_postfix_expression (parser);
4586 /* Parse a sizeof expression. */
4588 static struct c_expr
4589 c_parser_sizeof_expression (c_parser *parser)
4591 struct c_expr expr;
4592 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4593 c_parser_consume_token (parser);
4594 skip_evaluation++;
4595 in_sizeof++;
4596 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4597 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4599 /* Either sizeof ( type-name ) or sizeof unary-expression
4600 starting with a compound literal. */
4601 struct c_type_name *type_name;
4602 c_parser_consume_token (parser);
4603 type_name = c_parser_type_name (parser);
4604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4605 if (type_name == NULL)
4607 struct c_expr ret;
4608 skip_evaluation--;
4609 in_sizeof--;
4610 ret.value = error_mark_node;
4611 ret.original_code = ERROR_MARK;
4612 return ret;
4614 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4616 expr = c_parser_postfix_expression_after_paren_type (parser,
4617 type_name);
4618 goto sizeof_expr;
4620 /* sizeof ( type-name ). */
4621 skip_evaluation--;
4622 in_sizeof--;
4623 return c_expr_sizeof_type (type_name);
4625 else
4627 expr = c_parser_unary_expression (parser);
4628 sizeof_expr:
4629 skip_evaluation--;
4630 in_sizeof--;
4631 if (TREE_CODE (expr.value) == COMPONENT_REF
4632 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4633 error ("%<sizeof%> applied to a bit-field");
4634 return c_expr_sizeof_expr (expr);
4638 /* Parse an alignof expression. */
4640 static struct c_expr
4641 c_parser_alignof_expression (c_parser *parser)
4643 struct c_expr expr;
4644 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4645 c_parser_consume_token (parser);
4646 skip_evaluation++;
4647 in_alignof++;
4648 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4649 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4651 /* Either __alignof__ ( type-name ) or __alignof__
4652 unary-expression starting with a compound literal. */
4653 struct c_type_name *type_name;
4654 struct c_expr ret;
4655 c_parser_consume_token (parser);
4656 type_name = c_parser_type_name (parser);
4657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4658 if (type_name == NULL)
4660 struct c_expr ret;
4661 skip_evaluation--;
4662 in_alignof--;
4663 ret.value = error_mark_node;
4664 ret.original_code = ERROR_MARK;
4665 return ret;
4667 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4669 expr = c_parser_postfix_expression_after_paren_type (parser,
4670 type_name);
4671 goto alignof_expr;
4673 /* alignof ( type-name ). */
4674 skip_evaluation--;
4675 in_alignof--;
4676 ret.value = c_alignof (groktypename (type_name));
4677 ret.original_code = ERROR_MARK;
4678 return ret;
4680 else
4682 struct c_expr ret;
4683 expr = c_parser_unary_expression (parser);
4684 alignof_expr:
4685 skip_evaluation--;
4686 in_alignof--;
4687 ret.value = c_alignof_expr (expr.value);
4688 ret.original_code = ERROR_MARK;
4689 return ret;
4693 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4695 postfix-expression:
4696 primary-expression
4697 postfix-expression [ expression ]
4698 postfix-expression ( argument-expression-list[opt] )
4699 postfix-expression . identifier
4700 postfix-expression -> identifier
4701 postfix-expression ++
4702 postfix-expression --
4703 ( type-name ) { initializer-list }
4704 ( type-name ) { initializer-list , }
4706 argument-expression-list:
4707 argument-expression
4708 argument-expression-list , argument-expression
4710 primary-expression:
4711 identifier
4712 constant
4713 string-literal
4714 ( expression )
4716 GNU extensions:
4718 primary-expression:
4719 __func__
4720 (treated as a keyword in GNU C)
4721 __FUNCTION__
4722 __PRETTY_FUNCTION__
4723 ( compound-statement )
4724 __builtin_va_arg ( assignment-expression , type-name )
4725 __builtin_offsetof ( type-name , offsetof-member-designator )
4726 __builtin_choose_expr ( assignment-expression ,
4727 assignment-expression ,
4728 assignment-expression )
4729 __builtin_types_compatible_p ( type-name , type-name )
4731 offsetof-member-designator:
4732 identifier
4733 offsetof-member-designator . identifier
4734 offsetof-member-designator [ expression ]
4736 Objective-C:
4738 primary-expression:
4739 [ objc-receiver objc-message-args ]
4740 @selector ( objc-selector-arg )
4741 @protocol ( identifier )
4742 @encode ( type-name )
4743 objc-string-literal
4746 static struct c_expr
4747 c_parser_postfix_expression (c_parser *parser)
4749 struct c_expr expr, e1, e2, e3;
4750 struct c_type_name *t1, *t2;
4751 switch (c_parser_peek_token (parser)->type)
4753 case CPP_NUMBER:
4754 case CPP_CHAR:
4755 case CPP_WCHAR:
4756 expr.value = c_parser_peek_token (parser)->value;
4757 expr.original_code = ERROR_MARK;
4758 c_parser_consume_token (parser);
4759 break;
4760 case CPP_STRING:
4761 case CPP_WSTRING:
4762 expr.value = c_parser_peek_token (parser)->value;
4763 expr.original_code = STRING_CST;
4764 c_parser_consume_token (parser);
4765 break;
4766 case CPP_OBJC_STRING:
4767 gcc_assert (c_dialect_objc ());
4768 expr.value
4769 = objc_build_string_object (c_parser_peek_token (parser)->value);
4770 expr.original_code = ERROR_MARK;
4771 c_parser_consume_token (parser);
4772 break;
4773 case CPP_NAME:
4774 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4776 c_parser_error (parser, "expected expression");
4777 expr.value = error_mark_node;
4778 expr.original_code = ERROR_MARK;
4779 break;
4782 tree id = c_parser_peek_token (parser)->value;
4783 location_t loc = c_parser_peek_token (parser)->location;
4784 c_parser_consume_token (parser);
4785 expr.value = build_external_ref (id,
4786 (c_parser_peek_token (parser)->type
4787 == CPP_OPEN_PAREN), loc);
4788 expr.original_code = ERROR_MARK;
4790 break;
4791 case CPP_OPEN_PAREN:
4792 /* A parenthesized expression, statement expression or compound
4793 literal. */
4794 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4796 /* A statement expression. */
4797 tree stmt;
4798 c_parser_consume_token (parser);
4799 c_parser_consume_token (parser);
4800 if (cur_stmt_list == NULL)
4802 error ("braced-group within expression allowed "
4803 "only inside a function");
4804 parser->error = true;
4805 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4807 expr.value = error_mark_node;
4808 expr.original_code = ERROR_MARK;
4809 break;
4811 stmt = c_begin_stmt_expr ();
4812 c_parser_compound_statement_nostart (parser);
4813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4814 "expected %<)%>");
4815 if (pedantic)
4816 pedwarn ("ISO C forbids braced-groups within expressions");
4817 expr.value = c_finish_stmt_expr (stmt);
4818 expr.original_code = ERROR_MARK;
4820 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4822 /* A compound literal. ??? Can we actually get here rather
4823 than going directly to
4824 c_parser_postfix_expression_after_paren_type from
4825 elsewhere? */
4826 struct c_type_name *type_name;
4827 c_parser_consume_token (parser);
4828 type_name = c_parser_type_name (parser);
4829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4830 "expected %<)%>");
4831 if (type_name == NULL)
4833 expr.value = error_mark_node;
4834 expr.original_code = ERROR_MARK;
4836 else
4837 expr = c_parser_postfix_expression_after_paren_type (parser,
4838 type_name);
4840 else
4842 /* A parenthesized expression. */
4843 c_parser_consume_token (parser);
4844 expr = c_parser_expression (parser);
4845 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4846 TREE_NO_WARNING (expr.value) = 1;
4847 expr.original_code = ERROR_MARK;
4848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4849 "expected %<)%>");
4851 break;
4852 case CPP_KEYWORD:
4853 switch (c_parser_peek_token (parser)->keyword)
4855 case RID_FUNCTION_NAME:
4856 case RID_PRETTY_FUNCTION_NAME:
4857 case RID_C99_FUNCTION_NAME:
4858 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4859 c_parser_peek_token (parser)->value);
4860 expr.original_code = ERROR_MARK;
4861 c_parser_consume_token (parser);
4862 break;
4863 case RID_VA_ARG:
4864 c_parser_consume_token (parser);
4865 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4867 expr.value = error_mark_node;
4868 expr.original_code = ERROR_MARK;
4869 break;
4871 e1 = c_parser_expr_no_commas (parser, NULL);
4872 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4875 expr.value = error_mark_node;
4876 expr.original_code = ERROR_MARK;
4877 break;
4879 t1 = c_parser_type_name (parser);
4880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4881 "expected %<)%>");
4882 if (t1 == NULL)
4884 expr.value = error_mark_node;
4885 expr.original_code = ERROR_MARK;
4887 else
4889 expr.value = build_va_arg (e1.value, groktypename (t1));
4890 expr.original_code = ERROR_MARK;
4892 break;
4893 case RID_OFFSETOF:
4894 c_parser_consume_token (parser);
4895 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4897 expr.value = error_mark_node;
4898 expr.original_code = ERROR_MARK;
4899 break;
4901 t1 = c_parser_type_name (parser);
4902 if (t1 == NULL)
4904 expr.value = error_mark_node;
4905 expr.original_code = ERROR_MARK;
4906 break;
4908 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4910 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4911 expr.value = error_mark_node;
4912 expr.original_code = ERROR_MARK;
4913 break;
4916 tree type = groktypename (t1);
4917 tree offsetof_ref;
4918 if (type == error_mark_node)
4919 offsetof_ref = error_mark_node;
4920 else
4921 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
4922 /* Parse the second argument to __builtin_offsetof. We
4923 must have one identifier, and beyond that we want to
4924 accept sub structure and sub array references. */
4925 if (c_parser_next_token_is (parser, CPP_NAME))
4927 offsetof_ref = build_component_ref
4928 (offsetof_ref, c_parser_peek_token (parser)->value);
4929 c_parser_consume_token (parser);
4930 while (c_parser_next_token_is (parser, CPP_DOT)
4931 || c_parser_next_token_is (parser,
4932 CPP_OPEN_SQUARE))
4934 if (c_parser_next_token_is (parser, CPP_DOT))
4936 c_parser_consume_token (parser);
4937 if (c_parser_next_token_is_not (parser,
4938 CPP_NAME))
4940 c_parser_error (parser, "expected identifier");
4941 break;
4943 offsetof_ref = build_component_ref
4944 (offsetof_ref,
4945 c_parser_peek_token (parser)->value);
4946 c_parser_consume_token (parser);
4948 else
4950 tree idx;
4951 c_parser_consume_token (parser);
4952 idx = c_parser_expression (parser).value;
4953 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4954 "expected %<]%>");
4955 offsetof_ref = build_array_ref (offsetof_ref, idx);
4959 else
4960 c_parser_error (parser, "expected identifier");
4961 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4962 "expected %<)%>");
4963 expr.value = fold_offsetof (offsetof_ref);
4964 expr.original_code = ERROR_MARK;
4966 break;
4967 case RID_CHOOSE_EXPR:
4968 c_parser_consume_token (parser);
4969 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4971 expr.value = error_mark_node;
4972 expr.original_code = ERROR_MARK;
4973 break;
4975 e1 = c_parser_expr_no_commas (parser, NULL);
4976 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4978 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4979 expr.value = error_mark_node;
4980 expr.original_code = ERROR_MARK;
4981 break;
4983 e2 = c_parser_expr_no_commas (parser, NULL);
4984 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4986 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4987 expr.value = error_mark_node;
4988 expr.original_code = ERROR_MARK;
4989 break;
4991 e3 = c_parser_expr_no_commas (parser, NULL);
4992 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4993 "expected %<)%>");
4995 tree c;
4997 c = fold (e1.value);
4998 if (TREE_CODE (c) != INTEGER_CST)
4999 error ("first argument to %<__builtin_choose_expr%> not"
5000 " a constant");
5001 expr = integer_zerop (c) ? e3 : e2;
5003 break;
5004 case RID_TYPES_COMPATIBLE_P:
5005 c_parser_consume_token (parser);
5006 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5008 expr.value = error_mark_node;
5009 expr.original_code = ERROR_MARK;
5010 break;
5012 t1 = c_parser_type_name (parser);
5013 if (t1 == NULL)
5015 expr.value = error_mark_node;
5016 expr.original_code = ERROR_MARK;
5017 break;
5019 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5021 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5022 expr.value = error_mark_node;
5023 expr.original_code = ERROR_MARK;
5024 break;
5026 t2 = c_parser_type_name (parser);
5027 if (t2 == NULL)
5029 expr.value = error_mark_node;
5030 expr.original_code = ERROR_MARK;
5031 break;
5033 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5034 "expected %<)%>");
5036 tree e1, e2;
5038 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5039 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5041 expr.value = comptypes (e1, e2)
5042 ? build_int_cst (NULL_TREE, 1)
5043 : build_int_cst (NULL_TREE, 0);
5044 expr.original_code = ERROR_MARK;
5046 break;
5047 case RID_AT_SELECTOR:
5048 gcc_assert (c_dialect_objc ());
5049 c_parser_consume_token (parser);
5050 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5052 expr.value = error_mark_node;
5053 expr.original_code = ERROR_MARK;
5054 break;
5057 tree sel = c_parser_objc_selector_arg (parser);
5058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5059 "expected %<)%>");
5060 expr.value = objc_build_selector_expr (sel);
5061 expr.original_code = ERROR_MARK;
5063 break;
5064 case RID_AT_PROTOCOL:
5065 gcc_assert (c_dialect_objc ());
5066 c_parser_consume_token (parser);
5067 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5069 expr.value = error_mark_node;
5070 expr.original_code = ERROR_MARK;
5071 break;
5073 if (c_parser_next_token_is_not (parser, CPP_NAME))
5075 c_parser_error (parser, "expected identifier");
5076 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5077 expr.value = error_mark_node;
5078 expr.original_code = ERROR_MARK;
5079 break;
5082 tree id = c_parser_peek_token (parser)->value;
5083 c_parser_consume_token (parser);
5084 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5085 "expected %<)%>");
5086 expr.value = objc_build_protocol_expr (id);
5087 expr.original_code = ERROR_MARK;
5089 break;
5090 case RID_AT_ENCODE:
5091 /* Extension to support C-structures in the archiver. */
5092 gcc_assert (c_dialect_objc ());
5093 c_parser_consume_token (parser);
5094 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5096 expr.value = error_mark_node;
5097 expr.original_code = ERROR_MARK;
5098 break;
5100 t1 = c_parser_type_name (parser);
5101 if (t1 == NULL)
5103 expr.value = error_mark_node;
5104 expr.original_code = ERROR_MARK;
5105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5106 break;
5108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5109 "expected %<)%>");
5111 tree type = groktypename (t1);
5112 expr.value = objc_build_encode_expr (type);
5113 expr.original_code = ERROR_MARK;
5115 break;
5116 default:
5117 c_parser_error (parser, "expected expression");
5118 expr.value = error_mark_node;
5119 expr.original_code = ERROR_MARK;
5120 break;
5122 break;
5123 case CPP_OPEN_SQUARE:
5124 if (c_dialect_objc ())
5126 tree receiver, args;
5127 c_parser_consume_token (parser);
5128 receiver = c_parser_objc_receiver (parser);
5129 args = c_parser_objc_message_args (parser);
5130 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5131 "expected %<]%>");
5132 expr.value = objc_build_message_expr (build_tree_list (receiver,
5133 args));
5134 expr.original_code = ERROR_MARK;
5135 break;
5137 /* Else fall through to report error. */
5138 default:
5139 c_parser_error (parser, "expected expression");
5140 expr.value = error_mark_node;
5141 expr.original_code = ERROR_MARK;
5142 break;
5144 return c_parser_postfix_expression_after_primary (parser, expr);
5147 /* Parse a postfix expression after a parenthesized type name: the
5148 brace-enclosed initializer of a compound literal, possibly followed
5149 by some postfix operators. This is separate because it is not
5150 possible to tell until after the type name whether a cast
5151 expression has a cast or a compound literal, or whether the operand
5152 of sizeof is a parenthesized type name or starts with a compound
5153 literal. */
5155 static struct c_expr
5156 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5157 struct c_type_name *type_name)
5159 tree type;
5160 struct c_expr init;
5161 struct c_expr expr;
5162 start_init (NULL_TREE, NULL, 0);
5163 type = groktypename (type_name);
5164 if (C_TYPE_VARIABLE_SIZE (type))
5166 error ("compound literal has variable size");
5167 type = error_mark_node;
5169 init = c_parser_braced_init (parser, type, false);
5170 finish_init ();
5171 maybe_warn_string_init (type, init);
5173 if (pedantic && !flag_isoc99)
5174 pedwarn ("ISO C90 forbids compound literals");
5175 expr.value = build_compound_literal (type, init.value);
5176 expr.original_code = ERROR_MARK;
5177 return c_parser_postfix_expression_after_primary (parser, expr);
5180 /* Parse a postfix expression after the initial primary or compound
5181 literal; that is, parse a series of postfix operators. */
5183 static struct c_expr
5184 c_parser_postfix_expression_after_primary (c_parser *parser,
5185 struct c_expr expr)
5187 tree ident, idx, exprlist;
5188 while (true)
5190 switch (c_parser_peek_token (parser)->type)
5192 case CPP_OPEN_SQUARE:
5193 /* Array reference. */
5194 c_parser_consume_token (parser);
5195 idx = c_parser_expression (parser).value;
5196 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5197 "expected %<]%>");
5198 expr.value = build_array_ref (expr.value, idx);
5199 expr.original_code = ERROR_MARK;
5200 break;
5201 case CPP_OPEN_PAREN:
5202 /* Function call. */
5203 c_parser_consume_token (parser);
5204 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5205 exprlist = NULL_TREE;
5206 else
5207 exprlist = c_parser_expr_list (parser);
5208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5209 "expected %<)%>");
5210 expr.value = build_function_call (expr.value, exprlist);
5211 expr.original_code = ERROR_MARK;
5212 break;
5213 case CPP_DOT:
5214 /* Structure element reference. */
5215 c_parser_consume_token (parser);
5216 if (c_parser_next_token_is (parser, CPP_NAME))
5217 ident = c_parser_peek_token (parser)->value;
5218 else
5220 c_parser_error (parser, "expected identifier");
5221 expr.value = error_mark_node;
5222 expr.original_code = ERROR_MARK;
5223 return expr;
5225 c_parser_consume_token (parser);
5226 expr.value = build_component_ref (expr.value, ident);
5227 expr.original_code = ERROR_MARK;
5228 break;
5229 case CPP_DEREF:
5230 /* Structure element reference. */
5231 c_parser_consume_token (parser);
5232 if (c_parser_next_token_is (parser, CPP_NAME))
5233 ident = c_parser_peek_token (parser)->value;
5234 else
5236 c_parser_error (parser, "expected identifier");
5237 expr.value = error_mark_node;
5238 expr.original_code = ERROR_MARK;
5239 return expr;
5241 c_parser_consume_token (parser);
5242 expr.value = build_component_ref (build_indirect_ref (expr.value,
5243 "->"), ident);
5244 expr.original_code = ERROR_MARK;
5245 break;
5246 case CPP_PLUS_PLUS:
5247 /* Postincrement. */
5248 c_parser_consume_token (parser);
5249 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5250 expr.original_code = ERROR_MARK;
5251 break;
5252 case CPP_MINUS_MINUS:
5253 /* Postdecrement. */
5254 c_parser_consume_token (parser);
5255 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5256 expr.original_code = ERROR_MARK;
5257 break;
5258 default:
5259 return expr;
5264 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5266 expression:
5267 assignment-expression
5268 expression , assignment-expression
5271 static struct c_expr
5272 c_parser_expression (c_parser *parser)
5274 struct c_expr expr;
5275 expr = c_parser_expr_no_commas (parser, NULL);
5276 while (c_parser_next_token_is (parser, CPP_COMMA))
5278 struct c_expr next;
5279 c_parser_consume_token (parser);
5280 next = c_parser_expr_no_commas (parser, NULL);
5281 expr.value = build_compound_expr (expr.value, next.value);
5282 expr.original_code = COMPOUND_EXPR;
5284 return expr;
5287 /* Parse a non-empty list of expressions.
5289 nonempty-expr-list:
5290 assignment-expression
5291 nonempty-expr-list , assignment-expression
5294 static tree
5295 c_parser_expr_list (c_parser *parser)
5297 struct c_expr expr;
5298 tree ret, cur;
5299 expr = c_parser_expr_no_commas (parser, NULL);
5300 ret = cur = build_tree_list (NULL_TREE, expr.value);
5301 while (c_parser_next_token_is (parser, CPP_COMMA))
5303 c_parser_consume_token (parser);
5304 expr = c_parser_expr_no_commas (parser, NULL);
5305 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5307 return ret;
5311 /* Parse Objective-C-specific constructs. */
5313 /* Parse an objc-class-definition.
5315 objc-class-definition:
5316 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5317 objc-class-instance-variables[opt] objc-methodprotolist @end
5318 @implementation identifier objc-superclass[opt]
5319 objc-class-instance-variables[opt]
5320 @interface identifier ( identifier ) objc-protocol-refs[opt]
5321 objc-methodprotolist @end
5322 @implementation identifier ( identifier )
5324 objc-superclass:
5325 : identifier
5327 "@interface identifier (" must start "@interface identifier (
5328 identifier ) ...": objc-methodprotolist in the first production may
5329 not start with a parenthesized identifier as a declarator of a data
5330 definition with no declaration specifiers if the objc-superclass,
5331 objc-protocol-refs and objc-class-instance-variables are omitted. */
5333 static void
5334 c_parser_objc_class_definition (c_parser *parser)
5336 bool iface_p;
5337 tree id1;
5338 tree superclass;
5339 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5340 iface_p = true;
5341 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5342 iface_p = false;
5343 else
5344 gcc_unreachable ();
5345 c_parser_consume_token (parser);
5346 if (c_parser_next_token_is_not (parser, CPP_NAME))
5348 c_parser_error (parser, "expected identifier");
5349 return;
5351 id1 = c_parser_peek_token (parser)->value;
5352 c_parser_consume_token (parser);
5353 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5355 tree id2;
5356 tree proto = NULL_TREE;
5357 c_parser_consume_token (parser);
5358 if (c_parser_next_token_is_not (parser, CPP_NAME))
5360 c_parser_error (parser, "expected identifier");
5361 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5362 return;
5364 id2 = c_parser_peek_token (parser)->value;
5365 c_parser_consume_token (parser);
5366 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5367 if (!iface_p)
5369 objc_start_category_implementation (id1, id2);
5370 return;
5372 if (c_parser_next_token_is (parser, CPP_LESS))
5373 proto = c_parser_objc_protocol_refs (parser);
5374 objc_start_category_interface (id1, id2, proto);
5375 c_parser_objc_methodprotolist (parser);
5376 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5377 objc_finish_interface ();
5378 return;
5380 if (c_parser_next_token_is (parser, CPP_COLON))
5382 c_parser_consume_token (parser);
5383 if (c_parser_next_token_is_not (parser, CPP_NAME))
5385 c_parser_error (parser, "expected identifier");
5386 return;
5388 superclass = c_parser_peek_token (parser)->value;
5389 c_parser_consume_token (parser);
5391 else
5392 superclass = NULL_TREE;
5393 if (iface_p)
5395 tree proto = NULL_TREE;
5396 if (c_parser_next_token_is (parser, CPP_LESS))
5397 proto = c_parser_objc_protocol_refs (parser);
5398 objc_start_class_interface (id1, superclass, proto);
5400 else
5401 objc_start_class_implementation (id1, superclass);
5402 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5403 c_parser_objc_class_instance_variables (parser);
5404 if (iface_p)
5406 objc_continue_interface ();
5407 c_parser_objc_methodprotolist (parser);
5408 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5409 objc_finish_interface ();
5411 else
5413 objc_continue_implementation ();
5414 return;
5418 /* Parse objc-class-instance-variables.
5420 objc-class-instance-variables:
5421 { objc-instance-variable-decl-list[opt] }
5423 objc-instance-variable-decl-list:
5424 objc-visibility-spec
5425 objc-instance-variable-decl ;
5427 objc-instance-variable-decl-list objc-visibility-spec
5428 objc-instance-variable-decl-list objc-instance-variable-decl ;
5429 objc-instance-variable-decl-list ;
5431 objc-visibility-spec:
5432 @private
5433 @protected
5434 @public
5436 objc-instance-variable-decl:
5437 struct-declaration
5440 static void
5441 c_parser_objc_class_instance_variables (c_parser *parser)
5443 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5444 c_parser_consume_token (parser);
5445 while (c_parser_next_token_is_not (parser, CPP_EOF))
5447 tree decls;
5448 /* Parse any stray semicolon. */
5449 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5451 if (pedantic)
5452 pedwarn ("extra semicolon in struct or union specified");
5453 c_parser_consume_token (parser);
5454 continue;
5456 /* Stop if at the end of the instance variables. */
5457 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5459 c_parser_consume_token (parser);
5460 break;
5462 /* Parse any objc-visibility-spec. */
5463 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5465 c_parser_consume_token (parser);
5466 objc_set_visibility (2);
5467 continue;
5469 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5471 c_parser_consume_token (parser);
5472 objc_set_visibility (0);
5473 continue;
5475 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5477 c_parser_consume_token (parser);
5478 objc_set_visibility (1);
5479 continue;
5481 /* Parse some comma-separated declarations. */
5482 decls = c_parser_struct_declaration (parser);
5484 /* Comma-separated instance variables are chained together in
5485 reverse order; add them one by one. */
5486 tree ivar = nreverse (decls);
5487 for (; ivar; ivar = TREE_CHAIN (ivar))
5488 objc_add_instance_variable (copy_node (ivar));
5490 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5494 /* Parse an objc-class-declaration.
5496 objc-class-declaration:
5497 @class identifier-list ;
5500 static void
5501 c_parser_objc_class_declaration (c_parser *parser)
5503 tree list = NULL_TREE;
5504 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5505 c_parser_consume_token (parser);
5506 /* Any identifiers, including those declared as type names, are OK
5507 here. */
5508 while (true)
5510 tree id;
5511 if (c_parser_next_token_is_not (parser, CPP_NAME))
5513 c_parser_error (parser, "expected identifier");
5514 break;
5516 id = c_parser_peek_token (parser)->value;
5517 list = chainon (list, build_tree_list (NULL_TREE, id));
5518 c_parser_consume_token (parser);
5519 if (c_parser_next_token_is (parser, CPP_COMMA))
5520 c_parser_consume_token (parser);
5521 else
5522 break;
5524 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5525 objc_declare_class (list);
5528 /* Parse an objc-alias-declaration.
5530 objc-alias-declaration:
5531 @compatibility_alias identifier identifier ;
5534 static void
5535 c_parser_objc_alias_declaration (c_parser *parser)
5537 tree id1, id2;
5538 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5539 c_parser_consume_token (parser);
5540 if (c_parser_next_token_is_not (parser, CPP_NAME))
5542 c_parser_error (parser, "expected identifier");
5543 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5544 return;
5546 id1 = c_parser_peek_token (parser)->value;
5547 c_parser_consume_token (parser);
5548 if (c_parser_next_token_is_not (parser, CPP_NAME))
5550 c_parser_error (parser, "expected identifier");
5551 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5552 return;
5554 id2 = c_parser_peek_token (parser)->value;
5555 c_parser_consume_token (parser);
5556 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5557 objc_declare_alias (id1, id2);
5560 /* Parse an objc-protocol-definition.
5562 objc-protocol-definition:
5563 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5564 @protocol identifier-list ;
5566 "@protocol identifier ;" should be resolved as "@protocol
5567 identifier-list ;": objc-methodprotolist may not start with a
5568 semicolon in the first alternative if objc-protocol-refs are
5569 omitted. */
5571 static void
5572 c_parser_objc_protocol_definition (c_parser *parser)
5574 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5575 c_parser_consume_token (parser);
5576 if (c_parser_next_token_is_not (parser, CPP_NAME))
5578 c_parser_error (parser, "expected identifier");
5579 return;
5581 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5582 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5584 tree list = NULL_TREE;
5585 /* Any identifiers, including those declared as type names, are
5586 OK here. */
5587 while (true)
5589 tree id;
5590 if (c_parser_next_token_is_not (parser, CPP_NAME))
5592 c_parser_error (parser, "expected identifier");
5593 break;
5595 id = c_parser_peek_token (parser)->value;
5596 list = chainon (list, build_tree_list (NULL_TREE, id));
5597 c_parser_consume_token (parser);
5598 if (c_parser_next_token_is (parser, CPP_COMMA))
5599 c_parser_consume_token (parser);
5600 else
5601 break;
5603 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5604 objc_declare_protocols (list);
5606 else
5608 tree id = c_parser_peek_token (parser)->value;
5609 tree proto = NULL_TREE;
5610 c_parser_consume_token (parser);
5611 if (c_parser_next_token_is (parser, CPP_LESS))
5612 proto = c_parser_objc_protocol_refs (parser);
5613 objc_pq_context = 1;
5614 objc_start_protocol (id, proto);
5615 c_parser_objc_methodprotolist (parser);
5616 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5617 objc_pq_context = 0;
5618 objc_finish_interface ();
5622 /* Parse an objc-method-type.
5624 objc-method-type:
5629 static enum tree_code
5630 c_parser_objc_method_type (c_parser *parser)
5632 switch (c_parser_peek_token (parser)->type)
5634 case CPP_PLUS:
5635 c_parser_consume_token (parser);
5636 return PLUS_EXPR;
5637 case CPP_MINUS:
5638 c_parser_consume_token (parser);
5639 return MINUS_EXPR;
5640 default:
5641 gcc_unreachable ();
5645 /* Parse an objc-method-definition.
5647 objc-method-definition:
5648 objc-method-type objc-method-decl ;[opt] compound-statement
5651 static void
5652 c_parser_objc_method_definition (c_parser *parser)
5654 enum tree_code type = c_parser_objc_method_type (parser);
5655 tree decl;
5656 objc_set_method_type (type);
5657 objc_pq_context = 1;
5658 decl = c_parser_objc_method_decl (parser);
5659 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5661 c_parser_consume_token (parser);
5662 if (pedantic)
5663 pedwarn ("extra semicolon in method definition specified");
5665 objc_pq_context = 0;
5666 objc_start_method_definition (decl);
5667 add_stmt (c_parser_compound_statement (parser));
5668 objc_finish_method_definition (current_function_decl);
5671 /* Parse an objc-methodprotolist.
5673 objc-methodprotolist:
5674 empty
5675 objc-methodprotolist objc-methodproto
5676 objc-methodprotolist declaration
5677 objc-methodprotolist ;
5679 The declaration is a data definition, which may be missing
5680 declaration specifiers under the same rules and diagnostics as
5681 other data definitions outside functions, and the stray semicolon
5682 is diagnosed the same way as a stray semicolon outside a
5683 function. */
5685 static void
5686 c_parser_objc_methodprotolist (c_parser *parser)
5688 while (true)
5690 /* The list is terminated by @end. */
5691 switch (c_parser_peek_token (parser)->type)
5693 case CPP_SEMICOLON:
5694 if (pedantic)
5695 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5696 c_parser_consume_token (parser);
5697 break;
5698 case CPP_PLUS:
5699 case CPP_MINUS:
5700 c_parser_objc_methodproto (parser);
5701 break;
5702 case CPP_EOF:
5703 return;
5704 default:
5705 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5706 return;
5707 c_parser_declaration_or_fndef (parser, false, true, false, true);
5708 break;
5713 /* Parse an objc-methodproto.
5715 objc-methodproto:
5716 objc-method-type objc-method-decl ;
5719 static void
5720 c_parser_objc_methodproto (c_parser *parser)
5722 enum tree_code type = c_parser_objc_method_type (parser);
5723 tree decl;
5724 objc_set_method_type (type);
5725 /* Remember protocol qualifiers in prototypes. */
5726 objc_pq_context = 1;
5727 decl = c_parser_objc_method_decl (parser);
5728 /* Forget protocol qualifiers here. */
5729 objc_pq_context = 0;
5730 objc_add_method_declaration (decl);
5731 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5734 /* Parse an objc-method-decl.
5736 objc-method-decl:
5737 ( objc-type-name ) objc-selector
5738 objc-selector
5739 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5740 objc-keyword-selector objc-optparmlist
5742 objc-keyword-selector:
5743 objc-keyword-decl
5744 objc-keyword-selector objc-keyword-decl
5746 objc-keyword-decl:
5747 objc-selector : ( objc-type-name ) identifier
5748 objc-selector : identifier
5749 : ( objc-type-name ) identifier
5750 : identifier
5752 objc-optparmlist:
5753 objc-optparms objc-optellipsis
5755 objc-optparms:
5756 empty
5757 objc-opt-parms , parameter-declaration
5759 objc-optellipsis:
5760 empty
5761 , ...
5764 static tree
5765 c_parser_objc_method_decl (c_parser *parser)
5767 tree type = NULL_TREE;
5768 tree sel;
5769 tree parms = NULL_TREE;
5770 bool ellipsis = false;
5772 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5774 c_parser_consume_token (parser);
5775 type = c_parser_objc_type_name (parser);
5776 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5778 sel = c_parser_objc_selector (parser);
5779 /* If there is no selector, or a colon follows, we have an
5780 objc-keyword-selector. If there is a selector, and a colon does
5781 not follow, that selector ends the objc-method-decl. */
5782 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5784 tree tsel = sel;
5785 tree list = NULL_TREE;
5786 while (true)
5788 tree atype = NULL_TREE, id, keyworddecl;
5789 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5790 break;
5791 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5793 c_parser_consume_token (parser);
5794 atype = c_parser_objc_type_name (parser);
5795 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5796 "expected %<)%>");
5798 if (c_parser_next_token_is_not (parser, CPP_NAME))
5800 c_parser_error (parser, "expected identifier");
5801 return error_mark_node;
5803 id = c_parser_peek_token (parser)->value;
5804 c_parser_consume_token (parser);
5805 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5806 list = chainon (list, keyworddecl);
5807 tsel = c_parser_objc_selector (parser);
5808 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5809 break;
5811 /* Parse the optional parameter list. Optional Objective-C
5812 method parameters follow the C syntax, and may include '...'
5813 to denote a variable number of arguments. */
5814 parms = make_node (TREE_LIST);
5815 while (c_parser_next_token_is (parser, CPP_COMMA))
5817 struct c_parm *parm;
5818 c_parser_consume_token (parser);
5819 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5821 ellipsis = true;
5822 c_parser_consume_token (parser);
5823 break;
5825 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5826 if (parm == NULL)
5827 break;
5828 parms = chainon (parms,
5829 build_tree_list (NULL_TREE, grokparm (parm)));
5831 sel = list;
5833 return objc_build_method_signature (type, sel, parms, ellipsis);
5836 /* Parse an objc-type-name.
5838 objc-type-name:
5839 objc-type-qualifiers[opt] type-name
5840 objc-type-qualifiers[opt]
5842 objc-type-qualifiers:
5843 objc-type-qualifier
5844 objc-type-qualifiers objc-type-qualifier
5846 objc-type-qualifier: one of
5847 in out inout bycopy byref oneway
5850 static tree
5851 c_parser_objc_type_name (c_parser *parser)
5853 tree quals = NULL_TREE;
5854 struct c_type_name *typename = NULL;
5855 tree type = NULL_TREE;
5856 while (true)
5858 c_token *token = c_parser_peek_token (parser);
5859 if (token->type == CPP_KEYWORD
5860 && (token->keyword == RID_IN
5861 || token->keyword == RID_OUT
5862 || token->keyword == RID_INOUT
5863 || token->keyword == RID_BYCOPY
5864 || token->keyword == RID_BYREF
5865 || token->keyword == RID_ONEWAY))
5867 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
5868 c_parser_consume_token (parser);
5870 else
5871 break;
5873 if (c_parser_next_token_starts_typename (parser))
5874 typename = c_parser_type_name (parser);
5875 if (typename)
5876 type = groktypename (typename);
5877 return build_tree_list (quals, type);
5880 /* Parse objc-protocol-refs.
5882 objc-protocol-refs:
5883 < identifier-list >
5886 static tree
5887 c_parser_objc_protocol_refs (c_parser *parser)
5889 tree list = NULL_TREE;
5890 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
5891 c_parser_consume_token (parser);
5892 /* Any identifiers, including those declared as type names, are OK
5893 here. */
5894 while (true)
5896 tree id;
5897 if (c_parser_next_token_is_not (parser, CPP_NAME))
5899 c_parser_error (parser, "expected identifier");
5900 break;
5902 id = c_parser_peek_token (parser)->value;
5903 list = chainon (list, build_tree_list (NULL_TREE, id));
5904 c_parser_consume_token (parser);
5905 if (c_parser_next_token_is (parser, CPP_COMMA))
5906 c_parser_consume_token (parser);
5907 else
5908 break;
5910 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
5911 return list;
5914 /* Parse an objc-try-catch-statement.
5916 objc-try-catch-statement:
5917 @try compound-statement objc-catch-list[opt]
5918 @try compound-statement objc-catch-list[opt] @finally compound-statement
5920 objc-catch-list:
5921 @catch ( parameter-declaration ) compound-statement
5922 objc-catch-list @catch ( parameter-declaration ) compound-statement
5925 static void
5926 c_parser_objc_try_catch_statement (c_parser *parser)
5928 location_t loc;
5929 tree stmt;
5930 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
5931 c_parser_consume_token (parser);
5932 loc = c_parser_peek_token (parser)->location;
5933 stmt = c_parser_compound_statement (parser);
5934 objc_begin_try_stmt (loc, stmt);
5935 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
5937 struct c_parm *parm;
5938 c_parser_consume_token (parser);
5939 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5940 break;
5941 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5942 if (parm == NULL)
5944 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5945 break;
5947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5948 objc_begin_catch_clause (grokparm (parm));
5949 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5950 c_parser_compound_statement_nostart (parser);
5951 objc_finish_catch_clause ();
5953 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
5955 location_t finloc;
5956 tree finstmt;
5957 c_parser_consume_token (parser);
5958 finloc = c_parser_peek_token (parser)->location;
5959 finstmt = c_parser_compound_statement (parser);
5960 objc_build_finally_clause (finloc, finstmt);
5962 objc_finish_try_stmt ();
5965 /* Parse an objc-synchronized-statement.
5967 objc-synchronized-statement:
5968 @synchronized ( expression ) compound-statement
5971 static void
5972 c_parser_objc_synchronized_statement (c_parser *parser)
5974 location_t loc;
5975 tree expr, stmt;
5976 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
5977 c_parser_consume_token (parser);
5978 loc = c_parser_peek_token (parser)->location;
5979 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5981 expr = c_parser_expression (parser).value;
5982 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5984 else
5985 expr = error_mark_node;
5986 stmt = c_parser_compound_statement (parser);
5987 objc_build_synchronized (loc, expr, stmt);
5990 /* Parse an objc-selector; return NULL_TREE without an error if the
5991 next token is not an objc-selector.
5993 objc-selector:
5994 identifier
5995 one of
5996 enum struct union if else while do for switch case default
5997 break continue return goto asm sizeof typeof __alignof
5998 unsigned long const short volatile signed restrict _Complex
5999 in out inout bycopy byref oneway int char float double void _Bool
6001 ??? Why this selection of keywords but not, for example, storage
6002 class specifiers? */
6004 static tree
6005 c_parser_objc_selector (c_parser *parser)
6007 c_token *token = c_parser_peek_token (parser);
6008 tree value = token->value;
6009 if (token->type == CPP_NAME)
6011 c_parser_consume_token (parser);
6012 return value;
6014 if (token->type != CPP_KEYWORD)
6015 return NULL_TREE;
6016 switch (token->keyword)
6018 case RID_ENUM:
6019 case RID_STRUCT:
6020 case RID_UNION:
6021 case RID_IF:
6022 case RID_ELSE:
6023 case RID_WHILE:
6024 case RID_DO:
6025 case RID_FOR:
6026 case RID_SWITCH:
6027 case RID_CASE:
6028 case RID_DEFAULT:
6029 case RID_BREAK:
6030 case RID_CONTINUE:
6031 case RID_RETURN:
6032 case RID_GOTO:
6033 case RID_ASM:
6034 case RID_SIZEOF:
6035 case RID_TYPEOF:
6036 case RID_ALIGNOF:
6037 case RID_UNSIGNED:
6038 case RID_LONG:
6039 case RID_CONST:
6040 case RID_SHORT:
6041 case RID_VOLATILE:
6042 case RID_SIGNED:
6043 case RID_RESTRICT:
6044 case RID_COMPLEX:
6045 case RID_IN:
6046 case RID_OUT:
6047 case RID_INOUT:
6048 case RID_BYCOPY:
6049 case RID_BYREF:
6050 case RID_ONEWAY:
6051 case RID_INT:
6052 case RID_CHAR:
6053 case RID_FLOAT:
6054 case RID_DOUBLE:
6055 case RID_VOID:
6056 case RID_BOOL:
6057 c_parser_consume_token (parser);
6058 return value;
6059 default:
6060 return NULL_TREE;
6064 /* Parse an objc-selector-arg.
6066 objc-selector-arg:
6067 objc-selector
6068 objc-keywordname-list
6070 objc-keywordname-list:
6071 objc-keywordname
6072 objc-keywordname-list objc-keywordname
6074 objc-keywordname:
6075 objc-selector :
6079 static tree
6080 c_parser_objc_selector_arg (c_parser *parser)
6082 tree sel = c_parser_objc_selector (parser);
6083 tree list = NULL_TREE;
6084 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6085 return sel;
6086 while (true)
6088 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6089 return list;
6090 list = chainon (list, build_tree_list (sel, NULL_TREE));
6091 sel = c_parser_objc_selector (parser);
6092 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6093 break;
6095 return list;
6098 /* Parse an objc-receiver.
6100 objc-receiver:
6101 expression
6102 class-name
6103 type-name
6106 static tree
6107 c_parser_objc_receiver (c_parser *parser)
6109 if (c_parser_peek_token (parser)->type == CPP_NAME
6110 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6111 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6113 tree id = c_parser_peek_token (parser)->value;
6114 c_parser_consume_token (parser);
6115 return objc_get_class_reference (id);
6117 return c_parser_expression (parser).value;
6120 /* Parse objc-message-args.
6122 objc-message-args:
6123 objc-selector
6124 objc-keywordarg-list
6126 objc-keywordarg-list:
6127 objc-keywordarg
6128 objc-keywordarg-list objc-keywordarg
6130 objc-keywordarg:
6131 objc-selector : objc-keywordexpr
6132 : objc-keywordexpr
6135 static tree
6136 c_parser_objc_message_args (c_parser *parser)
6138 tree sel = c_parser_objc_selector (parser);
6139 tree list = NULL_TREE;
6140 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6141 return sel;
6142 while (true)
6144 tree keywordexpr;
6145 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6146 return list;
6147 keywordexpr = c_parser_objc_keywordexpr (parser);
6148 list = chainon (list, build_tree_list (sel, keywordexpr));
6149 sel = c_parser_objc_selector (parser);
6150 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6151 break;
6153 return list;
6156 /* Parse an objc-keywordexpr.
6158 objc-keywordexpr:
6159 nonempty-expr-list
6162 static tree
6163 c_parser_objc_keywordexpr (c_parser *parser)
6165 tree list = c_parser_expr_list (parser);
6166 if (TREE_CHAIN (list) == NULL_TREE)
6168 /* Just return the expression, remove a level of
6169 indirection. */
6170 return TREE_VALUE (list);
6172 else
6174 /* We have a comma expression, we will collapse later. */
6175 return list;
6180 /* The actual parser and external interface. ??? Does this need to be
6181 garbage-collected? */
6183 static GTY (()) c_parser *the_parser;
6185 /* Parse a single source file. */
6187 void
6188 c_parse_file (void)
6190 the_parser = c_parser_new ();
6191 c_parser_translation_unit (the_parser);
6192 the_parser = NULL;
6195 #include "gt-c-parser.h"