PR target/18668
[official-gcc.git] / gcc / c-parser.c
blobfa62475e943fb46565d7ef72d200dce066c3c88a
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 /* The number of token types, including C-specific ones. */
243 #define N_C_TTYPES ((int) (CPP_KEYWORD + 1))
245 /* More information about the type of a CPP_NAME token. */
246 typedef enum c_id_kind {
247 /* An ordinary identifier. */
248 C_ID_ID,
249 /* An identifier declared as a typedef name. */
250 C_ID_TYPENAME,
251 /* An identifier declared as an Objective-C class name. */
252 C_ID_CLASSNAME,
253 /* Not an identifier. */
254 C_ID_NONE
255 } c_id_kind;
257 /* A single C token after string literal concatenation and conversion
258 of preprocessing tokens to tokens. */
259 typedef struct c_token GTY (())
261 /* The kind of token. */
262 ENUM_BITFIELD (cpp_ttype) type : 8;
263 /* If this token is a CPP_NAME, this value indicates whether also
264 declared as some kind of type. Otherwise, it is C_ID_NONE. */
265 ENUM_BITFIELD (c_id_kind) id_kind : 8;
266 /* If this token is a keyword, this value indicates which keyword.
267 Otherwise, this value is RID_MAX. */
268 ENUM_BITFIELD (rid) keyword : 8;
269 /* True if this token is from a system header. */
270 BOOL_BITFIELD in_system_header : 1;
271 /* The value associated with this token, if any. */
272 tree value;
273 /* The location at which this token was found. */
274 location_t location;
275 } c_token;
277 /* A parser structure recording information about the state and
278 context of parsing. Includes lexer information with up to two
279 tokens of look-ahead; more are not needed for C. */
280 typedef struct c_parser GTY(())
282 /* The look-ahead tokens. */
283 c_token tokens[2];
284 /* How many look-ahead tokens are available (0, 1 or 2). */
285 short tokens_avail;
286 /* True if a syntax error is being recovered from; false otherwise.
287 c_parser_error sets this flag. It should clear this flag when
288 enough tokens have been consumed to recover from the error. */
289 BOOL_BITFIELD error : 1;
290 } c_parser;
292 /* Read in and lex a single token, storing it in *TOKEN. */
294 static void
295 c_lex_one_token (c_token *token)
297 timevar_push (TV_LEX);
298 token->type = c_lex_with_flags (&token->value, &token->location, NULL);
299 token->in_system_header = in_system_header;
300 switch (token->type)
302 case CPP_NAME:
303 token->id_kind = C_ID_NONE;
304 token->keyword = RID_MAX;
306 tree decl;
308 int objc_force_identifier = objc_need_raw_identifier;
309 OBJC_NEED_RAW_IDENTIFIER (0);
311 if (C_IS_RESERVED_WORD (token->value))
313 enum rid rid_code = C_RID_CODE (token->value);
315 if (c_dialect_objc ())
317 if (!OBJC_IS_AT_KEYWORD (rid_code)
318 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context))
320 /* Return the canonical spelling for this keyword. */
321 token->value = ridpointers[(int) rid_code];
322 token->type = CPP_KEYWORD;
323 token->keyword = rid_code;
324 break;
327 else
329 /* Return the canonical spelling for this keyword. */
330 token->value = ridpointers[(int) rid_code];
331 token->type = CPP_KEYWORD;
332 token->keyword = rid_code;
333 break;
337 decl = lookup_name (token->value);
338 if (decl)
340 if (TREE_CODE (decl) == TYPE_DECL)
342 token->id_kind = C_ID_TYPENAME;
343 break;
346 else if (c_dialect_objc ())
348 tree objc_interface_decl = objc_is_class_name (token->value);
349 /* Objective-C class names are in the same namespace as
350 variables and typedefs, and hence are shadowed by local
351 declarations. */
352 if (objc_interface_decl
353 && (global_bindings_p ()
354 || (!objc_force_identifier && !decl)))
356 token->value = objc_interface_decl;
357 token->id_kind = C_ID_CLASSNAME;
358 break;
362 token->id_kind = C_ID_ID;
363 break;
364 case CPP_AT_NAME:
365 /* This only happens in Objective-C; it must be a keyword. */
366 token->type = CPP_KEYWORD;
367 token->id_kind = C_ID_NONE;
368 token->keyword = C_RID_CODE (token->value);
369 break;
370 case CPP_COLON:
371 case CPP_COMMA:
372 case CPP_CLOSE_PAREN:
373 case CPP_SEMICOLON:
374 /* These tokens may affect the interpretation of any identifiers
375 following, if doing Objective-C. */
376 OBJC_NEED_RAW_IDENTIFIER (0);
377 token->id_kind = C_ID_NONE;
378 token->keyword = RID_MAX;
379 break;
380 default:
381 token->id_kind = C_ID_NONE;
382 token->keyword = RID_MAX;
383 break;
385 timevar_pop (TV_LEX);
388 /* Return a pointer to the next token from PARSER, reading it in if
389 necessary. */
391 static inline c_token *
392 c_parser_peek_token (c_parser *parser)
394 if (parser->tokens_avail == 0)
396 c_lex_one_token (&parser->tokens[0]);
397 parser->tokens_avail = 1;
399 return &parser->tokens[0];
402 /* Return true if the next token from PARSER has the indicated
403 TYPE. */
405 static inline bool
406 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
408 return c_parser_peek_token (parser)->type == type;
411 /* Return true if the next token from PARSER does not have the
412 indicated TYPE. */
414 static inline bool
415 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
417 return !c_parser_next_token_is (parser, type);
420 /* Return true if the next token from PARSER is the indicated
421 KEYWORD. */
423 static inline bool
424 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
426 c_token *token;
428 /* Peek at the next token. */
429 token = c_parser_peek_token (parser);
430 /* Check to see if it is the indicated keyword. */
431 return token->keyword == keyword;
434 /* Return true if TOKEN can start a type name,
435 false otherwise. */
436 static bool
437 c_token_starts_typename (c_token *token)
439 switch (token->type)
441 case CPP_NAME:
442 switch (token->id_kind)
444 case C_ID_ID:
445 return false;
446 case C_ID_TYPENAME:
447 return true;
448 case C_ID_CLASSNAME:
449 gcc_assert (c_dialect_objc ());
450 return true;
451 default:
452 gcc_unreachable ();
454 case CPP_KEYWORD:
455 switch (token->keyword)
457 case RID_UNSIGNED:
458 case RID_LONG:
459 case RID_SHORT:
460 case RID_SIGNED:
461 case RID_COMPLEX:
462 case RID_INT:
463 case RID_CHAR:
464 case RID_FLOAT:
465 case RID_DOUBLE:
466 case RID_VOID:
467 case RID_BOOL:
468 case RID_ENUM:
469 case RID_STRUCT:
470 case RID_UNION:
471 case RID_TYPEOF:
472 case RID_CONST:
473 case RID_VOLATILE:
474 case RID_RESTRICT:
475 case RID_ATTRIBUTE:
476 return true;
477 default:
478 return false;
480 case CPP_LESS:
481 if (c_dialect_objc ())
482 return true;
483 return false;
484 default:
485 return false;
489 /* Return true if the next token from PARSER can start a type name,
490 false otherwise. */
491 static inline bool
492 c_parser_next_token_starts_typename (c_parser *parser)
494 c_token *token = c_parser_peek_token (parser);
495 return c_token_starts_typename (token);
498 /* Return true if TOKEN can start declaration specifiers, false
499 otherwise. */
500 static bool
501 c_token_starts_declspecs (c_token *token)
503 switch (token->type)
505 case CPP_NAME:
506 switch (token->id_kind)
508 case C_ID_ID:
509 return false;
510 case C_ID_TYPENAME:
511 return true;
512 case C_ID_CLASSNAME:
513 gcc_assert (c_dialect_objc ());
514 return true;
515 default:
516 gcc_unreachable ();
518 case CPP_KEYWORD:
519 switch (token->keyword)
521 case RID_STATIC:
522 case RID_EXTERN:
523 case RID_REGISTER:
524 case RID_TYPEDEF:
525 case RID_INLINE:
526 case RID_AUTO:
527 case RID_THREAD:
528 case RID_UNSIGNED:
529 case RID_LONG:
530 case RID_SHORT:
531 case RID_SIGNED:
532 case RID_COMPLEX:
533 case RID_INT:
534 case RID_CHAR:
535 case RID_FLOAT:
536 case RID_DOUBLE:
537 case RID_VOID:
538 case RID_BOOL:
539 case RID_ENUM:
540 case RID_STRUCT:
541 case RID_UNION:
542 case RID_TYPEOF:
543 case RID_CONST:
544 case RID_VOLATILE:
545 case RID_RESTRICT:
546 case RID_ATTRIBUTE:
547 return true;
548 default:
549 return false;
551 case CPP_LESS:
552 if (c_dialect_objc ())
553 return true;
554 return false;
555 default:
556 return false;
560 /* Return true if the next token from PARSER can start declaration
561 specifiers, false otherwise. */
562 static inline bool
563 c_parser_next_token_starts_declspecs (c_parser *parser)
565 c_token *token = c_parser_peek_token (parser);
566 return c_token_starts_declspecs (token);
569 /* Return a pointer to the next-but-one token from PARSER, reading it
570 in if necessary. The next token is already read in. */
572 static c_token *
573 c_parser_peek_2nd_token (c_parser *parser)
575 if (parser->tokens_avail >= 2)
576 return &parser->tokens[1];
577 gcc_assert (parser->tokens_avail == 1);
578 gcc_assert (parser->tokens[0].type != CPP_EOF);
579 c_lex_one_token (&parser->tokens[1]);
580 parser->tokens_avail = 2;
581 return &parser->tokens[1];
584 /* Consume the next token from PARSER. */
586 static void
587 c_parser_consume_token (c_parser *parser)
589 if (parser->tokens_avail == 2)
590 parser->tokens[0] = parser->tokens[1];
591 else
593 gcc_assert (parser->tokens_avail == 1);
594 gcc_assert (parser->tokens[0].type != CPP_EOF);
596 parser->tokens_avail--;
599 /* Update the globals input_location and in_system_header from
600 TOKEN. */
601 static inline void
602 c_parser_set_source_position_from_token (c_token *token)
604 if (token->type != CPP_EOF)
606 input_location = token->location;
607 in_system_header = token->in_system_header;
611 /* Allocate a new parser. */
613 static c_parser *
614 c_parser_new (void)
616 /* Use local storage to lex the first token because loading a PCH
617 file may cause garbage collection. */
618 c_parser tparser;
619 c_parser *ret;
620 memset (&tparser, 0, sizeof tparser);
621 c_lex_one_token (&tparser.tokens[0]);
622 tparser.tokens_avail = 1;
623 ret = GGC_NEW (c_parser);
624 memcpy (ret, &tparser, sizeof tparser);
625 return ret;
628 /* Issue a diagnostic of the form
629 FILE:LINE: MESSAGE before TOKEN
630 where TOKEN is the next token in the input stream of PARSER.
631 MESSAGE (specified by the caller) is usually of the form "expected
632 OTHER-TOKEN".
634 Do not issue a diagnostic if still recovering from an error.
636 ??? This is taken from the C++ parser, but building up messages in
637 this way is not i18n-friendly and some other approach should be
638 used. */
640 static void
641 c_parser_error (c_parser *parser, const char *msgid)
643 c_token *token = c_parser_peek_token (parser);
644 if (parser->error)
645 return;
646 parser->error = true;
647 if (!msgid)
648 return;
649 /* This diagnostic makes more sense if it is tagged to the line of
650 the token we just peeked at. */
651 c_parser_set_source_position_from_token (token);
652 c_parse_error (msgid,
653 /* Because c_parse_error does not understand
654 CPP_KEYWORD, keywords are treated like
655 identifiers. */
656 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
657 token->value);
660 /* If the next token is of the indicated TYPE, consume it. Otherwise,
661 issue the error MSGID. If MSGID is NULL then a message has already
662 been produced and no message will be produced this time. Returns
663 true if found, false otherwise. */
665 static bool
666 c_parser_require (c_parser *parser,
667 enum cpp_ttype type,
668 const char *msgid)
670 if (c_parser_next_token_is (parser, type))
672 c_parser_consume_token (parser);
673 return true;
675 else
677 c_parser_error (parser, msgid);
678 return false;
682 /* If the next token is the indicated keyword, consume it. Otherwise,
683 issue the error MSGID. Returns true if found, false otherwise. */
685 static bool
686 c_parser_require_keyword (c_parser *parser,
687 enum rid keyword,
688 const char *msgid)
690 if (c_parser_next_token_is_keyword (parser, keyword))
692 c_parser_consume_token (parser);
693 return true;
695 else
697 c_parser_error (parser, msgid);
698 return false;
702 /* Like c_parser_require, except that tokens will be skipped until the
703 desired token is found. An error message is still produced if the
704 next token is not as expected. If MSGID is NULL then a message has
705 already been produced and no message will be produced this
706 time. */
708 static void
709 c_parser_skip_until_found (c_parser *parser,
710 enum cpp_ttype type,
711 const char *msgid)
713 unsigned nesting_depth = 0;
715 if (c_parser_require (parser, type, msgid))
716 return;
718 /* Skip tokens until the desired token is found. */
719 while (true)
721 /* Peek at the next token. */
722 c_token *token = c_parser_peek_token (parser);
723 /* If we've reached the token we want, consume it and stop. */
724 if (token->type == type && !nesting_depth)
726 c_parser_consume_token (parser);
727 break;
729 /* If we've run out of tokens, stop. */
730 if (token->type == CPP_EOF)
731 return;
732 if (token->type == CPP_OPEN_BRACE
733 || token->type == CPP_OPEN_PAREN
734 || token->type == CPP_OPEN_SQUARE)
735 ++nesting_depth;
736 else if (token->type == CPP_CLOSE_BRACE
737 || token->type == CPP_CLOSE_PAREN
738 || token->type == CPP_CLOSE_SQUARE)
740 if (nesting_depth-- == 0)
741 break;
743 /* Consume this token. */
744 c_parser_consume_token (parser);
746 parser->error = false;
749 /* Skip tokens until the end of a parameter is found, but do not
750 consume the comma, semicolon or closing delimiter. */
752 static void
753 c_parser_skip_to_end_of_parameter (c_parser *parser)
755 unsigned nesting_depth = 0;
757 while (true)
759 c_token *token = c_parser_peek_token (parser);
760 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
761 && !nesting_depth)
762 break;
763 /* If we've run out of tokens, stop. */
764 if (token->type == CPP_EOF)
765 return;
766 if (token->type == CPP_OPEN_BRACE
767 || token->type == CPP_OPEN_PAREN
768 || token->type == CPP_OPEN_SQUARE)
769 ++nesting_depth;
770 else if (token->type == CPP_CLOSE_BRACE
771 || token->type == CPP_CLOSE_PAREN
772 || token->type == CPP_CLOSE_SQUARE)
774 if (nesting_depth-- == 0)
775 break;
777 /* Consume this token. */
778 c_parser_consume_token (parser);
780 parser->error = false;
783 /* Skip tokens until we have consumed an entire block, or until we
784 have consumed a non-nested ';'. */
786 static void
787 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
789 unsigned nesting_depth = 0;
791 while (true)
793 c_token *token;
795 /* Peek at the next token. */
796 token = c_parser_peek_token (parser);
797 /* If we've run out of tokens, stop. */
798 if (token->type == CPP_EOF)
799 return;
800 /* If the next token is a ';', we have reached the end of the
801 statement. */
802 if (token->type == CPP_SEMICOLON && !nesting_depth)
804 /* Consume the ';'. */
805 c_parser_consume_token (parser);
806 break;
808 /* If the next token is a non-nested '}', then we have reached
809 the end of the current block. */
810 if (token->type == CPP_CLOSE_BRACE
811 && (nesting_depth == 0 || --nesting_depth == 0))
813 c_parser_consume_token (parser);
814 break;
816 /* If it the next token is a '{', then we are entering a new
817 block. Consume the entire block. */
818 if (token->type == CPP_OPEN_BRACE)
819 ++nesting_depth;
820 c_parser_consume_token (parser);
822 parser->error = false;
826 /* Save the warning flags which are controlled by __extension__. */
828 static inline int
829 disable_extension_diagnostics (void)
831 int ret = (pedantic
832 | (warn_pointer_arith << 1)
833 | (warn_traditional << 2)
834 | (flag_iso << 3));
835 pedantic = 0;
836 warn_pointer_arith = 0;
837 warn_traditional = 0;
838 flag_iso = 0;
839 return ret;
842 /* Restore the warning flags which are controlled by __extension__.
843 FLAGS is the return value from disable_extension_diagnostics. */
845 static inline void
846 restore_extension_diagnostics (int flags)
848 pedantic = flags & 1;
849 warn_pointer_arith = (flags >> 1) & 1;
850 warn_traditional = (flags >> 2) & 1;
851 flag_iso = (flags >> 3) & 1;
854 /* Possibly kinds of declarator to parse. */
855 typedef enum c_dtr_syn {
856 /* A normal declarator with an identifier. */
857 C_DTR_NORMAL,
858 /* An abstract declarator (maybe empty). */
859 C_DTR_ABSTRACT,
860 /* A parameter declarator: may be either, but after a type name does
861 not redeclare a typedef name as an identifier if it can
862 alternatively be interpreted as a typedef name; see DR#009,
863 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
864 following DR#249. For example, given a typedef T, "int T" and
865 "int *T" are valid parameter declarations redeclaring T, while
866 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
867 abstract declarators rather than involving redundant parentheses;
868 the same applies with attributes inside the parentheses before
869 "T". */
870 C_DTR_PARM
871 } c_dtr_syn;
873 static void c_parser_external_declaration (c_parser *);
874 static void c_parser_asm_definition (c_parser *);
875 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
876 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
877 bool);
878 static struct c_typespec c_parser_enum_specifier (c_parser *);
879 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
880 static tree c_parser_struct_declaration (c_parser *);
881 static struct c_typespec c_parser_typeof_specifier (c_parser *);
882 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
883 bool *);
884 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
885 c_dtr_syn, bool *);
886 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
887 bool,
888 struct c_declarator *);
889 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
890 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
891 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
892 static tree c_parser_simple_asm_expr (c_parser *);
893 static tree c_parser_attributes (c_parser *);
894 static struct c_type_name *c_parser_type_name (c_parser *);
895 static struct c_expr c_parser_initializer (c_parser *);
896 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
897 static void c_parser_initelt (c_parser *);
898 static void c_parser_initval (c_parser *, struct c_expr *);
899 static tree c_parser_compound_statement (c_parser *);
900 static void c_parser_compound_statement_nostart (c_parser *);
901 static void c_parser_label (c_parser *);
902 static void c_parser_statement (c_parser *);
903 static void c_parser_statement_after_labels (c_parser *);
904 static void c_parser_if_statement (c_parser *);
905 static void c_parser_switch_statement (c_parser *);
906 static void c_parser_while_statement (c_parser *);
907 static void c_parser_do_statement (c_parser *);
908 static void c_parser_for_statement (c_parser *);
909 static tree c_parser_asm_statement (c_parser *);
910 static tree c_parser_asm_operands (c_parser *);
911 static tree c_parser_asm_clobbers (c_parser *);
912 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
913 static struct c_expr c_parser_conditional_expression (c_parser *,
914 struct c_expr *);
915 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
916 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
917 static struct c_expr c_parser_unary_expression (c_parser *);
918 static struct c_expr c_parser_sizeof_expression (c_parser *);
919 static struct c_expr c_parser_alignof_expression (c_parser *);
920 static struct c_expr c_parser_postfix_expression (c_parser *);
921 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
922 struct c_type_name *);
923 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
924 struct c_expr);
925 static struct c_expr c_parser_expression (c_parser *);
926 static tree c_parser_expr_list (c_parser *);
928 /* These Objective-C parser functions are only ever called when
929 compiling Objective-C. */
930 static void c_parser_objc_class_definition (c_parser *);
931 static void c_parser_objc_class_instance_variables (c_parser *);
932 static void c_parser_objc_class_declaration (c_parser *);
933 static void c_parser_objc_alias_declaration (c_parser *);
934 static void c_parser_objc_protocol_definition (c_parser *);
935 static enum tree_code c_parser_objc_method_type (c_parser *);
936 static void c_parser_objc_method_definition (c_parser *);
937 static void c_parser_objc_methodprotolist (c_parser *);
938 static void c_parser_objc_methodproto (c_parser *);
939 static tree c_parser_objc_method_decl (c_parser *);
940 static tree c_parser_objc_type_name (c_parser *);
941 static tree c_parser_objc_protocol_refs (c_parser *);
942 static void c_parser_objc_try_catch_statement (c_parser *);
943 static void c_parser_objc_synchronized_statement (c_parser *);
944 static tree c_parser_objc_selector (c_parser *);
945 static tree c_parser_objc_selector_arg (c_parser *);
946 static tree c_parser_objc_receiver (c_parser *);
947 static tree c_parser_objc_message_args (c_parser *);
948 static tree c_parser_objc_keywordexpr (c_parser *);
950 /* Parse a translation unit (C90 6.7, C99 6.9).
952 translation-unit:
953 external-declarations
955 external-declarations:
956 external-declaration
957 external-declarations external-declaration
959 GNU extensions:
961 translation-unit:
962 empty
965 static void
966 c_parser_translation_unit (c_parser *parser)
968 if (c_parser_next_token_is (parser, CPP_EOF))
970 if (pedantic)
971 pedwarn ("ISO C forbids an empty source file");
973 else
975 void *obstack_position = obstack_alloc (&parser_obstack, 0);
978 ggc_collect ();
979 c_parser_external_declaration (parser);
980 obstack_free (&parser_obstack, obstack_position);
982 while (c_parser_next_token_is_not (parser, CPP_EOF));
986 /* Parse an external declaration (C90 6.7, C99 6.9).
988 external-declaration:
989 function-definition
990 declaration
992 GNU extensions:
994 external-declaration:
995 asm-definition
997 __extension__ external-declaration
999 Objective-C:
1001 external-declaration:
1002 objc-class-definition
1003 objc-class-declaration
1004 objc-alias-declaration
1005 objc-protocol-definition
1006 objc-method-definition
1007 @end
1010 static void
1011 c_parser_external_declaration (c_parser *parser)
1013 int ext;
1014 switch (c_parser_peek_token (parser)->type)
1016 case CPP_KEYWORD:
1017 switch (c_parser_peek_token (parser)->keyword)
1019 case RID_EXTENSION:
1020 ext = disable_extension_diagnostics ();
1021 c_parser_consume_token (parser);
1022 c_parser_external_declaration (parser);
1023 restore_extension_diagnostics (ext);
1024 break;
1025 case RID_ASM:
1026 c_parser_asm_definition (parser);
1027 break;
1028 case RID_AT_INTERFACE:
1029 case RID_AT_IMPLEMENTATION:
1030 gcc_assert (c_dialect_objc ());
1031 c_parser_objc_class_definition (parser);
1032 break;
1033 case RID_AT_CLASS:
1034 gcc_assert (c_dialect_objc ());
1035 c_parser_objc_class_declaration (parser);
1036 break;
1037 case RID_AT_ALIAS:
1038 gcc_assert (c_dialect_objc ());
1039 c_parser_objc_alias_declaration (parser);
1040 break;
1041 case RID_AT_PROTOCOL:
1042 gcc_assert (c_dialect_objc ());
1043 c_parser_objc_protocol_definition (parser);
1044 break;
1045 case RID_AT_END:
1046 gcc_assert (c_dialect_objc ());
1047 c_parser_consume_token (parser);
1048 objc_finish_implementation ();
1049 break;
1050 default:
1051 goto decl_or_fndef;
1053 break;
1054 case CPP_SEMICOLON:
1055 if (pedantic)
1056 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1057 c_parser_consume_token (parser);
1058 break;
1059 case CPP_PLUS:
1060 case CPP_MINUS:
1061 if (c_dialect_objc ())
1063 c_parser_objc_method_definition (parser);
1064 break;
1066 /* Else fall through, and yield a syntax error trying to parse
1067 as a declaration or function definition. */
1068 default:
1069 decl_or_fndef:
1070 /* A declaration or a function definition. We can only tell
1071 which after parsing the declaration specifiers, if any, and
1072 the first declarator. */
1073 c_parser_declaration_or_fndef (parser, true, true, false, true);
1074 break;
1078 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1079 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1080 accepted; otherwise (old-style parameter declarations) only other
1081 declarations are accepted. If NESTED is true, we are inside a
1082 function or parsing old-style parameter declarations; any functions
1083 encountered are nested functions and declaration specifiers are
1084 required; otherwise we are at top level and functions are normal
1085 functions and declaration specifiers may be optional. If EMPTY_OK
1086 is true, empty declarations are OK (subject to all other
1087 constraints); otherwise (old-style parameter declarations) they are
1088 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1089 may start with attributes; otherwise they may not.
1091 declaration:
1092 declaration-specifiers init-declarator-list[opt] ;
1094 function-definition:
1095 declaration-specifiers[opt] declarator declaration-list[opt]
1096 compound-statement
1098 declaration-list:
1099 declaration
1100 declaration-list declaration
1102 init-declarator-list:
1103 init-declarator
1104 init-declarator-list , init-declarator
1106 init-declarator:
1107 declarator simple-asm-expr[opt] attributes[opt]
1108 declarator simple-asm-expr[opt] attributes[opt] = initializer
1110 GNU extensions:
1112 nested-function-definition:
1113 declaration-specifiers declarator declaration-list[opt]
1114 compound-statement
1116 The simple-asm-expr and attributes are GNU extensions.
1118 This function does not handle __extension__; that is handled in its
1119 callers. ??? Following the old parser, __extension__ may start
1120 external declarations, declarations in functions and declarations
1121 at the start of "for" loops, but not old-style parameter
1122 declarations.
1124 C99 requires declaration specifiers in a function definition; the
1125 absence is diagnosed through the diagnosis of implicit int. In GNU
1126 C we also allow but diagnose declarations without declaration
1127 specifiers, but only at top level (elsewhere they conflict with
1128 other syntax). */
1130 static void
1131 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1132 bool nested, bool start_attr_ok)
1134 struct c_declspecs *specs;
1135 tree prefix_attrs;
1136 tree all_prefix_attrs;
1137 bool diagnosed_no_specs = false;
1138 specs = build_null_declspecs ();
1139 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1140 if (parser->error)
1142 c_parser_skip_to_end_of_block_or_statement (parser);
1143 return;
1145 if (nested && !specs->declspecs_seen_p)
1147 c_parser_error (parser, "expected declaration specifiers");
1148 c_parser_skip_to_end_of_block_or_statement (parser);
1149 return;
1151 finish_declspecs (specs);
1152 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1154 if (empty_ok)
1155 shadow_tag (specs);
1156 else
1158 shadow_tag_warned (specs, 1);
1159 pedwarn ("empty declaration");
1161 c_parser_consume_token (parser);
1162 return;
1164 pending_xref_error ();
1165 prefix_attrs = specs->attrs;
1166 all_prefix_attrs = prefix_attrs;
1167 specs->attrs = NULL_TREE;
1168 while (true)
1170 struct c_declarator *declarator;
1171 bool dummy = false;
1172 tree fnbody;
1173 /* Declaring either one or more declarators (in which case we
1174 should diagnose if there were no declaration specifiers) or a
1175 function definition (in which case the diagnostic for
1176 implicit int suffices). */
1177 declarator = c_parser_declarator (parser, specs->type_seen_p,
1178 C_DTR_NORMAL, &dummy);
1179 if (declarator == NULL)
1181 c_parser_skip_to_end_of_block_or_statement (parser);
1182 return;
1184 if (c_parser_next_token_is (parser, CPP_EQ)
1185 || c_parser_next_token_is (parser, CPP_COMMA)
1186 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1187 || c_parser_next_token_is_keyword (parser, RID_ASM)
1188 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1190 tree asm_name = NULL_TREE;
1191 tree postfix_attrs = NULL_TREE;
1192 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1194 diagnosed_no_specs = true;
1195 pedwarn ("data definition has no type or storage class");
1197 /* Having seen a data definition, there cannot now be a
1198 function definition. */
1199 fndef_ok = false;
1200 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1201 asm_name = c_parser_simple_asm_expr (parser);
1202 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1203 postfix_attrs = c_parser_attributes (parser);
1204 if (c_parser_next_token_is (parser, CPP_EQ))
1206 tree d;
1207 struct c_expr init;
1208 c_parser_consume_token (parser);
1209 /* The declaration of the variable is in effect while
1210 its initializer is parsed. */
1211 d = start_decl (declarator, specs, true,
1212 chainon (postfix_attrs, all_prefix_attrs));
1213 if (!d)
1214 d = error_mark_node;
1215 start_init (d, asm_name, global_bindings_p ());
1216 init = c_parser_initializer (parser);
1217 finish_init ();
1218 if (d != error_mark_node)
1220 maybe_warn_string_init (TREE_TYPE (d), init);
1221 finish_decl (d, init.value, asm_name);
1224 else
1226 tree d = start_decl (declarator, specs, false,
1227 chainon (postfix_attrs,
1228 all_prefix_attrs));
1229 if (d)
1230 finish_decl (d, NULL_TREE, asm_name);
1232 if (c_parser_next_token_is (parser, CPP_COMMA))
1234 c_parser_consume_token (parser);
1235 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1236 all_prefix_attrs = chainon (c_parser_attributes (parser),
1237 prefix_attrs);
1238 else
1239 all_prefix_attrs = prefix_attrs;
1240 continue;
1242 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1244 c_parser_consume_token (parser);
1245 return;
1247 else
1249 c_parser_error (parser, "expected %<,%> or %<;%>");
1250 c_parser_skip_to_end_of_block_or_statement (parser);
1251 return;
1254 else if (!fndef_ok)
1256 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1257 "%<asm%> or %<__attribute__%>");
1258 c_parser_skip_to_end_of_block_or_statement (parser);
1259 return;
1261 /* Function definition (nested or otherwise). */
1262 if (nested)
1264 if (pedantic)
1265 pedwarn ("ISO C forbids nested functions");
1266 push_function_context ();
1268 if (!start_function (specs, declarator, all_prefix_attrs))
1270 /* This can appear in many cases looking nothing like a
1271 function definition, so we don't give a more specific
1272 error suggesting there was one. */
1273 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1274 "or %<__attribute__%>");
1275 if (nested)
1276 pop_function_context ();
1277 break;
1279 /* Parse old-style parameter declarations. ??? Attributes are
1280 not allowed to start declaration specifiers here because of a
1281 syntax conflict between a function declaration with attribute
1282 suffix and a function definition with an attribute prefix on
1283 first old-style parameter declaration. Following the old
1284 parser, they are not accepted on subsequent old-style
1285 parameter declarations either. However, there is no
1286 ambiguity after the first declaration, nor indeed on the
1287 first as long as we don't allow postfix attributes after a
1288 declarator with a nonempty identifier list in a definition;
1289 and postfix attributes have never been accepted here in
1290 function definitions either. */
1291 while (c_parser_next_token_is_not (parser, CPP_EOF)
1292 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1293 c_parser_declaration_or_fndef (parser, false, false, true, false);
1294 DECL_SOURCE_LOCATION (current_function_decl)
1295 = c_parser_peek_token (parser)->location;
1296 store_parm_decls ();
1297 fnbody = c_parser_compound_statement (parser);
1298 if (nested)
1300 tree decl = current_function_decl;
1301 add_stmt (fnbody);
1302 finish_function ();
1303 pop_function_context ();
1304 add_stmt (build_stmt (DECL_EXPR, decl));
1306 else
1308 add_stmt (fnbody);
1309 finish_function ();
1311 break;
1315 /* Parse an asm-definition (asm() outside a function body). This is a
1316 GNU extension.
1318 asm-definition:
1319 simple-asm-expr ;
1322 static void
1323 c_parser_asm_definition (c_parser *parser)
1325 tree asm_str = c_parser_simple_asm_expr (parser);
1326 /* ??? This only works sensibly in the presence of
1327 -fno-unit-at-a-time; file-scope asms really need to be passed to
1328 cgraph which needs to preserve the order of functions and
1329 file-scope asms. */
1330 if (asm_str)
1331 assemble_asm (asm_str);
1332 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1335 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1336 6.7), adding them to SPECS (which may already include some).
1337 Storage class specifiers are accepted iff SCSPEC_OK; type
1338 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1339 the start iff START_ATTR_OK.
1341 declaration-specifiers:
1342 storage-class-specifier declaration-specifiers[opt]
1343 type-specifier declaration-specifiers[opt]
1344 type-qualifier declaration-specifiers[opt]
1345 function-specifier declaration-specifiers[opt]
1347 Function specifiers (inline) are from C99, and are currently
1348 handled as storage class specifiers, as is __thread.
1350 C90 6.5.1, C99 6.7.1:
1351 storage-class-specifier:
1352 typedef
1353 extern
1354 static
1355 auto
1356 register
1358 C99 6.7.4:
1359 function-specifier:
1360 inline
1362 C90 6.5.2, C99 6.7.2:
1363 type-specifier:
1364 void
1365 char
1366 short
1368 long
1369 float
1370 double
1371 signed
1372 unsigned
1373 _Bool
1374 _Complex
1375 [_Imaginary removed in C99 TC2]
1376 struct-or-union-specifier
1377 enum-specifier
1378 typedef-name
1380 (_Bool and _Complex are new in C99.)
1382 C90 6.5.3, C99 6.7.3:
1384 type-qualifier:
1385 const
1386 restrict
1387 volatile
1389 (restrict is new in C99.)
1391 GNU extensions:
1393 declaration-specifiers:
1394 attributes declaration-specifiers[opt]
1396 storage-class-specifier:
1397 __thread
1399 type-specifier:
1400 typeof-specifier
1402 Objective-C:
1404 type-specifier:
1405 class-name objc-protocol-refs[opt]
1406 typedef-name objc-protocol-refs
1407 objc-protocol-refs
1410 static void
1411 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1412 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1414 bool attrs_ok = start_attr_ok;
1415 bool seen_type = specs->type_seen_p;
1416 while (c_parser_next_token_is (parser, CPP_NAME)
1417 || c_parser_next_token_is (parser, CPP_KEYWORD)
1418 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1420 struct c_typespec t;
1421 tree attrs;
1422 if (c_parser_next_token_is (parser, CPP_NAME))
1424 tree value = c_parser_peek_token (parser)->value;
1425 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1426 /* This finishes the specifiers unless a type name is OK, it
1427 is declared as a type name and a type name hasn't yet
1428 been seen. */
1429 if (!typespec_ok || seen_type
1430 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1431 break;
1432 c_parser_consume_token (parser);
1433 seen_type = true;
1434 attrs_ok = true;
1435 if (kind == C_ID_TYPENAME
1436 && (!c_dialect_objc ()
1437 || c_parser_next_token_is_not (parser, CPP_LESS)))
1439 t.kind = ctsk_typedef;
1440 /* For a typedef name, record the meaning, not the name.
1441 In case of 'foo foo, bar;'. */
1442 t.spec = lookup_name (value);
1444 else
1446 tree proto = NULL_TREE;
1447 gcc_assert (c_dialect_objc ());
1448 t.kind = ctsk_objc;
1449 if (c_parser_next_token_is (parser, CPP_LESS))
1450 proto = c_parser_objc_protocol_refs (parser);
1451 t.spec = objc_get_protocol_qualified_type (value, proto);
1453 declspecs_add_type (specs, t);
1454 continue;
1456 if (c_parser_next_token_is (parser, CPP_LESS))
1458 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1459 nisse@lysator.liu.se. */
1460 tree proto;
1461 gcc_assert (c_dialect_objc ());
1462 if (!typespec_ok || seen_type)
1463 break;
1464 proto = c_parser_objc_protocol_refs (parser);
1465 t.kind = ctsk_objc;
1466 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1467 declspecs_add_type (specs, t);
1468 continue;
1470 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1471 switch (c_parser_peek_token (parser)->keyword)
1473 case RID_STATIC:
1474 case RID_EXTERN:
1475 case RID_REGISTER:
1476 case RID_TYPEDEF:
1477 case RID_INLINE:
1478 case RID_AUTO:
1479 case RID_THREAD:
1480 if (!scspec_ok)
1481 goto out;
1482 attrs_ok = true;
1483 /* TODO: Distinguish between function specifiers (inline)
1484 and storage class specifiers, either here or in
1485 declspecs_add_scspec. */
1486 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1487 c_parser_consume_token (parser);
1488 break;
1489 case RID_UNSIGNED:
1490 case RID_LONG:
1491 case RID_SHORT:
1492 case RID_SIGNED:
1493 case RID_COMPLEX:
1494 case RID_INT:
1495 case RID_CHAR:
1496 case RID_FLOAT:
1497 case RID_DOUBLE:
1498 case RID_VOID:
1499 case RID_BOOL:
1500 if (!typespec_ok)
1501 goto out;
1502 attrs_ok = true;
1503 seen_type = true;
1504 OBJC_NEED_RAW_IDENTIFIER (1);
1505 t.kind = ctsk_resword;
1506 t.spec = c_parser_peek_token (parser)->value;
1507 declspecs_add_type (specs, t);
1508 c_parser_consume_token (parser);
1509 break;
1510 case RID_ENUM:
1511 if (!typespec_ok)
1512 goto out;
1513 attrs_ok = true;
1514 seen_type = true;
1515 t = c_parser_enum_specifier (parser);
1516 declspecs_add_type (specs, t);
1517 break;
1518 case RID_STRUCT:
1519 case RID_UNION:
1520 if (!typespec_ok)
1521 goto out;
1522 attrs_ok = true;
1523 seen_type = true;
1524 t = c_parser_struct_or_union_specifier (parser);
1525 declspecs_add_type (specs, t);
1526 break;
1527 case RID_TYPEOF:
1528 /* ??? The old parser rejected typeof after other type
1529 specifiers, but is a syntax error the best way of
1530 handling this? */
1531 if (!typespec_ok || seen_type)
1532 goto out;
1533 attrs_ok = true;
1534 seen_type = true;
1535 t = c_parser_typeof_specifier (parser);
1536 declspecs_add_type (specs, t);
1537 break;
1538 case RID_CONST:
1539 case RID_VOLATILE:
1540 case RID_RESTRICT:
1541 attrs_ok = true;
1542 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1543 c_parser_consume_token (parser);
1544 break;
1545 case RID_ATTRIBUTE:
1546 if (!attrs_ok)
1547 goto out;
1548 attrs = c_parser_attributes (parser);
1549 declspecs_add_attrs (specs, attrs);
1550 break;
1551 default:
1552 goto out;
1555 out: ;
1558 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1560 enum-specifier:
1561 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1562 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1563 enum attributes[opt] identifier
1565 The form with trailing comma is new in C99. The forms with
1566 attributes are GNU extensions. In GNU C, we accept any expression
1567 without commas in the syntax (assignment expressions, not just
1568 conditional expressions); assignment expressions will be diagnosed
1569 as non-constant.
1571 enumerator-list:
1572 enumerator
1573 enumerator-list , enumerator
1575 enumerator:
1576 enumeration-constant
1577 enumeration-constant = constant-expression
1580 static struct c_typespec
1581 c_parser_enum_specifier (c_parser *parser)
1583 struct c_typespec ret;
1584 tree attrs;
1585 tree ident = NULL_TREE;
1586 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1587 c_parser_consume_token (parser);
1588 attrs = c_parser_attributes (parser);
1589 if (c_parser_next_token_is (parser, CPP_NAME))
1591 ident = c_parser_peek_token (parser)->value;
1592 c_parser_consume_token (parser);
1594 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1596 /* Parse an enum definition. */
1597 tree type = start_enum (ident);
1598 tree postfix_attrs;
1599 /* We chain the enumerators in reverse order, then put them in
1600 forward order at the end. */
1601 tree values = NULL_TREE;
1602 c_parser_consume_token (parser);
1603 while (true)
1605 tree enum_id;
1606 tree enum_value;
1607 tree enum_decl;
1608 bool seen_comma;
1609 if (c_parser_next_token_is_not (parser, CPP_NAME))
1611 c_parser_error (parser, "expected identifier");
1612 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1613 values = error_mark_node;
1614 break;
1616 enum_id = c_parser_peek_token (parser)->value;
1617 c_parser_consume_token (parser);
1618 if (c_parser_next_token_is (parser, CPP_EQ))
1620 c_parser_consume_token (parser);
1621 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1623 else
1624 enum_value = NULL_TREE;
1625 enum_decl = build_enumerator (enum_id, enum_value);
1626 TREE_CHAIN (enum_decl) = values;
1627 values = enum_decl;
1628 seen_comma = false;
1629 if (c_parser_next_token_is (parser, CPP_COMMA))
1631 seen_comma = true;
1632 c_parser_consume_token (parser);
1634 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1636 if (seen_comma && pedantic && !flag_isoc99)
1637 pedwarn ("comma at end of enumerator list");
1638 c_parser_consume_token (parser);
1639 break;
1641 if (!seen_comma)
1643 c_parser_error (parser, "expected %<,%> or %<}%>");
1644 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1645 values = error_mark_node;
1646 break;
1649 postfix_attrs = c_parser_attributes (parser);
1650 ret.spec = finish_enum (type, nreverse (values),
1651 chainon (attrs, postfix_attrs));
1652 ret.kind = ctsk_tagdef;
1653 return ret;
1655 else if (!ident)
1657 c_parser_error (parser, "expected %<{%>");
1658 ret.spec = error_mark_node;
1659 ret.kind = ctsk_tagref;
1660 return ret;
1662 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1663 /* In ISO C, enumerated types can be referred to only if already
1664 defined. */
1665 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1666 pedwarn ("ISO C forbids forward references to %<enum%> types");
1667 return ret;
1670 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1672 struct-or-union-specifier:
1673 struct-or-union attributes[opt] identifier[opt]
1674 { struct-contents } attributes[opt]
1675 struct-or-union attributes[opt] identifier
1677 struct-contents:
1678 struct-declaration-list
1680 struct-declaration-list:
1681 struct-declaration ;
1682 struct-declaration-list struct-declaration ;
1684 GNU extensions:
1686 struct-contents:
1687 empty
1688 struct-declaration
1689 struct-declaration-list struct-declaration
1691 struct-declaration-list:
1692 struct-declaration-list ;
1695 (Note that in the syntax here, unlike that in ISO C, the semicolons
1696 are included here rather than in struct-declaration, in order to
1697 describe the syntax with extra semicolons and missing semicolon at
1698 end.)
1700 Objective-C:
1702 struct-declaration-list:
1703 @defs ( class-name )
1705 (Note this does not include a trailing semicolon, but can be
1706 followed by further declarations, and gets a pedwarn-if-pedantic
1707 when followed by a semicolon.) */
1709 static struct c_typespec
1710 c_parser_struct_or_union_specifier (c_parser *parser)
1712 struct c_typespec ret;
1713 tree attrs;
1714 tree ident = NULL_TREE;
1715 enum tree_code code;
1716 switch (c_parser_peek_token (parser)->keyword)
1718 case RID_STRUCT:
1719 code = RECORD_TYPE;
1720 break;
1721 case RID_UNION:
1722 code = UNION_TYPE;
1723 break;
1724 default:
1725 gcc_unreachable ();
1727 c_parser_consume_token (parser);
1728 attrs = c_parser_attributes (parser);
1729 if (c_parser_next_token_is (parser, CPP_NAME))
1731 ident = c_parser_peek_token (parser)->value;
1732 c_parser_consume_token (parser);
1734 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1736 /* Parse a struct or union definition. Start the scope of the
1737 tag before parsing components. */
1738 tree type = start_struct (code, ident);
1739 tree postfix_attrs;
1740 /* We chain the components in reverse order, then put them in
1741 forward order at the end. Each struct-declaration may
1742 declare multiple components (comma-separated), so we must use
1743 chainon to join them, although when parsing each
1744 struct-declaration we can use TREE_CHAIN directly.
1746 The theory behind all this is that there will be more
1747 semicolon separated fields than comma separated fields, and
1748 so we'll be minimizing the number of node traversals required
1749 by chainon. */
1750 tree contents = NULL_TREE;
1751 c_parser_consume_token (parser);
1752 /* Handle the Objective-C @defs construct,
1753 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1754 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1756 tree name;
1757 gcc_assert (c_dialect_objc ());
1758 c_parser_consume_token (parser);
1759 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1760 goto end_at_defs;
1761 if (c_parser_next_token_is (parser, CPP_NAME)
1762 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1764 name = c_parser_peek_token (parser)->value;
1765 c_parser_consume_token (parser);
1767 else
1769 c_parser_error (parser, "expected class name");
1770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1771 goto end_at_defs;
1773 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1774 "expected %<)%>");
1775 contents = nreverse (objc_get_class_ivars (name));
1777 end_at_defs:
1778 /* Parse the struct-declarations and semicolons. Problems with
1779 semicolons are diagnosed here; empty structures are diagnosed
1780 elsewhere. */
1781 while (true)
1783 tree decls;
1784 /* Parse any stray semicolon. */
1785 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1787 if (pedantic)
1788 pedwarn ("extra semicolon in struct or union specified");
1789 c_parser_consume_token (parser);
1790 continue;
1792 /* Stop if at the end of the struct or union contents. */
1793 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1795 c_parser_consume_token (parser);
1796 break;
1798 /* Parse some comma-separated declarations, but not the
1799 trailing semicolon if any. */
1800 decls = c_parser_struct_declaration (parser);
1801 contents = chainon (decls, contents);
1802 /* If no semicolon follows, either we have a parse error or
1803 are at the end of the struct or union and should
1804 pedwarn. */
1805 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1806 c_parser_consume_token (parser);
1807 else
1809 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1810 pedwarn ("no semicolon at end of struct or union");
1811 else
1813 c_parser_error (parser, "expected %<;%>");
1814 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1815 break;
1819 postfix_attrs = c_parser_attributes (parser);
1820 ret.spec = finish_struct (type, nreverse (contents),
1821 chainon (attrs, postfix_attrs));
1822 ret.kind = ctsk_tagdef;
1823 return ret;
1825 else if (!ident)
1827 c_parser_error (parser, "expected %<{%>");
1828 ret.spec = error_mark_node;
1829 ret.kind = ctsk_tagref;
1830 return ret;
1832 ret = parser_xref_tag (code, ident);
1833 return ret;
1836 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1837 the trailing semicolon.
1839 struct-declaration:
1840 specifier-qualifier-list struct-declarator-list
1842 specifier-qualifier-list:
1843 type-specifier specifier-qualifier-list[opt]
1844 type-qualifier specifier-qualifier-list[opt]
1845 attributes specifier-qualifier-list[opt]
1847 struct-declarator-list:
1848 struct-declarator
1849 struct-declarator-list , attributes[opt] struct-declarator
1851 struct-declarator:
1852 declarator attributes[opt]
1853 declarator[opt] : constant-expression attributes[opt]
1855 GNU extensions:
1857 struct-declaration:
1858 __extension__ struct-declaration
1859 specifier-qualifier-list
1861 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1862 of attributes where shown is a GNU extension. In GNU C, we accept
1863 any expression without commas in the syntax (assignment
1864 expressions, not just conditional expressions); assignment
1865 expressions will be diagnosed as non-constant. */
1867 static tree
1868 c_parser_struct_declaration (c_parser *parser)
1870 struct c_declspecs *specs;
1871 tree prefix_attrs;
1872 tree all_prefix_attrs;
1873 tree decls;
1874 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
1876 int ext;
1877 tree decl;
1878 ext = disable_extension_diagnostics ();
1879 c_parser_consume_token (parser);
1880 decl = c_parser_struct_declaration (parser);
1881 restore_extension_diagnostics (ext);
1882 return decl;
1884 specs = build_null_declspecs ();
1885 c_parser_declspecs (parser, specs, false, true, true);
1886 if (parser->error)
1887 return NULL_TREE;
1888 if (!specs->declspecs_seen_p)
1890 c_parser_error (parser, "expected specifier-qualifier-list");
1891 return NULL_TREE;
1893 finish_declspecs (specs);
1894 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1896 tree ret;
1897 if (!specs->type_seen_p)
1899 if (pedantic)
1900 pedwarn ("ISO C forbids member declarations with no members");
1901 shadow_tag_warned (specs, pedantic);
1902 ret = NULL_TREE;
1904 else
1906 /* Support for unnamed structs or unions as members of
1907 structs or unions (which is [a] useful and [b] supports
1908 MS P-SDK). */
1909 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE);
1911 return ret;
1913 pending_xref_error ();
1914 prefix_attrs = specs->attrs;
1915 all_prefix_attrs = prefix_attrs;
1916 specs->attrs = NULL_TREE;
1917 decls = NULL_TREE;
1918 while (true)
1920 /* Declaring one or more declarators or un-named bit-fields. */
1921 struct c_declarator *declarator;
1922 bool dummy = false;
1923 if (c_parser_next_token_is (parser, CPP_COLON))
1924 declarator = build_id_declarator (NULL_TREE);
1925 else
1926 declarator = c_parser_declarator (parser, specs->type_seen_p,
1927 C_DTR_NORMAL, &dummy);
1928 if (declarator == NULL)
1930 c_parser_skip_to_end_of_block_or_statement (parser);
1931 break;
1933 if (c_parser_next_token_is (parser, CPP_COLON)
1934 || c_parser_next_token_is (parser, CPP_COMMA)
1935 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1936 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
1937 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1939 tree postfix_attrs = NULL_TREE;
1940 tree width = NULL_TREE;
1941 tree d;
1942 if (c_parser_next_token_is (parser, CPP_COLON))
1944 c_parser_consume_token (parser);
1945 width = c_parser_expr_no_commas (parser, NULL).value;
1947 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1948 postfix_attrs = c_parser_attributes (parser);
1949 d = grokfield (declarator, specs, width);
1950 decl_attributes (&d, chainon (postfix_attrs,
1951 all_prefix_attrs), 0);
1952 TREE_CHAIN (d) = decls;
1953 decls = d;
1954 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1955 all_prefix_attrs = chainon (c_parser_attributes (parser),
1956 prefix_attrs);
1957 else
1958 all_prefix_attrs = prefix_attrs;
1959 if (c_parser_next_token_is (parser, CPP_COMMA))
1960 c_parser_consume_token (parser);
1961 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
1962 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1964 /* Semicolon consumed in caller. */
1965 break;
1967 else
1969 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
1970 break;
1973 else
1975 c_parser_error (parser,
1976 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
1977 "%<__attribute__%>");
1978 break;
1981 return decls;
1984 /* Parse a typeof specifier (a GNU extension).
1986 typeof-specifier:
1987 typeof ( expression )
1988 typeof ( type-name )
1991 static struct c_typespec
1992 c_parser_typeof_specifier (c_parser *parser)
1994 struct c_typespec ret;
1995 ret.kind = ctsk_typeof;
1996 ret.spec = error_mark_node;
1997 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
1998 c_parser_consume_token (parser);
1999 skip_evaluation++;
2000 in_typeof++;
2001 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2003 skip_evaluation--;
2004 in_typeof--;
2005 return ret;
2007 if (c_parser_next_token_starts_typename (parser))
2009 struct c_type_name *type = c_parser_type_name (parser);
2010 skip_evaluation--;
2011 in_typeof--;
2012 if (type != NULL)
2014 ret.spec = groktypename (type);
2015 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2018 else
2020 struct c_expr expr = c_parser_expression (parser);
2021 skip_evaluation--;
2022 in_typeof--;
2023 if (TREE_CODE (expr.value) == COMPONENT_REF
2024 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2025 error ("%<typeof%> applied to a bit-field");
2026 ret.spec = TREE_TYPE (expr.value);
2027 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2029 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2030 return ret;
2033 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2034 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2035 be redeclared; otherwise it may not. KIND indicates which kind of
2036 declarator is wanted. Returns a valid declarator except in the
2037 case of a syntax error in which case NULL is returned. *SEEN_ID is
2038 set to true if an identifier being declared is seen; this is used
2039 to diagnose bad forms of abstract array declarators and to
2040 determine whether an identifier list is syntactically permitted.
2042 declarator:
2043 pointer[opt] direct-declarator
2045 direct-declarator:
2046 identifier
2047 ( attributes[opt] declarator )
2048 direct-declarator array-declarator
2049 direct-declarator ( parameter-type-list )
2050 direct-declarator ( identifier-list[opt] )
2052 pointer:
2053 * type-qualifier-list[opt]
2054 * type-qualifier-list[opt] pointer
2056 type-qualifier-list:
2057 type-qualifier
2058 attributes
2059 type-qualifier-list type-qualifier
2060 type-qualifier-list attributes
2062 parameter-type-list:
2063 parameter-list
2064 parameter-list , ...
2066 parameter-list:
2067 parameter-declaration
2068 parameter-list , parameter-declaration
2070 parameter-declaration:
2071 declaration-specifiers declarator attributes[opt]
2072 declaration-specifiers abstract-declarator[opt] attributes[opt]
2074 identifier-list:
2075 identifier
2076 identifier-list , identifier
2078 abstract-declarator:
2079 pointer
2080 pointer[opt] direct-abstract-declarator
2082 direct-abstract-declarator:
2083 ( attributes[opt] abstract-declarator )
2084 direct-abstract-declarator[opt] array-declarator
2085 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2087 GNU extensions:
2089 direct-declarator:
2090 direct-declarator ( parameter-forward-declarations
2091 parameter-type-list[opt] )
2093 direct-abstract-declarator:
2094 direct-abstract-declarator[opt] ( parameter-forward-declarations
2095 parameter-type-list[opt] )
2097 parameter-forward-declarations:
2098 parameter-list ;
2099 parameter-forward-declarations parameter-list ;
2101 The uses of attributes shown above are GNU extensions.
2103 Some forms of array declarator are not included in C99 in the
2104 syntax for abstract declarators; these are disallowed elsewhere.
2105 This may be a defect (DR#289).
2107 This function also accepts an omitted abstract declarator as being
2108 an abstract declarator, although not part of the formal syntax. */
2110 static struct c_declarator *
2111 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2112 bool *seen_id)
2114 /* Parse any initial pointer part. */
2115 if (c_parser_next_token_is (parser, CPP_MULT))
2117 struct c_declspecs *quals_attrs = build_null_declspecs ();
2118 struct c_declarator *inner;
2119 c_parser_consume_token (parser);
2120 c_parser_declspecs (parser, quals_attrs, false, false, true);
2121 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2122 if (inner == NULL)
2123 return NULL;
2124 else
2125 return make_pointer_declarator (quals_attrs, inner);
2127 /* Now we have a direct declarator, direct abstract declarator or
2128 nothing (which counts as a direct abstract declarator here). */
2129 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2132 /* Parse a direct declarator or direct abstract declarator; arguments
2133 as c_parser_declarator. */
2135 static struct c_declarator *
2136 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2137 bool *seen_id)
2139 /* The direct declarator must start with an identifier (possibly
2140 omitted) or a parenthesized declarator (possibly abstract). In
2141 an ordinary declarator, initial parentheses must start a
2142 parenthesized declarator. In an abstract declarator or parameter
2143 declarator, they could start a parenthesized declarator or a
2144 parameter list. To tell which, the open parenthesis and any
2145 following attributes must be read. If a declaration specifier
2146 follows, then it is a parameter list; if the specifier is a
2147 typedef name, there might be an ambiguity about redeclaring it,
2148 which is resolved in the direction of treating it as a typedef
2149 name. If a close parenthesis follows, it is also an empty
2150 parameter list, as the syntax does not permit empty abstract
2151 declarators. Otherwise, it is a parenthesised declarator (in
2152 which case the analysis may be repeated inside it, recursively).
2154 ??? There is an ambiguity in a parameter declaration "int
2155 (__attribute__((foo)) x)", where x is not a typedef name: it
2156 could be an abstract declarator for a function, or declare x with
2157 parentheses. The proper resolution of this ambiguity needs
2158 documenting. At present we follow an accident of the old
2159 parser's implementation, whereby the first parameter must have
2160 some declaration specifiers other than just attributes. Thus as
2161 a parameter declaration it is treated as a parenthesised
2162 parameter named x, and as an abstract declarator it is
2163 rejected.
2165 ??? Also following the old parser, attributes inside an empty
2166 parameter list are ignored, making it a list not yielding a
2167 prototype, rather than giving an error or making it have one
2168 parameter with implicit type int.
2170 ??? Also following the old parser, typedef names may be
2171 redeclared in declarators, but not Objective-C class names. */
2173 if (kind != C_DTR_ABSTRACT
2174 && c_parser_next_token_is (parser, CPP_NAME)
2175 && ((type_seen_p
2176 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2177 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2179 struct c_declarator *inner
2180 = build_id_declarator (c_parser_peek_token (parser)->value);
2181 *seen_id = true;
2182 inner->id_loc = c_parser_peek_token (parser)->location;
2183 c_parser_consume_token (parser);
2184 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2187 if (kind != C_DTR_NORMAL
2188 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2190 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2191 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2194 /* Either we are at the end of an abstract declarator, or we have
2195 parentheses. */
2197 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2199 tree attrs;
2200 struct c_declarator *inner;
2201 c_parser_consume_token (parser);
2202 attrs = c_parser_attributes (parser);
2203 if (kind != C_DTR_NORMAL
2204 && (c_parser_next_token_starts_declspecs (parser)
2205 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2207 struct c_arg_info *args
2208 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2209 attrs);
2210 if (args == NULL)
2211 return NULL;
2212 else
2214 inner
2215 = build_function_declarator (args,
2216 build_id_declarator (NULL_TREE));
2217 return c_parser_direct_declarator_inner (parser, *seen_id,
2218 inner);
2221 /* A parenthesized declarator. */
2222 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2223 if (inner != NULL && attrs != NULL)
2224 inner = build_attrs_declarator (attrs, inner);
2225 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2227 c_parser_consume_token (parser);
2228 if (inner == NULL)
2229 return NULL;
2230 else
2231 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2233 else
2235 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2236 "expected %<)%>");
2237 return NULL;
2240 else
2242 if (kind == C_DTR_NORMAL)
2244 c_parser_error (parser, "expected identifier or %<(%>");
2245 return NULL;
2247 else
2248 return build_id_declarator (NULL_TREE);
2252 /* Parse part of a direct declarator or direct abstract declarator,
2253 given that some (in INNER) has already been parsed; ID_PRESENT is
2254 true if an identifier is present, false for an abstract
2255 declarator. */
2257 static struct c_declarator *
2258 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2259 struct c_declarator *inner)
2261 /* Parse a sequence of array declarators and parameter lists. */
2262 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2264 struct c_declarator *declarator;
2265 struct c_declspecs *quals_attrs = build_null_declspecs ();
2266 bool static_seen;
2267 bool star_seen;
2268 tree dimen;
2269 c_parser_consume_token (parser);
2270 c_parser_declspecs (parser, quals_attrs, false, false, true);
2271 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2272 if (static_seen)
2273 c_parser_consume_token (parser);
2274 if (static_seen && !quals_attrs->declspecs_seen_p)
2275 c_parser_declspecs (parser, quals_attrs, false, false, true);
2276 if (!quals_attrs->declspecs_seen_p)
2277 quals_attrs = NULL;
2278 /* If "static" is present, there must be an array dimension.
2279 Otherwise, there may be a dimension, "*", or no
2280 dimension. */
2281 if (static_seen)
2283 star_seen = false;
2284 dimen = c_parser_expr_no_commas (parser, NULL).value;
2286 else
2288 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2290 dimen = NULL_TREE;
2291 star_seen = false;
2293 else if (c_parser_next_token_is (parser, CPP_MULT))
2295 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2297 dimen = NULL_TREE;
2298 star_seen = true;
2299 c_parser_consume_token (parser);
2301 else
2303 star_seen = false;
2304 dimen = c_parser_expr_no_commas (parser, NULL).value;
2307 else
2309 star_seen = false;
2310 dimen = c_parser_expr_no_commas (parser, NULL).value;
2313 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2314 c_parser_consume_token (parser);
2315 else
2317 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2318 "expected %<]%>");
2319 return NULL;
2321 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2322 star_seen);
2323 inner = set_array_declarator_inner (declarator, inner, !id_present);
2324 return c_parser_direct_declarator_inner (parser, id_present, inner);
2326 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2328 tree attrs;
2329 struct c_arg_info *args;
2330 c_parser_consume_token (parser);
2331 attrs = c_parser_attributes (parser);
2332 args = c_parser_parms_declarator (parser, id_present, attrs);
2333 if (args == NULL)
2334 return NULL;
2335 else
2337 inner = build_function_declarator (args, inner);
2338 return c_parser_direct_declarator_inner (parser, id_present, inner);
2341 return inner;
2344 /* Parse a parameter list or identifier list, including the closing
2345 parenthesis but not the opening one. ATTRS are the attributes at
2346 the start of the list. ID_LIST_OK is true if an identifier list is
2347 acceptable; such a list must not have attributes at the start. */
2349 static struct c_arg_info *
2350 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2352 push_scope ();
2353 declare_parm_level ();
2354 /* If the list starts with an identifier, it is an identifier list.
2355 Otherwise, it is either a prototype list or an empty list. */
2356 if (id_list_ok
2357 && !attrs
2358 && c_parser_next_token_is (parser, CPP_NAME)
2359 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2361 tree list = NULL_TREE;
2362 while (c_parser_next_token_is (parser, CPP_NAME)
2363 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2365 list = chainon (list, build_tree_list (NULL_TREE,
2366 c_parser_peek_token (parser)->value));
2367 c_parser_consume_token (parser);
2368 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2369 break;
2370 c_parser_consume_token (parser);
2371 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2373 c_parser_error (parser, "expected identifier");
2374 break;
2377 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2379 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2380 ret->parms = 0;
2381 ret->tags = 0;
2382 ret->types = list;
2383 ret->others = 0;
2384 c_parser_consume_token (parser);
2385 pop_scope ();
2386 return ret;
2388 else
2390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2391 "expected %<)%>");
2392 pop_scope ();
2393 return NULL;
2396 else
2398 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2399 pop_scope ();
2400 return ret;
2404 /* Parse a parameter list (possibly empty), including the closing
2405 parenthesis but not the opening one. ATTRS are the attributes at
2406 the start of the list. */
2408 static struct c_arg_info *
2409 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2411 bool good_parm = false;
2412 /* ??? Following the old parser, forward parameter declarations may
2413 use abstract declarators, and if no real parameter declarations
2414 follow the forward declarations then this is not diagnosed. Also
2415 note as above that attributes are ignored as the only contents of
2416 the parentheses, or as the only contents after forward
2417 declarations. */
2418 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2420 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2421 ret->parms = 0;
2422 ret->tags = 0;
2423 ret->types = 0;
2424 ret->others = 0;
2425 c_parser_consume_token (parser);
2426 return ret;
2428 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2430 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2431 ret->parms = 0;
2432 ret->tags = 0;
2433 ret->others = 0;
2434 /* Suppress -Wold-style-definition for this case. */
2435 ret->types = error_mark_node;
2436 error ("ISO C requires a named argument before %<...%>");
2437 c_parser_consume_token (parser);
2438 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2440 c_parser_consume_token (parser);
2441 return ret;
2443 else
2445 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2446 "expected %<)%>");
2447 return NULL;
2450 /* Nonempty list of parameters, either terminated with semicolon
2451 (forward declarations; recurse) or with close parenthesis (normal
2452 function) or with ", ... )" (variadic function). */
2453 while (true)
2455 /* Parse a parameter. */
2456 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2457 attrs = NULL_TREE;
2458 if (parm != NULL)
2460 good_parm = true;
2461 push_parm_decl (parm);
2463 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2465 tree new_attrs;
2466 c_parser_consume_token (parser);
2467 new_attrs = c_parser_attributes (parser);
2468 return c_parser_parms_list_declarator (parser, new_attrs);
2470 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2472 c_parser_consume_token (parser);
2473 if (good_parm)
2474 return get_parm_info (false);
2475 else
2477 struct c_arg_info *ret
2478 = XOBNEW (&parser_obstack, struct c_arg_info);
2479 ret->parms = 0;
2480 ret->tags = 0;
2481 ret->types = 0;
2482 ret->others = 0;
2483 return ret;
2486 if (!c_parser_require (parser, CPP_COMMA,
2487 "expected %<;%>, %<,%> or %<)%>"))
2489 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2490 return NULL;
2492 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2494 c_parser_consume_token (parser);
2495 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2497 c_parser_consume_token (parser);
2498 if (good_parm)
2499 return get_parm_info (true);
2500 else
2502 struct c_arg_info *ret
2503 = XOBNEW (&parser_obstack, struct c_arg_info);
2504 ret->parms = 0;
2505 ret->tags = 0;
2506 ret->types = 0;
2507 ret->others = 0;
2508 return ret;
2511 else
2513 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2514 "expected %<)%>");
2515 return NULL;
2521 /* Parse a parameter declaration. ATTRS are the attributes at the
2522 start of the declaration if it is the first parameter. */
2524 static struct c_parm *
2525 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2527 struct c_declspecs *specs;
2528 struct c_declarator *declarator;
2529 tree prefix_attrs;
2530 tree postfix_attrs = NULL_TREE;
2531 bool dummy = false;
2532 if (!c_parser_next_token_starts_declspecs (parser))
2534 /* ??? In some Objective-C cases '...' isn't applicable so there
2535 should be a different message. */
2536 c_parser_error (parser,
2537 "expected declaration specifiers or %<...%>");
2538 c_parser_skip_to_end_of_parameter (parser);
2539 return NULL;
2541 specs = build_null_declspecs ();
2542 if (attrs)
2544 declspecs_add_attrs (specs, attrs);
2545 attrs = NULL_TREE;
2547 c_parser_declspecs (parser, specs, true, true, true);
2548 finish_declspecs (specs);
2549 pending_xref_error ();
2550 prefix_attrs = specs->attrs;
2551 specs->attrs = NULL_TREE;
2552 declarator = c_parser_declarator (parser, specs->type_seen_p,
2553 C_DTR_PARM, &dummy);
2554 if (declarator == NULL)
2556 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2557 return NULL;
2559 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2560 postfix_attrs = c_parser_attributes (parser);
2561 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2562 declarator);
2565 /* Parse a string literal in an asm expression. It should not be
2566 translated, and wide string literals are an error although
2567 permitted by the syntax. This is a GNU extension.
2569 asm-string-literal:
2570 string-literal
2572 ??? At present, following the old parser, the caller needs to have
2573 set c_lex_string_translate to 0. It would be better to follow the
2574 C++ parser rather than using the c_lex_string_translate kludge. */
2576 static tree
2577 c_parser_asm_string_literal (c_parser *parser)
2579 tree str;
2580 if (c_parser_next_token_is (parser, CPP_STRING))
2582 str = c_parser_peek_token (parser)->value;
2583 c_parser_consume_token (parser);
2585 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2587 error ("wide string literal in %<asm%>");
2588 str = build_string (1, "");
2589 c_parser_consume_token (parser);
2591 else
2593 c_parser_error (parser, "expected string literal");
2594 str = NULL_TREE;
2596 return str;
2599 /* Parse a simple asm expression. This is used in restricted
2600 contexts, where a full expression with inputs and outputs does not
2601 make sense. This is a GNU extension.
2603 simple-asm-expr:
2604 asm ( asm-string-literal )
2607 static tree
2608 c_parser_simple_asm_expr (c_parser *parser)
2610 tree str;
2611 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2612 /* ??? Follow the C++ parser rather than using the
2613 c_lex_string_translate kludge. */
2614 c_lex_string_translate = 0;
2615 c_parser_consume_token (parser);
2616 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2618 c_lex_string_translate = 1;
2619 return NULL_TREE;
2621 str = c_parser_asm_string_literal (parser);
2622 c_lex_string_translate = 1;
2623 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2625 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2626 return NULL_TREE;
2628 return str;
2631 /* Parse (possibly empty) attributes. This is a GNU extension.
2633 attributes:
2634 empty
2635 attributes attribute
2637 attribute:
2638 __attribute__ ( ( attribute-list ) )
2640 attribute-list:
2641 attrib
2642 attribute_list , attrib
2644 attrib:
2645 empty
2646 any-word
2647 any-word ( identifier )
2648 any-word ( identifier , nonempty-expr-list )
2649 any-word ( expr-list )
2651 where the "identifier" must not be declared as a type, and
2652 "any-word" may be any identifier (including one declared as a
2653 type), a reserved word storage class specifier, type specifier or
2654 type qualifier. ??? This still leaves out most reserved keywords
2655 (following the old parser), shouldn't we include them, and why not
2656 allow identifiers declared as types to start the arguments? */
2658 static tree
2659 c_parser_attributes (c_parser *parser)
2661 tree attrs = NULL_TREE;
2662 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2664 /* ??? Follow the C++ parser rather than using the
2665 c_lex_string_translate kludge. */
2666 c_lex_string_translate = 0;
2667 c_parser_consume_token (parser);
2668 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2670 c_lex_string_translate = 1;
2671 return attrs;
2673 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2675 c_lex_string_translate = 1;
2676 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2677 return attrs;
2679 /* Parse the attribute list. */
2680 while (c_parser_next_token_is (parser, CPP_COMMA)
2681 || c_parser_next_token_is (parser, CPP_NAME)
2682 || c_parser_next_token_is (parser, CPP_KEYWORD))
2684 tree attr, attr_name, attr_args;
2685 if (c_parser_next_token_is (parser, CPP_COMMA))
2687 c_parser_consume_token (parser);
2688 continue;
2690 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2692 /* ??? See comment above about what keywords are
2693 accepted here. */
2694 bool ok;
2695 switch (c_parser_peek_token (parser)->keyword)
2697 case RID_STATIC:
2698 case RID_UNSIGNED:
2699 case RID_LONG:
2700 case RID_CONST:
2701 case RID_EXTERN:
2702 case RID_REGISTER:
2703 case RID_TYPEDEF:
2704 case RID_SHORT:
2705 case RID_INLINE:
2706 case RID_VOLATILE:
2707 case RID_SIGNED:
2708 case RID_AUTO:
2709 case RID_RESTRICT:
2710 case RID_COMPLEX:
2711 case RID_THREAD:
2712 case RID_INT:
2713 case RID_CHAR:
2714 case RID_FLOAT:
2715 case RID_DOUBLE:
2716 case RID_VOID:
2717 case RID_BOOL:
2718 ok = true;
2719 break;
2720 default:
2721 ok = false;
2722 break;
2724 if (!ok)
2725 break;
2727 attr_name = c_parser_peek_token (parser)->value;
2728 c_parser_consume_token (parser);
2729 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2731 attr = build_tree_list (attr_name, NULL_TREE);
2732 attrs = chainon (attrs, attr);
2733 continue;
2735 c_parser_consume_token (parser);
2736 /* Parse the attribute contents. If they start with an
2737 identifier which is followed by a comma or close
2738 parenthesis, then the arguments start with that
2739 identifier; otherwise they are an expression list. */
2740 if (c_parser_next_token_is (parser, CPP_NAME)
2741 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2742 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2743 || (c_parser_peek_2nd_token (parser)->type
2744 == CPP_CLOSE_PAREN)))
2746 tree arg1 = c_parser_peek_token (parser)->value;
2747 c_parser_consume_token (parser);
2748 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2749 attr_args = build_tree_list (NULL_TREE, arg1);
2750 else
2752 c_parser_consume_token (parser);
2753 attr_args = tree_cons (NULL_TREE, arg1,
2754 c_parser_expr_list (parser));
2757 else
2759 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2760 attr_args = NULL_TREE;
2761 else
2762 attr_args = c_parser_expr_list (parser);
2764 attr = build_tree_list (attr_name, attr_args);
2765 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2766 c_parser_consume_token (parser);
2767 else
2769 c_lex_string_translate = 1;
2770 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2771 "expected %<)%>");
2772 return attrs;
2774 attrs = chainon (attrs, attr);
2776 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2777 c_parser_consume_token (parser);
2778 else
2780 c_lex_string_translate = 1;
2781 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2782 "expected %<)%>");
2783 return attrs;
2785 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2786 c_parser_consume_token (parser);
2787 else
2789 c_lex_string_translate = 1;
2790 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2791 "expected %<)%>");
2792 return attrs;
2794 c_lex_string_translate = 1;
2796 return attrs;
2799 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2801 type-name:
2802 specifier-qualifier-list abstract-declarator[opt]
2805 static struct c_type_name *
2806 c_parser_type_name (c_parser *parser)
2808 struct c_declspecs *specs = build_null_declspecs ();
2809 struct c_declarator *declarator;
2810 struct c_type_name *ret;
2811 bool dummy = false;
2812 c_parser_declspecs (parser, specs, false, true, true);
2813 if (!specs->declspecs_seen_p)
2815 c_parser_error (parser, "expected specifier-qualifier-list");
2816 return NULL;
2818 pending_xref_error ();
2819 finish_declspecs (specs);
2820 declarator = c_parser_declarator (parser, specs->type_seen_p,
2821 C_DTR_ABSTRACT, &dummy);
2822 if (declarator == NULL)
2823 return NULL;
2824 ret = XOBNEW (&parser_obstack, struct c_type_name);
2825 ret->specs = specs;
2826 ret->declarator = declarator;
2827 return ret;
2830 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
2832 initializer:
2833 assignment-expression
2834 { initializer-list }
2835 { initializer-list , }
2837 initializer-list:
2838 designation[opt] initializer
2839 initializer-list , designation[opt] initializer
2841 designation:
2842 designator-list =
2844 designator-list:
2845 designator
2846 designator-list designator
2848 designator:
2849 array-designator
2850 . identifier
2852 array-designator:
2853 [ constant-expression ]
2855 GNU extensions:
2857 initializer:
2860 designation:
2861 array-designator
2862 identifier :
2864 array-designator:
2865 [ constant-expression ... constant-expression ]
2867 Any expression without commas is accepted in the syntax for the
2868 constant-expressions, with non-constant expressions rejected later.
2870 This function is only used for top-level initializers; for nested
2871 ones, see c_parser_initval. */
2873 static struct c_expr
2874 c_parser_initializer (c_parser *parser)
2876 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2877 return c_parser_braced_init (parser, NULL_TREE, false);
2878 else
2879 return c_parser_expr_no_commas (parser, NULL);
2882 /* Parse a braced initializer list. TYPE is the type specified for a
2883 compound literal, and NULL_TREE for other initializers and for
2884 nested braced lists. NESTED_P is true for nested braced lists,
2885 false for the list of a compound literal or the list that is the
2886 top-level initializer in a declaration. */
2888 static struct c_expr
2889 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
2891 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
2892 c_parser_consume_token (parser);
2893 if (nested_p)
2894 push_init_level (0);
2895 else
2896 really_start_incremental_init (type);
2897 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2899 if (pedantic)
2900 pedwarn ("ISO C forbids empty initializer braces");
2902 else
2904 /* Parse a non-empty initializer list, possibly with a trailing
2905 comma. */
2906 while (true)
2908 c_parser_initelt (parser);
2909 if (parser->error)
2910 break;
2911 if (c_parser_next_token_is (parser, CPP_COMMA))
2912 c_parser_consume_token (parser);
2913 else
2914 break;
2915 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2916 break;
2919 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
2921 struct c_expr ret;
2922 ret.value = error_mark_node;
2923 ret.original_code = ERROR_MARK;
2924 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
2925 return ret;
2927 c_parser_consume_token (parser);
2928 return pop_init_level (0);
2931 /* Parse a nested initializer, including designators. */
2933 static void
2934 c_parser_initelt (c_parser *parser)
2936 /* Parse any designator or designator list. A single array
2937 designator may have the subsequent "=" omitted in GNU C, but a
2938 longer list or a structure member designator may not. */
2939 if (c_parser_next_token_is (parser, CPP_NAME)
2940 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
2942 /* Old-style structure member designator. */
2943 set_init_label (c_parser_peek_token (parser)->value);
2944 if (pedantic)
2945 pedwarn ("obsolete use of designated initializer with %<:%>");
2946 c_parser_consume_token (parser);
2947 c_parser_consume_token (parser);
2949 else
2951 /* des_seen is 0 if there have been no designators, 1 if there
2952 has been a single array designator and 2 otherwise. */
2953 int des_seen = 0;
2954 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
2955 || c_parser_next_token_is (parser, CPP_DOT))
2957 int des_prev = des_seen;
2958 if (des_seen < 2)
2959 des_seen++;
2960 if (c_parser_next_token_is (parser, CPP_DOT))
2962 des_seen = 2;
2963 c_parser_consume_token (parser);
2964 if (c_parser_next_token_is (parser, CPP_NAME))
2966 set_init_label (c_parser_peek_token (parser)->value);
2967 c_parser_consume_token (parser);
2969 else
2971 struct c_expr init;
2972 init.value = error_mark_node;
2973 init.original_code = ERROR_MARK;
2974 c_parser_error (parser, "expected identifier");
2975 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2976 process_init_element (init);
2977 return;
2980 else
2982 tree first, second;
2983 /* ??? Following the old parser, [ objc-receiver
2984 objc-message-args ] is accepted as an initializer,
2985 being distinguished from a designator by what follows
2986 the first assignment expression inside the square
2987 brackets, but after a first array designator a
2988 subsequent square bracket is for Objective-C taken to
2989 start an expression, using the obsolete form of
2990 designated initializer without '=', rather than
2991 possibly being a second level of designation: in LALR
2992 terms, the '[' is shifted rather than reducing
2993 designator to designator-list. */
2994 if (des_prev == 1 && c_dialect_objc ())
2996 des_seen = des_prev;
2997 break;
2999 if (des_prev == 0 && c_dialect_objc ())
3001 /* This might be an array designator or an
3002 Objective-C message expression. If the former,
3003 continue parsing here; if the latter, parse the
3004 remainder of the initializer given the starting
3005 primary-expression. ??? It might make sense to
3006 distinguish when des_prev == 1 as well; see
3007 previous comment. */
3008 tree rec, args;
3009 struct c_expr mexpr;
3010 c_parser_consume_token (parser);
3011 if (c_parser_peek_token (parser)->type == CPP_NAME
3012 && ((c_parser_peek_token (parser)->id_kind
3013 == C_ID_TYPENAME)
3014 || (c_parser_peek_token (parser)->id_kind
3015 == C_ID_CLASSNAME)))
3017 /* Type name receiver. */
3018 tree id = c_parser_peek_token (parser)->value;
3019 c_parser_consume_token (parser);
3020 rec = objc_get_class_reference (id);
3021 goto parse_message_args;
3023 first = c_parser_expr_no_commas (parser, NULL).value;
3024 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3025 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3026 goto array_desig_after_first;
3027 /* Expression receiver. So far only one part
3028 without commas has been parsed; there might be
3029 more of the expression. */
3030 rec = first;
3031 while (c_parser_next_token_is (parser, CPP_COMMA))
3033 tree next;
3034 c_parser_consume_token (parser);
3035 next = c_parser_expr_no_commas (parser, NULL).value;
3036 rec = build_compound_expr (rec, next);
3038 parse_message_args:
3039 /* Now parse the objc-message-args. */
3040 args = c_parser_objc_message_args (parser);
3041 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3042 "expected %<]%>");
3043 mexpr.value
3044 = objc_build_message_expr (build_tree_list (rec, args));
3045 mexpr.original_code = ERROR_MARK;
3046 /* Now parse and process the remainder of the
3047 initializer, starting with this message
3048 expression as a primary-expression. */
3049 c_parser_initval (parser, &mexpr);
3050 return;
3052 c_parser_consume_token (parser);
3053 first = c_parser_expr_no_commas (parser, NULL).value;
3054 array_desig_after_first:
3055 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3057 c_parser_consume_token (parser);
3058 second = c_parser_expr_no_commas (parser, NULL).value;
3060 else
3061 second = NULL_TREE;
3062 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3064 c_parser_consume_token (parser);
3065 set_init_index (first, second);
3066 if (pedantic && second)
3067 pedwarn ("ISO C forbids specifying range of "
3068 "elements to initialize");
3070 else
3071 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3072 "expected %<]%>");
3075 if (des_seen >= 1)
3077 if (c_parser_next_token_is (parser, CPP_EQ))
3079 if (pedantic && !flag_isoc99)
3080 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3081 c_parser_consume_token (parser);
3083 else
3085 if (des_seen == 1)
3087 if (pedantic)
3088 pedwarn ("obsolete use of designated initializer "
3089 "without %<=%>");
3091 else
3093 struct c_expr init;
3094 init.value = error_mark_node;
3095 init.original_code = ERROR_MARK;
3096 c_parser_error (parser, "expected %<=%>");
3097 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3098 process_init_element (init);
3099 return;
3104 c_parser_initval (parser, NULL);
3107 /* Parse a nested initializer; as c_parser_initializer but parses
3108 initializers within braced lists, after any designators have been
3109 applied. If AFTER is not NULL then it is an Objective-C message
3110 expression which is the primary-expression starting the
3111 initializer. */
3113 static void
3114 c_parser_initval (c_parser *parser, struct c_expr *after)
3116 struct c_expr init;
3117 gcc_assert (!after || c_dialect_objc ());
3118 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3119 init = c_parser_braced_init (parser, NULL_TREE, true);
3120 else
3121 init = c_parser_expr_no_commas (parser, after);
3122 process_init_element (init);
3125 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3126 C99 6.8.2).
3128 compound-statement:
3129 { block-item-list[opt] }
3130 { label-declarations block-item-list }
3132 block-item-list:
3133 block-item
3134 block-item-list block-item
3136 block-item:
3137 nested-declaration
3138 statement
3140 nested-declaration:
3141 declaration
3143 GNU extensions:
3145 compound-statement:
3146 { label-declarations block-item-list }
3148 nested-declaration:
3149 __extension__ nested-declaration
3150 nested-function-definition
3152 label-declarations:
3153 label-declaration
3154 label-declarations label-declaration
3156 label-declaration:
3157 __label__ identifier-list ;
3159 Allowing the mixing of declarations and code is new in C99. The
3160 GNU syntax also permits (not shown above) labels at the end of
3161 compound statements, which yield an error. We don't allow labels
3162 on declarations; this might seem like a natural extension, but
3163 there would be a conflict between attributes on the label and
3164 prefix attributes on the declaration. ??? The syntax follows the
3165 old parser in requiring something after label declarations.
3166 Although they are erroneous if the labels declared aren't defined,
3167 is it useful for the syntax to be this way? */
3169 static tree
3170 c_parser_compound_statement (c_parser *parser)
3172 tree stmt;
3173 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3174 return error_mark_node;
3175 stmt = c_begin_compound_stmt (true);
3176 c_parser_compound_statement_nostart (parser);
3177 return c_end_compound_stmt (stmt, true);
3180 /* Parse a compound statement except for the opening brace. This is
3181 used for parsing both compound statements and statement expressions
3182 (which follow different paths to handling the opening). */
3184 static void
3185 c_parser_compound_statement_nostart (c_parser *parser)
3187 bool last_stmt = false;
3188 bool last_label = false;
3189 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3191 c_parser_consume_token (parser);
3192 return;
3194 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3196 /* Read zero or more forward-declarations for labels that nested
3197 functions can jump to. */
3198 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3200 c_parser_consume_token (parser);
3201 /* Any identifiers, including those declared as type names,
3202 are OK here. */
3203 while (true)
3205 tree label;
3206 if (c_parser_next_token_is_not (parser, CPP_NAME))
3208 c_parser_error (parser, "expected identifier");
3209 break;
3211 label
3212 = declare_label (c_parser_peek_token (parser)->value);
3213 C_DECLARED_LABEL_FLAG (label) = 1;
3214 add_stmt (build_stmt (DECL_EXPR, label));
3215 c_parser_consume_token (parser);
3216 if (c_parser_next_token_is (parser, CPP_COMMA))
3217 c_parser_consume_token (parser);
3218 else
3219 break;
3221 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3223 /* ??? Locating this diagnostic on the token after the
3224 declarations end follows the old parser, but it might be
3225 better to locate it where the declarations start instead. */
3226 if (pedantic)
3227 pedwarn ("ISO C forbids label declarations");
3229 /* We must now have at least one statement, label or declaration. */
3230 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3232 c_parser_error (parser, "expected declaration or statement");
3233 c_parser_consume_token (parser);
3234 return;
3236 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3238 location_t loc = c_parser_peek_token (parser)->location;
3239 if (c_parser_next_token_is (parser, CPP_EOF))
3241 parser->error = true;
3242 return;
3244 if (c_parser_next_token_is_keyword (parser, RID_CASE)
3245 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3246 || (c_parser_next_token_is (parser, CPP_NAME)
3247 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3249 last_label = true;
3250 last_stmt = false;
3251 c_parser_label (parser);
3253 else if (!last_label
3254 && c_parser_next_token_starts_declspecs (parser))
3256 last_label = false;
3257 c_parser_declaration_or_fndef (parser, true, true, true, true);
3258 if (last_stmt
3259 && ((pedantic && !flag_isoc99)
3260 || warn_declaration_after_statement))
3261 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3262 &loc);
3263 last_stmt = false;
3265 else if (!last_label
3266 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3268 /* __extension__ can start a declaration, but is also an
3269 unary operator that can start an expression. Consume all
3270 but the last of a possible series of __extension__ to
3271 determine which. */
3272 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3273 && (c_parser_peek_2nd_token (parser)->keyword
3274 == RID_EXTENSION))
3275 c_parser_consume_token (parser);
3276 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3278 int ext;
3279 ext = disable_extension_diagnostics ();
3280 c_parser_consume_token (parser);
3281 last_label = false;
3282 c_parser_declaration_or_fndef (parser, true, true, true, true);
3283 /* Following the old parser, __extension__ does not
3284 disable this diagnostic. */
3285 restore_extension_diagnostics (ext);
3286 if (last_stmt
3287 && ((pedantic && !flag_isoc99)
3288 || warn_declaration_after_statement))
3289 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3290 &loc);
3291 last_stmt = false;
3293 else
3294 goto statement;
3296 else
3298 statement:
3299 last_label = false;
3300 last_stmt = true;
3301 c_parser_statement_after_labels (parser);
3304 if (last_label)
3305 error ("label at end of compound statement");
3306 c_parser_consume_token (parser);
3309 /* Parse a label (C90 6.6.1, C99 6.8.1).
3311 label:
3312 identifier : attributes[opt]
3313 case constant-expression :
3314 default :
3316 GNU extensions:
3318 label:
3319 case constant-expression ... constant-expression :
3321 The use of attributes on labels is a GNU extension. The syntax in
3322 GNU C accepts any expressions without commas, non-constant
3323 expressions being rejected later. */
3325 static void
3326 c_parser_label (c_parser *parser)
3328 location_t loc1 = c_parser_peek_token (parser)->location;
3329 tree label = NULL_TREE;
3330 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3332 tree exp1, exp2;
3333 c_parser_consume_token (parser);
3334 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3335 if (c_parser_next_token_is (parser, CPP_COLON))
3337 c_parser_consume_token (parser);
3338 label = do_case (exp1, NULL_TREE);
3340 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3342 c_parser_consume_token (parser);
3343 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3344 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3345 label = do_case (exp1, exp2);
3347 else
3348 c_parser_error (parser, "expected %<:%> or %<...%>");
3350 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3352 c_parser_consume_token (parser);
3353 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3354 label = do_case (NULL_TREE, NULL_TREE);
3356 else
3358 tree name = c_parser_peek_token (parser)->value;
3359 tree tlab;
3360 location_t loc2;
3361 tree attrs;
3362 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3363 c_parser_consume_token (parser);
3364 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3365 loc2 = c_parser_peek_token (parser)->location;
3366 c_parser_consume_token (parser);
3367 attrs = c_parser_attributes (parser);
3368 tlab = define_label (loc2, name);
3369 if (tlab)
3371 decl_attributes (&tlab, attrs, 0);
3372 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3375 if (label)
3376 SET_EXPR_LOCATION (label, loc1);
3379 /* Parse a statement (C90 6.6, C99 6.8).
3381 statement:
3382 labeled-statement
3383 compound-statement
3384 expression-statement
3385 selection-statement
3386 iteration-statement
3387 jump-statement
3389 labeled-statement:
3390 label statement
3392 expression-statement:
3393 expression[opt] ;
3395 selection-statement:
3396 if-statement
3397 switch-statement
3399 iteration-statement:
3400 while-statement
3401 do-statement
3402 for-statement
3404 jump-statement:
3405 goto identifier ;
3406 continue ;
3407 break ;
3408 return expression[opt] ;
3410 GNU extensions:
3412 statement:
3413 asm-statement
3415 jump-statement:
3416 goto * expression ;
3418 Objective-C:
3420 statement:
3421 objc-throw-statement
3422 objc-try-catch-statement
3423 objc-synchronized-statement
3425 objc-throw-statement:
3426 @throw expression ;
3427 @throw ;
3430 static void
3431 c_parser_statement (c_parser *parser)
3433 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3434 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3435 || (c_parser_next_token_is (parser, CPP_NAME)
3436 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3437 c_parser_label (parser);
3438 c_parser_statement_after_labels (parser);
3441 /* Parse a statement, other than a labeled statement. */
3443 static void
3444 c_parser_statement_after_labels (c_parser *parser)
3446 location_t loc = c_parser_peek_token (parser)->location;
3447 tree stmt = NULL_TREE;
3448 switch (c_parser_peek_token (parser)->type)
3450 case CPP_OPEN_BRACE:
3451 add_stmt (c_parser_compound_statement (parser));
3452 break;
3453 case CPP_KEYWORD:
3454 switch (c_parser_peek_token (parser)->keyword)
3456 case RID_IF:
3457 c_parser_if_statement (parser);
3458 break;
3459 case RID_SWITCH:
3460 c_parser_switch_statement (parser);
3461 break;
3462 case RID_WHILE:
3463 c_parser_while_statement (parser);
3464 break;
3465 case RID_DO:
3466 c_parser_do_statement (parser);
3467 break;
3468 case RID_FOR:
3469 c_parser_for_statement (parser);
3470 break;
3471 case RID_GOTO:
3472 c_parser_consume_token (parser);
3473 if (c_parser_next_token_is (parser, CPP_NAME))
3475 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3476 c_parser_consume_token (parser);
3478 else if (c_parser_next_token_is (parser, CPP_MULT))
3480 c_parser_consume_token (parser);
3481 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3483 else
3484 c_parser_error (parser, "expected identifier or %<*%>");
3485 goto expect_semicolon;
3486 case RID_CONTINUE:
3487 c_parser_consume_token (parser);
3488 stmt = c_finish_bc_stmt (&c_cont_label, false);
3489 goto expect_semicolon;
3490 case RID_BREAK:
3491 c_parser_consume_token (parser);
3492 stmt = c_finish_bc_stmt (&c_break_label, true);
3493 goto expect_semicolon;
3494 case RID_RETURN:
3495 c_parser_consume_token (parser);
3496 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3498 stmt = c_finish_return (NULL_TREE);
3499 c_parser_consume_token (parser);
3501 else
3503 stmt = c_finish_return (c_parser_expression (parser).value);
3504 goto expect_semicolon;
3506 break;
3507 case RID_ASM:
3508 stmt = c_parser_asm_statement (parser);
3509 break;
3510 case RID_AT_THROW:
3511 gcc_assert (c_dialect_objc ());
3512 c_parser_consume_token (parser);
3513 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3515 stmt = objc_build_throw_stmt (NULL_TREE);
3516 c_parser_consume_token (parser);
3518 else
3520 stmt
3521 = objc_build_throw_stmt (c_parser_expression (parser).value);
3522 goto expect_semicolon;
3524 break;
3525 case RID_AT_TRY:
3526 gcc_assert (c_dialect_objc ());
3527 c_parser_objc_try_catch_statement (parser);
3528 break;
3529 case RID_AT_SYNCHRONIZED:
3530 gcc_assert (c_dialect_objc ());
3531 c_parser_objc_synchronized_statement (parser);
3532 break;
3533 default:
3534 goto expr_stmt;
3536 break;
3537 case CPP_SEMICOLON:
3538 c_parser_consume_token (parser);
3539 break;
3540 case CPP_CLOSE_PAREN:
3541 case CPP_CLOSE_SQUARE:
3542 /* Avoid infinite loop in error recovery:
3543 c_parser_skip_until_found stops at a closing nesting
3544 delimiter without consuming it, but here we need to consume
3545 it to proceed further. */
3546 c_parser_error (parser, "expected statement");
3547 c_parser_consume_token (parser);
3548 break;
3549 default:
3550 expr_stmt:
3551 stmt = c_finish_expr_stmt (c_parser_expression (parser).value);
3552 expect_semicolon:
3553 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3554 break;
3556 /* Two cases cannot and do not have line numbers associated: If stmt
3557 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3558 cannot hold line numbers. But that's OK because the statement
3559 will either be changed to a MODIFY_EXPR during gimplification of
3560 the statement expr, or discarded. If stmt was compound, but
3561 without new variables, we will have skipped the creation of a
3562 BIND and will have a bare STATEMENT_LIST. But that's OK because
3563 (recursively) all of the component statements should already have
3564 line numbers assigned. ??? Can we discard no-op statements
3565 earlier? */
3566 if (stmt && EXPR_P (stmt))
3567 SET_EXPR_LOCATION (stmt, loc);
3570 /* Parse a parenthesized condition from an if, do or while statement.
3572 condition:
3573 ( expression )
3575 static tree
3576 c_parser_paren_condition (c_parser *parser)
3578 location_t loc;
3579 tree cond;
3580 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3581 return error_mark_node;
3582 loc = c_parser_peek_token (parser)->location;
3583 cond = lang_hooks.truthvalue_conversion (c_parser_expression (parser).value);
3584 if (EXPR_P (cond))
3585 SET_EXPR_LOCATION (cond, loc);
3586 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3587 return cond;
3590 /* Parse a statement which is a block in C99. */
3592 static tree
3593 c_parser_c99_block_statement (c_parser *parser)
3595 tree block = c_begin_compound_stmt (flag_isoc99);
3596 c_parser_statement (parser);
3597 return c_end_compound_stmt (block, flag_isoc99);
3600 /* Parse the body of an if statement or the else half thereof. This
3601 is just parsing a statement but (a) it is a block in C99, (b) we
3602 track whether the body is an if statement for the sake of
3603 -Wparentheses warnings, (c) we handle an empty body specially for
3604 the sake of -Wextra warnings. */
3606 static tree
3607 c_parser_if_body (c_parser *parser, bool *if_p)
3609 tree block = c_begin_compound_stmt (flag_isoc99);
3610 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3611 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3612 || (c_parser_next_token_is (parser, CPP_NAME)
3613 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3614 c_parser_label (parser);
3615 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3616 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3617 add_stmt (build (NOP_EXPR, NULL_TREE, NULL_TREE));
3618 c_parser_statement_after_labels (parser);
3619 return c_end_compound_stmt (block, flag_isoc99);
3622 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3624 if-statement:
3625 if ( expression ) statement
3626 if ( expression ) statement else statement
3629 static void
3630 c_parser_if_statement (c_parser *parser)
3632 tree block;
3633 location_t loc;
3634 tree cond;
3635 bool first_if = false, second_if = false;
3636 tree first_body, second_body;
3637 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3638 c_parser_consume_token (parser);
3639 block = c_begin_compound_stmt (flag_isoc99);
3640 loc = c_parser_peek_token (parser)->location;
3641 cond = c_parser_paren_condition (parser);
3642 first_body = c_parser_if_body (parser, &first_if);
3643 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3645 c_parser_consume_token (parser);
3646 second_body = c_parser_if_body (parser, &second_if);
3648 else
3649 second_body = NULL_TREE;
3650 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3651 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3654 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3656 switch-statement:
3657 switch (expression) statement
3660 static void
3661 c_parser_switch_statement (c_parser *parser)
3663 tree block, expr, body, save_break;
3664 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3665 c_parser_consume_token (parser);
3666 block = c_begin_compound_stmt (flag_isoc99);
3667 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3669 expr = c_parser_expression (parser).value;
3670 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3672 else
3673 expr = error_mark_node;
3674 c_start_case (expr);
3675 save_break = c_break_label;
3676 c_break_label = NULL_TREE;
3677 body = c_parser_c99_block_statement (parser);
3678 c_finish_case (body);
3679 if (c_break_label)
3680 add_stmt (build (LABEL_EXPR, void_type_node, c_break_label));
3681 c_break_label = save_break;
3682 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3685 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3687 while-statement:
3688 while (expression) statement
3691 static void
3692 c_parser_while_statement (c_parser *parser)
3694 tree block, cond, body, save_break, save_cont;
3695 location_t loc;
3696 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3697 c_parser_consume_token (parser);
3698 block = c_begin_compound_stmt (flag_isoc99);
3699 loc = c_parser_peek_token (parser)->location;
3700 cond = c_parser_paren_condition (parser);
3701 save_break = c_break_label;
3702 c_break_label = NULL_TREE;
3703 save_cont = c_cont_label;
3704 c_cont_label = NULL_TREE;
3705 body = c_parser_c99_block_statement (parser);
3706 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3707 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3708 c_break_label = save_break;
3709 c_cont_label = save_cont;
3712 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3714 do-statement:
3715 do statement while ( expression ) ;
3718 static void
3719 c_parser_do_statement (c_parser *parser)
3721 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3722 location_t loc;
3723 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3724 c_parser_consume_token (parser);
3725 block = c_begin_compound_stmt (flag_isoc99);
3726 loc = c_parser_peek_token (parser)->location;
3727 save_break = c_break_label;
3728 c_break_label = NULL_TREE;
3729 save_cont = c_cont_label;
3730 c_cont_label = NULL_TREE;
3731 body = c_parser_c99_block_statement (parser);
3732 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3733 new_break = c_break_label;
3734 c_break_label = save_break;
3735 new_cont = c_cont_label;
3736 c_cont_label = save_cont;
3737 cond = c_parser_paren_condition (parser);
3738 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3739 c_parser_skip_to_end_of_block_or_statement (parser);
3740 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3741 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3744 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3746 for-statement:
3747 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3748 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3750 The form with a declaration is new in C99.
3752 ??? In accordance with the old parser, the declaration may be a
3753 nested function, which is then rejected in check_for_loop_decls,
3754 but does it make any sense for this to be included in the grammar?
3755 Note in particular that the nested function does not include a
3756 trailing ';', whereas the "declaration" production includes one.
3757 Also, can we reject bad declarations earlier and cheaper than
3758 check_for_loop_decls? */
3760 static void
3761 c_parser_for_statement (c_parser *parser)
3763 tree block, cond, incr, save_break, save_cont, body;
3764 location_t loc = UNKNOWN_LOCATION;
3765 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3766 c_parser_consume_token (parser);
3767 block = c_begin_compound_stmt (flag_isoc99);
3768 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3770 /* Parse the initialization declaration or expression. */
3771 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3773 c_parser_consume_token (parser);
3774 c_finish_expr_stmt (NULL_TREE);
3776 else if (c_parser_next_token_starts_declspecs (parser))
3778 c_parser_declaration_or_fndef (parser, true, true, true, true);
3779 check_for_loop_decls ();
3781 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3783 /* __extension__ can start a declaration, but is also an
3784 unary operator that can start an expression. Consume all
3785 but the last of a possible series of __extension__ to
3786 determine which. */
3787 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3788 && (c_parser_peek_2nd_token (parser)->keyword
3789 == RID_EXTENSION))
3790 c_parser_consume_token (parser);
3791 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3793 int ext;
3794 ext = disable_extension_diagnostics ();
3795 c_parser_consume_token (parser);
3796 c_parser_declaration_or_fndef (parser, true, true, true, true);
3797 restore_extension_diagnostics (ext);
3798 check_for_loop_decls ();
3800 else
3801 goto init_expr;
3803 else
3805 init_expr:
3806 c_finish_expr_stmt (c_parser_expression (parser).value);
3807 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3809 /* Parse the loop condition. */
3810 loc = c_parser_peek_token (parser)->location;
3811 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3813 c_parser_consume_token (parser);
3814 cond = NULL_TREE;
3816 else
3818 tree ocond = c_parser_expression (parser).value;
3819 cond = lang_hooks.truthvalue_conversion (ocond);
3820 if (EXPR_P (cond))
3821 SET_EXPR_LOCATION (cond, loc);
3822 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3824 /* Parse the increment expression. */
3825 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3826 incr = c_process_expr_stmt (NULL_TREE);
3827 else
3828 incr = c_process_expr_stmt (c_parser_expression (parser).value);
3829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3831 else
3833 cond = error_mark_node;
3834 incr = error_mark_node;
3836 save_break = c_break_label;
3837 c_break_label = NULL_TREE;
3838 save_cont = c_cont_label;
3839 c_cont_label = NULL_TREE;
3840 body = c_parser_c99_block_statement (parser);
3841 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3842 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3843 c_break_label = save_break;
3844 c_cont_label = save_cont;
3847 /* Parse an asm statement, a GNU extension. This is a full-blown asm
3848 statement with inputs, outputs, clobbers, and volatile tag
3849 allowed.
3851 asm-statement:
3852 asm type-qualifier[opt] ( asm-argument ) ;
3854 asm-argument:
3855 asm-string-literal
3856 asm-string-literal : asm-operands[opt]
3857 asm-string-literal : asm-operands[opt] : asm-operands[opt]
3858 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3860 Qualifiers other than volatile are accepted in the syntax but
3861 warned for. */
3863 static tree
3864 c_parser_asm_statement (c_parser *parser)
3866 tree quals, str, outputs, inputs, clobbers, ret;
3867 bool simple;
3868 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3869 c_parser_consume_token (parser);
3870 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3872 quals = c_parser_peek_token (parser)->value;
3873 c_parser_consume_token (parser);
3875 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3876 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3878 warning ("%E qualifier ignored on asm",
3879 c_parser_peek_token (parser)->value);
3880 quals = NULL_TREE;
3881 c_parser_consume_token (parser);
3883 else
3884 quals = NULL_TREE;
3885 /* ??? Follow the C++ parser rather than using the
3886 c_lex_string_translate kludge. */
3887 c_lex_string_translate = 0;
3888 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3890 c_lex_string_translate = 1;
3891 return NULL_TREE;
3893 str = c_parser_asm_string_literal (parser);
3894 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3896 simple = true;
3897 outputs = NULL_TREE;
3898 inputs = NULL_TREE;
3899 clobbers = NULL_TREE;
3900 goto done_asm;
3902 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3904 c_lex_string_translate = 1;
3905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3906 return NULL_TREE;
3908 simple = false;
3909 /* Parse outputs. */
3910 if (c_parser_next_token_is (parser, CPP_COLON)
3911 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3912 outputs = NULL_TREE;
3913 else
3914 outputs = c_parser_asm_operands (parser);
3915 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3917 inputs = NULL_TREE;
3918 clobbers = NULL_TREE;
3919 goto done_asm;
3921 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3923 c_lex_string_translate = 1;
3924 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3925 return NULL_TREE;
3927 /* Parse inputs. */
3928 if (c_parser_next_token_is (parser, CPP_COLON)
3929 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3930 inputs = NULL_TREE;
3931 else
3932 inputs = c_parser_asm_operands (parser);
3933 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3935 clobbers = NULL_TREE;
3936 goto done_asm;
3938 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3940 c_lex_string_translate = 1;
3941 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3942 return NULL_TREE;
3944 /* Parse clobbers. */
3945 clobbers = c_parser_asm_clobbers (parser);
3946 done_asm:
3947 c_lex_string_translate = 1;
3948 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3951 return NULL_TREE;
3953 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3954 c_parser_skip_to_end_of_block_or_statement (parser);
3955 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
3956 clobbers, simple));
3957 return ret;
3960 /* Parse asm operands, a GNU extension.
3962 asm-operands:
3963 asm-operand
3964 asm-operands , asm-operand
3966 asm-operand:
3967 asm-string-literal ( expression )
3968 [ identifier ] asm-string-literal ( expression )
3971 static tree
3972 c_parser_asm_operands (c_parser *parser)
3974 tree list = NULL_TREE;
3975 while (true)
3977 tree name, str, expr;
3978 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3980 c_parser_consume_token (parser);
3981 if (c_parser_next_token_is (parser, CPP_NAME))
3983 tree id = c_parser_peek_token (parser)->value;
3984 c_parser_consume_token (parser);
3985 name = build_string (IDENTIFIER_LENGTH (id),
3986 IDENTIFIER_POINTER (id));
3988 else
3990 c_parser_error (parser, "expected identifier");
3991 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3992 return NULL_TREE;
3994 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3995 "expected %<]%>");
3997 else
3998 name = NULL_TREE;
3999 str = c_parser_asm_string_literal (parser);
4000 if (str == NULL_TREE)
4001 return NULL_TREE;
4002 c_lex_string_translate = 1;
4003 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4005 c_lex_string_translate = 0;
4006 return NULL_TREE;
4008 expr = c_parser_expression (parser).value;
4009 c_lex_string_translate = 0;
4010 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4012 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4013 return NULL_TREE;
4015 list = chainon (list, build_tree_list (build_tree_list (name, str),
4016 expr));
4017 if (c_parser_next_token_is (parser, CPP_COMMA))
4018 c_parser_consume_token (parser);
4019 else
4020 break;
4022 return list;
4025 /* Parse asm clobbers, a GNU extension.
4027 asm-clobbers:
4028 asm-string-literal
4029 asm-clobbers , asm-string-literal
4032 static tree
4033 c_parser_asm_clobbers (c_parser *parser)
4035 tree list = NULL_TREE;
4036 while (true)
4038 tree str = c_parser_asm_string_literal (parser);
4039 if (str)
4040 list = tree_cons (NULL_TREE, str, list);
4041 else
4042 return NULL_TREE;
4043 if (c_parser_next_token_is (parser, CPP_COMMA))
4044 c_parser_consume_token (parser);
4045 else
4046 break;
4048 return list;
4051 /* Parse an expression other than a compound expression; that is, an
4052 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4053 NULL then it is an Objective-C message expression which is the
4054 primary-expression starting the expression as an initializer.
4056 assignment-expression:
4057 conditional-expression
4058 unary-expression assignment-operator assignment-expression
4060 assignment-operator: one of
4061 = *= /= %= += -= <<= >>= &= ^= |=
4063 In GNU C we accept any conditional expression on the LHS and
4064 diagnose the invalid lvalue rather than producing a syntax
4065 error. */
4067 static struct c_expr
4068 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4070 struct c_expr lhs, rhs, ret;
4071 enum tree_code code;
4072 gcc_assert (!after || c_dialect_objc ());
4073 lhs = c_parser_conditional_expression (parser, after);
4074 switch (c_parser_peek_token (parser)->type)
4076 case CPP_EQ:
4077 code = NOP_EXPR;
4078 break;
4079 case CPP_MULT_EQ:
4080 code = MULT_EXPR;
4081 break;
4082 case CPP_DIV_EQ:
4083 code = TRUNC_DIV_EXPR;
4084 break;
4085 case CPP_MOD_EQ:
4086 code = TRUNC_MOD_EXPR;
4087 break;
4088 case CPP_PLUS_EQ:
4089 code = PLUS_EXPR;
4090 break;
4091 case CPP_MINUS_EQ:
4092 code = MINUS_EXPR;
4093 break;
4094 case CPP_LSHIFT_EQ:
4095 code = LSHIFT_EXPR;
4096 break;
4097 case CPP_RSHIFT_EQ:
4098 code = RSHIFT_EXPR;
4099 break;
4100 case CPP_AND_EQ:
4101 code = BIT_AND_EXPR;
4102 break;
4103 case CPP_XOR_EQ:
4104 code = BIT_XOR_EXPR;
4105 break;
4106 case CPP_OR_EQ:
4107 code = BIT_IOR_EXPR;
4108 break;
4109 default:
4110 return lhs;
4112 c_parser_consume_token (parser);
4113 rhs = c_parser_expr_no_commas (parser, NULL);
4114 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4115 if (code == NOP_EXPR)
4116 ret.original_code = MODIFY_EXPR;
4117 else
4119 TREE_NO_WARNING (ret.value) = 1;
4120 ret.original_code = ERROR_MARK;
4122 return ret;
4125 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4126 is not NULL then it is an Objective-C message expression which is
4127 the primary-expression starting the expression as an initializer.
4129 conditional-expression:
4130 logical-OR-expression
4131 logical-OR-expression ? expression : conditional-expression
4133 GNU extensions:
4135 conditional-expression:
4136 logical-OR-expression ? : conditional-expression
4139 static struct c_expr
4140 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4142 struct c_expr cond, exp1, exp2, ret;
4143 gcc_assert (!after || c_dialect_objc ());
4144 cond = c_parser_binary_expression (parser, after);
4145 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4146 return cond;
4147 c_parser_consume_token (parser);
4148 if (c_parser_next_token_is (parser, CPP_COLON))
4150 if (pedantic)
4151 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4152 /* Make sure first operand is calculated only once. */
4153 exp1.value = save_expr (default_conversion (cond.value));
4154 cond.value = lang_hooks.truthvalue_conversion (exp1.value);
4155 skip_evaluation += cond.value == truthvalue_true_node;
4157 else
4159 cond.value
4160 = lang_hooks.truthvalue_conversion (default_conversion (cond.value));
4161 skip_evaluation += cond.value == truthvalue_false_node;
4162 exp1 = c_parser_expression (parser);
4163 skip_evaluation += ((cond.value == truthvalue_true_node)
4164 - (cond.value == truthvalue_false_node));
4166 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4168 skip_evaluation -= cond.value == truthvalue_true_node;
4169 ret.value = error_mark_node;
4170 ret.original_code = ERROR_MARK;
4171 return ret;
4173 exp2 = c_parser_conditional_expression (parser, NULL);
4174 skip_evaluation -= cond.value == truthvalue_true_node;
4175 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4176 ret.original_code = ERROR_MARK;
4177 return ret;
4180 /* Parse a binary expression; that is, a logical-OR-expression (C90
4181 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4182 an Objective-C message expression which is the primary-expression
4183 starting the expression as an initializer.
4185 multiplicative-expression:
4186 cast-expression
4187 multiplicative-expression * cast-expression
4188 multiplicative-expression / cast-expression
4189 multiplicative-expression % cast-expression
4191 additive-expression:
4192 multiplicative-expression
4193 additive-expression + multiplicative-expression
4194 additive-expression - multiplicative-expression
4196 shift-expression:
4197 additive-expression
4198 shift-expression << additive-expression
4199 shift-expression >> additive-expression
4201 relational-expression:
4202 shift-expression
4203 relational-expression < shift-expression
4204 relational-expression > shift-expression
4205 relational-expression <= shift-expression
4206 relational-expression >= shift-expression
4208 equality-expression:
4209 relational-expression
4210 equality-expression == relational-expression
4211 equality-expression != relational-expression
4213 AND-expression:
4214 equality-expression
4215 AND-expression & equality-expression
4217 exclusive-OR-expression:
4218 AND-expression
4219 exclusive-OR-expression ^ AND-expression
4221 inclusive-OR-expression:
4222 exclusive-OR-expression
4223 inclusive-OR-expression | exclusive-OR-expression
4225 logical-AND-expression:
4226 inclusive-OR-expression
4227 logical-AND-expression && inclusive-OR-expression
4229 logical-OR-expression:
4230 logical-AND-expression
4231 logical-OR-expression || logical-AND-expression
4234 static struct c_expr
4235 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4237 /* A binary expression is parsed using operator-precedence parsing,
4238 with the operands being cast expressions. All the binary
4239 operators are left-associative. Thus a binary expression is of
4240 form:
4242 E0 op1 E1 op2 E2 ...
4244 which we represent on a stack. On the stack, the precedence
4245 levels are strictly increasing. When a new operator is
4246 encountered of higher precedence than that at the top of the
4247 stack, it is pushed; its LHS is the top expression, and its RHS
4248 is everything parsed until it is popped. When a new operator is
4249 encountered with precedence less than or equal to that at the top
4250 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4251 by the result of the operation until the operator at the top of
4252 the stack has lower precedence than the new operator or there is
4253 only one element on the stack; then the top expression is the LHS
4254 of the new operator. In the case of logical AND and OR
4255 expressions, we also need to adjust skip_evaluation as
4256 appropriate when the operators are pushed and popped. */
4258 /* The precedence levels, where 0 is a dummy lowest level used for
4259 the bottom of the stack. */
4260 enum prec {
4261 PREC_NONE,
4262 PREC_LOGOR,
4263 PREC_LOGAND,
4264 PREC_BITOR,
4265 PREC_BITXOR,
4266 PREC_BITAND,
4267 PREC_EQ,
4268 PREC_REL,
4269 PREC_SHIFT,
4270 PREC_ADD,
4271 PREC_MULT,
4272 NUM_PRECS
4274 struct {
4275 /* The expression at this stack level. */
4276 struct c_expr expr;
4277 /* The precedence of the operator on its left, PREC_NONE at the
4278 bottom of the stack. */
4279 enum prec prec;
4280 /* The operation on its left. */
4281 enum tree_code op;
4282 } stack[NUM_PRECS];
4283 int sp;
4284 #define POP \
4285 do { \
4286 switch (stack[sp].op) \
4288 case TRUTH_ANDIF_EXPR: \
4289 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4290 break; \
4291 case TRUTH_ORIF_EXPR: \
4292 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4293 break; \
4294 default: \
4295 break; \
4297 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4298 stack[sp - 1].expr, \
4299 stack[sp].expr); \
4300 sp--; \
4301 } while (0)
4302 gcc_assert (!after || c_dialect_objc ());
4303 stack[0].expr = c_parser_cast_expression (parser, after);
4304 stack[0].prec = PREC_NONE;
4305 sp = 0;
4306 while (true)
4308 enum prec oprec;
4309 enum tree_code ocode;
4310 if (parser->error)
4311 goto out;
4312 switch (c_parser_peek_token (parser)->type)
4314 case CPP_MULT:
4315 oprec = PREC_MULT;
4316 ocode = MULT_EXPR;
4317 break;
4318 case CPP_DIV:
4319 oprec = PREC_MULT;
4320 ocode = TRUNC_DIV_EXPR;
4321 break;
4322 case CPP_MOD:
4323 oprec = PREC_MULT;
4324 ocode = TRUNC_MOD_EXPR;
4325 break;
4326 case CPP_PLUS:
4327 oprec = PREC_ADD;
4328 ocode = PLUS_EXPR;
4329 break;
4330 case CPP_MINUS:
4331 oprec = PREC_ADD;
4332 ocode = MINUS_EXPR;
4333 break;
4334 case CPP_LSHIFT:
4335 oprec = PREC_SHIFT;
4336 ocode = LSHIFT_EXPR;
4337 break;
4338 case CPP_RSHIFT:
4339 oprec = PREC_SHIFT;
4340 ocode = RSHIFT_EXPR;
4341 break;
4342 case CPP_LESS:
4343 oprec = PREC_REL;
4344 ocode = LT_EXPR;
4345 break;
4346 case CPP_GREATER:
4347 oprec = PREC_REL;
4348 ocode = GT_EXPR;
4349 break;
4350 case CPP_LESS_EQ:
4351 oprec = PREC_REL;
4352 ocode = LE_EXPR;
4353 break;
4354 case CPP_GREATER_EQ:
4355 oprec = PREC_REL;
4356 ocode = GE_EXPR;
4357 break;
4358 case CPP_EQ_EQ:
4359 oprec = PREC_EQ;
4360 ocode = EQ_EXPR;
4361 break;
4362 case CPP_NOT_EQ:
4363 oprec = PREC_EQ;
4364 ocode = NE_EXPR;
4365 break;
4366 case CPP_AND:
4367 oprec = PREC_BITAND;
4368 ocode = BIT_AND_EXPR;
4369 break;
4370 case CPP_XOR:
4371 oprec = PREC_BITXOR;
4372 ocode = BIT_XOR_EXPR;
4373 break;
4374 case CPP_OR:
4375 oprec = PREC_BITOR;
4376 ocode = BIT_IOR_EXPR;
4377 break;
4378 case CPP_AND_AND:
4379 oprec = PREC_LOGAND;
4380 ocode = TRUTH_ANDIF_EXPR;
4381 break;
4382 case CPP_OR_OR:
4383 oprec = PREC_LOGOR;
4384 ocode = TRUTH_ORIF_EXPR;
4385 break;
4386 default:
4387 /* Not a binary operator, so end of the binary
4388 expression. */
4389 goto out;
4391 c_parser_consume_token (parser);
4392 while (oprec <= stack[sp].prec)
4393 POP;
4394 switch (ocode)
4396 case TRUTH_ANDIF_EXPR:
4397 stack[sp].expr.value = lang_hooks.truthvalue_conversion
4398 (default_conversion (stack[sp].expr.value));
4399 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4400 break;
4401 case TRUTH_ORIF_EXPR:
4402 stack[sp].expr.value = lang_hooks.truthvalue_conversion
4403 (default_conversion (stack[sp].expr.value));
4404 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4405 break;
4406 default:
4407 break;
4409 sp++;
4410 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4411 stack[sp].prec = oprec;
4412 stack[sp].op = ocode;
4414 out:
4415 while (sp > 0)
4416 POP;
4417 return stack[0].expr;
4418 #undef POP
4421 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4422 NULL then it is an Objective-C message expression which is the
4423 primary-expression starting the expression as an initializer.
4425 cast-expression:
4426 unary-expression
4427 ( type-name ) unary-expression
4430 static struct c_expr
4431 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4433 gcc_assert (!after || c_dialect_objc ());
4434 if (after)
4435 return c_parser_postfix_expression_after_primary (parser, *after);
4436 /* If the expression begins with a parenthesized type name, it may
4437 be either a cast or a compound literal; we need to see whether
4438 the next character is '{' to tell the difference. If not, it is
4439 an unary expression. */
4440 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4441 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4443 struct c_type_name *type_name;
4444 struct c_expr ret;
4445 tree expr;
4446 c_parser_consume_token (parser);
4447 type_name = c_parser_type_name (parser);
4448 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4449 if (type_name == NULL)
4451 ret.value = error_mark_node;
4452 ret.original_code = ERROR_MARK;
4453 return ret;
4455 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4456 return c_parser_postfix_expression_after_paren_type (parser,
4457 type_name);
4458 expr = c_parser_cast_expression (parser, NULL).value;
4459 ret.value = c_cast_expr (type_name, expr);
4460 ret.original_code = ERROR_MARK;
4461 return ret;
4463 else
4464 return c_parser_unary_expression (parser);
4467 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4469 unary-expression:
4470 postfix-expression
4471 ++ unary-expression
4472 -- unary-expression
4473 unary-operator cast-expression
4474 sizeof unary-expression
4475 sizeof ( type-name )
4477 unary-operator: one of
4478 & * + - ~ !
4480 GNU extensions:
4482 unary-expression:
4483 __alignof__ unary-expression
4484 __alignof__ ( type-name )
4485 && identifier
4487 unary-operator: one of
4488 __extension__ __real__ __imag__
4490 In addition, the GNU syntax treats ++ and -- as unary operators, so
4491 they may be applied to cast expressions with errors for non-lvalues
4492 given later. */
4494 static struct c_expr
4495 c_parser_unary_expression (c_parser *parser)
4497 int ext;
4498 struct c_expr ret;
4499 ret.original_code = ERROR_MARK;
4500 switch (c_parser_peek_token (parser)->type)
4502 case CPP_PLUS_PLUS:
4503 c_parser_consume_token (parser);
4504 ret.value
4505 = build_unary_op (PREINCREMENT_EXPR,
4506 c_parser_cast_expression (parser, NULL).value, 0);
4507 overflow_warning (ret.value);
4508 return ret;
4509 case CPP_MINUS_MINUS:
4510 c_parser_consume_token (parser);
4511 ret.value
4512 = build_unary_op (PREDECREMENT_EXPR,
4513 c_parser_cast_expression (parser, NULL).value, 0);
4514 overflow_warning (ret.value);
4515 return ret;
4516 case CPP_AND:
4517 c_parser_consume_token (parser);
4518 ret.value
4519 = build_unary_op (ADDR_EXPR,
4520 c_parser_cast_expression (parser, NULL).value, 0);
4521 overflow_warning (ret.value);
4522 return ret;
4523 case CPP_MULT:
4524 c_parser_consume_token (parser);
4525 ret.value
4526 = build_indirect_ref (c_parser_cast_expression (parser, NULL).value,
4527 "unary *");
4528 return ret;
4529 case CPP_PLUS:
4530 c_parser_consume_token (parser);
4531 if (!c_dialect_objc () && warn_traditional && !in_system_header)
4532 warning ("traditional C rejects the unary plus operator");
4533 ret.value
4534 = build_unary_op (CONVERT_EXPR,
4535 c_parser_cast_expression (parser, NULL).value, 0);
4536 overflow_warning (ret.value);
4537 return ret;
4538 case CPP_MINUS:
4539 c_parser_consume_token (parser);
4540 ret.value
4541 = build_unary_op (NEGATE_EXPR,
4542 c_parser_cast_expression (parser, NULL).value, 0);
4543 overflow_warning (ret.value);
4544 return ret;
4545 case CPP_COMPL:
4546 c_parser_consume_token (parser);
4547 ret.value
4548 = build_unary_op (BIT_NOT_EXPR,
4549 c_parser_cast_expression (parser, NULL).value, 0);
4550 overflow_warning (ret.value);
4551 return ret;
4552 case CPP_NOT:
4553 c_parser_consume_token (parser);
4554 ret.value
4555 = build_unary_op (TRUTH_NOT_EXPR,
4556 c_parser_cast_expression (parser, NULL).value, 0);
4557 overflow_warning (ret.value);
4558 return ret;
4559 case CPP_AND_AND:
4560 /* Refer to the address of a label as a pointer. */
4561 c_parser_consume_token (parser);
4562 if (c_parser_next_token_is (parser, CPP_NAME))
4564 ret.value = finish_label_address_expr
4565 (c_parser_peek_token (parser)->value);
4566 c_parser_consume_token (parser);
4567 return ret;
4569 else
4571 c_parser_error (parser, "expected identifier");
4572 ret.value = error_mark_node;
4573 return ret;
4575 case CPP_KEYWORD:
4576 switch (c_parser_peek_token (parser)->keyword)
4578 case RID_SIZEOF:
4579 return c_parser_sizeof_expression (parser);
4580 case RID_ALIGNOF:
4581 return c_parser_alignof_expression (parser);
4582 case RID_EXTENSION:
4583 c_parser_consume_token (parser);
4584 ext = disable_extension_diagnostics ();
4585 ret = c_parser_cast_expression (parser, NULL);
4586 restore_extension_diagnostics (ext);
4587 return ret;
4588 case RID_REALPART:
4589 c_parser_consume_token (parser);
4590 ret.value
4591 = build_unary_op (REALPART_EXPR,
4592 c_parser_cast_expression (parser, NULL).value,
4594 return ret;
4595 case RID_IMAGPART:
4596 c_parser_consume_token (parser);
4597 ret.value
4598 = build_unary_op (IMAGPART_EXPR,
4599 c_parser_cast_expression (parser, NULL).value,
4601 return ret;
4602 default:
4603 return c_parser_postfix_expression (parser);
4605 default:
4606 return c_parser_postfix_expression (parser);
4610 /* Parse a sizeof expression. */
4612 static struct c_expr
4613 c_parser_sizeof_expression (c_parser *parser)
4615 struct c_expr expr;
4616 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4617 c_parser_consume_token (parser);
4618 skip_evaluation++;
4619 in_sizeof++;
4620 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4621 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4623 /* Either sizeof ( type-name ) or sizeof unary-expression
4624 starting with a compound literal. */
4625 struct c_type_name *type_name;
4626 c_parser_consume_token (parser);
4627 type_name = c_parser_type_name (parser);
4628 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4629 if (type_name == NULL)
4631 struct c_expr ret;
4632 skip_evaluation--;
4633 in_sizeof--;
4634 ret.value = error_mark_node;
4635 ret.original_code = ERROR_MARK;
4636 return ret;
4638 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4640 expr = c_parser_postfix_expression_after_paren_type (parser,
4641 type_name);
4642 goto sizeof_expr;
4644 /* sizeof ( type-name ). */
4645 skip_evaluation--;
4646 in_sizeof--;
4647 return c_expr_sizeof_type (type_name);
4649 else
4651 expr = c_parser_unary_expression (parser);
4652 sizeof_expr:
4653 skip_evaluation--;
4654 in_sizeof--;
4655 if (TREE_CODE (expr.value) == COMPONENT_REF
4656 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4657 error ("%<sizeof%> applied to a bit-field");
4658 return c_expr_sizeof_expr (expr);
4662 /* Parse an alignof expression. */
4664 static struct c_expr
4665 c_parser_alignof_expression (c_parser *parser)
4667 struct c_expr expr;
4668 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4669 c_parser_consume_token (parser);
4670 skip_evaluation++;
4671 in_alignof++;
4672 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4673 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4675 /* Either __alignof__ ( type-name ) or __alignof__
4676 unary-expression starting with a compound literal. */
4677 struct c_type_name *type_name;
4678 struct c_expr ret;
4679 c_parser_consume_token (parser);
4680 type_name = c_parser_type_name (parser);
4681 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4682 if (type_name == NULL)
4684 struct c_expr ret;
4685 skip_evaluation--;
4686 in_alignof--;
4687 ret.value = error_mark_node;
4688 ret.original_code = ERROR_MARK;
4689 return ret;
4691 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4693 expr = c_parser_postfix_expression_after_paren_type (parser,
4694 type_name);
4695 goto alignof_expr;
4697 /* alignof ( type-name ). */
4698 skip_evaluation--;
4699 in_alignof--;
4700 ret.value = c_alignof (groktypename (type_name));
4701 ret.original_code = ERROR_MARK;
4702 return ret;
4704 else
4706 struct c_expr ret;
4707 expr = c_parser_unary_expression (parser);
4708 alignof_expr:
4709 skip_evaluation--;
4710 in_alignof--;
4711 ret.value = c_alignof_expr (expr.value);
4712 ret.original_code = ERROR_MARK;
4713 return ret;
4717 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4719 postfix-expression:
4720 primary-expression
4721 postfix-expression [ expression ]
4722 postfix-expression ( argument-expression-list[opt] )
4723 postfix-expression . identifier
4724 postfix-expression -> identifier
4725 postfix-expression ++
4726 postfix-expression --
4727 ( type-name ) { initializer-list }
4728 ( type-name ) { initializer-list , }
4730 argument-expression-list:
4731 argument-expression
4732 argument-expression-list , argument-expression
4734 primary-expression:
4735 identifier
4736 constant
4737 string-literal
4738 ( expression )
4740 GNU extensions:
4742 primary-expression:
4743 __func__
4744 (treated as a keyword in GNU C)
4745 __FUNCTION__
4746 __PRETTY_FUNCTION__
4747 ( compound-statement )
4748 __builtin_va_arg ( assignment-expression , type-name )
4749 __builtin_offsetof ( type-name , offsetof-member-designator )
4750 __builtin_choose_expr ( assignment-expression ,
4751 assignment-expression ,
4752 assignment-expression )
4753 __builtin_types_compatible_p ( type-name , type-name )
4755 offsetof-member-designator:
4756 identifier
4757 offsetof-member-designator . identifier
4758 offsetof-member-designator [ expression ]
4760 Objective-C:
4762 primary-expression:
4763 [ objc-receiver objc-message-args ]
4764 @selector ( objc-selector-arg )
4765 @protocol ( identifier )
4766 @encode ( type-name )
4767 objc-string-literal
4770 static struct c_expr
4771 c_parser_postfix_expression (c_parser *parser)
4773 struct c_expr expr, e1, e2, e3;
4774 struct c_type_name *t1, *t2;
4775 switch (c_parser_peek_token (parser)->type)
4777 case CPP_NUMBER:
4778 case CPP_CHAR:
4779 case CPP_WCHAR:
4780 expr.value = c_parser_peek_token (parser)->value;
4781 expr.original_code = ERROR_MARK;
4782 c_parser_consume_token (parser);
4783 break;
4784 case CPP_STRING:
4785 case CPP_WSTRING:
4786 expr.value = c_parser_peek_token (parser)->value;
4787 expr.original_code = STRING_CST;
4788 c_parser_consume_token (parser);
4789 break;
4790 case CPP_OBJC_STRING:
4791 gcc_assert (c_dialect_objc ());
4792 expr.value
4793 = objc_build_string_object (c_parser_peek_token (parser)->value);
4794 expr.original_code = ERROR_MARK;
4795 c_parser_consume_token (parser);
4796 break;
4797 case CPP_NAME:
4798 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4800 c_parser_error (parser, "expected expression");
4801 expr.value = error_mark_node;
4802 expr.original_code = ERROR_MARK;
4803 break;
4806 tree id = c_parser_peek_token (parser)->value;
4807 location_t loc = c_parser_peek_token (parser)->location;
4808 c_parser_consume_token (parser);
4809 expr.value = build_external_ref (id,
4810 (c_parser_peek_token (parser)->type
4811 == CPP_OPEN_PAREN), loc);
4812 expr.original_code = ERROR_MARK;
4814 break;
4815 case CPP_OPEN_PAREN:
4816 /* A parenthesized expression, statement expression or compound
4817 literal. */
4818 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4820 /* A statement expression. */
4821 tree stmt;
4822 c_parser_consume_token (parser);
4823 c_parser_consume_token (parser);
4824 if (cur_stmt_list == NULL)
4826 error ("braced-group within expression allowed "
4827 "only inside a function");
4828 parser->error = true;
4829 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4830 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4831 expr.value = error_mark_node;
4832 expr.original_code = ERROR_MARK;
4833 break;
4835 stmt = c_begin_stmt_expr ();
4836 c_parser_compound_statement_nostart (parser);
4837 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4838 "expected %<)%>");
4839 if (pedantic)
4840 pedwarn ("ISO C forbids braced-groups within expressions");
4841 expr.value = c_finish_stmt_expr (stmt);
4842 expr.original_code = ERROR_MARK;
4844 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4846 /* A compound literal. ??? Can we actually get here rather
4847 than going directly to
4848 c_parser_postfix_expression_after_paren_type from
4849 elsewhere? */
4850 struct c_type_name *type_name;
4851 c_parser_consume_token (parser);
4852 type_name = c_parser_type_name (parser);
4853 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4854 "expected %<)%>");
4855 if (type_name == NULL)
4857 expr.value = error_mark_node;
4858 expr.original_code = ERROR_MARK;
4860 else
4861 expr = c_parser_postfix_expression_after_paren_type (parser,
4862 type_name);
4864 else
4866 /* A parenthesized expression. */
4867 c_parser_consume_token (parser);
4868 expr = c_parser_expression (parser);
4869 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4870 TREE_NO_WARNING (expr.value) = 1;
4871 expr.original_code = ERROR_MARK;
4872 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4873 "expected %<)%>");
4875 break;
4876 case CPP_KEYWORD:
4877 switch (c_parser_peek_token (parser)->keyword)
4879 case RID_FUNCTION_NAME:
4880 case RID_PRETTY_FUNCTION_NAME:
4881 case RID_C99_FUNCTION_NAME:
4882 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4883 c_parser_peek_token (parser)->value);
4884 expr.original_code = ERROR_MARK;
4885 c_parser_consume_token (parser);
4886 break;
4887 case RID_VA_ARG:
4888 c_parser_consume_token (parser);
4889 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4891 expr.value = error_mark_node;
4892 expr.original_code = ERROR_MARK;
4893 break;
4895 e1 = c_parser_expr_no_commas (parser, NULL);
4896 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4899 expr.value = error_mark_node;
4900 expr.original_code = ERROR_MARK;
4901 break;
4903 t1 = c_parser_type_name (parser);
4904 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4905 "expected %<)%>");
4906 if (t1 == NULL)
4908 expr.value = error_mark_node;
4909 expr.original_code = ERROR_MARK;
4911 else
4913 expr.value = build_va_arg (e1.value, groktypename (t1));
4914 expr.original_code = ERROR_MARK;
4916 break;
4917 case RID_OFFSETOF:
4918 c_parser_consume_token (parser);
4919 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4921 expr.value = error_mark_node;
4922 expr.original_code = ERROR_MARK;
4923 break;
4925 t1 = c_parser_type_name (parser);
4926 if (t1 == NULL)
4928 expr.value = error_mark_node;
4929 expr.original_code = ERROR_MARK;
4930 break;
4932 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4934 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4935 expr.value = error_mark_node;
4936 expr.original_code = ERROR_MARK;
4937 break;
4940 tree type = groktypename (t1);
4941 tree offsetof_ref;
4942 if (type == error_mark_node)
4943 offsetof_ref = error_mark_node;
4944 else
4945 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
4946 /* Parse the second argument to __builtin_offsetof. We
4947 must have one identifier, and beyond that we want to
4948 accept sub structure and sub array references. */
4949 if (c_parser_next_token_is (parser, CPP_NAME))
4951 offsetof_ref = build_component_ref
4952 (offsetof_ref, c_parser_peek_token (parser)->value);
4953 c_parser_consume_token (parser);
4954 while (c_parser_next_token_is (parser, CPP_DOT)
4955 || c_parser_next_token_is (parser,
4956 CPP_OPEN_SQUARE))
4958 if (c_parser_next_token_is (parser, CPP_DOT))
4960 c_parser_consume_token (parser);
4961 if (c_parser_next_token_is_not (parser,
4962 CPP_NAME))
4964 c_parser_error (parser, "expected identifier");
4965 break;
4967 offsetof_ref = build_component_ref
4968 (offsetof_ref,
4969 c_parser_peek_token (parser)->value);
4970 c_parser_consume_token (parser);
4972 else
4974 tree idx;
4975 c_parser_consume_token (parser);
4976 idx = c_parser_expression (parser).value;
4977 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4978 "expected %<]%>");
4979 offsetof_ref = build_array_ref (offsetof_ref, idx);
4983 else
4984 c_parser_error (parser, "expected identifier");
4985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4986 "expected %<)%>");
4987 expr.value = fold_offsetof (offsetof_ref);
4988 expr.original_code = ERROR_MARK;
4990 break;
4991 case RID_CHOOSE_EXPR:
4992 c_parser_consume_token (parser);
4993 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4995 expr.value = error_mark_node;
4996 expr.original_code = ERROR_MARK;
4997 break;
4999 e1 = c_parser_expr_no_commas (parser, NULL);
5000 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5002 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5003 expr.value = error_mark_node;
5004 expr.original_code = ERROR_MARK;
5005 break;
5007 e2 = c_parser_expr_no_commas (parser, NULL);
5008 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5010 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5011 expr.value = error_mark_node;
5012 expr.original_code = ERROR_MARK;
5013 break;
5015 e3 = c_parser_expr_no_commas (parser, NULL);
5016 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5017 "expected %<)%>");
5019 tree c;
5021 c = fold (e1.value);
5022 STRIP_NOPS (c);
5023 if (TREE_CODE (c) != INTEGER_CST)
5024 error ("first argument to %<__builtin_choose_expr%> not"
5025 " a constant");
5026 expr = integer_zerop (c) ? e3 : e2;
5028 break;
5029 case RID_TYPES_COMPATIBLE_P:
5030 c_parser_consume_token (parser);
5031 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5033 expr.value = error_mark_node;
5034 expr.original_code = ERROR_MARK;
5035 break;
5037 t1 = c_parser_type_name (parser);
5038 if (t1 == NULL)
5040 expr.value = error_mark_node;
5041 expr.original_code = ERROR_MARK;
5042 break;
5044 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5047 expr.value = error_mark_node;
5048 expr.original_code = ERROR_MARK;
5049 break;
5051 t2 = c_parser_type_name (parser);
5052 if (t2 == NULL)
5054 expr.value = error_mark_node;
5055 expr.original_code = ERROR_MARK;
5056 break;
5058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5059 "expected %<)%>");
5061 tree e1, e2;
5063 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5064 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5066 expr.value = comptypes (e1, e2)
5067 ? build_int_cst (NULL_TREE, 1)
5068 : build_int_cst (NULL_TREE, 0);
5069 expr.original_code = ERROR_MARK;
5071 break;
5072 case RID_AT_SELECTOR:
5073 gcc_assert (c_dialect_objc ());
5074 c_parser_consume_token (parser);
5075 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5077 expr.value = error_mark_node;
5078 expr.original_code = ERROR_MARK;
5079 break;
5082 tree sel = c_parser_objc_selector_arg (parser);
5083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5084 "expected %<)%>");
5085 expr.value = objc_build_selector_expr (sel);
5086 expr.original_code = ERROR_MARK;
5088 break;
5089 case RID_AT_PROTOCOL:
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 if (c_parser_next_token_is_not (parser, CPP_NAME))
5100 c_parser_error (parser, "expected identifier");
5101 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5102 expr.value = error_mark_node;
5103 expr.original_code = ERROR_MARK;
5104 break;
5107 tree id = c_parser_peek_token (parser)->value;
5108 c_parser_consume_token (parser);
5109 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5110 "expected %<)%>");
5111 expr.value = objc_build_protocol_expr (id);
5112 expr.original_code = ERROR_MARK;
5114 break;
5115 case RID_AT_ENCODE:
5116 /* Extension to support C-structures in the archiver. */
5117 gcc_assert (c_dialect_objc ());
5118 c_parser_consume_token (parser);
5119 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5121 expr.value = error_mark_node;
5122 expr.original_code = ERROR_MARK;
5123 break;
5125 t1 = c_parser_type_name (parser);
5126 if (t1 == NULL)
5128 expr.value = error_mark_node;
5129 expr.original_code = ERROR_MARK;
5130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5131 break;
5133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5134 "expected %<)%>");
5136 tree type = groktypename (t1);
5137 expr.value = objc_build_encode_expr (type);
5138 expr.original_code = ERROR_MARK;
5140 break;
5141 default:
5142 c_parser_error (parser, "expected expression");
5143 expr.value = error_mark_node;
5144 expr.original_code = ERROR_MARK;
5145 break;
5147 break;
5148 case CPP_OPEN_SQUARE:
5149 if (c_dialect_objc ())
5151 tree receiver, args;
5152 c_parser_consume_token (parser);
5153 receiver = c_parser_objc_receiver (parser);
5154 args = c_parser_objc_message_args (parser);
5155 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5156 "expected %<]%>");
5157 expr.value = objc_build_message_expr (build_tree_list (receiver,
5158 args));
5159 expr.original_code = ERROR_MARK;
5160 break;
5162 /* Else fall through to report error. */
5163 default:
5164 c_parser_error (parser, "expected expression");
5165 expr.value = error_mark_node;
5166 expr.original_code = ERROR_MARK;
5167 break;
5169 return c_parser_postfix_expression_after_primary (parser, expr);
5172 /* Parse a postfix expression after a parenthesized type name: the
5173 brace-enclosed initializer of a compound literal, possibly followed
5174 by some postfix operators. This is separate because it is not
5175 possible to tell until after the type name whether a cast
5176 expression has a cast or a compound literal, or whether the operand
5177 of sizeof is a parenthesized type name or starts with a compound
5178 literal. */
5180 static struct c_expr
5181 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5182 struct c_type_name *type_name)
5184 tree type;
5185 struct c_expr init;
5186 struct c_expr expr;
5187 start_init (NULL_TREE, NULL, 0);
5188 type = groktypename (type_name);
5189 if (C_TYPE_VARIABLE_SIZE (type))
5191 error ("compound literal has variable size");
5192 type = error_mark_node;
5194 init = c_parser_braced_init (parser, type, false);
5195 finish_init ();
5196 maybe_warn_string_init (type, init);
5198 if (pedantic && !flag_isoc99)
5199 pedwarn ("ISO C90 forbids compound literals");
5200 expr.value = build_compound_literal (type, init.value);
5201 expr.original_code = ERROR_MARK;
5202 return c_parser_postfix_expression_after_primary (parser, expr);
5205 /* Parse a postfix expression after the initial primary or compound
5206 literal; that is, parse a series of postfix operators. */
5208 static struct c_expr
5209 c_parser_postfix_expression_after_primary (c_parser *parser,
5210 struct c_expr expr)
5212 tree ident, idx, exprlist;
5213 while (true)
5215 switch (c_parser_peek_token (parser)->type)
5217 case CPP_OPEN_SQUARE:
5218 /* Array reference. */
5219 c_parser_consume_token (parser);
5220 idx = c_parser_expression (parser).value;
5221 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5222 "expected %<]%>");
5223 expr.value = build_array_ref (expr.value, idx);
5224 expr.original_code = ERROR_MARK;
5225 break;
5226 case CPP_OPEN_PAREN:
5227 /* Function call. */
5228 c_parser_consume_token (parser);
5229 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5230 exprlist = NULL_TREE;
5231 else
5232 exprlist = c_parser_expr_list (parser);
5233 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5234 "expected %<)%>");
5235 expr.value = build_function_call (expr.value, exprlist);
5236 expr.original_code = ERROR_MARK;
5237 break;
5238 case CPP_DOT:
5239 /* Structure element reference. */
5240 c_parser_consume_token (parser);
5241 if (c_parser_next_token_is (parser, CPP_NAME))
5242 ident = c_parser_peek_token (parser)->value;
5243 else
5245 c_parser_error (parser, "expected identifier");
5246 expr.value = error_mark_node;
5247 expr.original_code = ERROR_MARK;
5248 return expr;
5250 c_parser_consume_token (parser);
5251 expr.value = build_component_ref (expr.value, ident);
5252 expr.original_code = ERROR_MARK;
5253 break;
5254 case CPP_DEREF:
5255 /* Structure element reference. */
5256 c_parser_consume_token (parser);
5257 if (c_parser_next_token_is (parser, CPP_NAME))
5258 ident = c_parser_peek_token (parser)->value;
5259 else
5261 c_parser_error (parser, "expected identifier");
5262 expr.value = error_mark_node;
5263 expr.original_code = ERROR_MARK;
5264 return expr;
5266 c_parser_consume_token (parser);
5267 expr.value = build_component_ref (build_indirect_ref (expr.value,
5268 "->"), ident);
5269 expr.original_code = ERROR_MARK;
5270 break;
5271 case CPP_PLUS_PLUS:
5272 /* Postincrement. */
5273 c_parser_consume_token (parser);
5274 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5275 expr.original_code = ERROR_MARK;
5276 break;
5277 case CPP_MINUS_MINUS:
5278 /* Postdecrement. */
5279 c_parser_consume_token (parser);
5280 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5281 expr.original_code = ERROR_MARK;
5282 break;
5283 default:
5284 return expr;
5289 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5291 expression:
5292 assignment-expression
5293 expression , assignment-expression
5296 static struct c_expr
5297 c_parser_expression (c_parser *parser)
5299 struct c_expr expr;
5300 expr = c_parser_expr_no_commas (parser, NULL);
5301 while (c_parser_next_token_is (parser, CPP_COMMA))
5303 struct c_expr next;
5304 c_parser_consume_token (parser);
5305 next = c_parser_expr_no_commas (parser, NULL);
5306 expr.value = build_compound_expr (expr.value, next.value);
5307 expr.original_code = COMPOUND_EXPR;
5309 return expr;
5312 /* Parse a non-empty list of expressions.
5314 nonempty-expr-list:
5315 assignment-expression
5316 nonempty-expr-list , assignment-expression
5319 static tree
5320 c_parser_expr_list (c_parser *parser)
5322 struct c_expr expr;
5323 tree ret;
5324 expr = c_parser_expr_no_commas (parser, NULL);
5325 ret = build_tree_list (NULL_TREE, expr.value);
5326 while (c_parser_next_token_is (parser, CPP_COMMA))
5328 c_parser_consume_token (parser);
5329 expr = c_parser_expr_no_commas (parser, NULL);
5330 ret = chainon (ret, build_tree_list (NULL_TREE, expr.value));
5332 return ret;
5336 /* Parse Objective-C-specific constructs. */
5338 /* Parse an objc-class-definition.
5340 objc-class-definition:
5341 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5342 objc-class-instance-variables[opt] objc-methodprotolist @end
5343 @implementation identifier objc-superclass[opt]
5344 objc-class-instance-variables[opt]
5345 @interface identifier ( identifier ) objc-protocol-refs[opt]
5346 objc-methodprotolist @end
5347 @implementation identifier ( identifier )
5349 objc-superclass:
5350 : identifier
5352 "@interface identifier (" must start "@interface identifier (
5353 identifier ) ...": objc-methodprotolist in the first production may
5354 not start with a parenthesised identifier as a declarator of a data
5355 definition with no declaration specifiers if the objc-superclass,
5356 objc-protocol-refs and objc-class-instance-variables are omitted. */
5358 static void
5359 c_parser_objc_class_definition (c_parser *parser)
5361 bool iface_p;
5362 tree id1;
5363 tree superclass;
5364 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5365 iface_p = true;
5366 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5367 iface_p = false;
5368 else
5369 gcc_unreachable ();
5370 c_parser_consume_token (parser);
5371 if (c_parser_next_token_is_not (parser, CPP_NAME))
5373 c_parser_error (parser, "expected identifier");
5374 return;
5376 id1 = c_parser_peek_token (parser)->value;
5377 c_parser_consume_token (parser);
5378 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5380 tree id2;
5381 tree proto = NULL_TREE;
5382 c_parser_consume_token (parser);
5383 if (c_parser_next_token_is_not (parser, CPP_NAME))
5385 c_parser_error (parser, "expected identifier");
5386 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5387 return;
5389 id2 = c_parser_peek_token (parser)->value;
5390 c_parser_consume_token (parser);
5391 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5392 if (!iface_p)
5394 objc_start_category_implementation (id1, id2);
5395 return;
5397 if (c_parser_next_token_is (parser, CPP_LESS))
5398 proto = c_parser_objc_protocol_refs (parser);
5399 objc_start_category_interface (id1, id2, proto);
5400 c_parser_objc_methodprotolist (parser);
5401 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5402 objc_finish_interface ();
5403 return;
5405 if (c_parser_next_token_is (parser, CPP_COLON))
5407 c_parser_consume_token (parser);
5408 if (c_parser_next_token_is_not (parser, CPP_NAME))
5410 c_parser_error (parser, "expected identifier");
5411 return;
5413 superclass = c_parser_peek_token (parser)->value;
5414 c_parser_consume_token (parser);
5416 else
5417 superclass = NULL_TREE;
5418 if (iface_p)
5420 tree proto = NULL_TREE;
5421 if (c_parser_next_token_is (parser, CPP_LESS))
5422 proto = c_parser_objc_protocol_refs (parser);
5423 objc_start_class_interface (id1, superclass, proto);
5425 else
5426 objc_start_class_implementation (id1, superclass);
5427 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5428 c_parser_objc_class_instance_variables (parser);
5429 if (iface_p)
5431 objc_continue_interface ();
5432 c_parser_objc_methodprotolist (parser);
5433 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5434 objc_finish_interface ();
5436 else
5438 objc_continue_implementation ();
5439 return;
5443 /* Parse objc-class-instance-variables.
5445 objc-class-instance-variables:
5446 { objc-instance-variable-decl-list[opt] }
5448 objc-instance-variable-decl-list:
5449 objc-visibility-spec
5450 objc-instance-variable-decl ;
5452 objc-instance-variable-decl-list objc-visibility-spec
5453 objc-instance-variable-decl-list objc-instance-variable-decl ;
5454 objc-instance-variable-decl-list ;
5456 objc-visibility-spec:
5457 @private
5458 @protected
5459 @public
5461 objc-instance-variable-decl:
5462 struct-declaration
5465 static void
5466 c_parser_objc_class_instance_variables (c_parser *parser)
5468 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5469 c_parser_consume_token (parser);
5470 while (c_parser_next_token_is_not (parser, CPP_EOF))
5472 tree decls;
5473 /* Parse any stray semicolon. */
5474 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5476 if (pedantic)
5477 pedwarn ("extra semicolon in struct or union specified");
5478 c_parser_consume_token (parser);
5479 continue;
5481 /* Stop if at the end of the instance variables. */
5482 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5484 c_parser_consume_token (parser);
5485 break;
5487 /* Parse any objc-visibility-spec. */
5488 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5490 c_parser_consume_token (parser);
5491 objc_set_visibility (2);
5492 continue;
5494 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5496 c_parser_consume_token (parser);
5497 objc_set_visibility (0);
5498 continue;
5500 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5502 c_parser_consume_token (parser);
5503 objc_set_visibility (1);
5504 continue;
5506 /* Parse some comma-separated declarations. */
5507 decls = c_parser_struct_declaration (parser);
5509 /* Comma-separated instance variables are chained together in
5510 reverse order; add them one by one. */
5511 tree ivar = nreverse (decls);
5512 for (; ivar; ivar = TREE_CHAIN (ivar))
5513 objc_add_instance_variable (copy_node (ivar));
5515 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5519 /* Parse an objc-class-declaration.
5521 objc-class-declaration:
5522 @class identifier-list ;
5525 static void
5526 c_parser_objc_class_declaration (c_parser *parser)
5528 tree list = NULL_TREE;
5529 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5530 c_parser_consume_token (parser);
5531 /* Any identifiers, including those declared as type names, are OK
5532 here. */
5533 while (true)
5535 tree id;
5536 if (c_parser_next_token_is_not (parser, CPP_NAME))
5538 c_parser_error (parser, "expected identifier");
5539 break;
5541 id = c_parser_peek_token (parser)->value;
5542 list = chainon (list, build_tree_list (NULL_TREE, id));
5543 c_parser_consume_token (parser);
5544 if (c_parser_next_token_is (parser, CPP_COMMA))
5545 c_parser_consume_token (parser);
5546 else
5547 break;
5549 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5550 objc_declare_class (list);
5553 /* Parse an objc-alias-declaration.
5555 objc-alias-declaration:
5556 @compatibility_alias identifier identifier ;
5559 static void
5560 c_parser_objc_alias_declaration (c_parser *parser)
5562 tree id1, id2;
5563 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5564 c_parser_consume_token (parser);
5565 if (c_parser_next_token_is_not (parser, CPP_NAME))
5567 c_parser_error (parser, "expected identifier");
5568 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5569 return;
5571 id1 = c_parser_peek_token (parser)->value;
5572 c_parser_consume_token (parser);
5573 if (c_parser_next_token_is_not (parser, CPP_NAME))
5575 c_parser_error (parser, "expected identifier");
5576 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5577 return;
5579 id2 = c_parser_peek_token (parser)->value;
5580 c_parser_consume_token (parser);
5581 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5582 objc_declare_alias (id1, id2);
5585 /* Parse an objc-protocol-definition.
5587 objc-protocol-definition:
5588 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5589 @protocol identifier-list ;
5591 "@protocol identifier ;" should be resolved as "@protocol
5592 identifier-list ;": objc-methodprotolist may not start with a
5593 semicolon in the first alternative if objc-protocol-refs are
5594 omitted. */
5596 static void
5597 c_parser_objc_protocol_definition (c_parser *parser)
5599 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5600 c_parser_consume_token (parser);
5601 if (c_parser_next_token_is_not (parser, CPP_NAME))
5603 c_parser_error (parser, "expected identifier");
5604 return;
5606 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5607 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5609 tree list = NULL_TREE;
5610 /* Any identifiers, including those declared as type names, are
5611 OK here. */
5612 while (true)
5614 tree id;
5615 if (c_parser_next_token_is_not (parser, CPP_NAME))
5617 c_parser_error (parser, "expected identifier");
5618 break;
5620 id = c_parser_peek_token (parser)->value;
5621 list = chainon (list, build_tree_list (NULL_TREE, id));
5622 c_parser_consume_token (parser);
5623 if (c_parser_next_token_is (parser, CPP_COMMA))
5624 c_parser_consume_token (parser);
5625 else
5626 break;
5628 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5629 objc_declare_protocols (list);
5631 else
5633 tree id = c_parser_peek_token (parser)->value;
5634 tree proto = NULL_TREE;
5635 c_parser_consume_token (parser);
5636 if (c_parser_next_token_is (parser, CPP_LESS))
5637 proto = c_parser_objc_protocol_refs (parser);
5638 objc_pq_context = 1;
5639 objc_start_protocol (id, proto);
5640 c_parser_objc_methodprotolist (parser);
5641 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5642 objc_pq_context = 0;
5643 objc_finish_interface ();
5647 /* Parse an objc-method-type.
5649 objc-method-type:
5654 static enum tree_code
5655 c_parser_objc_method_type (c_parser *parser)
5657 switch (c_parser_peek_token (parser)->type)
5659 case CPP_PLUS:
5660 c_parser_consume_token (parser);
5661 return PLUS_EXPR;
5662 case CPP_MINUS:
5663 c_parser_consume_token (parser);
5664 return MINUS_EXPR;
5665 default:
5666 gcc_unreachable ();
5670 /* Parse an objc-method-definition.
5672 objc-method-definition:
5673 objc-method-type objc-method-decl ;[opt] compound-statement
5676 static void
5677 c_parser_objc_method_definition (c_parser *parser)
5679 enum tree_code type = c_parser_objc_method_type (parser);
5680 tree decl;
5681 objc_set_method_type (type);
5682 objc_pq_context = 1;
5683 decl = c_parser_objc_method_decl (parser);
5684 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5686 c_parser_consume_token (parser);
5687 if (pedantic)
5688 pedwarn ("extra semicolon in method definition specified");
5690 objc_pq_context = 0;
5691 objc_start_method_definition (decl);
5692 add_stmt (c_parser_compound_statement (parser));
5693 objc_finish_method_definition (current_function_decl);
5696 /* Parse an objc-methodprotolist.
5698 objc-methodprotolist:
5699 empty
5700 objc-methodprotolist objc-methodproto
5701 objc-methodprotolist declaration
5702 objc-methodprotolist ;
5704 The declaration is a data definition, which may be missing
5705 declaration specifiers under the same rules and diagnostics as
5706 other data definitions outside functions, and the stray semicolon
5707 is diagnosed the same way as a stray semicolon outside a
5708 function. */
5710 static void
5711 c_parser_objc_methodprotolist (c_parser *parser)
5713 while (true)
5715 /* The list is terminated by @end. */
5716 switch (c_parser_peek_token (parser)->type)
5718 case CPP_SEMICOLON:
5719 if (pedantic)
5720 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5721 c_parser_consume_token (parser);
5722 break;
5723 case CPP_PLUS:
5724 case CPP_MINUS:
5725 c_parser_objc_methodproto (parser);
5726 break;
5727 case CPP_EOF:
5728 return;
5729 default:
5730 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5731 return;
5732 c_parser_declaration_or_fndef (parser, false, true, false, true);
5733 break;
5738 /* Parse an objc-methodproto.
5740 objc-methodproto:
5741 objc-method-type objc-method-decl ;
5744 static void
5745 c_parser_objc_methodproto (c_parser *parser)
5747 enum tree_code type = c_parser_objc_method_type (parser);
5748 tree decl;
5749 objc_set_method_type (type);
5750 /* Remember protocol qualifiers in prototypes. */
5751 objc_pq_context = 1;
5752 decl = c_parser_objc_method_decl (parser);
5753 /* Forget protocol qualifiers here. */
5754 objc_pq_context = 0;
5755 objc_add_method_declaration (decl);
5756 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5759 /* Parse an objc-method-decl.
5761 objc-method-decl:
5762 ( objc-type-name ) objc-selector
5763 objc-selector
5764 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5765 objc-keyword-selector objc-optparmlist
5767 objc-keyword-selector:
5768 objc-keyword-decl
5769 objc-keyword-selector objc-keyword-decl
5771 objc-keyword-decl:
5772 objc-selector : ( objc-type-name ) identifier
5773 objc-selector : identifier
5774 : ( objc-type-name ) identifier
5775 : identifier
5777 objc-optparmlist:
5778 objc-optparms objc-optellipsis
5780 objc-optparms:
5781 empty
5782 objc-opt-parms , parameter-declaration
5784 objc-optellipsis:
5785 empty
5786 , ...
5789 static tree
5790 c_parser_objc_method_decl (c_parser *parser)
5792 tree type = NULL_TREE;
5793 tree sel;
5794 tree parms = NULL_TREE;
5795 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5797 c_parser_consume_token (parser);
5798 type = c_parser_objc_type_name (parser);
5799 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5801 sel = c_parser_objc_selector (parser);
5802 /* If there is no selector, or a colon follows, we have an
5803 objc-keyword-selector. If there is a selector, and a colon does
5804 not follow, that selector ends the objc-method-decl. */
5805 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5807 tree tsel = sel;
5808 tree list = NULL_TREE;
5809 bool ellipsis;
5810 while (true)
5812 tree atype = NULL_TREE, id, keyworddecl;
5813 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5814 break;
5815 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5817 c_parser_consume_token (parser);
5818 atype = c_parser_objc_type_name (parser);
5819 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5820 "expected %<)%>");
5822 if (c_parser_next_token_is_not (parser, CPP_NAME))
5824 c_parser_error (parser, "expected identifier");
5825 return error_mark_node;
5827 id = c_parser_peek_token (parser)->value;
5828 c_parser_consume_token (parser);
5829 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5830 list = chainon (list, keyworddecl);
5831 tsel = c_parser_objc_selector (parser);
5832 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5833 break;
5835 /* Parse the optional parameter list. Optional Objective-C
5836 method parameters follow the C syntax, and may include '...'
5837 to denote a variable number of arguments. */
5838 parms = make_node (TREE_LIST);
5839 ellipsis = false;
5840 while (c_parser_next_token_is (parser, CPP_COMMA))
5842 struct c_parm *parm;
5843 c_parser_consume_token (parser);
5844 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5846 ellipsis = true;
5847 c_parser_consume_token (parser);
5848 break;
5850 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5851 if (parm == NULL)
5852 break;
5853 parms = chainon (parms,
5854 build_tree_list (NULL_TREE, grokparm (parm)));
5856 TREE_OVERFLOW (parms) = ellipsis;
5857 sel = list;
5859 return objc_build_method_signature (type, sel, parms);
5862 /* Parse an objc-type-name.
5864 objc-type-name:
5865 objc-type-qualifiers[opt] type-name
5866 objc-type-qualifiers[opt]
5868 objc-type-qualifiers:
5869 objc-type-qualifier
5870 objc-type-qualifiers objc-type-qualifier
5872 objc-type-qualifier: one of
5873 in out inout bycopy byref oneway
5876 static tree
5877 c_parser_objc_type_name (c_parser *parser)
5879 tree quals = NULL_TREE;
5880 struct c_type_name *typename = NULL;
5881 tree type = NULL_TREE;
5882 while (true)
5884 c_token *token = c_parser_peek_token (parser);
5885 if (token->type == CPP_KEYWORD
5886 && (token->keyword == RID_IN
5887 || token->keyword == RID_OUT
5888 || token->keyword == RID_INOUT
5889 || token->keyword == RID_BYCOPY
5890 || token->keyword == RID_BYREF
5891 || token->keyword == RID_ONEWAY))
5893 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
5894 c_parser_consume_token (parser);
5896 else
5897 break;
5899 if (c_parser_next_token_starts_typename (parser))
5900 typename = c_parser_type_name (parser);
5901 if (typename)
5902 type = groktypename (typename);
5903 return build_tree_list (quals, type);
5906 /* Parse objc-protocol-refs.
5908 objc-protocol-refs:
5909 < identifier-list >
5912 static tree
5913 c_parser_objc_protocol_refs (c_parser *parser)
5915 tree list = NULL_TREE;
5916 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
5917 c_parser_consume_token (parser);
5918 /* Any identifiers, including those declared as type names, are OK
5919 here. */
5920 while (true)
5922 tree id;
5923 if (c_parser_next_token_is_not (parser, CPP_NAME))
5925 c_parser_error (parser, "expected identifier");
5926 break;
5928 id = c_parser_peek_token (parser)->value;
5929 list = chainon (list, build_tree_list (NULL_TREE, id));
5930 c_parser_consume_token (parser);
5931 if (c_parser_next_token_is (parser, CPP_COMMA))
5932 c_parser_consume_token (parser);
5933 else
5934 break;
5936 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
5937 return list;
5940 /* Parse an objc-try-catch-statement.
5942 objc-try-catch-statement:
5943 @try compound-statement objc-catch-list[opt]
5944 @try compound-statement objc-catch-list[opt] @finally compound-statement
5946 objc-catch-list:
5947 @catch ( parameter-declaration ) compound-statement
5948 objc-catch-list @catch ( parameter-declaration ) compound-statement
5951 static void
5952 c_parser_objc_try_catch_statement (c_parser *parser)
5954 location_t loc;
5955 tree stmt;
5956 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
5957 c_parser_consume_token (parser);
5958 loc = c_parser_peek_token (parser)->location;
5959 stmt = c_parser_compound_statement (parser);
5960 objc_begin_try_stmt (loc, stmt);
5961 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
5963 struct c_parm *parm;
5964 c_parser_consume_token (parser);
5965 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5966 break;
5967 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5968 if (parm == NULL)
5970 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5971 break;
5973 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5974 objc_begin_catch_clause (grokparm (parm));
5975 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5976 c_parser_compound_statement_nostart (parser);
5977 objc_finish_catch_clause ();
5979 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
5981 location_t finloc;
5982 tree finstmt;
5983 c_parser_consume_token (parser);
5984 finloc = c_parser_peek_token (parser)->location;
5985 finstmt = c_parser_compound_statement (parser);
5986 objc_build_finally_clause (finloc, finstmt);
5988 objc_finish_try_stmt ();
5991 /* Parse an objc-synchronized-statement.
5993 objc-synchronized-statement:
5994 @synchronized ( expression ) compound-statement
5997 static void
5998 c_parser_objc_synchronized_statement (c_parser *parser)
6000 location_t loc;
6001 tree expr, stmt;
6002 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6003 c_parser_consume_token (parser);
6004 loc = c_parser_peek_token (parser)->location;
6005 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6007 expr = c_parser_expression (parser).value;
6008 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6010 else
6011 expr = error_mark_node;
6012 stmt = c_parser_compound_statement (parser);
6013 objc_build_synchronized (loc, expr, stmt);
6016 /* Parse an objc-selector; return NULL_TREE without an error if the
6017 next token is not an objc-selector.
6019 objc-selector:
6020 identifier
6021 one of
6022 enum struct union if else while do for switch case default
6023 break continue return goto asm sizeof typeof __alignof
6024 unsigned long const short volatile signed restrict _Complex
6025 in out inout bycopy byref oneway int char float double void _Bool
6027 ??? Why this selection of keywords but not, for example, storage
6028 class specifiers? */
6030 static tree
6031 c_parser_objc_selector (c_parser *parser)
6033 c_token *token = c_parser_peek_token (parser);
6034 tree value = token->value;
6035 if (token->type == CPP_NAME)
6037 c_parser_consume_token (parser);
6038 return value;
6040 if (token->type != CPP_KEYWORD)
6041 return NULL_TREE;
6042 switch (token->keyword)
6044 case RID_ENUM:
6045 case RID_STRUCT:
6046 case RID_UNION:
6047 case RID_IF:
6048 case RID_ELSE:
6049 case RID_WHILE:
6050 case RID_DO:
6051 case RID_FOR:
6052 case RID_SWITCH:
6053 case RID_CASE:
6054 case RID_DEFAULT:
6055 case RID_BREAK:
6056 case RID_CONTINUE:
6057 case RID_RETURN:
6058 case RID_GOTO:
6059 case RID_ASM:
6060 case RID_SIZEOF:
6061 case RID_TYPEOF:
6062 case RID_ALIGNOF:
6063 case RID_UNSIGNED:
6064 case RID_LONG:
6065 case RID_CONST:
6066 case RID_SHORT:
6067 case RID_VOLATILE:
6068 case RID_SIGNED:
6069 case RID_RESTRICT:
6070 case RID_COMPLEX:
6071 case RID_IN:
6072 case RID_OUT:
6073 case RID_INOUT:
6074 case RID_BYCOPY:
6075 case RID_BYREF:
6076 case RID_ONEWAY:
6077 case RID_INT:
6078 case RID_CHAR:
6079 case RID_FLOAT:
6080 case RID_DOUBLE:
6081 case RID_VOID:
6082 case RID_BOOL:
6083 c_parser_consume_token (parser);
6084 return value;
6085 default:
6086 return NULL_TREE;
6090 /* Parse an objc-selector-arg.
6092 objc-selector-arg:
6093 objc-selector
6094 objc-keywordname-list
6096 objc-keywordname-list:
6097 objc-keywordname
6098 objc-keywordname-list objc-keywordname
6100 objc-keywordname:
6101 objc-selector :
6105 static tree
6106 c_parser_objc_selector_arg (c_parser *parser)
6108 tree sel = c_parser_objc_selector (parser);
6109 tree list = NULL_TREE;
6110 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6111 return sel;
6112 while (true)
6114 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6115 return list;
6116 list = chainon (list, build_tree_list (sel, NULL_TREE));
6117 sel = c_parser_objc_selector (parser);
6118 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6119 break;
6121 return list;
6124 /* Parse an objc-receiver.
6126 objc-receiver:
6127 expression
6128 class-name
6129 type-name
6132 static tree
6133 c_parser_objc_receiver (c_parser *parser)
6135 if (c_parser_peek_token (parser)->type == CPP_NAME
6136 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6137 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6139 tree id = c_parser_peek_token (parser)->value;
6140 c_parser_consume_token (parser);
6141 return objc_get_class_reference (id);
6143 return c_parser_expression (parser).value;
6146 /* Parse objc-message-args.
6148 objc-message-args:
6149 objc-selector
6150 objc-keywordarg-list
6152 objc-keywordarg-list:
6153 objc-keywordarg
6154 objc-keywordarg-list objc-keywordarg
6156 objc-keywordarg:
6157 objc-selector : objc-keywordexpr
6158 : objc-keywordexpr
6161 static tree
6162 c_parser_objc_message_args (c_parser *parser)
6164 tree sel = c_parser_objc_selector (parser);
6165 tree list = NULL_TREE;
6166 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6167 return sel;
6168 while (true)
6170 tree keywordexpr;
6171 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6172 return list;
6173 keywordexpr = c_parser_objc_keywordexpr (parser);
6174 list = chainon (list, build_tree_list (sel, keywordexpr));
6175 sel = c_parser_objc_selector (parser);
6176 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6177 break;
6179 return list;
6182 /* Parse an objc-keywordexpr.
6184 objc-keywordexpr:
6185 nonempty-expr-list
6188 static tree
6189 c_parser_objc_keywordexpr (c_parser *parser)
6191 tree list = c_parser_expr_list (parser);
6192 if (TREE_CHAIN (list) == NULL_TREE)
6194 /* Just return the expression, remove a level of
6195 indirection. */
6196 return TREE_VALUE (list);
6198 else
6200 /* We have a comma expression, we will collapse later. */
6201 return list;
6206 /* The actual parser and external interface. ??? Does this need to be
6207 garbage-collected? */
6209 static GTY (()) c_parser *the_parser;
6211 /* Parse a single source file. */
6213 void
6214 c_parse_file (void)
6216 the_parser = c_parser_new ();
6217 c_parser_translation_unit (the_parser);
6218 the_parser = NULL;
6221 #include "gt-c-parser.h"