* doc/install.texi: Fix a typo.
[official-gcc.git] / gcc / c-parser.c
blob9a5d669d034a7deaaf7b0d19cac3c01cdf44a503
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 c_parser_error (parser, "expected declaration or statement");
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 = c_objc_common_truthvalue_conversion
3584 (c_parser_expression (parser).value);
3585 if (EXPR_P (cond))
3586 SET_EXPR_LOCATION (cond, loc);
3587 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3588 return cond;
3591 /* Parse a statement which is a block in C99. */
3593 static tree
3594 c_parser_c99_block_statement (c_parser *parser)
3596 tree block = c_begin_compound_stmt (flag_isoc99);
3597 c_parser_statement (parser);
3598 return c_end_compound_stmt (block, flag_isoc99);
3601 /* Parse the body of an if statement or the else half thereof. This
3602 is just parsing a statement but (a) it is a block in C99, (b) we
3603 track whether the body is an if statement for the sake of
3604 -Wparentheses warnings, (c) we handle an empty body specially for
3605 the sake of -Wextra warnings. */
3607 static tree
3608 c_parser_if_body (c_parser *parser, bool *if_p)
3610 tree block = c_begin_compound_stmt (flag_isoc99);
3611 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3612 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3613 || (c_parser_next_token_is (parser, CPP_NAME)
3614 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3615 c_parser_label (parser);
3616 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3617 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON))
3618 add_stmt (build (NOP_EXPR, NULL_TREE, NULL_TREE));
3619 c_parser_statement_after_labels (parser);
3620 return c_end_compound_stmt (block, flag_isoc99);
3623 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3625 if-statement:
3626 if ( expression ) statement
3627 if ( expression ) statement else statement
3630 static void
3631 c_parser_if_statement (c_parser *parser)
3633 tree block;
3634 location_t loc;
3635 tree cond;
3636 bool first_if = false, second_if = false;
3637 tree first_body, second_body;
3638 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3639 c_parser_consume_token (parser);
3640 block = c_begin_compound_stmt (flag_isoc99);
3641 loc = c_parser_peek_token (parser)->location;
3642 cond = c_parser_paren_condition (parser);
3643 first_body = c_parser_if_body (parser, &first_if);
3644 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3646 c_parser_consume_token (parser);
3647 second_body = c_parser_if_body (parser, &second_if);
3649 else
3650 second_body = NULL_TREE;
3651 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3652 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3655 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3657 switch-statement:
3658 switch (expression) statement
3661 static void
3662 c_parser_switch_statement (c_parser *parser)
3664 tree block, expr, body, save_break;
3665 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3666 c_parser_consume_token (parser);
3667 block = c_begin_compound_stmt (flag_isoc99);
3668 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3670 expr = c_parser_expression (parser).value;
3671 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3673 else
3674 expr = error_mark_node;
3675 c_start_case (expr);
3676 save_break = c_break_label;
3677 c_break_label = NULL_TREE;
3678 body = c_parser_c99_block_statement (parser);
3679 c_finish_case (body);
3680 if (c_break_label)
3681 add_stmt (build (LABEL_EXPR, void_type_node, c_break_label));
3682 c_break_label = save_break;
3683 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3686 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3688 while-statement:
3689 while (expression) statement
3692 static void
3693 c_parser_while_statement (c_parser *parser)
3695 tree block, cond, body, save_break, save_cont;
3696 location_t loc;
3697 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3698 c_parser_consume_token (parser);
3699 block = c_begin_compound_stmt (flag_isoc99);
3700 loc = c_parser_peek_token (parser)->location;
3701 cond = c_parser_paren_condition (parser);
3702 save_break = c_break_label;
3703 c_break_label = NULL_TREE;
3704 save_cont = c_cont_label;
3705 c_cont_label = NULL_TREE;
3706 body = c_parser_c99_block_statement (parser);
3707 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3708 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3709 c_break_label = save_break;
3710 c_cont_label = save_cont;
3713 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3715 do-statement:
3716 do statement while ( expression ) ;
3719 static void
3720 c_parser_do_statement (c_parser *parser)
3722 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3723 location_t loc;
3724 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3725 c_parser_consume_token (parser);
3726 block = c_begin_compound_stmt (flag_isoc99);
3727 loc = c_parser_peek_token (parser)->location;
3728 save_break = c_break_label;
3729 c_break_label = NULL_TREE;
3730 save_cont = c_cont_label;
3731 c_cont_label = NULL_TREE;
3732 body = c_parser_c99_block_statement (parser);
3733 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3734 new_break = c_break_label;
3735 c_break_label = save_break;
3736 new_cont = c_cont_label;
3737 c_cont_label = save_cont;
3738 cond = c_parser_paren_condition (parser);
3739 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3740 c_parser_skip_to_end_of_block_or_statement (parser);
3741 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
3742 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3745 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
3747 for-statement:
3748 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
3749 for ( nested-declaration expression[opt] ; expression[opt] ) statement
3751 The form with a declaration is new in C99.
3753 ??? In accordance with the old parser, the declaration may be a
3754 nested function, which is then rejected in check_for_loop_decls,
3755 but does it make any sense for this to be included in the grammar?
3756 Note in particular that the nested function does not include a
3757 trailing ';', whereas the "declaration" production includes one.
3758 Also, can we reject bad declarations earlier and cheaper than
3759 check_for_loop_decls? */
3761 static void
3762 c_parser_for_statement (c_parser *parser)
3764 tree block, cond, incr, save_break, save_cont, body;
3765 location_t loc = UNKNOWN_LOCATION;
3766 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
3767 c_parser_consume_token (parser);
3768 block = c_begin_compound_stmt (flag_isoc99);
3769 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3771 /* Parse the initialization declaration or expression. */
3772 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3774 c_parser_consume_token (parser);
3775 c_finish_expr_stmt (NULL_TREE);
3777 else if (c_parser_next_token_starts_declspecs (parser))
3779 c_parser_declaration_or_fndef (parser, true, true, true, true);
3780 check_for_loop_decls ();
3782 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3784 /* __extension__ can start a declaration, but is also an
3785 unary operator that can start an expression. Consume all
3786 but the last of a possible series of __extension__ to
3787 determine which. */
3788 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3789 && (c_parser_peek_2nd_token (parser)->keyword
3790 == RID_EXTENSION))
3791 c_parser_consume_token (parser);
3792 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3794 int ext;
3795 ext = disable_extension_diagnostics ();
3796 c_parser_consume_token (parser);
3797 c_parser_declaration_or_fndef (parser, true, true, true, true);
3798 restore_extension_diagnostics (ext);
3799 check_for_loop_decls ();
3801 else
3802 goto init_expr;
3804 else
3806 init_expr:
3807 c_finish_expr_stmt (c_parser_expression (parser).value);
3808 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3810 /* Parse the loop condition. */
3811 loc = c_parser_peek_token (parser)->location;
3812 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3814 c_parser_consume_token (parser);
3815 cond = NULL_TREE;
3817 else
3819 tree ocond = c_parser_expression (parser).value;
3820 cond = c_objc_common_truthvalue_conversion (ocond);
3821 if (EXPR_P (cond))
3822 SET_EXPR_LOCATION (cond, loc);
3823 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3825 /* Parse the increment expression. */
3826 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3827 incr = c_process_expr_stmt (NULL_TREE);
3828 else
3829 incr = c_process_expr_stmt (c_parser_expression (parser).value);
3830 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3832 else
3834 cond = error_mark_node;
3835 incr = error_mark_node;
3837 save_break = c_break_label;
3838 c_break_label = NULL_TREE;
3839 save_cont = c_cont_label;
3840 c_cont_label = NULL_TREE;
3841 body = c_parser_c99_block_statement (parser);
3842 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
3843 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3844 c_break_label = save_break;
3845 c_cont_label = save_cont;
3848 /* Parse an asm statement, a GNU extension. This is a full-blown asm
3849 statement with inputs, outputs, clobbers, and volatile tag
3850 allowed.
3852 asm-statement:
3853 asm type-qualifier[opt] ( asm-argument ) ;
3855 asm-argument:
3856 asm-string-literal
3857 asm-string-literal : asm-operands[opt]
3858 asm-string-literal : asm-operands[opt] : asm-operands[opt]
3859 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
3861 Qualifiers other than volatile are accepted in the syntax but
3862 warned for. */
3864 static tree
3865 c_parser_asm_statement (c_parser *parser)
3867 tree quals, str, outputs, inputs, clobbers, ret;
3868 bool simple;
3869 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3870 c_parser_consume_token (parser);
3871 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
3873 quals = c_parser_peek_token (parser)->value;
3874 c_parser_consume_token (parser);
3876 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
3877 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
3879 warning ("%E qualifier ignored on asm",
3880 c_parser_peek_token (parser)->value);
3881 quals = NULL_TREE;
3882 c_parser_consume_token (parser);
3884 else
3885 quals = NULL_TREE;
3886 /* ??? Follow the C++ parser rather than using the
3887 c_lex_string_translate kludge. */
3888 c_lex_string_translate = 0;
3889 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3891 c_lex_string_translate = 1;
3892 return NULL_TREE;
3894 str = c_parser_asm_string_literal (parser);
3895 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3897 simple = true;
3898 outputs = NULL_TREE;
3899 inputs = NULL_TREE;
3900 clobbers = NULL_TREE;
3901 goto done_asm;
3903 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3905 c_lex_string_translate = 1;
3906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3907 return NULL_TREE;
3909 simple = false;
3910 /* Parse outputs. */
3911 if (c_parser_next_token_is (parser, CPP_COLON)
3912 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3913 outputs = NULL_TREE;
3914 else
3915 outputs = c_parser_asm_operands (parser);
3916 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3918 inputs = NULL_TREE;
3919 clobbers = NULL_TREE;
3920 goto done_asm;
3922 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3924 c_lex_string_translate = 1;
3925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3926 return NULL_TREE;
3928 /* Parse inputs. */
3929 if (c_parser_next_token_is (parser, CPP_COLON)
3930 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3931 inputs = NULL_TREE;
3932 else
3933 inputs = c_parser_asm_operands (parser);
3934 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3936 clobbers = NULL_TREE;
3937 goto done_asm;
3939 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
3941 c_lex_string_translate = 1;
3942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3943 return NULL_TREE;
3945 /* Parse clobbers. */
3946 clobbers = c_parser_asm_clobbers (parser);
3947 done_asm:
3948 c_lex_string_translate = 1;
3949 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3952 return NULL_TREE;
3954 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3955 c_parser_skip_to_end_of_block_or_statement (parser);
3956 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
3957 clobbers, simple));
3958 return ret;
3961 /* Parse asm operands, a GNU extension.
3963 asm-operands:
3964 asm-operand
3965 asm-operands , asm-operand
3967 asm-operand:
3968 asm-string-literal ( expression )
3969 [ identifier ] asm-string-literal ( expression )
3972 static tree
3973 c_parser_asm_operands (c_parser *parser)
3975 tree list = NULL_TREE;
3976 while (true)
3978 tree name, str, expr;
3979 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3981 c_parser_consume_token (parser);
3982 if (c_parser_next_token_is (parser, CPP_NAME))
3984 tree id = c_parser_peek_token (parser)->value;
3985 c_parser_consume_token (parser);
3986 name = build_string (IDENTIFIER_LENGTH (id),
3987 IDENTIFIER_POINTER (id));
3989 else
3991 c_parser_error (parser, "expected identifier");
3992 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3993 return NULL_TREE;
3995 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3996 "expected %<]%>");
3998 else
3999 name = NULL_TREE;
4000 str = c_parser_asm_string_literal (parser);
4001 if (str == NULL_TREE)
4002 return NULL_TREE;
4003 c_lex_string_translate = 1;
4004 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4006 c_lex_string_translate = 0;
4007 return NULL_TREE;
4009 expr = c_parser_expression (parser).value;
4010 c_lex_string_translate = 0;
4011 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4013 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4014 return NULL_TREE;
4016 list = chainon (list, build_tree_list (build_tree_list (name, str),
4017 expr));
4018 if (c_parser_next_token_is (parser, CPP_COMMA))
4019 c_parser_consume_token (parser);
4020 else
4021 break;
4023 return list;
4026 /* Parse asm clobbers, a GNU extension.
4028 asm-clobbers:
4029 asm-string-literal
4030 asm-clobbers , asm-string-literal
4033 static tree
4034 c_parser_asm_clobbers (c_parser *parser)
4036 tree list = NULL_TREE;
4037 while (true)
4039 tree str = c_parser_asm_string_literal (parser);
4040 if (str)
4041 list = tree_cons (NULL_TREE, str, list);
4042 else
4043 return NULL_TREE;
4044 if (c_parser_next_token_is (parser, CPP_COMMA))
4045 c_parser_consume_token (parser);
4046 else
4047 break;
4049 return list;
4052 /* Parse an expression other than a compound expression; that is, an
4053 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4054 NULL then it is an Objective-C message expression which is the
4055 primary-expression starting the expression as an initializer.
4057 assignment-expression:
4058 conditional-expression
4059 unary-expression assignment-operator assignment-expression
4061 assignment-operator: one of
4062 = *= /= %= += -= <<= >>= &= ^= |=
4064 In GNU C we accept any conditional expression on the LHS and
4065 diagnose the invalid lvalue rather than producing a syntax
4066 error. */
4068 static struct c_expr
4069 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4071 struct c_expr lhs, rhs, ret;
4072 enum tree_code code;
4073 gcc_assert (!after || c_dialect_objc ());
4074 lhs = c_parser_conditional_expression (parser, after);
4075 switch (c_parser_peek_token (parser)->type)
4077 case CPP_EQ:
4078 code = NOP_EXPR;
4079 break;
4080 case CPP_MULT_EQ:
4081 code = MULT_EXPR;
4082 break;
4083 case CPP_DIV_EQ:
4084 code = TRUNC_DIV_EXPR;
4085 break;
4086 case CPP_MOD_EQ:
4087 code = TRUNC_MOD_EXPR;
4088 break;
4089 case CPP_PLUS_EQ:
4090 code = PLUS_EXPR;
4091 break;
4092 case CPP_MINUS_EQ:
4093 code = MINUS_EXPR;
4094 break;
4095 case CPP_LSHIFT_EQ:
4096 code = LSHIFT_EXPR;
4097 break;
4098 case CPP_RSHIFT_EQ:
4099 code = RSHIFT_EXPR;
4100 break;
4101 case CPP_AND_EQ:
4102 code = BIT_AND_EXPR;
4103 break;
4104 case CPP_XOR_EQ:
4105 code = BIT_XOR_EXPR;
4106 break;
4107 case CPP_OR_EQ:
4108 code = BIT_IOR_EXPR;
4109 break;
4110 default:
4111 return lhs;
4113 c_parser_consume_token (parser);
4114 rhs = c_parser_expr_no_commas (parser, NULL);
4115 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4116 if (code == NOP_EXPR)
4117 ret.original_code = MODIFY_EXPR;
4118 else
4120 TREE_NO_WARNING (ret.value) = 1;
4121 ret.original_code = ERROR_MARK;
4123 return ret;
4126 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4127 is not NULL then it is an Objective-C message expression which is
4128 the primary-expression starting the expression as an initializer.
4130 conditional-expression:
4131 logical-OR-expression
4132 logical-OR-expression ? expression : conditional-expression
4134 GNU extensions:
4136 conditional-expression:
4137 logical-OR-expression ? : conditional-expression
4140 static struct c_expr
4141 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4143 struct c_expr cond, exp1, exp2, ret;
4144 gcc_assert (!after || c_dialect_objc ());
4145 cond = c_parser_binary_expression (parser, after);
4146 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4147 return cond;
4148 c_parser_consume_token (parser);
4149 if (c_parser_next_token_is (parser, CPP_COLON))
4151 if (pedantic)
4152 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4153 /* Make sure first operand is calculated only once. */
4154 exp1.value = save_expr (default_conversion (cond.value));
4155 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4156 skip_evaluation += cond.value == truthvalue_true_node;
4158 else
4160 cond.value
4161 = c_objc_common_truthvalue_conversion
4162 (default_conversion (cond.value));
4163 skip_evaluation += cond.value == truthvalue_false_node;
4164 exp1 = c_parser_expression (parser);
4165 skip_evaluation += ((cond.value == truthvalue_true_node)
4166 - (cond.value == truthvalue_false_node));
4168 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4170 skip_evaluation -= cond.value == truthvalue_true_node;
4171 ret.value = error_mark_node;
4172 ret.original_code = ERROR_MARK;
4173 return ret;
4175 exp2 = c_parser_conditional_expression (parser, NULL);
4176 skip_evaluation -= cond.value == truthvalue_true_node;
4177 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4178 ret.original_code = ERROR_MARK;
4179 return ret;
4182 /* Parse a binary expression; that is, a logical-OR-expression (C90
4183 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4184 an Objective-C message expression which is the primary-expression
4185 starting the expression as an initializer.
4187 multiplicative-expression:
4188 cast-expression
4189 multiplicative-expression * cast-expression
4190 multiplicative-expression / cast-expression
4191 multiplicative-expression % cast-expression
4193 additive-expression:
4194 multiplicative-expression
4195 additive-expression + multiplicative-expression
4196 additive-expression - multiplicative-expression
4198 shift-expression:
4199 additive-expression
4200 shift-expression << additive-expression
4201 shift-expression >> additive-expression
4203 relational-expression:
4204 shift-expression
4205 relational-expression < shift-expression
4206 relational-expression > shift-expression
4207 relational-expression <= shift-expression
4208 relational-expression >= shift-expression
4210 equality-expression:
4211 relational-expression
4212 equality-expression == relational-expression
4213 equality-expression != relational-expression
4215 AND-expression:
4216 equality-expression
4217 AND-expression & equality-expression
4219 exclusive-OR-expression:
4220 AND-expression
4221 exclusive-OR-expression ^ AND-expression
4223 inclusive-OR-expression:
4224 exclusive-OR-expression
4225 inclusive-OR-expression | exclusive-OR-expression
4227 logical-AND-expression:
4228 inclusive-OR-expression
4229 logical-AND-expression && inclusive-OR-expression
4231 logical-OR-expression:
4232 logical-AND-expression
4233 logical-OR-expression || logical-AND-expression
4236 static struct c_expr
4237 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4239 /* A binary expression is parsed using operator-precedence parsing,
4240 with the operands being cast expressions. All the binary
4241 operators are left-associative. Thus a binary expression is of
4242 form:
4244 E0 op1 E1 op2 E2 ...
4246 which we represent on a stack. On the stack, the precedence
4247 levels are strictly increasing. When a new operator is
4248 encountered of higher precedence than that at the top of the
4249 stack, it is pushed; its LHS is the top expression, and its RHS
4250 is everything parsed until it is popped. When a new operator is
4251 encountered with precedence less than or equal to that at the top
4252 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4253 by the result of the operation until the operator at the top of
4254 the stack has lower precedence than the new operator or there is
4255 only one element on the stack; then the top expression is the LHS
4256 of the new operator. In the case of logical AND and OR
4257 expressions, we also need to adjust skip_evaluation as
4258 appropriate when the operators are pushed and popped. */
4260 /* The precedence levels, where 0 is a dummy lowest level used for
4261 the bottom of the stack. */
4262 enum prec {
4263 PREC_NONE,
4264 PREC_LOGOR,
4265 PREC_LOGAND,
4266 PREC_BITOR,
4267 PREC_BITXOR,
4268 PREC_BITAND,
4269 PREC_EQ,
4270 PREC_REL,
4271 PREC_SHIFT,
4272 PREC_ADD,
4273 PREC_MULT,
4274 NUM_PRECS
4276 struct {
4277 /* The expression at this stack level. */
4278 struct c_expr expr;
4279 /* The precedence of the operator on its left, PREC_NONE at the
4280 bottom of the stack. */
4281 enum prec prec;
4282 /* The operation on its left. */
4283 enum tree_code op;
4284 } stack[NUM_PRECS];
4285 int sp;
4286 #define POP \
4287 do { \
4288 switch (stack[sp].op) \
4290 case TRUTH_ANDIF_EXPR: \
4291 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4292 break; \
4293 case TRUTH_ORIF_EXPR: \
4294 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4295 break; \
4296 default: \
4297 break; \
4299 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4300 stack[sp - 1].expr, \
4301 stack[sp].expr); \
4302 sp--; \
4303 } while (0)
4304 gcc_assert (!after || c_dialect_objc ());
4305 stack[0].expr = c_parser_cast_expression (parser, after);
4306 stack[0].prec = PREC_NONE;
4307 sp = 0;
4308 while (true)
4310 enum prec oprec;
4311 enum tree_code ocode;
4312 if (parser->error)
4313 goto out;
4314 switch (c_parser_peek_token (parser)->type)
4316 case CPP_MULT:
4317 oprec = PREC_MULT;
4318 ocode = MULT_EXPR;
4319 break;
4320 case CPP_DIV:
4321 oprec = PREC_MULT;
4322 ocode = TRUNC_DIV_EXPR;
4323 break;
4324 case CPP_MOD:
4325 oprec = PREC_MULT;
4326 ocode = TRUNC_MOD_EXPR;
4327 break;
4328 case CPP_PLUS:
4329 oprec = PREC_ADD;
4330 ocode = PLUS_EXPR;
4331 break;
4332 case CPP_MINUS:
4333 oprec = PREC_ADD;
4334 ocode = MINUS_EXPR;
4335 break;
4336 case CPP_LSHIFT:
4337 oprec = PREC_SHIFT;
4338 ocode = LSHIFT_EXPR;
4339 break;
4340 case CPP_RSHIFT:
4341 oprec = PREC_SHIFT;
4342 ocode = RSHIFT_EXPR;
4343 break;
4344 case CPP_LESS:
4345 oprec = PREC_REL;
4346 ocode = LT_EXPR;
4347 break;
4348 case CPP_GREATER:
4349 oprec = PREC_REL;
4350 ocode = GT_EXPR;
4351 break;
4352 case CPP_LESS_EQ:
4353 oprec = PREC_REL;
4354 ocode = LE_EXPR;
4355 break;
4356 case CPP_GREATER_EQ:
4357 oprec = PREC_REL;
4358 ocode = GE_EXPR;
4359 break;
4360 case CPP_EQ_EQ:
4361 oprec = PREC_EQ;
4362 ocode = EQ_EXPR;
4363 break;
4364 case CPP_NOT_EQ:
4365 oprec = PREC_EQ;
4366 ocode = NE_EXPR;
4367 break;
4368 case CPP_AND:
4369 oprec = PREC_BITAND;
4370 ocode = BIT_AND_EXPR;
4371 break;
4372 case CPP_XOR:
4373 oprec = PREC_BITXOR;
4374 ocode = BIT_XOR_EXPR;
4375 break;
4376 case CPP_OR:
4377 oprec = PREC_BITOR;
4378 ocode = BIT_IOR_EXPR;
4379 break;
4380 case CPP_AND_AND:
4381 oprec = PREC_LOGAND;
4382 ocode = TRUTH_ANDIF_EXPR;
4383 break;
4384 case CPP_OR_OR:
4385 oprec = PREC_LOGOR;
4386 ocode = TRUTH_ORIF_EXPR;
4387 break;
4388 default:
4389 /* Not a binary operator, so end of the binary
4390 expression. */
4391 goto out;
4393 c_parser_consume_token (parser);
4394 while (oprec <= stack[sp].prec)
4395 POP;
4396 switch (ocode)
4398 case TRUTH_ANDIF_EXPR:
4399 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4400 (default_conversion (stack[sp].expr.value));
4401 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4402 break;
4403 case TRUTH_ORIF_EXPR:
4404 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4405 (default_conversion (stack[sp].expr.value));
4406 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4407 break;
4408 default:
4409 break;
4411 sp++;
4412 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4413 stack[sp].prec = oprec;
4414 stack[sp].op = ocode;
4416 out:
4417 while (sp > 0)
4418 POP;
4419 return stack[0].expr;
4420 #undef POP
4423 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4424 NULL then it is an Objective-C message expression which is the
4425 primary-expression starting the expression as an initializer.
4427 cast-expression:
4428 unary-expression
4429 ( type-name ) unary-expression
4432 static struct c_expr
4433 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4435 gcc_assert (!after || c_dialect_objc ());
4436 if (after)
4437 return c_parser_postfix_expression_after_primary (parser, *after);
4438 /* If the expression begins with a parenthesized type name, it may
4439 be either a cast or a compound literal; we need to see whether
4440 the next character is '{' to tell the difference. If not, it is
4441 an unary expression. */
4442 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4443 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4445 struct c_type_name *type_name;
4446 struct c_expr ret;
4447 tree expr;
4448 c_parser_consume_token (parser);
4449 type_name = c_parser_type_name (parser);
4450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4451 if (type_name == NULL)
4453 ret.value = error_mark_node;
4454 ret.original_code = ERROR_MARK;
4455 return ret;
4457 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4458 return c_parser_postfix_expression_after_paren_type (parser,
4459 type_name);
4460 expr = c_parser_cast_expression (parser, NULL).value;
4461 ret.value = c_cast_expr (type_name, expr);
4462 ret.original_code = ERROR_MARK;
4463 return ret;
4465 else
4466 return c_parser_unary_expression (parser);
4469 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4471 unary-expression:
4472 postfix-expression
4473 ++ unary-expression
4474 -- unary-expression
4475 unary-operator cast-expression
4476 sizeof unary-expression
4477 sizeof ( type-name )
4479 unary-operator: one of
4480 & * + - ~ !
4482 GNU extensions:
4484 unary-expression:
4485 __alignof__ unary-expression
4486 __alignof__ ( type-name )
4487 && identifier
4489 unary-operator: one of
4490 __extension__ __real__ __imag__
4492 In addition, the GNU syntax treats ++ and -- as unary operators, so
4493 they may be applied to cast expressions with errors for non-lvalues
4494 given later. */
4496 static struct c_expr
4497 c_parser_unary_expression (c_parser *parser)
4499 int ext;
4500 struct c_expr ret;
4501 ret.original_code = ERROR_MARK;
4502 switch (c_parser_peek_token (parser)->type)
4504 case CPP_PLUS_PLUS:
4505 c_parser_consume_token (parser);
4506 ret.value
4507 = build_unary_op (PREINCREMENT_EXPR,
4508 c_parser_cast_expression (parser, NULL).value, 0);
4509 overflow_warning (ret.value);
4510 return ret;
4511 case CPP_MINUS_MINUS:
4512 c_parser_consume_token (parser);
4513 ret.value
4514 = build_unary_op (PREDECREMENT_EXPR,
4515 c_parser_cast_expression (parser, NULL).value, 0);
4516 overflow_warning (ret.value);
4517 return ret;
4518 case CPP_AND:
4519 c_parser_consume_token (parser);
4520 ret.value
4521 = build_unary_op (ADDR_EXPR,
4522 c_parser_cast_expression (parser, NULL).value, 0);
4523 overflow_warning (ret.value);
4524 return ret;
4525 case CPP_MULT:
4526 c_parser_consume_token (parser);
4527 ret.value
4528 = build_indirect_ref (c_parser_cast_expression (parser, NULL).value,
4529 "unary *");
4530 return ret;
4531 case CPP_PLUS:
4532 c_parser_consume_token (parser);
4533 if (!c_dialect_objc () && warn_traditional && !in_system_header)
4534 warning ("traditional C rejects the unary plus operator");
4535 ret.value
4536 = build_unary_op (CONVERT_EXPR,
4537 c_parser_cast_expression (parser, NULL).value, 0);
4538 overflow_warning (ret.value);
4539 return ret;
4540 case CPP_MINUS:
4541 c_parser_consume_token (parser);
4542 ret.value
4543 = build_unary_op (NEGATE_EXPR,
4544 c_parser_cast_expression (parser, NULL).value, 0);
4545 overflow_warning (ret.value);
4546 return ret;
4547 case CPP_COMPL:
4548 c_parser_consume_token (parser);
4549 ret.value
4550 = build_unary_op (BIT_NOT_EXPR,
4551 c_parser_cast_expression (parser, NULL).value, 0);
4552 overflow_warning (ret.value);
4553 return ret;
4554 case CPP_NOT:
4555 c_parser_consume_token (parser);
4556 ret.value
4557 = build_unary_op (TRUTH_NOT_EXPR,
4558 c_parser_cast_expression (parser, NULL).value, 0);
4559 overflow_warning (ret.value);
4560 return ret;
4561 case CPP_AND_AND:
4562 /* Refer to the address of a label as a pointer. */
4563 c_parser_consume_token (parser);
4564 if (c_parser_next_token_is (parser, CPP_NAME))
4566 ret.value = finish_label_address_expr
4567 (c_parser_peek_token (parser)->value);
4568 c_parser_consume_token (parser);
4569 return ret;
4571 else
4573 c_parser_error (parser, "expected identifier");
4574 ret.value = error_mark_node;
4575 return ret;
4577 case CPP_KEYWORD:
4578 switch (c_parser_peek_token (parser)->keyword)
4580 case RID_SIZEOF:
4581 return c_parser_sizeof_expression (parser);
4582 case RID_ALIGNOF:
4583 return c_parser_alignof_expression (parser);
4584 case RID_EXTENSION:
4585 c_parser_consume_token (parser);
4586 ext = disable_extension_diagnostics ();
4587 ret = c_parser_cast_expression (parser, NULL);
4588 restore_extension_diagnostics (ext);
4589 return ret;
4590 case RID_REALPART:
4591 c_parser_consume_token (parser);
4592 ret.value
4593 = build_unary_op (REALPART_EXPR,
4594 c_parser_cast_expression (parser, NULL).value,
4596 return ret;
4597 case RID_IMAGPART:
4598 c_parser_consume_token (parser);
4599 ret.value
4600 = build_unary_op (IMAGPART_EXPR,
4601 c_parser_cast_expression (parser, NULL).value,
4603 return ret;
4604 default:
4605 return c_parser_postfix_expression (parser);
4607 default:
4608 return c_parser_postfix_expression (parser);
4612 /* Parse a sizeof expression. */
4614 static struct c_expr
4615 c_parser_sizeof_expression (c_parser *parser)
4617 struct c_expr expr;
4618 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4619 c_parser_consume_token (parser);
4620 skip_evaluation++;
4621 in_sizeof++;
4622 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4623 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4625 /* Either sizeof ( type-name ) or sizeof unary-expression
4626 starting with a compound literal. */
4627 struct c_type_name *type_name;
4628 c_parser_consume_token (parser);
4629 type_name = c_parser_type_name (parser);
4630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4631 if (type_name == NULL)
4633 struct c_expr ret;
4634 skip_evaluation--;
4635 in_sizeof--;
4636 ret.value = error_mark_node;
4637 ret.original_code = ERROR_MARK;
4638 return ret;
4640 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4642 expr = c_parser_postfix_expression_after_paren_type (parser,
4643 type_name);
4644 goto sizeof_expr;
4646 /* sizeof ( type-name ). */
4647 skip_evaluation--;
4648 in_sizeof--;
4649 return c_expr_sizeof_type (type_name);
4651 else
4653 expr = c_parser_unary_expression (parser);
4654 sizeof_expr:
4655 skip_evaluation--;
4656 in_sizeof--;
4657 if (TREE_CODE (expr.value) == COMPONENT_REF
4658 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4659 error ("%<sizeof%> applied to a bit-field");
4660 return c_expr_sizeof_expr (expr);
4664 /* Parse an alignof expression. */
4666 static struct c_expr
4667 c_parser_alignof_expression (c_parser *parser)
4669 struct c_expr expr;
4670 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4671 c_parser_consume_token (parser);
4672 skip_evaluation++;
4673 in_alignof++;
4674 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4675 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4677 /* Either __alignof__ ( type-name ) or __alignof__
4678 unary-expression starting with a compound literal. */
4679 struct c_type_name *type_name;
4680 struct c_expr ret;
4681 c_parser_consume_token (parser);
4682 type_name = c_parser_type_name (parser);
4683 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4684 if (type_name == NULL)
4686 struct c_expr ret;
4687 skip_evaluation--;
4688 in_alignof--;
4689 ret.value = error_mark_node;
4690 ret.original_code = ERROR_MARK;
4691 return ret;
4693 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4695 expr = c_parser_postfix_expression_after_paren_type (parser,
4696 type_name);
4697 goto alignof_expr;
4699 /* alignof ( type-name ). */
4700 skip_evaluation--;
4701 in_alignof--;
4702 ret.value = c_alignof (groktypename (type_name));
4703 ret.original_code = ERROR_MARK;
4704 return ret;
4706 else
4708 struct c_expr ret;
4709 expr = c_parser_unary_expression (parser);
4710 alignof_expr:
4711 skip_evaluation--;
4712 in_alignof--;
4713 ret.value = c_alignof_expr (expr.value);
4714 ret.original_code = ERROR_MARK;
4715 return ret;
4719 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4721 postfix-expression:
4722 primary-expression
4723 postfix-expression [ expression ]
4724 postfix-expression ( argument-expression-list[opt] )
4725 postfix-expression . identifier
4726 postfix-expression -> identifier
4727 postfix-expression ++
4728 postfix-expression --
4729 ( type-name ) { initializer-list }
4730 ( type-name ) { initializer-list , }
4732 argument-expression-list:
4733 argument-expression
4734 argument-expression-list , argument-expression
4736 primary-expression:
4737 identifier
4738 constant
4739 string-literal
4740 ( expression )
4742 GNU extensions:
4744 primary-expression:
4745 __func__
4746 (treated as a keyword in GNU C)
4747 __FUNCTION__
4748 __PRETTY_FUNCTION__
4749 ( compound-statement )
4750 __builtin_va_arg ( assignment-expression , type-name )
4751 __builtin_offsetof ( type-name , offsetof-member-designator )
4752 __builtin_choose_expr ( assignment-expression ,
4753 assignment-expression ,
4754 assignment-expression )
4755 __builtin_types_compatible_p ( type-name , type-name )
4757 offsetof-member-designator:
4758 identifier
4759 offsetof-member-designator . identifier
4760 offsetof-member-designator [ expression ]
4762 Objective-C:
4764 primary-expression:
4765 [ objc-receiver objc-message-args ]
4766 @selector ( objc-selector-arg )
4767 @protocol ( identifier )
4768 @encode ( type-name )
4769 objc-string-literal
4772 static struct c_expr
4773 c_parser_postfix_expression (c_parser *parser)
4775 struct c_expr expr, e1, e2, e3;
4776 struct c_type_name *t1, *t2;
4777 switch (c_parser_peek_token (parser)->type)
4779 case CPP_NUMBER:
4780 case CPP_CHAR:
4781 case CPP_WCHAR:
4782 expr.value = c_parser_peek_token (parser)->value;
4783 expr.original_code = ERROR_MARK;
4784 c_parser_consume_token (parser);
4785 break;
4786 case CPP_STRING:
4787 case CPP_WSTRING:
4788 expr.value = c_parser_peek_token (parser)->value;
4789 expr.original_code = STRING_CST;
4790 c_parser_consume_token (parser);
4791 break;
4792 case CPP_OBJC_STRING:
4793 gcc_assert (c_dialect_objc ());
4794 expr.value
4795 = objc_build_string_object (c_parser_peek_token (parser)->value);
4796 expr.original_code = ERROR_MARK;
4797 c_parser_consume_token (parser);
4798 break;
4799 case CPP_NAME:
4800 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
4802 c_parser_error (parser, "expected expression");
4803 expr.value = error_mark_node;
4804 expr.original_code = ERROR_MARK;
4805 break;
4808 tree id = c_parser_peek_token (parser)->value;
4809 location_t loc = c_parser_peek_token (parser)->location;
4810 c_parser_consume_token (parser);
4811 expr.value = build_external_ref (id,
4812 (c_parser_peek_token (parser)->type
4813 == CPP_OPEN_PAREN), loc);
4814 expr.original_code = ERROR_MARK;
4816 break;
4817 case CPP_OPEN_PAREN:
4818 /* A parenthesized expression, statement expression or compound
4819 literal. */
4820 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
4822 /* A statement expression. */
4823 tree stmt;
4824 c_parser_consume_token (parser);
4825 c_parser_consume_token (parser);
4826 if (cur_stmt_list == NULL)
4828 error ("braced-group within expression allowed "
4829 "only inside a function");
4830 parser->error = true;
4831 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
4832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4833 expr.value = error_mark_node;
4834 expr.original_code = ERROR_MARK;
4835 break;
4837 stmt = c_begin_stmt_expr ();
4838 c_parser_compound_statement_nostart (parser);
4839 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4840 "expected %<)%>");
4841 if (pedantic)
4842 pedwarn ("ISO C forbids braced-groups within expressions");
4843 expr.value = c_finish_stmt_expr (stmt);
4844 expr.original_code = ERROR_MARK;
4846 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4848 /* A compound literal. ??? Can we actually get here rather
4849 than going directly to
4850 c_parser_postfix_expression_after_paren_type from
4851 elsewhere? */
4852 struct c_type_name *type_name;
4853 c_parser_consume_token (parser);
4854 type_name = c_parser_type_name (parser);
4855 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4856 "expected %<)%>");
4857 if (type_name == NULL)
4859 expr.value = error_mark_node;
4860 expr.original_code = ERROR_MARK;
4862 else
4863 expr = c_parser_postfix_expression_after_paren_type (parser,
4864 type_name);
4866 else
4868 /* A parenthesized expression. */
4869 c_parser_consume_token (parser);
4870 expr = c_parser_expression (parser);
4871 if (TREE_CODE (expr.value) == MODIFY_EXPR)
4872 TREE_NO_WARNING (expr.value) = 1;
4873 expr.original_code = ERROR_MARK;
4874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4875 "expected %<)%>");
4877 break;
4878 case CPP_KEYWORD:
4879 switch (c_parser_peek_token (parser)->keyword)
4881 case RID_FUNCTION_NAME:
4882 case RID_PRETTY_FUNCTION_NAME:
4883 case RID_C99_FUNCTION_NAME:
4884 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
4885 c_parser_peek_token (parser)->value);
4886 expr.original_code = ERROR_MARK;
4887 c_parser_consume_token (parser);
4888 break;
4889 case RID_VA_ARG:
4890 c_parser_consume_token (parser);
4891 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4893 expr.value = error_mark_node;
4894 expr.original_code = ERROR_MARK;
4895 break;
4897 e1 = c_parser_expr_no_commas (parser, NULL);
4898 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4900 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4901 expr.value = error_mark_node;
4902 expr.original_code = ERROR_MARK;
4903 break;
4905 t1 = c_parser_type_name (parser);
4906 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4907 "expected %<)%>");
4908 if (t1 == NULL)
4910 expr.value = error_mark_node;
4911 expr.original_code = ERROR_MARK;
4913 else
4915 expr.value = build_va_arg (e1.value, groktypename (t1));
4916 expr.original_code = ERROR_MARK;
4918 break;
4919 case RID_OFFSETOF:
4920 c_parser_consume_token (parser);
4921 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4923 expr.value = error_mark_node;
4924 expr.original_code = ERROR_MARK;
4925 break;
4927 t1 = c_parser_type_name (parser);
4928 if (t1 == NULL)
4930 expr.value = error_mark_node;
4931 expr.original_code = ERROR_MARK;
4932 break;
4934 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
4936 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4937 expr.value = error_mark_node;
4938 expr.original_code = ERROR_MARK;
4939 break;
4942 tree type = groktypename (t1);
4943 tree offsetof_ref;
4944 if (type == error_mark_node)
4945 offsetof_ref = error_mark_node;
4946 else
4947 offsetof_ref = build1 (INDIRECT_REF, type, NULL);
4948 /* Parse the second argument to __builtin_offsetof. We
4949 must have one identifier, and beyond that we want to
4950 accept sub structure and sub array references. */
4951 if (c_parser_next_token_is (parser, CPP_NAME))
4953 offsetof_ref = build_component_ref
4954 (offsetof_ref, c_parser_peek_token (parser)->value);
4955 c_parser_consume_token (parser);
4956 while (c_parser_next_token_is (parser, CPP_DOT)
4957 || c_parser_next_token_is (parser,
4958 CPP_OPEN_SQUARE))
4960 if (c_parser_next_token_is (parser, CPP_DOT))
4962 c_parser_consume_token (parser);
4963 if (c_parser_next_token_is_not (parser,
4964 CPP_NAME))
4966 c_parser_error (parser, "expected identifier");
4967 break;
4969 offsetof_ref = build_component_ref
4970 (offsetof_ref,
4971 c_parser_peek_token (parser)->value);
4972 c_parser_consume_token (parser);
4974 else
4976 tree idx;
4977 c_parser_consume_token (parser);
4978 idx = c_parser_expression (parser).value;
4979 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4980 "expected %<]%>");
4981 offsetof_ref = build_array_ref (offsetof_ref, idx);
4985 else
4986 c_parser_error (parser, "expected identifier");
4987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4988 "expected %<)%>");
4989 expr.value = fold_offsetof (offsetof_ref);
4990 expr.original_code = ERROR_MARK;
4992 break;
4993 case RID_CHOOSE_EXPR:
4994 c_parser_consume_token (parser);
4995 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4997 expr.value = error_mark_node;
4998 expr.original_code = ERROR_MARK;
4999 break;
5001 e1 = c_parser_expr_no_commas (parser, NULL);
5002 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5005 expr.value = error_mark_node;
5006 expr.original_code = ERROR_MARK;
5007 break;
5009 e2 = c_parser_expr_no_commas (parser, NULL);
5010 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5012 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5013 expr.value = error_mark_node;
5014 expr.original_code = ERROR_MARK;
5015 break;
5017 e3 = c_parser_expr_no_commas (parser, NULL);
5018 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5019 "expected %<)%>");
5021 tree c;
5023 c = fold (e1.value);
5024 if (TREE_CODE (c) != INTEGER_CST)
5025 error ("first argument to %<__builtin_choose_expr%> not"
5026 " a constant");
5027 expr = integer_zerop (c) ? e3 : e2;
5029 break;
5030 case RID_TYPES_COMPATIBLE_P:
5031 c_parser_consume_token (parser);
5032 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5034 expr.value = error_mark_node;
5035 expr.original_code = ERROR_MARK;
5036 break;
5038 t1 = c_parser_type_name (parser);
5039 if (t1 == NULL)
5041 expr.value = error_mark_node;
5042 expr.original_code = ERROR_MARK;
5043 break;
5045 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5047 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5048 expr.value = error_mark_node;
5049 expr.original_code = ERROR_MARK;
5050 break;
5052 t2 = c_parser_type_name (parser);
5053 if (t2 == NULL)
5055 expr.value = error_mark_node;
5056 expr.original_code = ERROR_MARK;
5057 break;
5059 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5060 "expected %<)%>");
5062 tree e1, e2;
5064 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5065 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5067 expr.value = comptypes (e1, e2)
5068 ? build_int_cst (NULL_TREE, 1)
5069 : build_int_cst (NULL_TREE, 0);
5070 expr.original_code = ERROR_MARK;
5072 break;
5073 case RID_AT_SELECTOR:
5074 gcc_assert (c_dialect_objc ());
5075 c_parser_consume_token (parser);
5076 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5078 expr.value = error_mark_node;
5079 expr.original_code = ERROR_MARK;
5080 break;
5083 tree sel = c_parser_objc_selector_arg (parser);
5084 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5085 "expected %<)%>");
5086 expr.value = objc_build_selector_expr (sel);
5087 expr.original_code = ERROR_MARK;
5089 break;
5090 case RID_AT_PROTOCOL:
5091 gcc_assert (c_dialect_objc ());
5092 c_parser_consume_token (parser);
5093 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5095 expr.value = error_mark_node;
5096 expr.original_code = ERROR_MARK;
5097 break;
5099 if (c_parser_next_token_is_not (parser, CPP_NAME))
5101 c_parser_error (parser, "expected identifier");
5102 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5103 expr.value = error_mark_node;
5104 expr.original_code = ERROR_MARK;
5105 break;
5108 tree id = c_parser_peek_token (parser)->value;
5109 c_parser_consume_token (parser);
5110 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5111 "expected %<)%>");
5112 expr.value = objc_build_protocol_expr (id);
5113 expr.original_code = ERROR_MARK;
5115 break;
5116 case RID_AT_ENCODE:
5117 /* Extension to support C-structures in the archiver. */
5118 gcc_assert (c_dialect_objc ());
5119 c_parser_consume_token (parser);
5120 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5122 expr.value = error_mark_node;
5123 expr.original_code = ERROR_MARK;
5124 break;
5126 t1 = c_parser_type_name (parser);
5127 if (t1 == NULL)
5129 expr.value = error_mark_node;
5130 expr.original_code = ERROR_MARK;
5131 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5132 break;
5134 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5135 "expected %<)%>");
5137 tree type = groktypename (t1);
5138 expr.value = objc_build_encode_expr (type);
5139 expr.original_code = ERROR_MARK;
5141 break;
5142 default:
5143 c_parser_error (parser, "expected expression");
5144 expr.value = error_mark_node;
5145 expr.original_code = ERROR_MARK;
5146 break;
5148 break;
5149 case CPP_OPEN_SQUARE:
5150 if (c_dialect_objc ())
5152 tree receiver, args;
5153 c_parser_consume_token (parser);
5154 receiver = c_parser_objc_receiver (parser);
5155 args = c_parser_objc_message_args (parser);
5156 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5157 "expected %<]%>");
5158 expr.value = objc_build_message_expr (build_tree_list (receiver,
5159 args));
5160 expr.original_code = ERROR_MARK;
5161 break;
5163 /* Else fall through to report error. */
5164 default:
5165 c_parser_error (parser, "expected expression");
5166 expr.value = error_mark_node;
5167 expr.original_code = ERROR_MARK;
5168 break;
5170 return c_parser_postfix_expression_after_primary (parser, expr);
5173 /* Parse a postfix expression after a parenthesized type name: the
5174 brace-enclosed initializer of a compound literal, possibly followed
5175 by some postfix operators. This is separate because it is not
5176 possible to tell until after the type name whether a cast
5177 expression has a cast or a compound literal, or whether the operand
5178 of sizeof is a parenthesized type name or starts with a compound
5179 literal. */
5181 static struct c_expr
5182 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5183 struct c_type_name *type_name)
5185 tree type;
5186 struct c_expr init;
5187 struct c_expr expr;
5188 start_init (NULL_TREE, NULL, 0);
5189 type = groktypename (type_name);
5190 if (C_TYPE_VARIABLE_SIZE (type))
5192 error ("compound literal has variable size");
5193 type = error_mark_node;
5195 init = c_parser_braced_init (parser, type, false);
5196 finish_init ();
5197 maybe_warn_string_init (type, init);
5199 if (pedantic && !flag_isoc99)
5200 pedwarn ("ISO C90 forbids compound literals");
5201 expr.value = build_compound_literal (type, init.value);
5202 expr.original_code = ERROR_MARK;
5203 return c_parser_postfix_expression_after_primary (parser, expr);
5206 /* Parse a postfix expression after the initial primary or compound
5207 literal; that is, parse a series of postfix operators. */
5209 static struct c_expr
5210 c_parser_postfix_expression_after_primary (c_parser *parser,
5211 struct c_expr expr)
5213 tree ident, idx, exprlist;
5214 while (true)
5216 switch (c_parser_peek_token (parser)->type)
5218 case CPP_OPEN_SQUARE:
5219 /* Array reference. */
5220 c_parser_consume_token (parser);
5221 idx = c_parser_expression (parser).value;
5222 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5223 "expected %<]%>");
5224 expr.value = build_array_ref (expr.value, idx);
5225 expr.original_code = ERROR_MARK;
5226 break;
5227 case CPP_OPEN_PAREN:
5228 /* Function call. */
5229 c_parser_consume_token (parser);
5230 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5231 exprlist = NULL_TREE;
5232 else
5233 exprlist = c_parser_expr_list (parser);
5234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5235 "expected %<)%>");
5236 expr.value = build_function_call (expr.value, exprlist);
5237 expr.original_code = ERROR_MARK;
5238 break;
5239 case CPP_DOT:
5240 /* Structure element reference. */
5241 c_parser_consume_token (parser);
5242 if (c_parser_next_token_is (parser, CPP_NAME))
5243 ident = c_parser_peek_token (parser)->value;
5244 else
5246 c_parser_error (parser, "expected identifier");
5247 expr.value = error_mark_node;
5248 expr.original_code = ERROR_MARK;
5249 return expr;
5251 c_parser_consume_token (parser);
5252 expr.value = build_component_ref (expr.value, ident);
5253 expr.original_code = ERROR_MARK;
5254 break;
5255 case CPP_DEREF:
5256 /* Structure element reference. */
5257 c_parser_consume_token (parser);
5258 if (c_parser_next_token_is (parser, CPP_NAME))
5259 ident = c_parser_peek_token (parser)->value;
5260 else
5262 c_parser_error (parser, "expected identifier");
5263 expr.value = error_mark_node;
5264 expr.original_code = ERROR_MARK;
5265 return expr;
5267 c_parser_consume_token (parser);
5268 expr.value = build_component_ref (build_indirect_ref (expr.value,
5269 "->"), ident);
5270 expr.original_code = ERROR_MARK;
5271 break;
5272 case CPP_PLUS_PLUS:
5273 /* Postincrement. */
5274 c_parser_consume_token (parser);
5275 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5276 expr.original_code = ERROR_MARK;
5277 break;
5278 case CPP_MINUS_MINUS:
5279 /* Postdecrement. */
5280 c_parser_consume_token (parser);
5281 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5282 expr.original_code = ERROR_MARK;
5283 break;
5284 default:
5285 return expr;
5290 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5292 expression:
5293 assignment-expression
5294 expression , assignment-expression
5297 static struct c_expr
5298 c_parser_expression (c_parser *parser)
5300 struct c_expr expr;
5301 expr = c_parser_expr_no_commas (parser, NULL);
5302 while (c_parser_next_token_is (parser, CPP_COMMA))
5304 struct c_expr next;
5305 c_parser_consume_token (parser);
5306 next = c_parser_expr_no_commas (parser, NULL);
5307 expr.value = build_compound_expr (expr.value, next.value);
5308 expr.original_code = COMPOUND_EXPR;
5310 return expr;
5313 /* Parse a non-empty list of expressions.
5315 nonempty-expr-list:
5316 assignment-expression
5317 nonempty-expr-list , assignment-expression
5320 static tree
5321 c_parser_expr_list (c_parser *parser)
5323 struct c_expr expr;
5324 tree ret;
5325 expr = c_parser_expr_no_commas (parser, NULL);
5326 ret = build_tree_list (NULL_TREE, expr.value);
5327 while (c_parser_next_token_is (parser, CPP_COMMA))
5329 c_parser_consume_token (parser);
5330 expr = c_parser_expr_no_commas (parser, NULL);
5331 ret = chainon (ret, build_tree_list (NULL_TREE, expr.value));
5333 return ret;
5337 /* Parse Objective-C-specific constructs. */
5339 /* Parse an objc-class-definition.
5341 objc-class-definition:
5342 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5343 objc-class-instance-variables[opt] objc-methodprotolist @end
5344 @implementation identifier objc-superclass[opt]
5345 objc-class-instance-variables[opt]
5346 @interface identifier ( identifier ) objc-protocol-refs[opt]
5347 objc-methodprotolist @end
5348 @implementation identifier ( identifier )
5350 objc-superclass:
5351 : identifier
5353 "@interface identifier (" must start "@interface identifier (
5354 identifier ) ...": objc-methodprotolist in the first production may
5355 not start with a parenthesised identifier as a declarator of a data
5356 definition with no declaration specifiers if the objc-superclass,
5357 objc-protocol-refs and objc-class-instance-variables are omitted. */
5359 static void
5360 c_parser_objc_class_definition (c_parser *parser)
5362 bool iface_p;
5363 tree id1;
5364 tree superclass;
5365 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5366 iface_p = true;
5367 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5368 iface_p = false;
5369 else
5370 gcc_unreachable ();
5371 c_parser_consume_token (parser);
5372 if (c_parser_next_token_is_not (parser, CPP_NAME))
5374 c_parser_error (parser, "expected identifier");
5375 return;
5377 id1 = c_parser_peek_token (parser)->value;
5378 c_parser_consume_token (parser);
5379 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5381 tree id2;
5382 tree proto = NULL_TREE;
5383 c_parser_consume_token (parser);
5384 if (c_parser_next_token_is_not (parser, CPP_NAME))
5386 c_parser_error (parser, "expected identifier");
5387 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5388 return;
5390 id2 = c_parser_peek_token (parser)->value;
5391 c_parser_consume_token (parser);
5392 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5393 if (!iface_p)
5395 objc_start_category_implementation (id1, id2);
5396 return;
5398 if (c_parser_next_token_is (parser, CPP_LESS))
5399 proto = c_parser_objc_protocol_refs (parser);
5400 objc_start_category_interface (id1, id2, proto);
5401 c_parser_objc_methodprotolist (parser);
5402 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5403 objc_finish_interface ();
5404 return;
5406 if (c_parser_next_token_is (parser, CPP_COLON))
5408 c_parser_consume_token (parser);
5409 if (c_parser_next_token_is_not (parser, CPP_NAME))
5411 c_parser_error (parser, "expected identifier");
5412 return;
5414 superclass = c_parser_peek_token (parser)->value;
5415 c_parser_consume_token (parser);
5417 else
5418 superclass = NULL_TREE;
5419 if (iface_p)
5421 tree proto = NULL_TREE;
5422 if (c_parser_next_token_is (parser, CPP_LESS))
5423 proto = c_parser_objc_protocol_refs (parser);
5424 objc_start_class_interface (id1, superclass, proto);
5426 else
5427 objc_start_class_implementation (id1, superclass);
5428 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5429 c_parser_objc_class_instance_variables (parser);
5430 if (iface_p)
5432 objc_continue_interface ();
5433 c_parser_objc_methodprotolist (parser);
5434 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5435 objc_finish_interface ();
5437 else
5439 objc_continue_implementation ();
5440 return;
5444 /* Parse objc-class-instance-variables.
5446 objc-class-instance-variables:
5447 { objc-instance-variable-decl-list[opt] }
5449 objc-instance-variable-decl-list:
5450 objc-visibility-spec
5451 objc-instance-variable-decl ;
5453 objc-instance-variable-decl-list objc-visibility-spec
5454 objc-instance-variable-decl-list objc-instance-variable-decl ;
5455 objc-instance-variable-decl-list ;
5457 objc-visibility-spec:
5458 @private
5459 @protected
5460 @public
5462 objc-instance-variable-decl:
5463 struct-declaration
5466 static void
5467 c_parser_objc_class_instance_variables (c_parser *parser)
5469 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5470 c_parser_consume_token (parser);
5471 while (c_parser_next_token_is_not (parser, CPP_EOF))
5473 tree decls;
5474 /* Parse any stray semicolon. */
5475 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5477 if (pedantic)
5478 pedwarn ("extra semicolon in struct or union specified");
5479 c_parser_consume_token (parser);
5480 continue;
5482 /* Stop if at the end of the instance variables. */
5483 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5485 c_parser_consume_token (parser);
5486 break;
5488 /* Parse any objc-visibility-spec. */
5489 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5491 c_parser_consume_token (parser);
5492 objc_set_visibility (2);
5493 continue;
5495 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5497 c_parser_consume_token (parser);
5498 objc_set_visibility (0);
5499 continue;
5501 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5503 c_parser_consume_token (parser);
5504 objc_set_visibility (1);
5505 continue;
5507 /* Parse some comma-separated declarations. */
5508 decls = c_parser_struct_declaration (parser);
5510 /* Comma-separated instance variables are chained together in
5511 reverse order; add them one by one. */
5512 tree ivar = nreverse (decls);
5513 for (; ivar; ivar = TREE_CHAIN (ivar))
5514 objc_add_instance_variable (copy_node (ivar));
5516 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5520 /* Parse an objc-class-declaration.
5522 objc-class-declaration:
5523 @class identifier-list ;
5526 static void
5527 c_parser_objc_class_declaration (c_parser *parser)
5529 tree list = NULL_TREE;
5530 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5531 c_parser_consume_token (parser);
5532 /* Any identifiers, including those declared as type names, are OK
5533 here. */
5534 while (true)
5536 tree id;
5537 if (c_parser_next_token_is_not (parser, CPP_NAME))
5539 c_parser_error (parser, "expected identifier");
5540 break;
5542 id = c_parser_peek_token (parser)->value;
5543 list = chainon (list, build_tree_list (NULL_TREE, id));
5544 c_parser_consume_token (parser);
5545 if (c_parser_next_token_is (parser, CPP_COMMA))
5546 c_parser_consume_token (parser);
5547 else
5548 break;
5550 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5551 objc_declare_class (list);
5554 /* Parse an objc-alias-declaration.
5556 objc-alias-declaration:
5557 @compatibility_alias identifier identifier ;
5560 static void
5561 c_parser_objc_alias_declaration (c_parser *parser)
5563 tree id1, id2;
5564 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5565 c_parser_consume_token (parser);
5566 if (c_parser_next_token_is_not (parser, CPP_NAME))
5568 c_parser_error (parser, "expected identifier");
5569 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5570 return;
5572 id1 = c_parser_peek_token (parser)->value;
5573 c_parser_consume_token (parser);
5574 if (c_parser_next_token_is_not (parser, CPP_NAME))
5576 c_parser_error (parser, "expected identifier");
5577 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5578 return;
5580 id2 = c_parser_peek_token (parser)->value;
5581 c_parser_consume_token (parser);
5582 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5583 objc_declare_alias (id1, id2);
5586 /* Parse an objc-protocol-definition.
5588 objc-protocol-definition:
5589 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5590 @protocol identifier-list ;
5592 "@protocol identifier ;" should be resolved as "@protocol
5593 identifier-list ;": objc-methodprotolist may not start with a
5594 semicolon in the first alternative if objc-protocol-refs are
5595 omitted. */
5597 static void
5598 c_parser_objc_protocol_definition (c_parser *parser)
5600 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5601 c_parser_consume_token (parser);
5602 if (c_parser_next_token_is_not (parser, CPP_NAME))
5604 c_parser_error (parser, "expected identifier");
5605 return;
5607 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5608 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5610 tree list = NULL_TREE;
5611 /* Any identifiers, including those declared as type names, are
5612 OK here. */
5613 while (true)
5615 tree id;
5616 if (c_parser_next_token_is_not (parser, CPP_NAME))
5618 c_parser_error (parser, "expected identifier");
5619 break;
5621 id = c_parser_peek_token (parser)->value;
5622 list = chainon (list, build_tree_list (NULL_TREE, id));
5623 c_parser_consume_token (parser);
5624 if (c_parser_next_token_is (parser, CPP_COMMA))
5625 c_parser_consume_token (parser);
5626 else
5627 break;
5629 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5630 objc_declare_protocols (list);
5632 else
5634 tree id = c_parser_peek_token (parser)->value;
5635 tree proto = NULL_TREE;
5636 c_parser_consume_token (parser);
5637 if (c_parser_next_token_is (parser, CPP_LESS))
5638 proto = c_parser_objc_protocol_refs (parser);
5639 objc_pq_context = 1;
5640 objc_start_protocol (id, proto);
5641 c_parser_objc_methodprotolist (parser);
5642 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5643 objc_pq_context = 0;
5644 objc_finish_interface ();
5648 /* Parse an objc-method-type.
5650 objc-method-type:
5655 static enum tree_code
5656 c_parser_objc_method_type (c_parser *parser)
5658 switch (c_parser_peek_token (parser)->type)
5660 case CPP_PLUS:
5661 c_parser_consume_token (parser);
5662 return PLUS_EXPR;
5663 case CPP_MINUS:
5664 c_parser_consume_token (parser);
5665 return MINUS_EXPR;
5666 default:
5667 gcc_unreachable ();
5671 /* Parse an objc-method-definition.
5673 objc-method-definition:
5674 objc-method-type objc-method-decl ;[opt] compound-statement
5677 static void
5678 c_parser_objc_method_definition (c_parser *parser)
5680 enum tree_code type = c_parser_objc_method_type (parser);
5681 tree decl;
5682 objc_set_method_type (type);
5683 objc_pq_context = 1;
5684 decl = c_parser_objc_method_decl (parser);
5685 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5687 c_parser_consume_token (parser);
5688 if (pedantic)
5689 pedwarn ("extra semicolon in method definition specified");
5691 objc_pq_context = 0;
5692 objc_start_method_definition (decl);
5693 add_stmt (c_parser_compound_statement (parser));
5694 objc_finish_method_definition (current_function_decl);
5697 /* Parse an objc-methodprotolist.
5699 objc-methodprotolist:
5700 empty
5701 objc-methodprotolist objc-methodproto
5702 objc-methodprotolist declaration
5703 objc-methodprotolist ;
5705 The declaration is a data definition, which may be missing
5706 declaration specifiers under the same rules and diagnostics as
5707 other data definitions outside functions, and the stray semicolon
5708 is diagnosed the same way as a stray semicolon outside a
5709 function. */
5711 static void
5712 c_parser_objc_methodprotolist (c_parser *parser)
5714 while (true)
5716 /* The list is terminated by @end. */
5717 switch (c_parser_peek_token (parser)->type)
5719 case CPP_SEMICOLON:
5720 if (pedantic)
5721 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
5722 c_parser_consume_token (parser);
5723 break;
5724 case CPP_PLUS:
5725 case CPP_MINUS:
5726 c_parser_objc_methodproto (parser);
5727 break;
5728 case CPP_EOF:
5729 return;
5730 default:
5731 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
5732 return;
5733 c_parser_declaration_or_fndef (parser, false, true, false, true);
5734 break;
5739 /* Parse an objc-methodproto.
5741 objc-methodproto:
5742 objc-method-type objc-method-decl ;
5745 static void
5746 c_parser_objc_methodproto (c_parser *parser)
5748 enum tree_code type = c_parser_objc_method_type (parser);
5749 tree decl;
5750 objc_set_method_type (type);
5751 /* Remember protocol qualifiers in prototypes. */
5752 objc_pq_context = 1;
5753 decl = c_parser_objc_method_decl (parser);
5754 /* Forget protocol qualifiers here. */
5755 objc_pq_context = 0;
5756 objc_add_method_declaration (decl);
5757 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5760 /* Parse an objc-method-decl.
5762 objc-method-decl:
5763 ( objc-type-name ) objc-selector
5764 objc-selector
5765 ( objc-type-name ) objc-keyword-selector objc-optparmlist
5766 objc-keyword-selector objc-optparmlist
5768 objc-keyword-selector:
5769 objc-keyword-decl
5770 objc-keyword-selector objc-keyword-decl
5772 objc-keyword-decl:
5773 objc-selector : ( objc-type-name ) identifier
5774 objc-selector : identifier
5775 : ( objc-type-name ) identifier
5776 : identifier
5778 objc-optparmlist:
5779 objc-optparms objc-optellipsis
5781 objc-optparms:
5782 empty
5783 objc-opt-parms , parameter-declaration
5785 objc-optellipsis:
5786 empty
5787 , ...
5790 static tree
5791 c_parser_objc_method_decl (c_parser *parser)
5793 tree type = NULL_TREE;
5794 tree sel;
5795 tree parms = NULL_TREE;
5796 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5798 c_parser_consume_token (parser);
5799 type = c_parser_objc_type_name (parser);
5800 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5802 sel = c_parser_objc_selector (parser);
5803 /* If there is no selector, or a colon follows, we have an
5804 objc-keyword-selector. If there is a selector, and a colon does
5805 not follow, that selector ends the objc-method-decl. */
5806 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
5808 tree tsel = sel;
5809 tree list = NULL_TREE;
5810 bool ellipsis;
5811 while (true)
5813 tree atype = NULL_TREE, id, keyworddecl;
5814 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5815 break;
5816 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5818 c_parser_consume_token (parser);
5819 atype = c_parser_objc_type_name (parser);
5820 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5821 "expected %<)%>");
5823 if (c_parser_next_token_is_not (parser, CPP_NAME))
5825 c_parser_error (parser, "expected identifier");
5826 return error_mark_node;
5828 id = c_parser_peek_token (parser)->value;
5829 c_parser_consume_token (parser);
5830 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
5831 list = chainon (list, keyworddecl);
5832 tsel = c_parser_objc_selector (parser);
5833 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
5834 break;
5836 /* Parse the optional parameter list. Optional Objective-C
5837 method parameters follow the C syntax, and may include '...'
5838 to denote a variable number of arguments. */
5839 parms = make_node (TREE_LIST);
5840 ellipsis = false;
5841 while (c_parser_next_token_is (parser, CPP_COMMA))
5843 struct c_parm *parm;
5844 c_parser_consume_token (parser);
5845 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5847 ellipsis = true;
5848 c_parser_consume_token (parser);
5849 break;
5851 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5852 if (parm == NULL)
5853 break;
5854 parms = chainon (parms,
5855 build_tree_list (NULL_TREE, grokparm (parm)));
5857 TREE_OVERFLOW (parms) = ellipsis;
5858 sel = list;
5860 return objc_build_method_signature (type, sel, parms);
5863 /* Parse an objc-type-name.
5865 objc-type-name:
5866 objc-type-qualifiers[opt] type-name
5867 objc-type-qualifiers[opt]
5869 objc-type-qualifiers:
5870 objc-type-qualifier
5871 objc-type-qualifiers objc-type-qualifier
5873 objc-type-qualifier: one of
5874 in out inout bycopy byref oneway
5877 static tree
5878 c_parser_objc_type_name (c_parser *parser)
5880 tree quals = NULL_TREE;
5881 struct c_type_name *typename = NULL;
5882 tree type = NULL_TREE;
5883 while (true)
5885 c_token *token = c_parser_peek_token (parser);
5886 if (token->type == CPP_KEYWORD
5887 && (token->keyword == RID_IN
5888 || token->keyword == RID_OUT
5889 || token->keyword == RID_INOUT
5890 || token->keyword == RID_BYCOPY
5891 || token->keyword == RID_BYREF
5892 || token->keyword == RID_ONEWAY))
5894 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
5895 c_parser_consume_token (parser);
5897 else
5898 break;
5900 if (c_parser_next_token_starts_typename (parser))
5901 typename = c_parser_type_name (parser);
5902 if (typename)
5903 type = groktypename (typename);
5904 return build_tree_list (quals, type);
5907 /* Parse objc-protocol-refs.
5909 objc-protocol-refs:
5910 < identifier-list >
5913 static tree
5914 c_parser_objc_protocol_refs (c_parser *parser)
5916 tree list = NULL_TREE;
5917 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
5918 c_parser_consume_token (parser);
5919 /* Any identifiers, including those declared as type names, are OK
5920 here. */
5921 while (true)
5923 tree id;
5924 if (c_parser_next_token_is_not (parser, CPP_NAME))
5926 c_parser_error (parser, "expected identifier");
5927 break;
5929 id = c_parser_peek_token (parser)->value;
5930 list = chainon (list, build_tree_list (NULL_TREE, id));
5931 c_parser_consume_token (parser);
5932 if (c_parser_next_token_is (parser, CPP_COMMA))
5933 c_parser_consume_token (parser);
5934 else
5935 break;
5937 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
5938 return list;
5941 /* Parse an objc-try-catch-statement.
5943 objc-try-catch-statement:
5944 @try compound-statement objc-catch-list[opt]
5945 @try compound-statement objc-catch-list[opt] @finally compound-statement
5947 objc-catch-list:
5948 @catch ( parameter-declaration ) compound-statement
5949 objc-catch-list @catch ( parameter-declaration ) compound-statement
5952 static void
5953 c_parser_objc_try_catch_statement (c_parser *parser)
5955 location_t loc;
5956 tree stmt;
5957 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
5958 c_parser_consume_token (parser);
5959 loc = c_parser_peek_token (parser)->location;
5960 stmt = c_parser_compound_statement (parser);
5961 objc_begin_try_stmt (loc, stmt);
5962 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
5964 struct c_parm *parm;
5965 c_parser_consume_token (parser);
5966 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5967 break;
5968 parm = c_parser_parameter_declaration (parser, NULL_TREE);
5969 if (parm == NULL)
5971 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5972 break;
5974 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5975 objc_begin_catch_clause (grokparm (parm));
5976 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5977 c_parser_compound_statement_nostart (parser);
5978 objc_finish_catch_clause ();
5980 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
5982 location_t finloc;
5983 tree finstmt;
5984 c_parser_consume_token (parser);
5985 finloc = c_parser_peek_token (parser)->location;
5986 finstmt = c_parser_compound_statement (parser);
5987 objc_build_finally_clause (finloc, finstmt);
5989 objc_finish_try_stmt ();
5992 /* Parse an objc-synchronized-statement.
5994 objc-synchronized-statement:
5995 @synchronized ( expression ) compound-statement
5998 static void
5999 c_parser_objc_synchronized_statement (c_parser *parser)
6001 location_t loc;
6002 tree expr, stmt;
6003 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6004 c_parser_consume_token (parser);
6005 loc = c_parser_peek_token (parser)->location;
6006 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6008 expr = c_parser_expression (parser).value;
6009 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6011 else
6012 expr = error_mark_node;
6013 stmt = c_parser_compound_statement (parser);
6014 objc_build_synchronized (loc, expr, stmt);
6017 /* Parse an objc-selector; return NULL_TREE without an error if the
6018 next token is not an objc-selector.
6020 objc-selector:
6021 identifier
6022 one of
6023 enum struct union if else while do for switch case default
6024 break continue return goto asm sizeof typeof __alignof
6025 unsigned long const short volatile signed restrict _Complex
6026 in out inout bycopy byref oneway int char float double void _Bool
6028 ??? Why this selection of keywords but not, for example, storage
6029 class specifiers? */
6031 static tree
6032 c_parser_objc_selector (c_parser *parser)
6034 c_token *token = c_parser_peek_token (parser);
6035 tree value = token->value;
6036 if (token->type == CPP_NAME)
6038 c_parser_consume_token (parser);
6039 return value;
6041 if (token->type != CPP_KEYWORD)
6042 return NULL_TREE;
6043 switch (token->keyword)
6045 case RID_ENUM:
6046 case RID_STRUCT:
6047 case RID_UNION:
6048 case RID_IF:
6049 case RID_ELSE:
6050 case RID_WHILE:
6051 case RID_DO:
6052 case RID_FOR:
6053 case RID_SWITCH:
6054 case RID_CASE:
6055 case RID_DEFAULT:
6056 case RID_BREAK:
6057 case RID_CONTINUE:
6058 case RID_RETURN:
6059 case RID_GOTO:
6060 case RID_ASM:
6061 case RID_SIZEOF:
6062 case RID_TYPEOF:
6063 case RID_ALIGNOF:
6064 case RID_UNSIGNED:
6065 case RID_LONG:
6066 case RID_CONST:
6067 case RID_SHORT:
6068 case RID_VOLATILE:
6069 case RID_SIGNED:
6070 case RID_RESTRICT:
6071 case RID_COMPLEX:
6072 case RID_IN:
6073 case RID_OUT:
6074 case RID_INOUT:
6075 case RID_BYCOPY:
6076 case RID_BYREF:
6077 case RID_ONEWAY:
6078 case RID_INT:
6079 case RID_CHAR:
6080 case RID_FLOAT:
6081 case RID_DOUBLE:
6082 case RID_VOID:
6083 case RID_BOOL:
6084 c_parser_consume_token (parser);
6085 return value;
6086 default:
6087 return NULL_TREE;
6091 /* Parse an objc-selector-arg.
6093 objc-selector-arg:
6094 objc-selector
6095 objc-keywordname-list
6097 objc-keywordname-list:
6098 objc-keywordname
6099 objc-keywordname-list objc-keywordname
6101 objc-keywordname:
6102 objc-selector :
6106 static tree
6107 c_parser_objc_selector_arg (c_parser *parser)
6109 tree sel = c_parser_objc_selector (parser);
6110 tree list = NULL_TREE;
6111 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6112 return sel;
6113 while (true)
6115 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6116 return list;
6117 list = chainon (list, build_tree_list (sel, NULL_TREE));
6118 sel = c_parser_objc_selector (parser);
6119 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6120 break;
6122 return list;
6125 /* Parse an objc-receiver.
6127 objc-receiver:
6128 expression
6129 class-name
6130 type-name
6133 static tree
6134 c_parser_objc_receiver (c_parser *parser)
6136 if (c_parser_peek_token (parser)->type == CPP_NAME
6137 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6138 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6140 tree id = c_parser_peek_token (parser)->value;
6141 c_parser_consume_token (parser);
6142 return objc_get_class_reference (id);
6144 return c_parser_expression (parser).value;
6147 /* Parse objc-message-args.
6149 objc-message-args:
6150 objc-selector
6151 objc-keywordarg-list
6153 objc-keywordarg-list:
6154 objc-keywordarg
6155 objc-keywordarg-list objc-keywordarg
6157 objc-keywordarg:
6158 objc-selector : objc-keywordexpr
6159 : objc-keywordexpr
6162 static tree
6163 c_parser_objc_message_args (c_parser *parser)
6165 tree sel = c_parser_objc_selector (parser);
6166 tree list = NULL_TREE;
6167 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6168 return sel;
6169 while (true)
6171 tree keywordexpr;
6172 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6173 return list;
6174 keywordexpr = c_parser_objc_keywordexpr (parser);
6175 list = chainon (list, build_tree_list (sel, keywordexpr));
6176 sel = c_parser_objc_selector (parser);
6177 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6178 break;
6180 return list;
6183 /* Parse an objc-keywordexpr.
6185 objc-keywordexpr:
6186 nonempty-expr-list
6189 static tree
6190 c_parser_objc_keywordexpr (c_parser *parser)
6192 tree list = c_parser_expr_list (parser);
6193 if (TREE_CHAIN (list) == NULL_TREE)
6195 /* Just return the expression, remove a level of
6196 indirection. */
6197 return TREE_VALUE (list);
6199 else
6201 /* We have a comma expression, we will collapse later. */
6202 return list;
6207 /* The actual parser and external interface. ??? Does this need to be
6208 garbage-collected? */
6210 static GTY (()) c_parser *the_parser;
6212 /* Parse a single source file. */
6214 void
6215 c_parse_file (void)
6217 the_parser = c_parser_new ();
6218 c_parser_translation_unit (the_parser);
6219 the_parser = NULL;
6222 #include "gt-c-parser.h"