Fix a bug that broke -freorder-functions
[official-gcc.git] / gcc / c-parser.c
blob0d2a1b7dfca075640c70c711e11e07804e3abcf6
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, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Parser actions based on the old Bison parser; structure somewhat
7 influenced by and fragments based on the C++ parser.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 /* TODO:
27 Make sure all relevant comments, and all relevant code from all
28 actions, brought over from old parser. Verify exact correspondence
29 of syntax accepted.
31 Add testcases covering every input symbol in every state in old and
32 new parsers.
34 Include full syntax for GNU C, including erroneous cases accepted
35 with error messages, in syntax productions in comments.
37 Make more diagnostics in the front end generally take an explicit
38 location rather than implicitly using input_location. */
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h" /* For rtl.h: needs enum reg_class. */
44 #include "tree.h"
45 #include "langhooks.h"
46 #include "input.h"
47 #include "cpplib.h"
48 #include "timevar.h"
49 #include "c-family/c-pragma.h"
50 #include "c-tree.h"
51 #include "flags.h"
52 #include "output.h"
53 #include "ggc.h"
54 #include "c-family/c-common.h"
55 #include "c-family/c-objc.h"
56 #include "vec.h"
57 #include "target.h"
58 #include "cgraph.h"
59 #include "plugin.h"
62 /* Initialization routine for this file. */
64 void
65 c_parse_init (void)
67 /* The only initialization required is of the reserved word
68 identifiers. */
69 unsigned int i;
70 tree id;
71 int mask = 0;
73 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
74 the c_token structure. */
75 gcc_assert (RID_MAX <= 255);
77 mask |= D_CXXONLY;
78 if (!flag_isoc99)
79 mask |= D_C99;
80 if (flag_no_asm)
82 mask |= D_ASM | D_EXT;
83 if (!flag_isoc99)
84 mask |= D_EXT89;
86 if (!c_dialect_objc ())
87 mask |= D_OBJC | D_CXX_OBJC;
89 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
90 for (i = 0; i < num_c_common_reswords; i++)
92 /* If a keyword is disabled, do not enter it into the table
93 and so create a canonical spelling that isn't a keyword. */
94 if (c_common_reswords[i].disable & mask)
96 if (warn_cxx_compat
97 && (c_common_reswords[i].disable & D_CXXWARN))
99 id = get_identifier (c_common_reswords[i].word);
100 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
101 C_IS_RESERVED_WORD (id) = 1;
103 continue;
106 id = get_identifier (c_common_reswords[i].word);
107 C_SET_RID_CODE (id, c_common_reswords[i].rid);
108 C_IS_RESERVED_WORD (id) = 1;
109 ridpointers [(int) c_common_reswords[i].rid] = id;
113 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
114 and the C parser. Unlike the C++ lexer, the parser structure
115 stores the lexer information instead of using a separate structure.
116 Identifiers are separated into ordinary identifiers, type names,
117 keywords and some other Objective-C types of identifiers, and some
118 look-ahead is maintained.
120 ??? It might be a good idea to lex the whole file up front (as for
121 C++). It would then be possible to share more of the C and C++
122 lexer code, if desired. */
124 /* The following local token type is used. */
126 /* A keyword. */
127 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
129 /* More information about the type of a CPP_NAME token. */
130 typedef enum c_id_kind {
131 /* An ordinary identifier. */
132 C_ID_ID,
133 /* An identifier declared as a typedef name. */
134 C_ID_TYPENAME,
135 /* An identifier declared as an Objective-C class name. */
136 C_ID_CLASSNAME,
137 /* An address space identifier. */
138 C_ID_ADDRSPACE,
139 /* Not an identifier. */
140 C_ID_NONE
141 } c_id_kind;
143 /* A single C token after string literal concatenation and conversion
144 of preprocessing tokens to tokens. */
145 typedef struct GTY (()) c_token {
146 /* The kind of token. */
147 ENUM_BITFIELD (cpp_ttype) type : 8;
148 /* If this token is a CPP_NAME, this value indicates whether also
149 declared as some kind of type. Otherwise, it is C_ID_NONE. */
150 ENUM_BITFIELD (c_id_kind) id_kind : 8;
151 /* If this token is a keyword, this value indicates which keyword.
152 Otherwise, this value is RID_MAX. */
153 ENUM_BITFIELD (rid) keyword : 8;
154 /* If this token is a CPP_PRAGMA, this indicates the pragma that
155 was seen. Otherwise it is PRAGMA_NONE. */
156 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
157 /* The location at which this token was found. */
158 location_t location;
159 /* The value associated with this token, if any. */
160 tree value;
161 } c_token;
163 /* A parser structure recording information about the state and
164 context of parsing. Includes lexer information with up to two
165 tokens of look-ahead; more are not needed for C. */
166 typedef struct GTY(()) c_parser {
167 /* The look-ahead tokens. */
168 c_token tokens[2];
169 /* How many look-ahead tokens are available (0, 1 or 2). */
170 short tokens_avail;
171 /* True if a syntax error is being recovered from; false otherwise.
172 c_parser_error sets this flag. It should clear this flag when
173 enough tokens have been consumed to recover from the error. */
174 BOOL_BITFIELD error : 1;
175 /* True if we're processing a pragma, and shouldn't automatically
176 consume CPP_PRAGMA_EOL. */
177 BOOL_BITFIELD in_pragma : 1;
178 /* True if we're parsing the outermost block of an if statement. */
179 BOOL_BITFIELD in_if_block : 1;
180 /* True if we want to lex an untranslated string. */
181 BOOL_BITFIELD lex_untranslated_string : 1;
183 /* Objective-C specific parser/lexer information. */
185 /* True if we are in a context where the Objective-C "PQ" keywords
186 are considered keywords. */
187 BOOL_BITFIELD objc_pq_context : 1;
188 /* True if we are parsing a (potential) Objective-C foreach
189 statement. This is set to true after we parsed 'for (' and while
190 we wait for 'in' or ';' to decide if it's a standard C for loop or an
191 Objective-C foreach loop. */
192 BOOL_BITFIELD objc_could_be_foreach_context : 1;
193 /* The following flag is needed to contextualize Objective-C lexical
194 analysis. In some cases (e.g., 'int NSObject;'), it is
195 undesirable to bind an identifier to an Objective-C class, even
196 if a class with that name exists. */
197 BOOL_BITFIELD objc_need_raw_identifier : 1;
198 /* True if we are in a context where the Objective-C "Property attribute"
199 keywords are valid. */
200 BOOL_BITFIELD objc_property_attr_context : 1;
201 } c_parser;
204 /* The actual parser and external interface. ??? Does this need to be
205 garbage-collected? */
207 static GTY (()) c_parser *the_parser;
209 /* Read in and lex a single token, storing it in *TOKEN. */
211 static void
212 c_lex_one_token (c_parser *parser, c_token *token)
214 timevar_push (TV_LEX);
216 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
217 (parser->lex_untranslated_string
218 ? C_LEX_STRING_NO_TRANSLATE : 0));
219 token->id_kind = C_ID_NONE;
220 token->keyword = RID_MAX;
221 token->pragma_kind = PRAGMA_NONE;
223 switch (token->type)
225 case CPP_NAME:
227 tree decl;
229 bool objc_force_identifier = parser->objc_need_raw_identifier;
230 if (c_dialect_objc ())
231 parser->objc_need_raw_identifier = false;
233 if (C_IS_RESERVED_WORD (token->value))
235 enum rid rid_code = C_RID_CODE (token->value);
237 if (rid_code == RID_CXX_COMPAT_WARN)
239 warning_at (token->location,
240 OPT_Wc___compat,
241 "identifier %qE conflicts with C++ keyword",
242 token->value);
244 else if (rid_code >= RID_FIRST_ADDR_SPACE
245 && rid_code <= RID_LAST_ADDR_SPACE)
247 token->id_kind = C_ID_ADDRSPACE;
248 token->keyword = rid_code;
249 break;
251 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
253 /* We found an Objective-C "pq" keyword (in, out,
254 inout, bycopy, byref, oneway). They need special
255 care because the interpretation depends on the
256 context. */
257 if (parser->objc_pq_context)
259 token->type = CPP_KEYWORD;
260 token->keyword = rid_code;
261 break;
263 else if (parser->objc_could_be_foreach_context
264 && rid_code == RID_IN)
266 /* We are in Objective-C, inside a (potential)
267 foreach context (which means after having
268 parsed 'for (', but before having parsed ';'),
269 and we found 'in'. We consider it the keyword
270 which terminates the declaration at the
271 beginning of a foreach-statement. Note that
272 this means you can't use 'in' for anything else
273 in that context; in particular, in Objective-C
274 you can't use 'in' as the name of the running
275 variable in a C for loop. We could potentially
276 try to add code here to disambiguate, but it
277 seems a reasonable limitation. */
278 token->type = CPP_KEYWORD;
279 token->keyword = rid_code;
280 break;
282 /* Else, "pq" keywords outside of the "pq" context are
283 not keywords, and we fall through to the code for
284 normal tokens. */
286 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
288 /* We found an Objective-C "property attribute"
289 keyword (getter, setter, readonly, etc). These are
290 only valid in the property context. */
291 if (parser->objc_property_attr_context)
293 token->type = CPP_KEYWORD;
294 token->keyword = rid_code;
295 break;
297 /* Else they are not special keywords.
300 else if (c_dialect_objc ()
301 && (OBJC_IS_AT_KEYWORD (rid_code)
302 || OBJC_IS_CXX_KEYWORD (rid_code)))
304 /* We found one of the Objective-C "@" keywords (defs,
305 selector, synchronized, etc) or one of the
306 Objective-C "cxx" keywords (class, private,
307 protected, public, try, catch, throw) without a
308 preceding '@' sign. Do nothing and fall through to
309 the code for normal tokens (in C++ we would still
310 consider the CXX ones keywords, but not in C). */
313 else
315 token->type = CPP_KEYWORD;
316 token->keyword = rid_code;
317 break;
321 decl = lookup_name (token->value);
322 if (decl)
324 if (TREE_CODE (decl) == TYPE_DECL)
326 token->id_kind = C_ID_TYPENAME;
327 break;
330 else if (c_dialect_objc ())
332 tree objc_interface_decl = objc_is_class_name (token->value);
333 /* Objective-C class names are in the same namespace as
334 variables and typedefs, and hence are shadowed by local
335 declarations. */
336 if (objc_interface_decl
337 && (!objc_force_identifier || global_bindings_p ()))
339 token->value = objc_interface_decl;
340 token->id_kind = C_ID_CLASSNAME;
341 break;
344 token->id_kind = C_ID_ID;
346 break;
347 case CPP_AT_NAME:
348 /* This only happens in Objective-C; it must be a keyword. */
349 token->type = CPP_KEYWORD;
350 switch (C_RID_CODE (token->value))
352 /* Replace 'class' with '@class', 'private' with '@private',
353 etc. This prevents confusion with the C++ keyword
354 'class', and makes the tokens consistent with other
355 Objective-C 'AT' keywords. For example '@class' is
356 reported as RID_AT_CLASS which is consistent with
357 '@synchronized', which is reported as
358 RID_AT_SYNCHRONIZED.
360 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
361 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
362 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
363 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
364 case RID_THROW: token->keyword = RID_AT_THROW; break;
365 case RID_TRY: token->keyword = RID_AT_TRY; break;
366 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
367 default: token->keyword = C_RID_CODE (token->value);
369 break;
370 case CPP_COLON:
371 case CPP_COMMA:
372 case CPP_CLOSE_PAREN:
373 case CPP_SEMICOLON:
374 /* These tokens may affect the interpretation of any identifiers
375 following, if doing Objective-C. */
376 if (c_dialect_objc ())
377 parser->objc_need_raw_identifier = false;
378 break;
379 case CPP_PRAGMA:
380 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
381 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
382 token->value = NULL;
383 break;
384 default:
385 break;
387 timevar_pop (TV_LEX);
390 /* Return a pointer to the next token from PARSER, reading it in if
391 necessary. */
393 static inline c_token *
394 c_parser_peek_token (c_parser *parser)
396 if (parser->tokens_avail == 0)
398 c_lex_one_token (parser, &parser->tokens[0]);
399 parser->tokens_avail = 1;
401 return &parser->tokens[0];
404 /* Return true if the next token from PARSER has the indicated
405 TYPE. */
407 static inline bool
408 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
410 return c_parser_peek_token (parser)->type == type;
413 /* Return true if the next token from PARSER does not have the
414 indicated TYPE. */
416 static inline bool
417 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
419 return !c_parser_next_token_is (parser, type);
422 /* Return true if the next token from PARSER is the indicated
423 KEYWORD. */
425 static inline bool
426 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
428 return c_parser_peek_token (parser)->keyword == keyword;
431 /* Return a pointer to the next-but-one token from PARSER, reading it
432 in if necessary. The next token is already read in. */
434 static c_token *
435 c_parser_peek_2nd_token (c_parser *parser)
437 if (parser->tokens_avail >= 2)
438 return &parser->tokens[1];
439 gcc_assert (parser->tokens_avail == 1);
440 gcc_assert (parser->tokens[0].type != CPP_EOF);
441 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
442 c_lex_one_token (parser, &parser->tokens[1]);
443 parser->tokens_avail = 2;
444 return &parser->tokens[1];
447 /* Return true if TOKEN can start a type name,
448 false otherwise. */
449 static bool
450 c_token_starts_typename (c_token *token)
452 switch (token->type)
454 case CPP_NAME:
455 switch (token->id_kind)
457 case C_ID_ID:
458 return false;
459 case C_ID_ADDRSPACE:
460 return true;
461 case C_ID_TYPENAME:
462 return true;
463 case C_ID_CLASSNAME:
464 gcc_assert (c_dialect_objc ());
465 return true;
466 default:
467 gcc_unreachable ();
469 case CPP_KEYWORD:
470 switch (token->keyword)
472 case RID_UNSIGNED:
473 case RID_LONG:
474 case RID_INT128:
475 case RID_SHORT:
476 case RID_SIGNED:
477 case RID_COMPLEX:
478 case RID_INT:
479 case RID_CHAR:
480 case RID_FLOAT:
481 case RID_DOUBLE:
482 case RID_VOID:
483 case RID_DFLOAT32:
484 case RID_DFLOAT64:
485 case RID_DFLOAT128:
486 case RID_BOOL:
487 case RID_ENUM:
488 case RID_STRUCT:
489 case RID_UNION:
490 case RID_TYPEOF:
491 case RID_CONST:
492 case RID_VOLATILE:
493 case RID_RESTRICT:
494 case RID_ATTRIBUTE:
495 case RID_FRACT:
496 case RID_ACCUM:
497 case RID_SAT:
498 return true;
499 default:
500 return false;
502 case CPP_LESS:
503 if (c_dialect_objc ())
504 return true;
505 return false;
506 default:
507 return false;
511 enum c_lookahead_kind {
512 /* Always treat unknown identifiers as typenames. */
513 cla_prefer_type,
515 /* Could be parsing a nonabstract declarator. Only treat an identifier
516 as a typename if followed by another identifier or a star. */
517 cla_nonabstract_decl,
519 /* Never treat identifiers as typenames. */
520 cla_prefer_id
523 /* Return true if the next token from PARSER can start a type name,
524 false otherwise. LA specifies how to do lookahead in order to
525 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
527 static inline bool
528 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
530 c_token *token = c_parser_peek_token (parser);
531 if (c_token_starts_typename (token))
532 return true;
534 /* Try a bit harder to detect an unknown typename. */
535 if (la != cla_prefer_id
536 && token->type == CPP_NAME
537 && token->id_kind == C_ID_ID
539 /* Do not try too hard when we could have "object in array". */
540 && !parser->objc_could_be_foreach_context
542 && (la == cla_prefer_type
543 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
544 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
546 /* Only unknown identifiers. */
547 && !lookup_name (token->value))
548 return true;
550 return false;
553 /* Return true if TOKEN is a type qualifier, false otherwise. */
554 static bool
555 c_token_is_qualifier (c_token *token)
557 switch (token->type)
559 case CPP_NAME:
560 switch (token->id_kind)
562 case C_ID_ADDRSPACE:
563 return true;
564 default:
565 return false;
567 case CPP_KEYWORD:
568 switch (token->keyword)
570 case RID_CONST:
571 case RID_VOLATILE:
572 case RID_RESTRICT:
573 case RID_ATTRIBUTE:
574 return true;
575 default:
576 return false;
578 case CPP_LESS:
579 return false;
580 default:
581 gcc_unreachable ();
585 /* Return true if the next token from PARSER is a type qualifier,
586 false otherwise. */
587 static inline bool
588 c_parser_next_token_is_qualifier (c_parser *parser)
590 c_token *token = c_parser_peek_token (parser);
591 return c_token_is_qualifier (token);
594 /* Return true if TOKEN can start declaration specifiers, false
595 otherwise. */
596 static bool
597 c_token_starts_declspecs (c_token *token)
599 switch (token->type)
601 case CPP_NAME:
602 switch (token->id_kind)
604 case C_ID_ID:
605 return false;
606 case C_ID_ADDRSPACE:
607 return true;
608 case C_ID_TYPENAME:
609 return true;
610 case C_ID_CLASSNAME:
611 gcc_assert (c_dialect_objc ());
612 return true;
613 default:
614 gcc_unreachable ();
616 case CPP_KEYWORD:
617 switch (token->keyword)
619 case RID_STATIC:
620 case RID_EXTERN:
621 case RID_REGISTER:
622 case RID_TYPEDEF:
623 case RID_INLINE:
624 case RID_AUTO:
625 case RID_THREAD:
626 case RID_UNSIGNED:
627 case RID_LONG:
628 case RID_INT128:
629 case RID_SHORT:
630 case RID_SIGNED:
631 case RID_COMPLEX:
632 case RID_INT:
633 case RID_CHAR:
634 case RID_FLOAT:
635 case RID_DOUBLE:
636 case RID_VOID:
637 case RID_DFLOAT32:
638 case RID_DFLOAT64:
639 case RID_DFLOAT128:
640 case RID_BOOL:
641 case RID_ENUM:
642 case RID_STRUCT:
643 case RID_UNION:
644 case RID_TYPEOF:
645 case RID_CONST:
646 case RID_VOLATILE:
647 case RID_RESTRICT:
648 case RID_ATTRIBUTE:
649 case RID_FRACT:
650 case RID_ACCUM:
651 case RID_SAT:
652 return true;
653 default:
654 return false;
656 case CPP_LESS:
657 if (c_dialect_objc ())
658 return true;
659 return false;
660 default:
661 return false;
666 /* Return true if TOKEN can start declaration specifiers or a static
667 assertion, false otherwise. */
668 static bool
669 c_token_starts_declaration (c_token *token)
671 if (c_token_starts_declspecs (token)
672 || token->keyword == RID_STATIC_ASSERT)
673 return true;
674 else
675 return false;
678 /* Return true if the next token from PARSER can start declaration
679 specifiers, false otherwise. */
680 static inline bool
681 c_parser_next_token_starts_declspecs (c_parser *parser)
683 c_token *token = c_parser_peek_token (parser);
685 /* In Objective-C, a classname normally starts a declspecs unless it
686 is immediately followed by a dot. In that case, it is the
687 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
688 setter/getter on the class. c_token_starts_declspecs() can't
689 differentiate between the two cases because it only checks the
690 current token, so we have a special check here. */
691 if (c_dialect_objc ()
692 && token->type == CPP_NAME
693 && token->id_kind == C_ID_CLASSNAME
694 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
695 return false;
697 return c_token_starts_declspecs (token);
700 /* Return true if the next tokens from PARSER can start declaration
701 specifiers or a static assertion, false otherwise. */
702 static inline bool
703 c_parser_next_tokens_start_declaration (c_parser *parser)
705 c_token *token = c_parser_peek_token (parser);
707 /* Same as above. */
708 if (c_dialect_objc ()
709 && token->type == CPP_NAME
710 && token->id_kind == C_ID_CLASSNAME
711 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
712 return false;
714 /* Labels do not start declarations. */
715 if (token->type == CPP_NAME
716 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
717 return false;
719 if (c_token_starts_declaration (token))
720 return true;
722 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
723 return true;
725 return false;
728 /* Consume the next token from PARSER. */
730 static void
731 c_parser_consume_token (c_parser *parser)
733 gcc_assert (parser->tokens_avail >= 1);
734 gcc_assert (parser->tokens[0].type != CPP_EOF);
735 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
736 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
737 if (parser->tokens_avail == 2)
738 parser->tokens[0] = parser->tokens[1];
739 parser->tokens_avail--;
742 /* Expect the current token to be a #pragma. Consume it and remember
743 that we've begun parsing a pragma. */
745 static void
746 c_parser_consume_pragma (c_parser *parser)
748 gcc_assert (!parser->in_pragma);
749 gcc_assert (parser->tokens_avail >= 1);
750 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
751 if (parser->tokens_avail == 2)
752 parser->tokens[0] = parser->tokens[1];
753 parser->tokens_avail--;
754 parser->in_pragma = true;
757 /* Update the globals input_location and in_system_header from
758 TOKEN. */
759 static inline void
760 c_parser_set_source_position_from_token (c_token *token)
762 if (token->type != CPP_EOF)
764 input_location = token->location;
768 /* Issue a diagnostic of the form
769 FILE:LINE: MESSAGE before TOKEN
770 where TOKEN is the next token in the input stream of PARSER.
771 MESSAGE (specified by the caller) is usually of the form "expected
772 OTHER-TOKEN".
774 Do not issue a diagnostic if still recovering from an error.
776 ??? This is taken from the C++ parser, but building up messages in
777 this way is not i18n-friendly and some other approach should be
778 used. */
780 static void
781 c_parser_error (c_parser *parser, const char *gmsgid)
783 c_token *token = c_parser_peek_token (parser);
784 if (parser->error)
785 return;
786 parser->error = true;
787 if (!gmsgid)
788 return;
789 /* This diagnostic makes more sense if it is tagged to the line of
790 the token we just peeked at. */
791 c_parser_set_source_position_from_token (token);
792 c_parse_error (gmsgid,
793 /* Because c_parse_error does not understand
794 CPP_KEYWORD, keywords are treated like
795 identifiers. */
796 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
797 /* ??? The C parser does not save the cpp flags of a
798 token, we need to pass 0 here and we will not get
799 the source spelling of some tokens but rather the
800 canonical spelling. */
801 token->value, /*flags=*/0);
804 /* If the next token is of the indicated TYPE, consume it. Otherwise,
805 issue the error MSGID. If MSGID is NULL then a message has already
806 been produced and no message will be produced this time. Returns
807 true if found, false otherwise. */
809 static bool
810 c_parser_require (c_parser *parser,
811 enum cpp_ttype type,
812 const char *msgid)
814 if (c_parser_next_token_is (parser, type))
816 c_parser_consume_token (parser);
817 return true;
819 else
821 c_parser_error (parser, msgid);
822 return false;
826 /* If the next token is the indicated keyword, consume it. Otherwise,
827 issue the error MSGID. Returns true if found, false otherwise. */
829 static bool
830 c_parser_require_keyword (c_parser *parser,
831 enum rid keyword,
832 const char *msgid)
834 if (c_parser_next_token_is_keyword (parser, keyword))
836 c_parser_consume_token (parser);
837 return true;
839 else
841 c_parser_error (parser, msgid);
842 return false;
846 /* Like c_parser_require, except that tokens will be skipped until the
847 desired token is found. An error message is still produced if the
848 next token is not as expected. If MSGID is NULL then a message has
849 already been produced and no message will be produced this
850 time. */
852 static void
853 c_parser_skip_until_found (c_parser *parser,
854 enum cpp_ttype type,
855 const char *msgid)
857 unsigned nesting_depth = 0;
859 if (c_parser_require (parser, type, msgid))
860 return;
862 /* Skip tokens until the desired token is found. */
863 while (true)
865 /* Peek at the next token. */
866 c_token *token = c_parser_peek_token (parser);
867 /* If we've reached the token we want, consume it and stop. */
868 if (token->type == type && !nesting_depth)
870 c_parser_consume_token (parser);
871 break;
874 /* If we've run out of tokens, stop. */
875 if (token->type == CPP_EOF)
876 return;
877 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
878 return;
879 if (token->type == CPP_OPEN_BRACE
880 || token->type == CPP_OPEN_PAREN
881 || token->type == CPP_OPEN_SQUARE)
882 ++nesting_depth;
883 else if (token->type == CPP_CLOSE_BRACE
884 || token->type == CPP_CLOSE_PAREN
885 || token->type == CPP_CLOSE_SQUARE)
887 if (nesting_depth-- == 0)
888 break;
890 /* Consume this token. */
891 c_parser_consume_token (parser);
893 parser->error = false;
896 /* Skip tokens until the end of a parameter is found, but do not
897 consume the comma, semicolon or closing delimiter. */
899 static void
900 c_parser_skip_to_end_of_parameter (c_parser *parser)
902 unsigned nesting_depth = 0;
904 while (true)
906 c_token *token = c_parser_peek_token (parser);
907 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
908 && !nesting_depth)
909 break;
910 /* If we've run out of tokens, stop. */
911 if (token->type == CPP_EOF)
912 return;
913 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
914 return;
915 if (token->type == CPP_OPEN_BRACE
916 || token->type == CPP_OPEN_PAREN
917 || token->type == CPP_OPEN_SQUARE)
918 ++nesting_depth;
919 else if (token->type == CPP_CLOSE_BRACE
920 || token->type == CPP_CLOSE_PAREN
921 || token->type == CPP_CLOSE_SQUARE)
923 if (nesting_depth-- == 0)
924 break;
926 /* Consume this token. */
927 c_parser_consume_token (parser);
929 parser->error = false;
932 /* Expect to be at the end of the pragma directive and consume an
933 end of line marker. */
935 static void
936 c_parser_skip_to_pragma_eol (c_parser *parser)
938 gcc_assert (parser->in_pragma);
939 parser->in_pragma = false;
941 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
942 while (true)
944 c_token *token = c_parser_peek_token (parser);
945 if (token->type == CPP_EOF)
946 break;
947 if (token->type == CPP_PRAGMA_EOL)
949 c_parser_consume_token (parser);
950 break;
952 c_parser_consume_token (parser);
955 parser->error = false;
958 /* Skip tokens until we have consumed an entire block, or until we
959 have consumed a non-nested ';'. */
961 static void
962 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
964 unsigned nesting_depth = 0;
965 bool save_error = parser->error;
967 while (true)
969 c_token *token;
971 /* Peek at the next token. */
972 token = c_parser_peek_token (parser);
974 switch (token->type)
976 case CPP_EOF:
977 return;
979 case CPP_PRAGMA_EOL:
980 if (parser->in_pragma)
981 return;
982 break;
984 case CPP_SEMICOLON:
985 /* If the next token is a ';', we have reached the
986 end of the statement. */
987 if (!nesting_depth)
989 /* Consume the ';'. */
990 c_parser_consume_token (parser);
991 goto finished;
993 break;
995 case CPP_CLOSE_BRACE:
996 /* If the next token is a non-nested '}', then we have
997 reached the end of the current block. */
998 if (nesting_depth == 0 || --nesting_depth == 0)
1000 c_parser_consume_token (parser);
1001 goto finished;
1003 break;
1005 case CPP_OPEN_BRACE:
1006 /* If it the next token is a '{', then we are entering a new
1007 block. Consume the entire block. */
1008 ++nesting_depth;
1009 break;
1011 case CPP_PRAGMA:
1012 /* If we see a pragma, consume the whole thing at once. We
1013 have some safeguards against consuming pragmas willy-nilly.
1014 Normally, we'd expect to be here with parser->error set,
1015 which disables these safeguards. But it's possible to get
1016 here for secondary error recovery, after parser->error has
1017 been cleared. */
1018 c_parser_consume_pragma (parser);
1019 c_parser_skip_to_pragma_eol (parser);
1020 parser->error = save_error;
1021 continue;
1023 default:
1024 break;
1027 c_parser_consume_token (parser);
1030 finished:
1031 parser->error = false;
1034 /* CPP's options (initialized by c-opts.c). */
1035 extern cpp_options *cpp_opts;
1037 /* Save the warning flags which are controlled by __extension__. */
1039 static inline int
1040 disable_extension_diagnostics (void)
1042 int ret = (pedantic
1043 | (warn_pointer_arith << 1)
1044 | (warn_traditional << 2)
1045 | (flag_iso << 3)
1046 | (warn_long_long << 4)
1047 | (warn_cxx_compat << 5)
1048 | (warn_overlength_strings << 6));
1049 cpp_opts->cpp_pedantic = pedantic = 0;
1050 warn_pointer_arith = 0;
1051 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1052 flag_iso = 0;
1053 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1054 warn_cxx_compat = 0;
1055 warn_overlength_strings = 0;
1056 return ret;
1059 /* Restore the warning flags which are controlled by __extension__.
1060 FLAGS is the return value from disable_extension_diagnostics. */
1062 static inline void
1063 restore_extension_diagnostics (int flags)
1065 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1066 warn_pointer_arith = (flags >> 1) & 1;
1067 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1068 flag_iso = (flags >> 3) & 1;
1069 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1070 warn_cxx_compat = (flags >> 5) & 1;
1071 warn_overlength_strings = (flags >> 6) & 1;
1074 /* Possibly kinds of declarator to parse. */
1075 typedef enum c_dtr_syn {
1076 /* A normal declarator with an identifier. */
1077 C_DTR_NORMAL,
1078 /* An abstract declarator (maybe empty). */
1079 C_DTR_ABSTRACT,
1080 /* A parameter declarator: may be either, but after a type name does
1081 not redeclare a typedef name as an identifier if it can
1082 alternatively be interpreted as a typedef name; see DR#009,
1083 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1084 following DR#249. For example, given a typedef T, "int T" and
1085 "int *T" are valid parameter declarations redeclaring T, while
1086 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1087 abstract declarators rather than involving redundant parentheses;
1088 the same applies with attributes inside the parentheses before
1089 "T". */
1090 C_DTR_PARM
1091 } c_dtr_syn;
1093 /* The binary operation precedence levels, where 0 is a dummy lowest level
1094 used for the bottom of the stack. */
1095 enum c_parser_prec {
1096 PREC_NONE,
1097 PREC_LOGOR,
1098 PREC_LOGAND,
1099 PREC_BITOR,
1100 PREC_BITXOR,
1101 PREC_BITAND,
1102 PREC_EQ,
1103 PREC_REL,
1104 PREC_SHIFT,
1105 PREC_ADD,
1106 PREC_MULT,
1107 NUM_PRECS
1110 static void c_parser_external_declaration (c_parser *);
1111 static void c_parser_asm_definition (c_parser *);
1112 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1113 bool, bool, tree *);
1114 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1115 static void c_parser_static_assert_declaration (c_parser *);
1116 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1117 bool, enum c_lookahead_kind);
1118 static struct c_typespec c_parser_enum_specifier (c_parser *);
1119 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1120 static tree c_parser_struct_declaration (c_parser *);
1121 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1122 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1123 bool *);
1124 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1125 c_dtr_syn, bool *);
1126 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1127 bool,
1128 struct c_declarator *);
1129 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1130 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1131 tree);
1132 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1133 static tree c_parser_simple_asm_expr (c_parser *);
1134 static tree c_parser_attributes (c_parser *);
1135 static struct c_type_name *c_parser_type_name (c_parser *);
1136 static struct c_expr c_parser_initializer (c_parser *);
1137 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1138 static void c_parser_initelt (c_parser *, struct obstack *);
1139 static void c_parser_initval (c_parser *, struct c_expr *,
1140 struct obstack *);
1141 static tree c_parser_compound_statement (c_parser *);
1142 static void c_parser_compound_statement_nostart (c_parser *);
1143 static void c_parser_label (c_parser *);
1144 static void c_parser_statement (c_parser *);
1145 static void c_parser_statement_after_labels (c_parser *);
1146 static void c_parser_if_statement (c_parser *);
1147 static void c_parser_switch_statement (c_parser *);
1148 static void c_parser_while_statement (c_parser *);
1149 static void c_parser_do_statement (c_parser *);
1150 static void c_parser_for_statement (c_parser *);
1151 static tree c_parser_asm_statement (c_parser *);
1152 static tree c_parser_asm_operands (c_parser *, bool);
1153 static tree c_parser_asm_goto_operands (c_parser *);
1154 static tree c_parser_asm_clobbers (c_parser *);
1155 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1156 static struct c_expr c_parser_conditional_expression (c_parser *,
1157 struct c_expr *);
1158 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1159 enum c_parser_prec);
1160 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1161 static struct c_expr c_parser_unary_expression (c_parser *);
1162 static struct c_expr c_parser_sizeof_expression (c_parser *);
1163 static struct c_expr c_parser_alignof_expression (c_parser *);
1164 static struct c_expr c_parser_postfix_expression (c_parser *);
1165 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1166 struct c_type_name *,
1167 location_t);
1168 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1169 location_t loc,
1170 struct c_expr);
1171 static struct c_expr c_parser_expression (c_parser *);
1172 static struct c_expr c_parser_expression_conv (c_parser *);
1173 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1174 VEC(tree,gc) **);
1175 static void c_parser_omp_construct (c_parser *);
1176 static void c_parser_omp_threadprivate (c_parser *);
1177 static void c_parser_omp_barrier (c_parser *);
1178 static void c_parser_omp_flush (c_parser *);
1179 static void c_parser_omp_taskwait (c_parser *);
1180 static void c_parser_omp_taskyield (c_parser *);
1182 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1183 static bool c_parser_pragma (c_parser *, enum pragma_context);
1185 /* These Objective-C parser functions are only ever called when
1186 compiling Objective-C. */
1187 static void c_parser_objc_class_definition (c_parser *, tree);
1188 static void c_parser_objc_class_instance_variables (c_parser *);
1189 static void c_parser_objc_class_declaration (c_parser *);
1190 static void c_parser_objc_alias_declaration (c_parser *);
1191 static void c_parser_objc_protocol_definition (c_parser *, tree);
1192 static bool c_parser_objc_method_type (c_parser *);
1193 static void c_parser_objc_method_definition (c_parser *);
1194 static void c_parser_objc_methodprotolist (c_parser *);
1195 static void c_parser_objc_methodproto (c_parser *);
1196 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1197 static tree c_parser_objc_type_name (c_parser *);
1198 static tree c_parser_objc_protocol_refs (c_parser *);
1199 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1200 static void c_parser_objc_synchronized_statement (c_parser *);
1201 static tree c_parser_objc_selector (c_parser *);
1202 static tree c_parser_objc_selector_arg (c_parser *);
1203 static tree c_parser_objc_receiver (c_parser *);
1204 static tree c_parser_objc_message_args (c_parser *);
1205 static tree c_parser_objc_keywordexpr (c_parser *);
1206 static void c_parser_objc_at_property_declaration (c_parser *);
1207 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1208 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1209 static bool c_parser_objc_diagnose_bad_element_prefix
1210 (c_parser *, struct c_declspecs *);
1212 /* Parse a translation unit (C90 6.7, C99 6.9).
1214 translation-unit:
1215 external-declarations
1217 external-declarations:
1218 external-declaration
1219 external-declarations external-declaration
1221 GNU extensions:
1223 translation-unit:
1224 empty
1227 static void
1228 c_parser_translation_unit (c_parser *parser)
1230 if (c_parser_next_token_is (parser, CPP_EOF))
1232 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1233 "ISO C forbids an empty translation unit");
1235 else
1237 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1238 mark_valid_location_for_stdc_pragma (false);
1241 ggc_collect ();
1242 c_parser_external_declaration (parser);
1243 obstack_free (&parser_obstack, obstack_position);
1245 while (c_parser_next_token_is_not (parser, CPP_EOF));
1249 /* Parse an external declaration (C90 6.7, C99 6.9).
1251 external-declaration:
1252 function-definition
1253 declaration
1255 GNU extensions:
1257 external-declaration:
1258 asm-definition
1260 __extension__ external-declaration
1262 Objective-C:
1264 external-declaration:
1265 objc-class-definition
1266 objc-class-declaration
1267 objc-alias-declaration
1268 objc-protocol-definition
1269 objc-method-definition
1270 @end
1273 static void
1274 c_parser_external_declaration (c_parser *parser)
1276 int ext;
1277 switch (c_parser_peek_token (parser)->type)
1279 case CPP_KEYWORD:
1280 switch (c_parser_peek_token (parser)->keyword)
1282 case RID_EXTENSION:
1283 ext = disable_extension_diagnostics ();
1284 c_parser_consume_token (parser);
1285 c_parser_external_declaration (parser);
1286 restore_extension_diagnostics (ext);
1287 break;
1288 case RID_ASM:
1289 c_parser_asm_definition (parser);
1290 break;
1291 case RID_AT_INTERFACE:
1292 case RID_AT_IMPLEMENTATION:
1293 gcc_assert (c_dialect_objc ());
1294 c_parser_objc_class_definition (parser, NULL_TREE);
1295 break;
1296 case RID_AT_CLASS:
1297 gcc_assert (c_dialect_objc ());
1298 c_parser_objc_class_declaration (parser);
1299 break;
1300 case RID_AT_ALIAS:
1301 gcc_assert (c_dialect_objc ());
1302 c_parser_objc_alias_declaration (parser);
1303 break;
1304 case RID_AT_PROTOCOL:
1305 gcc_assert (c_dialect_objc ());
1306 c_parser_objc_protocol_definition (parser, NULL_TREE);
1307 break;
1308 case RID_AT_PROPERTY:
1309 gcc_assert (c_dialect_objc ());
1310 c_parser_objc_at_property_declaration (parser);
1311 break;
1312 case RID_AT_SYNTHESIZE:
1313 gcc_assert (c_dialect_objc ());
1314 c_parser_objc_at_synthesize_declaration (parser);
1315 break;
1316 case RID_AT_DYNAMIC:
1317 gcc_assert (c_dialect_objc ());
1318 c_parser_objc_at_dynamic_declaration (parser);
1319 break;
1320 case RID_AT_END:
1321 gcc_assert (c_dialect_objc ());
1322 c_parser_consume_token (parser);
1323 objc_finish_implementation ();
1324 break;
1325 default:
1326 goto decl_or_fndef;
1328 break;
1329 case CPP_SEMICOLON:
1330 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
1331 "ISO C does not allow extra %<;%> outside of a function");
1332 c_parser_consume_token (parser);
1333 break;
1334 case CPP_PRAGMA:
1335 mark_valid_location_for_stdc_pragma (true);
1336 c_parser_pragma (parser, pragma_external);
1337 mark_valid_location_for_stdc_pragma (false);
1338 break;
1339 case CPP_PLUS:
1340 case CPP_MINUS:
1341 if (c_dialect_objc ())
1343 c_parser_objc_method_definition (parser);
1344 break;
1346 /* Else fall through, and yield a syntax error trying to parse
1347 as a declaration or function definition. */
1348 default:
1349 decl_or_fndef:
1350 /* A declaration or a function definition (or, in Objective-C,
1351 an @interface or @protocol with prefix attributes). We can
1352 only tell which after parsing the declaration specifiers, if
1353 any, and the first declarator. */
1354 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1355 break;
1359 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1360 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1361 accepted; otherwise (old-style parameter declarations) only other
1362 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1363 assertion is accepted; otherwise (old-style parameter declarations)
1364 it is not. If NESTED is true, we are inside a function or parsing
1365 old-style parameter declarations; any functions encountered are
1366 nested functions and declaration specifiers are required; otherwise
1367 we are at top level and functions are normal functions and
1368 declaration specifiers may be optional. If EMPTY_OK is true, empty
1369 declarations are OK (subject to all other constraints); otherwise
1370 (old-style parameter declarations) they are diagnosed. If
1371 START_ATTR_OK is true, the declaration specifiers may start with
1372 attributes; otherwise they may not.
1373 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1374 declaration when parsing an Objective-C foreach statement.
1376 declaration:
1377 declaration-specifiers init-declarator-list[opt] ;
1378 static_assert-declaration
1380 function-definition:
1381 declaration-specifiers[opt] declarator declaration-list[opt]
1382 compound-statement
1384 declaration-list:
1385 declaration
1386 declaration-list declaration
1388 init-declarator-list:
1389 init-declarator
1390 init-declarator-list , init-declarator
1392 init-declarator:
1393 declarator simple-asm-expr[opt] attributes[opt]
1394 declarator simple-asm-expr[opt] attributes[opt] = initializer
1396 GNU extensions:
1398 nested-function-definition:
1399 declaration-specifiers declarator declaration-list[opt]
1400 compound-statement
1402 Objective-C:
1403 attributes objc-class-definition
1404 attributes objc-category-definition
1405 attributes objc-protocol-definition
1407 The simple-asm-expr and attributes are GNU extensions.
1409 This function does not handle __extension__; that is handled in its
1410 callers. ??? Following the old parser, __extension__ may start
1411 external declarations, declarations in functions and declarations
1412 at the start of "for" loops, but not old-style parameter
1413 declarations.
1415 C99 requires declaration specifiers in a function definition; the
1416 absence is diagnosed through the diagnosis of implicit int. In GNU
1417 C we also allow but diagnose declarations without declaration
1418 specifiers, but only at top level (elsewhere they conflict with
1419 other syntax).
1421 In Objective-C, declarations of the looping variable in a foreach
1422 statement are exceptionally terminated by 'in' (for example, 'for
1423 (NSObject *object in array) { ... }').
1425 OpenMP:
1427 declaration:
1428 threadprivate-directive */
1430 static void
1431 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1432 bool static_assert_ok, bool empty_ok,
1433 bool nested, bool start_attr_ok,
1434 tree *objc_foreach_object_declaration)
1436 struct c_declspecs *specs;
1437 tree prefix_attrs;
1438 tree all_prefix_attrs;
1439 bool diagnosed_no_specs = false;
1440 location_t here = c_parser_peek_token (parser)->location;
1442 if (static_assert_ok
1443 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1445 c_parser_static_assert_declaration (parser);
1446 return;
1448 specs = build_null_declspecs ();
1450 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1451 if (c_parser_peek_token (parser)->type == CPP_NAME
1452 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1453 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1454 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1455 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1457 error_at (here, "unknown type name %qE",
1458 c_parser_peek_token (parser)->value);
1460 /* Parse declspecs normally to get a correct pointer type, but avoid
1461 a further "fails to be a type name" error. Refuse nested functions
1462 since it is not how the user likely wants us to recover. */
1463 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1464 c_parser_peek_token (parser)->keyword = RID_VOID;
1465 c_parser_peek_token (parser)->value = error_mark_node;
1466 fndef_ok = !nested;
1469 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1470 if (parser->error)
1472 c_parser_skip_to_end_of_block_or_statement (parser);
1473 return;
1475 if (nested && !specs->declspecs_seen_p)
1477 c_parser_error (parser, "expected declaration specifiers");
1478 c_parser_skip_to_end_of_block_or_statement (parser);
1479 return;
1481 finish_declspecs (specs);
1482 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1484 if (empty_ok)
1485 shadow_tag (specs);
1486 else
1488 shadow_tag_warned (specs, 1);
1489 pedwarn (here, 0, "empty declaration");
1491 c_parser_consume_token (parser);
1492 return;
1495 /* Provide better error recovery. Note that a type name here is usually
1496 better diagnosed as a redeclaration. */
1497 if (empty_ok
1498 && specs->typespec_kind == ctsk_tagdef
1499 && c_parser_next_token_starts_declspecs (parser)
1500 && !c_parser_next_token_is (parser, CPP_NAME))
1502 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1503 parser->error = false;
1504 shadow_tag_warned (specs, 1);
1505 return;
1507 else if (c_dialect_objc ())
1509 /* Prefix attributes are an error on method decls. */
1510 switch (c_parser_peek_token (parser)->type)
1512 case CPP_PLUS:
1513 case CPP_MINUS:
1514 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1515 return;
1516 if (specs->attrs)
1518 warning_at (c_parser_peek_token (parser)->location,
1519 OPT_Wattributes,
1520 "prefix attributes are ignored for methods");
1521 specs->attrs = NULL_TREE;
1523 if (fndef_ok)
1524 c_parser_objc_method_definition (parser);
1525 else
1526 c_parser_objc_methodproto (parser);
1527 return;
1528 break;
1529 default:
1530 break;
1532 /* This is where we parse 'attributes @interface ...',
1533 'attributes @implementation ...', 'attributes @protocol ...'
1534 (where attributes could be, for example, __attribute__
1535 ((deprecated)).
1537 switch (c_parser_peek_token (parser)->keyword)
1539 case RID_AT_INTERFACE:
1541 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1542 return;
1543 c_parser_objc_class_definition (parser, specs->attrs);
1544 return;
1546 break;
1547 case RID_AT_IMPLEMENTATION:
1549 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1550 return;
1551 if (specs->attrs)
1553 warning_at (c_parser_peek_token (parser)->location,
1554 OPT_Wattributes,
1555 "prefix attributes are ignored for implementations");
1556 specs->attrs = NULL_TREE;
1558 c_parser_objc_class_definition (parser, NULL_TREE);
1559 return;
1561 break;
1562 case RID_AT_PROTOCOL:
1564 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1565 return;
1566 c_parser_objc_protocol_definition (parser, specs->attrs);
1567 return;
1569 break;
1570 case RID_AT_ALIAS:
1571 case RID_AT_CLASS:
1572 case RID_AT_END:
1573 case RID_AT_PROPERTY:
1574 if (specs->attrs)
1576 c_parser_error (parser, "unexpected attribute");
1577 specs->attrs = NULL;
1579 break;
1580 default:
1581 break;
1585 pending_xref_error ();
1586 prefix_attrs = specs->attrs;
1587 all_prefix_attrs = prefix_attrs;
1588 specs->attrs = NULL_TREE;
1589 while (true)
1591 struct c_declarator *declarator;
1592 bool dummy = false;
1593 timevar_id_t tv;
1594 tree fnbody;
1595 /* Declaring either one or more declarators (in which case we
1596 should diagnose if there were no declaration specifiers) or a
1597 function definition (in which case the diagnostic for
1598 implicit int suffices). */
1599 declarator = c_parser_declarator (parser,
1600 specs->typespec_kind != ctsk_none,
1601 C_DTR_NORMAL, &dummy);
1602 if (declarator == NULL)
1604 c_parser_skip_to_end_of_block_or_statement (parser);
1605 return;
1607 if (c_parser_next_token_is (parser, CPP_EQ)
1608 || c_parser_next_token_is (parser, CPP_COMMA)
1609 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1610 || c_parser_next_token_is_keyword (parser, RID_ASM)
1611 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1612 || c_parser_next_token_is_keyword (parser, RID_IN))
1614 tree asm_name = NULL_TREE;
1615 tree postfix_attrs = NULL_TREE;
1616 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1618 diagnosed_no_specs = true;
1619 pedwarn (here, 0, "data definition has no type or storage class");
1621 /* Having seen a data definition, there cannot now be a
1622 function definition. */
1623 fndef_ok = false;
1624 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1625 asm_name = c_parser_simple_asm_expr (parser);
1626 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1627 postfix_attrs = c_parser_attributes (parser);
1628 if (c_parser_next_token_is (parser, CPP_EQ))
1630 tree d;
1631 struct c_expr init;
1632 location_t init_loc;
1633 c_parser_consume_token (parser);
1634 /* The declaration of the variable is in effect while
1635 its initializer is parsed. */
1636 d = start_decl (declarator, specs, true,
1637 chainon (postfix_attrs, all_prefix_attrs));
1638 if (!d)
1639 d = error_mark_node;
1640 start_init (d, asm_name, global_bindings_p ());
1641 init_loc = c_parser_peek_token (parser)->location;
1642 init = c_parser_initializer (parser);
1643 finish_init ();
1644 if (d != error_mark_node)
1646 maybe_warn_string_init (TREE_TYPE (d), init);
1647 finish_decl (d, init_loc, init.value,
1648 init.original_type, asm_name);
1651 else
1653 tree d = start_decl (declarator, specs, false,
1654 chainon (postfix_attrs,
1655 all_prefix_attrs));
1656 if (d)
1657 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1658 NULL_TREE, asm_name);
1660 if (c_parser_next_token_is_keyword (parser, RID_IN))
1662 if (d)
1663 *objc_foreach_object_declaration = d;
1664 else
1665 *objc_foreach_object_declaration = error_mark_node;
1668 if (c_parser_next_token_is (parser, CPP_COMMA))
1670 c_parser_consume_token (parser);
1671 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1672 all_prefix_attrs = chainon (c_parser_attributes (parser),
1673 prefix_attrs);
1674 else
1675 all_prefix_attrs = prefix_attrs;
1676 continue;
1678 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1680 c_parser_consume_token (parser);
1681 return;
1683 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1685 /* This can only happen in Objective-C: we found the
1686 'in' that terminates the declaration inside an
1687 Objective-C foreach statement. Do not consume the
1688 token, so that the caller can use it to determine
1689 that this indeed is a foreach context. */
1690 return;
1692 else
1694 c_parser_error (parser, "expected %<,%> or %<;%>");
1695 c_parser_skip_to_end_of_block_or_statement (parser);
1696 return;
1699 else if (!fndef_ok)
1701 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1702 "%<asm%> or %<__attribute__%>");
1703 c_parser_skip_to_end_of_block_or_statement (parser);
1704 return;
1706 /* Function definition (nested or otherwise). */
1707 if (nested)
1709 pedwarn (here, OPT_pedantic, "ISO C forbids nested functions");
1710 c_push_function_context ();
1712 if (!start_function (specs, declarator, all_prefix_attrs))
1714 /* This can appear in many cases looking nothing like a
1715 function definition, so we don't give a more specific
1716 error suggesting there was one. */
1717 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1718 "or %<__attribute__%>");
1719 if (nested)
1720 c_pop_function_context ();
1721 break;
1724 if (DECL_DECLARED_INLINE_P (current_function_decl))
1725 tv = TV_PARSE_INLINE;
1726 else
1727 tv = TV_PARSE_FUNC;
1728 timevar_push (tv);
1730 /* Parse old-style parameter declarations. ??? Attributes are
1731 not allowed to start declaration specifiers here because of a
1732 syntax conflict between a function declaration with attribute
1733 suffix and a function definition with an attribute prefix on
1734 first old-style parameter declaration. Following the old
1735 parser, they are not accepted on subsequent old-style
1736 parameter declarations either. However, there is no
1737 ambiguity after the first declaration, nor indeed on the
1738 first as long as we don't allow postfix attributes after a
1739 declarator with a nonempty identifier list in a definition;
1740 and postfix attributes have never been accepted here in
1741 function definitions either. */
1742 while (c_parser_next_token_is_not (parser, CPP_EOF)
1743 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1744 c_parser_declaration_or_fndef (parser, false, false, false,
1745 true, false, NULL);
1746 store_parm_decls ();
1747 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1748 = c_parser_peek_token (parser)->location;
1749 fnbody = c_parser_compound_statement (parser);
1750 if (nested)
1752 tree decl = current_function_decl;
1753 /* Mark nested functions as needing static-chain initially.
1754 lower_nested_functions will recompute it but the
1755 DECL_STATIC_CHAIN flag is also used before that happens,
1756 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1757 DECL_STATIC_CHAIN (decl) = 1;
1758 add_stmt (fnbody);
1759 finish_function ();
1760 c_pop_function_context ();
1761 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1763 else
1765 add_stmt (fnbody);
1766 finish_function ();
1769 timevar_pop (tv);
1770 break;
1774 /* Parse an asm-definition (asm() outside a function body). This is a
1775 GNU extension.
1777 asm-definition:
1778 simple-asm-expr ;
1781 static void
1782 c_parser_asm_definition (c_parser *parser)
1784 tree asm_str = c_parser_simple_asm_expr (parser);
1785 if (asm_str)
1786 cgraph_add_asm_node (asm_str);
1787 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1790 /* Parse a static assertion (C1X N1425 6.7.10).
1792 static_assert-declaration:
1793 static_assert-declaration-no-semi ;
1796 static void
1797 c_parser_static_assert_declaration (c_parser *parser)
1799 c_parser_static_assert_declaration_no_semi (parser);
1800 if (parser->error
1801 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1802 c_parser_skip_to_end_of_block_or_statement (parser);
1805 /* Parse a static assertion (C1X N1425 6.7.10), without the trailing
1806 semicolon.
1808 static_assert-declaration-no-semi:
1809 _Static_assert ( constant-expression , string-literal )
1812 static void
1813 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1815 location_t assert_loc, value_loc;
1816 tree value;
1817 tree string;
1819 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1820 assert_loc = c_parser_peek_token (parser)->location;
1821 if (!flag_isoc1x)
1823 if (flag_isoc99)
1824 pedwarn (assert_loc, OPT_pedantic,
1825 "ISO C99 does not support %<_Static_assert%>");
1826 else
1827 pedwarn (assert_loc, OPT_pedantic,
1828 "ISO C90 does not support %<_Static_assert%>");
1830 c_parser_consume_token (parser);
1831 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1832 return;
1833 value_loc = c_parser_peek_token (parser)->location;
1834 value = c_parser_expr_no_commas (parser, NULL).value;
1835 parser->lex_untranslated_string = true;
1836 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1838 parser->lex_untranslated_string = false;
1839 return;
1841 switch (c_parser_peek_token (parser)->type)
1843 case CPP_STRING:
1844 case CPP_STRING16:
1845 case CPP_STRING32:
1846 case CPP_WSTRING:
1847 case CPP_UTF8STRING:
1848 string = c_parser_peek_token (parser)->value;
1849 c_parser_consume_token (parser);
1850 parser->lex_untranslated_string = false;
1851 break;
1852 default:
1853 c_parser_error (parser, "expected string literal");
1854 parser->lex_untranslated_string = false;
1855 return;
1857 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1859 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1861 error_at (value_loc, "expression in static assertion is not an integer");
1862 return;
1864 if (TREE_CODE (value) != INTEGER_CST)
1866 value = c_fully_fold (value, false, NULL);
1867 if (TREE_CODE (value) == INTEGER_CST)
1868 pedwarn (value_loc, OPT_pedantic, "expression in static assertion "
1869 "is not an integer constant expression");
1871 if (TREE_CODE (value) != INTEGER_CST)
1873 error_at (value_loc, "expression in static assertion is not constant");
1874 return;
1876 constant_expression_warning (value);
1877 if (integer_zerop (value))
1878 error_at (assert_loc, "static assertion failed: %E", string);
1881 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1882 6.7), adding them to SPECS (which may already include some).
1883 Storage class specifiers are accepted iff SCSPEC_OK; type
1884 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1885 the start iff START_ATTR_OK.
1887 declaration-specifiers:
1888 storage-class-specifier declaration-specifiers[opt]
1889 type-specifier declaration-specifiers[opt]
1890 type-qualifier declaration-specifiers[opt]
1891 function-specifier declaration-specifiers[opt]
1893 Function specifiers (inline) are from C99, and are currently
1894 handled as storage class specifiers, as is __thread.
1896 C90 6.5.1, C99 6.7.1:
1897 storage-class-specifier:
1898 typedef
1899 extern
1900 static
1901 auto
1902 register
1904 C99 6.7.4:
1905 function-specifier:
1906 inline
1908 C90 6.5.2, C99 6.7.2:
1909 type-specifier:
1910 void
1911 char
1912 short
1914 long
1915 float
1916 double
1917 signed
1918 unsigned
1919 _Bool
1920 _Complex
1921 [_Imaginary removed in C99 TC2]
1922 struct-or-union-specifier
1923 enum-specifier
1924 typedef-name
1926 (_Bool and _Complex are new in C99.)
1928 C90 6.5.3, C99 6.7.3:
1930 type-qualifier:
1931 const
1932 restrict
1933 volatile
1934 address-space-qualifier
1936 (restrict is new in C99.)
1938 GNU extensions:
1940 declaration-specifiers:
1941 attributes declaration-specifiers[opt]
1943 type-qualifier:
1944 address-space
1946 address-space:
1947 identifier recognized by the target
1949 storage-class-specifier:
1950 __thread
1952 type-specifier:
1953 typeof-specifier
1954 __int128
1955 _Decimal32
1956 _Decimal64
1957 _Decimal128
1958 _Fract
1959 _Accum
1960 _Sat
1962 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1963 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1965 Objective-C:
1967 type-specifier:
1968 class-name objc-protocol-refs[opt]
1969 typedef-name objc-protocol-refs
1970 objc-protocol-refs
1973 static void
1974 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1975 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1976 enum c_lookahead_kind la)
1978 bool attrs_ok = start_attr_ok;
1979 bool seen_type = specs->typespec_kind != ctsk_none;
1981 if (!typespec_ok)
1982 gcc_assert (la == cla_prefer_id);
1984 while (c_parser_next_token_is (parser, CPP_NAME)
1985 || c_parser_next_token_is (parser, CPP_KEYWORD)
1986 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
1988 struct c_typespec t;
1989 tree attrs;
1990 location_t loc = c_parser_peek_token (parser)->location;
1992 /* If we cannot accept a type, exit if the next token must start
1993 one. Also, if we already have seen a tagged definition,
1994 a typename would be an error anyway and likely the user
1995 has simply forgotten a semicolon, so we exit. */
1996 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
1997 && c_parser_next_tokens_start_typename (parser, la)
1998 && !c_parser_next_token_is_qualifier (parser))
1999 break;
2001 if (c_parser_next_token_is (parser, CPP_NAME))
2003 tree value = c_parser_peek_token (parser)->value;
2004 c_id_kind kind = c_parser_peek_token (parser)->id_kind;
2006 if (kind == C_ID_ADDRSPACE)
2008 addr_space_t as
2009 = c_parser_peek_token (parser)->keyword - RID_FIRST_ADDR_SPACE;
2010 declspecs_add_addrspace (specs, as);
2011 c_parser_consume_token (parser);
2012 attrs_ok = true;
2013 continue;
2016 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2018 /* If we cannot accept a type, and the next token must start one,
2019 exit. Do the same if we already have seen a tagged definition,
2020 since it would be an error anyway and likely the user has simply
2021 forgotten a semicolon. */
2022 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2023 break;
2025 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2026 a C_ID_CLASSNAME. */
2027 c_parser_consume_token (parser);
2028 seen_type = true;
2029 attrs_ok = true;
2030 if (kind == C_ID_ID)
2032 error ("unknown type name %qE", value);
2033 t.kind = ctsk_typedef;
2034 t.spec = error_mark_node;
2036 else if (kind == C_ID_TYPENAME
2037 && (!c_dialect_objc ()
2038 || c_parser_next_token_is_not (parser, CPP_LESS)))
2040 t.kind = ctsk_typedef;
2041 /* For a typedef name, record the meaning, not the name.
2042 In case of 'foo foo, bar;'. */
2043 t.spec = lookup_name (value);
2045 else
2047 tree proto = NULL_TREE;
2048 gcc_assert (c_dialect_objc ());
2049 t.kind = ctsk_objc;
2050 if (c_parser_next_token_is (parser, CPP_LESS))
2051 proto = c_parser_objc_protocol_refs (parser);
2052 t.spec = objc_get_protocol_qualified_type (value, proto);
2054 t.expr = NULL_TREE;
2055 t.expr_const_operands = true;
2056 declspecs_add_type (loc, specs, t);
2057 continue;
2059 if (c_parser_next_token_is (parser, CPP_LESS))
2061 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2062 nisse@lysator.liu.se. */
2063 tree proto;
2064 gcc_assert (c_dialect_objc ());
2065 if (!typespec_ok || seen_type)
2066 break;
2067 proto = c_parser_objc_protocol_refs (parser);
2068 t.kind = ctsk_objc;
2069 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2070 t.expr = NULL_TREE;
2071 t.expr_const_operands = true;
2072 declspecs_add_type (loc, specs, t);
2073 continue;
2075 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2076 switch (c_parser_peek_token (parser)->keyword)
2078 case RID_STATIC:
2079 case RID_EXTERN:
2080 case RID_REGISTER:
2081 case RID_TYPEDEF:
2082 case RID_INLINE:
2083 case RID_AUTO:
2084 case RID_THREAD:
2085 if (!scspec_ok)
2086 goto out;
2087 attrs_ok = true;
2088 /* TODO: Distinguish between function specifiers (inline)
2089 and storage class specifiers, either here or in
2090 declspecs_add_scspec. */
2091 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value);
2092 c_parser_consume_token (parser);
2093 break;
2094 case RID_UNSIGNED:
2095 case RID_LONG:
2096 case RID_INT128:
2097 case RID_SHORT:
2098 case RID_SIGNED:
2099 case RID_COMPLEX:
2100 case RID_INT:
2101 case RID_CHAR:
2102 case RID_FLOAT:
2103 case RID_DOUBLE:
2104 case RID_VOID:
2105 case RID_DFLOAT32:
2106 case RID_DFLOAT64:
2107 case RID_DFLOAT128:
2108 case RID_BOOL:
2109 case RID_FRACT:
2110 case RID_ACCUM:
2111 case RID_SAT:
2112 if (!typespec_ok)
2113 goto out;
2114 attrs_ok = true;
2115 seen_type = true;
2116 if (c_dialect_objc ())
2117 parser->objc_need_raw_identifier = true;
2118 t.kind = ctsk_resword;
2119 t.spec = c_parser_peek_token (parser)->value;
2120 t.expr = NULL_TREE;
2121 t.expr_const_operands = true;
2122 declspecs_add_type (loc, specs, t);
2123 c_parser_consume_token (parser);
2124 break;
2125 case RID_ENUM:
2126 if (!typespec_ok)
2127 goto out;
2128 attrs_ok = true;
2129 seen_type = true;
2130 t = c_parser_enum_specifier (parser);
2131 declspecs_add_type (loc, specs, t);
2132 break;
2133 case RID_STRUCT:
2134 case RID_UNION:
2135 if (!typespec_ok)
2136 goto out;
2137 attrs_ok = true;
2138 seen_type = true;
2139 t = c_parser_struct_or_union_specifier (parser);
2140 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2141 declspecs_add_type (loc, specs, t);
2142 break;
2143 case RID_TYPEOF:
2144 /* ??? The old parser rejected typeof after other type
2145 specifiers, but is a syntax error the best way of
2146 handling this? */
2147 if (!typespec_ok || seen_type)
2148 goto out;
2149 attrs_ok = true;
2150 seen_type = true;
2151 t = c_parser_typeof_specifier (parser);
2152 declspecs_add_type (loc, specs, t);
2153 break;
2154 case RID_CONST:
2155 case RID_VOLATILE:
2156 case RID_RESTRICT:
2157 attrs_ok = true;
2158 declspecs_add_qual (specs, c_parser_peek_token (parser)->value);
2159 c_parser_consume_token (parser);
2160 break;
2161 case RID_ATTRIBUTE:
2162 if (!attrs_ok)
2163 goto out;
2164 attrs = c_parser_attributes (parser);
2165 declspecs_add_attrs (specs, attrs);
2166 break;
2167 default:
2168 goto out;
2171 out: ;
2174 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2176 enum-specifier:
2177 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2178 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2179 enum attributes[opt] identifier
2181 The form with trailing comma is new in C99. The forms with
2182 attributes are GNU extensions. In GNU C, we accept any expression
2183 without commas in the syntax (assignment expressions, not just
2184 conditional expressions); assignment expressions will be diagnosed
2185 as non-constant.
2187 enumerator-list:
2188 enumerator
2189 enumerator-list , enumerator
2191 enumerator:
2192 enumeration-constant
2193 enumeration-constant = constant-expression
2196 static struct c_typespec
2197 c_parser_enum_specifier (c_parser *parser)
2199 struct c_typespec ret;
2200 tree attrs;
2201 tree ident = NULL_TREE;
2202 location_t enum_loc;
2203 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2204 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2205 enum_loc = c_parser_peek_token (parser)->location;
2206 c_parser_consume_token (parser);
2207 attrs = c_parser_attributes (parser);
2208 enum_loc = c_parser_peek_token (parser)->location;
2209 /* Set the location in case we create a decl now. */
2210 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2211 if (c_parser_next_token_is (parser, CPP_NAME))
2213 ident = c_parser_peek_token (parser)->value;
2214 ident_loc = c_parser_peek_token (parser)->location;
2215 enum_loc = ident_loc;
2216 c_parser_consume_token (parser);
2218 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2220 /* Parse an enum definition. */
2221 struct c_enum_contents the_enum;
2222 tree type;
2223 tree postfix_attrs;
2224 /* We chain the enumerators in reverse order, then put them in
2225 forward order at the end. */
2226 tree values;
2227 timevar_push (TV_PARSE_ENUM);
2228 type = start_enum (enum_loc, &the_enum, ident);
2229 values = NULL_TREE;
2230 c_parser_consume_token (parser);
2231 while (true)
2233 tree enum_id;
2234 tree enum_value;
2235 tree enum_decl;
2236 bool seen_comma;
2237 c_token *token;
2238 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2239 location_t decl_loc, value_loc;
2240 if (c_parser_next_token_is_not (parser, CPP_NAME))
2242 c_parser_error (parser, "expected identifier");
2243 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2244 values = error_mark_node;
2245 break;
2247 token = c_parser_peek_token (parser);
2248 enum_id = token->value;
2249 /* Set the location in case we create a decl now. */
2250 c_parser_set_source_position_from_token (token);
2251 decl_loc = value_loc = token->location;
2252 c_parser_consume_token (parser);
2253 if (c_parser_next_token_is (parser, CPP_EQ))
2255 c_parser_consume_token (parser);
2256 value_loc = c_parser_peek_token (parser)->location;
2257 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2259 else
2260 enum_value = NULL_TREE;
2261 enum_decl = build_enumerator (decl_loc, value_loc,
2262 &the_enum, enum_id, enum_value);
2263 TREE_CHAIN (enum_decl) = values;
2264 values = enum_decl;
2265 seen_comma = false;
2266 if (c_parser_next_token_is (parser, CPP_COMMA))
2268 comma_loc = c_parser_peek_token (parser)->location;
2269 seen_comma = true;
2270 c_parser_consume_token (parser);
2272 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2274 if (seen_comma && !flag_isoc99)
2275 pedwarn (comma_loc, OPT_pedantic, "comma at end of enumerator list");
2276 c_parser_consume_token (parser);
2277 break;
2279 if (!seen_comma)
2281 c_parser_error (parser, "expected %<,%> or %<}%>");
2282 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2283 values = error_mark_node;
2284 break;
2287 postfix_attrs = c_parser_attributes (parser);
2288 ret.spec = finish_enum (type, nreverse (values),
2289 chainon (attrs, postfix_attrs));
2290 ret.kind = ctsk_tagdef;
2291 ret.expr = NULL_TREE;
2292 ret.expr_const_operands = true;
2293 timevar_pop (TV_PARSE_ENUM);
2294 return ret;
2296 else if (!ident)
2298 c_parser_error (parser, "expected %<{%>");
2299 ret.spec = error_mark_node;
2300 ret.kind = ctsk_tagref;
2301 ret.expr = NULL_TREE;
2302 ret.expr_const_operands = true;
2303 return ret;
2305 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2306 /* In ISO C, enumerated types can be referred to only if already
2307 defined. */
2308 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2310 gcc_assert (ident);
2311 pedwarn (enum_loc, OPT_pedantic,
2312 "ISO C forbids forward references to %<enum%> types");
2314 return ret;
2317 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2319 struct-or-union-specifier:
2320 struct-or-union attributes[opt] identifier[opt]
2321 { struct-contents } attributes[opt]
2322 struct-or-union attributes[opt] identifier
2324 struct-contents:
2325 struct-declaration-list
2327 struct-declaration-list:
2328 struct-declaration ;
2329 struct-declaration-list struct-declaration ;
2331 GNU extensions:
2333 struct-contents:
2334 empty
2335 struct-declaration
2336 struct-declaration-list struct-declaration
2338 struct-declaration-list:
2339 struct-declaration-list ;
2342 (Note that in the syntax here, unlike that in ISO C, the semicolons
2343 are included here rather than in struct-declaration, in order to
2344 describe the syntax with extra semicolons and missing semicolon at
2345 end.)
2347 Objective-C:
2349 struct-declaration-list:
2350 @defs ( class-name )
2352 (Note this does not include a trailing semicolon, but can be
2353 followed by further declarations, and gets a pedwarn-if-pedantic
2354 when followed by a semicolon.) */
2356 static struct c_typespec
2357 c_parser_struct_or_union_specifier (c_parser *parser)
2359 struct c_typespec ret;
2360 tree attrs;
2361 tree ident = NULL_TREE;
2362 location_t struct_loc;
2363 location_t ident_loc = UNKNOWN_LOCATION;
2364 enum tree_code code;
2365 switch (c_parser_peek_token (parser)->keyword)
2367 case RID_STRUCT:
2368 code = RECORD_TYPE;
2369 break;
2370 case RID_UNION:
2371 code = UNION_TYPE;
2372 break;
2373 default:
2374 gcc_unreachable ();
2376 struct_loc = c_parser_peek_token (parser)->location;
2377 c_parser_consume_token (parser);
2378 attrs = c_parser_attributes (parser);
2380 /* Set the location in case we create a decl now. */
2381 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2383 if (c_parser_next_token_is (parser, CPP_NAME))
2385 ident = c_parser_peek_token (parser)->value;
2386 ident_loc = c_parser_peek_token (parser)->location;
2387 struct_loc = ident_loc;
2388 c_parser_consume_token (parser);
2390 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2392 /* Parse a struct or union definition. Start the scope of the
2393 tag before parsing components. */
2394 struct c_struct_parse_info *struct_info;
2395 tree type = start_struct (struct_loc, code, ident, &struct_info);
2396 tree postfix_attrs;
2397 /* We chain the components in reverse order, then put them in
2398 forward order at the end. Each struct-declaration may
2399 declare multiple components (comma-separated), so we must use
2400 chainon to join them, although when parsing each
2401 struct-declaration we can use TREE_CHAIN directly.
2403 The theory behind all this is that there will be more
2404 semicolon separated fields than comma separated fields, and
2405 so we'll be minimizing the number of node traversals required
2406 by chainon. */
2407 tree contents;
2408 timevar_push (TV_PARSE_STRUCT);
2409 contents = NULL_TREE;
2410 c_parser_consume_token (parser);
2411 /* Handle the Objective-C @defs construct,
2412 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2413 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2415 tree name;
2416 gcc_assert (c_dialect_objc ());
2417 c_parser_consume_token (parser);
2418 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2419 goto end_at_defs;
2420 if (c_parser_next_token_is (parser, CPP_NAME)
2421 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2423 name = c_parser_peek_token (parser)->value;
2424 c_parser_consume_token (parser);
2426 else
2428 c_parser_error (parser, "expected class name");
2429 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2430 goto end_at_defs;
2432 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2433 "expected %<)%>");
2434 contents = nreverse (objc_get_class_ivars (name));
2436 end_at_defs:
2437 /* Parse the struct-declarations and semicolons. Problems with
2438 semicolons are diagnosed here; empty structures are diagnosed
2439 elsewhere. */
2440 while (true)
2442 tree decls;
2443 /* Parse any stray semicolon. */
2444 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2446 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
2447 "extra semicolon in struct or union specified");
2448 c_parser_consume_token (parser);
2449 continue;
2451 /* Stop if at the end of the struct or union contents. */
2452 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2454 c_parser_consume_token (parser);
2455 break;
2457 /* Accept #pragmas at struct scope. */
2458 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2460 c_parser_pragma (parser, pragma_external);
2461 continue;
2463 /* Parse some comma-separated declarations, but not the
2464 trailing semicolon if any. */
2465 decls = c_parser_struct_declaration (parser);
2466 contents = chainon (decls, contents);
2467 /* If no semicolon follows, either we have a parse error or
2468 are at the end of the struct or union and should
2469 pedwarn. */
2470 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2471 c_parser_consume_token (parser);
2472 else
2474 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2475 pedwarn (c_parser_peek_token (parser)->location, 0,
2476 "no semicolon at end of struct or union");
2477 else if (parser->error
2478 || !c_parser_next_token_starts_declspecs (parser))
2480 c_parser_error (parser, "expected %<;%>");
2481 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2482 break;
2485 /* If we come here, we have already emitted an error
2486 for an expected `;', identifier or `(', and we also
2487 recovered already. Go on with the next field. */
2490 postfix_attrs = c_parser_attributes (parser);
2491 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2492 chainon (attrs, postfix_attrs), struct_info);
2493 ret.kind = ctsk_tagdef;
2494 ret.expr = NULL_TREE;
2495 ret.expr_const_operands = true;
2496 timevar_pop (TV_PARSE_STRUCT);
2497 return ret;
2499 else if (!ident)
2501 c_parser_error (parser, "expected %<{%>");
2502 ret.spec = error_mark_node;
2503 ret.kind = ctsk_tagref;
2504 ret.expr = NULL_TREE;
2505 ret.expr_const_operands = true;
2506 return ret;
2508 ret = parser_xref_tag (ident_loc, code, ident);
2509 return ret;
2512 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2513 the trailing semicolon.
2515 struct-declaration:
2516 specifier-qualifier-list struct-declarator-list
2517 static_assert-declaration-no-semi
2519 specifier-qualifier-list:
2520 type-specifier specifier-qualifier-list[opt]
2521 type-qualifier specifier-qualifier-list[opt]
2522 attributes specifier-qualifier-list[opt]
2524 struct-declarator-list:
2525 struct-declarator
2526 struct-declarator-list , attributes[opt] struct-declarator
2528 struct-declarator:
2529 declarator attributes[opt]
2530 declarator[opt] : constant-expression attributes[opt]
2532 GNU extensions:
2534 struct-declaration:
2535 __extension__ struct-declaration
2536 specifier-qualifier-list
2538 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2539 of attributes where shown is a GNU extension. In GNU C, we accept
2540 any expression without commas in the syntax (assignment
2541 expressions, not just conditional expressions); assignment
2542 expressions will be diagnosed as non-constant. */
2544 static tree
2545 c_parser_struct_declaration (c_parser *parser)
2547 struct c_declspecs *specs;
2548 tree prefix_attrs;
2549 tree all_prefix_attrs;
2550 tree decls;
2551 location_t decl_loc;
2552 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2554 int ext;
2555 tree decl;
2556 ext = disable_extension_diagnostics ();
2557 c_parser_consume_token (parser);
2558 decl = c_parser_struct_declaration (parser);
2559 restore_extension_diagnostics (ext);
2560 return decl;
2562 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2564 c_parser_static_assert_declaration_no_semi (parser);
2565 return NULL_TREE;
2567 specs = build_null_declspecs ();
2568 decl_loc = c_parser_peek_token (parser)->location;
2569 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2570 if (parser->error)
2571 return NULL_TREE;
2572 if (!specs->declspecs_seen_p)
2574 c_parser_error (parser, "expected specifier-qualifier-list");
2575 return NULL_TREE;
2577 finish_declspecs (specs);
2578 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2579 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2581 tree ret;
2582 if (specs->typespec_kind == ctsk_none)
2584 pedwarn (decl_loc, OPT_pedantic,
2585 "ISO C forbids member declarations with no members");
2586 shadow_tag_warned (specs, pedantic);
2587 ret = NULL_TREE;
2589 else
2591 /* Support for unnamed structs or unions as members of
2592 structs or unions (which is [a] useful and [b] supports
2593 MS P-SDK). */
2594 tree attrs = NULL;
2596 ret = grokfield (c_parser_peek_token (parser)->location,
2597 build_id_declarator (NULL_TREE), specs,
2598 NULL_TREE, &attrs);
2599 if (ret)
2600 decl_attributes (&ret, attrs, 0);
2602 return ret;
2605 /* Provide better error recovery. Note that a type name here is valid,
2606 and will be treated as a field name. */
2607 if (specs->typespec_kind == ctsk_tagdef
2608 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2609 && c_parser_next_token_starts_declspecs (parser)
2610 && !c_parser_next_token_is (parser, CPP_NAME))
2612 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2613 parser->error = false;
2614 return NULL_TREE;
2617 pending_xref_error ();
2618 prefix_attrs = specs->attrs;
2619 all_prefix_attrs = prefix_attrs;
2620 specs->attrs = NULL_TREE;
2621 decls = NULL_TREE;
2622 while (true)
2624 /* Declaring one or more declarators or un-named bit-fields. */
2625 struct c_declarator *declarator;
2626 bool dummy = false;
2627 if (c_parser_next_token_is (parser, CPP_COLON))
2628 declarator = build_id_declarator (NULL_TREE);
2629 else
2630 declarator = c_parser_declarator (parser,
2631 specs->typespec_kind != ctsk_none,
2632 C_DTR_NORMAL, &dummy);
2633 if (declarator == NULL)
2635 c_parser_skip_to_end_of_block_or_statement (parser);
2636 break;
2638 if (c_parser_next_token_is (parser, CPP_COLON)
2639 || c_parser_next_token_is (parser, CPP_COMMA)
2640 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2641 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2642 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2644 tree postfix_attrs = NULL_TREE;
2645 tree width = NULL_TREE;
2646 tree d;
2647 if (c_parser_next_token_is (parser, CPP_COLON))
2649 c_parser_consume_token (parser);
2650 width = c_parser_expr_no_commas (parser, NULL).value;
2652 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2653 postfix_attrs = c_parser_attributes (parser);
2654 d = grokfield (c_parser_peek_token (parser)->location,
2655 declarator, specs, width, &all_prefix_attrs);
2656 decl_attributes (&d, chainon (postfix_attrs,
2657 all_prefix_attrs), 0);
2658 DECL_CHAIN (d) = decls;
2659 decls = d;
2660 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2661 all_prefix_attrs = chainon (c_parser_attributes (parser),
2662 prefix_attrs);
2663 else
2664 all_prefix_attrs = prefix_attrs;
2665 if (c_parser_next_token_is (parser, CPP_COMMA))
2666 c_parser_consume_token (parser);
2667 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2668 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2670 /* Semicolon consumed in caller. */
2671 break;
2673 else
2675 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2676 break;
2679 else
2681 c_parser_error (parser,
2682 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2683 "%<__attribute__%>");
2684 break;
2687 return decls;
2690 /* Parse a typeof specifier (a GNU extension).
2692 typeof-specifier:
2693 typeof ( expression )
2694 typeof ( type-name )
2697 static struct c_typespec
2698 c_parser_typeof_specifier (c_parser *parser)
2700 struct c_typespec ret;
2701 ret.kind = ctsk_typeof;
2702 ret.spec = error_mark_node;
2703 ret.expr = NULL_TREE;
2704 ret.expr_const_operands = true;
2705 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2706 c_parser_consume_token (parser);
2707 c_inhibit_evaluation_warnings++;
2708 in_typeof++;
2709 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2711 c_inhibit_evaluation_warnings--;
2712 in_typeof--;
2713 return ret;
2715 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2717 struct c_type_name *type = c_parser_type_name (parser);
2718 c_inhibit_evaluation_warnings--;
2719 in_typeof--;
2720 if (type != NULL)
2722 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2723 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2726 else
2728 bool was_vm;
2729 location_t here = c_parser_peek_token (parser)->location;
2730 struct c_expr expr = c_parser_expression (parser);
2731 c_inhibit_evaluation_warnings--;
2732 in_typeof--;
2733 if (TREE_CODE (expr.value) == COMPONENT_REF
2734 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2735 error_at (here, "%<typeof%> applied to a bit-field");
2736 mark_exp_read (expr.value);
2737 ret.spec = TREE_TYPE (expr.value);
2738 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2739 /* This is returned with the type so that when the type is
2740 evaluated, this can be evaluated. */
2741 if (was_vm)
2742 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2743 pop_maybe_used (was_vm);
2745 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2746 return ret;
2749 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2750 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2751 be redeclared; otherwise it may not. KIND indicates which kind of
2752 declarator is wanted. Returns a valid declarator except in the
2753 case of a syntax error in which case NULL is returned. *SEEN_ID is
2754 set to true if an identifier being declared is seen; this is used
2755 to diagnose bad forms of abstract array declarators and to
2756 determine whether an identifier list is syntactically permitted.
2758 declarator:
2759 pointer[opt] direct-declarator
2761 direct-declarator:
2762 identifier
2763 ( attributes[opt] declarator )
2764 direct-declarator array-declarator
2765 direct-declarator ( parameter-type-list )
2766 direct-declarator ( identifier-list[opt] )
2768 pointer:
2769 * type-qualifier-list[opt]
2770 * type-qualifier-list[opt] pointer
2772 type-qualifier-list:
2773 type-qualifier
2774 attributes
2775 type-qualifier-list type-qualifier
2776 type-qualifier-list attributes
2778 parameter-type-list:
2779 parameter-list
2780 parameter-list , ...
2782 parameter-list:
2783 parameter-declaration
2784 parameter-list , parameter-declaration
2786 parameter-declaration:
2787 declaration-specifiers declarator attributes[opt]
2788 declaration-specifiers abstract-declarator[opt] attributes[opt]
2790 identifier-list:
2791 identifier
2792 identifier-list , identifier
2794 abstract-declarator:
2795 pointer
2796 pointer[opt] direct-abstract-declarator
2798 direct-abstract-declarator:
2799 ( attributes[opt] abstract-declarator )
2800 direct-abstract-declarator[opt] array-declarator
2801 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2803 GNU extensions:
2805 direct-declarator:
2806 direct-declarator ( parameter-forward-declarations
2807 parameter-type-list[opt] )
2809 direct-abstract-declarator:
2810 direct-abstract-declarator[opt] ( parameter-forward-declarations
2811 parameter-type-list[opt] )
2813 parameter-forward-declarations:
2814 parameter-list ;
2815 parameter-forward-declarations parameter-list ;
2817 The uses of attributes shown above are GNU extensions.
2819 Some forms of array declarator are not included in C99 in the
2820 syntax for abstract declarators; these are disallowed elsewhere.
2821 This may be a defect (DR#289).
2823 This function also accepts an omitted abstract declarator as being
2824 an abstract declarator, although not part of the formal syntax. */
2826 static struct c_declarator *
2827 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2828 bool *seen_id)
2830 /* Parse any initial pointer part. */
2831 if (c_parser_next_token_is (parser, CPP_MULT))
2833 struct c_declspecs *quals_attrs = build_null_declspecs ();
2834 struct c_declarator *inner;
2835 c_parser_consume_token (parser);
2836 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2837 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2838 if (inner == NULL)
2839 return NULL;
2840 else
2841 return make_pointer_declarator (quals_attrs, inner);
2843 /* Now we have a direct declarator, direct abstract declarator or
2844 nothing (which counts as a direct abstract declarator here). */
2845 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2848 /* Parse a direct declarator or direct abstract declarator; arguments
2849 as c_parser_declarator. */
2851 static struct c_declarator *
2852 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2853 bool *seen_id)
2855 /* The direct declarator must start with an identifier (possibly
2856 omitted) or a parenthesized declarator (possibly abstract). In
2857 an ordinary declarator, initial parentheses must start a
2858 parenthesized declarator. In an abstract declarator or parameter
2859 declarator, they could start a parenthesized declarator or a
2860 parameter list. To tell which, the open parenthesis and any
2861 following attributes must be read. If a declaration specifier
2862 follows, then it is a parameter list; if the specifier is a
2863 typedef name, there might be an ambiguity about redeclaring it,
2864 which is resolved in the direction of treating it as a typedef
2865 name. If a close parenthesis follows, it is also an empty
2866 parameter list, as the syntax does not permit empty abstract
2867 declarators. Otherwise, it is a parenthesized declarator (in
2868 which case the analysis may be repeated inside it, recursively).
2870 ??? There is an ambiguity in a parameter declaration "int
2871 (__attribute__((foo)) x)", where x is not a typedef name: it
2872 could be an abstract declarator for a function, or declare x with
2873 parentheses. The proper resolution of this ambiguity needs
2874 documenting. At present we follow an accident of the old
2875 parser's implementation, whereby the first parameter must have
2876 some declaration specifiers other than just attributes. Thus as
2877 a parameter declaration it is treated as a parenthesized
2878 parameter named x, and as an abstract declarator it is
2879 rejected.
2881 ??? Also following the old parser, attributes inside an empty
2882 parameter list are ignored, making it a list not yielding a
2883 prototype, rather than giving an error or making it have one
2884 parameter with implicit type int.
2886 ??? Also following the old parser, typedef names may be
2887 redeclared in declarators, but not Objective-C class names. */
2889 if (kind != C_DTR_ABSTRACT
2890 && c_parser_next_token_is (parser, CPP_NAME)
2891 && ((type_seen_p
2892 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2893 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2894 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2896 struct c_declarator *inner
2897 = build_id_declarator (c_parser_peek_token (parser)->value);
2898 *seen_id = true;
2899 inner->id_loc = c_parser_peek_token (parser)->location;
2900 c_parser_consume_token (parser);
2901 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2904 if (kind != C_DTR_NORMAL
2905 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2907 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2908 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2911 /* Either we are at the end of an abstract declarator, or we have
2912 parentheses. */
2914 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2916 tree attrs;
2917 struct c_declarator *inner;
2918 c_parser_consume_token (parser);
2919 attrs = c_parser_attributes (parser);
2920 if (kind != C_DTR_NORMAL
2921 && (c_parser_next_token_starts_declspecs (parser)
2922 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2924 struct c_arg_info *args
2925 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2926 attrs);
2927 if (args == NULL)
2928 return NULL;
2929 else
2931 inner
2932 = build_function_declarator (args,
2933 build_id_declarator (NULL_TREE));
2934 return c_parser_direct_declarator_inner (parser, *seen_id,
2935 inner);
2938 /* A parenthesized declarator. */
2939 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2940 if (inner != NULL && attrs != NULL)
2941 inner = build_attrs_declarator (attrs, inner);
2942 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
2944 c_parser_consume_token (parser);
2945 if (inner == NULL)
2946 return NULL;
2947 else
2948 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2950 else
2952 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2953 "expected %<)%>");
2954 return NULL;
2957 else
2959 if (kind == C_DTR_NORMAL)
2961 c_parser_error (parser, "expected identifier or %<(%>");
2962 return NULL;
2964 else
2965 return build_id_declarator (NULL_TREE);
2969 /* Parse part of a direct declarator or direct abstract declarator,
2970 given that some (in INNER) has already been parsed; ID_PRESENT is
2971 true if an identifier is present, false for an abstract
2972 declarator. */
2974 static struct c_declarator *
2975 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
2976 struct c_declarator *inner)
2978 /* Parse a sequence of array declarators and parameter lists. */
2979 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2981 location_t brace_loc = c_parser_peek_token (parser)->location;
2982 struct c_declarator *declarator;
2983 struct c_declspecs *quals_attrs = build_null_declspecs ();
2984 bool static_seen;
2985 bool star_seen;
2986 tree dimen;
2987 c_parser_consume_token (parser);
2988 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2989 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
2990 if (static_seen)
2991 c_parser_consume_token (parser);
2992 if (static_seen && !quals_attrs->declspecs_seen_p)
2993 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2994 if (!quals_attrs->declspecs_seen_p)
2995 quals_attrs = NULL;
2996 /* If "static" is present, there must be an array dimension.
2997 Otherwise, there may be a dimension, "*", or no
2998 dimension. */
2999 if (static_seen)
3001 star_seen = false;
3002 dimen = c_parser_expr_no_commas (parser, NULL).value;
3004 else
3006 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3008 dimen = NULL_TREE;
3009 star_seen = false;
3011 else if (c_parser_next_token_is (parser, CPP_MULT))
3013 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3015 dimen = NULL_TREE;
3016 star_seen = true;
3017 c_parser_consume_token (parser);
3019 else
3021 star_seen = false;
3022 dimen = c_parser_expr_no_commas (parser, NULL).value;
3025 else
3027 star_seen = false;
3028 dimen = c_parser_expr_no_commas (parser, NULL).value;
3031 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3032 c_parser_consume_token (parser);
3033 else
3035 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3036 "expected %<]%>");
3037 return NULL;
3039 if (dimen)
3040 mark_exp_read (dimen);
3041 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3042 static_seen, star_seen);
3043 if (declarator == NULL)
3044 return NULL;
3045 inner = set_array_declarator_inner (declarator, inner);
3046 return c_parser_direct_declarator_inner (parser, id_present, inner);
3048 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3050 tree attrs;
3051 struct c_arg_info *args;
3052 c_parser_consume_token (parser);
3053 attrs = c_parser_attributes (parser);
3054 args = c_parser_parms_declarator (parser, id_present, attrs);
3055 if (args == NULL)
3056 return NULL;
3057 else
3059 inner = build_function_declarator (args, inner);
3060 return c_parser_direct_declarator_inner (parser, id_present, inner);
3063 return inner;
3066 /* Parse a parameter list or identifier list, including the closing
3067 parenthesis but not the opening one. ATTRS are the attributes at
3068 the start of the list. ID_LIST_OK is true if an identifier list is
3069 acceptable; such a list must not have attributes at the start. */
3071 static struct c_arg_info *
3072 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3074 push_scope ();
3075 declare_parm_level ();
3076 /* If the list starts with an identifier, it is an identifier list.
3077 Otherwise, it is either a prototype list or an empty list. */
3078 if (id_list_ok
3079 && !attrs
3080 && c_parser_next_token_is (parser, CPP_NAME)
3081 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3083 /* Look ahead to detect typos in type names. */
3084 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3085 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3086 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3087 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3089 tree list = NULL_TREE, *nextp = &list;
3090 while (c_parser_next_token_is (parser, CPP_NAME)
3091 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3093 *nextp = build_tree_list (NULL_TREE,
3094 c_parser_peek_token (parser)->value);
3095 nextp = & TREE_CHAIN (*nextp);
3096 c_parser_consume_token (parser);
3097 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3098 break;
3099 c_parser_consume_token (parser);
3100 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3102 c_parser_error (parser, "expected identifier");
3103 break;
3106 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3108 struct c_arg_info *ret = build_arg_info ();
3109 ret->types = list;
3110 c_parser_consume_token (parser);
3111 pop_scope ();
3112 return ret;
3114 else
3116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3117 "expected %<)%>");
3118 pop_scope ();
3119 return NULL;
3122 else
3124 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3125 NULL);
3126 pop_scope ();
3127 return ret;
3131 /* Parse a parameter list (possibly empty), including the closing
3132 parenthesis but not the opening one. ATTRS are the attributes at
3133 the start of the list. EXPR is NULL or an expression that needs to
3134 be evaluated for the side effects of array size expressions in the
3135 parameters. */
3137 static struct c_arg_info *
3138 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3140 bool bad_parm = false;
3142 /* ??? Following the old parser, forward parameter declarations may
3143 use abstract declarators, and if no real parameter declarations
3144 follow the forward declarations then this is not diagnosed. Also
3145 note as above that attributes are ignored as the only contents of
3146 the parentheses, or as the only contents after forward
3147 declarations. */
3148 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3150 struct c_arg_info *ret = build_arg_info ();
3151 c_parser_consume_token (parser);
3152 return ret;
3154 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3156 struct c_arg_info *ret = build_arg_info ();
3157 /* Suppress -Wold-style-definition for this case. */
3158 ret->types = error_mark_node;
3159 error_at (c_parser_peek_token (parser)->location,
3160 "ISO C requires a named argument before %<...%>");
3161 c_parser_consume_token (parser);
3162 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3164 c_parser_consume_token (parser);
3165 return ret;
3167 else
3169 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3170 "expected %<)%>");
3171 return NULL;
3174 /* Nonempty list of parameters, either terminated with semicolon
3175 (forward declarations; recurse) or with close parenthesis (normal
3176 function) or with ", ... )" (variadic function). */
3177 while (true)
3179 /* Parse a parameter. */
3180 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3181 attrs = NULL_TREE;
3182 if (parm == NULL)
3183 bad_parm = true;
3184 else
3185 push_parm_decl (parm, &expr);
3186 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3188 tree new_attrs;
3189 c_parser_consume_token (parser);
3190 mark_forward_parm_decls ();
3191 new_attrs = c_parser_attributes (parser);
3192 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3194 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3196 c_parser_consume_token (parser);
3197 if (bad_parm)
3198 return NULL;
3199 else
3200 return get_parm_info (false, expr);
3202 if (!c_parser_require (parser, CPP_COMMA,
3203 "expected %<;%>, %<,%> or %<)%>"))
3205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3206 return NULL;
3208 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3210 c_parser_consume_token (parser);
3211 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3213 c_parser_consume_token (parser);
3214 if (bad_parm)
3215 return NULL;
3216 else
3217 return get_parm_info (true, expr);
3219 else
3221 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3222 "expected %<)%>");
3223 return NULL;
3229 /* Parse a parameter declaration. ATTRS are the attributes at the
3230 start of the declaration if it is the first parameter. */
3232 static struct c_parm *
3233 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3235 struct c_declspecs *specs;
3236 struct c_declarator *declarator;
3237 tree prefix_attrs;
3238 tree postfix_attrs = NULL_TREE;
3239 bool dummy = false;
3240 if (!c_parser_next_token_starts_declspecs (parser))
3242 c_token *token = c_parser_peek_token (parser);
3243 if (parser->error)
3244 return NULL;
3245 c_parser_set_source_position_from_token (token);
3246 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3248 error ("unknown type name %qE", token->value);
3249 parser->error = true;
3251 /* ??? In some Objective-C cases '...' isn't applicable so there
3252 should be a different message. */
3253 else
3254 c_parser_error (parser,
3255 "expected declaration specifiers or %<...%>");
3256 c_parser_skip_to_end_of_parameter (parser);
3257 return NULL;
3259 specs = build_null_declspecs ();
3260 if (attrs)
3262 declspecs_add_attrs (specs, attrs);
3263 attrs = NULL_TREE;
3265 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3266 finish_declspecs (specs);
3267 pending_xref_error ();
3268 prefix_attrs = specs->attrs;
3269 specs->attrs = NULL_TREE;
3270 declarator = c_parser_declarator (parser,
3271 specs->typespec_kind != ctsk_none,
3272 C_DTR_PARM, &dummy);
3273 if (declarator == NULL)
3275 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3276 return NULL;
3278 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3279 postfix_attrs = c_parser_attributes (parser);
3280 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3281 declarator);
3284 /* Parse a string literal in an asm expression. It should not be
3285 translated, and wide string literals are an error although
3286 permitted by the syntax. This is a GNU extension.
3288 asm-string-literal:
3289 string-literal
3291 ??? At present, following the old parser, the caller needs to have
3292 set lex_untranslated_string to 1. It would be better to follow the
3293 C++ parser rather than using this kludge. */
3295 static tree
3296 c_parser_asm_string_literal (c_parser *parser)
3298 tree str;
3299 int save_flag = warn_overlength_strings;
3300 warn_overlength_strings = 0;
3301 if (c_parser_next_token_is (parser, CPP_STRING))
3303 str = c_parser_peek_token (parser)->value;
3304 c_parser_consume_token (parser);
3306 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3308 error_at (c_parser_peek_token (parser)->location,
3309 "wide string literal in %<asm%>");
3310 str = build_string (1, "");
3311 c_parser_consume_token (parser);
3313 else
3315 c_parser_error (parser, "expected string literal");
3316 str = NULL_TREE;
3318 warn_overlength_strings = save_flag;
3319 return str;
3322 /* Parse a simple asm expression. This is used in restricted
3323 contexts, where a full expression with inputs and outputs does not
3324 make sense. This is a GNU extension.
3326 simple-asm-expr:
3327 asm ( asm-string-literal )
3330 static tree
3331 c_parser_simple_asm_expr (c_parser *parser)
3333 tree str;
3334 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3335 /* ??? Follow the C++ parser rather than using the
3336 lex_untranslated_string kludge. */
3337 parser->lex_untranslated_string = true;
3338 c_parser_consume_token (parser);
3339 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3341 parser->lex_untranslated_string = false;
3342 return NULL_TREE;
3344 str = c_parser_asm_string_literal (parser);
3345 parser->lex_untranslated_string = false;
3346 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3348 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3349 return NULL_TREE;
3351 return str;
3354 /* Parse (possibly empty) attributes. This is a GNU extension.
3356 attributes:
3357 empty
3358 attributes attribute
3360 attribute:
3361 __attribute__ ( ( attribute-list ) )
3363 attribute-list:
3364 attrib
3365 attribute_list , attrib
3367 attrib:
3368 empty
3369 any-word
3370 any-word ( identifier )
3371 any-word ( identifier , nonempty-expr-list )
3372 any-word ( expr-list )
3374 where the "identifier" must not be declared as a type, and
3375 "any-word" may be any identifier (including one declared as a
3376 type), a reserved word storage class specifier, type specifier or
3377 type qualifier. ??? This still leaves out most reserved keywords
3378 (following the old parser), shouldn't we include them, and why not
3379 allow identifiers declared as types to start the arguments? */
3381 static tree
3382 c_parser_attributes (c_parser *parser)
3384 tree attrs = NULL_TREE;
3385 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3387 /* ??? Follow the C++ parser rather than using the
3388 lex_untranslated_string kludge. */
3389 parser->lex_untranslated_string = true;
3390 c_parser_consume_token (parser);
3391 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3393 parser->lex_untranslated_string = false;
3394 return attrs;
3396 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3398 parser->lex_untranslated_string = false;
3399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3400 return attrs;
3402 /* Parse the attribute list. */
3403 while (c_parser_next_token_is (parser, CPP_COMMA)
3404 || c_parser_next_token_is (parser, CPP_NAME)
3405 || c_parser_next_token_is (parser, CPP_KEYWORD))
3407 tree attr, attr_name, attr_args;
3408 VEC(tree,gc) *expr_list;
3409 if (c_parser_next_token_is (parser, CPP_COMMA))
3411 c_parser_consume_token (parser);
3412 continue;
3414 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3416 /* ??? See comment above about what keywords are
3417 accepted here. */
3418 bool ok;
3419 switch (c_parser_peek_token (parser)->keyword)
3421 case RID_STATIC:
3422 case RID_UNSIGNED:
3423 case RID_LONG:
3424 case RID_INT128:
3425 case RID_CONST:
3426 case RID_EXTERN:
3427 case RID_REGISTER:
3428 case RID_TYPEDEF:
3429 case RID_SHORT:
3430 case RID_INLINE:
3431 case RID_VOLATILE:
3432 case RID_SIGNED:
3433 case RID_AUTO:
3434 case RID_RESTRICT:
3435 case RID_COMPLEX:
3436 case RID_THREAD:
3437 case RID_INT:
3438 case RID_CHAR:
3439 case RID_FLOAT:
3440 case RID_DOUBLE:
3441 case RID_VOID:
3442 case RID_DFLOAT32:
3443 case RID_DFLOAT64:
3444 case RID_DFLOAT128:
3445 case RID_BOOL:
3446 case RID_FRACT:
3447 case RID_ACCUM:
3448 case RID_SAT:
3449 ok = true;
3450 break;
3451 default:
3452 ok = false;
3453 break;
3455 if (!ok)
3456 break;
3457 /* Accept __attribute__((__const)) as __attribute__((const))
3458 etc. */
3459 attr_name
3460 = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3462 else
3463 attr_name = c_parser_peek_token (parser)->value;
3464 c_parser_consume_token (parser);
3465 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3467 attr = build_tree_list (attr_name, NULL_TREE);
3468 attrs = chainon (attrs, attr);
3469 continue;
3471 c_parser_consume_token (parser);
3472 /* Parse the attribute contents. If they start with an
3473 identifier which is followed by a comma or close
3474 parenthesis, then the arguments start with that
3475 identifier; otherwise they are an expression list.
3476 In objective-c the identifier may be a classname. */
3477 if (c_parser_next_token_is (parser, CPP_NAME)
3478 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3479 || (c_dialect_objc ()
3480 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3481 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3482 || (c_parser_peek_2nd_token (parser)->type
3483 == CPP_CLOSE_PAREN)))
3485 tree arg1 = c_parser_peek_token (parser)->value;
3486 c_parser_consume_token (parser);
3487 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3488 attr_args = build_tree_list (NULL_TREE, arg1);
3489 else
3491 tree tree_list;
3492 c_parser_consume_token (parser);
3493 expr_list = c_parser_expr_list (parser, false, true, NULL);
3494 tree_list = build_tree_list_vec (expr_list);
3495 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3496 release_tree_vector (expr_list);
3499 else
3501 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3502 attr_args = NULL_TREE;
3503 else
3505 expr_list = c_parser_expr_list (parser, false, true, NULL);
3506 attr_args = build_tree_list_vec (expr_list);
3507 release_tree_vector (expr_list);
3510 attr = build_tree_list (attr_name, attr_args);
3511 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3512 c_parser_consume_token (parser);
3513 else
3515 parser->lex_untranslated_string = false;
3516 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3517 "expected %<)%>");
3518 return attrs;
3520 attrs = chainon (attrs, attr);
3522 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3523 c_parser_consume_token (parser);
3524 else
3526 parser->lex_untranslated_string = false;
3527 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3528 "expected %<)%>");
3529 return attrs;
3531 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3532 c_parser_consume_token (parser);
3533 else
3535 parser->lex_untranslated_string = false;
3536 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3537 "expected %<)%>");
3538 return attrs;
3540 parser->lex_untranslated_string = false;
3542 return attrs;
3545 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3547 type-name:
3548 specifier-qualifier-list abstract-declarator[opt]
3551 static struct c_type_name *
3552 c_parser_type_name (c_parser *parser)
3554 struct c_declspecs *specs = build_null_declspecs ();
3555 struct c_declarator *declarator;
3556 struct c_type_name *ret;
3557 bool dummy = false;
3558 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3559 if (!specs->declspecs_seen_p)
3561 c_parser_error (parser, "expected specifier-qualifier-list");
3562 return NULL;
3564 if (specs->type != error_mark_node)
3566 pending_xref_error ();
3567 finish_declspecs (specs);
3569 declarator = c_parser_declarator (parser,
3570 specs->typespec_kind != ctsk_none,
3571 C_DTR_ABSTRACT, &dummy);
3572 if (declarator == NULL)
3573 return NULL;
3574 ret = XOBNEW (&parser_obstack, struct c_type_name);
3575 ret->specs = specs;
3576 ret->declarator = declarator;
3577 return ret;
3580 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3582 initializer:
3583 assignment-expression
3584 { initializer-list }
3585 { initializer-list , }
3587 initializer-list:
3588 designation[opt] initializer
3589 initializer-list , designation[opt] initializer
3591 designation:
3592 designator-list =
3594 designator-list:
3595 designator
3596 designator-list designator
3598 designator:
3599 array-designator
3600 . identifier
3602 array-designator:
3603 [ constant-expression ]
3605 GNU extensions:
3607 initializer:
3610 designation:
3611 array-designator
3612 identifier :
3614 array-designator:
3615 [ constant-expression ... constant-expression ]
3617 Any expression without commas is accepted in the syntax for the
3618 constant-expressions, with non-constant expressions rejected later.
3620 This function is only used for top-level initializers; for nested
3621 ones, see c_parser_initval. */
3623 static struct c_expr
3624 c_parser_initializer (c_parser *parser)
3626 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3627 return c_parser_braced_init (parser, NULL_TREE, false);
3628 else
3630 struct c_expr ret;
3631 location_t loc = c_parser_peek_token (parser)->location;
3632 ret = c_parser_expr_no_commas (parser, NULL);
3633 if (TREE_CODE (ret.value) != STRING_CST
3634 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3635 ret = default_function_array_read_conversion (loc, ret);
3636 return ret;
3640 /* Parse a braced initializer list. TYPE is the type specified for a
3641 compound literal, and NULL_TREE for other initializers and for
3642 nested braced lists. NESTED_P is true for nested braced lists,
3643 false for the list of a compound literal or the list that is the
3644 top-level initializer in a declaration. */
3646 static struct c_expr
3647 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3649 struct c_expr ret;
3650 struct obstack braced_init_obstack;
3651 location_t brace_loc = c_parser_peek_token (parser)->location;
3652 gcc_obstack_init (&braced_init_obstack);
3653 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3654 c_parser_consume_token (parser);
3655 if (nested_p)
3656 push_init_level (0, &braced_init_obstack);
3657 else
3658 really_start_incremental_init (type);
3659 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3661 pedwarn (brace_loc, OPT_pedantic, "ISO C forbids empty initializer braces");
3663 else
3665 /* Parse a non-empty initializer list, possibly with a trailing
3666 comma. */
3667 while (true)
3669 c_parser_initelt (parser, &braced_init_obstack);
3670 if (parser->error)
3671 break;
3672 if (c_parser_next_token_is (parser, CPP_COMMA))
3673 c_parser_consume_token (parser);
3674 else
3675 break;
3676 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3677 break;
3680 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3682 ret.value = error_mark_node;
3683 ret.original_code = ERROR_MARK;
3684 ret.original_type = NULL;
3685 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3686 pop_init_level (0, &braced_init_obstack);
3687 obstack_free (&braced_init_obstack, NULL);
3688 return ret;
3690 c_parser_consume_token (parser);
3691 ret = pop_init_level (0, &braced_init_obstack);
3692 obstack_free (&braced_init_obstack, NULL);
3693 return ret;
3696 /* Parse a nested initializer, including designators. */
3698 static void
3699 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3701 /* Parse any designator or designator list. A single array
3702 designator may have the subsequent "=" omitted in GNU C, but a
3703 longer list or a structure member designator may not. */
3704 if (c_parser_next_token_is (parser, CPP_NAME)
3705 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3707 /* Old-style structure member designator. */
3708 set_init_label (c_parser_peek_token (parser)->value,
3709 braced_init_obstack);
3710 /* Use the colon as the error location. */
3711 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_pedantic,
3712 "obsolete use of designated initializer with %<:%>");
3713 c_parser_consume_token (parser);
3714 c_parser_consume_token (parser);
3716 else
3718 /* des_seen is 0 if there have been no designators, 1 if there
3719 has been a single array designator and 2 otherwise. */
3720 int des_seen = 0;
3721 /* Location of a designator. */
3722 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3723 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3724 || c_parser_next_token_is (parser, CPP_DOT))
3726 int des_prev = des_seen;
3727 if (!des_seen)
3728 des_loc = c_parser_peek_token (parser)->location;
3729 if (des_seen < 2)
3730 des_seen++;
3731 if (c_parser_next_token_is (parser, CPP_DOT))
3733 des_seen = 2;
3734 c_parser_consume_token (parser);
3735 if (c_parser_next_token_is (parser, CPP_NAME))
3737 set_init_label (c_parser_peek_token (parser)->value,
3738 braced_init_obstack);
3739 c_parser_consume_token (parser);
3741 else
3743 struct c_expr init;
3744 init.value = error_mark_node;
3745 init.original_code = ERROR_MARK;
3746 init.original_type = NULL;
3747 c_parser_error (parser, "expected identifier");
3748 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3749 process_init_element (init, false, braced_init_obstack);
3750 return;
3753 else
3755 tree first, second;
3756 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3757 /* ??? Following the old parser, [ objc-receiver
3758 objc-message-args ] is accepted as an initializer,
3759 being distinguished from a designator by what follows
3760 the first assignment expression inside the square
3761 brackets, but after a first array designator a
3762 subsequent square bracket is for Objective-C taken to
3763 start an expression, using the obsolete form of
3764 designated initializer without '=', rather than
3765 possibly being a second level of designation: in LALR
3766 terms, the '[' is shifted rather than reducing
3767 designator to designator-list. */
3768 if (des_prev == 1 && c_dialect_objc ())
3770 des_seen = des_prev;
3771 break;
3773 if (des_prev == 0 && c_dialect_objc ())
3775 /* This might be an array designator or an
3776 Objective-C message expression. If the former,
3777 continue parsing here; if the latter, parse the
3778 remainder of the initializer given the starting
3779 primary-expression. ??? It might make sense to
3780 distinguish when des_prev == 1 as well; see
3781 previous comment. */
3782 tree rec, args;
3783 struct c_expr mexpr;
3784 c_parser_consume_token (parser);
3785 if (c_parser_peek_token (parser)->type == CPP_NAME
3786 && ((c_parser_peek_token (parser)->id_kind
3787 == C_ID_TYPENAME)
3788 || (c_parser_peek_token (parser)->id_kind
3789 == C_ID_CLASSNAME)))
3791 /* Type name receiver. */
3792 tree id = c_parser_peek_token (parser)->value;
3793 c_parser_consume_token (parser);
3794 rec = objc_get_class_reference (id);
3795 goto parse_message_args;
3797 first = c_parser_expr_no_commas (parser, NULL).value;
3798 mark_exp_read (first);
3799 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3800 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3801 goto array_desig_after_first;
3802 /* Expression receiver. So far only one part
3803 without commas has been parsed; there might be
3804 more of the expression. */
3805 rec = first;
3806 while (c_parser_next_token_is (parser, CPP_COMMA))
3808 struct c_expr next;
3809 location_t comma_loc, exp_loc;
3810 comma_loc = c_parser_peek_token (parser)->location;
3811 c_parser_consume_token (parser);
3812 exp_loc = c_parser_peek_token (parser)->location;
3813 next = c_parser_expr_no_commas (parser, NULL);
3814 next = default_function_array_read_conversion (exp_loc,
3815 next);
3816 rec = build_compound_expr (comma_loc, rec, next.value);
3818 parse_message_args:
3819 /* Now parse the objc-message-args. */
3820 args = c_parser_objc_message_args (parser);
3821 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3822 "expected %<]%>");
3823 mexpr.value
3824 = objc_build_message_expr (rec, args);
3825 mexpr.original_code = ERROR_MARK;
3826 mexpr.original_type = NULL;
3827 /* Now parse and process the remainder of the
3828 initializer, starting with this message
3829 expression as a primary-expression. */
3830 c_parser_initval (parser, &mexpr, braced_init_obstack);
3831 return;
3833 c_parser_consume_token (parser);
3834 first = c_parser_expr_no_commas (parser, NULL).value;
3835 mark_exp_read (first);
3836 array_desig_after_first:
3837 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3839 ellipsis_loc = c_parser_peek_token (parser)->location;
3840 c_parser_consume_token (parser);
3841 second = c_parser_expr_no_commas (parser, NULL).value;
3842 mark_exp_read (second);
3844 else
3845 second = NULL_TREE;
3846 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3848 c_parser_consume_token (parser);
3849 set_init_index (first, second, braced_init_obstack);
3850 if (second)
3851 pedwarn (ellipsis_loc, OPT_pedantic,
3852 "ISO C forbids specifying range of elements to initialize");
3854 else
3855 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3856 "expected %<]%>");
3859 if (des_seen >= 1)
3861 if (c_parser_next_token_is (parser, CPP_EQ))
3863 if (!flag_isoc99)
3864 pedwarn (des_loc, OPT_pedantic,
3865 "ISO C90 forbids specifying subobject to initialize");
3866 c_parser_consume_token (parser);
3868 else
3870 if (des_seen == 1)
3871 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
3872 "obsolete use of designated initializer without %<=%>");
3873 else
3875 struct c_expr init;
3876 init.value = error_mark_node;
3877 init.original_code = ERROR_MARK;
3878 init.original_type = NULL;
3879 c_parser_error (parser, "expected %<=%>");
3880 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3881 process_init_element (init, false, braced_init_obstack);
3882 return;
3887 c_parser_initval (parser, NULL, braced_init_obstack);
3890 /* Parse a nested initializer; as c_parser_initializer but parses
3891 initializers within braced lists, after any designators have been
3892 applied. If AFTER is not NULL then it is an Objective-C message
3893 expression which is the primary-expression starting the
3894 initializer. */
3896 static void
3897 c_parser_initval (c_parser *parser, struct c_expr *after,
3898 struct obstack * braced_init_obstack)
3900 struct c_expr init;
3901 gcc_assert (!after || c_dialect_objc ());
3902 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3903 init = c_parser_braced_init (parser, NULL_TREE, true);
3904 else
3906 location_t loc = c_parser_peek_token (parser)->location;
3907 init = c_parser_expr_no_commas (parser, after);
3908 if (init.value != NULL_TREE
3909 && TREE_CODE (init.value) != STRING_CST
3910 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3911 init = default_function_array_read_conversion (loc, init);
3913 process_init_element (init, false, braced_init_obstack);
3916 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
3917 C99 6.8.2).
3919 compound-statement:
3920 { block-item-list[opt] }
3921 { label-declarations block-item-list }
3923 block-item-list:
3924 block-item
3925 block-item-list block-item
3927 block-item:
3928 nested-declaration
3929 statement
3931 nested-declaration:
3932 declaration
3934 GNU extensions:
3936 compound-statement:
3937 { label-declarations block-item-list }
3939 nested-declaration:
3940 __extension__ nested-declaration
3941 nested-function-definition
3943 label-declarations:
3944 label-declaration
3945 label-declarations label-declaration
3947 label-declaration:
3948 __label__ identifier-list ;
3950 Allowing the mixing of declarations and code is new in C99. The
3951 GNU syntax also permits (not shown above) labels at the end of
3952 compound statements, which yield an error. We don't allow labels
3953 on declarations; this might seem like a natural extension, but
3954 there would be a conflict between attributes on the label and
3955 prefix attributes on the declaration. ??? The syntax follows the
3956 old parser in requiring something after label declarations.
3957 Although they are erroneous if the labels declared aren't defined,
3958 is it useful for the syntax to be this way?
3960 OpenMP:
3962 block-item:
3963 openmp-directive
3965 openmp-directive:
3966 barrier-directive
3967 flush-directive */
3969 static tree
3970 c_parser_compound_statement (c_parser *parser)
3972 tree stmt;
3973 location_t brace_loc;
3974 brace_loc = c_parser_peek_token (parser)->location;
3975 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
3977 /* Ensure a scope is entered and left anyway to avoid confusion
3978 if we have just prepared to enter a function body. */
3979 stmt = c_begin_compound_stmt (true);
3980 c_end_compound_stmt (brace_loc, stmt, true);
3981 return error_mark_node;
3983 stmt = c_begin_compound_stmt (true);
3984 c_parser_compound_statement_nostart (parser);
3985 return c_end_compound_stmt (brace_loc, stmt, true);
3988 /* Parse a compound statement except for the opening brace. This is
3989 used for parsing both compound statements and statement expressions
3990 (which follow different paths to handling the opening). */
3992 static void
3993 c_parser_compound_statement_nostart (c_parser *parser)
3995 bool last_stmt = false;
3996 bool last_label = false;
3997 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
3998 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3999 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4001 c_parser_consume_token (parser);
4002 return;
4004 mark_valid_location_for_stdc_pragma (true);
4005 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4007 /* Read zero or more forward-declarations for labels that nested
4008 functions can jump to. */
4009 mark_valid_location_for_stdc_pragma (false);
4010 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4012 label_loc = c_parser_peek_token (parser)->location;
4013 c_parser_consume_token (parser);
4014 /* Any identifiers, including those declared as type names,
4015 are OK here. */
4016 while (true)
4018 tree label;
4019 if (c_parser_next_token_is_not (parser, CPP_NAME))
4021 c_parser_error (parser, "expected identifier");
4022 break;
4024 label
4025 = declare_label (c_parser_peek_token (parser)->value);
4026 C_DECLARED_LABEL_FLAG (label) = 1;
4027 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4028 c_parser_consume_token (parser);
4029 if (c_parser_next_token_is (parser, CPP_COMMA))
4030 c_parser_consume_token (parser);
4031 else
4032 break;
4034 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4036 pedwarn (label_loc, OPT_pedantic, "ISO C forbids label declarations");
4038 /* We must now have at least one statement, label or declaration. */
4039 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4041 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4042 c_parser_error (parser, "expected declaration or statement");
4043 c_parser_consume_token (parser);
4044 return;
4046 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4048 location_t loc = c_parser_peek_token (parser)->location;
4049 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4050 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4051 || (c_parser_next_token_is (parser, CPP_NAME)
4052 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4054 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4055 label_loc = c_parser_peek_2nd_token (parser)->location;
4056 else
4057 label_loc = c_parser_peek_token (parser)->location;
4058 last_label = true;
4059 last_stmt = false;
4060 mark_valid_location_for_stdc_pragma (false);
4061 c_parser_label (parser);
4063 else if (!last_label
4064 && c_parser_next_tokens_start_declaration (parser))
4066 last_label = false;
4067 mark_valid_location_for_stdc_pragma (false);
4068 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4069 if (last_stmt)
4070 pedwarn_c90 (loc,
4071 (pedantic && !flag_isoc99)
4072 ? OPT_pedantic
4073 : OPT_Wdeclaration_after_statement,
4074 "ISO C90 forbids mixed declarations and code");
4075 last_stmt = false;
4077 else if (!last_label
4078 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4080 /* __extension__ can start a declaration, but is also an
4081 unary operator that can start an expression. Consume all
4082 but the last of a possible series of __extension__ to
4083 determine which. */
4084 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4085 && (c_parser_peek_2nd_token (parser)->keyword
4086 == RID_EXTENSION))
4087 c_parser_consume_token (parser);
4088 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4090 int ext;
4091 ext = disable_extension_diagnostics ();
4092 c_parser_consume_token (parser);
4093 last_label = false;
4094 mark_valid_location_for_stdc_pragma (false);
4095 c_parser_declaration_or_fndef (parser, true, true, true, true,
4096 true, NULL);
4097 /* Following the old parser, __extension__ does not
4098 disable this diagnostic. */
4099 restore_extension_diagnostics (ext);
4100 if (last_stmt)
4101 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4102 ? OPT_pedantic
4103 : OPT_Wdeclaration_after_statement,
4104 "ISO C90 forbids mixed declarations and code");
4105 last_stmt = false;
4107 else
4108 goto statement;
4110 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4112 /* External pragmas, and some omp pragmas, are not associated
4113 with regular c code, and so are not to be considered statements
4114 syntactically. This ensures that the user doesn't put them
4115 places that would turn into syntax errors if the directive
4116 were ignored. */
4117 if (c_parser_pragma (parser, pragma_compound))
4118 last_label = false, last_stmt = true;
4120 else if (c_parser_next_token_is (parser, CPP_EOF))
4122 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4123 c_parser_error (parser, "expected declaration or statement");
4124 return;
4126 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4128 if (parser->in_if_block)
4130 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4131 error_at (loc, """expected %<}%> before %<else%>");
4132 return;
4134 else
4136 error_at (loc, "%<else%> without a previous %<if%>");
4137 c_parser_consume_token (parser);
4138 continue;
4141 else
4143 statement:
4144 last_label = false;
4145 last_stmt = true;
4146 mark_valid_location_for_stdc_pragma (false);
4147 c_parser_statement_after_labels (parser);
4150 parser->error = false;
4152 if (last_label)
4153 error_at (label_loc, "label at end of compound statement");
4154 c_parser_consume_token (parser);
4155 /* Restore the value we started with. */
4156 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4159 /* Parse a label (C90 6.6.1, C99 6.8.1).
4161 label:
4162 identifier : attributes[opt]
4163 case constant-expression :
4164 default :
4166 GNU extensions:
4168 label:
4169 case constant-expression ... constant-expression :
4171 The use of attributes on labels is a GNU extension. The syntax in
4172 GNU C accepts any expressions without commas, non-constant
4173 expressions being rejected later. */
4175 static void
4176 c_parser_label (c_parser *parser)
4178 location_t loc1 = c_parser_peek_token (parser)->location;
4179 tree label = NULL_TREE;
4180 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4182 tree exp1, exp2;
4183 c_parser_consume_token (parser);
4184 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4185 if (c_parser_next_token_is (parser, CPP_COLON))
4187 c_parser_consume_token (parser);
4188 label = do_case (loc1, exp1, NULL_TREE);
4190 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4192 c_parser_consume_token (parser);
4193 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4194 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4195 label = do_case (loc1, exp1, exp2);
4197 else
4198 c_parser_error (parser, "expected %<:%> or %<...%>");
4200 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4202 c_parser_consume_token (parser);
4203 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4204 label = do_case (loc1, NULL_TREE, NULL_TREE);
4206 else
4208 tree name = c_parser_peek_token (parser)->value;
4209 tree tlab;
4210 tree attrs;
4211 location_t loc2 = c_parser_peek_token (parser)->location;
4212 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4213 c_parser_consume_token (parser);
4214 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4215 c_parser_consume_token (parser);
4216 attrs = c_parser_attributes (parser);
4217 tlab = define_label (loc2, name);
4218 if (tlab)
4220 decl_attributes (&tlab, attrs, 0);
4221 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4224 if (label)
4226 if (c_parser_next_tokens_start_declaration (parser))
4228 error_at (c_parser_peek_token (parser)->location,
4229 "a label can only be part of a statement and "
4230 "a declaration is not a statement");
4231 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4232 /*static_assert_ok*/ true,
4233 /*nested*/ true, /*empty_ok*/ false,
4234 /*start_attr_ok*/ true, NULL);
4239 /* Parse a statement (C90 6.6, C99 6.8).
4241 statement:
4242 labeled-statement
4243 compound-statement
4244 expression-statement
4245 selection-statement
4246 iteration-statement
4247 jump-statement
4249 labeled-statement:
4250 label statement
4252 expression-statement:
4253 expression[opt] ;
4255 selection-statement:
4256 if-statement
4257 switch-statement
4259 iteration-statement:
4260 while-statement
4261 do-statement
4262 for-statement
4264 jump-statement:
4265 goto identifier ;
4266 continue ;
4267 break ;
4268 return expression[opt] ;
4270 GNU extensions:
4272 statement:
4273 asm-statement
4275 jump-statement:
4276 goto * expression ;
4278 Objective-C:
4280 statement:
4281 objc-throw-statement
4282 objc-try-catch-statement
4283 objc-synchronized-statement
4285 objc-throw-statement:
4286 @throw expression ;
4287 @throw ;
4289 OpenMP:
4291 statement:
4292 openmp-construct
4294 openmp-construct:
4295 parallel-construct
4296 for-construct
4297 sections-construct
4298 single-construct
4299 parallel-for-construct
4300 parallel-sections-construct
4301 master-construct
4302 critical-construct
4303 atomic-construct
4304 ordered-construct
4306 parallel-construct:
4307 parallel-directive structured-block
4309 for-construct:
4310 for-directive iteration-statement
4312 sections-construct:
4313 sections-directive section-scope
4315 single-construct:
4316 single-directive structured-block
4318 parallel-for-construct:
4319 parallel-for-directive iteration-statement
4321 parallel-sections-construct:
4322 parallel-sections-directive section-scope
4324 master-construct:
4325 master-directive structured-block
4327 critical-construct:
4328 critical-directive structured-block
4330 atomic-construct:
4331 atomic-directive expression-statement
4333 ordered-construct:
4334 ordered-directive structured-block */
4336 static void
4337 c_parser_statement (c_parser *parser)
4339 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4340 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4341 || (c_parser_next_token_is (parser, CPP_NAME)
4342 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4343 c_parser_label (parser);
4344 c_parser_statement_after_labels (parser);
4347 /* Parse a statement, other than a labeled statement. */
4349 static void
4350 c_parser_statement_after_labels (c_parser *parser)
4352 location_t loc = c_parser_peek_token (parser)->location;
4353 tree stmt = NULL_TREE;
4354 bool in_if_block = parser->in_if_block;
4355 parser->in_if_block = false;
4356 switch (c_parser_peek_token (parser)->type)
4358 case CPP_OPEN_BRACE:
4359 add_stmt (c_parser_compound_statement (parser));
4360 break;
4361 case CPP_KEYWORD:
4362 switch (c_parser_peek_token (parser)->keyword)
4364 case RID_IF:
4365 c_parser_if_statement (parser);
4366 break;
4367 case RID_SWITCH:
4368 c_parser_switch_statement (parser);
4369 break;
4370 case RID_WHILE:
4371 c_parser_while_statement (parser);
4372 break;
4373 case RID_DO:
4374 c_parser_do_statement (parser);
4375 break;
4376 case RID_FOR:
4377 c_parser_for_statement (parser);
4378 break;
4379 case RID_GOTO:
4380 c_parser_consume_token (parser);
4381 if (c_parser_next_token_is (parser, CPP_NAME))
4383 stmt = c_finish_goto_label (loc,
4384 c_parser_peek_token (parser)->value);
4385 c_parser_consume_token (parser);
4387 else if (c_parser_next_token_is (parser, CPP_MULT))
4389 tree val;
4391 c_parser_consume_token (parser);
4392 val = c_parser_expression (parser).value;
4393 mark_exp_read (val);
4394 stmt = c_finish_goto_ptr (loc, val);
4396 else
4397 c_parser_error (parser, "expected identifier or %<*%>");
4398 goto expect_semicolon;
4399 case RID_CONTINUE:
4400 c_parser_consume_token (parser);
4401 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4402 goto expect_semicolon;
4403 case RID_BREAK:
4404 c_parser_consume_token (parser);
4405 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4406 goto expect_semicolon;
4407 case RID_RETURN:
4408 c_parser_consume_token (parser);
4409 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4411 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4412 c_parser_consume_token (parser);
4414 else
4416 struct c_expr expr = c_parser_expression_conv (parser);
4417 mark_exp_read (expr.value);
4418 stmt = c_finish_return (loc, expr.value, expr.original_type);
4419 goto expect_semicolon;
4421 break;
4422 case RID_ASM:
4423 stmt = c_parser_asm_statement (parser);
4424 break;
4425 case RID_AT_THROW:
4426 gcc_assert (c_dialect_objc ());
4427 c_parser_consume_token (parser);
4428 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4430 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4431 c_parser_consume_token (parser);
4433 else
4435 tree expr = c_parser_expression (parser).value;
4436 expr = c_fully_fold (expr, false, NULL);
4437 stmt = objc_build_throw_stmt (loc, expr);
4438 goto expect_semicolon;
4440 break;
4441 case RID_AT_TRY:
4442 gcc_assert (c_dialect_objc ());
4443 c_parser_objc_try_catch_finally_statement (parser);
4444 break;
4445 case RID_AT_SYNCHRONIZED:
4446 gcc_assert (c_dialect_objc ());
4447 c_parser_objc_synchronized_statement (parser);
4448 break;
4449 default:
4450 goto expr_stmt;
4452 break;
4453 case CPP_SEMICOLON:
4454 c_parser_consume_token (parser);
4455 break;
4456 case CPP_CLOSE_PAREN:
4457 case CPP_CLOSE_SQUARE:
4458 /* Avoid infinite loop in error recovery:
4459 c_parser_skip_until_found stops at a closing nesting
4460 delimiter without consuming it, but here we need to consume
4461 it to proceed further. */
4462 c_parser_error (parser, "expected statement");
4463 c_parser_consume_token (parser);
4464 break;
4465 case CPP_PRAGMA:
4466 c_parser_pragma (parser, pragma_stmt);
4467 break;
4468 default:
4469 expr_stmt:
4470 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4471 expect_semicolon:
4472 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4473 break;
4475 /* Two cases cannot and do not have line numbers associated: If stmt
4476 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4477 cannot hold line numbers. But that's OK because the statement
4478 will either be changed to a MODIFY_EXPR during gimplification of
4479 the statement expr, or discarded. If stmt was compound, but
4480 without new variables, we will have skipped the creation of a
4481 BIND and will have a bare STATEMENT_LIST. But that's OK because
4482 (recursively) all of the component statements should already have
4483 line numbers assigned. ??? Can we discard no-op statements
4484 earlier? */
4485 if (CAN_HAVE_LOCATION_P (stmt)
4486 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4487 SET_EXPR_LOCATION (stmt, loc);
4489 parser->in_if_block = in_if_block;
4492 /* Parse the condition from an if, do, while or for statements. */
4494 static tree
4495 c_parser_condition (c_parser *parser)
4497 location_t loc = c_parser_peek_token (parser)->location;
4498 tree cond;
4499 cond = c_parser_expression_conv (parser).value;
4500 cond = c_objc_common_truthvalue_conversion (loc, cond);
4501 cond = c_fully_fold (cond, false, NULL);
4502 if (warn_sequence_point)
4503 verify_sequence_points (cond);
4504 return cond;
4507 /* Parse a parenthesized condition from an if, do or while statement.
4509 condition:
4510 ( expression )
4512 static tree
4513 c_parser_paren_condition (c_parser *parser)
4515 tree cond;
4516 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4517 return error_mark_node;
4518 cond = c_parser_condition (parser);
4519 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4520 return cond;
4523 /* Parse a statement which is a block in C99. */
4525 static tree
4526 c_parser_c99_block_statement (c_parser *parser)
4528 tree block = c_begin_compound_stmt (flag_isoc99);
4529 location_t loc = c_parser_peek_token (parser)->location;
4530 c_parser_statement (parser);
4531 return c_end_compound_stmt (loc, block, flag_isoc99);
4534 /* Parse the body of an if statement. This is just parsing a
4535 statement but (a) it is a block in C99, (b) we track whether the
4536 body is an if statement for the sake of -Wparentheses warnings, (c)
4537 we handle an empty body specially for the sake of -Wempty-body
4538 warnings, and (d) we call parser_compound_statement directly
4539 because c_parser_statement_after_labels resets
4540 parser->in_if_block. */
4542 static tree
4543 c_parser_if_body (c_parser *parser, bool *if_p)
4545 tree block = c_begin_compound_stmt (flag_isoc99);
4546 location_t body_loc = c_parser_peek_token (parser)->location;
4547 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4548 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4549 || (c_parser_next_token_is (parser, CPP_NAME)
4550 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4551 c_parser_label (parser);
4552 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4553 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4555 location_t loc = c_parser_peek_token (parser)->location;
4556 add_stmt (build_empty_stmt (loc));
4557 c_parser_consume_token (parser);
4558 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4559 warning_at (loc, OPT_Wempty_body,
4560 "suggest braces around empty body in an %<if%> statement");
4562 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4563 add_stmt (c_parser_compound_statement (parser));
4564 else
4565 c_parser_statement_after_labels (parser);
4566 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4569 /* Parse the else body of an if statement. This is just parsing a
4570 statement but (a) it is a block in C99, (b) we handle an empty body
4571 specially for the sake of -Wempty-body warnings. */
4573 static tree
4574 c_parser_else_body (c_parser *parser)
4576 location_t else_loc = c_parser_peek_token (parser)->location;
4577 tree block = c_begin_compound_stmt (flag_isoc99);
4578 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4579 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4580 || (c_parser_next_token_is (parser, CPP_NAME)
4581 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4582 c_parser_label (parser);
4583 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4585 location_t loc = c_parser_peek_token (parser)->location;
4586 warning_at (loc,
4587 OPT_Wempty_body,
4588 "suggest braces around empty body in an %<else%> statement");
4589 add_stmt (build_empty_stmt (loc));
4590 c_parser_consume_token (parser);
4592 else
4593 c_parser_statement_after_labels (parser);
4594 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4597 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4599 if-statement:
4600 if ( expression ) statement
4601 if ( expression ) statement else statement
4604 static void
4605 c_parser_if_statement (c_parser *parser)
4607 tree block;
4608 location_t loc;
4609 tree cond;
4610 bool first_if = false;
4611 tree first_body, second_body;
4612 bool in_if_block;
4614 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4615 c_parser_consume_token (parser);
4616 block = c_begin_compound_stmt (flag_isoc99);
4617 loc = c_parser_peek_token (parser)->location;
4618 cond = c_parser_paren_condition (parser);
4619 in_if_block = parser->in_if_block;
4620 parser->in_if_block = true;
4621 first_body = c_parser_if_body (parser, &first_if);
4622 parser->in_if_block = in_if_block;
4623 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4625 c_parser_consume_token (parser);
4626 second_body = c_parser_else_body (parser);
4628 else
4629 second_body = NULL_TREE;
4630 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4631 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4634 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4636 switch-statement:
4637 switch (expression) statement
4640 static void
4641 c_parser_switch_statement (c_parser *parser)
4643 tree block, expr, body, save_break;
4644 location_t switch_loc = c_parser_peek_token (parser)->location;
4645 location_t switch_cond_loc;
4646 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4647 c_parser_consume_token (parser);
4648 block = c_begin_compound_stmt (flag_isoc99);
4649 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4651 switch_cond_loc = c_parser_peek_token (parser)->location;
4652 expr = c_parser_expression (parser).value;
4653 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4655 else
4657 switch_cond_loc = UNKNOWN_LOCATION;
4658 expr = error_mark_node;
4660 c_start_case (switch_loc, switch_cond_loc, expr);
4661 save_break = c_break_label;
4662 c_break_label = NULL_TREE;
4663 body = c_parser_c99_block_statement (parser);
4664 c_finish_case (body);
4665 if (c_break_label)
4667 location_t here = c_parser_peek_token (parser)->location;
4668 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4669 SET_EXPR_LOCATION (t, here);
4670 add_stmt (t);
4672 c_break_label = save_break;
4673 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4676 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4678 while-statement:
4679 while (expression) statement
4682 static void
4683 c_parser_while_statement (c_parser *parser)
4685 tree block, cond, body, save_break, save_cont;
4686 location_t loc;
4687 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4688 c_parser_consume_token (parser);
4689 block = c_begin_compound_stmt (flag_isoc99);
4690 loc = c_parser_peek_token (parser)->location;
4691 cond = c_parser_paren_condition (parser);
4692 save_break = c_break_label;
4693 c_break_label = NULL_TREE;
4694 save_cont = c_cont_label;
4695 c_cont_label = NULL_TREE;
4696 body = c_parser_c99_block_statement (parser);
4697 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4698 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4699 c_break_label = save_break;
4700 c_cont_label = save_cont;
4703 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4705 do-statement:
4706 do statement while ( expression ) ;
4709 static void
4710 c_parser_do_statement (c_parser *parser)
4712 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4713 location_t loc;
4714 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4715 c_parser_consume_token (parser);
4716 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4717 warning_at (c_parser_peek_token (parser)->location,
4718 OPT_Wempty_body,
4719 "suggest braces around empty body in %<do%> statement");
4720 block = c_begin_compound_stmt (flag_isoc99);
4721 loc = c_parser_peek_token (parser)->location;
4722 save_break = c_break_label;
4723 c_break_label = NULL_TREE;
4724 save_cont = c_cont_label;
4725 c_cont_label = NULL_TREE;
4726 body = c_parser_c99_block_statement (parser);
4727 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4728 new_break = c_break_label;
4729 c_break_label = save_break;
4730 new_cont = c_cont_label;
4731 c_cont_label = save_cont;
4732 cond = c_parser_paren_condition (parser);
4733 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4734 c_parser_skip_to_end_of_block_or_statement (parser);
4735 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4736 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4739 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4741 for-statement:
4742 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4743 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4745 The form with a declaration is new in C99.
4747 ??? In accordance with the old parser, the declaration may be a
4748 nested function, which is then rejected in check_for_loop_decls,
4749 but does it make any sense for this to be included in the grammar?
4750 Note in particular that the nested function does not include a
4751 trailing ';', whereas the "declaration" production includes one.
4752 Also, can we reject bad declarations earlier and cheaper than
4753 check_for_loop_decls?
4755 In Objective-C, there are two additional variants:
4757 foreach-statement:
4758 for ( expression in expresssion ) statement
4759 for ( declaration in expression ) statement
4761 This is inconsistent with C, because the second variant is allowed
4762 even if c99 is not enabled.
4764 The rest of the comment documents these Objective-C foreach-statement.
4766 Here is the canonical example of the first variant:
4767 for (object in array) { do something with object }
4768 we call the first expression ("object") the "object_expression" and
4769 the second expression ("array") the "collection_expression".
4770 object_expression must be an lvalue of type "id" (a generic Objective-C
4771 object) because the loop works by assigning to object_expression the
4772 various objects from the collection_expression. collection_expression
4773 must evaluate to something of type "id" which responds to the method
4774 countByEnumeratingWithState:objects:count:.
4776 The canonical example of the second variant is:
4777 for (id object in array) { do something with object }
4778 which is completely equivalent to
4780 id object;
4781 for (object in array) { do something with object }
4783 Note that initizializing 'object' in some way (eg, "for ((object =
4784 xxx) in array) { do something with object }") is possibly
4785 technically valid, but completely pointless as 'object' will be
4786 assigned to something else as soon as the loop starts. We should
4787 most likely reject it (TODO).
4789 The beginning of the Objective-C foreach-statement looks exactly
4790 like the beginning of the for-statement, and we can tell it is a
4791 foreach-statement only because the initial declaration or
4792 expression is terminated by 'in' instead of ';'.
4795 static void
4796 c_parser_for_statement (c_parser *parser)
4798 tree block, cond, incr, save_break, save_cont, body;
4799 /* The following are only used when parsing an ObjC foreach statement. */
4800 tree object_expression;
4801 /* Silence the bogus uninitialized warning. */
4802 tree collection_expression = NULL;
4803 location_t loc = c_parser_peek_token (parser)->location;
4804 location_t for_loc = c_parser_peek_token (parser)->location;
4805 bool is_foreach_statement = false;
4806 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4807 c_parser_consume_token (parser);
4808 /* Open a compound statement in Objective-C as well, just in case this is
4809 as foreach expression. */
4810 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4811 cond = error_mark_node;
4812 incr = error_mark_node;
4813 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4815 /* Parse the initialization declaration or expression. */
4816 object_expression = error_mark_node;
4817 parser->objc_could_be_foreach_context = c_dialect_objc ();
4818 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4820 parser->objc_could_be_foreach_context = false;
4821 c_parser_consume_token (parser);
4822 c_finish_expr_stmt (loc, NULL_TREE);
4824 else if (c_parser_next_tokens_start_declaration (parser))
4826 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4827 &object_expression);
4828 parser->objc_could_be_foreach_context = false;
4830 if (c_parser_next_token_is_keyword (parser, RID_IN))
4832 c_parser_consume_token (parser);
4833 is_foreach_statement = true;
4834 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4835 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4837 else
4838 check_for_loop_decls (for_loc, flag_isoc99);
4840 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4842 /* __extension__ can start a declaration, but is also an
4843 unary operator that can start an expression. Consume all
4844 but the last of a possible series of __extension__ to
4845 determine which. */
4846 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4847 && (c_parser_peek_2nd_token (parser)->keyword
4848 == RID_EXTENSION))
4849 c_parser_consume_token (parser);
4850 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4852 int ext;
4853 ext = disable_extension_diagnostics ();
4854 c_parser_consume_token (parser);
4855 c_parser_declaration_or_fndef (parser, true, true, true, true,
4856 true, &object_expression);
4857 parser->objc_could_be_foreach_context = false;
4859 restore_extension_diagnostics (ext);
4860 if (c_parser_next_token_is_keyword (parser, RID_IN))
4862 c_parser_consume_token (parser);
4863 is_foreach_statement = true;
4864 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4865 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4867 else
4868 check_for_loop_decls (for_loc, flag_isoc99);
4870 else
4871 goto init_expr;
4873 else
4875 init_expr:
4877 tree init_expression;
4878 init_expression = c_parser_expression (parser).value;
4879 parser->objc_could_be_foreach_context = false;
4880 if (c_parser_next_token_is_keyword (parser, RID_IN))
4882 c_parser_consume_token (parser);
4883 is_foreach_statement = true;
4884 if (! lvalue_p (init_expression))
4885 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4886 object_expression = c_fully_fold (init_expression, false, NULL);
4888 else
4890 c_finish_expr_stmt (loc, init_expression);
4891 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4895 /* Parse the loop condition. In the case of a foreach
4896 statement, there is no loop condition. */
4897 gcc_assert (!parser->objc_could_be_foreach_context);
4898 if (!is_foreach_statement)
4900 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4902 c_parser_consume_token (parser);
4903 cond = NULL_TREE;
4905 else
4907 cond = c_parser_condition (parser);
4908 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4911 /* Parse the increment expression (the third expression in a
4912 for-statement). In the case of a foreach-statement, this is
4913 the expression that follows the 'in'. */
4914 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4916 if (is_foreach_statement)
4918 c_parser_error (parser, "missing collection in fast enumeration");
4919 collection_expression = error_mark_node;
4921 else
4922 incr = c_process_expr_stmt (loc, NULL_TREE);
4924 else
4926 if (is_foreach_statement)
4927 collection_expression = c_fully_fold (c_parser_expression (parser).value,
4928 false, NULL);
4929 else
4930 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
4932 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4934 save_break = c_break_label;
4935 c_break_label = NULL_TREE;
4936 save_cont = c_cont_label;
4937 c_cont_label = NULL_TREE;
4938 body = c_parser_c99_block_statement (parser);
4939 if (is_foreach_statement)
4940 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
4941 else
4942 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
4943 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
4944 c_break_label = save_break;
4945 c_cont_label = save_cont;
4948 /* Parse an asm statement, a GNU extension. This is a full-blown asm
4949 statement with inputs, outputs, clobbers, and volatile tag
4950 allowed.
4952 asm-statement:
4953 asm type-qualifier[opt] ( asm-argument ) ;
4954 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
4956 asm-argument:
4957 asm-string-literal
4958 asm-string-literal : asm-operands[opt]
4959 asm-string-literal : asm-operands[opt] : asm-operands[opt]
4960 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
4962 asm-goto-argument:
4963 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
4964 : asm-goto-operands
4966 Qualifiers other than volatile are accepted in the syntax but
4967 warned for. */
4969 static tree
4970 c_parser_asm_statement (c_parser *parser)
4972 tree quals, str, outputs, inputs, clobbers, labels, ret;
4973 bool simple, is_goto;
4974 location_t asm_loc = c_parser_peek_token (parser)->location;
4975 int section, nsections;
4977 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4978 c_parser_consume_token (parser);
4979 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
4981 quals = c_parser_peek_token (parser)->value;
4982 c_parser_consume_token (parser);
4984 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
4985 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
4987 warning_at (c_parser_peek_token (parser)->location,
4989 "%E qualifier ignored on asm",
4990 c_parser_peek_token (parser)->value);
4991 quals = NULL_TREE;
4992 c_parser_consume_token (parser);
4994 else
4995 quals = NULL_TREE;
4997 is_goto = false;
4998 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5000 c_parser_consume_token (parser);
5001 is_goto = true;
5004 /* ??? Follow the C++ parser rather than using the
5005 lex_untranslated_string kludge. */
5006 parser->lex_untranslated_string = true;
5007 ret = NULL;
5009 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5010 goto error;
5012 str = c_parser_asm_string_literal (parser);
5013 if (str == NULL_TREE)
5014 goto error_close_paren;
5016 simple = true;
5017 outputs = NULL_TREE;
5018 inputs = NULL_TREE;
5019 clobbers = NULL_TREE;
5020 labels = NULL_TREE;
5022 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5023 goto done_asm;
5025 /* Parse each colon-delimited section of operands. */
5026 nsections = 3 + is_goto;
5027 for (section = 0; section < nsections; ++section)
5029 if (!c_parser_require (parser, CPP_COLON,
5030 is_goto
5031 ? "expected %<:%>"
5032 : "expected %<:%> or %<)%>"))
5033 goto error_close_paren;
5035 /* Once past any colon, we're no longer a simple asm. */
5036 simple = false;
5038 if ((!c_parser_next_token_is (parser, CPP_COLON)
5039 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5040 || section == 3)
5041 switch (section)
5043 case 0:
5044 /* For asm goto, we don't allow output operands, but reserve
5045 the slot for a future extension that does allow them. */
5046 if (!is_goto)
5047 outputs = c_parser_asm_operands (parser, false);
5048 break;
5049 case 1:
5050 inputs = c_parser_asm_operands (parser, true);
5051 break;
5052 case 2:
5053 clobbers = c_parser_asm_clobbers (parser);
5054 break;
5055 case 3:
5056 labels = c_parser_asm_goto_operands (parser);
5057 break;
5058 default:
5059 gcc_unreachable ();
5062 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5063 goto done_asm;
5066 done_asm:
5067 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5069 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5070 goto error;
5073 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5074 c_parser_skip_to_end_of_block_or_statement (parser);
5076 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5077 clobbers, labels, simple));
5079 error:
5080 parser->lex_untranslated_string = false;
5081 return ret;
5083 error_close_paren:
5084 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5085 goto error;
5088 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5089 not outputs), apply the default conversion of functions and arrays
5090 to pointers.
5092 asm-operands:
5093 asm-operand
5094 asm-operands , asm-operand
5096 asm-operand:
5097 asm-string-literal ( expression )
5098 [ identifier ] asm-string-literal ( expression )
5101 static tree
5102 c_parser_asm_operands (c_parser *parser, bool convert_p)
5104 tree list = NULL_TREE;
5105 location_t loc;
5106 while (true)
5108 tree name, str;
5109 struct c_expr expr;
5110 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5112 c_parser_consume_token (parser);
5113 if (c_parser_next_token_is (parser, CPP_NAME))
5115 tree id = c_parser_peek_token (parser)->value;
5116 c_parser_consume_token (parser);
5117 name = build_string (IDENTIFIER_LENGTH (id),
5118 IDENTIFIER_POINTER (id));
5120 else
5122 c_parser_error (parser, "expected identifier");
5123 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5124 return NULL_TREE;
5126 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5127 "expected %<]%>");
5129 else
5130 name = NULL_TREE;
5131 str = c_parser_asm_string_literal (parser);
5132 if (str == NULL_TREE)
5133 return NULL_TREE;
5134 parser->lex_untranslated_string = false;
5135 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5137 parser->lex_untranslated_string = true;
5138 return NULL_TREE;
5140 loc = c_parser_peek_token (parser)->location;
5141 expr = c_parser_expression (parser);
5142 mark_exp_read (expr.value);
5143 if (convert_p)
5144 expr = default_function_array_conversion (loc, expr);
5145 expr.value = c_fully_fold (expr.value, false, NULL);
5146 parser->lex_untranslated_string = true;
5147 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5150 return NULL_TREE;
5152 list = chainon (list, build_tree_list (build_tree_list (name, str),
5153 expr.value));
5154 if (c_parser_next_token_is (parser, CPP_COMMA))
5155 c_parser_consume_token (parser);
5156 else
5157 break;
5159 return list;
5162 /* Parse asm clobbers, a GNU extension.
5164 asm-clobbers:
5165 asm-string-literal
5166 asm-clobbers , asm-string-literal
5169 static tree
5170 c_parser_asm_clobbers (c_parser *parser)
5172 tree list = NULL_TREE;
5173 while (true)
5175 tree str = c_parser_asm_string_literal (parser);
5176 if (str)
5177 list = tree_cons (NULL_TREE, str, list);
5178 else
5179 return NULL_TREE;
5180 if (c_parser_next_token_is (parser, CPP_COMMA))
5181 c_parser_consume_token (parser);
5182 else
5183 break;
5185 return list;
5188 /* Parse asm goto labels, a GNU extension.
5190 asm-goto-operands:
5191 identifier
5192 asm-goto-operands , identifier
5195 static tree
5196 c_parser_asm_goto_operands (c_parser *parser)
5198 tree list = NULL_TREE;
5199 while (true)
5201 tree name, label;
5203 if (c_parser_next_token_is (parser, CPP_NAME))
5205 c_token *tok = c_parser_peek_token (parser);
5206 name = tok->value;
5207 label = lookup_label_for_goto (tok->location, name);
5208 c_parser_consume_token (parser);
5209 TREE_USED (label) = 1;
5211 else
5213 c_parser_error (parser, "expected identifier");
5214 return NULL_TREE;
5217 name = build_string (IDENTIFIER_LENGTH (name),
5218 IDENTIFIER_POINTER (name));
5219 list = tree_cons (name, label, list);
5220 if (c_parser_next_token_is (parser, CPP_COMMA))
5221 c_parser_consume_token (parser);
5222 else
5223 return nreverse (list);
5227 /* Parse an expression other than a compound expression; that is, an
5228 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5229 NULL then it is an Objective-C message expression which is the
5230 primary-expression starting the expression as an initializer.
5232 assignment-expression:
5233 conditional-expression
5234 unary-expression assignment-operator assignment-expression
5236 assignment-operator: one of
5237 = *= /= %= += -= <<= >>= &= ^= |=
5239 In GNU C we accept any conditional expression on the LHS and
5240 diagnose the invalid lvalue rather than producing a syntax
5241 error. */
5243 static struct c_expr
5244 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5246 struct c_expr lhs, rhs, ret;
5247 enum tree_code code;
5248 location_t op_location, exp_location;
5249 gcc_assert (!after || c_dialect_objc ());
5250 lhs = c_parser_conditional_expression (parser, after);
5251 op_location = c_parser_peek_token (parser)->location;
5252 switch (c_parser_peek_token (parser)->type)
5254 case CPP_EQ:
5255 code = NOP_EXPR;
5256 break;
5257 case CPP_MULT_EQ:
5258 code = MULT_EXPR;
5259 break;
5260 case CPP_DIV_EQ:
5261 code = TRUNC_DIV_EXPR;
5262 break;
5263 case CPP_MOD_EQ:
5264 code = TRUNC_MOD_EXPR;
5265 break;
5266 case CPP_PLUS_EQ:
5267 code = PLUS_EXPR;
5268 break;
5269 case CPP_MINUS_EQ:
5270 code = MINUS_EXPR;
5271 break;
5272 case CPP_LSHIFT_EQ:
5273 code = LSHIFT_EXPR;
5274 break;
5275 case CPP_RSHIFT_EQ:
5276 code = RSHIFT_EXPR;
5277 break;
5278 case CPP_AND_EQ:
5279 code = BIT_AND_EXPR;
5280 break;
5281 case CPP_XOR_EQ:
5282 code = BIT_XOR_EXPR;
5283 break;
5284 case CPP_OR_EQ:
5285 code = BIT_IOR_EXPR;
5286 break;
5287 default:
5288 return lhs;
5290 c_parser_consume_token (parser);
5291 exp_location = c_parser_peek_token (parser)->location;
5292 rhs = c_parser_expr_no_commas (parser, NULL);
5293 rhs = default_function_array_read_conversion (exp_location, rhs);
5294 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5295 code, exp_location, rhs.value,
5296 rhs.original_type);
5297 if (code == NOP_EXPR)
5298 ret.original_code = MODIFY_EXPR;
5299 else
5301 TREE_NO_WARNING (ret.value) = 1;
5302 ret.original_code = ERROR_MARK;
5304 ret.original_type = NULL;
5305 return ret;
5308 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5309 is not NULL then it is an Objective-C message expression which is
5310 the primary-expression starting the expression as an initializer.
5312 conditional-expression:
5313 logical-OR-expression
5314 logical-OR-expression ? expression : conditional-expression
5316 GNU extensions:
5318 conditional-expression:
5319 logical-OR-expression ? : conditional-expression
5322 static struct c_expr
5323 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5325 struct c_expr cond, exp1, exp2, ret;
5326 location_t cond_loc, colon_loc, middle_loc;
5328 gcc_assert (!after || c_dialect_objc ());
5330 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5332 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5333 return cond;
5334 cond_loc = c_parser_peek_token (parser)->location;
5335 cond = default_function_array_read_conversion (cond_loc, cond);
5336 c_parser_consume_token (parser);
5337 if (c_parser_next_token_is (parser, CPP_COLON))
5339 tree eptype = NULL_TREE;
5341 middle_loc = c_parser_peek_token (parser)->location;
5342 pedwarn (middle_loc, OPT_pedantic,
5343 "ISO C forbids omitting the middle term of a ?: expression");
5344 warn_for_omitted_condop (middle_loc, cond.value);
5345 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5347 eptype = TREE_TYPE (cond.value);
5348 cond.value = TREE_OPERAND (cond.value, 0);
5350 /* Make sure first operand is calculated only once. */
5351 exp1.value = c_save_expr (default_conversion (cond.value));
5352 if (eptype)
5353 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5354 exp1.original_type = NULL;
5355 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5356 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5358 else
5360 cond.value
5361 = c_objc_common_truthvalue_conversion
5362 (cond_loc, default_conversion (cond.value));
5363 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5364 exp1 = c_parser_expression_conv (parser);
5365 mark_exp_read (exp1.value);
5366 c_inhibit_evaluation_warnings +=
5367 ((cond.value == truthvalue_true_node)
5368 - (cond.value == truthvalue_false_node));
5371 colon_loc = c_parser_peek_token (parser)->location;
5372 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5374 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5375 ret.value = error_mark_node;
5376 ret.original_code = ERROR_MARK;
5377 ret.original_type = NULL;
5378 return ret;
5381 location_t exp2_loc = c_parser_peek_token (parser)->location;
5382 exp2 = c_parser_conditional_expression (parser, NULL);
5383 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5385 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5386 ret.value = build_conditional_expr (colon_loc, cond.value,
5387 cond.original_code == C_MAYBE_CONST_EXPR,
5388 exp1.value, exp1.original_type,
5389 exp2.value, exp2.original_type);
5390 ret.original_code = ERROR_MARK;
5391 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5392 ret.original_type = NULL;
5393 else
5395 tree t1, t2;
5397 /* If both sides are enum type, the default conversion will have
5398 made the type of the result be an integer type. We want to
5399 remember the enum types we started with. */
5400 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5401 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5402 ret.original_type = ((t1 != error_mark_node
5403 && t2 != error_mark_node
5404 && (TYPE_MAIN_VARIANT (t1)
5405 == TYPE_MAIN_VARIANT (t2)))
5406 ? t1
5407 : NULL);
5409 return ret;
5412 /* Parse a binary expression; that is, a logical-OR-expression (C90
5413 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5414 an Objective-C message expression which is the primary-expression
5415 starting the expression as an initializer. PREC is the starting
5416 precedence, usually PREC_NONE.
5418 multiplicative-expression:
5419 cast-expression
5420 multiplicative-expression * cast-expression
5421 multiplicative-expression / cast-expression
5422 multiplicative-expression % cast-expression
5424 additive-expression:
5425 multiplicative-expression
5426 additive-expression + multiplicative-expression
5427 additive-expression - multiplicative-expression
5429 shift-expression:
5430 additive-expression
5431 shift-expression << additive-expression
5432 shift-expression >> additive-expression
5434 relational-expression:
5435 shift-expression
5436 relational-expression < shift-expression
5437 relational-expression > shift-expression
5438 relational-expression <= shift-expression
5439 relational-expression >= shift-expression
5441 equality-expression:
5442 relational-expression
5443 equality-expression == relational-expression
5444 equality-expression != relational-expression
5446 AND-expression:
5447 equality-expression
5448 AND-expression & equality-expression
5450 exclusive-OR-expression:
5451 AND-expression
5452 exclusive-OR-expression ^ AND-expression
5454 inclusive-OR-expression:
5455 exclusive-OR-expression
5456 inclusive-OR-expression | exclusive-OR-expression
5458 logical-AND-expression:
5459 inclusive-OR-expression
5460 logical-AND-expression && inclusive-OR-expression
5462 logical-OR-expression:
5463 logical-AND-expression
5464 logical-OR-expression || logical-AND-expression
5467 static struct c_expr
5468 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5469 enum c_parser_prec prec)
5471 /* A binary expression is parsed using operator-precedence parsing,
5472 with the operands being cast expressions. All the binary
5473 operators are left-associative. Thus a binary expression is of
5474 form:
5476 E0 op1 E1 op2 E2 ...
5478 which we represent on a stack. On the stack, the precedence
5479 levels are strictly increasing. When a new operator is
5480 encountered of higher precedence than that at the top of the
5481 stack, it is pushed; its LHS is the top expression, and its RHS
5482 is everything parsed until it is popped. When a new operator is
5483 encountered with precedence less than or equal to that at the top
5484 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5485 by the result of the operation until the operator at the top of
5486 the stack has lower precedence than the new operator or there is
5487 only one element on the stack; then the top expression is the LHS
5488 of the new operator. In the case of logical AND and OR
5489 expressions, we also need to adjust c_inhibit_evaluation_warnings
5490 as appropriate when the operators are pushed and popped. */
5492 struct {
5493 /* The expression at this stack level. */
5494 struct c_expr expr;
5495 /* The precedence of the operator on its left, PREC_NONE at the
5496 bottom of the stack. */
5497 enum c_parser_prec prec;
5498 /* The operation on its left. */
5499 enum tree_code op;
5500 /* The source location of this operation. */
5501 location_t loc;
5502 } stack[NUM_PRECS];
5503 int sp;
5504 /* Location of the binary operator. */
5505 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5506 #define POP \
5507 do { \
5508 switch (stack[sp].op) \
5510 case TRUTH_ANDIF_EXPR: \
5511 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5512 == truthvalue_false_node); \
5513 break; \
5514 case TRUTH_ORIF_EXPR: \
5515 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5516 == truthvalue_true_node); \
5517 break; \
5518 default: \
5519 break; \
5521 stack[sp - 1].expr \
5522 = default_function_array_read_conversion (stack[sp - 1].loc, \
5523 stack[sp - 1].expr); \
5524 stack[sp].expr \
5525 = default_function_array_read_conversion (stack[sp].loc, \
5526 stack[sp].expr); \
5527 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5528 stack[sp].op, \
5529 stack[sp - 1].expr, \
5530 stack[sp].expr); \
5531 sp--; \
5532 } while (0)
5533 gcc_assert (!after || c_dialect_objc ());
5534 stack[0].loc = c_parser_peek_token (parser)->location;
5535 stack[0].expr = c_parser_cast_expression (parser, after);
5536 stack[0].prec = prec;
5537 sp = 0;
5538 while (true)
5540 enum c_parser_prec oprec;
5541 enum tree_code ocode;
5542 if (parser->error)
5543 goto out;
5544 switch (c_parser_peek_token (parser)->type)
5546 case CPP_MULT:
5547 oprec = PREC_MULT;
5548 ocode = MULT_EXPR;
5549 break;
5550 case CPP_DIV:
5551 oprec = PREC_MULT;
5552 ocode = TRUNC_DIV_EXPR;
5553 break;
5554 case CPP_MOD:
5555 oprec = PREC_MULT;
5556 ocode = TRUNC_MOD_EXPR;
5557 break;
5558 case CPP_PLUS:
5559 oprec = PREC_ADD;
5560 ocode = PLUS_EXPR;
5561 break;
5562 case CPP_MINUS:
5563 oprec = PREC_ADD;
5564 ocode = MINUS_EXPR;
5565 break;
5566 case CPP_LSHIFT:
5567 oprec = PREC_SHIFT;
5568 ocode = LSHIFT_EXPR;
5569 break;
5570 case CPP_RSHIFT:
5571 oprec = PREC_SHIFT;
5572 ocode = RSHIFT_EXPR;
5573 break;
5574 case CPP_LESS:
5575 oprec = PREC_REL;
5576 ocode = LT_EXPR;
5577 break;
5578 case CPP_GREATER:
5579 oprec = PREC_REL;
5580 ocode = GT_EXPR;
5581 break;
5582 case CPP_LESS_EQ:
5583 oprec = PREC_REL;
5584 ocode = LE_EXPR;
5585 break;
5586 case CPP_GREATER_EQ:
5587 oprec = PREC_REL;
5588 ocode = GE_EXPR;
5589 break;
5590 case CPP_EQ_EQ:
5591 oprec = PREC_EQ;
5592 ocode = EQ_EXPR;
5593 break;
5594 case CPP_NOT_EQ:
5595 oprec = PREC_EQ;
5596 ocode = NE_EXPR;
5597 break;
5598 case CPP_AND:
5599 oprec = PREC_BITAND;
5600 ocode = BIT_AND_EXPR;
5601 break;
5602 case CPP_XOR:
5603 oprec = PREC_BITXOR;
5604 ocode = BIT_XOR_EXPR;
5605 break;
5606 case CPP_OR:
5607 oprec = PREC_BITOR;
5608 ocode = BIT_IOR_EXPR;
5609 break;
5610 case CPP_AND_AND:
5611 oprec = PREC_LOGAND;
5612 ocode = TRUTH_ANDIF_EXPR;
5613 break;
5614 case CPP_OR_OR:
5615 oprec = PREC_LOGOR;
5616 ocode = TRUTH_ORIF_EXPR;
5617 break;
5618 default:
5619 /* Not a binary operator, so end of the binary
5620 expression. */
5621 goto out;
5623 binary_loc = c_parser_peek_token (parser)->location;
5624 while (oprec <= stack[sp].prec)
5626 if (sp == 0)
5627 goto out;
5628 POP;
5630 c_parser_consume_token (parser);
5631 switch (ocode)
5633 case TRUTH_ANDIF_EXPR:
5634 stack[sp].expr
5635 = default_function_array_read_conversion (stack[sp].loc,
5636 stack[sp].expr);
5637 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5638 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5639 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5640 == truthvalue_false_node);
5641 break;
5642 case TRUTH_ORIF_EXPR:
5643 stack[sp].expr
5644 = default_function_array_read_conversion (stack[sp].loc,
5645 stack[sp].expr);
5646 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5647 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5648 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5649 == truthvalue_true_node);
5650 break;
5651 default:
5652 break;
5654 sp++;
5655 stack[sp].loc = binary_loc;
5656 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5657 stack[sp].prec = oprec;
5658 stack[sp].op = ocode;
5659 stack[sp].loc = binary_loc;
5661 out:
5662 while (sp > 0)
5663 POP;
5664 return stack[0].expr;
5665 #undef POP
5668 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5669 NULL then it is an Objective-C message expression which is the
5670 primary-expression starting the expression as an initializer.
5672 cast-expression:
5673 unary-expression
5674 ( type-name ) unary-expression
5677 static struct c_expr
5678 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5680 location_t cast_loc = c_parser_peek_token (parser)->location;
5681 gcc_assert (!after || c_dialect_objc ());
5682 if (after)
5683 return c_parser_postfix_expression_after_primary (parser,
5684 cast_loc, *after);
5685 /* If the expression begins with a parenthesized type name, it may
5686 be either a cast or a compound literal; we need to see whether
5687 the next character is '{' to tell the difference. If not, it is
5688 an unary expression. Full detection of unknown typenames here
5689 would require a 3-token lookahead. */
5690 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5691 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5693 struct c_type_name *type_name;
5694 struct c_expr ret;
5695 struct c_expr expr;
5696 c_parser_consume_token (parser);
5697 type_name = c_parser_type_name (parser);
5698 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5699 if (type_name == NULL)
5701 ret.value = error_mark_node;
5702 ret.original_code = ERROR_MARK;
5703 ret.original_type = NULL;
5704 return ret;
5707 /* Save casted types in the function's used types hash table. */
5708 used_types_insert (type_name->specs->type);
5710 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5711 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5712 cast_loc);
5714 location_t expr_loc = c_parser_peek_token (parser)->location;
5715 expr = c_parser_cast_expression (parser, NULL);
5716 expr = default_function_array_read_conversion (expr_loc, expr);
5718 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5719 ret.original_code = ERROR_MARK;
5720 ret.original_type = NULL;
5721 return ret;
5723 else
5724 return c_parser_unary_expression (parser);
5727 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5729 unary-expression:
5730 postfix-expression
5731 ++ unary-expression
5732 -- unary-expression
5733 unary-operator cast-expression
5734 sizeof unary-expression
5735 sizeof ( type-name )
5737 unary-operator: one of
5738 & * + - ~ !
5740 GNU extensions:
5742 unary-expression:
5743 __alignof__ unary-expression
5744 __alignof__ ( type-name )
5745 && identifier
5747 unary-operator: one of
5748 __extension__ __real__ __imag__
5750 In addition, the GNU syntax treats ++ and -- as unary operators, so
5751 they may be applied to cast expressions with errors for non-lvalues
5752 given later. */
5754 static struct c_expr
5755 c_parser_unary_expression (c_parser *parser)
5757 int ext;
5758 struct c_expr ret, op;
5759 location_t op_loc = c_parser_peek_token (parser)->location;
5760 location_t exp_loc;
5761 ret.original_code = ERROR_MARK;
5762 ret.original_type = NULL;
5763 switch (c_parser_peek_token (parser)->type)
5765 case CPP_PLUS_PLUS:
5766 c_parser_consume_token (parser);
5767 exp_loc = c_parser_peek_token (parser)->location;
5768 op = c_parser_cast_expression (parser, NULL);
5769 op = default_function_array_read_conversion (exp_loc, op);
5770 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5771 case CPP_MINUS_MINUS:
5772 c_parser_consume_token (parser);
5773 exp_loc = c_parser_peek_token (parser)->location;
5774 op = c_parser_cast_expression (parser, NULL);
5775 op = default_function_array_read_conversion (exp_loc, op);
5776 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5777 case CPP_AND:
5778 c_parser_consume_token (parser);
5779 op = c_parser_cast_expression (parser, NULL);
5780 mark_exp_read (op.value);
5781 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5782 case CPP_MULT:
5783 c_parser_consume_token (parser);
5784 exp_loc = c_parser_peek_token (parser)->location;
5785 op = c_parser_cast_expression (parser, NULL);
5786 op = default_function_array_read_conversion (exp_loc, op);
5787 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5788 return ret;
5789 case CPP_PLUS:
5790 if (!c_dialect_objc () && !in_system_header)
5791 warning_at (op_loc,
5792 OPT_Wtraditional,
5793 "traditional C rejects the unary plus operator");
5794 c_parser_consume_token (parser);
5795 exp_loc = c_parser_peek_token (parser)->location;
5796 op = c_parser_cast_expression (parser, NULL);
5797 op = default_function_array_read_conversion (exp_loc, op);
5798 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5799 case CPP_MINUS:
5800 c_parser_consume_token (parser);
5801 exp_loc = c_parser_peek_token (parser)->location;
5802 op = c_parser_cast_expression (parser, NULL);
5803 op = default_function_array_read_conversion (exp_loc, op);
5804 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5805 case CPP_COMPL:
5806 c_parser_consume_token (parser);
5807 exp_loc = c_parser_peek_token (parser)->location;
5808 op = c_parser_cast_expression (parser, NULL);
5809 op = default_function_array_read_conversion (exp_loc, op);
5810 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5811 case CPP_NOT:
5812 c_parser_consume_token (parser);
5813 exp_loc = c_parser_peek_token (parser)->location;
5814 op = c_parser_cast_expression (parser, NULL);
5815 op = default_function_array_read_conversion (exp_loc, op);
5816 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5817 case CPP_AND_AND:
5818 /* Refer to the address of a label as a pointer. */
5819 c_parser_consume_token (parser);
5820 if (c_parser_next_token_is (parser, CPP_NAME))
5822 ret.value = finish_label_address_expr
5823 (c_parser_peek_token (parser)->value, op_loc);
5824 c_parser_consume_token (parser);
5826 else
5828 c_parser_error (parser, "expected identifier");
5829 ret.value = error_mark_node;
5831 return ret;
5832 case CPP_KEYWORD:
5833 switch (c_parser_peek_token (parser)->keyword)
5835 case RID_SIZEOF:
5836 return c_parser_sizeof_expression (parser);
5837 case RID_ALIGNOF:
5838 return c_parser_alignof_expression (parser);
5839 case RID_EXTENSION:
5840 c_parser_consume_token (parser);
5841 ext = disable_extension_diagnostics ();
5842 ret = c_parser_cast_expression (parser, NULL);
5843 restore_extension_diagnostics (ext);
5844 return ret;
5845 case RID_REALPART:
5846 c_parser_consume_token (parser);
5847 exp_loc = c_parser_peek_token (parser)->location;
5848 op = c_parser_cast_expression (parser, NULL);
5849 op = default_function_array_conversion (exp_loc, op);
5850 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5851 case RID_IMAGPART:
5852 c_parser_consume_token (parser);
5853 exp_loc = c_parser_peek_token (parser)->location;
5854 op = c_parser_cast_expression (parser, NULL);
5855 op = default_function_array_conversion (exp_loc, op);
5856 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5857 default:
5858 return c_parser_postfix_expression (parser);
5860 default:
5861 return c_parser_postfix_expression (parser);
5865 /* Parse a sizeof expression. */
5867 static struct c_expr
5868 c_parser_sizeof_expression (c_parser *parser)
5870 struct c_expr expr;
5871 location_t expr_loc;
5872 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5873 c_parser_consume_token (parser);
5874 c_inhibit_evaluation_warnings++;
5875 in_sizeof++;
5876 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5877 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5879 /* Either sizeof ( type-name ) or sizeof unary-expression
5880 starting with a compound literal. */
5881 struct c_type_name *type_name;
5882 c_parser_consume_token (parser);
5883 expr_loc = c_parser_peek_token (parser)->location;
5884 type_name = c_parser_type_name (parser);
5885 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5886 if (type_name == NULL)
5888 struct c_expr ret;
5889 c_inhibit_evaluation_warnings--;
5890 in_sizeof--;
5891 ret.value = error_mark_node;
5892 ret.original_code = ERROR_MARK;
5893 ret.original_type = NULL;
5894 return ret;
5896 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5898 expr = c_parser_postfix_expression_after_paren_type (parser,
5899 type_name,
5900 expr_loc);
5901 goto sizeof_expr;
5903 /* sizeof ( type-name ). */
5904 c_inhibit_evaluation_warnings--;
5905 in_sizeof--;
5906 return c_expr_sizeof_type (expr_loc, type_name);
5908 else
5910 expr_loc = c_parser_peek_token (parser)->location;
5911 expr = c_parser_unary_expression (parser);
5912 sizeof_expr:
5913 c_inhibit_evaluation_warnings--;
5914 in_sizeof--;
5915 mark_exp_read (expr.value);
5916 if (TREE_CODE (expr.value) == COMPONENT_REF
5917 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
5918 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
5919 return c_expr_sizeof_expr (expr_loc, expr);
5923 /* Parse an alignof expression. */
5925 static struct c_expr
5926 c_parser_alignof_expression (c_parser *parser)
5928 struct c_expr expr;
5929 location_t loc = c_parser_peek_token (parser)->location;
5930 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
5931 c_parser_consume_token (parser);
5932 c_inhibit_evaluation_warnings++;
5933 in_alignof++;
5934 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5935 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5937 /* Either __alignof__ ( type-name ) or __alignof__
5938 unary-expression starting with a compound literal. */
5939 location_t loc;
5940 struct c_type_name *type_name;
5941 struct c_expr ret;
5942 c_parser_consume_token (parser);
5943 loc = c_parser_peek_token (parser)->location;
5944 type_name = c_parser_type_name (parser);
5945 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5946 if (type_name == NULL)
5948 struct c_expr ret;
5949 c_inhibit_evaluation_warnings--;
5950 in_alignof--;
5951 ret.value = error_mark_node;
5952 ret.original_code = ERROR_MARK;
5953 ret.original_type = NULL;
5954 return ret;
5956 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5958 expr = c_parser_postfix_expression_after_paren_type (parser,
5959 type_name,
5960 loc);
5961 goto alignof_expr;
5963 /* alignof ( type-name ). */
5964 c_inhibit_evaluation_warnings--;
5965 in_alignof--;
5966 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
5967 ret.original_code = ERROR_MARK;
5968 ret.original_type = NULL;
5969 return ret;
5971 else
5973 struct c_expr ret;
5974 expr = c_parser_unary_expression (parser);
5975 alignof_expr:
5976 mark_exp_read (expr.value);
5977 c_inhibit_evaluation_warnings--;
5978 in_alignof--;
5979 ret.value = c_alignof_expr (loc, expr.value);
5980 ret.original_code = ERROR_MARK;
5981 ret.original_type = NULL;
5982 return ret;
5986 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
5988 postfix-expression:
5989 primary-expression
5990 postfix-expression [ expression ]
5991 postfix-expression ( argument-expression-list[opt] )
5992 postfix-expression . identifier
5993 postfix-expression -> identifier
5994 postfix-expression ++
5995 postfix-expression --
5996 ( type-name ) { initializer-list }
5997 ( type-name ) { initializer-list , }
5999 argument-expression-list:
6000 argument-expression
6001 argument-expression-list , argument-expression
6003 primary-expression:
6004 identifier
6005 constant
6006 string-literal
6007 ( expression )
6009 GNU extensions:
6011 primary-expression:
6012 __func__
6013 (treated as a keyword in GNU C)
6014 __FUNCTION__
6015 __PRETTY_FUNCTION__
6016 ( compound-statement )
6017 __builtin_va_arg ( assignment-expression , type-name )
6018 __builtin_offsetof ( type-name , offsetof-member-designator )
6019 __builtin_choose_expr ( assignment-expression ,
6020 assignment-expression ,
6021 assignment-expression )
6022 __builtin_types_compatible_p ( type-name , type-name )
6024 offsetof-member-designator:
6025 identifier
6026 offsetof-member-designator . identifier
6027 offsetof-member-designator [ expression ]
6029 Objective-C:
6031 primary-expression:
6032 [ objc-receiver objc-message-args ]
6033 @selector ( objc-selector-arg )
6034 @protocol ( identifier )
6035 @encode ( type-name )
6036 objc-string-literal
6037 Classname . identifier
6040 static struct c_expr
6041 c_parser_postfix_expression (c_parser *parser)
6043 struct c_expr expr, e1, e2, e3;
6044 struct c_type_name *t1, *t2;
6045 location_t loc = c_parser_peek_token (parser)->location;;
6046 expr.original_code = ERROR_MARK;
6047 expr.original_type = NULL;
6048 switch (c_parser_peek_token (parser)->type)
6050 case CPP_NUMBER:
6051 expr.value = c_parser_peek_token (parser)->value;
6052 loc = c_parser_peek_token (parser)->location;
6053 c_parser_consume_token (parser);
6054 if (TREE_CODE (expr.value) == FIXED_CST
6055 && !targetm.fixed_point_supported_p ())
6057 error_at (loc, "fixed-point types not supported for this target");
6058 expr.value = error_mark_node;
6060 break;
6061 case CPP_CHAR:
6062 case CPP_CHAR16:
6063 case CPP_CHAR32:
6064 case CPP_WCHAR:
6065 expr.value = c_parser_peek_token (parser)->value;
6066 c_parser_consume_token (parser);
6067 break;
6068 case CPP_STRING:
6069 case CPP_STRING16:
6070 case CPP_STRING32:
6071 case CPP_WSTRING:
6072 case CPP_UTF8STRING:
6073 expr.value = c_parser_peek_token (parser)->value;
6074 expr.original_code = STRING_CST;
6075 c_parser_consume_token (parser);
6076 break;
6077 case CPP_OBJC_STRING:
6078 gcc_assert (c_dialect_objc ());
6079 expr.value
6080 = objc_build_string_object (c_parser_peek_token (parser)->value);
6081 c_parser_consume_token (parser);
6082 break;
6083 case CPP_NAME:
6084 switch (c_parser_peek_token (parser)->id_kind)
6086 case C_ID_ID:
6088 tree id = c_parser_peek_token (parser)->value;
6089 c_parser_consume_token (parser);
6090 expr.value = build_external_ref (loc, id,
6091 (c_parser_peek_token (parser)->type
6092 == CPP_OPEN_PAREN),
6093 &expr.original_type);
6094 break;
6096 case C_ID_CLASSNAME:
6098 /* Here we parse the Objective-C 2.0 Class.name dot
6099 syntax. */
6100 tree class_name = c_parser_peek_token (parser)->value;
6101 tree component;
6102 c_parser_consume_token (parser);
6103 gcc_assert (c_dialect_objc ());
6104 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6106 expr.value = error_mark_node;
6107 break;
6109 if (c_parser_next_token_is_not (parser, CPP_NAME))
6111 c_parser_error (parser, "expected identifier");
6112 expr.value = error_mark_node;
6113 break;
6115 component = c_parser_peek_token (parser)->value;
6116 c_parser_consume_token (parser);
6117 expr.value = objc_build_class_component_ref (class_name,
6118 component);
6119 break;
6121 default:
6122 c_parser_error (parser, "expected expression");
6123 expr.value = error_mark_node;
6124 break;
6126 break;
6127 case CPP_OPEN_PAREN:
6128 /* A parenthesized expression, statement expression or compound
6129 literal. */
6130 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6132 /* A statement expression. */
6133 tree stmt;
6134 location_t brace_loc;
6135 c_parser_consume_token (parser);
6136 brace_loc = c_parser_peek_token (parser)->location;
6137 c_parser_consume_token (parser);
6138 if (!building_stmt_list_p ())
6140 error_at (loc, "braced-group within expression allowed "
6141 "only inside a function");
6142 parser->error = true;
6143 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6144 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6145 expr.value = error_mark_node;
6146 break;
6148 stmt = c_begin_stmt_expr ();
6149 c_parser_compound_statement_nostart (parser);
6150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6151 "expected %<)%>");
6152 pedwarn (loc, OPT_pedantic,
6153 "ISO C forbids braced-groups within expressions");
6154 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6155 mark_exp_read (expr.value);
6157 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6159 /* A compound literal. ??? Can we actually get here rather
6160 than going directly to
6161 c_parser_postfix_expression_after_paren_type from
6162 elsewhere? */
6163 location_t loc;
6164 struct c_type_name *type_name;
6165 c_parser_consume_token (parser);
6166 loc = c_parser_peek_token (parser)->location;
6167 type_name = c_parser_type_name (parser);
6168 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6169 "expected %<)%>");
6170 if (type_name == NULL)
6172 expr.value = error_mark_node;
6174 else
6175 expr = c_parser_postfix_expression_after_paren_type (parser,
6176 type_name,
6177 loc);
6179 else
6181 /* A parenthesized expression. */
6182 c_parser_consume_token (parser);
6183 expr = c_parser_expression (parser);
6184 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6185 TREE_NO_WARNING (expr.value) = 1;
6186 if (expr.original_code != C_MAYBE_CONST_EXPR)
6187 expr.original_code = ERROR_MARK;
6188 /* Don't change EXPR.ORIGINAL_TYPE. */
6189 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6190 "expected %<)%>");
6192 break;
6193 case CPP_KEYWORD:
6194 switch (c_parser_peek_token (parser)->keyword)
6196 case RID_FUNCTION_NAME:
6197 case RID_PRETTY_FUNCTION_NAME:
6198 case RID_C99_FUNCTION_NAME:
6199 expr.value = fname_decl (loc,
6200 c_parser_peek_token (parser)->keyword,
6201 c_parser_peek_token (parser)->value);
6202 c_parser_consume_token (parser);
6203 break;
6204 case RID_VA_ARG:
6205 c_parser_consume_token (parser);
6206 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6208 expr.value = error_mark_node;
6209 break;
6211 e1 = c_parser_expr_no_commas (parser, NULL);
6212 mark_exp_read (e1.value);
6213 e1.value = c_fully_fold (e1.value, false, NULL);
6214 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6216 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6217 expr.value = error_mark_node;
6218 break;
6220 loc = c_parser_peek_token (parser)->location;
6221 t1 = c_parser_type_name (parser);
6222 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6223 "expected %<)%>");
6224 if (t1 == NULL)
6226 expr.value = error_mark_node;
6228 else
6230 tree type_expr = NULL_TREE;
6231 expr.value = c_build_va_arg (loc, e1.value,
6232 groktypename (t1, &type_expr, NULL));
6233 if (type_expr)
6235 expr.value = build2 (C_MAYBE_CONST_EXPR,
6236 TREE_TYPE (expr.value), type_expr,
6237 expr.value);
6238 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6241 break;
6242 case RID_OFFSETOF:
6243 c_parser_consume_token (parser);
6244 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6246 expr.value = error_mark_node;
6247 break;
6249 t1 = c_parser_type_name (parser);
6250 if (t1 == NULL)
6251 parser->error = true;
6252 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6253 gcc_assert (parser->error);
6254 if (parser->error)
6256 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6257 expr.value = error_mark_node;
6258 break;
6262 tree type = groktypename (t1, NULL, NULL);
6263 tree offsetof_ref;
6264 if (type == error_mark_node)
6265 offsetof_ref = error_mark_node;
6266 else
6268 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6269 SET_EXPR_LOCATION (offsetof_ref, loc);
6271 /* Parse the second argument to __builtin_offsetof. We
6272 must have one identifier, and beyond that we want to
6273 accept sub structure and sub array references. */
6274 if (c_parser_next_token_is (parser, CPP_NAME))
6276 offsetof_ref = build_component_ref
6277 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6278 c_parser_consume_token (parser);
6279 while (c_parser_next_token_is (parser, CPP_DOT)
6280 || c_parser_next_token_is (parser,
6281 CPP_OPEN_SQUARE)
6282 || c_parser_next_token_is (parser,
6283 CPP_DEREF))
6285 if (c_parser_next_token_is (parser, CPP_DEREF))
6287 loc = c_parser_peek_token (parser)->location;
6288 offsetof_ref = build_array_ref (loc,
6289 offsetof_ref,
6290 integer_zero_node);
6291 goto do_dot;
6293 else if (c_parser_next_token_is (parser, CPP_DOT))
6295 do_dot:
6296 c_parser_consume_token (parser);
6297 if (c_parser_next_token_is_not (parser,
6298 CPP_NAME))
6300 c_parser_error (parser, "expected identifier");
6301 break;
6303 offsetof_ref = build_component_ref
6304 (loc, offsetof_ref,
6305 c_parser_peek_token (parser)->value);
6306 c_parser_consume_token (parser);
6308 else
6310 tree idx;
6311 loc = c_parser_peek_token (parser)->location;
6312 c_parser_consume_token (parser);
6313 idx = c_parser_expression (parser).value;
6314 idx = c_fully_fold (idx, false, NULL);
6315 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6316 "expected %<]%>");
6317 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6321 else
6322 c_parser_error (parser, "expected identifier");
6323 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6324 "expected %<)%>");
6325 expr.value = fold_offsetof (offsetof_ref, NULL_TREE);
6327 break;
6328 case RID_CHOOSE_EXPR:
6329 c_parser_consume_token (parser);
6330 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6332 expr.value = error_mark_node;
6333 break;
6335 loc = c_parser_peek_token (parser)->location;
6336 e1 = c_parser_expr_no_commas (parser, NULL);
6337 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6339 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6340 expr.value = error_mark_node;
6341 break;
6343 e2 = c_parser_expr_no_commas (parser, NULL);
6344 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6346 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6347 expr.value = error_mark_node;
6348 break;
6350 e3 = c_parser_expr_no_commas (parser, NULL);
6351 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6352 "expected %<)%>");
6354 tree c;
6356 c = e1.value;
6357 mark_exp_read (e2.value);
6358 mark_exp_read (e3.value);
6359 if (TREE_CODE (c) != INTEGER_CST
6360 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6361 error_at (loc,
6362 "first argument to %<__builtin_choose_expr%> not"
6363 " a constant");
6364 constant_expression_warning (c);
6365 expr = integer_zerop (c) ? e3 : e2;
6367 break;
6368 case RID_TYPES_COMPATIBLE_P:
6369 c_parser_consume_token (parser);
6370 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6372 expr.value = error_mark_node;
6373 break;
6375 t1 = c_parser_type_name (parser);
6376 if (t1 == NULL)
6378 expr.value = error_mark_node;
6379 break;
6381 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6383 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6384 expr.value = error_mark_node;
6385 break;
6387 t2 = c_parser_type_name (parser);
6388 if (t2 == NULL)
6390 expr.value = error_mark_node;
6391 break;
6393 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6394 "expected %<)%>");
6396 tree e1, e2;
6398 e1 = TYPE_MAIN_VARIANT (groktypename (t1, NULL, NULL));
6399 e2 = TYPE_MAIN_VARIANT (groktypename (t2, NULL, NULL));
6401 expr.value
6402 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6404 break;
6405 case RID_AT_SELECTOR:
6406 gcc_assert (c_dialect_objc ());
6407 c_parser_consume_token (parser);
6408 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6410 expr.value = error_mark_node;
6411 break;
6414 tree sel = c_parser_objc_selector_arg (parser);
6415 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6416 "expected %<)%>");
6417 expr.value = objc_build_selector_expr (loc, sel);
6419 break;
6420 case RID_AT_PROTOCOL:
6421 gcc_assert (c_dialect_objc ());
6422 c_parser_consume_token (parser);
6423 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6425 expr.value = error_mark_node;
6426 break;
6428 if (c_parser_next_token_is_not (parser, CPP_NAME))
6430 c_parser_error (parser, "expected identifier");
6431 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6432 expr.value = error_mark_node;
6433 break;
6436 tree id = c_parser_peek_token (parser)->value;
6437 c_parser_consume_token (parser);
6438 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6439 "expected %<)%>");
6440 expr.value = objc_build_protocol_expr (id);
6442 break;
6443 case RID_AT_ENCODE:
6444 /* Extension to support C-structures in the archiver. */
6445 gcc_assert (c_dialect_objc ());
6446 c_parser_consume_token (parser);
6447 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6449 expr.value = error_mark_node;
6450 break;
6452 t1 = c_parser_type_name (parser);
6453 if (t1 == NULL)
6455 expr.value = error_mark_node;
6456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6457 break;
6459 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6460 "expected %<)%>");
6462 tree type = groktypename (t1, NULL, NULL);
6463 expr.value = objc_build_encode_expr (type);
6465 break;
6466 default:
6467 c_parser_error (parser, "expected expression");
6468 expr.value = error_mark_node;
6469 break;
6471 break;
6472 case CPP_OPEN_SQUARE:
6473 if (c_dialect_objc ())
6475 tree receiver, args;
6476 c_parser_consume_token (parser);
6477 receiver = c_parser_objc_receiver (parser);
6478 args = c_parser_objc_message_args (parser);
6479 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6480 "expected %<]%>");
6481 expr.value = objc_build_message_expr (receiver, args);
6482 break;
6484 /* Else fall through to report error. */
6485 default:
6486 c_parser_error (parser, "expected expression");
6487 expr.value = error_mark_node;
6488 break;
6490 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6493 /* Parse a postfix expression after a parenthesized type name: the
6494 brace-enclosed initializer of a compound literal, possibly followed
6495 by some postfix operators. This is separate because it is not
6496 possible to tell until after the type name whether a cast
6497 expression has a cast or a compound literal, or whether the operand
6498 of sizeof is a parenthesized type name or starts with a compound
6499 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6500 location of the first token after the parentheses around the type
6501 name. */
6503 static struct c_expr
6504 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6505 struct c_type_name *type_name,
6506 location_t type_loc)
6508 tree type;
6509 struct c_expr init;
6510 bool non_const;
6511 struct c_expr expr;
6512 location_t start_loc;
6513 tree type_expr = NULL_TREE;
6514 bool type_expr_const = true;
6515 check_compound_literal_type (type_loc, type_name);
6516 start_init (NULL_TREE, NULL, 0);
6517 type = groktypename (type_name, &type_expr, &type_expr_const);
6518 start_loc = c_parser_peek_token (parser)->location;
6519 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6521 error_at (type_loc, "compound literal has variable size");
6522 type = error_mark_node;
6524 init = c_parser_braced_init (parser, type, false);
6525 finish_init ();
6526 maybe_warn_string_init (type, init);
6528 if (type != error_mark_node
6529 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6530 && current_function_decl)
6532 error ("compound literal qualified by address-space qualifier");
6533 type = error_mark_node;
6536 if (!flag_isoc99)
6537 pedwarn (start_loc, OPT_pedantic, "ISO C90 forbids compound literals");
6538 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6539 ? CONSTRUCTOR_NON_CONST (init.value)
6540 : init.original_code == C_MAYBE_CONST_EXPR);
6541 non_const |= !type_expr_const;
6542 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6543 expr.original_code = ERROR_MARK;
6544 expr.original_type = NULL;
6545 if (type_expr)
6547 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6549 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6550 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6552 else
6554 gcc_assert (!non_const);
6555 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6556 type_expr, expr.value);
6559 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6562 /* Parse a postfix expression after the initial primary or compound
6563 literal; that is, parse a series of postfix operators.
6565 EXPR_LOC is the location of the primary expression. */
6567 static struct c_expr
6568 c_parser_postfix_expression_after_primary (c_parser *parser,
6569 location_t expr_loc,
6570 struct c_expr expr)
6572 struct c_expr orig_expr;
6573 tree ident, idx;
6574 VEC(tree,gc) *exprlist;
6575 VEC(tree,gc) *origtypes;
6576 while (true)
6578 location_t op_loc = c_parser_peek_token (parser)->location;
6579 switch (c_parser_peek_token (parser)->type)
6581 case CPP_OPEN_SQUARE:
6582 /* Array reference. */
6583 c_parser_consume_token (parser);
6584 idx = c_parser_expression (parser).value;
6585 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6586 "expected %<]%>");
6587 expr.value = build_array_ref (op_loc, expr.value, idx);
6588 expr.original_code = ERROR_MARK;
6589 expr.original_type = NULL;
6590 break;
6591 case CPP_OPEN_PAREN:
6592 /* Function call. */
6593 c_parser_consume_token (parser);
6594 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6595 exprlist = NULL;
6596 else
6597 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6598 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6599 "expected %<)%>");
6600 orig_expr = expr;
6601 mark_exp_read (expr.value);
6602 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6603 "(" after the FUNCNAME, which is what we have now. */
6604 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6605 origtypes);
6606 expr.original_code = ERROR_MARK;
6607 if (TREE_CODE (expr.value) == INTEGER_CST
6608 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6609 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6610 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6611 expr.original_code = C_MAYBE_CONST_EXPR;
6612 expr.original_type = NULL;
6613 if (exprlist != NULL)
6615 release_tree_vector (exprlist);
6616 release_tree_vector (origtypes);
6618 break;
6619 case CPP_DOT:
6620 /* Structure element reference. */
6621 c_parser_consume_token (parser);
6622 expr = default_function_array_conversion (expr_loc, expr);
6623 if (c_parser_next_token_is (parser, CPP_NAME))
6624 ident = c_parser_peek_token (parser)->value;
6625 else
6627 c_parser_error (parser, "expected identifier");
6628 expr.value = error_mark_node;
6629 expr.original_code = ERROR_MARK;
6630 expr.original_type = NULL;
6631 return expr;
6633 c_parser_consume_token (parser);
6634 expr.value = build_component_ref (op_loc, expr.value, ident);
6635 expr.original_code = ERROR_MARK;
6636 if (TREE_CODE (expr.value) != COMPONENT_REF)
6637 expr.original_type = NULL;
6638 else
6640 /* Remember the original type of a bitfield. */
6641 tree field = TREE_OPERAND (expr.value, 1);
6642 if (TREE_CODE (field) != FIELD_DECL)
6643 expr.original_type = NULL;
6644 else
6645 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6647 break;
6648 case CPP_DEREF:
6649 /* Structure element reference. */
6650 c_parser_consume_token (parser);
6651 expr = default_function_array_conversion (expr_loc, expr);
6652 if (c_parser_next_token_is (parser, CPP_NAME))
6653 ident = c_parser_peek_token (parser)->value;
6654 else
6656 c_parser_error (parser, "expected identifier");
6657 expr.value = error_mark_node;
6658 expr.original_code = ERROR_MARK;
6659 expr.original_type = NULL;
6660 return expr;
6662 c_parser_consume_token (parser);
6663 expr.value = build_component_ref (op_loc,
6664 build_indirect_ref (op_loc,
6665 expr.value,
6666 RO_ARROW),
6667 ident);
6668 expr.original_code = ERROR_MARK;
6669 if (TREE_CODE (expr.value) != COMPONENT_REF)
6670 expr.original_type = NULL;
6671 else
6673 /* Remember the original type of a bitfield. */
6674 tree field = TREE_OPERAND (expr.value, 1);
6675 if (TREE_CODE (field) != FIELD_DECL)
6676 expr.original_type = NULL;
6677 else
6678 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6680 break;
6681 case CPP_PLUS_PLUS:
6682 /* Postincrement. */
6683 c_parser_consume_token (parser);
6684 expr = default_function_array_read_conversion (expr_loc, expr);
6685 expr.value = build_unary_op (op_loc,
6686 POSTINCREMENT_EXPR, expr.value, 0);
6687 expr.original_code = ERROR_MARK;
6688 expr.original_type = NULL;
6689 break;
6690 case CPP_MINUS_MINUS:
6691 /* Postdecrement. */
6692 c_parser_consume_token (parser);
6693 expr = default_function_array_read_conversion (expr_loc, expr);
6694 expr.value = build_unary_op (op_loc,
6695 POSTDECREMENT_EXPR, expr.value, 0);
6696 expr.original_code = ERROR_MARK;
6697 expr.original_type = NULL;
6698 break;
6699 default:
6700 return expr;
6705 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6707 expression:
6708 assignment-expression
6709 expression , assignment-expression
6712 static struct c_expr
6713 c_parser_expression (c_parser *parser)
6715 struct c_expr expr;
6716 expr = c_parser_expr_no_commas (parser, NULL);
6717 while (c_parser_next_token_is (parser, CPP_COMMA))
6719 struct c_expr next;
6720 tree lhsval;
6721 location_t loc = c_parser_peek_token (parser)->location;
6722 location_t expr_loc;
6723 c_parser_consume_token (parser);
6724 expr_loc = c_parser_peek_token (parser)->location;
6725 lhsval = expr.value;
6726 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
6727 lhsval = TREE_OPERAND (lhsval, 1);
6728 if (DECL_P (lhsval) || handled_component_p (lhsval))
6729 mark_exp_read (lhsval);
6730 next = c_parser_expr_no_commas (parser, NULL);
6731 next = default_function_array_conversion (expr_loc, next);
6732 expr.value = build_compound_expr (loc, expr.value, next.value);
6733 expr.original_code = COMPOUND_EXPR;
6734 expr.original_type = next.original_type;
6736 return expr;
6739 /* Parse an expression and convert functions or arrays to
6740 pointers. */
6742 static struct c_expr
6743 c_parser_expression_conv (c_parser *parser)
6745 struct c_expr expr;
6746 location_t loc = c_parser_peek_token (parser)->location;
6747 expr = c_parser_expression (parser);
6748 expr = default_function_array_conversion (loc, expr);
6749 return expr;
6752 /* Parse a non-empty list of expressions. If CONVERT_P, convert
6753 functions and arrays to pointers. If FOLD_P, fold the expressions.
6755 nonempty-expr-list:
6756 assignment-expression
6757 nonempty-expr-list , assignment-expression
6760 static VEC(tree,gc) *
6761 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
6762 VEC(tree,gc) **p_orig_types)
6764 VEC(tree,gc) *ret;
6765 VEC(tree,gc) *orig_types;
6766 struct c_expr expr;
6767 location_t loc = c_parser_peek_token (parser)->location;
6769 ret = make_tree_vector ();
6770 if (p_orig_types == NULL)
6771 orig_types = NULL;
6772 else
6773 orig_types = make_tree_vector ();
6775 expr = c_parser_expr_no_commas (parser, NULL);
6776 if (convert_p)
6777 expr = default_function_array_read_conversion (loc, expr);
6778 if (fold_p)
6779 expr.value = c_fully_fold (expr.value, false, NULL);
6780 VEC_quick_push (tree, ret, expr.value);
6781 if (orig_types != NULL)
6782 VEC_quick_push (tree, orig_types, expr.original_type);
6783 while (c_parser_next_token_is (parser, CPP_COMMA))
6785 c_parser_consume_token (parser);
6786 loc = c_parser_peek_token (parser)->location;
6787 expr = c_parser_expr_no_commas (parser, NULL);
6788 if (convert_p)
6789 expr = default_function_array_read_conversion (loc, expr);
6790 if (fold_p)
6791 expr.value = c_fully_fold (expr.value, false, NULL);
6792 VEC_safe_push (tree, gc, ret, expr.value);
6793 if (orig_types != NULL)
6794 VEC_safe_push (tree, gc, orig_types, expr.original_type);
6796 if (orig_types != NULL)
6797 *p_orig_types = orig_types;
6798 return ret;
6801 /* Parse Objective-C-specific constructs. */
6803 /* Parse an objc-class-definition.
6805 objc-class-definition:
6806 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
6807 objc-class-instance-variables[opt] objc-methodprotolist @end
6808 @implementation identifier objc-superclass[opt]
6809 objc-class-instance-variables[opt]
6810 @interface identifier ( identifier ) objc-protocol-refs[opt]
6811 objc-methodprotolist @end
6812 @interface identifier ( ) objc-protocol-refs[opt]
6813 objc-methodprotolist @end
6814 @implementation identifier ( identifier )
6816 objc-superclass:
6817 : identifier
6819 "@interface identifier (" must start "@interface identifier (
6820 identifier ) ...": objc-methodprotolist in the first production may
6821 not start with a parenthesized identifier as a declarator of a data
6822 definition with no declaration specifiers if the objc-superclass,
6823 objc-protocol-refs and objc-class-instance-variables are omitted. */
6825 static void
6826 c_parser_objc_class_definition (c_parser *parser, tree attributes)
6828 bool iface_p;
6829 tree id1;
6830 tree superclass;
6831 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
6832 iface_p = true;
6833 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
6834 iface_p = false;
6835 else
6836 gcc_unreachable ();
6838 c_parser_consume_token (parser);
6839 if (c_parser_next_token_is_not (parser, CPP_NAME))
6841 c_parser_error (parser, "expected identifier");
6842 return;
6844 id1 = c_parser_peek_token (parser)->value;
6845 c_parser_consume_token (parser);
6846 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
6848 /* We have a category or class extension. */
6849 tree id2;
6850 tree proto = NULL_TREE;
6851 c_parser_consume_token (parser);
6852 if (c_parser_next_token_is_not (parser, CPP_NAME))
6854 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6856 /* We have a class extension. */
6857 id2 = NULL_TREE;
6859 else
6861 c_parser_error (parser, "expected identifier or %<)%>");
6862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6863 return;
6866 else
6868 id2 = c_parser_peek_token (parser)->value;
6869 c_parser_consume_token (parser);
6871 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6872 if (!iface_p)
6874 objc_start_category_implementation (id1, id2);
6875 return;
6877 if (c_parser_next_token_is (parser, CPP_LESS))
6878 proto = c_parser_objc_protocol_refs (parser);
6879 objc_start_category_interface (id1, id2, proto, attributes);
6880 c_parser_objc_methodprotolist (parser);
6881 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6882 objc_finish_interface ();
6883 return;
6885 if (c_parser_next_token_is (parser, CPP_COLON))
6887 c_parser_consume_token (parser);
6888 if (c_parser_next_token_is_not (parser, CPP_NAME))
6890 c_parser_error (parser, "expected identifier");
6891 return;
6893 superclass = c_parser_peek_token (parser)->value;
6894 c_parser_consume_token (parser);
6896 else
6897 superclass = NULL_TREE;
6898 if (iface_p)
6900 tree proto = NULL_TREE;
6901 if (c_parser_next_token_is (parser, CPP_LESS))
6902 proto = c_parser_objc_protocol_refs (parser);
6903 objc_start_class_interface (id1, superclass, proto, attributes);
6905 else
6906 objc_start_class_implementation (id1, superclass);
6907 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6908 c_parser_objc_class_instance_variables (parser);
6909 if (iface_p)
6911 objc_continue_interface ();
6912 c_parser_objc_methodprotolist (parser);
6913 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
6914 objc_finish_interface ();
6916 else
6918 objc_continue_implementation ();
6919 return;
6923 /* Parse objc-class-instance-variables.
6925 objc-class-instance-variables:
6926 { objc-instance-variable-decl-list[opt] }
6928 objc-instance-variable-decl-list:
6929 objc-visibility-spec
6930 objc-instance-variable-decl ;
6932 objc-instance-variable-decl-list objc-visibility-spec
6933 objc-instance-variable-decl-list objc-instance-variable-decl ;
6934 objc-instance-variable-decl-list ;
6936 objc-visibility-spec:
6937 @private
6938 @protected
6939 @public
6941 objc-instance-variable-decl:
6942 struct-declaration
6945 static void
6946 c_parser_objc_class_instance_variables (c_parser *parser)
6948 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
6949 c_parser_consume_token (parser);
6950 while (c_parser_next_token_is_not (parser, CPP_EOF))
6952 tree decls;
6953 /* Parse any stray semicolon. */
6954 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6956 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
6957 "extra semicolon");
6958 c_parser_consume_token (parser);
6959 continue;
6961 /* Stop if at the end of the instance variables. */
6962 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
6964 c_parser_consume_token (parser);
6965 break;
6967 /* Parse any objc-visibility-spec. */
6968 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
6970 c_parser_consume_token (parser);
6971 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
6972 continue;
6974 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
6976 c_parser_consume_token (parser);
6977 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
6978 continue;
6980 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
6982 c_parser_consume_token (parser);
6983 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
6984 continue;
6986 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
6988 c_parser_consume_token (parser);
6989 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
6990 continue;
6992 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
6994 c_parser_pragma (parser, pragma_external);
6995 continue;
6998 /* Parse some comma-separated declarations. */
6999 decls = c_parser_struct_declaration (parser);
7000 if (decls == NULL)
7002 /* There is a syntax error. We want to skip the offending
7003 tokens up to the next ';' (included) or '}'
7004 (excluded). */
7006 /* First, skip manually a ')' or ']'. This is because they
7007 reduce the nesting level, so c_parser_skip_until_found()
7008 wouldn't be able to skip past them. */
7009 c_token *token = c_parser_peek_token (parser);
7010 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7011 c_parser_consume_token (parser);
7013 /* Then, do the standard skipping. */
7014 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7016 /* We hopefully recovered. Start normal parsing again. */
7017 parser->error = false;
7018 continue;
7020 else
7022 /* Comma-separated instance variables are chained together
7023 in reverse order; add them one by one. */
7024 tree ivar = nreverse (decls);
7025 for (; ivar; ivar = DECL_CHAIN (ivar))
7026 objc_add_instance_variable (copy_node (ivar));
7028 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7032 /* Parse an objc-class-declaration.
7034 objc-class-declaration:
7035 @class identifier-list ;
7038 static void
7039 c_parser_objc_class_declaration (c_parser *parser)
7041 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7042 c_parser_consume_token (parser);
7043 /* Any identifiers, including those declared as type names, are OK
7044 here. */
7045 while (true)
7047 tree id;
7048 if (c_parser_next_token_is_not (parser, CPP_NAME))
7050 c_parser_error (parser, "expected identifier");
7051 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7052 parser->error = false;
7053 return;
7055 id = c_parser_peek_token (parser)->value;
7056 objc_declare_class (id);
7057 c_parser_consume_token (parser);
7058 if (c_parser_next_token_is (parser, CPP_COMMA))
7059 c_parser_consume_token (parser);
7060 else
7061 break;
7063 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7066 /* Parse an objc-alias-declaration.
7068 objc-alias-declaration:
7069 @compatibility_alias identifier identifier ;
7072 static void
7073 c_parser_objc_alias_declaration (c_parser *parser)
7075 tree id1, id2;
7076 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7077 c_parser_consume_token (parser);
7078 if (c_parser_next_token_is_not (parser, CPP_NAME))
7080 c_parser_error (parser, "expected identifier");
7081 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7082 return;
7084 id1 = c_parser_peek_token (parser)->value;
7085 c_parser_consume_token (parser);
7086 if (c_parser_next_token_is_not (parser, CPP_NAME))
7088 c_parser_error (parser, "expected identifier");
7089 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7090 return;
7092 id2 = c_parser_peek_token (parser)->value;
7093 c_parser_consume_token (parser);
7094 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7095 objc_declare_alias (id1, id2);
7098 /* Parse an objc-protocol-definition.
7100 objc-protocol-definition:
7101 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7102 @protocol identifier-list ;
7104 "@protocol identifier ;" should be resolved as "@protocol
7105 identifier-list ;": objc-methodprotolist may not start with a
7106 semicolon in the first alternative if objc-protocol-refs are
7107 omitted. */
7109 static void
7110 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7112 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7114 c_parser_consume_token (parser);
7115 if (c_parser_next_token_is_not (parser, CPP_NAME))
7117 c_parser_error (parser, "expected identifier");
7118 return;
7120 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7121 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7123 /* Any identifiers, including those declared as type names, are
7124 OK here. */
7125 while (true)
7127 tree id;
7128 if (c_parser_next_token_is_not (parser, CPP_NAME))
7130 c_parser_error (parser, "expected identifier");
7131 break;
7133 id = c_parser_peek_token (parser)->value;
7134 objc_declare_protocol (id, attributes);
7135 c_parser_consume_token (parser);
7136 if (c_parser_next_token_is (parser, CPP_COMMA))
7137 c_parser_consume_token (parser);
7138 else
7139 break;
7141 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7143 else
7145 tree id = c_parser_peek_token (parser)->value;
7146 tree proto = NULL_TREE;
7147 c_parser_consume_token (parser);
7148 if (c_parser_next_token_is (parser, CPP_LESS))
7149 proto = c_parser_objc_protocol_refs (parser);
7150 parser->objc_pq_context = true;
7151 objc_start_protocol (id, proto, attributes);
7152 c_parser_objc_methodprotolist (parser);
7153 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7154 parser->objc_pq_context = false;
7155 objc_finish_interface ();
7159 /* Parse an objc-method-type.
7161 objc-method-type:
7165 Return true if it is a class method (+) and false if it is
7166 an instance method (-).
7168 static inline bool
7169 c_parser_objc_method_type (c_parser *parser)
7171 switch (c_parser_peek_token (parser)->type)
7173 case CPP_PLUS:
7174 c_parser_consume_token (parser);
7175 return true;
7176 case CPP_MINUS:
7177 c_parser_consume_token (parser);
7178 return false;
7179 default:
7180 gcc_unreachable ();
7184 /* Parse an objc-method-definition.
7186 objc-method-definition:
7187 objc-method-type objc-method-decl ;[opt] compound-statement
7190 static void
7191 c_parser_objc_method_definition (c_parser *parser)
7193 bool is_class_method = c_parser_objc_method_type (parser);
7194 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7195 parser->objc_pq_context = true;
7196 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7197 &expr);
7198 if (decl == error_mark_node)
7199 return; /* Bail here. */
7201 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7203 c_parser_consume_token (parser);
7204 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7205 "extra semicolon in method definition specified");
7208 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7210 c_parser_error (parser, "expected %<{%>");
7211 return;
7214 parser->objc_pq_context = false;
7215 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7217 add_stmt (c_parser_compound_statement (parser));
7218 objc_finish_method_definition (current_function_decl);
7220 else
7222 /* This code is executed when we find a method definition
7223 outside of an @implementation context (or invalid for other
7224 reasons). Parse the method (to keep going) but do not emit
7225 any code.
7227 c_parser_compound_statement (parser);
7231 /* Parse an objc-methodprotolist.
7233 objc-methodprotolist:
7234 empty
7235 objc-methodprotolist objc-methodproto
7236 objc-methodprotolist declaration
7237 objc-methodprotolist ;
7238 @optional
7239 @required
7241 The declaration is a data definition, which may be missing
7242 declaration specifiers under the same rules and diagnostics as
7243 other data definitions outside functions, and the stray semicolon
7244 is diagnosed the same way as a stray semicolon outside a
7245 function. */
7247 static void
7248 c_parser_objc_methodprotolist (c_parser *parser)
7250 while (true)
7252 /* The list is terminated by @end. */
7253 switch (c_parser_peek_token (parser)->type)
7255 case CPP_SEMICOLON:
7256 pedwarn (c_parser_peek_token (parser)->location, OPT_pedantic,
7257 "ISO C does not allow extra %<;%> outside of a function");
7258 c_parser_consume_token (parser);
7259 break;
7260 case CPP_PLUS:
7261 case CPP_MINUS:
7262 c_parser_objc_methodproto (parser);
7263 break;
7264 case CPP_PRAGMA:
7265 c_parser_pragma (parser, pragma_external);
7266 break;
7267 case CPP_EOF:
7268 return;
7269 default:
7270 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7271 return;
7272 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7273 c_parser_objc_at_property_declaration (parser);
7274 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7276 objc_set_method_opt (true);
7277 c_parser_consume_token (parser);
7279 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7281 objc_set_method_opt (false);
7282 c_parser_consume_token (parser);
7284 else
7285 c_parser_declaration_or_fndef (parser, false, false, true,
7286 false, true, NULL);
7287 break;
7292 /* Parse an objc-methodproto.
7294 objc-methodproto:
7295 objc-method-type objc-method-decl ;
7298 static void
7299 c_parser_objc_methodproto (c_parser *parser)
7301 bool is_class_method = c_parser_objc_method_type (parser);
7302 tree decl, attributes = NULL_TREE;
7304 /* Remember protocol qualifiers in prototypes. */
7305 parser->objc_pq_context = true;
7306 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7307 NULL);
7308 /* Forget protocol qualifiers now. */
7309 parser->objc_pq_context = false;
7311 /* Do not allow the presence of attributes to hide an erroneous
7312 method implementation in the interface section. */
7313 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7315 c_parser_error (parser, "expected %<;%>");
7316 return;
7319 if (decl != error_mark_node)
7320 objc_add_method_declaration (is_class_method, decl, attributes);
7322 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7325 /* If we are at a position that method attributes may be present, check that
7326 there are not any parsed already (a syntax error) and then collect any
7327 specified at the current location. Finally, if new attributes were present,
7328 check that the next token is legal ( ';' for decls and '{' for defs). */
7330 static bool
7331 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7333 bool bad = false;
7334 if (*attributes)
7336 c_parser_error (parser,
7337 "method attributes must be specified at the end only");
7338 *attributes = NULL_TREE;
7339 bad = true;
7342 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7343 *attributes = c_parser_attributes (parser);
7345 /* If there were no attributes here, just report any earlier error. */
7346 if (*attributes == NULL_TREE || bad)
7347 return bad;
7349 /* If the attributes are followed by a ; or {, then just report any earlier
7350 error. */
7351 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7352 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7353 return bad;
7355 /* We've got attributes, but not at the end. */
7356 c_parser_error (parser,
7357 "expected %<;%> or %<{%> after method attribute definition");
7358 return true;
7361 /* Parse an objc-method-decl.
7363 objc-method-decl:
7364 ( objc-type-name ) objc-selector
7365 objc-selector
7366 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7367 objc-keyword-selector objc-optparmlist
7368 attributes
7370 objc-keyword-selector:
7371 objc-keyword-decl
7372 objc-keyword-selector objc-keyword-decl
7374 objc-keyword-decl:
7375 objc-selector : ( objc-type-name ) identifier
7376 objc-selector : identifier
7377 : ( objc-type-name ) identifier
7378 : identifier
7380 objc-optparmlist:
7381 objc-optparms objc-optellipsis
7383 objc-optparms:
7384 empty
7385 objc-opt-parms , parameter-declaration
7387 objc-optellipsis:
7388 empty
7389 , ...
7392 static tree
7393 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7394 tree *attributes, tree *expr)
7396 tree type = NULL_TREE;
7397 tree sel;
7398 tree parms = NULL_TREE;
7399 bool ellipsis = false;
7400 bool attr_err = false;
7402 *attributes = NULL_TREE;
7403 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7405 c_parser_consume_token (parser);
7406 type = c_parser_objc_type_name (parser);
7407 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7409 sel = c_parser_objc_selector (parser);
7410 /* If there is no selector, or a colon follows, we have an
7411 objc-keyword-selector. If there is a selector, and a colon does
7412 not follow, that selector ends the objc-method-decl. */
7413 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7415 tree tsel = sel;
7416 tree list = NULL_TREE;
7417 while (true)
7419 tree atype = NULL_TREE, id, keyworddecl;
7420 tree param_attr = NULL_TREE;
7421 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7422 break;
7423 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7425 c_parser_consume_token (parser);
7426 atype = c_parser_objc_type_name (parser);
7427 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7428 "expected %<)%>");
7430 /* New ObjC allows attributes on method parameters. */
7431 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7432 param_attr = c_parser_attributes (parser);
7433 if (c_parser_next_token_is_not (parser, CPP_NAME))
7435 c_parser_error (parser, "expected identifier");
7436 return error_mark_node;
7438 id = c_parser_peek_token (parser)->value;
7439 c_parser_consume_token (parser);
7440 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7441 list = chainon (list, keyworddecl);
7442 tsel = c_parser_objc_selector (parser);
7443 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7444 break;
7447 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7449 /* Parse the optional parameter list. Optional Objective-C
7450 method parameters follow the C syntax, and may include '...'
7451 to denote a variable number of arguments. */
7452 parms = make_node (TREE_LIST);
7453 while (c_parser_next_token_is (parser, CPP_COMMA))
7455 struct c_parm *parm;
7456 c_parser_consume_token (parser);
7457 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7459 ellipsis = true;
7460 c_parser_consume_token (parser);
7461 attr_err |= c_parser_objc_maybe_method_attributes
7462 (parser, attributes) ;
7463 break;
7465 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7466 if (parm == NULL)
7467 break;
7468 parms = chainon (parms,
7469 build_tree_list (NULL_TREE, grokparm (parm, expr)));
7471 sel = list;
7473 else
7474 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7476 if (sel == NULL)
7478 c_parser_error (parser, "objective-c method declaration is expected");
7479 return error_mark_node;
7482 if (attr_err)
7483 return error_mark_node;
7485 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7488 /* Parse an objc-type-name.
7490 objc-type-name:
7491 objc-type-qualifiers[opt] type-name
7492 objc-type-qualifiers[opt]
7494 objc-type-qualifiers:
7495 objc-type-qualifier
7496 objc-type-qualifiers objc-type-qualifier
7498 objc-type-qualifier: one of
7499 in out inout bycopy byref oneway
7502 static tree
7503 c_parser_objc_type_name (c_parser *parser)
7505 tree quals = NULL_TREE;
7506 struct c_type_name *type_name = NULL;
7507 tree type = NULL_TREE;
7508 while (true)
7510 c_token *token = c_parser_peek_token (parser);
7511 if (token->type == CPP_KEYWORD
7512 && (token->keyword == RID_IN
7513 || token->keyword == RID_OUT
7514 || token->keyword == RID_INOUT
7515 || token->keyword == RID_BYCOPY
7516 || token->keyword == RID_BYREF
7517 || token->keyword == RID_ONEWAY))
7519 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7520 c_parser_consume_token (parser);
7522 else
7523 break;
7525 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7526 type_name = c_parser_type_name (parser);
7527 if (type_name)
7528 type = groktypename (type_name, NULL, NULL);
7530 /* If the type is unknown, and error has already been produced and
7531 we need to recover from the error. In that case, use NULL_TREE
7532 for the type, as if no type had been specified; this will use the
7533 default type ('id') which is good for error recovery. */
7534 if (type == error_mark_node)
7535 type = NULL_TREE;
7537 return build_tree_list (quals, type);
7540 /* Parse objc-protocol-refs.
7542 objc-protocol-refs:
7543 < identifier-list >
7546 static tree
7547 c_parser_objc_protocol_refs (c_parser *parser)
7549 tree list = NULL_TREE;
7550 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7551 c_parser_consume_token (parser);
7552 /* Any identifiers, including those declared as type names, are OK
7553 here. */
7554 while (true)
7556 tree id;
7557 if (c_parser_next_token_is_not (parser, CPP_NAME))
7559 c_parser_error (parser, "expected identifier");
7560 break;
7562 id = c_parser_peek_token (parser)->value;
7563 list = chainon (list, build_tree_list (NULL_TREE, id));
7564 c_parser_consume_token (parser);
7565 if (c_parser_next_token_is (parser, CPP_COMMA))
7566 c_parser_consume_token (parser);
7567 else
7568 break;
7570 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7571 return list;
7574 /* Parse an objc-try-catch-finally-statement.
7576 objc-try-catch-finally-statement:
7577 @try compound-statement objc-catch-list[opt]
7578 @try compound-statement objc-catch-list[opt] @finally compound-statement
7580 objc-catch-list:
7581 @catch ( objc-catch-parameter-declaration ) compound-statement
7582 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7584 objc-catch-parameter-declaration:
7585 parameter-declaration
7586 '...'
7588 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7590 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7591 for C++. Keep them in sync. */
7593 static void
7594 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7596 location_t location;
7597 tree stmt;
7599 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7600 c_parser_consume_token (parser);
7601 location = c_parser_peek_token (parser)->location;
7602 objc_maybe_warn_exceptions (location);
7603 stmt = c_parser_compound_statement (parser);
7604 objc_begin_try_stmt (location, stmt);
7606 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7608 struct c_parm *parm;
7609 tree parameter_declaration = error_mark_node;
7610 bool seen_open_paren = false;
7612 c_parser_consume_token (parser);
7613 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7614 seen_open_paren = true;
7615 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7617 /* We have "@catch (...)" (where the '...' are literally
7618 what is in the code). Skip the '...'.
7619 parameter_declaration is set to NULL_TREE, and
7620 objc_being_catch_clauses() knows that that means
7621 '...'. */
7622 c_parser_consume_token (parser);
7623 parameter_declaration = NULL_TREE;
7625 else
7627 /* We have "@catch (NSException *exception)" or something
7628 like that. Parse the parameter declaration. */
7629 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7630 if (parm == NULL)
7631 parameter_declaration = error_mark_node;
7632 else
7633 parameter_declaration = grokparm (parm, NULL);
7635 if (seen_open_paren)
7636 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7637 else
7639 /* If there was no open parenthesis, we are recovering from
7640 an error, and we are trying to figure out what mistake
7641 the user has made. */
7643 /* If there is an immediate closing parenthesis, the user
7644 probably forgot the opening one (ie, they typed "@catch
7645 NSException *e)". Parse the closing parenthesis and keep
7646 going. */
7647 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7648 c_parser_consume_token (parser);
7650 /* If these is no immediate closing parenthesis, the user
7651 probably doesn't know that parenthesis are required at
7652 all (ie, they typed "@catch NSException *e"). So, just
7653 forget about the closing parenthesis and keep going. */
7655 objc_begin_catch_clause (parameter_declaration);
7656 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7657 c_parser_compound_statement_nostart (parser);
7658 objc_finish_catch_clause ();
7660 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7662 c_parser_consume_token (parser);
7663 location = c_parser_peek_token (parser)->location;
7664 stmt = c_parser_compound_statement (parser);
7665 objc_build_finally_clause (location, stmt);
7667 objc_finish_try_stmt ();
7670 /* Parse an objc-synchronized-statement.
7672 objc-synchronized-statement:
7673 @synchronized ( expression ) compound-statement
7676 static void
7677 c_parser_objc_synchronized_statement (c_parser *parser)
7679 location_t loc;
7680 tree expr, stmt;
7681 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7682 c_parser_consume_token (parser);
7683 loc = c_parser_peek_token (parser)->location;
7684 objc_maybe_warn_exceptions (loc);
7685 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7687 expr = c_parser_expression (parser).value;
7688 expr = c_fully_fold (expr, false, NULL);
7689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7691 else
7692 expr = error_mark_node;
7693 stmt = c_parser_compound_statement (parser);
7694 objc_build_synchronized (loc, expr, stmt);
7697 /* Parse an objc-selector; return NULL_TREE without an error if the
7698 next token is not an objc-selector.
7700 objc-selector:
7701 identifier
7702 one of
7703 enum struct union if else while do for switch case default
7704 break continue return goto asm sizeof typeof __alignof
7705 unsigned long const short volatile signed restrict _Complex
7706 in out inout bycopy byref oneway int char float double void _Bool
7708 ??? Why this selection of keywords but not, for example, storage
7709 class specifiers? */
7711 static tree
7712 c_parser_objc_selector (c_parser *parser)
7714 c_token *token = c_parser_peek_token (parser);
7715 tree value = token->value;
7716 if (token->type == CPP_NAME)
7718 c_parser_consume_token (parser);
7719 return value;
7721 if (token->type != CPP_KEYWORD)
7722 return NULL_TREE;
7723 switch (token->keyword)
7725 case RID_ENUM:
7726 case RID_STRUCT:
7727 case RID_UNION:
7728 case RID_IF:
7729 case RID_ELSE:
7730 case RID_WHILE:
7731 case RID_DO:
7732 case RID_FOR:
7733 case RID_SWITCH:
7734 case RID_CASE:
7735 case RID_DEFAULT:
7736 case RID_BREAK:
7737 case RID_CONTINUE:
7738 case RID_RETURN:
7739 case RID_GOTO:
7740 case RID_ASM:
7741 case RID_SIZEOF:
7742 case RID_TYPEOF:
7743 case RID_ALIGNOF:
7744 case RID_UNSIGNED:
7745 case RID_LONG:
7746 case RID_INT128:
7747 case RID_CONST:
7748 case RID_SHORT:
7749 case RID_VOLATILE:
7750 case RID_SIGNED:
7751 case RID_RESTRICT:
7752 case RID_COMPLEX:
7753 case RID_IN:
7754 case RID_OUT:
7755 case RID_INOUT:
7756 case RID_BYCOPY:
7757 case RID_BYREF:
7758 case RID_ONEWAY:
7759 case RID_INT:
7760 case RID_CHAR:
7761 case RID_FLOAT:
7762 case RID_DOUBLE:
7763 case RID_VOID:
7764 case RID_BOOL:
7765 c_parser_consume_token (parser);
7766 return value;
7767 default:
7768 return NULL_TREE;
7772 /* Parse an objc-selector-arg.
7774 objc-selector-arg:
7775 objc-selector
7776 objc-keywordname-list
7778 objc-keywordname-list:
7779 objc-keywordname
7780 objc-keywordname-list objc-keywordname
7782 objc-keywordname:
7783 objc-selector :
7787 static tree
7788 c_parser_objc_selector_arg (c_parser *parser)
7790 tree sel = c_parser_objc_selector (parser);
7791 tree list = NULL_TREE;
7792 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7793 return sel;
7794 while (true)
7796 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7797 return list;
7798 list = chainon (list, build_tree_list (sel, NULL_TREE));
7799 sel = c_parser_objc_selector (parser);
7800 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7801 break;
7803 return list;
7806 /* Parse an objc-receiver.
7808 objc-receiver:
7809 expression
7810 class-name
7811 type-name
7814 static tree
7815 c_parser_objc_receiver (c_parser *parser)
7817 if (c_parser_peek_token (parser)->type == CPP_NAME
7818 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
7819 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
7821 tree id = c_parser_peek_token (parser)->value;
7822 c_parser_consume_token (parser);
7823 return objc_get_class_reference (id);
7825 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
7828 /* Parse objc-message-args.
7830 objc-message-args:
7831 objc-selector
7832 objc-keywordarg-list
7834 objc-keywordarg-list:
7835 objc-keywordarg
7836 objc-keywordarg-list objc-keywordarg
7838 objc-keywordarg:
7839 objc-selector : objc-keywordexpr
7840 : objc-keywordexpr
7843 static tree
7844 c_parser_objc_message_args (c_parser *parser)
7846 tree sel = c_parser_objc_selector (parser);
7847 tree list = NULL_TREE;
7848 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
7849 return sel;
7850 while (true)
7852 tree keywordexpr;
7853 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7854 return error_mark_node;
7855 keywordexpr = c_parser_objc_keywordexpr (parser);
7856 list = chainon (list, build_tree_list (sel, keywordexpr));
7857 sel = c_parser_objc_selector (parser);
7858 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
7859 break;
7861 return list;
7864 /* Parse an objc-keywordexpr.
7866 objc-keywordexpr:
7867 nonempty-expr-list
7870 static tree
7871 c_parser_objc_keywordexpr (c_parser *parser)
7873 tree ret;
7874 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
7875 if (VEC_length (tree, expr_list) == 1)
7877 /* Just return the expression, remove a level of
7878 indirection. */
7879 ret = VEC_index (tree, expr_list, 0);
7881 else
7883 /* We have a comma expression, we will collapse later. */
7884 ret = build_tree_list_vec (expr_list);
7886 release_tree_vector (expr_list);
7887 return ret;
7890 /* A check, needed in several places, that ObjC interface, implementation or
7891 method definitions are not prefixed by incorrect items. */
7892 static bool
7893 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
7894 struct c_declspecs *specs)
7896 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
7897 || specs->typespec_kind != ctsk_none)
7899 c_parser_error (parser,
7900 "no type or storage class may be specified here,");
7901 c_parser_skip_to_end_of_block_or_statement (parser);
7902 return true;
7904 return false;
7907 /* Parse an Objective-C @property declaration. The syntax is:
7909 objc-property-declaration:
7910 '@property' objc-property-attributes[opt] struct-declaration ;
7912 objc-property-attributes:
7913 '(' objc-property-attribute-list ')'
7915 objc-property-attribute-list:
7916 objc-property-attribute
7917 objc-property-attribute-list, objc-property-attribute
7919 objc-property-attribute
7920 'getter' = identifier
7921 'setter' = identifier
7922 'readonly'
7923 'readwrite'
7924 'assign'
7925 'retain'
7926 'copy'
7927 'nonatomic'
7929 For example:
7930 @property NSString *name;
7931 @property (readonly) id object;
7932 @property (retain, nonatomic, getter=getTheName) id name;
7933 @property int a, b, c;
7935 PS: This function is identical to cp_parser_objc_at_propery_declaration
7936 for C++. Keep them in sync. */
7937 static void
7938 c_parser_objc_at_property_declaration (c_parser *parser)
7940 /* The following variables hold the attributes of the properties as
7941 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
7942 seen. When we see an attribute, we set them to 'true' (if they
7943 are boolean properties) or to the identifier (if they have an
7944 argument, ie, for getter and setter). Note that here we only
7945 parse the list of attributes, check the syntax and accumulate the
7946 attributes that we find. objc_add_property_declaration() will
7947 then process the information. */
7948 bool property_assign = false;
7949 bool property_copy = false;
7950 tree property_getter_ident = NULL_TREE;
7951 bool property_nonatomic = false;
7952 bool property_readonly = false;
7953 bool property_readwrite = false;
7954 bool property_retain = false;
7955 tree property_setter_ident = NULL_TREE;
7957 /* 'properties' is the list of properties that we read. Usually a
7958 single one, but maybe more (eg, in "@property int a, b, c;" there
7959 are three). */
7960 tree properties;
7961 location_t loc;
7963 loc = c_parser_peek_token (parser)->location;
7964 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
7966 c_parser_consume_token (parser); /* Eat '@property'. */
7968 /* Parse the optional attribute list... */
7969 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7971 /* Eat the '(' */
7972 c_parser_consume_token (parser);
7974 /* Property attribute keywords are valid now. */
7975 parser->objc_property_attr_context = true;
7977 while (true)
7979 bool syntax_error = false;
7980 c_token *token = c_parser_peek_token (parser);
7981 enum rid keyword;
7983 if (token->type != CPP_KEYWORD)
7985 if (token->type == CPP_CLOSE_PAREN)
7986 c_parser_error (parser, "expected identifier");
7987 else
7989 c_parser_consume_token (parser);
7990 c_parser_error (parser, "unknown property attribute");
7992 break;
7994 keyword = token->keyword;
7995 c_parser_consume_token (parser);
7996 switch (keyword)
7998 case RID_ASSIGN: property_assign = true; break;
7999 case RID_COPY: property_copy = true; break;
8000 case RID_NONATOMIC: property_nonatomic = true; break;
8001 case RID_READONLY: property_readonly = true; break;
8002 case RID_READWRITE: property_readwrite = true; break;
8003 case RID_RETAIN: property_retain = true; break;
8005 case RID_GETTER:
8006 case RID_SETTER:
8007 if (c_parser_next_token_is_not (parser, CPP_EQ))
8009 if (keyword == RID_GETTER)
8010 c_parser_error (parser,
8011 "missing %<=%> (after %<getter%> attribute)");
8012 else
8013 c_parser_error (parser,
8014 "missing %<=%> (after %<setter%> attribute)");
8015 syntax_error = true;
8016 break;
8018 c_parser_consume_token (parser); /* eat the = */
8019 if (c_parser_next_token_is_not (parser, CPP_NAME))
8021 c_parser_error (parser, "expected identifier");
8022 syntax_error = true;
8023 break;
8025 if (keyword == RID_SETTER)
8027 if (property_setter_ident != NULL_TREE)
8028 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8029 else
8030 property_setter_ident = c_parser_peek_token (parser)->value;
8031 c_parser_consume_token (parser);
8032 if (c_parser_next_token_is_not (parser, CPP_COLON))
8033 c_parser_error (parser, "setter name must terminate with %<:%>");
8034 else
8035 c_parser_consume_token (parser);
8037 else
8039 if (property_getter_ident != NULL_TREE)
8040 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8041 else
8042 property_getter_ident = c_parser_peek_token (parser)->value;
8043 c_parser_consume_token (parser);
8045 break;
8046 default:
8047 c_parser_error (parser, "unknown property attribute");
8048 syntax_error = true;
8049 break;
8052 if (syntax_error)
8053 break;
8055 if (c_parser_next_token_is (parser, CPP_COMMA))
8056 c_parser_consume_token (parser);
8057 else
8058 break;
8060 parser->objc_property_attr_context = false;
8061 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8063 /* ... and the property declaration(s). */
8064 properties = c_parser_struct_declaration (parser);
8066 if (properties == error_mark_node)
8068 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8069 parser->error = false;
8070 return;
8073 if (properties == NULL_TREE)
8074 c_parser_error (parser, "expected identifier");
8075 else
8077 /* Comma-separated properties are chained together in
8078 reverse order; add them one by one. */
8079 properties = nreverse (properties);
8081 for (; properties; properties = TREE_CHAIN (properties))
8082 objc_add_property_declaration (loc, copy_node (properties),
8083 property_readonly, property_readwrite,
8084 property_assign, property_retain,
8085 property_copy, property_nonatomic,
8086 property_getter_ident, property_setter_ident);
8089 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8090 parser->error = false;
8093 /* Parse an Objective-C @synthesize declaration. The syntax is:
8095 objc-synthesize-declaration:
8096 @synthesize objc-synthesize-identifier-list ;
8098 objc-synthesize-identifier-list:
8099 objc-synthesize-identifier
8100 objc-synthesize-identifier-list, objc-synthesize-identifier
8102 objc-synthesize-identifier
8103 identifier
8104 identifier = identifier
8106 For example:
8107 @synthesize MyProperty;
8108 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8110 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8111 for C++. Keep them in sync.
8113 static void
8114 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8116 tree list = NULL_TREE;
8117 location_t loc;
8118 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8119 loc = c_parser_peek_token (parser)->location;
8121 c_parser_consume_token (parser);
8122 while (true)
8124 tree property, ivar;
8125 if (c_parser_next_token_is_not (parser, CPP_NAME))
8127 c_parser_error (parser, "expected identifier");
8128 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8129 /* Once we find the semicolon, we can resume normal parsing.
8130 We have to reset parser->error manually because
8131 c_parser_skip_until_found() won't reset it for us if the
8132 next token is precisely a semicolon. */
8133 parser->error = false;
8134 return;
8136 property = c_parser_peek_token (parser)->value;
8137 c_parser_consume_token (parser);
8138 if (c_parser_next_token_is (parser, CPP_EQ))
8140 c_parser_consume_token (parser);
8141 if (c_parser_next_token_is_not (parser, CPP_NAME))
8143 c_parser_error (parser, "expected identifier");
8144 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8145 parser->error = false;
8146 return;
8148 ivar = c_parser_peek_token (parser)->value;
8149 c_parser_consume_token (parser);
8151 else
8152 ivar = NULL_TREE;
8153 list = chainon (list, build_tree_list (ivar, property));
8154 if (c_parser_next_token_is (parser, CPP_COMMA))
8155 c_parser_consume_token (parser);
8156 else
8157 break;
8159 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8160 objc_add_synthesize_declaration (loc, list);
8163 /* Parse an Objective-C @dynamic declaration. The syntax is:
8165 objc-dynamic-declaration:
8166 @dynamic identifier-list ;
8168 For example:
8169 @dynamic MyProperty;
8170 @dynamic MyProperty, AnotherProperty;
8172 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8173 for C++. Keep them in sync.
8175 static void
8176 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8178 tree list = NULL_TREE;
8179 location_t loc;
8180 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8181 loc = c_parser_peek_token (parser)->location;
8183 c_parser_consume_token (parser);
8184 while (true)
8186 tree property;
8187 if (c_parser_next_token_is_not (parser, CPP_NAME))
8189 c_parser_error (parser, "expected identifier");
8190 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8191 parser->error = false;
8192 return;
8194 property = c_parser_peek_token (parser)->value;
8195 list = chainon (list, build_tree_list (NULL_TREE, property));
8196 c_parser_consume_token (parser);
8197 if (c_parser_next_token_is (parser, CPP_COMMA))
8198 c_parser_consume_token (parser);
8199 else
8200 break;
8202 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8203 objc_add_dynamic_declaration (loc, list);
8207 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8208 should be considered, statements. ALLOW_STMT is true if we're within
8209 the context of a function and such pragmas are to be allowed. Returns
8210 true if we actually parsed such a pragma. */
8212 static bool
8213 c_parser_pragma (c_parser *parser, enum pragma_context context)
8215 unsigned int id;
8217 id = c_parser_peek_token (parser)->pragma_kind;
8218 gcc_assert (id != PRAGMA_NONE);
8220 switch (id)
8222 case PRAGMA_OMP_BARRIER:
8223 if (context != pragma_compound)
8225 if (context == pragma_stmt)
8226 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8227 "used in compound statements");
8228 goto bad_stmt;
8230 c_parser_omp_barrier (parser);
8231 return false;
8233 case PRAGMA_OMP_FLUSH:
8234 if (context != pragma_compound)
8236 if (context == pragma_stmt)
8237 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8238 "used in compound statements");
8239 goto bad_stmt;
8241 c_parser_omp_flush (parser);
8242 return false;
8244 case PRAGMA_OMP_TASKWAIT:
8245 if (context != pragma_compound)
8247 if (context == pragma_stmt)
8248 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8249 "used in compound statements");
8250 goto bad_stmt;
8252 c_parser_omp_taskwait (parser);
8253 return false;
8255 case PRAGMA_OMP_TASKYIELD:
8256 if (context != pragma_compound)
8258 if (context == pragma_stmt)
8259 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
8260 "used in compound statements");
8261 goto bad_stmt;
8263 c_parser_omp_taskyield (parser);
8264 return false;
8266 case PRAGMA_OMP_THREADPRIVATE:
8267 c_parser_omp_threadprivate (parser);
8268 return false;
8270 case PRAGMA_OMP_SECTION:
8271 error_at (c_parser_peek_token (parser)->location,
8272 "%<#pragma omp section%> may only be used in "
8273 "%<#pragma omp sections%> construct");
8274 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8275 return false;
8277 case PRAGMA_GCC_PCH_PREPROCESS:
8278 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8279 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8280 return false;
8282 default:
8283 if (id < PRAGMA_FIRST_EXTERNAL)
8285 if (context == pragma_external)
8287 bad_stmt:
8288 c_parser_error (parser, "expected declaration specifiers");
8289 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8290 return false;
8292 c_parser_omp_construct (parser);
8293 return true;
8295 break;
8298 c_parser_consume_pragma (parser);
8299 c_invoke_pragma_handler (id);
8301 /* Skip to EOL, but suppress any error message. Those will have been
8302 generated by the handler routine through calling error, as opposed
8303 to calling c_parser_error. */
8304 parser->error = true;
8305 c_parser_skip_to_pragma_eol (parser);
8307 return false;
8310 /* The interface the pragma parsers have to the lexer. */
8312 enum cpp_ttype
8313 pragma_lex (tree *value)
8315 c_token *tok = c_parser_peek_token (the_parser);
8316 enum cpp_ttype ret = tok->type;
8318 *value = tok->value;
8319 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8320 ret = CPP_EOF;
8321 else
8323 if (ret == CPP_KEYWORD)
8324 ret = CPP_NAME;
8325 c_parser_consume_token (the_parser);
8328 return ret;
8331 static void
8332 c_parser_pragma_pch_preprocess (c_parser *parser)
8334 tree name = NULL;
8336 c_parser_consume_pragma (parser);
8337 if (c_parser_next_token_is (parser, CPP_STRING))
8339 name = c_parser_peek_token (parser)->value;
8340 c_parser_consume_token (parser);
8342 else
8343 c_parser_error (parser, "expected string literal");
8344 c_parser_skip_to_pragma_eol (parser);
8346 if (name)
8347 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8350 /* OpenMP 2.5 parsing routines. */
8352 /* Returns name of the next clause.
8353 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8354 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8355 returned and the token is consumed. */
8357 static pragma_omp_clause
8358 c_parser_omp_clause_name (c_parser *parser)
8360 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8362 if (c_parser_next_token_is_keyword (parser, RID_IF))
8363 result = PRAGMA_OMP_CLAUSE_IF;
8364 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8365 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8366 else if (c_parser_next_token_is (parser, CPP_NAME))
8368 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8370 switch (p[0])
8372 case 'c':
8373 if (!strcmp ("collapse", p))
8374 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8375 else if (!strcmp ("copyin", p))
8376 result = PRAGMA_OMP_CLAUSE_COPYIN;
8377 else if (!strcmp ("copyprivate", p))
8378 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8379 break;
8380 case 'f':
8381 if (!strcmp ("final", p))
8382 result = PRAGMA_OMP_CLAUSE_FINAL;
8383 else if (!strcmp ("firstprivate", p))
8384 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8385 break;
8386 case 'l':
8387 if (!strcmp ("lastprivate", p))
8388 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8389 break;
8390 case 'm':
8391 if (!strcmp ("mergeable", p))
8392 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
8393 break;
8394 case 'n':
8395 if (!strcmp ("nowait", p))
8396 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8397 else if (!strcmp ("num_threads", p))
8398 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8399 break;
8400 case 'o':
8401 if (!strcmp ("ordered", p))
8402 result = PRAGMA_OMP_CLAUSE_ORDERED;
8403 break;
8404 case 'p':
8405 if (!strcmp ("private", p))
8406 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8407 break;
8408 case 'r':
8409 if (!strcmp ("reduction", p))
8410 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8411 break;
8412 case 's':
8413 if (!strcmp ("schedule", p))
8414 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8415 else if (!strcmp ("shared", p))
8416 result = PRAGMA_OMP_CLAUSE_SHARED;
8417 break;
8418 case 'u':
8419 if (!strcmp ("untied", p))
8420 result = PRAGMA_OMP_CLAUSE_UNTIED;
8421 break;
8425 if (result != PRAGMA_OMP_CLAUSE_NONE)
8426 c_parser_consume_token (parser);
8428 return result;
8431 /* Validate that a clause of the given type does not already exist. */
8433 static void
8434 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8435 const char *name)
8437 tree c;
8439 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8440 if (OMP_CLAUSE_CODE (c) == code)
8442 location_t loc = OMP_CLAUSE_LOCATION (c);
8443 error_at (loc, "too many %qs clauses", name);
8444 break;
8448 /* OpenMP 2.5:
8449 variable-list:
8450 identifier
8451 variable-list , identifier
8453 If KIND is nonzero, create the appropriate node and install the
8454 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8455 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8457 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8458 return the list created. */
8460 static tree
8461 c_parser_omp_variable_list (c_parser *parser,
8462 location_t clause_loc,
8463 enum omp_clause_code kind,
8464 tree list)
8466 if (c_parser_next_token_is_not (parser, CPP_NAME)
8467 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8468 c_parser_error (parser, "expected identifier");
8470 while (c_parser_next_token_is (parser, CPP_NAME)
8471 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8473 tree t = lookup_name (c_parser_peek_token (parser)->value);
8475 if (t == NULL_TREE)
8476 undeclared_variable (c_parser_peek_token (parser)->location,
8477 c_parser_peek_token (parser)->value);
8478 else if (t == error_mark_node)
8480 else if (kind != 0)
8482 tree u = build_omp_clause (clause_loc, kind);
8483 OMP_CLAUSE_DECL (u) = t;
8484 OMP_CLAUSE_CHAIN (u) = list;
8485 list = u;
8487 else
8488 list = tree_cons (t, NULL_TREE, list);
8490 c_parser_consume_token (parser);
8492 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8493 break;
8495 c_parser_consume_token (parser);
8498 return list;
8501 /* Similarly, but expect leading and trailing parenthesis. This is a very
8502 common case for omp clauses. */
8504 static tree
8505 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8506 tree list)
8508 /* The clauses location. */
8509 location_t loc = c_parser_peek_token (parser)->location;
8511 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8513 list = c_parser_omp_variable_list (parser, loc, kind, list);
8514 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8516 return list;
8519 /* OpenMP 3.0:
8520 collapse ( constant-expression ) */
8522 static tree
8523 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8525 tree c, num = error_mark_node;
8526 HOST_WIDE_INT n;
8527 location_t loc;
8529 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8531 loc = c_parser_peek_token (parser)->location;
8532 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8534 num = c_parser_expr_no_commas (parser, NULL).value;
8535 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8537 if (num == error_mark_node)
8538 return list;
8539 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8540 || !host_integerp (num, 0)
8541 || (n = tree_low_cst (num, 0)) <= 0
8542 || (int) n != n)
8544 error_at (loc,
8545 "collapse argument needs positive constant integer expression");
8546 return list;
8548 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8549 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8550 OMP_CLAUSE_CHAIN (c) = list;
8551 return c;
8554 /* OpenMP 2.5:
8555 copyin ( variable-list ) */
8557 static tree
8558 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8560 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8563 /* OpenMP 2.5:
8564 copyprivate ( variable-list ) */
8566 static tree
8567 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8569 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8572 /* OpenMP 2.5:
8573 default ( shared | none ) */
8575 static tree
8576 c_parser_omp_clause_default (c_parser *parser, tree list)
8578 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8579 location_t loc = c_parser_peek_token (parser)->location;
8580 tree c;
8582 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8583 return list;
8584 if (c_parser_next_token_is (parser, CPP_NAME))
8586 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8588 switch (p[0])
8590 case 'n':
8591 if (strcmp ("none", p) != 0)
8592 goto invalid_kind;
8593 kind = OMP_CLAUSE_DEFAULT_NONE;
8594 break;
8596 case 's':
8597 if (strcmp ("shared", p) != 0)
8598 goto invalid_kind;
8599 kind = OMP_CLAUSE_DEFAULT_SHARED;
8600 break;
8602 default:
8603 goto invalid_kind;
8606 c_parser_consume_token (parser);
8608 else
8610 invalid_kind:
8611 c_parser_error (parser, "expected %<none%> or %<shared%>");
8613 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8615 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8616 return list;
8618 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8619 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8620 OMP_CLAUSE_CHAIN (c) = list;
8621 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8623 return c;
8626 /* OpenMP 2.5:
8627 firstprivate ( variable-list ) */
8629 static tree
8630 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8632 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8635 /* OpenMP 3.1:
8636 final ( expression ) */
8638 static tree
8639 c_parser_omp_clause_final (c_parser *parser, tree list)
8641 location_t loc = c_parser_peek_token (parser)->location;
8642 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8644 tree t = c_parser_paren_condition (parser);
8645 tree c;
8647 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
8649 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
8650 OMP_CLAUSE_FINAL_EXPR (c) = t;
8651 OMP_CLAUSE_CHAIN (c) = list;
8652 list = c;
8654 else
8655 c_parser_error (parser, "expected %<(%>");
8657 return list;
8660 /* OpenMP 2.5:
8661 if ( expression ) */
8663 static tree
8664 c_parser_omp_clause_if (c_parser *parser, tree list)
8666 location_t loc = c_parser_peek_token (parser)->location;
8667 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8669 tree t = c_parser_paren_condition (parser);
8670 tree c;
8672 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8674 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8675 OMP_CLAUSE_IF_EXPR (c) = t;
8676 OMP_CLAUSE_CHAIN (c) = list;
8677 list = c;
8679 else
8680 c_parser_error (parser, "expected %<(%>");
8682 return list;
8685 /* OpenMP 2.5:
8686 lastprivate ( variable-list ) */
8688 static tree
8689 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8691 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8694 /* OpenMP 3.1:
8695 mergeable */
8697 static tree
8698 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8700 tree c;
8702 /* FIXME: Should we allow duplicates? */
8703 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
8705 c = build_omp_clause (c_parser_peek_token (parser)->location,
8706 OMP_CLAUSE_MERGEABLE);
8707 OMP_CLAUSE_CHAIN (c) = list;
8709 return c;
8712 /* OpenMP 2.5:
8713 nowait */
8715 static tree
8716 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8718 tree c;
8719 location_t loc = c_parser_peek_token (parser)->location;
8721 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
8723 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
8724 OMP_CLAUSE_CHAIN (c) = list;
8725 return c;
8728 /* OpenMP 2.5:
8729 num_threads ( expression ) */
8731 static tree
8732 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
8734 location_t num_threads_loc = c_parser_peek_token (parser)->location;
8735 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8737 location_t expr_loc = c_parser_peek_token (parser)->location;
8738 tree c, t = c_parser_expression (parser).value;
8739 t = c_fully_fold (t, false, NULL);
8741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8743 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
8745 c_parser_error (parser, "expected integer expression");
8746 return list;
8749 /* Attempt to statically determine when the number isn't positive. */
8750 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
8751 build_int_cst (TREE_TYPE (t), 0));
8752 if (CAN_HAVE_LOCATION_P (c))
8753 SET_EXPR_LOCATION (c, expr_loc);
8754 if (c == boolean_true_node)
8756 warning_at (expr_loc, 0,
8757 "%<num_threads%> value must be positive");
8758 t = integer_one_node;
8761 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
8763 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
8764 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
8765 OMP_CLAUSE_CHAIN (c) = list;
8766 list = c;
8769 return list;
8772 /* OpenMP 2.5:
8773 ordered */
8775 static tree
8776 c_parser_omp_clause_ordered (c_parser *parser, tree list)
8778 tree c;
8780 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
8782 c = build_omp_clause (c_parser_peek_token (parser)->location,
8783 OMP_CLAUSE_ORDERED);
8784 OMP_CLAUSE_CHAIN (c) = list;
8786 return c;
8789 /* OpenMP 2.5:
8790 private ( variable-list ) */
8792 static tree
8793 c_parser_omp_clause_private (c_parser *parser, tree list)
8795 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
8798 /* OpenMP 2.5:
8799 reduction ( reduction-operator : variable-list )
8801 reduction-operator:
8802 One of: + * - & ^ | && ||
8804 OpenMP 3.1:
8806 reduction-operator:
8807 One of: + * - & ^ | && || max min */
8809 static tree
8810 c_parser_omp_clause_reduction (c_parser *parser, tree list)
8812 location_t clause_loc = c_parser_peek_token (parser)->location;
8813 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8815 enum tree_code code;
8817 switch (c_parser_peek_token (parser)->type)
8819 case CPP_PLUS:
8820 code = PLUS_EXPR;
8821 break;
8822 case CPP_MULT:
8823 code = MULT_EXPR;
8824 break;
8825 case CPP_MINUS:
8826 code = MINUS_EXPR;
8827 break;
8828 case CPP_AND:
8829 code = BIT_AND_EXPR;
8830 break;
8831 case CPP_XOR:
8832 code = BIT_XOR_EXPR;
8833 break;
8834 case CPP_OR:
8835 code = BIT_IOR_EXPR;
8836 break;
8837 case CPP_AND_AND:
8838 code = TRUTH_ANDIF_EXPR;
8839 break;
8840 case CPP_OR_OR:
8841 code = TRUTH_ORIF_EXPR;
8842 break;
8843 case CPP_NAME:
8845 const char *p
8846 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8847 if (strcmp (p, "min") == 0)
8849 code = MIN_EXPR;
8850 break;
8852 if (strcmp (p, "max") == 0)
8854 code = MAX_EXPR;
8855 break;
8858 /* FALLTHRU */
8859 default:
8860 c_parser_error (parser,
8861 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
8862 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
8863 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8864 return list;
8866 c_parser_consume_token (parser);
8867 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8869 tree nl, c;
8871 nl = c_parser_omp_variable_list (parser, clause_loc,
8872 OMP_CLAUSE_REDUCTION, list);
8873 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
8874 OMP_CLAUSE_REDUCTION_CODE (c) = code;
8876 list = nl;
8878 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8880 return list;
8883 /* OpenMP 2.5:
8884 schedule ( schedule-kind )
8885 schedule ( schedule-kind , expression )
8887 schedule-kind:
8888 static | dynamic | guided | runtime | auto
8891 static tree
8892 c_parser_omp_clause_schedule (c_parser *parser, tree list)
8894 tree c, t;
8895 location_t loc = c_parser_peek_token (parser)->location;
8897 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8898 return list;
8900 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
8902 if (c_parser_next_token_is (parser, CPP_NAME))
8904 tree kind = c_parser_peek_token (parser)->value;
8905 const char *p = IDENTIFIER_POINTER (kind);
8907 switch (p[0])
8909 case 'd':
8910 if (strcmp ("dynamic", p) != 0)
8911 goto invalid_kind;
8912 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
8913 break;
8915 case 'g':
8916 if (strcmp ("guided", p) != 0)
8917 goto invalid_kind;
8918 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
8919 break;
8921 case 'r':
8922 if (strcmp ("runtime", p) != 0)
8923 goto invalid_kind;
8924 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
8925 break;
8927 default:
8928 goto invalid_kind;
8931 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
8932 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
8933 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
8934 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8935 else
8936 goto invalid_kind;
8938 c_parser_consume_token (parser);
8939 if (c_parser_next_token_is (parser, CPP_COMMA))
8941 location_t here;
8942 c_parser_consume_token (parser);
8944 here = c_parser_peek_token (parser)->location;
8945 t = c_parser_expr_no_commas (parser, NULL).value;
8946 t = c_fully_fold (t, false, NULL);
8948 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
8949 error_at (here, "schedule %<runtime%> does not take "
8950 "a %<chunk_size%> parameter");
8951 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
8952 error_at (here,
8953 "schedule %<auto%> does not take "
8954 "a %<chunk_size%> parameter");
8955 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
8956 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
8957 else
8958 c_parser_error (parser, "expected integer expression");
8960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8962 else
8963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8964 "expected %<,%> or %<)%>");
8966 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
8967 OMP_CLAUSE_CHAIN (c) = list;
8968 return c;
8970 invalid_kind:
8971 c_parser_error (parser, "invalid schedule kind");
8972 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
8973 return list;
8976 /* OpenMP 2.5:
8977 shared ( variable-list ) */
8979 static tree
8980 c_parser_omp_clause_shared (c_parser *parser, tree list)
8982 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
8985 /* OpenMP 3.0:
8986 untied */
8988 static tree
8989 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8991 tree c;
8993 /* FIXME: Should we allow duplicates? */
8994 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
8996 c = build_omp_clause (c_parser_peek_token (parser)->location,
8997 OMP_CLAUSE_UNTIED);
8998 OMP_CLAUSE_CHAIN (c) = list;
9000 return c;
9003 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9004 is a bitmask in MASK. Return the list of clauses found; the result
9005 of clause default goes in *pdefault. */
9007 static tree
9008 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9009 const char *where)
9011 tree clauses = NULL;
9012 bool first = true;
9014 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9016 location_t here;
9017 pragma_omp_clause c_kind;
9018 const char *c_name;
9019 tree prev = clauses;
9021 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9022 c_parser_consume_token (parser);
9024 first = false;
9025 here = c_parser_peek_token (parser)->location;
9026 c_kind = c_parser_omp_clause_name (parser);
9028 switch (c_kind)
9030 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9031 clauses = c_parser_omp_clause_collapse (parser, clauses);
9032 c_name = "collapse";
9033 break;
9034 case PRAGMA_OMP_CLAUSE_COPYIN:
9035 clauses = c_parser_omp_clause_copyin (parser, clauses);
9036 c_name = "copyin";
9037 break;
9038 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9039 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9040 c_name = "copyprivate";
9041 break;
9042 case PRAGMA_OMP_CLAUSE_DEFAULT:
9043 clauses = c_parser_omp_clause_default (parser, clauses);
9044 c_name = "default";
9045 break;
9046 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9047 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9048 c_name = "firstprivate";
9049 break;
9050 case PRAGMA_OMP_CLAUSE_FINAL:
9051 clauses = c_parser_omp_clause_final (parser, clauses);
9052 c_name = "final";
9053 break;
9054 case PRAGMA_OMP_CLAUSE_IF:
9055 clauses = c_parser_omp_clause_if (parser, clauses);
9056 c_name = "if";
9057 break;
9058 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9059 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9060 c_name = "lastprivate";
9061 break;
9062 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9063 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9064 c_name = "mergeable";
9065 break;
9066 case PRAGMA_OMP_CLAUSE_NOWAIT:
9067 clauses = c_parser_omp_clause_nowait (parser, clauses);
9068 c_name = "nowait";
9069 break;
9070 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9071 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9072 c_name = "num_threads";
9073 break;
9074 case PRAGMA_OMP_CLAUSE_ORDERED:
9075 clauses = c_parser_omp_clause_ordered (parser, clauses);
9076 c_name = "ordered";
9077 break;
9078 case PRAGMA_OMP_CLAUSE_PRIVATE:
9079 clauses = c_parser_omp_clause_private (parser, clauses);
9080 c_name = "private";
9081 break;
9082 case PRAGMA_OMP_CLAUSE_REDUCTION:
9083 clauses = c_parser_omp_clause_reduction (parser, clauses);
9084 c_name = "reduction";
9085 break;
9086 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9087 clauses = c_parser_omp_clause_schedule (parser, clauses);
9088 c_name = "schedule";
9089 break;
9090 case PRAGMA_OMP_CLAUSE_SHARED:
9091 clauses = c_parser_omp_clause_shared (parser, clauses);
9092 c_name = "shared";
9093 break;
9094 case PRAGMA_OMP_CLAUSE_UNTIED:
9095 clauses = c_parser_omp_clause_untied (parser, clauses);
9096 c_name = "untied";
9097 break;
9098 default:
9099 c_parser_error (parser, "expected %<#pragma omp%> clause");
9100 goto saw_error;
9103 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9105 /* Remove the invalid clause(s) from the list to avoid
9106 confusing the rest of the compiler. */
9107 clauses = prev;
9108 error_at (here, "%qs is not valid for %qs", c_name, where);
9112 saw_error:
9113 c_parser_skip_to_pragma_eol (parser);
9115 return c_finish_omp_clauses (clauses);
9118 /* OpenMP 2.5:
9119 structured-block:
9120 statement
9122 In practice, we're also interested in adding the statement to an
9123 outer node. So it is convenient if we work around the fact that
9124 c_parser_statement calls add_stmt. */
9126 static tree
9127 c_parser_omp_structured_block (c_parser *parser)
9129 tree stmt = push_stmt_list ();
9130 c_parser_statement (parser);
9131 return pop_stmt_list (stmt);
9134 /* OpenMP 2.5:
9135 # pragma omp atomic new-line
9136 expression-stmt
9138 expression-stmt:
9139 x binop= expr | x++ | ++x | x-- | --x
9140 binop:
9141 +, *, -, /, &, ^, |, <<, >>
9143 where x is an lvalue expression with scalar type.
9145 OpenMP 3.1:
9146 # pragma omp atomic new-line
9147 update-stmt
9149 # pragma omp atomic read new-line
9150 read-stmt
9152 # pragma omp atomic write new-line
9153 write-stmt
9155 # pragma omp atomic update new-line
9156 update-stmt
9158 # pragma omp atomic capture new-line
9159 capture-stmt
9161 # pragma omp atomic capture new-line
9162 capture-block
9164 read-stmt:
9165 v = x
9166 write-stmt:
9167 x = expr
9168 update-stmt:
9169 expression-stmt | x = x binop expr
9170 capture-stmt:
9171 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9172 capture-block:
9173 { v = x; update-stmt; } | { update-stmt; v = x; }
9175 where x and v are lvalue expressions with scalar type.
9177 LOC is the location of the #pragma token. */
9179 static void
9180 c_parser_omp_atomic (location_t loc, c_parser *parser)
9182 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9183 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9184 tree stmt, orig_lhs;
9185 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9186 struct c_expr rhs_expr;
9187 bool structured_block = false;
9189 if (c_parser_next_token_is (parser, CPP_NAME))
9191 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9193 if (!strcmp (p, "read"))
9194 code = OMP_ATOMIC_READ;
9195 else if (!strcmp (p, "write"))
9196 code = NOP_EXPR;
9197 else if (!strcmp (p, "update"))
9198 code = OMP_ATOMIC;
9199 else if (!strcmp (p, "capture"))
9200 code = OMP_ATOMIC_CAPTURE_NEW;
9201 else
9202 p = NULL;
9203 if (p)
9204 c_parser_consume_token (parser);
9206 c_parser_skip_to_pragma_eol (parser);
9208 switch (code)
9210 case OMP_ATOMIC_READ:
9211 case NOP_EXPR: /* atomic write */
9212 v = c_parser_unary_expression (parser).value;
9213 v = c_fully_fold (v, false, NULL);
9214 if (v == error_mark_node)
9215 goto saw_error;
9216 loc = c_parser_peek_token (parser)->location;
9217 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9218 goto saw_error;
9219 if (code == NOP_EXPR)
9220 lhs = c_parser_expression (parser).value;
9221 else
9222 lhs = c_parser_unary_expression (parser).value;
9223 lhs = c_fully_fold (lhs, false, NULL);
9224 if (lhs == error_mark_node)
9225 goto saw_error;
9226 if (code == NOP_EXPR)
9228 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
9229 opcode. */
9230 code = OMP_ATOMIC;
9231 rhs = lhs;
9232 lhs = v;
9233 v = NULL_TREE;
9235 goto done;
9236 case OMP_ATOMIC_CAPTURE_NEW:
9237 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9239 c_parser_consume_token (parser);
9240 structured_block = true;
9242 else
9244 v = c_parser_unary_expression (parser).value;
9245 v = c_fully_fold (v, false, NULL);
9246 if (v == error_mark_node)
9247 goto saw_error;
9248 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9249 goto saw_error;
9251 break;
9252 default:
9253 break;
9256 /* For structured_block case we don't know yet whether
9257 old or new x should be captured. */
9258 restart:
9259 lhs = c_parser_unary_expression (parser).value;
9260 lhs = c_fully_fold (lhs, false, NULL);
9261 orig_lhs = lhs;
9262 switch (TREE_CODE (lhs))
9264 case ERROR_MARK:
9265 saw_error:
9266 c_parser_skip_to_end_of_block_or_statement (parser);
9267 if (structured_block)
9269 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9270 c_parser_consume_token (parser);
9271 else if (code == OMP_ATOMIC_CAPTURE_NEW)
9273 c_parser_skip_to_end_of_block_or_statement (parser);
9274 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9275 c_parser_consume_token (parser);
9278 return;
9280 case POSTINCREMENT_EXPR:
9281 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9282 code = OMP_ATOMIC_CAPTURE_OLD;
9283 /* FALLTHROUGH */
9284 case PREINCREMENT_EXPR:
9285 lhs = TREE_OPERAND (lhs, 0);
9286 opcode = PLUS_EXPR;
9287 rhs = integer_one_node;
9288 break;
9290 case POSTDECREMENT_EXPR:
9291 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9292 code = OMP_ATOMIC_CAPTURE_OLD;
9293 /* FALLTHROUGH */
9294 case PREDECREMENT_EXPR:
9295 lhs = TREE_OPERAND (lhs, 0);
9296 opcode = MINUS_EXPR;
9297 rhs = integer_one_node;
9298 break;
9300 case COMPOUND_EXPR:
9301 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9302 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9303 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9304 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9305 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9306 (TREE_OPERAND (lhs, 1), 0), 0)))
9307 == BOOLEAN_TYPE)
9308 /* Undo effects of boolean_increment for post {in,de}crement. */
9309 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9310 /* FALLTHRU */
9311 case MODIFY_EXPR:
9312 if (TREE_CODE (lhs) == MODIFY_EXPR
9313 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9315 /* Undo effects of boolean_increment. */
9316 if (integer_onep (TREE_OPERAND (lhs, 1)))
9318 /* This is pre or post increment. */
9319 rhs = TREE_OPERAND (lhs, 1);
9320 lhs = TREE_OPERAND (lhs, 0);
9321 opcode = NOP_EXPR;
9322 if (code == OMP_ATOMIC_CAPTURE_NEW
9323 && !structured_block
9324 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9325 code = OMP_ATOMIC_CAPTURE_OLD;
9326 break;
9328 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9329 && TREE_OPERAND (lhs, 0)
9330 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9332 /* This is pre or post decrement. */
9333 rhs = TREE_OPERAND (lhs, 1);
9334 lhs = TREE_OPERAND (lhs, 0);
9335 opcode = NOP_EXPR;
9336 if (code == OMP_ATOMIC_CAPTURE_NEW
9337 && !structured_block
9338 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9339 code = OMP_ATOMIC_CAPTURE_OLD;
9340 break;
9343 /* FALLTHRU */
9344 default:
9345 switch (c_parser_peek_token (parser)->type)
9347 case CPP_MULT_EQ:
9348 opcode = MULT_EXPR;
9349 break;
9350 case CPP_DIV_EQ:
9351 opcode = TRUNC_DIV_EXPR;
9352 break;
9353 case CPP_PLUS_EQ:
9354 opcode = PLUS_EXPR;
9355 break;
9356 case CPP_MINUS_EQ:
9357 opcode = MINUS_EXPR;
9358 break;
9359 case CPP_LSHIFT_EQ:
9360 opcode = LSHIFT_EXPR;
9361 break;
9362 case CPP_RSHIFT_EQ:
9363 opcode = RSHIFT_EXPR;
9364 break;
9365 case CPP_AND_EQ:
9366 opcode = BIT_AND_EXPR;
9367 break;
9368 case CPP_OR_EQ:
9369 opcode = BIT_IOR_EXPR;
9370 break;
9371 case CPP_XOR_EQ:
9372 opcode = BIT_XOR_EXPR;
9373 break;
9374 case CPP_EQ:
9375 if (structured_block || code == OMP_ATOMIC)
9377 location_t aloc = c_parser_peek_token (parser)->location;
9378 location_t rhs_loc;
9379 enum c_parser_prec oprec = PREC_NONE;
9381 c_parser_consume_token (parser);
9382 rhs1 = c_parser_unary_expression (parser).value;
9383 rhs1 = c_fully_fold (rhs1, false, NULL);
9384 if (rhs1 == error_mark_node)
9385 goto saw_error;
9386 switch (c_parser_peek_token (parser)->type)
9388 case CPP_SEMICOLON:
9389 if (code == OMP_ATOMIC_CAPTURE_NEW)
9391 code = OMP_ATOMIC_CAPTURE_OLD;
9392 v = lhs;
9393 lhs = NULL_TREE;
9394 lhs1 = rhs1;
9395 rhs1 = NULL_TREE;
9396 c_parser_consume_token (parser);
9397 goto restart;
9399 c_parser_error (parser,
9400 "invalid form of %<#pragma omp atomic%>");
9401 goto saw_error;
9402 case CPP_MULT:
9403 opcode = MULT_EXPR;
9404 oprec = PREC_MULT;
9405 break;
9406 case CPP_DIV:
9407 opcode = TRUNC_DIV_EXPR;
9408 oprec = PREC_MULT;
9409 break;
9410 case CPP_PLUS:
9411 opcode = PLUS_EXPR;
9412 oprec = PREC_ADD;
9413 break;
9414 case CPP_MINUS:
9415 opcode = MINUS_EXPR;
9416 oprec = PREC_ADD;
9417 break;
9418 case CPP_LSHIFT:
9419 opcode = LSHIFT_EXPR;
9420 oprec = PREC_SHIFT;
9421 break;
9422 case CPP_RSHIFT:
9423 opcode = RSHIFT_EXPR;
9424 oprec = PREC_SHIFT;
9425 break;
9426 case CPP_AND:
9427 opcode = BIT_AND_EXPR;
9428 oprec = PREC_BITAND;
9429 break;
9430 case CPP_OR:
9431 opcode = BIT_IOR_EXPR;
9432 oprec = PREC_BITOR;
9433 break;
9434 case CPP_XOR:
9435 opcode = BIT_XOR_EXPR;
9436 oprec = PREC_BITXOR;
9437 break;
9438 default:
9439 c_parser_error (parser,
9440 "invalid operator for %<#pragma omp atomic%>");
9441 goto saw_error;
9443 loc = aloc;
9444 c_parser_consume_token (parser);
9445 rhs_loc = c_parser_peek_token (parser)->location;
9446 if (commutative_tree_code (opcode))
9447 oprec = (enum c_parser_prec) (oprec - 1);
9448 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
9449 rhs_expr = default_function_array_read_conversion (rhs_loc,
9450 rhs_expr);
9451 rhs = rhs_expr.value;
9452 rhs = c_fully_fold (rhs, false, NULL);
9453 goto stmt_done;
9455 /* FALLTHROUGH */
9456 default:
9457 c_parser_error (parser,
9458 "invalid operator for %<#pragma omp atomic%>");
9459 goto saw_error;
9462 /* Arrange to pass the location of the assignment operator to
9463 c_finish_omp_atomic. */
9464 loc = c_parser_peek_token (parser)->location;
9465 c_parser_consume_token (parser);
9467 location_t rhs_loc = c_parser_peek_token (parser)->location;
9468 rhs_expr = c_parser_expression (parser);
9469 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9471 rhs = rhs_expr.value;
9472 rhs = c_fully_fold (rhs, false, NULL);
9473 break;
9475 stmt_done:
9476 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
9478 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
9479 goto saw_error;
9480 v = c_parser_unary_expression (parser).value;
9481 v = c_fully_fold (v, false, NULL);
9482 if (v == error_mark_node)
9483 goto saw_error;
9484 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9485 goto saw_error;
9486 lhs1 = c_parser_unary_expression (parser).value;
9487 lhs1 = c_fully_fold (lhs1, false, NULL);
9488 if (lhs1 == error_mark_node)
9489 goto saw_error;
9491 if (structured_block)
9493 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9494 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
9496 done:
9497 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
9498 if (stmt != error_mark_node)
9499 add_stmt (stmt);
9501 if (!structured_block)
9502 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9506 /* OpenMP 2.5:
9507 # pragma omp barrier new-line
9510 static void
9511 c_parser_omp_barrier (c_parser *parser)
9513 location_t loc = c_parser_peek_token (parser)->location;
9514 c_parser_consume_pragma (parser);
9515 c_parser_skip_to_pragma_eol (parser);
9517 c_finish_omp_barrier (loc);
9520 /* OpenMP 2.5:
9521 # pragma omp critical [(name)] new-line
9522 structured-block
9524 LOC is the location of the #pragma itself. */
9526 static tree
9527 c_parser_omp_critical (location_t loc, c_parser *parser)
9529 tree stmt, name = NULL;
9531 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9533 c_parser_consume_token (parser);
9534 if (c_parser_next_token_is (parser, CPP_NAME))
9536 name = c_parser_peek_token (parser)->value;
9537 c_parser_consume_token (parser);
9538 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9540 else
9541 c_parser_error (parser, "expected identifier");
9543 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9544 c_parser_error (parser, "expected %<(%> or end of line");
9545 c_parser_skip_to_pragma_eol (parser);
9547 stmt = c_parser_omp_structured_block (parser);
9548 return c_finish_omp_critical (loc, stmt, name);
9551 /* OpenMP 2.5:
9552 # pragma omp flush flush-vars[opt] new-line
9554 flush-vars:
9555 ( variable-list ) */
9557 static void
9558 c_parser_omp_flush (c_parser *parser)
9560 location_t loc = c_parser_peek_token (parser)->location;
9561 c_parser_consume_pragma (parser);
9562 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9563 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9564 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9565 c_parser_error (parser, "expected %<(%> or end of line");
9566 c_parser_skip_to_pragma_eol (parser);
9568 c_finish_omp_flush (loc);
9571 /* Parse the restricted form of the for statement allowed by OpenMP.
9572 The real trick here is to determine the loop control variable early
9573 so that we can push a new decl if necessary to make it private.
9574 LOC is the location of the OMP in "#pragma omp". */
9576 static tree
9577 c_parser_omp_for_loop (location_t loc,
9578 c_parser *parser, tree clauses, tree *par_clauses)
9580 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9581 tree declv, condv, incrv, initv, ret = NULL;
9582 bool fail = false, open_brace_parsed = false;
9583 int i, collapse = 1, nbraces = 0;
9584 location_t for_loc;
9585 VEC(tree,gc) *for_block = make_tree_vector ();
9587 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9588 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9589 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9591 gcc_assert (collapse >= 1);
9593 declv = make_tree_vec (collapse);
9594 initv = make_tree_vec (collapse);
9595 condv = make_tree_vec (collapse);
9596 incrv = make_tree_vec (collapse);
9598 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9600 c_parser_error (parser, "for statement expected");
9601 return NULL;
9603 for_loc = c_parser_peek_token (parser)->location;
9604 c_parser_consume_token (parser);
9606 for (i = 0; i < collapse; i++)
9608 int bracecount = 0;
9610 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9611 goto pop_scopes;
9613 /* Parse the initialization declaration or expression. */
9614 if (c_parser_next_tokens_start_declaration (parser))
9616 if (i > 0)
9617 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9618 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9619 decl = check_for_loop_decls (for_loc, flag_isoc99);
9620 if (decl == NULL)
9621 goto error_init;
9622 if (DECL_INITIAL (decl) == error_mark_node)
9623 decl = error_mark_node;
9624 init = decl;
9626 else if (c_parser_next_token_is (parser, CPP_NAME)
9627 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9629 struct c_expr decl_exp;
9630 struct c_expr init_exp;
9631 location_t init_loc;
9633 decl_exp = c_parser_postfix_expression (parser);
9634 decl = decl_exp.value;
9636 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9638 init_loc = c_parser_peek_token (parser)->location;
9639 init_exp = c_parser_expr_no_commas (parser, NULL);
9640 init_exp = default_function_array_read_conversion (init_loc,
9641 init_exp);
9642 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9643 NOP_EXPR, init_loc, init_exp.value,
9644 init_exp.original_type);
9645 init = c_process_expr_stmt (init_loc, init);
9647 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9649 else
9651 error_init:
9652 c_parser_error (parser,
9653 "expected iteration declaration or initialization");
9654 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9655 "expected %<)%>");
9656 fail = true;
9657 goto parse_next;
9660 /* Parse the loop condition. */
9661 cond = NULL_TREE;
9662 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9664 location_t cond_loc = c_parser_peek_token (parser)->location;
9665 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
9666 PREC_NONE);
9668 cond = cond_expr.value;
9669 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9670 cond = c_fully_fold (cond, false, NULL);
9671 switch (cond_expr.original_code)
9673 case GT_EXPR:
9674 case GE_EXPR:
9675 case LT_EXPR:
9676 case LE_EXPR:
9677 break;
9678 default:
9679 /* Can't be cond = error_mark_node, because we want to preserve
9680 the location until c_finish_omp_for. */
9681 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9682 break;
9684 protected_set_expr_location (cond, cond_loc);
9686 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9688 /* Parse the increment expression. */
9689 incr = NULL_TREE;
9690 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9692 location_t incr_loc = c_parser_peek_token (parser)->location;
9694 incr = c_process_expr_stmt (incr_loc,
9695 c_parser_expression (parser).value);
9697 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9699 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9700 fail = true;
9701 else
9703 TREE_VEC_ELT (declv, i) = decl;
9704 TREE_VEC_ELT (initv, i) = init;
9705 TREE_VEC_ELT (condv, i) = cond;
9706 TREE_VEC_ELT (incrv, i) = incr;
9709 parse_next:
9710 if (i == collapse - 1)
9711 break;
9713 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
9714 in between the collapsed for loops to be still considered perfectly
9715 nested. Hopefully the final version clarifies this.
9716 For now handle (multiple) {'s and empty statements. */
9719 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9721 c_parser_consume_token (parser);
9722 break;
9724 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9726 c_parser_consume_token (parser);
9727 bracecount++;
9729 else if (bracecount
9730 && c_parser_next_token_is (parser, CPP_SEMICOLON))
9731 c_parser_consume_token (parser);
9732 else
9734 c_parser_error (parser, "not enough perfectly nested loops");
9735 if (bracecount)
9737 open_brace_parsed = true;
9738 bracecount--;
9740 fail = true;
9741 collapse = 0;
9742 break;
9745 while (1);
9747 nbraces += bracecount;
9750 save_break = c_break_label;
9751 c_break_label = size_one_node;
9752 save_cont = c_cont_label;
9753 c_cont_label = NULL_TREE;
9754 body = push_stmt_list ();
9756 if (open_brace_parsed)
9758 location_t here = c_parser_peek_token (parser)->location;
9759 stmt = c_begin_compound_stmt (true);
9760 c_parser_compound_statement_nostart (parser);
9761 add_stmt (c_end_compound_stmt (here, stmt, true));
9763 else
9764 add_stmt (c_parser_c99_block_statement (parser));
9765 if (c_cont_label)
9767 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
9768 SET_EXPR_LOCATION (t, loc);
9769 add_stmt (t);
9772 body = pop_stmt_list (body);
9773 c_break_label = save_break;
9774 c_cont_label = save_cont;
9776 while (nbraces)
9778 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9780 c_parser_consume_token (parser);
9781 nbraces--;
9783 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9784 c_parser_consume_token (parser);
9785 else
9787 c_parser_error (parser, "collapsed loops not perfectly nested");
9788 while (nbraces)
9790 location_t here = c_parser_peek_token (parser)->location;
9791 stmt = c_begin_compound_stmt (true);
9792 add_stmt (body);
9793 c_parser_compound_statement_nostart (parser);
9794 body = c_end_compound_stmt (here, stmt, true);
9795 nbraces--;
9797 goto pop_scopes;
9801 /* Only bother calling c_finish_omp_for if we haven't already generated
9802 an error from the initialization parsing. */
9803 if (!fail)
9805 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
9806 if (stmt)
9808 if (par_clauses != NULL)
9810 tree *c;
9811 for (c = par_clauses; *c ; )
9812 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
9813 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
9814 c = &OMP_CLAUSE_CHAIN (*c);
9815 else
9817 for (i = 0; i < collapse; i++)
9818 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
9819 break;
9820 if (i == collapse)
9821 c = &OMP_CLAUSE_CHAIN (*c);
9822 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
9824 error_at (loc,
9825 "iteration variable %qD should not be firstprivate",
9826 OMP_CLAUSE_DECL (*c));
9827 *c = OMP_CLAUSE_CHAIN (*c);
9829 else
9831 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
9832 change it to shared (decl) in
9833 OMP_PARALLEL_CLAUSES. */
9834 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
9835 OMP_CLAUSE_LASTPRIVATE);
9836 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
9837 OMP_CLAUSE_CHAIN (l) = clauses;
9838 clauses = l;
9839 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
9843 OMP_FOR_CLAUSES (stmt) = clauses;
9845 ret = stmt;
9847 pop_scopes:
9848 while (!VEC_empty (tree, for_block))
9850 /* FIXME diagnostics: LOC below should be the actual location of
9851 this particular for block. We need to build a list of
9852 locations to go along with FOR_BLOCK. */
9853 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
9854 add_stmt (stmt);
9856 release_tree_vector (for_block);
9857 return ret;
9860 /* OpenMP 2.5:
9861 #pragma omp for for-clause[optseq] new-line
9862 for-loop
9864 LOC is the location of the #pragma token.
9867 #define OMP_FOR_CLAUSE_MASK \
9868 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
9869 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
9870 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
9871 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
9872 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
9873 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
9874 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
9875 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
9877 static tree
9878 c_parser_omp_for (location_t loc, c_parser *parser)
9880 tree block, clauses, ret;
9882 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
9883 "#pragma omp for");
9885 block = c_begin_compound_stmt (true);
9886 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
9887 block = c_end_compound_stmt (loc, block, true);
9888 add_stmt (block);
9890 return ret;
9893 /* OpenMP 2.5:
9894 # pragma omp master new-line
9895 structured-block
9897 LOC is the location of the #pragma token.
9900 static tree
9901 c_parser_omp_master (location_t loc, c_parser *parser)
9903 c_parser_skip_to_pragma_eol (parser);
9904 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
9907 /* OpenMP 2.5:
9908 # pragma omp ordered new-line
9909 structured-block
9911 LOC is the location of the #pragma itself.
9914 static tree
9915 c_parser_omp_ordered (location_t loc, c_parser *parser)
9917 c_parser_skip_to_pragma_eol (parser);
9918 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
9921 /* OpenMP 2.5:
9923 section-scope:
9924 { section-sequence }
9926 section-sequence:
9927 section-directive[opt] structured-block
9928 section-sequence section-directive structured-block
9930 SECTIONS_LOC is the location of the #pragma omp sections. */
9932 static tree
9933 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
9935 tree stmt, substmt;
9936 bool error_suppress = false;
9937 location_t loc;
9939 loc = c_parser_peek_token (parser)->location;
9940 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9942 /* Avoid skipping until the end of the block. */
9943 parser->error = false;
9944 return NULL_TREE;
9947 stmt = push_stmt_list ();
9949 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
9951 substmt = push_stmt_list ();
9953 while (1)
9955 c_parser_statement (parser);
9957 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9958 break;
9959 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9960 break;
9961 if (c_parser_next_token_is (parser, CPP_EOF))
9962 break;
9965 substmt = pop_stmt_list (substmt);
9966 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9967 SET_EXPR_LOCATION (substmt, loc);
9968 add_stmt (substmt);
9971 while (1)
9973 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9974 break;
9975 if (c_parser_next_token_is (parser, CPP_EOF))
9976 break;
9978 loc = c_parser_peek_token (parser)->location;
9979 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
9981 c_parser_consume_pragma (parser);
9982 c_parser_skip_to_pragma_eol (parser);
9983 error_suppress = false;
9985 else if (!error_suppress)
9987 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
9988 error_suppress = true;
9991 substmt = c_parser_omp_structured_block (parser);
9992 substmt = build1 (OMP_SECTION, void_type_node, substmt);
9993 SET_EXPR_LOCATION (substmt, loc);
9994 add_stmt (substmt);
9996 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
9997 "expected %<#pragma omp section%> or %<}%>");
9999 substmt = pop_stmt_list (stmt);
10001 stmt = make_node (OMP_SECTIONS);
10002 SET_EXPR_LOCATION (stmt, sections_loc);
10003 TREE_TYPE (stmt) = void_type_node;
10004 OMP_SECTIONS_BODY (stmt) = substmt;
10006 return add_stmt (stmt);
10009 /* OpenMP 2.5:
10010 # pragma omp sections sections-clause[optseq] newline
10011 sections-scope
10013 LOC is the location of the #pragma token.
10016 #define OMP_SECTIONS_CLAUSE_MASK \
10017 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10018 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10019 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10020 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10021 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10023 static tree
10024 c_parser_omp_sections (location_t loc, c_parser *parser)
10026 tree block, clauses, ret;
10028 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10029 "#pragma omp sections");
10031 block = c_begin_compound_stmt (true);
10032 ret = c_parser_omp_sections_scope (loc, parser);
10033 if (ret)
10034 OMP_SECTIONS_CLAUSES (ret) = clauses;
10035 block = c_end_compound_stmt (loc, block, true);
10036 add_stmt (block);
10038 return ret;
10041 /* OpenMP 2.5:
10042 # pragma parallel parallel-clause new-line
10043 # pragma parallel for parallel-for-clause new-line
10044 # pragma parallel sections parallel-sections-clause new-line
10046 LOC is the location of the #pragma token.
10049 #define OMP_PARALLEL_CLAUSE_MASK \
10050 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10051 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10052 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10053 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10054 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10055 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10056 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10057 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10059 static tree
10060 c_parser_omp_parallel (location_t loc, c_parser *parser)
10062 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10063 const char *p_name = "#pragma omp parallel";
10064 tree stmt, clauses, par_clause, ws_clause, block;
10065 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10067 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10069 c_parser_consume_token (parser);
10070 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10071 p_name = "#pragma omp parallel for";
10072 mask |= OMP_FOR_CLAUSE_MASK;
10073 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10075 else if (c_parser_next_token_is (parser, CPP_NAME))
10077 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10078 if (strcmp (p, "sections") == 0)
10080 c_parser_consume_token (parser);
10081 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10082 p_name = "#pragma omp parallel sections";
10083 mask |= OMP_SECTIONS_CLAUSE_MASK;
10084 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10088 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10090 switch (p_kind)
10092 case PRAGMA_OMP_PARALLEL:
10093 block = c_begin_omp_parallel ();
10094 c_parser_statement (parser);
10095 stmt = c_finish_omp_parallel (loc, clauses, block);
10096 break;
10098 case PRAGMA_OMP_PARALLEL_FOR:
10099 block = c_begin_omp_parallel ();
10100 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10101 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10102 stmt = c_finish_omp_parallel (loc, par_clause, block);
10103 OMP_PARALLEL_COMBINED (stmt) = 1;
10104 break;
10106 case PRAGMA_OMP_PARALLEL_SECTIONS:
10107 block = c_begin_omp_parallel ();
10108 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10109 stmt = c_parser_omp_sections_scope (loc, parser);
10110 if (stmt)
10111 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10112 stmt = c_finish_omp_parallel (loc, par_clause, block);
10113 OMP_PARALLEL_COMBINED (stmt) = 1;
10114 break;
10116 default:
10117 gcc_unreachable ();
10120 return stmt;
10123 /* OpenMP 2.5:
10124 # pragma omp single single-clause[optseq] new-line
10125 structured-block
10127 LOC is the location of the #pragma.
10130 #define OMP_SINGLE_CLAUSE_MASK \
10131 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10132 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10133 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10134 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10136 static tree
10137 c_parser_omp_single (location_t loc, c_parser *parser)
10139 tree stmt = make_node (OMP_SINGLE);
10140 SET_EXPR_LOCATION (stmt, loc);
10141 TREE_TYPE (stmt) = void_type_node;
10143 OMP_SINGLE_CLAUSES (stmt)
10144 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10145 "#pragma omp single");
10146 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10148 return add_stmt (stmt);
10151 /* OpenMP 3.0:
10152 # pragma omp task task-clause[optseq] new-line
10154 LOC is the location of the #pragma.
10157 #define OMP_TASK_CLAUSE_MASK \
10158 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10159 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10160 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10161 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10162 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10163 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10164 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10165 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10167 static tree
10168 c_parser_omp_task (location_t loc, c_parser *parser)
10170 tree clauses, block;
10172 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10173 "#pragma omp task");
10175 block = c_begin_omp_task ();
10176 c_parser_statement (parser);
10177 return c_finish_omp_task (loc, clauses, block);
10180 /* OpenMP 3.0:
10181 # pragma omp taskwait new-line
10184 static void
10185 c_parser_omp_taskwait (c_parser *parser)
10187 location_t loc = c_parser_peek_token (parser)->location;
10188 c_parser_consume_pragma (parser);
10189 c_parser_skip_to_pragma_eol (parser);
10191 c_finish_omp_taskwait (loc);
10194 /* OpenMP 3.1:
10195 # pragma omp taskyield new-line
10198 static void
10199 c_parser_omp_taskyield (c_parser *parser)
10201 location_t loc = c_parser_peek_token (parser)->location;
10202 c_parser_consume_pragma (parser);
10203 c_parser_skip_to_pragma_eol (parser);
10205 c_finish_omp_taskyield (loc);
10208 /* Main entry point to parsing most OpenMP pragmas. */
10210 static void
10211 c_parser_omp_construct (c_parser *parser)
10213 enum pragma_kind p_kind;
10214 location_t loc;
10215 tree stmt;
10217 loc = c_parser_peek_token (parser)->location;
10218 p_kind = c_parser_peek_token (parser)->pragma_kind;
10219 c_parser_consume_pragma (parser);
10221 switch (p_kind)
10223 case PRAGMA_OMP_ATOMIC:
10224 c_parser_omp_atomic (loc, parser);
10225 return;
10226 case PRAGMA_OMP_CRITICAL:
10227 stmt = c_parser_omp_critical (loc, parser);
10228 break;
10229 case PRAGMA_OMP_FOR:
10230 stmt = c_parser_omp_for (loc, parser);
10231 break;
10232 case PRAGMA_OMP_MASTER:
10233 stmt = c_parser_omp_master (loc, parser);
10234 break;
10235 case PRAGMA_OMP_ORDERED:
10236 stmt = c_parser_omp_ordered (loc, parser);
10237 break;
10238 case PRAGMA_OMP_PARALLEL:
10239 stmt = c_parser_omp_parallel (loc, parser);
10240 break;
10241 case PRAGMA_OMP_SECTIONS:
10242 stmt = c_parser_omp_sections (loc, parser);
10243 break;
10244 case PRAGMA_OMP_SINGLE:
10245 stmt = c_parser_omp_single (loc, parser);
10246 break;
10247 case PRAGMA_OMP_TASK:
10248 stmt = c_parser_omp_task (loc, parser);
10249 break;
10250 default:
10251 gcc_unreachable ();
10254 if (stmt)
10255 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
10259 /* OpenMP 2.5:
10260 # pragma omp threadprivate (variable-list) */
10262 static void
10263 c_parser_omp_threadprivate (c_parser *parser)
10265 tree vars, t;
10266 location_t loc;
10268 c_parser_consume_pragma (parser);
10269 loc = c_parser_peek_token (parser)->location;
10270 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10272 /* Mark every variable in VARS to be assigned thread local storage. */
10273 for (t = vars; t; t = TREE_CHAIN (t))
10275 tree v = TREE_PURPOSE (t);
10277 /* FIXME diagnostics: Ideally we should keep individual
10278 locations for all the variables in the var list to make the
10279 following errors more precise. Perhaps
10280 c_parser_omp_var_list_parens() should construct a list of
10281 locations to go along with the var list. */
10283 /* If V had already been marked threadprivate, it doesn't matter
10284 whether it had been used prior to this point. */
10285 if (TREE_CODE (v) != VAR_DECL)
10286 error_at (loc, "%qD is not a variable", v);
10287 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
10288 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
10289 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
10290 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
10291 else if (TREE_TYPE (v) == error_mark_node)
10293 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
10294 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
10295 else
10297 if (! DECL_THREAD_LOCAL_P (v))
10299 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
10300 /* If rtl has been already set for this var, call
10301 make_decl_rtl once again, so that encode_section_info
10302 has a chance to look at the new decl flags. */
10303 if (DECL_RTL_SET_P (v))
10304 make_decl_rtl (v);
10306 C_DECL_THREADPRIVATE_P (v) = 1;
10310 c_parser_skip_to_pragma_eol (parser);
10314 /* Parse a single source file. */
10316 void
10317 c_parse_file (void)
10319 /* Use local storage to begin. If the first token is a pragma, parse it.
10320 If it is #pragma GCC pch_preprocess, then this will load a PCH file
10321 which will cause garbage collection. */
10322 c_parser tparser;
10324 memset (&tparser, 0, sizeof tparser);
10325 the_parser = &tparser;
10327 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
10328 c_parser_pragma_pch_preprocess (&tparser);
10330 the_parser = ggc_alloc_c_parser ();
10331 *the_parser = tparser;
10333 /* Initialize EH, if we've been told to do so. */
10334 if (flag_exceptions)
10335 using_eh_for_cleanups ();
10337 c_parser_translation_unit (the_parser);
10338 the_parser = NULL;
10341 #include "gt-c-parser.h"