svn merge -r215707:216846 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / c / c-parser.c
blob51ba1194388a4435b7bc38e2f545ff78ef69b405
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 "hash-map.h"
62 #include "is-a.h"
63 #include "plugin-api.h"
64 #include "hashtab.h"
65 #include "hash-set.h"
66 #include "machmode.h"
67 #include "hard-reg-set.h"
68 #include "function.h"
69 #include "ipa-ref.h"
70 #include "cgraph.h"
71 #include "plugin.h"
72 #include "omp-low.h"
73 #include "builtins.h"
76 /* Initialization routine for this file. */
78 void
79 c_parse_init (void)
81 /* The only initialization required is of the reserved word
82 identifiers. */
83 unsigned int i;
84 tree id;
85 int mask = 0;
87 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
88 the c_token structure. */
89 gcc_assert (RID_MAX <= 255);
91 mask |= D_CXXONLY;
92 if (!flag_isoc99)
93 mask |= D_C99;
94 if (flag_no_asm)
96 mask |= D_ASM | D_EXT;
97 if (!flag_isoc99)
98 mask |= D_EXT89;
100 if (!c_dialect_objc ())
101 mask |= D_OBJC | D_CXX_OBJC;
103 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
104 for (i = 0; i < num_c_common_reswords; i++)
106 /* If a keyword is disabled, do not enter it into the table
107 and so create a canonical spelling that isn't a keyword. */
108 if (c_common_reswords[i].disable & mask)
110 if (warn_cxx_compat
111 && (c_common_reswords[i].disable & D_CXXWARN))
113 id = get_identifier (c_common_reswords[i].word);
114 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
115 C_IS_RESERVED_WORD (id) = 1;
117 continue;
120 id = get_identifier (c_common_reswords[i].word);
121 C_SET_RID_CODE (id, c_common_reswords[i].rid);
122 C_IS_RESERVED_WORD (id) = 1;
123 ridpointers [(int) c_common_reswords[i].rid] = id;
126 for (i = 0; i < NUM_INT_N_ENTS; i++)
128 /* We always create the symbols but they aren't always supported. */
129 char name[50];
130 sprintf (name, "__int%d", int_n_data[i].bitsize);
131 id = get_identifier (xstrdup (name));
132 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
133 C_IS_RESERVED_WORD (id) = 1;
137 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
138 and the C parser. Unlike the C++ lexer, the parser structure
139 stores the lexer information instead of using a separate structure.
140 Identifiers are separated into ordinary identifiers, type names,
141 keywords and some other Objective-C types of identifiers, and some
142 look-ahead is maintained.
144 ??? It might be a good idea to lex the whole file up front (as for
145 C++). It would then be possible to share more of the C and C++
146 lexer code, if desired. */
148 /* More information about the type of a CPP_NAME token. */
149 typedef enum c_id_kind {
150 /* An ordinary identifier. */
151 C_ID_ID,
152 /* An identifier declared as a typedef name. */
153 C_ID_TYPENAME,
154 /* An identifier declared as an Objective-C class name. */
155 C_ID_CLASSNAME,
156 /* An address space identifier. */
157 C_ID_ADDRSPACE,
158 /* Not an identifier. */
159 C_ID_NONE
160 } c_id_kind;
162 /* A single C token after string literal concatenation and conversion
163 of preprocessing tokens to tokens. */
164 typedef struct GTY (()) c_token {
165 /* The kind of token. */
166 ENUM_BITFIELD (cpp_ttype) type : 8;
167 /* If this token is a CPP_NAME, this value indicates whether also
168 declared as some kind of type. Otherwise, it is C_ID_NONE. */
169 ENUM_BITFIELD (c_id_kind) id_kind : 8;
170 /* If this token is a keyword, this value indicates which keyword.
171 Otherwise, this value is RID_MAX. */
172 ENUM_BITFIELD (rid) keyword : 8;
173 /* If this token is a CPP_PRAGMA, this indicates the pragma that
174 was seen. Otherwise it is PRAGMA_NONE. */
175 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
176 /* The location at which this token was found. */
177 location_t location;
178 /* The value associated with this token, if any. */
179 tree value;
180 } c_token;
182 /* A parser structure recording information about the state and
183 context of parsing. Includes lexer information with up to two
184 tokens of look-ahead; more are not needed for C. */
185 typedef struct GTY(()) c_parser {
186 /* The look-ahead tokens. */
187 c_token * GTY((skip)) tokens;
188 /* Buffer for look-ahead tokens. */
189 c_token tokens_buf[2];
190 /* How many look-ahead tokens are available (0, 1 or 2, or
191 more if parsing from pre-lexed tokens). */
192 unsigned int tokens_avail;
193 /* True if a syntax error is being recovered from; false otherwise.
194 c_parser_error sets this flag. It should clear this flag when
195 enough tokens have been consumed to recover from the error. */
196 BOOL_BITFIELD error : 1;
197 /* True if we're processing a pragma, and shouldn't automatically
198 consume CPP_PRAGMA_EOL. */
199 BOOL_BITFIELD in_pragma : 1;
200 /* True if we're parsing the outermost block of an if statement. */
201 BOOL_BITFIELD in_if_block : 1;
202 /* True if we want to lex an untranslated string. */
203 BOOL_BITFIELD lex_untranslated_string : 1;
205 /* Objective-C specific parser/lexer information. */
207 /* True if we are in a context where the Objective-C "PQ" keywords
208 are considered keywords. */
209 BOOL_BITFIELD objc_pq_context : 1;
210 /* True if we are parsing a (potential) Objective-C foreach
211 statement. This is set to true after we parsed 'for (' and while
212 we wait for 'in' or ';' to decide if it's a standard C for loop or an
213 Objective-C foreach loop. */
214 BOOL_BITFIELD objc_could_be_foreach_context : 1;
215 /* The following flag is needed to contextualize Objective-C lexical
216 analysis. In some cases (e.g., 'int NSObject;'), it is
217 undesirable to bind an identifier to an Objective-C class, even
218 if a class with that name exists. */
219 BOOL_BITFIELD objc_need_raw_identifier : 1;
220 /* Nonzero if we're processing a __transaction statement. The value
221 is 1 | TM_STMT_ATTR_*. */
222 unsigned int in_transaction : 4;
223 /* True if we are in a context where the Objective-C "Property attribute"
224 keywords are valid. */
225 BOOL_BITFIELD objc_property_attr_context : 1;
227 /* Cilk Plus specific parser/lexer information. */
229 /* Buffer to hold all the tokens from parsing the vector attribute for the
230 SIMD-enabled functions (formerly known as elemental functions). */
231 vec <c_token, va_gc> *cilk_simd_fn_tokens;
232 } c_parser;
235 /* The actual parser and external interface. ??? Does this need to be
236 garbage-collected? */
238 static GTY (()) c_parser *the_parser;
240 /* Read in and lex a single token, storing it in *TOKEN. */
242 static void
243 c_lex_one_token (c_parser *parser, c_token *token)
245 timevar_push (TV_LEX);
247 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
248 (parser->lex_untranslated_string
249 ? C_LEX_STRING_NO_TRANSLATE : 0));
250 token->id_kind = C_ID_NONE;
251 token->keyword = RID_MAX;
252 token->pragma_kind = PRAGMA_NONE;
254 switch (token->type)
256 case CPP_NAME:
258 tree decl;
260 bool objc_force_identifier = parser->objc_need_raw_identifier;
261 if (c_dialect_objc ())
262 parser->objc_need_raw_identifier = false;
264 if (C_IS_RESERVED_WORD (token->value))
266 enum rid rid_code = C_RID_CODE (token->value);
268 if (rid_code == RID_CXX_COMPAT_WARN)
270 warning_at (token->location,
271 OPT_Wc___compat,
272 "identifier %qE conflicts with C++ keyword",
273 token->value);
275 else if (rid_code >= RID_FIRST_ADDR_SPACE
276 && rid_code <= RID_LAST_ADDR_SPACE)
278 token->id_kind = C_ID_ADDRSPACE;
279 token->keyword = rid_code;
280 break;
282 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
284 /* We found an Objective-C "pq" keyword (in, out,
285 inout, bycopy, byref, oneway). They need special
286 care because the interpretation depends on the
287 context. */
288 if (parser->objc_pq_context)
290 token->type = CPP_KEYWORD;
291 token->keyword = rid_code;
292 break;
294 else if (parser->objc_could_be_foreach_context
295 && rid_code == RID_IN)
297 /* We are in Objective-C, inside a (potential)
298 foreach context (which means after having
299 parsed 'for (', but before having parsed ';'),
300 and we found 'in'. We consider it the keyword
301 which terminates the declaration at the
302 beginning of a foreach-statement. Note that
303 this means you can't use 'in' for anything else
304 in that context; in particular, in Objective-C
305 you can't use 'in' as the name of the running
306 variable in a C for loop. We could potentially
307 try to add code here to disambiguate, but it
308 seems a reasonable limitation. */
309 token->type = CPP_KEYWORD;
310 token->keyword = rid_code;
311 break;
313 /* Else, "pq" keywords outside of the "pq" context are
314 not keywords, and we fall through to the code for
315 normal tokens. */
317 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
319 /* We found an Objective-C "property attribute"
320 keyword (getter, setter, readonly, etc). These are
321 only valid in the property context. */
322 if (parser->objc_property_attr_context)
324 token->type = CPP_KEYWORD;
325 token->keyword = rid_code;
326 break;
328 /* Else they are not special keywords.
331 else if (c_dialect_objc ()
332 && (OBJC_IS_AT_KEYWORD (rid_code)
333 || OBJC_IS_CXX_KEYWORD (rid_code)))
335 /* We found one of the Objective-C "@" keywords (defs,
336 selector, synchronized, etc) or one of the
337 Objective-C "cxx" keywords (class, private,
338 protected, public, try, catch, throw) without a
339 preceding '@' sign. Do nothing and fall through to
340 the code for normal tokens (in C++ we would still
341 consider the CXX ones keywords, but not in C). */
344 else
346 token->type = CPP_KEYWORD;
347 token->keyword = rid_code;
348 break;
352 decl = lookup_name (token->value);
353 if (decl)
355 if (TREE_CODE (decl) == TYPE_DECL)
357 token->id_kind = C_ID_TYPENAME;
358 break;
361 else if (c_dialect_objc ())
363 tree objc_interface_decl = objc_is_class_name (token->value);
364 /* Objective-C class names are in the same namespace as
365 variables and typedefs, and hence are shadowed by local
366 declarations. */
367 if (objc_interface_decl
368 && (!objc_force_identifier || global_bindings_p ()))
370 token->value = objc_interface_decl;
371 token->id_kind = C_ID_CLASSNAME;
372 break;
375 token->id_kind = C_ID_ID;
377 break;
378 case CPP_AT_NAME:
379 /* This only happens in Objective-C; it must be a keyword. */
380 token->type = CPP_KEYWORD;
381 switch (C_RID_CODE (token->value))
383 /* Replace 'class' with '@class', 'private' with '@private',
384 etc. This prevents confusion with the C++ keyword
385 'class', and makes the tokens consistent with other
386 Objective-C 'AT' keywords. For example '@class' is
387 reported as RID_AT_CLASS which is consistent with
388 '@synchronized', which is reported as
389 RID_AT_SYNCHRONIZED.
391 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
392 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
393 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
394 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
395 case RID_THROW: token->keyword = RID_AT_THROW; break;
396 case RID_TRY: token->keyword = RID_AT_TRY; break;
397 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
398 default: token->keyword = C_RID_CODE (token->value);
400 break;
401 case CPP_COLON:
402 case CPP_COMMA:
403 case CPP_CLOSE_PAREN:
404 case CPP_SEMICOLON:
405 /* These tokens may affect the interpretation of any identifiers
406 following, if doing Objective-C. */
407 if (c_dialect_objc ())
408 parser->objc_need_raw_identifier = false;
409 break;
410 case CPP_PRAGMA:
411 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
412 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
413 token->value = NULL;
414 break;
415 default:
416 break;
418 timevar_pop (TV_LEX);
421 /* Return a pointer to the next token from PARSER, reading it in if
422 necessary. */
424 static inline c_token *
425 c_parser_peek_token (c_parser *parser)
427 if (parser->tokens_avail == 0)
429 c_lex_one_token (parser, &parser->tokens[0]);
430 parser->tokens_avail = 1;
432 return &parser->tokens[0];
435 /* Return true if the next token from PARSER has the indicated
436 TYPE. */
438 static inline bool
439 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
441 return c_parser_peek_token (parser)->type == type;
444 /* Return true if the next token from PARSER does not have the
445 indicated TYPE. */
447 static inline bool
448 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
450 return !c_parser_next_token_is (parser, type);
453 /* Return true if the next token from PARSER is the indicated
454 KEYWORD. */
456 static inline bool
457 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
459 return c_parser_peek_token (parser)->keyword == keyword;
462 /* Return a pointer to the next-but-one token from PARSER, reading it
463 in if necessary. The next token is already read in. */
465 static c_token *
466 c_parser_peek_2nd_token (c_parser *parser)
468 if (parser->tokens_avail >= 2)
469 return &parser->tokens[1];
470 gcc_assert (parser->tokens_avail == 1);
471 gcc_assert (parser->tokens[0].type != CPP_EOF);
472 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
473 c_lex_one_token (parser, &parser->tokens[1]);
474 parser->tokens_avail = 2;
475 return &parser->tokens[1];
478 /* Return true if TOKEN can start a type name,
479 false otherwise. */
480 static bool
481 c_token_starts_typename (c_token *token)
483 switch (token->type)
485 case CPP_NAME:
486 switch (token->id_kind)
488 case C_ID_ID:
489 return false;
490 case C_ID_ADDRSPACE:
491 return true;
492 case C_ID_TYPENAME:
493 return true;
494 case C_ID_CLASSNAME:
495 gcc_assert (c_dialect_objc ());
496 return true;
497 default:
498 gcc_unreachable ();
500 case CPP_KEYWORD:
501 switch (token->keyword)
503 case RID_UNSIGNED:
504 case RID_LONG:
505 case RID_SHORT:
506 case RID_SIGNED:
507 case RID_COMPLEX:
508 case RID_INT:
509 case RID_CHAR:
510 case RID_FLOAT:
511 case RID_DOUBLE:
512 case RID_VOID:
513 case RID_DFLOAT32:
514 case RID_DFLOAT64:
515 case RID_DFLOAT128:
516 case RID_BOOL:
517 case RID_ENUM:
518 case RID_STRUCT:
519 case RID_UNION:
520 case RID_TYPEOF:
521 case RID_CONST:
522 case RID_ATOMIC:
523 case RID_VOLATILE:
524 case RID_RESTRICT:
525 case RID_ATTRIBUTE:
526 case RID_FRACT:
527 case RID_ACCUM:
528 case RID_SAT:
529 case RID_AUTO_TYPE:
530 return true;
531 default:
532 if (token->keyword >= RID_FIRST_INT_N
533 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
534 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
535 return true;
536 return false;
538 case CPP_LESS:
539 if (c_dialect_objc ())
540 return true;
541 return false;
542 default:
543 return false;
547 enum c_lookahead_kind {
548 /* Always treat unknown identifiers as typenames. */
549 cla_prefer_type,
551 /* Could be parsing a nonabstract declarator. Only treat an identifier
552 as a typename if followed by another identifier or a star. */
553 cla_nonabstract_decl,
555 /* Never treat identifiers as typenames. */
556 cla_prefer_id
559 /* Return true if the next token from PARSER can start a type name,
560 false otherwise. LA specifies how to do lookahead in order to
561 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
563 static inline bool
564 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
566 c_token *token = c_parser_peek_token (parser);
567 if (c_token_starts_typename (token))
568 return true;
570 /* Try a bit harder to detect an unknown typename. */
571 if (la != cla_prefer_id
572 && token->type == CPP_NAME
573 && token->id_kind == C_ID_ID
575 /* Do not try too hard when we could have "object in array". */
576 && !parser->objc_could_be_foreach_context
578 && (la == cla_prefer_type
579 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
580 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
582 /* Only unknown identifiers. */
583 && !lookup_name (token->value))
584 return true;
586 return false;
589 /* Return true if TOKEN is a type qualifier, false otherwise. */
590 static bool
591 c_token_is_qualifier (c_token *token)
593 switch (token->type)
595 case CPP_NAME:
596 switch (token->id_kind)
598 case C_ID_ADDRSPACE:
599 return true;
600 default:
601 return false;
603 case CPP_KEYWORD:
604 switch (token->keyword)
606 case RID_CONST:
607 case RID_VOLATILE:
608 case RID_RESTRICT:
609 case RID_ATTRIBUTE:
610 case RID_ATOMIC:
611 return true;
612 default:
613 return false;
615 case CPP_LESS:
616 return false;
617 default:
618 gcc_unreachable ();
622 /* Return true if the next token from PARSER is a type qualifier,
623 false otherwise. */
624 static inline bool
625 c_parser_next_token_is_qualifier (c_parser *parser)
627 c_token *token = c_parser_peek_token (parser);
628 return c_token_is_qualifier (token);
631 /* Return true if TOKEN can start declaration specifiers, false
632 otherwise. */
633 static bool
634 c_token_starts_declspecs (c_token *token)
636 switch (token->type)
638 case CPP_NAME:
639 switch (token->id_kind)
641 case C_ID_ID:
642 return false;
643 case C_ID_ADDRSPACE:
644 return true;
645 case C_ID_TYPENAME:
646 return true;
647 case C_ID_CLASSNAME:
648 gcc_assert (c_dialect_objc ());
649 return true;
650 default:
651 gcc_unreachable ();
653 case CPP_KEYWORD:
654 switch (token->keyword)
656 case RID_STATIC:
657 case RID_EXTERN:
658 case RID_REGISTER:
659 case RID_TYPEDEF:
660 case RID_INLINE:
661 case RID_NORETURN:
662 case RID_AUTO:
663 case RID_THREAD:
664 case RID_UNSIGNED:
665 case RID_LONG:
666 case RID_SHORT:
667 case RID_SIGNED:
668 case RID_COMPLEX:
669 case RID_INT:
670 case RID_CHAR:
671 case RID_FLOAT:
672 case RID_DOUBLE:
673 case RID_VOID:
674 case RID_DFLOAT32:
675 case RID_DFLOAT64:
676 case RID_DFLOAT128:
677 case RID_BOOL:
678 case RID_ENUM:
679 case RID_STRUCT:
680 case RID_UNION:
681 case RID_TYPEOF:
682 case RID_CONST:
683 case RID_VOLATILE:
684 case RID_RESTRICT:
685 case RID_ATTRIBUTE:
686 case RID_FRACT:
687 case RID_ACCUM:
688 case RID_SAT:
689 case RID_ALIGNAS:
690 case RID_ATOMIC:
691 case RID_AUTO_TYPE:
692 return true;
693 default:
694 if (token->keyword >= RID_FIRST_INT_N
695 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
696 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
697 return true;
698 return false;
700 case CPP_LESS:
701 if (c_dialect_objc ())
702 return true;
703 return false;
704 default:
705 return false;
710 /* Return true if TOKEN can start declaration specifiers or a static
711 assertion, false otherwise. */
712 static bool
713 c_token_starts_declaration (c_token *token)
715 if (c_token_starts_declspecs (token)
716 || token->keyword == RID_STATIC_ASSERT)
717 return true;
718 else
719 return false;
722 /* Return true if the next token from PARSER can start declaration
723 specifiers, false otherwise. */
724 static inline bool
725 c_parser_next_token_starts_declspecs (c_parser *parser)
727 c_token *token = c_parser_peek_token (parser);
729 /* In Objective-C, a classname normally starts a declspecs unless it
730 is immediately followed by a dot. In that case, it is the
731 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
732 setter/getter on the class. c_token_starts_declspecs() can't
733 differentiate between the two cases because it only checks the
734 current token, so we have a special check here. */
735 if (c_dialect_objc ()
736 && token->type == CPP_NAME
737 && token->id_kind == C_ID_CLASSNAME
738 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
739 return false;
741 return c_token_starts_declspecs (token);
744 /* Return true if the next tokens from PARSER can start declaration
745 specifiers or a static assertion, false otherwise. */
746 static inline bool
747 c_parser_next_tokens_start_declaration (c_parser *parser)
749 c_token *token = c_parser_peek_token (parser);
751 /* Same as above. */
752 if (c_dialect_objc ()
753 && token->type == CPP_NAME
754 && token->id_kind == C_ID_CLASSNAME
755 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
756 return false;
758 /* Labels do not start declarations. */
759 if (token->type == CPP_NAME
760 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
761 return false;
763 if (c_token_starts_declaration (token))
764 return true;
766 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
767 return true;
769 return false;
772 /* Consume the next token from PARSER. */
774 static void
775 c_parser_consume_token (c_parser *parser)
777 gcc_assert (parser->tokens_avail >= 1);
778 gcc_assert (parser->tokens[0].type != CPP_EOF);
779 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
780 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
781 if (parser->tokens != &parser->tokens_buf[0])
782 parser->tokens++;
783 else if (parser->tokens_avail == 2)
784 parser->tokens[0] = parser->tokens[1];
785 parser->tokens_avail--;
788 /* Expect the current token to be a #pragma. Consume it and remember
789 that we've begun parsing a pragma. */
791 static void
792 c_parser_consume_pragma (c_parser *parser)
794 gcc_assert (!parser->in_pragma);
795 gcc_assert (parser->tokens_avail >= 1);
796 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
797 if (parser->tokens != &parser->tokens_buf[0])
798 parser->tokens++;
799 else if (parser->tokens_avail == 2)
800 parser->tokens[0] = parser->tokens[1];
801 parser->tokens_avail--;
802 parser->in_pragma = true;
805 /* Update the global input_location from TOKEN. */
806 static inline void
807 c_parser_set_source_position_from_token (c_token *token)
809 if (token->type != CPP_EOF)
811 input_location = token->location;
815 /* Issue a diagnostic of the form
816 FILE:LINE: MESSAGE before TOKEN
817 where TOKEN is the next token in the input stream of PARSER.
818 MESSAGE (specified by the caller) is usually of the form "expected
819 OTHER-TOKEN".
821 Do not issue a diagnostic if still recovering from an error.
823 ??? This is taken from the C++ parser, but building up messages in
824 this way is not i18n-friendly and some other approach should be
825 used. */
827 static void
828 c_parser_error (c_parser *parser, const char *gmsgid)
830 c_token *token = c_parser_peek_token (parser);
831 if (parser->error)
832 return;
833 parser->error = true;
834 if (!gmsgid)
835 return;
836 /* This diagnostic makes more sense if it is tagged to the line of
837 the token we just peeked at. */
838 c_parser_set_source_position_from_token (token);
839 c_parse_error (gmsgid,
840 /* Because c_parse_error does not understand
841 CPP_KEYWORD, keywords are treated like
842 identifiers. */
843 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
844 /* ??? The C parser does not save the cpp flags of a
845 token, we need to pass 0 here and we will not get
846 the source spelling of some tokens but rather the
847 canonical spelling. */
848 token->value, /*flags=*/0);
851 /* If the next token is of the indicated TYPE, consume it. Otherwise,
852 issue the error MSGID. If MSGID is NULL then a message has already
853 been produced and no message will be produced this time. Returns
854 true if found, false otherwise. */
856 static bool
857 c_parser_require (c_parser *parser,
858 enum cpp_ttype type,
859 const char *msgid)
861 if (c_parser_next_token_is (parser, type))
863 c_parser_consume_token (parser);
864 return true;
866 else
868 c_parser_error (parser, msgid);
869 return false;
873 /* If the next token is the indicated keyword, consume it. Otherwise,
874 issue the error MSGID. Returns true if found, false otherwise. */
876 static bool
877 c_parser_require_keyword (c_parser *parser,
878 enum rid keyword,
879 const char *msgid)
881 if (c_parser_next_token_is_keyword (parser, keyword))
883 c_parser_consume_token (parser);
884 return true;
886 else
888 c_parser_error (parser, msgid);
889 return false;
893 /* Like c_parser_require, except that tokens will be skipped until the
894 desired token is found. An error message is still produced if the
895 next token is not as expected. If MSGID is NULL then a message has
896 already been produced and no message will be produced this
897 time. */
899 static void
900 c_parser_skip_until_found (c_parser *parser,
901 enum cpp_ttype type,
902 const char *msgid)
904 unsigned nesting_depth = 0;
906 if (c_parser_require (parser, type, msgid))
907 return;
909 /* Skip tokens until the desired token is found. */
910 while (true)
912 /* Peek at the next token. */
913 c_token *token = c_parser_peek_token (parser);
914 /* If we've reached the token we want, consume it and stop. */
915 if (token->type == type && !nesting_depth)
917 c_parser_consume_token (parser);
918 break;
921 /* If we've run out of tokens, stop. */
922 if (token->type == CPP_EOF)
923 return;
924 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
925 return;
926 if (token->type == CPP_OPEN_BRACE
927 || token->type == CPP_OPEN_PAREN
928 || token->type == CPP_OPEN_SQUARE)
929 ++nesting_depth;
930 else if (token->type == CPP_CLOSE_BRACE
931 || token->type == CPP_CLOSE_PAREN
932 || token->type == CPP_CLOSE_SQUARE)
934 if (nesting_depth-- == 0)
935 break;
937 /* Consume this token. */
938 c_parser_consume_token (parser);
940 parser->error = false;
943 /* Skip tokens until the end of a parameter is found, but do not
944 consume the comma, semicolon or closing delimiter. */
946 static void
947 c_parser_skip_to_end_of_parameter (c_parser *parser)
949 unsigned nesting_depth = 0;
951 while (true)
953 c_token *token = c_parser_peek_token (parser);
954 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
955 && !nesting_depth)
956 break;
957 /* If we've run out of tokens, stop. */
958 if (token->type == CPP_EOF)
959 return;
960 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
961 return;
962 if (token->type == CPP_OPEN_BRACE
963 || token->type == CPP_OPEN_PAREN
964 || token->type == CPP_OPEN_SQUARE)
965 ++nesting_depth;
966 else if (token->type == CPP_CLOSE_BRACE
967 || token->type == CPP_CLOSE_PAREN
968 || token->type == CPP_CLOSE_SQUARE)
970 if (nesting_depth-- == 0)
971 break;
973 /* Consume this token. */
974 c_parser_consume_token (parser);
976 parser->error = false;
979 /* Expect to be at the end of the pragma directive and consume an
980 end of line marker. */
982 static void
983 c_parser_skip_to_pragma_eol (c_parser *parser)
985 gcc_assert (parser->in_pragma);
986 parser->in_pragma = false;
988 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line"))
989 while (true)
991 c_token *token = c_parser_peek_token (parser);
992 if (token->type == CPP_EOF)
993 break;
994 if (token->type == CPP_PRAGMA_EOL)
996 c_parser_consume_token (parser);
997 break;
999 c_parser_consume_token (parser);
1002 parser->error = false;
1005 /* Skip tokens until we have consumed an entire block, or until we
1006 have consumed a non-nested ';'. */
1008 static void
1009 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1011 unsigned nesting_depth = 0;
1012 bool save_error = parser->error;
1014 while (true)
1016 c_token *token;
1018 /* Peek at the next token. */
1019 token = c_parser_peek_token (parser);
1021 switch (token->type)
1023 case CPP_EOF:
1024 return;
1026 case CPP_PRAGMA_EOL:
1027 if (parser->in_pragma)
1028 return;
1029 break;
1031 case CPP_SEMICOLON:
1032 /* If the next token is a ';', we have reached the
1033 end of the statement. */
1034 if (!nesting_depth)
1036 /* Consume the ';'. */
1037 c_parser_consume_token (parser);
1038 goto finished;
1040 break;
1042 case CPP_CLOSE_BRACE:
1043 /* If the next token is a non-nested '}', then we have
1044 reached the end of the current block. */
1045 if (nesting_depth == 0 || --nesting_depth == 0)
1047 c_parser_consume_token (parser);
1048 goto finished;
1050 break;
1052 case CPP_OPEN_BRACE:
1053 /* If it the next token is a '{', then we are entering a new
1054 block. Consume the entire block. */
1055 ++nesting_depth;
1056 break;
1058 case CPP_PRAGMA:
1059 /* If we see a pragma, consume the whole thing at once. We
1060 have some safeguards against consuming pragmas willy-nilly.
1061 Normally, we'd expect to be here with parser->error set,
1062 which disables these safeguards. But it's possible to get
1063 here for secondary error recovery, after parser->error has
1064 been cleared. */
1065 c_parser_consume_pragma (parser);
1066 c_parser_skip_to_pragma_eol (parser);
1067 parser->error = save_error;
1068 continue;
1070 default:
1071 break;
1074 c_parser_consume_token (parser);
1077 finished:
1078 parser->error = false;
1081 /* CPP's options (initialized by c-opts.c). */
1082 extern cpp_options *cpp_opts;
1084 /* Save the warning flags which are controlled by __extension__. */
1086 static inline int
1087 disable_extension_diagnostics (void)
1089 int ret = (pedantic
1090 | (warn_pointer_arith << 1)
1091 | (warn_traditional << 2)
1092 | (flag_iso << 3)
1093 | (warn_long_long << 4)
1094 | (warn_cxx_compat << 5)
1095 | (warn_overlength_strings << 6)
1096 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1097 play tricks to properly restore it. */
1098 | ((warn_c90_c99_compat == 1) << 7)
1099 | ((warn_c90_c99_compat == -1) << 8)
1100 /* Similarly for warn_c99_c11_compat. */
1101 | ((warn_c99_c11_compat == 1) << 9)
1102 | ((warn_c99_c11_compat == -1) << 10)
1104 cpp_opts->cpp_pedantic = pedantic = 0;
1105 warn_pointer_arith = 0;
1106 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1107 flag_iso = 0;
1108 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1109 warn_cxx_compat = 0;
1110 warn_overlength_strings = 0;
1111 warn_c90_c99_compat = 0;
1112 warn_c99_c11_compat = 0;
1113 return ret;
1116 /* Restore the warning flags which are controlled by __extension__.
1117 FLAGS is the return value from disable_extension_diagnostics. */
1119 static inline void
1120 restore_extension_diagnostics (int flags)
1122 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1123 warn_pointer_arith = (flags >> 1) & 1;
1124 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1125 flag_iso = (flags >> 3) & 1;
1126 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1127 warn_cxx_compat = (flags >> 5) & 1;
1128 warn_overlength_strings = (flags >> 6) & 1;
1129 /* See above for why is this needed. */
1130 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1131 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1134 /* Possibly kinds of declarator to parse. */
1135 typedef enum c_dtr_syn {
1136 /* A normal declarator with an identifier. */
1137 C_DTR_NORMAL,
1138 /* An abstract declarator (maybe empty). */
1139 C_DTR_ABSTRACT,
1140 /* A parameter declarator: may be either, but after a type name does
1141 not redeclare a typedef name as an identifier if it can
1142 alternatively be interpreted as a typedef name; see DR#009,
1143 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1144 following DR#249. For example, given a typedef T, "int T" and
1145 "int *T" are valid parameter declarations redeclaring T, while
1146 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1147 abstract declarators rather than involving redundant parentheses;
1148 the same applies with attributes inside the parentheses before
1149 "T". */
1150 C_DTR_PARM
1151 } c_dtr_syn;
1153 /* The binary operation precedence levels, where 0 is a dummy lowest level
1154 used for the bottom of the stack. */
1155 enum c_parser_prec {
1156 PREC_NONE,
1157 PREC_LOGOR,
1158 PREC_LOGAND,
1159 PREC_BITOR,
1160 PREC_BITXOR,
1161 PREC_BITAND,
1162 PREC_EQ,
1163 PREC_REL,
1164 PREC_SHIFT,
1165 PREC_ADD,
1166 PREC_MULT,
1167 NUM_PRECS
1170 static void c_parser_external_declaration (c_parser *);
1171 static void c_parser_asm_definition (c_parser *);
1172 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1173 bool, bool, tree *, vec<c_token>);
1174 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1175 static void c_parser_static_assert_declaration (c_parser *);
1176 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1177 bool, bool, bool, enum c_lookahead_kind);
1178 static struct c_typespec c_parser_enum_specifier (c_parser *);
1179 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1180 static tree c_parser_struct_declaration (c_parser *);
1181 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1182 static tree c_parser_alignas_specifier (c_parser *);
1183 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1184 bool *);
1185 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1186 c_dtr_syn, bool *);
1187 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1188 bool,
1189 struct c_declarator *);
1190 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1191 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1192 tree);
1193 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1194 static tree c_parser_simple_asm_expr (c_parser *);
1195 static tree c_parser_attributes (c_parser *);
1196 static struct c_type_name *c_parser_type_name (c_parser *);
1197 static struct c_expr c_parser_initializer (c_parser *);
1198 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1199 static void c_parser_initelt (c_parser *, struct obstack *);
1200 static void c_parser_initval (c_parser *, struct c_expr *,
1201 struct obstack *);
1202 static tree c_parser_compound_statement (c_parser *);
1203 static void c_parser_compound_statement_nostart (c_parser *);
1204 static void c_parser_label (c_parser *);
1205 static void c_parser_statement (c_parser *);
1206 static void c_parser_statement_after_labels (c_parser *);
1207 static void c_parser_if_statement (c_parser *);
1208 static void c_parser_switch_statement (c_parser *);
1209 static void c_parser_while_statement (c_parser *, bool);
1210 static void c_parser_do_statement (c_parser *, bool);
1211 static void c_parser_for_statement (c_parser *, bool);
1212 static tree c_parser_asm_statement (c_parser *);
1213 static tree c_parser_asm_operands (c_parser *);
1214 static tree c_parser_asm_goto_operands (c_parser *);
1215 static tree c_parser_asm_clobbers (c_parser *);
1216 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1217 tree = NULL_TREE);
1218 static struct c_expr c_parser_conditional_expression (c_parser *,
1219 struct c_expr *, tree);
1220 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1221 tree);
1222 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1223 static struct c_expr c_parser_unary_expression (c_parser *);
1224 static struct c_expr c_parser_sizeof_expression (c_parser *);
1225 static struct c_expr c_parser_alignof_expression (c_parser *);
1226 static struct c_expr c_parser_postfix_expression (c_parser *);
1227 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1228 struct c_type_name *,
1229 location_t);
1230 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1231 location_t loc,
1232 struct c_expr);
1233 static tree c_parser_transaction (c_parser *, enum rid);
1234 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1235 static tree c_parser_transaction_cancel (c_parser *);
1236 static struct c_expr c_parser_expression (c_parser *);
1237 static struct c_expr c_parser_expression_conv (c_parser *);
1238 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1239 vec<tree, va_gc> **, location_t *,
1240 tree *, vec<location_t> *,
1241 unsigned int * = NULL);
1242 static tree c_parser_oacc_loop (location_t, c_parser *, char *);
1243 static void c_parser_omp_construct (c_parser *);
1244 static void c_parser_omp_threadprivate (c_parser *);
1245 static void c_parser_oacc_update (c_parser *);
1246 static void c_parser_omp_barrier (c_parser *);
1247 static void c_parser_omp_flush (c_parser *);
1248 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1249 tree, tree *);
1250 static void c_parser_omp_taskwait (c_parser *);
1251 static void c_parser_omp_taskyield (c_parser *);
1252 static void c_parser_omp_cancel (c_parser *);
1253 static void c_parser_omp_cancellation_point (c_parser *);
1255 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1256 pragma_stmt, pragma_compound };
1257 static bool c_parser_pragma (c_parser *, enum pragma_context);
1258 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1259 static void c_parser_omp_end_declare_target (c_parser *);
1260 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1262 /* These Objective-C parser functions are only ever called when
1263 compiling Objective-C. */
1264 static void c_parser_objc_class_definition (c_parser *, tree);
1265 static void c_parser_objc_class_instance_variables (c_parser *);
1266 static void c_parser_objc_class_declaration (c_parser *);
1267 static void c_parser_objc_alias_declaration (c_parser *);
1268 static void c_parser_objc_protocol_definition (c_parser *, tree);
1269 static bool c_parser_objc_method_type (c_parser *);
1270 static void c_parser_objc_method_definition (c_parser *);
1271 static void c_parser_objc_methodprotolist (c_parser *);
1272 static void c_parser_objc_methodproto (c_parser *);
1273 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1274 static tree c_parser_objc_type_name (c_parser *);
1275 static tree c_parser_objc_protocol_refs (c_parser *);
1276 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1277 static void c_parser_objc_synchronized_statement (c_parser *);
1278 static tree c_parser_objc_selector (c_parser *);
1279 static tree c_parser_objc_selector_arg (c_parser *);
1280 static tree c_parser_objc_receiver (c_parser *);
1281 static tree c_parser_objc_message_args (c_parser *);
1282 static tree c_parser_objc_keywordexpr (c_parser *);
1283 static void c_parser_objc_at_property_declaration (c_parser *);
1284 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1285 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1286 static bool c_parser_objc_diagnose_bad_element_prefix
1287 (c_parser *, struct c_declspecs *);
1289 /* Cilk Plus supporting routines. */
1290 static void c_parser_cilk_simd (c_parser *);
1291 static void c_parser_cilk_for (c_parser *, tree);
1292 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1293 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1294 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1295 static void c_parser_cilk_grainsize (c_parser *);
1297 /* Parse a translation unit (C90 6.7, C99 6.9).
1299 translation-unit:
1300 external-declarations
1302 external-declarations:
1303 external-declaration
1304 external-declarations external-declaration
1306 GNU extensions:
1308 translation-unit:
1309 empty
1312 static void
1313 c_parser_translation_unit (c_parser *parser)
1315 if (c_parser_next_token_is (parser, CPP_EOF))
1317 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1318 "ISO C forbids an empty translation unit");
1320 else
1322 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1323 mark_valid_location_for_stdc_pragma (false);
1326 ggc_collect ();
1327 c_parser_external_declaration (parser);
1328 obstack_free (&parser_obstack, obstack_position);
1330 while (c_parser_next_token_is_not (parser, CPP_EOF));
1334 /* Parse an external declaration (C90 6.7, C99 6.9).
1336 external-declaration:
1337 function-definition
1338 declaration
1340 GNU extensions:
1342 external-declaration:
1343 asm-definition
1345 __extension__ external-declaration
1347 Objective-C:
1349 external-declaration:
1350 objc-class-definition
1351 objc-class-declaration
1352 objc-alias-declaration
1353 objc-protocol-definition
1354 objc-method-definition
1355 @end
1358 static void
1359 c_parser_external_declaration (c_parser *parser)
1361 int ext;
1362 switch (c_parser_peek_token (parser)->type)
1364 case CPP_KEYWORD:
1365 switch (c_parser_peek_token (parser)->keyword)
1367 case RID_EXTENSION:
1368 ext = disable_extension_diagnostics ();
1369 c_parser_consume_token (parser);
1370 c_parser_external_declaration (parser);
1371 restore_extension_diagnostics (ext);
1372 break;
1373 case RID_ASM:
1374 c_parser_asm_definition (parser);
1375 break;
1376 case RID_AT_INTERFACE:
1377 case RID_AT_IMPLEMENTATION:
1378 gcc_assert (c_dialect_objc ());
1379 c_parser_objc_class_definition (parser, NULL_TREE);
1380 break;
1381 case RID_AT_CLASS:
1382 gcc_assert (c_dialect_objc ());
1383 c_parser_objc_class_declaration (parser);
1384 break;
1385 case RID_AT_ALIAS:
1386 gcc_assert (c_dialect_objc ());
1387 c_parser_objc_alias_declaration (parser);
1388 break;
1389 case RID_AT_PROTOCOL:
1390 gcc_assert (c_dialect_objc ());
1391 c_parser_objc_protocol_definition (parser, NULL_TREE);
1392 break;
1393 case RID_AT_PROPERTY:
1394 gcc_assert (c_dialect_objc ());
1395 c_parser_objc_at_property_declaration (parser);
1396 break;
1397 case RID_AT_SYNTHESIZE:
1398 gcc_assert (c_dialect_objc ());
1399 c_parser_objc_at_synthesize_declaration (parser);
1400 break;
1401 case RID_AT_DYNAMIC:
1402 gcc_assert (c_dialect_objc ());
1403 c_parser_objc_at_dynamic_declaration (parser);
1404 break;
1405 case RID_AT_END:
1406 gcc_assert (c_dialect_objc ());
1407 c_parser_consume_token (parser);
1408 objc_finish_implementation ();
1409 break;
1410 default:
1411 goto decl_or_fndef;
1413 break;
1414 case CPP_SEMICOLON:
1415 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1416 "ISO C does not allow extra %<;%> outside of a function");
1417 c_parser_consume_token (parser);
1418 break;
1419 case CPP_PRAGMA:
1420 mark_valid_location_for_stdc_pragma (true);
1421 c_parser_pragma (parser, pragma_external);
1422 mark_valid_location_for_stdc_pragma (false);
1423 break;
1424 case CPP_PLUS:
1425 case CPP_MINUS:
1426 if (c_dialect_objc ())
1428 c_parser_objc_method_definition (parser);
1429 break;
1431 /* Else fall through, and yield a syntax error trying to parse
1432 as a declaration or function definition. */
1433 default:
1434 decl_or_fndef:
1435 /* A declaration or a function definition (or, in Objective-C,
1436 an @interface or @protocol with prefix attributes). We can
1437 only tell which after parsing the declaration specifiers, if
1438 any, and the first declarator. */
1439 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1440 NULL, vNULL);
1441 break;
1445 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1447 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1448 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1449 accepted; otherwise (old-style parameter declarations) only other
1450 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1451 assertion is accepted; otherwise (old-style parameter declarations)
1452 it is not. If NESTED is true, we are inside a function or parsing
1453 old-style parameter declarations; any functions encountered are
1454 nested functions and declaration specifiers are required; otherwise
1455 we are at top level and functions are normal functions and
1456 declaration specifiers may be optional. If EMPTY_OK is true, empty
1457 declarations are OK (subject to all other constraints); otherwise
1458 (old-style parameter declarations) they are diagnosed. If
1459 START_ATTR_OK is true, the declaration specifiers may start with
1460 attributes; otherwise they may not.
1461 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1462 declaration when parsing an Objective-C foreach statement.
1464 declaration:
1465 declaration-specifiers init-declarator-list[opt] ;
1466 static_assert-declaration
1468 function-definition:
1469 declaration-specifiers[opt] declarator declaration-list[opt]
1470 compound-statement
1472 declaration-list:
1473 declaration
1474 declaration-list declaration
1476 init-declarator-list:
1477 init-declarator
1478 init-declarator-list , init-declarator
1480 init-declarator:
1481 declarator simple-asm-expr[opt] attributes[opt]
1482 declarator simple-asm-expr[opt] attributes[opt] = initializer
1484 GNU extensions:
1486 nested-function-definition:
1487 declaration-specifiers declarator declaration-list[opt]
1488 compound-statement
1490 Objective-C:
1491 attributes objc-class-definition
1492 attributes objc-category-definition
1493 attributes objc-protocol-definition
1495 The simple-asm-expr and attributes are GNU extensions.
1497 This function does not handle __extension__; that is handled in its
1498 callers. ??? Following the old parser, __extension__ may start
1499 external declarations, declarations in functions and declarations
1500 at the start of "for" loops, but not old-style parameter
1501 declarations.
1503 C99 requires declaration specifiers in a function definition; the
1504 absence is diagnosed through the diagnosis of implicit int. In GNU
1505 C we also allow but diagnose declarations without declaration
1506 specifiers, but only at top level (elsewhere they conflict with
1507 other syntax).
1509 In Objective-C, declarations of the looping variable in a foreach
1510 statement are exceptionally terminated by 'in' (for example, 'for
1511 (NSObject *object in array) { ... }').
1513 OpenMP:
1515 declaration:
1516 threadprivate-directive */
1518 static void
1519 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1520 bool static_assert_ok, bool empty_ok,
1521 bool nested, bool start_attr_ok,
1522 tree *objc_foreach_object_declaration,
1523 vec<c_token> omp_declare_simd_clauses)
1525 struct c_declspecs *specs;
1526 tree prefix_attrs;
1527 tree all_prefix_attrs;
1528 bool diagnosed_no_specs = false;
1529 location_t here = c_parser_peek_token (parser)->location;
1531 if (static_assert_ok
1532 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1534 c_parser_static_assert_declaration (parser);
1535 return;
1537 specs = build_null_declspecs ();
1539 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1540 if (c_parser_peek_token (parser)->type == CPP_NAME
1541 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1542 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1543 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1544 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1546 error_at (here, "unknown type name %qE",
1547 c_parser_peek_token (parser)->value);
1549 /* Parse declspecs normally to get a correct pointer type, but avoid
1550 a further "fails to be a type name" error. Refuse nested functions
1551 since it is not how the user likely wants us to recover. */
1552 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1553 c_parser_peek_token (parser)->keyword = RID_VOID;
1554 c_parser_peek_token (parser)->value = error_mark_node;
1555 fndef_ok = !nested;
1558 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1559 true, true, cla_nonabstract_decl);
1560 if (parser->error)
1562 c_parser_skip_to_end_of_block_or_statement (parser);
1563 return;
1565 if (nested && !specs->declspecs_seen_p)
1567 c_parser_error (parser, "expected declaration specifiers");
1568 c_parser_skip_to_end_of_block_or_statement (parser);
1569 return;
1571 finish_declspecs (specs);
1572 bool auto_type_p = specs->typespec_word == cts_auto_type;
1573 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1575 if (auto_type_p)
1576 error_at (here, "%<__auto_type%> in empty declaration");
1577 else if (empty_ok)
1578 shadow_tag (specs);
1579 else
1581 shadow_tag_warned (specs, 1);
1582 pedwarn (here, 0, "empty declaration");
1584 c_parser_consume_token (parser);
1585 return;
1588 /* Provide better error recovery. Note that a type name here is usually
1589 better diagnosed as a redeclaration. */
1590 if (empty_ok
1591 && specs->typespec_kind == ctsk_tagdef
1592 && c_parser_next_token_starts_declspecs (parser)
1593 && !c_parser_next_token_is (parser, CPP_NAME))
1595 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1596 parser->error = false;
1597 shadow_tag_warned (specs, 1);
1598 return;
1600 else if (c_dialect_objc () && !auto_type_p)
1602 /* Prefix attributes are an error on method decls. */
1603 switch (c_parser_peek_token (parser)->type)
1605 case CPP_PLUS:
1606 case CPP_MINUS:
1607 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1608 return;
1609 if (specs->attrs)
1611 warning_at (c_parser_peek_token (parser)->location,
1612 OPT_Wattributes,
1613 "prefix attributes are ignored for methods");
1614 specs->attrs = NULL_TREE;
1616 if (fndef_ok)
1617 c_parser_objc_method_definition (parser);
1618 else
1619 c_parser_objc_methodproto (parser);
1620 return;
1621 break;
1622 default:
1623 break;
1625 /* This is where we parse 'attributes @interface ...',
1626 'attributes @implementation ...', 'attributes @protocol ...'
1627 (where attributes could be, for example, __attribute__
1628 ((deprecated)).
1630 switch (c_parser_peek_token (parser)->keyword)
1632 case RID_AT_INTERFACE:
1634 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1635 return;
1636 c_parser_objc_class_definition (parser, specs->attrs);
1637 return;
1639 break;
1640 case RID_AT_IMPLEMENTATION:
1642 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1643 return;
1644 if (specs->attrs)
1646 warning_at (c_parser_peek_token (parser)->location,
1647 OPT_Wattributes,
1648 "prefix attributes are ignored for implementations");
1649 specs->attrs = NULL_TREE;
1651 c_parser_objc_class_definition (parser, NULL_TREE);
1652 return;
1654 break;
1655 case RID_AT_PROTOCOL:
1657 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1658 return;
1659 c_parser_objc_protocol_definition (parser, specs->attrs);
1660 return;
1662 break;
1663 case RID_AT_ALIAS:
1664 case RID_AT_CLASS:
1665 case RID_AT_END:
1666 case RID_AT_PROPERTY:
1667 if (specs->attrs)
1669 c_parser_error (parser, "unexpected attribute");
1670 specs->attrs = NULL;
1672 break;
1673 default:
1674 break;
1678 pending_xref_error ();
1679 prefix_attrs = specs->attrs;
1680 all_prefix_attrs = prefix_attrs;
1681 specs->attrs = NULL_TREE;
1682 while (true)
1684 struct c_declarator *declarator;
1685 bool dummy = false;
1686 timevar_id_t tv;
1687 tree fnbody;
1688 /* Declaring either one or more declarators (in which case we
1689 should diagnose if there were no declaration specifiers) or a
1690 function definition (in which case the diagnostic for
1691 implicit int suffices). */
1692 declarator = c_parser_declarator (parser,
1693 specs->typespec_kind != ctsk_none,
1694 C_DTR_NORMAL, &dummy);
1695 if (declarator == NULL)
1697 if (omp_declare_simd_clauses.exists ()
1698 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1699 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1700 omp_declare_simd_clauses);
1701 c_parser_skip_to_end_of_block_or_statement (parser);
1702 return;
1704 if (auto_type_p && declarator->kind != cdk_id)
1706 error_at (here,
1707 "%<__auto_type%> requires a plain identifier"
1708 " as declarator");
1709 c_parser_skip_to_end_of_block_or_statement (parser);
1710 return;
1712 if (c_parser_next_token_is (parser, CPP_EQ)
1713 || c_parser_next_token_is (parser, CPP_COMMA)
1714 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1715 || c_parser_next_token_is_keyword (parser, RID_ASM)
1716 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1717 || c_parser_next_token_is_keyword (parser, RID_IN))
1719 tree asm_name = NULL_TREE;
1720 tree postfix_attrs = NULL_TREE;
1721 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1723 diagnosed_no_specs = true;
1724 pedwarn (here, 0, "data definition has no type or storage class");
1726 /* Having seen a data definition, there cannot now be a
1727 function definition. */
1728 fndef_ok = false;
1729 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1730 asm_name = c_parser_simple_asm_expr (parser);
1731 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1733 postfix_attrs = c_parser_attributes (parser);
1734 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1736 /* This means there is an attribute specifier after
1737 the declarator in a function definition. Provide
1738 some more information for the user. */
1739 error_at (here, "attributes should be specified before the "
1740 "declarator in a function definition");
1741 c_parser_skip_to_end_of_block_or_statement (parser);
1742 return;
1745 if (c_parser_next_token_is (parser, CPP_EQ))
1747 tree d;
1748 struct c_expr init;
1749 location_t init_loc;
1750 c_parser_consume_token (parser);
1751 if (auto_type_p)
1753 start_init (NULL_TREE, asm_name, global_bindings_p ());
1754 init_loc = c_parser_peek_token (parser)->location;
1755 init = c_parser_expr_no_commas (parser, NULL);
1756 if (TREE_CODE (init.value) == COMPONENT_REF
1757 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1758 error_at (here,
1759 "%<__auto_type%> used with a bit-field"
1760 " initializer");
1761 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1762 tree init_type = TREE_TYPE (init.value);
1763 /* As with typeof, remove all qualifiers from atomic types. */
1764 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1765 init_type
1766 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1767 bool vm_type = variably_modified_type_p (init_type,
1768 NULL_TREE);
1769 if (vm_type)
1770 init.value = c_save_expr (init.value);
1771 finish_init ();
1772 specs->typespec_kind = ctsk_typeof;
1773 specs->locations[cdw_typedef] = init_loc;
1774 specs->typedef_p = true;
1775 specs->type = init_type;
1776 if (vm_type)
1778 bool maybe_const = true;
1779 tree type_expr = c_fully_fold (init.value, false,
1780 &maybe_const);
1781 specs->expr_const_operands &= maybe_const;
1782 if (specs->expr)
1783 specs->expr = build2 (COMPOUND_EXPR,
1784 TREE_TYPE (type_expr),
1785 specs->expr, type_expr);
1786 else
1787 specs->expr = type_expr;
1789 d = start_decl (declarator, specs, true,
1790 chainon (postfix_attrs, all_prefix_attrs));
1791 if (!d)
1792 d = error_mark_node;
1793 if (omp_declare_simd_clauses.exists ()
1794 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1795 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1796 omp_declare_simd_clauses);
1798 else
1800 /* The declaration of the variable is in effect while
1801 its initializer is parsed. */
1802 d = start_decl (declarator, specs, true,
1803 chainon (postfix_attrs, all_prefix_attrs));
1804 if (!d)
1805 d = error_mark_node;
1806 if (omp_declare_simd_clauses.exists ()
1807 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1808 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1809 omp_declare_simd_clauses);
1810 start_init (d, asm_name, global_bindings_p ());
1811 init_loc = c_parser_peek_token (parser)->location;
1812 init = c_parser_initializer (parser);
1813 finish_init ();
1815 if (d != error_mark_node)
1817 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1818 finish_decl (d, init_loc, init.value,
1819 init.original_type, asm_name);
1822 else
1824 if (auto_type_p)
1826 error_at (here,
1827 "%<__auto_type%> requires an initialized "
1828 "data declaration");
1829 c_parser_skip_to_end_of_block_or_statement (parser);
1830 return;
1832 tree d = start_decl (declarator, specs, false,
1833 chainon (postfix_attrs,
1834 all_prefix_attrs));
1835 if (omp_declare_simd_clauses.exists ()
1836 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1838 tree parms = NULL_TREE;
1839 if (d && TREE_CODE (d) == FUNCTION_DECL)
1841 struct c_declarator *ce = declarator;
1842 while (ce != NULL)
1843 if (ce->kind == cdk_function)
1845 parms = ce->u.arg_info->parms;
1846 break;
1848 else
1849 ce = ce->declarator;
1851 if (parms)
1852 temp_store_parm_decls (d, parms);
1853 c_finish_omp_declare_simd (parser, d, parms,
1854 omp_declare_simd_clauses);
1855 if (parms)
1856 temp_pop_parm_decls ();
1858 if (d)
1859 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1860 NULL_TREE, asm_name);
1862 if (c_parser_next_token_is_keyword (parser, RID_IN))
1864 if (d)
1865 *objc_foreach_object_declaration = d;
1866 else
1867 *objc_foreach_object_declaration = error_mark_node;
1870 if (c_parser_next_token_is (parser, CPP_COMMA))
1872 if (auto_type_p)
1874 error_at (here,
1875 "%<__auto_type%> may only be used with"
1876 " a single declarator");
1877 c_parser_skip_to_end_of_block_or_statement (parser);
1878 return;
1880 c_parser_consume_token (parser);
1881 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1882 all_prefix_attrs = chainon (c_parser_attributes (parser),
1883 prefix_attrs);
1884 else
1885 all_prefix_attrs = prefix_attrs;
1886 continue;
1888 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1890 c_parser_consume_token (parser);
1891 return;
1893 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1895 /* This can only happen in Objective-C: we found the
1896 'in' that terminates the declaration inside an
1897 Objective-C foreach statement. Do not consume the
1898 token, so that the caller can use it to determine
1899 that this indeed is a foreach context. */
1900 return;
1902 else
1904 c_parser_error (parser, "expected %<,%> or %<;%>");
1905 c_parser_skip_to_end_of_block_or_statement (parser);
1906 return;
1909 else if (auto_type_p)
1911 error_at (here,
1912 "%<__auto_type%> requires an initialized data declaration");
1913 c_parser_skip_to_end_of_block_or_statement (parser);
1914 return;
1916 else if (!fndef_ok)
1918 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1919 "%<asm%> or %<__attribute__%>");
1920 c_parser_skip_to_end_of_block_or_statement (parser);
1921 return;
1923 /* Function definition (nested or otherwise). */
1924 if (nested)
1926 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1927 c_push_function_context ();
1929 if (!start_function (specs, declarator, all_prefix_attrs))
1931 /* This can appear in many cases looking nothing like a
1932 function definition, so we don't give a more specific
1933 error suggesting there was one. */
1934 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1935 "or %<__attribute__%>");
1936 if (nested)
1937 c_pop_function_context ();
1938 break;
1941 if (DECL_DECLARED_INLINE_P (current_function_decl))
1942 tv = TV_PARSE_INLINE;
1943 else
1944 tv = TV_PARSE_FUNC;
1945 timevar_push (tv);
1947 /* Parse old-style parameter declarations. ??? Attributes are
1948 not allowed to start declaration specifiers here because of a
1949 syntax conflict between a function declaration with attribute
1950 suffix and a function definition with an attribute prefix on
1951 first old-style parameter declaration. Following the old
1952 parser, they are not accepted on subsequent old-style
1953 parameter declarations either. However, there is no
1954 ambiguity after the first declaration, nor indeed on the
1955 first as long as we don't allow postfix attributes after a
1956 declarator with a nonempty identifier list in a definition;
1957 and postfix attributes have never been accepted here in
1958 function definitions either. */
1959 while (c_parser_next_token_is_not (parser, CPP_EOF)
1960 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
1961 c_parser_declaration_or_fndef (parser, false, false, false,
1962 true, false, NULL, vNULL);
1963 store_parm_decls ();
1964 if (omp_declare_simd_clauses.exists ()
1965 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1966 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
1967 omp_declare_simd_clauses);
1968 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
1969 = c_parser_peek_token (parser)->location;
1970 fnbody = c_parser_compound_statement (parser);
1971 if (flag_cilkplus && contains_array_notation_expr (fnbody))
1972 fnbody = expand_array_notation_exprs (fnbody);
1973 if (nested)
1975 tree decl = current_function_decl;
1976 /* Mark nested functions as needing static-chain initially.
1977 lower_nested_functions will recompute it but the
1978 DECL_STATIC_CHAIN flag is also used before that happens,
1979 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
1980 DECL_STATIC_CHAIN (decl) = 1;
1981 add_stmt (fnbody);
1982 finish_function ();
1983 c_pop_function_context ();
1984 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
1986 else
1988 add_stmt (fnbody);
1989 finish_function ();
1992 timevar_pop (tv);
1993 break;
1997 /* Parse an asm-definition (asm() outside a function body). This is a
1998 GNU extension.
2000 asm-definition:
2001 simple-asm-expr ;
2004 static void
2005 c_parser_asm_definition (c_parser *parser)
2007 tree asm_str = c_parser_simple_asm_expr (parser);
2008 if (asm_str)
2009 symtab->finalize_toplevel_asm (asm_str);
2010 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2013 /* Parse a static assertion (C11 6.7.10).
2015 static_assert-declaration:
2016 static_assert-declaration-no-semi ;
2019 static void
2020 c_parser_static_assert_declaration (c_parser *parser)
2022 c_parser_static_assert_declaration_no_semi (parser);
2023 if (parser->error
2024 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2025 c_parser_skip_to_end_of_block_or_statement (parser);
2028 /* Parse a static assertion (C11 6.7.10), without the trailing
2029 semicolon.
2031 static_assert-declaration-no-semi:
2032 _Static_assert ( constant-expression , string-literal )
2035 static void
2036 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2038 location_t assert_loc, value_loc;
2039 tree value;
2040 tree string;
2042 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2043 assert_loc = c_parser_peek_token (parser)->location;
2044 if (flag_isoc99)
2045 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2046 "ISO C99 does not support %<_Static_assert%>");
2047 else
2048 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2049 "ISO C90 does not support %<_Static_assert%>");
2050 c_parser_consume_token (parser);
2051 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2052 return;
2053 value_loc = c_parser_peek_token (parser)->location;
2054 value = c_parser_expr_no_commas (parser, NULL).value;
2055 parser->lex_untranslated_string = true;
2056 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2058 parser->lex_untranslated_string = false;
2059 return;
2061 switch (c_parser_peek_token (parser)->type)
2063 case CPP_STRING:
2064 case CPP_STRING16:
2065 case CPP_STRING32:
2066 case CPP_WSTRING:
2067 case CPP_UTF8STRING:
2068 string = c_parser_peek_token (parser)->value;
2069 c_parser_consume_token (parser);
2070 parser->lex_untranslated_string = false;
2071 break;
2072 default:
2073 c_parser_error (parser, "expected string literal");
2074 parser->lex_untranslated_string = false;
2075 return;
2077 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2079 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2081 error_at (value_loc, "expression in static assertion is not an integer");
2082 return;
2084 if (TREE_CODE (value) != INTEGER_CST)
2086 value = c_fully_fold (value, false, NULL);
2087 /* Strip no-op conversions. */
2088 STRIP_TYPE_NOPS (value);
2089 if (TREE_CODE (value) == INTEGER_CST)
2090 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2091 "is not an integer constant expression");
2093 if (TREE_CODE (value) != INTEGER_CST)
2095 error_at (value_loc, "expression in static assertion is not constant");
2096 return;
2098 constant_expression_warning (value);
2099 if (integer_zerop (value))
2100 error_at (assert_loc, "static assertion failed: %E", string);
2103 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2104 6.7), adding them to SPECS (which may already include some).
2105 Storage class specifiers are accepted iff SCSPEC_OK; type
2106 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2107 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2108 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2110 declaration-specifiers:
2111 storage-class-specifier declaration-specifiers[opt]
2112 type-specifier declaration-specifiers[opt]
2113 type-qualifier declaration-specifiers[opt]
2114 function-specifier declaration-specifiers[opt]
2115 alignment-specifier declaration-specifiers[opt]
2117 Function specifiers (inline) are from C99, and are currently
2118 handled as storage class specifiers, as is __thread. Alignment
2119 specifiers are from C11.
2121 C90 6.5.1, C99 6.7.1:
2122 storage-class-specifier:
2123 typedef
2124 extern
2125 static
2126 auto
2127 register
2128 _Thread_local
2130 (_Thread_local is new in C11.)
2132 C99 6.7.4:
2133 function-specifier:
2134 inline
2135 _Noreturn
2137 (_Noreturn is new in C11.)
2139 C90 6.5.2, C99 6.7.2:
2140 type-specifier:
2141 void
2142 char
2143 short
2145 long
2146 float
2147 double
2148 signed
2149 unsigned
2150 _Bool
2151 _Complex
2152 [_Imaginary removed in C99 TC2]
2153 struct-or-union-specifier
2154 enum-specifier
2155 typedef-name
2156 atomic-type-specifier
2158 (_Bool and _Complex are new in C99.)
2159 (atomic-type-specifier is new in C11.)
2161 C90 6.5.3, C99 6.7.3:
2163 type-qualifier:
2164 const
2165 restrict
2166 volatile
2167 address-space-qualifier
2168 _Atomic
2170 (restrict is new in C99.)
2171 (_Atomic is new in C11.)
2173 GNU extensions:
2175 declaration-specifiers:
2176 attributes declaration-specifiers[opt]
2178 type-qualifier:
2179 address-space
2181 address-space:
2182 identifier recognized by the target
2184 storage-class-specifier:
2185 __thread
2187 type-specifier:
2188 typeof-specifier
2189 __auto_type
2190 __intN
2191 _Decimal32
2192 _Decimal64
2193 _Decimal128
2194 _Fract
2195 _Accum
2196 _Sat
2198 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2199 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2201 atomic-type-specifier
2202 _Atomic ( type-name )
2204 Objective-C:
2206 type-specifier:
2207 class-name objc-protocol-refs[opt]
2208 typedef-name objc-protocol-refs
2209 objc-protocol-refs
2212 static void
2213 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2214 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2215 bool alignspec_ok, bool auto_type_ok,
2216 enum c_lookahead_kind la)
2218 bool attrs_ok = start_attr_ok;
2219 bool seen_type = specs->typespec_kind != ctsk_none;
2221 if (!typespec_ok)
2222 gcc_assert (la == cla_prefer_id);
2224 while (c_parser_next_token_is (parser, CPP_NAME)
2225 || c_parser_next_token_is (parser, CPP_KEYWORD)
2226 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2228 struct c_typespec t;
2229 tree attrs;
2230 tree align;
2231 location_t loc = c_parser_peek_token (parser)->location;
2233 /* If we cannot accept a type, exit if the next token must start
2234 one. Also, if we already have seen a tagged definition,
2235 a typename would be an error anyway and likely the user
2236 has simply forgotten a semicolon, so we exit. */
2237 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2238 && c_parser_next_tokens_start_typename (parser, la)
2239 && !c_parser_next_token_is_qualifier (parser))
2240 break;
2242 if (c_parser_next_token_is (parser, CPP_NAME))
2244 c_token *name_token = c_parser_peek_token (parser);
2245 tree value = name_token->value;
2246 c_id_kind kind = name_token->id_kind;
2248 if (kind == C_ID_ADDRSPACE)
2250 addr_space_t as
2251 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2252 declspecs_add_addrspace (name_token->location, specs, as);
2253 c_parser_consume_token (parser);
2254 attrs_ok = true;
2255 continue;
2258 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2260 /* If we cannot accept a type, and the next token must start one,
2261 exit. Do the same if we already have seen a tagged definition,
2262 since it would be an error anyway and likely the user has simply
2263 forgotten a semicolon. */
2264 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2265 break;
2267 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2268 a C_ID_CLASSNAME. */
2269 c_parser_consume_token (parser);
2270 seen_type = true;
2271 attrs_ok = true;
2272 if (kind == C_ID_ID)
2274 error_at (loc, "unknown type name %qE", value);
2275 t.kind = ctsk_typedef;
2276 t.spec = error_mark_node;
2278 else if (kind == C_ID_TYPENAME
2279 && (!c_dialect_objc ()
2280 || c_parser_next_token_is_not (parser, CPP_LESS)))
2282 t.kind = ctsk_typedef;
2283 /* For a typedef name, record the meaning, not the name.
2284 In case of 'foo foo, bar;'. */
2285 t.spec = lookup_name (value);
2287 else
2289 tree proto = NULL_TREE;
2290 gcc_assert (c_dialect_objc ());
2291 t.kind = ctsk_objc;
2292 if (c_parser_next_token_is (parser, CPP_LESS))
2293 proto = c_parser_objc_protocol_refs (parser);
2294 t.spec = objc_get_protocol_qualified_type (value, proto);
2296 t.expr = NULL_TREE;
2297 t.expr_const_operands = true;
2298 declspecs_add_type (name_token->location, specs, t);
2299 continue;
2301 if (c_parser_next_token_is (parser, CPP_LESS))
2303 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2304 nisse@lysator.liu.se. */
2305 tree proto;
2306 gcc_assert (c_dialect_objc ());
2307 if (!typespec_ok || seen_type)
2308 break;
2309 proto = c_parser_objc_protocol_refs (parser);
2310 t.kind = ctsk_objc;
2311 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2312 t.expr = NULL_TREE;
2313 t.expr_const_operands = true;
2314 declspecs_add_type (loc, specs, t);
2315 continue;
2317 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2318 switch (c_parser_peek_token (parser)->keyword)
2320 case RID_STATIC:
2321 case RID_EXTERN:
2322 case RID_REGISTER:
2323 case RID_TYPEDEF:
2324 case RID_INLINE:
2325 case RID_NORETURN:
2326 case RID_AUTO:
2327 case RID_THREAD:
2328 if (!scspec_ok)
2329 goto out;
2330 attrs_ok = true;
2331 /* TODO: Distinguish between function specifiers (inline, noreturn)
2332 and storage class specifiers, either here or in
2333 declspecs_add_scspec. */
2334 declspecs_add_scspec (loc, specs,
2335 c_parser_peek_token (parser)->value);
2336 c_parser_consume_token (parser);
2337 break;
2338 case RID_AUTO_TYPE:
2339 if (!auto_type_ok)
2340 goto out;
2341 /* Fall through. */
2342 case RID_UNSIGNED:
2343 case RID_LONG:
2344 case RID_SHORT:
2345 case RID_SIGNED:
2346 case RID_COMPLEX:
2347 case RID_INT:
2348 case RID_CHAR:
2349 case RID_FLOAT:
2350 case RID_DOUBLE:
2351 case RID_VOID:
2352 case RID_DFLOAT32:
2353 case RID_DFLOAT64:
2354 case RID_DFLOAT128:
2355 case RID_BOOL:
2356 case RID_FRACT:
2357 case RID_ACCUM:
2358 case RID_SAT:
2359 case RID_INT_N_0:
2360 case RID_INT_N_1:
2361 case RID_INT_N_2:
2362 case RID_INT_N_3:
2363 if (!typespec_ok)
2364 goto out;
2365 attrs_ok = true;
2366 seen_type = true;
2367 if (c_dialect_objc ())
2368 parser->objc_need_raw_identifier = true;
2369 t.kind = ctsk_resword;
2370 t.spec = c_parser_peek_token (parser)->value;
2371 t.expr = NULL_TREE;
2372 t.expr_const_operands = true;
2373 declspecs_add_type (loc, specs, t);
2374 c_parser_consume_token (parser);
2375 break;
2376 case RID_ENUM:
2377 if (!typespec_ok)
2378 goto out;
2379 attrs_ok = true;
2380 seen_type = true;
2381 t = c_parser_enum_specifier (parser);
2382 declspecs_add_type (loc, specs, t);
2383 break;
2384 case RID_STRUCT:
2385 case RID_UNION:
2386 if (!typespec_ok)
2387 goto out;
2388 attrs_ok = true;
2389 seen_type = true;
2390 t = c_parser_struct_or_union_specifier (parser);
2391 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2392 declspecs_add_type (loc, specs, t);
2393 break;
2394 case RID_TYPEOF:
2395 /* ??? The old parser rejected typeof after other type
2396 specifiers, but is a syntax error the best way of
2397 handling this? */
2398 if (!typespec_ok || seen_type)
2399 goto out;
2400 attrs_ok = true;
2401 seen_type = true;
2402 t = c_parser_typeof_specifier (parser);
2403 declspecs_add_type (loc, specs, t);
2404 break;
2405 case RID_ATOMIC:
2406 /* C parser handling of Objective-C constructs needs
2407 checking for correct lvalue-to-rvalue conversions, and
2408 the code in build_modify_expr handling various
2409 Objective-C cases, and that in build_unary_op handling
2410 Objective-C cases for increment / decrement, also needs
2411 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2412 and objc_types_are_equivalent may also need updates. */
2413 if (c_dialect_objc ())
2414 sorry ("%<_Atomic%> in Objective-C");
2415 /* C parser handling of OpenMP constructs needs checking for
2416 correct lvalue-to-rvalue conversions. */
2417 if (flag_openmp)
2418 sorry ("%<_Atomic%> with OpenMP");
2419 if (flag_isoc99)
2420 pedwarn_c99 (loc, OPT_Wpedantic,
2421 "ISO C99 does not support the %<_Atomic%> qualifier");
2422 else
2423 pedwarn_c99 (loc, OPT_Wpedantic,
2424 "ISO C90 does not support the %<_Atomic%> qualifier");
2425 attrs_ok = true;
2426 tree value;
2427 value = c_parser_peek_token (parser)->value;
2428 c_parser_consume_token (parser);
2429 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2431 /* _Atomic ( type-name ). */
2432 seen_type = true;
2433 c_parser_consume_token (parser);
2434 struct c_type_name *type = c_parser_type_name (parser);
2435 t.kind = ctsk_typeof;
2436 t.spec = error_mark_node;
2437 t.expr = NULL_TREE;
2438 t.expr_const_operands = true;
2439 if (type != NULL)
2440 t.spec = groktypename (type, &t.expr,
2441 &t.expr_const_operands);
2442 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2443 "expected %<)%>");
2444 if (t.spec != error_mark_node)
2446 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2447 error_at (loc, "%<_Atomic%>-qualified array type");
2448 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2449 error_at (loc, "%<_Atomic%>-qualified function type");
2450 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2451 error_at (loc, "%<_Atomic%> applied to a qualified type");
2452 else
2453 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2455 declspecs_add_type (loc, specs, t);
2457 else
2458 declspecs_add_qual (loc, specs, value);
2459 break;
2460 case RID_CONST:
2461 case RID_VOLATILE:
2462 case RID_RESTRICT:
2463 attrs_ok = true;
2464 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2465 c_parser_consume_token (parser);
2466 break;
2467 case RID_ATTRIBUTE:
2468 if (!attrs_ok)
2469 goto out;
2470 attrs = c_parser_attributes (parser);
2471 declspecs_add_attrs (loc, specs, attrs);
2472 break;
2473 case RID_ALIGNAS:
2474 if (!alignspec_ok)
2475 goto out;
2476 align = c_parser_alignas_specifier (parser);
2477 declspecs_add_alignas (loc, specs, align);
2478 break;
2479 default:
2480 goto out;
2483 out: ;
2486 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2488 enum-specifier:
2489 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2490 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2491 enum attributes[opt] identifier
2493 The form with trailing comma is new in C99. The forms with
2494 attributes are GNU extensions. In GNU C, we accept any expression
2495 without commas in the syntax (assignment expressions, not just
2496 conditional expressions); assignment expressions will be diagnosed
2497 as non-constant.
2499 enumerator-list:
2500 enumerator
2501 enumerator-list , enumerator
2503 enumerator:
2504 enumeration-constant
2505 enumeration-constant = constant-expression
2508 static struct c_typespec
2509 c_parser_enum_specifier (c_parser *parser)
2511 struct c_typespec ret;
2512 tree attrs;
2513 tree ident = NULL_TREE;
2514 location_t enum_loc;
2515 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2516 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2517 enum_loc = c_parser_peek_token (parser)->location;
2518 c_parser_consume_token (parser);
2519 attrs = c_parser_attributes (parser);
2520 enum_loc = c_parser_peek_token (parser)->location;
2521 /* Set the location in case we create a decl now. */
2522 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2523 if (c_parser_next_token_is (parser, CPP_NAME))
2525 ident = c_parser_peek_token (parser)->value;
2526 ident_loc = c_parser_peek_token (parser)->location;
2527 enum_loc = ident_loc;
2528 c_parser_consume_token (parser);
2530 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2532 /* Parse an enum definition. */
2533 struct c_enum_contents the_enum;
2534 tree type;
2535 tree postfix_attrs;
2536 /* We chain the enumerators in reverse order, then put them in
2537 forward order at the end. */
2538 tree values;
2539 timevar_push (TV_PARSE_ENUM);
2540 type = start_enum (enum_loc, &the_enum, ident);
2541 values = NULL_TREE;
2542 c_parser_consume_token (parser);
2543 while (true)
2545 tree enum_id;
2546 tree enum_value;
2547 tree enum_decl;
2548 bool seen_comma;
2549 c_token *token;
2550 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2551 location_t decl_loc, value_loc;
2552 if (c_parser_next_token_is_not (parser, CPP_NAME))
2554 c_parser_error (parser, "expected identifier");
2555 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2556 values = error_mark_node;
2557 break;
2559 token = c_parser_peek_token (parser);
2560 enum_id = token->value;
2561 /* Set the location in case we create a decl now. */
2562 c_parser_set_source_position_from_token (token);
2563 decl_loc = value_loc = token->location;
2564 c_parser_consume_token (parser);
2565 if (c_parser_next_token_is (parser, CPP_EQ))
2567 c_parser_consume_token (parser);
2568 value_loc = c_parser_peek_token (parser)->location;
2569 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2571 else
2572 enum_value = NULL_TREE;
2573 enum_decl = build_enumerator (decl_loc, value_loc,
2574 &the_enum, enum_id, enum_value);
2575 TREE_CHAIN (enum_decl) = values;
2576 values = enum_decl;
2577 seen_comma = false;
2578 if (c_parser_next_token_is (parser, CPP_COMMA))
2580 comma_loc = c_parser_peek_token (parser)->location;
2581 seen_comma = true;
2582 c_parser_consume_token (parser);
2584 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2586 if (seen_comma)
2587 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2588 "comma at end of enumerator list");
2589 c_parser_consume_token (parser);
2590 break;
2592 if (!seen_comma)
2594 c_parser_error (parser, "expected %<,%> or %<}%>");
2595 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2596 values = error_mark_node;
2597 break;
2600 postfix_attrs = c_parser_attributes (parser);
2601 ret.spec = finish_enum (type, nreverse (values),
2602 chainon (attrs, postfix_attrs));
2603 ret.kind = ctsk_tagdef;
2604 ret.expr = NULL_TREE;
2605 ret.expr_const_operands = true;
2606 timevar_pop (TV_PARSE_ENUM);
2607 return ret;
2609 else if (!ident)
2611 c_parser_error (parser, "expected %<{%>");
2612 ret.spec = error_mark_node;
2613 ret.kind = ctsk_tagref;
2614 ret.expr = NULL_TREE;
2615 ret.expr_const_operands = true;
2616 return ret;
2618 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2619 /* In ISO C, enumerated types can be referred to only if already
2620 defined. */
2621 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2623 gcc_assert (ident);
2624 pedwarn (enum_loc, OPT_Wpedantic,
2625 "ISO C forbids forward references to %<enum%> types");
2627 return ret;
2630 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2632 struct-or-union-specifier:
2633 struct-or-union attributes[opt] identifier[opt]
2634 { struct-contents } attributes[opt]
2635 struct-or-union attributes[opt] identifier
2637 struct-contents:
2638 struct-declaration-list
2640 struct-declaration-list:
2641 struct-declaration ;
2642 struct-declaration-list struct-declaration ;
2644 GNU extensions:
2646 struct-contents:
2647 empty
2648 struct-declaration
2649 struct-declaration-list struct-declaration
2651 struct-declaration-list:
2652 struct-declaration-list ;
2655 (Note that in the syntax here, unlike that in ISO C, the semicolons
2656 are included here rather than in struct-declaration, in order to
2657 describe the syntax with extra semicolons and missing semicolon at
2658 end.)
2660 Objective-C:
2662 struct-declaration-list:
2663 @defs ( class-name )
2665 (Note this does not include a trailing semicolon, but can be
2666 followed by further declarations, and gets a pedwarn-if-pedantic
2667 when followed by a semicolon.) */
2669 static struct c_typespec
2670 c_parser_struct_or_union_specifier (c_parser *parser)
2672 struct c_typespec ret;
2673 tree attrs;
2674 tree ident = NULL_TREE;
2675 location_t struct_loc;
2676 location_t ident_loc = UNKNOWN_LOCATION;
2677 enum tree_code code;
2678 switch (c_parser_peek_token (parser)->keyword)
2680 case RID_STRUCT:
2681 code = RECORD_TYPE;
2682 break;
2683 case RID_UNION:
2684 code = UNION_TYPE;
2685 break;
2686 default:
2687 gcc_unreachable ();
2689 struct_loc = c_parser_peek_token (parser)->location;
2690 c_parser_consume_token (parser);
2691 attrs = c_parser_attributes (parser);
2693 /* Set the location in case we create a decl now. */
2694 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2696 if (c_parser_next_token_is (parser, CPP_NAME))
2698 ident = c_parser_peek_token (parser)->value;
2699 ident_loc = c_parser_peek_token (parser)->location;
2700 struct_loc = ident_loc;
2701 c_parser_consume_token (parser);
2703 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2705 /* Parse a struct or union definition. Start the scope of the
2706 tag before parsing components. */
2707 struct c_struct_parse_info *struct_info;
2708 tree type = start_struct (struct_loc, code, ident, &struct_info);
2709 tree postfix_attrs;
2710 /* We chain the components in reverse order, then put them in
2711 forward order at the end. Each struct-declaration may
2712 declare multiple components (comma-separated), so we must use
2713 chainon to join them, although when parsing each
2714 struct-declaration we can use TREE_CHAIN directly.
2716 The theory behind all this is that there will be more
2717 semicolon separated fields than comma separated fields, and
2718 so we'll be minimizing the number of node traversals required
2719 by chainon. */
2720 tree contents;
2721 timevar_push (TV_PARSE_STRUCT);
2722 contents = NULL_TREE;
2723 c_parser_consume_token (parser);
2724 /* Handle the Objective-C @defs construct,
2725 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2726 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2728 tree name;
2729 gcc_assert (c_dialect_objc ());
2730 c_parser_consume_token (parser);
2731 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2732 goto end_at_defs;
2733 if (c_parser_next_token_is (parser, CPP_NAME)
2734 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2736 name = c_parser_peek_token (parser)->value;
2737 c_parser_consume_token (parser);
2739 else
2741 c_parser_error (parser, "expected class name");
2742 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2743 goto end_at_defs;
2745 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2746 "expected %<)%>");
2747 contents = nreverse (objc_get_class_ivars (name));
2749 end_at_defs:
2750 /* Parse the struct-declarations and semicolons. Problems with
2751 semicolons are diagnosed here; empty structures are diagnosed
2752 elsewhere. */
2753 while (true)
2755 tree decls;
2756 /* Parse any stray semicolon. */
2757 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2759 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2760 "extra semicolon in struct or union specified");
2761 c_parser_consume_token (parser);
2762 continue;
2764 /* Stop if at the end of the struct or union contents. */
2765 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2767 c_parser_consume_token (parser);
2768 break;
2770 /* Accept #pragmas at struct scope. */
2771 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2773 c_parser_pragma (parser, pragma_struct);
2774 continue;
2776 /* Parse some comma-separated declarations, but not the
2777 trailing semicolon if any. */
2778 decls = c_parser_struct_declaration (parser);
2779 contents = chainon (decls, contents);
2780 /* If no semicolon follows, either we have a parse error or
2781 are at the end of the struct or union and should
2782 pedwarn. */
2783 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2784 c_parser_consume_token (parser);
2785 else
2787 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2788 pedwarn (c_parser_peek_token (parser)->location, 0,
2789 "no semicolon at end of struct or union");
2790 else if (parser->error
2791 || !c_parser_next_token_starts_declspecs (parser))
2793 c_parser_error (parser, "expected %<;%>");
2794 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2795 break;
2798 /* If we come here, we have already emitted an error
2799 for an expected `;', identifier or `(', and we also
2800 recovered already. Go on with the next field. */
2803 postfix_attrs = c_parser_attributes (parser);
2804 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2805 chainon (attrs, postfix_attrs), struct_info);
2806 ret.kind = ctsk_tagdef;
2807 ret.expr = NULL_TREE;
2808 ret.expr_const_operands = true;
2809 timevar_pop (TV_PARSE_STRUCT);
2810 return ret;
2812 else if (!ident)
2814 c_parser_error (parser, "expected %<{%>");
2815 ret.spec = error_mark_node;
2816 ret.kind = ctsk_tagref;
2817 ret.expr = NULL_TREE;
2818 ret.expr_const_operands = true;
2819 return ret;
2821 ret = parser_xref_tag (ident_loc, code, ident);
2822 return ret;
2825 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2826 the trailing semicolon.
2828 struct-declaration:
2829 specifier-qualifier-list struct-declarator-list
2830 static_assert-declaration-no-semi
2832 specifier-qualifier-list:
2833 type-specifier specifier-qualifier-list[opt]
2834 type-qualifier specifier-qualifier-list[opt]
2835 attributes specifier-qualifier-list[opt]
2837 struct-declarator-list:
2838 struct-declarator
2839 struct-declarator-list , attributes[opt] struct-declarator
2841 struct-declarator:
2842 declarator attributes[opt]
2843 declarator[opt] : constant-expression attributes[opt]
2845 GNU extensions:
2847 struct-declaration:
2848 __extension__ struct-declaration
2849 specifier-qualifier-list
2851 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2852 of attributes where shown is a GNU extension. In GNU C, we accept
2853 any expression without commas in the syntax (assignment
2854 expressions, not just conditional expressions); assignment
2855 expressions will be diagnosed as non-constant. */
2857 static tree
2858 c_parser_struct_declaration (c_parser *parser)
2860 struct c_declspecs *specs;
2861 tree prefix_attrs;
2862 tree all_prefix_attrs;
2863 tree decls;
2864 location_t decl_loc;
2865 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2867 int ext;
2868 tree decl;
2869 ext = disable_extension_diagnostics ();
2870 c_parser_consume_token (parser);
2871 decl = c_parser_struct_declaration (parser);
2872 restore_extension_diagnostics (ext);
2873 return decl;
2875 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2877 c_parser_static_assert_declaration_no_semi (parser);
2878 return NULL_TREE;
2880 specs = build_null_declspecs ();
2881 decl_loc = c_parser_peek_token (parser)->location;
2882 /* Strictly by the standard, we shouldn't allow _Alignas here,
2883 but it appears to have been intended to allow it there, so
2884 we're keeping it as it is until WG14 reaches a conclusion
2885 of N1731.
2886 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2887 c_parser_declspecs (parser, specs, false, true, true,
2888 true, false, cla_nonabstract_decl);
2889 if (parser->error)
2890 return NULL_TREE;
2891 if (!specs->declspecs_seen_p)
2893 c_parser_error (parser, "expected specifier-qualifier-list");
2894 return NULL_TREE;
2896 finish_declspecs (specs);
2897 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2898 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2900 tree ret;
2901 if (specs->typespec_kind == ctsk_none)
2903 pedwarn (decl_loc, OPT_Wpedantic,
2904 "ISO C forbids member declarations with no members");
2905 shadow_tag_warned (specs, pedantic);
2906 ret = NULL_TREE;
2908 else
2910 /* Support for unnamed structs or unions as members of
2911 structs or unions (which is [a] useful and [b] supports
2912 MS P-SDK). */
2913 tree attrs = NULL;
2915 ret = grokfield (c_parser_peek_token (parser)->location,
2916 build_id_declarator (NULL_TREE), specs,
2917 NULL_TREE, &attrs);
2918 if (ret)
2919 decl_attributes (&ret, attrs, 0);
2921 return ret;
2924 /* Provide better error recovery. Note that a type name here is valid,
2925 and will be treated as a field name. */
2926 if (specs->typespec_kind == ctsk_tagdef
2927 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2928 && c_parser_next_token_starts_declspecs (parser)
2929 && !c_parser_next_token_is (parser, CPP_NAME))
2931 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
2932 parser->error = false;
2933 return NULL_TREE;
2936 pending_xref_error ();
2937 prefix_attrs = specs->attrs;
2938 all_prefix_attrs = prefix_attrs;
2939 specs->attrs = NULL_TREE;
2940 decls = NULL_TREE;
2941 while (true)
2943 /* Declaring one or more declarators or un-named bit-fields. */
2944 struct c_declarator *declarator;
2945 bool dummy = false;
2946 if (c_parser_next_token_is (parser, CPP_COLON))
2947 declarator = build_id_declarator (NULL_TREE);
2948 else
2949 declarator = c_parser_declarator (parser,
2950 specs->typespec_kind != ctsk_none,
2951 C_DTR_NORMAL, &dummy);
2952 if (declarator == NULL)
2954 c_parser_skip_to_end_of_block_or_statement (parser);
2955 break;
2957 if (c_parser_next_token_is (parser, CPP_COLON)
2958 || c_parser_next_token_is (parser, CPP_COMMA)
2959 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2960 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2961 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2963 tree postfix_attrs = NULL_TREE;
2964 tree width = NULL_TREE;
2965 tree d;
2966 if (c_parser_next_token_is (parser, CPP_COLON))
2968 c_parser_consume_token (parser);
2969 width = c_parser_expr_no_commas (parser, NULL).value;
2971 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2972 postfix_attrs = c_parser_attributes (parser);
2973 d = grokfield (c_parser_peek_token (parser)->location,
2974 declarator, specs, width, &all_prefix_attrs);
2975 decl_attributes (&d, chainon (postfix_attrs,
2976 all_prefix_attrs), 0);
2977 DECL_CHAIN (d) = decls;
2978 decls = d;
2979 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2980 all_prefix_attrs = chainon (c_parser_attributes (parser),
2981 prefix_attrs);
2982 else
2983 all_prefix_attrs = prefix_attrs;
2984 if (c_parser_next_token_is (parser, CPP_COMMA))
2985 c_parser_consume_token (parser);
2986 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2987 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2989 /* Semicolon consumed in caller. */
2990 break;
2992 else
2994 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
2995 break;
2998 else
3000 c_parser_error (parser,
3001 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3002 "%<__attribute__%>");
3003 break;
3006 return decls;
3009 /* Parse a typeof specifier (a GNU extension).
3011 typeof-specifier:
3012 typeof ( expression )
3013 typeof ( type-name )
3016 static struct c_typespec
3017 c_parser_typeof_specifier (c_parser *parser)
3019 struct c_typespec ret;
3020 ret.kind = ctsk_typeof;
3021 ret.spec = error_mark_node;
3022 ret.expr = NULL_TREE;
3023 ret.expr_const_operands = true;
3024 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3025 c_parser_consume_token (parser);
3026 c_inhibit_evaluation_warnings++;
3027 in_typeof++;
3028 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3030 c_inhibit_evaluation_warnings--;
3031 in_typeof--;
3032 return ret;
3034 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3036 struct c_type_name *type = c_parser_type_name (parser);
3037 c_inhibit_evaluation_warnings--;
3038 in_typeof--;
3039 if (type != NULL)
3041 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3042 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3045 else
3047 bool was_vm;
3048 location_t here = c_parser_peek_token (parser)->location;
3049 struct c_expr expr = c_parser_expression (parser);
3050 c_inhibit_evaluation_warnings--;
3051 in_typeof--;
3052 if (TREE_CODE (expr.value) == COMPONENT_REF
3053 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3054 error_at (here, "%<typeof%> applied to a bit-field");
3055 mark_exp_read (expr.value);
3056 ret.spec = TREE_TYPE (expr.value);
3057 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3058 /* This is returned with the type so that when the type is
3059 evaluated, this can be evaluated. */
3060 if (was_vm)
3061 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3062 pop_maybe_used (was_vm);
3063 /* For use in macros such as those in <stdatomic.h>, remove all
3064 qualifiers from atomic types. (const can be an issue for more macros
3065 using typeof than just the <stdatomic.h> ones.) */
3066 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3067 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3069 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3070 return ret;
3073 /* Parse an alignment-specifier.
3075 C11 6.7.5:
3077 alignment-specifier:
3078 _Alignas ( type-name )
3079 _Alignas ( constant-expression )
3082 static tree
3083 c_parser_alignas_specifier (c_parser * parser)
3085 tree ret = error_mark_node;
3086 location_t loc = c_parser_peek_token (parser)->location;
3087 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3088 c_parser_consume_token (parser);
3089 if (flag_isoc99)
3090 pedwarn_c99 (loc, OPT_Wpedantic,
3091 "ISO C99 does not support %<_Alignas%>");
3092 else
3093 pedwarn_c99 (loc, OPT_Wpedantic,
3094 "ISO C90 does not support %<_Alignas%>");
3095 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3096 return ret;
3097 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3099 struct c_type_name *type = c_parser_type_name (parser);
3100 if (type != NULL)
3101 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3102 false, true, 1);
3104 else
3105 ret = c_parser_expr_no_commas (parser, NULL).value;
3106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3107 return ret;
3110 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3111 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3112 be redeclared; otherwise it may not. KIND indicates which kind of
3113 declarator is wanted. Returns a valid declarator except in the
3114 case of a syntax error in which case NULL is returned. *SEEN_ID is
3115 set to true if an identifier being declared is seen; this is used
3116 to diagnose bad forms of abstract array declarators and to
3117 determine whether an identifier list is syntactically permitted.
3119 declarator:
3120 pointer[opt] direct-declarator
3122 direct-declarator:
3123 identifier
3124 ( attributes[opt] declarator )
3125 direct-declarator array-declarator
3126 direct-declarator ( parameter-type-list )
3127 direct-declarator ( identifier-list[opt] )
3129 pointer:
3130 * type-qualifier-list[opt]
3131 * type-qualifier-list[opt] pointer
3133 type-qualifier-list:
3134 type-qualifier
3135 attributes
3136 type-qualifier-list type-qualifier
3137 type-qualifier-list attributes
3139 array-declarator:
3140 [ type-qualifier-list[opt] assignment-expression[opt] ]
3141 [ static type-qualifier-list[opt] assignment-expression ]
3142 [ type-qualifier-list static assignment-expression ]
3143 [ type-qualifier-list[opt] * ]
3145 parameter-type-list:
3146 parameter-list
3147 parameter-list , ...
3149 parameter-list:
3150 parameter-declaration
3151 parameter-list , parameter-declaration
3153 parameter-declaration:
3154 declaration-specifiers declarator attributes[opt]
3155 declaration-specifiers abstract-declarator[opt] attributes[opt]
3157 identifier-list:
3158 identifier
3159 identifier-list , identifier
3161 abstract-declarator:
3162 pointer
3163 pointer[opt] direct-abstract-declarator
3165 direct-abstract-declarator:
3166 ( attributes[opt] abstract-declarator )
3167 direct-abstract-declarator[opt] array-declarator
3168 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3170 GNU extensions:
3172 direct-declarator:
3173 direct-declarator ( parameter-forward-declarations
3174 parameter-type-list[opt] )
3176 direct-abstract-declarator:
3177 direct-abstract-declarator[opt] ( parameter-forward-declarations
3178 parameter-type-list[opt] )
3180 parameter-forward-declarations:
3181 parameter-list ;
3182 parameter-forward-declarations parameter-list ;
3184 The uses of attributes shown above are GNU extensions.
3186 Some forms of array declarator are not included in C99 in the
3187 syntax for abstract declarators; these are disallowed elsewhere.
3188 This may be a defect (DR#289).
3190 This function also accepts an omitted abstract declarator as being
3191 an abstract declarator, although not part of the formal syntax. */
3193 static struct c_declarator *
3194 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3195 bool *seen_id)
3197 /* Parse any initial pointer part. */
3198 if (c_parser_next_token_is (parser, CPP_MULT))
3200 struct c_declspecs *quals_attrs = build_null_declspecs ();
3201 struct c_declarator *inner;
3202 c_parser_consume_token (parser);
3203 c_parser_declspecs (parser, quals_attrs, false, false, true,
3204 false, false, cla_prefer_id);
3205 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3206 if (inner == NULL)
3207 return NULL;
3208 else
3209 return make_pointer_declarator (quals_attrs, inner);
3211 /* Now we have a direct declarator, direct abstract declarator or
3212 nothing (which counts as a direct abstract declarator here). */
3213 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3216 /* Parse a direct declarator or direct abstract declarator; arguments
3217 as c_parser_declarator. */
3219 static struct c_declarator *
3220 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3221 bool *seen_id)
3223 /* The direct declarator must start with an identifier (possibly
3224 omitted) or a parenthesized declarator (possibly abstract). In
3225 an ordinary declarator, initial parentheses must start a
3226 parenthesized declarator. In an abstract declarator or parameter
3227 declarator, they could start a parenthesized declarator or a
3228 parameter list. To tell which, the open parenthesis and any
3229 following attributes must be read. If a declaration specifier
3230 follows, then it is a parameter list; if the specifier is a
3231 typedef name, there might be an ambiguity about redeclaring it,
3232 which is resolved in the direction of treating it as a typedef
3233 name. If a close parenthesis follows, it is also an empty
3234 parameter list, as the syntax does not permit empty abstract
3235 declarators. Otherwise, it is a parenthesized declarator (in
3236 which case the analysis may be repeated inside it, recursively).
3238 ??? There is an ambiguity in a parameter declaration "int
3239 (__attribute__((foo)) x)", where x is not a typedef name: it
3240 could be an abstract declarator for a function, or declare x with
3241 parentheses. The proper resolution of this ambiguity needs
3242 documenting. At present we follow an accident of the old
3243 parser's implementation, whereby the first parameter must have
3244 some declaration specifiers other than just attributes. Thus as
3245 a parameter declaration it is treated as a parenthesized
3246 parameter named x, and as an abstract declarator it is
3247 rejected.
3249 ??? Also following the old parser, attributes inside an empty
3250 parameter list are ignored, making it a list not yielding a
3251 prototype, rather than giving an error or making it have one
3252 parameter with implicit type int.
3254 ??? Also following the old parser, typedef names may be
3255 redeclared in declarators, but not Objective-C class names. */
3257 if (kind != C_DTR_ABSTRACT
3258 && c_parser_next_token_is (parser, CPP_NAME)
3259 && ((type_seen_p
3260 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3261 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3262 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3264 struct c_declarator *inner
3265 = build_id_declarator (c_parser_peek_token (parser)->value);
3266 *seen_id = true;
3267 inner->id_loc = c_parser_peek_token (parser)->location;
3268 c_parser_consume_token (parser);
3269 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3272 if (kind != C_DTR_NORMAL
3273 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3275 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3276 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3279 /* Either we are at the end of an abstract declarator, or we have
3280 parentheses. */
3282 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3284 tree attrs;
3285 struct c_declarator *inner;
3286 c_parser_consume_token (parser);
3287 attrs = c_parser_attributes (parser);
3288 if (kind != C_DTR_NORMAL
3289 && (c_parser_next_token_starts_declspecs (parser)
3290 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3292 struct c_arg_info *args
3293 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3294 attrs);
3295 if (args == NULL)
3296 return NULL;
3297 else
3299 inner
3300 = build_function_declarator (args,
3301 build_id_declarator (NULL_TREE));
3302 return c_parser_direct_declarator_inner (parser, *seen_id,
3303 inner);
3306 /* A parenthesized declarator. */
3307 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3308 if (inner != NULL && attrs != NULL)
3309 inner = build_attrs_declarator (attrs, inner);
3310 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3312 c_parser_consume_token (parser);
3313 if (inner == NULL)
3314 return NULL;
3315 else
3316 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3318 else
3320 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3321 "expected %<)%>");
3322 return NULL;
3325 else
3327 if (kind == C_DTR_NORMAL)
3329 c_parser_error (parser, "expected identifier or %<(%>");
3330 return NULL;
3332 else
3333 return build_id_declarator (NULL_TREE);
3337 /* Parse part of a direct declarator or direct abstract declarator,
3338 given that some (in INNER) has already been parsed; ID_PRESENT is
3339 true if an identifier is present, false for an abstract
3340 declarator. */
3342 static struct c_declarator *
3343 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3344 struct c_declarator *inner)
3346 /* Parse a sequence of array declarators and parameter lists. */
3347 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3349 location_t brace_loc = c_parser_peek_token (parser)->location;
3350 struct c_declarator *declarator;
3351 struct c_declspecs *quals_attrs = build_null_declspecs ();
3352 bool static_seen;
3353 bool star_seen;
3354 struct c_expr dimen;
3355 dimen.value = NULL_TREE;
3356 dimen.original_code = ERROR_MARK;
3357 dimen.original_type = NULL_TREE;
3358 c_parser_consume_token (parser);
3359 c_parser_declspecs (parser, quals_attrs, false, false, true,
3360 false, false, cla_prefer_id);
3361 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3362 if (static_seen)
3363 c_parser_consume_token (parser);
3364 if (static_seen && !quals_attrs->declspecs_seen_p)
3365 c_parser_declspecs (parser, quals_attrs, false, false, true,
3366 false, false, cla_prefer_id);
3367 if (!quals_attrs->declspecs_seen_p)
3368 quals_attrs = NULL;
3369 /* If "static" is present, there must be an array dimension.
3370 Otherwise, there may be a dimension, "*", or no
3371 dimension. */
3372 if (static_seen)
3374 star_seen = false;
3375 dimen = c_parser_expr_no_commas (parser, NULL);
3377 else
3379 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3381 dimen.value = NULL_TREE;
3382 star_seen = false;
3384 else if (flag_cilkplus
3385 && c_parser_next_token_is (parser, CPP_COLON))
3387 dimen.value = error_mark_node;
3388 star_seen = false;
3389 error_at (c_parser_peek_token (parser)->location,
3390 "array notations cannot be used in declaration");
3391 c_parser_consume_token (parser);
3393 else if (c_parser_next_token_is (parser, CPP_MULT))
3395 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3397 dimen.value = NULL_TREE;
3398 star_seen = true;
3399 c_parser_consume_token (parser);
3401 else
3403 star_seen = false;
3404 dimen = c_parser_expr_no_commas (parser, NULL);
3407 else
3409 star_seen = false;
3410 dimen = c_parser_expr_no_commas (parser, NULL);
3413 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3414 c_parser_consume_token (parser);
3415 else if (flag_cilkplus
3416 && c_parser_next_token_is (parser, CPP_COLON))
3418 error_at (c_parser_peek_token (parser)->location,
3419 "array notations cannot be used in declaration");
3420 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3421 return NULL;
3423 else
3425 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3426 "expected %<]%>");
3427 return NULL;
3429 if (dimen.value)
3430 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3431 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3432 static_seen, star_seen);
3433 if (declarator == NULL)
3434 return NULL;
3435 inner = set_array_declarator_inner (declarator, inner);
3436 return c_parser_direct_declarator_inner (parser, id_present, inner);
3438 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3440 tree attrs;
3441 struct c_arg_info *args;
3442 c_parser_consume_token (parser);
3443 attrs = c_parser_attributes (parser);
3444 args = c_parser_parms_declarator (parser, id_present, attrs);
3445 if (args == NULL)
3446 return NULL;
3447 else
3449 inner = build_function_declarator (args, inner);
3450 return c_parser_direct_declarator_inner (parser, id_present, inner);
3453 return inner;
3456 /* Parse a parameter list or identifier list, including the closing
3457 parenthesis but not the opening one. ATTRS are the attributes at
3458 the start of the list. ID_LIST_OK is true if an identifier list is
3459 acceptable; such a list must not have attributes at the start. */
3461 static struct c_arg_info *
3462 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3464 push_scope ();
3465 declare_parm_level ();
3466 /* If the list starts with an identifier, it is an identifier list.
3467 Otherwise, it is either a prototype list or an empty list. */
3468 if (id_list_ok
3469 && !attrs
3470 && c_parser_next_token_is (parser, CPP_NAME)
3471 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3473 /* Look ahead to detect typos in type names. */
3474 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3475 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3476 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3477 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3479 tree list = NULL_TREE, *nextp = &list;
3480 while (c_parser_next_token_is (parser, CPP_NAME)
3481 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3483 *nextp = build_tree_list (NULL_TREE,
3484 c_parser_peek_token (parser)->value);
3485 nextp = & TREE_CHAIN (*nextp);
3486 c_parser_consume_token (parser);
3487 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3488 break;
3489 c_parser_consume_token (parser);
3490 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3492 c_parser_error (parser, "expected identifier");
3493 break;
3496 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3498 struct c_arg_info *ret = build_arg_info ();
3499 ret->types = list;
3500 c_parser_consume_token (parser);
3501 pop_scope ();
3502 return ret;
3504 else
3506 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3507 "expected %<)%>");
3508 pop_scope ();
3509 return NULL;
3512 else
3514 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3515 NULL);
3516 pop_scope ();
3517 return ret;
3521 /* Parse a parameter list (possibly empty), including the closing
3522 parenthesis but not the opening one. ATTRS are the attributes at
3523 the start of the list. EXPR is NULL or an expression that needs to
3524 be evaluated for the side effects of array size expressions in the
3525 parameters. */
3527 static struct c_arg_info *
3528 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3530 bool bad_parm = false;
3532 /* ??? Following the old parser, forward parameter declarations may
3533 use abstract declarators, and if no real parameter declarations
3534 follow the forward declarations then this is not diagnosed. Also
3535 note as above that attributes are ignored as the only contents of
3536 the parentheses, or as the only contents after forward
3537 declarations. */
3538 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3540 struct c_arg_info *ret = build_arg_info ();
3541 c_parser_consume_token (parser);
3542 return ret;
3544 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3546 struct c_arg_info *ret = build_arg_info ();
3548 if (flag_allow_parameterless_variadic_functions)
3550 /* F (...) is allowed. */
3551 ret->types = NULL_TREE;
3553 else
3555 /* Suppress -Wold-style-definition for this case. */
3556 ret->types = error_mark_node;
3557 error_at (c_parser_peek_token (parser)->location,
3558 "ISO C requires a named argument before %<...%>");
3560 c_parser_consume_token (parser);
3561 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3563 c_parser_consume_token (parser);
3564 return ret;
3566 else
3568 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3569 "expected %<)%>");
3570 return NULL;
3573 /* Nonempty list of parameters, either terminated with semicolon
3574 (forward declarations; recurse) or with close parenthesis (normal
3575 function) or with ", ... )" (variadic function). */
3576 while (true)
3578 /* Parse a parameter. */
3579 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3580 attrs = NULL_TREE;
3581 if (parm == NULL)
3582 bad_parm = true;
3583 else
3584 push_parm_decl (parm, &expr);
3585 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3587 tree new_attrs;
3588 c_parser_consume_token (parser);
3589 mark_forward_parm_decls ();
3590 new_attrs = c_parser_attributes (parser);
3591 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3593 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3595 c_parser_consume_token (parser);
3596 if (bad_parm)
3597 return NULL;
3598 else
3599 return get_parm_info (false, expr);
3601 if (!c_parser_require (parser, CPP_COMMA,
3602 "expected %<;%>, %<,%> or %<)%>"))
3604 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3605 return NULL;
3607 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3609 c_parser_consume_token (parser);
3610 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3612 c_parser_consume_token (parser);
3613 if (bad_parm)
3614 return NULL;
3615 else
3616 return get_parm_info (true, expr);
3618 else
3620 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3621 "expected %<)%>");
3622 return NULL;
3628 /* Parse a parameter declaration. ATTRS are the attributes at the
3629 start of the declaration if it is the first parameter. */
3631 static struct c_parm *
3632 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3634 struct c_declspecs *specs;
3635 struct c_declarator *declarator;
3636 tree prefix_attrs;
3637 tree postfix_attrs = NULL_TREE;
3638 bool dummy = false;
3640 /* Accept #pragmas between parameter declarations. */
3641 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3642 c_parser_pragma (parser, pragma_param);
3644 if (!c_parser_next_token_starts_declspecs (parser))
3646 c_token *token = c_parser_peek_token (parser);
3647 if (parser->error)
3648 return NULL;
3649 c_parser_set_source_position_from_token (token);
3650 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3652 error_at (token->location, "unknown type name %qE", token->value);
3653 parser->error = true;
3655 /* ??? In some Objective-C cases '...' isn't applicable so there
3656 should be a different message. */
3657 else
3658 c_parser_error (parser,
3659 "expected declaration specifiers or %<...%>");
3660 c_parser_skip_to_end_of_parameter (parser);
3661 return NULL;
3663 specs = build_null_declspecs ();
3664 if (attrs)
3666 declspecs_add_attrs (input_location, specs, attrs);
3667 attrs = NULL_TREE;
3669 c_parser_declspecs (parser, specs, true, true, true, true, false,
3670 cla_nonabstract_decl);
3671 finish_declspecs (specs);
3672 pending_xref_error ();
3673 prefix_attrs = specs->attrs;
3674 specs->attrs = NULL_TREE;
3675 declarator = c_parser_declarator (parser,
3676 specs->typespec_kind != ctsk_none,
3677 C_DTR_PARM, &dummy);
3678 if (declarator == NULL)
3680 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3681 return NULL;
3683 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3684 postfix_attrs = c_parser_attributes (parser);
3685 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3686 declarator);
3689 /* Parse a string literal in an asm expression. It should not be
3690 translated, and wide string literals are an error although
3691 permitted by the syntax. This is a GNU extension.
3693 asm-string-literal:
3694 string-literal
3696 ??? At present, following the old parser, the caller needs to have
3697 set lex_untranslated_string to 1. It would be better to follow the
3698 C++ parser rather than using this kludge. */
3700 static tree
3701 c_parser_asm_string_literal (c_parser *parser)
3703 tree str;
3704 int save_flag = warn_overlength_strings;
3705 warn_overlength_strings = 0;
3706 if (c_parser_next_token_is (parser, CPP_STRING))
3708 str = c_parser_peek_token (parser)->value;
3709 c_parser_consume_token (parser);
3711 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3713 error_at (c_parser_peek_token (parser)->location,
3714 "wide string literal in %<asm%>");
3715 str = build_string (1, "");
3716 c_parser_consume_token (parser);
3718 else
3720 c_parser_error (parser, "expected string literal");
3721 str = NULL_TREE;
3723 warn_overlength_strings = save_flag;
3724 return str;
3727 /* Parse a simple asm expression. This is used in restricted
3728 contexts, where a full expression with inputs and outputs does not
3729 make sense. This is a GNU extension.
3731 simple-asm-expr:
3732 asm ( asm-string-literal )
3735 static tree
3736 c_parser_simple_asm_expr (c_parser *parser)
3738 tree str;
3739 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3740 /* ??? Follow the C++ parser rather than using the
3741 lex_untranslated_string kludge. */
3742 parser->lex_untranslated_string = true;
3743 c_parser_consume_token (parser);
3744 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3746 parser->lex_untranslated_string = false;
3747 return NULL_TREE;
3749 str = c_parser_asm_string_literal (parser);
3750 parser->lex_untranslated_string = false;
3751 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3754 return NULL_TREE;
3756 return str;
3759 static tree
3760 c_parser_attribute_any_word (c_parser *parser)
3762 tree attr_name = NULL_TREE;
3764 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3766 /* ??? See comment above about what keywords are accepted here. */
3767 bool ok;
3768 switch (c_parser_peek_token (parser)->keyword)
3770 case RID_STATIC:
3771 case RID_UNSIGNED:
3772 case RID_LONG:
3773 case RID_CONST:
3774 case RID_EXTERN:
3775 case RID_REGISTER:
3776 case RID_TYPEDEF:
3777 case RID_SHORT:
3778 case RID_INLINE:
3779 case RID_NORETURN:
3780 case RID_VOLATILE:
3781 case RID_SIGNED:
3782 case RID_AUTO:
3783 case RID_RESTRICT:
3784 case RID_COMPLEX:
3785 case RID_THREAD:
3786 case RID_INT:
3787 case RID_CHAR:
3788 case RID_FLOAT:
3789 case RID_DOUBLE:
3790 case RID_VOID:
3791 case RID_DFLOAT32:
3792 case RID_DFLOAT64:
3793 case RID_DFLOAT128:
3794 case RID_BOOL:
3795 case RID_FRACT:
3796 case RID_ACCUM:
3797 case RID_SAT:
3798 case RID_TRANSACTION_ATOMIC:
3799 case RID_TRANSACTION_CANCEL:
3800 case RID_ATOMIC:
3801 case RID_AUTO_TYPE:
3802 case RID_INT_N_0:
3803 case RID_INT_N_1:
3804 case RID_INT_N_2:
3805 case RID_INT_N_3:
3806 ok = true;
3807 break;
3808 default:
3809 ok = false;
3810 break;
3812 if (!ok)
3813 return NULL_TREE;
3815 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3816 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3818 else if (c_parser_next_token_is (parser, CPP_NAME))
3819 attr_name = c_parser_peek_token (parser)->value;
3821 return attr_name;
3824 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
3825 "__vector" or "__vector__." */
3827 static inline bool
3828 is_cilkplus_vector_p (tree name)
3830 if (flag_cilkplus && is_attribute_p ("vector", name))
3831 return true;
3832 return false;
3835 #define CILK_SIMD_FN_CLAUSE_MASK \
3836 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3837 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3838 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3839 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3840 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3842 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3843 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3844 pushed into the token list.
3845 Syntax:
3846 vector
3847 vector (<vector attributes>). */
3849 static void
3850 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3852 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3854 int paren_scope = 0;
3855 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3856 /* Consume the "vector" token. */
3857 c_parser_consume_token (parser);
3859 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3861 c_parser_consume_token (parser);
3862 paren_scope++;
3864 while (paren_scope > 0)
3866 c_token *token = c_parser_peek_token (parser);
3867 if (token->type == CPP_OPEN_PAREN)
3868 paren_scope++;
3869 else if (token->type == CPP_CLOSE_PAREN)
3870 paren_scope--;
3871 /* Do not push the last ')' since we are not pushing the '('. */
3872 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3873 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3874 c_parser_consume_token (parser);
3877 /* Since we are converting an attribute to a pragma, we need to end the
3878 attribute with PRAGMA_EOL. */
3879 c_token eol_token;
3880 memset (&eol_token, 0, sizeof (eol_token));
3881 eol_token.type = CPP_PRAGMA_EOL;
3882 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3885 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3887 static void
3888 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3890 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3892 /* c_parser_attributes is called in several places, so if these EOF
3893 tokens are already inserted, then don't do them again. */
3894 if (last_token.type == CPP_EOF)
3895 return;
3897 /* Two CPP_EOF token are added as a safety net since the normal C
3898 front-end has two token look-ahead. */
3899 c_token eof_token;
3900 eof_token.type = CPP_EOF;
3901 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3902 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3905 /* Parse (possibly empty) attributes. This is a GNU extension.
3907 attributes:
3908 empty
3909 attributes attribute
3911 attribute:
3912 __attribute__ ( ( attribute-list ) )
3914 attribute-list:
3915 attrib
3916 attribute_list , attrib
3918 attrib:
3919 empty
3920 any-word
3921 any-word ( identifier )
3922 any-word ( identifier , nonempty-expr-list )
3923 any-word ( expr-list )
3925 where the "identifier" must not be declared as a type, and
3926 "any-word" may be any identifier (including one declared as a
3927 type), a reserved word storage class specifier, type specifier or
3928 type qualifier. ??? This still leaves out most reserved keywords
3929 (following the old parser), shouldn't we include them, and why not
3930 allow identifiers declared as types to start the arguments? */
3932 static tree
3933 c_parser_attributes (c_parser *parser)
3935 tree attrs = NULL_TREE;
3936 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3938 /* ??? Follow the C++ parser rather than using the
3939 lex_untranslated_string kludge. */
3940 parser->lex_untranslated_string = true;
3941 c_parser_consume_token (parser);
3942 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3944 parser->lex_untranslated_string = false;
3945 return attrs;
3947 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3949 parser->lex_untranslated_string = false;
3950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3951 return attrs;
3953 /* Parse the attribute list. */
3954 while (c_parser_next_token_is (parser, CPP_COMMA)
3955 || c_parser_next_token_is (parser, CPP_NAME)
3956 || c_parser_next_token_is (parser, CPP_KEYWORD))
3958 tree attr, attr_name, attr_args;
3959 vec<tree, va_gc> *expr_list;
3960 if (c_parser_next_token_is (parser, CPP_COMMA))
3962 c_parser_consume_token (parser);
3963 continue;
3966 attr_name = c_parser_attribute_any_word (parser);
3967 if (attr_name == NULL)
3968 break;
3969 if (is_cilkplus_vector_p (attr_name))
3971 c_token *v_token = c_parser_peek_token (parser);
3972 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
3973 continue;
3975 c_parser_consume_token (parser);
3976 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
3978 attr = build_tree_list (attr_name, NULL_TREE);
3979 attrs = chainon (attrs, attr);
3980 continue;
3982 c_parser_consume_token (parser);
3983 /* Parse the attribute contents. If they start with an
3984 identifier which is followed by a comma or close
3985 parenthesis, then the arguments start with that
3986 identifier; otherwise they are an expression list.
3987 In objective-c the identifier may be a classname. */
3988 if (c_parser_next_token_is (parser, CPP_NAME)
3989 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
3990 || (c_dialect_objc ()
3991 && c_parser_peek_token (parser)->id_kind
3992 == C_ID_CLASSNAME))
3993 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
3994 || (c_parser_peek_2nd_token (parser)->type
3995 == CPP_CLOSE_PAREN))
3996 && (attribute_takes_identifier_p (attr_name)
3997 || (c_dialect_objc ()
3998 && c_parser_peek_token (parser)->id_kind
3999 == C_ID_CLASSNAME)))
4001 tree arg1 = c_parser_peek_token (parser)->value;
4002 c_parser_consume_token (parser);
4003 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4004 attr_args = build_tree_list (NULL_TREE, arg1);
4005 else
4007 tree tree_list;
4008 c_parser_consume_token (parser);
4009 expr_list = c_parser_expr_list (parser, false, true,
4010 NULL, NULL, NULL, NULL);
4011 tree_list = build_tree_list_vec (expr_list);
4012 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4013 release_tree_vector (expr_list);
4016 else
4018 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4019 attr_args = NULL_TREE;
4020 else
4022 expr_list = c_parser_expr_list (parser, false, true,
4023 NULL, NULL, NULL, NULL);
4024 attr_args = build_tree_list_vec (expr_list);
4025 release_tree_vector (expr_list);
4028 attr = build_tree_list (attr_name, attr_args);
4029 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4030 c_parser_consume_token (parser);
4031 else
4033 parser->lex_untranslated_string = false;
4034 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4035 "expected %<)%>");
4036 return attrs;
4038 attrs = chainon (attrs, attr);
4040 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4041 c_parser_consume_token (parser);
4042 else
4044 parser->lex_untranslated_string = false;
4045 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4046 "expected %<)%>");
4047 return attrs;
4049 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4050 c_parser_consume_token (parser);
4051 else
4053 parser->lex_untranslated_string = false;
4054 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4055 "expected %<)%>");
4056 return attrs;
4058 parser->lex_untranslated_string = false;
4061 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4062 c_finish_cilk_simd_fn_tokens (parser);
4063 return attrs;
4066 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4068 type-name:
4069 specifier-qualifier-list abstract-declarator[opt]
4072 static struct c_type_name *
4073 c_parser_type_name (c_parser *parser)
4075 struct c_declspecs *specs = build_null_declspecs ();
4076 struct c_declarator *declarator;
4077 struct c_type_name *ret;
4078 bool dummy = false;
4079 c_parser_declspecs (parser, specs, false, true, true, false, false,
4080 cla_prefer_type);
4081 if (!specs->declspecs_seen_p)
4083 c_parser_error (parser, "expected specifier-qualifier-list");
4084 return NULL;
4086 if (specs->type != error_mark_node)
4088 pending_xref_error ();
4089 finish_declspecs (specs);
4091 declarator = c_parser_declarator (parser,
4092 specs->typespec_kind != ctsk_none,
4093 C_DTR_ABSTRACT, &dummy);
4094 if (declarator == NULL)
4095 return NULL;
4096 ret = XOBNEW (&parser_obstack, struct c_type_name);
4097 ret->specs = specs;
4098 ret->declarator = declarator;
4099 return ret;
4102 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4104 initializer:
4105 assignment-expression
4106 { initializer-list }
4107 { initializer-list , }
4109 initializer-list:
4110 designation[opt] initializer
4111 initializer-list , designation[opt] initializer
4113 designation:
4114 designator-list =
4116 designator-list:
4117 designator
4118 designator-list designator
4120 designator:
4121 array-designator
4122 . identifier
4124 array-designator:
4125 [ constant-expression ]
4127 GNU extensions:
4129 initializer:
4132 designation:
4133 array-designator
4134 identifier :
4136 array-designator:
4137 [ constant-expression ... constant-expression ]
4139 Any expression without commas is accepted in the syntax for the
4140 constant-expressions, with non-constant expressions rejected later.
4142 This function is only used for top-level initializers; for nested
4143 ones, see c_parser_initval. */
4145 static struct c_expr
4146 c_parser_initializer (c_parser *parser)
4148 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4149 return c_parser_braced_init (parser, NULL_TREE, false);
4150 else
4152 struct c_expr ret;
4153 location_t loc = c_parser_peek_token (parser)->location;
4154 ret = c_parser_expr_no_commas (parser, NULL);
4155 if (TREE_CODE (ret.value) != STRING_CST
4156 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4157 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4158 return ret;
4162 /* Parse a braced initializer list. TYPE is the type specified for a
4163 compound literal, and NULL_TREE for other initializers and for
4164 nested braced lists. NESTED_P is true for nested braced lists,
4165 false for the list of a compound literal or the list that is the
4166 top-level initializer in a declaration. */
4168 static struct c_expr
4169 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4171 struct c_expr ret;
4172 struct obstack braced_init_obstack;
4173 location_t brace_loc = c_parser_peek_token (parser)->location;
4174 gcc_obstack_init (&braced_init_obstack);
4175 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4176 c_parser_consume_token (parser);
4177 if (nested_p)
4178 push_init_level (brace_loc, 0, &braced_init_obstack);
4179 else
4180 really_start_incremental_init (type);
4181 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4183 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4185 else
4187 /* Parse a non-empty initializer list, possibly with a trailing
4188 comma. */
4189 while (true)
4191 c_parser_initelt (parser, &braced_init_obstack);
4192 if (parser->error)
4193 break;
4194 if (c_parser_next_token_is (parser, CPP_COMMA))
4195 c_parser_consume_token (parser);
4196 else
4197 break;
4198 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4199 break;
4202 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4204 ret.value = error_mark_node;
4205 ret.original_code = ERROR_MARK;
4206 ret.original_type = NULL;
4207 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4208 pop_init_level (brace_loc, 0, &braced_init_obstack);
4209 obstack_free (&braced_init_obstack, NULL);
4210 return ret;
4212 c_parser_consume_token (parser);
4213 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4214 obstack_free (&braced_init_obstack, NULL);
4215 return ret;
4218 /* Parse a nested initializer, including designators. */
4220 static void
4221 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4223 /* Parse any designator or designator list. A single array
4224 designator may have the subsequent "=" omitted in GNU C, but a
4225 longer list or a structure member designator may not. */
4226 if (c_parser_next_token_is (parser, CPP_NAME)
4227 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4229 /* Old-style structure member designator. */
4230 set_init_label (c_parser_peek_token (parser)->location,
4231 c_parser_peek_token (parser)->value,
4232 braced_init_obstack);
4233 /* Use the colon as the error location. */
4234 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4235 "obsolete use of designated initializer with %<:%>");
4236 c_parser_consume_token (parser);
4237 c_parser_consume_token (parser);
4239 else
4241 /* des_seen is 0 if there have been no designators, 1 if there
4242 has been a single array designator and 2 otherwise. */
4243 int des_seen = 0;
4244 /* Location of a designator. */
4245 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4246 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4247 || c_parser_next_token_is (parser, CPP_DOT))
4249 int des_prev = des_seen;
4250 if (!des_seen)
4251 des_loc = c_parser_peek_token (parser)->location;
4252 if (des_seen < 2)
4253 des_seen++;
4254 if (c_parser_next_token_is (parser, CPP_DOT))
4256 des_seen = 2;
4257 c_parser_consume_token (parser);
4258 if (c_parser_next_token_is (parser, CPP_NAME))
4260 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4261 braced_init_obstack);
4262 c_parser_consume_token (parser);
4264 else
4266 struct c_expr init;
4267 init.value = error_mark_node;
4268 init.original_code = ERROR_MARK;
4269 init.original_type = NULL;
4270 c_parser_error (parser, "expected identifier");
4271 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4272 process_init_element (input_location, init, false,
4273 braced_init_obstack);
4274 return;
4277 else
4279 tree first, second;
4280 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4281 location_t array_index_loc = UNKNOWN_LOCATION;
4282 /* ??? Following the old parser, [ objc-receiver
4283 objc-message-args ] is accepted as an initializer,
4284 being distinguished from a designator by what follows
4285 the first assignment expression inside the square
4286 brackets, but after a first array designator a
4287 subsequent square bracket is for Objective-C taken to
4288 start an expression, using the obsolete form of
4289 designated initializer without '=', rather than
4290 possibly being a second level of designation: in LALR
4291 terms, the '[' is shifted rather than reducing
4292 designator to designator-list. */
4293 if (des_prev == 1 && c_dialect_objc ())
4295 des_seen = des_prev;
4296 break;
4298 if (des_prev == 0 && c_dialect_objc ())
4300 /* This might be an array designator or an
4301 Objective-C message expression. If the former,
4302 continue parsing here; if the latter, parse the
4303 remainder of the initializer given the starting
4304 primary-expression. ??? It might make sense to
4305 distinguish when des_prev == 1 as well; see
4306 previous comment. */
4307 tree rec, args;
4308 struct c_expr mexpr;
4309 c_parser_consume_token (parser);
4310 if (c_parser_peek_token (parser)->type == CPP_NAME
4311 && ((c_parser_peek_token (parser)->id_kind
4312 == C_ID_TYPENAME)
4313 || (c_parser_peek_token (parser)->id_kind
4314 == C_ID_CLASSNAME)))
4316 /* Type name receiver. */
4317 tree id = c_parser_peek_token (parser)->value;
4318 c_parser_consume_token (parser);
4319 rec = objc_get_class_reference (id);
4320 goto parse_message_args;
4322 first = c_parser_expr_no_commas (parser, NULL).value;
4323 mark_exp_read (first);
4324 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4325 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4326 goto array_desig_after_first;
4327 /* Expression receiver. So far only one part
4328 without commas has been parsed; there might be
4329 more of the expression. */
4330 rec = first;
4331 while (c_parser_next_token_is (parser, CPP_COMMA))
4333 struct c_expr next;
4334 location_t comma_loc, exp_loc;
4335 comma_loc = c_parser_peek_token (parser)->location;
4336 c_parser_consume_token (parser);
4337 exp_loc = c_parser_peek_token (parser)->location;
4338 next = c_parser_expr_no_commas (parser, NULL);
4339 next = convert_lvalue_to_rvalue (exp_loc, next,
4340 true, true);
4341 rec = build_compound_expr (comma_loc, rec, next.value);
4343 parse_message_args:
4344 /* Now parse the objc-message-args. */
4345 args = c_parser_objc_message_args (parser);
4346 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4347 "expected %<]%>");
4348 mexpr.value
4349 = objc_build_message_expr (rec, args);
4350 mexpr.original_code = ERROR_MARK;
4351 mexpr.original_type = NULL;
4352 /* Now parse and process the remainder of the
4353 initializer, starting with this message
4354 expression as a primary-expression. */
4355 c_parser_initval (parser, &mexpr, braced_init_obstack);
4356 return;
4358 c_parser_consume_token (parser);
4359 array_index_loc = c_parser_peek_token (parser)->location;
4360 first = c_parser_expr_no_commas (parser, NULL).value;
4361 mark_exp_read (first);
4362 array_desig_after_first:
4363 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4365 ellipsis_loc = c_parser_peek_token (parser)->location;
4366 c_parser_consume_token (parser);
4367 second = c_parser_expr_no_commas (parser, NULL).value;
4368 mark_exp_read (second);
4370 else
4371 second = NULL_TREE;
4372 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4374 c_parser_consume_token (parser);
4375 set_init_index (array_index_loc, first, second,
4376 braced_init_obstack);
4377 if (second)
4378 pedwarn (ellipsis_loc, OPT_Wpedantic,
4379 "ISO C forbids specifying range of elements to initialize");
4381 else
4382 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4383 "expected %<]%>");
4386 if (des_seen >= 1)
4388 if (c_parser_next_token_is (parser, CPP_EQ))
4390 pedwarn_c90 (des_loc, OPT_Wpedantic,
4391 "ISO C90 forbids specifying subobject "
4392 "to initialize");
4393 c_parser_consume_token (parser);
4395 else
4397 if (des_seen == 1)
4398 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4399 "obsolete use of designated initializer without %<=%>");
4400 else
4402 struct c_expr init;
4403 init.value = error_mark_node;
4404 init.original_code = ERROR_MARK;
4405 init.original_type = NULL;
4406 c_parser_error (parser, "expected %<=%>");
4407 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4408 process_init_element (input_location, init, false,
4409 braced_init_obstack);
4410 return;
4415 c_parser_initval (parser, NULL, braced_init_obstack);
4418 /* Parse a nested initializer; as c_parser_initializer but parses
4419 initializers within braced lists, after any designators have been
4420 applied. If AFTER is not NULL then it is an Objective-C message
4421 expression which is the primary-expression starting the
4422 initializer. */
4424 static void
4425 c_parser_initval (c_parser *parser, struct c_expr *after,
4426 struct obstack * braced_init_obstack)
4428 struct c_expr init;
4429 gcc_assert (!after || c_dialect_objc ());
4430 location_t loc = c_parser_peek_token (parser)->location;
4432 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4433 init = c_parser_braced_init (parser, NULL_TREE, true);
4434 else
4436 init = c_parser_expr_no_commas (parser, after);
4437 if (init.value != NULL_TREE
4438 && TREE_CODE (init.value) != STRING_CST
4439 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4440 init = convert_lvalue_to_rvalue (loc, init, true, true);
4442 process_init_element (loc, init, false, braced_init_obstack);
4445 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4446 C99 6.8.2).
4448 compound-statement:
4449 { block-item-list[opt] }
4450 { label-declarations block-item-list }
4452 block-item-list:
4453 block-item
4454 block-item-list block-item
4456 block-item:
4457 nested-declaration
4458 statement
4460 nested-declaration:
4461 declaration
4463 GNU extensions:
4465 compound-statement:
4466 { label-declarations block-item-list }
4468 nested-declaration:
4469 __extension__ nested-declaration
4470 nested-function-definition
4472 label-declarations:
4473 label-declaration
4474 label-declarations label-declaration
4476 label-declaration:
4477 __label__ identifier-list ;
4479 Allowing the mixing of declarations and code is new in C99. The
4480 GNU syntax also permits (not shown above) labels at the end of
4481 compound statements, which yield an error. We don't allow labels
4482 on declarations; this might seem like a natural extension, but
4483 there would be a conflict between attributes on the label and
4484 prefix attributes on the declaration. ??? The syntax follows the
4485 old parser in requiring something after label declarations.
4486 Although they are erroneous if the labels declared aren't defined,
4487 is it useful for the syntax to be this way?
4489 OpenACC:
4491 block-item:
4492 openacc-directive
4494 openacc-directive:
4495 update-directive
4497 OpenMP:
4499 block-item:
4500 openmp-directive
4502 openmp-directive:
4503 barrier-directive
4504 flush-directive
4505 taskwait-directive
4506 taskyield-directive
4507 cancel-directive
4508 cancellation-point-directive */
4510 static tree
4511 c_parser_compound_statement (c_parser *parser)
4513 tree stmt;
4514 location_t brace_loc;
4515 brace_loc = c_parser_peek_token (parser)->location;
4516 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4518 /* Ensure a scope is entered and left anyway to avoid confusion
4519 if we have just prepared to enter a function body. */
4520 stmt = c_begin_compound_stmt (true);
4521 c_end_compound_stmt (brace_loc, stmt, true);
4522 return error_mark_node;
4524 stmt = c_begin_compound_stmt (true);
4525 c_parser_compound_statement_nostart (parser);
4527 /* If the compound stmt contains array notations, then we expand them. */
4528 if (flag_cilkplus && contains_array_notation_expr (stmt))
4529 stmt = expand_array_notation_exprs (stmt);
4530 return c_end_compound_stmt (brace_loc, stmt, true);
4533 /* Parse a compound statement except for the opening brace. This is
4534 used for parsing both compound statements and statement expressions
4535 (which follow different paths to handling the opening). */
4537 static void
4538 c_parser_compound_statement_nostart (c_parser *parser)
4540 bool last_stmt = false;
4541 bool last_label = false;
4542 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4543 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4544 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4546 c_parser_consume_token (parser);
4547 return;
4549 mark_valid_location_for_stdc_pragma (true);
4550 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4552 /* Read zero or more forward-declarations for labels that nested
4553 functions can jump to. */
4554 mark_valid_location_for_stdc_pragma (false);
4555 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4557 label_loc = c_parser_peek_token (parser)->location;
4558 c_parser_consume_token (parser);
4559 /* Any identifiers, including those declared as type names,
4560 are OK here. */
4561 while (true)
4563 tree label;
4564 if (c_parser_next_token_is_not (parser, CPP_NAME))
4566 c_parser_error (parser, "expected identifier");
4567 break;
4569 label
4570 = declare_label (c_parser_peek_token (parser)->value);
4571 C_DECLARED_LABEL_FLAG (label) = 1;
4572 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4573 c_parser_consume_token (parser);
4574 if (c_parser_next_token_is (parser, CPP_COMMA))
4575 c_parser_consume_token (parser);
4576 else
4577 break;
4579 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4581 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4583 /* We must now have at least one statement, label or declaration. */
4584 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4586 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4587 c_parser_error (parser, "expected declaration or statement");
4588 c_parser_consume_token (parser);
4589 return;
4591 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4593 location_t loc = c_parser_peek_token (parser)->location;
4594 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4595 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4596 || (c_parser_next_token_is (parser, CPP_NAME)
4597 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4599 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4600 label_loc = c_parser_peek_2nd_token (parser)->location;
4601 else
4602 label_loc = c_parser_peek_token (parser)->location;
4603 last_label = true;
4604 last_stmt = false;
4605 mark_valid_location_for_stdc_pragma (false);
4606 c_parser_label (parser);
4608 else if (!last_label
4609 && c_parser_next_tokens_start_declaration (parser))
4611 last_label = false;
4612 mark_valid_location_for_stdc_pragma (false);
4613 c_parser_declaration_or_fndef (parser, true, true, true, true,
4614 true, NULL, vNULL);
4615 if (last_stmt)
4616 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4617 "ISO C90 forbids mixed declarations and code");
4618 last_stmt = false;
4620 else if (!last_label
4621 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4623 /* __extension__ can start a declaration, but is also an
4624 unary operator that can start an expression. Consume all
4625 but the last of a possible series of __extension__ to
4626 determine which. */
4627 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4628 && (c_parser_peek_2nd_token (parser)->keyword
4629 == RID_EXTENSION))
4630 c_parser_consume_token (parser);
4631 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4633 int ext;
4634 ext = disable_extension_diagnostics ();
4635 c_parser_consume_token (parser);
4636 last_label = false;
4637 mark_valid_location_for_stdc_pragma (false);
4638 c_parser_declaration_or_fndef (parser, true, true, true, true,
4639 true, NULL, vNULL);
4640 /* Following the old parser, __extension__ does not
4641 disable this diagnostic. */
4642 restore_extension_diagnostics (ext);
4643 if (last_stmt)
4644 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4645 "ISO C90 forbids mixed declarations and code");
4646 last_stmt = false;
4648 else
4649 goto statement;
4651 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4653 /* External pragmas, and some omp pragmas, are not associated
4654 with regular c code, and so are not to be considered statements
4655 syntactically. This ensures that the user doesn't put them
4656 places that would turn into syntax errors if the directive
4657 were ignored. */
4658 if (c_parser_pragma (parser, pragma_compound))
4659 last_label = false, last_stmt = true;
4661 else if (c_parser_next_token_is (parser, CPP_EOF))
4663 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4664 c_parser_error (parser, "expected declaration or statement");
4665 return;
4667 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4669 if (parser->in_if_block)
4671 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4672 error_at (loc, """expected %<}%> before %<else%>");
4673 return;
4675 else
4677 error_at (loc, "%<else%> without a previous %<if%>");
4678 c_parser_consume_token (parser);
4679 continue;
4682 else
4684 statement:
4685 last_label = false;
4686 last_stmt = true;
4687 mark_valid_location_for_stdc_pragma (false);
4688 c_parser_statement_after_labels (parser);
4691 parser->error = false;
4693 if (last_label)
4694 error_at (label_loc, "label at end of compound statement");
4695 c_parser_consume_token (parser);
4696 /* Restore the value we started with. */
4697 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4700 /* Parse all consecutive labels. */
4702 static void
4703 c_parser_all_labels (c_parser *parser)
4705 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4706 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4707 || (c_parser_next_token_is (parser, CPP_NAME)
4708 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4709 c_parser_label (parser);
4712 /* Parse a label (C90 6.6.1, C99 6.8.1).
4714 label:
4715 identifier : attributes[opt]
4716 case constant-expression :
4717 default :
4719 GNU extensions:
4721 label:
4722 case constant-expression ... constant-expression :
4724 The use of attributes on labels is a GNU extension. The syntax in
4725 GNU C accepts any expressions without commas, non-constant
4726 expressions being rejected later. */
4728 static void
4729 c_parser_label (c_parser *parser)
4731 location_t loc1 = c_parser_peek_token (parser)->location;
4732 tree label = NULL_TREE;
4733 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4735 tree exp1, exp2;
4736 c_parser_consume_token (parser);
4737 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4738 if (c_parser_next_token_is (parser, CPP_COLON))
4740 c_parser_consume_token (parser);
4741 label = do_case (loc1, exp1, NULL_TREE);
4743 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4745 c_parser_consume_token (parser);
4746 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4747 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4748 label = do_case (loc1, exp1, exp2);
4750 else
4751 c_parser_error (parser, "expected %<:%> or %<...%>");
4753 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4755 c_parser_consume_token (parser);
4756 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4757 label = do_case (loc1, NULL_TREE, NULL_TREE);
4759 else
4761 tree name = c_parser_peek_token (parser)->value;
4762 tree tlab;
4763 tree attrs;
4764 location_t loc2 = c_parser_peek_token (parser)->location;
4765 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4766 c_parser_consume_token (parser);
4767 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4768 c_parser_consume_token (parser);
4769 attrs = c_parser_attributes (parser);
4770 tlab = define_label (loc2, name);
4771 if (tlab)
4773 decl_attributes (&tlab, attrs, 0);
4774 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4777 if (label)
4779 if (c_parser_next_tokens_start_declaration (parser))
4781 error_at (c_parser_peek_token (parser)->location,
4782 "a label can only be part of a statement and "
4783 "a declaration is not a statement");
4784 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4785 /*static_assert_ok*/ true,
4786 /*empty_ok*/ true, /*nested*/ true,
4787 /*start_attr_ok*/ true, NULL,
4788 vNULL);
4793 /* Parse a statement (C90 6.6, C99 6.8).
4795 statement:
4796 labeled-statement
4797 compound-statement
4798 expression-statement
4799 selection-statement
4800 iteration-statement
4801 jump-statement
4803 labeled-statement:
4804 label statement
4806 expression-statement:
4807 expression[opt] ;
4809 selection-statement:
4810 if-statement
4811 switch-statement
4813 iteration-statement:
4814 while-statement
4815 do-statement
4816 for-statement
4818 jump-statement:
4819 goto identifier ;
4820 continue ;
4821 break ;
4822 return expression[opt] ;
4824 GNU extensions:
4826 statement:
4827 asm-statement
4829 jump-statement:
4830 goto * expression ;
4832 Objective-C:
4834 statement:
4835 objc-throw-statement
4836 objc-try-catch-statement
4837 objc-synchronized-statement
4839 objc-throw-statement:
4840 @throw expression ;
4841 @throw ;
4843 OpenACC:
4845 statement:
4846 openacc-construct
4848 openacc-construct:
4849 parallel-construct
4850 kernels-construct
4851 data-construct
4852 loop-construct
4854 parallel-construct:
4855 parallel-directive structured-block
4857 kernels-construct:
4858 kernels-directive structured-block
4860 data-construct:
4861 data-directive structured-block
4863 loop-construct:
4864 loop-directive structured-block
4866 OpenMP:
4868 statement:
4869 openmp-construct
4871 openmp-construct:
4872 parallel-construct
4873 for-construct
4874 simd-construct
4875 for-simd-construct
4876 sections-construct
4877 single-construct
4878 parallel-for-construct
4879 parallel-for-simd-construct
4880 parallel-sections-construct
4881 master-construct
4882 critical-construct
4883 atomic-construct
4884 ordered-construct
4886 parallel-construct:
4887 parallel-directive structured-block
4889 for-construct:
4890 for-directive iteration-statement
4892 simd-construct:
4893 simd-directive iteration-statements
4895 for-simd-construct:
4896 for-simd-directive iteration-statements
4898 sections-construct:
4899 sections-directive section-scope
4901 single-construct:
4902 single-directive structured-block
4904 parallel-for-construct:
4905 parallel-for-directive iteration-statement
4907 parallel-for-simd-construct:
4908 parallel-for-simd-directive iteration-statement
4910 parallel-sections-construct:
4911 parallel-sections-directive section-scope
4913 master-construct:
4914 master-directive structured-block
4916 critical-construct:
4917 critical-directive structured-block
4919 atomic-construct:
4920 atomic-directive expression-statement
4922 ordered-construct:
4923 ordered-directive structured-block
4925 Transactional Memory:
4927 statement:
4928 transaction-statement
4929 transaction-cancel-statement
4932 static void
4933 c_parser_statement (c_parser *parser)
4935 c_parser_all_labels (parser);
4936 c_parser_statement_after_labels (parser);
4939 /* Parse a statement, other than a labeled statement. */
4941 static void
4942 c_parser_statement_after_labels (c_parser *parser)
4944 location_t loc = c_parser_peek_token (parser)->location;
4945 tree stmt = NULL_TREE;
4946 bool in_if_block = parser->in_if_block;
4947 parser->in_if_block = false;
4948 switch (c_parser_peek_token (parser)->type)
4950 case CPP_OPEN_BRACE:
4951 add_stmt (c_parser_compound_statement (parser));
4952 break;
4953 case CPP_KEYWORD:
4954 switch (c_parser_peek_token (parser)->keyword)
4956 case RID_IF:
4957 c_parser_if_statement (parser);
4958 break;
4959 case RID_SWITCH:
4960 c_parser_switch_statement (parser);
4961 break;
4962 case RID_WHILE:
4963 c_parser_while_statement (parser, false);
4964 break;
4965 case RID_DO:
4966 c_parser_do_statement (parser, false);
4967 break;
4968 case RID_FOR:
4969 c_parser_for_statement (parser, false);
4970 break;
4971 case RID_CILK_FOR:
4972 if (!flag_cilkplus)
4974 error_at (c_parser_peek_token (parser)->location,
4975 "-fcilkplus must be enabled to use %<_Cilk_for%>");
4976 c_parser_skip_to_end_of_block_or_statement (parser);
4978 else
4979 c_parser_cilk_for (parser, integer_zero_node);
4980 break;
4981 case RID_CILK_SYNC:
4982 c_parser_consume_token (parser);
4983 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4984 if (!flag_cilkplus)
4985 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
4986 else
4987 add_stmt (build_cilk_sync ());
4988 break;
4989 case RID_GOTO:
4990 c_parser_consume_token (parser);
4991 if (c_parser_next_token_is (parser, CPP_NAME))
4993 stmt = c_finish_goto_label (loc,
4994 c_parser_peek_token (parser)->value);
4995 c_parser_consume_token (parser);
4997 else if (c_parser_next_token_is (parser, CPP_MULT))
4999 struct c_expr val;
5001 c_parser_consume_token (parser);
5002 val = c_parser_expression (parser);
5003 val = convert_lvalue_to_rvalue (loc, val, false, true);
5004 stmt = c_finish_goto_ptr (loc, val.value);
5006 else
5007 c_parser_error (parser, "expected identifier or %<*%>");
5008 goto expect_semicolon;
5009 case RID_CONTINUE:
5010 c_parser_consume_token (parser);
5011 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5012 goto expect_semicolon;
5013 case RID_BREAK:
5014 c_parser_consume_token (parser);
5015 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5016 goto expect_semicolon;
5017 case RID_RETURN:
5018 c_parser_consume_token (parser);
5019 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5021 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5022 c_parser_consume_token (parser);
5024 else
5026 location_t xloc = c_parser_peek_token (parser)->location;
5027 struct c_expr expr = c_parser_expression_conv (parser);
5028 mark_exp_read (expr.value);
5029 stmt = c_finish_return (xloc, expr.value, expr.original_type);
5030 goto expect_semicolon;
5032 break;
5033 case RID_ASM:
5034 stmt = c_parser_asm_statement (parser);
5035 break;
5036 case RID_TRANSACTION_ATOMIC:
5037 case RID_TRANSACTION_RELAXED:
5038 stmt = c_parser_transaction (parser,
5039 c_parser_peek_token (parser)->keyword);
5040 break;
5041 case RID_TRANSACTION_CANCEL:
5042 stmt = c_parser_transaction_cancel (parser);
5043 goto expect_semicolon;
5044 case RID_AT_THROW:
5045 gcc_assert (c_dialect_objc ());
5046 c_parser_consume_token (parser);
5047 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5049 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5050 c_parser_consume_token (parser);
5052 else
5054 struct c_expr expr = c_parser_expression (parser);
5055 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5056 expr.value = c_fully_fold (expr.value, false, NULL);
5057 stmt = objc_build_throw_stmt (loc, expr.value);
5058 goto expect_semicolon;
5060 break;
5061 case RID_AT_TRY:
5062 gcc_assert (c_dialect_objc ());
5063 c_parser_objc_try_catch_finally_statement (parser);
5064 break;
5065 case RID_AT_SYNCHRONIZED:
5066 gcc_assert (c_dialect_objc ());
5067 c_parser_objc_synchronized_statement (parser);
5068 break;
5069 default:
5070 goto expr_stmt;
5072 break;
5073 case CPP_SEMICOLON:
5074 c_parser_consume_token (parser);
5075 break;
5076 case CPP_CLOSE_PAREN:
5077 case CPP_CLOSE_SQUARE:
5078 /* Avoid infinite loop in error recovery:
5079 c_parser_skip_until_found stops at a closing nesting
5080 delimiter without consuming it, but here we need to consume
5081 it to proceed further. */
5082 c_parser_error (parser, "expected statement");
5083 c_parser_consume_token (parser);
5084 break;
5085 case CPP_PRAGMA:
5086 c_parser_pragma (parser, pragma_stmt);
5087 break;
5088 default:
5089 expr_stmt:
5090 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5091 expect_semicolon:
5092 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5093 break;
5095 /* Two cases cannot and do not have line numbers associated: If stmt
5096 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5097 cannot hold line numbers. But that's OK because the statement
5098 will either be changed to a MODIFY_EXPR during gimplification of
5099 the statement expr, or discarded. If stmt was compound, but
5100 without new variables, we will have skipped the creation of a
5101 BIND and will have a bare STATEMENT_LIST. But that's OK because
5102 (recursively) all of the component statements should already have
5103 line numbers assigned. ??? Can we discard no-op statements
5104 earlier? */
5105 if (CAN_HAVE_LOCATION_P (stmt)
5106 && EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5107 SET_EXPR_LOCATION (stmt, loc);
5109 parser->in_if_block = in_if_block;
5112 /* Parse the condition from an if, do, while or for statements. */
5114 static tree
5115 c_parser_condition (c_parser *parser)
5117 location_t loc = c_parser_peek_token (parser)->location;
5118 tree cond;
5119 cond = c_parser_expression_conv (parser).value;
5120 cond = c_objc_common_truthvalue_conversion (loc, cond);
5121 cond = c_fully_fold (cond, false, NULL);
5122 if (warn_sequence_point)
5123 verify_sequence_points (cond);
5124 return cond;
5127 /* Parse a parenthesized condition from an if, do or while statement.
5129 condition:
5130 ( expression )
5132 static tree
5133 c_parser_paren_condition (c_parser *parser)
5135 tree cond;
5136 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5137 return error_mark_node;
5138 cond = c_parser_condition (parser);
5139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5140 return cond;
5143 /* Parse a statement which is a block in C99. */
5145 static tree
5146 c_parser_c99_block_statement (c_parser *parser)
5148 tree block = c_begin_compound_stmt (flag_isoc99);
5149 location_t loc = c_parser_peek_token (parser)->location;
5150 c_parser_statement (parser);
5151 return c_end_compound_stmt (loc, block, flag_isoc99);
5154 /* Parse the body of an if statement. This is just parsing a
5155 statement but (a) it is a block in C99, (b) we track whether the
5156 body is an if statement for the sake of -Wparentheses warnings, (c)
5157 we handle an empty body specially for the sake of -Wempty-body
5158 warnings, and (d) we call parser_compound_statement directly
5159 because c_parser_statement_after_labels resets
5160 parser->in_if_block. */
5162 static tree
5163 c_parser_if_body (c_parser *parser, bool *if_p)
5165 tree block = c_begin_compound_stmt (flag_isoc99);
5166 location_t body_loc = c_parser_peek_token (parser)->location;
5167 c_parser_all_labels (parser);
5168 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5169 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5171 location_t loc = c_parser_peek_token (parser)->location;
5172 add_stmt (build_empty_stmt (loc));
5173 c_parser_consume_token (parser);
5174 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5175 warning_at (loc, OPT_Wempty_body,
5176 "suggest braces around empty body in an %<if%> statement");
5178 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5179 add_stmt (c_parser_compound_statement (parser));
5180 else
5181 c_parser_statement_after_labels (parser);
5182 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5185 /* Parse the else body of an if statement. This is just parsing a
5186 statement but (a) it is a block in C99, (b) we handle an empty body
5187 specially for the sake of -Wempty-body warnings. */
5189 static tree
5190 c_parser_else_body (c_parser *parser)
5192 location_t else_loc = c_parser_peek_token (parser)->location;
5193 tree block = c_begin_compound_stmt (flag_isoc99);
5194 c_parser_all_labels (parser);
5195 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5197 location_t loc = c_parser_peek_token (parser)->location;
5198 warning_at (loc,
5199 OPT_Wempty_body,
5200 "suggest braces around empty body in an %<else%> statement");
5201 add_stmt (build_empty_stmt (loc));
5202 c_parser_consume_token (parser);
5204 else
5205 c_parser_statement_after_labels (parser);
5206 return c_end_compound_stmt (else_loc, block, flag_isoc99);
5209 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5211 if-statement:
5212 if ( expression ) statement
5213 if ( expression ) statement else statement
5216 static void
5217 c_parser_if_statement (c_parser *parser)
5219 tree block;
5220 location_t loc;
5221 tree cond;
5222 bool first_if = false;
5223 tree first_body, second_body;
5224 bool in_if_block;
5225 tree if_stmt;
5227 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5228 c_parser_consume_token (parser);
5229 block = c_begin_compound_stmt (flag_isoc99);
5230 loc = c_parser_peek_token (parser)->location;
5231 cond = c_parser_paren_condition (parser);
5232 in_if_block = parser->in_if_block;
5233 parser->in_if_block = true;
5234 first_body = c_parser_if_body (parser, &first_if);
5235 parser->in_if_block = in_if_block;
5236 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5238 c_parser_consume_token (parser);
5239 second_body = c_parser_else_body (parser);
5241 else
5242 second_body = NULL_TREE;
5243 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5244 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5246 /* If the if statement contains array notations, then we expand them. */
5247 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5248 if_stmt = fix_conditional_array_notations (if_stmt);
5249 add_stmt (if_stmt);
5252 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5254 switch-statement:
5255 switch (expression) statement
5258 static void
5259 c_parser_switch_statement (c_parser *parser)
5261 struct c_expr ce;
5262 tree block, expr, body, save_break;
5263 location_t switch_loc = c_parser_peek_token (parser)->location;
5264 location_t switch_cond_loc;
5265 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5266 c_parser_consume_token (parser);
5267 block = c_begin_compound_stmt (flag_isoc99);
5268 bool explicit_cast_p = false;
5269 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5271 switch_cond_loc = c_parser_peek_token (parser)->location;
5272 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5273 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5274 explicit_cast_p = true;
5275 ce = c_parser_expression (parser);
5276 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5277 expr = ce.value;
5278 if (flag_cilkplus && contains_array_notation_expr (expr))
5280 error_at (switch_cond_loc,
5281 "array notations cannot be used as a condition for switch "
5282 "statement");
5283 expr = error_mark_node;
5285 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5287 else
5289 switch_cond_loc = UNKNOWN_LOCATION;
5290 expr = error_mark_node;
5292 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5293 save_break = c_break_label;
5294 c_break_label = NULL_TREE;
5295 body = c_parser_c99_block_statement (parser);
5296 c_finish_case (body, ce.original_type);
5297 if (c_break_label)
5299 location_t here = c_parser_peek_token (parser)->location;
5300 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5301 SET_EXPR_LOCATION (t, here);
5302 add_stmt (t);
5304 c_break_label = save_break;
5305 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5308 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5310 while-statement:
5311 while (expression) statement
5314 static void
5315 c_parser_while_statement (c_parser *parser, bool ivdep)
5317 tree block, cond, body, save_break, save_cont;
5318 location_t loc;
5319 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5320 c_parser_consume_token (parser);
5321 block = c_begin_compound_stmt (flag_isoc99);
5322 loc = c_parser_peek_token (parser)->location;
5323 cond = c_parser_paren_condition (parser);
5324 if (flag_cilkplus && contains_array_notation_expr (cond))
5326 error_at (loc, "array notations cannot be used as a condition for while "
5327 "statement");
5328 cond = error_mark_node;
5331 if (ivdep && cond != error_mark_node)
5332 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5333 build_int_cst (integer_type_node,
5334 annot_expr_ivdep_kind));
5335 save_break = c_break_label;
5336 c_break_label = NULL_TREE;
5337 save_cont = c_cont_label;
5338 c_cont_label = NULL_TREE;
5339 body = c_parser_c99_block_statement (parser);
5340 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5341 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5342 c_break_label = save_break;
5343 c_cont_label = save_cont;
5346 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5348 do-statement:
5349 do statement while ( expression ) ;
5352 static void
5353 c_parser_do_statement (c_parser *parser, bool ivdep)
5355 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5356 location_t loc;
5357 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5358 c_parser_consume_token (parser);
5359 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5360 warning_at (c_parser_peek_token (parser)->location,
5361 OPT_Wempty_body,
5362 "suggest braces around empty body in %<do%> statement");
5363 block = c_begin_compound_stmt (flag_isoc99);
5364 loc = c_parser_peek_token (parser)->location;
5365 save_break = c_break_label;
5366 c_break_label = NULL_TREE;
5367 save_cont = c_cont_label;
5368 c_cont_label = NULL_TREE;
5369 body = c_parser_c99_block_statement (parser);
5370 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5371 new_break = c_break_label;
5372 c_break_label = save_break;
5373 new_cont = c_cont_label;
5374 c_cont_label = save_cont;
5375 cond = c_parser_paren_condition (parser);
5376 if (flag_cilkplus && contains_array_notation_expr (cond))
5378 error_at (loc, "array notations cannot be used as a condition for a "
5379 "do-while statement");
5380 cond = error_mark_node;
5382 if (ivdep && cond != error_mark_node)
5383 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5384 build_int_cst (integer_type_node,
5385 annot_expr_ivdep_kind));
5386 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5387 c_parser_skip_to_end_of_block_or_statement (parser);
5388 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5389 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5392 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5394 for-statement:
5395 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5396 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5398 The form with a declaration is new in C99.
5400 ??? In accordance with the old parser, the declaration may be a
5401 nested function, which is then rejected in check_for_loop_decls,
5402 but does it make any sense for this to be included in the grammar?
5403 Note in particular that the nested function does not include a
5404 trailing ';', whereas the "declaration" production includes one.
5405 Also, can we reject bad declarations earlier and cheaper than
5406 check_for_loop_decls?
5408 In Objective-C, there are two additional variants:
5410 foreach-statement:
5411 for ( expression in expresssion ) statement
5412 for ( declaration in expression ) statement
5414 This is inconsistent with C, because the second variant is allowed
5415 even if c99 is not enabled.
5417 The rest of the comment documents these Objective-C foreach-statement.
5419 Here is the canonical example of the first variant:
5420 for (object in array) { do something with object }
5421 we call the first expression ("object") the "object_expression" and
5422 the second expression ("array") the "collection_expression".
5423 object_expression must be an lvalue of type "id" (a generic Objective-C
5424 object) because the loop works by assigning to object_expression the
5425 various objects from the collection_expression. collection_expression
5426 must evaluate to something of type "id" which responds to the method
5427 countByEnumeratingWithState:objects:count:.
5429 The canonical example of the second variant is:
5430 for (id object in array) { do something with object }
5431 which is completely equivalent to
5433 id object;
5434 for (object in array) { do something with object }
5436 Note that initizializing 'object' in some way (eg, "for ((object =
5437 xxx) in array) { do something with object }") is possibly
5438 technically valid, but completely pointless as 'object' will be
5439 assigned to something else as soon as the loop starts. We should
5440 most likely reject it (TODO).
5442 The beginning of the Objective-C foreach-statement looks exactly
5443 like the beginning of the for-statement, and we can tell it is a
5444 foreach-statement only because the initial declaration or
5445 expression is terminated by 'in' instead of ';'.
5448 static void
5449 c_parser_for_statement (c_parser *parser, bool ivdep)
5451 tree block, cond, incr, save_break, save_cont, body;
5452 /* The following are only used when parsing an ObjC foreach statement. */
5453 tree object_expression;
5454 /* Silence the bogus uninitialized warning. */
5455 tree collection_expression = NULL;
5456 location_t loc = c_parser_peek_token (parser)->location;
5457 location_t for_loc = c_parser_peek_token (parser)->location;
5458 bool is_foreach_statement = false;
5459 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5460 c_parser_consume_token (parser);
5461 /* Open a compound statement in Objective-C as well, just in case this is
5462 as foreach expression. */
5463 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5464 cond = error_mark_node;
5465 incr = error_mark_node;
5466 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5468 /* Parse the initialization declaration or expression. */
5469 object_expression = error_mark_node;
5470 parser->objc_could_be_foreach_context = c_dialect_objc ();
5471 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5473 parser->objc_could_be_foreach_context = false;
5474 c_parser_consume_token (parser);
5475 c_finish_expr_stmt (loc, NULL_TREE);
5477 else if (c_parser_next_tokens_start_declaration (parser))
5479 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5480 &object_expression, vNULL);
5481 parser->objc_could_be_foreach_context = false;
5483 if (c_parser_next_token_is_keyword (parser, RID_IN))
5485 c_parser_consume_token (parser);
5486 is_foreach_statement = true;
5487 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5488 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5490 else
5491 check_for_loop_decls (for_loc, flag_isoc99);
5493 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5495 /* __extension__ can start a declaration, but is also an
5496 unary operator that can start an expression. Consume all
5497 but the last of a possible series of __extension__ to
5498 determine which. */
5499 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5500 && (c_parser_peek_2nd_token (parser)->keyword
5501 == RID_EXTENSION))
5502 c_parser_consume_token (parser);
5503 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5505 int ext;
5506 ext = disable_extension_diagnostics ();
5507 c_parser_consume_token (parser);
5508 c_parser_declaration_or_fndef (parser, true, true, true, true,
5509 true, &object_expression, vNULL);
5510 parser->objc_could_be_foreach_context = false;
5512 restore_extension_diagnostics (ext);
5513 if (c_parser_next_token_is_keyword (parser, RID_IN))
5515 c_parser_consume_token (parser);
5516 is_foreach_statement = true;
5517 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5518 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5520 else
5521 check_for_loop_decls (for_loc, flag_isoc99);
5523 else
5524 goto init_expr;
5526 else
5528 init_expr:
5530 struct c_expr ce;
5531 tree init_expression;
5532 ce = c_parser_expression (parser);
5533 init_expression = ce.value;
5534 parser->objc_could_be_foreach_context = false;
5535 if (c_parser_next_token_is_keyword (parser, RID_IN))
5537 c_parser_consume_token (parser);
5538 is_foreach_statement = true;
5539 if (! lvalue_p (init_expression))
5540 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5541 object_expression = c_fully_fold (init_expression, false, NULL);
5543 else
5545 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5546 init_expression = ce.value;
5547 c_finish_expr_stmt (loc, init_expression);
5548 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5552 /* Parse the loop condition. In the case of a foreach
5553 statement, there is no loop condition. */
5554 gcc_assert (!parser->objc_could_be_foreach_context);
5555 if (!is_foreach_statement)
5557 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5559 if (ivdep)
5561 c_parser_error (parser, "missing loop condition in loop with "
5562 "%<GCC ivdep%> pragma");
5563 cond = error_mark_node;
5565 else
5567 c_parser_consume_token (parser);
5568 cond = NULL_TREE;
5571 else
5573 cond = c_parser_condition (parser);
5574 if (flag_cilkplus && contains_array_notation_expr (cond))
5576 error_at (loc, "array notations cannot be used in a "
5577 "condition for a for-loop");
5578 cond = error_mark_node;
5580 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5581 "expected %<;%>");
5583 if (ivdep && cond != error_mark_node)
5584 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5585 build_int_cst (integer_type_node,
5586 annot_expr_ivdep_kind));
5588 /* Parse the increment expression (the third expression in a
5589 for-statement). In the case of a foreach-statement, this is
5590 the expression that follows the 'in'. */
5591 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5593 if (is_foreach_statement)
5595 c_parser_error (parser, "missing collection in fast enumeration");
5596 collection_expression = error_mark_node;
5598 else
5599 incr = c_process_expr_stmt (loc, NULL_TREE);
5601 else
5603 if (is_foreach_statement)
5604 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5605 false, NULL);
5606 else
5608 struct c_expr ce = c_parser_expression (parser);
5609 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5610 incr = c_process_expr_stmt (loc, ce.value);
5613 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5615 save_break = c_break_label;
5616 c_break_label = NULL_TREE;
5617 save_cont = c_cont_label;
5618 c_cont_label = NULL_TREE;
5619 body = c_parser_c99_block_statement (parser);
5620 if (is_foreach_statement)
5621 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5622 else
5623 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5624 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5625 c_break_label = save_break;
5626 c_cont_label = save_cont;
5629 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5630 statement with inputs, outputs, clobbers, and volatile tag
5631 allowed.
5633 asm-statement:
5634 asm type-qualifier[opt] ( asm-argument ) ;
5635 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5637 asm-argument:
5638 asm-string-literal
5639 asm-string-literal : asm-operands[opt]
5640 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5641 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5643 asm-goto-argument:
5644 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5645 : asm-goto-operands
5647 Qualifiers other than volatile are accepted in the syntax but
5648 warned for. */
5650 static tree
5651 c_parser_asm_statement (c_parser *parser)
5653 tree quals, str, outputs, inputs, clobbers, labels, ret;
5654 bool simple, is_goto;
5655 location_t asm_loc = c_parser_peek_token (parser)->location;
5656 int section, nsections;
5658 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5659 c_parser_consume_token (parser);
5660 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5662 quals = c_parser_peek_token (parser)->value;
5663 c_parser_consume_token (parser);
5665 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5666 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5668 warning_at (c_parser_peek_token (parser)->location,
5670 "%E qualifier ignored on asm",
5671 c_parser_peek_token (parser)->value);
5672 quals = NULL_TREE;
5673 c_parser_consume_token (parser);
5675 else
5676 quals = NULL_TREE;
5678 is_goto = false;
5679 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5681 c_parser_consume_token (parser);
5682 is_goto = true;
5685 /* ??? Follow the C++ parser rather than using the
5686 lex_untranslated_string kludge. */
5687 parser->lex_untranslated_string = true;
5688 ret = NULL;
5690 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5691 goto error;
5693 str = c_parser_asm_string_literal (parser);
5694 if (str == NULL_TREE)
5695 goto error_close_paren;
5697 simple = true;
5698 outputs = NULL_TREE;
5699 inputs = NULL_TREE;
5700 clobbers = NULL_TREE;
5701 labels = NULL_TREE;
5703 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5704 goto done_asm;
5706 /* Parse each colon-delimited section of operands. */
5707 nsections = 3 + is_goto;
5708 for (section = 0; section < nsections; ++section)
5710 if (!c_parser_require (parser, CPP_COLON,
5711 is_goto
5712 ? "expected %<:%>"
5713 : "expected %<:%> or %<)%>"))
5714 goto error_close_paren;
5716 /* Once past any colon, we're no longer a simple asm. */
5717 simple = false;
5719 if ((!c_parser_next_token_is (parser, CPP_COLON)
5720 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5721 || section == 3)
5722 switch (section)
5724 case 0:
5725 /* For asm goto, we don't allow output operands, but reserve
5726 the slot for a future extension that does allow them. */
5727 if (!is_goto)
5728 outputs = c_parser_asm_operands (parser);
5729 break;
5730 case 1:
5731 inputs = c_parser_asm_operands (parser);
5732 break;
5733 case 2:
5734 clobbers = c_parser_asm_clobbers (parser);
5735 break;
5736 case 3:
5737 labels = c_parser_asm_goto_operands (parser);
5738 break;
5739 default:
5740 gcc_unreachable ();
5743 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5744 goto done_asm;
5747 done_asm:
5748 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5751 goto error;
5754 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5755 c_parser_skip_to_end_of_block_or_statement (parser);
5757 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5758 clobbers, labels, simple));
5760 error:
5761 parser->lex_untranslated_string = false;
5762 return ret;
5764 error_close_paren:
5765 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5766 goto error;
5769 /* Parse asm operands, a GNU extension.
5771 asm-operands:
5772 asm-operand
5773 asm-operands , asm-operand
5775 asm-operand:
5776 asm-string-literal ( expression )
5777 [ identifier ] asm-string-literal ( expression )
5780 static tree
5781 c_parser_asm_operands (c_parser *parser)
5783 tree list = NULL_TREE;
5784 while (true)
5786 tree name, str;
5787 struct c_expr expr;
5788 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5790 c_parser_consume_token (parser);
5791 if (c_parser_next_token_is (parser, CPP_NAME))
5793 tree id = c_parser_peek_token (parser)->value;
5794 c_parser_consume_token (parser);
5795 name = build_string (IDENTIFIER_LENGTH (id),
5796 IDENTIFIER_POINTER (id));
5798 else
5800 c_parser_error (parser, "expected identifier");
5801 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5802 return NULL_TREE;
5804 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5805 "expected %<]%>");
5807 else
5808 name = NULL_TREE;
5809 str = c_parser_asm_string_literal (parser);
5810 if (str == NULL_TREE)
5811 return NULL_TREE;
5812 parser->lex_untranslated_string = false;
5813 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5815 parser->lex_untranslated_string = true;
5816 return NULL_TREE;
5818 expr = c_parser_expression (parser);
5819 mark_exp_read (expr.value);
5820 parser->lex_untranslated_string = true;
5821 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5823 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5824 return NULL_TREE;
5826 list = chainon (list, build_tree_list (build_tree_list (name, str),
5827 expr.value));
5828 if (c_parser_next_token_is (parser, CPP_COMMA))
5829 c_parser_consume_token (parser);
5830 else
5831 break;
5833 return list;
5836 /* Parse asm clobbers, a GNU extension.
5838 asm-clobbers:
5839 asm-string-literal
5840 asm-clobbers , asm-string-literal
5843 static tree
5844 c_parser_asm_clobbers (c_parser *parser)
5846 tree list = NULL_TREE;
5847 while (true)
5849 tree str = c_parser_asm_string_literal (parser);
5850 if (str)
5851 list = tree_cons (NULL_TREE, str, list);
5852 else
5853 return NULL_TREE;
5854 if (c_parser_next_token_is (parser, CPP_COMMA))
5855 c_parser_consume_token (parser);
5856 else
5857 break;
5859 return list;
5862 /* Parse asm goto labels, a GNU extension.
5864 asm-goto-operands:
5865 identifier
5866 asm-goto-operands , identifier
5869 static tree
5870 c_parser_asm_goto_operands (c_parser *parser)
5872 tree list = NULL_TREE;
5873 while (true)
5875 tree name, label;
5877 if (c_parser_next_token_is (parser, CPP_NAME))
5879 c_token *tok = c_parser_peek_token (parser);
5880 name = tok->value;
5881 label = lookup_label_for_goto (tok->location, name);
5882 c_parser_consume_token (parser);
5883 TREE_USED (label) = 1;
5885 else
5887 c_parser_error (parser, "expected identifier");
5888 return NULL_TREE;
5891 name = build_string (IDENTIFIER_LENGTH (name),
5892 IDENTIFIER_POINTER (name));
5893 list = tree_cons (name, label, list);
5894 if (c_parser_next_token_is (parser, CPP_COMMA))
5895 c_parser_consume_token (parser);
5896 else
5897 return nreverse (list);
5901 /* Parse an expression other than a compound expression; that is, an
5902 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
5903 NULL then it is an Objective-C message expression which is the
5904 primary-expression starting the expression as an initializer.
5906 assignment-expression:
5907 conditional-expression
5908 unary-expression assignment-operator assignment-expression
5910 assignment-operator: one of
5911 = *= /= %= += -= <<= >>= &= ^= |=
5913 In GNU C we accept any conditional expression on the LHS and
5914 diagnose the invalid lvalue rather than producing a syntax
5915 error. */
5917 static struct c_expr
5918 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
5919 tree omp_atomic_lhs)
5921 struct c_expr lhs, rhs, ret;
5922 enum tree_code code;
5923 location_t op_location, exp_location;
5924 gcc_assert (!after || c_dialect_objc ());
5925 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
5926 op_location = c_parser_peek_token (parser)->location;
5927 switch (c_parser_peek_token (parser)->type)
5929 case CPP_EQ:
5930 code = NOP_EXPR;
5931 break;
5932 case CPP_MULT_EQ:
5933 code = MULT_EXPR;
5934 break;
5935 case CPP_DIV_EQ:
5936 code = TRUNC_DIV_EXPR;
5937 break;
5938 case CPP_MOD_EQ:
5939 code = TRUNC_MOD_EXPR;
5940 break;
5941 case CPP_PLUS_EQ:
5942 code = PLUS_EXPR;
5943 break;
5944 case CPP_MINUS_EQ:
5945 code = MINUS_EXPR;
5946 break;
5947 case CPP_LSHIFT_EQ:
5948 code = LSHIFT_EXPR;
5949 break;
5950 case CPP_RSHIFT_EQ:
5951 code = RSHIFT_EXPR;
5952 break;
5953 case CPP_AND_EQ:
5954 code = BIT_AND_EXPR;
5955 break;
5956 case CPP_XOR_EQ:
5957 code = BIT_XOR_EXPR;
5958 break;
5959 case CPP_OR_EQ:
5960 code = BIT_IOR_EXPR;
5961 break;
5962 default:
5963 return lhs;
5965 c_parser_consume_token (parser);
5966 exp_location = c_parser_peek_token (parser)->location;
5967 rhs = c_parser_expr_no_commas (parser, NULL);
5968 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
5970 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
5971 code, exp_location, rhs.value,
5972 rhs.original_type);
5973 if (code == NOP_EXPR)
5974 ret.original_code = MODIFY_EXPR;
5975 else
5977 TREE_NO_WARNING (ret.value) = 1;
5978 ret.original_code = ERROR_MARK;
5980 ret.original_type = NULL;
5981 return ret;
5984 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
5985 is not NULL then it is an Objective-C message expression which is
5986 the primary-expression starting the expression as an initializer.
5988 conditional-expression:
5989 logical-OR-expression
5990 logical-OR-expression ? expression : conditional-expression
5992 GNU extensions:
5994 conditional-expression:
5995 logical-OR-expression ? : conditional-expression
5998 static struct c_expr
5999 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6000 tree omp_atomic_lhs)
6002 struct c_expr cond, exp1, exp2, ret;
6003 location_t cond_loc, colon_loc, middle_loc;
6005 gcc_assert (!after || c_dialect_objc ());
6007 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6009 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6010 return cond;
6011 cond_loc = c_parser_peek_token (parser)->location;
6012 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6013 c_parser_consume_token (parser);
6014 if (c_parser_next_token_is (parser, CPP_COLON))
6016 tree eptype = NULL_TREE;
6018 middle_loc = c_parser_peek_token (parser)->location;
6019 pedwarn (middle_loc, OPT_Wpedantic,
6020 "ISO C forbids omitting the middle term of a ?: expression");
6021 warn_for_omitted_condop (middle_loc, cond.value);
6022 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6024 eptype = TREE_TYPE (cond.value);
6025 cond.value = TREE_OPERAND (cond.value, 0);
6027 /* Make sure first operand is calculated only once. */
6028 exp1.value = c_save_expr (default_conversion (cond.value));
6029 if (eptype)
6030 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6031 exp1.original_type = NULL;
6032 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6033 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6035 else
6037 cond.value
6038 = c_objc_common_truthvalue_conversion
6039 (cond_loc, default_conversion (cond.value));
6040 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6041 exp1 = c_parser_expression_conv (parser);
6042 mark_exp_read (exp1.value);
6043 c_inhibit_evaluation_warnings +=
6044 ((cond.value == truthvalue_true_node)
6045 - (cond.value == truthvalue_false_node));
6048 colon_loc = c_parser_peek_token (parser)->location;
6049 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6051 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6052 ret.value = error_mark_node;
6053 ret.original_code = ERROR_MARK;
6054 ret.original_type = NULL;
6055 return ret;
6058 location_t exp2_loc = c_parser_peek_token (parser)->location;
6059 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6060 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6062 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6063 ret.value = build_conditional_expr (colon_loc, cond.value,
6064 cond.original_code == C_MAYBE_CONST_EXPR,
6065 exp1.value, exp1.original_type,
6066 exp2.value, exp2.original_type);
6067 ret.original_code = ERROR_MARK;
6068 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6069 ret.original_type = NULL;
6070 else
6072 tree t1, t2;
6074 /* If both sides are enum type, the default conversion will have
6075 made the type of the result be an integer type. We want to
6076 remember the enum types we started with. */
6077 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6078 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6079 ret.original_type = ((t1 != error_mark_node
6080 && t2 != error_mark_node
6081 && (TYPE_MAIN_VARIANT (t1)
6082 == TYPE_MAIN_VARIANT (t2)))
6083 ? t1
6084 : NULL);
6086 return ret;
6089 /* Parse a binary expression; that is, a logical-OR-expression (C90
6090 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6091 an Objective-C message expression which is the primary-expression
6092 starting the expression as an initializer.
6094 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6095 when it should be the unfolded lhs. In a valid OpenMP source,
6096 one of the operands of the toplevel binary expression must be equal
6097 to it. In that case, just return a build2 created binary operation
6098 rather than result of parser_build_binary_op.
6100 multiplicative-expression:
6101 cast-expression
6102 multiplicative-expression * cast-expression
6103 multiplicative-expression / cast-expression
6104 multiplicative-expression % cast-expression
6106 additive-expression:
6107 multiplicative-expression
6108 additive-expression + multiplicative-expression
6109 additive-expression - multiplicative-expression
6111 shift-expression:
6112 additive-expression
6113 shift-expression << additive-expression
6114 shift-expression >> additive-expression
6116 relational-expression:
6117 shift-expression
6118 relational-expression < shift-expression
6119 relational-expression > shift-expression
6120 relational-expression <= shift-expression
6121 relational-expression >= shift-expression
6123 equality-expression:
6124 relational-expression
6125 equality-expression == relational-expression
6126 equality-expression != relational-expression
6128 AND-expression:
6129 equality-expression
6130 AND-expression & equality-expression
6132 exclusive-OR-expression:
6133 AND-expression
6134 exclusive-OR-expression ^ AND-expression
6136 inclusive-OR-expression:
6137 exclusive-OR-expression
6138 inclusive-OR-expression | exclusive-OR-expression
6140 logical-AND-expression:
6141 inclusive-OR-expression
6142 logical-AND-expression && inclusive-OR-expression
6144 logical-OR-expression:
6145 logical-AND-expression
6146 logical-OR-expression || logical-AND-expression
6149 static struct c_expr
6150 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6151 tree omp_atomic_lhs)
6153 /* A binary expression is parsed using operator-precedence parsing,
6154 with the operands being cast expressions. All the binary
6155 operators are left-associative. Thus a binary expression is of
6156 form:
6158 E0 op1 E1 op2 E2 ...
6160 which we represent on a stack. On the stack, the precedence
6161 levels are strictly increasing. When a new operator is
6162 encountered of higher precedence than that at the top of the
6163 stack, it is pushed; its LHS is the top expression, and its RHS
6164 is everything parsed until it is popped. When a new operator is
6165 encountered with precedence less than or equal to that at the top
6166 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6167 by the result of the operation until the operator at the top of
6168 the stack has lower precedence than the new operator or there is
6169 only one element on the stack; then the top expression is the LHS
6170 of the new operator. In the case of logical AND and OR
6171 expressions, we also need to adjust c_inhibit_evaluation_warnings
6172 as appropriate when the operators are pushed and popped. */
6174 struct {
6175 /* The expression at this stack level. */
6176 struct c_expr expr;
6177 /* The precedence of the operator on its left, PREC_NONE at the
6178 bottom of the stack. */
6179 enum c_parser_prec prec;
6180 /* The operation on its left. */
6181 enum tree_code op;
6182 /* The source location of this operation. */
6183 location_t loc;
6184 } stack[NUM_PRECS];
6185 int sp;
6186 /* Location of the binary operator. */
6187 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6188 #define POP \
6189 do { \
6190 switch (stack[sp].op) \
6192 case TRUTH_ANDIF_EXPR: \
6193 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6194 == truthvalue_false_node); \
6195 break; \
6196 case TRUTH_ORIF_EXPR: \
6197 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6198 == truthvalue_true_node); \
6199 break; \
6200 default: \
6201 break; \
6203 stack[sp - 1].expr \
6204 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6205 stack[sp - 1].expr, true, true); \
6206 stack[sp].expr \
6207 = convert_lvalue_to_rvalue (stack[sp].loc, \
6208 stack[sp].expr, true, true); \
6209 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6210 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6211 && ((1 << stack[sp].prec) \
6212 & (1 << (PREC_BITOR | PREC_BITXOR | PREC_BITAND | PREC_SHIFT \
6213 | PREC_ADD | PREC_MULT))) \
6214 && stack[sp].op != TRUNC_MOD_EXPR \
6215 && stack[0].expr.value != error_mark_node \
6216 && stack[1].expr.value != error_mark_node \
6217 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6218 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6219 stack[0].expr.value \
6220 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6221 stack[0].expr.value, stack[1].expr.value); \
6222 else \
6223 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6224 stack[sp].op, \
6225 stack[sp - 1].expr, \
6226 stack[sp].expr); \
6227 sp--; \
6228 } while (0)
6229 gcc_assert (!after || c_dialect_objc ());
6230 stack[0].loc = c_parser_peek_token (parser)->location;
6231 stack[0].expr = c_parser_cast_expression (parser, after);
6232 stack[0].prec = PREC_NONE;
6233 sp = 0;
6234 while (true)
6236 enum c_parser_prec oprec;
6237 enum tree_code ocode;
6238 if (parser->error)
6239 goto out;
6240 switch (c_parser_peek_token (parser)->type)
6242 case CPP_MULT:
6243 oprec = PREC_MULT;
6244 ocode = MULT_EXPR;
6245 break;
6246 case CPP_DIV:
6247 oprec = PREC_MULT;
6248 ocode = TRUNC_DIV_EXPR;
6249 break;
6250 case CPP_MOD:
6251 oprec = PREC_MULT;
6252 ocode = TRUNC_MOD_EXPR;
6253 break;
6254 case CPP_PLUS:
6255 oprec = PREC_ADD;
6256 ocode = PLUS_EXPR;
6257 break;
6258 case CPP_MINUS:
6259 oprec = PREC_ADD;
6260 ocode = MINUS_EXPR;
6261 break;
6262 case CPP_LSHIFT:
6263 oprec = PREC_SHIFT;
6264 ocode = LSHIFT_EXPR;
6265 break;
6266 case CPP_RSHIFT:
6267 oprec = PREC_SHIFT;
6268 ocode = RSHIFT_EXPR;
6269 break;
6270 case CPP_LESS:
6271 oprec = PREC_REL;
6272 ocode = LT_EXPR;
6273 break;
6274 case CPP_GREATER:
6275 oprec = PREC_REL;
6276 ocode = GT_EXPR;
6277 break;
6278 case CPP_LESS_EQ:
6279 oprec = PREC_REL;
6280 ocode = LE_EXPR;
6281 break;
6282 case CPP_GREATER_EQ:
6283 oprec = PREC_REL;
6284 ocode = GE_EXPR;
6285 break;
6286 case CPP_EQ_EQ:
6287 oprec = PREC_EQ;
6288 ocode = EQ_EXPR;
6289 break;
6290 case CPP_NOT_EQ:
6291 oprec = PREC_EQ;
6292 ocode = NE_EXPR;
6293 break;
6294 case CPP_AND:
6295 oprec = PREC_BITAND;
6296 ocode = BIT_AND_EXPR;
6297 break;
6298 case CPP_XOR:
6299 oprec = PREC_BITXOR;
6300 ocode = BIT_XOR_EXPR;
6301 break;
6302 case CPP_OR:
6303 oprec = PREC_BITOR;
6304 ocode = BIT_IOR_EXPR;
6305 break;
6306 case CPP_AND_AND:
6307 oprec = PREC_LOGAND;
6308 ocode = TRUTH_ANDIF_EXPR;
6309 break;
6310 case CPP_OR_OR:
6311 oprec = PREC_LOGOR;
6312 ocode = TRUTH_ORIF_EXPR;
6313 break;
6314 default:
6315 /* Not a binary operator, so end of the binary
6316 expression. */
6317 goto out;
6319 binary_loc = c_parser_peek_token (parser)->location;
6320 while (oprec <= stack[sp].prec)
6321 POP;
6322 c_parser_consume_token (parser);
6323 switch (ocode)
6325 case TRUTH_ANDIF_EXPR:
6326 stack[sp].expr
6327 = convert_lvalue_to_rvalue (stack[sp].loc,
6328 stack[sp].expr, true, true);
6329 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6330 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6331 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6332 == truthvalue_false_node);
6333 break;
6334 case TRUTH_ORIF_EXPR:
6335 stack[sp].expr
6336 = convert_lvalue_to_rvalue (stack[sp].loc,
6337 stack[sp].expr, true, true);
6338 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6339 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6340 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6341 == truthvalue_true_node);
6342 break;
6343 default:
6344 break;
6346 sp++;
6347 stack[sp].loc = binary_loc;
6348 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6349 stack[sp].prec = oprec;
6350 stack[sp].op = ocode;
6351 stack[sp].loc = binary_loc;
6353 out:
6354 while (sp > 0)
6355 POP;
6356 return stack[0].expr;
6357 #undef POP
6360 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6361 NULL then it is an Objective-C message expression which is the
6362 primary-expression starting the expression as an initializer.
6364 cast-expression:
6365 unary-expression
6366 ( type-name ) unary-expression
6369 static struct c_expr
6370 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6372 location_t cast_loc = c_parser_peek_token (parser)->location;
6373 gcc_assert (!after || c_dialect_objc ());
6374 if (after)
6375 return c_parser_postfix_expression_after_primary (parser,
6376 cast_loc, *after);
6377 /* If the expression begins with a parenthesized type name, it may
6378 be either a cast or a compound literal; we need to see whether
6379 the next character is '{' to tell the difference. If not, it is
6380 an unary expression. Full detection of unknown typenames here
6381 would require a 3-token lookahead. */
6382 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6383 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6385 struct c_type_name *type_name;
6386 struct c_expr ret;
6387 struct c_expr expr;
6388 c_parser_consume_token (parser);
6389 type_name = c_parser_type_name (parser);
6390 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6391 if (type_name == NULL)
6393 ret.value = error_mark_node;
6394 ret.original_code = ERROR_MARK;
6395 ret.original_type = NULL;
6396 return ret;
6399 /* Save casted types in the function's used types hash table. */
6400 used_types_insert (type_name->specs->type);
6402 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6403 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6404 cast_loc);
6406 location_t expr_loc = c_parser_peek_token (parser)->location;
6407 expr = c_parser_cast_expression (parser, NULL);
6408 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6410 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6411 ret.original_code = ERROR_MARK;
6412 ret.original_type = NULL;
6413 return ret;
6415 else
6416 return c_parser_unary_expression (parser);
6419 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6421 unary-expression:
6422 postfix-expression
6423 ++ unary-expression
6424 -- unary-expression
6425 unary-operator cast-expression
6426 sizeof unary-expression
6427 sizeof ( type-name )
6429 unary-operator: one of
6430 & * + - ~ !
6432 GNU extensions:
6434 unary-expression:
6435 __alignof__ unary-expression
6436 __alignof__ ( type-name )
6437 && identifier
6439 (C11 permits _Alignof with type names only.)
6441 unary-operator: one of
6442 __extension__ __real__ __imag__
6444 Transactional Memory:
6446 unary-expression:
6447 transaction-expression
6449 In addition, the GNU syntax treats ++ and -- as unary operators, so
6450 they may be applied to cast expressions with errors for non-lvalues
6451 given later. */
6453 static struct c_expr
6454 c_parser_unary_expression (c_parser *parser)
6456 int ext;
6457 struct c_expr ret, op;
6458 location_t op_loc = c_parser_peek_token (parser)->location;
6459 location_t exp_loc;
6460 ret.original_code = ERROR_MARK;
6461 ret.original_type = NULL;
6462 switch (c_parser_peek_token (parser)->type)
6464 case CPP_PLUS_PLUS:
6465 c_parser_consume_token (parser);
6466 exp_loc = c_parser_peek_token (parser)->location;
6467 op = c_parser_cast_expression (parser, NULL);
6469 /* If there is array notations in op, we expand them. */
6470 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6471 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6472 else
6474 op = default_function_array_read_conversion (exp_loc, op);
6475 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6477 case CPP_MINUS_MINUS:
6478 c_parser_consume_token (parser);
6479 exp_loc = c_parser_peek_token (parser)->location;
6480 op = c_parser_cast_expression (parser, NULL);
6482 /* If there is array notations in op, we expand them. */
6483 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6484 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6485 else
6487 op = default_function_array_read_conversion (exp_loc, op);
6488 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6490 case CPP_AND:
6491 c_parser_consume_token (parser);
6492 op = c_parser_cast_expression (parser, NULL);
6493 mark_exp_read (op.value);
6494 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6495 case CPP_MULT:
6496 c_parser_consume_token (parser);
6497 exp_loc = c_parser_peek_token (parser)->location;
6498 op = c_parser_cast_expression (parser, NULL);
6499 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6500 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6501 return ret;
6502 case CPP_PLUS:
6503 if (!c_dialect_objc () && !in_system_header_at (input_location))
6504 warning_at (op_loc,
6505 OPT_Wtraditional,
6506 "traditional C rejects the unary plus operator");
6507 c_parser_consume_token (parser);
6508 exp_loc = c_parser_peek_token (parser)->location;
6509 op = c_parser_cast_expression (parser, NULL);
6510 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6511 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6512 case CPP_MINUS:
6513 c_parser_consume_token (parser);
6514 exp_loc = c_parser_peek_token (parser)->location;
6515 op = c_parser_cast_expression (parser, NULL);
6516 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6517 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6518 case CPP_COMPL:
6519 c_parser_consume_token (parser);
6520 exp_loc = c_parser_peek_token (parser)->location;
6521 op = c_parser_cast_expression (parser, NULL);
6522 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6523 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6524 case CPP_NOT:
6525 c_parser_consume_token (parser);
6526 exp_loc = c_parser_peek_token (parser)->location;
6527 op = c_parser_cast_expression (parser, NULL);
6528 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6529 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6530 case CPP_AND_AND:
6531 /* Refer to the address of a label as a pointer. */
6532 c_parser_consume_token (parser);
6533 if (c_parser_next_token_is (parser, CPP_NAME))
6535 ret.value = finish_label_address_expr
6536 (c_parser_peek_token (parser)->value, op_loc);
6537 c_parser_consume_token (parser);
6539 else
6541 c_parser_error (parser, "expected identifier");
6542 ret.value = error_mark_node;
6544 return ret;
6545 case CPP_KEYWORD:
6546 switch (c_parser_peek_token (parser)->keyword)
6548 case RID_SIZEOF:
6549 return c_parser_sizeof_expression (parser);
6550 case RID_ALIGNOF:
6551 return c_parser_alignof_expression (parser);
6552 case RID_EXTENSION:
6553 c_parser_consume_token (parser);
6554 ext = disable_extension_diagnostics ();
6555 ret = c_parser_cast_expression (parser, NULL);
6556 restore_extension_diagnostics (ext);
6557 return ret;
6558 case RID_REALPART:
6559 c_parser_consume_token (parser);
6560 exp_loc = c_parser_peek_token (parser)->location;
6561 op = c_parser_cast_expression (parser, NULL);
6562 op = default_function_array_conversion (exp_loc, op);
6563 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6564 case RID_IMAGPART:
6565 c_parser_consume_token (parser);
6566 exp_loc = c_parser_peek_token (parser)->location;
6567 op = c_parser_cast_expression (parser, NULL);
6568 op = default_function_array_conversion (exp_loc, op);
6569 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6570 case RID_TRANSACTION_ATOMIC:
6571 case RID_TRANSACTION_RELAXED:
6572 return c_parser_transaction_expression (parser,
6573 c_parser_peek_token (parser)->keyword);
6574 default:
6575 return c_parser_postfix_expression (parser);
6577 default:
6578 return c_parser_postfix_expression (parser);
6582 /* Parse a sizeof expression. */
6584 static struct c_expr
6585 c_parser_sizeof_expression (c_parser *parser)
6587 struct c_expr expr;
6588 location_t expr_loc;
6589 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6590 c_parser_consume_token (parser);
6591 c_inhibit_evaluation_warnings++;
6592 in_sizeof++;
6593 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6594 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6596 /* Either sizeof ( type-name ) or sizeof unary-expression
6597 starting with a compound literal. */
6598 struct c_type_name *type_name;
6599 c_parser_consume_token (parser);
6600 expr_loc = c_parser_peek_token (parser)->location;
6601 type_name = c_parser_type_name (parser);
6602 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6603 if (type_name == NULL)
6605 struct c_expr ret;
6606 c_inhibit_evaluation_warnings--;
6607 in_sizeof--;
6608 ret.value = error_mark_node;
6609 ret.original_code = ERROR_MARK;
6610 ret.original_type = NULL;
6611 return ret;
6613 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6615 expr = c_parser_postfix_expression_after_paren_type (parser,
6616 type_name,
6617 expr_loc);
6618 goto sizeof_expr;
6620 /* sizeof ( type-name ). */
6621 c_inhibit_evaluation_warnings--;
6622 in_sizeof--;
6623 return c_expr_sizeof_type (expr_loc, type_name);
6625 else
6627 expr_loc = c_parser_peek_token (parser)->location;
6628 expr = c_parser_unary_expression (parser);
6629 sizeof_expr:
6630 c_inhibit_evaluation_warnings--;
6631 in_sizeof--;
6632 mark_exp_read (expr.value);
6633 if (TREE_CODE (expr.value) == COMPONENT_REF
6634 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6635 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6636 return c_expr_sizeof_expr (expr_loc, expr);
6640 /* Parse an alignof expression. */
6642 static struct c_expr
6643 c_parser_alignof_expression (c_parser *parser)
6645 struct c_expr expr;
6646 location_t loc = c_parser_peek_token (parser)->location;
6647 tree alignof_spelling = c_parser_peek_token (parser)->value;
6648 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6649 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6650 "_Alignof") == 0;
6651 /* A diagnostic is not required for the use of this identifier in
6652 the implementation namespace; only diagnose it for the C11
6653 spelling because of existing code using the other spellings. */
6654 if (is_c11_alignof)
6656 if (flag_isoc99)
6657 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6658 alignof_spelling);
6659 else
6660 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6661 alignof_spelling);
6663 c_parser_consume_token (parser);
6664 c_inhibit_evaluation_warnings++;
6665 in_alignof++;
6666 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6667 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6669 /* Either __alignof__ ( type-name ) or __alignof__
6670 unary-expression starting with a compound literal. */
6671 location_t loc;
6672 struct c_type_name *type_name;
6673 struct c_expr ret;
6674 c_parser_consume_token (parser);
6675 loc = c_parser_peek_token (parser)->location;
6676 type_name = c_parser_type_name (parser);
6677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6678 if (type_name == NULL)
6680 struct c_expr ret;
6681 c_inhibit_evaluation_warnings--;
6682 in_alignof--;
6683 ret.value = error_mark_node;
6684 ret.original_code = ERROR_MARK;
6685 ret.original_type = NULL;
6686 return ret;
6688 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6690 expr = c_parser_postfix_expression_after_paren_type (parser,
6691 type_name,
6692 loc);
6693 goto alignof_expr;
6695 /* alignof ( type-name ). */
6696 c_inhibit_evaluation_warnings--;
6697 in_alignof--;
6698 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6699 NULL, NULL),
6700 false, is_c11_alignof, 1);
6701 ret.original_code = ERROR_MARK;
6702 ret.original_type = NULL;
6703 return ret;
6705 else
6707 struct c_expr ret;
6708 expr = c_parser_unary_expression (parser);
6709 alignof_expr:
6710 mark_exp_read (expr.value);
6711 c_inhibit_evaluation_warnings--;
6712 in_alignof--;
6713 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6714 alignof_spelling);
6715 ret.value = c_alignof_expr (loc, expr.value);
6716 ret.original_code = ERROR_MARK;
6717 ret.original_type = NULL;
6718 return ret;
6722 /* Helper function to read arguments of builtins which are interfaces
6723 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6724 others. The name of the builtin is passed using BNAME parameter.
6725 Function returns true if there were no errors while parsing and
6726 stores the arguments in CEXPR_LIST. */
6727 static bool
6728 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6729 vec<c_expr_t, va_gc> **ret_cexpr_list,
6730 bool choose_expr_p)
6732 location_t loc = c_parser_peek_token (parser)->location;
6733 vec<c_expr_t, va_gc> *cexpr_list;
6734 c_expr_t expr;
6735 bool saved_force_folding_builtin_constant_p;
6737 *ret_cexpr_list = NULL;
6738 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6740 error_at (loc, "cannot take address of %qs", bname);
6741 return false;
6744 c_parser_consume_token (parser);
6746 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6748 c_parser_consume_token (parser);
6749 return true;
6752 saved_force_folding_builtin_constant_p
6753 = force_folding_builtin_constant_p;
6754 force_folding_builtin_constant_p |= choose_expr_p;
6755 expr = c_parser_expr_no_commas (parser, NULL);
6756 force_folding_builtin_constant_p
6757 = saved_force_folding_builtin_constant_p;
6758 vec_alloc (cexpr_list, 1);
6759 vec_safe_push (cexpr_list, expr);
6760 while (c_parser_next_token_is (parser, CPP_COMMA))
6762 c_parser_consume_token (parser);
6763 expr = c_parser_expr_no_commas (parser, NULL);
6764 vec_safe_push (cexpr_list, expr);
6767 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6768 return false;
6770 *ret_cexpr_list = cexpr_list;
6771 return true;
6774 /* This represents a single generic-association. */
6776 struct c_generic_association
6778 /* The location of the starting token of the type. */
6779 location_t type_location;
6780 /* The association's type, or NULL_TREE for 'default'. */
6781 tree type;
6782 /* The association's expression. */
6783 struct c_expr expression;
6786 /* Parse a generic-selection. (C11 6.5.1.1).
6788 generic-selection:
6789 _Generic ( assignment-expression , generic-assoc-list )
6791 generic-assoc-list:
6792 generic-association
6793 generic-assoc-list , generic-association
6795 generic-association:
6796 type-name : assignment-expression
6797 default : assignment-expression
6800 static struct c_expr
6801 c_parser_generic_selection (c_parser *parser)
6803 vec<c_generic_association> associations = vNULL;
6804 struct c_expr selector, error_expr;
6805 tree selector_type;
6806 struct c_generic_association matched_assoc;
6807 bool match_found = false;
6808 location_t generic_loc, selector_loc;
6810 error_expr.original_code = ERROR_MARK;
6811 error_expr.original_type = NULL;
6812 error_expr.value = error_mark_node;
6813 matched_assoc.type_location = UNKNOWN_LOCATION;
6814 matched_assoc.type = NULL_TREE;
6815 matched_assoc.expression = error_expr;
6817 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
6818 generic_loc = c_parser_peek_token (parser)->location;
6819 c_parser_consume_token (parser);
6820 if (flag_isoc99)
6821 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6822 "ISO C99 does not support %<_Generic%>");
6823 else
6824 pedwarn_c99 (generic_loc, OPT_Wpedantic,
6825 "ISO C90 does not support %<_Generic%>");
6827 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6828 return error_expr;
6830 c_inhibit_evaluation_warnings++;
6831 selector_loc = c_parser_peek_token (parser)->location;
6832 selector = c_parser_expr_no_commas (parser, NULL);
6833 selector = default_function_array_conversion (selector_loc, selector);
6834 c_inhibit_evaluation_warnings--;
6836 if (selector.value == error_mark_node)
6838 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6839 return selector;
6841 selector_type = TREE_TYPE (selector.value);
6842 /* In ISO C terms, rvalues (including the controlling expression of
6843 _Generic) do not have qualified types. */
6844 if (TREE_CODE (selector_type) != ARRAY_TYPE)
6845 selector_type = TYPE_MAIN_VARIANT (selector_type);
6846 /* In ISO C terms, _Noreturn is not part of the type of expressions
6847 such as &abort, but in GCC it is represented internally as a type
6848 qualifier. */
6849 if (FUNCTION_POINTER_TYPE_P (selector_type)
6850 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
6851 selector_type
6852 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
6854 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
6856 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6857 return error_expr;
6860 while (1)
6862 struct c_generic_association assoc, *iter;
6863 unsigned int ix;
6864 c_token *token = c_parser_peek_token (parser);
6866 assoc.type_location = token->location;
6867 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
6869 c_parser_consume_token (parser);
6870 assoc.type = NULL_TREE;
6872 else
6874 struct c_type_name *type_name;
6876 type_name = c_parser_type_name (parser);
6877 if (type_name == NULL)
6879 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6880 goto error_exit;
6882 assoc.type = groktypename (type_name, NULL, NULL);
6883 if (assoc.type == error_mark_node)
6885 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6886 goto error_exit;
6889 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
6890 error_at (assoc.type_location,
6891 "%<_Generic%> association has function type");
6892 else if (!COMPLETE_TYPE_P (assoc.type))
6893 error_at (assoc.type_location,
6894 "%<_Generic%> association has incomplete type");
6896 if (variably_modified_type_p (assoc.type, NULL_TREE))
6897 error_at (assoc.type_location,
6898 "%<_Generic%> association has "
6899 "variable length type");
6902 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6904 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6905 goto error_exit;
6908 assoc.expression = c_parser_expr_no_commas (parser, NULL);
6909 if (assoc.expression.value == error_mark_node)
6911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6912 goto error_exit;
6915 for (ix = 0; associations.iterate (ix, &iter); ++ix)
6917 if (assoc.type == NULL_TREE)
6919 if (iter->type == NULL_TREE)
6921 error_at (assoc.type_location,
6922 "duplicate %<default%> case in %<_Generic%>");
6923 inform (iter->type_location, "original %<default%> is here");
6926 else if (iter->type != NULL_TREE)
6928 if (comptypes (assoc.type, iter->type))
6930 error_at (assoc.type_location,
6931 "%<_Generic%> specifies two compatible types");
6932 inform (iter->type_location, "compatible type is here");
6937 if (assoc.type == NULL_TREE)
6939 if (!match_found)
6941 matched_assoc = assoc;
6942 match_found = true;
6945 else if (comptypes (assoc.type, selector_type))
6947 if (!match_found || matched_assoc.type == NULL_TREE)
6949 matched_assoc = assoc;
6950 match_found = true;
6952 else
6954 error_at (assoc.type_location,
6955 "%<_Generic> selector matches multiple associations");
6956 inform (matched_assoc.type_location,
6957 "other match is here");
6961 associations.safe_push (assoc);
6963 if (c_parser_peek_token (parser)->type != CPP_COMMA)
6964 break;
6965 c_parser_consume_token (parser);
6968 associations.release ();
6970 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6972 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6973 return error_expr;
6976 if (!match_found)
6978 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
6979 "compatible with any association",
6980 selector_type);
6981 return error_expr;
6984 return matched_assoc.expression;
6986 error_exit:
6987 associations.release ();
6988 return error_expr;
6991 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
6993 postfix-expression:
6994 primary-expression
6995 postfix-expression [ expression ]
6996 postfix-expression ( argument-expression-list[opt] )
6997 postfix-expression . identifier
6998 postfix-expression -> identifier
6999 postfix-expression ++
7000 postfix-expression --
7001 ( type-name ) { initializer-list }
7002 ( type-name ) { initializer-list , }
7004 argument-expression-list:
7005 argument-expression
7006 argument-expression-list , argument-expression
7008 primary-expression:
7009 identifier
7010 constant
7011 string-literal
7012 ( expression )
7013 generic-selection
7015 GNU extensions:
7017 primary-expression:
7018 __func__
7019 (treated as a keyword in GNU C)
7020 __FUNCTION__
7021 __PRETTY_FUNCTION__
7022 ( compound-statement )
7023 __builtin_va_arg ( assignment-expression , type-name )
7024 __builtin_offsetof ( type-name , offsetof-member-designator )
7025 __builtin_choose_expr ( assignment-expression ,
7026 assignment-expression ,
7027 assignment-expression )
7028 __builtin_types_compatible_p ( type-name , type-name )
7029 __builtin_complex ( assignment-expression , assignment-expression )
7030 __builtin_shuffle ( assignment-expression , assignment-expression )
7031 __builtin_shuffle ( assignment-expression ,
7032 assignment-expression ,
7033 assignment-expression, )
7035 offsetof-member-designator:
7036 identifier
7037 offsetof-member-designator . identifier
7038 offsetof-member-designator [ expression ]
7040 Objective-C:
7042 primary-expression:
7043 [ objc-receiver objc-message-args ]
7044 @selector ( objc-selector-arg )
7045 @protocol ( identifier )
7046 @encode ( type-name )
7047 objc-string-literal
7048 Classname . identifier
7051 static struct c_expr
7052 c_parser_postfix_expression (c_parser *parser)
7054 struct c_expr expr, e1;
7055 struct c_type_name *t1, *t2;
7056 location_t loc = c_parser_peek_token (parser)->location;;
7057 expr.original_code = ERROR_MARK;
7058 expr.original_type = NULL;
7059 switch (c_parser_peek_token (parser)->type)
7061 case CPP_NUMBER:
7062 expr.value = c_parser_peek_token (parser)->value;
7063 loc = c_parser_peek_token (parser)->location;
7064 c_parser_consume_token (parser);
7065 if (TREE_CODE (expr.value) == FIXED_CST
7066 && !targetm.fixed_point_supported_p ())
7068 error_at (loc, "fixed-point types not supported for this target");
7069 expr.value = error_mark_node;
7071 break;
7072 case CPP_CHAR:
7073 case CPP_CHAR16:
7074 case CPP_CHAR32:
7075 case CPP_WCHAR:
7076 expr.value = c_parser_peek_token (parser)->value;
7077 c_parser_consume_token (parser);
7078 break;
7079 case CPP_STRING:
7080 case CPP_STRING16:
7081 case CPP_STRING32:
7082 case CPP_WSTRING:
7083 case CPP_UTF8STRING:
7084 expr.value = c_parser_peek_token (parser)->value;
7085 expr.original_code = STRING_CST;
7086 c_parser_consume_token (parser);
7087 break;
7088 case CPP_OBJC_STRING:
7089 gcc_assert (c_dialect_objc ());
7090 expr.value
7091 = objc_build_string_object (c_parser_peek_token (parser)->value);
7092 c_parser_consume_token (parser);
7093 break;
7094 case CPP_NAME:
7095 switch (c_parser_peek_token (parser)->id_kind)
7097 case C_ID_ID:
7099 tree id = c_parser_peek_token (parser)->value;
7100 c_parser_consume_token (parser);
7101 expr.value = build_external_ref (loc, id,
7102 (c_parser_peek_token (parser)->type
7103 == CPP_OPEN_PAREN),
7104 &expr.original_type);
7105 break;
7107 case C_ID_CLASSNAME:
7109 /* Here we parse the Objective-C 2.0 Class.name dot
7110 syntax. */
7111 tree class_name = c_parser_peek_token (parser)->value;
7112 tree component;
7113 c_parser_consume_token (parser);
7114 gcc_assert (c_dialect_objc ());
7115 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7117 expr.value = error_mark_node;
7118 break;
7120 if (c_parser_next_token_is_not (parser, CPP_NAME))
7122 c_parser_error (parser, "expected identifier");
7123 expr.value = error_mark_node;
7124 break;
7126 component = c_parser_peek_token (parser)->value;
7127 c_parser_consume_token (parser);
7128 expr.value = objc_build_class_component_ref (class_name,
7129 component);
7130 break;
7132 default:
7133 c_parser_error (parser, "expected expression");
7134 expr.value = error_mark_node;
7135 break;
7137 break;
7138 case CPP_OPEN_PAREN:
7139 /* A parenthesized expression, statement expression or compound
7140 literal. */
7141 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7143 /* A statement expression. */
7144 tree stmt;
7145 location_t brace_loc;
7146 c_parser_consume_token (parser);
7147 brace_loc = c_parser_peek_token (parser)->location;
7148 c_parser_consume_token (parser);
7149 if (!building_stmt_list_p ())
7151 error_at (loc, "braced-group within expression allowed "
7152 "only inside a function");
7153 parser->error = true;
7154 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7155 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7156 expr.value = error_mark_node;
7157 break;
7159 stmt = c_begin_stmt_expr ();
7160 c_parser_compound_statement_nostart (parser);
7161 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7162 "expected %<)%>");
7163 pedwarn (loc, OPT_Wpedantic,
7164 "ISO C forbids braced-groups within expressions");
7165 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7166 mark_exp_read (expr.value);
7168 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7170 /* A compound literal. ??? Can we actually get here rather
7171 than going directly to
7172 c_parser_postfix_expression_after_paren_type from
7173 elsewhere? */
7174 location_t loc;
7175 struct c_type_name *type_name;
7176 c_parser_consume_token (parser);
7177 loc = c_parser_peek_token (parser)->location;
7178 type_name = c_parser_type_name (parser);
7179 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7180 "expected %<)%>");
7181 if (type_name == NULL)
7183 expr.value = error_mark_node;
7185 else
7186 expr = c_parser_postfix_expression_after_paren_type (parser,
7187 type_name,
7188 loc);
7190 else
7192 /* A parenthesized expression. */
7193 c_parser_consume_token (parser);
7194 expr = c_parser_expression (parser);
7195 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7196 TREE_NO_WARNING (expr.value) = 1;
7197 if (expr.original_code != C_MAYBE_CONST_EXPR)
7198 expr.original_code = ERROR_MARK;
7199 /* Don't change EXPR.ORIGINAL_TYPE. */
7200 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7201 "expected %<)%>");
7203 break;
7204 case CPP_KEYWORD:
7205 switch (c_parser_peek_token (parser)->keyword)
7207 case RID_FUNCTION_NAME:
7208 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7209 "%<__FUNCTION__%> predefined identifier");
7210 expr.value = fname_decl (loc,
7211 c_parser_peek_token (parser)->keyword,
7212 c_parser_peek_token (parser)->value);
7213 c_parser_consume_token (parser);
7214 break;
7215 case RID_PRETTY_FUNCTION_NAME:
7216 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7217 "%<__PRETTY_FUNCTION__%> predefined identifier");
7218 expr.value = fname_decl (loc,
7219 c_parser_peek_token (parser)->keyword,
7220 c_parser_peek_token (parser)->value);
7221 c_parser_consume_token (parser);
7222 break;
7223 case RID_C99_FUNCTION_NAME:
7224 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7225 "%<__func__%> predefined identifier");
7226 expr.value = fname_decl (loc,
7227 c_parser_peek_token (parser)->keyword,
7228 c_parser_peek_token (parser)->value);
7229 c_parser_consume_token (parser);
7230 break;
7231 case RID_VA_ARG:
7232 c_parser_consume_token (parser);
7233 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7235 expr.value = error_mark_node;
7236 break;
7238 e1 = c_parser_expr_no_commas (parser, NULL);
7239 mark_exp_read (e1.value);
7240 e1.value = c_fully_fold (e1.value, false, NULL);
7241 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7244 expr.value = error_mark_node;
7245 break;
7247 loc = c_parser_peek_token (parser)->location;
7248 t1 = c_parser_type_name (parser);
7249 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7250 "expected %<)%>");
7251 if (t1 == NULL)
7253 expr.value = error_mark_node;
7255 else
7257 tree type_expr = NULL_TREE;
7258 expr.value = c_build_va_arg (loc, e1.value,
7259 groktypename (t1, &type_expr, NULL));
7260 if (type_expr)
7262 expr.value = build2 (C_MAYBE_CONST_EXPR,
7263 TREE_TYPE (expr.value), type_expr,
7264 expr.value);
7265 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7268 break;
7269 case RID_OFFSETOF:
7270 c_parser_consume_token (parser);
7271 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7273 expr.value = error_mark_node;
7274 break;
7276 t1 = c_parser_type_name (parser);
7277 if (t1 == NULL)
7278 parser->error = true;
7279 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7280 gcc_assert (parser->error);
7281 if (parser->error)
7283 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7284 expr.value = error_mark_node;
7285 break;
7289 tree type = groktypename (t1, NULL, NULL);
7290 tree offsetof_ref;
7291 if (type == error_mark_node)
7292 offsetof_ref = error_mark_node;
7293 else
7295 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7296 SET_EXPR_LOCATION (offsetof_ref, loc);
7298 /* Parse the second argument to __builtin_offsetof. We
7299 must have one identifier, and beyond that we want to
7300 accept sub structure and sub array references. */
7301 if (c_parser_next_token_is (parser, CPP_NAME))
7303 offsetof_ref = build_component_ref
7304 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7305 c_parser_consume_token (parser);
7306 while (c_parser_next_token_is (parser, CPP_DOT)
7307 || c_parser_next_token_is (parser,
7308 CPP_OPEN_SQUARE)
7309 || c_parser_next_token_is (parser,
7310 CPP_DEREF))
7312 if (c_parser_next_token_is (parser, CPP_DEREF))
7314 loc = c_parser_peek_token (parser)->location;
7315 offsetof_ref = build_array_ref (loc,
7316 offsetof_ref,
7317 integer_zero_node);
7318 goto do_dot;
7320 else if (c_parser_next_token_is (parser, CPP_DOT))
7322 do_dot:
7323 c_parser_consume_token (parser);
7324 if (c_parser_next_token_is_not (parser,
7325 CPP_NAME))
7327 c_parser_error (parser, "expected identifier");
7328 break;
7330 offsetof_ref = build_component_ref
7331 (loc, offsetof_ref,
7332 c_parser_peek_token (parser)->value);
7333 c_parser_consume_token (parser);
7335 else
7337 struct c_expr ce;
7338 tree idx;
7339 loc = c_parser_peek_token (parser)->location;
7340 c_parser_consume_token (parser);
7341 ce = c_parser_expression (parser);
7342 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7343 idx = ce.value;
7344 idx = c_fully_fold (idx, false, NULL);
7345 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7346 "expected %<]%>");
7347 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7351 else
7352 c_parser_error (parser, "expected identifier");
7353 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7354 "expected %<)%>");
7355 expr.value = fold_offsetof (offsetof_ref);
7357 break;
7358 case RID_CHOOSE_EXPR:
7360 vec<c_expr_t, va_gc> *cexpr_list;
7361 c_expr_t *e1_p, *e2_p, *e3_p;
7362 tree c;
7364 c_parser_consume_token (parser);
7365 if (!c_parser_get_builtin_args (parser,
7366 "__builtin_choose_expr",
7367 &cexpr_list, true))
7369 expr.value = error_mark_node;
7370 break;
7373 if (vec_safe_length (cexpr_list) != 3)
7375 error_at (loc, "wrong number of arguments to "
7376 "%<__builtin_choose_expr%>");
7377 expr.value = error_mark_node;
7378 break;
7381 e1_p = &(*cexpr_list)[0];
7382 e2_p = &(*cexpr_list)[1];
7383 e3_p = &(*cexpr_list)[2];
7385 c = e1_p->value;
7386 mark_exp_read (e2_p->value);
7387 mark_exp_read (e3_p->value);
7388 if (TREE_CODE (c) != INTEGER_CST
7389 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7390 error_at (loc,
7391 "first argument to %<__builtin_choose_expr%> not"
7392 " a constant");
7393 constant_expression_warning (c);
7394 expr = integer_zerop (c) ? *e3_p : *e2_p;
7395 break;
7397 case RID_TYPES_COMPATIBLE_P:
7398 c_parser_consume_token (parser);
7399 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7401 expr.value = error_mark_node;
7402 break;
7404 t1 = c_parser_type_name (parser);
7405 if (t1 == NULL)
7407 expr.value = error_mark_node;
7408 break;
7410 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7412 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7413 expr.value = error_mark_node;
7414 break;
7416 t2 = c_parser_type_name (parser);
7417 if (t2 == NULL)
7419 expr.value = error_mark_node;
7420 break;
7422 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7423 "expected %<)%>");
7425 tree e1, e2;
7426 e1 = groktypename (t1, NULL, NULL);
7427 e2 = groktypename (t2, NULL, NULL);
7428 if (e1 == error_mark_node || e2 == error_mark_node)
7430 expr.value = error_mark_node;
7431 break;
7434 e1 = TYPE_MAIN_VARIANT (e1);
7435 e2 = TYPE_MAIN_VARIANT (e2);
7437 expr.value
7438 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7440 break;
7441 case RID_BUILTIN_COMPLEX:
7443 vec<c_expr_t, va_gc> *cexpr_list;
7444 c_expr_t *e1_p, *e2_p;
7446 c_parser_consume_token (parser);
7447 if (!c_parser_get_builtin_args (parser,
7448 "__builtin_complex",
7449 &cexpr_list, false))
7451 expr.value = error_mark_node;
7452 break;
7455 if (vec_safe_length (cexpr_list) != 2)
7457 error_at (loc, "wrong number of arguments to "
7458 "%<__builtin_complex%>");
7459 expr.value = error_mark_node;
7460 break;
7463 e1_p = &(*cexpr_list)[0];
7464 e2_p = &(*cexpr_list)[1];
7466 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7467 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7468 e1_p->value = convert (TREE_TYPE (e1_p->value),
7469 TREE_OPERAND (e1_p->value, 0));
7470 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7471 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7472 e2_p->value = convert (TREE_TYPE (e2_p->value),
7473 TREE_OPERAND (e2_p->value, 0));
7474 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7475 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7476 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7477 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7479 error_at (loc, "%<__builtin_complex%> operand "
7480 "not of real binary floating-point type");
7481 expr.value = error_mark_node;
7482 break;
7484 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7485 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7487 error_at (loc,
7488 "%<__builtin_complex%> operands of different types");
7489 expr.value = error_mark_node;
7490 break;
7492 pedwarn_c90 (loc, OPT_Wpedantic,
7493 "ISO C90 does not support complex types");
7494 expr.value = build2 (COMPLEX_EXPR,
7495 build_complex_type
7496 (TYPE_MAIN_VARIANT
7497 (TREE_TYPE (e1_p->value))),
7498 e1_p->value, e2_p->value);
7499 break;
7501 case RID_BUILTIN_SHUFFLE:
7503 vec<c_expr_t, va_gc> *cexpr_list;
7504 unsigned int i;
7505 c_expr_t *p;
7507 c_parser_consume_token (parser);
7508 if (!c_parser_get_builtin_args (parser,
7509 "__builtin_shuffle",
7510 &cexpr_list, false))
7512 expr.value = error_mark_node;
7513 break;
7516 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7517 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7519 if (vec_safe_length (cexpr_list) == 2)
7520 expr.value =
7521 c_build_vec_perm_expr
7522 (loc, (*cexpr_list)[0].value,
7523 NULL_TREE, (*cexpr_list)[1].value);
7525 else if (vec_safe_length (cexpr_list) == 3)
7526 expr.value =
7527 c_build_vec_perm_expr
7528 (loc, (*cexpr_list)[0].value,
7529 (*cexpr_list)[1].value,
7530 (*cexpr_list)[2].value);
7531 else
7533 error_at (loc, "wrong number of arguments to "
7534 "%<__builtin_shuffle%>");
7535 expr.value = error_mark_node;
7537 break;
7539 case RID_AT_SELECTOR:
7540 gcc_assert (c_dialect_objc ());
7541 c_parser_consume_token (parser);
7542 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7544 expr.value = error_mark_node;
7545 break;
7548 tree sel = c_parser_objc_selector_arg (parser);
7549 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7550 "expected %<)%>");
7551 expr.value = objc_build_selector_expr (loc, sel);
7553 break;
7554 case RID_AT_PROTOCOL:
7555 gcc_assert (c_dialect_objc ());
7556 c_parser_consume_token (parser);
7557 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7559 expr.value = error_mark_node;
7560 break;
7562 if (c_parser_next_token_is_not (parser, CPP_NAME))
7564 c_parser_error (parser, "expected identifier");
7565 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7566 expr.value = error_mark_node;
7567 break;
7570 tree id = c_parser_peek_token (parser)->value;
7571 c_parser_consume_token (parser);
7572 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7573 "expected %<)%>");
7574 expr.value = objc_build_protocol_expr (id);
7576 break;
7577 case RID_AT_ENCODE:
7578 /* Extension to support C-structures in the archiver. */
7579 gcc_assert (c_dialect_objc ());
7580 c_parser_consume_token (parser);
7581 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7583 expr.value = error_mark_node;
7584 break;
7586 t1 = c_parser_type_name (parser);
7587 if (t1 == NULL)
7589 expr.value = error_mark_node;
7590 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7591 break;
7593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7594 "expected %<)%>");
7596 tree type = groktypename (t1, NULL, NULL);
7597 expr.value = objc_build_encode_expr (type);
7599 break;
7600 case RID_GENERIC:
7601 expr = c_parser_generic_selection (parser);
7602 break;
7603 case RID_CILK_SPAWN:
7604 c_parser_consume_token (parser);
7605 if (!flag_cilkplus)
7607 error_at (loc, "-fcilkplus must be enabled to use "
7608 "%<_Cilk_spawn%>");
7609 expr = c_parser_postfix_expression (parser);
7610 expr.value = error_mark_node;
7612 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7614 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7615 "are not permitted");
7616 /* Now flush out all the _Cilk_spawns. */
7617 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7618 c_parser_consume_token (parser);
7619 expr = c_parser_postfix_expression (parser);
7621 else
7623 expr = c_parser_postfix_expression (parser);
7624 expr.value = build_cilk_spawn (loc, expr.value);
7626 break;
7627 default:
7628 c_parser_error (parser, "expected expression");
7629 expr.value = error_mark_node;
7630 break;
7632 break;
7633 case CPP_OPEN_SQUARE:
7634 if (c_dialect_objc ())
7636 tree receiver, args;
7637 c_parser_consume_token (parser);
7638 receiver = c_parser_objc_receiver (parser);
7639 args = c_parser_objc_message_args (parser);
7640 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7641 "expected %<]%>");
7642 expr.value = objc_build_message_expr (receiver, args);
7643 break;
7645 /* Else fall through to report error. */
7646 default:
7647 c_parser_error (parser, "expected expression");
7648 expr.value = error_mark_node;
7649 break;
7651 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7654 /* Parse a postfix expression after a parenthesized type name: the
7655 brace-enclosed initializer of a compound literal, possibly followed
7656 by some postfix operators. This is separate because it is not
7657 possible to tell until after the type name whether a cast
7658 expression has a cast or a compound literal, or whether the operand
7659 of sizeof is a parenthesized type name or starts with a compound
7660 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7661 location of the first token after the parentheses around the type
7662 name. */
7664 static struct c_expr
7665 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7666 struct c_type_name *type_name,
7667 location_t type_loc)
7669 tree type;
7670 struct c_expr init;
7671 bool non_const;
7672 struct c_expr expr;
7673 location_t start_loc;
7674 tree type_expr = NULL_TREE;
7675 bool type_expr_const = true;
7676 check_compound_literal_type (type_loc, type_name);
7677 start_init (NULL_TREE, NULL, 0);
7678 type = groktypename (type_name, &type_expr, &type_expr_const);
7679 start_loc = c_parser_peek_token (parser)->location;
7680 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7682 error_at (type_loc, "compound literal has variable size");
7683 type = error_mark_node;
7685 init = c_parser_braced_init (parser, type, false);
7686 finish_init ();
7687 maybe_warn_string_init (type_loc, type, init);
7689 if (type != error_mark_node
7690 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7691 && current_function_decl)
7693 error ("compound literal qualified by address-space qualifier");
7694 type = error_mark_node;
7697 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7698 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7699 ? CONSTRUCTOR_NON_CONST (init.value)
7700 : init.original_code == C_MAYBE_CONST_EXPR);
7701 non_const |= !type_expr_const;
7702 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7703 expr.original_code = ERROR_MARK;
7704 expr.original_type = NULL;
7705 if (type_expr)
7707 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7709 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7710 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7712 else
7714 gcc_assert (!non_const);
7715 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7716 type_expr, expr.value);
7719 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
7722 /* Callback function for sizeof_pointer_memaccess_warning to compare
7723 types. */
7725 static bool
7726 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
7728 return comptypes (type1, type2) == 1;
7731 /* Parse a postfix expression after the initial primary or compound
7732 literal; that is, parse a series of postfix operators.
7734 EXPR_LOC is the location of the primary expression. */
7736 static struct c_expr
7737 c_parser_postfix_expression_after_primary (c_parser *parser,
7738 location_t expr_loc,
7739 struct c_expr expr)
7741 struct c_expr orig_expr;
7742 tree ident, idx;
7743 location_t sizeof_arg_loc[3];
7744 tree sizeof_arg[3];
7745 unsigned int literal_zero_mask;
7746 unsigned int i;
7747 vec<tree, va_gc> *exprlist;
7748 vec<tree, va_gc> *origtypes = NULL;
7749 vec<location_t> arg_loc = vNULL;
7751 while (true)
7753 location_t op_loc = c_parser_peek_token (parser)->location;
7754 switch (c_parser_peek_token (parser)->type)
7756 case CPP_OPEN_SQUARE:
7757 /* Array reference. */
7758 c_parser_consume_token (parser);
7759 if (flag_cilkplus
7760 && c_parser_peek_token (parser)->type == CPP_COLON)
7761 /* If we are here, then we have something like this:
7762 Array [ : ]
7764 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
7765 expr.value);
7766 else
7768 idx = c_parser_expression (parser).value;
7769 /* Here we have 3 options:
7770 1. Array [EXPR] -- Normal Array call.
7771 2. Array [EXPR : EXPR] -- Array notation without stride.
7772 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
7774 For 1, we just handle it just like a normal array expression.
7775 For 2 and 3 we handle it like we handle array notations. The
7776 idx value we have above becomes the initial/start index.
7778 if (flag_cilkplus
7779 && c_parser_peek_token (parser)->type == CPP_COLON)
7780 expr.value = c_parser_array_notation (expr_loc, parser, idx,
7781 expr.value);
7782 else
7784 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7785 "expected %<]%>");
7786 expr.value = build_array_ref (op_loc, expr.value, idx);
7789 expr.original_code = ERROR_MARK;
7790 expr.original_type = NULL;
7791 break;
7792 case CPP_OPEN_PAREN:
7793 /* Function call. */
7794 c_parser_consume_token (parser);
7795 for (i = 0; i < 3; i++)
7797 sizeof_arg[i] = NULL_TREE;
7798 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
7800 literal_zero_mask = 0;
7801 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7802 exprlist = NULL;
7803 else
7804 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
7805 sizeof_arg_loc, sizeof_arg,
7806 &arg_loc, &literal_zero_mask);
7807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7808 "expected %<)%>");
7809 orig_expr = expr;
7810 mark_exp_read (expr.value);
7811 if (warn_sizeof_pointer_memaccess)
7812 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
7813 expr.value, exprlist,
7814 sizeof_arg,
7815 sizeof_ptr_memacc_comptypes);
7816 if (warn_memset_transposed_args
7817 && TREE_CODE (expr.value) == FUNCTION_DECL
7818 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
7819 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
7820 && vec_safe_length (exprlist) == 3
7821 && integer_zerop ((*exprlist)[2])
7822 && (literal_zero_mask & (1 << 2)) != 0
7823 && (!integer_zerop ((*exprlist)[1])
7824 || (literal_zero_mask & (1 << 1)) == 0))
7825 warning_at (expr_loc, OPT_Wmemset_transposed_args,
7826 "%<memset%> used with constant zero length parameter; "
7827 "this could be due to transposed parameters");
7829 expr.value
7830 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
7831 exprlist, origtypes);
7832 expr.original_code = ERROR_MARK;
7833 if (TREE_CODE (expr.value) == INTEGER_CST
7834 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
7835 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
7836 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
7837 expr.original_code = C_MAYBE_CONST_EXPR;
7838 expr.original_type = NULL;
7839 if (exprlist)
7841 release_tree_vector (exprlist);
7842 release_tree_vector (origtypes);
7844 arg_loc.release ();
7845 break;
7846 case CPP_DOT:
7847 /* Structure element reference. */
7848 c_parser_consume_token (parser);
7849 expr = default_function_array_conversion (expr_loc, expr);
7850 if (c_parser_next_token_is (parser, CPP_NAME))
7851 ident = c_parser_peek_token (parser)->value;
7852 else
7854 c_parser_error (parser, "expected identifier");
7855 expr.value = error_mark_node;
7856 expr.original_code = ERROR_MARK;
7857 expr.original_type = NULL;
7858 return expr;
7860 c_parser_consume_token (parser);
7861 expr.value = build_component_ref (op_loc, expr.value, ident);
7862 expr.original_code = ERROR_MARK;
7863 if (TREE_CODE (expr.value) != COMPONENT_REF)
7864 expr.original_type = NULL;
7865 else
7867 /* Remember the original type of a bitfield. */
7868 tree field = TREE_OPERAND (expr.value, 1);
7869 if (TREE_CODE (field) != FIELD_DECL)
7870 expr.original_type = NULL;
7871 else
7872 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7874 break;
7875 case CPP_DEREF:
7876 /* Structure element reference. */
7877 c_parser_consume_token (parser);
7878 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
7879 if (c_parser_next_token_is (parser, CPP_NAME))
7880 ident = c_parser_peek_token (parser)->value;
7881 else
7883 c_parser_error (parser, "expected identifier");
7884 expr.value = error_mark_node;
7885 expr.original_code = ERROR_MARK;
7886 expr.original_type = NULL;
7887 return expr;
7889 c_parser_consume_token (parser);
7890 expr.value = build_component_ref (op_loc,
7891 build_indirect_ref (op_loc,
7892 expr.value,
7893 RO_ARROW),
7894 ident);
7895 expr.original_code = ERROR_MARK;
7896 if (TREE_CODE (expr.value) != COMPONENT_REF)
7897 expr.original_type = NULL;
7898 else
7900 /* Remember the original type of a bitfield. */
7901 tree field = TREE_OPERAND (expr.value, 1);
7902 if (TREE_CODE (field) != FIELD_DECL)
7903 expr.original_type = NULL;
7904 else
7905 expr.original_type = DECL_BIT_FIELD_TYPE (field);
7907 break;
7908 case CPP_PLUS_PLUS:
7909 /* Postincrement. */
7910 c_parser_consume_token (parser);
7911 /* If the expressions have array notations, we expand them. */
7912 if (flag_cilkplus
7913 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7914 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
7915 else
7917 expr = default_function_array_read_conversion (expr_loc, expr);
7918 expr.value = build_unary_op (op_loc,
7919 POSTINCREMENT_EXPR, expr.value, 0);
7921 expr.original_code = ERROR_MARK;
7922 expr.original_type = NULL;
7923 break;
7924 case CPP_MINUS_MINUS:
7925 /* Postdecrement. */
7926 c_parser_consume_token (parser);
7927 /* If the expressions have array notations, we expand them. */
7928 if (flag_cilkplus
7929 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
7930 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
7931 else
7933 expr = default_function_array_read_conversion (expr_loc, expr);
7934 expr.value = build_unary_op (op_loc,
7935 POSTDECREMENT_EXPR, expr.value, 0);
7937 expr.original_code = ERROR_MARK;
7938 expr.original_type = NULL;
7939 break;
7940 default:
7941 return expr;
7946 /* Parse an expression (C90 6.3.17, C99 6.5.17).
7948 expression:
7949 assignment-expression
7950 expression , assignment-expression
7953 static struct c_expr
7954 c_parser_expression (c_parser *parser)
7956 location_t tloc = c_parser_peek_token (parser)->location;
7957 struct c_expr expr;
7958 expr = c_parser_expr_no_commas (parser, NULL);
7959 if (c_parser_next_token_is (parser, CPP_COMMA))
7960 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
7961 while (c_parser_next_token_is (parser, CPP_COMMA))
7963 struct c_expr next;
7964 tree lhsval;
7965 location_t loc = c_parser_peek_token (parser)->location;
7966 location_t expr_loc;
7967 c_parser_consume_token (parser);
7968 expr_loc = c_parser_peek_token (parser)->location;
7969 lhsval = expr.value;
7970 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
7971 lhsval = TREE_OPERAND (lhsval, 1);
7972 if (DECL_P (lhsval) || handled_component_p (lhsval))
7973 mark_exp_read (lhsval);
7974 next = c_parser_expr_no_commas (parser, NULL);
7975 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
7976 expr.value = build_compound_expr (loc, expr.value, next.value);
7977 expr.original_code = COMPOUND_EXPR;
7978 expr.original_type = next.original_type;
7980 return expr;
7983 /* Parse an expression and convert functions or arrays to pointers and
7984 lvalues to rvalues. */
7986 static struct c_expr
7987 c_parser_expression_conv (c_parser *parser)
7989 struct c_expr expr;
7990 location_t loc = c_parser_peek_token (parser)->location;
7991 expr = c_parser_expression (parser);
7992 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
7993 return expr;
7996 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
7997 argument is a literal zero alone and if so, set it in literal_zero_mask. */
7999 static inline void
8000 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8001 unsigned int idx)
8003 if (idx >= HOST_BITS_PER_INT)
8004 return;
8006 c_token *tok = c_parser_peek_token (parser);
8007 switch (tok->type)
8009 case CPP_NUMBER:
8010 case CPP_CHAR:
8011 case CPP_WCHAR:
8012 case CPP_CHAR16:
8013 case CPP_CHAR32:
8014 /* If a parameter is literal zero alone, remember it
8015 for -Wmemset-transposed-args warning. */
8016 if (integer_zerop (tok->value)
8017 && !TREE_OVERFLOW (tok->value)
8018 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8019 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8020 *literal_zero_mask |= 1U << idx;
8021 default:
8022 break;
8026 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8027 functions and arrays to pointers and lvalues to rvalues. If
8028 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8029 locations of function arguments into this vector.
8031 nonempty-expr-list:
8032 assignment-expression
8033 nonempty-expr-list , assignment-expression
8036 static vec<tree, va_gc> *
8037 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8038 vec<tree, va_gc> **p_orig_types,
8039 location_t *sizeof_arg_loc, tree *sizeof_arg,
8040 vec<location_t> *locations,
8041 unsigned int *literal_zero_mask)
8043 vec<tree, va_gc> *ret;
8044 vec<tree, va_gc> *orig_types;
8045 struct c_expr expr;
8046 location_t loc = c_parser_peek_token (parser)->location;
8047 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8048 unsigned int idx = 0;
8050 ret = make_tree_vector ();
8051 if (p_orig_types == NULL)
8052 orig_types = NULL;
8053 else
8054 orig_types = make_tree_vector ();
8056 if (sizeof_arg != NULL
8057 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8058 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8059 if (literal_zero_mask)
8060 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8061 expr = c_parser_expr_no_commas (parser, NULL);
8062 if (convert_p)
8063 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8064 if (fold_p)
8065 expr.value = c_fully_fold (expr.value, false, NULL);
8066 ret->quick_push (expr.value);
8067 if (orig_types)
8068 orig_types->quick_push (expr.original_type);
8069 if (locations)
8070 locations->safe_push (loc);
8071 if (sizeof_arg != NULL
8072 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8073 && expr.original_code == SIZEOF_EXPR)
8075 sizeof_arg[0] = c_last_sizeof_arg;
8076 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8078 while (c_parser_next_token_is (parser, CPP_COMMA))
8080 c_parser_consume_token (parser);
8081 loc = c_parser_peek_token (parser)->location;
8082 if (sizeof_arg != NULL
8083 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8084 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8085 else
8086 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8087 if (literal_zero_mask)
8088 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8089 expr = c_parser_expr_no_commas (parser, NULL);
8090 if (convert_p)
8091 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8092 if (fold_p)
8093 expr.value = c_fully_fold (expr.value, false, NULL);
8094 vec_safe_push (ret, expr.value);
8095 if (orig_types)
8096 vec_safe_push (orig_types, expr.original_type);
8097 if (locations)
8098 locations->safe_push (loc);
8099 if (++idx < 3
8100 && sizeof_arg != NULL
8101 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8102 && expr.original_code == SIZEOF_EXPR)
8104 sizeof_arg[idx] = c_last_sizeof_arg;
8105 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8108 if (orig_types)
8109 *p_orig_types = orig_types;
8110 return ret;
8113 /* Parse Objective-C-specific constructs. */
8115 /* Parse an objc-class-definition.
8117 objc-class-definition:
8118 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8119 objc-class-instance-variables[opt] objc-methodprotolist @end
8120 @implementation identifier objc-superclass[opt]
8121 objc-class-instance-variables[opt]
8122 @interface identifier ( identifier ) objc-protocol-refs[opt]
8123 objc-methodprotolist @end
8124 @interface identifier ( ) objc-protocol-refs[opt]
8125 objc-methodprotolist @end
8126 @implementation identifier ( identifier )
8128 objc-superclass:
8129 : identifier
8131 "@interface identifier (" must start "@interface identifier (
8132 identifier ) ...": objc-methodprotolist in the first production may
8133 not start with a parenthesized identifier as a declarator of a data
8134 definition with no declaration specifiers if the objc-superclass,
8135 objc-protocol-refs and objc-class-instance-variables are omitted. */
8137 static void
8138 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8140 bool iface_p;
8141 tree id1;
8142 tree superclass;
8143 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8144 iface_p = true;
8145 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8146 iface_p = false;
8147 else
8148 gcc_unreachable ();
8150 c_parser_consume_token (parser);
8151 if (c_parser_next_token_is_not (parser, CPP_NAME))
8153 c_parser_error (parser, "expected identifier");
8154 return;
8156 id1 = c_parser_peek_token (parser)->value;
8157 c_parser_consume_token (parser);
8158 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8160 /* We have a category or class extension. */
8161 tree id2;
8162 tree proto = NULL_TREE;
8163 c_parser_consume_token (parser);
8164 if (c_parser_next_token_is_not (parser, CPP_NAME))
8166 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8168 /* We have a class extension. */
8169 id2 = NULL_TREE;
8171 else
8173 c_parser_error (parser, "expected identifier or %<)%>");
8174 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8175 return;
8178 else
8180 id2 = c_parser_peek_token (parser)->value;
8181 c_parser_consume_token (parser);
8183 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8184 if (!iface_p)
8186 objc_start_category_implementation (id1, id2);
8187 return;
8189 if (c_parser_next_token_is (parser, CPP_LESS))
8190 proto = c_parser_objc_protocol_refs (parser);
8191 objc_start_category_interface (id1, id2, proto, attributes);
8192 c_parser_objc_methodprotolist (parser);
8193 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8194 objc_finish_interface ();
8195 return;
8197 if (c_parser_next_token_is (parser, CPP_COLON))
8199 c_parser_consume_token (parser);
8200 if (c_parser_next_token_is_not (parser, CPP_NAME))
8202 c_parser_error (parser, "expected identifier");
8203 return;
8205 superclass = c_parser_peek_token (parser)->value;
8206 c_parser_consume_token (parser);
8208 else
8209 superclass = NULL_TREE;
8210 if (iface_p)
8212 tree proto = NULL_TREE;
8213 if (c_parser_next_token_is (parser, CPP_LESS))
8214 proto = c_parser_objc_protocol_refs (parser);
8215 objc_start_class_interface (id1, superclass, proto, attributes);
8217 else
8218 objc_start_class_implementation (id1, superclass);
8219 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8220 c_parser_objc_class_instance_variables (parser);
8221 if (iface_p)
8223 objc_continue_interface ();
8224 c_parser_objc_methodprotolist (parser);
8225 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8226 objc_finish_interface ();
8228 else
8230 objc_continue_implementation ();
8231 return;
8235 /* Parse objc-class-instance-variables.
8237 objc-class-instance-variables:
8238 { objc-instance-variable-decl-list[opt] }
8240 objc-instance-variable-decl-list:
8241 objc-visibility-spec
8242 objc-instance-variable-decl ;
8244 objc-instance-variable-decl-list objc-visibility-spec
8245 objc-instance-variable-decl-list objc-instance-variable-decl ;
8246 objc-instance-variable-decl-list ;
8248 objc-visibility-spec:
8249 @private
8250 @protected
8251 @public
8253 objc-instance-variable-decl:
8254 struct-declaration
8257 static void
8258 c_parser_objc_class_instance_variables (c_parser *parser)
8260 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8261 c_parser_consume_token (parser);
8262 while (c_parser_next_token_is_not (parser, CPP_EOF))
8264 tree decls;
8265 /* Parse any stray semicolon. */
8266 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8268 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8269 "extra semicolon");
8270 c_parser_consume_token (parser);
8271 continue;
8273 /* Stop if at the end of the instance variables. */
8274 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8276 c_parser_consume_token (parser);
8277 break;
8279 /* Parse any objc-visibility-spec. */
8280 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8282 c_parser_consume_token (parser);
8283 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8284 continue;
8286 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8288 c_parser_consume_token (parser);
8289 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8290 continue;
8292 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8294 c_parser_consume_token (parser);
8295 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8296 continue;
8298 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8300 c_parser_consume_token (parser);
8301 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8302 continue;
8304 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8306 c_parser_pragma (parser, pragma_external);
8307 continue;
8310 /* Parse some comma-separated declarations. */
8311 decls = c_parser_struct_declaration (parser);
8312 if (decls == NULL)
8314 /* There is a syntax error. We want to skip the offending
8315 tokens up to the next ';' (included) or '}'
8316 (excluded). */
8318 /* First, skip manually a ')' or ']'. This is because they
8319 reduce the nesting level, so c_parser_skip_until_found()
8320 wouldn't be able to skip past them. */
8321 c_token *token = c_parser_peek_token (parser);
8322 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8323 c_parser_consume_token (parser);
8325 /* Then, do the standard skipping. */
8326 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8328 /* We hopefully recovered. Start normal parsing again. */
8329 parser->error = false;
8330 continue;
8332 else
8334 /* Comma-separated instance variables are chained together
8335 in reverse order; add them one by one. */
8336 tree ivar = nreverse (decls);
8337 for (; ivar; ivar = DECL_CHAIN (ivar))
8338 objc_add_instance_variable (copy_node (ivar));
8340 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8344 /* Parse an objc-class-declaration.
8346 objc-class-declaration:
8347 @class identifier-list ;
8350 static void
8351 c_parser_objc_class_declaration (c_parser *parser)
8353 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8354 c_parser_consume_token (parser);
8355 /* Any identifiers, including those declared as type names, are OK
8356 here. */
8357 while (true)
8359 tree id;
8360 if (c_parser_next_token_is_not (parser, CPP_NAME))
8362 c_parser_error (parser, "expected identifier");
8363 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8364 parser->error = false;
8365 return;
8367 id = c_parser_peek_token (parser)->value;
8368 objc_declare_class (id);
8369 c_parser_consume_token (parser);
8370 if (c_parser_next_token_is (parser, CPP_COMMA))
8371 c_parser_consume_token (parser);
8372 else
8373 break;
8375 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8378 /* Parse an objc-alias-declaration.
8380 objc-alias-declaration:
8381 @compatibility_alias identifier identifier ;
8384 static void
8385 c_parser_objc_alias_declaration (c_parser *parser)
8387 tree id1, id2;
8388 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8389 c_parser_consume_token (parser);
8390 if (c_parser_next_token_is_not (parser, CPP_NAME))
8392 c_parser_error (parser, "expected identifier");
8393 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8394 return;
8396 id1 = c_parser_peek_token (parser)->value;
8397 c_parser_consume_token (parser);
8398 if (c_parser_next_token_is_not (parser, CPP_NAME))
8400 c_parser_error (parser, "expected identifier");
8401 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8402 return;
8404 id2 = c_parser_peek_token (parser)->value;
8405 c_parser_consume_token (parser);
8406 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8407 objc_declare_alias (id1, id2);
8410 /* Parse an objc-protocol-definition.
8412 objc-protocol-definition:
8413 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8414 @protocol identifier-list ;
8416 "@protocol identifier ;" should be resolved as "@protocol
8417 identifier-list ;": objc-methodprotolist may not start with a
8418 semicolon in the first alternative if objc-protocol-refs are
8419 omitted. */
8421 static void
8422 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8424 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8426 c_parser_consume_token (parser);
8427 if (c_parser_next_token_is_not (parser, CPP_NAME))
8429 c_parser_error (parser, "expected identifier");
8430 return;
8432 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8433 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8435 /* Any identifiers, including those declared as type names, are
8436 OK here. */
8437 while (true)
8439 tree id;
8440 if (c_parser_next_token_is_not (parser, CPP_NAME))
8442 c_parser_error (parser, "expected identifier");
8443 break;
8445 id = c_parser_peek_token (parser)->value;
8446 objc_declare_protocol (id, attributes);
8447 c_parser_consume_token (parser);
8448 if (c_parser_next_token_is (parser, CPP_COMMA))
8449 c_parser_consume_token (parser);
8450 else
8451 break;
8453 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8455 else
8457 tree id = c_parser_peek_token (parser)->value;
8458 tree proto = NULL_TREE;
8459 c_parser_consume_token (parser);
8460 if (c_parser_next_token_is (parser, CPP_LESS))
8461 proto = c_parser_objc_protocol_refs (parser);
8462 parser->objc_pq_context = true;
8463 objc_start_protocol (id, proto, attributes);
8464 c_parser_objc_methodprotolist (parser);
8465 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8466 parser->objc_pq_context = false;
8467 objc_finish_interface ();
8471 /* Parse an objc-method-type.
8473 objc-method-type:
8477 Return true if it is a class method (+) and false if it is
8478 an instance method (-).
8480 static inline bool
8481 c_parser_objc_method_type (c_parser *parser)
8483 switch (c_parser_peek_token (parser)->type)
8485 case CPP_PLUS:
8486 c_parser_consume_token (parser);
8487 return true;
8488 case CPP_MINUS:
8489 c_parser_consume_token (parser);
8490 return false;
8491 default:
8492 gcc_unreachable ();
8496 /* Parse an objc-method-definition.
8498 objc-method-definition:
8499 objc-method-type objc-method-decl ;[opt] compound-statement
8502 static void
8503 c_parser_objc_method_definition (c_parser *parser)
8505 bool is_class_method = c_parser_objc_method_type (parser);
8506 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8507 parser->objc_pq_context = true;
8508 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8509 &expr);
8510 if (decl == error_mark_node)
8511 return; /* Bail here. */
8513 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8515 c_parser_consume_token (parser);
8516 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8517 "extra semicolon in method definition specified");
8520 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8522 c_parser_error (parser, "expected %<{%>");
8523 return;
8526 parser->objc_pq_context = false;
8527 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8529 add_stmt (c_parser_compound_statement (parser));
8530 objc_finish_method_definition (current_function_decl);
8532 else
8534 /* This code is executed when we find a method definition
8535 outside of an @implementation context (or invalid for other
8536 reasons). Parse the method (to keep going) but do not emit
8537 any code.
8539 c_parser_compound_statement (parser);
8543 /* Parse an objc-methodprotolist.
8545 objc-methodprotolist:
8546 empty
8547 objc-methodprotolist objc-methodproto
8548 objc-methodprotolist declaration
8549 objc-methodprotolist ;
8550 @optional
8551 @required
8553 The declaration is a data definition, which may be missing
8554 declaration specifiers under the same rules and diagnostics as
8555 other data definitions outside functions, and the stray semicolon
8556 is diagnosed the same way as a stray semicolon outside a
8557 function. */
8559 static void
8560 c_parser_objc_methodprotolist (c_parser *parser)
8562 while (true)
8564 /* The list is terminated by @end. */
8565 switch (c_parser_peek_token (parser)->type)
8567 case CPP_SEMICOLON:
8568 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8569 "ISO C does not allow extra %<;%> outside of a function");
8570 c_parser_consume_token (parser);
8571 break;
8572 case CPP_PLUS:
8573 case CPP_MINUS:
8574 c_parser_objc_methodproto (parser);
8575 break;
8576 case CPP_PRAGMA:
8577 c_parser_pragma (parser, pragma_external);
8578 break;
8579 case CPP_EOF:
8580 return;
8581 default:
8582 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8583 return;
8584 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8585 c_parser_objc_at_property_declaration (parser);
8586 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8588 objc_set_method_opt (true);
8589 c_parser_consume_token (parser);
8591 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8593 objc_set_method_opt (false);
8594 c_parser_consume_token (parser);
8596 else
8597 c_parser_declaration_or_fndef (parser, false, false, true,
8598 false, true, NULL, vNULL);
8599 break;
8604 /* Parse an objc-methodproto.
8606 objc-methodproto:
8607 objc-method-type objc-method-decl ;
8610 static void
8611 c_parser_objc_methodproto (c_parser *parser)
8613 bool is_class_method = c_parser_objc_method_type (parser);
8614 tree decl, attributes = NULL_TREE;
8616 /* Remember protocol qualifiers in prototypes. */
8617 parser->objc_pq_context = true;
8618 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8619 NULL);
8620 /* Forget protocol qualifiers now. */
8621 parser->objc_pq_context = false;
8623 /* Do not allow the presence of attributes to hide an erroneous
8624 method implementation in the interface section. */
8625 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8627 c_parser_error (parser, "expected %<;%>");
8628 return;
8631 if (decl != error_mark_node)
8632 objc_add_method_declaration (is_class_method, decl, attributes);
8634 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8637 /* If we are at a position that method attributes may be present, check that
8638 there are not any parsed already (a syntax error) and then collect any
8639 specified at the current location. Finally, if new attributes were present,
8640 check that the next token is legal ( ';' for decls and '{' for defs). */
8642 static bool
8643 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8645 bool bad = false;
8646 if (*attributes)
8648 c_parser_error (parser,
8649 "method attributes must be specified at the end only");
8650 *attributes = NULL_TREE;
8651 bad = true;
8654 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8655 *attributes = c_parser_attributes (parser);
8657 /* If there were no attributes here, just report any earlier error. */
8658 if (*attributes == NULL_TREE || bad)
8659 return bad;
8661 /* If the attributes are followed by a ; or {, then just report any earlier
8662 error. */
8663 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8664 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8665 return bad;
8667 /* We've got attributes, but not at the end. */
8668 c_parser_error (parser,
8669 "expected %<;%> or %<{%> after method attribute definition");
8670 return true;
8673 /* Parse an objc-method-decl.
8675 objc-method-decl:
8676 ( objc-type-name ) objc-selector
8677 objc-selector
8678 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8679 objc-keyword-selector objc-optparmlist
8680 attributes
8682 objc-keyword-selector:
8683 objc-keyword-decl
8684 objc-keyword-selector objc-keyword-decl
8686 objc-keyword-decl:
8687 objc-selector : ( objc-type-name ) identifier
8688 objc-selector : identifier
8689 : ( objc-type-name ) identifier
8690 : identifier
8692 objc-optparmlist:
8693 objc-optparms objc-optellipsis
8695 objc-optparms:
8696 empty
8697 objc-opt-parms , parameter-declaration
8699 objc-optellipsis:
8700 empty
8701 , ...
8704 static tree
8705 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
8706 tree *attributes, tree *expr)
8708 tree type = NULL_TREE;
8709 tree sel;
8710 tree parms = NULL_TREE;
8711 bool ellipsis = false;
8712 bool attr_err = false;
8714 *attributes = NULL_TREE;
8715 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8717 c_parser_consume_token (parser);
8718 type = c_parser_objc_type_name (parser);
8719 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8721 sel = c_parser_objc_selector (parser);
8722 /* If there is no selector, or a colon follows, we have an
8723 objc-keyword-selector. If there is a selector, and a colon does
8724 not follow, that selector ends the objc-method-decl. */
8725 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
8727 tree tsel = sel;
8728 tree list = NULL_TREE;
8729 while (true)
8731 tree atype = NULL_TREE, id, keyworddecl;
8732 tree param_attr = NULL_TREE;
8733 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
8734 break;
8735 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8737 c_parser_consume_token (parser);
8738 atype = c_parser_objc_type_name (parser);
8739 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8740 "expected %<)%>");
8742 /* New ObjC allows attributes on method parameters. */
8743 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8744 param_attr = c_parser_attributes (parser);
8745 if (c_parser_next_token_is_not (parser, CPP_NAME))
8747 c_parser_error (parser, "expected identifier");
8748 return error_mark_node;
8750 id = c_parser_peek_token (parser)->value;
8751 c_parser_consume_token (parser);
8752 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
8753 list = chainon (list, keyworddecl);
8754 tsel = c_parser_objc_selector (parser);
8755 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
8756 break;
8759 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8761 /* Parse the optional parameter list. Optional Objective-C
8762 method parameters follow the C syntax, and may include '...'
8763 to denote a variable number of arguments. */
8764 parms = make_node (TREE_LIST);
8765 while (c_parser_next_token_is (parser, CPP_COMMA))
8767 struct c_parm *parm;
8768 c_parser_consume_token (parser);
8769 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8771 ellipsis = true;
8772 c_parser_consume_token (parser);
8773 attr_err |= c_parser_objc_maybe_method_attributes
8774 (parser, attributes) ;
8775 break;
8777 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8778 if (parm == NULL)
8779 break;
8780 parms = chainon (parms,
8781 build_tree_list (NULL_TREE, grokparm (parm, expr)));
8783 sel = list;
8785 else
8786 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
8788 if (sel == NULL)
8790 c_parser_error (parser, "objective-c method declaration is expected");
8791 return error_mark_node;
8794 if (attr_err)
8795 return error_mark_node;
8797 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
8800 /* Parse an objc-type-name.
8802 objc-type-name:
8803 objc-type-qualifiers[opt] type-name
8804 objc-type-qualifiers[opt]
8806 objc-type-qualifiers:
8807 objc-type-qualifier
8808 objc-type-qualifiers objc-type-qualifier
8810 objc-type-qualifier: one of
8811 in out inout bycopy byref oneway
8814 static tree
8815 c_parser_objc_type_name (c_parser *parser)
8817 tree quals = NULL_TREE;
8818 struct c_type_name *type_name = NULL;
8819 tree type = NULL_TREE;
8820 while (true)
8822 c_token *token = c_parser_peek_token (parser);
8823 if (token->type == CPP_KEYWORD
8824 && (token->keyword == RID_IN
8825 || token->keyword == RID_OUT
8826 || token->keyword == RID_INOUT
8827 || token->keyword == RID_BYCOPY
8828 || token->keyword == RID_BYREF
8829 || token->keyword == RID_ONEWAY))
8831 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
8832 c_parser_consume_token (parser);
8834 else
8835 break;
8837 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
8838 type_name = c_parser_type_name (parser);
8839 if (type_name)
8840 type = groktypename (type_name, NULL, NULL);
8842 /* If the type is unknown, and error has already been produced and
8843 we need to recover from the error. In that case, use NULL_TREE
8844 for the type, as if no type had been specified; this will use the
8845 default type ('id') which is good for error recovery. */
8846 if (type == error_mark_node)
8847 type = NULL_TREE;
8849 return build_tree_list (quals, type);
8852 /* Parse objc-protocol-refs.
8854 objc-protocol-refs:
8855 < identifier-list >
8858 static tree
8859 c_parser_objc_protocol_refs (c_parser *parser)
8861 tree list = NULL_TREE;
8862 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
8863 c_parser_consume_token (parser);
8864 /* Any identifiers, including those declared as type names, are OK
8865 here. */
8866 while (true)
8868 tree id;
8869 if (c_parser_next_token_is_not (parser, CPP_NAME))
8871 c_parser_error (parser, "expected identifier");
8872 break;
8874 id = c_parser_peek_token (parser)->value;
8875 list = chainon (list, build_tree_list (NULL_TREE, id));
8876 c_parser_consume_token (parser);
8877 if (c_parser_next_token_is (parser, CPP_COMMA))
8878 c_parser_consume_token (parser);
8879 else
8880 break;
8882 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
8883 return list;
8886 /* Parse an objc-try-catch-finally-statement.
8888 objc-try-catch-finally-statement:
8889 @try compound-statement objc-catch-list[opt]
8890 @try compound-statement objc-catch-list[opt] @finally compound-statement
8892 objc-catch-list:
8893 @catch ( objc-catch-parameter-declaration ) compound-statement
8894 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
8896 objc-catch-parameter-declaration:
8897 parameter-declaration
8898 '...'
8900 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
8902 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
8903 for C++. Keep them in sync. */
8905 static void
8906 c_parser_objc_try_catch_finally_statement (c_parser *parser)
8908 location_t location;
8909 tree stmt;
8911 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
8912 c_parser_consume_token (parser);
8913 location = c_parser_peek_token (parser)->location;
8914 objc_maybe_warn_exceptions (location);
8915 stmt = c_parser_compound_statement (parser);
8916 objc_begin_try_stmt (location, stmt);
8918 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
8920 struct c_parm *parm;
8921 tree parameter_declaration = error_mark_node;
8922 bool seen_open_paren = false;
8924 c_parser_consume_token (parser);
8925 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8926 seen_open_paren = true;
8927 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
8929 /* We have "@catch (...)" (where the '...' are literally
8930 what is in the code). Skip the '...'.
8931 parameter_declaration is set to NULL_TREE, and
8932 objc_being_catch_clauses() knows that that means
8933 '...'. */
8934 c_parser_consume_token (parser);
8935 parameter_declaration = NULL_TREE;
8937 else
8939 /* We have "@catch (NSException *exception)" or something
8940 like that. Parse the parameter declaration. */
8941 parm = c_parser_parameter_declaration (parser, NULL_TREE);
8942 if (parm == NULL)
8943 parameter_declaration = error_mark_node;
8944 else
8945 parameter_declaration = grokparm (parm, NULL);
8947 if (seen_open_paren)
8948 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8949 else
8951 /* If there was no open parenthesis, we are recovering from
8952 an error, and we are trying to figure out what mistake
8953 the user has made. */
8955 /* If there is an immediate closing parenthesis, the user
8956 probably forgot the opening one (ie, they typed "@catch
8957 NSException *e)". Parse the closing parenthesis and keep
8958 going. */
8959 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8960 c_parser_consume_token (parser);
8962 /* If these is no immediate closing parenthesis, the user
8963 probably doesn't know that parenthesis are required at
8964 all (ie, they typed "@catch NSException *e"). So, just
8965 forget about the closing parenthesis and keep going. */
8967 objc_begin_catch_clause (parameter_declaration);
8968 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
8969 c_parser_compound_statement_nostart (parser);
8970 objc_finish_catch_clause ();
8972 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
8974 c_parser_consume_token (parser);
8975 location = c_parser_peek_token (parser)->location;
8976 stmt = c_parser_compound_statement (parser);
8977 objc_build_finally_clause (location, stmt);
8979 objc_finish_try_stmt ();
8982 /* Parse an objc-synchronized-statement.
8984 objc-synchronized-statement:
8985 @synchronized ( expression ) compound-statement
8988 static void
8989 c_parser_objc_synchronized_statement (c_parser *parser)
8991 location_t loc;
8992 tree expr, stmt;
8993 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
8994 c_parser_consume_token (parser);
8995 loc = c_parser_peek_token (parser)->location;
8996 objc_maybe_warn_exceptions (loc);
8997 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8999 struct c_expr ce = c_parser_expression (parser);
9000 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9001 expr = ce.value;
9002 expr = c_fully_fold (expr, false, NULL);
9003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9005 else
9006 expr = error_mark_node;
9007 stmt = c_parser_compound_statement (parser);
9008 objc_build_synchronized (loc, expr, stmt);
9011 /* Parse an objc-selector; return NULL_TREE without an error if the
9012 next token is not an objc-selector.
9014 objc-selector:
9015 identifier
9016 one of
9017 enum struct union if else while do for switch case default
9018 break continue return goto asm sizeof typeof __alignof
9019 unsigned long const short volatile signed restrict _Complex
9020 in out inout bycopy byref oneway int char float double void _Bool
9021 _Atomic
9023 ??? Why this selection of keywords but not, for example, storage
9024 class specifiers? */
9026 static tree
9027 c_parser_objc_selector (c_parser *parser)
9029 c_token *token = c_parser_peek_token (parser);
9030 tree value = token->value;
9031 if (token->type == CPP_NAME)
9033 c_parser_consume_token (parser);
9034 return value;
9036 if (token->type != CPP_KEYWORD)
9037 return NULL_TREE;
9038 switch (token->keyword)
9040 case RID_ENUM:
9041 case RID_STRUCT:
9042 case RID_UNION:
9043 case RID_IF:
9044 case RID_ELSE:
9045 case RID_WHILE:
9046 case RID_DO:
9047 case RID_FOR:
9048 case RID_SWITCH:
9049 case RID_CASE:
9050 case RID_DEFAULT:
9051 case RID_BREAK:
9052 case RID_CONTINUE:
9053 case RID_RETURN:
9054 case RID_GOTO:
9055 case RID_ASM:
9056 case RID_SIZEOF:
9057 case RID_TYPEOF:
9058 case RID_ALIGNOF:
9059 case RID_UNSIGNED:
9060 case RID_LONG:
9061 case RID_CONST:
9062 case RID_SHORT:
9063 case RID_VOLATILE:
9064 case RID_SIGNED:
9065 case RID_RESTRICT:
9066 case RID_COMPLEX:
9067 case RID_IN:
9068 case RID_OUT:
9069 case RID_INOUT:
9070 case RID_BYCOPY:
9071 case RID_BYREF:
9072 case RID_ONEWAY:
9073 case RID_INT:
9074 case RID_CHAR:
9075 case RID_FLOAT:
9076 case RID_DOUBLE:
9077 case RID_VOID:
9078 case RID_BOOL:
9079 case RID_ATOMIC:
9080 case RID_AUTO_TYPE:
9081 case RID_INT_N_0:
9082 case RID_INT_N_1:
9083 case RID_INT_N_2:
9084 case RID_INT_N_3:
9085 c_parser_consume_token (parser);
9086 return value;
9087 default:
9088 return NULL_TREE;
9092 /* Parse an objc-selector-arg.
9094 objc-selector-arg:
9095 objc-selector
9096 objc-keywordname-list
9098 objc-keywordname-list:
9099 objc-keywordname
9100 objc-keywordname-list objc-keywordname
9102 objc-keywordname:
9103 objc-selector :
9107 static tree
9108 c_parser_objc_selector_arg (c_parser *parser)
9110 tree sel = c_parser_objc_selector (parser);
9111 tree list = NULL_TREE;
9112 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9113 return sel;
9114 while (true)
9116 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9117 return list;
9118 list = chainon (list, build_tree_list (sel, NULL_TREE));
9119 sel = c_parser_objc_selector (parser);
9120 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9121 break;
9123 return list;
9126 /* Parse an objc-receiver.
9128 objc-receiver:
9129 expression
9130 class-name
9131 type-name
9134 static tree
9135 c_parser_objc_receiver (c_parser *parser)
9137 location_t loc = c_parser_peek_token (parser)->location;
9139 if (c_parser_peek_token (parser)->type == CPP_NAME
9140 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9141 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9143 tree id = c_parser_peek_token (parser)->value;
9144 c_parser_consume_token (parser);
9145 return objc_get_class_reference (id);
9147 struct c_expr ce = c_parser_expression (parser);
9148 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9149 return c_fully_fold (ce.value, false, NULL);
9152 /* Parse objc-message-args.
9154 objc-message-args:
9155 objc-selector
9156 objc-keywordarg-list
9158 objc-keywordarg-list:
9159 objc-keywordarg
9160 objc-keywordarg-list objc-keywordarg
9162 objc-keywordarg:
9163 objc-selector : objc-keywordexpr
9164 : objc-keywordexpr
9167 static tree
9168 c_parser_objc_message_args (c_parser *parser)
9170 tree sel = c_parser_objc_selector (parser);
9171 tree list = NULL_TREE;
9172 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9173 return sel;
9174 while (true)
9176 tree keywordexpr;
9177 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9178 return error_mark_node;
9179 keywordexpr = c_parser_objc_keywordexpr (parser);
9180 list = chainon (list, build_tree_list (sel, keywordexpr));
9181 sel = c_parser_objc_selector (parser);
9182 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9183 break;
9185 return list;
9188 /* Parse an objc-keywordexpr.
9190 objc-keywordexpr:
9191 nonempty-expr-list
9194 static tree
9195 c_parser_objc_keywordexpr (c_parser *parser)
9197 tree ret;
9198 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9199 NULL, NULL, NULL, NULL);
9200 if (vec_safe_length (expr_list) == 1)
9202 /* Just return the expression, remove a level of
9203 indirection. */
9204 ret = (*expr_list)[0];
9206 else
9208 /* We have a comma expression, we will collapse later. */
9209 ret = build_tree_list_vec (expr_list);
9211 release_tree_vector (expr_list);
9212 return ret;
9215 /* A check, needed in several places, that ObjC interface, implementation or
9216 method definitions are not prefixed by incorrect items. */
9217 static bool
9218 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9219 struct c_declspecs *specs)
9221 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9222 || specs->typespec_kind != ctsk_none)
9224 c_parser_error (parser,
9225 "no type or storage class may be specified here,");
9226 c_parser_skip_to_end_of_block_or_statement (parser);
9227 return true;
9229 return false;
9232 /* Parse an Objective-C @property declaration. The syntax is:
9234 objc-property-declaration:
9235 '@property' objc-property-attributes[opt] struct-declaration ;
9237 objc-property-attributes:
9238 '(' objc-property-attribute-list ')'
9240 objc-property-attribute-list:
9241 objc-property-attribute
9242 objc-property-attribute-list, objc-property-attribute
9244 objc-property-attribute
9245 'getter' = identifier
9246 'setter' = identifier
9247 'readonly'
9248 'readwrite'
9249 'assign'
9250 'retain'
9251 'copy'
9252 'nonatomic'
9254 For example:
9255 @property NSString *name;
9256 @property (readonly) id object;
9257 @property (retain, nonatomic, getter=getTheName) id name;
9258 @property int a, b, c;
9260 PS: This function is identical to cp_parser_objc_at_propery_declaration
9261 for C++. Keep them in sync. */
9262 static void
9263 c_parser_objc_at_property_declaration (c_parser *parser)
9265 /* The following variables hold the attributes of the properties as
9266 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9267 seen. When we see an attribute, we set them to 'true' (if they
9268 are boolean properties) or to the identifier (if they have an
9269 argument, ie, for getter and setter). Note that here we only
9270 parse the list of attributes, check the syntax and accumulate the
9271 attributes that we find. objc_add_property_declaration() will
9272 then process the information. */
9273 bool property_assign = false;
9274 bool property_copy = false;
9275 tree property_getter_ident = NULL_TREE;
9276 bool property_nonatomic = false;
9277 bool property_readonly = false;
9278 bool property_readwrite = false;
9279 bool property_retain = false;
9280 tree property_setter_ident = NULL_TREE;
9282 /* 'properties' is the list of properties that we read. Usually a
9283 single one, but maybe more (eg, in "@property int a, b, c;" there
9284 are three). */
9285 tree properties;
9286 location_t loc;
9288 loc = c_parser_peek_token (parser)->location;
9289 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9291 c_parser_consume_token (parser); /* Eat '@property'. */
9293 /* Parse the optional attribute list... */
9294 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9296 /* Eat the '(' */
9297 c_parser_consume_token (parser);
9299 /* Property attribute keywords are valid now. */
9300 parser->objc_property_attr_context = true;
9302 while (true)
9304 bool syntax_error = false;
9305 c_token *token = c_parser_peek_token (parser);
9306 enum rid keyword;
9308 if (token->type != CPP_KEYWORD)
9310 if (token->type == CPP_CLOSE_PAREN)
9311 c_parser_error (parser, "expected identifier");
9312 else
9314 c_parser_consume_token (parser);
9315 c_parser_error (parser, "unknown property attribute");
9317 break;
9319 keyword = token->keyword;
9320 c_parser_consume_token (parser);
9321 switch (keyword)
9323 case RID_ASSIGN: property_assign = true; break;
9324 case RID_COPY: property_copy = true; break;
9325 case RID_NONATOMIC: property_nonatomic = true; break;
9326 case RID_READONLY: property_readonly = true; break;
9327 case RID_READWRITE: property_readwrite = true; break;
9328 case RID_RETAIN: property_retain = true; break;
9330 case RID_GETTER:
9331 case RID_SETTER:
9332 if (c_parser_next_token_is_not (parser, CPP_EQ))
9334 if (keyword == RID_GETTER)
9335 c_parser_error (parser,
9336 "missing %<=%> (after %<getter%> attribute)");
9337 else
9338 c_parser_error (parser,
9339 "missing %<=%> (after %<setter%> attribute)");
9340 syntax_error = true;
9341 break;
9343 c_parser_consume_token (parser); /* eat the = */
9344 if (c_parser_next_token_is_not (parser, CPP_NAME))
9346 c_parser_error (parser, "expected identifier");
9347 syntax_error = true;
9348 break;
9350 if (keyword == RID_SETTER)
9352 if (property_setter_ident != NULL_TREE)
9353 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9354 else
9355 property_setter_ident = c_parser_peek_token (parser)->value;
9356 c_parser_consume_token (parser);
9357 if (c_parser_next_token_is_not (parser, CPP_COLON))
9358 c_parser_error (parser, "setter name must terminate with %<:%>");
9359 else
9360 c_parser_consume_token (parser);
9362 else
9364 if (property_getter_ident != NULL_TREE)
9365 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9366 else
9367 property_getter_ident = c_parser_peek_token (parser)->value;
9368 c_parser_consume_token (parser);
9370 break;
9371 default:
9372 c_parser_error (parser, "unknown property attribute");
9373 syntax_error = true;
9374 break;
9377 if (syntax_error)
9378 break;
9380 if (c_parser_next_token_is (parser, CPP_COMMA))
9381 c_parser_consume_token (parser);
9382 else
9383 break;
9385 parser->objc_property_attr_context = false;
9386 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9388 /* ... and the property declaration(s). */
9389 properties = c_parser_struct_declaration (parser);
9391 if (properties == error_mark_node)
9393 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9394 parser->error = false;
9395 return;
9398 if (properties == NULL_TREE)
9399 c_parser_error (parser, "expected identifier");
9400 else
9402 /* Comma-separated properties are chained together in
9403 reverse order; add them one by one. */
9404 properties = nreverse (properties);
9406 for (; properties; properties = TREE_CHAIN (properties))
9407 objc_add_property_declaration (loc, copy_node (properties),
9408 property_readonly, property_readwrite,
9409 property_assign, property_retain,
9410 property_copy, property_nonatomic,
9411 property_getter_ident, property_setter_ident);
9414 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9415 parser->error = false;
9418 /* Parse an Objective-C @synthesize declaration. The syntax is:
9420 objc-synthesize-declaration:
9421 @synthesize objc-synthesize-identifier-list ;
9423 objc-synthesize-identifier-list:
9424 objc-synthesize-identifier
9425 objc-synthesize-identifier-list, objc-synthesize-identifier
9427 objc-synthesize-identifier
9428 identifier
9429 identifier = identifier
9431 For example:
9432 @synthesize MyProperty;
9433 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9435 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9436 for C++. Keep them in sync.
9438 static void
9439 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9441 tree list = NULL_TREE;
9442 location_t loc;
9443 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9444 loc = c_parser_peek_token (parser)->location;
9446 c_parser_consume_token (parser);
9447 while (true)
9449 tree property, ivar;
9450 if (c_parser_next_token_is_not (parser, CPP_NAME))
9452 c_parser_error (parser, "expected identifier");
9453 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9454 /* Once we find the semicolon, we can resume normal parsing.
9455 We have to reset parser->error manually because
9456 c_parser_skip_until_found() won't reset it for us if the
9457 next token is precisely a semicolon. */
9458 parser->error = false;
9459 return;
9461 property = c_parser_peek_token (parser)->value;
9462 c_parser_consume_token (parser);
9463 if (c_parser_next_token_is (parser, CPP_EQ))
9465 c_parser_consume_token (parser);
9466 if (c_parser_next_token_is_not (parser, CPP_NAME))
9468 c_parser_error (parser, "expected identifier");
9469 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9470 parser->error = false;
9471 return;
9473 ivar = c_parser_peek_token (parser)->value;
9474 c_parser_consume_token (parser);
9476 else
9477 ivar = NULL_TREE;
9478 list = chainon (list, build_tree_list (ivar, property));
9479 if (c_parser_next_token_is (parser, CPP_COMMA))
9480 c_parser_consume_token (parser);
9481 else
9482 break;
9484 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9485 objc_add_synthesize_declaration (loc, list);
9488 /* Parse an Objective-C @dynamic declaration. The syntax is:
9490 objc-dynamic-declaration:
9491 @dynamic identifier-list ;
9493 For example:
9494 @dynamic MyProperty;
9495 @dynamic MyProperty, AnotherProperty;
9497 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9498 for C++. Keep them in sync.
9500 static void
9501 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9503 tree list = NULL_TREE;
9504 location_t loc;
9505 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9506 loc = c_parser_peek_token (parser)->location;
9508 c_parser_consume_token (parser);
9509 while (true)
9511 tree property;
9512 if (c_parser_next_token_is_not (parser, CPP_NAME))
9514 c_parser_error (parser, "expected identifier");
9515 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9516 parser->error = false;
9517 return;
9519 property = c_parser_peek_token (parser)->value;
9520 list = chainon (list, build_tree_list (NULL_TREE, property));
9521 c_parser_consume_token (parser);
9522 if (c_parser_next_token_is (parser, CPP_COMMA))
9523 c_parser_consume_token (parser);
9524 else
9525 break;
9527 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9528 objc_add_dynamic_declaration (loc, list);
9532 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9533 should be considered, statements. ALLOW_STMT is true if we're within
9534 the context of a function and such pragmas are to be allowed. Returns
9535 true if we actually parsed such a pragma. */
9537 static bool
9538 c_parser_pragma (c_parser *parser, enum pragma_context context)
9540 unsigned int id;
9542 id = c_parser_peek_token (parser)->pragma_kind;
9543 gcc_assert (id != PRAGMA_NONE);
9545 switch (id)
9547 case PRAGMA_OACC_UPDATE:
9548 if (context != pragma_compound)
9550 if (context == pragma_stmt)
9551 c_parser_error (parser, "%<#pragma acc update%> may only be "
9552 "used in compound statements");
9553 goto bad_stmt;
9555 c_parser_oacc_update (parser);
9556 return false;
9558 case PRAGMA_OMP_BARRIER:
9559 if (context != pragma_compound)
9561 if (context == pragma_stmt)
9562 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9563 "used in compound statements");
9564 goto bad_stmt;
9566 c_parser_omp_barrier (parser);
9567 return false;
9569 case PRAGMA_OMP_FLUSH:
9570 if (context != pragma_compound)
9572 if (context == pragma_stmt)
9573 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9574 "used in compound statements");
9575 goto bad_stmt;
9577 c_parser_omp_flush (parser);
9578 return false;
9580 case PRAGMA_OMP_TASKWAIT:
9581 if (context != pragma_compound)
9583 if (context == pragma_stmt)
9584 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9585 "used in compound statements");
9586 goto bad_stmt;
9588 c_parser_omp_taskwait (parser);
9589 return false;
9591 case PRAGMA_OMP_TASKYIELD:
9592 if (context != pragma_compound)
9594 if (context == pragma_stmt)
9595 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9596 "used in compound statements");
9597 goto bad_stmt;
9599 c_parser_omp_taskyield (parser);
9600 return false;
9602 case PRAGMA_OMP_CANCEL:
9603 if (context != pragma_compound)
9605 if (context == pragma_stmt)
9606 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9607 "used in compound statements");
9608 goto bad_stmt;
9610 c_parser_omp_cancel (parser);
9611 return false;
9613 case PRAGMA_OMP_CANCELLATION_POINT:
9614 if (context != pragma_compound)
9616 if (context == pragma_stmt)
9617 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9618 "only be used in compound statements");
9619 goto bad_stmt;
9621 c_parser_omp_cancellation_point (parser);
9622 return false;
9624 case PRAGMA_OMP_THREADPRIVATE:
9625 c_parser_omp_threadprivate (parser);
9626 return false;
9628 case PRAGMA_OMP_TARGET:
9629 return c_parser_omp_target (parser, context);
9631 case PRAGMA_OMP_END_DECLARE_TARGET:
9632 c_parser_omp_end_declare_target (parser);
9633 return false;
9635 case PRAGMA_OMP_SECTION:
9636 error_at (c_parser_peek_token (parser)->location,
9637 "%<#pragma omp section%> may only be used in "
9638 "%<#pragma omp sections%> construct");
9639 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9640 return false;
9642 case PRAGMA_OMP_DECLARE_REDUCTION:
9643 c_parser_omp_declare (parser, context);
9644 return false;
9645 case PRAGMA_IVDEP:
9646 c_parser_consume_pragma (parser);
9647 c_parser_skip_to_pragma_eol (parser);
9648 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9649 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9650 && !c_parser_next_token_is_keyword (parser, RID_DO))
9652 c_parser_error (parser, "for, while or do statement expected");
9653 return false;
9655 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9656 c_parser_for_statement (parser, true);
9657 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9658 c_parser_while_statement (parser, true);
9659 else
9660 c_parser_do_statement (parser, true);
9661 return false;
9663 case PRAGMA_GCC_PCH_PREPROCESS:
9664 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9665 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9666 return false;
9668 case PRAGMA_CILK_SIMD:
9669 if (!c_parser_cilk_verify_simd (parser, context))
9670 return false;
9671 c_parser_consume_pragma (parser);
9672 c_parser_cilk_simd (parser);
9673 return false;
9674 case PRAGMA_CILK_GRAINSIZE:
9675 if (!flag_cilkplus)
9677 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
9678 " enabled");
9679 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9680 return false;
9682 if (context == pragma_external)
9684 error_at (c_parser_peek_token (parser)->location,
9685 "%<#pragma grainsize%> must be inside a function");
9686 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9687 return false;
9689 c_parser_cilk_grainsize (parser);
9690 return false;
9692 default:
9693 if (id < PRAGMA_FIRST_EXTERNAL)
9695 if (context != pragma_stmt && context != pragma_compound)
9697 bad_stmt:
9698 c_parser_error (parser, "expected declaration specifiers");
9699 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9700 return false;
9702 c_parser_omp_construct (parser);
9703 return true;
9705 break;
9708 c_parser_consume_pragma (parser);
9709 c_invoke_pragma_handler (id);
9711 /* Skip to EOL, but suppress any error message. Those will have been
9712 generated by the handler routine through calling error, as opposed
9713 to calling c_parser_error. */
9714 parser->error = true;
9715 c_parser_skip_to_pragma_eol (parser);
9717 return false;
9720 /* The interface the pragma parsers have to the lexer. */
9722 enum cpp_ttype
9723 pragma_lex (tree *value)
9725 c_token *tok = c_parser_peek_token (the_parser);
9726 enum cpp_ttype ret = tok->type;
9728 *value = tok->value;
9729 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
9730 ret = CPP_EOF;
9731 else
9733 if (ret == CPP_KEYWORD)
9734 ret = CPP_NAME;
9735 c_parser_consume_token (the_parser);
9738 return ret;
9741 static void
9742 c_parser_pragma_pch_preprocess (c_parser *parser)
9744 tree name = NULL;
9746 c_parser_consume_pragma (parser);
9747 if (c_parser_next_token_is (parser, CPP_STRING))
9749 name = c_parser_peek_token (parser)->value;
9750 c_parser_consume_token (parser);
9752 else
9753 c_parser_error (parser, "expected string literal");
9754 c_parser_skip_to_pragma_eol (parser);
9756 if (name)
9757 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
9760 /* OpenACC and OpenMP parsing routines. */
9762 /* Returns name of the next clause.
9763 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
9764 the token is not consumed. Otherwise appropriate pragma_omp_clause is
9765 returned and the token is consumed. */
9767 static pragma_omp_clause
9768 c_parser_omp_clause_name (c_parser *parser)
9770 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
9772 if (c_parser_next_token_is_keyword (parser, RID_IF))
9773 result = PRAGMA_OMP_CLAUSE_IF;
9774 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
9775 result = PRAGMA_OMP_CLAUSE_DEFAULT;
9776 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
9777 result = PRAGMA_OMP_CLAUSE_FOR;
9778 else if (c_parser_next_token_is (parser, CPP_NAME))
9780 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
9782 switch (p[0])
9784 case 'a':
9785 if (!strcmp ("aligned", p))
9786 result = PRAGMA_OMP_CLAUSE_ALIGNED;
9787 else if (!strcmp ("async", p))
9788 result = PRAGMA_OMP_CLAUSE_ASYNC;
9789 break;
9790 case 'c':
9791 if (!strcmp ("collapse", p))
9792 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
9793 else if (!strcmp ("copy", p))
9794 result = PRAGMA_OMP_CLAUSE_COPY;
9795 else if (!strcmp ("copyin", p))
9796 result = PRAGMA_OMP_CLAUSE_COPYIN;
9797 else if (!strcmp ("copyout", p))
9798 result = PRAGMA_OMP_CLAUSE_COPYOUT;
9799 else if (!strcmp ("copyprivate", p))
9800 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
9801 else if (!strcmp ("create", p))
9802 result = PRAGMA_OMP_CLAUSE_CREATE;
9803 break;
9804 case 'd':
9805 if (!strcmp ("delete", p))
9806 result = PRAGMA_OMP_CLAUSE_DELETE;
9807 else if (!strcmp ("depend", p))
9808 result = PRAGMA_OMP_CLAUSE_DEPEND;
9809 else if (!strcmp ("device", p))
9810 result = PRAGMA_OMP_CLAUSE_DEVICE;
9811 else if (!strcmp ("deviceptr", p))
9812 result = PRAGMA_OMP_CLAUSE_DEVICEPTR;
9813 else if (!strcmp ("dist_schedule", p))
9814 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
9815 break;
9816 case 'f':
9817 if (!strcmp ("final", p))
9818 result = PRAGMA_OMP_CLAUSE_FINAL;
9819 else if (!strcmp ("firstprivate", p))
9820 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
9821 else if (!strcmp ("from", p))
9822 result = PRAGMA_OMP_CLAUSE_FROM;
9823 break;
9824 case 'h':
9825 if (!strcmp ("host", p))
9826 result = PRAGMA_OMP_CLAUSE_SELF;
9827 break;
9828 case 'i':
9829 if (!strcmp ("inbranch", p))
9830 result = PRAGMA_OMP_CLAUSE_INBRANCH;
9831 break;
9832 case 'l':
9833 if (!strcmp ("lastprivate", p))
9834 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
9835 else if (!strcmp ("linear", p))
9836 result = PRAGMA_OMP_CLAUSE_LINEAR;
9837 break;
9838 case 'm':
9839 if (!strcmp ("map", p))
9840 result = PRAGMA_OMP_CLAUSE_MAP;
9841 else if (!strcmp ("mergeable", p))
9842 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
9843 else if (flag_cilkplus && !strcmp ("mask", p))
9844 result = PRAGMA_CILK_CLAUSE_MASK;
9845 break;
9846 case 'n':
9847 if (!strcmp ("notinbranch", p))
9848 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
9849 else if (!strcmp ("nowait", p))
9850 result = PRAGMA_OMP_CLAUSE_NOWAIT;
9851 else if (!strcmp ("num_gangs", p))
9852 result = PRAGMA_OMP_CLAUSE_NUM_GANGS;
9853 else if (!strcmp ("num_teams", p))
9854 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
9855 else if (!strcmp ("num_threads", p))
9856 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
9857 else if (!strcmp ("num_workers", p))
9858 result = PRAGMA_OMP_CLAUSE_NUM_WORKERS;
9859 else if (flag_cilkplus && !strcmp ("nomask", p))
9860 result = PRAGMA_CILK_CLAUSE_NOMASK;
9861 break;
9862 case 'o':
9863 if (!strcmp ("ordered", p))
9864 result = PRAGMA_OMP_CLAUSE_ORDERED;
9865 break;
9866 case 'p':
9867 if (!strcmp ("parallel", p))
9868 result = PRAGMA_OMP_CLAUSE_PARALLEL;
9869 else if (!strcmp ("present", p))
9870 result = PRAGMA_OMP_CLAUSE_PRESENT;
9871 else if (!strcmp ("present_or_copy", p)
9872 || !strcmp ("pcopy", p))
9873 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY;
9874 else if (!strcmp ("present_or_copyin", p)
9875 || !strcmp ("pcopyin", p))
9876 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN;
9877 else if (!strcmp ("present_or_copyout", p)
9878 || !strcmp ("pcopyout", p))
9879 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT;
9880 else if (!strcmp ("present_or_create", p)
9881 || !strcmp ("pcreate", p))
9882 result = PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE;
9883 else if (!strcmp ("private", p))
9884 result = PRAGMA_OMP_CLAUSE_PRIVATE;
9885 else if (!strcmp ("proc_bind", p))
9886 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
9887 break;
9888 case 'r':
9889 if (!strcmp ("reduction", p))
9890 result = PRAGMA_OMP_CLAUSE_REDUCTION;
9891 break;
9892 case 's':
9893 if (!strcmp ("safelen", p))
9894 result = PRAGMA_OMP_CLAUSE_SAFELEN;
9895 else if (!strcmp ("schedule", p))
9896 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
9897 else if (!strcmp ("sections", p))
9898 result = PRAGMA_OMP_CLAUSE_SECTIONS;
9899 else if (!strcmp ("shared", p))
9900 result = PRAGMA_OMP_CLAUSE_SHARED;
9901 else if (!strcmp ("simdlen", p))
9902 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
9903 else if (!strcmp ("self", p))
9904 result = PRAGMA_OMP_CLAUSE_SELF;
9905 break;
9906 case 't':
9907 if (!strcmp ("taskgroup", p))
9908 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
9909 else if (!strcmp ("thread_limit", p))
9910 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
9911 else if (!strcmp ("to", p))
9912 result = PRAGMA_OMP_CLAUSE_TO;
9913 break;
9914 case 'u':
9915 if (!strcmp ("uniform", p))
9916 result = PRAGMA_OMP_CLAUSE_UNIFORM;
9917 else if (!strcmp ("untied", p))
9918 result = PRAGMA_OMP_CLAUSE_UNTIED;
9919 break;
9920 case 'v':
9921 if (!strcmp ("vector_length", p))
9922 result = PRAGMA_OMP_CLAUSE_VECTOR_LENGTH;
9923 else if (flag_cilkplus && !strcmp ("vectorlength", p))
9924 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
9925 break;
9926 case 'w':
9927 if (!strcmp ("wait", p))
9928 result = PRAGMA_OMP_CLAUSE_WAIT;
9929 break;
9933 if (result != PRAGMA_OMP_CLAUSE_NONE)
9934 c_parser_consume_token (parser);
9936 return result;
9939 /* Validate that a clause of the given type does not already exist. */
9941 static void
9942 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
9943 const char *name)
9945 tree c;
9947 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
9948 if (OMP_CLAUSE_CODE (c) == code)
9950 location_t loc = OMP_CLAUSE_LOCATION (c);
9951 error_at (loc, "too many %qs clauses", name);
9952 break;
9956 /* OpenACC 2.0
9957 Parse wait clause or wait directive parameters. */
9959 static tree
9960 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
9962 vec<tree, va_gc> *args;
9963 tree t, args_tree;
9965 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9966 return list;
9968 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
9970 if (args->length () == 0)
9972 c_parser_error (parser, "expected integer expression before ')'");
9973 release_tree_vector (args);
9974 return list;
9977 args_tree = build_tree_list_vec (args);
9979 for (t = args_tree; t; t = TREE_CHAIN (t))
9981 tree targ = TREE_VALUE (t);
9983 if (targ != error_mark_node)
9985 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
9987 c_parser_error (parser, "expression must be integral");
9988 targ = error_mark_node;
9990 else
9992 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
9994 OMP_CLAUSE_DECL (c) = targ;
9995 OMP_CLAUSE_CHAIN (c) = list;
9996 list = c;
10001 release_tree_vector (args);
10002 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10003 return list;
10006 /* OpenACC 2.0, OpenMP 2.5:
10007 variable-list:
10008 identifier
10009 variable-list , identifier
10011 If KIND is nonzero, create the appropriate node and install the
10012 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10013 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10015 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10016 return the list created. */
10018 static tree
10019 c_parser_omp_variable_list (c_parser *parser,
10020 location_t clause_loc,
10021 enum omp_clause_code kind, tree list)
10023 if (c_parser_next_token_is_not (parser, CPP_NAME)
10024 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10025 c_parser_error (parser, "expected identifier");
10027 while (c_parser_next_token_is (parser, CPP_NAME)
10028 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10030 tree t = lookup_name (c_parser_peek_token (parser)->value);
10032 if (t == NULL_TREE)
10034 undeclared_variable (c_parser_peek_token (parser)->location,
10035 c_parser_peek_token (parser)->value);
10036 t = error_mark_node;
10039 c_parser_consume_token (parser);
10041 if (t == error_mark_node)
10043 else if (kind != 0)
10045 switch (kind)
10047 case OMP_CLAUSE_MAP:
10048 case OMP_CLAUSE_FROM:
10049 case OMP_CLAUSE_TO:
10050 case OMP_CLAUSE_DEPEND:
10051 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10053 tree low_bound = NULL_TREE, length = NULL_TREE;
10055 c_parser_consume_token (parser);
10056 if (!c_parser_next_token_is (parser, CPP_COLON))
10058 low_bound = c_parser_expression (parser).value;
10059 mark_exp_read (low_bound);
10061 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10062 length = integer_one_node;
10063 else
10065 /* Look for `:'. */
10066 if (!c_parser_require (parser, CPP_COLON,
10067 "expected %<:%>"))
10069 t = error_mark_node;
10070 break;
10072 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10074 length = c_parser_expression (parser).value;
10075 mark_exp_read (length);
10078 /* Look for the closing `]'. */
10079 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10080 "expected %<]%>"))
10082 t = error_mark_node;
10083 break;
10085 t = tree_cons (low_bound, length, t);
10087 break;
10088 default:
10089 break;
10092 if (t != error_mark_node)
10094 tree u = build_omp_clause (clause_loc, kind);
10095 OMP_CLAUSE_DECL (u) = t;
10096 OMP_CLAUSE_CHAIN (u) = list;
10097 list = u;
10100 else
10101 list = tree_cons (t, NULL_TREE, list);
10103 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10104 break;
10106 c_parser_consume_token (parser);
10109 return list;
10112 /* Similarly, but expect leading and trailing parenthesis. This is a very
10113 common case for OpenACC and OpenMP clauses. */
10115 static tree
10116 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10117 tree list)
10119 /* The clauses location. */
10120 location_t loc = c_parser_peek_token (parser)->location;
10122 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10124 list = c_parser_omp_variable_list (parser, loc, kind, list);
10125 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10127 return list;
10130 /* OpenACC 2.0:
10131 copy ( variable-list )
10132 copyin ( variable-list )
10133 copyout ( variable-list )
10134 create ( variable-list )
10135 delete ( variable-list )
10136 present ( variable-list )
10137 present_or_copy ( variable-list )
10138 pcopy ( variable-list )
10139 present_or_copyin ( variable-list )
10140 pcopyin ( variable-list )
10141 present_or_copyout ( variable-list )
10142 pcopyout ( variable-list )
10143 present_or_create ( variable-list )
10144 pcreate ( variable-list ) */
10146 static tree
10147 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10148 tree list)
10150 enum omp_clause_map_kind kind;
10151 switch (c_kind)
10153 default:
10154 gcc_unreachable ();
10155 case PRAGMA_OMP_CLAUSE_COPY:
10156 kind = OMP_CLAUSE_MAP_FORCE_TOFROM;
10157 break;
10158 case PRAGMA_OMP_CLAUSE_COPYIN:
10159 kind = OMP_CLAUSE_MAP_FORCE_TO;
10160 break;
10161 case PRAGMA_OMP_CLAUSE_COPYOUT:
10162 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10163 break;
10164 case PRAGMA_OMP_CLAUSE_CREATE:
10165 kind = OMP_CLAUSE_MAP_FORCE_ALLOC;
10166 break;
10167 case PRAGMA_OMP_CLAUSE_DELETE:
10168 kind = OMP_CLAUSE_MAP_FORCE_DEALLOC;
10169 break;
10170 case PRAGMA_OMP_CLAUSE_DEVICE:
10171 kind = OMP_CLAUSE_MAP_FORCE_TO;
10172 break;
10173 case PRAGMA_OMP_CLAUSE_HOST:
10174 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10175 break;
10176 case PRAGMA_OMP_CLAUSE_PRESENT:
10177 kind = OMP_CLAUSE_MAP_FORCE_PRESENT;
10178 break;
10179 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
10180 kind = OMP_CLAUSE_MAP_TOFROM;
10181 break;
10182 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
10183 kind = OMP_CLAUSE_MAP_TO;
10184 break;
10185 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
10186 kind = OMP_CLAUSE_MAP_FROM;
10187 break;
10188 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
10189 kind = OMP_CLAUSE_MAP_ALLOC;
10190 break;
10191 case PRAGMA_OMP_CLAUSE_SELF:
10192 kind = OMP_CLAUSE_MAP_FORCE_FROM;
10193 break;
10195 tree nl, c;
10196 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10198 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10199 OMP_CLAUSE_MAP_KIND (c) = kind;
10201 return nl;
10204 /* OpenACC 2.0:
10205 deviceptr ( variable-list ) */
10207 static tree
10208 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10210 location_t loc = c_parser_peek_token (parser)->location;
10211 tree vars, t;
10213 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10214 c_parser_oacc_data_clause), as for PRAGMA_OMP_CLAUSE_DEVICEPTR,
10215 variable-list must only allow for pointer variables. */
10216 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10217 for (t = vars; t && t; t = TREE_CHAIN (t))
10219 tree v = TREE_PURPOSE (t);
10221 /* FIXME diagnostics: Ideally we should keep individual
10222 locations for all the variables in the var list to make the
10223 following errors more precise. Perhaps
10224 c_parser_omp_var_list_parens() should construct a list of
10225 locations to go along with the var list. */
10227 if (TREE_CODE (v) != VAR_DECL)
10228 error_at (loc, "%qD is not a variable", v);
10229 else if (TREE_TYPE (v) == error_mark_node)
10231 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10232 error_at (loc, "%qD is not a pointer variable", v);
10234 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10235 OMP_CLAUSE_MAP_KIND (u) = OMP_CLAUSE_MAP_FORCE_DEVICEPTR;
10236 OMP_CLAUSE_DECL (u) = v;
10237 OMP_CLAUSE_CHAIN (u) = list;
10238 list = u;
10241 return list;
10244 /* OpenACC 2.0, OpenMP 3.0:
10245 collapse ( constant-expression ) */
10247 static tree
10248 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10250 tree c, num = error_mark_node;
10251 HOST_WIDE_INT n;
10252 location_t loc;
10254 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10256 loc = c_parser_peek_token (parser)->location;
10257 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10259 num = c_parser_expr_no_commas (parser, NULL).value;
10260 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10262 if (num == error_mark_node)
10263 return list;
10264 mark_exp_read (num);
10265 num = c_fully_fold (num, false, NULL);
10266 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10267 || !tree_fits_shwi_p (num)
10268 || (n = tree_to_shwi (num)) <= 0
10269 || (int) n != n)
10271 error_at (loc,
10272 "collapse argument needs positive constant integer expression");
10273 return list;
10275 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10276 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10277 OMP_CLAUSE_CHAIN (c) = list;
10278 return c;
10281 /* OpenMP 2.5:
10282 copyin ( variable-list ) */
10284 static tree
10285 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10287 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10290 /* OpenMP 2.5:
10291 copyprivate ( variable-list ) */
10293 static tree
10294 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10296 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10299 /* OpenMP 2.5:
10300 default ( shared | none ) */
10302 static tree
10303 c_parser_omp_clause_default (c_parser *parser, tree list)
10305 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10306 location_t loc = c_parser_peek_token (parser)->location;
10307 tree c;
10309 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10310 return list;
10311 if (c_parser_next_token_is (parser, CPP_NAME))
10313 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10315 switch (p[0])
10317 case 'n':
10318 if (strcmp ("none", p) != 0)
10319 goto invalid_kind;
10320 kind = OMP_CLAUSE_DEFAULT_NONE;
10321 break;
10323 case 's':
10324 if (strcmp ("shared", p) != 0)
10325 goto invalid_kind;
10326 kind = OMP_CLAUSE_DEFAULT_SHARED;
10327 break;
10329 default:
10330 goto invalid_kind;
10333 c_parser_consume_token (parser);
10335 else
10337 invalid_kind:
10338 c_parser_error (parser, "expected %<none%> or %<shared%>");
10340 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10342 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10343 return list;
10345 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10346 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10347 OMP_CLAUSE_CHAIN (c) = list;
10348 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10350 return c;
10353 /* OpenMP 2.5:
10354 firstprivate ( variable-list ) */
10356 static tree
10357 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10359 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10362 /* OpenMP 3.1:
10363 final ( expression ) */
10365 static tree
10366 c_parser_omp_clause_final (c_parser *parser, tree list)
10368 location_t loc = c_parser_peek_token (parser)->location;
10369 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10371 tree t = c_parser_paren_condition (parser);
10372 tree c;
10374 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10376 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10377 OMP_CLAUSE_FINAL_EXPR (c) = t;
10378 OMP_CLAUSE_CHAIN (c) = list;
10379 list = c;
10381 else
10382 c_parser_error (parser, "expected %<(%>");
10384 return list;
10387 /* OpenACC, OpenMP 2.5:
10388 if ( expression ) */
10390 static tree
10391 c_parser_omp_clause_if (c_parser *parser, tree list)
10393 location_t loc = c_parser_peek_token (parser)->location;
10394 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10396 tree t = c_parser_paren_condition (parser);
10397 tree c;
10399 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
10401 c = build_omp_clause (loc, OMP_CLAUSE_IF);
10402 OMP_CLAUSE_IF_EXPR (c) = t;
10403 OMP_CLAUSE_CHAIN (c) = list;
10404 list = c;
10406 else
10407 c_parser_error (parser, "expected %<(%>");
10409 return list;
10412 /* OpenMP 2.5:
10413 lastprivate ( variable-list ) */
10415 static tree
10416 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10418 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10421 /* OpenMP 3.1:
10422 mergeable */
10424 static tree
10425 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10427 tree c;
10429 /* FIXME: Should we allow duplicates? */
10430 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10432 c = build_omp_clause (c_parser_peek_token (parser)->location,
10433 OMP_CLAUSE_MERGEABLE);
10434 OMP_CLAUSE_CHAIN (c) = list;
10436 return c;
10439 /* OpenMP 2.5:
10440 nowait */
10442 static tree
10443 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10445 tree c;
10446 location_t loc = c_parser_peek_token (parser)->location;
10448 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10450 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10451 OMP_CLAUSE_CHAIN (c) = list;
10452 return c;
10455 /* OpenACC:
10456 num_gangs ( expression ) */
10458 static tree
10459 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
10461 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
10462 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10464 location_t expr_loc = c_parser_peek_token (parser)->location;
10465 tree c, t = c_parser_expression (parser).value;
10466 mark_exp_read (t);
10467 t = c_fully_fold (t, false, NULL);
10469 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10471 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10473 c_parser_error (parser, "expected integer expression");
10474 return list;
10477 /* Attempt to statically determine when the number isn't positive. */
10478 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10479 build_int_cst (TREE_TYPE (t), 0));
10480 if (CAN_HAVE_LOCATION_P (c))
10481 SET_EXPR_LOCATION (c, expr_loc);
10482 if (c == boolean_true_node)
10484 warning_at (expr_loc, 0,
10485 "%<num_gangs%> value must be positive");
10486 t = integer_one_node;
10489 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
10491 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
10492 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
10493 OMP_CLAUSE_CHAIN (c) = list;
10494 list = c;
10497 return list;
10500 /* OpenMP 2.5:
10501 num_threads ( expression ) */
10503 static tree
10504 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
10506 location_t num_threads_loc = c_parser_peek_token (parser)->location;
10507 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10509 location_t expr_loc = c_parser_peek_token (parser)->location;
10510 tree c, t = c_parser_expression (parser).value;
10511 mark_exp_read (t);
10512 t = c_fully_fold (t, false, NULL);
10514 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10516 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10518 c_parser_error (parser, "expected integer expression");
10519 return list;
10522 /* Attempt to statically determine when the number isn't positive. */
10523 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10524 build_int_cst (TREE_TYPE (t), 0));
10525 if (CAN_HAVE_LOCATION_P (c))
10526 SET_EXPR_LOCATION (c, expr_loc);
10527 if (c == boolean_true_node)
10529 warning_at (expr_loc, 0,
10530 "%<num_threads%> value must be positive");
10531 t = integer_one_node;
10534 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
10536 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
10537 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
10538 OMP_CLAUSE_CHAIN (c) = list;
10539 list = c;
10542 return list;
10545 /* OpenACC:
10546 num_workers ( expression ) */
10548 static tree
10549 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
10551 location_t num_workers_loc = c_parser_peek_token (parser)->location;
10552 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10554 location_t expr_loc = c_parser_peek_token (parser)->location;
10555 tree c, t = c_parser_expression (parser).value;
10556 mark_exp_read (t);
10557 t = c_fully_fold (t, false, NULL);
10559 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10561 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10563 c_parser_error (parser, "expected integer expression");
10564 return list;
10567 /* Attempt to statically determine when the number isn't positive. */
10568 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10569 build_int_cst (TREE_TYPE (t), 0));
10570 if (CAN_HAVE_LOCATION_P (c))
10571 SET_EXPR_LOCATION (c, expr_loc);
10572 if (c == boolean_true_node)
10574 warning_at (expr_loc, 0,
10575 "%<num_workers%> value must be positive");
10576 t = integer_one_node;
10579 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
10581 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
10582 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
10583 OMP_CLAUSE_CHAIN (c) = list;
10584 list = c;
10587 return list;
10590 /* OpenACC:
10591 async [( int-expr )] */
10593 static tree
10594 c_parser_oacc_clause_async (c_parser *parser, tree list)
10596 tree c, t;
10597 location_t loc = c_parser_peek_token (parser)->location;
10599 /* TODO XXX: FIX -1 (acc_async_noval). */
10600 t = build_int_cst (integer_type_node, -1);
10602 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10604 c_parser_consume_token (parser);
10606 t = c_parser_expression (parser).value;
10607 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10608 c_parser_error (parser, "expected integer expression");
10609 else if (t == error_mark_node
10610 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
10611 return list;
10613 else
10615 t = c_fully_fold (t, false, NULL);
10618 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
10620 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
10621 OMP_CLAUSE_ASYNC_EXPR (c) = t;
10622 OMP_CLAUSE_CHAIN (c) = list;
10623 list = c;
10625 return list;
10628 /* OpenACC:
10629 wait ( int-expr-list ) */
10631 static tree
10632 c_parser_oacc_clause_wait (c_parser *parser, tree list)
10634 location_t clause_loc = c_parser_peek_token (parser)->location;
10636 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
10637 list = c_parser_oacc_wait_list (parser, clause_loc, list);
10639 return list;
10642 /* OpenMP 2.5:
10643 ordered */
10645 static tree
10646 c_parser_omp_clause_ordered (c_parser *parser, tree list)
10648 tree c;
10650 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
10652 c = build_omp_clause (c_parser_peek_token (parser)->location,
10653 OMP_CLAUSE_ORDERED);
10654 OMP_CLAUSE_CHAIN (c) = list;
10656 return c;
10659 /* OpenMP 2.5:
10660 private ( variable-list ) */
10662 static tree
10663 c_parser_omp_clause_private (c_parser *parser, tree list)
10665 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
10668 /* OpenMP 2.5:
10669 reduction ( reduction-operator : variable-list )
10671 reduction-operator:
10672 One of: + * - & ^ | && ||
10674 OpenMP 3.1:
10676 reduction-operator:
10677 One of: + * - & ^ | && || max min
10679 OpenMP 4.0:
10681 reduction-operator:
10682 One of: + * - & ^ | && ||
10683 identifier */
10685 static tree
10686 c_parser_omp_clause_reduction (c_parser *parser, tree list)
10688 location_t clause_loc = c_parser_peek_token (parser)->location;
10689 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10691 enum tree_code code = ERROR_MARK;
10692 tree reduc_id = NULL_TREE;
10694 switch (c_parser_peek_token (parser)->type)
10696 case CPP_PLUS:
10697 code = PLUS_EXPR;
10698 break;
10699 case CPP_MULT:
10700 code = MULT_EXPR;
10701 break;
10702 case CPP_MINUS:
10703 code = MINUS_EXPR;
10704 break;
10705 case CPP_AND:
10706 code = BIT_AND_EXPR;
10707 break;
10708 case CPP_XOR:
10709 code = BIT_XOR_EXPR;
10710 break;
10711 case CPP_OR:
10712 code = BIT_IOR_EXPR;
10713 break;
10714 case CPP_AND_AND:
10715 code = TRUTH_ANDIF_EXPR;
10716 break;
10717 case CPP_OR_OR:
10718 code = TRUTH_ORIF_EXPR;
10719 break;
10720 case CPP_NAME:
10722 const char *p
10723 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10724 if (strcmp (p, "min") == 0)
10726 code = MIN_EXPR;
10727 break;
10729 if (strcmp (p, "max") == 0)
10731 code = MAX_EXPR;
10732 break;
10734 reduc_id = c_parser_peek_token (parser)->value;
10735 break;
10737 default:
10738 c_parser_error (parser,
10739 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
10740 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
10741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10742 return list;
10744 c_parser_consume_token (parser);
10745 reduc_id = c_omp_reduction_id (code, reduc_id);
10746 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10748 tree nl, c;
10750 nl = c_parser_omp_variable_list (parser, clause_loc,
10751 OMP_CLAUSE_REDUCTION, list);
10752 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10754 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
10755 OMP_CLAUSE_REDUCTION_CODE (c) = code;
10756 if (code == ERROR_MARK
10757 || !(INTEGRAL_TYPE_P (type)
10758 || TREE_CODE (type) == REAL_TYPE
10759 || TREE_CODE (type) == COMPLEX_TYPE))
10760 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
10761 = c_omp_reduction_lookup (reduc_id,
10762 TYPE_MAIN_VARIANT (type));
10765 list = nl;
10767 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10769 return list;
10772 /* OpenMP 2.5:
10773 schedule ( schedule-kind )
10774 schedule ( schedule-kind , expression )
10776 schedule-kind:
10777 static | dynamic | guided | runtime | auto
10780 static tree
10781 c_parser_omp_clause_schedule (c_parser *parser, tree list)
10783 tree c, t;
10784 location_t loc = c_parser_peek_token (parser)->location;
10786 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10787 return list;
10789 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
10791 if (c_parser_next_token_is (parser, CPP_NAME))
10793 tree kind = c_parser_peek_token (parser)->value;
10794 const char *p = IDENTIFIER_POINTER (kind);
10796 switch (p[0])
10798 case 'd':
10799 if (strcmp ("dynamic", p) != 0)
10800 goto invalid_kind;
10801 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
10802 break;
10804 case 'g':
10805 if (strcmp ("guided", p) != 0)
10806 goto invalid_kind;
10807 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
10808 break;
10810 case 'r':
10811 if (strcmp ("runtime", p) != 0)
10812 goto invalid_kind;
10813 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
10814 break;
10816 default:
10817 goto invalid_kind;
10820 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
10821 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
10822 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10823 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
10824 else
10825 goto invalid_kind;
10827 c_parser_consume_token (parser);
10828 if (c_parser_next_token_is (parser, CPP_COMMA))
10830 location_t here;
10831 c_parser_consume_token (parser);
10833 here = c_parser_peek_token (parser)->location;
10834 t = c_parser_expr_no_commas (parser, NULL).value;
10835 mark_exp_read (t);
10836 t = c_fully_fold (t, false, NULL);
10838 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
10839 error_at (here, "schedule %<runtime%> does not take "
10840 "a %<chunk_size%> parameter");
10841 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
10842 error_at (here,
10843 "schedule %<auto%> does not take "
10844 "a %<chunk_size%> parameter");
10845 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
10846 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
10847 else
10848 c_parser_error (parser, "expected integer expression");
10850 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10852 else
10853 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10854 "expected %<,%> or %<)%>");
10856 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
10857 OMP_CLAUSE_CHAIN (c) = list;
10858 return c;
10860 invalid_kind:
10861 c_parser_error (parser, "invalid schedule kind");
10862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
10863 return list;
10866 /* OpenMP 2.5:
10867 shared ( variable-list ) */
10869 static tree
10870 c_parser_omp_clause_shared (c_parser *parser, tree list)
10872 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
10875 /* OpenMP 3.0:
10876 untied */
10878 static tree
10879 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10881 tree c;
10883 /* FIXME: Should we allow duplicates? */
10884 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
10886 c = build_omp_clause (c_parser_peek_token (parser)->location,
10887 OMP_CLAUSE_UNTIED);
10888 OMP_CLAUSE_CHAIN (c) = list;
10890 return c;
10893 /* OpenACC:
10894 vector_length ( expression ) */
10896 static tree
10897 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
10899 location_t vector_length_loc = c_parser_peek_token (parser)->location;
10900 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10902 location_t expr_loc = c_parser_peek_token (parser)->location;
10903 tree c, t = c_parser_expression (parser).value;
10904 mark_exp_read (t);
10905 t = c_fully_fold (t, false, NULL);
10907 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10909 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10911 c_parser_error (parser, "expected integer expression");
10912 return list;
10915 /* Attempt to statically determine when the number isn't positive. */
10916 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10917 build_int_cst (TREE_TYPE (t), 0));
10918 if (CAN_HAVE_LOCATION_P (c))
10919 SET_EXPR_LOCATION (c, expr_loc);
10920 if (c == boolean_true_node)
10922 warning_at (expr_loc, 0,
10923 "%<vector_length%> value must be positive");
10924 t = integer_one_node;
10927 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
10929 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
10930 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
10931 OMP_CLAUSE_CHAIN (c) = list;
10932 list = c;
10935 return list;
10938 /* OpenMP 4.0:
10939 inbranch
10940 notinbranch */
10942 static tree
10943 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
10944 enum omp_clause_code code, tree list)
10946 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
10948 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10949 OMP_CLAUSE_CHAIN (c) = list;
10951 return c;
10954 /* OpenMP 4.0:
10955 parallel
10957 sections
10958 taskgroup */
10960 static tree
10961 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
10962 enum omp_clause_code code, tree list)
10964 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
10965 OMP_CLAUSE_CHAIN (c) = list;
10967 return c;
10970 /* OpenMP 4.0:
10971 num_teams ( expression ) */
10973 static tree
10974 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
10976 location_t num_teams_loc = c_parser_peek_token (parser)->location;
10977 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10979 location_t expr_loc = c_parser_peek_token (parser)->location;
10980 tree c, t = c_parser_expression (parser).value;
10981 mark_exp_read (t);
10982 t = c_fully_fold (t, false, NULL);
10984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10986 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10988 c_parser_error (parser, "expected integer expression");
10989 return list;
10992 /* Attempt to statically determine when the number isn't positive. */
10993 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
10994 build_int_cst (TREE_TYPE (t), 0));
10995 if (CAN_HAVE_LOCATION_P (c))
10996 SET_EXPR_LOCATION (c, expr_loc);
10997 if (c == boolean_true_node)
10999 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
11000 t = integer_one_node;
11003 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
11005 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
11006 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
11007 OMP_CLAUSE_CHAIN (c) = list;
11008 list = c;
11011 return list;
11014 /* OpenMP 4.0:
11015 thread_limit ( expression ) */
11017 static tree
11018 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
11020 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
11021 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11023 location_t expr_loc = c_parser_peek_token (parser)->location;
11024 tree c, t = c_parser_expression (parser).value;
11025 mark_exp_read (t);
11026 t = c_fully_fold (t, false, NULL);
11028 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11030 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11032 c_parser_error (parser, "expected integer expression");
11033 return list;
11036 /* Attempt to statically determine when the number isn't positive. */
11037 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11038 build_int_cst (TREE_TYPE (t), 0));
11039 if (CAN_HAVE_LOCATION_P (c))
11040 SET_EXPR_LOCATION (c, expr_loc);
11041 if (c == boolean_true_node)
11043 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
11044 t = integer_one_node;
11047 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
11048 "thread_limit");
11050 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
11051 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
11052 OMP_CLAUSE_CHAIN (c) = list;
11053 list = c;
11056 return list;
11059 /* OpenMP 4.0:
11060 aligned ( variable-list )
11061 aligned ( variable-list : constant-expression ) */
11063 static tree
11064 c_parser_omp_clause_aligned (c_parser *parser, tree list)
11066 location_t clause_loc = c_parser_peek_token (parser)->location;
11067 tree nl, c;
11069 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11070 return list;
11072 nl = c_parser_omp_variable_list (parser, clause_loc,
11073 OMP_CLAUSE_ALIGNED, list);
11075 if (c_parser_next_token_is (parser, CPP_COLON))
11077 c_parser_consume_token (parser);
11078 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
11079 mark_exp_read (alignment);
11080 alignment = c_fully_fold (alignment, false, NULL);
11081 if (!INTEGRAL_TYPE_P (TREE_TYPE (alignment))
11082 && TREE_CODE (alignment) != INTEGER_CST
11083 && tree_int_cst_sgn (alignment) != 1)
11085 error_at (clause_loc, "%<aligned%> clause alignment expression must "
11086 "be positive constant integer expression");
11087 alignment = NULL_TREE;
11090 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11091 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
11094 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11095 return nl;
11098 /* OpenMP 4.0:
11099 linear ( variable-list )
11100 linear ( variable-list : expression ) */
11102 static tree
11103 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
11105 location_t clause_loc = c_parser_peek_token (parser)->location;
11106 tree nl, c, step;
11108 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11109 return list;
11111 nl = c_parser_omp_variable_list (parser, clause_loc,
11112 OMP_CLAUSE_LINEAR, list);
11114 if (c_parser_next_token_is (parser, CPP_COLON))
11116 c_parser_consume_token (parser);
11117 step = c_parser_expression (parser).value;
11118 mark_exp_read (step);
11119 step = c_fully_fold (step, false, NULL);
11120 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
11122 sorry ("using parameters for %<linear%> step is not supported yet");
11123 step = integer_one_node;
11125 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
11127 error_at (clause_loc, "%<linear%> clause step expression must "
11128 "be integral");
11129 step = integer_one_node;
11133 else
11134 step = integer_one_node;
11136 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11138 OMP_CLAUSE_LINEAR_STEP (c) = step;
11141 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11142 return nl;
11145 /* OpenMP 4.0:
11146 safelen ( constant-expression ) */
11148 static tree
11149 c_parser_omp_clause_safelen (c_parser *parser, tree list)
11151 location_t clause_loc = c_parser_peek_token (parser)->location;
11152 tree c, t;
11154 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11155 return list;
11157 t = c_parser_expr_no_commas (parser, NULL).value;
11158 mark_exp_read (t);
11159 t = c_fully_fold (t, false, NULL);
11160 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11161 && TREE_CODE (t) != INTEGER_CST
11162 && tree_int_cst_sgn (t) != 1)
11164 error_at (clause_loc, "%<safelen%> clause expression must "
11165 "be positive constant integer expression");
11166 t = NULL_TREE;
11169 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11170 if (t == NULL_TREE || t == error_mark_node)
11171 return list;
11173 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
11175 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
11176 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
11177 OMP_CLAUSE_CHAIN (c) = list;
11178 return c;
11181 /* OpenMP 4.0:
11182 simdlen ( constant-expression ) */
11184 static tree
11185 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
11187 location_t clause_loc = c_parser_peek_token (parser)->location;
11188 tree c, t;
11190 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11191 return list;
11193 t = c_parser_expr_no_commas (parser, NULL).value;
11194 mark_exp_read (t);
11195 t = c_fully_fold (t, false, NULL);
11196 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11197 && TREE_CODE (t) != INTEGER_CST
11198 && tree_int_cst_sgn (t) != 1)
11200 error_at (clause_loc, "%<simdlen%> clause expression must "
11201 "be positive constant integer expression");
11202 t = NULL_TREE;
11205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11206 if (t == NULL_TREE || t == error_mark_node)
11207 return list;
11209 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
11211 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
11212 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
11213 OMP_CLAUSE_CHAIN (c) = list;
11214 return c;
11217 /* OpenMP 4.0:
11218 depend ( depend-kind: variable-list )
11220 depend-kind:
11221 in | out | inout */
11223 static tree
11224 c_parser_omp_clause_depend (c_parser *parser, tree list)
11226 location_t clause_loc = c_parser_peek_token (parser)->location;
11227 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
11228 tree nl, c;
11230 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11231 return list;
11233 if (c_parser_next_token_is (parser, CPP_NAME))
11235 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11236 if (strcmp ("in", p) == 0)
11237 kind = OMP_CLAUSE_DEPEND_IN;
11238 else if (strcmp ("inout", p) == 0)
11239 kind = OMP_CLAUSE_DEPEND_INOUT;
11240 else if (strcmp ("out", p) == 0)
11241 kind = OMP_CLAUSE_DEPEND_OUT;
11242 else
11243 goto invalid_kind;
11245 else
11246 goto invalid_kind;
11248 c_parser_consume_token (parser);
11249 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11250 goto resync_fail;
11252 nl = c_parser_omp_variable_list (parser, clause_loc,
11253 OMP_CLAUSE_DEPEND, list);
11255 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11256 OMP_CLAUSE_DEPEND_KIND (c) = kind;
11258 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11259 return nl;
11261 invalid_kind:
11262 c_parser_error (parser, "invalid depend kind");
11263 resync_fail:
11264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11265 return list;
11268 /* OpenMP 4.0:
11269 map ( map-kind: variable-list )
11270 map ( variable-list )
11272 map-kind:
11273 alloc | to | from | tofrom */
11275 static tree
11276 c_parser_omp_clause_map (c_parser *parser, tree list)
11278 location_t clause_loc = c_parser_peek_token (parser)->location;
11279 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
11280 tree nl, c;
11282 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11283 return list;
11285 if (c_parser_next_token_is (parser, CPP_NAME)
11286 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11288 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11289 if (strcmp ("alloc", p) == 0)
11290 kind = OMP_CLAUSE_MAP_ALLOC;
11291 else if (strcmp ("to", p) == 0)
11292 kind = OMP_CLAUSE_MAP_TO;
11293 else if (strcmp ("from", p) == 0)
11294 kind = OMP_CLAUSE_MAP_FROM;
11295 else if (strcmp ("tofrom", p) == 0)
11296 kind = OMP_CLAUSE_MAP_TOFROM;
11297 else
11299 c_parser_error (parser, "invalid map kind");
11300 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11301 "expected %<)%>");
11302 return list;
11304 c_parser_consume_token (parser);
11305 c_parser_consume_token (parser);
11308 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
11310 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11311 OMP_CLAUSE_MAP_KIND (c) = kind;
11313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11314 return nl;
11317 /* OpenMP 4.0:
11318 device ( expression ) */
11320 static tree
11321 c_parser_omp_clause_device (c_parser *parser, tree list)
11323 location_t clause_loc = c_parser_peek_token (parser)->location;
11324 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11326 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
11327 mark_exp_read (t);
11328 t = c_fully_fold (t, false, NULL);
11330 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11332 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11334 c_parser_error (parser, "expected integer expression");
11335 return list;
11338 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
11340 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
11341 OMP_CLAUSE_DEVICE_ID (c) = t;
11342 OMP_CLAUSE_CHAIN (c) = list;
11343 list = c;
11346 return list;
11349 /* OpenMP 4.0:
11350 dist_schedule ( static )
11351 dist_schedule ( static , expression ) */
11353 static tree
11354 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
11356 tree c, t = NULL_TREE;
11357 location_t loc = c_parser_peek_token (parser)->location;
11359 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11360 return list;
11362 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
11364 c_parser_error (parser, "invalid dist_schedule kind");
11365 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11366 "expected %<)%>");
11367 return list;
11370 c_parser_consume_token (parser);
11371 if (c_parser_next_token_is (parser, CPP_COMMA))
11373 c_parser_consume_token (parser);
11375 t = c_parser_expr_no_commas (parser, NULL).value;
11376 mark_exp_read (t);
11377 t = c_fully_fold (t, false, NULL);
11378 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11380 else
11381 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11382 "expected %<,%> or %<)%>");
11384 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11385 if (t == error_mark_node)
11386 return list;
11388 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
11389 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
11390 OMP_CLAUSE_CHAIN (c) = list;
11391 return c;
11394 /* OpenMP 4.0:
11395 proc_bind ( proc-bind-kind )
11397 proc-bind-kind:
11398 master | close | spread */
11400 static tree
11401 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
11403 location_t clause_loc = c_parser_peek_token (parser)->location;
11404 enum omp_clause_proc_bind_kind kind;
11405 tree c;
11407 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11408 return list;
11410 if (c_parser_next_token_is (parser, CPP_NAME))
11412 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11413 if (strcmp ("master", p) == 0)
11414 kind = OMP_CLAUSE_PROC_BIND_MASTER;
11415 else if (strcmp ("close", p) == 0)
11416 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
11417 else if (strcmp ("spread", p) == 0)
11418 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
11419 else
11420 goto invalid_kind;
11422 else
11423 goto invalid_kind;
11425 c_parser_consume_token (parser);
11426 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11427 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
11428 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
11429 OMP_CLAUSE_CHAIN (c) = list;
11430 return c;
11432 invalid_kind:
11433 c_parser_error (parser, "invalid proc_bind kind");
11434 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11435 return list;
11438 /* OpenMP 4.0:
11439 to ( variable-list ) */
11441 static tree
11442 c_parser_omp_clause_to (c_parser *parser, tree list)
11444 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
11447 /* OpenMP 4.0:
11448 from ( variable-list ) */
11450 static tree
11451 c_parser_omp_clause_from (c_parser *parser, tree list)
11453 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
11456 /* OpenMP 4.0:
11457 uniform ( variable-list ) */
11459 static tree
11460 c_parser_omp_clause_uniform (c_parser *parser, tree list)
11462 /* The clauses location. */
11463 location_t loc = c_parser_peek_token (parser)->location;
11465 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11467 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
11468 list);
11469 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11471 return list;
11474 /* Parse all OpenACC clauses. The set clauses allowed by the directive
11475 is a bitmask in MASK. Return the list of clauses found. */
11477 static tree
11478 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
11479 const char *where, bool finish_p = true)
11481 tree clauses = NULL;
11482 bool first = true;
11484 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11486 location_t here;
11487 pragma_omp_clause c_kind;
11488 const char *c_name;
11489 tree prev = clauses;
11491 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11492 c_parser_consume_token (parser);
11494 here = c_parser_peek_token (parser)->location;
11495 c_kind = c_parser_omp_clause_name (parser);
11497 switch (c_kind)
11499 case PRAGMA_OMP_CLAUSE_ASYNC:
11500 clauses = c_parser_oacc_clause_async (parser, clauses);
11501 c_name = "async";
11502 break;
11503 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11504 clauses = c_parser_omp_clause_collapse (parser, clauses);
11505 c_name = "collapse";
11506 break;
11507 case PRAGMA_OMP_CLAUSE_COPY:
11508 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11509 c_name = "copy";
11510 break;
11511 case PRAGMA_OMP_CLAUSE_COPYIN:
11512 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11513 c_name = "copyin";
11514 break;
11515 case PRAGMA_OMP_CLAUSE_COPYOUT:
11516 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11517 c_name = "copyout";
11518 break;
11519 case PRAGMA_OMP_CLAUSE_CREATE:
11520 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11521 c_name = "create";
11522 break;
11523 case PRAGMA_OMP_CLAUSE_DELETE:
11524 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11525 c_name = "delete";
11526 break;
11527 case PRAGMA_OMP_CLAUSE_DEVICE:
11528 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11529 c_name = "device";
11530 break;
11531 case PRAGMA_OMP_CLAUSE_DEVICEPTR:
11532 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
11533 c_name = "deviceptr";
11534 break;
11535 case PRAGMA_OMP_CLAUSE_HOST:
11536 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11537 c_name = "host";
11538 break;
11539 case PRAGMA_OMP_CLAUSE_IF:
11540 clauses = c_parser_omp_clause_if (parser, clauses);
11541 c_name = "if";
11542 break;
11543 case PRAGMA_OMP_CLAUSE_NUM_GANGS:
11544 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
11545 c_name = "num_gangs";
11546 break;
11547 case PRAGMA_OMP_CLAUSE_NUM_WORKERS:
11548 clauses = c_parser_omp_clause_num_workers (parser, clauses);
11549 c_name = "num_workers";
11550 break;
11551 case PRAGMA_OMP_CLAUSE_PRESENT:
11552 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11553 c_name = "present";
11554 break;
11555 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY:
11556 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11557 c_name = "present_or_copy";
11558 break;
11559 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN:
11560 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11561 c_name = "present_or_copyin";
11562 break;
11563 case PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT:
11564 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11565 c_name = "present_or_copyout";
11566 break;
11567 case PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE:
11568 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11569 c_name = "present_or_create";
11570 break;
11571 case PRAGMA_OMP_CLAUSE_REDUCTION:
11572 clauses = c_parser_omp_clause_reduction (parser, clauses);
11573 c_name = "reduction";
11574 break;
11575 case PRAGMA_OMP_CLAUSE_SELF:
11576 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
11577 c_name = "self";
11578 break;
11579 case PRAGMA_OMP_CLAUSE_VECTOR_LENGTH:
11580 clauses = c_parser_omp_clause_vector_length (parser, clauses);
11581 c_name = "vector_length";
11582 break;
11583 case PRAGMA_OMP_CLAUSE_WAIT:
11584 clauses = c_parser_oacc_clause_wait (parser, clauses);
11585 c_name = "wait";
11586 break;
11587 default:
11588 c_parser_error (parser, "expected clause");
11589 goto saw_error;
11592 first = false;
11594 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11596 /* Remove the invalid clause(s) from the list to avoid
11597 confusing the rest of the compiler. */
11598 clauses = prev;
11599 error_at (here, "%qs is not valid for %qs", c_name, where);
11603 saw_error:
11604 c_parser_skip_to_pragma_eol (parser);
11606 if (finish_p)
11607 return c_finish_omp_clauses (clauses);
11609 return clauses;
11612 /* Parse all OpenMP clauses. The set clauses allowed by the directive
11613 is a bitmask in MASK. Return the list of clauses found. */
11615 static tree
11616 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
11617 const char *where, bool finish_p = true)
11619 tree clauses = NULL;
11620 bool first = true, cilk_simd_fn = false;
11622 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
11624 location_t here;
11625 pragma_omp_clause c_kind;
11626 const char *c_name;
11627 tree prev = clauses;
11629 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
11630 c_parser_consume_token (parser);
11632 here = c_parser_peek_token (parser)->location;
11633 c_kind = c_parser_omp_clause_name (parser);
11635 switch (c_kind)
11637 case PRAGMA_OMP_CLAUSE_COLLAPSE:
11638 clauses = c_parser_omp_clause_collapse (parser, clauses);
11639 c_name = "collapse";
11640 break;
11641 case PRAGMA_OMP_CLAUSE_COPYIN:
11642 clauses = c_parser_omp_clause_copyin (parser, clauses);
11643 c_name = "copyin";
11644 break;
11645 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
11646 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
11647 c_name = "copyprivate";
11648 break;
11649 case PRAGMA_OMP_CLAUSE_DEFAULT:
11650 clauses = c_parser_omp_clause_default (parser, clauses);
11651 c_name = "default";
11652 break;
11653 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
11654 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
11655 c_name = "firstprivate";
11656 break;
11657 case PRAGMA_OMP_CLAUSE_FINAL:
11658 clauses = c_parser_omp_clause_final (parser, clauses);
11659 c_name = "final";
11660 break;
11661 case PRAGMA_OMP_CLAUSE_IF:
11662 clauses = c_parser_omp_clause_if (parser, clauses);
11663 c_name = "if";
11664 break;
11665 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
11666 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
11667 c_name = "lastprivate";
11668 break;
11669 case PRAGMA_OMP_CLAUSE_MERGEABLE:
11670 clauses = c_parser_omp_clause_mergeable (parser, clauses);
11671 c_name = "mergeable";
11672 break;
11673 case PRAGMA_OMP_CLAUSE_NOWAIT:
11674 clauses = c_parser_omp_clause_nowait (parser, clauses);
11675 c_name = "nowait";
11676 break;
11677 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
11678 clauses = c_parser_omp_clause_num_threads (parser, clauses);
11679 c_name = "num_threads";
11680 break;
11681 case PRAGMA_OMP_CLAUSE_ORDERED:
11682 clauses = c_parser_omp_clause_ordered (parser, clauses);
11683 c_name = "ordered";
11684 break;
11685 case PRAGMA_OMP_CLAUSE_PRIVATE:
11686 clauses = c_parser_omp_clause_private (parser, clauses);
11687 c_name = "private";
11688 break;
11689 case PRAGMA_OMP_CLAUSE_REDUCTION:
11690 clauses = c_parser_omp_clause_reduction (parser, clauses);
11691 c_name = "reduction";
11692 break;
11693 case PRAGMA_OMP_CLAUSE_SCHEDULE:
11694 clauses = c_parser_omp_clause_schedule (parser, clauses);
11695 c_name = "schedule";
11696 break;
11697 case PRAGMA_OMP_CLAUSE_SHARED:
11698 clauses = c_parser_omp_clause_shared (parser, clauses);
11699 c_name = "shared";
11700 break;
11701 case PRAGMA_OMP_CLAUSE_UNTIED:
11702 clauses = c_parser_omp_clause_untied (parser, clauses);
11703 c_name = "untied";
11704 break;
11705 case PRAGMA_OMP_CLAUSE_INBRANCH:
11706 case PRAGMA_CILK_CLAUSE_MASK:
11707 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
11708 clauses);
11709 c_name = "inbranch";
11710 break;
11711 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
11712 case PRAGMA_CILK_CLAUSE_NOMASK:
11713 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
11714 clauses);
11715 c_name = "notinbranch";
11716 break;
11717 case PRAGMA_OMP_CLAUSE_PARALLEL:
11718 clauses
11719 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
11720 clauses);
11721 c_name = "parallel";
11722 if (!first)
11724 clause_not_first:
11725 error_at (here, "%qs must be the first clause of %qs",
11726 c_name, where);
11727 clauses = prev;
11729 break;
11730 case PRAGMA_OMP_CLAUSE_FOR:
11731 clauses
11732 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
11733 clauses);
11734 c_name = "for";
11735 if (!first)
11736 goto clause_not_first;
11737 break;
11738 case PRAGMA_OMP_CLAUSE_SECTIONS:
11739 clauses
11740 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
11741 clauses);
11742 c_name = "sections";
11743 if (!first)
11744 goto clause_not_first;
11745 break;
11746 case PRAGMA_OMP_CLAUSE_TASKGROUP:
11747 clauses
11748 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
11749 clauses);
11750 c_name = "taskgroup";
11751 if (!first)
11752 goto clause_not_first;
11753 break;
11754 case PRAGMA_OMP_CLAUSE_TO:
11755 clauses = c_parser_omp_clause_to (parser, clauses);
11756 c_name = "to";
11757 break;
11758 case PRAGMA_OMP_CLAUSE_FROM:
11759 clauses = c_parser_omp_clause_from (parser, clauses);
11760 c_name = "from";
11761 break;
11762 case PRAGMA_OMP_CLAUSE_UNIFORM:
11763 clauses = c_parser_omp_clause_uniform (parser, clauses);
11764 c_name = "uniform";
11765 break;
11766 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
11767 clauses = c_parser_omp_clause_num_teams (parser, clauses);
11768 c_name = "num_teams";
11769 break;
11770 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
11771 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
11772 c_name = "thread_limit";
11773 break;
11774 case PRAGMA_OMP_CLAUSE_ALIGNED:
11775 clauses = c_parser_omp_clause_aligned (parser, clauses);
11776 c_name = "aligned";
11777 break;
11778 case PRAGMA_OMP_CLAUSE_LINEAR:
11779 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
11780 cilk_simd_fn = true;
11781 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
11782 c_name = "linear";
11783 break;
11784 case PRAGMA_OMP_CLAUSE_DEPEND:
11785 clauses = c_parser_omp_clause_depend (parser, clauses);
11786 c_name = "depend";
11787 break;
11788 case PRAGMA_OMP_CLAUSE_MAP:
11789 clauses = c_parser_omp_clause_map (parser, clauses);
11790 c_name = "map";
11791 break;
11792 case PRAGMA_OMP_CLAUSE_DEVICE:
11793 clauses = c_parser_omp_clause_device (parser, clauses);
11794 c_name = "device";
11795 break;
11796 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
11797 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
11798 c_name = "dist_schedule";
11799 break;
11800 case PRAGMA_OMP_CLAUSE_PROC_BIND:
11801 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
11802 c_name = "proc_bind";
11803 break;
11804 case PRAGMA_OMP_CLAUSE_SAFELEN:
11805 clauses = c_parser_omp_clause_safelen (parser, clauses);
11806 c_name = "safelen";
11807 break;
11808 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
11809 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
11810 c_name = "simdlen";
11811 break;
11812 case PRAGMA_OMP_CLAUSE_SIMDLEN:
11813 clauses = c_parser_omp_clause_simdlen (parser, clauses);
11814 c_name = "simdlen";
11815 break;
11816 default:
11817 c_parser_error (parser, "expected clause");
11818 goto saw_error;
11821 first = false;
11823 if (((mask >> c_kind) & 1) == 0 && !parser->error)
11825 /* Remove the invalid clause(s) from the list to avoid
11826 confusing the rest of the compiler. */
11827 clauses = prev;
11828 error_at (here, "%qs is not valid for %qs", c_name, where);
11832 saw_error:
11833 c_parser_skip_to_pragma_eol (parser);
11835 if (finish_p)
11836 return c_finish_omp_clauses (clauses);
11838 return clauses;
11841 /* OpenACC 2.0, OpenMP 2.5:
11842 structured-block:
11843 statement
11845 In practice, we're also interested in adding the statement to an
11846 outer node. So it is convenient if we work around the fact that
11847 c_parser_statement calls add_stmt. */
11849 static tree
11850 c_parser_omp_structured_block (c_parser *parser)
11852 tree stmt = push_stmt_list ();
11853 c_parser_statement (parser);
11854 return pop_stmt_list (stmt);
11857 /* OpenACC 2.0:
11858 # pragma acc data oacc-data-clause[optseq] new-line
11859 structured-block
11861 LOC is the location of the #pragma token.
11864 #define OACC_DATA_CLAUSE_MASK \
11865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
11866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
11868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
11869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
11870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
11872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
11873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
11874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
11875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) )
11877 static tree
11878 c_parser_oacc_data (location_t loc, c_parser *parser)
11880 tree stmt, clauses, block;
11882 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
11883 "#pragma acc data");
11885 block = c_begin_omp_parallel ();
11886 add_stmt (c_parser_omp_structured_block (parser));
11888 stmt = c_finish_oacc_data (loc, clauses, block);
11890 return stmt;
11893 /* OpenACC 2.0:
11894 # pragma acc kernels oacc-kernels-clause[optseq] new-line
11895 structured-block
11897 LOC is the location of the #pragma token.
11900 #define OACC_KERNELS_CLAUSE_MASK \
11901 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
11902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
11903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
11905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
11906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
11907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
11909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
11910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
11911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
11912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
11913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
11915 static tree
11916 c_parser_oacc_kernels (location_t loc, c_parser *parser, char *p_name)
11918 tree stmt, clauses = NULL_TREE, block;
11920 strcat (p_name, " kernels");
11922 if (c_parser_next_token_is (parser, CPP_NAME))
11924 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11925 if (strcmp (p, "loop") == 0)
11927 c_parser_consume_token (parser);
11928 block = c_begin_omp_parallel ();
11929 c_parser_oacc_loop (loc, parser, p_name);
11930 stmt = c_finish_oacc_kernels (loc, clauses, block);
11931 OACC_KERNELS_COMBINED (stmt) = 1;
11932 return stmt;
11936 clauses = c_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
11937 p_name);
11939 block = c_begin_omp_parallel ();
11940 add_stmt (c_parser_omp_structured_block (parser));
11942 stmt = c_finish_oacc_kernels (loc, clauses, block);
11944 return stmt;
11947 /* OpenACC 2.0:
11948 # pragma acc loop oacc-loop-clause[optseq] new-line
11949 structured-block
11951 LOC is the location of the #pragma token.
11954 #define OACC_LOOP_CLAUSE_MASK \
11955 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
11956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) )
11958 static tree
11959 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name)
11961 tree stmt, clauses, block;
11963 strcat (p_name, " loop");
11965 clauses = c_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK, p_name);
11967 block = c_begin_compound_stmt (true);
11968 stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
11969 block = c_end_compound_stmt (loc, block, true);
11970 add_stmt (block);
11972 return stmt;
11975 /* OpenACC 2.0:
11976 # pragma acc parallel oacc-parallel-clause[optseq] new-line
11977 structured-block
11979 LOC is the location of the #pragma token.
11982 #define OACC_PARALLEL_CLAUSE_MASK \
11983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
11984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPY) \
11985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
11986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYOUT) \
11987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_CREATE) \
11988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICEPTR) \
11989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
11990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_GANGS) \
11991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_WORKERS) \
11992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT) \
11993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPY) \
11994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYIN) \
11995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_COPYOUT) \
11996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRESENT_OR_CREATE) \
11997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
11998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_VECTOR_LENGTH) \
11999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
12001 static tree
12002 c_parser_oacc_parallel (location_t loc, c_parser *parser, char *p_name)
12004 tree stmt, clauses = NULL_TREE, block;
12006 strcat (p_name, " parallel");
12008 if (c_parser_next_token_is (parser, CPP_NAME))
12010 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12011 if (strcmp (p, "loop") == 0)
12013 c_parser_consume_token (parser);
12014 block = c_begin_omp_parallel ();
12015 c_parser_oacc_loop (loc, parser, p_name);
12016 stmt = c_finish_oacc_parallel (loc, clauses, block);
12017 OACC_PARALLEL_COMBINED (stmt) = 1;
12018 return stmt;
12022 clauses = c_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
12023 p_name);
12025 block = c_begin_omp_parallel ();
12026 add_stmt (c_parser_omp_structured_block (parser));
12028 stmt = c_finish_oacc_parallel (loc, clauses, block);
12030 return stmt;
12033 /* OpenACC 2.0:
12034 # pragma acc update oacc-update-clause[optseq] new-line
12037 #define OACC_UPDATE_CLAUSE_MASK \
12038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) \
12039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
12040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HOST) \
12041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
12042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SELF) \
12043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_WAIT) )
12045 static void
12046 c_parser_oacc_update (c_parser *parser)
12048 location_t loc = c_parser_peek_token (parser)->location;
12050 c_parser_consume_pragma (parser);
12052 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
12053 "#pragma acc update");
12054 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
12056 error_at (loc,
12057 "%<#pragma acc update%> must contain at least one "
12058 "%<device%> or %<host/self%> clause");
12059 return;
12062 if (parser->error)
12063 return;
12065 tree stmt = make_node (OACC_UPDATE);
12066 TREE_TYPE (stmt) = void_type_node;
12067 OACC_UPDATE_CLAUSES (stmt) = clauses;
12068 SET_EXPR_LOCATION (stmt, loc);
12069 add_stmt (stmt);
12072 /* OpenACC 2.0:
12073 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
12075 LOC is the location of the #pragma token.
12078 #define OACC_WAIT_CLAUSE_MASK \
12079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ASYNC) )
12081 static tree
12082 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
12084 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
12086 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12087 list = c_parser_oacc_wait_list (parser, loc, list);
12089 strcpy (p_name, " wait");
12090 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
12091 stmt = c_finish_oacc_wait (loc, list, clauses);
12093 return stmt;
12096 /* OpenMP 2.5:
12097 # pragma omp atomic new-line
12098 expression-stmt
12100 expression-stmt:
12101 x binop= expr | x++ | ++x | x-- | --x
12102 binop:
12103 +, *, -, /, &, ^, |, <<, >>
12105 where x is an lvalue expression with scalar type.
12107 OpenMP 3.1:
12108 # pragma omp atomic new-line
12109 update-stmt
12111 # pragma omp atomic read new-line
12112 read-stmt
12114 # pragma omp atomic write new-line
12115 write-stmt
12117 # pragma omp atomic update new-line
12118 update-stmt
12120 # pragma omp atomic capture new-line
12121 capture-stmt
12123 # pragma omp atomic capture new-line
12124 capture-block
12126 read-stmt:
12127 v = x
12128 write-stmt:
12129 x = expr
12130 update-stmt:
12131 expression-stmt | x = x binop expr
12132 capture-stmt:
12133 v = expression-stmt
12134 capture-block:
12135 { v = x; update-stmt; } | { update-stmt; v = x; }
12137 OpenMP 4.0:
12138 update-stmt:
12139 expression-stmt | x = x binop expr | x = expr binop x
12140 capture-stmt:
12141 v = update-stmt
12142 capture-block:
12143 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
12145 where x and v are lvalue expressions with scalar type.
12147 LOC is the location of the #pragma token. */
12149 static void
12150 c_parser_omp_atomic (location_t loc, c_parser *parser)
12152 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
12153 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
12154 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
12155 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
12156 struct c_expr expr;
12157 location_t eloc;
12158 bool structured_block = false;
12159 bool swapped = false;
12160 bool seq_cst = false;
12162 if (c_parser_next_token_is (parser, CPP_NAME))
12164 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12165 if (!strcmp (p, "seq_cst"))
12167 seq_cst = true;
12168 c_parser_consume_token (parser);
12169 if (c_parser_next_token_is (parser, CPP_COMMA)
12170 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12171 c_parser_consume_token (parser);
12174 if (c_parser_next_token_is (parser, CPP_NAME))
12176 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12178 if (!strcmp (p, "read"))
12179 code = OMP_ATOMIC_READ;
12180 else if (!strcmp (p, "write"))
12181 code = NOP_EXPR;
12182 else if (!strcmp (p, "update"))
12183 code = OMP_ATOMIC;
12184 else if (!strcmp (p, "capture"))
12185 code = OMP_ATOMIC_CAPTURE_NEW;
12186 else
12187 p = NULL;
12188 if (p)
12189 c_parser_consume_token (parser);
12191 if (!seq_cst)
12193 if (c_parser_next_token_is (parser, CPP_COMMA)
12194 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12195 c_parser_consume_token (parser);
12197 if (c_parser_next_token_is (parser, CPP_NAME))
12199 const char *p
12200 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12201 if (!strcmp (p, "seq_cst"))
12203 seq_cst = true;
12204 c_parser_consume_token (parser);
12208 c_parser_skip_to_pragma_eol (parser);
12210 switch (code)
12212 case OMP_ATOMIC_READ:
12213 case NOP_EXPR: /* atomic write */
12214 v = c_parser_unary_expression (parser).value;
12215 v = c_fully_fold (v, false, NULL);
12216 if (v == error_mark_node)
12217 goto saw_error;
12218 loc = c_parser_peek_token (parser)->location;
12219 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12220 goto saw_error;
12221 if (code == NOP_EXPR)
12222 lhs = c_parser_expression (parser).value;
12223 else
12224 lhs = c_parser_unary_expression (parser).value;
12225 lhs = c_fully_fold (lhs, false, NULL);
12226 if (lhs == error_mark_node)
12227 goto saw_error;
12228 if (code == NOP_EXPR)
12230 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
12231 opcode. */
12232 code = OMP_ATOMIC;
12233 rhs = lhs;
12234 lhs = v;
12235 v = NULL_TREE;
12237 goto done;
12238 case OMP_ATOMIC_CAPTURE_NEW:
12239 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12241 c_parser_consume_token (parser);
12242 structured_block = true;
12244 else
12246 v = c_parser_unary_expression (parser).value;
12247 v = c_fully_fold (v, false, NULL);
12248 if (v == error_mark_node)
12249 goto saw_error;
12250 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12251 goto saw_error;
12253 break;
12254 default:
12255 break;
12258 /* For structured_block case we don't know yet whether
12259 old or new x should be captured. */
12260 restart:
12261 eloc = c_parser_peek_token (parser)->location;
12262 expr = c_parser_unary_expression (parser);
12263 lhs = expr.value;
12264 expr = default_function_array_conversion (eloc, expr);
12265 unfolded_lhs = expr.value;
12266 lhs = c_fully_fold (lhs, false, NULL);
12267 orig_lhs = lhs;
12268 switch (TREE_CODE (lhs))
12270 case ERROR_MARK:
12271 saw_error:
12272 c_parser_skip_to_end_of_block_or_statement (parser);
12273 if (structured_block)
12275 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12276 c_parser_consume_token (parser);
12277 else if (code == OMP_ATOMIC_CAPTURE_NEW)
12279 c_parser_skip_to_end_of_block_or_statement (parser);
12280 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12281 c_parser_consume_token (parser);
12284 return;
12286 case POSTINCREMENT_EXPR:
12287 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12288 code = OMP_ATOMIC_CAPTURE_OLD;
12289 /* FALLTHROUGH */
12290 case PREINCREMENT_EXPR:
12291 lhs = TREE_OPERAND (lhs, 0);
12292 unfolded_lhs = NULL_TREE;
12293 opcode = PLUS_EXPR;
12294 rhs = integer_one_node;
12295 break;
12297 case POSTDECREMENT_EXPR:
12298 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
12299 code = OMP_ATOMIC_CAPTURE_OLD;
12300 /* FALLTHROUGH */
12301 case PREDECREMENT_EXPR:
12302 lhs = TREE_OPERAND (lhs, 0);
12303 unfolded_lhs = NULL_TREE;
12304 opcode = MINUS_EXPR;
12305 rhs = integer_one_node;
12306 break;
12308 case COMPOUND_EXPR:
12309 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
12310 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
12311 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
12312 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
12313 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
12314 (TREE_OPERAND (lhs, 1), 0), 0)))
12315 == BOOLEAN_TYPE)
12316 /* Undo effects of boolean_increment for post {in,de}crement. */
12317 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
12318 /* FALLTHRU */
12319 case MODIFY_EXPR:
12320 if (TREE_CODE (lhs) == MODIFY_EXPR
12321 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
12323 /* Undo effects of boolean_increment. */
12324 if (integer_onep (TREE_OPERAND (lhs, 1)))
12326 /* This is pre or post increment. */
12327 rhs = TREE_OPERAND (lhs, 1);
12328 lhs = TREE_OPERAND (lhs, 0);
12329 unfolded_lhs = NULL_TREE;
12330 opcode = NOP_EXPR;
12331 if (code == OMP_ATOMIC_CAPTURE_NEW
12332 && !structured_block
12333 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12334 code = OMP_ATOMIC_CAPTURE_OLD;
12335 break;
12337 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
12338 && TREE_OPERAND (lhs, 0)
12339 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
12341 /* This is pre or post decrement. */
12342 rhs = TREE_OPERAND (lhs, 1);
12343 lhs = TREE_OPERAND (lhs, 0);
12344 unfolded_lhs = NULL_TREE;
12345 opcode = NOP_EXPR;
12346 if (code == OMP_ATOMIC_CAPTURE_NEW
12347 && !structured_block
12348 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
12349 code = OMP_ATOMIC_CAPTURE_OLD;
12350 break;
12353 /* FALLTHRU */
12354 default:
12355 switch (c_parser_peek_token (parser)->type)
12357 case CPP_MULT_EQ:
12358 opcode = MULT_EXPR;
12359 break;
12360 case CPP_DIV_EQ:
12361 opcode = TRUNC_DIV_EXPR;
12362 break;
12363 case CPP_PLUS_EQ:
12364 opcode = PLUS_EXPR;
12365 break;
12366 case CPP_MINUS_EQ:
12367 opcode = MINUS_EXPR;
12368 break;
12369 case CPP_LSHIFT_EQ:
12370 opcode = LSHIFT_EXPR;
12371 break;
12372 case CPP_RSHIFT_EQ:
12373 opcode = RSHIFT_EXPR;
12374 break;
12375 case CPP_AND_EQ:
12376 opcode = BIT_AND_EXPR;
12377 break;
12378 case CPP_OR_EQ:
12379 opcode = BIT_IOR_EXPR;
12380 break;
12381 case CPP_XOR_EQ:
12382 opcode = BIT_XOR_EXPR;
12383 break;
12384 case CPP_EQ:
12385 c_parser_consume_token (parser);
12386 eloc = c_parser_peek_token (parser)->location;
12387 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
12388 rhs1 = expr.value;
12389 switch (TREE_CODE (rhs1))
12391 case MULT_EXPR:
12392 case TRUNC_DIV_EXPR:
12393 case PLUS_EXPR:
12394 case MINUS_EXPR:
12395 case LSHIFT_EXPR:
12396 case RSHIFT_EXPR:
12397 case BIT_AND_EXPR:
12398 case BIT_IOR_EXPR:
12399 case BIT_XOR_EXPR:
12400 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
12402 opcode = TREE_CODE (rhs1);
12403 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12404 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12405 goto stmt_done;
12407 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
12409 opcode = TREE_CODE (rhs1);
12410 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
12411 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
12412 swapped = !commutative_tree_code (opcode);
12413 goto stmt_done;
12415 break;
12416 case ERROR_MARK:
12417 goto saw_error;
12418 default:
12419 break;
12421 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
12423 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12425 code = OMP_ATOMIC_CAPTURE_OLD;
12426 v = lhs;
12427 lhs = NULL_TREE;
12428 expr = default_function_array_read_conversion (eloc, expr);
12429 unfolded_lhs1 = expr.value;
12430 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
12431 rhs1 = NULL_TREE;
12432 c_parser_consume_token (parser);
12433 goto restart;
12435 if (structured_block)
12437 opcode = NOP_EXPR;
12438 expr = default_function_array_read_conversion (eloc, expr);
12439 rhs = c_fully_fold (expr.value, false, NULL);
12440 rhs1 = NULL_TREE;
12441 goto stmt_done;
12444 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
12445 goto saw_error;
12446 default:
12447 c_parser_error (parser,
12448 "invalid operator for %<#pragma omp atomic%>");
12449 goto saw_error;
12452 /* Arrange to pass the location of the assignment operator to
12453 c_finish_omp_atomic. */
12454 loc = c_parser_peek_token (parser)->location;
12455 c_parser_consume_token (parser);
12456 eloc = c_parser_peek_token (parser)->location;
12457 expr = c_parser_expression (parser);
12458 expr = default_function_array_read_conversion (eloc, expr);
12459 rhs = expr.value;
12460 rhs = c_fully_fold (rhs, false, NULL);
12461 break;
12463 stmt_done:
12464 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
12466 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
12467 goto saw_error;
12468 v = c_parser_unary_expression (parser).value;
12469 v = c_fully_fold (v, false, NULL);
12470 if (v == error_mark_node)
12471 goto saw_error;
12472 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
12473 goto saw_error;
12474 eloc = c_parser_peek_token (parser)->location;
12475 expr = c_parser_unary_expression (parser);
12476 lhs1 = expr.value;
12477 expr = default_function_array_read_conversion (eloc, expr);
12478 unfolded_lhs1 = expr.value;
12479 lhs1 = c_fully_fold (lhs1, false, NULL);
12480 if (lhs1 == error_mark_node)
12481 goto saw_error;
12483 if (structured_block)
12485 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12486 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
12488 done:
12489 if (unfolded_lhs && unfolded_lhs1
12490 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
12492 error ("%<#pragma omp atomic capture%> uses two different "
12493 "expressions for memory");
12494 stmt = error_mark_node;
12496 else
12497 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
12498 swapped, seq_cst);
12499 if (stmt != error_mark_node)
12500 add_stmt (stmt);
12502 if (!structured_block)
12503 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12507 /* OpenMP 2.5:
12508 # pragma omp barrier new-line
12511 static void
12512 c_parser_omp_barrier (c_parser *parser)
12514 location_t loc = c_parser_peek_token (parser)->location;
12515 c_parser_consume_pragma (parser);
12516 c_parser_skip_to_pragma_eol (parser);
12518 c_finish_omp_barrier (loc);
12521 /* OpenMP 2.5:
12522 # pragma omp critical [(name)] new-line
12523 structured-block
12525 LOC is the location of the #pragma itself. */
12527 static tree
12528 c_parser_omp_critical (location_t loc, c_parser *parser)
12530 tree stmt, name = NULL;
12532 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12534 c_parser_consume_token (parser);
12535 if (c_parser_next_token_is (parser, CPP_NAME))
12537 name = c_parser_peek_token (parser)->value;
12538 c_parser_consume_token (parser);
12539 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12541 else
12542 c_parser_error (parser, "expected identifier");
12544 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12545 c_parser_error (parser, "expected %<(%> or end of line");
12546 c_parser_skip_to_pragma_eol (parser);
12548 stmt = c_parser_omp_structured_block (parser);
12549 return c_finish_omp_critical (loc, stmt, name);
12552 /* OpenMP 2.5:
12553 # pragma omp flush flush-vars[opt] new-line
12555 flush-vars:
12556 ( variable-list ) */
12558 static void
12559 c_parser_omp_flush (c_parser *parser)
12561 location_t loc = c_parser_peek_token (parser)->location;
12562 c_parser_consume_pragma (parser);
12563 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12564 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
12565 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12566 c_parser_error (parser, "expected %<(%> or end of line");
12567 c_parser_skip_to_pragma_eol (parser);
12569 c_finish_omp_flush (loc);
12572 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
12573 The real trick here is to determine the loop control variable early
12574 so that we can push a new decl if necessary to make it private.
12575 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
12576 respectively. */
12578 static tree
12579 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
12580 tree clauses, tree *cclauses)
12582 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
12583 tree declv, condv, incrv, initv, ret = NULL;
12584 bool fail = false, open_brace_parsed = false;
12585 int i, collapse = 1, nbraces = 0;
12586 location_t for_loc;
12587 vec<tree, va_gc> *for_block = make_tree_vector ();
12589 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
12590 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
12591 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
12593 gcc_assert (collapse >= 1);
12595 declv = make_tree_vec (collapse);
12596 initv = make_tree_vec (collapse);
12597 condv = make_tree_vec (collapse);
12598 incrv = make_tree_vec (collapse);
12600 if (code != CILK_FOR
12601 && !c_parser_next_token_is_keyword (parser, RID_FOR))
12603 c_parser_error (parser, "for statement expected");
12604 return NULL;
12606 if (code == CILK_FOR
12607 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
12609 c_parser_error (parser, "_Cilk_for statement expected");
12610 return NULL;
12612 for_loc = c_parser_peek_token (parser)->location;
12613 c_parser_consume_token (parser);
12615 for (i = 0; i < collapse; i++)
12617 int bracecount = 0;
12619 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12620 goto pop_scopes;
12622 /* Parse the initialization declaration or expression. */
12623 if (c_parser_next_tokens_start_declaration (parser))
12625 if (i > 0)
12626 vec_safe_push (for_block, c_begin_compound_stmt (true));
12627 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
12628 NULL, vNULL);
12629 decl = check_for_loop_decls (for_loc, flag_isoc99);
12630 if (decl == NULL)
12631 goto error_init;
12632 if (DECL_INITIAL (decl) == error_mark_node)
12633 decl = error_mark_node;
12634 init = decl;
12636 else if (c_parser_next_token_is (parser, CPP_NAME)
12637 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
12639 struct c_expr decl_exp;
12640 struct c_expr init_exp;
12641 location_t init_loc;
12643 decl_exp = c_parser_postfix_expression (parser);
12644 decl = decl_exp.value;
12646 c_parser_require (parser, CPP_EQ, "expected %<=%>");
12648 init_loc = c_parser_peek_token (parser)->location;
12649 init_exp = c_parser_expr_no_commas (parser, NULL);
12650 init_exp = default_function_array_read_conversion (init_loc,
12651 init_exp);
12652 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
12653 NOP_EXPR, init_loc, init_exp.value,
12654 init_exp.original_type);
12655 init = c_process_expr_stmt (init_loc, init);
12657 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12659 else
12661 error_init:
12662 c_parser_error (parser,
12663 "expected iteration declaration or initialization");
12664 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12665 "expected %<)%>");
12666 fail = true;
12667 goto parse_next;
12670 /* Parse the loop condition. */
12671 cond = NULL_TREE;
12672 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
12674 location_t cond_loc = c_parser_peek_token (parser)->location;
12675 struct c_expr cond_expr
12676 = c_parser_binary_expression (parser, NULL, NULL_TREE);
12678 cond = cond_expr.value;
12679 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
12680 cond = c_fully_fold (cond, false, NULL);
12681 switch (cond_expr.original_code)
12683 case GT_EXPR:
12684 case GE_EXPR:
12685 case LT_EXPR:
12686 case LE_EXPR:
12687 break;
12688 case NE_EXPR:
12689 if (code == CILK_SIMD || code == CILK_FOR)
12690 break;
12691 /* FALLTHRU. */
12692 default:
12693 /* Can't be cond = error_mark_node, because we want to preserve
12694 the location until c_finish_omp_for. */
12695 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
12696 break;
12698 protected_set_expr_location (cond, cond_loc);
12700 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
12702 /* Parse the increment expression. */
12703 incr = NULL_TREE;
12704 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
12706 location_t incr_loc = c_parser_peek_token (parser)->location;
12708 incr = c_process_expr_stmt (incr_loc,
12709 c_parser_expression (parser).value);
12711 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12713 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
12714 fail = true;
12715 else
12717 TREE_VEC_ELT (declv, i) = decl;
12718 TREE_VEC_ELT (initv, i) = init;
12719 TREE_VEC_ELT (condv, i) = cond;
12720 TREE_VEC_ELT (incrv, i) = incr;
12723 parse_next:
12724 if (i == collapse - 1)
12725 break;
12727 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
12728 in between the collapsed for loops to be still considered perfectly
12729 nested. Hopefully the final version clarifies this.
12730 For now handle (multiple) {'s and empty statements. */
12733 if (c_parser_next_token_is_keyword (parser, RID_FOR))
12735 c_parser_consume_token (parser);
12736 break;
12738 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
12740 c_parser_consume_token (parser);
12741 bracecount++;
12743 else if (bracecount
12744 && c_parser_next_token_is (parser, CPP_SEMICOLON))
12745 c_parser_consume_token (parser);
12746 else
12748 c_parser_error (parser, "not enough perfectly nested loops");
12749 if (bracecount)
12751 open_brace_parsed = true;
12752 bracecount--;
12754 fail = true;
12755 collapse = 0;
12756 break;
12759 while (1);
12761 nbraces += bracecount;
12764 save_break = c_break_label;
12765 if (code == CILK_SIMD)
12766 c_break_label = build_int_cst (size_type_node, 2);
12767 else
12768 c_break_label = size_one_node;
12769 save_cont = c_cont_label;
12770 c_cont_label = NULL_TREE;
12771 body = push_stmt_list ();
12773 if (open_brace_parsed)
12775 location_t here = c_parser_peek_token (parser)->location;
12776 stmt = c_begin_compound_stmt (true);
12777 c_parser_compound_statement_nostart (parser);
12778 add_stmt (c_end_compound_stmt (here, stmt, true));
12780 else
12781 add_stmt (c_parser_c99_block_statement (parser));
12782 if (c_cont_label)
12784 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
12785 SET_EXPR_LOCATION (t, loc);
12786 add_stmt (t);
12789 body = pop_stmt_list (body);
12790 c_break_label = save_break;
12791 c_cont_label = save_cont;
12793 while (nbraces)
12795 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
12797 c_parser_consume_token (parser);
12798 nbraces--;
12800 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
12801 c_parser_consume_token (parser);
12802 else
12804 c_parser_error (parser, "collapsed loops not perfectly nested");
12805 while (nbraces)
12807 location_t here = c_parser_peek_token (parser)->location;
12808 stmt = c_begin_compound_stmt (true);
12809 add_stmt (body);
12810 c_parser_compound_statement_nostart (parser);
12811 body = c_end_compound_stmt (here, stmt, true);
12812 nbraces--;
12814 goto pop_scopes;
12818 /* Only bother calling c_finish_omp_for if we haven't already generated
12819 an error from the initialization parsing. */
12820 if (!fail)
12822 stmt = c_finish_omp_for (loc, code, declv, initv, condv,
12823 incrv, body, NULL);
12824 if (stmt)
12826 if (cclauses != NULL
12827 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
12829 gcc_assert (code != OACC_LOOP);
12830 tree *c;
12831 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
12832 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
12833 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
12834 c = &OMP_CLAUSE_CHAIN (*c);
12835 else
12837 for (i = 0; i < collapse; i++)
12838 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
12839 break;
12840 if (i == collapse)
12841 c = &OMP_CLAUSE_CHAIN (*c);
12842 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
12844 error_at (loc,
12845 "iteration variable %qD should not be firstprivate",
12846 OMP_CLAUSE_DECL (*c));
12847 *c = OMP_CLAUSE_CHAIN (*c);
12849 else
12851 /* Copy lastprivate (decl) clause to OMP_FOR_CLAUSES,
12852 change it to shared (decl) in
12853 OMP_PARALLEL_CLAUSES. */
12854 tree l = build_omp_clause (OMP_CLAUSE_LOCATION (*c),
12855 OMP_CLAUSE_LASTPRIVATE);
12856 OMP_CLAUSE_DECL (l) = OMP_CLAUSE_DECL (*c);
12857 if (code == OMP_SIMD)
12859 OMP_CLAUSE_CHAIN (l)
12860 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12861 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
12863 else
12865 OMP_CLAUSE_CHAIN (l) = clauses;
12866 clauses = l;
12868 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
12872 OMP_FOR_CLAUSES (stmt) = clauses;
12874 ret = stmt;
12876 pop_scopes:
12877 while (!for_block->is_empty ())
12879 /* FIXME diagnostics: LOC below should be the actual location of
12880 this particular for block. We need to build a list of
12881 locations to go along with FOR_BLOCK. */
12882 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
12883 add_stmt (stmt);
12885 release_tree_vector (for_block);
12886 return ret;
12889 /* Helper function for OpenMP parsing, split clauses and call
12890 finish_omp_clauses on each of the set of clauses afterwards. */
12892 static void
12893 omp_split_clauses (location_t loc, enum tree_code code,
12894 omp_clause_mask mask, tree clauses, tree *cclauses)
12896 int i;
12897 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
12898 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
12899 if (cclauses[i])
12900 cclauses[i] = c_finish_omp_clauses (cclauses[i]);
12903 /* OpenMP 4.0:
12904 #pragma omp simd simd-clause[optseq] new-line
12905 for-loop
12907 LOC is the location of the #pragma token.
12910 #define OMP_SIMD_CLAUSE_MASK \
12911 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
12912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
12913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
12914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
12919 static tree
12920 c_parser_omp_simd (location_t loc, c_parser *parser,
12921 char *p_name, omp_clause_mask mask, tree *cclauses)
12923 tree block, clauses, ret;
12925 strcat (p_name, " simd");
12926 mask |= OMP_SIMD_CLAUSE_MASK;
12927 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
12929 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
12930 if (cclauses)
12932 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
12933 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
12936 block = c_begin_compound_stmt (true);
12937 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
12938 block = c_end_compound_stmt (loc, block, true);
12939 add_stmt (block);
12941 return ret;
12944 /* OpenMP 2.5:
12945 #pragma omp for for-clause[optseq] new-line
12946 for-loop
12948 OpenMP 4.0:
12949 #pragma omp for simd for-simd-clause[optseq] new-line
12950 for-loop
12952 LOC is the location of the #pragma token.
12955 #define OMP_FOR_CLAUSE_MASK \
12956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
12957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
12958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
12959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
12960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
12961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
12962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
12963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
12965 static tree
12966 c_parser_omp_for (location_t loc, c_parser *parser,
12967 char *p_name, omp_clause_mask mask, tree *cclauses)
12969 tree block, clauses, ret;
12971 strcat (p_name, " for");
12972 mask |= OMP_FOR_CLAUSE_MASK;
12973 if (cclauses)
12974 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
12976 if (c_parser_next_token_is (parser, CPP_NAME))
12978 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12980 if (strcmp (p, "simd") == 0)
12982 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
12983 if (cclauses == NULL)
12984 cclauses = cclauses_buf;
12986 c_parser_consume_token (parser);
12987 if (!flag_openmp) /* flag_openmp_simd */
12988 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12989 block = c_begin_compound_stmt (true);
12990 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
12991 block = c_end_compound_stmt (loc, block, true);
12992 if (ret == NULL_TREE)
12993 return ret;
12994 ret = make_node (OMP_FOR);
12995 TREE_TYPE (ret) = void_type_node;
12996 OMP_FOR_BODY (ret) = block;
12997 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
12998 SET_EXPR_LOCATION (ret, loc);
12999 add_stmt (ret);
13000 return ret;
13003 if (!flag_openmp) /* flag_openmp_simd */
13005 c_parser_skip_to_pragma_eol (parser);
13006 return NULL_TREE;
13009 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13010 if (cclauses)
13012 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
13013 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
13016 block = c_begin_compound_stmt (true);
13017 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
13018 block = c_end_compound_stmt (loc, block, true);
13019 add_stmt (block);
13021 return ret;
13024 /* OpenMP 2.5:
13025 # pragma omp master new-line
13026 structured-block
13028 LOC is the location of the #pragma token.
13031 static tree
13032 c_parser_omp_master (location_t loc, c_parser *parser)
13034 c_parser_skip_to_pragma_eol (parser);
13035 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
13038 /* OpenMP 2.5:
13039 # pragma omp ordered new-line
13040 structured-block
13042 LOC is the location of the #pragma itself.
13045 static tree
13046 c_parser_omp_ordered (location_t loc, c_parser *parser)
13048 c_parser_skip_to_pragma_eol (parser);
13049 return c_finish_omp_ordered (loc, c_parser_omp_structured_block (parser));
13052 /* OpenMP 2.5:
13054 section-scope:
13055 { section-sequence }
13057 section-sequence:
13058 section-directive[opt] structured-block
13059 section-sequence section-directive structured-block
13061 SECTIONS_LOC is the location of the #pragma omp sections. */
13063 static tree
13064 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
13066 tree stmt, substmt;
13067 bool error_suppress = false;
13068 location_t loc;
13070 loc = c_parser_peek_token (parser)->location;
13071 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
13073 /* Avoid skipping until the end of the block. */
13074 parser->error = false;
13075 return NULL_TREE;
13078 stmt = push_stmt_list ();
13080 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
13082 substmt = c_parser_omp_structured_block (parser);
13083 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13084 SET_EXPR_LOCATION (substmt, loc);
13085 add_stmt (substmt);
13088 while (1)
13090 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
13091 break;
13092 if (c_parser_next_token_is (parser, CPP_EOF))
13093 break;
13095 loc = c_parser_peek_token (parser)->location;
13096 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
13098 c_parser_consume_pragma (parser);
13099 c_parser_skip_to_pragma_eol (parser);
13100 error_suppress = false;
13102 else if (!error_suppress)
13104 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
13105 error_suppress = true;
13108 substmt = c_parser_omp_structured_block (parser);
13109 substmt = build1 (OMP_SECTION, void_type_node, substmt);
13110 SET_EXPR_LOCATION (substmt, loc);
13111 add_stmt (substmt);
13113 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
13114 "expected %<#pragma omp section%> or %<}%>");
13116 substmt = pop_stmt_list (stmt);
13118 stmt = make_node (OMP_SECTIONS);
13119 SET_EXPR_LOCATION (stmt, sections_loc);
13120 TREE_TYPE (stmt) = void_type_node;
13121 OMP_SECTIONS_BODY (stmt) = substmt;
13123 return add_stmt (stmt);
13126 /* OpenMP 2.5:
13127 # pragma omp sections sections-clause[optseq] newline
13128 sections-scope
13130 LOC is the location of the #pragma token.
13133 #define OMP_SECTIONS_CLAUSE_MASK \
13134 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
13137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13140 static tree
13141 c_parser_omp_sections (location_t loc, c_parser *parser,
13142 char *p_name, omp_clause_mask mask, tree *cclauses)
13144 tree block, clauses, ret;
13146 strcat (p_name, " sections");
13147 mask |= OMP_SECTIONS_CLAUSE_MASK;
13148 if (cclauses)
13149 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
13151 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13152 if (cclauses)
13154 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
13155 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
13158 block = c_begin_compound_stmt (true);
13159 ret = c_parser_omp_sections_scope (loc, parser);
13160 if (ret)
13161 OMP_SECTIONS_CLAUSES (ret) = clauses;
13162 block = c_end_compound_stmt (loc, block, true);
13163 add_stmt (block);
13165 return ret;
13168 /* OpenMP 2.5:
13169 # pragma omp parallel parallel-clause[optseq] new-line
13170 structured-block
13171 # pragma omp parallel for parallel-for-clause[optseq] new-line
13172 structured-block
13173 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
13174 structured-block
13176 OpenMP 4.0:
13177 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
13178 structured-block
13180 LOC is the location of the #pragma token.
13183 #define OMP_PARALLEL_CLAUSE_MASK \
13184 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
13190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
13192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
13194 static tree
13195 c_parser_omp_parallel (location_t loc, c_parser *parser,
13196 char *p_name, omp_clause_mask mask, tree *cclauses)
13198 tree stmt, clauses, block;
13200 strcat (p_name, " parallel");
13201 mask |= OMP_PARALLEL_CLAUSE_MASK;
13203 if (c_parser_next_token_is_keyword (parser, RID_FOR))
13205 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13206 if (cclauses == NULL)
13207 cclauses = cclauses_buf;
13209 c_parser_consume_token (parser);
13210 if (!flag_openmp) /* flag_openmp_simd */
13211 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13212 block = c_begin_omp_parallel ();
13213 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
13214 stmt
13215 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13216 block);
13217 if (ret == NULL_TREE)
13218 return ret;
13219 OMP_PARALLEL_COMBINED (stmt) = 1;
13220 return stmt;
13222 else if (cclauses)
13224 error_at (loc, "expected %<for%> after %qs", p_name);
13225 c_parser_skip_to_pragma_eol (parser);
13226 return NULL_TREE;
13228 else if (!flag_openmp) /* flag_openmp_simd */
13230 c_parser_skip_to_pragma_eol (parser);
13231 return NULL_TREE;
13233 else if (c_parser_next_token_is (parser, CPP_NAME))
13235 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13236 if (strcmp (p, "sections") == 0)
13238 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13239 if (cclauses == NULL)
13240 cclauses = cclauses_buf;
13242 c_parser_consume_token (parser);
13243 block = c_begin_omp_parallel ();
13244 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
13245 stmt = c_finish_omp_parallel (loc,
13246 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
13247 block);
13248 OMP_PARALLEL_COMBINED (stmt) = 1;
13249 return stmt;
13253 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13255 block = c_begin_omp_parallel ();
13256 c_parser_statement (parser);
13257 stmt = c_finish_omp_parallel (loc, clauses, block);
13259 return stmt;
13262 /* OpenMP 2.5:
13263 # pragma omp single single-clause[optseq] new-line
13264 structured-block
13266 LOC is the location of the #pragma.
13269 #define OMP_SINGLE_CLAUSE_MASK \
13270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
13273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
13275 static tree
13276 c_parser_omp_single (location_t loc, c_parser *parser)
13278 tree stmt = make_node (OMP_SINGLE);
13279 SET_EXPR_LOCATION (stmt, loc);
13280 TREE_TYPE (stmt) = void_type_node;
13282 OMP_SINGLE_CLAUSES (stmt)
13283 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
13284 "#pragma omp single");
13285 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
13287 return add_stmt (stmt);
13290 /* OpenMP 3.0:
13291 # pragma omp task task-clause[optseq] new-line
13293 LOC is the location of the #pragma.
13296 #define OMP_TASK_CLAUSE_MASK \
13297 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
13298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
13299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
13300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
13304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
13305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
13307 static tree
13308 c_parser_omp_task (location_t loc, c_parser *parser)
13310 tree clauses, block;
13312 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
13313 "#pragma omp task");
13315 block = c_begin_omp_task ();
13316 c_parser_statement (parser);
13317 return c_finish_omp_task (loc, clauses, block);
13320 /* OpenMP 3.0:
13321 # pragma omp taskwait new-line
13324 static void
13325 c_parser_omp_taskwait (c_parser *parser)
13327 location_t loc = c_parser_peek_token (parser)->location;
13328 c_parser_consume_pragma (parser);
13329 c_parser_skip_to_pragma_eol (parser);
13331 c_finish_omp_taskwait (loc);
13334 /* OpenMP 3.1:
13335 # pragma omp taskyield new-line
13338 static void
13339 c_parser_omp_taskyield (c_parser *parser)
13341 location_t loc = c_parser_peek_token (parser)->location;
13342 c_parser_consume_pragma (parser);
13343 c_parser_skip_to_pragma_eol (parser);
13345 c_finish_omp_taskyield (loc);
13348 /* OpenMP 4.0:
13349 # pragma omp taskgroup new-line
13352 static tree
13353 c_parser_omp_taskgroup (c_parser *parser)
13355 location_t loc = c_parser_peek_token (parser)->location;
13356 c_parser_skip_to_pragma_eol (parser);
13357 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
13360 /* OpenMP 4.0:
13361 # pragma omp cancel cancel-clause[optseq] new-line
13363 LOC is the location of the #pragma.
13366 #define OMP_CANCEL_CLAUSE_MASK \
13367 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
13371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13373 static void
13374 c_parser_omp_cancel (c_parser *parser)
13376 location_t loc = c_parser_peek_token (parser)->location;
13378 c_parser_consume_pragma (parser);
13379 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
13380 "#pragma omp cancel");
13382 c_finish_omp_cancel (loc, clauses);
13385 /* OpenMP 4.0:
13386 # pragma omp cancellation point cancelpt-clause[optseq] new-line
13388 LOC is the location of the #pragma.
13391 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
13392 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
13393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
13394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
13395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
13397 static void
13398 c_parser_omp_cancellation_point (c_parser *parser)
13400 location_t loc = c_parser_peek_token (parser)->location;
13401 tree clauses;
13402 bool point_seen = false;
13404 c_parser_consume_pragma (parser);
13405 if (c_parser_next_token_is (parser, CPP_NAME))
13407 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13408 if (strcmp (p, "point") == 0)
13410 c_parser_consume_token (parser);
13411 point_seen = true;
13414 if (!point_seen)
13416 c_parser_error (parser, "expected %<point%>");
13417 c_parser_skip_to_pragma_eol (parser);
13418 return;
13421 clauses
13422 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
13423 "#pragma omp cancellation point");
13425 c_finish_omp_cancellation_point (loc, clauses);
13428 /* OpenMP 4.0:
13429 #pragma omp distribute distribute-clause[optseq] new-line
13430 for-loop */
13432 #define OMP_DISTRIBUTE_CLAUSE_MASK \
13433 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
13436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
13438 static tree
13439 c_parser_omp_distribute (location_t loc, c_parser *parser,
13440 char *p_name, omp_clause_mask mask, tree *cclauses)
13442 tree clauses, block, ret;
13444 strcat (p_name, " distribute");
13445 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
13447 if (c_parser_next_token_is (parser, CPP_NAME))
13449 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13450 bool simd = false;
13451 bool parallel = false;
13453 if (strcmp (p, "simd") == 0)
13454 simd = true;
13455 else
13456 parallel = strcmp (p, "parallel") == 0;
13457 if (parallel || simd)
13459 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13460 if (cclauses == NULL)
13461 cclauses = cclauses_buf;
13462 c_parser_consume_token (parser);
13463 if (!flag_openmp) /* flag_openmp_simd */
13465 if (simd)
13466 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13467 else
13468 return c_parser_omp_parallel (loc, parser, p_name, mask,
13469 cclauses);
13471 block = c_begin_compound_stmt (true);
13472 if (simd)
13473 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
13474 else
13475 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
13476 block = c_end_compound_stmt (loc, block, true);
13477 if (ret == NULL)
13478 return ret;
13479 ret = make_node (OMP_DISTRIBUTE);
13480 TREE_TYPE (ret) = void_type_node;
13481 OMP_FOR_BODY (ret) = block;
13482 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13483 SET_EXPR_LOCATION (ret, loc);
13484 add_stmt (ret);
13485 return ret;
13488 if (!flag_openmp) /* flag_openmp_simd */
13490 c_parser_skip_to_pragma_eol (parser);
13491 return NULL_TREE;
13494 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13495 if (cclauses)
13497 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
13498 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
13501 block = c_begin_compound_stmt (true);
13502 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
13503 block = c_end_compound_stmt (loc, block, true);
13504 add_stmt (block);
13506 return ret;
13509 /* OpenMP 4.0:
13510 # pragma omp teams teams-clause[optseq] new-line
13511 structured-block */
13513 #define OMP_TEAMS_CLAUSE_MASK \
13514 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
13515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
13516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
13517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
13518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
13519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
13520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
13522 static tree
13523 c_parser_omp_teams (location_t loc, c_parser *parser,
13524 char *p_name, omp_clause_mask mask, tree *cclauses)
13526 tree clauses, block, ret;
13528 strcat (p_name, " teams");
13529 mask |= OMP_TEAMS_CLAUSE_MASK;
13531 if (c_parser_next_token_is (parser, CPP_NAME))
13533 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13534 if (strcmp (p, "distribute") == 0)
13536 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
13537 if (cclauses == NULL)
13538 cclauses = cclauses_buf;
13540 c_parser_consume_token (parser);
13541 if (!flag_openmp) /* flag_openmp_simd */
13542 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13543 block = c_begin_compound_stmt (true);
13544 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
13545 block = c_end_compound_stmt (loc, block, true);
13546 if (ret == NULL)
13547 return ret;
13548 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13549 ret = make_node (OMP_TEAMS);
13550 TREE_TYPE (ret) = void_type_node;
13551 OMP_TEAMS_CLAUSES (ret) = clauses;
13552 OMP_TEAMS_BODY (ret) = block;
13553 return add_stmt (ret);
13556 if (!flag_openmp) /* flag_openmp_simd */
13558 c_parser_skip_to_pragma_eol (parser);
13559 return NULL_TREE;
13562 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
13563 if (cclauses)
13565 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
13566 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
13569 tree stmt = make_node (OMP_TEAMS);
13570 TREE_TYPE (stmt) = void_type_node;
13571 OMP_TEAMS_CLAUSES (stmt) = clauses;
13572 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
13574 return add_stmt (stmt);
13577 /* OpenMP 4.0:
13578 # pragma omp target data target-data-clause[optseq] new-line
13579 structured-block */
13581 #define OMP_TARGET_DATA_CLAUSE_MASK \
13582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13586 static tree
13587 c_parser_omp_target_data (location_t loc, c_parser *parser)
13589 tree stmt = make_node (OMP_TARGET_DATA);
13590 TREE_TYPE (stmt) = void_type_node;
13592 OMP_TARGET_DATA_CLAUSES (stmt)
13593 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
13594 "#pragma omp target data");
13595 keep_next_level ();
13596 tree block = c_begin_compound_stmt (true);
13597 add_stmt (c_parser_omp_structured_block (parser));
13598 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13600 SET_EXPR_LOCATION (stmt, loc);
13601 return add_stmt (stmt);
13604 /* OpenMP 4.0:
13605 # pragma omp target update target-update-clause[optseq] new-line */
13607 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
13608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
13609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
13610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13613 static bool
13614 c_parser_omp_target_update (location_t loc, c_parser *parser,
13615 enum pragma_context context)
13617 if (context == pragma_stmt)
13619 error_at (loc,
13620 "%<#pragma omp target update%> may only be "
13621 "used in compound statements");
13622 c_parser_skip_to_pragma_eol (parser);
13623 return false;
13626 tree clauses
13627 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
13628 "#pragma omp target update");
13629 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
13630 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
13632 error_at (loc,
13633 "%<#pragma omp target update must contain at least one "
13634 "%<from%> or %<to%> clauses");
13635 return false;
13638 tree stmt = make_node (OMP_TARGET_UPDATE);
13639 TREE_TYPE (stmt) = void_type_node;
13640 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
13641 SET_EXPR_LOCATION (stmt, loc);
13642 add_stmt (stmt);
13643 return false;
13646 /* OpenMP 4.0:
13647 # pragma omp target target-clause[optseq] new-line
13648 structured-block */
13650 #define OMP_TARGET_CLAUSE_MASK \
13651 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
13652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
13653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
13655 static bool
13656 c_parser_omp_target (c_parser *parser, enum pragma_context context)
13658 location_t loc = c_parser_peek_token (parser)->location;
13659 c_parser_consume_pragma (parser);
13661 if (context != pragma_stmt && context != pragma_compound)
13663 c_parser_error (parser, "expected declaration specifiers");
13664 c_parser_skip_to_pragma_eol (parser);
13665 return false;
13668 if (c_parser_next_token_is (parser, CPP_NAME))
13670 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13672 if (strcmp (p, "teams") == 0)
13674 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
13675 char p_name[sizeof ("#pragma omp target teams distribute "
13676 "parallel for simd")];
13678 c_parser_consume_token (parser);
13679 strcpy (p_name, "#pragma omp target");
13680 if (!flag_openmp) /* flag_openmp_simd */
13682 tree stmt = c_parser_omp_teams (loc, parser, p_name,
13683 OMP_TARGET_CLAUSE_MASK,
13684 cclauses);
13685 return stmt != NULL_TREE;
13687 keep_next_level ();
13688 tree block = c_begin_compound_stmt (true);
13689 tree ret = c_parser_omp_teams (loc, parser, p_name,
13690 OMP_TARGET_CLAUSE_MASK, cclauses);
13691 block = c_end_compound_stmt (loc, block, true);
13692 if (ret == NULL_TREE)
13693 return false;
13694 tree stmt = make_node (OMP_TARGET);
13695 TREE_TYPE (stmt) = void_type_node;
13696 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
13697 OMP_TARGET_BODY (stmt) = block;
13698 add_stmt (stmt);
13699 return true;
13701 else if (!flag_openmp) /* flag_openmp_simd */
13703 c_parser_skip_to_pragma_eol (parser);
13704 return false;
13706 else if (strcmp (p, "data") == 0)
13708 c_parser_consume_token (parser);
13709 c_parser_omp_target_data (loc, parser);
13710 return true;
13712 else if (strcmp (p, "update") == 0)
13714 c_parser_consume_token (parser);
13715 return c_parser_omp_target_update (loc, parser, context);
13719 tree stmt = make_node (OMP_TARGET);
13720 TREE_TYPE (stmt) = void_type_node;
13722 OMP_TARGET_CLAUSES (stmt)
13723 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
13724 "#pragma omp target");
13725 keep_next_level ();
13726 tree block = c_begin_compound_stmt (true);
13727 add_stmt (c_parser_omp_structured_block (parser));
13728 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
13730 SET_EXPR_LOCATION (stmt, loc);
13731 add_stmt (stmt);
13732 return true;
13735 /* OpenMP 4.0:
13736 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
13738 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
13739 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
13740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
13741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
13742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
13743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
13744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
13746 static void
13747 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
13749 vec<c_token> clauses = vNULL;
13750 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13752 c_token *token = c_parser_peek_token (parser);
13753 if (token->type == CPP_EOF)
13755 c_parser_skip_to_pragma_eol (parser);
13756 clauses.release ();
13757 return;
13759 clauses.safe_push (*token);
13760 c_parser_consume_token (parser);
13762 clauses.safe_push (*c_parser_peek_token (parser));
13763 c_parser_skip_to_pragma_eol (parser);
13765 while (c_parser_next_token_is (parser, CPP_PRAGMA))
13767 if (c_parser_peek_token (parser)->pragma_kind
13768 != PRAGMA_OMP_DECLARE_REDUCTION
13769 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
13770 || strcmp (IDENTIFIER_POINTER
13771 (c_parser_peek_2nd_token (parser)->value),
13772 "simd") != 0)
13774 c_parser_error (parser,
13775 "%<#pragma omp declare simd%> must be followed by "
13776 "function declaration or definition or another "
13777 "%<#pragma omp declare simd%>");
13778 clauses.release ();
13779 return;
13781 c_parser_consume_pragma (parser);
13782 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13784 c_token *token = c_parser_peek_token (parser);
13785 if (token->type == CPP_EOF)
13787 c_parser_skip_to_pragma_eol (parser);
13788 clauses.release ();
13789 return;
13791 clauses.safe_push (*token);
13792 c_parser_consume_token (parser);
13794 clauses.safe_push (*c_parser_peek_token (parser));
13795 c_parser_skip_to_pragma_eol (parser);
13798 /* Make sure nothing tries to read past the end of the tokens. */
13799 c_token eof_token;
13800 memset (&eof_token, 0, sizeof (eof_token));
13801 eof_token.type = CPP_EOF;
13802 clauses.safe_push (eof_token);
13803 clauses.safe_push (eof_token);
13805 switch (context)
13807 case pragma_external:
13808 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13809 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13811 int ext = disable_extension_diagnostics ();
13813 c_parser_consume_token (parser);
13814 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13815 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13816 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13817 NULL, clauses);
13818 restore_extension_diagnostics (ext);
13820 else
13821 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
13822 NULL, clauses);
13823 break;
13824 case pragma_struct:
13825 case pragma_param:
13826 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13827 "function declaration or definition");
13828 break;
13829 case pragma_compound:
13830 case pragma_stmt:
13831 if (c_parser_next_token_is (parser, CPP_KEYWORD)
13832 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
13834 int ext = disable_extension_diagnostics ();
13836 c_parser_consume_token (parser);
13837 while (c_parser_next_token_is (parser, CPP_KEYWORD)
13838 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
13839 if (c_parser_next_tokens_start_declaration (parser))
13841 c_parser_declaration_or_fndef (parser, true, true, true, true,
13842 true, NULL, clauses);
13843 restore_extension_diagnostics (ext);
13844 break;
13846 restore_extension_diagnostics (ext);
13848 else if (c_parser_next_tokens_start_declaration (parser))
13850 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
13851 NULL, clauses);
13852 break;
13854 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
13855 "function declaration or definition");
13856 break;
13857 default:
13858 gcc_unreachable ();
13860 clauses.release ();
13863 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
13864 and put that into "omp declare simd" attribute. */
13866 static void
13867 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
13868 vec<c_token> clauses)
13870 if (flag_cilkplus
13871 && clauses.exists () && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13873 error ("%<#pragma omp declare simd%> cannot be used in the same "
13874 "function marked as a Cilk Plus SIMD-enabled function");
13875 vec_free (parser->cilk_simd_fn_tokens);
13876 return;
13879 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
13880 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
13881 has already processed the tokens. */
13882 if (clauses.exists () && clauses[0].type == CPP_EOF)
13883 return;
13884 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
13886 error ("%<#pragma omp declare simd%> not immediately followed by "
13887 "a function declaration or definition");
13888 clauses[0].type = CPP_EOF;
13889 return;
13891 if (clauses.exists () && clauses[0].type != CPP_NAME)
13893 error_at (DECL_SOURCE_LOCATION (fndecl),
13894 "%<#pragma omp declare simd%> not immediately followed by "
13895 "a single function declaration or definition");
13896 clauses[0].type = CPP_EOF;
13897 return;
13900 if (parms == NULL_TREE)
13901 parms = DECL_ARGUMENTS (fndecl);
13903 unsigned int tokens_avail = parser->tokens_avail;
13904 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
13905 bool is_cilkplus_cilk_simd_fn = false;
13907 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13909 parser->tokens = parser->cilk_simd_fn_tokens->address ();
13910 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
13911 is_cilkplus_cilk_simd_fn = true;
13913 else
13915 parser->tokens = clauses.address ();
13916 parser->tokens_avail = clauses.length ();
13919 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
13920 while (parser->tokens_avail > 3)
13922 c_token *token = c_parser_peek_token (parser);
13923 if (!is_cilkplus_cilk_simd_fn)
13924 gcc_assert (token->type == CPP_NAME
13925 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
13926 else
13927 gcc_assert (token->type == CPP_NAME
13928 && is_cilkplus_vector_p (token->value));
13929 c_parser_consume_token (parser);
13930 parser->in_pragma = true;
13932 tree c = NULL_TREE;
13933 if (is_cilkplus_cilk_simd_fn)
13934 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
13935 "SIMD-enabled functions attribute");
13936 else
13937 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
13938 "#pragma omp declare simd");
13939 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
13940 if (c != NULL_TREE)
13941 c = tree_cons (NULL_TREE, c, NULL_TREE);
13942 if (is_cilkplus_cilk_simd_fn)
13944 tree k = build_tree_list (get_identifier ("cilk simd function"),
13945 NULL_TREE);
13946 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
13947 DECL_ATTRIBUTES (fndecl) = k;
13949 c = build_tree_list (get_identifier ("omp declare simd"), c);
13950 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
13951 DECL_ATTRIBUTES (fndecl) = c;
13954 parser->tokens = &parser->tokens_buf[0];
13955 parser->tokens_avail = tokens_avail;
13956 if (clauses.exists ())
13957 clauses[0].type = CPP_PRAGMA;
13959 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
13960 vec_free (parser->cilk_simd_fn_tokens);
13964 /* OpenMP 4.0:
13965 # pragma omp declare target new-line
13966 declarations and definitions
13967 # pragma omp end declare target new-line */
13969 static void
13970 c_parser_omp_declare_target (c_parser *parser)
13972 c_parser_skip_to_pragma_eol (parser);
13973 current_omp_declare_target_attribute++;
13976 static void
13977 c_parser_omp_end_declare_target (c_parser *parser)
13979 location_t loc = c_parser_peek_token (parser)->location;
13980 c_parser_consume_pragma (parser);
13981 if (c_parser_next_token_is (parser, CPP_NAME)
13982 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13983 "declare") == 0)
13985 c_parser_consume_token (parser);
13986 if (c_parser_next_token_is (parser, CPP_NAME)
13987 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
13988 "target") == 0)
13989 c_parser_consume_token (parser);
13990 else
13992 c_parser_error (parser, "expected %<target%>");
13993 c_parser_skip_to_pragma_eol (parser);
13994 return;
13997 else
13999 c_parser_error (parser, "expected %<declare%>");
14000 c_parser_skip_to_pragma_eol (parser);
14001 return;
14003 c_parser_skip_to_pragma_eol (parser);
14004 if (!current_omp_declare_target_attribute)
14005 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
14006 "%<#pragma omp declare target%>");
14007 else
14008 current_omp_declare_target_attribute--;
14012 /* OpenMP 4.0
14013 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14014 initializer-clause[opt] new-line
14016 initializer-clause:
14017 initializer (omp_priv = initializer)
14018 initializer (function-name (argument-list)) */
14020 static void
14021 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
14023 unsigned int tokens_avail = 0, i;
14024 vec<tree> types = vNULL;
14025 vec<c_token> clauses = vNULL;
14026 enum tree_code reduc_code = ERROR_MARK;
14027 tree reduc_id = NULL_TREE;
14028 tree type;
14029 location_t rloc = c_parser_peek_token (parser)->location;
14031 if (context == pragma_struct || context == pragma_param)
14033 error ("%<#pragma omp declare reduction%> not at file or block scope");
14034 goto fail;
14037 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14038 goto fail;
14040 switch (c_parser_peek_token (parser)->type)
14042 case CPP_PLUS:
14043 reduc_code = PLUS_EXPR;
14044 break;
14045 case CPP_MULT:
14046 reduc_code = MULT_EXPR;
14047 break;
14048 case CPP_MINUS:
14049 reduc_code = MINUS_EXPR;
14050 break;
14051 case CPP_AND:
14052 reduc_code = BIT_AND_EXPR;
14053 break;
14054 case CPP_XOR:
14055 reduc_code = BIT_XOR_EXPR;
14056 break;
14057 case CPP_OR:
14058 reduc_code = BIT_IOR_EXPR;
14059 break;
14060 case CPP_AND_AND:
14061 reduc_code = TRUTH_ANDIF_EXPR;
14062 break;
14063 case CPP_OR_OR:
14064 reduc_code = TRUTH_ORIF_EXPR;
14065 break;
14066 case CPP_NAME:
14067 const char *p;
14068 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14069 if (strcmp (p, "min") == 0)
14071 reduc_code = MIN_EXPR;
14072 break;
14074 if (strcmp (p, "max") == 0)
14076 reduc_code = MAX_EXPR;
14077 break;
14079 reduc_id = c_parser_peek_token (parser)->value;
14080 break;
14081 default:
14082 c_parser_error (parser,
14083 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
14084 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
14085 goto fail;
14088 tree orig_reduc_id, reduc_decl;
14089 orig_reduc_id = reduc_id;
14090 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
14091 reduc_decl = c_omp_reduction_decl (reduc_id);
14092 c_parser_consume_token (parser);
14094 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
14095 goto fail;
14097 while (true)
14099 location_t loc = c_parser_peek_token (parser)->location;
14100 struct c_type_name *ctype = c_parser_type_name (parser);
14101 if (ctype != NULL)
14103 type = groktypename (ctype, NULL, NULL);
14104 if (type == error_mark_node)
14106 else if ((INTEGRAL_TYPE_P (type)
14107 || TREE_CODE (type) == REAL_TYPE
14108 || TREE_CODE (type) == COMPLEX_TYPE)
14109 && orig_reduc_id == NULL_TREE)
14110 error_at (loc, "predeclared arithmetic type in "
14111 "%<#pragma omp declare reduction%>");
14112 else if (TREE_CODE (type) == FUNCTION_TYPE
14113 || TREE_CODE (type) == ARRAY_TYPE)
14114 error_at (loc, "function or array type in "
14115 "%<#pragma omp declare reduction%>");
14116 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
14117 error_at (loc, "const, volatile or restrict qualified type in "
14118 "%<#pragma omp declare reduction%>");
14119 else
14121 tree t;
14122 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
14123 if (comptypes (TREE_PURPOSE (t), type))
14125 error_at (loc, "redeclaration of %qs "
14126 "%<#pragma omp declare reduction%> for "
14127 "type %qT",
14128 IDENTIFIER_POINTER (reduc_id)
14129 + sizeof ("omp declare reduction ") - 1,
14130 type);
14131 location_t ploc
14132 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
14133 0));
14134 error_at (ploc, "previous %<#pragma omp declare "
14135 "reduction%>");
14136 break;
14138 if (t == NULL_TREE)
14139 types.safe_push (type);
14141 if (c_parser_next_token_is (parser, CPP_COMMA))
14142 c_parser_consume_token (parser);
14143 else
14144 break;
14146 else
14147 break;
14150 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
14151 || types.is_empty ())
14153 fail:
14154 clauses.release ();
14155 types.release ();
14156 while (true)
14158 c_token *token = c_parser_peek_token (parser);
14159 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
14160 break;
14161 c_parser_consume_token (parser);
14163 c_parser_skip_to_pragma_eol (parser);
14164 return;
14167 if (types.length () > 1)
14169 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14171 c_token *token = c_parser_peek_token (parser);
14172 if (token->type == CPP_EOF)
14173 goto fail;
14174 clauses.safe_push (*token);
14175 c_parser_consume_token (parser);
14177 clauses.safe_push (*c_parser_peek_token (parser));
14178 c_parser_skip_to_pragma_eol (parser);
14180 /* Make sure nothing tries to read past the end of the tokens. */
14181 c_token eof_token;
14182 memset (&eof_token, 0, sizeof (eof_token));
14183 eof_token.type = CPP_EOF;
14184 clauses.safe_push (eof_token);
14185 clauses.safe_push (eof_token);
14188 int errs = errorcount;
14189 FOR_EACH_VEC_ELT (types, i, type)
14191 tokens_avail = parser->tokens_avail;
14192 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
14193 if (!clauses.is_empty ())
14195 parser->tokens = clauses.address ();
14196 parser->tokens_avail = clauses.length ();
14197 parser->in_pragma = true;
14200 bool nested = current_function_decl != NULL_TREE;
14201 if (nested)
14202 c_push_function_context ();
14203 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
14204 reduc_id, default_function_type);
14205 current_function_decl = fndecl;
14206 allocate_struct_function (fndecl, true);
14207 push_scope ();
14208 tree stmt = push_stmt_list ();
14209 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
14210 warn about these. */
14211 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
14212 get_identifier ("omp_out"), type);
14213 DECL_ARTIFICIAL (omp_out) = 1;
14214 DECL_CONTEXT (omp_out) = fndecl;
14215 pushdecl (omp_out);
14216 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
14217 get_identifier ("omp_in"), type);
14218 DECL_ARTIFICIAL (omp_in) = 1;
14219 DECL_CONTEXT (omp_in) = fndecl;
14220 pushdecl (omp_in);
14221 struct c_expr combiner = c_parser_expression (parser);
14222 struct c_expr initializer;
14223 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
14224 bool bad = false;
14225 initializer.value = error_mark_node;
14226 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14227 bad = true;
14228 else if (c_parser_next_token_is (parser, CPP_NAME)
14229 && strcmp (IDENTIFIER_POINTER
14230 (c_parser_peek_token (parser)->value),
14231 "initializer") == 0)
14233 c_parser_consume_token (parser);
14234 pop_scope ();
14235 push_scope ();
14236 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
14237 get_identifier ("omp_priv"), type);
14238 DECL_ARTIFICIAL (omp_priv) = 1;
14239 DECL_INITIAL (omp_priv) = error_mark_node;
14240 DECL_CONTEXT (omp_priv) = fndecl;
14241 pushdecl (omp_priv);
14242 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
14243 get_identifier ("omp_orig"), type);
14244 DECL_ARTIFICIAL (omp_orig) = 1;
14245 DECL_CONTEXT (omp_orig) = fndecl;
14246 pushdecl (omp_orig);
14247 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14248 bad = true;
14249 else if (!c_parser_next_token_is (parser, CPP_NAME))
14251 c_parser_error (parser, "expected %<omp_priv%> or "
14252 "function-name");
14253 bad = true;
14255 else if (strcmp (IDENTIFIER_POINTER
14256 (c_parser_peek_token (parser)->value),
14257 "omp_priv") != 0)
14259 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
14260 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14262 c_parser_error (parser, "expected function-name %<(%>");
14263 bad = true;
14265 else
14266 initializer = c_parser_postfix_expression (parser);
14267 if (initializer.value
14268 && TREE_CODE (initializer.value) == CALL_EXPR)
14270 int j;
14271 tree c = initializer.value;
14272 for (j = 0; j < call_expr_nargs (c); j++)
14273 if (TREE_CODE (CALL_EXPR_ARG (c, j)) == ADDR_EXPR
14274 && TREE_OPERAND (CALL_EXPR_ARG (c, j), 0) == omp_priv)
14275 break;
14276 if (j == call_expr_nargs (c))
14277 error ("one of the initializer call arguments should be "
14278 "%<&omp_priv%>");
14281 else
14283 c_parser_consume_token (parser);
14284 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14285 bad = true;
14286 else
14288 tree st = push_stmt_list ();
14289 start_init (omp_priv, NULL_TREE, 0);
14290 location_t loc = c_parser_peek_token (parser)->location;
14291 struct c_expr init = c_parser_initializer (parser);
14292 finish_init ();
14293 finish_decl (omp_priv, loc, init.value,
14294 init.original_type, NULL_TREE);
14295 pop_stmt_list (st);
14298 if (!bad
14299 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14300 bad = true;
14303 if (!bad)
14305 c_parser_skip_to_pragma_eol (parser);
14307 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
14308 DECL_INITIAL (reduc_decl));
14309 DECL_INITIAL (reduc_decl) = t;
14310 DECL_SOURCE_LOCATION (omp_out) = rloc;
14311 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
14312 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
14313 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
14314 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
14315 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
14316 if (omp_priv)
14318 DECL_SOURCE_LOCATION (omp_priv) = rloc;
14319 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
14320 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
14321 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
14322 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
14323 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14324 walk_tree (&DECL_INITIAL (omp_priv),
14325 c_check_omp_declare_reduction_r,
14326 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
14330 pop_stmt_list (stmt);
14331 pop_scope ();
14332 if (cfun->language != NULL)
14334 ggc_free (cfun->language);
14335 cfun->language = NULL;
14337 set_cfun (NULL);
14338 current_function_decl = NULL_TREE;
14339 if (nested)
14340 c_pop_function_context ();
14342 if (!clauses.is_empty ())
14344 parser->tokens = &parser->tokens_buf[0];
14345 parser->tokens_avail = tokens_avail;
14347 if (bad)
14348 goto fail;
14349 if (errs != errorcount)
14350 break;
14353 clauses.release ();
14354 types.release ();
14358 /* OpenMP 4.0
14359 #pragma omp declare simd declare-simd-clauses[optseq] new-line
14360 #pragma omp declare reduction (reduction-id : typename-list : expression) \
14361 initializer-clause[opt] new-line
14362 #pragma omp declare target new-line */
14364 static void
14365 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
14367 c_parser_consume_pragma (parser);
14368 if (c_parser_next_token_is (parser, CPP_NAME))
14370 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14371 if (strcmp (p, "simd") == 0)
14373 /* c_parser_consume_token (parser); done in
14374 c_parser_omp_declare_simd. */
14375 c_parser_omp_declare_simd (parser, context);
14376 return;
14378 if (strcmp (p, "reduction") == 0)
14380 c_parser_consume_token (parser);
14381 c_parser_omp_declare_reduction (parser, context);
14382 return;
14384 if (!flag_openmp) /* flag_openmp_simd */
14386 c_parser_skip_to_pragma_eol (parser);
14387 return;
14389 if (strcmp (p, "target") == 0)
14391 c_parser_consume_token (parser);
14392 c_parser_omp_declare_target (parser);
14393 return;
14397 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
14398 "or %<target%>");
14399 c_parser_skip_to_pragma_eol (parser);
14402 /* Main entry point to parsing most OpenMP pragmas. */
14404 static void
14405 c_parser_omp_construct (c_parser *parser)
14407 enum pragma_kind p_kind;
14408 location_t loc;
14409 tree stmt;
14410 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
14411 omp_clause_mask mask (0);
14413 loc = c_parser_peek_token (parser)->location;
14414 p_kind = c_parser_peek_token (parser)->pragma_kind;
14415 c_parser_consume_pragma (parser);
14417 switch (p_kind)
14419 case PRAGMA_OACC_DATA:
14420 stmt = c_parser_oacc_data (loc, parser);
14421 break;
14422 case PRAGMA_OACC_KERNELS:
14423 strcpy (p_name, "#pragma acc");
14424 stmt = c_parser_oacc_kernels (loc, parser, p_name);
14425 break;
14426 case PRAGMA_OACC_LOOP:
14427 strcpy (p_name, "#pragma acc");
14428 stmt = c_parser_oacc_loop (loc, parser, p_name);
14429 break;
14430 case PRAGMA_OACC_PARALLEL:
14431 strcpy (p_name, "#pragma acc");
14432 stmt = c_parser_oacc_parallel (loc, parser, p_name);
14433 break;
14434 case PRAGMA_OACC_WAIT:
14435 strcpy (p_name, "#pragma wait");
14436 stmt = c_parser_oacc_wait (loc, parser, p_name);
14437 break;
14438 case PRAGMA_OMP_ATOMIC:
14439 c_parser_omp_atomic (loc, parser);
14440 return;
14441 case PRAGMA_OMP_CRITICAL:
14442 stmt = c_parser_omp_critical (loc, parser);
14443 break;
14444 case PRAGMA_OMP_DISTRIBUTE:
14445 strcpy (p_name, "#pragma omp");
14446 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
14447 break;
14448 case PRAGMA_OMP_FOR:
14449 strcpy (p_name, "#pragma omp");
14450 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
14451 break;
14452 case PRAGMA_OMP_MASTER:
14453 stmt = c_parser_omp_master (loc, parser);
14454 break;
14455 case PRAGMA_OMP_ORDERED:
14456 stmt = c_parser_omp_ordered (loc, parser);
14457 break;
14458 case PRAGMA_OMP_PARALLEL:
14459 strcpy (p_name, "#pragma omp");
14460 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
14461 break;
14462 case PRAGMA_OMP_SECTIONS:
14463 strcpy (p_name, "#pragma omp");
14464 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
14465 break;
14466 case PRAGMA_OMP_SIMD:
14467 strcpy (p_name, "#pragma omp");
14468 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
14469 break;
14470 case PRAGMA_OMP_SINGLE:
14471 stmt = c_parser_omp_single (loc, parser);
14472 break;
14473 case PRAGMA_OMP_TASK:
14474 stmt = c_parser_omp_task (loc, parser);
14475 break;
14476 case PRAGMA_OMP_TASKGROUP:
14477 stmt = c_parser_omp_taskgroup (parser);
14478 break;
14479 case PRAGMA_OMP_TEAMS:
14480 strcpy (p_name, "#pragma omp");
14481 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
14482 break;
14483 default:
14484 gcc_unreachable ();
14487 if (stmt)
14488 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
14492 /* OpenMP 2.5:
14493 # pragma omp threadprivate (variable-list) */
14495 static void
14496 c_parser_omp_threadprivate (c_parser *parser)
14498 tree vars, t;
14499 location_t loc;
14501 c_parser_consume_pragma (parser);
14502 loc = c_parser_peek_token (parser)->location;
14503 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14505 /* Mark every variable in VARS to be assigned thread local storage. */
14506 for (t = vars; t; t = TREE_CHAIN (t))
14508 tree v = TREE_PURPOSE (t);
14510 /* FIXME diagnostics: Ideally we should keep individual
14511 locations for all the variables in the var list to make the
14512 following errors more precise. Perhaps
14513 c_parser_omp_var_list_parens() should construct a list of
14514 locations to go along with the var list. */
14516 /* If V had already been marked threadprivate, it doesn't matter
14517 whether it had been used prior to this point. */
14518 if (TREE_CODE (v) != VAR_DECL)
14519 error_at (loc, "%qD is not a variable", v);
14520 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
14521 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
14522 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
14523 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
14524 else if (TREE_TYPE (v) == error_mark_node)
14526 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
14527 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
14528 else
14530 if (! DECL_THREAD_LOCAL_P (v))
14532 set_decl_tls_model (v, decl_default_tls_model (v));
14533 /* If rtl has been already set for this var, call
14534 make_decl_rtl once again, so that encode_section_info
14535 has a chance to look at the new decl flags. */
14536 if (DECL_RTL_SET_P (v))
14537 make_decl_rtl (v);
14539 C_DECL_THREADPRIVATE_P (v) = 1;
14543 c_parser_skip_to_pragma_eol (parser);
14546 /* Cilk Plus <#pragma simd> parsing routines. */
14548 /* Helper function for c_parser_pragma. Perform some sanity checking
14549 for <#pragma simd> constructs. Returns FALSE if there was a
14550 problem. */
14552 static bool
14553 c_parser_cilk_verify_simd (c_parser *parser,
14554 enum pragma_context context)
14556 if (!flag_cilkplus)
14558 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
14559 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14560 return false;
14562 if (context == pragma_external)
14564 c_parser_error (parser,"pragma simd must be inside a function");
14565 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14566 return false;
14568 return true;
14571 /* Cilk Plus:
14572 This function is shared by SIMD-enabled functions and #pragma simd.
14573 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
14574 CLAUSES is unused. The main purpose of this function is to parse a
14575 vectorlength attribute or clause and check for parse errors.
14576 When IS_SIMD_FN is true then the function is merely caching the tokens
14577 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
14578 cache is cleared since there is no reason to continue.
14579 Syntax:
14580 vectorlength ( constant-expression ) */
14582 static tree
14583 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
14584 bool is_simd_fn)
14586 if (is_simd_fn)
14587 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
14588 else
14589 /* The vectorlength clause behaves exactly like OpenMP's safelen
14590 clause. Represent it in OpenMP terms. */
14591 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
14593 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14594 return clauses;
14596 location_t loc = c_parser_peek_token (parser)->location;
14597 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14598 expr = c_fully_fold (expr, false, NULL);
14600 /* If expr is an error_mark_node then the above function would have
14601 emitted an error. No reason to do it twice. */
14602 if (expr == error_mark_node)
14604 else if (!TREE_TYPE (expr)
14605 || !TREE_CONSTANT (expr)
14606 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
14608 error_at (loc, "vectorlength must be an integer constant");
14609 else if (wi::exact_log2 (expr) == -1)
14610 error_at (loc, "vectorlength must be a power of 2");
14611 else
14613 if (is_simd_fn)
14615 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
14616 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
14617 OMP_CLAUSE_CHAIN (u) = clauses;
14618 clauses = u;
14620 else
14622 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
14623 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
14624 OMP_CLAUSE_CHAIN (u) = clauses;
14625 clauses = u;
14629 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14631 return clauses;
14634 /* Cilk Plus:
14635 linear ( simd-linear-variable-list )
14637 simd-linear-variable-list:
14638 simd-linear-variable
14639 simd-linear-variable-list , simd-linear-variable
14641 simd-linear-variable:
14642 id-expression
14643 id-expression : simd-linear-step
14645 simd-linear-step:
14646 conditional-expression */
14648 static tree
14649 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
14651 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14652 return clauses;
14654 location_t loc = c_parser_peek_token (parser)->location;
14656 if (c_parser_next_token_is_not (parser, CPP_NAME)
14657 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
14658 c_parser_error (parser, "expected identifier");
14660 while (c_parser_next_token_is (parser, CPP_NAME)
14661 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
14663 tree var = lookup_name (c_parser_peek_token (parser)->value);
14665 if (var == NULL)
14667 undeclared_variable (c_parser_peek_token (parser)->location,
14668 c_parser_peek_token (parser)->value);
14669 c_parser_consume_token (parser);
14671 else if (var == error_mark_node)
14672 c_parser_consume_token (parser);
14673 else
14675 tree step = integer_one_node;
14677 /* Parse the linear step if present. */
14678 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
14680 c_parser_consume_token (parser);
14681 c_parser_consume_token (parser);
14683 tree expr = c_parser_expr_no_commas (parser, NULL).value;
14684 expr = c_fully_fold (expr, false, NULL);
14686 if (TREE_TYPE (expr)
14687 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
14688 && (TREE_CONSTANT (expr)
14689 || DECL_P (expr)))
14690 step = expr;
14691 else
14692 c_parser_error (parser,
14693 "step size must be an integer constant "
14694 "expression or an integer variable");
14696 else
14697 c_parser_consume_token (parser);
14699 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
14700 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
14701 OMP_CLAUSE_DECL (u) = var;
14702 OMP_CLAUSE_LINEAR_STEP (u) = step;
14703 OMP_CLAUSE_CHAIN (u) = clauses;
14704 clauses = u;
14707 if (c_parser_next_token_is_not (parser, CPP_COMMA))
14708 break;
14710 c_parser_consume_token (parser);
14713 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14715 return clauses;
14718 /* Returns the name of the next clause. If the clause is not
14719 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
14720 not consumed. Otherwise, the appropriate pragma_simd_clause is
14721 returned and the token is consumed. */
14723 static pragma_omp_clause
14724 c_parser_cilk_clause_name (c_parser *parser)
14726 pragma_omp_clause result;
14727 c_token *token = c_parser_peek_token (parser);
14729 if (!token->value || token->type != CPP_NAME)
14730 return PRAGMA_CILK_CLAUSE_NONE;
14732 const char *p = IDENTIFIER_POINTER (token->value);
14734 if (!strcmp (p, "vectorlength"))
14735 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
14736 else if (!strcmp (p, "linear"))
14737 result = PRAGMA_CILK_CLAUSE_LINEAR;
14738 else if (!strcmp (p, "private"))
14739 result = PRAGMA_CILK_CLAUSE_PRIVATE;
14740 else if (!strcmp (p, "firstprivate"))
14741 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
14742 else if (!strcmp (p, "lastprivate"))
14743 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
14744 else if (!strcmp (p, "reduction"))
14745 result = PRAGMA_CILK_CLAUSE_REDUCTION;
14746 else
14747 return PRAGMA_CILK_CLAUSE_NONE;
14749 c_parser_consume_token (parser);
14750 return result;
14753 /* Parse all #<pragma simd> clauses. Return the list of clauses
14754 found. */
14756 static tree
14757 c_parser_cilk_all_clauses (c_parser *parser)
14759 tree clauses = NULL;
14761 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14763 pragma_omp_clause c_kind;
14765 c_kind = c_parser_cilk_clause_name (parser);
14767 switch (c_kind)
14769 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
14770 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
14771 break;
14772 case PRAGMA_CILK_CLAUSE_LINEAR:
14773 clauses = c_parser_cilk_clause_linear (parser, clauses);
14774 break;
14775 case PRAGMA_CILK_CLAUSE_PRIVATE:
14776 /* Use the OpenMP counterpart. */
14777 clauses = c_parser_omp_clause_private (parser, clauses);
14778 break;
14779 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
14780 /* Use the OpenMP counterpart. */
14781 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14782 break;
14783 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
14784 /* Use the OpenMP counterpart. */
14785 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14786 break;
14787 case PRAGMA_CILK_CLAUSE_REDUCTION:
14788 /* Use the OpenMP counterpart. */
14789 clauses = c_parser_omp_clause_reduction (parser, clauses);
14790 break;
14791 default:
14792 c_parser_error (parser, "expected %<#pragma simd%> clause");
14793 goto saw_error;
14797 saw_error:
14798 c_parser_skip_to_pragma_eol (parser);
14799 return c_finish_cilk_clauses (clauses);
14802 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
14803 Here is the correct syntax of this pragma:
14804 #pragma cilk grainsize = <EXP>
14807 static void
14808 c_parser_cilk_grainsize (c_parser *parser)
14810 extern tree convert_to_integer (tree, tree);
14812 /* consume the 'grainsize' keyword. */
14813 c_parser_consume_pragma (parser);
14815 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
14817 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
14818 if (g_expr.value == error_mark_node)
14820 c_parser_skip_to_pragma_eol (parser);
14821 return;
14823 tree grain = convert_to_integer (long_integer_type_node,
14824 c_fully_fold (g_expr.value, false,
14825 NULL));
14826 c_parser_skip_to_pragma_eol (parser);
14827 c_token *token = c_parser_peek_token (parser);
14828 if (token && token->type == CPP_KEYWORD
14829 && token->keyword == RID_CILK_FOR)
14831 if (grain == NULL_TREE || grain == error_mark_node)
14832 grain = integer_zero_node;
14833 c_parser_cilk_for (parser, grain);
14835 else
14836 warning (0, "%<#pragma cilk grainsize%> is not followed by "
14837 "%<_Cilk_for%>");
14839 else
14840 c_parser_skip_to_pragma_eol (parser);
14843 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
14845 static void
14846 c_parser_cilk_simd (c_parser *parser)
14848 tree clauses = c_parser_cilk_all_clauses (parser);
14849 tree block = c_begin_compound_stmt (true);
14850 location_t loc = c_parser_peek_token (parser)->location;
14851 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
14852 block = c_end_compound_stmt (loc, block, true);
14853 add_stmt (block);
14856 /* Create an artificial decl with TYPE and emit initialization of it with
14857 INIT. */
14859 static tree
14860 c_get_temp_regvar (tree type, tree init)
14862 location_t loc = EXPR_LOCATION (init);
14863 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
14864 DECL_ARTIFICIAL (decl) = 1;
14865 DECL_IGNORED_P (decl) = 1;
14866 pushdecl (decl);
14867 tree t = build2 (INIT_EXPR, type, decl, init);
14868 add_stmt (t);
14869 return decl;
14872 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
14873 GRAIN is the grain value passed in through pragma or 0. */
14875 static void
14876 c_parser_cilk_for (c_parser *parser, tree grain)
14878 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
14879 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
14880 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
14881 clauses = c_finish_omp_clauses (clauses);
14883 tree block = c_begin_compound_stmt (true);
14884 tree sb = push_stmt_list ();
14885 location_t loc = c_parser_peek_token (parser)->location;
14886 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
14887 sb = pop_stmt_list (sb);
14889 if (omp_for)
14891 tree omp_par = make_node (OMP_PARALLEL);
14892 TREE_TYPE (omp_par) = void_type_node;
14893 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
14894 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
14895 TREE_SIDE_EFFECTS (bind) = 1;
14896 BIND_EXPR_BODY (bind) = sb;
14897 OMP_PARALLEL_BODY (omp_par) = bind;
14898 if (OMP_FOR_PRE_BODY (omp_for))
14900 add_stmt (OMP_FOR_PRE_BODY (omp_for));
14901 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
14903 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
14904 tree decl = TREE_OPERAND (init, 0);
14905 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
14906 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
14907 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
14908 if (TREE_CODE (t) != INTEGER_CST)
14910 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
14911 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14912 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
14913 OMP_CLAUSE_CHAIN (c) = clauses;
14914 clauses = c;
14916 if (TREE_CODE (incr) == MODIFY_EXPR)
14918 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
14919 if (TREE_CODE (t) != INTEGER_CST)
14921 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
14922 = c_get_temp_regvar (TREE_TYPE (t), t);
14923 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14924 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
14925 OMP_CLAUSE_CHAIN (c) = clauses;
14926 clauses = c;
14929 t = TREE_OPERAND (init, 1);
14930 if (TREE_CODE (t) != INTEGER_CST)
14932 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
14933 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
14934 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
14935 OMP_CLAUSE_CHAIN (c) = clauses;
14936 clauses = c;
14938 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
14939 OMP_CLAUSE_DECL (c) = decl;
14940 OMP_CLAUSE_CHAIN (c) = clauses;
14941 clauses = c;
14942 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
14943 OMP_CLAUSE_OPERAND (c, 0)
14944 = cilk_for_number_of_iterations (omp_for);
14945 OMP_CLAUSE_CHAIN (c) = clauses;
14946 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c);
14947 add_stmt (omp_par);
14950 block = c_end_compound_stmt (loc, block, true);
14951 add_stmt (block);
14955 /* Parse a transaction attribute (GCC Extension).
14957 transaction-attribute:
14958 attributes
14959 [ [ any-word ] ]
14961 The transactional memory language description is written for C++,
14962 and uses the C++0x attribute syntax. For compatibility, allow the
14963 bracket style for transactions in C as well. */
14965 static tree
14966 c_parser_transaction_attributes (c_parser *parser)
14968 tree attr_name, attr = NULL;
14970 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
14971 return c_parser_attributes (parser);
14973 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
14974 return NULL_TREE;
14975 c_parser_consume_token (parser);
14976 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
14977 goto error1;
14979 attr_name = c_parser_attribute_any_word (parser);
14980 if (attr_name)
14982 c_parser_consume_token (parser);
14983 attr = build_tree_list (attr_name, NULL_TREE);
14985 else
14986 c_parser_error (parser, "expected identifier");
14988 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14989 error1:
14990 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
14991 return attr;
14994 /* Parse a __transaction_atomic or __transaction_relaxed statement
14995 (GCC Extension).
14997 transaction-statement:
14998 __transaction_atomic transaction-attribute[opt] compound-statement
14999 __transaction_relaxed compound-statement
15001 Note that the only valid attribute is: "outer".
15004 static tree
15005 c_parser_transaction (c_parser *parser, enum rid keyword)
15007 unsigned int old_in = parser->in_transaction;
15008 unsigned int this_in = 1, new_in;
15009 location_t loc = c_parser_peek_token (parser)->location;
15010 tree stmt, attrs;
15012 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15013 || keyword == RID_TRANSACTION_RELAXED)
15014 && c_parser_next_token_is_keyword (parser, keyword));
15015 c_parser_consume_token (parser);
15017 if (keyword == RID_TRANSACTION_RELAXED)
15018 this_in |= TM_STMT_ATTR_RELAXED;
15019 else
15021 attrs = c_parser_transaction_attributes (parser);
15022 if (attrs)
15023 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
15026 /* Keep track if we're in the lexical scope of an outer transaction. */
15027 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
15029 parser->in_transaction = new_in;
15030 stmt = c_parser_compound_statement (parser);
15031 parser->in_transaction = old_in;
15033 if (flag_tm)
15034 stmt = c_finish_transaction (loc, stmt, this_in);
15035 else
15036 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15037 "%<__transaction_atomic%> without transactional memory support enabled"
15038 : "%<__transaction_relaxed %> "
15039 "without transactional memory support enabled"));
15041 return stmt;
15044 /* Parse a __transaction_atomic or __transaction_relaxed expression
15045 (GCC Extension).
15047 transaction-expression:
15048 __transaction_atomic ( expression )
15049 __transaction_relaxed ( expression )
15052 static struct c_expr
15053 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
15055 struct c_expr ret;
15056 unsigned int old_in = parser->in_transaction;
15057 unsigned int this_in = 1;
15058 location_t loc = c_parser_peek_token (parser)->location;
15059 tree attrs;
15061 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
15062 || keyword == RID_TRANSACTION_RELAXED)
15063 && c_parser_next_token_is_keyword (parser, keyword));
15064 c_parser_consume_token (parser);
15066 if (keyword == RID_TRANSACTION_RELAXED)
15067 this_in |= TM_STMT_ATTR_RELAXED;
15068 else
15070 attrs = c_parser_transaction_attributes (parser);
15071 if (attrs)
15072 this_in |= parse_tm_stmt_attr (attrs, 0);
15075 parser->in_transaction = this_in;
15076 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
15078 tree expr = c_parser_expression (parser).value;
15079 ret.original_type = TREE_TYPE (expr);
15080 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
15081 if (this_in & TM_STMT_ATTR_RELAXED)
15082 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
15083 SET_EXPR_LOCATION (ret.value, loc);
15084 ret.original_code = TRANSACTION_EXPR;
15085 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15087 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
15088 goto error;
15091 else
15093 error:
15094 ret.value = error_mark_node;
15095 ret.original_code = ERROR_MARK;
15096 ret.original_type = NULL;
15098 parser->in_transaction = old_in;
15100 if (!flag_tm)
15101 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
15102 "%<__transaction_atomic%> without transactional memory support enabled"
15103 : "%<__transaction_relaxed %> "
15104 "without transactional memory support enabled"));
15106 return ret;
15109 /* Parse a __transaction_cancel statement (GCC Extension).
15111 transaction-cancel-statement:
15112 __transaction_cancel transaction-attribute[opt] ;
15114 Note that the only valid attribute is "outer".
15117 static tree
15118 c_parser_transaction_cancel (c_parser *parser)
15120 location_t loc = c_parser_peek_token (parser)->location;
15121 tree attrs;
15122 bool is_outer = false;
15124 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
15125 c_parser_consume_token (parser);
15127 attrs = c_parser_transaction_attributes (parser);
15128 if (attrs)
15129 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
15131 if (!flag_tm)
15133 error_at (loc, "%<__transaction_cancel%> without "
15134 "transactional memory support enabled");
15135 goto ret_error;
15137 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
15139 error_at (loc, "%<__transaction_cancel%> within a "
15140 "%<__transaction_relaxed%>");
15141 goto ret_error;
15143 else if (is_outer)
15145 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
15146 && !is_tm_may_cancel_outer (current_function_decl))
15148 error_at (loc, "outer %<__transaction_cancel%> not "
15149 "within outer %<__transaction_atomic%>");
15150 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
15151 goto ret_error;
15154 else if (parser->in_transaction == 0)
15156 error_at (loc, "%<__transaction_cancel%> not within "
15157 "%<__transaction_atomic%>");
15158 goto ret_error;
15161 return add_stmt (build_tm_abort_call (loc, is_outer));
15163 ret_error:
15164 return build1 (NOP_EXPR, void_type_node, error_mark_node);
15167 /* Parse a single source file. */
15169 void
15170 c_parse_file (void)
15172 /* Use local storage to begin. If the first token is a pragma, parse it.
15173 If it is #pragma GCC pch_preprocess, then this will load a PCH file
15174 which will cause garbage collection. */
15175 c_parser tparser;
15177 memset (&tparser, 0, sizeof tparser);
15178 tparser.tokens = &tparser.tokens_buf[0];
15179 the_parser = &tparser;
15181 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
15182 c_parser_pragma_pch_preprocess (&tparser);
15184 the_parser = ggc_alloc<c_parser> ();
15185 *the_parser = tparser;
15186 if (tparser.tokens == &tparser.tokens_buf[0])
15187 the_parser->tokens = &the_parser->tokens_buf[0];
15189 /* Initialize EH, if we've been told to do so. */
15190 if (flag_exceptions)
15191 using_eh_for_cleanups ();
15193 c_parser_translation_unit (the_parser);
15194 the_parser = NULL;
15197 /* This function parses Cilk Plus array notation. The starting index is
15198 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
15199 return value of this function is a tree_node called VALUE_TREE of type
15200 ARRAY_NOTATION_REF. */
15202 static tree
15203 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
15204 tree array_value)
15206 c_token *token = NULL;
15207 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
15208 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
15209 tree array_type_domain = NULL_TREE;
15211 if (array_value == error_mark_node || initial_index == error_mark_node)
15213 /* No need to continue. If either of these 2 were true, then an error
15214 must be emitted already. Thus, no need to emit them twice. */
15215 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15216 return error_mark_node;
15219 array_type = TREE_TYPE (array_value);
15220 gcc_assert (array_type);
15221 if (TREE_CODE (array_type) != ARRAY_TYPE
15222 && TREE_CODE (array_type) != POINTER_TYPE)
15224 error_at (loc, "base of array section must be pointer or array type");
15225 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15226 return error_mark_node;
15228 type = TREE_TYPE (array_type);
15229 token = c_parser_peek_token (parser);
15231 if (token->type == CPP_EOF)
15233 c_parser_error (parser, "expected %<:%> or numeral");
15234 return value_tree;
15236 else if (token->type == CPP_COLON)
15238 if (!initial_index)
15240 /* If we are here, then we have a case like this A[:]. */
15241 c_parser_consume_token (parser);
15242 if (TREE_CODE (array_type) == POINTER_TYPE)
15244 error_at (loc, "start-index and length fields necessary for "
15245 "using array notations in pointers");
15246 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15247 return error_mark_node;
15249 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15251 error_at (loc, "array notations cannot be used with function "
15252 "type");
15253 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15254 return error_mark_node;
15256 array_type_domain = TYPE_DOMAIN (array_type);
15258 if (!array_type_domain)
15260 error_at (loc, "start-index and length fields necessary for "
15261 "using array notations in dimensionless arrays");
15262 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15263 return error_mark_node;
15266 start_index = TYPE_MINVAL (array_type_domain);
15267 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
15268 start_index);
15269 if (!TYPE_MAXVAL (array_type_domain)
15270 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
15272 error_at (loc, "start-index and length fields necessary for "
15273 "using array notations in variable-length arrays");
15274 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15275 return error_mark_node;
15277 end_index = TYPE_MAXVAL (array_type_domain);
15278 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
15279 end_index, integer_one_node);
15280 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
15281 stride = build_int_cst (integer_type_node, 1);
15282 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
15284 else if (initial_index != error_mark_node)
15286 /* If we are here, then there should be 2 possibilities:
15287 1. Array [EXPR : EXPR]
15288 2. Array [EXPR : EXPR : EXPR]
15290 start_index = initial_index;
15292 if (TREE_CODE (array_type) == FUNCTION_TYPE)
15294 error_at (loc, "array notations cannot be used with function "
15295 "type");
15296 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
15297 return error_mark_node;
15299 c_parser_consume_token (parser); /* consume the ':' */
15300 struct c_expr ce = c_parser_expression (parser);
15301 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15302 end_index = ce.value;
15303 if (!end_index || end_index == error_mark_node)
15305 c_parser_skip_to_end_of_block_or_statement (parser);
15306 return error_mark_node;
15308 if (c_parser_peek_token (parser)->type == CPP_COLON)
15310 c_parser_consume_token (parser);
15311 ce = c_parser_expression (parser);
15312 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
15313 stride = ce.value;
15314 if (!stride || stride == error_mark_node)
15316 c_parser_skip_to_end_of_block_or_statement (parser);
15317 return error_mark_node;
15321 else
15322 c_parser_error (parser, "expected array notation expression");
15324 else
15325 c_parser_error (parser, "expected array notation expression");
15327 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
15329 value_tree = build_array_notation_ref (loc, array_value, start_index,
15330 end_index, stride, type);
15331 if (value_tree != error_mark_node)
15332 SET_EXPR_LOCATION (value_tree, loc);
15333 return value_tree;
15336 #include "gt-c-c-parser.h"