PR middle-end/25568
[official-gcc.git] / gcc / c-parser.c
blob8469ecad2295fb1c7f4c0c28dcee5789133a1d29
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, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, 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 { "_Decimal32", RID_DFLOAT32, D_EXT },
97 { "_Decimal64", RID_DFLOAT64, D_EXT },
98 { "_Decimal128", RID_DFLOAT128, D_EXT },
99 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
100 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
101 { "__alignof", RID_ALIGNOF, 0 },
102 { "__alignof__", RID_ALIGNOF, 0 },
103 { "__asm", RID_ASM, 0 },
104 { "__asm__", RID_ASM, 0 },
105 { "__attribute", RID_ATTRIBUTE, 0 },
106 { "__attribute__", RID_ATTRIBUTE, 0 },
107 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
108 { "__builtin_offsetof", RID_OFFSETOF, 0 },
109 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
110 { "__builtin_va_arg", RID_VA_ARG, 0 },
111 { "__complex", RID_COMPLEX, 0 },
112 { "__complex__", RID_COMPLEX, 0 },
113 { "__const", RID_CONST, 0 },
114 { "__const__", RID_CONST, 0 },
115 { "__extension__", RID_EXTENSION, 0 },
116 { "__func__", RID_C99_FUNCTION_NAME, 0 },
117 { "__imag", RID_IMAGPART, 0 },
118 { "__imag__", RID_IMAGPART, 0 },
119 { "__inline", RID_INLINE, 0 },
120 { "__inline__", RID_INLINE, 0 },
121 { "__label__", RID_LABEL, 0 },
122 { "__real", RID_REALPART, 0 },
123 { "__real__", RID_REALPART, 0 },
124 { "__restrict", RID_RESTRICT, 0 },
125 { "__restrict__", RID_RESTRICT, 0 },
126 { "__signed", RID_SIGNED, 0 },
127 { "__signed__", RID_SIGNED, 0 },
128 { "__thread", RID_THREAD, 0 },
129 { "__typeof", RID_TYPEOF, 0 },
130 { "__typeof__", RID_TYPEOF, 0 },
131 { "__volatile", RID_VOLATILE, 0 },
132 { "__volatile__", RID_VOLATILE, 0 },
133 { "asm", RID_ASM, D_EXT },
134 { "auto", RID_AUTO, 0 },
135 { "break", RID_BREAK, 0 },
136 { "case", RID_CASE, 0 },
137 { "char", RID_CHAR, 0 },
138 { "const", RID_CONST, 0 },
139 { "continue", RID_CONTINUE, 0 },
140 { "default", RID_DEFAULT, 0 },
141 { "do", RID_DO, 0 },
142 { "double", RID_DOUBLE, 0 },
143 { "else", RID_ELSE, 0 },
144 { "enum", RID_ENUM, 0 },
145 { "extern", RID_EXTERN, 0 },
146 { "float", RID_FLOAT, 0 },
147 { "for", RID_FOR, 0 },
148 { "goto", RID_GOTO, 0 },
149 { "if", RID_IF, 0 },
150 { "inline", RID_INLINE, D_EXT89 },
151 { "int", RID_INT, 0 },
152 { "long", RID_LONG, 0 },
153 { "register", RID_REGISTER, 0 },
154 { "restrict", RID_RESTRICT, D_C89 },
155 { "return", RID_RETURN, 0 },
156 { "short", RID_SHORT, 0 },
157 { "signed", RID_SIGNED, 0 },
158 { "sizeof", RID_SIZEOF, 0 },
159 { "static", RID_STATIC, 0 },
160 { "struct", RID_STRUCT, 0 },
161 { "switch", RID_SWITCH, 0 },
162 { "typedef", RID_TYPEDEF, 0 },
163 { "typeof", RID_TYPEOF, D_EXT },
164 { "union", RID_UNION, 0 },
165 { "unsigned", RID_UNSIGNED, 0 },
166 { "void", RID_VOID, 0 },
167 { "volatile", RID_VOLATILE, 0 },
168 { "while", RID_WHILE, 0 },
169 /* These Objective-C keywords are recognized only immediately after
170 an '@'. */
171 { "class", RID_AT_CLASS, D_OBJC },
172 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
173 { "defs", RID_AT_DEFS, D_OBJC },
174 { "encode", RID_AT_ENCODE, D_OBJC },
175 { "end", RID_AT_END, D_OBJC },
176 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
177 { "interface", RID_AT_INTERFACE, D_OBJC },
178 { "private", RID_AT_PRIVATE, D_OBJC },
179 { "protected", RID_AT_PROTECTED, D_OBJC },
180 { "protocol", RID_AT_PROTOCOL, D_OBJC },
181 { "public", RID_AT_PUBLIC, D_OBJC },
182 { "selector", RID_AT_SELECTOR, D_OBJC },
183 { "throw", RID_AT_THROW, D_OBJC },
184 { "try", RID_AT_TRY, D_OBJC },
185 { "catch", RID_AT_CATCH, D_OBJC },
186 { "finally", RID_AT_FINALLY, D_OBJC },
187 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
188 /* These are recognized only in protocol-qualifier context
189 (see above) */
190 { "bycopy", RID_BYCOPY, D_OBJC },
191 { "byref", RID_BYREF, D_OBJC },
192 { "in", RID_IN, D_OBJC },
193 { "inout", RID_INOUT, D_OBJC },
194 { "oneway", RID_ONEWAY, D_OBJC },
195 { "out", RID_OUT, D_OBJC },
197 #define N_reswords (sizeof reswords / sizeof (struct resword))
199 /* Initialization routine for this file. */
201 void
202 c_parse_init (void)
204 /* The only initialization required is of the reserved word
205 identifiers. */
206 unsigned int i;
207 tree id;
208 int mask = (flag_isoc99 ? 0 : D_C89)
209 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
211 if (!c_dialect_objc ())
212 mask |= D_OBJC;
214 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
215 for (i = 0; i < N_reswords; i++)
217 /* If a keyword is disabled, do not enter it into the table
218 and so create a canonical spelling that isn't a keyword. */
219 if (reswords[i].disable & mask)
220 continue;
222 id = get_identifier (reswords[i].word);
223 C_RID_CODE (id) = reswords[i].rid;
224 C_IS_RESERVED_WORD (id) = 1;
225 ridpointers [(int) reswords[i].rid] = id;
229 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
230 and the C parser. Unlike the C++ lexer, the parser structure
231 stores the lexer information instead of using a separate structure.
232 Identifiers are separated into ordinary identifiers, type names,
233 keywords and some other Objective-C types of identifiers, and some
234 look-ahead is maintained.
236 ??? It might be a good idea to lex the whole file up front (as for
237 C++). It would then be possible to share more of the C and C++
238 lexer code, if desired. */
240 /* The following local token type is used. */
242 /* A keyword. */
243 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
245 /* More information about the type of a CPP_NAME token. */
246 typedef enum c_id_kind {
247 /* An ordinary identifier. */
248 C_ID_ID,
249 /* An identifier declared as a typedef name. */
250 C_ID_TYPENAME,
251 /* An identifier declared as an Objective-C class name. */
252 C_ID_CLASSNAME,
253 /* Not an identifier. */
254 C_ID_NONE
255 } c_id_kind;
257 /* A single C token after string literal concatenation and conversion
258 of preprocessing tokens to tokens. */
259 typedef struct c_token GTY (())
261 /* The kind of token. */
262 ENUM_BITFIELD (cpp_ttype) type : 8;
263 /* If this token is a CPP_NAME, this value indicates whether also
264 declared as some kind of type. Otherwise, it is C_ID_NONE. */
265 ENUM_BITFIELD (c_id_kind) id_kind : 8;
266 /* If this token is a keyword, this value indicates which keyword.
267 Otherwise, this value is RID_MAX. */
268 ENUM_BITFIELD (rid) keyword : 8;
269 /* True if this token is from a system header. */
270 BOOL_BITFIELD in_system_header : 1;
271 /* The value associated with this token, if any. */
272 tree value;
273 /* The location at which this token was found. */
274 location_t location;
275 } c_token;
277 /* A parser structure recording information about the state and
278 context of parsing. Includes lexer information with up to two
279 tokens of look-ahead; more are not needed for C. */
280 typedef struct c_parser GTY(())
282 /* The look-ahead tokens. */
283 c_token tokens[2];
284 /* How many look-ahead tokens are available (0, 1 or 2). */
285 short tokens_avail;
286 /* True if a syntax error is being recovered from; false otherwise.
287 c_parser_error sets this flag. It should clear this flag when
288 enough tokens have been consumed to recover from the error. */
289 BOOL_BITFIELD error : 1;
290 } c_parser;
292 /* Read in and lex a single token, storing it in *TOKEN. */
294 static void
295 c_lex_one_token (c_token *token)
297 timevar_push (TV_LEX);
298 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
299 token->in_system_header = in_system_header;
300 switch (token->type)
302 case CPP_NAME:
303 token->id_kind = C_ID_NONE;
304 token->keyword = RID_MAX;
306 tree decl;
308 int objc_force_identifier = objc_need_raw_identifier;
309 OBJC_NEED_RAW_IDENTIFIER (0);
311 if (C_IS_RESERVED_WORD (token->value))
313 enum rid rid_code = C_RID_CODE (token->value);
315 if (c_dialect_objc ())
317 if (!OBJC_IS_AT_KEYWORD (rid_code)
318 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
320 /* Return the canonical spelling for this keyword. */
321 token->value = ridpointers[(int) rid_code];
322 token->type = CPP_KEYWORD;
323 token->keyword = rid_code;
324 break;
327 else
329 /* Return the canonical spelling for this keyword. */
330 token->value = ridpointers[(int) rid_code];
331 token->type = CPP_KEYWORD;
332 token->keyword = rid_code;
333 break;
337 decl = lookup_name (token->value);
338 if (decl)
340 if (TREE_CODE (decl) == TYPE_DECL)
342 token->id_kind = C_ID_TYPENAME;
343 break;
346 else if (c_dialect_objc ())
348 tree objc_interface_decl = objc_is_class_name (token->value);
349 /* Objective-C class names are in the same namespace as
350 variables and typedefs, and hence are shadowed by local
351 declarations. */
352 if (objc_interface_decl
353 && (global_bindings_p ()
354 || (!objc_force_identifier && !decl)))
356 token->value = objc_interface_decl;
357 token->id_kind = C_ID_CLASSNAME;
358 break;
362 token->id_kind = C_ID_ID;
363 break;
364 case CPP_AT_NAME:
365 /* This only happens in Objective-C; it must be a keyword. */
366 token->type = CPP_KEYWORD;
367 token->id_kind = C_ID_NONE;
368 token->keyword = C_RID_CODE (token->value);
369 break;
370 case CPP_COLON:
371 case CPP_COMMA:
372 case CPP_CLOSE_PAREN:
373 case CPP_SEMICOLON:
374 /* These tokens may affect the interpretation of any identifiers
375 following, if doing Objective-C. */
376 OBJC_NEED_RAW_IDENTIFIER (0);
377 token->id_kind = C_ID_NONE;
378 token->keyword = RID_MAX;
379 break;
380 default:
381 token->id_kind = C_ID_NONE;
382 token->keyword = RID_MAX;
383 break;
385 timevar_pop (TV_LEX);
388 /* Return a pointer to the next token from PARSER, reading it in if
389 necessary. */
391 static inline c_token *
392 c_parser_peek_token (c_parser *parser)
394 if (parser->tokens_avail == 0)
396 c_lex_one_token (&parser->tokens[0]);
397 parser->tokens_avail = 1;
399 return &parser->tokens[0];
402 /* Return true if the next token from PARSER has the indicated
403 TYPE. */
405 static inline bool
406 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
408 return c_parser_peek_token (parser)->type == type;
411 /* Return true if the next token from PARSER does not have the
412 indicated TYPE. */
414 static inline bool
415 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
417 return !c_parser_next_token_is (parser, type);
420 /* Return true if the next token from PARSER is the indicated
421 KEYWORD. */
423 static inline bool
424 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
426 c_token *token;
428 /* Peek at the next token. */
429 token = c_parser_peek_token (parser);
430 /* Check to see if it is the indicated keyword. */
431 return token->keyword == keyword;
434 /* Return true if TOKEN can start a type name,
435 false otherwise. */
436 static bool
437 c_token_starts_typename (c_token *token)
439 switch (token->type)
441 case CPP_NAME:
442 switch (token->id_kind)
444 case C_ID_ID:
445 return false;
446 case C_ID_TYPENAME:
447 return true;
448 case C_ID_CLASSNAME:
449 gcc_assert (c_dialect_objc ());
450 return true;
451 default:
452 gcc_unreachable ();
454 case CPP_KEYWORD:
455 switch (token->keyword)
457 case RID_UNSIGNED:
458 case RID_LONG:
459 case RID_SHORT:
460 case RID_SIGNED:
461 case RID_COMPLEX:
462 case RID_INT:
463 case RID_CHAR:
464 case RID_FLOAT:
465 case RID_DOUBLE:
466 case RID_VOID:
467 case RID_DFLOAT32:
468 case RID_DFLOAT64:
469 case RID_DFLOAT128:
470 case RID_BOOL:
471 case RID_ENUM:
472 case RID_STRUCT:
473 case RID_UNION:
474 case RID_TYPEOF:
475 case RID_CONST:
476 case RID_VOLATILE:
477 case RID_RESTRICT:
478 case RID_ATTRIBUTE:
479 return true;
480 default:
481 return false;
483 case CPP_LESS:
484 if (c_dialect_objc ())
485 return true;
486 return false;
487 default:
488 return false;
492 /* Return true if the next token from PARSER can start a type name,
493 false otherwise. */
494 static inline bool
495 c_parser_next_token_starts_typename (c_parser *parser)
497 c_token *token = c_parser_peek_token (parser);
498 return c_token_starts_typename (token);
501 /* Return true if TOKEN can start declaration specifiers, false
502 otherwise. */
503 static bool
504 c_token_starts_declspecs (c_token *token)
506 switch (token->type)
508 case CPP_NAME:
509 switch (token->id_kind)
511 case C_ID_ID:
512 return false;
513 case C_ID_TYPENAME:
514 return true;
515 case C_ID_CLASSNAME:
516 gcc_assert (c_dialect_objc ());
517 return true;
518 default:
519 gcc_unreachable ();
521 case CPP_KEYWORD:
522 switch (token->keyword)
524 case RID_STATIC:
525 case RID_EXTERN:
526 case RID_REGISTER:
527 case RID_TYPEDEF:
528 case RID_INLINE:
529 case RID_AUTO:
530 case RID_THREAD:
531 case RID_UNSIGNED:
532 case RID_LONG:
533 case RID_SHORT:
534 case RID_SIGNED:
535 case RID_COMPLEX:
536 case RID_INT:
537 case RID_CHAR:
538 case RID_FLOAT:
539 case RID_DOUBLE:
540 case RID_VOID:
541 case RID_DFLOAT32:
542 case RID_DFLOAT64:
543 case RID_DFLOAT128:
544 case RID_BOOL:
545 case RID_ENUM:
546 case RID_STRUCT:
547 case RID_UNION:
548 case RID_TYPEOF:
549 case RID_CONST:
550 case RID_VOLATILE:
551 case RID_RESTRICT:
552 case RID_ATTRIBUTE:
553 return true;
554 default:
555 return false;
557 case CPP_LESS:
558 if (c_dialect_objc ())
559 return true;
560 return false;
561 default:
562 return false;
566 /* Return true if the next token from PARSER can start declaration
567 specifiers, false otherwise. */
568 static inline bool
569 c_parser_next_token_starts_declspecs (c_parser *parser)
571 c_token *token = c_parser_peek_token (parser);
572 return c_token_starts_declspecs (token);
575 /* Return a pointer to the next-but-one token from PARSER, reading it
576 in if necessary. The next token is already read in. */
578 static c_token *
579 c_parser_peek_2nd_token (c_parser *parser)
581 if (parser->tokens_avail >= 2)
582 return &parser->tokens[1];
583 gcc_assert (parser->tokens_avail == 1);
584 gcc_assert (parser->tokens[0].type != CPP_EOF);
585 c_lex_one_token (&parser->tokens[1]);
586 parser->tokens_avail = 2;
587 return &parser->tokens[1];
590 /* Consume the next token from PARSER. */
592 static void
593 c_parser_consume_token (c_parser *parser)
595 if (parser->tokens_avail == 2)
596 parser->tokens[0] = parser->tokens[1];
597 else
599 gcc_assert (parser->tokens_avail == 1);
600 gcc_assert (parser->tokens[0].type != CPP_EOF);
602 parser->tokens_avail--;
605 /* Update the globals input_location and in_system_header from
606 TOKEN. */
607 static inline void
608 c_parser_set_source_position_from_token (c_token *token)
610 if (token->type != CPP_EOF)
612 input_location = token->location;
613 in_system_header = token->in_system_header;
617 /* Allocate a new parser. */
619 static c_parser *
620 c_parser_new (void)
622 /* Use local storage to lex the first token because loading a PCH
623 file may cause garbage collection. */
624 c_parser tparser;
625 c_parser *ret;
626 memset (&tparser, 0, sizeof tparser);
627 c_lex_one_token (&tparser.tokens[0]);
628 tparser.tokens_avail = 1;
629 ret = GGC_NEW (c_parser);
630 memcpy (ret, &tparser, sizeof tparser);
631 return ret;
634 /* Issue a diagnostic of the form
635 FILE:LINE: MESSAGE before TOKEN
636 where TOKEN is the next token in the input stream of PARSER.
637 MESSAGE (specified by the caller) is usually of the form "expected
638 OTHER-TOKEN".
640 Do not issue a diagnostic if still recovering from an error.
642 ??? This is taken from the C++ parser, but building up messages in
643 this way is not i18n-friendly and some other approach should be
644 used. */
646 static void
647 c_parser_error (c_parser *parser, const char *gmsgid)
649 c_token *token = c_parser_peek_token (parser);
650 if (parser->error)
651 return;
652 parser->error = true;
653 if (!gmsgid)
654 return;
655 /* This diagnostic makes more sense if it is tagged to the line of
656 the token we just peeked at. */
657 c_parser_set_source_position_from_token (token);
658 c_parse_error (gmsgid,
659 /* Because c_parse_error does not understand
660 CPP_KEYWORD, keywords are treated like
661 identifiers. */
662 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
663 token->value);
666 /* If the next token is of the indicated TYPE, consume it. Otherwise,
667 issue the error MSGID. If MSGID is NULL then a message has already
668 been produced and no message will be produced this time. Returns
669 true if found, false otherwise. */
671 static bool
672 c_parser_require (c_parser *parser,
673 enum cpp_ttype type,
674 const char *msgid)
676 if (c_parser_next_token_is (parser, type))
678 c_parser_consume_token (parser);
679 return true;
681 else
683 c_parser_error (parser, msgid);
684 return false;
688 /* If the next token is the indicated keyword, consume it. Otherwise,
689 issue the error MSGID. Returns true if found, false otherwise. */
691 static bool
692 c_parser_require_keyword (c_parser *parser,
693 enum rid keyword,
694 const char *msgid)
696 if (c_parser_next_token_is_keyword (parser, keyword))
698 c_parser_consume_token (parser);
699 return true;
701 else
703 c_parser_error (parser, msgid);
704 return false;
708 /* Like c_parser_require, except that tokens will be skipped until the
709 desired token is found. An error message is still produced if the
710 next token is not as expected. If MSGID is NULL then a message has
711 already been produced and no message will be produced this
712 time. */
714 static void
715 c_parser_skip_until_found (c_parser *parser,
716 enum cpp_ttype type,
717 const char *msgid)
719 unsigned nesting_depth = 0;
721 if (c_parser_require (parser, type, msgid))
722 return;
724 /* Skip tokens until the desired token is found. */
725 while (true)
727 /* Peek at the next token. */
728 c_token *token = c_parser_peek_token (parser);
729 /* If we've reached the token we want, consume it and stop. */
730 if (token->type == type && !nesting_depth)
732 c_parser_consume_token (parser);
733 break;
735 /* If we've run out of tokens, stop. */
736 if (token->type == CPP_EOF)
737 return;
738 if (token->type == CPP_OPEN_BRACE
739 || token->type == CPP_OPEN_PAREN
740 || token->type == CPP_OPEN_SQUARE)
741 ++nesting_depth;
742 else if (token->type == CPP_CLOSE_BRACE
743 || token->type == CPP_CLOSE_PAREN
744 || token->type == CPP_CLOSE_SQUARE)
746 if (nesting_depth-- == 0)
747 break;
749 /* Consume this token. */
750 c_parser_consume_token (parser);
752 parser->error = false;
755 /* Skip tokens until the end of a parameter is found, but do not
756 consume the comma, semicolon or closing delimiter. */
758 static void
759 c_parser_skip_to_end_of_parameter (c_parser *parser)
761 unsigned nesting_depth = 0;
763 while (true)
765 c_token *token = c_parser_peek_token (parser);
766 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
767 && !nesting_depth)
768 break;
769 /* If we've run out of tokens, stop. */
770 if (token->type == CPP_EOF)
771 return;
772 if (token->type == CPP_OPEN_BRACE
773 || token->type == CPP_OPEN_PAREN
774 || token->type == CPP_OPEN_SQUARE)
775 ++nesting_depth;
776 else if (token->type == CPP_CLOSE_BRACE
777 || token->type == CPP_CLOSE_PAREN
778 || token->type == CPP_CLOSE_SQUARE)
780 if (nesting_depth-- == 0)
781 break;
783 /* Consume this token. */
784 c_parser_consume_token (parser);
786 parser->error = false;
789 /* Skip tokens until we have consumed an entire block, or until we
790 have consumed a non-nested ';'. */
792 static void
793 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
795 unsigned nesting_depth = 0;
797 while (true)
799 c_token *token;
801 /* Peek at the next token. */
802 token = c_parser_peek_token (parser);
803 /* If we've run out of tokens, stop. */
804 if (token->type == CPP_EOF)
805 return;
806 /* If the next token is a ';', we have reached the end of the
807 statement. */
808 if (token->type == CPP_SEMICOLON && !nesting_depth)
810 /* Consume the ';'. */
811 c_parser_consume_token (parser);
812 break;
814 /* If the next token is a non-nested '}', then we have reached
815 the end of the current block. */
816 if (token->type == CPP_CLOSE_BRACE
817 && (nesting_depth == 0 || --nesting_depth == 0))
819 c_parser_consume_token (parser);
820 break;
822 /* If it the next token is a '{', then we are entering a new
823 block. Consume the entire block. */
824 if (token->type == CPP_OPEN_BRACE)
825 ++nesting_depth;
826 c_parser_consume_token (parser);
828 parser->error = false;
832 /* Save the warning flags which are controlled by __extension__. */
834 static inline int
835 disable_extension_diagnostics (void)
837 int ret = (pedantic
838 | (warn_pointer_arith << 1)
839 | (warn_traditional << 2)
840 | (flag_iso << 3));
841 pedantic = 0;
842 warn_pointer_arith = 0;
843 warn_traditional = 0;
844 flag_iso = 0;
845 return ret;
848 /* Restore the warning flags which are controlled by __extension__.
849 FLAGS is the return value from disable_extension_diagnostics. */
851 static inline void
852 restore_extension_diagnostics (int flags)
854 pedantic = flags & 1;
855 warn_pointer_arith = (flags >> 1) & 1;
856 warn_traditional = (flags >> 2) & 1;
857 flag_iso = (flags >> 3) & 1;
860 /* Possibly kinds of declarator to parse. */
861 typedef enum c_dtr_syn {
862 /* A normal declarator with an identifier. */
863 C_DTR_NORMAL,
864 /* An abstract declarator (maybe empty). */
865 C_DTR_ABSTRACT,
866 /* A parameter declarator: may be either, but after a type name does
867 not redeclare a typedef name as an identifier if it can
868 alternatively be interpreted as a typedef name; see DR#009,
869 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
870 following DR#249. For example, given a typedef T, "int T" and
871 "int *T" are valid parameter declarations redeclaring T, while
872 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
873 abstract declarators rather than involving redundant parentheses;
874 the same applies with attributes inside the parentheses before
875 "T". */
876 C_DTR_PARM
877 } c_dtr_syn;
879 static void c_parser_external_declaration (c_parser *);
880 static void c_parser_asm_definition (c_parser *);
881 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
882 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
883 bool);
884 static struct c_typespec c_parser_enum_specifier (c_parser *);
885 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
886 static tree c_parser_struct_declaration (c_parser *);
887 static struct c_typespec c_parser_typeof_specifier (c_parser *);
888 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
889 bool *);
890 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
891 c_dtr_syn, bool *);
892 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
893 bool,
894 struct c_declarator *);
895 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
896 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
897 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
898 static tree c_parser_simple_asm_expr (c_parser *);
899 static tree c_parser_attributes (c_parser *);
900 static struct c_type_name *c_parser_type_name (c_parser *);
901 static struct c_expr c_parser_initializer (c_parser *);
902 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
903 static void c_parser_initelt (c_parser *);
904 static void c_parser_initval (c_parser *, struct c_expr *);
905 static tree c_parser_compound_statement (c_parser *);
906 static void c_parser_compound_statement_nostart (c_parser *);
907 static void c_parser_label (c_parser *);
908 static void c_parser_statement (c_parser *);
909 static void c_parser_statement_after_labels (c_parser *);
910 static void c_parser_if_statement (c_parser *);
911 static void c_parser_switch_statement (c_parser *);
912 static void c_parser_while_statement (c_parser *);
913 static void c_parser_do_statement (c_parser *);
914 static void c_parser_for_statement (c_parser *);
915 static tree c_parser_asm_statement (c_parser *);
916 static tree c_parser_asm_operands (c_parser *, bool);
917 static tree c_parser_asm_clobbers (c_parser *);
918 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
919 static struct c_expr c_parser_conditional_expression (c_parser *,
920 struct c_expr *);
921 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
922 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
923 static struct c_expr c_parser_unary_expression (c_parser *);
924 static struct c_expr c_parser_sizeof_expression (c_parser *);
925 static struct c_expr c_parser_alignof_expression (c_parser *);
926 static struct c_expr c_parser_postfix_expression (c_parser *);
927 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
928 struct c_type_name *);
929 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
930 struct c_expr);
931 static struct c_expr c_parser_expression (c_parser *);
932 static struct c_expr c_parser_expression_conv (c_parser *);
933 static tree c_parser_expr_list (c_parser *, bool);
935 /* These Objective-C parser functions are only ever called when
936 compiling Objective-C. */
937 static void c_parser_objc_class_definition (c_parser *);
938 static void c_parser_objc_class_instance_variables (c_parser *);
939 static void c_parser_objc_class_declaration (c_parser *);
940 static void c_parser_objc_alias_declaration (c_parser *);
941 static void c_parser_objc_protocol_definition (c_parser *);
942 static enum tree_code c_parser_objc_method_type (c_parser *);
943 static void c_parser_objc_method_definition (c_parser *);
944 static void c_parser_objc_methodprotolist (c_parser *);
945 static void c_parser_objc_methodproto (c_parser *);
946 static tree c_parser_objc_method_decl (c_parser *);
947 static tree c_parser_objc_type_name (c_parser *);
948 static tree c_parser_objc_protocol_refs (c_parser *);
949 static void c_parser_objc_try_catch_statement (c_parser *);
950 static void c_parser_objc_synchronized_statement (c_parser *);
951 static tree c_parser_objc_selector (c_parser *);
952 static tree c_parser_objc_selector_arg (c_parser *);
953 static tree c_parser_objc_receiver (c_parser *);
954 static tree c_parser_objc_message_args (c_parser *);
955 static tree c_parser_objc_keywordexpr (c_parser *);
957 /* Parse a translation unit (C90 6.7, C99 6.9).
959 translation-unit:
960 external-declarations
962 external-declarations:
963 external-declaration
964 external-declarations external-declaration
966 GNU extensions:
968 translation-unit:
969 empty
972 static void
973 c_parser_translation_unit (c_parser *parser)
975 if (c_parser_next_token_is (parser, CPP_EOF))
977 if (pedantic)
978 pedwarn ("ISO C forbids an empty source file");
980 else
982 void *obstack_position = obstack_alloc (&parser_obstack, 0);
985 ggc_collect ();
986 c_parser_external_declaration (parser);
987 obstack_free (&parser_obstack, obstack_position);
989 while (c_parser_next_token_is_not (parser, CPP_EOF));
993 /* Parse an external declaration (C90 6.7, C99 6.9).
995 external-declaration:
996 function-definition
997 declaration
999 GNU extensions:
1001 external-declaration:
1002 asm-definition
1004 __extension__ external-declaration
1006 Objective-C:
1008 external-declaration:
1009 objc-class-definition
1010 objc-class-declaration
1011 objc-alias-declaration
1012 objc-protocol-definition
1013 objc-method-definition
1014 @end
1017 static void
1018 c_parser_external_declaration (c_parser *parser)
1020 int ext;
1021 switch (c_parser_peek_token (parser)->type)
1023 case CPP_KEYWORD:
1024 switch (c_parser_peek_token (parser)->keyword)
1026 case RID_EXTENSION:
1027 ext = disable_extension_diagnostics ();
1028 c_parser_consume_token (parser);
1029 c_parser_external_declaration (parser);
1030 restore_extension_diagnostics (ext);
1031 break;
1032 case RID_ASM:
1033 c_parser_asm_definition (parser);
1034 break;
1035 case RID_AT_INTERFACE:
1036 case RID_AT_IMPLEMENTATION:
1037 gcc_assert (c_dialect_objc ());
1038 c_parser_objc_class_definition (parser);
1039 break;
1040 case RID_AT_CLASS:
1041 gcc_assert (c_dialect_objc ());
1042 c_parser_objc_class_declaration (parser);
1043 break;
1044 case RID_AT_ALIAS:
1045 gcc_assert (c_dialect_objc ());
1046 c_parser_objc_alias_declaration (parser);
1047 break;
1048 case RID_AT_PROTOCOL:
1049 gcc_assert (c_dialect_objc ());
1050 c_parser_objc_protocol_definition (parser);
1051 break;
1052 case RID_AT_END:
1053 gcc_assert (c_dialect_objc ());
1054 c_parser_consume_token (parser);
1055 objc_finish_implementation ();
1056 break;
1057 default:
1058 goto decl_or_fndef;
1060 break;
1061 case CPP_SEMICOLON:
1062 if (pedantic)
1063 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1064 c_parser_consume_token (parser);
1065 break;
1066 case CPP_PLUS:
1067 case CPP_MINUS:
1068 if (c_dialect_objc ())
1070 c_parser_objc_method_definition (parser);
1071 break;
1073 /* Else fall through, and yield a syntax error trying to parse
1074 as a declaration or function definition. */
1075 default:
1076 decl_or_fndef:
1077 /* A declaration or a function definition. We can only tell
1078 which after parsing the declaration specifiers, if any, and
1079 the first declarator. */
1080 c_parser_declaration_or_fndef (parser, true, true, false, true);
1081 break;
1085 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1086 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1087 accepted; otherwise (old-style parameter declarations) only other
1088 declarations are accepted. If NESTED is true, we are inside a
1089 function or parsing old-style parameter declarations; any functions
1090 encountered are nested functions and declaration specifiers are
1091 required; otherwise we are at top level and functions are normal
1092 functions and declaration specifiers may be optional. If EMPTY_OK
1093 is true, empty declarations are OK (subject to all other
1094 constraints); otherwise (old-style parameter declarations) they are
1095 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1096 may start with attributes; otherwise they may not.
1098 declaration:
1099 declaration-specifiers init-declarator-list[opt] ;
1101 function-definition:
1102 declaration-specifiers[opt] declarator declaration-list[opt]
1103 compound-statement
1105 declaration-list:
1106 declaration
1107 declaration-list declaration
1109 init-declarator-list:
1110 init-declarator
1111 init-declarator-list , init-declarator
1113 init-declarator:
1114 declarator simple-asm-expr[opt] attributes[opt]
1115 declarator simple-asm-expr[opt] attributes[opt] = initializer
1117 GNU extensions:
1119 nested-function-definition:
1120 declaration-specifiers declarator declaration-list[opt]
1121 compound-statement
1123 The simple-asm-expr and attributes are GNU extensions.
1125 This function does not handle __extension__; that is handled in its
1126 callers. ??? Following the old parser, __extension__ may start
1127 external declarations, declarations in functions and declarations
1128 at the start of "for" loops, but not old-style parameter
1129 declarations.
1131 C99 requires declaration specifiers in a function definition; the
1132 absence is diagnosed through the diagnosis of implicit int. In GNU
1133 C we also allow but diagnose declarations without declaration
1134 specifiers, but only at top level (elsewhere they conflict with
1135 other syntax). */
1137 static void
1138 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1139 bool nested, bool start_attr_ok)
1141 struct c_declspecs *specs;
1142 tree prefix_attrs;
1143 tree all_prefix_attrs;
1144 bool diagnosed_no_specs = false;
1145 specs = build_null_declspecs ();
1146 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1147 if (parser->error)
1149 c_parser_skip_to_end_of_block_or_statement (parser);
1150 return;
1152 if (nested && !specs->declspecs_seen_p)
1154 c_parser_error (parser, "expected declaration specifiers");
1155 c_parser_skip_to_end_of_block_or_statement (parser);
1156 return;
1158 finish_declspecs (specs);
1159 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1161 if (empty_ok)
1162 shadow_tag (specs);
1163 else
1165 shadow_tag_warned (specs, 1);
1166 pedwarn ("empty declaration");
1168 c_parser_consume_token (parser);
1169 return;
1171 pending_xref_error ();
1172 prefix_attrs = specs->attrs;
1173 all_prefix_attrs = prefix_attrs;
1174 specs->attrs = NULL_TREE;
1175 while (true)
1177 struct c_declarator *declarator;
1178 bool dummy = false;
1179 tree fnbody;
1180 /* Declaring either one or more declarators (in which case we
1181 should diagnose if there were no declaration specifiers) or a
1182 function definition (in which case the diagnostic for
1183 implicit int suffices). */
1184 declarator = c_parser_declarator (parser, specs->type_seen_p,
1185 C_DTR_NORMAL, &dummy);
1186 if (declarator == NULL)
1188 c_parser_skip_to_end_of_block_or_statement (parser);
1189 return;
1191 if (c_parser_next_token_is (parser, CPP_EQ)
1192 || c_parser_next_token_is (parser, CPP_COMMA)
1193 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1194 || c_parser_next_token_is_keyword (parser, RID_ASM)
1195 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1197 tree asm_name = NULL_TREE;
1198 tree postfix_attrs = NULL_TREE;
1199 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1201 diagnosed_no_specs = true;
1202 pedwarn ("data definition has no type or storage class");
1204 /* Having seen a data definition, there cannot now be a
1205 function definition. */
1206 fndef_ok = false;
1207 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1208 asm_name = c_parser_simple_asm_expr (parser);
1209 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1210 postfix_attrs = c_parser_attributes (parser);
1211 if (c_parser_next_token_is (parser, CPP_EQ))
1213 tree d;
1214 struct c_expr init;
1215 c_parser_consume_token (parser);
1216 /* The declaration of the variable is in effect while
1217 its initializer is parsed. */
1218 d = start_decl (declarator, specs, true,
1219 chainon (postfix_attrs, all_prefix_attrs));
1220 if (!d)
1221 d = error_mark_node;
1222 start_init (d, asm_name, global_bindings_p ());
1223 init = c_parser_initializer (parser);
1224 finish_init ();
1225 if (d != error_mark_node)
1227 maybe_warn_string_init (TREE_TYPE (d), init);
1228 finish_decl (d, init.value, asm_name);
1231 else
1233 tree d = start_decl (declarator, specs, false,
1234 chainon (postfix_attrs,
1235 all_prefix_attrs));
1236 if (d)
1237 finish_decl (d, NULL_TREE, asm_name);
1239 if (c_parser_next_token_is (parser, CPP_COMMA))
1241 c_parser_consume_token (parser);
1242 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1243 all_prefix_attrs = chainon (c_parser_attributes (parser),
1244 prefix_attrs);
1245 else
1246 all_prefix_attrs = prefix_attrs;
1247 continue;
1249 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1251 c_parser_consume_token (parser);
1252 return;
1254 else
1256 c_parser_error (parser, "expected %<,%> or %<;%>");
1257 c_parser_skip_to_end_of_block_or_statement (parser);
1258 return;
1261 else if (!fndef_ok)
1263 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1264 "%<asm%> or %<__attribute__%>");
1265 c_parser_skip_to_end_of_block_or_statement (parser);
1266 return;
1268 /* Function definition (nested or otherwise). */
1269 if (nested)
1271 if (pedantic)
1272 pedwarn ("ISO C forbids nested functions");
1273 push_function_context ();
1275 if (!start_function (specs, declarator, all_prefix_attrs))
1277 /* This can appear in many cases looking nothing like a
1278 function definition, so we don't give a more specific
1279 error suggesting there was one. */
1280 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1281 "or %<__attribute__%>");
1282 if (nested)
1283 pop_function_context ();
1284 break;
1286 /* Parse old-style parameter declarations. ??? Attributes are
1287 not allowed to start declaration specifiers here because of a
1288 syntax conflict between a function declaration with attribute
1289 suffix and a function definition with an attribute prefix on
1290 first old-style parameter declaration. Following the old
1291 parser, they are not accepted on subsequent old-style
1292 parameter declarations either. However, there is no
1293 ambiguity after the first declaration, nor indeed on the
1294 first as long as we don't allow postfix attributes after a
1295 declarator with a nonempty identifier list in a definition;
1296 and postfix attributes have never been accepted here in
1297 function definitions either. */
1298 while (c_parser_next_token_is_not (parser, CPP_EOF)
1299 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1300 c_parser_declaration_or_fndef (parser, false, false, true, false);
1301 DECL_SOURCE_LOCATION (current_function_decl)
1302 = c_parser_peek_token (parser)->location;
1303 store_parm_decls ();
1304 fnbody = c_parser_compound_statement (parser);
1305 if (nested)
1307 tree decl = current_function_decl;
1308 add_stmt (fnbody);
1309 finish_function ();
1310 pop_function_context ();
1311 add_stmt (build_stmt (DECL_EXPR, decl));
1313 else
1315 add_stmt (fnbody);
1316 finish_function ();
1318 break;
1322 /* Parse an asm-definition (asm() outside a function body). This is a
1323 GNU extension.
1325 asm-definition:
1326 simple-asm-expr ;
1329 static void
1330 c_parser_asm_definition (c_parser *parser)
1332 tree asm_str = c_parser_simple_asm_expr (parser);
1333 /* ??? This only works sensibly in the presence of
1334 -fno-unit-at-a-time; file-scope asms really need to be passed to
1335 cgraph which needs to preserve the order of functions and
1336 file-scope asms. */
1337 if (asm_str)
1338 assemble_asm (asm_str);
1339 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1342 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1343 6.7), adding them to SPECS (which may already include some).
1344 Storage class specifiers are accepted iff SCSPEC_OK; type
1345 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1346 the start iff START_ATTR_OK.
1348 declaration-specifiers:
1349 storage-class-specifier declaration-specifiers[opt]
1350 type-specifier declaration-specifiers[opt]
1351 type-qualifier declaration-specifiers[opt]
1352 function-specifier declaration-specifiers[opt]
1354 Function specifiers (inline) are from C99, and are currently
1355 handled as storage class specifiers, as is __thread.
1357 C90 6.5.1, C99 6.7.1:
1358 storage-class-specifier:
1359 typedef
1360 extern
1361 static
1362 auto
1363 register
1365 C99 6.7.4:
1366 function-specifier:
1367 inline
1369 C90 6.5.2, C99 6.7.2:
1370 type-specifier:
1371 void
1372 char
1373 short
1375 long
1376 float
1377 double
1378 signed
1379 unsigned
1380 _Bool
1381 _Complex
1382 [_Imaginary removed in C99 TC2]
1383 struct-or-union-specifier
1384 enum-specifier
1385 typedef-name
1387 (_Bool and _Complex are new in C99.)
1389 C90 6.5.3, C99 6.7.3:
1391 type-qualifier:
1392 const
1393 restrict
1394 volatile
1396 (restrict is new in C99.)
1398 GNU extensions:
1400 declaration-specifiers:
1401 attributes declaration-specifiers[opt]
1403 storage-class-specifier:
1404 __thread
1406 type-specifier:
1407 typeof-specifier
1408 _Decimal32
1409 _Decimal64
1410 _Decimal128
1412 Objective-C:
1414 type-specifier:
1415 class-name objc-protocol-refs[opt]
1416 typedef-name objc-protocol-refs
1417 objc-protocol-refs
1420 static void
1421 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1422 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1424 bool attrs_ok = start_attr_ok;
1425 bool seen_type = specs->type_seen_p;
1426 while (c_parser_next_token_is (parser, CPP_NAME)
1427 || c_parser_next_token_is (parser, CPP_KEYWORD)
1428 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1430 struct c_typespec t;
1431 tree attrs;
1432 if (c_parser_next_token_is (parser, CPP_NAME))
1434 tree value = c_parser_peek_token (parser)->value;
1435 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1436 /* This finishes the specifiers unless a type name is OK, it
1437 is declared as a type name and a type name hasn't yet
1438 been seen. */
1439 if (!typespec_ok || seen_type
1440 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1441 break;
1442 c_parser_consume_token (parser);
1443 seen_type = true;
1444 attrs_ok = true;
1445 if (kind == C_ID_TYPENAME
1446 && (!c_dialect_objc ()
1447 || c_parser_next_token_is_not (parser, CPP_LESS)))
1449 t.kind = ctsk_typedef;
1450 /* For a typedef name, record the meaning, not the name.
1451 In case of 'foo foo, bar;'. */
1452 t.spec = lookup_name (value);
1454 else
1456 tree proto = NULL_TREE;
1457 gcc_assert (c_dialect_objc ());
1458 t.kind = ctsk_objc;
1459 if (c_parser_next_token_is (parser, CPP_LESS))
1460 proto = c_parser_objc_protocol_refs (parser);
1461 t.spec = objc_get_protocol_qualified_type (value, proto);
1463 declspecs_add_type (specs, t);
1464 continue;
1466 if (c_parser_next_token_is (parser, CPP_LESS))
1468 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1469 nisse@lysator.liu.se. */
1470 tree proto;
1471 gcc_assert (c_dialect_objc ());
1472 if (!typespec_ok || seen_type)
1473 break;
1474 proto = c_parser_objc_protocol_refs (parser);
1475 t.kind = ctsk_objc;
1476 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1477 declspecs_add_type (specs, t);
1478 continue;
1480 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1481 switch (c_parser_peek_token (parser)->keyword)
1483 case RID_STATIC:
1484 case RID_EXTERN:
1485 case RID_REGISTER:
1486 case RID_TYPEDEF:
1487 case RID_INLINE:
1488 case RID_AUTO:
1489 case RID_THREAD:
1490 if (!scspec_ok)
1491 goto out;
1492 attrs_ok = true;
1493 /* TODO: Distinguish between function specifiers (inline)
1494 and storage class specifiers, either here or in
1495 declspecs_add_scspec. */
1496 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1497 c_parser_consume_token (parser);
1498 break;
1499 case RID_UNSIGNED:
1500 case RID_LONG:
1501 case RID_SHORT:
1502 case RID_SIGNED:
1503 case RID_COMPLEX:
1504 case RID_INT:
1505 case RID_CHAR:
1506 case RID_FLOAT:
1507 case RID_DOUBLE:
1508 case RID_VOID:
1509 case RID_DFLOAT32:
1510 case RID_DFLOAT64:
1511 case RID_DFLOAT128:
1512 case RID_BOOL:
1513 if (!typespec_ok)
1514 goto out;
1515 attrs_ok = true;
1516 seen_type = true;
1517 OBJC_NEED_RAW_IDENTIFIER (1);
1518 t.kind = ctsk_resword;
1519 t.spec = c_parser_peek_token (parser)->value;
1520 declspecs_add_type (specs, t);
1521 c_parser_consume_token (parser);
1522 break;
1523 case RID_ENUM:
1524 if (!typespec_ok)
1525 goto out;
1526 attrs_ok = true;
1527 seen_type = true;
1528 t = c_parser_enum_specifier (parser);
1529 declspecs_add_type (specs, t);
1530 break;
1531 case RID_STRUCT:
1532 case RID_UNION:
1533 if (!typespec_ok)
1534 goto out;
1535 attrs_ok = true;
1536 seen_type = true;
1537 t = c_parser_struct_or_union_specifier (parser);
1538 declspecs_add_type (specs, t);
1539 break;
1540 case RID_TYPEOF:
1541 /* ??? The old parser rejected typeof after other type
1542 specifiers, but is a syntax error the best way of
1543 handling this? */
1544 if (!typespec_ok || seen_type)
1545 goto out;
1546 attrs_ok = true;
1547 seen_type = true;
1548 t = c_parser_typeof_specifier (parser);
1549 declspecs_add_type (specs, t);
1550 break;
1551 case RID_CONST:
1552 case RID_VOLATILE:
1553 case RID_RESTRICT:
1554 attrs_ok = true;
1555 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1556 c_parser_consume_token (parser);
1557 break;
1558 case RID_ATTRIBUTE:
1559 if (!attrs_ok)
1560 goto out;
1561 attrs = c_parser_attributes (parser);
1562 declspecs_add_attrs (specs, attrs);
1563 break;
1564 default:
1565 goto out;
1568 out: ;
1571 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1573 enum-specifier:
1574 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1575 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1576 enum attributes[opt] identifier
1578 The form with trailing comma is new in C99. The forms with
1579 attributes are GNU extensions. In GNU C, we accept any expression
1580 without commas in the syntax (assignment expressions, not just
1581 conditional expressions); assignment expressions will be diagnosed
1582 as non-constant.
1584 enumerator-list:
1585 enumerator
1586 enumerator-list , enumerator
1588 enumerator:
1589 enumeration-constant
1590 enumeration-constant = constant-expression
1593 static struct c_typespec
1594 c_parser_enum_specifier (c_parser *parser)
1596 struct c_typespec ret;
1597 tree attrs;
1598 tree ident = NULL_TREE;
1599 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1600 c_parser_consume_token (parser);
1601 attrs = c_parser_attributes (parser);
1602 if (c_parser_next_token_is (parser, CPP_NAME))
1604 ident = c_parser_peek_token (parser)->value;
1605 c_parser_consume_token (parser);
1607 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1609 /* Parse an enum definition. */
1610 tree type = start_enum (ident);
1611 tree postfix_attrs;
1612 /* We chain the enumerators in reverse order, then put them in
1613 forward order at the end. */
1614 tree values = NULL_TREE;
1615 c_parser_consume_token (parser);
1616 while (true)
1618 tree enum_id;
1619 tree enum_value;
1620 tree enum_decl;
1621 bool seen_comma;
1622 if (c_parser_next_token_is_not (parser, CPP_NAME))
1624 c_parser_error (parser, "expected identifier");
1625 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1626 values = error_mark_node;
1627 break;
1629 enum_id = c_parser_peek_token (parser)->value;
1630 c_parser_consume_token (parser);
1631 if (c_parser_next_token_is (parser, CPP_EQ))
1633 c_parser_consume_token (parser);
1634 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1636 else
1637 enum_value = NULL_TREE;
1638 enum_decl = build_enumerator (enum_id, enum_value);
1639 TREE_CHAIN (enum_decl) = values;
1640 values = enum_decl;
1641 seen_comma = false;
1642 if (c_parser_next_token_is (parser, CPP_COMMA))
1644 seen_comma = true;
1645 c_parser_consume_token (parser);
1647 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1649 if (seen_comma && pedantic && !flag_isoc99)
1650 pedwarn ("comma at end of enumerator list");
1651 c_parser_consume_token (parser);
1652 break;
1654 if (!seen_comma)
1656 c_parser_error (parser, "expected %<,%> or %<}%>");
1657 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1658 values = error_mark_node;
1659 break;
1662 postfix_attrs = c_parser_attributes (parser);
1663 ret.spec = finish_enum (type, nreverse (values),
1664 chainon (attrs, postfix_attrs));
1665 ret.kind = ctsk_tagdef;
1666 return ret;
1668 else if (!ident)
1670 c_parser_error (parser, "expected %<{%>");
1671 ret.spec = error_mark_node;
1672 ret.kind = ctsk_tagref;
1673 return ret;
1675 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1676 /* In ISO C, enumerated types can be referred to only if already
1677 defined. */
1678 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1679 pedwarn ("ISO C forbids forward references to %<enum%> types");
1680 return ret;
1683 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1685 struct-or-union-specifier:
1686 struct-or-union attributes[opt] identifier[opt]
1687 { struct-contents } attributes[opt]
1688 struct-or-union attributes[opt] identifier
1690 struct-contents:
1691 struct-declaration-list
1693 struct-declaration-list:
1694 struct-declaration ;
1695 struct-declaration-list struct-declaration ;
1697 GNU extensions:
1699 struct-contents:
1700 empty
1701 struct-declaration
1702 struct-declaration-list struct-declaration
1704 struct-declaration-list:
1705 struct-declaration-list ;
1708 (Note that in the syntax here, unlike that in ISO C, the semicolons
1709 are included here rather than in struct-declaration, in order to
1710 describe the syntax with extra semicolons and missing semicolon at
1711 end.)
1713 Objective-C:
1715 struct-declaration-list:
1716 @defs ( class-name )
1718 (Note this does not include a trailing semicolon, but can be
1719 followed by further declarations, and gets a pedwarn-if-pedantic
1720 when followed by a semicolon.) */
1722 static struct c_typespec
1723 c_parser_struct_or_union_specifier (c_parser *parser)
1725 struct c_typespec ret;
1726 tree attrs;
1727 tree ident = NULL_TREE;
1728 enum tree_code code;
1729 switch (c_parser_peek_token (parser)->keyword)
1731 case RID_STRUCT:
1732 code = RECORD_TYPE;
1733 break;
1734 case RID_UNION:
1735 code = UNION_TYPE;
1736 break;
1737 default:
1738 gcc_unreachable ();
1740 c_parser_consume_token (parser);
1741 attrs = c_parser_attributes (parser);
1742 if (c_parser_next_token_is (parser, CPP_NAME))
1744 ident = c_parser_peek_token (parser)->value;
1745 c_parser_consume_token (parser);
1747 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1749 /* Parse a struct or union definition. Start the scope of the
1750 tag before parsing components. */
1751 tree type = start_struct (code, ident);
1752 tree postfix_attrs;
1753 /* We chain the components in reverse order, then put them in
1754 forward order at the end. Each struct-declaration may
1755 declare multiple components (comma-separated), so we must use
1756 chainon to join them, although when parsing each
1757 struct-declaration we can use TREE_CHAIN directly.
1759 The theory behind all this is that there will be more
1760 semicolon separated fields than comma separated fields, and
1761 so we'll be minimizing the number of node traversals required
1762 by chainon. */
1763 tree contents = NULL_TREE;
1764 c_parser_consume_token (parser);
1765 /* Handle the Objective-C @defs construct,
1766 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1767 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1769 tree name;
1770 gcc_assert (c_dialect_objc ());
1771 c_parser_consume_token (parser);
1772 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1773 goto end_at_defs;
1774 if (c_parser_next_token_is (parser, CPP_NAME)
1775 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1777 name = c_parser_peek_token (parser)->value;
1778 c_parser_consume_token (parser);
1780 else
1782 c_parser_error (parser, "expected class name");
1783 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1784 goto end_at_defs;
1786 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1787 "expected %<)%>");
1788 contents = nreverse (objc_get_class_ivars (name));
1790 end_at_defs:
1791 /* Parse the struct-declarations and semicolons. Problems with
1792 semicolons are diagnosed here; empty structures are diagnosed
1793 elsewhere. */
1794 while (true)
1796 tree decls;
1797 /* Parse any stray semicolon. */
1798 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1800 if (pedantic)
1801 pedwarn ("extra semicolon in struct or union specified");
1802 c_parser_consume_token (parser);
1803 continue;
1805 /* Stop if at the end of the struct or union contents. */
1806 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1808 c_parser_consume_token (parser);
1809 break;
1811 /* Parse some comma-separated declarations, but not the
1812 trailing semicolon if any. */
1813 decls = c_parser_struct_declaration (parser);
1814 contents = chainon (decls, contents);
1815 /* If no semicolon follows, either we have a parse error or
1816 are at the end of the struct or union and should
1817 pedwarn. */
1818 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1819 c_parser_consume_token (parser);
1820 else
1822 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1823 pedwarn ("no semicolon at end of struct or union");
1824 else
1826 c_parser_error (parser, "expected %<;%>");
1827 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1828 break;
1832 postfix_attrs = c_parser_attributes (parser);
1833 ret.spec = finish_struct (type, nreverse (contents),
1834 chainon (attrs, postfix_attrs));
1835 ret.kind = ctsk_tagdef;
1836 return ret;
1838 else if (!ident)
1840 c_parser_error (parser, "expected %<{%>");
1841 ret.spec = error_mark_node;
1842 ret.kind = ctsk_tagref;
1843 return ret;
1845 ret = parser_xref_tag (code, ident);
1846 return ret;
1849 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1850 the trailing semicolon.
1852 struct-declaration:
1853 specifier-qualifier-list struct-declarator-list
1855 specifier-qualifier-list:
1856 type-specifier specifier-qualifier-list[opt]
1857 type-qualifier specifier-qualifier-list[opt]
1858 attributes specifier-qualifier-list[opt]
1860 struct-declarator-list:
1861 struct-declarator
1862 struct-declarator-list , attributes[opt] struct-declarator
1864 struct-declarator:
1865 declarator attributes[opt]
1866 declarator[opt] : constant-expression attributes[opt]
1868 GNU extensions:
1870 struct-declaration:
1871 __extension__ struct-declaration
1872 specifier-qualifier-list
1874 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1875 of attributes where shown is a GNU extension. In GNU C, we accept
1876 any expression without commas in the syntax (assignment
1877 expressions, not just conditional expressions); assignment
1878 expressions will be diagnosed as non-constant. */
1880 static tree
1881 c_parser_struct_declaration (c_parser *parser)
1883 struct c_declspecs *specs;
1884 tree prefix_attrs;
1885 tree all_prefix_attrs;
1886 tree decls;
1887 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1889 int ext;
1890 tree decl;
1891 ext = disable_extension_diagnostics ();
1892 c_parser_consume_token (parser);
1893 decl = c_parser_struct_declaration (parser);
1894 restore_extension_diagnostics (ext);
1895 return decl;
1897 specs = build_null_declspecs ();
1898 c_parser_declspecs (parser, specs, false, true, true);
1899 if (parser->error)
1900 return NULL_TREE;
1901 if (!specs->declspecs_seen_p)
1903 c_parser_error (parser, "expected specifier-qualifier-list");
1904 return NULL_TREE;
1906 finish_declspecs (specs);
1907 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1909 tree ret;
1910 if (!specs->type_seen_p)
1912 if (pedantic)
1913 pedwarn ("ISO C forbids member declarations with no members");
1914 shadow_tag_warned (specs, pedantic);
1915 ret = NULL_TREE;
1917 else
1919 /* Support for unnamed structs or unions as members of
1920 structs or unions (which is [a] useful and [b] supports
1921 MS P-SDK). */
1922 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1924 return ret;
1926 pending_xref_error ();
1927 prefix_attrs = specs->attrs;
1928 all_prefix_attrs = prefix_attrs;
1929 specs->attrs = NULL_TREE;
1930 decls = NULL_TREE;
1931 while (true)
1933 /* Declaring one or more declarators or un-named bit-fields. */
1934 struct c_declarator *declarator;
1935 bool dummy = false;
1936 if (c_parser_next_token_is (parser, CPP_COLON))
1937 declarator = build_id_declarator (NULL_TREE);
1938 else
1939 declarator = c_parser_declarator (parser, specs->type_seen_p,
1940 C_DTR_NORMAL, &dummy);
1941 if (declarator == NULL)
1943 c_parser_skip_to_end_of_block_or_statement (parser);
1944 break;
1946 if (c_parser_next_token_is (parser, CPP_COLON)
1947 || c_parser_next_token_is (parser, CPP_COMMA)
1948 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1949 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1950 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1952 tree postfix_attrs = NULL_TREE;
1953 tree width = NULL_TREE;
1954 tree d;
1955 if (c_parser_next_token_is (parser, CPP_COLON))
1957 c_parser_consume_token (parser);
1958 width = c_parser_expr_no_commas (parser, NULL).value;
1960 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1961 postfix_attrs = c_parser_attributes (parser);
1962 d = grokfield (declarator, specs, width);
1963 decl_attributes (&d, chainon (postfix_attrs,
1964 all_prefix_attrs), 0);
1965 TREE_CHAIN (d) = decls;
1966 decls = d;
1967 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1968 all_prefix_attrs = chainon (c_parser_attributes (parser),
1969 prefix_attrs);
1970 else
1971 all_prefix_attrs = prefix_attrs;
1972 if (c_parser_next_token_is (parser, CPP_COMMA))
1973 c_parser_consume_token (parser);
1974 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
1975 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1977 /* Semicolon consumed in caller. */
1978 break;
1980 else
1982 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
1983 break;
1986 else
1988 c_parser_error (parser,
1989 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
1990 "%<__attribute__%>");
1991 break;
1994 return decls;
1997 /* Parse a typeof specifier (a GNU extension).
1999 typeof-specifier:
2000 typeof ( expression )
2001 typeof ( type-name )
2004 static struct c_typespec
2005 c_parser_typeof_specifier (c_parser *parser)
2007 struct c_typespec ret;
2008 ret.kind = ctsk_typeof;
2009 ret.spec = error_mark_node;
2010 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2011 c_parser_consume_token (parser);
2012 skip_evaluation++;
2013 in_typeof++;
2014 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2016 skip_evaluation--;
2017 in_typeof--;
2018 return ret;
2020 if (c_parser_next_token_starts_typename (parser))
2022 struct c_type_name *type = c_parser_type_name (parser);
2023 skip_evaluation--;
2024 in_typeof--;
2025 if (type != NULL)
2027 ret.spec = groktypename (type);
2028 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2031 else
2033 struct c_expr expr = c_parser_expression (parser);
2034 skip_evaluation--;
2035 in_typeof--;
2036 if (TREE_CODE (expr.value) == COMPONENT_REF
2037 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2038 error ("%<typeof%> applied to a bit-field");
2039 ret.spec = TREE_TYPE (expr.value);
2040 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2042 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2043 return ret;
2046 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2047 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2048 be redeclared; otherwise it may not. KIND indicates which kind of
2049 declarator is wanted. Returns a valid declarator except in the
2050 case of a syntax error in which case NULL is returned. *SEEN_ID is
2051 set to true if an identifier being declared is seen; this is used
2052 to diagnose bad forms of abstract array declarators and to
2053 determine whether an identifier list is syntactically permitted.
2055 declarator:
2056 pointer[opt] direct-declarator
2058 direct-declarator:
2059 identifier
2060 ( attributes[opt] declarator )
2061 direct-declarator array-declarator
2062 direct-declarator ( parameter-type-list )
2063 direct-declarator ( identifier-list[opt] )
2065 pointer:
2066 * type-qualifier-list[opt]
2067 * type-qualifier-list[opt] pointer
2069 type-qualifier-list:
2070 type-qualifier
2071 attributes
2072 type-qualifier-list type-qualifier
2073 type-qualifier-list attributes
2075 parameter-type-list:
2076 parameter-list
2077 parameter-list , ...
2079 parameter-list:
2080 parameter-declaration
2081 parameter-list , parameter-declaration
2083 parameter-declaration:
2084 declaration-specifiers declarator attributes[opt]
2085 declaration-specifiers abstract-declarator[opt] attributes[opt]
2087 identifier-list:
2088 identifier
2089 identifier-list , identifier
2091 abstract-declarator:
2092 pointer
2093 pointer[opt] direct-abstract-declarator
2095 direct-abstract-declarator:
2096 ( attributes[opt] abstract-declarator )
2097 direct-abstract-declarator[opt] array-declarator
2098 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2100 GNU extensions:
2102 direct-declarator:
2103 direct-declarator ( parameter-forward-declarations
2104 parameter-type-list[opt] )
2106 direct-abstract-declarator:
2107 direct-abstract-declarator[opt] ( parameter-forward-declarations
2108 parameter-type-list[opt] )
2110 parameter-forward-declarations:
2111 parameter-list ;
2112 parameter-forward-declarations parameter-list ;
2114 The uses of attributes shown above are GNU extensions.
2116 Some forms of array declarator are not included in C99 in the
2117 syntax for abstract declarators; these are disallowed elsewhere.
2118 This may be a defect (DR#289).
2120 This function also accepts an omitted abstract declarator as being
2121 an abstract declarator, although not part of the formal syntax. */
2123 static struct c_declarator *
2124 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2125 bool *seen_id)
2127 /* Parse any initial pointer part. */
2128 if (c_parser_next_token_is (parser, CPP_MULT))
2130 struct c_declspecs *quals_attrs = build_null_declspecs ();
2131 struct c_declarator *inner;
2132 c_parser_consume_token (parser);
2133 c_parser_declspecs (parser, quals_attrs, false, false, true);
2134 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2135 if (inner == NULL)
2136 return NULL;
2137 else
2138 return make_pointer_declarator (quals_attrs, inner);
2140 /* Now we have a direct declarator, direct abstract declarator or
2141 nothing (which counts as a direct abstract declarator here). */
2142 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2145 /* Parse a direct declarator or direct abstract declarator; arguments
2146 as c_parser_declarator. */
2148 static struct c_declarator *
2149 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2150 bool *seen_id)
2152 /* The direct declarator must start with an identifier (possibly
2153 omitted) or a parenthesized declarator (possibly abstract). In
2154 an ordinary declarator, initial parentheses must start a
2155 parenthesized declarator. In an abstract declarator or parameter
2156 declarator, they could start a parenthesized declarator or a
2157 parameter list. To tell which, the open parenthesis and any
2158 following attributes must be read. If a declaration specifier
2159 follows, then it is a parameter list; if the specifier is a
2160 typedef name, there might be an ambiguity about redeclaring it,
2161 which is resolved in the direction of treating it as a typedef
2162 name. If a close parenthesis follows, it is also an empty
2163 parameter list, as the syntax does not permit empty abstract
2164 declarators. Otherwise, it is a parenthesized declarator (in
2165 which case the analysis may be repeated inside it, recursively).
2167 ??? There is an ambiguity in a parameter declaration "int
2168 (__attribute__((foo)) x)", where x is not a typedef name: it
2169 could be an abstract declarator for a function, or declare x with
2170 parentheses. The proper resolution of this ambiguity needs
2171 documenting. At present we follow an accident of the old
2172 parser's implementation, whereby the first parameter must have
2173 some declaration specifiers other than just attributes. Thus as
2174 a parameter declaration it is treated as a parenthesized
2175 parameter named x, and as an abstract declarator it is
2176 rejected.
2178 ??? Also following the old parser, attributes inside an empty
2179 parameter list are ignored, making it a list not yielding a
2180 prototype, rather than giving an error or making it have one
2181 parameter with implicit type int.
2183 ??? Also following the old parser, typedef names may be
2184 redeclared in declarators, but not Objective-C class names. */
2186 if (kind != C_DTR_ABSTRACT
2187 && c_parser_next_token_is (parser, CPP_NAME)
2188 && ((type_seen_p
2189 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2190 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2192 struct c_declarator *inner
2193 = build_id_declarator (c_parser_peek_token (parser)->value);
2194 *seen_id = true;
2195 inner->id_loc = c_parser_peek_token (parser)->location;
2196 c_parser_consume_token (parser);
2197 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2200 if (kind != C_DTR_NORMAL
2201 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2203 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2204 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2207 /* Either we are at the end of an abstract declarator, or we have
2208 parentheses. */
2210 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2212 tree attrs;
2213 struct c_declarator *inner;
2214 c_parser_consume_token (parser);
2215 attrs = c_parser_attributes (parser);
2216 if (kind != C_DTR_NORMAL
2217 && (c_parser_next_token_starts_declspecs (parser)
2218 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2220 struct c_arg_info *args
2221 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2222 attrs);
2223 if (args == NULL)
2224 return NULL;
2225 else
2227 inner
2228 = build_function_declarator (args,
2229 build_id_declarator (NULL_TREE));
2230 return c_parser_direct_declarator_inner (parser, *seen_id,
2231 inner);
2234 /* A parenthesized declarator. */
2235 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2236 if (inner != NULL && attrs != NULL)
2237 inner = build_attrs_declarator (attrs, inner);
2238 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2240 c_parser_consume_token (parser);
2241 if (inner == NULL)
2242 return NULL;
2243 else
2244 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2246 else
2248 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2249 "expected %<)%>");
2250 return NULL;
2253 else
2255 if (kind == C_DTR_NORMAL)
2257 c_parser_error (parser, "expected identifier or %<(%>");
2258 return NULL;
2260 else
2261 return build_id_declarator (NULL_TREE);
2265 /* Parse part of a direct declarator or direct abstract declarator,
2266 given that some (in INNER) has already been parsed; ID_PRESENT is
2267 true if an identifier is present, false for an abstract
2268 declarator. */
2270 static struct c_declarator *
2271 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2272 struct c_declarator *inner)
2274 /* Parse a sequence of array declarators and parameter lists. */
2275 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2277 struct c_declarator *declarator;
2278 struct c_declspecs *quals_attrs = build_null_declspecs ();
2279 bool static_seen;
2280 bool star_seen;
2281 tree dimen;
2282 c_parser_consume_token (parser);
2283 c_parser_declspecs (parser, quals_attrs, false, false, true);
2284 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2285 if (static_seen)
2286 c_parser_consume_token (parser);
2287 if (static_seen && !quals_attrs->declspecs_seen_p)
2288 c_parser_declspecs (parser, quals_attrs, false, false, true);
2289 if (!quals_attrs->declspecs_seen_p)
2290 quals_attrs = NULL;
2291 /* If "static" is present, there must be an array dimension.
2292 Otherwise, there may be a dimension, "*", or no
2293 dimension. */
2294 if (static_seen)
2296 star_seen = false;
2297 dimen = c_parser_expr_no_commas (parser, NULL).value;
2299 else
2301 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2303 dimen = NULL_TREE;
2304 star_seen = false;
2306 else if (c_parser_next_token_is (parser, CPP_MULT))
2308 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2310 dimen = NULL_TREE;
2311 star_seen = true;
2312 c_parser_consume_token (parser);
2314 else
2316 star_seen = false;
2317 dimen = c_parser_expr_no_commas (parser, NULL).value;
2320 else
2322 star_seen = false;
2323 dimen = c_parser_expr_no_commas (parser, NULL).value;
2326 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2327 c_parser_consume_token (parser);
2328 else
2330 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2331 "expected %<]%>");
2332 return NULL;
2334 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2335 star_seen);
2336 inner = set_array_declarator_inner (declarator, inner, !id_present);
2337 return c_parser_direct_declarator_inner (parser, id_present, inner);
2339 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2341 tree attrs;
2342 struct c_arg_info *args;
2343 c_parser_consume_token (parser);
2344 attrs = c_parser_attributes (parser);
2345 args = c_parser_parms_declarator (parser, id_present, attrs);
2346 if (args == NULL)
2347 return NULL;
2348 else
2350 inner = build_function_declarator (args, inner);
2351 return c_parser_direct_declarator_inner (parser, id_present, inner);
2354 return inner;
2357 /* Parse a parameter list or identifier list, including the closing
2358 parenthesis but not the opening one. ATTRS are the attributes at
2359 the start of the list. ID_LIST_OK is true if an identifier list is
2360 acceptable; such a list must not have attributes at the start. */
2362 static struct c_arg_info *
2363 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2365 push_scope ();
2366 declare_parm_level ();
2367 /* If the list starts with an identifier, it is an identifier list.
2368 Otherwise, it is either a prototype list or an empty list. */
2369 if (id_list_ok
2370 && !attrs
2371 && c_parser_next_token_is (parser, CPP_NAME)
2372 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2374 tree list = NULL_TREE, *nextp = &list;
2375 while (c_parser_next_token_is (parser, CPP_NAME)
2376 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2378 *nextp = build_tree_list (NULL_TREE,
2379 c_parser_peek_token (parser)->value);
2380 nextp = & TREE_CHAIN (*nextp);
2381 c_parser_consume_token (parser);
2382 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2383 break;
2384 c_parser_consume_token (parser);
2385 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2387 c_parser_error (parser, "expected identifier");
2388 break;
2391 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2393 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2394 ret->parms = 0;
2395 ret->tags = 0;
2396 ret->types = list;
2397 ret->others = 0;
2398 c_parser_consume_token (parser);
2399 pop_scope ();
2400 return ret;
2402 else
2404 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2405 "expected %<)%>");
2406 pop_scope ();
2407 return NULL;
2410 else
2412 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2413 pop_scope ();
2414 return ret;
2418 /* Parse a parameter list (possibly empty), including the closing
2419 parenthesis but not the opening one. ATTRS are the attributes at
2420 the start of the list. */
2422 static struct c_arg_info *
2423 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2425 bool good_parm = false;
2426 /* ??? Following the old parser, forward parameter declarations may
2427 use abstract declarators, and if no real parameter declarations
2428 follow the forward declarations then this is not diagnosed. Also
2429 note as above that attributes are ignored as the only contents of
2430 the parentheses, or as the only contents after forward
2431 declarations. */
2432 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2434 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2435 ret->parms = 0;
2436 ret->tags = 0;
2437 ret->types = 0;
2438 ret->others = 0;
2439 c_parser_consume_token (parser);
2440 return ret;
2442 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2444 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2445 ret->parms = 0;
2446 ret->tags = 0;
2447 ret->others = 0;
2448 /* Suppress -Wold-style-definition for this case. */
2449 ret->types = error_mark_node;
2450 error ("ISO C requires a named argument before %<...%>");
2451 c_parser_consume_token (parser);
2452 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2454 c_parser_consume_token (parser);
2455 return ret;
2457 else
2459 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2460 "expected %<)%>");
2461 return NULL;
2464 /* Nonempty list of parameters, either terminated with semicolon
2465 (forward declarations; recurse) or with close parenthesis (normal
2466 function) or with ", ... )" (variadic function). */
2467 while (true)
2469 /* Parse a parameter. */
2470 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2471 attrs = NULL_TREE;
2472 if (parm != NULL)
2474 good_parm = true;
2475 push_parm_decl (parm);
2477 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2479 tree new_attrs;
2480 c_parser_consume_token (parser);
2481 mark_forward_parm_decls ();
2482 new_attrs = c_parser_attributes (parser);
2483 return c_parser_parms_list_declarator (parser, new_attrs);
2485 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2487 c_parser_consume_token (parser);
2488 if (good_parm)
2489 return get_parm_info (false);
2490 else
2492 struct c_arg_info *ret
2493 = XOBNEW (&parser_obstack, struct c_arg_info);
2494 ret->parms = 0;
2495 ret->tags = 0;
2496 ret->types = 0;
2497 ret->others = 0;
2498 return ret;
2501 if (!c_parser_require (parser, CPP_COMMA,
2502 "expected %<;%>, %<,%> or %<)%>"))
2504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2505 return NULL;
2507 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2509 c_parser_consume_token (parser);
2510 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2512 c_parser_consume_token (parser);
2513 if (good_parm)
2514 return get_parm_info (true);
2515 else
2517 struct c_arg_info *ret
2518 = XOBNEW (&parser_obstack, struct c_arg_info);
2519 ret->parms = 0;
2520 ret->tags = 0;
2521 ret->types = 0;
2522 ret->others = 0;
2523 return ret;
2526 else
2528 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2529 "expected %<)%>");
2530 return NULL;
2536 /* Parse a parameter declaration. ATTRS are the attributes at the
2537 start of the declaration if it is the first parameter. */
2539 static struct c_parm *
2540 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2542 struct c_declspecs *specs;
2543 struct c_declarator *declarator;
2544 tree prefix_attrs;
2545 tree postfix_attrs = NULL_TREE;
2546 bool dummy = false;
2547 if (!c_parser_next_token_starts_declspecs (parser))
2549 /* ??? In some Objective-C cases '...' isn't applicable so there
2550 should be a different message. */
2551 c_parser_error (parser,
2552 "expected declaration specifiers or %<...%>");
2553 c_parser_skip_to_end_of_parameter (parser);
2554 return NULL;
2556 specs = build_null_declspecs ();
2557 if (attrs)
2559 declspecs_add_attrs (specs, attrs);
2560 attrs = NULL_TREE;
2562 c_parser_declspecs (parser, specs, true, true, true);
2563 finish_declspecs (specs);
2564 pending_xref_error ();
2565 prefix_attrs = specs->attrs;
2566 specs->attrs = NULL_TREE;
2567 declarator = c_parser_declarator (parser, specs->type_seen_p,
2568 C_DTR_PARM, &dummy);
2569 if (declarator == NULL)
2571 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2572 return NULL;
2574 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2575 postfix_attrs = c_parser_attributes (parser);
2576 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2577 declarator);
2580 /* Parse a string literal in an asm expression. It should not be
2581 translated, and wide string literals are an error although
2582 permitted by the syntax. This is a GNU extension.
2584 asm-string-literal:
2585 string-literal
2587 ??? At present, following the old parser, the caller needs to have
2588 set c_lex_string_translate to 0. It would be better to follow the
2589 C++ parser rather than using the c_lex_string_translate kludge. */
2591 static tree
2592 c_parser_asm_string_literal (c_parser *parser)
2594 tree str;
2595 if (c_parser_next_token_is (parser, CPP_STRING))
2597 str = c_parser_peek_token (parser)->value;
2598 c_parser_consume_token (parser);
2600 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2602 error ("wide string literal in %<asm%>");
2603 str = build_string (1, "");
2604 c_parser_consume_token (parser);
2606 else
2608 c_parser_error (parser, "expected string literal");
2609 str = NULL_TREE;
2611 return str;
2614 /* Parse a simple asm expression. This is used in restricted
2615 contexts, where a full expression with inputs and outputs does not
2616 make sense. This is a GNU extension.
2618 simple-asm-expr:
2619 asm ( asm-string-literal )
2622 static tree
2623 c_parser_simple_asm_expr (c_parser *parser)
2625 tree str;
2626 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2627 /* ??? Follow the C++ parser rather than using the
2628 c_lex_string_translate kludge. */
2629 c_lex_string_translate = 0;
2630 c_parser_consume_token (parser);
2631 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2633 c_lex_string_translate = 1;
2634 return NULL_TREE;
2636 str = c_parser_asm_string_literal (parser);
2637 c_lex_string_translate = 1;
2638 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2640 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2641 return NULL_TREE;
2643 return str;
2646 /* Parse (possibly empty) attributes. This is a GNU extension.
2648 attributes:
2649 empty
2650 attributes attribute
2652 attribute:
2653 __attribute__ ( ( attribute-list ) )
2655 attribute-list:
2656 attrib
2657 attribute_list , attrib
2659 attrib:
2660 empty
2661 any-word
2662 any-word ( identifier )
2663 any-word ( identifier , nonempty-expr-list )
2664 any-word ( expr-list )
2666 where the "identifier" must not be declared as a type, and
2667 "any-word" may be any identifier (including one declared as a
2668 type), a reserved word storage class specifier, type specifier or
2669 type qualifier. ??? This still leaves out most reserved keywords
2670 (following the old parser), shouldn't we include them, and why not
2671 allow identifiers declared as types to start the arguments? */
2673 static tree
2674 c_parser_attributes (c_parser *parser)
2676 tree attrs = NULL_TREE;
2677 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2679 /* ??? Follow the C++ parser rather than using the
2680 c_lex_string_translate kludge. */
2681 c_lex_string_translate = 0;
2682 c_parser_consume_token (parser);
2683 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2685 c_lex_string_translate = 1;
2686 return attrs;
2688 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2690 c_lex_string_translate = 1;
2691 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2692 return attrs;
2694 /* Parse the attribute list. */
2695 while (c_parser_next_token_is (parser, CPP_COMMA)
2696 || c_parser_next_token_is (parser, CPP_NAME)
2697 || c_parser_next_token_is (parser, CPP_KEYWORD))
2699 tree attr, attr_name, attr_args;
2700 if (c_parser_next_token_is (parser, CPP_COMMA))
2702 c_parser_consume_token (parser);
2703 continue;
2705 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2707 /* ??? See comment above about what keywords are
2708 accepted here. */
2709 bool ok;
2710 switch (c_parser_peek_token (parser)->keyword)
2712 case RID_STATIC:
2713 case RID_UNSIGNED:
2714 case RID_LONG:
2715 case RID_CONST:
2716 case RID_EXTERN:
2717 case RID_REGISTER:
2718 case RID_TYPEDEF:
2719 case RID_SHORT:
2720 case RID_INLINE:
2721 case RID_VOLATILE:
2722 case RID_SIGNED:
2723 case RID_AUTO:
2724 case RID_RESTRICT:
2725 case RID_COMPLEX:
2726 case RID_THREAD:
2727 case RID_INT:
2728 case RID_CHAR:
2729 case RID_FLOAT:
2730 case RID_DOUBLE:
2731 case RID_VOID:
2732 case RID_DFLOAT32:
2733 case RID_DFLOAT64:
2734 case RID_DFLOAT128:
2735 case RID_BOOL:
2736 ok = true;
2737 break;
2738 default:
2739 ok = false;
2740 break;
2742 if (!ok)
2743 break;
2745 attr_name = c_parser_peek_token (parser)->value;
2746 c_parser_consume_token (parser);
2747 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2749 attr = build_tree_list (attr_name, NULL_TREE);
2750 attrs = chainon (attrs, attr);
2751 continue;
2753 c_parser_consume_token (parser);
2754 /* Parse the attribute contents. If they start with an
2755 identifier which is followed by a comma or close
2756 parenthesis, then the arguments start with that
2757 identifier; otherwise they are an expression list. */
2758 if (c_parser_next_token_is (parser, CPP_NAME)
2759 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2760 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2761 || (c_parser_peek_2nd_token (parser)->type
2762 == CPP_CLOSE_PAREN)))
2764 tree arg1 = c_parser_peek_token (parser)->value;
2765 c_parser_consume_token (parser);
2766 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2767 attr_args = build_tree_list (NULL_TREE, arg1);
2768 else
2770 c_parser_consume_token (parser);
2771 attr_args = tree_cons (NULL_TREE, arg1,
2772 c_parser_expr_list (parser, false));
2775 else
2777 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2778 attr_args = NULL_TREE;
2779 else
2780 attr_args = c_parser_expr_list (parser, false);
2782 attr = build_tree_list (attr_name, attr_args);
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 attrs = chainon (attrs, attr);
2794 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2795 c_parser_consume_token (parser);
2796 else
2798 c_lex_string_translate = 1;
2799 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2800 "expected %<)%>");
2801 return attrs;
2803 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2804 c_parser_consume_token (parser);
2805 else
2807 c_lex_string_translate = 1;
2808 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2809 "expected %<)%>");
2810 return attrs;
2812 c_lex_string_translate = 1;
2814 return attrs;
2817 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2819 type-name:
2820 specifier-qualifier-list abstract-declarator[opt]
2823 static struct c_type_name *
2824 c_parser_type_name (c_parser *parser)
2826 struct c_declspecs *specs = build_null_declspecs ();
2827 struct c_declarator *declarator;
2828 struct c_type_name *ret;
2829 bool dummy = false;
2830 c_parser_declspecs (parser, specs, false, true, true);
2831 if (!specs->declspecs_seen_p)
2833 c_parser_error (parser, "expected specifier-qualifier-list");
2834 return NULL;
2836 pending_xref_error ();
2837 finish_declspecs (specs);
2838 declarator = c_parser_declarator (parser, specs->type_seen_p,
2839 C_DTR_ABSTRACT, &dummy);
2840 if (declarator == NULL)
2841 return NULL;
2842 ret = XOBNEW (&parser_obstack, struct c_type_name);
2843 ret->specs = specs;
2844 ret->declarator = declarator;
2845 return ret;
2848 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2850 initializer:
2851 assignment-expression
2852 { initializer-list }
2853 { initializer-list , }
2855 initializer-list:
2856 designation[opt] initializer
2857 initializer-list , designation[opt] initializer
2859 designation:
2860 designator-list =
2862 designator-list:
2863 designator
2864 designator-list designator
2866 designator:
2867 array-designator
2868 . identifier
2870 array-designator:
2871 [ constant-expression ]
2873 GNU extensions:
2875 initializer:
2878 designation:
2879 array-designator
2880 identifier :
2882 array-designator:
2883 [ constant-expression ... constant-expression ]
2885 Any expression without commas is accepted in the syntax for the
2886 constant-expressions, with non-constant expressions rejected later.
2888 This function is only used for top-level initializers; for nested
2889 ones, see c_parser_initval. */
2891 static struct c_expr
2892 c_parser_initializer (c_parser *parser)
2894 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2895 return c_parser_braced_init (parser, NULL_TREE, false);
2896 else
2898 struct c_expr ret;
2899 ret = c_parser_expr_no_commas (parser, NULL);
2900 if (TREE_CODE (ret.value) != STRING_CST
2901 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
2902 ret = default_function_array_conversion (ret);
2903 return ret;
2907 /* Parse a braced initializer list. TYPE is the type specified for a
2908 compound literal, and NULL_TREE for other initializers and for
2909 nested braced lists. NESTED_P is true for nested braced lists,
2910 false for the list of a compound literal or the list that is the
2911 top-level initializer in a declaration. */
2913 static struct c_expr
2914 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2916 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2917 c_parser_consume_token (parser);
2918 if (nested_p)
2919 push_init_level (0);
2920 else
2921 really_start_incremental_init (type);
2922 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2924 if (pedantic)
2925 pedwarn ("ISO C forbids empty initializer braces");
2927 else
2929 /* Parse a non-empty initializer list, possibly with a trailing
2930 comma. */
2931 while (true)
2933 c_parser_initelt (parser);
2934 if (parser->error)
2935 break;
2936 if (c_parser_next_token_is (parser, CPP_COMMA))
2937 c_parser_consume_token (parser);
2938 else
2939 break;
2940 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2941 break;
2944 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
2946 struct c_expr ret;
2947 ret.value = error_mark_node;
2948 ret.original_code = ERROR_MARK;
2949 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
2950 return ret;
2952 c_parser_consume_token (parser);
2953 return pop_init_level (0);
2956 /* Parse a nested initializer, including designators. */
2958 static void
2959 c_parser_initelt (c_parser *parser)
2961 /* Parse any designator or designator list. A single array
2962 designator may have the subsequent "=" omitted in GNU C, but a
2963 longer list or a structure member designator may not. */
2964 if (c_parser_next_token_is (parser, CPP_NAME)
2965 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
2967 /* Old-style structure member designator. */
2968 set_init_label (c_parser_peek_token (parser)->value);
2969 if (pedantic)
2970 pedwarn ("obsolete use of designated initializer with %<:%>");
2971 c_parser_consume_token (parser);
2972 c_parser_consume_token (parser);
2974 else
2976 /* des_seen is 0 if there have been no designators, 1 if there
2977 has been a single array designator and 2 otherwise. */
2978 int des_seen = 0;
2979 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
2980 || c_parser_next_token_is (parser, CPP_DOT))
2982 int des_prev = des_seen;
2983 if (des_seen < 2)
2984 des_seen++;
2985 if (c_parser_next_token_is (parser, CPP_DOT))
2987 des_seen = 2;
2988 c_parser_consume_token (parser);
2989 if (c_parser_next_token_is (parser, CPP_NAME))
2991 set_init_label (c_parser_peek_token (parser)->value);
2992 c_parser_consume_token (parser);
2994 else
2996 struct c_expr init;
2997 init.value = error_mark_node;
2998 init.original_code = ERROR_MARK;
2999 c_parser_error (parser, "expected identifier");
3000 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3001 process_init_element (init);
3002 return;
3005 else
3007 tree first, second;
3008 /* ??? Following the old parser, [ objc-receiver
3009 objc-message-args ] is accepted as an initializer,
3010 being distinguished from a designator by what follows
3011 the first assignment expression inside the square
3012 brackets, but after a first array designator a
3013 subsequent square bracket is for Objective-C taken to
3014 start an expression, using the obsolete form of
3015 designated initializer without '=', rather than
3016 possibly being a second level of designation: in LALR
3017 terms, the '[' is shifted rather than reducing
3018 designator to designator-list. */
3019 if (des_prev == 1 && c_dialect_objc ())
3021 des_seen = des_prev;
3022 break;
3024 if (des_prev == 0 && c_dialect_objc ())
3026 /* This might be an array designator or an
3027 Objective-C message expression. If the former,
3028 continue parsing here; if the latter, parse the
3029 remainder of the initializer given the starting
3030 primary-expression. ??? It might make sense to
3031 distinguish when des_prev == 1 as well; see
3032 previous comment. */
3033 tree rec, args;
3034 struct c_expr mexpr;
3035 c_parser_consume_token (parser);
3036 if (c_parser_peek_token (parser)->type == CPP_NAME
3037 && ((c_parser_peek_token (parser)->id_kind
3038 == C_ID_TYPENAME)
3039 || (c_parser_peek_token (parser)->id_kind
3040 == C_ID_CLASSNAME)))
3042 /* Type name receiver. */
3043 tree id = c_parser_peek_token (parser)->value;
3044 c_parser_consume_token (parser);
3045 rec = objc_get_class_reference (id);
3046 goto parse_message_args;
3048 first = c_parser_expr_no_commas (parser, NULL).value;
3049 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3050 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3051 goto array_desig_after_first;
3052 /* Expression receiver. So far only one part
3053 without commas has been parsed; there might be
3054 more of the expression. */
3055 rec = first;
3056 while (c_parser_next_token_is (parser, CPP_COMMA))
3058 struct c_expr next;
3059 c_parser_consume_token (parser);
3060 next = c_parser_expr_no_commas (parser, NULL);
3061 next = default_function_array_conversion (next);
3062 rec = build_compound_expr (rec, next.value);
3064 parse_message_args:
3065 /* Now parse the objc-message-args. */
3066 args = c_parser_objc_message_args (parser);
3067 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3068 "expected %<]%>");
3069 mexpr.value
3070 = objc_build_message_expr (build_tree_list (rec, args));
3071 mexpr.original_code = ERROR_MARK;
3072 /* Now parse and process the remainder of the
3073 initializer, starting with this message
3074 expression as a primary-expression. */
3075 c_parser_initval (parser, &mexpr);
3076 return;
3078 c_parser_consume_token (parser);
3079 first = c_parser_expr_no_commas (parser, NULL).value;
3080 array_desig_after_first:
3081 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3083 c_parser_consume_token (parser);
3084 second = c_parser_expr_no_commas (parser, NULL).value;
3086 else
3087 second = NULL_TREE;
3088 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3090 c_parser_consume_token (parser);
3091 set_init_index (first, second);
3092 if (pedantic && second)
3093 pedwarn ("ISO C forbids specifying range of "
3094 "elements to initialize");
3096 else
3097 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3098 "expected %<]%>");
3101 if (des_seen >= 1)
3103 if (c_parser_next_token_is (parser, CPP_EQ))
3105 if (pedantic && !flag_isoc99)
3106 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3107 c_parser_consume_token (parser);
3109 else
3111 if (des_seen == 1)
3113 if (pedantic)
3114 pedwarn ("obsolete use of designated initializer "
3115 "without %<=%>");
3117 else
3119 struct c_expr init;
3120 init.value = error_mark_node;
3121 init.original_code = ERROR_MARK;
3122 c_parser_error (parser, "expected %<=%>");
3123 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3124 process_init_element (init);
3125 return;
3130 c_parser_initval (parser, NULL);
3133 /* Parse a nested initializer; as c_parser_initializer but parses
3134 initializers within braced lists, after any designators have been
3135 applied. If AFTER is not NULL then it is an Objective-C message
3136 expression which is the primary-expression starting the
3137 initializer. */
3139 static void
3140 c_parser_initval (c_parser *parser, struct c_expr *after)
3142 struct c_expr init;
3143 gcc_assert (!after || c_dialect_objc ());
3144 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3145 init = c_parser_braced_init (parser, NULL_TREE, true);
3146 else
3148 init = c_parser_expr_no_commas (parser, after);
3149 if (init.value != NULL_TREE
3150 && TREE_CODE (init.value) != STRING_CST
3151 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3152 init = default_function_array_conversion (init);
3154 process_init_element (init);
3157 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3158 C99 6.8.2).
3160 compound-statement:
3161 { block-item-list[opt] }
3162 { label-declarations block-item-list }
3164 block-item-list:
3165 block-item
3166 block-item-list block-item
3168 block-item:
3169 nested-declaration
3170 statement
3172 nested-declaration:
3173 declaration
3175 GNU extensions:
3177 compound-statement:
3178 { label-declarations block-item-list }
3180 nested-declaration:
3181 __extension__ nested-declaration
3182 nested-function-definition
3184 label-declarations:
3185 label-declaration
3186 label-declarations label-declaration
3188 label-declaration:
3189 __label__ identifier-list ;
3191 Allowing the mixing of declarations and code is new in C99. The
3192 GNU syntax also permits (not shown above) labels at the end of
3193 compound statements, which yield an error. We don't allow labels
3194 on declarations; this might seem like a natural extension, but
3195 there would be a conflict between attributes on the label and
3196 prefix attributes on the declaration. ??? The syntax follows the
3197 old parser in requiring something after label declarations.
3198 Although they are erroneous if the labels declared aren't defined,
3199 is it useful for the syntax to be this way? */
3201 static tree
3202 c_parser_compound_statement (c_parser *parser)
3204 tree stmt;
3205 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3206 return error_mark_node;
3207 stmt = c_begin_compound_stmt (true);
3208 c_parser_compound_statement_nostart (parser);
3209 return c_end_compound_stmt (stmt, true);
3212 /* Parse a compound statement except for the opening brace. This is
3213 used for parsing both compound statements and statement expressions
3214 (which follow different paths to handling the opening). */
3216 static void
3217 c_parser_compound_statement_nostart (c_parser *parser)
3219 bool last_stmt = false;
3220 bool last_label = false;
3221 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3223 c_parser_consume_token (parser);
3224 return;
3226 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3228 /* Read zero or more forward-declarations for labels that nested
3229 functions can jump to. */
3230 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3232 c_parser_consume_token (parser);
3233 /* Any identifiers, including those declared as type names,
3234 are OK here. */
3235 while (true)
3237 tree label;
3238 if (c_parser_next_token_is_not (parser, CPP_NAME))
3240 c_parser_error (parser, "expected identifier");
3241 break;
3243 label
3244 = declare_label (c_parser_peek_token (parser)->value);
3245 C_DECLARED_LABEL_FLAG (label) = 1;
3246 add_stmt (build_stmt (DECL_EXPR, label));
3247 c_parser_consume_token (parser);
3248 if (c_parser_next_token_is (parser, CPP_COMMA))
3249 c_parser_consume_token (parser);
3250 else
3251 break;
3253 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3255 /* ??? Locating this diagnostic on the token after the
3256 declarations end follows the old parser, but it might be
3257 better to locate it where the declarations start instead. */
3258 if (pedantic)
3259 pedwarn ("ISO C forbids label declarations");
3261 /* We must now have at least one statement, label or declaration. */
3262 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3264 c_parser_error (parser, "expected declaration or statement");
3265 c_parser_consume_token (parser);
3266 return;
3268 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3270 location_t loc = c_parser_peek_token (parser)->location;
3271 if (c_parser_next_token_is (parser, CPP_EOF))
3273 c_parser_error (parser, "expected declaration or statement");
3274 return;
3276 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3277 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3278 || (c_parser_next_token_is (parser, CPP_NAME)
3279 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3281 last_label = true;
3282 last_stmt = false;
3283 c_parser_label (parser);
3285 else if (!last_label
3286 && c_parser_next_token_starts_declspecs (parser))
3288 last_label = false;
3289 c_parser_declaration_or_fndef (parser, true, true, true, true);
3290 if (last_stmt
3291 && ((pedantic && !flag_isoc99)
3292 || warn_declaration_after_statement))
3293 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3294 &loc);
3295 last_stmt = false;
3297 else if (!last_label
3298 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3300 /* __extension__ can start a declaration, but is also an
3301 unary operator that can start an expression. Consume all
3302 but the last of a possible series of __extension__ to
3303 determine which. */
3304 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3305 && (c_parser_peek_2nd_token (parser)->keyword
3306 == RID_EXTENSION))
3307 c_parser_consume_token (parser);
3308 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3310 int ext;
3311 ext = disable_extension_diagnostics ();
3312 c_parser_consume_token (parser);
3313 last_label = false;
3314 c_parser_declaration_or_fndef (parser, true, true, true, true);
3315 /* Following the old parser, __extension__ does not
3316 disable this diagnostic. */
3317 restore_extension_diagnostics (ext);
3318 if (last_stmt
3319 && ((pedantic && !flag_isoc99)
3320 || warn_declaration_after_statement))
3321 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3322 &loc);
3323 last_stmt = false;
3325 else
3326 goto statement;
3328 else
3330 statement:
3331 last_label = false;
3332 last_stmt = true;
3333 c_parser_statement_after_labels (parser);
3336 if (last_label)
3337 error ("label at end of compound statement");
3338 c_parser_consume_token (parser);
3341 /* Parse a label (C90 6.6.1, C99 6.8.1).
3343 label:
3344 identifier : attributes[opt]
3345 case constant-expression :
3346 default :
3348 GNU extensions:
3350 label:
3351 case constant-expression ... constant-expression :
3353 The use of attributes on labels is a GNU extension. The syntax in
3354 GNU C accepts any expressions without commas, non-constant
3355 expressions being rejected later. */
3357 static void
3358 c_parser_label (c_parser *parser)
3360 location_t loc1 = c_parser_peek_token (parser)->location;
3361 tree label = NULL_TREE;
3362 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3364 tree exp1, exp2;
3365 c_parser_consume_token (parser);
3366 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3367 if (c_parser_next_token_is (parser, CPP_COLON))
3369 c_parser_consume_token (parser);
3370 label = do_case (exp1, NULL_TREE);
3372 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3374 c_parser_consume_token (parser);
3375 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3376 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3377 label = do_case (exp1, exp2);
3379 else
3380 c_parser_error (parser, "expected %<:%> or %<...%>");
3382 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3384 c_parser_consume_token (parser);
3385 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3386 label = do_case (NULL_TREE, NULL_TREE);
3388 else
3390 tree name = c_parser_peek_token (parser)->value;
3391 tree tlab;
3392 location_t loc2;
3393 tree attrs;
3394 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3395 c_parser_consume_token (parser);
3396 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3397 loc2 = c_parser_peek_token (parser)->location;
3398 c_parser_consume_token (parser);
3399 attrs = c_parser_attributes (parser);
3400 tlab = define_label (loc2, name);
3401 if (tlab)
3403 decl_attributes (&tlab, attrs, 0);
3404 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3407 if (label)
3408 SET_EXPR_LOCATION (label, loc1);
3411 /* Parse a statement (C90 6.6, C99 6.8).
3413 statement:
3414 labeled-statement
3415 compound-statement
3416 expression-statement
3417 selection-statement
3418 iteration-statement
3419 jump-statement
3421 labeled-statement:
3422 label statement
3424 expression-statement:
3425 expression[opt] ;
3427 selection-statement:
3428 if-statement
3429 switch-statement
3431 iteration-statement:
3432 while-statement
3433 do-statement
3434 for-statement
3436 jump-statement:
3437 goto identifier ;
3438 continue ;
3439 break ;
3440 return expression[opt] ;
3442 GNU extensions:
3444 statement:
3445 asm-statement
3447 jump-statement:
3448 goto * expression ;
3450 Objective-C:
3452 statement:
3453 objc-throw-statement
3454 objc-try-catch-statement
3455 objc-synchronized-statement
3457 objc-throw-statement:
3458 @throw expression ;
3459 @throw ;
3462 static void
3463 c_parser_statement (c_parser *parser)
3465 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3466 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3467 || (c_parser_next_token_is (parser, CPP_NAME)
3468 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3469 c_parser_label (parser);
3470 c_parser_statement_after_labels (parser);
3473 /* Parse a statement, other than a labeled statement. */
3475 static void
3476 c_parser_statement_after_labels (c_parser *parser)
3478 location_t loc = c_parser_peek_token (parser)->location;
3479 tree stmt = NULL_TREE;
3480 switch (c_parser_peek_token (parser)->type)
3482 case CPP_OPEN_BRACE:
3483 add_stmt (c_parser_compound_statement (parser));
3484 break;
3485 case CPP_KEYWORD:
3486 switch (c_parser_peek_token (parser)->keyword)
3488 case RID_IF:
3489 c_parser_if_statement (parser);
3490 break;
3491 case RID_SWITCH:
3492 c_parser_switch_statement (parser);
3493 break;
3494 case RID_WHILE:
3495 c_parser_while_statement (parser);
3496 break;
3497 case RID_DO:
3498 c_parser_do_statement (parser);
3499 break;
3500 case RID_FOR:
3501 c_parser_for_statement (parser);
3502 break;
3503 case RID_GOTO:
3504 c_parser_consume_token (parser);
3505 if (c_parser_next_token_is (parser, CPP_NAME))
3507 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3508 c_parser_consume_token (parser);
3510 else if (c_parser_next_token_is (parser, CPP_MULT))
3512 c_parser_consume_token (parser);
3513 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3515 else
3516 c_parser_error (parser, "expected identifier or %<*%>");
3517 goto expect_semicolon;
3518 case RID_CONTINUE:
3519 c_parser_consume_token (parser);
3520 stmt = c_finish_bc_stmt (&c_cont_label, false);
3521 goto expect_semicolon;
3522 case RID_BREAK:
3523 c_parser_consume_token (parser);
3524 stmt = c_finish_bc_stmt (&c_break_label, true);
3525 goto expect_semicolon;
3526 case RID_RETURN:
3527 c_parser_consume_token (parser);
3528 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3530 stmt = c_finish_return (NULL_TREE);
3531 c_parser_consume_token (parser);
3533 else
3535 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3536 goto expect_semicolon;
3538 break;
3539 case RID_ASM:
3540 stmt = c_parser_asm_statement (parser);
3541 break;
3542 case RID_AT_THROW:
3543 gcc_assert (c_dialect_objc ());
3544 c_parser_consume_token (parser);
3545 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3547 stmt = objc_build_throw_stmt (NULL_TREE);
3548 c_parser_consume_token (parser);
3550 else
3552 stmt
3553 = objc_build_throw_stmt (c_parser_expression (parser).value);
3554 goto expect_semicolon;
3556 break;
3557 case RID_AT_TRY:
3558 gcc_assert (c_dialect_objc ());
3559 c_parser_objc_try_catch_statement (parser);
3560 break;
3561 case RID_AT_SYNCHRONIZED:
3562 gcc_assert (c_dialect_objc ());
3563 c_parser_objc_synchronized_statement (parser);
3564 break;
3565 default:
3566 goto expr_stmt;
3568 break;
3569 case CPP_SEMICOLON:
3570 c_parser_consume_token (parser);
3571 break;
3572 case CPP_CLOSE_PAREN:
3573 case CPP_CLOSE_SQUARE:
3574 /* Avoid infinite loop in error recovery:
3575 c_parser_skip_until_found stops at a closing nesting
3576 delimiter without consuming it, but here we need to consume
3577 it to proceed further. */
3578 c_parser_error (parser, "expected statement");
3579 c_parser_consume_token (parser);
3580 break;
3581 default:
3582 expr_stmt:
3583 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3584 expect_semicolon:
3585 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3586 break;
3588 /* Two cases cannot and do not have line numbers associated: If stmt
3589 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3590 cannot hold line numbers. But that's OK because the statement
3591 will either be changed to a MODIFY_EXPR during gimplification of
3592 the statement expr, or discarded. If stmt was compound, but
3593 without new variables, we will have skipped the creation of a
3594 BIND and will have a bare STATEMENT_LIST. But that's OK because
3595 (recursively) all of the component statements should already have
3596 line numbers assigned. ??? Can we discard no-op statements
3597 earlier? */
3598 if (stmt && EXPR_P (stmt))
3599 SET_EXPR_LOCATION (stmt, loc);
3602 /* Parse a parenthesized condition from an if, do or while statement.
3604 condition:
3605 ( expression )
3607 static tree
3608 c_parser_paren_condition (c_parser *parser)
3610 location_t loc;
3611 tree cond;
3612 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3613 return error_mark_node;
3614 loc = c_parser_peek_token (parser)->location;
3615 cond = c_objc_common_truthvalue_conversion
3616 (c_parser_expression_conv (parser).value);
3617 if (EXPR_P (cond))
3618 SET_EXPR_LOCATION (cond, loc);
3619 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3620 return cond;
3623 /* Parse a statement which is a block in C99. */
3625 static tree
3626 c_parser_c99_block_statement (c_parser *parser)
3628 tree block = c_begin_compound_stmt (flag_isoc99);
3629 c_parser_statement (parser);
3630 return c_end_compound_stmt (block, flag_isoc99);
3633 /* Parse the body of an if statement or the else half thereof. This
3634 is just parsing a statement but (a) it is a block in C99, (b) we
3635 track whether the body is an if statement for the sake of
3636 -Wparentheses warnings, (c) we handle an empty body specially for
3637 the sake of -Wextra warnings. */
3639 static tree
3640 c_parser_if_body (c_parser *parser, bool *if_p)
3642 tree block = c_begin_compound_stmt (flag_isoc99);
3643 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3644 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3645 || (c_parser_next_token_is (parser, CPP_NAME)
3646 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3647 c_parser_label (parser);
3648 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3649 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3650 add_stmt (build1 (NOP_EXPR, NULL_TREE, NULL_TREE));
3651 c_parser_statement_after_labels (parser);
3652 return c_end_compound_stmt (block, flag_isoc99);
3655 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3657 if-statement:
3658 if ( expression ) statement
3659 if ( expression ) statement else statement
3662 static void
3663 c_parser_if_statement (c_parser *parser)
3665 tree block;
3666 location_t loc;
3667 tree cond;
3668 bool first_if = false, second_if = false;
3669 tree first_body, second_body;
3670 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3671 c_parser_consume_token (parser);
3672 block = c_begin_compound_stmt (flag_isoc99);
3673 loc = c_parser_peek_token (parser)->location;
3674 cond = c_parser_paren_condition (parser);
3675 first_body = c_parser_if_body (parser, &first_if);
3676 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3678 c_parser_consume_token (parser);
3679 second_body = c_parser_if_body (parser, &second_if);
3681 else
3682 second_body = NULL_TREE;
3683 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3684 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3687 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3689 switch-statement:
3690 switch (expression) statement
3693 static void
3694 c_parser_switch_statement (c_parser *parser)
3696 tree block, expr, body, save_break;
3697 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3698 c_parser_consume_token (parser);
3699 block = c_begin_compound_stmt (flag_isoc99);
3700 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3702 expr = c_parser_expression (parser).value;
3703 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3705 else
3706 expr = error_mark_node;
3707 c_start_case (expr);
3708 save_break = c_break_label;
3709 c_break_label = NULL_TREE;
3710 body = c_parser_c99_block_statement (parser);
3711 c_finish_case (body);
3712 if (c_break_label)
3713 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3714 c_break_label = save_break;
3715 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3718 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3720 while-statement:
3721 while (expression) statement
3724 static void
3725 c_parser_while_statement (c_parser *parser)
3727 tree block, cond, body, save_break, save_cont;
3728 location_t loc;
3729 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3730 c_parser_consume_token (parser);
3731 block = c_begin_compound_stmt (flag_isoc99);
3732 loc = c_parser_peek_token (parser)->location;
3733 cond = c_parser_paren_condition (parser);
3734 save_break = c_break_label;
3735 c_break_label = NULL_TREE;
3736 save_cont = c_cont_label;
3737 c_cont_label = NULL_TREE;
3738 body = c_parser_c99_block_statement (parser);
3739 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3740 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3741 c_break_label = save_break;
3742 c_cont_label = save_cont;
3745 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3747 do-statement:
3748 do statement while ( expression ) ;
3751 static void
3752 c_parser_do_statement (c_parser *parser)
3754 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3755 location_t loc;
3756 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3757 c_parser_consume_token (parser);
3758 block = c_begin_compound_stmt (flag_isoc99);
3759 loc = c_parser_peek_token (parser)->location;
3760 save_break = c_break_label;
3761 c_break_label = NULL_TREE;
3762 save_cont = c_cont_label;
3763 c_cont_label = NULL_TREE;
3764 body = c_parser_c99_block_statement (parser);
3765 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3766 new_break = c_break_label;
3767 c_break_label = save_break;
3768 new_cont = c_cont_label;
3769 c_cont_label = save_cont;
3770 cond = c_parser_paren_condition (parser);
3771 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3772 c_parser_skip_to_end_of_block_or_statement (parser);
3773 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3774 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3777 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3779 for-statement:
3780 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3781 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3783 The form with a declaration is new in C99.
3785 ??? In accordance with the old parser, the declaration may be a
3786 nested function, which is then rejected in check_for_loop_decls,
3787 but does it make any sense for this to be included in the grammar?
3788 Note in particular that the nested function does not include a
3789 trailing ';', whereas the "declaration" production includes one.
3790 Also, can we reject bad declarations earlier and cheaper than
3791 check_for_loop_decls? */
3793 static void
3794 c_parser_for_statement (c_parser *parser)
3796 tree block, cond, incr, save_break, save_cont, body;
3797 location_t loc;
3798 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3799 loc = c_parser_peek_token (parser)->location;
3800 c_parser_consume_token (parser);
3801 block = c_begin_compound_stmt (flag_isoc99);
3802 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3804 /* Parse the initialization declaration or expression. */
3805 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3807 c_parser_consume_token (parser);
3808 c_finish_expr_stmt (NULL_TREE);
3810 else if (c_parser_next_token_starts_declspecs (parser))
3812 c_parser_declaration_or_fndef (parser, true, true, true, true);
3813 check_for_loop_decls ();
3815 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3817 /* __extension__ can start a declaration, but is also an
3818 unary operator that can start an expression. Consume all
3819 but the last of a possible series of __extension__ to
3820 determine which. */
3821 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3822 && (c_parser_peek_2nd_token (parser)->keyword
3823 == RID_EXTENSION))
3824 c_parser_consume_token (parser);
3825 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3827 int ext;
3828 ext = disable_extension_diagnostics ();
3829 c_parser_consume_token (parser);
3830 c_parser_declaration_or_fndef (parser, true, true, true, true);
3831 restore_extension_diagnostics (ext);
3832 check_for_loop_decls ();
3834 else
3835 goto init_expr;
3837 else
3839 init_expr:
3840 c_finish_expr_stmt (c_parser_expression (parser).value);
3841 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3843 /* Parse the loop condition. */
3844 loc = c_parser_peek_token (parser)->location;
3845 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3847 c_parser_consume_token (parser);
3848 cond = NULL_TREE;
3850 else
3852 tree ocond = c_parser_expression_conv (parser).value;
3853 cond = c_objc_common_truthvalue_conversion (ocond);
3854 if (EXPR_P (cond))
3855 SET_EXPR_LOCATION (cond, loc);
3856 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3858 /* Parse the increment expression. */
3859 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3860 incr = c_process_expr_stmt (NULL_TREE);
3861 else
3862 incr = c_process_expr_stmt (c_parser_expression (parser).value);
3863 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3865 else
3867 cond = error_mark_node;
3868 incr = error_mark_node;
3870 save_break = c_break_label;
3871 c_break_label = NULL_TREE;
3872 save_cont = c_cont_label;
3873 c_cont_label = NULL_TREE;
3874 body = c_parser_c99_block_statement (parser);
3875 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3876 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3877 c_break_label = save_break;
3878 c_cont_label = save_cont;
3881 /* Parse an asm statement, a GNU extension. This is a full-blown asm
3882 statement with inputs, outputs, clobbers, and volatile tag
3883 allowed.
3885 asm-statement:
3886 asm type-qualifier[opt] ( asm-argument ) ;
3888 asm-argument:
3889 asm-string-literal
3890 asm-string-literal : asm-operands[opt]
3891 asm-string-literal : asm-operands[opt] : asm-operands[opt]
3892 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3894 Qualifiers other than volatile are accepted in the syntax but
3895 warned for. */
3897 static tree
3898 c_parser_asm_statement (c_parser *parser)
3900 tree quals, str, outputs, inputs, clobbers, ret;
3901 bool simple;
3902 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3903 c_parser_consume_token (parser);
3904 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3906 quals = c_parser_peek_token (parser)->value;
3907 c_parser_consume_token (parser);
3909 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3910 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3912 warning (0, "%E qualifier ignored on asm",
3913 c_parser_peek_token (parser)->value);
3914 quals = NULL_TREE;
3915 c_parser_consume_token (parser);
3917 else
3918 quals = NULL_TREE;
3919 /* ??? Follow the C++ parser rather than using the
3920 c_lex_string_translate kludge. */
3921 c_lex_string_translate = 0;
3922 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3924 c_lex_string_translate = 1;
3925 return NULL_TREE;
3927 str = c_parser_asm_string_literal (parser);
3928 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3930 simple = true;
3931 outputs = NULL_TREE;
3932 inputs = NULL_TREE;
3933 clobbers = NULL_TREE;
3934 goto done_asm;
3936 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3938 c_lex_string_translate = 1;
3939 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3940 return NULL_TREE;
3942 simple = false;
3943 /* Parse outputs. */
3944 if (c_parser_next_token_is (parser, CPP_COLON)
3945 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3946 outputs = NULL_TREE;
3947 else
3948 outputs = c_parser_asm_operands (parser, false);
3949 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3951 inputs = NULL_TREE;
3952 clobbers = NULL_TREE;
3953 goto done_asm;
3955 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3957 c_lex_string_translate = 1;
3958 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3959 return NULL_TREE;
3961 /* Parse inputs. */
3962 if (c_parser_next_token_is (parser, CPP_COLON)
3963 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3964 inputs = NULL_TREE;
3965 else
3966 inputs = c_parser_asm_operands (parser, true);
3967 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3969 clobbers = NULL_TREE;
3970 goto done_asm;
3972 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3974 c_lex_string_translate = 1;
3975 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3976 return NULL_TREE;
3978 /* Parse clobbers. */
3979 clobbers = c_parser_asm_clobbers (parser);
3980 done_asm:
3981 c_lex_string_translate = 1;
3982 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3985 return NULL_TREE;
3987 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3988 c_parser_skip_to_end_of_block_or_statement (parser);
3989 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
3990 clobbers, simple));
3991 return ret;
3994 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
3995 not outputs), apply the default conversion of functions and arrays
3996 to pointers.
3998 asm-operands:
3999 asm-operand
4000 asm-operands , asm-operand
4002 asm-operand:
4003 asm-string-literal ( expression )
4004 [ identifier ] asm-string-literal ( expression )
4007 static tree
4008 c_parser_asm_operands (c_parser *parser, bool convert_p)
4010 tree list = NULL_TREE;
4011 while (true)
4013 tree name, str;
4014 struct c_expr expr;
4015 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4017 c_parser_consume_token (parser);
4018 if (c_parser_next_token_is (parser, CPP_NAME))
4020 tree id = c_parser_peek_token (parser)->value;
4021 c_parser_consume_token (parser);
4022 name = build_string (IDENTIFIER_LENGTH (id),
4023 IDENTIFIER_POINTER (id));
4025 else
4027 c_parser_error (parser, "expected identifier");
4028 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4029 return NULL_TREE;
4031 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4032 "expected %<]%>");
4034 else
4035 name = NULL_TREE;
4036 str = c_parser_asm_string_literal (parser);
4037 if (str == NULL_TREE)
4038 return NULL_TREE;
4039 c_lex_string_translate = 1;
4040 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4042 c_lex_string_translate = 0;
4043 return NULL_TREE;
4045 expr = c_parser_expression (parser);
4046 if (convert_p)
4047 expr = default_function_array_conversion (expr);
4048 c_lex_string_translate = 0;
4049 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4051 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4052 return NULL_TREE;
4054 list = chainon (list, build_tree_list (build_tree_list (name, str),
4055 expr.value));
4056 if (c_parser_next_token_is (parser, CPP_COMMA))
4057 c_parser_consume_token (parser);
4058 else
4059 break;
4061 return list;
4064 /* Parse asm clobbers, a GNU extension.
4066 asm-clobbers:
4067 asm-string-literal
4068 asm-clobbers , asm-string-literal
4071 static tree
4072 c_parser_asm_clobbers (c_parser *parser)
4074 tree list = NULL_TREE;
4075 while (true)
4077 tree str = c_parser_asm_string_literal (parser);
4078 if (str)
4079 list = tree_cons (NULL_TREE, str, list);
4080 else
4081 return NULL_TREE;
4082 if (c_parser_next_token_is (parser, CPP_COMMA))
4083 c_parser_consume_token (parser);
4084 else
4085 break;
4087 return list;
4090 /* Parse an expression other than a compound expression; that is, an
4091 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4092 NULL then it is an Objective-C message expression which is the
4093 primary-expression starting the expression as an initializer.
4095 assignment-expression:
4096 conditional-expression
4097 unary-expression assignment-operator assignment-expression
4099 assignment-operator: one of
4100 = *= /= %= += -= <<= >>= &= ^= |=
4102 In GNU C we accept any conditional expression on the LHS and
4103 diagnose the invalid lvalue rather than producing a syntax
4104 error. */
4106 static struct c_expr
4107 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4109 struct c_expr lhs, rhs, ret;
4110 enum tree_code code;
4111 gcc_assert (!after || c_dialect_objc ());
4112 lhs = c_parser_conditional_expression (parser, after);
4113 switch (c_parser_peek_token (parser)->type)
4115 case CPP_EQ:
4116 code = NOP_EXPR;
4117 break;
4118 case CPP_MULT_EQ:
4119 code = MULT_EXPR;
4120 break;
4121 case CPP_DIV_EQ:
4122 code = TRUNC_DIV_EXPR;
4123 break;
4124 case CPP_MOD_EQ:
4125 code = TRUNC_MOD_EXPR;
4126 break;
4127 case CPP_PLUS_EQ:
4128 code = PLUS_EXPR;
4129 break;
4130 case CPP_MINUS_EQ:
4131 code = MINUS_EXPR;
4132 break;
4133 case CPP_LSHIFT_EQ:
4134 code = LSHIFT_EXPR;
4135 break;
4136 case CPP_RSHIFT_EQ:
4137 code = RSHIFT_EXPR;
4138 break;
4139 case CPP_AND_EQ:
4140 code = BIT_AND_EXPR;
4141 break;
4142 case CPP_XOR_EQ:
4143 code = BIT_XOR_EXPR;
4144 break;
4145 case CPP_OR_EQ:
4146 code = BIT_IOR_EXPR;
4147 break;
4148 default:
4149 return lhs;
4151 c_parser_consume_token (parser);
4152 rhs = c_parser_expr_no_commas (parser, NULL);
4153 rhs = default_function_array_conversion (rhs);
4154 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4155 if (code == NOP_EXPR)
4156 ret.original_code = MODIFY_EXPR;
4157 else
4159 TREE_NO_WARNING (ret.value) = 1;
4160 ret.original_code = ERROR_MARK;
4162 return ret;
4165 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4166 is not NULL then it is an Objective-C message expression which is
4167 the primary-expression starting the expression as an initializer.
4169 conditional-expression:
4170 logical-OR-expression
4171 logical-OR-expression ? expression : conditional-expression
4173 GNU extensions:
4175 conditional-expression:
4176 logical-OR-expression ? : conditional-expression
4179 static struct c_expr
4180 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4182 struct c_expr cond, exp1, exp2, ret;
4183 gcc_assert (!after || c_dialect_objc ());
4184 cond = c_parser_binary_expression (parser, after);
4185 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4186 return cond;
4187 cond = default_function_array_conversion (cond);
4188 c_parser_consume_token (parser);
4189 if (c_parser_next_token_is (parser, CPP_COLON))
4191 if (pedantic)
4192 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4193 /* Make sure first operand is calculated only once. */
4194 exp1.value = save_expr (default_conversion (cond.value));
4195 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4196 skip_evaluation += cond.value == truthvalue_true_node;
4198 else
4200 cond.value
4201 = c_objc_common_truthvalue_conversion
4202 (default_conversion (cond.value));
4203 skip_evaluation += cond.value == truthvalue_false_node;
4204 exp1 = c_parser_expression_conv (parser);
4205 skip_evaluation += ((cond.value == truthvalue_true_node)
4206 - (cond.value == truthvalue_false_node));
4208 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4210 skip_evaluation -= cond.value == truthvalue_true_node;
4211 ret.value = error_mark_node;
4212 ret.original_code = ERROR_MARK;
4213 return ret;
4215 exp2 = c_parser_conditional_expression (parser, NULL);
4216 exp2 = default_function_array_conversion (exp2);
4217 skip_evaluation -= cond.value == truthvalue_true_node;
4218 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4219 ret.original_code = ERROR_MARK;
4220 return ret;
4223 /* Parse a binary expression; that is, a logical-OR-expression (C90
4224 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4225 an Objective-C message expression which is the primary-expression
4226 starting the expression as an initializer.
4228 multiplicative-expression:
4229 cast-expression
4230 multiplicative-expression * cast-expression
4231 multiplicative-expression / cast-expression
4232 multiplicative-expression % cast-expression
4234 additive-expression:
4235 multiplicative-expression
4236 additive-expression + multiplicative-expression
4237 additive-expression - multiplicative-expression
4239 shift-expression:
4240 additive-expression
4241 shift-expression << additive-expression
4242 shift-expression >> additive-expression
4244 relational-expression:
4245 shift-expression
4246 relational-expression < shift-expression
4247 relational-expression > shift-expression
4248 relational-expression <= shift-expression
4249 relational-expression >= shift-expression
4251 equality-expression:
4252 relational-expression
4253 equality-expression == relational-expression
4254 equality-expression != relational-expression
4256 AND-expression:
4257 equality-expression
4258 AND-expression & equality-expression
4260 exclusive-OR-expression:
4261 AND-expression
4262 exclusive-OR-expression ^ AND-expression
4264 inclusive-OR-expression:
4265 exclusive-OR-expression
4266 inclusive-OR-expression | exclusive-OR-expression
4268 logical-AND-expression:
4269 inclusive-OR-expression
4270 logical-AND-expression && inclusive-OR-expression
4272 logical-OR-expression:
4273 logical-AND-expression
4274 logical-OR-expression || logical-AND-expression
4277 static struct c_expr
4278 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4280 /* A binary expression is parsed using operator-precedence parsing,
4281 with the operands being cast expressions. All the binary
4282 operators are left-associative. Thus a binary expression is of
4283 form:
4285 E0 op1 E1 op2 E2 ...
4287 which we represent on a stack. On the stack, the precedence
4288 levels are strictly increasing. When a new operator is
4289 encountered of higher precedence than that at the top of the
4290 stack, it is pushed; its LHS is the top expression, and its RHS
4291 is everything parsed until it is popped. When a new operator is
4292 encountered with precedence less than or equal to that at the top
4293 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4294 by the result of the operation until the operator at the top of
4295 the stack has lower precedence than the new operator or there is
4296 only one element on the stack; then the top expression is the LHS
4297 of the new operator. In the case of logical AND and OR
4298 expressions, we also need to adjust skip_evaluation as
4299 appropriate when the operators are pushed and popped. */
4301 /* The precedence levels, where 0 is a dummy lowest level used for
4302 the bottom of the stack. */
4303 enum prec {
4304 PREC_NONE,
4305 PREC_LOGOR,
4306 PREC_LOGAND,
4307 PREC_BITOR,
4308 PREC_BITXOR,
4309 PREC_BITAND,
4310 PREC_EQ,
4311 PREC_REL,
4312 PREC_SHIFT,
4313 PREC_ADD,
4314 PREC_MULT,
4315 NUM_PRECS
4317 struct {
4318 /* The expression at this stack level. */
4319 struct c_expr expr;
4320 /* The precedence of the operator on its left, PREC_NONE at the
4321 bottom of the stack. */
4322 enum prec prec;
4323 /* The operation on its left. */
4324 enum tree_code op;
4325 } stack[NUM_PRECS];
4326 int sp;
4327 #define POP \
4328 do { \
4329 switch (stack[sp].op) \
4331 case TRUTH_ANDIF_EXPR: \
4332 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4333 break; \
4334 case TRUTH_ORIF_EXPR: \
4335 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4336 break; \
4337 default: \
4338 break; \
4340 stack[sp - 1].expr \
4341 = default_function_array_conversion (stack[sp - 1].expr); \
4342 stack[sp].expr \
4343 = default_function_array_conversion (stack[sp].expr); \
4344 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4345 stack[sp - 1].expr, \
4346 stack[sp].expr); \
4347 sp--; \
4348 } while (0)
4349 gcc_assert (!after || c_dialect_objc ());
4350 stack[0].expr = c_parser_cast_expression (parser, after);
4351 stack[0].prec = PREC_NONE;
4352 sp = 0;
4353 while (true)
4355 enum prec oprec;
4356 enum tree_code ocode;
4357 if (parser->error)
4358 goto out;
4359 switch (c_parser_peek_token (parser)->type)
4361 case CPP_MULT:
4362 oprec = PREC_MULT;
4363 ocode = MULT_EXPR;
4364 break;
4365 case CPP_DIV:
4366 oprec = PREC_MULT;
4367 ocode = TRUNC_DIV_EXPR;
4368 break;
4369 case CPP_MOD:
4370 oprec = PREC_MULT;
4371 ocode = TRUNC_MOD_EXPR;
4372 break;
4373 case CPP_PLUS:
4374 oprec = PREC_ADD;
4375 ocode = PLUS_EXPR;
4376 break;
4377 case CPP_MINUS:
4378 oprec = PREC_ADD;
4379 ocode = MINUS_EXPR;
4380 break;
4381 case CPP_LSHIFT:
4382 oprec = PREC_SHIFT;
4383 ocode = LSHIFT_EXPR;
4384 break;
4385 case CPP_RSHIFT:
4386 oprec = PREC_SHIFT;
4387 ocode = RSHIFT_EXPR;
4388 break;
4389 case CPP_LESS:
4390 oprec = PREC_REL;
4391 ocode = LT_EXPR;
4392 break;
4393 case CPP_GREATER:
4394 oprec = PREC_REL;
4395 ocode = GT_EXPR;
4396 break;
4397 case CPP_LESS_EQ:
4398 oprec = PREC_REL;
4399 ocode = LE_EXPR;
4400 break;
4401 case CPP_GREATER_EQ:
4402 oprec = PREC_REL;
4403 ocode = GE_EXPR;
4404 break;
4405 case CPP_EQ_EQ:
4406 oprec = PREC_EQ;
4407 ocode = EQ_EXPR;
4408 break;
4409 case CPP_NOT_EQ:
4410 oprec = PREC_EQ;
4411 ocode = NE_EXPR;
4412 break;
4413 case CPP_AND:
4414 oprec = PREC_BITAND;
4415 ocode = BIT_AND_EXPR;
4416 break;
4417 case CPP_XOR:
4418 oprec = PREC_BITXOR;
4419 ocode = BIT_XOR_EXPR;
4420 break;
4421 case CPP_OR:
4422 oprec = PREC_BITOR;
4423 ocode = BIT_IOR_EXPR;
4424 break;
4425 case CPP_AND_AND:
4426 oprec = PREC_LOGAND;
4427 ocode = TRUTH_ANDIF_EXPR;
4428 break;
4429 case CPP_OR_OR:
4430 oprec = PREC_LOGOR;
4431 ocode = TRUTH_ORIF_EXPR;
4432 break;
4433 default:
4434 /* Not a binary operator, so end of the binary
4435 expression. */
4436 goto out;
4438 c_parser_consume_token (parser);
4439 while (oprec <= stack[sp].prec)
4440 POP;
4441 switch (ocode)
4443 case TRUTH_ANDIF_EXPR:
4444 stack[sp].expr
4445 = default_function_array_conversion (stack[sp].expr);
4446 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4447 (default_conversion (stack[sp].expr.value));
4448 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4449 break;
4450 case TRUTH_ORIF_EXPR:
4451 stack[sp].expr
4452 = default_function_array_conversion (stack[sp].expr);
4453 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4454 (default_conversion (stack[sp].expr.value));
4455 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4456 break;
4457 default:
4458 break;
4460 sp++;
4461 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4462 stack[sp].prec = oprec;
4463 stack[sp].op = ocode;
4465 out:
4466 while (sp > 0)
4467 POP;
4468 return stack[0].expr;
4469 #undef POP
4472 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4473 NULL then it is an Objective-C message expression which is the
4474 primary-expression starting the expression as an initializer.
4476 cast-expression:
4477 unary-expression
4478 ( type-name ) unary-expression
4481 static struct c_expr
4482 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4484 gcc_assert (!after || c_dialect_objc ());
4485 if (after)
4486 return c_parser_postfix_expression_after_primary (parser, *after);
4487 /* If the expression begins with a parenthesized type name, it may
4488 be either a cast or a compound literal; we need to see whether
4489 the next character is '{' to tell the difference. If not, it is
4490 an unary expression. */
4491 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4492 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4494 struct c_type_name *type_name;
4495 struct c_expr ret;
4496 struct c_expr expr;
4497 c_parser_consume_token (parser);
4498 type_name = c_parser_type_name (parser);
4499 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4500 if (type_name == NULL)
4502 ret.value = error_mark_node;
4503 ret.original_code = ERROR_MARK;
4504 return ret;
4506 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4507 return c_parser_postfix_expression_after_paren_type (parser,
4508 type_name);
4509 expr = c_parser_cast_expression (parser, NULL);
4510 expr = default_function_array_conversion (expr);
4511 ret.value = c_cast_expr (type_name, expr.value);
4512 ret.original_code = ERROR_MARK;
4513 return ret;
4515 else
4516 return c_parser_unary_expression (parser);
4519 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4521 unary-expression:
4522 postfix-expression
4523 ++ unary-expression
4524 -- unary-expression
4525 unary-operator cast-expression
4526 sizeof unary-expression
4527 sizeof ( type-name )
4529 unary-operator: one of
4530 & * + - ~ !
4532 GNU extensions:
4534 unary-expression:
4535 __alignof__ unary-expression
4536 __alignof__ ( type-name )
4537 && identifier
4539 unary-operator: one of
4540 __extension__ __real__ __imag__
4542 In addition, the GNU syntax treats ++ and -- as unary operators, so
4543 they may be applied to cast expressions with errors for non-lvalues
4544 given later. */
4546 static struct c_expr
4547 c_parser_unary_expression (c_parser *parser)
4549 int ext;
4550 struct c_expr ret, op;
4551 switch (c_parser_peek_token (parser)->type)
4553 case CPP_PLUS_PLUS:
4554 c_parser_consume_token (parser);
4555 op = c_parser_cast_expression (parser, NULL);
4556 op = default_function_array_conversion (op);
4557 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4558 case CPP_MINUS_MINUS:
4559 c_parser_consume_token (parser);
4560 op = c_parser_cast_expression (parser, NULL);
4561 op = default_function_array_conversion (op);
4562 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4563 case CPP_AND:
4564 c_parser_consume_token (parser);
4565 return parser_build_unary_op (ADDR_EXPR,
4566 c_parser_cast_expression (parser, NULL));
4567 case CPP_MULT:
4568 c_parser_consume_token (parser);
4569 op = c_parser_cast_expression (parser, NULL);
4570 op = default_function_array_conversion (op);
4571 ret.value = build_indirect_ref (op.value, "unary *");
4572 ret.original_code = ERROR_MARK;
4573 return ret;
4574 case CPP_PLUS:
4575 c_parser_consume_token (parser);
4576 if (!c_dialect_objc () && !in_system_header)
4577 warning (OPT_Wtraditional,
4578 "traditional C rejects the unary plus operator");
4579 op = c_parser_cast_expression (parser, NULL);
4580 op = default_function_array_conversion (op);
4581 return parser_build_unary_op (CONVERT_EXPR, op);
4582 case CPP_MINUS:
4583 c_parser_consume_token (parser);
4584 op = c_parser_cast_expression (parser, NULL);
4585 op = default_function_array_conversion (op);
4586 return parser_build_unary_op (NEGATE_EXPR, op);
4587 case CPP_COMPL:
4588 c_parser_consume_token (parser);
4589 op = c_parser_cast_expression (parser, NULL);
4590 op = default_function_array_conversion (op);
4591 return parser_build_unary_op (BIT_NOT_EXPR, op);
4592 case CPP_NOT:
4593 c_parser_consume_token (parser);
4594 op = c_parser_cast_expression (parser, NULL);
4595 op = default_function_array_conversion (op);
4596 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4597 case CPP_AND_AND:
4598 /* Refer to the address of a label as a pointer. */
4599 c_parser_consume_token (parser);
4600 if (c_parser_next_token_is (parser, CPP_NAME))
4602 ret.value = finish_label_address_expr
4603 (c_parser_peek_token (parser)->value);
4604 c_parser_consume_token (parser);
4606 else
4608 c_parser_error (parser, "expected identifier");
4609 ret.value = error_mark_node;
4611 ret.original_code = ERROR_MARK;
4612 return ret;
4613 case CPP_KEYWORD:
4614 switch (c_parser_peek_token (parser)->keyword)
4616 case RID_SIZEOF:
4617 return c_parser_sizeof_expression (parser);
4618 case RID_ALIGNOF:
4619 return c_parser_alignof_expression (parser);
4620 case RID_EXTENSION:
4621 c_parser_consume_token (parser);
4622 ext = disable_extension_diagnostics ();
4623 ret = c_parser_cast_expression (parser, NULL);
4624 restore_extension_diagnostics (ext);
4625 return ret;
4626 case RID_REALPART:
4627 c_parser_consume_token (parser);
4628 op = c_parser_cast_expression (parser, NULL);
4629 op = default_function_array_conversion (op);
4630 return parser_build_unary_op (REALPART_EXPR, op);
4631 case RID_IMAGPART:
4632 c_parser_consume_token (parser);
4633 op = c_parser_cast_expression (parser, NULL);
4634 op = default_function_array_conversion (op);
4635 return parser_build_unary_op (IMAGPART_EXPR, op);
4636 default:
4637 return c_parser_postfix_expression (parser);
4639 default:
4640 return c_parser_postfix_expression (parser);
4644 /* Parse a sizeof expression. */
4646 static struct c_expr
4647 c_parser_sizeof_expression (c_parser *parser)
4649 struct c_expr expr;
4650 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4651 c_parser_consume_token (parser);
4652 skip_evaluation++;
4653 in_sizeof++;
4654 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4655 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4657 /* Either sizeof ( type-name ) or sizeof unary-expression
4658 starting with a compound literal. */
4659 struct c_type_name *type_name;
4660 c_parser_consume_token (parser);
4661 type_name = c_parser_type_name (parser);
4662 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4663 if (type_name == NULL)
4665 struct c_expr ret;
4666 skip_evaluation--;
4667 in_sizeof--;
4668 ret.value = error_mark_node;
4669 ret.original_code = ERROR_MARK;
4670 return ret;
4672 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4674 expr = c_parser_postfix_expression_after_paren_type (parser,
4675 type_name);
4676 goto sizeof_expr;
4678 /* sizeof ( type-name ). */
4679 skip_evaluation--;
4680 in_sizeof--;
4681 return c_expr_sizeof_type (type_name);
4683 else
4685 expr = c_parser_unary_expression (parser);
4686 sizeof_expr:
4687 skip_evaluation--;
4688 in_sizeof--;
4689 if (TREE_CODE (expr.value) == COMPONENT_REF
4690 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4691 error ("%<sizeof%> applied to a bit-field");
4692 return c_expr_sizeof_expr (expr);
4696 /* Parse an alignof expression. */
4698 static struct c_expr
4699 c_parser_alignof_expression (c_parser *parser)
4701 struct c_expr expr;
4702 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4703 c_parser_consume_token (parser);
4704 skip_evaluation++;
4705 in_alignof++;
4706 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4707 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4709 /* Either __alignof__ ( type-name ) or __alignof__
4710 unary-expression starting with a compound literal. */
4711 struct c_type_name *type_name;
4712 struct c_expr ret;
4713 c_parser_consume_token (parser);
4714 type_name = c_parser_type_name (parser);
4715 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4716 if (type_name == NULL)
4718 struct c_expr ret;
4719 skip_evaluation--;
4720 in_alignof--;
4721 ret.value = error_mark_node;
4722 ret.original_code = ERROR_MARK;
4723 return ret;
4725 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4727 expr = c_parser_postfix_expression_after_paren_type (parser,
4728 type_name);
4729 goto alignof_expr;
4731 /* alignof ( type-name ). */
4732 skip_evaluation--;
4733 in_alignof--;
4734 ret.value = c_alignof (groktypename (type_name));
4735 ret.original_code = ERROR_MARK;
4736 return ret;
4738 else
4740 struct c_expr ret;
4741 expr = c_parser_unary_expression (parser);
4742 alignof_expr:
4743 skip_evaluation--;
4744 in_alignof--;
4745 ret.value = c_alignof_expr (expr.value);
4746 ret.original_code = ERROR_MARK;
4747 return ret;
4751 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4753 postfix-expression:
4754 primary-expression
4755 postfix-expression [ expression ]
4756 postfix-expression ( argument-expression-list[opt] )
4757 postfix-expression . identifier
4758 postfix-expression -> identifier
4759 postfix-expression ++
4760 postfix-expression --
4761 ( type-name ) { initializer-list }
4762 ( type-name ) { initializer-list , }
4764 argument-expression-list:
4765 argument-expression
4766 argument-expression-list , argument-expression
4768 primary-expression:
4769 identifier
4770 constant
4771 string-literal
4772 ( expression )
4774 GNU extensions:
4776 primary-expression:
4777 __func__
4778 (treated as a keyword in GNU C)
4779 __FUNCTION__
4780 __PRETTY_FUNCTION__
4781 ( compound-statement )
4782 __builtin_va_arg ( assignment-expression , type-name )
4783 __builtin_offsetof ( type-name , offsetof-member-designator )
4784 __builtin_choose_expr ( assignment-expression ,
4785 assignment-expression ,
4786 assignment-expression )
4787 __builtin_types_compatible_p ( type-name , type-name )
4789 offsetof-member-designator:
4790 identifier
4791 offsetof-member-designator . identifier
4792 offsetof-member-designator [ expression ]
4794 Objective-C:
4796 primary-expression:
4797 [ objc-receiver objc-message-args ]
4798 @selector ( objc-selector-arg )
4799 @protocol ( identifier )
4800 @encode ( type-name )
4801 objc-string-literal
4804 static struct c_expr
4805 c_parser_postfix_expression (c_parser *parser)
4807 struct c_expr expr, e1, e2, e3;
4808 struct c_type_name *t1, *t2;
4809 switch (c_parser_peek_token (parser)->type)
4811 case CPP_NUMBER:
4812 case CPP_CHAR:
4813 case CPP_WCHAR:
4814 expr.value = c_parser_peek_token (parser)->value;
4815 expr.original_code = ERROR_MARK;
4816 c_parser_consume_token (parser);
4817 break;
4818 case CPP_STRING:
4819 case CPP_WSTRING:
4820 expr.value = c_parser_peek_token (parser)->value;
4821 expr.original_code = STRING_CST;
4822 c_parser_consume_token (parser);
4823 break;
4824 case CPP_OBJC_STRING:
4825 gcc_assert (c_dialect_objc ());
4826 expr.value
4827 = objc_build_string_object (c_parser_peek_token (parser)->value);
4828 expr.original_code = ERROR_MARK;
4829 c_parser_consume_token (parser);
4830 break;
4831 case CPP_NAME:
4832 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4834 c_parser_error (parser, "expected expression");
4835 expr.value = error_mark_node;
4836 expr.original_code = ERROR_MARK;
4837 break;
4840 tree id = c_parser_peek_token (parser)->value;
4841 location_t loc = c_parser_peek_token (parser)->location;
4842 c_parser_consume_token (parser);
4843 expr.value = build_external_ref (id,
4844 (c_parser_peek_token (parser)->type
4845 == CPP_OPEN_PAREN), loc);
4846 expr.original_code = ERROR_MARK;
4848 break;
4849 case CPP_OPEN_PAREN:
4850 /* A parenthesized expression, statement expression or compound
4851 literal. */
4852 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4854 /* A statement expression. */
4855 tree stmt;
4856 c_parser_consume_token (parser);
4857 c_parser_consume_token (parser);
4858 if (cur_stmt_list == NULL)
4860 error ("braced-group within expression allowed "
4861 "only inside a function");
4862 parser->error = true;
4863 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4864 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4865 expr.value = error_mark_node;
4866 expr.original_code = ERROR_MARK;
4867 break;
4869 stmt = c_begin_stmt_expr ();
4870 c_parser_compound_statement_nostart (parser);
4871 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4872 "expected %<)%>");
4873 if (pedantic)
4874 pedwarn ("ISO C forbids braced-groups within expressions");
4875 expr.value = c_finish_stmt_expr (stmt);
4876 expr.original_code = ERROR_MARK;
4878 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4880 /* A compound literal. ??? Can we actually get here rather
4881 than going directly to
4882 c_parser_postfix_expression_after_paren_type from
4883 elsewhere? */
4884 struct c_type_name *type_name;
4885 c_parser_consume_token (parser);
4886 type_name = c_parser_type_name (parser);
4887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4888 "expected %<)%>");
4889 if (type_name == NULL)
4891 expr.value = error_mark_node;
4892 expr.original_code = ERROR_MARK;
4894 else
4895 expr = c_parser_postfix_expression_after_paren_type (parser,
4896 type_name);
4898 else
4900 /* A parenthesized expression. */
4901 c_parser_consume_token (parser);
4902 expr = c_parser_expression (parser);
4903 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4904 TREE_NO_WARNING (expr.value) = 1;
4905 expr.original_code = ERROR_MARK;
4906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4907 "expected %<)%>");
4909 break;
4910 case CPP_KEYWORD:
4911 switch (c_parser_peek_token (parser)->keyword)
4913 case RID_FUNCTION_NAME:
4914 case RID_PRETTY_FUNCTION_NAME:
4915 case RID_C99_FUNCTION_NAME:
4916 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4917 c_parser_peek_token (parser)->value);
4918 expr.original_code = ERROR_MARK;
4919 c_parser_consume_token (parser);
4920 break;
4921 case RID_VA_ARG:
4922 c_parser_consume_token (parser);
4923 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4925 expr.value = error_mark_node;
4926 expr.original_code = ERROR_MARK;
4927 break;
4929 e1 = c_parser_expr_no_commas (parser, NULL);
4930 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4932 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4933 expr.value = error_mark_node;
4934 expr.original_code = ERROR_MARK;
4935 break;
4937 t1 = c_parser_type_name (parser);
4938 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4939 "expected %<)%>");
4940 if (t1 == NULL)
4942 expr.value = error_mark_node;
4943 expr.original_code = ERROR_MARK;
4945 else
4947 expr.value = build_va_arg (e1.value, groktypename (t1));
4948 expr.original_code = ERROR_MARK;
4950 break;
4951 case RID_OFFSETOF:
4952 c_parser_consume_token (parser);
4953 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4955 expr.value = error_mark_node;
4956 expr.original_code = ERROR_MARK;
4957 break;
4959 t1 = c_parser_type_name (parser);
4960 if (t1 == NULL)
4962 expr.value = error_mark_node;
4963 expr.original_code = ERROR_MARK;
4964 break;
4966 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4968 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4969 expr.value = error_mark_node;
4970 expr.original_code = ERROR_MARK;
4971 break;
4974 tree type = groktypename (t1);
4975 tree offsetof_ref;
4976 if (type == error_mark_node)
4977 offsetof_ref = error_mark_node;
4978 else
4979 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
4980 /* Parse the second argument to __builtin_offsetof. We
4981 must have one identifier, and beyond that we want to
4982 accept sub structure and sub array references. */
4983 if (c_parser_next_token_is (parser, CPP_NAME))
4985 offsetof_ref = build_component_ref
4986 (offsetof_ref, c_parser_peek_token (parser)->value);
4987 c_parser_consume_token (parser);
4988 while (c_parser_next_token_is (parser, CPP_DOT)
4989 || c_parser_next_token_is (parser,
4990 CPP_OPEN_SQUARE))
4992 if (c_parser_next_token_is (parser, CPP_DOT))
4994 c_parser_consume_token (parser);
4995 if (c_parser_next_token_is_not (parser,
4996 CPP_NAME))
4998 c_parser_error (parser, "expected identifier");
4999 break;
5001 offsetof_ref = build_component_ref
5002 (offsetof_ref,
5003 c_parser_peek_token (parser)->value);
5004 c_parser_consume_token (parser);
5006 else
5008 tree idx;
5009 c_parser_consume_token (parser);
5010 idx = c_parser_expression (parser).value;
5011 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5012 "expected %<]%>");
5013 offsetof_ref = build_array_ref (offsetof_ref, idx);
5017 else
5018 c_parser_error (parser, "expected identifier");
5019 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5020 "expected %<)%>");
5021 expr.value = fold_offsetof (offsetof_ref);
5022 expr.original_code = ERROR_MARK;
5024 break;
5025 case RID_CHOOSE_EXPR:
5026 c_parser_consume_token (parser);
5027 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5029 expr.value = error_mark_node;
5030 expr.original_code = ERROR_MARK;
5031 break;
5033 e1 = c_parser_expr_no_commas (parser, NULL);
5034 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5036 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5037 expr.value = error_mark_node;
5038 expr.original_code = ERROR_MARK;
5039 break;
5041 e2 = c_parser_expr_no_commas (parser, NULL);
5042 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5044 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5045 expr.value = error_mark_node;
5046 expr.original_code = ERROR_MARK;
5047 break;
5049 e3 = c_parser_expr_no_commas (parser, NULL);
5050 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5051 "expected %<)%>");
5053 tree c;
5055 c = fold (e1.value);
5056 if (TREE_CODE (c) != INTEGER_CST)
5057 error ("first argument to %<__builtin_choose_expr%> not"
5058 " a constant");
5059 expr = integer_zerop (c) ? e3 : e2;
5061 break;
5062 case RID_TYPES_COMPATIBLE_P:
5063 c_parser_consume_token (parser);
5064 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5066 expr.value = error_mark_node;
5067 expr.original_code = ERROR_MARK;
5068 break;
5070 t1 = c_parser_type_name (parser);
5071 if (t1 == NULL)
5073 expr.value = error_mark_node;
5074 expr.original_code = ERROR_MARK;
5075 break;
5077 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5079 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5080 expr.value = error_mark_node;
5081 expr.original_code = ERROR_MARK;
5082 break;
5084 t2 = c_parser_type_name (parser);
5085 if (t2 == NULL)
5087 expr.value = error_mark_node;
5088 expr.original_code = ERROR_MARK;
5089 break;
5091 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5092 "expected %<)%>");
5094 tree e1, e2;
5096 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5097 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5099 expr.value = comptypes (e1, e2)
5100 ? build_int_cst (NULL_TREE, 1)
5101 : build_int_cst (NULL_TREE, 0);
5102 expr.original_code = ERROR_MARK;
5104 break;
5105 case RID_AT_SELECTOR:
5106 gcc_assert (c_dialect_objc ());
5107 c_parser_consume_token (parser);
5108 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5110 expr.value = error_mark_node;
5111 expr.original_code = ERROR_MARK;
5112 break;
5115 tree sel = c_parser_objc_selector_arg (parser);
5116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5117 "expected %<)%>");
5118 expr.value = objc_build_selector_expr (sel);
5119 expr.original_code = ERROR_MARK;
5121 break;
5122 case RID_AT_PROTOCOL:
5123 gcc_assert (c_dialect_objc ());
5124 c_parser_consume_token (parser);
5125 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5127 expr.value = error_mark_node;
5128 expr.original_code = ERROR_MARK;
5129 break;
5131 if (c_parser_next_token_is_not (parser, CPP_NAME))
5133 c_parser_error (parser, "expected identifier");
5134 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5135 expr.value = error_mark_node;
5136 expr.original_code = ERROR_MARK;
5137 break;
5140 tree id = c_parser_peek_token (parser)->value;
5141 c_parser_consume_token (parser);
5142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5143 "expected %<)%>");
5144 expr.value = objc_build_protocol_expr (id);
5145 expr.original_code = ERROR_MARK;
5147 break;
5148 case RID_AT_ENCODE:
5149 /* Extension to support C-structures in the archiver. */
5150 gcc_assert (c_dialect_objc ());
5151 c_parser_consume_token (parser);
5152 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5154 expr.value = error_mark_node;
5155 expr.original_code = ERROR_MARK;
5156 break;
5158 t1 = c_parser_type_name (parser);
5159 if (t1 == NULL)
5161 expr.value = error_mark_node;
5162 expr.original_code = ERROR_MARK;
5163 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5164 break;
5166 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5167 "expected %<)%>");
5169 tree type = groktypename (t1);
5170 expr.value = objc_build_encode_expr (type);
5171 expr.original_code = ERROR_MARK;
5173 break;
5174 default:
5175 c_parser_error (parser, "expected expression");
5176 expr.value = error_mark_node;
5177 expr.original_code = ERROR_MARK;
5178 break;
5180 break;
5181 case CPP_OPEN_SQUARE:
5182 if (c_dialect_objc ())
5184 tree receiver, args;
5185 c_parser_consume_token (parser);
5186 receiver = c_parser_objc_receiver (parser);
5187 args = c_parser_objc_message_args (parser);
5188 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5189 "expected %<]%>");
5190 expr.value = objc_build_message_expr (build_tree_list (receiver,
5191 args));
5192 expr.original_code = ERROR_MARK;
5193 break;
5195 /* Else fall through to report error. */
5196 default:
5197 c_parser_error (parser, "expected expression");
5198 expr.value = error_mark_node;
5199 expr.original_code = ERROR_MARK;
5200 break;
5202 return c_parser_postfix_expression_after_primary (parser, expr);
5205 /* Parse a postfix expression after a parenthesized type name: the
5206 brace-enclosed initializer of a compound literal, possibly followed
5207 by some postfix operators. This is separate because it is not
5208 possible to tell until after the type name whether a cast
5209 expression has a cast or a compound literal, or whether the operand
5210 of sizeof is a parenthesized type name or starts with a compound
5211 literal. */
5213 static struct c_expr
5214 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5215 struct c_type_name *type_name)
5217 tree type;
5218 struct c_expr init;
5219 struct c_expr expr;
5220 start_init (NULL_TREE, NULL, 0);
5221 type = groktypename (type_name);
5222 if (C_TYPE_VARIABLE_SIZE (type))
5224 error ("compound literal has variable size");
5225 type = error_mark_node;
5227 init = c_parser_braced_init (parser, type, false);
5228 finish_init ();
5229 maybe_warn_string_init (type, init);
5231 if (pedantic && !flag_isoc99)
5232 pedwarn ("ISO C90 forbids compound literals");
5233 expr.value = build_compound_literal (type, init.value);
5234 expr.original_code = ERROR_MARK;
5235 return c_parser_postfix_expression_after_primary (parser, expr);
5238 /* Parse a postfix expression after the initial primary or compound
5239 literal; that is, parse a series of postfix operators. */
5241 static struct c_expr
5242 c_parser_postfix_expression_after_primary (c_parser *parser,
5243 struct c_expr expr)
5245 tree ident, idx, exprlist;
5246 while (true)
5248 switch (c_parser_peek_token (parser)->type)
5250 case CPP_OPEN_SQUARE:
5251 /* Array reference. */
5252 c_parser_consume_token (parser);
5253 idx = c_parser_expression (parser).value;
5254 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5255 "expected %<]%>");
5256 expr.value = build_array_ref (expr.value, idx);
5257 expr.original_code = ERROR_MARK;
5258 break;
5259 case CPP_OPEN_PAREN:
5260 /* Function call. */
5261 c_parser_consume_token (parser);
5262 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5263 exprlist = NULL_TREE;
5264 else
5265 exprlist = c_parser_expr_list (parser, true);
5266 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5267 "expected %<)%>");
5268 expr.value = build_function_call (expr.value, exprlist);
5269 expr.original_code = ERROR_MARK;
5270 break;
5271 case CPP_DOT:
5272 /* Structure element reference. */
5273 c_parser_consume_token (parser);
5274 expr = default_function_array_conversion (expr);
5275 if (c_parser_next_token_is (parser, CPP_NAME))
5276 ident = c_parser_peek_token (parser)->value;
5277 else
5279 c_parser_error (parser, "expected identifier");
5280 expr.value = error_mark_node;
5281 expr.original_code = ERROR_MARK;
5282 return expr;
5284 c_parser_consume_token (parser);
5285 expr.value = build_component_ref (expr.value, ident);
5286 expr.original_code = ERROR_MARK;
5287 break;
5288 case CPP_DEREF:
5289 /* Structure element reference. */
5290 c_parser_consume_token (parser);
5291 expr = default_function_array_conversion (expr);
5292 if (c_parser_next_token_is (parser, CPP_NAME))
5293 ident = c_parser_peek_token (parser)->value;
5294 else
5296 c_parser_error (parser, "expected identifier");
5297 expr.value = error_mark_node;
5298 expr.original_code = ERROR_MARK;
5299 return expr;
5301 c_parser_consume_token (parser);
5302 expr.value = build_component_ref (build_indirect_ref (expr.value,
5303 "->"), ident);
5304 expr.original_code = ERROR_MARK;
5305 break;
5306 case CPP_PLUS_PLUS:
5307 /* Postincrement. */
5308 c_parser_consume_token (parser);
5309 expr = default_function_array_conversion (expr);
5310 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5311 expr.original_code = ERROR_MARK;
5312 break;
5313 case CPP_MINUS_MINUS:
5314 /* Postdecrement. */
5315 c_parser_consume_token (parser);
5316 expr = default_function_array_conversion (expr);
5317 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5318 expr.original_code = ERROR_MARK;
5319 break;
5320 default:
5321 return expr;
5326 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5328 expression:
5329 assignment-expression
5330 expression , assignment-expression
5333 static struct c_expr
5334 c_parser_expression (c_parser *parser)
5336 struct c_expr expr;
5337 expr = c_parser_expr_no_commas (parser, NULL);
5338 while (c_parser_next_token_is (parser, CPP_COMMA))
5340 struct c_expr next;
5341 c_parser_consume_token (parser);
5342 next = c_parser_expr_no_commas (parser, NULL);
5343 next = default_function_array_conversion (next);
5344 expr.value = build_compound_expr (expr.value, next.value);
5345 expr.original_code = COMPOUND_EXPR;
5347 return expr;
5350 /* Parse an expression and convert functions or arrays to
5351 pointers. */
5353 static struct c_expr
5354 c_parser_expression_conv (c_parser *parser)
5356 struct c_expr expr;
5357 expr = c_parser_expression (parser);
5358 expr = default_function_array_conversion (expr);
5359 return expr;
5362 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5363 functions and arrays to pointers.
5365 nonempty-expr-list:
5366 assignment-expression
5367 nonempty-expr-list , assignment-expression
5370 static tree
5371 c_parser_expr_list (c_parser *parser, bool convert_p)
5373 struct c_expr expr;
5374 tree ret, cur;
5375 expr = c_parser_expr_no_commas (parser, NULL);
5376 if (convert_p)
5377 expr = default_function_array_conversion (expr);
5378 ret = cur = build_tree_list (NULL_TREE, expr.value);
5379 while (c_parser_next_token_is (parser, CPP_COMMA))
5381 c_parser_consume_token (parser);
5382 expr = c_parser_expr_no_commas (parser, NULL);
5383 if (convert_p)
5384 expr = default_function_array_conversion (expr);
5385 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5387 return ret;
5391 /* Parse Objective-C-specific constructs. */
5393 /* Parse an objc-class-definition.
5395 objc-class-definition:
5396 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5397 objc-class-instance-variables[opt] objc-methodprotolist @end
5398 @implementation identifier objc-superclass[opt]
5399 objc-class-instance-variables[opt]
5400 @interface identifier ( identifier ) objc-protocol-refs[opt]
5401 objc-methodprotolist @end
5402 @implementation identifier ( identifier )
5404 objc-superclass:
5405 : identifier
5407 "@interface identifier (" must start "@interface identifier (
5408 identifier ) ...": objc-methodprotolist in the first production may
5409 not start with a parenthesized identifier as a declarator of a data
5410 definition with no declaration specifiers if the objc-superclass,
5411 objc-protocol-refs and objc-class-instance-variables are omitted. */
5413 static void
5414 c_parser_objc_class_definition (c_parser *parser)
5416 bool iface_p;
5417 tree id1;
5418 tree superclass;
5419 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5420 iface_p = true;
5421 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5422 iface_p = false;
5423 else
5424 gcc_unreachable ();
5425 c_parser_consume_token (parser);
5426 if (c_parser_next_token_is_not (parser, CPP_NAME))
5428 c_parser_error (parser, "expected identifier");
5429 return;
5431 id1 = c_parser_peek_token (parser)->value;
5432 c_parser_consume_token (parser);
5433 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5435 tree id2;
5436 tree proto = NULL_TREE;
5437 c_parser_consume_token (parser);
5438 if (c_parser_next_token_is_not (parser, CPP_NAME))
5440 c_parser_error (parser, "expected identifier");
5441 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5442 return;
5444 id2 = c_parser_peek_token (parser)->value;
5445 c_parser_consume_token (parser);
5446 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5447 if (!iface_p)
5449 objc_start_category_implementation (id1, id2);
5450 return;
5452 if (c_parser_next_token_is (parser, CPP_LESS))
5453 proto = c_parser_objc_protocol_refs (parser);
5454 objc_start_category_interface (id1, id2, proto);
5455 c_parser_objc_methodprotolist (parser);
5456 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5457 objc_finish_interface ();
5458 return;
5460 if (c_parser_next_token_is (parser, CPP_COLON))
5462 c_parser_consume_token (parser);
5463 if (c_parser_next_token_is_not (parser, CPP_NAME))
5465 c_parser_error (parser, "expected identifier");
5466 return;
5468 superclass = c_parser_peek_token (parser)->value;
5469 c_parser_consume_token (parser);
5471 else
5472 superclass = NULL_TREE;
5473 if (iface_p)
5475 tree proto = NULL_TREE;
5476 if (c_parser_next_token_is (parser, CPP_LESS))
5477 proto = c_parser_objc_protocol_refs (parser);
5478 objc_start_class_interface (id1, superclass, proto);
5480 else
5481 objc_start_class_implementation (id1, superclass);
5482 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5483 c_parser_objc_class_instance_variables (parser);
5484 if (iface_p)
5486 objc_continue_interface ();
5487 c_parser_objc_methodprotolist (parser);
5488 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5489 objc_finish_interface ();
5491 else
5493 objc_continue_implementation ();
5494 return;
5498 /* Parse objc-class-instance-variables.
5500 objc-class-instance-variables:
5501 { objc-instance-variable-decl-list[opt] }
5503 objc-instance-variable-decl-list:
5504 objc-visibility-spec
5505 objc-instance-variable-decl ;
5507 objc-instance-variable-decl-list objc-visibility-spec
5508 objc-instance-variable-decl-list objc-instance-variable-decl ;
5509 objc-instance-variable-decl-list ;
5511 objc-visibility-spec:
5512 @private
5513 @protected
5514 @public
5516 objc-instance-variable-decl:
5517 struct-declaration
5520 static void
5521 c_parser_objc_class_instance_variables (c_parser *parser)
5523 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5524 c_parser_consume_token (parser);
5525 while (c_parser_next_token_is_not (parser, CPP_EOF))
5527 tree decls;
5528 /* Parse any stray semicolon. */
5529 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5531 if (pedantic)
5532 pedwarn ("extra semicolon in struct or union specified");
5533 c_parser_consume_token (parser);
5534 continue;
5536 /* Stop if at the end of the instance variables. */
5537 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5539 c_parser_consume_token (parser);
5540 break;
5542 /* Parse any objc-visibility-spec. */
5543 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5545 c_parser_consume_token (parser);
5546 objc_set_visibility (2);
5547 continue;
5549 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5551 c_parser_consume_token (parser);
5552 objc_set_visibility (0);
5553 continue;
5555 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5557 c_parser_consume_token (parser);
5558 objc_set_visibility (1);
5559 continue;
5561 /* Parse some comma-separated declarations. */
5562 decls = c_parser_struct_declaration (parser);
5564 /* Comma-separated instance variables are chained together in
5565 reverse order; add them one by one. */
5566 tree ivar = nreverse (decls);
5567 for (; ivar; ivar = TREE_CHAIN (ivar))
5568 objc_add_instance_variable (copy_node (ivar));
5570 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5574 /* Parse an objc-class-declaration.
5576 objc-class-declaration:
5577 @class identifier-list ;
5580 static void
5581 c_parser_objc_class_declaration (c_parser *parser)
5583 tree list = NULL_TREE;
5584 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5585 c_parser_consume_token (parser);
5586 /* Any identifiers, including those declared as type names, are OK
5587 here. */
5588 while (true)
5590 tree id;
5591 if (c_parser_next_token_is_not (parser, CPP_NAME))
5593 c_parser_error (parser, "expected identifier");
5594 break;
5596 id = c_parser_peek_token (parser)->value;
5597 list = chainon (list, build_tree_list (NULL_TREE, id));
5598 c_parser_consume_token (parser);
5599 if (c_parser_next_token_is (parser, CPP_COMMA))
5600 c_parser_consume_token (parser);
5601 else
5602 break;
5604 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5605 objc_declare_class (list);
5608 /* Parse an objc-alias-declaration.
5610 objc-alias-declaration:
5611 @compatibility_alias identifier identifier ;
5614 static void
5615 c_parser_objc_alias_declaration (c_parser *parser)
5617 tree id1, id2;
5618 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5619 c_parser_consume_token (parser);
5620 if (c_parser_next_token_is_not (parser, CPP_NAME))
5622 c_parser_error (parser, "expected identifier");
5623 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5624 return;
5626 id1 = c_parser_peek_token (parser)->value;
5627 c_parser_consume_token (parser);
5628 if (c_parser_next_token_is_not (parser, CPP_NAME))
5630 c_parser_error (parser, "expected identifier");
5631 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5632 return;
5634 id2 = c_parser_peek_token (parser)->value;
5635 c_parser_consume_token (parser);
5636 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5637 objc_declare_alias (id1, id2);
5640 /* Parse an objc-protocol-definition.
5642 objc-protocol-definition:
5643 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5644 @protocol identifier-list ;
5646 "@protocol identifier ;" should be resolved as "@protocol
5647 identifier-list ;": objc-methodprotolist may not start with a
5648 semicolon in the first alternative if objc-protocol-refs are
5649 omitted. */
5651 static void
5652 c_parser_objc_protocol_definition (c_parser *parser)
5654 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5655 c_parser_consume_token (parser);
5656 if (c_parser_next_token_is_not (parser, CPP_NAME))
5658 c_parser_error (parser, "expected identifier");
5659 return;
5661 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5662 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5664 tree list = NULL_TREE;
5665 /* Any identifiers, including those declared as type names, are
5666 OK here. */
5667 while (true)
5669 tree id;
5670 if (c_parser_next_token_is_not (parser, CPP_NAME))
5672 c_parser_error (parser, "expected identifier");
5673 break;
5675 id = c_parser_peek_token (parser)->value;
5676 list = chainon (list, build_tree_list (NULL_TREE, id));
5677 c_parser_consume_token (parser);
5678 if (c_parser_next_token_is (parser, CPP_COMMA))
5679 c_parser_consume_token (parser);
5680 else
5681 break;
5683 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5684 objc_declare_protocols (list);
5686 else
5688 tree id = c_parser_peek_token (parser)->value;
5689 tree proto = NULL_TREE;
5690 c_parser_consume_token (parser);
5691 if (c_parser_next_token_is (parser, CPP_LESS))
5692 proto = c_parser_objc_protocol_refs (parser);
5693 objc_pq_context = 1;
5694 objc_start_protocol (id, proto);
5695 c_parser_objc_methodprotolist (parser);
5696 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5697 objc_pq_context = 0;
5698 objc_finish_interface ();
5702 /* Parse an objc-method-type.
5704 objc-method-type:
5709 static enum tree_code
5710 c_parser_objc_method_type (c_parser *parser)
5712 switch (c_parser_peek_token (parser)->type)
5714 case CPP_PLUS:
5715 c_parser_consume_token (parser);
5716 return PLUS_EXPR;
5717 case CPP_MINUS:
5718 c_parser_consume_token (parser);
5719 return MINUS_EXPR;
5720 default:
5721 gcc_unreachable ();
5725 /* Parse an objc-method-definition.
5727 objc-method-definition:
5728 objc-method-type objc-method-decl ;[opt] compound-statement
5731 static void
5732 c_parser_objc_method_definition (c_parser *parser)
5734 enum tree_code type = c_parser_objc_method_type (parser);
5735 tree decl;
5736 objc_set_method_type (type);
5737 objc_pq_context = 1;
5738 decl = c_parser_objc_method_decl (parser);
5739 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5741 c_parser_consume_token (parser);
5742 if (pedantic)
5743 pedwarn ("extra semicolon in method definition specified");
5745 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5747 c_parser_error (parser, "expected %<{%>");
5748 return;
5750 objc_pq_context = 0;
5751 objc_start_method_definition (decl);
5752 add_stmt (c_parser_compound_statement (parser));
5753 objc_finish_method_definition (current_function_decl);
5756 /* Parse an objc-methodprotolist.
5758 objc-methodprotolist:
5759 empty
5760 objc-methodprotolist objc-methodproto
5761 objc-methodprotolist declaration
5762 objc-methodprotolist ;
5764 The declaration is a data definition, which may be missing
5765 declaration specifiers under the same rules and diagnostics as
5766 other data definitions outside functions, and the stray semicolon
5767 is diagnosed the same way as a stray semicolon outside a
5768 function. */
5770 static void
5771 c_parser_objc_methodprotolist (c_parser *parser)
5773 while (true)
5775 /* The list is terminated by @end. */
5776 switch (c_parser_peek_token (parser)->type)
5778 case CPP_SEMICOLON:
5779 if (pedantic)
5780 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5781 c_parser_consume_token (parser);
5782 break;
5783 case CPP_PLUS:
5784 case CPP_MINUS:
5785 c_parser_objc_methodproto (parser);
5786 break;
5787 case CPP_EOF:
5788 return;
5789 default:
5790 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5791 return;
5792 c_parser_declaration_or_fndef (parser, false, true, false, true);
5793 break;
5798 /* Parse an objc-methodproto.
5800 objc-methodproto:
5801 objc-method-type objc-method-decl ;
5804 static void
5805 c_parser_objc_methodproto (c_parser *parser)
5807 enum tree_code type = c_parser_objc_method_type (parser);
5808 tree decl;
5809 objc_set_method_type (type);
5810 /* Remember protocol qualifiers in prototypes. */
5811 objc_pq_context = 1;
5812 decl = c_parser_objc_method_decl (parser);
5813 /* Forget protocol qualifiers here. */
5814 objc_pq_context = 0;
5815 objc_add_method_declaration (decl);
5816 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5819 /* Parse an objc-method-decl.
5821 objc-method-decl:
5822 ( objc-type-name ) objc-selector
5823 objc-selector
5824 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5825 objc-keyword-selector objc-optparmlist
5827 objc-keyword-selector:
5828 objc-keyword-decl
5829 objc-keyword-selector objc-keyword-decl
5831 objc-keyword-decl:
5832 objc-selector : ( objc-type-name ) identifier
5833 objc-selector : identifier
5834 : ( objc-type-name ) identifier
5835 : identifier
5837 objc-optparmlist:
5838 objc-optparms objc-optellipsis
5840 objc-optparms:
5841 empty
5842 objc-opt-parms , parameter-declaration
5844 objc-optellipsis:
5845 empty
5846 , ...
5849 static tree
5850 c_parser_objc_method_decl (c_parser *parser)
5852 tree type = NULL_TREE;
5853 tree sel;
5854 tree parms = NULL_TREE;
5855 bool ellipsis = false;
5857 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5859 c_parser_consume_token (parser);
5860 type = c_parser_objc_type_name (parser);
5861 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5863 sel = c_parser_objc_selector (parser);
5864 /* If there is no selector, or a colon follows, we have an
5865 objc-keyword-selector. If there is a selector, and a colon does
5866 not follow, that selector ends the objc-method-decl. */
5867 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5869 tree tsel = sel;
5870 tree list = NULL_TREE;
5871 while (true)
5873 tree atype = NULL_TREE, id, keyworddecl;
5874 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5875 break;
5876 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5878 c_parser_consume_token (parser);
5879 atype = c_parser_objc_type_name (parser);
5880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5881 "expected %<)%>");
5883 if (c_parser_next_token_is_not (parser, CPP_NAME))
5885 c_parser_error (parser, "expected identifier");
5886 return error_mark_node;
5888 id = c_parser_peek_token (parser)->value;
5889 c_parser_consume_token (parser);
5890 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5891 list = chainon (list, keyworddecl);
5892 tsel = c_parser_objc_selector (parser);
5893 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5894 break;
5896 /* Parse the optional parameter list. Optional Objective-C
5897 method parameters follow the C syntax, and may include '...'
5898 to denote a variable number of arguments. */
5899 parms = make_node (TREE_LIST);
5900 while (c_parser_next_token_is (parser, CPP_COMMA))
5902 struct c_parm *parm;
5903 c_parser_consume_token (parser);
5904 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5906 ellipsis = true;
5907 c_parser_consume_token (parser);
5908 break;
5910 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5911 if (parm == NULL)
5912 break;
5913 parms = chainon (parms,
5914 build_tree_list (NULL_TREE, grokparm (parm)));
5916 sel = list;
5918 return objc_build_method_signature (type, sel, parms, ellipsis);
5921 /* Parse an objc-type-name.
5923 objc-type-name:
5924 objc-type-qualifiers[opt] type-name
5925 objc-type-qualifiers[opt]
5927 objc-type-qualifiers:
5928 objc-type-qualifier
5929 objc-type-qualifiers objc-type-qualifier
5931 objc-type-qualifier: one of
5932 in out inout bycopy byref oneway
5935 static tree
5936 c_parser_objc_type_name (c_parser *parser)
5938 tree quals = NULL_TREE;
5939 struct c_type_name *typename = NULL;
5940 tree type = NULL_TREE;
5941 while (true)
5943 c_token *token = c_parser_peek_token (parser);
5944 if (token->type == CPP_KEYWORD
5945 && (token->keyword == RID_IN
5946 || token->keyword == RID_OUT
5947 || token->keyword == RID_INOUT
5948 || token->keyword == RID_BYCOPY
5949 || token->keyword == RID_BYREF
5950 || token->keyword == RID_ONEWAY))
5952 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
5953 c_parser_consume_token (parser);
5955 else
5956 break;
5958 if (c_parser_next_token_starts_typename (parser))
5959 typename = c_parser_type_name (parser);
5960 if (typename)
5961 type = groktypename (typename);
5962 return build_tree_list (quals, type);
5965 /* Parse objc-protocol-refs.
5967 objc-protocol-refs:
5968 < identifier-list >
5971 static tree
5972 c_parser_objc_protocol_refs (c_parser *parser)
5974 tree list = NULL_TREE;
5975 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
5976 c_parser_consume_token (parser);
5977 /* Any identifiers, including those declared as type names, are OK
5978 here. */
5979 while (true)
5981 tree id;
5982 if (c_parser_next_token_is_not (parser, CPP_NAME))
5984 c_parser_error (parser, "expected identifier");
5985 break;
5987 id = c_parser_peek_token (parser)->value;
5988 list = chainon (list, build_tree_list (NULL_TREE, id));
5989 c_parser_consume_token (parser);
5990 if (c_parser_next_token_is (parser, CPP_COMMA))
5991 c_parser_consume_token (parser);
5992 else
5993 break;
5995 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
5996 return list;
5999 /* Parse an objc-try-catch-statement.
6001 objc-try-catch-statement:
6002 @try compound-statement objc-catch-list[opt]
6003 @try compound-statement objc-catch-list[opt] @finally compound-statement
6005 objc-catch-list:
6006 @catch ( parameter-declaration ) compound-statement
6007 objc-catch-list @catch ( parameter-declaration ) compound-statement
6010 static void
6011 c_parser_objc_try_catch_statement (c_parser *parser)
6013 location_t loc;
6014 tree stmt;
6015 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
6016 c_parser_consume_token (parser);
6017 loc = c_parser_peek_token (parser)->location;
6018 stmt = c_parser_compound_statement (parser);
6019 objc_begin_try_stmt (loc, stmt);
6020 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
6022 struct c_parm *parm;
6023 c_parser_consume_token (parser);
6024 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6025 break;
6026 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6027 if (parm == NULL)
6029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6030 break;
6032 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6033 objc_begin_catch_clause (grokparm (parm));
6034 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6035 c_parser_compound_statement_nostart (parser);
6036 objc_finish_catch_clause ();
6038 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6040 location_t finloc;
6041 tree finstmt;
6042 c_parser_consume_token (parser);
6043 finloc = c_parser_peek_token (parser)->location;
6044 finstmt = c_parser_compound_statement (parser);
6045 objc_build_finally_clause (finloc, finstmt);
6047 objc_finish_try_stmt ();
6050 /* Parse an objc-synchronized-statement.
6052 objc-synchronized-statement:
6053 @synchronized ( expression ) compound-statement
6056 static void
6057 c_parser_objc_synchronized_statement (c_parser *parser)
6059 location_t loc;
6060 tree expr, stmt;
6061 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6062 c_parser_consume_token (parser);
6063 loc = c_parser_peek_token (parser)->location;
6064 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6066 expr = c_parser_expression (parser).value;
6067 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6069 else
6070 expr = error_mark_node;
6071 stmt = c_parser_compound_statement (parser);
6072 objc_build_synchronized (loc, expr, stmt);
6075 /* Parse an objc-selector; return NULL_TREE without an error if the
6076 next token is not an objc-selector.
6078 objc-selector:
6079 identifier
6080 one of
6081 enum struct union if else while do for switch case default
6082 break continue return goto asm sizeof typeof __alignof
6083 unsigned long const short volatile signed restrict _Complex
6084 in out inout bycopy byref oneway int char float double void _Bool
6086 ??? Why this selection of keywords but not, for example, storage
6087 class specifiers? */
6089 static tree
6090 c_parser_objc_selector (c_parser *parser)
6092 c_token *token = c_parser_peek_token (parser);
6093 tree value = token->value;
6094 if (token->type == CPP_NAME)
6096 c_parser_consume_token (parser);
6097 return value;
6099 if (token->type != CPP_KEYWORD)
6100 return NULL_TREE;
6101 switch (token->keyword)
6103 case RID_ENUM:
6104 case RID_STRUCT:
6105 case RID_UNION:
6106 case RID_IF:
6107 case RID_ELSE:
6108 case RID_WHILE:
6109 case RID_DO:
6110 case RID_FOR:
6111 case RID_SWITCH:
6112 case RID_CASE:
6113 case RID_DEFAULT:
6114 case RID_BREAK:
6115 case RID_CONTINUE:
6116 case RID_RETURN:
6117 case RID_GOTO:
6118 case RID_ASM:
6119 case RID_SIZEOF:
6120 case RID_TYPEOF:
6121 case RID_ALIGNOF:
6122 case RID_UNSIGNED:
6123 case RID_LONG:
6124 case RID_CONST:
6125 case RID_SHORT:
6126 case RID_VOLATILE:
6127 case RID_SIGNED:
6128 case RID_RESTRICT:
6129 case RID_COMPLEX:
6130 case RID_IN:
6131 case RID_OUT:
6132 case RID_INOUT:
6133 case RID_BYCOPY:
6134 case RID_BYREF:
6135 case RID_ONEWAY:
6136 case RID_INT:
6137 case RID_CHAR:
6138 case RID_FLOAT:
6139 case RID_DOUBLE:
6140 case RID_VOID:
6141 case RID_BOOL:
6142 c_parser_consume_token (parser);
6143 return value;
6144 default:
6145 return NULL_TREE;
6149 /* Parse an objc-selector-arg.
6151 objc-selector-arg:
6152 objc-selector
6153 objc-keywordname-list
6155 objc-keywordname-list:
6156 objc-keywordname
6157 objc-keywordname-list objc-keywordname
6159 objc-keywordname:
6160 objc-selector :
6164 static tree
6165 c_parser_objc_selector_arg (c_parser *parser)
6167 tree sel = c_parser_objc_selector (parser);
6168 tree list = NULL_TREE;
6169 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6170 return sel;
6171 while (true)
6173 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6174 return list;
6175 list = chainon (list, build_tree_list (sel, NULL_TREE));
6176 sel = c_parser_objc_selector (parser);
6177 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6178 break;
6180 return list;
6183 /* Parse an objc-receiver.
6185 objc-receiver:
6186 expression
6187 class-name
6188 type-name
6191 static tree
6192 c_parser_objc_receiver (c_parser *parser)
6194 if (c_parser_peek_token (parser)->type == CPP_NAME
6195 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6196 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6198 tree id = c_parser_peek_token (parser)->value;
6199 c_parser_consume_token (parser);
6200 return objc_get_class_reference (id);
6202 return c_parser_expression (parser).value;
6205 /* Parse objc-message-args.
6207 objc-message-args:
6208 objc-selector
6209 objc-keywordarg-list
6211 objc-keywordarg-list:
6212 objc-keywordarg
6213 objc-keywordarg-list objc-keywordarg
6215 objc-keywordarg:
6216 objc-selector : objc-keywordexpr
6217 : objc-keywordexpr
6220 static tree
6221 c_parser_objc_message_args (c_parser *parser)
6223 tree sel = c_parser_objc_selector (parser);
6224 tree list = NULL_TREE;
6225 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6226 return sel;
6227 while (true)
6229 tree keywordexpr;
6230 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6231 return list;
6232 keywordexpr = c_parser_objc_keywordexpr (parser);
6233 list = chainon (list, build_tree_list (sel, keywordexpr));
6234 sel = c_parser_objc_selector (parser);
6235 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6236 break;
6238 return list;
6241 /* Parse an objc-keywordexpr.
6243 objc-keywordexpr:
6244 nonempty-expr-list
6247 static tree
6248 c_parser_objc_keywordexpr (c_parser *parser)
6250 tree list = c_parser_expr_list (parser, true);
6251 if (TREE_CHAIN (list) == NULL_TREE)
6253 /* Just return the expression, remove a level of
6254 indirection. */
6255 return TREE_VALUE (list);
6257 else
6259 /* We have a comma expression, we will collapse later. */
6260 return list;
6265 /* The actual parser and external interface. ??? Does this need to be
6266 garbage-collected? */
6268 static GTY (()) c_parser *the_parser;
6270 /* Parse a single source file. */
6272 void
6273 c_parse_file (void)
6275 the_parser = c_parser_new ();
6276 c_parser_translation_unit (the_parser);
6277 the_parser = NULL;
6280 #include "gt-c-parser.h"