gcc/
[official-gcc.git] / gcc-4_9-branch / gcc / c / c-parser.c
blobed5a40d0042383b8bc028a6ad73e81e15981a6d0
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h" /* For rtl.h: needs enum reg_class. */
42 #include "tree.h"
43 #include "stringpool.h"
44 #include "attribs.h"
45 #include "stor-layout.h"
46 #include "varasm.h"
47 #include "trans-mem.h"
48 #include "langhooks.h"
49 #include "input.h"
50 #include "cpplib.h"
51 #include "timevar.h"
52 #include "c-family/c-pragma.h"
53 #include "c-tree.h"
54 #include "c-lang.h"
55 #include "flags.h"
56 #include "ggc.h"
57 #include "c-family/c-common.h"
58 #include "c-family/c-objc.h"
59 #include "vec.h"
60 #include "target.h"
61 #include "cgraph.h"
62 #include "plugin.h"
63 #include "omp-low.h"
66 /* Initialization routine for this file. */
68 void
69 c_parse_init (void)
71 /* The only initialization required is of the reserved word
72 identifiers. */
73 unsigned int i;
74 tree id;
75 int mask = 0;
77 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
78 the c_token structure. */
79 gcc_assert (RID_MAX <= 255);
81 mask |= D_CXXONLY;
82 if (!flag_isoc99)
83 mask |= D_C99;
84 if (flag_no_asm)
86 mask |= D_ASM | D_EXT;
87 if (!flag_isoc99)
88 mask |= D_EXT89;
90 if (!c_dialect_objc ())
91 mask |= D_OBJC | D_CXX_OBJC;
93 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
94 for (i = 0; i < num_c_common_reswords; i++)
96 /* If a keyword is disabled, do not enter it into the table
97 and so create a canonical spelling that isn't a keyword. */
98 if (c_common_reswords[i].disable & mask)
100 if (warn_cxx_compat
101 && (c_common_reswords[i].disable & D_CXXWARN))
103 id = get_identifier (c_common_reswords[i].word);
104 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
105 C_IS_RESERVED_WORD (id) = 1;
107 continue;
110 id = get_identifier (c_common_reswords[i].word);
111 C_SET_RID_CODE (id, c_common_reswords[i].rid);
112 C_IS_RESERVED_WORD (id) = 1;
113 ridpointers [(int) c_common_reswords[i].rid] = id;
117 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
118 and the C parser. Unlike the C++ lexer, the parser structure
119 stores the lexer information instead of using a separate structure.
120 Identifiers are separated into ordinary identifiers, type names,
121 keywords and some other Objective-C types of identifiers, and some
122 look-ahead is maintained.
124 ??? It might be a good idea to lex the whole file up front (as for
125 C++). It would then be possible to share more of the C and C++
126 lexer code, if desired. */
128 /* The following local token type is used. */
130 /* A keyword. */
131 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
133 /* More information about the type of a CPP_NAME token. */
134 typedef enum c_id_kind {
135 /* An ordinary identifier. */
136 C_ID_ID,
137 /* An identifier declared as a typedef name. */
138 C_ID_TYPENAME,
139 /* An identifier declared as an Objective-C class name. */
140 C_ID_CLASSNAME,
141 /* An address space identifier. */
142 C_ID_ADDRSPACE,
143 /* Not an identifier. */
144 C_ID_NONE
145 } c_id_kind;
147 /* A single C token after string literal concatenation and conversion
148 of preprocessing tokens to tokens. */
149 typedef struct GTY (()) c_token {
150 /* The kind of token. */
151 ENUM_BITFIELD (cpp_ttype) type : 8;
152 /* If this token is a CPP_NAME, this value indicates whether also
153 declared as some kind of type. Otherwise, it is C_ID_NONE. */
154 ENUM_BITFIELD (c_id_kind) id_kind : 8;
155 /* If this token is a keyword, this value indicates which keyword.
156 Otherwise, this value is RID_MAX. */
157 ENUM_BITFIELD (rid) keyword : 8;
158 /* If this token is a CPP_PRAGMA, this indicates the pragma that
159 was seen. Otherwise it is PRAGMA_NONE. */
160 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
161 /* The location at which this token was found. */
162 location_t location;
163 /* The value associated with this token, if any. */
164 tree value;
165 } c_token;
167 /* A parser structure recording information about the state and
168 context of parsing. Includes lexer information with up to two
169 tokens of look-ahead; more are not needed for C. */
170 typedef struct GTY(()) c_parser {
171 /* The look-ahead tokens. */
172 c_token * GTY((skip)) tokens;
173 /* Buffer for look-ahead tokens. */
174 c_token tokens_buf[2];
175 /* How many look-ahead tokens are available (0, 1 or 2, or
176 more if parsing from pre-lexed tokens). */
177 unsigned int tokens_avail;
178 /* True if a syntax error is being recovered from; false otherwise.
179 c_parser_error sets this flag. It should clear this flag when
180 enough tokens have been consumed to recover from the error. */
181 BOOL_BITFIELD error : 1;
182 /* True if we're processing a pragma, and shouldn't automatically
183 consume CPP_PRAGMA_EOL. */
184 BOOL_BITFIELD in_pragma : 1;
185 /* True if we're parsing the outermost block of an if statement. */
186 BOOL_BITFIELD in_if_block : 1;
187 /* True if we want to lex an untranslated string. */
188 BOOL_BITFIELD lex_untranslated_string : 1;
190 /* Objective-C specific parser/lexer information. */
192 /* True if we are in a context where the Objective-C "PQ" keywords
193 are considered keywords. */
194 BOOL_BITFIELD objc_pq_context : 1;
195 /* True if we are parsing a (potential) Objective-C foreach
196 statement. This is set to true after we parsed 'for (' and while
197 we wait for 'in' or ';' to decide if it's a standard C for loop or an
198 Objective-C foreach loop. */
199 BOOL_BITFIELD objc_could_be_foreach_context : 1;
200 /* The following flag is needed to contextualize Objective-C lexical
201 analysis. In some cases (e.g., 'int NSObject;'), it is
202 undesirable to bind an identifier to an Objective-C class, even
203 if a class with that name exists. */
204 BOOL_BITFIELD objc_need_raw_identifier : 1;
205 /* Nonzero if we're processing a __transaction statement. The value
206 is 1 | TM_STMT_ATTR_*. */
207 unsigned int in_transaction : 4;
208 /* True if we are in a context where the Objective-C "Property attribute"
209 keywords are valid. */
210 BOOL_BITFIELD objc_property_attr_context : 1;
212 /* Cilk Plus specific parser/lexer information. */
214 /* Buffer to hold all the tokens from parsing the vector attribute for the
215 SIMD-enabled functions (formerly known as elemental functions). */
216 vec <c_token, va_gc> *cilk_simd_fn_tokens;
217 } c_parser;
220 /* The actual parser and external interface. ??? Does this need to be
221 garbage-collected? */
223 static GTY (()) c_parser *the_parser;
225 /* Read in and lex a single token, storing it in *TOKEN. */
227 static void
228 c_lex_one_token (c_parser *parser, c_token *token)
230 timevar_push (TV_LEX);
232 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
233 (parser->lex_untranslated_string
234 ? C_LEX_STRING_NO_TRANSLATE : 0));
235 token->id_kind = C_ID_NONE;
236 token->keyword = RID_MAX;
237 token->pragma_kind = PRAGMA_NONE;
239 switch (token->type)
241 case CPP_NAME:
243 tree decl;
245 bool objc_force_identifier = parser->objc_need_raw_identifier;
246 if (c_dialect_objc ())
247 parser->objc_need_raw_identifier = false;
249 if (C_IS_RESERVED_WORD (token->value))
251 enum rid rid_code = C_RID_CODE (token->value);
253 if (rid_code == RID_CXX_COMPAT_WARN)
255 warning_at (token->location,
256 OPT_Wc___compat,
257 "identifier %qE conflicts with C++ keyword",
258 token->value);
260 else if (rid_code >= RID_FIRST_ADDR_SPACE
261 && rid_code <= RID_LAST_ADDR_SPACE)
263 token->id_kind = C_ID_ADDRSPACE;
264 token->keyword = rid_code;
265 break;
267 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
269 /* We found an Objective-C "pq" keyword (in, out,
270 inout, bycopy, byref, oneway). They need special
271 care because the interpretation depends on the
272 context. */
273 if (parser->objc_pq_context)
275 token->type = CPP_KEYWORD;
276 token->keyword = rid_code;
277 break;
279 else if (parser->objc_could_be_foreach_context
280 && rid_code == RID_IN)
282 /* We are in Objective-C, inside a (potential)
283 foreach context (which means after having
284 parsed 'for (', but before having parsed ';'),
285 and we found 'in'. We consider it the keyword
286 which terminates the declaration at the
287 beginning of a foreach-statement. Note that
288 this means you can't use 'in' for anything else
289 in that context; in particular, in Objective-C
290 you can't use 'in' as the name of the running
291 variable in a C for loop. We could potentially
292 try to add code here to disambiguate, but it
293 seems a reasonable limitation. */
294 token->type = CPP_KEYWORD;
295 token->keyword = rid_code;
296 break;
298 /* Else, "pq" keywords outside of the "pq" context are
299 not keywords, and we fall through to the code for
300 normal tokens. */
302 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
304 /* We found an Objective-C "property attribute"
305 keyword (getter, setter, readonly, etc). These are
306 only valid in the property context. */
307 if (parser->objc_property_attr_context)
309 token->type = CPP_KEYWORD;
310 token->keyword = rid_code;
311 break;
313 /* Else they are not special keywords.
316 else if (c_dialect_objc ()
317 && (OBJC_IS_AT_KEYWORD (rid_code)
318 || OBJC_IS_CXX_KEYWORD (rid_code)))
320 /* We found one of the Objective-C "@" keywords (defs,
321 selector, synchronized, etc) or one of the
322 Objective-C "cxx" keywords (class, private,
323 protected, public, try, catch, throw) without a
324 preceding '@' sign. Do nothing and fall through to
325 the code for normal tokens (in C++ we would still
326 consider the CXX ones keywords, but not in C). */
329 else
331 token->type = CPP_KEYWORD;
332 token->keyword = rid_code;
333 break;
337 decl = lookup_name (token->value);
338 if (decl)
340 if (TREE_CODE (decl) == TYPE_DECL)
342 token->id_kind = C_ID_TYPENAME;
343 break;
346 else if (c_dialect_objc ())
348 tree objc_interface_decl = objc_is_class_name (token->value);
349 /* Objective-C class names are in the same namespace as
350 variables and typedefs, and hence are shadowed by local
351 declarations. */
352 if (objc_interface_decl
353 && (!objc_force_identifier || global_bindings_p ()))
355 token->value = objc_interface_decl;
356 token->id_kind = C_ID_CLASSNAME;
357 break;
360 token->id_kind = C_ID_ID;
362 break;
363 case CPP_AT_NAME:
364 /* This only happens in Objective-C; it must be a keyword. */
365 token->type = CPP_KEYWORD;
366 switch (C_RID_CODE (token->value))
368 /* Replace 'class' with '@class', 'private' with '@private',
369 etc. This prevents confusion with the C++ keyword
370 'class', and makes the tokens consistent with other
371 Objective-C 'AT' keywords. For example '@class' is
372 reported as RID_AT_CLASS which is consistent with
373 '@synchronized', which is reported as
374 RID_AT_SYNCHRONIZED.
376 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
377 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
378 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
379 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
380 case RID_THROW: token->keyword = RID_AT_THROW; break;
381 case RID_TRY: token->keyword = RID_AT_TRY; break;
382 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
383 default: token->keyword = C_RID_CODE (token->value);
385 break;
386 case CPP_COLON:
387 case CPP_COMMA:
388 case CPP_CLOSE_PAREN:
389 case CPP_SEMICOLON:
390 /* These tokens may affect the interpretation of any identifiers
391 following, if doing Objective-C. */
392 if (c_dialect_objc ())
393 parser->objc_need_raw_identifier = false;
394 break;
395 case CPP_PRAGMA:
396 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
397 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
398 token->value = NULL;
399 break;
400 default:
401 break;
403 timevar_pop (TV_LEX);
406 /* Return a pointer to the next token from PARSER, reading it in if
407 necessary. */
409 static inline c_token *
410 c_parser_peek_token (c_parser *parser)
412 if (parser->tokens_avail == 0)
414 c_lex_one_token (parser, &parser->tokens[0]);
415 parser->tokens_avail = 1;
417 return &parser->tokens[0];
420 /* Return true if the next token from PARSER has the indicated
421 TYPE. */
423 static inline bool
424 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
426 return c_parser_peek_token (parser)->type == type;
429 /* Return true if the next token from PARSER does not have the
430 indicated TYPE. */
432 static inline bool
433 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
435 return !c_parser_next_token_is (parser, type);
438 /* Return true if the next token from PARSER is the indicated
439 KEYWORD. */
441 static inline bool
442 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
444 return c_parser_peek_token (parser)->keyword == keyword;
447 /* Return a pointer to the next-but-one token from PARSER, reading it
448 in if necessary. The next token is already read in. */
450 static c_token *
451 c_parser_peek_2nd_token (c_parser *parser)
453 if (parser->tokens_avail >= 2)
454 return &parser->tokens[1];
455 gcc_assert (parser->tokens_avail == 1);
456 gcc_assert (parser->tokens[0].type != CPP_EOF);
457 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
458 c_lex_one_token (parser, &parser->tokens[1]);
459 parser->tokens_avail = 2;
460 return &parser->tokens[1];
463 /* Return true if TOKEN can start a type name,
464 false otherwise. */
465 static bool
466 c_token_starts_typename (c_token *token)
468 switch (token->type)
470 case CPP_NAME:
471 switch (token->id_kind)
473 case C_ID_ID:
474 return false;
475 case C_ID_ADDRSPACE:
476 return true;
477 case C_ID_TYPENAME:
478 return true;
479 case C_ID_CLASSNAME:
480 gcc_assert (c_dialect_objc ());
481 return true;
482 default:
483 gcc_unreachable ();
485 case CPP_KEYWORD:
486 switch (token->keyword)
488 case RID_UNSIGNED:
489 case RID_LONG:
490 case RID_INT128:
491 case RID_SHORT:
492 case RID_SIGNED:
493 case RID_COMPLEX:
494 case RID_INT:
495 case RID_CHAR:
496 case RID_FLOAT:
497 case RID_DOUBLE:
498 case RID_VOID:
499 case RID_DFLOAT32:
500 case RID_DFLOAT64:
501 case RID_DFLOAT128:
502 case RID_BOOL:
503 case RID_ENUM:
504 case RID_STRUCT:
505 case RID_UNION:
506 case RID_TYPEOF:
507 case RID_CONST:
508 case RID_ATOMIC:
509 case RID_VOLATILE:
510 case RID_RESTRICT:
511 case RID_ATTRIBUTE:
512 case RID_FRACT:
513 case RID_ACCUM:
514 case RID_SAT:
515 case RID_AUTO_TYPE:
516 return true;
517 default:
518 return false;
520 case CPP_LESS:
521 if (c_dialect_objc ())
522 return true;
523 return false;
524 default:
525 return false;
529 enum c_lookahead_kind {
530 /* Always treat unknown identifiers as typenames. */
531 cla_prefer_type,
533 /* Could be parsing a nonabstract declarator. Only treat an identifier
534 as a typename if followed by another identifier or a star. */
535 cla_nonabstract_decl,
537 /* Never treat identifiers as typenames. */
538 cla_prefer_id
541 /* Return true if the next token from PARSER can start a type name,
542 false otherwise. LA specifies how to do lookahead in order to
543 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
545 static inline bool
546 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
548 c_token *token = c_parser_peek_token (parser);
549 if (c_token_starts_typename (token))
550 return true;
552 /* Try a bit harder to detect an unknown typename. */
553 if (la != cla_prefer_id
554 && token->type == CPP_NAME
555 && token->id_kind == C_ID_ID
557 /* Do not try too hard when we could have "object in array". */
558 && !parser->objc_could_be_foreach_context
560 && (la == cla_prefer_type
561 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
562 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
564 /* Only unknown identifiers. */
565 && !lookup_name (token->value))
566 return true;
568 return false;
571 /* Return true if TOKEN is a type qualifier, false otherwise. */
572 static bool
573 c_token_is_qualifier (c_token *token)
575 switch (token->type)
577 case CPP_NAME:
578 switch (token->id_kind)
580 case C_ID_ADDRSPACE:
581 return true;
582 default:
583 return false;
585 case CPP_KEYWORD:
586 switch (token->keyword)
588 case RID_CONST:
589 case RID_VOLATILE:
590 case RID_RESTRICT:
591 case RID_ATTRIBUTE:
592 case RID_ATOMIC:
593 return true;
594 default:
595 return false;
597 case CPP_LESS:
598 return false;
599 default:
600 gcc_unreachable ();
604 /* Return true if the next token from PARSER is a type qualifier,
605 false otherwise. */
606 static inline bool
607 c_parser_next_token_is_qualifier (c_parser *parser)
609 c_token *token = c_parser_peek_token (parser);
610 return c_token_is_qualifier (token);
613 /* Return true if TOKEN can start declaration specifiers, false
614 otherwise. */
615 static bool
616 c_token_starts_declspecs (c_token *token)
618 switch (token->type)
620 case CPP_NAME:
621 switch (token->id_kind)
623 case C_ID_ID:
624 return false;
625 case C_ID_ADDRSPACE:
626 return true;
627 case C_ID_TYPENAME:
628 return true;
629 case C_ID_CLASSNAME:
630 gcc_assert (c_dialect_objc ());
631 return true;
632 default:
633 gcc_unreachable ();
635 case CPP_KEYWORD:
636 switch (token->keyword)
638 case RID_STATIC:
639 case RID_EXTERN:
640 case RID_REGISTER:
641 case RID_TYPEDEF:
642 case RID_INLINE:
643 case RID_NORETURN:
644 case RID_AUTO:
645 case RID_THREAD:
646 case RID_UNSIGNED:
647 case RID_LONG:
648 case RID_INT128:
649 case RID_SHORT:
650 case RID_SIGNED:
651 case RID_COMPLEX:
652 case RID_INT:
653 case RID_CHAR:
654 case RID_FLOAT:
655 case RID_DOUBLE:
656 case RID_VOID:
657 case RID_DFLOAT32:
658 case RID_DFLOAT64:
659 case RID_DFLOAT128:
660 case RID_BOOL:
661 case RID_ENUM:
662 case RID_STRUCT:
663 case RID_UNION:
664 case RID_TYPEOF:
665 case RID_CONST:
666 case RID_VOLATILE:
667 case RID_RESTRICT:
668 case RID_ATTRIBUTE:
669 case RID_FRACT:
670 case RID_ACCUM:
671 case RID_SAT:
672 case RID_ALIGNAS:
673 case RID_ATOMIC:
674 case RID_AUTO_TYPE:
675 return true;
676 default:
677 return false;
679 case CPP_LESS:
680 if (c_dialect_objc ())
681 return true;
682 return false;
683 default:
684 return false;
689 /* Return true if TOKEN can start declaration specifiers or a static
690 assertion, false otherwise. */
691 static bool
692 c_token_starts_declaration (c_token *token)
694 if (c_token_starts_declspecs (token)
695 || token->keyword == RID_STATIC_ASSERT)
696 return true;
697 else
698 return false;
701 /* Return true if the next token from PARSER can start declaration
702 specifiers, false otherwise. */
703 static inline bool
704 c_parser_next_token_starts_declspecs (c_parser *parser)
706 c_token *token = c_parser_peek_token (parser);
708 /* In Objective-C, a classname normally starts a declspecs unless it
709 is immediately followed by a dot. In that case, it is the
710 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
711 setter/getter on the class. c_token_starts_declspecs() can't
712 differentiate between the two cases because it only checks the
713 current token, so we have a special check here. */
714 if (c_dialect_objc ()
715 && token->type == CPP_NAME
716 && token->id_kind == C_ID_CLASSNAME
717 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
718 return false;
720 return c_token_starts_declspecs (token);
723 /* Return true if the next tokens from PARSER can start declaration
724 specifiers or a static assertion, false otherwise. */
725 static inline bool
726 c_parser_next_tokens_start_declaration (c_parser *parser)
728 c_token *token = c_parser_peek_token (parser);
730 /* Same as above. */
731 if (c_dialect_objc ()
732 && token->type == CPP_NAME
733 && token->id_kind == C_ID_CLASSNAME
734 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
735 return false;
737 /* Labels do not start declarations. */
738 if (token->type == CPP_NAME
739 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
740 return false;
742 if (c_token_starts_declaration (token))
743 return true;
745 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
746 return true;
748 return false;
751 /* Consume the next token from PARSER. */
753 static void
754 c_parser_consume_token (c_parser *parser)
756 gcc_assert (parser->tokens_avail >= 1);
757 gcc_assert (parser->tokens[0].type != CPP_EOF);
758 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
759 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
760 if (parser->tokens != &parser->tokens_buf[0])
761 parser->tokens++;
762 else if (parser->tokens_avail == 2)
763 parser->tokens[0] = parser->tokens[1];
764 parser->tokens_avail--;
767 /* Expect the current token to be a #pragma. Consume it and remember
768 that we've begun parsing a pragma. */
770 static void
771 c_parser_consume_pragma (c_parser *parser)
773 gcc_assert (!parser->in_pragma);
774 gcc_assert (parser->tokens_avail >= 1);
775 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
776 if (parser->tokens != &parser->tokens_buf[0])
777 parser->tokens++;
778 else if (parser->tokens_avail == 2)
779 parser->tokens[0] = parser->tokens[1];
780 parser->tokens_avail--;
781 parser->in_pragma = true;
784 /* Update the global input_location from TOKEN. */
785 static inline void
786 c_parser_set_source_position_from_token (c_token *token)
788 if (token->type != CPP_EOF)
790 input_location = token->location;
794 /* Issue a diagnostic of the form
795 FILE:LINE: MESSAGE before TOKEN
796 where TOKEN is the next token in the input stream of PARSER.
797 MESSAGE (specified by the caller) is usually of the form "expected
798 OTHER-TOKEN".
800 Do not issue a diagnostic if still recovering from an error.
802 ??? This is taken from the C++ parser, but building up messages in
803 this way is not i18n-friendly and some other approach should be
804 used. */
806 static void
807 c_parser_error (c_parser *parser, const char *gmsgid)
809 c_token *token = c_parser_peek_token (parser);
810 if (parser->error)
811 return;
812 parser->error = true;
813 if (!gmsgid)
814 return;
815 /* This diagnostic makes more sense if it is tagged to the line of
816 the token we just peeked at. */
817 c_parser_set_source_position_from_token (token);
818 c_parse_error (gmsgid,
819 /* Because c_parse_error does not understand
820 CPP_KEYWORD, keywords are treated like
821 identifiers. */
822 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
823 /* ??? The C parser does not save the cpp flags of a
824 token, we need to pass 0 here and we will not get
825 the source spelling of some tokens but rather the
826 canonical spelling. */
827 token->value, /*flags=*/0);
830 /* If the next token is of the indicated TYPE, consume it. Otherwise,
831 issue the error MSGID. If MSGID is NULL then a message has already
832 been produced and no message will be produced this time. Returns
833 true if found, false otherwise. */
835 static bool
836 c_parser_require (c_parser *parser,
837 enum cpp_ttype type,
838 const char *msgid)
840 if (c_parser_next_token_is (parser, type))
842 c_parser_consume_token (parser);
843 return true;
845 else
847 c_parser_error (parser, msgid);
848 return false;
852 /* If the next token is the indicated keyword, consume it. Otherwise,
853 issue the error MSGID. Returns true if found, false otherwise. */
855 static bool
856 c_parser_require_keyword (c_parser *parser,
857 enum rid keyword,
858 const char *msgid)
860 if (c_parser_next_token_is_keyword (parser, keyword))
862 c_parser_consume_token (parser);
863 return true;
865 else
867 c_parser_error (parser, msgid);
868 return false;
872 /* Like c_parser_require, except that tokens will be skipped until the
873 desired token is found. An error message is still produced if the
874 next token is not as expected. If MSGID is NULL then a message has
875 already been produced and no message will be produced this
876 time. */
878 static void
879 c_parser_skip_until_found (c_parser *parser,
880 enum cpp_ttype type,
881 const char *msgid)
883 unsigned nesting_depth = 0;
885 if (c_parser_require (parser, type, msgid))
886 return;
888 /* Skip tokens until the desired token is found. */
889 while (true)
891 /* Peek at the next token. */
892 c_token *token = c_parser_peek_token (parser);
893 /* If we've reached the token we want, consume it and stop. */
894 if (token->type == type && !nesting_depth)
896 c_parser_consume_token (parser);
897 break;
900 /* If we've run out of tokens, stop. */
901 if (token->type == CPP_EOF)
902 return;
903 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
904 return;
905 if (token->type == CPP_OPEN_BRACE
906 || token->type == CPP_OPEN_PAREN
907 || token->type == CPP_OPEN_SQUARE)
908 ++nesting_depth;
909 else if (token->type == CPP_CLOSE_BRACE
910 || token->type == CPP_CLOSE_PAREN
911 || token->type == CPP_CLOSE_SQUARE)
913 if (nesting_depth-- == 0)
914 break;
916 /* Consume this token. */
917 c_parser_consume_token (parser);
919 parser->error = false;
922 /* Skip tokens until the end of a parameter is found, but do not
923 consume the comma, semicolon or closing delimiter. */
925 static void
926 c_parser_skip_to_end_of_parameter (c_parser *parser)
928 unsigned nesting_depth = 0;
930 while (true)
932 c_token *token = c_parser_peek_token (parser);
933 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
934 && !nesting_depth)
935 break;
936 /* If we've run out of tokens, stop. */
937 if (token->type == CPP_EOF)
938 return;
939 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
940 return;
941 if (token->type == CPP_OPEN_BRACE
942 || token->type == CPP_OPEN_PAREN
943 || token->type == CPP_OPEN_SQUARE)
944 ++nesting_depth;
945 else if (token->type == CPP_CLOSE_BRACE
946 || token->type == CPP_CLOSE_PAREN
947 || token->type == CPP_CLOSE_SQUARE)
949 if (nesting_depth-- == 0)
950 break;
952 /* Consume this token. */
953 c_parser_consume_token (parser);
955 parser->error = false;
958 /* Expect to be at the end of the pragma directive and consume an
959 end of line marker. */
961 static void
962 c_parser_skip_to_pragma_eol (c_parser *parser)
964 gcc_assert (parser->in_pragma);
965 parser->in_pragma = false;
967 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
968 while (true)
970 c_token *token = c_parser_peek_token (parser);
971 if (token->type == CPP_EOF)
972 break;
973 if (token->type == CPP_PRAGMA_EOL)
975 c_parser_consume_token (parser);
976 break;
978 c_parser_consume_token (parser);
981 parser->error = false;
984 /* Skip tokens until we have consumed an entire block, or until we
985 have consumed a non-nested ';'. */
987 static void
988 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
990 unsigned nesting_depth = 0;
991 bool save_error = parser->error;
993 while (true)
995 c_token *token;
997 /* Peek at the next token. */
998 token = c_parser_peek_token (parser);
1000 switch (token->type)
1002 case CPP_EOF:
1003 return;
1005 case CPP_PRAGMA_EOL:
1006 if (parser->in_pragma)
1007 return;
1008 break;
1010 case CPP_SEMICOLON:
1011 /* If the next token is a ';', we have reached the
1012 end of the statement. */
1013 if (!nesting_depth)
1015 /* Consume the ';'. */
1016 c_parser_consume_token (parser);
1017 goto finished;
1019 break;
1021 case CPP_CLOSE_BRACE:
1022 /* If the next token is a non-nested '}', then we have
1023 reached the end of the current block. */
1024 if (nesting_depth == 0 || --nesting_depth == 0)
1026 c_parser_consume_token (parser);
1027 goto finished;
1029 break;
1031 case CPP_OPEN_BRACE:
1032 /* If it the next token is a '{', then we are entering a new
1033 block. Consume the entire block. */
1034 ++nesting_depth;
1035 break;
1037 case CPP_PRAGMA:
1038 /* If we see a pragma, consume the whole thing at once. We
1039 have some safeguards against consuming pragmas willy-nilly.
1040 Normally, we'd expect to be here with parser->error set,
1041 which disables these safeguards. But it's possible to get
1042 here for secondary error recovery, after parser->error has
1043 been cleared. */
1044 c_parser_consume_pragma (parser);
1045 c_parser_skip_to_pragma_eol (parser);
1046 parser->error = save_error;
1047 continue;
1049 default:
1050 break;
1053 c_parser_consume_token (parser);
1056 finished:
1057 parser->error = false;
1060 /* CPP's options (initialized by c-opts.c). */
1061 extern cpp_options *cpp_opts;
1063 /* Save the warning flags which are controlled by __extension__. */
1065 static inline int
1066 disable_extension_diagnostics (void)
1068 int ret = (pedantic
1069 | (warn_pointer_arith << 1)
1070 | (warn_traditional << 2)
1071 | (flag_iso << 3)
1072 | (warn_long_long << 4)
1073 | (warn_cxx_compat << 5)
1074 | (warn_overlength_strings << 6));
1075 cpp_opts->cpp_pedantic = pedantic = 0;
1076 warn_pointer_arith = 0;
1077 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1078 flag_iso = 0;
1079 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1080 warn_cxx_compat = 0;
1081 warn_overlength_strings = 0;
1082 return ret;
1085 /* Restore the warning flags which are controlled by __extension__.
1086 FLAGS is the return value from disable_extension_diagnostics. */
1088 static inline void
1089 restore_extension_diagnostics (int flags)
1091 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1092 warn_pointer_arith = (flags >> 1) & 1;
1093 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1094 flag_iso = (flags >> 3) & 1;
1095 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1096 warn_cxx_compat = (flags >> 5) & 1;
1097 warn_overlength_strings = (flags >> 6) & 1;
1100 /* Possibly kinds of declarator to parse. */
1101 typedef enum c_dtr_syn {
1102 /* A normal declarator with an identifier. */
1103 C_DTR_NORMAL,
1104 /* An abstract declarator (maybe empty). */
1105 C_DTR_ABSTRACT,
1106 /* A parameter declarator: may be either, but after a type name does
1107 not redeclare a typedef name as an identifier if it can
1108 alternatively be interpreted as a typedef name; see DR#009,
1109 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1110 following DR#249. For example, given a typedef T, "int T" and
1111 "int *T" are valid parameter declarations redeclaring T, while
1112 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1113 abstract declarators rather than involving redundant parentheses;
1114 the same applies with attributes inside the parentheses before
1115 "T". */
1116 C_DTR_PARM
1117 } c_dtr_syn;
1119 /* The binary operation precedence levels, where 0 is a dummy lowest level
1120 used for the bottom of the stack. */
1121 enum c_parser_prec {
1122 PREC_NONE,
1123 PREC_LOGOR,
1124 PREC_LOGAND,
1125 PREC_BITOR,
1126 PREC_BITXOR,
1127 PREC_BITAND,
1128 PREC_EQ,
1129 PREC_REL,
1130 PREC_SHIFT,
1131 PREC_ADD,
1132 PREC_MULT,
1133 NUM_PRECS
1136 static void c_parser_external_declaration (c_parser *);
1137 static void c_parser_asm_definition (c_parser *);
1138 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1139 bool, bool, tree *, vec<c_token>);
1140 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1141 static void c_parser_static_assert_declaration (c_parser *);
1142 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1143 bool, bool, bool, enum c_lookahead_kind);
1144 static struct c_typespec c_parser_enum_specifier (c_parser *);
1145 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1146 static tree c_parser_struct_declaration (c_parser *);
1147 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1148 static tree c_parser_alignas_specifier (c_parser *);
1149 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1150 bool *);
1151 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1152 c_dtr_syn, bool *);
1153 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1154 bool,
1155 struct c_declarator *);
1156 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1157 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1158 tree);
1159 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1160 static tree c_parser_simple_asm_expr (c_parser *);
1161 static tree c_parser_attributes (c_parser *);
1162 static struct c_type_name *c_parser_type_name (c_parser *);
1163 static struct c_expr c_parser_initializer (c_parser *);
1164 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1165 static void c_parser_initelt (c_parser *, struct obstack *);
1166 static void c_parser_initval (c_parser *, struct c_expr *,
1167 struct obstack *);
1168 static tree c_parser_compound_statement (c_parser *);
1169 static void c_parser_compound_statement_nostart (c_parser *);
1170 static void c_parser_label (c_parser *);
1171 static void c_parser_statement (c_parser *);
1172 static void c_parser_statement_after_labels (c_parser *);
1173 static void c_parser_if_statement (c_parser *);
1174 static void c_parser_switch_statement (c_parser *);
1175 static void c_parser_while_statement (c_parser *, bool);
1176 static void c_parser_do_statement (c_parser *, bool);
1177 static void c_parser_for_statement (c_parser *, bool);
1178 static tree c_parser_asm_statement (c_parser *);
1179 static tree c_parser_asm_operands (c_parser *);
1180 static tree c_parser_asm_goto_operands (c_parser *);
1181 static tree c_parser_asm_clobbers (c_parser *);
1182 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1183 tree = NULL_TREE);
1184 static struct c_expr c_parser_conditional_expression (c_parser *,
1185 struct c_expr *, tree);
1186 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1187 tree);
1188 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1189 static struct c_expr c_parser_unary_expression (c_parser *);
1190 static struct c_expr c_parser_sizeof_expression (c_parser *);
1191 static struct c_expr c_parser_alignof_expression (c_parser *);
1192 static struct c_expr c_parser_postfix_expression (c_parser *);
1193 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1194 struct c_type_name *,
1195 location_t);
1196 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1197 location_t loc,
1198 struct c_expr);
1199 static tree c_parser_transaction (c_parser *, enum rid);
1200 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1201 static tree c_parser_transaction_cancel (c_parser *);
1202 static struct c_expr c_parser_expression (c_parser *);
1203 static struct c_expr c_parser_expression_conv (c_parser *);
1204 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1205 vec<tree, va_gc> **, location_t *,
1206 tree *, vec<location_t> *);
1207 static void c_parser_omp_construct (c_parser *);
1208 static void c_parser_omp_threadprivate (c_parser *);
1209 static void c_parser_omp_barrier (c_parser *);
1210 static void c_parser_omp_flush (c_parser *);
1211 static void c_parser_omp_taskwait (c_parser *);
1212 static void c_parser_omp_taskyield (c_parser *);
1213 static void c_parser_omp_cancel (c_parser *);
1214 static void c_parser_omp_cancellation_point (c_parser *);
1216 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1217 pragma_stmt, pragma_compound };
1218 static bool c_parser_pragma (c_parser *, enum pragma_context);
1219 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1220 static void c_parser_omp_end_declare_target (c_parser *);
1221 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1223 /* These Objective-C parser functions are only ever called when
1224 compiling Objective-C. */
1225 static void c_parser_objc_class_definition (c_parser *, tree);
1226 static void c_parser_objc_class_instance_variables (c_parser *);
1227 static void c_parser_objc_class_declaration (c_parser *);
1228 static void c_parser_objc_alias_declaration (c_parser *);
1229 static void c_parser_objc_protocol_definition (c_parser *, tree);
1230 static bool c_parser_objc_method_type (c_parser *);
1231 static void c_parser_objc_method_definition (c_parser *);
1232 static void c_parser_objc_methodprotolist (c_parser *);
1233 static void c_parser_objc_methodproto (c_parser *);
1234 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1235 static tree c_parser_objc_type_name (c_parser *);
1236 static tree c_parser_objc_protocol_refs (c_parser *);
1237 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1238 static void c_parser_objc_synchronized_statement (c_parser *);
1239 static tree c_parser_objc_selector (c_parser *);
1240 static tree c_parser_objc_selector_arg (c_parser *);
1241 static tree c_parser_objc_receiver (c_parser *);
1242 static tree c_parser_objc_message_args (c_parser *);
1243 static tree c_parser_objc_keywordexpr (c_parser *);
1244 static void c_parser_objc_at_property_declaration (c_parser *);
1245 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1246 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1247 static bool c_parser_objc_diagnose_bad_element_prefix
1248 (c_parser *, struct c_declspecs *);
1250 /* Cilk Plus supporting routines. */
1251 static void c_parser_cilk_simd (c_parser *);
1252 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1253 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1254 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1256 /* Parse a translation unit (C90 6.7, C99 6.9).
1258 translation-unit:
1259 external-declarations
1261 external-declarations:
1262 external-declaration
1263 external-declarations external-declaration
1265 GNU extensions:
1267 translation-unit:
1268 empty
1271 static void
1272 c_parser_translation_unit (c_parser *parser)
1274 if (c_parser_next_token_is (parser, CPP_EOF))
1276 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1277 "ISO C forbids an empty translation unit");
1279 else
1281 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1282 mark_valid_location_for_stdc_pragma (false);
1285 ggc_collect ();
1286 c_parser_external_declaration (parser);
1287 obstack_free (&parser_obstack, obstack_position);
1289 while (c_parser_next_token_is_not (parser, CPP_EOF));
1293 /* Parse an external declaration (C90 6.7, C99 6.9).
1295 external-declaration:
1296 function-definition
1297 declaration
1299 GNU extensions:
1301 external-declaration:
1302 asm-definition
1304 __extension__ external-declaration
1306 Objective-C:
1308 external-declaration:
1309 objc-class-definition
1310 objc-class-declaration
1311 objc-alias-declaration
1312 objc-protocol-definition
1313 objc-method-definition
1314 @end
1317 static void
1318 c_parser_external_declaration (c_parser *parser)
1320 int ext;
1321 switch (c_parser_peek_token (parser)->type)
1323 case CPP_KEYWORD:
1324 switch (c_parser_peek_token (parser)->keyword)
1326 case RID_EXTENSION:
1327 ext = disable_extension_diagnostics ();
1328 c_parser_consume_token (parser);
1329 c_parser_external_declaration (parser);
1330 restore_extension_diagnostics (ext);
1331 break;
1332 case RID_ASM:
1333 c_parser_asm_definition (parser);
1334 break;
1335 case RID_AT_INTERFACE:
1336 case RID_AT_IMPLEMENTATION:
1337 gcc_assert (c_dialect_objc ());
1338 c_parser_objc_class_definition (parser, NULL_TREE);
1339 break;
1340 case RID_AT_CLASS:
1341 gcc_assert (c_dialect_objc ());
1342 c_parser_objc_class_declaration (parser);
1343 break;
1344 case RID_AT_ALIAS:
1345 gcc_assert (c_dialect_objc ());
1346 c_parser_objc_alias_declaration (parser);
1347 break;
1348 case RID_AT_PROTOCOL:
1349 gcc_assert (c_dialect_objc ());
1350 c_parser_objc_protocol_definition (parser, NULL_TREE);
1351 break;
1352 case RID_AT_PROPERTY:
1353 gcc_assert (c_dialect_objc ());
1354 c_parser_objc_at_property_declaration (parser);
1355 break;
1356 case RID_AT_SYNTHESIZE:
1357 gcc_assert (c_dialect_objc ());
1358 c_parser_objc_at_synthesize_declaration (parser);
1359 break;
1360 case RID_AT_DYNAMIC:
1361 gcc_assert (c_dialect_objc ());
1362 c_parser_objc_at_dynamic_declaration (parser);
1363 break;
1364 case RID_AT_END:
1365 gcc_assert (c_dialect_objc ());
1366 c_parser_consume_token (parser);
1367 objc_finish_implementation ();
1368 break;
1369 default:
1370 goto decl_or_fndef;
1372 break;
1373 case CPP_SEMICOLON:
1374 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1375 "ISO C does not allow extra %<;%> outside of a function");
1376 c_parser_consume_token (parser);
1377 break;
1378 case CPP_PRAGMA:
1379 mark_valid_location_for_stdc_pragma (true);
1380 c_parser_pragma (parser, pragma_external);
1381 mark_valid_location_for_stdc_pragma (false);
1382 break;
1383 case CPP_PLUS:
1384 case CPP_MINUS:
1385 if (c_dialect_objc ())
1387 c_parser_objc_method_definition (parser);
1388 break;
1390 /* Else fall through, and yield a syntax error trying to parse
1391 as a declaration or function definition. */
1392 default:
1393 decl_or_fndef:
1394 /* A declaration or a function definition (or, in Objective-C,
1395 an @interface or @protocol with prefix attributes). We can
1396 only tell which after parsing the declaration specifiers, if
1397 any, and the first declarator. */
1398 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1399 NULL, vNULL);
1400 break;
1404 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1406 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1407 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1408 accepted; otherwise (old-style parameter declarations) only other
1409 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1410 assertion is accepted; otherwise (old-style parameter declarations)
1411 it is not. If NESTED is true, we are inside a function or parsing
1412 old-style parameter declarations; any functions encountered are
1413 nested functions and declaration specifiers are required; otherwise
1414 we are at top level and functions are normal functions and
1415 declaration specifiers may be optional. If EMPTY_OK is true, empty
1416 declarations are OK (subject to all other constraints); otherwise
1417 (old-style parameter declarations) they are diagnosed. If
1418 START_ATTR_OK is true, the declaration specifiers may start with
1419 attributes; otherwise they may not.
1420 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1421 declaration when parsing an Objective-C foreach statement.
1423 declaration:
1424 declaration-specifiers init-declarator-list[opt] ;
1425 static_assert-declaration
1427 function-definition:
1428 declaration-specifiers[opt] declarator declaration-list[opt]
1429 compound-statement
1431 declaration-list:
1432 declaration
1433 declaration-list declaration
1435 init-declarator-list:
1436 init-declarator
1437 init-declarator-list , init-declarator
1439 init-declarator:
1440 declarator simple-asm-expr[opt] attributes[opt]
1441 declarator simple-asm-expr[opt] attributes[opt] = initializer
1443 GNU extensions:
1445 nested-function-definition:
1446 declaration-specifiers declarator declaration-list[opt]
1447 compound-statement
1449 Objective-C:
1450 attributes objc-class-definition
1451 attributes objc-category-definition
1452 attributes objc-protocol-definition
1454 The simple-asm-expr and attributes are GNU extensions.
1456 This function does not handle __extension__; that is handled in its
1457 callers. ??? Following the old parser, __extension__ may start
1458 external declarations, declarations in functions and declarations
1459 at the start of "for" loops, but not old-style parameter
1460 declarations.
1462 C99 requires declaration specifiers in a function definition; the
1463 absence is diagnosed through the diagnosis of implicit int. In GNU
1464 C we also allow but diagnose declarations without declaration
1465 specifiers, but only at top level (elsewhere they conflict with
1466 other syntax).
1468 In Objective-C, declarations of the looping variable in a foreach
1469 statement are exceptionally terminated by 'in' (for example, 'for
1470 (NSObject *object in array) { ... }').
1472 OpenMP:
1474 declaration:
1475 threadprivate-directive */
1477 static void
1478 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1479 bool static_assert_ok, bool empty_ok,
1480 bool nested, bool start_attr_ok,
1481 tree *objc_foreach_object_declaration,
1482 vec<c_token> omp_declare_simd_clauses)
1484 struct c_declspecs *specs;
1485 tree prefix_attrs;
1486 tree all_prefix_attrs;
1487 bool diagnosed_no_specs = false;
1488 location_t here = c_parser_peek_token (parser)->location;
1490 if (static_assert_ok
1491 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1493 c_parser_static_assert_declaration (parser);
1494 return;
1496 specs = build_null_declspecs ();
1498 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1499 if (c_parser_peek_token (parser)->type == CPP_NAME
1500 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1501 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1502 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1503 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1505 error_at (here, "unknown type name %qE",
1506 c_parser_peek_token (parser)->value);
1508 /* Parse declspecs normally to get a correct pointer type, but avoid
1509 a further "fails to be a type name" error. Refuse nested functions
1510 since it is not how the user likely wants us to recover. */
1511 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1512 c_parser_peek_token (parser)->keyword = RID_VOID;
1513 c_parser_peek_token (parser)->value = error_mark_node;
1514 fndef_ok = !nested;
1517 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1518 true, true, cla_nonabstract_decl);
1519 if (parser->error)
1521 c_parser_skip_to_end_of_block_or_statement (parser);
1522 return;
1524 if (nested && !specs->declspecs_seen_p)
1526 c_parser_error (parser, "expected declaration specifiers");
1527 c_parser_skip_to_end_of_block_or_statement (parser);
1528 return;
1530 finish_declspecs (specs);
1531 bool auto_type_p = specs->typespec_word == cts_auto_type;
1532 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1534 if (auto_type_p)
1535 error_at (here, "%<__auto_type%> in empty declaration");
1536 else if (empty_ok)
1537 shadow_tag (specs);
1538 else
1540 shadow_tag_warned (specs, 1);
1541 pedwarn (here, 0, "empty declaration");
1543 c_parser_consume_token (parser);
1544 return;
1547 /* Provide better error recovery. Note that a type name here is usually
1548 better diagnosed as a redeclaration. */
1549 if (empty_ok
1550 && specs->typespec_kind == ctsk_tagdef
1551 && c_parser_next_token_starts_declspecs (parser)
1552 && !c_parser_next_token_is (parser, CPP_NAME))
1554 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1555 parser->error = false;
1556 shadow_tag_warned (specs, 1);
1557 return;
1559 else if (c_dialect_objc () && !auto_type_p)
1561 /* Prefix attributes are an error on method decls. */
1562 switch (c_parser_peek_token (parser)->type)
1564 case CPP_PLUS:
1565 case CPP_MINUS:
1566 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1567 return;
1568 if (specs->attrs)
1570 warning_at (c_parser_peek_token (parser)->location,
1571 OPT_Wattributes,
1572 "prefix attributes are ignored for methods");
1573 specs->attrs = NULL_TREE;
1575 if (fndef_ok)
1576 c_parser_objc_method_definition (parser);
1577 else
1578 c_parser_objc_methodproto (parser);
1579 return;
1580 break;
1581 default:
1582 break;
1584 /* This is where we parse 'attributes @interface ...',
1585 'attributes @implementation ...', 'attributes @protocol ...'
1586 (where attributes could be, for example, __attribute__
1587 ((deprecated)).
1589 switch (c_parser_peek_token (parser)->keyword)
1591 case RID_AT_INTERFACE:
1593 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1594 return;
1595 c_parser_objc_class_definition (parser, specs->attrs);
1596 return;
1598 break;
1599 case RID_AT_IMPLEMENTATION:
1601 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1602 return;
1603 if (specs->attrs)
1605 warning_at (c_parser_peek_token (parser)->location,
1606 OPT_Wattributes,
1607 "prefix attributes are ignored for implementations");
1608 specs->attrs = NULL_TREE;
1610 c_parser_objc_class_definition (parser, NULL_TREE);
1611 return;
1613 break;
1614 case RID_AT_PROTOCOL:
1616 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1617 return;
1618 c_parser_objc_protocol_definition (parser, specs->attrs);
1619 return;
1621 break;
1622 case RID_AT_ALIAS:
1623 case RID_AT_CLASS:
1624 case RID_AT_END:
1625 case RID_AT_PROPERTY:
1626 if (specs->attrs)
1628 c_parser_error (parser, "unexpected attribute");
1629 specs->attrs = NULL;
1631 break;
1632 default:
1633 break;
1637 pending_xref_error ();
1638 prefix_attrs = specs->attrs;
1639 all_prefix_attrs = prefix_attrs;
1640 specs->attrs = NULL_TREE;
1641 while (true)
1643 struct c_declarator *declarator;
1644 bool dummy = false;
1645 timevar_id_t tv;
1646 tree fnbody;
1647 /* Declaring either one or more declarators (in which case we
1648 should diagnose if there were no declaration specifiers) or a
1649 function definition (in which case the diagnostic for
1650 implicit int suffices). */
1651 declarator = c_parser_declarator (parser,
1652 specs->typespec_kind != ctsk_none,
1653 C_DTR_NORMAL, &dummy);
1654 if (declarator == NULL)
1656 if (omp_declare_simd_clauses.exists ()
1657 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1658 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1659 omp_declare_simd_clauses);
1660 c_parser_skip_to_end_of_block_or_statement (parser);
1661 return;
1663 if (auto_type_p && declarator->kind != cdk_id)
1665 error_at (here,
1666 "%<__auto_type%> requires a plain identifier"
1667 " as declarator");
1668 c_parser_skip_to_end_of_block_or_statement (parser);
1669 return;
1671 if (c_parser_next_token_is (parser, CPP_EQ)
1672 || c_parser_next_token_is (parser, CPP_COMMA)
1673 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1674 || c_parser_next_token_is_keyword (parser, RID_ASM)
1675 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1676 || c_parser_next_token_is_keyword (parser, RID_IN))
1678 tree asm_name = NULL_TREE;
1679 tree postfix_attrs = NULL_TREE;
1680 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1682 diagnosed_no_specs = true;
1683 pedwarn (here, 0, "data definition has no type or storage class");
1685 /* Having seen a data definition, there cannot now be a
1686 function definition. */
1687 fndef_ok = false;
1688 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1689 asm_name = c_parser_simple_asm_expr (parser);
1690 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1691 postfix_attrs = c_parser_attributes (parser);
1692 if (c_parser_next_token_is (parser, CPP_EQ))
1694 tree d;
1695 struct c_expr init;
1696 location_t init_loc;
1697 c_parser_consume_token (parser);
1698 if (auto_type_p)
1700 start_init (NULL_TREE, asm_name, global_bindings_p ());
1701 init_loc = c_parser_peek_token (parser)->location;
1702 init = c_parser_expr_no_commas (parser, NULL);
1703 if (TREE_CODE (init.value) == COMPONENT_REF
1704 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1705 error_at (here,
1706 "%<__auto_type%> used with a bit-field"
1707 " initializer");
1708 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1709 tree init_type = TREE_TYPE (init.value);
1710 /* As with typeof, remove all qualifiers from atomic types. */
1711 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1712 init_type
1713 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1714 bool vm_type = variably_modified_type_p (init_type,
1715 NULL_TREE);
1716 if (vm_type)
1717 init.value = c_save_expr (init.value);
1718 finish_init ();
1719 specs->typespec_kind = ctsk_typeof;
1720 specs->locations[cdw_typedef] = init_loc;
1721 specs->typedef_p = true;
1722 specs->type = init_type;
1723 if (vm_type)
1725 bool maybe_const = true;
1726 tree type_expr = c_fully_fold (init.value, false,
1727 &maybe_const);
1728 specs->expr_const_operands &= maybe_const;
1729 if (specs->expr)
1730 specs->expr = build2 (COMPOUND_EXPR,
1731 TREE_TYPE (type_expr),
1732 specs->expr, type_expr);
1733 else
1734 specs->expr = type_expr;
1736 d = start_decl (declarator, specs, true,
1737 chainon (postfix_attrs, all_prefix_attrs));
1738 if (!d)
1739 d = error_mark_node;
1740 if (omp_declare_simd_clauses.exists ()
1741 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1742 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1743 omp_declare_simd_clauses);
1745 else
1747 /* The declaration of the variable is in effect while
1748 its initializer is parsed. */
1749 d = start_decl (declarator, specs, true,
1750 chainon (postfix_attrs, all_prefix_attrs));
1751 if (!d)
1752 d = error_mark_node;
1753 if (omp_declare_simd_clauses.exists ()
1754 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1755 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1756 omp_declare_simd_clauses);
1757 start_init (d, asm_name, global_bindings_p ());
1758 init_loc = c_parser_peek_token (parser)->location;
1759 init = c_parser_initializer (parser);
1760 finish_init ();
1762 if (d != error_mark_node)
1764 maybe_warn_string_init (TREE_TYPE (d), init);
1765 finish_decl (d, init_loc, init.value,
1766 init.original_type, asm_name);
1769 else
1771 if (auto_type_p)
1773 error_at (here,
1774 "%<__auto_type%> requires an initialized "
1775 "data declaration");
1776 c_parser_skip_to_end_of_block_or_statement (parser);
1777 return;
1779 tree d = start_decl (declarator, specs, false,
1780 chainon (postfix_attrs,
1781 all_prefix_attrs));
1782 if (omp_declare_simd_clauses.exists ()
1783 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1785 tree parms = NULL_TREE;
1786 if (d && TREE_CODE (d) == FUNCTION_DECL)
1788 struct c_declarator *ce = declarator;
1789 while (ce != NULL)
1790 if (ce->kind == cdk_function)
1792 parms = ce->u.arg_info->parms;
1793 break;
1795 else
1796 ce = ce->declarator;
1798 if (parms)
1799 temp_store_parm_decls (d, parms);
1800 c_finish_omp_declare_simd (parser, d, parms,
1801 omp_declare_simd_clauses);
1802 if (parms)
1803 temp_pop_parm_decls ();
1805 if (d)
1806 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1807 NULL_TREE, asm_name);
1809 if (c_parser_next_token_is_keyword (parser, RID_IN))
1811 if (d)
1812 *objc_foreach_object_declaration = d;
1813 else
1814 *objc_foreach_object_declaration = error_mark_node;
1817 if (c_parser_next_token_is (parser, CPP_COMMA))
1819 if (auto_type_p)
1821 error_at (here,
1822 "%<__auto_type%> may only be used with"
1823 " a single declarator");
1824 c_parser_skip_to_end_of_block_or_statement (parser);
1825 return;
1827 c_parser_consume_token (parser);
1828 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1829 all_prefix_attrs = chainon (c_parser_attributes (parser),
1830 prefix_attrs);
1831 else
1832 all_prefix_attrs = prefix_attrs;
1833 continue;
1835 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1837 c_parser_consume_token (parser);
1838 return;
1840 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1842 /* This can only happen in Objective-C: we found the
1843 'in' that terminates the declaration inside an
1844 Objective-C foreach statement. Do not consume the
1845 token, so that the caller can use it to determine
1846 that this indeed is a foreach context. */
1847 return;
1849 else
1851 c_parser_error (parser, "expected %<,%> or %<;%>");
1852 c_parser_skip_to_end_of_block_or_statement (parser);
1853 return;
1856 else if (auto_type_p)
1858 error_at (here,
1859 "%<__auto_type%> requires an initialized data declaration");
1860 c_parser_skip_to_end_of_block_or_statement (parser);
1861 return;
1863 else if (!fndef_ok)
1865 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1866 "%<asm%> or %<__attribute__%>");
1867 c_parser_skip_to_end_of_block_or_statement (parser);
1868 return;
1870 /* Function definition (nested or otherwise). */
1871 if (nested)
1873 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1874 c_push_function_context ();
1876 if (!start_function (specs, declarator, all_prefix_attrs))
1878 /* This can appear in many cases looking nothing like a
1879 function definition, so we don't give a more specific
1880 error suggesting there was one. */
1881 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1882 "or %<__attribute__%>");
1883 if (nested)
1884 c_pop_function_context ();
1885 break;
1888 if (DECL_DECLARED_INLINE_P (current_function_decl))
1889 tv = TV_PARSE_INLINE;
1890 else
1891 tv = TV_PARSE_FUNC;
1892 timevar_push (tv);
1894 /* Parse old-style parameter declarations. ??? Attributes are
1895 not allowed to start declaration specifiers here because of a
1896 syntax conflict between a function declaration with attribute
1897 suffix and a function definition with an attribute prefix on
1898 first old-style parameter declaration. Following the old
1899 parser, they are not accepted on subsequent old-style
1900 parameter declarations either. However, there is no
1901 ambiguity after the first declaration, nor indeed on the
1902 first as long as we don't allow postfix attributes after a
1903 declarator with a nonempty identifier list in a definition;
1904 and postfix attributes have never been accepted here in
1905 function definitions either. */
1906 while (c_parser_next_token_is_not (parser, CPP_EOF)
1907 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1908 c_parser_declaration_or_fndef (parser, false, false, false,
1909 true, false, NULL, vNULL);
1910 store_parm_decls ();
1911 if (omp_declare_simd_clauses.exists ()
1912 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1913 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1914 omp_declare_simd_clauses);
1915 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1916 = c_parser_peek_token (parser)->location;
1917 fnbody = c_parser_compound_statement (parser);
1918 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1919 fnbody = expand_array_notation_exprs (fnbody);
1920 if (nested)
1922 tree decl = current_function_decl;
1923 /* Mark nested functions as needing static-chain initially.
1924 lower_nested_functions will recompute it but the
1925 DECL_STATIC_CHAIN flag is also used before that happens,
1926 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1927 DECL_STATIC_CHAIN (decl) = 1;
1928 add_stmt (fnbody);
1929 finish_function ();
1930 c_pop_function_context ();
1931 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1933 else
1935 add_stmt (fnbody);
1936 finish_function ();
1939 timevar_pop (tv);
1940 break;
1944 /* Parse an asm-definition (asm() outside a function body). This is a
1945 GNU extension.
1947 asm-definition:
1948 simple-asm-expr ;
1951 static void
1952 c_parser_asm_definition (c_parser *parser)
1954 tree asm_str = c_parser_simple_asm_expr (parser);
1955 if (asm_str)
1956 add_asm_node (asm_str);
1957 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
1960 /* Parse a static assertion (C11 6.7.10).
1962 static_assert-declaration:
1963 static_assert-declaration-no-semi ;
1966 static void
1967 c_parser_static_assert_declaration (c_parser *parser)
1969 c_parser_static_assert_declaration_no_semi (parser);
1970 if (parser->error
1971 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
1972 c_parser_skip_to_end_of_block_or_statement (parser);
1975 /* Parse a static assertion (C11 6.7.10), without the trailing
1976 semicolon.
1978 static_assert-declaration-no-semi:
1979 _Static_assert ( constant-expression , string-literal )
1982 static void
1983 c_parser_static_assert_declaration_no_semi (c_parser *parser)
1985 location_t assert_loc, value_loc;
1986 tree value;
1987 tree string;
1989 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
1990 assert_loc = c_parser_peek_token (parser)->location;
1991 if (!flag_isoc11)
1993 if (flag_isoc99)
1994 pedwarn (assert_loc, OPT_Wpedantic,
1995 "ISO C99 does not support %<_Static_assert%>");
1996 else
1997 pedwarn (assert_loc, OPT_Wpedantic,
1998 "ISO C90 does not support %<_Static_assert%>");
2000 c_parser_consume_token (parser);
2001 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2002 return;
2003 value_loc = c_parser_peek_token (parser)->location;
2004 value = c_parser_expr_no_commas (parser, NULL).value;
2005 parser->lex_untranslated_string = true;
2006 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2008 parser->lex_untranslated_string = false;
2009 return;
2011 switch (c_parser_peek_token (parser)->type)
2013 case CPP_STRING:
2014 case CPP_STRING16:
2015 case CPP_STRING32:
2016 case CPP_WSTRING:
2017 case CPP_UTF8STRING:
2018 string = c_parser_peek_token (parser)->value;
2019 c_parser_consume_token (parser);
2020 parser->lex_untranslated_string = false;
2021 break;
2022 default:
2023 c_parser_error (parser, "expected string literal");
2024 parser->lex_untranslated_string = false;
2025 return;
2027 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2029 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2031 error_at (value_loc, "expression in static assertion is not an integer");
2032 return;
2034 if (TREE_CODE (value) != INTEGER_CST)
2036 value = c_fully_fold (value, false, NULL);
2037 if (TREE_CODE (value) == INTEGER_CST)
2038 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2039 "is not an integer constant expression");
2041 if (TREE_CODE (value) != INTEGER_CST)
2043 error_at (value_loc, "expression in static assertion is not constant");
2044 return;
2046 constant_expression_warning (value);
2047 if (integer_zerop (value))
2048 error_at (assert_loc, "static assertion failed: %E", string);
2051 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2052 6.7), adding them to SPECS (which may already include some).
2053 Storage class specifiers are accepted iff SCSPEC_OK; type
2054 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2055 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2056 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2058 declaration-specifiers:
2059 storage-class-specifier declaration-specifiers[opt]
2060 type-specifier declaration-specifiers[opt]
2061 type-qualifier declaration-specifiers[opt]
2062 function-specifier declaration-specifiers[opt]
2063 alignment-specifier declaration-specifiers[opt]
2065 Function specifiers (inline) are from C99, and are currently
2066 handled as storage class specifiers, as is __thread. Alignment
2067 specifiers are from C11.
2069 C90 6.5.1, C99 6.7.1:
2070 storage-class-specifier:
2071 typedef
2072 extern
2073 static
2074 auto
2075 register
2076 _Thread_local
2078 (_Thread_local is new in C11.)
2080 C99 6.7.4:
2081 function-specifier:
2082 inline
2083 _Noreturn
2085 (_Noreturn is new in C11.)
2087 C90 6.5.2, C99 6.7.2:
2088 type-specifier:
2089 void
2090 char
2091 short
2093 long
2094 float
2095 double
2096 signed
2097 unsigned
2098 _Bool
2099 _Complex
2100 [_Imaginary removed in C99 TC2]
2101 struct-or-union-specifier
2102 enum-specifier
2103 typedef-name
2104 atomic-type-specifier
2106 (_Bool and _Complex are new in C99.)
2107 (atomic-type-specifier is new in C11.)
2109 C90 6.5.3, C99 6.7.3:
2111 type-qualifier:
2112 const
2113 restrict
2114 volatile
2115 address-space-qualifier
2116 _Atomic
2118 (restrict is new in C99.)
2119 (_Atomic is new in C11.)
2121 GNU extensions:
2123 declaration-specifiers:
2124 attributes declaration-specifiers[opt]
2126 type-qualifier:
2127 address-space
2129 address-space:
2130 identifier recognized by the target
2132 storage-class-specifier:
2133 __thread
2135 type-specifier:
2136 typeof-specifier
2137 __auto_type
2138 __int128
2139 _Decimal32
2140 _Decimal64
2141 _Decimal128
2142 _Fract
2143 _Accum
2144 _Sat
2146 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2147 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2149 atomic-type-specifier
2150 _Atomic ( type-name )
2152 Objective-C:
2154 type-specifier:
2155 class-name objc-protocol-refs[opt]
2156 typedef-name objc-protocol-refs
2157 objc-protocol-refs
2160 static void
2161 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2162 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2163 bool alignspec_ok, bool auto_type_ok,
2164 enum c_lookahead_kind la)
2166 bool attrs_ok = start_attr_ok;
2167 bool seen_type = specs->typespec_kind != ctsk_none;
2169 if (!typespec_ok)
2170 gcc_assert (la == cla_prefer_id);
2172 while (c_parser_next_token_is (parser, CPP_NAME)
2173 || c_parser_next_token_is (parser, CPP_KEYWORD)
2174 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2176 struct c_typespec t;
2177 tree attrs;
2178 tree align;
2179 location_t loc = c_parser_peek_token (parser)->location;
2181 /* If we cannot accept a type, exit if the next token must start
2182 one. Also, if we already have seen a tagged definition,
2183 a typename would be an error anyway and likely the user
2184 has simply forgotten a semicolon, so we exit. */
2185 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2186 && c_parser_next_tokens_start_typename (parser, la)
2187 && !c_parser_next_token_is_qualifier (parser))
2188 break;
2190 if (c_parser_next_token_is (parser, CPP_NAME))
2192 c_token *name_token = c_parser_peek_token (parser);
2193 tree value = name_token->value;
2194 c_id_kind kind = name_token->id_kind;
2196 if (kind == C_ID_ADDRSPACE)
2198 addr_space_t as
2199 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2200 declspecs_add_addrspace (name_token->location, specs, as);
2201 c_parser_consume_token (parser);
2202 attrs_ok = true;
2203 continue;
2206 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2208 /* If we cannot accept a type, and the next token must start one,
2209 exit. Do the same if we already have seen a tagged definition,
2210 since it would be an error anyway and likely the user has simply
2211 forgotten a semicolon. */
2212 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2213 break;
2215 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2216 a C_ID_CLASSNAME. */
2217 c_parser_consume_token (parser);
2218 seen_type = true;
2219 attrs_ok = true;
2220 if (kind == C_ID_ID)
2222 error_at (loc, "unknown type name %qE", value);
2223 t.kind = ctsk_typedef;
2224 t.spec = error_mark_node;
2226 else if (kind == C_ID_TYPENAME
2227 && (!c_dialect_objc ()
2228 || c_parser_next_token_is_not (parser, CPP_LESS)))
2230 t.kind = ctsk_typedef;
2231 /* For a typedef name, record the meaning, not the name.
2232 In case of 'foo foo, bar;'. */
2233 t.spec = lookup_name (value);
2235 else
2237 tree proto = NULL_TREE;
2238 gcc_assert (c_dialect_objc ());
2239 t.kind = ctsk_objc;
2240 if (c_parser_next_token_is (parser, CPP_LESS))
2241 proto = c_parser_objc_protocol_refs (parser);
2242 t.spec = objc_get_protocol_qualified_type (value, proto);
2244 t.expr = NULL_TREE;
2245 t.expr_const_operands = true;
2246 declspecs_add_type (name_token->location, specs, t);
2247 continue;
2249 if (c_parser_next_token_is (parser, CPP_LESS))
2251 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2252 nisse@lysator.liu.se. */
2253 tree proto;
2254 gcc_assert (c_dialect_objc ());
2255 if (!typespec_ok || seen_type)
2256 break;
2257 proto = c_parser_objc_protocol_refs (parser);
2258 t.kind = ctsk_objc;
2259 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2260 t.expr = NULL_TREE;
2261 t.expr_const_operands = true;
2262 declspecs_add_type (loc, specs, t);
2263 continue;
2265 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2266 switch (c_parser_peek_token (parser)->keyword)
2268 case RID_STATIC:
2269 case RID_EXTERN:
2270 case RID_REGISTER:
2271 case RID_TYPEDEF:
2272 case RID_INLINE:
2273 case RID_NORETURN:
2274 case RID_AUTO:
2275 case RID_THREAD:
2276 if (!scspec_ok)
2277 goto out;
2278 attrs_ok = true;
2279 /* TODO: Distinguish between function specifiers (inline, noreturn)
2280 and storage class specifiers, either here or in
2281 declspecs_add_scspec. */
2282 declspecs_add_scspec (loc, specs,
2283 c_parser_peek_token (parser)->value);
2284 c_parser_consume_token (parser);
2285 break;
2286 case RID_AUTO_TYPE:
2287 if (!auto_type_ok)
2288 goto out;
2289 /* Fall through. */
2290 case RID_UNSIGNED:
2291 case RID_LONG:
2292 case RID_INT128:
2293 case RID_SHORT:
2294 case RID_SIGNED:
2295 case RID_COMPLEX:
2296 case RID_INT:
2297 case RID_CHAR:
2298 case RID_FLOAT:
2299 case RID_DOUBLE:
2300 case RID_VOID:
2301 case RID_DFLOAT32:
2302 case RID_DFLOAT64:
2303 case RID_DFLOAT128:
2304 case RID_BOOL:
2305 case RID_FRACT:
2306 case RID_ACCUM:
2307 case RID_SAT:
2308 if (!typespec_ok)
2309 goto out;
2310 attrs_ok = true;
2311 seen_type = true;
2312 if (c_dialect_objc ())
2313 parser->objc_need_raw_identifier = true;
2314 t.kind = ctsk_resword;
2315 t.spec = c_parser_peek_token (parser)->value;
2316 t.expr = NULL_TREE;
2317 t.expr_const_operands = true;
2318 declspecs_add_type (loc, specs, t);
2319 c_parser_consume_token (parser);
2320 break;
2321 case RID_ENUM:
2322 if (!typespec_ok)
2323 goto out;
2324 attrs_ok = true;
2325 seen_type = true;
2326 t = c_parser_enum_specifier (parser);
2327 declspecs_add_type (loc, specs, t);
2328 break;
2329 case RID_STRUCT:
2330 case RID_UNION:
2331 if (!typespec_ok)
2332 goto out;
2333 attrs_ok = true;
2334 seen_type = true;
2335 t = c_parser_struct_or_union_specifier (parser);
2336 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2337 declspecs_add_type (loc, specs, t);
2338 break;
2339 case RID_TYPEOF:
2340 /* ??? The old parser rejected typeof after other type
2341 specifiers, but is a syntax error the best way of
2342 handling this? */
2343 if (!typespec_ok || seen_type)
2344 goto out;
2345 attrs_ok = true;
2346 seen_type = true;
2347 t = c_parser_typeof_specifier (parser);
2348 declspecs_add_type (loc, specs, t);
2349 break;
2350 case RID_ATOMIC:
2351 /* C parser handling of Objective-C constructs needs
2352 checking for correct lvalue-to-rvalue conversions, and
2353 the code in build_modify_expr handling various
2354 Objective-C cases, and that in build_unary_op handling
2355 Objective-C cases for increment / decrement, also needs
2356 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2357 and objc_types_are_equivalent may also need updates. */
2358 if (c_dialect_objc ())
2359 sorry ("%<_Atomic%> in Objective-C");
2360 /* C parser handling of OpenMP constructs needs checking for
2361 correct lvalue-to-rvalue conversions. */
2362 if (flag_openmp)
2363 sorry ("%<_Atomic%> with OpenMP");
2364 if (!flag_isoc11)
2366 if (flag_isoc99)
2367 pedwarn (loc, OPT_Wpedantic,
2368 "ISO C99 does not support the %<_Atomic%> qualifier");
2369 else
2370 pedwarn (loc, OPT_Wpedantic,
2371 "ISO C90 does not support the %<_Atomic%> qualifier");
2373 attrs_ok = true;
2374 tree value;
2375 value = c_parser_peek_token (parser)->value;
2376 c_parser_consume_token (parser);
2377 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2379 /* _Atomic ( type-name ). */
2380 seen_type = true;
2381 c_parser_consume_token (parser);
2382 struct c_type_name *type = c_parser_type_name (parser);
2383 t.kind = ctsk_typeof;
2384 t.spec = error_mark_node;
2385 t.expr = NULL_TREE;
2386 t.expr_const_operands = true;
2387 if (type != NULL)
2388 t.spec = groktypename (type, &t.expr,
2389 &t.expr_const_operands);
2390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2391 "expected %<)%>");
2392 if (t.spec != error_mark_node)
2394 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2395 error_at (loc, "%<_Atomic%>-qualified array type");
2396 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2397 error_at (loc, "%<_Atomic%>-qualified function type");
2398 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2399 error_at (loc, "%<_Atomic%> applied to a qualified type");
2400 else
2401 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2403 declspecs_add_type (loc, specs, t);
2405 else
2406 declspecs_add_qual (loc, specs, value);
2407 break;
2408 case RID_CONST:
2409 case RID_VOLATILE:
2410 case RID_RESTRICT:
2411 attrs_ok = true;
2412 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2413 c_parser_consume_token (parser);
2414 break;
2415 case RID_ATTRIBUTE:
2416 if (!attrs_ok)
2417 goto out;
2418 attrs = c_parser_attributes (parser);
2419 declspecs_add_attrs (loc, specs, attrs);
2420 break;
2421 case RID_ALIGNAS:
2422 if (!alignspec_ok)
2423 goto out;
2424 align = c_parser_alignas_specifier (parser);
2425 declspecs_add_alignas (loc, specs, align);
2426 break;
2427 default:
2428 goto out;
2431 out: ;
2434 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2436 enum-specifier:
2437 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2438 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2439 enum attributes[opt] identifier
2441 The form with trailing comma is new in C99. The forms with
2442 attributes are GNU extensions. In GNU C, we accept any expression
2443 without commas in the syntax (assignment expressions, not just
2444 conditional expressions); assignment expressions will be diagnosed
2445 as non-constant.
2447 enumerator-list:
2448 enumerator
2449 enumerator-list , enumerator
2451 enumerator:
2452 enumeration-constant
2453 enumeration-constant = constant-expression
2456 static struct c_typespec
2457 c_parser_enum_specifier (c_parser *parser)
2459 struct c_typespec ret;
2460 tree attrs;
2461 tree ident = NULL_TREE;
2462 location_t enum_loc;
2463 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2464 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2465 enum_loc = c_parser_peek_token (parser)->location;
2466 c_parser_consume_token (parser);
2467 attrs = c_parser_attributes (parser);
2468 enum_loc = c_parser_peek_token (parser)->location;
2469 /* Set the location in case we create a decl now. */
2470 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2471 if (c_parser_next_token_is (parser, CPP_NAME))
2473 ident = c_parser_peek_token (parser)->value;
2474 ident_loc = c_parser_peek_token (parser)->location;
2475 enum_loc = ident_loc;
2476 c_parser_consume_token (parser);
2478 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2480 /* Parse an enum definition. */
2481 struct c_enum_contents the_enum;
2482 tree type;
2483 tree postfix_attrs;
2484 /* We chain the enumerators in reverse order, then put them in
2485 forward order at the end. */
2486 tree values;
2487 timevar_push (TV_PARSE_ENUM);
2488 type = start_enum (enum_loc, &the_enum, ident);
2489 values = NULL_TREE;
2490 c_parser_consume_token (parser);
2491 while (true)
2493 tree enum_id;
2494 tree enum_value;
2495 tree enum_decl;
2496 bool seen_comma;
2497 c_token *token;
2498 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2499 location_t decl_loc, value_loc;
2500 if (c_parser_next_token_is_not (parser, CPP_NAME))
2502 c_parser_error (parser, "expected identifier");
2503 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2504 values = error_mark_node;
2505 break;
2507 token = c_parser_peek_token (parser);
2508 enum_id = token->value;
2509 /* Set the location in case we create a decl now. */
2510 c_parser_set_source_position_from_token (token);
2511 decl_loc = value_loc = token->location;
2512 c_parser_consume_token (parser);
2513 if (c_parser_next_token_is (parser, CPP_EQ))
2515 c_parser_consume_token (parser);
2516 value_loc = c_parser_peek_token (parser)->location;
2517 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2519 else
2520 enum_value = NULL_TREE;
2521 enum_decl = build_enumerator (decl_loc, value_loc,
2522 &the_enum, enum_id, enum_value);
2523 TREE_CHAIN (enum_decl) = values;
2524 values = enum_decl;
2525 seen_comma = false;
2526 if (c_parser_next_token_is (parser, CPP_COMMA))
2528 comma_loc = c_parser_peek_token (parser)->location;
2529 seen_comma = true;
2530 c_parser_consume_token (parser);
2532 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2534 if (seen_comma && !flag_isoc99)
2535 pedwarn (comma_loc, OPT_Wpedantic, "comma at end of enumerator list");
2536 c_parser_consume_token (parser);
2537 break;
2539 if (!seen_comma)
2541 c_parser_error (parser, "expected %<,%> or %<}%>");
2542 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2543 values = error_mark_node;
2544 break;
2547 postfix_attrs = c_parser_attributes (parser);
2548 ret.spec = finish_enum (type, nreverse (values),
2549 chainon (attrs, postfix_attrs));
2550 ret.kind = ctsk_tagdef;
2551 ret.expr = NULL_TREE;
2552 ret.expr_const_operands = true;
2553 timevar_pop (TV_PARSE_ENUM);
2554 return ret;
2556 else if (!ident)
2558 c_parser_error (parser, "expected %<{%>");
2559 ret.spec = error_mark_node;
2560 ret.kind = ctsk_tagref;
2561 ret.expr = NULL_TREE;
2562 ret.expr_const_operands = true;
2563 return ret;
2565 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2566 /* In ISO C, enumerated types can be referred to only if already
2567 defined. */
2568 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2570 gcc_assert (ident);
2571 pedwarn (enum_loc, OPT_Wpedantic,
2572 "ISO C forbids forward references to %<enum%> types");
2574 return ret;
2577 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2579 struct-or-union-specifier:
2580 struct-or-union attributes[opt] identifier[opt]
2581 { struct-contents } attributes[opt]
2582 struct-or-union attributes[opt] identifier
2584 struct-contents:
2585 struct-declaration-list
2587 struct-declaration-list:
2588 struct-declaration ;
2589 struct-declaration-list struct-declaration ;
2591 GNU extensions:
2593 struct-contents:
2594 empty
2595 struct-declaration
2596 struct-declaration-list struct-declaration
2598 struct-declaration-list:
2599 struct-declaration-list ;
2602 (Note that in the syntax here, unlike that in ISO C, the semicolons
2603 are included here rather than in struct-declaration, in order to
2604 describe the syntax with extra semicolons and missing semicolon at
2605 end.)
2607 Objective-C:
2609 struct-declaration-list:
2610 @defs ( class-name )
2612 (Note this does not include a trailing semicolon, but can be
2613 followed by further declarations, and gets a pedwarn-if-pedantic
2614 when followed by a semicolon.) */
2616 static struct c_typespec
2617 c_parser_struct_or_union_specifier (c_parser *parser)
2619 struct c_typespec ret;
2620 tree attrs;
2621 tree ident = NULL_TREE;
2622 location_t struct_loc;
2623 location_t ident_loc = UNKNOWN_LOCATION;
2624 enum tree_code code;
2625 switch (c_parser_peek_token (parser)->keyword)
2627 case RID_STRUCT:
2628 code = RECORD_TYPE;
2629 break;
2630 case RID_UNION:
2631 code = UNION_TYPE;
2632 break;
2633 default:
2634 gcc_unreachable ();
2636 struct_loc = c_parser_peek_token (parser)->location;
2637 c_parser_consume_token (parser);
2638 attrs = c_parser_attributes (parser);
2640 /* Set the location in case we create a decl now. */
2641 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2643 if (c_parser_next_token_is (parser, CPP_NAME))
2645 ident = c_parser_peek_token (parser)->value;
2646 ident_loc = c_parser_peek_token (parser)->location;
2647 struct_loc = ident_loc;
2648 c_parser_consume_token (parser);
2650 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2652 /* Parse a struct or union definition. Start the scope of the
2653 tag before parsing components. */
2654 struct c_struct_parse_info *struct_info;
2655 tree type = start_struct (struct_loc, code, ident, &struct_info);
2656 tree postfix_attrs;
2657 /* We chain the components in reverse order, then put them in
2658 forward order at the end. Each struct-declaration may
2659 declare multiple components (comma-separated), so we must use
2660 chainon to join them, although when parsing each
2661 struct-declaration we can use TREE_CHAIN directly.
2663 The theory behind all this is that there will be more
2664 semicolon separated fields than comma separated fields, and
2665 so we'll be minimizing the number of node traversals required
2666 by chainon. */
2667 tree contents;
2668 timevar_push (TV_PARSE_STRUCT);
2669 contents = NULL_TREE;
2670 c_parser_consume_token (parser);
2671 /* Handle the Objective-C @defs construct,
2672 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2673 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2675 tree name;
2676 gcc_assert (c_dialect_objc ());
2677 c_parser_consume_token (parser);
2678 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2679 goto end_at_defs;
2680 if (c_parser_next_token_is (parser, CPP_NAME)
2681 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2683 name = c_parser_peek_token (parser)->value;
2684 c_parser_consume_token (parser);
2686 else
2688 c_parser_error (parser, "expected class name");
2689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2690 goto end_at_defs;
2692 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2693 "expected %<)%>");
2694 contents = nreverse (objc_get_class_ivars (name));
2696 end_at_defs:
2697 /* Parse the struct-declarations and semicolons. Problems with
2698 semicolons are diagnosed here; empty structures are diagnosed
2699 elsewhere. */
2700 while (true)
2702 tree decls;
2703 /* Parse any stray semicolon. */
2704 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2706 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2707 "extra semicolon in struct or union specified");
2708 c_parser_consume_token (parser);
2709 continue;
2711 /* Stop if at the end of the struct or union contents. */
2712 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2714 c_parser_consume_token (parser);
2715 break;
2717 /* Accept #pragmas at struct scope. */
2718 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2720 c_parser_pragma (parser, pragma_struct);
2721 continue;
2723 /* Parse some comma-separated declarations, but not the
2724 trailing semicolon if any. */
2725 decls = c_parser_struct_declaration (parser);
2726 contents = chainon (decls, contents);
2727 /* If no semicolon follows, either we have a parse error or
2728 are at the end of the struct or union and should
2729 pedwarn. */
2730 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2731 c_parser_consume_token (parser);
2732 else
2734 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2735 pedwarn (c_parser_peek_token (parser)->location, 0,
2736 "no semicolon at end of struct or union");
2737 else if (parser->error
2738 || !c_parser_next_token_starts_declspecs (parser))
2740 c_parser_error (parser, "expected %<;%>");
2741 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2742 break;
2745 /* If we come here, we have already emitted an error
2746 for an expected `;', identifier or `(', and we also
2747 recovered already. Go on with the next field. */
2750 postfix_attrs = c_parser_attributes (parser);
2751 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2752 chainon (attrs, postfix_attrs), struct_info);
2753 ret.kind = ctsk_tagdef;
2754 ret.expr = NULL_TREE;
2755 ret.expr_const_operands = true;
2756 timevar_pop (TV_PARSE_STRUCT);
2757 return ret;
2759 else if (!ident)
2761 c_parser_error (parser, "expected %<{%>");
2762 ret.spec = error_mark_node;
2763 ret.kind = ctsk_tagref;
2764 ret.expr = NULL_TREE;
2765 ret.expr_const_operands = true;
2766 return ret;
2768 ret = parser_xref_tag (ident_loc, code, ident);
2769 return ret;
2772 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2773 the trailing semicolon.
2775 struct-declaration:
2776 specifier-qualifier-list struct-declarator-list
2777 static_assert-declaration-no-semi
2779 specifier-qualifier-list:
2780 type-specifier specifier-qualifier-list[opt]
2781 type-qualifier specifier-qualifier-list[opt]
2782 attributes specifier-qualifier-list[opt]
2784 struct-declarator-list:
2785 struct-declarator
2786 struct-declarator-list , attributes[opt] struct-declarator
2788 struct-declarator:
2789 declarator attributes[opt]
2790 declarator[opt] : constant-expression attributes[opt]
2792 GNU extensions:
2794 struct-declaration:
2795 __extension__ struct-declaration
2796 specifier-qualifier-list
2798 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2799 of attributes where shown is a GNU extension. In GNU C, we accept
2800 any expression without commas in the syntax (assignment
2801 expressions, not just conditional expressions); assignment
2802 expressions will be diagnosed as non-constant. */
2804 static tree
2805 c_parser_struct_declaration (c_parser *parser)
2807 struct c_declspecs *specs;
2808 tree prefix_attrs;
2809 tree all_prefix_attrs;
2810 tree decls;
2811 location_t decl_loc;
2812 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2814 int ext;
2815 tree decl;
2816 ext = disable_extension_diagnostics ();
2817 c_parser_consume_token (parser);
2818 decl = c_parser_struct_declaration (parser);
2819 restore_extension_diagnostics (ext);
2820 return decl;
2822 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2824 c_parser_static_assert_declaration_no_semi (parser);
2825 return NULL_TREE;
2827 specs = build_null_declspecs ();
2828 decl_loc = c_parser_peek_token (parser)->location;
2829 /* Strictly by the standard, we shouldn't allow _Alignas here,
2830 but it appears to have been intended to allow it there, so
2831 we're keeping it as it is until WG14 reaches a conclusion
2832 of N1731.
2833 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2834 c_parser_declspecs (parser, specs, false, true, true,
2835 true, false, cla_nonabstract_decl);
2836 if (parser->error)
2837 return NULL_TREE;
2838 if (!specs->declspecs_seen_p)
2840 c_parser_error (parser, "expected specifier-qualifier-list");
2841 return NULL_TREE;
2843 finish_declspecs (specs);
2844 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2845 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2847 tree ret;
2848 if (specs->typespec_kind == ctsk_none)
2850 pedwarn (decl_loc, OPT_Wpedantic,
2851 "ISO C forbids member declarations with no members");
2852 shadow_tag_warned (specs, pedantic);
2853 ret = NULL_TREE;
2855 else
2857 /* Support for unnamed structs or unions as members of
2858 structs or unions (which is [a] useful and [b] supports
2859 MS P-SDK). */
2860 tree attrs = NULL;
2862 ret = grokfield (c_parser_peek_token (parser)->location,
2863 build_id_declarator (NULL_TREE), specs,
2864 NULL_TREE, &attrs);
2865 if (ret)
2866 decl_attributes (&ret, attrs, 0);
2868 return ret;
2871 /* Provide better error recovery. Note that a type name here is valid,
2872 and will be treated as a field name. */
2873 if (specs->typespec_kind == ctsk_tagdef
2874 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2875 && c_parser_next_token_starts_declspecs (parser)
2876 && !c_parser_next_token_is (parser, CPP_NAME))
2878 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2879 parser->error = false;
2880 return NULL_TREE;
2883 pending_xref_error ();
2884 prefix_attrs = specs->attrs;
2885 all_prefix_attrs = prefix_attrs;
2886 specs->attrs = NULL_TREE;
2887 decls = NULL_TREE;
2888 while (true)
2890 /* Declaring one or more declarators or un-named bit-fields. */
2891 struct c_declarator *declarator;
2892 bool dummy = false;
2893 if (c_parser_next_token_is (parser, CPP_COLON))
2894 declarator = build_id_declarator (NULL_TREE);
2895 else
2896 declarator = c_parser_declarator (parser,
2897 specs->typespec_kind != ctsk_none,
2898 C_DTR_NORMAL, &dummy);
2899 if (declarator == NULL)
2901 c_parser_skip_to_end_of_block_or_statement (parser);
2902 break;
2904 if (c_parser_next_token_is (parser, CPP_COLON)
2905 || c_parser_next_token_is (parser, CPP_COMMA)
2906 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2907 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2908 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2910 tree postfix_attrs = NULL_TREE;
2911 tree width = NULL_TREE;
2912 tree d;
2913 if (c_parser_next_token_is (parser, CPP_COLON))
2915 c_parser_consume_token (parser);
2916 width = c_parser_expr_no_commas (parser, NULL).value;
2918 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2919 postfix_attrs = c_parser_attributes (parser);
2920 d = grokfield (c_parser_peek_token (parser)->location,
2921 declarator, specs, width, &all_prefix_attrs);
2922 decl_attributes (&d, chainon (postfix_attrs,
2923 all_prefix_attrs), 0);
2924 DECL_CHAIN (d) = decls;
2925 decls = d;
2926 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2927 all_prefix_attrs = chainon (c_parser_attributes (parser),
2928 prefix_attrs);
2929 else
2930 all_prefix_attrs = prefix_attrs;
2931 if (c_parser_next_token_is (parser, CPP_COMMA))
2932 c_parser_consume_token (parser);
2933 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2934 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2936 /* Semicolon consumed in caller. */
2937 break;
2939 else
2941 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2942 break;
2945 else
2947 c_parser_error (parser,
2948 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
2949 "%<__attribute__%>");
2950 break;
2953 return decls;
2956 /* Parse a typeof specifier (a GNU extension).
2958 typeof-specifier:
2959 typeof ( expression )
2960 typeof ( type-name )
2963 static struct c_typespec
2964 c_parser_typeof_specifier (c_parser *parser)
2966 struct c_typespec ret;
2967 ret.kind = ctsk_typeof;
2968 ret.spec = error_mark_node;
2969 ret.expr = NULL_TREE;
2970 ret.expr_const_operands = true;
2971 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
2972 c_parser_consume_token (parser);
2973 c_inhibit_evaluation_warnings++;
2974 in_typeof++;
2975 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2977 c_inhibit_evaluation_warnings--;
2978 in_typeof--;
2979 return ret;
2981 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
2983 struct c_type_name *type = c_parser_type_name (parser);
2984 c_inhibit_evaluation_warnings--;
2985 in_typeof--;
2986 if (type != NULL)
2988 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
2989 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
2992 else
2994 bool was_vm;
2995 location_t here = c_parser_peek_token (parser)->location;
2996 struct c_expr expr = c_parser_expression (parser);
2997 c_inhibit_evaluation_warnings--;
2998 in_typeof--;
2999 if (TREE_CODE (expr.value) == COMPONENT_REF
3000 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3001 error_at (here, "%<typeof%> applied to a bit-field");
3002 mark_exp_read (expr.value);
3003 ret.spec = TREE_TYPE (expr.value);
3004 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3005 /* This is returned with the type so that when the type is
3006 evaluated, this can be evaluated. */
3007 if (was_vm)
3008 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3009 pop_maybe_used (was_vm);
3010 /* For use in macros such as those in <stdatomic.h>, remove all
3011 qualifiers from atomic types. (const can be an issue for more macros
3012 using typeof than just the <stdatomic.h> ones.) */
3013 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3014 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3016 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3017 return ret;
3020 /* Parse an alignment-specifier.
3022 C11 6.7.5:
3024 alignment-specifier:
3025 _Alignas ( type-name )
3026 _Alignas ( constant-expression )
3029 static tree
3030 c_parser_alignas_specifier (c_parser * parser)
3032 tree ret = error_mark_node;
3033 location_t loc = c_parser_peek_token (parser)->location;
3034 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3035 c_parser_consume_token (parser);
3036 if (!flag_isoc11)
3038 if (flag_isoc99)
3039 pedwarn (loc, OPT_Wpedantic,
3040 "ISO C99 does not support %<_Alignas%>");
3041 else
3042 pedwarn (loc, OPT_Wpedantic,
3043 "ISO C90 does not support %<_Alignas%>");
3045 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3046 return ret;
3047 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3049 struct c_type_name *type = c_parser_type_name (parser);
3050 if (type != NULL)
3051 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3052 false, true, 1);
3054 else
3055 ret = c_parser_expr_no_commas (parser, NULL).value;
3056 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3057 return ret;
3060 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3061 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3062 be redeclared; otherwise it may not. KIND indicates which kind of
3063 declarator is wanted. Returns a valid declarator except in the
3064 case of a syntax error in which case NULL is returned. *SEEN_ID is
3065 set to true if an identifier being declared is seen; this is used
3066 to diagnose bad forms of abstract array declarators and to
3067 determine whether an identifier list is syntactically permitted.
3069 declarator:
3070 pointer[opt] direct-declarator
3072 direct-declarator:
3073 identifier
3074 ( attributes[opt] declarator )
3075 direct-declarator array-declarator
3076 direct-declarator ( parameter-type-list )
3077 direct-declarator ( identifier-list[opt] )
3079 pointer:
3080 * type-qualifier-list[opt]
3081 * type-qualifier-list[opt] pointer
3083 type-qualifier-list:
3084 type-qualifier
3085 attributes
3086 type-qualifier-list type-qualifier
3087 type-qualifier-list attributes
3089 array-declarator:
3090 [ type-qualifier-list[opt] assignment-expression[opt] ]
3091 [ static type-qualifier-list[opt] assignment-expression ]
3092 [ type-qualifier-list static assignment-expression ]
3093 [ type-qualifier-list[opt] * ]
3095 parameter-type-list:
3096 parameter-list
3097 parameter-list , ...
3099 parameter-list:
3100 parameter-declaration
3101 parameter-list , parameter-declaration
3103 parameter-declaration:
3104 declaration-specifiers declarator attributes[opt]
3105 declaration-specifiers abstract-declarator[opt] attributes[opt]
3107 identifier-list:
3108 identifier
3109 identifier-list , identifier
3111 abstract-declarator:
3112 pointer
3113 pointer[opt] direct-abstract-declarator
3115 direct-abstract-declarator:
3116 ( attributes[opt] abstract-declarator )
3117 direct-abstract-declarator[opt] array-declarator
3118 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3120 GNU extensions:
3122 direct-declarator:
3123 direct-declarator ( parameter-forward-declarations
3124 parameter-type-list[opt] )
3126 direct-abstract-declarator:
3127 direct-abstract-declarator[opt] ( parameter-forward-declarations
3128 parameter-type-list[opt] )
3130 parameter-forward-declarations:
3131 parameter-list ;
3132 parameter-forward-declarations parameter-list ;
3134 The uses of attributes shown above are GNU extensions.
3136 Some forms of array declarator are not included in C99 in the
3137 syntax for abstract declarators; these are disallowed elsewhere.
3138 This may be a defect (DR#289).
3140 This function also accepts an omitted abstract declarator as being
3141 an abstract declarator, although not part of the formal syntax. */
3143 static struct c_declarator *
3144 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3145 bool *seen_id)
3147 /* Parse any initial pointer part. */
3148 if (c_parser_next_token_is (parser, CPP_MULT))
3150 struct c_declspecs *quals_attrs = build_null_declspecs ();
3151 struct c_declarator *inner;
3152 c_parser_consume_token (parser);
3153 c_parser_declspecs (parser, quals_attrs, false, false, true,
3154 false, false, cla_prefer_id);
3155 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3156 if (inner == NULL)
3157 return NULL;
3158 else
3159 return make_pointer_declarator (quals_attrs, inner);
3161 /* Now we have a direct declarator, direct abstract declarator or
3162 nothing (which counts as a direct abstract declarator here). */
3163 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3166 /* Parse a direct declarator or direct abstract declarator; arguments
3167 as c_parser_declarator. */
3169 static struct c_declarator *
3170 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3171 bool *seen_id)
3173 /* The direct declarator must start with an identifier (possibly
3174 omitted) or a parenthesized declarator (possibly abstract). In
3175 an ordinary declarator, initial parentheses must start a
3176 parenthesized declarator. In an abstract declarator or parameter
3177 declarator, they could start a parenthesized declarator or a
3178 parameter list. To tell which, the open parenthesis and any
3179 following attributes must be read. If a declaration specifier
3180 follows, then it is a parameter list; if the specifier is a
3181 typedef name, there might be an ambiguity about redeclaring it,
3182 which is resolved in the direction of treating it as a typedef
3183 name. If a close parenthesis follows, it is also an empty
3184 parameter list, as the syntax does not permit empty abstract
3185 declarators. Otherwise, it is a parenthesized declarator (in
3186 which case the analysis may be repeated inside it, recursively).
3188 ??? There is an ambiguity in a parameter declaration "int
3189 (__attribute__((foo)) x)", where x is not a typedef name: it
3190 could be an abstract declarator for a function, or declare x with
3191 parentheses. The proper resolution of this ambiguity needs
3192 documenting. At present we follow an accident of the old
3193 parser's implementation, whereby the first parameter must have
3194 some declaration specifiers other than just attributes. Thus as
3195 a parameter declaration it is treated as a parenthesized
3196 parameter named x, and as an abstract declarator it is
3197 rejected.
3199 ??? Also following the old parser, attributes inside an empty
3200 parameter list are ignored, making it a list not yielding a
3201 prototype, rather than giving an error or making it have one
3202 parameter with implicit type int.
3204 ??? Also following the old parser, typedef names may be
3205 redeclared in declarators, but not Objective-C class names. */
3207 if (kind != C_DTR_ABSTRACT
3208 && c_parser_next_token_is (parser, CPP_NAME)
3209 && ((type_seen_p
3210 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3211 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3212 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3214 struct c_declarator *inner
3215 = build_id_declarator (c_parser_peek_token (parser)->value);
3216 *seen_id = true;
3217 inner->id_loc = c_parser_peek_token (parser)->location;
3218 c_parser_consume_token (parser);
3219 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3222 if (kind != C_DTR_NORMAL
3223 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3225 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3226 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3229 /* Either we are at the end of an abstract declarator, or we have
3230 parentheses. */
3232 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3234 tree attrs;
3235 struct c_declarator *inner;
3236 c_parser_consume_token (parser);
3237 attrs = c_parser_attributes (parser);
3238 if (kind != C_DTR_NORMAL
3239 && (c_parser_next_token_starts_declspecs (parser)
3240 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3242 struct c_arg_info *args
3243 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3244 attrs);
3245 if (args == NULL)
3246 return NULL;
3247 else
3249 inner
3250 = build_function_declarator (args,
3251 build_id_declarator (NULL_TREE));
3252 return c_parser_direct_declarator_inner (parser, *seen_id,
3253 inner);
3256 /* A parenthesized declarator. */
3257 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3258 if (inner != NULL && attrs != NULL)
3259 inner = build_attrs_declarator (attrs, inner);
3260 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3262 c_parser_consume_token (parser);
3263 if (inner == NULL)
3264 return NULL;
3265 else
3266 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3268 else
3270 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3271 "expected %<)%>");
3272 return NULL;
3275 else
3277 if (kind == C_DTR_NORMAL)
3279 c_parser_error (parser, "expected identifier or %<(%>");
3280 return NULL;
3282 else
3283 return build_id_declarator (NULL_TREE);
3287 /* Parse part of a direct declarator or direct abstract declarator,
3288 given that some (in INNER) has already been parsed; ID_PRESENT is
3289 true if an identifier is present, false for an abstract
3290 declarator. */
3292 static struct c_declarator *
3293 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3294 struct c_declarator *inner)
3296 /* Parse a sequence of array declarators and parameter lists. */
3297 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3299 location_t brace_loc = c_parser_peek_token (parser)->location;
3300 struct c_declarator *declarator;
3301 struct c_declspecs *quals_attrs = build_null_declspecs ();
3302 bool static_seen;
3303 bool star_seen;
3304 struct c_expr dimen;
3305 dimen.value = NULL_TREE;
3306 dimen.original_code = ERROR_MARK;
3307 dimen.original_type = NULL_TREE;
3308 c_parser_consume_token (parser);
3309 c_parser_declspecs (parser, quals_attrs, false, false, true,
3310 false, false, cla_prefer_id);
3311 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3312 if (static_seen)
3313 c_parser_consume_token (parser);
3314 if (static_seen && !quals_attrs->declspecs_seen_p)
3315 c_parser_declspecs (parser, quals_attrs, false, false, true,
3316 false, false, cla_prefer_id);
3317 if (!quals_attrs->declspecs_seen_p)
3318 quals_attrs = NULL;
3319 /* If "static" is present, there must be an array dimension.
3320 Otherwise, there may be a dimension, "*", or no
3321 dimension. */
3322 if (static_seen)
3324 star_seen = false;
3325 dimen = c_parser_expr_no_commas (parser, NULL);
3327 else
3329 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3331 dimen.value = NULL_TREE;
3332 star_seen = false;
3334 else if (flag_cilkplus
3335 && c_parser_next_token_is (parser, CPP_COLON))
3337 dimen.value = error_mark_node;
3338 star_seen = false;
3339 error_at (c_parser_peek_token (parser)->location,
3340 "array notations cannot be used in declaration");
3341 c_parser_consume_token (parser);
3343 else if (c_parser_next_token_is (parser, CPP_MULT))
3345 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3347 dimen.value = NULL_TREE;
3348 star_seen = true;
3349 c_parser_consume_token (parser);
3351 else
3353 star_seen = false;
3354 dimen = c_parser_expr_no_commas (parser, NULL);
3357 else
3359 star_seen = false;
3360 dimen = c_parser_expr_no_commas (parser, NULL);
3363 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3364 c_parser_consume_token (parser);
3365 else if (flag_cilkplus
3366 && c_parser_next_token_is (parser, CPP_COLON))
3368 error_at (c_parser_peek_token (parser)->location,
3369 "array notations cannot be used in declaration");
3370 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3371 return NULL;
3373 else
3375 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3376 "expected %<]%>");
3377 return NULL;
3379 if (dimen.value)
3380 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3381 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3382 static_seen, star_seen);
3383 if (declarator == NULL)
3384 return NULL;
3385 inner = set_array_declarator_inner (declarator, inner);
3386 return c_parser_direct_declarator_inner (parser, id_present, inner);
3388 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3390 tree attrs;
3391 struct c_arg_info *args;
3392 c_parser_consume_token (parser);
3393 attrs = c_parser_attributes (parser);
3394 args = c_parser_parms_declarator (parser, id_present, attrs);
3395 if (args == NULL)
3396 return NULL;
3397 else
3399 inner = build_function_declarator (args, inner);
3400 return c_parser_direct_declarator_inner (parser, id_present, inner);
3403 return inner;
3406 /* Parse a parameter list or identifier list, including the closing
3407 parenthesis but not the opening one. ATTRS are the attributes at
3408 the start of the list. ID_LIST_OK is true if an identifier list is
3409 acceptable; such a list must not have attributes at the start. */
3411 static struct c_arg_info *
3412 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3414 push_scope ();
3415 declare_parm_level ();
3416 /* If the list starts with an identifier, it is an identifier list.
3417 Otherwise, it is either a prototype list or an empty list. */
3418 if (id_list_ok
3419 && !attrs
3420 && c_parser_next_token_is (parser, CPP_NAME)
3421 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3423 /* Look ahead to detect typos in type names. */
3424 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3425 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3426 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3427 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3429 tree list = NULL_TREE, *nextp = &list;
3430 while (c_parser_next_token_is (parser, CPP_NAME)
3431 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3433 *nextp = build_tree_list (NULL_TREE,
3434 c_parser_peek_token (parser)->value);
3435 nextp = & TREE_CHAIN (*nextp);
3436 c_parser_consume_token (parser);
3437 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3438 break;
3439 c_parser_consume_token (parser);
3440 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3442 c_parser_error (parser, "expected identifier");
3443 break;
3446 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3448 struct c_arg_info *ret = build_arg_info ();
3449 ret->types = list;
3450 c_parser_consume_token (parser);
3451 pop_scope ();
3452 return ret;
3454 else
3456 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3457 "expected %<)%>");
3458 pop_scope ();
3459 return NULL;
3462 else
3464 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3465 NULL);
3466 pop_scope ();
3467 return ret;
3471 /* Parse a parameter list (possibly empty), including the closing
3472 parenthesis but not the opening one. ATTRS are the attributes at
3473 the start of the list. EXPR is NULL or an expression that needs to
3474 be evaluated for the side effects of array size expressions in the
3475 parameters. */
3477 static struct c_arg_info *
3478 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3480 bool bad_parm = false;
3482 /* ??? Following the old parser, forward parameter declarations may
3483 use abstract declarators, and if no real parameter declarations
3484 follow the forward declarations then this is not diagnosed. Also
3485 note as above that attributes are ignored as the only contents of
3486 the parentheses, or as the only contents after forward
3487 declarations. */
3488 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3490 struct c_arg_info *ret = build_arg_info ();
3491 c_parser_consume_token (parser);
3492 return ret;
3494 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3496 struct c_arg_info *ret = build_arg_info ();
3498 if (flag_allow_parameterless_variadic_functions)
3500 /* F (...) is allowed. */
3501 ret->types = NULL_TREE;
3503 else
3505 /* Suppress -Wold-style-definition for this case. */
3506 ret->types = error_mark_node;
3507 error_at (c_parser_peek_token (parser)->location,
3508 "ISO C requires a named argument before %<...%>");
3510 c_parser_consume_token (parser);
3511 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3513 c_parser_consume_token (parser);
3514 return ret;
3516 else
3518 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3519 "expected %<)%>");
3520 return NULL;
3523 /* Nonempty list of parameters, either terminated with semicolon
3524 (forward declarations; recurse) or with close parenthesis (normal
3525 function) or with ", ... )" (variadic function). */
3526 while (true)
3528 /* Parse a parameter. */
3529 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3530 attrs = NULL_TREE;
3531 if (parm == NULL)
3532 bad_parm = true;
3533 else
3534 push_parm_decl (parm, &expr);
3535 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3537 tree new_attrs;
3538 c_parser_consume_token (parser);
3539 mark_forward_parm_decls ();
3540 new_attrs = c_parser_attributes (parser);
3541 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3543 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3545 c_parser_consume_token (parser);
3546 if (bad_parm)
3547 return NULL;
3548 else
3549 return get_parm_info (false, expr);
3551 if (!c_parser_require (parser, CPP_COMMA,
3552 "expected %<;%>, %<,%> or %<)%>"))
3554 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3555 return NULL;
3557 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3559 c_parser_consume_token (parser);
3560 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3562 c_parser_consume_token (parser);
3563 if (bad_parm)
3564 return NULL;
3565 else
3566 return get_parm_info (true, expr);
3568 else
3570 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3571 "expected %<)%>");
3572 return NULL;
3578 /* Parse a parameter declaration. ATTRS are the attributes at the
3579 start of the declaration if it is the first parameter. */
3581 static struct c_parm *
3582 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3584 struct c_declspecs *specs;
3585 struct c_declarator *declarator;
3586 tree prefix_attrs;
3587 tree postfix_attrs = NULL_TREE;
3588 bool dummy = false;
3590 /* Accept #pragmas between parameter declarations. */
3591 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3592 c_parser_pragma (parser, pragma_param);
3594 if (!c_parser_next_token_starts_declspecs (parser))
3596 c_token *token = c_parser_peek_token (parser);
3597 if (parser->error)
3598 return NULL;
3599 c_parser_set_source_position_from_token (token);
3600 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3602 error_at (token->location, "unknown type name %qE", token->value);
3603 parser->error = true;
3605 /* ??? In some Objective-C cases '...' isn't applicable so there
3606 should be a different message. */
3607 else
3608 c_parser_error (parser,
3609 "expected declaration specifiers or %<...%>");
3610 c_parser_skip_to_end_of_parameter (parser);
3611 return NULL;
3613 specs = build_null_declspecs ();
3614 if (attrs)
3616 declspecs_add_attrs (input_location, specs, attrs);
3617 attrs = NULL_TREE;
3619 c_parser_declspecs (parser, specs, true, true, true, true, false,
3620 cla_nonabstract_decl);
3621 finish_declspecs (specs);
3622 pending_xref_error ();
3623 prefix_attrs = specs->attrs;
3624 specs->attrs = NULL_TREE;
3625 declarator = c_parser_declarator (parser,
3626 specs->typespec_kind != ctsk_none,
3627 C_DTR_PARM, &dummy);
3628 if (declarator == NULL)
3630 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3631 return NULL;
3633 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3634 postfix_attrs = c_parser_attributes (parser);
3635 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3636 declarator);
3639 /* Parse a string literal in an asm expression. It should not be
3640 translated, and wide string literals are an error although
3641 permitted by the syntax. This is a GNU extension.
3643 asm-string-literal:
3644 string-literal
3646 ??? At present, following the old parser, the caller needs to have
3647 set lex_untranslated_string to 1. It would be better to follow the
3648 C++ parser rather than using this kludge. */
3650 static tree
3651 c_parser_asm_string_literal (c_parser *parser)
3653 tree str;
3654 int save_flag = warn_overlength_strings;
3655 warn_overlength_strings = 0;
3656 if (c_parser_next_token_is (parser, CPP_STRING))
3658 str = c_parser_peek_token (parser)->value;
3659 c_parser_consume_token (parser);
3661 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3663 error_at (c_parser_peek_token (parser)->location,
3664 "wide string literal in %<asm%>");
3665 str = build_string (1, "");
3666 c_parser_consume_token (parser);
3668 else
3670 c_parser_error (parser, "expected string literal");
3671 str = NULL_TREE;
3673 warn_overlength_strings = save_flag;
3674 return str;
3677 /* Parse a simple asm expression. This is used in restricted
3678 contexts, where a full expression with inputs and outputs does not
3679 make sense. This is a GNU extension.
3681 simple-asm-expr:
3682 asm ( asm-string-literal )
3685 static tree
3686 c_parser_simple_asm_expr (c_parser *parser)
3688 tree str;
3689 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3690 /* ??? Follow the C++ parser rather than using the
3691 lex_untranslated_string kludge. */
3692 parser->lex_untranslated_string = true;
3693 c_parser_consume_token (parser);
3694 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3696 parser->lex_untranslated_string = false;
3697 return NULL_TREE;
3699 str = c_parser_asm_string_literal (parser);
3700 parser->lex_untranslated_string = false;
3701 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3703 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3704 return NULL_TREE;
3706 return str;
3709 static tree
3710 c_parser_attribute_any_word (c_parser *parser)
3712 tree attr_name = NULL_TREE;
3714 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3716 /* ??? See comment above about what keywords are accepted here. */
3717 bool ok;
3718 switch (c_parser_peek_token (parser)->keyword)
3720 case RID_STATIC:
3721 case RID_UNSIGNED:
3722 case RID_LONG:
3723 case RID_INT128:
3724 case RID_CONST:
3725 case RID_EXTERN:
3726 case RID_REGISTER:
3727 case RID_TYPEDEF:
3728 case RID_SHORT:
3729 case RID_INLINE:
3730 case RID_NORETURN:
3731 case RID_VOLATILE:
3732 case RID_SIGNED:
3733 case RID_AUTO:
3734 case RID_RESTRICT:
3735 case RID_COMPLEX:
3736 case RID_THREAD:
3737 case RID_INT:
3738 case RID_CHAR:
3739 case RID_FLOAT:
3740 case RID_DOUBLE:
3741 case RID_VOID:
3742 case RID_DFLOAT32:
3743 case RID_DFLOAT64:
3744 case RID_DFLOAT128:
3745 case RID_BOOL:
3746 case RID_FRACT:
3747 case RID_ACCUM:
3748 case RID_SAT:
3749 case RID_TRANSACTION_ATOMIC:
3750 case RID_TRANSACTION_CANCEL:
3751 case RID_ATOMIC:
3752 case RID_AUTO_TYPE:
3753 ok = true;
3754 break;
3755 default:
3756 ok = false;
3757 break;
3759 if (!ok)
3760 return NULL_TREE;
3762 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3763 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3765 else if (c_parser_next_token_is (parser, CPP_NAME))
3766 attr_name = c_parser_peek_token (parser)->value;
3768 return attr_name;
3771 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3772 "__vector" or "__vector__." */
3774 static inline bool
3775 is_cilkplus_vector_p (tree name)
3777 if (flag_cilkplus && is_attribute_p ("vector", name))
3778 return true;
3779 return false;
3782 #define CILK_SIMD_FN_CLAUSE_MASK \
3783 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3784 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3785 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3786 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3787 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3789 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3790 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3791 pushed into the token list.
3792 Syntax:
3793 vector
3794 vector (<vector attributes>). */
3796 static void
3797 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3799 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3801 int paren_scope = 0;
3802 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3803 /* Consume the "vector" token. */
3804 c_parser_consume_token (parser);
3806 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3808 c_parser_consume_token (parser);
3809 paren_scope++;
3811 while (paren_scope > 0)
3813 c_token *token = c_parser_peek_token (parser);
3814 if (token->type == CPP_OPEN_PAREN)
3815 paren_scope++;
3816 else if (token->type == CPP_CLOSE_PAREN)
3817 paren_scope--;
3818 /* Do not push the last ')' since we are not pushing the '('. */
3819 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3820 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3821 c_parser_consume_token (parser);
3824 /* Since we are converting an attribute to a pragma, we need to end the
3825 attribute with PRAGMA_EOL. */
3826 c_token eol_token;
3827 memset (&eol_token, 0, sizeof (eol_token));
3828 eol_token.type = CPP_PRAGMA_EOL;
3829 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3832 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3834 static void
3835 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3837 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3839 /* c_parser_attributes is called in several places, so if these EOF
3840 tokens are already inserted, then don't do them again. */
3841 if (last_token.type == CPP_EOF)
3842 return;
3844 /* Two CPP_EOF token are added as a safety net since the normal C
3845 front-end has two token look-ahead. */
3846 c_token eof_token;
3847 eof_token.type = CPP_EOF;
3848 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3849 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3852 /* Parse (possibly empty) attributes. This is a GNU extension.
3854 attributes:
3855 empty
3856 attributes attribute
3858 attribute:
3859 __attribute__ ( ( attribute-list ) )
3861 attribute-list:
3862 attrib
3863 attribute_list , attrib
3865 attrib:
3866 empty
3867 any-word
3868 any-word ( identifier )
3869 any-word ( identifier , nonempty-expr-list )
3870 any-word ( expr-list )
3872 where the "identifier" must not be declared as a type, and
3873 "any-word" may be any identifier (including one declared as a
3874 type), a reserved word storage class specifier, type specifier or
3875 type qualifier. ??? This still leaves out most reserved keywords
3876 (following the old parser), shouldn't we include them, and why not
3877 allow identifiers declared as types to start the arguments? */
3879 static tree
3880 c_parser_attributes (c_parser *parser)
3882 tree attrs = NULL_TREE;
3883 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3885 /* ??? Follow the C++ parser rather than using the
3886 lex_untranslated_string kludge. */
3887 parser->lex_untranslated_string = true;
3888 c_parser_consume_token (parser);
3889 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3891 parser->lex_untranslated_string = false;
3892 return attrs;
3894 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3896 parser->lex_untranslated_string = false;
3897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3898 return attrs;
3900 /* Parse the attribute list. */
3901 while (c_parser_next_token_is (parser, CPP_COMMA)
3902 || c_parser_next_token_is (parser, CPP_NAME)
3903 || c_parser_next_token_is (parser, CPP_KEYWORD))
3905 tree attr, attr_name, attr_args;
3906 vec<tree, va_gc> *expr_list;
3907 if (c_parser_next_token_is (parser, CPP_COMMA))
3909 c_parser_consume_token (parser);
3910 continue;
3913 attr_name = c_parser_attribute_any_word (parser);
3914 if (attr_name == NULL)
3915 break;
3916 if (is_cilkplus_vector_p (attr_name))
3918 c_token *v_token = c_parser_peek_token (parser);
3919 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3920 continue;
3922 c_parser_consume_token (parser);
3923 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3925 attr = build_tree_list (attr_name, NULL_TREE);
3926 attrs = chainon (attrs, attr);
3927 continue;
3929 c_parser_consume_token (parser);
3930 /* Parse the attribute contents. If they start with an
3931 identifier which is followed by a comma or close
3932 parenthesis, then the arguments start with that
3933 identifier; otherwise they are an expression list.
3934 In objective-c the identifier may be a classname. */
3935 if (c_parser_next_token_is (parser, CPP_NAME)
3936 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3937 || (c_dialect_objc ()
3938 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3939 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3940 || (c_parser_peek_2nd_token (parser)->type
3941 == CPP_CLOSE_PAREN)))
3943 tree arg1 = c_parser_peek_token (parser)->value;
3944 c_parser_consume_token (parser);
3945 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3946 attr_args = build_tree_list (NULL_TREE, arg1);
3947 else
3949 tree tree_list;
3950 c_parser_consume_token (parser);
3951 expr_list = c_parser_expr_list (parser, false, true,
3952 NULL, NULL, NULL, NULL);
3953 tree_list = build_tree_list_vec (expr_list);
3954 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
3955 release_tree_vector (expr_list);
3958 else
3960 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3961 attr_args = NULL_TREE;
3962 else
3964 expr_list = c_parser_expr_list (parser, false, true,
3965 NULL, NULL, NULL, NULL);
3966 attr_args = build_tree_list_vec (expr_list);
3967 release_tree_vector (expr_list);
3970 attr = build_tree_list (attr_name, attr_args);
3971 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3972 c_parser_consume_token (parser);
3973 else
3975 parser->lex_untranslated_string = false;
3976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3977 "expected %<)%>");
3978 return attrs;
3980 attrs = chainon (attrs, attr);
3982 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3983 c_parser_consume_token (parser);
3984 else
3986 parser->lex_untranslated_string = false;
3987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3988 "expected %<)%>");
3989 return attrs;
3991 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3992 c_parser_consume_token (parser);
3993 else
3995 parser->lex_untranslated_string = false;
3996 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3997 "expected %<)%>");
3998 return attrs;
4000 parser->lex_untranslated_string = false;
4003 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4004 c_finish_cilk_simd_fn_tokens (parser);
4005 return attrs;
4008 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4010 type-name:
4011 specifier-qualifier-list abstract-declarator[opt]
4014 static struct c_type_name *
4015 c_parser_type_name (c_parser *parser)
4017 struct c_declspecs *specs = build_null_declspecs ();
4018 struct c_declarator *declarator;
4019 struct c_type_name *ret;
4020 bool dummy = false;
4021 c_parser_declspecs (parser, specs, false, true, true, false, false,
4022 cla_prefer_type);
4023 if (!specs->declspecs_seen_p)
4025 c_parser_error (parser, "expected specifier-qualifier-list");
4026 return NULL;
4028 if (specs->type != error_mark_node)
4030 pending_xref_error ();
4031 finish_declspecs (specs);
4033 declarator = c_parser_declarator (parser,
4034 specs->typespec_kind != ctsk_none,
4035 C_DTR_ABSTRACT, &dummy);
4036 if (declarator == NULL)
4037 return NULL;
4038 ret = XOBNEW (&parser_obstack, struct c_type_name);
4039 ret->specs = specs;
4040 ret->declarator = declarator;
4041 return ret;
4044 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4046 initializer:
4047 assignment-expression
4048 { initializer-list }
4049 { initializer-list , }
4051 initializer-list:
4052 designation[opt] initializer
4053 initializer-list , designation[opt] initializer
4055 designation:
4056 designator-list =
4058 designator-list:
4059 designator
4060 designator-list designator
4062 designator:
4063 array-designator
4064 . identifier
4066 array-designator:
4067 [ constant-expression ]
4069 GNU extensions:
4071 initializer:
4074 designation:
4075 array-designator
4076 identifier :
4078 array-designator:
4079 [ constant-expression ... constant-expression ]
4081 Any expression without commas is accepted in the syntax for the
4082 constant-expressions, with non-constant expressions rejected later.
4084 This function is only used for top-level initializers; for nested
4085 ones, see c_parser_initval. */
4087 static struct c_expr
4088 c_parser_initializer (c_parser *parser)
4090 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4091 return c_parser_braced_init (parser, NULL_TREE, false);
4092 else
4094 struct c_expr ret;
4095 location_t loc = c_parser_peek_token (parser)->location;
4096 ret = c_parser_expr_no_commas (parser, NULL);
4097 if (TREE_CODE (ret.value) != STRING_CST
4098 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4099 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4100 return ret;
4104 /* Parse a braced initializer list. TYPE is the type specified for a
4105 compound literal, and NULL_TREE for other initializers and for
4106 nested braced lists. NESTED_P is true for nested braced lists,
4107 false for the list of a compound literal or the list that is the
4108 top-level initializer in a declaration. */
4110 static struct c_expr
4111 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4113 struct c_expr ret;
4114 struct obstack braced_init_obstack;
4115 location_t brace_loc = c_parser_peek_token (parser)->location;
4116 gcc_obstack_init (&braced_init_obstack);
4117 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4118 c_parser_consume_token (parser);
4119 if (nested_p)
4120 push_init_level (0, &braced_init_obstack);
4121 else
4122 really_start_incremental_init (type);
4123 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4125 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4127 else
4129 /* Parse a non-empty initializer list, possibly with a trailing
4130 comma. */
4131 while (true)
4133 c_parser_initelt (parser, &braced_init_obstack);
4134 if (parser->error)
4135 break;
4136 if (c_parser_next_token_is (parser, CPP_COMMA))
4137 c_parser_consume_token (parser);
4138 else
4139 break;
4140 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4141 break;
4144 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4146 ret.value = error_mark_node;
4147 ret.original_code = ERROR_MARK;
4148 ret.original_type = NULL;
4149 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4150 pop_init_level (0, &braced_init_obstack);
4151 obstack_free (&braced_init_obstack, NULL);
4152 return ret;
4154 c_parser_consume_token (parser);
4155 ret = pop_init_level (0, &braced_init_obstack);
4156 obstack_free (&braced_init_obstack, NULL);
4157 return ret;
4160 /* Parse a nested initializer, including designators. */
4162 static void
4163 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4165 /* Parse any designator or designator list. A single array
4166 designator may have the subsequent "=" omitted in GNU C, but a
4167 longer list or a structure member designator may not. */
4168 if (c_parser_next_token_is (parser, CPP_NAME)
4169 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4171 /* Old-style structure member designator. */
4172 set_init_label (c_parser_peek_token (parser)->value,
4173 braced_init_obstack);
4174 /* Use the colon as the error location. */
4175 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4176 "obsolete use of designated initializer with %<:%>");
4177 c_parser_consume_token (parser);
4178 c_parser_consume_token (parser);
4180 else
4182 /* des_seen is 0 if there have been no designators, 1 if there
4183 has been a single array designator and 2 otherwise. */
4184 int des_seen = 0;
4185 /* Location of a designator. */
4186 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4187 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4188 || c_parser_next_token_is (parser, CPP_DOT))
4190 int des_prev = des_seen;
4191 if (!des_seen)
4192 des_loc = c_parser_peek_token (parser)->location;
4193 if (des_seen < 2)
4194 des_seen++;
4195 if (c_parser_next_token_is (parser, CPP_DOT))
4197 des_seen = 2;
4198 c_parser_consume_token (parser);
4199 if (c_parser_next_token_is (parser, CPP_NAME))
4201 set_init_label (c_parser_peek_token (parser)->value,
4202 braced_init_obstack);
4203 c_parser_consume_token (parser);
4205 else
4207 struct c_expr init;
4208 init.value = error_mark_node;
4209 init.original_code = ERROR_MARK;
4210 init.original_type = NULL;
4211 c_parser_error (parser, "expected identifier");
4212 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4213 process_init_element (input_location, init, false,
4214 braced_init_obstack);
4215 return;
4218 else
4220 tree first, second;
4221 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4222 /* ??? Following the old parser, [ objc-receiver
4223 objc-message-args ] is accepted as an initializer,
4224 being distinguished from a designator by what follows
4225 the first assignment expression inside the square
4226 brackets, but after a first array designator a
4227 subsequent square bracket is for Objective-C taken to
4228 start an expression, using the obsolete form of
4229 designated initializer without '=', rather than
4230 possibly being a second level of designation: in LALR
4231 terms, the '[' is shifted rather than reducing
4232 designator to designator-list. */
4233 if (des_prev == 1 && c_dialect_objc ())
4235 des_seen = des_prev;
4236 break;
4238 if (des_prev == 0 && c_dialect_objc ())
4240 /* This might be an array designator or an
4241 Objective-C message expression. If the former,
4242 continue parsing here; if the latter, parse the
4243 remainder of the initializer given the starting
4244 primary-expression. ??? It might make sense to
4245 distinguish when des_prev == 1 as well; see
4246 previous comment. */
4247 tree rec, args;
4248 struct c_expr mexpr;
4249 c_parser_consume_token (parser);
4250 if (c_parser_peek_token (parser)->type == CPP_NAME
4251 && ((c_parser_peek_token (parser)->id_kind
4252 == C_ID_TYPENAME)
4253 || (c_parser_peek_token (parser)->id_kind
4254 == C_ID_CLASSNAME)))
4256 /* Type name receiver. */
4257 tree id = c_parser_peek_token (parser)->value;
4258 c_parser_consume_token (parser);
4259 rec = objc_get_class_reference (id);
4260 goto parse_message_args;
4262 first = c_parser_expr_no_commas (parser, NULL).value;
4263 mark_exp_read (first);
4264 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4265 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4266 goto array_desig_after_first;
4267 /* Expression receiver. So far only one part
4268 without commas has been parsed; there might be
4269 more of the expression. */
4270 rec = first;
4271 while (c_parser_next_token_is (parser, CPP_COMMA))
4273 struct c_expr next;
4274 location_t comma_loc, exp_loc;
4275 comma_loc = c_parser_peek_token (parser)->location;
4276 c_parser_consume_token (parser);
4277 exp_loc = c_parser_peek_token (parser)->location;
4278 next = c_parser_expr_no_commas (parser, NULL);
4279 next = convert_lvalue_to_rvalue (exp_loc, next,
4280 true, true);
4281 rec = build_compound_expr (comma_loc, rec, next.value);
4283 parse_message_args:
4284 /* Now parse the objc-message-args. */
4285 args = c_parser_objc_message_args (parser);
4286 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4287 "expected %<]%>");
4288 mexpr.value
4289 = objc_build_message_expr (rec, args);
4290 mexpr.original_code = ERROR_MARK;
4291 mexpr.original_type = NULL;
4292 /* Now parse and process the remainder of the
4293 initializer, starting with this message
4294 expression as a primary-expression. */
4295 c_parser_initval (parser, &mexpr, braced_init_obstack);
4296 return;
4298 c_parser_consume_token (parser);
4299 first = c_parser_expr_no_commas (parser, NULL).value;
4300 mark_exp_read (first);
4301 array_desig_after_first:
4302 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4304 ellipsis_loc = c_parser_peek_token (parser)->location;
4305 c_parser_consume_token (parser);
4306 second = c_parser_expr_no_commas (parser, NULL).value;
4307 mark_exp_read (second);
4309 else
4310 second = NULL_TREE;
4311 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4313 c_parser_consume_token (parser);
4314 set_init_index (first, second, braced_init_obstack);
4315 if (second)
4316 pedwarn (ellipsis_loc, OPT_Wpedantic,
4317 "ISO C forbids specifying range of elements to initialize");
4319 else
4320 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4321 "expected %<]%>");
4324 if (des_seen >= 1)
4326 if (c_parser_next_token_is (parser, CPP_EQ))
4328 if (!flag_isoc99)
4329 pedwarn (des_loc, OPT_Wpedantic,
4330 "ISO C90 forbids specifying subobject to initialize");
4331 c_parser_consume_token (parser);
4333 else
4335 if (des_seen == 1)
4336 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4337 "obsolete use of designated initializer without %<=%>");
4338 else
4340 struct c_expr init;
4341 init.value = error_mark_node;
4342 init.original_code = ERROR_MARK;
4343 init.original_type = NULL;
4344 c_parser_error (parser, "expected %<=%>");
4345 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4346 process_init_element (input_location, init, false,
4347 braced_init_obstack);
4348 return;
4353 c_parser_initval (parser, NULL, braced_init_obstack);
4356 /* Parse a nested initializer; as c_parser_initializer but parses
4357 initializers within braced lists, after any designators have been
4358 applied. If AFTER is not NULL then it is an Objective-C message
4359 expression which is the primary-expression starting the
4360 initializer. */
4362 static void
4363 c_parser_initval (c_parser *parser, struct c_expr *after,
4364 struct obstack * braced_init_obstack)
4366 struct c_expr init;
4367 gcc_assert (!after || c_dialect_objc ());
4368 location_t loc = c_parser_peek_token (parser)->location;
4370 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4371 init = c_parser_braced_init (parser, NULL_TREE, true);
4372 else
4374 init = c_parser_expr_no_commas (parser, after);
4375 if (init.value != NULL_TREE
4376 && TREE_CODE (init.value) != STRING_CST
4377 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4378 init = convert_lvalue_to_rvalue (loc, init, true, true);
4380 process_init_element (loc, init, false, braced_init_obstack);
4383 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4384 C99 6.8.2).
4386 compound-statement:
4387 { block-item-list[opt] }
4388 { label-declarations block-item-list }
4390 block-item-list:
4391 block-item
4392 block-item-list block-item
4394 block-item:
4395 nested-declaration
4396 statement
4398 nested-declaration:
4399 declaration
4401 GNU extensions:
4403 compound-statement:
4404 { label-declarations block-item-list }
4406 nested-declaration:
4407 __extension__ nested-declaration
4408 nested-function-definition
4410 label-declarations:
4411 label-declaration
4412 label-declarations label-declaration
4414 label-declaration:
4415 __label__ identifier-list ;
4417 Allowing the mixing of declarations and code is new in C99. The
4418 GNU syntax also permits (not shown above) labels at the end of
4419 compound statements, which yield an error. We don't allow labels
4420 on declarations; this might seem like a natural extension, but
4421 there would be a conflict between attributes on the label and
4422 prefix attributes on the declaration. ??? The syntax follows the
4423 old parser in requiring something after label declarations.
4424 Although they are erroneous if the labels declared aren't defined,
4425 is it useful for the syntax to be this way?
4427 OpenMP:
4429 block-item:
4430 openmp-directive
4432 openmp-directive:
4433 barrier-directive
4434 flush-directive
4435 taskwait-directive
4436 taskyield-directive
4437 cancel-directive
4438 cancellation-point-directive */
4440 static tree
4441 c_parser_compound_statement (c_parser *parser)
4443 tree stmt;
4444 location_t brace_loc;
4445 brace_loc = c_parser_peek_token (parser)->location;
4446 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4448 /* Ensure a scope is entered and left anyway to avoid confusion
4449 if we have just prepared to enter a function body. */
4450 stmt = c_begin_compound_stmt (true);
4451 c_end_compound_stmt (brace_loc, stmt, true);
4452 return error_mark_node;
4454 stmt = c_begin_compound_stmt (true);
4455 c_parser_compound_statement_nostart (parser);
4457 /* If the compound stmt contains array notations, then we expand them. */
4458 if (flag_cilkplus && contains_array_notation_expr (stmt))
4459 stmt = expand_array_notation_exprs (stmt);
4460 return c_end_compound_stmt (brace_loc, stmt, true);
4463 /* Parse a compound statement except for the opening brace. This is
4464 used for parsing both compound statements and statement expressions
4465 (which follow different paths to handling the opening). */
4467 static void
4468 c_parser_compound_statement_nostart (c_parser *parser)
4470 bool last_stmt = false;
4471 bool last_label = false;
4472 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4473 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4474 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4476 c_parser_consume_token (parser);
4477 return;
4479 mark_valid_location_for_stdc_pragma (true);
4480 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4482 /* Read zero or more forward-declarations for labels that nested
4483 functions can jump to. */
4484 mark_valid_location_for_stdc_pragma (false);
4485 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4487 label_loc = c_parser_peek_token (parser)->location;
4488 c_parser_consume_token (parser);
4489 /* Any identifiers, including those declared as type names,
4490 are OK here. */
4491 while (true)
4493 tree label;
4494 if (c_parser_next_token_is_not (parser, CPP_NAME))
4496 c_parser_error (parser, "expected identifier");
4497 break;
4499 label
4500 = declare_label (c_parser_peek_token (parser)->value);
4501 C_DECLARED_LABEL_FLAG (label) = 1;
4502 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4503 c_parser_consume_token (parser);
4504 if (c_parser_next_token_is (parser, CPP_COMMA))
4505 c_parser_consume_token (parser);
4506 else
4507 break;
4509 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4511 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4513 /* We must now have at least one statement, label or declaration. */
4514 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4516 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4517 c_parser_error (parser, "expected declaration or statement");
4518 c_parser_consume_token (parser);
4519 return;
4521 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4523 location_t loc = c_parser_peek_token (parser)->location;
4524 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4525 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4526 || (c_parser_next_token_is (parser, CPP_NAME)
4527 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4529 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4530 label_loc = c_parser_peek_2nd_token (parser)->location;
4531 else
4532 label_loc = c_parser_peek_token (parser)->location;
4533 last_label = true;
4534 last_stmt = false;
4535 mark_valid_location_for_stdc_pragma (false);
4536 c_parser_label (parser);
4538 else if (!last_label
4539 && c_parser_next_tokens_start_declaration (parser))
4541 last_label = false;
4542 mark_valid_location_for_stdc_pragma (false);
4543 c_parser_declaration_or_fndef (parser, true, true, true, true,
4544 true, NULL, vNULL);
4545 if (last_stmt)
4546 pedwarn_c90 (loc,
4547 (pedantic && !flag_isoc99)
4548 ? OPT_Wpedantic
4549 : OPT_Wdeclaration_after_statement,
4550 "ISO C90 forbids mixed declarations and code");
4551 last_stmt = false;
4553 else if (!last_label
4554 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4556 /* __extension__ can start a declaration, but is also an
4557 unary operator that can start an expression. Consume all
4558 but the last of a possible series of __extension__ to
4559 determine which. */
4560 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4561 && (c_parser_peek_2nd_token (parser)->keyword
4562 == RID_EXTENSION))
4563 c_parser_consume_token (parser);
4564 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4566 int ext;
4567 ext = disable_extension_diagnostics ();
4568 c_parser_consume_token (parser);
4569 last_label = false;
4570 mark_valid_location_for_stdc_pragma (false);
4571 c_parser_declaration_or_fndef (parser, true, true, true, true,
4572 true, NULL, vNULL);
4573 /* Following the old parser, __extension__ does not
4574 disable this diagnostic. */
4575 restore_extension_diagnostics (ext);
4576 if (last_stmt)
4577 pedwarn_c90 (loc, (pedantic && !flag_isoc99)
4578 ? OPT_Wpedantic
4579 : OPT_Wdeclaration_after_statement,
4580 "ISO C90 forbids mixed declarations and code");
4581 last_stmt = false;
4583 else
4584 goto statement;
4586 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4588 /* External pragmas, and some omp pragmas, are not associated
4589 with regular c code, and so are not to be considered statements
4590 syntactically. This ensures that the user doesn't put them
4591 places that would turn into syntax errors if the directive
4592 were ignored. */
4593 if (c_parser_pragma (parser, pragma_compound))
4594 last_label = false, last_stmt = true;
4596 else if (c_parser_next_token_is (parser, CPP_EOF))
4598 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4599 c_parser_error (parser, "expected declaration or statement");
4600 return;
4602 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4604 if (parser->in_if_block)
4606 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4607 error_at (loc, """expected %<}%> before %<else%>");
4608 return;
4610 else
4612 error_at (loc, "%<else%> without a previous %<if%>");
4613 c_parser_consume_token (parser);
4614 continue;
4617 else
4619 statement:
4620 last_label = false;
4621 last_stmt = true;
4622 mark_valid_location_for_stdc_pragma (false);
4623 c_parser_statement_after_labels (parser);
4626 parser->error = false;
4628 if (last_label)
4629 error_at (label_loc, "label at end of compound statement");
4630 c_parser_consume_token (parser);
4631 /* Restore the value we started with. */
4632 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4635 /* Parse a label (C90 6.6.1, C99 6.8.1).
4637 label:
4638 identifier : attributes[opt]
4639 case constant-expression :
4640 default :
4642 GNU extensions:
4644 label:
4645 case constant-expression ... constant-expression :
4647 The use of attributes on labels is a GNU extension. The syntax in
4648 GNU C accepts any expressions without commas, non-constant
4649 expressions being rejected later. */
4651 static void
4652 c_parser_label (c_parser *parser)
4654 location_t loc1 = c_parser_peek_token (parser)->location;
4655 tree label = NULL_TREE;
4656 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4658 tree exp1, exp2;
4659 c_parser_consume_token (parser);
4660 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4661 if (c_parser_next_token_is (parser, CPP_COLON))
4663 c_parser_consume_token (parser);
4664 label = do_case (loc1, exp1, NULL_TREE);
4666 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4668 c_parser_consume_token (parser);
4669 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4670 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4671 label = do_case (loc1, exp1, exp2);
4673 else
4674 c_parser_error (parser, "expected %<:%> or %<...%>");
4676 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4678 c_parser_consume_token (parser);
4679 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4680 label = do_case (loc1, NULL_TREE, NULL_TREE);
4682 else
4684 tree name = c_parser_peek_token (parser)->value;
4685 tree tlab;
4686 tree attrs;
4687 location_t loc2 = c_parser_peek_token (parser)->location;
4688 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4689 c_parser_consume_token (parser);
4690 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4691 c_parser_consume_token (parser);
4692 attrs = c_parser_attributes (parser);
4693 tlab = define_label (loc2, name);
4694 if (tlab)
4696 decl_attributes (&tlab, attrs, 0);
4697 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4700 if (label)
4702 if (c_parser_next_tokens_start_declaration (parser))
4704 error_at (c_parser_peek_token (parser)->location,
4705 "a label can only be part of a statement and "
4706 "a declaration is not a statement");
4707 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4708 /*static_assert_ok*/ true,
4709 /*empty_ok*/ true, /*nested*/ true,
4710 /*start_attr_ok*/ true, NULL,
4711 vNULL);
4716 /* Parse a statement (C90 6.6, C99 6.8).
4718 statement:
4719 labeled-statement
4720 compound-statement
4721 expression-statement
4722 selection-statement
4723 iteration-statement
4724 jump-statement
4726 labeled-statement:
4727 label statement
4729 expression-statement:
4730 expression[opt] ;
4732 selection-statement:
4733 if-statement
4734 switch-statement
4736 iteration-statement:
4737 while-statement
4738 do-statement
4739 for-statement
4741 jump-statement:
4742 goto identifier ;
4743 continue ;
4744 break ;
4745 return expression[opt] ;
4747 GNU extensions:
4749 statement:
4750 asm-statement
4752 jump-statement:
4753 goto * expression ;
4755 Objective-C:
4757 statement:
4758 objc-throw-statement
4759 objc-try-catch-statement
4760 objc-synchronized-statement
4762 objc-throw-statement:
4763 @throw expression ;
4764 @throw ;
4766 OpenMP:
4768 statement:
4769 openmp-construct
4771 openmp-construct:
4772 parallel-construct
4773 for-construct
4774 simd-construct
4775 for-simd-construct
4776 sections-construct
4777 single-construct
4778 parallel-for-construct
4779 parallel-for-simd-construct
4780 parallel-sections-construct
4781 master-construct
4782 critical-construct
4783 atomic-construct
4784 ordered-construct
4786 parallel-construct:
4787 parallel-directive structured-block
4789 for-construct:
4790 for-directive iteration-statement
4792 simd-construct:
4793 simd-directive iteration-statements
4795 for-simd-construct:
4796 for-simd-directive iteration-statements
4798 sections-construct:
4799 sections-directive section-scope
4801 single-construct:
4802 single-directive structured-block
4804 parallel-for-construct:
4805 parallel-for-directive iteration-statement
4807 parallel-for-simd-construct:
4808 parallel-for-simd-directive iteration-statement
4810 parallel-sections-construct:
4811 parallel-sections-directive section-scope
4813 master-construct:
4814 master-directive structured-block
4816 critical-construct:
4817 critical-directive structured-block
4819 atomic-construct:
4820 atomic-directive expression-statement
4822 ordered-construct:
4823 ordered-directive structured-block
4825 Transactional Memory:
4827 statement:
4828 transaction-statement
4829 transaction-cancel-statement
4832 static void
4833 c_parser_statement (c_parser *parser)
4835 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4836 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4837 || (c_parser_next_token_is (parser, CPP_NAME)
4838 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4839 c_parser_label (parser);
4840 c_parser_statement_after_labels (parser);
4843 /* Parse a statement, other than a labeled statement. */
4845 static void
4846 c_parser_statement_after_labels (c_parser *parser)
4848 location_t loc = c_parser_peek_token (parser)->location;
4849 tree stmt = NULL_TREE;
4850 bool in_if_block = parser->in_if_block;
4851 parser->in_if_block = false;
4852 switch (c_parser_peek_token (parser)->type)
4854 case CPP_OPEN_BRACE:
4855 add_stmt (c_parser_compound_statement (parser));
4856 break;
4857 case CPP_KEYWORD:
4858 switch (c_parser_peek_token (parser)->keyword)
4860 case RID_IF:
4861 c_parser_if_statement (parser);
4862 break;
4863 case RID_SWITCH:
4864 c_parser_switch_statement (parser);
4865 break;
4866 case RID_WHILE:
4867 c_parser_while_statement (parser, false);
4868 break;
4869 case RID_DO:
4870 c_parser_do_statement (parser, false);
4871 break;
4872 case RID_FOR:
4873 c_parser_for_statement (parser, false);
4874 break;
4875 case RID_CILK_SYNC:
4876 c_parser_consume_token (parser);
4877 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4878 if (!flag_cilkplus)
4879 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4880 else
4881 add_stmt (build_cilk_sync ());
4882 break;
4883 case RID_GOTO:
4884 c_parser_consume_token (parser);
4885 if (c_parser_next_token_is (parser, CPP_NAME))
4887 stmt = c_finish_goto_label (loc,
4888 c_parser_peek_token (parser)->value);
4889 c_parser_consume_token (parser);
4891 else if (c_parser_next_token_is (parser, CPP_MULT))
4893 struct c_expr val;
4895 c_parser_consume_token (parser);
4896 val = c_parser_expression (parser);
4897 val = convert_lvalue_to_rvalue (loc, val, false, true);
4898 stmt = c_finish_goto_ptr (loc, val.value);
4900 else
4901 c_parser_error (parser, "expected identifier or %<*%>");
4902 goto expect_semicolon;
4903 case RID_CONTINUE:
4904 c_parser_consume_token (parser);
4905 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
4906 goto expect_semicolon;
4907 case RID_BREAK:
4908 c_parser_consume_token (parser);
4909 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
4910 goto expect_semicolon;
4911 case RID_RETURN:
4912 c_parser_consume_token (parser);
4913 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4915 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
4916 c_parser_consume_token (parser);
4918 else
4920 struct c_expr expr = c_parser_expression_conv (parser);
4921 mark_exp_read (expr.value);
4922 stmt = c_finish_return (loc, expr.value, expr.original_type);
4923 goto expect_semicolon;
4925 break;
4926 case RID_ASM:
4927 stmt = c_parser_asm_statement (parser);
4928 break;
4929 case RID_TRANSACTION_ATOMIC:
4930 case RID_TRANSACTION_RELAXED:
4931 stmt = c_parser_transaction (parser,
4932 c_parser_peek_token (parser)->keyword);
4933 break;
4934 case RID_TRANSACTION_CANCEL:
4935 stmt = c_parser_transaction_cancel (parser);
4936 goto expect_semicolon;
4937 case RID_AT_THROW:
4938 gcc_assert (c_dialect_objc ());
4939 c_parser_consume_token (parser);
4940 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4942 stmt = objc_build_throw_stmt (loc, NULL_TREE);
4943 c_parser_consume_token (parser);
4945 else
4947 struct c_expr expr = c_parser_expression (parser);
4948 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
4949 expr.value = c_fully_fold (expr.value, false, NULL);
4950 stmt = objc_build_throw_stmt (loc, expr.value);
4951 goto expect_semicolon;
4953 break;
4954 case RID_AT_TRY:
4955 gcc_assert (c_dialect_objc ());
4956 c_parser_objc_try_catch_finally_statement (parser);
4957 break;
4958 case RID_AT_SYNCHRONIZED:
4959 gcc_assert (c_dialect_objc ());
4960 c_parser_objc_synchronized_statement (parser);
4961 break;
4962 default:
4963 goto expr_stmt;
4965 break;
4966 case CPP_SEMICOLON:
4967 c_parser_consume_token (parser);
4968 break;
4969 case CPP_CLOSE_PAREN:
4970 case CPP_CLOSE_SQUARE:
4971 /* Avoid infinite loop in error recovery:
4972 c_parser_skip_until_found stops at a closing nesting
4973 delimiter without consuming it, but here we need to consume
4974 it to proceed further. */
4975 c_parser_error (parser, "expected statement");
4976 c_parser_consume_token (parser);
4977 break;
4978 case CPP_PRAGMA:
4979 c_parser_pragma (parser, pragma_stmt);
4980 break;
4981 default:
4982 expr_stmt:
4983 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
4984 expect_semicolon:
4985 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4986 break;
4988 /* Two cases cannot and do not have line numbers associated: If stmt
4989 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
4990 cannot hold line numbers. But that's OK because the statement
4991 will either be changed to a MODIFY_EXPR during gimplification of
4992 the statement expr, or discarded. If stmt was compound, but
4993 without new variables, we will have skipped the creation of a
4994 BIND and will have a bare STATEMENT_LIST. But that's OK because
4995 (recursively) all of the component statements should already have
4996 line numbers assigned. ??? Can we discard no-op statements
4997 earlier? */
4998 if (CAN_HAVE_LOCATION_P (stmt)
4999 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5000 SET_EXPR_LOCATION (stmt, loc);
5002 parser->in_if_block = in_if_block;
5005 /* Parse the condition from an if, do, while or for statements. */
5007 static tree
5008 c_parser_condition (c_parser *parser)
5010 location_t loc = c_parser_peek_token (parser)->location;
5011 tree cond;
5012 cond = c_parser_expression_conv (parser).value;
5013 cond = c_objc_common_truthvalue_conversion (loc, cond);
5014 cond = c_fully_fold (cond, false, NULL);
5015 if (warn_sequence_point)
5016 verify_sequence_points (cond);
5017 return cond;
5020 /* Parse a parenthesized condition from an if, do or while statement.
5022 condition:
5023 ( expression )
5025 static tree
5026 c_parser_paren_condition (c_parser *parser)
5028 tree cond;
5029 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5030 return error_mark_node;
5031 cond = c_parser_condition (parser);
5032 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5033 return cond;
5036 /* Parse a statement which is a block in C99. */
5038 static tree
5039 c_parser_c99_block_statement (c_parser *parser)
5041 tree block = c_begin_compound_stmt (flag_isoc99);
5042 location_t loc = c_parser_peek_token (parser)->location;
5043 c_parser_statement (parser);
5044 return c_end_compound_stmt (loc, block, flag_isoc99);
5047 /* Parse the body of an if statement. This is just parsing a
5048 statement but (a) it is a block in C99, (b) we track whether the
5049 body is an if statement for the sake of -Wparentheses warnings, (c)
5050 we handle an empty body specially for the sake of -Wempty-body
5051 warnings, and (d) we call parser_compound_statement directly
5052 because c_parser_statement_after_labels resets
5053 parser->in_if_block. */
5055 static tree
5056 c_parser_if_body (c_parser *parser, bool *if_p)
5058 tree block = c_begin_compound_stmt (flag_isoc99);
5059 location_t body_loc = c_parser_peek_token (parser)->location;
5060 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5061 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5062 || (c_parser_next_token_is (parser, CPP_NAME)
5063 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5064 c_parser_label (parser);
5065 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5066 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5068 location_t loc = c_parser_peek_token (parser)->location;
5069 add_stmt (build_empty_stmt (loc));
5070 c_parser_consume_token (parser);
5071 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5072 warning_at (loc, OPT_Wempty_body,
5073 "suggest braces around empty body in an %<if%> statement");
5075 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5076 add_stmt (c_parser_compound_statement (parser));
5077 else
5078 c_parser_statement_after_labels (parser);
5079 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5082 /* Parse the else body of an if statement. This is just parsing a
5083 statement but (a) it is a block in C99, (b) we handle an empty body
5084 specially for the sake of -Wempty-body warnings. */
5086 static tree
5087 c_parser_else_body (c_parser *parser)
5089 location_t else_loc = c_parser_peek_token (parser)->location;
5090 tree block = c_begin_compound_stmt (flag_isoc99);
5091 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5092 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5093 || (c_parser_next_token_is (parser, CPP_NAME)
5094 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5095 c_parser_label (parser);
5096 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5098 location_t loc = c_parser_peek_token (parser)->location;
5099 warning_at (loc,
5100 OPT_Wempty_body,
5101 "suggest braces around empty body in an %<else%> statement");
5102 add_stmt (build_empty_stmt (loc));
5103 c_parser_consume_token (parser);
5105 else
5106 c_parser_statement_after_labels (parser);
5107 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5110 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5112 if-statement:
5113 if ( expression ) statement
5114 if ( expression ) statement else statement
5117 static void
5118 c_parser_if_statement (c_parser *parser)
5120 tree block;
5121 location_t loc;
5122 tree cond;
5123 bool first_if = false;
5124 tree first_body, second_body;
5125 bool in_if_block;
5126 tree if_stmt;
5128 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5129 c_parser_consume_token (parser);
5130 block = c_begin_compound_stmt (flag_isoc99);
5131 loc = c_parser_peek_token (parser)->location;
5132 cond = c_parser_paren_condition (parser);
5133 in_if_block = parser->in_if_block;
5134 parser->in_if_block = true;
5135 first_body = c_parser_if_body (parser, &first_if);
5136 parser->in_if_block = in_if_block;
5137 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5139 c_parser_consume_token (parser);
5140 second_body = c_parser_else_body (parser);
5142 else
5143 second_body = NULL_TREE;
5144 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5145 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5147 /* If the if statement contains array notations, then we expand them. */
5148 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5149 if_stmt = fix_conditional_array_notations (if_stmt);
5150 add_stmt (if_stmt);
5153 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5155 switch-statement:
5156 switch (expression) statement
5159 static void
5160 c_parser_switch_statement (c_parser *parser)
5162 struct c_expr ce;
5163 tree block, expr, body, save_break;
5164 location_t switch_loc = c_parser_peek_token (parser)->location;
5165 location_t switch_cond_loc;
5166 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5167 c_parser_consume_token (parser);
5168 block = c_begin_compound_stmt (flag_isoc99);
5169 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5171 switch_cond_loc = c_parser_peek_token (parser)->location;
5172 ce = c_parser_expression (parser);
5173 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5174 expr = ce.value;
5175 if (flag_cilkplus && contains_array_notation_expr (expr))
5177 error_at (switch_cond_loc,
5178 "array notations cannot be used as a condition for switch "
5179 "statement");
5180 expr = error_mark_node;
5182 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5184 else
5186 switch_cond_loc = UNKNOWN_LOCATION;
5187 expr = error_mark_node;
5189 c_start_case (switch_loc, switch_cond_loc, expr);
5190 save_break = c_break_label;
5191 c_break_label = NULL_TREE;
5192 body = c_parser_c99_block_statement (parser);
5193 c_finish_case (body);
5194 if (c_break_label)
5196 location_t here = c_parser_peek_token (parser)->location;
5197 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5198 SET_EXPR_LOCATION (t, here);
5199 add_stmt (t);
5201 c_break_label = save_break;
5202 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5205 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5207 while-statement:
5208 while (expression) statement
5211 static void
5212 c_parser_while_statement (c_parser *parser, bool ivdep)
5214 tree block, cond, body, save_break, save_cont;
5215 location_t loc;
5216 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5217 c_parser_consume_token (parser);
5218 block = c_begin_compound_stmt (flag_isoc99);
5219 loc = c_parser_peek_token (parser)->location;
5220 cond = c_parser_paren_condition (parser);
5221 if (flag_cilkplus && contains_array_notation_expr (cond))
5223 error_at (loc, "array notations cannot be used as a condition for while "
5224 "statement");
5225 cond = error_mark_node;
5228 if (ivdep && cond != error_mark_node)
5229 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5230 build_int_cst (integer_type_node,
5231 annot_expr_ivdep_kind));
5232 save_break = c_break_label;
5233 c_break_label = NULL_TREE;
5234 save_cont = c_cont_label;
5235 c_cont_label = NULL_TREE;
5236 body = c_parser_c99_block_statement (parser);
5237 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5238 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5239 c_break_label = save_break;
5240 c_cont_label = save_cont;
5243 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5245 do-statement:
5246 do statement while ( expression ) ;
5249 static void
5250 c_parser_do_statement (c_parser *parser, bool ivdep)
5252 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5253 location_t loc;
5254 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5255 c_parser_consume_token (parser);
5256 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5257 warning_at (c_parser_peek_token (parser)->location,
5258 OPT_Wempty_body,
5259 "suggest braces around empty body in %<do%> statement");
5260 block = c_begin_compound_stmt (flag_isoc99);
5261 loc = c_parser_peek_token (parser)->location;
5262 save_break = c_break_label;
5263 c_break_label = NULL_TREE;
5264 save_cont = c_cont_label;
5265 c_cont_label = NULL_TREE;
5266 body = c_parser_c99_block_statement (parser);
5267 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5268 new_break = c_break_label;
5269 c_break_label = save_break;
5270 new_cont = c_cont_label;
5271 c_cont_label = save_cont;
5272 cond = c_parser_paren_condition (parser);
5273 if (flag_cilkplus && contains_array_notation_expr (cond))
5275 error_at (loc, "array notations cannot be used as a condition for a "
5276 "do-while statement");
5277 cond = error_mark_node;
5279 if (ivdep && cond != error_mark_node)
5280 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5281 build_int_cst (integer_type_node,
5282 annot_expr_ivdep_kind));
5283 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5284 c_parser_skip_to_end_of_block_or_statement (parser);
5285 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5286 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5289 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5291 for-statement:
5292 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5293 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5295 The form with a declaration is new in C99.
5297 ??? In accordance with the old parser, the declaration may be a
5298 nested function, which is then rejected in check_for_loop_decls,
5299 but does it make any sense for this to be included in the grammar?
5300 Note in particular that the nested function does not include a
5301 trailing ';', whereas the "declaration" production includes one.
5302 Also, can we reject bad declarations earlier and cheaper than
5303 check_for_loop_decls?
5305 In Objective-C, there are two additional variants:
5307 foreach-statement:
5308 for ( expression in expresssion ) statement
5309 for ( declaration in expression ) statement
5311 This is inconsistent with C, because the second variant is allowed
5312 even if c99 is not enabled.
5314 The rest of the comment documents these Objective-C foreach-statement.
5316 Here is the canonical example of the first variant:
5317 for (object in array) { do something with object }
5318 we call the first expression ("object") the "object_expression" and
5319 the second expression ("array") the "collection_expression".
5320 object_expression must be an lvalue of type "id" (a generic Objective-C
5321 object) because the loop works by assigning to object_expression the
5322 various objects from the collection_expression. collection_expression
5323 must evaluate to something of type "id" which responds to the method
5324 countByEnumeratingWithState:objects:count:.
5326 The canonical example of the second variant is:
5327 for (id object in array) { do something with object }
5328 which is completely equivalent to
5330 id object;
5331 for (object in array) { do something with object }
5333 Note that initizializing 'object' in some way (eg, "for ((object =
5334 xxx) in array) { do something with object }") is possibly
5335 technically valid, but completely pointless as 'object' will be
5336 assigned to something else as soon as the loop starts. We should
5337 most likely reject it (TODO).
5339 The beginning of the Objective-C foreach-statement looks exactly
5340 like the beginning of the for-statement, and we can tell it is a
5341 foreach-statement only because the initial declaration or
5342 expression is terminated by 'in' instead of ';'.
5345 static void
5346 c_parser_for_statement (c_parser *parser, bool ivdep)
5348 tree block, cond, incr, save_break, save_cont, body;
5349 /* The following are only used when parsing an ObjC foreach statement. */
5350 tree object_expression;
5351 /* Silence the bogus uninitialized warning. */
5352 tree collection_expression = NULL;
5353 location_t loc = c_parser_peek_token (parser)->location;
5354 location_t for_loc = c_parser_peek_token (parser)->location;
5355 bool is_foreach_statement = false;
5356 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5357 c_parser_consume_token (parser);
5358 /* Open a compound statement in Objective-C as well, just in case this is
5359 as foreach expression. */
5360 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5361 cond = error_mark_node;
5362 incr = error_mark_node;
5363 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5365 /* Parse the initialization declaration or expression. */
5366 object_expression = error_mark_node;
5367 parser->objc_could_be_foreach_context = c_dialect_objc ();
5368 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5370 parser->objc_could_be_foreach_context = false;
5371 c_parser_consume_token (parser);
5372 c_finish_expr_stmt (loc, NULL_TREE);
5374 else if (c_parser_next_tokens_start_declaration (parser))
5376 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5377 &object_expression, vNULL);
5378 parser->objc_could_be_foreach_context = false;
5380 if (c_parser_next_token_is_keyword (parser, RID_IN))
5382 c_parser_consume_token (parser);
5383 is_foreach_statement = true;
5384 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5385 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5387 else
5388 check_for_loop_decls (for_loc, flag_isoc99);
5390 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5392 /* __extension__ can start a declaration, but is also an
5393 unary operator that can start an expression. Consume all
5394 but the last of a possible series of __extension__ to
5395 determine which. */
5396 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5397 && (c_parser_peek_2nd_token (parser)->keyword
5398 == RID_EXTENSION))
5399 c_parser_consume_token (parser);
5400 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5402 int ext;
5403 ext = disable_extension_diagnostics ();
5404 c_parser_consume_token (parser);
5405 c_parser_declaration_or_fndef (parser, true, true, true, true,
5406 true, &object_expression, vNULL);
5407 parser->objc_could_be_foreach_context = false;
5409 restore_extension_diagnostics (ext);
5410 if (c_parser_next_token_is_keyword (parser, RID_IN))
5412 c_parser_consume_token (parser);
5413 is_foreach_statement = true;
5414 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5415 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5417 else
5418 check_for_loop_decls (for_loc, flag_isoc99);
5420 else
5421 goto init_expr;
5423 else
5425 init_expr:
5427 struct c_expr ce;
5428 tree init_expression;
5429 ce = c_parser_expression (parser);
5430 init_expression = ce.value;
5431 parser->objc_could_be_foreach_context = false;
5432 if (c_parser_next_token_is_keyword (parser, RID_IN))
5434 c_parser_consume_token (parser);
5435 is_foreach_statement = true;
5436 if (! lvalue_p (init_expression))
5437 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5438 object_expression = c_fully_fold (init_expression, false, NULL);
5440 else
5442 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5443 init_expression = ce.value;
5444 c_finish_expr_stmt (loc, init_expression);
5445 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5449 /* Parse the loop condition. In the case of a foreach
5450 statement, there is no loop condition. */
5451 gcc_assert (!parser->objc_could_be_foreach_context);
5452 if (!is_foreach_statement)
5454 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5456 if (ivdep)
5458 c_parser_error (parser, "missing loop condition in loop with "
5459 "%<GCC ivdep%> pragma");
5460 cond = error_mark_node;
5462 else
5464 c_parser_consume_token (parser);
5465 cond = NULL_TREE;
5468 else
5470 cond = c_parser_condition (parser);
5471 if (flag_cilkplus && contains_array_notation_expr (cond))
5473 error_at (loc, "array notations cannot be used in a "
5474 "condition for a for-loop");
5475 cond = error_mark_node;
5477 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5478 "expected %<;%>");
5480 if (ivdep && cond != error_mark_node)
5481 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5482 build_int_cst (integer_type_node,
5483 annot_expr_ivdep_kind));
5485 /* Parse the increment expression (the third expression in a
5486 for-statement). In the case of a foreach-statement, this is
5487 the expression that follows the 'in'. */
5488 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5490 if (is_foreach_statement)
5492 c_parser_error (parser, "missing collection in fast enumeration");
5493 collection_expression = error_mark_node;
5495 else
5496 incr = c_process_expr_stmt (loc, NULL_TREE);
5498 else
5500 if (is_foreach_statement)
5501 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5502 false, NULL);
5503 else
5505 struct c_expr ce = c_parser_expression (parser);
5506 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5507 incr = c_process_expr_stmt (loc, ce.value);
5510 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5512 save_break = c_break_label;
5513 c_break_label = NULL_TREE;
5514 save_cont = c_cont_label;
5515 c_cont_label = NULL_TREE;
5516 body = c_parser_c99_block_statement (parser);
5517 if (is_foreach_statement)
5518 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5519 else
5520 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5521 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5522 c_break_label = save_break;
5523 c_cont_label = save_cont;
5526 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5527 statement with inputs, outputs, clobbers, and volatile tag
5528 allowed.
5530 asm-statement:
5531 asm type-qualifier[opt] ( asm-argument ) ;
5532 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5534 asm-argument:
5535 asm-string-literal
5536 asm-string-literal : asm-operands[opt]
5537 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5538 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5540 asm-goto-argument:
5541 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5542 : asm-goto-operands
5544 Qualifiers other than volatile are accepted in the syntax but
5545 warned for. */
5547 static tree
5548 c_parser_asm_statement (c_parser *parser)
5550 tree quals, str, outputs, inputs, clobbers, labels, ret;
5551 bool simple, is_goto;
5552 location_t asm_loc = c_parser_peek_token (parser)->location;
5553 int section, nsections;
5555 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5556 c_parser_consume_token (parser);
5557 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5559 quals = c_parser_peek_token (parser)->value;
5560 c_parser_consume_token (parser);
5562 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5563 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5565 warning_at (c_parser_peek_token (parser)->location,
5567 "%E qualifier ignored on asm",
5568 c_parser_peek_token (parser)->value);
5569 quals = NULL_TREE;
5570 c_parser_consume_token (parser);
5572 else
5573 quals = NULL_TREE;
5575 is_goto = false;
5576 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5578 c_parser_consume_token (parser);
5579 is_goto = true;
5582 /* ??? Follow the C++ parser rather than using the
5583 lex_untranslated_string kludge. */
5584 parser->lex_untranslated_string = true;
5585 ret = NULL;
5587 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5588 goto error;
5590 str = c_parser_asm_string_literal (parser);
5591 if (str == NULL_TREE)
5592 goto error_close_paren;
5594 simple = true;
5595 outputs = NULL_TREE;
5596 inputs = NULL_TREE;
5597 clobbers = NULL_TREE;
5598 labels = NULL_TREE;
5600 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5601 goto done_asm;
5603 /* Parse each colon-delimited section of operands. */
5604 nsections = 3 + is_goto;
5605 for (section = 0; section < nsections; ++section)
5607 if (!c_parser_require (parser, CPP_COLON,
5608 is_goto
5609 ? "expected %<:%>"
5610 : "expected %<:%> or %<)%>"))
5611 goto error_close_paren;
5613 /* Once past any colon, we're no longer a simple asm. */
5614 simple = false;
5616 if ((!c_parser_next_token_is (parser, CPP_COLON)
5617 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5618 || section == 3)
5619 switch (section)
5621 case 0:
5622 /* For asm goto, we don't allow output operands, but reserve
5623 the slot for a future extension that does allow them. */
5624 if (!is_goto)
5625 outputs = c_parser_asm_operands (parser);
5626 break;
5627 case 1:
5628 inputs = c_parser_asm_operands (parser);
5629 break;
5630 case 2:
5631 clobbers = c_parser_asm_clobbers (parser);
5632 break;
5633 case 3:
5634 labels = c_parser_asm_goto_operands (parser);
5635 break;
5636 default:
5637 gcc_unreachable ();
5640 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5641 goto done_asm;
5644 done_asm:
5645 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5647 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5648 goto error;
5651 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5652 c_parser_skip_to_end_of_block_or_statement (parser);
5654 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5655 clobbers, labels, simple));
5657 error:
5658 parser->lex_untranslated_string = false;
5659 return ret;
5661 error_close_paren:
5662 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5663 goto error;
5666 /* Parse asm operands, a GNU extension.
5668 asm-operands:
5669 asm-operand
5670 asm-operands , asm-operand
5672 asm-operand:
5673 asm-string-literal ( expression )
5674 [ identifier ] asm-string-literal ( expression )
5677 static tree
5678 c_parser_asm_operands (c_parser *parser)
5680 tree list = NULL_TREE;
5681 while (true)
5683 tree name, str;
5684 struct c_expr expr;
5685 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5687 c_parser_consume_token (parser);
5688 if (c_parser_next_token_is (parser, CPP_NAME))
5690 tree id = c_parser_peek_token (parser)->value;
5691 c_parser_consume_token (parser);
5692 name = build_string (IDENTIFIER_LENGTH (id),
5693 IDENTIFIER_POINTER (id));
5695 else
5697 c_parser_error (parser, "expected identifier");
5698 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5699 return NULL_TREE;
5701 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5702 "expected %<]%>");
5704 else
5705 name = NULL_TREE;
5706 str = c_parser_asm_string_literal (parser);
5707 if (str == NULL_TREE)
5708 return NULL_TREE;
5709 parser->lex_untranslated_string = false;
5710 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5712 parser->lex_untranslated_string = true;
5713 return NULL_TREE;
5715 expr = c_parser_expression (parser);
5716 mark_exp_read (expr.value);
5717 parser->lex_untranslated_string = true;
5718 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5720 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5721 return NULL_TREE;
5723 list = chainon (list, build_tree_list (build_tree_list (name, str),
5724 expr.value));
5725 if (c_parser_next_token_is (parser, CPP_COMMA))
5726 c_parser_consume_token (parser);
5727 else
5728 break;
5730 return list;
5733 /* Parse asm clobbers, a GNU extension.
5735 asm-clobbers:
5736 asm-string-literal
5737 asm-clobbers , asm-string-literal
5740 static tree
5741 c_parser_asm_clobbers (c_parser *parser)
5743 tree list = NULL_TREE;
5744 while (true)
5746 tree str = c_parser_asm_string_literal (parser);
5747 if (str)
5748 list = tree_cons (NULL_TREE, str, list);
5749 else
5750 return NULL_TREE;
5751 if (c_parser_next_token_is (parser, CPP_COMMA))
5752 c_parser_consume_token (parser);
5753 else
5754 break;
5756 return list;
5759 /* Parse asm goto labels, a GNU extension.
5761 asm-goto-operands:
5762 identifier
5763 asm-goto-operands , identifier
5766 static tree
5767 c_parser_asm_goto_operands (c_parser *parser)
5769 tree list = NULL_TREE;
5770 while (true)
5772 tree name, label;
5774 if (c_parser_next_token_is (parser, CPP_NAME))
5776 c_token *tok = c_parser_peek_token (parser);
5777 name = tok->value;
5778 label = lookup_label_for_goto (tok->location, name);
5779 c_parser_consume_token (parser);
5780 TREE_USED (label) = 1;
5782 else
5784 c_parser_error (parser, "expected identifier");
5785 return NULL_TREE;
5788 name = build_string (IDENTIFIER_LENGTH (name),
5789 IDENTIFIER_POINTER (name));
5790 list = tree_cons (name, label, list);
5791 if (c_parser_next_token_is (parser, CPP_COMMA))
5792 c_parser_consume_token (parser);
5793 else
5794 return nreverse (list);
5798 /* Parse an expression other than a compound expression; that is, an
5799 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5800 NULL then it is an Objective-C message expression which is the
5801 primary-expression starting the expression as an initializer.
5803 assignment-expression:
5804 conditional-expression
5805 unary-expression assignment-operator assignment-expression
5807 assignment-operator: one of
5808 = *= /= %= += -= <<= >>= &= ^= |=
5810 In GNU C we accept any conditional expression on the LHS and
5811 diagnose the invalid lvalue rather than producing a syntax
5812 error. */
5814 static struct c_expr
5815 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5816 tree omp_atomic_lhs)
5818 struct c_expr lhs, rhs, ret;
5819 enum tree_code code;
5820 location_t op_location, exp_location;
5821 gcc_assert (!after || c_dialect_objc ());
5822 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5823 op_location = c_parser_peek_token (parser)->location;
5824 switch (c_parser_peek_token (parser)->type)
5826 case CPP_EQ:
5827 code = NOP_EXPR;
5828 break;
5829 case CPP_MULT_EQ:
5830 code = MULT_EXPR;
5831 break;
5832 case CPP_DIV_EQ:
5833 code = TRUNC_DIV_EXPR;
5834 break;
5835 case CPP_MOD_EQ:
5836 code = TRUNC_MOD_EXPR;
5837 break;
5838 case CPP_PLUS_EQ:
5839 code = PLUS_EXPR;
5840 break;
5841 case CPP_MINUS_EQ:
5842 code = MINUS_EXPR;
5843 break;
5844 case CPP_LSHIFT_EQ:
5845 code = LSHIFT_EXPR;
5846 break;
5847 case CPP_RSHIFT_EQ:
5848 code = RSHIFT_EXPR;
5849 break;
5850 case CPP_AND_EQ:
5851 code = BIT_AND_EXPR;
5852 break;
5853 case CPP_XOR_EQ:
5854 code = BIT_XOR_EXPR;
5855 break;
5856 case CPP_OR_EQ:
5857 code = BIT_IOR_EXPR;
5858 break;
5859 default:
5860 return lhs;
5862 c_parser_consume_token (parser);
5863 exp_location = c_parser_peek_token (parser)->location;
5864 rhs = c_parser_expr_no_commas (parser, NULL);
5865 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5867 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5868 code, exp_location, rhs.value,
5869 rhs.original_type);
5870 if (code == NOP_EXPR)
5871 ret.original_code = MODIFY_EXPR;
5872 else
5874 TREE_NO_WARNING (ret.value) = 1;
5875 ret.original_code = ERROR_MARK;
5877 ret.original_type = NULL;
5878 return ret;
5881 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5882 is not NULL then it is an Objective-C message expression which is
5883 the primary-expression starting the expression as an initializer.
5885 conditional-expression:
5886 logical-OR-expression
5887 logical-OR-expression ? expression : conditional-expression
5889 GNU extensions:
5891 conditional-expression:
5892 logical-OR-expression ? : conditional-expression
5895 static struct c_expr
5896 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
5897 tree omp_atomic_lhs)
5899 struct c_expr cond, exp1, exp2, ret;
5900 location_t cond_loc, colon_loc, middle_loc;
5902 gcc_assert (!after || c_dialect_objc ());
5904 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
5906 if (c_parser_next_token_is_not (parser, CPP_QUERY))
5907 return cond;
5908 cond_loc = c_parser_peek_token (parser)->location;
5909 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
5910 c_parser_consume_token (parser);
5911 if (c_parser_next_token_is (parser, CPP_COLON))
5913 tree eptype = NULL_TREE;
5915 middle_loc = c_parser_peek_token (parser)->location;
5916 pedwarn (middle_loc, OPT_Wpedantic,
5917 "ISO C forbids omitting the middle term of a ?: expression");
5918 warn_for_omitted_condop (middle_loc, cond.value);
5919 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
5921 eptype = TREE_TYPE (cond.value);
5922 cond.value = TREE_OPERAND (cond.value, 0);
5924 /* Make sure first operand is calculated only once. */
5925 exp1.value = c_save_expr (default_conversion (cond.value));
5926 if (eptype)
5927 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
5928 exp1.original_type = NULL;
5929 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
5930 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
5932 else
5934 cond.value
5935 = c_objc_common_truthvalue_conversion
5936 (cond_loc, default_conversion (cond.value));
5937 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
5938 exp1 = c_parser_expression_conv (parser);
5939 mark_exp_read (exp1.value);
5940 c_inhibit_evaluation_warnings +=
5941 ((cond.value == truthvalue_true_node)
5942 - (cond.value == truthvalue_false_node));
5945 colon_loc = c_parser_peek_token (parser)->location;
5946 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5948 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5949 ret.value = error_mark_node;
5950 ret.original_code = ERROR_MARK;
5951 ret.original_type = NULL;
5952 return ret;
5955 location_t exp2_loc = c_parser_peek_token (parser)->location;
5956 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
5957 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
5959 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
5960 ret.value = build_conditional_expr (colon_loc, cond.value,
5961 cond.original_code == C_MAYBE_CONST_EXPR,
5962 exp1.value, exp1.original_type,
5963 exp2.value, exp2.original_type);
5964 ret.original_code = ERROR_MARK;
5965 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
5966 ret.original_type = NULL;
5967 else
5969 tree t1, t2;
5971 /* If both sides are enum type, the default conversion will have
5972 made the type of the result be an integer type. We want to
5973 remember the enum types we started with. */
5974 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
5975 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
5976 ret.original_type = ((t1 != error_mark_node
5977 && t2 != error_mark_node
5978 && (TYPE_MAIN_VARIANT (t1)
5979 == TYPE_MAIN_VARIANT (t2)))
5980 ? t1
5981 : NULL);
5983 return ret;
5986 /* Parse a binary expression; that is, a logical-OR-expression (C90
5987 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
5988 an Objective-C message expression which is the primary-expression
5989 starting the expression as an initializer.
5991 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
5992 when it should be the unfolded lhs. In a valid OpenMP source,
5993 one of the operands of the toplevel binary expression must be equal
5994 to it. In that case, just return a build2 created binary operation
5995 rather than result of parser_build_binary_op.
5997 multiplicative-expression:
5998 cast-expression
5999 multiplicative-expression * cast-expression
6000 multiplicative-expression / cast-expression
6001 multiplicative-expression % cast-expression
6003 additive-expression:
6004 multiplicative-expression
6005 additive-expression + multiplicative-expression
6006 additive-expression - multiplicative-expression
6008 shift-expression:
6009 additive-expression
6010 shift-expression << additive-expression
6011 shift-expression >> additive-expression
6013 relational-expression:
6014 shift-expression
6015 relational-expression < shift-expression
6016 relational-expression > shift-expression
6017 relational-expression <= shift-expression
6018 relational-expression >= shift-expression
6020 equality-expression:
6021 relational-expression
6022 equality-expression == relational-expression
6023 equality-expression != relational-expression
6025 AND-expression:
6026 equality-expression
6027 AND-expression & equality-expression
6029 exclusive-OR-expression:
6030 AND-expression
6031 exclusive-OR-expression ^ AND-expression
6033 inclusive-OR-expression:
6034 exclusive-OR-expression
6035 inclusive-OR-expression | exclusive-OR-expression
6037 logical-AND-expression:
6038 inclusive-OR-expression
6039 logical-AND-expression && inclusive-OR-expression
6041 logical-OR-expression:
6042 logical-AND-expression
6043 logical-OR-expression || logical-AND-expression
6046 static struct c_expr
6047 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6048 tree omp_atomic_lhs)
6050 /* A binary expression is parsed using operator-precedence parsing,
6051 with the operands being cast expressions. All the binary
6052 operators are left-associative. Thus a binary expression is of
6053 form:
6055 E0 op1 E1 op2 E2 ...
6057 which we represent on a stack. On the stack, the precedence
6058 levels are strictly increasing. When a new operator is
6059 encountered of higher precedence than that at the top of the
6060 stack, it is pushed; its LHS is the top expression, and its RHS
6061 is everything parsed until it is popped. When a new operator is
6062 encountered with precedence less than or equal to that at the top
6063 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6064 by the result of the operation until the operator at the top of
6065 the stack has lower precedence than the new operator or there is
6066 only one element on the stack; then the top expression is the LHS
6067 of the new operator. In the case of logical AND and OR
6068 expressions, we also need to adjust c_inhibit_evaluation_warnings
6069 as appropriate when the operators are pushed and popped. */
6071 struct {
6072 /* The expression at this stack level. */
6073 struct c_expr expr;
6074 /* The precedence of the operator on its left, PREC_NONE at the
6075 bottom of the stack. */
6076 enum c_parser_prec prec;
6077 /* The operation on its left. */
6078 enum tree_code op;
6079 /* The source location of this operation. */
6080 location_t loc;
6081 } stack[NUM_PRECS];
6082 int sp;
6083 /* Location of the binary operator. */
6084 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6085 #define POP \
6086 do { \
6087 switch (stack[sp].op) \
6089 case TRUTH_ANDIF_EXPR: \
6090 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6091 == truthvalue_false_node); \
6092 break; \
6093 case TRUTH_ORIF_EXPR: \
6094 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6095 == truthvalue_true_node); \
6096 break; \
6097 default: \
6098 break; \
6100 stack[sp - 1].expr \
6101 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6102 stack[sp - 1].expr, true, true); \
6103 stack[sp].expr \
6104 = convert_lvalue_to_rvalue (stack[sp].loc, \
6105 stack[sp].expr, true, true); \
6106 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6107 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6108 && ((1 << stack[sp].prec) \
6109 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6110 | PREC_ADD | PREC_MULT))) \
6111 && stack[sp].op != TRUNC_MOD_EXPR \
6112 && stack[0].expr.value != error_mark_node \
6113 && stack[1].expr.value != error_mark_node \
6114 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6115 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6116 stack[0].expr.value \
6117 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6118 stack[0].expr.value, stack[1].expr.value); \
6119 else \
6120 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6121 stack[sp].op, \
6122 stack[sp - 1].expr, \
6123 stack[sp].expr); \
6124 sp--; \
6125 } while (0)
6126 gcc_assert (!after || c_dialect_objc ());
6127 stack[0].loc = c_parser_peek_token (parser)->location;
6128 stack[0].expr = c_parser_cast_expression (parser, after);
6129 stack[0].prec = PREC_NONE;
6130 sp = 0;
6131 while (true)
6133 enum c_parser_prec oprec;
6134 enum tree_code ocode;
6135 if (parser->error)
6136 goto out;
6137 switch (c_parser_peek_token (parser)->type)
6139 case CPP_MULT:
6140 oprec = PREC_MULT;
6141 ocode = MULT_EXPR;
6142 break;
6143 case CPP_DIV:
6144 oprec = PREC_MULT;
6145 ocode = TRUNC_DIV_EXPR;
6146 break;
6147 case CPP_MOD:
6148 oprec = PREC_MULT;
6149 ocode = TRUNC_MOD_EXPR;
6150 break;
6151 case CPP_PLUS:
6152 oprec = PREC_ADD;
6153 ocode = PLUS_EXPR;
6154 break;
6155 case CPP_MINUS:
6156 oprec = PREC_ADD;
6157 ocode = MINUS_EXPR;
6158 break;
6159 case CPP_LSHIFT:
6160 oprec = PREC_SHIFT;
6161 ocode = LSHIFT_EXPR;
6162 break;
6163 case CPP_RSHIFT:
6164 oprec = PREC_SHIFT;
6165 ocode = RSHIFT_EXPR;
6166 break;
6167 case CPP_LESS:
6168 oprec = PREC_REL;
6169 ocode = LT_EXPR;
6170 break;
6171 case CPP_GREATER:
6172 oprec = PREC_REL;
6173 ocode = GT_EXPR;
6174 break;
6175 case CPP_LESS_EQ:
6176 oprec = PREC_REL;
6177 ocode = LE_EXPR;
6178 break;
6179 case CPP_GREATER_EQ:
6180 oprec = PREC_REL;
6181 ocode = GE_EXPR;
6182 break;
6183 case CPP_EQ_EQ:
6184 oprec = PREC_EQ;
6185 ocode = EQ_EXPR;
6186 break;
6187 case CPP_NOT_EQ:
6188 oprec = PREC_EQ;
6189 ocode = NE_EXPR;
6190 break;
6191 case CPP_AND:
6192 oprec = PREC_BITAND;
6193 ocode = BIT_AND_EXPR;
6194 break;
6195 case CPP_XOR:
6196 oprec = PREC_BITXOR;
6197 ocode = BIT_XOR_EXPR;
6198 break;
6199 case CPP_OR:
6200 oprec = PREC_BITOR;
6201 ocode = BIT_IOR_EXPR;
6202 break;
6203 case CPP_AND_AND:
6204 oprec = PREC_LOGAND;
6205 ocode = TRUTH_ANDIF_EXPR;
6206 break;
6207 case CPP_OR_OR:
6208 oprec = PREC_LOGOR;
6209 ocode = TRUTH_ORIF_EXPR;
6210 break;
6211 default:
6212 /* Not a binary operator, so end of the binary
6213 expression. */
6214 goto out;
6216 binary_loc = c_parser_peek_token (parser)->location;
6217 while (oprec <= stack[sp].prec)
6218 POP;
6219 c_parser_consume_token (parser);
6220 switch (ocode)
6222 case TRUTH_ANDIF_EXPR:
6223 stack[sp].expr
6224 = convert_lvalue_to_rvalue (stack[sp].loc,
6225 stack[sp].expr, true, true);
6226 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6227 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6228 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6229 == truthvalue_false_node);
6230 break;
6231 case TRUTH_ORIF_EXPR:
6232 stack[sp].expr
6233 = convert_lvalue_to_rvalue (stack[sp].loc,
6234 stack[sp].expr, true, true);
6235 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6236 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6237 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6238 == truthvalue_true_node);
6239 break;
6240 default:
6241 break;
6243 sp++;
6244 stack[sp].loc = binary_loc;
6245 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6246 stack[sp].prec = oprec;
6247 stack[sp].op = ocode;
6248 stack[sp].loc = binary_loc;
6250 out:
6251 while (sp > 0)
6252 POP;
6253 return stack[0].expr;
6254 #undef POP
6257 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6258 NULL then it is an Objective-C message expression which is the
6259 primary-expression starting the expression as an initializer.
6261 cast-expression:
6262 unary-expression
6263 ( type-name ) unary-expression
6266 static struct c_expr
6267 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6269 location_t cast_loc = c_parser_peek_token (parser)->location;
6270 gcc_assert (!after || c_dialect_objc ());
6271 if (after)
6272 return c_parser_postfix_expression_after_primary (parser,
6273 cast_loc, *after);
6274 /* If the expression begins with a parenthesized type name, it may
6275 be either a cast or a compound literal; we need to see whether
6276 the next character is '{' to tell the difference. If not, it is
6277 an unary expression. Full detection of unknown typenames here
6278 would require a 3-token lookahead. */
6279 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6280 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6282 struct c_type_name *type_name;
6283 struct c_expr ret;
6284 struct c_expr expr;
6285 c_parser_consume_token (parser);
6286 type_name = c_parser_type_name (parser);
6287 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6288 if (type_name == NULL)
6290 ret.value = error_mark_node;
6291 ret.original_code = ERROR_MARK;
6292 ret.original_type = NULL;
6293 return ret;
6296 /* Save casted types in the function's used types hash table. */
6297 used_types_insert (type_name->specs->type);
6299 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6300 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6301 cast_loc);
6303 location_t expr_loc = c_parser_peek_token (parser)->location;
6304 expr = c_parser_cast_expression (parser, NULL);
6305 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6307 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6308 ret.original_code = ERROR_MARK;
6309 ret.original_type = NULL;
6310 return ret;
6312 else
6313 return c_parser_unary_expression (parser);
6316 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6318 unary-expression:
6319 postfix-expression
6320 ++ unary-expression
6321 -- unary-expression
6322 unary-operator cast-expression
6323 sizeof unary-expression
6324 sizeof ( type-name )
6326 unary-operator: one of
6327 & * + - ~ !
6329 GNU extensions:
6331 unary-expression:
6332 __alignof__ unary-expression
6333 __alignof__ ( type-name )
6334 && identifier
6336 (C11 permits _Alignof with type names only.)
6338 unary-operator: one of
6339 __extension__ __real__ __imag__
6341 Transactional Memory:
6343 unary-expression:
6344 transaction-expression
6346 In addition, the GNU syntax treats ++ and -- as unary operators, so
6347 they may be applied to cast expressions with errors for non-lvalues
6348 given later. */
6350 static struct c_expr
6351 c_parser_unary_expression (c_parser *parser)
6353 int ext;
6354 struct c_expr ret, op;
6355 location_t op_loc = c_parser_peek_token (parser)->location;
6356 location_t exp_loc;
6357 ret.original_code = ERROR_MARK;
6358 ret.original_type = NULL;
6359 switch (c_parser_peek_token (parser)->type)
6361 case CPP_PLUS_PLUS:
6362 c_parser_consume_token (parser);
6363 exp_loc = c_parser_peek_token (parser)->location;
6364 op = c_parser_cast_expression (parser, NULL);
6366 /* If there is array notations in op, we expand them. */
6367 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6368 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6369 else
6371 op = default_function_array_read_conversion (exp_loc, op);
6372 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6374 case CPP_MINUS_MINUS:
6375 c_parser_consume_token (parser);
6376 exp_loc = c_parser_peek_token (parser)->location;
6377 op = c_parser_cast_expression (parser, NULL);
6379 /* If there is array notations in op, we expand them. */
6380 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6381 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6382 else
6384 op = default_function_array_read_conversion (exp_loc, op);
6385 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6387 case CPP_AND:
6388 c_parser_consume_token (parser);
6389 op = c_parser_cast_expression (parser, NULL);
6390 mark_exp_read (op.value);
6391 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6392 case CPP_MULT:
6393 c_parser_consume_token (parser);
6394 exp_loc = c_parser_peek_token (parser)->location;
6395 op = c_parser_cast_expression (parser, NULL);
6396 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6397 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6398 return ret;
6399 case CPP_PLUS:
6400 if (!c_dialect_objc () && !in_system_header_at (input_location))
6401 warning_at (op_loc,
6402 OPT_Wtraditional,
6403 "traditional C rejects the unary plus operator");
6404 c_parser_consume_token (parser);
6405 exp_loc = c_parser_peek_token (parser)->location;
6406 op = c_parser_cast_expression (parser, NULL);
6407 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6408 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6409 case CPP_MINUS:
6410 c_parser_consume_token (parser);
6411 exp_loc = c_parser_peek_token (parser)->location;
6412 op = c_parser_cast_expression (parser, NULL);
6413 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6414 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6415 case CPP_COMPL:
6416 c_parser_consume_token (parser);
6417 exp_loc = c_parser_peek_token (parser)->location;
6418 op = c_parser_cast_expression (parser, NULL);
6419 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6420 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6421 case CPP_NOT:
6422 c_parser_consume_token (parser);
6423 exp_loc = c_parser_peek_token (parser)->location;
6424 op = c_parser_cast_expression (parser, NULL);
6425 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6426 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6427 case CPP_AND_AND:
6428 /* Refer to the address of a label as a pointer. */
6429 c_parser_consume_token (parser);
6430 if (c_parser_next_token_is (parser, CPP_NAME))
6432 ret.value = finish_label_address_expr
6433 (c_parser_peek_token (parser)->value, op_loc);
6434 c_parser_consume_token (parser);
6436 else
6438 c_parser_error (parser, "expected identifier");
6439 ret.value = error_mark_node;
6441 return ret;
6442 case CPP_KEYWORD:
6443 switch (c_parser_peek_token (parser)->keyword)
6445 case RID_SIZEOF:
6446 return c_parser_sizeof_expression (parser);
6447 case RID_ALIGNOF:
6448 return c_parser_alignof_expression (parser);
6449 case RID_EXTENSION:
6450 c_parser_consume_token (parser);
6451 ext = disable_extension_diagnostics ();
6452 ret = c_parser_cast_expression (parser, NULL);
6453 restore_extension_diagnostics (ext);
6454 return ret;
6455 case RID_REALPART:
6456 c_parser_consume_token (parser);
6457 exp_loc = c_parser_peek_token (parser)->location;
6458 op = c_parser_cast_expression (parser, NULL);
6459 op = default_function_array_conversion (exp_loc, op);
6460 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6461 case RID_IMAGPART:
6462 c_parser_consume_token (parser);
6463 exp_loc = c_parser_peek_token (parser)->location;
6464 op = c_parser_cast_expression (parser, NULL);
6465 op = default_function_array_conversion (exp_loc, op);
6466 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6467 case RID_TRANSACTION_ATOMIC:
6468 case RID_TRANSACTION_RELAXED:
6469 return c_parser_transaction_expression (parser,
6470 c_parser_peek_token (parser)->keyword);
6471 default:
6472 return c_parser_postfix_expression (parser);
6474 default:
6475 return c_parser_postfix_expression (parser);
6479 /* Parse a sizeof expression. */
6481 static struct c_expr
6482 c_parser_sizeof_expression (c_parser *parser)
6484 struct c_expr expr;
6485 location_t expr_loc;
6486 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6487 c_parser_consume_token (parser);
6488 c_inhibit_evaluation_warnings++;
6489 in_sizeof++;
6490 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6491 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6493 /* Either sizeof ( type-name ) or sizeof unary-expression
6494 starting with a compound literal. */
6495 struct c_type_name *type_name;
6496 c_parser_consume_token (parser);
6497 expr_loc = c_parser_peek_token (parser)->location;
6498 type_name = c_parser_type_name (parser);
6499 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6500 if (type_name == NULL)
6502 struct c_expr ret;
6503 c_inhibit_evaluation_warnings--;
6504 in_sizeof--;
6505 ret.value = error_mark_node;
6506 ret.original_code = ERROR_MARK;
6507 ret.original_type = NULL;
6508 return ret;
6510 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6512 expr = c_parser_postfix_expression_after_paren_type (parser,
6513 type_name,
6514 expr_loc);
6515 goto sizeof_expr;
6517 /* sizeof ( type-name ). */
6518 c_inhibit_evaluation_warnings--;
6519 in_sizeof--;
6520 return c_expr_sizeof_type (expr_loc, type_name);
6522 else
6524 expr_loc = c_parser_peek_token (parser)->location;
6525 expr = c_parser_unary_expression (parser);
6526 sizeof_expr:
6527 c_inhibit_evaluation_warnings--;
6528 in_sizeof--;
6529 mark_exp_read (expr.value);
6530 if (TREE_CODE (expr.value) == COMPONENT_REF
6531 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6532 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6533 return c_expr_sizeof_expr (expr_loc, expr);
6537 /* Parse an alignof expression. */
6539 static struct c_expr
6540 c_parser_alignof_expression (c_parser *parser)
6542 struct c_expr expr;
6543 location_t loc = c_parser_peek_token (parser)->location;
6544 tree alignof_spelling = c_parser_peek_token (parser)->value;
6545 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6546 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6547 "_Alignof") == 0;
6548 /* A diagnostic is not required for the use of this identifier in
6549 the implementation namespace; only diagnose it for the C11
6550 spelling because of existing code using the other spellings. */
6551 if (!flag_isoc11 && is_c11_alignof)
6553 if (flag_isoc99)
6554 pedwarn (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6555 alignof_spelling);
6556 else
6557 pedwarn (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6558 alignof_spelling);
6560 c_parser_consume_token (parser);
6561 c_inhibit_evaluation_warnings++;
6562 in_alignof++;
6563 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6564 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6566 /* Either __alignof__ ( type-name ) or __alignof__
6567 unary-expression starting with a compound literal. */
6568 location_t loc;
6569 struct c_type_name *type_name;
6570 struct c_expr ret;
6571 c_parser_consume_token (parser);
6572 loc = c_parser_peek_token (parser)->location;
6573 type_name = c_parser_type_name (parser);
6574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6575 if (type_name == NULL)
6577 struct c_expr ret;
6578 c_inhibit_evaluation_warnings--;
6579 in_alignof--;
6580 ret.value = error_mark_node;
6581 ret.original_code = ERROR_MARK;
6582 ret.original_type = NULL;
6583 return ret;
6585 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6587 expr = c_parser_postfix_expression_after_paren_type (parser,
6588 type_name,
6589 loc);
6590 goto alignof_expr;
6592 /* alignof ( type-name ). */
6593 c_inhibit_evaluation_warnings--;
6594 in_alignof--;
6595 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6596 NULL, NULL),
6597 false, is_c11_alignof, 1);
6598 ret.original_code = ERROR_MARK;
6599 ret.original_type = NULL;
6600 return ret;
6602 else
6604 struct c_expr ret;
6605 expr = c_parser_unary_expression (parser);
6606 alignof_expr:
6607 mark_exp_read (expr.value);
6608 c_inhibit_evaluation_warnings--;
6609 in_alignof--;
6610 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6611 alignof_spelling);
6612 ret.value = c_alignof_expr (loc, expr.value);
6613 ret.original_code = ERROR_MARK;
6614 ret.original_type = NULL;
6615 return ret;
6619 /* Helper function to read arguments of builtins which are interfaces
6620 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6621 others. The name of the builtin is passed using BNAME parameter.
6622 Function returns true if there were no errors while parsing and
6623 stores the arguments in CEXPR_LIST. */
6624 static bool
6625 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6626 vec<c_expr_t, va_gc> **ret_cexpr_list,
6627 bool choose_expr_p)
6629 location_t loc = c_parser_peek_token (parser)->location;
6630 vec<c_expr_t, va_gc> *cexpr_list;
6631 c_expr_t expr;
6632 bool saved_force_folding_builtin_constant_p;
6634 *ret_cexpr_list = NULL;
6635 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6637 error_at (loc, "cannot take address of %qs", bname);
6638 return false;
6641 c_parser_consume_token (parser);
6643 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6645 c_parser_consume_token (parser);
6646 return true;
6649 saved_force_folding_builtin_constant_p
6650 = force_folding_builtin_constant_p;
6651 force_folding_builtin_constant_p |= choose_expr_p;
6652 expr = c_parser_expr_no_commas (parser, NULL);
6653 force_folding_builtin_constant_p
6654 = saved_force_folding_builtin_constant_p;
6655 vec_alloc (cexpr_list, 1);
6656 vec_safe_push (cexpr_list, expr);
6657 while (c_parser_next_token_is (parser, CPP_COMMA))
6659 c_parser_consume_token (parser);
6660 expr = c_parser_expr_no_commas (parser, NULL);
6661 vec_safe_push (cexpr_list, expr);
6664 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6665 return false;
6667 *ret_cexpr_list = cexpr_list;
6668 return true;
6671 /* This represents a single generic-association. */
6673 struct c_generic_association
6675 /* The location of the starting token of the type. */
6676 location_t type_location;
6677 /* The association's type, or NULL_TREE for 'default'. */
6678 tree type;
6679 /* The association's expression. */
6680 struct c_expr expression;
6683 /* Parse a generic-selection. (C11 6.5.1.1).
6685 generic-selection:
6686 _Generic ( assignment-expression , generic-assoc-list )
6688 generic-assoc-list:
6689 generic-association
6690 generic-assoc-list , generic-association
6692 generic-association:
6693 type-name : assignment-expression
6694 default : assignment-expression
6697 static struct c_expr
6698 c_parser_generic_selection (c_parser *parser)
6700 vec<c_generic_association> associations = vNULL;
6701 struct c_expr selector, error_expr;
6702 tree selector_type;
6703 struct c_generic_association matched_assoc;
6704 bool match_found = false;
6705 location_t generic_loc, selector_loc;
6707 error_expr.original_code = ERROR_MARK;
6708 error_expr.original_type = NULL;
6709 error_expr.value = error_mark_node;
6710 matched_assoc.type_location = UNKNOWN_LOCATION;
6711 matched_assoc.type = NULL_TREE;
6712 matched_assoc.expression = error_expr;
6714 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6715 generic_loc = c_parser_peek_token (parser)->location;
6716 c_parser_consume_token (parser);
6717 if (!flag_isoc11)
6719 if (flag_isoc99)
6720 pedwarn (generic_loc, OPT_Wpedantic,
6721 "ISO C99 does not support %<_Generic%>");
6722 else
6723 pedwarn (generic_loc, OPT_Wpedantic,
6724 "ISO C90 does not support %<_Generic%>");
6727 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6728 return error_expr;
6730 c_inhibit_evaluation_warnings++;
6731 selector_loc = c_parser_peek_token (parser)->location;
6732 selector = c_parser_expr_no_commas (parser, NULL);
6733 selector = default_function_array_conversion (selector_loc, selector);
6734 c_inhibit_evaluation_warnings--;
6736 if (selector.value == error_mark_node)
6738 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6739 return selector;
6741 selector_type = TREE_TYPE (selector.value);
6742 /* In ISO C terms, rvalues (including the controlling expression of
6743 _Generic) do not have qualified types. */
6744 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6745 selector_type = TYPE_MAIN_VARIANT (selector_type);
6746 /* In ISO C terms, _Noreturn is not part of the type of expressions
6747 such as &abort, but in GCC it is represented internally as a type
6748 qualifier. */
6749 if (FUNCTION_POINTER_TYPE_P (selector_type)
6750 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6751 selector_type
6752 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6754 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6756 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6757 return error_expr;
6760 while (1)
6762 struct c_generic_association assoc, *iter;
6763 unsigned int ix;
6764 c_token *token = c_parser_peek_token (parser);
6766 assoc.type_location = token->location;
6767 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6769 c_parser_consume_token (parser);
6770 assoc.type = NULL_TREE;
6772 else
6774 struct c_type_name *type_name;
6776 type_name = c_parser_type_name (parser);
6777 if (type_name == NULL)
6779 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6780 goto error_exit;
6782 assoc.type = groktypename (type_name, NULL, NULL);
6783 if (assoc.type == error_mark_node)
6785 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6786 goto error_exit;
6789 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6790 error_at (assoc.type_location,
6791 "%<_Generic%> association has function type");
6792 else if (!COMPLETE_TYPE_P (assoc.type))
6793 error_at (assoc.type_location,
6794 "%<_Generic%> association has incomplete type");
6796 if (variably_modified_type_p (assoc.type, NULL_TREE))
6797 error_at (assoc.type_location,
6798 "%<_Generic%> association has "
6799 "variable length type");
6802 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6805 goto error_exit;
6808 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6809 if (assoc.expression.value == error_mark_node)
6811 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6812 goto error_exit;
6815 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6817 if (assoc.type == NULL_TREE)
6819 if (iter->type == NULL_TREE)
6821 error_at (assoc.type_location,
6822 "duplicate %<default%> case in %<_Generic%>");
6823 inform (iter->type_location, "original %<default%> is here");
6826 else if (iter->type != NULL_TREE)
6828 if (comptypes (assoc.type, iter->type))
6830 error_at (assoc.type_location,
6831 "%<_Generic%> specifies two compatible types");
6832 inform (iter->type_location, "compatible type is here");
6837 if (assoc.type == NULL_TREE)
6839 if (!match_found)
6841 matched_assoc = assoc;
6842 match_found = true;
6845 else if (comptypes (assoc.type, selector_type))
6847 if (!match_found || matched_assoc.type == NULL_TREE)
6849 matched_assoc = assoc;
6850 match_found = true;
6852 else
6854 error_at (assoc.type_location,
6855 "%<_Generic> selector matches multiple associations");
6856 inform (matched_assoc.type_location,
6857 "other match is here");
6861 associations.safe_push (assoc);
6863 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6864 break;
6865 c_parser_consume_token (parser);
6868 associations.release ();
6870 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6872 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6873 return error_expr;
6876 if (!match_found)
6878 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6879 "compatible with any association",
6880 selector_type);
6881 return error_expr;
6884 return matched_assoc.expression;
6886 error_exit:
6887 associations.release ();
6888 return error_expr;
6891 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6893 postfix-expression:
6894 primary-expression
6895 postfix-expression [ expression ]
6896 postfix-expression ( argument-expression-list[opt] )
6897 postfix-expression . identifier
6898 postfix-expression -> identifier
6899 postfix-expression ++
6900 postfix-expression --
6901 ( type-name ) { initializer-list }
6902 ( type-name ) { initializer-list , }
6904 argument-expression-list:
6905 argument-expression
6906 argument-expression-list , argument-expression
6908 primary-expression:
6909 identifier
6910 constant
6911 string-literal
6912 ( expression )
6913 generic-selection
6915 GNU extensions:
6917 primary-expression:
6918 __func__
6919 (treated as a keyword in GNU C)
6920 __FUNCTION__
6921 __PRETTY_FUNCTION__
6922 ( compound-statement )
6923 __builtin_va_arg ( assignment-expression , type-name )
6924 __builtin_offsetof ( type-name , offsetof-member-designator )
6925 __builtin_choose_expr ( assignment-expression ,
6926 assignment-expression ,
6927 assignment-expression )
6928 __builtin_types_compatible_p ( type-name , type-name )
6929 __builtin_complex ( assignment-expression , assignment-expression )
6930 __builtin_shuffle ( assignment-expression , assignment-expression )
6931 __builtin_shuffle ( assignment-expression ,
6932 assignment-expression ,
6933 assignment-expression, )
6935 offsetof-member-designator:
6936 identifier
6937 offsetof-member-designator . identifier
6938 offsetof-member-designator [ expression ]
6940 Objective-C:
6942 primary-expression:
6943 [ objc-receiver objc-message-args ]
6944 @selector ( objc-selector-arg )
6945 @protocol ( identifier )
6946 @encode ( type-name )
6947 objc-string-literal
6948 Classname . identifier
6951 static struct c_expr
6952 c_parser_postfix_expression (c_parser *parser)
6954 struct c_expr expr, e1;
6955 struct c_type_name *t1, *t2;
6956 location_t loc = c_parser_peek_token (parser)->location;;
6957 expr.original_code = ERROR_MARK;
6958 expr.original_type = NULL;
6959 switch (c_parser_peek_token (parser)->type)
6961 case CPP_NUMBER:
6962 expr.value = c_parser_peek_token (parser)->value;
6963 loc = c_parser_peek_token (parser)->location;
6964 c_parser_consume_token (parser);
6965 if (TREE_CODE (expr.value) == FIXED_CST
6966 && !targetm.fixed_point_supported_p ())
6968 error_at (loc, "fixed-point types not supported for this target");
6969 expr.value = error_mark_node;
6971 break;
6972 case CPP_CHAR:
6973 case CPP_CHAR16:
6974 case CPP_CHAR32:
6975 case CPP_WCHAR:
6976 expr.value = c_parser_peek_token (parser)->value;
6977 c_parser_consume_token (parser);
6978 break;
6979 case CPP_STRING:
6980 case CPP_STRING16:
6981 case CPP_STRING32:
6982 case CPP_WSTRING:
6983 case CPP_UTF8STRING:
6984 expr.value = c_parser_peek_token (parser)->value;
6985 expr.original_code = STRING_CST;
6986 c_parser_consume_token (parser);
6987 break;
6988 case CPP_OBJC_STRING:
6989 gcc_assert (c_dialect_objc ());
6990 expr.value
6991 = objc_build_string_object (c_parser_peek_token (parser)->value);
6992 c_parser_consume_token (parser);
6993 break;
6994 case CPP_NAME:
6995 switch (c_parser_peek_token (parser)->id_kind)
6997 case C_ID_ID:
6999 tree id = c_parser_peek_token (parser)->value;
7000 c_parser_consume_token (parser);
7001 expr.value = build_external_ref (loc, id,
7002 (c_parser_peek_token (parser)->type
7003 == CPP_OPEN_PAREN),
7004 &expr.original_type);
7005 break;
7007 case C_ID_CLASSNAME:
7009 /* Here we parse the Objective-C 2.0 Class.name dot
7010 syntax. */
7011 tree class_name = c_parser_peek_token (parser)->value;
7012 tree component;
7013 c_parser_consume_token (parser);
7014 gcc_assert (c_dialect_objc ());
7015 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7017 expr.value = error_mark_node;
7018 break;
7020 if (c_parser_next_token_is_not (parser, CPP_NAME))
7022 c_parser_error (parser, "expected identifier");
7023 expr.value = error_mark_node;
7024 break;
7026 component = c_parser_peek_token (parser)->value;
7027 c_parser_consume_token (parser);
7028 expr.value = objc_build_class_component_ref (class_name,
7029 component);
7030 break;
7032 default:
7033 c_parser_error (parser, "expected expression");
7034 expr.value = error_mark_node;
7035 break;
7037 break;
7038 case CPP_OPEN_PAREN:
7039 /* A parenthesized expression, statement expression or compound
7040 literal. */
7041 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7043 /* A statement expression. */
7044 tree stmt;
7045 location_t brace_loc;
7046 c_parser_consume_token (parser);
7047 brace_loc = c_parser_peek_token (parser)->location;
7048 c_parser_consume_token (parser);
7049 if (!building_stmt_list_p ())
7051 error_at (loc, "braced-group within expression allowed "
7052 "only inside a function");
7053 parser->error = true;
7054 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7055 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7056 expr.value = error_mark_node;
7057 break;
7059 stmt = c_begin_stmt_expr ();
7060 c_parser_compound_statement_nostart (parser);
7061 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7062 "expected %<)%>");
7063 pedwarn (loc, OPT_Wpedantic,
7064 "ISO C forbids braced-groups within expressions");
7065 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7066 mark_exp_read (expr.value);
7068 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7070 /* A compound literal. ??? Can we actually get here rather
7071 than going directly to
7072 c_parser_postfix_expression_after_paren_type from
7073 elsewhere? */
7074 location_t loc;
7075 struct c_type_name *type_name;
7076 c_parser_consume_token (parser);
7077 loc = c_parser_peek_token (parser)->location;
7078 type_name = c_parser_type_name (parser);
7079 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7080 "expected %<)%>");
7081 if (type_name == NULL)
7083 expr.value = error_mark_node;
7085 else
7086 expr = c_parser_postfix_expression_after_paren_type (parser,
7087 type_name,
7088 loc);
7090 else
7092 /* A parenthesized expression. */
7093 c_parser_consume_token (parser);
7094 expr = c_parser_expression (parser);
7095 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7096 TREE_NO_WARNING (expr.value) = 1;
7097 if (expr.original_code != C_MAYBE_CONST_EXPR)
7098 expr.original_code = ERROR_MARK;
7099 /* Don't change EXPR.ORIGINAL_TYPE. */
7100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7101 "expected %<)%>");
7103 break;
7104 case CPP_KEYWORD:
7105 switch (c_parser_peek_token (parser)->keyword)
7107 case RID_FUNCTION_NAME:
7108 case RID_PRETTY_FUNCTION_NAME:
7109 case RID_C99_FUNCTION_NAME:
7110 expr.value = fname_decl (loc,
7111 c_parser_peek_token (parser)->keyword,
7112 c_parser_peek_token (parser)->value);
7113 c_parser_consume_token (parser);
7114 break;
7115 case RID_VA_ARG:
7116 c_parser_consume_token (parser);
7117 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7119 expr.value = error_mark_node;
7120 break;
7122 e1 = c_parser_expr_no_commas (parser, NULL);
7123 mark_exp_read (e1.value);
7124 e1.value = c_fully_fold (e1.value, false, NULL);
7125 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7127 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7128 expr.value = error_mark_node;
7129 break;
7131 loc = c_parser_peek_token (parser)->location;
7132 t1 = c_parser_type_name (parser);
7133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7134 "expected %<)%>");
7135 if (t1 == NULL)
7137 expr.value = error_mark_node;
7139 else
7141 tree type_expr = NULL_TREE;
7142 expr.value = c_build_va_arg (loc, e1.value,
7143 groktypename (t1, &type_expr, NULL));
7144 if (type_expr)
7146 expr.value = build2 (C_MAYBE_CONST_EXPR,
7147 TREE_TYPE (expr.value), type_expr,
7148 expr.value);
7149 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7152 break;
7153 case RID_OFFSETOF:
7154 c_parser_consume_token (parser);
7155 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7157 expr.value = error_mark_node;
7158 break;
7160 t1 = c_parser_type_name (parser);
7161 if (t1 == NULL)
7162 parser->error = true;
7163 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7164 gcc_assert (parser->error);
7165 if (parser->error)
7167 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7168 expr.value = error_mark_node;
7169 break;
7173 tree type = groktypename (t1, NULL, NULL);
7174 tree offsetof_ref;
7175 if (type == error_mark_node)
7176 offsetof_ref = error_mark_node;
7177 else
7179 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7180 SET_EXPR_LOCATION (offsetof_ref, loc);
7182 /* Parse the second argument to __builtin_offsetof. We
7183 must have one identifier, and beyond that we want to
7184 accept sub structure and sub array references. */
7185 if (c_parser_next_token_is (parser, CPP_NAME))
7187 offsetof_ref = build_component_ref
7188 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7189 c_parser_consume_token (parser);
7190 while (c_parser_next_token_is (parser, CPP_DOT)
7191 || c_parser_next_token_is (parser,
7192 CPP_OPEN_SQUARE)
7193 || c_parser_next_token_is (parser,
7194 CPP_DEREF))
7196 if (c_parser_next_token_is (parser, CPP_DEREF))
7198 loc = c_parser_peek_token (parser)->location;
7199 offsetof_ref = build_array_ref (loc,
7200 offsetof_ref,
7201 integer_zero_node);
7202 goto do_dot;
7204 else if (c_parser_next_token_is (parser, CPP_DOT))
7206 do_dot:
7207 c_parser_consume_token (parser);
7208 if (c_parser_next_token_is_not (parser,
7209 CPP_NAME))
7211 c_parser_error (parser, "expected identifier");
7212 break;
7214 offsetof_ref = build_component_ref
7215 (loc, offsetof_ref,
7216 c_parser_peek_token (parser)->value);
7217 c_parser_consume_token (parser);
7219 else
7221 struct c_expr ce;
7222 tree idx;
7223 loc = c_parser_peek_token (parser)->location;
7224 c_parser_consume_token (parser);
7225 ce = c_parser_expression (parser);
7226 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7227 idx = ce.value;
7228 idx = c_fully_fold (idx, false, NULL);
7229 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7230 "expected %<]%>");
7231 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7235 else
7236 c_parser_error (parser, "expected identifier");
7237 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7238 "expected %<)%>");
7239 expr.value = fold_offsetof (offsetof_ref);
7241 break;
7242 case RID_CHOOSE_EXPR:
7244 vec<c_expr_t, va_gc> *cexpr_list;
7245 c_expr_t *e1_p, *e2_p, *e3_p;
7246 tree c;
7248 c_parser_consume_token (parser);
7249 if (!c_parser_get_builtin_args (parser,
7250 "__builtin_choose_expr",
7251 &cexpr_list, true))
7253 expr.value = error_mark_node;
7254 break;
7257 if (vec_safe_length (cexpr_list) != 3)
7259 error_at (loc, "wrong number of arguments to "
7260 "%<__builtin_choose_expr%>");
7261 expr.value = error_mark_node;
7262 break;
7265 e1_p = &(*cexpr_list)[0];
7266 e2_p = &(*cexpr_list)[1];
7267 e3_p = &(*cexpr_list)[2];
7269 c = e1_p->value;
7270 mark_exp_read (e2_p->value);
7271 mark_exp_read (e3_p->value);
7272 if (TREE_CODE (c) != INTEGER_CST
7273 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7274 error_at (loc,
7275 "first argument to %<__builtin_choose_expr%> not"
7276 " a constant");
7277 constant_expression_warning (c);
7278 expr = integer_zerop (c) ? *e3_p : *e2_p;
7279 break;
7281 case RID_TYPES_COMPATIBLE_P:
7282 c_parser_consume_token (parser);
7283 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7285 expr.value = error_mark_node;
7286 break;
7288 t1 = c_parser_type_name (parser);
7289 if (t1 == NULL)
7291 expr.value = error_mark_node;
7292 break;
7294 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7296 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7297 expr.value = error_mark_node;
7298 break;
7300 t2 = c_parser_type_name (parser);
7301 if (t2 == NULL)
7303 expr.value = error_mark_node;
7304 break;
7306 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7307 "expected %<)%>");
7309 tree e1, e2;
7310 e1 = groktypename (t1, NULL, NULL);
7311 e2 = groktypename (t2, NULL, NULL);
7312 if (e1 == error_mark_node || e2 == error_mark_node)
7314 expr.value = error_mark_node;
7315 break;
7318 e1 = TYPE_MAIN_VARIANT (e1);
7319 e2 = TYPE_MAIN_VARIANT (e2);
7321 expr.value
7322 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7324 break;
7325 case RID_BUILTIN_COMPLEX:
7327 vec<c_expr_t, va_gc> *cexpr_list;
7328 c_expr_t *e1_p, *e2_p;
7330 c_parser_consume_token (parser);
7331 if (!c_parser_get_builtin_args (parser,
7332 "__builtin_complex",
7333 &cexpr_list, false))
7335 expr.value = error_mark_node;
7336 break;
7339 if (vec_safe_length (cexpr_list) != 2)
7341 error_at (loc, "wrong number of arguments to "
7342 "%<__builtin_complex%>");
7343 expr.value = error_mark_node;
7344 break;
7347 e1_p = &(*cexpr_list)[0];
7348 e2_p = &(*cexpr_list)[1];
7350 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7351 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7352 e1_p->value = convert (TREE_TYPE (e1_p->value),
7353 TREE_OPERAND (e1_p->value, 0));
7354 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7355 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7356 e2_p->value = convert (TREE_TYPE (e2_p->value),
7357 TREE_OPERAND (e2_p->value, 0));
7358 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7359 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7360 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7361 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7363 error_at (loc, "%<__builtin_complex%> operand "
7364 "not of real binary floating-point type");
7365 expr.value = error_mark_node;
7366 break;
7368 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7369 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7371 error_at (loc,
7372 "%<__builtin_complex%> operands of different types");
7373 expr.value = error_mark_node;
7374 break;
7376 if (!flag_isoc99)
7377 pedwarn (loc, OPT_Wpedantic,
7378 "ISO C90 does not support complex types");
7379 expr.value = build2 (COMPLEX_EXPR,
7380 build_complex_type
7381 (TYPE_MAIN_VARIANT
7382 (TREE_TYPE (e1_p->value))),
7383 e1_p->value, e2_p->value);
7384 break;
7386 case RID_BUILTIN_SHUFFLE:
7388 vec<c_expr_t, va_gc> *cexpr_list;
7389 unsigned int i;
7390 c_expr_t *p;
7392 c_parser_consume_token (parser);
7393 if (!c_parser_get_builtin_args (parser,
7394 "__builtin_shuffle",
7395 &cexpr_list, false))
7397 expr.value = error_mark_node;
7398 break;
7401 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7402 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7404 if (vec_safe_length (cexpr_list) == 2)
7405 expr.value =
7406 c_build_vec_perm_expr
7407 (loc, (*cexpr_list)[0].value,
7408 NULL_TREE, (*cexpr_list)[1].value);
7410 else if (vec_safe_length (cexpr_list) == 3)
7411 expr.value =
7412 c_build_vec_perm_expr
7413 (loc, (*cexpr_list)[0].value,
7414 (*cexpr_list)[1].value,
7415 (*cexpr_list)[2].value);
7416 else
7418 error_at (loc, "wrong number of arguments to "
7419 "%<__builtin_shuffle%>");
7420 expr.value = error_mark_node;
7422 break;
7424 case RID_AT_SELECTOR:
7425 gcc_assert (c_dialect_objc ());
7426 c_parser_consume_token (parser);
7427 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7429 expr.value = error_mark_node;
7430 break;
7433 tree sel = c_parser_objc_selector_arg (parser);
7434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7435 "expected %<)%>");
7436 expr.value = objc_build_selector_expr (loc, sel);
7438 break;
7439 case RID_AT_PROTOCOL:
7440 gcc_assert (c_dialect_objc ());
7441 c_parser_consume_token (parser);
7442 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7444 expr.value = error_mark_node;
7445 break;
7447 if (c_parser_next_token_is_not (parser, CPP_NAME))
7449 c_parser_error (parser, "expected identifier");
7450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7451 expr.value = error_mark_node;
7452 break;
7455 tree id = c_parser_peek_token (parser)->value;
7456 c_parser_consume_token (parser);
7457 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7458 "expected %<)%>");
7459 expr.value = objc_build_protocol_expr (id);
7461 break;
7462 case RID_AT_ENCODE:
7463 /* Extension to support C-structures in the archiver. */
7464 gcc_assert (c_dialect_objc ());
7465 c_parser_consume_token (parser);
7466 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7468 expr.value = error_mark_node;
7469 break;
7471 t1 = c_parser_type_name (parser);
7472 if (t1 == NULL)
7474 expr.value = error_mark_node;
7475 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7476 break;
7478 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7479 "expected %<)%>");
7481 tree type = groktypename (t1, NULL, NULL);
7482 expr.value = objc_build_encode_expr (type);
7484 break;
7485 case RID_GENERIC:
7486 expr = c_parser_generic_selection (parser);
7487 break;
7488 case RID_CILK_SPAWN:
7489 c_parser_consume_token (parser);
7490 if (!flag_cilkplus)
7492 error_at (loc, "-fcilkplus must be enabled to use "
7493 "%<_Cilk_spawn%>");
7494 expr = c_parser_postfix_expression (parser);
7495 expr.value = error_mark_node;
7497 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7499 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7500 "are not permitted");
7501 /* Now flush out all the _Cilk_spawns. */
7502 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7503 c_parser_consume_token (parser);
7504 expr = c_parser_postfix_expression (parser);
7506 else
7508 expr = c_parser_postfix_expression (parser);
7509 expr.value = build_cilk_spawn (loc, expr.value);
7511 break;
7512 default:
7513 c_parser_error (parser, "expected expression");
7514 expr.value = error_mark_node;
7515 break;
7517 break;
7518 case CPP_OPEN_SQUARE:
7519 if (c_dialect_objc ())
7521 tree receiver, args;
7522 c_parser_consume_token (parser);
7523 receiver = c_parser_objc_receiver (parser);
7524 args = c_parser_objc_message_args (parser);
7525 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7526 "expected %<]%>");
7527 expr.value = objc_build_message_expr (receiver, args);
7528 break;
7530 /* Else fall through to report error. */
7531 default:
7532 c_parser_error (parser, "expected expression");
7533 expr.value = error_mark_node;
7534 break;
7536 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7539 /* Parse a postfix expression after a parenthesized type name: the
7540 brace-enclosed initializer of a compound literal, possibly followed
7541 by some postfix operators. This is separate because it is not
7542 possible to tell until after the type name whether a cast
7543 expression has a cast or a compound literal, or whether the operand
7544 of sizeof is a parenthesized type name or starts with a compound
7545 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7546 location of the first token after the parentheses around the type
7547 name. */
7549 static struct c_expr
7550 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7551 struct c_type_name *type_name,
7552 location_t type_loc)
7554 tree type;
7555 struct c_expr init;
7556 bool non_const;
7557 struct c_expr expr;
7558 location_t start_loc;
7559 tree type_expr = NULL_TREE;
7560 bool type_expr_const = true;
7561 check_compound_literal_type (type_loc, type_name);
7562 start_init (NULL_TREE, NULL, 0);
7563 type = groktypename (type_name, &type_expr, &type_expr_const);
7564 start_loc = c_parser_peek_token (parser)->location;
7565 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7567 error_at (type_loc, "compound literal has variable size");
7568 type = error_mark_node;
7570 init = c_parser_braced_init (parser, type, false);
7571 finish_init ();
7572 maybe_warn_string_init (type, init);
7574 if (type != error_mark_node
7575 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7576 && current_function_decl)
7578 error ("compound literal qualified by address-space qualifier");
7579 type = error_mark_node;
7582 if (!flag_isoc99)
7583 pedwarn (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7584 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7585 ? CONSTRUCTOR_NON_CONST (init.value)
7586 : init.original_code == C_MAYBE_CONST_EXPR);
7587 non_const |= !type_expr_const;
7588 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7589 expr.original_code = ERROR_MARK;
7590 expr.original_type = NULL;
7591 if (type_expr)
7593 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7595 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7596 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7598 else
7600 gcc_assert (!non_const);
7601 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7602 type_expr, expr.value);
7605 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7608 /* Callback function for sizeof_pointer_memaccess_warning to compare
7609 types. */
7611 static bool
7612 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7614 return comptypes (type1, type2) == 1;
7617 /* Parse a postfix expression after the initial primary or compound
7618 literal; that is, parse a series of postfix operators.
7620 EXPR_LOC is the location of the primary expression. */
7622 static struct c_expr
7623 c_parser_postfix_expression_after_primary (c_parser *parser,
7624 location_t expr_loc,
7625 struct c_expr expr)
7627 struct c_expr orig_expr;
7628 tree ident, idx;
7629 location_t sizeof_arg_loc[3];
7630 tree sizeof_arg[3];
7631 unsigned int i;
7632 vec<tree, va_gc> *exprlist;
7633 vec<tree, va_gc> *origtypes = NULL;
7634 vec<location_t> arg_loc = vNULL;
7636 while (true)
7638 location_t op_loc = c_parser_peek_token (parser)->location;
7639 switch (c_parser_peek_token (parser)->type)
7641 case CPP_OPEN_SQUARE:
7642 /* Array reference. */
7643 c_parser_consume_token (parser);
7644 if (flag_cilkplus
7645 && c_parser_peek_token (parser)->type == CPP_COLON)
7646 /* If we are here, then we have something like this:
7647 Array [ : ]
7649 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7650 expr.value);
7651 else
7653 idx = c_parser_expression (parser).value;
7654 /* Here we have 3 options:
7655 1. Array [EXPR] -- Normal Array call.
7656 2. Array [EXPR : EXPR] -- Array notation without stride.
7657 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7659 For 1, we just handle it just like a normal array expression.
7660 For 2 and 3 we handle it like we handle array notations. The
7661 idx value we have above becomes the initial/start index.
7663 if (flag_cilkplus
7664 && c_parser_peek_token (parser)->type == CPP_COLON)
7665 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7666 expr.value);
7667 else
7669 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7670 "expected %<]%>");
7671 expr.value = build_array_ref (op_loc, expr.value, idx);
7674 expr.original_code = ERROR_MARK;
7675 expr.original_type = NULL;
7676 break;
7677 case CPP_OPEN_PAREN:
7678 /* Function call. */
7679 c_parser_consume_token (parser);
7680 for (i = 0; i < 3; i++)
7682 sizeof_arg[i] = NULL_TREE;
7683 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7685 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7686 exprlist = NULL;
7687 else
7688 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7689 sizeof_arg_loc, sizeof_arg,
7690 &arg_loc);
7691 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7692 "expected %<)%>");
7693 orig_expr = expr;
7694 mark_exp_read (expr.value);
7695 if (warn_sizeof_pointer_memaccess)
7696 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7697 expr.value, exprlist,
7698 sizeof_arg,
7699 sizeof_ptr_memacc_comptypes);
7700 expr.value
7701 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7702 exprlist, origtypes);
7703 expr.original_code = ERROR_MARK;
7704 if (TREE_CODE (expr.value) == INTEGER_CST
7705 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7706 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7707 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7708 expr.original_code = C_MAYBE_CONST_EXPR;
7709 expr.original_type = NULL;
7710 if (exprlist)
7712 release_tree_vector (exprlist);
7713 release_tree_vector (origtypes);
7715 arg_loc.release ();
7716 break;
7717 case CPP_DOT:
7718 /* Structure element reference. */
7719 c_parser_consume_token (parser);
7720 expr = default_function_array_conversion (expr_loc, expr);
7721 if (c_parser_next_token_is (parser, CPP_NAME))
7722 ident = c_parser_peek_token (parser)->value;
7723 else
7725 c_parser_error (parser, "expected identifier");
7726 expr.value = error_mark_node;
7727 expr.original_code = ERROR_MARK;
7728 expr.original_type = NULL;
7729 return expr;
7731 c_parser_consume_token (parser);
7732 expr.value = build_component_ref (op_loc, expr.value, ident);
7733 expr.original_code = ERROR_MARK;
7734 if (TREE_CODE (expr.value) != COMPONENT_REF)
7735 expr.original_type = NULL;
7736 else
7738 /* Remember the original type of a bitfield. */
7739 tree field = TREE_OPERAND (expr.value, 1);
7740 if (TREE_CODE (field) != FIELD_DECL)
7741 expr.original_type = NULL;
7742 else
7743 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7745 break;
7746 case CPP_DEREF:
7747 /* Structure element reference. */
7748 c_parser_consume_token (parser);
7749 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7750 if (c_parser_next_token_is (parser, CPP_NAME))
7751 ident = c_parser_peek_token (parser)->value;
7752 else
7754 c_parser_error (parser, "expected identifier");
7755 expr.value = error_mark_node;
7756 expr.original_code = ERROR_MARK;
7757 expr.original_type = NULL;
7758 return expr;
7760 c_parser_consume_token (parser);
7761 expr.value = build_component_ref (op_loc,
7762 build_indirect_ref (op_loc,
7763 expr.value,
7764 RO_ARROW),
7765 ident);
7766 expr.original_code = ERROR_MARK;
7767 if (TREE_CODE (expr.value) != COMPONENT_REF)
7768 expr.original_type = NULL;
7769 else
7771 /* Remember the original type of a bitfield. */
7772 tree field = TREE_OPERAND (expr.value, 1);
7773 if (TREE_CODE (field) != FIELD_DECL)
7774 expr.original_type = NULL;
7775 else
7776 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7778 break;
7779 case CPP_PLUS_PLUS:
7780 /* Postincrement. */
7781 c_parser_consume_token (parser);
7782 /* If the expressions have array notations, we expand them. */
7783 if (flag_cilkplus
7784 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7785 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7786 else
7788 expr = default_function_array_read_conversion (expr_loc, expr);
7789 expr.value = build_unary_op (op_loc,
7790 POSTINCREMENT_EXPR, expr.value, 0);
7792 expr.original_code = ERROR_MARK;
7793 expr.original_type = NULL;
7794 break;
7795 case CPP_MINUS_MINUS:
7796 /* Postdecrement. */
7797 c_parser_consume_token (parser);
7798 /* If the expressions have array notations, we expand them. */
7799 if (flag_cilkplus
7800 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7801 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7802 else
7804 expr = default_function_array_read_conversion (expr_loc, expr);
7805 expr.value = build_unary_op (op_loc,
7806 POSTDECREMENT_EXPR, expr.value, 0);
7808 expr.original_code = ERROR_MARK;
7809 expr.original_type = NULL;
7810 break;
7811 default:
7812 return expr;
7817 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7819 expression:
7820 assignment-expression
7821 expression , assignment-expression
7824 static struct c_expr
7825 c_parser_expression (c_parser *parser)
7827 location_t tloc = c_parser_peek_token (parser)->location;
7828 struct c_expr expr;
7829 expr = c_parser_expr_no_commas (parser, NULL);
7830 if (c_parser_next_token_is (parser, CPP_COMMA))
7831 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7832 while (c_parser_next_token_is (parser, CPP_COMMA))
7834 struct c_expr next;
7835 tree lhsval;
7836 location_t loc = c_parser_peek_token (parser)->location;
7837 location_t expr_loc;
7838 c_parser_consume_token (parser);
7839 expr_loc = c_parser_peek_token (parser)->location;
7840 lhsval = expr.value;
7841 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7842 lhsval = TREE_OPERAND (lhsval, 1);
7843 if (DECL_P (lhsval) || handled_component_p (lhsval))
7844 mark_exp_read (lhsval);
7845 next = c_parser_expr_no_commas (parser, NULL);
7846 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7847 expr.value = build_compound_expr (loc, expr.value, next.value);
7848 expr.original_code = COMPOUND_EXPR;
7849 expr.original_type = next.original_type;
7851 return expr;
7854 /* Parse an expression and convert functions or arrays to pointers and
7855 lvalues to rvalues. */
7857 static struct c_expr
7858 c_parser_expression_conv (c_parser *parser)
7860 struct c_expr expr;
7861 location_t loc = c_parser_peek_token (parser)->location;
7862 expr = c_parser_expression (parser);
7863 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7864 return expr;
7867 /* Parse a non-empty list of expressions. If CONVERT_P, convert
7868 functions and arrays to pointers and lvalues to rvalues. If
7869 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
7870 locations of function arguments into this vector.
7872 nonempty-expr-list:
7873 assignment-expression
7874 nonempty-expr-list , assignment-expression
7877 static vec<tree, va_gc> *
7878 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
7879 vec<tree, va_gc> **p_orig_types,
7880 location_t *sizeof_arg_loc, tree *sizeof_arg,
7881 vec<location_t> *locations)
7883 vec<tree, va_gc> *ret;
7884 vec<tree, va_gc> *orig_types;
7885 struct c_expr expr;
7886 location_t loc = c_parser_peek_token (parser)->location;
7887 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7888 unsigned int idx = 0;
7890 ret = make_tree_vector ();
7891 if (p_orig_types == NULL)
7892 orig_types = NULL;
7893 else
7894 orig_types = make_tree_vector ();
7896 if (sizeof_arg != NULL
7897 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7898 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7899 expr = c_parser_expr_no_commas (parser, NULL);
7900 if (convert_p)
7901 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7902 if (fold_p)
7903 expr.value = c_fully_fold (expr.value, false, NULL);
7904 ret->quick_push (expr.value);
7905 if (orig_types)
7906 orig_types->quick_push (expr.original_type);
7907 if (locations)
7908 locations->safe_push (loc);
7909 if (sizeof_arg != NULL
7910 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7911 && expr.original_code == SIZEOF_EXPR)
7913 sizeof_arg[0] = c_last_sizeof_arg;
7914 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
7916 while (c_parser_next_token_is (parser, CPP_COMMA))
7918 c_parser_consume_token (parser);
7919 loc = c_parser_peek_token (parser)->location;
7920 if (sizeof_arg != NULL
7921 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
7922 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
7923 else
7924 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
7925 expr = c_parser_expr_no_commas (parser, NULL);
7926 if (convert_p)
7927 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
7928 if (fold_p)
7929 expr.value = c_fully_fold (expr.value, false, NULL);
7930 vec_safe_push (ret, expr.value);
7931 if (orig_types)
7932 vec_safe_push (orig_types, expr.original_type);
7933 if (locations)
7934 locations->safe_push (loc);
7935 if (++idx < 3
7936 && sizeof_arg != NULL
7937 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
7938 && expr.original_code == SIZEOF_EXPR)
7940 sizeof_arg[idx] = c_last_sizeof_arg;
7941 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
7944 if (orig_types)
7945 *p_orig_types = orig_types;
7946 return ret;
7949 /* Parse Objective-C-specific constructs. */
7951 /* Parse an objc-class-definition.
7953 objc-class-definition:
7954 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
7955 objc-class-instance-variables[opt] objc-methodprotolist @end
7956 @implementation identifier objc-superclass[opt]
7957 objc-class-instance-variables[opt]
7958 @interface identifier ( identifier ) objc-protocol-refs[opt]
7959 objc-methodprotolist @end
7960 @interface identifier ( ) objc-protocol-refs[opt]
7961 objc-methodprotolist @end
7962 @implementation identifier ( identifier )
7964 objc-superclass:
7965 : identifier
7967 "@interface identifier (" must start "@interface identifier (
7968 identifier ) ...": objc-methodprotolist in the first production may
7969 not start with a parenthesized identifier as a declarator of a data
7970 definition with no declaration specifiers if the objc-superclass,
7971 objc-protocol-refs and objc-class-instance-variables are omitted. */
7973 static void
7974 c_parser_objc_class_definition (c_parser *parser, tree attributes)
7976 bool iface_p;
7977 tree id1;
7978 tree superclass;
7979 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
7980 iface_p = true;
7981 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
7982 iface_p = false;
7983 else
7984 gcc_unreachable ();
7986 c_parser_consume_token (parser);
7987 if (c_parser_next_token_is_not (parser, CPP_NAME))
7989 c_parser_error (parser, "expected identifier");
7990 return;
7992 id1 = c_parser_peek_token (parser)->value;
7993 c_parser_consume_token (parser);
7994 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
7996 /* We have a category or class extension. */
7997 tree id2;
7998 tree proto = NULL_TREE;
7999 c_parser_consume_token (parser);
8000 if (c_parser_next_token_is_not (parser, CPP_NAME))
8002 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8004 /* We have a class extension. */
8005 id2 = NULL_TREE;
8007 else
8009 c_parser_error (parser, "expected identifier or %<)%>");
8010 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8011 return;
8014 else
8016 id2 = c_parser_peek_token (parser)->value;
8017 c_parser_consume_token (parser);
8019 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8020 if (!iface_p)
8022 objc_start_category_implementation (id1, id2);
8023 return;
8025 if (c_parser_next_token_is (parser, CPP_LESS))
8026 proto = c_parser_objc_protocol_refs (parser);
8027 objc_start_category_interface (id1, id2, proto, attributes);
8028 c_parser_objc_methodprotolist (parser);
8029 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8030 objc_finish_interface ();
8031 return;
8033 if (c_parser_next_token_is (parser, CPP_COLON))
8035 c_parser_consume_token (parser);
8036 if (c_parser_next_token_is_not (parser, CPP_NAME))
8038 c_parser_error (parser, "expected identifier");
8039 return;
8041 superclass = c_parser_peek_token (parser)->value;
8042 c_parser_consume_token (parser);
8044 else
8045 superclass = NULL_TREE;
8046 if (iface_p)
8048 tree proto = NULL_TREE;
8049 if (c_parser_next_token_is (parser, CPP_LESS))
8050 proto = c_parser_objc_protocol_refs (parser);
8051 objc_start_class_interface (id1, superclass, proto, attributes);
8053 else
8054 objc_start_class_implementation (id1, superclass);
8055 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8056 c_parser_objc_class_instance_variables (parser);
8057 if (iface_p)
8059 objc_continue_interface ();
8060 c_parser_objc_methodprotolist (parser);
8061 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8062 objc_finish_interface ();
8064 else
8066 objc_continue_implementation ();
8067 return;
8071 /* Parse objc-class-instance-variables.
8073 objc-class-instance-variables:
8074 { objc-instance-variable-decl-list[opt] }
8076 objc-instance-variable-decl-list:
8077 objc-visibility-spec
8078 objc-instance-variable-decl ;
8080 objc-instance-variable-decl-list objc-visibility-spec
8081 objc-instance-variable-decl-list objc-instance-variable-decl ;
8082 objc-instance-variable-decl-list ;
8084 objc-visibility-spec:
8085 @private
8086 @protected
8087 @public
8089 objc-instance-variable-decl:
8090 struct-declaration
8093 static void
8094 c_parser_objc_class_instance_variables (c_parser *parser)
8096 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8097 c_parser_consume_token (parser);
8098 while (c_parser_next_token_is_not (parser, CPP_EOF))
8100 tree decls;
8101 /* Parse any stray semicolon. */
8102 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8104 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8105 "extra semicolon");
8106 c_parser_consume_token (parser);
8107 continue;
8109 /* Stop if at the end of the instance variables. */
8110 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8112 c_parser_consume_token (parser);
8113 break;
8115 /* Parse any objc-visibility-spec. */
8116 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8118 c_parser_consume_token (parser);
8119 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8120 continue;
8122 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8124 c_parser_consume_token (parser);
8125 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8126 continue;
8128 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8130 c_parser_consume_token (parser);
8131 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8132 continue;
8134 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8136 c_parser_consume_token (parser);
8137 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8138 continue;
8140 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8142 c_parser_pragma (parser, pragma_external);
8143 continue;
8146 /* Parse some comma-separated declarations. */
8147 decls = c_parser_struct_declaration (parser);
8148 if (decls == NULL)
8150 /* There is a syntax error. We want to skip the offending
8151 tokens up to the next ';' (included) or '}'
8152 (excluded). */
8154 /* First, skip manually a ')' or ']'. This is because they
8155 reduce the nesting level, so c_parser_skip_until_found()
8156 wouldn't be able to skip past them. */
8157 c_token *token = c_parser_peek_token (parser);
8158 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8159 c_parser_consume_token (parser);
8161 /* Then, do the standard skipping. */
8162 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8164 /* We hopefully recovered. Start normal parsing again. */
8165 parser->error = false;
8166 continue;
8168 else
8170 /* Comma-separated instance variables are chained together
8171 in reverse order; add them one by one. */
8172 tree ivar = nreverse (decls);
8173 for (; ivar; ivar = DECL_CHAIN (ivar))
8174 objc_add_instance_variable (copy_node (ivar));
8176 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8180 /* Parse an objc-class-declaration.
8182 objc-class-declaration:
8183 @class identifier-list ;
8186 static void
8187 c_parser_objc_class_declaration (c_parser *parser)
8189 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8190 c_parser_consume_token (parser);
8191 /* Any identifiers, including those declared as type names, are OK
8192 here. */
8193 while (true)
8195 tree id;
8196 if (c_parser_next_token_is_not (parser, CPP_NAME))
8198 c_parser_error (parser, "expected identifier");
8199 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8200 parser->error = false;
8201 return;
8203 id = c_parser_peek_token (parser)->value;
8204 objc_declare_class (id);
8205 c_parser_consume_token (parser);
8206 if (c_parser_next_token_is (parser, CPP_COMMA))
8207 c_parser_consume_token (parser);
8208 else
8209 break;
8211 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8214 /* Parse an objc-alias-declaration.
8216 objc-alias-declaration:
8217 @compatibility_alias identifier identifier ;
8220 static void
8221 c_parser_objc_alias_declaration (c_parser *parser)
8223 tree id1, id2;
8224 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8225 c_parser_consume_token (parser);
8226 if (c_parser_next_token_is_not (parser, CPP_NAME))
8228 c_parser_error (parser, "expected identifier");
8229 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8230 return;
8232 id1 = c_parser_peek_token (parser)->value;
8233 c_parser_consume_token (parser);
8234 if (c_parser_next_token_is_not (parser, CPP_NAME))
8236 c_parser_error (parser, "expected identifier");
8237 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8238 return;
8240 id2 = c_parser_peek_token (parser)->value;
8241 c_parser_consume_token (parser);
8242 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8243 objc_declare_alias (id1, id2);
8246 /* Parse an objc-protocol-definition.
8248 objc-protocol-definition:
8249 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8250 @protocol identifier-list ;
8252 "@protocol identifier ;" should be resolved as "@protocol
8253 identifier-list ;": objc-methodprotolist may not start with a
8254 semicolon in the first alternative if objc-protocol-refs are
8255 omitted. */
8257 static void
8258 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8260 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8262 c_parser_consume_token (parser);
8263 if (c_parser_next_token_is_not (parser, CPP_NAME))
8265 c_parser_error (parser, "expected identifier");
8266 return;
8268 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8269 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8271 /* Any identifiers, including those declared as type names, are
8272 OK here. */
8273 while (true)
8275 tree id;
8276 if (c_parser_next_token_is_not (parser, CPP_NAME))
8278 c_parser_error (parser, "expected identifier");
8279 break;
8281 id = c_parser_peek_token (parser)->value;
8282 objc_declare_protocol (id, attributes);
8283 c_parser_consume_token (parser);
8284 if (c_parser_next_token_is (parser, CPP_COMMA))
8285 c_parser_consume_token (parser);
8286 else
8287 break;
8289 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8291 else
8293 tree id = c_parser_peek_token (parser)->value;
8294 tree proto = NULL_TREE;
8295 c_parser_consume_token (parser);
8296 if (c_parser_next_token_is (parser, CPP_LESS))
8297 proto = c_parser_objc_protocol_refs (parser);
8298 parser->objc_pq_context = true;
8299 objc_start_protocol (id, proto, attributes);
8300 c_parser_objc_methodprotolist (parser);
8301 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8302 parser->objc_pq_context = false;
8303 objc_finish_interface ();
8307 /* Parse an objc-method-type.
8309 objc-method-type:
8313 Return true if it is a class method (+) and false if it is
8314 an instance method (-).
8316 static inline bool
8317 c_parser_objc_method_type (c_parser *parser)
8319 switch (c_parser_peek_token (parser)->type)
8321 case CPP_PLUS:
8322 c_parser_consume_token (parser);
8323 return true;
8324 case CPP_MINUS:
8325 c_parser_consume_token (parser);
8326 return false;
8327 default:
8328 gcc_unreachable ();
8332 /* Parse an objc-method-definition.
8334 objc-method-definition:
8335 objc-method-type objc-method-decl ;[opt] compound-statement
8338 static void
8339 c_parser_objc_method_definition (c_parser *parser)
8341 bool is_class_method = c_parser_objc_method_type (parser);
8342 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8343 parser->objc_pq_context = true;
8344 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8345 &expr);
8346 if (decl == error_mark_node)
8347 return; /* Bail here. */
8349 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8351 c_parser_consume_token (parser);
8352 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8353 "extra semicolon in method definition specified");
8356 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8358 c_parser_error (parser, "expected %<{%>");
8359 return;
8362 parser->objc_pq_context = false;
8363 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8365 add_stmt (c_parser_compound_statement (parser));
8366 objc_finish_method_definition (current_function_decl);
8368 else
8370 /* This code is executed when we find a method definition
8371 outside of an @implementation context (or invalid for other
8372 reasons). Parse the method (to keep going) but do not emit
8373 any code.
8375 c_parser_compound_statement (parser);
8379 /* Parse an objc-methodprotolist.
8381 objc-methodprotolist:
8382 empty
8383 objc-methodprotolist objc-methodproto
8384 objc-methodprotolist declaration
8385 objc-methodprotolist ;
8386 @optional
8387 @required
8389 The declaration is a data definition, which may be missing
8390 declaration specifiers under the same rules and diagnostics as
8391 other data definitions outside functions, and the stray semicolon
8392 is diagnosed the same way as a stray semicolon outside a
8393 function. */
8395 static void
8396 c_parser_objc_methodprotolist (c_parser *parser)
8398 while (true)
8400 /* The list is terminated by @end. */
8401 switch (c_parser_peek_token (parser)->type)
8403 case CPP_SEMICOLON:
8404 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8405 "ISO C does not allow extra %<;%> outside of a function");
8406 c_parser_consume_token (parser);
8407 break;
8408 case CPP_PLUS:
8409 case CPP_MINUS:
8410 c_parser_objc_methodproto (parser);
8411 break;
8412 case CPP_PRAGMA:
8413 c_parser_pragma (parser, pragma_external);
8414 break;
8415 case CPP_EOF:
8416 return;
8417 default:
8418 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8419 return;
8420 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8421 c_parser_objc_at_property_declaration (parser);
8422 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8424 objc_set_method_opt (true);
8425 c_parser_consume_token (parser);
8427 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8429 objc_set_method_opt (false);
8430 c_parser_consume_token (parser);
8432 else
8433 c_parser_declaration_or_fndef (parser, false, false, true,
8434 false, true, NULL, vNULL);
8435 break;
8440 /* Parse an objc-methodproto.
8442 objc-methodproto:
8443 objc-method-type objc-method-decl ;
8446 static void
8447 c_parser_objc_methodproto (c_parser *parser)
8449 bool is_class_method = c_parser_objc_method_type (parser);
8450 tree decl, attributes = NULL_TREE;
8452 /* Remember protocol qualifiers in prototypes. */
8453 parser->objc_pq_context = true;
8454 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8455 NULL);
8456 /* Forget protocol qualifiers now. */
8457 parser->objc_pq_context = false;
8459 /* Do not allow the presence of attributes to hide an erroneous
8460 method implementation in the interface section. */
8461 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8463 c_parser_error (parser, "expected %<;%>");
8464 return;
8467 if (decl != error_mark_node)
8468 objc_add_method_declaration (is_class_method, decl, attributes);
8470 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8473 /* If we are at a position that method attributes may be present, check that
8474 there are not any parsed already (a syntax error) and then collect any
8475 specified at the current location. Finally, if new attributes were present,
8476 check that the next token is legal ( ';' for decls and '{' for defs). */
8478 static bool
8479 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8481 bool bad = false;
8482 if (*attributes)
8484 c_parser_error (parser,
8485 "method attributes must be specified at the end only");
8486 *attributes = NULL_TREE;
8487 bad = true;
8490 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8491 *attributes = c_parser_attributes (parser);
8493 /* If there were no attributes here, just report any earlier error. */
8494 if (*attributes == NULL_TREE || bad)
8495 return bad;
8497 /* If the attributes are followed by a ; or {, then just report any earlier
8498 error. */
8499 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8500 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8501 return bad;
8503 /* We've got attributes, but not at the end. */
8504 c_parser_error (parser,
8505 "expected %<;%> or %<{%> after method attribute definition");
8506 return true;
8509 /* Parse an objc-method-decl.
8511 objc-method-decl:
8512 ( objc-type-name ) objc-selector
8513 objc-selector
8514 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8515 objc-keyword-selector objc-optparmlist
8516 attributes
8518 objc-keyword-selector:
8519 objc-keyword-decl
8520 objc-keyword-selector objc-keyword-decl
8522 objc-keyword-decl:
8523 objc-selector : ( objc-type-name ) identifier
8524 objc-selector : identifier
8525 : ( objc-type-name ) identifier
8526 : identifier
8528 objc-optparmlist:
8529 objc-optparms objc-optellipsis
8531 objc-optparms:
8532 empty
8533 objc-opt-parms , parameter-declaration
8535 objc-optellipsis:
8536 empty
8537 , ...
8540 static tree
8541 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8542 tree *attributes, tree *expr)
8544 tree type = NULL_TREE;
8545 tree sel;
8546 tree parms = NULL_TREE;
8547 bool ellipsis = false;
8548 bool attr_err = false;
8550 *attributes = NULL_TREE;
8551 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8553 c_parser_consume_token (parser);
8554 type = c_parser_objc_type_name (parser);
8555 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8557 sel = c_parser_objc_selector (parser);
8558 /* If there is no selector, or a colon follows, we have an
8559 objc-keyword-selector. If there is a selector, and a colon does
8560 not follow, that selector ends the objc-method-decl. */
8561 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8563 tree tsel = sel;
8564 tree list = NULL_TREE;
8565 while (true)
8567 tree atype = NULL_TREE, id, keyworddecl;
8568 tree param_attr = NULL_TREE;
8569 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8570 break;
8571 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8573 c_parser_consume_token (parser);
8574 atype = c_parser_objc_type_name (parser);
8575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8576 "expected %<)%>");
8578 /* New ObjC allows attributes on method parameters. */
8579 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8580 param_attr = c_parser_attributes (parser);
8581 if (c_parser_next_token_is_not (parser, CPP_NAME))
8583 c_parser_error (parser, "expected identifier");
8584 return error_mark_node;
8586 id = c_parser_peek_token (parser)->value;
8587 c_parser_consume_token (parser);
8588 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8589 list = chainon (list, keyworddecl);
8590 tsel = c_parser_objc_selector (parser);
8591 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8592 break;
8595 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8597 /* Parse the optional parameter list. Optional Objective-C
8598 method parameters follow the C syntax, and may include '...'
8599 to denote a variable number of arguments. */
8600 parms = make_node (TREE_LIST);
8601 while (c_parser_next_token_is (parser, CPP_COMMA))
8603 struct c_parm *parm;
8604 c_parser_consume_token (parser);
8605 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8607 ellipsis = true;
8608 c_parser_consume_token (parser);
8609 attr_err |= c_parser_objc_maybe_method_attributes
8610 (parser, attributes) ;
8611 break;
8613 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8614 if (parm == NULL)
8615 break;
8616 parms = chainon (parms,
8617 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8619 sel = list;
8621 else
8622 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8624 if (sel == NULL)
8626 c_parser_error (parser, "objective-c method declaration is expected");
8627 return error_mark_node;
8630 if (attr_err)
8631 return error_mark_node;
8633 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8636 /* Parse an objc-type-name.
8638 objc-type-name:
8639 objc-type-qualifiers[opt] type-name
8640 objc-type-qualifiers[opt]
8642 objc-type-qualifiers:
8643 objc-type-qualifier
8644 objc-type-qualifiers objc-type-qualifier
8646 objc-type-qualifier: one of
8647 in out inout bycopy byref oneway
8650 static tree
8651 c_parser_objc_type_name (c_parser *parser)
8653 tree quals = NULL_TREE;
8654 struct c_type_name *type_name = NULL;
8655 tree type = NULL_TREE;
8656 while (true)
8658 c_token *token = c_parser_peek_token (parser);
8659 if (token->type == CPP_KEYWORD
8660 && (token->keyword == RID_IN
8661 || token->keyword == RID_OUT
8662 || token->keyword == RID_INOUT
8663 || token->keyword == RID_BYCOPY
8664 || token->keyword == RID_BYREF
8665 || token->keyword == RID_ONEWAY))
8667 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8668 c_parser_consume_token (parser);
8670 else
8671 break;
8673 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8674 type_name = c_parser_type_name (parser);
8675 if (type_name)
8676 type = groktypename (type_name, NULL, NULL);
8678 /* If the type is unknown, and error has already been produced and
8679 we need to recover from the error. In that case, use NULL_TREE
8680 for the type, as if no type had been specified; this will use the
8681 default type ('id') which is good for error recovery. */
8682 if (type == error_mark_node)
8683 type = NULL_TREE;
8685 return build_tree_list (quals, type);
8688 /* Parse objc-protocol-refs.
8690 objc-protocol-refs:
8691 < identifier-list >
8694 static tree
8695 c_parser_objc_protocol_refs (c_parser *parser)
8697 tree list = NULL_TREE;
8698 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8699 c_parser_consume_token (parser);
8700 /* Any identifiers, including those declared as type names, are OK
8701 here. */
8702 while (true)
8704 tree id;
8705 if (c_parser_next_token_is_not (parser, CPP_NAME))
8707 c_parser_error (parser, "expected identifier");
8708 break;
8710 id = c_parser_peek_token (parser)->value;
8711 list = chainon (list, build_tree_list (NULL_TREE, id));
8712 c_parser_consume_token (parser);
8713 if (c_parser_next_token_is (parser, CPP_COMMA))
8714 c_parser_consume_token (parser);
8715 else
8716 break;
8718 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8719 return list;
8722 /* Parse an objc-try-catch-finally-statement.
8724 objc-try-catch-finally-statement:
8725 @try compound-statement objc-catch-list[opt]
8726 @try compound-statement objc-catch-list[opt] @finally compound-statement
8728 objc-catch-list:
8729 @catch ( objc-catch-parameter-declaration ) compound-statement
8730 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8732 objc-catch-parameter-declaration:
8733 parameter-declaration
8734 '...'
8736 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8738 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8739 for C++. Keep them in sync. */
8741 static void
8742 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8744 location_t location;
8745 tree stmt;
8747 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8748 c_parser_consume_token (parser);
8749 location = c_parser_peek_token (parser)->location;
8750 objc_maybe_warn_exceptions (location);
8751 stmt = c_parser_compound_statement (parser);
8752 objc_begin_try_stmt (location, stmt);
8754 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8756 struct c_parm *parm;
8757 tree parameter_declaration = error_mark_node;
8758 bool seen_open_paren = false;
8760 c_parser_consume_token (parser);
8761 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8762 seen_open_paren = true;
8763 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8765 /* We have "@catch (...)" (where the '...' are literally
8766 what is in the code). Skip the '...'.
8767 parameter_declaration is set to NULL_TREE, and
8768 objc_being_catch_clauses() knows that that means
8769 '...'. */
8770 c_parser_consume_token (parser);
8771 parameter_declaration = NULL_TREE;
8773 else
8775 /* We have "@catch (NSException *exception)" or something
8776 like that. Parse the parameter declaration. */
8777 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8778 if (parm == NULL)
8779 parameter_declaration = error_mark_node;
8780 else
8781 parameter_declaration = grokparm (parm, NULL);
8783 if (seen_open_paren)
8784 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8785 else
8787 /* If there was no open parenthesis, we are recovering from
8788 an error, and we are trying to figure out what mistake
8789 the user has made. */
8791 /* If there is an immediate closing parenthesis, the user
8792 probably forgot the opening one (ie, they typed "@catch
8793 NSException *e)". Parse the closing parenthesis and keep
8794 going. */
8795 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8796 c_parser_consume_token (parser);
8798 /* If these is no immediate closing parenthesis, the user
8799 probably doesn't know that parenthesis are required at
8800 all (ie, they typed "@catch NSException *e"). So, just
8801 forget about the closing parenthesis and keep going. */
8803 objc_begin_catch_clause (parameter_declaration);
8804 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8805 c_parser_compound_statement_nostart (parser);
8806 objc_finish_catch_clause ();
8808 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8810 c_parser_consume_token (parser);
8811 location = c_parser_peek_token (parser)->location;
8812 stmt = c_parser_compound_statement (parser);
8813 objc_build_finally_clause (location, stmt);
8815 objc_finish_try_stmt ();
8818 /* Parse an objc-synchronized-statement.
8820 objc-synchronized-statement:
8821 @synchronized ( expression ) compound-statement
8824 static void
8825 c_parser_objc_synchronized_statement (c_parser *parser)
8827 location_t loc;
8828 tree expr, stmt;
8829 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8830 c_parser_consume_token (parser);
8831 loc = c_parser_peek_token (parser)->location;
8832 objc_maybe_warn_exceptions (loc);
8833 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8835 struct c_expr ce = c_parser_expression (parser);
8836 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8837 expr = ce.value;
8838 expr = c_fully_fold (expr, false, NULL);
8839 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8841 else
8842 expr = error_mark_node;
8843 stmt = c_parser_compound_statement (parser);
8844 objc_build_synchronized (loc, expr, stmt);
8847 /* Parse an objc-selector; return NULL_TREE without an error if the
8848 next token is not an objc-selector.
8850 objc-selector:
8851 identifier
8852 one of
8853 enum struct union if else while do for switch case default
8854 break continue return goto asm sizeof typeof __alignof
8855 unsigned long const short volatile signed restrict _Complex
8856 in out inout bycopy byref oneway int char float double void _Bool
8857 _Atomic
8859 ??? Why this selection of keywords but not, for example, storage
8860 class specifiers? */
8862 static tree
8863 c_parser_objc_selector (c_parser *parser)
8865 c_token *token = c_parser_peek_token (parser);
8866 tree value = token->value;
8867 if (token->type == CPP_NAME)
8869 c_parser_consume_token (parser);
8870 return value;
8872 if (token->type != CPP_KEYWORD)
8873 return NULL_TREE;
8874 switch (token->keyword)
8876 case RID_ENUM:
8877 case RID_STRUCT:
8878 case RID_UNION:
8879 case RID_IF:
8880 case RID_ELSE:
8881 case RID_WHILE:
8882 case RID_DO:
8883 case RID_FOR:
8884 case RID_SWITCH:
8885 case RID_CASE:
8886 case RID_DEFAULT:
8887 case RID_BREAK:
8888 case RID_CONTINUE:
8889 case RID_RETURN:
8890 case RID_GOTO:
8891 case RID_ASM:
8892 case RID_SIZEOF:
8893 case RID_TYPEOF:
8894 case RID_ALIGNOF:
8895 case RID_UNSIGNED:
8896 case RID_LONG:
8897 case RID_INT128:
8898 case RID_CONST:
8899 case RID_SHORT:
8900 case RID_VOLATILE:
8901 case RID_SIGNED:
8902 case RID_RESTRICT:
8903 case RID_COMPLEX:
8904 case RID_IN:
8905 case RID_OUT:
8906 case RID_INOUT:
8907 case RID_BYCOPY:
8908 case RID_BYREF:
8909 case RID_ONEWAY:
8910 case RID_INT:
8911 case RID_CHAR:
8912 case RID_FLOAT:
8913 case RID_DOUBLE:
8914 case RID_VOID:
8915 case RID_BOOL:
8916 case RID_ATOMIC:
8917 case RID_AUTO_TYPE:
8918 c_parser_consume_token (parser);
8919 return value;
8920 default:
8921 return NULL_TREE;
8925 /* Parse an objc-selector-arg.
8927 objc-selector-arg:
8928 objc-selector
8929 objc-keywordname-list
8931 objc-keywordname-list:
8932 objc-keywordname
8933 objc-keywordname-list objc-keywordname
8935 objc-keywordname:
8936 objc-selector :
8940 static tree
8941 c_parser_objc_selector_arg (c_parser *parser)
8943 tree sel = c_parser_objc_selector (parser);
8944 tree list = NULL_TREE;
8945 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
8946 return sel;
8947 while (true)
8949 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8950 return list;
8951 list = chainon (list, build_tree_list (sel, NULL_TREE));
8952 sel = c_parser_objc_selector (parser);
8953 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
8954 break;
8956 return list;
8959 /* Parse an objc-receiver.
8961 objc-receiver:
8962 expression
8963 class-name
8964 type-name
8967 static tree
8968 c_parser_objc_receiver (c_parser *parser)
8970 location_t loc = c_parser_peek_token (parser)->location;
8972 if (c_parser_peek_token (parser)->type == CPP_NAME
8973 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
8974 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
8976 tree id = c_parser_peek_token (parser)->value;
8977 c_parser_consume_token (parser);
8978 return objc_get_class_reference (id);
8980 struct c_expr ce = c_parser_expression (parser);
8981 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8982 return c_fully_fold (ce.value, false, NULL);
8985 /* Parse objc-message-args.
8987 objc-message-args:
8988 objc-selector
8989 objc-keywordarg-list
8991 objc-keywordarg-list:
8992 objc-keywordarg
8993 objc-keywordarg-list objc-keywordarg
8995 objc-keywordarg:
8996 objc-selector : objc-keywordexpr
8997 : objc-keywordexpr
9000 static tree
9001 c_parser_objc_message_args (c_parser *parser)
9003 tree sel = c_parser_objc_selector (parser);
9004 tree list = NULL_TREE;
9005 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9006 return sel;
9007 while (true)
9009 tree keywordexpr;
9010 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9011 return error_mark_node;
9012 keywordexpr = c_parser_objc_keywordexpr (parser);
9013 list = chainon (list, build_tree_list (sel, keywordexpr));
9014 sel = c_parser_objc_selector (parser);
9015 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9016 break;
9018 return list;
9021 /* Parse an objc-keywordexpr.
9023 objc-keywordexpr:
9024 nonempty-expr-list
9027 static tree
9028 c_parser_objc_keywordexpr (c_parser *parser)
9030 tree ret;
9031 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9032 NULL, NULL, NULL, NULL);
9033 if (vec_safe_length (expr_list) == 1)
9035 /* Just return the expression, remove a level of
9036 indirection. */
9037 ret = (*expr_list)[0];
9039 else
9041 /* We have a comma expression, we will collapse later. */
9042 ret = build_tree_list_vec (expr_list);
9044 release_tree_vector (expr_list);
9045 return ret;
9048 /* A check, needed in several places, that ObjC interface, implementation or
9049 method definitions are not prefixed by incorrect items. */
9050 static bool
9051 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9052 struct c_declspecs *specs)
9054 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9055 || specs->typespec_kind != ctsk_none)
9057 c_parser_error (parser,
9058 "no type or storage class may be specified here,");
9059 c_parser_skip_to_end_of_block_or_statement (parser);
9060 return true;
9062 return false;
9065 /* Parse an Objective-C @property declaration. The syntax is:
9067 objc-property-declaration:
9068 '@property' objc-property-attributes[opt] struct-declaration ;
9070 objc-property-attributes:
9071 '(' objc-property-attribute-list ')'
9073 objc-property-attribute-list:
9074 objc-property-attribute
9075 objc-property-attribute-list, objc-property-attribute
9077 objc-property-attribute
9078 'getter' = identifier
9079 'setter' = identifier
9080 'readonly'
9081 'readwrite'
9082 'assign'
9083 'retain'
9084 'copy'
9085 'nonatomic'
9087 For example:
9088 @property NSString *name;
9089 @property (readonly) id object;
9090 @property (retain, nonatomic, getter=getTheName) id name;
9091 @property int a, b, c;
9093 PS: This function is identical to cp_parser_objc_at_propery_declaration
9094 for C++. Keep them in sync. */
9095 static void
9096 c_parser_objc_at_property_declaration (c_parser *parser)
9098 /* The following variables hold the attributes of the properties as
9099 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9100 seen. When we see an attribute, we set them to 'true' (if they
9101 are boolean properties) or to the identifier (if they have an
9102 argument, ie, for getter and setter). Note that here we only
9103 parse the list of attributes, check the syntax and accumulate the
9104 attributes that we find. objc_add_property_declaration() will
9105 then process the information. */
9106 bool property_assign = false;
9107 bool property_copy = false;
9108 tree property_getter_ident = NULL_TREE;
9109 bool property_nonatomic = false;
9110 bool property_readonly = false;
9111 bool property_readwrite = false;
9112 bool property_retain = false;
9113 tree property_setter_ident = NULL_TREE;
9115 /* 'properties' is the list of properties that we read. Usually a
9116 single one, but maybe more (eg, in "@property int a, b, c;" there
9117 are three). */
9118 tree properties;
9119 location_t loc;
9121 loc = c_parser_peek_token (parser)->location;
9122 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9124 c_parser_consume_token (parser); /* Eat '@property'. */
9126 /* Parse the optional attribute list... */
9127 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9129 /* Eat the '(' */
9130 c_parser_consume_token (parser);
9132 /* Property attribute keywords are valid now. */
9133 parser->objc_property_attr_context = true;
9135 while (true)
9137 bool syntax_error = false;
9138 c_token *token = c_parser_peek_token (parser);
9139 enum rid keyword;
9141 if (token->type != CPP_KEYWORD)
9143 if (token->type == CPP_CLOSE_PAREN)
9144 c_parser_error (parser, "expected identifier");
9145 else
9147 c_parser_consume_token (parser);
9148 c_parser_error (parser, "unknown property attribute");
9150 break;
9152 keyword = token->keyword;
9153 c_parser_consume_token (parser);
9154 switch (keyword)
9156 case RID_ASSIGN: property_assign = true; break;
9157 case RID_COPY: property_copy = true; break;
9158 case RID_NONATOMIC: property_nonatomic = true; break;
9159 case RID_READONLY: property_readonly = true; break;
9160 case RID_READWRITE: property_readwrite = true; break;
9161 case RID_RETAIN: property_retain = true; break;
9163 case RID_GETTER:
9164 case RID_SETTER:
9165 if (c_parser_next_token_is_not (parser, CPP_EQ))
9167 if (keyword == RID_GETTER)
9168 c_parser_error (parser,
9169 "missing %<=%> (after %<getter%> attribute)");
9170 else
9171 c_parser_error (parser,
9172 "missing %<=%> (after %<setter%> attribute)");
9173 syntax_error = true;
9174 break;
9176 c_parser_consume_token (parser); /* eat the = */
9177 if (c_parser_next_token_is_not (parser, CPP_NAME))
9179 c_parser_error (parser, "expected identifier");
9180 syntax_error = true;
9181 break;
9183 if (keyword == RID_SETTER)
9185 if (property_setter_ident != NULL_TREE)
9186 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9187 else
9188 property_setter_ident = c_parser_peek_token (parser)->value;
9189 c_parser_consume_token (parser);
9190 if (c_parser_next_token_is_not (parser, CPP_COLON))
9191 c_parser_error (parser, "setter name must terminate with %<:%>");
9192 else
9193 c_parser_consume_token (parser);
9195 else
9197 if (property_getter_ident != NULL_TREE)
9198 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9199 else
9200 property_getter_ident = c_parser_peek_token (parser)->value;
9201 c_parser_consume_token (parser);
9203 break;
9204 default:
9205 c_parser_error (parser, "unknown property attribute");
9206 syntax_error = true;
9207 break;
9210 if (syntax_error)
9211 break;
9213 if (c_parser_next_token_is (parser, CPP_COMMA))
9214 c_parser_consume_token (parser);
9215 else
9216 break;
9218 parser->objc_property_attr_context = false;
9219 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9221 /* ... and the property declaration(s). */
9222 properties = c_parser_struct_declaration (parser);
9224 if (properties == error_mark_node)
9226 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9227 parser->error = false;
9228 return;
9231 if (properties == NULL_TREE)
9232 c_parser_error (parser, "expected identifier");
9233 else
9235 /* Comma-separated properties are chained together in
9236 reverse order; add them one by one. */
9237 properties = nreverse (properties);
9239 for (; properties; properties = TREE_CHAIN (properties))
9240 objc_add_property_declaration (loc, copy_node (properties),
9241 property_readonly, property_readwrite,
9242 property_assign, property_retain,
9243 property_copy, property_nonatomic,
9244 property_getter_ident, property_setter_ident);
9247 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9248 parser->error = false;
9251 /* Parse an Objective-C @synthesize declaration. The syntax is:
9253 objc-synthesize-declaration:
9254 @synthesize objc-synthesize-identifier-list ;
9256 objc-synthesize-identifier-list:
9257 objc-synthesize-identifier
9258 objc-synthesize-identifier-list, objc-synthesize-identifier
9260 objc-synthesize-identifier
9261 identifier
9262 identifier = identifier
9264 For example:
9265 @synthesize MyProperty;
9266 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9268 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9269 for C++. Keep them in sync.
9271 static void
9272 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9274 tree list = NULL_TREE;
9275 location_t loc;
9276 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9277 loc = c_parser_peek_token (parser)->location;
9279 c_parser_consume_token (parser);
9280 while (true)
9282 tree property, ivar;
9283 if (c_parser_next_token_is_not (parser, CPP_NAME))
9285 c_parser_error (parser, "expected identifier");
9286 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9287 /* Once we find the semicolon, we can resume normal parsing.
9288 We have to reset parser->error manually because
9289 c_parser_skip_until_found() won't reset it for us if the
9290 next token is precisely a semicolon. */
9291 parser->error = false;
9292 return;
9294 property = c_parser_peek_token (parser)->value;
9295 c_parser_consume_token (parser);
9296 if (c_parser_next_token_is (parser, CPP_EQ))
9298 c_parser_consume_token (parser);
9299 if (c_parser_next_token_is_not (parser, CPP_NAME))
9301 c_parser_error (parser, "expected identifier");
9302 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9303 parser->error = false;
9304 return;
9306 ivar = c_parser_peek_token (parser)->value;
9307 c_parser_consume_token (parser);
9309 else
9310 ivar = NULL_TREE;
9311 list = chainon (list, build_tree_list (ivar, property));
9312 if (c_parser_next_token_is (parser, CPP_COMMA))
9313 c_parser_consume_token (parser);
9314 else
9315 break;
9317 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9318 objc_add_synthesize_declaration (loc, list);
9321 /* Parse an Objective-C @dynamic declaration. The syntax is:
9323 objc-dynamic-declaration:
9324 @dynamic identifier-list ;
9326 For example:
9327 @dynamic MyProperty;
9328 @dynamic MyProperty, AnotherProperty;
9330 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9331 for C++. Keep them in sync.
9333 static void
9334 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9336 tree list = NULL_TREE;
9337 location_t loc;
9338 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9339 loc = c_parser_peek_token (parser)->location;
9341 c_parser_consume_token (parser);
9342 while (true)
9344 tree property;
9345 if (c_parser_next_token_is_not (parser, CPP_NAME))
9347 c_parser_error (parser, "expected identifier");
9348 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9349 parser->error = false;
9350 return;
9352 property = c_parser_peek_token (parser)->value;
9353 list = chainon (list, build_tree_list (NULL_TREE, property));
9354 c_parser_consume_token (parser);
9355 if (c_parser_next_token_is (parser, CPP_COMMA))
9356 c_parser_consume_token (parser);
9357 else
9358 break;
9360 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9361 objc_add_dynamic_declaration (loc, list);
9365 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9366 should be considered, statements. ALLOW_STMT is true if we're within
9367 the context of a function and such pragmas are to be allowed. Returns
9368 true if we actually parsed such a pragma. */
9370 static bool
9371 c_parser_pragma (c_parser *parser, enum pragma_context context)
9373 unsigned int id;
9375 id = c_parser_peek_token (parser)->pragma_kind;
9376 gcc_assert (id != PRAGMA_NONE);
9378 switch (id)
9380 case PRAGMA_OMP_BARRIER:
9381 if (context != pragma_compound)
9383 if (context == pragma_stmt)
9384 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9385 "used in compound statements");
9386 goto bad_stmt;
9388 c_parser_omp_barrier (parser);
9389 return false;
9391 case PRAGMA_OMP_FLUSH:
9392 if (context != pragma_compound)
9394 if (context == pragma_stmt)
9395 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9396 "used in compound statements");
9397 goto bad_stmt;
9399 c_parser_omp_flush (parser);
9400 return false;
9402 case PRAGMA_OMP_TASKWAIT:
9403 if (context != pragma_compound)
9405 if (context == pragma_stmt)
9406 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9407 "used in compound statements");
9408 goto bad_stmt;
9410 c_parser_omp_taskwait (parser);
9411 return false;
9413 case PRAGMA_OMP_TASKYIELD:
9414 if (context != pragma_compound)
9416 if (context == pragma_stmt)
9417 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9418 "used in compound statements");
9419 goto bad_stmt;
9421 c_parser_omp_taskyield (parser);
9422 return false;
9424 case PRAGMA_OMP_CANCEL:
9425 if (context != pragma_compound)
9427 if (context == pragma_stmt)
9428 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9429 "used in compound statements");
9430 goto bad_stmt;
9432 c_parser_omp_cancel (parser);
9433 return false;
9435 case PRAGMA_OMP_CANCELLATION_POINT:
9436 if (context != pragma_compound)
9438 if (context == pragma_stmt)
9439 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9440 "only be used in compound statements");
9441 goto bad_stmt;
9443 c_parser_omp_cancellation_point (parser);
9444 return false;
9446 case PRAGMA_OMP_THREADPRIVATE:
9447 c_parser_omp_threadprivate (parser);
9448 return false;
9450 case PRAGMA_OMP_TARGET:
9451 return c_parser_omp_target (parser, context);
9453 case PRAGMA_OMP_END_DECLARE_TARGET:
9454 c_parser_omp_end_declare_target (parser);
9455 return false;
9457 case PRAGMA_OMP_SECTION:
9458 error_at (c_parser_peek_token (parser)->location,
9459 "%<#pragma omp section%> may only be used in "
9460 "%<#pragma omp sections%> construct");
9461 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9462 return false;
9464 case PRAGMA_OMP_DECLARE_REDUCTION:
9465 c_parser_omp_declare (parser, context);
9466 return false;
9467 case PRAGMA_IVDEP:
9468 c_parser_consume_pragma (parser);
9469 c_parser_skip_to_pragma_eol (parser);
9470 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9471 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9472 && !c_parser_next_token_is_keyword (parser, RID_DO))
9474 c_parser_error (parser, "for, while or do statement expected");
9475 return false;
9477 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9478 c_parser_for_statement (parser, true);
9479 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9480 c_parser_while_statement (parser, true);
9481 else
9482 c_parser_do_statement (parser, true);
9483 return false;
9485 case PRAGMA_GCC_PCH_PREPROCESS:
9486 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9487 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9488 return false;
9490 case PRAGMA_CILK_SIMD:
9491 if (!c_parser_cilk_verify_simd (parser, context))
9492 return false;
9493 c_parser_consume_pragma (parser);
9494 c_parser_cilk_simd (parser);
9495 return false;
9497 default:
9498 if (id < PRAGMA_FIRST_EXTERNAL)
9500 if (context != pragma_stmt && context != pragma_compound)
9502 bad_stmt:
9503 c_parser_error (parser, "expected declaration specifiers");
9504 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9505 return false;
9507 c_parser_omp_construct (parser);
9508 return true;
9510 break;
9513 c_parser_consume_pragma (parser);
9514 c_invoke_pragma_handler (id);
9516 /* Skip to EOL, but suppress any error message. Those will have been
9517 generated by the handler routine through calling error, as opposed
9518 to calling c_parser_error. */
9519 parser->error = true;
9520 c_parser_skip_to_pragma_eol (parser);
9522 return false;
9525 /* The interface the pragma parsers have to the lexer. */
9527 enum cpp_ttype
9528 pragma_lex (tree *value)
9530 c_token *tok = c_parser_peek_token (the_parser);
9531 enum cpp_ttype ret = tok->type;
9533 *value = tok->value;
9534 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9535 ret = CPP_EOF;
9536 else
9538 if (ret == CPP_KEYWORD)
9539 ret = CPP_NAME;
9540 c_parser_consume_token (the_parser);
9543 return ret;
9546 static void
9547 c_parser_pragma_pch_preprocess (c_parser *parser)
9549 tree name = NULL;
9551 c_parser_consume_pragma (parser);
9552 if (c_parser_next_token_is (parser, CPP_STRING))
9554 name = c_parser_peek_token (parser)->value;
9555 c_parser_consume_token (parser);
9557 else
9558 c_parser_error (parser, "expected string literal");
9559 c_parser_skip_to_pragma_eol (parser);
9561 if (name)
9562 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9565 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
9567 /* Returns name of the next clause.
9568 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9569 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9570 returned and the token is consumed. */
9572 static pragma_omp_clause
9573 c_parser_omp_clause_name (c_parser *parser)
9575 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9577 if (c_parser_next_token_is_keyword (parser, RID_IF))
9578 result = PRAGMA_OMP_CLAUSE_IF;
9579 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9580 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9581 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9582 result = PRAGMA_OMP_CLAUSE_FOR;
9583 else if (c_parser_next_token_is (parser, CPP_NAME))
9585 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9587 switch (p[0])
9589 case 'a':
9590 if (!strcmp ("aligned", p))
9591 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9592 break;
9593 case 'c':
9594 if (!strcmp ("collapse", p))
9595 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9596 else if (!strcmp ("copyin", p))
9597 result = PRAGMA_OMP_CLAUSE_COPYIN;
9598 else if (!strcmp ("copyprivate", p))
9599 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9600 break;
9601 case 'd':
9602 if (!strcmp ("depend", p))
9603 result = PRAGMA_OMP_CLAUSE_DEPEND;
9604 else if (!strcmp ("device", p))
9605 result = PRAGMA_OMP_CLAUSE_DEVICE;
9606 else if (!strcmp ("dist_schedule", p))
9607 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9608 break;
9609 case 'f':
9610 if (!strcmp ("final", p))
9611 result = PRAGMA_OMP_CLAUSE_FINAL;
9612 else if (!strcmp ("firstprivate", p))
9613 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9614 else if (!strcmp ("from", p))
9615 result = PRAGMA_OMP_CLAUSE_FROM;
9616 break;
9617 case 'i':
9618 if (!strcmp ("inbranch", p))
9619 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9620 break;
9621 case 'l':
9622 if (!strcmp ("lastprivate", p))
9623 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9624 else if (!strcmp ("linear", p))
9625 result = PRAGMA_OMP_CLAUSE_LINEAR;
9626 break;
9627 case 'm':
9628 if (!strcmp ("map", p))
9629 result = PRAGMA_OMP_CLAUSE_MAP;
9630 else if (!strcmp ("mergeable", p))
9631 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9632 else if (flag_cilkplus && !strcmp ("mask", p))
9633 result = PRAGMA_CILK_CLAUSE_MASK;
9634 break;
9635 case 'n':
9636 if (!strcmp ("notinbranch", p))
9637 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9638 else if (!strcmp ("nowait", p))
9639 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9640 else if (!strcmp ("num_teams", p))
9641 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9642 else if (!strcmp ("num_threads", p))
9643 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9644 else if (flag_cilkplus && !strcmp ("nomask", p))
9645 result = PRAGMA_CILK_CLAUSE_NOMASK;
9646 break;
9647 case 'o':
9648 if (!strcmp ("ordered", p))
9649 result = PRAGMA_OMP_CLAUSE_ORDERED;
9650 break;
9651 case 'p':
9652 if (!strcmp ("parallel", p))
9653 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9654 else if (!strcmp ("private", p))
9655 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9656 else if (!strcmp ("proc_bind", p))
9657 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9658 break;
9659 case 'r':
9660 if (!strcmp ("reduction", p))
9661 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9662 break;
9663 case 's':
9664 if (!strcmp ("safelen", p))
9665 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9666 else if (!strcmp ("schedule", p))
9667 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9668 else if (!strcmp ("sections", p))
9669 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9670 else if (!strcmp ("shared", p))
9671 result = PRAGMA_OMP_CLAUSE_SHARED;
9672 else if (!strcmp ("simdlen", p))
9673 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9674 break;
9675 case 't':
9676 if (!strcmp ("taskgroup", p))
9677 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9678 else if (!strcmp ("thread_limit", p))
9679 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9680 else if (!strcmp ("to", p))
9681 result = PRAGMA_OMP_CLAUSE_TO;
9682 break;
9683 case 'u':
9684 if (!strcmp ("uniform", p))
9685 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9686 else if (!strcmp ("untied", p))
9687 result = PRAGMA_OMP_CLAUSE_UNTIED;
9688 break;
9689 case 'v':
9690 if (flag_cilkplus && !strcmp ("vectorlength", p))
9691 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9692 break;
9696 if (result != PRAGMA_OMP_CLAUSE_NONE)
9697 c_parser_consume_token (parser);
9699 return result;
9702 /* Validate that a clause of the given type does not already exist. */
9704 static void
9705 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9706 const char *name)
9708 tree c;
9710 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9711 if (OMP_CLAUSE_CODE (c) == code)
9713 location_t loc = OMP_CLAUSE_LOCATION (c);
9714 error_at (loc, "too many %qs clauses", name);
9715 break;
9719 /* OpenMP 2.5:
9720 variable-list:
9721 identifier
9722 variable-list , identifier
9724 If KIND is nonzero, create the appropriate node and install the
9725 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
9726 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
9728 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
9729 return the list created. */
9731 static tree
9732 c_parser_omp_variable_list (c_parser *parser,
9733 location_t clause_loc,
9734 enum omp_clause_code kind, tree list)
9736 if (c_parser_next_token_is_not (parser, CPP_NAME)
9737 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
9738 c_parser_error (parser, "expected identifier");
9740 while (c_parser_next_token_is (parser, CPP_NAME)
9741 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
9743 tree t = lookup_name (c_parser_peek_token (parser)->value);
9745 if (t == NULL_TREE)
9747 undeclared_variable (c_parser_peek_token (parser)->location,
9748 c_parser_peek_token (parser)->value);
9749 t = error_mark_node;
9752 c_parser_consume_token (parser);
9754 if (t == error_mark_node)
9756 else if (kind != 0)
9758 switch (kind)
9760 case OMP_CLAUSE_MAP:
9761 case OMP_CLAUSE_FROM:
9762 case OMP_CLAUSE_TO:
9763 case OMP_CLAUSE_DEPEND:
9764 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
9766 tree low_bound = NULL_TREE, length = NULL_TREE;
9768 c_parser_consume_token (parser);
9769 if (!c_parser_next_token_is (parser, CPP_COLON))
9770 low_bound = c_parser_expression (parser).value;
9771 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9772 length = integer_one_node;
9773 else
9775 /* Look for `:'. */
9776 if (!c_parser_require (parser, CPP_COLON,
9777 "expected %<:%>"))
9779 t = error_mark_node;
9780 break;
9782 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
9783 length = c_parser_expression (parser).value;
9785 /* Look for the closing `]'. */
9786 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
9787 "expected %<]%>"))
9789 t = error_mark_node;
9790 break;
9792 t = tree_cons (low_bound, length, t);
9794 break;
9795 default:
9796 break;
9799 if (t != error_mark_node)
9801 tree u = build_omp_clause (clause_loc, kind);
9802 OMP_CLAUSE_DECL (u) = t;
9803 OMP_CLAUSE_CHAIN (u) = list;
9804 list = u;
9807 else
9808 list = tree_cons (t, NULL_TREE, list);
9810 if (c_parser_next_token_is_not (parser, CPP_COMMA))
9811 break;
9813 c_parser_consume_token (parser);
9816 return list;
9819 /* Similarly, but expect leading and trailing parenthesis. This is a very
9820 common case for omp clauses. */
9822 static tree
9823 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
9824 tree list)
9826 /* The clauses location. */
9827 location_t loc = c_parser_peek_token (parser)->location;
9829 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9831 list = c_parser_omp_variable_list (parser, loc, kind, list);
9832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9834 return list;
9837 /* OpenMP 3.0:
9838 collapse ( constant-expression ) */
9840 static tree
9841 c_parser_omp_clause_collapse (c_parser *parser, tree list)
9843 tree c, num = error_mark_node;
9844 HOST_WIDE_INT n;
9845 location_t loc;
9847 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
9849 loc = c_parser_peek_token (parser)->location;
9850 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9852 num = c_parser_expr_no_commas (parser, NULL).value;
9853 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9855 if (num == error_mark_node)
9856 return list;
9857 mark_exp_read (num);
9858 num = c_fully_fold (num, false, NULL);
9859 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
9860 || !tree_fits_shwi_p (num)
9861 || (n = tree_to_shwi (num)) <= 0
9862 || (int) n != n)
9864 error_at (loc,
9865 "collapse argument needs positive constant integer expression");
9866 return list;
9868 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
9869 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
9870 OMP_CLAUSE_CHAIN (c) = list;
9871 return c;
9874 /* OpenMP 2.5:
9875 copyin ( variable-list ) */
9877 static tree
9878 c_parser_omp_clause_copyin (c_parser *parser, tree list)
9880 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
9883 /* OpenMP 2.5:
9884 copyprivate ( variable-list ) */
9886 static tree
9887 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
9889 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
9892 /* OpenMP 2.5:
9893 default ( shared | none ) */
9895 static tree
9896 c_parser_omp_clause_default (c_parser *parser, tree list)
9898 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
9899 location_t loc = c_parser_peek_token (parser)->location;
9900 tree c;
9902 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9903 return list;
9904 if (c_parser_next_token_is (parser, CPP_NAME))
9906 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9908 switch (p[0])
9910 case 'n':
9911 if (strcmp ("none", p) != 0)
9912 goto invalid_kind;
9913 kind = OMP_CLAUSE_DEFAULT_NONE;
9914 break;
9916 case 's':
9917 if (strcmp ("shared", p) != 0)
9918 goto invalid_kind;
9919 kind = OMP_CLAUSE_DEFAULT_SHARED;
9920 break;
9922 default:
9923 goto invalid_kind;
9926 c_parser_consume_token (parser);
9928 else
9930 invalid_kind:
9931 c_parser_error (parser, "expected %<none%> or %<shared%>");
9933 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9935 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
9936 return list;
9938 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
9939 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
9940 OMP_CLAUSE_CHAIN (c) = list;
9941 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
9943 return c;
9946 /* OpenMP 2.5:
9947 firstprivate ( variable-list ) */
9949 static tree
9950 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
9952 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
9955 /* OpenMP 3.1:
9956 final ( expression ) */
9958 static tree
9959 c_parser_omp_clause_final (c_parser *parser, tree list)
9961 location_t loc = c_parser_peek_token (parser)->location;
9962 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9964 tree t = c_parser_paren_condition (parser);
9965 tree c;
9967 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
9969 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
9970 OMP_CLAUSE_FINAL_EXPR (c) = t;
9971 OMP_CLAUSE_CHAIN (c) = list;
9972 list = c;
9974 else
9975 c_parser_error (parser, "expected %<(%>");
9977 return list;
9980 /* OpenMP 2.5:
9981 if ( expression ) */
9983 static tree
9984 c_parser_omp_clause_if (c_parser *parser, tree list)
9986 location_t loc = c_parser_peek_token (parser)->location;
9987 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9989 tree t = c_parser_paren_condition (parser);
9990 tree c;
9992 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
9994 c = build_omp_clause (loc, OMP_CLAUSE_IF);
9995 OMP_CLAUSE_IF_EXPR (c) = t;
9996 OMP_CLAUSE_CHAIN (c) = list;
9997 list = c;
9999 else
10000 c_parser_error (parser, "expected %<(%>");
10002 return list;
10005 /* OpenMP 2.5:
10006 lastprivate ( variable-list ) */
10008 static tree
10009 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10011 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10014 /* OpenMP 3.1:
10015 mergeable */
10017 static tree
10018 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10020 tree c;
10022 /* FIXME: Should we allow duplicates? */
10023 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10025 c = build_omp_clause (c_parser_peek_token (parser)->location,
10026 OMP_CLAUSE_MERGEABLE);
10027 OMP_CLAUSE_CHAIN (c) = list;
10029 return c;
10032 /* OpenMP 2.5:
10033 nowait */
10035 static tree
10036 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10038 tree c;
10039 location_t loc = c_parser_peek_token (parser)->location;
10041 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10043 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10044 OMP_CLAUSE_CHAIN (c) = list;
10045 return c;
10048 /* OpenMP 2.5:
10049 num_threads ( expression ) */
10051 static tree
10052 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10054 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10055 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10057 location_t expr_loc = c_parser_peek_token (parser)->location;
10058 tree c, t = c_parser_expression (parser).value;
10059 mark_exp_read (t);
10060 t = c_fully_fold (t, false, NULL);
10062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10064 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10066 c_parser_error (parser, "expected integer expression");
10067 return list;
10070 /* Attempt to statically determine when the number isn't positive. */
10071 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10072 build_int_cst (TREE_TYPE (t), 0));
10073 if (CAN_HAVE_LOCATION_P (c))
10074 SET_EXPR_LOCATION (c, expr_loc);
10075 if (c == boolean_true_node)
10077 warning_at (expr_loc, 0,
10078 "%<num_threads%> value must be positive");
10079 t = integer_one_node;
10082 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10084 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10085 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10086 OMP_CLAUSE_CHAIN (c) = list;
10087 list = c;
10090 return list;
10093 /* OpenMP 2.5:
10094 ordered */
10096 static tree
10097 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10099 tree c;
10101 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10103 c = build_omp_clause (c_parser_peek_token (parser)->location,
10104 OMP_CLAUSE_ORDERED);
10105 OMP_CLAUSE_CHAIN (c) = list;
10107 return c;
10110 /* OpenMP 2.5:
10111 private ( variable-list ) */
10113 static tree
10114 c_parser_omp_clause_private (c_parser *parser, tree list)
10116 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10119 /* OpenMP 2.5:
10120 reduction ( reduction-operator : variable-list )
10122 reduction-operator:
10123 One of: + * - & ^ | && ||
10125 OpenMP 3.1:
10127 reduction-operator:
10128 One of: + * - & ^ | && || max min
10130 OpenMP 4.0:
10132 reduction-operator:
10133 One of: + * - & ^ | && ||
10134 identifier */
10136 static tree
10137 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10139 location_t clause_loc = c_parser_peek_token (parser)->location;
10140 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10142 enum tree_code code = ERROR_MARK;
10143 tree reduc_id = NULL_TREE;
10145 switch (c_parser_peek_token (parser)->type)
10147 case CPP_PLUS:
10148 code = PLUS_EXPR;
10149 break;
10150 case CPP_MULT:
10151 code = MULT_EXPR;
10152 break;
10153 case CPP_MINUS:
10154 code = MINUS_EXPR;
10155 break;
10156 case CPP_AND:
10157 code = BIT_AND_EXPR;
10158 break;
10159 case CPP_XOR:
10160 code = BIT_XOR_EXPR;
10161 break;
10162 case CPP_OR:
10163 code = BIT_IOR_EXPR;
10164 break;
10165 case CPP_AND_AND:
10166 code = TRUTH_ANDIF_EXPR;
10167 break;
10168 case CPP_OR_OR:
10169 code = TRUTH_ORIF_EXPR;
10170 break;
10171 case CPP_NAME:
10173 const char *p
10174 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10175 if (strcmp (p, "min") == 0)
10177 code = MIN_EXPR;
10178 break;
10180 if (strcmp (p, "max") == 0)
10182 code = MAX_EXPR;
10183 break;
10185 reduc_id = c_parser_peek_token (parser)->value;
10186 break;
10188 default:
10189 c_parser_error (parser,
10190 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10191 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10193 return list;
10195 c_parser_consume_token (parser);
10196 reduc_id = c_omp_reduction_id (code, reduc_id);
10197 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10199 tree nl, c;
10201 nl = c_parser_omp_variable_list (parser, clause_loc,
10202 OMP_CLAUSE_REDUCTION, list);
10203 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10205 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10206 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10207 if (code == ERROR_MARK
10208 || !(INTEGRAL_TYPE_P (type)
10209 || TREE_CODE (type) == REAL_TYPE
10210 || TREE_CODE (type) == COMPLEX_TYPE))
10211 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10212 = c_omp_reduction_lookup (reduc_id,
10213 TYPE_MAIN_VARIANT (type));
10216 list = nl;
10218 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10220 return list;
10223 /* OpenMP 2.5:
10224 schedule ( schedule-kind )
10225 schedule ( schedule-kind , expression )
10227 schedule-kind:
10228 static | dynamic | guided | runtime | auto
10231 static tree
10232 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10234 tree c, t;
10235 location_t loc = c_parser_peek_token (parser)->location;
10237 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10238 return list;
10240 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10242 if (c_parser_next_token_is (parser, CPP_NAME))
10244 tree kind = c_parser_peek_token (parser)->value;
10245 const char *p = IDENTIFIER_POINTER (kind);
10247 switch (p[0])
10249 case 'd':
10250 if (strcmp ("dynamic", p) != 0)
10251 goto invalid_kind;
10252 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10253 break;
10255 case 'g':
10256 if (strcmp ("guided", p) != 0)
10257 goto invalid_kind;
10258 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10259 break;
10261 case 'r':
10262 if (strcmp ("runtime", p) != 0)
10263 goto invalid_kind;
10264 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10265 break;
10267 default:
10268 goto invalid_kind;
10271 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10272 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10273 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10274 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10275 else
10276 goto invalid_kind;
10278 c_parser_consume_token (parser);
10279 if (c_parser_next_token_is (parser, CPP_COMMA))
10281 location_t here;
10282 c_parser_consume_token (parser);
10284 here = c_parser_peek_token (parser)->location;
10285 t = c_parser_expr_no_commas (parser, NULL).value;
10286 mark_exp_read (t);
10287 t = c_fully_fold (t, false, NULL);
10289 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10290 error_at (here, "schedule %<runtime%> does not take "
10291 "a %<chunk_size%> parameter");
10292 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10293 error_at (here,
10294 "schedule %<auto%> does not take "
10295 "a %<chunk_size%> parameter");
10296 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10297 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10298 else
10299 c_parser_error (parser, "expected integer expression");
10301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10303 else
10304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10305 "expected %<,%> or %<)%>");
10307 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10308 OMP_CLAUSE_CHAIN (c) = list;
10309 return c;
10311 invalid_kind:
10312 c_parser_error (parser, "invalid schedule kind");
10313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10314 return list;
10317 /* OpenMP 2.5:
10318 shared ( variable-list ) */
10320 static tree
10321 c_parser_omp_clause_shared (c_parser *parser, tree list)
10323 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10326 /* OpenMP 3.0:
10327 untied */
10329 static tree
10330 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10332 tree c;
10334 /* FIXME: Should we allow duplicates? */
10335 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10337 c = build_omp_clause (c_parser_peek_token (parser)->location,
10338 OMP_CLAUSE_UNTIED);
10339 OMP_CLAUSE_CHAIN (c) = list;
10341 return c;
10344 /* OpenMP 4.0:
10345 inbranch
10346 notinbranch */
10348 static tree
10349 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10350 enum omp_clause_code code, tree list)
10352 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10354 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10355 OMP_CLAUSE_CHAIN (c) = list;
10357 return c;
10360 /* OpenMP 4.0:
10361 parallel
10363 sections
10364 taskgroup */
10366 static tree
10367 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10368 enum omp_clause_code code, tree list)
10370 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10371 OMP_CLAUSE_CHAIN (c) = list;
10373 return c;
10376 /* OpenMP 4.0:
10377 num_teams ( expression ) */
10379 static tree
10380 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10382 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10383 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10385 location_t expr_loc = c_parser_peek_token (parser)->location;
10386 tree c, t = c_parser_expression (parser).value;
10387 mark_exp_read (t);
10388 t = c_fully_fold (t, false, NULL);
10390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10392 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10394 c_parser_error (parser, "expected integer expression");
10395 return list;
10398 /* Attempt to statically determine when the number isn't positive. */
10399 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10400 build_int_cst (TREE_TYPE (t), 0));
10401 if (CAN_HAVE_LOCATION_P (c))
10402 SET_EXPR_LOCATION (c, expr_loc);
10403 if (c == boolean_true_node)
10405 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
10406 t = integer_one_node;
10409 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
10411 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
10412 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
10413 OMP_CLAUSE_CHAIN (c) = list;
10414 list = c;
10417 return list;
10420 /* OpenMP 4.0:
10421 thread_limit ( expression ) */
10423 static tree
10424 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
10426 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10427 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10429 location_t expr_loc = c_parser_peek_token (parser)->location;
10430 tree c, t = c_parser_expression (parser).value;
10431 mark_exp_read (t);
10432 t = c_fully_fold (t, false, NULL);
10434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10436 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10438 c_parser_error (parser, "expected integer expression");
10439 return list;
10442 /* Attempt to statically determine when the number isn't positive. */
10443 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10444 build_int_cst (TREE_TYPE (t), 0));
10445 if (CAN_HAVE_LOCATION_P (c))
10446 SET_EXPR_LOCATION (c, expr_loc);
10447 if (c == boolean_true_node)
10449 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
10450 t = integer_one_node;
10453 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
10454 "thread_limit");
10456 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_THREAD_LIMIT);
10457 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
10458 OMP_CLAUSE_CHAIN (c) = list;
10459 list = c;
10462 return list;
10465 /* OpenMP 4.0:
10466 aligned ( variable-list )
10467 aligned ( variable-list : constant-expression ) */
10469 static tree
10470 c_parser_omp_clause_aligned (c_parser *parser, tree list)
10472 location_t clause_loc = c_parser_peek_token (parser)->location;
10473 tree nl, c;
10475 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10476 return list;
10478 nl = c_parser_omp_variable_list (parser, clause_loc,
10479 OMP_CLAUSE_ALIGNED, list);
10481 if (c_parser_next_token_is (parser, CPP_COLON))
10483 c_parser_consume_token (parser);
10484 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
10485 mark_exp_read (alignment);
10486 alignment = c_fully_fold (alignment, false, NULL);
10487 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
10488 && TREE_CODE (alignment) != INTEGER_CST
10489 && tree_int_cst_sgn (alignment) != 1)
10491 error_at (clause_loc, "%<aligned%> clause alignment expression must "
10492 "be positive constant integer expression");
10493 alignment = NULL_TREE;
10496 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10497 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
10500 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10501 return nl;
10504 /* OpenMP 4.0:
10505 linear ( variable-list )
10506 linear ( variable-list : expression ) */
10508 static tree
10509 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
10511 location_t clause_loc = c_parser_peek_token (parser)->location;
10512 tree nl, c, step;
10514 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10515 return list;
10517 nl = c_parser_omp_variable_list (parser, clause_loc,
10518 OMP_CLAUSE_LINEAR, list);
10520 if (c_parser_next_token_is (parser, CPP_COLON))
10522 c_parser_consume_token (parser);
10523 step = c_parser_expression (parser).value;
10524 mark_exp_read (step);
10525 step = c_fully_fold (step, false, NULL);
10526 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
10528 sorry ("using parameters for %<linear%> step is not supported yet");
10529 step = integer_one_node;
10531 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
10533 error_at (clause_loc, "%<linear%> clause step expression must "
10534 "be integral");
10535 step = integer_one_node;
10539 else
10540 step = integer_one_node;
10542 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10544 OMP_CLAUSE_LINEAR_STEP (c) = step;
10547 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10548 return nl;
10551 /* OpenMP 4.0:
10552 safelen ( constant-expression ) */
10554 static tree
10555 c_parser_omp_clause_safelen (c_parser *parser, tree list)
10557 location_t clause_loc = c_parser_peek_token (parser)->location;
10558 tree c, t;
10560 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10561 return list;
10563 t = c_parser_expr_no_commas (parser, NULL).value;
10564 mark_exp_read (t);
10565 t = c_fully_fold (t, false, NULL);
10566 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10567 && TREE_CODE (t) != INTEGER_CST
10568 && tree_int_cst_sgn (t) != 1)
10570 error_at (clause_loc, "%<safelen%> clause expression must "
10571 "be positive constant integer expression");
10572 t = NULL_TREE;
10575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10576 if (t == NULL_TREE || t == error_mark_node)
10577 return list;
10579 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
10581 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
10582 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
10583 OMP_CLAUSE_CHAIN (c) = list;
10584 return c;
10587 /* OpenMP 4.0:
10588 simdlen ( constant-expression ) */
10590 static tree
10591 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
10593 location_t clause_loc = c_parser_peek_token (parser)->location;
10594 tree c, t;
10596 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10597 return list;
10599 t = c_parser_expr_no_commas (parser, NULL).value;
10600 mark_exp_read (t);
10601 t = c_fully_fold (t, false, NULL);
10602 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
10603 && TREE_CODE (t) != INTEGER_CST
10604 && tree_int_cst_sgn (t) != 1)
10606 error_at (clause_loc, "%<simdlen%> clause expression must "
10607 "be positive constant integer expression");
10608 t = NULL_TREE;
10611 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10612 if (t == NULL_TREE || t == error_mark_node)
10613 return list;
10615 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
10617 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
10618 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
10619 OMP_CLAUSE_CHAIN (c) = list;
10620 return c;
10623 /* OpenMP 4.0:
10624 depend ( depend-kind: variable-list )
10626 depend-kind:
10627 in | out | inout */
10629 static tree
10630 c_parser_omp_clause_depend (c_parser *parser, tree list)
10632 location_t clause_loc = c_parser_peek_token (parser)->location;
10633 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
10634 tree nl, c;
10636 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10637 return list;
10639 if (c_parser_next_token_is (parser, CPP_NAME))
10641 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10642 if (strcmp ("in", p) == 0)
10643 kind = OMP_CLAUSE_DEPEND_IN;
10644 else if (strcmp ("inout", p) == 0)
10645 kind = OMP_CLAUSE_DEPEND_INOUT;
10646 else if (strcmp ("out", p) == 0)
10647 kind = OMP_CLAUSE_DEPEND_OUT;
10648 else
10649 goto invalid_kind;
10651 else
10652 goto invalid_kind;
10654 c_parser_consume_token (parser);
10655 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10656 goto resync_fail;
10658 nl = c_parser_omp_variable_list (parser, clause_loc,
10659 OMP_CLAUSE_DEPEND, list);
10661 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10662 OMP_CLAUSE_DEPEND_KIND (c) = kind;
10664 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10665 return nl;
10667 invalid_kind:
10668 c_parser_error (parser, "invalid depend kind");
10669 resync_fail:
10670 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10671 return list;
10674 /* OpenMP 4.0:
10675 map ( map-kind: variable-list )
10676 map ( variable-list )
10678 map-kind:
10679 alloc | to | from | tofrom */
10681 static tree
10682 c_parser_omp_clause_map (c_parser *parser, tree list)
10684 location_t clause_loc = c_parser_peek_token (parser)->location;
10685 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
10686 tree nl, c;
10688 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10689 return list;
10691 if (c_parser_next_token_is (parser, CPP_NAME)
10692 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10694 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10695 if (strcmp ("alloc", p) == 0)
10696 kind = OMP_CLAUSE_MAP_ALLOC;
10697 else if (strcmp ("to", p) == 0)
10698 kind = OMP_CLAUSE_MAP_TO;
10699 else if (strcmp ("from", p) == 0)
10700 kind = OMP_CLAUSE_MAP_FROM;
10701 else if (strcmp ("tofrom", p) == 0)
10702 kind = OMP_CLAUSE_MAP_TOFROM;
10703 else
10705 c_parser_error (parser, "invalid map kind");
10706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10707 "expected %<)%>");
10708 return list;
10710 c_parser_consume_token (parser);
10711 c_parser_consume_token (parser);
10714 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
10716 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10717 OMP_CLAUSE_MAP_KIND (c) = kind;
10719 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10720 return nl;
10723 /* OpenMP 4.0:
10724 device ( expression ) */
10726 static tree
10727 c_parser_omp_clause_device (c_parser *parser, tree list)
10729 location_t clause_loc = c_parser_peek_token (parser)->location;
10730 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10732 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
10733 mark_exp_read (t);
10734 t = c_fully_fold (t, false, NULL);
10736 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10738 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10740 c_parser_error (parser, "expected integer expression");
10741 return list;
10744 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
10746 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
10747 OMP_CLAUSE_DEVICE_ID (c) = t;
10748 OMP_CLAUSE_CHAIN (c) = list;
10749 list = c;
10752 return list;
10755 /* OpenMP 4.0:
10756 dist_schedule ( static )
10757 dist_schedule ( static , expression ) */
10759 static tree
10760 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
10762 tree c, t = NULL_TREE;
10763 location_t loc = c_parser_peek_token (parser)->location;
10765 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10766 return list;
10768 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
10770 c_parser_error (parser, "invalid dist_schedule kind");
10771 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10772 "expected %<)%>");
10773 return list;
10776 c_parser_consume_token (parser);
10777 if (c_parser_next_token_is (parser, CPP_COMMA))
10779 c_parser_consume_token (parser);
10781 t = c_parser_expr_no_commas (parser, NULL).value;
10782 mark_exp_read (t);
10783 t = c_fully_fold (t, false, NULL);
10784 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10786 else
10787 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10788 "expected %<,%> or %<)%>");
10790 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10791 if (t == error_mark_node)
10792 return list;
10794 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
10795 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
10796 OMP_CLAUSE_CHAIN (c) = list;
10797 return c;
10800 /* OpenMP 4.0:
10801 proc_bind ( proc-bind-kind )
10803 proc-bind-kind:
10804 master | close | spread */
10806 static tree
10807 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
10809 location_t clause_loc = c_parser_peek_token (parser)->location;
10810 enum omp_clause_proc_bind_kind kind;
10811 tree c;
10813 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10814 return list;
10816 if (c_parser_next_token_is (parser, CPP_NAME))
10818 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10819 if (strcmp ("master", p) == 0)
10820 kind = OMP_CLAUSE_PROC_BIND_MASTER;
10821 else if (strcmp ("close", p) == 0)
10822 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
10823 else if (strcmp ("spread", p) == 0)
10824 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
10825 else
10826 goto invalid_kind;
10828 else
10829 goto invalid_kind;
10831 c_parser_consume_token (parser);
10832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10833 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
10834 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
10835 OMP_CLAUSE_CHAIN (c) = list;
10836 return c;
10838 invalid_kind:
10839 c_parser_error (parser, "invalid proc_bind kind");
10840 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10841 return list;
10844 /* OpenMP 4.0:
10845 to ( variable-list ) */
10847 static tree
10848 c_parser_omp_clause_to (c_parser *parser, tree list)
10850 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
10853 /* OpenMP 4.0:
10854 from ( variable-list ) */
10856 static tree
10857 c_parser_omp_clause_from (c_parser *parser, tree list)
10859 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
10862 /* OpenMP 4.0:
10863 uniform ( variable-list ) */
10865 static tree
10866 c_parser_omp_clause_uniform (c_parser *parser, tree list)
10868 /* The clauses location. */
10869 location_t loc = c_parser_peek_token (parser)->location;
10871 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10873 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
10874 list);
10875 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10877 return list;
10880 /* Parse all OpenMP clauses. The set clauses allowed by the directive
10881 is a bitmask in MASK. Return the list of clauses found; the result
10882 of clause default goes in *pdefault. */
10884 static tree
10885 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
10886 const char *where, bool finish_p = true)
10888 tree clauses = NULL;
10889 bool first = true, cilk_simd_fn = false;
10891 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
10893 location_t here;
10894 pragma_omp_clause c_kind;
10895 const char *c_name;
10896 tree prev = clauses;
10898 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
10899 c_parser_consume_token (parser);
10901 here = c_parser_peek_token (parser)->location;
10902 c_kind = c_parser_omp_clause_name (parser);
10904 switch (c_kind)
10906 case PRAGMA_OMP_CLAUSE_COLLAPSE:
10907 clauses = c_parser_omp_clause_collapse (parser, clauses);
10908 c_name = "collapse";
10909 break;
10910 case PRAGMA_OMP_CLAUSE_COPYIN:
10911 clauses = c_parser_omp_clause_copyin (parser, clauses);
10912 c_name = "copyin";
10913 break;
10914 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
10915 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
10916 c_name = "copyprivate";
10917 break;
10918 case PRAGMA_OMP_CLAUSE_DEFAULT:
10919 clauses = c_parser_omp_clause_default (parser, clauses);
10920 c_name = "default";
10921 break;
10922 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
10923 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
10924 c_name = "firstprivate";
10925 break;
10926 case PRAGMA_OMP_CLAUSE_FINAL:
10927 clauses = c_parser_omp_clause_final (parser, clauses);
10928 c_name = "final";
10929 break;
10930 case PRAGMA_OMP_CLAUSE_IF:
10931 clauses = c_parser_omp_clause_if (parser, clauses);
10932 c_name = "if";
10933 break;
10934 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
10935 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
10936 c_name = "lastprivate";
10937 break;
10938 case PRAGMA_OMP_CLAUSE_MERGEABLE:
10939 clauses = c_parser_omp_clause_mergeable (parser, clauses);
10940 c_name = "mergeable";
10941 break;
10942 case PRAGMA_OMP_CLAUSE_NOWAIT:
10943 clauses = c_parser_omp_clause_nowait (parser, clauses);
10944 c_name = "nowait";
10945 break;
10946 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
10947 clauses = c_parser_omp_clause_num_threads (parser, clauses);
10948 c_name = "num_threads";
10949 break;
10950 case PRAGMA_OMP_CLAUSE_ORDERED:
10951 clauses = c_parser_omp_clause_ordered (parser, clauses);
10952 c_name = "ordered";
10953 break;
10954 case PRAGMA_OMP_CLAUSE_PRIVATE:
10955 clauses = c_parser_omp_clause_private (parser, clauses);
10956 c_name = "private";
10957 break;
10958 case PRAGMA_OMP_CLAUSE_REDUCTION:
10959 clauses = c_parser_omp_clause_reduction (parser, clauses);
10960 c_name = "reduction";
10961 break;
10962 case PRAGMA_OMP_CLAUSE_SCHEDULE:
10963 clauses = c_parser_omp_clause_schedule (parser, clauses);
10964 c_name = "schedule";
10965 break;
10966 case PRAGMA_OMP_CLAUSE_SHARED:
10967 clauses = c_parser_omp_clause_shared (parser, clauses);
10968 c_name = "shared";
10969 break;
10970 case PRAGMA_OMP_CLAUSE_UNTIED:
10971 clauses = c_parser_omp_clause_untied (parser, clauses);
10972 c_name = "untied";
10973 break;
10974 case PRAGMA_OMP_CLAUSE_INBRANCH:
10975 case PRAGMA_CILK_CLAUSE_MASK:
10976 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
10977 clauses);
10978 c_name = "inbranch";
10979 break;
10980 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
10981 case PRAGMA_CILK_CLAUSE_NOMASK:
10982 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
10983 clauses);
10984 c_name = "notinbranch";
10985 break;
10986 case PRAGMA_OMP_CLAUSE_PARALLEL:
10987 clauses
10988 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
10989 clauses);
10990 c_name = "parallel";
10991 if (!first)
10993 clause_not_first:
10994 error_at (here, "%qs must be the first clause of %qs",
10995 c_name, where);
10996 clauses = prev;
10998 break;
10999 case PRAGMA_OMP_CLAUSE_FOR:
11000 clauses
11001 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11002 clauses);
11003 c_name = "for";
11004 if (!first)
11005 goto clause_not_first;
11006 break;
11007 case PRAGMA_OMP_CLAUSE_SECTIONS:
11008 clauses
11009 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11010 clauses);
11011 c_name = "sections";
11012 if (!first)
11013 goto clause_not_first;
11014 break;
11015 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11016 clauses
11017 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11018 clauses);
11019 c_name = "taskgroup";
11020 if (!first)
11021 goto clause_not_first;
11022 break;
11023 case PRAGMA_OMP_CLAUSE_TO:
11024 clauses = c_parser_omp_clause_to (parser, clauses);
11025 c_name = "to";
11026 break;
11027 case PRAGMA_OMP_CLAUSE_FROM:
11028 clauses = c_parser_omp_clause_from (parser, clauses);
11029 c_name = "from";
11030 break;
11031 case PRAGMA_OMP_CLAUSE_UNIFORM:
11032 clauses = c_parser_omp_clause_uniform (parser, clauses);
11033 c_name = "uniform";
11034 break;
11035 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11036 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11037 c_name = "num_teams";
11038 break;
11039 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11040 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11041 c_name = "thread_limit";
11042 break;
11043 case PRAGMA_OMP_CLAUSE_ALIGNED:
11044 clauses = c_parser_omp_clause_aligned (parser, clauses);
11045 c_name = "aligned";
11046 break;
11047 case PRAGMA_OMP_CLAUSE_LINEAR:
11048 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11049 cilk_simd_fn = true;
11050 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11051 c_name = "linear";
11052 break;
11053 case PRAGMA_OMP_CLAUSE_DEPEND:
11054 clauses = c_parser_omp_clause_depend (parser, clauses);
11055 c_name = "depend";
11056 break;
11057 case PRAGMA_OMP_CLAUSE_MAP:
11058 clauses = c_parser_omp_clause_map (parser, clauses);
11059 c_name = "map";
11060 break;
11061 case PRAGMA_OMP_CLAUSE_DEVICE:
11062 clauses = c_parser_omp_clause_device (parser, clauses);
11063 c_name = "device";
11064 break;
11065 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11066 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11067 c_name = "dist_schedule";
11068 break;
11069 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11070 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11071 c_name = "proc_bind";
11072 break;
11073 case PRAGMA_OMP_CLAUSE_SAFELEN:
11074 clauses = c_parser_omp_clause_safelen (parser, clauses);
11075 c_name = "safelen";
11076 break;
11077 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11078 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11079 c_name = "simdlen";
11080 break;
11081 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11082 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11083 c_name = "simdlen";
11084 break;
11085 default:
11086 c_parser_error (parser, "expected %<#pragma omp%> clause");
11087 goto saw_error;
11090 first = false;
11092 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11094 /* Remove the invalid clause(s) from the list to avoid
11095 confusing the rest of the compiler. */
11096 clauses = prev;
11097 error_at (here, "%qs is not valid for %qs", c_name, where);
11101 saw_error:
11102 c_parser_skip_to_pragma_eol (parser);
11104 if (finish_p)
11105 return c_finish_omp_clauses (clauses);
11107 return clauses;
11110 /* OpenMP 2.5:
11111 structured-block:
11112 statement
11114 In practice, we're also interested in adding the statement to an
11115 outer node. So it is convenient if we work around the fact that
11116 c_parser_statement calls add_stmt. */
11118 static tree
11119 c_parser_omp_structured_block (c_parser *parser)
11121 tree stmt = push_stmt_list ();
11122 c_parser_statement (parser);
11123 return pop_stmt_list (stmt);
11126 /* OpenMP 2.5:
11127 # pragma omp atomic new-line
11128 expression-stmt
11130 expression-stmt:
11131 x binop= expr | x++ | ++x | x-- | --x
11132 binop:
11133 +, *, -, /, &, ^, |, <<, >>
11135 where x is an lvalue expression with scalar type.
11137 OpenMP 3.1:
11138 # pragma omp atomic new-line
11139 update-stmt
11141 # pragma omp atomic read new-line
11142 read-stmt
11144 # pragma omp atomic write new-line
11145 write-stmt
11147 # pragma omp atomic update new-line
11148 update-stmt
11150 # pragma omp atomic capture new-line
11151 capture-stmt
11153 # pragma omp atomic capture new-line
11154 capture-block
11156 read-stmt:
11157 v = x
11158 write-stmt:
11159 x = expr
11160 update-stmt:
11161 expression-stmt | x = x binop expr
11162 capture-stmt:
11163 v = expression-stmt
11164 capture-block:
11165 { v = x; update-stmt; } | { update-stmt; v = x; }
11167 OpenMP 4.0:
11168 update-stmt:
11169 expression-stmt | x = x binop expr | x = expr binop x
11170 capture-stmt:
11171 v = update-stmt
11172 capture-block:
11173 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
11175 where x and v are lvalue expressions with scalar type.
11177 LOC is the location of the #pragma token. */
11179 static void
11180 c_parser_omp_atomic (location_t loc, c_parser *parser)
11182 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
11183 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
11184 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
11185 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
11186 struct c_expr expr;
11187 location_t eloc;
11188 bool structured_block = false;
11189 bool swapped = false;
11190 bool seq_cst = false;
11192 if (c_parser_next_token_is (parser, CPP_NAME))
11194 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11195 if (!strcmp (p, "seq_cst"))
11197 seq_cst = true;
11198 c_parser_consume_token (parser);
11199 if (c_parser_next_token_is (parser, CPP_COMMA)
11200 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11201 c_parser_consume_token (parser);
11204 if (c_parser_next_token_is (parser, CPP_NAME))
11206 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11208 if (!strcmp (p, "read"))
11209 code = OMP_ATOMIC_READ;
11210 else if (!strcmp (p, "write"))
11211 code = NOP_EXPR;
11212 else if (!strcmp (p, "update"))
11213 code = OMP_ATOMIC;
11214 else if (!strcmp (p, "capture"))
11215 code = OMP_ATOMIC_CAPTURE_NEW;
11216 else
11217 p = NULL;
11218 if (p)
11219 c_parser_consume_token (parser);
11221 if (!seq_cst)
11223 if (c_parser_next_token_is (parser, CPP_COMMA)
11224 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11225 c_parser_consume_token (parser);
11227 if (c_parser_next_token_is (parser, CPP_NAME))
11229 const char *p
11230 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11231 if (!strcmp (p, "seq_cst"))
11233 seq_cst = true;
11234 c_parser_consume_token (parser);
11238 c_parser_skip_to_pragma_eol (parser);
11240 switch (code)
11242 case OMP_ATOMIC_READ:
11243 case NOP_EXPR: /* atomic write */
11244 v = c_parser_unary_expression (parser).value;
11245 v = c_fully_fold (v, false, NULL);
11246 if (v == error_mark_node)
11247 goto saw_error;
11248 loc = c_parser_peek_token (parser)->location;
11249 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11250 goto saw_error;
11251 if (code == NOP_EXPR)
11252 lhs = c_parser_expression (parser).value;
11253 else
11254 lhs = c_parser_unary_expression (parser).value;
11255 lhs = c_fully_fold (lhs, false, NULL);
11256 if (lhs == error_mark_node)
11257 goto saw_error;
11258 if (code == NOP_EXPR)
11260 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
11261 opcode. */
11262 code = OMP_ATOMIC;
11263 rhs = lhs;
11264 lhs = v;
11265 v = NULL_TREE;
11267 goto done;
11268 case OMP_ATOMIC_CAPTURE_NEW:
11269 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11271 c_parser_consume_token (parser);
11272 structured_block = true;
11274 else
11276 v = c_parser_unary_expression (parser).value;
11277 v = c_fully_fold (v, false, NULL);
11278 if (v == error_mark_node)
11279 goto saw_error;
11280 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11281 goto saw_error;
11283 break;
11284 default:
11285 break;
11288 /* For structured_block case we don't know yet whether
11289 old or new x should be captured. */
11290 restart:
11291 eloc = c_parser_peek_token (parser)->location;
11292 expr = c_parser_unary_expression (parser);
11293 lhs = expr.value;
11294 expr = default_function_array_conversion (eloc, expr);
11295 unfolded_lhs = expr.value;
11296 lhs = c_fully_fold (lhs, false, NULL);
11297 orig_lhs = lhs;
11298 switch (TREE_CODE (lhs))
11300 case ERROR_MARK:
11301 saw_error:
11302 c_parser_skip_to_end_of_block_or_statement (parser);
11303 if (structured_block)
11305 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11306 c_parser_consume_token (parser);
11307 else if (code == OMP_ATOMIC_CAPTURE_NEW)
11309 c_parser_skip_to_end_of_block_or_statement (parser);
11310 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11311 c_parser_consume_token (parser);
11314 return;
11316 case POSTINCREMENT_EXPR:
11317 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11318 code = OMP_ATOMIC_CAPTURE_OLD;
11319 /* FALLTHROUGH */
11320 case PREINCREMENT_EXPR:
11321 lhs = TREE_OPERAND (lhs, 0);
11322 unfolded_lhs = NULL_TREE;
11323 opcode = PLUS_EXPR;
11324 rhs = integer_one_node;
11325 break;
11327 case POSTDECREMENT_EXPR:
11328 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
11329 code = OMP_ATOMIC_CAPTURE_OLD;
11330 /* FALLTHROUGH */
11331 case PREDECREMENT_EXPR:
11332 lhs = TREE_OPERAND (lhs, 0);
11333 unfolded_lhs = NULL_TREE;
11334 opcode = MINUS_EXPR;
11335 rhs = integer_one_node;
11336 break;
11338 case COMPOUND_EXPR:
11339 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
11340 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
11341 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
11342 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
11343 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
11344 (TREE_OPERAND (lhs, 1), 0), 0)))
11345 == BOOLEAN_TYPE)
11346 /* Undo effects of boolean_increment for post {in,de}crement. */
11347 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
11348 /* FALLTHRU */
11349 case MODIFY_EXPR:
11350 if (TREE_CODE (lhs) == MODIFY_EXPR
11351 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
11353 /* Undo effects of boolean_increment. */
11354 if (integer_onep (TREE_OPERAND (lhs, 1)))
11356 /* This is pre or post increment. */
11357 rhs = TREE_OPERAND (lhs, 1);
11358 lhs = TREE_OPERAND (lhs, 0);
11359 unfolded_lhs = NULL_TREE;
11360 opcode = NOP_EXPR;
11361 if (code == OMP_ATOMIC_CAPTURE_NEW
11362 && !structured_block
11363 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11364 code = OMP_ATOMIC_CAPTURE_OLD;
11365 break;
11367 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
11368 && TREE_OPERAND (lhs, 0)
11369 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
11371 /* This is pre or post decrement. */
11372 rhs = TREE_OPERAND (lhs, 1);
11373 lhs = TREE_OPERAND (lhs, 0);
11374 unfolded_lhs = NULL_TREE;
11375 opcode = NOP_EXPR;
11376 if (code == OMP_ATOMIC_CAPTURE_NEW
11377 && !structured_block
11378 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
11379 code = OMP_ATOMIC_CAPTURE_OLD;
11380 break;
11383 /* FALLTHRU */
11384 default:
11385 switch (c_parser_peek_token (parser)->type)
11387 case CPP_MULT_EQ:
11388 opcode = MULT_EXPR;
11389 break;
11390 case CPP_DIV_EQ:
11391 opcode = TRUNC_DIV_EXPR;
11392 break;
11393 case CPP_PLUS_EQ:
11394 opcode = PLUS_EXPR;
11395 break;
11396 case CPP_MINUS_EQ:
11397 opcode = MINUS_EXPR;
11398 break;
11399 case CPP_LSHIFT_EQ:
11400 opcode = LSHIFT_EXPR;
11401 break;
11402 case CPP_RSHIFT_EQ:
11403 opcode = RSHIFT_EXPR;
11404 break;
11405 case CPP_AND_EQ:
11406 opcode = BIT_AND_EXPR;
11407 break;
11408 case CPP_OR_EQ:
11409 opcode = BIT_IOR_EXPR;
11410 break;
11411 case CPP_XOR_EQ:
11412 opcode = BIT_XOR_EXPR;
11413 break;
11414 case CPP_EQ:
11415 c_parser_consume_token (parser);
11416 eloc = c_parser_peek_token (parser)->location;
11417 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
11418 rhs1 = expr.value;
11419 switch (TREE_CODE (rhs1))
11421 case MULT_EXPR:
11422 case TRUNC_DIV_EXPR:
11423 case PLUS_EXPR:
11424 case MINUS_EXPR:
11425 case LSHIFT_EXPR:
11426 case RSHIFT_EXPR:
11427 case BIT_AND_EXPR:
11428 case BIT_IOR_EXPR:
11429 case BIT_XOR_EXPR:
11430 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
11432 opcode = TREE_CODE (rhs1);
11433 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11434 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11435 goto stmt_done;
11437 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
11439 opcode = TREE_CODE (rhs1);
11440 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
11441 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
11442 swapped = !commutative_tree_code (opcode);
11443 goto stmt_done;
11445 break;
11446 case ERROR_MARK:
11447 goto saw_error;
11448 default:
11449 break;
11451 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
11453 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11455 code = OMP_ATOMIC_CAPTURE_OLD;
11456 v = lhs;
11457 lhs = NULL_TREE;
11458 expr = default_function_array_read_conversion (eloc, expr);
11459 unfolded_lhs1 = expr.value;
11460 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
11461 rhs1 = NULL_TREE;
11462 c_parser_consume_token (parser);
11463 goto restart;
11465 if (structured_block)
11467 opcode = NOP_EXPR;
11468 expr = default_function_array_read_conversion (eloc, expr);
11469 rhs = c_fully_fold (expr.value, false, NULL);
11470 rhs1 = NULL_TREE;
11471 goto stmt_done;
11474 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
11475 goto saw_error;
11476 default:
11477 c_parser_error (parser,
11478 "invalid operator for %<#pragma omp atomic%>");
11479 goto saw_error;
11482 /* Arrange to pass the location of the assignment operator to
11483 c_finish_omp_atomic. */
11484 loc = c_parser_peek_token (parser)->location;
11485 c_parser_consume_token (parser);
11486 eloc = c_parser_peek_token (parser)->location;
11487 expr = c_parser_expression (parser);
11488 expr = default_function_array_read_conversion (eloc, expr);
11489 rhs = expr.value;
11490 rhs = c_fully_fold (rhs, false, NULL);
11491 break;
11493 stmt_done:
11494 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
11496 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
11497 goto saw_error;
11498 v = c_parser_unary_expression (parser).value;
11499 v = c_fully_fold (v, false, NULL);
11500 if (v == error_mark_node)
11501 goto saw_error;
11502 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
11503 goto saw_error;
11504 eloc = c_parser_peek_token (parser)->location;
11505 expr = c_parser_unary_expression (parser);
11506 lhs1 = expr.value;
11507 expr = default_function_array_read_conversion (eloc, expr);
11508 unfolded_lhs1 = expr.value;
11509 lhs1 = c_fully_fold (lhs1, false, NULL);
11510 if (lhs1 == error_mark_node)
11511 goto saw_error;
11513 if (structured_block)
11515 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11516 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
11518 done:
11519 if (unfolded_lhs && unfolded_lhs1
11520 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
11522 error ("%<#pragma omp atomic capture%> uses two different "
11523 "expressions for memory");
11524 stmt = error_mark_node;
11526 else
11527 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
11528 swapped, seq_cst);
11529 if (stmt != error_mark_node)
11530 add_stmt (stmt);
11532 if (!structured_block)
11533 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11537 /* OpenMP 2.5:
11538 # pragma omp barrier new-line
11541 static void
11542 c_parser_omp_barrier (c_parser *parser)
11544 location_t loc = c_parser_peek_token (parser)->location;
11545 c_parser_consume_pragma (parser);
11546 c_parser_skip_to_pragma_eol (parser);
11548 c_finish_omp_barrier (loc);
11551 /* OpenMP 2.5:
11552 # pragma omp critical [(name)] new-line
11553 structured-block
11555 LOC is the location of the #pragma itself. */
11557 static tree
11558 c_parser_omp_critical (location_t loc, c_parser *parser)
11560 tree stmt, name = NULL;
11562 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11564 c_parser_consume_token (parser);
11565 if (c_parser_next_token_is (parser, CPP_NAME))
11567 name = c_parser_peek_token (parser)->value;
11568 c_parser_consume_token (parser);
11569 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11571 else
11572 c_parser_error (parser, "expected identifier");
11574 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11575 c_parser_error (parser, "expected %<(%> or end of line");
11576 c_parser_skip_to_pragma_eol (parser);
11578 stmt = c_parser_omp_structured_block (parser);
11579 return c_finish_omp_critical (loc, stmt, name);
11582 /* OpenMP 2.5:
11583 # pragma omp flush flush-vars[opt] new-line
11585 flush-vars:
11586 ( variable-list ) */
11588 static void
11589 c_parser_omp_flush (c_parser *parser)
11591 location_t loc = c_parser_peek_token (parser)->location;
11592 c_parser_consume_pragma (parser);
11593 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11594 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11595 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11596 c_parser_error (parser, "expected %<(%> or end of line");
11597 c_parser_skip_to_pragma_eol (parser);
11599 c_finish_omp_flush (loc);
11602 /* Parse the restricted form of the for statement allowed by OpenMP.
11603 The real trick here is to determine the loop control variable early
11604 so that we can push a new decl if necessary to make it private.
11605 LOC is the location of the OMP in "#pragma omp". */
11607 static tree
11608 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
11609 tree clauses, tree *cclauses)
11611 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
11612 tree declv, condv, incrv, initv, ret = NULL;
11613 bool fail = false, open_brace_parsed = false;
11614 int i, collapse = 1, nbraces = 0;
11615 location_t for_loc;
11616 vec<tree, va_gc> *for_block = make_tree_vector ();
11618 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
11619 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
11620 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
11622 gcc_assert (collapse >= 1);
11624 declv = make_tree_vec (collapse);
11625 initv = make_tree_vec (collapse);
11626 condv = make_tree_vec (collapse);
11627 incrv = make_tree_vec (collapse);
11629 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
11631 c_parser_error (parser, "for statement expected");
11632 return NULL;
11634 for_loc = c_parser_peek_token (parser)->location;
11635 c_parser_consume_token (parser);
11637 for (i = 0; i < collapse; i++)
11639 int bracecount = 0;
11641 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11642 goto pop_scopes;
11644 /* Parse the initialization declaration or expression. */
11645 if (c_parser_next_tokens_start_declaration (parser))
11647 if (i > 0)
11648 vec_safe_push (for_block, c_begin_compound_stmt (true));
11649 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
11650 NULL, vNULL);
11651 decl = check_for_loop_decls (for_loc, flag_isoc99);
11652 if (decl == NULL)
11653 goto error_init;
11654 if (DECL_INITIAL (decl) == error_mark_node)
11655 decl = error_mark_node;
11656 init = decl;
11658 else if (c_parser_next_token_is (parser, CPP_NAME)
11659 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
11661 struct c_expr decl_exp;
11662 struct c_expr init_exp;
11663 location_t init_loc;
11665 decl_exp = c_parser_postfix_expression (parser);
11666 decl = decl_exp.value;
11668 c_parser_require (parser, CPP_EQ, "expected %<=%>");
11670 init_loc = c_parser_peek_token (parser)->location;
11671 init_exp = c_parser_expr_no_commas (parser, NULL);
11672 init_exp = default_function_array_read_conversion (init_loc,
11673 init_exp);
11674 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
11675 NOP_EXPR, init_loc, init_exp.value,
11676 init_exp.original_type);
11677 init = c_process_expr_stmt (init_loc, init);
11679 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11681 else
11683 error_init:
11684 c_parser_error (parser,
11685 "expected iteration declaration or initialization");
11686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11687 "expected %<)%>");
11688 fail = true;
11689 goto parse_next;
11692 /* Parse the loop condition. */
11693 cond = NULL_TREE;
11694 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
11696 location_t cond_loc = c_parser_peek_token (parser)->location;
11697 struct c_expr cond_expr
11698 = c_parser_binary_expression (parser, NULL, NULL_TREE);
11700 cond = cond_expr.value;
11701 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
11702 cond = c_fully_fold (cond, false, NULL);
11703 switch (cond_expr.original_code)
11705 case GT_EXPR:
11706 case GE_EXPR:
11707 case LT_EXPR:
11708 case LE_EXPR:
11709 break;
11710 case NE_EXPR:
11711 if (code == CILK_SIMD)
11712 break;
11713 /* FALLTHRU. */
11714 default:
11715 /* Can't be cond = error_mark_node, because we want to preserve
11716 the location until c_finish_omp_for. */
11717 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
11718 break;
11720 protected_set_expr_location (cond, cond_loc);
11722 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11724 /* Parse the increment expression. */
11725 incr = NULL_TREE;
11726 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
11728 location_t incr_loc = c_parser_peek_token (parser)->location;
11730 incr = c_process_expr_stmt (incr_loc,
11731 c_parser_expression (parser).value);
11733 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11735 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
11736 fail = true;
11737 else
11739 TREE_VEC_ELT (declv, i) = decl;
11740 TREE_VEC_ELT (initv, i) = init;
11741 TREE_VEC_ELT (condv, i) = cond;
11742 TREE_VEC_ELT (incrv, i) = incr;
11745 parse_next:
11746 if (i == collapse - 1)
11747 break;
11749 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
11750 in between the collapsed for loops to be still considered perfectly
11751 nested. Hopefully the final version clarifies this.
11752 For now handle (multiple) {'s and empty statements. */
11755 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11757 c_parser_consume_token (parser);
11758 break;
11760 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
11762 c_parser_consume_token (parser);
11763 bracecount++;
11765 else if (bracecount
11766 && c_parser_next_token_is (parser, CPP_SEMICOLON))
11767 c_parser_consume_token (parser);
11768 else
11770 c_parser_error (parser, "not enough perfectly nested loops");
11771 if (bracecount)
11773 open_brace_parsed = true;
11774 bracecount--;
11776 fail = true;
11777 collapse = 0;
11778 break;
11781 while (1);
11783 nbraces += bracecount;
11786 save_break = c_break_label;
11787 if (code == CILK_SIMD)
11788 c_break_label = build_int_cst (size_type_node, 2);
11789 else
11790 c_break_label = size_one_node;
11791 save_cont = c_cont_label;
11792 c_cont_label = NULL_TREE;
11793 body = push_stmt_list ();
11795 if (open_brace_parsed)
11797 location_t here = c_parser_peek_token (parser)->location;
11798 stmt = c_begin_compound_stmt (true);
11799 c_parser_compound_statement_nostart (parser);
11800 add_stmt (c_end_compound_stmt (here, stmt, true));
11802 else
11803 add_stmt (c_parser_c99_block_statement (parser));
11804 if (c_cont_label)
11806 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
11807 SET_EXPR_LOCATION (t, loc);
11808 add_stmt (t);
11811 body = pop_stmt_list (body);
11812 c_break_label = save_break;
11813 c_cont_label = save_cont;
11815 while (nbraces)
11817 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
11819 c_parser_consume_token (parser);
11820 nbraces--;
11822 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
11823 c_parser_consume_token (parser);
11824 else
11826 c_parser_error (parser, "collapsed loops not perfectly nested");
11827 while (nbraces)
11829 location_t here = c_parser_peek_token (parser)->location;
11830 stmt = c_begin_compound_stmt (true);
11831 add_stmt (body);
11832 c_parser_compound_statement_nostart (parser);
11833 body = c_end_compound_stmt (here, stmt, true);
11834 nbraces--;
11836 goto pop_scopes;
11840 /* Only bother calling c_finish_omp_for if we haven't already generated
11841 an error from the initialization parsing. */
11842 if (!fail)
11844 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
11845 incrv, body, NULL);
11846 if (stmt)
11848 if (cclauses != NULL
11849 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
11851 tree *c;
11852 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
11853 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
11854 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
11855 c = &OMP_CLAUSE_CHAIN (*c);
11856 else
11858 for (i = 0; i < collapse; i++)
11859 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
11860 break;
11861 if (i == collapse)
11862 c = &OMP_CLAUSE_CHAIN (*c);
11863 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
11865 error_at (loc,
11866 "iteration variable %qD should not be firstprivate",
11867 OMP_CLAUSE_DECL (*c));
11868 *c = OMP_CLAUSE_CHAIN (*c);
11870 else
11872 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
11873 change it to shared (decl) in
11874 OMP_PARALLEL_CLAUSES. */
11875 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
11876 OMP_CLAUSE_LASTPRIVATE);
11877 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
11878 if (code == OMP_SIMD)
11880 OMP_CLAUSE_CHAIN (l)
11881 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
11882 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
11884 else
11886 OMP_CLAUSE_CHAIN (l) = clauses;
11887 clauses = l;
11889 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
11893 OMP_FOR_CLAUSES (stmt) = clauses;
11895 ret = stmt;
11897 pop_scopes:
11898 while (!for_block->is_empty ())
11900 /* FIXME diagnostics: LOC below should be the actual location of
11901 this particular for block. We need to build a list of
11902 locations to go along with FOR_BLOCK. */
11903 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
11904 add_stmt (stmt);
11906 release_tree_vector (for_block);
11907 return ret;
11910 /* Helper function for OpenMP parsing, split clauses and call
11911 finish_omp_clauses on each of the set of clauses afterwards. */
11913 static void
11914 omp_split_clauses (location_t loc, enum tree_code code,
11915 omp_clause_mask mask, tree clauses, tree *cclauses)
11917 int i;
11918 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
11919 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
11920 if (cclauses[i])
11921 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
11924 /* OpenMP 4.0:
11925 #pragma omp simd simd-clause[optseq] new-line
11926 for-loop
11928 LOC is the location of the #pragma token.
11931 #define OMP_SIMD_CLAUSE_MASK \
11932 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
11933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
11934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
11935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
11940 static tree
11941 c_parser_omp_simd (location_t loc, c_parser *parser,
11942 char *p_name, omp_clause_mask mask, tree *cclauses)
11944 tree block, clauses, ret;
11946 strcat (p_name, " simd");
11947 mask |= OMP_SIMD_CLAUSE_MASK;
11948 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
11950 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
11951 if (cclauses)
11953 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
11954 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
11957 block = c_begin_compound_stmt (true);
11958 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
11959 block = c_end_compound_stmt (loc, block, true);
11960 add_stmt (block);
11962 return ret;
11965 /* OpenMP 2.5:
11966 #pragma omp for for-clause[optseq] new-line
11967 for-loop
11969 OpenMP 4.0:
11970 #pragma omp for simd for-simd-clause[optseq] new-line
11971 for-loop
11973 LOC is the location of the #pragma token.
11976 #define OMP_FOR_CLAUSE_MASK \
11977 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
11978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
11979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
11980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
11982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
11983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
11984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
11986 static tree
11987 c_parser_omp_for (location_t loc, c_parser *parser,
11988 char *p_name, omp_clause_mask mask, tree *cclauses)
11990 tree block, clauses, ret;
11992 strcat (p_name, " for");
11993 mask |= OMP_FOR_CLAUSE_MASK;
11994 if (cclauses)
11995 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
11997 if (c_parser_next_token_is (parser, CPP_NAME))
11999 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12001 if (strcmp (p, "simd") == 0)
12003 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12004 if (cclauses == NULL)
12005 cclauses = cclauses_buf;
12007 c_parser_consume_token (parser);
12008 if (!flag_openmp) /* flag_openmp_simd */
12009 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12010 block = c_begin_compound_stmt (true);
12011 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12012 block = c_end_compound_stmt (loc, block, true);
12013 if (ret == NULL_TREE)
12014 return ret;
12015 ret = make_node (OMP_FOR);
12016 TREE_TYPE (ret) = void_type_node;
12017 OMP_FOR_BODY (ret) = block;
12018 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12019 SET_EXPR_LOCATION (ret, loc);
12020 add_stmt (ret);
12021 return ret;
12024 if (!flag_openmp) /* flag_openmp_simd */
12026 c_parser_skip_to_pragma_eol (parser);
12027 return NULL_TREE;
12030 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12031 if (cclauses)
12033 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
12034 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12037 block = c_begin_compound_stmt (true);
12038 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
12039 block = c_end_compound_stmt (loc, block, true);
12040 add_stmt (block);
12042 return ret;
12045 /* OpenMP 2.5:
12046 # pragma omp master new-line
12047 structured-block
12049 LOC is the location of the #pragma token.
12052 static tree
12053 c_parser_omp_master (location_t loc, c_parser *parser)
12055 c_parser_skip_to_pragma_eol (parser);
12056 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
12059 /* OpenMP 2.5:
12060 # pragma omp ordered new-line
12061 structured-block
12063 LOC is the location of the #pragma itself.
12066 static tree
12067 c_parser_omp_ordered (location_t loc, c_parser *parser)
12069 c_parser_skip_to_pragma_eol (parser);
12070 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
12073 /* OpenMP 2.5:
12075 section-scope:
12076 { section-sequence }
12078 section-sequence:
12079 section-directive[opt] structured-block
12080 section-sequence section-directive structured-block
12082 SECTIONS_LOC is the location of the #pragma omp sections. */
12084 static tree
12085 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
12087 tree stmt, substmt;
12088 bool error_suppress = false;
12089 location_t loc;
12091 loc = c_parser_peek_token (parser)->location;
12092 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
12094 /* Avoid skipping until the end of the block. */
12095 parser->error = false;
12096 return NULL_TREE;
12099 stmt = push_stmt_list ();
12101 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
12103 substmt = c_parser_omp_structured_block (parser);
12104 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12105 SET_EXPR_LOCATION (substmt, loc);
12106 add_stmt (substmt);
12109 while (1)
12111 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12112 break;
12113 if (c_parser_next_token_is (parser, CPP_EOF))
12114 break;
12116 loc = c_parser_peek_token (parser)->location;
12117 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
12119 c_parser_consume_pragma (parser);
12120 c_parser_skip_to_pragma_eol (parser);
12121 error_suppress = false;
12123 else if (!error_suppress)
12125 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
12126 error_suppress = true;
12129 substmt = c_parser_omp_structured_block (parser);
12130 substmt = build1 (OMP_SECTION, void_type_node, substmt);
12131 SET_EXPR_LOCATION (substmt, loc);
12132 add_stmt (substmt);
12134 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
12135 "expected %<#pragma omp section%> or %<}%>");
12137 substmt = pop_stmt_list (stmt);
12139 stmt = make_node (OMP_SECTIONS);
12140 SET_EXPR_LOCATION (stmt, sections_loc);
12141 TREE_TYPE (stmt) = void_type_node;
12142 OMP_SECTIONS_BODY (stmt) = substmt;
12144 return add_stmt (stmt);
12147 /* OpenMP 2.5:
12148 # pragma omp sections sections-clause[optseq] newline
12149 sections-scope
12151 LOC is the location of the #pragma token.
12154 #define OMP_SECTIONS_CLAUSE_MASK \
12155 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12161 static tree
12162 c_parser_omp_sections (location_t loc, c_parser *parser,
12163 char *p_name, omp_clause_mask mask, tree *cclauses)
12165 tree block, clauses, ret;
12167 strcat (p_name, " sections");
12168 mask |= OMP_SECTIONS_CLAUSE_MASK;
12169 if (cclauses)
12170 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12172 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12173 if (cclauses)
12175 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
12176 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
12179 block = c_begin_compound_stmt (true);
12180 ret = c_parser_omp_sections_scope (loc, parser);
12181 if (ret)
12182 OMP_SECTIONS_CLAUSES (ret) = clauses;
12183 block = c_end_compound_stmt (loc, block, true);
12184 add_stmt (block);
12186 return ret;
12189 /* OpenMP 2.5:
12190 # pragma omp parallel parallel-clause[optseq] new-line
12191 structured-block
12192 # pragma omp parallel for parallel-for-clause[optseq] new-line
12193 structured-block
12194 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
12195 structured-block
12197 OpenMP 4.0:
12198 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
12199 structured-block
12201 LOC is the location of the #pragma token.
12204 #define OMP_PARALLEL_CLAUSE_MASK \
12205 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
12211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
12213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
12215 static tree
12216 c_parser_omp_parallel (location_t loc, c_parser *parser,
12217 char *p_name, omp_clause_mask mask, tree *cclauses)
12219 tree stmt, clauses, block;
12221 strcat (p_name, " parallel");
12222 mask |= OMP_PARALLEL_CLAUSE_MASK;
12224 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12226 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12227 if (cclauses == NULL)
12228 cclauses = cclauses_buf;
12230 c_parser_consume_token (parser);
12231 if (!flag_openmp) /* flag_openmp_simd */
12232 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12233 block = c_begin_omp_parallel ();
12234 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
12235 stmt
12236 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12237 block);
12238 if (ret == NULL_TREE)
12239 return ret;
12240 OMP_PARALLEL_COMBINED (stmt) = 1;
12241 return stmt;
12243 else if (cclauses)
12245 error_at (loc, "expected %<for%> after %qs", p_name);
12246 c_parser_skip_to_pragma_eol (parser);
12247 return NULL_TREE;
12249 else if (!flag_openmp) /* flag_openmp_simd */
12251 c_parser_skip_to_pragma_eol (parser);
12252 return NULL_TREE;
12254 else if (c_parser_next_token_is (parser, CPP_NAME))
12256 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12257 if (strcmp (p, "sections") == 0)
12259 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12260 if (cclauses == NULL)
12261 cclauses = cclauses_buf;
12263 c_parser_consume_token (parser);
12264 block = c_begin_omp_parallel ();
12265 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
12266 stmt = c_finish_omp_parallel (loc,
12267 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
12268 block);
12269 OMP_PARALLEL_COMBINED (stmt) = 1;
12270 return stmt;
12274 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12276 block = c_begin_omp_parallel ();
12277 c_parser_statement (parser);
12278 stmt = c_finish_omp_parallel (loc, clauses, block);
12280 return stmt;
12283 /* OpenMP 2.5:
12284 # pragma omp single single-clause[optseq] new-line
12285 structured-block
12287 LOC is the location of the #pragma.
12290 #define OMP_SINGLE_CLAUSE_MASK \
12291 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
12294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12296 static tree
12297 c_parser_omp_single (location_t loc, c_parser *parser)
12299 tree stmt = make_node (OMP_SINGLE);
12300 SET_EXPR_LOCATION (stmt, loc);
12301 TREE_TYPE (stmt) = void_type_node;
12303 OMP_SINGLE_CLAUSES (stmt)
12304 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
12305 "#pragma omp single");
12306 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
12308 return add_stmt (stmt);
12311 /* OpenMP 3.0:
12312 # pragma omp task task-clause[optseq] new-line
12314 LOC is the location of the #pragma.
12317 #define OMP_TASK_CLAUSE_MASK \
12318 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
12320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
12321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
12325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
12326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
12328 static tree
12329 c_parser_omp_task (location_t loc, c_parser *parser)
12331 tree clauses, block;
12333 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
12334 "#pragma omp task");
12336 block = c_begin_omp_task ();
12337 c_parser_statement (parser);
12338 return c_finish_omp_task (loc, clauses, block);
12341 /* OpenMP 3.0:
12342 # pragma omp taskwait new-line
12345 static void
12346 c_parser_omp_taskwait (c_parser *parser)
12348 location_t loc = c_parser_peek_token (parser)->location;
12349 c_parser_consume_pragma (parser);
12350 c_parser_skip_to_pragma_eol (parser);
12352 c_finish_omp_taskwait (loc);
12355 /* OpenMP 3.1:
12356 # pragma omp taskyield new-line
12359 static void
12360 c_parser_omp_taskyield (c_parser *parser)
12362 location_t loc = c_parser_peek_token (parser)->location;
12363 c_parser_consume_pragma (parser);
12364 c_parser_skip_to_pragma_eol (parser);
12366 c_finish_omp_taskyield (loc);
12369 /* OpenMP 4.0:
12370 # pragma omp taskgroup new-line
12373 static tree
12374 c_parser_omp_taskgroup (c_parser *parser)
12376 location_t loc = c_parser_peek_token (parser)->location;
12377 c_parser_skip_to_pragma_eol (parser);
12378 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
12381 /* OpenMP 4.0:
12382 # pragma omp cancel cancel-clause[optseq] new-line
12384 LOC is the location of the #pragma.
12387 #define OMP_CANCEL_CLAUSE_MASK \
12388 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
12392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12394 static void
12395 c_parser_omp_cancel (c_parser *parser)
12397 location_t loc = c_parser_peek_token (parser)->location;
12399 c_parser_consume_pragma (parser);
12400 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
12401 "#pragma omp cancel");
12403 c_finish_omp_cancel (loc, clauses);
12406 /* OpenMP 4.0:
12407 # pragma omp cancellation point cancelpt-clause[optseq] new-line
12409 LOC is the location of the #pragma.
12412 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
12413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
12414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
12415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
12416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
12418 static void
12419 c_parser_omp_cancellation_point (c_parser *parser)
12421 location_t loc = c_parser_peek_token (parser)->location;
12422 tree clauses;
12423 bool point_seen = false;
12425 c_parser_consume_pragma (parser);
12426 if (c_parser_next_token_is (parser, CPP_NAME))
12428 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12429 if (strcmp (p, "point") == 0)
12431 c_parser_consume_token (parser);
12432 point_seen = true;
12435 if (!point_seen)
12437 c_parser_error (parser, "expected %<point%>");
12438 c_parser_skip_to_pragma_eol (parser);
12439 return;
12442 clauses
12443 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
12444 "#pragma omp cancellation point");
12446 c_finish_omp_cancellation_point (loc, clauses);
12449 /* OpenMP 4.0:
12450 #pragma omp distribute distribute-clause[optseq] new-line
12451 for-loop */
12453 #define OMP_DISTRIBUTE_CLAUSE_MASK \
12454 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
12457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12459 static tree
12460 c_parser_omp_distribute (location_t loc, c_parser *parser,
12461 char *p_name, omp_clause_mask mask, tree *cclauses)
12463 tree clauses, block, ret;
12465 strcat (p_name, " distribute");
12466 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
12468 if (c_parser_next_token_is (parser, CPP_NAME))
12470 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12471 bool simd = false;
12472 bool parallel = false;
12474 if (strcmp (p, "simd") == 0)
12475 simd = true;
12476 else
12477 parallel = strcmp (p, "parallel") == 0;
12478 if (parallel || simd)
12480 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12481 if (cclauses == NULL)
12482 cclauses = cclauses_buf;
12483 c_parser_consume_token (parser);
12484 if (!flag_openmp) /* flag_openmp_simd */
12486 if (simd)
12487 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12488 else
12489 return c_parser_omp_parallel (loc, parser, p_name, mask,
12490 cclauses);
12492 block = c_begin_compound_stmt (true);
12493 if (simd)
12494 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12495 else
12496 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
12497 block = c_end_compound_stmt (loc, block, true);
12498 if (ret == NULL)
12499 return ret;
12500 ret = make_node (OMP_DISTRIBUTE);
12501 TREE_TYPE (ret) = void_type_node;
12502 OMP_FOR_BODY (ret) = block;
12503 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12504 SET_EXPR_LOCATION (ret, loc);
12505 add_stmt (ret);
12506 return ret;
12509 if (!flag_openmp) /* flag_openmp_simd */
12511 c_parser_skip_to_pragma_eol (parser);
12512 return NULL_TREE;
12515 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12516 if (cclauses)
12518 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
12519 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
12522 block = c_begin_compound_stmt (true);
12523 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
12524 block = c_end_compound_stmt (loc, block, true);
12525 add_stmt (block);
12527 return ret;
12530 /* OpenMP 4.0:
12531 # pragma omp teams teams-clause[optseq] new-line
12532 structured-block */
12534 #define OMP_TEAMS_CLAUSE_MASK \
12535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
12538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
12540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
12541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
12543 static tree
12544 c_parser_omp_teams (location_t loc, c_parser *parser,
12545 char *p_name, omp_clause_mask mask, tree *cclauses)
12547 tree clauses, block, ret;
12549 strcat (p_name, " teams");
12550 mask |= OMP_TEAMS_CLAUSE_MASK;
12552 if (c_parser_next_token_is (parser, CPP_NAME))
12554 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12555 if (strcmp (p, "distribute") == 0)
12557 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12558 if (cclauses == NULL)
12559 cclauses = cclauses_buf;
12561 c_parser_consume_token (parser);
12562 if (!flag_openmp) /* flag_openmp_simd */
12563 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12564 block = c_begin_compound_stmt (true);
12565 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
12566 block = c_end_compound_stmt (loc, block, true);
12567 if (ret == NULL)
12568 return ret;
12569 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12570 ret = make_node (OMP_TEAMS);
12571 TREE_TYPE (ret) = void_type_node;
12572 OMP_TEAMS_CLAUSES (ret) = clauses;
12573 OMP_TEAMS_BODY (ret) = block;
12574 return add_stmt (ret);
12577 if (!flag_openmp) /* flag_openmp_simd */
12579 c_parser_skip_to_pragma_eol (parser);
12580 return NULL_TREE;
12583 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12584 if (cclauses)
12586 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
12587 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
12590 tree stmt = make_node (OMP_TEAMS);
12591 TREE_TYPE (stmt) = void_type_node;
12592 OMP_TEAMS_CLAUSES (stmt) = clauses;
12593 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
12595 return add_stmt (stmt);
12598 /* OpenMP 4.0:
12599 # pragma omp target data target-data-clause[optseq] new-line
12600 structured-block */
12602 #define OMP_TARGET_DATA_CLAUSE_MASK \
12603 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12607 static tree
12608 c_parser_omp_target_data (location_t loc, c_parser *parser)
12610 tree stmt = make_node (OMP_TARGET_DATA);
12611 TREE_TYPE (stmt) = void_type_node;
12613 OMP_TARGET_DATA_CLAUSES (stmt)
12614 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
12615 "#pragma omp target data");
12616 keep_next_level ();
12617 tree block = c_begin_compound_stmt (true);
12618 add_stmt (c_parser_omp_structured_block (parser));
12619 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12621 SET_EXPR_LOCATION (stmt, loc);
12622 return add_stmt (stmt);
12625 /* OpenMP 4.0:
12626 # pragma omp target update target-update-clause[optseq] new-line */
12628 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
12629 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
12630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
12631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12634 static bool
12635 c_parser_omp_target_update (location_t loc, c_parser *parser,
12636 enum pragma_context context)
12638 if (context == pragma_stmt)
12640 error_at (loc,
12641 "%<#pragma omp target update%> may only be "
12642 "used in compound statements");
12643 c_parser_skip_to_pragma_eol (parser);
12644 return false;
12647 tree clauses
12648 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
12649 "#pragma omp target update");
12650 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
12651 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
12653 error_at (loc,
12654 "%<#pragma omp target update must contain at least one "
12655 "%<from%> or %<to%> clauses");
12656 return false;
12659 tree stmt = make_node (OMP_TARGET_UPDATE);
12660 TREE_TYPE (stmt) = void_type_node;
12661 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
12662 SET_EXPR_LOCATION (stmt, loc);
12663 add_stmt (stmt);
12664 return false;
12667 /* OpenMP 4.0:
12668 # pragma omp target target-clause[optseq] new-line
12669 structured-block */
12671 #define OMP_TARGET_CLAUSE_MASK \
12672 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
12674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
12676 static bool
12677 c_parser_omp_target (c_parser *parser, enum pragma_context context)
12679 location_t loc = c_parser_peek_token (parser)->location;
12680 c_parser_consume_pragma (parser);
12682 if (context != pragma_stmt && context != pragma_compound)
12684 c_parser_error (parser, "expected declaration specifiers");
12685 c_parser_skip_to_pragma_eol (parser);
12686 return false;
12689 if (c_parser_next_token_is (parser, CPP_NAME))
12691 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12693 if (strcmp (p, "teams") == 0)
12695 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
12696 char p_name[sizeof ("#pragma omp target teams distribute "
12697 "parallel for simd")];
12699 c_parser_consume_token (parser);
12700 strcpy (p_name, "#pragma omp target");
12701 if (!flag_openmp) /* flag_openmp_simd */
12702 return c_parser_omp_teams (loc, parser, p_name,
12703 OMP_TARGET_CLAUSE_MASK, cclauses);
12704 keep_next_level ();
12705 tree block = c_begin_compound_stmt (true);
12706 tree ret = c_parser_omp_teams (loc, parser, p_name,
12707 OMP_TARGET_CLAUSE_MASK, cclauses);
12708 block = c_end_compound_stmt (loc, block, true);
12709 if (ret == NULL)
12710 return ret;
12711 tree stmt = make_node (OMP_TARGET);
12712 TREE_TYPE (stmt) = void_type_node;
12713 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
12714 OMP_TARGET_BODY (stmt) = block;
12715 add_stmt (stmt);
12716 return true;
12718 else if (!flag_openmp) /* flag_openmp_simd */
12720 c_parser_skip_to_pragma_eol (parser);
12721 return NULL_TREE;
12723 else if (strcmp (p, "data") == 0)
12725 c_parser_consume_token (parser);
12726 c_parser_omp_target_data (loc, parser);
12727 return true;
12729 else if (strcmp (p, "update") == 0)
12731 c_parser_consume_token (parser);
12732 return c_parser_omp_target_update (loc, parser, context);
12736 tree stmt = make_node (OMP_TARGET);
12737 TREE_TYPE (stmt) = void_type_node;
12739 OMP_TARGET_CLAUSES (stmt)
12740 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
12741 "#pragma omp target");
12742 keep_next_level ();
12743 tree block = c_begin_compound_stmt (true);
12744 add_stmt (c_parser_omp_structured_block (parser));
12745 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
12747 SET_EXPR_LOCATION (stmt, loc);
12748 add_stmt (stmt);
12749 return true;
12752 /* OpenMP 4.0:
12753 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
12755 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
12756 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
12757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
12760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
12761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
12763 static void
12764 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
12766 vec<c_token> clauses = vNULL;
12767 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12769 c_token *token = c_parser_peek_token (parser);
12770 if (token->type == CPP_EOF)
12772 c_parser_skip_to_pragma_eol (parser);
12773 clauses.release ();
12774 return;
12776 clauses.safe_push (*token);
12777 c_parser_consume_token (parser);
12779 clauses.safe_push (*c_parser_peek_token (parser));
12780 c_parser_skip_to_pragma_eol (parser);
12782 while (c_parser_next_token_is (parser, CPP_PRAGMA))
12784 if (c_parser_peek_token (parser)->pragma_kind
12785 != PRAGMA_OMP_DECLARE_REDUCTION
12786 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
12787 || strcmp (IDENTIFIER_POINTER
12788 (c_parser_peek_2nd_token (parser)->value),
12789 "simd") != 0)
12791 c_parser_error (parser,
12792 "%<#pragma omp declare simd%> must be followed by "
12793 "function declaration or definition or another "
12794 "%<#pragma omp declare simd%>");
12795 clauses.release ();
12796 return;
12798 c_parser_consume_pragma (parser);
12799 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12801 c_token *token = c_parser_peek_token (parser);
12802 if (token->type == CPP_EOF)
12804 c_parser_skip_to_pragma_eol (parser);
12805 clauses.release ();
12806 return;
12808 clauses.safe_push (*token);
12809 c_parser_consume_token (parser);
12811 clauses.safe_push (*c_parser_peek_token (parser));
12812 c_parser_skip_to_pragma_eol (parser);
12815 /* Make sure nothing tries to read past the end of the tokens. */
12816 c_token eof_token;
12817 memset (&eof_token, 0, sizeof (eof_token));
12818 eof_token.type = CPP_EOF;
12819 clauses.safe_push (eof_token);
12820 clauses.safe_push (eof_token);
12822 switch (context)
12824 case pragma_external:
12825 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12826 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12828 int ext = disable_extension_diagnostics ();
12830 c_parser_consume_token (parser);
12831 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12832 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12833 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12834 NULL, clauses);
12835 restore_extension_diagnostics (ext);
12837 else
12838 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
12839 NULL, clauses);
12840 break;
12841 case pragma_struct:
12842 case pragma_param:
12843 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12844 "function declaration or definition");
12845 break;
12846 case pragma_compound:
12847 case pragma_stmt:
12848 if (c_parser_next_token_is (parser, CPP_KEYWORD)
12849 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
12851 int ext = disable_extension_diagnostics ();
12853 c_parser_consume_token (parser);
12854 while (c_parser_next_token_is (parser, CPP_KEYWORD)
12855 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
12856 if (c_parser_next_tokens_start_declaration (parser))
12858 c_parser_declaration_or_fndef (parser, true, true, true, true,
12859 true, NULL, clauses);
12860 restore_extension_diagnostics (ext);
12861 break;
12863 restore_extension_diagnostics (ext);
12865 else if (c_parser_next_tokens_start_declaration (parser))
12867 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12868 NULL, clauses);
12869 break;
12871 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
12872 "function declaration or definition");
12873 break;
12874 default:
12875 gcc_unreachable ();
12877 clauses.release ();
12880 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
12881 and put that into "omp declare simd" attribute. */
12883 static void
12884 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
12885 vec<c_token> clauses)
12887 if (flag_cilkplus
12888 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12890 error ("%<#pragma omp declare simd%> cannot be used in the same "
12891 "function marked as a Cilk Plus SIMD-enabled function");
12892 vec_free (parser->cilk_simd_fn_tokens);
12893 return;
12896 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
12897 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
12898 has already processed the tokens. */
12899 if (clauses.exists () && clauses[0].type == CPP_EOF)
12900 return;
12901 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
12903 error ("%<#pragma omp declare simd%> not immediately followed by "
12904 "a function declaration or definition");
12905 clauses[0].type = CPP_EOF;
12906 return;
12908 if (clauses.exists () && clauses[0].type != CPP_NAME)
12910 error_at (DECL_SOURCE_LOCATION (fndecl),
12911 "%<#pragma omp declare simd%> not immediately followed by "
12912 "a single function declaration or definition");
12913 clauses[0].type = CPP_EOF;
12914 return;
12917 if (parms == NULL_TREE)
12918 parms = DECL_ARGUMENTS (fndecl);
12920 unsigned int tokens_avail = parser->tokens_avail;
12921 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
12922 bool is_cilkplus_cilk_simd_fn = false;
12924 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12926 parser->tokens = parser->cilk_simd_fn_tokens->address ();
12927 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
12928 is_cilkplus_cilk_simd_fn = true;
12930 else
12932 parser->tokens = clauses.address ();
12933 parser->tokens_avail = clauses.length ();
12936 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
12937 while (parser->tokens_avail > 3)
12939 c_token *token = c_parser_peek_token (parser);
12940 if (!is_cilkplus_cilk_simd_fn)
12941 gcc_assert (token->type == CPP_NAME
12942 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
12943 else
12944 gcc_assert (token->type == CPP_NAME
12945 && is_cilkplus_vector_p (token->value));
12946 c_parser_consume_token (parser);
12947 parser->in_pragma = true;
12949 tree c = NULL_TREE;
12950 if (is_cilkplus_cilk_simd_fn)
12951 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
12952 "SIMD-enabled functions attribute");
12953 else
12954 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
12955 "#pragma omp declare simd");
12956 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
12957 if (c != NULL_TREE)
12958 c = tree_cons (NULL_TREE, c, NULL_TREE);
12959 if (is_cilkplus_cilk_simd_fn)
12961 tree k = build_tree_list (get_identifier ("cilk simd function"),
12962 NULL_TREE);
12963 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
12964 DECL_ATTRIBUTES (fndecl) = k;
12966 c = build_tree_list (get_identifier ("omp declare simd"), c);
12967 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
12968 DECL_ATTRIBUTES (fndecl) = c;
12971 parser->tokens = &parser->tokens_buf[0];
12972 parser->tokens_avail = tokens_avail;
12973 if (clauses.exists ())
12974 clauses[0].type = CPP_PRAGMA;
12976 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
12977 vec_free (parser->cilk_simd_fn_tokens);
12981 /* OpenMP 4.0:
12982 # pragma omp declare target new-line
12983 declarations and definitions
12984 # pragma omp end declare target new-line */
12986 static void
12987 c_parser_omp_declare_target (c_parser *parser)
12989 c_parser_skip_to_pragma_eol (parser);
12990 current_omp_declare_target_attribute++;
12993 static void
12994 c_parser_omp_end_declare_target (c_parser *parser)
12996 location_t loc = c_parser_peek_token (parser)->location;
12997 c_parser_consume_pragma (parser);
12998 if (c_parser_next_token_is (parser, CPP_NAME)
12999 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13000 "declare") == 0)
13002 c_parser_consume_token (parser);
13003 if (c_parser_next_token_is (parser, CPP_NAME)
13004 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13005 "target") == 0)
13006 c_parser_consume_token (parser);
13007 else
13009 c_parser_error (parser, "expected %<target%>");
13010 c_parser_skip_to_pragma_eol (parser);
13011 return;
13014 else
13016 c_parser_error (parser, "expected %<declare%>");
13017 c_parser_skip_to_pragma_eol (parser);
13018 return;
13020 c_parser_skip_to_pragma_eol (parser);
13021 if (!current_omp_declare_target_attribute)
13022 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
13023 "%<#pragma omp declare target%>");
13024 else
13025 current_omp_declare_target_attribute--;
13029 /* OpenMP 4.0
13030 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13031 initializer-clause[opt] new-line
13033 initializer-clause:
13034 initializer (omp_priv = initializer)
13035 initializer (function-name (argument-list)) */
13037 static void
13038 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
13040 unsigned int tokens_avail = 0, i;
13041 vec<tree> types = vNULL;
13042 vec<c_token> clauses = vNULL;
13043 enum tree_code reduc_code = ERROR_MARK;
13044 tree reduc_id = NULL_TREE;
13045 tree type;
13046 location_t rloc = c_parser_peek_token (parser)->location;
13048 if (context == pragma_struct || context == pragma_param)
13050 error ("%<#pragma omp declare reduction%> not at file or block scope");
13051 goto fail;
13054 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13055 goto fail;
13057 switch (c_parser_peek_token (parser)->type)
13059 case CPP_PLUS:
13060 reduc_code = PLUS_EXPR;
13061 break;
13062 case CPP_MULT:
13063 reduc_code = MULT_EXPR;
13064 break;
13065 case CPP_MINUS:
13066 reduc_code = MINUS_EXPR;
13067 break;
13068 case CPP_AND:
13069 reduc_code = BIT_AND_EXPR;
13070 break;
13071 case CPP_XOR:
13072 reduc_code = BIT_XOR_EXPR;
13073 break;
13074 case CPP_OR:
13075 reduc_code = BIT_IOR_EXPR;
13076 break;
13077 case CPP_AND_AND:
13078 reduc_code = TRUTH_ANDIF_EXPR;
13079 break;
13080 case CPP_OR_OR:
13081 reduc_code = TRUTH_ORIF_EXPR;
13082 break;
13083 case CPP_NAME:
13084 const char *p;
13085 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13086 if (strcmp (p, "min") == 0)
13088 reduc_code = MIN_EXPR;
13089 break;
13091 if (strcmp (p, "max") == 0)
13093 reduc_code = MAX_EXPR;
13094 break;
13096 reduc_id = c_parser_peek_token (parser)->value;
13097 break;
13098 default:
13099 c_parser_error (parser,
13100 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13101 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
13102 goto fail;
13105 tree orig_reduc_id, reduc_decl;
13106 orig_reduc_id = reduc_id;
13107 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
13108 reduc_decl = c_omp_reduction_decl (reduc_id);
13109 c_parser_consume_token (parser);
13111 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13112 goto fail;
13114 while (true)
13116 location_t loc = c_parser_peek_token (parser)->location;
13117 struct c_type_name *ctype = c_parser_type_name (parser);
13118 if (ctype != NULL)
13120 type = groktypename (ctype, NULL, NULL);
13121 if (type == error_mark_node)
13123 else if ((INTEGRAL_TYPE_P (type)
13124 || TREE_CODE (type) == REAL_TYPE
13125 || TREE_CODE (type) == COMPLEX_TYPE)
13126 && orig_reduc_id == NULL_TREE)
13127 error_at (loc, "predeclared arithmetic type in "
13128 "%<#pragma omp declare reduction%>");
13129 else if (TREE_CODE (type) == FUNCTION_TYPE
13130 || TREE_CODE (type) == ARRAY_TYPE)
13131 error_at (loc, "function or array type in "
13132 "%<#pragma omp declare reduction%>");
13133 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
13134 error_at (loc, "const, volatile or restrict qualified type in "
13135 "%<#pragma omp declare reduction%>");
13136 else
13138 tree t;
13139 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
13140 if (comptypes (TREE_PURPOSE (t), type))
13142 error_at (loc, "redeclaration of %qs "
13143 "%<#pragma omp declare reduction%> for "
13144 "type %qT",
13145 IDENTIFIER_POINTER (reduc_id)
13146 + sizeof ("omp declare reduction ") - 1,
13147 type);
13148 location_t ploc
13149 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
13150 0));
13151 error_at (ploc, "previous %<#pragma omp declare "
13152 "reduction%>");
13153 break;
13155 if (t == NULL_TREE)
13156 types.safe_push (type);
13158 if (c_parser_next_token_is (parser, CPP_COMMA))
13159 c_parser_consume_token (parser);
13160 else
13161 break;
13163 else
13164 break;
13167 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
13168 || types.is_empty ())
13170 fail:
13171 clauses.release ();
13172 types.release ();
13173 while (true)
13175 c_token *token = c_parser_peek_token (parser);
13176 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
13177 break;
13178 c_parser_consume_token (parser);
13180 c_parser_skip_to_pragma_eol (parser);
13181 return;
13184 if (types.length () > 1)
13186 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13188 c_token *token = c_parser_peek_token (parser);
13189 if (token->type == CPP_EOF)
13190 goto fail;
13191 clauses.safe_push (*token);
13192 c_parser_consume_token (parser);
13194 clauses.safe_push (*c_parser_peek_token (parser));
13195 c_parser_skip_to_pragma_eol (parser);
13197 /* Make sure nothing tries to read past the end of the tokens. */
13198 c_token eof_token;
13199 memset (&eof_token, 0, sizeof (eof_token));
13200 eof_token.type = CPP_EOF;
13201 clauses.safe_push (eof_token);
13202 clauses.safe_push (eof_token);
13205 int errs = errorcount;
13206 FOR_EACH_VEC_ELT (types, i, type)
13208 tokens_avail = parser->tokens_avail;
13209 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13210 if (!clauses.is_empty ())
13212 parser->tokens = clauses.address ();
13213 parser->tokens_avail = clauses.length ();
13214 parser->in_pragma = true;
13217 bool nested = current_function_decl != NULL_TREE;
13218 if (nested)
13219 c_push_function_context ();
13220 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
13221 reduc_id, default_function_type);
13222 current_function_decl = fndecl;
13223 allocate_struct_function (fndecl, true);
13224 push_scope ();
13225 tree stmt = push_stmt_list ();
13226 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
13227 warn about these. */
13228 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
13229 get_identifier ("omp_out"), type);
13230 DECL_ARTIFICIAL (omp_out) = 1;
13231 DECL_CONTEXT (omp_out) = fndecl;
13232 pushdecl (omp_out);
13233 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
13234 get_identifier ("omp_in"), type);
13235 DECL_ARTIFICIAL (omp_in) = 1;
13236 DECL_CONTEXT (omp_in) = fndecl;
13237 pushdecl (omp_in);
13238 struct c_expr combiner = c_parser_expression (parser);
13239 struct c_expr initializer;
13240 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
13241 bool bad = false;
13242 initializer.value = error_mark_node;
13243 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13244 bad = true;
13245 else if (c_parser_next_token_is (parser, CPP_NAME)
13246 && strcmp (IDENTIFIER_POINTER
13247 (c_parser_peek_token (parser)->value),
13248 "initializer") == 0)
13250 c_parser_consume_token (parser);
13251 pop_scope ();
13252 push_scope ();
13253 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
13254 get_identifier ("omp_priv"), type);
13255 DECL_ARTIFICIAL (omp_priv) = 1;
13256 DECL_INITIAL (omp_priv) = error_mark_node;
13257 DECL_CONTEXT (omp_priv) = fndecl;
13258 pushdecl (omp_priv);
13259 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
13260 get_identifier ("omp_orig"), type);
13261 DECL_ARTIFICIAL (omp_orig) = 1;
13262 DECL_CONTEXT (omp_orig) = fndecl;
13263 pushdecl (omp_orig);
13264 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13265 bad = true;
13266 else if (!c_parser_next_token_is (parser, CPP_NAME))
13268 c_parser_error (parser, "expected %<omp_priv%> or "
13269 "function-name");
13270 bad = true;
13272 else if (strcmp (IDENTIFIER_POINTER
13273 (c_parser_peek_token (parser)->value),
13274 "omp_priv") != 0)
13276 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
13277 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13279 c_parser_error (parser, "expected function-name %<(%>");
13280 bad = true;
13282 else
13283 initializer = c_parser_postfix_expression (parser);
13284 if (initializer.value
13285 && TREE_CODE (initializer.value) == CALL_EXPR)
13287 int j;
13288 tree c = initializer.value;
13289 for (j = 0; j < call_expr_nargs (c); j++)
13290 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
13291 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
13292 break;
13293 if (j == call_expr_nargs (c))
13294 error ("one of the initializer call arguments should be "
13295 "%<&omp_priv%>");
13298 else
13300 c_parser_consume_token (parser);
13301 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
13302 bad = true;
13303 else
13305 tree st = push_stmt_list ();
13306 start_init (omp_priv, NULL_TREE, 0);
13307 location_t loc = c_parser_peek_token (parser)->location;
13308 struct c_expr init = c_parser_initializer (parser);
13309 finish_init ();
13310 finish_decl (omp_priv, loc, init.value,
13311 init.original_type, NULL_TREE);
13312 pop_stmt_list (st);
13315 if (!bad
13316 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13317 bad = true;
13320 if (!bad)
13322 c_parser_skip_to_pragma_eol (parser);
13324 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
13325 DECL_INITIAL (reduc_decl));
13326 DECL_INITIAL (reduc_decl) = t;
13327 DECL_SOURCE_LOCATION (omp_out) = rloc;
13328 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
13329 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
13330 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
13331 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
13332 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
13333 if (omp_priv)
13335 DECL_SOURCE_LOCATION (omp_priv) = rloc;
13336 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
13337 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
13338 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
13339 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
13340 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13341 walk_tree (&DECL_INITIAL (omp_priv),
13342 c_check_omp_declare_reduction_r,
13343 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
13347 pop_stmt_list (stmt);
13348 pop_scope ();
13349 if (cfun->language != NULL)
13351 ggc_free (cfun->language);
13352 cfun->language = NULL;
13354 set_cfun (NULL);
13355 current_function_decl = NULL_TREE;
13356 if (nested)
13357 c_pop_function_context ();
13359 if (!clauses.is_empty ())
13361 parser->tokens = &parser->tokens_buf[0];
13362 parser->tokens_avail = tokens_avail;
13364 if (bad)
13365 goto fail;
13366 if (errs != errorcount)
13367 break;
13370 clauses.release ();
13371 types.release ();
13375 /* OpenMP 4.0
13376 #pragma omp declare simd declare-simd-clauses[optseq] new-line
13377 #pragma omp declare reduction (reduction-id : typename-list : expression) \
13378 initializer-clause[opt] new-line
13379 #pragma omp declare target new-line */
13381 static void
13382 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
13384 c_parser_consume_pragma (parser);
13385 if (c_parser_next_token_is (parser, CPP_NAME))
13387 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13388 if (strcmp (p, "simd") == 0)
13390 /* c_parser_consume_token (parser); done in
13391 c_parser_omp_declare_simd. */
13392 c_parser_omp_declare_simd (parser, context);
13393 return;
13395 if (strcmp (p, "reduction") == 0)
13397 c_parser_consume_token (parser);
13398 c_parser_omp_declare_reduction (parser, context);
13399 return;
13401 if (!flag_openmp) /* flag_openmp_simd */
13403 c_parser_skip_to_pragma_eol (parser);
13404 return;
13406 if (strcmp (p, "target") == 0)
13408 c_parser_consume_token (parser);
13409 c_parser_omp_declare_target (parser);
13410 return;
13414 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
13415 "or %<target%>");
13416 c_parser_skip_to_pragma_eol (parser);
13419 /* Main entry point to parsing most OpenMP pragmas. */
13421 static void
13422 c_parser_omp_construct (c_parser *parser)
13424 enum pragma_kind p_kind;
13425 location_t loc;
13426 tree stmt;
13427 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
13428 omp_clause_mask mask (0);
13430 loc = c_parser_peek_token (parser)->location;
13431 p_kind = c_parser_peek_token (parser)->pragma_kind;
13432 c_parser_consume_pragma (parser);
13434 switch (p_kind)
13436 case PRAGMA_OMP_ATOMIC:
13437 c_parser_omp_atomic (loc, parser);
13438 return;
13439 case PRAGMA_OMP_CRITICAL:
13440 stmt = c_parser_omp_critical (loc, parser);
13441 break;
13442 case PRAGMA_OMP_DISTRIBUTE:
13443 strcpy (p_name, "#pragma omp");
13444 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
13445 break;
13446 case PRAGMA_OMP_FOR:
13447 strcpy (p_name, "#pragma omp");
13448 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
13449 break;
13450 case PRAGMA_OMP_MASTER:
13451 stmt = c_parser_omp_master (loc, parser);
13452 break;
13453 case PRAGMA_OMP_ORDERED:
13454 stmt = c_parser_omp_ordered (loc, parser);
13455 break;
13456 case PRAGMA_OMP_PARALLEL:
13457 strcpy (p_name, "#pragma omp");
13458 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
13459 break;
13460 case PRAGMA_OMP_SECTIONS:
13461 strcpy (p_name, "#pragma omp");
13462 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
13463 break;
13464 case PRAGMA_OMP_SIMD:
13465 strcpy (p_name, "#pragma omp");
13466 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
13467 break;
13468 case PRAGMA_OMP_SINGLE:
13469 stmt = c_parser_omp_single (loc, parser);
13470 break;
13471 case PRAGMA_OMP_TASK:
13472 stmt = c_parser_omp_task (loc, parser);
13473 break;
13474 case PRAGMA_OMP_TASKGROUP:
13475 stmt = c_parser_omp_taskgroup (parser);
13476 break;
13477 case PRAGMA_OMP_TEAMS:
13478 strcpy (p_name, "#pragma omp");
13479 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
13480 break;
13481 default:
13482 gcc_unreachable ();
13485 if (stmt)
13486 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
13490 /* OpenMP 2.5:
13491 # pragma omp threadprivate (variable-list) */
13493 static void
13494 c_parser_omp_threadprivate (c_parser *parser)
13496 tree vars, t;
13497 location_t loc;
13499 c_parser_consume_pragma (parser);
13500 loc = c_parser_peek_token (parser)->location;
13501 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
13503 /* Mark every variable in VARS to be assigned thread local storage. */
13504 for (t = vars; t; t = TREE_CHAIN (t))
13506 tree v = TREE_PURPOSE (t);
13508 /* FIXME diagnostics: Ideally we should keep individual
13509 locations for all the variables in the var list to make the
13510 following errors more precise. Perhaps
13511 c_parser_omp_var_list_parens() should construct a list of
13512 locations to go along with the var list. */
13514 /* If V had already been marked threadprivate, it doesn't matter
13515 whether it had been used prior to this point. */
13516 if (TREE_CODE (v) != VAR_DECL)
13517 error_at (loc, "%qD is not a variable", v);
13518 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
13519 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
13520 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
13521 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
13522 else if (TREE_TYPE (v) == error_mark_node)
13524 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
13525 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
13526 else
13528 if (! DECL_THREAD_LOCAL_P (v))
13530 DECL_TLS_MODEL (v) = decl_default_tls_model (v);
13531 /* If rtl has been already set for this var, call
13532 make_decl_rtl once again, so that encode_section_info
13533 has a chance to look at the new decl flags. */
13534 if (DECL_RTL_SET_P (v))
13535 make_decl_rtl (v);
13537 C_DECL_THREADPRIVATE_P (v) = 1;
13541 c_parser_skip_to_pragma_eol (parser);
13544 /* Cilk Plus <#pragma simd> parsing routines. */
13546 /* Helper function for c_parser_pragma. Perform some sanity checking
13547 for <#pragma simd> constructs. Returns FALSE if there was a
13548 problem. */
13550 static bool
13551 c_parser_cilk_verify_simd (c_parser *parser,
13552 enum pragma_context context)
13554 if (!flag_cilkplus)
13556 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
13557 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13558 return false;
13560 if (context == pragma_external)
13562 c_parser_error (parser,"pragma simd must be inside a function");
13563 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
13564 return false;
13566 return true;
13569 /* Cilk Plus:
13570 This function is shared by SIMD-enabled functions and #pragma simd.
13571 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
13572 CLAUSES is unused. The main purpose of this function is to parse a
13573 vectorlength attribute or clause and check for parse errors.
13574 When IS_SIMD_FN is true then the function is merely caching the tokens
13575 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
13576 cache is cleared since there is no reason to continue.
13577 Syntax:
13578 vectorlength ( constant-expression ) */
13580 static tree
13581 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
13582 bool is_simd_fn)
13584 if (is_simd_fn)
13585 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
13586 else
13587 /* The vectorlength clause behaves exactly like OpenMP's safelen
13588 clause. Represent it in OpenMP terms. */
13589 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
13591 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13592 return clauses;
13594 location_t loc = c_parser_peek_token (parser)->location;
13595 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13596 expr = c_fully_fold (expr, false, NULL);
13598 /* If expr is an error_mark_node then the above function would have
13599 emitted an error. No reason to do it twice. */
13600 if (expr == error_mark_node)
13602 else if (!TREE_TYPE (expr)
13603 || !TREE_CONSTANT (expr)
13604 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
13606 error_at (loc, "vectorlength must be an integer constant");
13607 else if (exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
13608 error_at (loc, "vectorlength must be a power of 2");
13609 else
13611 if (is_simd_fn)
13613 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
13614 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
13615 OMP_CLAUSE_CHAIN (u) = clauses;
13616 clauses = u;
13618 else
13620 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
13621 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
13622 OMP_CLAUSE_CHAIN (u) = clauses;
13623 clauses = u;
13627 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13629 return clauses;
13632 /* Cilk Plus:
13633 linear ( simd-linear-variable-list )
13635 simd-linear-variable-list:
13636 simd-linear-variable
13637 simd-linear-variable-list , simd-linear-variable
13639 simd-linear-variable:
13640 id-expression
13641 id-expression : simd-linear-step
13643 simd-linear-step:
13644 conditional-expression */
13646 static tree
13647 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
13649 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13650 return clauses;
13652 location_t loc = c_parser_peek_token (parser)->location;
13654 if (c_parser_next_token_is_not (parser, CPP_NAME)
13655 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13656 c_parser_error (parser, "expected identifier");
13658 while (c_parser_next_token_is (parser, CPP_NAME)
13659 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13661 tree var = lookup_name (c_parser_peek_token (parser)->value);
13663 if (var == NULL)
13665 undeclared_variable (c_parser_peek_token (parser)->location,
13666 c_parser_peek_token (parser)->value);
13667 c_parser_consume_token (parser);
13669 else if (var == error_mark_node)
13670 c_parser_consume_token (parser);
13671 else
13673 tree step = integer_one_node;
13675 /* Parse the linear step if present. */
13676 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13678 c_parser_consume_token (parser);
13679 c_parser_consume_token (parser);
13681 tree expr = c_parser_expr_no_commas (parser, NULL).value;
13682 expr = c_fully_fold (expr, false, NULL);
13684 if (TREE_TYPE (expr)
13685 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
13686 && (TREE_CONSTANT (expr)
13687 || DECL_P (expr)))
13688 step = expr;
13689 else
13690 c_parser_error (parser,
13691 "step size must be an integer constant "
13692 "expression or an integer variable");
13694 else
13695 c_parser_consume_token (parser);
13697 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
13698 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
13699 OMP_CLAUSE_DECL (u) = var;
13700 OMP_CLAUSE_LINEAR_STEP (u) = step;
13701 OMP_CLAUSE_CHAIN (u) = clauses;
13702 clauses = u;
13705 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13706 break;
13708 c_parser_consume_token (parser);
13711 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13713 return clauses;
13716 /* Returns the name of the next clause. If the clause is not
13717 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
13718 not consumed. Otherwise, the appropriate pragma_simd_clause is
13719 returned and the token is consumed. */
13721 static pragma_omp_clause
13722 c_parser_cilk_clause_name (c_parser *parser)
13724 pragma_omp_clause result;
13725 c_token *token = c_parser_peek_token (parser);
13727 if (!token->value || token->type != CPP_NAME)
13728 return PRAGMA_CILK_CLAUSE_NONE;
13730 const char *p = IDENTIFIER_POINTER (token->value);
13732 if (!strcmp (p, "vectorlength"))
13733 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
13734 else if (!strcmp (p, "linear"))
13735 result = PRAGMA_CILK_CLAUSE_LINEAR;
13736 else if (!strcmp (p, "private"))
13737 result = PRAGMA_CILK_CLAUSE_PRIVATE;
13738 else if (!strcmp (p, "firstprivate"))
13739 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
13740 else if (!strcmp (p, "lastprivate"))
13741 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
13742 else if (!strcmp (p, "reduction"))
13743 result = PRAGMA_CILK_CLAUSE_REDUCTION;
13744 else
13745 return PRAGMA_CILK_CLAUSE_NONE;
13747 c_parser_consume_token (parser);
13748 return result;
13751 /* Parse all #<pragma simd> clauses. Return the list of clauses
13752 found. */
13754 static tree
13755 c_parser_cilk_all_clauses (c_parser *parser)
13757 tree clauses = NULL;
13759 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13761 pragma_omp_clause c_kind;
13763 c_kind = c_parser_cilk_clause_name (parser);
13765 switch (c_kind)
13767 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13768 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
13769 break;
13770 case PRAGMA_CILK_CLAUSE_LINEAR:
13771 clauses = c_parser_cilk_clause_linear (parser, clauses);
13772 break;
13773 case PRAGMA_CILK_CLAUSE_PRIVATE:
13774 /* Use the OpenMP counterpart. */
13775 clauses = c_parser_omp_clause_private (parser, clauses);
13776 break;
13777 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
13778 /* Use the OpenMP counterpart. */
13779 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13780 break;
13781 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
13782 /* Use the OpenMP counterpart. */
13783 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13784 break;
13785 case PRAGMA_CILK_CLAUSE_REDUCTION:
13786 /* Use the OpenMP counterpart. */
13787 clauses = c_parser_omp_clause_reduction (parser, clauses);
13788 break;
13789 default:
13790 c_parser_error (parser, "expected %<#pragma simd%> clause");
13791 goto saw_error;
13795 saw_error:
13796 c_parser_skip_to_pragma_eol (parser);
13797 return c_finish_cilk_clauses (clauses);
13800 /* Main entry point for parsing Cilk Plus <#pragma simd> for
13801 loops. */
13803 static void
13804 c_parser_cilk_simd (c_parser *parser)
13806 tree clauses = c_parser_cilk_all_clauses (parser);
13807 tree block = c_begin_compound_stmt (true);
13808 location_t loc = c_parser_peek_token (parser)->location;
13809 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
13810 block = c_end_compound_stmt (loc, block, true);
13811 add_stmt (block);
13814 /* Parse a transaction attribute (GCC Extension).
13816 transaction-attribute:
13817 attributes
13818 [ [ any-word ] ]
13820 The transactional memory language description is written for C++,
13821 and uses the C++0x attribute syntax. For compatibility, allow the
13822 bracket style for transactions in C as well. */
13824 static tree
13825 c_parser_transaction_attributes (c_parser *parser)
13827 tree attr_name, attr = NULL;
13829 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
13830 return c_parser_attributes (parser);
13832 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
13833 return NULL_TREE;
13834 c_parser_consume_token (parser);
13835 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
13836 goto error1;
13838 attr_name = c_parser_attribute_any_word (parser);
13839 if (attr_name)
13841 c_parser_consume_token (parser);
13842 attr = build_tree_list (attr_name, NULL_TREE);
13844 else
13845 c_parser_error (parser, "expected identifier");
13847 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13848 error1:
13849 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
13850 return attr;
13853 /* Parse a __transaction_atomic or __transaction_relaxed statement
13854 (GCC Extension).
13856 transaction-statement:
13857 __transaction_atomic transaction-attribute[opt] compound-statement
13858 __transaction_relaxed compound-statement
13860 Note that the only valid attribute is: "outer".
13863 static tree
13864 c_parser_transaction (c_parser *parser, enum rid keyword)
13866 unsigned int old_in = parser->in_transaction;
13867 unsigned int this_in = 1, new_in;
13868 location_t loc = c_parser_peek_token (parser)->location;
13869 tree stmt, attrs;
13871 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13872 || keyword == RID_TRANSACTION_RELAXED)
13873 && c_parser_next_token_is_keyword (parser, keyword));
13874 c_parser_consume_token (parser);
13876 if (keyword == RID_TRANSACTION_RELAXED)
13877 this_in |= TM_STMT_ATTR_RELAXED;
13878 else
13880 attrs = c_parser_transaction_attributes (parser);
13881 if (attrs)
13882 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
13885 /* Keep track if we're in the lexical scope of an outer transaction. */
13886 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
13888 parser->in_transaction = new_in;
13889 stmt = c_parser_compound_statement (parser);
13890 parser->in_transaction = old_in;
13892 if (flag_tm)
13893 stmt = c_finish_transaction (loc, stmt, this_in);
13894 else
13895 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13896 "%<__transaction_atomic%> without transactional memory support enabled"
13897 : "%<__transaction_relaxed %> "
13898 "without transactional memory support enabled"));
13900 return stmt;
13903 /* Parse a __transaction_atomic or __transaction_relaxed expression
13904 (GCC Extension).
13906 transaction-expression:
13907 __transaction_atomic ( expression )
13908 __transaction_relaxed ( expression )
13911 static struct c_expr
13912 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
13914 struct c_expr ret;
13915 unsigned int old_in = parser->in_transaction;
13916 unsigned int this_in = 1;
13917 location_t loc = c_parser_peek_token (parser)->location;
13918 tree attrs;
13920 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
13921 || keyword == RID_TRANSACTION_RELAXED)
13922 && c_parser_next_token_is_keyword (parser, keyword));
13923 c_parser_consume_token (parser);
13925 if (keyword == RID_TRANSACTION_RELAXED)
13926 this_in |= TM_STMT_ATTR_RELAXED;
13927 else
13929 attrs = c_parser_transaction_attributes (parser);
13930 if (attrs)
13931 this_in |= parse_tm_stmt_attr (attrs, 0);
13934 parser->in_transaction = this_in;
13935 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13937 tree expr = c_parser_expression (parser).value;
13938 ret.original_type = TREE_TYPE (expr);
13939 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
13940 if (this_in & TM_STMT_ATTR_RELAXED)
13941 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
13942 SET_EXPR_LOCATION (ret.value, loc);
13943 ret.original_code = TRANSACTION_EXPR;
13944 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
13946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
13947 goto error;
13950 else
13952 error:
13953 ret.value = error_mark_node;
13954 ret.original_code = ERROR_MARK;
13955 ret.original_type = NULL;
13957 parser->in_transaction = old_in;
13959 if (!flag_tm)
13960 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
13961 "%<__transaction_atomic%> without transactional memory support enabled"
13962 : "%<__transaction_relaxed %> "
13963 "without transactional memory support enabled"));
13965 return ret;
13968 /* Parse a __transaction_cancel statement (GCC Extension).
13970 transaction-cancel-statement:
13971 __transaction_cancel transaction-attribute[opt] ;
13973 Note that the only valid attribute is "outer".
13976 static tree
13977 c_parser_transaction_cancel (c_parser *parser)
13979 location_t loc = c_parser_peek_token (parser)->location;
13980 tree attrs;
13981 bool is_outer = false;
13983 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
13984 c_parser_consume_token (parser);
13986 attrs = c_parser_transaction_attributes (parser);
13987 if (attrs)
13988 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
13990 if (!flag_tm)
13992 error_at (loc, "%<__transaction_cancel%> without "
13993 "transactional memory support enabled");
13994 goto ret_error;
13996 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
13998 error_at (loc, "%<__transaction_cancel%> within a "
13999 "%<__transaction_relaxed%>");
14000 goto ret_error;
14002 else if (is_outer)
14004 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
14005 && !is_tm_may_cancel_outer (current_function_decl))
14007 error_at (loc, "outer %<__transaction_cancel%> not "
14008 "within outer %<__transaction_atomic%>");
14009 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
14010 goto ret_error;
14013 else if (parser->in_transaction == 0)
14015 error_at (loc, "%<__transaction_cancel%> not within "
14016 "%<__transaction_atomic%>");
14017 goto ret_error;
14020 return add_stmt (build_tm_abort_call (loc, is_outer));
14022 ret_error:
14023 return build1 (NOP_EXPR, void_type_node, error_mark_node);
14026 /* Parse a single source file. */
14028 void
14029 c_parse_file (void)
14031 /* Use local storage to begin. If the first token is a pragma, parse it.
14032 If it is #pragma GCC pch_preprocess, then this will load a PCH file
14033 which will cause garbage collection. */
14034 c_parser tparser;
14036 memset (&tparser, 0, sizeof tparser);
14037 tparser.tokens = &tparser.tokens_buf[0];
14038 the_parser = &tparser;
14040 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
14041 c_parser_pragma_pch_preprocess (&tparser);
14043 the_parser = ggc_alloc_c_parser ();
14044 *the_parser = tparser;
14045 if (tparser.tokens == &tparser.tokens_buf[0])
14046 the_parser->tokens = &the_parser->tokens_buf[0];
14048 /* Initialize EH, if we've been told to do so. */
14049 if (flag_exceptions)
14050 using_eh_for_cleanups ();
14052 c_parser_translation_unit (the_parser);
14053 the_parser = NULL;
14056 /* This function parses Cilk Plus array notation. The starting index is
14057 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
14058 return value of this function is a tree_node called VALUE_TREE of type
14059 ARRAY_NOTATION_REF. */
14061 static tree
14062 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
14063 tree array_value)
14065 c_token *token = NULL;
14066 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
14067 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
14068 tree array_type_domain = NULL_TREE;
14070 if (array_value == error_mark_node || initial_index == error_mark_node)
14072 /* No need to continue. If either of these 2 were true, then an error
14073 must be emitted already. Thus, no need to emit them twice. */
14074 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14075 return error_mark_node;
14078 array_type = TREE_TYPE (array_value);
14079 gcc_assert (array_type);
14080 type = TREE_TYPE (array_type);
14081 token = c_parser_peek_token (parser);
14083 if (token->type == CPP_EOF)
14085 c_parser_error (parser, "expected %<:%> or numeral");
14086 return value_tree;
14088 else if (token->type == CPP_COLON)
14090 if (!initial_index)
14092 /* If we are here, then we have a case like this A[:]. */
14093 c_parser_consume_token (parser);
14094 if (TREE_CODE (array_type) == POINTER_TYPE)
14096 error_at (loc, "start-index and length fields necessary for "
14097 "using array notations in pointers");
14098 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14099 return error_mark_node;
14101 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14103 error_at (loc, "array notations cannot be used with function "
14104 "type");
14105 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14106 return error_mark_node;
14108 array_type_domain = TYPE_DOMAIN (array_type);
14110 if (!array_type_domain)
14112 error_at (loc, "start-index and length fields necessary for "
14113 "using array notations in dimensionless arrays");
14114 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14115 return error_mark_node;
14118 start_index = TYPE_MINVAL (array_type_domain);
14119 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
14120 start_index);
14121 if (!TYPE_MAXVAL (array_type_domain)
14122 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
14124 error_at (loc, "start-index and length fields necessary for "
14125 "using array notations in variable-length arrays");
14126 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14127 return error_mark_node;
14129 end_index = TYPE_MAXVAL (array_type_domain);
14130 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
14131 end_index, integer_one_node);
14132 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
14133 stride = build_int_cst (integer_type_node, 1);
14134 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
14136 else if (initial_index != error_mark_node)
14138 /* If we are here, then there should be 2 possibilities:
14139 1. Array [EXPR : EXPR]
14140 2. Array [EXPR : EXPR : EXPR]
14142 start_index = initial_index;
14144 if (TREE_CODE (array_type) == FUNCTION_TYPE)
14146 error_at (loc, "array notations cannot be used with function "
14147 "type");
14148 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
14149 return error_mark_node;
14151 c_parser_consume_token (parser); /* consume the ':' */
14152 struct c_expr ce = c_parser_expression (parser);
14153 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14154 end_index = ce.value;
14155 if (!end_index || end_index == error_mark_node)
14157 c_parser_skip_to_end_of_block_or_statement (parser);
14158 return error_mark_node;
14160 if (c_parser_peek_token (parser)->type == CPP_COLON)
14162 c_parser_consume_token (parser);
14163 ce = c_parser_expression (parser);
14164 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
14165 stride = ce.value;
14166 if (!stride || stride == error_mark_node)
14168 c_parser_skip_to_end_of_block_or_statement (parser);
14169 return error_mark_node;
14173 else
14174 c_parser_error (parser, "expected array notation expression");
14176 else
14177 c_parser_error (parser, "expected array notation expression");
14179 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14181 value_tree = build_array_notation_ref (loc, array_value, start_index,
14182 end_index, stride, type);
14183 if (value_tree != error_mark_node)
14184 SET_EXPR_LOCATION (value_tree, loc);
14185 return value_tree;
14188 #include "gt-c-c-parser.h"