2005-06-22 Kelley Cook <kcook@gcc.gnu.org>
[official-gcc.git] / gcc / c-parser.c
blob88b89e2804f7c3c8e20484356529dd995bd847de
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 Parser actions based on the old Bison parser; structure somewhat
6 influenced by and fragments based on the C++ parser.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 02111-1307, USA. */
25 /* TODO:
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "toplev.h"
54 #include "ggc.h"
55 #include "c-common.h"
58 /* Miscellaneous data and functions needed for the parser. */
60 int yydebug;
62 /* Objective-C specific parser/lexer information. */
64 static int objc_pq_context = 0;
66 /* The following flag is needed to contextualize Objective-C lexical
67 analysis. In some cases (e.g., 'int NSObject;'), it is undesirable
68 to bind an identifier to an Objective-C class, even if a class with
69 that name exists. */
70 static int objc_need_raw_identifier = 0;
71 #define OBJC_NEED_RAW_IDENTIFIER(VAL) \
72 do { \
73 if (c_dialect_objc ()) \
74 objc_need_raw_identifier = VAL; \
75 } while (0)
77 /* The reserved keyword table. */
78 struct resword
80 const char *word;
81 ENUM_BITFIELD(rid) rid : 16;
82 unsigned int disable : 16;
85 /* Disable mask. Keywords are disabled if (reswords[i].disable &
86 mask) is _true_. */
87 #define D_C89 0x01 /* not in C89 */
88 #define D_EXT 0x02 /* GCC extension */
89 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
90 #define D_OBJC 0x08 /* Objective C only */
92 static const struct resword reswords[] =
94 { "_Bool", RID_BOOL, 0 },
95 { "_Complex", RID_COMPLEX, 0 },
96 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
97 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
98 { "__alignof", RID_ALIGNOF, 0 },
99 { "__alignof__", RID_ALIGNOF, 0 },
100 { "__asm", RID_ASM, 0 },
101 { "__asm__", RID_ASM, 0 },
102 { "__attribute", RID_ATTRIBUTE, 0 },
103 { "__attribute__", RID_ATTRIBUTE, 0 },
104 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
105 { "__builtin_offsetof", RID_OFFSETOF, 0 },
106 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
107 { "__builtin_va_arg", RID_VA_ARG, 0 },
108 { "__complex", RID_COMPLEX, 0 },
109 { "__complex__", RID_COMPLEX, 0 },
110 { "__const", RID_CONST, 0 },
111 { "__const__", RID_CONST, 0 },
112 { "__extension__", RID_EXTENSION, 0 },
113 { "__func__", RID_C99_FUNCTION_NAME, 0 },
114 { "__imag", RID_IMAGPART, 0 },
115 { "__imag__", RID_IMAGPART, 0 },
116 { "__inline", RID_INLINE, 0 },
117 { "__inline__", RID_INLINE, 0 },
118 { "__label__", RID_LABEL, 0 },
119 { "__real", RID_REALPART, 0 },
120 { "__real__", RID_REALPART, 0 },
121 { "__restrict", RID_RESTRICT, 0 },
122 { "__restrict__", RID_RESTRICT, 0 },
123 { "__signed", RID_SIGNED, 0 },
124 { "__signed__", RID_SIGNED, 0 },
125 { "__thread", RID_THREAD, 0 },
126 { "__typeof", RID_TYPEOF, 0 },
127 { "__typeof__", RID_TYPEOF, 0 },
128 { "__volatile", RID_VOLATILE, 0 },
129 { "__volatile__", RID_VOLATILE, 0 },
130 { "asm", RID_ASM, D_EXT },
131 { "auto", RID_AUTO, 0 },
132 { "break", RID_BREAK, 0 },
133 { "case", RID_CASE, 0 },
134 { "char", RID_CHAR, 0 },
135 { "const", RID_CONST, 0 },
136 { "continue", RID_CONTINUE, 0 },
137 { "default", RID_DEFAULT, 0 },
138 { "do", RID_DO, 0 },
139 { "double", RID_DOUBLE, 0 },
140 { "else", RID_ELSE, 0 },
141 { "enum", RID_ENUM, 0 },
142 { "extern", RID_EXTERN, 0 },
143 { "float", RID_FLOAT, 0 },
144 { "for", RID_FOR, 0 },
145 { "goto", RID_GOTO, 0 },
146 { "if", RID_IF, 0 },
147 { "inline", RID_INLINE, D_EXT89 },
148 { "int", RID_INT, 0 },
149 { "long", RID_LONG, 0 },
150 { "register", RID_REGISTER, 0 },
151 { "restrict", RID_RESTRICT, D_C89 },
152 { "return", RID_RETURN, 0 },
153 { "short", RID_SHORT, 0 },
154 { "signed", RID_SIGNED, 0 },
155 { "sizeof", RID_SIZEOF, 0 },
156 { "static", RID_STATIC, 0 },
157 { "struct", RID_STRUCT, 0 },
158 { "switch", RID_SWITCH, 0 },
159 { "typedef", RID_TYPEDEF, 0 },
160 { "typeof", RID_TYPEOF, D_EXT },
161 { "union", RID_UNION, 0 },
162 { "unsigned", RID_UNSIGNED, 0 },
163 { "void", RID_VOID, 0 },
164 { "volatile", RID_VOLATILE, 0 },
165 { "while", RID_WHILE, 0 },
166 /* These Objective-C keywords are recognized only immediately after
167 an '@'. */
168 { "class", RID_AT_CLASS, D_OBJC },
169 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
170 { "defs", RID_AT_DEFS, D_OBJC },
171 { "encode", RID_AT_ENCODE, D_OBJC },
172 { "end", RID_AT_END, D_OBJC },
173 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
174 { "interface", RID_AT_INTERFACE, D_OBJC },
175 { "private", RID_AT_PRIVATE, D_OBJC },
176 { "protected", RID_AT_PROTECTED, D_OBJC },
177 { "protocol", RID_AT_PROTOCOL, D_OBJC },
178 { "public", RID_AT_PUBLIC, D_OBJC },
179 { "selector", RID_AT_SELECTOR, D_OBJC },
180 { "throw", RID_AT_THROW, D_OBJC },
181 { "try", RID_AT_TRY, D_OBJC },
182 { "catch", RID_AT_CATCH, D_OBJC },
183 { "finally", RID_AT_FINALLY, D_OBJC },
184 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
185 /* These are recognized only in protocol-qualifier context
186 (see above) */
187 { "bycopy", RID_BYCOPY, D_OBJC },
188 { "byref", RID_BYREF, D_OBJC },
189 { "in", RID_IN, D_OBJC },
190 { "inout", RID_INOUT, D_OBJC },
191 { "oneway", RID_ONEWAY, D_OBJC },
192 { "out", RID_OUT, D_OBJC },
194 #define N_reswords (sizeof reswords / sizeof (struct resword))
196 /* Initialization routine for this file. */
198 void
199 c_parse_init (void)
201 /* The only initialization required is of the reserved word
202 identifiers. */
203 unsigned int i;
204 tree id;
205 int mask = (flag_isoc99 ? 0 : D_C89)
206 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
208 if (!c_dialect_objc ())
209 mask |= D_OBJC;
211 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
212 for (i = 0; i < N_reswords; i++)
214 /* If a keyword is disabled, do not enter it into the table
215 and so create a canonical spelling that isn't a keyword. */
216 if (reswords[i].disable & mask)
217 continue;
219 id = get_identifier (reswords[i].word);
220 C_RID_CODE (id) = reswords[i].rid;
221 C_IS_RESERVED_WORD (id) = 1;
222 ridpointers [(int) reswords[i].rid] = id;
226 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
227 and the C parser. Unlike the C++ lexer, the parser structure
228 stores the lexer information instead of using a separate structure.
229 Identifiers are separated into ordinary identifiers, type names,
230 keywords and some other Objective-C types of identifiers, and some
231 look-ahead is maintained.
233 ??? It might be a good idea to lex the whole file up front (as for
234 C++). It would then be possible to share more of the C and C++
235 lexer code, if desired. */
237 /* The following local token type is used. */
239 /* A keyword. */
240 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
242 /* More information about the type of a CPP_NAME token. */
243 typedef enum c_id_kind {
244 /* An ordinary identifier. */
245 C_ID_ID,
246 /* An identifier declared as a typedef name. */
247 C_ID_TYPENAME,
248 /* An identifier declared as an Objective-C class name. */
249 C_ID_CLASSNAME,
250 /* Not an identifier. */
251 C_ID_NONE
252 } c_id_kind;
254 /* A single C token after string literal concatenation and conversion
255 of preprocessing tokens to tokens. */
256 typedef struct c_token GTY (())
258 /* The kind of token. */
259 ENUM_BITFIELD (cpp_ttype) type : 8;
260 /* If this token is a CPP_NAME, this value indicates whether also
261 declared as some kind of type. Otherwise, it is C_ID_NONE. */
262 ENUM_BITFIELD (c_id_kind) id_kind : 8;
263 /* If this token is a keyword, this value indicates which keyword.
264 Otherwise, this value is RID_MAX. */
265 ENUM_BITFIELD (rid) keyword : 8;
266 /* True if this token is from a system header. */
267 BOOL_BITFIELD in_system_header : 1;
268 /* The value associated with this token, if any. */
269 tree value;
270 /* The location at which this token was found. */
271 location_t location;
272 } c_token;
274 /* A parser structure recording information about the state and
275 context of parsing. Includes lexer information with up to two
276 tokens of look-ahead; more are not needed for C. */
277 typedef struct c_parser GTY(())
279 /* The look-ahead tokens. */
280 c_token tokens[2];
281 /* How many look-ahead tokens are available (0, 1 or 2). */
282 short tokens_avail;
283 /* True if a syntax error is being recovered from; false otherwise.
284 c_parser_error sets this flag. It should clear this flag when
285 enough tokens have been consumed to recover from the error. */
286 BOOL_BITFIELD error : 1;
287 } c_parser;
289 /* Read in and lex a single token, storing it in *TOKEN. */
291 static void
292 c_lex_one_token (c_token *token)
294 timevar_push (TV_LEX);
295 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
296 token->in_system_header = in_system_header;
297 switch (token->type)
299 case CPP_NAME:
300 token->id_kind = C_ID_NONE;
301 token->keyword = RID_MAX;
303 tree decl;
305 int objc_force_identifier = objc_need_raw_identifier;
306 OBJC_NEED_RAW_IDENTIFIER (0);
308 if (C_IS_RESERVED_WORD (token->value))
310 enum rid rid_code = C_RID_CODE (token->value);
312 if (c_dialect_objc ())
314 if (!OBJC_IS_AT_KEYWORD (rid_code)
315 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
317 /* Return the canonical spelling for this keyword. */
318 token->value = ridpointers[(int) rid_code];
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
324 else
326 /* Return the canonical spelling for this keyword. */
327 token->value = ridpointers[(int) rid_code];
328 token->type = CPP_KEYWORD;
329 token->keyword = rid_code;
330 break;
334 decl = lookup_name (token->value);
335 if (decl)
337 if (TREE_CODE (decl) == TYPE_DECL)
339 token->id_kind = C_ID_TYPENAME;
340 break;
343 else if (c_dialect_objc ())
345 tree objc_interface_decl = objc_is_class_name (token->value);
346 /* Objective-C class names are in the same namespace as
347 variables and typedefs, and hence are shadowed by local
348 declarations. */
349 if (objc_interface_decl
350 && (global_bindings_p ()
351 || (!objc_force_identifier && !decl)))
353 token->value = objc_interface_decl;
354 token->id_kind = C_ID_CLASSNAME;
355 break;
359 token->id_kind = C_ID_ID;
360 break;
361 case CPP_AT_NAME:
362 /* This only happens in Objective-C; it must be a keyword. */
363 token->type = CPP_KEYWORD;
364 token->id_kind = C_ID_NONE;
365 token->keyword = C_RID_CODE (token->value);
366 break;
367 case CPP_COLON:
368 case CPP_COMMA:
369 case CPP_CLOSE_PAREN:
370 case CPP_SEMICOLON:
371 /* These tokens may affect the interpretation of any identifiers
372 following, if doing Objective-C. */
373 OBJC_NEED_RAW_IDENTIFIER (0);
374 token->id_kind = C_ID_NONE;
375 token->keyword = RID_MAX;
376 break;
377 default:
378 token->id_kind = C_ID_NONE;
379 token->keyword = RID_MAX;
380 break;
382 timevar_pop (TV_LEX);
385 /* Return a pointer to the next token from PARSER, reading it in if
386 necessary. */
388 static inline c_token *
389 c_parser_peek_token (c_parser *parser)
391 if (parser->tokens_avail == 0)
393 c_lex_one_token (&parser->tokens[0]);
394 parser->tokens_avail = 1;
396 return &parser->tokens[0];
399 /* Return true if the next token from PARSER has the indicated
400 TYPE. */
402 static inline bool
403 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
405 return c_parser_peek_token (parser)->type == type;
408 /* Return true if the next token from PARSER does not have the
409 indicated TYPE. */
411 static inline bool
412 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
414 return !c_parser_next_token_is (parser, type);
417 /* Return true if the next token from PARSER is the indicated
418 KEYWORD. */
420 static inline bool
421 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
423 c_token *token;
425 /* Peek at the next token. */
426 token = c_parser_peek_token (parser);
427 /* Check to see if it is the indicated keyword. */
428 return token->keyword == keyword;
431 /* Return true if TOKEN can start a type name,
432 false otherwise. */
433 static bool
434 c_token_starts_typename (c_token *token)
436 switch (token->type)
438 case CPP_NAME:
439 switch (token->id_kind)
441 case C_ID_ID:
442 return false;
443 case C_ID_TYPENAME:
444 return true;
445 case C_ID_CLASSNAME:
446 gcc_assert (c_dialect_objc ());
447 return true;
448 default:
449 gcc_unreachable ();
451 case CPP_KEYWORD:
452 switch (token->keyword)
454 case RID_UNSIGNED:
455 case RID_LONG:
456 case RID_SHORT:
457 case RID_SIGNED:
458 case RID_COMPLEX:
459 case RID_INT:
460 case RID_CHAR:
461 case RID_FLOAT:
462 case RID_DOUBLE:
463 case RID_VOID:
464 case RID_BOOL:
465 case RID_ENUM:
466 case RID_STRUCT:
467 case RID_UNION:
468 case RID_TYPEOF:
469 case RID_CONST:
470 case RID_VOLATILE:
471 case RID_RESTRICT:
472 case RID_ATTRIBUTE:
473 return true;
474 default:
475 return false;
477 case CPP_LESS:
478 if (c_dialect_objc ())
479 return true;
480 return false;
481 default:
482 return false;
486 /* Return true if the next token from PARSER can start a type name,
487 false otherwise. */
488 static inline bool
489 c_parser_next_token_starts_typename (c_parser *parser)
491 c_token *token = c_parser_peek_token (parser);
492 return c_token_starts_typename (token);
495 /* Return true if TOKEN can start declaration specifiers, false
496 otherwise. */
497 static bool
498 c_token_starts_declspecs (c_token *token)
500 switch (token->type)
502 case CPP_NAME:
503 switch (token->id_kind)
505 case C_ID_ID:
506 return false;
507 case C_ID_TYPENAME:
508 return true;
509 case C_ID_CLASSNAME:
510 gcc_assert (c_dialect_objc ());
511 return true;
512 default:
513 gcc_unreachable ();
515 case CPP_KEYWORD:
516 switch (token->keyword)
518 case RID_STATIC:
519 case RID_EXTERN:
520 case RID_REGISTER:
521 case RID_TYPEDEF:
522 case RID_INLINE:
523 case RID_AUTO:
524 case RID_THREAD:
525 case RID_UNSIGNED:
526 case RID_LONG:
527 case RID_SHORT:
528 case RID_SIGNED:
529 case RID_COMPLEX:
530 case RID_INT:
531 case RID_CHAR:
532 case RID_FLOAT:
533 case RID_DOUBLE:
534 case RID_VOID:
535 case RID_BOOL:
536 case RID_ENUM:
537 case RID_STRUCT:
538 case RID_UNION:
539 case RID_TYPEOF:
540 case RID_CONST:
541 case RID_VOLATILE:
542 case RID_RESTRICT:
543 case RID_ATTRIBUTE:
544 return true;
545 default:
546 return false;
548 case CPP_LESS:
549 if (c_dialect_objc ())
550 return true;
551 return false;
552 default:
553 return false;
557 /* Return true if the next token from PARSER can start declaration
558 specifiers, false otherwise. */
559 static inline bool
560 c_parser_next_token_starts_declspecs (c_parser *parser)
562 c_token *token = c_parser_peek_token (parser);
563 return c_token_starts_declspecs (token);
566 /* Return a pointer to the next-but-one token from PARSER, reading it
567 in if necessary. The next token is already read in. */
569 static c_token *
570 c_parser_peek_2nd_token (c_parser *parser)
572 if (parser->tokens_avail >= 2)
573 return &parser->tokens[1];
574 gcc_assert (parser->tokens_avail == 1);
575 gcc_assert (parser->tokens[0].type != CPP_EOF);
576 c_lex_one_token (&parser->tokens[1]);
577 parser->tokens_avail = 2;
578 return &parser->tokens[1];
581 /* Consume the next token from PARSER. */
583 static void
584 c_parser_consume_token (c_parser *parser)
586 if (parser->tokens_avail == 2)
587 parser->tokens[0] = parser->tokens[1];
588 else
590 gcc_assert (parser->tokens_avail == 1);
591 gcc_assert (parser->tokens[0].type != CPP_EOF);
593 parser->tokens_avail--;
596 /* Update the globals input_location and in_system_header from
597 TOKEN. */
598 static inline void
599 c_parser_set_source_position_from_token (c_token *token)
601 if (token->type != CPP_EOF)
603 input_location = token->location;
604 in_system_header = token->in_system_header;
608 /* Allocate a new parser. */
610 static c_parser *
611 c_parser_new (void)
613 /* Use local storage to lex the first token because loading a PCH
614 file may cause garbage collection. */
615 c_parser tparser;
616 c_parser *ret;
617 memset (&tparser, 0, sizeof tparser);
618 c_lex_one_token (&tparser.tokens[0]);
619 tparser.tokens_avail = 1;
620 ret = GGC_NEW (c_parser);
621 memcpy (ret, &tparser, sizeof tparser);
622 return ret;
625 /* Issue a diagnostic of the form
626 FILE:LINE: MESSAGE before TOKEN
627 where TOKEN is the next token in the input stream of PARSER.
628 MESSAGE (specified by the caller) is usually of the form "expected
629 OTHER-TOKEN".
631 Do not issue a diagnostic if still recovering from an error.
633 ??? This is taken from the C++ parser, but building up messages in
634 this way is not i18n-friendly and some other approach should be
635 used. */
637 static void
638 c_parser_error (c_parser *parser, const char *gmsgid)
640 c_token *token = c_parser_peek_token (parser);
641 if (parser->error)
642 return;
643 parser->error = true;
644 if (!gmsgid)
645 return;
646 /* This diagnostic makes more sense if it is tagged to the line of
647 the token we just peeked at. */
648 c_parser_set_source_position_from_token (token);
649 c_parse_error (gmsgid,
650 /* Because c_parse_error does not understand
651 CPP_KEYWORD, keywords are treated like
652 identifiers. */
653 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
654 token->value);
657 /* If the next token is of the indicated TYPE, consume it. Otherwise,
658 issue the error MSGID. If MSGID is NULL then a message has already
659 been produced and no message will be produced this time. Returns
660 true if found, false otherwise. */
662 static bool
663 c_parser_require (c_parser *parser,
664 enum cpp_ttype type,
665 const char *msgid)
667 if (c_parser_next_token_is (parser, type))
669 c_parser_consume_token (parser);
670 return true;
672 else
674 c_parser_error (parser, msgid);
675 return false;
679 /* If the next token is the indicated keyword, consume it. Otherwise,
680 issue the error MSGID. Returns true if found, false otherwise. */
682 static bool
683 c_parser_require_keyword (c_parser *parser,
684 enum rid keyword,
685 const char *msgid)
687 if (c_parser_next_token_is_keyword (parser, keyword))
689 c_parser_consume_token (parser);
690 return true;
692 else
694 c_parser_error (parser, msgid);
695 return false;
699 /* Like c_parser_require, except that tokens will be skipped until the
700 desired token is found. An error message is still produced if the
701 next token is not as expected. If MSGID is NULL then a message has
702 already been produced and no message will be produced this
703 time. */
705 static void
706 c_parser_skip_until_found (c_parser *parser,
707 enum cpp_ttype type,
708 const char *msgid)
710 unsigned nesting_depth = 0;
712 if (c_parser_require (parser, type, msgid))
713 return;
715 /* Skip tokens until the desired token is found. */
716 while (true)
718 /* Peek at the next token. */
719 c_token *token = c_parser_peek_token (parser);
720 /* If we've reached the token we want, consume it and stop. */
721 if (token->type == type && !nesting_depth)
723 c_parser_consume_token (parser);
724 break;
726 /* If we've run out of tokens, stop. */
727 if (token->type == CPP_EOF)
728 return;
729 if (token->type == CPP_OPEN_BRACE
730 || token->type == CPP_OPEN_PAREN
731 || token->type == CPP_OPEN_SQUARE)
732 ++nesting_depth;
733 else if (token->type == CPP_CLOSE_BRACE
734 || token->type == CPP_CLOSE_PAREN
735 || token->type == CPP_CLOSE_SQUARE)
737 if (nesting_depth-- == 0)
738 break;
740 /* Consume this token. */
741 c_parser_consume_token (parser);
743 parser->error = false;
746 /* Skip tokens until the end of a parameter is found, but do not
747 consume the comma, semicolon or closing delimiter. */
749 static void
750 c_parser_skip_to_end_of_parameter (c_parser *parser)
752 unsigned nesting_depth = 0;
754 while (true)
756 c_token *token = c_parser_peek_token (parser);
757 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
758 && !nesting_depth)
759 break;
760 /* If we've run out of tokens, stop. */
761 if (token->type == CPP_EOF)
762 return;
763 if (token->type == CPP_OPEN_BRACE
764 || token->type == CPP_OPEN_PAREN
765 || token->type == CPP_OPEN_SQUARE)
766 ++nesting_depth;
767 else if (token->type == CPP_CLOSE_BRACE
768 || token->type == CPP_CLOSE_PAREN
769 || token->type == CPP_CLOSE_SQUARE)
771 if (nesting_depth-- == 0)
772 break;
774 /* Consume this token. */
775 c_parser_consume_token (parser);
777 parser->error = false;
780 /* Skip tokens until we have consumed an entire block, or until we
781 have consumed a non-nested ';'. */
783 static void
784 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
786 unsigned nesting_depth = 0;
788 while (true)
790 c_token *token;
792 /* Peek at the next token. */
793 token = c_parser_peek_token (parser);
794 /* If we've run out of tokens, stop. */
795 if (token->type == CPP_EOF)
796 return;
797 /* If the next token is a ';', we have reached the end of the
798 statement. */
799 if (token->type == CPP_SEMICOLON && !nesting_depth)
801 /* Consume the ';'. */
802 c_parser_consume_token (parser);
803 break;
805 /* If the next token is a non-nested '}', then we have reached
806 the end of the current block. */
807 if (token->type == CPP_CLOSE_BRACE
808 && (nesting_depth == 0 || --nesting_depth == 0))
810 c_parser_consume_token (parser);
811 break;
813 /* If it the next token is a '{', then we are entering a new
814 block. Consume the entire block. */
815 if (token->type == CPP_OPEN_BRACE)
816 ++nesting_depth;
817 c_parser_consume_token (parser);
819 parser->error = false;
823 /* Save the warning flags which are controlled by __extension__. */
825 static inline int
826 disable_extension_diagnostics (void)
828 int ret = (pedantic
829 | (warn_pointer_arith << 1)
830 | (warn_traditional << 2)
831 | (flag_iso << 3));
832 pedantic = 0;
833 warn_pointer_arith = 0;
834 warn_traditional = 0;
835 flag_iso = 0;
836 return ret;
839 /* Restore the warning flags which are controlled by __extension__.
840 FLAGS is the return value from disable_extension_diagnostics. */
842 static inline void
843 restore_extension_diagnostics (int flags)
845 pedantic = flags & 1;
846 warn_pointer_arith = (flags >> 1) & 1;
847 warn_traditional = (flags >> 2) & 1;
848 flag_iso = (flags >> 3) & 1;
851 /* Possibly kinds of declarator to parse. */
852 typedef enum c_dtr_syn {
853 /* A normal declarator with an identifier. */
854 C_DTR_NORMAL,
855 /* An abstract declarator (maybe empty). */
856 C_DTR_ABSTRACT,
857 /* A parameter declarator: may be either, but after a type name does
858 not redeclare a typedef name as an identifier if it can
859 alternatively be interpreted as a typedef name; see DR#009,
860 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
861 following DR#249. For example, given a typedef T, "int T" and
862 "int *T" are valid parameter declarations redeclaring T, while
863 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
864 abstract declarators rather than involving redundant parentheses;
865 the same applies with attributes inside the parentheses before
866 "T". */
867 C_DTR_PARM
868 } c_dtr_syn;
870 static void c_parser_external_declaration (c_parser *);
871 static void c_parser_asm_definition (c_parser *);
872 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
873 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
874 bool);
875 static struct c_typespec c_parser_enum_specifier (c_parser *);
876 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
877 static tree c_parser_struct_declaration (c_parser *);
878 static struct c_typespec c_parser_typeof_specifier (c_parser *);
879 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
880 bool *);
881 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
882 c_dtr_syn, bool *);
883 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
884 bool,
885 struct c_declarator *);
886 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
887 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
888 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
889 static tree c_parser_simple_asm_expr (c_parser *);
890 static tree c_parser_attributes (c_parser *);
891 static struct c_type_name *c_parser_type_name (c_parser *);
892 static struct c_expr c_parser_initializer (c_parser *);
893 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
894 static void c_parser_initelt (c_parser *);
895 static void c_parser_initval (c_parser *, struct c_expr *);
896 static tree c_parser_compound_statement (c_parser *);
897 static void c_parser_compound_statement_nostart (c_parser *);
898 static void c_parser_label (c_parser *);
899 static void c_parser_statement (c_parser *);
900 static void c_parser_statement_after_labels (c_parser *);
901 static void c_parser_if_statement (c_parser *);
902 static void c_parser_switch_statement (c_parser *);
903 static void c_parser_while_statement (c_parser *);
904 static void c_parser_do_statement (c_parser *);
905 static void c_parser_for_statement (c_parser *);
906 static tree c_parser_asm_statement (c_parser *);
907 static tree c_parser_asm_operands (c_parser *, bool);
908 static tree c_parser_asm_clobbers (c_parser *);
909 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
910 static struct c_expr c_parser_conditional_expression (c_parser *,
911 struct c_expr *);
912 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
913 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
914 static struct c_expr c_parser_unary_expression (c_parser *);
915 static struct c_expr c_parser_sizeof_expression (c_parser *);
916 static struct c_expr c_parser_alignof_expression (c_parser *);
917 static struct c_expr c_parser_postfix_expression (c_parser *);
918 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
919 struct c_type_name *);
920 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
921 struct c_expr);
922 static struct c_expr c_parser_expression (c_parser *);
923 static struct c_expr c_parser_expression_conv (c_parser *);
924 static tree c_parser_expr_list (c_parser *, bool);
926 /* These Objective-C parser functions are only ever called when
927 compiling Objective-C. */
928 static void c_parser_objc_class_definition (c_parser *);
929 static void c_parser_objc_class_instance_variables (c_parser *);
930 static void c_parser_objc_class_declaration (c_parser *);
931 static void c_parser_objc_alias_declaration (c_parser *);
932 static void c_parser_objc_protocol_definition (c_parser *);
933 static enum tree_code c_parser_objc_method_type (c_parser *);
934 static void c_parser_objc_method_definition (c_parser *);
935 static void c_parser_objc_methodprotolist (c_parser *);
936 static void c_parser_objc_methodproto (c_parser *);
937 static tree c_parser_objc_method_decl (c_parser *);
938 static tree c_parser_objc_type_name (c_parser *);
939 static tree c_parser_objc_protocol_refs (c_parser *);
940 static void c_parser_objc_try_catch_statement (c_parser *);
941 static void c_parser_objc_synchronized_statement (c_parser *);
942 static tree c_parser_objc_selector (c_parser *);
943 static tree c_parser_objc_selector_arg (c_parser *);
944 static tree c_parser_objc_receiver (c_parser *);
945 static tree c_parser_objc_message_args (c_parser *);
946 static tree c_parser_objc_keywordexpr (c_parser *);
948 /* Parse a translation unit (C90 6.7, C99 6.9).
950 translation-unit:
951 external-declarations
953 external-declarations:
954 external-declaration
955 external-declarations external-declaration
957 GNU extensions:
959 translation-unit:
960 empty
963 static void
964 c_parser_translation_unit (c_parser *parser)
966 if (c_parser_next_token_is (parser, CPP_EOF))
968 if (pedantic)
969 pedwarn ("ISO C forbids an empty source file");
971 else
973 void *obstack_position = obstack_alloc (&parser_obstack, 0);
976 ggc_collect ();
977 c_parser_external_declaration (parser);
978 obstack_free (&parser_obstack, obstack_position);
980 while (c_parser_next_token_is_not (parser, CPP_EOF));
984 /* Parse an external declaration (C90 6.7, C99 6.9).
986 external-declaration:
987 function-definition
988 declaration
990 GNU extensions:
992 external-declaration:
993 asm-definition
995 __extension__ external-declaration
997 Objective-C:
999 external-declaration:
1000 objc-class-definition
1001 objc-class-declaration
1002 objc-alias-declaration
1003 objc-protocol-definition
1004 objc-method-definition
1005 @end
1008 static void
1009 c_parser_external_declaration (c_parser *parser)
1011 int ext;
1012 switch (c_parser_peek_token (parser)->type)
1014 case CPP_KEYWORD:
1015 switch (c_parser_peek_token (parser)->keyword)
1017 case RID_EXTENSION:
1018 ext = disable_extension_diagnostics ();
1019 c_parser_consume_token (parser);
1020 c_parser_external_declaration (parser);
1021 restore_extension_diagnostics (ext);
1022 break;
1023 case RID_ASM:
1024 c_parser_asm_definition (parser);
1025 break;
1026 case RID_AT_INTERFACE:
1027 case RID_AT_IMPLEMENTATION:
1028 gcc_assert (c_dialect_objc ());
1029 c_parser_objc_class_definition (parser);
1030 break;
1031 case RID_AT_CLASS:
1032 gcc_assert (c_dialect_objc ());
1033 c_parser_objc_class_declaration (parser);
1034 break;
1035 case RID_AT_ALIAS:
1036 gcc_assert (c_dialect_objc ());
1037 c_parser_objc_alias_declaration (parser);
1038 break;
1039 case RID_AT_PROTOCOL:
1040 gcc_assert (c_dialect_objc ());
1041 c_parser_objc_protocol_definition (parser);
1042 break;
1043 case RID_AT_END:
1044 gcc_assert (c_dialect_objc ());
1045 c_parser_consume_token (parser);
1046 objc_finish_implementation ();
1047 break;
1048 default:
1049 goto decl_or_fndef;
1051 break;
1052 case CPP_SEMICOLON:
1053 if (pedantic)
1054 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1055 c_parser_consume_token (parser);
1056 break;
1057 case CPP_PLUS:
1058 case CPP_MINUS:
1059 if (c_dialect_objc ())
1061 c_parser_objc_method_definition (parser);
1062 break;
1064 /* Else fall through, and yield a syntax error trying to parse
1065 as a declaration or function definition. */
1066 default:
1067 decl_or_fndef:
1068 /* A declaration or a function definition. We can only tell
1069 which after parsing the declaration specifiers, if any, and
1070 the first declarator. */
1071 c_parser_declaration_or_fndef (parser, true, true, false, true);
1072 break;
1076 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1077 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1078 accepted; otherwise (old-style parameter declarations) only other
1079 declarations are accepted. If NESTED is true, we are inside a
1080 function or parsing old-style parameter declarations; any functions
1081 encountered are nested functions and declaration specifiers are
1082 required; otherwise we are at top level and functions are normal
1083 functions and declaration specifiers may be optional. If EMPTY_OK
1084 is true, empty declarations are OK (subject to all other
1085 constraints); otherwise (old-style parameter declarations) they are
1086 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1087 may start with attributes; otherwise they may not.
1089 declaration:
1090 declaration-specifiers init-declarator-list[opt] ;
1092 function-definition:
1093 declaration-specifiers[opt] declarator declaration-list[opt]
1094 compound-statement
1096 declaration-list:
1097 declaration
1098 declaration-list declaration
1100 init-declarator-list:
1101 init-declarator
1102 init-declarator-list , init-declarator
1104 init-declarator:
1105 declarator simple-asm-expr[opt] attributes[opt]
1106 declarator simple-asm-expr[opt] attributes[opt] = initializer
1108 GNU extensions:
1110 nested-function-definition:
1111 declaration-specifiers declarator declaration-list[opt]
1112 compound-statement
1114 The simple-asm-expr and attributes are GNU extensions.
1116 This function does not handle __extension__; that is handled in its
1117 callers. ??? Following the old parser, __extension__ may start
1118 external declarations, declarations in functions and declarations
1119 at the start of "for" loops, but not old-style parameter
1120 declarations.
1122 C99 requires declaration specifiers in a function definition; the
1123 absence is diagnosed through the diagnosis of implicit int. In GNU
1124 C we also allow but diagnose declarations without declaration
1125 specifiers, but only at top level (elsewhere they conflict with
1126 other syntax). */
1128 static void
1129 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1130 bool nested, bool start_attr_ok)
1132 struct c_declspecs *specs;
1133 tree prefix_attrs;
1134 tree all_prefix_attrs;
1135 bool diagnosed_no_specs = false;
1136 specs = build_null_declspecs ();
1137 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1138 if (parser->error)
1140 c_parser_skip_to_end_of_block_or_statement (parser);
1141 return;
1143 if (nested && !specs->declspecs_seen_p)
1145 c_parser_error (parser, "expected declaration specifiers");
1146 c_parser_skip_to_end_of_block_or_statement (parser);
1147 return;
1149 finish_declspecs (specs);
1150 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1152 if (empty_ok)
1153 shadow_tag (specs);
1154 else
1156 shadow_tag_warned (specs, 1);
1157 pedwarn ("empty declaration");
1159 c_parser_consume_token (parser);
1160 return;
1162 pending_xref_error ();
1163 prefix_attrs = specs->attrs;
1164 all_prefix_attrs = prefix_attrs;
1165 specs->attrs = NULL_TREE;
1166 while (true)
1168 struct c_declarator *declarator;
1169 bool dummy = false;
1170 tree fnbody;
1171 /* Declaring either one or more declarators (in which case we
1172 should diagnose if there were no declaration specifiers) or a
1173 function definition (in which case the diagnostic for
1174 implicit int suffices). */
1175 declarator = c_parser_declarator (parser, specs->type_seen_p,
1176 C_DTR_NORMAL, &dummy);
1177 if (declarator == NULL)
1179 c_parser_skip_to_end_of_block_or_statement (parser);
1180 return;
1182 if (c_parser_next_token_is (parser, CPP_EQ)
1183 || c_parser_next_token_is (parser, CPP_COMMA)
1184 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1185 || c_parser_next_token_is_keyword (parser, RID_ASM)
1186 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1188 tree asm_name = NULL_TREE;
1189 tree postfix_attrs = NULL_TREE;
1190 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1192 diagnosed_no_specs = true;
1193 pedwarn ("data definition has no type or storage class");
1195 /* Having seen a data definition, there cannot now be a
1196 function definition. */
1197 fndef_ok = false;
1198 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1199 asm_name = c_parser_simple_asm_expr (parser);
1200 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1201 postfix_attrs = c_parser_attributes (parser);
1202 if (c_parser_next_token_is (parser, CPP_EQ))
1204 tree d;
1205 struct c_expr init;
1206 c_parser_consume_token (parser);
1207 /* The declaration of the variable is in effect while
1208 its initializer is parsed. */
1209 d = start_decl (declarator, specs, true,
1210 chainon (postfix_attrs, all_prefix_attrs));
1211 if (!d)
1212 d = error_mark_node;
1213 start_init (d, asm_name, global_bindings_p ());
1214 init = c_parser_initializer (parser);
1215 finish_init ();
1216 if (d != error_mark_node)
1218 maybe_warn_string_init (TREE_TYPE (d), init);
1219 finish_decl (d, init.value, asm_name);
1222 else
1224 tree d = start_decl (declarator, specs, false,
1225 chainon (postfix_attrs,
1226 all_prefix_attrs));
1227 if (d)
1228 finish_decl (d, NULL_TREE, asm_name);
1230 if (c_parser_next_token_is (parser, CPP_COMMA))
1232 c_parser_consume_token (parser);
1233 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1234 all_prefix_attrs = chainon (c_parser_attributes (parser),
1235 prefix_attrs);
1236 else
1237 all_prefix_attrs = prefix_attrs;
1238 continue;
1240 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1242 c_parser_consume_token (parser);
1243 return;
1245 else
1247 c_parser_error (parser, "expected %<,%> or %<;%>");
1248 c_parser_skip_to_end_of_block_or_statement (parser);
1249 return;
1252 else if (!fndef_ok)
1254 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1255 "%<asm%> or %<__attribute__%>");
1256 c_parser_skip_to_end_of_block_or_statement (parser);
1257 return;
1259 /* Function definition (nested or otherwise). */
1260 if (nested)
1262 if (pedantic)
1263 pedwarn ("ISO C forbids nested functions");
1264 push_function_context ();
1266 if (!start_function (specs, declarator, all_prefix_attrs))
1268 /* This can appear in many cases looking nothing like a
1269 function definition, so we don't give a more specific
1270 error suggesting there was one. */
1271 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1272 "or %<__attribute__%>");
1273 if (nested)
1274 pop_function_context ();
1275 break;
1277 /* Parse old-style parameter declarations. ??? Attributes are
1278 not allowed to start declaration specifiers here because of a
1279 syntax conflict between a function declaration with attribute
1280 suffix and a function definition with an attribute prefix on
1281 first old-style parameter declaration. Following the old
1282 parser, they are not accepted on subsequent old-style
1283 parameter declarations either. However, there is no
1284 ambiguity after the first declaration, nor indeed on the
1285 first as long as we don't allow postfix attributes after a
1286 declarator with a nonempty identifier list in a definition;
1287 and postfix attributes have never been accepted here in
1288 function definitions either. */
1289 while (c_parser_next_token_is_not (parser, CPP_EOF)
1290 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1291 c_parser_declaration_or_fndef (parser, false, false, true, false);
1292 DECL_SOURCE_LOCATION (current_function_decl)
1293 = c_parser_peek_token (parser)->location;
1294 store_parm_decls ();
1295 fnbody = c_parser_compound_statement (parser);
1296 if (nested)
1298 tree decl = current_function_decl;
1299 add_stmt (fnbody);
1300 finish_function ();
1301 pop_function_context ();
1302 add_stmt (build_stmt (DECL_EXPR, decl));
1304 else
1306 add_stmt (fnbody);
1307 finish_function ();
1309 break;
1313 /* Parse an asm-definition (asm() outside a function body). This is a
1314 GNU extension.
1316 asm-definition:
1317 simple-asm-expr ;
1320 static void
1321 c_parser_asm_definition (c_parser *parser)
1323 tree asm_str = c_parser_simple_asm_expr (parser);
1324 /* ??? This only works sensibly in the presence of
1325 -fno-unit-at-a-time; file-scope asms really need to be passed to
1326 cgraph which needs to preserve the order of functions and
1327 file-scope asms. */
1328 if (asm_str)
1329 assemble_asm (asm_str);
1330 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1333 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1334 6.7), adding them to SPECS (which may already include some).
1335 Storage class specifiers are accepted iff SCSPEC_OK; type
1336 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1337 the start iff START_ATTR_OK.
1339 declaration-specifiers:
1340 storage-class-specifier declaration-specifiers[opt]
1341 type-specifier declaration-specifiers[opt]
1342 type-qualifier declaration-specifiers[opt]
1343 function-specifier declaration-specifiers[opt]
1345 Function specifiers (inline) are from C99, and are currently
1346 handled as storage class specifiers, as is __thread.
1348 C90 6.5.1, C99 6.7.1:
1349 storage-class-specifier:
1350 typedef
1351 extern
1352 static
1353 auto
1354 register
1356 C99 6.7.4:
1357 function-specifier:
1358 inline
1360 C90 6.5.2, C99 6.7.2:
1361 type-specifier:
1362 void
1363 char
1364 short
1366 long
1367 float
1368 double
1369 signed
1370 unsigned
1371 _Bool
1372 _Complex
1373 [_Imaginary removed in C99 TC2]
1374 struct-or-union-specifier
1375 enum-specifier
1376 typedef-name
1378 (_Bool and _Complex are new in C99.)
1380 C90 6.5.3, C99 6.7.3:
1382 type-qualifier:
1383 const
1384 restrict
1385 volatile
1387 (restrict is new in C99.)
1389 GNU extensions:
1391 declaration-specifiers:
1392 attributes declaration-specifiers[opt]
1394 storage-class-specifier:
1395 __thread
1397 type-specifier:
1398 typeof-specifier
1400 Objective-C:
1402 type-specifier:
1403 class-name objc-protocol-refs[opt]
1404 typedef-name objc-protocol-refs
1405 objc-protocol-refs
1408 static void
1409 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1410 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1412 bool attrs_ok = start_attr_ok;
1413 bool seen_type = specs->type_seen_p;
1414 while (c_parser_next_token_is (parser, CPP_NAME)
1415 || c_parser_next_token_is (parser, CPP_KEYWORD)
1416 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1418 struct c_typespec t;
1419 tree attrs;
1420 if (c_parser_next_token_is (parser, CPP_NAME))
1422 tree value = c_parser_peek_token (parser)->value;
1423 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1424 /* This finishes the specifiers unless a type name is OK, it
1425 is declared as a type name and a type name hasn't yet
1426 been seen. */
1427 if (!typespec_ok || seen_type
1428 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1429 break;
1430 c_parser_consume_token (parser);
1431 seen_type = true;
1432 attrs_ok = true;
1433 if (kind == C_ID_TYPENAME
1434 && (!c_dialect_objc ()
1435 || c_parser_next_token_is_not (parser, CPP_LESS)))
1437 t.kind = ctsk_typedef;
1438 /* For a typedef name, record the meaning, not the name.
1439 In case of 'foo foo, bar;'. */
1440 t.spec = lookup_name (value);
1442 else
1444 tree proto = NULL_TREE;
1445 gcc_assert (c_dialect_objc ());
1446 t.kind = ctsk_objc;
1447 if (c_parser_next_token_is (parser, CPP_LESS))
1448 proto = c_parser_objc_protocol_refs (parser);
1449 t.spec = objc_get_protocol_qualified_type (value, proto);
1451 declspecs_add_type (specs, t);
1452 continue;
1454 if (c_parser_next_token_is (parser, CPP_LESS))
1456 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1457 nisse@lysator.liu.se. */
1458 tree proto;
1459 gcc_assert (c_dialect_objc ());
1460 if (!typespec_ok || seen_type)
1461 break;
1462 proto = c_parser_objc_protocol_refs (parser);
1463 t.kind = ctsk_objc;
1464 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1465 declspecs_add_type (specs, t);
1466 continue;
1468 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1469 switch (c_parser_peek_token (parser)->keyword)
1471 case RID_STATIC:
1472 case RID_EXTERN:
1473 case RID_REGISTER:
1474 case RID_TYPEDEF:
1475 case RID_INLINE:
1476 case RID_AUTO:
1477 case RID_THREAD:
1478 if (!scspec_ok)
1479 goto out;
1480 attrs_ok = true;
1481 /* TODO: Distinguish between function specifiers (inline)
1482 and storage class specifiers, either here or in
1483 declspecs_add_scspec. */
1484 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1485 c_parser_consume_token (parser);
1486 break;
1487 case RID_UNSIGNED:
1488 case RID_LONG:
1489 case RID_SHORT:
1490 case RID_SIGNED:
1491 case RID_COMPLEX:
1492 case RID_INT:
1493 case RID_CHAR:
1494 case RID_FLOAT:
1495 case RID_DOUBLE:
1496 case RID_VOID:
1497 case RID_BOOL:
1498 if (!typespec_ok)
1499 goto out;
1500 attrs_ok = true;
1501 seen_type = true;
1502 OBJC_NEED_RAW_IDENTIFIER (1);
1503 t.kind = ctsk_resword;
1504 t.spec = c_parser_peek_token (parser)->value;
1505 declspecs_add_type (specs, t);
1506 c_parser_consume_token (parser);
1507 break;
1508 case RID_ENUM:
1509 if (!typespec_ok)
1510 goto out;
1511 attrs_ok = true;
1512 seen_type = true;
1513 t = c_parser_enum_specifier (parser);
1514 declspecs_add_type (specs, t);
1515 break;
1516 case RID_STRUCT:
1517 case RID_UNION:
1518 if (!typespec_ok)
1519 goto out;
1520 attrs_ok = true;
1521 seen_type = true;
1522 t = c_parser_struct_or_union_specifier (parser);
1523 declspecs_add_type (specs, t);
1524 break;
1525 case RID_TYPEOF:
1526 /* ??? The old parser rejected typeof after other type
1527 specifiers, but is a syntax error the best way of
1528 handling this? */
1529 if (!typespec_ok || seen_type)
1530 goto out;
1531 attrs_ok = true;
1532 seen_type = true;
1533 t = c_parser_typeof_specifier (parser);
1534 declspecs_add_type (specs, t);
1535 break;
1536 case RID_CONST:
1537 case RID_VOLATILE:
1538 case RID_RESTRICT:
1539 attrs_ok = true;
1540 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1541 c_parser_consume_token (parser);
1542 break;
1543 case RID_ATTRIBUTE:
1544 if (!attrs_ok)
1545 goto out;
1546 attrs = c_parser_attributes (parser);
1547 declspecs_add_attrs (specs, attrs);
1548 break;
1549 default:
1550 goto out;
1553 out: ;
1556 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1558 enum-specifier:
1559 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1560 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1561 enum attributes[opt] identifier
1563 The form with trailing comma is new in C99. The forms with
1564 attributes are GNU extensions. In GNU C, we accept any expression
1565 without commas in the syntax (assignment expressions, not just
1566 conditional expressions); assignment expressions will be diagnosed
1567 as non-constant.
1569 enumerator-list:
1570 enumerator
1571 enumerator-list , enumerator
1573 enumerator:
1574 enumeration-constant
1575 enumeration-constant = constant-expression
1578 static struct c_typespec
1579 c_parser_enum_specifier (c_parser *parser)
1581 struct c_typespec ret;
1582 tree attrs;
1583 tree ident = NULL_TREE;
1584 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1585 c_parser_consume_token (parser);
1586 attrs = c_parser_attributes (parser);
1587 if (c_parser_next_token_is (parser, CPP_NAME))
1589 ident = c_parser_peek_token (parser)->value;
1590 c_parser_consume_token (parser);
1592 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1594 /* Parse an enum definition. */
1595 tree type = start_enum (ident);
1596 tree postfix_attrs;
1597 /* We chain the enumerators in reverse order, then put them in
1598 forward order at the end. */
1599 tree values = NULL_TREE;
1600 c_parser_consume_token (parser);
1601 while (true)
1603 tree enum_id;
1604 tree enum_value;
1605 tree enum_decl;
1606 bool seen_comma;
1607 if (c_parser_next_token_is_not (parser, CPP_NAME))
1609 c_parser_error (parser, "expected identifier");
1610 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1611 values = error_mark_node;
1612 break;
1614 enum_id = c_parser_peek_token (parser)->value;
1615 c_parser_consume_token (parser);
1616 if (c_parser_next_token_is (parser, CPP_EQ))
1618 c_parser_consume_token (parser);
1619 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1621 else
1622 enum_value = NULL_TREE;
1623 enum_decl = build_enumerator (enum_id, enum_value);
1624 TREE_CHAIN (enum_decl) = values;
1625 values = enum_decl;
1626 seen_comma = false;
1627 if (c_parser_next_token_is (parser, CPP_COMMA))
1629 seen_comma = true;
1630 c_parser_consume_token (parser);
1632 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1634 if (seen_comma && pedantic && !flag_isoc99)
1635 pedwarn ("comma at end of enumerator list");
1636 c_parser_consume_token (parser);
1637 break;
1639 if (!seen_comma)
1641 c_parser_error (parser, "expected %<,%> or %<}%>");
1642 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1643 values = error_mark_node;
1644 break;
1647 postfix_attrs = c_parser_attributes (parser);
1648 ret.spec = finish_enum (type, nreverse (values),
1649 chainon (attrs, postfix_attrs));
1650 ret.kind = ctsk_tagdef;
1651 return ret;
1653 else if (!ident)
1655 c_parser_error (parser, "expected %<{%>");
1656 ret.spec = error_mark_node;
1657 ret.kind = ctsk_tagref;
1658 return ret;
1660 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1661 /* In ISO C, enumerated types can be referred to only if already
1662 defined. */
1663 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1664 pedwarn ("ISO C forbids forward references to %<enum%> types");
1665 return ret;
1668 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1670 struct-or-union-specifier:
1671 struct-or-union attributes[opt] identifier[opt]
1672 { struct-contents } attributes[opt]
1673 struct-or-union attributes[opt] identifier
1675 struct-contents:
1676 struct-declaration-list
1678 struct-declaration-list:
1679 struct-declaration ;
1680 struct-declaration-list struct-declaration ;
1682 GNU extensions:
1684 struct-contents:
1685 empty
1686 struct-declaration
1687 struct-declaration-list struct-declaration
1689 struct-declaration-list:
1690 struct-declaration-list ;
1693 (Note that in the syntax here, unlike that in ISO C, the semicolons
1694 are included here rather than in struct-declaration, in order to
1695 describe the syntax with extra semicolons and missing semicolon at
1696 end.)
1698 Objective-C:
1700 struct-declaration-list:
1701 @defs ( class-name )
1703 (Note this does not include a trailing semicolon, but can be
1704 followed by further declarations, and gets a pedwarn-if-pedantic
1705 when followed by a semicolon.) */
1707 static struct c_typespec
1708 c_parser_struct_or_union_specifier (c_parser *parser)
1710 struct c_typespec ret;
1711 tree attrs;
1712 tree ident = NULL_TREE;
1713 enum tree_code code;
1714 switch (c_parser_peek_token (parser)->keyword)
1716 case RID_STRUCT:
1717 code = RECORD_TYPE;
1718 break;
1719 case RID_UNION:
1720 code = UNION_TYPE;
1721 break;
1722 default:
1723 gcc_unreachable ();
1725 c_parser_consume_token (parser);
1726 attrs = c_parser_attributes (parser);
1727 if (c_parser_next_token_is (parser, CPP_NAME))
1729 ident = c_parser_peek_token (parser)->value;
1730 c_parser_consume_token (parser);
1732 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1734 /* Parse a struct or union definition. Start the scope of the
1735 tag before parsing components. */
1736 tree type = start_struct (code, ident);
1737 tree postfix_attrs;
1738 /* We chain the components in reverse order, then put them in
1739 forward order at the end. Each struct-declaration may
1740 declare multiple components (comma-separated), so we must use
1741 chainon to join them, although when parsing each
1742 struct-declaration we can use TREE_CHAIN directly.
1744 The theory behind all this is that there will be more
1745 semicolon separated fields than comma separated fields, and
1746 so we'll be minimizing the number of node traversals required
1747 by chainon. */
1748 tree contents = NULL_TREE;
1749 c_parser_consume_token (parser);
1750 /* Handle the Objective-C @defs construct,
1751 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1752 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1754 tree name;
1755 gcc_assert (c_dialect_objc ());
1756 c_parser_consume_token (parser);
1757 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1758 goto end_at_defs;
1759 if (c_parser_next_token_is (parser, CPP_NAME)
1760 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1762 name = c_parser_peek_token (parser)->value;
1763 c_parser_consume_token (parser);
1765 else
1767 c_parser_error (parser, "expected class name");
1768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1769 goto end_at_defs;
1771 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1772 "expected %<)%>");
1773 contents = nreverse (objc_get_class_ivars (name));
1775 end_at_defs:
1776 /* Parse the struct-declarations and semicolons. Problems with
1777 semicolons are diagnosed here; empty structures are diagnosed
1778 elsewhere. */
1779 while (true)
1781 tree decls;
1782 /* Parse any stray semicolon. */
1783 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1785 if (pedantic)
1786 pedwarn ("extra semicolon in struct or union specified");
1787 c_parser_consume_token (parser);
1788 continue;
1790 /* Stop if at the end of the struct or union contents. */
1791 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1793 c_parser_consume_token (parser);
1794 break;
1796 /* Parse some comma-separated declarations, but not the
1797 trailing semicolon if any. */
1798 decls = c_parser_struct_declaration (parser);
1799 contents = chainon (decls, contents);
1800 /* If no semicolon follows, either we have a parse error or
1801 are at the end of the struct or union and should
1802 pedwarn. */
1803 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1804 c_parser_consume_token (parser);
1805 else
1807 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1808 pedwarn ("no semicolon at end of struct or union");
1809 else
1811 c_parser_error (parser, "expected %<;%>");
1812 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1813 break;
1817 postfix_attrs = c_parser_attributes (parser);
1818 ret.spec = finish_struct (type, nreverse (contents),
1819 chainon (attrs, postfix_attrs));
1820 ret.kind = ctsk_tagdef;
1821 return ret;
1823 else if (!ident)
1825 c_parser_error (parser, "expected %<{%>");
1826 ret.spec = error_mark_node;
1827 ret.kind = ctsk_tagref;
1828 return ret;
1830 ret = parser_xref_tag (code, ident);
1831 return ret;
1834 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1835 the trailing semicolon.
1837 struct-declaration:
1838 specifier-qualifier-list struct-declarator-list
1840 specifier-qualifier-list:
1841 type-specifier specifier-qualifier-list[opt]
1842 type-qualifier specifier-qualifier-list[opt]
1843 attributes specifier-qualifier-list[opt]
1845 struct-declarator-list:
1846 struct-declarator
1847 struct-declarator-list , attributes[opt] struct-declarator
1849 struct-declarator:
1850 declarator attributes[opt]
1851 declarator[opt] : constant-expression attributes[opt]
1853 GNU extensions:
1855 struct-declaration:
1856 __extension__ struct-declaration
1857 specifier-qualifier-list
1859 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1860 of attributes where shown is a GNU extension. In GNU C, we accept
1861 any expression without commas in the syntax (assignment
1862 expressions, not just conditional expressions); assignment
1863 expressions will be diagnosed as non-constant. */
1865 static tree
1866 c_parser_struct_declaration (c_parser *parser)
1868 struct c_declspecs *specs;
1869 tree prefix_attrs;
1870 tree all_prefix_attrs;
1871 tree decls;
1872 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1874 int ext;
1875 tree decl;
1876 ext = disable_extension_diagnostics ();
1877 c_parser_consume_token (parser);
1878 decl = c_parser_struct_declaration (parser);
1879 restore_extension_diagnostics (ext);
1880 return decl;
1882 specs = build_null_declspecs ();
1883 c_parser_declspecs (parser, specs, false, true, true);
1884 if (parser->error)
1885 return NULL_TREE;
1886 if (!specs->declspecs_seen_p)
1888 c_parser_error (parser, "expected specifier-qualifier-list");
1889 return NULL_TREE;
1891 finish_declspecs (specs);
1892 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1894 tree ret;
1895 if (!specs->type_seen_p)
1897 if (pedantic)
1898 pedwarn ("ISO C forbids member declarations with no members");
1899 shadow_tag_warned (specs, pedantic);
1900 ret = NULL_TREE;
1902 else
1904 /* Support for unnamed structs or unions as members of
1905 structs or unions (which is [a] useful and [b] supports
1906 MS P-SDK). */
1907 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1909 return ret;
1911 pending_xref_error ();
1912 prefix_attrs = specs->attrs;
1913 all_prefix_attrs = prefix_attrs;
1914 specs->attrs = NULL_TREE;
1915 decls = NULL_TREE;
1916 while (true)
1918 /* Declaring one or more declarators or un-named bit-fields. */
1919 struct c_declarator *declarator;
1920 bool dummy = false;
1921 if (c_parser_next_token_is (parser, CPP_COLON))
1922 declarator = build_id_declarator (NULL_TREE);
1923 else
1924 declarator = c_parser_declarator (parser, specs->type_seen_p,
1925 C_DTR_NORMAL, &dummy);
1926 if (declarator == NULL)
1928 c_parser_skip_to_end_of_block_or_statement (parser);
1929 break;
1931 if (c_parser_next_token_is (parser, CPP_COLON)
1932 || c_parser_next_token_is (parser, CPP_COMMA)
1933 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1934 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1935 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1937 tree postfix_attrs = NULL_TREE;
1938 tree width = NULL_TREE;
1939 tree d;
1940 if (c_parser_next_token_is (parser, CPP_COLON))
1942 c_parser_consume_token (parser);
1943 width = c_parser_expr_no_commas (parser, NULL).value;
1945 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1946 postfix_attrs = c_parser_attributes (parser);
1947 d = grokfield (declarator, specs, width);
1948 decl_attributes (&d, chainon (postfix_attrs,
1949 all_prefix_attrs), 0);
1950 TREE_CHAIN (d) = decls;
1951 decls = d;
1952 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1953 all_prefix_attrs = chainon (c_parser_attributes (parser),
1954 prefix_attrs);
1955 else
1956 all_prefix_attrs = prefix_attrs;
1957 if (c_parser_next_token_is (parser, CPP_COMMA))
1958 c_parser_consume_token (parser);
1959 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
1960 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1962 /* Semicolon consumed in caller. */
1963 break;
1965 else
1967 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
1968 break;
1971 else
1973 c_parser_error (parser,
1974 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
1975 "%<__attribute__%>");
1976 break;
1979 return decls;
1982 /* Parse a typeof specifier (a GNU extension).
1984 typeof-specifier:
1985 typeof ( expression )
1986 typeof ( type-name )
1989 static struct c_typespec
1990 c_parser_typeof_specifier (c_parser *parser)
1992 struct c_typespec ret;
1993 ret.kind = ctsk_typeof;
1994 ret.spec = error_mark_node;
1995 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
1996 c_parser_consume_token (parser);
1997 skip_evaluation++;
1998 in_typeof++;
1999 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2001 skip_evaluation--;
2002 in_typeof--;
2003 return ret;
2005 if (c_parser_next_token_starts_typename (parser))
2007 struct c_type_name *type = c_parser_type_name (parser);
2008 skip_evaluation--;
2009 in_typeof--;
2010 if (type != NULL)
2012 ret.spec = groktypename (type);
2013 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2016 else
2018 struct c_expr expr = c_parser_expression (parser);
2019 skip_evaluation--;
2020 in_typeof--;
2021 if (TREE_CODE (expr.value) == COMPONENT_REF
2022 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2023 error ("%<typeof%> applied to a bit-field");
2024 ret.spec = TREE_TYPE (expr.value);
2025 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2027 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2028 return ret;
2031 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2032 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2033 be redeclared; otherwise it may not. KIND indicates which kind of
2034 declarator is wanted. Returns a valid declarator except in the
2035 case of a syntax error in which case NULL is returned. *SEEN_ID is
2036 set to true if an identifier being declared is seen; this is used
2037 to diagnose bad forms of abstract array declarators and to
2038 determine whether an identifier list is syntactically permitted.
2040 declarator:
2041 pointer[opt] direct-declarator
2043 direct-declarator:
2044 identifier
2045 ( attributes[opt] declarator )
2046 direct-declarator array-declarator
2047 direct-declarator ( parameter-type-list )
2048 direct-declarator ( identifier-list[opt] )
2050 pointer:
2051 * type-qualifier-list[opt]
2052 * type-qualifier-list[opt] pointer
2054 type-qualifier-list:
2055 type-qualifier
2056 attributes
2057 type-qualifier-list type-qualifier
2058 type-qualifier-list attributes
2060 parameter-type-list:
2061 parameter-list
2062 parameter-list , ...
2064 parameter-list:
2065 parameter-declaration
2066 parameter-list , parameter-declaration
2068 parameter-declaration:
2069 declaration-specifiers declarator attributes[opt]
2070 declaration-specifiers abstract-declarator[opt] attributes[opt]
2072 identifier-list:
2073 identifier
2074 identifier-list , identifier
2076 abstract-declarator:
2077 pointer
2078 pointer[opt] direct-abstract-declarator
2080 direct-abstract-declarator:
2081 ( attributes[opt] abstract-declarator )
2082 direct-abstract-declarator[opt] array-declarator
2083 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2085 GNU extensions:
2087 direct-declarator:
2088 direct-declarator ( parameter-forward-declarations
2089 parameter-type-list[opt] )
2091 direct-abstract-declarator:
2092 direct-abstract-declarator[opt] ( parameter-forward-declarations
2093 parameter-type-list[opt] )
2095 parameter-forward-declarations:
2096 parameter-list ;
2097 parameter-forward-declarations parameter-list ;
2099 The uses of attributes shown above are GNU extensions.
2101 Some forms of array declarator are not included in C99 in the
2102 syntax for abstract declarators; these are disallowed elsewhere.
2103 This may be a defect (DR#289).
2105 This function also accepts an omitted abstract declarator as being
2106 an abstract declarator, although not part of the formal syntax. */
2108 static struct c_declarator *
2109 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2110 bool *seen_id)
2112 /* Parse any initial pointer part. */
2113 if (c_parser_next_token_is (parser, CPP_MULT))
2115 struct c_declspecs *quals_attrs = build_null_declspecs ();
2116 struct c_declarator *inner;
2117 c_parser_consume_token (parser);
2118 c_parser_declspecs (parser, quals_attrs, false, false, true);
2119 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2120 if (inner == NULL)
2121 return NULL;
2122 else
2123 return make_pointer_declarator (quals_attrs, inner);
2125 /* Now we have a direct declarator, direct abstract declarator or
2126 nothing (which counts as a direct abstract declarator here). */
2127 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2130 /* Parse a direct declarator or direct abstract declarator; arguments
2131 as c_parser_declarator. */
2133 static struct c_declarator *
2134 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2135 bool *seen_id)
2137 /* The direct declarator must start with an identifier (possibly
2138 omitted) or a parenthesized declarator (possibly abstract). In
2139 an ordinary declarator, initial parentheses must start a
2140 parenthesized declarator. In an abstract declarator or parameter
2141 declarator, they could start a parenthesized declarator or a
2142 parameter list. To tell which, the open parenthesis and any
2143 following attributes must be read. If a declaration specifier
2144 follows, then it is a parameter list; if the specifier is a
2145 typedef name, there might be an ambiguity about redeclaring it,
2146 which is resolved in the direction of treating it as a typedef
2147 name. If a close parenthesis follows, it is also an empty
2148 parameter list, as the syntax does not permit empty abstract
2149 declarators. Otherwise, it is a parenthesized declarator (in
2150 which case the analysis may be repeated inside it, recursively).
2152 ??? There is an ambiguity in a parameter declaration "int
2153 (__attribute__((foo)) x)", where x is not a typedef name: it
2154 could be an abstract declarator for a function, or declare x with
2155 parentheses. The proper resolution of this ambiguity needs
2156 documenting. At present we follow an accident of the old
2157 parser's implementation, whereby the first parameter must have
2158 some declaration specifiers other than just attributes. Thus as
2159 a parameter declaration it is treated as a parenthesized
2160 parameter named x, and as an abstract declarator it is
2161 rejected.
2163 ??? Also following the old parser, attributes inside an empty
2164 parameter list are ignored, making it a list not yielding a
2165 prototype, rather than giving an error or making it have one
2166 parameter with implicit type int.
2168 ??? Also following the old parser, typedef names may be
2169 redeclared in declarators, but not Objective-C class names. */
2171 if (kind != C_DTR_ABSTRACT
2172 && c_parser_next_token_is (parser, CPP_NAME)
2173 && ((type_seen_p
2174 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2175 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2177 struct c_declarator *inner
2178 = build_id_declarator (c_parser_peek_token (parser)->value);
2179 *seen_id = true;
2180 inner->id_loc = c_parser_peek_token (parser)->location;
2181 c_parser_consume_token (parser);
2182 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2185 if (kind != C_DTR_NORMAL
2186 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2188 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2189 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2192 /* Either we are at the end of an abstract declarator, or we have
2193 parentheses. */
2195 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2197 tree attrs;
2198 struct c_declarator *inner;
2199 c_parser_consume_token (parser);
2200 attrs = c_parser_attributes (parser);
2201 if (kind != C_DTR_NORMAL
2202 && (c_parser_next_token_starts_declspecs (parser)
2203 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2205 struct c_arg_info *args
2206 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2207 attrs);
2208 if (args == NULL)
2209 return NULL;
2210 else
2212 inner
2213 = build_function_declarator (args,
2214 build_id_declarator (NULL_TREE));
2215 return c_parser_direct_declarator_inner (parser, *seen_id,
2216 inner);
2219 /* A parenthesized declarator. */
2220 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2221 if (inner != NULL && attrs != NULL)
2222 inner = build_attrs_declarator (attrs, inner);
2223 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2225 c_parser_consume_token (parser);
2226 if (inner == NULL)
2227 return NULL;
2228 else
2229 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2231 else
2233 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2234 "expected %<)%>");
2235 return NULL;
2238 else
2240 if (kind == C_DTR_NORMAL)
2242 c_parser_error (parser, "expected identifier or %<(%>");
2243 return NULL;
2245 else
2246 return build_id_declarator (NULL_TREE);
2250 /* Parse part of a direct declarator or direct abstract declarator,
2251 given that some (in INNER) has already been parsed; ID_PRESENT is
2252 true if an identifier is present, false for an abstract
2253 declarator. */
2255 static struct c_declarator *
2256 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2257 struct c_declarator *inner)
2259 /* Parse a sequence of array declarators and parameter lists. */
2260 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2262 struct c_declarator *declarator;
2263 struct c_declspecs *quals_attrs = build_null_declspecs ();
2264 bool static_seen;
2265 bool star_seen;
2266 tree dimen;
2267 c_parser_consume_token (parser);
2268 c_parser_declspecs (parser, quals_attrs, false, false, true);
2269 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2270 if (static_seen)
2271 c_parser_consume_token (parser);
2272 if (static_seen && !quals_attrs->declspecs_seen_p)
2273 c_parser_declspecs (parser, quals_attrs, false, false, true);
2274 if (!quals_attrs->declspecs_seen_p)
2275 quals_attrs = NULL;
2276 /* If "static" is present, there must be an array dimension.
2277 Otherwise, there may be a dimension, "*", or no
2278 dimension. */
2279 if (static_seen)
2281 star_seen = false;
2282 dimen = c_parser_expr_no_commas (parser, NULL).value;
2284 else
2286 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2288 dimen = NULL_TREE;
2289 star_seen = false;
2291 else if (c_parser_next_token_is (parser, CPP_MULT))
2293 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2295 dimen = NULL_TREE;
2296 star_seen = true;
2297 c_parser_consume_token (parser);
2299 else
2301 star_seen = false;
2302 dimen = c_parser_expr_no_commas (parser, NULL).value;
2305 else
2307 star_seen = false;
2308 dimen = c_parser_expr_no_commas (parser, NULL).value;
2311 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2312 c_parser_consume_token (parser);
2313 else
2315 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2316 "expected %<]%>");
2317 return NULL;
2319 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2320 star_seen);
2321 inner = set_array_declarator_inner (declarator, inner, !id_present);
2322 return c_parser_direct_declarator_inner (parser, id_present, inner);
2324 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2326 tree attrs;
2327 struct c_arg_info *args;
2328 c_parser_consume_token (parser);
2329 attrs = c_parser_attributes (parser);
2330 args = c_parser_parms_declarator (parser, id_present, attrs);
2331 if (args == NULL)
2332 return NULL;
2333 else
2335 inner = build_function_declarator (args, inner);
2336 return c_parser_direct_declarator_inner (parser, id_present, inner);
2339 return inner;
2342 /* Parse a parameter list or identifier list, including the closing
2343 parenthesis but not the opening one. ATTRS are the attributes at
2344 the start of the list. ID_LIST_OK is true if an identifier list is
2345 acceptable; such a list must not have attributes at the start. */
2347 static struct c_arg_info *
2348 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2350 push_scope ();
2351 declare_parm_level ();
2352 /* If the list starts with an identifier, it is an identifier list.
2353 Otherwise, it is either a prototype list or an empty list. */
2354 if (id_list_ok
2355 && !attrs
2356 && c_parser_next_token_is (parser, CPP_NAME)
2357 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2359 tree list = NULL_TREE, *nextp = &list;
2360 while (c_parser_next_token_is (parser, CPP_NAME)
2361 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2363 *nextp = build_tree_list (NULL_TREE,
2364 c_parser_peek_token (parser)->value);
2365 nextp = & TREE_CHAIN (*nextp);
2366 c_parser_consume_token (parser);
2367 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2368 break;
2369 c_parser_consume_token (parser);
2370 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2372 c_parser_error (parser, "expected identifier");
2373 break;
2376 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2378 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2379 ret->parms = 0;
2380 ret->tags = 0;
2381 ret->types = list;
2382 ret->others = 0;
2383 c_parser_consume_token (parser);
2384 pop_scope ();
2385 return ret;
2387 else
2389 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2390 "expected %<)%>");
2391 pop_scope ();
2392 return NULL;
2395 else
2397 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2398 pop_scope ();
2399 return ret;
2403 /* Parse a parameter list (possibly empty), including the closing
2404 parenthesis but not the opening one. ATTRS are the attributes at
2405 the start of the list. */
2407 static struct c_arg_info *
2408 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2410 bool good_parm = false;
2411 /* ??? Following the old parser, forward parameter declarations may
2412 use abstract declarators, and if no real parameter declarations
2413 follow the forward declarations then this is not diagnosed. Also
2414 note as above that attributes are ignored as the only contents of
2415 the parentheses, or as the only contents after forward
2416 declarations. */
2417 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2419 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2420 ret->parms = 0;
2421 ret->tags = 0;
2422 ret->types = 0;
2423 ret->others = 0;
2424 c_parser_consume_token (parser);
2425 return ret;
2427 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2429 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2430 ret->parms = 0;
2431 ret->tags = 0;
2432 ret->others = 0;
2433 /* Suppress -Wold-style-definition for this case. */
2434 ret->types = error_mark_node;
2435 error ("ISO C requires a named argument before %<...%>");
2436 c_parser_consume_token (parser);
2437 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2439 c_parser_consume_token (parser);
2440 return ret;
2442 else
2444 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2445 "expected %<)%>");
2446 return NULL;
2449 /* Nonempty list of parameters, either terminated with semicolon
2450 (forward declarations; recurse) or with close parenthesis (normal
2451 function) or with ", ... )" (variadic function). */
2452 while (true)
2454 /* Parse a parameter. */
2455 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2456 attrs = NULL_TREE;
2457 if (parm != NULL)
2459 good_parm = true;
2460 push_parm_decl (parm);
2462 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2464 tree new_attrs;
2465 c_parser_consume_token (parser);
2466 new_attrs = c_parser_attributes (parser);
2467 return c_parser_parms_list_declarator (parser, new_attrs);
2469 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2471 c_parser_consume_token (parser);
2472 if (good_parm)
2473 return get_parm_info (false);
2474 else
2476 struct c_arg_info *ret
2477 = XOBNEW (&parser_obstack, struct c_arg_info);
2478 ret->parms = 0;
2479 ret->tags = 0;
2480 ret->types = 0;
2481 ret->others = 0;
2482 return ret;
2485 if (!c_parser_require (parser, CPP_COMMA,
2486 "expected %<;%>, %<,%> or %<)%>"))
2488 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2489 return NULL;
2491 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2493 c_parser_consume_token (parser);
2494 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2496 c_parser_consume_token (parser);
2497 if (good_parm)
2498 return get_parm_info (true);
2499 else
2501 struct c_arg_info *ret
2502 = XOBNEW (&parser_obstack, struct c_arg_info);
2503 ret->parms = 0;
2504 ret->tags = 0;
2505 ret->types = 0;
2506 ret->others = 0;
2507 return ret;
2510 else
2512 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2513 "expected %<)%>");
2514 return NULL;
2520 /* Parse a parameter declaration. ATTRS are the attributes at the
2521 start of the declaration if it is the first parameter. */
2523 static struct c_parm *
2524 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2526 struct c_declspecs *specs;
2527 struct c_declarator *declarator;
2528 tree prefix_attrs;
2529 tree postfix_attrs = NULL_TREE;
2530 bool dummy = false;
2531 if (!c_parser_next_token_starts_declspecs (parser))
2533 /* ??? In some Objective-C cases '...' isn't applicable so there
2534 should be a different message. */
2535 c_parser_error (parser,
2536 "expected declaration specifiers or %<...%>");
2537 c_parser_skip_to_end_of_parameter (parser);
2538 return NULL;
2540 specs = build_null_declspecs ();
2541 if (attrs)
2543 declspecs_add_attrs (specs, attrs);
2544 attrs = NULL_TREE;
2546 c_parser_declspecs (parser, specs, true, true, true);
2547 finish_declspecs (specs);
2548 pending_xref_error ();
2549 prefix_attrs = specs->attrs;
2550 specs->attrs = NULL_TREE;
2551 declarator = c_parser_declarator (parser, specs->type_seen_p,
2552 C_DTR_PARM, &dummy);
2553 if (declarator == NULL)
2555 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2556 return NULL;
2558 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2559 postfix_attrs = c_parser_attributes (parser);
2560 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2561 declarator);
2564 /* Parse a string literal in an asm expression. It should not be
2565 translated, and wide string literals are an error although
2566 permitted by the syntax. This is a GNU extension.
2568 asm-string-literal:
2569 string-literal
2571 ??? At present, following the old parser, the caller needs to have
2572 set c_lex_string_translate to 0. It would be better to follow the
2573 C++ parser rather than using the c_lex_string_translate kludge. */
2575 static tree
2576 c_parser_asm_string_literal (c_parser *parser)
2578 tree str;
2579 if (c_parser_next_token_is (parser, CPP_STRING))
2581 str = c_parser_peek_token (parser)->value;
2582 c_parser_consume_token (parser);
2584 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2586 error ("wide string literal in %<asm%>");
2587 str = build_string (1, "");
2588 c_parser_consume_token (parser);
2590 else
2592 c_parser_error (parser, "expected string literal");
2593 str = NULL_TREE;
2595 return str;
2598 /* Parse a simple asm expression. This is used in restricted
2599 contexts, where a full expression with inputs and outputs does not
2600 make sense. This is a GNU extension.
2602 simple-asm-expr:
2603 asm ( asm-string-literal )
2606 static tree
2607 c_parser_simple_asm_expr (c_parser *parser)
2609 tree str;
2610 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2611 /* ??? Follow the C++ parser rather than using the
2612 c_lex_string_translate kludge. */
2613 c_lex_string_translate = 0;
2614 c_parser_consume_token (parser);
2615 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2617 c_lex_string_translate = 1;
2618 return NULL_TREE;
2620 str = c_parser_asm_string_literal (parser);
2621 c_lex_string_translate = 1;
2622 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2625 return NULL_TREE;
2627 return str;
2630 /* Parse (possibly empty) attributes. This is a GNU extension.
2632 attributes:
2633 empty
2634 attributes attribute
2636 attribute:
2637 __attribute__ ( ( attribute-list ) )
2639 attribute-list:
2640 attrib
2641 attribute_list , attrib
2643 attrib:
2644 empty
2645 any-word
2646 any-word ( identifier )
2647 any-word ( identifier , nonempty-expr-list )
2648 any-word ( expr-list )
2650 where the "identifier" must not be declared as a type, and
2651 "any-word" may be any identifier (including one declared as a
2652 type), a reserved word storage class specifier, type specifier or
2653 type qualifier. ??? This still leaves out most reserved keywords
2654 (following the old parser), shouldn't we include them, and why not
2655 allow identifiers declared as types to start the arguments? */
2657 static tree
2658 c_parser_attributes (c_parser *parser)
2660 tree attrs = NULL_TREE;
2661 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2663 /* ??? Follow the C++ parser rather than using the
2664 c_lex_string_translate kludge. */
2665 c_lex_string_translate = 0;
2666 c_parser_consume_token (parser);
2667 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2669 c_lex_string_translate = 1;
2670 return attrs;
2672 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2674 c_lex_string_translate = 1;
2675 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2676 return attrs;
2678 /* Parse the attribute list. */
2679 while (c_parser_next_token_is (parser, CPP_COMMA)
2680 || c_parser_next_token_is (parser, CPP_NAME)
2681 || c_parser_next_token_is (parser, CPP_KEYWORD))
2683 tree attr, attr_name, attr_args;
2684 if (c_parser_next_token_is (parser, CPP_COMMA))
2686 c_parser_consume_token (parser);
2687 continue;
2689 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2691 /* ??? See comment above about what keywords are
2692 accepted here. */
2693 bool ok;
2694 switch (c_parser_peek_token (parser)->keyword)
2696 case RID_STATIC:
2697 case RID_UNSIGNED:
2698 case RID_LONG:
2699 case RID_CONST:
2700 case RID_EXTERN:
2701 case RID_REGISTER:
2702 case RID_TYPEDEF:
2703 case RID_SHORT:
2704 case RID_INLINE:
2705 case RID_VOLATILE:
2706 case RID_SIGNED:
2707 case RID_AUTO:
2708 case RID_RESTRICT:
2709 case RID_COMPLEX:
2710 case RID_THREAD:
2711 case RID_INT:
2712 case RID_CHAR:
2713 case RID_FLOAT:
2714 case RID_DOUBLE:
2715 case RID_VOID:
2716 case RID_BOOL:
2717 ok = true;
2718 break;
2719 default:
2720 ok = false;
2721 break;
2723 if (!ok)
2724 break;
2726 attr_name = c_parser_peek_token (parser)->value;
2727 c_parser_consume_token (parser);
2728 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2730 attr = build_tree_list (attr_name, NULL_TREE);
2731 attrs = chainon (attrs, attr);
2732 continue;
2734 c_parser_consume_token (parser);
2735 /* Parse the attribute contents. If they start with an
2736 identifier which is followed by a comma or close
2737 parenthesis, then the arguments start with that
2738 identifier; otherwise they are an expression list. */
2739 if (c_parser_next_token_is (parser, CPP_NAME)
2740 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2741 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2742 || (c_parser_peek_2nd_token (parser)->type
2743 == CPP_CLOSE_PAREN)))
2745 tree arg1 = c_parser_peek_token (parser)->value;
2746 c_parser_consume_token (parser);
2747 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2748 attr_args = build_tree_list (NULL_TREE, arg1);
2749 else
2751 c_parser_consume_token (parser);
2752 attr_args = tree_cons (NULL_TREE, arg1,
2753 c_parser_expr_list (parser, false));
2756 else
2758 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2759 attr_args = NULL_TREE;
2760 else
2761 attr_args = c_parser_expr_list (parser, false);
2763 attr = build_tree_list (attr_name, attr_args);
2764 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2765 c_parser_consume_token (parser);
2766 else
2768 c_lex_string_translate = 1;
2769 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2770 "expected %<)%>");
2771 return attrs;
2773 attrs = chainon (attrs, attr);
2775 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2776 c_parser_consume_token (parser);
2777 else
2779 c_lex_string_translate = 1;
2780 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2781 "expected %<)%>");
2782 return attrs;
2784 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2785 c_parser_consume_token (parser);
2786 else
2788 c_lex_string_translate = 1;
2789 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2790 "expected %<)%>");
2791 return attrs;
2793 c_lex_string_translate = 1;
2795 return attrs;
2798 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2800 type-name:
2801 specifier-qualifier-list abstract-declarator[opt]
2804 static struct c_type_name *
2805 c_parser_type_name (c_parser *parser)
2807 struct c_declspecs *specs = build_null_declspecs ();
2808 struct c_declarator *declarator;
2809 struct c_type_name *ret;
2810 bool dummy = false;
2811 c_parser_declspecs (parser, specs, false, true, true);
2812 if (!specs->declspecs_seen_p)
2814 c_parser_error (parser, "expected specifier-qualifier-list");
2815 return NULL;
2817 pending_xref_error ();
2818 finish_declspecs (specs);
2819 declarator = c_parser_declarator (parser, specs->type_seen_p,
2820 C_DTR_ABSTRACT, &dummy);
2821 if (declarator == NULL)
2822 return NULL;
2823 ret = XOBNEW (&parser_obstack, struct c_type_name);
2824 ret->specs = specs;
2825 ret->declarator = declarator;
2826 return ret;
2829 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2831 initializer:
2832 assignment-expression
2833 { initializer-list }
2834 { initializer-list , }
2836 initializer-list:
2837 designation[opt] initializer
2838 initializer-list , designation[opt] initializer
2840 designation:
2841 designator-list =
2843 designator-list:
2844 designator
2845 designator-list designator
2847 designator:
2848 array-designator
2849 . identifier
2851 array-designator:
2852 [ constant-expression ]
2854 GNU extensions:
2856 initializer:
2859 designation:
2860 array-designator
2861 identifier :
2863 array-designator:
2864 [ constant-expression ... constant-expression ]
2866 Any expression without commas is accepted in the syntax for the
2867 constant-expressions, with non-constant expressions rejected later.
2869 This function is only used for top-level initializers; for nested
2870 ones, see c_parser_initval. */
2872 static struct c_expr
2873 c_parser_initializer (c_parser *parser)
2875 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2876 return c_parser_braced_init (parser, NULL_TREE, false);
2877 else
2879 struct c_expr ret;
2880 ret = c_parser_expr_no_commas (parser, NULL);
2881 if (TREE_CODE (ret.value) != STRING_CST
2882 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
2883 ret.value = default_function_array_conversion (ret.value);
2884 return ret;
2888 /* Parse a braced initializer list. TYPE is the type specified for a
2889 compound literal, and NULL_TREE for other initializers and for
2890 nested braced lists. NESTED_P is true for nested braced lists,
2891 false for the list of a compound literal or the list that is the
2892 top-level initializer in a declaration. */
2894 static struct c_expr
2895 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2897 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2898 c_parser_consume_token (parser);
2899 if (nested_p)
2900 push_init_level (0);
2901 else
2902 really_start_incremental_init (type);
2903 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2905 if (pedantic)
2906 pedwarn ("ISO C forbids empty initializer braces");
2908 else
2910 /* Parse a non-empty initializer list, possibly with a trailing
2911 comma. */
2912 while (true)
2914 c_parser_initelt (parser);
2915 if (parser->error)
2916 break;
2917 if (c_parser_next_token_is (parser, CPP_COMMA))
2918 c_parser_consume_token (parser);
2919 else
2920 break;
2921 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2922 break;
2925 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
2927 struct c_expr ret;
2928 ret.value = error_mark_node;
2929 ret.original_code = ERROR_MARK;
2930 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
2931 return ret;
2933 c_parser_consume_token (parser);
2934 return pop_init_level (0);
2937 /* Parse a nested initializer, including designators. */
2939 static void
2940 c_parser_initelt (c_parser *parser)
2942 /* Parse any designator or designator list. A single array
2943 designator may have the subsequent "=" omitted in GNU C, but a
2944 longer list or a structure member designator may not. */
2945 if (c_parser_next_token_is (parser, CPP_NAME)
2946 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
2948 /* Old-style structure member designator. */
2949 set_init_label (c_parser_peek_token (parser)->value);
2950 if (pedantic)
2951 pedwarn ("obsolete use of designated initializer with %<:%>");
2952 c_parser_consume_token (parser);
2953 c_parser_consume_token (parser);
2955 else
2957 /* des_seen is 0 if there have been no designators, 1 if there
2958 has been a single array designator and 2 otherwise. */
2959 int des_seen = 0;
2960 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
2961 || c_parser_next_token_is (parser, CPP_DOT))
2963 int des_prev = des_seen;
2964 if (des_seen < 2)
2965 des_seen++;
2966 if (c_parser_next_token_is (parser, CPP_DOT))
2968 des_seen = 2;
2969 c_parser_consume_token (parser);
2970 if (c_parser_next_token_is (parser, CPP_NAME))
2972 set_init_label (c_parser_peek_token (parser)->value);
2973 c_parser_consume_token (parser);
2975 else
2977 struct c_expr init;
2978 init.value = error_mark_node;
2979 init.original_code = ERROR_MARK;
2980 c_parser_error (parser, "expected identifier");
2981 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2982 process_init_element (init);
2983 return;
2986 else
2988 tree first, second;
2989 /* ??? Following the old parser, [ objc-receiver
2990 objc-message-args ] is accepted as an initializer,
2991 being distinguished from a designator by what follows
2992 the first assignment expression inside the square
2993 brackets, but after a first array designator a
2994 subsequent square bracket is for Objective-C taken to
2995 start an expression, using the obsolete form of
2996 designated initializer without '=', rather than
2997 possibly being a second level of designation: in LALR
2998 terms, the '[' is shifted rather than reducing
2999 designator to designator-list. */
3000 if (des_prev == 1 && c_dialect_objc ())
3002 des_seen = des_prev;
3003 break;
3005 if (des_prev == 0 && c_dialect_objc ())
3007 /* This might be an array designator or an
3008 Objective-C message expression. If the former,
3009 continue parsing here; if the latter, parse the
3010 remainder of the initializer given the starting
3011 primary-expression. ??? It might make sense to
3012 distinguish when des_prev == 1 as well; see
3013 previous comment. */
3014 tree rec, args;
3015 struct c_expr mexpr;
3016 c_parser_consume_token (parser);
3017 if (c_parser_peek_token (parser)->type == CPP_NAME
3018 && ((c_parser_peek_token (parser)->id_kind
3019 == C_ID_TYPENAME)
3020 || (c_parser_peek_token (parser)->id_kind
3021 == C_ID_CLASSNAME)))
3023 /* Type name receiver. */
3024 tree id = c_parser_peek_token (parser)->value;
3025 c_parser_consume_token (parser);
3026 rec = objc_get_class_reference (id);
3027 goto parse_message_args;
3029 first = c_parser_expr_no_commas (parser, NULL).value;
3030 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3031 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3032 goto array_desig_after_first;
3033 /* Expression receiver. So far only one part
3034 without commas has been parsed; there might be
3035 more of the expression. */
3036 rec = first;
3037 while (c_parser_next_token_is (parser, CPP_COMMA))
3039 tree next;
3040 c_parser_consume_token (parser);
3041 next = c_parser_expr_no_commas (parser, NULL).value;
3042 next = default_function_array_conversion (next);
3043 rec = build_compound_expr (rec, next);
3045 parse_message_args:
3046 /* Now parse the objc-message-args. */
3047 args = c_parser_objc_message_args (parser);
3048 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3049 "expected %<]%>");
3050 mexpr.value
3051 = objc_build_message_expr (build_tree_list (rec, args));
3052 mexpr.original_code = ERROR_MARK;
3053 /* Now parse and process the remainder of the
3054 initializer, starting with this message
3055 expression as a primary-expression. */
3056 c_parser_initval (parser, &mexpr);
3057 return;
3059 c_parser_consume_token (parser);
3060 first = c_parser_expr_no_commas (parser, NULL).value;
3061 array_desig_after_first:
3062 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3064 c_parser_consume_token (parser);
3065 second = c_parser_expr_no_commas (parser, NULL).value;
3067 else
3068 second = NULL_TREE;
3069 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3071 c_parser_consume_token (parser);
3072 set_init_index (first, second);
3073 if (pedantic && second)
3074 pedwarn ("ISO C forbids specifying range of "
3075 "elements to initialize");
3077 else
3078 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3079 "expected %<]%>");
3082 if (des_seen >= 1)
3084 if (c_parser_next_token_is (parser, CPP_EQ))
3086 if (pedantic && !flag_isoc99)
3087 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3088 c_parser_consume_token (parser);
3090 else
3092 if (des_seen == 1)
3094 if (pedantic)
3095 pedwarn ("obsolete use of designated initializer "
3096 "without %<=%>");
3098 else
3100 struct c_expr init;
3101 init.value = error_mark_node;
3102 init.original_code = ERROR_MARK;
3103 c_parser_error (parser, "expected %<=%>");
3104 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3105 process_init_element (init);
3106 return;
3111 c_parser_initval (parser, NULL);
3114 /* Parse a nested initializer; as c_parser_initializer but parses
3115 initializers within braced lists, after any designators have been
3116 applied. If AFTER is not NULL then it is an Objective-C message
3117 expression which is the primary-expression starting the
3118 initializer. */
3120 static void
3121 c_parser_initval (c_parser *parser, struct c_expr *after)
3123 struct c_expr init;
3124 gcc_assert (!after || c_dialect_objc ());
3125 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3126 init = c_parser_braced_init (parser, NULL_TREE, true);
3127 else
3129 init = c_parser_expr_no_commas (parser, after);
3130 if (init.value != NULL_TREE
3131 && TREE_CODE (init.value) != STRING_CST
3132 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3133 init.value = default_function_array_conversion (init.value);
3135 process_init_element (init);
3138 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3139 C99 6.8.2).
3141 compound-statement:
3142 { block-item-list[opt] }
3143 { label-declarations block-item-list }
3145 block-item-list:
3146 block-item
3147 block-item-list block-item
3149 block-item:
3150 nested-declaration
3151 statement
3153 nested-declaration:
3154 declaration
3156 GNU extensions:
3158 compound-statement:
3159 { label-declarations block-item-list }
3161 nested-declaration:
3162 __extension__ nested-declaration
3163 nested-function-definition
3165 label-declarations:
3166 label-declaration
3167 label-declarations label-declaration
3169 label-declaration:
3170 __label__ identifier-list ;
3172 Allowing the mixing of declarations and code is new in C99. The
3173 GNU syntax also permits (not shown above) labels at the end of
3174 compound statements, which yield an error. We don't allow labels
3175 on declarations; this might seem like a natural extension, but
3176 there would be a conflict between attributes on the label and
3177 prefix attributes on the declaration. ??? The syntax follows the
3178 old parser in requiring something after label declarations.
3179 Although they are erroneous if the labels declared aren't defined,
3180 is it useful for the syntax to be this way? */
3182 static tree
3183 c_parser_compound_statement (c_parser *parser)
3185 tree stmt;
3186 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3187 return error_mark_node;
3188 stmt = c_begin_compound_stmt (true);
3189 c_parser_compound_statement_nostart (parser);
3190 return c_end_compound_stmt (stmt, true);
3193 /* Parse a compound statement except for the opening brace. This is
3194 used for parsing both compound statements and statement expressions
3195 (which follow different paths to handling the opening). */
3197 static void
3198 c_parser_compound_statement_nostart (c_parser *parser)
3200 bool last_stmt = false;
3201 bool last_label = false;
3202 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3204 c_parser_consume_token (parser);
3205 return;
3207 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3209 /* Read zero or more forward-declarations for labels that nested
3210 functions can jump to. */
3211 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3213 c_parser_consume_token (parser);
3214 /* Any identifiers, including those declared as type names,
3215 are OK here. */
3216 while (true)
3218 tree label;
3219 if (c_parser_next_token_is_not (parser, CPP_NAME))
3221 c_parser_error (parser, "expected identifier");
3222 break;
3224 label
3225 = declare_label (c_parser_peek_token (parser)->value);
3226 C_DECLARED_LABEL_FLAG (label) = 1;
3227 add_stmt (build_stmt (DECL_EXPR, label));
3228 c_parser_consume_token (parser);
3229 if (c_parser_next_token_is (parser, CPP_COMMA))
3230 c_parser_consume_token (parser);
3231 else
3232 break;
3234 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3236 /* ??? Locating this diagnostic on the token after the
3237 declarations end follows the old parser, but it might be
3238 better to locate it where the declarations start instead. */
3239 if (pedantic)
3240 pedwarn ("ISO C forbids label declarations");
3242 /* We must now have at least one statement, label or declaration. */
3243 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3245 c_parser_error (parser, "expected declaration or statement");
3246 c_parser_consume_token (parser);
3247 return;
3249 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3251 location_t loc = c_parser_peek_token (parser)->location;
3252 if (c_parser_next_token_is (parser, CPP_EOF))
3254 c_parser_error (parser, "expected declaration or statement");
3255 return;
3257 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3258 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3259 || (c_parser_next_token_is (parser, CPP_NAME)
3260 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3262 last_label = true;
3263 last_stmt = false;
3264 c_parser_label (parser);
3266 else if (!last_label
3267 && c_parser_next_token_starts_declspecs (parser))
3269 last_label = false;
3270 c_parser_declaration_or_fndef (parser, true, true, true, true);
3271 if (last_stmt
3272 && ((pedantic && !flag_isoc99)
3273 || warn_declaration_after_statement))
3274 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3275 &loc);
3276 last_stmt = false;
3278 else if (!last_label
3279 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3281 /* __extension__ can start a declaration, but is also an
3282 unary operator that can start an expression. Consume all
3283 but the last of a possible series of __extension__ to
3284 determine which. */
3285 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3286 && (c_parser_peek_2nd_token (parser)->keyword
3287 == RID_EXTENSION))
3288 c_parser_consume_token (parser);
3289 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3291 int ext;
3292 ext = disable_extension_diagnostics ();
3293 c_parser_consume_token (parser);
3294 last_label = false;
3295 c_parser_declaration_or_fndef (parser, true, true, true, true);
3296 /* Following the old parser, __extension__ does not
3297 disable this diagnostic. */
3298 restore_extension_diagnostics (ext);
3299 if (last_stmt
3300 && ((pedantic && !flag_isoc99)
3301 || warn_declaration_after_statement))
3302 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3303 &loc);
3304 last_stmt = false;
3306 else
3307 goto statement;
3309 else
3311 statement:
3312 last_label = false;
3313 last_stmt = true;
3314 c_parser_statement_after_labels (parser);
3317 if (last_label)
3318 error ("label at end of compound statement");
3319 c_parser_consume_token (parser);
3322 /* Parse a label (C90 6.6.1, C99 6.8.1).
3324 label:
3325 identifier : attributes[opt]
3326 case constant-expression :
3327 default :
3329 GNU extensions:
3331 label:
3332 case constant-expression ... constant-expression :
3334 The use of attributes on labels is a GNU extension. The syntax in
3335 GNU C accepts any expressions without commas, non-constant
3336 expressions being rejected later. */
3338 static void
3339 c_parser_label (c_parser *parser)
3341 location_t loc1 = c_parser_peek_token (parser)->location;
3342 tree label = NULL_TREE;
3343 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3345 tree exp1, exp2;
3346 c_parser_consume_token (parser);
3347 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3348 if (c_parser_next_token_is (parser, CPP_COLON))
3350 c_parser_consume_token (parser);
3351 label = do_case (exp1, NULL_TREE);
3353 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3355 c_parser_consume_token (parser);
3356 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3357 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3358 label = do_case (exp1, exp2);
3360 else
3361 c_parser_error (parser, "expected %<:%> or %<...%>");
3363 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3365 c_parser_consume_token (parser);
3366 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3367 label = do_case (NULL_TREE, NULL_TREE);
3369 else
3371 tree name = c_parser_peek_token (parser)->value;
3372 tree tlab;
3373 location_t loc2;
3374 tree attrs;
3375 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3376 c_parser_consume_token (parser);
3377 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3378 loc2 = c_parser_peek_token (parser)->location;
3379 c_parser_consume_token (parser);
3380 attrs = c_parser_attributes (parser);
3381 tlab = define_label (loc2, name);
3382 if (tlab)
3384 decl_attributes (&tlab, attrs, 0);
3385 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3388 if (label)
3389 SET_EXPR_LOCATION (label, loc1);
3392 /* Parse a statement (C90 6.6, C99 6.8).
3394 statement:
3395 labeled-statement
3396 compound-statement
3397 expression-statement
3398 selection-statement
3399 iteration-statement
3400 jump-statement
3402 labeled-statement:
3403 label statement
3405 expression-statement:
3406 expression[opt] ;
3408 selection-statement:
3409 if-statement
3410 switch-statement
3412 iteration-statement:
3413 while-statement
3414 do-statement
3415 for-statement
3417 jump-statement:
3418 goto identifier ;
3419 continue ;
3420 break ;
3421 return expression[opt] ;
3423 GNU extensions:
3425 statement:
3426 asm-statement
3428 jump-statement:
3429 goto * expression ;
3431 Objective-C:
3433 statement:
3434 objc-throw-statement
3435 objc-try-catch-statement
3436 objc-synchronized-statement
3438 objc-throw-statement:
3439 @throw expression ;
3440 @throw ;
3443 static void
3444 c_parser_statement (c_parser *parser)
3446 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3447 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3448 || (c_parser_next_token_is (parser, CPP_NAME)
3449 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3450 c_parser_label (parser);
3451 c_parser_statement_after_labels (parser);
3454 /* Parse a statement, other than a labeled statement. */
3456 static void
3457 c_parser_statement_after_labels (c_parser *parser)
3459 location_t loc = c_parser_peek_token (parser)->location;
3460 tree stmt = NULL_TREE;
3461 switch (c_parser_peek_token (parser)->type)
3463 case CPP_OPEN_BRACE:
3464 add_stmt (c_parser_compound_statement (parser));
3465 break;
3466 case CPP_KEYWORD:
3467 switch (c_parser_peek_token (parser)->keyword)
3469 case RID_IF:
3470 c_parser_if_statement (parser);
3471 break;
3472 case RID_SWITCH:
3473 c_parser_switch_statement (parser);
3474 break;
3475 case RID_WHILE:
3476 c_parser_while_statement (parser);
3477 break;
3478 case RID_DO:
3479 c_parser_do_statement (parser);
3480 break;
3481 case RID_FOR:
3482 c_parser_for_statement (parser);
3483 break;
3484 case RID_GOTO:
3485 c_parser_consume_token (parser);
3486 if (c_parser_next_token_is (parser, CPP_NAME))
3488 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3489 c_parser_consume_token (parser);
3491 else if (c_parser_next_token_is (parser, CPP_MULT))
3493 c_parser_consume_token (parser);
3494 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3496 else
3497 c_parser_error (parser, "expected identifier or %<*%>");
3498 goto expect_semicolon;
3499 case RID_CONTINUE:
3500 c_parser_consume_token (parser);
3501 stmt = c_finish_bc_stmt (&c_cont_label, false);
3502 goto expect_semicolon;
3503 case RID_BREAK:
3504 c_parser_consume_token (parser);
3505 stmt = c_finish_bc_stmt (&c_break_label, true);
3506 goto expect_semicolon;
3507 case RID_RETURN:
3508 c_parser_consume_token (parser);
3509 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3511 stmt = c_finish_return (NULL_TREE);
3512 c_parser_consume_token (parser);
3514 else
3516 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3517 goto expect_semicolon;
3519 break;
3520 case RID_ASM:
3521 stmt = c_parser_asm_statement (parser);
3522 break;
3523 case RID_AT_THROW:
3524 gcc_assert (c_dialect_objc ());
3525 c_parser_consume_token (parser);
3526 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3528 stmt = objc_build_throw_stmt (NULL_TREE);
3529 c_parser_consume_token (parser);
3531 else
3533 stmt
3534 = objc_build_throw_stmt (c_parser_expression (parser).value);
3535 goto expect_semicolon;
3537 break;
3538 case RID_AT_TRY:
3539 gcc_assert (c_dialect_objc ());
3540 c_parser_objc_try_catch_statement (parser);
3541 break;
3542 case RID_AT_SYNCHRONIZED:
3543 gcc_assert (c_dialect_objc ());
3544 c_parser_objc_synchronized_statement (parser);
3545 break;
3546 default:
3547 goto expr_stmt;
3549 break;
3550 case CPP_SEMICOLON:
3551 c_parser_consume_token (parser);
3552 break;
3553 case CPP_CLOSE_PAREN:
3554 case CPP_CLOSE_SQUARE:
3555 /* Avoid infinite loop in error recovery:
3556 c_parser_skip_until_found stops at a closing nesting
3557 delimiter without consuming it, but here we need to consume
3558 it to proceed further. */
3559 c_parser_error (parser, "expected statement");
3560 c_parser_consume_token (parser);
3561 break;
3562 default:
3563 expr_stmt:
3564 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3565 expect_semicolon:
3566 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3567 break;
3569 /* Two cases cannot and do not have line numbers associated: If stmt
3570 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3571 cannot hold line numbers. But that's OK because the statement
3572 will either be changed to a MODIFY_EXPR during gimplification of
3573 the statement expr, or discarded. If stmt was compound, but
3574 without new variables, we will have skipped the creation of a
3575 BIND and will have a bare STATEMENT_LIST. But that's OK because
3576 (recursively) all of the component statements should already have
3577 line numbers assigned. ??? Can we discard no-op statements
3578 earlier? */
3579 if (stmt && EXPR_P (stmt))
3580 SET_EXPR_LOCATION (stmt, loc);
3583 /* Parse a parenthesized condition from an if, do or while statement.
3585 condition:
3586 ( expression )
3588 static tree
3589 c_parser_paren_condition (c_parser *parser)
3591 location_t loc;
3592 tree cond;
3593 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3594 return error_mark_node;
3595 loc = c_parser_peek_token (parser)->location;
3596 cond = c_objc_common_truthvalue_conversion
3597 (c_parser_expression_conv (parser).value);
3598 if (EXPR_P (cond))
3599 SET_EXPR_LOCATION (cond, loc);
3600 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3601 return cond;
3604 /* Parse a statement which is a block in C99. */
3606 static tree
3607 c_parser_c99_block_statement (c_parser *parser)
3609 tree block = c_begin_compound_stmt (flag_isoc99);
3610 c_parser_statement (parser);
3611 return c_end_compound_stmt (block, flag_isoc99);
3614 /* Parse the body of an if statement or the else half thereof. This
3615 is just parsing a statement but (a) it is a block in C99, (b) we
3616 track whether the body is an if statement for the sake of
3617 -Wparentheses warnings, (c) we handle an empty body specially for
3618 the sake of -Wextra warnings. */
3620 static tree
3621 c_parser_if_body (c_parser *parser, bool *if_p)
3623 tree block = c_begin_compound_stmt (flag_isoc99);
3624 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3625 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3626 || (c_parser_next_token_is (parser, CPP_NAME)
3627 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3628 c_parser_label (parser);
3629 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3630 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3631 add_stmt (build (NOP_EXPR, NULL_TREE, NULL_TREE));
3632 c_parser_statement_after_labels (parser);
3633 return c_end_compound_stmt (block, flag_isoc99);
3636 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3638 if-statement:
3639 if ( expression ) statement
3640 if ( expression ) statement else statement
3643 static void
3644 c_parser_if_statement (c_parser *parser)
3646 tree block;
3647 location_t loc;
3648 tree cond;
3649 bool first_if = false, second_if = false;
3650 tree first_body, second_body;
3651 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3652 c_parser_consume_token (parser);
3653 block = c_begin_compound_stmt (flag_isoc99);
3654 loc = c_parser_peek_token (parser)->location;
3655 cond = c_parser_paren_condition (parser);
3656 first_body = c_parser_if_body (parser, &first_if);
3657 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3659 c_parser_consume_token (parser);
3660 second_body = c_parser_if_body (parser, &second_if);
3662 else
3663 second_body = NULL_TREE;
3664 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3665 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3668 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3670 switch-statement:
3671 switch (expression) statement
3674 static void
3675 c_parser_switch_statement (c_parser *parser)
3677 tree block, expr, body, save_break;
3678 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3679 c_parser_consume_token (parser);
3680 block = c_begin_compound_stmt (flag_isoc99);
3681 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3683 expr = c_parser_expression (parser).value;
3684 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3686 else
3687 expr = error_mark_node;
3688 c_start_case (expr);
3689 save_break = c_break_label;
3690 c_break_label = NULL_TREE;
3691 body = c_parser_c99_block_statement (parser);
3692 c_finish_case (body);
3693 if (c_break_label)
3694 add_stmt (build (LABEL_EXPR, void_type_node, c_break_label));
3695 c_break_label = save_break;
3696 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3699 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3701 while-statement:
3702 while (expression) statement
3705 static void
3706 c_parser_while_statement (c_parser *parser)
3708 tree block, cond, body, save_break, save_cont;
3709 location_t loc;
3710 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3711 c_parser_consume_token (parser);
3712 block = c_begin_compound_stmt (flag_isoc99);
3713 loc = c_parser_peek_token (parser)->location;
3714 cond = c_parser_paren_condition (parser);
3715 save_break = c_break_label;
3716 c_break_label = NULL_TREE;
3717 save_cont = c_cont_label;
3718 c_cont_label = NULL_TREE;
3719 body = c_parser_c99_block_statement (parser);
3720 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3721 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3722 c_break_label = save_break;
3723 c_cont_label = save_cont;
3726 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3728 do-statement:
3729 do statement while ( expression ) ;
3732 static void
3733 c_parser_do_statement (c_parser *parser)
3735 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3736 location_t loc;
3737 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3738 c_parser_consume_token (parser);
3739 block = c_begin_compound_stmt (flag_isoc99);
3740 loc = c_parser_peek_token (parser)->location;
3741 save_break = c_break_label;
3742 c_break_label = NULL_TREE;
3743 save_cont = c_cont_label;
3744 c_cont_label = NULL_TREE;
3745 body = c_parser_c99_block_statement (parser);
3746 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3747 new_break = c_break_label;
3748 c_break_label = save_break;
3749 new_cont = c_cont_label;
3750 c_cont_label = save_cont;
3751 cond = c_parser_paren_condition (parser);
3752 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3753 c_parser_skip_to_end_of_block_or_statement (parser);
3754 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3755 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3758 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3760 for-statement:
3761 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3762 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3764 The form with a declaration is new in C99.
3766 ??? In accordance with the old parser, the declaration may be a
3767 nested function, which is then rejected in check_for_loop_decls,
3768 but does it make any sense for this to be included in the grammar?
3769 Note in particular that the nested function does not include a
3770 trailing ';', whereas the "declaration" production includes one.
3771 Also, can we reject bad declarations earlier and cheaper than
3772 check_for_loop_decls? */
3774 static void
3775 c_parser_for_statement (c_parser *parser)
3777 tree block, cond, incr, save_break, save_cont, body;
3778 location_t loc = UNKNOWN_LOCATION;
3779 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3780 c_parser_consume_token (parser);
3781 block = c_begin_compound_stmt (flag_isoc99);
3782 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3784 /* Parse the initialization declaration or expression. */
3785 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3787 c_parser_consume_token (parser);
3788 c_finish_expr_stmt (NULL_TREE);
3790 else if (c_parser_next_token_starts_declspecs (parser))
3792 c_parser_declaration_or_fndef (parser, true, true, true, true);
3793 check_for_loop_decls ();
3795 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3797 /* __extension__ can start a declaration, but is also an
3798 unary operator that can start an expression. Consume all
3799 but the last of a possible series of __extension__ to
3800 determine which. */
3801 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3802 && (c_parser_peek_2nd_token (parser)->keyword
3803 == RID_EXTENSION))
3804 c_parser_consume_token (parser);
3805 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3807 int ext;
3808 ext = disable_extension_diagnostics ();
3809 c_parser_consume_token (parser);
3810 c_parser_declaration_or_fndef (parser, true, true, true, true);
3811 restore_extension_diagnostics (ext);
3812 check_for_loop_decls ();
3814 else
3815 goto init_expr;
3817 else
3819 init_expr:
3820 c_finish_expr_stmt (c_parser_expression (parser).value);
3821 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3823 /* Parse the loop condition. */
3824 loc = c_parser_peek_token (parser)->location;
3825 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3827 c_parser_consume_token (parser);
3828 cond = NULL_TREE;
3830 else
3832 tree ocond = c_parser_expression_conv (parser).value;
3833 cond = c_objc_common_truthvalue_conversion (ocond);
3834 if (EXPR_P (cond))
3835 SET_EXPR_LOCATION (cond, loc);
3836 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3838 /* Parse the increment expression. */
3839 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3840 incr = c_process_expr_stmt (NULL_TREE);
3841 else
3842 incr = c_process_expr_stmt (c_parser_expression (parser).value);
3843 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3845 else
3847 cond = error_mark_node;
3848 incr = error_mark_node;
3850 save_break = c_break_label;
3851 c_break_label = NULL_TREE;
3852 save_cont = c_cont_label;
3853 c_cont_label = NULL_TREE;
3854 body = c_parser_c99_block_statement (parser);
3855 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3856 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3857 c_break_label = save_break;
3858 c_cont_label = save_cont;
3861 /* Parse an asm statement, a GNU extension. This is a full-blown asm
3862 statement with inputs, outputs, clobbers, and volatile tag
3863 allowed.
3865 asm-statement:
3866 asm type-qualifier[opt] ( asm-argument ) ;
3868 asm-argument:
3869 asm-string-literal
3870 asm-string-literal : asm-operands[opt]
3871 asm-string-literal : asm-operands[opt] : asm-operands[opt]
3872 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3874 Qualifiers other than volatile are accepted in the syntax but
3875 warned for. */
3877 static tree
3878 c_parser_asm_statement (c_parser *parser)
3880 tree quals, str, outputs, inputs, clobbers, ret;
3881 bool simple;
3882 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3883 c_parser_consume_token (parser);
3884 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3886 quals = c_parser_peek_token (parser)->value;
3887 c_parser_consume_token (parser);
3889 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3890 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3892 warning (0, "%E qualifier ignored on asm",
3893 c_parser_peek_token (parser)->value);
3894 quals = NULL_TREE;
3895 c_parser_consume_token (parser);
3897 else
3898 quals = NULL_TREE;
3899 /* ??? Follow the C++ parser rather than using the
3900 c_lex_string_translate kludge. */
3901 c_lex_string_translate = 0;
3902 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3904 c_lex_string_translate = 1;
3905 return NULL_TREE;
3907 str = c_parser_asm_string_literal (parser);
3908 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3910 simple = true;
3911 outputs = NULL_TREE;
3912 inputs = NULL_TREE;
3913 clobbers = NULL_TREE;
3914 goto done_asm;
3916 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3918 c_lex_string_translate = 1;
3919 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3920 return NULL_TREE;
3922 simple = false;
3923 /* Parse outputs. */
3924 if (c_parser_next_token_is (parser, CPP_COLON)
3925 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3926 outputs = NULL_TREE;
3927 else
3928 outputs = c_parser_asm_operands (parser, false);
3929 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3931 inputs = NULL_TREE;
3932 clobbers = NULL_TREE;
3933 goto done_asm;
3935 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3937 c_lex_string_translate = 1;
3938 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3939 return NULL_TREE;
3941 /* Parse inputs. */
3942 if (c_parser_next_token_is (parser, CPP_COLON)
3943 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3944 inputs = NULL_TREE;
3945 else
3946 inputs = c_parser_asm_operands (parser, true);
3947 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3949 clobbers = NULL_TREE;
3950 goto done_asm;
3952 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3954 c_lex_string_translate = 1;
3955 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3956 return NULL_TREE;
3958 /* Parse clobbers. */
3959 clobbers = c_parser_asm_clobbers (parser);
3960 done_asm:
3961 c_lex_string_translate = 1;
3962 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3965 return NULL_TREE;
3967 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3968 c_parser_skip_to_end_of_block_or_statement (parser);
3969 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
3970 clobbers, simple));
3971 return ret;
3974 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
3975 not outputs), apply the default conversion of functions and arrays
3976 to pointers.
3978 asm-operands:
3979 asm-operand
3980 asm-operands , asm-operand
3982 asm-operand:
3983 asm-string-literal ( expression )
3984 [ identifier ] asm-string-literal ( expression )
3987 static tree
3988 c_parser_asm_operands (c_parser *parser, bool convert_p)
3990 tree list = NULL_TREE;
3991 while (true)
3993 tree name, str, expr;
3994 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3996 c_parser_consume_token (parser);
3997 if (c_parser_next_token_is (parser, CPP_NAME))
3999 tree id = c_parser_peek_token (parser)->value;
4000 c_parser_consume_token (parser);
4001 name = build_string (IDENTIFIER_LENGTH (id),
4002 IDENTIFIER_POINTER (id));
4004 else
4006 c_parser_error (parser, "expected identifier");
4007 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4008 return NULL_TREE;
4010 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4011 "expected %<]%>");
4013 else
4014 name = NULL_TREE;
4015 str = c_parser_asm_string_literal (parser);
4016 if (str == NULL_TREE)
4017 return NULL_TREE;
4018 c_lex_string_translate = 1;
4019 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4021 c_lex_string_translate = 0;
4022 return NULL_TREE;
4024 expr = c_parser_expression (parser).value;
4025 if (convert_p)
4026 expr = default_function_array_conversion (expr);
4027 c_lex_string_translate = 0;
4028 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4030 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4031 return NULL_TREE;
4033 list = chainon (list, build_tree_list (build_tree_list (name, str),
4034 expr));
4035 if (c_parser_next_token_is (parser, CPP_COMMA))
4036 c_parser_consume_token (parser);
4037 else
4038 break;
4040 return list;
4043 /* Parse asm clobbers, a GNU extension.
4045 asm-clobbers:
4046 asm-string-literal
4047 asm-clobbers , asm-string-literal
4050 static tree
4051 c_parser_asm_clobbers (c_parser *parser)
4053 tree list = NULL_TREE;
4054 while (true)
4056 tree str = c_parser_asm_string_literal (parser);
4057 if (str)
4058 list = tree_cons (NULL_TREE, str, list);
4059 else
4060 return NULL_TREE;
4061 if (c_parser_next_token_is (parser, CPP_COMMA))
4062 c_parser_consume_token (parser);
4063 else
4064 break;
4066 return list;
4069 /* Parse an expression other than a compound expression; that is, an
4070 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4071 NULL then it is an Objective-C message expression which is the
4072 primary-expression starting the expression as an initializer.
4074 assignment-expression:
4075 conditional-expression
4076 unary-expression assignment-operator assignment-expression
4078 assignment-operator: one of
4079 = *= /= %= += -= <<= >>= &= ^= |=
4081 In GNU C we accept any conditional expression on the LHS and
4082 diagnose the invalid lvalue rather than producing a syntax
4083 error. */
4085 static struct c_expr
4086 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4088 struct c_expr lhs, rhs, ret;
4089 enum tree_code code;
4090 gcc_assert (!after || c_dialect_objc ());
4091 lhs = c_parser_conditional_expression (parser, after);
4092 switch (c_parser_peek_token (parser)->type)
4094 case CPP_EQ:
4095 code = NOP_EXPR;
4096 break;
4097 case CPP_MULT_EQ:
4098 code = MULT_EXPR;
4099 break;
4100 case CPP_DIV_EQ:
4101 code = TRUNC_DIV_EXPR;
4102 break;
4103 case CPP_MOD_EQ:
4104 code = TRUNC_MOD_EXPR;
4105 break;
4106 case CPP_PLUS_EQ:
4107 code = PLUS_EXPR;
4108 break;
4109 case CPP_MINUS_EQ:
4110 code = MINUS_EXPR;
4111 break;
4112 case CPP_LSHIFT_EQ:
4113 code = LSHIFT_EXPR;
4114 break;
4115 case CPP_RSHIFT_EQ:
4116 code = RSHIFT_EXPR;
4117 break;
4118 case CPP_AND_EQ:
4119 code = BIT_AND_EXPR;
4120 break;
4121 case CPP_XOR_EQ:
4122 code = BIT_XOR_EXPR;
4123 break;
4124 case CPP_OR_EQ:
4125 code = BIT_IOR_EXPR;
4126 break;
4127 default:
4128 return lhs;
4130 c_parser_consume_token (parser);
4131 rhs = c_parser_expr_no_commas (parser, NULL);
4132 rhs.value = default_function_array_conversion (rhs.value);
4133 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4134 if (code == NOP_EXPR)
4135 ret.original_code = MODIFY_EXPR;
4136 else
4138 TREE_NO_WARNING (ret.value) = 1;
4139 ret.original_code = ERROR_MARK;
4141 return ret;
4144 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4145 is not NULL then it is an Objective-C message expression which is
4146 the primary-expression starting the expression as an initializer.
4148 conditional-expression:
4149 logical-OR-expression
4150 logical-OR-expression ? expression : conditional-expression
4152 GNU extensions:
4154 conditional-expression:
4155 logical-OR-expression ? : conditional-expression
4158 static struct c_expr
4159 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4161 struct c_expr cond, exp1, exp2, ret;
4162 gcc_assert (!after || c_dialect_objc ());
4163 cond = c_parser_binary_expression (parser, after);
4164 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4165 return cond;
4166 cond.value = default_function_array_conversion (cond.value);
4167 c_parser_consume_token (parser);
4168 if (c_parser_next_token_is (parser, CPP_COLON))
4170 if (pedantic)
4171 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4172 /* Make sure first operand is calculated only once. */
4173 exp1.value = save_expr (default_conversion (cond.value));
4174 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4175 skip_evaluation += cond.value == truthvalue_true_node;
4177 else
4179 cond.value
4180 = c_objc_common_truthvalue_conversion
4181 (default_conversion (cond.value));
4182 skip_evaluation += cond.value == truthvalue_false_node;
4183 exp1 = c_parser_expression_conv (parser);
4184 skip_evaluation += ((cond.value == truthvalue_true_node)
4185 - (cond.value == truthvalue_false_node));
4187 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4189 skip_evaluation -= cond.value == truthvalue_true_node;
4190 ret.value = error_mark_node;
4191 ret.original_code = ERROR_MARK;
4192 return ret;
4194 exp2 = c_parser_conditional_expression (parser, NULL);
4195 exp2.value = default_function_array_conversion (exp2.value);
4196 skip_evaluation -= cond.value == truthvalue_true_node;
4197 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4198 ret.original_code = ERROR_MARK;
4199 return ret;
4202 /* Parse a binary expression; that is, a logical-OR-expression (C90
4203 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4204 an Objective-C message expression which is the primary-expression
4205 starting the expression as an initializer.
4207 multiplicative-expression:
4208 cast-expression
4209 multiplicative-expression * cast-expression
4210 multiplicative-expression / cast-expression
4211 multiplicative-expression % cast-expression
4213 additive-expression:
4214 multiplicative-expression
4215 additive-expression + multiplicative-expression
4216 additive-expression - multiplicative-expression
4218 shift-expression:
4219 additive-expression
4220 shift-expression << additive-expression
4221 shift-expression >> additive-expression
4223 relational-expression:
4224 shift-expression
4225 relational-expression < shift-expression
4226 relational-expression > shift-expression
4227 relational-expression <= shift-expression
4228 relational-expression >= shift-expression
4230 equality-expression:
4231 relational-expression
4232 equality-expression == relational-expression
4233 equality-expression != relational-expression
4235 AND-expression:
4236 equality-expression
4237 AND-expression & equality-expression
4239 exclusive-OR-expression:
4240 AND-expression
4241 exclusive-OR-expression ^ AND-expression
4243 inclusive-OR-expression:
4244 exclusive-OR-expression
4245 inclusive-OR-expression | exclusive-OR-expression
4247 logical-AND-expression:
4248 inclusive-OR-expression
4249 logical-AND-expression && inclusive-OR-expression
4251 logical-OR-expression:
4252 logical-AND-expression
4253 logical-OR-expression || logical-AND-expression
4256 static struct c_expr
4257 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4259 /* A binary expression is parsed using operator-precedence parsing,
4260 with the operands being cast expressions. All the binary
4261 operators are left-associative. Thus a binary expression is of
4262 form:
4264 E0 op1 E1 op2 E2 ...
4266 which we represent on a stack. On the stack, the precedence
4267 levels are strictly increasing. When a new operator is
4268 encountered of higher precedence than that at the top of the
4269 stack, it is pushed; its LHS is the top expression, and its RHS
4270 is everything parsed until it is popped. When a new operator is
4271 encountered with precedence less than or equal to that at the top
4272 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4273 by the result of the operation until the operator at the top of
4274 the stack has lower precedence than the new operator or there is
4275 only one element on the stack; then the top expression is the LHS
4276 of the new operator. In the case of logical AND and OR
4277 expressions, we also need to adjust skip_evaluation as
4278 appropriate when the operators are pushed and popped. */
4280 /* The precedence levels, where 0 is a dummy lowest level used for
4281 the bottom of the stack. */
4282 enum prec {
4283 PREC_NONE,
4284 PREC_LOGOR,
4285 PREC_LOGAND,
4286 PREC_BITOR,
4287 PREC_BITXOR,
4288 PREC_BITAND,
4289 PREC_EQ,
4290 PREC_REL,
4291 PREC_SHIFT,
4292 PREC_ADD,
4293 PREC_MULT,
4294 NUM_PRECS
4296 struct {
4297 /* The expression at this stack level. */
4298 struct c_expr expr;
4299 /* The precedence of the operator on its left, PREC_NONE at the
4300 bottom of the stack. */
4301 enum prec prec;
4302 /* The operation on its left. */
4303 enum tree_code op;
4304 } stack[NUM_PRECS];
4305 int sp;
4306 #define POP \
4307 do { \
4308 switch (stack[sp].op) \
4310 case TRUTH_ANDIF_EXPR: \
4311 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4312 break; \
4313 case TRUTH_ORIF_EXPR: \
4314 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4315 break; \
4316 default: \
4317 break; \
4319 stack[sp - 1].expr.value \
4320 = default_function_array_conversion (stack[sp - 1].expr.value); \
4321 stack[sp].expr.value \
4322 = default_function_array_conversion (stack[sp].expr.value); \
4323 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4324 stack[sp - 1].expr, \
4325 stack[sp].expr); \
4326 sp--; \
4327 } while (0)
4328 gcc_assert (!after || c_dialect_objc ());
4329 stack[0].expr = c_parser_cast_expression (parser, after);
4330 stack[0].prec = PREC_NONE;
4331 sp = 0;
4332 while (true)
4334 enum prec oprec;
4335 enum tree_code ocode;
4336 if (parser->error)
4337 goto out;
4338 switch (c_parser_peek_token (parser)->type)
4340 case CPP_MULT:
4341 oprec = PREC_MULT;
4342 ocode = MULT_EXPR;
4343 break;
4344 case CPP_DIV:
4345 oprec = PREC_MULT;
4346 ocode = TRUNC_DIV_EXPR;
4347 break;
4348 case CPP_MOD:
4349 oprec = PREC_MULT;
4350 ocode = TRUNC_MOD_EXPR;
4351 break;
4352 case CPP_PLUS:
4353 oprec = PREC_ADD;
4354 ocode = PLUS_EXPR;
4355 break;
4356 case CPP_MINUS:
4357 oprec = PREC_ADD;
4358 ocode = MINUS_EXPR;
4359 break;
4360 case CPP_LSHIFT:
4361 oprec = PREC_SHIFT;
4362 ocode = LSHIFT_EXPR;
4363 break;
4364 case CPP_RSHIFT:
4365 oprec = PREC_SHIFT;
4366 ocode = RSHIFT_EXPR;
4367 break;
4368 case CPP_LESS:
4369 oprec = PREC_REL;
4370 ocode = LT_EXPR;
4371 break;
4372 case CPP_GREATER:
4373 oprec = PREC_REL;
4374 ocode = GT_EXPR;
4375 break;
4376 case CPP_LESS_EQ:
4377 oprec = PREC_REL;
4378 ocode = LE_EXPR;
4379 break;
4380 case CPP_GREATER_EQ:
4381 oprec = PREC_REL;
4382 ocode = GE_EXPR;
4383 break;
4384 case CPP_EQ_EQ:
4385 oprec = PREC_EQ;
4386 ocode = EQ_EXPR;
4387 break;
4388 case CPP_NOT_EQ:
4389 oprec = PREC_EQ;
4390 ocode = NE_EXPR;
4391 break;
4392 case CPP_AND:
4393 oprec = PREC_BITAND;
4394 ocode = BIT_AND_EXPR;
4395 break;
4396 case CPP_XOR:
4397 oprec = PREC_BITXOR;
4398 ocode = BIT_XOR_EXPR;
4399 break;
4400 case CPP_OR:
4401 oprec = PREC_BITOR;
4402 ocode = BIT_IOR_EXPR;
4403 break;
4404 case CPP_AND_AND:
4405 oprec = PREC_LOGAND;
4406 ocode = TRUTH_ANDIF_EXPR;
4407 break;
4408 case CPP_OR_OR:
4409 oprec = PREC_LOGOR;
4410 ocode = TRUTH_ORIF_EXPR;
4411 break;
4412 default:
4413 /* Not a binary operator, so end of the binary
4414 expression. */
4415 goto out;
4417 c_parser_consume_token (parser);
4418 while (oprec <= stack[sp].prec)
4419 POP;
4420 switch (ocode)
4422 case TRUTH_ANDIF_EXPR:
4423 stack[sp].expr.value
4424 = default_function_array_conversion (stack[sp].expr.value);
4425 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4426 (default_conversion (stack[sp].expr.value));
4427 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4428 break;
4429 case TRUTH_ORIF_EXPR:
4430 stack[sp].expr.value
4431 = default_function_array_conversion (stack[sp].expr.value);
4432 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4433 (default_conversion (stack[sp].expr.value));
4434 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4435 break;
4436 default:
4437 break;
4439 sp++;
4440 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4441 stack[sp].prec = oprec;
4442 stack[sp].op = ocode;
4444 out:
4445 while (sp > 0)
4446 POP;
4447 return stack[0].expr;
4448 #undef POP
4451 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4452 NULL then it is an Objective-C message expression which is the
4453 primary-expression starting the expression as an initializer.
4455 cast-expression:
4456 unary-expression
4457 ( type-name ) unary-expression
4460 static struct c_expr
4461 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4463 gcc_assert (!after || c_dialect_objc ());
4464 if (after)
4465 return c_parser_postfix_expression_after_primary (parser, *after);
4466 /* If the expression begins with a parenthesized type name, it may
4467 be either a cast or a compound literal; we need to see whether
4468 the next character is '{' to tell the difference. If not, it is
4469 an unary expression. */
4470 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4471 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4473 struct c_type_name *type_name;
4474 struct c_expr ret;
4475 tree expr;
4476 c_parser_consume_token (parser);
4477 type_name = c_parser_type_name (parser);
4478 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4479 if (type_name == NULL)
4481 ret.value = error_mark_node;
4482 ret.original_code = ERROR_MARK;
4483 return ret;
4485 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4486 return c_parser_postfix_expression_after_paren_type (parser,
4487 type_name);
4488 expr = c_parser_cast_expression (parser, NULL).value;
4489 expr = default_function_array_conversion (expr);
4490 ret.value = c_cast_expr (type_name, expr);
4491 ret.original_code = ERROR_MARK;
4492 return ret;
4494 else
4495 return c_parser_unary_expression (parser);
4498 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4500 unary-expression:
4501 postfix-expression
4502 ++ unary-expression
4503 -- unary-expression
4504 unary-operator cast-expression
4505 sizeof unary-expression
4506 sizeof ( type-name )
4508 unary-operator: one of
4509 & * + - ~ !
4511 GNU extensions:
4513 unary-expression:
4514 __alignof__ unary-expression
4515 __alignof__ ( type-name )
4516 && identifier
4518 unary-operator: one of
4519 __extension__ __real__ __imag__
4521 In addition, the GNU syntax treats ++ and -- as unary operators, so
4522 they may be applied to cast expressions with errors for non-lvalues
4523 given later. */
4525 static struct c_expr
4526 c_parser_unary_expression (c_parser *parser)
4528 int ext;
4529 struct c_expr ret, op;
4530 switch (c_parser_peek_token (parser)->type)
4532 case CPP_PLUS_PLUS:
4533 c_parser_consume_token (parser);
4534 op = c_parser_cast_expression (parser, NULL);
4535 op.value = default_function_array_conversion (op.value);
4536 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4537 case CPP_MINUS_MINUS:
4538 c_parser_consume_token (parser);
4539 op = c_parser_cast_expression (parser, NULL);
4540 op.value = default_function_array_conversion (op.value);
4541 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4542 case CPP_AND:
4543 c_parser_consume_token (parser);
4544 return parser_build_unary_op (ADDR_EXPR,
4545 c_parser_cast_expression (parser, NULL));
4546 case CPP_MULT:
4547 c_parser_consume_token (parser);
4548 op = c_parser_cast_expression (parser, NULL);
4549 op.value = default_function_array_conversion (op.value);
4550 ret.value = build_indirect_ref (op.value, "unary *");
4551 ret.original_code = ERROR_MARK;
4552 return ret;
4553 case CPP_PLUS:
4554 c_parser_consume_token (parser);
4555 if (!c_dialect_objc () && !in_system_header)
4556 warning (OPT_Wtraditional,
4557 "traditional C rejects the unary plus operator");
4558 op = c_parser_cast_expression (parser, NULL);
4559 op.value = default_function_array_conversion (op.value);
4560 return parser_build_unary_op (CONVERT_EXPR, op);
4561 case CPP_MINUS:
4562 c_parser_consume_token (parser);
4563 op = c_parser_cast_expression (parser, NULL);
4564 op.value = default_function_array_conversion (op.value);
4565 return parser_build_unary_op (NEGATE_EXPR, op);
4566 case CPP_COMPL:
4567 c_parser_consume_token (parser);
4568 op = c_parser_cast_expression (parser, NULL);
4569 op.value = default_function_array_conversion (op.value);
4570 return parser_build_unary_op (BIT_NOT_EXPR, op);
4571 case CPP_NOT:
4572 c_parser_consume_token (parser);
4573 op = c_parser_cast_expression (parser, NULL);
4574 op.value = default_function_array_conversion (op.value);
4575 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4576 case CPP_AND_AND:
4577 /* Refer to the address of a label as a pointer. */
4578 c_parser_consume_token (parser);
4579 if (c_parser_next_token_is (parser, CPP_NAME))
4581 ret.value = finish_label_address_expr
4582 (c_parser_peek_token (parser)->value);
4583 c_parser_consume_token (parser);
4585 else
4587 c_parser_error (parser, "expected identifier");
4588 ret.value = error_mark_node;
4590 ret.original_code = ERROR_MARK;
4591 return ret;
4592 case CPP_KEYWORD:
4593 switch (c_parser_peek_token (parser)->keyword)
4595 case RID_SIZEOF:
4596 return c_parser_sizeof_expression (parser);
4597 case RID_ALIGNOF:
4598 return c_parser_alignof_expression (parser);
4599 case RID_EXTENSION:
4600 c_parser_consume_token (parser);
4601 ext = disable_extension_diagnostics ();
4602 ret = c_parser_cast_expression (parser, NULL);
4603 restore_extension_diagnostics (ext);
4604 return ret;
4605 case RID_REALPART:
4606 c_parser_consume_token (parser);
4607 op = c_parser_cast_expression (parser, NULL);
4608 op.value = default_function_array_conversion (op.value);
4609 return parser_build_unary_op (REALPART_EXPR, op);
4610 case RID_IMAGPART:
4611 c_parser_consume_token (parser);
4612 op = c_parser_cast_expression (parser, NULL);
4613 op.value = default_function_array_conversion (op.value);
4614 return parser_build_unary_op (IMAGPART_EXPR, op);
4615 default:
4616 return c_parser_postfix_expression (parser);
4618 default:
4619 return c_parser_postfix_expression (parser);
4623 /* Parse a sizeof expression. */
4625 static struct c_expr
4626 c_parser_sizeof_expression (c_parser *parser)
4628 struct c_expr expr;
4629 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4630 c_parser_consume_token (parser);
4631 skip_evaluation++;
4632 in_sizeof++;
4633 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4634 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4636 /* Either sizeof ( type-name ) or sizeof unary-expression
4637 starting with a compound literal. */
4638 struct c_type_name *type_name;
4639 c_parser_consume_token (parser);
4640 type_name = c_parser_type_name (parser);
4641 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4642 if (type_name == NULL)
4644 struct c_expr ret;
4645 skip_evaluation--;
4646 in_sizeof--;
4647 ret.value = error_mark_node;
4648 ret.original_code = ERROR_MARK;
4649 return ret;
4651 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4653 expr = c_parser_postfix_expression_after_paren_type (parser,
4654 type_name);
4655 goto sizeof_expr;
4657 /* sizeof ( type-name ). */
4658 skip_evaluation--;
4659 in_sizeof--;
4660 return c_expr_sizeof_type (type_name);
4662 else
4664 expr = c_parser_unary_expression (parser);
4665 sizeof_expr:
4666 skip_evaluation--;
4667 in_sizeof--;
4668 if (TREE_CODE (expr.value) == COMPONENT_REF
4669 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4670 error ("%<sizeof%> applied to a bit-field");
4671 return c_expr_sizeof_expr (expr);
4675 /* Parse an alignof expression. */
4677 static struct c_expr
4678 c_parser_alignof_expression (c_parser *parser)
4680 struct c_expr expr;
4681 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4682 c_parser_consume_token (parser);
4683 skip_evaluation++;
4684 in_alignof++;
4685 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4686 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4688 /* Either __alignof__ ( type-name ) or __alignof__
4689 unary-expression starting with a compound literal. */
4690 struct c_type_name *type_name;
4691 struct c_expr ret;
4692 c_parser_consume_token (parser);
4693 type_name = c_parser_type_name (parser);
4694 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4695 if (type_name == NULL)
4697 struct c_expr ret;
4698 skip_evaluation--;
4699 in_alignof--;
4700 ret.value = error_mark_node;
4701 ret.original_code = ERROR_MARK;
4702 return ret;
4704 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4706 expr = c_parser_postfix_expression_after_paren_type (parser,
4707 type_name);
4708 goto alignof_expr;
4710 /* alignof ( type-name ). */
4711 skip_evaluation--;
4712 in_alignof--;
4713 ret.value = c_alignof (groktypename (type_name));
4714 ret.original_code = ERROR_MARK;
4715 return ret;
4717 else
4719 struct c_expr ret;
4720 expr = c_parser_unary_expression (parser);
4721 alignof_expr:
4722 skip_evaluation--;
4723 in_alignof--;
4724 ret.value = c_alignof_expr (expr.value);
4725 ret.original_code = ERROR_MARK;
4726 return ret;
4730 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4732 postfix-expression:
4733 primary-expression
4734 postfix-expression [ expression ]
4735 postfix-expression ( argument-expression-list[opt] )
4736 postfix-expression . identifier
4737 postfix-expression -> identifier
4738 postfix-expression ++
4739 postfix-expression --
4740 ( type-name ) { initializer-list }
4741 ( type-name ) { initializer-list , }
4743 argument-expression-list:
4744 argument-expression
4745 argument-expression-list , argument-expression
4747 primary-expression:
4748 identifier
4749 constant
4750 string-literal
4751 ( expression )
4753 GNU extensions:
4755 primary-expression:
4756 __func__
4757 (treated as a keyword in GNU C)
4758 __FUNCTION__
4759 __PRETTY_FUNCTION__
4760 ( compound-statement )
4761 __builtin_va_arg ( assignment-expression , type-name )
4762 __builtin_offsetof ( type-name , offsetof-member-designator )
4763 __builtin_choose_expr ( assignment-expression ,
4764 assignment-expression ,
4765 assignment-expression )
4766 __builtin_types_compatible_p ( type-name , type-name )
4768 offsetof-member-designator:
4769 identifier
4770 offsetof-member-designator . identifier
4771 offsetof-member-designator [ expression ]
4773 Objective-C:
4775 primary-expression:
4776 [ objc-receiver objc-message-args ]
4777 @selector ( objc-selector-arg )
4778 @protocol ( identifier )
4779 @encode ( type-name )
4780 objc-string-literal
4783 static struct c_expr
4784 c_parser_postfix_expression (c_parser *parser)
4786 struct c_expr expr, e1, e2, e3;
4787 struct c_type_name *t1, *t2;
4788 switch (c_parser_peek_token (parser)->type)
4790 case CPP_NUMBER:
4791 case CPP_CHAR:
4792 case CPP_WCHAR:
4793 expr.value = c_parser_peek_token (parser)->value;
4794 expr.original_code = ERROR_MARK;
4795 c_parser_consume_token (parser);
4796 break;
4797 case CPP_STRING:
4798 case CPP_WSTRING:
4799 expr.value = c_parser_peek_token (parser)->value;
4800 expr.original_code = STRING_CST;
4801 c_parser_consume_token (parser);
4802 break;
4803 case CPP_OBJC_STRING:
4804 gcc_assert (c_dialect_objc ());
4805 expr.value
4806 = objc_build_string_object (c_parser_peek_token (parser)->value);
4807 expr.original_code = ERROR_MARK;
4808 c_parser_consume_token (parser);
4809 break;
4810 case CPP_NAME:
4811 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4813 c_parser_error (parser, "expected expression");
4814 expr.value = error_mark_node;
4815 expr.original_code = ERROR_MARK;
4816 break;
4819 tree id = c_parser_peek_token (parser)->value;
4820 location_t loc = c_parser_peek_token (parser)->location;
4821 c_parser_consume_token (parser);
4822 expr.value = build_external_ref (id,
4823 (c_parser_peek_token (parser)->type
4824 == CPP_OPEN_PAREN), loc);
4825 expr.original_code = ERROR_MARK;
4827 break;
4828 case CPP_OPEN_PAREN:
4829 /* A parenthesized expression, statement expression or compound
4830 literal. */
4831 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4833 /* A statement expression. */
4834 tree stmt;
4835 c_parser_consume_token (parser);
4836 c_parser_consume_token (parser);
4837 if (cur_stmt_list == NULL)
4839 error ("braced-group within expression allowed "
4840 "only inside a function");
4841 parser->error = true;
4842 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4843 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4844 expr.value = error_mark_node;
4845 expr.original_code = ERROR_MARK;
4846 break;
4848 stmt = c_begin_stmt_expr ();
4849 c_parser_compound_statement_nostart (parser);
4850 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4851 "expected %<)%>");
4852 if (pedantic)
4853 pedwarn ("ISO C forbids braced-groups within expressions");
4854 expr.value = c_finish_stmt_expr (stmt);
4855 expr.original_code = ERROR_MARK;
4857 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4859 /* A compound literal. ??? Can we actually get here rather
4860 than going directly to
4861 c_parser_postfix_expression_after_paren_type from
4862 elsewhere? */
4863 struct c_type_name *type_name;
4864 c_parser_consume_token (parser);
4865 type_name = c_parser_type_name (parser);
4866 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4867 "expected %<)%>");
4868 if (type_name == NULL)
4870 expr.value = error_mark_node;
4871 expr.original_code = ERROR_MARK;
4873 else
4874 expr = c_parser_postfix_expression_after_paren_type (parser,
4875 type_name);
4877 else
4879 /* A parenthesized expression. */
4880 c_parser_consume_token (parser);
4881 expr = c_parser_expression (parser);
4882 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4883 TREE_NO_WARNING (expr.value) = 1;
4884 expr.original_code = ERROR_MARK;
4885 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4886 "expected %<)%>");
4888 break;
4889 case CPP_KEYWORD:
4890 switch (c_parser_peek_token (parser)->keyword)
4892 case RID_FUNCTION_NAME:
4893 case RID_PRETTY_FUNCTION_NAME:
4894 case RID_C99_FUNCTION_NAME:
4895 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4896 c_parser_peek_token (parser)->value);
4897 expr.original_code = ERROR_MARK;
4898 c_parser_consume_token (parser);
4899 break;
4900 case RID_VA_ARG:
4901 c_parser_consume_token (parser);
4902 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4904 expr.value = error_mark_node;
4905 expr.original_code = ERROR_MARK;
4906 break;
4908 e1 = c_parser_expr_no_commas (parser, NULL);
4909 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4912 expr.value = error_mark_node;
4913 expr.original_code = ERROR_MARK;
4914 break;
4916 t1 = c_parser_type_name (parser);
4917 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4918 "expected %<)%>");
4919 if (t1 == NULL)
4921 expr.value = error_mark_node;
4922 expr.original_code = ERROR_MARK;
4924 else
4926 expr.value = build_va_arg (e1.value, groktypename (t1));
4927 expr.original_code = ERROR_MARK;
4929 break;
4930 case RID_OFFSETOF:
4931 c_parser_consume_token (parser);
4932 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4934 expr.value = error_mark_node;
4935 expr.original_code = ERROR_MARK;
4936 break;
4938 t1 = c_parser_type_name (parser);
4939 if (t1 == NULL)
4941 expr.value = error_mark_node;
4942 expr.original_code = ERROR_MARK;
4943 break;
4945 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4948 expr.value = error_mark_node;
4949 expr.original_code = ERROR_MARK;
4950 break;
4953 tree type = groktypename (t1);
4954 tree offsetof_ref;
4955 if (type == error_mark_node)
4956 offsetof_ref = error_mark_node;
4957 else
4958 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
4959 /* Parse the second argument to __builtin_offsetof. We
4960 must have one identifier, and beyond that we want to
4961 accept sub structure and sub array references. */
4962 if (c_parser_next_token_is (parser, CPP_NAME))
4964 offsetof_ref = build_component_ref
4965 (offsetof_ref, c_parser_peek_token (parser)->value);
4966 c_parser_consume_token (parser);
4967 while (c_parser_next_token_is (parser, CPP_DOT)
4968 || c_parser_next_token_is (parser,
4969 CPP_OPEN_SQUARE))
4971 if (c_parser_next_token_is (parser, CPP_DOT))
4973 c_parser_consume_token (parser);
4974 if (c_parser_next_token_is_not (parser,
4975 CPP_NAME))
4977 c_parser_error (parser, "expected identifier");
4978 break;
4980 offsetof_ref = build_component_ref
4981 (offsetof_ref,
4982 c_parser_peek_token (parser)->value);
4983 c_parser_consume_token (parser);
4985 else
4987 tree idx;
4988 c_parser_consume_token (parser);
4989 idx = c_parser_expression (parser).value;
4990 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4991 "expected %<]%>");
4992 offsetof_ref = build_array_ref (offsetof_ref, idx);
4996 else
4997 c_parser_error (parser, "expected identifier");
4998 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4999 "expected %<)%>");
5000 expr.value = fold_offsetof (offsetof_ref);
5001 expr.original_code = ERROR_MARK;
5003 break;
5004 case RID_CHOOSE_EXPR:
5005 c_parser_consume_token (parser);
5006 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5008 expr.value = error_mark_node;
5009 expr.original_code = ERROR_MARK;
5010 break;
5012 e1 = c_parser_expr_no_commas (parser, NULL);
5013 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5015 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5016 expr.value = error_mark_node;
5017 expr.original_code = ERROR_MARK;
5018 break;
5020 e2 = c_parser_expr_no_commas (parser, NULL);
5021 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5023 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5024 expr.value = error_mark_node;
5025 expr.original_code = ERROR_MARK;
5026 break;
5028 e3 = c_parser_expr_no_commas (parser, NULL);
5029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5030 "expected %<)%>");
5032 tree c;
5034 c = fold (e1.value);
5035 if (TREE_CODE (c) != INTEGER_CST)
5036 error ("first argument to %<__builtin_choose_expr%> not"
5037 " a constant");
5038 expr = integer_zerop (c) ? e3 : e2;
5040 break;
5041 case RID_TYPES_COMPATIBLE_P:
5042 c_parser_consume_token (parser);
5043 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5045 expr.value = error_mark_node;
5046 expr.original_code = ERROR_MARK;
5047 break;
5049 t1 = c_parser_type_name (parser);
5050 if (t1 == NULL)
5052 expr.value = error_mark_node;
5053 expr.original_code = ERROR_MARK;
5054 break;
5056 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5059 expr.value = error_mark_node;
5060 expr.original_code = ERROR_MARK;
5061 break;
5063 t2 = c_parser_type_name (parser);
5064 if (t2 == NULL)
5066 expr.value = error_mark_node;
5067 expr.original_code = ERROR_MARK;
5068 break;
5070 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5071 "expected %<)%>");
5073 tree e1, e2;
5075 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5076 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5078 expr.value = comptypes (e1, e2)
5079 ? build_int_cst (NULL_TREE, 1)
5080 : build_int_cst (NULL_TREE, 0);
5081 expr.original_code = ERROR_MARK;
5083 break;
5084 case RID_AT_SELECTOR:
5085 gcc_assert (c_dialect_objc ());
5086 c_parser_consume_token (parser);
5087 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5089 expr.value = error_mark_node;
5090 expr.original_code = ERROR_MARK;
5091 break;
5094 tree sel = c_parser_objc_selector_arg (parser);
5095 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5096 "expected %<)%>");
5097 expr.value = objc_build_selector_expr (sel);
5098 expr.original_code = ERROR_MARK;
5100 break;
5101 case RID_AT_PROTOCOL:
5102 gcc_assert (c_dialect_objc ());
5103 c_parser_consume_token (parser);
5104 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5106 expr.value = error_mark_node;
5107 expr.original_code = ERROR_MARK;
5108 break;
5110 if (c_parser_next_token_is_not (parser, CPP_NAME))
5112 c_parser_error (parser, "expected identifier");
5113 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5114 expr.value = error_mark_node;
5115 expr.original_code = ERROR_MARK;
5116 break;
5119 tree id = c_parser_peek_token (parser)->value;
5120 c_parser_consume_token (parser);
5121 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5122 "expected %<)%>");
5123 expr.value = objc_build_protocol_expr (id);
5124 expr.original_code = ERROR_MARK;
5126 break;
5127 case RID_AT_ENCODE:
5128 /* Extension to support C-structures in the archiver. */
5129 gcc_assert (c_dialect_objc ());
5130 c_parser_consume_token (parser);
5131 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5133 expr.value = error_mark_node;
5134 expr.original_code = ERROR_MARK;
5135 break;
5137 t1 = c_parser_type_name (parser);
5138 if (t1 == NULL)
5140 expr.value = error_mark_node;
5141 expr.original_code = ERROR_MARK;
5142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5143 break;
5145 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5146 "expected %<)%>");
5148 tree type = groktypename (t1);
5149 expr.value = objc_build_encode_expr (type);
5150 expr.original_code = ERROR_MARK;
5152 break;
5153 default:
5154 c_parser_error (parser, "expected expression");
5155 expr.value = error_mark_node;
5156 expr.original_code = ERROR_MARK;
5157 break;
5159 break;
5160 case CPP_OPEN_SQUARE:
5161 if (c_dialect_objc ())
5163 tree receiver, args;
5164 c_parser_consume_token (parser);
5165 receiver = c_parser_objc_receiver (parser);
5166 args = c_parser_objc_message_args (parser);
5167 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5168 "expected %<]%>");
5169 expr.value = objc_build_message_expr (build_tree_list (receiver,
5170 args));
5171 expr.original_code = ERROR_MARK;
5172 break;
5174 /* Else fall through to report error. */
5175 default:
5176 c_parser_error (parser, "expected expression");
5177 expr.value = error_mark_node;
5178 expr.original_code = ERROR_MARK;
5179 break;
5181 return c_parser_postfix_expression_after_primary (parser, expr);
5184 /* Parse a postfix expression after a parenthesized type name: the
5185 brace-enclosed initializer of a compound literal, possibly followed
5186 by some postfix operators. This is separate because it is not
5187 possible to tell until after the type name whether a cast
5188 expression has a cast or a compound literal, or whether the operand
5189 of sizeof is a parenthesized type name or starts with a compound
5190 literal. */
5192 static struct c_expr
5193 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5194 struct c_type_name *type_name)
5196 tree type;
5197 struct c_expr init;
5198 struct c_expr expr;
5199 start_init (NULL_TREE, NULL, 0);
5200 type = groktypename (type_name);
5201 if (C_TYPE_VARIABLE_SIZE (type))
5203 error ("compound literal has variable size");
5204 type = error_mark_node;
5206 init = c_parser_braced_init (parser, type, false);
5207 finish_init ();
5208 maybe_warn_string_init (type, init);
5210 if (pedantic && !flag_isoc99)
5211 pedwarn ("ISO C90 forbids compound literals");
5212 expr.value = build_compound_literal (type, init.value);
5213 expr.original_code = ERROR_MARK;
5214 return c_parser_postfix_expression_after_primary (parser, expr);
5217 /* Parse a postfix expression after the initial primary or compound
5218 literal; that is, parse a series of postfix operators. */
5220 static struct c_expr
5221 c_parser_postfix_expression_after_primary (c_parser *parser,
5222 struct c_expr expr)
5224 tree ident, idx, exprlist;
5225 while (true)
5227 switch (c_parser_peek_token (parser)->type)
5229 case CPP_OPEN_SQUARE:
5230 /* Array reference. */
5231 c_parser_consume_token (parser);
5232 idx = c_parser_expression (parser).value;
5233 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5234 "expected %<]%>");
5235 expr.value = build_array_ref (expr.value, idx);
5236 expr.original_code = ERROR_MARK;
5237 break;
5238 case CPP_OPEN_PAREN:
5239 /* Function call. */
5240 c_parser_consume_token (parser);
5241 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5242 exprlist = NULL_TREE;
5243 else
5244 exprlist = c_parser_expr_list (parser, true);
5245 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5246 "expected %<)%>");
5247 expr.value = build_function_call (expr.value, exprlist);
5248 expr.original_code = ERROR_MARK;
5249 break;
5250 case CPP_DOT:
5251 /* Structure element reference. */
5252 c_parser_consume_token (parser);
5253 expr.value = default_function_array_conversion (expr.value);
5254 if (c_parser_next_token_is (parser, CPP_NAME))
5255 ident = c_parser_peek_token (parser)->value;
5256 else
5258 c_parser_error (parser, "expected identifier");
5259 expr.value = error_mark_node;
5260 expr.original_code = ERROR_MARK;
5261 return expr;
5263 c_parser_consume_token (parser);
5264 expr.value = build_component_ref (expr.value, ident);
5265 expr.original_code = ERROR_MARK;
5266 break;
5267 case CPP_DEREF:
5268 /* Structure element reference. */
5269 c_parser_consume_token (parser);
5270 expr.value = default_function_array_conversion (expr.value);
5271 if (c_parser_next_token_is (parser, CPP_NAME))
5272 ident = c_parser_peek_token (parser)->value;
5273 else
5275 c_parser_error (parser, "expected identifier");
5276 expr.value = error_mark_node;
5277 expr.original_code = ERROR_MARK;
5278 return expr;
5280 c_parser_consume_token (parser);
5281 expr.value = build_component_ref (build_indirect_ref (expr.value,
5282 "->"), ident);
5283 expr.original_code = ERROR_MARK;
5284 break;
5285 case CPP_PLUS_PLUS:
5286 /* Postincrement. */
5287 c_parser_consume_token (parser);
5288 expr.value = default_function_array_conversion (expr.value);
5289 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5290 expr.original_code = ERROR_MARK;
5291 break;
5292 case CPP_MINUS_MINUS:
5293 /* Postdecrement. */
5294 c_parser_consume_token (parser);
5295 expr.value = default_function_array_conversion (expr.value);
5296 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5297 expr.original_code = ERROR_MARK;
5298 break;
5299 default:
5300 return expr;
5305 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5307 expression:
5308 assignment-expression
5309 expression , assignment-expression
5312 static struct c_expr
5313 c_parser_expression (c_parser *parser)
5315 struct c_expr expr;
5316 expr = c_parser_expr_no_commas (parser, NULL);
5317 while (c_parser_next_token_is (parser, CPP_COMMA))
5319 struct c_expr next;
5320 c_parser_consume_token (parser);
5321 next = c_parser_expr_no_commas (parser, NULL);
5322 next.value = default_function_array_conversion (next.value);
5323 expr.value = build_compound_expr (expr.value, next.value);
5324 expr.original_code = COMPOUND_EXPR;
5326 return expr;
5329 /* Parse an expression and convert functions or arrays to
5330 pointers. */
5332 static struct c_expr
5333 c_parser_expression_conv (c_parser *parser)
5335 struct c_expr expr;
5336 expr = c_parser_expression (parser);
5337 expr.value = default_function_array_conversion (expr.value);
5338 return expr;
5341 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5342 functions and arrays to pointers.
5344 nonempty-expr-list:
5345 assignment-expression
5346 nonempty-expr-list , assignment-expression
5349 static tree
5350 c_parser_expr_list (c_parser *parser, bool convert_p)
5352 struct c_expr expr;
5353 tree ret, cur;
5354 expr = c_parser_expr_no_commas (parser, NULL);
5355 if (convert_p)
5356 expr.value = default_function_array_conversion (expr.value);
5357 ret = cur = build_tree_list (NULL_TREE, expr.value);
5358 while (c_parser_next_token_is (parser, CPP_COMMA))
5360 c_parser_consume_token (parser);
5361 expr = c_parser_expr_no_commas (parser, NULL);
5362 if (convert_p)
5363 expr.value = default_function_array_conversion (expr.value);
5364 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5366 return ret;
5370 /* Parse Objective-C-specific constructs. */
5372 /* Parse an objc-class-definition.
5374 objc-class-definition:
5375 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5376 objc-class-instance-variables[opt] objc-methodprotolist @end
5377 @implementation identifier objc-superclass[opt]
5378 objc-class-instance-variables[opt]
5379 @interface identifier ( identifier ) objc-protocol-refs[opt]
5380 objc-methodprotolist @end
5381 @implementation identifier ( identifier )
5383 objc-superclass:
5384 : identifier
5386 "@interface identifier (" must start "@interface identifier (
5387 identifier ) ...": objc-methodprotolist in the first production may
5388 not start with a parenthesized identifier as a declarator of a data
5389 definition with no declaration specifiers if the objc-superclass,
5390 objc-protocol-refs and objc-class-instance-variables are omitted. */
5392 static void
5393 c_parser_objc_class_definition (c_parser *parser)
5395 bool iface_p;
5396 tree id1;
5397 tree superclass;
5398 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5399 iface_p = true;
5400 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5401 iface_p = false;
5402 else
5403 gcc_unreachable ();
5404 c_parser_consume_token (parser);
5405 if (c_parser_next_token_is_not (parser, CPP_NAME))
5407 c_parser_error (parser, "expected identifier");
5408 return;
5410 id1 = c_parser_peek_token (parser)->value;
5411 c_parser_consume_token (parser);
5412 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5414 tree id2;
5415 tree proto = NULL_TREE;
5416 c_parser_consume_token (parser);
5417 if (c_parser_next_token_is_not (parser, CPP_NAME))
5419 c_parser_error (parser, "expected identifier");
5420 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5421 return;
5423 id2 = c_parser_peek_token (parser)->value;
5424 c_parser_consume_token (parser);
5425 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5426 if (!iface_p)
5428 objc_start_category_implementation (id1, id2);
5429 return;
5431 if (c_parser_next_token_is (parser, CPP_LESS))
5432 proto = c_parser_objc_protocol_refs (parser);
5433 objc_start_category_interface (id1, id2, proto);
5434 c_parser_objc_methodprotolist (parser);
5435 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5436 objc_finish_interface ();
5437 return;
5439 if (c_parser_next_token_is (parser, CPP_COLON))
5441 c_parser_consume_token (parser);
5442 if (c_parser_next_token_is_not (parser, CPP_NAME))
5444 c_parser_error (parser, "expected identifier");
5445 return;
5447 superclass = c_parser_peek_token (parser)->value;
5448 c_parser_consume_token (parser);
5450 else
5451 superclass = NULL_TREE;
5452 if (iface_p)
5454 tree proto = NULL_TREE;
5455 if (c_parser_next_token_is (parser, CPP_LESS))
5456 proto = c_parser_objc_protocol_refs (parser);
5457 objc_start_class_interface (id1, superclass, proto);
5459 else
5460 objc_start_class_implementation (id1, superclass);
5461 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5462 c_parser_objc_class_instance_variables (parser);
5463 if (iface_p)
5465 objc_continue_interface ();
5466 c_parser_objc_methodprotolist (parser);
5467 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5468 objc_finish_interface ();
5470 else
5472 objc_continue_implementation ();
5473 return;
5477 /* Parse objc-class-instance-variables.
5479 objc-class-instance-variables:
5480 { objc-instance-variable-decl-list[opt] }
5482 objc-instance-variable-decl-list:
5483 objc-visibility-spec
5484 objc-instance-variable-decl ;
5486 objc-instance-variable-decl-list objc-visibility-spec
5487 objc-instance-variable-decl-list objc-instance-variable-decl ;
5488 objc-instance-variable-decl-list ;
5490 objc-visibility-spec:
5491 @private
5492 @protected
5493 @public
5495 objc-instance-variable-decl:
5496 struct-declaration
5499 static void
5500 c_parser_objc_class_instance_variables (c_parser *parser)
5502 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5503 c_parser_consume_token (parser);
5504 while (c_parser_next_token_is_not (parser, CPP_EOF))
5506 tree decls;
5507 /* Parse any stray semicolon. */
5508 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5510 if (pedantic)
5511 pedwarn ("extra semicolon in struct or union specified");
5512 c_parser_consume_token (parser);
5513 continue;
5515 /* Stop if at the end of the instance variables. */
5516 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5518 c_parser_consume_token (parser);
5519 break;
5521 /* Parse any objc-visibility-spec. */
5522 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5524 c_parser_consume_token (parser);
5525 objc_set_visibility (2);
5526 continue;
5528 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5530 c_parser_consume_token (parser);
5531 objc_set_visibility (0);
5532 continue;
5534 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5536 c_parser_consume_token (parser);
5537 objc_set_visibility (1);
5538 continue;
5540 /* Parse some comma-separated declarations. */
5541 decls = c_parser_struct_declaration (parser);
5543 /* Comma-separated instance variables are chained together in
5544 reverse order; add them one by one. */
5545 tree ivar = nreverse (decls);
5546 for (; ivar; ivar = TREE_CHAIN (ivar))
5547 objc_add_instance_variable (copy_node (ivar));
5549 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5553 /* Parse an objc-class-declaration.
5555 objc-class-declaration:
5556 @class identifier-list ;
5559 static void
5560 c_parser_objc_class_declaration (c_parser *parser)
5562 tree list = NULL_TREE;
5563 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5564 c_parser_consume_token (parser);
5565 /* Any identifiers, including those declared as type names, are OK
5566 here. */
5567 while (true)
5569 tree id;
5570 if (c_parser_next_token_is_not (parser, CPP_NAME))
5572 c_parser_error (parser, "expected identifier");
5573 break;
5575 id = c_parser_peek_token (parser)->value;
5576 list = chainon (list, build_tree_list (NULL_TREE, id));
5577 c_parser_consume_token (parser);
5578 if (c_parser_next_token_is (parser, CPP_COMMA))
5579 c_parser_consume_token (parser);
5580 else
5581 break;
5583 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5584 objc_declare_class (list);
5587 /* Parse an objc-alias-declaration.
5589 objc-alias-declaration:
5590 @compatibility_alias identifier identifier ;
5593 static void
5594 c_parser_objc_alias_declaration (c_parser *parser)
5596 tree id1, id2;
5597 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5598 c_parser_consume_token (parser);
5599 if (c_parser_next_token_is_not (parser, CPP_NAME))
5601 c_parser_error (parser, "expected identifier");
5602 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5603 return;
5605 id1 = c_parser_peek_token (parser)->value;
5606 c_parser_consume_token (parser);
5607 if (c_parser_next_token_is_not (parser, CPP_NAME))
5609 c_parser_error (parser, "expected identifier");
5610 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5611 return;
5613 id2 = c_parser_peek_token (parser)->value;
5614 c_parser_consume_token (parser);
5615 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5616 objc_declare_alias (id1, id2);
5619 /* Parse an objc-protocol-definition.
5621 objc-protocol-definition:
5622 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5623 @protocol identifier-list ;
5625 "@protocol identifier ;" should be resolved as "@protocol
5626 identifier-list ;": objc-methodprotolist may not start with a
5627 semicolon in the first alternative if objc-protocol-refs are
5628 omitted. */
5630 static void
5631 c_parser_objc_protocol_definition (c_parser *parser)
5633 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5634 c_parser_consume_token (parser);
5635 if (c_parser_next_token_is_not (parser, CPP_NAME))
5637 c_parser_error (parser, "expected identifier");
5638 return;
5640 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5641 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5643 tree list = NULL_TREE;
5644 /* Any identifiers, including those declared as type names, are
5645 OK here. */
5646 while (true)
5648 tree id;
5649 if (c_parser_next_token_is_not (parser, CPP_NAME))
5651 c_parser_error (parser, "expected identifier");
5652 break;
5654 id = c_parser_peek_token (parser)->value;
5655 list = chainon (list, build_tree_list (NULL_TREE, id));
5656 c_parser_consume_token (parser);
5657 if (c_parser_next_token_is (parser, CPP_COMMA))
5658 c_parser_consume_token (parser);
5659 else
5660 break;
5662 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5663 objc_declare_protocols (list);
5665 else
5667 tree id = c_parser_peek_token (parser)->value;
5668 tree proto = NULL_TREE;
5669 c_parser_consume_token (parser);
5670 if (c_parser_next_token_is (parser, CPP_LESS))
5671 proto = c_parser_objc_protocol_refs (parser);
5672 objc_pq_context = 1;
5673 objc_start_protocol (id, proto);
5674 c_parser_objc_methodprotolist (parser);
5675 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5676 objc_pq_context = 0;
5677 objc_finish_interface ();
5681 /* Parse an objc-method-type.
5683 objc-method-type:
5688 static enum tree_code
5689 c_parser_objc_method_type (c_parser *parser)
5691 switch (c_parser_peek_token (parser)->type)
5693 case CPP_PLUS:
5694 c_parser_consume_token (parser);
5695 return PLUS_EXPR;
5696 case CPP_MINUS:
5697 c_parser_consume_token (parser);
5698 return MINUS_EXPR;
5699 default:
5700 gcc_unreachable ();
5704 /* Parse an objc-method-definition.
5706 objc-method-definition:
5707 objc-method-type objc-method-decl ;[opt] compound-statement
5710 static void
5711 c_parser_objc_method_definition (c_parser *parser)
5713 enum tree_code type = c_parser_objc_method_type (parser);
5714 tree decl;
5715 objc_set_method_type (type);
5716 objc_pq_context = 1;
5717 decl = c_parser_objc_method_decl (parser);
5718 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5720 c_parser_consume_token (parser);
5721 if (pedantic)
5722 pedwarn ("extra semicolon in method definition specified");
5724 objc_pq_context = 0;
5725 objc_start_method_definition (decl);
5726 add_stmt (c_parser_compound_statement (parser));
5727 objc_finish_method_definition (current_function_decl);
5730 /* Parse an objc-methodprotolist.
5732 objc-methodprotolist:
5733 empty
5734 objc-methodprotolist objc-methodproto
5735 objc-methodprotolist declaration
5736 objc-methodprotolist ;
5738 The declaration is a data definition, which may be missing
5739 declaration specifiers under the same rules and diagnostics as
5740 other data definitions outside functions, and the stray semicolon
5741 is diagnosed the same way as a stray semicolon outside a
5742 function. */
5744 static void
5745 c_parser_objc_methodprotolist (c_parser *parser)
5747 while (true)
5749 /* The list is terminated by @end. */
5750 switch (c_parser_peek_token (parser)->type)
5752 case CPP_SEMICOLON:
5753 if (pedantic)
5754 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5755 c_parser_consume_token (parser);
5756 break;
5757 case CPP_PLUS:
5758 case CPP_MINUS:
5759 c_parser_objc_methodproto (parser);
5760 break;
5761 case CPP_EOF:
5762 return;
5763 default:
5764 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5765 return;
5766 c_parser_declaration_or_fndef (parser, false, true, false, true);
5767 break;
5772 /* Parse an objc-methodproto.
5774 objc-methodproto:
5775 objc-method-type objc-method-decl ;
5778 static void
5779 c_parser_objc_methodproto (c_parser *parser)
5781 enum tree_code type = c_parser_objc_method_type (parser);
5782 tree decl;
5783 objc_set_method_type (type);
5784 /* Remember protocol qualifiers in prototypes. */
5785 objc_pq_context = 1;
5786 decl = c_parser_objc_method_decl (parser);
5787 /* Forget protocol qualifiers here. */
5788 objc_pq_context = 0;
5789 objc_add_method_declaration (decl);
5790 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5793 /* Parse an objc-method-decl.
5795 objc-method-decl:
5796 ( objc-type-name ) objc-selector
5797 objc-selector
5798 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5799 objc-keyword-selector objc-optparmlist
5801 objc-keyword-selector:
5802 objc-keyword-decl
5803 objc-keyword-selector objc-keyword-decl
5805 objc-keyword-decl:
5806 objc-selector : ( objc-type-name ) identifier
5807 objc-selector : identifier
5808 : ( objc-type-name ) identifier
5809 : identifier
5811 objc-optparmlist:
5812 objc-optparms objc-optellipsis
5814 objc-optparms:
5815 empty
5816 objc-opt-parms , parameter-declaration
5818 objc-optellipsis:
5819 empty
5820 , ...
5823 static tree
5824 c_parser_objc_method_decl (c_parser *parser)
5826 tree type = NULL_TREE;
5827 tree sel;
5828 tree parms = NULL_TREE;
5829 bool ellipsis = false;
5831 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5833 c_parser_consume_token (parser);
5834 type = c_parser_objc_type_name (parser);
5835 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5837 sel = c_parser_objc_selector (parser);
5838 /* If there is no selector, or a colon follows, we have an
5839 objc-keyword-selector. If there is a selector, and a colon does
5840 not follow, that selector ends the objc-method-decl. */
5841 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5843 tree tsel = sel;
5844 tree list = NULL_TREE;
5845 while (true)
5847 tree atype = NULL_TREE, id, keyworddecl;
5848 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5849 break;
5850 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5852 c_parser_consume_token (parser);
5853 atype = c_parser_objc_type_name (parser);
5854 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5855 "expected %<)%>");
5857 if (c_parser_next_token_is_not (parser, CPP_NAME))
5859 c_parser_error (parser, "expected identifier");
5860 return error_mark_node;
5862 id = c_parser_peek_token (parser)->value;
5863 c_parser_consume_token (parser);
5864 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5865 list = chainon (list, keyworddecl);
5866 tsel = c_parser_objc_selector (parser);
5867 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5868 break;
5870 /* Parse the optional parameter list. Optional Objective-C
5871 method parameters follow the C syntax, and may include '...'
5872 to denote a variable number of arguments. */
5873 parms = make_node (TREE_LIST);
5874 while (c_parser_next_token_is (parser, CPP_COMMA))
5876 struct c_parm *parm;
5877 c_parser_consume_token (parser);
5878 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5880 ellipsis = true;
5881 c_parser_consume_token (parser);
5882 break;
5884 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5885 if (parm == NULL)
5886 break;
5887 parms = chainon (parms,
5888 build_tree_list (NULL_TREE, grokparm (parm)));
5890 sel = list;
5892 return objc_build_method_signature (type, sel, parms, ellipsis);
5895 /* Parse an objc-type-name.
5897 objc-type-name:
5898 objc-type-qualifiers[opt] type-name
5899 objc-type-qualifiers[opt]
5901 objc-type-qualifiers:
5902 objc-type-qualifier
5903 objc-type-qualifiers objc-type-qualifier
5905 objc-type-qualifier: one of
5906 in out inout bycopy byref oneway
5909 static tree
5910 c_parser_objc_type_name (c_parser *parser)
5912 tree quals = NULL_TREE;
5913 struct c_type_name *typename = NULL;
5914 tree type = NULL_TREE;
5915 while (true)
5917 c_token *token = c_parser_peek_token (parser);
5918 if (token->type == CPP_KEYWORD
5919 && (token->keyword == RID_IN
5920 || token->keyword == RID_OUT
5921 || token->keyword == RID_INOUT
5922 || token->keyword == RID_BYCOPY
5923 || token->keyword == RID_BYREF
5924 || token->keyword == RID_ONEWAY))
5926 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
5927 c_parser_consume_token (parser);
5929 else
5930 break;
5932 if (c_parser_next_token_starts_typename (parser))
5933 typename = c_parser_type_name (parser);
5934 if (typename)
5935 type = groktypename (typename);
5936 return build_tree_list (quals, type);
5939 /* Parse objc-protocol-refs.
5941 objc-protocol-refs:
5942 < identifier-list >
5945 static tree
5946 c_parser_objc_protocol_refs (c_parser *parser)
5948 tree list = NULL_TREE;
5949 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
5950 c_parser_consume_token (parser);
5951 /* Any identifiers, including those declared as type names, are OK
5952 here. */
5953 while (true)
5955 tree id;
5956 if (c_parser_next_token_is_not (parser, CPP_NAME))
5958 c_parser_error (parser, "expected identifier");
5959 break;
5961 id = c_parser_peek_token (parser)->value;
5962 list = chainon (list, build_tree_list (NULL_TREE, id));
5963 c_parser_consume_token (parser);
5964 if (c_parser_next_token_is (parser, CPP_COMMA))
5965 c_parser_consume_token (parser);
5966 else
5967 break;
5969 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
5970 return list;
5973 /* Parse an objc-try-catch-statement.
5975 objc-try-catch-statement:
5976 @try compound-statement objc-catch-list[opt]
5977 @try compound-statement objc-catch-list[opt] @finally compound-statement
5979 objc-catch-list:
5980 @catch ( parameter-declaration ) compound-statement
5981 objc-catch-list @catch ( parameter-declaration ) compound-statement
5984 static void
5985 c_parser_objc_try_catch_statement (c_parser *parser)
5987 location_t loc;
5988 tree stmt;
5989 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
5990 c_parser_consume_token (parser);
5991 loc = c_parser_peek_token (parser)->location;
5992 stmt = c_parser_compound_statement (parser);
5993 objc_begin_try_stmt (loc, stmt);
5994 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
5996 struct c_parm *parm;
5997 c_parser_consume_token (parser);
5998 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5999 break;
6000 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6001 if (parm == NULL)
6003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6004 break;
6006 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6007 objc_begin_catch_clause (grokparm (parm));
6008 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6009 c_parser_compound_statement_nostart (parser);
6010 objc_finish_catch_clause ();
6012 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6014 location_t finloc;
6015 tree finstmt;
6016 c_parser_consume_token (parser);
6017 finloc = c_parser_peek_token (parser)->location;
6018 finstmt = c_parser_compound_statement (parser);
6019 objc_build_finally_clause (finloc, finstmt);
6021 objc_finish_try_stmt ();
6024 /* Parse an objc-synchronized-statement.
6026 objc-synchronized-statement:
6027 @synchronized ( expression ) compound-statement
6030 static void
6031 c_parser_objc_synchronized_statement (c_parser *parser)
6033 location_t loc;
6034 tree expr, stmt;
6035 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6036 c_parser_consume_token (parser);
6037 loc = c_parser_peek_token (parser)->location;
6038 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6040 expr = c_parser_expression (parser).value;
6041 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6043 else
6044 expr = error_mark_node;
6045 stmt = c_parser_compound_statement (parser);
6046 objc_build_synchronized (loc, expr, stmt);
6049 /* Parse an objc-selector; return NULL_TREE without an error if the
6050 next token is not an objc-selector.
6052 objc-selector:
6053 identifier
6054 one of
6055 enum struct union if else while do for switch case default
6056 break continue return goto asm sizeof typeof __alignof
6057 unsigned long const short volatile signed restrict _Complex
6058 in out inout bycopy byref oneway int char float double void _Bool
6060 ??? Why this selection of keywords but not, for example, storage
6061 class specifiers? */
6063 static tree
6064 c_parser_objc_selector (c_parser *parser)
6066 c_token *token = c_parser_peek_token (parser);
6067 tree value = token->value;
6068 if (token->type == CPP_NAME)
6070 c_parser_consume_token (parser);
6071 return value;
6073 if (token->type != CPP_KEYWORD)
6074 return NULL_TREE;
6075 switch (token->keyword)
6077 case RID_ENUM:
6078 case RID_STRUCT:
6079 case RID_UNION:
6080 case RID_IF:
6081 case RID_ELSE:
6082 case RID_WHILE:
6083 case RID_DO:
6084 case RID_FOR:
6085 case RID_SWITCH:
6086 case RID_CASE:
6087 case RID_DEFAULT:
6088 case RID_BREAK:
6089 case RID_CONTINUE:
6090 case RID_RETURN:
6091 case RID_GOTO:
6092 case RID_ASM:
6093 case RID_SIZEOF:
6094 case RID_TYPEOF:
6095 case RID_ALIGNOF:
6096 case RID_UNSIGNED:
6097 case RID_LONG:
6098 case RID_CONST:
6099 case RID_SHORT:
6100 case RID_VOLATILE:
6101 case RID_SIGNED:
6102 case RID_RESTRICT:
6103 case RID_COMPLEX:
6104 case RID_IN:
6105 case RID_OUT:
6106 case RID_INOUT:
6107 case RID_BYCOPY:
6108 case RID_BYREF:
6109 case RID_ONEWAY:
6110 case RID_INT:
6111 case RID_CHAR:
6112 case RID_FLOAT:
6113 case RID_DOUBLE:
6114 case RID_VOID:
6115 case RID_BOOL:
6116 c_parser_consume_token (parser);
6117 return value;
6118 default:
6119 return NULL_TREE;
6123 /* Parse an objc-selector-arg.
6125 objc-selector-arg:
6126 objc-selector
6127 objc-keywordname-list
6129 objc-keywordname-list:
6130 objc-keywordname
6131 objc-keywordname-list objc-keywordname
6133 objc-keywordname:
6134 objc-selector :
6138 static tree
6139 c_parser_objc_selector_arg (c_parser *parser)
6141 tree sel = c_parser_objc_selector (parser);
6142 tree list = NULL_TREE;
6143 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6144 return sel;
6145 while (true)
6147 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6148 return list;
6149 list = chainon (list, build_tree_list (sel, NULL_TREE));
6150 sel = c_parser_objc_selector (parser);
6151 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6152 break;
6154 return list;
6157 /* Parse an objc-receiver.
6159 objc-receiver:
6160 expression
6161 class-name
6162 type-name
6165 static tree
6166 c_parser_objc_receiver (c_parser *parser)
6168 if (c_parser_peek_token (parser)->type == CPP_NAME
6169 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6170 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6172 tree id = c_parser_peek_token (parser)->value;
6173 c_parser_consume_token (parser);
6174 return objc_get_class_reference (id);
6176 return c_parser_expression (parser).value;
6179 /* Parse objc-message-args.
6181 objc-message-args:
6182 objc-selector
6183 objc-keywordarg-list
6185 objc-keywordarg-list:
6186 objc-keywordarg
6187 objc-keywordarg-list objc-keywordarg
6189 objc-keywordarg:
6190 objc-selector : objc-keywordexpr
6191 : objc-keywordexpr
6194 static tree
6195 c_parser_objc_message_args (c_parser *parser)
6197 tree sel = c_parser_objc_selector (parser);
6198 tree list = NULL_TREE;
6199 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6200 return sel;
6201 while (true)
6203 tree keywordexpr;
6204 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6205 return list;
6206 keywordexpr = c_parser_objc_keywordexpr (parser);
6207 list = chainon (list, build_tree_list (sel, keywordexpr));
6208 sel = c_parser_objc_selector (parser);
6209 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6210 break;
6212 return list;
6215 /* Parse an objc-keywordexpr.
6217 objc-keywordexpr:
6218 nonempty-expr-list
6221 static tree
6222 c_parser_objc_keywordexpr (c_parser *parser)
6224 tree list = c_parser_expr_list (parser, true);
6225 if (TREE_CHAIN (list) == NULL_TREE)
6227 /* Just return the expression, remove a level of
6228 indirection. */
6229 return TREE_VALUE (list);
6231 else
6233 /* We have a comma expression, we will collapse later. */
6234 return list;
6239 /* The actual parser and external interface. ??? Does this need to be
6240 garbage-collected? */
6242 static GTY (()) c_parser *the_parser;
6244 /* Parse a single source file. */
6246 void
6247 c_parse_file (void)
6249 the_parser = c_parser_new ();
6250 c_parser_translation_unit (the_parser);
6251 the_parser = NULL;
6254 #include "gt-c-parser.h"