2005-05-11 Kenneth Zadeck <zadeck@naturalbridge.com>
[official-gcc.git] / gcc / c-parser.c
blob96bab469f4198c45b62dc63e0d64d27856e84038
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 *msgid)
640 c_token *token = c_parser_peek_token (parser);
641 if (parser->error)
642 return;
643 parser->error = true;
644 if (!msgid)
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 (msgid,
650 /* Because c_parse_error does not understand
651 CPP_KEYWORD, keywords are treated like
652 identifiers. */
653 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
654 token->value);
657 /* If the next token is of the indicated TYPE, consume it. Otherwise,
658 issue the error MSGID. If MSGID is NULL then a message has already
659 been produced and no message will be produced this time. Returns
660 true if found, false otherwise. */
662 static bool
663 c_parser_require (c_parser *parser,
664 enum cpp_ttype type,
665 const char *msgid)
667 if (c_parser_next_token_is (parser, type))
669 c_parser_consume_token (parser);
670 return true;
672 else
674 c_parser_error (parser, msgid);
675 return false;
679 /* If the next token is the indicated keyword, consume it. Otherwise,
680 issue the error MSGID. Returns true if found, false otherwise. */
682 static bool
683 c_parser_require_keyword (c_parser *parser,
684 enum rid keyword,
685 const char *msgid)
687 if (c_parser_next_token_is_keyword (parser, keyword))
689 c_parser_consume_token (parser);
690 return true;
692 else
694 c_parser_error (parser, msgid);
695 return false;
699 /* Like c_parser_require, except that tokens will be skipped until the
700 desired token is found. An error message is still produced if the
701 next token is not as expected. If MSGID is NULL then a message has
702 already been produced and no message will be produced this
703 time. */
705 static void
706 c_parser_skip_until_found (c_parser *parser,
707 enum cpp_ttype type,
708 const char *msgid)
710 unsigned nesting_depth = 0;
712 if (c_parser_require (parser, type, msgid))
713 return;
715 /* Skip tokens until the desired token is found. */
716 while (true)
718 /* Peek at the next token. */
719 c_token *token = c_parser_peek_token (parser);
720 /* If we've reached the token we want, consume it and stop. */
721 if (token->type == type && !nesting_depth)
723 c_parser_consume_token (parser);
724 break;
726 /* If we've run out of tokens, stop. */
727 if (token->type == CPP_EOF)
728 return;
729 if (token->type == CPP_OPEN_BRACE
730 || token->type == CPP_OPEN_PAREN
731 || token->type == CPP_OPEN_SQUARE)
732 ++nesting_depth;
733 else if (token->type == CPP_CLOSE_BRACE
734 || token->type == CPP_CLOSE_PAREN
735 || token->type == CPP_CLOSE_SQUARE)
737 if (nesting_depth-- == 0)
738 break;
740 /* Consume this token. */
741 c_parser_consume_token (parser);
743 parser->error = false;
746 /* Skip tokens until the end of a parameter is found, but do not
747 consume the comma, semicolon or closing delimiter. */
749 static void
750 c_parser_skip_to_end_of_parameter (c_parser *parser)
752 unsigned nesting_depth = 0;
754 while (true)
756 c_token *token = c_parser_peek_token (parser);
757 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
758 && !nesting_depth)
759 break;
760 /* If we've run out of tokens, stop. */
761 if (token->type == CPP_EOF)
762 return;
763 if (token->type == CPP_OPEN_BRACE
764 || token->type == CPP_OPEN_PAREN
765 || token->type == CPP_OPEN_SQUARE)
766 ++nesting_depth;
767 else if (token->type == CPP_CLOSE_BRACE
768 || token->type == CPP_CLOSE_PAREN
769 || token->type == CPP_CLOSE_SQUARE)
771 if (nesting_depth-- == 0)
772 break;
774 /* Consume this token. */
775 c_parser_consume_token (parser);
777 parser->error = false;
780 /* Skip tokens until we have consumed an entire block, or until we
781 have consumed a non-nested ';'. */
783 static void
784 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
786 unsigned nesting_depth = 0;
788 while (true)
790 c_token *token;
792 /* Peek at the next token. */
793 token = c_parser_peek_token (parser);
794 /* If we've run out of tokens, stop. */
795 if (token->type == CPP_EOF)
796 return;
797 /* If the next token is a ';', we have reached the end of the
798 statement. */
799 if (token->type == CPP_SEMICOLON && !nesting_depth)
801 /* Consume the ';'. */
802 c_parser_consume_token (parser);
803 break;
805 /* If the next token is a non-nested '}', then we have reached
806 the end of the current block. */
807 if (token->type == CPP_CLOSE_BRACE
808 && (nesting_depth == 0 || --nesting_depth == 0))
810 c_parser_consume_token (parser);
811 break;
813 /* If it the next token is a '{', then we are entering a new
814 block. Consume the entire block. */
815 if (token->type == CPP_OPEN_BRACE)
816 ++nesting_depth;
817 c_parser_consume_token (parser);
819 parser->error = false;
823 /* Save the warning flags which are controlled by __extension__. */
825 static inline int
826 disable_extension_diagnostics (void)
828 int ret = (pedantic
829 | (warn_pointer_arith << 1)
830 | (warn_traditional << 2)
831 | (flag_iso << 3));
832 pedantic = 0;
833 warn_pointer_arith = 0;
834 warn_traditional = 0;
835 flag_iso = 0;
836 return ret;
839 /* Restore the warning flags which are controlled by __extension__.
840 FLAGS is the return value from disable_extension_diagnostics. */
842 static inline void
843 restore_extension_diagnostics (int flags)
845 pedantic = flags & 1;
846 warn_pointer_arith = (flags >> 1) & 1;
847 warn_traditional = (flags >> 2) & 1;
848 flag_iso = (flags >> 3) & 1;
851 /* Possibly kinds of declarator to parse. */
852 typedef enum c_dtr_syn {
853 /* A normal declarator with an identifier. */
854 C_DTR_NORMAL,
855 /* An abstract declarator (maybe empty). */
856 C_DTR_ABSTRACT,
857 /* A parameter declarator: may be either, but after a type name does
858 not redeclare a typedef name as an identifier if it can
859 alternatively be interpreted as a typedef name; see DR#009,
860 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
861 following DR#249. For example, given a typedef T, "int T" and
862 "int *T" are valid parameter declarations redeclaring T, while
863 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
864 abstract declarators rather than involving redundant parentheses;
865 the same applies with attributes inside the parentheses before
866 "T". */
867 C_DTR_PARM
868 } c_dtr_syn;
870 static void c_parser_external_declaration (c_parser *);
871 static void c_parser_asm_definition (c_parser *);
872 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
873 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
874 bool);
875 static struct c_typespec c_parser_enum_specifier (c_parser *);
876 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
877 static tree c_parser_struct_declaration (c_parser *);
878 static struct c_typespec c_parser_typeof_specifier (c_parser *);
879 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
880 bool *);
881 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
882 c_dtr_syn, bool *);
883 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
884 bool,
885 struct c_declarator *);
886 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
887 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
888 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
889 static tree c_parser_simple_asm_expr (c_parser *);
890 static tree c_parser_attributes (c_parser *);
891 static struct c_type_name *c_parser_type_name (c_parser *);
892 static struct c_expr c_parser_initializer (c_parser *);
893 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
894 static void c_parser_initelt (c_parser *);
895 static void c_parser_initval (c_parser *, struct c_expr *);
896 static tree c_parser_compound_statement (c_parser *);
897 static void c_parser_compound_statement_nostart (c_parser *);
898 static void c_parser_label (c_parser *);
899 static void c_parser_statement (c_parser *);
900 static void c_parser_statement_after_labels (c_parser *);
901 static void c_parser_if_statement (c_parser *);
902 static void c_parser_switch_statement (c_parser *);
903 static void c_parser_while_statement (c_parser *);
904 static void c_parser_do_statement (c_parser *);
905 static void c_parser_for_statement (c_parser *);
906 static tree c_parser_asm_statement (c_parser *);
907 static tree c_parser_asm_operands (c_parser *);
908 static tree c_parser_asm_clobbers (c_parser *);
909 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
910 static struct c_expr c_parser_conditional_expression (c_parser *,
911 struct c_expr *);
912 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
913 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
914 static struct c_expr c_parser_unary_expression (c_parser *);
915 static struct c_expr c_parser_sizeof_expression (c_parser *);
916 static struct c_expr c_parser_alignof_expression (c_parser *);
917 static struct c_expr c_parser_postfix_expression (c_parser *);
918 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
919 struct c_type_name *);
920 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
921 struct c_expr);
922 static struct c_expr c_parser_expression (c_parser *);
923 static tree c_parser_expr_list (c_parser *);
925 /* These Objective-C parser functions are only ever called when
926 compiling Objective-C. */
927 static void c_parser_objc_class_definition (c_parser *);
928 static void c_parser_objc_class_instance_variables (c_parser *);
929 static void c_parser_objc_class_declaration (c_parser *);
930 static void c_parser_objc_alias_declaration (c_parser *);
931 static void c_parser_objc_protocol_definition (c_parser *);
932 static enum tree_code c_parser_objc_method_type (c_parser *);
933 static void c_parser_objc_method_definition (c_parser *);
934 static void c_parser_objc_methodprotolist (c_parser *);
935 static void c_parser_objc_methodproto (c_parser *);
936 static tree c_parser_objc_method_decl (c_parser *);
937 static tree c_parser_objc_type_name (c_parser *);
938 static tree c_parser_objc_protocol_refs (c_parser *);
939 static void c_parser_objc_try_catch_statement (c_parser *);
940 static void c_parser_objc_synchronized_statement (c_parser *);
941 static tree c_parser_objc_selector (c_parser *);
942 static tree c_parser_objc_selector_arg (c_parser *);
943 static tree c_parser_objc_receiver (c_parser *);
944 static tree c_parser_objc_message_args (c_parser *);
945 static tree c_parser_objc_keywordexpr (c_parser *);
947 /* Parse a translation unit (C90 6.7, C99 6.9).
949 translation-unit:
950 external-declarations
952 external-declarations:
953 external-declaration
954 external-declarations external-declaration
956 GNU extensions:
958 translation-unit:
959 empty
962 static void
963 c_parser_translation_unit (c_parser *parser)
965 if (c_parser_next_token_is (parser, CPP_EOF))
967 if (pedantic)
968 pedwarn ("ISO C forbids an empty source file");
970 else
972 void *obstack_position = obstack_alloc (&parser_obstack, 0);
975 ggc_collect ();
976 c_parser_external_declaration (parser);
977 obstack_free (&parser_obstack, obstack_position);
979 while (c_parser_next_token_is_not (parser, CPP_EOF));
983 /* Parse an external declaration (C90 6.7, C99 6.9).
985 external-declaration:
986 function-definition
987 declaration
989 GNU extensions:
991 external-declaration:
992 asm-definition
994 __extension__ external-declaration
996 Objective-C:
998 external-declaration:
999 objc-class-definition
1000 objc-class-declaration
1001 objc-alias-declaration
1002 objc-protocol-definition
1003 objc-method-definition
1004 @end
1007 static void
1008 c_parser_external_declaration (c_parser *parser)
1010 int ext;
1011 switch (c_parser_peek_token (parser)->type)
1013 case CPP_KEYWORD:
1014 switch (c_parser_peek_token (parser)->keyword)
1016 case RID_EXTENSION:
1017 ext = disable_extension_diagnostics ();
1018 c_parser_consume_token (parser);
1019 c_parser_external_declaration (parser);
1020 restore_extension_diagnostics (ext);
1021 break;
1022 case RID_ASM:
1023 c_parser_asm_definition (parser);
1024 break;
1025 case RID_AT_INTERFACE:
1026 case RID_AT_IMPLEMENTATION:
1027 gcc_assert (c_dialect_objc ());
1028 c_parser_objc_class_definition (parser);
1029 break;
1030 case RID_AT_CLASS:
1031 gcc_assert (c_dialect_objc ());
1032 c_parser_objc_class_declaration (parser);
1033 break;
1034 case RID_AT_ALIAS:
1035 gcc_assert (c_dialect_objc ());
1036 c_parser_objc_alias_declaration (parser);
1037 break;
1038 case RID_AT_PROTOCOL:
1039 gcc_assert (c_dialect_objc ());
1040 c_parser_objc_protocol_definition (parser);
1041 break;
1042 case RID_AT_END:
1043 gcc_assert (c_dialect_objc ());
1044 c_parser_consume_token (parser);
1045 objc_finish_implementation ();
1046 break;
1047 default:
1048 goto decl_or_fndef;
1050 break;
1051 case CPP_SEMICOLON:
1052 if (pedantic)
1053 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1054 c_parser_consume_token (parser);
1055 break;
1056 case CPP_PLUS:
1057 case CPP_MINUS:
1058 if (c_dialect_objc ())
1060 c_parser_objc_method_definition (parser);
1061 break;
1063 /* Else fall through, and yield a syntax error trying to parse
1064 as a declaration or function definition. */
1065 default:
1066 decl_or_fndef:
1067 /* A declaration or a function definition. We can only tell
1068 which after parsing the declaration specifiers, if any, and
1069 the first declarator. */
1070 c_parser_declaration_or_fndef (parser, true, true, false, true);
1071 break;
1075 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1076 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1077 accepted; otherwise (old-style parameter declarations) only other
1078 declarations are accepted. If NESTED is true, we are inside a
1079 function or parsing old-style parameter declarations; any functions
1080 encountered are nested functions and declaration specifiers are
1081 required; otherwise we are at top level and functions are normal
1082 functions and declaration specifiers may be optional. If EMPTY_OK
1083 is true, empty declarations are OK (subject to all other
1084 constraints); otherwise (old-style parameter declarations) they are
1085 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1086 may start with attributes; otherwise they may not.
1088 declaration:
1089 declaration-specifiers init-declarator-list[opt] ;
1091 function-definition:
1092 declaration-specifiers[opt] declarator declaration-list[opt]
1093 compound-statement
1095 declaration-list:
1096 declaration
1097 declaration-list declaration
1099 init-declarator-list:
1100 init-declarator
1101 init-declarator-list , init-declarator
1103 init-declarator:
1104 declarator simple-asm-expr[opt] attributes[opt]
1105 declarator simple-asm-expr[opt] attributes[opt] = initializer
1107 GNU extensions:
1109 nested-function-definition:
1110 declaration-specifiers declarator declaration-list[opt]
1111 compound-statement
1113 The simple-asm-expr and attributes are GNU extensions.
1115 This function does not handle __extension__; that is handled in its
1116 callers. ??? Following the old parser, __extension__ may start
1117 external declarations, declarations in functions and declarations
1118 at the start of "for" loops, but not old-style parameter
1119 declarations.
1121 C99 requires declaration specifiers in a function definition; the
1122 absence is diagnosed through the diagnosis of implicit int. In GNU
1123 C we also allow but diagnose declarations without declaration
1124 specifiers, but only at top level (elsewhere they conflict with
1125 other syntax). */
1127 static void
1128 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1129 bool nested, bool start_attr_ok)
1131 struct c_declspecs *specs;
1132 tree prefix_attrs;
1133 tree all_prefix_attrs;
1134 bool diagnosed_no_specs = false;
1135 specs = build_null_declspecs ();
1136 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1137 if (parser->error)
1139 c_parser_skip_to_end_of_block_or_statement (parser);
1140 return;
1142 if (nested && !specs->declspecs_seen_p)
1144 c_parser_error (parser, "expected declaration specifiers");
1145 c_parser_skip_to_end_of_block_or_statement (parser);
1146 return;
1148 finish_declspecs (specs);
1149 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1151 if (empty_ok)
1152 shadow_tag (specs);
1153 else
1155 shadow_tag_warned (specs, 1);
1156 pedwarn ("empty declaration");
1158 c_parser_consume_token (parser);
1159 return;
1161 pending_xref_error ();
1162 prefix_attrs = specs->attrs;
1163 all_prefix_attrs = prefix_attrs;
1164 specs->attrs = NULL_TREE;
1165 while (true)
1167 struct c_declarator *declarator;
1168 bool dummy = false;
1169 tree fnbody;
1170 /* Declaring either one or more declarators (in which case we
1171 should diagnose if there were no declaration specifiers) or a
1172 function definition (in which case the diagnostic for
1173 implicit int suffices). */
1174 declarator = c_parser_declarator (parser, specs->type_seen_p,
1175 C_DTR_NORMAL, &dummy);
1176 if (declarator == NULL)
1178 c_parser_skip_to_end_of_block_or_statement (parser);
1179 return;
1181 if (c_parser_next_token_is (parser, CPP_EQ)
1182 || c_parser_next_token_is (parser, CPP_COMMA)
1183 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1184 || c_parser_next_token_is_keyword (parser, RID_ASM)
1185 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1187 tree asm_name = NULL_TREE;
1188 tree postfix_attrs = NULL_TREE;
1189 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1191 diagnosed_no_specs = true;
1192 pedwarn ("data definition has no type or storage class");
1194 /* Having seen a data definition, there cannot now be a
1195 function definition. */
1196 fndef_ok = false;
1197 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1198 asm_name = c_parser_simple_asm_expr (parser);
1199 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1200 postfix_attrs = c_parser_attributes (parser);
1201 if (c_parser_next_token_is (parser, CPP_EQ))
1203 tree d;
1204 struct c_expr init;
1205 c_parser_consume_token (parser);
1206 /* The declaration of the variable is in effect while
1207 its initializer is parsed. */
1208 d = start_decl (declarator, specs, true,
1209 chainon (postfix_attrs, all_prefix_attrs));
1210 if (!d)
1211 d = error_mark_node;
1212 start_init (d, asm_name, global_bindings_p ());
1213 init = c_parser_initializer (parser);
1214 finish_init ();
1215 if (d != error_mark_node)
1217 maybe_warn_string_init (TREE_TYPE (d), init);
1218 finish_decl (d, init.value, asm_name);
1221 else
1223 tree d = start_decl (declarator, specs, false,
1224 chainon (postfix_attrs,
1225 all_prefix_attrs));
1226 if (d)
1227 finish_decl (d, NULL_TREE, asm_name);
1229 if (c_parser_next_token_is (parser, CPP_COMMA))
1231 c_parser_consume_token (parser);
1232 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1233 all_prefix_attrs = chainon (c_parser_attributes (parser),
1234 prefix_attrs);
1235 else
1236 all_prefix_attrs = prefix_attrs;
1237 continue;
1239 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1241 c_parser_consume_token (parser);
1242 return;
1244 else
1246 c_parser_error (parser, "expected %<,%> or %<;%>");
1247 c_parser_skip_to_end_of_block_or_statement (parser);
1248 return;
1251 else if (!fndef_ok)
1253 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1254 "%<asm%> or %<__attribute__%>");
1255 c_parser_skip_to_end_of_block_or_statement (parser);
1256 return;
1258 /* Function definition (nested or otherwise). */
1259 if (nested)
1261 if (pedantic)
1262 pedwarn ("ISO C forbids nested functions");
1263 push_function_context ();
1265 if (!start_function (specs, declarator, all_prefix_attrs))
1267 /* This can appear in many cases looking nothing like a
1268 function definition, so we don't give a more specific
1269 error suggesting there was one. */
1270 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1271 "or %<__attribute__%>");
1272 if (nested)
1273 pop_function_context ();
1274 break;
1276 /* Parse old-style parameter declarations. ??? Attributes are
1277 not allowed to start declaration specifiers here because of a
1278 syntax conflict between a function declaration with attribute
1279 suffix and a function definition with an attribute prefix on
1280 first old-style parameter declaration. Following the old
1281 parser, they are not accepted on subsequent old-style
1282 parameter declarations either. However, there is no
1283 ambiguity after the first declaration, nor indeed on the
1284 first as long as we don't allow postfix attributes after a
1285 declarator with a nonempty identifier list in a definition;
1286 and postfix attributes have never been accepted here in
1287 function definitions either. */
1288 while (c_parser_next_token_is_not (parser, CPP_EOF)
1289 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1290 c_parser_declaration_or_fndef (parser, false, false, true, false);
1291 DECL_SOURCE_LOCATION (current_function_decl)
1292 = c_parser_peek_token (parser)->location;
1293 store_parm_decls ();
1294 fnbody = c_parser_compound_statement (parser);
1295 if (nested)
1297 tree decl = current_function_decl;
1298 add_stmt (fnbody);
1299 finish_function ();
1300 pop_function_context ();
1301 add_stmt (build_stmt (DECL_EXPR, decl));
1303 else
1305 add_stmt (fnbody);
1306 finish_function ();
1308 break;
1312 /* Parse an asm-definition (asm() outside a function body). This is a
1313 GNU extension.
1315 asm-definition:
1316 simple-asm-expr ;
1319 static void
1320 c_parser_asm_definition (c_parser *parser)
1322 tree asm_str = c_parser_simple_asm_expr (parser);
1323 /* ??? This only works sensibly in the presence of
1324 -fno-unit-at-a-time; file-scope asms really need to be passed to
1325 cgraph which needs to preserve the order of functions and
1326 file-scope asms. */
1327 if (asm_str)
1328 assemble_asm (asm_str);
1329 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1332 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1333 6.7), adding them to SPECS (which may already include some).
1334 Storage class specifiers are accepted iff SCSPEC_OK; type
1335 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1336 the start iff START_ATTR_OK.
1338 declaration-specifiers:
1339 storage-class-specifier declaration-specifiers[opt]
1340 type-specifier declaration-specifiers[opt]
1341 type-qualifier declaration-specifiers[opt]
1342 function-specifier declaration-specifiers[opt]
1344 Function specifiers (inline) are from C99, and are currently
1345 handled as storage class specifiers, as is __thread.
1347 C90 6.5.1, C99 6.7.1:
1348 storage-class-specifier:
1349 typedef
1350 extern
1351 static
1352 auto
1353 register
1355 C99 6.7.4:
1356 function-specifier:
1357 inline
1359 C90 6.5.2, C99 6.7.2:
1360 type-specifier:
1361 void
1362 char
1363 short
1365 long
1366 float
1367 double
1368 signed
1369 unsigned
1370 _Bool
1371 _Complex
1372 [_Imaginary removed in C99 TC2]
1373 struct-or-union-specifier
1374 enum-specifier
1375 typedef-name
1377 (_Bool and _Complex are new in C99.)
1379 C90 6.5.3, C99 6.7.3:
1381 type-qualifier:
1382 const
1383 restrict
1384 volatile
1386 (restrict is new in C99.)
1388 GNU extensions:
1390 declaration-specifiers:
1391 attributes declaration-specifiers[opt]
1393 storage-class-specifier:
1394 __thread
1396 type-specifier:
1397 typeof-specifier
1399 Objective-C:
1401 type-specifier:
1402 class-name objc-protocol-refs[opt]
1403 typedef-name objc-protocol-refs
1404 objc-protocol-refs
1407 static void
1408 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1409 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1411 bool attrs_ok = start_attr_ok;
1412 bool seen_type = specs->type_seen_p;
1413 while (c_parser_next_token_is (parser, CPP_NAME)
1414 || c_parser_next_token_is (parser, CPP_KEYWORD)
1415 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1417 struct c_typespec t;
1418 tree attrs;
1419 if (c_parser_next_token_is (parser, CPP_NAME))
1421 tree value = c_parser_peek_token (parser)->value;
1422 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1423 /* This finishes the specifiers unless a type name is OK, it
1424 is declared as a type name and a type name hasn't yet
1425 been seen. */
1426 if (!typespec_ok || seen_type
1427 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1428 break;
1429 c_parser_consume_token (parser);
1430 seen_type = true;
1431 attrs_ok = true;
1432 if (kind == C_ID_TYPENAME
1433 && (!c_dialect_objc ()
1434 || c_parser_next_token_is_not (parser, CPP_LESS)))
1436 t.kind = ctsk_typedef;
1437 /* For a typedef name, record the meaning, not the name.
1438 In case of 'foo foo, bar;'. */
1439 t.spec = lookup_name (value);
1441 else
1443 tree proto = NULL_TREE;
1444 gcc_assert (c_dialect_objc ());
1445 t.kind = ctsk_objc;
1446 if (c_parser_next_token_is (parser, CPP_LESS))
1447 proto = c_parser_objc_protocol_refs (parser);
1448 t.spec = objc_get_protocol_qualified_type (value, proto);
1450 declspecs_add_type (specs, t);
1451 continue;
1453 if (c_parser_next_token_is (parser, CPP_LESS))
1455 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1456 nisse@lysator.liu.se. */
1457 tree proto;
1458 gcc_assert (c_dialect_objc ());
1459 if (!typespec_ok || seen_type)
1460 break;
1461 proto = c_parser_objc_protocol_refs (parser);
1462 t.kind = ctsk_objc;
1463 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1464 declspecs_add_type (specs, t);
1465 continue;
1467 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1468 switch (c_parser_peek_token (parser)->keyword)
1470 case RID_STATIC:
1471 case RID_EXTERN:
1472 case RID_REGISTER:
1473 case RID_TYPEDEF:
1474 case RID_INLINE:
1475 case RID_AUTO:
1476 case RID_THREAD:
1477 if (!scspec_ok)
1478 goto out;
1479 attrs_ok = true;
1480 /* TODO: Distinguish between function specifiers (inline)
1481 and storage class specifiers, either here or in
1482 declspecs_add_scspec. */
1483 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1484 c_parser_consume_token (parser);
1485 break;
1486 case RID_UNSIGNED:
1487 case RID_LONG:
1488 case RID_SHORT:
1489 case RID_SIGNED:
1490 case RID_COMPLEX:
1491 case RID_INT:
1492 case RID_CHAR:
1493 case RID_FLOAT:
1494 case RID_DOUBLE:
1495 case RID_VOID:
1496 case RID_BOOL:
1497 if (!typespec_ok)
1498 goto out;
1499 attrs_ok = true;
1500 seen_type = true;
1501 OBJC_NEED_RAW_IDENTIFIER (1);
1502 t.kind = ctsk_resword;
1503 t.spec = c_parser_peek_token (parser)->value;
1504 declspecs_add_type (specs, t);
1505 c_parser_consume_token (parser);
1506 break;
1507 case RID_ENUM:
1508 if (!typespec_ok)
1509 goto out;
1510 attrs_ok = true;
1511 seen_type = true;
1512 t = c_parser_enum_specifier (parser);
1513 declspecs_add_type (specs, t);
1514 break;
1515 case RID_STRUCT:
1516 case RID_UNION:
1517 if (!typespec_ok)
1518 goto out;
1519 attrs_ok = true;
1520 seen_type = true;
1521 t = c_parser_struct_or_union_specifier (parser);
1522 declspecs_add_type (specs, t);
1523 break;
1524 case RID_TYPEOF:
1525 /* ??? The old parser rejected typeof after other type
1526 specifiers, but is a syntax error the best way of
1527 handling this? */
1528 if (!typespec_ok || seen_type)
1529 goto out;
1530 attrs_ok = true;
1531 seen_type = true;
1532 t = c_parser_typeof_specifier (parser);
1533 declspecs_add_type (specs, t);
1534 break;
1535 case RID_CONST:
1536 case RID_VOLATILE:
1537 case RID_RESTRICT:
1538 attrs_ok = true;
1539 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1540 c_parser_consume_token (parser);
1541 break;
1542 case RID_ATTRIBUTE:
1543 if (!attrs_ok)
1544 goto out;
1545 attrs = c_parser_attributes (parser);
1546 declspecs_add_attrs (specs, attrs);
1547 break;
1548 default:
1549 goto out;
1552 out: ;
1555 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1557 enum-specifier:
1558 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1559 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1560 enum attributes[opt] identifier
1562 The form with trailing comma is new in C99. The forms with
1563 attributes are GNU extensions. In GNU C, we accept any expression
1564 without commas in the syntax (assignment expressions, not just
1565 conditional expressions); assignment expressions will be diagnosed
1566 as non-constant.
1568 enumerator-list:
1569 enumerator
1570 enumerator-list , enumerator
1572 enumerator:
1573 enumeration-constant
1574 enumeration-constant = constant-expression
1577 static struct c_typespec
1578 c_parser_enum_specifier (c_parser *parser)
1580 struct c_typespec ret;
1581 tree attrs;
1582 tree ident = NULL_TREE;
1583 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1584 c_parser_consume_token (parser);
1585 attrs = c_parser_attributes (parser);
1586 if (c_parser_next_token_is (parser, CPP_NAME))
1588 ident = c_parser_peek_token (parser)->value;
1589 c_parser_consume_token (parser);
1591 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1593 /* Parse an enum definition. */
1594 tree type = start_enum (ident);
1595 tree postfix_attrs;
1596 /* We chain the enumerators in reverse order, then put them in
1597 forward order at the end. */
1598 tree values = NULL_TREE;
1599 c_parser_consume_token (parser);
1600 while (true)
1602 tree enum_id;
1603 tree enum_value;
1604 tree enum_decl;
1605 bool seen_comma;
1606 if (c_parser_next_token_is_not (parser, CPP_NAME))
1608 c_parser_error (parser, "expected identifier");
1609 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1610 values = error_mark_node;
1611 break;
1613 enum_id = c_parser_peek_token (parser)->value;
1614 c_parser_consume_token (parser);
1615 if (c_parser_next_token_is (parser, CPP_EQ))
1617 c_parser_consume_token (parser);
1618 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1620 else
1621 enum_value = NULL_TREE;
1622 enum_decl = build_enumerator (enum_id, enum_value);
1623 TREE_CHAIN (enum_decl) = values;
1624 values = enum_decl;
1625 seen_comma = false;
1626 if (c_parser_next_token_is (parser, CPP_COMMA))
1628 seen_comma = true;
1629 c_parser_consume_token (parser);
1631 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1633 if (seen_comma && pedantic && !flag_isoc99)
1634 pedwarn ("comma at end of enumerator list");
1635 c_parser_consume_token (parser);
1636 break;
1638 if (!seen_comma)
1640 c_parser_error (parser, "expected %<,%> or %<}%>");
1641 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1642 values = error_mark_node;
1643 break;
1646 postfix_attrs = c_parser_attributes (parser);
1647 ret.spec = finish_enum (type, nreverse (values),
1648 chainon (attrs, postfix_attrs));
1649 ret.kind = ctsk_tagdef;
1650 return ret;
1652 else if (!ident)
1654 c_parser_error (parser, "expected %<{%>");
1655 ret.spec = error_mark_node;
1656 ret.kind = ctsk_tagref;
1657 return ret;
1659 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1660 /* In ISO C, enumerated types can be referred to only if already
1661 defined. */
1662 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1663 pedwarn ("ISO C forbids forward references to %<enum%> types");
1664 return ret;
1667 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1669 struct-or-union-specifier:
1670 struct-or-union attributes[opt] identifier[opt]
1671 { struct-contents } attributes[opt]
1672 struct-or-union attributes[opt] identifier
1674 struct-contents:
1675 struct-declaration-list
1677 struct-declaration-list:
1678 struct-declaration ;
1679 struct-declaration-list struct-declaration ;
1681 GNU extensions:
1683 struct-contents:
1684 empty
1685 struct-declaration
1686 struct-declaration-list struct-declaration
1688 struct-declaration-list:
1689 struct-declaration-list ;
1692 (Note that in the syntax here, unlike that in ISO C, the semicolons
1693 are included here rather than in struct-declaration, in order to
1694 describe the syntax with extra semicolons and missing semicolon at
1695 end.)
1697 Objective-C:
1699 struct-declaration-list:
1700 @defs ( class-name )
1702 (Note this does not include a trailing semicolon, but can be
1703 followed by further declarations, and gets a pedwarn-if-pedantic
1704 when followed by a semicolon.) */
1706 static struct c_typespec
1707 c_parser_struct_or_union_specifier (c_parser *parser)
1709 struct c_typespec ret;
1710 tree attrs;
1711 tree ident = NULL_TREE;
1712 enum tree_code code;
1713 switch (c_parser_peek_token (parser)->keyword)
1715 case RID_STRUCT:
1716 code = RECORD_TYPE;
1717 break;
1718 case RID_UNION:
1719 code = UNION_TYPE;
1720 break;
1721 default:
1722 gcc_unreachable ();
1724 c_parser_consume_token (parser);
1725 attrs = c_parser_attributes (parser);
1726 if (c_parser_next_token_is (parser, CPP_NAME))
1728 ident = c_parser_peek_token (parser)->value;
1729 c_parser_consume_token (parser);
1731 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1733 /* Parse a struct or union definition. Start the scope of the
1734 tag before parsing components. */
1735 tree type = start_struct (code, ident);
1736 tree postfix_attrs;
1737 /* We chain the components in reverse order, then put them in
1738 forward order at the end. Each struct-declaration may
1739 declare multiple components (comma-separated), so we must use
1740 chainon to join them, although when parsing each
1741 struct-declaration we can use TREE_CHAIN directly.
1743 The theory behind all this is that there will be more
1744 semicolon separated fields than comma separated fields, and
1745 so we'll be minimizing the number of node traversals required
1746 by chainon. */
1747 tree contents = NULL_TREE;
1748 c_parser_consume_token (parser);
1749 /* Handle the Objective-C @defs construct,
1750 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1751 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1753 tree name;
1754 gcc_assert (c_dialect_objc ());
1755 c_parser_consume_token (parser);
1756 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1757 goto end_at_defs;
1758 if (c_parser_next_token_is (parser, CPP_NAME)
1759 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1761 name = c_parser_peek_token (parser)->value;
1762 c_parser_consume_token (parser);
1764 else
1766 c_parser_error (parser, "expected class name");
1767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1768 goto end_at_defs;
1770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1771 "expected %<)%>");
1772 contents = nreverse (objc_get_class_ivars (name));
1774 end_at_defs:
1775 /* Parse the struct-declarations and semicolons. Problems with
1776 semicolons are diagnosed here; empty structures are diagnosed
1777 elsewhere. */
1778 while (true)
1780 tree decls;
1781 /* Parse any stray semicolon. */
1782 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1784 if (pedantic)
1785 pedwarn ("extra semicolon in struct or union specified");
1786 c_parser_consume_token (parser);
1787 continue;
1789 /* Stop if at the end of the struct or union contents. */
1790 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1792 c_parser_consume_token (parser);
1793 break;
1795 /* Parse some comma-separated declarations, but not the
1796 trailing semicolon if any. */
1797 decls = c_parser_struct_declaration (parser);
1798 contents = chainon (decls, contents);
1799 /* If no semicolon follows, either we have a parse error or
1800 are at the end of the struct or union and should
1801 pedwarn. */
1802 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1803 c_parser_consume_token (parser);
1804 else
1806 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1807 pedwarn ("no semicolon at end of struct or union");
1808 else
1810 c_parser_error (parser, "expected %<;%>");
1811 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1812 break;
1816 postfix_attrs = c_parser_attributes (parser);
1817 ret.spec = finish_struct (type, nreverse (contents),
1818 chainon (attrs, postfix_attrs));
1819 ret.kind = ctsk_tagdef;
1820 return ret;
1822 else if (!ident)
1824 c_parser_error (parser, "expected %<{%>");
1825 ret.spec = error_mark_node;
1826 ret.kind = ctsk_tagref;
1827 return ret;
1829 ret = parser_xref_tag (code, ident);
1830 return ret;
1833 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1834 the trailing semicolon.
1836 struct-declaration:
1837 specifier-qualifier-list struct-declarator-list
1839 specifier-qualifier-list:
1840 type-specifier specifier-qualifier-list[opt]
1841 type-qualifier specifier-qualifier-list[opt]
1842 attributes specifier-qualifier-list[opt]
1844 struct-declarator-list:
1845 struct-declarator
1846 struct-declarator-list , attributes[opt] struct-declarator
1848 struct-declarator:
1849 declarator attributes[opt]
1850 declarator[opt] : constant-expression attributes[opt]
1852 GNU extensions:
1854 struct-declaration:
1855 __extension__ struct-declaration
1856 specifier-qualifier-list
1858 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1859 of attributes where shown is a GNU extension. In GNU C, we accept
1860 any expression without commas in the syntax (assignment
1861 expressions, not just conditional expressions); assignment
1862 expressions will be diagnosed as non-constant. */
1864 static tree
1865 c_parser_struct_declaration (c_parser *parser)
1867 struct c_declspecs *specs;
1868 tree prefix_attrs;
1869 tree all_prefix_attrs;
1870 tree decls;
1871 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1873 int ext;
1874 tree decl;
1875 ext = disable_extension_diagnostics ();
1876 c_parser_consume_token (parser);
1877 decl = c_parser_struct_declaration (parser);
1878 restore_extension_diagnostics (ext);
1879 return decl;
1881 specs = build_null_declspecs ();
1882 c_parser_declspecs (parser, specs, false, true, true);
1883 if (parser->error)
1884 return NULL_TREE;
1885 if (!specs->declspecs_seen_p)
1887 c_parser_error (parser, "expected specifier-qualifier-list");
1888 return NULL_TREE;
1890 finish_declspecs (specs);
1891 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1893 tree ret;
1894 if (!specs->type_seen_p)
1896 if (pedantic)
1897 pedwarn ("ISO C forbids member declarations with no members");
1898 shadow_tag_warned (specs, pedantic);
1899 ret = NULL_TREE;
1901 else
1903 /* Support for unnamed structs or unions as members of
1904 structs or unions (which is [a] useful and [b] supports
1905 MS P-SDK). */
1906 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1908 return ret;
1910 pending_xref_error ();
1911 prefix_attrs = specs->attrs;
1912 all_prefix_attrs = prefix_attrs;
1913 specs->attrs = NULL_TREE;
1914 decls = NULL_TREE;
1915 while (true)
1917 /* Declaring one or more declarators or un-named bit-fields. */
1918 struct c_declarator *declarator;
1919 bool dummy = false;
1920 if (c_parser_next_token_is (parser, CPP_COLON))
1921 declarator = build_id_declarator (NULL_TREE);
1922 else
1923 declarator = c_parser_declarator (parser, specs->type_seen_p,
1924 C_DTR_NORMAL, &dummy);
1925 if (declarator == NULL)
1927 c_parser_skip_to_end_of_block_or_statement (parser);
1928 break;
1930 if (c_parser_next_token_is (parser, CPP_COLON)
1931 || c_parser_next_token_is (parser, CPP_COMMA)
1932 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1933 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1934 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1936 tree postfix_attrs = NULL_TREE;
1937 tree width = NULL_TREE;
1938 tree d;
1939 if (c_parser_next_token_is (parser, CPP_COLON))
1941 c_parser_consume_token (parser);
1942 width = c_parser_expr_no_commas (parser, NULL).value;
1944 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1945 postfix_attrs = c_parser_attributes (parser);
1946 d = grokfield (declarator, specs, width);
1947 decl_attributes (&d, chainon (postfix_attrs,
1948 all_prefix_attrs), 0);
1949 TREE_CHAIN (d) = decls;
1950 decls = d;
1951 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1952 all_prefix_attrs = chainon (c_parser_attributes (parser),
1953 prefix_attrs);
1954 else
1955 all_prefix_attrs = prefix_attrs;
1956 if (c_parser_next_token_is (parser, CPP_COMMA))
1957 c_parser_consume_token (parser);
1958 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
1959 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1961 /* Semicolon consumed in caller. */
1962 break;
1964 else
1966 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
1967 break;
1970 else
1972 c_parser_error (parser,
1973 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
1974 "%<__attribute__%>");
1975 break;
1978 return decls;
1981 /* Parse a typeof specifier (a GNU extension).
1983 typeof-specifier:
1984 typeof ( expression )
1985 typeof ( type-name )
1988 static struct c_typespec
1989 c_parser_typeof_specifier (c_parser *parser)
1991 struct c_typespec ret;
1992 ret.kind = ctsk_typeof;
1993 ret.spec = error_mark_node;
1994 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
1995 c_parser_consume_token (parser);
1996 skip_evaluation++;
1997 in_typeof++;
1998 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2000 skip_evaluation--;
2001 in_typeof--;
2002 return ret;
2004 if (c_parser_next_token_starts_typename (parser))
2006 struct c_type_name *type = c_parser_type_name (parser);
2007 skip_evaluation--;
2008 in_typeof--;
2009 if (type != NULL)
2011 ret.spec = groktypename (type);
2012 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2015 else
2017 struct c_expr expr = c_parser_expression (parser);
2018 skip_evaluation--;
2019 in_typeof--;
2020 if (TREE_CODE (expr.value) == COMPONENT_REF
2021 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2022 error ("%<typeof%> applied to a bit-field");
2023 ret.spec = TREE_TYPE (expr.value);
2024 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2027 return ret;
2030 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2031 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2032 be redeclared; otherwise it may not. KIND indicates which kind of
2033 declarator is wanted. Returns a valid declarator except in the
2034 case of a syntax error in which case NULL is returned. *SEEN_ID is
2035 set to true if an identifier being declared is seen; this is used
2036 to diagnose bad forms of abstract array declarators and to
2037 determine whether an identifier list is syntactically permitted.
2039 declarator:
2040 pointer[opt] direct-declarator
2042 direct-declarator:
2043 identifier
2044 ( attributes[opt] declarator )
2045 direct-declarator array-declarator
2046 direct-declarator ( parameter-type-list )
2047 direct-declarator ( identifier-list[opt] )
2049 pointer:
2050 * type-qualifier-list[opt]
2051 * type-qualifier-list[opt] pointer
2053 type-qualifier-list:
2054 type-qualifier
2055 attributes
2056 type-qualifier-list type-qualifier
2057 type-qualifier-list attributes
2059 parameter-type-list:
2060 parameter-list
2061 parameter-list , ...
2063 parameter-list:
2064 parameter-declaration
2065 parameter-list , parameter-declaration
2067 parameter-declaration:
2068 declaration-specifiers declarator attributes[opt]
2069 declaration-specifiers abstract-declarator[opt] attributes[opt]
2071 identifier-list:
2072 identifier
2073 identifier-list , identifier
2075 abstract-declarator:
2076 pointer
2077 pointer[opt] direct-abstract-declarator
2079 direct-abstract-declarator:
2080 ( attributes[opt] abstract-declarator )
2081 direct-abstract-declarator[opt] array-declarator
2082 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2084 GNU extensions:
2086 direct-declarator:
2087 direct-declarator ( parameter-forward-declarations
2088 parameter-type-list[opt] )
2090 direct-abstract-declarator:
2091 direct-abstract-declarator[opt] ( parameter-forward-declarations
2092 parameter-type-list[opt] )
2094 parameter-forward-declarations:
2095 parameter-list ;
2096 parameter-forward-declarations parameter-list ;
2098 The uses of attributes shown above are GNU extensions.
2100 Some forms of array declarator are not included in C99 in the
2101 syntax for abstract declarators; these are disallowed elsewhere.
2102 This may be a defect (DR#289).
2104 This function also accepts an omitted abstract declarator as being
2105 an abstract declarator, although not part of the formal syntax. */
2107 static struct c_declarator *
2108 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2109 bool *seen_id)
2111 /* Parse any initial pointer part. */
2112 if (c_parser_next_token_is (parser, CPP_MULT))
2114 struct c_declspecs *quals_attrs = build_null_declspecs ();
2115 struct c_declarator *inner;
2116 c_parser_consume_token (parser);
2117 c_parser_declspecs (parser, quals_attrs, false, false, true);
2118 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2119 if (inner == NULL)
2120 return NULL;
2121 else
2122 return make_pointer_declarator (quals_attrs, inner);
2124 /* Now we have a direct declarator, direct abstract declarator or
2125 nothing (which counts as a direct abstract declarator here). */
2126 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2129 /* Parse a direct declarator or direct abstract declarator; arguments
2130 as c_parser_declarator. */
2132 static struct c_declarator *
2133 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2134 bool *seen_id)
2136 /* The direct declarator must start with an identifier (possibly
2137 omitted) or a parenthesized declarator (possibly abstract). In
2138 an ordinary declarator, initial parentheses must start a
2139 parenthesized declarator. In an abstract declarator or parameter
2140 declarator, they could start a parenthesized declarator or a
2141 parameter list. To tell which, the open parenthesis and any
2142 following attributes must be read. If a declaration specifier
2143 follows, then it is a parameter list; if the specifier is a
2144 typedef name, there might be an ambiguity about redeclaring it,
2145 which is resolved in the direction of treating it as a typedef
2146 name. If a close parenthesis follows, it is also an empty
2147 parameter list, as the syntax does not permit empty abstract
2148 declarators. Otherwise, it is a parenthesised declarator (in
2149 which case the analysis may be repeated inside it, recursively).
2151 ??? There is an ambiguity in a parameter declaration "int
2152 (__attribute__((foo)) x)", where x is not a typedef name: it
2153 could be an abstract declarator for a function, or declare x with
2154 parentheses. The proper resolution of this ambiguity needs
2155 documenting. At present we follow an accident of the old
2156 parser's implementation, whereby the first parameter must have
2157 some declaration specifiers other than just attributes. Thus as
2158 a parameter declaration it is treated as a parenthesised
2159 parameter named x, and as an abstract declarator it is
2160 rejected.
2162 ??? Also following the old parser, attributes inside an empty
2163 parameter list are ignored, making it a list not yielding a
2164 prototype, rather than giving an error or making it have one
2165 parameter with implicit type int.
2167 ??? Also following the old parser, typedef names may be
2168 redeclared in declarators, but not Objective-C class names. */
2170 if (kind != C_DTR_ABSTRACT
2171 && c_parser_next_token_is (parser, CPP_NAME)
2172 && ((type_seen_p
2173 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2174 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2176 struct c_declarator *inner
2177 = build_id_declarator (c_parser_peek_token (parser)->value);
2178 *seen_id = true;
2179 inner->id_loc = c_parser_peek_token (parser)->location;
2180 c_parser_consume_token (parser);
2181 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2184 if (kind != C_DTR_NORMAL
2185 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2187 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2188 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2191 /* Either we are at the end of an abstract declarator, or we have
2192 parentheses. */
2194 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2196 tree attrs;
2197 struct c_declarator *inner;
2198 c_parser_consume_token (parser);
2199 attrs = c_parser_attributes (parser);
2200 if (kind != C_DTR_NORMAL
2201 && (c_parser_next_token_starts_declspecs (parser)
2202 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2204 struct c_arg_info *args
2205 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2206 attrs);
2207 if (args == NULL)
2208 return NULL;
2209 else
2211 inner
2212 = build_function_declarator (args,
2213 build_id_declarator (NULL_TREE));
2214 return c_parser_direct_declarator_inner (parser, *seen_id,
2215 inner);
2218 /* A parenthesized declarator. */
2219 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2220 if (inner != NULL && attrs != NULL)
2221 inner = build_attrs_declarator (attrs, inner);
2222 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2224 c_parser_consume_token (parser);
2225 if (inner == NULL)
2226 return NULL;
2227 else
2228 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2230 else
2232 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2233 "expected %<)%>");
2234 return NULL;
2237 else
2239 if (kind == C_DTR_NORMAL)
2241 c_parser_error (parser, "expected identifier or %<(%>");
2242 return NULL;
2244 else
2245 return build_id_declarator (NULL_TREE);
2249 /* Parse part of a direct declarator or direct abstract declarator,
2250 given that some (in INNER) has already been parsed; ID_PRESENT is
2251 true if an identifier is present, false for an abstract
2252 declarator. */
2254 static struct c_declarator *
2255 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2256 struct c_declarator *inner)
2258 /* Parse a sequence of array declarators and parameter lists. */
2259 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2261 struct c_declarator *declarator;
2262 struct c_declspecs *quals_attrs = build_null_declspecs ();
2263 bool static_seen;
2264 bool star_seen;
2265 tree dimen;
2266 c_parser_consume_token (parser);
2267 c_parser_declspecs (parser, quals_attrs, false, false, true);
2268 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2269 if (static_seen)
2270 c_parser_consume_token (parser);
2271 if (static_seen && !quals_attrs->declspecs_seen_p)
2272 c_parser_declspecs (parser, quals_attrs, false, false, true);
2273 if (!quals_attrs->declspecs_seen_p)
2274 quals_attrs = NULL;
2275 /* If "static" is present, there must be an array dimension.
2276 Otherwise, there may be a dimension, "*", or no
2277 dimension. */
2278 if (static_seen)
2280 star_seen = false;
2281 dimen = c_parser_expr_no_commas (parser, NULL).value;
2283 else
2285 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2287 dimen = NULL_TREE;
2288 star_seen = false;
2290 else if (c_parser_next_token_is (parser, CPP_MULT))
2292 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2294 dimen = NULL_TREE;
2295 star_seen = true;
2296 c_parser_consume_token (parser);
2298 else
2300 star_seen = false;
2301 dimen = c_parser_expr_no_commas (parser, NULL).value;
2304 else
2306 star_seen = false;
2307 dimen = c_parser_expr_no_commas (parser, NULL).value;
2310 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2311 c_parser_consume_token (parser);
2312 else
2314 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2315 "expected %<]%>");
2316 return NULL;
2318 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2319 star_seen);
2320 inner = set_array_declarator_inner (declarator, inner, !id_present);
2321 return c_parser_direct_declarator_inner (parser, id_present, inner);
2323 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2325 tree attrs;
2326 struct c_arg_info *args;
2327 c_parser_consume_token (parser);
2328 attrs = c_parser_attributes (parser);
2329 args = c_parser_parms_declarator (parser, id_present, attrs);
2330 if (args == NULL)
2331 return NULL;
2332 else
2334 inner = build_function_declarator (args, inner);
2335 return c_parser_direct_declarator_inner (parser, id_present, inner);
2338 return inner;
2341 /* Parse a parameter list or identifier list, including the closing
2342 parenthesis but not the opening one. ATTRS are the attributes at
2343 the start of the list. ID_LIST_OK is true if an identifier list is
2344 acceptable; such a list must not have attributes at the start. */
2346 static struct c_arg_info *
2347 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2349 push_scope ();
2350 declare_parm_level ();
2351 /* If the list starts with an identifier, it is an identifier list.
2352 Otherwise, it is either a prototype list or an empty list. */
2353 if (id_list_ok
2354 && !attrs
2355 && c_parser_next_token_is (parser, CPP_NAME)
2356 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2358 tree list = NULL_TREE;
2359 while (c_parser_next_token_is (parser, CPP_NAME)
2360 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2362 list = chainon (list, build_tree_list (NULL_TREE,
2363 c_parser_peek_token (parser)->value));
2364 c_parser_consume_token (parser);
2365 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2366 break;
2367 c_parser_consume_token (parser);
2368 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2370 c_parser_error (parser, "expected identifier");
2371 break;
2374 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2376 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2377 ret->parms = 0;
2378 ret->tags = 0;
2379 ret->types = list;
2380 ret->others = 0;
2381 c_parser_consume_token (parser);
2382 pop_scope ();
2383 return ret;
2385 else
2387 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2388 "expected %<)%>");
2389 pop_scope ();
2390 return NULL;
2393 else
2395 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2396 pop_scope ();
2397 return ret;
2401 /* Parse a parameter list (possibly empty), including the closing
2402 parenthesis but not the opening one. ATTRS are the attributes at
2403 the start of the list. */
2405 static struct c_arg_info *
2406 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2408 bool good_parm = false;
2409 /* ??? Following the old parser, forward parameter declarations may
2410 use abstract declarators, and if no real parameter declarations
2411 follow the forward declarations then this is not diagnosed. Also
2412 note as above that attributes are ignored as the only contents of
2413 the parentheses, or as the only contents after forward
2414 declarations. */
2415 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2417 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2418 ret->parms = 0;
2419 ret->tags = 0;
2420 ret->types = 0;
2421 ret->others = 0;
2422 c_parser_consume_token (parser);
2423 return ret;
2425 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2427 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2428 ret->parms = 0;
2429 ret->tags = 0;
2430 ret->others = 0;
2431 /* Suppress -Wold-style-definition for this case. */
2432 ret->types = error_mark_node;
2433 error ("ISO C requires a named argument before %<...%>");
2434 c_parser_consume_token (parser);
2435 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2437 c_parser_consume_token (parser);
2438 return ret;
2440 else
2442 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2443 "expected %<)%>");
2444 return NULL;
2447 /* Nonempty list of parameters, either terminated with semicolon
2448 (forward declarations; recurse) or with close parenthesis (normal
2449 function) or with ", ... )" (variadic function). */
2450 while (true)
2452 /* Parse a parameter. */
2453 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2454 attrs = NULL_TREE;
2455 if (parm != NULL)
2457 good_parm = true;
2458 push_parm_decl (parm);
2460 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2462 tree new_attrs;
2463 c_parser_consume_token (parser);
2464 new_attrs = c_parser_attributes (parser);
2465 return c_parser_parms_list_declarator (parser, new_attrs);
2467 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2469 c_parser_consume_token (parser);
2470 if (good_parm)
2471 return get_parm_info (false);
2472 else
2474 struct c_arg_info *ret
2475 = XOBNEW (&parser_obstack, struct c_arg_info);
2476 ret->parms = 0;
2477 ret->tags = 0;
2478 ret->types = 0;
2479 ret->others = 0;
2480 return ret;
2483 if (!c_parser_require (parser, CPP_COMMA,
2484 "expected %<;%>, %<,%> or %<)%>"))
2486 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2487 return NULL;
2489 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2491 c_parser_consume_token (parser);
2492 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2494 c_parser_consume_token (parser);
2495 if (good_parm)
2496 return get_parm_info (true);
2497 else
2499 struct c_arg_info *ret
2500 = XOBNEW (&parser_obstack, struct c_arg_info);
2501 ret->parms = 0;
2502 ret->tags = 0;
2503 ret->types = 0;
2504 ret->others = 0;
2505 return ret;
2508 else
2510 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2511 "expected %<)%>");
2512 return NULL;
2518 /* Parse a parameter declaration. ATTRS are the attributes at the
2519 start of the declaration if it is the first parameter. */
2521 static struct c_parm *
2522 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2524 struct c_declspecs *specs;
2525 struct c_declarator *declarator;
2526 tree prefix_attrs;
2527 tree postfix_attrs = NULL_TREE;
2528 bool dummy = false;
2529 if (!c_parser_next_token_starts_declspecs (parser))
2531 /* ??? In some Objective-C cases '...' isn't applicable so there
2532 should be a different message. */
2533 c_parser_error (parser,
2534 "expected declaration specifiers or %<...%>");
2535 c_parser_skip_to_end_of_parameter (parser);
2536 return NULL;
2538 specs = build_null_declspecs ();
2539 if (attrs)
2541 declspecs_add_attrs (specs, attrs);
2542 attrs = NULL_TREE;
2544 c_parser_declspecs (parser, specs, true, true, true);
2545 finish_declspecs (specs);
2546 pending_xref_error ();
2547 prefix_attrs = specs->attrs;
2548 specs->attrs = NULL_TREE;
2549 declarator = c_parser_declarator (parser, specs->type_seen_p,
2550 C_DTR_PARM, &dummy);
2551 if (declarator == NULL)
2553 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2554 return NULL;
2556 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2557 postfix_attrs = c_parser_attributes (parser);
2558 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2559 declarator);
2562 /* Parse a string literal in an asm expression. It should not be
2563 translated, and wide string literals are an error although
2564 permitted by the syntax. This is a GNU extension.
2566 asm-string-literal:
2567 string-literal
2569 ??? At present, following the old parser, the caller needs to have
2570 set c_lex_string_translate to 0. It would be better to follow the
2571 C++ parser rather than using the c_lex_string_translate kludge. */
2573 static tree
2574 c_parser_asm_string_literal (c_parser *parser)
2576 tree str;
2577 if (c_parser_next_token_is (parser, CPP_STRING))
2579 str = c_parser_peek_token (parser)->value;
2580 c_parser_consume_token (parser);
2582 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2584 error ("wide string literal in %<asm%>");
2585 str = build_string (1, "");
2586 c_parser_consume_token (parser);
2588 else
2590 c_parser_error (parser, "expected string literal");
2591 str = NULL_TREE;
2593 return str;
2596 /* Parse a simple asm expression. This is used in restricted
2597 contexts, where a full expression with inputs and outputs does not
2598 make sense. This is a GNU extension.
2600 simple-asm-expr:
2601 asm ( asm-string-literal )
2604 static tree
2605 c_parser_simple_asm_expr (c_parser *parser)
2607 tree str;
2608 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2609 /* ??? Follow the C++ parser rather than using the
2610 c_lex_string_translate kludge. */
2611 c_lex_string_translate = 0;
2612 c_parser_consume_token (parser);
2613 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2615 c_lex_string_translate = 1;
2616 return NULL_TREE;
2618 str = c_parser_asm_string_literal (parser);
2619 c_lex_string_translate = 1;
2620 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2622 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2623 return NULL_TREE;
2625 return str;
2628 /* Parse (possibly empty) attributes. This is a GNU extension.
2630 attributes:
2631 empty
2632 attributes attribute
2634 attribute:
2635 __attribute__ ( ( attribute-list ) )
2637 attribute-list:
2638 attrib
2639 attribute_list , attrib
2641 attrib:
2642 empty
2643 any-word
2644 any-word ( identifier )
2645 any-word ( identifier , nonempty-expr-list )
2646 any-word ( expr-list )
2648 where the "identifier" must not be declared as a type, and
2649 "any-word" may be any identifier (including one declared as a
2650 type), a reserved word storage class specifier, type specifier or
2651 type qualifier. ??? This still leaves out most reserved keywords
2652 (following the old parser), shouldn't we include them, and why not
2653 allow identifiers declared as types to start the arguments? */
2655 static tree
2656 c_parser_attributes (c_parser *parser)
2658 tree attrs = NULL_TREE;
2659 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2661 /* ??? Follow the C++ parser rather than using the
2662 c_lex_string_translate kludge. */
2663 c_lex_string_translate = 0;
2664 c_parser_consume_token (parser);
2665 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2667 c_lex_string_translate = 1;
2668 return attrs;
2670 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2672 c_lex_string_translate = 1;
2673 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2674 return attrs;
2676 /* Parse the attribute list. */
2677 while (c_parser_next_token_is (parser, CPP_COMMA)
2678 || c_parser_next_token_is (parser, CPP_NAME)
2679 || c_parser_next_token_is (parser, CPP_KEYWORD))
2681 tree attr, attr_name, attr_args;
2682 if (c_parser_next_token_is (parser, CPP_COMMA))
2684 c_parser_consume_token (parser);
2685 continue;
2687 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2689 /* ??? See comment above about what keywords are
2690 accepted here. */
2691 bool ok;
2692 switch (c_parser_peek_token (parser)->keyword)
2694 case RID_STATIC:
2695 case RID_UNSIGNED:
2696 case RID_LONG:
2697 case RID_CONST:
2698 case RID_EXTERN:
2699 case RID_REGISTER:
2700 case RID_TYPEDEF:
2701 case RID_SHORT:
2702 case RID_INLINE:
2703 case RID_VOLATILE:
2704 case RID_SIGNED:
2705 case RID_AUTO:
2706 case RID_RESTRICT:
2707 case RID_COMPLEX:
2708 case RID_THREAD:
2709 case RID_INT:
2710 case RID_CHAR:
2711 case RID_FLOAT:
2712 case RID_DOUBLE:
2713 case RID_VOID:
2714 case RID_BOOL:
2715 ok = true;
2716 break;
2717 default:
2718 ok = false;
2719 break;
2721 if (!ok)
2722 break;
2724 attr_name = c_parser_peek_token (parser)->value;
2725 c_parser_consume_token (parser);
2726 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2728 attr = build_tree_list (attr_name, NULL_TREE);
2729 attrs = chainon (attrs, attr);
2730 continue;
2732 c_parser_consume_token (parser);
2733 /* Parse the attribute contents. If they start with an
2734 identifier which is followed by a comma or close
2735 parenthesis, then the arguments start with that
2736 identifier; otherwise they are an expression list. */
2737 if (c_parser_next_token_is (parser, CPP_NAME)
2738 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2739 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2740 || (c_parser_peek_2nd_token (parser)->type
2741 == CPP_CLOSE_PAREN)))
2743 tree arg1 = c_parser_peek_token (parser)->value;
2744 c_parser_consume_token (parser);
2745 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2746 attr_args = build_tree_list (NULL_TREE, arg1);
2747 else
2749 c_parser_consume_token (parser);
2750 attr_args = tree_cons (NULL_TREE, arg1,
2751 c_parser_expr_list (parser));
2754 else
2756 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2757 attr_args = NULL_TREE;
2758 else
2759 attr_args = c_parser_expr_list (parser);
2761 attr = build_tree_list (attr_name, attr_args);
2762 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2763 c_parser_consume_token (parser);
2764 else
2766 c_lex_string_translate = 1;
2767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2768 "expected %<)%>");
2769 return attrs;
2771 attrs = chainon (attrs, attr);
2773 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2774 c_parser_consume_token (parser);
2775 else
2777 c_lex_string_translate = 1;
2778 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2779 "expected %<)%>");
2780 return attrs;
2782 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2783 c_parser_consume_token (parser);
2784 else
2786 c_lex_string_translate = 1;
2787 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2788 "expected %<)%>");
2789 return attrs;
2791 c_lex_string_translate = 1;
2793 return attrs;
2796 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2798 type-name:
2799 specifier-qualifier-list abstract-declarator[opt]
2802 static struct c_type_name *
2803 c_parser_type_name (c_parser *parser)
2805 struct c_declspecs *specs = build_null_declspecs ();
2806 struct c_declarator *declarator;
2807 struct c_type_name *ret;
2808 bool dummy = false;
2809 c_parser_declspecs (parser, specs, false, true, true);
2810 if (!specs->declspecs_seen_p)
2812 c_parser_error (parser, "expected specifier-qualifier-list");
2813 return NULL;
2815 pending_xref_error ();
2816 finish_declspecs (specs);
2817 declarator = c_parser_declarator (parser, specs->type_seen_p,
2818 C_DTR_ABSTRACT, &dummy);
2819 if (declarator == NULL)
2820 return NULL;
2821 ret = XOBNEW (&parser_obstack, struct c_type_name);
2822 ret->specs = specs;
2823 ret->declarator = declarator;
2824 return ret;
2827 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2829 initializer:
2830 assignment-expression
2831 { initializer-list }
2832 { initializer-list , }
2834 initializer-list:
2835 designation[opt] initializer
2836 initializer-list , designation[opt] initializer
2838 designation:
2839 designator-list =
2841 designator-list:
2842 designator
2843 designator-list designator
2845 designator:
2846 array-designator
2847 . identifier
2849 array-designator:
2850 [ constant-expression ]
2852 GNU extensions:
2854 initializer:
2857 designation:
2858 array-designator
2859 identifier :
2861 array-designator:
2862 [ constant-expression ... constant-expression ]
2864 Any expression without commas is accepted in the syntax for the
2865 constant-expressions, with non-constant expressions rejected later.
2867 This function is only used for top-level initializers; for nested
2868 ones, see c_parser_initval. */
2870 static struct c_expr
2871 c_parser_initializer (c_parser *parser)
2873 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2874 return c_parser_braced_init (parser, NULL_TREE, false);
2875 else
2876 return c_parser_expr_no_commas (parser, NULL);
2879 /* Parse a braced initializer list. TYPE is the type specified for a
2880 compound literal, and NULL_TREE for other initializers and for
2881 nested braced lists. NESTED_P is true for nested braced lists,
2882 false for the list of a compound literal or the list that is the
2883 top-level initializer in a declaration. */
2885 static struct c_expr
2886 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2888 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2889 c_parser_consume_token (parser);
2890 if (nested_p)
2891 push_init_level (0);
2892 else
2893 really_start_incremental_init (type);
2894 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2896 if (pedantic)
2897 pedwarn ("ISO C forbids empty initializer braces");
2899 else
2901 /* Parse a non-empty initializer list, possibly with a trailing
2902 comma. */
2903 while (true)
2905 c_parser_initelt (parser);
2906 if (parser->error)
2907 break;
2908 if (c_parser_next_token_is (parser, CPP_COMMA))
2909 c_parser_consume_token (parser);
2910 else
2911 break;
2912 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2913 break;
2916 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
2918 struct c_expr ret;
2919 ret.value = error_mark_node;
2920 ret.original_code = ERROR_MARK;
2921 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
2922 return ret;
2924 c_parser_consume_token (parser);
2925 return pop_init_level (0);
2928 /* Parse a nested initializer, including designators. */
2930 static void
2931 c_parser_initelt (c_parser *parser)
2933 /* Parse any designator or designator list. A single array
2934 designator may have the subsequent "=" omitted in GNU C, but a
2935 longer list or a structure member designator may not. */
2936 if (c_parser_next_token_is (parser, CPP_NAME)
2937 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
2939 /* Old-style structure member designator. */
2940 set_init_label (c_parser_peek_token (parser)->value);
2941 if (pedantic)
2942 pedwarn ("obsolete use of designated initializer with %<:%>");
2943 c_parser_consume_token (parser);
2944 c_parser_consume_token (parser);
2946 else
2948 /* des_seen is 0 if there have been no designators, 1 if there
2949 has been a single array designator and 2 otherwise. */
2950 int des_seen = 0;
2951 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
2952 || c_parser_next_token_is (parser, CPP_DOT))
2954 int des_prev = des_seen;
2955 if (des_seen < 2)
2956 des_seen++;
2957 if (c_parser_next_token_is (parser, CPP_DOT))
2959 des_seen = 2;
2960 c_parser_consume_token (parser);
2961 if (c_parser_next_token_is (parser, CPP_NAME))
2963 set_init_label (c_parser_peek_token (parser)->value);
2964 c_parser_consume_token (parser);
2966 else
2968 struct c_expr init;
2969 init.value = error_mark_node;
2970 init.original_code = ERROR_MARK;
2971 c_parser_error (parser, "expected identifier");
2972 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2973 process_init_element (init);
2974 return;
2977 else
2979 tree first, second;
2980 /* ??? Following the old parser, [ objc-receiver
2981 objc-message-args ] is accepted as an initializer,
2982 being distinguished from a designator by what follows
2983 the first assignment expression inside the square
2984 brackets, but after a first array designator a
2985 subsequent square bracket is for Objective-C taken to
2986 start an expression, using the obsolete form of
2987 designated initializer without '=', rather than
2988 possibly being a second level of designation: in LALR
2989 terms, the '[' is shifted rather than reducing
2990 designator to designator-list. */
2991 if (des_prev == 1 && c_dialect_objc ())
2993 des_seen = des_prev;
2994 break;
2996 if (des_prev == 0 && c_dialect_objc ())
2998 /* This might be an array designator or an
2999 Objective-C message expression. If the former,
3000 continue parsing here; if the latter, parse the
3001 remainder of the initializer given the starting
3002 primary-expression. ??? It might make sense to
3003 distinguish when des_prev == 1 as well; see
3004 previous comment. */
3005 tree rec, args;
3006 struct c_expr mexpr;
3007 c_parser_consume_token (parser);
3008 if (c_parser_peek_token (parser)->type == CPP_NAME
3009 && ((c_parser_peek_token (parser)->id_kind
3010 == C_ID_TYPENAME)
3011 || (c_parser_peek_token (parser)->id_kind
3012 == C_ID_CLASSNAME)))
3014 /* Type name receiver. */
3015 tree id = c_parser_peek_token (parser)->value;
3016 c_parser_consume_token (parser);
3017 rec = objc_get_class_reference (id);
3018 goto parse_message_args;
3020 first = c_parser_expr_no_commas (parser, NULL).value;
3021 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3022 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3023 goto array_desig_after_first;
3024 /* Expression receiver. So far only one part
3025 without commas has been parsed; there might be
3026 more of the expression. */
3027 rec = first;
3028 while (c_parser_next_token_is (parser, CPP_COMMA))
3030 tree next;
3031 c_parser_consume_token (parser);
3032 next = c_parser_expr_no_commas (parser, NULL).value;
3033 rec = build_compound_expr (rec, next);
3035 parse_message_args:
3036 /* Now parse the objc-message-args. */
3037 args = c_parser_objc_message_args (parser);
3038 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3039 "expected %<]%>");
3040 mexpr.value
3041 = objc_build_message_expr (build_tree_list (rec, args));
3042 mexpr.original_code = ERROR_MARK;
3043 /* Now parse and process the remainder of the
3044 initializer, starting with this message
3045 expression as a primary-expression. */
3046 c_parser_initval (parser, &mexpr);
3047 return;
3049 c_parser_consume_token (parser);
3050 first = c_parser_expr_no_commas (parser, NULL).value;
3051 array_desig_after_first:
3052 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3054 c_parser_consume_token (parser);
3055 second = c_parser_expr_no_commas (parser, NULL).value;
3057 else
3058 second = NULL_TREE;
3059 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3061 c_parser_consume_token (parser);
3062 set_init_index (first, second);
3063 if (pedantic && second)
3064 pedwarn ("ISO C forbids specifying range of "
3065 "elements to initialize");
3067 else
3068 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3069 "expected %<]%>");
3072 if (des_seen >= 1)
3074 if (c_parser_next_token_is (parser, CPP_EQ))
3076 if (pedantic && !flag_isoc99)
3077 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3078 c_parser_consume_token (parser);
3080 else
3082 if (des_seen == 1)
3084 if (pedantic)
3085 pedwarn ("obsolete use of designated initializer "
3086 "without %<=%>");
3088 else
3090 struct c_expr init;
3091 init.value = error_mark_node;
3092 init.original_code = ERROR_MARK;
3093 c_parser_error (parser, "expected %<=%>");
3094 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3095 process_init_element (init);
3096 return;
3101 c_parser_initval (parser, NULL);
3104 /* Parse a nested initializer; as c_parser_initializer but parses
3105 initializers within braced lists, after any designators have been
3106 applied. If AFTER is not NULL then it is an Objective-C message
3107 expression which is the primary-expression starting the
3108 initializer. */
3110 static void
3111 c_parser_initval (c_parser *parser, struct c_expr *after)
3113 struct c_expr init;
3114 gcc_assert (!after || c_dialect_objc ());
3115 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3116 init = c_parser_braced_init (parser, NULL_TREE, true);
3117 else
3118 init = c_parser_expr_no_commas (parser, after);
3119 process_init_element (init);
3122 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3123 C99 6.8.2).
3125 compound-statement:
3126 { block-item-list[opt] }
3127 { label-declarations block-item-list }
3129 block-item-list:
3130 block-item
3131 block-item-list block-item
3133 block-item:
3134 nested-declaration
3135 statement
3137 nested-declaration:
3138 declaration
3140 GNU extensions:
3142 compound-statement:
3143 { label-declarations block-item-list }
3145 nested-declaration:
3146 __extension__ nested-declaration
3147 nested-function-definition
3149 label-declarations:
3150 label-declaration
3151 label-declarations label-declaration
3153 label-declaration:
3154 __label__ identifier-list ;
3156 Allowing the mixing of declarations and code is new in C99. The
3157 GNU syntax also permits (not shown above) labels at the end of
3158 compound statements, which yield an error. We don't allow labels
3159 on declarations; this might seem like a natural extension, but
3160 there would be a conflict between attributes on the label and
3161 prefix attributes on the declaration. ??? The syntax follows the
3162 old parser in requiring something after label declarations.
3163 Although they are erroneous if the labels declared aren't defined,
3164 is it useful for the syntax to be this way? */
3166 static tree
3167 c_parser_compound_statement (c_parser *parser)
3169 tree stmt;
3170 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3171 return error_mark_node;
3172 stmt = c_begin_compound_stmt (true);
3173 c_parser_compound_statement_nostart (parser);
3174 return c_end_compound_stmt (stmt, true);
3177 /* Parse a compound statement except for the opening brace. This is
3178 used for parsing both compound statements and statement expressions
3179 (which follow different paths to handling the opening). */
3181 static void
3182 c_parser_compound_statement_nostart (c_parser *parser)
3184 bool last_stmt = false;
3185 bool last_label = false;
3186 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3188 c_parser_consume_token (parser);
3189 return;
3191 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3193 /* Read zero or more forward-declarations for labels that nested
3194 functions can jump to. */
3195 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3197 c_parser_consume_token (parser);
3198 /* Any identifiers, including those declared as type names,
3199 are OK here. */
3200 while (true)
3202 tree label;
3203 if (c_parser_next_token_is_not (parser, CPP_NAME))
3205 c_parser_error (parser, "expected identifier");
3206 break;
3208 label
3209 = declare_label (c_parser_peek_token (parser)->value);
3210 C_DECLARED_LABEL_FLAG (label) = 1;
3211 add_stmt (build_stmt (DECL_EXPR, label));
3212 c_parser_consume_token (parser);
3213 if (c_parser_next_token_is (parser, CPP_COMMA))
3214 c_parser_consume_token (parser);
3215 else
3216 break;
3218 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3220 /* ??? Locating this diagnostic on the token after the
3221 declarations end follows the old parser, but it might be
3222 better to locate it where the declarations start instead. */
3223 if (pedantic)
3224 pedwarn ("ISO C forbids label declarations");
3226 /* We must now have at least one statement, label or declaration. */
3227 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3229 c_parser_error (parser, "expected declaration or statement");
3230 c_parser_consume_token (parser);
3231 return;
3233 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3235 location_t loc = c_parser_peek_token (parser)->location;
3236 if (c_parser_next_token_is (parser, CPP_EOF))
3238 c_parser_error (parser, "expected declaration or statement");
3239 return;
3241 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3242 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3243 || (c_parser_next_token_is (parser, CPP_NAME)
3244 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3246 last_label = true;
3247 last_stmt = false;
3248 c_parser_label (parser);
3250 else if (!last_label
3251 && c_parser_next_token_starts_declspecs (parser))
3253 last_label = false;
3254 c_parser_declaration_or_fndef (parser, true, true, true, true);
3255 if (last_stmt
3256 && ((pedantic && !flag_isoc99)
3257 || warn_declaration_after_statement))
3258 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3259 &loc);
3260 last_stmt = false;
3262 else if (!last_label
3263 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3265 /* __extension__ can start a declaration, but is also an
3266 unary operator that can start an expression. Consume all
3267 but the last of a possible series of __extension__ to
3268 determine which. */
3269 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3270 && (c_parser_peek_2nd_token (parser)->keyword
3271 == RID_EXTENSION))
3272 c_parser_consume_token (parser);
3273 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3275 int ext;
3276 ext = disable_extension_diagnostics ();
3277 c_parser_consume_token (parser);
3278 last_label = false;
3279 c_parser_declaration_or_fndef (parser, true, true, true, true);
3280 /* Following the old parser, __extension__ does not
3281 disable this diagnostic. */
3282 restore_extension_diagnostics (ext);
3283 if (last_stmt
3284 && ((pedantic && !flag_isoc99)
3285 || warn_declaration_after_statement))
3286 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3287 &loc);
3288 last_stmt = false;
3290 else
3291 goto statement;
3293 else
3295 statement:
3296 last_label = false;
3297 last_stmt = true;
3298 c_parser_statement_after_labels (parser);
3301 if (last_label)
3302 error ("label at end of compound statement");
3303 c_parser_consume_token (parser);
3306 /* Parse a label (C90 6.6.1, C99 6.8.1).
3308 label:
3309 identifier : attributes[opt]
3310 case constant-expression :
3311 default :
3313 GNU extensions:
3315 label:
3316 case constant-expression ... constant-expression :
3318 The use of attributes on labels is a GNU extension. The syntax in
3319 GNU C accepts any expressions without commas, non-constant
3320 expressions being rejected later. */
3322 static void
3323 c_parser_label (c_parser *parser)
3325 location_t loc1 = c_parser_peek_token (parser)->location;
3326 tree label = NULL_TREE;
3327 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3329 tree exp1, exp2;
3330 c_parser_consume_token (parser);
3331 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3332 if (c_parser_next_token_is (parser, CPP_COLON))
3334 c_parser_consume_token (parser);
3335 label = do_case (exp1, NULL_TREE);
3337 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3339 c_parser_consume_token (parser);
3340 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3341 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3342 label = do_case (exp1, exp2);
3344 else
3345 c_parser_error (parser, "expected %<:%> or %<...%>");
3347 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3349 c_parser_consume_token (parser);
3350 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3351 label = do_case (NULL_TREE, NULL_TREE);
3353 else
3355 tree name = c_parser_peek_token (parser)->value;
3356 tree tlab;
3357 location_t loc2;
3358 tree attrs;
3359 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3360 c_parser_consume_token (parser);
3361 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3362 loc2 = c_parser_peek_token (parser)->location;
3363 c_parser_consume_token (parser);
3364 attrs = c_parser_attributes (parser);
3365 tlab = define_label (loc2, name);
3366 if (tlab)
3368 decl_attributes (&tlab, attrs, 0);
3369 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3372 if (label)
3373 SET_EXPR_LOCATION (label, loc1);
3376 /* Parse a statement (C90 6.6, C99 6.8).
3378 statement:
3379 labeled-statement
3380 compound-statement
3381 expression-statement
3382 selection-statement
3383 iteration-statement
3384 jump-statement
3386 labeled-statement:
3387 label statement
3389 expression-statement:
3390 expression[opt] ;
3392 selection-statement:
3393 if-statement
3394 switch-statement
3396 iteration-statement:
3397 while-statement
3398 do-statement
3399 for-statement
3401 jump-statement:
3402 goto identifier ;
3403 continue ;
3404 break ;
3405 return expression[opt] ;
3407 GNU extensions:
3409 statement:
3410 asm-statement
3412 jump-statement:
3413 goto * expression ;
3415 Objective-C:
3417 statement:
3418 objc-throw-statement
3419 objc-try-catch-statement
3420 objc-synchronized-statement
3422 objc-throw-statement:
3423 @throw expression ;
3424 @throw ;
3427 static void
3428 c_parser_statement (c_parser *parser)
3430 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3431 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3432 || (c_parser_next_token_is (parser, CPP_NAME)
3433 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3434 c_parser_label (parser);
3435 c_parser_statement_after_labels (parser);
3438 /* Parse a statement, other than a labeled statement. */
3440 static void
3441 c_parser_statement_after_labels (c_parser *parser)
3443 location_t loc = c_parser_peek_token (parser)->location;
3444 tree stmt = NULL_TREE;
3445 switch (c_parser_peek_token (parser)->type)
3447 case CPP_OPEN_BRACE:
3448 add_stmt (c_parser_compound_statement (parser));
3449 break;
3450 case CPP_KEYWORD:
3451 switch (c_parser_peek_token (parser)->keyword)
3453 case RID_IF:
3454 c_parser_if_statement (parser);
3455 break;
3456 case RID_SWITCH:
3457 c_parser_switch_statement (parser);
3458 break;
3459 case RID_WHILE:
3460 c_parser_while_statement (parser);
3461 break;
3462 case RID_DO:
3463 c_parser_do_statement (parser);
3464 break;
3465 case RID_FOR:
3466 c_parser_for_statement (parser);
3467 break;
3468 case RID_GOTO:
3469 c_parser_consume_token (parser);
3470 if (c_parser_next_token_is (parser, CPP_NAME))
3472 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3473 c_parser_consume_token (parser);
3475 else if (c_parser_next_token_is (parser, CPP_MULT))
3477 c_parser_consume_token (parser);
3478 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3480 else
3481 c_parser_error (parser, "expected identifier or %<*%>");
3482 goto expect_semicolon;
3483 case RID_CONTINUE:
3484 c_parser_consume_token (parser);
3485 stmt = c_finish_bc_stmt (&c_cont_label, false);
3486 goto expect_semicolon;
3487 case RID_BREAK:
3488 c_parser_consume_token (parser);
3489 stmt = c_finish_bc_stmt (&c_break_label, true);
3490 goto expect_semicolon;
3491 case RID_RETURN:
3492 c_parser_consume_token (parser);
3493 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3495 stmt = c_finish_return (NULL_TREE);
3496 c_parser_consume_token (parser);
3498 else
3500 stmt = c_finish_return (c_parser_expression (parser).value);
3501 goto expect_semicolon;
3503 break;
3504 case RID_ASM:
3505 stmt = c_parser_asm_statement (parser);
3506 break;
3507 case RID_AT_THROW:
3508 gcc_assert (c_dialect_objc ());
3509 c_parser_consume_token (parser);
3510 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3512 stmt = objc_build_throw_stmt (NULL_TREE);
3513 c_parser_consume_token (parser);
3515 else
3517 stmt
3518 = objc_build_throw_stmt (c_parser_expression (parser).value);
3519 goto expect_semicolon;
3521 break;
3522 case RID_AT_TRY:
3523 gcc_assert (c_dialect_objc ());
3524 c_parser_objc_try_catch_statement (parser);
3525 break;
3526 case RID_AT_SYNCHRONIZED:
3527 gcc_assert (c_dialect_objc ());
3528 c_parser_objc_synchronized_statement (parser);
3529 break;
3530 default:
3531 goto expr_stmt;
3533 break;
3534 case CPP_SEMICOLON:
3535 c_parser_consume_token (parser);
3536 break;
3537 case CPP_CLOSE_PAREN:
3538 case CPP_CLOSE_SQUARE:
3539 /* Avoid infinite loop in error recovery:
3540 c_parser_skip_until_found stops at a closing nesting
3541 delimiter without consuming it, but here we need to consume
3542 it to proceed further. */
3543 c_parser_error (parser, "expected statement");
3544 c_parser_consume_token (parser);
3545 break;
3546 default:
3547 expr_stmt:
3548 stmt = c_finish_expr_stmt (c_parser_expression (parser).value);
3549 expect_semicolon:
3550 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3551 break;
3553 /* Two cases cannot and do not have line numbers associated: If stmt
3554 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3555 cannot hold line numbers. But that's OK because the statement
3556 will either be changed to a MODIFY_EXPR during gimplification of
3557 the statement expr, or discarded. If stmt was compound, but
3558 without new variables, we will have skipped the creation of a
3559 BIND and will have a bare STATEMENT_LIST. But that's OK because
3560 (recursively) all of the component statements should already have
3561 line numbers assigned. ??? Can we discard no-op statements
3562 earlier? */
3563 if (stmt && EXPR_P (stmt))
3564 SET_EXPR_LOCATION (stmt, loc);
3567 /* Parse a parenthesized condition from an if, do or while statement.
3569 condition:
3570 ( expression )
3572 static tree
3573 c_parser_paren_condition (c_parser *parser)
3575 location_t loc;
3576 tree cond;
3577 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3578 return error_mark_node;
3579 loc = c_parser_peek_token (parser)->location;
3580 cond = c_objc_common_truthvalue_conversion
3581 (c_parser_expression (parser).value);
3582 if (EXPR_P (cond))
3583 SET_EXPR_LOCATION (cond, loc);
3584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3585 return cond;
3588 /* Parse a statement which is a block in C99. */
3590 static tree
3591 c_parser_c99_block_statement (c_parser *parser)
3593 tree block = c_begin_compound_stmt (flag_isoc99);
3594 c_parser_statement (parser);
3595 return c_end_compound_stmt (block, flag_isoc99);
3598 /* Parse the body of an if statement or the else half thereof. This
3599 is just parsing a statement but (a) it is a block in C99, (b) we
3600 track whether the body is an if statement for the sake of
3601 -Wparentheses warnings, (c) we handle an empty body specially for
3602 the sake of -Wextra warnings. */
3604 static tree
3605 c_parser_if_body (c_parser *parser, bool *if_p)
3607 tree block = c_begin_compound_stmt (flag_isoc99);
3608 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3609 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3610 || (c_parser_next_token_is (parser, CPP_NAME)
3611 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3612 c_parser_label (parser);
3613 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3614 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3615 add_stmt (build (NOP_EXPR, NULL_TREE, NULL_TREE));
3616 c_parser_statement_after_labels (parser);
3617 return c_end_compound_stmt (block, flag_isoc99);
3620 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3622 if-statement:
3623 if ( expression ) statement
3624 if ( expression ) statement else statement
3627 static void
3628 c_parser_if_statement (c_parser *parser)
3630 tree block;
3631 location_t loc;
3632 tree cond;
3633 bool first_if = false, second_if = false;
3634 tree first_body, second_body;
3635 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3636 c_parser_consume_token (parser);
3637 block = c_begin_compound_stmt (flag_isoc99);
3638 loc = c_parser_peek_token (parser)->location;
3639 cond = c_parser_paren_condition (parser);
3640 first_body = c_parser_if_body (parser, &first_if);
3641 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3643 c_parser_consume_token (parser);
3644 second_body = c_parser_if_body (parser, &second_if);
3646 else
3647 second_body = NULL_TREE;
3648 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3649 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3652 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3654 switch-statement:
3655 switch (expression) statement
3658 static void
3659 c_parser_switch_statement (c_parser *parser)
3661 tree block, expr, body, save_break;
3662 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3663 c_parser_consume_token (parser);
3664 block = c_begin_compound_stmt (flag_isoc99);
3665 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3667 expr = c_parser_expression (parser).value;
3668 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3670 else
3671 expr = error_mark_node;
3672 c_start_case (expr);
3673 save_break = c_break_label;
3674 c_break_label = NULL_TREE;
3675 body = c_parser_c99_block_statement (parser);
3676 c_finish_case (body);
3677 if (c_break_label)
3678 add_stmt (build (LABEL_EXPR, void_type_node, c_break_label));
3679 c_break_label = save_break;
3680 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3683 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3685 while-statement:
3686 while (expression) statement
3689 static void
3690 c_parser_while_statement (c_parser *parser)
3692 tree block, cond, body, save_break, save_cont;
3693 location_t loc;
3694 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3695 c_parser_consume_token (parser);
3696 block = c_begin_compound_stmt (flag_isoc99);
3697 loc = c_parser_peek_token (parser)->location;
3698 cond = c_parser_paren_condition (parser);
3699 save_break = c_break_label;
3700 c_break_label = NULL_TREE;
3701 save_cont = c_cont_label;
3702 c_cont_label = NULL_TREE;
3703 body = c_parser_c99_block_statement (parser);
3704 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3705 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3706 c_break_label = save_break;
3707 c_cont_label = save_cont;
3710 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3712 do-statement:
3713 do statement while ( expression ) ;
3716 static void
3717 c_parser_do_statement (c_parser *parser)
3719 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3720 location_t loc;
3721 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3722 c_parser_consume_token (parser);
3723 block = c_begin_compound_stmt (flag_isoc99);
3724 loc = c_parser_peek_token (parser)->location;
3725 save_break = c_break_label;
3726 c_break_label = NULL_TREE;
3727 save_cont = c_cont_label;
3728 c_cont_label = NULL_TREE;
3729 body = c_parser_c99_block_statement (parser);
3730 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3731 new_break = c_break_label;
3732 c_break_label = save_break;
3733 new_cont = c_cont_label;
3734 c_cont_label = save_cont;
3735 cond = c_parser_paren_condition (parser);
3736 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3737 c_parser_skip_to_end_of_block_or_statement (parser);
3738 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3739 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3742 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3744 for-statement:
3745 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3746 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3748 The form with a declaration is new in C99.
3750 ??? In accordance with the old parser, the declaration may be a
3751 nested function, which is then rejected in check_for_loop_decls,
3752 but does it make any sense for this to be included in the grammar?
3753 Note in particular that the nested function does not include a
3754 trailing ';', whereas the "declaration" production includes one.
3755 Also, can we reject bad declarations earlier and cheaper than
3756 check_for_loop_decls? */
3758 static void
3759 c_parser_for_statement (c_parser *parser)
3761 tree block, cond, incr, save_break, save_cont, body;
3762 location_t loc = UNKNOWN_LOCATION;
3763 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3764 c_parser_consume_token (parser);
3765 block = c_begin_compound_stmt (flag_isoc99);
3766 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3768 /* Parse the initialization declaration or expression. */
3769 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3771 c_parser_consume_token (parser);
3772 c_finish_expr_stmt (NULL_TREE);
3774 else if (c_parser_next_token_starts_declspecs (parser))
3776 c_parser_declaration_or_fndef (parser, true, true, true, true);
3777 check_for_loop_decls ();
3779 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3781 /* __extension__ can start a declaration, but is also an
3782 unary operator that can start an expression. Consume all
3783 but the last of a possible series of __extension__ to
3784 determine which. */
3785 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3786 && (c_parser_peek_2nd_token (parser)->keyword
3787 == RID_EXTENSION))
3788 c_parser_consume_token (parser);
3789 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3791 int ext;
3792 ext = disable_extension_diagnostics ();
3793 c_parser_consume_token (parser);
3794 c_parser_declaration_or_fndef (parser, true, true, true, true);
3795 restore_extension_diagnostics (ext);
3796 check_for_loop_decls ();
3798 else
3799 goto init_expr;
3801 else
3803 init_expr:
3804 c_finish_expr_stmt (c_parser_expression (parser).value);
3805 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3807 /* Parse the loop condition. */
3808 loc = c_parser_peek_token (parser)->location;
3809 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3811 c_parser_consume_token (parser);
3812 cond = NULL_TREE;
3814 else
3816 tree ocond = c_parser_expression (parser).value;
3817 cond = c_objc_common_truthvalue_conversion (ocond);
3818 if (EXPR_P (cond))
3819 SET_EXPR_LOCATION (cond, loc);
3820 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3822 /* Parse the increment expression. */
3823 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3824 incr = c_process_expr_stmt (NULL_TREE);
3825 else
3826 incr = c_process_expr_stmt (c_parser_expression (parser).value);
3827 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3829 else
3831 cond = error_mark_node;
3832 incr = error_mark_node;
3834 save_break = c_break_label;
3835 c_break_label = NULL_TREE;
3836 save_cont = c_cont_label;
3837 c_cont_label = NULL_TREE;
3838 body = c_parser_c99_block_statement (parser);
3839 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3840 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3841 c_break_label = save_break;
3842 c_cont_label = save_cont;
3845 /* Parse an asm statement, a GNU extension. This is a full-blown asm
3846 statement with inputs, outputs, clobbers, and volatile tag
3847 allowed.
3849 asm-statement:
3850 asm type-qualifier[opt] ( asm-argument ) ;
3852 asm-argument:
3853 asm-string-literal
3854 asm-string-literal : asm-operands[opt]
3855 asm-string-literal : asm-operands[opt] : asm-operands[opt]
3856 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3858 Qualifiers other than volatile are accepted in the syntax but
3859 warned for. */
3861 static tree
3862 c_parser_asm_statement (c_parser *parser)
3864 tree quals, str, outputs, inputs, clobbers, ret;
3865 bool simple;
3866 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3867 c_parser_consume_token (parser);
3868 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3870 quals = c_parser_peek_token (parser)->value;
3871 c_parser_consume_token (parser);
3873 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3874 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3876 warning (0, "%E qualifier ignored on asm",
3877 c_parser_peek_token (parser)->value);
3878 quals = NULL_TREE;
3879 c_parser_consume_token (parser);
3881 else
3882 quals = NULL_TREE;
3883 /* ??? Follow the C++ parser rather than using the
3884 c_lex_string_translate kludge. */
3885 c_lex_string_translate = 0;
3886 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3888 c_lex_string_translate = 1;
3889 return NULL_TREE;
3891 str = c_parser_asm_string_literal (parser);
3892 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3894 simple = true;
3895 outputs = NULL_TREE;
3896 inputs = NULL_TREE;
3897 clobbers = NULL_TREE;
3898 goto done_asm;
3900 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3902 c_lex_string_translate = 1;
3903 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3904 return NULL_TREE;
3906 simple = false;
3907 /* Parse outputs. */
3908 if (c_parser_next_token_is (parser, CPP_COLON)
3909 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3910 outputs = NULL_TREE;
3911 else
3912 outputs = c_parser_asm_operands (parser);
3913 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3915 inputs = NULL_TREE;
3916 clobbers = NULL_TREE;
3917 goto done_asm;
3919 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3921 c_lex_string_translate = 1;
3922 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3923 return NULL_TREE;
3925 /* Parse inputs. */
3926 if (c_parser_next_token_is (parser, CPP_COLON)
3927 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3928 inputs = NULL_TREE;
3929 else
3930 inputs = c_parser_asm_operands (parser);
3931 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3933 clobbers = NULL_TREE;
3934 goto done_asm;
3936 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3938 c_lex_string_translate = 1;
3939 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3940 return NULL_TREE;
3942 /* Parse clobbers. */
3943 clobbers = c_parser_asm_clobbers (parser);
3944 done_asm:
3945 c_lex_string_translate = 1;
3946 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3948 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3949 return NULL_TREE;
3951 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3952 c_parser_skip_to_end_of_block_or_statement (parser);
3953 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
3954 clobbers, simple));
3955 return ret;
3958 /* Parse asm operands, a GNU extension.
3960 asm-operands:
3961 asm-operand
3962 asm-operands , asm-operand
3964 asm-operand:
3965 asm-string-literal ( expression )
3966 [ identifier ] asm-string-literal ( expression )
3969 static tree
3970 c_parser_asm_operands (c_parser *parser)
3972 tree list = NULL_TREE;
3973 while (true)
3975 tree name, str, expr;
3976 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3978 c_parser_consume_token (parser);
3979 if (c_parser_next_token_is (parser, CPP_NAME))
3981 tree id = c_parser_peek_token (parser)->value;
3982 c_parser_consume_token (parser);
3983 name = build_string (IDENTIFIER_LENGTH (id),
3984 IDENTIFIER_POINTER (id));
3986 else
3988 c_parser_error (parser, "expected identifier");
3989 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3990 return NULL_TREE;
3992 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3993 "expected %<]%>");
3995 else
3996 name = NULL_TREE;
3997 str = c_parser_asm_string_literal (parser);
3998 if (str == NULL_TREE)
3999 return NULL_TREE;
4000 c_lex_string_translate = 1;
4001 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4003 c_lex_string_translate = 0;
4004 return NULL_TREE;
4006 expr = c_parser_expression (parser).value;
4007 c_lex_string_translate = 0;
4008 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4010 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4011 return NULL_TREE;
4013 list = chainon (list, build_tree_list (build_tree_list (name, str),
4014 expr));
4015 if (c_parser_next_token_is (parser, CPP_COMMA))
4016 c_parser_consume_token (parser);
4017 else
4018 break;
4020 return list;
4023 /* Parse asm clobbers, a GNU extension.
4025 asm-clobbers:
4026 asm-string-literal
4027 asm-clobbers , asm-string-literal
4030 static tree
4031 c_parser_asm_clobbers (c_parser *parser)
4033 tree list = NULL_TREE;
4034 while (true)
4036 tree str = c_parser_asm_string_literal (parser);
4037 if (str)
4038 list = tree_cons (NULL_TREE, str, list);
4039 else
4040 return NULL_TREE;
4041 if (c_parser_next_token_is (parser, CPP_COMMA))
4042 c_parser_consume_token (parser);
4043 else
4044 break;
4046 return list;
4049 /* Parse an expression other than a compound expression; that is, an
4050 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4051 NULL then it is an Objective-C message expression which is the
4052 primary-expression starting the expression as an initializer.
4054 assignment-expression:
4055 conditional-expression
4056 unary-expression assignment-operator assignment-expression
4058 assignment-operator: one of
4059 = *= /= %= += -= <<= >>= &= ^= |=
4061 In GNU C we accept any conditional expression on the LHS and
4062 diagnose the invalid lvalue rather than producing a syntax
4063 error. */
4065 static struct c_expr
4066 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4068 struct c_expr lhs, rhs, ret;
4069 enum tree_code code;
4070 gcc_assert (!after || c_dialect_objc ());
4071 lhs = c_parser_conditional_expression (parser, after);
4072 switch (c_parser_peek_token (parser)->type)
4074 case CPP_EQ:
4075 code = NOP_EXPR;
4076 break;
4077 case CPP_MULT_EQ:
4078 code = MULT_EXPR;
4079 break;
4080 case CPP_DIV_EQ:
4081 code = TRUNC_DIV_EXPR;
4082 break;
4083 case CPP_MOD_EQ:
4084 code = TRUNC_MOD_EXPR;
4085 break;
4086 case CPP_PLUS_EQ:
4087 code = PLUS_EXPR;
4088 break;
4089 case CPP_MINUS_EQ:
4090 code = MINUS_EXPR;
4091 break;
4092 case CPP_LSHIFT_EQ:
4093 code = LSHIFT_EXPR;
4094 break;
4095 case CPP_RSHIFT_EQ:
4096 code = RSHIFT_EXPR;
4097 break;
4098 case CPP_AND_EQ:
4099 code = BIT_AND_EXPR;
4100 break;
4101 case CPP_XOR_EQ:
4102 code = BIT_XOR_EXPR;
4103 break;
4104 case CPP_OR_EQ:
4105 code = BIT_IOR_EXPR;
4106 break;
4107 default:
4108 return lhs;
4110 c_parser_consume_token (parser);
4111 rhs = c_parser_expr_no_commas (parser, NULL);
4112 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4113 if (code == NOP_EXPR)
4114 ret.original_code = MODIFY_EXPR;
4115 else
4117 TREE_NO_WARNING (ret.value) = 1;
4118 ret.original_code = ERROR_MARK;
4120 return ret;
4123 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4124 is not NULL then it is an Objective-C message expression which is
4125 the primary-expression starting the expression as an initializer.
4127 conditional-expression:
4128 logical-OR-expression
4129 logical-OR-expression ? expression : conditional-expression
4131 GNU extensions:
4133 conditional-expression:
4134 logical-OR-expression ? : conditional-expression
4137 static struct c_expr
4138 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4140 struct c_expr cond, exp1, exp2, ret;
4141 gcc_assert (!after || c_dialect_objc ());
4142 cond = c_parser_binary_expression (parser, after);
4143 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4144 return cond;
4145 c_parser_consume_token (parser);
4146 if (c_parser_next_token_is (parser, CPP_COLON))
4148 if (pedantic)
4149 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4150 /* Make sure first operand is calculated only once. */
4151 exp1.value = save_expr (default_conversion (cond.value));
4152 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4153 skip_evaluation += cond.value == truthvalue_true_node;
4155 else
4157 cond.value
4158 = c_objc_common_truthvalue_conversion
4159 (default_conversion (cond.value));
4160 skip_evaluation += cond.value == truthvalue_false_node;
4161 exp1 = c_parser_expression (parser);
4162 skip_evaluation += ((cond.value == truthvalue_true_node)
4163 - (cond.value == truthvalue_false_node));
4165 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4167 skip_evaluation -= cond.value == truthvalue_true_node;
4168 ret.value = error_mark_node;
4169 ret.original_code = ERROR_MARK;
4170 return ret;
4172 exp2 = c_parser_conditional_expression (parser, NULL);
4173 skip_evaluation -= cond.value == truthvalue_true_node;
4174 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4175 ret.original_code = ERROR_MARK;
4176 return ret;
4179 /* Parse a binary expression; that is, a logical-OR-expression (C90
4180 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4181 an Objective-C message expression which is the primary-expression
4182 starting the expression as an initializer.
4184 multiplicative-expression:
4185 cast-expression
4186 multiplicative-expression * cast-expression
4187 multiplicative-expression / cast-expression
4188 multiplicative-expression % cast-expression
4190 additive-expression:
4191 multiplicative-expression
4192 additive-expression + multiplicative-expression
4193 additive-expression - multiplicative-expression
4195 shift-expression:
4196 additive-expression
4197 shift-expression << additive-expression
4198 shift-expression >> additive-expression
4200 relational-expression:
4201 shift-expression
4202 relational-expression < shift-expression
4203 relational-expression > shift-expression
4204 relational-expression <= shift-expression
4205 relational-expression >= shift-expression
4207 equality-expression:
4208 relational-expression
4209 equality-expression == relational-expression
4210 equality-expression != relational-expression
4212 AND-expression:
4213 equality-expression
4214 AND-expression & equality-expression
4216 exclusive-OR-expression:
4217 AND-expression
4218 exclusive-OR-expression ^ AND-expression
4220 inclusive-OR-expression:
4221 exclusive-OR-expression
4222 inclusive-OR-expression | exclusive-OR-expression
4224 logical-AND-expression:
4225 inclusive-OR-expression
4226 logical-AND-expression && inclusive-OR-expression
4228 logical-OR-expression:
4229 logical-AND-expression
4230 logical-OR-expression || logical-AND-expression
4233 static struct c_expr
4234 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4236 /* A binary expression is parsed using operator-precedence parsing,
4237 with the operands being cast expressions. All the binary
4238 operators are left-associative. Thus a binary expression is of
4239 form:
4241 E0 op1 E1 op2 E2 ...
4243 which we represent on a stack. On the stack, the precedence
4244 levels are strictly increasing. When a new operator is
4245 encountered of higher precedence than that at the top of the
4246 stack, it is pushed; its LHS is the top expression, and its RHS
4247 is everything parsed until it is popped. When a new operator is
4248 encountered with precedence less than or equal to that at the top
4249 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4250 by the result of the operation until the operator at the top of
4251 the stack has lower precedence than the new operator or there is
4252 only one element on the stack; then the top expression is the LHS
4253 of the new operator. In the case of logical AND and OR
4254 expressions, we also need to adjust skip_evaluation as
4255 appropriate when the operators are pushed and popped. */
4257 /* The precedence levels, where 0 is a dummy lowest level used for
4258 the bottom of the stack. */
4259 enum prec {
4260 PREC_NONE,
4261 PREC_LOGOR,
4262 PREC_LOGAND,
4263 PREC_BITOR,
4264 PREC_BITXOR,
4265 PREC_BITAND,
4266 PREC_EQ,
4267 PREC_REL,
4268 PREC_SHIFT,
4269 PREC_ADD,
4270 PREC_MULT,
4271 NUM_PRECS
4273 struct {
4274 /* The expression at this stack level. */
4275 struct c_expr expr;
4276 /* The precedence of the operator on its left, PREC_NONE at the
4277 bottom of the stack. */
4278 enum prec prec;
4279 /* The operation on its left. */
4280 enum tree_code op;
4281 } stack[NUM_PRECS];
4282 int sp;
4283 #define POP \
4284 do { \
4285 switch (stack[sp].op) \
4287 case TRUTH_ANDIF_EXPR: \
4288 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4289 break; \
4290 case TRUTH_ORIF_EXPR: \
4291 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4292 break; \
4293 default: \
4294 break; \
4296 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4297 stack[sp - 1].expr, \
4298 stack[sp].expr); \
4299 sp--; \
4300 } while (0)
4301 gcc_assert (!after || c_dialect_objc ());
4302 stack[0].expr = c_parser_cast_expression (parser, after);
4303 stack[0].prec = PREC_NONE;
4304 sp = 0;
4305 while (true)
4307 enum prec oprec;
4308 enum tree_code ocode;
4309 if (parser->error)
4310 goto out;
4311 switch (c_parser_peek_token (parser)->type)
4313 case CPP_MULT:
4314 oprec = PREC_MULT;
4315 ocode = MULT_EXPR;
4316 break;
4317 case CPP_DIV:
4318 oprec = PREC_MULT;
4319 ocode = TRUNC_DIV_EXPR;
4320 break;
4321 case CPP_MOD:
4322 oprec = PREC_MULT;
4323 ocode = TRUNC_MOD_EXPR;
4324 break;
4325 case CPP_PLUS:
4326 oprec = PREC_ADD;
4327 ocode = PLUS_EXPR;
4328 break;
4329 case CPP_MINUS:
4330 oprec = PREC_ADD;
4331 ocode = MINUS_EXPR;
4332 break;
4333 case CPP_LSHIFT:
4334 oprec = PREC_SHIFT;
4335 ocode = LSHIFT_EXPR;
4336 break;
4337 case CPP_RSHIFT:
4338 oprec = PREC_SHIFT;
4339 ocode = RSHIFT_EXPR;
4340 break;
4341 case CPP_LESS:
4342 oprec = PREC_REL;
4343 ocode = LT_EXPR;
4344 break;
4345 case CPP_GREATER:
4346 oprec = PREC_REL;
4347 ocode = GT_EXPR;
4348 break;
4349 case CPP_LESS_EQ:
4350 oprec = PREC_REL;
4351 ocode = LE_EXPR;
4352 break;
4353 case CPP_GREATER_EQ:
4354 oprec = PREC_REL;
4355 ocode = GE_EXPR;
4356 break;
4357 case CPP_EQ_EQ:
4358 oprec = PREC_EQ;
4359 ocode = EQ_EXPR;
4360 break;
4361 case CPP_NOT_EQ:
4362 oprec = PREC_EQ;
4363 ocode = NE_EXPR;
4364 break;
4365 case CPP_AND:
4366 oprec = PREC_BITAND;
4367 ocode = BIT_AND_EXPR;
4368 break;
4369 case CPP_XOR:
4370 oprec = PREC_BITXOR;
4371 ocode = BIT_XOR_EXPR;
4372 break;
4373 case CPP_OR:
4374 oprec = PREC_BITOR;
4375 ocode = BIT_IOR_EXPR;
4376 break;
4377 case CPP_AND_AND:
4378 oprec = PREC_LOGAND;
4379 ocode = TRUTH_ANDIF_EXPR;
4380 break;
4381 case CPP_OR_OR:
4382 oprec = PREC_LOGOR;
4383 ocode = TRUTH_ORIF_EXPR;
4384 break;
4385 default:
4386 /* Not a binary operator, so end of the binary
4387 expression. */
4388 goto out;
4390 c_parser_consume_token (parser);
4391 while (oprec <= stack[sp].prec)
4392 POP;
4393 switch (ocode)
4395 case TRUTH_ANDIF_EXPR:
4396 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4397 (default_conversion (stack[sp].expr.value));
4398 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4399 break;
4400 case TRUTH_ORIF_EXPR:
4401 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4402 (default_conversion (stack[sp].expr.value));
4403 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4404 break;
4405 default:
4406 break;
4408 sp++;
4409 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4410 stack[sp].prec = oprec;
4411 stack[sp].op = ocode;
4413 out:
4414 while (sp > 0)
4415 POP;
4416 return stack[0].expr;
4417 #undef POP
4420 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4421 NULL then it is an Objective-C message expression which is the
4422 primary-expression starting the expression as an initializer.
4424 cast-expression:
4425 unary-expression
4426 ( type-name ) unary-expression
4429 static struct c_expr
4430 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4432 gcc_assert (!after || c_dialect_objc ());
4433 if (after)
4434 return c_parser_postfix_expression_after_primary (parser, *after);
4435 /* If the expression begins with a parenthesized type name, it may
4436 be either a cast or a compound literal; we need to see whether
4437 the next character is '{' to tell the difference. If not, it is
4438 an unary expression. */
4439 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4440 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4442 struct c_type_name *type_name;
4443 struct c_expr ret;
4444 tree expr;
4445 c_parser_consume_token (parser);
4446 type_name = c_parser_type_name (parser);
4447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4448 if (type_name == NULL)
4450 ret.value = error_mark_node;
4451 ret.original_code = ERROR_MARK;
4452 return ret;
4454 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4455 return c_parser_postfix_expression_after_paren_type (parser,
4456 type_name);
4457 expr = c_parser_cast_expression (parser, NULL).value;
4458 ret.value = c_cast_expr (type_name, expr);
4459 ret.original_code = ERROR_MARK;
4460 return ret;
4462 else
4463 return c_parser_unary_expression (parser);
4466 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4468 unary-expression:
4469 postfix-expression
4470 ++ unary-expression
4471 -- unary-expression
4472 unary-operator cast-expression
4473 sizeof unary-expression
4474 sizeof ( type-name )
4476 unary-operator: one of
4477 & * + - ~ !
4479 GNU extensions:
4481 unary-expression:
4482 __alignof__ unary-expression
4483 __alignof__ ( type-name )
4484 && identifier
4486 unary-operator: one of
4487 __extension__ __real__ __imag__
4489 In addition, the GNU syntax treats ++ and -- as unary operators, so
4490 they may be applied to cast expressions with errors for non-lvalues
4491 given later. */
4493 static struct c_expr
4494 c_parser_unary_expression (c_parser *parser)
4496 int ext;
4497 struct c_expr ret;
4498 switch (c_parser_peek_token (parser)->type)
4500 case CPP_PLUS_PLUS:
4501 c_parser_consume_token (parser);
4502 return parser_build_unary_op (PREINCREMENT_EXPR,
4503 c_parser_cast_expression (parser, NULL));
4504 case CPP_MINUS_MINUS:
4505 c_parser_consume_token (parser);
4506 return parser_build_unary_op (PREDECREMENT_EXPR,
4507 c_parser_cast_expression (parser, NULL));
4508 case CPP_AND:
4509 c_parser_consume_token (parser);
4510 return parser_build_unary_op (ADDR_EXPR,
4511 c_parser_cast_expression (parser, NULL));
4512 case CPP_MULT:
4513 c_parser_consume_token (parser);
4514 ret.value
4515 = build_indirect_ref (c_parser_cast_expression (parser, NULL).value,
4516 "unary *");
4517 ret.original_code = ERROR_MARK;
4518 return ret;
4519 case CPP_PLUS:
4520 c_parser_consume_token (parser);
4521 if (!c_dialect_objc () && warn_traditional && !in_system_header)
4522 warning (0, "traditional C rejects the unary plus operator");
4523 return parser_build_unary_op (CONVERT_EXPR,
4524 c_parser_cast_expression (parser, NULL));
4525 case CPP_MINUS:
4526 c_parser_consume_token (parser);
4527 return parser_build_unary_op (NEGATE_EXPR,
4528 c_parser_cast_expression (parser, NULL));
4529 case CPP_COMPL:
4530 c_parser_consume_token (parser);
4531 return parser_build_unary_op (BIT_NOT_EXPR,
4532 c_parser_cast_expression (parser, NULL));
4533 case CPP_NOT:
4534 c_parser_consume_token (parser);
4535 return parser_build_unary_op (TRUTH_NOT_EXPR,
4536 c_parser_cast_expression (parser, NULL));
4537 case CPP_AND_AND:
4538 /* Refer to the address of a label as a pointer. */
4539 c_parser_consume_token (parser);
4540 if (c_parser_next_token_is (parser, CPP_NAME))
4542 ret.value = finish_label_address_expr
4543 (c_parser_peek_token (parser)->value);
4544 c_parser_consume_token (parser);
4546 else
4548 c_parser_error (parser, "expected identifier");
4549 ret.value = error_mark_node;
4551 ret.original_code = ERROR_MARK;
4552 return ret;
4553 case CPP_KEYWORD:
4554 switch (c_parser_peek_token (parser)->keyword)
4556 case RID_SIZEOF:
4557 return c_parser_sizeof_expression (parser);
4558 case RID_ALIGNOF:
4559 return c_parser_alignof_expression (parser);
4560 case RID_EXTENSION:
4561 c_parser_consume_token (parser);
4562 ext = disable_extension_diagnostics ();
4563 ret = c_parser_cast_expression (parser, NULL);
4564 restore_extension_diagnostics (ext);
4565 return ret;
4566 case RID_REALPART:
4567 c_parser_consume_token (parser);
4568 return parser_build_unary_op (REALPART_EXPR,
4569 c_parser_cast_expression (parser,
4570 NULL));
4571 case RID_IMAGPART:
4572 c_parser_consume_token (parser);
4573 return parser_build_unary_op (IMAGPART_EXPR,
4574 c_parser_cast_expression (parser,
4575 NULL));
4576 default:
4577 return c_parser_postfix_expression (parser);
4579 default:
4580 return c_parser_postfix_expression (parser);
4584 /* Parse a sizeof expression. */
4586 static struct c_expr
4587 c_parser_sizeof_expression (c_parser *parser)
4589 struct c_expr expr;
4590 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4591 c_parser_consume_token (parser);
4592 skip_evaluation++;
4593 in_sizeof++;
4594 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4595 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4597 /* Either sizeof ( type-name ) or sizeof unary-expression
4598 starting with a compound literal. */
4599 struct c_type_name *type_name;
4600 c_parser_consume_token (parser);
4601 type_name = c_parser_type_name (parser);
4602 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4603 if (type_name == NULL)
4605 struct c_expr ret;
4606 skip_evaluation--;
4607 in_sizeof--;
4608 ret.value = error_mark_node;
4609 ret.original_code = ERROR_MARK;
4610 return ret;
4612 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4614 expr = c_parser_postfix_expression_after_paren_type (parser,
4615 type_name);
4616 goto sizeof_expr;
4618 /* sizeof ( type-name ). */
4619 skip_evaluation--;
4620 in_sizeof--;
4621 return c_expr_sizeof_type (type_name);
4623 else
4625 expr = c_parser_unary_expression (parser);
4626 sizeof_expr:
4627 skip_evaluation--;
4628 in_sizeof--;
4629 if (TREE_CODE (expr.value) == COMPONENT_REF
4630 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4631 error ("%<sizeof%> applied to a bit-field");
4632 return c_expr_sizeof_expr (expr);
4636 /* Parse an alignof expression. */
4638 static struct c_expr
4639 c_parser_alignof_expression (c_parser *parser)
4641 struct c_expr expr;
4642 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4643 c_parser_consume_token (parser);
4644 skip_evaluation++;
4645 in_alignof++;
4646 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4647 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4649 /* Either __alignof__ ( type-name ) or __alignof__
4650 unary-expression starting with a compound literal. */
4651 struct c_type_name *type_name;
4652 struct c_expr ret;
4653 c_parser_consume_token (parser);
4654 type_name = c_parser_type_name (parser);
4655 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4656 if (type_name == NULL)
4658 struct c_expr ret;
4659 skip_evaluation--;
4660 in_alignof--;
4661 ret.value = error_mark_node;
4662 ret.original_code = ERROR_MARK;
4663 return ret;
4665 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4667 expr = c_parser_postfix_expression_after_paren_type (parser,
4668 type_name);
4669 goto alignof_expr;
4671 /* alignof ( type-name ). */
4672 skip_evaluation--;
4673 in_alignof--;
4674 ret.value = c_alignof (groktypename (type_name));
4675 ret.original_code = ERROR_MARK;
4676 return ret;
4678 else
4680 struct c_expr ret;
4681 expr = c_parser_unary_expression (parser);
4682 alignof_expr:
4683 skip_evaluation--;
4684 in_alignof--;
4685 ret.value = c_alignof_expr (expr.value);
4686 ret.original_code = ERROR_MARK;
4687 return ret;
4691 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4693 postfix-expression:
4694 primary-expression
4695 postfix-expression [ expression ]
4696 postfix-expression ( argument-expression-list[opt] )
4697 postfix-expression . identifier
4698 postfix-expression -> identifier
4699 postfix-expression ++
4700 postfix-expression --
4701 ( type-name ) { initializer-list }
4702 ( type-name ) { initializer-list , }
4704 argument-expression-list:
4705 argument-expression
4706 argument-expression-list , argument-expression
4708 primary-expression:
4709 identifier
4710 constant
4711 string-literal
4712 ( expression )
4714 GNU extensions:
4716 primary-expression:
4717 __func__
4718 (treated as a keyword in GNU C)
4719 __FUNCTION__
4720 __PRETTY_FUNCTION__
4721 ( compound-statement )
4722 __builtin_va_arg ( assignment-expression , type-name )
4723 __builtin_offsetof ( type-name , offsetof-member-designator )
4724 __builtin_choose_expr ( assignment-expression ,
4725 assignment-expression ,
4726 assignment-expression )
4727 __builtin_types_compatible_p ( type-name , type-name )
4729 offsetof-member-designator:
4730 identifier
4731 offsetof-member-designator . identifier
4732 offsetof-member-designator [ expression ]
4734 Objective-C:
4736 primary-expression:
4737 [ objc-receiver objc-message-args ]
4738 @selector ( objc-selector-arg )
4739 @protocol ( identifier )
4740 @encode ( type-name )
4741 objc-string-literal
4744 static struct c_expr
4745 c_parser_postfix_expression (c_parser *parser)
4747 struct c_expr expr, e1, e2, e3;
4748 struct c_type_name *t1, *t2;
4749 switch (c_parser_peek_token (parser)->type)
4751 case CPP_NUMBER:
4752 case CPP_CHAR:
4753 case CPP_WCHAR:
4754 expr.value = c_parser_peek_token (parser)->value;
4755 expr.original_code = ERROR_MARK;
4756 c_parser_consume_token (parser);
4757 break;
4758 case CPP_STRING:
4759 case CPP_WSTRING:
4760 expr.value = c_parser_peek_token (parser)->value;
4761 expr.original_code = STRING_CST;
4762 c_parser_consume_token (parser);
4763 break;
4764 case CPP_OBJC_STRING:
4765 gcc_assert (c_dialect_objc ());
4766 expr.value
4767 = objc_build_string_object (c_parser_peek_token (parser)->value);
4768 expr.original_code = ERROR_MARK;
4769 c_parser_consume_token (parser);
4770 break;
4771 case CPP_NAME:
4772 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4774 c_parser_error (parser, "expected expression");
4775 expr.value = error_mark_node;
4776 expr.original_code = ERROR_MARK;
4777 break;
4780 tree id = c_parser_peek_token (parser)->value;
4781 location_t loc = c_parser_peek_token (parser)->location;
4782 c_parser_consume_token (parser);
4783 expr.value = build_external_ref (id,
4784 (c_parser_peek_token (parser)->type
4785 == CPP_OPEN_PAREN), loc);
4786 expr.original_code = ERROR_MARK;
4788 break;
4789 case CPP_OPEN_PAREN:
4790 /* A parenthesized expression, statement expression or compound
4791 literal. */
4792 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4794 /* A statement expression. */
4795 tree stmt;
4796 c_parser_consume_token (parser);
4797 c_parser_consume_token (parser);
4798 if (cur_stmt_list == NULL)
4800 error ("braced-group within expression allowed "
4801 "only inside a function");
4802 parser->error = true;
4803 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4805 expr.value = error_mark_node;
4806 expr.original_code = ERROR_MARK;
4807 break;
4809 stmt = c_begin_stmt_expr ();
4810 c_parser_compound_statement_nostart (parser);
4811 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4812 "expected %<)%>");
4813 if (pedantic)
4814 pedwarn ("ISO C forbids braced-groups within expressions");
4815 expr.value = c_finish_stmt_expr (stmt);
4816 expr.original_code = ERROR_MARK;
4818 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4820 /* A compound literal. ??? Can we actually get here rather
4821 than going directly to
4822 c_parser_postfix_expression_after_paren_type from
4823 elsewhere? */
4824 struct c_type_name *type_name;
4825 c_parser_consume_token (parser);
4826 type_name = c_parser_type_name (parser);
4827 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4828 "expected %<)%>");
4829 if (type_name == NULL)
4831 expr.value = error_mark_node;
4832 expr.original_code = ERROR_MARK;
4834 else
4835 expr = c_parser_postfix_expression_after_paren_type (parser,
4836 type_name);
4838 else
4840 /* A parenthesized expression. */
4841 c_parser_consume_token (parser);
4842 expr = c_parser_expression (parser);
4843 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4844 TREE_NO_WARNING (expr.value) = 1;
4845 expr.original_code = ERROR_MARK;
4846 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4847 "expected %<)%>");
4849 break;
4850 case CPP_KEYWORD:
4851 switch (c_parser_peek_token (parser)->keyword)
4853 case RID_FUNCTION_NAME:
4854 case RID_PRETTY_FUNCTION_NAME:
4855 case RID_C99_FUNCTION_NAME:
4856 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4857 c_parser_peek_token (parser)->value);
4858 expr.original_code = ERROR_MARK;
4859 c_parser_consume_token (parser);
4860 break;
4861 case RID_VA_ARG:
4862 c_parser_consume_token (parser);
4863 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4865 expr.value = error_mark_node;
4866 expr.original_code = ERROR_MARK;
4867 break;
4869 e1 = c_parser_expr_no_commas (parser, NULL);
4870 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4872 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4873 expr.value = error_mark_node;
4874 expr.original_code = ERROR_MARK;
4875 break;
4877 t1 = c_parser_type_name (parser);
4878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4879 "expected %<)%>");
4880 if (t1 == NULL)
4882 expr.value = error_mark_node;
4883 expr.original_code = ERROR_MARK;
4885 else
4887 expr.value = build_va_arg (e1.value, groktypename (t1));
4888 expr.original_code = ERROR_MARK;
4890 break;
4891 case RID_OFFSETOF:
4892 c_parser_consume_token (parser);
4893 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4895 expr.value = error_mark_node;
4896 expr.original_code = ERROR_MARK;
4897 break;
4899 t1 = c_parser_type_name (parser);
4900 if (t1 == NULL)
4902 expr.value = error_mark_node;
4903 expr.original_code = ERROR_MARK;
4904 break;
4906 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4908 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4909 expr.value = error_mark_node;
4910 expr.original_code = ERROR_MARK;
4911 break;
4914 tree type = groktypename (t1);
4915 tree offsetof_ref;
4916 if (type == error_mark_node)
4917 offsetof_ref = error_mark_node;
4918 else
4919 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
4920 /* Parse the second argument to __builtin_offsetof. We
4921 must have one identifier, and beyond that we want to
4922 accept sub structure and sub array references. */
4923 if (c_parser_next_token_is (parser, CPP_NAME))
4925 offsetof_ref = build_component_ref
4926 (offsetof_ref, c_parser_peek_token (parser)->value);
4927 c_parser_consume_token (parser);
4928 while (c_parser_next_token_is (parser, CPP_DOT)
4929 || c_parser_next_token_is (parser,
4930 CPP_OPEN_SQUARE))
4932 if (c_parser_next_token_is (parser, CPP_DOT))
4934 c_parser_consume_token (parser);
4935 if (c_parser_next_token_is_not (parser,
4936 CPP_NAME))
4938 c_parser_error (parser, "expected identifier");
4939 break;
4941 offsetof_ref = build_component_ref
4942 (offsetof_ref,
4943 c_parser_peek_token (parser)->value);
4944 c_parser_consume_token (parser);
4946 else
4948 tree idx;
4949 c_parser_consume_token (parser);
4950 idx = c_parser_expression (parser).value;
4951 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4952 "expected %<]%>");
4953 offsetof_ref = build_array_ref (offsetof_ref, idx);
4957 else
4958 c_parser_error (parser, "expected identifier");
4959 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4960 "expected %<)%>");
4961 expr.value = fold_offsetof (offsetof_ref);
4962 expr.original_code = ERROR_MARK;
4964 break;
4965 case RID_CHOOSE_EXPR:
4966 c_parser_consume_token (parser);
4967 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4969 expr.value = error_mark_node;
4970 expr.original_code = ERROR_MARK;
4971 break;
4973 e1 = c_parser_expr_no_commas (parser, NULL);
4974 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4977 expr.value = error_mark_node;
4978 expr.original_code = ERROR_MARK;
4979 break;
4981 e2 = c_parser_expr_no_commas (parser, NULL);
4982 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4985 expr.value = error_mark_node;
4986 expr.original_code = ERROR_MARK;
4987 break;
4989 e3 = c_parser_expr_no_commas (parser, NULL);
4990 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4991 "expected %<)%>");
4993 tree c;
4995 c = fold (e1.value);
4996 if (TREE_CODE (c) != INTEGER_CST)
4997 error ("first argument to %<__builtin_choose_expr%> not"
4998 " a constant");
4999 expr = integer_zerop (c) ? e3 : e2;
5001 break;
5002 case RID_TYPES_COMPATIBLE_P:
5003 c_parser_consume_token (parser);
5004 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5006 expr.value = error_mark_node;
5007 expr.original_code = ERROR_MARK;
5008 break;
5010 t1 = c_parser_type_name (parser);
5011 if (t1 == NULL)
5013 expr.value = error_mark_node;
5014 expr.original_code = ERROR_MARK;
5015 break;
5017 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5019 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5020 expr.value = error_mark_node;
5021 expr.original_code = ERROR_MARK;
5022 break;
5024 t2 = c_parser_type_name (parser);
5025 if (t2 == NULL)
5027 expr.value = error_mark_node;
5028 expr.original_code = ERROR_MARK;
5029 break;
5031 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5032 "expected %<)%>");
5034 tree e1, e2;
5036 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5037 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5039 expr.value = comptypes (e1, e2)
5040 ? build_int_cst (NULL_TREE, 1)
5041 : build_int_cst (NULL_TREE, 0);
5042 expr.original_code = ERROR_MARK;
5044 break;
5045 case RID_AT_SELECTOR:
5046 gcc_assert (c_dialect_objc ());
5047 c_parser_consume_token (parser);
5048 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5050 expr.value = error_mark_node;
5051 expr.original_code = ERROR_MARK;
5052 break;
5055 tree sel = c_parser_objc_selector_arg (parser);
5056 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5057 "expected %<)%>");
5058 expr.value = objc_build_selector_expr (sel);
5059 expr.original_code = ERROR_MARK;
5061 break;
5062 case RID_AT_PROTOCOL:
5063 gcc_assert (c_dialect_objc ());
5064 c_parser_consume_token (parser);
5065 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5067 expr.value = error_mark_node;
5068 expr.original_code = ERROR_MARK;
5069 break;
5071 if (c_parser_next_token_is_not (parser, CPP_NAME))
5073 c_parser_error (parser, "expected identifier");
5074 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5075 expr.value = error_mark_node;
5076 expr.original_code = ERROR_MARK;
5077 break;
5080 tree id = c_parser_peek_token (parser)->value;
5081 c_parser_consume_token (parser);
5082 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5083 "expected %<)%>");
5084 expr.value = objc_build_protocol_expr (id);
5085 expr.original_code = ERROR_MARK;
5087 break;
5088 case RID_AT_ENCODE:
5089 /* Extension to support C-structures in the archiver. */
5090 gcc_assert (c_dialect_objc ());
5091 c_parser_consume_token (parser);
5092 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5094 expr.value = error_mark_node;
5095 expr.original_code = ERROR_MARK;
5096 break;
5098 t1 = c_parser_type_name (parser);
5099 if (t1 == NULL)
5101 expr.value = error_mark_node;
5102 expr.original_code = ERROR_MARK;
5103 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5104 break;
5106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5107 "expected %<)%>");
5109 tree type = groktypename (t1);
5110 expr.value = objc_build_encode_expr (type);
5111 expr.original_code = ERROR_MARK;
5113 break;
5114 default:
5115 c_parser_error (parser, "expected expression");
5116 expr.value = error_mark_node;
5117 expr.original_code = ERROR_MARK;
5118 break;
5120 break;
5121 case CPP_OPEN_SQUARE:
5122 if (c_dialect_objc ())
5124 tree receiver, args;
5125 c_parser_consume_token (parser);
5126 receiver = c_parser_objc_receiver (parser);
5127 args = c_parser_objc_message_args (parser);
5128 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5129 "expected %<]%>");
5130 expr.value = objc_build_message_expr (build_tree_list (receiver,
5131 args));
5132 expr.original_code = ERROR_MARK;
5133 break;
5135 /* Else fall through to report error. */
5136 default:
5137 c_parser_error (parser, "expected expression");
5138 expr.value = error_mark_node;
5139 expr.original_code = ERROR_MARK;
5140 break;
5142 return c_parser_postfix_expression_after_primary (parser, expr);
5145 /* Parse a postfix expression after a parenthesized type name: the
5146 brace-enclosed initializer of a compound literal, possibly followed
5147 by some postfix operators. This is separate because it is not
5148 possible to tell until after the type name whether a cast
5149 expression has a cast or a compound literal, or whether the operand
5150 of sizeof is a parenthesized type name or starts with a compound
5151 literal. */
5153 static struct c_expr
5154 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5155 struct c_type_name *type_name)
5157 tree type;
5158 struct c_expr init;
5159 struct c_expr expr;
5160 start_init (NULL_TREE, NULL, 0);
5161 type = groktypename (type_name);
5162 if (C_TYPE_VARIABLE_SIZE (type))
5164 error ("compound literal has variable size");
5165 type = error_mark_node;
5167 init = c_parser_braced_init (parser, type, false);
5168 finish_init ();
5169 maybe_warn_string_init (type, init);
5171 if (pedantic && !flag_isoc99)
5172 pedwarn ("ISO C90 forbids compound literals");
5173 expr.value = build_compound_literal (type, init.value);
5174 expr.original_code = ERROR_MARK;
5175 return c_parser_postfix_expression_after_primary (parser, expr);
5178 /* Parse a postfix expression after the initial primary or compound
5179 literal; that is, parse a series of postfix operators. */
5181 static struct c_expr
5182 c_parser_postfix_expression_after_primary (c_parser *parser,
5183 struct c_expr expr)
5185 tree ident, idx, exprlist;
5186 while (true)
5188 switch (c_parser_peek_token (parser)->type)
5190 case CPP_OPEN_SQUARE:
5191 /* Array reference. */
5192 c_parser_consume_token (parser);
5193 idx = c_parser_expression (parser).value;
5194 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5195 "expected %<]%>");
5196 expr.value = build_array_ref (expr.value, idx);
5197 expr.original_code = ERROR_MARK;
5198 break;
5199 case CPP_OPEN_PAREN:
5200 /* Function call. */
5201 c_parser_consume_token (parser);
5202 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5203 exprlist = NULL_TREE;
5204 else
5205 exprlist = c_parser_expr_list (parser);
5206 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5207 "expected %<)%>");
5208 expr.value = build_function_call (expr.value, exprlist);
5209 expr.original_code = ERROR_MARK;
5210 break;
5211 case CPP_DOT:
5212 /* Structure element reference. */
5213 c_parser_consume_token (parser);
5214 if (c_parser_next_token_is (parser, CPP_NAME))
5215 ident = c_parser_peek_token (parser)->value;
5216 else
5218 c_parser_error (parser, "expected identifier");
5219 expr.value = error_mark_node;
5220 expr.original_code = ERROR_MARK;
5221 return expr;
5223 c_parser_consume_token (parser);
5224 expr.value = build_component_ref (expr.value, ident);
5225 expr.original_code = ERROR_MARK;
5226 break;
5227 case CPP_DEREF:
5228 /* Structure element reference. */
5229 c_parser_consume_token (parser);
5230 if (c_parser_next_token_is (parser, CPP_NAME))
5231 ident = c_parser_peek_token (parser)->value;
5232 else
5234 c_parser_error (parser, "expected identifier");
5235 expr.value = error_mark_node;
5236 expr.original_code = ERROR_MARK;
5237 return expr;
5239 c_parser_consume_token (parser);
5240 expr.value = build_component_ref (build_indirect_ref (expr.value,
5241 "->"), ident);
5242 expr.original_code = ERROR_MARK;
5243 break;
5244 case CPP_PLUS_PLUS:
5245 /* Postincrement. */
5246 c_parser_consume_token (parser);
5247 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5248 expr.original_code = ERROR_MARK;
5249 break;
5250 case CPP_MINUS_MINUS:
5251 /* Postdecrement. */
5252 c_parser_consume_token (parser);
5253 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5254 expr.original_code = ERROR_MARK;
5255 break;
5256 default:
5257 return expr;
5262 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5264 expression:
5265 assignment-expression
5266 expression , assignment-expression
5269 static struct c_expr
5270 c_parser_expression (c_parser *parser)
5272 struct c_expr expr;
5273 expr = c_parser_expr_no_commas (parser, NULL);
5274 while (c_parser_next_token_is (parser, CPP_COMMA))
5276 struct c_expr next;
5277 c_parser_consume_token (parser);
5278 next = c_parser_expr_no_commas (parser, NULL);
5279 expr.value = build_compound_expr (expr.value, next.value);
5280 expr.original_code = COMPOUND_EXPR;
5282 return expr;
5285 /* Parse a non-empty list of expressions.
5287 nonempty-expr-list:
5288 assignment-expression
5289 nonempty-expr-list , assignment-expression
5292 static tree
5293 c_parser_expr_list (c_parser *parser)
5295 struct c_expr expr;
5296 tree ret;
5297 expr = c_parser_expr_no_commas (parser, NULL);
5298 ret = build_tree_list (NULL_TREE, expr.value);
5299 while (c_parser_next_token_is (parser, CPP_COMMA))
5301 c_parser_consume_token (parser);
5302 expr = c_parser_expr_no_commas (parser, NULL);
5303 ret = chainon (ret, build_tree_list (NULL_TREE, expr.value));
5305 return ret;
5309 /* Parse Objective-C-specific constructs. */
5311 /* Parse an objc-class-definition.
5313 objc-class-definition:
5314 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5315 objc-class-instance-variables[opt] objc-methodprotolist @end
5316 @implementation identifier objc-superclass[opt]
5317 objc-class-instance-variables[opt]
5318 @interface identifier ( identifier ) objc-protocol-refs[opt]
5319 objc-methodprotolist @end
5320 @implementation identifier ( identifier )
5322 objc-superclass:
5323 : identifier
5325 "@interface identifier (" must start "@interface identifier (
5326 identifier ) ...": objc-methodprotolist in the first production may
5327 not start with a parenthesised identifier as a declarator of a data
5328 definition with no declaration specifiers if the objc-superclass,
5329 objc-protocol-refs and objc-class-instance-variables are omitted. */
5331 static void
5332 c_parser_objc_class_definition (c_parser *parser)
5334 bool iface_p;
5335 tree id1;
5336 tree superclass;
5337 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5338 iface_p = true;
5339 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5340 iface_p = false;
5341 else
5342 gcc_unreachable ();
5343 c_parser_consume_token (parser);
5344 if (c_parser_next_token_is_not (parser, CPP_NAME))
5346 c_parser_error (parser, "expected identifier");
5347 return;
5349 id1 = c_parser_peek_token (parser)->value;
5350 c_parser_consume_token (parser);
5351 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5353 tree id2;
5354 tree proto = NULL_TREE;
5355 c_parser_consume_token (parser);
5356 if (c_parser_next_token_is_not (parser, CPP_NAME))
5358 c_parser_error (parser, "expected identifier");
5359 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5360 return;
5362 id2 = c_parser_peek_token (parser)->value;
5363 c_parser_consume_token (parser);
5364 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5365 if (!iface_p)
5367 objc_start_category_implementation (id1, id2);
5368 return;
5370 if (c_parser_next_token_is (parser, CPP_LESS))
5371 proto = c_parser_objc_protocol_refs (parser);
5372 objc_start_category_interface (id1, id2, proto);
5373 c_parser_objc_methodprotolist (parser);
5374 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5375 objc_finish_interface ();
5376 return;
5378 if (c_parser_next_token_is (parser, CPP_COLON))
5380 c_parser_consume_token (parser);
5381 if (c_parser_next_token_is_not (parser, CPP_NAME))
5383 c_parser_error (parser, "expected identifier");
5384 return;
5386 superclass = c_parser_peek_token (parser)->value;
5387 c_parser_consume_token (parser);
5389 else
5390 superclass = NULL_TREE;
5391 if (iface_p)
5393 tree proto = NULL_TREE;
5394 if (c_parser_next_token_is (parser, CPP_LESS))
5395 proto = c_parser_objc_protocol_refs (parser);
5396 objc_start_class_interface (id1, superclass, proto);
5398 else
5399 objc_start_class_implementation (id1, superclass);
5400 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5401 c_parser_objc_class_instance_variables (parser);
5402 if (iface_p)
5404 objc_continue_interface ();
5405 c_parser_objc_methodprotolist (parser);
5406 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5407 objc_finish_interface ();
5409 else
5411 objc_continue_implementation ();
5412 return;
5416 /* Parse objc-class-instance-variables.
5418 objc-class-instance-variables:
5419 { objc-instance-variable-decl-list[opt] }
5421 objc-instance-variable-decl-list:
5422 objc-visibility-spec
5423 objc-instance-variable-decl ;
5425 objc-instance-variable-decl-list objc-visibility-spec
5426 objc-instance-variable-decl-list objc-instance-variable-decl ;
5427 objc-instance-variable-decl-list ;
5429 objc-visibility-spec:
5430 @private
5431 @protected
5432 @public
5434 objc-instance-variable-decl:
5435 struct-declaration
5438 static void
5439 c_parser_objc_class_instance_variables (c_parser *parser)
5441 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5442 c_parser_consume_token (parser);
5443 while (c_parser_next_token_is_not (parser, CPP_EOF))
5445 tree decls;
5446 /* Parse any stray semicolon. */
5447 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5449 if (pedantic)
5450 pedwarn ("extra semicolon in struct or union specified");
5451 c_parser_consume_token (parser);
5452 continue;
5454 /* Stop if at the end of the instance variables. */
5455 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5457 c_parser_consume_token (parser);
5458 break;
5460 /* Parse any objc-visibility-spec. */
5461 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5463 c_parser_consume_token (parser);
5464 objc_set_visibility (2);
5465 continue;
5467 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5469 c_parser_consume_token (parser);
5470 objc_set_visibility (0);
5471 continue;
5473 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5475 c_parser_consume_token (parser);
5476 objc_set_visibility (1);
5477 continue;
5479 /* Parse some comma-separated declarations. */
5480 decls = c_parser_struct_declaration (parser);
5482 /* Comma-separated instance variables are chained together in
5483 reverse order; add them one by one. */
5484 tree ivar = nreverse (decls);
5485 for (; ivar; ivar = TREE_CHAIN (ivar))
5486 objc_add_instance_variable (copy_node (ivar));
5488 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5492 /* Parse an objc-class-declaration.
5494 objc-class-declaration:
5495 @class identifier-list ;
5498 static void
5499 c_parser_objc_class_declaration (c_parser *parser)
5501 tree list = NULL_TREE;
5502 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5503 c_parser_consume_token (parser);
5504 /* Any identifiers, including those declared as type names, are OK
5505 here. */
5506 while (true)
5508 tree id;
5509 if (c_parser_next_token_is_not (parser, CPP_NAME))
5511 c_parser_error (parser, "expected identifier");
5512 break;
5514 id = c_parser_peek_token (parser)->value;
5515 list = chainon (list, build_tree_list (NULL_TREE, id));
5516 c_parser_consume_token (parser);
5517 if (c_parser_next_token_is (parser, CPP_COMMA))
5518 c_parser_consume_token (parser);
5519 else
5520 break;
5522 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5523 objc_declare_class (list);
5526 /* Parse an objc-alias-declaration.
5528 objc-alias-declaration:
5529 @compatibility_alias identifier identifier ;
5532 static void
5533 c_parser_objc_alias_declaration (c_parser *parser)
5535 tree id1, id2;
5536 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5537 c_parser_consume_token (parser);
5538 if (c_parser_next_token_is_not (parser, CPP_NAME))
5540 c_parser_error (parser, "expected identifier");
5541 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5542 return;
5544 id1 = c_parser_peek_token (parser)->value;
5545 c_parser_consume_token (parser);
5546 if (c_parser_next_token_is_not (parser, CPP_NAME))
5548 c_parser_error (parser, "expected identifier");
5549 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5550 return;
5552 id2 = c_parser_peek_token (parser)->value;
5553 c_parser_consume_token (parser);
5554 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5555 objc_declare_alias (id1, id2);
5558 /* Parse an objc-protocol-definition.
5560 objc-protocol-definition:
5561 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5562 @protocol identifier-list ;
5564 "@protocol identifier ;" should be resolved as "@protocol
5565 identifier-list ;": objc-methodprotolist may not start with a
5566 semicolon in the first alternative if objc-protocol-refs are
5567 omitted. */
5569 static void
5570 c_parser_objc_protocol_definition (c_parser *parser)
5572 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5573 c_parser_consume_token (parser);
5574 if (c_parser_next_token_is_not (parser, CPP_NAME))
5576 c_parser_error (parser, "expected identifier");
5577 return;
5579 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5580 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5582 tree list = NULL_TREE;
5583 /* Any identifiers, including those declared as type names, are
5584 OK here. */
5585 while (true)
5587 tree id;
5588 if (c_parser_next_token_is_not (parser, CPP_NAME))
5590 c_parser_error (parser, "expected identifier");
5591 break;
5593 id = c_parser_peek_token (parser)->value;
5594 list = chainon (list, build_tree_list (NULL_TREE, id));
5595 c_parser_consume_token (parser);
5596 if (c_parser_next_token_is (parser, CPP_COMMA))
5597 c_parser_consume_token (parser);
5598 else
5599 break;
5601 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5602 objc_declare_protocols (list);
5604 else
5606 tree id = c_parser_peek_token (parser)->value;
5607 tree proto = NULL_TREE;
5608 c_parser_consume_token (parser);
5609 if (c_parser_next_token_is (parser, CPP_LESS))
5610 proto = c_parser_objc_protocol_refs (parser);
5611 objc_pq_context = 1;
5612 objc_start_protocol (id, proto);
5613 c_parser_objc_methodprotolist (parser);
5614 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5615 objc_pq_context = 0;
5616 objc_finish_interface ();
5620 /* Parse an objc-method-type.
5622 objc-method-type:
5627 static enum tree_code
5628 c_parser_objc_method_type (c_parser *parser)
5630 switch (c_parser_peek_token (parser)->type)
5632 case CPP_PLUS:
5633 c_parser_consume_token (parser);
5634 return PLUS_EXPR;
5635 case CPP_MINUS:
5636 c_parser_consume_token (parser);
5637 return MINUS_EXPR;
5638 default:
5639 gcc_unreachable ();
5643 /* Parse an objc-method-definition.
5645 objc-method-definition:
5646 objc-method-type objc-method-decl ;[opt] compound-statement
5649 static void
5650 c_parser_objc_method_definition (c_parser *parser)
5652 enum tree_code type = c_parser_objc_method_type (parser);
5653 tree decl;
5654 objc_set_method_type (type);
5655 objc_pq_context = 1;
5656 decl = c_parser_objc_method_decl (parser);
5657 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5659 c_parser_consume_token (parser);
5660 if (pedantic)
5661 pedwarn ("extra semicolon in method definition specified");
5663 objc_pq_context = 0;
5664 objc_start_method_definition (decl);
5665 add_stmt (c_parser_compound_statement (parser));
5666 objc_finish_method_definition (current_function_decl);
5669 /* Parse an objc-methodprotolist.
5671 objc-methodprotolist:
5672 empty
5673 objc-methodprotolist objc-methodproto
5674 objc-methodprotolist declaration
5675 objc-methodprotolist ;
5677 The declaration is a data definition, which may be missing
5678 declaration specifiers under the same rules and diagnostics as
5679 other data definitions outside functions, and the stray semicolon
5680 is diagnosed the same way as a stray semicolon outside a
5681 function. */
5683 static void
5684 c_parser_objc_methodprotolist (c_parser *parser)
5686 while (true)
5688 /* The list is terminated by @end. */
5689 switch (c_parser_peek_token (parser)->type)
5691 case CPP_SEMICOLON:
5692 if (pedantic)
5693 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5694 c_parser_consume_token (parser);
5695 break;
5696 case CPP_PLUS:
5697 case CPP_MINUS:
5698 c_parser_objc_methodproto (parser);
5699 break;
5700 case CPP_EOF:
5701 return;
5702 default:
5703 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5704 return;
5705 c_parser_declaration_or_fndef (parser, false, true, false, true);
5706 break;
5711 /* Parse an objc-methodproto.
5713 objc-methodproto:
5714 objc-method-type objc-method-decl ;
5717 static void
5718 c_parser_objc_methodproto (c_parser *parser)
5720 enum tree_code type = c_parser_objc_method_type (parser);
5721 tree decl;
5722 objc_set_method_type (type);
5723 /* Remember protocol qualifiers in prototypes. */
5724 objc_pq_context = 1;
5725 decl = c_parser_objc_method_decl (parser);
5726 /* Forget protocol qualifiers here. */
5727 objc_pq_context = 0;
5728 objc_add_method_declaration (decl);
5729 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5732 /* Parse an objc-method-decl.
5734 objc-method-decl:
5735 ( objc-type-name ) objc-selector
5736 objc-selector
5737 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5738 objc-keyword-selector objc-optparmlist
5740 objc-keyword-selector:
5741 objc-keyword-decl
5742 objc-keyword-selector objc-keyword-decl
5744 objc-keyword-decl:
5745 objc-selector : ( objc-type-name ) identifier
5746 objc-selector : identifier
5747 : ( objc-type-name ) identifier
5748 : identifier
5750 objc-optparmlist:
5751 objc-optparms objc-optellipsis
5753 objc-optparms:
5754 empty
5755 objc-opt-parms , parameter-declaration
5757 objc-optellipsis:
5758 empty
5759 , ...
5762 static tree
5763 c_parser_objc_method_decl (c_parser *parser)
5765 tree type = NULL_TREE;
5766 tree sel;
5767 tree parms = NULL_TREE;
5768 bool ellipsis = false;
5770 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5772 c_parser_consume_token (parser);
5773 type = c_parser_objc_type_name (parser);
5774 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5776 sel = c_parser_objc_selector (parser);
5777 /* If there is no selector, or a colon follows, we have an
5778 objc-keyword-selector. If there is a selector, and a colon does
5779 not follow, that selector ends the objc-method-decl. */
5780 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5782 tree tsel = sel;
5783 tree list = NULL_TREE;
5784 while (true)
5786 tree atype = NULL_TREE, id, keyworddecl;
5787 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5788 break;
5789 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5791 c_parser_consume_token (parser);
5792 atype = c_parser_objc_type_name (parser);
5793 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5794 "expected %<)%>");
5796 if (c_parser_next_token_is_not (parser, CPP_NAME))
5798 c_parser_error (parser, "expected identifier");
5799 return error_mark_node;
5801 id = c_parser_peek_token (parser)->value;
5802 c_parser_consume_token (parser);
5803 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5804 list = chainon (list, keyworddecl);
5805 tsel = c_parser_objc_selector (parser);
5806 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5807 break;
5809 /* Parse the optional parameter list. Optional Objective-C
5810 method parameters follow the C syntax, and may include '...'
5811 to denote a variable number of arguments. */
5812 parms = make_node (TREE_LIST);
5813 while (c_parser_next_token_is (parser, CPP_COMMA))
5815 struct c_parm *parm;
5816 c_parser_consume_token (parser);
5817 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5819 ellipsis = true;
5820 c_parser_consume_token (parser);
5821 break;
5823 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5824 if (parm == NULL)
5825 break;
5826 parms = chainon (parms,
5827 build_tree_list (NULL_TREE, grokparm (parm)));
5829 sel = list;
5831 return objc_build_method_signature (type, sel, parms, ellipsis);
5834 /* Parse an objc-type-name.
5836 objc-type-name:
5837 objc-type-qualifiers[opt] type-name
5838 objc-type-qualifiers[opt]
5840 objc-type-qualifiers:
5841 objc-type-qualifier
5842 objc-type-qualifiers objc-type-qualifier
5844 objc-type-qualifier: one of
5845 in out inout bycopy byref oneway
5848 static tree
5849 c_parser_objc_type_name (c_parser *parser)
5851 tree quals = NULL_TREE;
5852 struct c_type_name *typename = NULL;
5853 tree type = NULL_TREE;
5854 while (true)
5856 c_token *token = c_parser_peek_token (parser);
5857 if (token->type == CPP_KEYWORD
5858 && (token->keyword == RID_IN
5859 || token->keyword == RID_OUT
5860 || token->keyword == RID_INOUT
5861 || token->keyword == RID_BYCOPY
5862 || token->keyword == RID_BYREF
5863 || token->keyword == RID_ONEWAY))
5865 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
5866 c_parser_consume_token (parser);
5868 else
5869 break;
5871 if (c_parser_next_token_starts_typename (parser))
5872 typename = c_parser_type_name (parser);
5873 if (typename)
5874 type = groktypename (typename);
5875 return build_tree_list (quals, type);
5878 /* Parse objc-protocol-refs.
5880 objc-protocol-refs:
5881 < identifier-list >
5884 static tree
5885 c_parser_objc_protocol_refs (c_parser *parser)
5887 tree list = NULL_TREE;
5888 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
5889 c_parser_consume_token (parser);
5890 /* Any identifiers, including those declared as type names, are OK
5891 here. */
5892 while (true)
5894 tree id;
5895 if (c_parser_next_token_is_not (parser, CPP_NAME))
5897 c_parser_error (parser, "expected identifier");
5898 break;
5900 id = c_parser_peek_token (parser)->value;
5901 list = chainon (list, build_tree_list (NULL_TREE, id));
5902 c_parser_consume_token (parser);
5903 if (c_parser_next_token_is (parser, CPP_COMMA))
5904 c_parser_consume_token (parser);
5905 else
5906 break;
5908 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
5909 return list;
5912 /* Parse an objc-try-catch-statement.
5914 objc-try-catch-statement:
5915 @try compound-statement objc-catch-list[opt]
5916 @try compound-statement objc-catch-list[opt] @finally compound-statement
5918 objc-catch-list:
5919 @catch ( parameter-declaration ) compound-statement
5920 objc-catch-list @catch ( parameter-declaration ) compound-statement
5923 static void
5924 c_parser_objc_try_catch_statement (c_parser *parser)
5926 location_t loc;
5927 tree stmt;
5928 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
5929 c_parser_consume_token (parser);
5930 loc = c_parser_peek_token (parser)->location;
5931 stmt = c_parser_compound_statement (parser);
5932 objc_begin_try_stmt (loc, stmt);
5933 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
5935 struct c_parm *parm;
5936 c_parser_consume_token (parser);
5937 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5938 break;
5939 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5940 if (parm == NULL)
5942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5943 break;
5945 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5946 objc_begin_catch_clause (grokparm (parm));
5947 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5948 c_parser_compound_statement_nostart (parser);
5949 objc_finish_catch_clause ();
5951 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
5953 location_t finloc;
5954 tree finstmt;
5955 c_parser_consume_token (parser);
5956 finloc = c_parser_peek_token (parser)->location;
5957 finstmt = c_parser_compound_statement (parser);
5958 objc_build_finally_clause (finloc, finstmt);
5960 objc_finish_try_stmt ();
5963 /* Parse an objc-synchronized-statement.
5965 objc-synchronized-statement:
5966 @synchronized ( expression ) compound-statement
5969 static void
5970 c_parser_objc_synchronized_statement (c_parser *parser)
5972 location_t loc;
5973 tree expr, stmt;
5974 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
5975 c_parser_consume_token (parser);
5976 loc = c_parser_peek_token (parser)->location;
5977 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5979 expr = c_parser_expression (parser).value;
5980 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5982 else
5983 expr = error_mark_node;
5984 stmt = c_parser_compound_statement (parser);
5985 objc_build_synchronized (loc, expr, stmt);
5988 /* Parse an objc-selector; return NULL_TREE without an error if the
5989 next token is not an objc-selector.
5991 objc-selector:
5992 identifier
5993 one of
5994 enum struct union if else while do for switch case default
5995 break continue return goto asm sizeof typeof __alignof
5996 unsigned long const short volatile signed restrict _Complex
5997 in out inout bycopy byref oneway int char float double void _Bool
5999 ??? Why this selection of keywords but not, for example, storage
6000 class specifiers? */
6002 static tree
6003 c_parser_objc_selector (c_parser *parser)
6005 c_token *token = c_parser_peek_token (parser);
6006 tree value = token->value;
6007 if (token->type == CPP_NAME)
6009 c_parser_consume_token (parser);
6010 return value;
6012 if (token->type != CPP_KEYWORD)
6013 return NULL_TREE;
6014 switch (token->keyword)
6016 case RID_ENUM:
6017 case RID_STRUCT:
6018 case RID_UNION:
6019 case RID_IF:
6020 case RID_ELSE:
6021 case RID_WHILE:
6022 case RID_DO:
6023 case RID_FOR:
6024 case RID_SWITCH:
6025 case RID_CASE:
6026 case RID_DEFAULT:
6027 case RID_BREAK:
6028 case RID_CONTINUE:
6029 case RID_RETURN:
6030 case RID_GOTO:
6031 case RID_ASM:
6032 case RID_SIZEOF:
6033 case RID_TYPEOF:
6034 case RID_ALIGNOF:
6035 case RID_UNSIGNED:
6036 case RID_LONG:
6037 case RID_CONST:
6038 case RID_SHORT:
6039 case RID_VOLATILE:
6040 case RID_SIGNED:
6041 case RID_RESTRICT:
6042 case RID_COMPLEX:
6043 case RID_IN:
6044 case RID_OUT:
6045 case RID_INOUT:
6046 case RID_BYCOPY:
6047 case RID_BYREF:
6048 case RID_ONEWAY:
6049 case RID_INT:
6050 case RID_CHAR:
6051 case RID_FLOAT:
6052 case RID_DOUBLE:
6053 case RID_VOID:
6054 case RID_BOOL:
6055 c_parser_consume_token (parser);
6056 return value;
6057 default:
6058 return NULL_TREE;
6062 /* Parse an objc-selector-arg.
6064 objc-selector-arg:
6065 objc-selector
6066 objc-keywordname-list
6068 objc-keywordname-list:
6069 objc-keywordname
6070 objc-keywordname-list objc-keywordname
6072 objc-keywordname:
6073 objc-selector :
6077 static tree
6078 c_parser_objc_selector_arg (c_parser *parser)
6080 tree sel = c_parser_objc_selector (parser);
6081 tree list = NULL_TREE;
6082 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6083 return sel;
6084 while (true)
6086 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6087 return list;
6088 list = chainon (list, build_tree_list (sel, NULL_TREE));
6089 sel = c_parser_objc_selector (parser);
6090 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6091 break;
6093 return list;
6096 /* Parse an objc-receiver.
6098 objc-receiver:
6099 expression
6100 class-name
6101 type-name
6104 static tree
6105 c_parser_objc_receiver (c_parser *parser)
6107 if (c_parser_peek_token (parser)->type == CPP_NAME
6108 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6109 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6111 tree id = c_parser_peek_token (parser)->value;
6112 c_parser_consume_token (parser);
6113 return objc_get_class_reference (id);
6115 return c_parser_expression (parser).value;
6118 /* Parse objc-message-args.
6120 objc-message-args:
6121 objc-selector
6122 objc-keywordarg-list
6124 objc-keywordarg-list:
6125 objc-keywordarg
6126 objc-keywordarg-list objc-keywordarg
6128 objc-keywordarg:
6129 objc-selector : objc-keywordexpr
6130 : objc-keywordexpr
6133 static tree
6134 c_parser_objc_message_args (c_parser *parser)
6136 tree sel = c_parser_objc_selector (parser);
6137 tree list = NULL_TREE;
6138 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6139 return sel;
6140 while (true)
6142 tree keywordexpr;
6143 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6144 return list;
6145 keywordexpr = c_parser_objc_keywordexpr (parser);
6146 list = chainon (list, build_tree_list (sel, keywordexpr));
6147 sel = c_parser_objc_selector (parser);
6148 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6149 break;
6151 return list;
6154 /* Parse an objc-keywordexpr.
6156 objc-keywordexpr:
6157 nonempty-expr-list
6160 static tree
6161 c_parser_objc_keywordexpr (c_parser *parser)
6163 tree list = c_parser_expr_list (parser);
6164 if (TREE_CHAIN (list) == NULL_TREE)
6166 /* Just return the expression, remove a level of
6167 indirection. */
6168 return TREE_VALUE (list);
6170 else
6172 /* We have a comma expression, we will collapse later. */
6173 return list;
6178 /* The actual parser and external interface. ??? Does this need to be
6179 garbage-collected? */
6181 static GTY (()) c_parser *the_parser;
6183 /* Parse a single source file. */
6185 void
6186 c_parse_file (void)
6188 the_parser = c_parser_new ();
6189 c_parser_translation_unit (the_parser);
6190 the_parser = NULL;
6193 #include "gt-c-parser.h"