target-supports.exp (check_effective_target_mips16_attribute): Reject hard-float...
[official-gcc.git] / gcc / c-parser.c
blobbdf96ca6d920781e5f99adc33cacc342cafaecec
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, 2007 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 3, 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 COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* TODO:
26 Make sure all relevant comments, and all relevant code from all
27 actions, brought over from old parser. Verify exact correspondence
28 of syntax accepted.
30 Add testcases covering every input symbol in every state in old and
31 new parsers.
33 Include full syntax for GNU C, including erroneous cases accepted
34 with error messages, in syntax productions in comments.
36 Make more diagnostics in the front end generally take an explicit
37 location rather than implicitly using input_location. */
39 #include "config.h"
40 #include "system.h"
41 #include "coretypes.h"
42 #include "tm.h"
43 #include "tree.h"
44 #include "rtl.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"
56 #include "vec.h"
57 #include "target.h"
58 #include "cgraph.h"
61 /* The reserved keyword table. */
62 struct resword
64 const char *word;
65 ENUM_BITFIELD(rid) rid : 16;
66 unsigned int disable : 16;
69 /* Disable mask. Keywords are disabled if (reswords[i].disable &
70 mask) is _true_. */
71 #define D_C89 0x01 /* not in C89 */
72 #define D_EXT 0x02 /* GCC extension */
73 #define D_EXT89 0x04 /* GCC extension incorporated in C99 */
74 #define D_OBJC 0x08 /* Objective C only */
76 static const struct resword reswords[] =
78 { "_Bool", RID_BOOL, 0 },
79 { "_Complex", RID_COMPLEX, 0 },
80 { "_Decimal32", RID_DFLOAT32, D_EXT },
81 { "_Decimal64", RID_DFLOAT64, D_EXT },
82 { "_Decimal128", RID_DFLOAT128, D_EXT },
83 { "_Fract", RID_FRACT, D_EXT },
84 { "_Accum", RID_ACCUM, D_EXT },
85 { "_Sat", RID_SAT, D_EXT },
86 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
87 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
88 { "__alignof", RID_ALIGNOF, 0 },
89 { "__alignof__", RID_ALIGNOF, 0 },
90 { "__asm", RID_ASM, 0 },
91 { "__asm__", RID_ASM, 0 },
92 { "__attribute", RID_ATTRIBUTE, 0 },
93 { "__attribute__", RID_ATTRIBUTE, 0 },
94 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
95 { "__builtin_offsetof", RID_OFFSETOF, 0 },
96 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
97 { "__builtin_va_arg", RID_VA_ARG, 0 },
98 { "__complex", RID_COMPLEX, 0 },
99 { "__complex__", RID_COMPLEX, 0 },
100 { "__const", RID_CONST, 0 },
101 { "__const__", RID_CONST, 0 },
102 { "__extension__", RID_EXTENSION, 0 },
103 { "__func__", RID_C99_FUNCTION_NAME, 0 },
104 { "__imag", RID_IMAGPART, 0 },
105 { "__imag__", RID_IMAGPART, 0 },
106 { "__inline", RID_INLINE, 0 },
107 { "__inline__", RID_INLINE, 0 },
108 { "__label__", RID_LABEL, 0 },
109 { "__real", RID_REALPART, 0 },
110 { "__real__", RID_REALPART, 0 },
111 { "__restrict", RID_RESTRICT, 0 },
112 { "__restrict__", RID_RESTRICT, 0 },
113 { "__signed", RID_SIGNED, 0 },
114 { "__signed__", RID_SIGNED, 0 },
115 { "__thread", RID_THREAD, 0 },
116 { "__typeof", RID_TYPEOF, 0 },
117 { "__typeof__", RID_TYPEOF, 0 },
118 { "__volatile", RID_VOLATILE, 0 },
119 { "__volatile__", RID_VOLATILE, 0 },
120 { "asm", RID_ASM, D_EXT },
121 { "auto", RID_AUTO, 0 },
122 { "break", RID_BREAK, 0 },
123 { "case", RID_CASE, 0 },
124 { "char", RID_CHAR, 0 },
125 { "const", RID_CONST, 0 },
126 { "continue", RID_CONTINUE, 0 },
127 { "default", RID_DEFAULT, 0 },
128 { "do", RID_DO, 0 },
129 { "double", RID_DOUBLE, 0 },
130 { "else", RID_ELSE, 0 },
131 { "enum", RID_ENUM, 0 },
132 { "extern", RID_EXTERN, 0 },
133 { "float", RID_FLOAT, 0 },
134 { "for", RID_FOR, 0 },
135 { "goto", RID_GOTO, 0 },
136 { "if", RID_IF, 0 },
137 { "inline", RID_INLINE, D_EXT89 },
138 { "int", RID_INT, 0 },
139 { "long", RID_LONG, 0 },
140 { "register", RID_REGISTER, 0 },
141 { "restrict", RID_RESTRICT, D_C89 },
142 { "return", RID_RETURN, 0 },
143 { "short", RID_SHORT, 0 },
144 { "signed", RID_SIGNED, 0 },
145 { "sizeof", RID_SIZEOF, 0 },
146 { "static", RID_STATIC, 0 },
147 { "struct", RID_STRUCT, 0 },
148 { "switch", RID_SWITCH, 0 },
149 { "typedef", RID_TYPEDEF, 0 },
150 { "typeof", RID_TYPEOF, D_EXT },
151 { "union", RID_UNION, 0 },
152 { "unsigned", RID_UNSIGNED, 0 },
153 { "void", RID_VOID, 0 },
154 { "volatile", RID_VOLATILE, 0 },
155 { "while", RID_WHILE, 0 },
156 /* These Objective-C keywords are recognized only immediately after
157 an '@'. */
158 { "class", RID_AT_CLASS, D_OBJC },
159 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
160 { "defs", RID_AT_DEFS, D_OBJC },
161 { "encode", RID_AT_ENCODE, D_OBJC },
162 { "end", RID_AT_END, D_OBJC },
163 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
164 { "interface", RID_AT_INTERFACE, D_OBJC },
165 { "private", RID_AT_PRIVATE, D_OBJC },
166 { "protected", RID_AT_PROTECTED, D_OBJC },
167 { "protocol", RID_AT_PROTOCOL, D_OBJC },
168 { "public", RID_AT_PUBLIC, D_OBJC },
169 { "selector", RID_AT_SELECTOR, D_OBJC },
170 { "throw", RID_AT_THROW, D_OBJC },
171 { "try", RID_AT_TRY, D_OBJC },
172 { "catch", RID_AT_CATCH, D_OBJC },
173 { "finally", RID_AT_FINALLY, D_OBJC },
174 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
175 /* These are recognized only in protocol-qualifier context
176 (see above) */
177 { "bycopy", RID_BYCOPY, D_OBJC },
178 { "byref", RID_BYREF, D_OBJC },
179 { "in", RID_IN, D_OBJC },
180 { "inout", RID_INOUT, D_OBJC },
181 { "oneway", RID_ONEWAY, D_OBJC },
182 { "out", RID_OUT, D_OBJC },
184 #define N_reswords (sizeof reswords / sizeof (struct resword))
186 /* Initialization routine for this file. */
188 void
189 c_parse_init (void)
191 /* The only initialization required is of the reserved word
192 identifiers. */
193 unsigned int i;
194 tree id;
195 int mask = (flag_isoc99 ? 0 : D_C89)
196 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0);
198 if (!c_dialect_objc ())
199 mask |= D_OBJC;
201 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
202 for (i = 0; i < N_reswords; i++)
204 /* If a keyword is disabled, do not enter it into the table
205 and so create a canonical spelling that isn't a keyword. */
206 if (reswords[i].disable & mask)
207 continue;
209 id = get_identifier (reswords[i].word);
210 C_RID_CODE (id) = reswords[i].rid;
211 C_IS_RESERVED_WORD (id) = 1;
212 ridpointers [(int) reswords[i].rid] = id;
216 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
217 and the C parser. Unlike the C++ lexer, the parser structure
218 stores the lexer information instead of using a separate structure.
219 Identifiers are separated into ordinary identifiers, type names,
220 keywords and some other Objective-C types of identifiers, and some
221 look-ahead is maintained.
223 ??? It might be a good idea to lex the whole file up front (as for
224 C++). It would then be possible to share more of the C and C++
225 lexer code, if desired. */
227 /* The following local token type is used. */
229 /* A keyword. */
230 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
232 /* More information about the type of a CPP_NAME token. */
233 typedef enum c_id_kind {
234 /* An ordinary identifier. */
235 C_ID_ID,
236 /* An identifier declared as a typedef name. */
237 C_ID_TYPENAME,
238 /* An identifier declared as an Objective-C class name. */
239 C_ID_CLASSNAME,
240 /* Not an identifier. */
241 C_ID_NONE
242 } c_id_kind;
244 /* A single C token after string literal concatenation and conversion
245 of preprocessing tokens to tokens. */
246 typedef struct c_token GTY (())
248 /* The kind of token. */
249 ENUM_BITFIELD (cpp_ttype) type : 8;
250 /* If this token is a CPP_NAME, this value indicates whether also
251 declared as some kind of type. Otherwise, it is C_ID_NONE. */
252 ENUM_BITFIELD (c_id_kind) id_kind : 8;
253 /* If this token is a keyword, this value indicates which keyword.
254 Otherwise, this value is RID_MAX. */
255 ENUM_BITFIELD (rid) keyword : 8;
256 /* If this token is a CPP_PRAGMA, this indicates the pragma that
257 was seen. Otherwise it is PRAGMA_NONE. */
258 ENUM_BITFIELD (pragma_kind) pragma_kind : 7;
259 /* True if this token is from a system header. */
260 BOOL_BITFIELD in_system_header : 1;
261 /* The value associated with this token, if any. */
262 tree value;
263 /* The location at which this token was found. */
264 location_t location;
265 } c_token;
267 /* A parser structure recording information about the state and
268 context of parsing. Includes lexer information with up to two
269 tokens of look-ahead; more are not needed for C. */
270 typedef struct c_parser GTY(())
272 /* The look-ahead tokens. */
273 c_token tokens[2];
274 /* How many look-ahead tokens are available (0, 1 or 2). */
275 short tokens_avail;
276 /* True if a syntax error is being recovered from; false otherwise.
277 c_parser_error sets this flag. It should clear this flag when
278 enough tokens have been consumed to recover from the error. */
279 BOOL_BITFIELD error : 1;
280 /* True if we're processing a pragma, and shouldn't automatically
281 consume CPP_PRAGMA_EOL. */
282 BOOL_BITFIELD in_pragma : 1;
283 /* True if we want to lex an untranslated string. */
284 BOOL_BITFIELD lex_untranslated_string : 1;
285 /* Objective-C specific parser/lexer information. */
286 BOOL_BITFIELD objc_pq_context : 1;
287 /* The following flag is needed to contextualize Objective-C lexical
288 analysis. In some cases (e.g., 'int NSObject;'), it is
289 undesirable to bind an identifier to an Objective-C class, even
290 if a class with that name exists. */
291 BOOL_BITFIELD objc_need_raw_identifier : 1;
292 } c_parser;
295 /* The actual parser and external interface. ??? Does this need to be
296 garbage-collected? */
298 static GTY (()) c_parser *the_parser;
301 /* Read in and lex a single token, storing it in *TOKEN. */
303 static void
304 c_lex_one_token (c_parser *parser, c_token *token)
306 timevar_push (TV_LEX);
308 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
309 (parser->lex_untranslated_string
310 ? C_LEX_STRING_NO_TRANSLATE : 0));
311 token->id_kind = C_ID_NONE;
312 token->keyword = RID_MAX;
313 token->pragma_kind = PRAGMA_NONE;
314 token->in_system_header = in_system_header;
316 switch (token->type)
318 case CPP_NAME:
320 tree decl;
322 bool objc_force_identifier = parser->objc_need_raw_identifier;
323 if (c_dialect_objc ())
324 parser->objc_need_raw_identifier = false;
326 if (C_IS_RESERVED_WORD (token->value))
328 enum rid rid_code = C_RID_CODE (token->value);
330 if (c_dialect_objc ())
332 if (!OBJC_IS_AT_KEYWORD (rid_code)
333 && (!OBJC_IS_PQ_KEYWORD (rid_code)
334 || parser->objc_pq_context))
336 /* Return the canonical spelling for this keyword. */
337 token->value = ridpointers[(int) rid_code];
338 token->type = CPP_KEYWORD;
339 token->keyword = rid_code;
340 break;
343 else
345 /* Return the canonical spelling for this keyword. */
346 token->value = ridpointers[(int) rid_code];
347 token->type = CPP_KEYWORD;
348 token->keyword = rid_code;
349 break;
353 decl = lookup_name (token->value);
354 if (decl)
356 if (TREE_CODE (decl) == TYPE_DECL)
358 token->id_kind = C_ID_TYPENAME;
359 break;
362 else if (c_dialect_objc ())
364 tree objc_interface_decl = objc_is_class_name (token->value);
365 /* Objective-C class names are in the same namespace as
366 variables and typedefs, and hence are shadowed by local
367 declarations. */
368 if (objc_interface_decl
369 && (global_bindings_p ()
370 || (!objc_force_identifier && !decl)))
372 token->value = objc_interface_decl;
373 token->id_kind = C_ID_CLASSNAME;
374 break;
377 token->id_kind = C_ID_ID;
379 break;
380 case CPP_AT_NAME:
381 /* This only happens in Objective-C; it must be a keyword. */
382 token->type = CPP_KEYWORD;
383 token->keyword = C_RID_CODE (token->value);
384 break;
385 case CPP_COLON:
386 case CPP_COMMA:
387 case CPP_CLOSE_PAREN:
388 case CPP_SEMICOLON:
389 /* These tokens may affect the interpretation of any identifiers
390 following, if doing Objective-C. */
391 if (c_dialect_objc ())
392 parser->objc_need_raw_identifier = false;
393 break;
394 case CPP_PRAGMA:
395 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
396 token->pragma_kind = TREE_INT_CST_LOW (token->value);
397 token->value = NULL;
398 break;
399 default:
400 break;
402 timevar_pop (TV_LEX);
405 /* Return a pointer to the next token from PARSER, reading it in if
406 necessary. */
408 static inline c_token *
409 c_parser_peek_token (c_parser *parser)
411 if (parser->tokens_avail == 0)
413 c_lex_one_token (parser, &parser->tokens[0]);
414 parser->tokens_avail = 1;
416 return &parser->tokens[0];
419 /* Return true if the next token from PARSER has the indicated
420 TYPE. */
422 static inline bool
423 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
425 return c_parser_peek_token (parser)->type == type;
428 /* Return true if the next token from PARSER does not have the
429 indicated TYPE. */
431 static inline bool
432 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
434 return !c_parser_next_token_is (parser, type);
437 /* Return true if the next token from PARSER is the indicated
438 KEYWORD. */
440 static inline bool
441 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
443 c_token *token;
445 /* Peek at the next token. */
446 token = c_parser_peek_token (parser);
447 /* Check to see if it is the indicated keyword. */
448 return token->keyword == keyword;
451 /* Return true if TOKEN can start a type name,
452 false otherwise. */
453 static bool
454 c_token_starts_typename (c_token *token)
456 switch (token->type)
458 case CPP_NAME:
459 switch (token->id_kind)
461 case C_ID_ID:
462 return false;
463 case C_ID_TYPENAME:
464 return true;
465 case C_ID_CLASSNAME:
466 gcc_assert (c_dialect_objc ());
467 return true;
468 default:
469 gcc_unreachable ();
471 case CPP_KEYWORD:
472 switch (token->keyword)
474 case RID_UNSIGNED:
475 case RID_LONG:
476 case RID_SHORT:
477 case RID_SIGNED:
478 case RID_COMPLEX:
479 case RID_INT:
480 case RID_CHAR:
481 case RID_FLOAT:
482 case RID_DOUBLE:
483 case RID_VOID:
484 case RID_DFLOAT32:
485 case RID_DFLOAT64:
486 case RID_DFLOAT128:
487 case RID_BOOL:
488 case RID_ENUM:
489 case RID_STRUCT:
490 case RID_UNION:
491 case RID_TYPEOF:
492 case RID_CONST:
493 case RID_VOLATILE:
494 case RID_RESTRICT:
495 case RID_ATTRIBUTE:
496 case RID_FRACT:
497 case RID_ACCUM:
498 case RID_SAT:
499 return true;
500 default:
501 return false;
503 case CPP_LESS:
504 if (c_dialect_objc ())
505 return true;
506 return false;
507 default:
508 return false;
512 /* Return true if the next token from PARSER can start a type name,
513 false otherwise. */
514 static inline bool
515 c_parser_next_token_starts_typename (c_parser *parser)
517 c_token *token = c_parser_peek_token (parser);
518 return c_token_starts_typename (token);
521 /* Return true if TOKEN can start declaration specifiers, false
522 otherwise. */
523 static bool
524 c_token_starts_declspecs (c_token *token)
526 switch (token->type)
528 case CPP_NAME:
529 switch (token->id_kind)
531 case C_ID_ID:
532 return false;
533 case C_ID_TYPENAME:
534 return true;
535 case C_ID_CLASSNAME:
536 gcc_assert (c_dialect_objc ());
537 return true;
538 default:
539 gcc_unreachable ();
541 case CPP_KEYWORD:
542 switch (token->keyword)
544 case RID_STATIC:
545 case RID_EXTERN:
546 case RID_REGISTER:
547 case RID_TYPEDEF:
548 case RID_INLINE:
549 case RID_AUTO:
550 case RID_THREAD:
551 case RID_UNSIGNED:
552 case RID_LONG:
553 case RID_SHORT:
554 case RID_SIGNED:
555 case RID_COMPLEX:
556 case RID_INT:
557 case RID_CHAR:
558 case RID_FLOAT:
559 case RID_DOUBLE:
560 case RID_VOID:
561 case RID_DFLOAT32:
562 case RID_DFLOAT64:
563 case RID_DFLOAT128:
564 case RID_BOOL:
565 case RID_ENUM:
566 case RID_STRUCT:
567 case RID_UNION:
568 case RID_TYPEOF:
569 case RID_CONST:
570 case RID_VOLATILE:
571 case RID_RESTRICT:
572 case RID_ATTRIBUTE:
573 case RID_FRACT:
574 case RID_ACCUM:
575 case RID_SAT:
576 return true;
577 default:
578 return false;
580 case CPP_LESS:
581 if (c_dialect_objc ())
582 return true;
583 return false;
584 default:
585 return false;
589 /* Return true if the next token from PARSER can start declaration
590 specifiers, false otherwise. */
591 static inline bool
592 c_parser_next_token_starts_declspecs (c_parser *parser)
594 c_token *token = c_parser_peek_token (parser);
595 return c_token_starts_declspecs (token);
598 /* Return a pointer to the next-but-one token from PARSER, reading it
599 in if necessary. The next token is already read in. */
601 static c_token *
602 c_parser_peek_2nd_token (c_parser *parser)
604 if (parser->tokens_avail >= 2)
605 return &parser->tokens[1];
606 gcc_assert (parser->tokens_avail == 1);
607 gcc_assert (parser->tokens[0].type != CPP_EOF);
608 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
609 c_lex_one_token (parser, &parser->tokens[1]);
610 parser->tokens_avail = 2;
611 return &parser->tokens[1];
614 /* Consume the next token from PARSER. */
616 static void
617 c_parser_consume_token (c_parser *parser)
619 gcc_assert (parser->tokens_avail >= 1);
620 gcc_assert (parser->tokens[0].type != CPP_EOF);
621 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
622 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
623 if (parser->tokens_avail == 2)
624 parser->tokens[0] = parser->tokens[1];
625 parser->tokens_avail--;
628 /* Expect the current token to be a #pragma. Consume it and remember
629 that we've begun parsing a pragma. */
631 static void
632 c_parser_consume_pragma (c_parser *parser)
634 gcc_assert (!parser->in_pragma);
635 gcc_assert (parser->tokens_avail >= 1);
636 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
637 if (parser->tokens_avail == 2)
638 parser->tokens[0] = parser->tokens[1];
639 parser->tokens_avail--;
640 parser->in_pragma = true;
643 /* Update the globals input_location and in_system_header from
644 TOKEN. */
645 static inline void
646 c_parser_set_source_position_from_token (c_token *token)
648 if (token->type != CPP_EOF)
650 input_location = token->location;
651 in_system_header = token->in_system_header;
655 /* Issue a diagnostic of the form
656 FILE:LINE: MESSAGE before TOKEN
657 where TOKEN is the next token in the input stream of PARSER.
658 MESSAGE (specified by the caller) is usually of the form "expected
659 OTHER-TOKEN".
661 Do not issue a diagnostic if still recovering from an error.
663 ??? This is taken from the C++ parser, but building up messages in
664 this way is not i18n-friendly and some other approach should be
665 used. */
667 static void
668 c_parser_error (c_parser *parser, const char *gmsgid)
670 c_token *token = c_parser_peek_token (parser);
671 if (parser->error)
672 return;
673 parser->error = true;
674 if (!gmsgid)
675 return;
676 /* This diagnostic makes more sense if it is tagged to the line of
677 the token we just peeked at. */
678 c_parser_set_source_position_from_token (token);
679 c_parse_error (gmsgid,
680 /* Because c_parse_error does not understand
681 CPP_KEYWORD, keywords are treated like
682 identifiers. */
683 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
684 token->value);
687 /* If the next token is of the indicated TYPE, consume it. Otherwise,
688 issue the error MSGID. If MSGID is NULL then a message has already
689 been produced and no message will be produced this time. Returns
690 true if found, false otherwise. */
692 static bool
693 c_parser_require (c_parser *parser,
694 enum cpp_ttype type,
695 const char *msgid)
697 if (c_parser_next_token_is (parser, type))
699 c_parser_consume_token (parser);
700 return true;
702 else
704 c_parser_error (parser, msgid);
705 return false;
709 /* If the next token is the indicated keyword, consume it. Otherwise,
710 issue the error MSGID. Returns true if found, false otherwise. */
712 static bool
713 c_parser_require_keyword (c_parser *parser,
714 enum rid keyword,
715 const char *msgid)
717 if (c_parser_next_token_is_keyword (parser, keyword))
719 c_parser_consume_token (parser);
720 return true;
722 else
724 c_parser_error (parser, msgid);
725 return false;
729 /* Like c_parser_require, except that tokens will be skipped until the
730 desired token is found. An error message is still produced if the
731 next token is not as expected. If MSGID is NULL then a message has
732 already been produced and no message will be produced this
733 time. */
735 static void
736 c_parser_skip_until_found (c_parser *parser,
737 enum cpp_ttype type,
738 const char *msgid)
740 unsigned nesting_depth = 0;
742 if (c_parser_require (parser, type, msgid))
743 return;
745 /* Skip tokens until the desired token is found. */
746 while (true)
748 /* Peek at the next token. */
749 c_token *token = c_parser_peek_token (parser);
750 /* If we've reached the token we want, consume it and stop. */
751 if (token->type == type && !nesting_depth)
753 c_parser_consume_token (parser);
754 break;
757 /* If we've run out of tokens, stop. */
758 if (token->type == CPP_EOF)
759 return;
760 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
761 return;
762 if (token->type == CPP_OPEN_BRACE
763 || token->type == CPP_OPEN_PAREN
764 || token->type == CPP_OPEN_SQUARE)
765 ++nesting_depth;
766 else if (token->type == CPP_CLOSE_BRACE
767 || token->type == CPP_CLOSE_PAREN
768 || token->type == CPP_CLOSE_SQUARE)
770 if (nesting_depth-- == 0)
771 break;
773 /* Consume this token. */
774 c_parser_consume_token (parser);
776 parser->error = false;
779 /* Skip tokens until the end of a parameter is found, but do not
780 consume the comma, semicolon or closing delimiter. */
782 static void
783 c_parser_skip_to_end_of_parameter (c_parser *parser)
785 unsigned nesting_depth = 0;
787 while (true)
789 c_token *token = c_parser_peek_token (parser);
790 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
791 && !nesting_depth)
792 break;
793 /* If we've run out of tokens, stop. */
794 if (token->type == CPP_EOF)
795 return;
796 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
797 return;
798 if (token->type == CPP_OPEN_BRACE
799 || token->type == CPP_OPEN_PAREN
800 || token->type == CPP_OPEN_SQUARE)
801 ++nesting_depth;
802 else if (token->type == CPP_CLOSE_BRACE
803 || token->type == CPP_CLOSE_PAREN
804 || token->type == CPP_CLOSE_SQUARE)
806 if (nesting_depth-- == 0)
807 break;
809 /* Consume this token. */
810 c_parser_consume_token (parser);
812 parser->error = false;
815 /* Expect to be at the end of the pragma directive and consume an
816 end of line marker. */
818 static void
819 c_parser_skip_to_pragma_eol (c_parser *parser)
821 gcc_assert (parser->in_pragma);
822 parser->in_pragma = false;
824 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
825 while (true)
827 c_token *token = c_parser_peek_token (parser);
828 if (token->type == CPP_EOF)
829 break;
830 if (token->type == CPP_PRAGMA_EOL)
832 c_parser_consume_token (parser);
833 break;
835 c_parser_consume_token (parser);
838 parser->error = false;
841 /* Skip tokens until we have consumed an entire block, or until we
842 have consumed a non-nested ';'. */
844 static void
845 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
847 unsigned nesting_depth = 0;
848 bool save_error = parser->error;
850 while (true)
852 c_token *token;
854 /* Peek at the next token. */
855 token = c_parser_peek_token (parser);
857 switch (token->type)
859 case CPP_EOF:
860 return;
862 case CPP_PRAGMA_EOL:
863 if (parser->in_pragma)
864 return;
865 break;
867 case CPP_SEMICOLON:
868 /* If the next token is a ';', we have reached the
869 end of the statement. */
870 if (!nesting_depth)
872 /* Consume the ';'. */
873 c_parser_consume_token (parser);
874 goto finished;
876 break;
878 case CPP_CLOSE_BRACE:
879 /* If the next token is a non-nested '}', then we have
880 reached the end of the current block. */
881 if (nesting_depth == 0 || --nesting_depth == 0)
883 c_parser_consume_token (parser);
884 goto finished;
886 break;
888 case CPP_OPEN_BRACE:
889 /* If it the next token is a '{', then we are entering a new
890 block. Consume the entire block. */
891 ++nesting_depth;
892 break;
894 case CPP_PRAGMA:
895 /* If we see a pragma, consume the whole thing at once. We
896 have some safeguards against consuming pragmas willy-nilly.
897 Normally, we'd expect to be here with parser->error set,
898 which disables these safeguards. But it's possible to get
899 here for secondary error recovery, after parser->error has
900 been cleared. */
901 c_parser_consume_pragma (parser);
902 c_parser_skip_to_pragma_eol (parser);
903 parser->error = save_error;
904 continue;
906 default:
907 break;
910 c_parser_consume_token (parser);
913 finished:
914 parser->error = false;
917 /* Save the warning flags which are controlled by __extension__. */
919 static inline int
920 disable_extension_diagnostics (void)
922 int ret = (pedantic
923 | (warn_pointer_arith << 1)
924 | (warn_traditional << 2)
925 | (flag_iso << 3));
926 pedantic = 0;
927 warn_pointer_arith = 0;
928 warn_traditional = 0;
929 flag_iso = 0;
930 return ret;
933 /* Restore the warning flags which are controlled by __extension__.
934 FLAGS is the return value from disable_extension_diagnostics. */
936 static inline void
937 restore_extension_diagnostics (int flags)
939 pedantic = flags & 1;
940 warn_pointer_arith = (flags >> 1) & 1;
941 warn_traditional = (flags >> 2) & 1;
942 flag_iso = (flags >> 3) & 1;
945 /* Possibly kinds of declarator to parse. */
946 typedef enum c_dtr_syn {
947 /* A normal declarator with an identifier. */
948 C_DTR_NORMAL,
949 /* An abstract declarator (maybe empty). */
950 C_DTR_ABSTRACT,
951 /* A parameter declarator: may be either, but after a type name does
952 not redeclare a typedef name as an identifier if it can
953 alternatively be interpreted as a typedef name; see DR#009,
954 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
955 following DR#249. For example, given a typedef T, "int T" and
956 "int *T" are valid parameter declarations redeclaring T, while
957 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
958 abstract declarators rather than involving redundant parentheses;
959 the same applies with attributes inside the parentheses before
960 "T". */
961 C_DTR_PARM
962 } c_dtr_syn;
964 static void c_parser_external_declaration (c_parser *);
965 static void c_parser_asm_definition (c_parser *);
966 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool);
967 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
968 bool);
969 static struct c_typespec c_parser_enum_specifier (c_parser *);
970 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
971 static tree c_parser_struct_declaration (c_parser *);
972 static struct c_typespec c_parser_typeof_specifier (c_parser *);
973 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
974 bool *);
975 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
976 c_dtr_syn, bool *);
977 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
978 bool,
979 struct c_declarator *);
980 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
981 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree);
982 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
983 static tree c_parser_simple_asm_expr (c_parser *);
984 static tree c_parser_attributes (c_parser *);
985 static struct c_type_name *c_parser_type_name (c_parser *);
986 static struct c_expr c_parser_initializer (c_parser *);
987 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
988 static void c_parser_initelt (c_parser *);
989 static void c_parser_initval (c_parser *, struct c_expr *);
990 static tree c_parser_compound_statement (c_parser *);
991 static void c_parser_compound_statement_nostart (c_parser *);
992 static void c_parser_label (c_parser *);
993 static void c_parser_statement (c_parser *);
994 static void c_parser_statement_after_labels (c_parser *);
995 static void c_parser_if_statement (c_parser *);
996 static void c_parser_switch_statement (c_parser *);
997 static void c_parser_while_statement (c_parser *);
998 static void c_parser_do_statement (c_parser *);
999 static void c_parser_for_statement (c_parser *);
1000 static tree c_parser_asm_statement (c_parser *);
1001 static tree c_parser_asm_operands (c_parser *, bool);
1002 static tree c_parser_asm_clobbers (c_parser *);
1003 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1004 static struct c_expr c_parser_conditional_expression (c_parser *,
1005 struct c_expr *);
1006 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *);
1007 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1008 static struct c_expr c_parser_unary_expression (c_parser *);
1009 static struct c_expr c_parser_sizeof_expression (c_parser *);
1010 static struct c_expr c_parser_alignof_expression (c_parser *);
1011 static struct c_expr c_parser_postfix_expression (c_parser *);
1012 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1013 struct c_type_name *);
1014 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1015 struct c_expr);
1016 static struct c_expr c_parser_expression (c_parser *);
1017 static struct c_expr c_parser_expression_conv (c_parser *);
1018 static tree c_parser_expr_list (c_parser *, bool);
1019 static void c_parser_omp_construct (c_parser *);
1020 static void c_parser_omp_threadprivate (c_parser *);
1021 static void c_parser_omp_barrier (c_parser *);
1022 static void c_parser_omp_flush (c_parser *);
1024 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1025 static bool c_parser_pragma (c_parser *, enum pragma_context);
1027 /* These Objective-C parser functions are only ever called when
1028 compiling Objective-C. */
1029 static void c_parser_objc_class_definition (c_parser *);
1030 static void c_parser_objc_class_instance_variables (c_parser *);
1031 static void c_parser_objc_class_declaration (c_parser *);
1032 static void c_parser_objc_alias_declaration (c_parser *);
1033 static void c_parser_objc_protocol_definition (c_parser *);
1034 static enum tree_code c_parser_objc_method_type (c_parser *);
1035 static void c_parser_objc_method_definition (c_parser *);
1036 static void c_parser_objc_methodprotolist (c_parser *);
1037 static void c_parser_objc_methodproto (c_parser *);
1038 static tree c_parser_objc_method_decl (c_parser *);
1039 static tree c_parser_objc_type_name (c_parser *);
1040 static tree c_parser_objc_protocol_refs (c_parser *);
1041 static void c_parser_objc_try_catch_statement (c_parser *);
1042 static void c_parser_objc_synchronized_statement (c_parser *);
1043 static tree c_parser_objc_selector (c_parser *);
1044 static tree c_parser_objc_selector_arg (c_parser *);
1045 static tree c_parser_objc_receiver (c_parser *);
1046 static tree c_parser_objc_message_args (c_parser *);
1047 static tree c_parser_objc_keywordexpr (c_parser *);
1049 /* Parse a translation unit (C90 6.7, C99 6.9).
1051 translation-unit:
1052 external-declarations
1054 external-declarations:
1055 external-declaration
1056 external-declarations external-declaration
1058 GNU extensions:
1060 translation-unit:
1061 empty
1064 static void
1065 c_parser_translation_unit (c_parser *parser)
1067 if (c_parser_next_token_is (parser, CPP_EOF))
1069 if (pedantic)
1070 pedwarn ("ISO C forbids an empty source file");
1072 else
1074 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1077 ggc_collect ();
1078 c_parser_external_declaration (parser);
1079 obstack_free (&parser_obstack, obstack_position);
1081 while (c_parser_next_token_is_not (parser, CPP_EOF));
1085 /* Parse an external declaration (C90 6.7, C99 6.9).
1087 external-declaration:
1088 function-definition
1089 declaration
1091 GNU extensions:
1093 external-declaration:
1094 asm-definition
1096 __extension__ external-declaration
1098 Objective-C:
1100 external-declaration:
1101 objc-class-definition
1102 objc-class-declaration
1103 objc-alias-declaration
1104 objc-protocol-definition
1105 objc-method-definition
1106 @end
1109 static void
1110 c_parser_external_declaration (c_parser *parser)
1112 int ext;
1113 switch (c_parser_peek_token (parser)->type)
1115 case CPP_KEYWORD:
1116 switch (c_parser_peek_token (parser)->keyword)
1118 case RID_EXTENSION:
1119 ext = disable_extension_diagnostics ();
1120 c_parser_consume_token (parser);
1121 c_parser_external_declaration (parser);
1122 restore_extension_diagnostics (ext);
1123 break;
1124 case RID_ASM:
1125 c_parser_asm_definition (parser);
1126 break;
1127 case RID_AT_INTERFACE:
1128 case RID_AT_IMPLEMENTATION:
1129 gcc_assert (c_dialect_objc ());
1130 c_parser_objc_class_definition (parser);
1131 break;
1132 case RID_AT_CLASS:
1133 gcc_assert (c_dialect_objc ());
1134 c_parser_objc_class_declaration (parser);
1135 break;
1136 case RID_AT_ALIAS:
1137 gcc_assert (c_dialect_objc ());
1138 c_parser_objc_alias_declaration (parser);
1139 break;
1140 case RID_AT_PROTOCOL:
1141 gcc_assert (c_dialect_objc ());
1142 c_parser_objc_protocol_definition (parser);
1143 break;
1144 case RID_AT_END:
1145 gcc_assert (c_dialect_objc ());
1146 c_parser_consume_token (parser);
1147 objc_finish_implementation ();
1148 break;
1149 default:
1150 goto decl_or_fndef;
1152 break;
1153 case CPP_SEMICOLON:
1154 if (pedantic)
1155 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
1156 c_parser_consume_token (parser);
1157 break;
1158 case CPP_PRAGMA:
1159 c_parser_pragma (parser, pragma_external);
1160 break;
1161 case CPP_PLUS:
1162 case CPP_MINUS:
1163 if (c_dialect_objc ())
1165 c_parser_objc_method_definition (parser);
1166 break;
1168 /* Else fall through, and yield a syntax error trying to parse
1169 as a declaration or function definition. */
1170 default:
1171 decl_or_fndef:
1172 /* A declaration or a function definition. We can only tell
1173 which after parsing the declaration specifiers, if any, and
1174 the first declarator. */
1175 c_parser_declaration_or_fndef (parser, true, true, false, true);
1176 break;
1181 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1182 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1183 accepted; otherwise (old-style parameter declarations) only other
1184 declarations are accepted. If NESTED is true, we are inside a
1185 function or parsing old-style parameter declarations; any functions
1186 encountered are nested functions and declaration specifiers are
1187 required; otherwise we are at top level and functions are normal
1188 functions and declaration specifiers may be optional. If EMPTY_OK
1189 is true, empty declarations are OK (subject to all other
1190 constraints); otherwise (old-style parameter declarations) they are
1191 diagnosed. If START_ATTR_OK is true, the declaration specifiers
1192 may start with attributes; otherwise they may not.
1194 declaration:
1195 declaration-specifiers init-declarator-list[opt] ;
1197 function-definition:
1198 declaration-specifiers[opt] declarator declaration-list[opt]
1199 compound-statement
1201 declaration-list:
1202 declaration
1203 declaration-list declaration
1205 init-declarator-list:
1206 init-declarator
1207 init-declarator-list , init-declarator
1209 init-declarator:
1210 declarator simple-asm-expr[opt] attributes[opt]
1211 declarator simple-asm-expr[opt] attributes[opt] = initializer
1213 GNU extensions:
1215 nested-function-definition:
1216 declaration-specifiers declarator declaration-list[opt]
1217 compound-statement
1219 The simple-asm-expr and attributes are GNU extensions.
1221 This function does not handle __extension__; that is handled in its
1222 callers. ??? Following the old parser, __extension__ may start
1223 external declarations, declarations in functions and declarations
1224 at the start of "for" loops, but not old-style parameter
1225 declarations.
1227 C99 requires declaration specifiers in a function definition; the
1228 absence is diagnosed through the diagnosis of implicit int. In GNU
1229 C we also allow but diagnose declarations without declaration
1230 specifiers, but only at top level (elsewhere they conflict with
1231 other syntax).
1233 OpenMP:
1235 declaration:
1236 threadprivate-directive */
1238 static void
1239 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok,
1240 bool nested, bool start_attr_ok)
1242 struct c_declspecs *specs;
1243 tree prefix_attrs;
1244 tree all_prefix_attrs;
1245 bool diagnosed_no_specs = false;
1247 specs = build_null_declspecs ();
1248 c_parser_declspecs (parser, specs, true, true, start_attr_ok);
1249 if (parser->error)
1251 c_parser_skip_to_end_of_block_or_statement (parser);
1252 return;
1254 if (nested && !specs->declspecs_seen_p)
1256 c_parser_error (parser, "expected declaration specifiers");
1257 c_parser_skip_to_end_of_block_or_statement (parser);
1258 return;
1260 finish_declspecs (specs);
1261 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1263 if (empty_ok)
1264 shadow_tag (specs);
1265 else
1267 shadow_tag_warned (specs, 1);
1268 pedwarn ("empty declaration");
1270 c_parser_consume_token (parser);
1271 return;
1273 pending_xref_error ();
1274 prefix_attrs = specs->attrs;
1275 all_prefix_attrs = prefix_attrs;
1276 specs->attrs = NULL_TREE;
1277 while (true)
1279 struct c_declarator *declarator;
1280 bool dummy = false;
1281 tree fnbody;
1282 /* Declaring either one or more declarators (in which case we
1283 should diagnose if there were no declaration specifiers) or a
1284 function definition (in which case the diagnostic for
1285 implicit int suffices). */
1286 declarator = c_parser_declarator (parser, specs->type_seen_p,
1287 C_DTR_NORMAL, &dummy);
1288 if (declarator == NULL)
1290 c_parser_skip_to_end_of_block_or_statement (parser);
1291 return;
1293 if (c_parser_next_token_is (parser, CPP_EQ)
1294 || c_parser_next_token_is (parser, CPP_COMMA)
1295 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1296 || c_parser_next_token_is_keyword (parser, RID_ASM)
1297 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1299 tree asm_name = NULL_TREE;
1300 tree postfix_attrs = NULL_TREE;
1301 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1303 diagnosed_no_specs = true;
1304 pedwarn ("data definition has no type or storage class");
1306 /* Having seen a data definition, there cannot now be a
1307 function definition. */
1308 fndef_ok = false;
1309 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1310 asm_name = c_parser_simple_asm_expr (parser);
1311 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1312 postfix_attrs = c_parser_attributes (parser);
1313 if (c_parser_next_token_is (parser, CPP_EQ))
1315 tree d;
1316 struct c_expr init;
1317 c_parser_consume_token (parser);
1318 /* The declaration of the variable is in effect while
1319 its initializer is parsed. */
1320 d = start_decl (declarator, specs, true,
1321 chainon (postfix_attrs, all_prefix_attrs));
1322 if (!d)
1323 d = error_mark_node;
1324 start_init (d, asm_name, global_bindings_p ());
1325 init = c_parser_initializer (parser);
1326 finish_init ();
1327 if (d != error_mark_node)
1329 maybe_warn_string_init (TREE_TYPE (d), init);
1330 finish_decl (d, init.value, asm_name);
1333 else
1335 tree d = start_decl (declarator, specs, false,
1336 chainon (postfix_attrs,
1337 all_prefix_attrs));
1338 if (d)
1339 finish_decl (d, NULL_TREE, asm_name);
1341 if (c_parser_next_token_is (parser, CPP_COMMA))
1343 c_parser_consume_token (parser);
1344 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1345 all_prefix_attrs = chainon (c_parser_attributes (parser),
1346 prefix_attrs);
1347 else
1348 all_prefix_attrs = prefix_attrs;
1349 continue;
1351 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1353 c_parser_consume_token (parser);
1354 return;
1356 else
1358 c_parser_error (parser, "expected %<,%> or %<;%>");
1359 c_parser_skip_to_end_of_block_or_statement (parser);
1360 return;
1363 else if (!fndef_ok)
1365 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1366 "%<asm%> or %<__attribute__%>");
1367 c_parser_skip_to_end_of_block_or_statement (parser);
1368 return;
1370 /* Function definition (nested or otherwise). */
1371 if (nested)
1373 if (pedantic)
1374 pedwarn ("ISO C forbids nested functions");
1375 push_function_context ();
1377 if (!start_function (specs, declarator, all_prefix_attrs))
1379 /* This can appear in many cases looking nothing like a
1380 function definition, so we don't give a more specific
1381 error suggesting there was one. */
1382 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1383 "or %<__attribute__%>");
1384 if (nested)
1385 pop_function_context ();
1386 break;
1388 /* Parse old-style parameter declarations. ??? Attributes are
1389 not allowed to start declaration specifiers here because of a
1390 syntax conflict between a function declaration with attribute
1391 suffix and a function definition with an attribute prefix on
1392 first old-style parameter declaration. Following the old
1393 parser, they are not accepted on subsequent old-style
1394 parameter declarations either. However, there is no
1395 ambiguity after the first declaration, nor indeed on the
1396 first as long as we don't allow postfix attributes after a
1397 declarator with a nonempty identifier list in a definition;
1398 and postfix attributes have never been accepted here in
1399 function definitions either. */
1400 while (c_parser_next_token_is_not (parser, CPP_EOF)
1401 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1402 c_parser_declaration_or_fndef (parser, false, false, true, false);
1403 DECL_SOURCE_LOCATION (current_function_decl)
1404 = c_parser_peek_token (parser)->location;
1405 store_parm_decls ();
1406 fnbody = c_parser_compound_statement (parser);
1407 if (nested)
1409 tree decl = current_function_decl;
1410 add_stmt (fnbody);
1411 finish_function ();
1412 pop_function_context ();
1413 add_stmt (build_stmt (DECL_EXPR, decl));
1415 else
1417 add_stmt (fnbody);
1418 finish_function ();
1420 break;
1424 /* Parse an asm-definition (asm() outside a function body). This is a
1425 GNU extension.
1427 asm-definition:
1428 simple-asm-expr ;
1431 static void
1432 c_parser_asm_definition (c_parser *parser)
1434 tree asm_str = c_parser_simple_asm_expr (parser);
1435 if (asm_str)
1436 cgraph_add_asm_node (asm_str);
1437 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1440 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1441 6.7), adding them to SPECS (which may already include some).
1442 Storage class specifiers are accepted iff SCSPEC_OK; type
1443 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1444 the start iff START_ATTR_OK.
1446 declaration-specifiers:
1447 storage-class-specifier declaration-specifiers[opt]
1448 type-specifier declaration-specifiers[opt]
1449 type-qualifier declaration-specifiers[opt]
1450 function-specifier declaration-specifiers[opt]
1452 Function specifiers (inline) are from C99, and are currently
1453 handled as storage class specifiers, as is __thread.
1455 C90 6.5.1, C99 6.7.1:
1456 storage-class-specifier:
1457 typedef
1458 extern
1459 static
1460 auto
1461 register
1463 C99 6.7.4:
1464 function-specifier:
1465 inline
1467 C90 6.5.2, C99 6.7.2:
1468 type-specifier:
1469 void
1470 char
1471 short
1473 long
1474 float
1475 double
1476 signed
1477 unsigned
1478 _Bool
1479 _Complex
1480 [_Imaginary removed in C99 TC2]
1481 struct-or-union-specifier
1482 enum-specifier
1483 typedef-name
1485 (_Bool and _Complex are new in C99.)
1487 C90 6.5.3, C99 6.7.3:
1489 type-qualifier:
1490 const
1491 restrict
1492 volatile
1494 (restrict is new in C99.)
1496 GNU extensions:
1498 declaration-specifiers:
1499 attributes declaration-specifiers[opt]
1501 storage-class-specifier:
1502 __thread
1504 type-specifier:
1505 typeof-specifier
1506 _Decimal32
1507 _Decimal64
1508 _Decimal128
1509 _Fract
1510 _Accum
1511 _Sat
1513 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1514 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1516 Objective-C:
1518 type-specifier:
1519 class-name objc-protocol-refs[opt]
1520 typedef-name objc-protocol-refs
1521 objc-protocol-refs
1524 static void
1525 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1526 bool scspec_ok, bool typespec_ok, bool start_attr_ok)
1528 bool attrs_ok = start_attr_ok;
1529 bool seen_type = specs->type_seen_p;
1530 while (c_parser_next_token_is (parser, CPP_NAME)
1531 || c_parser_next_token_is (parser, CPP_KEYWORD)
1532 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1534 struct c_typespec t;
1535 tree attrs;
1536 if (c_parser_next_token_is (parser, CPP_NAME))
1538 tree value = c_parser_peek_token (parser)->value;
1539 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
1540 /* This finishes the specifiers unless a type name is OK, it
1541 is declared as a type name and a type name hasn't yet
1542 been seen. */
1543 if (!typespec_ok || seen_type
1544 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME))
1545 break;
1546 c_parser_consume_token (parser);
1547 seen_type = true;
1548 attrs_ok = true;
1549 if (kind == C_ID_TYPENAME
1550 && (!c_dialect_objc ()
1551 || c_parser_next_token_is_not (parser, CPP_LESS)))
1553 t.kind = ctsk_typedef;
1554 /* For a typedef name, record the meaning, not the name.
1555 In case of 'foo foo, bar;'. */
1556 t.spec = lookup_name (value);
1558 else
1560 tree proto = NULL_TREE;
1561 gcc_assert (c_dialect_objc ());
1562 t.kind = ctsk_objc;
1563 if (c_parser_next_token_is (parser, CPP_LESS))
1564 proto = c_parser_objc_protocol_refs (parser);
1565 t.spec = objc_get_protocol_qualified_type (value, proto);
1567 declspecs_add_type (specs, t);
1568 continue;
1570 if (c_parser_next_token_is (parser, CPP_LESS))
1572 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
1573 nisse@lysator.liu.se. */
1574 tree proto;
1575 gcc_assert (c_dialect_objc ());
1576 if (!typespec_ok || seen_type)
1577 break;
1578 proto = c_parser_objc_protocol_refs (parser);
1579 t.kind = ctsk_objc;
1580 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
1581 declspecs_add_type (specs, t);
1582 continue;
1584 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
1585 switch (c_parser_peek_token (parser)->keyword)
1587 case RID_STATIC:
1588 case RID_EXTERN:
1589 case RID_REGISTER:
1590 case RID_TYPEDEF:
1591 case RID_INLINE:
1592 case RID_AUTO:
1593 case RID_THREAD:
1594 if (!scspec_ok)
1595 goto out;
1596 attrs_ok = true;
1597 /* TODO: Distinguish between function specifiers (inline)
1598 and storage class specifiers, either here or in
1599 declspecs_add_scspec. */
1600 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
1601 c_parser_consume_token (parser);
1602 break;
1603 case RID_UNSIGNED:
1604 case RID_LONG:
1605 case RID_SHORT:
1606 case RID_SIGNED:
1607 case RID_COMPLEX:
1608 case RID_INT:
1609 case RID_CHAR:
1610 case RID_FLOAT:
1611 case RID_DOUBLE:
1612 case RID_VOID:
1613 case RID_DFLOAT32:
1614 case RID_DFLOAT64:
1615 case RID_DFLOAT128:
1616 case RID_BOOL:
1617 case RID_FRACT:
1618 case RID_ACCUM:
1619 case RID_SAT:
1620 if (!typespec_ok)
1621 goto out;
1622 attrs_ok = true;
1623 seen_type = true;
1624 if (c_dialect_objc ())
1625 parser->objc_need_raw_identifier = true;
1626 t.kind = ctsk_resword;
1627 t.spec = c_parser_peek_token (parser)->value;
1628 declspecs_add_type (specs, t);
1629 c_parser_consume_token (parser);
1630 break;
1631 case RID_ENUM:
1632 if (!typespec_ok)
1633 goto out;
1634 attrs_ok = true;
1635 seen_type = true;
1636 t = c_parser_enum_specifier (parser);
1637 declspecs_add_type (specs, t);
1638 break;
1639 case RID_STRUCT:
1640 case RID_UNION:
1641 if (!typespec_ok)
1642 goto out;
1643 attrs_ok = true;
1644 seen_type = true;
1645 t = c_parser_struct_or_union_specifier (parser);
1646 declspecs_add_type (specs, t);
1647 break;
1648 case RID_TYPEOF:
1649 /* ??? The old parser rejected typeof after other type
1650 specifiers, but is a syntax error the best way of
1651 handling this? */
1652 if (!typespec_ok || seen_type)
1653 goto out;
1654 attrs_ok = true;
1655 seen_type = true;
1656 t = c_parser_typeof_specifier (parser);
1657 declspecs_add_type (specs, t);
1658 break;
1659 case RID_CONST:
1660 case RID_VOLATILE:
1661 case RID_RESTRICT:
1662 attrs_ok = true;
1663 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
1664 c_parser_consume_token (parser);
1665 break;
1666 case RID_ATTRIBUTE:
1667 if (!attrs_ok)
1668 goto out;
1669 attrs = c_parser_attributes (parser);
1670 declspecs_add_attrs (specs, attrs);
1671 break;
1672 default:
1673 goto out;
1676 out: ;
1679 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
1681 enum-specifier:
1682 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
1683 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
1684 enum attributes[opt] identifier
1686 The form with trailing comma is new in C99. The forms with
1687 attributes are GNU extensions. In GNU C, we accept any expression
1688 without commas in the syntax (assignment expressions, not just
1689 conditional expressions); assignment expressions will be diagnosed
1690 as non-constant.
1692 enumerator-list:
1693 enumerator
1694 enumerator-list , enumerator
1696 enumerator:
1697 enumeration-constant
1698 enumeration-constant = constant-expression
1701 static struct c_typespec
1702 c_parser_enum_specifier (c_parser *parser)
1704 struct c_typespec ret;
1705 tree attrs;
1706 tree ident = NULL_TREE;
1707 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
1708 c_parser_consume_token (parser);
1709 attrs = c_parser_attributes (parser);
1710 if (c_parser_next_token_is (parser, CPP_NAME))
1712 ident = c_parser_peek_token (parser)->value;
1713 c_parser_consume_token (parser);
1715 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1717 /* Parse an enum definition. */
1718 struct c_enum_contents the_enum;
1719 tree type = start_enum (&the_enum, ident);
1720 tree postfix_attrs;
1721 /* We chain the enumerators in reverse order, then put them in
1722 forward order at the end. */
1723 tree values = NULL_TREE;
1724 c_parser_consume_token (parser);
1725 while (true)
1727 tree enum_id;
1728 tree enum_value;
1729 tree enum_decl;
1730 bool seen_comma;
1731 if (c_parser_next_token_is_not (parser, CPP_NAME))
1733 c_parser_error (parser, "expected identifier");
1734 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1735 values = error_mark_node;
1736 break;
1738 enum_id = c_parser_peek_token (parser)->value;
1739 c_parser_consume_token (parser);
1740 if (c_parser_next_token_is (parser, CPP_EQ))
1742 c_parser_consume_token (parser);
1743 enum_value = c_parser_expr_no_commas (parser, NULL).value;
1745 else
1746 enum_value = NULL_TREE;
1747 enum_decl = build_enumerator (&the_enum, enum_id, enum_value);
1748 TREE_CHAIN (enum_decl) = values;
1749 values = enum_decl;
1750 seen_comma = false;
1751 if (c_parser_next_token_is (parser, CPP_COMMA))
1753 seen_comma = true;
1754 c_parser_consume_token (parser);
1756 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1758 if (seen_comma && pedantic && !flag_isoc99)
1759 pedwarn ("comma at end of enumerator list");
1760 c_parser_consume_token (parser);
1761 break;
1763 if (!seen_comma)
1765 c_parser_error (parser, "expected %<,%> or %<}%>");
1766 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1767 values = error_mark_node;
1768 break;
1771 postfix_attrs = c_parser_attributes (parser);
1772 ret.spec = finish_enum (type, nreverse (values),
1773 chainon (attrs, postfix_attrs));
1774 ret.kind = ctsk_tagdef;
1775 return ret;
1777 else if (!ident)
1779 c_parser_error (parser, "expected %<{%>");
1780 ret.spec = error_mark_node;
1781 ret.kind = ctsk_tagref;
1782 return ret;
1784 ret = parser_xref_tag (ENUMERAL_TYPE, ident);
1785 /* In ISO C, enumerated types can be referred to only if already
1786 defined. */
1787 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
1788 pedwarn ("ISO C forbids forward references to %<enum%> types");
1789 return ret;
1792 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
1794 struct-or-union-specifier:
1795 struct-or-union attributes[opt] identifier[opt]
1796 { struct-contents } attributes[opt]
1797 struct-or-union attributes[opt] identifier
1799 struct-contents:
1800 struct-declaration-list
1802 struct-declaration-list:
1803 struct-declaration ;
1804 struct-declaration-list struct-declaration ;
1806 GNU extensions:
1808 struct-contents:
1809 empty
1810 struct-declaration
1811 struct-declaration-list struct-declaration
1813 struct-declaration-list:
1814 struct-declaration-list ;
1817 (Note that in the syntax here, unlike that in ISO C, the semicolons
1818 are included here rather than in struct-declaration, in order to
1819 describe the syntax with extra semicolons and missing semicolon at
1820 end.)
1822 Objective-C:
1824 struct-declaration-list:
1825 @defs ( class-name )
1827 (Note this does not include a trailing semicolon, but can be
1828 followed by further declarations, and gets a pedwarn-if-pedantic
1829 when followed by a semicolon.) */
1831 static struct c_typespec
1832 c_parser_struct_or_union_specifier (c_parser *parser)
1834 struct c_typespec ret;
1835 tree attrs;
1836 tree ident = NULL_TREE;
1837 enum tree_code code;
1838 switch (c_parser_peek_token (parser)->keyword)
1840 case RID_STRUCT:
1841 code = RECORD_TYPE;
1842 break;
1843 case RID_UNION:
1844 code = UNION_TYPE;
1845 break;
1846 default:
1847 gcc_unreachable ();
1849 c_parser_consume_token (parser);
1850 attrs = c_parser_attributes (parser);
1851 if (c_parser_next_token_is (parser, CPP_NAME))
1853 ident = c_parser_peek_token (parser)->value;
1854 c_parser_consume_token (parser);
1856 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1858 /* Parse a struct or union definition. Start the scope of the
1859 tag before parsing components. */
1860 tree type = start_struct (code, ident);
1861 tree postfix_attrs;
1862 /* We chain the components in reverse order, then put them in
1863 forward order at the end. Each struct-declaration may
1864 declare multiple components (comma-separated), so we must use
1865 chainon to join them, although when parsing each
1866 struct-declaration we can use TREE_CHAIN directly.
1868 The theory behind all this is that there will be more
1869 semicolon separated fields than comma separated fields, and
1870 so we'll be minimizing the number of node traversals required
1871 by chainon. */
1872 tree contents = NULL_TREE;
1873 c_parser_consume_token (parser);
1874 /* Handle the Objective-C @defs construct,
1875 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
1876 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
1878 tree name;
1879 gcc_assert (c_dialect_objc ());
1880 c_parser_consume_token (parser);
1881 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1882 goto end_at_defs;
1883 if (c_parser_next_token_is (parser, CPP_NAME)
1884 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
1886 name = c_parser_peek_token (parser)->value;
1887 c_parser_consume_token (parser);
1889 else
1891 c_parser_error (parser, "expected class name");
1892 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
1893 goto end_at_defs;
1895 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
1896 "expected %<)%>");
1897 contents = nreverse (objc_get_class_ivars (name));
1899 end_at_defs:
1900 /* Parse the struct-declarations and semicolons. Problems with
1901 semicolons are diagnosed here; empty structures are diagnosed
1902 elsewhere. */
1903 while (true)
1905 tree decls;
1906 /* Parse any stray semicolon. */
1907 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1909 if (pedantic)
1910 pedwarn ("extra semicolon in struct or union specified");
1911 c_parser_consume_token (parser);
1912 continue;
1914 /* Stop if at the end of the struct or union contents. */
1915 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1917 c_parser_consume_token (parser);
1918 break;
1920 /* Accept #pragmas at struct scope. */
1921 if (c_parser_next_token_is (parser, CPP_PRAGMA))
1923 c_parser_pragma (parser, pragma_external);
1924 continue;
1926 /* Parse some comma-separated declarations, but not the
1927 trailing semicolon if any. */
1928 decls = c_parser_struct_declaration (parser);
1929 contents = chainon (decls, contents);
1930 /* If no semicolon follows, either we have a parse error or
1931 are at the end of the struct or union and should
1932 pedwarn. */
1933 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1934 c_parser_consume_token (parser);
1935 else
1937 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
1938 pedwarn ("no semicolon at end of struct or union");
1939 else
1941 c_parser_error (parser, "expected %<;%>");
1942 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
1943 break;
1947 postfix_attrs = c_parser_attributes (parser);
1948 ret.spec = finish_struct (type, nreverse (contents),
1949 chainon (attrs, postfix_attrs));
1950 ret.kind = ctsk_tagdef;
1951 return ret;
1953 else if (!ident)
1955 c_parser_error (parser, "expected %<{%>");
1956 ret.spec = error_mark_node;
1957 ret.kind = ctsk_tagref;
1958 return ret;
1960 ret = parser_xref_tag (code, ident);
1961 return ret;
1964 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
1965 the trailing semicolon.
1967 struct-declaration:
1968 specifier-qualifier-list struct-declarator-list
1970 specifier-qualifier-list:
1971 type-specifier specifier-qualifier-list[opt]
1972 type-qualifier specifier-qualifier-list[opt]
1973 attributes specifier-qualifier-list[opt]
1975 struct-declarator-list:
1976 struct-declarator
1977 struct-declarator-list , attributes[opt] struct-declarator
1979 struct-declarator:
1980 declarator attributes[opt]
1981 declarator[opt] : constant-expression attributes[opt]
1983 GNU extensions:
1985 struct-declaration:
1986 __extension__ struct-declaration
1987 specifier-qualifier-list
1989 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
1990 of attributes where shown is a GNU extension. In GNU C, we accept
1991 any expression without commas in the syntax (assignment
1992 expressions, not just conditional expressions); assignment
1993 expressions will be diagnosed as non-constant. */
1995 static tree
1996 c_parser_struct_declaration (c_parser *parser)
1998 struct c_declspecs *specs;
1999 tree prefix_attrs;
2000 tree all_prefix_attrs;
2001 tree decls;
2002 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2004 int ext;
2005 tree decl;
2006 ext = disable_extension_diagnostics ();
2007 c_parser_consume_token (parser);
2008 decl = c_parser_struct_declaration (parser);
2009 restore_extension_diagnostics (ext);
2010 return decl;
2012 specs = build_null_declspecs ();
2013 c_parser_declspecs (parser, specs, false, true, true);
2014 if (parser->error)
2015 return NULL_TREE;
2016 if (!specs->declspecs_seen_p)
2018 c_parser_error (parser, "expected specifier-qualifier-list");
2019 return NULL_TREE;
2021 finish_declspecs (specs);
2022 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2024 tree ret;
2025 if (!specs->type_seen_p)
2027 if (pedantic)
2028 pedwarn ("ISO C forbids member declarations with no members");
2029 shadow_tag_warned (specs, pedantic);
2030 ret = NULL_TREE;
2032 else
2034 /* Support for unnamed structs or unions as members of
2035 structs or unions (which is [a] useful and [b] supports
2036 MS P-SDK). */
2037 tree attrs = NULL;
2038 ret = grokfield (build_id_declarator (NULL_TREE), specs,
2039 NULL_TREE, &attrs);
2040 if (ret)
2041 decl_attributes (&ret, attrs, 0);
2043 return ret;
2045 pending_xref_error ();
2046 prefix_attrs = specs->attrs;
2047 all_prefix_attrs = prefix_attrs;
2048 specs->attrs = NULL_TREE;
2049 decls = NULL_TREE;
2050 while (true)
2052 /* Declaring one or more declarators or un-named bit-fields. */
2053 struct c_declarator *declarator;
2054 bool dummy = false;
2055 if (c_parser_next_token_is (parser, CPP_COLON))
2056 declarator = build_id_declarator (NULL_TREE);
2057 else
2058 declarator = c_parser_declarator (parser, specs->type_seen_p,
2059 C_DTR_NORMAL, &dummy);
2060 if (declarator == NULL)
2062 c_parser_skip_to_end_of_block_or_statement (parser);
2063 break;
2065 if (c_parser_next_token_is (parser, CPP_COLON)
2066 || c_parser_next_token_is (parser, CPP_COMMA)
2067 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2068 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2069 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2071 tree postfix_attrs = NULL_TREE;
2072 tree width = NULL_TREE;
2073 tree d;
2074 if (c_parser_next_token_is (parser, CPP_COLON))
2076 c_parser_consume_token (parser);
2077 width = c_parser_expr_no_commas (parser, NULL).value;
2079 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2080 postfix_attrs = c_parser_attributes (parser);
2081 d = grokfield (declarator, specs, width, &all_prefix_attrs);
2082 decl_attributes (&d, chainon (postfix_attrs,
2083 all_prefix_attrs), 0);
2084 TREE_CHAIN (d) = decls;
2085 decls = d;
2086 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2087 all_prefix_attrs = chainon (c_parser_attributes (parser),
2088 prefix_attrs);
2089 else
2090 all_prefix_attrs = prefix_attrs;
2091 if (c_parser_next_token_is (parser, CPP_COMMA))
2092 c_parser_consume_token (parser);
2093 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2094 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2096 /* Semicolon consumed in caller. */
2097 break;
2099 else
2101 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2102 break;
2105 else
2107 c_parser_error (parser,
2108 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2109 "%<__attribute__%>");
2110 break;
2113 return decls;
2116 /* Parse a typeof specifier (a GNU extension).
2118 typeof-specifier:
2119 typeof ( expression )
2120 typeof ( type-name )
2123 static struct c_typespec
2124 c_parser_typeof_specifier (c_parser *parser)
2126 struct c_typespec ret;
2127 ret.kind = ctsk_typeof;
2128 ret.spec = error_mark_node;
2129 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2130 c_parser_consume_token (parser);
2131 skip_evaluation++;
2132 in_typeof++;
2133 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2135 skip_evaluation--;
2136 in_typeof--;
2137 return ret;
2139 if (c_parser_next_token_starts_typename (parser))
2141 struct c_type_name *type = c_parser_type_name (parser);
2142 skip_evaluation--;
2143 in_typeof--;
2144 if (type != NULL)
2146 ret.spec = groktypename (type);
2147 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2150 else
2152 bool was_vm;
2153 struct c_expr expr = c_parser_expression (parser);
2154 skip_evaluation--;
2155 in_typeof--;
2156 if (TREE_CODE (expr.value) == COMPONENT_REF
2157 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2158 error ("%<typeof%> applied to a bit-field");
2159 ret.spec = TREE_TYPE (expr.value);
2160 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2161 /* This should be returned with the type so that when the type
2162 is evaluated, this can be evaluated. For now, we avoid
2163 evaluation when the context might. */
2164 if (!skip_evaluation && was_vm)
2166 tree e = expr.value;
2168 /* If the expression is not of a type to which we cannot assign a line
2169 number, wrap the thing in a no-op NOP_EXPR. */
2170 if (DECL_P (e) || CONSTANT_CLASS_P (e))
2171 e = build1 (NOP_EXPR, void_type_node, e);
2173 if (CAN_HAVE_LOCATION_P (e))
2174 SET_EXPR_LOCATION (e, input_location);
2176 add_stmt (e);
2178 pop_maybe_used (was_vm);
2180 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2181 return ret;
2184 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2185 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2186 be redeclared; otherwise it may not. KIND indicates which kind of
2187 declarator is wanted. Returns a valid declarator except in the
2188 case of a syntax error in which case NULL is returned. *SEEN_ID is
2189 set to true if an identifier being declared is seen; this is used
2190 to diagnose bad forms of abstract array declarators and to
2191 determine whether an identifier list is syntactically permitted.
2193 declarator:
2194 pointer[opt] direct-declarator
2196 direct-declarator:
2197 identifier
2198 ( attributes[opt] declarator )
2199 direct-declarator array-declarator
2200 direct-declarator ( parameter-type-list )
2201 direct-declarator ( identifier-list[opt] )
2203 pointer:
2204 * type-qualifier-list[opt]
2205 * type-qualifier-list[opt] pointer
2207 type-qualifier-list:
2208 type-qualifier
2209 attributes
2210 type-qualifier-list type-qualifier
2211 type-qualifier-list attributes
2213 parameter-type-list:
2214 parameter-list
2215 parameter-list , ...
2217 parameter-list:
2218 parameter-declaration
2219 parameter-list , parameter-declaration
2221 parameter-declaration:
2222 declaration-specifiers declarator attributes[opt]
2223 declaration-specifiers abstract-declarator[opt] attributes[opt]
2225 identifier-list:
2226 identifier
2227 identifier-list , identifier
2229 abstract-declarator:
2230 pointer
2231 pointer[opt] direct-abstract-declarator
2233 direct-abstract-declarator:
2234 ( attributes[opt] abstract-declarator )
2235 direct-abstract-declarator[opt] array-declarator
2236 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2238 GNU extensions:
2240 direct-declarator:
2241 direct-declarator ( parameter-forward-declarations
2242 parameter-type-list[opt] )
2244 direct-abstract-declarator:
2245 direct-abstract-declarator[opt] ( parameter-forward-declarations
2246 parameter-type-list[opt] )
2248 parameter-forward-declarations:
2249 parameter-list ;
2250 parameter-forward-declarations parameter-list ;
2252 The uses of attributes shown above are GNU extensions.
2254 Some forms of array declarator are not included in C99 in the
2255 syntax for abstract declarators; these are disallowed elsewhere.
2256 This may be a defect (DR#289).
2258 This function also accepts an omitted abstract declarator as being
2259 an abstract declarator, although not part of the formal syntax. */
2261 static struct c_declarator *
2262 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2263 bool *seen_id)
2265 /* Parse any initial pointer part. */
2266 if (c_parser_next_token_is (parser, CPP_MULT))
2268 struct c_declspecs *quals_attrs = build_null_declspecs ();
2269 struct c_declarator *inner;
2270 c_parser_consume_token (parser);
2271 c_parser_declspecs (parser, quals_attrs, false, false, true);
2272 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2273 if (inner == NULL)
2274 return NULL;
2275 else
2276 return make_pointer_declarator (quals_attrs, inner);
2278 /* Now we have a direct declarator, direct abstract declarator or
2279 nothing (which counts as a direct abstract declarator here). */
2280 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2283 /* Parse a direct declarator or direct abstract declarator; arguments
2284 as c_parser_declarator. */
2286 static struct c_declarator *
2287 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2288 bool *seen_id)
2290 /* The direct declarator must start with an identifier (possibly
2291 omitted) or a parenthesized declarator (possibly abstract). In
2292 an ordinary declarator, initial parentheses must start a
2293 parenthesized declarator. In an abstract declarator or parameter
2294 declarator, they could start a parenthesized declarator or a
2295 parameter list. To tell which, the open parenthesis and any
2296 following attributes must be read. If a declaration specifier
2297 follows, then it is a parameter list; if the specifier is a
2298 typedef name, there might be an ambiguity about redeclaring it,
2299 which is resolved in the direction of treating it as a typedef
2300 name. If a close parenthesis follows, it is also an empty
2301 parameter list, as the syntax does not permit empty abstract
2302 declarators. Otherwise, it is a parenthesized declarator (in
2303 which case the analysis may be repeated inside it, recursively).
2305 ??? There is an ambiguity in a parameter declaration "int
2306 (__attribute__((foo)) x)", where x is not a typedef name: it
2307 could be an abstract declarator for a function, or declare x with
2308 parentheses. The proper resolution of this ambiguity needs
2309 documenting. At present we follow an accident of the old
2310 parser's implementation, whereby the first parameter must have
2311 some declaration specifiers other than just attributes. Thus as
2312 a parameter declaration it is treated as a parenthesized
2313 parameter named x, and as an abstract declarator it is
2314 rejected.
2316 ??? Also following the old parser, attributes inside an empty
2317 parameter list are ignored, making it a list not yielding a
2318 prototype, rather than giving an error or making it have one
2319 parameter with implicit type int.
2321 ??? Also following the old parser, typedef names may be
2322 redeclared in declarators, but not Objective-C class names. */
2324 if (kind != C_DTR_ABSTRACT
2325 && c_parser_next_token_is (parser, CPP_NAME)
2326 && ((type_seen_p
2327 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME)
2328 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2330 struct c_declarator *inner
2331 = build_id_declarator (c_parser_peek_token (parser)->value);
2332 *seen_id = true;
2333 inner->id_loc = c_parser_peek_token (parser)->location;
2334 c_parser_consume_token (parser);
2335 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2338 if (kind != C_DTR_NORMAL
2339 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2341 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2342 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2345 /* Either we are at the end of an abstract declarator, or we have
2346 parentheses. */
2348 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2350 tree attrs;
2351 struct c_declarator *inner;
2352 c_parser_consume_token (parser);
2353 attrs = c_parser_attributes (parser);
2354 if (kind != C_DTR_NORMAL
2355 && (c_parser_next_token_starts_declspecs (parser)
2356 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2358 struct c_arg_info *args
2359 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2360 attrs);
2361 if (args == NULL)
2362 return NULL;
2363 else
2365 inner
2366 = build_function_declarator (args,
2367 build_id_declarator (NULL_TREE));
2368 return c_parser_direct_declarator_inner (parser, *seen_id,
2369 inner);
2372 /* A parenthesized declarator. */
2373 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2374 if (inner != NULL && attrs != NULL)
2375 inner = build_attrs_declarator (attrs, inner);
2376 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2378 c_parser_consume_token (parser);
2379 if (inner == NULL)
2380 return NULL;
2381 else
2382 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2384 else
2386 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2387 "expected %<)%>");
2388 return NULL;
2391 else
2393 if (kind == C_DTR_NORMAL)
2395 c_parser_error (parser, "expected identifier or %<(%>");
2396 return NULL;
2398 else
2399 return build_id_declarator (NULL_TREE);
2403 /* Parse part of a direct declarator or direct abstract declarator,
2404 given that some (in INNER) has already been parsed; ID_PRESENT is
2405 true if an identifier is present, false for an abstract
2406 declarator. */
2408 static struct c_declarator *
2409 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2410 struct c_declarator *inner)
2412 /* Parse a sequence of array declarators and parameter lists. */
2413 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2415 struct c_declarator *declarator;
2416 struct c_declspecs *quals_attrs = build_null_declspecs ();
2417 bool static_seen;
2418 bool star_seen;
2419 tree dimen;
2420 c_parser_consume_token (parser);
2421 c_parser_declspecs (parser, quals_attrs, false, false, true);
2422 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2423 if (static_seen)
2424 c_parser_consume_token (parser);
2425 if (static_seen && !quals_attrs->declspecs_seen_p)
2426 c_parser_declspecs (parser, quals_attrs, false, false, true);
2427 if (!quals_attrs->declspecs_seen_p)
2428 quals_attrs = NULL;
2429 /* If "static" is present, there must be an array dimension.
2430 Otherwise, there may be a dimension, "*", or no
2431 dimension. */
2432 if (static_seen)
2434 star_seen = false;
2435 dimen = c_parser_expr_no_commas (parser, NULL).value;
2437 else
2439 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2441 dimen = NULL_TREE;
2442 star_seen = false;
2444 else if (c_parser_next_token_is (parser, CPP_MULT))
2446 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
2448 dimen = NULL_TREE;
2449 star_seen = true;
2450 c_parser_consume_token (parser);
2452 else
2454 star_seen = false;
2455 dimen = c_parser_expr_no_commas (parser, NULL).value;
2458 else
2460 star_seen = false;
2461 dimen = c_parser_expr_no_commas (parser, NULL).value;
2464 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
2465 c_parser_consume_token (parser);
2466 else
2468 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
2469 "expected %<]%>");
2470 return NULL;
2472 declarator = build_array_declarator (dimen, quals_attrs, static_seen,
2473 star_seen);
2474 if (declarator == NULL)
2475 return NULL;
2476 inner = set_array_declarator_inner (declarator, inner, !id_present);
2477 return c_parser_direct_declarator_inner (parser, id_present, inner);
2479 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2481 tree attrs;
2482 struct c_arg_info *args;
2483 c_parser_consume_token (parser);
2484 attrs = c_parser_attributes (parser);
2485 args = c_parser_parms_declarator (parser, id_present, attrs);
2486 if (args == NULL)
2487 return NULL;
2488 else
2490 inner = build_function_declarator (args, inner);
2491 return c_parser_direct_declarator_inner (parser, id_present, inner);
2494 return inner;
2497 /* Parse a parameter list or identifier list, including the closing
2498 parenthesis but not the opening one. ATTRS are the attributes at
2499 the start of the list. ID_LIST_OK is true if an identifier list is
2500 acceptable; such a list must not have attributes at the start. */
2502 static struct c_arg_info *
2503 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
2505 push_scope ();
2506 declare_parm_level ();
2507 /* If the list starts with an identifier, it is an identifier list.
2508 Otherwise, it is either a prototype list or an empty list. */
2509 if (id_list_ok
2510 && !attrs
2511 && c_parser_next_token_is (parser, CPP_NAME)
2512 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2514 tree list = NULL_TREE, *nextp = &list;
2515 while (c_parser_next_token_is (parser, CPP_NAME)
2516 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
2518 *nextp = build_tree_list (NULL_TREE,
2519 c_parser_peek_token (parser)->value);
2520 nextp = & TREE_CHAIN (*nextp);
2521 c_parser_consume_token (parser);
2522 if (c_parser_next_token_is_not (parser, CPP_COMMA))
2523 break;
2524 c_parser_consume_token (parser);
2525 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2527 c_parser_error (parser, "expected identifier");
2528 break;
2531 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2533 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2534 ret->parms = 0;
2535 ret->tags = 0;
2536 ret->types = list;
2537 ret->others = 0;
2538 ret->pending_sizes = 0;
2539 ret->had_vla_unspec = 0;
2540 c_parser_consume_token (parser);
2541 pop_scope ();
2542 return ret;
2544 else
2546 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2547 "expected %<)%>");
2548 pop_scope ();
2549 return NULL;
2552 else
2554 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs);
2555 pop_scope ();
2556 return ret;
2560 /* Parse a parameter list (possibly empty), including the closing
2561 parenthesis but not the opening one. ATTRS are the attributes at
2562 the start of the list. */
2564 static struct c_arg_info *
2565 c_parser_parms_list_declarator (c_parser *parser, tree attrs)
2567 bool good_parm = false;
2568 /* ??? Following the old parser, forward parameter declarations may
2569 use abstract declarators, and if no real parameter declarations
2570 follow the forward declarations then this is not diagnosed. Also
2571 note as above that attributes are ignored as the only contents of
2572 the parentheses, or as the only contents after forward
2573 declarations. */
2574 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2576 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2577 ret->parms = 0;
2578 ret->tags = 0;
2579 ret->types = 0;
2580 ret->others = 0;
2581 ret->pending_sizes = 0;
2582 ret->had_vla_unspec = 0;
2583 c_parser_consume_token (parser);
2584 return ret;
2586 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2588 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
2589 ret->parms = 0;
2590 ret->tags = 0;
2591 ret->others = 0;
2592 ret->pending_sizes = 0;
2593 ret->had_vla_unspec = 0;
2594 /* Suppress -Wold-style-definition for this case. */
2595 ret->types = error_mark_node;
2596 error ("ISO C requires a named argument before %<...%>");
2597 c_parser_consume_token (parser);
2598 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2600 c_parser_consume_token (parser);
2601 return ret;
2603 else
2605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2606 "expected %<)%>");
2607 return NULL;
2610 /* Nonempty list of parameters, either terminated with semicolon
2611 (forward declarations; recurse) or with close parenthesis (normal
2612 function) or with ", ... )" (variadic function). */
2613 while (true)
2615 /* Parse a parameter. */
2616 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
2617 attrs = NULL_TREE;
2618 if (parm != NULL)
2620 good_parm = true;
2621 push_parm_decl (parm);
2623 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2625 tree new_attrs;
2626 c_parser_consume_token (parser);
2627 mark_forward_parm_decls ();
2628 new_attrs = c_parser_attributes (parser);
2629 return c_parser_parms_list_declarator (parser, new_attrs);
2631 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2633 c_parser_consume_token (parser);
2634 if (good_parm)
2635 return get_parm_info (false);
2636 else
2638 struct c_arg_info *ret
2639 = XOBNEW (&parser_obstack, struct c_arg_info);
2640 ret->parms = 0;
2641 ret->tags = 0;
2642 ret->types = 0;
2643 ret->others = 0;
2644 ret->pending_sizes = 0;
2645 ret->had_vla_unspec = 0;
2646 return ret;
2649 if (!c_parser_require (parser, CPP_COMMA,
2650 "expected %<;%>, %<,%> or %<)%>"))
2652 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2653 return NULL;
2655 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
2657 c_parser_consume_token (parser);
2658 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2660 c_parser_consume_token (parser);
2661 if (good_parm)
2662 return get_parm_info (true);
2663 else
2665 struct c_arg_info *ret
2666 = XOBNEW (&parser_obstack, struct c_arg_info);
2667 ret->parms = 0;
2668 ret->tags = 0;
2669 ret->types = 0;
2670 ret->others = 0;
2671 ret->pending_sizes = 0;
2672 ret->had_vla_unspec = 0;
2673 return ret;
2676 else
2678 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2679 "expected %<)%>");
2680 return NULL;
2686 /* Parse a parameter declaration. ATTRS are the attributes at the
2687 start of the declaration if it is the first parameter. */
2689 static struct c_parm *
2690 c_parser_parameter_declaration (c_parser *parser, tree attrs)
2692 struct c_declspecs *specs;
2693 struct c_declarator *declarator;
2694 tree prefix_attrs;
2695 tree postfix_attrs = NULL_TREE;
2696 bool dummy = false;
2697 if (!c_parser_next_token_starts_declspecs (parser))
2699 /* ??? In some Objective-C cases '...' isn't applicable so there
2700 should be a different message. */
2701 c_parser_error (parser,
2702 "expected declaration specifiers or %<...%>");
2703 c_parser_skip_to_end_of_parameter (parser);
2704 return NULL;
2706 specs = build_null_declspecs ();
2707 if (attrs)
2709 declspecs_add_attrs (specs, attrs);
2710 attrs = NULL_TREE;
2712 c_parser_declspecs (parser, specs, true, true, true);
2713 finish_declspecs (specs);
2714 pending_xref_error ();
2715 prefix_attrs = specs->attrs;
2716 specs->attrs = NULL_TREE;
2717 declarator = c_parser_declarator (parser, specs->type_seen_p,
2718 C_DTR_PARM, &dummy);
2719 if (declarator == NULL)
2721 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
2722 return NULL;
2724 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2725 postfix_attrs = c_parser_attributes (parser);
2726 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
2727 declarator);
2730 /* Parse a string literal in an asm expression. It should not be
2731 translated, and wide string literals are an error although
2732 permitted by the syntax. This is a GNU extension.
2734 asm-string-literal:
2735 string-literal
2737 ??? At present, following the old parser, the caller needs to have
2738 set lex_untranslated_string to 1. It would be better to follow the
2739 C++ parser rather than using this kludge. */
2741 static tree
2742 c_parser_asm_string_literal (c_parser *parser)
2744 tree str;
2745 if (c_parser_next_token_is (parser, CPP_STRING))
2747 str = c_parser_peek_token (parser)->value;
2748 c_parser_consume_token (parser);
2750 else if (c_parser_next_token_is (parser, CPP_WSTRING))
2752 error ("wide string literal in %<asm%>");
2753 str = build_string (1, "");
2754 c_parser_consume_token (parser);
2756 else
2758 c_parser_error (parser, "expected string literal");
2759 str = NULL_TREE;
2761 return str;
2764 /* Parse a simple asm expression. This is used in restricted
2765 contexts, where a full expression with inputs and outputs does not
2766 make sense. This is a GNU extension.
2768 simple-asm-expr:
2769 asm ( asm-string-literal )
2772 static tree
2773 c_parser_simple_asm_expr (c_parser *parser)
2775 tree str;
2776 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
2777 /* ??? Follow the C++ parser rather than using the
2778 lex_untranslated_string kludge. */
2779 parser->lex_untranslated_string = true;
2780 c_parser_consume_token (parser);
2781 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2783 parser->lex_untranslated_string = false;
2784 return NULL_TREE;
2786 str = c_parser_asm_string_literal (parser);
2787 parser->lex_untranslated_string = false;
2788 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
2790 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2791 return NULL_TREE;
2793 return str;
2796 /* Parse (possibly empty) attributes. This is a GNU extension.
2798 attributes:
2799 empty
2800 attributes attribute
2802 attribute:
2803 __attribute__ ( ( attribute-list ) )
2805 attribute-list:
2806 attrib
2807 attribute_list , attrib
2809 attrib:
2810 empty
2811 any-word
2812 any-word ( identifier )
2813 any-word ( identifier , nonempty-expr-list )
2814 any-word ( expr-list )
2816 where the "identifier" must not be declared as a type, and
2817 "any-word" may be any identifier (including one declared as a
2818 type), a reserved word storage class specifier, type specifier or
2819 type qualifier. ??? This still leaves out most reserved keywords
2820 (following the old parser), shouldn't we include them, and why not
2821 allow identifiers declared as types to start the arguments? */
2823 static tree
2824 c_parser_attributes (c_parser *parser)
2826 tree attrs = NULL_TREE;
2827 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2829 /* ??? Follow the C++ parser rather than using the
2830 lex_untranslated_string kludge. */
2831 parser->lex_untranslated_string = true;
2832 c_parser_consume_token (parser);
2833 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2835 parser->lex_untranslated_string = false;
2836 return attrs;
2838 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2840 parser->lex_untranslated_string = false;
2841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2842 return attrs;
2844 /* Parse the attribute list. */
2845 while (c_parser_next_token_is (parser, CPP_COMMA)
2846 || c_parser_next_token_is (parser, CPP_NAME)
2847 || c_parser_next_token_is (parser, CPP_KEYWORD))
2849 tree attr, attr_name, attr_args;
2850 if (c_parser_next_token_is (parser, CPP_COMMA))
2852 c_parser_consume_token (parser);
2853 continue;
2855 if (c_parser_next_token_is (parser, CPP_KEYWORD))
2857 /* ??? See comment above about what keywords are
2858 accepted here. */
2859 bool ok;
2860 switch (c_parser_peek_token (parser)->keyword)
2862 case RID_STATIC:
2863 case RID_UNSIGNED:
2864 case RID_LONG:
2865 case RID_CONST:
2866 case RID_EXTERN:
2867 case RID_REGISTER:
2868 case RID_TYPEDEF:
2869 case RID_SHORT:
2870 case RID_INLINE:
2871 case RID_VOLATILE:
2872 case RID_SIGNED:
2873 case RID_AUTO:
2874 case RID_RESTRICT:
2875 case RID_COMPLEX:
2876 case RID_THREAD:
2877 case RID_INT:
2878 case RID_CHAR:
2879 case RID_FLOAT:
2880 case RID_DOUBLE:
2881 case RID_VOID:
2882 case RID_DFLOAT32:
2883 case RID_DFLOAT64:
2884 case RID_DFLOAT128:
2885 case RID_BOOL:
2886 case RID_FRACT:
2887 case RID_ACCUM:
2888 case RID_SAT:
2889 ok = true;
2890 break;
2891 default:
2892 ok = false;
2893 break;
2895 if (!ok)
2896 break;
2898 attr_name = c_parser_peek_token (parser)->value;
2899 c_parser_consume_token (parser);
2900 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
2902 attr = build_tree_list (attr_name, NULL_TREE);
2903 attrs = chainon (attrs, attr);
2904 continue;
2906 c_parser_consume_token (parser);
2907 /* Parse the attribute contents. If they start with an
2908 identifier which is followed by a comma or close
2909 parenthesis, then the arguments start with that
2910 identifier; otherwise they are an expression list. */
2911 if (c_parser_next_token_is (parser, CPP_NAME)
2912 && c_parser_peek_token (parser)->id_kind == C_ID_ID
2913 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
2914 || (c_parser_peek_2nd_token (parser)->type
2915 == CPP_CLOSE_PAREN)))
2917 tree arg1 = c_parser_peek_token (parser)->value;
2918 c_parser_consume_token (parser);
2919 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2920 attr_args = build_tree_list (NULL_TREE, arg1);
2921 else
2923 c_parser_consume_token (parser);
2924 attr_args = tree_cons (NULL_TREE, arg1,
2925 c_parser_expr_list (parser, false));
2928 else
2930 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2931 attr_args = NULL_TREE;
2932 else
2933 attr_args = c_parser_expr_list (parser, false);
2935 attr = build_tree_list (attr_name, attr_args);
2936 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2937 c_parser_consume_token (parser);
2938 else
2940 parser->lex_untranslated_string = false;
2941 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2942 "expected %<)%>");
2943 return attrs;
2945 attrs = chainon (attrs, attr);
2947 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2948 c_parser_consume_token (parser);
2949 else
2951 parser->lex_untranslated_string = false;
2952 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2953 "expected %<)%>");
2954 return attrs;
2956 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2957 c_parser_consume_token (parser);
2958 else
2960 parser->lex_untranslated_string = false;
2961 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2962 "expected %<)%>");
2963 return attrs;
2965 parser->lex_untranslated_string = false;
2967 return attrs;
2970 /* Parse a type name (C90 6.5.5, C99 6.7.6).
2972 type-name:
2973 specifier-qualifier-list abstract-declarator[opt]
2976 static struct c_type_name *
2977 c_parser_type_name (c_parser *parser)
2979 struct c_declspecs *specs = build_null_declspecs ();
2980 struct c_declarator *declarator;
2981 struct c_type_name *ret;
2982 bool dummy = false;
2983 c_parser_declspecs (parser, specs, false, true, true);
2984 if (!specs->declspecs_seen_p)
2986 c_parser_error (parser, "expected specifier-qualifier-list");
2987 return NULL;
2989 pending_xref_error ();
2990 finish_declspecs (specs);
2991 declarator = c_parser_declarator (parser, specs->type_seen_p,
2992 C_DTR_ABSTRACT, &dummy);
2993 if (declarator == NULL)
2994 return NULL;
2995 ret = XOBNEW (&parser_obstack, struct c_type_name);
2996 ret->specs = specs;
2997 ret->declarator = declarator;
2998 return ret;
3001 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3003 initializer:
3004 assignment-expression
3005 { initializer-list }
3006 { initializer-list , }
3008 initializer-list:
3009 designation[opt] initializer
3010 initializer-list , designation[opt] initializer
3012 designation:
3013 designator-list =
3015 designator-list:
3016 designator
3017 designator-list designator
3019 designator:
3020 array-designator
3021 . identifier
3023 array-designator:
3024 [ constant-expression ]
3026 GNU extensions:
3028 initializer:
3031 designation:
3032 array-designator
3033 identifier :
3035 array-designator:
3036 [ constant-expression ... constant-expression ]
3038 Any expression without commas is accepted in the syntax for the
3039 constant-expressions, with non-constant expressions rejected later.
3041 This function is only used for top-level initializers; for nested
3042 ones, see c_parser_initval. */
3044 static struct c_expr
3045 c_parser_initializer (c_parser *parser)
3047 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3048 return c_parser_braced_init (parser, NULL_TREE, false);
3049 else
3051 struct c_expr ret;
3052 ret = c_parser_expr_no_commas (parser, NULL);
3053 if (TREE_CODE (ret.value) != STRING_CST
3054 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3055 ret = default_function_array_conversion (ret);
3056 return ret;
3060 /* Parse a braced initializer list. TYPE is the type specified for a
3061 compound literal, and NULL_TREE for other initializers and for
3062 nested braced lists. NESTED_P is true for nested braced lists,
3063 false for the list of a compound literal or the list that is the
3064 top-level initializer in a declaration. */
3066 static struct c_expr
3067 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3069 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3070 c_parser_consume_token (parser);
3071 if (nested_p)
3072 push_init_level (0);
3073 else
3074 really_start_incremental_init (type);
3075 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3077 if (pedantic)
3078 pedwarn ("ISO C forbids empty initializer braces");
3080 else
3082 /* Parse a non-empty initializer list, possibly with a trailing
3083 comma. */
3084 while (true)
3086 c_parser_initelt (parser);
3087 if (parser->error)
3088 break;
3089 if (c_parser_next_token_is (parser, CPP_COMMA))
3090 c_parser_consume_token (parser);
3091 else
3092 break;
3093 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3094 break;
3097 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3099 struct c_expr ret;
3100 ret.value = error_mark_node;
3101 ret.original_code = ERROR_MARK;
3102 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3103 return ret;
3105 c_parser_consume_token (parser);
3106 return pop_init_level (0);
3109 /* Parse a nested initializer, including designators. */
3111 static void
3112 c_parser_initelt (c_parser *parser)
3114 /* Parse any designator or designator list. A single array
3115 designator may have the subsequent "=" omitted in GNU C, but a
3116 longer list or a structure member designator may not. */
3117 if (c_parser_next_token_is (parser, CPP_NAME)
3118 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3120 /* Old-style structure member designator. */
3121 set_init_label (c_parser_peek_token (parser)->value);
3122 if (pedantic)
3123 pedwarn ("obsolete use of designated initializer with %<:%>");
3124 c_parser_consume_token (parser);
3125 c_parser_consume_token (parser);
3127 else
3129 /* des_seen is 0 if there have been no designators, 1 if there
3130 has been a single array designator and 2 otherwise. */
3131 int des_seen = 0;
3132 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3133 || c_parser_next_token_is (parser, CPP_DOT))
3135 int des_prev = des_seen;
3136 if (des_seen < 2)
3137 des_seen++;
3138 if (c_parser_next_token_is (parser, CPP_DOT))
3140 des_seen = 2;
3141 c_parser_consume_token (parser);
3142 if (c_parser_next_token_is (parser, CPP_NAME))
3144 set_init_label (c_parser_peek_token (parser)->value);
3145 c_parser_consume_token (parser);
3147 else
3149 struct c_expr init;
3150 init.value = error_mark_node;
3151 init.original_code = ERROR_MARK;
3152 c_parser_error (parser, "expected identifier");
3153 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3154 process_init_element (init);
3155 return;
3158 else
3160 tree first, second;
3161 /* ??? Following the old parser, [ objc-receiver
3162 objc-message-args ] is accepted as an initializer,
3163 being distinguished from a designator by what follows
3164 the first assignment expression inside the square
3165 brackets, but after a first array designator a
3166 subsequent square bracket is for Objective-C taken to
3167 start an expression, using the obsolete form of
3168 designated initializer without '=', rather than
3169 possibly being a second level of designation: in LALR
3170 terms, the '[' is shifted rather than reducing
3171 designator to designator-list. */
3172 if (des_prev == 1 && c_dialect_objc ())
3174 des_seen = des_prev;
3175 break;
3177 if (des_prev == 0 && c_dialect_objc ())
3179 /* This might be an array designator or an
3180 Objective-C message expression. If the former,
3181 continue parsing here; if the latter, parse the
3182 remainder of the initializer given the starting
3183 primary-expression. ??? It might make sense to
3184 distinguish when des_prev == 1 as well; see
3185 previous comment. */
3186 tree rec, args;
3187 struct c_expr mexpr;
3188 c_parser_consume_token (parser);
3189 if (c_parser_peek_token (parser)->type == CPP_NAME
3190 && ((c_parser_peek_token (parser)->id_kind
3191 == C_ID_TYPENAME)
3192 || (c_parser_peek_token (parser)->id_kind
3193 == C_ID_CLASSNAME)))
3195 /* Type name receiver. */
3196 tree id = c_parser_peek_token (parser)->value;
3197 c_parser_consume_token (parser);
3198 rec = objc_get_class_reference (id);
3199 goto parse_message_args;
3201 first = c_parser_expr_no_commas (parser, NULL).value;
3202 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3203 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3204 goto array_desig_after_first;
3205 /* Expression receiver. So far only one part
3206 without commas has been parsed; there might be
3207 more of the expression. */
3208 rec = first;
3209 while (c_parser_next_token_is (parser, CPP_COMMA))
3211 struct c_expr next;
3212 c_parser_consume_token (parser);
3213 next = c_parser_expr_no_commas (parser, NULL);
3214 next = default_function_array_conversion (next);
3215 rec = build_compound_expr (rec, next.value);
3217 parse_message_args:
3218 /* Now parse the objc-message-args. */
3219 args = c_parser_objc_message_args (parser);
3220 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3221 "expected %<]%>");
3222 mexpr.value
3223 = objc_build_message_expr (build_tree_list (rec, args));
3224 mexpr.original_code = ERROR_MARK;
3225 /* Now parse and process the remainder of the
3226 initializer, starting with this message
3227 expression as a primary-expression. */
3228 c_parser_initval (parser, &mexpr);
3229 return;
3231 c_parser_consume_token (parser);
3232 first = c_parser_expr_no_commas (parser, NULL).value;
3233 array_desig_after_first:
3234 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3236 c_parser_consume_token (parser);
3237 second = c_parser_expr_no_commas (parser, NULL).value;
3239 else
3240 second = NULL_TREE;
3241 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3243 c_parser_consume_token (parser);
3244 set_init_index (first, second);
3245 if (pedantic && second)
3246 pedwarn ("ISO C forbids specifying range of "
3247 "elements to initialize");
3249 else
3250 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3251 "expected %<]%>");
3254 if (des_seen >= 1)
3256 if (c_parser_next_token_is (parser, CPP_EQ))
3258 if (pedantic && !flag_isoc99)
3259 pedwarn ("ISO C90 forbids specifying subobject to initialize");
3260 c_parser_consume_token (parser);
3262 else
3264 if (des_seen == 1)
3266 if (pedantic)
3267 pedwarn ("obsolete use of designated initializer "
3268 "without %<=%>");
3270 else
3272 struct c_expr init;
3273 init.value = error_mark_node;
3274 init.original_code = ERROR_MARK;
3275 c_parser_error (parser, "expected %<=%>");
3276 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3277 process_init_element (init);
3278 return;
3283 c_parser_initval (parser, NULL);
3286 /* Parse a nested initializer; as c_parser_initializer but parses
3287 initializers within braced lists, after any designators have been
3288 applied. If AFTER is not NULL then it is an Objective-C message
3289 expression which is the primary-expression starting the
3290 initializer. */
3292 static void
3293 c_parser_initval (c_parser *parser, struct c_expr *after)
3295 struct c_expr init;
3296 gcc_assert (!after || c_dialect_objc ());
3297 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3298 init = c_parser_braced_init (parser, NULL_TREE, true);
3299 else
3301 init = c_parser_expr_no_commas (parser, after);
3302 if (init.value != NULL_TREE
3303 && TREE_CODE (init.value) != STRING_CST
3304 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3305 init = default_function_array_conversion (init);
3307 process_init_element (init);
3310 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3311 C99 6.8.2).
3313 compound-statement:
3314 { block-item-list[opt] }
3315 { label-declarations block-item-list }
3317 block-item-list:
3318 block-item
3319 block-item-list block-item
3321 block-item:
3322 nested-declaration
3323 statement
3325 nested-declaration:
3326 declaration
3328 GNU extensions:
3330 compound-statement:
3331 { label-declarations block-item-list }
3333 nested-declaration:
3334 __extension__ nested-declaration
3335 nested-function-definition
3337 label-declarations:
3338 label-declaration
3339 label-declarations label-declaration
3341 label-declaration:
3342 __label__ identifier-list ;
3344 Allowing the mixing of declarations and code is new in C99. The
3345 GNU syntax also permits (not shown above) labels at the end of
3346 compound statements, which yield an error. We don't allow labels
3347 on declarations; this might seem like a natural extension, but
3348 there would be a conflict between attributes on the label and
3349 prefix attributes on the declaration. ??? The syntax follows the
3350 old parser in requiring something after label declarations.
3351 Although they are erroneous if the labels declared aren't defined,
3352 is it useful for the syntax to be this way?
3354 OpenMP:
3356 block-item:
3357 openmp-directive
3359 openmp-directive:
3360 barrier-directive
3361 flush-directive */
3363 static tree
3364 c_parser_compound_statement (c_parser *parser)
3366 tree stmt;
3367 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3368 return error_mark_node;
3369 stmt = c_begin_compound_stmt (true);
3370 c_parser_compound_statement_nostart (parser);
3371 return c_end_compound_stmt (stmt, true);
3374 /* Parse a compound statement except for the opening brace. This is
3375 used for parsing both compound statements and statement expressions
3376 (which follow different paths to handling the opening). */
3378 static void
3379 c_parser_compound_statement_nostart (c_parser *parser)
3381 bool last_stmt = false;
3382 bool last_label = false;
3383 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3385 c_parser_consume_token (parser);
3386 return;
3388 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
3390 /* Read zero or more forward-declarations for labels that nested
3391 functions can jump to. */
3392 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
3394 c_parser_consume_token (parser);
3395 /* Any identifiers, including those declared as type names,
3396 are OK here. */
3397 while (true)
3399 tree label;
3400 if (c_parser_next_token_is_not (parser, CPP_NAME))
3402 c_parser_error (parser, "expected identifier");
3403 break;
3405 label
3406 = declare_label (c_parser_peek_token (parser)->value);
3407 C_DECLARED_LABEL_FLAG (label) = 1;
3408 add_stmt (build_stmt (DECL_EXPR, label));
3409 c_parser_consume_token (parser);
3410 if (c_parser_next_token_is (parser, CPP_COMMA))
3411 c_parser_consume_token (parser);
3412 else
3413 break;
3415 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3417 /* ??? Locating this diagnostic on the token after the
3418 declarations end follows the old parser, but it might be
3419 better to locate it where the declarations start instead. */
3420 if (pedantic)
3421 pedwarn ("ISO C forbids label declarations");
3423 /* We must now have at least one statement, label or declaration. */
3424 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3426 c_parser_error (parser, "expected declaration or statement");
3427 c_parser_consume_token (parser);
3428 return;
3430 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3432 location_t loc = c_parser_peek_token (parser)->location;
3433 if (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))
3438 last_label = true;
3439 last_stmt = false;
3440 c_parser_label (parser);
3442 else if (!last_label
3443 && c_parser_next_token_starts_declspecs (parser))
3445 last_label = false;
3446 c_parser_declaration_or_fndef (parser, true, true, true, true);
3447 if (last_stmt
3448 && ((pedantic && !flag_isoc99)
3449 || warn_declaration_after_statement))
3450 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3451 &loc);
3452 last_stmt = false;
3454 else if (!last_label
3455 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3457 /* __extension__ can start a declaration, but is also an
3458 unary operator that can start an expression. Consume all
3459 but the last of a possible series of __extension__ to
3460 determine which. */
3461 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
3462 && (c_parser_peek_2nd_token (parser)->keyword
3463 == RID_EXTENSION))
3464 c_parser_consume_token (parser);
3465 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
3467 int ext;
3468 ext = disable_extension_diagnostics ();
3469 c_parser_consume_token (parser);
3470 last_label = false;
3471 c_parser_declaration_or_fndef (parser, true, true, true, true);
3472 /* Following the old parser, __extension__ does not
3473 disable this diagnostic. */
3474 restore_extension_diagnostics (ext);
3475 if (last_stmt
3476 && ((pedantic && !flag_isoc99)
3477 || warn_declaration_after_statement))
3478 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code",
3479 &loc);
3480 last_stmt = false;
3482 else
3483 goto statement;
3485 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
3487 /* External pragmas, and some omp pragmas, are not associated
3488 with regular c code, and so are not to be considered statements
3489 syntactically. This ensures that the user doesn't put them
3490 places that would turn into syntax errors if the directive
3491 were ignored. */
3492 if (c_parser_pragma (parser, pragma_compound))
3493 last_label = false, last_stmt = true;
3495 else if (c_parser_next_token_is (parser, CPP_EOF))
3497 c_parser_error (parser, "expected declaration or statement");
3498 return;
3500 else
3502 statement:
3503 last_label = false;
3504 last_stmt = true;
3505 c_parser_statement_after_labels (parser);
3508 parser->error = false;
3510 if (last_label)
3511 error ("label at end of compound statement");
3512 c_parser_consume_token (parser);
3515 /* Parse a label (C90 6.6.1, C99 6.8.1).
3517 label:
3518 identifier : attributes[opt]
3519 case constant-expression :
3520 default :
3522 GNU extensions:
3524 label:
3525 case constant-expression ... constant-expression :
3527 The use of attributes on labels is a GNU extension. The syntax in
3528 GNU C accepts any expressions without commas, non-constant
3529 expressions being rejected later. */
3531 static void
3532 c_parser_label (c_parser *parser)
3534 location_t loc1 = c_parser_peek_token (parser)->location;
3535 tree label = NULL_TREE;
3536 if (c_parser_next_token_is_keyword (parser, RID_CASE))
3538 tree exp1, exp2;
3539 c_parser_consume_token (parser);
3540 exp1 = c_parser_expr_no_commas (parser, NULL).value;
3541 if (c_parser_next_token_is (parser, CPP_COLON))
3543 c_parser_consume_token (parser);
3544 label = do_case (exp1, NULL_TREE);
3546 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3548 c_parser_consume_token (parser);
3549 exp2 = c_parser_expr_no_commas (parser, NULL).value;
3550 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3551 label = do_case (exp1, exp2);
3553 else
3554 c_parser_error (parser, "expected %<:%> or %<...%>");
3556 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
3558 c_parser_consume_token (parser);
3559 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
3560 label = do_case (NULL_TREE, NULL_TREE);
3562 else
3564 tree name = c_parser_peek_token (parser)->value;
3565 tree tlab;
3566 location_t loc2;
3567 tree attrs;
3568 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
3569 c_parser_consume_token (parser);
3570 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
3571 loc2 = c_parser_peek_token (parser)->location;
3572 c_parser_consume_token (parser);
3573 attrs = c_parser_attributes (parser);
3574 tlab = define_label (loc2, name);
3575 if (tlab)
3577 decl_attributes (&tlab, attrs, 0);
3578 label = add_stmt (build_stmt (LABEL_EXPR, tlab));
3581 if (label)
3582 SET_EXPR_LOCATION (label, loc1);
3585 /* Parse a statement (C90 6.6, C99 6.8).
3587 statement:
3588 labeled-statement
3589 compound-statement
3590 expression-statement
3591 selection-statement
3592 iteration-statement
3593 jump-statement
3595 labeled-statement:
3596 label statement
3598 expression-statement:
3599 expression[opt] ;
3601 selection-statement:
3602 if-statement
3603 switch-statement
3605 iteration-statement:
3606 while-statement
3607 do-statement
3608 for-statement
3610 jump-statement:
3611 goto identifier ;
3612 continue ;
3613 break ;
3614 return expression[opt] ;
3616 GNU extensions:
3618 statement:
3619 asm-statement
3621 jump-statement:
3622 goto * expression ;
3624 Objective-C:
3626 statement:
3627 objc-throw-statement
3628 objc-try-catch-statement
3629 objc-synchronized-statement
3631 objc-throw-statement:
3632 @throw expression ;
3633 @throw ;
3635 OpenMP:
3637 statement:
3638 openmp-construct
3640 openmp-construct:
3641 parallel-construct
3642 for-construct
3643 sections-construct
3644 single-construct
3645 parallel-for-construct
3646 parallel-sections-construct
3647 master-construct
3648 critical-construct
3649 atomic-construct
3650 ordered-construct
3652 parallel-construct:
3653 parallel-directive structured-block
3655 for-construct:
3656 for-directive iteration-statement
3658 sections-construct:
3659 sections-directive section-scope
3661 single-construct:
3662 single-directive structured-block
3664 parallel-for-construct:
3665 parallel-for-directive iteration-statement
3667 parallel-sections-construct:
3668 parallel-sections-directive section-scope
3670 master-construct:
3671 master-directive structured-block
3673 critical-construct:
3674 critical-directive structured-block
3676 atomic-construct:
3677 atomic-directive expression-statement
3679 ordered-construct:
3680 ordered-directive structured-block */
3682 static void
3683 c_parser_statement (c_parser *parser)
3685 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3686 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3687 || (c_parser_next_token_is (parser, CPP_NAME)
3688 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3689 c_parser_label (parser);
3690 c_parser_statement_after_labels (parser);
3693 /* Parse a statement, other than a labeled statement. */
3695 static void
3696 c_parser_statement_after_labels (c_parser *parser)
3698 location_t loc = c_parser_peek_token (parser)->location;
3699 tree stmt = NULL_TREE;
3700 switch (c_parser_peek_token (parser)->type)
3702 case CPP_OPEN_BRACE:
3703 add_stmt (c_parser_compound_statement (parser));
3704 break;
3705 case CPP_KEYWORD:
3706 switch (c_parser_peek_token (parser)->keyword)
3708 case RID_IF:
3709 c_parser_if_statement (parser);
3710 break;
3711 case RID_SWITCH:
3712 c_parser_switch_statement (parser);
3713 break;
3714 case RID_WHILE:
3715 c_parser_while_statement (parser);
3716 break;
3717 case RID_DO:
3718 c_parser_do_statement (parser);
3719 break;
3720 case RID_FOR:
3721 c_parser_for_statement (parser);
3722 break;
3723 case RID_GOTO:
3724 c_parser_consume_token (parser);
3725 if (c_parser_next_token_is (parser, CPP_NAME))
3727 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value);
3728 c_parser_consume_token (parser);
3730 else if (c_parser_next_token_is (parser, CPP_MULT))
3732 c_parser_consume_token (parser);
3733 stmt = c_finish_goto_ptr (c_parser_expression (parser).value);
3735 else
3736 c_parser_error (parser, "expected identifier or %<*%>");
3737 goto expect_semicolon;
3738 case RID_CONTINUE:
3739 c_parser_consume_token (parser);
3740 stmt = c_finish_bc_stmt (&c_cont_label, false);
3741 goto expect_semicolon;
3742 case RID_BREAK:
3743 c_parser_consume_token (parser);
3744 stmt = c_finish_bc_stmt (&c_break_label, true);
3745 goto expect_semicolon;
3746 case RID_RETURN:
3747 c_parser_consume_token (parser);
3748 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3750 stmt = c_finish_return (NULL_TREE);
3751 c_parser_consume_token (parser);
3753 else
3755 stmt = c_finish_return (c_parser_expression_conv (parser).value);
3756 goto expect_semicolon;
3758 break;
3759 case RID_ASM:
3760 stmt = c_parser_asm_statement (parser);
3761 break;
3762 case RID_AT_THROW:
3763 gcc_assert (c_dialect_objc ());
3764 c_parser_consume_token (parser);
3765 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3767 stmt = objc_build_throw_stmt (NULL_TREE);
3768 c_parser_consume_token (parser);
3770 else
3772 stmt
3773 = objc_build_throw_stmt (c_parser_expression (parser).value);
3774 goto expect_semicolon;
3776 break;
3777 case RID_AT_TRY:
3778 gcc_assert (c_dialect_objc ());
3779 c_parser_objc_try_catch_statement (parser);
3780 break;
3781 case RID_AT_SYNCHRONIZED:
3782 gcc_assert (c_dialect_objc ());
3783 c_parser_objc_synchronized_statement (parser);
3784 break;
3785 default:
3786 goto expr_stmt;
3788 break;
3789 case CPP_SEMICOLON:
3790 c_parser_consume_token (parser);
3791 break;
3792 case CPP_CLOSE_PAREN:
3793 case CPP_CLOSE_SQUARE:
3794 /* Avoid infinite loop in error recovery:
3795 c_parser_skip_until_found stops at a closing nesting
3796 delimiter without consuming it, but here we need to consume
3797 it to proceed further. */
3798 c_parser_error (parser, "expected statement");
3799 c_parser_consume_token (parser);
3800 break;
3801 case CPP_PRAGMA:
3802 c_parser_pragma (parser, pragma_stmt);
3803 break;
3804 default:
3805 expr_stmt:
3806 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value);
3807 expect_semicolon:
3808 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
3809 break;
3811 /* Two cases cannot and do not have line numbers associated: If stmt
3812 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
3813 cannot hold line numbers. But that's OK because the statement
3814 will either be changed to a MODIFY_EXPR during gimplification of
3815 the statement expr, or discarded. If stmt was compound, but
3816 without new variables, we will have skipped the creation of a
3817 BIND and will have a bare STATEMENT_LIST. But that's OK because
3818 (recursively) all of the component statements should already have
3819 line numbers assigned. ??? Can we discard no-op statements
3820 earlier? */
3821 if (stmt && CAN_HAVE_LOCATION_P (stmt))
3822 SET_EXPR_LOCATION (stmt, loc);
3825 /* Parse a parenthesized condition from an if, do or while statement.
3827 condition:
3828 ( expression )
3830 static tree
3831 c_parser_paren_condition (c_parser *parser)
3833 location_t loc;
3834 tree cond;
3835 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3836 return error_mark_node;
3837 loc = c_parser_peek_token (parser)->location;
3838 cond = c_objc_common_truthvalue_conversion
3839 (c_parser_expression_conv (parser).value);
3840 if (CAN_HAVE_LOCATION_P (cond))
3841 SET_EXPR_LOCATION (cond, loc);
3842 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3843 return cond;
3846 /* Parse a statement which is a block in C99. */
3848 static tree
3849 c_parser_c99_block_statement (c_parser *parser)
3851 tree block = c_begin_compound_stmt (flag_isoc99);
3852 c_parser_statement (parser);
3853 return c_end_compound_stmt (block, flag_isoc99);
3856 /* Parse the body of an if statement or the else half thereof. This
3857 is just parsing a statement but (a) it is a block in C99, (b) we
3858 track whether the body is an if statement for the sake of
3859 -Wparentheses warnings, (c) we handle an empty body specially for
3860 the sake of -Wempty-body warnings. */
3862 static tree
3863 c_parser_if_body (c_parser *parser, bool *if_p)
3865 tree block = c_begin_compound_stmt (flag_isoc99);
3866 while (c_parser_next_token_is_keyword (parser, RID_CASE)
3867 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
3868 || (c_parser_next_token_is (parser, CPP_NAME)
3869 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
3870 c_parser_label (parser);
3871 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
3872 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3873 add_stmt (build_empty_stmt ());
3874 c_parser_statement_after_labels (parser);
3875 return c_end_compound_stmt (block, flag_isoc99);
3878 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
3880 if-statement:
3881 if ( expression ) statement
3882 if ( expression ) statement else statement
3885 static void
3886 c_parser_if_statement (c_parser *parser)
3888 tree block;
3889 location_t loc;
3890 tree cond;
3891 bool first_if = false, second_if = false;
3892 tree first_body, second_body;
3893 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
3894 c_parser_consume_token (parser);
3895 block = c_begin_compound_stmt (flag_isoc99);
3896 loc = c_parser_peek_token (parser)->location;
3897 cond = c_parser_paren_condition (parser);
3898 first_body = c_parser_if_body (parser, &first_if);
3899 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
3901 c_parser_consume_token (parser);
3902 second_body = c_parser_if_body (parser, &second_if);
3904 else
3905 second_body = NULL_TREE;
3906 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
3907 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3910 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
3912 switch-statement:
3913 switch (expression) statement
3916 static void
3917 c_parser_switch_statement (c_parser *parser)
3919 tree block, expr, body, save_break;
3920 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
3921 c_parser_consume_token (parser);
3922 block = c_begin_compound_stmt (flag_isoc99);
3923 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3925 expr = c_parser_expression (parser).value;
3926 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3928 else
3929 expr = error_mark_node;
3930 c_start_case (expr);
3931 save_break = c_break_label;
3932 c_break_label = NULL_TREE;
3933 body = c_parser_c99_block_statement (parser);
3934 c_finish_case (body);
3935 if (c_break_label)
3936 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label));
3937 c_break_label = save_break;
3938 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3941 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
3943 while-statement:
3944 while (expression) statement
3947 static void
3948 c_parser_while_statement (c_parser *parser)
3950 tree block, cond, body, save_break, save_cont;
3951 location_t loc;
3952 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
3953 c_parser_consume_token (parser);
3954 block = c_begin_compound_stmt (flag_isoc99);
3955 loc = c_parser_peek_token (parser)->location;
3956 cond = c_parser_paren_condition (parser);
3957 save_break = c_break_label;
3958 c_break_label = NULL_TREE;
3959 save_cont = c_cont_label;
3960 c_cont_label = NULL_TREE;
3961 body = c_parser_c99_block_statement (parser);
3962 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
3963 add_stmt (c_end_compound_stmt (block, flag_isoc99));
3964 c_break_label = save_break;
3965 c_cont_label = save_cont;
3968 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
3970 do-statement:
3971 do statement while ( expression ) ;
3974 static void
3975 c_parser_do_statement (c_parser *parser)
3977 tree block, cond, body, save_break, save_cont, new_break, new_cont;
3978 location_t loc;
3979 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
3980 c_parser_consume_token (parser);
3981 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3982 warning (OPT_Wempty_body,
3983 "suggest braces around empty body in %<do%> statement");
3984 block = c_begin_compound_stmt (flag_isoc99);
3985 loc = c_parser_peek_token (parser)->location;
3986 save_break = c_break_label;
3987 c_break_label = NULL_TREE;
3988 save_cont = c_cont_label;
3989 c_cont_label = NULL_TREE;
3990 body = c_parser_c99_block_statement (parser);
3991 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
3992 new_break = c_break_label;
3993 c_break_label = save_break;
3994 new_cont = c_cont_label;
3995 c_cont_label = save_cont;
3996 cond = c_parser_paren_condition (parser);
3997 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
3998 c_parser_skip_to_end_of_block_or_statement (parser);
3999 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4000 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4003 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4005 for-statement:
4006 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4007 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4009 The form with a declaration is new in C99.
4011 ??? In accordance with the old parser, the declaration may be a
4012 nested function, which is then rejected in check_for_loop_decls,
4013 but does it make any sense for this to be included in the grammar?
4014 Note in particular that the nested function does not include a
4015 trailing ';', whereas the "declaration" production includes one.
4016 Also, can we reject bad declarations earlier and cheaper than
4017 check_for_loop_decls? */
4019 static void
4020 c_parser_for_statement (c_parser *parser)
4022 tree block, cond, incr, save_break, save_cont, body;
4023 location_t loc;
4024 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4025 loc = c_parser_peek_token (parser)->location;
4026 c_parser_consume_token (parser);
4027 block = c_begin_compound_stmt (flag_isoc99);
4028 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4030 /* Parse the initialization declaration or expression. */
4031 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4033 c_parser_consume_token (parser);
4034 c_finish_expr_stmt (NULL_TREE);
4036 else if (c_parser_next_token_starts_declspecs (parser))
4038 c_parser_declaration_or_fndef (parser, true, true, true, true);
4039 check_for_loop_decls ();
4041 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4043 /* __extension__ can start a declaration, but is also an
4044 unary operator that can start an expression. Consume all
4045 but the last of a possible series of __extension__ to
4046 determine which. */
4047 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4048 && (c_parser_peek_2nd_token (parser)->keyword
4049 == RID_EXTENSION))
4050 c_parser_consume_token (parser);
4051 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser)))
4053 int ext;
4054 ext = disable_extension_diagnostics ();
4055 c_parser_consume_token (parser);
4056 c_parser_declaration_or_fndef (parser, true, true, true, true);
4057 restore_extension_diagnostics (ext);
4058 check_for_loop_decls ();
4060 else
4061 goto init_expr;
4063 else
4065 init_expr:
4066 c_finish_expr_stmt (c_parser_expression (parser).value);
4067 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4069 /* Parse the loop condition. */
4070 loc = c_parser_peek_token (parser)->location;
4071 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4073 c_parser_consume_token (parser);
4074 cond = NULL_TREE;
4076 else
4078 tree ocond = c_parser_expression_conv (parser).value;
4079 cond = c_objc_common_truthvalue_conversion (ocond);
4080 if (CAN_HAVE_LOCATION_P (cond))
4081 SET_EXPR_LOCATION (cond, loc);
4082 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4084 /* Parse the increment expression. */
4085 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4086 incr = c_process_expr_stmt (NULL_TREE);
4087 else
4088 incr = c_process_expr_stmt (c_parser_expression (parser).value);
4089 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4091 else
4093 cond = error_mark_node;
4094 incr = error_mark_node;
4096 save_break = c_break_label;
4097 c_break_label = NULL_TREE;
4098 save_cont = c_cont_label;
4099 c_cont_label = NULL_TREE;
4100 body = c_parser_c99_block_statement (parser);
4101 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4102 add_stmt (c_end_compound_stmt (block, flag_isoc99));
4103 c_break_label = save_break;
4104 c_cont_label = save_cont;
4107 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4108 statement with inputs, outputs, clobbers, and volatile tag
4109 allowed.
4111 asm-statement:
4112 asm type-qualifier[opt] ( asm-argument ) ;
4114 asm-argument:
4115 asm-string-literal
4116 asm-string-literal : asm-operands[opt]
4117 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4118 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers
4120 Qualifiers other than volatile are accepted in the syntax but
4121 warned for. */
4123 static tree
4124 c_parser_asm_statement (c_parser *parser)
4126 tree quals, str, outputs, inputs, clobbers, ret;
4127 bool simple;
4128 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4129 c_parser_consume_token (parser);
4130 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4132 quals = c_parser_peek_token (parser)->value;
4133 c_parser_consume_token (parser);
4135 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4136 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4138 warning (0, "%E qualifier ignored on asm",
4139 c_parser_peek_token (parser)->value);
4140 quals = NULL_TREE;
4141 c_parser_consume_token (parser);
4143 else
4144 quals = NULL_TREE;
4145 /* ??? Follow the C++ parser rather than using the
4146 lex_untranslated_string kludge. */
4147 parser->lex_untranslated_string = true;
4148 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4150 parser->lex_untranslated_string = false;
4151 return NULL_TREE;
4153 str = c_parser_asm_string_literal (parser);
4154 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4156 simple = true;
4157 outputs = NULL_TREE;
4158 inputs = NULL_TREE;
4159 clobbers = NULL_TREE;
4160 goto done_asm;
4162 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4164 parser->lex_untranslated_string = false;
4165 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4166 return NULL_TREE;
4168 simple = false;
4169 /* Parse outputs. */
4170 if (c_parser_next_token_is (parser, CPP_COLON)
4171 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4172 outputs = NULL_TREE;
4173 else
4174 outputs = c_parser_asm_operands (parser, false);
4175 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4177 inputs = NULL_TREE;
4178 clobbers = NULL_TREE;
4179 goto done_asm;
4181 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4183 parser->lex_untranslated_string = false;
4184 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4185 return NULL_TREE;
4187 /* Parse inputs. */
4188 if (c_parser_next_token_is (parser, CPP_COLON)
4189 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4190 inputs = NULL_TREE;
4191 else
4192 inputs = c_parser_asm_operands (parser, true);
4193 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4195 clobbers = NULL_TREE;
4196 goto done_asm;
4198 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>"))
4200 parser->lex_untranslated_string = false;
4201 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4202 return NULL_TREE;
4204 /* Parse clobbers. */
4205 clobbers = c_parser_asm_clobbers (parser);
4206 done_asm:
4207 parser->lex_untranslated_string = false;
4208 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4210 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4211 return NULL_TREE;
4213 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4214 c_parser_skip_to_end_of_block_or_statement (parser);
4215 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs,
4216 clobbers, simple));
4217 return ret;
4220 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
4221 not outputs), apply the default conversion of functions and arrays
4222 to pointers.
4224 asm-operands:
4225 asm-operand
4226 asm-operands , asm-operand
4228 asm-operand:
4229 asm-string-literal ( expression )
4230 [ identifier ] asm-string-literal ( expression )
4233 static tree
4234 c_parser_asm_operands (c_parser *parser, bool convert_p)
4236 tree list = NULL_TREE;
4237 while (true)
4239 tree name, str;
4240 struct c_expr expr;
4241 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
4243 c_parser_consume_token (parser);
4244 if (c_parser_next_token_is (parser, CPP_NAME))
4246 tree id = c_parser_peek_token (parser)->value;
4247 c_parser_consume_token (parser);
4248 name = build_string (IDENTIFIER_LENGTH (id),
4249 IDENTIFIER_POINTER (id));
4251 else
4253 c_parser_error (parser, "expected identifier");
4254 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
4255 return NULL_TREE;
4257 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4258 "expected %<]%>");
4260 else
4261 name = NULL_TREE;
4262 str = c_parser_asm_string_literal (parser);
4263 if (str == NULL_TREE)
4264 return NULL_TREE;
4265 parser->lex_untranslated_string = false;
4266 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4268 parser->lex_untranslated_string = true;
4269 return NULL_TREE;
4271 expr = c_parser_expression (parser);
4272 if (convert_p)
4273 expr = default_function_array_conversion (expr);
4274 parser->lex_untranslated_string = true;
4275 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
4277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4278 return NULL_TREE;
4280 list = chainon (list, build_tree_list (build_tree_list (name, str),
4281 expr.value));
4282 if (c_parser_next_token_is (parser, CPP_COMMA))
4283 c_parser_consume_token (parser);
4284 else
4285 break;
4287 return list;
4290 /* Parse asm clobbers, a GNU extension.
4292 asm-clobbers:
4293 asm-string-literal
4294 asm-clobbers , asm-string-literal
4297 static tree
4298 c_parser_asm_clobbers (c_parser *parser)
4300 tree list = NULL_TREE;
4301 while (true)
4303 tree str = c_parser_asm_string_literal (parser);
4304 if (str)
4305 list = tree_cons (NULL_TREE, str, list);
4306 else
4307 return NULL_TREE;
4308 if (c_parser_next_token_is (parser, CPP_COMMA))
4309 c_parser_consume_token (parser);
4310 else
4311 break;
4313 return list;
4316 /* Parse an expression other than a compound expression; that is, an
4317 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
4318 NULL then it is an Objective-C message expression which is the
4319 primary-expression starting the expression as an initializer.
4321 assignment-expression:
4322 conditional-expression
4323 unary-expression assignment-operator assignment-expression
4325 assignment-operator: one of
4326 = *= /= %= += -= <<= >>= &= ^= |=
4328 In GNU C we accept any conditional expression on the LHS and
4329 diagnose the invalid lvalue rather than producing a syntax
4330 error. */
4332 static struct c_expr
4333 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
4335 struct c_expr lhs, rhs, ret;
4336 enum tree_code code;
4337 gcc_assert (!after || c_dialect_objc ());
4338 lhs = c_parser_conditional_expression (parser, after);
4339 switch (c_parser_peek_token (parser)->type)
4341 case CPP_EQ:
4342 code = NOP_EXPR;
4343 break;
4344 case CPP_MULT_EQ:
4345 code = MULT_EXPR;
4346 break;
4347 case CPP_DIV_EQ:
4348 code = TRUNC_DIV_EXPR;
4349 break;
4350 case CPP_MOD_EQ:
4351 code = TRUNC_MOD_EXPR;
4352 break;
4353 case CPP_PLUS_EQ:
4354 code = PLUS_EXPR;
4355 break;
4356 case CPP_MINUS_EQ:
4357 code = MINUS_EXPR;
4358 break;
4359 case CPP_LSHIFT_EQ:
4360 code = LSHIFT_EXPR;
4361 break;
4362 case CPP_RSHIFT_EQ:
4363 code = RSHIFT_EXPR;
4364 break;
4365 case CPP_AND_EQ:
4366 code = BIT_AND_EXPR;
4367 break;
4368 case CPP_XOR_EQ:
4369 code = BIT_XOR_EXPR;
4370 break;
4371 case CPP_OR_EQ:
4372 code = BIT_IOR_EXPR;
4373 break;
4374 default:
4375 return lhs;
4377 c_parser_consume_token (parser);
4378 rhs = c_parser_expr_no_commas (parser, NULL);
4379 rhs = default_function_array_conversion (rhs);
4380 ret.value = build_modify_expr (lhs.value, code, rhs.value);
4381 if (code == NOP_EXPR)
4382 ret.original_code = MODIFY_EXPR;
4383 else
4385 TREE_NO_WARNING (ret.value) = 1;
4386 ret.original_code = ERROR_MARK;
4388 return ret;
4391 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
4392 is not NULL then it is an Objective-C message expression which is
4393 the primary-expression starting the expression as an initializer.
4395 conditional-expression:
4396 logical-OR-expression
4397 logical-OR-expression ? expression : conditional-expression
4399 GNU extensions:
4401 conditional-expression:
4402 logical-OR-expression ? : conditional-expression
4405 static struct c_expr
4406 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
4408 struct c_expr cond, exp1, exp2, ret;
4409 gcc_assert (!after || c_dialect_objc ());
4410 cond = c_parser_binary_expression (parser, after);
4411 if (c_parser_next_token_is_not (parser, CPP_QUERY))
4412 return cond;
4413 cond = default_function_array_conversion (cond);
4414 c_parser_consume_token (parser);
4415 if (c_parser_next_token_is (parser, CPP_COLON))
4417 if (pedantic)
4418 pedwarn ("ISO C forbids omitting the middle term of a ?: expression");
4419 /* Make sure first operand is calculated only once. */
4420 exp1.value = save_expr (default_conversion (cond.value));
4421 cond.value = c_objc_common_truthvalue_conversion (exp1.value);
4422 skip_evaluation += cond.value == truthvalue_true_node;
4424 else
4426 cond.value
4427 = c_objc_common_truthvalue_conversion
4428 (default_conversion (cond.value));
4429 skip_evaluation += cond.value == truthvalue_false_node;
4430 exp1 = c_parser_expression_conv (parser);
4431 skip_evaluation += ((cond.value == truthvalue_true_node)
4432 - (cond.value == truthvalue_false_node));
4434 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4436 skip_evaluation -= cond.value == truthvalue_true_node;
4437 ret.value = error_mark_node;
4438 ret.original_code = ERROR_MARK;
4439 return ret;
4441 exp2 = c_parser_conditional_expression (parser, NULL);
4442 exp2 = default_function_array_conversion (exp2);
4443 skip_evaluation -= cond.value == truthvalue_true_node;
4444 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value);
4445 ret.original_code = ERROR_MARK;
4446 return ret;
4449 /* Parse a binary expression; that is, a logical-OR-expression (C90
4450 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
4451 an Objective-C message expression which is the primary-expression
4452 starting the expression as an initializer.
4454 multiplicative-expression:
4455 cast-expression
4456 multiplicative-expression * cast-expression
4457 multiplicative-expression / cast-expression
4458 multiplicative-expression % cast-expression
4460 additive-expression:
4461 multiplicative-expression
4462 additive-expression + multiplicative-expression
4463 additive-expression - multiplicative-expression
4465 shift-expression:
4466 additive-expression
4467 shift-expression << additive-expression
4468 shift-expression >> additive-expression
4470 relational-expression:
4471 shift-expression
4472 relational-expression < shift-expression
4473 relational-expression > shift-expression
4474 relational-expression <= shift-expression
4475 relational-expression >= shift-expression
4477 equality-expression:
4478 relational-expression
4479 equality-expression == relational-expression
4480 equality-expression != relational-expression
4482 AND-expression:
4483 equality-expression
4484 AND-expression & equality-expression
4486 exclusive-OR-expression:
4487 AND-expression
4488 exclusive-OR-expression ^ AND-expression
4490 inclusive-OR-expression:
4491 exclusive-OR-expression
4492 inclusive-OR-expression | exclusive-OR-expression
4494 logical-AND-expression:
4495 inclusive-OR-expression
4496 logical-AND-expression && inclusive-OR-expression
4498 logical-OR-expression:
4499 logical-AND-expression
4500 logical-OR-expression || logical-AND-expression
4503 static struct c_expr
4504 c_parser_binary_expression (c_parser *parser, struct c_expr *after)
4506 /* A binary expression is parsed using operator-precedence parsing,
4507 with the operands being cast expressions. All the binary
4508 operators are left-associative. Thus a binary expression is of
4509 form:
4511 E0 op1 E1 op2 E2 ...
4513 which we represent on a stack. On the stack, the precedence
4514 levels are strictly increasing. When a new operator is
4515 encountered of higher precedence than that at the top of the
4516 stack, it is pushed; its LHS is the top expression, and its RHS
4517 is everything parsed until it is popped. When a new operator is
4518 encountered with precedence less than or equal to that at the top
4519 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
4520 by the result of the operation until the operator at the top of
4521 the stack has lower precedence than the new operator or there is
4522 only one element on the stack; then the top expression is the LHS
4523 of the new operator. In the case of logical AND and OR
4524 expressions, we also need to adjust skip_evaluation as
4525 appropriate when the operators are pushed and popped. */
4527 /* The precedence levels, where 0 is a dummy lowest level used for
4528 the bottom of the stack. */
4529 enum prec {
4530 PREC_NONE,
4531 PREC_LOGOR,
4532 PREC_LOGAND,
4533 PREC_BITOR,
4534 PREC_BITXOR,
4535 PREC_BITAND,
4536 PREC_EQ,
4537 PREC_REL,
4538 PREC_SHIFT,
4539 PREC_ADD,
4540 PREC_MULT,
4541 NUM_PRECS
4543 struct {
4544 /* The expression at this stack level. */
4545 struct c_expr expr;
4546 /* The precedence of the operator on its left, PREC_NONE at the
4547 bottom of the stack. */
4548 enum prec prec;
4549 /* The operation on its left. */
4550 enum tree_code op;
4551 } stack[NUM_PRECS];
4552 int sp;
4553 #define POP \
4554 do { \
4555 switch (stack[sp].op) \
4557 case TRUTH_ANDIF_EXPR: \
4558 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \
4559 break; \
4560 case TRUTH_ORIF_EXPR: \
4561 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \
4562 break; \
4563 default: \
4564 break; \
4566 stack[sp - 1].expr \
4567 = default_function_array_conversion (stack[sp - 1].expr); \
4568 stack[sp].expr \
4569 = default_function_array_conversion (stack[sp].expr); \
4570 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \
4571 stack[sp - 1].expr, \
4572 stack[sp].expr); \
4573 sp--; \
4574 } while (0)
4575 gcc_assert (!after || c_dialect_objc ());
4576 stack[0].expr = c_parser_cast_expression (parser, after);
4577 stack[0].prec = PREC_NONE;
4578 sp = 0;
4579 while (true)
4581 enum prec oprec;
4582 enum tree_code ocode;
4583 if (parser->error)
4584 goto out;
4585 switch (c_parser_peek_token (parser)->type)
4587 case CPP_MULT:
4588 oprec = PREC_MULT;
4589 ocode = MULT_EXPR;
4590 break;
4591 case CPP_DIV:
4592 oprec = PREC_MULT;
4593 ocode = TRUNC_DIV_EXPR;
4594 break;
4595 case CPP_MOD:
4596 oprec = PREC_MULT;
4597 ocode = TRUNC_MOD_EXPR;
4598 break;
4599 case CPP_PLUS:
4600 oprec = PREC_ADD;
4601 ocode = PLUS_EXPR;
4602 break;
4603 case CPP_MINUS:
4604 oprec = PREC_ADD;
4605 ocode = MINUS_EXPR;
4606 break;
4607 case CPP_LSHIFT:
4608 oprec = PREC_SHIFT;
4609 ocode = LSHIFT_EXPR;
4610 break;
4611 case CPP_RSHIFT:
4612 oprec = PREC_SHIFT;
4613 ocode = RSHIFT_EXPR;
4614 break;
4615 case CPP_LESS:
4616 oprec = PREC_REL;
4617 ocode = LT_EXPR;
4618 break;
4619 case CPP_GREATER:
4620 oprec = PREC_REL;
4621 ocode = GT_EXPR;
4622 break;
4623 case CPP_LESS_EQ:
4624 oprec = PREC_REL;
4625 ocode = LE_EXPR;
4626 break;
4627 case CPP_GREATER_EQ:
4628 oprec = PREC_REL;
4629 ocode = GE_EXPR;
4630 break;
4631 case CPP_EQ_EQ:
4632 oprec = PREC_EQ;
4633 ocode = EQ_EXPR;
4634 break;
4635 case CPP_NOT_EQ:
4636 oprec = PREC_EQ;
4637 ocode = NE_EXPR;
4638 break;
4639 case CPP_AND:
4640 oprec = PREC_BITAND;
4641 ocode = BIT_AND_EXPR;
4642 break;
4643 case CPP_XOR:
4644 oprec = PREC_BITXOR;
4645 ocode = BIT_XOR_EXPR;
4646 break;
4647 case CPP_OR:
4648 oprec = PREC_BITOR;
4649 ocode = BIT_IOR_EXPR;
4650 break;
4651 case CPP_AND_AND:
4652 oprec = PREC_LOGAND;
4653 ocode = TRUTH_ANDIF_EXPR;
4654 break;
4655 case CPP_OR_OR:
4656 oprec = PREC_LOGOR;
4657 ocode = TRUTH_ORIF_EXPR;
4658 break;
4659 default:
4660 /* Not a binary operator, so end of the binary
4661 expression. */
4662 goto out;
4664 c_parser_consume_token (parser);
4665 while (oprec <= stack[sp].prec)
4666 POP;
4667 switch (ocode)
4669 case TRUTH_ANDIF_EXPR:
4670 stack[sp].expr
4671 = default_function_array_conversion (stack[sp].expr);
4672 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4673 (default_conversion (stack[sp].expr.value));
4674 skip_evaluation += stack[sp].expr.value == truthvalue_false_node;
4675 break;
4676 case TRUTH_ORIF_EXPR:
4677 stack[sp].expr
4678 = default_function_array_conversion (stack[sp].expr);
4679 stack[sp].expr.value = c_objc_common_truthvalue_conversion
4680 (default_conversion (stack[sp].expr.value));
4681 skip_evaluation += stack[sp].expr.value == truthvalue_true_node;
4682 break;
4683 default:
4684 break;
4686 sp++;
4687 stack[sp].expr = c_parser_cast_expression (parser, NULL);
4688 stack[sp].prec = oprec;
4689 stack[sp].op = ocode;
4691 out:
4692 while (sp > 0)
4693 POP;
4694 return stack[0].expr;
4695 #undef POP
4698 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
4699 NULL then it is an Objective-C message expression which is the
4700 primary-expression starting the expression as an initializer.
4702 cast-expression:
4703 unary-expression
4704 ( type-name ) unary-expression
4707 static struct c_expr
4708 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
4710 gcc_assert (!after || c_dialect_objc ());
4711 if (after)
4712 return c_parser_postfix_expression_after_primary (parser, *after);
4713 /* If the expression begins with a parenthesized type name, it may
4714 be either a cast or a compound literal; we need to see whether
4715 the next character is '{' to tell the difference. If not, it is
4716 an unary expression. */
4717 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4718 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4720 struct c_type_name *type_name;
4721 struct c_expr ret;
4722 struct c_expr expr;
4723 c_parser_consume_token (parser);
4724 type_name = c_parser_type_name (parser);
4725 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4726 if (type_name == NULL)
4728 ret.value = error_mark_node;
4729 ret.original_code = ERROR_MARK;
4730 return ret;
4733 /* Save casted types in the function's used types hash table. */
4734 used_types_insert (type_name->specs->type);
4736 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4737 return c_parser_postfix_expression_after_paren_type (parser,
4738 type_name);
4739 expr = c_parser_cast_expression (parser, NULL);
4740 expr = default_function_array_conversion (expr);
4741 ret.value = c_cast_expr (type_name, expr.value);
4742 ret.original_code = ERROR_MARK;
4743 return ret;
4745 else
4746 return c_parser_unary_expression (parser);
4749 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
4751 unary-expression:
4752 postfix-expression
4753 ++ unary-expression
4754 -- unary-expression
4755 unary-operator cast-expression
4756 sizeof unary-expression
4757 sizeof ( type-name )
4759 unary-operator: one of
4760 & * + - ~ !
4762 GNU extensions:
4764 unary-expression:
4765 __alignof__ unary-expression
4766 __alignof__ ( type-name )
4767 && identifier
4769 unary-operator: one of
4770 __extension__ __real__ __imag__
4772 In addition, the GNU syntax treats ++ and -- as unary operators, so
4773 they may be applied to cast expressions with errors for non-lvalues
4774 given later. */
4776 static struct c_expr
4777 c_parser_unary_expression (c_parser *parser)
4779 int ext;
4780 struct c_expr ret, op;
4781 switch (c_parser_peek_token (parser)->type)
4783 case CPP_PLUS_PLUS:
4784 c_parser_consume_token (parser);
4785 op = c_parser_cast_expression (parser, NULL);
4786 op = default_function_array_conversion (op);
4787 return parser_build_unary_op (PREINCREMENT_EXPR, op);
4788 case CPP_MINUS_MINUS:
4789 c_parser_consume_token (parser);
4790 op = c_parser_cast_expression (parser, NULL);
4791 op = default_function_array_conversion (op);
4792 return parser_build_unary_op (PREDECREMENT_EXPR, op);
4793 case CPP_AND:
4794 c_parser_consume_token (parser);
4795 return parser_build_unary_op (ADDR_EXPR,
4796 c_parser_cast_expression (parser, NULL));
4797 case CPP_MULT:
4798 c_parser_consume_token (parser);
4799 op = c_parser_cast_expression (parser, NULL);
4800 op = default_function_array_conversion (op);
4801 ret.value = build_indirect_ref (op.value, "unary *");
4802 ret.original_code = ERROR_MARK;
4803 return ret;
4804 case CPP_PLUS:
4805 c_parser_consume_token (parser);
4806 if (!c_dialect_objc () && !in_system_header)
4807 warning (OPT_Wtraditional,
4808 "traditional C rejects the unary plus operator");
4809 op = c_parser_cast_expression (parser, NULL);
4810 op = default_function_array_conversion (op);
4811 return parser_build_unary_op (CONVERT_EXPR, op);
4812 case CPP_MINUS:
4813 c_parser_consume_token (parser);
4814 op = c_parser_cast_expression (parser, NULL);
4815 op = default_function_array_conversion (op);
4816 return parser_build_unary_op (NEGATE_EXPR, op);
4817 case CPP_COMPL:
4818 c_parser_consume_token (parser);
4819 op = c_parser_cast_expression (parser, NULL);
4820 op = default_function_array_conversion (op);
4821 return parser_build_unary_op (BIT_NOT_EXPR, op);
4822 case CPP_NOT:
4823 c_parser_consume_token (parser);
4824 op = c_parser_cast_expression (parser, NULL);
4825 op = default_function_array_conversion (op);
4826 return parser_build_unary_op (TRUTH_NOT_EXPR, op);
4827 case CPP_AND_AND:
4828 /* Refer to the address of a label as a pointer. */
4829 c_parser_consume_token (parser);
4830 if (c_parser_next_token_is (parser, CPP_NAME))
4832 ret.value = finish_label_address_expr
4833 (c_parser_peek_token (parser)->value);
4834 c_parser_consume_token (parser);
4836 else
4838 c_parser_error (parser, "expected identifier");
4839 ret.value = error_mark_node;
4841 ret.original_code = ERROR_MARK;
4842 return ret;
4843 case CPP_KEYWORD:
4844 switch (c_parser_peek_token (parser)->keyword)
4846 case RID_SIZEOF:
4847 return c_parser_sizeof_expression (parser);
4848 case RID_ALIGNOF:
4849 return c_parser_alignof_expression (parser);
4850 case RID_EXTENSION:
4851 c_parser_consume_token (parser);
4852 ext = disable_extension_diagnostics ();
4853 ret = c_parser_cast_expression (parser, NULL);
4854 restore_extension_diagnostics (ext);
4855 return ret;
4856 case RID_REALPART:
4857 c_parser_consume_token (parser);
4858 op = c_parser_cast_expression (parser, NULL);
4859 op = default_function_array_conversion (op);
4860 return parser_build_unary_op (REALPART_EXPR, op);
4861 case RID_IMAGPART:
4862 c_parser_consume_token (parser);
4863 op = c_parser_cast_expression (parser, NULL);
4864 op = default_function_array_conversion (op);
4865 return parser_build_unary_op (IMAGPART_EXPR, op);
4866 default:
4867 return c_parser_postfix_expression (parser);
4869 default:
4870 return c_parser_postfix_expression (parser);
4874 /* Parse a sizeof expression. */
4876 static struct c_expr
4877 c_parser_sizeof_expression (c_parser *parser)
4879 struct c_expr expr;
4880 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
4881 c_parser_consume_token (parser);
4882 skip_evaluation++;
4883 in_sizeof++;
4884 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4885 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4887 /* Either sizeof ( type-name ) or sizeof unary-expression
4888 starting with a compound literal. */
4889 struct c_type_name *type_name;
4890 c_parser_consume_token (parser);
4891 type_name = c_parser_type_name (parser);
4892 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4893 if (type_name == NULL)
4895 struct c_expr ret;
4896 skip_evaluation--;
4897 in_sizeof--;
4898 ret.value = error_mark_node;
4899 ret.original_code = ERROR_MARK;
4900 return ret;
4902 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4904 expr = c_parser_postfix_expression_after_paren_type (parser,
4905 type_name);
4906 goto sizeof_expr;
4908 /* sizeof ( type-name ). */
4909 skip_evaluation--;
4910 in_sizeof--;
4911 if (type_name->declarator->kind == cdk_array
4912 && type_name->declarator->u.array.vla_unspec_p)
4914 /* C99 6.7.5.2p4 */
4915 error ("%<[*]%> not allowed in other than a declaration");
4917 return c_expr_sizeof_type (type_name);
4919 else
4921 expr = c_parser_unary_expression (parser);
4922 sizeof_expr:
4923 skip_evaluation--;
4924 in_sizeof--;
4925 if (TREE_CODE (expr.value) == COMPONENT_REF
4926 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
4927 error ("%<sizeof%> applied to a bit-field");
4928 return c_expr_sizeof_expr (expr);
4932 /* Parse an alignof expression. */
4934 static struct c_expr
4935 c_parser_alignof_expression (c_parser *parser)
4937 struct c_expr expr;
4938 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
4939 c_parser_consume_token (parser);
4940 skip_evaluation++;
4941 in_alignof++;
4942 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
4943 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
4945 /* Either __alignof__ ( type-name ) or __alignof__
4946 unary-expression starting with a compound literal. */
4947 struct c_type_name *type_name;
4948 struct c_expr ret;
4949 c_parser_consume_token (parser);
4950 type_name = c_parser_type_name (parser);
4951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4952 if (type_name == NULL)
4954 struct c_expr ret;
4955 skip_evaluation--;
4956 in_alignof--;
4957 ret.value = error_mark_node;
4958 ret.original_code = ERROR_MARK;
4959 return ret;
4961 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4963 expr = c_parser_postfix_expression_after_paren_type (parser,
4964 type_name);
4965 goto alignof_expr;
4967 /* alignof ( type-name ). */
4968 skip_evaluation--;
4969 in_alignof--;
4970 ret.value = c_alignof (groktypename (type_name));
4971 ret.original_code = ERROR_MARK;
4972 return ret;
4974 else
4976 struct c_expr ret;
4977 expr = c_parser_unary_expression (parser);
4978 alignof_expr:
4979 skip_evaluation--;
4980 in_alignof--;
4981 ret.value = c_alignof_expr (expr.value);
4982 ret.original_code = ERROR_MARK;
4983 return ret;
4987 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
4989 postfix-expression:
4990 primary-expression
4991 postfix-expression [ expression ]
4992 postfix-expression ( argument-expression-list[opt] )
4993 postfix-expression . identifier
4994 postfix-expression -> identifier
4995 postfix-expression ++
4996 postfix-expression --
4997 ( type-name ) { initializer-list }
4998 ( type-name ) { initializer-list , }
5000 argument-expression-list:
5001 argument-expression
5002 argument-expression-list , argument-expression
5004 primary-expression:
5005 identifier
5006 constant
5007 string-literal
5008 ( expression )
5010 GNU extensions:
5012 primary-expression:
5013 __func__
5014 (treated as a keyword in GNU C)
5015 __FUNCTION__
5016 __PRETTY_FUNCTION__
5017 ( compound-statement )
5018 __builtin_va_arg ( assignment-expression , type-name )
5019 __builtin_offsetof ( type-name , offsetof-member-designator )
5020 __builtin_choose_expr ( assignment-expression ,
5021 assignment-expression ,
5022 assignment-expression )
5023 __builtin_types_compatible_p ( type-name , type-name )
5025 offsetof-member-designator:
5026 identifier
5027 offsetof-member-designator . identifier
5028 offsetof-member-designator [ expression ]
5030 Objective-C:
5032 primary-expression:
5033 [ objc-receiver objc-message-args ]
5034 @selector ( objc-selector-arg )
5035 @protocol ( identifier )
5036 @encode ( type-name )
5037 objc-string-literal
5040 static struct c_expr
5041 c_parser_postfix_expression (c_parser *parser)
5043 struct c_expr expr, e1, e2, e3;
5044 struct c_type_name *t1, *t2;
5045 switch (c_parser_peek_token (parser)->type)
5047 case CPP_NUMBER:
5048 case CPP_CHAR:
5049 case CPP_WCHAR:
5050 expr.value = c_parser_peek_token (parser)->value;
5051 expr.original_code = ERROR_MARK;
5052 c_parser_consume_token (parser);
5053 break;
5054 case CPP_STRING:
5055 case CPP_WSTRING:
5056 expr.value = c_parser_peek_token (parser)->value;
5057 expr.original_code = STRING_CST;
5058 c_parser_consume_token (parser);
5059 break;
5060 case CPP_OBJC_STRING:
5061 gcc_assert (c_dialect_objc ());
5062 expr.value
5063 = objc_build_string_object (c_parser_peek_token (parser)->value);
5064 expr.original_code = ERROR_MARK;
5065 c_parser_consume_token (parser);
5066 break;
5067 case CPP_NAME:
5068 if (c_parser_peek_token (parser)->id_kind != C_ID_ID)
5070 c_parser_error (parser, "expected expression");
5071 expr.value = error_mark_node;
5072 expr.original_code = ERROR_MARK;
5073 break;
5076 tree id = c_parser_peek_token (parser)->value;
5077 location_t loc = c_parser_peek_token (parser)->location;
5078 c_parser_consume_token (parser);
5079 expr.value = build_external_ref (id,
5080 (c_parser_peek_token (parser)->type
5081 == CPP_OPEN_PAREN), loc);
5082 expr.original_code = ERROR_MARK;
5084 break;
5085 case CPP_OPEN_PAREN:
5086 /* A parenthesized expression, statement expression or compound
5087 literal. */
5088 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
5090 /* A statement expression. */
5091 tree stmt;
5092 c_parser_consume_token (parser);
5093 c_parser_consume_token (parser);
5094 if (cur_stmt_list == NULL)
5096 error ("braced-group within expression allowed "
5097 "only inside a function");
5098 parser->error = true;
5099 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
5100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5101 expr.value = error_mark_node;
5102 expr.original_code = ERROR_MARK;
5103 break;
5105 stmt = c_begin_stmt_expr ();
5106 c_parser_compound_statement_nostart (parser);
5107 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5108 "expected %<)%>");
5109 if (pedantic)
5110 pedwarn ("ISO C forbids braced-groups within expressions");
5111 expr.value = c_finish_stmt_expr (stmt);
5112 expr.original_code = ERROR_MARK;
5114 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5116 /* A compound literal. ??? Can we actually get here rather
5117 than going directly to
5118 c_parser_postfix_expression_after_paren_type from
5119 elsewhere? */
5120 struct c_type_name *type_name;
5121 c_parser_consume_token (parser);
5122 type_name = c_parser_type_name (parser);
5123 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5124 "expected %<)%>");
5125 if (type_name == NULL)
5127 expr.value = error_mark_node;
5128 expr.original_code = ERROR_MARK;
5130 else
5131 expr = c_parser_postfix_expression_after_paren_type (parser,
5132 type_name);
5134 else
5136 /* A parenthesized expression. */
5137 c_parser_consume_token (parser);
5138 expr = c_parser_expression (parser);
5139 if (TREE_CODE (expr.value) == MODIFY_EXPR)
5140 TREE_NO_WARNING (expr.value) = 1;
5141 expr.original_code = ERROR_MARK;
5142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5143 "expected %<)%>");
5145 break;
5146 case CPP_KEYWORD:
5147 switch (c_parser_peek_token (parser)->keyword)
5149 case RID_FUNCTION_NAME:
5150 case RID_PRETTY_FUNCTION_NAME:
5151 case RID_C99_FUNCTION_NAME:
5152 expr.value = fname_decl (c_parser_peek_token (parser)->keyword,
5153 c_parser_peek_token (parser)->value);
5154 expr.original_code = ERROR_MARK;
5155 c_parser_consume_token (parser);
5156 break;
5157 case RID_VA_ARG:
5158 c_parser_consume_token (parser);
5159 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5161 expr.value = error_mark_node;
5162 expr.original_code = ERROR_MARK;
5163 break;
5165 e1 = c_parser_expr_no_commas (parser, NULL);
5166 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5168 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5169 expr.value = error_mark_node;
5170 expr.original_code = ERROR_MARK;
5171 break;
5173 t1 = c_parser_type_name (parser);
5174 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5175 "expected %<)%>");
5176 if (t1 == NULL)
5178 expr.value = error_mark_node;
5179 expr.original_code = ERROR_MARK;
5181 else
5183 expr.value = build_va_arg (e1.value, groktypename (t1));
5184 expr.original_code = ERROR_MARK;
5186 break;
5187 case RID_OFFSETOF:
5188 c_parser_consume_token (parser);
5189 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5191 expr.value = error_mark_node;
5192 expr.original_code = ERROR_MARK;
5193 break;
5195 t1 = c_parser_type_name (parser);
5196 if (t1 == NULL)
5198 expr.value = error_mark_node;
5199 expr.original_code = ERROR_MARK;
5200 break;
5202 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5204 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5205 expr.value = error_mark_node;
5206 expr.original_code = ERROR_MARK;
5207 break;
5210 tree type = groktypename (t1);
5211 tree offsetof_ref;
5212 if (type == error_mark_node)
5213 offsetof_ref = error_mark_node;
5214 else
5215 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
5216 /* Parse the second argument to __builtin_offsetof. We
5217 must have one identifier, and beyond that we want to
5218 accept sub structure and sub array references. */
5219 if (c_parser_next_token_is (parser, CPP_NAME))
5221 offsetof_ref = build_component_ref
5222 (offsetof_ref, c_parser_peek_token (parser)->value);
5223 c_parser_consume_token (parser);
5224 while (c_parser_next_token_is (parser, CPP_DOT)
5225 || c_parser_next_token_is (parser,
5226 CPP_OPEN_SQUARE))
5228 if (c_parser_next_token_is (parser, CPP_DOT))
5230 c_parser_consume_token (parser);
5231 if (c_parser_next_token_is_not (parser,
5232 CPP_NAME))
5234 c_parser_error (parser, "expected identifier");
5235 break;
5237 offsetof_ref = build_component_ref
5238 (offsetof_ref,
5239 c_parser_peek_token (parser)->value);
5240 c_parser_consume_token (parser);
5242 else
5244 tree idx;
5245 c_parser_consume_token (parser);
5246 idx = c_parser_expression (parser).value;
5247 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5248 "expected %<]%>");
5249 offsetof_ref = build_array_ref (offsetof_ref, idx);
5253 else
5254 c_parser_error (parser, "expected identifier");
5255 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5256 "expected %<)%>");
5257 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
5258 expr.original_code = ERROR_MARK;
5260 break;
5261 case RID_CHOOSE_EXPR:
5262 c_parser_consume_token (parser);
5263 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5265 expr.value = error_mark_node;
5266 expr.original_code = ERROR_MARK;
5267 break;
5269 e1 = c_parser_expr_no_commas (parser, NULL);
5270 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5272 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5273 expr.value = error_mark_node;
5274 expr.original_code = ERROR_MARK;
5275 break;
5277 e2 = c_parser_expr_no_commas (parser, NULL);
5278 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5280 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5281 expr.value = error_mark_node;
5282 expr.original_code = ERROR_MARK;
5283 break;
5285 e3 = c_parser_expr_no_commas (parser, NULL);
5286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5287 "expected %<)%>");
5289 tree c;
5291 c = fold (e1.value);
5292 if (TREE_CODE (c) != INTEGER_CST)
5293 error ("first argument to %<__builtin_choose_expr%> not"
5294 " a constant");
5295 expr = integer_zerop (c) ? e3 : e2;
5297 break;
5298 case RID_TYPES_COMPATIBLE_P:
5299 c_parser_consume_token (parser);
5300 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5302 expr.value = error_mark_node;
5303 expr.original_code = ERROR_MARK;
5304 break;
5306 t1 = c_parser_type_name (parser);
5307 if (t1 == NULL)
5309 expr.value = error_mark_node;
5310 expr.original_code = ERROR_MARK;
5311 break;
5313 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
5315 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5316 expr.value = error_mark_node;
5317 expr.original_code = ERROR_MARK;
5318 break;
5320 t2 = c_parser_type_name (parser);
5321 if (t2 == NULL)
5323 expr.value = error_mark_node;
5324 expr.original_code = ERROR_MARK;
5325 break;
5327 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5328 "expected %<)%>");
5330 tree e1, e2;
5332 e1 = TYPE_MAIN_VARIANT (groktypename (t1));
5333 e2 = TYPE_MAIN_VARIANT (groktypename (t2));
5335 expr.value = comptypes (e1, e2)
5336 ? build_int_cst (NULL_TREE, 1)
5337 : build_int_cst (NULL_TREE, 0);
5338 expr.original_code = ERROR_MARK;
5340 break;
5341 case RID_AT_SELECTOR:
5342 gcc_assert (c_dialect_objc ());
5343 c_parser_consume_token (parser);
5344 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5346 expr.value = error_mark_node;
5347 expr.original_code = ERROR_MARK;
5348 break;
5351 tree sel = c_parser_objc_selector_arg (parser);
5352 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5353 "expected %<)%>");
5354 expr.value = objc_build_selector_expr (sel);
5355 expr.original_code = ERROR_MARK;
5357 break;
5358 case RID_AT_PROTOCOL:
5359 gcc_assert (c_dialect_objc ());
5360 c_parser_consume_token (parser);
5361 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5363 expr.value = error_mark_node;
5364 expr.original_code = ERROR_MARK;
5365 break;
5367 if (c_parser_next_token_is_not (parser, CPP_NAME))
5369 c_parser_error (parser, "expected identifier");
5370 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5371 expr.value = error_mark_node;
5372 expr.original_code = ERROR_MARK;
5373 break;
5376 tree id = c_parser_peek_token (parser)->value;
5377 c_parser_consume_token (parser);
5378 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5379 "expected %<)%>");
5380 expr.value = objc_build_protocol_expr (id);
5381 expr.original_code = ERROR_MARK;
5383 break;
5384 case RID_AT_ENCODE:
5385 /* Extension to support C-structures in the archiver. */
5386 gcc_assert (c_dialect_objc ());
5387 c_parser_consume_token (parser);
5388 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5390 expr.value = error_mark_node;
5391 expr.original_code = ERROR_MARK;
5392 break;
5394 t1 = c_parser_type_name (parser);
5395 if (t1 == NULL)
5397 expr.value = error_mark_node;
5398 expr.original_code = ERROR_MARK;
5399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5400 break;
5402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5403 "expected %<)%>");
5405 tree type = groktypename (t1);
5406 expr.value = objc_build_encode_expr (type);
5407 expr.original_code = ERROR_MARK;
5409 break;
5410 default:
5411 c_parser_error (parser, "expected expression");
5412 expr.value = error_mark_node;
5413 expr.original_code = ERROR_MARK;
5414 break;
5416 break;
5417 case CPP_OPEN_SQUARE:
5418 if (c_dialect_objc ())
5420 tree receiver, args;
5421 c_parser_consume_token (parser);
5422 receiver = c_parser_objc_receiver (parser);
5423 args = c_parser_objc_message_args (parser);
5424 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5425 "expected %<]%>");
5426 expr.value = objc_build_message_expr (build_tree_list (receiver,
5427 args));
5428 expr.original_code = ERROR_MARK;
5429 break;
5431 /* Else fall through to report error. */
5432 default:
5433 c_parser_error (parser, "expected expression");
5434 expr.value = error_mark_node;
5435 expr.original_code = ERROR_MARK;
5436 break;
5438 return c_parser_postfix_expression_after_primary (parser, expr);
5441 /* Parse a postfix expression after a parenthesized type name: the
5442 brace-enclosed initializer of a compound literal, possibly followed
5443 by some postfix operators. This is separate because it is not
5444 possible to tell until after the type name whether a cast
5445 expression has a cast or a compound literal, or whether the operand
5446 of sizeof is a parenthesized type name or starts with a compound
5447 literal. */
5449 static struct c_expr
5450 c_parser_postfix_expression_after_paren_type (c_parser *parser,
5451 struct c_type_name *type_name)
5453 tree type;
5454 struct c_expr init;
5455 struct c_expr expr;
5456 start_init (NULL_TREE, NULL, 0);
5457 type = groktypename (type_name);
5458 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
5460 error ("compound literal has variable size");
5461 type = error_mark_node;
5463 init = c_parser_braced_init (parser, type, false);
5464 finish_init ();
5465 maybe_warn_string_init (type, init);
5467 if (pedantic && !flag_isoc99)
5468 pedwarn ("ISO C90 forbids compound literals");
5469 expr.value = build_compound_literal (type, init.value);
5470 expr.original_code = ERROR_MARK;
5471 return c_parser_postfix_expression_after_primary (parser, expr);
5474 /* Parse a postfix expression after the initial primary or compound
5475 literal; that is, parse a series of postfix operators. */
5477 static struct c_expr
5478 c_parser_postfix_expression_after_primary (c_parser *parser,
5479 struct c_expr expr)
5481 tree ident, idx, exprlist;
5482 while (true)
5484 switch (c_parser_peek_token (parser)->type)
5486 case CPP_OPEN_SQUARE:
5487 /* Array reference. */
5488 c_parser_consume_token (parser);
5489 idx = c_parser_expression (parser).value;
5490 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5491 "expected %<]%>");
5492 expr.value = build_array_ref (expr.value, idx);
5493 expr.original_code = ERROR_MARK;
5494 break;
5495 case CPP_OPEN_PAREN:
5496 /* Function call. */
5497 c_parser_consume_token (parser);
5498 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5499 exprlist = NULL_TREE;
5500 else
5501 exprlist = c_parser_expr_list (parser, true);
5502 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
5503 "expected %<)%>");
5504 expr.value = build_function_call (expr.value, exprlist);
5505 expr.original_code = ERROR_MARK;
5506 break;
5507 case CPP_DOT:
5508 /* Structure element reference. */
5509 c_parser_consume_token (parser);
5510 expr = default_function_array_conversion (expr);
5511 if (c_parser_next_token_is (parser, CPP_NAME))
5512 ident = c_parser_peek_token (parser)->value;
5513 else
5515 c_parser_error (parser, "expected identifier");
5516 expr.value = error_mark_node;
5517 expr.original_code = ERROR_MARK;
5518 return expr;
5520 c_parser_consume_token (parser);
5521 expr.value = build_component_ref (expr.value, ident);
5522 expr.original_code = ERROR_MARK;
5523 break;
5524 case CPP_DEREF:
5525 /* Structure element reference. */
5526 c_parser_consume_token (parser);
5527 expr = default_function_array_conversion (expr);
5528 if (c_parser_next_token_is (parser, CPP_NAME))
5529 ident = c_parser_peek_token (parser)->value;
5530 else
5532 c_parser_error (parser, "expected identifier");
5533 expr.value = error_mark_node;
5534 expr.original_code = ERROR_MARK;
5535 return expr;
5537 c_parser_consume_token (parser);
5538 expr.value = build_component_ref (build_indirect_ref (expr.value,
5539 "->"), ident);
5540 expr.original_code = ERROR_MARK;
5541 break;
5542 case CPP_PLUS_PLUS:
5543 /* Postincrement. */
5544 c_parser_consume_token (parser);
5545 expr = default_function_array_conversion (expr);
5546 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0);
5547 expr.original_code = ERROR_MARK;
5548 break;
5549 case CPP_MINUS_MINUS:
5550 /* Postdecrement. */
5551 c_parser_consume_token (parser);
5552 expr = default_function_array_conversion (expr);
5553 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0);
5554 expr.original_code = ERROR_MARK;
5555 break;
5556 default:
5557 return expr;
5562 /* Parse an expression (C90 6.3.17, C99 6.5.17).
5564 expression:
5565 assignment-expression
5566 expression , assignment-expression
5569 static struct c_expr
5570 c_parser_expression (c_parser *parser)
5572 struct c_expr expr;
5573 expr = c_parser_expr_no_commas (parser, NULL);
5574 while (c_parser_next_token_is (parser, CPP_COMMA))
5576 struct c_expr next;
5577 c_parser_consume_token (parser);
5578 next = c_parser_expr_no_commas (parser, NULL);
5579 next = default_function_array_conversion (next);
5580 expr.value = build_compound_expr (expr.value, next.value);
5581 expr.original_code = COMPOUND_EXPR;
5583 return expr;
5586 /* Parse an expression and convert functions or arrays to
5587 pointers. */
5589 static struct c_expr
5590 c_parser_expression_conv (c_parser *parser)
5592 struct c_expr expr;
5593 expr = c_parser_expression (parser);
5594 expr = default_function_array_conversion (expr);
5595 return expr;
5598 /* Parse a non-empty list of expressions. If CONVERT_P, convert
5599 functions and arrays to pointers.
5601 nonempty-expr-list:
5602 assignment-expression
5603 nonempty-expr-list , assignment-expression
5606 static tree
5607 c_parser_expr_list (c_parser *parser, bool convert_p)
5609 struct c_expr expr;
5610 tree ret, cur;
5611 expr = c_parser_expr_no_commas (parser, NULL);
5612 if (convert_p)
5613 expr = default_function_array_conversion (expr);
5614 ret = cur = build_tree_list (NULL_TREE, expr.value);
5615 while (c_parser_next_token_is (parser, CPP_COMMA))
5617 c_parser_consume_token (parser);
5618 expr = c_parser_expr_no_commas (parser, NULL);
5619 if (convert_p)
5620 expr = default_function_array_conversion (expr);
5621 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value);
5623 return ret;
5627 /* Parse Objective-C-specific constructs. */
5629 /* Parse an objc-class-definition.
5631 objc-class-definition:
5632 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
5633 objc-class-instance-variables[opt] objc-methodprotolist @end
5634 @implementation identifier objc-superclass[opt]
5635 objc-class-instance-variables[opt]
5636 @interface identifier ( identifier ) objc-protocol-refs[opt]
5637 objc-methodprotolist @end
5638 @implementation identifier ( identifier )
5640 objc-superclass:
5641 : identifier
5643 "@interface identifier (" must start "@interface identifier (
5644 identifier ) ...": objc-methodprotolist in the first production may
5645 not start with a parenthesized identifier as a declarator of a data
5646 definition with no declaration specifiers if the objc-superclass,
5647 objc-protocol-refs and objc-class-instance-variables are omitted. */
5649 static void
5650 c_parser_objc_class_definition (c_parser *parser)
5652 bool iface_p;
5653 tree id1;
5654 tree superclass;
5655 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
5656 iface_p = true;
5657 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
5658 iface_p = false;
5659 else
5660 gcc_unreachable ();
5661 c_parser_consume_token (parser);
5662 if (c_parser_next_token_is_not (parser, CPP_NAME))
5664 c_parser_error (parser, "expected identifier");
5665 return;
5667 id1 = c_parser_peek_token (parser)->value;
5668 c_parser_consume_token (parser);
5669 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
5671 tree id2;
5672 tree proto = NULL_TREE;
5673 c_parser_consume_token (parser);
5674 if (c_parser_next_token_is_not (parser, CPP_NAME))
5676 c_parser_error (parser, "expected identifier");
5677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5678 return;
5680 id2 = c_parser_peek_token (parser)->value;
5681 c_parser_consume_token (parser);
5682 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5683 if (!iface_p)
5685 objc_start_category_implementation (id1, id2);
5686 return;
5688 if (c_parser_next_token_is (parser, CPP_LESS))
5689 proto = c_parser_objc_protocol_refs (parser);
5690 objc_start_category_interface (id1, id2, proto);
5691 c_parser_objc_methodprotolist (parser);
5692 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5693 objc_finish_interface ();
5694 return;
5696 if (c_parser_next_token_is (parser, CPP_COLON))
5698 c_parser_consume_token (parser);
5699 if (c_parser_next_token_is_not (parser, CPP_NAME))
5701 c_parser_error (parser, "expected identifier");
5702 return;
5704 superclass = c_parser_peek_token (parser)->value;
5705 c_parser_consume_token (parser);
5707 else
5708 superclass = NULL_TREE;
5709 if (iface_p)
5711 tree proto = NULL_TREE;
5712 if (c_parser_next_token_is (parser, CPP_LESS))
5713 proto = c_parser_objc_protocol_refs (parser);
5714 objc_start_class_interface (id1, superclass, proto);
5716 else
5717 objc_start_class_implementation (id1, superclass);
5718 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5719 c_parser_objc_class_instance_variables (parser);
5720 if (iface_p)
5722 objc_continue_interface ();
5723 c_parser_objc_methodprotolist (parser);
5724 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5725 objc_finish_interface ();
5727 else
5729 objc_continue_implementation ();
5730 return;
5734 /* Parse objc-class-instance-variables.
5736 objc-class-instance-variables:
5737 { objc-instance-variable-decl-list[opt] }
5739 objc-instance-variable-decl-list:
5740 objc-visibility-spec
5741 objc-instance-variable-decl ;
5743 objc-instance-variable-decl-list objc-visibility-spec
5744 objc-instance-variable-decl-list objc-instance-variable-decl ;
5745 objc-instance-variable-decl-list ;
5747 objc-visibility-spec:
5748 @private
5749 @protected
5750 @public
5752 objc-instance-variable-decl:
5753 struct-declaration
5756 static void
5757 c_parser_objc_class_instance_variables (c_parser *parser)
5759 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
5760 c_parser_consume_token (parser);
5761 while (c_parser_next_token_is_not (parser, CPP_EOF))
5763 tree decls;
5764 /* Parse any stray semicolon. */
5765 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5767 if (pedantic)
5768 pedwarn ("extra semicolon in struct or union specified");
5769 c_parser_consume_token (parser);
5770 continue;
5772 /* Stop if at the end of the instance variables. */
5773 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5775 c_parser_consume_token (parser);
5776 break;
5778 /* Parse any objc-visibility-spec. */
5779 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
5781 c_parser_consume_token (parser);
5782 objc_set_visibility (2);
5783 continue;
5785 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
5787 c_parser_consume_token (parser);
5788 objc_set_visibility (0);
5789 continue;
5791 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
5793 c_parser_consume_token (parser);
5794 objc_set_visibility (1);
5795 continue;
5797 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5799 c_parser_pragma (parser, pragma_external);
5800 continue;
5803 /* Parse some comma-separated declarations. */
5804 decls = c_parser_struct_declaration (parser);
5806 /* Comma-separated instance variables are chained together in
5807 reverse order; add them one by one. */
5808 tree ivar = nreverse (decls);
5809 for (; ivar; ivar = TREE_CHAIN (ivar))
5810 objc_add_instance_variable (copy_node (ivar));
5812 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5816 /* Parse an objc-class-declaration.
5818 objc-class-declaration:
5819 @class identifier-list ;
5822 static void
5823 c_parser_objc_class_declaration (c_parser *parser)
5825 tree list = NULL_TREE;
5826 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
5827 c_parser_consume_token (parser);
5828 /* Any identifiers, including those declared as type names, are OK
5829 here. */
5830 while (true)
5832 tree id;
5833 if (c_parser_next_token_is_not (parser, CPP_NAME))
5835 c_parser_error (parser, "expected identifier");
5836 break;
5838 id = c_parser_peek_token (parser)->value;
5839 list = chainon (list, build_tree_list (NULL_TREE, id));
5840 c_parser_consume_token (parser);
5841 if (c_parser_next_token_is (parser, CPP_COMMA))
5842 c_parser_consume_token (parser);
5843 else
5844 break;
5846 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5847 objc_declare_class (list);
5850 /* Parse an objc-alias-declaration.
5852 objc-alias-declaration:
5853 @compatibility_alias identifier identifier ;
5856 static void
5857 c_parser_objc_alias_declaration (c_parser *parser)
5859 tree id1, id2;
5860 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
5861 c_parser_consume_token (parser);
5862 if (c_parser_next_token_is_not (parser, CPP_NAME))
5864 c_parser_error (parser, "expected identifier");
5865 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5866 return;
5868 id1 = c_parser_peek_token (parser)->value;
5869 c_parser_consume_token (parser);
5870 if (c_parser_next_token_is_not (parser, CPP_NAME))
5872 c_parser_error (parser, "expected identifier");
5873 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
5874 return;
5876 id2 = c_parser_peek_token (parser)->value;
5877 c_parser_consume_token (parser);
5878 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5879 objc_declare_alias (id1, id2);
5882 /* Parse an objc-protocol-definition.
5884 objc-protocol-definition:
5885 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
5886 @protocol identifier-list ;
5888 "@protocol identifier ;" should be resolved as "@protocol
5889 identifier-list ;": objc-methodprotolist may not start with a
5890 semicolon in the first alternative if objc-protocol-refs are
5891 omitted. */
5893 static void
5894 c_parser_objc_protocol_definition (c_parser *parser)
5896 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
5897 c_parser_consume_token (parser);
5898 if (c_parser_next_token_is_not (parser, CPP_NAME))
5900 c_parser_error (parser, "expected identifier");
5901 return;
5903 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
5904 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
5906 tree list = NULL_TREE;
5907 /* Any identifiers, including those declared as type names, are
5908 OK here. */
5909 while (true)
5911 tree id;
5912 if (c_parser_next_token_is_not (parser, CPP_NAME))
5914 c_parser_error (parser, "expected identifier");
5915 break;
5917 id = c_parser_peek_token (parser)->value;
5918 list = chainon (list, build_tree_list (NULL_TREE, id));
5919 c_parser_consume_token (parser);
5920 if (c_parser_next_token_is (parser, CPP_COMMA))
5921 c_parser_consume_token (parser);
5922 else
5923 break;
5925 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5926 objc_declare_protocols (list);
5928 else
5930 tree id = c_parser_peek_token (parser)->value;
5931 tree proto = NULL_TREE;
5932 c_parser_consume_token (parser);
5933 if (c_parser_next_token_is (parser, CPP_LESS))
5934 proto = c_parser_objc_protocol_refs (parser);
5935 parser->objc_pq_context = true;
5936 objc_start_protocol (id, proto);
5937 c_parser_objc_methodprotolist (parser);
5938 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
5939 parser->objc_pq_context = false;
5940 objc_finish_interface ();
5944 /* Parse an objc-method-type.
5946 objc-method-type:
5951 static enum tree_code
5952 c_parser_objc_method_type (c_parser *parser)
5954 switch (c_parser_peek_token (parser)->type)
5956 case CPP_PLUS:
5957 c_parser_consume_token (parser);
5958 return PLUS_EXPR;
5959 case CPP_MINUS:
5960 c_parser_consume_token (parser);
5961 return MINUS_EXPR;
5962 default:
5963 gcc_unreachable ();
5967 /* Parse an objc-method-definition.
5969 objc-method-definition:
5970 objc-method-type objc-method-decl ;[opt] compound-statement
5973 static void
5974 c_parser_objc_method_definition (c_parser *parser)
5976 enum tree_code type = c_parser_objc_method_type (parser);
5977 tree decl;
5978 objc_set_method_type (type);
5979 parser->objc_pq_context = true;
5980 decl = c_parser_objc_method_decl (parser);
5981 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5983 c_parser_consume_token (parser);
5984 if (pedantic)
5985 pedwarn ("extra semicolon in method definition specified");
5987 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5989 c_parser_error (parser, "expected %<{%>");
5990 return;
5992 parser->objc_pq_context = false;
5993 objc_start_method_definition (decl);
5994 add_stmt (c_parser_compound_statement (parser));
5995 objc_finish_method_definition (current_function_decl);
5998 /* Parse an objc-methodprotolist.
6000 objc-methodprotolist:
6001 empty
6002 objc-methodprotolist objc-methodproto
6003 objc-methodprotolist declaration
6004 objc-methodprotolist ;
6006 The declaration is a data definition, which may be missing
6007 declaration specifiers under the same rules and diagnostics as
6008 other data definitions outside functions, and the stray semicolon
6009 is diagnosed the same way as a stray semicolon outside a
6010 function. */
6012 static void
6013 c_parser_objc_methodprotolist (c_parser *parser)
6015 while (true)
6017 /* The list is terminated by @end. */
6018 switch (c_parser_peek_token (parser)->type)
6020 case CPP_SEMICOLON:
6021 if (pedantic)
6022 pedwarn ("ISO C does not allow extra %<;%> outside of a function");
6023 c_parser_consume_token (parser);
6024 break;
6025 case CPP_PLUS:
6026 case CPP_MINUS:
6027 c_parser_objc_methodproto (parser);
6028 break;
6029 case CPP_PRAGMA:
6030 c_parser_pragma (parser, pragma_external);
6031 break;
6032 case CPP_EOF:
6033 return;
6034 default:
6035 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
6036 return;
6037 c_parser_declaration_or_fndef (parser, false, true, false, true);
6038 break;
6043 /* Parse an objc-methodproto.
6045 objc-methodproto:
6046 objc-method-type objc-method-decl ;
6049 static void
6050 c_parser_objc_methodproto (c_parser *parser)
6052 enum tree_code type = c_parser_objc_method_type (parser);
6053 tree decl;
6054 objc_set_method_type (type);
6055 /* Remember protocol qualifiers in prototypes. */
6056 parser->objc_pq_context = true;
6057 decl = c_parser_objc_method_decl (parser);
6058 /* Forget protocol qualifiers here. */
6059 parser->objc_pq_context = false;
6060 objc_add_method_declaration (decl);
6061 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6064 /* Parse an objc-method-decl.
6066 objc-method-decl:
6067 ( objc-type-name ) objc-selector
6068 objc-selector
6069 ( objc-type-name ) objc-keyword-selector objc-optparmlist
6070 objc-keyword-selector objc-optparmlist
6072 objc-keyword-selector:
6073 objc-keyword-decl
6074 objc-keyword-selector objc-keyword-decl
6076 objc-keyword-decl:
6077 objc-selector : ( objc-type-name ) identifier
6078 objc-selector : identifier
6079 : ( objc-type-name ) identifier
6080 : identifier
6082 objc-optparmlist:
6083 objc-optparms objc-optellipsis
6085 objc-optparms:
6086 empty
6087 objc-opt-parms , parameter-declaration
6089 objc-optellipsis:
6090 empty
6091 , ...
6094 static tree
6095 c_parser_objc_method_decl (c_parser *parser)
6097 tree type = NULL_TREE;
6098 tree sel;
6099 tree parms = NULL_TREE;
6100 bool ellipsis = false;
6102 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6104 c_parser_consume_token (parser);
6105 type = c_parser_objc_type_name (parser);
6106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6108 sel = c_parser_objc_selector (parser);
6109 /* If there is no selector, or a colon follows, we have an
6110 objc-keyword-selector. If there is a selector, and a colon does
6111 not follow, that selector ends the objc-method-decl. */
6112 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
6114 tree tsel = sel;
6115 tree list = NULL_TREE;
6116 while (true)
6118 tree atype = NULL_TREE, id, keyworddecl;
6119 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6120 break;
6121 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6123 c_parser_consume_token (parser);
6124 atype = c_parser_objc_type_name (parser);
6125 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6126 "expected %<)%>");
6128 if (c_parser_next_token_is_not (parser, CPP_NAME))
6130 c_parser_error (parser, "expected identifier");
6131 return error_mark_node;
6133 id = c_parser_peek_token (parser)->value;
6134 c_parser_consume_token (parser);
6135 keyworddecl = objc_build_keyword_decl (tsel, atype, id);
6136 list = chainon (list, keyworddecl);
6137 tsel = c_parser_objc_selector (parser);
6138 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
6139 break;
6141 /* Parse the optional parameter list. Optional Objective-C
6142 method parameters follow the C syntax, and may include '...'
6143 to denote a variable number of arguments. */
6144 parms = make_node (TREE_LIST);
6145 while (c_parser_next_token_is (parser, CPP_COMMA))
6147 struct c_parm *parm;
6148 c_parser_consume_token (parser);
6149 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
6151 ellipsis = true;
6152 c_parser_consume_token (parser);
6153 break;
6155 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6156 if (parm == NULL)
6157 break;
6158 parms = chainon (parms,
6159 build_tree_list (NULL_TREE, grokparm (parm)));
6161 sel = list;
6163 return objc_build_method_signature (type, sel, parms, ellipsis);
6166 /* Parse an objc-type-name.
6168 objc-type-name:
6169 objc-type-qualifiers[opt] type-name
6170 objc-type-qualifiers[opt]
6172 objc-type-qualifiers:
6173 objc-type-qualifier
6174 objc-type-qualifiers objc-type-qualifier
6176 objc-type-qualifier: one of
6177 in out inout bycopy byref oneway
6180 static tree
6181 c_parser_objc_type_name (c_parser *parser)
6183 tree quals = NULL_TREE;
6184 struct c_type_name *typename = NULL;
6185 tree type = NULL_TREE;
6186 while (true)
6188 c_token *token = c_parser_peek_token (parser);
6189 if (token->type == CPP_KEYWORD
6190 && (token->keyword == RID_IN
6191 || token->keyword == RID_OUT
6192 || token->keyword == RID_INOUT
6193 || token->keyword == RID_BYCOPY
6194 || token->keyword == RID_BYREF
6195 || token->keyword == RID_ONEWAY))
6197 quals = chainon (quals, build_tree_list (NULL_TREE, token->value));
6198 c_parser_consume_token (parser);
6200 else
6201 break;
6203 if (c_parser_next_token_starts_typename (parser))
6204 typename = c_parser_type_name (parser);
6205 if (typename)
6206 type = groktypename (typename);
6207 return build_tree_list (quals, type);
6210 /* Parse objc-protocol-refs.
6212 objc-protocol-refs:
6213 < identifier-list >
6216 static tree
6217 c_parser_objc_protocol_refs (c_parser *parser)
6219 tree list = NULL_TREE;
6220 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
6221 c_parser_consume_token (parser);
6222 /* Any identifiers, including those declared as type names, are OK
6223 here. */
6224 while (true)
6226 tree id;
6227 if (c_parser_next_token_is_not (parser, CPP_NAME))
6229 c_parser_error (parser, "expected identifier");
6230 break;
6232 id = c_parser_peek_token (parser)->value;
6233 list = chainon (list, build_tree_list (NULL_TREE, id));
6234 c_parser_consume_token (parser);
6235 if (c_parser_next_token_is (parser, CPP_COMMA))
6236 c_parser_consume_token (parser);
6237 else
6238 break;
6240 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
6241 return list;
6244 /* Parse an objc-try-catch-statement.
6246 objc-try-catch-statement:
6247 @try compound-statement objc-catch-list[opt]
6248 @try compound-statement objc-catch-list[opt] @finally compound-statement
6250 objc-catch-list:
6251 @catch ( parameter-declaration ) compound-statement
6252 objc-catch-list @catch ( parameter-declaration ) compound-statement
6255 static void
6256 c_parser_objc_try_catch_statement (c_parser *parser)
6258 location_t loc;
6259 tree stmt;
6260 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
6261 c_parser_consume_token (parser);
6262 loc = c_parser_peek_token (parser)->location;
6263 stmt = c_parser_compound_statement (parser);
6264 objc_begin_try_stmt (loc, stmt);
6265 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
6267 struct c_parm *parm;
6268 c_parser_consume_token (parser);
6269 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6270 break;
6271 parm = c_parser_parameter_declaration (parser, NULL_TREE);
6272 if (parm == NULL)
6274 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6275 break;
6277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6278 objc_begin_catch_clause (grokparm (parm));
6279 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
6280 c_parser_compound_statement_nostart (parser);
6281 objc_finish_catch_clause ();
6283 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
6285 location_t finloc;
6286 tree finstmt;
6287 c_parser_consume_token (parser);
6288 finloc = c_parser_peek_token (parser)->location;
6289 finstmt = c_parser_compound_statement (parser);
6290 objc_build_finally_clause (finloc, finstmt);
6292 objc_finish_try_stmt ();
6295 /* Parse an objc-synchronized-statement.
6297 objc-synchronized-statement:
6298 @synchronized ( expression ) compound-statement
6301 static void
6302 c_parser_objc_synchronized_statement (c_parser *parser)
6304 location_t loc;
6305 tree expr, stmt;
6306 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
6307 c_parser_consume_token (parser);
6308 loc = c_parser_peek_token (parser)->location;
6309 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6311 expr = c_parser_expression (parser).value;
6312 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6314 else
6315 expr = error_mark_node;
6316 stmt = c_parser_compound_statement (parser);
6317 objc_build_synchronized (loc, expr, stmt);
6320 /* Parse an objc-selector; return NULL_TREE without an error if the
6321 next token is not an objc-selector.
6323 objc-selector:
6324 identifier
6325 one of
6326 enum struct union if else while do for switch case default
6327 break continue return goto asm sizeof typeof __alignof
6328 unsigned long const short volatile signed restrict _Complex
6329 in out inout bycopy byref oneway int char float double void _Bool
6331 ??? Why this selection of keywords but not, for example, storage
6332 class specifiers? */
6334 static tree
6335 c_parser_objc_selector (c_parser *parser)
6337 c_token *token = c_parser_peek_token (parser);
6338 tree value = token->value;
6339 if (token->type == CPP_NAME)
6341 c_parser_consume_token (parser);
6342 return value;
6344 if (token->type != CPP_KEYWORD)
6345 return NULL_TREE;
6346 switch (token->keyword)
6348 case RID_ENUM:
6349 case RID_STRUCT:
6350 case RID_UNION:
6351 case RID_IF:
6352 case RID_ELSE:
6353 case RID_WHILE:
6354 case RID_DO:
6355 case RID_FOR:
6356 case RID_SWITCH:
6357 case RID_CASE:
6358 case RID_DEFAULT:
6359 case RID_BREAK:
6360 case RID_CONTINUE:
6361 case RID_RETURN:
6362 case RID_GOTO:
6363 case RID_ASM:
6364 case RID_SIZEOF:
6365 case RID_TYPEOF:
6366 case RID_ALIGNOF:
6367 case RID_UNSIGNED:
6368 case RID_LONG:
6369 case RID_CONST:
6370 case RID_SHORT:
6371 case RID_VOLATILE:
6372 case RID_SIGNED:
6373 case RID_RESTRICT:
6374 case RID_COMPLEX:
6375 case RID_IN:
6376 case RID_OUT:
6377 case RID_INOUT:
6378 case RID_BYCOPY:
6379 case RID_BYREF:
6380 case RID_ONEWAY:
6381 case RID_INT:
6382 case RID_CHAR:
6383 case RID_FLOAT:
6384 case RID_DOUBLE:
6385 case RID_VOID:
6386 case RID_BOOL:
6387 c_parser_consume_token (parser);
6388 return value;
6389 default:
6390 return NULL_TREE;
6394 /* Parse an objc-selector-arg.
6396 objc-selector-arg:
6397 objc-selector
6398 objc-keywordname-list
6400 objc-keywordname-list:
6401 objc-keywordname
6402 objc-keywordname-list objc-keywordname
6404 objc-keywordname:
6405 objc-selector :
6409 static tree
6410 c_parser_objc_selector_arg (c_parser *parser)
6412 tree sel = c_parser_objc_selector (parser);
6413 tree list = NULL_TREE;
6414 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6415 return sel;
6416 while (true)
6418 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6419 return list;
6420 list = chainon (list, build_tree_list (sel, NULL_TREE));
6421 sel = c_parser_objc_selector (parser);
6422 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6423 break;
6425 return list;
6428 /* Parse an objc-receiver.
6430 objc-receiver:
6431 expression
6432 class-name
6433 type-name
6436 static tree
6437 c_parser_objc_receiver (c_parser *parser)
6439 if (c_parser_peek_token (parser)->type == CPP_NAME
6440 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
6441 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
6443 tree id = c_parser_peek_token (parser)->value;
6444 c_parser_consume_token (parser);
6445 return objc_get_class_reference (id);
6447 return c_parser_expression (parser).value;
6450 /* Parse objc-message-args.
6452 objc-message-args:
6453 objc-selector
6454 objc-keywordarg-list
6456 objc-keywordarg-list:
6457 objc-keywordarg
6458 objc-keywordarg-list objc-keywordarg
6460 objc-keywordarg:
6461 objc-selector : objc-keywordexpr
6462 : objc-keywordexpr
6465 static tree
6466 c_parser_objc_message_args (c_parser *parser)
6468 tree sel = c_parser_objc_selector (parser);
6469 tree list = NULL_TREE;
6470 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
6471 return sel;
6472 while (true)
6474 tree keywordexpr;
6475 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6476 return list;
6477 keywordexpr = c_parser_objc_keywordexpr (parser);
6478 list = chainon (list, build_tree_list (sel, keywordexpr));
6479 sel = c_parser_objc_selector (parser);
6480 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
6481 break;
6483 return list;
6486 /* Parse an objc-keywordexpr.
6488 objc-keywordexpr:
6489 nonempty-expr-list
6492 static tree
6493 c_parser_objc_keywordexpr (c_parser *parser)
6495 tree list = c_parser_expr_list (parser, true);
6496 if (TREE_CHAIN (list) == NULL_TREE)
6498 /* Just return the expression, remove a level of
6499 indirection. */
6500 return TREE_VALUE (list);
6502 else
6504 /* We have a comma expression, we will collapse later. */
6505 return list;
6510 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
6511 should be considered, statements. ALLOW_STMT is true if we're within
6512 the context of a function and such pragmas are to be allowed. Returns
6513 true if we actually parsed such a pragma. */
6515 static bool
6516 c_parser_pragma (c_parser *parser, enum pragma_context context)
6518 unsigned int id;
6520 id = c_parser_peek_token (parser)->pragma_kind;
6521 gcc_assert (id != PRAGMA_NONE);
6523 switch (id)
6525 case PRAGMA_OMP_BARRIER:
6526 if (context != pragma_compound)
6528 if (context == pragma_stmt)
6529 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
6530 "used in compound statements");
6531 goto bad_stmt;
6533 c_parser_omp_barrier (parser);
6534 return false;
6536 case PRAGMA_OMP_FLUSH:
6537 if (context != pragma_compound)
6539 if (context == pragma_stmt)
6540 c_parser_error (parser, "%<#pragma omp flush%> may only be "
6541 "used in compound statements");
6542 goto bad_stmt;
6544 c_parser_omp_flush (parser);
6545 return false;
6547 case PRAGMA_OMP_THREADPRIVATE:
6548 c_parser_omp_threadprivate (parser);
6549 return false;
6551 case PRAGMA_OMP_SECTION:
6552 error ("%<#pragma omp section%> may only be used in "
6553 "%<#pragma omp sections%> construct");
6554 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6555 return false;
6557 case PRAGMA_GCC_PCH_PREPROCESS:
6558 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
6559 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6560 return false;
6562 default:
6563 if (id < PRAGMA_FIRST_EXTERNAL)
6565 if (context == pragma_external)
6567 bad_stmt:
6568 c_parser_error (parser, "expected declaration specifiers");
6569 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
6570 return false;
6572 c_parser_omp_construct (parser);
6573 return true;
6575 break;
6578 c_parser_consume_pragma (parser);
6579 c_invoke_pragma_handler (id);
6581 /* Skip to EOL, but suppress any error message. Those will have been
6582 generated by the handler routine through calling error, as opposed
6583 to calling c_parser_error. */
6584 parser->error = true;
6585 c_parser_skip_to_pragma_eol (parser);
6587 return false;
6590 /* The interface the pragma parsers have to the lexer. */
6592 enum cpp_ttype
6593 pragma_lex (tree *value)
6595 c_token *tok = c_parser_peek_token (the_parser);
6596 enum cpp_ttype ret = tok->type;
6598 *value = tok->value;
6599 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
6600 ret = CPP_EOF;
6601 else
6603 if (ret == CPP_KEYWORD)
6604 ret = CPP_NAME;
6605 c_parser_consume_token (the_parser);
6608 return ret;
6611 static void
6612 c_parser_pragma_pch_preprocess (c_parser *parser)
6614 tree name = NULL;
6616 c_parser_consume_pragma (parser);
6617 if (c_parser_next_token_is (parser, CPP_STRING))
6619 name = c_parser_peek_token (parser)->value;
6620 c_parser_consume_token (parser);
6622 else
6623 c_parser_error (parser, "expected string literal");
6624 c_parser_skip_to_pragma_eol (parser);
6626 if (name)
6627 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
6630 /* OpenMP 2.5 parsing routines. */
6632 /* Returns name of the next clause.
6633 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
6634 the token is not consumed. Otherwise appropriate pragma_omp_clause is
6635 returned and the token is consumed. */
6637 static pragma_omp_clause
6638 c_parser_omp_clause_name (c_parser *parser)
6640 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
6642 if (c_parser_next_token_is_keyword (parser, RID_IF))
6643 result = PRAGMA_OMP_CLAUSE_IF;
6644 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
6645 result = PRAGMA_OMP_CLAUSE_DEFAULT;
6646 else if (c_parser_next_token_is (parser, CPP_NAME))
6648 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6650 switch (p[0])
6652 case 'c':
6653 if (!strcmp ("copyin", p))
6654 result = PRAGMA_OMP_CLAUSE_COPYIN;
6655 else if (!strcmp ("copyprivate", p))
6656 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
6657 break;
6658 case 'f':
6659 if (!strcmp ("firstprivate", p))
6660 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
6661 break;
6662 case 'l':
6663 if (!strcmp ("lastprivate", p))
6664 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
6665 break;
6666 case 'n':
6667 if (!strcmp ("nowait", p))
6668 result = PRAGMA_OMP_CLAUSE_NOWAIT;
6669 else if (!strcmp ("num_threads", p))
6670 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
6671 break;
6672 case 'o':
6673 if (!strcmp ("ordered", p))
6674 result = PRAGMA_OMP_CLAUSE_ORDERED;
6675 break;
6676 case 'p':
6677 if (!strcmp ("private", p))
6678 result = PRAGMA_OMP_CLAUSE_PRIVATE;
6679 break;
6680 case 'r':
6681 if (!strcmp ("reduction", p))
6682 result = PRAGMA_OMP_CLAUSE_REDUCTION;
6683 break;
6684 case 's':
6685 if (!strcmp ("schedule", p))
6686 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
6687 else if (!strcmp ("shared", p))
6688 result = PRAGMA_OMP_CLAUSE_SHARED;
6689 break;
6693 if (result != PRAGMA_OMP_CLAUSE_NONE)
6694 c_parser_consume_token (parser);
6696 return result;
6699 /* Validate that a clause of the given type does not already exist. */
6701 static void
6702 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
6704 tree c;
6706 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
6707 if (OMP_CLAUSE_CODE (c) == code)
6709 error ("too many %qs clauses", name);
6710 break;
6714 /* OpenMP 2.5:
6715 variable-list:
6716 identifier
6717 variable-list , identifier
6719 If KIND is nonzero, create the appropriate node and install the decl
6720 in OMP_CLAUSE_DECL and add the node to the head of the list.
6722 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
6723 return the list created. */
6725 static tree
6726 c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind,
6727 tree list)
6729 if (c_parser_next_token_is_not (parser, CPP_NAME)
6730 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
6731 c_parser_error (parser, "expected identifier");
6733 while (c_parser_next_token_is (parser, CPP_NAME)
6734 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
6736 tree t = lookup_name (c_parser_peek_token (parser)->value);
6738 if (t == NULL_TREE)
6739 undeclared_variable (c_parser_peek_token (parser)->value,
6740 c_parser_peek_token (parser)->location);
6741 else if (t == error_mark_node)
6743 else if (kind != 0)
6745 tree u = build_omp_clause (kind);
6746 OMP_CLAUSE_DECL (u) = t;
6747 OMP_CLAUSE_CHAIN (u) = list;
6748 list = u;
6750 else
6751 list = tree_cons (t, NULL_TREE, list);
6753 c_parser_consume_token (parser);
6755 if (c_parser_next_token_is_not (parser, CPP_COMMA))
6756 break;
6758 c_parser_consume_token (parser);
6761 return list;
6764 /* Similarly, but expect leading and trailing parenthesis. This is a very
6765 common case for omp clauses. */
6767 static tree
6768 c_parser_omp_var_list_parens (c_parser *parser, enum tree_code kind, tree list)
6770 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6772 list = c_parser_omp_variable_list (parser, kind, list);
6773 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6775 return list;
6778 /* OpenMP 2.5:
6779 copyin ( variable-list ) */
6781 static tree
6782 c_parser_omp_clause_copyin (c_parser *parser, tree list)
6784 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
6787 /* OpenMP 2.5:
6788 copyprivate ( variable-list ) */
6790 static tree
6791 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
6793 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
6796 /* OpenMP 2.5:
6797 default ( shared | none ) */
6799 static tree
6800 c_parser_omp_clause_default (c_parser *parser, tree list)
6802 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
6803 tree c;
6805 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6806 return list;
6807 if (c_parser_next_token_is (parser, CPP_NAME))
6809 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
6811 switch (p[0])
6813 case 'n':
6814 if (strcmp ("none", p) != 0)
6815 goto invalid_kind;
6816 kind = OMP_CLAUSE_DEFAULT_NONE;
6817 break;
6819 case 's':
6820 if (strcmp ("shared", p) != 0)
6821 goto invalid_kind;
6822 kind = OMP_CLAUSE_DEFAULT_SHARED;
6823 break;
6825 default:
6826 goto invalid_kind;
6829 c_parser_consume_token (parser);
6831 else
6833 invalid_kind:
6834 c_parser_error (parser, "expected %<none%> or %<shared%>");
6836 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6838 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
6839 return list;
6841 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
6842 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
6843 OMP_CLAUSE_CHAIN (c) = list;
6844 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
6846 return c;
6849 /* OpenMP 2.5:
6850 firstprivate ( variable-list ) */
6852 static tree
6853 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
6855 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
6858 /* OpenMP 2.5:
6859 if ( expression ) */
6861 static tree
6862 c_parser_omp_clause_if (c_parser *parser, tree list)
6864 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6866 tree t = c_parser_paren_condition (parser);
6867 tree c;
6869 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
6871 c = build_omp_clause (OMP_CLAUSE_IF);
6872 OMP_CLAUSE_IF_EXPR (c) = t;
6873 OMP_CLAUSE_CHAIN (c) = list;
6874 list = c;
6876 else
6877 c_parser_error (parser, "expected %<(%>");
6879 return list;
6882 /* OpenMP 2.5:
6883 lastprivate ( variable-list ) */
6885 static tree
6886 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
6888 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
6891 /* OpenMP 2.5:
6892 nowait */
6894 static tree
6895 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6897 tree c;
6899 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
6901 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
6902 OMP_CLAUSE_CHAIN (c) = list;
6903 return c;
6906 /* OpenMP 2.5:
6907 num_threads ( expression ) */
6909 static tree
6910 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
6912 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6914 tree c, t = c_parser_expression (parser).value;
6916 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6918 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
6920 c_parser_error (parser, "expected integer expression");
6921 return list;
6924 /* Attempt to statically determine when the number isn't positive. */
6925 c = fold_build2 (LE_EXPR, boolean_type_node, t,
6926 build_int_cst (TREE_TYPE (t), 0));
6927 if (c == boolean_true_node)
6929 warning (0, "%<num_threads%> value must be positive");
6930 t = integer_one_node;
6933 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
6935 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
6936 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
6937 OMP_CLAUSE_CHAIN (c) = list;
6938 list = c;
6941 return list;
6944 /* OpenMP 2.5:
6945 ordered */
6947 static tree
6948 c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list)
6950 tree c;
6952 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
6954 c = build_omp_clause (OMP_CLAUSE_ORDERED);
6955 OMP_CLAUSE_CHAIN (c) = list;
6956 return c;
6959 /* OpenMP 2.5:
6960 private ( variable-list ) */
6962 static tree
6963 c_parser_omp_clause_private (c_parser *parser, tree list)
6965 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
6968 /* OpenMP 2.5:
6969 reduction ( reduction-operator : variable-list )
6971 reduction-operator:
6972 One of: + * - & ^ | && || */
6974 static tree
6975 c_parser_omp_clause_reduction (c_parser *parser, tree list)
6977 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6979 enum tree_code code;
6981 switch (c_parser_peek_token (parser)->type)
6983 case CPP_PLUS:
6984 code = PLUS_EXPR;
6985 break;
6986 case CPP_MULT:
6987 code = MULT_EXPR;
6988 break;
6989 case CPP_MINUS:
6990 code = MINUS_EXPR;
6991 break;
6992 case CPP_AND:
6993 code = BIT_AND_EXPR;
6994 break;
6995 case CPP_XOR:
6996 code = BIT_XOR_EXPR;
6997 break;
6998 case CPP_OR:
6999 code = BIT_IOR_EXPR;
7000 break;
7001 case CPP_AND_AND:
7002 code = TRUTH_ANDIF_EXPR;
7003 break;
7004 case CPP_OR_OR:
7005 code = TRUTH_ORIF_EXPR;
7006 break;
7007 default:
7008 c_parser_error (parser,
7009 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
7010 "%<^%>, %<|%>, %<&&%>, or %<||%>");
7011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7012 return list;
7014 c_parser_consume_token (parser);
7015 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7017 tree nl, c;
7019 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list);
7020 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
7021 OMP_CLAUSE_REDUCTION_CODE (c) = code;
7023 list = nl;
7025 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7027 return list;
7030 /* OpenMP 2.5:
7031 schedule ( schedule-kind )
7032 schedule ( schedule-kind , expression )
7034 schedule-kind:
7035 static | dynamic | guided | runtime
7038 static tree
7039 c_parser_omp_clause_schedule (c_parser *parser, tree list)
7041 tree c, t;
7043 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7044 return list;
7046 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
7048 if (c_parser_next_token_is (parser, CPP_NAME))
7050 tree kind = c_parser_peek_token (parser)->value;
7051 const char *p = IDENTIFIER_POINTER (kind);
7053 switch (p[0])
7055 case 'd':
7056 if (strcmp ("dynamic", p) != 0)
7057 goto invalid_kind;
7058 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
7059 break;
7061 case 'g':
7062 if (strcmp ("guided", p) != 0)
7063 goto invalid_kind;
7064 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
7065 break;
7067 case 'r':
7068 if (strcmp ("runtime", p) != 0)
7069 goto invalid_kind;
7070 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
7071 break;
7073 default:
7074 goto invalid_kind;
7077 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
7078 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
7079 else
7080 goto invalid_kind;
7082 c_parser_consume_token (parser);
7083 if (c_parser_next_token_is (parser, CPP_COMMA))
7085 c_parser_consume_token (parser);
7087 t = c_parser_expr_no_commas (parser, NULL).value;
7089 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
7090 error ("schedule %<runtime%> does not take "
7091 "a %<chunk_size%> parameter");
7092 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
7093 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7094 else
7095 c_parser_error (parser, "expected integer expression");
7097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7099 else
7100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7101 "expected %<,%> or %<)%>");
7103 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
7104 OMP_CLAUSE_CHAIN (c) = list;
7105 return c;
7107 invalid_kind:
7108 c_parser_error (parser, "invalid schedule kind");
7109 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
7110 return list;
7113 /* OpenMP 2.5:
7114 shared ( variable-list ) */
7116 static tree
7117 c_parser_omp_clause_shared (c_parser *parser, tree list)
7119 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
7122 /* Parse all OpenMP clauses. The set clauses allowed by the directive
7123 is a bitmask in MASK. Return the list of clauses found; the result
7124 of clause default goes in *pdefault. */
7126 static tree
7127 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
7128 const char *where)
7130 tree clauses = NULL;
7132 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7134 const pragma_omp_clause c_kind = c_parser_omp_clause_name (parser);
7135 const char *c_name;
7136 tree prev = clauses;
7138 switch (c_kind)
7140 case PRAGMA_OMP_CLAUSE_COPYIN:
7141 clauses = c_parser_omp_clause_copyin (parser, clauses);
7142 c_name = "copyin";
7143 break;
7144 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
7145 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
7146 c_name = "copyprivate";
7147 break;
7148 case PRAGMA_OMP_CLAUSE_DEFAULT:
7149 clauses = c_parser_omp_clause_default (parser, clauses);
7150 c_name = "default";
7151 break;
7152 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
7153 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
7154 c_name = "firstprivate";
7155 break;
7156 case PRAGMA_OMP_CLAUSE_IF:
7157 clauses = c_parser_omp_clause_if (parser, clauses);
7158 c_name = "if";
7159 break;
7160 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
7161 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
7162 c_name = "lastprivate";
7163 break;
7164 case PRAGMA_OMP_CLAUSE_NOWAIT:
7165 clauses = c_parser_omp_clause_nowait (parser, clauses);
7166 c_name = "nowait";
7167 break;
7168 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
7169 clauses = c_parser_omp_clause_num_threads (parser, clauses);
7170 c_name = "num_threads";
7171 break;
7172 case PRAGMA_OMP_CLAUSE_ORDERED:
7173 clauses = c_parser_omp_clause_ordered (parser, clauses);
7174 c_name = "ordered";
7175 break;
7176 case PRAGMA_OMP_CLAUSE_PRIVATE:
7177 clauses = c_parser_omp_clause_private (parser, clauses);
7178 c_name = "private";
7179 break;
7180 case PRAGMA_OMP_CLAUSE_REDUCTION:
7181 clauses = c_parser_omp_clause_reduction (parser, clauses);
7182 c_name = "reduction";
7183 break;
7184 case PRAGMA_OMP_CLAUSE_SCHEDULE:
7185 clauses = c_parser_omp_clause_schedule (parser, clauses);
7186 c_name = "schedule";
7187 break;
7188 case PRAGMA_OMP_CLAUSE_SHARED:
7189 clauses = c_parser_omp_clause_shared (parser, clauses);
7190 c_name = "shared";
7191 break;
7192 default:
7193 c_parser_error (parser, "expected %<#pragma omp%> clause");
7194 goto saw_error;
7197 if (((mask >> c_kind) & 1) == 0 && !parser->error)
7199 /* Remove the invalid clause(s) from the list to avoid
7200 confusing the rest of the compiler. */
7201 clauses = prev;
7202 error ("%qs is not valid for %qs", c_name, where);
7206 saw_error:
7207 c_parser_skip_to_pragma_eol (parser);
7209 return c_finish_omp_clauses (clauses);
7212 /* OpenMP 2.5:
7213 structured-block:
7214 statement
7216 In practice, we're also interested in adding the statement to an
7217 outer node. So it is convenient if we work around the fact that
7218 c_parser_statement calls add_stmt. */
7220 static tree
7221 c_parser_omp_structured_block (c_parser *parser)
7223 tree stmt = push_stmt_list ();
7224 c_parser_statement (parser);
7225 return pop_stmt_list (stmt);
7228 /* OpenMP 2.5:
7229 # pragma omp atomic new-line
7230 expression-stmt
7232 expression-stmt:
7233 x binop= expr | x++ | ++x | x-- | --x
7234 binop:
7235 +, *, -, /, &, ^, |, <<, >>
7237 where x is an lvalue expression with scalar type. */
7239 static void
7240 c_parser_omp_atomic (c_parser *parser)
7242 tree lhs, rhs;
7243 tree stmt;
7244 enum tree_code code;
7246 c_parser_skip_to_pragma_eol (parser);
7248 lhs = c_parser_unary_expression (parser).value;
7249 switch (TREE_CODE (lhs))
7251 case ERROR_MARK:
7252 saw_error:
7253 c_parser_skip_to_end_of_block_or_statement (parser);
7254 return;
7256 case PREINCREMENT_EXPR:
7257 case POSTINCREMENT_EXPR:
7258 lhs = TREE_OPERAND (lhs, 0);
7259 code = PLUS_EXPR;
7260 rhs = integer_one_node;
7261 break;
7263 case PREDECREMENT_EXPR:
7264 case POSTDECREMENT_EXPR:
7265 lhs = TREE_OPERAND (lhs, 0);
7266 code = MINUS_EXPR;
7267 rhs = integer_one_node;
7268 break;
7270 default:
7271 switch (c_parser_peek_token (parser)->type)
7273 case CPP_MULT_EQ:
7274 code = MULT_EXPR;
7275 break;
7276 case CPP_DIV_EQ:
7277 code = TRUNC_DIV_EXPR;
7278 break;
7279 case CPP_PLUS_EQ:
7280 code = PLUS_EXPR;
7281 break;
7282 case CPP_MINUS_EQ:
7283 code = MINUS_EXPR;
7284 break;
7285 case CPP_LSHIFT_EQ:
7286 code = LSHIFT_EXPR;
7287 break;
7288 case CPP_RSHIFT_EQ:
7289 code = RSHIFT_EXPR;
7290 break;
7291 case CPP_AND_EQ:
7292 code = BIT_AND_EXPR;
7293 break;
7294 case CPP_OR_EQ:
7295 code = BIT_IOR_EXPR;
7296 break;
7297 case CPP_XOR_EQ:
7298 code = BIT_XOR_EXPR;
7299 break;
7300 default:
7301 c_parser_error (parser,
7302 "invalid operator for %<#pragma omp atomic%>");
7303 goto saw_error;
7306 c_parser_consume_token (parser);
7307 rhs = c_parser_expression (parser).value;
7308 break;
7310 stmt = c_finish_omp_atomic (code, lhs, rhs);
7311 if (stmt != error_mark_node)
7312 add_stmt (stmt);
7313 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7317 /* OpenMP 2.5:
7318 # pragma omp barrier new-line
7321 static void
7322 c_parser_omp_barrier (c_parser *parser)
7324 c_parser_consume_pragma (parser);
7325 c_parser_skip_to_pragma_eol (parser);
7327 c_finish_omp_barrier ();
7330 /* OpenMP 2.5:
7331 # pragma omp critical [(name)] new-line
7332 structured-block
7335 static tree
7336 c_parser_omp_critical (c_parser *parser)
7338 tree stmt, name = NULL;
7340 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7342 c_parser_consume_token (parser);
7343 if (c_parser_next_token_is (parser, CPP_NAME))
7345 name = c_parser_peek_token (parser)->value;
7346 c_parser_consume_token (parser);
7347 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7349 else
7350 c_parser_error (parser, "expected identifier");
7352 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7353 c_parser_error (parser, "expected %<(%> or end of line");
7354 c_parser_skip_to_pragma_eol (parser);
7356 stmt = c_parser_omp_structured_block (parser);
7357 return c_finish_omp_critical (stmt, name);
7360 /* OpenMP 2.5:
7361 # pragma omp flush flush-vars[opt] new-line
7363 flush-vars:
7364 ( variable-list ) */
7366 static void
7367 c_parser_omp_flush (c_parser *parser)
7369 c_parser_consume_pragma (parser);
7370 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7371 c_parser_omp_var_list_parens (parser, 0, NULL);
7372 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
7373 c_parser_error (parser, "expected %<(%> or end of line");
7374 c_parser_skip_to_pragma_eol (parser);
7376 c_finish_omp_flush ();
7379 /* Parse the restricted form of the for statment allowed by OpenMP.
7380 The real trick here is to determine the loop control variable early
7381 so that we can push a new decl if necessary to make it private. */
7383 static tree
7384 c_parser_omp_for_loop (c_parser *parser)
7386 tree decl, cond, incr, save_break, save_cont, body, init;
7387 location_t loc;
7389 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
7391 c_parser_error (parser, "for statement expected");
7392 return NULL;
7394 loc = c_parser_peek_token (parser)->location;
7395 c_parser_consume_token (parser);
7397 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7398 return NULL;
7400 /* Parse the initialization declaration or expression. */
7401 if (c_parser_next_token_starts_declspecs (parser))
7403 c_parser_declaration_or_fndef (parser, true, true, true, true);
7404 decl = check_for_loop_decls ();
7405 if (decl == NULL)
7406 goto error_init;
7407 init = decl;
7409 else if (c_parser_next_token_is (parser, CPP_NAME)
7410 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
7412 decl = c_parser_postfix_expression (parser).value;
7414 c_parser_require (parser, CPP_EQ, "expected %<=%>");
7416 init = c_parser_expr_no_commas (parser, NULL).value;
7417 init = build_modify_expr (decl, NOP_EXPR, init);
7418 init = c_process_expr_stmt (init);
7420 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7422 else
7423 goto error_init;
7425 /* Parse the loop condition. */
7426 cond = NULL_TREE;
7427 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
7429 cond = c_parser_expression_conv (parser).value;
7430 cond = c_objc_common_truthvalue_conversion (cond);
7431 if (CAN_HAVE_LOCATION_P (cond))
7432 SET_EXPR_LOCATION (cond, input_location);
7434 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7436 /* Parse the increment expression. */
7437 incr = NULL_TREE;
7438 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
7439 incr = c_process_expr_stmt (c_parser_expression (parser).value);
7440 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7442 parse_body:
7443 save_break = c_break_label;
7444 c_break_label = size_one_node;
7445 save_cont = c_cont_label;
7446 c_cont_label = NULL_TREE;
7447 body = push_stmt_list ();
7449 add_stmt (c_parser_c99_block_statement (parser));
7450 if (c_cont_label)
7451 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label));
7453 body = pop_stmt_list (body);
7454 c_break_label = save_break;
7455 c_cont_label = save_cont;
7457 /* Only bother calling c_finish_omp_for if we havn't already generated
7458 an error from the initialization parsing. */
7459 if (decl != NULL && decl != error_mark_node && init != error_mark_node)
7460 return c_finish_omp_for (loc, decl, init, cond, incr, body, NULL);
7461 return NULL;
7463 error_init:
7464 c_parser_error (parser, "expected iteration declaration or initialization");
7465 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7466 decl = init = cond = incr = NULL_TREE;
7467 goto parse_body;
7470 /* OpenMP 2.5:
7471 #pragma omp for for-clause[optseq] new-line
7472 for-loop
7475 #define OMP_FOR_CLAUSE_MASK \
7476 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7477 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7478 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7479 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7480 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
7481 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
7482 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7484 static tree
7485 c_parser_omp_for (c_parser *parser)
7487 tree block, clauses, ret;
7489 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
7490 "#pragma omp for");
7492 block = c_begin_compound_stmt (true);
7493 ret = c_parser_omp_for_loop (parser);
7494 if (ret)
7495 OMP_FOR_CLAUSES (ret) = clauses;
7496 block = c_end_compound_stmt (block, true);
7497 add_stmt (block);
7499 return ret;
7502 /* OpenMP 2.5:
7503 # pragma omp master new-line
7504 structured-block
7507 static tree
7508 c_parser_omp_master (c_parser *parser)
7510 c_parser_skip_to_pragma_eol (parser);
7511 return c_finish_omp_master (c_parser_omp_structured_block (parser));
7514 /* OpenMP 2.5:
7515 # pragma omp ordered new-line
7516 structured-block
7519 static tree
7520 c_parser_omp_ordered (c_parser *parser)
7522 c_parser_skip_to_pragma_eol (parser);
7523 return c_finish_omp_ordered (c_parser_omp_structured_block (parser));
7526 /* OpenMP 2.5:
7528 section-scope:
7529 { section-sequence }
7531 section-sequence:
7532 section-directive[opt] structured-block
7533 section-sequence section-directive structured-block */
7535 static tree
7536 c_parser_omp_sections_scope (c_parser *parser)
7538 tree stmt, substmt;
7539 bool error_suppress = false;
7540 location_t loc;
7542 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7544 /* Avoid skipping until the end of the block. */
7545 parser->error = false;
7546 return NULL_TREE;
7549 stmt = push_stmt_list ();
7551 loc = c_parser_peek_token (parser)->location;
7552 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
7554 substmt = push_stmt_list ();
7556 while (1)
7558 c_parser_statement (parser);
7560 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7561 break;
7562 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7563 break;
7564 if (c_parser_next_token_is (parser, CPP_EOF))
7565 break;
7568 substmt = pop_stmt_list (substmt);
7569 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7570 SET_EXPR_LOCATION (substmt, loc);
7571 add_stmt (substmt);
7574 while (1)
7576 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7577 break;
7578 if (c_parser_next_token_is (parser, CPP_EOF))
7579 break;
7581 loc = c_parser_peek_token (parser)->location;
7582 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
7584 c_parser_consume_pragma (parser);
7585 c_parser_skip_to_pragma_eol (parser);
7586 error_suppress = false;
7588 else if (!error_suppress)
7590 error ("expected %<#pragma omp section%> or %<}%>");
7591 error_suppress = true;
7594 substmt = c_parser_omp_structured_block (parser);
7595 substmt = build1 (OMP_SECTION, void_type_node, substmt);
7596 SET_EXPR_LOCATION (substmt, loc);
7597 add_stmt (substmt);
7599 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
7600 "expected %<#pragma omp section%> or %<}%>");
7602 substmt = pop_stmt_list (stmt);
7604 stmt = make_node (OMP_SECTIONS);
7605 TREE_TYPE (stmt) = void_type_node;
7606 OMP_SECTIONS_BODY (stmt) = substmt;
7608 return add_stmt (stmt);
7611 /* OpenMP 2.5:
7612 # pragma omp sections sections-clause[optseq] newline
7613 sections-scope
7616 #define OMP_SECTIONS_CLAUSE_MASK \
7617 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7618 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7619 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
7620 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7621 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7623 static tree
7624 c_parser_omp_sections (c_parser *parser)
7626 tree block, clauses, ret;
7628 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
7629 "#pragma omp sections");
7631 block = c_begin_compound_stmt (true);
7632 ret = c_parser_omp_sections_scope (parser);
7633 if (ret)
7634 OMP_SECTIONS_CLAUSES (ret) = clauses;
7635 block = c_end_compound_stmt (block, true);
7636 add_stmt (block);
7638 return ret;
7641 /* OpenMP 2.5:
7642 # pragma parallel parallel-clause new-line
7643 # pragma parallel for parallel-for-clause new-line
7644 # pragma parallel sections parallel-sections-clause new-line
7647 #define OMP_PARALLEL_CLAUSE_MASK \
7648 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
7649 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7650 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7651 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
7652 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
7653 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
7654 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
7655 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
7657 static tree
7658 c_parser_omp_parallel (c_parser *parser)
7660 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
7661 const char *p_name = "#pragma omp parallel";
7662 tree stmt, clauses, par_clause, ws_clause, block;
7663 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
7665 if (c_parser_next_token_is_keyword (parser, RID_FOR))
7667 c_parser_consume_token (parser);
7668 p_kind = PRAGMA_OMP_PARALLEL_FOR;
7669 p_name = "#pragma omp parallel for";
7670 mask |= OMP_FOR_CLAUSE_MASK;
7671 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7673 else if (c_parser_next_token_is (parser, CPP_NAME))
7675 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
7676 if (strcmp (p, "sections") == 0)
7678 c_parser_consume_token (parser);
7679 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
7680 p_name = "#pragma omp parallel sections";
7681 mask |= OMP_SECTIONS_CLAUSE_MASK;
7682 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
7686 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
7688 switch (p_kind)
7690 case PRAGMA_OMP_PARALLEL:
7691 block = c_begin_omp_parallel ();
7692 c_parser_statement (parser);
7693 stmt = c_finish_omp_parallel (clauses, block);
7694 break;
7696 case PRAGMA_OMP_PARALLEL_FOR:
7697 block = c_begin_omp_parallel ();
7698 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7699 stmt = c_parser_omp_for_loop (parser);
7700 if (stmt)
7701 OMP_FOR_CLAUSES (stmt) = ws_clause;
7702 stmt = c_finish_omp_parallel (par_clause, block);
7703 OMP_PARALLEL_COMBINED (stmt) = 1;
7704 break;
7706 case PRAGMA_OMP_PARALLEL_SECTIONS:
7707 block = c_begin_omp_parallel ();
7708 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
7709 stmt = c_parser_omp_sections_scope (parser);
7710 if (stmt)
7711 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
7712 stmt = c_finish_omp_parallel (par_clause, block);
7713 OMP_PARALLEL_COMBINED (stmt) = 1;
7714 break;
7716 default:
7717 gcc_unreachable ();
7720 return stmt;
7723 /* OpenMP 2.5:
7724 # pragma omp single single-clause[optseq] new-line
7725 structured-block
7728 #define OMP_SINGLE_CLAUSE_MASK \
7729 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
7730 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
7731 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
7732 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
7734 static tree
7735 c_parser_omp_single (c_parser *parser)
7737 tree stmt = make_node (OMP_SINGLE);
7738 TREE_TYPE (stmt) = void_type_node;
7740 OMP_SINGLE_CLAUSES (stmt)
7741 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
7742 "#pragma omp single");
7743 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
7745 return add_stmt (stmt);
7749 /* Main entry point to parsing most OpenMP pragmas. */
7751 static void
7752 c_parser_omp_construct (c_parser *parser)
7754 enum pragma_kind p_kind;
7755 location_t loc;
7756 tree stmt;
7758 loc = c_parser_peek_token (parser)->location;
7759 p_kind = c_parser_peek_token (parser)->pragma_kind;
7760 c_parser_consume_pragma (parser);
7762 /* For all constructs below except #pragma omp atomic
7763 MUST_NOT_THROW catch handlers are needed when exceptions
7764 are enabled. */
7765 if (p_kind != PRAGMA_OMP_ATOMIC)
7766 c_maybe_initialize_eh ();
7768 switch (p_kind)
7770 case PRAGMA_OMP_ATOMIC:
7771 c_parser_omp_atomic (parser);
7772 return;
7773 case PRAGMA_OMP_CRITICAL:
7774 stmt = c_parser_omp_critical (parser);
7775 break;
7776 case PRAGMA_OMP_FOR:
7777 stmt = c_parser_omp_for (parser);
7778 break;
7779 case PRAGMA_OMP_MASTER:
7780 stmt = c_parser_omp_master (parser);
7781 break;
7782 case PRAGMA_OMP_ORDERED:
7783 stmt = c_parser_omp_ordered (parser);
7784 break;
7785 case PRAGMA_OMP_PARALLEL:
7786 stmt = c_parser_omp_parallel (parser);
7787 break;
7788 case PRAGMA_OMP_SECTIONS:
7789 stmt = c_parser_omp_sections (parser);
7790 break;
7791 case PRAGMA_OMP_SINGLE:
7792 stmt = c_parser_omp_single (parser);
7793 break;
7794 default:
7795 gcc_unreachable ();
7798 if (stmt)
7799 SET_EXPR_LOCATION (stmt, loc);
7803 /* OpenMP 2.5:
7804 # pragma omp threadprivate (variable-list) */
7806 static void
7807 c_parser_omp_threadprivate (c_parser *parser)
7809 tree vars, t;
7811 c_parser_consume_pragma (parser);
7812 vars = c_parser_omp_var_list_parens (parser, 0, NULL);
7814 /* Mark every variable in VARS to be assigned thread local storage. */
7815 for (t = vars; t; t = TREE_CHAIN (t))
7817 tree v = TREE_PURPOSE (t);
7819 /* If V had already been marked threadprivate, it doesn't matter
7820 whether it had been used prior to this point. */
7821 if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
7822 error ("%qE declared %<threadprivate%> after first use", v);
7823 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
7824 error ("automatic variable %qE cannot be %<threadprivate%>", v);
7825 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
7826 error ("%<threadprivate%> %qE has incomplete type", v);
7827 else
7829 if (! DECL_THREAD_LOCAL_P (v))
7831 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
7832 /* If rtl has been already set for this var, call
7833 make_decl_rtl once again, so that encode_section_info
7834 has a chance to look at the new decl flags. */
7835 if (DECL_RTL_SET_P (v))
7836 make_decl_rtl (v);
7838 C_DECL_THREADPRIVATE_P (v) = 1;
7842 c_parser_skip_to_pragma_eol (parser);
7846 /* Parse a single source file. */
7848 void
7849 c_parse_file (void)
7851 /* Use local storage to begin. If the first token is a pragma, parse it.
7852 If it is #pragma GCC pch_preprocess, then this will load a PCH file
7853 which will cause garbage collection. */
7854 c_parser tparser;
7856 memset (&tparser, 0, sizeof tparser);
7857 the_parser = &tparser;
7859 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
7860 c_parser_pragma_pch_preprocess (&tparser);
7862 the_parser = GGC_NEW (c_parser);
7863 *the_parser = tparser;
7865 c_parser_translation_unit (the_parser);
7866 the_parser = NULL;
7869 #include "gt-c-parser.h"