* cpplib.pot: Regenerate.
[official-gcc.git] / gcc / c-parser.c
blobb4135eedea5d509cbaaac7d0481f3e8ba556c807
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 2012 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 "ggc.h"
53 #include "c-family/c-common.h"
54 #include "c-family/c-objc.h"
55 #include "vec.h"
56 #include "target.h"
57 #include "cgraph.h"
58 #include "plugin.h"
61 /* Initialization routine for this file. */
63 void
64 c_parse_init (void)
66 /* The only initialization required is of the reserved word
67 identifiers. */
68 unsigned int i;
69 tree id;
70 int mask = 0;
72 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
73 the c_token structure. */
74 gcc_assert (RID_MAX <= 255);
76 mask |= D_CXXONLY;
77 if (!flag_isoc99)
78 mask |= D_C99;
79 if (flag_no_asm)
81 mask |= D_ASM | D_EXT;
82 if (!flag_isoc99)
83 mask |= D_EXT89;
85 if (!c_dialect_objc ())
86 mask |= D_OBJC | D_CXX_OBJC;
88 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
89 for (i = 0; i < num_c_common_reswords; i++)
91 /* If a keyword is disabled, do not enter it into the table
92 and so create a canonical spelling that isn't a keyword. */
93 if (c_common_reswords[i].disable & mask)
95 if (warn_cxx_compat
96 && (c_common_reswords[i].disable & D_CXXWARN))
98 id = get_identifier (c_common_reswords[i].word);
99 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
100 C_IS_RESERVED_WORD (id) = 1;
102 continue;
105 id = get_identifier (c_common_reswords[i].word);
106 C_SET_RID_CODE (id, c_common_reswords[i].rid);
107 C_IS_RESERVED_WORD (id) = 1;
108 ridpointers [(int) c_common_reswords[i].rid] = id;
112 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
113 and the C parser. Unlike the C++ lexer, the parser structure
114 stores the lexer information instead of using a separate structure.
115 Identifiers are separated into ordinary identifiers, type names,
116 keywords and some other Objective-C types of identifiers, and some
117 look-ahead is maintained.
119 ??? It might be a good idea to lex the whole file up front (as for
120 C++). It would then be possible to share more of the C and C++
121 lexer code, if desired. */
123 /* The following local token type is used. */
125 /* A keyword. */
126 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
128 /* More information about the type of a CPP_NAME token. */
129 typedef enum c_id_kind {
130 /* An ordinary identifier. */
131 C_ID_ID,
132 /* An identifier declared as a typedef name. */
133 C_ID_TYPENAME,
134 /* An identifier declared as an Objective-C class name. */
135 C_ID_CLASSNAME,
136 /* An address space identifier. */
137 C_ID_ADDRSPACE,
138 /* Not an identifier. */
139 C_ID_NONE
140 } c_id_kind;
142 /* A single C token after string literal concatenation and conversion
143 of preprocessing tokens to tokens. */
144 typedef struct GTY (()) c_token {
145 /* The kind of token. */
146 ENUM_BITFIELD (cpp_ttype) type : 8;
147 /* If this token is a CPP_NAME, this value indicates whether also
148 declared as some kind of type. Otherwise, it is C_ID_NONE. */
149 ENUM_BITFIELD (c_id_kind) id_kind : 8;
150 /* If this token is a keyword, this value indicates which keyword.
151 Otherwise, this value is RID_MAX. */
152 ENUM_BITFIELD (rid) keyword : 8;
153 /* If this token is a CPP_PRAGMA, this indicates the pragma that
154 was seen. Otherwise it is PRAGMA_NONE. */
155 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
156 /* The location at which this token was found. */
157 location_t location;
158 /* The value associated with this token, if any. */
159 tree value;
160 } c_token;
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 typedef struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token tokens[2];
168 /* How many look-ahead tokens are available (0, 1 or 2). */
169 short tokens_avail;
170 /* True if a syntax error is being recovered from; false otherwise.
171 c_parser_error sets this flag. It should clear this flag when
172 enough tokens have been consumed to recover from the error. */
173 BOOL_BITFIELD error : 1;
174 /* True if we're processing a pragma, and shouldn't automatically
175 consume CPP_PRAGMA_EOL. */
176 BOOL_BITFIELD in_pragma : 1;
177 /* True if we're parsing the outermost block of an if statement. */
178 BOOL_BITFIELD in_if_block : 1;
179 /* True if we want to lex an untranslated string. */
180 BOOL_BITFIELD lex_untranslated_string : 1;
182 /* Objective-C specific parser/lexer information. */
184 /* True if we are in a context where the Objective-C "PQ" keywords
185 are considered keywords. */
186 BOOL_BITFIELD objc_pq_context : 1;
187 /* True if we are parsing a (potential) Objective-C foreach
188 statement. This is set to true after we parsed 'for (' and while
189 we wait for 'in' or ';' to decide if it's a standard C for loop or an
190 Objective-C foreach loop. */
191 BOOL_BITFIELD objc_could_be_foreach_context : 1;
192 /* The following flag is needed to contextualize Objective-C lexical
193 analysis. In some cases (e.g., 'int NSObject;'), it is
194 undesirable to bind an identifier to an Objective-C class, even
195 if a class with that name exists. */
196 BOOL_BITFIELD objc_need_raw_identifier : 1;
197 /* Nonzero if we're processing a __transaction statement. The value
198 is 1 | TM_STMT_ATTR_*. */
199 unsigned int in_transaction : 4;
200 /* True if we are in a context where the Objective-C "Property attribute"
201 keywords are valid. */
202 BOOL_BITFIELD objc_property_attr_context : 1;
203 } c_parser;
206 /* The actual parser and external interface. ??? Does this need to be
207 garbage-collected? */
209 static GTY (()) c_parser *the_parser;
211 /* Read in and lex a single token, storing it in *TOKEN. */
213 static void
214 c_lex_one_token (c_parser *parser, c_token *token)
216 timevar_push (TV_LEX);
218 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
219 (parser->lex_untranslated_string
220 ? C_LEX_STRING_NO_TRANSLATE : 0));
221 token->id_kind = C_ID_NONE;
222 token->keyword = RID_MAX;
223 token->pragma_kind = PRAGMA_NONE;
225 switch (token->type)
227 case CPP_NAME:
229 tree decl;
231 bool objc_force_identifier = parser->objc_need_raw_identifier;
232 if (c_dialect_objc ())
233 parser->objc_need_raw_identifier = false;
235 if (C_IS_RESERVED_WORD (token->value))
237 enum rid rid_code = C_RID_CODE (token->value);
239 if (rid_code == RID_CXX_COMPAT_WARN)
241 warning_at (token->location,
242 OPT_Wc___compat,
243 "identifier %qE conflicts with C++ keyword",
244 token->value);
246 else if (rid_code >= RID_FIRST_ADDR_SPACE
247 && rid_code <= RID_LAST_ADDR_SPACE)
249 token->id_kind = C_ID_ADDRSPACE;
250 token->keyword = rid_code;
251 break;
253 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
255 /* We found an Objective-C "pq" keyword (in, out,
256 inout, bycopy, byref, oneway). They need special
257 care because the interpretation depends on the
258 context. */
259 if (parser->objc_pq_context)
261 token->type = CPP_KEYWORD;
262 token->keyword = rid_code;
263 break;
265 else if (parser->objc_could_be_foreach_context
266 && rid_code == RID_IN)
268 /* We are in Objective-C, inside a (potential)
269 foreach context (which means after having
270 parsed 'for (', but before having parsed ';'),
271 and we found 'in'. We consider it the keyword
272 which terminates the declaration at the
273 beginning of a foreach-statement. Note that
274 this means you can't use 'in' for anything else
275 in that context; in particular, in Objective-C
276 you can't use 'in' as the name of the running
277 variable in a C for loop. We could potentially
278 try to add code here to disambiguate, but it
279 seems a reasonable limitation. */
280 token->type = CPP_KEYWORD;
281 token->keyword = rid_code;
282 break;
284 /* Else, "pq" keywords outside of the "pq" context are
285 not keywords, and we fall through to the code for
286 normal tokens. */
288 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
290 /* We found an Objective-C "property attribute"
291 keyword (getter, setter, readonly, etc). These are
292 only valid in the property context. */
293 if (parser->objc_property_attr_context)
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 /* Else they are not special keywords.
302 else if (c_dialect_objc ()
303 && (OBJC_IS_AT_KEYWORD (rid_code)
304 || OBJC_IS_CXX_KEYWORD (rid_code)))
306 /* We found one of the Objective-C "@" keywords (defs,
307 selector, synchronized, etc) or one of the
308 Objective-C "cxx" keywords (class, private,
309 protected, public, try, catch, throw) without a
310 preceding '@' sign. Do nothing and fall through to
311 the code for normal tokens (in C++ we would still
312 consider the CXX ones keywords, but not in C). */
315 else
317 token->type = CPP_KEYWORD;
318 token->keyword = rid_code;
319 break;
323 decl = lookup_name (token->value);
324 if (decl)
326 if (TREE_CODE (decl) == TYPE_DECL)
328 token->id_kind = C_ID_TYPENAME;
329 break;
332 else if (c_dialect_objc ())
334 tree objc_interface_decl = objc_is_class_name (token->value);
335 /* Objective-C class names are in the same namespace as
336 variables and typedefs, and hence are shadowed by local
337 declarations. */
338 if (objc_interface_decl
339 && (!objc_force_identifier || global_bindings_p ()))
341 token->value = objc_interface_decl;
342 token->id_kind = C_ID_CLASSNAME;
343 break;
346 token->id_kind = C_ID_ID;
348 break;
349 case CPP_AT_NAME:
350 /* This only happens in Objective-C; it must be a keyword. */
351 token->type = CPP_KEYWORD;
352 switch (C_RID_CODE (token->value))
354 /* Replace 'class' with '@class', 'private' with '@private',
355 etc. This prevents confusion with the C++ keyword
356 'class', and makes the tokens consistent with other
357 Objective-C 'AT' keywords. For example '@class' is
358 reported as RID_AT_CLASS which is consistent with
359 '@synchronized', which is reported as
360 RID_AT_SYNCHRONIZED.
362 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
363 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
364 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
365 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
366 case RID_THROW: token->keyword = RID_AT_THROW; break;
367 case RID_TRY: token->keyword = RID_AT_TRY; break;
368 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
369 default: token->keyword = C_RID_CODE (token->value);
371 break;
372 case CPP_COLON:
373 case CPP_COMMA:
374 case CPP_CLOSE_PAREN:
375 case CPP_SEMICOLON:
376 /* These tokens may affect the interpretation of any identifiers
377 following, if doing Objective-C. */
378 if (c_dialect_objc ())
379 parser->objc_need_raw_identifier = false;
380 break;
381 case CPP_PRAGMA:
382 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
383 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
384 token->value = NULL;
385 break;
386 default:
387 break;
389 timevar_pop (TV_LEX);
392 /* Return a pointer to the next token from PARSER, reading it in if
393 necessary. */
395 static inline c_token *
396 c_parser_peek_token (c_parser *parser)
398 if (parser->tokens_avail == 0)
400 c_lex_one_token (parser, &parser->tokens[0]);
401 parser->tokens_avail = 1;
403 return &parser->tokens[0];
406 /* Return true if the next token from PARSER has the indicated
407 TYPE. */
409 static inline bool
410 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
412 return c_parser_peek_token (parser)->type == type;
415 /* Return true if the next token from PARSER does not have the
416 indicated TYPE. */
418 static inline bool
419 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
421 return !c_parser_next_token_is (parser, type);
424 /* Return true if the next token from PARSER is the indicated
425 KEYWORD. */
427 static inline bool
428 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
430 return c_parser_peek_token (parser)->keyword == keyword;
433 /* Return a pointer to the next-but-one token from PARSER, reading it
434 in if necessary. The next token is already read in. */
436 static c_token *
437 c_parser_peek_2nd_token (c_parser *parser)
439 if (parser->tokens_avail >= 2)
440 return &parser->tokens[1];
441 gcc_assert (parser->tokens_avail == 1);
442 gcc_assert (parser->tokens[0].type != CPP_EOF);
443 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
444 c_lex_one_token (parser, &parser->tokens[1]);
445 parser->tokens_avail = 2;
446 return &parser->tokens[1];
449 /* Return true if TOKEN can start a type name,
450 false otherwise. */
451 static bool
452 c_token_starts_typename (c_token *token)
454 switch (token->type)
456 case CPP_NAME:
457 switch (token->id_kind)
459 case C_ID_ID:
460 return false;
461 case C_ID_ADDRSPACE:
462 return true;
463 case C_ID_TYPENAME:
464 return true;
465 case C_ID_CLASSNAME:
466 gcc_assert (c_dialect_objc ());
467 return true;
468 default:
469 gcc_unreachable ();
471 case CPP_KEYWORD:
472 switch (token->keyword)
474 case RID_UNSIGNED:
475 case RID_LONG:
476 case RID_INT128:
477 case RID_SHORT:
478 case RID_SIGNED:
479 case RID_COMPLEX:
480 case RID_INT:
481 case RID_CHAR:
482 case RID_FLOAT:
483 case RID_DOUBLE:
484 case RID_VOID:
485 case RID_DFLOAT32:
486 case RID_DFLOAT64:
487 case RID_DFLOAT128:
488 case RID_BOOL:
489 case RID_ENUM:
490 case RID_STRUCT:
491 case RID_UNION:
492 case RID_TYPEOF:
493 case RID_CONST:
494 case RID_VOLATILE:
495 case RID_RESTRICT:
496 case RID_ATTRIBUTE:
497 case RID_FRACT:
498 case RID_ACCUM:
499 case RID_SAT:
500 return true;
501 default:
502 return false;
504 case CPP_LESS:
505 if (c_dialect_objc ())
506 return true;
507 return false;
508 default:
509 return false;
513 enum c_lookahead_kind {
514 /* Always treat unknown identifiers as typenames. */
515 cla_prefer_type,
517 /* Could be parsing a nonabstract declarator. Only treat an identifier
518 as a typename if followed by another identifier or a star. */
519 cla_nonabstract_decl,
521 /* Never treat identifiers as typenames. */
522 cla_prefer_id
525 /* Return true if the next token from PARSER can start a type name,
526 false otherwise. LA specifies how to do lookahead in order to
527 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
529 static inline bool
530 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
532 c_token *token = c_parser_peek_token (parser);
533 if (c_token_starts_typename (token))
534 return true;
536 /* Try a bit harder to detect an unknown typename. */
537 if (la != cla_prefer_id
538 && token->type == CPP_NAME
539 && token->id_kind == C_ID_ID
541 /* Do not try too hard when we could have "object in array". */
542 && !parser->objc_could_be_foreach_context
544 && (la == cla_prefer_type
545 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
546 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
548 /* Only unknown identifiers. */
549 && !lookup_name (token->value))
550 return true;
552 return false;
555 /* Return true if TOKEN is a type qualifier, false otherwise. */
556 static bool
557 c_token_is_qualifier (c_token *token)
559 switch (token->type)
561 case CPP_NAME:
562 switch (token->id_kind)
564 case C_ID_ADDRSPACE:
565 return true;
566 default:
567 return false;
569 case CPP_KEYWORD:
570 switch (token->keyword)
572 case RID_CONST:
573 case RID_VOLATILE:
574 case RID_RESTRICT:
575 case RID_ATTRIBUTE:
576 return true;
577 default:
578 return false;
580 case CPP_LESS:
581 return false;
582 default:
583 gcc_unreachable ();
587 /* Return true if the next token from PARSER is a type qualifier,
588 false otherwise. */
589 static inline bool
590 c_parser_next_token_is_qualifier (c_parser *parser)
592 c_token *token = c_parser_peek_token (parser);
593 return c_token_is_qualifier (token);
596 /* Return true if TOKEN can start declaration specifiers, false
597 otherwise. */
598 static bool
599 c_token_starts_declspecs (c_token *token)
601 switch (token->type)
603 case CPP_NAME:
604 switch (token->id_kind)
606 case C_ID_ID:
607 return false;
608 case C_ID_ADDRSPACE:
609 return true;
610 case C_ID_TYPENAME:
611 return true;
612 case C_ID_CLASSNAME:
613 gcc_assert (c_dialect_objc ());
614 return true;
615 default:
616 gcc_unreachable ();
618 case CPP_KEYWORD:
619 switch (token->keyword)
621 case RID_STATIC:
622 case RID_EXTERN:
623 case RID_REGISTER:
624 case RID_TYPEDEF:
625 case RID_INLINE:
626 case RID_NORETURN:
627 case RID_AUTO:
628 case RID_THREAD:
629 case RID_UNSIGNED:
630 case RID_LONG:
631 case RID_INT128:
632 case RID_SHORT:
633 case RID_SIGNED:
634 case RID_COMPLEX:
635 case RID_INT:
636 case RID_CHAR:
637 case RID_FLOAT:
638 case RID_DOUBLE:
639 case RID_VOID:
640 case RID_DFLOAT32:
641 case RID_DFLOAT64:
642 case RID_DFLOAT128:
643 case RID_BOOL:
644 case RID_ENUM:
645 case RID_STRUCT:
646 case RID_UNION:
647 case RID_TYPEOF:
648 case RID_CONST:
649 case RID_VOLATILE:
650 case RID_RESTRICT:
651 case RID_ATTRIBUTE:
652 case RID_FRACT:
653 case RID_ACCUM:
654 case RID_SAT:
655 case RID_ALIGNAS:
656 return true;
657 default:
658 return false;
660 case CPP_LESS:
661 if (c_dialect_objc ())
662 return true;
663 return false;
664 default:
665 return false;
670 /* Return true if TOKEN can start declaration specifiers or a static
671 assertion, false otherwise. */
672 static bool
673 c_token_starts_declaration (c_token *token)
675 if (c_token_starts_declspecs (token)
676 || token->keyword == RID_STATIC_ASSERT)
677 return true;
678 else
679 return false;
682 /* Return true if the next token from PARSER can start declaration
683 specifiers, false otherwise. */
684 static inline bool
685 c_parser_next_token_starts_declspecs (c_parser *parser)
687 c_token *token = c_parser_peek_token (parser);
689 /* In Objective-C, a classname normally starts a declspecs unless it
690 is immediately followed by a dot. In that case, it is the
691 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
692 setter/getter on the class. c_token_starts_declspecs() can't
693 differentiate between the two cases because it only checks the
694 current token, so we have a special check here. */
695 if (c_dialect_objc ()
696 && token->type == CPP_NAME
697 && token->id_kind == C_ID_CLASSNAME
698 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
699 return false;
701 return c_token_starts_declspecs (token);
704 /* Return true if the next tokens from PARSER can start declaration
705 specifiers or a static assertion, false otherwise. */
706 static inline bool
707 c_parser_next_tokens_start_declaration (c_parser *parser)
709 c_token *token = c_parser_peek_token (parser);
711 /* Same as above. */
712 if (c_dialect_objc ()
713 && token->type == CPP_NAME
714 && token->id_kind == C_ID_CLASSNAME
715 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
716 return false;
718 /* Labels do not start declarations. */
719 if (token->type == CPP_NAME
720 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
721 return false;
723 if (c_token_starts_declaration (token))
724 return true;
726 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
727 return true;
729 return false;
732 /* Consume the next token from PARSER. */
734 static void
735 c_parser_consume_token (c_parser *parser)
737 gcc_assert (parser->tokens_avail >= 1);
738 gcc_assert (parser->tokens[0].type != CPP_EOF);
739 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
740 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
741 if (parser->tokens_avail == 2)
742 parser->tokens[0] = parser->tokens[1];
743 parser->tokens_avail--;
746 /* Expect the current token to be a #pragma. Consume it and remember
747 that we've begun parsing a pragma. */
749 static void
750 c_parser_consume_pragma (c_parser *parser)
752 gcc_assert (!parser->in_pragma);
753 gcc_assert (parser->tokens_avail >= 1);
754 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
755 if (parser->tokens_avail == 2)
756 parser->tokens[0] = parser->tokens[1];
757 parser->tokens_avail--;
758 parser->in_pragma = true;
761 /* Update the globals input_location and in_system_header from
762 TOKEN. */
763 static inline void
764 c_parser_set_source_position_from_token (c_token *token)
766 if (token->type != CPP_EOF)
768 input_location = token->location;
772 /* Issue a diagnostic of the form
773 FILE:LINE: MESSAGE before TOKEN
774 where TOKEN is the next token in the input stream of PARSER.
775 MESSAGE (specified by the caller) is usually of the form "expected
776 OTHER-TOKEN".
778 Do not issue a diagnostic if still recovering from an error.
780 ??? This is taken from the C++ parser, but building up messages in
781 this way is not i18n-friendly and some other approach should be
782 used. */
784 static void
785 c_parser_error (c_parser *parser, const char *gmsgid)
787 c_token *token = c_parser_peek_token (parser);
788 if (parser->error)
789 return;
790 parser->error = true;
791 if (!gmsgid)
792 return;
793 /* This diagnostic makes more sense if it is tagged to the line of
794 the token we just peeked at. */
795 c_parser_set_source_position_from_token (token);
796 c_parse_error (gmsgid,
797 /* Because c_parse_error does not understand
798 CPP_KEYWORD, keywords are treated like
799 identifiers. */
800 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
801 /* ??? The C parser does not save the cpp flags of a
802 token, we need to pass 0 here and we will not get
803 the source spelling of some tokens but rather the
804 canonical spelling. */
805 token->value, /*flags=*/0);
808 /* If the next token is of the indicated TYPE, consume it. Otherwise,
809 issue the error MSGID. If MSGID is NULL then a message has already
810 been produced and no message will be produced this time. Returns
811 true if found, false otherwise. */
813 static bool
814 c_parser_require (c_parser *parser,
815 enum cpp_ttype type,
816 const char *msgid)
818 if (c_parser_next_token_is (parser, type))
820 c_parser_consume_token (parser);
821 return true;
823 else
825 c_parser_error (parser, msgid);
826 return false;
830 /* If the next token is the indicated keyword, consume it. Otherwise,
831 issue the error MSGID. Returns true if found, false otherwise. */
833 static bool
834 c_parser_require_keyword (c_parser *parser,
835 enum rid keyword,
836 const char *msgid)
838 if (c_parser_next_token_is_keyword (parser, keyword))
840 c_parser_consume_token (parser);
841 return true;
843 else
845 c_parser_error (parser, msgid);
846 return false;
850 /* Like c_parser_require, except that tokens will be skipped until the
851 desired token is found. An error message is still produced if the
852 next token is not as expected. If MSGID is NULL then a message has
853 already been produced and no message will be produced this
854 time. */
856 static void
857 c_parser_skip_until_found (c_parser *parser,
858 enum cpp_ttype type,
859 const char *msgid)
861 unsigned nesting_depth = 0;
863 if (c_parser_require (parser, type, msgid))
864 return;
866 /* Skip tokens until the desired token is found. */
867 while (true)
869 /* Peek at the next token. */
870 c_token *token = c_parser_peek_token (parser);
871 /* If we've reached the token we want, consume it and stop. */
872 if (token->type == type && !nesting_depth)
874 c_parser_consume_token (parser);
875 break;
878 /* If we've run out of tokens, stop. */
879 if (token->type == CPP_EOF)
880 return;
881 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
882 return;
883 if (token->type == CPP_OPEN_BRACE
884 || token->type == CPP_OPEN_PAREN
885 || token->type == CPP_OPEN_SQUARE)
886 ++nesting_depth;
887 else if (token->type == CPP_CLOSE_BRACE
888 || token->type == CPP_CLOSE_PAREN
889 || token->type == CPP_CLOSE_SQUARE)
891 if (nesting_depth-- == 0)
892 break;
894 /* Consume this token. */
895 c_parser_consume_token (parser);
897 parser->error = false;
900 /* Skip tokens until the end of a parameter is found, but do not
901 consume the comma, semicolon or closing delimiter. */
903 static void
904 c_parser_skip_to_end_of_parameter (c_parser *parser)
906 unsigned nesting_depth = 0;
908 while (true)
910 c_token *token = c_parser_peek_token (parser);
911 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
912 && !nesting_depth)
913 break;
914 /* If we've run out of tokens, stop. */
915 if (token->type == CPP_EOF)
916 return;
917 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
918 return;
919 if (token->type == CPP_OPEN_BRACE
920 || token->type == CPP_OPEN_PAREN
921 || token->type == CPP_OPEN_SQUARE)
922 ++nesting_depth;
923 else if (token->type == CPP_CLOSE_BRACE
924 || token->type == CPP_CLOSE_PAREN
925 || token->type == CPP_CLOSE_SQUARE)
927 if (nesting_depth-- == 0)
928 break;
930 /* Consume this token. */
931 c_parser_consume_token (parser);
933 parser->error = false;
936 /* Expect to be at the end of the pragma directive and consume an
937 end of line marker. */
939 static void
940 c_parser_skip_to_pragma_eol (c_parser *parser)
942 gcc_assert (parser->in_pragma);
943 parser->in_pragma = false;
945 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
946 while (true)
948 c_token *token = c_parser_peek_token (parser);
949 if (token->type == CPP_EOF)
950 break;
951 if (token->type == CPP_PRAGMA_EOL)
953 c_parser_consume_token (parser);
954 break;
956 c_parser_consume_token (parser);
959 parser->error = false;
962 /* Skip tokens until we have consumed an entire block, or until we
963 have consumed a non-nested ';'. */
965 static void
966 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
968 unsigned nesting_depth = 0;
969 bool save_error = parser->error;
971 while (true)
973 c_token *token;
975 /* Peek at the next token. */
976 token = c_parser_peek_token (parser);
978 switch (token->type)
980 case CPP_EOF:
981 return;
983 case CPP_PRAGMA_EOL:
984 if (parser->in_pragma)
985 return;
986 break;
988 case CPP_SEMICOLON:
989 /* If the next token is a ';', we have reached the
990 end of the statement. */
991 if (!nesting_depth)
993 /* Consume the ';'. */
994 c_parser_consume_token (parser);
995 goto finished;
997 break;
999 case CPP_CLOSE_BRACE:
1000 /* If the next token is a non-nested '}', then we have
1001 reached the end of the current block. */
1002 if (nesting_depth == 0 || --nesting_depth == 0)
1004 c_parser_consume_token (parser);
1005 goto finished;
1007 break;
1009 case CPP_OPEN_BRACE:
1010 /* If it the next token is a '{', then we are entering a new
1011 block. Consume the entire block. */
1012 ++nesting_depth;
1013 break;
1015 case CPP_PRAGMA:
1016 /* If we see a pragma, consume the whole thing at once. We
1017 have some safeguards against consuming pragmas willy-nilly.
1018 Normally, we'd expect to be here with parser->error set,
1019 which disables these safeguards. But it's possible to get
1020 here for secondary error recovery, after parser->error has
1021 been cleared. */
1022 c_parser_consume_pragma (parser);
1023 c_parser_skip_to_pragma_eol (parser);
1024 parser->error = save_error;
1025 continue;
1027 default:
1028 break;
1031 c_parser_consume_token (parser);
1034 finished:
1035 parser->error = false;
1038 /* CPP's options (initialized by c-opts.c). */
1039 extern cpp_options *cpp_opts;
1041 /* Save the warning flags which are controlled by __extension__. */
1043 static inline int
1044 disable_extension_diagnostics (void)
1046 int ret = (pedantic
1047 | (warn_pointer_arith << 1)
1048 | (warn_traditional << 2)
1049 | (flag_iso << 3)
1050 | (warn_long_long << 4)
1051 | (warn_cxx_compat << 5)
1052 | (warn_overlength_strings << 6));
1053 cpp_opts->cpp_pedantic = pedantic = 0;
1054 warn_pointer_arith = 0;
1055 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1056 flag_iso = 0;
1057 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1058 warn_cxx_compat = 0;
1059 warn_overlength_strings = 0;
1060 return ret;
1063 /* Restore the warning flags which are controlled by __extension__.
1064 FLAGS is the return value from disable_extension_diagnostics. */
1066 static inline void
1067 restore_extension_diagnostics (int flags)
1069 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1070 warn_pointer_arith = (flags >> 1) & 1;
1071 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1072 flag_iso = (flags >> 3) & 1;
1073 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1074 warn_cxx_compat = (flags >> 5) & 1;
1075 warn_overlength_strings = (flags >> 6) & 1;
1078 /* Possibly kinds of declarator to parse. */
1079 typedef enum c_dtr_syn {
1080 /* A normal declarator with an identifier. */
1081 C_DTR_NORMAL,
1082 /* An abstract declarator (maybe empty). */
1083 C_DTR_ABSTRACT,
1084 /* A parameter declarator: may be either, but after a type name does
1085 not redeclare a typedef name as an identifier if it can
1086 alternatively be interpreted as a typedef name; see DR#009,
1087 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1088 following DR#249. For example, given a typedef T, "int T" and
1089 "int *T" are valid parameter declarations redeclaring T, while
1090 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1091 abstract declarators rather than involving redundant parentheses;
1092 the same applies with attributes inside the parentheses before
1093 "T". */
1094 C_DTR_PARM
1095 } c_dtr_syn;
1097 /* The binary operation precedence levels, where 0 is a dummy lowest level
1098 used for the bottom of the stack. */
1099 enum c_parser_prec {
1100 PREC_NONE,
1101 PREC_LOGOR,
1102 PREC_LOGAND,
1103 PREC_BITOR,
1104 PREC_BITXOR,
1105 PREC_BITAND,
1106 PREC_EQ,
1107 PREC_REL,
1108 PREC_SHIFT,
1109 PREC_ADD,
1110 PREC_MULT,
1111 NUM_PRECS
1114 static void c_parser_external_declaration (c_parser *);
1115 static void c_parser_asm_definition (c_parser *);
1116 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1117 bool, bool, tree *);
1118 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1119 static void c_parser_static_assert_declaration (c_parser *);
1120 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1121 bool, enum c_lookahead_kind);
1122 static struct c_typespec c_parser_enum_specifier (c_parser *);
1123 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1124 static tree c_parser_struct_declaration (c_parser *);
1125 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1126 static tree c_parser_alignas_specifier (c_parser *);
1127 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1128 bool *);
1129 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1130 c_dtr_syn, bool *);
1131 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1132 bool,
1133 struct c_declarator *);
1134 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1135 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1136 tree);
1137 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1138 static tree c_parser_simple_asm_expr (c_parser *);
1139 static tree c_parser_attributes (c_parser *);
1140 static struct c_type_name *c_parser_type_name (c_parser *);
1141 static struct c_expr c_parser_initializer (c_parser *);
1142 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1143 static void c_parser_initelt (c_parser *, struct obstack *);
1144 static void c_parser_initval (c_parser *, struct c_expr *,
1145 struct obstack *);
1146 static tree c_parser_compound_statement (c_parser *);
1147 static void c_parser_compound_statement_nostart (c_parser *);
1148 static void c_parser_label (c_parser *);
1149 static void c_parser_statement (c_parser *);
1150 static void c_parser_statement_after_labels (c_parser *);
1151 static void c_parser_if_statement (c_parser *);
1152 static void c_parser_switch_statement (c_parser *);
1153 static void c_parser_while_statement (c_parser *);
1154 static void c_parser_do_statement (c_parser *);
1155 static void c_parser_for_statement (c_parser *);
1156 static tree c_parser_asm_statement (c_parser *);
1157 static tree c_parser_asm_operands (c_parser *, bool);
1158 static tree c_parser_asm_goto_operands (c_parser *);
1159 static tree c_parser_asm_clobbers (c_parser *);
1160 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *);
1161 static struct c_expr c_parser_conditional_expression (c_parser *,
1162 struct c_expr *);
1163 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1164 enum c_parser_prec);
1165 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1166 static struct c_expr c_parser_unary_expression (c_parser *);
1167 static struct c_expr c_parser_sizeof_expression (c_parser *);
1168 static struct c_expr c_parser_alignof_expression (c_parser *);
1169 static struct c_expr c_parser_postfix_expression (c_parser *);
1170 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1171 struct c_type_name *,
1172 location_t);
1173 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1174 location_t loc,
1175 struct c_expr);
1176 static tree c_parser_transaction (c_parser *, enum rid);
1177 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1178 static tree c_parser_transaction_cancel (c_parser *);
1179 static struct c_expr c_parser_expression (c_parser *);
1180 static struct c_expr c_parser_expression_conv (c_parser *);
1181 static VEC(tree,gc) *c_parser_expr_list (c_parser *, bool, bool,
1182 VEC(tree,gc) **);
1183 static void c_parser_omp_construct (c_parser *);
1184 static void c_parser_omp_threadprivate (c_parser *);
1185 static void c_parser_omp_barrier (c_parser *);
1186 static void c_parser_omp_flush (c_parser *);
1187 static void c_parser_omp_taskwait (c_parser *);
1188 static void c_parser_omp_taskyield (c_parser *);
1190 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1191 static bool c_parser_pragma (c_parser *, enum pragma_context);
1193 /* These Objective-C parser functions are only ever called when
1194 compiling Objective-C. */
1195 static void c_parser_objc_class_definition (c_parser *, tree);
1196 static void c_parser_objc_class_instance_variables (c_parser *);
1197 static void c_parser_objc_class_declaration (c_parser *);
1198 static void c_parser_objc_alias_declaration (c_parser *);
1199 static void c_parser_objc_protocol_definition (c_parser *, tree);
1200 static bool c_parser_objc_method_type (c_parser *);
1201 static void c_parser_objc_method_definition (c_parser *);
1202 static void c_parser_objc_methodprotolist (c_parser *);
1203 static void c_parser_objc_methodproto (c_parser *);
1204 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1205 static tree c_parser_objc_type_name (c_parser *);
1206 static tree c_parser_objc_protocol_refs (c_parser *);
1207 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1208 static void c_parser_objc_synchronized_statement (c_parser *);
1209 static tree c_parser_objc_selector (c_parser *);
1210 static tree c_parser_objc_selector_arg (c_parser *);
1211 static tree c_parser_objc_receiver (c_parser *);
1212 static tree c_parser_objc_message_args (c_parser *);
1213 static tree c_parser_objc_keywordexpr (c_parser *);
1214 static void c_parser_objc_at_property_declaration (c_parser *);
1215 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1216 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1217 static bool c_parser_objc_diagnose_bad_element_prefix
1218 (c_parser *, struct c_declspecs *);
1220 /* Parse a translation unit (C90 6.7, C99 6.9).
1222 translation-unit:
1223 external-declarations
1225 external-declarations:
1226 external-declaration
1227 external-declarations external-declaration
1229 GNU extensions:
1231 translation-unit:
1232 empty
1235 static void
1236 c_parser_translation_unit (c_parser *parser)
1238 if (c_parser_next_token_is (parser, CPP_EOF))
1240 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1241 "ISO C forbids an empty translation unit");
1243 else
1245 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1246 mark_valid_location_for_stdc_pragma (false);
1249 ggc_collect ();
1250 c_parser_external_declaration (parser);
1251 obstack_free (&parser_obstack, obstack_position);
1253 while (c_parser_next_token_is_not (parser, CPP_EOF));
1257 /* Parse an external declaration (C90 6.7, C99 6.9).
1259 external-declaration:
1260 function-definition
1261 declaration
1263 GNU extensions:
1265 external-declaration:
1266 asm-definition
1268 __extension__ external-declaration
1270 Objective-C:
1272 external-declaration:
1273 objc-class-definition
1274 objc-class-declaration
1275 objc-alias-declaration
1276 objc-protocol-definition
1277 objc-method-definition
1278 @end
1281 static void
1282 c_parser_external_declaration (c_parser *parser)
1284 int ext;
1285 switch (c_parser_peek_token (parser)->type)
1287 case CPP_KEYWORD:
1288 switch (c_parser_peek_token (parser)->keyword)
1290 case RID_EXTENSION:
1291 ext = disable_extension_diagnostics ();
1292 c_parser_consume_token (parser);
1293 c_parser_external_declaration (parser);
1294 restore_extension_diagnostics (ext);
1295 break;
1296 case RID_ASM:
1297 c_parser_asm_definition (parser);
1298 break;
1299 case RID_AT_INTERFACE:
1300 case RID_AT_IMPLEMENTATION:
1301 gcc_assert (c_dialect_objc ());
1302 c_parser_objc_class_definition (parser, NULL_TREE);
1303 break;
1304 case RID_AT_CLASS:
1305 gcc_assert (c_dialect_objc ());
1306 c_parser_objc_class_declaration (parser);
1307 break;
1308 case RID_AT_ALIAS:
1309 gcc_assert (c_dialect_objc ());
1310 c_parser_objc_alias_declaration (parser);
1311 break;
1312 case RID_AT_PROTOCOL:
1313 gcc_assert (c_dialect_objc ());
1314 c_parser_objc_protocol_definition (parser, NULL_TREE);
1315 break;
1316 case RID_AT_PROPERTY:
1317 gcc_assert (c_dialect_objc ());
1318 c_parser_objc_at_property_declaration (parser);
1319 break;
1320 case RID_AT_SYNTHESIZE:
1321 gcc_assert (c_dialect_objc ());
1322 c_parser_objc_at_synthesize_declaration (parser);
1323 break;
1324 case RID_AT_DYNAMIC:
1325 gcc_assert (c_dialect_objc ());
1326 c_parser_objc_at_dynamic_declaration (parser);
1327 break;
1328 case RID_AT_END:
1329 gcc_assert (c_dialect_objc ());
1330 c_parser_consume_token (parser);
1331 objc_finish_implementation ();
1332 break;
1333 default:
1334 goto decl_or_fndef;
1336 break;
1337 case CPP_SEMICOLON:
1338 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1339 "ISO C does not allow extra %<;%> outside of a function");
1340 c_parser_consume_token (parser);
1341 break;
1342 case CPP_PRAGMA:
1343 mark_valid_location_for_stdc_pragma (true);
1344 c_parser_pragma (parser, pragma_external);
1345 mark_valid_location_for_stdc_pragma (false);
1346 break;
1347 case CPP_PLUS:
1348 case CPP_MINUS:
1349 if (c_dialect_objc ())
1351 c_parser_objc_method_definition (parser);
1352 break;
1354 /* Else fall through, and yield a syntax error trying to parse
1355 as a declaration or function definition. */
1356 default:
1357 decl_or_fndef:
1358 /* A declaration or a function definition (or, in Objective-C,
1359 an @interface or @protocol with prefix attributes). We can
1360 only tell which after parsing the declaration specifiers, if
1361 any, and the first declarator. */
1362 c_parser_declaration_or_fndef (parser, true, true, true, false, true, NULL);
1363 break;
1367 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1368 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1369 accepted; otherwise (old-style parameter declarations) only other
1370 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1371 assertion is accepted; otherwise (old-style parameter declarations)
1372 it is not. If NESTED is true, we are inside a function or parsing
1373 old-style parameter declarations; any functions encountered are
1374 nested functions and declaration specifiers are required; otherwise
1375 we are at top level and functions are normal functions and
1376 declaration specifiers may be optional. If EMPTY_OK is true, empty
1377 declarations are OK (subject to all other constraints); otherwise
1378 (old-style parameter declarations) they are diagnosed. If
1379 START_ATTR_OK is true, the declaration specifiers may start with
1380 attributes; otherwise they may not.
1381 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1382 declaration when parsing an Objective-C foreach statement.
1384 declaration:
1385 declaration-specifiers init-declarator-list[opt] ;
1386 static_assert-declaration
1388 function-definition:
1389 declaration-specifiers[opt] declarator declaration-list[opt]
1390 compound-statement
1392 declaration-list:
1393 declaration
1394 declaration-list declaration
1396 init-declarator-list:
1397 init-declarator
1398 init-declarator-list , init-declarator
1400 init-declarator:
1401 declarator simple-asm-expr[opt] attributes[opt]
1402 declarator simple-asm-expr[opt] attributes[opt] = initializer
1404 GNU extensions:
1406 nested-function-definition:
1407 declaration-specifiers declarator declaration-list[opt]
1408 compound-statement
1410 Objective-C:
1411 attributes objc-class-definition
1412 attributes objc-category-definition
1413 attributes objc-protocol-definition
1415 The simple-asm-expr and attributes are GNU extensions.
1417 This function does not handle __extension__; that is handled in its
1418 callers. ??? Following the old parser, __extension__ may start
1419 external declarations, declarations in functions and declarations
1420 at the start of "for" loops, but not old-style parameter
1421 declarations.
1423 C99 requires declaration specifiers in a function definition; the
1424 absence is diagnosed through the diagnosis of implicit int. In GNU
1425 C we also allow but diagnose declarations without declaration
1426 specifiers, but only at top level (elsewhere they conflict with
1427 other syntax).
1429 In Objective-C, declarations of the looping variable in a foreach
1430 statement are exceptionally terminated by 'in' (for example, 'for
1431 (NSObject *object in array) { ... }').
1433 OpenMP:
1435 declaration:
1436 threadprivate-directive */
1438 static void
1439 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1440 bool static_assert_ok, bool empty_ok,
1441 bool nested, bool start_attr_ok,
1442 tree *objc_foreach_object_declaration)
1444 struct c_declspecs *specs;
1445 tree prefix_attrs;
1446 tree all_prefix_attrs;
1447 bool diagnosed_no_specs = false;
1448 location_t here = c_parser_peek_token (parser)->location;
1450 if (static_assert_ok
1451 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1453 c_parser_static_assert_declaration (parser);
1454 return;
1456 specs = build_null_declspecs ();
1458 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1459 if (c_parser_peek_token (parser)->type == CPP_NAME
1460 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1461 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1462 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1463 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1465 error_at (here, "unknown type name %qE",
1466 c_parser_peek_token (parser)->value);
1468 /* Parse declspecs normally to get a correct pointer type, but avoid
1469 a further "fails to be a type name" error. Refuse nested functions
1470 since it is not how the user likely wants us to recover. */
1471 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1472 c_parser_peek_token (parser)->keyword = RID_VOID;
1473 c_parser_peek_token (parser)->value = error_mark_node;
1474 fndef_ok = !nested;
1477 c_parser_declspecs (parser, specs, true, true, start_attr_ok, cla_nonabstract_decl);
1478 if (parser->error)
1480 c_parser_skip_to_end_of_block_or_statement (parser);
1481 return;
1483 if (nested && !specs->declspecs_seen_p)
1485 c_parser_error (parser, "expected declaration specifiers");
1486 c_parser_skip_to_end_of_block_or_statement (parser);
1487 return;
1489 finish_declspecs (specs);
1490 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1492 if (empty_ok)
1493 shadow_tag (specs);
1494 else
1496 shadow_tag_warned (specs, 1);
1497 pedwarn (here, 0, "empty declaration");
1499 c_parser_consume_token (parser);
1500 return;
1503 /* Provide better error recovery. Note that a type name here is usually
1504 better diagnosed as a redeclaration. */
1505 if (empty_ok
1506 && specs->typespec_kind == ctsk_tagdef
1507 && c_parser_next_token_starts_declspecs (parser)
1508 && !c_parser_next_token_is (parser, CPP_NAME))
1510 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1511 parser->error = false;
1512 shadow_tag_warned (specs, 1);
1513 return;
1515 else if (c_dialect_objc ())
1517 /* Prefix attributes are an error on method decls. */
1518 switch (c_parser_peek_token (parser)->type)
1520 case CPP_PLUS:
1521 case CPP_MINUS:
1522 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1523 return;
1524 if (specs->attrs)
1526 warning_at (c_parser_peek_token (parser)->location,
1527 OPT_Wattributes,
1528 "prefix attributes are ignored for methods");
1529 specs->attrs = NULL_TREE;
1531 if (fndef_ok)
1532 c_parser_objc_method_definition (parser);
1533 else
1534 c_parser_objc_methodproto (parser);
1535 return;
1536 break;
1537 default:
1538 break;
1540 /* This is where we parse 'attributes @interface ...',
1541 'attributes @implementation ...', 'attributes @protocol ...'
1542 (where attributes could be, for example, __attribute__
1543 ((deprecated)).
1545 switch (c_parser_peek_token (parser)->keyword)
1547 case RID_AT_INTERFACE:
1549 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1550 return;
1551 c_parser_objc_class_definition (parser, specs->attrs);
1552 return;
1554 break;
1555 case RID_AT_IMPLEMENTATION:
1557 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1558 return;
1559 if (specs->attrs)
1561 warning_at (c_parser_peek_token (parser)->location,
1562 OPT_Wattributes,
1563 "prefix attributes are ignored for implementations");
1564 specs->attrs = NULL_TREE;
1566 c_parser_objc_class_definition (parser, NULL_TREE);
1567 return;
1569 break;
1570 case RID_AT_PROTOCOL:
1572 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1573 return;
1574 c_parser_objc_protocol_definition (parser, specs->attrs);
1575 return;
1577 break;
1578 case RID_AT_ALIAS:
1579 case RID_AT_CLASS:
1580 case RID_AT_END:
1581 case RID_AT_PROPERTY:
1582 if (specs->attrs)
1584 c_parser_error (parser, "unexpected attribute");
1585 specs->attrs = NULL;
1587 break;
1588 default:
1589 break;
1593 pending_xref_error ();
1594 prefix_attrs = specs->attrs;
1595 all_prefix_attrs = prefix_attrs;
1596 specs->attrs = NULL_TREE;
1597 while (true)
1599 struct c_declarator *declarator;
1600 bool dummy = false;
1601 timevar_id_t tv;
1602 tree fnbody;
1603 /* Declaring either one or more declarators (in which case we
1604 should diagnose if there were no declaration specifiers) or a
1605 function definition (in which case the diagnostic for
1606 implicit int suffices). */
1607 declarator = c_parser_declarator (parser,
1608 specs->typespec_kind != ctsk_none,
1609 C_DTR_NORMAL, &dummy);
1610 if (declarator == NULL)
1612 c_parser_skip_to_end_of_block_or_statement (parser);
1613 return;
1615 if (c_parser_next_token_is (parser, CPP_EQ)
1616 || c_parser_next_token_is (parser, CPP_COMMA)
1617 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1618 || c_parser_next_token_is_keyword (parser, RID_ASM)
1619 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1620 || c_parser_next_token_is_keyword (parser, RID_IN))
1622 tree asm_name = NULL_TREE;
1623 tree postfix_attrs = NULL_TREE;
1624 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1626 diagnosed_no_specs = true;
1627 pedwarn (here, 0, "data definition has no type or storage class");
1629 /* Having seen a data definition, there cannot now be a
1630 function definition. */
1631 fndef_ok = false;
1632 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1633 asm_name = c_parser_simple_asm_expr (parser);
1634 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1635 postfix_attrs = c_parser_attributes (parser);
1636 if (c_parser_next_token_is (parser, CPP_EQ))
1638 tree d;
1639 struct c_expr init;
1640 location_t init_loc;
1641 c_parser_consume_token (parser);
1642 /* The declaration of the variable is in effect while
1643 its initializer is parsed. */
1644 d = start_decl (declarator, specs, true,
1645 chainon (postfix_attrs, all_prefix_attrs));
1646 if (!d)
1647 d = error_mark_node;
1648 start_init (d, asm_name, global_bindings_p ());
1649 init_loc = c_parser_peek_token (parser)->location;
1650 init = c_parser_initializer (parser);
1651 finish_init ();
1652 if (d != error_mark_node)
1654 maybe_warn_string_init (TREE_TYPE (d), init);
1655 finish_decl (d, init_loc, init.value,
1656 init.original_type, asm_name);
1659 else
1661 tree d = start_decl (declarator, specs, false,
1662 chainon (postfix_attrs,
1663 all_prefix_attrs));
1664 if (d)
1665 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1666 NULL_TREE, asm_name);
1668 if (c_parser_next_token_is_keyword (parser, RID_IN))
1670 if (d)
1671 *objc_foreach_object_declaration = d;
1672 else
1673 *objc_foreach_object_declaration = error_mark_node;
1676 if (c_parser_next_token_is (parser, CPP_COMMA))
1678 c_parser_consume_token (parser);
1679 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1680 all_prefix_attrs = chainon (c_parser_attributes (parser),
1681 prefix_attrs);
1682 else
1683 all_prefix_attrs = prefix_attrs;
1684 continue;
1686 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1688 c_parser_consume_token (parser);
1689 return;
1691 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1693 /* This can only happen in Objective-C: we found the
1694 'in' that terminates the declaration inside an
1695 Objective-C foreach statement. Do not consume the
1696 token, so that the caller can use it to determine
1697 that this indeed is a foreach context. */
1698 return;
1700 else
1702 c_parser_error (parser, "expected %<,%> or %<;%>");
1703 c_parser_skip_to_end_of_block_or_statement (parser);
1704 return;
1707 else if (!fndef_ok)
1709 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1710 "%<asm%> or %<__attribute__%>");
1711 c_parser_skip_to_end_of_block_or_statement (parser);
1712 return;
1714 /* Function definition (nested or otherwise). */
1715 if (nested)
1717 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1718 c_push_function_context ();
1720 if (!start_function (specs, declarator, all_prefix_attrs))
1722 /* This can appear in many cases looking nothing like a
1723 function definition, so we don't give a more specific
1724 error suggesting there was one. */
1725 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1726 "or %<__attribute__%>");
1727 if (nested)
1728 c_pop_function_context ();
1729 break;
1732 if (DECL_DECLARED_INLINE_P (current_function_decl))
1733 tv = TV_PARSE_INLINE;
1734 else
1735 tv = TV_PARSE_FUNC;
1736 timevar_push (tv);
1738 /* Parse old-style parameter declarations. ??? Attributes are
1739 not allowed to start declaration specifiers here because of a
1740 syntax conflict between a function declaration with attribute
1741 suffix and a function definition with an attribute prefix on
1742 first old-style parameter declaration. Following the old
1743 parser, they are not accepted on subsequent old-style
1744 parameter declarations either. However, there is no
1745 ambiguity after the first declaration, nor indeed on the
1746 first as long as we don't allow postfix attributes after a
1747 declarator with a nonempty identifier list in a definition;
1748 and postfix attributes have never been accepted here in
1749 function definitions either. */
1750 while (c_parser_next_token_is_not (parser, CPP_EOF)
1751 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1752 c_parser_declaration_or_fndef (parser, false, false, false,
1753 true, false, NULL);
1754 store_parm_decls ();
1755 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1756 = c_parser_peek_token (parser)->location;
1757 fnbody = c_parser_compound_statement (parser);
1758 if (nested)
1760 tree decl = current_function_decl;
1761 /* Mark nested functions as needing static-chain initially.
1762 lower_nested_functions will recompute it but the
1763 DECL_STATIC_CHAIN flag is also used before that happens,
1764 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1765 DECL_STATIC_CHAIN (decl) = 1;
1766 add_stmt (fnbody);
1767 finish_function ();
1768 c_pop_function_context ();
1769 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1771 else
1773 add_stmt (fnbody);
1774 finish_function ();
1777 timevar_pop (tv);
1778 break;
1782 /* Parse an asm-definition (asm() outside a function body). This is a
1783 GNU extension.
1785 asm-definition:
1786 simple-asm-expr ;
1789 static void
1790 c_parser_asm_definition (c_parser *parser)
1792 tree asm_str = c_parser_simple_asm_expr (parser);
1793 if (asm_str)
1794 add_asm_node (asm_str);
1795 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1798 /* Parse a static assertion (C11 6.7.10).
1800 static_assert-declaration:
1801 static_assert-declaration-no-semi ;
1804 static void
1805 c_parser_static_assert_declaration (c_parser *parser)
1807 c_parser_static_assert_declaration_no_semi (parser);
1808 if (parser->error
1809 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1810 c_parser_skip_to_end_of_block_or_statement (parser);
1813 /* Parse a static assertion (C11 6.7.10), without the trailing
1814 semicolon.
1816 static_assert-declaration-no-semi:
1817 _Static_assert ( constant-expression , string-literal )
1820 static void
1821 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1823 location_t assert_loc, value_loc;
1824 tree value;
1825 tree string;
1827 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1828 assert_loc = c_parser_peek_token (parser)->location;
1829 if (!flag_isoc11)
1831 if (flag_isoc99)
1832 pedwarn (assert_loc, OPT_Wpedantic,
1833 "ISO C99 does not support %<_Static_assert%>");
1834 else
1835 pedwarn (assert_loc, OPT_Wpedantic,
1836 "ISO C90 does not support %<_Static_assert%>");
1838 c_parser_consume_token (parser);
1839 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
1840 return;
1841 value_loc = c_parser_peek_token (parser)->location;
1842 value = c_parser_expr_no_commas (parser, NULL).value;
1843 parser->lex_untranslated_string = true;
1844 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
1846 parser->lex_untranslated_string = false;
1847 return;
1849 switch (c_parser_peek_token (parser)->type)
1851 case CPP_STRING:
1852 case CPP_STRING16:
1853 case CPP_STRING32:
1854 case CPP_WSTRING:
1855 case CPP_UTF8STRING:
1856 string = c_parser_peek_token (parser)->value;
1857 c_parser_consume_token (parser);
1858 parser->lex_untranslated_string = false;
1859 break;
1860 default:
1861 c_parser_error (parser, "expected string literal");
1862 parser->lex_untranslated_string = false;
1863 return;
1865 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
1867 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
1869 error_at (value_loc, "expression in static assertion is not an integer");
1870 return;
1872 if (TREE_CODE (value) != INTEGER_CST)
1874 value = c_fully_fold (value, false, NULL);
1875 if (TREE_CODE (value) == INTEGER_CST)
1876 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
1877 "is not an integer constant expression");
1879 if (TREE_CODE (value) != INTEGER_CST)
1881 error_at (value_loc, "expression in static assertion is not constant");
1882 return;
1884 constant_expression_warning (value);
1885 if (integer_zerop (value))
1886 error_at (assert_loc, "static assertion failed: %E", string);
1889 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
1890 6.7), adding them to SPECS (which may already include some).
1891 Storage class specifiers are accepted iff SCSPEC_OK; type
1892 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at
1893 the start iff START_ATTR_OK.
1895 declaration-specifiers:
1896 storage-class-specifier declaration-specifiers[opt]
1897 type-specifier declaration-specifiers[opt]
1898 type-qualifier declaration-specifiers[opt]
1899 function-specifier declaration-specifiers[opt]
1900 alignment-specifier declaration-specifiers[opt]
1902 Function specifiers (inline) are from C99, and are currently
1903 handled as storage class specifiers, as is __thread. Alignment
1904 specifiers are from C11.
1906 C90 6.5.1, C99 6.7.1:
1907 storage-class-specifier:
1908 typedef
1909 extern
1910 static
1911 auto
1912 register
1914 C99 6.7.4:
1915 function-specifier:
1916 inline
1917 _Noreturn
1919 (_Noreturn is new in C11.)
1921 C90 6.5.2, C99 6.7.2:
1922 type-specifier:
1923 void
1924 char
1925 short
1927 long
1928 float
1929 double
1930 signed
1931 unsigned
1932 _Bool
1933 _Complex
1934 [_Imaginary removed in C99 TC2]
1935 struct-or-union-specifier
1936 enum-specifier
1937 typedef-name
1939 (_Bool and _Complex are new in C99.)
1941 C90 6.5.3, C99 6.7.3:
1943 type-qualifier:
1944 const
1945 restrict
1946 volatile
1947 address-space-qualifier
1949 (restrict is new in C99.)
1951 GNU extensions:
1953 declaration-specifiers:
1954 attributes declaration-specifiers[opt]
1956 type-qualifier:
1957 address-space
1959 address-space:
1960 identifier recognized by the target
1962 storage-class-specifier:
1963 __thread
1965 type-specifier:
1966 typeof-specifier
1967 __int128
1968 _Decimal32
1969 _Decimal64
1970 _Decimal128
1971 _Fract
1972 _Accum
1973 _Sat
1975 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
1976 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
1978 Objective-C:
1980 type-specifier:
1981 class-name objc-protocol-refs[opt]
1982 typedef-name objc-protocol-refs
1983 objc-protocol-refs
1986 static void
1987 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
1988 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
1989 enum c_lookahead_kind la)
1991 bool attrs_ok = start_attr_ok;
1992 bool seen_type = specs->typespec_kind != ctsk_none;
1994 if (!typespec_ok)
1995 gcc_assert (la == cla_prefer_id);
1997 while (c_parser_next_token_is (parser, CPP_NAME)
1998 || c_parser_next_token_is (parser, CPP_KEYWORD)
1999 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2001 struct c_typespec t;
2002 tree attrs;
2003 tree align;
2004 location_t loc = c_parser_peek_token (parser)->location;
2006 /* If we cannot accept a type, exit if the next token must start
2007 one. Also, if we already have seen a tagged definition,
2008 a typename would be an error anyway and likely the user
2009 has simply forgotten a semicolon, so we exit. */
2010 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2011 && c_parser_next_tokens_start_typename (parser, la)
2012 && !c_parser_next_token_is_qualifier (parser))
2013 break;
2015 if (c_parser_next_token_is (parser, CPP_NAME))
2017 c_token *name_token = c_parser_peek_token (parser);
2018 tree value = name_token->value;
2019 c_id_kind kind = name_token->id_kind;
2021 if (kind == C_ID_ADDRSPACE)
2023 addr_space_t as
2024 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2025 declspecs_add_addrspace (name_token->location, specs, as);
2026 c_parser_consume_token (parser);
2027 attrs_ok = true;
2028 continue;
2031 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2033 /* If we cannot accept a type, and the next token must start one,
2034 exit. Do the same if we already have seen a tagged definition,
2035 since it would be an error anyway and likely the user has simply
2036 forgotten a semicolon. */
2037 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2038 break;
2040 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2041 a C_ID_CLASSNAME. */
2042 c_parser_consume_token (parser);
2043 seen_type = true;
2044 attrs_ok = true;
2045 if (kind == C_ID_ID)
2047 error ("unknown type name %qE", value);
2048 t.kind = ctsk_typedef;
2049 t.spec = error_mark_node;
2051 else if (kind == C_ID_TYPENAME
2052 && (!c_dialect_objc ()
2053 || c_parser_next_token_is_not (parser, CPP_LESS)))
2055 t.kind = ctsk_typedef;
2056 /* For a typedef name, record the meaning, not the name.
2057 In case of 'foo foo, bar;'. */
2058 t.spec = lookup_name (value);
2060 else
2062 tree proto = NULL_TREE;
2063 gcc_assert (c_dialect_objc ());
2064 t.kind = ctsk_objc;
2065 if (c_parser_next_token_is (parser, CPP_LESS))
2066 proto = c_parser_objc_protocol_refs (parser);
2067 t.spec = objc_get_protocol_qualified_type (value, proto);
2069 t.expr = NULL_TREE;
2070 t.expr_const_operands = true;
2071 declspecs_add_type (name_token->location, specs, t);
2072 continue;
2074 if (c_parser_next_token_is (parser, CPP_LESS))
2076 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2077 nisse@lysator.liu.se. */
2078 tree proto;
2079 gcc_assert (c_dialect_objc ());
2080 if (!typespec_ok || seen_type)
2081 break;
2082 proto = c_parser_objc_protocol_refs (parser);
2083 t.kind = ctsk_objc;
2084 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2085 t.expr = NULL_TREE;
2086 t.expr_const_operands = true;
2087 declspecs_add_type (loc, specs, t);
2088 continue;
2090 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2091 switch (c_parser_peek_token (parser)->keyword)
2093 case RID_STATIC:
2094 case RID_EXTERN:
2095 case RID_REGISTER:
2096 case RID_TYPEDEF:
2097 case RID_INLINE:
2098 case RID_NORETURN:
2099 case RID_AUTO:
2100 case RID_THREAD:
2101 if (!scspec_ok)
2102 goto out;
2103 attrs_ok = true;
2104 /* TODO: Distinguish between function specifiers (inline, noreturn)
2105 and storage class specifiers, either here or in
2106 declspecs_add_scspec. */
2107 declspecs_add_scspec (loc, specs,
2108 c_parser_peek_token (parser)->value);
2109 c_parser_consume_token (parser);
2110 break;
2111 case RID_UNSIGNED:
2112 case RID_LONG:
2113 case RID_INT128:
2114 case RID_SHORT:
2115 case RID_SIGNED:
2116 case RID_COMPLEX:
2117 case RID_INT:
2118 case RID_CHAR:
2119 case RID_FLOAT:
2120 case RID_DOUBLE:
2121 case RID_VOID:
2122 case RID_DFLOAT32:
2123 case RID_DFLOAT64:
2124 case RID_DFLOAT128:
2125 case RID_BOOL:
2126 case RID_FRACT:
2127 case RID_ACCUM:
2128 case RID_SAT:
2129 if (!typespec_ok)
2130 goto out;
2131 attrs_ok = true;
2132 seen_type = true;
2133 if (c_dialect_objc ())
2134 parser->objc_need_raw_identifier = true;
2135 t.kind = ctsk_resword;
2136 t.spec = c_parser_peek_token (parser)->value;
2137 t.expr = NULL_TREE;
2138 t.expr_const_operands = true;
2139 declspecs_add_type (loc, specs, t);
2140 c_parser_consume_token (parser);
2141 break;
2142 case RID_ENUM:
2143 if (!typespec_ok)
2144 goto out;
2145 attrs_ok = true;
2146 seen_type = true;
2147 t = c_parser_enum_specifier (parser);
2148 declspecs_add_type (loc, specs, t);
2149 break;
2150 case RID_STRUCT:
2151 case RID_UNION:
2152 if (!typespec_ok)
2153 goto out;
2154 attrs_ok = true;
2155 seen_type = true;
2156 t = c_parser_struct_or_union_specifier (parser);
2157 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2158 declspecs_add_type (loc, specs, t);
2159 break;
2160 case RID_TYPEOF:
2161 /* ??? The old parser rejected typeof after other type
2162 specifiers, but is a syntax error the best way of
2163 handling this? */
2164 if (!typespec_ok || seen_type)
2165 goto out;
2166 attrs_ok = true;
2167 seen_type = true;
2168 t = c_parser_typeof_specifier (parser);
2169 declspecs_add_type (loc, specs, t);
2170 break;
2171 case RID_CONST:
2172 case RID_VOLATILE:
2173 case RID_RESTRICT:
2174 attrs_ok = true;
2175 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2176 c_parser_consume_token (parser);
2177 break;
2178 case RID_ATTRIBUTE:
2179 if (!attrs_ok)
2180 goto out;
2181 attrs = c_parser_attributes (parser);
2182 declspecs_add_attrs (loc, specs, attrs);
2183 break;
2184 case RID_ALIGNAS:
2185 align = c_parser_alignas_specifier (parser);
2186 declspecs_add_alignas (loc, specs, align);
2187 break;
2188 default:
2189 goto out;
2192 out: ;
2195 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2197 enum-specifier:
2198 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2199 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2200 enum attributes[opt] identifier
2202 The form with trailing comma is new in C99. The forms with
2203 attributes are GNU extensions. In GNU C, we accept any expression
2204 without commas in the syntax (assignment expressions, not just
2205 conditional expressions); assignment expressions will be diagnosed
2206 as non-constant.
2208 enumerator-list:
2209 enumerator
2210 enumerator-list , enumerator
2212 enumerator:
2213 enumeration-constant
2214 enumeration-constant = constant-expression
2217 static struct c_typespec
2218 c_parser_enum_specifier (c_parser *parser)
2220 struct c_typespec ret;
2221 tree attrs;
2222 tree ident = NULL_TREE;
2223 location_t enum_loc;
2224 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2225 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2226 enum_loc = c_parser_peek_token (parser)->location;
2227 c_parser_consume_token (parser);
2228 attrs = c_parser_attributes (parser);
2229 enum_loc = c_parser_peek_token (parser)->location;
2230 /* Set the location in case we create a decl now. */
2231 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2232 if (c_parser_next_token_is (parser, CPP_NAME))
2234 ident = c_parser_peek_token (parser)->value;
2235 ident_loc = c_parser_peek_token (parser)->location;
2236 enum_loc = ident_loc;
2237 c_parser_consume_token (parser);
2239 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2241 /* Parse an enum definition. */
2242 struct c_enum_contents the_enum;
2243 tree type;
2244 tree postfix_attrs;
2245 /* We chain the enumerators in reverse order, then put them in
2246 forward order at the end. */
2247 tree values;
2248 timevar_push (TV_PARSE_ENUM);
2249 type = start_enum (enum_loc, &the_enum, ident);
2250 values = NULL_TREE;
2251 c_parser_consume_token (parser);
2252 while (true)
2254 tree enum_id;
2255 tree enum_value;
2256 tree enum_decl;
2257 bool seen_comma;
2258 c_token *token;
2259 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2260 location_t decl_loc, value_loc;
2261 if (c_parser_next_token_is_not (parser, CPP_NAME))
2263 c_parser_error (parser, "expected identifier");
2264 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2265 values = error_mark_node;
2266 break;
2268 token = c_parser_peek_token (parser);
2269 enum_id = token->value;
2270 /* Set the location in case we create a decl now. */
2271 c_parser_set_source_position_from_token (token);
2272 decl_loc = value_loc = token->location;
2273 c_parser_consume_token (parser);
2274 if (c_parser_next_token_is (parser, CPP_EQ))
2276 c_parser_consume_token (parser);
2277 value_loc = c_parser_peek_token (parser)->location;
2278 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2280 else
2281 enum_value = NULL_TREE;
2282 enum_decl = build_enumerator (decl_loc, value_loc,
2283 &the_enum, enum_id, enum_value);
2284 TREE_CHAIN (enum_decl) = values;
2285 values = enum_decl;
2286 seen_comma = false;
2287 if (c_parser_next_token_is (parser, CPP_COMMA))
2289 comma_loc = c_parser_peek_token (parser)->location;
2290 seen_comma = true;
2291 c_parser_consume_token (parser);
2293 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2295 if (seen_comma && !flag_isoc99)
2296 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2297 c_parser_consume_token (parser);
2298 break;
2300 if (!seen_comma)
2302 c_parser_error (parser, "expected %<,%> or %<}%>");
2303 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2304 values = error_mark_node;
2305 break;
2308 postfix_attrs = c_parser_attributes (parser);
2309 ret.spec = finish_enum (type, nreverse (values),
2310 chainon (attrs, postfix_attrs));
2311 ret.kind = ctsk_tagdef;
2312 ret.expr = NULL_TREE;
2313 ret.expr_const_operands = true;
2314 timevar_pop (TV_PARSE_ENUM);
2315 return ret;
2317 else if (!ident)
2319 c_parser_error (parser, "expected %<{%>");
2320 ret.spec = error_mark_node;
2321 ret.kind = ctsk_tagref;
2322 ret.expr = NULL_TREE;
2323 ret.expr_const_operands = true;
2324 return ret;
2326 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2327 /* In ISO C, enumerated types can be referred to only if already
2328 defined. */
2329 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2331 gcc_assert (ident);
2332 pedwarn (enum_loc, OPT_Wpedantic,
2333 "ISO C forbids forward references to %<enum%> types");
2335 return ret;
2338 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2340 struct-or-union-specifier:
2341 struct-or-union attributes[opt] identifier[opt]
2342 { struct-contents } attributes[opt]
2343 struct-or-union attributes[opt] identifier
2345 struct-contents:
2346 struct-declaration-list
2348 struct-declaration-list:
2349 struct-declaration ;
2350 struct-declaration-list struct-declaration ;
2352 GNU extensions:
2354 struct-contents:
2355 empty
2356 struct-declaration
2357 struct-declaration-list struct-declaration
2359 struct-declaration-list:
2360 struct-declaration-list ;
2363 (Note that in the syntax here, unlike that in ISO C, the semicolons
2364 are included here rather than in struct-declaration, in order to
2365 describe the syntax with extra semicolons and missing semicolon at
2366 end.)
2368 Objective-C:
2370 struct-declaration-list:
2371 @defs ( class-name )
2373 (Note this does not include a trailing semicolon, but can be
2374 followed by further declarations, and gets a pedwarn-if-pedantic
2375 when followed by a semicolon.) */
2377 static struct c_typespec
2378 c_parser_struct_or_union_specifier (c_parser *parser)
2380 struct c_typespec ret;
2381 tree attrs;
2382 tree ident = NULL_TREE;
2383 location_t struct_loc;
2384 location_t ident_loc = UNKNOWN_LOCATION;
2385 enum tree_code code;
2386 switch (c_parser_peek_token (parser)->keyword)
2388 case RID_STRUCT:
2389 code = RECORD_TYPE;
2390 break;
2391 case RID_UNION:
2392 code = UNION_TYPE;
2393 break;
2394 default:
2395 gcc_unreachable ();
2397 struct_loc = c_parser_peek_token (parser)->location;
2398 c_parser_consume_token (parser);
2399 attrs = c_parser_attributes (parser);
2401 /* Set the location in case we create a decl now. */
2402 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2404 if (c_parser_next_token_is (parser, CPP_NAME))
2406 ident = c_parser_peek_token (parser)->value;
2407 ident_loc = c_parser_peek_token (parser)->location;
2408 struct_loc = ident_loc;
2409 c_parser_consume_token (parser);
2411 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2413 /* Parse a struct or union definition. Start the scope of the
2414 tag before parsing components. */
2415 struct c_struct_parse_info *struct_info;
2416 tree type = start_struct (struct_loc, code, ident, &struct_info);
2417 tree postfix_attrs;
2418 /* We chain the components in reverse order, then put them in
2419 forward order at the end. Each struct-declaration may
2420 declare multiple components (comma-separated), so we must use
2421 chainon to join them, although when parsing each
2422 struct-declaration we can use TREE_CHAIN directly.
2424 The theory behind all this is that there will be more
2425 semicolon separated fields than comma separated fields, and
2426 so we'll be minimizing the number of node traversals required
2427 by chainon. */
2428 tree contents;
2429 timevar_push (TV_PARSE_STRUCT);
2430 contents = NULL_TREE;
2431 c_parser_consume_token (parser);
2432 /* Handle the Objective-C @defs construct,
2433 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2434 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2436 tree name;
2437 gcc_assert (c_dialect_objc ());
2438 c_parser_consume_token (parser);
2439 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2440 goto end_at_defs;
2441 if (c_parser_next_token_is (parser, CPP_NAME)
2442 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2444 name = c_parser_peek_token (parser)->value;
2445 c_parser_consume_token (parser);
2447 else
2449 c_parser_error (parser, "expected class name");
2450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2451 goto end_at_defs;
2453 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2454 "expected %<)%>");
2455 contents = nreverse (objc_get_class_ivars (name));
2457 end_at_defs:
2458 /* Parse the struct-declarations and semicolons. Problems with
2459 semicolons are diagnosed here; empty structures are diagnosed
2460 elsewhere. */
2461 while (true)
2463 tree decls;
2464 /* Parse any stray semicolon. */
2465 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2467 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2468 "extra semicolon in struct or union specified");
2469 c_parser_consume_token (parser);
2470 continue;
2472 /* Stop if at the end of the struct or union contents. */
2473 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2475 c_parser_consume_token (parser);
2476 break;
2478 /* Accept #pragmas at struct scope. */
2479 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2481 c_parser_pragma (parser, pragma_external);
2482 continue;
2484 /* Parse some comma-separated declarations, but not the
2485 trailing semicolon if any. */
2486 decls = c_parser_struct_declaration (parser);
2487 contents = chainon (decls, contents);
2488 /* If no semicolon follows, either we have a parse error or
2489 are at the end of the struct or union and should
2490 pedwarn. */
2491 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2492 c_parser_consume_token (parser);
2493 else
2495 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2496 pedwarn (c_parser_peek_token (parser)->location, 0,
2497 "no semicolon at end of struct or union");
2498 else if (parser->error
2499 || !c_parser_next_token_starts_declspecs (parser))
2501 c_parser_error (parser, "expected %<;%>");
2502 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2503 break;
2506 /* If we come here, we have already emitted an error
2507 for an expected `;', identifier or `(', and we also
2508 recovered already. Go on with the next field. */
2511 postfix_attrs = c_parser_attributes (parser);
2512 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2513 chainon (attrs, postfix_attrs), struct_info);
2514 ret.kind = ctsk_tagdef;
2515 ret.expr = NULL_TREE;
2516 ret.expr_const_operands = true;
2517 timevar_pop (TV_PARSE_STRUCT);
2518 return ret;
2520 else if (!ident)
2522 c_parser_error (parser, "expected %<{%>");
2523 ret.spec = error_mark_node;
2524 ret.kind = ctsk_tagref;
2525 ret.expr = NULL_TREE;
2526 ret.expr_const_operands = true;
2527 return ret;
2529 ret = parser_xref_tag (ident_loc, code, ident);
2530 return ret;
2533 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2534 the trailing semicolon.
2536 struct-declaration:
2537 specifier-qualifier-list struct-declarator-list
2538 static_assert-declaration-no-semi
2540 specifier-qualifier-list:
2541 type-specifier specifier-qualifier-list[opt]
2542 type-qualifier specifier-qualifier-list[opt]
2543 attributes specifier-qualifier-list[opt]
2545 struct-declarator-list:
2546 struct-declarator
2547 struct-declarator-list , attributes[opt] struct-declarator
2549 struct-declarator:
2550 declarator attributes[opt]
2551 declarator[opt] : constant-expression attributes[opt]
2553 GNU extensions:
2555 struct-declaration:
2556 __extension__ struct-declaration
2557 specifier-qualifier-list
2559 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2560 of attributes where shown is a GNU extension. In GNU C, we accept
2561 any expression without commas in the syntax (assignment
2562 expressions, not just conditional expressions); assignment
2563 expressions will be diagnosed as non-constant. */
2565 static tree
2566 c_parser_struct_declaration (c_parser *parser)
2568 struct c_declspecs *specs;
2569 tree prefix_attrs;
2570 tree all_prefix_attrs;
2571 tree decls;
2572 location_t decl_loc;
2573 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2575 int ext;
2576 tree decl;
2577 ext = disable_extension_diagnostics ();
2578 c_parser_consume_token (parser);
2579 decl = c_parser_struct_declaration (parser);
2580 restore_extension_diagnostics (ext);
2581 return decl;
2583 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2585 c_parser_static_assert_declaration_no_semi (parser);
2586 return NULL_TREE;
2588 specs = build_null_declspecs ();
2589 decl_loc = c_parser_peek_token (parser)->location;
2590 c_parser_declspecs (parser, specs, false, true, true, cla_nonabstract_decl);
2591 if (parser->error)
2592 return NULL_TREE;
2593 if (!specs->declspecs_seen_p)
2595 c_parser_error (parser, "expected specifier-qualifier-list");
2596 return NULL_TREE;
2598 finish_declspecs (specs);
2599 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2600 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2602 tree ret;
2603 if (specs->typespec_kind == ctsk_none)
2605 pedwarn (decl_loc, OPT_Wpedantic,
2606 "ISO C forbids member declarations with no members");
2607 shadow_tag_warned (specs, pedantic);
2608 ret = NULL_TREE;
2610 else
2612 /* Support for unnamed structs or unions as members of
2613 structs or unions (which is [a] useful and [b] supports
2614 MS P-SDK). */
2615 tree attrs = NULL;
2617 ret = grokfield (c_parser_peek_token (parser)->location,
2618 build_id_declarator (NULL_TREE), specs,
2619 NULL_TREE, &attrs);
2620 if (ret)
2621 decl_attributes (&ret, attrs, 0);
2623 return ret;
2626 /* Provide better error recovery. Note that a type name here is valid,
2627 and will be treated as a field name. */
2628 if (specs->typespec_kind == ctsk_tagdef
2629 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2630 && c_parser_next_token_starts_declspecs (parser)
2631 && !c_parser_next_token_is (parser, CPP_NAME))
2633 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2634 parser->error = false;
2635 return NULL_TREE;
2638 pending_xref_error ();
2639 prefix_attrs = specs->attrs;
2640 all_prefix_attrs = prefix_attrs;
2641 specs->attrs = NULL_TREE;
2642 decls = NULL_TREE;
2643 while (true)
2645 /* Declaring one or more declarators or un-named bit-fields. */
2646 struct c_declarator *declarator;
2647 bool dummy = false;
2648 if (c_parser_next_token_is (parser, CPP_COLON))
2649 declarator = build_id_declarator (NULL_TREE);
2650 else
2651 declarator = c_parser_declarator (parser,
2652 specs->typespec_kind != ctsk_none,
2653 C_DTR_NORMAL, &dummy);
2654 if (declarator == NULL)
2656 c_parser_skip_to_end_of_block_or_statement (parser);
2657 break;
2659 if (c_parser_next_token_is (parser, CPP_COLON)
2660 || c_parser_next_token_is (parser, CPP_COMMA)
2661 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2662 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2663 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2665 tree postfix_attrs = NULL_TREE;
2666 tree width = NULL_TREE;
2667 tree d;
2668 if (c_parser_next_token_is (parser, CPP_COLON))
2670 c_parser_consume_token (parser);
2671 width = c_parser_expr_no_commas (parser, NULL).value;
2673 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2674 postfix_attrs = c_parser_attributes (parser);
2675 d = grokfield (c_parser_peek_token (parser)->location,
2676 declarator, specs, width, &all_prefix_attrs);
2677 decl_attributes (&d, chainon (postfix_attrs,
2678 all_prefix_attrs), 0);
2679 DECL_CHAIN (d) = decls;
2680 decls = d;
2681 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2682 all_prefix_attrs = chainon (c_parser_attributes (parser),
2683 prefix_attrs);
2684 else
2685 all_prefix_attrs = prefix_attrs;
2686 if (c_parser_next_token_is (parser, CPP_COMMA))
2687 c_parser_consume_token (parser);
2688 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2689 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2691 /* Semicolon consumed in caller. */
2692 break;
2694 else
2696 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2697 break;
2700 else
2702 c_parser_error (parser,
2703 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2704 "%<__attribute__%>");
2705 break;
2708 return decls;
2711 /* Parse a typeof specifier (a GNU extension).
2713 typeof-specifier:
2714 typeof ( expression )
2715 typeof ( type-name )
2718 static struct c_typespec
2719 c_parser_typeof_specifier (c_parser *parser)
2721 struct c_typespec ret;
2722 ret.kind = ctsk_typeof;
2723 ret.spec = error_mark_node;
2724 ret.expr = NULL_TREE;
2725 ret.expr_const_operands = true;
2726 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2727 c_parser_consume_token (parser);
2728 c_inhibit_evaluation_warnings++;
2729 in_typeof++;
2730 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2732 c_inhibit_evaluation_warnings--;
2733 in_typeof--;
2734 return ret;
2736 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2738 struct c_type_name *type = c_parser_type_name (parser);
2739 c_inhibit_evaluation_warnings--;
2740 in_typeof--;
2741 if (type != NULL)
2743 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2744 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2747 else
2749 bool was_vm;
2750 location_t here = c_parser_peek_token (parser)->location;
2751 struct c_expr expr = c_parser_expression (parser);
2752 c_inhibit_evaluation_warnings--;
2753 in_typeof--;
2754 if (TREE_CODE (expr.value) == COMPONENT_REF
2755 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
2756 error_at (here, "%<typeof%> applied to a bit-field");
2757 mark_exp_read (expr.value);
2758 ret.spec = TREE_TYPE (expr.value);
2759 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
2760 /* This is returned with the type so that when the type is
2761 evaluated, this can be evaluated. */
2762 if (was_vm)
2763 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
2764 pop_maybe_used (was_vm);
2766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2767 return ret;
2770 /* Parse an alignment-specifier.
2772 C11 6.7.5:
2774 alignment-specifier:
2775 _Alignas ( type-name )
2776 _Alignas ( constant-expression )
2779 static tree
2780 c_parser_alignas_specifier (c_parser * parser)
2782 tree ret = error_mark_node;
2783 location_t loc = c_parser_peek_token (parser)->location;
2784 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
2785 c_parser_consume_token (parser);
2786 if (!flag_isoc11)
2788 if (flag_isoc99)
2789 pedwarn (loc, OPT_Wpedantic,
2790 "ISO C99 does not support %<_Alignas%>");
2791 else
2792 pedwarn (loc, OPT_Wpedantic,
2793 "ISO C90 does not support %<_Alignas%>");
2795 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2796 return ret;
2797 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2799 struct c_type_name *type = c_parser_type_name (parser);
2800 if (type != NULL)
2801 ret = c_alignof (loc, groktypename (type, NULL, NULL));
2803 else
2804 ret = c_parser_expr_no_commas (parser, NULL).value;
2805 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2806 return ret;
2809 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
2810 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
2811 be redeclared; otherwise it may not. KIND indicates which kind of
2812 declarator is wanted. Returns a valid declarator except in the
2813 case of a syntax error in which case NULL is returned. *SEEN_ID is
2814 set to true if an identifier being declared is seen; this is used
2815 to diagnose bad forms of abstract array declarators and to
2816 determine whether an identifier list is syntactically permitted.
2818 declarator:
2819 pointer[opt] direct-declarator
2821 direct-declarator:
2822 identifier
2823 ( attributes[opt] declarator )
2824 direct-declarator array-declarator
2825 direct-declarator ( parameter-type-list )
2826 direct-declarator ( identifier-list[opt] )
2828 pointer:
2829 * type-qualifier-list[opt]
2830 * type-qualifier-list[opt] pointer
2832 type-qualifier-list:
2833 type-qualifier
2834 attributes
2835 type-qualifier-list type-qualifier
2836 type-qualifier-list attributes
2838 parameter-type-list:
2839 parameter-list
2840 parameter-list , ...
2842 parameter-list:
2843 parameter-declaration
2844 parameter-list , parameter-declaration
2846 parameter-declaration:
2847 declaration-specifiers declarator attributes[opt]
2848 declaration-specifiers abstract-declarator[opt] attributes[opt]
2850 identifier-list:
2851 identifier
2852 identifier-list , identifier
2854 abstract-declarator:
2855 pointer
2856 pointer[opt] direct-abstract-declarator
2858 direct-abstract-declarator:
2859 ( attributes[opt] abstract-declarator )
2860 direct-abstract-declarator[opt] array-declarator
2861 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
2863 GNU extensions:
2865 direct-declarator:
2866 direct-declarator ( parameter-forward-declarations
2867 parameter-type-list[opt] )
2869 direct-abstract-declarator:
2870 direct-abstract-declarator[opt] ( parameter-forward-declarations
2871 parameter-type-list[opt] )
2873 parameter-forward-declarations:
2874 parameter-list ;
2875 parameter-forward-declarations parameter-list ;
2877 The uses of attributes shown above are GNU extensions.
2879 Some forms of array declarator are not included in C99 in the
2880 syntax for abstract declarators; these are disallowed elsewhere.
2881 This may be a defect (DR#289).
2883 This function also accepts an omitted abstract declarator as being
2884 an abstract declarator, although not part of the formal syntax. */
2886 static struct c_declarator *
2887 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2888 bool *seen_id)
2890 /* Parse any initial pointer part. */
2891 if (c_parser_next_token_is (parser, CPP_MULT))
2893 struct c_declspecs *quals_attrs = build_null_declspecs ();
2894 struct c_declarator *inner;
2895 c_parser_consume_token (parser);
2896 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
2897 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
2898 if (inner == NULL)
2899 return NULL;
2900 else
2901 return make_pointer_declarator (quals_attrs, inner);
2903 /* Now we have a direct declarator, direct abstract declarator or
2904 nothing (which counts as a direct abstract declarator here). */
2905 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
2908 /* Parse a direct declarator or direct abstract declarator; arguments
2909 as c_parser_declarator. */
2911 static struct c_declarator *
2912 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
2913 bool *seen_id)
2915 /* The direct declarator must start with an identifier (possibly
2916 omitted) or a parenthesized declarator (possibly abstract). In
2917 an ordinary declarator, initial parentheses must start a
2918 parenthesized declarator. In an abstract declarator or parameter
2919 declarator, they could start a parenthesized declarator or a
2920 parameter list. To tell which, the open parenthesis and any
2921 following attributes must be read. If a declaration specifier
2922 follows, then it is a parameter list; if the specifier is a
2923 typedef name, there might be an ambiguity about redeclaring it,
2924 which is resolved in the direction of treating it as a typedef
2925 name. If a close parenthesis follows, it is also an empty
2926 parameter list, as the syntax does not permit empty abstract
2927 declarators. Otherwise, it is a parenthesized declarator (in
2928 which case the analysis may be repeated inside it, recursively).
2930 ??? There is an ambiguity in a parameter declaration "int
2931 (__attribute__((foo)) x)", where x is not a typedef name: it
2932 could be an abstract declarator for a function, or declare x with
2933 parentheses. The proper resolution of this ambiguity needs
2934 documenting. At present we follow an accident of the old
2935 parser's implementation, whereby the first parameter must have
2936 some declaration specifiers other than just attributes. Thus as
2937 a parameter declaration it is treated as a parenthesized
2938 parameter named x, and as an abstract declarator it is
2939 rejected.
2941 ??? Also following the old parser, attributes inside an empty
2942 parameter list are ignored, making it a list not yielding a
2943 prototype, rather than giving an error or making it have one
2944 parameter with implicit type int.
2946 ??? Also following the old parser, typedef names may be
2947 redeclared in declarators, but not Objective-C class names. */
2949 if (kind != C_DTR_ABSTRACT
2950 && c_parser_next_token_is (parser, CPP_NAME)
2951 && ((type_seen_p
2952 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
2953 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
2954 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
2956 struct c_declarator *inner
2957 = build_id_declarator (c_parser_peek_token (parser)->value);
2958 *seen_id = true;
2959 inner->id_loc = c_parser_peek_token (parser)->location;
2960 c_parser_consume_token (parser);
2961 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2964 if (kind != C_DTR_NORMAL
2965 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
2967 struct c_declarator *inner = build_id_declarator (NULL_TREE);
2968 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
2971 /* Either we are at the end of an abstract declarator, or we have
2972 parentheses. */
2974 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2976 tree attrs;
2977 struct c_declarator *inner;
2978 c_parser_consume_token (parser);
2979 attrs = c_parser_attributes (parser);
2980 if (kind != C_DTR_NORMAL
2981 && (c_parser_next_token_starts_declspecs (parser)
2982 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
2984 struct c_arg_info *args
2985 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
2986 attrs);
2987 if (args == NULL)
2988 return NULL;
2989 else
2991 inner
2992 = build_function_declarator (args,
2993 build_id_declarator (NULL_TREE));
2994 return c_parser_direct_declarator_inner (parser, *seen_id,
2995 inner);
2998 /* A parenthesized declarator. */
2999 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3000 if (inner != NULL && attrs != NULL)
3001 inner = build_attrs_declarator (attrs, inner);
3002 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3004 c_parser_consume_token (parser);
3005 if (inner == NULL)
3006 return NULL;
3007 else
3008 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3010 else
3012 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3013 "expected %<)%>");
3014 return NULL;
3017 else
3019 if (kind == C_DTR_NORMAL)
3021 c_parser_error (parser, "expected identifier or %<(%>");
3022 return NULL;
3024 else
3025 return build_id_declarator (NULL_TREE);
3029 /* Parse part of a direct declarator or direct abstract declarator,
3030 given that some (in INNER) has already been parsed; ID_PRESENT is
3031 true if an identifier is present, false for an abstract
3032 declarator. */
3034 static struct c_declarator *
3035 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3036 struct c_declarator *inner)
3038 /* Parse a sequence of array declarators and parameter lists. */
3039 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3041 location_t brace_loc = c_parser_peek_token (parser)->location;
3042 struct c_declarator *declarator;
3043 struct c_declspecs *quals_attrs = build_null_declspecs ();
3044 bool static_seen;
3045 bool star_seen;
3046 tree dimen;
3047 c_parser_consume_token (parser);
3048 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3049 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3050 if (static_seen)
3051 c_parser_consume_token (parser);
3052 if (static_seen && !quals_attrs->declspecs_seen_p)
3053 c_parser_declspecs (parser, quals_attrs, false, false, true, cla_prefer_id);
3054 if (!quals_attrs->declspecs_seen_p)
3055 quals_attrs = NULL;
3056 /* If "static" is present, there must be an array dimension.
3057 Otherwise, there may be a dimension, "*", or no
3058 dimension. */
3059 if (static_seen)
3061 star_seen = false;
3062 dimen = c_parser_expr_no_commas (parser, NULL).value;
3064 else
3066 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3068 dimen = NULL_TREE;
3069 star_seen = false;
3071 else if (c_parser_next_token_is (parser, CPP_MULT))
3073 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3075 dimen = NULL_TREE;
3076 star_seen = true;
3077 c_parser_consume_token (parser);
3079 else
3081 star_seen = false;
3082 dimen = c_parser_expr_no_commas (parser, NULL).value;
3085 else
3087 star_seen = false;
3088 dimen = c_parser_expr_no_commas (parser, NULL).value;
3091 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3092 c_parser_consume_token (parser);
3093 else
3095 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3096 "expected %<]%>");
3097 return NULL;
3099 if (dimen)
3100 mark_exp_read (dimen);
3101 declarator = build_array_declarator (brace_loc, dimen, quals_attrs,
3102 static_seen, star_seen);
3103 if (declarator == NULL)
3104 return NULL;
3105 inner = set_array_declarator_inner (declarator, inner);
3106 return c_parser_direct_declarator_inner (parser, id_present, inner);
3108 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3110 tree attrs;
3111 struct c_arg_info *args;
3112 c_parser_consume_token (parser);
3113 attrs = c_parser_attributes (parser);
3114 args = c_parser_parms_declarator (parser, id_present, attrs);
3115 if (args == NULL)
3116 return NULL;
3117 else
3119 inner = build_function_declarator (args, inner);
3120 return c_parser_direct_declarator_inner (parser, id_present, inner);
3123 return inner;
3126 /* Parse a parameter list or identifier list, including the closing
3127 parenthesis but not the opening one. ATTRS are the attributes at
3128 the start of the list. ID_LIST_OK is true if an identifier list is
3129 acceptable; such a list must not have attributes at the start. */
3131 static struct c_arg_info *
3132 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3134 push_scope ();
3135 declare_parm_level ();
3136 /* If the list starts with an identifier, it is an identifier list.
3137 Otherwise, it is either a prototype list or an empty list. */
3138 if (id_list_ok
3139 && !attrs
3140 && c_parser_next_token_is (parser, CPP_NAME)
3141 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3143 /* Look ahead to detect typos in type names. */
3144 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3145 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3146 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3147 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3149 tree list = NULL_TREE, *nextp = &list;
3150 while (c_parser_next_token_is (parser, CPP_NAME)
3151 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3153 *nextp = build_tree_list (NULL_TREE,
3154 c_parser_peek_token (parser)->value);
3155 nextp = & TREE_CHAIN (*nextp);
3156 c_parser_consume_token (parser);
3157 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3158 break;
3159 c_parser_consume_token (parser);
3160 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3162 c_parser_error (parser, "expected identifier");
3163 break;
3166 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3168 struct c_arg_info *ret = build_arg_info ();
3169 ret->types = list;
3170 c_parser_consume_token (parser);
3171 pop_scope ();
3172 return ret;
3174 else
3176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3177 "expected %<)%>");
3178 pop_scope ();
3179 return NULL;
3182 else
3184 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3185 NULL);
3186 pop_scope ();
3187 return ret;
3191 /* Parse a parameter list (possibly empty), including the closing
3192 parenthesis but not the opening one. ATTRS are the attributes at
3193 the start of the list. EXPR is NULL or an expression that needs to
3194 be evaluated for the side effects of array size expressions in the
3195 parameters. */
3197 static struct c_arg_info *
3198 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3200 bool bad_parm = false;
3202 /* ??? Following the old parser, forward parameter declarations may
3203 use abstract declarators, and if no real parameter declarations
3204 follow the forward declarations then this is not diagnosed. Also
3205 note as above that attributes are ignored as the only contents of
3206 the parentheses, or as the only contents after forward
3207 declarations. */
3208 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3210 struct c_arg_info *ret = build_arg_info ();
3211 c_parser_consume_token (parser);
3212 return ret;
3214 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3216 struct c_arg_info *ret = build_arg_info ();
3218 if (flag_allow_parameterless_variadic_functions)
3220 /* F (...) is allowed. */
3221 ret->types = NULL_TREE;
3223 else
3225 /* Suppress -Wold-style-definition for this case. */
3226 ret->types = error_mark_node;
3227 error_at (c_parser_peek_token (parser)->location,
3228 "ISO C requires a named argument before %<...%>");
3230 c_parser_consume_token (parser);
3231 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3233 c_parser_consume_token (parser);
3234 return ret;
3236 else
3238 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3239 "expected %<)%>");
3240 return NULL;
3243 /* Nonempty list of parameters, either terminated with semicolon
3244 (forward declarations; recurse) or with close parenthesis (normal
3245 function) or with ", ... )" (variadic function). */
3246 while (true)
3248 /* Parse a parameter. */
3249 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3250 attrs = NULL_TREE;
3251 if (parm == NULL)
3252 bad_parm = true;
3253 else
3254 push_parm_decl (parm, &expr);
3255 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3257 tree new_attrs;
3258 c_parser_consume_token (parser);
3259 mark_forward_parm_decls ();
3260 new_attrs = c_parser_attributes (parser);
3261 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3263 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3265 c_parser_consume_token (parser);
3266 if (bad_parm)
3267 return NULL;
3268 else
3269 return get_parm_info (false, expr);
3271 if (!c_parser_require (parser, CPP_COMMA,
3272 "expected %<;%>, %<,%> or %<)%>"))
3274 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3275 return NULL;
3277 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3279 c_parser_consume_token (parser);
3280 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3282 c_parser_consume_token (parser);
3283 if (bad_parm)
3284 return NULL;
3285 else
3286 return get_parm_info (true, expr);
3288 else
3290 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3291 "expected %<)%>");
3292 return NULL;
3298 /* Parse a parameter declaration. ATTRS are the attributes at the
3299 start of the declaration if it is the first parameter. */
3301 static struct c_parm *
3302 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3304 struct c_declspecs *specs;
3305 struct c_declarator *declarator;
3306 tree prefix_attrs;
3307 tree postfix_attrs = NULL_TREE;
3308 bool dummy = false;
3310 /* Accept #pragmas between parameter declarations. */
3311 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3312 c_parser_pragma (parser, pragma_external);
3314 if (!c_parser_next_token_starts_declspecs (parser))
3316 c_token *token = c_parser_peek_token (parser);
3317 if (parser->error)
3318 return NULL;
3319 c_parser_set_source_position_from_token (token);
3320 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3322 error ("unknown type name %qE", token->value);
3323 parser->error = true;
3325 /* ??? In some Objective-C cases '...' isn't applicable so there
3326 should be a different message. */
3327 else
3328 c_parser_error (parser,
3329 "expected declaration specifiers or %<...%>");
3330 c_parser_skip_to_end_of_parameter (parser);
3331 return NULL;
3333 specs = build_null_declspecs ();
3334 if (attrs)
3336 declspecs_add_attrs (input_location, specs, attrs);
3337 attrs = NULL_TREE;
3339 c_parser_declspecs (parser, specs, true, true, true, cla_nonabstract_decl);
3340 finish_declspecs (specs);
3341 pending_xref_error ();
3342 prefix_attrs = specs->attrs;
3343 specs->attrs = NULL_TREE;
3344 declarator = c_parser_declarator (parser,
3345 specs->typespec_kind != ctsk_none,
3346 C_DTR_PARM, &dummy);
3347 if (declarator == NULL)
3349 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3350 return NULL;
3352 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3353 postfix_attrs = c_parser_attributes (parser);
3354 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3355 declarator);
3358 /* Parse a string literal in an asm expression. It should not be
3359 translated, and wide string literals are an error although
3360 permitted by the syntax. This is a GNU extension.
3362 asm-string-literal:
3363 string-literal
3365 ??? At present, following the old parser, the caller needs to have
3366 set lex_untranslated_string to 1. It would be better to follow the
3367 C++ parser rather than using this kludge. */
3369 static tree
3370 c_parser_asm_string_literal (c_parser *parser)
3372 tree str;
3373 int save_flag = warn_overlength_strings;
3374 warn_overlength_strings = 0;
3375 if (c_parser_next_token_is (parser, CPP_STRING))
3377 str = c_parser_peek_token (parser)->value;
3378 c_parser_consume_token (parser);
3380 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3382 error_at (c_parser_peek_token (parser)->location,
3383 "wide string literal in %<asm%>");
3384 str = build_string (1, "");
3385 c_parser_consume_token (parser);
3387 else
3389 c_parser_error (parser, "expected string literal");
3390 str = NULL_TREE;
3392 warn_overlength_strings = save_flag;
3393 return str;
3396 /* Parse a simple asm expression. This is used in restricted
3397 contexts, where a full expression with inputs and outputs does not
3398 make sense. This is a GNU extension.
3400 simple-asm-expr:
3401 asm ( asm-string-literal )
3404 static tree
3405 c_parser_simple_asm_expr (c_parser *parser)
3407 tree str;
3408 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3409 /* ??? Follow the C++ parser rather than using the
3410 lex_untranslated_string kludge. */
3411 parser->lex_untranslated_string = true;
3412 c_parser_consume_token (parser);
3413 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3415 parser->lex_untranslated_string = false;
3416 return NULL_TREE;
3418 str = c_parser_asm_string_literal (parser);
3419 parser->lex_untranslated_string = false;
3420 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3423 return NULL_TREE;
3425 return str;
3428 static tree
3429 c_parser_attribute_any_word (c_parser *parser)
3431 tree attr_name = NULL_TREE;
3433 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3435 /* ??? See comment above about what keywords are accepted here. */
3436 bool ok;
3437 switch (c_parser_peek_token (parser)->keyword)
3439 case RID_STATIC:
3440 case RID_UNSIGNED:
3441 case RID_LONG:
3442 case RID_INT128:
3443 case RID_CONST:
3444 case RID_EXTERN:
3445 case RID_REGISTER:
3446 case RID_TYPEDEF:
3447 case RID_SHORT:
3448 case RID_INLINE:
3449 case RID_NORETURN:
3450 case RID_VOLATILE:
3451 case RID_SIGNED:
3452 case RID_AUTO:
3453 case RID_RESTRICT:
3454 case RID_COMPLEX:
3455 case RID_THREAD:
3456 case RID_INT:
3457 case RID_CHAR:
3458 case RID_FLOAT:
3459 case RID_DOUBLE:
3460 case RID_VOID:
3461 case RID_DFLOAT32:
3462 case RID_DFLOAT64:
3463 case RID_DFLOAT128:
3464 case RID_BOOL:
3465 case RID_FRACT:
3466 case RID_ACCUM:
3467 case RID_SAT:
3468 case RID_TRANSACTION_ATOMIC:
3469 case RID_TRANSACTION_CANCEL:
3470 ok = true;
3471 break;
3472 default:
3473 ok = false;
3474 break;
3476 if (!ok)
3477 return NULL_TREE;
3479 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3480 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3482 else if (c_parser_next_token_is (parser, CPP_NAME))
3483 attr_name = c_parser_peek_token (parser)->value;
3485 return attr_name;
3488 /* Parse (possibly empty) attributes. This is a GNU extension.
3490 attributes:
3491 empty
3492 attributes attribute
3494 attribute:
3495 __attribute__ ( ( attribute-list ) )
3497 attribute-list:
3498 attrib
3499 attribute_list , attrib
3501 attrib:
3502 empty
3503 any-word
3504 any-word ( identifier )
3505 any-word ( identifier , nonempty-expr-list )
3506 any-word ( expr-list )
3508 where the "identifier" must not be declared as a type, and
3509 "any-word" may be any identifier (including one declared as a
3510 type), a reserved word storage class specifier, type specifier or
3511 type qualifier. ??? This still leaves out most reserved keywords
3512 (following the old parser), shouldn't we include them, and why not
3513 allow identifiers declared as types to start the arguments? */
3515 static tree
3516 c_parser_attributes (c_parser *parser)
3518 tree attrs = NULL_TREE;
3519 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3521 /* ??? Follow the C++ parser rather than using the
3522 lex_untranslated_string kludge. */
3523 parser->lex_untranslated_string = true;
3524 c_parser_consume_token (parser);
3525 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3527 parser->lex_untranslated_string = false;
3528 return attrs;
3530 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3532 parser->lex_untranslated_string = false;
3533 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3534 return attrs;
3536 /* Parse the attribute list. */
3537 while (c_parser_next_token_is (parser, CPP_COMMA)
3538 || c_parser_next_token_is (parser, CPP_NAME)
3539 || c_parser_next_token_is (parser, CPP_KEYWORD))
3541 tree attr, attr_name, attr_args;
3542 VEC(tree,gc) *expr_list;
3543 if (c_parser_next_token_is (parser, CPP_COMMA))
3545 c_parser_consume_token (parser);
3546 continue;
3549 attr_name = c_parser_attribute_any_word (parser);
3550 if (attr_name == NULL)
3551 break;
3552 c_parser_consume_token (parser);
3553 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3555 attr = build_tree_list (attr_name, NULL_TREE);
3556 attrs = chainon (attrs, attr);
3557 continue;
3559 c_parser_consume_token (parser);
3560 /* Parse the attribute contents. If they start with an
3561 identifier which is followed by a comma or close
3562 parenthesis, then the arguments start with that
3563 identifier; otherwise they are an expression list.
3564 In objective-c the identifier may be a classname. */
3565 if (c_parser_next_token_is (parser, CPP_NAME)
3566 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3567 || (c_dialect_objc ()
3568 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3569 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3570 || (c_parser_peek_2nd_token (parser)->type
3571 == CPP_CLOSE_PAREN)))
3573 tree arg1 = c_parser_peek_token (parser)->value;
3574 c_parser_consume_token (parser);
3575 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3576 attr_args = build_tree_list (NULL_TREE, arg1);
3577 else
3579 tree tree_list;
3580 c_parser_consume_token (parser);
3581 expr_list = c_parser_expr_list (parser, false, true, NULL);
3582 tree_list = build_tree_list_vec (expr_list);
3583 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3584 release_tree_vector (expr_list);
3587 else
3589 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3590 attr_args = NULL_TREE;
3591 else
3593 expr_list = c_parser_expr_list (parser, false, true, NULL);
3594 attr_args = build_tree_list_vec (expr_list);
3595 release_tree_vector (expr_list);
3598 attr = build_tree_list (attr_name, attr_args);
3599 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3600 c_parser_consume_token (parser);
3601 else
3603 parser->lex_untranslated_string = false;
3604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3605 "expected %<)%>");
3606 return attrs;
3608 attrs = chainon (attrs, attr);
3610 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3611 c_parser_consume_token (parser);
3612 else
3614 parser->lex_untranslated_string = false;
3615 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3616 "expected %<)%>");
3617 return attrs;
3619 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3620 c_parser_consume_token (parser);
3621 else
3623 parser->lex_untranslated_string = false;
3624 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3625 "expected %<)%>");
3626 return attrs;
3628 parser->lex_untranslated_string = false;
3630 return attrs;
3633 /* Parse a type name (C90 6.5.5, C99 6.7.6).
3635 type-name:
3636 specifier-qualifier-list abstract-declarator[opt]
3639 static struct c_type_name *
3640 c_parser_type_name (c_parser *parser)
3642 struct c_declspecs *specs = build_null_declspecs ();
3643 struct c_declarator *declarator;
3644 struct c_type_name *ret;
3645 bool dummy = false;
3646 c_parser_declspecs (parser, specs, false, true, true, cla_prefer_type);
3647 if (!specs->declspecs_seen_p)
3649 c_parser_error (parser, "expected specifier-qualifier-list");
3650 return NULL;
3652 if (specs->type != error_mark_node)
3654 pending_xref_error ();
3655 finish_declspecs (specs);
3657 declarator = c_parser_declarator (parser,
3658 specs->typespec_kind != ctsk_none,
3659 C_DTR_ABSTRACT, &dummy);
3660 if (declarator == NULL)
3661 return NULL;
3662 ret = XOBNEW (&parser_obstack, struct c_type_name);
3663 ret->specs = specs;
3664 ret->declarator = declarator;
3665 return ret;
3668 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
3670 initializer:
3671 assignment-expression
3672 { initializer-list }
3673 { initializer-list , }
3675 initializer-list:
3676 designation[opt] initializer
3677 initializer-list , designation[opt] initializer
3679 designation:
3680 designator-list =
3682 designator-list:
3683 designator
3684 designator-list designator
3686 designator:
3687 array-designator
3688 . identifier
3690 array-designator:
3691 [ constant-expression ]
3693 GNU extensions:
3695 initializer:
3698 designation:
3699 array-designator
3700 identifier :
3702 array-designator:
3703 [ constant-expression ... constant-expression ]
3705 Any expression without commas is accepted in the syntax for the
3706 constant-expressions, with non-constant expressions rejected later.
3708 This function is only used for top-level initializers; for nested
3709 ones, see c_parser_initval. */
3711 static struct c_expr
3712 c_parser_initializer (c_parser *parser)
3714 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3715 return c_parser_braced_init (parser, NULL_TREE, false);
3716 else
3718 struct c_expr ret;
3719 location_t loc = c_parser_peek_token (parser)->location;
3720 ret = c_parser_expr_no_commas (parser, NULL);
3721 if (TREE_CODE (ret.value) != STRING_CST
3722 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
3723 ret = default_function_array_read_conversion (loc, ret);
3724 return ret;
3728 /* Parse a braced initializer list. TYPE is the type specified for a
3729 compound literal, and NULL_TREE for other initializers and for
3730 nested braced lists. NESTED_P is true for nested braced lists,
3731 false for the list of a compound literal or the list that is the
3732 top-level initializer in a declaration. */
3734 static struct c_expr
3735 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
3737 struct c_expr ret;
3738 struct obstack braced_init_obstack;
3739 location_t brace_loc = c_parser_peek_token (parser)->location;
3740 gcc_obstack_init (&braced_init_obstack);
3741 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
3742 c_parser_consume_token (parser);
3743 if (nested_p)
3744 push_init_level (0, &braced_init_obstack);
3745 else
3746 really_start_incremental_init (type);
3747 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3749 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
3751 else
3753 /* Parse a non-empty initializer list, possibly with a trailing
3754 comma. */
3755 while (true)
3757 c_parser_initelt (parser, &braced_init_obstack);
3758 if (parser->error)
3759 break;
3760 if (c_parser_next_token_is (parser, CPP_COMMA))
3761 c_parser_consume_token (parser);
3762 else
3763 break;
3764 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3765 break;
3768 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
3770 ret.value = error_mark_node;
3771 ret.original_code = ERROR_MARK;
3772 ret.original_type = NULL;
3773 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
3774 pop_init_level (0, &braced_init_obstack);
3775 obstack_free (&braced_init_obstack, NULL);
3776 return ret;
3778 c_parser_consume_token (parser);
3779 ret = pop_init_level (0, &braced_init_obstack);
3780 obstack_free (&braced_init_obstack, NULL);
3781 return ret;
3784 /* Parse a nested initializer, including designators. */
3786 static void
3787 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
3789 /* Parse any designator or designator list. A single array
3790 designator may have the subsequent "=" omitted in GNU C, but a
3791 longer list or a structure member designator may not. */
3792 if (c_parser_next_token_is (parser, CPP_NAME)
3793 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
3795 /* Old-style structure member designator. */
3796 set_init_label (c_parser_peek_token (parser)->value,
3797 braced_init_obstack);
3798 /* Use the colon as the error location. */
3799 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
3800 "obsolete use of designated initializer with %<:%>");
3801 c_parser_consume_token (parser);
3802 c_parser_consume_token (parser);
3804 else
3806 /* des_seen is 0 if there have been no designators, 1 if there
3807 has been a single array designator and 2 otherwise. */
3808 int des_seen = 0;
3809 /* Location of a designator. */
3810 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3811 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
3812 || c_parser_next_token_is (parser, CPP_DOT))
3814 int des_prev = des_seen;
3815 if (!des_seen)
3816 des_loc = c_parser_peek_token (parser)->location;
3817 if (des_seen < 2)
3818 des_seen++;
3819 if (c_parser_next_token_is (parser, CPP_DOT))
3821 des_seen = 2;
3822 c_parser_consume_token (parser);
3823 if (c_parser_next_token_is (parser, CPP_NAME))
3825 set_init_label (c_parser_peek_token (parser)->value,
3826 braced_init_obstack);
3827 c_parser_consume_token (parser);
3829 else
3831 struct c_expr init;
3832 init.value = error_mark_node;
3833 init.original_code = ERROR_MARK;
3834 init.original_type = NULL;
3835 c_parser_error (parser, "expected identifier");
3836 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3837 process_init_element (init, false, braced_init_obstack);
3838 return;
3841 else
3843 tree first, second;
3844 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
3845 /* ??? Following the old parser, [ objc-receiver
3846 objc-message-args ] is accepted as an initializer,
3847 being distinguished from a designator by what follows
3848 the first assignment expression inside the square
3849 brackets, but after a first array designator a
3850 subsequent square bracket is for Objective-C taken to
3851 start an expression, using the obsolete form of
3852 designated initializer without '=', rather than
3853 possibly being a second level of designation: in LALR
3854 terms, the '[' is shifted rather than reducing
3855 designator to designator-list. */
3856 if (des_prev == 1 && c_dialect_objc ())
3858 des_seen = des_prev;
3859 break;
3861 if (des_prev == 0 && c_dialect_objc ())
3863 /* This might be an array designator or an
3864 Objective-C message expression. If the former,
3865 continue parsing here; if the latter, parse the
3866 remainder of the initializer given the starting
3867 primary-expression. ??? It might make sense to
3868 distinguish when des_prev == 1 as well; see
3869 previous comment. */
3870 tree rec, args;
3871 struct c_expr mexpr;
3872 c_parser_consume_token (parser);
3873 if (c_parser_peek_token (parser)->type == CPP_NAME
3874 && ((c_parser_peek_token (parser)->id_kind
3875 == C_ID_TYPENAME)
3876 || (c_parser_peek_token (parser)->id_kind
3877 == C_ID_CLASSNAME)))
3879 /* Type name receiver. */
3880 tree id = c_parser_peek_token (parser)->value;
3881 c_parser_consume_token (parser);
3882 rec = objc_get_class_reference (id);
3883 goto parse_message_args;
3885 first = c_parser_expr_no_commas (parser, NULL).value;
3886 mark_exp_read (first);
3887 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
3888 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3889 goto array_desig_after_first;
3890 /* Expression receiver. So far only one part
3891 without commas has been parsed; there might be
3892 more of the expression. */
3893 rec = first;
3894 while (c_parser_next_token_is (parser, CPP_COMMA))
3896 struct c_expr next;
3897 location_t comma_loc, exp_loc;
3898 comma_loc = c_parser_peek_token (parser)->location;
3899 c_parser_consume_token (parser);
3900 exp_loc = c_parser_peek_token (parser)->location;
3901 next = c_parser_expr_no_commas (parser, NULL);
3902 next = default_function_array_read_conversion (exp_loc,
3903 next);
3904 rec = build_compound_expr (comma_loc, rec, next.value);
3906 parse_message_args:
3907 /* Now parse the objc-message-args. */
3908 args = c_parser_objc_message_args (parser);
3909 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3910 "expected %<]%>");
3911 mexpr.value
3912 = objc_build_message_expr (rec, args);
3913 mexpr.original_code = ERROR_MARK;
3914 mexpr.original_type = NULL;
3915 /* Now parse and process the remainder of the
3916 initializer, starting with this message
3917 expression as a primary-expression. */
3918 c_parser_initval (parser, &mexpr, braced_init_obstack);
3919 return;
3921 c_parser_consume_token (parser);
3922 first = c_parser_expr_no_commas (parser, NULL).value;
3923 mark_exp_read (first);
3924 array_desig_after_first:
3925 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3927 ellipsis_loc = c_parser_peek_token (parser)->location;
3928 c_parser_consume_token (parser);
3929 second = c_parser_expr_no_commas (parser, NULL).value;
3930 mark_exp_read (second);
3932 else
3933 second = NULL_TREE;
3934 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3936 c_parser_consume_token (parser);
3937 set_init_index (first, second, braced_init_obstack);
3938 if (second)
3939 pedwarn (ellipsis_loc, OPT_Wpedantic,
3940 "ISO C forbids specifying range of elements to initialize");
3942 else
3943 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3944 "expected %<]%>");
3947 if (des_seen >= 1)
3949 if (c_parser_next_token_is (parser, CPP_EQ))
3951 if (!flag_isoc99)
3952 pedwarn (des_loc, OPT_Wpedantic,
3953 "ISO C90 forbids specifying subobject to initialize");
3954 c_parser_consume_token (parser);
3956 else
3958 if (des_seen == 1)
3959 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
3960 "obsolete use of designated initializer without %<=%>");
3961 else
3963 struct c_expr init;
3964 init.value = error_mark_node;
3965 init.original_code = ERROR_MARK;
3966 init.original_type = NULL;
3967 c_parser_error (parser, "expected %<=%>");
3968 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3969 process_init_element (init, false, braced_init_obstack);
3970 return;
3975 c_parser_initval (parser, NULL, braced_init_obstack);
3978 /* Parse a nested initializer; as c_parser_initializer but parses
3979 initializers within braced lists, after any designators have been
3980 applied. If AFTER is not NULL then it is an Objective-C message
3981 expression which is the primary-expression starting the
3982 initializer. */
3984 static void
3985 c_parser_initval (c_parser *parser, struct c_expr *after,
3986 struct obstack * braced_init_obstack)
3988 struct c_expr init;
3989 gcc_assert (!after || c_dialect_objc ());
3990 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
3991 init = c_parser_braced_init (parser, NULL_TREE, true);
3992 else
3994 location_t loc = c_parser_peek_token (parser)->location;
3995 init = c_parser_expr_no_commas (parser, after);
3996 if (init.value != NULL_TREE
3997 && TREE_CODE (init.value) != STRING_CST
3998 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
3999 init = default_function_array_read_conversion (loc, init);
4001 process_init_element (init, false, braced_init_obstack);
4004 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4005 C99 6.8.2).
4007 compound-statement:
4008 { block-item-list[opt] }
4009 { label-declarations block-item-list }
4011 block-item-list:
4012 block-item
4013 block-item-list block-item
4015 block-item:
4016 nested-declaration
4017 statement
4019 nested-declaration:
4020 declaration
4022 GNU extensions:
4024 compound-statement:
4025 { label-declarations block-item-list }
4027 nested-declaration:
4028 __extension__ nested-declaration
4029 nested-function-definition
4031 label-declarations:
4032 label-declaration
4033 label-declarations label-declaration
4035 label-declaration:
4036 __label__ identifier-list ;
4038 Allowing the mixing of declarations and code is new in C99. The
4039 GNU syntax also permits (not shown above) labels at the end of
4040 compound statements, which yield an error. We don't allow labels
4041 on declarations; this might seem like a natural extension, but
4042 there would be a conflict between attributes on the label and
4043 prefix attributes on the declaration. ??? The syntax follows the
4044 old parser in requiring something after label declarations.
4045 Although they are erroneous if the labels declared aren't defined,
4046 is it useful for the syntax to be this way?
4048 OpenMP:
4050 block-item:
4051 openmp-directive
4053 openmp-directive:
4054 barrier-directive
4055 flush-directive */
4057 static tree
4058 c_parser_compound_statement (c_parser *parser)
4060 tree stmt;
4061 location_t brace_loc;
4062 brace_loc = c_parser_peek_token (parser)->location;
4063 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4065 /* Ensure a scope is entered and left anyway to avoid confusion
4066 if we have just prepared to enter a function body. */
4067 stmt = c_begin_compound_stmt (true);
4068 c_end_compound_stmt (brace_loc, stmt, true);
4069 return error_mark_node;
4071 stmt = c_begin_compound_stmt (true);
4072 c_parser_compound_statement_nostart (parser);
4073 return c_end_compound_stmt (brace_loc, stmt, true);
4076 /* Parse a compound statement except for the opening brace. This is
4077 used for parsing both compound statements and statement expressions
4078 (which follow different paths to handling the opening). */
4080 static void
4081 c_parser_compound_statement_nostart (c_parser *parser)
4083 bool last_stmt = false;
4084 bool last_label = false;
4085 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4086 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4087 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4089 c_parser_consume_token (parser);
4090 return;
4092 mark_valid_location_for_stdc_pragma (true);
4093 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4095 /* Read zero or more forward-declarations for labels that nested
4096 functions can jump to. */
4097 mark_valid_location_for_stdc_pragma (false);
4098 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4100 label_loc = c_parser_peek_token (parser)->location;
4101 c_parser_consume_token (parser);
4102 /* Any identifiers, including those declared as type names,
4103 are OK here. */
4104 while (true)
4106 tree label;
4107 if (c_parser_next_token_is_not (parser, CPP_NAME))
4109 c_parser_error (parser, "expected identifier");
4110 break;
4112 label
4113 = declare_label (c_parser_peek_token (parser)->value);
4114 C_DECLARED_LABEL_FLAG (label) = 1;
4115 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4116 c_parser_consume_token (parser);
4117 if (c_parser_next_token_is (parser, CPP_COMMA))
4118 c_parser_consume_token (parser);
4119 else
4120 break;
4122 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4124 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4126 /* We must now have at least one statement, label or declaration. */
4127 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4129 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4130 c_parser_error (parser, "expected declaration or statement");
4131 c_parser_consume_token (parser);
4132 return;
4134 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4136 location_t loc = c_parser_peek_token (parser)->location;
4137 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4138 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4139 || (c_parser_next_token_is (parser, CPP_NAME)
4140 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4142 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4143 label_loc = c_parser_peek_2nd_token (parser)->location;
4144 else
4145 label_loc = c_parser_peek_token (parser)->location;
4146 last_label = true;
4147 last_stmt = false;
4148 mark_valid_location_for_stdc_pragma (false);
4149 c_parser_label (parser);
4151 else if (!last_label
4152 && c_parser_next_tokens_start_declaration (parser))
4154 last_label = false;
4155 mark_valid_location_for_stdc_pragma (false);
4156 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
4157 if (last_stmt)
4158 pedwarn_c90 (loc,
4159 (pedantic && !flag_isoc99)
4160 ? OPT_Wpedantic
4161 : OPT_Wdeclaration_after_statement,
4162 "ISO C90 forbids mixed declarations and code");
4163 last_stmt = false;
4165 else if (!last_label
4166 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4168 /* __extension__ can start a declaration, but is also an
4169 unary operator that can start an expression. Consume all
4170 but the last of a possible series of __extension__ to
4171 determine which. */
4172 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4173 && (c_parser_peek_2nd_token (parser)->keyword
4174 == RID_EXTENSION))
4175 c_parser_consume_token (parser);
4176 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4178 int ext;
4179 ext = disable_extension_diagnostics ();
4180 c_parser_consume_token (parser);
4181 last_label = false;
4182 mark_valid_location_for_stdc_pragma (false);
4183 c_parser_declaration_or_fndef (parser, true, true, true, true,
4184 true, NULL);
4185 /* Following the old parser, __extension__ does not
4186 disable this diagnostic. */
4187 restore_extension_diagnostics (ext);
4188 if (last_stmt)
4189 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4190 ? OPT_Wpedantic
4191 : OPT_Wdeclaration_after_statement,
4192 "ISO C90 forbids mixed declarations and code");
4193 last_stmt = false;
4195 else
4196 goto statement;
4198 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4200 /* External pragmas, and some omp pragmas, are not associated
4201 with regular c code, and so are not to be considered statements
4202 syntactically. This ensures that the user doesn't put them
4203 places that would turn into syntax errors if the directive
4204 were ignored. */
4205 if (c_parser_pragma (parser, pragma_compound))
4206 last_label = false, last_stmt = true;
4208 else if (c_parser_next_token_is (parser, CPP_EOF))
4210 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4211 c_parser_error (parser, "expected declaration or statement");
4212 return;
4214 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4216 if (parser->in_if_block)
4218 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4219 error_at (loc, """expected %<}%> before %<else%>");
4220 return;
4222 else
4224 error_at (loc, "%<else%> without a previous %<if%>");
4225 c_parser_consume_token (parser);
4226 continue;
4229 else
4231 statement:
4232 last_label = false;
4233 last_stmt = true;
4234 mark_valid_location_for_stdc_pragma (false);
4235 c_parser_statement_after_labels (parser);
4238 parser->error = false;
4240 if (last_label)
4241 error_at (label_loc, "label at end of compound statement");
4242 c_parser_consume_token (parser);
4243 /* Restore the value we started with. */
4244 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4247 /* Parse a label (C90 6.6.1, C99 6.8.1).
4249 label:
4250 identifier : attributes[opt]
4251 case constant-expression :
4252 default :
4254 GNU extensions:
4256 label:
4257 case constant-expression ... constant-expression :
4259 The use of attributes on labels is a GNU extension. The syntax in
4260 GNU C accepts any expressions without commas, non-constant
4261 expressions being rejected later. */
4263 static void
4264 c_parser_label (c_parser *parser)
4266 location_t loc1 = c_parser_peek_token (parser)->location;
4267 tree label = NULL_TREE;
4268 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4270 tree exp1, exp2;
4271 c_parser_consume_token (parser);
4272 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4273 if (c_parser_next_token_is (parser, CPP_COLON))
4275 c_parser_consume_token (parser);
4276 label = do_case (loc1, exp1, NULL_TREE);
4278 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4280 c_parser_consume_token (parser);
4281 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4282 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4283 label = do_case (loc1, exp1, exp2);
4285 else
4286 c_parser_error (parser, "expected %<:%> or %<...%>");
4288 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4290 c_parser_consume_token (parser);
4291 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4292 label = do_case (loc1, NULL_TREE, NULL_TREE);
4294 else
4296 tree name = c_parser_peek_token (parser)->value;
4297 tree tlab;
4298 tree attrs;
4299 location_t loc2 = c_parser_peek_token (parser)->location;
4300 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4301 c_parser_consume_token (parser);
4302 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4303 c_parser_consume_token (parser);
4304 attrs = c_parser_attributes (parser);
4305 tlab = define_label (loc2, name);
4306 if (tlab)
4308 decl_attributes (&tlab, attrs, 0);
4309 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4312 if (label)
4314 if (c_parser_next_tokens_start_declaration (parser))
4316 error_at (c_parser_peek_token (parser)->location,
4317 "a label can only be part of a statement and "
4318 "a declaration is not a statement");
4319 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4320 /*static_assert_ok*/ true,
4321 /*nested*/ true, /*empty_ok*/ false,
4322 /*start_attr_ok*/ true, NULL);
4327 /* Parse a statement (C90 6.6, C99 6.8).
4329 statement:
4330 labeled-statement
4331 compound-statement
4332 expression-statement
4333 selection-statement
4334 iteration-statement
4335 jump-statement
4337 labeled-statement:
4338 label statement
4340 expression-statement:
4341 expression[opt] ;
4343 selection-statement:
4344 if-statement
4345 switch-statement
4347 iteration-statement:
4348 while-statement
4349 do-statement
4350 for-statement
4352 jump-statement:
4353 goto identifier ;
4354 continue ;
4355 break ;
4356 return expression[opt] ;
4358 GNU extensions:
4360 statement:
4361 asm-statement
4363 jump-statement:
4364 goto * expression ;
4366 Objective-C:
4368 statement:
4369 objc-throw-statement
4370 objc-try-catch-statement
4371 objc-synchronized-statement
4373 objc-throw-statement:
4374 @throw expression ;
4375 @throw ;
4377 OpenMP:
4379 statement:
4380 openmp-construct
4382 openmp-construct:
4383 parallel-construct
4384 for-construct
4385 sections-construct
4386 single-construct
4387 parallel-for-construct
4388 parallel-sections-construct
4389 master-construct
4390 critical-construct
4391 atomic-construct
4392 ordered-construct
4394 parallel-construct:
4395 parallel-directive structured-block
4397 for-construct:
4398 for-directive iteration-statement
4400 sections-construct:
4401 sections-directive section-scope
4403 single-construct:
4404 single-directive structured-block
4406 parallel-for-construct:
4407 parallel-for-directive iteration-statement
4409 parallel-sections-construct:
4410 parallel-sections-directive section-scope
4412 master-construct:
4413 master-directive structured-block
4415 critical-construct:
4416 critical-directive structured-block
4418 atomic-construct:
4419 atomic-directive expression-statement
4421 ordered-construct:
4422 ordered-directive structured-block
4424 Transactional Memory:
4426 statement:
4427 transaction-statement
4428 transaction-cancel-statement
4431 static void
4432 c_parser_statement (c_parser *parser)
4434 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4435 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4436 || (c_parser_next_token_is (parser, CPP_NAME)
4437 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4438 c_parser_label (parser);
4439 c_parser_statement_after_labels (parser);
4442 /* Parse a statement, other than a labeled statement. */
4444 static void
4445 c_parser_statement_after_labels (c_parser *parser)
4447 location_t loc = c_parser_peek_token (parser)->location;
4448 tree stmt = NULL_TREE;
4449 bool in_if_block = parser->in_if_block;
4450 parser->in_if_block = false;
4451 switch (c_parser_peek_token (parser)->type)
4453 case CPP_OPEN_BRACE:
4454 add_stmt (c_parser_compound_statement (parser));
4455 break;
4456 case CPP_KEYWORD:
4457 switch (c_parser_peek_token (parser)->keyword)
4459 case RID_IF:
4460 c_parser_if_statement (parser);
4461 break;
4462 case RID_SWITCH:
4463 c_parser_switch_statement (parser);
4464 break;
4465 case RID_WHILE:
4466 c_parser_while_statement (parser);
4467 break;
4468 case RID_DO:
4469 c_parser_do_statement (parser);
4470 break;
4471 case RID_FOR:
4472 c_parser_for_statement (parser);
4473 break;
4474 case RID_GOTO:
4475 c_parser_consume_token (parser);
4476 if (c_parser_next_token_is (parser, CPP_NAME))
4478 stmt = c_finish_goto_label (loc,
4479 c_parser_peek_token (parser)->value);
4480 c_parser_consume_token (parser);
4482 else if (c_parser_next_token_is (parser, CPP_MULT))
4484 tree val;
4486 c_parser_consume_token (parser);
4487 val = c_parser_expression (parser).value;
4488 mark_exp_read (val);
4489 stmt = c_finish_goto_ptr (loc, val);
4491 else
4492 c_parser_error (parser, "expected identifier or %<*%>");
4493 goto expect_semicolon;
4494 case RID_CONTINUE:
4495 c_parser_consume_token (parser);
4496 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4497 goto expect_semicolon;
4498 case RID_BREAK:
4499 c_parser_consume_token (parser);
4500 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4501 goto expect_semicolon;
4502 case RID_RETURN:
4503 c_parser_consume_token (parser);
4504 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4506 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4507 c_parser_consume_token (parser);
4509 else
4511 struct c_expr expr = c_parser_expression_conv (parser);
4512 mark_exp_read (expr.value);
4513 stmt = c_finish_return (loc, expr.value, expr.original_type);
4514 goto expect_semicolon;
4516 break;
4517 case RID_ASM:
4518 stmt = c_parser_asm_statement (parser);
4519 break;
4520 case RID_TRANSACTION_ATOMIC:
4521 case RID_TRANSACTION_RELAXED:
4522 stmt = c_parser_transaction (parser,
4523 c_parser_peek_token (parser)->keyword);
4524 break;
4525 case RID_TRANSACTION_CANCEL:
4526 stmt = c_parser_transaction_cancel (parser);
4527 goto expect_semicolon;
4528 case RID_AT_THROW:
4529 gcc_assert (c_dialect_objc ());
4530 c_parser_consume_token (parser);
4531 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4533 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4534 c_parser_consume_token (parser);
4536 else
4538 tree expr = c_parser_expression (parser).value;
4539 expr = c_fully_fold (expr, false, NULL);
4540 stmt = objc_build_throw_stmt (loc, expr);
4541 goto expect_semicolon;
4543 break;
4544 case RID_AT_TRY:
4545 gcc_assert (c_dialect_objc ());
4546 c_parser_objc_try_catch_finally_statement (parser);
4547 break;
4548 case RID_AT_SYNCHRONIZED:
4549 gcc_assert (c_dialect_objc ());
4550 c_parser_objc_synchronized_statement (parser);
4551 break;
4552 default:
4553 goto expr_stmt;
4555 break;
4556 case CPP_SEMICOLON:
4557 c_parser_consume_token (parser);
4558 break;
4559 case CPP_CLOSE_PAREN:
4560 case CPP_CLOSE_SQUARE:
4561 /* Avoid infinite loop in error recovery:
4562 c_parser_skip_until_found stops at a closing nesting
4563 delimiter without consuming it, but here we need to consume
4564 it to proceed further. */
4565 c_parser_error (parser, "expected statement");
4566 c_parser_consume_token (parser);
4567 break;
4568 case CPP_PRAGMA:
4569 c_parser_pragma (parser, pragma_stmt);
4570 break;
4571 default:
4572 expr_stmt:
4573 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4574 expect_semicolon:
4575 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4576 break;
4578 /* Two cases cannot and do not have line numbers associated: If stmt
4579 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4580 cannot hold line numbers. But that's OK because the statement
4581 will either be changed to a MODIFY_EXPR during gimplification of
4582 the statement expr, or discarded. If stmt was compound, but
4583 without new variables, we will have skipped the creation of a
4584 BIND and will have a bare STATEMENT_LIST. But that's OK because
4585 (recursively) all of the component statements should already have
4586 line numbers assigned. ??? Can we discard no-op statements
4587 earlier? */
4588 if (CAN_HAVE_LOCATION_P (stmt)
4589 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
4590 SET_EXPR_LOCATION (stmt, loc);
4592 parser->in_if_block = in_if_block;
4595 /* Parse the condition from an if, do, while or for statements. */
4597 static tree
4598 c_parser_condition (c_parser *parser)
4600 location_t loc = c_parser_peek_token (parser)->location;
4601 tree cond;
4602 cond = c_parser_expression_conv (parser).value;
4603 cond = c_objc_common_truthvalue_conversion (loc, cond);
4604 cond = c_fully_fold (cond, false, NULL);
4605 if (warn_sequence_point)
4606 verify_sequence_points (cond);
4607 return cond;
4610 /* Parse a parenthesized condition from an if, do or while statement.
4612 condition:
4613 ( expression )
4615 static tree
4616 c_parser_paren_condition (c_parser *parser)
4618 tree cond;
4619 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4620 return error_mark_node;
4621 cond = c_parser_condition (parser);
4622 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4623 return cond;
4626 /* Parse a statement which is a block in C99. */
4628 static tree
4629 c_parser_c99_block_statement (c_parser *parser)
4631 tree block = c_begin_compound_stmt (flag_isoc99);
4632 location_t loc = c_parser_peek_token (parser)->location;
4633 c_parser_statement (parser);
4634 return c_end_compound_stmt (loc, block, flag_isoc99);
4637 /* Parse the body of an if statement. This is just parsing a
4638 statement but (a) it is a block in C99, (b) we track whether the
4639 body is an if statement for the sake of -Wparentheses warnings, (c)
4640 we handle an empty body specially for the sake of -Wempty-body
4641 warnings, and (d) we call parser_compound_statement directly
4642 because c_parser_statement_after_labels resets
4643 parser->in_if_block. */
4645 static tree
4646 c_parser_if_body (c_parser *parser, bool *if_p)
4648 tree block = c_begin_compound_stmt (flag_isoc99);
4649 location_t body_loc = c_parser_peek_token (parser)->location;
4650 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4651 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4652 || (c_parser_next_token_is (parser, CPP_NAME)
4653 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4654 c_parser_label (parser);
4655 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
4656 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4658 location_t loc = c_parser_peek_token (parser)->location;
4659 add_stmt (build_empty_stmt (loc));
4660 c_parser_consume_token (parser);
4661 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
4662 warning_at (loc, OPT_Wempty_body,
4663 "suggest braces around empty body in an %<if%> statement");
4665 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4666 add_stmt (c_parser_compound_statement (parser));
4667 else
4668 c_parser_statement_after_labels (parser);
4669 return c_end_compound_stmt (body_loc, block, flag_isoc99);
4672 /* Parse the else body of an if statement. This is just parsing a
4673 statement but (a) it is a block in C99, (b) we handle an empty body
4674 specially for the sake of -Wempty-body warnings. */
4676 static tree
4677 c_parser_else_body (c_parser *parser)
4679 location_t else_loc = c_parser_peek_token (parser)->location;
4680 tree block = c_begin_compound_stmt (flag_isoc99);
4681 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4682 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4683 || (c_parser_next_token_is (parser, CPP_NAME)
4684 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4685 c_parser_label (parser);
4686 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4688 location_t loc = c_parser_peek_token (parser)->location;
4689 warning_at (loc,
4690 OPT_Wempty_body,
4691 "suggest braces around empty body in an %<else%> statement");
4692 add_stmt (build_empty_stmt (loc));
4693 c_parser_consume_token (parser);
4695 else
4696 c_parser_statement_after_labels (parser);
4697 return c_end_compound_stmt (else_loc, block, flag_isoc99);
4700 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
4702 if-statement:
4703 if ( expression ) statement
4704 if ( expression ) statement else statement
4707 static void
4708 c_parser_if_statement (c_parser *parser)
4710 tree block;
4711 location_t loc;
4712 tree cond;
4713 bool first_if = false;
4714 tree first_body, second_body;
4715 bool in_if_block;
4717 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
4718 c_parser_consume_token (parser);
4719 block = c_begin_compound_stmt (flag_isoc99);
4720 loc = c_parser_peek_token (parser)->location;
4721 cond = c_parser_paren_condition (parser);
4722 in_if_block = parser->in_if_block;
4723 parser->in_if_block = true;
4724 first_body = c_parser_if_body (parser, &first_if);
4725 parser->in_if_block = in_if_block;
4726 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4728 c_parser_consume_token (parser);
4729 second_body = c_parser_else_body (parser);
4731 else
4732 second_body = NULL_TREE;
4733 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
4734 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4737 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
4739 switch-statement:
4740 switch (expression) statement
4743 static void
4744 c_parser_switch_statement (c_parser *parser)
4746 tree block, expr, body, save_break;
4747 location_t switch_loc = c_parser_peek_token (parser)->location;
4748 location_t switch_cond_loc;
4749 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
4750 c_parser_consume_token (parser);
4751 block = c_begin_compound_stmt (flag_isoc99);
4752 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4754 switch_cond_loc = c_parser_peek_token (parser)->location;
4755 expr = c_parser_expression (parser).value;
4756 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
4758 else
4760 switch_cond_loc = UNKNOWN_LOCATION;
4761 expr = error_mark_node;
4763 c_start_case (switch_loc, switch_cond_loc, expr);
4764 save_break = c_break_label;
4765 c_break_label = NULL_TREE;
4766 body = c_parser_c99_block_statement (parser);
4767 c_finish_case (body);
4768 if (c_break_label)
4770 location_t here = c_parser_peek_token (parser)->location;
4771 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
4772 SET_EXPR_LOCATION (t, here);
4773 add_stmt (t);
4775 c_break_label = save_break;
4776 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
4779 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
4781 while-statement:
4782 while (expression) statement
4785 static void
4786 c_parser_while_statement (c_parser *parser)
4788 tree block, cond, body, save_break, save_cont;
4789 location_t loc;
4790 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
4791 c_parser_consume_token (parser);
4792 block = c_begin_compound_stmt (flag_isoc99);
4793 loc = c_parser_peek_token (parser)->location;
4794 cond = c_parser_paren_condition (parser);
4795 save_break = c_break_label;
4796 c_break_label = NULL_TREE;
4797 save_cont = c_cont_label;
4798 c_cont_label = NULL_TREE;
4799 body = c_parser_c99_block_statement (parser);
4800 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
4801 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4802 c_break_label = save_break;
4803 c_cont_label = save_cont;
4806 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
4808 do-statement:
4809 do statement while ( expression ) ;
4812 static void
4813 c_parser_do_statement (c_parser *parser)
4815 tree block, cond, body, save_break, save_cont, new_break, new_cont;
4816 location_t loc;
4817 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
4818 c_parser_consume_token (parser);
4819 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4820 warning_at (c_parser_peek_token (parser)->location,
4821 OPT_Wempty_body,
4822 "suggest braces around empty body in %<do%> statement");
4823 block = c_begin_compound_stmt (flag_isoc99);
4824 loc = c_parser_peek_token (parser)->location;
4825 save_break = c_break_label;
4826 c_break_label = NULL_TREE;
4827 save_cont = c_cont_label;
4828 c_cont_label = NULL_TREE;
4829 body = c_parser_c99_block_statement (parser);
4830 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
4831 new_break = c_break_label;
4832 c_break_label = save_break;
4833 new_cont = c_cont_label;
4834 c_cont_label = save_cont;
4835 cond = c_parser_paren_condition (parser);
4836 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
4837 c_parser_skip_to_end_of_block_or_statement (parser);
4838 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
4839 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
4842 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
4844 for-statement:
4845 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
4846 for ( nested-declaration expression[opt] ; expression[opt] ) statement
4848 The form with a declaration is new in C99.
4850 ??? In accordance with the old parser, the declaration may be a
4851 nested function, which is then rejected in check_for_loop_decls,
4852 but does it make any sense for this to be included in the grammar?
4853 Note in particular that the nested function does not include a
4854 trailing ';', whereas the "declaration" production includes one.
4855 Also, can we reject bad declarations earlier and cheaper than
4856 check_for_loop_decls?
4858 In Objective-C, there are two additional variants:
4860 foreach-statement:
4861 for ( expression in expresssion ) statement
4862 for ( declaration in expression ) statement
4864 This is inconsistent with C, because the second variant is allowed
4865 even if c99 is not enabled.
4867 The rest of the comment documents these Objective-C foreach-statement.
4869 Here is the canonical example of the first variant:
4870 for (object in array) { do something with object }
4871 we call the first expression ("object") the "object_expression" and
4872 the second expression ("array") the "collection_expression".
4873 object_expression must be an lvalue of type "id" (a generic Objective-C
4874 object) because the loop works by assigning to object_expression the
4875 various objects from the collection_expression. collection_expression
4876 must evaluate to something of type "id" which responds to the method
4877 countByEnumeratingWithState:objects:count:.
4879 The canonical example of the second variant is:
4880 for (id object in array) { do something with object }
4881 which is completely equivalent to
4883 id object;
4884 for (object in array) { do something with object }
4886 Note that initizializing 'object' in some way (eg, "for ((object =
4887 xxx) in array) { do something with object }") is possibly
4888 technically valid, but completely pointless as 'object' will be
4889 assigned to something else as soon as the loop starts. We should
4890 most likely reject it (TODO).
4892 The beginning of the Objective-C foreach-statement looks exactly
4893 like the beginning of the for-statement, and we can tell it is a
4894 foreach-statement only because the initial declaration or
4895 expression is terminated by 'in' instead of ';'.
4898 static void
4899 c_parser_for_statement (c_parser *parser)
4901 tree block, cond, incr, save_break, save_cont, body;
4902 /* The following are only used when parsing an ObjC foreach statement. */
4903 tree object_expression;
4904 /* Silence the bogus uninitialized warning. */
4905 tree collection_expression = NULL;
4906 location_t loc = c_parser_peek_token (parser)->location;
4907 location_t for_loc = c_parser_peek_token (parser)->location;
4908 bool is_foreach_statement = false;
4909 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
4910 c_parser_consume_token (parser);
4911 /* Open a compound statement in Objective-C as well, just in case this is
4912 as foreach expression. */
4913 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
4914 cond = error_mark_node;
4915 incr = error_mark_node;
4916 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4918 /* Parse the initialization declaration or expression. */
4919 object_expression = error_mark_node;
4920 parser->objc_could_be_foreach_context = c_dialect_objc ();
4921 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4923 parser->objc_could_be_foreach_context = false;
4924 c_parser_consume_token (parser);
4925 c_finish_expr_stmt (loc, NULL_TREE);
4927 else if (c_parser_next_tokens_start_declaration (parser))
4929 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
4930 &object_expression);
4931 parser->objc_could_be_foreach_context = false;
4933 if (c_parser_next_token_is_keyword (parser, RID_IN))
4935 c_parser_consume_token (parser);
4936 is_foreach_statement = true;
4937 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4938 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4940 else
4941 check_for_loop_decls (for_loc, flag_isoc99);
4943 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4945 /* __extension__ can start a declaration, but is also an
4946 unary operator that can start an expression. Consume all
4947 but the last of a possible series of __extension__ to
4948 determine which. */
4949 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4950 && (c_parser_peek_2nd_token (parser)->keyword
4951 == RID_EXTENSION))
4952 c_parser_consume_token (parser);
4953 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4955 int ext;
4956 ext = disable_extension_diagnostics ();
4957 c_parser_consume_token (parser);
4958 c_parser_declaration_or_fndef (parser, true, true, true, true,
4959 true, &object_expression);
4960 parser->objc_could_be_foreach_context = false;
4962 restore_extension_diagnostics (ext);
4963 if (c_parser_next_token_is_keyword (parser, RID_IN))
4965 c_parser_consume_token (parser);
4966 is_foreach_statement = true;
4967 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
4968 c_parser_error (parser, "multiple iterating variables in fast enumeration");
4970 else
4971 check_for_loop_decls (for_loc, flag_isoc99);
4973 else
4974 goto init_expr;
4976 else
4978 init_expr:
4980 tree init_expression;
4981 init_expression = c_parser_expression (parser).value;
4982 parser->objc_could_be_foreach_context = false;
4983 if (c_parser_next_token_is_keyword (parser, RID_IN))
4985 c_parser_consume_token (parser);
4986 is_foreach_statement = true;
4987 if (! lvalue_p (init_expression))
4988 c_parser_error (parser, "invalid iterating variable in fast enumeration");
4989 object_expression = c_fully_fold (init_expression, false, NULL);
4991 else
4993 c_finish_expr_stmt (loc, init_expression);
4994 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4998 /* Parse the loop condition. In the case of a foreach
4999 statement, there is no loop condition. */
5000 gcc_assert (!parser->objc_could_be_foreach_context);
5001 if (!is_foreach_statement)
5003 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5005 c_parser_consume_token (parser);
5006 cond = NULL_TREE;
5008 else
5010 cond = c_parser_condition (parser);
5011 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5014 /* Parse the increment expression (the third expression in a
5015 for-statement). In the case of a foreach-statement, this is
5016 the expression that follows the 'in'. */
5017 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5019 if (is_foreach_statement)
5021 c_parser_error (parser, "missing collection in fast enumeration");
5022 collection_expression = error_mark_node;
5024 else
5025 incr = c_process_expr_stmt (loc, NULL_TREE);
5027 else
5029 if (is_foreach_statement)
5030 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5031 false, NULL);
5032 else
5033 incr = c_process_expr_stmt (loc, c_parser_expression (parser).value);
5035 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5037 save_break = c_break_label;
5038 c_break_label = NULL_TREE;
5039 save_cont = c_cont_label;
5040 c_cont_label = NULL_TREE;
5041 body = c_parser_c99_block_statement (parser);
5042 if (is_foreach_statement)
5043 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5044 else
5045 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5046 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5047 c_break_label = save_break;
5048 c_cont_label = save_cont;
5051 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5052 statement with inputs, outputs, clobbers, and volatile tag
5053 allowed.
5055 asm-statement:
5056 asm type-qualifier[opt] ( asm-argument ) ;
5057 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5059 asm-argument:
5060 asm-string-literal
5061 asm-string-literal : asm-operands[opt]
5062 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5063 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5065 asm-goto-argument:
5066 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5067 : asm-goto-operands
5069 Qualifiers other than volatile are accepted in the syntax but
5070 warned for. */
5072 static tree
5073 c_parser_asm_statement (c_parser *parser)
5075 tree quals, str, outputs, inputs, clobbers, labels, ret;
5076 bool simple, is_goto;
5077 location_t asm_loc = c_parser_peek_token (parser)->location;
5078 int section, nsections;
5080 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5081 c_parser_consume_token (parser);
5082 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5084 quals = c_parser_peek_token (parser)->value;
5085 c_parser_consume_token (parser);
5087 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5088 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5090 warning_at (c_parser_peek_token (parser)->location,
5092 "%E qualifier ignored on asm",
5093 c_parser_peek_token (parser)->value);
5094 quals = NULL_TREE;
5095 c_parser_consume_token (parser);
5097 else
5098 quals = NULL_TREE;
5100 is_goto = false;
5101 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5103 c_parser_consume_token (parser);
5104 is_goto = true;
5107 /* ??? Follow the C++ parser rather than using the
5108 lex_untranslated_string kludge. */
5109 parser->lex_untranslated_string = true;
5110 ret = NULL;
5112 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5113 goto error;
5115 str = c_parser_asm_string_literal (parser);
5116 if (str == NULL_TREE)
5117 goto error_close_paren;
5119 simple = true;
5120 outputs = NULL_TREE;
5121 inputs = NULL_TREE;
5122 clobbers = NULL_TREE;
5123 labels = NULL_TREE;
5125 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5126 goto done_asm;
5128 /* Parse each colon-delimited section of operands. */
5129 nsections = 3 + is_goto;
5130 for (section = 0; section < nsections; ++section)
5132 if (!c_parser_require (parser, CPP_COLON,
5133 is_goto
5134 ? "expected %<:%>"
5135 : "expected %<:%> or %<)%>"))
5136 goto error_close_paren;
5138 /* Once past any colon, we're no longer a simple asm. */
5139 simple = false;
5141 if ((!c_parser_next_token_is (parser, CPP_COLON)
5142 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5143 || section == 3)
5144 switch (section)
5146 case 0:
5147 /* For asm goto, we don't allow output operands, but reserve
5148 the slot for a future extension that does allow them. */
5149 if (!is_goto)
5150 outputs = c_parser_asm_operands (parser, false);
5151 break;
5152 case 1:
5153 inputs = c_parser_asm_operands (parser, true);
5154 break;
5155 case 2:
5156 clobbers = c_parser_asm_clobbers (parser);
5157 break;
5158 case 3:
5159 labels = c_parser_asm_goto_operands (parser);
5160 break;
5161 default:
5162 gcc_unreachable ();
5165 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5166 goto done_asm;
5169 done_asm:
5170 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5172 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5173 goto error;
5176 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5177 c_parser_skip_to_end_of_block_or_statement (parser);
5179 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5180 clobbers, labels, simple));
5182 error:
5183 parser->lex_untranslated_string = false;
5184 return ret;
5186 error_close_paren:
5187 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5188 goto error;
5191 /* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but
5192 not outputs), apply the default conversion of functions and arrays
5193 to pointers.
5195 asm-operands:
5196 asm-operand
5197 asm-operands , asm-operand
5199 asm-operand:
5200 asm-string-literal ( expression )
5201 [ identifier ] asm-string-literal ( expression )
5204 static tree
5205 c_parser_asm_operands (c_parser *parser, bool convert_p)
5207 tree list = NULL_TREE;
5208 location_t loc;
5209 while (true)
5211 tree name, str;
5212 struct c_expr expr;
5213 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5215 c_parser_consume_token (parser);
5216 if (c_parser_next_token_is (parser, CPP_NAME))
5218 tree id = c_parser_peek_token (parser)->value;
5219 c_parser_consume_token (parser);
5220 name = build_string (IDENTIFIER_LENGTH (id),
5221 IDENTIFIER_POINTER (id));
5223 else
5225 c_parser_error (parser, "expected identifier");
5226 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5227 return NULL_TREE;
5229 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5230 "expected %<]%>");
5232 else
5233 name = NULL_TREE;
5234 str = c_parser_asm_string_literal (parser);
5235 if (str == NULL_TREE)
5236 return NULL_TREE;
5237 parser->lex_untranslated_string = false;
5238 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5240 parser->lex_untranslated_string = true;
5241 return NULL_TREE;
5243 loc = c_parser_peek_token (parser)->location;
5244 expr = c_parser_expression (parser);
5245 mark_exp_read (expr.value);
5246 if (convert_p)
5247 expr = default_function_array_conversion (loc, expr);
5248 expr.value = c_fully_fold (expr.value, false, NULL);
5249 parser->lex_untranslated_string = true;
5250 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5252 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5253 return NULL_TREE;
5255 list = chainon (list, build_tree_list (build_tree_list (name, str),
5256 expr.value));
5257 if (c_parser_next_token_is (parser, CPP_COMMA))
5258 c_parser_consume_token (parser);
5259 else
5260 break;
5262 return list;
5265 /* Parse asm clobbers, a GNU extension.
5267 asm-clobbers:
5268 asm-string-literal
5269 asm-clobbers , asm-string-literal
5272 static tree
5273 c_parser_asm_clobbers (c_parser *parser)
5275 tree list = NULL_TREE;
5276 while (true)
5278 tree str = c_parser_asm_string_literal (parser);
5279 if (str)
5280 list = tree_cons (NULL_TREE, str, list);
5281 else
5282 return NULL_TREE;
5283 if (c_parser_next_token_is (parser, CPP_COMMA))
5284 c_parser_consume_token (parser);
5285 else
5286 break;
5288 return list;
5291 /* Parse asm goto labels, a GNU extension.
5293 asm-goto-operands:
5294 identifier
5295 asm-goto-operands , identifier
5298 static tree
5299 c_parser_asm_goto_operands (c_parser *parser)
5301 tree list = NULL_TREE;
5302 while (true)
5304 tree name, label;
5306 if (c_parser_next_token_is (parser, CPP_NAME))
5308 c_token *tok = c_parser_peek_token (parser);
5309 name = tok->value;
5310 label = lookup_label_for_goto (tok->location, name);
5311 c_parser_consume_token (parser);
5312 TREE_USED (label) = 1;
5314 else
5316 c_parser_error (parser, "expected identifier");
5317 return NULL_TREE;
5320 name = build_string (IDENTIFIER_LENGTH (name),
5321 IDENTIFIER_POINTER (name));
5322 list = tree_cons (name, label, list);
5323 if (c_parser_next_token_is (parser, CPP_COMMA))
5324 c_parser_consume_token (parser);
5325 else
5326 return nreverse (list);
5330 /* Parse an expression other than a compound expression; that is, an
5331 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5332 NULL then it is an Objective-C message expression which is the
5333 primary-expression starting the expression as an initializer.
5335 assignment-expression:
5336 conditional-expression
5337 unary-expression assignment-operator assignment-expression
5339 assignment-operator: one of
5340 = *= /= %= += -= <<= >>= &= ^= |=
5342 In GNU C we accept any conditional expression on the LHS and
5343 diagnose the invalid lvalue rather than producing a syntax
5344 error. */
5346 static struct c_expr
5347 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after)
5349 struct c_expr lhs, rhs, ret;
5350 enum tree_code code;
5351 location_t op_location, exp_location;
5352 gcc_assert (!after || c_dialect_objc ());
5353 lhs = c_parser_conditional_expression (parser, after);
5354 op_location = c_parser_peek_token (parser)->location;
5355 switch (c_parser_peek_token (parser)->type)
5357 case CPP_EQ:
5358 code = NOP_EXPR;
5359 break;
5360 case CPP_MULT_EQ:
5361 code = MULT_EXPR;
5362 break;
5363 case CPP_DIV_EQ:
5364 code = TRUNC_DIV_EXPR;
5365 break;
5366 case CPP_MOD_EQ:
5367 code = TRUNC_MOD_EXPR;
5368 break;
5369 case CPP_PLUS_EQ:
5370 code = PLUS_EXPR;
5371 break;
5372 case CPP_MINUS_EQ:
5373 code = MINUS_EXPR;
5374 break;
5375 case CPP_LSHIFT_EQ:
5376 code = LSHIFT_EXPR;
5377 break;
5378 case CPP_RSHIFT_EQ:
5379 code = RSHIFT_EXPR;
5380 break;
5381 case CPP_AND_EQ:
5382 code = BIT_AND_EXPR;
5383 break;
5384 case CPP_XOR_EQ:
5385 code = BIT_XOR_EXPR;
5386 break;
5387 case CPP_OR_EQ:
5388 code = BIT_IOR_EXPR;
5389 break;
5390 default:
5391 return lhs;
5393 c_parser_consume_token (parser);
5394 exp_location = c_parser_peek_token (parser)->location;
5395 rhs = c_parser_expr_no_commas (parser, NULL);
5396 rhs = default_function_array_read_conversion (exp_location, rhs);
5397 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5398 code, exp_location, rhs.value,
5399 rhs.original_type);
5400 if (code == NOP_EXPR)
5401 ret.original_code = MODIFY_EXPR;
5402 else
5404 TREE_NO_WARNING (ret.value) = 1;
5405 ret.original_code = ERROR_MARK;
5407 ret.original_type = NULL;
5408 return ret;
5411 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5412 is not NULL then it is an Objective-C message expression which is
5413 the primary-expression starting the expression as an initializer.
5415 conditional-expression:
5416 logical-OR-expression
5417 logical-OR-expression ? expression : conditional-expression
5419 GNU extensions:
5421 conditional-expression:
5422 logical-OR-expression ? : conditional-expression
5425 static struct c_expr
5426 c_parser_conditional_expression (c_parser *parser, struct c_expr *after)
5428 struct c_expr cond, exp1, exp2, ret;
5429 location_t cond_loc, colon_loc, middle_loc;
5431 gcc_assert (!after || c_dialect_objc ());
5433 cond = c_parser_binary_expression (parser, after, PREC_NONE);
5435 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5436 return cond;
5437 cond_loc = c_parser_peek_token (parser)->location;
5438 cond = default_function_array_read_conversion (cond_loc, cond);
5439 c_parser_consume_token (parser);
5440 if (c_parser_next_token_is (parser, CPP_COLON))
5442 tree eptype = NULL_TREE;
5444 middle_loc = c_parser_peek_token (parser)->location;
5445 pedwarn (middle_loc, OPT_Wpedantic,
5446 "ISO C forbids omitting the middle term of a ?: expression");
5447 warn_for_omitted_condop (middle_loc, cond.value);
5448 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5450 eptype = TREE_TYPE (cond.value);
5451 cond.value = TREE_OPERAND (cond.value, 0);
5453 /* Make sure first operand is calculated only once. */
5454 exp1.value = c_save_expr (default_conversion (cond.value));
5455 if (eptype)
5456 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5457 exp1.original_type = NULL;
5458 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5459 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5461 else
5463 cond.value
5464 = c_objc_common_truthvalue_conversion
5465 (cond_loc, default_conversion (cond.value));
5466 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5467 exp1 = c_parser_expression_conv (parser);
5468 mark_exp_read (exp1.value);
5469 c_inhibit_evaluation_warnings +=
5470 ((cond.value == truthvalue_true_node)
5471 - (cond.value == truthvalue_false_node));
5474 colon_loc = c_parser_peek_token (parser)->location;
5475 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5477 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5478 ret.value = error_mark_node;
5479 ret.original_code = ERROR_MARK;
5480 ret.original_type = NULL;
5481 return ret;
5484 location_t exp2_loc = c_parser_peek_token (parser)->location;
5485 exp2 = c_parser_conditional_expression (parser, NULL);
5486 exp2 = default_function_array_read_conversion (exp2_loc, exp2);
5488 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5489 ret.value = build_conditional_expr (colon_loc, cond.value,
5490 cond.original_code == C_MAYBE_CONST_EXPR,
5491 exp1.value, exp1.original_type,
5492 exp2.value, exp2.original_type);
5493 ret.original_code = ERROR_MARK;
5494 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5495 ret.original_type = NULL;
5496 else
5498 tree t1, t2;
5500 /* If both sides are enum type, the default conversion will have
5501 made the type of the result be an integer type. We want to
5502 remember the enum types we started with. */
5503 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5504 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5505 ret.original_type = ((t1 != error_mark_node
5506 && t2 != error_mark_node
5507 && (TYPE_MAIN_VARIANT (t1)
5508 == TYPE_MAIN_VARIANT (t2)))
5509 ? t1
5510 : NULL);
5512 return ret;
5515 /* Parse a binary expression; that is, a logical-OR-expression (C90
5516 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5517 an Objective-C message expression which is the primary-expression
5518 starting the expression as an initializer. PREC is the starting
5519 precedence, usually PREC_NONE.
5521 multiplicative-expression:
5522 cast-expression
5523 multiplicative-expression * cast-expression
5524 multiplicative-expression / cast-expression
5525 multiplicative-expression % cast-expression
5527 additive-expression:
5528 multiplicative-expression
5529 additive-expression + multiplicative-expression
5530 additive-expression - multiplicative-expression
5532 shift-expression:
5533 additive-expression
5534 shift-expression << additive-expression
5535 shift-expression >> additive-expression
5537 relational-expression:
5538 shift-expression
5539 relational-expression < shift-expression
5540 relational-expression > shift-expression
5541 relational-expression <= shift-expression
5542 relational-expression >= shift-expression
5544 equality-expression:
5545 relational-expression
5546 equality-expression == relational-expression
5547 equality-expression != relational-expression
5549 AND-expression:
5550 equality-expression
5551 AND-expression & equality-expression
5553 exclusive-OR-expression:
5554 AND-expression
5555 exclusive-OR-expression ^ AND-expression
5557 inclusive-OR-expression:
5558 exclusive-OR-expression
5559 inclusive-OR-expression | exclusive-OR-expression
5561 logical-AND-expression:
5562 inclusive-OR-expression
5563 logical-AND-expression && inclusive-OR-expression
5565 logical-OR-expression:
5566 logical-AND-expression
5567 logical-OR-expression || logical-AND-expression
5570 static struct c_expr
5571 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
5572 enum c_parser_prec prec)
5574 /* A binary expression is parsed using operator-precedence parsing,
5575 with the operands being cast expressions. All the binary
5576 operators are left-associative. Thus a binary expression is of
5577 form:
5579 E0 op1 E1 op2 E2 ...
5581 which we represent on a stack. On the stack, the precedence
5582 levels are strictly increasing. When a new operator is
5583 encountered of higher precedence than that at the top of the
5584 stack, it is pushed; its LHS is the top expression, and its RHS
5585 is everything parsed until it is popped. When a new operator is
5586 encountered with precedence less than or equal to that at the top
5587 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
5588 by the result of the operation until the operator at the top of
5589 the stack has lower precedence than the new operator or there is
5590 only one element on the stack; then the top expression is the LHS
5591 of the new operator. In the case of logical AND and OR
5592 expressions, we also need to adjust c_inhibit_evaluation_warnings
5593 as appropriate when the operators are pushed and popped. */
5595 struct {
5596 /* The expression at this stack level. */
5597 struct c_expr expr;
5598 /* The precedence of the operator on its left, PREC_NONE at the
5599 bottom of the stack. */
5600 enum c_parser_prec prec;
5601 /* The operation on its left. */
5602 enum tree_code op;
5603 /* The source location of this operation. */
5604 location_t loc;
5605 } stack[NUM_PRECS];
5606 int sp;
5607 /* Location of the binary operator. */
5608 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5609 #define POP \
5610 do { \
5611 switch (stack[sp].op) \
5613 case TRUTH_ANDIF_EXPR: \
5614 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5615 == truthvalue_false_node); \
5616 break; \
5617 case TRUTH_ORIF_EXPR: \
5618 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
5619 == truthvalue_true_node); \
5620 break; \
5621 default: \
5622 break; \
5624 stack[sp - 1].expr \
5625 = default_function_array_read_conversion (stack[sp - 1].loc, \
5626 stack[sp - 1].expr); \
5627 stack[sp].expr \
5628 = default_function_array_read_conversion (stack[sp].loc, \
5629 stack[sp].expr); \
5630 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
5631 stack[sp].op, \
5632 stack[sp - 1].expr, \
5633 stack[sp].expr); \
5634 sp--; \
5635 } while (0)
5636 gcc_assert (!after || c_dialect_objc ());
5637 stack[0].loc = c_parser_peek_token (parser)->location;
5638 stack[0].expr = c_parser_cast_expression (parser, after);
5639 stack[0].prec = prec;
5640 sp = 0;
5641 while (true)
5643 enum c_parser_prec oprec;
5644 enum tree_code ocode;
5645 if (parser->error)
5646 goto out;
5647 switch (c_parser_peek_token (parser)->type)
5649 case CPP_MULT:
5650 oprec = PREC_MULT;
5651 ocode = MULT_EXPR;
5652 break;
5653 case CPP_DIV:
5654 oprec = PREC_MULT;
5655 ocode = TRUNC_DIV_EXPR;
5656 break;
5657 case CPP_MOD:
5658 oprec = PREC_MULT;
5659 ocode = TRUNC_MOD_EXPR;
5660 break;
5661 case CPP_PLUS:
5662 oprec = PREC_ADD;
5663 ocode = PLUS_EXPR;
5664 break;
5665 case CPP_MINUS:
5666 oprec = PREC_ADD;
5667 ocode = MINUS_EXPR;
5668 break;
5669 case CPP_LSHIFT:
5670 oprec = PREC_SHIFT;
5671 ocode = LSHIFT_EXPR;
5672 break;
5673 case CPP_RSHIFT:
5674 oprec = PREC_SHIFT;
5675 ocode = RSHIFT_EXPR;
5676 break;
5677 case CPP_LESS:
5678 oprec = PREC_REL;
5679 ocode = LT_EXPR;
5680 break;
5681 case CPP_GREATER:
5682 oprec = PREC_REL;
5683 ocode = GT_EXPR;
5684 break;
5685 case CPP_LESS_EQ:
5686 oprec = PREC_REL;
5687 ocode = LE_EXPR;
5688 break;
5689 case CPP_GREATER_EQ:
5690 oprec = PREC_REL;
5691 ocode = GE_EXPR;
5692 break;
5693 case CPP_EQ_EQ:
5694 oprec = PREC_EQ;
5695 ocode = EQ_EXPR;
5696 break;
5697 case CPP_NOT_EQ:
5698 oprec = PREC_EQ;
5699 ocode = NE_EXPR;
5700 break;
5701 case CPP_AND:
5702 oprec = PREC_BITAND;
5703 ocode = BIT_AND_EXPR;
5704 break;
5705 case CPP_XOR:
5706 oprec = PREC_BITXOR;
5707 ocode = BIT_XOR_EXPR;
5708 break;
5709 case CPP_OR:
5710 oprec = PREC_BITOR;
5711 ocode = BIT_IOR_EXPR;
5712 break;
5713 case CPP_AND_AND:
5714 oprec = PREC_LOGAND;
5715 ocode = TRUTH_ANDIF_EXPR;
5716 break;
5717 case CPP_OR_OR:
5718 oprec = PREC_LOGOR;
5719 ocode = TRUTH_ORIF_EXPR;
5720 break;
5721 default:
5722 /* Not a binary operator, so end of the binary
5723 expression. */
5724 goto out;
5726 binary_loc = c_parser_peek_token (parser)->location;
5727 while (oprec <= stack[sp].prec)
5729 if (sp == 0)
5730 goto out;
5731 POP;
5733 c_parser_consume_token (parser);
5734 switch (ocode)
5736 case TRUTH_ANDIF_EXPR:
5737 stack[sp].expr
5738 = default_function_array_read_conversion (stack[sp].loc,
5739 stack[sp].expr);
5740 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5741 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5742 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5743 == truthvalue_false_node);
5744 break;
5745 case TRUTH_ORIF_EXPR:
5746 stack[sp].expr
5747 = default_function_array_read_conversion (stack[sp].loc,
5748 stack[sp].expr);
5749 stack[sp].expr.value = c_objc_common_truthvalue_conversion
5750 (stack[sp].loc, default_conversion (stack[sp].expr.value));
5751 c_inhibit_evaluation_warnings += (stack[sp].expr.value
5752 == truthvalue_true_node);
5753 break;
5754 default:
5755 break;
5757 sp++;
5758 stack[sp].loc = binary_loc;
5759 stack[sp].expr = c_parser_cast_expression (parser, NULL);
5760 stack[sp].prec = oprec;
5761 stack[sp].op = ocode;
5762 stack[sp].loc = binary_loc;
5764 out:
5765 while (sp > 0)
5766 POP;
5767 return stack[0].expr;
5768 #undef POP
5771 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
5772 NULL then it is an Objective-C message expression which is the
5773 primary-expression starting the expression as an initializer.
5775 cast-expression:
5776 unary-expression
5777 ( type-name ) unary-expression
5780 static struct c_expr
5781 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
5783 location_t cast_loc = c_parser_peek_token (parser)->location;
5784 gcc_assert (!after || c_dialect_objc ());
5785 if (after)
5786 return c_parser_postfix_expression_after_primary (parser,
5787 cast_loc, *after);
5788 /* If the expression begins with a parenthesized type name, it may
5789 be either a cast or a compound literal; we need to see whether
5790 the next character is '{' to tell the difference. If not, it is
5791 an unary expression. Full detection of unknown typenames here
5792 would require a 3-token lookahead. */
5793 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5794 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5796 struct c_type_name *type_name;
5797 struct c_expr ret;
5798 struct c_expr expr;
5799 c_parser_consume_token (parser);
5800 type_name = c_parser_type_name (parser);
5801 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5802 if (type_name == NULL)
5804 ret.value = error_mark_node;
5805 ret.original_code = ERROR_MARK;
5806 ret.original_type = NULL;
5807 return ret;
5810 /* Save casted types in the function's used types hash table. */
5811 used_types_insert (type_name->specs->type);
5813 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5814 return c_parser_postfix_expression_after_paren_type (parser, type_name,
5815 cast_loc);
5817 location_t expr_loc = c_parser_peek_token (parser)->location;
5818 expr = c_parser_cast_expression (parser, NULL);
5819 expr = default_function_array_read_conversion (expr_loc, expr);
5821 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
5822 ret.original_code = ERROR_MARK;
5823 ret.original_type = NULL;
5824 return ret;
5826 else
5827 return c_parser_unary_expression (parser);
5830 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
5832 unary-expression:
5833 postfix-expression
5834 ++ unary-expression
5835 -- unary-expression
5836 unary-operator cast-expression
5837 sizeof unary-expression
5838 sizeof ( type-name )
5840 unary-operator: one of
5841 & * + - ~ !
5843 GNU extensions:
5845 unary-expression:
5846 __alignof__ unary-expression
5847 __alignof__ ( type-name )
5848 && identifier
5850 (C11 permits _Alignof with type names only.)
5852 unary-operator: one of
5853 __extension__ __real__ __imag__
5855 Transactional Memory:
5857 unary-expression:
5858 transaction-expression
5860 In addition, the GNU syntax treats ++ and -- as unary operators, so
5861 they may be applied to cast expressions with errors for non-lvalues
5862 given later. */
5864 static struct c_expr
5865 c_parser_unary_expression (c_parser *parser)
5867 int ext;
5868 struct c_expr ret, op;
5869 location_t op_loc = c_parser_peek_token (parser)->location;
5870 location_t exp_loc;
5871 ret.original_code = ERROR_MARK;
5872 ret.original_type = NULL;
5873 switch (c_parser_peek_token (parser)->type)
5875 case CPP_PLUS_PLUS:
5876 c_parser_consume_token (parser);
5877 exp_loc = c_parser_peek_token (parser)->location;
5878 op = c_parser_cast_expression (parser, NULL);
5879 op = default_function_array_read_conversion (exp_loc, op);
5880 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
5881 case CPP_MINUS_MINUS:
5882 c_parser_consume_token (parser);
5883 exp_loc = c_parser_peek_token (parser)->location;
5884 op = c_parser_cast_expression (parser, NULL);
5885 op = default_function_array_read_conversion (exp_loc, op);
5886 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
5887 case CPP_AND:
5888 c_parser_consume_token (parser);
5889 op = c_parser_cast_expression (parser, NULL);
5890 mark_exp_read (op.value);
5891 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
5892 case CPP_MULT:
5893 c_parser_consume_token (parser);
5894 exp_loc = c_parser_peek_token (parser)->location;
5895 op = c_parser_cast_expression (parser, NULL);
5896 op = default_function_array_read_conversion (exp_loc, op);
5897 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
5898 return ret;
5899 case CPP_PLUS:
5900 if (!c_dialect_objc () && !in_system_header)
5901 warning_at (op_loc,
5902 OPT_Wtraditional,
5903 "traditional C rejects the unary plus operator");
5904 c_parser_consume_token (parser);
5905 exp_loc = c_parser_peek_token (parser)->location;
5906 op = c_parser_cast_expression (parser, NULL);
5907 op = default_function_array_read_conversion (exp_loc, op);
5908 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
5909 case CPP_MINUS:
5910 c_parser_consume_token (parser);
5911 exp_loc = c_parser_peek_token (parser)->location;
5912 op = c_parser_cast_expression (parser, NULL);
5913 op = default_function_array_read_conversion (exp_loc, op);
5914 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
5915 case CPP_COMPL:
5916 c_parser_consume_token (parser);
5917 exp_loc = c_parser_peek_token (parser)->location;
5918 op = c_parser_cast_expression (parser, NULL);
5919 op = default_function_array_read_conversion (exp_loc, op);
5920 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
5921 case CPP_NOT:
5922 c_parser_consume_token (parser);
5923 exp_loc = c_parser_peek_token (parser)->location;
5924 op = c_parser_cast_expression (parser, NULL);
5925 op = default_function_array_read_conversion (exp_loc, op);
5926 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
5927 case CPP_AND_AND:
5928 /* Refer to the address of a label as a pointer. */
5929 c_parser_consume_token (parser);
5930 if (c_parser_next_token_is (parser, CPP_NAME))
5932 ret.value = finish_label_address_expr
5933 (c_parser_peek_token (parser)->value, op_loc);
5934 c_parser_consume_token (parser);
5936 else
5938 c_parser_error (parser, "expected identifier");
5939 ret.value = error_mark_node;
5941 return ret;
5942 case CPP_KEYWORD:
5943 switch (c_parser_peek_token (parser)->keyword)
5945 case RID_SIZEOF:
5946 return c_parser_sizeof_expression (parser);
5947 case RID_ALIGNOF:
5948 return c_parser_alignof_expression (parser);
5949 case RID_EXTENSION:
5950 c_parser_consume_token (parser);
5951 ext = disable_extension_diagnostics ();
5952 ret = c_parser_cast_expression (parser, NULL);
5953 restore_extension_diagnostics (ext);
5954 return ret;
5955 case RID_REALPART:
5956 c_parser_consume_token (parser);
5957 exp_loc = c_parser_peek_token (parser)->location;
5958 op = c_parser_cast_expression (parser, NULL);
5959 op = default_function_array_conversion (exp_loc, op);
5960 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
5961 case RID_IMAGPART:
5962 c_parser_consume_token (parser);
5963 exp_loc = c_parser_peek_token (parser)->location;
5964 op = c_parser_cast_expression (parser, NULL);
5965 op = default_function_array_conversion (exp_loc, op);
5966 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
5967 case RID_TRANSACTION_ATOMIC:
5968 case RID_TRANSACTION_RELAXED:
5969 return c_parser_transaction_expression (parser,
5970 c_parser_peek_token (parser)->keyword);
5971 default:
5972 return c_parser_postfix_expression (parser);
5974 default:
5975 return c_parser_postfix_expression (parser);
5979 /* Parse a sizeof expression. */
5981 static struct c_expr
5982 c_parser_sizeof_expression (c_parser *parser)
5984 struct c_expr expr;
5985 location_t expr_loc;
5986 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
5987 c_parser_consume_token (parser);
5988 c_inhibit_evaluation_warnings++;
5989 in_sizeof++;
5990 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5991 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5993 /* Either sizeof ( type-name ) or sizeof unary-expression
5994 starting with a compound literal. */
5995 struct c_type_name *type_name;
5996 c_parser_consume_token (parser);
5997 expr_loc = c_parser_peek_token (parser)->location;
5998 type_name = c_parser_type_name (parser);
5999 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6000 if (type_name == NULL)
6002 struct c_expr ret;
6003 c_inhibit_evaluation_warnings--;
6004 in_sizeof--;
6005 ret.value = error_mark_node;
6006 ret.original_code = ERROR_MARK;
6007 ret.original_type = NULL;
6008 return ret;
6010 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6012 expr = c_parser_postfix_expression_after_paren_type (parser,
6013 type_name,
6014 expr_loc);
6015 goto sizeof_expr;
6017 /* sizeof ( type-name ). */
6018 c_inhibit_evaluation_warnings--;
6019 in_sizeof--;
6020 return c_expr_sizeof_type (expr_loc, type_name);
6022 else
6024 expr_loc = c_parser_peek_token (parser)->location;
6025 expr = c_parser_unary_expression (parser);
6026 sizeof_expr:
6027 c_inhibit_evaluation_warnings--;
6028 in_sizeof--;
6029 mark_exp_read (expr.value);
6030 if (TREE_CODE (expr.value) == COMPONENT_REF
6031 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6032 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6033 return c_expr_sizeof_expr (expr_loc, expr);
6037 /* Parse an alignof expression. */
6039 static struct c_expr
6040 c_parser_alignof_expression (c_parser *parser)
6042 struct c_expr expr;
6043 location_t loc = c_parser_peek_token (parser)->location;
6044 tree alignof_spelling = c_parser_peek_token (parser)->value;
6045 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6046 /* A diagnostic is not required for the use of this identifier in
6047 the implementation namespace; only diagnose it for the C11
6048 spelling because of existing code using the other spellings. */
6049 if (!flag_isoc11
6050 && strcmp (IDENTIFIER_POINTER (alignof_spelling), "_Alignof") == 0)
6052 if (flag_isoc99)
6053 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6054 alignof_spelling);
6055 else
6056 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6057 alignof_spelling);
6059 c_parser_consume_token (parser);
6060 c_inhibit_evaluation_warnings++;
6061 in_alignof++;
6062 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6063 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6065 /* Either __alignof__ ( type-name ) or __alignof__
6066 unary-expression starting with a compound literal. */
6067 location_t loc;
6068 struct c_type_name *type_name;
6069 struct c_expr ret;
6070 c_parser_consume_token (parser);
6071 loc = c_parser_peek_token (parser)->location;
6072 type_name = c_parser_type_name (parser);
6073 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6074 if (type_name == NULL)
6076 struct c_expr ret;
6077 c_inhibit_evaluation_warnings--;
6078 in_alignof--;
6079 ret.value = error_mark_node;
6080 ret.original_code = ERROR_MARK;
6081 ret.original_type = NULL;
6082 return ret;
6084 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6086 expr = c_parser_postfix_expression_after_paren_type (parser,
6087 type_name,
6088 loc);
6089 goto alignof_expr;
6091 /* alignof ( type-name ). */
6092 c_inhibit_evaluation_warnings--;
6093 in_alignof--;
6094 ret.value = c_alignof (loc, groktypename (type_name, NULL, NULL));
6095 ret.original_code = ERROR_MARK;
6096 ret.original_type = NULL;
6097 return ret;
6099 else
6101 struct c_expr ret;
6102 expr = c_parser_unary_expression (parser);
6103 alignof_expr:
6104 mark_exp_read (expr.value);
6105 c_inhibit_evaluation_warnings--;
6106 in_alignof--;
6107 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6108 alignof_spelling);
6109 ret.value = c_alignof_expr (loc, expr.value);
6110 ret.original_code = ERROR_MARK;
6111 ret.original_type = NULL;
6112 return ret;
6116 /* Helper function to read arguments of builtins which are interfaces
6117 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6118 others. The name of the builtin is passed using BNAME parameter.
6119 Function returns true if there were no errors while parsing and
6120 stores the arguments in CEXPR_LIST. */
6121 static bool
6122 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6123 VEC(c_expr_t,gc) **ret_cexpr_list)
6125 location_t loc = c_parser_peek_token (parser)->location;
6126 VEC (c_expr_t,gc) *cexpr_list;
6127 c_expr_t expr;
6129 *ret_cexpr_list = NULL;
6130 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6132 error_at (loc, "cannot take address of %qs", bname);
6133 return false;
6136 c_parser_consume_token (parser);
6138 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6140 c_parser_consume_token (parser);
6141 return true;
6144 expr = c_parser_expr_no_commas (parser, NULL);
6145 cexpr_list = VEC_alloc (c_expr_t, gc, 1);
6146 C_EXPR_APPEND (cexpr_list, expr);
6147 while (c_parser_next_token_is (parser, CPP_COMMA))
6149 c_parser_consume_token (parser);
6150 expr = c_parser_expr_no_commas (parser, NULL);
6151 C_EXPR_APPEND (cexpr_list, expr);
6154 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6155 return false;
6157 *ret_cexpr_list = cexpr_list;
6158 return true;
6162 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6164 postfix-expression:
6165 primary-expression
6166 postfix-expression [ expression ]
6167 postfix-expression ( argument-expression-list[opt] )
6168 postfix-expression . identifier
6169 postfix-expression -> identifier
6170 postfix-expression ++
6171 postfix-expression --
6172 ( type-name ) { initializer-list }
6173 ( type-name ) { initializer-list , }
6175 argument-expression-list:
6176 argument-expression
6177 argument-expression-list , argument-expression
6179 primary-expression:
6180 identifier
6181 constant
6182 string-literal
6183 ( expression )
6185 GNU extensions:
6187 primary-expression:
6188 __func__
6189 (treated as a keyword in GNU C)
6190 __FUNCTION__
6191 __PRETTY_FUNCTION__
6192 ( compound-statement )
6193 __builtin_va_arg ( assignment-expression , type-name )
6194 __builtin_offsetof ( type-name , offsetof-member-designator )
6195 __builtin_choose_expr ( assignment-expression ,
6196 assignment-expression ,
6197 assignment-expression )
6198 __builtin_types_compatible_p ( type-name , type-name )
6199 __builtin_complex ( assignment-expression , assignment-expression )
6200 __builtin_shuffle ( assignment-expression , assignment-expression )
6201 __builtin_shuffle ( assignment-expression ,
6202 assignment-expression ,
6203 assignment-expression, )
6205 offsetof-member-designator:
6206 identifier
6207 offsetof-member-designator . identifier
6208 offsetof-member-designator [ expression ]
6210 Objective-C:
6212 primary-expression:
6213 [ objc-receiver objc-message-args ]
6214 @selector ( objc-selector-arg )
6215 @protocol ( identifier )
6216 @encode ( type-name )
6217 objc-string-literal
6218 Classname . identifier
6221 static struct c_expr
6222 c_parser_postfix_expression (c_parser *parser)
6224 struct c_expr expr, e1;
6225 struct c_type_name *t1, *t2;
6226 location_t loc = c_parser_peek_token (parser)->location;;
6227 expr.original_code = ERROR_MARK;
6228 expr.original_type = NULL;
6229 switch (c_parser_peek_token (parser)->type)
6231 case CPP_NUMBER:
6232 expr.value = c_parser_peek_token (parser)->value;
6233 loc = c_parser_peek_token (parser)->location;
6234 c_parser_consume_token (parser);
6235 if (TREE_CODE (expr.value) == FIXED_CST
6236 && !targetm.fixed_point_supported_p ())
6238 error_at (loc, "fixed-point types not supported for this target");
6239 expr.value = error_mark_node;
6241 break;
6242 case CPP_CHAR:
6243 case CPP_CHAR16:
6244 case CPP_CHAR32:
6245 case CPP_WCHAR:
6246 expr.value = c_parser_peek_token (parser)->value;
6247 c_parser_consume_token (parser);
6248 break;
6249 case CPP_STRING:
6250 case CPP_STRING16:
6251 case CPP_STRING32:
6252 case CPP_WSTRING:
6253 case CPP_UTF8STRING:
6254 expr.value = c_parser_peek_token (parser)->value;
6255 expr.original_code = STRING_CST;
6256 c_parser_consume_token (parser);
6257 break;
6258 case CPP_OBJC_STRING:
6259 gcc_assert (c_dialect_objc ());
6260 expr.value
6261 = objc_build_string_object (c_parser_peek_token (parser)->value);
6262 c_parser_consume_token (parser);
6263 break;
6264 case CPP_NAME:
6265 switch (c_parser_peek_token (parser)->id_kind)
6267 case C_ID_ID:
6269 tree id = c_parser_peek_token (parser)->value;
6270 c_parser_consume_token (parser);
6271 expr.value = build_external_ref (loc, id,
6272 (c_parser_peek_token (parser)->type
6273 == CPP_OPEN_PAREN),
6274 &expr.original_type);
6275 break;
6277 case C_ID_CLASSNAME:
6279 /* Here we parse the Objective-C 2.0 Class.name dot
6280 syntax. */
6281 tree class_name = c_parser_peek_token (parser)->value;
6282 tree component;
6283 c_parser_consume_token (parser);
6284 gcc_assert (c_dialect_objc ());
6285 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
6287 expr.value = error_mark_node;
6288 break;
6290 if (c_parser_next_token_is_not (parser, CPP_NAME))
6292 c_parser_error (parser, "expected identifier");
6293 expr.value = error_mark_node;
6294 break;
6296 component = c_parser_peek_token (parser)->value;
6297 c_parser_consume_token (parser);
6298 expr.value = objc_build_class_component_ref (class_name,
6299 component);
6300 break;
6302 default:
6303 c_parser_error (parser, "expected expression");
6304 expr.value = error_mark_node;
6305 break;
6307 break;
6308 case CPP_OPEN_PAREN:
6309 /* A parenthesized expression, statement expression or compound
6310 literal. */
6311 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
6313 /* A statement expression. */
6314 tree stmt;
6315 location_t brace_loc;
6316 c_parser_consume_token (parser);
6317 brace_loc = c_parser_peek_token (parser)->location;
6318 c_parser_consume_token (parser);
6319 if (!building_stmt_list_p ())
6321 error_at (loc, "braced-group within expression allowed "
6322 "only inside a function");
6323 parser->error = true;
6324 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
6325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6326 expr.value = error_mark_node;
6327 break;
6329 stmt = c_begin_stmt_expr ();
6330 c_parser_compound_statement_nostart (parser);
6331 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6332 "expected %<)%>");
6333 pedwarn (loc, OPT_Wpedantic,
6334 "ISO C forbids braced-groups within expressions");
6335 expr.value = c_finish_stmt_expr (brace_loc, stmt);
6336 mark_exp_read (expr.value);
6338 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6340 /* A compound literal. ??? Can we actually get here rather
6341 than going directly to
6342 c_parser_postfix_expression_after_paren_type from
6343 elsewhere? */
6344 location_t loc;
6345 struct c_type_name *type_name;
6346 c_parser_consume_token (parser);
6347 loc = c_parser_peek_token (parser)->location;
6348 type_name = c_parser_type_name (parser);
6349 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6350 "expected %<)%>");
6351 if (type_name == NULL)
6353 expr.value = error_mark_node;
6355 else
6356 expr = c_parser_postfix_expression_after_paren_type (parser,
6357 type_name,
6358 loc);
6360 else
6362 /* A parenthesized expression. */
6363 c_parser_consume_token (parser);
6364 expr = c_parser_expression (parser);
6365 if (TREE_CODE (expr.value) == MODIFY_EXPR)
6366 TREE_NO_WARNING (expr.value) = 1;
6367 if (expr.original_code != C_MAYBE_CONST_EXPR)
6368 expr.original_code = ERROR_MARK;
6369 /* Don't change EXPR.ORIGINAL_TYPE. */
6370 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6371 "expected %<)%>");
6373 break;
6374 case CPP_KEYWORD:
6375 switch (c_parser_peek_token (parser)->keyword)
6377 case RID_FUNCTION_NAME:
6378 case RID_PRETTY_FUNCTION_NAME:
6379 case RID_C99_FUNCTION_NAME:
6380 expr.value = fname_decl (loc,
6381 c_parser_peek_token (parser)->keyword,
6382 c_parser_peek_token (parser)->value);
6383 c_parser_consume_token (parser);
6384 break;
6385 case RID_VA_ARG:
6386 c_parser_consume_token (parser);
6387 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6389 expr.value = error_mark_node;
6390 break;
6392 e1 = c_parser_expr_no_commas (parser, NULL);
6393 mark_exp_read (e1.value);
6394 e1.value = c_fully_fold (e1.value, false, NULL);
6395 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6397 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6398 expr.value = error_mark_node;
6399 break;
6401 loc = c_parser_peek_token (parser)->location;
6402 t1 = c_parser_type_name (parser);
6403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6404 "expected %<)%>");
6405 if (t1 == NULL)
6407 expr.value = error_mark_node;
6409 else
6411 tree type_expr = NULL_TREE;
6412 expr.value = c_build_va_arg (loc, e1.value,
6413 groktypename (t1, &type_expr, NULL));
6414 if (type_expr)
6416 expr.value = build2 (C_MAYBE_CONST_EXPR,
6417 TREE_TYPE (expr.value), type_expr,
6418 expr.value);
6419 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
6422 break;
6423 case RID_OFFSETOF:
6424 c_parser_consume_token (parser);
6425 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6427 expr.value = error_mark_node;
6428 break;
6430 t1 = c_parser_type_name (parser);
6431 if (t1 == NULL)
6432 parser->error = true;
6433 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6434 gcc_assert (parser->error);
6435 if (parser->error)
6437 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6438 expr.value = error_mark_node;
6439 break;
6443 tree type = groktypename (t1, NULL, NULL);
6444 tree offsetof_ref;
6445 if (type == error_mark_node)
6446 offsetof_ref = error_mark_node;
6447 else
6449 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
6450 SET_EXPR_LOCATION (offsetof_ref, loc);
6452 /* Parse the second argument to __builtin_offsetof. We
6453 must have one identifier, and beyond that we want to
6454 accept sub structure and sub array references. */
6455 if (c_parser_next_token_is (parser, CPP_NAME))
6457 offsetof_ref = build_component_ref
6458 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
6459 c_parser_consume_token (parser);
6460 while (c_parser_next_token_is (parser, CPP_DOT)
6461 || c_parser_next_token_is (parser,
6462 CPP_OPEN_SQUARE)
6463 || c_parser_next_token_is (parser,
6464 CPP_DEREF))
6466 if (c_parser_next_token_is (parser, CPP_DEREF))
6468 loc = c_parser_peek_token (parser)->location;
6469 offsetof_ref = build_array_ref (loc,
6470 offsetof_ref,
6471 integer_zero_node);
6472 goto do_dot;
6474 else if (c_parser_next_token_is (parser, CPP_DOT))
6476 do_dot:
6477 c_parser_consume_token (parser);
6478 if (c_parser_next_token_is_not (parser,
6479 CPP_NAME))
6481 c_parser_error (parser, "expected identifier");
6482 break;
6484 offsetof_ref = build_component_ref
6485 (loc, offsetof_ref,
6486 c_parser_peek_token (parser)->value);
6487 c_parser_consume_token (parser);
6489 else
6491 tree idx;
6492 loc = c_parser_peek_token (parser)->location;
6493 c_parser_consume_token (parser);
6494 idx = c_parser_expression (parser).value;
6495 idx = c_fully_fold (idx, false, NULL);
6496 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6497 "expected %<]%>");
6498 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
6502 else
6503 c_parser_error (parser, "expected identifier");
6504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6505 "expected %<)%>");
6506 expr.value = fold_offsetof (offsetof_ref);
6508 break;
6509 case RID_CHOOSE_EXPR:
6511 VEC (c_expr_t, gc) *cexpr_list;
6512 c_expr_t *e1_p, *e2_p, *e3_p;
6513 tree c;
6515 c_parser_consume_token (parser);
6516 if (!c_parser_get_builtin_args (parser,
6517 "__builtin_choose_expr",
6518 &cexpr_list))
6520 expr.value = error_mark_node;
6521 break;
6524 if (VEC_length (c_expr_t, cexpr_list) != 3)
6526 error_at (loc, "wrong number of arguments to "
6527 "%<__builtin_choose_expr%>");
6528 expr.value = error_mark_node;
6529 break;
6532 e1_p = VEC_index (c_expr_t, cexpr_list, 0);
6533 e2_p = VEC_index (c_expr_t, cexpr_list, 1);
6534 e3_p = VEC_index (c_expr_t, cexpr_list, 2);
6536 c = e1_p->value;
6537 mark_exp_read (e2_p->value);
6538 mark_exp_read (e3_p->value);
6539 if (TREE_CODE (c) != INTEGER_CST
6540 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
6541 error_at (loc,
6542 "first argument to %<__builtin_choose_expr%> not"
6543 " a constant");
6544 constant_expression_warning (c);
6545 expr = integer_zerop (c) ? *e3_p : *e2_p;
6546 break;
6548 case RID_TYPES_COMPATIBLE_P:
6549 c_parser_consume_token (parser);
6550 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6552 expr.value = error_mark_node;
6553 break;
6555 t1 = c_parser_type_name (parser);
6556 if (t1 == NULL)
6558 expr.value = error_mark_node;
6559 break;
6561 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6563 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6564 expr.value = error_mark_node;
6565 break;
6567 t2 = c_parser_type_name (parser);
6568 if (t2 == NULL)
6570 expr.value = error_mark_node;
6571 break;
6573 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6574 "expected %<)%>");
6576 tree e1, e2;
6577 e1 = groktypename (t1, NULL, NULL);
6578 e2 = groktypename (t2, NULL, NULL);
6579 if (e1 == error_mark_node || e2 == error_mark_node)
6581 expr.value = error_mark_node;
6582 break;
6585 e1 = TYPE_MAIN_VARIANT (e1);
6586 e2 = TYPE_MAIN_VARIANT (e2);
6588 expr.value
6589 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
6591 break;
6592 case RID_BUILTIN_COMPLEX:
6594 VEC(c_expr_t, gc) *cexpr_list;
6595 c_expr_t *e1_p, *e2_p;
6597 c_parser_consume_token (parser);
6598 if (!c_parser_get_builtin_args (parser,
6599 "__builtin_complex",
6600 &cexpr_list))
6602 expr.value = error_mark_node;
6603 break;
6606 if (VEC_length (c_expr_t, cexpr_list) != 2)
6608 error_at (loc, "wrong number of arguments to "
6609 "%<__builtin_complex%>");
6610 expr.value = error_mark_node;
6611 break;
6614 e1_p = VEC_index (c_expr_t, cexpr_list, 0);
6615 e2_p = VEC_index (c_expr_t, cexpr_list, 1);
6617 mark_exp_read (e1_p->value);
6618 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
6619 e1_p->value = convert (TREE_TYPE (e1_p->value),
6620 TREE_OPERAND (e1_p->value, 0));
6621 mark_exp_read (e2_p->value);
6622 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
6623 e2_p->value = convert (TREE_TYPE (e2_p->value),
6624 TREE_OPERAND (e2_p->value, 0));
6625 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6626 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
6627 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
6628 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
6630 error_at (loc, "%<__builtin_complex%> operand "
6631 "not of real binary floating-point type");
6632 expr.value = error_mark_node;
6633 break;
6635 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
6636 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
6638 error_at (loc,
6639 "%<__builtin_complex%> operands of different types");
6640 expr.value = error_mark_node;
6641 break;
6643 if (!flag_isoc99)
6644 pedwarn (loc, OPT_Wpedantic,
6645 "ISO C90 does not support complex types");
6646 expr.value = build2 (COMPLEX_EXPR,
6647 build_complex_type
6648 (TYPE_MAIN_VARIANT
6649 (TREE_TYPE (e1_p->value))),
6650 e1_p->value, e2_p->value);
6651 break;
6653 case RID_BUILTIN_SHUFFLE:
6655 VEC(c_expr_t,gc) *cexpr_list;
6656 unsigned int i;
6657 c_expr_t *p;
6659 c_parser_consume_token (parser);
6660 if (!c_parser_get_builtin_args (parser,
6661 "__builtin_shuffle",
6662 &cexpr_list))
6664 expr.value = error_mark_node;
6665 break;
6668 FOR_EACH_VEC_ELT (c_expr_t, cexpr_list, i, p)
6669 mark_exp_read (p->value);
6671 if (VEC_length (c_expr_t, cexpr_list) == 2)
6672 expr.value =
6673 c_build_vec_perm_expr
6674 (loc, VEC_index (c_expr_t, cexpr_list, 0)->value,
6675 NULL_TREE, VEC_index (c_expr_t, cexpr_list, 1)->value);
6677 else if (VEC_length (c_expr_t, cexpr_list) == 3)
6678 expr.value =
6679 c_build_vec_perm_expr
6680 (loc, VEC_index (c_expr_t, cexpr_list, 0)->value,
6681 VEC_index (c_expr_t, cexpr_list, 1)->value,
6682 VEC_index (c_expr_t, cexpr_list, 2)->value);
6683 else
6685 error_at (loc, "wrong number of arguments to "
6686 "%<__builtin_shuffle%>");
6687 expr.value = error_mark_node;
6689 break;
6691 case RID_AT_SELECTOR:
6692 gcc_assert (c_dialect_objc ());
6693 c_parser_consume_token (parser);
6694 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6696 expr.value = error_mark_node;
6697 break;
6700 tree sel = c_parser_objc_selector_arg (parser);
6701 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6702 "expected %<)%>");
6703 expr.value = objc_build_selector_expr (loc, sel);
6705 break;
6706 case RID_AT_PROTOCOL:
6707 gcc_assert (c_dialect_objc ());
6708 c_parser_consume_token (parser);
6709 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6711 expr.value = error_mark_node;
6712 break;
6714 if (c_parser_next_token_is_not (parser, CPP_NAME))
6716 c_parser_error (parser, "expected identifier");
6717 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6718 expr.value = error_mark_node;
6719 break;
6722 tree id = c_parser_peek_token (parser)->value;
6723 c_parser_consume_token (parser);
6724 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6725 "expected %<)%>");
6726 expr.value = objc_build_protocol_expr (id);
6728 break;
6729 case RID_AT_ENCODE:
6730 /* Extension to support C-structures in the archiver. */
6731 gcc_assert (c_dialect_objc ());
6732 c_parser_consume_token (parser);
6733 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6735 expr.value = error_mark_node;
6736 break;
6738 t1 = c_parser_type_name (parser);
6739 if (t1 == NULL)
6741 expr.value = error_mark_node;
6742 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6743 break;
6745 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6746 "expected %<)%>");
6748 tree type = groktypename (t1, NULL, NULL);
6749 expr.value = objc_build_encode_expr (type);
6751 break;
6752 default:
6753 c_parser_error (parser, "expected expression");
6754 expr.value = error_mark_node;
6755 break;
6757 break;
6758 case CPP_OPEN_SQUARE:
6759 if (c_dialect_objc ())
6761 tree receiver, args;
6762 c_parser_consume_token (parser);
6763 receiver = c_parser_objc_receiver (parser);
6764 args = c_parser_objc_message_args (parser);
6765 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6766 "expected %<]%>");
6767 expr.value = objc_build_message_expr (receiver, args);
6768 break;
6770 /* Else fall through to report error. */
6771 default:
6772 c_parser_error (parser, "expected expression");
6773 expr.value = error_mark_node;
6774 break;
6776 return c_parser_postfix_expression_after_primary (parser, loc, expr);
6779 /* Parse a postfix expression after a parenthesized type name: the
6780 brace-enclosed initializer of a compound literal, possibly followed
6781 by some postfix operators. This is separate because it is not
6782 possible to tell until after the type name whether a cast
6783 expression has a cast or a compound literal, or whether the operand
6784 of sizeof is a parenthesized type name or starts with a compound
6785 literal. TYPE_LOC is the location where TYPE_NAME starts--the
6786 location of the first token after the parentheses around the type
6787 name. */
6789 static struct c_expr
6790 c_parser_postfix_expression_after_paren_type (c_parser *parser,
6791 struct c_type_name *type_name,
6792 location_t type_loc)
6794 tree type;
6795 struct c_expr init;
6796 bool non_const;
6797 struct c_expr expr;
6798 location_t start_loc;
6799 tree type_expr = NULL_TREE;
6800 bool type_expr_const = true;
6801 check_compound_literal_type (type_loc, type_name);
6802 start_init (NULL_TREE, NULL, 0);
6803 type = groktypename (type_name, &type_expr, &type_expr_const);
6804 start_loc = c_parser_peek_token (parser)->location;
6805 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
6807 error_at (type_loc, "compound literal has variable size");
6808 type = error_mark_node;
6810 init = c_parser_braced_init (parser, type, false);
6811 finish_init ();
6812 maybe_warn_string_init (type, init);
6814 if (type != error_mark_node
6815 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
6816 && current_function_decl)
6818 error ("compound literal qualified by address-space qualifier");
6819 type = error_mark_node;
6822 if (!flag_isoc99)
6823 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
6824 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
6825 ? CONSTRUCTOR_NON_CONST (init.value)
6826 : init.original_code == C_MAYBE_CONST_EXPR);
6827 non_const |= !type_expr_const;
6828 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
6829 expr.original_code = ERROR_MARK;
6830 expr.original_type = NULL;
6831 if (type_expr)
6833 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
6835 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
6836 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
6838 else
6840 gcc_assert (!non_const);
6841 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
6842 type_expr, expr.value);
6845 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
6848 /* Parse a postfix expression after the initial primary or compound
6849 literal; that is, parse a series of postfix operators.
6851 EXPR_LOC is the location of the primary expression. */
6853 static struct c_expr
6854 c_parser_postfix_expression_after_primary (c_parser *parser,
6855 location_t expr_loc,
6856 struct c_expr expr)
6858 struct c_expr orig_expr;
6859 tree ident, idx;
6860 VEC(tree,gc) *exprlist;
6861 VEC(tree,gc) *origtypes;
6862 while (true)
6864 location_t op_loc = c_parser_peek_token (parser)->location;
6865 switch (c_parser_peek_token (parser)->type)
6867 case CPP_OPEN_SQUARE:
6868 /* Array reference. */
6869 c_parser_consume_token (parser);
6870 idx = c_parser_expression (parser).value;
6871 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6872 "expected %<]%>");
6873 expr.value = build_array_ref (op_loc, expr.value, idx);
6874 expr.original_code = ERROR_MARK;
6875 expr.original_type = NULL;
6876 break;
6877 case CPP_OPEN_PAREN:
6878 /* Function call. */
6879 c_parser_consume_token (parser);
6880 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6881 exprlist = NULL;
6882 else
6883 exprlist = c_parser_expr_list (parser, true, false, &origtypes);
6884 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
6885 "expected %<)%>");
6886 orig_expr = expr;
6887 mark_exp_read (expr.value);
6888 /* FIXME diagnostics: Ideally we want the FUNCNAME, not the
6889 "(" after the FUNCNAME, which is what we have now. */
6890 expr.value = build_function_call_vec (op_loc, expr.value, exprlist,
6891 origtypes);
6892 expr.original_code = ERROR_MARK;
6893 if (TREE_CODE (expr.value) == INTEGER_CST
6894 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
6895 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
6896 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
6897 expr.original_code = C_MAYBE_CONST_EXPR;
6898 expr.original_type = NULL;
6899 if (exprlist != NULL)
6901 release_tree_vector (exprlist);
6902 release_tree_vector (origtypes);
6904 break;
6905 case CPP_DOT:
6906 /* Structure element reference. */
6907 c_parser_consume_token (parser);
6908 expr = default_function_array_conversion (expr_loc, expr);
6909 if (c_parser_next_token_is (parser, CPP_NAME))
6910 ident = c_parser_peek_token (parser)->value;
6911 else
6913 c_parser_error (parser, "expected identifier");
6914 expr.value = error_mark_node;
6915 expr.original_code = ERROR_MARK;
6916 expr.original_type = NULL;
6917 return expr;
6919 c_parser_consume_token (parser);
6920 expr.value = build_component_ref (op_loc, expr.value, ident);
6921 expr.original_code = ERROR_MARK;
6922 if (TREE_CODE (expr.value) != COMPONENT_REF)
6923 expr.original_type = NULL;
6924 else
6926 /* Remember the original type of a bitfield. */
6927 tree field = TREE_OPERAND (expr.value, 1);
6928 if (TREE_CODE (field) != FIELD_DECL)
6929 expr.original_type = NULL;
6930 else
6931 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6933 break;
6934 case CPP_DEREF:
6935 /* Structure element reference. */
6936 c_parser_consume_token (parser);
6937 expr = default_function_array_conversion (expr_loc, expr);
6938 if (c_parser_next_token_is (parser, CPP_NAME))
6939 ident = c_parser_peek_token (parser)->value;
6940 else
6942 c_parser_error (parser, "expected identifier");
6943 expr.value = error_mark_node;
6944 expr.original_code = ERROR_MARK;
6945 expr.original_type = NULL;
6946 return expr;
6948 c_parser_consume_token (parser);
6949 expr.value = build_component_ref (op_loc,
6950 build_indirect_ref (op_loc,
6951 expr.value,
6952 RO_ARROW),
6953 ident);
6954 expr.original_code = ERROR_MARK;
6955 if (TREE_CODE (expr.value) != COMPONENT_REF)
6956 expr.original_type = NULL;
6957 else
6959 /* Remember the original type of a bitfield. */
6960 tree field = TREE_OPERAND (expr.value, 1);
6961 if (TREE_CODE (field) != FIELD_DECL)
6962 expr.original_type = NULL;
6963 else
6964 expr.original_type = DECL_BIT_FIELD_TYPE (field);
6966 break;
6967 case CPP_PLUS_PLUS:
6968 /* Postincrement. */
6969 c_parser_consume_token (parser);
6970 expr = default_function_array_read_conversion (expr_loc, expr);
6971 expr.value = build_unary_op (op_loc,
6972 POSTINCREMENT_EXPR, expr.value, 0);
6973 expr.original_code = ERROR_MARK;
6974 expr.original_type = NULL;
6975 break;
6976 case CPP_MINUS_MINUS:
6977 /* Postdecrement. */
6978 c_parser_consume_token (parser);
6979 expr = default_function_array_read_conversion (expr_loc, expr);
6980 expr.value = build_unary_op (op_loc,
6981 POSTDECREMENT_EXPR, expr.value, 0);
6982 expr.original_code = ERROR_MARK;
6983 expr.original_type = NULL;
6984 break;
6985 default:
6986 return expr;
6991 /* Parse an expression (C90 6.3.17, C99 6.5.17).
6993 expression:
6994 assignment-expression
6995 expression , assignment-expression
6998 static struct c_expr
6999 c_parser_expression (c_parser *parser)
7001 struct c_expr expr;
7002 expr = c_parser_expr_no_commas (parser, NULL);
7003 while (c_parser_next_token_is (parser, CPP_COMMA))
7005 struct c_expr next;
7006 tree lhsval;
7007 location_t loc = c_parser_peek_token (parser)->location;
7008 location_t expr_loc;
7009 c_parser_consume_token (parser);
7010 expr_loc = c_parser_peek_token (parser)->location;
7011 lhsval = expr.value;
7012 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7013 lhsval = TREE_OPERAND (lhsval, 1);
7014 if (DECL_P (lhsval) || handled_component_p (lhsval))
7015 mark_exp_read (lhsval);
7016 next = c_parser_expr_no_commas (parser, NULL);
7017 next = default_function_array_conversion (expr_loc, next);
7018 expr.value = build_compound_expr (loc, expr.value, next.value);
7019 expr.original_code = COMPOUND_EXPR;
7020 expr.original_type = next.original_type;
7022 return expr;
7025 /* Parse an expression and convert functions or arrays to
7026 pointers. */
7028 static struct c_expr
7029 c_parser_expression_conv (c_parser *parser)
7031 struct c_expr expr;
7032 location_t loc = c_parser_peek_token (parser)->location;
7033 expr = c_parser_expression (parser);
7034 expr = default_function_array_conversion (loc, expr);
7035 return expr;
7038 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7039 functions and arrays to pointers. If FOLD_P, fold the expressions.
7041 nonempty-expr-list:
7042 assignment-expression
7043 nonempty-expr-list , assignment-expression
7046 static VEC(tree,gc) *
7047 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7048 VEC(tree,gc) **p_orig_types)
7050 VEC(tree,gc) *ret;
7051 VEC(tree,gc) *orig_types;
7052 struct c_expr expr;
7053 location_t loc = c_parser_peek_token (parser)->location;
7055 ret = make_tree_vector ();
7056 if (p_orig_types == NULL)
7057 orig_types = NULL;
7058 else
7059 orig_types = make_tree_vector ();
7061 expr = c_parser_expr_no_commas (parser, NULL);
7062 if (convert_p)
7063 expr = default_function_array_read_conversion (loc, expr);
7064 if (fold_p)
7065 expr.value = c_fully_fold (expr.value, false, NULL);
7066 VEC_quick_push (tree, ret, expr.value);
7067 if (orig_types != NULL)
7068 VEC_quick_push (tree, orig_types, expr.original_type);
7069 while (c_parser_next_token_is (parser, CPP_COMMA))
7071 c_parser_consume_token (parser);
7072 loc = c_parser_peek_token (parser)->location;
7073 expr = c_parser_expr_no_commas (parser, NULL);
7074 if (convert_p)
7075 expr = default_function_array_read_conversion (loc, expr);
7076 if (fold_p)
7077 expr.value = c_fully_fold (expr.value, false, NULL);
7078 VEC_safe_push (tree, gc, ret, expr.value);
7079 if (orig_types != NULL)
7080 VEC_safe_push (tree, gc, orig_types, expr.original_type);
7082 if (orig_types != NULL)
7083 *p_orig_types = orig_types;
7084 return ret;
7087 /* Parse Objective-C-specific constructs. */
7089 /* Parse an objc-class-definition.
7091 objc-class-definition:
7092 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7093 objc-class-instance-variables[opt] objc-methodprotolist @end
7094 @implementation identifier objc-superclass[opt]
7095 objc-class-instance-variables[opt]
7096 @interface identifier ( identifier ) objc-protocol-refs[opt]
7097 objc-methodprotolist @end
7098 @interface identifier ( ) objc-protocol-refs[opt]
7099 objc-methodprotolist @end
7100 @implementation identifier ( identifier )
7102 objc-superclass:
7103 : identifier
7105 "@interface identifier (" must start "@interface identifier (
7106 identifier ) ...": objc-methodprotolist in the first production may
7107 not start with a parenthesized identifier as a declarator of a data
7108 definition with no declaration specifiers if the objc-superclass,
7109 objc-protocol-refs and objc-class-instance-variables are omitted. */
7111 static void
7112 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7114 bool iface_p;
7115 tree id1;
7116 tree superclass;
7117 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7118 iface_p = true;
7119 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7120 iface_p = false;
7121 else
7122 gcc_unreachable ();
7124 c_parser_consume_token (parser);
7125 if (c_parser_next_token_is_not (parser, CPP_NAME))
7127 c_parser_error (parser, "expected identifier");
7128 return;
7130 id1 = c_parser_peek_token (parser)->value;
7131 c_parser_consume_token (parser);
7132 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7134 /* We have a category or class extension. */
7135 tree id2;
7136 tree proto = NULL_TREE;
7137 c_parser_consume_token (parser);
7138 if (c_parser_next_token_is_not (parser, CPP_NAME))
7140 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7142 /* We have a class extension. */
7143 id2 = NULL_TREE;
7145 else
7147 c_parser_error (parser, "expected identifier or %<)%>");
7148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7149 return;
7152 else
7154 id2 = c_parser_peek_token (parser)->value;
7155 c_parser_consume_token (parser);
7157 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7158 if (!iface_p)
7160 objc_start_category_implementation (id1, id2);
7161 return;
7163 if (c_parser_next_token_is (parser, CPP_LESS))
7164 proto = c_parser_objc_protocol_refs (parser);
7165 objc_start_category_interface (id1, id2, proto, attributes);
7166 c_parser_objc_methodprotolist (parser);
7167 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7168 objc_finish_interface ();
7169 return;
7171 if (c_parser_next_token_is (parser, CPP_COLON))
7173 c_parser_consume_token (parser);
7174 if (c_parser_next_token_is_not (parser, CPP_NAME))
7176 c_parser_error (parser, "expected identifier");
7177 return;
7179 superclass = c_parser_peek_token (parser)->value;
7180 c_parser_consume_token (parser);
7182 else
7183 superclass = NULL_TREE;
7184 if (iface_p)
7186 tree proto = NULL_TREE;
7187 if (c_parser_next_token_is (parser, CPP_LESS))
7188 proto = c_parser_objc_protocol_refs (parser);
7189 objc_start_class_interface (id1, superclass, proto, attributes);
7191 else
7192 objc_start_class_implementation (id1, superclass);
7193 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7194 c_parser_objc_class_instance_variables (parser);
7195 if (iface_p)
7197 objc_continue_interface ();
7198 c_parser_objc_methodprotolist (parser);
7199 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7200 objc_finish_interface ();
7202 else
7204 objc_continue_implementation ();
7205 return;
7209 /* Parse objc-class-instance-variables.
7211 objc-class-instance-variables:
7212 { objc-instance-variable-decl-list[opt] }
7214 objc-instance-variable-decl-list:
7215 objc-visibility-spec
7216 objc-instance-variable-decl ;
7218 objc-instance-variable-decl-list objc-visibility-spec
7219 objc-instance-variable-decl-list objc-instance-variable-decl ;
7220 objc-instance-variable-decl-list ;
7222 objc-visibility-spec:
7223 @private
7224 @protected
7225 @public
7227 objc-instance-variable-decl:
7228 struct-declaration
7231 static void
7232 c_parser_objc_class_instance_variables (c_parser *parser)
7234 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
7235 c_parser_consume_token (parser);
7236 while (c_parser_next_token_is_not (parser, CPP_EOF))
7238 tree decls;
7239 /* Parse any stray semicolon. */
7240 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7242 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7243 "extra semicolon");
7244 c_parser_consume_token (parser);
7245 continue;
7247 /* Stop if at the end of the instance variables. */
7248 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
7250 c_parser_consume_token (parser);
7251 break;
7253 /* Parse any objc-visibility-spec. */
7254 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
7256 c_parser_consume_token (parser);
7257 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7258 continue;
7260 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
7262 c_parser_consume_token (parser);
7263 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7264 continue;
7266 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
7268 c_parser_consume_token (parser);
7269 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
7270 continue;
7272 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
7274 c_parser_consume_token (parser);
7275 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7276 continue;
7278 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
7280 c_parser_pragma (parser, pragma_external);
7281 continue;
7284 /* Parse some comma-separated declarations. */
7285 decls = c_parser_struct_declaration (parser);
7286 if (decls == NULL)
7288 /* There is a syntax error. We want to skip the offending
7289 tokens up to the next ';' (included) or '}'
7290 (excluded). */
7292 /* First, skip manually a ')' or ']'. This is because they
7293 reduce the nesting level, so c_parser_skip_until_found()
7294 wouldn't be able to skip past them. */
7295 c_token *token = c_parser_peek_token (parser);
7296 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
7297 c_parser_consume_token (parser);
7299 /* Then, do the standard skipping. */
7300 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7302 /* We hopefully recovered. Start normal parsing again. */
7303 parser->error = false;
7304 continue;
7306 else
7308 /* Comma-separated instance variables are chained together
7309 in reverse order; add them one by one. */
7310 tree ivar = nreverse (decls);
7311 for (; ivar; ivar = DECL_CHAIN (ivar))
7312 objc_add_instance_variable (copy_node (ivar));
7314 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7318 /* Parse an objc-class-declaration.
7320 objc-class-declaration:
7321 @class identifier-list ;
7324 static void
7325 c_parser_objc_class_declaration (c_parser *parser)
7327 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
7328 c_parser_consume_token (parser);
7329 /* Any identifiers, including those declared as type names, are OK
7330 here. */
7331 while (true)
7333 tree id;
7334 if (c_parser_next_token_is_not (parser, CPP_NAME))
7336 c_parser_error (parser, "expected identifier");
7337 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7338 parser->error = false;
7339 return;
7341 id = c_parser_peek_token (parser)->value;
7342 objc_declare_class (id);
7343 c_parser_consume_token (parser);
7344 if (c_parser_next_token_is (parser, CPP_COMMA))
7345 c_parser_consume_token (parser);
7346 else
7347 break;
7349 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7352 /* Parse an objc-alias-declaration.
7354 objc-alias-declaration:
7355 @compatibility_alias identifier identifier ;
7358 static void
7359 c_parser_objc_alias_declaration (c_parser *parser)
7361 tree id1, id2;
7362 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
7363 c_parser_consume_token (parser);
7364 if (c_parser_next_token_is_not (parser, CPP_NAME))
7366 c_parser_error (parser, "expected identifier");
7367 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7368 return;
7370 id1 = c_parser_peek_token (parser)->value;
7371 c_parser_consume_token (parser);
7372 if (c_parser_next_token_is_not (parser, CPP_NAME))
7374 c_parser_error (parser, "expected identifier");
7375 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
7376 return;
7378 id2 = c_parser_peek_token (parser)->value;
7379 c_parser_consume_token (parser);
7380 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7381 objc_declare_alias (id1, id2);
7384 /* Parse an objc-protocol-definition.
7386 objc-protocol-definition:
7387 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
7388 @protocol identifier-list ;
7390 "@protocol identifier ;" should be resolved as "@protocol
7391 identifier-list ;": objc-methodprotolist may not start with a
7392 semicolon in the first alternative if objc-protocol-refs are
7393 omitted. */
7395 static void
7396 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
7398 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
7400 c_parser_consume_token (parser);
7401 if (c_parser_next_token_is_not (parser, CPP_NAME))
7403 c_parser_error (parser, "expected identifier");
7404 return;
7406 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
7407 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
7409 /* Any identifiers, including those declared as type names, are
7410 OK here. */
7411 while (true)
7413 tree id;
7414 if (c_parser_next_token_is_not (parser, CPP_NAME))
7416 c_parser_error (parser, "expected identifier");
7417 break;
7419 id = c_parser_peek_token (parser)->value;
7420 objc_declare_protocol (id, attributes);
7421 c_parser_consume_token (parser);
7422 if (c_parser_next_token_is (parser, CPP_COMMA))
7423 c_parser_consume_token (parser);
7424 else
7425 break;
7427 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7429 else
7431 tree id = c_parser_peek_token (parser)->value;
7432 tree proto = NULL_TREE;
7433 c_parser_consume_token (parser);
7434 if (c_parser_next_token_is (parser, CPP_LESS))
7435 proto = c_parser_objc_protocol_refs (parser);
7436 parser->objc_pq_context = true;
7437 objc_start_protocol (id, proto, attributes);
7438 c_parser_objc_methodprotolist (parser);
7439 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
7440 parser->objc_pq_context = false;
7441 objc_finish_interface ();
7445 /* Parse an objc-method-type.
7447 objc-method-type:
7451 Return true if it is a class method (+) and false if it is
7452 an instance method (-).
7454 static inline bool
7455 c_parser_objc_method_type (c_parser *parser)
7457 switch (c_parser_peek_token (parser)->type)
7459 case CPP_PLUS:
7460 c_parser_consume_token (parser);
7461 return true;
7462 case CPP_MINUS:
7463 c_parser_consume_token (parser);
7464 return false;
7465 default:
7466 gcc_unreachable ();
7470 /* Parse an objc-method-definition.
7472 objc-method-definition:
7473 objc-method-type objc-method-decl ;[opt] compound-statement
7476 static void
7477 c_parser_objc_method_definition (c_parser *parser)
7479 bool is_class_method = c_parser_objc_method_type (parser);
7480 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
7481 parser->objc_pq_context = true;
7482 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7483 &expr);
7484 if (decl == error_mark_node)
7485 return; /* Bail here. */
7487 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
7489 c_parser_consume_token (parser);
7490 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7491 "extra semicolon in method definition specified");
7494 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7496 c_parser_error (parser, "expected %<{%>");
7497 return;
7500 parser->objc_pq_context = false;
7501 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
7503 add_stmt (c_parser_compound_statement (parser));
7504 objc_finish_method_definition (current_function_decl);
7506 else
7508 /* This code is executed when we find a method definition
7509 outside of an @implementation context (or invalid for other
7510 reasons). Parse the method (to keep going) but do not emit
7511 any code.
7513 c_parser_compound_statement (parser);
7517 /* Parse an objc-methodprotolist.
7519 objc-methodprotolist:
7520 empty
7521 objc-methodprotolist objc-methodproto
7522 objc-methodprotolist declaration
7523 objc-methodprotolist ;
7524 @optional
7525 @required
7527 The declaration is a data definition, which may be missing
7528 declaration specifiers under the same rules and diagnostics as
7529 other data definitions outside functions, and the stray semicolon
7530 is diagnosed the same way as a stray semicolon outside a
7531 function. */
7533 static void
7534 c_parser_objc_methodprotolist (c_parser *parser)
7536 while (true)
7538 /* The list is terminated by @end. */
7539 switch (c_parser_peek_token (parser)->type)
7541 case CPP_SEMICOLON:
7542 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
7543 "ISO C does not allow extra %<;%> outside of a function");
7544 c_parser_consume_token (parser);
7545 break;
7546 case CPP_PLUS:
7547 case CPP_MINUS:
7548 c_parser_objc_methodproto (parser);
7549 break;
7550 case CPP_PRAGMA:
7551 c_parser_pragma (parser, pragma_external);
7552 break;
7553 case CPP_EOF:
7554 return;
7555 default:
7556 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
7557 return;
7558 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
7559 c_parser_objc_at_property_declaration (parser);
7560 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
7562 objc_set_method_opt (true);
7563 c_parser_consume_token (parser);
7565 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
7567 objc_set_method_opt (false);
7568 c_parser_consume_token (parser);
7570 else
7571 c_parser_declaration_or_fndef (parser, false, false, true,
7572 false, true, NULL);
7573 break;
7578 /* Parse an objc-methodproto.
7580 objc-methodproto:
7581 objc-method-type objc-method-decl ;
7584 static void
7585 c_parser_objc_methodproto (c_parser *parser)
7587 bool is_class_method = c_parser_objc_method_type (parser);
7588 tree decl, attributes = NULL_TREE;
7590 /* Remember protocol qualifiers in prototypes. */
7591 parser->objc_pq_context = true;
7592 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
7593 NULL);
7594 /* Forget protocol qualifiers now. */
7595 parser->objc_pq_context = false;
7597 /* Do not allow the presence of attributes to hide an erroneous
7598 method implementation in the interface section. */
7599 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
7601 c_parser_error (parser, "expected %<;%>");
7602 return;
7605 if (decl != error_mark_node)
7606 objc_add_method_declaration (is_class_method, decl, attributes);
7608 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
7611 /* If we are at a position that method attributes may be present, check that
7612 there are not any parsed already (a syntax error) and then collect any
7613 specified at the current location. Finally, if new attributes were present,
7614 check that the next token is legal ( ';' for decls and '{' for defs). */
7616 static bool
7617 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
7619 bool bad = false;
7620 if (*attributes)
7622 c_parser_error (parser,
7623 "method attributes must be specified at the end only");
7624 *attributes = NULL_TREE;
7625 bad = true;
7628 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7629 *attributes = c_parser_attributes (parser);
7631 /* If there were no attributes here, just report any earlier error. */
7632 if (*attributes == NULL_TREE || bad)
7633 return bad;
7635 /* If the attributes are followed by a ; or {, then just report any earlier
7636 error. */
7637 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
7638 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7639 return bad;
7641 /* We've got attributes, but not at the end. */
7642 c_parser_error (parser,
7643 "expected %<;%> or %<{%> after method attribute definition");
7644 return true;
7647 /* Parse an objc-method-decl.
7649 objc-method-decl:
7650 ( objc-type-name ) objc-selector
7651 objc-selector
7652 ( objc-type-name ) objc-keyword-selector objc-optparmlist
7653 objc-keyword-selector objc-optparmlist
7654 attributes
7656 objc-keyword-selector:
7657 objc-keyword-decl
7658 objc-keyword-selector objc-keyword-decl
7660 objc-keyword-decl:
7661 objc-selector : ( objc-type-name ) identifier
7662 objc-selector : identifier
7663 : ( objc-type-name ) identifier
7664 : identifier
7666 objc-optparmlist:
7667 objc-optparms objc-optellipsis
7669 objc-optparms:
7670 empty
7671 objc-opt-parms , parameter-declaration
7673 objc-optellipsis:
7674 empty
7675 , ...
7678 static tree
7679 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
7680 tree *attributes, tree *expr)
7682 tree type = NULL_TREE;
7683 tree sel;
7684 tree parms = NULL_TREE;
7685 bool ellipsis = false;
7686 bool attr_err = false;
7688 *attributes = NULL_TREE;
7689 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7691 c_parser_consume_token (parser);
7692 type = c_parser_objc_type_name (parser);
7693 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7695 sel = c_parser_objc_selector (parser);
7696 /* If there is no selector, or a colon follows, we have an
7697 objc-keyword-selector. If there is a selector, and a colon does
7698 not follow, that selector ends the objc-method-decl. */
7699 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
7701 tree tsel = sel;
7702 tree list = NULL_TREE;
7703 while (true)
7705 tree atype = NULL_TREE, id, keyworddecl;
7706 tree param_attr = NULL_TREE;
7707 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7708 break;
7709 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7711 c_parser_consume_token (parser);
7712 atype = c_parser_objc_type_name (parser);
7713 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7714 "expected %<)%>");
7716 /* New ObjC allows attributes on method parameters. */
7717 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
7718 param_attr = c_parser_attributes (parser);
7719 if (c_parser_next_token_is_not (parser, CPP_NAME))
7721 c_parser_error (parser, "expected identifier");
7722 return error_mark_node;
7724 id = c_parser_peek_token (parser)->value;
7725 c_parser_consume_token (parser);
7726 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
7727 list = chainon (list, keyworddecl);
7728 tsel = c_parser_objc_selector (parser);
7729 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
7730 break;
7733 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7735 /* Parse the optional parameter list. Optional Objective-C
7736 method parameters follow the C syntax, and may include '...'
7737 to denote a variable number of arguments. */
7738 parms = make_node (TREE_LIST);
7739 while (c_parser_next_token_is (parser, CPP_COMMA))
7741 struct c_parm *parm;
7742 c_parser_consume_token (parser);
7743 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7745 ellipsis = true;
7746 c_parser_consume_token (parser);
7747 attr_err |= c_parser_objc_maybe_method_attributes
7748 (parser, attributes) ;
7749 break;
7751 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7752 if (parm == NULL)
7753 break;
7754 parms = chainon (parms,
7755 build_tree_list (NULL_TREE, grokparm (parm, expr)));
7757 sel = list;
7759 else
7760 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
7762 if (sel == NULL)
7764 c_parser_error (parser, "objective-c method declaration is expected");
7765 return error_mark_node;
7768 if (attr_err)
7769 return error_mark_node;
7771 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
7774 /* Parse an objc-type-name.
7776 objc-type-name:
7777 objc-type-qualifiers[opt] type-name
7778 objc-type-qualifiers[opt]
7780 objc-type-qualifiers:
7781 objc-type-qualifier
7782 objc-type-qualifiers objc-type-qualifier
7784 objc-type-qualifier: one of
7785 in out inout bycopy byref oneway
7788 static tree
7789 c_parser_objc_type_name (c_parser *parser)
7791 tree quals = NULL_TREE;
7792 struct c_type_name *type_name = NULL;
7793 tree type = NULL_TREE;
7794 while (true)
7796 c_token *token = c_parser_peek_token (parser);
7797 if (token->type == CPP_KEYWORD
7798 && (token->keyword == RID_IN
7799 || token->keyword == RID_OUT
7800 || token->keyword == RID_INOUT
7801 || token->keyword == RID_BYCOPY
7802 || token->keyword == RID_BYREF
7803 || token->keyword == RID_ONEWAY))
7805 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
7806 c_parser_consume_token (parser);
7808 else
7809 break;
7811 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
7812 type_name = c_parser_type_name (parser);
7813 if (type_name)
7814 type = groktypename (type_name, NULL, NULL);
7816 /* If the type is unknown, and error has already been produced and
7817 we need to recover from the error. In that case, use NULL_TREE
7818 for the type, as if no type had been specified; this will use the
7819 default type ('id') which is good for error recovery. */
7820 if (type == error_mark_node)
7821 type = NULL_TREE;
7823 return build_tree_list (quals, type);
7826 /* Parse objc-protocol-refs.
7828 objc-protocol-refs:
7829 < identifier-list >
7832 static tree
7833 c_parser_objc_protocol_refs (c_parser *parser)
7835 tree list = NULL_TREE;
7836 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
7837 c_parser_consume_token (parser);
7838 /* Any identifiers, including those declared as type names, are OK
7839 here. */
7840 while (true)
7842 tree id;
7843 if (c_parser_next_token_is_not (parser, CPP_NAME))
7845 c_parser_error (parser, "expected identifier");
7846 break;
7848 id = c_parser_peek_token (parser)->value;
7849 list = chainon (list, build_tree_list (NULL_TREE, id));
7850 c_parser_consume_token (parser);
7851 if (c_parser_next_token_is (parser, CPP_COMMA))
7852 c_parser_consume_token (parser);
7853 else
7854 break;
7856 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
7857 return list;
7860 /* Parse an objc-try-catch-finally-statement.
7862 objc-try-catch-finally-statement:
7863 @try compound-statement objc-catch-list[opt]
7864 @try compound-statement objc-catch-list[opt] @finally compound-statement
7866 objc-catch-list:
7867 @catch ( objc-catch-parameter-declaration ) compound-statement
7868 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
7870 objc-catch-parameter-declaration:
7871 parameter-declaration
7872 '...'
7874 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
7876 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
7877 for C++. Keep them in sync. */
7879 static void
7880 c_parser_objc_try_catch_finally_statement (c_parser *parser)
7882 location_t location;
7883 tree stmt;
7885 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
7886 c_parser_consume_token (parser);
7887 location = c_parser_peek_token (parser)->location;
7888 objc_maybe_warn_exceptions (location);
7889 stmt = c_parser_compound_statement (parser);
7890 objc_begin_try_stmt (location, stmt);
7892 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
7894 struct c_parm *parm;
7895 tree parameter_declaration = error_mark_node;
7896 bool seen_open_paren = false;
7898 c_parser_consume_token (parser);
7899 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7900 seen_open_paren = true;
7901 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
7903 /* We have "@catch (...)" (where the '...' are literally
7904 what is in the code). Skip the '...'.
7905 parameter_declaration is set to NULL_TREE, and
7906 objc_being_catch_clauses() knows that that means
7907 '...'. */
7908 c_parser_consume_token (parser);
7909 parameter_declaration = NULL_TREE;
7911 else
7913 /* We have "@catch (NSException *exception)" or something
7914 like that. Parse the parameter declaration. */
7915 parm = c_parser_parameter_declaration (parser, NULL_TREE);
7916 if (parm == NULL)
7917 parameter_declaration = error_mark_node;
7918 else
7919 parameter_declaration = grokparm (parm, NULL);
7921 if (seen_open_paren)
7922 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7923 else
7925 /* If there was no open parenthesis, we are recovering from
7926 an error, and we are trying to figure out what mistake
7927 the user has made. */
7929 /* If there is an immediate closing parenthesis, the user
7930 probably forgot the opening one (ie, they typed "@catch
7931 NSException *e)". Parse the closing parenthesis and keep
7932 going. */
7933 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7934 c_parser_consume_token (parser);
7936 /* If these is no immediate closing parenthesis, the user
7937 probably doesn't know that parenthesis are required at
7938 all (ie, they typed "@catch NSException *e"). So, just
7939 forget about the closing parenthesis and keep going. */
7941 objc_begin_catch_clause (parameter_declaration);
7942 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
7943 c_parser_compound_statement_nostart (parser);
7944 objc_finish_catch_clause ();
7946 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
7948 c_parser_consume_token (parser);
7949 location = c_parser_peek_token (parser)->location;
7950 stmt = c_parser_compound_statement (parser);
7951 objc_build_finally_clause (location, stmt);
7953 objc_finish_try_stmt ();
7956 /* Parse an objc-synchronized-statement.
7958 objc-synchronized-statement:
7959 @synchronized ( expression ) compound-statement
7962 static void
7963 c_parser_objc_synchronized_statement (c_parser *parser)
7965 location_t loc;
7966 tree expr, stmt;
7967 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
7968 c_parser_consume_token (parser);
7969 loc = c_parser_peek_token (parser)->location;
7970 objc_maybe_warn_exceptions (loc);
7971 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7973 expr = c_parser_expression (parser).value;
7974 expr = c_fully_fold (expr, false, NULL);
7975 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7977 else
7978 expr = error_mark_node;
7979 stmt = c_parser_compound_statement (parser);
7980 objc_build_synchronized (loc, expr, stmt);
7983 /* Parse an objc-selector; return NULL_TREE without an error if the
7984 next token is not an objc-selector.
7986 objc-selector:
7987 identifier
7988 one of
7989 enum struct union if else while do for switch case default
7990 break continue return goto asm sizeof typeof __alignof
7991 unsigned long const short volatile signed restrict _Complex
7992 in out inout bycopy byref oneway int char float double void _Bool
7994 ??? Why this selection of keywords but not, for example, storage
7995 class specifiers? */
7997 static tree
7998 c_parser_objc_selector (c_parser *parser)
8000 c_token *token = c_parser_peek_token (parser);
8001 tree value = token->value;
8002 if (token->type == CPP_NAME)
8004 c_parser_consume_token (parser);
8005 return value;
8007 if (token->type != CPP_KEYWORD)
8008 return NULL_TREE;
8009 switch (token->keyword)
8011 case RID_ENUM:
8012 case RID_STRUCT:
8013 case RID_UNION:
8014 case RID_IF:
8015 case RID_ELSE:
8016 case RID_WHILE:
8017 case RID_DO:
8018 case RID_FOR:
8019 case RID_SWITCH:
8020 case RID_CASE:
8021 case RID_DEFAULT:
8022 case RID_BREAK:
8023 case RID_CONTINUE:
8024 case RID_RETURN:
8025 case RID_GOTO:
8026 case RID_ASM:
8027 case RID_SIZEOF:
8028 case RID_TYPEOF:
8029 case RID_ALIGNOF:
8030 case RID_UNSIGNED:
8031 case RID_LONG:
8032 case RID_INT128:
8033 case RID_CONST:
8034 case RID_SHORT:
8035 case RID_VOLATILE:
8036 case RID_SIGNED:
8037 case RID_RESTRICT:
8038 case RID_COMPLEX:
8039 case RID_IN:
8040 case RID_OUT:
8041 case RID_INOUT:
8042 case RID_BYCOPY:
8043 case RID_BYREF:
8044 case RID_ONEWAY:
8045 case RID_INT:
8046 case RID_CHAR:
8047 case RID_FLOAT:
8048 case RID_DOUBLE:
8049 case RID_VOID:
8050 case RID_BOOL:
8051 c_parser_consume_token (parser);
8052 return value;
8053 default:
8054 return NULL_TREE;
8058 /* Parse an objc-selector-arg.
8060 objc-selector-arg:
8061 objc-selector
8062 objc-keywordname-list
8064 objc-keywordname-list:
8065 objc-keywordname
8066 objc-keywordname-list objc-keywordname
8068 objc-keywordname:
8069 objc-selector :
8073 static tree
8074 c_parser_objc_selector_arg (c_parser *parser)
8076 tree sel = c_parser_objc_selector (parser);
8077 tree list = NULL_TREE;
8078 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8079 return sel;
8080 while (true)
8082 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8083 return list;
8084 list = chainon (list, build_tree_list (sel, NULL_TREE));
8085 sel = c_parser_objc_selector (parser);
8086 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8087 break;
8089 return list;
8092 /* Parse an objc-receiver.
8094 objc-receiver:
8095 expression
8096 class-name
8097 type-name
8100 static tree
8101 c_parser_objc_receiver (c_parser *parser)
8103 if (c_parser_peek_token (parser)->type == CPP_NAME
8104 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8105 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8107 tree id = c_parser_peek_token (parser)->value;
8108 c_parser_consume_token (parser);
8109 return objc_get_class_reference (id);
8111 return c_fully_fold (c_parser_expression (parser).value, false, NULL);
8114 /* Parse objc-message-args.
8116 objc-message-args:
8117 objc-selector
8118 objc-keywordarg-list
8120 objc-keywordarg-list:
8121 objc-keywordarg
8122 objc-keywordarg-list objc-keywordarg
8124 objc-keywordarg:
8125 objc-selector : objc-keywordexpr
8126 : objc-keywordexpr
8129 static tree
8130 c_parser_objc_message_args (c_parser *parser)
8132 tree sel = c_parser_objc_selector (parser);
8133 tree list = NULL_TREE;
8134 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8135 return sel;
8136 while (true)
8138 tree keywordexpr;
8139 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8140 return error_mark_node;
8141 keywordexpr = c_parser_objc_keywordexpr (parser);
8142 list = chainon (list, build_tree_list (sel, keywordexpr));
8143 sel = c_parser_objc_selector (parser);
8144 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8145 break;
8147 return list;
8150 /* Parse an objc-keywordexpr.
8152 objc-keywordexpr:
8153 nonempty-expr-list
8156 static tree
8157 c_parser_objc_keywordexpr (c_parser *parser)
8159 tree ret;
8160 VEC(tree,gc) *expr_list = c_parser_expr_list (parser, true, true, NULL);
8161 if (VEC_length (tree, expr_list) == 1)
8163 /* Just return the expression, remove a level of
8164 indirection. */
8165 ret = VEC_index (tree, expr_list, 0);
8167 else
8169 /* We have a comma expression, we will collapse later. */
8170 ret = build_tree_list_vec (expr_list);
8172 release_tree_vector (expr_list);
8173 return ret;
8176 /* A check, needed in several places, that ObjC interface, implementation or
8177 method definitions are not prefixed by incorrect items. */
8178 static bool
8179 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
8180 struct c_declspecs *specs)
8182 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
8183 || specs->typespec_kind != ctsk_none)
8185 c_parser_error (parser,
8186 "no type or storage class may be specified here,");
8187 c_parser_skip_to_end_of_block_or_statement (parser);
8188 return true;
8190 return false;
8193 /* Parse an Objective-C @property declaration. The syntax is:
8195 objc-property-declaration:
8196 '@property' objc-property-attributes[opt] struct-declaration ;
8198 objc-property-attributes:
8199 '(' objc-property-attribute-list ')'
8201 objc-property-attribute-list:
8202 objc-property-attribute
8203 objc-property-attribute-list, objc-property-attribute
8205 objc-property-attribute
8206 'getter' = identifier
8207 'setter' = identifier
8208 'readonly'
8209 'readwrite'
8210 'assign'
8211 'retain'
8212 'copy'
8213 'nonatomic'
8215 For example:
8216 @property NSString *name;
8217 @property (readonly) id object;
8218 @property (retain, nonatomic, getter=getTheName) id name;
8219 @property int a, b, c;
8221 PS: This function is identical to cp_parser_objc_at_propery_declaration
8222 for C++. Keep them in sync. */
8223 static void
8224 c_parser_objc_at_property_declaration (c_parser *parser)
8226 /* The following variables hold the attributes of the properties as
8227 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
8228 seen. When we see an attribute, we set them to 'true' (if they
8229 are boolean properties) or to the identifier (if they have an
8230 argument, ie, for getter and setter). Note that here we only
8231 parse the list of attributes, check the syntax and accumulate the
8232 attributes that we find. objc_add_property_declaration() will
8233 then process the information. */
8234 bool property_assign = false;
8235 bool property_copy = false;
8236 tree property_getter_ident = NULL_TREE;
8237 bool property_nonatomic = false;
8238 bool property_readonly = false;
8239 bool property_readwrite = false;
8240 bool property_retain = false;
8241 tree property_setter_ident = NULL_TREE;
8243 /* 'properties' is the list of properties that we read. Usually a
8244 single one, but maybe more (eg, in "@property int a, b, c;" there
8245 are three). */
8246 tree properties;
8247 location_t loc;
8249 loc = c_parser_peek_token (parser)->location;
8250 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
8252 c_parser_consume_token (parser); /* Eat '@property'. */
8254 /* Parse the optional attribute list... */
8255 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8257 /* Eat the '(' */
8258 c_parser_consume_token (parser);
8260 /* Property attribute keywords are valid now. */
8261 parser->objc_property_attr_context = true;
8263 while (true)
8265 bool syntax_error = false;
8266 c_token *token = c_parser_peek_token (parser);
8267 enum rid keyword;
8269 if (token->type != CPP_KEYWORD)
8271 if (token->type == CPP_CLOSE_PAREN)
8272 c_parser_error (parser, "expected identifier");
8273 else
8275 c_parser_consume_token (parser);
8276 c_parser_error (parser, "unknown property attribute");
8278 break;
8280 keyword = token->keyword;
8281 c_parser_consume_token (parser);
8282 switch (keyword)
8284 case RID_ASSIGN: property_assign = true; break;
8285 case RID_COPY: property_copy = true; break;
8286 case RID_NONATOMIC: property_nonatomic = true; break;
8287 case RID_READONLY: property_readonly = true; break;
8288 case RID_READWRITE: property_readwrite = true; break;
8289 case RID_RETAIN: property_retain = true; break;
8291 case RID_GETTER:
8292 case RID_SETTER:
8293 if (c_parser_next_token_is_not (parser, CPP_EQ))
8295 if (keyword == RID_GETTER)
8296 c_parser_error (parser,
8297 "missing %<=%> (after %<getter%> attribute)");
8298 else
8299 c_parser_error (parser,
8300 "missing %<=%> (after %<setter%> attribute)");
8301 syntax_error = true;
8302 break;
8304 c_parser_consume_token (parser); /* eat the = */
8305 if (c_parser_next_token_is_not (parser, CPP_NAME))
8307 c_parser_error (parser, "expected identifier");
8308 syntax_error = true;
8309 break;
8311 if (keyword == RID_SETTER)
8313 if (property_setter_ident != NULL_TREE)
8314 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
8315 else
8316 property_setter_ident = c_parser_peek_token (parser)->value;
8317 c_parser_consume_token (parser);
8318 if (c_parser_next_token_is_not (parser, CPP_COLON))
8319 c_parser_error (parser, "setter name must terminate with %<:%>");
8320 else
8321 c_parser_consume_token (parser);
8323 else
8325 if (property_getter_ident != NULL_TREE)
8326 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
8327 else
8328 property_getter_ident = c_parser_peek_token (parser)->value;
8329 c_parser_consume_token (parser);
8331 break;
8332 default:
8333 c_parser_error (parser, "unknown property attribute");
8334 syntax_error = true;
8335 break;
8338 if (syntax_error)
8339 break;
8341 if (c_parser_next_token_is (parser, CPP_COMMA))
8342 c_parser_consume_token (parser);
8343 else
8344 break;
8346 parser->objc_property_attr_context = false;
8347 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8349 /* ... and the property declaration(s). */
8350 properties = c_parser_struct_declaration (parser);
8352 if (properties == error_mark_node)
8354 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8355 parser->error = false;
8356 return;
8359 if (properties == NULL_TREE)
8360 c_parser_error (parser, "expected identifier");
8361 else
8363 /* Comma-separated properties are chained together in
8364 reverse order; add them one by one. */
8365 properties = nreverse (properties);
8367 for (; properties; properties = TREE_CHAIN (properties))
8368 objc_add_property_declaration (loc, copy_node (properties),
8369 property_readonly, property_readwrite,
8370 property_assign, property_retain,
8371 property_copy, property_nonatomic,
8372 property_getter_ident, property_setter_ident);
8375 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8376 parser->error = false;
8379 /* Parse an Objective-C @synthesize declaration. The syntax is:
8381 objc-synthesize-declaration:
8382 @synthesize objc-synthesize-identifier-list ;
8384 objc-synthesize-identifier-list:
8385 objc-synthesize-identifier
8386 objc-synthesize-identifier-list, objc-synthesize-identifier
8388 objc-synthesize-identifier
8389 identifier
8390 identifier = identifier
8392 For example:
8393 @synthesize MyProperty;
8394 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
8396 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
8397 for C++. Keep them in sync.
8399 static void
8400 c_parser_objc_at_synthesize_declaration (c_parser *parser)
8402 tree list = NULL_TREE;
8403 location_t loc;
8404 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
8405 loc = c_parser_peek_token (parser)->location;
8407 c_parser_consume_token (parser);
8408 while (true)
8410 tree property, ivar;
8411 if (c_parser_next_token_is_not (parser, CPP_NAME))
8413 c_parser_error (parser, "expected identifier");
8414 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8415 /* Once we find the semicolon, we can resume normal parsing.
8416 We have to reset parser->error manually because
8417 c_parser_skip_until_found() won't reset it for us if the
8418 next token is precisely a semicolon. */
8419 parser->error = false;
8420 return;
8422 property = c_parser_peek_token (parser)->value;
8423 c_parser_consume_token (parser);
8424 if (c_parser_next_token_is (parser, CPP_EQ))
8426 c_parser_consume_token (parser);
8427 if (c_parser_next_token_is_not (parser, CPP_NAME))
8429 c_parser_error (parser, "expected identifier");
8430 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8431 parser->error = false;
8432 return;
8434 ivar = c_parser_peek_token (parser)->value;
8435 c_parser_consume_token (parser);
8437 else
8438 ivar = NULL_TREE;
8439 list = chainon (list, build_tree_list (ivar, property));
8440 if (c_parser_next_token_is (parser, CPP_COMMA))
8441 c_parser_consume_token (parser);
8442 else
8443 break;
8445 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8446 objc_add_synthesize_declaration (loc, list);
8449 /* Parse an Objective-C @dynamic declaration. The syntax is:
8451 objc-dynamic-declaration:
8452 @dynamic identifier-list ;
8454 For example:
8455 @dynamic MyProperty;
8456 @dynamic MyProperty, AnotherProperty;
8458 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
8459 for C++. Keep them in sync.
8461 static void
8462 c_parser_objc_at_dynamic_declaration (c_parser *parser)
8464 tree list = NULL_TREE;
8465 location_t loc;
8466 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
8467 loc = c_parser_peek_token (parser)->location;
8469 c_parser_consume_token (parser);
8470 while (true)
8472 tree property;
8473 if (c_parser_next_token_is_not (parser, CPP_NAME))
8475 c_parser_error (parser, "expected identifier");
8476 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8477 parser->error = false;
8478 return;
8480 property = c_parser_peek_token (parser)->value;
8481 list = chainon (list, build_tree_list (NULL_TREE, property));
8482 c_parser_consume_token (parser);
8483 if (c_parser_next_token_is (parser, CPP_COMMA))
8484 c_parser_consume_token (parser);
8485 else
8486 break;
8488 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8489 objc_add_dynamic_declaration (loc, list);
8493 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
8494 should be considered, statements. ALLOW_STMT is true if we're within
8495 the context of a function and such pragmas are to be allowed. Returns
8496 true if we actually parsed such a pragma. */
8498 static bool
8499 c_parser_pragma (c_parser *parser, enum pragma_context context)
8501 unsigned int id;
8503 id = c_parser_peek_token (parser)->pragma_kind;
8504 gcc_assert (id != PRAGMA_NONE);
8506 switch (id)
8508 case PRAGMA_OMP_BARRIER:
8509 if (context != pragma_compound)
8511 if (context == pragma_stmt)
8512 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
8513 "used in compound statements");
8514 goto bad_stmt;
8516 c_parser_omp_barrier (parser);
8517 return false;
8519 case PRAGMA_OMP_FLUSH:
8520 if (context != pragma_compound)
8522 if (context == pragma_stmt)
8523 c_parser_error (parser, "%<#pragma omp flush%> may only be "
8524 "used in compound statements");
8525 goto bad_stmt;
8527 c_parser_omp_flush (parser);
8528 return false;
8530 case PRAGMA_OMP_TASKWAIT:
8531 if (context != pragma_compound)
8533 if (context == pragma_stmt)
8534 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
8535 "used in compound statements");
8536 goto bad_stmt;
8538 c_parser_omp_taskwait (parser);
8539 return false;
8541 case PRAGMA_OMP_TASKYIELD:
8542 if (context != pragma_compound)
8544 if (context == pragma_stmt)
8545 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
8546 "used in compound statements");
8547 goto bad_stmt;
8549 c_parser_omp_taskyield (parser);
8550 return false;
8552 case PRAGMA_OMP_THREADPRIVATE:
8553 c_parser_omp_threadprivate (parser);
8554 return false;
8556 case PRAGMA_OMP_SECTION:
8557 error_at (c_parser_peek_token (parser)->location,
8558 "%<#pragma omp section%> may only be used in "
8559 "%<#pragma omp sections%> construct");
8560 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8561 return false;
8563 case PRAGMA_GCC_PCH_PREPROCESS:
8564 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
8565 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8566 return false;
8568 default:
8569 if (id < PRAGMA_FIRST_EXTERNAL)
8571 if (context == pragma_external)
8573 bad_stmt:
8574 c_parser_error (parser, "expected declaration specifiers");
8575 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
8576 return false;
8578 c_parser_omp_construct (parser);
8579 return true;
8581 break;
8584 c_parser_consume_pragma (parser);
8585 c_invoke_pragma_handler (id);
8587 /* Skip to EOL, but suppress any error message. Those will have been
8588 generated by the handler routine through calling error, as opposed
8589 to calling c_parser_error. */
8590 parser->error = true;
8591 c_parser_skip_to_pragma_eol (parser);
8593 return false;
8596 /* The interface the pragma parsers have to the lexer. */
8598 enum cpp_ttype
8599 pragma_lex (tree *value)
8601 c_token *tok = c_parser_peek_token (the_parser);
8602 enum cpp_ttype ret = tok->type;
8604 *value = tok->value;
8605 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
8606 ret = CPP_EOF;
8607 else
8609 if (ret == CPP_KEYWORD)
8610 ret = CPP_NAME;
8611 c_parser_consume_token (the_parser);
8614 return ret;
8617 static void
8618 c_parser_pragma_pch_preprocess (c_parser *parser)
8620 tree name = NULL;
8622 c_parser_consume_pragma (parser);
8623 if (c_parser_next_token_is (parser, CPP_STRING))
8625 name = c_parser_peek_token (parser)->value;
8626 c_parser_consume_token (parser);
8628 else
8629 c_parser_error (parser, "expected string literal");
8630 c_parser_skip_to_pragma_eol (parser);
8632 if (name)
8633 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
8636 /* OpenMP 2.5 parsing routines. */
8638 /* Returns name of the next clause.
8639 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
8640 the token is not consumed. Otherwise appropriate pragma_omp_clause is
8641 returned and the token is consumed. */
8643 static pragma_omp_clause
8644 c_parser_omp_clause_name (c_parser *parser)
8646 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
8648 if (c_parser_next_token_is_keyword (parser, RID_IF))
8649 result = PRAGMA_OMP_CLAUSE_IF;
8650 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
8651 result = PRAGMA_OMP_CLAUSE_DEFAULT;
8652 else if (c_parser_next_token_is (parser, CPP_NAME))
8654 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8656 switch (p[0])
8658 case 'c':
8659 if (!strcmp ("collapse", p))
8660 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
8661 else if (!strcmp ("copyin", p))
8662 result = PRAGMA_OMP_CLAUSE_COPYIN;
8663 else if (!strcmp ("copyprivate", p))
8664 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
8665 break;
8666 case 'f':
8667 if (!strcmp ("final", p))
8668 result = PRAGMA_OMP_CLAUSE_FINAL;
8669 else if (!strcmp ("firstprivate", p))
8670 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
8671 break;
8672 case 'l':
8673 if (!strcmp ("lastprivate", p))
8674 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
8675 break;
8676 case 'm':
8677 if (!strcmp ("mergeable", p))
8678 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
8679 break;
8680 case 'n':
8681 if (!strcmp ("nowait", p))
8682 result = PRAGMA_OMP_CLAUSE_NOWAIT;
8683 else if (!strcmp ("num_threads", p))
8684 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
8685 break;
8686 case 'o':
8687 if (!strcmp ("ordered", p))
8688 result = PRAGMA_OMP_CLAUSE_ORDERED;
8689 break;
8690 case 'p':
8691 if (!strcmp ("private", p))
8692 result = PRAGMA_OMP_CLAUSE_PRIVATE;
8693 break;
8694 case 'r':
8695 if (!strcmp ("reduction", p))
8696 result = PRAGMA_OMP_CLAUSE_REDUCTION;
8697 break;
8698 case 's':
8699 if (!strcmp ("schedule", p))
8700 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
8701 else if (!strcmp ("shared", p))
8702 result = PRAGMA_OMP_CLAUSE_SHARED;
8703 break;
8704 case 'u':
8705 if (!strcmp ("untied", p))
8706 result = PRAGMA_OMP_CLAUSE_UNTIED;
8707 break;
8711 if (result != PRAGMA_OMP_CLAUSE_NONE)
8712 c_parser_consume_token (parser);
8714 return result;
8717 /* Validate that a clause of the given type does not already exist. */
8719 static void
8720 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
8721 const char *name)
8723 tree c;
8725 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
8726 if (OMP_CLAUSE_CODE (c) == code)
8728 location_t loc = OMP_CLAUSE_LOCATION (c);
8729 error_at (loc, "too many %qs clauses", name);
8730 break;
8734 /* OpenMP 2.5:
8735 variable-list:
8736 identifier
8737 variable-list , identifier
8739 If KIND is nonzero, create the appropriate node and install the
8740 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
8741 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
8743 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
8744 return the list created. */
8746 static tree
8747 c_parser_omp_variable_list (c_parser *parser,
8748 location_t clause_loc,
8749 enum omp_clause_code kind,
8750 tree list)
8752 if (c_parser_next_token_is_not (parser, CPP_NAME)
8753 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
8754 c_parser_error (parser, "expected identifier");
8756 while (c_parser_next_token_is (parser, CPP_NAME)
8757 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
8759 tree t = lookup_name (c_parser_peek_token (parser)->value);
8761 if (t == NULL_TREE)
8762 undeclared_variable (c_parser_peek_token (parser)->location,
8763 c_parser_peek_token (parser)->value);
8764 else if (t == error_mark_node)
8766 else if (kind != 0)
8768 tree u = build_omp_clause (clause_loc, kind);
8769 OMP_CLAUSE_DECL (u) = t;
8770 OMP_CLAUSE_CHAIN (u) = list;
8771 list = u;
8773 else
8774 list = tree_cons (t, NULL_TREE, list);
8776 c_parser_consume_token (parser);
8778 if (c_parser_next_token_is_not (parser, CPP_COMMA))
8779 break;
8781 c_parser_consume_token (parser);
8784 return list;
8787 /* Similarly, but expect leading and trailing parenthesis. This is a very
8788 common case for omp clauses. */
8790 static tree
8791 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
8792 tree list)
8794 /* The clauses location. */
8795 location_t loc = c_parser_peek_token (parser)->location;
8797 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8799 list = c_parser_omp_variable_list (parser, loc, kind, list);
8800 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8802 return list;
8805 /* OpenMP 3.0:
8806 collapse ( constant-expression ) */
8808 static tree
8809 c_parser_omp_clause_collapse (c_parser *parser, tree list)
8811 tree c, num = error_mark_node;
8812 HOST_WIDE_INT n;
8813 location_t loc;
8815 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
8817 loc = c_parser_peek_token (parser)->location;
8818 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8820 num = c_parser_expr_no_commas (parser, NULL).value;
8821 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8823 if (num == error_mark_node)
8824 return list;
8825 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
8826 || !host_integerp (num, 0)
8827 || (n = tree_low_cst (num, 0)) <= 0
8828 || (int) n != n)
8830 error_at (loc,
8831 "collapse argument needs positive constant integer expression");
8832 return list;
8834 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
8835 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
8836 OMP_CLAUSE_CHAIN (c) = list;
8837 return c;
8840 /* OpenMP 2.5:
8841 copyin ( variable-list ) */
8843 static tree
8844 c_parser_omp_clause_copyin (c_parser *parser, tree list)
8846 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
8849 /* OpenMP 2.5:
8850 copyprivate ( variable-list ) */
8852 static tree
8853 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
8855 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
8858 /* OpenMP 2.5:
8859 default ( shared | none ) */
8861 static tree
8862 c_parser_omp_clause_default (c_parser *parser, tree list)
8864 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
8865 location_t loc = c_parser_peek_token (parser)->location;
8866 tree c;
8868 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8869 return list;
8870 if (c_parser_next_token_is (parser, CPP_NAME))
8872 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
8874 switch (p[0])
8876 case 'n':
8877 if (strcmp ("none", p) != 0)
8878 goto invalid_kind;
8879 kind = OMP_CLAUSE_DEFAULT_NONE;
8880 break;
8882 case 's':
8883 if (strcmp ("shared", p) != 0)
8884 goto invalid_kind;
8885 kind = OMP_CLAUSE_DEFAULT_SHARED;
8886 break;
8888 default:
8889 goto invalid_kind;
8892 c_parser_consume_token (parser);
8894 else
8896 invalid_kind:
8897 c_parser_error (parser, "expected %<none%> or %<shared%>");
8899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8901 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
8902 return list;
8904 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
8905 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
8906 OMP_CLAUSE_CHAIN (c) = list;
8907 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
8909 return c;
8912 /* OpenMP 2.5:
8913 firstprivate ( variable-list ) */
8915 static tree
8916 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
8918 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
8921 /* OpenMP 3.1:
8922 final ( expression ) */
8924 static tree
8925 c_parser_omp_clause_final (c_parser *parser, tree list)
8927 location_t loc = c_parser_peek_token (parser)->location;
8928 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8930 tree t = c_parser_paren_condition (parser);
8931 tree c;
8933 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
8935 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
8936 OMP_CLAUSE_FINAL_EXPR (c) = t;
8937 OMP_CLAUSE_CHAIN (c) = list;
8938 list = c;
8940 else
8941 c_parser_error (parser, "expected %<(%>");
8943 return list;
8946 /* OpenMP 2.5:
8947 if ( expression ) */
8949 static tree
8950 c_parser_omp_clause_if (c_parser *parser, tree list)
8952 location_t loc = c_parser_peek_token (parser)->location;
8953 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8955 tree t = c_parser_paren_condition (parser);
8956 tree c;
8958 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
8960 c = build_omp_clause (loc, OMP_CLAUSE_IF);
8961 OMP_CLAUSE_IF_EXPR (c) = t;
8962 OMP_CLAUSE_CHAIN (c) = list;
8963 list = c;
8965 else
8966 c_parser_error (parser, "expected %<(%>");
8968 return list;
8971 /* OpenMP 2.5:
8972 lastprivate ( variable-list ) */
8974 static tree
8975 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
8977 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
8980 /* OpenMP 3.1:
8981 mergeable */
8983 static tree
8984 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
8986 tree c;
8988 /* FIXME: Should we allow duplicates? */
8989 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
8991 c = build_omp_clause (c_parser_peek_token (parser)->location,
8992 OMP_CLAUSE_MERGEABLE);
8993 OMP_CLAUSE_CHAIN (c) = list;
8995 return c;
8998 /* OpenMP 2.5:
8999 nowait */
9001 static tree
9002 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9004 tree c;
9005 location_t loc = c_parser_peek_token (parser)->location;
9007 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
9009 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
9010 OMP_CLAUSE_CHAIN (c) = list;
9011 return c;
9014 /* OpenMP 2.5:
9015 num_threads ( expression ) */
9017 static tree
9018 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
9020 location_t num_threads_loc = c_parser_peek_token (parser)->location;
9021 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9023 location_t expr_loc = c_parser_peek_token (parser)->location;
9024 tree c, t = c_parser_expression (parser).value;
9025 mark_exp_read (t);
9026 t = c_fully_fold (t, false, NULL);
9028 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9030 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9032 c_parser_error (parser, "expected integer expression");
9033 return list;
9036 /* Attempt to statically determine when the number isn't positive. */
9037 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
9038 build_int_cst (TREE_TYPE (t), 0));
9039 if (CAN_HAVE_LOCATION_P (c))
9040 SET_EXPR_LOCATION (c, expr_loc);
9041 if (c == boolean_true_node)
9043 warning_at (expr_loc, 0,
9044 "%<num_threads%> value must be positive");
9045 t = integer_one_node;
9048 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
9050 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
9051 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
9052 OMP_CLAUSE_CHAIN (c) = list;
9053 list = c;
9056 return list;
9059 /* OpenMP 2.5:
9060 ordered */
9062 static tree
9063 c_parser_omp_clause_ordered (c_parser *parser, tree list)
9065 tree c;
9067 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
9069 c = build_omp_clause (c_parser_peek_token (parser)->location,
9070 OMP_CLAUSE_ORDERED);
9071 OMP_CLAUSE_CHAIN (c) = list;
9073 return c;
9076 /* OpenMP 2.5:
9077 private ( variable-list ) */
9079 static tree
9080 c_parser_omp_clause_private (c_parser *parser, tree list)
9082 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
9085 /* OpenMP 2.5:
9086 reduction ( reduction-operator : variable-list )
9088 reduction-operator:
9089 One of: + * - & ^ | && ||
9091 OpenMP 3.1:
9093 reduction-operator:
9094 One of: + * - & ^ | && || max min */
9096 static tree
9097 c_parser_omp_clause_reduction (c_parser *parser, tree list)
9099 location_t clause_loc = c_parser_peek_token (parser)->location;
9100 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9102 enum tree_code code;
9104 switch (c_parser_peek_token (parser)->type)
9106 case CPP_PLUS:
9107 code = PLUS_EXPR;
9108 break;
9109 case CPP_MULT:
9110 code = MULT_EXPR;
9111 break;
9112 case CPP_MINUS:
9113 code = MINUS_EXPR;
9114 break;
9115 case CPP_AND:
9116 code = BIT_AND_EXPR;
9117 break;
9118 case CPP_XOR:
9119 code = BIT_XOR_EXPR;
9120 break;
9121 case CPP_OR:
9122 code = BIT_IOR_EXPR;
9123 break;
9124 case CPP_AND_AND:
9125 code = TRUTH_ANDIF_EXPR;
9126 break;
9127 case CPP_OR_OR:
9128 code = TRUTH_ORIF_EXPR;
9129 break;
9130 case CPP_NAME:
9132 const char *p
9133 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9134 if (strcmp (p, "min") == 0)
9136 code = MIN_EXPR;
9137 break;
9139 if (strcmp (p, "max") == 0)
9141 code = MAX_EXPR;
9142 break;
9145 /* FALLTHRU */
9146 default:
9147 c_parser_error (parser,
9148 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
9149 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
9150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9151 return list;
9153 c_parser_consume_token (parser);
9154 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9156 tree nl, c;
9158 nl = c_parser_omp_variable_list (parser, clause_loc,
9159 OMP_CLAUSE_REDUCTION, list);
9160 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
9161 OMP_CLAUSE_REDUCTION_CODE (c) = code;
9163 list = nl;
9165 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9167 return list;
9170 /* OpenMP 2.5:
9171 schedule ( schedule-kind )
9172 schedule ( schedule-kind , expression )
9174 schedule-kind:
9175 static | dynamic | guided | runtime | auto
9178 static tree
9179 c_parser_omp_clause_schedule (c_parser *parser, tree list)
9181 tree c, t;
9182 location_t loc = c_parser_peek_token (parser)->location;
9184 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9185 return list;
9187 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
9189 if (c_parser_next_token_is (parser, CPP_NAME))
9191 tree kind = c_parser_peek_token (parser)->value;
9192 const char *p = IDENTIFIER_POINTER (kind);
9194 switch (p[0])
9196 case 'd':
9197 if (strcmp ("dynamic", p) != 0)
9198 goto invalid_kind;
9199 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
9200 break;
9202 case 'g':
9203 if (strcmp ("guided", p) != 0)
9204 goto invalid_kind;
9205 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
9206 break;
9208 case 'r':
9209 if (strcmp ("runtime", p) != 0)
9210 goto invalid_kind;
9211 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
9212 break;
9214 default:
9215 goto invalid_kind;
9218 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
9219 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
9220 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
9221 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
9222 else
9223 goto invalid_kind;
9225 c_parser_consume_token (parser);
9226 if (c_parser_next_token_is (parser, CPP_COMMA))
9228 location_t here;
9229 c_parser_consume_token (parser);
9231 here = c_parser_peek_token (parser)->location;
9232 t = c_parser_expr_no_commas (parser, NULL).value;
9233 mark_exp_read (t);
9234 t = c_fully_fold (t, false, NULL);
9236 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
9237 error_at (here, "schedule %<runtime%> does not take "
9238 "a %<chunk_size%> parameter");
9239 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
9240 error_at (here,
9241 "schedule %<auto%> does not take "
9242 "a %<chunk_size%> parameter");
9243 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
9244 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
9245 else
9246 c_parser_error (parser, "expected integer expression");
9248 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9250 else
9251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9252 "expected %<,%> or %<)%>");
9254 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
9255 OMP_CLAUSE_CHAIN (c) = list;
9256 return c;
9258 invalid_kind:
9259 c_parser_error (parser, "invalid schedule kind");
9260 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
9261 return list;
9264 /* OpenMP 2.5:
9265 shared ( variable-list ) */
9267 static tree
9268 c_parser_omp_clause_shared (c_parser *parser, tree list)
9270 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
9273 /* OpenMP 3.0:
9274 untied */
9276 static tree
9277 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
9279 tree c;
9281 /* FIXME: Should we allow duplicates? */
9282 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
9284 c = build_omp_clause (c_parser_peek_token (parser)->location,
9285 OMP_CLAUSE_UNTIED);
9286 OMP_CLAUSE_CHAIN (c) = list;
9288 return c;
9291 /* Parse all OpenMP clauses. The set clauses allowed by the directive
9292 is a bitmask in MASK. Return the list of clauses found; the result
9293 of clause default goes in *pdefault. */
9295 static tree
9296 c_parser_omp_all_clauses (c_parser *parser, unsigned int mask,
9297 const char *where)
9299 tree clauses = NULL;
9300 bool first = true;
9302 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9304 location_t here;
9305 pragma_omp_clause c_kind;
9306 const char *c_name;
9307 tree prev = clauses;
9309 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
9310 c_parser_consume_token (parser);
9312 first = false;
9313 here = c_parser_peek_token (parser)->location;
9314 c_kind = c_parser_omp_clause_name (parser);
9316 switch (c_kind)
9318 case PRAGMA_OMP_CLAUSE_COLLAPSE:
9319 clauses = c_parser_omp_clause_collapse (parser, clauses);
9320 c_name = "collapse";
9321 break;
9322 case PRAGMA_OMP_CLAUSE_COPYIN:
9323 clauses = c_parser_omp_clause_copyin (parser, clauses);
9324 c_name = "copyin";
9325 break;
9326 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
9327 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
9328 c_name = "copyprivate";
9329 break;
9330 case PRAGMA_OMP_CLAUSE_DEFAULT:
9331 clauses = c_parser_omp_clause_default (parser, clauses);
9332 c_name = "default";
9333 break;
9334 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
9335 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
9336 c_name = "firstprivate";
9337 break;
9338 case PRAGMA_OMP_CLAUSE_FINAL:
9339 clauses = c_parser_omp_clause_final (parser, clauses);
9340 c_name = "final";
9341 break;
9342 case PRAGMA_OMP_CLAUSE_IF:
9343 clauses = c_parser_omp_clause_if (parser, clauses);
9344 c_name = "if";
9345 break;
9346 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
9347 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
9348 c_name = "lastprivate";
9349 break;
9350 case PRAGMA_OMP_CLAUSE_MERGEABLE:
9351 clauses = c_parser_omp_clause_mergeable (parser, clauses);
9352 c_name = "mergeable";
9353 break;
9354 case PRAGMA_OMP_CLAUSE_NOWAIT:
9355 clauses = c_parser_omp_clause_nowait (parser, clauses);
9356 c_name = "nowait";
9357 break;
9358 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
9359 clauses = c_parser_omp_clause_num_threads (parser, clauses);
9360 c_name = "num_threads";
9361 break;
9362 case PRAGMA_OMP_CLAUSE_ORDERED:
9363 clauses = c_parser_omp_clause_ordered (parser, clauses);
9364 c_name = "ordered";
9365 break;
9366 case PRAGMA_OMP_CLAUSE_PRIVATE:
9367 clauses = c_parser_omp_clause_private (parser, clauses);
9368 c_name = "private";
9369 break;
9370 case PRAGMA_OMP_CLAUSE_REDUCTION:
9371 clauses = c_parser_omp_clause_reduction (parser, clauses);
9372 c_name = "reduction";
9373 break;
9374 case PRAGMA_OMP_CLAUSE_SCHEDULE:
9375 clauses = c_parser_omp_clause_schedule (parser, clauses);
9376 c_name = "schedule";
9377 break;
9378 case PRAGMA_OMP_CLAUSE_SHARED:
9379 clauses = c_parser_omp_clause_shared (parser, clauses);
9380 c_name = "shared";
9381 break;
9382 case PRAGMA_OMP_CLAUSE_UNTIED:
9383 clauses = c_parser_omp_clause_untied (parser, clauses);
9384 c_name = "untied";
9385 break;
9386 default:
9387 c_parser_error (parser, "expected %<#pragma omp%> clause");
9388 goto saw_error;
9391 if (((mask >> c_kind) & 1) == 0 && !parser->error)
9393 /* Remove the invalid clause(s) from the list to avoid
9394 confusing the rest of the compiler. */
9395 clauses = prev;
9396 error_at (here, "%qs is not valid for %qs", c_name, where);
9400 saw_error:
9401 c_parser_skip_to_pragma_eol (parser);
9403 return c_finish_omp_clauses (clauses);
9406 /* OpenMP 2.5:
9407 structured-block:
9408 statement
9410 In practice, we're also interested in adding the statement to an
9411 outer node. So it is convenient if we work around the fact that
9412 c_parser_statement calls add_stmt. */
9414 static tree
9415 c_parser_omp_structured_block (c_parser *parser)
9417 tree stmt = push_stmt_list ();
9418 c_parser_statement (parser);
9419 return pop_stmt_list (stmt);
9422 /* OpenMP 2.5:
9423 # pragma omp atomic new-line
9424 expression-stmt
9426 expression-stmt:
9427 x binop= expr | x++ | ++x | x-- | --x
9428 binop:
9429 +, *, -, /, &, ^, |, <<, >>
9431 where x is an lvalue expression with scalar type.
9433 OpenMP 3.1:
9434 # pragma omp atomic new-line
9435 update-stmt
9437 # pragma omp atomic read new-line
9438 read-stmt
9440 # pragma omp atomic write new-line
9441 write-stmt
9443 # pragma omp atomic update new-line
9444 update-stmt
9446 # pragma omp atomic capture new-line
9447 capture-stmt
9449 # pragma omp atomic capture new-line
9450 capture-block
9452 read-stmt:
9453 v = x
9454 write-stmt:
9455 x = expr
9456 update-stmt:
9457 expression-stmt | x = x binop expr
9458 capture-stmt:
9459 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
9460 capture-block:
9461 { v = x; update-stmt; } | { update-stmt; v = x; }
9463 where x and v are lvalue expressions with scalar type.
9465 LOC is the location of the #pragma token. */
9467 static void
9468 c_parser_omp_atomic (location_t loc, c_parser *parser)
9470 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
9471 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
9472 tree stmt, orig_lhs;
9473 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
9474 struct c_expr rhs_expr;
9475 bool structured_block = false;
9477 if (c_parser_next_token_is (parser, CPP_NAME))
9479 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9481 if (!strcmp (p, "read"))
9482 code = OMP_ATOMIC_READ;
9483 else if (!strcmp (p, "write"))
9484 code = NOP_EXPR;
9485 else if (!strcmp (p, "update"))
9486 code = OMP_ATOMIC;
9487 else if (!strcmp (p, "capture"))
9488 code = OMP_ATOMIC_CAPTURE_NEW;
9489 else
9490 p = NULL;
9491 if (p)
9492 c_parser_consume_token (parser);
9494 c_parser_skip_to_pragma_eol (parser);
9496 switch (code)
9498 case OMP_ATOMIC_READ:
9499 case NOP_EXPR: /* atomic write */
9500 v = c_parser_unary_expression (parser).value;
9501 v = c_fully_fold (v, false, NULL);
9502 if (v == error_mark_node)
9503 goto saw_error;
9504 loc = c_parser_peek_token (parser)->location;
9505 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9506 goto saw_error;
9507 if (code == NOP_EXPR)
9508 lhs = c_parser_expression (parser).value;
9509 else
9510 lhs = c_parser_unary_expression (parser).value;
9511 lhs = c_fully_fold (lhs, false, NULL);
9512 if (lhs == error_mark_node)
9513 goto saw_error;
9514 if (code == NOP_EXPR)
9516 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
9517 opcode. */
9518 code = OMP_ATOMIC;
9519 rhs = lhs;
9520 lhs = v;
9521 v = NULL_TREE;
9523 goto done;
9524 case OMP_ATOMIC_CAPTURE_NEW:
9525 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9527 c_parser_consume_token (parser);
9528 structured_block = true;
9530 else
9532 v = c_parser_unary_expression (parser).value;
9533 v = c_fully_fold (v, false, NULL);
9534 if (v == error_mark_node)
9535 goto saw_error;
9536 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9537 goto saw_error;
9539 break;
9540 default:
9541 break;
9544 /* For structured_block case we don't know yet whether
9545 old or new x should be captured. */
9546 restart:
9547 lhs = c_parser_unary_expression (parser).value;
9548 lhs = c_fully_fold (lhs, false, NULL);
9549 orig_lhs = lhs;
9550 switch (TREE_CODE (lhs))
9552 case ERROR_MARK:
9553 saw_error:
9554 c_parser_skip_to_end_of_block_or_statement (parser);
9555 if (structured_block)
9557 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9558 c_parser_consume_token (parser);
9559 else if (code == OMP_ATOMIC_CAPTURE_NEW)
9561 c_parser_skip_to_end_of_block_or_statement (parser);
9562 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9563 c_parser_consume_token (parser);
9566 return;
9568 case POSTINCREMENT_EXPR:
9569 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9570 code = OMP_ATOMIC_CAPTURE_OLD;
9571 /* FALLTHROUGH */
9572 case PREINCREMENT_EXPR:
9573 lhs = TREE_OPERAND (lhs, 0);
9574 opcode = PLUS_EXPR;
9575 rhs = integer_one_node;
9576 break;
9578 case POSTDECREMENT_EXPR:
9579 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
9580 code = OMP_ATOMIC_CAPTURE_OLD;
9581 /* FALLTHROUGH */
9582 case PREDECREMENT_EXPR:
9583 lhs = TREE_OPERAND (lhs, 0);
9584 opcode = MINUS_EXPR;
9585 rhs = integer_one_node;
9586 break;
9588 case COMPOUND_EXPR:
9589 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
9590 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
9591 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
9592 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
9593 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
9594 (TREE_OPERAND (lhs, 1), 0), 0)))
9595 == BOOLEAN_TYPE)
9596 /* Undo effects of boolean_increment for post {in,de}crement. */
9597 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
9598 /* FALLTHRU */
9599 case MODIFY_EXPR:
9600 if (TREE_CODE (lhs) == MODIFY_EXPR
9601 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
9603 /* Undo effects of boolean_increment. */
9604 if (integer_onep (TREE_OPERAND (lhs, 1)))
9606 /* This is pre or post increment. */
9607 rhs = TREE_OPERAND (lhs, 1);
9608 lhs = TREE_OPERAND (lhs, 0);
9609 opcode = NOP_EXPR;
9610 if (code == OMP_ATOMIC_CAPTURE_NEW
9611 && !structured_block
9612 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9613 code = OMP_ATOMIC_CAPTURE_OLD;
9614 break;
9616 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
9617 && TREE_OPERAND (lhs, 0)
9618 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
9620 /* This is pre or post decrement. */
9621 rhs = TREE_OPERAND (lhs, 1);
9622 lhs = TREE_OPERAND (lhs, 0);
9623 opcode = NOP_EXPR;
9624 if (code == OMP_ATOMIC_CAPTURE_NEW
9625 && !structured_block
9626 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
9627 code = OMP_ATOMIC_CAPTURE_OLD;
9628 break;
9631 /* FALLTHRU */
9632 default:
9633 switch (c_parser_peek_token (parser)->type)
9635 case CPP_MULT_EQ:
9636 opcode = MULT_EXPR;
9637 break;
9638 case CPP_DIV_EQ:
9639 opcode = TRUNC_DIV_EXPR;
9640 break;
9641 case CPP_PLUS_EQ:
9642 opcode = PLUS_EXPR;
9643 break;
9644 case CPP_MINUS_EQ:
9645 opcode = MINUS_EXPR;
9646 break;
9647 case CPP_LSHIFT_EQ:
9648 opcode = LSHIFT_EXPR;
9649 break;
9650 case CPP_RSHIFT_EQ:
9651 opcode = RSHIFT_EXPR;
9652 break;
9653 case CPP_AND_EQ:
9654 opcode = BIT_AND_EXPR;
9655 break;
9656 case CPP_OR_EQ:
9657 opcode = BIT_IOR_EXPR;
9658 break;
9659 case CPP_XOR_EQ:
9660 opcode = BIT_XOR_EXPR;
9661 break;
9662 case CPP_EQ:
9663 if (structured_block || code == OMP_ATOMIC)
9665 location_t aloc = c_parser_peek_token (parser)->location;
9666 location_t rhs_loc;
9667 enum c_parser_prec oprec = PREC_NONE;
9669 c_parser_consume_token (parser);
9670 rhs1 = c_parser_unary_expression (parser).value;
9671 rhs1 = c_fully_fold (rhs1, false, NULL);
9672 if (rhs1 == error_mark_node)
9673 goto saw_error;
9674 switch (c_parser_peek_token (parser)->type)
9676 case CPP_SEMICOLON:
9677 if (code == OMP_ATOMIC_CAPTURE_NEW)
9679 code = OMP_ATOMIC_CAPTURE_OLD;
9680 v = lhs;
9681 lhs = NULL_TREE;
9682 lhs1 = rhs1;
9683 rhs1 = NULL_TREE;
9684 c_parser_consume_token (parser);
9685 goto restart;
9687 c_parser_error (parser,
9688 "invalid form of %<#pragma omp atomic%>");
9689 goto saw_error;
9690 case CPP_MULT:
9691 opcode = MULT_EXPR;
9692 oprec = PREC_MULT;
9693 break;
9694 case CPP_DIV:
9695 opcode = TRUNC_DIV_EXPR;
9696 oprec = PREC_MULT;
9697 break;
9698 case CPP_PLUS:
9699 opcode = PLUS_EXPR;
9700 oprec = PREC_ADD;
9701 break;
9702 case CPP_MINUS:
9703 opcode = MINUS_EXPR;
9704 oprec = PREC_ADD;
9705 break;
9706 case CPP_LSHIFT:
9707 opcode = LSHIFT_EXPR;
9708 oprec = PREC_SHIFT;
9709 break;
9710 case CPP_RSHIFT:
9711 opcode = RSHIFT_EXPR;
9712 oprec = PREC_SHIFT;
9713 break;
9714 case CPP_AND:
9715 opcode = BIT_AND_EXPR;
9716 oprec = PREC_BITAND;
9717 break;
9718 case CPP_OR:
9719 opcode = BIT_IOR_EXPR;
9720 oprec = PREC_BITOR;
9721 break;
9722 case CPP_XOR:
9723 opcode = BIT_XOR_EXPR;
9724 oprec = PREC_BITXOR;
9725 break;
9726 default:
9727 c_parser_error (parser,
9728 "invalid operator for %<#pragma omp atomic%>");
9729 goto saw_error;
9731 loc = aloc;
9732 c_parser_consume_token (parser);
9733 rhs_loc = c_parser_peek_token (parser)->location;
9734 if (commutative_tree_code (opcode))
9735 oprec = (enum c_parser_prec) (oprec - 1);
9736 rhs_expr = c_parser_binary_expression (parser, NULL, oprec);
9737 rhs_expr = default_function_array_read_conversion (rhs_loc,
9738 rhs_expr);
9739 rhs = rhs_expr.value;
9740 rhs = c_fully_fold (rhs, false, NULL);
9741 goto stmt_done;
9743 /* FALLTHROUGH */
9744 default:
9745 c_parser_error (parser,
9746 "invalid operator for %<#pragma omp atomic%>");
9747 goto saw_error;
9750 /* Arrange to pass the location of the assignment operator to
9751 c_finish_omp_atomic. */
9752 loc = c_parser_peek_token (parser)->location;
9753 c_parser_consume_token (parser);
9755 location_t rhs_loc = c_parser_peek_token (parser)->location;
9756 rhs_expr = c_parser_expression (parser);
9757 rhs_expr = default_function_array_read_conversion (rhs_loc, rhs_expr);
9759 rhs = rhs_expr.value;
9760 rhs = c_fully_fold (rhs, false, NULL);
9761 break;
9763 stmt_done:
9764 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
9766 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
9767 goto saw_error;
9768 v = c_parser_unary_expression (parser).value;
9769 v = c_fully_fold (v, false, NULL);
9770 if (v == error_mark_node)
9771 goto saw_error;
9772 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
9773 goto saw_error;
9774 lhs1 = c_parser_unary_expression (parser).value;
9775 lhs1 = c_fully_fold (lhs1, false, NULL);
9776 if (lhs1 == error_mark_node)
9777 goto saw_error;
9779 if (structured_block)
9781 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9782 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
9784 done:
9785 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1);
9786 if (stmt != error_mark_node)
9787 add_stmt (stmt);
9789 if (!structured_block)
9790 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9794 /* OpenMP 2.5:
9795 # pragma omp barrier new-line
9798 static void
9799 c_parser_omp_barrier (c_parser *parser)
9801 location_t loc = c_parser_peek_token (parser)->location;
9802 c_parser_consume_pragma (parser);
9803 c_parser_skip_to_pragma_eol (parser);
9805 c_finish_omp_barrier (loc);
9808 /* OpenMP 2.5:
9809 # pragma omp critical [(name)] new-line
9810 structured-block
9812 LOC is the location of the #pragma itself. */
9814 static tree
9815 c_parser_omp_critical (location_t loc, c_parser *parser)
9817 tree stmt, name = NULL;
9819 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9821 c_parser_consume_token (parser);
9822 if (c_parser_next_token_is (parser, CPP_NAME))
9824 name = c_parser_peek_token (parser)->value;
9825 c_parser_consume_token (parser);
9826 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9828 else
9829 c_parser_error (parser, "expected identifier");
9831 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9832 c_parser_error (parser, "expected %<(%> or end of line");
9833 c_parser_skip_to_pragma_eol (parser);
9835 stmt = c_parser_omp_structured_block (parser);
9836 return c_finish_omp_critical (loc, stmt, name);
9839 /* OpenMP 2.5:
9840 # pragma omp flush flush-vars[opt] new-line
9842 flush-vars:
9843 ( variable-list ) */
9845 static void
9846 c_parser_omp_flush (c_parser *parser)
9848 location_t loc = c_parser_peek_token (parser)->location;
9849 c_parser_consume_pragma (parser);
9850 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9851 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
9852 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
9853 c_parser_error (parser, "expected %<(%> or end of line");
9854 c_parser_skip_to_pragma_eol (parser);
9856 c_finish_omp_flush (loc);
9859 /* Parse the restricted form of the for statement allowed by OpenMP.
9860 The real trick here is to determine the loop control variable early
9861 so that we can push a new decl if necessary to make it private.
9862 LOC is the location of the OMP in "#pragma omp". */
9864 static tree
9865 c_parser_omp_for_loop (location_t loc,
9866 c_parser *parser, tree clauses, tree *par_clauses)
9868 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
9869 tree declv, condv, incrv, initv, ret = NULL;
9870 bool fail = false, open_brace_parsed = false;
9871 int i, collapse = 1, nbraces = 0;
9872 location_t for_loc;
9873 VEC(tree,gc) *for_block = make_tree_vector ();
9875 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
9876 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
9877 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
9879 gcc_assert (collapse >= 1);
9881 declv = make_tree_vec (collapse);
9882 initv = make_tree_vec (collapse);
9883 condv = make_tree_vec (collapse);
9884 incrv = make_tree_vec (collapse);
9886 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
9888 c_parser_error (parser, "for statement expected");
9889 return NULL;
9891 for_loc = c_parser_peek_token (parser)->location;
9892 c_parser_consume_token (parser);
9894 for (i = 0; i < collapse; i++)
9896 int bracecount = 0;
9898 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9899 goto pop_scopes;
9901 /* Parse the initialization declaration or expression. */
9902 if (c_parser_next_tokens_start_declaration (parser))
9904 if (i > 0)
9905 VEC_safe_push (tree, gc, for_block, c_begin_compound_stmt (true));
9906 c_parser_declaration_or_fndef (parser, true, true, true, true, true, NULL);
9907 decl = check_for_loop_decls (for_loc, flag_isoc99);
9908 if (decl == NULL)
9909 goto error_init;
9910 if (DECL_INITIAL (decl) == error_mark_node)
9911 decl = error_mark_node;
9912 init = decl;
9914 else if (c_parser_next_token_is (parser, CPP_NAME)
9915 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
9917 struct c_expr decl_exp;
9918 struct c_expr init_exp;
9919 location_t init_loc;
9921 decl_exp = c_parser_postfix_expression (parser);
9922 decl = decl_exp.value;
9924 c_parser_require (parser, CPP_EQ, "expected %<=%>");
9926 init_loc = c_parser_peek_token (parser)->location;
9927 init_exp = c_parser_expr_no_commas (parser, NULL);
9928 init_exp = default_function_array_read_conversion (init_loc,
9929 init_exp);
9930 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
9931 NOP_EXPR, init_loc, init_exp.value,
9932 init_exp.original_type);
9933 init = c_process_expr_stmt (init_loc, init);
9935 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9937 else
9939 error_init:
9940 c_parser_error (parser,
9941 "expected iteration declaration or initialization");
9942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9943 "expected %<)%>");
9944 fail = true;
9945 goto parse_next;
9948 /* Parse the loop condition. */
9949 cond = NULL_TREE;
9950 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
9952 location_t cond_loc = c_parser_peek_token (parser)->location;
9953 struct c_expr cond_expr = c_parser_binary_expression (parser, NULL,
9954 PREC_NONE);
9956 cond = cond_expr.value;
9957 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
9958 cond = c_fully_fold (cond, false, NULL);
9959 switch (cond_expr.original_code)
9961 case GT_EXPR:
9962 case GE_EXPR:
9963 case LT_EXPR:
9964 case LE_EXPR:
9965 break;
9966 default:
9967 /* Can't be cond = error_mark_node, because we want to preserve
9968 the location until c_finish_omp_for. */
9969 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
9970 break;
9972 protected_set_expr_location (cond, cond_loc);
9974 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9976 /* Parse the increment expression. */
9977 incr = NULL_TREE;
9978 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
9980 location_t incr_loc = c_parser_peek_token (parser)->location;
9982 incr = c_process_expr_stmt (incr_loc,
9983 c_parser_expression (parser).value);
9985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9987 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
9988 fail = true;
9989 else
9991 TREE_VEC_ELT (declv, i) = decl;
9992 TREE_VEC_ELT (initv, i) = init;
9993 TREE_VEC_ELT (condv, i) = cond;
9994 TREE_VEC_ELT (incrv, i) = incr;
9997 parse_next:
9998 if (i == collapse - 1)
9999 break;
10001 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
10002 in between the collapsed for loops to be still considered perfectly
10003 nested. Hopefully the final version clarifies this.
10004 For now handle (multiple) {'s and empty statements. */
10007 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10009 c_parser_consume_token (parser);
10010 break;
10012 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10014 c_parser_consume_token (parser);
10015 bracecount++;
10017 else if (bracecount
10018 && c_parser_next_token_is (parser, CPP_SEMICOLON))
10019 c_parser_consume_token (parser);
10020 else
10022 c_parser_error (parser, "not enough perfectly nested loops");
10023 if (bracecount)
10025 open_brace_parsed = true;
10026 bracecount--;
10028 fail = true;
10029 collapse = 0;
10030 break;
10033 while (1);
10035 nbraces += bracecount;
10038 save_break = c_break_label;
10039 c_break_label = size_one_node;
10040 save_cont = c_cont_label;
10041 c_cont_label = NULL_TREE;
10042 body = push_stmt_list ();
10044 if (open_brace_parsed)
10046 location_t here = c_parser_peek_token (parser)->location;
10047 stmt = c_begin_compound_stmt (true);
10048 c_parser_compound_statement_nostart (parser);
10049 add_stmt (c_end_compound_stmt (here, stmt, true));
10051 else
10052 add_stmt (c_parser_c99_block_statement (parser));
10053 if (c_cont_label)
10055 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
10056 SET_EXPR_LOCATION (t, loc);
10057 add_stmt (t);
10060 body = pop_stmt_list (body);
10061 c_break_label = save_break;
10062 c_cont_label = save_cont;
10064 while (nbraces)
10066 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10068 c_parser_consume_token (parser);
10069 nbraces--;
10071 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10072 c_parser_consume_token (parser);
10073 else
10075 c_parser_error (parser, "collapsed loops not perfectly nested");
10076 while (nbraces)
10078 location_t here = c_parser_peek_token (parser)->location;
10079 stmt = c_begin_compound_stmt (true);
10080 add_stmt (body);
10081 c_parser_compound_statement_nostart (parser);
10082 body = c_end_compound_stmt (here, stmt, true);
10083 nbraces--;
10085 goto pop_scopes;
10089 /* Only bother calling c_finish_omp_for if we haven't already generated
10090 an error from the initialization parsing. */
10091 if (!fail)
10093 stmt = c_finish_omp_for (loc, declv, initv, condv, incrv, body, NULL);
10094 if (stmt)
10096 if (par_clauses != NULL)
10098 tree *c;
10099 for (c = par_clauses; *c ; )
10100 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
10101 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
10102 c = &OMP_CLAUSE_CHAIN (*c);
10103 else
10105 for (i = 0; i < collapse; i++)
10106 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
10107 break;
10108 if (i == collapse)
10109 c = &OMP_CLAUSE_CHAIN (*c);
10110 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
10112 error_at (loc,
10113 "iteration variable %qD should not be firstprivate",
10114 OMP_CLAUSE_DECL (*c));
10115 *c = OMP_CLAUSE_CHAIN (*c);
10117 else
10119 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
10120 change it to shared (decl) in
10121 OMP_PARALLEL_CLAUSES. */
10122 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
10123 OMP_CLAUSE_LASTPRIVATE);
10124 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
10125 OMP_CLAUSE_CHAIN (l) = clauses;
10126 clauses = l;
10127 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
10131 OMP_FOR_CLAUSES (stmt) = clauses;
10133 ret = stmt;
10135 pop_scopes:
10136 while (!VEC_empty (tree, for_block))
10138 /* FIXME diagnostics: LOC below should be the actual location of
10139 this particular for block. We need to build a list of
10140 locations to go along with FOR_BLOCK. */
10141 stmt = c_end_compound_stmt (loc, VEC_pop (tree, for_block), true);
10142 add_stmt (stmt);
10144 release_tree_vector (for_block);
10145 return ret;
10148 /* OpenMP 2.5:
10149 #pragma omp for for-clause[optseq] new-line
10150 for-loop
10152 LOC is the location of the #pragma token.
10155 #define OMP_FOR_CLAUSE_MASK \
10156 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10157 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10158 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10159 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10160 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
10161 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
10162 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE) \
10163 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10165 static tree
10166 c_parser_omp_for (location_t loc, c_parser *parser)
10168 tree block, clauses, ret;
10170 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
10171 "#pragma omp for");
10173 block = c_begin_compound_stmt (true);
10174 ret = c_parser_omp_for_loop (loc, parser, clauses, NULL);
10175 block = c_end_compound_stmt (loc, block, true);
10176 add_stmt (block);
10178 return ret;
10181 /* OpenMP 2.5:
10182 # pragma omp master new-line
10183 structured-block
10185 LOC is the location of the #pragma token.
10188 static tree
10189 c_parser_omp_master (location_t loc, c_parser *parser)
10191 c_parser_skip_to_pragma_eol (parser);
10192 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
10195 /* OpenMP 2.5:
10196 # pragma omp ordered new-line
10197 structured-block
10199 LOC is the location of the #pragma itself.
10202 static tree
10203 c_parser_omp_ordered (location_t loc, c_parser *parser)
10205 c_parser_skip_to_pragma_eol (parser);
10206 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
10209 /* OpenMP 2.5:
10211 section-scope:
10212 { section-sequence }
10214 section-sequence:
10215 section-directive[opt] structured-block
10216 section-sequence section-directive structured-block
10218 SECTIONS_LOC is the location of the #pragma omp sections. */
10220 static tree
10221 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
10223 tree stmt, substmt;
10224 bool error_suppress = false;
10225 location_t loc;
10227 loc = c_parser_peek_token (parser)->location;
10228 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10230 /* Avoid skipping until the end of the block. */
10231 parser->error = false;
10232 return NULL_TREE;
10235 stmt = push_stmt_list ();
10237 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
10239 substmt = push_stmt_list ();
10241 while (1)
10243 c_parser_statement (parser);
10245 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10246 break;
10247 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10248 break;
10249 if (c_parser_next_token_is (parser, CPP_EOF))
10250 break;
10253 substmt = pop_stmt_list (substmt);
10254 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10255 SET_EXPR_LOCATION (substmt, loc);
10256 add_stmt (substmt);
10259 while (1)
10261 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
10262 break;
10263 if (c_parser_next_token_is (parser, CPP_EOF))
10264 break;
10266 loc = c_parser_peek_token (parser)->location;
10267 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
10269 c_parser_consume_pragma (parser);
10270 c_parser_skip_to_pragma_eol (parser);
10271 error_suppress = false;
10273 else if (!error_suppress)
10275 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
10276 error_suppress = true;
10279 substmt = c_parser_omp_structured_block (parser);
10280 substmt = build1 (OMP_SECTION, void_type_node, substmt);
10281 SET_EXPR_LOCATION (substmt, loc);
10282 add_stmt (substmt);
10284 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
10285 "expected %<#pragma omp section%> or %<}%>");
10287 substmt = pop_stmt_list (stmt);
10289 stmt = make_node (OMP_SECTIONS);
10290 SET_EXPR_LOCATION (stmt, sections_loc);
10291 TREE_TYPE (stmt) = void_type_node;
10292 OMP_SECTIONS_BODY (stmt) = substmt;
10294 return add_stmt (stmt);
10297 /* OpenMP 2.5:
10298 # pragma omp sections sections-clause[optseq] newline
10299 sections-scope
10301 LOC is the location of the #pragma token.
10304 #define OMP_SECTIONS_CLAUSE_MASK \
10305 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10306 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10307 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
10308 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10309 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10311 static tree
10312 c_parser_omp_sections (location_t loc, c_parser *parser)
10314 tree block, clauses, ret;
10316 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
10317 "#pragma omp sections");
10319 block = c_begin_compound_stmt (true);
10320 ret = c_parser_omp_sections_scope (loc, parser);
10321 if (ret)
10322 OMP_SECTIONS_CLAUSES (ret) = clauses;
10323 block = c_end_compound_stmt (loc, block, true);
10324 add_stmt (block);
10326 return ret;
10329 /* OpenMP 2.5:
10330 # pragma parallel parallel-clause new-line
10331 # pragma parallel for parallel-for-clause new-line
10332 # pragma parallel sections parallel-sections-clause new-line
10334 LOC is the location of the #pragma token.
10337 #define OMP_PARALLEL_CLAUSE_MASK \
10338 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10339 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10340 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10341 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10342 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10343 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
10344 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
10345 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
10347 static tree
10348 c_parser_omp_parallel (location_t loc, c_parser *parser)
10350 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
10351 const char *p_name = "#pragma omp parallel";
10352 tree stmt, clauses, par_clause, ws_clause, block;
10353 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
10355 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10357 c_parser_consume_token (parser);
10358 p_kind = PRAGMA_OMP_PARALLEL_FOR;
10359 p_name = "#pragma omp parallel for";
10360 mask |= OMP_FOR_CLAUSE_MASK;
10361 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10363 else if (c_parser_next_token_is (parser, CPP_NAME))
10365 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10366 if (strcmp (p, "sections") == 0)
10368 c_parser_consume_token (parser);
10369 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
10370 p_name = "#pragma omp parallel sections";
10371 mask |= OMP_SECTIONS_CLAUSE_MASK;
10372 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
10376 clauses = c_parser_omp_all_clauses (parser, mask, p_name);
10378 switch (p_kind)
10380 case PRAGMA_OMP_PARALLEL:
10381 block = c_begin_omp_parallel ();
10382 c_parser_statement (parser);
10383 stmt = c_finish_omp_parallel (loc, clauses, block);
10384 break;
10386 case PRAGMA_OMP_PARALLEL_FOR:
10387 block = c_begin_omp_parallel ();
10388 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10389 c_parser_omp_for_loop (loc, parser, ws_clause, &par_clause);
10390 stmt = c_finish_omp_parallel (loc, par_clause, block);
10391 OMP_PARALLEL_COMBINED (stmt) = 1;
10392 break;
10394 case PRAGMA_OMP_PARALLEL_SECTIONS:
10395 block = c_begin_omp_parallel ();
10396 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
10397 stmt = c_parser_omp_sections_scope (loc, parser);
10398 if (stmt)
10399 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
10400 stmt = c_finish_omp_parallel (loc, par_clause, block);
10401 OMP_PARALLEL_COMBINED (stmt) = 1;
10402 break;
10404 default:
10405 gcc_unreachable ();
10408 return stmt;
10411 /* OpenMP 2.5:
10412 # pragma omp single single-clause[optseq] new-line
10413 structured-block
10415 LOC is the location of the #pragma.
10418 #define OMP_SINGLE_CLAUSE_MASK \
10419 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10420 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10421 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
10422 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
10424 static tree
10425 c_parser_omp_single (location_t loc, c_parser *parser)
10427 tree stmt = make_node (OMP_SINGLE);
10428 SET_EXPR_LOCATION (stmt, loc);
10429 TREE_TYPE (stmt) = void_type_node;
10431 OMP_SINGLE_CLAUSES (stmt)
10432 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
10433 "#pragma omp single");
10434 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
10436 return add_stmt (stmt);
10439 /* OpenMP 3.0:
10440 # pragma omp task task-clause[optseq] new-line
10442 LOC is the location of the #pragma.
10445 #define OMP_TASK_CLAUSE_MASK \
10446 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
10447 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
10448 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
10449 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
10450 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
10451 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
10452 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
10453 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
10455 static tree
10456 c_parser_omp_task (location_t loc, c_parser *parser)
10458 tree clauses, block;
10460 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
10461 "#pragma omp task");
10463 block = c_begin_omp_task ();
10464 c_parser_statement (parser);
10465 return c_finish_omp_task (loc, clauses, block);
10468 /* OpenMP 3.0:
10469 # pragma omp taskwait new-line
10472 static void
10473 c_parser_omp_taskwait (c_parser *parser)
10475 location_t loc = c_parser_peek_token (parser)->location;
10476 c_parser_consume_pragma (parser);
10477 c_parser_skip_to_pragma_eol (parser);
10479 c_finish_omp_taskwait (loc);
10482 /* OpenMP 3.1:
10483 # pragma omp taskyield new-line
10486 static void
10487 c_parser_omp_taskyield (c_parser *parser)
10489 location_t loc = c_parser_peek_token (parser)->location;
10490 c_parser_consume_pragma (parser);
10491 c_parser_skip_to_pragma_eol (parser);
10493 c_finish_omp_taskyield (loc);
10496 /* Main entry point to parsing most OpenMP pragmas. */
10498 static void
10499 c_parser_omp_construct (c_parser *parser)
10501 enum pragma_kind p_kind;
10502 location_t loc;
10503 tree stmt;
10505 loc = c_parser_peek_token (parser)->location;
10506 p_kind = c_parser_peek_token (parser)->pragma_kind;
10507 c_parser_consume_pragma (parser);
10509 switch (p_kind)
10511 case PRAGMA_OMP_ATOMIC:
10512 c_parser_omp_atomic (loc, parser);
10513 return;
10514 case PRAGMA_OMP_CRITICAL:
10515 stmt = c_parser_omp_critical (loc, parser);
10516 break;
10517 case PRAGMA_OMP_FOR:
10518 stmt = c_parser_omp_for (loc, parser);
10519 break;
10520 case PRAGMA_OMP_MASTER:
10521 stmt = c_parser_omp_master (loc, parser);
10522 break;
10523 case PRAGMA_OMP_ORDERED:
10524 stmt = c_parser_omp_ordered (loc, parser);
10525 break;
10526 case PRAGMA_OMP_PARALLEL:
10527 stmt = c_parser_omp_parallel (loc, parser);
10528 break;
10529 case PRAGMA_OMP_SECTIONS:
10530 stmt = c_parser_omp_sections (loc, parser);
10531 break;
10532 case PRAGMA_OMP_SINGLE:
10533 stmt = c_parser_omp_single (loc, parser);
10534 break;
10535 case PRAGMA_OMP_TASK:
10536 stmt = c_parser_omp_task (loc, parser);
10537 break;
10538 default:
10539 gcc_unreachable ();
10542 if (stmt)
10543 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
10547 /* OpenMP 2.5:
10548 # pragma omp threadprivate (variable-list) */
10550 static void
10551 c_parser_omp_threadprivate (c_parser *parser)
10553 tree vars, t;
10554 location_t loc;
10556 c_parser_consume_pragma (parser);
10557 loc = c_parser_peek_token (parser)->location;
10558 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10560 /* Mark every variable in VARS to be assigned thread local storage. */
10561 for (t = vars; t; t = TREE_CHAIN (t))
10563 tree v = TREE_PURPOSE (t);
10565 /* FIXME diagnostics: Ideally we should keep individual
10566 locations for all the variables in the var list to make the
10567 following errors more precise. Perhaps
10568 c_parser_omp_var_list_parens() should construct a list of
10569 locations to go along with the var list. */
10571 /* If V had already been marked threadprivate, it doesn't matter
10572 whether it had been used prior to this point. */
10573 if (TREE_CODE (v) != VAR_DECL)
10574 error_at (loc, "%qD is not a variable", v);
10575 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
10576 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
10577 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
10578 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
10579 else if (TREE_TYPE (v) == error_mark_node)
10581 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
10582 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
10583 else
10585 if (! DECL_THREAD_LOCAL_P (v))
10587 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
10588 /* If rtl has been already set for this var, call
10589 make_decl_rtl once again, so that encode_section_info
10590 has a chance to look at the new decl flags. */
10591 if (DECL_RTL_SET_P (v))
10592 make_decl_rtl (v);
10594 C_DECL_THREADPRIVATE_P (v) = 1;
10598 c_parser_skip_to_pragma_eol (parser);
10601 /* Parse a transaction attribute (GCC Extension).
10603 transaction-attribute:
10604 attributes
10605 [ [ any-word ] ]
10607 The transactional memory language description is written for C++,
10608 and uses the C++0x attribute syntax. For compatibility, allow the
10609 bracket style for transactions in C as well. */
10611 static tree
10612 c_parser_transaction_attributes (c_parser *parser)
10614 tree attr_name, attr = NULL;
10616 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10617 return c_parser_attributes (parser);
10619 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10620 return NULL_TREE;
10621 c_parser_consume_token (parser);
10622 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
10623 goto error1;
10625 attr_name = c_parser_attribute_any_word (parser);
10626 if (attr_name)
10628 c_parser_consume_token (parser);
10629 attr = build_tree_list (attr_name, NULL_TREE);
10631 else
10632 c_parser_error (parser, "expected identifier");
10634 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
10635 error1:
10636 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
10637 return attr;
10640 /* Parse a __transaction_atomic or __transaction_relaxed statement
10641 (GCC Extension).
10643 transaction-statement:
10644 __transaction_atomic transaction-attribute[opt] compound-statement
10645 __transaction_relaxed compound-statement
10647 Note that the only valid attribute is: "outer".
10650 static tree
10651 c_parser_transaction (c_parser *parser, enum rid keyword)
10653 unsigned int old_in = parser->in_transaction;
10654 unsigned int this_in = 1, new_in;
10655 location_t loc = c_parser_peek_token (parser)->location;
10656 tree stmt, attrs;
10658 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
10659 || keyword == RID_TRANSACTION_RELAXED)
10660 && c_parser_next_token_is_keyword (parser, keyword));
10661 c_parser_consume_token (parser);
10663 if (keyword == RID_TRANSACTION_RELAXED)
10664 this_in |= TM_STMT_ATTR_RELAXED;
10665 else
10667 attrs = c_parser_transaction_attributes (parser);
10668 if (attrs)
10669 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
10672 /* Keep track if we're in the lexical scope of an outer transaction. */
10673 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
10675 parser->in_transaction = new_in;
10676 stmt = c_parser_compound_statement (parser);
10677 parser->in_transaction = old_in;
10679 if (flag_tm)
10680 stmt = c_finish_transaction (loc, stmt, this_in);
10681 else
10682 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
10683 "%<__transaction_atomic%> without transactional memory support enabled"
10684 : "%<__transaction_relaxed %> "
10685 "without transactional memory support enabled"));
10687 return stmt;
10690 /* Parse a __transaction_atomic or __transaction_relaxed expression
10691 (GCC Extension).
10693 transaction-expression:
10694 __transaction_atomic ( expression )
10695 __transaction_relaxed ( expression )
10698 static struct c_expr
10699 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
10701 struct c_expr ret;
10702 unsigned int old_in = parser->in_transaction;
10703 unsigned int this_in = 1;
10704 location_t loc = c_parser_peek_token (parser)->location;
10705 tree attrs;
10707 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
10708 || keyword == RID_TRANSACTION_RELAXED)
10709 && c_parser_next_token_is_keyword (parser, keyword));
10710 c_parser_consume_token (parser);
10712 if (keyword == RID_TRANSACTION_RELAXED)
10713 this_in |= TM_STMT_ATTR_RELAXED;
10714 else
10716 attrs = c_parser_transaction_attributes (parser);
10717 if (attrs)
10718 this_in |= parse_tm_stmt_attr (attrs, 0);
10721 parser->in_transaction = this_in;
10722 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10724 tree expr = c_parser_expression (parser).value;
10725 ret.original_type = TREE_TYPE (expr);
10726 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
10727 if (this_in & TM_STMT_ATTR_RELAXED)
10728 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
10729 SET_EXPR_LOCATION (ret.value, loc);
10730 ret.original_code = TRANSACTION_EXPR;
10731 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10733 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
10734 goto error;
10737 else
10739 error:
10740 ret.value = error_mark_node;
10741 ret.original_code = ERROR_MARK;
10742 ret.original_type = NULL;
10744 parser->in_transaction = old_in;
10746 if (!flag_tm)
10747 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
10748 "%<__transaction_atomic%> without transactional memory support enabled"
10749 : "%<__transaction_relaxed %> "
10750 "without transactional memory support enabled"));
10752 return ret;
10755 /* Parse a __transaction_cancel statement (GCC Extension).
10757 transaction-cancel-statement:
10758 __transaction_cancel transaction-attribute[opt] ;
10760 Note that the only valid attribute is "outer".
10763 static tree
10764 c_parser_transaction_cancel(c_parser *parser)
10766 location_t loc = c_parser_peek_token (parser)->location;
10767 tree attrs;
10768 bool is_outer = false;
10770 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
10771 c_parser_consume_token (parser);
10773 attrs = c_parser_transaction_attributes (parser);
10774 if (attrs)
10775 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
10777 if (!flag_tm)
10779 error_at (loc, "%<__transaction_cancel%> without "
10780 "transactional memory support enabled");
10781 goto ret_error;
10783 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
10785 error_at (loc, "%<__transaction_cancel%> within a "
10786 "%<__transaction_relaxed%>");
10787 goto ret_error;
10789 else if (is_outer)
10791 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
10792 && !is_tm_may_cancel_outer (current_function_decl))
10794 error_at (loc, "outer %<__transaction_cancel%> not "
10795 "within outer %<__transaction_atomic%>");
10796 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
10797 goto ret_error;
10800 else if (parser->in_transaction == 0)
10802 error_at (loc, "%<__transaction_cancel%> not within "
10803 "%<__transaction_atomic%>");
10804 goto ret_error;
10807 return add_stmt (build_tm_abort_call (loc, is_outer));
10809 ret_error:
10810 return build1 (NOP_EXPR, void_type_node, error_mark_node);
10813 /* Parse a single source file. */
10815 void
10816 c_parse_file (void)
10818 /* Use local storage to begin. If the first token is a pragma, parse it.
10819 If it is #pragma GCC pch_preprocess, then this will load a PCH file
10820 which will cause garbage collection. */
10821 c_parser tparser;
10823 memset (&tparser, 0, sizeof tparser);
10824 the_parser = &tparser;
10826 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
10827 c_parser_pragma_pch_preprocess (&tparser);
10829 the_parser = ggc_alloc_c_parser ();
10830 *the_parser = tparser;
10832 /* Initialize EH, if we've been told to do so. */
10833 if (flag_exceptions)
10834 using_eh_for_cleanups ();
10836 c_parser_translation_unit (the_parser);
10837 the_parser = NULL;
10840 #include "gt-c-parser.h"