libcpp/
[official-gcc.git] / gcc / c-parser.c
blobeff5b8323479ded1969b7b0d511339aa34dc9758
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 "rtl.h"
46 #include "langhooks.h"
47 #include "input.h"
48 #include "cpplib.h"
49 #include "timevar.h"
50 #include "c-pragma.h"
51 #include "c-tree.h"
52 #include "flags.h"
53 #include "output.h"
54 #include "toplev.h"
55 #include "ggc.h"
56 #include "c-common.h"
57 #include "vec.h"
58 #include "target.h"
61 /* Miscellaneous data and functions needed for the parser. */
63 int yydebug;
65 /* Objective-C specific parser/lexer information. */
67 static int objc_pq_context = 0;
69 /* The following flag is needed to contextualize Objective-C lexical
70 analysis. In some cases (e.g., 'int NSObject;'), it is undesirable
71 to bind an identifier to an Objective-C class, even if a class with
72 that name exists. */
73 static int objc_need_raw_identifier = 0;
74 #define OBJC_NEED_RAW_IDENTIFIER(VAL) \
75 do { \
76 if (c_dialect_objc ()) \
77 objc_need_raw_identifier = VAL; \
78 } while (0)
80 /* The reserved keyword table. */
81 struct resword
83 const char *word;
84 ENUM_BITFIELD(rid) rid : 16;
85 unsigned int disable : 16;
88 /* Disable mask. Keywords are disabled if (reswords[i].disable &
89 mask) is _true_. */
90 #define D_C89 0x01 /* not in C89 */
91 #define D_EXT 0x02 /* GCC extension */
92 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
93 #define D_OBJC 0x08 /* Objective C only */
95 static const struct resword reswords[] =
97 { "_Bool", RID_BOOL, 0 },
98 { "_Complex", RID_COMPLEX, 0 },
99 { "_Decimal32", RID_DFLOAT32, D_EXT },
100 { "_Decimal64", RID_DFLOAT64, D_EXT },
101 { "_Decimal128", RID_DFLOAT128, D_EXT },
102 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
103 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
104 { "__alignof", RID_ALIGNOF, 0 },
105 { "__alignof__", RID_ALIGNOF, 0 },
106 { "__asm", RID_ASM, 0 },
107 { "__asm__", RID_ASM, 0 },
108 { "__attribute", RID_ATTRIBUTE, 0 },
109 { "__attribute__", RID_ATTRIBUTE, 0 },
110 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
111 { "__builtin_offsetof", RID_OFFSETOF, 0 },
112 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
113 { "__builtin_va_arg", RID_VA_ARG, 0 },
114 { "__complex", RID_COMPLEX, 0 },
115 { "__complex__", RID_COMPLEX, 0 },
116 { "__const", RID_CONST, 0 },
117 { "__const__", RID_CONST, 0 },
118 { "__extension__", RID_EXTENSION, 0 },
119 { "__func__", RID_C99_FUNCTION_NAME, 0 },
120 { "__imag", RID_IMAGPART, 0 },
121 { "__imag__", RID_IMAGPART, 0 },
122 { "__inline", RID_INLINE, 0 },
123 { "__inline__", RID_INLINE, 0 },
124 { "__label__", RID_LABEL, 0 },
125 { "__real", RID_REALPART, 0 },
126 { "__real__", RID_REALPART, 0 },
127 { "__restrict", RID_RESTRICT, 0 },
128 { "__restrict__", RID_RESTRICT, 0 },
129 { "__signed", RID_SIGNED, 0 },
130 { "__signed__", RID_SIGNED, 0 },
131 { "__thread", RID_THREAD, 0 },
132 { "__typeof", RID_TYPEOF, 0 },
133 { "__typeof__", RID_TYPEOF, 0 },
134 { "__volatile", RID_VOLATILE, 0 },
135 { "__volatile__", RID_VOLATILE, 0 },
136 { "asm", RID_ASM, D_EXT },
137 { "auto", RID_AUTO, 0 },
138 { "break", RID_BREAK, 0 },
139 { "case", RID_CASE, 0 },
140 { "char", RID_CHAR, 0 },
141 { "const", RID_CONST, 0 },
142 { "continue", RID_CONTINUE, 0 },
143 { "default", RID_DEFAULT, 0 },
144 { "do", RID_DO, 0 },
145 { "double", RID_DOUBLE, 0 },
146 { "else", RID_ELSE, 0 },
147 { "enum", RID_ENUM, 0 },
148 { "extern", RID_EXTERN, 0 },
149 { "float", RID_FLOAT, 0 },
150 { "for", RID_FOR, 0 },
151 { "goto", RID_GOTO, 0 },
152 { "if", RID_IF, 0 },
153 { "inline", RID_INLINE, D_EXT89 },
154 { "int", RID_INT, 0 },
155 { "long", RID_LONG, 0 },
156 { "register", RID_REGISTER, 0 },
157 { "restrict", RID_RESTRICT, D_C89 },
158 { "return", RID_RETURN, 0 },
159 { "short", RID_SHORT, 0 },
160 { "signed", RID_SIGNED, 0 },
161 { "sizeof", RID_SIZEOF, 0 },
162 { "static", RID_STATIC, 0 },
163 { "struct", RID_STRUCT, 0 },
164 { "switch", RID_SWITCH, 0 },
165 { "typedef", RID_TYPEDEF, 0 },
166 { "typeof", RID_TYPEOF, D_EXT },
167 { "union", RID_UNION, 0 },
168 { "unsigned", RID_UNSIGNED, 0 },
169 { "void", RID_VOID, 0 },
170 { "volatile", RID_VOLATILE, 0 },
171 { "while", RID_WHILE, 0 },
172 /* These Objective-C keywords are recognized only immediately after
173 an '@'. */
174 { "class", RID_AT_CLASS, D_OBJC },
175 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
176 { "defs", RID_AT_DEFS, D_OBJC },
177 { "encode", RID_AT_ENCODE, D_OBJC },
178 { "end", RID_AT_END, D_OBJC },
179 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
180 { "interface", RID_AT_INTERFACE, D_OBJC },
181 { "private", RID_AT_PRIVATE, D_OBJC },
182 { "protected", RID_AT_PROTECTED, D_OBJC },
183 { "protocol", RID_AT_PROTOCOL, D_OBJC },
184 { "public", RID_AT_PUBLIC, D_OBJC },
185 { "selector", RID_AT_SELECTOR, D_OBJC },
186 { "throw", RID_AT_THROW, D_OBJC },
187 { "try", RID_AT_TRY, D_OBJC },
188 { "catch", RID_AT_CATCH, D_OBJC },
189 { "finally", RID_AT_FINALLY, D_OBJC },
190 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
191 /* These are recognized only in protocol-qualifier context
192 (see above) */
193 { "bycopy", RID_BYCOPY, D_OBJC },
194 { "byref", RID_BYREF, D_OBJC },
195 { "in", RID_IN, D_OBJC },
196 { "inout", RID_INOUT, D_OBJC },
197 { "oneway", RID_ONEWAY, D_OBJC },
198 { "out", RID_OUT, D_OBJC },
200 #define N_reswords (sizeof reswords / sizeof (struct resword))
202 /* Initialization routine for this file. */
204 void
205 c_parse_init (void)
207 /* The only initialization required is of the reserved word
208 identifiers. */
209 unsigned int i;
210 tree id;
211 int mask = (flag_isoc99 ? 0 : D_C89)
212 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
214 if (!c_dialect_objc ())
215 mask |= D_OBJC;
217 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
218 for (i = 0; i < N_reswords; i++)
220 /* If a keyword is disabled, do not enter it into the table
221 and so create a canonical spelling that isn't a keyword. */
222 if (reswords[i].disable & mask)
223 continue;
225 id = get_identifier (reswords[i].word);
226 C_RID_CODE (id) = reswords[i].rid;
227 C_IS_RESERVED_WORD (id) = 1;
228 ridpointers [(int) reswords[i].rid] = id;
232 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
233 and the C parser. Unlike the C++ lexer, the parser structure
234 stores the lexer information instead of using a separate structure.
235 Identifiers are separated into ordinary identifiers, type names,
236 keywords and some other Objective-C types of identifiers, and some
237 look-ahead is maintained.
239 ??? It might be a good idea to lex the whole file up front (as for
240 C++). It would then be possible to share more of the C and C++
241 lexer code, if desired. */
243 /* The following local token type is used. */
245 /* A keyword. */
246 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
248 /* More information about the type of a CPP_NAME token. */
249 typedef enum c_id_kind {
250 /* An ordinary identifier. */
251 C_ID_ID,
252 /* An identifier declared as a typedef name. */
253 C_ID_TYPENAME,
254 /* An identifier declared as an Objective-C class name. */
255 C_ID_CLASSNAME,
256 /* Not an identifier. */
257 C_ID_NONE
258 } c_id_kind;
260 /* A single C token after string literal concatenation and conversion
261 of preprocessing tokens to tokens. */
262 typedef struct c_token GTY (())
264 /* The kind of token. */
265 ENUM_BITFIELD (cpp_ttype) type : 8;
266 /* If this token is a CPP_NAME, this value indicates whether also
267 declared as some kind of type. Otherwise, it is C_ID_NONE. */
268 ENUM_BITFIELD (c_id_kind) id_kind : 8;
269 /* If this token is a keyword, this value indicates which keyword.
270 Otherwise, this value is RID_MAX. */
271 ENUM_BITFIELD (rid) keyword : 8;
272 /* If this token is a CPP_PRAGMA, this indicates the pragma that
273 was seen. Otherwise it is PRAGMA_NONE. */
274 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
275 /* True if this token is from a system header. */
276 BOOL_BITFIELD in_system_header : 1;
277 /* The value associated with this token, if any. */
278 tree value;
279 /* The location at which this token was found. */
280 location_t location;
281 } c_token;
283 /* A parser structure recording information about the state and
284 context of parsing. Includes lexer information with up to two
285 tokens of look-ahead; more are not needed for C. */
286 typedef struct c_parser GTY(())
288 /* The look-ahead tokens. */
289 c_token tokens[2];
290 /* How many look-ahead tokens are available (0, 1 or 2). */
291 short tokens_avail;
292 /* True if a syntax error is being recovered from; false otherwise.
293 c_parser_error sets this flag. It should clear this flag when
294 enough tokens have been consumed to recover from the error. */
295 BOOL_BITFIELD error : 1;
296 /* True if we're processing a pragma, and shouldn't automatically
297 consume CPP_PRAGMA_EOL. */
298 BOOL_BITFIELD in_pragma : 1;
299 } c_parser;
302 /* The actual parser and external interface. ??? Does this need to be
303 garbage-collected? */
305 static GTY (()) c_parser *the_parser;
308 /* Read in and lex a single token, storing it in *TOKEN. */
310 static void
311 c_lex_one_token (c_token *token)
313 timevar_push (TV_LEX);
315 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
316 token->id_kind = C_ID_NONE;
317 token->keyword = RID_MAX;
318 token->pragma_kind = PRAGMA_NONE;
319 token->in_system_header = in_system_header;
321 switch (token->type)
323 case CPP_NAME:
325 tree decl;
327 int objc_force_identifier = objc_need_raw_identifier;
328 OBJC_NEED_RAW_IDENTIFIER (0);
330 if (C_IS_RESERVED_WORD (token->value))
332 enum rid rid_code = C_RID_CODE (token->value);
334 if (c_dialect_objc ())
336 if (!OBJC_IS_AT_KEYWORD (rid_code)
337 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
339 /* Return the canonical spelling for this keyword. */
340 token->value = ridpointers[(int) rid_code];
341 token->type = CPP_KEYWORD;
342 token->keyword = rid_code;
343 break;
346 else
348 /* Return the canonical spelling for this keyword. */
349 token->value = ridpointers[(int) rid_code];
350 token->type = CPP_KEYWORD;
351 token->keyword = rid_code;
352 break;
356 decl = lookup_name (token->value);
357 if (decl)
359 if (TREE_CODE (decl) == TYPE_DECL)
361 token->id_kind = C_ID_TYPENAME;
362 break;
365 else if (c_dialect_objc ())
367 tree objc_interface_decl = objc_is_class_name (token->value);
368 /* Objective-C class names are in the same namespace as
369 variables and typedefs, and hence are shadowed by local
370 declarations. */
371 if (objc_interface_decl
372 && (global_bindings_p ()
373 || (!objc_force_identifier && !decl)))
375 token->value = objc_interface_decl;
376 token->id_kind = C_ID_CLASSNAME;
377 break;
380 token->id_kind = C_ID_ID;
382 break;
383 case CPP_AT_NAME:
384 /* This only happens in Objective-C; it must be a keyword. */
385 token->type = CPP_KEYWORD;
386 token->keyword = C_RID_CODE (token->value);
387 break;
388 case CPP_COLON:
389 case CPP_COMMA:
390 case CPP_CLOSE_PAREN:
391 case CPP_SEMICOLON:
392 /* These tokens may affect the interpretation of any identifiers
393 following, if doing Objective-C. */
394 OBJC_NEED_RAW_IDENTIFIER (0);
395 break;
396 case CPP_PRAGMA:
397 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
398 token->pragma_kind = TREE_INT_CST_LOW (token->value);
399 token->value = NULL;
400 break;
401 default:
402 break;
404 timevar_pop (TV_LEX);
407 /* Return a pointer to the next token from PARSER, reading it in if
408 necessary. */
410 static inline c_token *
411 c_parser_peek_token (c_parser *parser)
413 if (parser->tokens_avail == 0)
415 c_lex_one_token (&parser->tokens[0]);
416 parser->tokens_avail = 1;
418 return &parser->tokens[0];
421 /* Return true if the next token from PARSER has the indicated
422 TYPE. */
424 static inline bool
425 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
427 return c_parser_peek_token (parser)->type == type;
430 /* Return true if the next token from PARSER does not have the
431 indicated TYPE. */
433 static inline bool
434 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
436 return !c_parser_next_token_is (parser, type);
439 /* Return true if the next token from PARSER is the indicated
440 KEYWORD. */
442 static inline bool
443 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
445 c_token *token;
447 /* Peek at the next token. */
448 token = c_parser_peek_token (parser);
449 /* Check to see if it is the indicated keyword. */
450 return token->keyword == keyword;
453 /* Return true if TOKEN can start a type name,
454 false otherwise. */
455 static bool
456 c_token_starts_typename (c_token *token)
458 switch (token->type)
460 case CPP_NAME:
461 switch (token->id_kind)
463 case C_ID_ID:
464 return false;
465 case C_ID_TYPENAME:
466 return true;
467 case C_ID_CLASSNAME:
468 gcc_assert (c_dialect_objc ());
469 return true;
470 default:
471 gcc_unreachable ();
473 case CPP_KEYWORD:
474 switch (token->keyword)
476 case RID_UNSIGNED:
477 case RID_LONG:
478 case RID_SHORT:
479 case RID_SIGNED:
480 case RID_COMPLEX:
481 case RID_INT:
482 case RID_CHAR:
483 case RID_FLOAT:
484 case RID_DOUBLE:
485 case RID_VOID:
486 case RID_DFLOAT32:
487 case RID_DFLOAT64:
488 case RID_DFLOAT128:
489 case RID_BOOL:
490 case RID_ENUM:
491 case RID_STRUCT:
492 case RID_UNION:
493 case RID_TYPEOF:
494 case RID_CONST:
495 case RID_VOLATILE:
496 case RID_RESTRICT:
497 case RID_ATTRIBUTE:
498 return true;
499 default:
500 return false;
502 case CPP_LESS:
503 if (c_dialect_objc ())
504 return true;
505 return false;
506 default:
507 return false;
511 /* Return true if the next token from PARSER can start a type name,
512 false otherwise. */
513 static inline bool
514 c_parser_next_token_starts_typename (c_parser *parser)
516 c_token *token = c_parser_peek_token (parser);
517 return c_token_starts_typename (token);
520 /* Return true if TOKEN can start declaration specifiers, false
521 otherwise. */
522 static bool
523 c_token_starts_declspecs (c_token *token)
525 switch (token->type)
527 case CPP_NAME:
528 switch (token->id_kind)
530 case C_ID_ID:
531 return false;
532 case C_ID_TYPENAME:
533 return true;
534 case C_ID_CLASSNAME:
535 gcc_assert (c_dialect_objc ());
536 return true;
537 default:
538 gcc_unreachable ();
540 case CPP_KEYWORD:
541 switch (token->keyword)
543 case RID_STATIC:
544 case RID_EXTERN:
545 case RID_REGISTER:
546 case RID_TYPEDEF:
547 case RID_INLINE:
548 case RID_AUTO:
549 case RID_THREAD:
550 case RID_UNSIGNED:
551 case RID_LONG:
552 case RID_SHORT:
553 case RID_SIGNED:
554 case RID_COMPLEX:
555 case RID_INT:
556 case RID_CHAR:
557 case RID_FLOAT:
558 case RID_DOUBLE:
559 case RID_VOID:
560 case RID_DFLOAT32:
561 case RID_DFLOAT64:
562 case RID_DFLOAT128:
563 case RID_BOOL:
564 case RID_ENUM:
565 case RID_STRUCT:
566 case RID_UNION:
567 case RID_TYPEOF:
568 case RID_CONST:
569 case RID_VOLATILE:
570 case RID_RESTRICT:
571 case RID_ATTRIBUTE:
572 return true;
573 default:
574 return false;
576 case CPP_LESS:
577 if (c_dialect_objc ())
578 return true;
579 return false;
580 default:
581 return false;
585 /* Return true if the next token from PARSER can start declaration
586 specifiers, false otherwise. */
587 static inline bool
588 c_parser_next_token_starts_declspecs (c_parser *parser)
590 c_token *token = c_parser_peek_token (parser);
591 return c_token_starts_declspecs (token);
594 /* Return a pointer to the next-but-one token from PARSER, reading it
595 in if necessary. The next token is already read in. */
597 static c_token *
598 c_parser_peek_2nd_token (c_parser *parser)
600 if (parser->tokens_avail >= 2)
601 return &parser->tokens[1];
602 gcc_assert (parser->tokens_avail == 1);
603 gcc_assert (parser->tokens[0].type != CPP_EOF);
604 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
605 c_lex_one_token (&parser->tokens[1]);
606 parser->tokens_avail = 2;
607 return &parser->tokens[1];
610 /* Consume the next token from PARSER. */
612 static void
613 c_parser_consume_token (c_parser *parser)
615 gcc_assert (parser->tokens_avail >= 1);
616 gcc_assert (parser->tokens[0].type != CPP_EOF);
617 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
618 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
619 if (parser->tokens_avail == 2)
620 parser->tokens[0] = parser->tokens[1];
621 parser->tokens_avail--;
624 /* Expect the current token to be a #pragma. Consume it and remember
625 that we've begun parsing a pragma. */
627 static void
628 c_parser_consume_pragma (c_parser *parser)
630 gcc_assert (!parser->in_pragma);
631 gcc_assert (parser->tokens_avail >= 1);
632 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
633 if (parser->tokens_avail == 2)
634 parser->tokens[0] = parser->tokens[1];
635 parser->tokens_avail--;
636 parser->in_pragma = true;
639 /* Update the globals input_location and in_system_header from
640 TOKEN. */
641 static inline void
642 c_parser_set_source_position_from_token (c_token *token)
644 if (token->type != CPP_EOF)
646 input_location = token->location;
647 in_system_header = token->in_system_header;
651 /* Issue a diagnostic of the form
652 FILE:LINE: MESSAGE before TOKEN
653 where TOKEN is the next token in the input stream of PARSER.
654 MESSAGE (specified by the caller) is usually of the form "expected
655 OTHER-TOKEN".
657 Do not issue a diagnostic if still recovering from an error.
659 ??? This is taken from the C++ parser, but building up messages in
660 this way is not i18n-friendly and some other approach should be
661 used. */
663 static void
664 c_parser_error (c_parser *parser, const char *gmsgid)
666 c_token *token = c_parser_peek_token (parser);
667 if (parser->error)
668 return;
669 parser->error = true;
670 if (!gmsgid)
671 return;
672 /* This diagnostic makes more sense if it is tagged to the line of
673 the token we just peeked at. */
674 c_parser_set_source_position_from_token (token);
675 c_parse_error (gmsgid,
676 /* Because c_parse_error does not understand
677 CPP_KEYWORD, keywords are treated like
678 identifiers. */
679 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
680 token->value);
683 /* If the next token is of the indicated TYPE, consume it. Otherwise,
684 issue the error MSGID. If MSGID is NULL then a message has already
685 been produced and no message will be produced this time. Returns
686 true if found, false otherwise. */
688 static bool
689 c_parser_require (c_parser *parser,
690 enum cpp_ttype type,
691 const char *msgid)
693 if (c_parser_next_token_is (parser, type))
695 c_parser_consume_token (parser);
696 return true;
698 else
700 c_parser_error (parser, msgid);
701 return false;
705 /* If the next token is the indicated keyword, consume it. Otherwise,
706 issue the error MSGID. Returns true if found, false otherwise. */
708 static bool
709 c_parser_require_keyword (c_parser *parser,
710 enum rid keyword,
711 const char *msgid)
713 if (c_parser_next_token_is_keyword (parser, keyword))
715 c_parser_consume_token (parser);
716 return true;
718 else
720 c_parser_error (parser, msgid);
721 return false;
725 /* Like c_parser_require, except that tokens will be skipped until the
726 desired token is found. An error message is still produced if the
727 next token is not as expected. If MSGID is NULL then a message has
728 already been produced and no message will be produced this
729 time. */
731 static void
732 c_parser_skip_until_found (c_parser *parser,
733 enum cpp_ttype type,
734 const char *msgid)
736 unsigned nesting_depth = 0;
738 if (c_parser_require (parser, type, msgid))
739 return;
741 /* Skip tokens until the desired token is found. */
742 while (true)
744 /* Peek at the next token. */
745 c_token *token = c_parser_peek_token (parser);
746 /* If we've reached the token we want, consume it and stop. */
747 if (token->type == type && !nesting_depth)
749 c_parser_consume_token (parser);
750 break;
753 /* If we've run out of tokens, stop. */
754 if (token->type == CPP_EOF)
755 return;
756 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
757 return;
758 if (token->type == CPP_OPEN_BRACE
759 || token->type == CPP_OPEN_PAREN
760 || token->type == CPP_OPEN_SQUARE)
761 ++nesting_depth;
762 else if (token->type == CPP_CLOSE_BRACE
763 || token->type == CPP_CLOSE_PAREN
764 || token->type == CPP_CLOSE_SQUARE)
766 if (nesting_depth-- == 0)
767 break;
769 /* Consume this token. */
770 c_parser_consume_token (parser);
772 parser->error = false;
775 /* Skip tokens until the end of a parameter is found, but do not
776 consume the comma, semicolon or closing delimiter. */
778 static void
779 c_parser_skip_to_end_of_parameter (c_parser *parser)
781 unsigned nesting_depth = 0;
783 while (true)
785 c_token *token = c_parser_peek_token (parser);
786 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
787 && !nesting_depth)
788 break;
789 /* If we've run out of tokens, stop. */
790 if (token->type == CPP_EOF)
791 return;
792 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
793 return;
794 if (token->type == CPP_OPEN_BRACE
795 || token->type == CPP_OPEN_PAREN
796 || token->type == CPP_OPEN_SQUARE)
797 ++nesting_depth;
798 else if (token->type == CPP_CLOSE_BRACE
799 || token->type == CPP_CLOSE_PAREN
800 || token->type == CPP_CLOSE_SQUARE)
802 if (nesting_depth-- == 0)
803 break;
805 /* Consume this token. */
806 c_parser_consume_token (parser);
808 parser->error = false;
811 /* Skip tokens until we have consumed an entire block, or until we
812 have consumed a non-nested ';'. */
814 static void
815 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
817 unsigned nesting_depth = 0;
819 while (true)
821 c_token *token;
823 /* Peek at the next token. */
824 token = c_parser_peek_token (parser);
825 /* If we've run out of tokens, stop. */
826 if (token->type == CPP_EOF)
827 return;
828 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
829 return;
830 /* If the next token is a ';', we have reached the end of the
831 statement. */
832 if (token->type == CPP_SEMICOLON && !nesting_depth)
834 /* Consume the ';'. */
835 c_parser_consume_token (parser);
836 break;
838 /* If the next token is a non-nested '}', then we have reached
839 the end of the current block. */
840 if (token->type == CPP_CLOSE_BRACE
841 && (nesting_depth == 0 || --nesting_depth == 0))
843 c_parser_consume_token (parser);
844 break;
846 /* If it the next token is a '{', then we are entering a new
847 block. Consume the entire block. */
848 if (token->type == CPP_OPEN_BRACE)
849 ++nesting_depth;
850 c_parser_consume_token (parser);
852 parser->error = false;
855 /* Expect to be at the end of the pragma directive and consume an
856 end of line marker. */
858 static void
859 c_parser_skip_to_pragma_eol (c_parser *parser)
861 gcc_assert (parser->in_pragma);
862 parser->in_pragma = false;
864 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
865 while (true)
867 c_token *token = c_parser_peek_token (parser);
868 if (token->type == CPP_EOF)
869 break;
870 if (token->type == CPP_PRAGMA_EOL)
872 c_parser_consume_token (parser);
873 break;
875 c_parser_consume_token (parser);
878 parser->error = false;
881 /* Save the warning flags which are controlled by __extension__. */
883 static inline int
884 disable_extension_diagnostics (void)
886 int ret = (pedantic
887 | (warn_pointer_arith << 1)
888 | (warn_traditional << 2)
889 | (flag_iso << 3));
890 pedantic = 0;
891 warn_pointer_arith = 0;
892 warn_traditional = 0;
893 flag_iso = 0;
894 return ret;
897 /* Restore the warning flags which are controlled by __extension__.
898 FLAGS is the return value from disable_extension_diagnostics. */
900 static inline void
901 restore_extension_diagnostics (int flags)
903 pedantic = flags & 1;
904 warn_pointer_arith = (flags >> 1) & 1;
905 warn_traditional = (flags >> 2) & 1;
906 flag_iso = (flags >> 3) & 1;
909 /* Possibly kinds of declarator to parse. */
910 typedef enum c_dtr_syn {
911 /* A normal declarator with an identifier. */
912 C_DTR_NORMAL,
913 /* An abstract declarator (maybe empty). */
914 C_DTR_ABSTRACT,
915 /* A parameter declarator: may be either, but after a type name does
916 not redeclare a typedef name as an identifier if it can
917 alternatively be interpreted as a typedef name; see DR#009,
918 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
919 following DR#249. For example, given a typedef T, "int T" and
920 "int *T" are valid parameter declarations redeclaring T, while
921 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
922 abstract declarators rather than involving redundant parentheses;
923 the same applies with attributes inside the parentheses before
924 "T". */
925 C_DTR_PARM
926 } c_dtr_syn;
928 static void c_parser_external_declaration (c_parser *);
929 static void c_parser_asm_definition (c_parser *);
930 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
931 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
932 bool);
933 static struct c_typespec c_parser_enum_specifier (c_parser *);
934 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
935 static tree c_parser_struct_declaration (c_parser *);
936 static struct c_typespec c_parser_typeof_specifier (c_parser *);
937 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
938 bool *);
939 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
940 c_dtr_syn, bool *);
941 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
942 bool,
943 struct c_declarator *);
944 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
945 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
946 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
947 static tree c_parser_simple_asm_expr (c_parser *);
948 static tree c_parser_attributes (c_parser *);
949 static struct c_type_name *c_parser_type_name (c_parser *);
950 static struct c_expr c_parser_initializer (c_parser *);
951 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
952 static void c_parser_initelt (c_parser *);
953 static void c_parser_initval (c_parser *, struct c_expr *);
954 static tree c_parser_compound_statement (c_parser *);
955 static void c_parser_compound_statement_nostart (c_parser *);
956 static void c_parser_label (c_parser *);
957 static void c_parser_statement (c_parser *);
958 static void c_parser_statement_after_labels (c_parser *);
959 static void c_parser_if_statement (c_parser *);
960 static void c_parser_switch_statement (c_parser *);
961 static void c_parser_while_statement (c_parser *);
962 static void c_parser_do_statement (c_parser *);
963 static void c_parser_for_statement (c_parser *);
964 static tree c_parser_asm_statement (c_parser *);
965 static tree c_parser_asm_operands (c_parser *, bool);
966 static tree c_parser_asm_clobbers (c_parser *);
967 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
968 static struct c_expr c_parser_conditional_expression (c_parser *,
969 struct c_expr *);
970 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
971 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
972 static struct c_expr c_parser_unary_expression (c_parser *);
973 static struct c_expr c_parser_sizeof_expression (c_parser *);
974 static struct c_expr c_parser_alignof_expression (c_parser *);
975 static struct c_expr c_parser_postfix_expression (c_parser *);
976 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
977 struct c_type_name *);
978 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
979 struct c_expr);
980 static struct c_expr c_parser_expression (c_parser *);
981 static struct c_expr c_parser_expression_conv (c_parser *);
982 static tree c_parser_expr_list (c_parser *, bool);
984 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
985 static bool c_parser_pragma (c_parser *, enum pragma_context);
987 /* These Objective-C parser functions are only ever called when
988 compiling Objective-C. */
989 static void c_parser_objc_class_definition (c_parser *);
990 static void c_parser_objc_class_instance_variables (c_parser *);
991 static void c_parser_objc_class_declaration (c_parser *);
992 static void c_parser_objc_alias_declaration (c_parser *);
993 static void c_parser_objc_protocol_definition (c_parser *);
994 static enum tree_code c_parser_objc_method_type (c_parser *);
995 static void c_parser_objc_method_definition (c_parser *);
996 static void c_parser_objc_methodprotolist (c_parser *);
997 static void c_parser_objc_methodproto (c_parser *);
998 static tree c_parser_objc_method_decl (c_parser *);
999 static tree c_parser_objc_type_name (c_parser *);
1000 static tree c_parser_objc_protocol_refs (c_parser *);
1001 static void c_parser_objc_try_catch_statement (c_parser *);
1002 static void c_parser_objc_synchronized_statement (c_parser *);
1003 static tree c_parser_objc_selector (c_parser *);
1004 static tree c_parser_objc_selector_arg (c_parser *);
1005 static tree c_parser_objc_receiver (c_parser *);
1006 static tree c_parser_objc_message_args (c_parser *);
1007 static tree c_parser_objc_keywordexpr (c_parser *);
1009 /* Parse a translation unit (C90 6.7, C99 6.9).
1011 translation-unit:
1012 external-declarations
1014 external-declarations:
1015 external-declaration
1016 external-declarations external-declaration
1018 GNU extensions:
1020 translation-unit:
1021 empty
1024 static void
1025 c_parser_translation_unit (c_parser *parser)
1027 if (c_parser_next_token_is (parser, CPP_EOF))
1029 if (pedantic)
1030 pedwarn ("ISO C forbids an empty source file");
1032 else
1034 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1037 ggc_collect ();
1038 c_parser_external_declaration (parser);
1039 obstack_free (&parser_obstack, obstack_position);
1041 while (c_parser_next_token_is_not (parser, CPP_EOF));
1045 /* Parse an external declaration (C90 6.7, C99 6.9).
1047 external-declaration:
1048 function-definition
1049 declaration
1051 GNU extensions:
1053 external-declaration:
1054 asm-definition
1056 __extension__ external-declaration
1058 Objective-C:
1060 external-declaration:
1061 objc-class-definition
1062 objc-class-declaration
1063 objc-alias-declaration
1064 objc-protocol-definition
1065 objc-method-definition
1066 @end
1069 static void
1070 c_parser_external_declaration (c_parser *parser)
1072 int ext;
1073 switch (c_parser_peek_token (parser)->type)
1075 case CPP_KEYWORD:
1076 switch (c_parser_peek_token (parser)->keyword)
1078 case RID_EXTENSION:
1079 ext = disable_extension_diagnostics ();
1080 c_parser_consume_token (parser);
1081 c_parser_external_declaration (parser);
1082 restore_extension_diagnostics (ext);
1083 break;
1084 case RID_ASM:
1085 c_parser_asm_definition (parser);
1086 break;
1087 case RID_AT_INTERFACE:
1088 case RID_AT_IMPLEMENTATION:
1089 gcc_assert (c_dialect_objc ());
1090 c_parser_objc_class_definition (parser);
1091 break;
1092 case RID_AT_CLASS:
1093 gcc_assert (c_dialect_objc ());
1094 c_parser_objc_class_declaration (parser);
1095 break;
1096 case RID_AT_ALIAS:
1097 gcc_assert (c_dialect_objc ());
1098 c_parser_objc_alias_declaration (parser);
1099 break;
1100 case RID_AT_PROTOCOL:
1101 gcc_assert (c_dialect_objc ());
1102 c_parser_objc_protocol_definition (parser);
1103 break;
1104 case RID_AT_END:
1105 gcc_assert (c_dialect_objc ());
1106 c_parser_consume_token (parser);
1107 objc_finish_implementation ();
1108 break;
1109 default:
1110 goto decl_or_fndef;
1112 break;
1113 case CPP_SEMICOLON:
1114 if (pedantic)
1115 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1116 c_parser_consume_token (parser);
1117 break;
1118 case CPP_PRAGMA:
1119 c_parser_pragma (parser, pragma_external);
1120 break;
1121 case CPP_PLUS:
1122 case CPP_MINUS:
1123 if (c_dialect_objc ())
1125 c_parser_objc_method_definition (parser);
1126 break;
1128 /* Else fall through, and yield a syntax error trying to parse
1129 as a declaration or function definition. */
1130 default:
1131 decl_or_fndef:
1132 /* A declaration or a function definition. We can only tell
1133 which after parsing the declaration specifiers, if any, and
1134 the first declarator. */
1135 c_parser_declaration_or_fndef (parser, true, true, false, true);
1136 break;
1141 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1142 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1143 accepted; otherwise (old-style parameter declarations) only other
1144 declarations are accepted. If NESTED is true, we are inside a
1145 function or parsing old-style parameter declarations; any functions
1146 encountered are nested functions and declaration specifiers are
1147 required; otherwise we are at top level and functions are normal
1148 functions and declaration specifiers may be optional. If EMPTY_OK
1149 is true, empty declarations are OK (subject to all other
1150 constraints); otherwise (old-style parameter declarations) they are
1151 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1152 may start with attributes; otherwise they may not.
1154 declaration:
1155 declaration-specifiers init-declarator-list[opt] ;
1157 function-definition:
1158 declaration-specifiers[opt] declarator declaration-list[opt]
1159 compound-statement
1161 declaration-list:
1162 declaration
1163 declaration-list declaration
1165 init-declarator-list:
1166 init-declarator
1167 init-declarator-list , init-declarator
1169 init-declarator:
1170 declarator simple-asm-expr[opt] attributes[opt]
1171 declarator simple-asm-expr[opt] attributes[opt] = initializer
1173 GNU extensions:
1175 nested-function-definition:
1176 declaration-specifiers declarator declaration-list[opt]
1177 compound-statement
1179 The simple-asm-expr and attributes are GNU extensions.
1181 This function does not handle __extension__; that is handled in its
1182 callers. ??? Following the old parser, __extension__ may start
1183 external declarations, declarations in functions and declarations
1184 at the start of "for" loops, but not old-style parameter
1185 declarations.
1187 C99 requires declaration specifiers in a function definition; the
1188 absence is diagnosed through the diagnosis of implicit int. In GNU
1189 C we also allow but diagnose declarations without declaration
1190 specifiers, but only at top level (elsewhere they conflict with
1191 other syntax). */
1193 static void
1194 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1195 bool nested, bool start_attr_ok)
1197 struct c_declspecs *specs;
1198 tree prefix_attrs;
1199 tree all_prefix_attrs;
1200 bool diagnosed_no_specs = false;
1202 specs = build_null_declspecs ();
1203 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1204 if (parser->error)
1206 c_parser_skip_to_end_of_block_or_statement (parser);
1207 return;
1209 if (nested && !specs->declspecs_seen_p)
1211 c_parser_error (parser, "expected declaration specifiers");
1212 c_parser_skip_to_end_of_block_or_statement (parser);
1213 return;
1215 finish_declspecs (specs);
1216 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1218 if (empty_ok)
1219 shadow_tag (specs);
1220 else
1222 shadow_tag_warned (specs, 1);
1223 pedwarn ("empty declaration");
1225 c_parser_consume_token (parser);
1226 return;
1228 pending_xref_error ();
1229 prefix_attrs = specs->attrs;
1230 all_prefix_attrs = prefix_attrs;
1231 specs->attrs = NULL_TREE;
1232 while (true)
1234 struct c_declarator *declarator;
1235 bool dummy = false;
1236 tree fnbody;
1237 /* Declaring either one or more declarators (in which case we
1238 should diagnose if there were no declaration specifiers) or a
1239 function definition (in which case the diagnostic for
1240 implicit int suffices). */
1241 declarator = c_parser_declarator (parser, specs->type_seen_p,
1242 C_DTR_NORMAL, &dummy);
1243 if (declarator == NULL)
1245 c_parser_skip_to_end_of_block_or_statement (parser);
1246 return;
1248 if (c_parser_next_token_is (parser, CPP_EQ)
1249 || c_parser_next_token_is (parser, CPP_COMMA)
1250 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1251 || c_parser_next_token_is_keyword (parser, RID_ASM)
1252 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1254 tree asm_name = NULL_TREE;
1255 tree postfix_attrs = NULL_TREE;
1256 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1258 diagnosed_no_specs = true;
1259 pedwarn ("data definition has no type or storage class");
1261 /* Having seen a data definition, there cannot now be a
1262 function definition. */
1263 fndef_ok = false;
1264 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1265 asm_name = c_parser_simple_asm_expr (parser);
1266 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1267 postfix_attrs = c_parser_attributes (parser);
1268 if (c_parser_next_token_is (parser, CPP_EQ))
1270 tree d;
1271 struct c_expr init;
1272 c_parser_consume_token (parser);
1273 /* The declaration of the variable is in effect while
1274 its initializer is parsed. */
1275 d = start_decl (declarator, specs, true,
1276 chainon (postfix_attrs, all_prefix_attrs));
1277 if (!d)
1278 d = error_mark_node;
1279 start_init (d, asm_name, global_bindings_p ());
1280 init = c_parser_initializer (parser);
1281 finish_init ();
1282 if (d != error_mark_node)
1284 maybe_warn_string_init (TREE_TYPE (d), init);
1285 finish_decl (d, init.value, asm_name);
1288 else
1290 tree d = start_decl (declarator, specs, false,
1291 chainon (postfix_attrs,
1292 all_prefix_attrs));
1293 if (d)
1294 finish_decl (d, NULL_TREE, asm_name);
1296 if (c_parser_next_token_is (parser, CPP_COMMA))
1298 c_parser_consume_token (parser);
1299 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1300 all_prefix_attrs = chainon (c_parser_attributes (parser),
1301 prefix_attrs);
1302 else
1303 all_prefix_attrs = prefix_attrs;
1304 continue;
1306 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1308 c_parser_consume_token (parser);
1309 return;
1311 else
1313 c_parser_error (parser, "expected %<,%> or %<;%>");
1314 c_parser_skip_to_end_of_block_or_statement (parser);
1315 return;
1318 else if (!fndef_ok)
1320 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1321 "%<asm%> or %<__attribute__%>");
1322 c_parser_skip_to_end_of_block_or_statement (parser);
1323 return;
1325 /* Function definition (nested or otherwise). */
1326 if (nested)
1328 if (pedantic)
1329 pedwarn ("ISO C forbids nested functions");
1330 push_function_context ();
1332 if (!start_function (specs, declarator, all_prefix_attrs))
1334 /* This can appear in many cases looking nothing like a
1335 function definition, so we don't give a more specific
1336 error suggesting there was one. */
1337 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1338 "or %<__attribute__%>");
1339 if (nested)
1340 pop_function_context ();
1341 break;
1343 /* Parse old-style parameter declarations. ??? Attributes are
1344 not allowed to start declaration specifiers here because of a
1345 syntax conflict between a function declaration with attribute
1346 suffix and a function definition with an attribute prefix on
1347 first old-style parameter declaration. Following the old
1348 parser, they are not accepted on subsequent old-style
1349 parameter declarations either. However, there is no
1350 ambiguity after the first declaration, nor indeed on the
1351 first as long as we don't allow postfix attributes after a
1352 declarator with a nonempty identifier list in a definition;
1353 and postfix attributes have never been accepted here in
1354 function definitions either. */
1355 while (c_parser_next_token_is_not (parser, CPP_EOF)
1356 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1357 c_parser_declaration_or_fndef (parser, false, false, true, false);
1358 DECL_SOURCE_LOCATION (current_function_decl)
1359 = c_parser_peek_token (parser)->location;
1360 store_parm_decls ();
1361 fnbody = c_parser_compound_statement (parser);
1362 if (nested)
1364 tree decl = current_function_decl;
1365 add_stmt (fnbody);
1366 finish_function ();
1367 pop_function_context ();
1368 add_stmt (build_stmt (DECL_EXPR, decl));
1370 else
1372 add_stmt (fnbody);
1373 finish_function ();
1375 break;
1379 /* Parse an asm-definition (asm() outside a function body). This is a
1380 GNU extension.
1382 asm-definition:
1383 simple-asm-expr ;
1386 static void
1387 c_parser_asm_definition (c_parser *parser)
1389 tree asm_str = c_parser_simple_asm_expr (parser);
1390 /* ??? This only works sensibly in the presence of
1391 -fno-unit-at-a-time; file-scope asms really need to be passed to
1392 cgraph which needs to preserve the order of functions and
1393 file-scope asms. */
1394 if (asm_str)
1395 assemble_asm (asm_str);
1396 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1399 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1400 6.7), adding them to SPECS (which may already include some).
1401 Storage class specifiers are accepted iff SCSPEC_OK; type
1402 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1403 the start iff START_ATTR_OK.
1405 declaration-specifiers:
1406 storage-class-specifier declaration-specifiers[opt]
1407 type-specifier declaration-specifiers[opt]
1408 type-qualifier declaration-specifiers[opt]
1409 function-specifier declaration-specifiers[opt]
1411 Function specifiers (inline) are from C99, and are currently
1412 handled as storage class specifiers, as is __thread.
1414 C90 6.5.1, C99 6.7.1:
1415 storage-class-specifier:
1416 typedef
1417 extern
1418 static
1419 auto
1420 register
1422 C99 6.7.4:
1423 function-specifier:
1424 inline
1426 C90 6.5.2, C99 6.7.2:
1427 type-specifier:
1428 void
1429 char
1430 short
1432 long
1433 float
1434 double
1435 signed
1436 unsigned
1437 _Bool
1438 _Complex
1439 [_Imaginary removed in C99 TC2]
1440 struct-or-union-specifier
1441 enum-specifier
1442 typedef-name
1444 (_Bool and _Complex are new in C99.)
1446 C90 6.5.3, C99 6.7.3:
1448 type-qualifier:
1449 const
1450 restrict
1451 volatile
1453 (restrict is new in C99.)
1455 GNU extensions:
1457 declaration-specifiers:
1458 attributes declaration-specifiers[opt]
1460 storage-class-specifier:
1461 __thread
1463 type-specifier:
1464 typeof-specifier
1465 _Decimal32
1466 _Decimal64
1467 _Decimal128
1469 Objective-C:
1471 type-specifier:
1472 class-name objc-protocol-refs[opt]
1473 typedef-name objc-protocol-refs
1474 objc-protocol-refs
1477 static void
1478 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1479 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1481 bool attrs_ok = start_attr_ok;
1482 bool seen_type = specs->type_seen_p;
1483 while (c_parser_next_token_is (parser, CPP_NAME)
1484 || c_parser_next_token_is (parser, CPP_KEYWORD)
1485 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1487 struct c_typespec t;
1488 tree attrs;
1489 if (c_parser_next_token_is (parser, CPP_NAME))
1491 tree value = c_parser_peek_token (parser)->value;
1492 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1493 /* This finishes the specifiers unless a type name is OK, it
1494 is declared as a type name and a type name hasn't yet
1495 been seen. */
1496 if (!typespec_ok || seen_type
1497 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1498 break;
1499 c_parser_consume_token (parser);
1500 seen_type = true;
1501 attrs_ok = true;
1502 if (kind == C_ID_TYPENAME
1503 && (!c_dialect_objc ()
1504 || c_parser_next_token_is_not (parser, CPP_LESS)))
1506 t.kind = ctsk_typedef;
1507 /* For a typedef name, record the meaning, not the name.
1508 In case of 'foo foo, bar;'. */
1509 t.spec = lookup_name (value);
1511 else
1513 tree proto = NULL_TREE;
1514 gcc_assert (c_dialect_objc ());
1515 t.kind = ctsk_objc;
1516 if (c_parser_next_token_is (parser, CPP_LESS))
1517 proto = c_parser_objc_protocol_refs (parser);
1518 t.spec = objc_get_protocol_qualified_type (value, proto);
1520 declspecs_add_type (specs, t);
1521 continue;
1523 if (c_parser_next_token_is (parser, CPP_LESS))
1525 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1526 nisse@lysator.liu.se. */
1527 tree proto;
1528 gcc_assert (c_dialect_objc ());
1529 if (!typespec_ok || seen_type)
1530 break;
1531 proto = c_parser_objc_protocol_refs (parser);
1532 t.kind = ctsk_objc;
1533 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1534 declspecs_add_type (specs, t);
1535 continue;
1537 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1538 switch (c_parser_peek_token (parser)->keyword)
1540 case RID_STATIC:
1541 case RID_EXTERN:
1542 case RID_REGISTER:
1543 case RID_TYPEDEF:
1544 case RID_INLINE:
1545 case RID_AUTO:
1546 case RID_THREAD:
1547 if (!scspec_ok)
1548 goto out;
1549 attrs_ok = true;
1550 /* TODO: Distinguish between function specifiers (inline)
1551 and storage class specifiers, either here or in
1552 declspecs_add_scspec. */
1553 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1554 c_parser_consume_token (parser);
1555 break;
1556 case RID_UNSIGNED:
1557 case RID_LONG:
1558 case RID_SHORT:
1559 case RID_SIGNED:
1560 case RID_COMPLEX:
1561 case RID_INT:
1562 case RID_CHAR:
1563 case RID_FLOAT:
1564 case RID_DOUBLE:
1565 case RID_VOID:
1566 case RID_DFLOAT32:
1567 case RID_DFLOAT64:
1568 case RID_DFLOAT128:
1569 case RID_BOOL:
1570 if (!typespec_ok)
1571 goto out;
1572 attrs_ok = true;
1573 seen_type = true;
1574 OBJC_NEED_RAW_IDENTIFIER (1);
1575 t.kind = ctsk_resword;
1576 t.spec = c_parser_peek_token (parser)->value;
1577 declspecs_add_type (specs, t);
1578 c_parser_consume_token (parser);
1579 break;
1580 case RID_ENUM:
1581 if (!typespec_ok)
1582 goto out;
1583 attrs_ok = true;
1584 seen_type = true;
1585 t = c_parser_enum_specifier (parser);
1586 declspecs_add_type (specs, t);
1587 break;
1588 case RID_STRUCT:
1589 case RID_UNION:
1590 if (!typespec_ok)
1591 goto out;
1592 attrs_ok = true;
1593 seen_type = true;
1594 t = c_parser_struct_or_union_specifier (parser);
1595 declspecs_add_type (specs, t);
1596 break;
1597 case RID_TYPEOF:
1598 /* ??? The old parser rejected typeof after other type
1599 specifiers, but is a syntax error the best way of
1600 handling this? */
1601 if (!typespec_ok || seen_type)
1602 goto out;
1603 attrs_ok = true;
1604 seen_type = true;
1605 t = c_parser_typeof_specifier (parser);
1606 declspecs_add_type (specs, t);
1607 break;
1608 case RID_CONST:
1609 case RID_VOLATILE:
1610 case RID_RESTRICT:
1611 attrs_ok = true;
1612 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1613 c_parser_consume_token (parser);
1614 break;
1615 case RID_ATTRIBUTE:
1616 if (!attrs_ok)
1617 goto out;
1618 attrs = c_parser_attributes (parser);
1619 declspecs_add_attrs (specs, attrs);
1620 break;
1621 default:
1622 goto out;
1625 out: ;
1628 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1630 enum-specifier:
1631 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1632 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1633 enum attributes[opt] identifier
1635 The form with trailing comma is new in C99. The forms with
1636 attributes are GNU extensions. In GNU C, we accept any expression
1637 without commas in the syntax (assignment expressions, not just
1638 conditional expressions); assignment expressions will be diagnosed
1639 as non-constant.
1641 enumerator-list:
1642 enumerator
1643 enumerator-list , enumerator
1645 enumerator:
1646 enumeration-constant
1647 enumeration-constant = constant-expression
1650 static struct c_typespec
1651 c_parser_enum_specifier (c_parser *parser)
1653 struct c_typespec ret;
1654 tree attrs;
1655 tree ident = NULL_TREE;
1656 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1657 c_parser_consume_token (parser);
1658 attrs = c_parser_attributes (parser);
1659 if (c_parser_next_token_is (parser, CPP_NAME))
1661 ident = c_parser_peek_token (parser)->value;
1662 c_parser_consume_token (parser);
1664 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1666 /* Parse an enum definition. */
1667 tree type = start_enum (ident);
1668 tree postfix_attrs;
1669 /* We chain the enumerators in reverse order, then put them in
1670 forward order at the end. */
1671 tree values = NULL_TREE;
1672 c_parser_consume_token (parser);
1673 while (true)
1675 tree enum_id;
1676 tree enum_value;
1677 tree enum_decl;
1678 bool seen_comma;
1679 if (c_parser_next_token_is_not (parser, CPP_NAME))
1681 c_parser_error (parser, "expected identifier");
1682 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1683 values = error_mark_node;
1684 break;
1686 enum_id = c_parser_peek_token (parser)->value;
1687 c_parser_consume_token (parser);
1688 if (c_parser_next_token_is (parser, CPP_EQ))
1690 c_parser_consume_token (parser);
1691 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1693 else
1694 enum_value = NULL_TREE;
1695 enum_decl = build_enumerator (enum_id, enum_value);
1696 TREE_CHAIN (enum_decl) = values;
1697 values = enum_decl;
1698 seen_comma = false;
1699 if (c_parser_next_token_is (parser, CPP_COMMA))
1701 seen_comma = true;
1702 c_parser_consume_token (parser);
1704 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1706 if (seen_comma && pedantic && !flag_isoc99)
1707 pedwarn ("comma at end of enumerator list");
1708 c_parser_consume_token (parser);
1709 break;
1711 if (!seen_comma)
1713 c_parser_error (parser, "expected %<,%> or %<}%>");
1714 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1715 values = error_mark_node;
1716 break;
1719 postfix_attrs = c_parser_attributes (parser);
1720 ret.spec = finish_enum (type, nreverse (values),
1721 chainon (attrs, postfix_attrs));
1722 ret.kind = ctsk_tagdef;
1723 return ret;
1725 else if (!ident)
1727 c_parser_error (parser, "expected %<{%>");
1728 ret.spec = error_mark_node;
1729 ret.kind = ctsk_tagref;
1730 return ret;
1732 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1733 /* In ISO C, enumerated types can be referred to only if already
1734 defined. */
1735 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1736 pedwarn ("ISO C forbids forward references to %<enum%> types");
1737 return ret;
1740 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1742 struct-or-union-specifier:
1743 struct-or-union attributes[opt] identifier[opt]
1744 { struct-contents } attributes[opt]
1745 struct-or-union attributes[opt] identifier
1747 struct-contents:
1748 struct-declaration-list
1750 struct-declaration-list:
1751 struct-declaration ;
1752 struct-declaration-list struct-declaration ;
1754 GNU extensions:
1756 struct-contents:
1757 empty
1758 struct-declaration
1759 struct-declaration-list struct-declaration
1761 struct-declaration-list:
1762 struct-declaration-list ;
1765 (Note that in the syntax here, unlike that in ISO C, the semicolons
1766 are included here rather than in struct-declaration, in order to
1767 describe the syntax with extra semicolons and missing semicolon at
1768 end.)
1770 Objective-C:
1772 struct-declaration-list:
1773 @defs ( class-name )
1775 (Note this does not include a trailing semicolon, but can be
1776 followed by further declarations, and gets a pedwarn-if-pedantic
1777 when followed by a semicolon.) */
1779 static struct c_typespec
1780 c_parser_struct_or_union_specifier (c_parser *parser)
1782 struct c_typespec ret;
1783 tree attrs;
1784 tree ident = NULL_TREE;
1785 enum tree_code code;
1786 switch (c_parser_peek_token (parser)->keyword)
1788 case RID_STRUCT:
1789 code = RECORD_TYPE;
1790 break;
1791 case RID_UNION:
1792 code = UNION_TYPE;
1793 break;
1794 default:
1795 gcc_unreachable ();
1797 c_parser_consume_token (parser);
1798 attrs = c_parser_attributes (parser);
1799 if (c_parser_next_token_is (parser, CPP_NAME))
1801 ident = c_parser_peek_token (parser)->value;
1802 c_parser_consume_token (parser);
1804 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1806 /* Parse a struct or union definition. Start the scope of the
1807 tag before parsing components. */
1808 tree type = start_struct (code, ident);
1809 tree postfix_attrs;
1810 /* We chain the components in reverse order, then put them in
1811 forward order at the end. Each struct-declaration may
1812 declare multiple components (comma-separated), so we must use
1813 chainon to join them, although when parsing each
1814 struct-declaration we can use TREE_CHAIN directly.
1816 The theory behind all this is that there will be more
1817 semicolon separated fields than comma separated fields, and
1818 so we'll be minimizing the number of node traversals required
1819 by chainon. */
1820 tree contents = NULL_TREE;
1821 c_parser_consume_token (parser);
1822 /* Handle the Objective-C @defs construct,
1823 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1824 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1826 tree name;
1827 gcc_assert (c_dialect_objc ());
1828 c_parser_consume_token (parser);
1829 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1830 goto end_at_defs;
1831 if (c_parser_next_token_is (parser, CPP_NAME)
1832 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1834 name = c_parser_peek_token (parser)->value;
1835 c_parser_consume_token (parser);
1837 else
1839 c_parser_error (parser, "expected class name");
1840 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1841 goto end_at_defs;
1843 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1844 "expected %<)%>");
1845 contents = nreverse (objc_get_class_ivars (name));
1847 end_at_defs:
1848 /* Parse the struct-declarations and semicolons. Problems with
1849 semicolons are diagnosed here; empty structures are diagnosed
1850 elsewhere. */
1851 while (true)
1853 tree decls;
1854 /* Parse any stray semicolon. */
1855 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1857 if (pedantic)
1858 pedwarn ("extra semicolon in struct or union specified");
1859 c_parser_consume_token (parser);
1860 continue;
1862 /* Stop if at the end of the struct or union contents. */
1863 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1865 c_parser_consume_token (parser);
1866 break;
1868 /* Accept #pragmas at struct scope. */
1869 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1871 c_parser_pragma (parser, pragma_external);
1872 continue;
1874 /* Parse some comma-separated declarations, but not the
1875 trailing semicolon if any. */
1876 decls = c_parser_struct_declaration (parser);
1877 contents = chainon (decls, contents);
1878 /* If no semicolon follows, either we have a parse error or
1879 are at the end of the struct or union and should
1880 pedwarn. */
1881 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1882 c_parser_consume_token (parser);
1883 else
1885 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1886 pedwarn ("no semicolon at end of struct or union");
1887 else
1889 c_parser_error (parser, "expected %<;%>");
1890 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1891 break;
1895 postfix_attrs = c_parser_attributes (parser);
1896 ret.spec = finish_struct (type, nreverse (contents),
1897 chainon (attrs, postfix_attrs));
1898 ret.kind = ctsk_tagdef;
1899 return ret;
1901 else if (!ident)
1903 c_parser_error (parser, "expected %<{%>");
1904 ret.spec = error_mark_node;
1905 ret.kind = ctsk_tagref;
1906 return ret;
1908 ret = parser_xref_tag (code, ident);
1909 return ret;
1912 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1913 the trailing semicolon.
1915 struct-declaration:
1916 specifier-qualifier-list struct-declarator-list
1918 specifier-qualifier-list:
1919 type-specifier specifier-qualifier-list[opt]
1920 type-qualifier specifier-qualifier-list[opt]
1921 attributes specifier-qualifier-list[opt]
1923 struct-declarator-list:
1924 struct-declarator
1925 struct-declarator-list , attributes[opt] struct-declarator
1927 struct-declarator:
1928 declarator attributes[opt]
1929 declarator[opt] : constant-expression attributes[opt]
1931 GNU extensions:
1933 struct-declaration:
1934 __extension__ struct-declaration
1935 specifier-qualifier-list
1937 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1938 of attributes where shown is a GNU extension. In GNU C, we accept
1939 any expression without commas in the syntax (assignment
1940 expressions, not just conditional expressions); assignment
1941 expressions will be diagnosed as non-constant. */
1943 static tree
1944 c_parser_struct_declaration (c_parser *parser)
1946 struct c_declspecs *specs;
1947 tree prefix_attrs;
1948 tree all_prefix_attrs;
1949 tree decls;
1950 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1952 int ext;
1953 tree decl;
1954 ext = disable_extension_diagnostics ();
1955 c_parser_consume_token (parser);
1956 decl = c_parser_struct_declaration (parser);
1957 restore_extension_diagnostics (ext);
1958 return decl;
1960 specs = build_null_declspecs ();
1961 c_parser_declspecs (parser, specs, false, true, true);
1962 if (parser->error)
1963 return NULL_TREE;
1964 if (!specs->declspecs_seen_p)
1966 c_parser_error (parser, "expected specifier-qualifier-list");
1967 return NULL_TREE;
1969 finish_declspecs (specs);
1970 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1972 tree ret;
1973 if (!specs->type_seen_p)
1975 if (pedantic)
1976 pedwarn ("ISO C forbids member declarations with no members");
1977 shadow_tag_warned (specs, pedantic);
1978 ret = NULL_TREE;
1980 else
1982 /* Support for unnamed structs or unions as members of
1983 structs or unions (which is [a] useful and [b] supports
1984 MS P-SDK). */
1985 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1987 return ret;
1989 pending_xref_error ();
1990 prefix_attrs = specs->attrs;
1991 all_prefix_attrs = prefix_attrs;
1992 specs->attrs = NULL_TREE;
1993 decls = NULL_TREE;
1994 while (true)
1996 /* Declaring one or more declarators or un-named bit-fields. */
1997 struct c_declarator *declarator;
1998 bool dummy = false;
1999 if (c_parser_next_token_is (parser, CPP_COLON))
2000 declarator = build_id_declarator (NULL_TREE);
2001 else
2002 declarator = c_parser_declarator (parser, specs->type_seen_p,
2003 C_DTR_NORMAL, &dummy);
2004 if (declarator == NULL)
2006 c_parser_skip_to_end_of_block_or_statement (parser);
2007 break;
2009 if (c_parser_next_token_is (parser, CPP_COLON)
2010 || c_parser_next_token_is (parser, CPP_COMMA)
2011 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2012 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2013 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2015 tree postfix_attrs = NULL_TREE;
2016 tree width = NULL_TREE;
2017 tree d;
2018 if (c_parser_next_token_is (parser, CPP_COLON))
2020 c_parser_consume_token (parser);
2021 width = c_parser_expr_no_commas (parser, NULL).value;
2023 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2024 postfix_attrs = c_parser_attributes (parser);
2025 d = grokfield (declarator, specs, width);
2026 decl_attributes (&d, chainon (postfix_attrs,
2027 all_prefix_attrs), 0);
2028 TREE_CHAIN (d) = decls;
2029 decls = d;
2030 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2031 all_prefix_attrs = chainon (c_parser_attributes (parser),
2032 prefix_attrs);
2033 else
2034 all_prefix_attrs = prefix_attrs;
2035 if (c_parser_next_token_is (parser, CPP_COMMA))
2036 c_parser_consume_token (parser);
2037 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2038 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2040 /* Semicolon consumed in caller. */
2041 break;
2043 else
2045 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2046 break;
2049 else
2051 c_parser_error (parser,
2052 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2053 "%<__attribute__%>");
2054 break;
2057 return decls;
2060 /* Parse a typeof specifier (a GNU extension).
2062 typeof-specifier:
2063 typeof ( expression )
2064 typeof ( type-name )
2067 static struct c_typespec
2068 c_parser_typeof_specifier (c_parser *parser)
2070 struct c_typespec ret;
2071 ret.kind = ctsk_typeof;
2072 ret.spec = error_mark_node;
2073 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2074 c_parser_consume_token (parser);
2075 skip_evaluation++;
2076 in_typeof++;
2077 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2079 skip_evaluation--;
2080 in_typeof--;
2081 return ret;
2083 if (c_parser_next_token_starts_typename (parser))
2085 struct c_type_name *type = c_parser_type_name (parser);
2086 skip_evaluation--;
2087 in_typeof--;
2088 if (type != NULL)
2090 ret.spec = groktypename (type);
2091 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2094 else
2096 struct c_expr expr = c_parser_expression (parser);
2097 skip_evaluation--;
2098 in_typeof--;
2099 if (TREE_CODE (expr.value) == COMPONENT_REF
2100 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2101 error ("%<typeof%> applied to a bit-field");
2102 ret.spec = TREE_TYPE (expr.value);
2103 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2106 return ret;
2109 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2110 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2111 be redeclared; otherwise it may not. KIND indicates which kind of
2112 declarator is wanted. Returns a valid declarator except in the
2113 case of a syntax error in which case NULL is returned. *SEEN_ID is
2114 set to true if an identifier being declared is seen; this is used
2115 to diagnose bad forms of abstract array declarators and to
2116 determine whether an identifier list is syntactically permitted.
2118 declarator:
2119 pointer[opt] direct-declarator
2121 direct-declarator:
2122 identifier
2123 ( attributes[opt] declarator )
2124 direct-declarator array-declarator
2125 direct-declarator ( parameter-type-list )
2126 direct-declarator ( identifier-list[opt] )
2128 pointer:
2129 * type-qualifier-list[opt]
2130 * type-qualifier-list[opt] pointer
2132 type-qualifier-list:
2133 type-qualifier
2134 attributes
2135 type-qualifier-list type-qualifier
2136 type-qualifier-list attributes
2138 parameter-type-list:
2139 parameter-list
2140 parameter-list , ...
2142 parameter-list:
2143 parameter-declaration
2144 parameter-list , parameter-declaration
2146 parameter-declaration:
2147 declaration-specifiers declarator attributes[opt]
2148 declaration-specifiers abstract-declarator[opt] attributes[opt]
2150 identifier-list:
2151 identifier
2152 identifier-list , identifier
2154 abstract-declarator:
2155 pointer
2156 pointer[opt] direct-abstract-declarator
2158 direct-abstract-declarator:
2159 ( attributes[opt] abstract-declarator )
2160 direct-abstract-declarator[opt] array-declarator
2161 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2163 GNU extensions:
2165 direct-declarator:
2166 direct-declarator ( parameter-forward-declarations
2167 parameter-type-list[opt] )
2169 direct-abstract-declarator:
2170 direct-abstract-declarator[opt] ( parameter-forward-declarations
2171 parameter-type-list[opt] )
2173 parameter-forward-declarations:
2174 parameter-list ;
2175 parameter-forward-declarations parameter-list ;
2177 The uses of attributes shown above are GNU extensions.
2179 Some forms of array declarator are not included in C99 in the
2180 syntax for abstract declarators; these are disallowed elsewhere.
2181 This may be a defect (DR#289).
2183 This function also accepts an omitted abstract declarator as being
2184 an abstract declarator, although not part of the formal syntax. */
2186 static struct c_declarator *
2187 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2188 bool *seen_id)
2190 /* Parse any initial pointer part. */
2191 if (c_parser_next_token_is (parser, CPP_MULT))
2193 struct c_declspecs *quals_attrs = build_null_declspecs ();
2194 struct c_declarator *inner;
2195 c_parser_consume_token (parser);
2196 c_parser_declspecs (parser, quals_attrs, false, false, true);
2197 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2198 if (inner == NULL)
2199 return NULL;
2200 else
2201 return make_pointer_declarator (quals_attrs, inner);
2203 /* Now we have a direct declarator, direct abstract declarator or
2204 nothing (which counts as a direct abstract declarator here). */
2205 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2208 /* Parse a direct declarator or direct abstract declarator; arguments
2209 as c_parser_declarator. */
2211 static struct c_declarator *
2212 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2213 bool *seen_id)
2215 /* The direct declarator must start with an identifier (possibly
2216 omitted) or a parenthesized declarator (possibly abstract). In
2217 an ordinary declarator, initial parentheses must start a
2218 parenthesized declarator. In an abstract declarator or parameter
2219 declarator, they could start a parenthesized declarator or a
2220 parameter list. To tell which, the open parenthesis and any
2221 following attributes must be read. If a declaration specifier
2222 follows, then it is a parameter list; if the specifier is a
2223 typedef name, there might be an ambiguity about redeclaring it,
2224 which is resolved in the direction of treating it as a typedef
2225 name. If a close parenthesis follows, it is also an empty
2226 parameter list, as the syntax does not permit empty abstract
2227 declarators. Otherwise, it is a parenthesized declarator (in
2228 which case the analysis may be repeated inside it, recursively).
2230 ??? There is an ambiguity in a parameter declaration "int
2231 (__attribute__((foo)) x)", where x is not a typedef name: it
2232 could be an abstract declarator for a function, or declare x with
2233 parentheses. The proper resolution of this ambiguity needs
2234 documenting. At present we follow an accident of the old
2235 parser's implementation, whereby the first parameter must have
2236 some declaration specifiers other than just attributes. Thus as
2237 a parameter declaration it is treated as a parenthesized
2238 parameter named x, and as an abstract declarator it is
2239 rejected.
2241 ??? Also following the old parser, attributes inside an empty
2242 parameter list are ignored, making it a list not yielding a
2243 prototype, rather than giving an error or making it have one
2244 parameter with implicit type int.
2246 ??? Also following the old parser, typedef names may be
2247 redeclared in declarators, but not Objective-C class names. */
2249 if (kind != C_DTR_ABSTRACT
2250 && c_parser_next_token_is (parser, CPP_NAME)
2251 && ((type_seen_p
2252 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2253 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2255 struct c_declarator *inner
2256 = build_id_declarator (c_parser_peek_token (parser)->value);
2257 *seen_id = true;
2258 inner->id_loc = c_parser_peek_token (parser)->location;
2259 c_parser_consume_token (parser);
2260 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2263 if (kind != C_DTR_NORMAL
2264 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2266 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2267 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2270 /* Either we are at the end of an abstract declarator, or we have
2271 parentheses. */
2273 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2275 tree attrs;
2276 struct c_declarator *inner;
2277 c_parser_consume_token (parser);
2278 attrs = c_parser_attributes (parser);
2279 if (kind != C_DTR_NORMAL
2280 && (c_parser_next_token_starts_declspecs (parser)
2281 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2283 struct c_arg_info *args
2284 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2285 attrs);
2286 if (args == NULL)
2287 return NULL;
2288 else
2290 inner
2291 = build_function_declarator (args,
2292 build_id_declarator (NULL_TREE));
2293 return c_parser_direct_declarator_inner (parser, *seen_id,
2294 inner);
2297 /* A parenthesized declarator. */
2298 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2299 if (inner != NULL && attrs != NULL)
2300 inner = build_attrs_declarator (attrs, inner);
2301 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2303 c_parser_consume_token (parser);
2304 if (inner == NULL)
2305 return NULL;
2306 else
2307 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2309 else
2311 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2312 "expected %<)%>");
2313 return NULL;
2316 else
2318 if (kind == C_DTR_NORMAL)
2320 c_parser_error (parser, "expected identifier or %<(%>");
2321 return NULL;
2323 else
2324 return build_id_declarator (NULL_TREE);
2328 /* Parse part of a direct declarator or direct abstract declarator,
2329 given that some (in INNER) has already been parsed; ID_PRESENT is
2330 true if an identifier is present, false for an abstract
2331 declarator. */
2333 static struct c_declarator *
2334 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2335 struct c_declarator *inner)
2337 /* Parse a sequence of array declarators and parameter lists. */
2338 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2340 struct c_declarator *declarator;
2341 struct c_declspecs *quals_attrs = build_null_declspecs ();
2342 bool static_seen;
2343 bool star_seen;
2344 tree dimen;
2345 c_parser_consume_token (parser);
2346 c_parser_declspecs (parser, quals_attrs, false, false, true);
2347 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2348 if (static_seen)
2349 c_parser_consume_token (parser);
2350 if (static_seen && !quals_attrs->declspecs_seen_p)
2351 c_parser_declspecs (parser, quals_attrs, false, false, true);
2352 if (!quals_attrs->declspecs_seen_p)
2353 quals_attrs = NULL;
2354 /* If "static" is present, there must be an array dimension.
2355 Otherwise, there may be a dimension, "*", or no
2356 dimension. */
2357 if (static_seen)
2359 star_seen = false;
2360 dimen = c_parser_expr_no_commas (parser, NULL).value;
2362 else
2364 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2366 dimen = NULL_TREE;
2367 star_seen = false;
2369 else if (c_parser_next_token_is (parser, CPP_MULT))
2371 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2373 dimen = NULL_TREE;
2374 star_seen = true;
2375 c_parser_consume_token (parser);
2377 else
2379 star_seen = false;
2380 dimen = c_parser_expr_no_commas (parser, NULL).value;
2383 else
2385 star_seen = false;
2386 dimen = c_parser_expr_no_commas (parser, NULL).value;
2389 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2390 c_parser_consume_token (parser);
2391 else
2393 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2394 "expected %<]%>");
2395 return NULL;
2397 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2398 star_seen);
2399 inner = set_array_declarator_inner (declarator, inner, !id_present);
2400 return c_parser_direct_declarator_inner (parser, id_present, inner);
2402 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2404 tree attrs;
2405 struct c_arg_info *args;
2406 c_parser_consume_token (parser);
2407 attrs = c_parser_attributes (parser);
2408 args = c_parser_parms_declarator (parser, id_present, attrs);
2409 if (args == NULL)
2410 return NULL;
2411 else
2413 inner = build_function_declarator (args, inner);
2414 return c_parser_direct_declarator_inner (parser, id_present, inner);
2417 return inner;
2420 /* Parse a parameter list or identifier list, including the closing
2421 parenthesis but not the opening one. ATTRS are the attributes at
2422 the start of the list. ID_LIST_OK is true if an identifier list is
2423 acceptable; such a list must not have attributes at the start. */
2425 static struct c_arg_info *
2426 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2428 push_scope ();
2429 declare_parm_level ();
2430 /* If the list starts with an identifier, it is an identifier list.
2431 Otherwise, it is either a prototype list or an empty list. */
2432 if (id_list_ok
2433 && !attrs
2434 && c_parser_next_token_is (parser, CPP_NAME)
2435 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2437 tree list = NULL_TREE, *nextp = &list;
2438 while (c_parser_next_token_is (parser, CPP_NAME)
2439 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2441 *nextp = build_tree_list (NULL_TREE,
2442 c_parser_peek_token (parser)->value);
2443 nextp = & TREE_CHAIN (*nextp);
2444 c_parser_consume_token (parser);
2445 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2446 break;
2447 c_parser_consume_token (parser);
2448 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2450 c_parser_error (parser, "expected identifier");
2451 break;
2454 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2456 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2457 ret->parms = 0;
2458 ret->tags = 0;
2459 ret->types = list;
2460 ret->others = 0;
2461 c_parser_consume_token (parser);
2462 pop_scope ();
2463 return ret;
2465 else
2467 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2468 "expected %<)%>");
2469 pop_scope ();
2470 return NULL;
2473 else
2475 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2476 pop_scope ();
2477 return ret;
2481 /* Parse a parameter list (possibly empty), including the closing
2482 parenthesis but not the opening one. ATTRS are the attributes at
2483 the start of the list. */
2485 static struct c_arg_info *
2486 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2488 bool good_parm = false;
2489 /* ??? Following the old parser, forward parameter declarations may
2490 use abstract declarators, and if no real parameter declarations
2491 follow the forward declarations then this is not diagnosed. Also
2492 note as above that attributes are ignored as the only contents of
2493 the parentheses, or as the only contents after forward
2494 declarations. */
2495 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2497 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2498 ret->parms = 0;
2499 ret->tags = 0;
2500 ret->types = 0;
2501 ret->others = 0;
2502 c_parser_consume_token (parser);
2503 return ret;
2505 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2507 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2508 ret->parms = 0;
2509 ret->tags = 0;
2510 ret->others = 0;
2511 /* Suppress -Wold-style-definition for this case. */
2512 ret->types = error_mark_node;
2513 error ("ISO C requires a named argument before %<...%>");
2514 c_parser_consume_token (parser);
2515 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2517 c_parser_consume_token (parser);
2518 return ret;
2520 else
2522 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2523 "expected %<)%>");
2524 return NULL;
2527 /* Nonempty list of parameters, either terminated with semicolon
2528 (forward declarations; recurse) or with close parenthesis (normal
2529 function) or with ", ... )" (variadic function). */
2530 while (true)
2532 /* Parse a parameter. */
2533 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2534 attrs = NULL_TREE;
2535 if (parm != NULL)
2537 good_parm = true;
2538 push_parm_decl (parm);
2540 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2542 tree new_attrs;
2543 c_parser_consume_token (parser);
2544 mark_forward_parm_decls ();
2545 new_attrs = c_parser_attributes (parser);
2546 return c_parser_parms_list_declarator (parser, new_attrs);
2548 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2550 c_parser_consume_token (parser);
2551 if (good_parm)
2552 return get_parm_info (false);
2553 else
2555 struct c_arg_info *ret
2556 = XOBNEW (&parser_obstack, struct c_arg_info);
2557 ret->parms = 0;
2558 ret->tags = 0;
2559 ret->types = 0;
2560 ret->others = 0;
2561 return ret;
2564 if (!c_parser_require (parser, CPP_COMMA,
2565 "expected %<;%>, %<,%> or %<)%>"))
2567 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2568 return NULL;
2570 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2572 c_parser_consume_token (parser);
2573 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2575 c_parser_consume_token (parser);
2576 if (good_parm)
2577 return get_parm_info (true);
2578 else
2580 struct c_arg_info *ret
2581 = XOBNEW (&parser_obstack, struct c_arg_info);
2582 ret->parms = 0;
2583 ret->tags = 0;
2584 ret->types = 0;
2585 ret->others = 0;
2586 return ret;
2589 else
2591 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2592 "expected %<)%>");
2593 return NULL;
2599 /* Parse a parameter declaration. ATTRS are the attributes at the
2600 start of the declaration if it is the first parameter. */
2602 static struct c_parm *
2603 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2605 struct c_declspecs *specs;
2606 struct c_declarator *declarator;
2607 tree prefix_attrs;
2608 tree postfix_attrs = NULL_TREE;
2609 bool dummy = false;
2610 if (!c_parser_next_token_starts_declspecs (parser))
2612 /* ??? In some Objective-C cases '...' isn't applicable so there
2613 should be a different message. */
2614 c_parser_error (parser,
2615 "expected declaration specifiers or %<...%>");
2616 c_parser_skip_to_end_of_parameter (parser);
2617 return NULL;
2619 specs = build_null_declspecs ();
2620 if (attrs)
2622 declspecs_add_attrs (specs, attrs);
2623 attrs = NULL_TREE;
2625 c_parser_declspecs (parser, specs, true, true, true);
2626 finish_declspecs (specs);
2627 pending_xref_error ();
2628 prefix_attrs = specs->attrs;
2629 specs->attrs = NULL_TREE;
2630 declarator = c_parser_declarator (parser, specs->type_seen_p,
2631 C_DTR_PARM, &dummy);
2632 if (declarator == NULL)
2634 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2635 return NULL;
2637 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2638 postfix_attrs = c_parser_attributes (parser);
2639 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2640 declarator);
2643 /* Parse a string literal in an asm expression. It should not be
2644 translated, and wide string literals are an error although
2645 permitted by the syntax. This is a GNU extension.
2647 asm-string-literal:
2648 string-literal
2650 ??? At present, following the old parser, the caller needs to have
2651 set c_lex_string_translate to 0. It would be better to follow the
2652 C++ parser rather than using the c_lex_string_translate kludge. */
2654 static tree
2655 c_parser_asm_string_literal (c_parser *parser)
2657 tree str;
2658 if (c_parser_next_token_is (parser, CPP_STRING))
2660 str = c_parser_peek_token (parser)->value;
2661 c_parser_consume_token (parser);
2663 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2665 error ("wide string literal in %<asm%>");
2666 str = build_string (1, "");
2667 c_parser_consume_token (parser);
2669 else
2671 c_parser_error (parser, "expected string literal");
2672 str = NULL_TREE;
2674 return str;
2677 /* Parse a simple asm expression. This is used in restricted
2678 contexts, where a full expression with inputs and outputs does not
2679 make sense. This is a GNU extension.
2681 simple-asm-expr:
2682 asm ( asm-string-literal )
2685 static tree
2686 c_parser_simple_asm_expr (c_parser *parser)
2688 tree str;
2689 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2690 /* ??? Follow the C++ parser rather than using the
2691 c_lex_string_translate kludge. */
2692 c_lex_string_translate = 0;
2693 c_parser_consume_token (parser);
2694 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2696 c_lex_string_translate = 1;
2697 return NULL_TREE;
2699 str = c_parser_asm_string_literal (parser);
2700 c_lex_string_translate = 1;
2701 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2703 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2704 return NULL_TREE;
2706 return str;
2709 /* Parse (possibly empty) attributes. This is a GNU extension.
2711 attributes:
2712 empty
2713 attributes attribute
2715 attribute:
2716 __attribute__ ( ( attribute-list ) )
2718 attribute-list:
2719 attrib
2720 attribute_list , attrib
2722 attrib:
2723 empty
2724 any-word
2725 any-word ( identifier )
2726 any-word ( identifier , nonempty-expr-list )
2727 any-word ( expr-list )
2729 where the "identifier" must not be declared as a type, and
2730 "any-word" may be any identifier (including one declared as a
2731 type), a reserved word storage class specifier, type specifier or
2732 type qualifier. ??? This still leaves out most reserved keywords
2733 (following the old parser), shouldn't we include them, and why not
2734 allow identifiers declared as types to start the arguments? */
2736 static tree
2737 c_parser_attributes (c_parser *parser)
2739 tree attrs = NULL_TREE;
2740 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2742 /* ??? Follow the C++ parser rather than using the
2743 c_lex_string_translate kludge. */
2744 c_lex_string_translate = 0;
2745 c_parser_consume_token (parser);
2746 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2748 c_lex_string_translate = 1;
2749 return attrs;
2751 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2753 c_lex_string_translate = 1;
2754 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2755 return attrs;
2757 /* Parse the attribute list. */
2758 while (c_parser_next_token_is (parser, CPP_COMMA)
2759 || c_parser_next_token_is (parser, CPP_NAME)
2760 || c_parser_next_token_is (parser, CPP_KEYWORD))
2762 tree attr, attr_name, attr_args;
2763 if (c_parser_next_token_is (parser, CPP_COMMA))
2765 c_parser_consume_token (parser);
2766 continue;
2768 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2770 /* ??? See comment above about what keywords are
2771 accepted here. */
2772 bool ok;
2773 switch (c_parser_peek_token (parser)->keyword)
2775 case RID_STATIC:
2776 case RID_UNSIGNED:
2777 case RID_LONG:
2778 case RID_CONST:
2779 case RID_EXTERN:
2780 case RID_REGISTER:
2781 case RID_TYPEDEF:
2782 case RID_SHORT:
2783 case RID_INLINE:
2784 case RID_VOLATILE:
2785 case RID_SIGNED:
2786 case RID_AUTO:
2787 case RID_RESTRICT:
2788 case RID_COMPLEX:
2789 case RID_THREAD:
2790 case RID_INT:
2791 case RID_CHAR:
2792 case RID_FLOAT:
2793 case RID_DOUBLE:
2794 case RID_VOID:
2795 case RID_DFLOAT32:
2796 case RID_DFLOAT64:
2797 case RID_DFLOAT128:
2798 case RID_BOOL:
2799 ok = true;
2800 break;
2801 default:
2802 ok = false;
2803 break;
2805 if (!ok)
2806 break;
2808 attr_name = c_parser_peek_token (parser)->value;
2809 c_parser_consume_token (parser);
2810 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2812 attr = build_tree_list (attr_name, NULL_TREE);
2813 attrs = chainon (attrs, attr);
2814 continue;
2816 c_parser_consume_token (parser);
2817 /* Parse the attribute contents. If they start with an
2818 identifier which is followed by a comma or close
2819 parenthesis, then the arguments start with that
2820 identifier; otherwise they are an expression list. */
2821 if (c_parser_next_token_is (parser, CPP_NAME)
2822 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2823 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2824 || (c_parser_peek_2nd_token (parser)->type
2825 == CPP_CLOSE_PAREN)))
2827 tree arg1 = c_parser_peek_token (parser)->value;
2828 c_parser_consume_token (parser);
2829 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2830 attr_args = build_tree_list (NULL_TREE, arg1);
2831 else
2833 c_parser_consume_token (parser);
2834 attr_args = tree_cons (NULL_TREE, arg1,
2835 c_parser_expr_list (parser, false));
2838 else
2840 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2841 attr_args = NULL_TREE;
2842 else
2843 attr_args = c_parser_expr_list (parser, false);
2845 attr = build_tree_list (attr_name, attr_args);
2846 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2847 c_parser_consume_token (parser);
2848 else
2850 c_lex_string_translate = 1;
2851 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2852 "expected %<)%>");
2853 return attrs;
2855 attrs = chainon (attrs, attr);
2857 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2858 c_parser_consume_token (parser);
2859 else
2861 c_lex_string_translate = 1;
2862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2863 "expected %<)%>");
2864 return attrs;
2866 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2867 c_parser_consume_token (parser);
2868 else
2870 c_lex_string_translate = 1;
2871 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2872 "expected %<)%>");
2873 return attrs;
2875 c_lex_string_translate = 1;
2877 return attrs;
2880 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2882 type-name:
2883 specifier-qualifier-list abstract-declarator[opt]
2886 static struct c_type_name *
2887 c_parser_type_name (c_parser *parser)
2889 struct c_declspecs *specs = build_null_declspecs ();
2890 struct c_declarator *declarator;
2891 struct c_type_name *ret;
2892 bool dummy = false;
2893 c_parser_declspecs (parser, specs, false, true, true);
2894 if (!specs->declspecs_seen_p)
2896 c_parser_error (parser, "expected specifier-qualifier-list");
2897 return NULL;
2899 pending_xref_error ();
2900 finish_declspecs (specs);
2901 declarator = c_parser_declarator (parser, specs->type_seen_p,
2902 C_DTR_ABSTRACT, &dummy);
2903 if (declarator == NULL)
2904 return NULL;
2905 ret = XOBNEW (&parser_obstack, struct c_type_name);
2906 ret->specs = specs;
2907 ret->declarator = declarator;
2908 return ret;
2911 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2913 initializer:
2914 assignment-expression
2915 { initializer-list }
2916 { initializer-list , }
2918 initializer-list:
2919 designation[opt] initializer
2920 initializer-list , designation[opt] initializer
2922 designation:
2923 designator-list =
2925 designator-list:
2926 designator
2927 designator-list designator
2929 designator:
2930 array-designator
2931 . identifier
2933 array-designator:
2934 [ constant-expression ]
2936 GNU extensions:
2938 initializer:
2941 designation:
2942 array-designator
2943 identifier :
2945 array-designator:
2946 [ constant-expression ... constant-expression ]
2948 Any expression without commas is accepted in the syntax for the
2949 constant-expressions, with non-constant expressions rejected later.
2951 This function is only used for top-level initializers; for nested
2952 ones, see c_parser_initval. */
2954 static struct c_expr
2955 c_parser_initializer (c_parser *parser)
2957 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2958 return c_parser_braced_init (parser, NULL_TREE, false);
2959 else
2961 struct c_expr ret;
2962 ret = c_parser_expr_no_commas (parser, NULL);
2963 if (TREE_CODE (ret.value) != STRING_CST
2964 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
2965 ret = default_function_array_conversion (ret);
2966 return ret;
2970 /* Parse a braced initializer list. TYPE is the type specified for a
2971 compound literal, and NULL_TREE for other initializers and for
2972 nested braced lists. NESTED_P is true for nested braced lists,
2973 false for the list of a compound literal or the list that is the
2974 top-level initializer in a declaration. */
2976 static struct c_expr
2977 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2979 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2980 c_parser_consume_token (parser);
2981 if (nested_p)
2982 push_init_level (0);
2983 else
2984 really_start_incremental_init (type);
2985 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2987 if (pedantic)
2988 pedwarn ("ISO C forbids empty initializer braces");
2990 else
2992 /* Parse a non-empty initializer list, possibly with a trailing
2993 comma. */
2994 while (true)
2996 c_parser_initelt (parser);
2997 if (parser->error)
2998 break;
2999 if (c_parser_next_token_is (parser, CPP_COMMA))
3000 c_parser_consume_token (parser);
3001 else
3002 break;
3003 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3004 break;
3007 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3009 struct c_expr ret;
3010 ret.value = error_mark_node;
3011 ret.original_code = ERROR_MARK;
3012 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3013 return ret;
3015 c_parser_consume_token (parser);
3016 return pop_init_level (0);
3019 /* Parse a nested initializer, including designators. */
3021 static void
3022 c_parser_initelt (c_parser *parser)
3024 /* Parse any designator or designator list. A single array
3025 designator may have the subsequent "=" omitted in GNU C, but a
3026 longer list or a structure member designator may not. */
3027 if (c_parser_next_token_is (parser, CPP_NAME)
3028 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3030 /* Old-style structure member designator. */
3031 set_init_label (c_parser_peek_token (parser)->value);
3032 if (pedantic)
3033 pedwarn ("obsolete use of designated initializer with %<:%>");
3034 c_parser_consume_token (parser);
3035 c_parser_consume_token (parser);
3037 else
3039 /* des_seen is 0 if there have been no designators, 1 if there
3040 has been a single array designator and 2 otherwise. */
3041 int des_seen = 0;
3042 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3043 || c_parser_next_token_is (parser, CPP_DOT))
3045 int des_prev = des_seen;
3046 if (des_seen < 2)
3047 des_seen++;
3048 if (c_parser_next_token_is (parser, CPP_DOT))
3050 des_seen = 2;
3051 c_parser_consume_token (parser);
3052 if (c_parser_next_token_is (parser, CPP_NAME))
3054 set_init_label (c_parser_peek_token (parser)->value);
3055 c_parser_consume_token (parser);
3057 else
3059 struct c_expr init;
3060 init.value = error_mark_node;
3061 init.original_code = ERROR_MARK;
3062 c_parser_error (parser, "expected identifier");
3063 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3064 process_init_element (init);
3065 return;
3068 else
3070 tree first, second;
3071 /* ??? Following the old parser, [ objc-receiver
3072 objc-message-args ] is accepted as an initializer,
3073 being distinguished from a designator by what follows
3074 the first assignment expression inside the square
3075 brackets, but after a first array designator a
3076 subsequent square bracket is for Objective-C taken to
3077 start an expression, using the obsolete form of
3078 designated initializer without '=', rather than
3079 possibly being a second level of designation: in LALR
3080 terms, the '[' is shifted rather than reducing
3081 designator to designator-list. */
3082 if (des_prev == 1 && c_dialect_objc ())
3084 des_seen = des_prev;
3085 break;
3087 if (des_prev == 0 && c_dialect_objc ())
3089 /* This might be an array designator or an
3090 Objective-C message expression. If the former,
3091 continue parsing here; if the latter, parse the
3092 remainder of the initializer given the starting
3093 primary-expression. ??? It might make sense to
3094 distinguish when des_prev == 1 as well; see
3095 previous comment. */
3096 tree rec, args;
3097 struct c_expr mexpr;
3098 c_parser_consume_token (parser);
3099 if (c_parser_peek_token (parser)->type == CPP_NAME
3100 && ((c_parser_peek_token (parser)->id_kind
3101 == C_ID_TYPENAME)
3102 || (c_parser_peek_token (parser)->id_kind
3103 == C_ID_CLASSNAME)))
3105 /* Type name receiver. */
3106 tree id = c_parser_peek_token (parser)->value;
3107 c_parser_consume_token (parser);
3108 rec = objc_get_class_reference (id);
3109 goto parse_message_args;
3111 first = c_parser_expr_no_commas (parser, NULL).value;
3112 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3113 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3114 goto array_desig_after_first;
3115 /* Expression receiver. So far only one part
3116 without commas has been parsed; there might be
3117 more of the expression. */
3118 rec = first;
3119 while (c_parser_next_token_is (parser, CPP_COMMA))
3121 struct c_expr next;
3122 c_parser_consume_token (parser);
3123 next = c_parser_expr_no_commas (parser, NULL);
3124 next = default_function_array_conversion (next);
3125 rec = build_compound_expr (rec, next.value);
3127 parse_message_args:
3128 /* Now parse the objc-message-args. */
3129 args = c_parser_objc_message_args (parser);
3130 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3131 "expected %<]%>");
3132 mexpr.value
3133 = objc_build_message_expr (build_tree_list (rec, args));
3134 mexpr.original_code = ERROR_MARK;
3135 /* Now parse and process the remainder of the
3136 initializer, starting with this message
3137 expression as a primary-expression. */
3138 c_parser_initval (parser, &mexpr);
3139 return;
3141 c_parser_consume_token (parser);
3142 first = c_parser_expr_no_commas (parser, NULL).value;
3143 array_desig_after_first:
3144 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3146 c_parser_consume_token (parser);
3147 second = c_parser_expr_no_commas (parser, NULL).value;
3149 else
3150 second = NULL_TREE;
3151 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3153 c_parser_consume_token (parser);
3154 set_init_index (first, second);
3155 if (pedantic && second)
3156 pedwarn ("ISO C forbids specifying range of "
3157 "elements to initialize");
3159 else
3160 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3161 "expected %<]%>");
3164 if (des_seen >= 1)
3166 if (c_parser_next_token_is (parser, CPP_EQ))
3168 if (pedantic && !flag_isoc99)
3169 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3170 c_parser_consume_token (parser);
3172 else
3174 if (des_seen == 1)
3176 if (pedantic)
3177 pedwarn ("obsolete use of designated initializer "
3178 "without %<=%>");
3180 else
3182 struct c_expr init;
3183 init.value = error_mark_node;
3184 init.original_code = ERROR_MARK;
3185 c_parser_error (parser, "expected %<=%>");
3186 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3187 process_init_element (init);
3188 return;
3193 c_parser_initval (parser, NULL);
3196 /* Parse a nested initializer; as c_parser_initializer but parses
3197 initializers within braced lists, after any designators have been
3198 applied. If AFTER is not NULL then it is an Objective-C message
3199 expression which is the primary-expression starting the
3200 initializer. */
3202 static void
3203 c_parser_initval (c_parser *parser, struct c_expr *after)
3205 struct c_expr init;
3206 gcc_assert (!after || c_dialect_objc ());
3207 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3208 init = c_parser_braced_init (parser, NULL_TREE, true);
3209 else
3211 init = c_parser_expr_no_commas (parser, after);
3212 if (init.value != NULL_TREE
3213 && TREE_CODE (init.value) != STRING_CST
3214 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3215 init = default_function_array_conversion (init);
3217 process_init_element (init);
3220 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3221 C99 6.8.2).
3223 compound-statement:
3224 { block-item-list[opt] }
3225 { label-declarations block-item-list }
3227 block-item-list:
3228 block-item
3229 block-item-list block-item
3231 block-item:
3232 nested-declaration
3233 statement
3235 nested-declaration:
3236 declaration
3238 GNU extensions:
3240 compound-statement:
3241 { label-declarations block-item-list }
3243 nested-declaration:
3244 __extension__ nested-declaration
3245 nested-function-definition
3247 label-declarations:
3248 label-declaration
3249 label-declarations label-declaration
3251 label-declaration:
3252 __label__ identifier-list ;
3254 Allowing the mixing of declarations and code is new in C99. The
3255 GNU syntax also permits (not shown above) labels at the end of
3256 compound statements, which yield an error. We don't allow labels
3257 on declarations; this might seem like a natural extension, but
3258 there would be a conflict between attributes on the label and
3259 prefix attributes on the declaration. ??? The syntax follows the
3260 old parser in requiring something after label declarations.
3261 Although they are erroneous if the labels declared aren't defined,
3262 is it useful for the syntax to be this way? */
3264 static tree
3265 c_parser_compound_statement (c_parser *parser)
3267 tree stmt;
3268 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3269 return error_mark_node;
3270 stmt = c_begin_compound_stmt (true);
3271 c_parser_compound_statement_nostart (parser);
3272 return c_end_compound_stmt (stmt, true);
3275 /* Parse a compound statement except for the opening brace. This is
3276 used for parsing both compound statements and statement expressions
3277 (which follow different paths to handling the opening). */
3279 static void
3280 c_parser_compound_statement_nostart (c_parser *parser)
3282 bool last_stmt = false;
3283 bool last_label = false;
3284 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3286 c_parser_consume_token (parser);
3287 return;
3289 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3291 /* Read zero or more forward-declarations for labels that nested
3292 functions can jump to. */
3293 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3295 c_parser_consume_token (parser);
3296 /* Any identifiers, including those declared as type names,
3297 are OK here. */
3298 while (true)
3300 tree label;
3301 if (c_parser_next_token_is_not (parser, CPP_NAME))
3303 c_parser_error (parser, "expected identifier");
3304 break;
3306 label
3307 = declare_label (c_parser_peek_token (parser)->value);
3308 C_DECLARED_LABEL_FLAG (label) = 1;
3309 add_stmt (build_stmt (DECL_EXPR, label));
3310 c_parser_consume_token (parser);
3311 if (c_parser_next_token_is (parser, CPP_COMMA))
3312 c_parser_consume_token (parser);
3313 else
3314 break;
3316 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3318 /* ??? Locating this diagnostic on the token after the
3319 declarations end follows the old parser, but it might be
3320 better to locate it where the declarations start instead. */
3321 if (pedantic)
3322 pedwarn ("ISO C forbids label declarations");
3324 /* We must now have at least one statement, label or declaration. */
3325 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3327 c_parser_error (parser, "expected declaration or statement");
3328 c_parser_consume_token (parser);
3329 return;
3331 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3333 location_t loc = c_parser_peek_token (parser)->location;
3334 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3335 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3336 || (c_parser_next_token_is (parser, CPP_NAME)
3337 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3339 last_label = true;
3340 last_stmt = false;
3341 c_parser_label (parser);
3343 else if (!last_label
3344 && c_parser_next_token_starts_declspecs (parser))
3346 last_label = false;
3347 c_parser_declaration_or_fndef (parser, true, true, true, true);
3348 if (last_stmt
3349 && ((pedantic && !flag_isoc99)
3350 || warn_declaration_after_statement))
3351 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3352 &loc);
3353 last_stmt = false;
3355 else if (!last_label
3356 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3358 /* __extension__ can start a declaration, but is also an
3359 unary operator that can start an expression. Consume all
3360 but the last of a possible series of __extension__ to
3361 determine which. */
3362 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3363 && (c_parser_peek_2nd_token (parser)->keyword
3364 == RID_EXTENSION))
3365 c_parser_consume_token (parser);
3366 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3368 int ext;
3369 ext = disable_extension_diagnostics ();
3370 c_parser_consume_token (parser);
3371 last_label = false;
3372 c_parser_declaration_or_fndef (parser, true, true, true, true);
3373 /* Following the old parser, __extension__ does not
3374 disable this diagnostic. */
3375 restore_extension_diagnostics (ext);
3376 if (last_stmt
3377 && ((pedantic && !flag_isoc99)
3378 || warn_declaration_after_statement))
3379 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3380 &loc);
3381 last_stmt = false;
3383 else
3384 goto statement;
3386 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3388 /* External pragmas, and some omp pragmas, are not associated
3389 with regular c code, and so are not to be considered statements
3390 syntactically. This ensures that the user doesn't put them
3391 places that would turn into syntax errors if the directive
3392 were ignored. */
3393 if (c_parser_pragma (parser, pragma_compound))
3394 last_label = false, last_stmt = true;
3396 else if (c_parser_next_token_is (parser, CPP_EOF))
3398 c_parser_error (parser, "expected declaration or statement");
3399 return;
3401 else
3403 statement:
3404 last_label = false;
3405 last_stmt = true;
3406 c_parser_statement_after_labels (parser);
3409 if (last_label)
3410 error ("label at end of compound statement");
3411 c_parser_consume_token (parser);
3414 /* Parse a label (C90 6.6.1, C99 6.8.1).
3416 label:
3417 identifier : attributes[opt]
3418 case constant-expression :
3419 default :
3421 GNU extensions:
3423 label:
3424 case constant-expression ... constant-expression :
3426 The use of attributes on labels is a GNU extension. The syntax in
3427 GNU C accepts any expressions without commas, non-constant
3428 expressions being rejected later. */
3430 static void
3431 c_parser_label (c_parser *parser)
3433 location_t loc1 = c_parser_peek_token (parser)->location;
3434 tree label = NULL_TREE;
3435 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3437 tree exp1, exp2;
3438 c_parser_consume_token (parser);
3439 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3440 if (c_parser_next_token_is (parser, CPP_COLON))
3442 c_parser_consume_token (parser);
3443 label = do_case (exp1, NULL_TREE);
3445 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3447 c_parser_consume_token (parser);
3448 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3449 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3450 label = do_case (exp1, exp2);
3452 else
3453 c_parser_error (parser, "expected %<:%> or %<...%>");
3455 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3457 c_parser_consume_token (parser);
3458 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3459 label = do_case (NULL_TREE, NULL_TREE);
3461 else
3463 tree name = c_parser_peek_token (parser)->value;
3464 tree tlab;
3465 location_t loc2;
3466 tree attrs;
3467 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3468 c_parser_consume_token (parser);
3469 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3470 loc2 = c_parser_peek_token (parser)->location;
3471 c_parser_consume_token (parser);
3472 attrs = c_parser_attributes (parser);
3473 tlab = define_label (loc2, name);
3474 if (tlab)
3476 decl_attributes (&tlab, attrs, 0);
3477 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3480 if (label)
3481 SET_EXPR_LOCATION (label, loc1);
3484 /* Parse a statement (C90 6.6, C99 6.8).
3486 statement:
3487 labeled-statement
3488 compound-statement
3489 expression-statement
3490 selection-statement
3491 iteration-statement
3492 jump-statement
3494 labeled-statement:
3495 label statement
3497 expression-statement:
3498 expression[opt] ;
3500 selection-statement:
3501 if-statement
3502 switch-statement
3504 iteration-statement:
3505 while-statement
3506 do-statement
3507 for-statement
3509 jump-statement:
3510 goto identifier ;
3511 continue ;
3512 break ;
3513 return expression[opt] ;
3515 GNU extensions:
3517 statement:
3518 asm-statement
3520 jump-statement:
3521 goto * expression ;
3523 Objective-C:
3525 statement:
3526 objc-throw-statement
3527 objc-try-catch-statement
3528 objc-synchronized-statement
3530 objc-throw-statement:
3531 @throw expression ;
3532 @throw ;
3535 static void
3536 c_parser_statement (c_parser *parser)
3538 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3539 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3540 || (c_parser_next_token_is (parser, CPP_NAME)
3541 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3542 c_parser_label (parser);
3543 c_parser_statement_after_labels (parser);
3546 /* Parse a statement, other than a labeled statement. */
3548 static void
3549 c_parser_statement_after_labels (c_parser *parser)
3551 location_t loc = c_parser_peek_token (parser)->location;
3552 tree stmt = NULL_TREE;
3553 switch (c_parser_peek_token (parser)->type)
3555 case CPP_OPEN_BRACE:
3556 add_stmt (c_parser_compound_statement (parser));
3557 break;
3558 case CPP_KEYWORD:
3559 switch (c_parser_peek_token (parser)->keyword)
3561 case RID_IF:
3562 c_parser_if_statement (parser);
3563 break;
3564 case RID_SWITCH:
3565 c_parser_switch_statement (parser);
3566 break;
3567 case RID_WHILE:
3568 c_parser_while_statement (parser);
3569 break;
3570 case RID_DO:
3571 c_parser_do_statement (parser);
3572 break;
3573 case RID_FOR:
3574 c_parser_for_statement (parser);
3575 break;
3576 case RID_GOTO:
3577 c_parser_consume_token (parser);
3578 if (c_parser_next_token_is (parser, CPP_NAME))
3580 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3581 c_parser_consume_token (parser);
3583 else if (c_parser_next_token_is (parser, CPP_MULT))
3585 c_parser_consume_token (parser);
3586 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3588 else
3589 c_parser_error (parser, "expected identifier or %<*%>");
3590 goto expect_semicolon;
3591 case RID_CONTINUE:
3592 c_parser_consume_token (parser);
3593 stmt = c_finish_bc_stmt (&c_cont_label, false);
3594 goto expect_semicolon;
3595 case RID_BREAK:
3596 c_parser_consume_token (parser);
3597 stmt = c_finish_bc_stmt (&c_break_label, true);
3598 goto expect_semicolon;
3599 case RID_RETURN:
3600 c_parser_consume_token (parser);
3601 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3603 stmt = c_finish_return (NULL_TREE);
3604 c_parser_consume_token (parser);
3606 else
3608 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3609 goto expect_semicolon;
3611 break;
3612 case RID_ASM:
3613 stmt = c_parser_asm_statement (parser);
3614 break;
3615 case RID_AT_THROW:
3616 gcc_assert (c_dialect_objc ());
3617 c_parser_consume_token (parser);
3618 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3620 stmt = objc_build_throw_stmt (NULL_TREE);
3621 c_parser_consume_token (parser);
3623 else
3625 stmt
3626 = objc_build_throw_stmt (c_parser_expression (parser).value);
3627 goto expect_semicolon;
3629 break;
3630 case RID_AT_TRY:
3631 gcc_assert (c_dialect_objc ());
3632 c_parser_objc_try_catch_statement (parser);
3633 break;
3634 case RID_AT_SYNCHRONIZED:
3635 gcc_assert (c_dialect_objc ());
3636 c_parser_objc_synchronized_statement (parser);
3637 break;
3638 default:
3639 goto expr_stmt;
3641 break;
3642 case CPP_SEMICOLON:
3643 c_parser_consume_token (parser);
3644 break;
3645 case CPP_CLOSE_PAREN:
3646 case CPP_CLOSE_SQUARE:
3647 /* Avoid infinite loop in error recovery:
3648 c_parser_skip_until_found stops at a closing nesting
3649 delimiter without consuming it, but here we need to consume
3650 it to proceed further. */
3651 c_parser_error (parser, "expected statement");
3652 c_parser_consume_token (parser);
3653 break;
3654 case CPP_PRAGMA:
3655 c_parser_pragma (parser, pragma_stmt);
3656 break;
3657 default:
3658 expr_stmt:
3659 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3660 expect_semicolon:
3661 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3662 break;
3664 /* Two cases cannot and do not have line numbers associated: If stmt
3665 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3666 cannot hold line numbers. But that's OK because the statement
3667 will either be changed to a MODIFY_EXPR during gimplification of
3668 the statement expr, or discarded. If stmt was compound, but
3669 without new variables, we will have skipped the creation of a
3670 BIND and will have a bare STATEMENT_LIST. But that's OK because
3671 (recursively) all of the component statements should already have
3672 line numbers assigned. ??? Can we discard no-op statements
3673 earlier? */
3674 if (stmt && EXPR_P (stmt))
3675 SET_EXPR_LOCATION (stmt, loc);
3678 /* Parse a parenthesized condition from an if, do or while statement.
3680 condition:
3681 ( expression )
3683 static tree
3684 c_parser_paren_condition (c_parser *parser)
3686 location_t loc;
3687 tree cond;
3688 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3689 return error_mark_node;
3690 loc = c_parser_peek_token (parser)->location;
3691 cond = c_objc_common_truthvalue_conversion
3692 (c_parser_expression_conv (parser).value);
3693 if (EXPR_P (cond))
3694 SET_EXPR_LOCATION (cond, loc);
3695 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3696 return cond;
3699 /* Parse a statement which is a block in C99. */
3701 static tree
3702 c_parser_c99_block_statement (c_parser *parser)
3704 tree block = c_begin_compound_stmt (flag_isoc99);
3705 c_parser_statement (parser);
3706 return c_end_compound_stmt (block, flag_isoc99);
3709 /* Parse the body of an if statement or the else half thereof. This
3710 is just parsing a statement but (a) it is a block in C99, (b) we
3711 track whether the body is an if statement for the sake of
3712 -Wparentheses warnings, (c) we handle an empty body specially for
3713 the sake of -Wextra warnings. */
3715 static tree
3716 c_parser_if_body (c_parser *parser, bool *if_p)
3718 tree block = c_begin_compound_stmt (flag_isoc99);
3719 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3720 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3721 || (c_parser_next_token_is (parser, CPP_NAME)
3722 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3723 c_parser_label (parser);
3724 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3725 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3726 add_stmt (build1 (NOP_EXPR, NULL_TREE, NULL_TREE));
3727 c_parser_statement_after_labels (parser);
3728 return c_end_compound_stmt (block, flag_isoc99);
3731 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3733 if-statement:
3734 if ( expression ) statement
3735 if ( expression ) statement else statement
3738 static void
3739 c_parser_if_statement (c_parser *parser)
3741 tree block;
3742 location_t loc;
3743 tree cond;
3744 bool first_if = false, second_if = false;
3745 tree first_body, second_body;
3746 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3747 c_parser_consume_token (parser);
3748 block = c_begin_compound_stmt (flag_isoc99);
3749 loc = c_parser_peek_token (parser)->location;
3750 cond = c_parser_paren_condition (parser);
3751 first_body = c_parser_if_body (parser, &first_if);
3752 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3754 c_parser_consume_token (parser);
3755 second_body = c_parser_if_body (parser, &second_if);
3757 else
3758 second_body = NULL_TREE;
3759 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3760 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3763 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3765 switch-statement:
3766 switch (expression) statement
3769 static void
3770 c_parser_switch_statement (c_parser *parser)
3772 tree block, expr, body, save_break;
3773 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3774 c_parser_consume_token (parser);
3775 block = c_begin_compound_stmt (flag_isoc99);
3776 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3778 expr = c_parser_expression (parser).value;
3779 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3781 else
3782 expr = error_mark_node;
3783 c_start_case (expr);
3784 save_break = c_break_label;
3785 c_break_label = NULL_TREE;
3786 body = c_parser_c99_block_statement (parser);
3787 c_finish_case (body);
3788 if (c_break_label)
3789 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3790 c_break_label = save_break;
3791 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3794 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3796 while-statement:
3797 while (expression) statement
3800 static void
3801 c_parser_while_statement (c_parser *parser)
3803 tree block, cond, body, save_break, save_cont;
3804 location_t loc;
3805 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3806 c_parser_consume_token (parser);
3807 block = c_begin_compound_stmt (flag_isoc99);
3808 loc = c_parser_peek_token (parser)->location;
3809 cond = c_parser_paren_condition (parser);
3810 save_break = c_break_label;
3811 c_break_label = NULL_TREE;
3812 save_cont = c_cont_label;
3813 c_cont_label = NULL_TREE;
3814 body = c_parser_c99_block_statement (parser);
3815 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3816 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3817 c_break_label = save_break;
3818 c_cont_label = save_cont;
3821 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3823 do-statement:
3824 do statement while ( expression ) ;
3827 static void
3828 c_parser_do_statement (c_parser *parser)
3830 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3831 location_t loc;
3832 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3833 c_parser_consume_token (parser);
3834 block = c_begin_compound_stmt (flag_isoc99);
3835 loc = c_parser_peek_token (parser)->location;
3836 save_break = c_break_label;
3837 c_break_label = NULL_TREE;
3838 save_cont = c_cont_label;
3839 c_cont_label = NULL_TREE;
3840 body = c_parser_c99_block_statement (parser);
3841 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3842 new_break = c_break_label;
3843 c_break_label = save_break;
3844 new_cont = c_cont_label;
3845 c_cont_label = save_cont;
3846 cond = c_parser_paren_condition (parser);
3847 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3848 c_parser_skip_to_end_of_block_or_statement (parser);
3849 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3850 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3853 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3855 for-statement:
3856 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3857 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3859 The form with a declaration is new in C99.
3861 ??? In accordance with the old parser, the declaration may be a
3862 nested function, which is then rejected in check_for_loop_decls,
3863 but does it make any sense for this to be included in the grammar?
3864 Note in particular that the nested function does not include a
3865 trailing ';', whereas the "declaration" production includes one.
3866 Also, can we reject bad declarations earlier and cheaper than
3867 check_for_loop_decls? */
3869 static void
3870 c_parser_for_statement (c_parser *parser)
3872 tree block, cond, incr, save_break, save_cont, body;
3873 location_t loc;
3874 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3875 loc = c_parser_peek_token (parser)->location;
3876 c_parser_consume_token (parser);
3877 block = c_begin_compound_stmt (flag_isoc99);
3878 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3880 /* Parse the initialization declaration or expression. */
3881 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3883 c_parser_consume_token (parser);
3884 c_finish_expr_stmt (NULL_TREE);
3886 else if (c_parser_next_token_starts_declspecs (parser))
3888 c_parser_declaration_or_fndef (parser, true, true, true, true);
3889 check_for_loop_decls ();
3891 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3893 /* __extension__ can start a declaration, but is also an
3894 unary operator that can start an expression. Consume all
3895 but the last of a possible series of __extension__ to
3896 determine which. */
3897 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3898 && (c_parser_peek_2nd_token (parser)->keyword
3899 == RID_EXTENSION))
3900 c_parser_consume_token (parser);
3901 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3903 int ext;
3904 ext = disable_extension_diagnostics ();
3905 c_parser_consume_token (parser);
3906 c_parser_declaration_or_fndef (parser, true, true, true, true);
3907 restore_extension_diagnostics (ext);
3908 check_for_loop_decls ();
3910 else
3911 goto init_expr;
3913 else
3915 init_expr:
3916 c_finish_expr_stmt (c_parser_expression (parser).value);
3917 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3919 /* Parse the loop condition. */
3920 loc = c_parser_peek_token (parser)->location;
3921 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3923 c_parser_consume_token (parser);
3924 cond = NULL_TREE;
3926 else
3928 tree ocond = c_parser_expression_conv (parser).value;
3929 cond = c_objc_common_truthvalue_conversion (ocond);
3930 if (EXPR_P (cond))
3931 SET_EXPR_LOCATION (cond, loc);
3932 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3934 /* Parse the increment expression. */
3935 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3936 incr = c_process_expr_stmt (NULL_TREE);
3937 else
3938 incr = c_process_expr_stmt (c_parser_expression (parser).value);
3939 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3941 else
3943 cond = error_mark_node;
3944 incr = error_mark_node;
3946 save_break = c_break_label;
3947 c_break_label = NULL_TREE;
3948 save_cont = c_cont_label;
3949 c_cont_label = NULL_TREE;
3950 body = c_parser_c99_block_statement (parser);
3951 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3952 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3953 c_break_label = save_break;
3954 c_cont_label = save_cont;
3957 /* Parse an asm statement, a GNU extension. This is a full-blown asm
3958 statement with inputs, outputs, clobbers, and volatile tag
3959 allowed.
3961 asm-statement:
3962 asm type-qualifier[opt] ( asm-argument ) ;
3964 asm-argument:
3965 asm-string-literal
3966 asm-string-literal : asm-operands[opt]
3967 asm-string-literal : asm-operands[opt] : asm-operands[opt]
3968 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3970 Qualifiers other than volatile are accepted in the syntax but
3971 warned for. */
3973 static tree
3974 c_parser_asm_statement (c_parser *parser)
3976 tree quals, str, outputs, inputs, clobbers, ret;
3977 bool simple;
3978 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3979 c_parser_consume_token (parser);
3980 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3982 quals = c_parser_peek_token (parser)->value;
3983 c_parser_consume_token (parser);
3985 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3986 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3988 warning (0, "%E qualifier ignored on asm",
3989 c_parser_peek_token (parser)->value);
3990 quals = NULL_TREE;
3991 c_parser_consume_token (parser);
3993 else
3994 quals = NULL_TREE;
3995 /* ??? Follow the C++ parser rather than using the
3996 c_lex_string_translate kludge. */
3997 c_lex_string_translate = 0;
3998 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4000 c_lex_string_translate = 1;
4001 return NULL_TREE;
4003 str = c_parser_asm_string_literal (parser);
4004 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4006 simple = true;
4007 outputs = NULL_TREE;
4008 inputs = NULL_TREE;
4009 clobbers = NULL_TREE;
4010 goto done_asm;
4012 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4014 c_lex_string_translate = 1;
4015 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4016 return NULL_TREE;
4018 simple = false;
4019 /* Parse outputs. */
4020 if (c_parser_next_token_is (parser, CPP_COLON)
4021 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4022 outputs = NULL_TREE;
4023 else
4024 outputs = c_parser_asm_operands (parser, false);
4025 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4027 inputs = NULL_TREE;
4028 clobbers = NULL_TREE;
4029 goto done_asm;
4031 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4033 c_lex_string_translate = 1;
4034 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4035 return NULL_TREE;
4037 /* Parse inputs. */
4038 if (c_parser_next_token_is (parser, CPP_COLON)
4039 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4040 inputs = NULL_TREE;
4041 else
4042 inputs = c_parser_asm_operands (parser, true);
4043 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4045 clobbers = NULL_TREE;
4046 goto done_asm;
4048 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4050 c_lex_string_translate = 1;
4051 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4052 return NULL_TREE;
4054 /* Parse clobbers. */
4055 clobbers = c_parser_asm_clobbers (parser);
4056 done_asm:
4057 c_lex_string_translate = 1;
4058 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4060 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4061 return NULL_TREE;
4063 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4064 c_parser_skip_to_end_of_block_or_statement (parser);
4065 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4066 clobbers, simple));
4067 return ret;
4070 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4071 not outputs), apply the default conversion of functions and arrays
4072 to pointers.
4074 asm-operands:
4075 asm-operand
4076 asm-operands , asm-operand
4078 asm-operand:
4079 asm-string-literal ( expression )
4080 [ identifier ] asm-string-literal ( expression )
4083 static tree
4084 c_parser_asm_operands (c_parser *parser, bool convert_p)
4086 tree list = NULL_TREE;
4087 while (true)
4089 tree name, str;
4090 struct c_expr expr;
4091 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4093 c_parser_consume_token (parser);
4094 if (c_parser_next_token_is (parser, CPP_NAME))
4096 tree id = c_parser_peek_token (parser)->value;
4097 c_parser_consume_token (parser);
4098 name = build_string (IDENTIFIER_LENGTH (id),
4099 IDENTIFIER_POINTER (id));
4101 else
4103 c_parser_error (parser, "expected identifier");
4104 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4105 return NULL_TREE;
4107 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4108 "expected %<]%>");
4110 else
4111 name = NULL_TREE;
4112 str = c_parser_asm_string_literal (parser);
4113 if (str == NULL_TREE)
4114 return NULL_TREE;
4115 c_lex_string_translate = 1;
4116 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4118 c_lex_string_translate = 0;
4119 return NULL_TREE;
4121 expr = c_parser_expression (parser);
4122 if (convert_p)
4123 expr = default_function_array_conversion (expr);
4124 c_lex_string_translate = 0;
4125 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4127 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4128 return NULL_TREE;
4130 list = chainon (list, build_tree_list (build_tree_list (name, str),
4131 expr.value));
4132 if (c_parser_next_token_is (parser, CPP_COMMA))
4133 c_parser_consume_token (parser);
4134 else
4135 break;
4137 return list;
4140 /* Parse asm clobbers, a GNU extension.
4142 asm-clobbers:
4143 asm-string-literal
4144 asm-clobbers , asm-string-literal
4147 static tree
4148 c_parser_asm_clobbers (c_parser *parser)
4150 tree list = NULL_TREE;
4151 while (true)
4153 tree str = c_parser_asm_string_literal (parser);
4154 if (str)
4155 list = tree_cons (NULL_TREE, str, list);
4156 else
4157 return NULL_TREE;
4158 if (c_parser_next_token_is (parser, CPP_COMMA))
4159 c_parser_consume_token (parser);
4160 else
4161 break;
4163 return list;
4166 /* Parse an expression other than a compound expression; that is, an
4167 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4168 NULL then it is an Objective-C message expression which is the
4169 primary-expression starting the expression as an initializer.
4171 assignment-expression:
4172 conditional-expression
4173 unary-expression assignment-operator assignment-expression
4175 assignment-operator: one of
4176 = *= /= %= += -= <<= >>= &= ^= |=
4178 In GNU C we accept any conditional expression on the LHS and
4179 diagnose the invalid lvalue rather than producing a syntax
4180 error. */
4182 static struct c_expr
4183 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4185 struct c_expr lhs, rhs, ret;
4186 enum tree_code code;
4187 gcc_assert (!after || c_dialect_objc ());
4188 lhs = c_parser_conditional_expression (parser, after);
4189 switch (c_parser_peek_token (parser)->type)
4191 case CPP_EQ:
4192 code = NOP_EXPR;
4193 break;
4194 case CPP_MULT_EQ:
4195 code = MULT_EXPR;
4196 break;
4197 case CPP_DIV_EQ:
4198 code = TRUNC_DIV_EXPR;
4199 break;
4200 case CPP_MOD_EQ:
4201 code = TRUNC_MOD_EXPR;
4202 break;
4203 case CPP_PLUS_EQ:
4204 code = PLUS_EXPR;
4205 break;
4206 case CPP_MINUS_EQ:
4207 code = MINUS_EXPR;
4208 break;
4209 case CPP_LSHIFT_EQ:
4210 code = LSHIFT_EXPR;
4211 break;
4212 case CPP_RSHIFT_EQ:
4213 code = RSHIFT_EXPR;
4214 break;
4215 case CPP_AND_EQ:
4216 code = BIT_AND_EXPR;
4217 break;
4218 case CPP_XOR_EQ:
4219 code = BIT_XOR_EXPR;
4220 break;
4221 case CPP_OR_EQ:
4222 code = BIT_IOR_EXPR;
4223 break;
4224 default:
4225 return lhs;
4227 c_parser_consume_token (parser);
4228 rhs = c_parser_expr_no_commas (parser, NULL);
4229 rhs = default_function_array_conversion (rhs);
4230 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4231 if (code == NOP_EXPR)
4232 ret.original_code = MODIFY_EXPR;
4233 else
4235 TREE_NO_WARNING (ret.value) = 1;
4236 ret.original_code = ERROR_MARK;
4238 return ret;
4241 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4242 is not NULL then it is an Objective-C message expression which is
4243 the primary-expression starting the expression as an initializer.
4245 conditional-expression:
4246 logical-OR-expression
4247 logical-OR-expression ? expression : conditional-expression
4249 GNU extensions:
4251 conditional-expression:
4252 logical-OR-expression ? : conditional-expression
4255 static struct c_expr
4256 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4258 struct c_expr cond, exp1, exp2, ret;
4259 gcc_assert (!after || c_dialect_objc ());
4260 cond = c_parser_binary_expression (parser, after);
4261 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4262 return cond;
4263 cond = default_function_array_conversion (cond);
4264 c_parser_consume_token (parser);
4265 if (c_parser_next_token_is (parser, CPP_COLON))
4267 if (pedantic)
4268 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4269 /* Make sure first operand is calculated only once. */
4270 exp1.value = save_expr (default_conversion (cond.value));
4271 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4272 skip_evaluation += cond.value == truthvalue_true_node;
4274 else
4276 cond.value
4277 = c_objc_common_truthvalue_conversion
4278 (default_conversion (cond.value));
4279 skip_evaluation += cond.value == truthvalue_false_node;
4280 exp1 = c_parser_expression_conv (parser);
4281 skip_evaluation += ((cond.value == truthvalue_true_node)
4282 - (cond.value == truthvalue_false_node));
4284 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4286 skip_evaluation -= cond.value == truthvalue_true_node;
4287 ret.value = error_mark_node;
4288 ret.original_code = ERROR_MARK;
4289 return ret;
4291 exp2 = c_parser_conditional_expression (parser, NULL);
4292 exp2 = default_function_array_conversion (exp2);
4293 skip_evaluation -= cond.value == truthvalue_true_node;
4294 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4295 ret.original_code = ERROR_MARK;
4296 return ret;
4299 /* Parse a binary expression; that is, a logical-OR-expression (C90
4300 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4301 an Objective-C message expression which is the primary-expression
4302 starting the expression as an initializer.
4304 multiplicative-expression:
4305 cast-expression
4306 multiplicative-expression * cast-expression
4307 multiplicative-expression / cast-expression
4308 multiplicative-expression % cast-expression
4310 additive-expression:
4311 multiplicative-expression
4312 additive-expression + multiplicative-expression
4313 additive-expression - multiplicative-expression
4315 shift-expression:
4316 additive-expression
4317 shift-expression << additive-expression
4318 shift-expression >> additive-expression
4320 relational-expression:
4321 shift-expression
4322 relational-expression < shift-expression
4323 relational-expression > shift-expression
4324 relational-expression <= shift-expression
4325 relational-expression >= shift-expression
4327 equality-expression:
4328 relational-expression
4329 equality-expression == relational-expression
4330 equality-expression != relational-expression
4332 AND-expression:
4333 equality-expression
4334 AND-expression & equality-expression
4336 exclusive-OR-expression:
4337 AND-expression
4338 exclusive-OR-expression ^ AND-expression
4340 inclusive-OR-expression:
4341 exclusive-OR-expression
4342 inclusive-OR-expression | exclusive-OR-expression
4344 logical-AND-expression:
4345 inclusive-OR-expression
4346 logical-AND-expression && inclusive-OR-expression
4348 logical-OR-expression:
4349 logical-AND-expression
4350 logical-OR-expression || logical-AND-expression
4353 static struct c_expr
4354 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4356 /* A binary expression is parsed using operator-precedence parsing,
4357 with the operands being cast expressions. All the binary
4358 operators are left-associative. Thus a binary expression is of
4359 form:
4361 E0 op1 E1 op2 E2 ...
4363 which we represent on a stack. On the stack, the precedence
4364 levels are strictly increasing. When a new operator is
4365 encountered of higher precedence than that at the top of the
4366 stack, it is pushed; its LHS is the top expression, and its RHS
4367 is everything parsed until it is popped. When a new operator is
4368 encountered with precedence less than or equal to that at the top
4369 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4370 by the result of the operation until the operator at the top of
4371 the stack has lower precedence than the new operator or there is
4372 only one element on the stack; then the top expression is the LHS
4373 of the new operator. In the case of logical AND and OR
4374 expressions, we also need to adjust skip_evaluation as
4375 appropriate when the operators are pushed and popped. */
4377 /* The precedence levels, where 0 is a dummy lowest level used for
4378 the bottom of the stack. */
4379 enum prec {
4380 PREC_NONE,
4381 PREC_LOGOR,
4382 PREC_LOGAND,
4383 PREC_BITOR,
4384 PREC_BITXOR,
4385 PREC_BITAND,
4386 PREC_EQ,
4387 PREC_REL,
4388 PREC_SHIFT,
4389 PREC_ADD,
4390 PREC_MULT,
4391 NUM_PRECS
4393 struct {
4394 /* The expression at this stack level. */
4395 struct c_expr expr;
4396 /* The precedence of the operator on its left, PREC_NONE at the
4397 bottom of the stack. */
4398 enum prec prec;
4399 /* The operation on its left. */
4400 enum tree_code op;
4401 } stack[NUM_PRECS];
4402 int sp;
4403 #define POP \
4404 do { \
4405 switch (stack[sp].op) \
4407 case TRUTH_ANDIF_EXPR: \
4408 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4409 break; \
4410 case TRUTH_ORIF_EXPR: \
4411 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4412 break; \
4413 default: \
4414 break; \
4416 stack[sp - 1].expr \
4417 = default_function_array_conversion (stack[sp - 1].expr); \
4418 stack[sp].expr \
4419 = default_function_array_conversion (stack[sp].expr); \
4420 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4421 stack[sp - 1].expr, \
4422 stack[sp].expr); \
4423 sp--; \
4424 } while (0)
4425 gcc_assert (!after || c_dialect_objc ());
4426 stack[0].expr = c_parser_cast_expression (parser, after);
4427 stack[0].prec = PREC_NONE;
4428 sp = 0;
4429 while (true)
4431 enum prec oprec;
4432 enum tree_code ocode;
4433 if (parser->error)
4434 goto out;
4435 switch (c_parser_peek_token (parser)->type)
4437 case CPP_MULT:
4438 oprec = PREC_MULT;
4439 ocode = MULT_EXPR;
4440 break;
4441 case CPP_DIV:
4442 oprec = PREC_MULT;
4443 ocode = TRUNC_DIV_EXPR;
4444 break;
4445 case CPP_MOD:
4446 oprec = PREC_MULT;
4447 ocode = TRUNC_MOD_EXPR;
4448 break;
4449 case CPP_PLUS:
4450 oprec = PREC_ADD;
4451 ocode = PLUS_EXPR;
4452 break;
4453 case CPP_MINUS:
4454 oprec = PREC_ADD;
4455 ocode = MINUS_EXPR;
4456 break;
4457 case CPP_LSHIFT:
4458 oprec = PREC_SHIFT;
4459 ocode = LSHIFT_EXPR;
4460 break;
4461 case CPP_RSHIFT:
4462 oprec = PREC_SHIFT;
4463 ocode = RSHIFT_EXPR;
4464 break;
4465 case CPP_LESS:
4466 oprec = PREC_REL;
4467 ocode = LT_EXPR;
4468 break;
4469 case CPP_GREATER:
4470 oprec = PREC_REL;
4471 ocode = GT_EXPR;
4472 break;
4473 case CPP_LESS_EQ:
4474 oprec = PREC_REL;
4475 ocode = LE_EXPR;
4476 break;
4477 case CPP_GREATER_EQ:
4478 oprec = PREC_REL;
4479 ocode = GE_EXPR;
4480 break;
4481 case CPP_EQ_EQ:
4482 oprec = PREC_EQ;
4483 ocode = EQ_EXPR;
4484 break;
4485 case CPP_NOT_EQ:
4486 oprec = PREC_EQ;
4487 ocode = NE_EXPR;
4488 break;
4489 case CPP_AND:
4490 oprec = PREC_BITAND;
4491 ocode = BIT_AND_EXPR;
4492 break;
4493 case CPP_XOR:
4494 oprec = PREC_BITXOR;
4495 ocode = BIT_XOR_EXPR;
4496 break;
4497 case CPP_OR:
4498 oprec = PREC_BITOR;
4499 ocode = BIT_IOR_EXPR;
4500 break;
4501 case CPP_AND_AND:
4502 oprec = PREC_LOGAND;
4503 ocode = TRUTH_ANDIF_EXPR;
4504 break;
4505 case CPP_OR_OR:
4506 oprec = PREC_LOGOR;
4507 ocode = TRUTH_ORIF_EXPR;
4508 break;
4509 default:
4510 /* Not a binary operator, so end of the binary
4511 expression. */
4512 goto out;
4514 c_parser_consume_token (parser);
4515 while (oprec <= stack[sp].prec)
4516 POP;
4517 switch (ocode)
4519 case TRUTH_ANDIF_EXPR:
4520 stack[sp].expr
4521 = default_function_array_conversion (stack[sp].expr);
4522 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4523 (default_conversion (stack[sp].expr.value));
4524 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4525 break;
4526 case TRUTH_ORIF_EXPR:
4527 stack[sp].expr
4528 = default_function_array_conversion (stack[sp].expr);
4529 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4530 (default_conversion (stack[sp].expr.value));
4531 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4532 break;
4533 default:
4534 break;
4536 sp++;
4537 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4538 stack[sp].prec = oprec;
4539 stack[sp].op = ocode;
4541 out:
4542 while (sp > 0)
4543 POP;
4544 return stack[0].expr;
4545 #undef POP
4548 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4549 NULL then it is an Objective-C message expression which is the
4550 primary-expression starting the expression as an initializer.
4552 cast-expression:
4553 unary-expression
4554 ( type-name ) unary-expression
4557 static struct c_expr
4558 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4560 gcc_assert (!after || c_dialect_objc ());
4561 if (after)
4562 return c_parser_postfix_expression_after_primary (parser, *after);
4563 /* If the expression begins with a parenthesized type name, it may
4564 be either a cast or a compound literal; we need to see whether
4565 the next character is '{' to tell the difference. If not, it is
4566 an unary expression. */
4567 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4568 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4570 struct c_type_name *type_name;
4571 struct c_expr ret;
4572 struct c_expr expr;
4573 c_parser_consume_token (parser);
4574 type_name = c_parser_type_name (parser);
4575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4576 if (type_name == NULL)
4578 ret.value = error_mark_node;
4579 ret.original_code = ERROR_MARK;
4580 return ret;
4582 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4583 return c_parser_postfix_expression_after_paren_type (parser,
4584 type_name);
4585 expr = c_parser_cast_expression (parser, NULL);
4586 expr = default_function_array_conversion (expr);
4587 ret.value = c_cast_expr (type_name, expr.value);
4588 ret.original_code = ERROR_MARK;
4589 return ret;
4591 else
4592 return c_parser_unary_expression (parser);
4595 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4597 unary-expression:
4598 postfix-expression
4599 ++ unary-expression
4600 -- unary-expression
4601 unary-operator cast-expression
4602 sizeof unary-expression
4603 sizeof ( type-name )
4605 unary-operator: one of
4606 & * + - ~ !
4608 GNU extensions:
4610 unary-expression:
4611 __alignof__ unary-expression
4612 __alignof__ ( type-name )
4613 && identifier
4615 unary-operator: one of
4616 __extension__ __real__ __imag__
4618 In addition, the GNU syntax treats ++ and -- as unary operators, so
4619 they may be applied to cast expressions with errors for non-lvalues
4620 given later. */
4622 static struct c_expr
4623 c_parser_unary_expression (c_parser *parser)
4625 int ext;
4626 struct c_expr ret, op;
4627 switch (c_parser_peek_token (parser)->type)
4629 case CPP_PLUS_PLUS:
4630 c_parser_consume_token (parser);
4631 op = c_parser_cast_expression (parser, NULL);
4632 op = default_function_array_conversion (op);
4633 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4634 case CPP_MINUS_MINUS:
4635 c_parser_consume_token (parser);
4636 op = c_parser_cast_expression (parser, NULL);
4637 op = default_function_array_conversion (op);
4638 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4639 case CPP_AND:
4640 c_parser_consume_token (parser);
4641 return parser_build_unary_op (ADDR_EXPR,
4642 c_parser_cast_expression (parser, NULL));
4643 case CPP_MULT:
4644 c_parser_consume_token (parser);
4645 op = c_parser_cast_expression (parser, NULL);
4646 op = default_function_array_conversion (op);
4647 ret.value = build_indirect_ref (op.value, "unary *");
4648 ret.original_code = ERROR_MARK;
4649 return ret;
4650 case CPP_PLUS:
4651 c_parser_consume_token (parser);
4652 if (!c_dialect_objc () && !in_system_header)
4653 warning (OPT_Wtraditional,
4654 "traditional C rejects the unary plus operator");
4655 op = c_parser_cast_expression (parser, NULL);
4656 op = default_function_array_conversion (op);
4657 return parser_build_unary_op (CONVERT_EXPR, op);
4658 case CPP_MINUS:
4659 c_parser_consume_token (parser);
4660 op = c_parser_cast_expression (parser, NULL);
4661 op = default_function_array_conversion (op);
4662 return parser_build_unary_op (NEGATE_EXPR, op);
4663 case CPP_COMPL:
4664 c_parser_consume_token (parser);
4665 op = c_parser_cast_expression (parser, NULL);
4666 op = default_function_array_conversion (op);
4667 return parser_build_unary_op (BIT_NOT_EXPR, op);
4668 case CPP_NOT:
4669 c_parser_consume_token (parser);
4670 op = c_parser_cast_expression (parser, NULL);
4671 op = default_function_array_conversion (op);
4672 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4673 case CPP_AND_AND:
4674 /* Refer to the address of a label as a pointer. */
4675 c_parser_consume_token (parser);
4676 if (c_parser_next_token_is (parser, CPP_NAME))
4678 ret.value = finish_label_address_expr
4679 (c_parser_peek_token (parser)->value);
4680 c_parser_consume_token (parser);
4682 else
4684 c_parser_error (parser, "expected identifier");
4685 ret.value = error_mark_node;
4687 ret.original_code = ERROR_MARK;
4688 return ret;
4689 case CPP_KEYWORD:
4690 switch (c_parser_peek_token (parser)->keyword)
4692 case RID_SIZEOF:
4693 return c_parser_sizeof_expression (parser);
4694 case RID_ALIGNOF:
4695 return c_parser_alignof_expression (parser);
4696 case RID_EXTENSION:
4697 c_parser_consume_token (parser);
4698 ext = disable_extension_diagnostics ();
4699 ret = c_parser_cast_expression (parser, NULL);
4700 restore_extension_diagnostics (ext);
4701 return ret;
4702 case RID_REALPART:
4703 c_parser_consume_token (parser);
4704 op = c_parser_cast_expression (parser, NULL);
4705 op = default_function_array_conversion (op);
4706 return parser_build_unary_op (REALPART_EXPR, op);
4707 case RID_IMAGPART:
4708 c_parser_consume_token (parser);
4709 op = c_parser_cast_expression (parser, NULL);
4710 op = default_function_array_conversion (op);
4711 return parser_build_unary_op (IMAGPART_EXPR, op);
4712 default:
4713 return c_parser_postfix_expression (parser);
4715 default:
4716 return c_parser_postfix_expression (parser);
4720 /* Parse a sizeof expression. */
4722 static struct c_expr
4723 c_parser_sizeof_expression (c_parser *parser)
4725 struct c_expr expr;
4726 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4727 c_parser_consume_token (parser);
4728 skip_evaluation++;
4729 in_sizeof++;
4730 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4731 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4733 /* Either sizeof ( type-name ) or sizeof unary-expression
4734 starting with a compound literal. */
4735 struct c_type_name *type_name;
4736 c_parser_consume_token (parser);
4737 type_name = c_parser_type_name (parser);
4738 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4739 if (type_name == NULL)
4741 struct c_expr ret;
4742 skip_evaluation--;
4743 in_sizeof--;
4744 ret.value = error_mark_node;
4745 ret.original_code = ERROR_MARK;
4746 return ret;
4748 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4750 expr = c_parser_postfix_expression_after_paren_type (parser,
4751 type_name);
4752 goto sizeof_expr;
4754 /* sizeof ( type-name ). */
4755 skip_evaluation--;
4756 in_sizeof--;
4757 return c_expr_sizeof_type (type_name);
4759 else
4761 expr = c_parser_unary_expression (parser);
4762 sizeof_expr:
4763 skip_evaluation--;
4764 in_sizeof--;
4765 if (TREE_CODE (expr.value) == COMPONENT_REF
4766 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4767 error ("%<sizeof%> applied to a bit-field");
4768 return c_expr_sizeof_expr (expr);
4772 /* Parse an alignof expression. */
4774 static struct c_expr
4775 c_parser_alignof_expression (c_parser *parser)
4777 struct c_expr expr;
4778 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4779 c_parser_consume_token (parser);
4780 skip_evaluation++;
4781 in_alignof++;
4782 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4783 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4785 /* Either __alignof__ ( type-name ) or __alignof__
4786 unary-expression starting with a compound literal. */
4787 struct c_type_name *type_name;
4788 struct c_expr ret;
4789 c_parser_consume_token (parser);
4790 type_name = c_parser_type_name (parser);
4791 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4792 if (type_name == NULL)
4794 struct c_expr ret;
4795 skip_evaluation--;
4796 in_alignof--;
4797 ret.value = error_mark_node;
4798 ret.original_code = ERROR_MARK;
4799 return ret;
4801 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4803 expr = c_parser_postfix_expression_after_paren_type (parser,
4804 type_name);
4805 goto alignof_expr;
4807 /* alignof ( type-name ). */
4808 skip_evaluation--;
4809 in_alignof--;
4810 ret.value = c_alignof (groktypename (type_name));
4811 ret.original_code = ERROR_MARK;
4812 return ret;
4814 else
4816 struct c_expr ret;
4817 expr = c_parser_unary_expression (parser);
4818 alignof_expr:
4819 skip_evaluation--;
4820 in_alignof--;
4821 ret.value = c_alignof_expr (expr.value);
4822 ret.original_code = ERROR_MARK;
4823 return ret;
4827 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4829 postfix-expression:
4830 primary-expression
4831 postfix-expression [ expression ]
4832 postfix-expression ( argument-expression-list[opt] )
4833 postfix-expression . identifier
4834 postfix-expression -> identifier
4835 postfix-expression ++
4836 postfix-expression --
4837 ( type-name ) { initializer-list }
4838 ( type-name ) { initializer-list , }
4840 argument-expression-list:
4841 argument-expression
4842 argument-expression-list , argument-expression
4844 primary-expression:
4845 identifier
4846 constant
4847 string-literal
4848 ( expression )
4850 GNU extensions:
4852 primary-expression:
4853 __func__
4854 (treated as a keyword in GNU C)
4855 __FUNCTION__
4856 __PRETTY_FUNCTION__
4857 ( compound-statement )
4858 __builtin_va_arg ( assignment-expression , type-name )
4859 __builtin_offsetof ( type-name , offsetof-member-designator )
4860 __builtin_choose_expr ( assignment-expression ,
4861 assignment-expression ,
4862 assignment-expression )
4863 __builtin_types_compatible_p ( type-name , type-name )
4865 offsetof-member-designator:
4866 identifier
4867 offsetof-member-designator . identifier
4868 offsetof-member-designator [ expression ]
4870 Objective-C:
4872 primary-expression:
4873 [ objc-receiver objc-message-args ]
4874 @selector ( objc-selector-arg )
4875 @protocol ( identifier )
4876 @encode ( type-name )
4877 objc-string-literal
4880 static struct c_expr
4881 c_parser_postfix_expression (c_parser *parser)
4883 struct c_expr expr, e1, e2, e3;
4884 struct c_type_name *t1, *t2;
4885 switch (c_parser_peek_token (parser)->type)
4887 case CPP_NUMBER:
4888 case CPP_CHAR:
4889 case CPP_WCHAR:
4890 expr.value = c_parser_peek_token (parser)->value;
4891 expr.original_code = ERROR_MARK;
4892 c_parser_consume_token (parser);
4893 break;
4894 case CPP_STRING:
4895 case CPP_WSTRING:
4896 expr.value = c_parser_peek_token (parser)->value;
4897 expr.original_code = STRING_CST;
4898 c_parser_consume_token (parser);
4899 break;
4900 case CPP_OBJC_STRING:
4901 gcc_assert (c_dialect_objc ());
4902 expr.value
4903 = objc_build_string_object (c_parser_peek_token (parser)->value);
4904 expr.original_code = ERROR_MARK;
4905 c_parser_consume_token (parser);
4906 break;
4907 case CPP_NAME:
4908 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4910 c_parser_error (parser, "expected expression");
4911 expr.value = error_mark_node;
4912 expr.original_code = ERROR_MARK;
4913 break;
4916 tree id = c_parser_peek_token (parser)->value;
4917 location_t loc = c_parser_peek_token (parser)->location;
4918 c_parser_consume_token (parser);
4919 expr.value = build_external_ref (id,
4920 (c_parser_peek_token (parser)->type
4921 == CPP_OPEN_PAREN), loc);
4922 expr.original_code = ERROR_MARK;
4924 break;
4925 case CPP_OPEN_PAREN:
4926 /* A parenthesized expression, statement expression or compound
4927 literal. */
4928 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4930 /* A statement expression. */
4931 tree stmt;
4932 c_parser_consume_token (parser);
4933 c_parser_consume_token (parser);
4934 if (cur_stmt_list == NULL)
4936 error ("braced-group within expression allowed "
4937 "only inside a function");
4938 parser->error = true;
4939 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4940 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4941 expr.value = error_mark_node;
4942 expr.original_code = ERROR_MARK;
4943 break;
4945 stmt = c_begin_stmt_expr ();
4946 c_parser_compound_statement_nostart (parser);
4947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4948 "expected %<)%>");
4949 if (pedantic)
4950 pedwarn ("ISO C forbids braced-groups within expressions");
4951 expr.value = c_finish_stmt_expr (stmt);
4952 expr.original_code = ERROR_MARK;
4954 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4956 /* A compound literal. ??? Can we actually get here rather
4957 than going directly to
4958 c_parser_postfix_expression_after_paren_type from
4959 elsewhere? */
4960 struct c_type_name *type_name;
4961 c_parser_consume_token (parser);
4962 type_name = c_parser_type_name (parser);
4963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4964 "expected %<)%>");
4965 if (type_name == NULL)
4967 expr.value = error_mark_node;
4968 expr.original_code = ERROR_MARK;
4970 else
4971 expr = c_parser_postfix_expression_after_paren_type (parser,
4972 type_name);
4974 else
4976 /* A parenthesized expression. */
4977 c_parser_consume_token (parser);
4978 expr = c_parser_expression (parser);
4979 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4980 TREE_NO_WARNING (expr.value) = 1;
4981 expr.original_code = ERROR_MARK;
4982 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4983 "expected %<)%>");
4985 break;
4986 case CPP_KEYWORD:
4987 switch (c_parser_peek_token (parser)->keyword)
4989 case RID_FUNCTION_NAME:
4990 case RID_PRETTY_FUNCTION_NAME:
4991 case RID_C99_FUNCTION_NAME:
4992 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4993 c_parser_peek_token (parser)->value);
4994 expr.original_code = ERROR_MARK;
4995 c_parser_consume_token (parser);
4996 break;
4997 case RID_VA_ARG:
4998 c_parser_consume_token (parser);
4999 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5001 expr.value = error_mark_node;
5002 expr.original_code = ERROR_MARK;
5003 break;
5005 e1 = c_parser_expr_no_commas (parser, NULL);
5006 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5008 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5009 expr.value = error_mark_node;
5010 expr.original_code = ERROR_MARK;
5011 break;
5013 t1 = c_parser_type_name (parser);
5014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5015 "expected %<)%>");
5016 if (t1 == NULL)
5018 expr.value = error_mark_node;
5019 expr.original_code = ERROR_MARK;
5021 else
5023 expr.value = build_va_arg (e1.value, groktypename (t1));
5024 expr.original_code = ERROR_MARK;
5026 break;
5027 case RID_OFFSETOF:
5028 c_parser_consume_token (parser);
5029 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5031 expr.value = error_mark_node;
5032 expr.original_code = ERROR_MARK;
5033 break;
5035 t1 = c_parser_type_name (parser);
5036 if (t1 == NULL)
5038 expr.value = error_mark_node;
5039 expr.original_code = ERROR_MARK;
5040 break;
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;
5050 tree type = groktypename (t1);
5051 tree offsetof_ref;
5052 if (type == error_mark_node)
5053 offsetof_ref = error_mark_node;
5054 else
5055 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
5056 /* Parse the second argument to __builtin_offsetof. We
5057 must have one identifier, and beyond that we want to
5058 accept sub structure and sub array references. */
5059 if (c_parser_next_token_is (parser, CPP_NAME))
5061 offsetof_ref = build_component_ref
5062 (offsetof_ref, c_parser_peek_token (parser)->value);
5063 c_parser_consume_token (parser);
5064 while (c_parser_next_token_is (parser, CPP_DOT)
5065 || c_parser_next_token_is (parser,
5066 CPP_OPEN_SQUARE))
5068 if (c_parser_next_token_is (parser, CPP_DOT))
5070 c_parser_consume_token (parser);
5071 if (c_parser_next_token_is_not (parser,
5072 CPP_NAME))
5074 c_parser_error (parser, "expected identifier");
5075 break;
5077 offsetof_ref = build_component_ref
5078 (offsetof_ref,
5079 c_parser_peek_token (parser)->value);
5080 c_parser_consume_token (parser);
5082 else
5084 tree idx;
5085 c_parser_consume_token (parser);
5086 idx = c_parser_expression (parser).value;
5087 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5088 "expected %<]%>");
5089 offsetof_ref = build_array_ref (offsetof_ref, idx);
5093 else
5094 c_parser_error (parser, "expected identifier");
5095 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5096 "expected %<)%>");
5097 expr.value = fold_offsetof (offsetof_ref);
5098 expr.original_code = ERROR_MARK;
5100 break;
5101 case RID_CHOOSE_EXPR:
5102 c_parser_consume_token (parser);
5103 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5105 expr.value = error_mark_node;
5106 expr.original_code = ERROR_MARK;
5107 break;
5109 e1 = c_parser_expr_no_commas (parser, NULL);
5110 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5112 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5113 expr.value = error_mark_node;
5114 expr.original_code = ERROR_MARK;
5115 break;
5117 e2 = c_parser_expr_no_commas (parser, NULL);
5118 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5120 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5121 expr.value = error_mark_node;
5122 expr.original_code = ERROR_MARK;
5123 break;
5125 e3 = c_parser_expr_no_commas (parser, NULL);
5126 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5127 "expected %<)%>");
5129 tree c;
5131 c = fold (e1.value);
5132 if (TREE_CODE (c) != INTEGER_CST)
5133 error ("first argument to %<__builtin_choose_expr%> not"
5134 " a constant");
5135 expr = integer_zerop (c) ? e3 : e2;
5137 break;
5138 case RID_TYPES_COMPATIBLE_P:
5139 c_parser_consume_token (parser);
5140 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5142 expr.value = error_mark_node;
5143 expr.original_code = ERROR_MARK;
5144 break;
5146 t1 = c_parser_type_name (parser);
5147 if (t1 == NULL)
5149 expr.value = error_mark_node;
5150 expr.original_code = ERROR_MARK;
5151 break;
5153 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5155 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5156 expr.value = error_mark_node;
5157 expr.original_code = ERROR_MARK;
5158 break;
5160 t2 = c_parser_type_name (parser);
5161 if (t2 == NULL)
5163 expr.value = error_mark_node;
5164 expr.original_code = ERROR_MARK;
5165 break;
5167 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5168 "expected %<)%>");
5170 tree e1, e2;
5172 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5173 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5175 expr.value = comptypes (e1, e2)
5176 ? build_int_cst (NULL_TREE, 1)
5177 : build_int_cst (NULL_TREE, 0);
5178 expr.original_code = ERROR_MARK;
5180 break;
5181 case RID_AT_SELECTOR:
5182 gcc_assert (c_dialect_objc ());
5183 c_parser_consume_token (parser);
5184 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5186 expr.value = error_mark_node;
5187 expr.original_code = ERROR_MARK;
5188 break;
5191 tree sel = c_parser_objc_selector_arg (parser);
5192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5193 "expected %<)%>");
5194 expr.value = objc_build_selector_expr (sel);
5195 expr.original_code = ERROR_MARK;
5197 break;
5198 case RID_AT_PROTOCOL:
5199 gcc_assert (c_dialect_objc ());
5200 c_parser_consume_token (parser);
5201 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5203 expr.value = error_mark_node;
5204 expr.original_code = ERROR_MARK;
5205 break;
5207 if (c_parser_next_token_is_not (parser, CPP_NAME))
5209 c_parser_error (parser, "expected identifier");
5210 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5211 expr.value = error_mark_node;
5212 expr.original_code = ERROR_MARK;
5213 break;
5216 tree id = c_parser_peek_token (parser)->value;
5217 c_parser_consume_token (parser);
5218 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5219 "expected %<)%>");
5220 expr.value = objc_build_protocol_expr (id);
5221 expr.original_code = ERROR_MARK;
5223 break;
5224 case RID_AT_ENCODE:
5225 /* Extension to support C-structures in the archiver. */
5226 gcc_assert (c_dialect_objc ());
5227 c_parser_consume_token (parser);
5228 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5230 expr.value = error_mark_node;
5231 expr.original_code = ERROR_MARK;
5232 break;
5234 t1 = c_parser_type_name (parser);
5235 if (t1 == NULL)
5237 expr.value = error_mark_node;
5238 expr.original_code = ERROR_MARK;
5239 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5240 break;
5242 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5243 "expected %<)%>");
5245 tree type = groktypename (t1);
5246 expr.value = objc_build_encode_expr (type);
5247 expr.original_code = ERROR_MARK;
5249 break;
5250 default:
5251 c_parser_error (parser, "expected expression");
5252 expr.value = error_mark_node;
5253 expr.original_code = ERROR_MARK;
5254 break;
5256 break;
5257 case CPP_OPEN_SQUARE:
5258 if (c_dialect_objc ())
5260 tree receiver, args;
5261 c_parser_consume_token (parser);
5262 receiver = c_parser_objc_receiver (parser);
5263 args = c_parser_objc_message_args (parser);
5264 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5265 "expected %<]%>");
5266 expr.value = objc_build_message_expr (build_tree_list (receiver,
5267 args));
5268 expr.original_code = ERROR_MARK;
5269 break;
5271 /* Else fall through to report error. */
5272 default:
5273 c_parser_error (parser, "expected expression");
5274 expr.value = error_mark_node;
5275 expr.original_code = ERROR_MARK;
5276 break;
5278 return c_parser_postfix_expression_after_primary (parser, expr);
5281 /* Parse a postfix expression after a parenthesized type name: the
5282 brace-enclosed initializer of a compound literal, possibly followed
5283 by some postfix operators. This is separate because it is not
5284 possible to tell until after the type name whether a cast
5285 expression has a cast or a compound literal, or whether the operand
5286 of sizeof is a parenthesized type name or starts with a compound
5287 literal. */
5289 static struct c_expr
5290 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5291 struct c_type_name *type_name)
5293 tree type;
5294 struct c_expr init;
5295 struct c_expr expr;
5296 start_init (NULL_TREE, NULL, 0);
5297 type = groktypename (type_name);
5298 if (C_TYPE_VARIABLE_SIZE (type))
5300 error ("compound literal has variable size");
5301 type = error_mark_node;
5303 init = c_parser_braced_init (parser, type, false);
5304 finish_init ();
5305 maybe_warn_string_init (type, init);
5307 if (pedantic && !flag_isoc99)
5308 pedwarn ("ISO C90 forbids compound literals");
5309 expr.value = build_compound_literal (type, init.value);
5310 expr.original_code = ERROR_MARK;
5311 return c_parser_postfix_expression_after_primary (parser, expr);
5314 /* Parse a postfix expression after the initial primary or compound
5315 literal; that is, parse a series of postfix operators. */
5317 static struct c_expr
5318 c_parser_postfix_expression_after_primary (c_parser *parser,
5319 struct c_expr expr)
5321 tree ident, idx, exprlist;
5322 while (true)
5324 switch (c_parser_peek_token (parser)->type)
5326 case CPP_OPEN_SQUARE:
5327 /* Array reference. */
5328 c_parser_consume_token (parser);
5329 idx = c_parser_expression (parser).value;
5330 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5331 "expected %<]%>");
5332 expr.value = build_array_ref (expr.value, idx);
5333 expr.original_code = ERROR_MARK;
5334 break;
5335 case CPP_OPEN_PAREN:
5336 /* Function call. */
5337 c_parser_consume_token (parser);
5338 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5339 exprlist = NULL_TREE;
5340 else
5341 exprlist = c_parser_expr_list (parser, true);
5342 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5343 "expected %<)%>");
5344 expr.value = build_function_call (expr.value, exprlist);
5345 expr.original_code = ERROR_MARK;
5346 break;
5347 case CPP_DOT:
5348 /* Structure element reference. */
5349 c_parser_consume_token (parser);
5350 expr = default_function_array_conversion (expr);
5351 if (c_parser_next_token_is (parser, CPP_NAME))
5352 ident = c_parser_peek_token (parser)->value;
5353 else
5355 c_parser_error (parser, "expected identifier");
5356 expr.value = error_mark_node;
5357 expr.original_code = ERROR_MARK;
5358 return expr;
5360 c_parser_consume_token (parser);
5361 expr.value = build_component_ref (expr.value, ident);
5362 expr.original_code = ERROR_MARK;
5363 break;
5364 case CPP_DEREF:
5365 /* Structure element reference. */
5366 c_parser_consume_token (parser);
5367 expr = default_function_array_conversion (expr);
5368 if (c_parser_next_token_is (parser, CPP_NAME))
5369 ident = c_parser_peek_token (parser)->value;
5370 else
5372 c_parser_error (parser, "expected identifier");
5373 expr.value = error_mark_node;
5374 expr.original_code = ERROR_MARK;
5375 return expr;
5377 c_parser_consume_token (parser);
5378 expr.value = build_component_ref (build_indirect_ref (expr.value,
5379 "->"), ident);
5380 expr.original_code = ERROR_MARK;
5381 break;
5382 case CPP_PLUS_PLUS:
5383 /* Postincrement. */
5384 c_parser_consume_token (parser);
5385 expr = default_function_array_conversion (expr);
5386 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5387 expr.original_code = ERROR_MARK;
5388 break;
5389 case CPP_MINUS_MINUS:
5390 /* Postdecrement. */
5391 c_parser_consume_token (parser);
5392 expr = default_function_array_conversion (expr);
5393 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5394 expr.original_code = ERROR_MARK;
5395 break;
5396 default:
5397 return expr;
5402 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5404 expression:
5405 assignment-expression
5406 expression , assignment-expression
5409 static struct c_expr
5410 c_parser_expression (c_parser *parser)
5412 struct c_expr expr;
5413 expr = c_parser_expr_no_commas (parser, NULL);
5414 while (c_parser_next_token_is (parser, CPP_COMMA))
5416 struct c_expr next;
5417 c_parser_consume_token (parser);
5418 next = c_parser_expr_no_commas (parser, NULL);
5419 next = default_function_array_conversion (next);
5420 expr.value = build_compound_expr (expr.value, next.value);
5421 expr.original_code = COMPOUND_EXPR;
5423 return expr;
5426 /* Parse an expression and convert functions or arrays to
5427 pointers. */
5429 static struct c_expr
5430 c_parser_expression_conv (c_parser *parser)
5432 struct c_expr expr;
5433 expr = c_parser_expression (parser);
5434 expr = default_function_array_conversion (expr);
5435 return expr;
5438 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5439 functions and arrays to pointers.
5441 nonempty-expr-list:
5442 assignment-expression
5443 nonempty-expr-list , assignment-expression
5446 static tree
5447 c_parser_expr_list (c_parser *parser, bool convert_p)
5449 struct c_expr expr;
5450 tree ret, cur;
5451 expr = c_parser_expr_no_commas (parser, NULL);
5452 if (convert_p)
5453 expr = default_function_array_conversion (expr);
5454 ret = cur = build_tree_list (NULL_TREE, expr.value);
5455 while (c_parser_next_token_is (parser, CPP_COMMA))
5457 c_parser_consume_token (parser);
5458 expr = c_parser_expr_no_commas (parser, NULL);
5459 if (convert_p)
5460 expr = default_function_array_conversion (expr);
5461 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5463 return ret;
5467 /* Parse Objective-C-specific constructs. */
5469 /* Parse an objc-class-definition.
5471 objc-class-definition:
5472 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5473 objc-class-instance-variables[opt] objc-methodprotolist @end
5474 @implementation identifier objc-superclass[opt]
5475 objc-class-instance-variables[opt]
5476 @interface identifier ( identifier ) objc-protocol-refs[opt]
5477 objc-methodprotolist @end
5478 @implementation identifier ( identifier )
5480 objc-superclass:
5481 : identifier
5483 "@interface identifier (" must start "@interface identifier (
5484 identifier ) ...": objc-methodprotolist in the first production may
5485 not start with a parenthesized identifier as a declarator of a data
5486 definition with no declaration specifiers if the objc-superclass,
5487 objc-protocol-refs and objc-class-instance-variables are omitted. */
5489 static void
5490 c_parser_objc_class_definition (c_parser *parser)
5492 bool iface_p;
5493 tree id1;
5494 tree superclass;
5495 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5496 iface_p = true;
5497 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5498 iface_p = false;
5499 else
5500 gcc_unreachable ();
5501 c_parser_consume_token (parser);
5502 if (c_parser_next_token_is_not (parser, CPP_NAME))
5504 c_parser_error (parser, "expected identifier");
5505 return;
5507 id1 = c_parser_peek_token (parser)->value;
5508 c_parser_consume_token (parser);
5509 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5511 tree id2;
5512 tree proto = NULL_TREE;
5513 c_parser_consume_token (parser);
5514 if (c_parser_next_token_is_not (parser, CPP_NAME))
5516 c_parser_error (parser, "expected identifier");
5517 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5518 return;
5520 id2 = c_parser_peek_token (parser)->value;
5521 c_parser_consume_token (parser);
5522 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5523 if (!iface_p)
5525 objc_start_category_implementation (id1, id2);
5526 return;
5528 if (c_parser_next_token_is (parser, CPP_LESS))
5529 proto = c_parser_objc_protocol_refs (parser);
5530 objc_start_category_interface (id1, id2, proto);
5531 c_parser_objc_methodprotolist (parser);
5532 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5533 objc_finish_interface ();
5534 return;
5536 if (c_parser_next_token_is (parser, CPP_COLON))
5538 c_parser_consume_token (parser);
5539 if (c_parser_next_token_is_not (parser, CPP_NAME))
5541 c_parser_error (parser, "expected identifier");
5542 return;
5544 superclass = c_parser_peek_token (parser)->value;
5545 c_parser_consume_token (parser);
5547 else
5548 superclass = NULL_TREE;
5549 if (iface_p)
5551 tree proto = NULL_TREE;
5552 if (c_parser_next_token_is (parser, CPP_LESS))
5553 proto = c_parser_objc_protocol_refs (parser);
5554 objc_start_class_interface (id1, superclass, proto);
5556 else
5557 objc_start_class_implementation (id1, superclass);
5558 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5559 c_parser_objc_class_instance_variables (parser);
5560 if (iface_p)
5562 objc_continue_interface ();
5563 c_parser_objc_methodprotolist (parser);
5564 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5565 objc_finish_interface ();
5567 else
5569 objc_continue_implementation ();
5570 return;
5574 /* Parse objc-class-instance-variables.
5576 objc-class-instance-variables:
5577 { objc-instance-variable-decl-list[opt] }
5579 objc-instance-variable-decl-list:
5580 objc-visibility-spec
5581 objc-instance-variable-decl ;
5583 objc-instance-variable-decl-list objc-visibility-spec
5584 objc-instance-variable-decl-list objc-instance-variable-decl ;
5585 objc-instance-variable-decl-list ;
5587 objc-visibility-spec:
5588 @private
5589 @protected
5590 @public
5592 objc-instance-variable-decl:
5593 struct-declaration
5596 static void
5597 c_parser_objc_class_instance_variables (c_parser *parser)
5599 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5600 c_parser_consume_token (parser);
5601 while (c_parser_next_token_is_not (parser, CPP_EOF))
5603 tree decls;
5604 /* Parse any stray semicolon. */
5605 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5607 if (pedantic)
5608 pedwarn ("extra semicolon in struct or union specified");
5609 c_parser_consume_token (parser);
5610 continue;
5612 /* Stop if at the end of the instance variables. */
5613 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5615 c_parser_consume_token (parser);
5616 break;
5618 /* Parse any objc-visibility-spec. */
5619 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5621 c_parser_consume_token (parser);
5622 objc_set_visibility (2);
5623 continue;
5625 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5627 c_parser_consume_token (parser);
5628 objc_set_visibility (0);
5629 continue;
5631 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5633 c_parser_consume_token (parser);
5634 objc_set_visibility (1);
5635 continue;
5637 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5639 c_parser_pragma (parser, pragma_external);
5640 continue;
5643 /* Parse some comma-separated declarations. */
5644 decls = c_parser_struct_declaration (parser);
5646 /* Comma-separated instance variables are chained together in
5647 reverse order; add them one by one. */
5648 tree ivar = nreverse (decls);
5649 for (; ivar; ivar = TREE_CHAIN (ivar))
5650 objc_add_instance_variable (copy_node (ivar));
5652 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5656 /* Parse an objc-class-declaration.
5658 objc-class-declaration:
5659 @class identifier-list ;
5662 static void
5663 c_parser_objc_class_declaration (c_parser *parser)
5665 tree list = NULL_TREE;
5666 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5667 c_parser_consume_token (parser);
5668 /* Any identifiers, including those declared as type names, are OK
5669 here. */
5670 while (true)
5672 tree id;
5673 if (c_parser_next_token_is_not (parser, CPP_NAME))
5675 c_parser_error (parser, "expected identifier");
5676 break;
5678 id = c_parser_peek_token (parser)->value;
5679 list = chainon (list, build_tree_list (NULL_TREE, id));
5680 c_parser_consume_token (parser);
5681 if (c_parser_next_token_is (parser, CPP_COMMA))
5682 c_parser_consume_token (parser);
5683 else
5684 break;
5686 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5687 objc_declare_class (list);
5690 /* Parse an objc-alias-declaration.
5692 objc-alias-declaration:
5693 @compatibility_alias identifier identifier ;
5696 static void
5697 c_parser_objc_alias_declaration (c_parser *parser)
5699 tree id1, id2;
5700 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5701 c_parser_consume_token (parser);
5702 if (c_parser_next_token_is_not (parser, CPP_NAME))
5704 c_parser_error (parser, "expected identifier");
5705 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5706 return;
5708 id1 = c_parser_peek_token (parser)->value;
5709 c_parser_consume_token (parser);
5710 if (c_parser_next_token_is_not (parser, CPP_NAME))
5712 c_parser_error (parser, "expected identifier");
5713 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5714 return;
5716 id2 = c_parser_peek_token (parser)->value;
5717 c_parser_consume_token (parser);
5718 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5719 objc_declare_alias (id1, id2);
5722 /* Parse an objc-protocol-definition.
5724 objc-protocol-definition:
5725 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5726 @protocol identifier-list ;
5728 "@protocol identifier ;" should be resolved as "@protocol
5729 identifier-list ;": objc-methodprotolist may not start with a
5730 semicolon in the first alternative if objc-protocol-refs are
5731 omitted. */
5733 static void
5734 c_parser_objc_protocol_definition (c_parser *parser)
5736 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5737 c_parser_consume_token (parser);
5738 if (c_parser_next_token_is_not (parser, CPP_NAME))
5740 c_parser_error (parser, "expected identifier");
5741 return;
5743 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5744 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5746 tree list = NULL_TREE;
5747 /* Any identifiers, including those declared as type names, are
5748 OK here. */
5749 while (true)
5751 tree id;
5752 if (c_parser_next_token_is_not (parser, CPP_NAME))
5754 c_parser_error (parser, "expected identifier");
5755 break;
5757 id = c_parser_peek_token (parser)->value;
5758 list = chainon (list, build_tree_list (NULL_TREE, id));
5759 c_parser_consume_token (parser);
5760 if (c_parser_next_token_is (parser, CPP_COMMA))
5761 c_parser_consume_token (parser);
5762 else
5763 break;
5765 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5766 objc_declare_protocols (list);
5768 else
5770 tree id = c_parser_peek_token (parser)->value;
5771 tree proto = NULL_TREE;
5772 c_parser_consume_token (parser);
5773 if (c_parser_next_token_is (parser, CPP_LESS))
5774 proto = c_parser_objc_protocol_refs (parser);
5775 objc_pq_context = 1;
5776 objc_start_protocol (id, proto);
5777 c_parser_objc_methodprotolist (parser);
5778 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5779 objc_pq_context = 0;
5780 objc_finish_interface ();
5784 /* Parse an objc-method-type.
5786 objc-method-type:
5791 static enum tree_code
5792 c_parser_objc_method_type (c_parser *parser)
5794 switch (c_parser_peek_token (parser)->type)
5796 case CPP_PLUS:
5797 c_parser_consume_token (parser);
5798 return PLUS_EXPR;
5799 case CPP_MINUS:
5800 c_parser_consume_token (parser);
5801 return MINUS_EXPR;
5802 default:
5803 gcc_unreachable ();
5807 /* Parse an objc-method-definition.
5809 objc-method-definition:
5810 objc-method-type objc-method-decl ;[opt] compound-statement
5813 static void
5814 c_parser_objc_method_definition (c_parser *parser)
5816 enum tree_code type = c_parser_objc_method_type (parser);
5817 tree decl;
5818 objc_set_method_type (type);
5819 objc_pq_context = 1;
5820 decl = c_parser_objc_method_decl (parser);
5821 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5823 c_parser_consume_token (parser);
5824 if (pedantic)
5825 pedwarn ("extra semicolon in method definition specified");
5827 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5829 c_parser_error (parser, "expected %<{%>");
5830 return;
5832 objc_pq_context = 0;
5833 objc_start_method_definition (decl);
5834 add_stmt (c_parser_compound_statement (parser));
5835 objc_finish_method_definition (current_function_decl);
5838 /* Parse an objc-methodprotolist.
5840 objc-methodprotolist:
5841 empty
5842 objc-methodprotolist objc-methodproto
5843 objc-methodprotolist declaration
5844 objc-methodprotolist ;
5846 The declaration is a data definition, which may be missing
5847 declaration specifiers under the same rules and diagnostics as
5848 other data definitions outside functions, and the stray semicolon
5849 is diagnosed the same way as a stray semicolon outside a
5850 function. */
5852 static void
5853 c_parser_objc_methodprotolist (c_parser *parser)
5855 while (true)
5857 /* The list is terminated by @end. */
5858 switch (c_parser_peek_token (parser)->type)
5860 case CPP_SEMICOLON:
5861 if (pedantic)
5862 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5863 c_parser_consume_token (parser);
5864 break;
5865 case CPP_PLUS:
5866 case CPP_MINUS:
5867 c_parser_objc_methodproto (parser);
5868 break;
5869 case CPP_EOF:
5870 return;
5871 default:
5872 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5873 return;
5874 c_parser_declaration_or_fndef (parser, false, true, false, true);
5875 break;
5880 /* Parse an objc-methodproto.
5882 objc-methodproto:
5883 objc-method-type objc-method-decl ;
5886 static void
5887 c_parser_objc_methodproto (c_parser *parser)
5889 enum tree_code type = c_parser_objc_method_type (parser);
5890 tree decl;
5891 objc_set_method_type (type);
5892 /* Remember protocol qualifiers in prototypes. */
5893 objc_pq_context = 1;
5894 decl = c_parser_objc_method_decl (parser);
5895 /* Forget protocol qualifiers here. */
5896 objc_pq_context = 0;
5897 objc_add_method_declaration (decl);
5898 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5901 /* Parse an objc-method-decl.
5903 objc-method-decl:
5904 ( objc-type-name ) objc-selector
5905 objc-selector
5906 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5907 objc-keyword-selector objc-optparmlist
5909 objc-keyword-selector:
5910 objc-keyword-decl
5911 objc-keyword-selector objc-keyword-decl
5913 objc-keyword-decl:
5914 objc-selector : ( objc-type-name ) identifier
5915 objc-selector : identifier
5916 : ( objc-type-name ) identifier
5917 : identifier
5919 objc-optparmlist:
5920 objc-optparms objc-optellipsis
5922 objc-optparms:
5923 empty
5924 objc-opt-parms , parameter-declaration
5926 objc-optellipsis:
5927 empty
5928 , ...
5931 static tree
5932 c_parser_objc_method_decl (c_parser *parser)
5934 tree type = NULL_TREE;
5935 tree sel;
5936 tree parms = NULL_TREE;
5937 bool ellipsis = false;
5939 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5941 c_parser_consume_token (parser);
5942 type = c_parser_objc_type_name (parser);
5943 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5945 sel = c_parser_objc_selector (parser);
5946 /* If there is no selector, or a colon follows, we have an
5947 objc-keyword-selector. If there is a selector, and a colon does
5948 not follow, that selector ends the objc-method-decl. */
5949 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5951 tree tsel = sel;
5952 tree list = NULL_TREE;
5953 while (true)
5955 tree atype = NULL_TREE, id, keyworddecl;
5956 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5957 break;
5958 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5960 c_parser_consume_token (parser);
5961 atype = c_parser_objc_type_name (parser);
5962 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5963 "expected %<)%>");
5965 if (c_parser_next_token_is_not (parser, CPP_NAME))
5967 c_parser_error (parser, "expected identifier");
5968 return error_mark_node;
5970 id = c_parser_peek_token (parser)->value;
5971 c_parser_consume_token (parser);
5972 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5973 list = chainon (list, keyworddecl);
5974 tsel = c_parser_objc_selector (parser);
5975 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5976 break;
5978 /* Parse the optional parameter list. Optional Objective-C
5979 method parameters follow the C syntax, and may include '...'
5980 to denote a variable number of arguments. */
5981 parms = make_node (TREE_LIST);
5982 while (c_parser_next_token_is (parser, CPP_COMMA))
5984 struct c_parm *parm;
5985 c_parser_consume_token (parser);
5986 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5988 ellipsis = true;
5989 c_parser_consume_token (parser);
5990 break;
5992 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5993 if (parm == NULL)
5994 break;
5995 parms = chainon (parms,
5996 build_tree_list (NULL_TREE, grokparm (parm)));
5998 sel = list;
6000 return objc_build_method_signature (type, sel, parms, ellipsis);
6003 /* Parse an objc-type-name.
6005 objc-type-name:
6006 objc-type-qualifiers[opt] type-name
6007 objc-type-qualifiers[opt]
6009 objc-type-qualifiers:
6010 objc-type-qualifier
6011 objc-type-qualifiers objc-type-qualifier
6013 objc-type-qualifier: one of
6014 in out inout bycopy byref oneway
6017 static tree
6018 c_parser_objc_type_name (c_parser *parser)
6020 tree quals = NULL_TREE;
6021 struct c_type_name *typename = NULL;
6022 tree type = NULL_TREE;
6023 while (true)
6025 c_token *token = c_parser_peek_token (parser);
6026 if (token->type == CPP_KEYWORD
6027 && (token->keyword == RID_IN
6028 || token->keyword == RID_OUT
6029 || token->keyword == RID_INOUT
6030 || token->keyword == RID_BYCOPY
6031 || token->keyword == RID_BYREF
6032 || token->keyword == RID_ONEWAY))
6034 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6035 c_parser_consume_token (parser);
6037 else
6038 break;
6040 if (c_parser_next_token_starts_typename (parser))
6041 typename = c_parser_type_name (parser);
6042 if (typename)
6043 type = groktypename (typename);
6044 return build_tree_list (quals, type);
6047 /* Parse objc-protocol-refs.
6049 objc-protocol-refs:
6050 < identifier-list >
6053 static tree
6054 c_parser_objc_protocol_refs (c_parser *parser)
6056 tree list = NULL_TREE;
6057 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6058 c_parser_consume_token (parser);
6059 /* Any identifiers, including those declared as type names, are OK
6060 here. */
6061 while (true)
6063 tree id;
6064 if (c_parser_next_token_is_not (parser, CPP_NAME))
6066 c_parser_error (parser, "expected identifier");
6067 break;
6069 id = c_parser_peek_token (parser)->value;
6070 list = chainon (list, build_tree_list (NULL_TREE, id));
6071 c_parser_consume_token (parser);
6072 if (c_parser_next_token_is (parser, CPP_COMMA))
6073 c_parser_consume_token (parser);
6074 else
6075 break;
6077 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6078 return list;
6081 /* Parse an objc-try-catch-statement.
6083 objc-try-catch-statement:
6084 @try compound-statement objc-catch-list[opt]
6085 @try compound-statement objc-catch-list[opt] @finally compound-statement
6087 objc-catch-list:
6088 @catch ( parameter-declaration ) compound-statement
6089 objc-catch-list @catch ( parameter-declaration ) compound-statement
6092 static void
6093 c_parser_objc_try_catch_statement (c_parser *parser)
6095 location_t loc;
6096 tree stmt;
6097 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
6098 c_parser_consume_token (parser);
6099 loc = c_parser_peek_token (parser)->location;
6100 stmt = c_parser_compound_statement (parser);
6101 objc_begin_try_stmt (loc, stmt);
6102 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
6104 struct c_parm *parm;
6105 c_parser_consume_token (parser);
6106 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6107 break;
6108 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6109 if (parm == NULL)
6111 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6112 break;
6114 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6115 objc_begin_catch_clause (grokparm (parm));
6116 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6117 c_parser_compound_statement_nostart (parser);
6118 objc_finish_catch_clause ();
6120 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6122 location_t finloc;
6123 tree finstmt;
6124 c_parser_consume_token (parser);
6125 finloc = c_parser_peek_token (parser)->location;
6126 finstmt = c_parser_compound_statement (parser);
6127 objc_build_finally_clause (finloc, finstmt);
6129 objc_finish_try_stmt ();
6132 /* Parse an objc-synchronized-statement.
6134 objc-synchronized-statement:
6135 @synchronized ( expression ) compound-statement
6138 static void
6139 c_parser_objc_synchronized_statement (c_parser *parser)
6141 location_t loc;
6142 tree expr, stmt;
6143 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6144 c_parser_consume_token (parser);
6145 loc = c_parser_peek_token (parser)->location;
6146 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6148 expr = c_parser_expression (parser).value;
6149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6151 else
6152 expr = error_mark_node;
6153 stmt = c_parser_compound_statement (parser);
6154 objc_build_synchronized (loc, expr, stmt);
6157 /* Parse an objc-selector; return NULL_TREE without an error if the
6158 next token is not an objc-selector.
6160 objc-selector:
6161 identifier
6162 one of
6163 enum struct union if else while do for switch case default
6164 break continue return goto asm sizeof typeof __alignof
6165 unsigned long const short volatile signed restrict _Complex
6166 in out inout bycopy byref oneway int char float double void _Bool
6168 ??? Why this selection of keywords but not, for example, storage
6169 class specifiers? */
6171 static tree
6172 c_parser_objc_selector (c_parser *parser)
6174 c_token *token = c_parser_peek_token (parser);
6175 tree value = token->value;
6176 if (token->type == CPP_NAME)
6178 c_parser_consume_token (parser);
6179 return value;
6181 if (token->type != CPP_KEYWORD)
6182 return NULL_TREE;
6183 switch (token->keyword)
6185 case RID_ENUM:
6186 case RID_STRUCT:
6187 case RID_UNION:
6188 case RID_IF:
6189 case RID_ELSE:
6190 case RID_WHILE:
6191 case RID_DO:
6192 case RID_FOR:
6193 case RID_SWITCH:
6194 case RID_CASE:
6195 case RID_DEFAULT:
6196 case RID_BREAK:
6197 case RID_CONTINUE:
6198 case RID_RETURN:
6199 case RID_GOTO:
6200 case RID_ASM:
6201 case RID_SIZEOF:
6202 case RID_TYPEOF:
6203 case RID_ALIGNOF:
6204 case RID_UNSIGNED:
6205 case RID_LONG:
6206 case RID_CONST:
6207 case RID_SHORT:
6208 case RID_VOLATILE:
6209 case RID_SIGNED:
6210 case RID_RESTRICT:
6211 case RID_COMPLEX:
6212 case RID_IN:
6213 case RID_OUT:
6214 case RID_INOUT:
6215 case RID_BYCOPY:
6216 case RID_BYREF:
6217 case RID_ONEWAY:
6218 case RID_INT:
6219 case RID_CHAR:
6220 case RID_FLOAT:
6221 case RID_DOUBLE:
6222 case RID_VOID:
6223 case RID_BOOL:
6224 c_parser_consume_token (parser);
6225 return value;
6226 default:
6227 return NULL_TREE;
6231 /* Parse an objc-selector-arg.
6233 objc-selector-arg:
6234 objc-selector
6235 objc-keywordname-list
6237 objc-keywordname-list:
6238 objc-keywordname
6239 objc-keywordname-list objc-keywordname
6241 objc-keywordname:
6242 objc-selector :
6246 static tree
6247 c_parser_objc_selector_arg (c_parser *parser)
6249 tree sel = c_parser_objc_selector (parser);
6250 tree list = NULL_TREE;
6251 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6252 return sel;
6253 while (true)
6255 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6256 return list;
6257 list = chainon (list, build_tree_list (sel, NULL_TREE));
6258 sel = c_parser_objc_selector (parser);
6259 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6260 break;
6262 return list;
6265 /* Parse an objc-receiver.
6267 objc-receiver:
6268 expression
6269 class-name
6270 type-name
6273 static tree
6274 c_parser_objc_receiver (c_parser *parser)
6276 if (c_parser_peek_token (parser)->type == CPP_NAME
6277 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6278 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6280 tree id = c_parser_peek_token (parser)->value;
6281 c_parser_consume_token (parser);
6282 return objc_get_class_reference (id);
6284 return c_parser_expression (parser).value;
6287 /* Parse objc-message-args.
6289 objc-message-args:
6290 objc-selector
6291 objc-keywordarg-list
6293 objc-keywordarg-list:
6294 objc-keywordarg
6295 objc-keywordarg-list objc-keywordarg
6297 objc-keywordarg:
6298 objc-selector : objc-keywordexpr
6299 : objc-keywordexpr
6302 static tree
6303 c_parser_objc_message_args (c_parser *parser)
6305 tree sel = c_parser_objc_selector (parser);
6306 tree list = NULL_TREE;
6307 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6308 return sel;
6309 while (true)
6311 tree keywordexpr;
6312 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6313 return list;
6314 keywordexpr = c_parser_objc_keywordexpr (parser);
6315 list = chainon (list, build_tree_list (sel, keywordexpr));
6316 sel = c_parser_objc_selector (parser);
6317 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6318 break;
6320 return list;
6323 /* Parse an objc-keywordexpr.
6325 objc-keywordexpr:
6326 nonempty-expr-list
6329 static tree
6330 c_parser_objc_keywordexpr (c_parser *parser)
6332 tree list = c_parser_expr_list (parser, true);
6333 if (TREE_CHAIN (list) == NULL_TREE)
6335 /* Just return the expression, remove a level of
6336 indirection. */
6337 return TREE_VALUE (list);
6339 else
6341 /* We have a comma expression, we will collapse later. */
6342 return list;
6347 /* Handle pragmas. ALLOW_STMT is true if we're within the context of
6348 a function and such pragmas are to be allowed. Returns true if we
6349 actually parsed such a pragma. */
6351 static bool
6352 c_parser_pragma (c_parser *parser, enum pragma_context context ATTRIBUTE_UNUSED)
6354 unsigned int id;
6356 id = c_parser_peek_token (parser)->pragma_kind;
6357 gcc_assert (id != PRAGMA_NONE);
6359 switch (id)
6361 case PRAGMA_GCC_PCH_PREPROCESS:
6362 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6363 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6364 return false;
6366 default:
6367 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
6368 break;
6371 c_parser_consume_pragma (parser);
6372 c_invoke_pragma_handler (id);
6374 /* Skip to EOL, but suppress any error message. Those will have been
6375 generated by the handler routine through calling error, as opposed
6376 to calling c_parser_error. */
6377 parser->error = true;
6378 c_parser_skip_to_pragma_eol (parser);
6380 return false;
6383 /* The interface the pragma parsers have to the lexer. */
6385 enum cpp_ttype
6386 pragma_lex (tree *value)
6388 c_token *tok = c_parser_peek_token (the_parser);
6389 enum cpp_ttype ret = tok->type;
6391 *value = tok->value;
6392 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6393 ret = CPP_EOF;
6394 else
6396 if (ret == CPP_KEYWORD)
6397 ret = CPP_NAME;
6398 c_parser_consume_token (the_parser);
6401 return ret;
6404 static void
6405 c_parser_pragma_pch_preprocess (c_parser *parser)
6407 tree name = NULL;
6409 c_parser_consume_pragma (parser);
6410 if (c_parser_next_token_is (parser, CPP_STRING))
6412 name = c_parser_peek_token (parser)->value;
6413 c_parser_consume_token (parser);
6415 else
6416 c_parser_error (parser, "expected string literal");
6417 c_parser_skip_to_pragma_eol (parser);
6419 if (name)
6420 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6423 /* Parse a single source file. */
6425 void
6426 c_parse_file (void)
6428 /* Use local storage to begin. If the first token is a pragma, parse it.
6429 If it is #pragma GCC pch_preprocess, then this will load a PCH file
6430 which will cause garbage collection. */
6431 c_parser tparser;
6433 memset (&tparser, 0, sizeof tparser);
6434 the_parser = &tparser;
6436 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
6437 c_parser_pragma_pch_preprocess (&tparser);
6439 the_parser = GGC_NEW (c_parser);
6440 *the_parser = tparser;
6442 c_parser_translation_unit (the_parser);
6443 the_parser = NULL;
6446 #include "gt-c-parser.h"