C: Fix missing spaces in 'struct' fix-it hints
[official-gcc.git] / gcc / c / c-parser.c
blob0581899e307dd782cd1d6c45c0a8a050f9928f4b
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2016 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 "target.h"
42 #include "function.h"
43 #include "c-tree.h"
44 #include "timevar.h"
45 #include "stringpool.h"
46 #include "cgraph.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "varasm.h"
50 #include "trans-mem.h"
51 #include "c-family/c-pragma.h"
52 #include "c-lang.h"
53 #include "c-family/c-objc.h"
54 #include "plugin.h"
55 #include "omp-low.h"
56 #include "builtins.h"
57 #include "gomp-constants.h"
58 #include "c-family/c-indentation.h"
59 #include "gimple-expr.h"
60 #include "context.h"
61 #include "gcc-rich-location.h"
63 /* We need to walk over decls with incomplete struct/union/enum types
64 after parsing the whole translation unit.
65 In finish_decl(), if the decl is static, has incomplete
66 struct/union/enum type, it is appeneded to incomplete_record_decls.
67 In c_parser_translation_unit(), we iterate over incomplete_record_decls
68 and report error if any of the decls are still incomplete. */
70 vec<tree> incomplete_record_decls = vNULL;
72 void
73 set_c_expr_source_range (c_expr *expr,
74 location_t start, location_t finish)
76 expr->src_range.m_start = start;
77 expr->src_range.m_finish = finish;
78 if (expr->value)
79 set_source_range (expr->value, start, finish);
82 void
83 set_c_expr_source_range (c_expr *expr,
84 source_range src_range)
86 expr->src_range = src_range;
87 if (expr->value)
88 set_source_range (expr->value, src_range);
92 /* Initialization routine for this file. */
94 void
95 c_parse_init (void)
97 /* The only initialization required is of the reserved word
98 identifiers. */
99 unsigned int i;
100 tree id;
101 int mask = 0;
103 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
104 the c_token structure. */
105 gcc_assert (RID_MAX <= 255);
107 mask |= D_CXXONLY;
108 if (!flag_isoc99)
109 mask |= D_C99;
110 if (flag_no_asm)
112 mask |= D_ASM | D_EXT;
113 if (!flag_isoc99)
114 mask |= D_EXT89;
116 if (!c_dialect_objc ())
117 mask |= D_OBJC | D_CXX_OBJC;
119 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
120 for (i = 0; i < num_c_common_reswords; i++)
122 /* If a keyword is disabled, do not enter it into the table
123 and so create a canonical spelling that isn't a keyword. */
124 if (c_common_reswords[i].disable & mask)
126 if (warn_cxx_compat
127 && (c_common_reswords[i].disable & D_CXXWARN))
129 id = get_identifier (c_common_reswords[i].word);
130 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
131 C_IS_RESERVED_WORD (id) = 1;
133 continue;
136 id = get_identifier (c_common_reswords[i].word);
137 C_SET_RID_CODE (id, c_common_reswords[i].rid);
138 C_IS_RESERVED_WORD (id) = 1;
139 ridpointers [(int) c_common_reswords[i].rid] = id;
142 for (i = 0; i < NUM_INT_N_ENTS; i++)
144 /* We always create the symbols but they aren't always supported. */
145 char name[50];
146 sprintf (name, "__int%d", int_n_data[i].bitsize);
147 id = get_identifier (name);
148 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
149 C_IS_RESERVED_WORD (id) = 1;
153 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
154 and the C parser. Unlike the C++ lexer, the parser structure
155 stores the lexer information instead of using a separate structure.
156 Identifiers are separated into ordinary identifiers, type names,
157 keywords and some other Objective-C types of identifiers, and some
158 look-ahead is maintained.
160 ??? It might be a good idea to lex the whole file up front (as for
161 C++). It would then be possible to share more of the C and C++
162 lexer code, if desired. */
164 /* More information about the type of a CPP_NAME token. */
165 enum c_id_kind {
166 /* An ordinary identifier. */
167 C_ID_ID,
168 /* An identifier declared as a typedef name. */
169 C_ID_TYPENAME,
170 /* An identifier declared as an Objective-C class name. */
171 C_ID_CLASSNAME,
172 /* An address space identifier. */
173 C_ID_ADDRSPACE,
174 /* Not an identifier. */
175 C_ID_NONE
178 /* A single C token after string literal concatenation and conversion
179 of preprocessing tokens to tokens. */
180 struct GTY (()) c_token {
181 /* The kind of token. */
182 ENUM_BITFIELD (cpp_ttype) type : 8;
183 /* If this token is a CPP_NAME, this value indicates whether also
184 declared as some kind of type. Otherwise, it is C_ID_NONE. */
185 ENUM_BITFIELD (c_id_kind) id_kind : 8;
186 /* If this token is a keyword, this value indicates which keyword.
187 Otherwise, this value is RID_MAX. */
188 ENUM_BITFIELD (rid) keyword : 8;
189 /* If this token is a CPP_PRAGMA, this indicates the pragma that
190 was seen. Otherwise it is PRAGMA_NONE. */
191 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
192 /* The location at which this token was found. */
193 location_t location;
194 /* The value associated with this token, if any. */
195 tree value;
197 source_range get_range () const
199 return get_range_from_loc (line_table, location);
202 location_t get_finish () const
204 return get_range ().m_finish;
208 /* A parser structure recording information about the state and
209 context of parsing. Includes lexer information with up to two
210 tokens of look-ahead; more are not needed for C. */
211 struct GTY(()) c_parser {
212 /* The look-ahead tokens. */
213 c_token * GTY((skip)) tokens;
214 /* Buffer for look-ahead tokens. */
215 c_token tokens_buf[4];
216 /* How many look-ahead tokens are available (0 - 4, or
217 more if parsing from pre-lexed tokens). */
218 unsigned int tokens_avail;
219 /* True if a syntax error is being recovered from; false otherwise.
220 c_parser_error sets this flag. It should clear this flag when
221 enough tokens have been consumed to recover from the error. */
222 BOOL_BITFIELD error : 1;
223 /* True if we're processing a pragma, and shouldn't automatically
224 consume CPP_PRAGMA_EOL. */
225 BOOL_BITFIELD in_pragma : 1;
226 /* True if we're parsing the outermost block of an if statement. */
227 BOOL_BITFIELD in_if_block : 1;
228 /* True if we want to lex an untranslated string. */
229 BOOL_BITFIELD lex_untranslated_string : 1;
231 /* Objective-C specific parser/lexer information. */
233 /* True if we are in a context where the Objective-C "PQ" keywords
234 are considered keywords. */
235 BOOL_BITFIELD objc_pq_context : 1;
236 /* True if we are parsing a (potential) Objective-C foreach
237 statement. This is set to true after we parsed 'for (' and while
238 we wait for 'in' or ';' to decide if it's a standard C for loop or an
239 Objective-C foreach loop. */
240 BOOL_BITFIELD objc_could_be_foreach_context : 1;
241 /* The following flag is needed to contextualize Objective-C lexical
242 analysis. In some cases (e.g., 'int NSObject;'), it is
243 undesirable to bind an identifier to an Objective-C class, even
244 if a class with that name exists. */
245 BOOL_BITFIELD objc_need_raw_identifier : 1;
246 /* Nonzero if we're processing a __transaction statement. The value
247 is 1 | TM_STMT_ATTR_*. */
248 unsigned int in_transaction : 4;
249 /* True if we are in a context where the Objective-C "Property attribute"
250 keywords are valid. */
251 BOOL_BITFIELD objc_property_attr_context : 1;
253 /* Cilk Plus specific parser/lexer information. */
255 /* Buffer to hold all the tokens from parsing the vector attribute for the
256 SIMD-enabled functions (formerly known as elemental functions). */
257 vec <c_token, va_gc> *cilk_simd_fn_tokens;
261 /* The actual parser and external interface. ??? Does this need to be
262 garbage-collected? */
264 static GTY (()) c_parser *the_parser;
266 /* Read in and lex a single token, storing it in *TOKEN. */
268 static void
269 c_lex_one_token (c_parser *parser, c_token *token)
271 timevar_push (TV_LEX);
273 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
274 (parser->lex_untranslated_string
275 ? C_LEX_STRING_NO_TRANSLATE : 0));
276 token->id_kind = C_ID_NONE;
277 token->keyword = RID_MAX;
278 token->pragma_kind = PRAGMA_NONE;
280 switch (token->type)
282 case CPP_NAME:
284 tree decl;
286 bool objc_force_identifier = parser->objc_need_raw_identifier;
287 if (c_dialect_objc ())
288 parser->objc_need_raw_identifier = false;
290 if (C_IS_RESERVED_WORD (token->value))
292 enum rid rid_code = C_RID_CODE (token->value);
294 if (rid_code == RID_CXX_COMPAT_WARN)
296 warning_at (token->location,
297 OPT_Wc___compat,
298 "identifier %qE conflicts with C++ keyword",
299 token->value);
301 else if (rid_code >= RID_FIRST_ADDR_SPACE
302 && rid_code <= RID_LAST_ADDR_SPACE)
304 addr_space_t as;
305 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
306 targetm.addr_space.diagnose_usage (as, token->location);
307 token->id_kind = C_ID_ADDRSPACE;
308 token->keyword = rid_code;
309 break;
311 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
313 /* We found an Objective-C "pq" keyword (in, out,
314 inout, bycopy, byref, oneway). They need special
315 care because the interpretation depends on the
316 context. */
317 if (parser->objc_pq_context)
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
323 else if (parser->objc_could_be_foreach_context
324 && rid_code == RID_IN)
326 /* We are in Objective-C, inside a (potential)
327 foreach context (which means after having
328 parsed 'for (', but before having parsed ';'),
329 and we found 'in'. We consider it the keyword
330 which terminates the declaration at the
331 beginning of a foreach-statement. Note that
332 this means you can't use 'in' for anything else
333 in that context; in particular, in Objective-C
334 you can't use 'in' as the name of the running
335 variable in a C for loop. We could potentially
336 try to add code here to disambiguate, but it
337 seems a reasonable limitation. */
338 token->type = CPP_KEYWORD;
339 token->keyword = rid_code;
340 break;
342 /* Else, "pq" keywords outside of the "pq" context are
343 not keywords, and we fall through to the code for
344 normal tokens. */
346 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
348 /* We found an Objective-C "property attribute"
349 keyword (getter, setter, readonly, etc). These are
350 only valid in the property context. */
351 if (parser->objc_property_attr_context)
353 token->type = CPP_KEYWORD;
354 token->keyword = rid_code;
355 break;
357 /* Else they are not special keywords.
360 else if (c_dialect_objc ()
361 && (OBJC_IS_AT_KEYWORD (rid_code)
362 || OBJC_IS_CXX_KEYWORD (rid_code)))
364 /* We found one of the Objective-C "@" keywords (defs,
365 selector, synchronized, etc) or one of the
366 Objective-C "cxx" keywords (class, private,
367 protected, public, try, catch, throw) without a
368 preceding '@' sign. Do nothing and fall through to
369 the code for normal tokens (in C++ we would still
370 consider the CXX ones keywords, but not in C). */
373 else
375 token->type = CPP_KEYWORD;
376 token->keyword = rid_code;
377 break;
381 decl = lookup_name (token->value);
382 if (decl)
384 if (TREE_CODE (decl) == TYPE_DECL)
386 token->id_kind = C_ID_TYPENAME;
387 break;
390 else if (c_dialect_objc ())
392 tree objc_interface_decl = objc_is_class_name (token->value);
393 /* Objective-C class names are in the same namespace as
394 variables and typedefs, and hence are shadowed by local
395 declarations. */
396 if (objc_interface_decl
397 && (!objc_force_identifier || global_bindings_p ()))
399 token->value = objc_interface_decl;
400 token->id_kind = C_ID_CLASSNAME;
401 break;
404 token->id_kind = C_ID_ID;
406 break;
407 case CPP_AT_NAME:
408 /* This only happens in Objective-C; it must be a keyword. */
409 token->type = CPP_KEYWORD;
410 switch (C_RID_CODE (token->value))
412 /* Replace 'class' with '@class', 'private' with '@private',
413 etc. This prevents confusion with the C++ keyword
414 'class', and makes the tokens consistent with other
415 Objective-C 'AT' keywords. For example '@class' is
416 reported as RID_AT_CLASS which is consistent with
417 '@synchronized', which is reported as
418 RID_AT_SYNCHRONIZED.
420 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
421 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
422 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
423 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
424 case RID_THROW: token->keyword = RID_AT_THROW; break;
425 case RID_TRY: token->keyword = RID_AT_TRY; break;
426 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
427 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
428 default: token->keyword = C_RID_CODE (token->value);
430 break;
431 case CPP_COLON:
432 case CPP_COMMA:
433 case CPP_CLOSE_PAREN:
434 case CPP_SEMICOLON:
435 /* These tokens may affect the interpretation of any identifiers
436 following, if doing Objective-C. */
437 if (c_dialect_objc ())
438 parser->objc_need_raw_identifier = false;
439 break;
440 case CPP_PRAGMA:
441 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
442 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
443 token->value = NULL;
444 break;
445 default:
446 break;
448 timevar_pop (TV_LEX);
451 /* Return a pointer to the next token from PARSER, reading it in if
452 necessary. */
454 static inline c_token *
455 c_parser_peek_token (c_parser *parser)
457 if (parser->tokens_avail == 0)
459 c_lex_one_token (parser, &parser->tokens[0]);
460 parser->tokens_avail = 1;
462 return &parser->tokens[0];
465 /* Return true if the next token from PARSER has the indicated
466 TYPE. */
468 static inline bool
469 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
471 return c_parser_peek_token (parser)->type == type;
474 /* Return true if the next token from PARSER does not have the
475 indicated TYPE. */
477 static inline bool
478 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
480 return !c_parser_next_token_is (parser, type);
483 /* Return true if the next token from PARSER is the indicated
484 KEYWORD. */
486 static inline bool
487 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
489 return c_parser_peek_token (parser)->keyword == keyword;
492 /* Return a pointer to the next-but-one token from PARSER, reading it
493 in if necessary. The next token is already read in. */
495 static c_token *
496 c_parser_peek_2nd_token (c_parser *parser)
498 if (parser->tokens_avail >= 2)
499 return &parser->tokens[1];
500 gcc_assert (parser->tokens_avail == 1);
501 gcc_assert (parser->tokens[0].type != CPP_EOF);
502 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
503 c_lex_one_token (parser, &parser->tokens[1]);
504 parser->tokens_avail = 2;
505 return &parser->tokens[1];
508 /* Return a pointer to the Nth token from PARSER, reading it
509 in if necessary. The N-1th token is already read in. */
511 static c_token *
512 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
514 /* N is 1-based, not zero-based. */
515 gcc_assert (n > 0);
517 if (parser->tokens_avail >= n)
518 return &parser->tokens[n - 1];
519 gcc_assert (parser->tokens_avail == n - 1);
520 c_lex_one_token (parser, &parser->tokens[n - 1]);
521 parser->tokens_avail = n;
522 return &parser->tokens[n - 1];
525 bool
526 c_keyword_starts_typename (enum rid keyword)
528 switch (keyword)
530 case RID_UNSIGNED:
531 case RID_LONG:
532 case RID_SHORT:
533 case RID_SIGNED:
534 case RID_COMPLEX:
535 case RID_INT:
536 case RID_CHAR:
537 case RID_FLOAT:
538 case RID_DOUBLE:
539 case RID_VOID:
540 case RID_DFLOAT32:
541 case RID_DFLOAT64:
542 case RID_DFLOAT128:
543 CASE_RID_FLOATN_NX:
544 case RID_BOOL:
545 case RID_ENUM:
546 case RID_STRUCT:
547 case RID_UNION:
548 case RID_TYPEOF:
549 case RID_CONST:
550 case RID_ATOMIC:
551 case RID_VOLATILE:
552 case RID_RESTRICT:
553 case RID_ATTRIBUTE:
554 case RID_FRACT:
555 case RID_ACCUM:
556 case RID_SAT:
557 case RID_AUTO_TYPE:
558 return true;
559 default:
560 if (keyword >= RID_FIRST_INT_N
561 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
562 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
563 return true;
564 return false;
568 /* Return true if TOKEN can start a type name,
569 false otherwise. */
570 static bool
571 c_token_starts_typename (c_token *token)
573 switch (token->type)
575 case CPP_NAME:
576 switch (token->id_kind)
578 case C_ID_ID:
579 return false;
580 case C_ID_ADDRSPACE:
581 return true;
582 case C_ID_TYPENAME:
583 return true;
584 case C_ID_CLASSNAME:
585 gcc_assert (c_dialect_objc ());
586 return true;
587 default:
588 gcc_unreachable ();
590 case CPP_KEYWORD:
591 return c_keyword_starts_typename (token->keyword);
592 case CPP_LESS:
593 if (c_dialect_objc ())
594 return true;
595 return false;
596 default:
597 return false;
601 enum c_lookahead_kind {
602 /* Always treat unknown identifiers as typenames. */
603 cla_prefer_type,
605 /* Could be parsing a nonabstract declarator. Only treat an identifier
606 as a typename if followed by another identifier or a star. */
607 cla_nonabstract_decl,
609 /* Never treat identifiers as typenames. */
610 cla_prefer_id
613 /* Return true if the next token from PARSER can start a type name,
614 false otherwise. LA specifies how to do lookahead in order to
615 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
617 static inline bool
618 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
620 c_token *token = c_parser_peek_token (parser);
621 if (c_token_starts_typename (token))
622 return true;
624 /* Try a bit harder to detect an unknown typename. */
625 if (la != cla_prefer_id
626 && token->type == CPP_NAME
627 && token->id_kind == C_ID_ID
629 /* Do not try too hard when we could have "object in array". */
630 && !parser->objc_could_be_foreach_context
632 && (la == cla_prefer_type
633 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
634 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
636 /* Only unknown identifiers. */
637 && !lookup_name (token->value))
638 return true;
640 return false;
643 /* Return true if TOKEN is a type qualifier, false otherwise. */
644 static bool
645 c_token_is_qualifier (c_token *token)
647 switch (token->type)
649 case CPP_NAME:
650 switch (token->id_kind)
652 case C_ID_ADDRSPACE:
653 return true;
654 default:
655 return false;
657 case CPP_KEYWORD:
658 switch (token->keyword)
660 case RID_CONST:
661 case RID_VOLATILE:
662 case RID_RESTRICT:
663 case RID_ATTRIBUTE:
664 case RID_ATOMIC:
665 return true;
666 default:
667 return false;
669 case CPP_LESS:
670 return false;
671 default:
672 gcc_unreachable ();
676 /* Return true if the next token from PARSER is a type qualifier,
677 false otherwise. */
678 static inline bool
679 c_parser_next_token_is_qualifier (c_parser *parser)
681 c_token *token = c_parser_peek_token (parser);
682 return c_token_is_qualifier (token);
685 /* Return true if TOKEN can start declaration specifiers, false
686 otherwise. */
687 static bool
688 c_token_starts_declspecs (c_token *token)
690 switch (token->type)
692 case CPP_NAME:
693 switch (token->id_kind)
695 case C_ID_ID:
696 return false;
697 case C_ID_ADDRSPACE:
698 return true;
699 case C_ID_TYPENAME:
700 return true;
701 case C_ID_CLASSNAME:
702 gcc_assert (c_dialect_objc ());
703 return true;
704 default:
705 gcc_unreachable ();
707 case CPP_KEYWORD:
708 switch (token->keyword)
710 case RID_STATIC:
711 case RID_EXTERN:
712 case RID_REGISTER:
713 case RID_TYPEDEF:
714 case RID_INLINE:
715 case RID_NORETURN:
716 case RID_AUTO:
717 case RID_THREAD:
718 case RID_UNSIGNED:
719 case RID_LONG:
720 case RID_SHORT:
721 case RID_SIGNED:
722 case RID_COMPLEX:
723 case RID_INT:
724 case RID_CHAR:
725 case RID_FLOAT:
726 case RID_DOUBLE:
727 case RID_VOID:
728 case RID_DFLOAT32:
729 case RID_DFLOAT64:
730 case RID_DFLOAT128:
731 CASE_RID_FLOATN_NX:
732 case RID_BOOL:
733 case RID_ENUM:
734 case RID_STRUCT:
735 case RID_UNION:
736 case RID_TYPEOF:
737 case RID_CONST:
738 case RID_VOLATILE:
739 case RID_RESTRICT:
740 case RID_ATTRIBUTE:
741 case RID_FRACT:
742 case RID_ACCUM:
743 case RID_SAT:
744 case RID_ALIGNAS:
745 case RID_ATOMIC:
746 case RID_AUTO_TYPE:
747 return true;
748 default:
749 if (token->keyword >= RID_FIRST_INT_N
750 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
751 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
752 return true;
753 return false;
755 case CPP_LESS:
756 if (c_dialect_objc ())
757 return true;
758 return false;
759 default:
760 return false;
765 /* Return true if TOKEN can start declaration specifiers or a static
766 assertion, false otherwise. */
767 static bool
768 c_token_starts_declaration (c_token *token)
770 if (c_token_starts_declspecs (token)
771 || token->keyword == RID_STATIC_ASSERT)
772 return true;
773 else
774 return false;
777 /* Return true if the next token from PARSER can start declaration
778 specifiers, false otherwise. */
779 static inline bool
780 c_parser_next_token_starts_declspecs (c_parser *parser)
782 c_token *token = c_parser_peek_token (parser);
784 /* In Objective-C, a classname normally starts a declspecs unless it
785 is immediately followed by a dot. In that case, it is the
786 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
787 setter/getter on the class. c_token_starts_declspecs() can't
788 differentiate between the two cases because it only checks the
789 current token, so we have a special check here. */
790 if (c_dialect_objc ()
791 && token->type == CPP_NAME
792 && token->id_kind == C_ID_CLASSNAME
793 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
794 return false;
796 return c_token_starts_declspecs (token);
799 /* Return true if the next tokens from PARSER can start declaration
800 specifiers or a static assertion, false otherwise. */
801 static inline bool
802 c_parser_next_tokens_start_declaration (c_parser *parser)
804 c_token *token = c_parser_peek_token (parser);
806 /* Same as above. */
807 if (c_dialect_objc ()
808 && token->type == CPP_NAME
809 && token->id_kind == C_ID_CLASSNAME
810 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
811 return false;
813 /* Labels do not start declarations. */
814 if (token->type == CPP_NAME
815 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
816 return false;
818 if (c_token_starts_declaration (token))
819 return true;
821 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
822 return true;
824 return false;
827 /* Consume the next token from PARSER. */
829 static void
830 c_parser_consume_token (c_parser *parser)
832 gcc_assert (parser->tokens_avail >= 1);
833 gcc_assert (parser->tokens[0].type != CPP_EOF);
834 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
835 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
836 if (parser->tokens != &parser->tokens_buf[0])
837 parser->tokens++;
838 else if (parser->tokens_avail == 2)
839 parser->tokens[0] = parser->tokens[1];
840 parser->tokens_avail--;
843 /* Expect the current token to be a #pragma. Consume it and remember
844 that we've begun parsing a pragma. */
846 static void
847 c_parser_consume_pragma (c_parser *parser)
849 gcc_assert (!parser->in_pragma);
850 gcc_assert (parser->tokens_avail >= 1);
851 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
852 if (parser->tokens != &parser->tokens_buf[0])
853 parser->tokens++;
854 else if (parser->tokens_avail == 2)
855 parser->tokens[0] = parser->tokens[1];
856 parser->tokens_avail--;
857 parser->in_pragma = true;
860 /* Update the global input_location from TOKEN. */
861 static inline void
862 c_parser_set_source_position_from_token (c_token *token)
864 if (token->type != CPP_EOF)
866 input_location = token->location;
870 /* Helper function for c_parser_error.
871 Having peeked a token of kind TOK1_KIND that might signify
872 a conflict marker, peek successor tokens to determine
873 if we actually do have a conflict marker.
874 Specifically, we consider a run of 7 '<', '=' or '>' characters
875 at the start of a line as a conflict marker.
876 These come through the lexer as three pairs and a single,
877 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
878 If it returns true, *OUT_LOC is written to with the location/range
879 of the marker. */
881 static bool
882 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
883 location_t *out_loc)
885 c_token *token2 = c_parser_peek_2nd_token (parser);
886 if (token2->type != tok1_kind)
887 return false;
888 c_token *token3 = c_parser_peek_nth_token (parser, 3);
889 if (token3->type != tok1_kind)
890 return false;
891 c_token *token4 = c_parser_peek_nth_token (parser, 4);
892 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
893 return false;
895 /* It must be at the start of the line. */
896 location_t start_loc = c_parser_peek_token (parser)->location;
897 if (LOCATION_COLUMN (start_loc) != 1)
898 return false;
900 /* We have a conflict marker. Construct a location of the form:
901 <<<<<<<
902 ^~~~~~~
903 with start == caret, finishing at the end of the marker. */
904 location_t finish_loc = get_finish (token4->location);
905 *out_loc = make_location (start_loc, start_loc, finish_loc);
907 return true;
910 /* Issue a diagnostic of the form
911 FILE:LINE: MESSAGE before TOKEN
912 where TOKEN is the next token in the input stream of PARSER.
913 MESSAGE (specified by the caller) is usually of the form "expected
914 OTHER-TOKEN".
916 Do not issue a diagnostic if still recovering from an error.
918 ??? This is taken from the C++ parser, but building up messages in
919 this way is not i18n-friendly and some other approach should be
920 used. */
922 static void
923 c_parser_error (c_parser *parser, const char *gmsgid)
925 c_token *token = c_parser_peek_token (parser);
926 if (parser->error)
927 return;
928 parser->error = true;
929 if (!gmsgid)
930 return;
932 /* If this is actually a conflict marker, report it as such. */
933 if (token->type == CPP_LSHIFT
934 || token->type == CPP_RSHIFT
935 || token->type == CPP_EQ_EQ)
937 location_t loc;
938 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
940 error_at (loc, "version control conflict marker in file");
941 return;
945 /* This diagnostic makes more sense if it is tagged to the line of
946 the token we just peeked at. */
947 c_parser_set_source_position_from_token (token);
948 c_parse_error (gmsgid,
949 /* Because c_parse_error does not understand
950 CPP_KEYWORD, keywords are treated like
951 identifiers. */
952 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
953 /* ??? The C parser does not save the cpp flags of a
954 token, we need to pass 0 here and we will not get
955 the source spelling of some tokens but rather the
956 canonical spelling. */
957 token->value, /*flags=*/0);
960 /* If the next token is of the indicated TYPE, consume it. Otherwise,
961 issue the error MSGID. If MSGID is NULL then a message has already
962 been produced and no message will be produced this time. Returns
963 true if found, false otherwise. */
965 static bool
966 c_parser_require (c_parser *parser,
967 enum cpp_ttype type,
968 const char *msgid)
970 if (c_parser_next_token_is (parser, type))
972 c_parser_consume_token (parser);
973 return true;
975 else
977 c_parser_error (parser, msgid);
978 return false;
982 /* If the next token is the indicated keyword, consume it. Otherwise,
983 issue the error MSGID. Returns true if found, false otherwise. */
985 static bool
986 c_parser_require_keyword (c_parser *parser,
987 enum rid keyword,
988 const char *msgid)
990 if (c_parser_next_token_is_keyword (parser, keyword))
992 c_parser_consume_token (parser);
993 return true;
995 else
997 c_parser_error (parser, msgid);
998 return false;
1002 /* Like c_parser_require, except that tokens will be skipped until the
1003 desired token is found. An error message is still produced if the
1004 next token is not as expected. If MSGID is NULL then a message has
1005 already been produced and no message will be produced this
1006 time. */
1008 static void
1009 c_parser_skip_until_found (c_parser *parser,
1010 enum cpp_ttype type,
1011 const char *msgid)
1013 unsigned nesting_depth = 0;
1015 if (c_parser_require (parser, type, msgid))
1016 return;
1018 /* Skip tokens until the desired token is found. */
1019 while (true)
1021 /* Peek at the next token. */
1022 c_token *token = c_parser_peek_token (parser);
1023 /* If we've reached the token we want, consume it and stop. */
1024 if (token->type == type && !nesting_depth)
1026 c_parser_consume_token (parser);
1027 break;
1030 /* If we've run out of tokens, stop. */
1031 if (token->type == CPP_EOF)
1032 return;
1033 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1034 return;
1035 if (token->type == CPP_OPEN_BRACE
1036 || token->type == CPP_OPEN_PAREN
1037 || token->type == CPP_OPEN_SQUARE)
1038 ++nesting_depth;
1039 else if (token->type == CPP_CLOSE_BRACE
1040 || token->type == CPP_CLOSE_PAREN
1041 || token->type == CPP_CLOSE_SQUARE)
1043 if (nesting_depth-- == 0)
1044 break;
1046 /* Consume this token. */
1047 c_parser_consume_token (parser);
1049 parser->error = false;
1052 /* Skip tokens until the end of a parameter is found, but do not
1053 consume the comma, semicolon or closing delimiter. */
1055 static void
1056 c_parser_skip_to_end_of_parameter (c_parser *parser)
1058 unsigned nesting_depth = 0;
1060 while (true)
1062 c_token *token = c_parser_peek_token (parser);
1063 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1064 && !nesting_depth)
1065 break;
1066 /* If we've run out of tokens, stop. */
1067 if (token->type == CPP_EOF)
1068 return;
1069 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1070 return;
1071 if (token->type == CPP_OPEN_BRACE
1072 || token->type == CPP_OPEN_PAREN
1073 || token->type == CPP_OPEN_SQUARE)
1074 ++nesting_depth;
1075 else if (token->type == CPP_CLOSE_BRACE
1076 || token->type == CPP_CLOSE_PAREN
1077 || token->type == CPP_CLOSE_SQUARE)
1079 if (nesting_depth-- == 0)
1080 break;
1082 /* Consume this token. */
1083 c_parser_consume_token (parser);
1085 parser->error = false;
1088 /* Expect to be at the end of the pragma directive and consume an
1089 end of line marker. */
1091 static void
1092 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1094 gcc_assert (parser->in_pragma);
1095 parser->in_pragma = false;
1097 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1098 c_parser_error (parser, "expected end of line");
1100 cpp_ttype token_type;
1103 c_token *token = c_parser_peek_token (parser);
1104 token_type = token->type;
1105 if (token_type == CPP_EOF)
1106 break;
1107 c_parser_consume_token (parser);
1109 while (token_type != CPP_PRAGMA_EOL);
1111 parser->error = false;
1114 /* Skip tokens until we have consumed an entire block, or until we
1115 have consumed a non-nested ';'. */
1117 static void
1118 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1120 unsigned nesting_depth = 0;
1121 bool save_error = parser->error;
1123 while (true)
1125 c_token *token;
1127 /* Peek at the next token. */
1128 token = c_parser_peek_token (parser);
1130 switch (token->type)
1132 case CPP_EOF:
1133 return;
1135 case CPP_PRAGMA_EOL:
1136 if (parser->in_pragma)
1137 return;
1138 break;
1140 case CPP_SEMICOLON:
1141 /* If the next token is a ';', we have reached the
1142 end of the statement. */
1143 if (!nesting_depth)
1145 /* Consume the ';'. */
1146 c_parser_consume_token (parser);
1147 goto finished;
1149 break;
1151 case CPP_CLOSE_BRACE:
1152 /* If the next token is a non-nested '}', then we have
1153 reached the end of the current block. */
1154 if (nesting_depth == 0 || --nesting_depth == 0)
1156 c_parser_consume_token (parser);
1157 goto finished;
1159 break;
1161 case CPP_OPEN_BRACE:
1162 /* If it the next token is a '{', then we are entering a new
1163 block. Consume the entire block. */
1164 ++nesting_depth;
1165 break;
1167 case CPP_PRAGMA:
1168 /* If we see a pragma, consume the whole thing at once. We
1169 have some safeguards against consuming pragmas willy-nilly.
1170 Normally, we'd expect to be here with parser->error set,
1171 which disables these safeguards. But it's possible to get
1172 here for secondary error recovery, after parser->error has
1173 been cleared. */
1174 c_parser_consume_pragma (parser);
1175 c_parser_skip_to_pragma_eol (parser);
1176 parser->error = save_error;
1177 continue;
1179 default:
1180 break;
1183 c_parser_consume_token (parser);
1186 finished:
1187 parser->error = false;
1190 /* CPP's options (initialized by c-opts.c). */
1191 extern cpp_options *cpp_opts;
1193 /* Save the warning flags which are controlled by __extension__. */
1195 static inline int
1196 disable_extension_diagnostics (void)
1198 int ret = (pedantic
1199 | (warn_pointer_arith << 1)
1200 | (warn_traditional << 2)
1201 | (flag_iso << 3)
1202 | (warn_long_long << 4)
1203 | (warn_cxx_compat << 5)
1204 | (warn_overlength_strings << 6)
1205 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1206 play tricks to properly restore it. */
1207 | ((warn_c90_c99_compat == 1) << 7)
1208 | ((warn_c90_c99_compat == -1) << 8)
1209 /* Similarly for warn_c99_c11_compat. */
1210 | ((warn_c99_c11_compat == 1) << 9)
1211 | ((warn_c99_c11_compat == -1) << 10)
1213 cpp_opts->cpp_pedantic = pedantic = 0;
1214 warn_pointer_arith = 0;
1215 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1216 flag_iso = 0;
1217 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1218 warn_cxx_compat = 0;
1219 warn_overlength_strings = 0;
1220 warn_c90_c99_compat = 0;
1221 warn_c99_c11_compat = 0;
1222 return ret;
1225 /* Restore the warning flags which are controlled by __extension__.
1226 FLAGS is the return value from disable_extension_diagnostics. */
1228 static inline void
1229 restore_extension_diagnostics (int flags)
1231 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1232 warn_pointer_arith = (flags >> 1) & 1;
1233 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1234 flag_iso = (flags >> 3) & 1;
1235 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1236 warn_cxx_compat = (flags >> 5) & 1;
1237 warn_overlength_strings = (flags >> 6) & 1;
1238 /* See above for why is this needed. */
1239 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1240 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1243 /* Possibly kinds of declarator to parse. */
1244 enum c_dtr_syn {
1245 /* A normal declarator with an identifier. */
1246 C_DTR_NORMAL,
1247 /* An abstract declarator (maybe empty). */
1248 C_DTR_ABSTRACT,
1249 /* A parameter declarator: may be either, but after a type name does
1250 not redeclare a typedef name as an identifier if it can
1251 alternatively be interpreted as a typedef name; see DR#009,
1252 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1253 following DR#249. For example, given a typedef T, "int T" and
1254 "int *T" are valid parameter declarations redeclaring T, while
1255 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1256 abstract declarators rather than involving redundant parentheses;
1257 the same applies with attributes inside the parentheses before
1258 "T". */
1259 C_DTR_PARM
1262 /* The binary operation precedence levels, where 0 is a dummy lowest level
1263 used for the bottom of the stack. */
1264 enum c_parser_prec {
1265 PREC_NONE,
1266 PREC_LOGOR,
1267 PREC_LOGAND,
1268 PREC_BITOR,
1269 PREC_BITXOR,
1270 PREC_BITAND,
1271 PREC_EQ,
1272 PREC_REL,
1273 PREC_SHIFT,
1274 PREC_ADD,
1275 PREC_MULT,
1276 NUM_PRECS
1279 /* Helper data structure for parsing #pragma acc routine. */
1280 struct oacc_routine_data {
1281 bool error_seen; /* Set if error has been reported. */
1282 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1283 tree clauses;
1284 location_t loc;
1287 static void c_parser_external_declaration (c_parser *);
1288 static void c_parser_asm_definition (c_parser *);
1289 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1290 bool, bool, tree *, vec<c_token>,
1291 struct oacc_routine_data * = NULL);
1292 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1293 static void c_parser_static_assert_declaration (c_parser *);
1294 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1295 bool, bool, bool, enum c_lookahead_kind);
1296 static struct c_typespec c_parser_enum_specifier (c_parser *);
1297 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1298 static tree c_parser_struct_declaration (c_parser *);
1299 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1300 static tree c_parser_alignas_specifier (c_parser *);
1301 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1302 bool *);
1303 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1304 c_dtr_syn, bool *);
1305 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1306 bool,
1307 struct c_declarator *);
1308 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1309 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1310 tree);
1311 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1312 static tree c_parser_simple_asm_expr (c_parser *);
1313 static tree c_parser_attributes (c_parser *);
1314 static struct c_type_name *c_parser_type_name (c_parser *);
1315 static struct c_expr c_parser_initializer (c_parser *);
1316 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1317 struct obstack *);
1318 static void c_parser_initelt (c_parser *, struct obstack *);
1319 static void c_parser_initval (c_parser *, struct c_expr *,
1320 struct obstack *);
1321 static tree c_parser_compound_statement (c_parser *);
1322 static void c_parser_compound_statement_nostart (c_parser *);
1323 static void c_parser_label (c_parser *);
1324 static void c_parser_statement (c_parser *, bool *);
1325 static void c_parser_statement_after_labels (c_parser *, bool *,
1326 vec<tree> * = NULL);
1327 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1328 static void c_parser_switch_statement (c_parser *, bool *);
1329 static void c_parser_while_statement (c_parser *, bool, bool *);
1330 static void c_parser_do_statement (c_parser *, bool);
1331 static void c_parser_for_statement (c_parser *, bool, bool *);
1332 static tree c_parser_asm_statement (c_parser *);
1333 static tree c_parser_asm_operands (c_parser *);
1334 static tree c_parser_asm_goto_operands (c_parser *);
1335 static tree c_parser_asm_clobbers (c_parser *);
1336 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1337 tree = NULL_TREE);
1338 static struct c_expr c_parser_conditional_expression (c_parser *,
1339 struct c_expr *, tree);
1340 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1341 tree);
1342 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1343 static struct c_expr c_parser_unary_expression (c_parser *);
1344 static struct c_expr c_parser_sizeof_expression (c_parser *);
1345 static struct c_expr c_parser_alignof_expression (c_parser *);
1346 static struct c_expr c_parser_postfix_expression (c_parser *);
1347 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1348 struct c_type_name *,
1349 location_t);
1350 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1351 location_t loc,
1352 struct c_expr);
1353 static tree c_parser_transaction (c_parser *, enum rid);
1354 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1355 static tree c_parser_transaction_cancel (c_parser *);
1356 static struct c_expr c_parser_expression (c_parser *);
1357 static struct c_expr c_parser_expression_conv (c_parser *);
1358 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1359 vec<tree, va_gc> **, location_t *,
1360 tree *, vec<location_t> *,
1361 unsigned int * = NULL);
1362 static void c_parser_oacc_declare (c_parser *);
1363 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1364 static void c_parser_oacc_update (c_parser *);
1365 static void c_parser_omp_construct (c_parser *, bool *);
1366 static void c_parser_omp_threadprivate (c_parser *);
1367 static void c_parser_omp_barrier (c_parser *);
1368 static void c_parser_omp_flush (c_parser *);
1369 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1370 tree, tree *, bool *);
1371 static void c_parser_omp_taskwait (c_parser *);
1372 static void c_parser_omp_taskyield (c_parser *);
1373 static void c_parser_omp_cancel (c_parser *);
1375 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1376 pragma_stmt, pragma_compound };
1377 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1378 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1379 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1380 static void c_parser_omp_end_declare_target (c_parser *);
1381 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1382 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1383 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1385 /* These Objective-C parser functions are only ever called when
1386 compiling Objective-C. */
1387 static void c_parser_objc_class_definition (c_parser *, tree);
1388 static void c_parser_objc_class_instance_variables (c_parser *);
1389 static void c_parser_objc_class_declaration (c_parser *);
1390 static void c_parser_objc_alias_declaration (c_parser *);
1391 static void c_parser_objc_protocol_definition (c_parser *, tree);
1392 static bool c_parser_objc_method_type (c_parser *);
1393 static void c_parser_objc_method_definition (c_parser *);
1394 static void c_parser_objc_methodprotolist (c_parser *);
1395 static void c_parser_objc_methodproto (c_parser *);
1396 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1397 static tree c_parser_objc_type_name (c_parser *);
1398 static tree c_parser_objc_protocol_refs (c_parser *);
1399 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1400 static void c_parser_objc_synchronized_statement (c_parser *);
1401 static tree c_parser_objc_selector (c_parser *);
1402 static tree c_parser_objc_selector_arg (c_parser *);
1403 static tree c_parser_objc_receiver (c_parser *);
1404 static tree c_parser_objc_message_args (c_parser *);
1405 static tree c_parser_objc_keywordexpr (c_parser *);
1406 static void c_parser_objc_at_property_declaration (c_parser *);
1407 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1408 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1409 static bool c_parser_objc_diagnose_bad_element_prefix
1410 (c_parser *, struct c_declspecs *);
1412 /* Cilk Plus supporting routines. */
1413 static void c_parser_cilk_simd (c_parser *, bool *);
1414 static void c_parser_cilk_for (c_parser *, tree, bool *);
1415 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1416 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1417 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1418 static void c_parser_cilk_grainsize (c_parser *, bool *);
1420 /* Parse a translation unit (C90 6.7, C99 6.9).
1422 translation-unit:
1423 external-declarations
1425 external-declarations:
1426 external-declaration
1427 external-declarations external-declaration
1429 GNU extensions:
1431 translation-unit:
1432 empty
1435 static void
1436 c_parser_translation_unit (c_parser *parser)
1438 if (c_parser_next_token_is (parser, CPP_EOF))
1440 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1441 "ISO C forbids an empty translation unit");
1443 else
1445 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1446 mark_valid_location_for_stdc_pragma (false);
1449 ggc_collect ();
1450 c_parser_external_declaration (parser);
1451 obstack_free (&parser_obstack, obstack_position);
1453 while (c_parser_next_token_is_not (parser, CPP_EOF));
1456 unsigned int i;
1457 tree decl;
1458 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1459 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1460 error ("storage size of %q+D isn%'t known", decl);
1463 /* Parse an external declaration (C90 6.7, C99 6.9).
1465 external-declaration:
1466 function-definition
1467 declaration
1469 GNU extensions:
1471 external-declaration:
1472 asm-definition
1474 __extension__ external-declaration
1476 Objective-C:
1478 external-declaration:
1479 objc-class-definition
1480 objc-class-declaration
1481 objc-alias-declaration
1482 objc-protocol-definition
1483 objc-method-definition
1484 @end
1487 static void
1488 c_parser_external_declaration (c_parser *parser)
1490 int ext;
1491 switch (c_parser_peek_token (parser)->type)
1493 case CPP_KEYWORD:
1494 switch (c_parser_peek_token (parser)->keyword)
1496 case RID_EXTENSION:
1497 ext = disable_extension_diagnostics ();
1498 c_parser_consume_token (parser);
1499 c_parser_external_declaration (parser);
1500 restore_extension_diagnostics (ext);
1501 break;
1502 case RID_ASM:
1503 c_parser_asm_definition (parser);
1504 break;
1505 case RID_AT_INTERFACE:
1506 case RID_AT_IMPLEMENTATION:
1507 gcc_assert (c_dialect_objc ());
1508 c_parser_objc_class_definition (parser, NULL_TREE);
1509 break;
1510 case RID_AT_CLASS:
1511 gcc_assert (c_dialect_objc ());
1512 c_parser_objc_class_declaration (parser);
1513 break;
1514 case RID_AT_ALIAS:
1515 gcc_assert (c_dialect_objc ());
1516 c_parser_objc_alias_declaration (parser);
1517 break;
1518 case RID_AT_PROTOCOL:
1519 gcc_assert (c_dialect_objc ());
1520 c_parser_objc_protocol_definition (parser, NULL_TREE);
1521 break;
1522 case RID_AT_PROPERTY:
1523 gcc_assert (c_dialect_objc ());
1524 c_parser_objc_at_property_declaration (parser);
1525 break;
1526 case RID_AT_SYNTHESIZE:
1527 gcc_assert (c_dialect_objc ());
1528 c_parser_objc_at_synthesize_declaration (parser);
1529 break;
1530 case RID_AT_DYNAMIC:
1531 gcc_assert (c_dialect_objc ());
1532 c_parser_objc_at_dynamic_declaration (parser);
1533 break;
1534 case RID_AT_END:
1535 gcc_assert (c_dialect_objc ());
1536 c_parser_consume_token (parser);
1537 objc_finish_implementation ();
1538 break;
1539 default:
1540 goto decl_or_fndef;
1542 break;
1543 case CPP_SEMICOLON:
1544 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1545 "ISO C does not allow extra %<;%> outside of a function");
1546 c_parser_consume_token (parser);
1547 break;
1548 case CPP_PRAGMA:
1549 mark_valid_location_for_stdc_pragma (true);
1550 c_parser_pragma (parser, pragma_external, NULL);
1551 mark_valid_location_for_stdc_pragma (false);
1552 break;
1553 case CPP_PLUS:
1554 case CPP_MINUS:
1555 if (c_dialect_objc ())
1557 c_parser_objc_method_definition (parser);
1558 break;
1560 /* Else fall through, and yield a syntax error trying to parse
1561 as a declaration or function definition. */
1562 /* FALLTHRU */
1563 default:
1564 decl_or_fndef:
1565 /* A declaration or a function definition (or, in Objective-C,
1566 an @interface or @protocol with prefix attributes). We can
1567 only tell which after parsing the declaration specifiers, if
1568 any, and the first declarator. */
1569 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1570 NULL, vNULL);
1571 break;
1575 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1576 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1578 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1579 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1580 accepted; otherwise (old-style parameter declarations) only other
1581 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1582 assertion is accepted; otherwise (old-style parameter declarations)
1583 it is not. If NESTED is true, we are inside a function or parsing
1584 old-style parameter declarations; any functions encountered are
1585 nested functions and declaration specifiers are required; otherwise
1586 we are at top level and functions are normal functions and
1587 declaration specifiers may be optional. If EMPTY_OK is true, empty
1588 declarations are OK (subject to all other constraints); otherwise
1589 (old-style parameter declarations) they are diagnosed. If
1590 START_ATTR_OK is true, the declaration specifiers may start with
1591 attributes; otherwise they may not.
1592 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1593 declaration when parsing an Objective-C foreach statement.
1595 declaration:
1596 declaration-specifiers init-declarator-list[opt] ;
1597 static_assert-declaration
1599 function-definition:
1600 declaration-specifiers[opt] declarator declaration-list[opt]
1601 compound-statement
1603 declaration-list:
1604 declaration
1605 declaration-list declaration
1607 init-declarator-list:
1608 init-declarator
1609 init-declarator-list , init-declarator
1611 init-declarator:
1612 declarator simple-asm-expr[opt] attributes[opt]
1613 declarator simple-asm-expr[opt] attributes[opt] = initializer
1615 GNU extensions:
1617 nested-function-definition:
1618 declaration-specifiers declarator declaration-list[opt]
1619 compound-statement
1621 Objective-C:
1622 attributes objc-class-definition
1623 attributes objc-category-definition
1624 attributes objc-protocol-definition
1626 The simple-asm-expr and attributes are GNU extensions.
1628 This function does not handle __extension__; that is handled in its
1629 callers. ??? Following the old parser, __extension__ may start
1630 external declarations, declarations in functions and declarations
1631 at the start of "for" loops, but not old-style parameter
1632 declarations.
1634 C99 requires declaration specifiers in a function definition; the
1635 absence is diagnosed through the diagnosis of implicit int. In GNU
1636 C we also allow but diagnose declarations without declaration
1637 specifiers, but only at top level (elsewhere they conflict with
1638 other syntax).
1640 In Objective-C, declarations of the looping variable in a foreach
1641 statement are exceptionally terminated by 'in' (for example, 'for
1642 (NSObject *object in array) { ... }').
1644 OpenMP:
1646 declaration:
1647 threadprivate-directive */
1649 static void
1650 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1651 bool static_assert_ok, bool empty_ok,
1652 bool nested, bool start_attr_ok,
1653 tree *objc_foreach_object_declaration,
1654 vec<c_token> omp_declare_simd_clauses,
1655 struct oacc_routine_data *oacc_routine_data)
1657 struct c_declspecs *specs;
1658 tree prefix_attrs;
1659 tree all_prefix_attrs;
1660 bool diagnosed_no_specs = false;
1661 location_t here = c_parser_peek_token (parser)->location;
1663 if (static_assert_ok
1664 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1666 c_parser_static_assert_declaration (parser);
1667 return;
1669 specs = build_null_declspecs ();
1671 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1672 if (c_parser_peek_token (parser)->type == CPP_NAME
1673 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1674 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1675 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1676 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1678 tree name = c_parser_peek_token (parser)->value;
1680 /* Issue a warning about NAME being an unknown type name, perhaps
1681 with some kind of hint.
1682 If the user forgot a "struct" etc, suggest inserting
1683 it. Otherwise, attempt to look for misspellings. */
1684 gcc_rich_location richloc (here);
1685 if (tag_exists_p (RECORD_TYPE, name))
1687 /* This is not C++ with its implicit typedef. */
1688 richloc.add_fixit_insert ("struct ");
1689 error_at_rich_loc (&richloc,
1690 "unknown type name %qE;"
1691 " use %<struct%> keyword to refer to the type",
1692 name);
1694 else if (tag_exists_p (UNION_TYPE, name))
1696 richloc.add_fixit_insert ("union ");
1697 error_at_rich_loc (&richloc,
1698 "unknown type name %qE;"
1699 " use %<union%> keyword to refer to the type",
1700 name);
1702 else if (tag_exists_p (ENUMERAL_TYPE, name))
1704 richloc.add_fixit_insert ("enum ");
1705 error_at_rich_loc (&richloc,
1706 "unknown type name %qE;"
1707 " use %<enum%> keyword to refer to the type",
1708 name);
1710 else
1712 const char *hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME);
1713 if (hint)
1715 richloc.add_fixit_replace (hint);
1716 error_at_rich_loc (&richloc,
1717 "unknown type name %qE; did you mean %qs?",
1718 name, hint);
1720 else
1721 error_at (here, "unknown type name %qE", name);
1724 /* Parse declspecs normally to get a correct pointer type, but avoid
1725 a further "fails to be a type name" error. Refuse nested functions
1726 since it is not how the user likely wants us to recover. */
1727 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1728 c_parser_peek_token (parser)->keyword = RID_VOID;
1729 c_parser_peek_token (parser)->value = error_mark_node;
1730 fndef_ok = !nested;
1733 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1734 true, true, cla_nonabstract_decl);
1735 if (parser->error)
1737 c_parser_skip_to_end_of_block_or_statement (parser);
1738 return;
1740 if (nested && !specs->declspecs_seen_p)
1742 c_parser_error (parser, "expected declaration specifiers");
1743 c_parser_skip_to_end_of_block_or_statement (parser);
1744 return;
1746 finish_declspecs (specs);
1747 bool auto_type_p = specs->typespec_word == cts_auto_type;
1748 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1750 if (auto_type_p)
1751 error_at (here, "%<__auto_type%> in empty declaration");
1752 else if (empty_ok)
1753 shadow_tag (specs);
1754 else
1756 shadow_tag_warned (specs, 1);
1757 pedwarn (here, 0, "empty declaration");
1759 c_parser_consume_token (parser);
1760 if (oacc_routine_data)
1761 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1762 return;
1765 /* Provide better error recovery. Note that a type name here is usually
1766 better diagnosed as a redeclaration. */
1767 if (empty_ok
1768 && specs->typespec_kind == ctsk_tagdef
1769 && c_parser_next_token_starts_declspecs (parser)
1770 && !c_parser_next_token_is (parser, CPP_NAME))
1772 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1773 parser->error = false;
1774 shadow_tag_warned (specs, 1);
1775 return;
1777 else if (c_dialect_objc () && !auto_type_p)
1779 /* Prefix attributes are an error on method decls. */
1780 switch (c_parser_peek_token (parser)->type)
1782 case CPP_PLUS:
1783 case CPP_MINUS:
1784 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1785 return;
1786 if (specs->attrs)
1788 warning_at (c_parser_peek_token (parser)->location,
1789 OPT_Wattributes,
1790 "prefix attributes are ignored for methods");
1791 specs->attrs = NULL_TREE;
1793 if (fndef_ok)
1794 c_parser_objc_method_definition (parser);
1795 else
1796 c_parser_objc_methodproto (parser);
1797 return;
1798 break;
1799 default:
1800 break;
1802 /* This is where we parse 'attributes @interface ...',
1803 'attributes @implementation ...', 'attributes @protocol ...'
1804 (where attributes could be, for example, __attribute__
1805 ((deprecated)).
1807 switch (c_parser_peek_token (parser)->keyword)
1809 case RID_AT_INTERFACE:
1811 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1812 return;
1813 c_parser_objc_class_definition (parser, specs->attrs);
1814 return;
1816 break;
1817 case RID_AT_IMPLEMENTATION:
1819 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1820 return;
1821 if (specs->attrs)
1823 warning_at (c_parser_peek_token (parser)->location,
1824 OPT_Wattributes,
1825 "prefix attributes are ignored for implementations");
1826 specs->attrs = NULL_TREE;
1828 c_parser_objc_class_definition (parser, NULL_TREE);
1829 return;
1831 break;
1832 case RID_AT_PROTOCOL:
1834 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1835 return;
1836 c_parser_objc_protocol_definition (parser, specs->attrs);
1837 return;
1839 break;
1840 case RID_AT_ALIAS:
1841 case RID_AT_CLASS:
1842 case RID_AT_END:
1843 case RID_AT_PROPERTY:
1844 if (specs->attrs)
1846 c_parser_error (parser, "unexpected attribute");
1847 specs->attrs = NULL;
1849 break;
1850 default:
1851 break;
1855 pending_xref_error ();
1856 prefix_attrs = specs->attrs;
1857 all_prefix_attrs = prefix_attrs;
1858 specs->attrs = NULL_TREE;
1859 while (true)
1861 struct c_declarator *declarator;
1862 bool dummy = false;
1863 timevar_id_t tv;
1864 tree fnbody;
1865 /* Declaring either one or more declarators (in which case we
1866 should diagnose if there were no declaration specifiers) or a
1867 function definition (in which case the diagnostic for
1868 implicit int suffices). */
1869 declarator = c_parser_declarator (parser,
1870 specs->typespec_kind != ctsk_none,
1871 C_DTR_NORMAL, &dummy);
1872 if (declarator == NULL)
1874 if (omp_declare_simd_clauses.exists ()
1875 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1876 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1877 omp_declare_simd_clauses);
1878 if (oacc_routine_data)
1879 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1880 c_parser_skip_to_end_of_block_or_statement (parser);
1881 return;
1883 if (auto_type_p && declarator->kind != cdk_id)
1885 error_at (here,
1886 "%<__auto_type%> requires a plain identifier"
1887 " as declarator");
1888 c_parser_skip_to_end_of_block_or_statement (parser);
1889 return;
1891 if (c_parser_next_token_is (parser, CPP_EQ)
1892 || c_parser_next_token_is (parser, CPP_COMMA)
1893 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1894 || c_parser_next_token_is_keyword (parser, RID_ASM)
1895 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1896 || c_parser_next_token_is_keyword (parser, RID_IN))
1898 tree asm_name = NULL_TREE;
1899 tree postfix_attrs = NULL_TREE;
1900 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1902 diagnosed_no_specs = true;
1903 pedwarn (here, 0, "data definition has no type or storage class");
1905 /* Having seen a data definition, there cannot now be a
1906 function definition. */
1907 fndef_ok = false;
1908 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1909 asm_name = c_parser_simple_asm_expr (parser);
1910 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1912 postfix_attrs = c_parser_attributes (parser);
1913 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1915 /* This means there is an attribute specifier after
1916 the declarator in a function definition. Provide
1917 some more information for the user. */
1918 error_at (here, "attributes should be specified before the "
1919 "declarator in a function definition");
1920 c_parser_skip_to_end_of_block_or_statement (parser);
1921 return;
1924 if (c_parser_next_token_is (parser, CPP_EQ))
1926 tree d;
1927 struct c_expr init;
1928 location_t init_loc;
1929 c_parser_consume_token (parser);
1930 if (auto_type_p)
1932 start_init (NULL_TREE, asm_name, global_bindings_p ());
1933 init_loc = c_parser_peek_token (parser)->location;
1934 init = c_parser_expr_no_commas (parser, NULL);
1935 if (TREE_CODE (init.value) == COMPONENT_REF
1936 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1937 error_at (here,
1938 "%<__auto_type%> used with a bit-field"
1939 " initializer");
1940 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1941 tree init_type = TREE_TYPE (init.value);
1942 /* As with typeof, remove all qualifiers from atomic types. */
1943 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1944 init_type
1945 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1946 bool vm_type = variably_modified_type_p (init_type,
1947 NULL_TREE);
1948 if (vm_type)
1949 init.value = c_save_expr (init.value);
1950 finish_init ();
1951 specs->typespec_kind = ctsk_typeof;
1952 specs->locations[cdw_typedef] = init_loc;
1953 specs->typedef_p = true;
1954 specs->type = init_type;
1955 if (vm_type)
1957 bool maybe_const = true;
1958 tree type_expr = c_fully_fold (init.value, false,
1959 &maybe_const);
1960 specs->expr_const_operands &= maybe_const;
1961 if (specs->expr)
1962 specs->expr = build2 (COMPOUND_EXPR,
1963 TREE_TYPE (type_expr),
1964 specs->expr, type_expr);
1965 else
1966 specs->expr = type_expr;
1968 d = start_decl (declarator, specs, true,
1969 chainon (postfix_attrs, all_prefix_attrs));
1970 if (!d)
1971 d = error_mark_node;
1972 if (omp_declare_simd_clauses.exists ()
1973 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1974 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1975 omp_declare_simd_clauses);
1977 else
1979 /* The declaration of the variable is in effect while
1980 its initializer is parsed. */
1981 d = start_decl (declarator, specs, true,
1982 chainon (postfix_attrs, all_prefix_attrs));
1983 if (!d)
1984 d = error_mark_node;
1985 if (omp_declare_simd_clauses.exists ()
1986 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1987 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1988 omp_declare_simd_clauses);
1989 start_init (d, asm_name, global_bindings_p ());
1990 init_loc = c_parser_peek_token (parser)->location;
1991 init = c_parser_initializer (parser);
1992 finish_init ();
1994 if (oacc_routine_data)
1995 c_finish_oacc_routine (oacc_routine_data, d, false);
1996 if (d != error_mark_node)
1998 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1999 finish_decl (d, init_loc, init.value,
2000 init.original_type, asm_name);
2003 else
2005 if (auto_type_p)
2007 error_at (here,
2008 "%<__auto_type%> requires an initialized "
2009 "data declaration");
2010 c_parser_skip_to_end_of_block_or_statement (parser);
2011 return;
2013 tree d = start_decl (declarator, specs, false,
2014 chainon (postfix_attrs,
2015 all_prefix_attrs));
2016 if (omp_declare_simd_clauses.exists ()
2017 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2019 tree parms = NULL_TREE;
2020 if (d && TREE_CODE (d) == FUNCTION_DECL)
2022 struct c_declarator *ce = declarator;
2023 while (ce != NULL)
2024 if (ce->kind == cdk_function)
2026 parms = ce->u.arg_info->parms;
2027 break;
2029 else
2030 ce = ce->declarator;
2032 if (parms)
2033 temp_store_parm_decls (d, parms);
2034 c_finish_omp_declare_simd (parser, d, parms,
2035 omp_declare_simd_clauses);
2036 if (parms)
2037 temp_pop_parm_decls ();
2039 if (oacc_routine_data)
2040 c_finish_oacc_routine (oacc_routine_data, d, false);
2041 if (d)
2042 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2043 NULL_TREE, asm_name);
2045 if (c_parser_next_token_is_keyword (parser, RID_IN))
2047 if (d)
2048 *objc_foreach_object_declaration = d;
2049 else
2050 *objc_foreach_object_declaration = error_mark_node;
2053 if (c_parser_next_token_is (parser, CPP_COMMA))
2055 if (auto_type_p)
2057 error_at (here,
2058 "%<__auto_type%> may only be used with"
2059 " a single declarator");
2060 c_parser_skip_to_end_of_block_or_statement (parser);
2061 return;
2063 c_parser_consume_token (parser);
2064 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2065 all_prefix_attrs = chainon (c_parser_attributes (parser),
2066 prefix_attrs);
2067 else
2068 all_prefix_attrs = prefix_attrs;
2069 continue;
2071 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2073 c_parser_consume_token (parser);
2074 return;
2076 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2078 /* This can only happen in Objective-C: we found the
2079 'in' that terminates the declaration inside an
2080 Objective-C foreach statement. Do not consume the
2081 token, so that the caller can use it to determine
2082 that this indeed is a foreach context. */
2083 return;
2085 else
2087 c_parser_error (parser, "expected %<,%> or %<;%>");
2088 c_parser_skip_to_end_of_block_or_statement (parser);
2089 return;
2092 else if (auto_type_p)
2094 error_at (here,
2095 "%<__auto_type%> requires an initialized data declaration");
2096 c_parser_skip_to_end_of_block_or_statement (parser);
2097 return;
2099 else if (!fndef_ok)
2101 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2102 "%<asm%> or %<__attribute__%>");
2103 c_parser_skip_to_end_of_block_or_statement (parser);
2104 return;
2106 /* Function definition (nested or otherwise). */
2107 if (nested)
2109 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2110 c_push_function_context ();
2112 if (!start_function (specs, declarator, all_prefix_attrs))
2114 /* This can appear in many cases looking nothing like a
2115 function definition, so we don't give a more specific
2116 error suggesting there was one. */
2117 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2118 "or %<__attribute__%>");
2119 if (nested)
2120 c_pop_function_context ();
2121 break;
2124 if (DECL_DECLARED_INLINE_P (current_function_decl))
2125 tv = TV_PARSE_INLINE;
2126 else
2127 tv = TV_PARSE_FUNC;
2128 timevar_push (tv);
2130 /* Parse old-style parameter declarations. ??? Attributes are
2131 not allowed to start declaration specifiers here because of a
2132 syntax conflict between a function declaration with attribute
2133 suffix and a function definition with an attribute prefix on
2134 first old-style parameter declaration. Following the old
2135 parser, they are not accepted on subsequent old-style
2136 parameter declarations either. However, there is no
2137 ambiguity after the first declaration, nor indeed on the
2138 first as long as we don't allow postfix attributes after a
2139 declarator with a nonempty identifier list in a definition;
2140 and postfix attributes have never been accepted here in
2141 function definitions either. */
2142 while (c_parser_next_token_is_not (parser, CPP_EOF)
2143 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2144 c_parser_declaration_or_fndef (parser, false, false, false,
2145 true, false, NULL, vNULL);
2146 store_parm_decls ();
2147 if (omp_declare_simd_clauses.exists ()
2148 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2149 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2150 omp_declare_simd_clauses);
2151 if (oacc_routine_data)
2152 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2153 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2154 = c_parser_peek_token (parser)->location;
2155 fnbody = c_parser_compound_statement (parser);
2156 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2157 fnbody = expand_array_notation_exprs (fnbody);
2158 if (nested)
2160 tree decl = current_function_decl;
2161 /* Mark nested functions as needing static-chain initially.
2162 lower_nested_functions will recompute it but the
2163 DECL_STATIC_CHAIN flag is also used before that happens,
2164 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2165 DECL_STATIC_CHAIN (decl) = 1;
2166 add_stmt (fnbody);
2167 finish_function ();
2168 c_pop_function_context ();
2169 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2171 else
2173 add_stmt (fnbody);
2174 finish_function ();
2177 timevar_pop (tv);
2178 break;
2182 /* Parse an asm-definition (asm() outside a function body). This is a
2183 GNU extension.
2185 asm-definition:
2186 simple-asm-expr ;
2189 static void
2190 c_parser_asm_definition (c_parser *parser)
2192 tree asm_str = c_parser_simple_asm_expr (parser);
2193 if (asm_str)
2194 symtab->finalize_toplevel_asm (asm_str);
2195 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2198 /* Parse a static assertion (C11 6.7.10).
2200 static_assert-declaration:
2201 static_assert-declaration-no-semi ;
2204 static void
2205 c_parser_static_assert_declaration (c_parser *parser)
2207 c_parser_static_assert_declaration_no_semi (parser);
2208 if (parser->error
2209 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2210 c_parser_skip_to_end_of_block_or_statement (parser);
2213 /* Parse a static assertion (C11 6.7.10), without the trailing
2214 semicolon.
2216 static_assert-declaration-no-semi:
2217 _Static_assert ( constant-expression , string-literal )
2220 static void
2221 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2223 location_t assert_loc, value_loc;
2224 tree value;
2225 tree string;
2227 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2228 assert_loc = c_parser_peek_token (parser)->location;
2229 if (flag_isoc99)
2230 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2231 "ISO C99 does not support %<_Static_assert%>");
2232 else
2233 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2234 "ISO C90 does not support %<_Static_assert%>");
2235 c_parser_consume_token (parser);
2236 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2237 return;
2238 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2239 value = c_parser_expr_no_commas (parser, NULL).value;
2240 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2241 parser->lex_untranslated_string = true;
2242 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2244 parser->lex_untranslated_string = false;
2245 return;
2247 switch (c_parser_peek_token (parser)->type)
2249 case CPP_STRING:
2250 case CPP_STRING16:
2251 case CPP_STRING32:
2252 case CPP_WSTRING:
2253 case CPP_UTF8STRING:
2254 string = c_parser_peek_token (parser)->value;
2255 c_parser_consume_token (parser);
2256 parser->lex_untranslated_string = false;
2257 break;
2258 default:
2259 c_parser_error (parser, "expected string literal");
2260 parser->lex_untranslated_string = false;
2261 return;
2263 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2265 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2267 error_at (value_loc, "expression in static assertion is not an integer");
2268 return;
2270 if (TREE_CODE (value) != INTEGER_CST)
2272 value = c_fully_fold (value, false, NULL);
2273 /* Strip no-op conversions. */
2274 STRIP_TYPE_NOPS (value);
2275 if (TREE_CODE (value) == INTEGER_CST)
2276 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2277 "is not an integer constant expression");
2279 if (TREE_CODE (value) != INTEGER_CST)
2281 error_at (value_loc, "expression in static assertion is not constant");
2282 return;
2284 constant_expression_warning (value);
2285 if (integer_zerop (value))
2286 error_at (assert_loc, "static assertion failed: %E", string);
2289 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2290 6.7), adding them to SPECS (which may already include some).
2291 Storage class specifiers are accepted iff SCSPEC_OK; type
2292 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2293 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2294 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2296 declaration-specifiers:
2297 storage-class-specifier declaration-specifiers[opt]
2298 type-specifier declaration-specifiers[opt]
2299 type-qualifier declaration-specifiers[opt]
2300 function-specifier declaration-specifiers[opt]
2301 alignment-specifier declaration-specifiers[opt]
2303 Function specifiers (inline) are from C99, and are currently
2304 handled as storage class specifiers, as is __thread. Alignment
2305 specifiers are from C11.
2307 C90 6.5.1, C99 6.7.1:
2308 storage-class-specifier:
2309 typedef
2310 extern
2311 static
2312 auto
2313 register
2314 _Thread_local
2316 (_Thread_local is new in C11.)
2318 C99 6.7.4:
2319 function-specifier:
2320 inline
2321 _Noreturn
2323 (_Noreturn is new in C11.)
2325 C90 6.5.2, C99 6.7.2:
2326 type-specifier:
2327 void
2328 char
2329 short
2331 long
2332 float
2333 double
2334 signed
2335 unsigned
2336 _Bool
2337 _Complex
2338 [_Imaginary removed in C99 TC2]
2339 struct-or-union-specifier
2340 enum-specifier
2341 typedef-name
2342 atomic-type-specifier
2344 (_Bool and _Complex are new in C99.)
2345 (atomic-type-specifier is new in C11.)
2347 C90 6.5.3, C99 6.7.3:
2349 type-qualifier:
2350 const
2351 restrict
2352 volatile
2353 address-space-qualifier
2354 _Atomic
2356 (restrict is new in C99.)
2357 (_Atomic is new in C11.)
2359 GNU extensions:
2361 declaration-specifiers:
2362 attributes declaration-specifiers[opt]
2364 type-qualifier:
2365 address-space
2367 address-space:
2368 identifier recognized by the target
2370 storage-class-specifier:
2371 __thread
2373 type-specifier:
2374 typeof-specifier
2375 __auto_type
2376 __intN
2377 _Decimal32
2378 _Decimal64
2379 _Decimal128
2380 _Fract
2381 _Accum
2382 _Sat
2384 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2385 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2387 atomic-type-specifier
2388 _Atomic ( type-name )
2390 Objective-C:
2392 type-specifier:
2393 class-name objc-protocol-refs[opt]
2394 typedef-name objc-protocol-refs
2395 objc-protocol-refs
2398 static void
2399 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2400 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2401 bool alignspec_ok, bool auto_type_ok,
2402 enum c_lookahead_kind la)
2404 bool attrs_ok = start_attr_ok;
2405 bool seen_type = specs->typespec_kind != ctsk_none;
2407 if (!typespec_ok)
2408 gcc_assert (la == cla_prefer_id);
2410 while (c_parser_next_token_is (parser, CPP_NAME)
2411 || c_parser_next_token_is (parser, CPP_KEYWORD)
2412 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2414 struct c_typespec t;
2415 tree attrs;
2416 tree align;
2417 location_t loc = c_parser_peek_token (parser)->location;
2419 /* If we cannot accept a type, exit if the next token must start
2420 one. Also, if we already have seen a tagged definition,
2421 a typename would be an error anyway and likely the user
2422 has simply forgotten a semicolon, so we exit. */
2423 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2424 && c_parser_next_tokens_start_typename (parser, la)
2425 && !c_parser_next_token_is_qualifier (parser))
2426 break;
2428 if (c_parser_next_token_is (parser, CPP_NAME))
2430 c_token *name_token = c_parser_peek_token (parser);
2431 tree value = name_token->value;
2432 c_id_kind kind = name_token->id_kind;
2434 if (kind == C_ID_ADDRSPACE)
2436 addr_space_t as
2437 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2438 declspecs_add_addrspace (name_token->location, specs, as);
2439 c_parser_consume_token (parser);
2440 attrs_ok = true;
2441 continue;
2444 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2446 /* If we cannot accept a type, and the next token must start one,
2447 exit. Do the same if we already have seen a tagged definition,
2448 since it would be an error anyway and likely the user has simply
2449 forgotten a semicolon. */
2450 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2451 break;
2453 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2454 a C_ID_CLASSNAME. */
2455 c_parser_consume_token (parser);
2456 seen_type = true;
2457 attrs_ok = true;
2458 if (kind == C_ID_ID)
2460 error_at (loc, "unknown type name %qE", value);
2461 t.kind = ctsk_typedef;
2462 t.spec = error_mark_node;
2464 else if (kind == C_ID_TYPENAME
2465 && (!c_dialect_objc ()
2466 || c_parser_next_token_is_not (parser, CPP_LESS)))
2468 t.kind = ctsk_typedef;
2469 /* For a typedef name, record the meaning, not the name.
2470 In case of 'foo foo, bar;'. */
2471 t.spec = lookup_name (value);
2473 else
2475 tree proto = NULL_TREE;
2476 gcc_assert (c_dialect_objc ());
2477 t.kind = ctsk_objc;
2478 if (c_parser_next_token_is (parser, CPP_LESS))
2479 proto = c_parser_objc_protocol_refs (parser);
2480 t.spec = objc_get_protocol_qualified_type (value, proto);
2482 t.expr = NULL_TREE;
2483 t.expr_const_operands = true;
2484 declspecs_add_type (name_token->location, specs, t);
2485 continue;
2487 if (c_parser_next_token_is (parser, CPP_LESS))
2489 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2490 nisse@lysator.liu.se. */
2491 tree proto;
2492 gcc_assert (c_dialect_objc ());
2493 if (!typespec_ok || seen_type)
2494 break;
2495 proto = c_parser_objc_protocol_refs (parser);
2496 t.kind = ctsk_objc;
2497 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2498 t.expr = NULL_TREE;
2499 t.expr_const_operands = true;
2500 declspecs_add_type (loc, specs, t);
2501 continue;
2503 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2504 switch (c_parser_peek_token (parser)->keyword)
2506 case RID_STATIC:
2507 case RID_EXTERN:
2508 case RID_REGISTER:
2509 case RID_TYPEDEF:
2510 case RID_INLINE:
2511 case RID_NORETURN:
2512 case RID_AUTO:
2513 case RID_THREAD:
2514 if (!scspec_ok)
2515 goto out;
2516 attrs_ok = true;
2517 /* TODO: Distinguish between function specifiers (inline, noreturn)
2518 and storage class specifiers, either here or in
2519 declspecs_add_scspec. */
2520 declspecs_add_scspec (loc, specs,
2521 c_parser_peek_token (parser)->value);
2522 c_parser_consume_token (parser);
2523 break;
2524 case RID_AUTO_TYPE:
2525 if (!auto_type_ok)
2526 goto out;
2527 /* Fall through. */
2528 case RID_UNSIGNED:
2529 case RID_LONG:
2530 case RID_SHORT:
2531 case RID_SIGNED:
2532 case RID_COMPLEX:
2533 case RID_INT:
2534 case RID_CHAR:
2535 case RID_FLOAT:
2536 case RID_DOUBLE:
2537 case RID_VOID:
2538 case RID_DFLOAT32:
2539 case RID_DFLOAT64:
2540 case RID_DFLOAT128:
2541 CASE_RID_FLOATN_NX:
2542 case RID_BOOL:
2543 case RID_FRACT:
2544 case RID_ACCUM:
2545 case RID_SAT:
2546 case RID_INT_N_0:
2547 case RID_INT_N_1:
2548 case RID_INT_N_2:
2549 case RID_INT_N_3:
2550 if (!typespec_ok)
2551 goto out;
2552 attrs_ok = true;
2553 seen_type = true;
2554 if (c_dialect_objc ())
2555 parser->objc_need_raw_identifier = true;
2556 t.kind = ctsk_resword;
2557 t.spec = c_parser_peek_token (parser)->value;
2558 t.expr = NULL_TREE;
2559 t.expr_const_operands = true;
2560 declspecs_add_type (loc, specs, t);
2561 c_parser_consume_token (parser);
2562 break;
2563 case RID_ENUM:
2564 if (!typespec_ok)
2565 goto out;
2566 attrs_ok = true;
2567 seen_type = true;
2568 t = c_parser_enum_specifier (parser);
2569 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2570 declspecs_add_type (loc, specs, t);
2571 break;
2572 case RID_STRUCT:
2573 case RID_UNION:
2574 if (!typespec_ok)
2575 goto out;
2576 attrs_ok = true;
2577 seen_type = true;
2578 t = c_parser_struct_or_union_specifier (parser);
2579 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2580 declspecs_add_type (loc, specs, t);
2581 break;
2582 case RID_TYPEOF:
2583 /* ??? The old parser rejected typeof after other type
2584 specifiers, but is a syntax error the best way of
2585 handling this? */
2586 if (!typespec_ok || seen_type)
2587 goto out;
2588 attrs_ok = true;
2589 seen_type = true;
2590 t = c_parser_typeof_specifier (parser);
2591 declspecs_add_type (loc, specs, t);
2592 break;
2593 case RID_ATOMIC:
2594 /* C parser handling of Objective-C constructs needs
2595 checking for correct lvalue-to-rvalue conversions, and
2596 the code in build_modify_expr handling various
2597 Objective-C cases, and that in build_unary_op handling
2598 Objective-C cases for increment / decrement, also needs
2599 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2600 and objc_types_are_equivalent may also need updates. */
2601 if (c_dialect_objc ())
2602 sorry ("%<_Atomic%> in Objective-C");
2603 /* C parser handling of OpenMP constructs needs checking for
2604 correct lvalue-to-rvalue conversions. */
2605 if (flag_openmp)
2606 sorry ("%<_Atomic%> with OpenMP");
2607 if (flag_isoc99)
2608 pedwarn_c99 (loc, OPT_Wpedantic,
2609 "ISO C99 does not support the %<_Atomic%> qualifier");
2610 else
2611 pedwarn_c99 (loc, OPT_Wpedantic,
2612 "ISO C90 does not support the %<_Atomic%> qualifier");
2613 attrs_ok = true;
2614 tree value;
2615 value = c_parser_peek_token (parser)->value;
2616 c_parser_consume_token (parser);
2617 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2619 /* _Atomic ( type-name ). */
2620 seen_type = true;
2621 c_parser_consume_token (parser);
2622 struct c_type_name *type = c_parser_type_name (parser);
2623 t.kind = ctsk_typeof;
2624 t.spec = error_mark_node;
2625 t.expr = NULL_TREE;
2626 t.expr_const_operands = true;
2627 if (type != NULL)
2628 t.spec = groktypename (type, &t.expr,
2629 &t.expr_const_operands);
2630 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2631 "expected %<)%>");
2632 if (t.spec != error_mark_node)
2634 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2635 error_at (loc, "%<_Atomic%>-qualified array type");
2636 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2637 error_at (loc, "%<_Atomic%>-qualified function type");
2638 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2639 error_at (loc, "%<_Atomic%> applied to a qualified type");
2640 else
2641 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2643 declspecs_add_type (loc, specs, t);
2645 else
2646 declspecs_add_qual (loc, specs, value);
2647 break;
2648 case RID_CONST:
2649 case RID_VOLATILE:
2650 case RID_RESTRICT:
2651 attrs_ok = true;
2652 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2653 c_parser_consume_token (parser);
2654 break;
2655 case RID_ATTRIBUTE:
2656 if (!attrs_ok)
2657 goto out;
2658 attrs = c_parser_attributes (parser);
2659 declspecs_add_attrs (loc, specs, attrs);
2660 break;
2661 case RID_ALIGNAS:
2662 if (!alignspec_ok)
2663 goto out;
2664 align = c_parser_alignas_specifier (parser);
2665 declspecs_add_alignas (loc, specs, align);
2666 break;
2667 default:
2668 goto out;
2671 out: ;
2674 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2676 enum-specifier:
2677 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2678 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2679 enum attributes[opt] identifier
2681 The form with trailing comma is new in C99. The forms with
2682 attributes are GNU extensions. In GNU C, we accept any expression
2683 without commas in the syntax (assignment expressions, not just
2684 conditional expressions); assignment expressions will be diagnosed
2685 as non-constant.
2687 enumerator-list:
2688 enumerator
2689 enumerator-list , enumerator
2691 enumerator:
2692 enumeration-constant
2693 enumeration-constant = constant-expression
2695 GNU Extensions:
2697 enumerator:
2698 enumeration-constant attributes[opt]
2699 enumeration-constant attributes[opt] = constant-expression
2703 static struct c_typespec
2704 c_parser_enum_specifier (c_parser *parser)
2706 struct c_typespec ret;
2707 tree attrs;
2708 tree ident = NULL_TREE;
2709 location_t enum_loc;
2710 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2711 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2712 enum_loc = c_parser_peek_token (parser)->location;
2713 c_parser_consume_token (parser);
2714 attrs = c_parser_attributes (parser);
2715 enum_loc = c_parser_peek_token (parser)->location;
2716 /* Set the location in case we create a decl now. */
2717 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2718 if (c_parser_next_token_is (parser, CPP_NAME))
2720 ident = c_parser_peek_token (parser)->value;
2721 ident_loc = c_parser_peek_token (parser)->location;
2722 enum_loc = ident_loc;
2723 c_parser_consume_token (parser);
2725 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2727 /* Parse an enum definition. */
2728 struct c_enum_contents the_enum;
2729 tree type;
2730 tree postfix_attrs;
2731 /* We chain the enumerators in reverse order, then put them in
2732 forward order at the end. */
2733 tree values;
2734 timevar_push (TV_PARSE_ENUM);
2735 type = start_enum (enum_loc, &the_enum, ident);
2736 values = NULL_TREE;
2737 c_parser_consume_token (parser);
2738 while (true)
2740 tree enum_id;
2741 tree enum_value;
2742 tree enum_decl;
2743 bool seen_comma;
2744 c_token *token;
2745 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2746 location_t decl_loc, value_loc;
2747 if (c_parser_next_token_is_not (parser, CPP_NAME))
2749 /* Give a nicer error for "enum {}". */
2750 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2751 && !parser->error)
2753 error_at (c_parser_peek_token (parser)->location,
2754 "empty enum is invalid");
2755 parser->error = true;
2757 else
2758 c_parser_error (parser, "expected identifier");
2759 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2760 values = error_mark_node;
2761 break;
2763 token = c_parser_peek_token (parser);
2764 enum_id = token->value;
2765 /* Set the location in case we create a decl now. */
2766 c_parser_set_source_position_from_token (token);
2767 decl_loc = value_loc = token->location;
2768 c_parser_consume_token (parser);
2769 /* Parse any specified attributes. */
2770 tree enum_attrs = c_parser_attributes (parser);
2771 if (c_parser_next_token_is (parser, CPP_EQ))
2773 c_parser_consume_token (parser);
2774 value_loc = c_parser_peek_token (parser)->location;
2775 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2777 else
2778 enum_value = NULL_TREE;
2779 enum_decl = build_enumerator (decl_loc, value_loc,
2780 &the_enum, enum_id, enum_value);
2781 if (enum_attrs)
2782 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2783 TREE_CHAIN (enum_decl) = values;
2784 values = enum_decl;
2785 seen_comma = false;
2786 if (c_parser_next_token_is (parser, CPP_COMMA))
2788 comma_loc = c_parser_peek_token (parser)->location;
2789 seen_comma = true;
2790 c_parser_consume_token (parser);
2792 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2794 if (seen_comma)
2795 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2796 "comma at end of enumerator list");
2797 c_parser_consume_token (parser);
2798 break;
2800 if (!seen_comma)
2802 c_parser_error (parser, "expected %<,%> or %<}%>");
2803 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2804 values = error_mark_node;
2805 break;
2808 postfix_attrs = c_parser_attributes (parser);
2809 ret.spec = finish_enum (type, nreverse (values),
2810 chainon (attrs, postfix_attrs));
2811 ret.kind = ctsk_tagdef;
2812 ret.expr = NULL_TREE;
2813 ret.expr_const_operands = true;
2814 timevar_pop (TV_PARSE_ENUM);
2815 return ret;
2817 else if (!ident)
2819 c_parser_error (parser, "expected %<{%>");
2820 ret.spec = error_mark_node;
2821 ret.kind = ctsk_tagref;
2822 ret.expr = NULL_TREE;
2823 ret.expr_const_operands = true;
2824 return ret;
2826 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2827 /* In ISO C, enumerated types can be referred to only if already
2828 defined. */
2829 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2831 gcc_assert (ident);
2832 pedwarn (enum_loc, OPT_Wpedantic,
2833 "ISO C forbids forward references to %<enum%> types");
2835 return ret;
2838 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2840 struct-or-union-specifier:
2841 struct-or-union attributes[opt] identifier[opt]
2842 { struct-contents } attributes[opt]
2843 struct-or-union attributes[opt] identifier
2845 struct-contents:
2846 struct-declaration-list
2848 struct-declaration-list:
2849 struct-declaration ;
2850 struct-declaration-list struct-declaration ;
2852 GNU extensions:
2854 struct-contents:
2855 empty
2856 struct-declaration
2857 struct-declaration-list struct-declaration
2859 struct-declaration-list:
2860 struct-declaration-list ;
2863 (Note that in the syntax here, unlike that in ISO C, the semicolons
2864 are included here rather than in struct-declaration, in order to
2865 describe the syntax with extra semicolons and missing semicolon at
2866 end.)
2868 Objective-C:
2870 struct-declaration-list:
2871 @defs ( class-name )
2873 (Note this does not include a trailing semicolon, but can be
2874 followed by further declarations, and gets a pedwarn-if-pedantic
2875 when followed by a semicolon.) */
2877 static struct c_typespec
2878 c_parser_struct_or_union_specifier (c_parser *parser)
2880 struct c_typespec ret;
2881 tree attrs;
2882 tree ident = NULL_TREE;
2883 location_t struct_loc;
2884 location_t ident_loc = UNKNOWN_LOCATION;
2885 enum tree_code code;
2886 switch (c_parser_peek_token (parser)->keyword)
2888 case RID_STRUCT:
2889 code = RECORD_TYPE;
2890 break;
2891 case RID_UNION:
2892 code = UNION_TYPE;
2893 break;
2894 default:
2895 gcc_unreachable ();
2897 struct_loc = c_parser_peek_token (parser)->location;
2898 c_parser_consume_token (parser);
2899 attrs = c_parser_attributes (parser);
2901 /* Set the location in case we create a decl now. */
2902 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2904 if (c_parser_next_token_is (parser, CPP_NAME))
2906 ident = c_parser_peek_token (parser)->value;
2907 ident_loc = c_parser_peek_token (parser)->location;
2908 struct_loc = ident_loc;
2909 c_parser_consume_token (parser);
2911 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2913 /* Parse a struct or union definition. Start the scope of the
2914 tag before parsing components. */
2915 struct c_struct_parse_info *struct_info;
2916 tree type = start_struct (struct_loc, code, ident, &struct_info);
2917 tree postfix_attrs;
2918 /* We chain the components in reverse order, then put them in
2919 forward order at the end. Each struct-declaration may
2920 declare multiple components (comma-separated), so we must use
2921 chainon to join them, although when parsing each
2922 struct-declaration we can use TREE_CHAIN directly.
2924 The theory behind all this is that there will be more
2925 semicolon separated fields than comma separated fields, and
2926 so we'll be minimizing the number of node traversals required
2927 by chainon. */
2928 tree contents;
2929 timevar_push (TV_PARSE_STRUCT);
2930 contents = NULL_TREE;
2931 c_parser_consume_token (parser);
2932 /* Handle the Objective-C @defs construct,
2933 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2934 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2936 tree name;
2937 gcc_assert (c_dialect_objc ());
2938 c_parser_consume_token (parser);
2939 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2940 goto end_at_defs;
2941 if (c_parser_next_token_is (parser, CPP_NAME)
2942 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2944 name = c_parser_peek_token (parser)->value;
2945 c_parser_consume_token (parser);
2947 else
2949 c_parser_error (parser, "expected class name");
2950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2951 goto end_at_defs;
2953 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2954 "expected %<)%>");
2955 contents = nreverse (objc_get_class_ivars (name));
2957 end_at_defs:
2958 /* Parse the struct-declarations and semicolons. Problems with
2959 semicolons are diagnosed here; empty structures are diagnosed
2960 elsewhere. */
2961 while (true)
2963 tree decls;
2964 /* Parse any stray semicolon. */
2965 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2967 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2968 "extra semicolon in struct or union specified");
2969 c_parser_consume_token (parser);
2970 continue;
2972 /* Stop if at the end of the struct or union contents. */
2973 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2975 c_parser_consume_token (parser);
2976 break;
2978 /* Accept #pragmas at struct scope. */
2979 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2981 c_parser_pragma (parser, pragma_struct, NULL);
2982 continue;
2984 /* Parse some comma-separated declarations, but not the
2985 trailing semicolon if any. */
2986 decls = c_parser_struct_declaration (parser);
2987 contents = chainon (decls, contents);
2988 /* If no semicolon follows, either we have a parse error or
2989 are at the end of the struct or union and should
2990 pedwarn. */
2991 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2992 c_parser_consume_token (parser);
2993 else
2995 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2996 pedwarn (c_parser_peek_token (parser)->location, 0,
2997 "no semicolon at end of struct or union");
2998 else if (parser->error
2999 || !c_parser_next_token_starts_declspecs (parser))
3001 c_parser_error (parser, "expected %<;%>");
3002 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3003 break;
3006 /* If we come here, we have already emitted an error
3007 for an expected `;', identifier or `(', and we also
3008 recovered already. Go on with the next field. */
3011 postfix_attrs = c_parser_attributes (parser);
3012 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3013 chainon (attrs, postfix_attrs), struct_info);
3014 ret.kind = ctsk_tagdef;
3015 ret.expr = NULL_TREE;
3016 ret.expr_const_operands = true;
3017 timevar_pop (TV_PARSE_STRUCT);
3018 return ret;
3020 else if (!ident)
3022 c_parser_error (parser, "expected %<{%>");
3023 ret.spec = error_mark_node;
3024 ret.kind = ctsk_tagref;
3025 ret.expr = NULL_TREE;
3026 ret.expr_const_operands = true;
3027 return ret;
3029 ret = parser_xref_tag (ident_loc, code, ident);
3030 return ret;
3033 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
3034 the trailing semicolon.
3036 struct-declaration:
3037 specifier-qualifier-list struct-declarator-list
3038 static_assert-declaration-no-semi
3040 specifier-qualifier-list:
3041 type-specifier specifier-qualifier-list[opt]
3042 type-qualifier specifier-qualifier-list[opt]
3043 attributes specifier-qualifier-list[opt]
3045 struct-declarator-list:
3046 struct-declarator
3047 struct-declarator-list , attributes[opt] struct-declarator
3049 struct-declarator:
3050 declarator attributes[opt]
3051 declarator[opt] : constant-expression attributes[opt]
3053 GNU extensions:
3055 struct-declaration:
3056 __extension__ struct-declaration
3057 specifier-qualifier-list
3059 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3060 of attributes where shown is a GNU extension. In GNU C, we accept
3061 any expression without commas in the syntax (assignment
3062 expressions, not just conditional expressions); assignment
3063 expressions will be diagnosed as non-constant. */
3065 static tree
3066 c_parser_struct_declaration (c_parser *parser)
3068 struct c_declspecs *specs;
3069 tree prefix_attrs;
3070 tree all_prefix_attrs;
3071 tree decls;
3072 location_t decl_loc;
3073 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3075 int ext;
3076 tree decl;
3077 ext = disable_extension_diagnostics ();
3078 c_parser_consume_token (parser);
3079 decl = c_parser_struct_declaration (parser);
3080 restore_extension_diagnostics (ext);
3081 return decl;
3083 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3085 c_parser_static_assert_declaration_no_semi (parser);
3086 return NULL_TREE;
3088 specs = build_null_declspecs ();
3089 decl_loc = c_parser_peek_token (parser)->location;
3090 /* Strictly by the standard, we shouldn't allow _Alignas here,
3091 but it appears to have been intended to allow it there, so
3092 we're keeping it as it is until WG14 reaches a conclusion
3093 of N1731.
3094 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3095 c_parser_declspecs (parser, specs, false, true, true,
3096 true, false, cla_nonabstract_decl);
3097 if (parser->error)
3098 return NULL_TREE;
3099 if (!specs->declspecs_seen_p)
3101 c_parser_error (parser, "expected specifier-qualifier-list");
3102 return NULL_TREE;
3104 finish_declspecs (specs);
3105 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3106 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3108 tree ret;
3109 if (specs->typespec_kind == ctsk_none)
3111 pedwarn (decl_loc, OPT_Wpedantic,
3112 "ISO C forbids member declarations with no members");
3113 shadow_tag_warned (specs, pedantic);
3114 ret = NULL_TREE;
3116 else
3118 /* Support for unnamed structs or unions as members of
3119 structs or unions (which is [a] useful and [b] supports
3120 MS P-SDK). */
3121 tree attrs = NULL;
3123 ret = grokfield (c_parser_peek_token (parser)->location,
3124 build_id_declarator (NULL_TREE), specs,
3125 NULL_TREE, &attrs);
3126 if (ret)
3127 decl_attributes (&ret, attrs, 0);
3129 return ret;
3132 /* Provide better error recovery. Note that a type name here is valid,
3133 and will be treated as a field name. */
3134 if (specs->typespec_kind == ctsk_tagdef
3135 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3136 && c_parser_next_token_starts_declspecs (parser)
3137 && !c_parser_next_token_is (parser, CPP_NAME))
3139 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3140 parser->error = false;
3141 return NULL_TREE;
3144 pending_xref_error ();
3145 prefix_attrs = specs->attrs;
3146 all_prefix_attrs = prefix_attrs;
3147 specs->attrs = NULL_TREE;
3148 decls = NULL_TREE;
3149 while (true)
3151 /* Declaring one or more declarators or un-named bit-fields. */
3152 struct c_declarator *declarator;
3153 bool dummy = false;
3154 if (c_parser_next_token_is (parser, CPP_COLON))
3155 declarator = build_id_declarator (NULL_TREE);
3156 else
3157 declarator = c_parser_declarator (parser,
3158 specs->typespec_kind != ctsk_none,
3159 C_DTR_NORMAL, &dummy);
3160 if (declarator == NULL)
3162 c_parser_skip_to_end_of_block_or_statement (parser);
3163 break;
3165 if (c_parser_next_token_is (parser, CPP_COLON)
3166 || c_parser_next_token_is (parser, CPP_COMMA)
3167 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3168 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3169 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3171 tree postfix_attrs = NULL_TREE;
3172 tree width = NULL_TREE;
3173 tree d;
3174 if (c_parser_next_token_is (parser, CPP_COLON))
3176 c_parser_consume_token (parser);
3177 width = c_parser_expr_no_commas (parser, NULL).value;
3179 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3180 postfix_attrs = c_parser_attributes (parser);
3181 d = grokfield (c_parser_peek_token (parser)->location,
3182 declarator, specs, width, &all_prefix_attrs);
3183 decl_attributes (&d, chainon (postfix_attrs,
3184 all_prefix_attrs), 0);
3185 DECL_CHAIN (d) = decls;
3186 decls = d;
3187 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3188 all_prefix_attrs = chainon (c_parser_attributes (parser),
3189 prefix_attrs);
3190 else
3191 all_prefix_attrs = prefix_attrs;
3192 if (c_parser_next_token_is (parser, CPP_COMMA))
3193 c_parser_consume_token (parser);
3194 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3195 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3197 /* Semicolon consumed in caller. */
3198 break;
3200 else
3202 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3203 break;
3206 else
3208 c_parser_error (parser,
3209 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3210 "%<__attribute__%>");
3211 break;
3214 return decls;
3217 /* Parse a typeof specifier (a GNU extension).
3219 typeof-specifier:
3220 typeof ( expression )
3221 typeof ( type-name )
3224 static struct c_typespec
3225 c_parser_typeof_specifier (c_parser *parser)
3227 struct c_typespec ret;
3228 ret.kind = ctsk_typeof;
3229 ret.spec = error_mark_node;
3230 ret.expr = NULL_TREE;
3231 ret.expr_const_operands = true;
3232 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3233 c_parser_consume_token (parser);
3234 c_inhibit_evaluation_warnings++;
3235 in_typeof++;
3236 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3238 c_inhibit_evaluation_warnings--;
3239 in_typeof--;
3240 return ret;
3242 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3244 struct c_type_name *type = c_parser_type_name (parser);
3245 c_inhibit_evaluation_warnings--;
3246 in_typeof--;
3247 if (type != NULL)
3249 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3250 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3253 else
3255 bool was_vm;
3256 location_t here = c_parser_peek_token (parser)->location;
3257 struct c_expr expr = c_parser_expression (parser);
3258 c_inhibit_evaluation_warnings--;
3259 in_typeof--;
3260 if (TREE_CODE (expr.value) == COMPONENT_REF
3261 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3262 error_at (here, "%<typeof%> applied to a bit-field");
3263 mark_exp_read (expr.value);
3264 ret.spec = TREE_TYPE (expr.value);
3265 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3266 /* This is returned with the type so that when the type is
3267 evaluated, this can be evaluated. */
3268 if (was_vm)
3269 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3270 pop_maybe_used (was_vm);
3271 /* For use in macros such as those in <stdatomic.h>, remove all
3272 qualifiers from atomic types. (const can be an issue for more macros
3273 using typeof than just the <stdatomic.h> ones.) */
3274 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3275 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3277 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3278 return ret;
3281 /* Parse an alignment-specifier.
3283 C11 6.7.5:
3285 alignment-specifier:
3286 _Alignas ( type-name )
3287 _Alignas ( constant-expression )
3290 static tree
3291 c_parser_alignas_specifier (c_parser * parser)
3293 tree ret = error_mark_node;
3294 location_t loc = c_parser_peek_token (parser)->location;
3295 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3296 c_parser_consume_token (parser);
3297 if (flag_isoc99)
3298 pedwarn_c99 (loc, OPT_Wpedantic,
3299 "ISO C99 does not support %<_Alignas%>");
3300 else
3301 pedwarn_c99 (loc, OPT_Wpedantic,
3302 "ISO C90 does not support %<_Alignas%>");
3303 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3304 return ret;
3305 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3307 struct c_type_name *type = c_parser_type_name (parser);
3308 if (type != NULL)
3309 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3310 false, true, 1);
3312 else
3313 ret = c_parser_expr_no_commas (parser, NULL).value;
3314 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3315 return ret;
3318 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3319 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3320 be redeclared; otherwise it may not. KIND indicates which kind of
3321 declarator is wanted. Returns a valid declarator except in the
3322 case of a syntax error in which case NULL is returned. *SEEN_ID is
3323 set to true if an identifier being declared is seen; this is used
3324 to diagnose bad forms of abstract array declarators and to
3325 determine whether an identifier list is syntactically permitted.
3327 declarator:
3328 pointer[opt] direct-declarator
3330 direct-declarator:
3331 identifier
3332 ( attributes[opt] declarator )
3333 direct-declarator array-declarator
3334 direct-declarator ( parameter-type-list )
3335 direct-declarator ( identifier-list[opt] )
3337 pointer:
3338 * type-qualifier-list[opt]
3339 * type-qualifier-list[opt] pointer
3341 type-qualifier-list:
3342 type-qualifier
3343 attributes
3344 type-qualifier-list type-qualifier
3345 type-qualifier-list attributes
3347 array-declarator:
3348 [ type-qualifier-list[opt] assignment-expression[opt] ]
3349 [ static type-qualifier-list[opt] assignment-expression ]
3350 [ type-qualifier-list static assignment-expression ]
3351 [ type-qualifier-list[opt] * ]
3353 parameter-type-list:
3354 parameter-list
3355 parameter-list , ...
3357 parameter-list:
3358 parameter-declaration
3359 parameter-list , parameter-declaration
3361 parameter-declaration:
3362 declaration-specifiers declarator attributes[opt]
3363 declaration-specifiers abstract-declarator[opt] attributes[opt]
3365 identifier-list:
3366 identifier
3367 identifier-list , identifier
3369 abstract-declarator:
3370 pointer
3371 pointer[opt] direct-abstract-declarator
3373 direct-abstract-declarator:
3374 ( attributes[opt] abstract-declarator )
3375 direct-abstract-declarator[opt] array-declarator
3376 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3378 GNU extensions:
3380 direct-declarator:
3381 direct-declarator ( parameter-forward-declarations
3382 parameter-type-list[opt] )
3384 direct-abstract-declarator:
3385 direct-abstract-declarator[opt] ( parameter-forward-declarations
3386 parameter-type-list[opt] )
3388 parameter-forward-declarations:
3389 parameter-list ;
3390 parameter-forward-declarations parameter-list ;
3392 The uses of attributes shown above are GNU extensions.
3394 Some forms of array declarator are not included in C99 in the
3395 syntax for abstract declarators; these are disallowed elsewhere.
3396 This may be a defect (DR#289).
3398 This function also accepts an omitted abstract declarator as being
3399 an abstract declarator, although not part of the formal syntax. */
3401 static struct c_declarator *
3402 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3403 bool *seen_id)
3405 /* Parse any initial pointer part. */
3406 if (c_parser_next_token_is (parser, CPP_MULT))
3408 struct c_declspecs *quals_attrs = build_null_declspecs ();
3409 struct c_declarator *inner;
3410 c_parser_consume_token (parser);
3411 c_parser_declspecs (parser, quals_attrs, false, false, true,
3412 false, false, cla_prefer_id);
3413 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3414 if (inner == NULL)
3415 return NULL;
3416 else
3417 return make_pointer_declarator (quals_attrs, inner);
3419 /* Now we have a direct declarator, direct abstract declarator or
3420 nothing (which counts as a direct abstract declarator here). */
3421 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3424 /* Parse a direct declarator or direct abstract declarator; arguments
3425 as c_parser_declarator. */
3427 static struct c_declarator *
3428 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3429 bool *seen_id)
3431 /* The direct declarator must start with an identifier (possibly
3432 omitted) or a parenthesized declarator (possibly abstract). In
3433 an ordinary declarator, initial parentheses must start a
3434 parenthesized declarator. In an abstract declarator or parameter
3435 declarator, they could start a parenthesized declarator or a
3436 parameter list. To tell which, the open parenthesis and any
3437 following attributes must be read. If a declaration specifier
3438 follows, then it is a parameter list; if the specifier is a
3439 typedef name, there might be an ambiguity about redeclaring it,
3440 which is resolved in the direction of treating it as a typedef
3441 name. If a close parenthesis follows, it is also an empty
3442 parameter list, as the syntax does not permit empty abstract
3443 declarators. Otherwise, it is a parenthesized declarator (in
3444 which case the analysis may be repeated inside it, recursively).
3446 ??? There is an ambiguity in a parameter declaration "int
3447 (__attribute__((foo)) x)", where x is not a typedef name: it
3448 could be an abstract declarator for a function, or declare x with
3449 parentheses. The proper resolution of this ambiguity needs
3450 documenting. At present we follow an accident of the old
3451 parser's implementation, whereby the first parameter must have
3452 some declaration specifiers other than just attributes. Thus as
3453 a parameter declaration it is treated as a parenthesized
3454 parameter named x, and as an abstract declarator it is
3455 rejected.
3457 ??? Also following the old parser, attributes inside an empty
3458 parameter list are ignored, making it a list not yielding a
3459 prototype, rather than giving an error or making it have one
3460 parameter with implicit type int.
3462 ??? Also following the old parser, typedef names may be
3463 redeclared in declarators, but not Objective-C class names. */
3465 if (kind != C_DTR_ABSTRACT
3466 && c_parser_next_token_is (parser, CPP_NAME)
3467 && ((type_seen_p
3468 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3469 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3470 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3472 struct c_declarator *inner
3473 = build_id_declarator (c_parser_peek_token (parser)->value);
3474 *seen_id = true;
3475 inner->id_loc = c_parser_peek_token (parser)->location;
3476 c_parser_consume_token (parser);
3477 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3480 if (kind != C_DTR_NORMAL
3481 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3483 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3484 inner->id_loc = c_parser_peek_token (parser)->location;
3485 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3488 /* Either we are at the end of an abstract declarator, or we have
3489 parentheses. */
3491 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3493 tree attrs;
3494 struct c_declarator *inner;
3495 c_parser_consume_token (parser);
3496 attrs = c_parser_attributes (parser);
3497 if (kind != C_DTR_NORMAL
3498 && (c_parser_next_token_starts_declspecs (parser)
3499 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3501 struct c_arg_info *args
3502 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3503 attrs);
3504 if (args == NULL)
3505 return NULL;
3506 else
3508 inner
3509 = build_function_declarator (args,
3510 build_id_declarator (NULL_TREE));
3511 return c_parser_direct_declarator_inner (parser, *seen_id,
3512 inner);
3515 /* A parenthesized declarator. */
3516 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3517 if (inner != NULL && attrs != NULL)
3518 inner = build_attrs_declarator (attrs, inner);
3519 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3521 c_parser_consume_token (parser);
3522 if (inner == NULL)
3523 return NULL;
3524 else
3525 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3527 else
3529 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3530 "expected %<)%>");
3531 return NULL;
3534 else
3536 if (kind == C_DTR_NORMAL)
3538 c_parser_error (parser, "expected identifier or %<(%>");
3539 return NULL;
3541 else
3542 return build_id_declarator (NULL_TREE);
3546 /* Parse part of a direct declarator or direct abstract declarator,
3547 given that some (in INNER) has already been parsed; ID_PRESENT is
3548 true if an identifier is present, false for an abstract
3549 declarator. */
3551 static struct c_declarator *
3552 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3553 struct c_declarator *inner)
3555 /* Parse a sequence of array declarators and parameter lists. */
3556 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3558 location_t brace_loc = c_parser_peek_token (parser)->location;
3559 struct c_declarator *declarator;
3560 struct c_declspecs *quals_attrs = build_null_declspecs ();
3561 bool static_seen;
3562 bool star_seen;
3563 struct c_expr dimen;
3564 dimen.value = NULL_TREE;
3565 dimen.original_code = ERROR_MARK;
3566 dimen.original_type = NULL_TREE;
3567 c_parser_consume_token (parser);
3568 c_parser_declspecs (parser, quals_attrs, false, false, true,
3569 false, false, cla_prefer_id);
3570 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3571 if (static_seen)
3572 c_parser_consume_token (parser);
3573 if (static_seen && !quals_attrs->declspecs_seen_p)
3574 c_parser_declspecs (parser, quals_attrs, false, false, true,
3575 false, false, cla_prefer_id);
3576 if (!quals_attrs->declspecs_seen_p)
3577 quals_attrs = NULL;
3578 /* If "static" is present, there must be an array dimension.
3579 Otherwise, there may be a dimension, "*", or no
3580 dimension. */
3581 if (static_seen)
3583 star_seen = false;
3584 dimen = c_parser_expr_no_commas (parser, NULL);
3586 else
3588 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3590 dimen.value = NULL_TREE;
3591 star_seen = false;
3593 else if (flag_cilkplus
3594 && c_parser_next_token_is (parser, CPP_COLON))
3596 dimen.value = error_mark_node;
3597 star_seen = false;
3598 error_at (c_parser_peek_token (parser)->location,
3599 "array notations cannot be used in declaration");
3600 c_parser_consume_token (parser);
3602 else if (c_parser_next_token_is (parser, CPP_MULT))
3604 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3606 dimen.value = NULL_TREE;
3607 star_seen = true;
3608 c_parser_consume_token (parser);
3610 else
3612 star_seen = false;
3613 dimen = c_parser_expr_no_commas (parser, NULL);
3616 else
3618 star_seen = false;
3619 dimen = c_parser_expr_no_commas (parser, NULL);
3622 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3623 c_parser_consume_token (parser);
3624 else if (flag_cilkplus
3625 && c_parser_next_token_is (parser, CPP_COLON))
3627 error_at (c_parser_peek_token (parser)->location,
3628 "array notations cannot be used in declaration");
3629 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3630 return NULL;
3632 else
3634 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3635 "expected %<]%>");
3636 return NULL;
3638 if (dimen.value)
3639 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3640 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3641 static_seen, star_seen);
3642 if (declarator == NULL)
3643 return NULL;
3644 inner = set_array_declarator_inner (declarator, inner);
3645 return c_parser_direct_declarator_inner (parser, id_present, inner);
3647 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3649 tree attrs;
3650 struct c_arg_info *args;
3651 c_parser_consume_token (parser);
3652 attrs = c_parser_attributes (parser);
3653 args = c_parser_parms_declarator (parser, id_present, attrs);
3654 if (args == NULL)
3655 return NULL;
3656 else
3658 inner = build_function_declarator (args, inner);
3659 return c_parser_direct_declarator_inner (parser, id_present, inner);
3662 return inner;
3665 /* Parse a parameter list or identifier list, including the closing
3666 parenthesis but not the opening one. ATTRS are the attributes at
3667 the start of the list. ID_LIST_OK is true if an identifier list is
3668 acceptable; such a list must not have attributes at the start. */
3670 static struct c_arg_info *
3671 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3673 push_scope ();
3674 declare_parm_level ();
3675 /* If the list starts with an identifier, it is an identifier list.
3676 Otherwise, it is either a prototype list or an empty list. */
3677 if (id_list_ok
3678 && !attrs
3679 && c_parser_next_token_is (parser, CPP_NAME)
3680 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3682 /* Look ahead to detect typos in type names. */
3683 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3684 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3685 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3686 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3687 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3689 tree list = NULL_TREE, *nextp = &list;
3690 while (c_parser_next_token_is (parser, CPP_NAME)
3691 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3693 *nextp = build_tree_list (NULL_TREE,
3694 c_parser_peek_token (parser)->value);
3695 nextp = & TREE_CHAIN (*nextp);
3696 c_parser_consume_token (parser);
3697 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3698 break;
3699 c_parser_consume_token (parser);
3700 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3702 c_parser_error (parser, "expected identifier");
3703 break;
3706 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3708 struct c_arg_info *ret = build_arg_info ();
3709 ret->types = list;
3710 c_parser_consume_token (parser);
3711 pop_scope ();
3712 return ret;
3714 else
3716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3717 "expected %<)%>");
3718 pop_scope ();
3719 return NULL;
3722 else
3724 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3725 NULL);
3726 pop_scope ();
3727 return ret;
3731 /* Parse a parameter list (possibly empty), including the closing
3732 parenthesis but not the opening one. ATTRS are the attributes at
3733 the start of the list. EXPR is NULL or an expression that needs to
3734 be evaluated for the side effects of array size expressions in the
3735 parameters. */
3737 static struct c_arg_info *
3738 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3740 bool bad_parm = false;
3742 /* ??? Following the old parser, forward parameter declarations may
3743 use abstract declarators, and if no real parameter declarations
3744 follow the forward declarations then this is not diagnosed. Also
3745 note as above that attributes are ignored as the only contents of
3746 the parentheses, or as the only contents after forward
3747 declarations. */
3748 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3750 struct c_arg_info *ret = build_arg_info ();
3751 c_parser_consume_token (parser);
3752 return ret;
3754 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3756 struct c_arg_info *ret = build_arg_info ();
3758 if (flag_allow_parameterless_variadic_functions)
3760 /* F (...) is allowed. */
3761 ret->types = NULL_TREE;
3763 else
3765 /* Suppress -Wold-style-definition for this case. */
3766 ret->types = error_mark_node;
3767 error_at (c_parser_peek_token (parser)->location,
3768 "ISO C requires a named argument before %<...%>");
3770 c_parser_consume_token (parser);
3771 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3773 c_parser_consume_token (parser);
3774 return ret;
3776 else
3778 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3779 "expected %<)%>");
3780 return NULL;
3783 /* Nonempty list of parameters, either terminated with semicolon
3784 (forward declarations; recurse) or with close parenthesis (normal
3785 function) or with ", ... )" (variadic function). */
3786 while (true)
3788 /* Parse a parameter. */
3789 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3790 attrs = NULL_TREE;
3791 if (parm == NULL)
3792 bad_parm = true;
3793 else
3794 push_parm_decl (parm, &expr);
3795 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3797 tree new_attrs;
3798 c_parser_consume_token (parser);
3799 mark_forward_parm_decls ();
3800 new_attrs = c_parser_attributes (parser);
3801 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3803 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3805 c_parser_consume_token (parser);
3806 if (bad_parm)
3807 return NULL;
3808 else
3809 return get_parm_info (false, expr);
3811 if (!c_parser_require (parser, CPP_COMMA,
3812 "expected %<;%>, %<,%> or %<)%>"))
3814 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3815 return NULL;
3817 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3819 c_parser_consume_token (parser);
3820 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3822 c_parser_consume_token (parser);
3823 if (bad_parm)
3824 return NULL;
3825 else
3826 return get_parm_info (true, expr);
3828 else
3830 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3831 "expected %<)%>");
3832 return NULL;
3838 /* Parse a parameter declaration. ATTRS are the attributes at the
3839 start of the declaration if it is the first parameter. */
3841 static struct c_parm *
3842 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3844 struct c_declspecs *specs;
3845 struct c_declarator *declarator;
3846 tree prefix_attrs;
3847 tree postfix_attrs = NULL_TREE;
3848 bool dummy = false;
3850 /* Accept #pragmas between parameter declarations. */
3851 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3852 c_parser_pragma (parser, pragma_param, NULL);
3854 if (!c_parser_next_token_starts_declspecs (parser))
3856 c_token *token = c_parser_peek_token (parser);
3857 if (parser->error)
3858 return NULL;
3859 c_parser_set_source_position_from_token (token);
3860 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3862 const char *hint = lookup_name_fuzzy (token->value,
3863 FUZZY_LOOKUP_TYPENAME);
3864 if (hint)
3866 gcc_rich_location richloc (token->location);
3867 richloc.add_fixit_replace (hint);
3868 error_at_rich_loc (&richloc,
3869 "unknown type name %qE; did you mean %qs?",
3870 token->value, hint);
3872 else
3873 error_at (token->location, "unknown type name %qE", token->value);
3874 parser->error = true;
3876 /* ??? In some Objective-C cases '...' isn't applicable so there
3877 should be a different message. */
3878 else
3879 c_parser_error (parser,
3880 "expected declaration specifiers or %<...%>");
3881 c_parser_skip_to_end_of_parameter (parser);
3882 return NULL;
3884 specs = build_null_declspecs ();
3885 if (attrs)
3887 declspecs_add_attrs (input_location, specs, attrs);
3888 attrs = NULL_TREE;
3890 c_parser_declspecs (parser, specs, true, true, true, true, false,
3891 cla_nonabstract_decl);
3892 finish_declspecs (specs);
3893 pending_xref_error ();
3894 prefix_attrs = specs->attrs;
3895 specs->attrs = NULL_TREE;
3896 declarator = c_parser_declarator (parser,
3897 specs->typespec_kind != ctsk_none,
3898 C_DTR_PARM, &dummy);
3899 if (declarator == NULL)
3901 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3902 return NULL;
3904 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3905 postfix_attrs = c_parser_attributes (parser);
3906 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3907 declarator);
3910 /* Parse a string literal in an asm expression. It should not be
3911 translated, and wide string literals are an error although
3912 permitted by the syntax. This is a GNU extension.
3914 asm-string-literal:
3915 string-literal
3917 ??? At present, following the old parser, the caller needs to have
3918 set lex_untranslated_string to 1. It would be better to follow the
3919 C++ parser rather than using this kludge. */
3921 static tree
3922 c_parser_asm_string_literal (c_parser *parser)
3924 tree str;
3925 int save_flag = warn_overlength_strings;
3926 warn_overlength_strings = 0;
3927 if (c_parser_next_token_is (parser, CPP_STRING))
3929 str = c_parser_peek_token (parser)->value;
3930 c_parser_consume_token (parser);
3932 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3934 error_at (c_parser_peek_token (parser)->location,
3935 "wide string literal in %<asm%>");
3936 str = build_string (1, "");
3937 c_parser_consume_token (parser);
3939 else
3941 c_parser_error (parser, "expected string literal");
3942 str = NULL_TREE;
3944 warn_overlength_strings = save_flag;
3945 return str;
3948 /* Parse a simple asm expression. This is used in restricted
3949 contexts, where a full expression with inputs and outputs does not
3950 make sense. This is a GNU extension.
3952 simple-asm-expr:
3953 asm ( asm-string-literal )
3956 static tree
3957 c_parser_simple_asm_expr (c_parser *parser)
3959 tree str;
3960 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3961 /* ??? Follow the C++ parser rather than using the
3962 lex_untranslated_string kludge. */
3963 parser->lex_untranslated_string = true;
3964 c_parser_consume_token (parser);
3965 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3967 parser->lex_untranslated_string = false;
3968 return NULL_TREE;
3970 str = c_parser_asm_string_literal (parser);
3971 parser->lex_untranslated_string = false;
3972 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3974 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3975 return NULL_TREE;
3977 return str;
3980 static tree
3981 c_parser_attribute_any_word (c_parser *parser)
3983 tree attr_name = NULL_TREE;
3985 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3987 /* ??? See comment above about what keywords are accepted here. */
3988 bool ok;
3989 switch (c_parser_peek_token (parser)->keyword)
3991 case RID_STATIC:
3992 case RID_UNSIGNED:
3993 case RID_LONG:
3994 case RID_CONST:
3995 case RID_EXTERN:
3996 case RID_REGISTER:
3997 case RID_TYPEDEF:
3998 case RID_SHORT:
3999 case RID_INLINE:
4000 case RID_NORETURN:
4001 case RID_VOLATILE:
4002 case RID_SIGNED:
4003 case RID_AUTO:
4004 case RID_RESTRICT:
4005 case RID_COMPLEX:
4006 case RID_THREAD:
4007 case RID_INT:
4008 case RID_CHAR:
4009 case RID_FLOAT:
4010 case RID_DOUBLE:
4011 case RID_VOID:
4012 case RID_DFLOAT32:
4013 case RID_DFLOAT64:
4014 case RID_DFLOAT128:
4015 CASE_RID_FLOATN_NX:
4016 case RID_BOOL:
4017 case RID_FRACT:
4018 case RID_ACCUM:
4019 case RID_SAT:
4020 case RID_TRANSACTION_ATOMIC:
4021 case RID_TRANSACTION_CANCEL:
4022 case RID_ATOMIC:
4023 case RID_AUTO_TYPE:
4024 case RID_INT_N_0:
4025 case RID_INT_N_1:
4026 case RID_INT_N_2:
4027 case RID_INT_N_3:
4028 ok = true;
4029 break;
4030 default:
4031 ok = false;
4032 break;
4034 if (!ok)
4035 return NULL_TREE;
4037 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4038 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4040 else if (c_parser_next_token_is (parser, CPP_NAME))
4041 attr_name = c_parser_peek_token (parser)->value;
4043 return attr_name;
4046 #define CILK_SIMD_FN_CLAUSE_MASK \
4047 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
4048 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
4049 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
4050 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
4051 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
4053 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
4054 VEC_TOKEN is the "vector" token that is replaced with "simd" and
4055 pushed into the token list.
4056 Syntax:
4057 vector
4058 vector (<vector attributes>). */
4060 static void
4061 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
4063 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4065 int paren_scope = 0;
4066 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4067 /* Consume the "vector" token. */
4068 c_parser_consume_token (parser);
4070 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4072 c_parser_consume_token (parser);
4073 paren_scope++;
4075 while (paren_scope > 0)
4077 c_token *token = c_parser_peek_token (parser);
4078 if (token->type == CPP_OPEN_PAREN)
4079 paren_scope++;
4080 else if (token->type == CPP_CLOSE_PAREN)
4081 paren_scope--;
4082 /* Do not push the last ')' since we are not pushing the '('. */
4083 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4084 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4085 c_parser_consume_token (parser);
4088 /* Since we are converting an attribute to a pragma, we need to end the
4089 attribute with PRAGMA_EOL. */
4090 c_token eol_token;
4091 memset (&eol_token, 0, sizeof (eol_token));
4092 eol_token.type = CPP_PRAGMA_EOL;
4093 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4096 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4098 static void
4099 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4101 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4103 /* c_parser_attributes is called in several places, so if these EOF
4104 tokens are already inserted, then don't do them again. */
4105 if (last_token.type == CPP_EOF)
4106 return;
4108 /* Two CPP_EOF token are added as a safety net since the normal C
4109 front-end has two token look-ahead. */
4110 c_token eof_token;
4111 eof_token.type = CPP_EOF;
4112 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4113 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4116 /* Parse (possibly empty) attributes. This is a GNU extension.
4118 attributes:
4119 empty
4120 attributes attribute
4122 attribute:
4123 __attribute__ ( ( attribute-list ) )
4125 attribute-list:
4126 attrib
4127 attribute_list , attrib
4129 attrib:
4130 empty
4131 any-word
4132 any-word ( identifier )
4133 any-word ( identifier , nonempty-expr-list )
4134 any-word ( expr-list )
4136 where the "identifier" must not be declared as a type, and
4137 "any-word" may be any identifier (including one declared as a
4138 type), a reserved word storage class specifier, type specifier or
4139 type qualifier. ??? This still leaves out most reserved keywords
4140 (following the old parser), shouldn't we include them, and why not
4141 allow identifiers declared as types to start the arguments? */
4143 static tree
4144 c_parser_attributes (c_parser *parser)
4146 tree attrs = NULL_TREE;
4147 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4149 /* ??? Follow the C++ parser rather than using the
4150 lex_untranslated_string kludge. */
4151 parser->lex_untranslated_string = true;
4152 /* Consume the `__attribute__' keyword. */
4153 c_parser_consume_token (parser);
4154 /* Look for the two `(' tokens. */
4155 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4157 parser->lex_untranslated_string = false;
4158 return attrs;
4160 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4162 parser->lex_untranslated_string = false;
4163 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4164 return attrs;
4166 /* Parse the attribute list. */
4167 while (c_parser_next_token_is (parser, CPP_COMMA)
4168 || c_parser_next_token_is (parser, CPP_NAME)
4169 || c_parser_next_token_is (parser, CPP_KEYWORD))
4171 tree attr, attr_name, attr_args;
4172 vec<tree, va_gc> *expr_list;
4173 if (c_parser_next_token_is (parser, CPP_COMMA))
4175 c_parser_consume_token (parser);
4176 continue;
4179 attr_name = c_parser_attribute_any_word (parser);
4180 if (attr_name == NULL)
4181 break;
4182 if (is_cilkplus_vector_p (attr_name))
4184 c_token *v_token = c_parser_peek_token (parser);
4185 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4186 /* If the next token isn't a comma, we're done. */
4187 if (!c_parser_next_token_is (parser, CPP_COMMA))
4188 break;
4189 continue;
4191 c_parser_consume_token (parser);
4192 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4194 attr = build_tree_list (attr_name, NULL_TREE);
4195 /* Add this attribute to the list. */
4196 attrs = chainon (attrs, attr);
4197 /* If the next token isn't a comma, we're done. */
4198 if (!c_parser_next_token_is (parser, CPP_COMMA))
4199 break;
4200 continue;
4202 c_parser_consume_token (parser);
4203 /* Parse the attribute contents. If they start with an
4204 identifier which is followed by a comma or close
4205 parenthesis, then the arguments start with that
4206 identifier; otherwise they are an expression list.
4207 In objective-c the identifier may be a classname. */
4208 if (c_parser_next_token_is (parser, CPP_NAME)
4209 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4210 || (c_dialect_objc ()
4211 && c_parser_peek_token (parser)->id_kind
4212 == C_ID_CLASSNAME))
4213 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4214 || (c_parser_peek_2nd_token (parser)->type
4215 == CPP_CLOSE_PAREN))
4216 && (attribute_takes_identifier_p (attr_name)
4217 || (c_dialect_objc ()
4218 && c_parser_peek_token (parser)->id_kind
4219 == C_ID_CLASSNAME)))
4221 tree arg1 = c_parser_peek_token (parser)->value;
4222 c_parser_consume_token (parser);
4223 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4224 attr_args = build_tree_list (NULL_TREE, arg1);
4225 else
4227 tree tree_list;
4228 c_parser_consume_token (parser);
4229 expr_list = c_parser_expr_list (parser, false, true,
4230 NULL, NULL, NULL, NULL);
4231 tree_list = build_tree_list_vec (expr_list);
4232 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4233 release_tree_vector (expr_list);
4236 else
4238 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4239 attr_args = NULL_TREE;
4240 else
4242 expr_list = c_parser_expr_list (parser, false, true,
4243 NULL, NULL, NULL, NULL);
4244 attr_args = build_tree_list_vec (expr_list);
4245 release_tree_vector (expr_list);
4248 attr = build_tree_list (attr_name, attr_args);
4249 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4250 c_parser_consume_token (parser);
4251 else
4253 parser->lex_untranslated_string = false;
4254 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4255 "expected %<)%>");
4256 return attrs;
4258 /* Add this attribute to the list. */
4259 attrs = chainon (attrs, attr);
4260 /* If the next token isn't a comma, we're done. */
4261 if (!c_parser_next_token_is (parser, CPP_COMMA))
4262 break;
4264 /* Look for the two `)' tokens. */
4265 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4266 c_parser_consume_token (parser);
4267 else
4269 parser->lex_untranslated_string = false;
4270 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4271 "expected %<)%>");
4272 return attrs;
4274 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4275 c_parser_consume_token (parser);
4276 else
4278 parser->lex_untranslated_string = false;
4279 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4280 "expected %<)%>");
4281 return attrs;
4283 parser->lex_untranslated_string = false;
4286 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4287 c_finish_cilk_simd_fn_tokens (parser);
4288 return attrs;
4291 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4293 type-name:
4294 specifier-qualifier-list abstract-declarator[opt]
4297 static struct c_type_name *
4298 c_parser_type_name (c_parser *parser)
4300 struct c_declspecs *specs = build_null_declspecs ();
4301 struct c_declarator *declarator;
4302 struct c_type_name *ret;
4303 bool dummy = false;
4304 c_parser_declspecs (parser, specs, false, true, true, false, false,
4305 cla_prefer_type);
4306 if (!specs->declspecs_seen_p)
4308 c_parser_error (parser, "expected specifier-qualifier-list");
4309 return NULL;
4311 if (specs->type != error_mark_node)
4313 pending_xref_error ();
4314 finish_declspecs (specs);
4316 declarator = c_parser_declarator (parser,
4317 specs->typespec_kind != ctsk_none,
4318 C_DTR_ABSTRACT, &dummy);
4319 if (declarator == NULL)
4320 return NULL;
4321 ret = XOBNEW (&parser_obstack, struct c_type_name);
4322 ret->specs = specs;
4323 ret->declarator = declarator;
4324 return ret;
4327 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4329 initializer:
4330 assignment-expression
4331 { initializer-list }
4332 { initializer-list , }
4334 initializer-list:
4335 designation[opt] initializer
4336 initializer-list , designation[opt] initializer
4338 designation:
4339 designator-list =
4341 designator-list:
4342 designator
4343 designator-list designator
4345 designator:
4346 array-designator
4347 . identifier
4349 array-designator:
4350 [ constant-expression ]
4352 GNU extensions:
4354 initializer:
4357 designation:
4358 array-designator
4359 identifier :
4361 array-designator:
4362 [ constant-expression ... constant-expression ]
4364 Any expression without commas is accepted in the syntax for the
4365 constant-expressions, with non-constant expressions rejected later.
4367 This function is only used for top-level initializers; for nested
4368 ones, see c_parser_initval. */
4370 static struct c_expr
4371 c_parser_initializer (c_parser *parser)
4373 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4374 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4375 else
4377 struct c_expr ret;
4378 location_t loc = c_parser_peek_token (parser)->location;
4379 ret = c_parser_expr_no_commas (parser, NULL);
4380 if (TREE_CODE (ret.value) != STRING_CST
4381 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4382 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4383 return ret;
4387 /* Parse a braced initializer list. TYPE is the type specified for a
4388 compound literal, and NULL_TREE for other initializers and for
4389 nested braced lists. NESTED_P is true for nested braced lists,
4390 false for the list of a compound literal or the list that is the
4391 top-level initializer in a declaration. */
4393 static struct c_expr
4394 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4395 struct obstack *outer_obstack)
4397 struct c_expr ret;
4398 struct obstack braced_init_obstack;
4399 location_t brace_loc = c_parser_peek_token (parser)->location;
4400 gcc_obstack_init (&braced_init_obstack);
4401 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4402 c_parser_consume_token (parser);
4403 if (nested_p)
4405 finish_implicit_inits (brace_loc, outer_obstack);
4406 push_init_level (brace_loc, 0, &braced_init_obstack);
4408 else
4409 really_start_incremental_init (type);
4410 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4412 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4414 else
4416 /* Parse a non-empty initializer list, possibly with a trailing
4417 comma. */
4418 while (true)
4420 c_parser_initelt (parser, &braced_init_obstack);
4421 if (parser->error)
4422 break;
4423 if (c_parser_next_token_is (parser, CPP_COMMA))
4424 c_parser_consume_token (parser);
4425 else
4426 break;
4427 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4428 break;
4431 c_token *next_tok = c_parser_peek_token (parser);
4432 if (next_tok->type != CPP_CLOSE_BRACE)
4434 ret.value = error_mark_node;
4435 ret.original_code = ERROR_MARK;
4436 ret.original_type = NULL;
4437 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4438 pop_init_level (brace_loc, 0, &braced_init_obstack);
4439 obstack_free (&braced_init_obstack, NULL);
4440 return ret;
4442 location_t close_loc = next_tok->location;
4443 c_parser_consume_token (parser);
4444 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4445 obstack_free (&braced_init_obstack, NULL);
4446 set_c_expr_source_range (&ret, brace_loc, close_loc);
4447 return ret;
4450 /* Parse a nested initializer, including designators. */
4452 static void
4453 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4455 /* Parse any designator or designator list. A single array
4456 designator may have the subsequent "=" omitted in GNU C, but a
4457 longer list or a structure member designator may not. */
4458 if (c_parser_next_token_is (parser, CPP_NAME)
4459 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4461 /* Old-style structure member designator. */
4462 set_init_label (c_parser_peek_token (parser)->location,
4463 c_parser_peek_token (parser)->value,
4464 c_parser_peek_token (parser)->location,
4465 braced_init_obstack);
4466 /* Use the colon as the error location. */
4467 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4468 "obsolete use of designated initializer with %<:%>");
4469 c_parser_consume_token (parser);
4470 c_parser_consume_token (parser);
4472 else
4474 /* des_seen is 0 if there have been no designators, 1 if there
4475 has been a single array designator and 2 otherwise. */
4476 int des_seen = 0;
4477 /* Location of a designator. */
4478 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4479 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4480 || c_parser_next_token_is (parser, CPP_DOT))
4482 int des_prev = des_seen;
4483 if (!des_seen)
4484 des_loc = c_parser_peek_token (parser)->location;
4485 if (des_seen < 2)
4486 des_seen++;
4487 if (c_parser_next_token_is (parser, CPP_DOT))
4489 des_seen = 2;
4490 c_parser_consume_token (parser);
4491 if (c_parser_next_token_is (parser, CPP_NAME))
4493 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4494 c_parser_peek_token (parser)->location,
4495 braced_init_obstack);
4496 c_parser_consume_token (parser);
4498 else
4500 struct c_expr init;
4501 init.value = error_mark_node;
4502 init.original_code = ERROR_MARK;
4503 init.original_type = NULL;
4504 c_parser_error (parser, "expected identifier");
4505 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4506 process_init_element (input_location, init, false,
4507 braced_init_obstack);
4508 return;
4511 else
4513 tree first, second;
4514 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4515 location_t array_index_loc = UNKNOWN_LOCATION;
4516 /* ??? Following the old parser, [ objc-receiver
4517 objc-message-args ] is accepted as an initializer,
4518 being distinguished from a designator by what follows
4519 the first assignment expression inside the square
4520 brackets, but after a first array designator a
4521 subsequent square bracket is for Objective-C taken to
4522 start an expression, using the obsolete form of
4523 designated initializer without '=', rather than
4524 possibly being a second level of designation: in LALR
4525 terms, the '[' is shifted rather than reducing
4526 designator to designator-list. */
4527 if (des_prev == 1 && c_dialect_objc ())
4529 des_seen = des_prev;
4530 break;
4532 if (des_prev == 0 && c_dialect_objc ())
4534 /* This might be an array designator or an
4535 Objective-C message expression. If the former,
4536 continue parsing here; if the latter, parse the
4537 remainder of the initializer given the starting
4538 primary-expression. ??? It might make sense to
4539 distinguish when des_prev == 1 as well; see
4540 previous comment. */
4541 tree rec, args;
4542 struct c_expr mexpr;
4543 c_parser_consume_token (parser);
4544 if (c_parser_peek_token (parser)->type == CPP_NAME
4545 && ((c_parser_peek_token (parser)->id_kind
4546 == C_ID_TYPENAME)
4547 || (c_parser_peek_token (parser)->id_kind
4548 == C_ID_CLASSNAME)))
4550 /* Type name receiver. */
4551 tree id = c_parser_peek_token (parser)->value;
4552 c_parser_consume_token (parser);
4553 rec = objc_get_class_reference (id);
4554 goto parse_message_args;
4556 first = c_parser_expr_no_commas (parser, NULL).value;
4557 mark_exp_read (first);
4558 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4559 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4560 goto array_desig_after_first;
4561 /* Expression receiver. So far only one part
4562 without commas has been parsed; there might be
4563 more of the expression. */
4564 rec = first;
4565 while (c_parser_next_token_is (parser, CPP_COMMA))
4567 struct c_expr next;
4568 location_t comma_loc, exp_loc;
4569 comma_loc = c_parser_peek_token (parser)->location;
4570 c_parser_consume_token (parser);
4571 exp_loc = c_parser_peek_token (parser)->location;
4572 next = c_parser_expr_no_commas (parser, NULL);
4573 next = convert_lvalue_to_rvalue (exp_loc, next,
4574 true, true);
4575 rec = build_compound_expr (comma_loc, rec, next.value);
4577 parse_message_args:
4578 /* Now parse the objc-message-args. */
4579 args = c_parser_objc_message_args (parser);
4580 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4581 "expected %<]%>");
4582 mexpr.value
4583 = objc_build_message_expr (rec, args);
4584 mexpr.original_code = ERROR_MARK;
4585 mexpr.original_type = NULL;
4586 /* Now parse and process the remainder of the
4587 initializer, starting with this message
4588 expression as a primary-expression. */
4589 c_parser_initval (parser, &mexpr, braced_init_obstack);
4590 return;
4592 c_parser_consume_token (parser);
4593 array_index_loc = c_parser_peek_token (parser)->location;
4594 first = c_parser_expr_no_commas (parser, NULL).value;
4595 mark_exp_read (first);
4596 array_desig_after_first:
4597 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4599 ellipsis_loc = c_parser_peek_token (parser)->location;
4600 c_parser_consume_token (parser);
4601 second = c_parser_expr_no_commas (parser, NULL).value;
4602 mark_exp_read (second);
4604 else
4605 second = NULL_TREE;
4606 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4608 c_parser_consume_token (parser);
4609 set_init_index (array_index_loc, first, second,
4610 braced_init_obstack);
4611 if (second)
4612 pedwarn (ellipsis_loc, OPT_Wpedantic,
4613 "ISO C forbids specifying range of elements to initialize");
4615 else
4616 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4617 "expected %<]%>");
4620 if (des_seen >= 1)
4622 if (c_parser_next_token_is (parser, CPP_EQ))
4624 pedwarn_c90 (des_loc, OPT_Wpedantic,
4625 "ISO C90 forbids specifying subobject "
4626 "to initialize");
4627 c_parser_consume_token (parser);
4629 else
4631 if (des_seen == 1)
4632 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4633 "obsolete use of designated initializer without %<=%>");
4634 else
4636 struct c_expr init;
4637 init.value = error_mark_node;
4638 init.original_code = ERROR_MARK;
4639 init.original_type = NULL;
4640 c_parser_error (parser, "expected %<=%>");
4641 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4642 process_init_element (input_location, init, false,
4643 braced_init_obstack);
4644 return;
4649 c_parser_initval (parser, NULL, braced_init_obstack);
4652 /* Parse a nested initializer; as c_parser_initializer but parses
4653 initializers within braced lists, after any designators have been
4654 applied. If AFTER is not NULL then it is an Objective-C message
4655 expression which is the primary-expression starting the
4656 initializer. */
4658 static void
4659 c_parser_initval (c_parser *parser, struct c_expr *after,
4660 struct obstack * braced_init_obstack)
4662 struct c_expr init;
4663 gcc_assert (!after || c_dialect_objc ());
4664 location_t loc = c_parser_peek_token (parser)->location;
4666 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4667 init = c_parser_braced_init (parser, NULL_TREE, true,
4668 braced_init_obstack);
4669 else
4671 init = c_parser_expr_no_commas (parser, after);
4672 if (init.value != NULL_TREE
4673 && TREE_CODE (init.value) != STRING_CST
4674 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4675 init = convert_lvalue_to_rvalue (loc, init, true, true);
4677 process_init_element (loc, init, false, braced_init_obstack);
4680 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4681 C99 6.8.2).
4683 compound-statement:
4684 { block-item-list[opt] }
4685 { label-declarations block-item-list }
4687 block-item-list:
4688 block-item
4689 block-item-list block-item
4691 block-item:
4692 nested-declaration
4693 statement
4695 nested-declaration:
4696 declaration
4698 GNU extensions:
4700 compound-statement:
4701 { label-declarations block-item-list }
4703 nested-declaration:
4704 __extension__ nested-declaration
4705 nested-function-definition
4707 label-declarations:
4708 label-declaration
4709 label-declarations label-declaration
4711 label-declaration:
4712 __label__ identifier-list ;
4714 Allowing the mixing of declarations and code is new in C99. The
4715 GNU syntax also permits (not shown above) labels at the end of
4716 compound statements, which yield an error. We don't allow labels
4717 on declarations; this might seem like a natural extension, but
4718 there would be a conflict between attributes on the label and
4719 prefix attributes on the declaration. ??? The syntax follows the
4720 old parser in requiring something after label declarations.
4721 Although they are erroneous if the labels declared aren't defined,
4722 is it useful for the syntax to be this way?
4724 OpenACC:
4726 block-item:
4727 openacc-directive
4729 openacc-directive:
4730 update-directive
4732 OpenMP:
4734 block-item:
4735 openmp-directive
4737 openmp-directive:
4738 barrier-directive
4739 flush-directive
4740 taskwait-directive
4741 taskyield-directive
4742 cancel-directive
4743 cancellation-point-directive */
4745 static tree
4746 c_parser_compound_statement (c_parser *parser)
4748 tree stmt;
4749 location_t brace_loc;
4750 brace_loc = c_parser_peek_token (parser)->location;
4751 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4753 /* Ensure a scope is entered and left anyway to avoid confusion
4754 if we have just prepared to enter a function body. */
4755 stmt = c_begin_compound_stmt (true);
4756 c_end_compound_stmt (brace_loc, stmt, true);
4757 return error_mark_node;
4759 stmt = c_begin_compound_stmt (true);
4760 c_parser_compound_statement_nostart (parser);
4762 /* If the compound stmt contains array notations, then we expand them. */
4763 if (flag_cilkplus && contains_array_notation_expr (stmt))
4764 stmt = expand_array_notation_exprs (stmt);
4765 return c_end_compound_stmt (brace_loc, stmt, true);
4768 /* Parse a compound statement except for the opening brace. This is
4769 used for parsing both compound statements and statement expressions
4770 (which follow different paths to handling the opening). */
4772 static void
4773 c_parser_compound_statement_nostart (c_parser *parser)
4775 bool last_stmt = false;
4776 bool last_label = false;
4777 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4778 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4779 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4781 c_parser_consume_token (parser);
4782 return;
4784 mark_valid_location_for_stdc_pragma (true);
4785 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4787 /* Read zero or more forward-declarations for labels that nested
4788 functions can jump to. */
4789 mark_valid_location_for_stdc_pragma (false);
4790 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4792 label_loc = c_parser_peek_token (parser)->location;
4793 c_parser_consume_token (parser);
4794 /* Any identifiers, including those declared as type names,
4795 are OK here. */
4796 while (true)
4798 tree label;
4799 if (c_parser_next_token_is_not (parser, CPP_NAME))
4801 c_parser_error (parser, "expected identifier");
4802 break;
4804 label
4805 = declare_label (c_parser_peek_token (parser)->value);
4806 C_DECLARED_LABEL_FLAG (label) = 1;
4807 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4808 c_parser_consume_token (parser);
4809 if (c_parser_next_token_is (parser, CPP_COMMA))
4810 c_parser_consume_token (parser);
4811 else
4812 break;
4814 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4816 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4818 /* We must now have at least one statement, label or declaration. */
4819 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4821 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4822 c_parser_error (parser, "expected declaration or statement");
4823 c_parser_consume_token (parser);
4824 return;
4826 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4828 location_t loc = c_parser_peek_token (parser)->location;
4829 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4830 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4831 || (c_parser_next_token_is (parser, CPP_NAME)
4832 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4834 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4835 label_loc = c_parser_peek_2nd_token (parser)->location;
4836 else
4837 label_loc = c_parser_peek_token (parser)->location;
4838 last_label = true;
4839 last_stmt = false;
4840 mark_valid_location_for_stdc_pragma (false);
4841 c_parser_label (parser);
4843 else if (!last_label
4844 && c_parser_next_tokens_start_declaration (parser))
4846 last_label = false;
4847 mark_valid_location_for_stdc_pragma (false);
4848 c_parser_declaration_or_fndef (parser, true, true, true, true,
4849 true, NULL, vNULL);
4850 if (last_stmt)
4851 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4852 "ISO C90 forbids mixed declarations and code");
4853 last_stmt = false;
4855 else if (!last_label
4856 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4858 /* __extension__ can start a declaration, but is also an
4859 unary operator that can start an expression. Consume all
4860 but the last of a possible series of __extension__ to
4861 determine which. */
4862 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4863 && (c_parser_peek_2nd_token (parser)->keyword
4864 == RID_EXTENSION))
4865 c_parser_consume_token (parser);
4866 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4868 int ext;
4869 ext = disable_extension_diagnostics ();
4870 c_parser_consume_token (parser);
4871 last_label = false;
4872 mark_valid_location_for_stdc_pragma (false);
4873 c_parser_declaration_or_fndef (parser, true, true, true, true,
4874 true, NULL, vNULL);
4875 /* Following the old parser, __extension__ does not
4876 disable this diagnostic. */
4877 restore_extension_diagnostics (ext);
4878 if (last_stmt)
4879 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4880 "ISO C90 forbids mixed declarations and code");
4881 last_stmt = false;
4883 else
4884 goto statement;
4886 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4888 /* External pragmas, and some omp pragmas, are not associated
4889 with regular c code, and so are not to be considered statements
4890 syntactically. This ensures that the user doesn't put them
4891 places that would turn into syntax errors if the directive
4892 were ignored. */
4893 if (c_parser_pragma (parser,
4894 last_label ? pragma_stmt : pragma_compound,
4895 NULL))
4896 last_label = false, last_stmt = true;
4898 else if (c_parser_next_token_is (parser, CPP_EOF))
4900 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4901 c_parser_error (parser, "expected declaration or statement");
4902 return;
4904 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4906 if (parser->in_if_block)
4908 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4909 error_at (loc, """expected %<}%> before %<else%>");
4910 return;
4912 else
4914 error_at (loc, "%<else%> without a previous %<if%>");
4915 c_parser_consume_token (parser);
4916 continue;
4919 else
4921 statement:
4922 last_label = false;
4923 last_stmt = true;
4924 mark_valid_location_for_stdc_pragma (false);
4925 c_parser_statement_after_labels (parser, NULL);
4928 parser->error = false;
4930 if (last_label)
4931 error_at (label_loc, "label at end of compound statement");
4932 c_parser_consume_token (parser);
4933 /* Restore the value we started with. */
4934 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4937 /* Parse all consecutive labels. */
4939 static void
4940 c_parser_all_labels (c_parser *parser)
4942 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4943 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4944 || (c_parser_next_token_is (parser, CPP_NAME)
4945 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4946 c_parser_label (parser);
4949 /* Parse a label (C90 6.6.1, C99 6.8.1).
4951 label:
4952 identifier : attributes[opt]
4953 case constant-expression :
4954 default :
4956 GNU extensions:
4958 label:
4959 case constant-expression ... constant-expression :
4961 The use of attributes on labels is a GNU extension. The syntax in
4962 GNU C accepts any expressions without commas, non-constant
4963 expressions being rejected later. */
4965 static void
4966 c_parser_label (c_parser *parser)
4968 location_t loc1 = c_parser_peek_token (parser)->location;
4969 tree label = NULL_TREE;
4970 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4972 tree exp1, exp2;
4973 c_parser_consume_token (parser);
4974 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4975 if (c_parser_next_token_is (parser, CPP_COLON))
4977 c_parser_consume_token (parser);
4978 label = do_case (loc1, exp1, NULL_TREE);
4980 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4982 c_parser_consume_token (parser);
4983 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4984 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4985 label = do_case (loc1, exp1, exp2);
4987 else
4988 c_parser_error (parser, "expected %<:%> or %<...%>");
4990 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4992 c_parser_consume_token (parser);
4993 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4994 label = do_case (loc1, NULL_TREE, NULL_TREE);
4996 else
4998 tree name = c_parser_peek_token (parser)->value;
4999 tree tlab;
5000 tree attrs;
5001 location_t loc2 = c_parser_peek_token (parser)->location;
5002 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5003 c_parser_consume_token (parser);
5004 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5005 c_parser_consume_token (parser);
5006 attrs = c_parser_attributes (parser);
5007 tlab = define_label (loc2, name);
5008 if (tlab)
5010 decl_attributes (&tlab, attrs, 0);
5011 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5014 if (label)
5016 if (c_parser_next_tokens_start_declaration (parser))
5018 error_at (c_parser_peek_token (parser)->location,
5019 "a label can only be part of a statement and "
5020 "a declaration is not a statement");
5021 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5022 /*static_assert_ok*/ true,
5023 /*empty_ok*/ true, /*nested*/ true,
5024 /*start_attr_ok*/ true, NULL,
5025 vNULL);
5030 /* Parse a statement (C90 6.6, C99 6.8).
5032 statement:
5033 labeled-statement
5034 compound-statement
5035 expression-statement
5036 selection-statement
5037 iteration-statement
5038 jump-statement
5040 labeled-statement:
5041 label statement
5043 expression-statement:
5044 expression[opt] ;
5046 selection-statement:
5047 if-statement
5048 switch-statement
5050 iteration-statement:
5051 while-statement
5052 do-statement
5053 for-statement
5055 jump-statement:
5056 goto identifier ;
5057 continue ;
5058 break ;
5059 return expression[opt] ;
5061 GNU extensions:
5063 statement:
5064 asm-statement
5066 jump-statement:
5067 goto * expression ;
5069 Objective-C:
5071 statement:
5072 objc-throw-statement
5073 objc-try-catch-statement
5074 objc-synchronized-statement
5076 objc-throw-statement:
5077 @throw expression ;
5078 @throw ;
5080 OpenACC:
5082 statement:
5083 openacc-construct
5085 openacc-construct:
5086 parallel-construct
5087 kernels-construct
5088 data-construct
5089 loop-construct
5091 parallel-construct:
5092 parallel-directive structured-block
5094 kernels-construct:
5095 kernels-directive structured-block
5097 data-construct:
5098 data-directive structured-block
5100 loop-construct:
5101 loop-directive structured-block
5103 OpenMP:
5105 statement:
5106 openmp-construct
5108 openmp-construct:
5109 parallel-construct
5110 for-construct
5111 simd-construct
5112 for-simd-construct
5113 sections-construct
5114 single-construct
5115 parallel-for-construct
5116 parallel-for-simd-construct
5117 parallel-sections-construct
5118 master-construct
5119 critical-construct
5120 atomic-construct
5121 ordered-construct
5123 parallel-construct:
5124 parallel-directive structured-block
5126 for-construct:
5127 for-directive iteration-statement
5129 simd-construct:
5130 simd-directive iteration-statements
5132 for-simd-construct:
5133 for-simd-directive iteration-statements
5135 sections-construct:
5136 sections-directive section-scope
5138 single-construct:
5139 single-directive structured-block
5141 parallel-for-construct:
5142 parallel-for-directive iteration-statement
5144 parallel-for-simd-construct:
5145 parallel-for-simd-directive iteration-statement
5147 parallel-sections-construct:
5148 parallel-sections-directive section-scope
5150 master-construct:
5151 master-directive structured-block
5153 critical-construct:
5154 critical-directive structured-block
5156 atomic-construct:
5157 atomic-directive expression-statement
5159 ordered-construct:
5160 ordered-directive structured-block
5162 Transactional Memory:
5164 statement:
5165 transaction-statement
5166 transaction-cancel-statement
5168 IF_P is used to track whether there's a (possibly labeled) if statement
5169 which is not enclosed in braces and has an else clause. This is used to
5170 implement -Wparentheses. */
5172 static void
5173 c_parser_statement (c_parser *parser, bool *if_p)
5175 c_parser_all_labels (parser);
5176 c_parser_statement_after_labels (parser, if_p, NULL);
5179 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5180 of if-else-if conditions.
5182 IF_P is used to track whether there's a (possibly labeled) if statement
5183 which is not enclosed in braces and has an else clause. This is used to
5184 implement -Wparentheses. */
5186 static void
5187 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5188 vec<tree> *chain)
5190 location_t loc = c_parser_peek_token (parser)->location;
5191 tree stmt = NULL_TREE;
5192 bool in_if_block = parser->in_if_block;
5193 parser->in_if_block = false;
5194 if (if_p != NULL)
5195 *if_p = false;
5196 switch (c_parser_peek_token (parser)->type)
5198 case CPP_OPEN_BRACE:
5199 add_stmt (c_parser_compound_statement (parser));
5200 break;
5201 case CPP_KEYWORD:
5202 switch (c_parser_peek_token (parser)->keyword)
5204 case RID_IF:
5205 c_parser_if_statement (parser, if_p, chain);
5206 break;
5207 case RID_SWITCH:
5208 c_parser_switch_statement (parser, if_p);
5209 break;
5210 case RID_WHILE:
5211 c_parser_while_statement (parser, false, if_p);
5212 break;
5213 case RID_DO:
5214 c_parser_do_statement (parser, false);
5215 break;
5216 case RID_FOR:
5217 c_parser_for_statement (parser, false, if_p);
5218 break;
5219 case RID_CILK_FOR:
5220 if (!flag_cilkplus)
5222 error_at (c_parser_peek_token (parser)->location,
5223 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5224 c_parser_skip_to_end_of_block_or_statement (parser);
5226 else
5227 c_parser_cilk_for (parser, integer_zero_node, if_p);
5228 break;
5229 case RID_CILK_SYNC:
5230 c_parser_consume_token (parser);
5231 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5232 if (!flag_cilkplus)
5233 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5234 else
5235 add_stmt (build_cilk_sync ());
5236 break;
5237 case RID_GOTO:
5238 c_parser_consume_token (parser);
5239 if (c_parser_next_token_is (parser, CPP_NAME))
5241 stmt = c_finish_goto_label (loc,
5242 c_parser_peek_token (parser)->value);
5243 c_parser_consume_token (parser);
5245 else if (c_parser_next_token_is (parser, CPP_MULT))
5247 struct c_expr val;
5249 c_parser_consume_token (parser);
5250 val = c_parser_expression (parser);
5251 if (check_no_cilk (val.value,
5252 "Cilk array notation cannot be used as a computed goto expression",
5253 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5254 loc))
5255 val.value = error_mark_node;
5256 val = convert_lvalue_to_rvalue (loc, val, false, true);
5257 stmt = c_finish_goto_ptr (loc, val.value);
5259 else
5260 c_parser_error (parser, "expected identifier or %<*%>");
5261 goto expect_semicolon;
5262 case RID_CONTINUE:
5263 c_parser_consume_token (parser);
5264 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5265 goto expect_semicolon;
5266 case RID_BREAK:
5267 c_parser_consume_token (parser);
5268 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5269 goto expect_semicolon;
5270 case RID_RETURN:
5271 c_parser_consume_token (parser);
5272 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5274 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5275 c_parser_consume_token (parser);
5277 else
5279 location_t xloc = c_parser_peek_token (parser)->location;
5280 struct c_expr expr = c_parser_expression_conv (parser);
5281 mark_exp_read (expr.value);
5282 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5283 expr.value, expr.original_type);
5284 goto expect_semicolon;
5286 break;
5287 case RID_ASM:
5288 stmt = c_parser_asm_statement (parser);
5289 break;
5290 case RID_TRANSACTION_ATOMIC:
5291 case RID_TRANSACTION_RELAXED:
5292 stmt = c_parser_transaction (parser,
5293 c_parser_peek_token (parser)->keyword);
5294 break;
5295 case RID_TRANSACTION_CANCEL:
5296 stmt = c_parser_transaction_cancel (parser);
5297 goto expect_semicolon;
5298 case RID_AT_THROW:
5299 gcc_assert (c_dialect_objc ());
5300 c_parser_consume_token (parser);
5301 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5303 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5304 c_parser_consume_token (parser);
5306 else
5308 struct c_expr expr = c_parser_expression (parser);
5309 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5310 if (check_no_cilk (expr.value,
5311 "Cilk array notation cannot be used for a throw expression",
5312 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5313 expr.value = error_mark_node;
5314 else
5316 expr.value = c_fully_fold (expr.value, false, NULL);
5317 stmt = objc_build_throw_stmt (loc, expr.value);
5319 goto expect_semicolon;
5321 break;
5322 case RID_AT_TRY:
5323 gcc_assert (c_dialect_objc ());
5324 c_parser_objc_try_catch_finally_statement (parser);
5325 break;
5326 case RID_AT_SYNCHRONIZED:
5327 gcc_assert (c_dialect_objc ());
5328 c_parser_objc_synchronized_statement (parser);
5329 break;
5330 default:
5331 goto expr_stmt;
5333 break;
5334 case CPP_SEMICOLON:
5335 c_parser_consume_token (parser);
5336 break;
5337 case CPP_CLOSE_PAREN:
5338 case CPP_CLOSE_SQUARE:
5339 /* Avoid infinite loop in error recovery:
5340 c_parser_skip_until_found stops at a closing nesting
5341 delimiter without consuming it, but here we need to consume
5342 it to proceed further. */
5343 c_parser_error (parser, "expected statement");
5344 c_parser_consume_token (parser);
5345 break;
5346 case CPP_PRAGMA:
5347 c_parser_pragma (parser, pragma_stmt, if_p);
5348 break;
5349 default:
5350 expr_stmt:
5351 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5352 expect_semicolon:
5353 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5354 break;
5356 /* Two cases cannot and do not have line numbers associated: If stmt
5357 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5358 cannot hold line numbers. But that's OK because the statement
5359 will either be changed to a MODIFY_EXPR during gimplification of
5360 the statement expr, or discarded. If stmt was compound, but
5361 without new variables, we will have skipped the creation of a
5362 BIND and will have a bare STATEMENT_LIST. But that's OK because
5363 (recursively) all of the component statements should already have
5364 line numbers assigned. ??? Can we discard no-op statements
5365 earlier? */
5366 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5367 protected_set_expr_location (stmt, loc);
5369 parser->in_if_block = in_if_block;
5372 /* Parse the condition from an if, do, while or for statements. */
5374 static tree
5375 c_parser_condition (c_parser *parser)
5377 location_t loc = c_parser_peek_token (parser)->location;
5378 tree cond;
5379 cond = c_parser_expression_conv (parser).value;
5380 cond = c_objc_common_truthvalue_conversion (loc, cond);
5381 cond = c_fully_fold (cond, false, NULL);
5382 if (warn_sequence_point)
5383 verify_sequence_points (cond);
5384 return cond;
5387 /* Parse a parenthesized condition from an if, do or while statement.
5389 condition:
5390 ( expression )
5392 static tree
5393 c_parser_paren_condition (c_parser *parser)
5395 tree cond;
5396 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5397 return error_mark_node;
5398 cond = c_parser_condition (parser);
5399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5400 return cond;
5403 /* Parse a statement which is a block in C99.
5405 IF_P is used to track whether there's a (possibly labeled) if statement
5406 which is not enclosed in braces and has an else clause. This is used to
5407 implement -Wparentheses. */
5409 static tree
5410 c_parser_c99_block_statement (c_parser *parser, bool *if_p)
5412 tree block = c_begin_compound_stmt (flag_isoc99);
5413 location_t loc = c_parser_peek_token (parser)->location;
5414 c_parser_statement (parser, if_p);
5415 return c_end_compound_stmt (loc, block, flag_isoc99);
5418 /* Parse the body of an if statement. This is just parsing a
5419 statement but (a) it is a block in C99, (b) we track whether the
5420 body is an if statement for the sake of -Wparentheses warnings, (c)
5421 we handle an empty body specially for the sake of -Wempty-body
5422 warnings, and (d) we call parser_compound_statement directly
5423 because c_parser_statement_after_labels resets
5424 parser->in_if_block.
5426 IF_P is used to track whether there's a (possibly labeled) if statement
5427 which is not enclosed in braces and has an else clause. This is used to
5428 implement -Wparentheses. */
5430 static tree
5431 c_parser_if_body (c_parser *parser, bool *if_p,
5432 const token_indent_info &if_tinfo)
5434 tree block = c_begin_compound_stmt (flag_isoc99);
5435 location_t body_loc = c_parser_peek_token (parser)->location;
5436 token_indent_info body_tinfo
5437 = get_token_indent_info (c_parser_peek_token (parser));
5439 c_parser_all_labels (parser);
5440 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5442 location_t loc = c_parser_peek_token (parser)->location;
5443 add_stmt (build_empty_stmt (loc));
5444 c_parser_consume_token (parser);
5445 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5446 warning_at (loc, OPT_Wempty_body,
5447 "suggest braces around empty body in an %<if%> statement");
5449 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5450 add_stmt (c_parser_compound_statement (parser));
5451 else
5452 c_parser_statement_after_labels (parser, if_p);
5454 token_indent_info next_tinfo
5455 = get_token_indent_info (c_parser_peek_token (parser));
5456 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5458 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5461 /* Parse the else body of an if statement. This is just parsing a
5462 statement but (a) it is a block in C99, (b) we handle an empty body
5463 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5464 of if-else-if conditions. */
5466 static tree
5467 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5468 vec<tree> *chain)
5470 location_t body_loc = c_parser_peek_token (parser)->location;
5471 tree block = c_begin_compound_stmt (flag_isoc99);
5472 token_indent_info body_tinfo
5473 = get_token_indent_info (c_parser_peek_token (parser));
5475 c_parser_all_labels (parser);
5476 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5478 location_t loc = c_parser_peek_token (parser)->location;
5479 warning_at (loc,
5480 OPT_Wempty_body,
5481 "suggest braces around empty body in an %<else%> statement");
5482 add_stmt (build_empty_stmt (loc));
5483 c_parser_consume_token (parser);
5485 else
5486 c_parser_statement_after_labels (parser, NULL, chain);
5488 token_indent_info next_tinfo
5489 = get_token_indent_info (c_parser_peek_token (parser));
5490 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5492 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5495 /* We might need to reclassify any previously-lexed identifier, e.g.
5496 when we've left a for loop with an if-statement without else in the
5497 body - we might have used a wrong scope for the token. See PR67784. */
5499 static void
5500 c_parser_maybe_reclassify_token (c_parser *parser)
5502 if (c_parser_next_token_is (parser, CPP_NAME))
5504 c_token *token = c_parser_peek_token (parser);
5506 if (token->id_kind != C_ID_CLASSNAME)
5508 tree decl = lookup_name (token->value);
5510 token->id_kind = C_ID_ID;
5511 if (decl)
5513 if (TREE_CODE (decl) == TYPE_DECL)
5514 token->id_kind = C_ID_TYPENAME;
5516 else if (c_dialect_objc ())
5518 tree objc_interface_decl = objc_is_class_name (token->value);
5519 /* Objective-C class names are in the same namespace as
5520 variables and typedefs, and hence are shadowed by local
5521 declarations. */
5522 if (objc_interface_decl)
5524 token->value = objc_interface_decl;
5525 token->id_kind = C_ID_CLASSNAME;
5532 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5534 if-statement:
5535 if ( expression ) statement
5536 if ( expression ) statement else statement
5538 CHAIN is a vector of if-else-if conditions.
5539 IF_P is used to track whether there's a (possibly labeled) if statement
5540 which is not enclosed in braces and has an else clause. This is used to
5541 implement -Wparentheses. */
5543 static void
5544 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5546 tree block;
5547 location_t loc;
5548 tree cond;
5549 bool nested_if = false;
5550 tree first_body, second_body;
5551 bool in_if_block;
5552 tree if_stmt;
5554 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5555 token_indent_info if_tinfo
5556 = get_token_indent_info (c_parser_peek_token (parser));
5557 c_parser_consume_token (parser);
5558 block = c_begin_compound_stmt (flag_isoc99);
5559 loc = c_parser_peek_token (parser)->location;
5560 cond = c_parser_paren_condition (parser);
5561 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5563 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5564 cond = error_mark_node;
5566 in_if_block = parser->in_if_block;
5567 parser->in_if_block = true;
5568 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5569 parser->in_if_block = in_if_block;
5571 if (warn_duplicated_cond)
5572 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5574 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5576 token_indent_info else_tinfo
5577 = get_token_indent_info (c_parser_peek_token (parser));
5578 c_parser_consume_token (parser);
5579 if (warn_duplicated_cond)
5581 if (c_parser_next_token_is_keyword (parser, RID_IF)
5582 && chain == NULL)
5584 /* We've got "if (COND) else if (COND2)". Start the
5585 condition chain and add COND as the first element. */
5586 chain = new vec<tree> ();
5587 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5588 chain->safe_push (cond);
5590 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5592 /* This is if-else without subsequent if. Zap the condition
5593 chain; we would have already warned at this point. */
5594 delete chain;
5595 chain = NULL;
5598 second_body = c_parser_else_body (parser, else_tinfo, chain);
5599 /* Set IF_P to true to indicate that this if statement has an
5600 else clause. This may trigger the Wparentheses warning
5601 below when we get back up to the parent if statement. */
5602 if (if_p != NULL)
5603 *if_p = true;
5605 else
5607 second_body = NULL_TREE;
5609 /* Diagnose an ambiguous else if if-then-else is nested inside
5610 if-then. */
5611 if (nested_if)
5612 warning_at (loc, OPT_Wdangling_else,
5613 "suggest explicit braces to avoid ambiguous %<else%>");
5615 if (warn_duplicated_cond)
5617 /* This if statement does not have an else clause. We don't
5618 need the condition chain anymore. */
5619 delete chain;
5620 chain = NULL;
5623 c_finish_if_stmt (loc, cond, first_body, second_body);
5624 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5626 /* If the if statement contains array notations, then we expand them. */
5627 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5628 if_stmt = fix_conditional_array_notations (if_stmt);
5629 add_stmt (if_stmt);
5630 c_parser_maybe_reclassify_token (parser);
5633 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5635 switch-statement:
5636 switch (expression) statement
5639 static void
5640 c_parser_switch_statement (c_parser *parser, bool *if_p)
5642 struct c_expr ce;
5643 tree block, expr, body, save_break;
5644 location_t switch_loc = c_parser_peek_token (parser)->location;
5645 location_t switch_cond_loc;
5646 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5647 c_parser_consume_token (parser);
5648 block = c_begin_compound_stmt (flag_isoc99);
5649 bool explicit_cast_p = false;
5650 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5652 switch_cond_loc = c_parser_peek_token (parser)->location;
5653 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5654 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5655 explicit_cast_p = true;
5656 ce = c_parser_expression (parser);
5657 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5658 expr = ce.value;
5659 /* ??? expr has no valid location? */
5660 if (check_no_cilk (expr,
5661 "Cilk array notation cannot be used as a condition for switch statement",
5662 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5663 switch_cond_loc))
5664 expr = error_mark_node;
5665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5667 else
5669 switch_cond_loc = UNKNOWN_LOCATION;
5670 expr = error_mark_node;
5671 ce.original_type = error_mark_node;
5673 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5674 save_break = c_break_label;
5675 c_break_label = NULL_TREE;
5676 body = c_parser_c99_block_statement (parser, if_p);
5677 c_finish_case (body, ce.original_type);
5678 if (c_break_label)
5680 location_t here = c_parser_peek_token (parser)->location;
5681 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5682 SET_EXPR_LOCATION (t, here);
5683 add_stmt (t);
5685 c_break_label = save_break;
5686 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5687 c_parser_maybe_reclassify_token (parser);
5690 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5692 while-statement:
5693 while (expression) statement
5695 IF_P is used to track whether there's a (possibly labeled) if statement
5696 which is not enclosed in braces and has an else clause. This is used to
5697 implement -Wparentheses. */
5699 static void
5700 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5702 tree block, cond, body, save_break, save_cont;
5703 location_t loc;
5704 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5705 token_indent_info while_tinfo
5706 = get_token_indent_info (c_parser_peek_token (parser));
5707 c_parser_consume_token (parser);
5708 block = c_begin_compound_stmt (flag_isoc99);
5709 loc = c_parser_peek_token (parser)->location;
5710 cond = c_parser_paren_condition (parser);
5711 if (check_no_cilk (cond,
5712 "Cilk array notation cannot be used as a condition for while statement",
5713 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5714 cond = error_mark_node;
5715 if (ivdep && cond != error_mark_node)
5716 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5717 build_int_cst (integer_type_node,
5718 annot_expr_ivdep_kind));
5719 save_break = c_break_label;
5720 c_break_label = NULL_TREE;
5721 save_cont = c_cont_label;
5722 c_cont_label = NULL_TREE;
5724 token_indent_info body_tinfo
5725 = get_token_indent_info (c_parser_peek_token (parser));
5727 body = c_parser_c99_block_statement (parser, if_p);
5728 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5729 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5730 c_parser_maybe_reclassify_token (parser);
5732 token_indent_info next_tinfo
5733 = get_token_indent_info (c_parser_peek_token (parser));
5734 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5736 c_break_label = save_break;
5737 c_cont_label = save_cont;
5740 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5742 do-statement:
5743 do statement while ( expression ) ;
5746 static void
5747 c_parser_do_statement (c_parser *parser, bool ivdep)
5749 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5750 location_t loc;
5751 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5752 c_parser_consume_token (parser);
5753 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5754 warning_at (c_parser_peek_token (parser)->location,
5755 OPT_Wempty_body,
5756 "suggest braces around empty body in %<do%> statement");
5757 block = c_begin_compound_stmt (flag_isoc99);
5758 loc = c_parser_peek_token (parser)->location;
5759 save_break = c_break_label;
5760 c_break_label = NULL_TREE;
5761 save_cont = c_cont_label;
5762 c_cont_label = NULL_TREE;
5763 body = c_parser_c99_block_statement (parser, NULL);
5764 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5765 new_break = c_break_label;
5766 c_break_label = save_break;
5767 new_cont = c_cont_label;
5768 c_cont_label = save_cont;
5769 cond = c_parser_paren_condition (parser);
5770 if (check_no_cilk (cond,
5771 "Cilk array notation cannot be used as a condition for a do-while statement",
5772 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5773 cond = error_mark_node;
5774 if (ivdep && cond != error_mark_node)
5775 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5776 build_int_cst (integer_type_node,
5777 annot_expr_ivdep_kind));
5778 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5779 c_parser_skip_to_end_of_block_or_statement (parser);
5780 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5781 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5784 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5786 for-statement:
5787 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5788 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5790 The form with a declaration is new in C99.
5792 ??? In accordance with the old parser, the declaration may be a
5793 nested function, which is then rejected in check_for_loop_decls,
5794 but does it make any sense for this to be included in the grammar?
5795 Note in particular that the nested function does not include a
5796 trailing ';', whereas the "declaration" production includes one.
5797 Also, can we reject bad declarations earlier and cheaper than
5798 check_for_loop_decls?
5800 In Objective-C, there are two additional variants:
5802 foreach-statement:
5803 for ( expression in expresssion ) statement
5804 for ( declaration in expression ) statement
5806 This is inconsistent with C, because the second variant is allowed
5807 even if c99 is not enabled.
5809 The rest of the comment documents these Objective-C foreach-statement.
5811 Here is the canonical example of the first variant:
5812 for (object in array) { do something with object }
5813 we call the first expression ("object") the "object_expression" and
5814 the second expression ("array") the "collection_expression".
5815 object_expression must be an lvalue of type "id" (a generic Objective-C
5816 object) because the loop works by assigning to object_expression the
5817 various objects from the collection_expression. collection_expression
5818 must evaluate to something of type "id" which responds to the method
5819 countByEnumeratingWithState:objects:count:.
5821 The canonical example of the second variant is:
5822 for (id object in array) { do something with object }
5823 which is completely equivalent to
5825 id object;
5826 for (object in array) { do something with object }
5828 Note that initizializing 'object' in some way (eg, "for ((object =
5829 xxx) in array) { do something with object }") is possibly
5830 technically valid, but completely pointless as 'object' will be
5831 assigned to something else as soon as the loop starts. We should
5832 most likely reject it (TODO).
5834 The beginning of the Objective-C foreach-statement looks exactly
5835 like the beginning of the for-statement, and we can tell it is a
5836 foreach-statement only because the initial declaration or
5837 expression is terminated by 'in' instead of ';'.
5839 IF_P is used to track whether there's a (possibly labeled) if statement
5840 which is not enclosed in braces and has an else clause. This is used to
5841 implement -Wparentheses. */
5843 static void
5844 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
5846 tree block, cond, incr, save_break, save_cont, body;
5847 /* The following are only used when parsing an ObjC foreach statement. */
5848 tree object_expression;
5849 /* Silence the bogus uninitialized warning. */
5850 tree collection_expression = NULL;
5851 location_t loc = c_parser_peek_token (parser)->location;
5852 location_t for_loc = c_parser_peek_token (parser)->location;
5853 bool is_foreach_statement = false;
5854 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5855 token_indent_info for_tinfo
5856 = get_token_indent_info (c_parser_peek_token (parser));
5857 c_parser_consume_token (parser);
5858 /* Open a compound statement in Objective-C as well, just in case this is
5859 as foreach expression. */
5860 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5861 cond = error_mark_node;
5862 incr = error_mark_node;
5863 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5865 /* Parse the initialization declaration or expression. */
5866 object_expression = error_mark_node;
5867 parser->objc_could_be_foreach_context = c_dialect_objc ();
5868 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5870 parser->objc_could_be_foreach_context = false;
5871 c_parser_consume_token (parser);
5872 c_finish_expr_stmt (loc, NULL_TREE);
5874 else if (c_parser_next_tokens_start_declaration (parser))
5876 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5877 &object_expression, vNULL);
5878 parser->objc_could_be_foreach_context = false;
5880 if (c_parser_next_token_is_keyword (parser, RID_IN))
5882 c_parser_consume_token (parser);
5883 is_foreach_statement = true;
5884 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5885 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5887 else
5888 check_for_loop_decls (for_loc, flag_isoc99);
5890 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5892 /* __extension__ can start a declaration, but is also an
5893 unary operator that can start an expression. Consume all
5894 but the last of a possible series of __extension__ to
5895 determine which. */
5896 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5897 && (c_parser_peek_2nd_token (parser)->keyword
5898 == RID_EXTENSION))
5899 c_parser_consume_token (parser);
5900 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5902 int ext;
5903 ext = disable_extension_diagnostics ();
5904 c_parser_consume_token (parser);
5905 c_parser_declaration_or_fndef (parser, true, true, true, true,
5906 true, &object_expression, vNULL);
5907 parser->objc_could_be_foreach_context = false;
5909 restore_extension_diagnostics (ext);
5910 if (c_parser_next_token_is_keyword (parser, RID_IN))
5912 c_parser_consume_token (parser);
5913 is_foreach_statement = true;
5914 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5915 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5917 else
5918 check_for_loop_decls (for_loc, flag_isoc99);
5920 else
5921 goto init_expr;
5923 else
5925 init_expr:
5927 struct c_expr ce;
5928 tree init_expression;
5929 ce = c_parser_expression (parser);
5930 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5931 level statement", but it works just fine, so allow it. */
5932 init_expression = ce.value;
5933 parser->objc_could_be_foreach_context = false;
5934 if (c_parser_next_token_is_keyword (parser, RID_IN))
5936 c_parser_consume_token (parser);
5937 is_foreach_statement = true;
5938 if (! lvalue_p (init_expression))
5939 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5940 object_expression = c_fully_fold (init_expression, false, NULL);
5942 else
5944 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5945 init_expression = ce.value;
5946 c_finish_expr_stmt (loc, init_expression);
5947 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5951 /* Parse the loop condition. In the case of a foreach
5952 statement, there is no loop condition. */
5953 gcc_assert (!parser->objc_could_be_foreach_context);
5954 if (!is_foreach_statement)
5956 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5958 if (ivdep)
5960 c_parser_error (parser, "missing loop condition in loop with "
5961 "%<GCC ivdep%> pragma");
5962 cond = error_mark_node;
5964 else
5966 c_parser_consume_token (parser);
5967 cond = NULL_TREE;
5970 else
5972 cond = c_parser_condition (parser);
5973 if (check_no_cilk (cond,
5974 "Cilk array notation cannot be used in a condition for a for-loop",
5975 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5976 cond = error_mark_node;
5977 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5978 "expected %<;%>");
5980 if (ivdep && cond != error_mark_node)
5981 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5982 build_int_cst (integer_type_node,
5983 annot_expr_ivdep_kind));
5985 /* Parse the increment expression (the third expression in a
5986 for-statement). In the case of a foreach-statement, this is
5987 the expression that follows the 'in'. */
5988 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5990 if (is_foreach_statement)
5992 c_parser_error (parser, "missing collection in fast enumeration");
5993 collection_expression = error_mark_node;
5995 else
5996 incr = c_process_expr_stmt (loc, NULL_TREE);
5998 else
6000 if (is_foreach_statement)
6001 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6002 false, NULL);
6003 else
6005 struct c_expr ce = c_parser_expression (parser);
6006 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6007 incr = c_process_expr_stmt (loc, ce.value);
6010 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6012 save_break = c_break_label;
6013 c_break_label = NULL_TREE;
6014 save_cont = c_cont_label;
6015 c_cont_label = NULL_TREE;
6017 token_indent_info body_tinfo
6018 = get_token_indent_info (c_parser_peek_token (parser));
6020 body = c_parser_c99_block_statement (parser, if_p);
6022 if (is_foreach_statement)
6023 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6024 else
6025 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6026 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6027 c_parser_maybe_reclassify_token (parser);
6029 token_indent_info next_tinfo
6030 = get_token_indent_info (c_parser_peek_token (parser));
6031 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6033 c_break_label = save_break;
6034 c_cont_label = save_cont;
6037 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6038 statement with inputs, outputs, clobbers, and volatile tag
6039 allowed.
6041 asm-statement:
6042 asm type-qualifier[opt] ( asm-argument ) ;
6043 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6045 asm-argument:
6046 asm-string-literal
6047 asm-string-literal : asm-operands[opt]
6048 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6049 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6051 asm-goto-argument:
6052 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6053 : asm-goto-operands
6055 Qualifiers other than volatile are accepted in the syntax but
6056 warned for. */
6058 static tree
6059 c_parser_asm_statement (c_parser *parser)
6061 tree quals, str, outputs, inputs, clobbers, labels, ret;
6062 bool simple, is_goto;
6063 location_t asm_loc = c_parser_peek_token (parser)->location;
6064 int section, nsections;
6066 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6067 c_parser_consume_token (parser);
6068 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6070 quals = c_parser_peek_token (parser)->value;
6071 c_parser_consume_token (parser);
6073 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6074 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6076 warning_at (c_parser_peek_token (parser)->location,
6078 "%E qualifier ignored on asm",
6079 c_parser_peek_token (parser)->value);
6080 quals = NULL_TREE;
6081 c_parser_consume_token (parser);
6083 else
6084 quals = NULL_TREE;
6086 is_goto = false;
6087 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6089 c_parser_consume_token (parser);
6090 is_goto = true;
6093 /* ??? Follow the C++ parser rather than using the
6094 lex_untranslated_string kludge. */
6095 parser->lex_untranslated_string = true;
6096 ret = NULL;
6098 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6099 goto error;
6101 str = c_parser_asm_string_literal (parser);
6102 if (str == NULL_TREE)
6103 goto error_close_paren;
6105 simple = true;
6106 outputs = NULL_TREE;
6107 inputs = NULL_TREE;
6108 clobbers = NULL_TREE;
6109 labels = NULL_TREE;
6111 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6112 goto done_asm;
6114 /* Parse each colon-delimited section of operands. */
6115 nsections = 3 + is_goto;
6116 for (section = 0; section < nsections; ++section)
6118 if (!c_parser_require (parser, CPP_COLON,
6119 is_goto
6120 ? "expected %<:%>"
6121 : "expected %<:%> or %<)%>"))
6122 goto error_close_paren;
6124 /* Once past any colon, we're no longer a simple asm. */
6125 simple = false;
6127 if ((!c_parser_next_token_is (parser, CPP_COLON)
6128 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6129 || section == 3)
6130 switch (section)
6132 case 0:
6133 /* For asm goto, we don't allow output operands, but reserve
6134 the slot for a future extension that does allow them. */
6135 if (!is_goto)
6136 outputs = c_parser_asm_operands (parser);
6137 break;
6138 case 1:
6139 inputs = c_parser_asm_operands (parser);
6140 break;
6141 case 2:
6142 clobbers = c_parser_asm_clobbers (parser);
6143 break;
6144 case 3:
6145 labels = c_parser_asm_goto_operands (parser);
6146 break;
6147 default:
6148 gcc_unreachable ();
6151 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6152 goto done_asm;
6155 done_asm:
6156 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6159 goto error;
6162 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6163 c_parser_skip_to_end_of_block_or_statement (parser);
6165 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6166 clobbers, labels, simple));
6168 error:
6169 parser->lex_untranslated_string = false;
6170 return ret;
6172 error_close_paren:
6173 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6174 goto error;
6177 /* Parse asm operands, a GNU extension.
6179 asm-operands:
6180 asm-operand
6181 asm-operands , asm-operand
6183 asm-operand:
6184 asm-string-literal ( expression )
6185 [ identifier ] asm-string-literal ( expression )
6188 static tree
6189 c_parser_asm_operands (c_parser *parser)
6191 tree list = NULL_TREE;
6192 while (true)
6194 tree name, str;
6195 struct c_expr expr;
6196 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6198 c_parser_consume_token (parser);
6199 if (c_parser_next_token_is (parser, CPP_NAME))
6201 tree id = c_parser_peek_token (parser)->value;
6202 c_parser_consume_token (parser);
6203 name = build_string (IDENTIFIER_LENGTH (id),
6204 IDENTIFIER_POINTER (id));
6206 else
6208 c_parser_error (parser, "expected identifier");
6209 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6210 return NULL_TREE;
6212 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6213 "expected %<]%>");
6215 else
6216 name = NULL_TREE;
6217 str = c_parser_asm_string_literal (parser);
6218 if (str == NULL_TREE)
6219 return NULL_TREE;
6220 parser->lex_untranslated_string = false;
6221 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6223 parser->lex_untranslated_string = true;
6224 return NULL_TREE;
6226 expr = c_parser_expression (parser);
6227 mark_exp_read (expr.value);
6228 parser->lex_untranslated_string = true;
6229 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6232 return NULL_TREE;
6234 list = chainon (list, build_tree_list (build_tree_list (name, str),
6235 expr.value));
6236 if (c_parser_next_token_is (parser, CPP_COMMA))
6237 c_parser_consume_token (parser);
6238 else
6239 break;
6241 return list;
6244 /* Parse asm clobbers, a GNU extension.
6246 asm-clobbers:
6247 asm-string-literal
6248 asm-clobbers , asm-string-literal
6251 static tree
6252 c_parser_asm_clobbers (c_parser *parser)
6254 tree list = NULL_TREE;
6255 while (true)
6257 tree str = c_parser_asm_string_literal (parser);
6258 if (str)
6259 list = tree_cons (NULL_TREE, str, list);
6260 else
6261 return NULL_TREE;
6262 if (c_parser_next_token_is (parser, CPP_COMMA))
6263 c_parser_consume_token (parser);
6264 else
6265 break;
6267 return list;
6270 /* Parse asm goto labels, a GNU extension.
6272 asm-goto-operands:
6273 identifier
6274 asm-goto-operands , identifier
6277 static tree
6278 c_parser_asm_goto_operands (c_parser *parser)
6280 tree list = NULL_TREE;
6281 while (true)
6283 tree name, label;
6285 if (c_parser_next_token_is (parser, CPP_NAME))
6287 c_token *tok = c_parser_peek_token (parser);
6288 name = tok->value;
6289 label = lookup_label_for_goto (tok->location, name);
6290 c_parser_consume_token (parser);
6291 TREE_USED (label) = 1;
6293 else
6295 c_parser_error (parser, "expected identifier");
6296 return NULL_TREE;
6299 name = build_string (IDENTIFIER_LENGTH (name),
6300 IDENTIFIER_POINTER (name));
6301 list = tree_cons (name, label, list);
6302 if (c_parser_next_token_is (parser, CPP_COMMA))
6303 c_parser_consume_token (parser);
6304 else
6305 return nreverse (list);
6309 /* Parse an expression other than a compound expression; that is, an
6310 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6311 NULL then it is an Objective-C message expression which is the
6312 primary-expression starting the expression as an initializer.
6314 assignment-expression:
6315 conditional-expression
6316 unary-expression assignment-operator assignment-expression
6318 assignment-operator: one of
6319 = *= /= %= += -= <<= >>= &= ^= |=
6321 In GNU C we accept any conditional expression on the LHS and
6322 diagnose the invalid lvalue rather than producing a syntax
6323 error. */
6325 static struct c_expr
6326 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6327 tree omp_atomic_lhs)
6329 struct c_expr lhs, rhs, ret;
6330 enum tree_code code;
6331 location_t op_location, exp_location;
6332 gcc_assert (!after || c_dialect_objc ());
6333 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6334 op_location = c_parser_peek_token (parser)->location;
6335 switch (c_parser_peek_token (parser)->type)
6337 case CPP_EQ:
6338 code = NOP_EXPR;
6339 break;
6340 case CPP_MULT_EQ:
6341 code = MULT_EXPR;
6342 break;
6343 case CPP_DIV_EQ:
6344 code = TRUNC_DIV_EXPR;
6345 break;
6346 case CPP_MOD_EQ:
6347 code = TRUNC_MOD_EXPR;
6348 break;
6349 case CPP_PLUS_EQ:
6350 code = PLUS_EXPR;
6351 break;
6352 case CPP_MINUS_EQ:
6353 code = MINUS_EXPR;
6354 break;
6355 case CPP_LSHIFT_EQ:
6356 code = LSHIFT_EXPR;
6357 break;
6358 case CPP_RSHIFT_EQ:
6359 code = RSHIFT_EXPR;
6360 break;
6361 case CPP_AND_EQ:
6362 code = BIT_AND_EXPR;
6363 break;
6364 case CPP_XOR_EQ:
6365 code = BIT_XOR_EXPR;
6366 break;
6367 case CPP_OR_EQ:
6368 code = BIT_IOR_EXPR;
6369 break;
6370 default:
6371 return lhs;
6373 c_parser_consume_token (parser);
6374 exp_location = c_parser_peek_token (parser)->location;
6375 rhs = c_parser_expr_no_commas (parser, NULL);
6376 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6378 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6379 code, exp_location, rhs.value,
6380 rhs.original_type);
6381 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6382 if (code == NOP_EXPR)
6383 ret.original_code = MODIFY_EXPR;
6384 else
6386 TREE_NO_WARNING (ret.value) = 1;
6387 ret.original_code = ERROR_MARK;
6389 ret.original_type = NULL;
6390 return ret;
6393 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6394 is not NULL then it is an Objective-C message expression which is
6395 the primary-expression starting the expression as an initializer.
6397 conditional-expression:
6398 logical-OR-expression
6399 logical-OR-expression ? expression : conditional-expression
6401 GNU extensions:
6403 conditional-expression:
6404 logical-OR-expression ? : conditional-expression
6407 static struct c_expr
6408 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6409 tree omp_atomic_lhs)
6411 struct c_expr cond, exp1, exp2, ret;
6412 location_t start, cond_loc, colon_loc, middle_loc;
6414 gcc_assert (!after || c_dialect_objc ());
6416 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6418 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6419 return cond;
6420 if (cond.value != error_mark_node)
6421 start = cond.get_start ();
6422 else
6423 start = UNKNOWN_LOCATION;
6424 cond_loc = c_parser_peek_token (parser)->location;
6425 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6426 c_parser_consume_token (parser);
6427 if (c_parser_next_token_is (parser, CPP_COLON))
6429 tree eptype = NULL_TREE;
6431 middle_loc = c_parser_peek_token (parser)->location;
6432 pedwarn (middle_loc, OPT_Wpedantic,
6433 "ISO C forbids omitting the middle term of a ?: expression");
6434 warn_for_omitted_condop (middle_loc, cond.value);
6435 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6437 eptype = TREE_TYPE (cond.value);
6438 cond.value = TREE_OPERAND (cond.value, 0);
6440 /* Make sure first operand is calculated only once. */
6441 exp1.value = c_save_expr (default_conversion (cond.value));
6442 if (eptype)
6443 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6444 exp1.original_type = NULL;
6445 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6446 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6448 else
6450 cond.value
6451 = c_objc_common_truthvalue_conversion
6452 (cond_loc, default_conversion (cond.value));
6453 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6454 exp1 = c_parser_expression_conv (parser);
6455 mark_exp_read (exp1.value);
6456 c_inhibit_evaluation_warnings +=
6457 ((cond.value == truthvalue_true_node)
6458 - (cond.value == truthvalue_false_node));
6461 colon_loc = c_parser_peek_token (parser)->location;
6462 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6464 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6465 ret.value = error_mark_node;
6466 ret.original_code = ERROR_MARK;
6467 ret.original_type = NULL;
6468 return ret;
6471 location_t exp2_loc = c_parser_peek_token (parser)->location;
6472 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6473 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6475 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6476 ret.value = build_conditional_expr (colon_loc, cond.value,
6477 cond.original_code == C_MAYBE_CONST_EXPR,
6478 exp1.value, exp1.original_type,
6479 exp2.value, exp2.original_type);
6480 ret.original_code = ERROR_MARK;
6481 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6482 ret.original_type = NULL;
6483 else
6485 tree t1, t2;
6487 /* If both sides are enum type, the default conversion will have
6488 made the type of the result be an integer type. We want to
6489 remember the enum types we started with. */
6490 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6491 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6492 ret.original_type = ((t1 != error_mark_node
6493 && t2 != error_mark_node
6494 && (TYPE_MAIN_VARIANT (t1)
6495 == TYPE_MAIN_VARIANT (t2)))
6496 ? t1
6497 : NULL);
6499 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6500 return ret;
6503 /* Parse a binary expression; that is, a logical-OR-expression (C90
6504 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6505 an Objective-C message expression which is the primary-expression
6506 starting the expression as an initializer.
6508 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6509 when it should be the unfolded lhs. In a valid OpenMP source,
6510 one of the operands of the toplevel binary expression must be equal
6511 to it. In that case, just return a build2 created binary operation
6512 rather than result of parser_build_binary_op.
6514 multiplicative-expression:
6515 cast-expression
6516 multiplicative-expression * cast-expression
6517 multiplicative-expression / cast-expression
6518 multiplicative-expression % cast-expression
6520 additive-expression:
6521 multiplicative-expression
6522 additive-expression + multiplicative-expression
6523 additive-expression - multiplicative-expression
6525 shift-expression:
6526 additive-expression
6527 shift-expression << additive-expression
6528 shift-expression >> additive-expression
6530 relational-expression:
6531 shift-expression
6532 relational-expression < shift-expression
6533 relational-expression > shift-expression
6534 relational-expression <= shift-expression
6535 relational-expression >= shift-expression
6537 equality-expression:
6538 relational-expression
6539 equality-expression == relational-expression
6540 equality-expression != relational-expression
6542 AND-expression:
6543 equality-expression
6544 AND-expression & equality-expression
6546 exclusive-OR-expression:
6547 AND-expression
6548 exclusive-OR-expression ^ AND-expression
6550 inclusive-OR-expression:
6551 exclusive-OR-expression
6552 inclusive-OR-expression | exclusive-OR-expression
6554 logical-AND-expression:
6555 inclusive-OR-expression
6556 logical-AND-expression && inclusive-OR-expression
6558 logical-OR-expression:
6559 logical-AND-expression
6560 logical-OR-expression || logical-AND-expression
6563 static struct c_expr
6564 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6565 tree omp_atomic_lhs)
6567 /* A binary expression is parsed using operator-precedence parsing,
6568 with the operands being cast expressions. All the binary
6569 operators are left-associative. Thus a binary expression is of
6570 form:
6572 E0 op1 E1 op2 E2 ...
6574 which we represent on a stack. On the stack, the precedence
6575 levels are strictly increasing. When a new operator is
6576 encountered of higher precedence than that at the top of the
6577 stack, it is pushed; its LHS is the top expression, and its RHS
6578 is everything parsed until it is popped. When a new operator is
6579 encountered with precedence less than or equal to that at the top
6580 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6581 by the result of the operation until the operator at the top of
6582 the stack has lower precedence than the new operator or there is
6583 only one element on the stack; then the top expression is the LHS
6584 of the new operator. In the case of logical AND and OR
6585 expressions, we also need to adjust c_inhibit_evaluation_warnings
6586 as appropriate when the operators are pushed and popped. */
6588 struct {
6589 /* The expression at this stack level. */
6590 struct c_expr expr;
6591 /* The precedence of the operator on its left, PREC_NONE at the
6592 bottom of the stack. */
6593 enum c_parser_prec prec;
6594 /* The operation on its left. */
6595 enum tree_code op;
6596 /* The source location of this operation. */
6597 location_t loc;
6598 } stack[NUM_PRECS];
6599 int sp;
6600 /* Location of the binary operator. */
6601 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6602 #define POP \
6603 do { \
6604 switch (stack[sp].op) \
6606 case TRUTH_ANDIF_EXPR: \
6607 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6608 == truthvalue_false_node); \
6609 break; \
6610 case TRUTH_ORIF_EXPR: \
6611 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6612 == truthvalue_true_node); \
6613 break; \
6614 default: \
6615 break; \
6617 stack[sp - 1].expr \
6618 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6619 stack[sp - 1].expr, true, true); \
6620 stack[sp].expr \
6621 = convert_lvalue_to_rvalue (stack[sp].loc, \
6622 stack[sp].expr, true, true); \
6623 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6624 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6625 && ((1 << stack[sp].prec) \
6626 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6627 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6628 && stack[sp].op != TRUNC_MOD_EXPR \
6629 && stack[0].expr.value != error_mark_node \
6630 && stack[1].expr.value != error_mark_node \
6631 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6632 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6633 stack[0].expr.value \
6634 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6635 stack[0].expr.value, stack[1].expr.value); \
6636 else \
6637 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6638 stack[sp].op, \
6639 stack[sp - 1].expr, \
6640 stack[sp].expr); \
6641 sp--; \
6642 } while (0)
6643 gcc_assert (!after || c_dialect_objc ());
6644 stack[0].loc = c_parser_peek_token (parser)->location;
6645 stack[0].expr = c_parser_cast_expression (parser, after);
6646 stack[0].prec = PREC_NONE;
6647 sp = 0;
6648 while (true)
6650 enum c_parser_prec oprec;
6651 enum tree_code ocode;
6652 source_range src_range;
6653 if (parser->error)
6654 goto out;
6655 switch (c_parser_peek_token (parser)->type)
6657 case CPP_MULT:
6658 oprec = PREC_MULT;
6659 ocode = MULT_EXPR;
6660 break;
6661 case CPP_DIV:
6662 oprec = PREC_MULT;
6663 ocode = TRUNC_DIV_EXPR;
6664 break;
6665 case CPP_MOD:
6666 oprec = PREC_MULT;
6667 ocode = TRUNC_MOD_EXPR;
6668 break;
6669 case CPP_PLUS:
6670 oprec = PREC_ADD;
6671 ocode = PLUS_EXPR;
6672 break;
6673 case CPP_MINUS:
6674 oprec = PREC_ADD;
6675 ocode = MINUS_EXPR;
6676 break;
6677 case CPP_LSHIFT:
6678 oprec = PREC_SHIFT;
6679 ocode = LSHIFT_EXPR;
6680 break;
6681 case CPP_RSHIFT:
6682 oprec = PREC_SHIFT;
6683 ocode = RSHIFT_EXPR;
6684 break;
6685 case CPP_LESS:
6686 oprec = PREC_REL;
6687 ocode = LT_EXPR;
6688 break;
6689 case CPP_GREATER:
6690 oprec = PREC_REL;
6691 ocode = GT_EXPR;
6692 break;
6693 case CPP_LESS_EQ:
6694 oprec = PREC_REL;
6695 ocode = LE_EXPR;
6696 break;
6697 case CPP_GREATER_EQ:
6698 oprec = PREC_REL;
6699 ocode = GE_EXPR;
6700 break;
6701 case CPP_EQ_EQ:
6702 oprec = PREC_EQ;
6703 ocode = EQ_EXPR;
6704 break;
6705 case CPP_NOT_EQ:
6706 oprec = PREC_EQ;
6707 ocode = NE_EXPR;
6708 break;
6709 case CPP_AND:
6710 oprec = PREC_BITAND;
6711 ocode = BIT_AND_EXPR;
6712 break;
6713 case CPP_XOR:
6714 oprec = PREC_BITXOR;
6715 ocode = BIT_XOR_EXPR;
6716 break;
6717 case CPP_OR:
6718 oprec = PREC_BITOR;
6719 ocode = BIT_IOR_EXPR;
6720 break;
6721 case CPP_AND_AND:
6722 oprec = PREC_LOGAND;
6723 ocode = TRUTH_ANDIF_EXPR;
6724 break;
6725 case CPP_OR_OR:
6726 oprec = PREC_LOGOR;
6727 ocode = TRUTH_ORIF_EXPR;
6728 break;
6729 default:
6730 /* Not a binary operator, so end of the binary
6731 expression. */
6732 goto out;
6734 binary_loc = c_parser_peek_token (parser)->location;
6735 while (oprec <= stack[sp].prec)
6736 POP;
6737 c_parser_consume_token (parser);
6738 switch (ocode)
6740 case TRUTH_ANDIF_EXPR:
6741 src_range = stack[sp].expr.src_range;
6742 stack[sp].expr
6743 = convert_lvalue_to_rvalue (stack[sp].loc,
6744 stack[sp].expr, true, true);
6745 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6746 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6747 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6748 == truthvalue_false_node);
6749 set_c_expr_source_range (&stack[sp].expr, src_range);
6750 break;
6751 case TRUTH_ORIF_EXPR:
6752 src_range = stack[sp].expr.src_range;
6753 stack[sp].expr
6754 = convert_lvalue_to_rvalue (stack[sp].loc,
6755 stack[sp].expr, true, true);
6756 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6757 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6758 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6759 == truthvalue_true_node);
6760 set_c_expr_source_range (&stack[sp].expr, src_range);
6761 break;
6762 default:
6763 break;
6765 sp++;
6766 stack[sp].loc = binary_loc;
6767 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6768 stack[sp].prec = oprec;
6769 stack[sp].op = ocode;
6771 out:
6772 while (sp > 0)
6773 POP;
6774 return stack[0].expr;
6775 #undef POP
6778 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6779 NULL then it is an Objective-C message expression which is the
6780 primary-expression starting the expression as an initializer.
6782 cast-expression:
6783 unary-expression
6784 ( type-name ) unary-expression
6787 static struct c_expr
6788 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6790 location_t cast_loc = c_parser_peek_token (parser)->location;
6791 gcc_assert (!after || c_dialect_objc ());
6792 if (after)
6793 return c_parser_postfix_expression_after_primary (parser,
6794 cast_loc, *after);
6795 /* If the expression begins with a parenthesized type name, it may
6796 be either a cast or a compound literal; we need to see whether
6797 the next character is '{' to tell the difference. If not, it is
6798 an unary expression. Full detection of unknown typenames here
6799 would require a 3-token lookahead. */
6800 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6801 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6803 struct c_type_name *type_name;
6804 struct c_expr ret;
6805 struct c_expr expr;
6806 c_parser_consume_token (parser);
6807 type_name = c_parser_type_name (parser);
6808 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6809 if (type_name == NULL)
6811 ret.value = error_mark_node;
6812 ret.original_code = ERROR_MARK;
6813 ret.original_type = NULL;
6814 return ret;
6817 /* Save casted types in the function's used types hash table. */
6818 used_types_insert (type_name->specs->type);
6820 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6821 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6822 cast_loc);
6824 location_t expr_loc = c_parser_peek_token (parser)->location;
6825 expr = c_parser_cast_expression (parser, NULL);
6826 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6828 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6829 if (ret.value && expr.value)
6830 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6831 ret.original_code = ERROR_MARK;
6832 ret.original_type = NULL;
6833 return ret;
6835 else
6836 return c_parser_unary_expression (parser);
6839 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6841 unary-expression:
6842 postfix-expression
6843 ++ unary-expression
6844 -- unary-expression
6845 unary-operator cast-expression
6846 sizeof unary-expression
6847 sizeof ( type-name )
6849 unary-operator: one of
6850 & * + - ~ !
6852 GNU extensions:
6854 unary-expression:
6855 __alignof__ unary-expression
6856 __alignof__ ( type-name )
6857 && identifier
6859 (C11 permits _Alignof with type names only.)
6861 unary-operator: one of
6862 __extension__ __real__ __imag__
6864 Transactional Memory:
6866 unary-expression:
6867 transaction-expression
6869 In addition, the GNU syntax treats ++ and -- as unary operators, so
6870 they may be applied to cast expressions with errors for non-lvalues
6871 given later. */
6873 static struct c_expr
6874 c_parser_unary_expression (c_parser *parser)
6876 int ext;
6877 struct c_expr ret, op;
6878 location_t op_loc = c_parser_peek_token (parser)->location;
6879 location_t exp_loc;
6880 location_t finish;
6881 ret.original_code = ERROR_MARK;
6882 ret.original_type = NULL;
6883 switch (c_parser_peek_token (parser)->type)
6885 case CPP_PLUS_PLUS:
6886 c_parser_consume_token (parser);
6887 exp_loc = c_parser_peek_token (parser)->location;
6888 op = c_parser_cast_expression (parser, NULL);
6890 /* If there is array notations in op, we expand them. */
6891 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6892 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6893 else
6895 op = default_function_array_read_conversion (exp_loc, op);
6896 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6898 case CPP_MINUS_MINUS:
6899 c_parser_consume_token (parser);
6900 exp_loc = c_parser_peek_token (parser)->location;
6901 op = c_parser_cast_expression (parser, NULL);
6903 /* If there is array notations in op, we expand them. */
6904 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6905 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6906 else
6908 op = default_function_array_read_conversion (exp_loc, op);
6909 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6911 case CPP_AND:
6912 c_parser_consume_token (parser);
6913 op = c_parser_cast_expression (parser, NULL);
6914 mark_exp_read (op.value);
6915 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6916 case CPP_MULT:
6918 c_parser_consume_token (parser);
6919 exp_loc = c_parser_peek_token (parser)->location;
6920 op = c_parser_cast_expression (parser, NULL);
6921 finish = op.get_finish ();
6922 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6923 location_t combined_loc = make_location (op_loc, op_loc, finish);
6924 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
6925 ret.src_range.m_start = op_loc;
6926 ret.src_range.m_finish = finish;
6927 return ret;
6929 case CPP_PLUS:
6930 if (!c_dialect_objc () && !in_system_header_at (input_location))
6931 warning_at (op_loc,
6932 OPT_Wtraditional,
6933 "traditional C rejects the unary plus operator");
6934 c_parser_consume_token (parser);
6935 exp_loc = c_parser_peek_token (parser)->location;
6936 op = c_parser_cast_expression (parser, NULL);
6937 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6938 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6939 case CPP_MINUS:
6940 c_parser_consume_token (parser);
6941 exp_loc = c_parser_peek_token (parser)->location;
6942 op = c_parser_cast_expression (parser, NULL);
6943 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6944 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6945 case CPP_COMPL:
6946 c_parser_consume_token (parser);
6947 exp_loc = c_parser_peek_token (parser)->location;
6948 op = c_parser_cast_expression (parser, NULL);
6949 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6950 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6951 case CPP_NOT:
6952 c_parser_consume_token (parser);
6953 exp_loc = c_parser_peek_token (parser)->location;
6954 op = c_parser_cast_expression (parser, NULL);
6955 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6956 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6957 case CPP_AND_AND:
6958 /* Refer to the address of a label as a pointer. */
6959 c_parser_consume_token (parser);
6960 if (c_parser_next_token_is (parser, CPP_NAME))
6962 ret.value = finish_label_address_expr
6963 (c_parser_peek_token (parser)->value, op_loc);
6964 set_c_expr_source_range (&ret, op_loc,
6965 c_parser_peek_token (parser)->get_finish ());
6966 c_parser_consume_token (parser);
6968 else
6970 c_parser_error (parser, "expected identifier");
6971 ret.value = error_mark_node;
6973 return ret;
6974 case CPP_KEYWORD:
6975 switch (c_parser_peek_token (parser)->keyword)
6977 case RID_SIZEOF:
6978 return c_parser_sizeof_expression (parser);
6979 case RID_ALIGNOF:
6980 return c_parser_alignof_expression (parser);
6981 case RID_EXTENSION:
6982 c_parser_consume_token (parser);
6983 ext = disable_extension_diagnostics ();
6984 ret = c_parser_cast_expression (parser, NULL);
6985 restore_extension_diagnostics (ext);
6986 return ret;
6987 case RID_REALPART:
6988 c_parser_consume_token (parser);
6989 exp_loc = c_parser_peek_token (parser)->location;
6990 op = c_parser_cast_expression (parser, NULL);
6991 op = default_function_array_conversion (exp_loc, op);
6992 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6993 case RID_IMAGPART:
6994 c_parser_consume_token (parser);
6995 exp_loc = c_parser_peek_token (parser)->location;
6996 op = c_parser_cast_expression (parser, NULL);
6997 op = default_function_array_conversion (exp_loc, op);
6998 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6999 case RID_TRANSACTION_ATOMIC:
7000 case RID_TRANSACTION_RELAXED:
7001 return c_parser_transaction_expression (parser,
7002 c_parser_peek_token (parser)->keyword);
7003 default:
7004 return c_parser_postfix_expression (parser);
7006 default:
7007 return c_parser_postfix_expression (parser);
7011 /* Parse a sizeof expression. */
7013 static struct c_expr
7014 c_parser_sizeof_expression (c_parser *parser)
7016 struct c_expr expr;
7017 struct c_expr result;
7018 location_t expr_loc;
7019 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7021 location_t start;
7022 location_t finish = UNKNOWN_LOCATION;
7024 start = c_parser_peek_token (parser)->location;
7026 c_parser_consume_token (parser);
7027 c_inhibit_evaluation_warnings++;
7028 in_sizeof++;
7029 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7030 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7032 /* Either sizeof ( type-name ) or sizeof unary-expression
7033 starting with a compound literal. */
7034 struct c_type_name *type_name;
7035 c_parser_consume_token (parser);
7036 expr_loc = c_parser_peek_token (parser)->location;
7037 type_name = c_parser_type_name (parser);
7038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7039 finish = parser->tokens_buf[0].location;
7040 if (type_name == NULL)
7042 struct c_expr ret;
7043 c_inhibit_evaluation_warnings--;
7044 in_sizeof--;
7045 ret.value = error_mark_node;
7046 ret.original_code = ERROR_MARK;
7047 ret.original_type = NULL;
7048 return ret;
7050 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7052 expr = c_parser_postfix_expression_after_paren_type (parser,
7053 type_name,
7054 expr_loc);
7055 finish = expr.get_finish ();
7056 goto sizeof_expr;
7058 /* sizeof ( type-name ). */
7059 c_inhibit_evaluation_warnings--;
7060 in_sizeof--;
7061 result = c_expr_sizeof_type (expr_loc, type_name);
7063 else
7065 expr_loc = c_parser_peek_token (parser)->location;
7066 expr = c_parser_unary_expression (parser);
7067 finish = expr.get_finish ();
7068 sizeof_expr:
7069 c_inhibit_evaluation_warnings--;
7070 in_sizeof--;
7071 mark_exp_read (expr.value);
7072 if (TREE_CODE (expr.value) == COMPONENT_REF
7073 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7074 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7075 result = c_expr_sizeof_expr (expr_loc, expr);
7077 if (finish != UNKNOWN_LOCATION)
7078 set_c_expr_source_range (&result, start, finish);
7079 return result;
7082 /* Parse an alignof expression. */
7084 static struct c_expr
7085 c_parser_alignof_expression (c_parser *parser)
7087 struct c_expr expr;
7088 location_t start_loc = c_parser_peek_token (parser)->location;
7089 location_t end_loc;
7090 tree alignof_spelling = c_parser_peek_token (parser)->value;
7091 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7092 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7093 "_Alignof") == 0;
7094 /* A diagnostic is not required for the use of this identifier in
7095 the implementation namespace; only diagnose it for the C11
7096 spelling because of existing code using the other spellings. */
7097 if (is_c11_alignof)
7099 if (flag_isoc99)
7100 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7101 alignof_spelling);
7102 else
7103 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7104 alignof_spelling);
7106 c_parser_consume_token (parser);
7107 c_inhibit_evaluation_warnings++;
7108 in_alignof++;
7109 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7110 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7112 /* Either __alignof__ ( type-name ) or __alignof__
7113 unary-expression starting with a compound literal. */
7114 location_t loc;
7115 struct c_type_name *type_name;
7116 struct c_expr ret;
7117 c_parser_consume_token (parser);
7118 loc = c_parser_peek_token (parser)->location;
7119 type_name = c_parser_type_name (parser);
7120 end_loc = c_parser_peek_token (parser)->location;
7121 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7122 if (type_name == NULL)
7124 struct c_expr ret;
7125 c_inhibit_evaluation_warnings--;
7126 in_alignof--;
7127 ret.value = error_mark_node;
7128 ret.original_code = ERROR_MARK;
7129 ret.original_type = NULL;
7130 return ret;
7132 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7134 expr = c_parser_postfix_expression_after_paren_type (parser,
7135 type_name,
7136 loc);
7137 goto alignof_expr;
7139 /* alignof ( type-name ). */
7140 c_inhibit_evaluation_warnings--;
7141 in_alignof--;
7142 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7143 NULL, NULL),
7144 false, is_c11_alignof, 1);
7145 ret.original_code = ERROR_MARK;
7146 ret.original_type = NULL;
7147 set_c_expr_source_range (&ret, start_loc, end_loc);
7148 return ret;
7150 else
7152 struct c_expr ret;
7153 expr = c_parser_unary_expression (parser);
7154 end_loc = expr.src_range.m_finish;
7155 alignof_expr:
7156 mark_exp_read (expr.value);
7157 c_inhibit_evaluation_warnings--;
7158 in_alignof--;
7159 if (is_c11_alignof)
7160 pedwarn (start_loc,
7161 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7162 alignof_spelling);
7163 ret.value = c_alignof_expr (start_loc, expr.value);
7164 ret.original_code = ERROR_MARK;
7165 ret.original_type = NULL;
7166 set_c_expr_source_range (&ret, start_loc, end_loc);
7167 return ret;
7171 /* Helper function to read arguments of builtins which are interfaces
7172 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7173 others. The name of the builtin is passed using BNAME parameter.
7174 Function returns true if there were no errors while parsing and
7175 stores the arguments in CEXPR_LIST. If it returns true,
7176 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7177 parenthesis. */
7178 static bool
7179 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7180 vec<c_expr_t, va_gc> **ret_cexpr_list,
7181 bool choose_expr_p,
7182 location_t *out_close_paren_loc)
7184 location_t loc = c_parser_peek_token (parser)->location;
7185 vec<c_expr_t, va_gc> *cexpr_list;
7186 c_expr_t expr;
7187 bool saved_force_folding_builtin_constant_p;
7189 *ret_cexpr_list = NULL;
7190 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7192 error_at (loc, "cannot take address of %qs", bname);
7193 return false;
7196 c_parser_consume_token (parser);
7198 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7200 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7201 c_parser_consume_token (parser);
7202 return true;
7205 saved_force_folding_builtin_constant_p
7206 = force_folding_builtin_constant_p;
7207 force_folding_builtin_constant_p |= choose_expr_p;
7208 expr = c_parser_expr_no_commas (parser, NULL);
7209 force_folding_builtin_constant_p
7210 = saved_force_folding_builtin_constant_p;
7211 vec_alloc (cexpr_list, 1);
7212 vec_safe_push (cexpr_list, expr);
7213 while (c_parser_next_token_is (parser, CPP_COMMA))
7215 c_parser_consume_token (parser);
7216 expr = c_parser_expr_no_commas (parser, NULL);
7217 vec_safe_push (cexpr_list, expr);
7220 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7221 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7222 return false;
7224 *ret_cexpr_list = cexpr_list;
7225 return true;
7228 /* This represents a single generic-association. */
7230 struct c_generic_association
7232 /* The location of the starting token of the type. */
7233 location_t type_location;
7234 /* The association's type, or NULL_TREE for 'default'. */
7235 tree type;
7236 /* The association's expression. */
7237 struct c_expr expression;
7240 /* Parse a generic-selection. (C11 6.5.1.1).
7242 generic-selection:
7243 _Generic ( assignment-expression , generic-assoc-list )
7245 generic-assoc-list:
7246 generic-association
7247 generic-assoc-list , generic-association
7249 generic-association:
7250 type-name : assignment-expression
7251 default : assignment-expression
7254 static struct c_expr
7255 c_parser_generic_selection (c_parser *parser)
7257 struct c_expr selector, error_expr;
7258 tree selector_type;
7259 struct c_generic_association matched_assoc;
7260 bool match_found = false;
7261 location_t generic_loc, selector_loc;
7263 error_expr.original_code = ERROR_MARK;
7264 error_expr.original_type = NULL;
7265 error_expr.set_error ();
7266 matched_assoc.type_location = UNKNOWN_LOCATION;
7267 matched_assoc.type = NULL_TREE;
7268 matched_assoc.expression = error_expr;
7270 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7271 generic_loc = c_parser_peek_token (parser)->location;
7272 c_parser_consume_token (parser);
7273 if (flag_isoc99)
7274 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7275 "ISO C99 does not support %<_Generic%>");
7276 else
7277 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7278 "ISO C90 does not support %<_Generic%>");
7280 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7281 return error_expr;
7283 c_inhibit_evaluation_warnings++;
7284 selector_loc = c_parser_peek_token (parser)->location;
7285 selector = c_parser_expr_no_commas (parser, NULL);
7286 selector = default_function_array_conversion (selector_loc, selector);
7287 c_inhibit_evaluation_warnings--;
7289 if (selector.value == error_mark_node)
7291 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7292 return selector;
7294 selector_type = TREE_TYPE (selector.value);
7295 /* In ISO C terms, rvalues (including the controlling expression of
7296 _Generic) do not have qualified types. */
7297 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7298 selector_type = TYPE_MAIN_VARIANT (selector_type);
7299 /* In ISO C terms, _Noreturn is not part of the type of expressions
7300 such as &abort, but in GCC it is represented internally as a type
7301 qualifier. */
7302 if (FUNCTION_POINTER_TYPE_P (selector_type)
7303 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7304 selector_type
7305 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7307 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7309 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7310 return error_expr;
7313 auto_vec<c_generic_association> associations;
7314 while (1)
7316 struct c_generic_association assoc, *iter;
7317 unsigned int ix;
7318 c_token *token = c_parser_peek_token (parser);
7320 assoc.type_location = token->location;
7321 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7323 c_parser_consume_token (parser);
7324 assoc.type = NULL_TREE;
7326 else
7328 struct c_type_name *type_name;
7330 type_name = c_parser_type_name (parser);
7331 if (type_name == NULL)
7333 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7334 return error_expr;
7336 assoc.type = groktypename (type_name, NULL, NULL);
7337 if (assoc.type == error_mark_node)
7339 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7340 return error_expr;
7343 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7344 error_at (assoc.type_location,
7345 "%<_Generic%> association has function type");
7346 else if (!COMPLETE_TYPE_P (assoc.type))
7347 error_at (assoc.type_location,
7348 "%<_Generic%> association has incomplete type");
7350 if (variably_modified_type_p (assoc.type, NULL_TREE))
7351 error_at (assoc.type_location,
7352 "%<_Generic%> association has "
7353 "variable length type");
7356 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7358 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7359 return error_expr;
7362 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7363 if (assoc.expression.value == error_mark_node)
7365 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7366 return error_expr;
7369 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7371 if (assoc.type == NULL_TREE)
7373 if (iter->type == NULL_TREE)
7375 error_at (assoc.type_location,
7376 "duplicate %<default%> case in %<_Generic%>");
7377 inform (iter->type_location, "original %<default%> is here");
7380 else if (iter->type != NULL_TREE)
7382 if (comptypes (assoc.type, iter->type))
7384 error_at (assoc.type_location,
7385 "%<_Generic%> specifies two compatible types");
7386 inform (iter->type_location, "compatible type is here");
7391 if (assoc.type == NULL_TREE)
7393 if (!match_found)
7395 matched_assoc = assoc;
7396 match_found = true;
7399 else if (comptypes (assoc.type, selector_type))
7401 if (!match_found || matched_assoc.type == NULL_TREE)
7403 matched_assoc = assoc;
7404 match_found = true;
7406 else
7408 error_at (assoc.type_location,
7409 "%<_Generic> selector matches multiple associations");
7410 inform (matched_assoc.type_location,
7411 "other match is here");
7415 associations.safe_push (assoc);
7417 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7418 break;
7419 c_parser_consume_token (parser);
7422 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7424 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7425 return error_expr;
7428 if (!match_found)
7430 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7431 "compatible with any association",
7432 selector_type);
7433 return error_expr;
7436 return matched_assoc.expression;
7439 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7441 postfix-expression:
7442 primary-expression
7443 postfix-expression [ expression ]
7444 postfix-expression ( argument-expression-list[opt] )
7445 postfix-expression . identifier
7446 postfix-expression -> identifier
7447 postfix-expression ++
7448 postfix-expression --
7449 ( type-name ) { initializer-list }
7450 ( type-name ) { initializer-list , }
7452 argument-expression-list:
7453 argument-expression
7454 argument-expression-list , argument-expression
7456 primary-expression:
7457 identifier
7458 constant
7459 string-literal
7460 ( expression )
7461 generic-selection
7463 GNU extensions:
7465 primary-expression:
7466 __func__
7467 (treated as a keyword in GNU C)
7468 __FUNCTION__
7469 __PRETTY_FUNCTION__
7470 ( compound-statement )
7471 __builtin_va_arg ( assignment-expression , type-name )
7472 __builtin_offsetof ( type-name , offsetof-member-designator )
7473 __builtin_choose_expr ( assignment-expression ,
7474 assignment-expression ,
7475 assignment-expression )
7476 __builtin_types_compatible_p ( type-name , type-name )
7477 __builtin_complex ( assignment-expression , assignment-expression )
7478 __builtin_shuffle ( assignment-expression , assignment-expression )
7479 __builtin_shuffle ( assignment-expression ,
7480 assignment-expression ,
7481 assignment-expression, )
7483 offsetof-member-designator:
7484 identifier
7485 offsetof-member-designator . identifier
7486 offsetof-member-designator [ expression ]
7488 Objective-C:
7490 primary-expression:
7491 [ objc-receiver objc-message-args ]
7492 @selector ( objc-selector-arg )
7493 @protocol ( identifier )
7494 @encode ( type-name )
7495 objc-string-literal
7496 Classname . identifier
7499 static struct c_expr
7500 c_parser_postfix_expression (c_parser *parser)
7502 struct c_expr expr, e1;
7503 struct c_type_name *t1, *t2;
7504 location_t loc = c_parser_peek_token (parser)->location;;
7505 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7506 expr.original_code = ERROR_MARK;
7507 expr.original_type = NULL;
7508 switch (c_parser_peek_token (parser)->type)
7510 case CPP_NUMBER:
7511 expr.value = c_parser_peek_token (parser)->value;
7512 set_c_expr_source_range (&expr, tok_range);
7513 loc = c_parser_peek_token (parser)->location;
7514 c_parser_consume_token (parser);
7515 if (TREE_CODE (expr.value) == FIXED_CST
7516 && !targetm.fixed_point_supported_p ())
7518 error_at (loc, "fixed-point types not supported for this target");
7519 expr.value = error_mark_node;
7521 break;
7522 case CPP_CHAR:
7523 case CPP_CHAR16:
7524 case CPP_CHAR32:
7525 case CPP_WCHAR:
7526 expr.value = c_parser_peek_token (parser)->value;
7527 set_c_expr_source_range (&expr, tok_range);
7528 c_parser_consume_token (parser);
7529 break;
7530 case CPP_STRING:
7531 case CPP_STRING16:
7532 case CPP_STRING32:
7533 case CPP_WSTRING:
7534 case CPP_UTF8STRING:
7535 expr.value = c_parser_peek_token (parser)->value;
7536 set_c_expr_source_range (&expr, tok_range);
7537 expr.original_code = STRING_CST;
7538 c_parser_consume_token (parser);
7539 break;
7540 case CPP_OBJC_STRING:
7541 gcc_assert (c_dialect_objc ());
7542 expr.value
7543 = objc_build_string_object (c_parser_peek_token (parser)->value);
7544 set_c_expr_source_range (&expr, tok_range);
7545 c_parser_consume_token (parser);
7546 break;
7547 case CPP_NAME:
7548 switch (c_parser_peek_token (parser)->id_kind)
7550 case C_ID_ID:
7552 tree id = c_parser_peek_token (parser)->value;
7553 c_parser_consume_token (parser);
7554 expr.value = build_external_ref (loc, id,
7555 (c_parser_peek_token (parser)->type
7556 == CPP_OPEN_PAREN),
7557 &expr.original_type);
7558 set_c_expr_source_range (&expr, tok_range);
7559 break;
7561 case C_ID_CLASSNAME:
7563 /* Here we parse the Objective-C 2.0 Class.name dot
7564 syntax. */
7565 tree class_name = c_parser_peek_token (parser)->value;
7566 tree component;
7567 c_parser_consume_token (parser);
7568 gcc_assert (c_dialect_objc ());
7569 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7571 expr.set_error ();
7572 break;
7574 if (c_parser_next_token_is_not (parser, CPP_NAME))
7576 c_parser_error (parser, "expected identifier");
7577 expr.set_error ();
7578 break;
7580 c_token *component_tok = c_parser_peek_token (parser);
7581 component = component_tok->value;
7582 location_t end_loc = component_tok->get_finish ();
7583 c_parser_consume_token (parser);
7584 expr.value = objc_build_class_component_ref (class_name,
7585 component);
7586 set_c_expr_source_range (&expr, loc, end_loc);
7587 break;
7589 default:
7590 c_parser_error (parser, "expected expression");
7591 expr.set_error ();
7592 break;
7594 break;
7595 case CPP_OPEN_PAREN:
7596 /* A parenthesized expression, statement expression or compound
7597 literal. */
7598 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7600 /* A statement expression. */
7601 tree stmt;
7602 location_t brace_loc;
7603 c_parser_consume_token (parser);
7604 brace_loc = c_parser_peek_token (parser)->location;
7605 c_parser_consume_token (parser);
7606 if (!building_stmt_list_p ())
7608 error_at (loc, "braced-group within expression allowed "
7609 "only inside a function");
7610 parser->error = true;
7611 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7612 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7613 expr.set_error ();
7614 break;
7616 stmt = c_begin_stmt_expr ();
7617 c_parser_compound_statement_nostart (parser);
7618 location_t close_loc = c_parser_peek_token (parser)->location;
7619 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7620 "expected %<)%>");
7621 pedwarn (loc, OPT_Wpedantic,
7622 "ISO C forbids braced-groups within expressions");
7623 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7624 set_c_expr_source_range (&expr, loc, close_loc);
7625 mark_exp_read (expr.value);
7627 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7629 /* A compound literal. ??? Can we actually get here rather
7630 than going directly to
7631 c_parser_postfix_expression_after_paren_type from
7632 elsewhere? */
7633 location_t loc;
7634 struct c_type_name *type_name;
7635 c_parser_consume_token (parser);
7636 loc = c_parser_peek_token (parser)->location;
7637 type_name = c_parser_type_name (parser);
7638 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7639 "expected %<)%>");
7640 if (type_name == NULL)
7642 expr.set_error ();
7644 else
7645 expr = c_parser_postfix_expression_after_paren_type (parser,
7646 type_name,
7647 loc);
7649 else
7651 /* A parenthesized expression. */
7652 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7653 c_parser_consume_token (parser);
7654 expr = c_parser_expression (parser);
7655 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7656 TREE_NO_WARNING (expr.value) = 1;
7657 if (expr.original_code != C_MAYBE_CONST_EXPR)
7658 expr.original_code = ERROR_MARK;
7659 /* Don't change EXPR.ORIGINAL_TYPE. */
7660 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7661 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7662 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7663 "expected %<)%>");
7665 break;
7666 case CPP_KEYWORD:
7667 switch (c_parser_peek_token (parser)->keyword)
7669 case RID_FUNCTION_NAME:
7670 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7671 "%<__FUNCTION__%> predefined identifier");
7672 expr.value = fname_decl (loc,
7673 c_parser_peek_token (parser)->keyword,
7674 c_parser_peek_token (parser)->value);
7675 set_c_expr_source_range (&expr, loc, loc);
7676 c_parser_consume_token (parser);
7677 break;
7678 case RID_PRETTY_FUNCTION_NAME:
7679 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7680 "%<__PRETTY_FUNCTION__%> predefined identifier");
7681 expr.value = fname_decl (loc,
7682 c_parser_peek_token (parser)->keyword,
7683 c_parser_peek_token (parser)->value);
7684 set_c_expr_source_range (&expr, loc, loc);
7685 c_parser_consume_token (parser);
7686 break;
7687 case RID_C99_FUNCTION_NAME:
7688 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7689 "%<__func__%> predefined identifier");
7690 expr.value = fname_decl (loc,
7691 c_parser_peek_token (parser)->keyword,
7692 c_parser_peek_token (parser)->value);
7693 set_c_expr_source_range (&expr, loc, loc);
7694 c_parser_consume_token (parser);
7695 break;
7696 case RID_VA_ARG:
7698 location_t start_loc = loc;
7699 c_parser_consume_token (parser);
7700 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7702 expr.set_error ();
7703 break;
7705 e1 = c_parser_expr_no_commas (parser, NULL);
7706 mark_exp_read (e1.value);
7707 e1.value = c_fully_fold (e1.value, false, NULL);
7708 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7710 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7711 expr.set_error ();
7712 break;
7714 loc = c_parser_peek_token (parser)->location;
7715 t1 = c_parser_type_name (parser);
7716 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7717 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7718 "expected %<)%>");
7719 if (t1 == NULL)
7721 expr.set_error ();
7723 else
7725 tree type_expr = NULL_TREE;
7726 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7727 groktypename (t1, &type_expr, NULL));
7728 if (type_expr)
7730 expr.value = build2 (C_MAYBE_CONST_EXPR,
7731 TREE_TYPE (expr.value), type_expr,
7732 expr.value);
7733 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7735 set_c_expr_source_range (&expr, start_loc, end_loc);
7738 break;
7739 case RID_OFFSETOF:
7740 c_parser_consume_token (parser);
7741 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7743 expr.set_error ();
7744 break;
7746 t1 = c_parser_type_name (parser);
7747 if (t1 == NULL)
7748 parser->error = true;
7749 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7750 gcc_assert (parser->error);
7751 if (parser->error)
7753 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7754 expr.set_error ();
7755 break;
7759 tree type = groktypename (t1, NULL, NULL);
7760 tree offsetof_ref;
7761 if (type == error_mark_node)
7762 offsetof_ref = error_mark_node;
7763 else
7765 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7766 SET_EXPR_LOCATION (offsetof_ref, loc);
7768 /* Parse the second argument to __builtin_offsetof. We
7769 must have one identifier, and beyond that we want to
7770 accept sub structure and sub array references. */
7771 if (c_parser_next_token_is (parser, CPP_NAME))
7773 c_token *comp_tok = c_parser_peek_token (parser);
7774 offsetof_ref = build_component_ref
7775 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
7776 c_parser_consume_token (parser);
7777 while (c_parser_next_token_is (parser, CPP_DOT)
7778 || c_parser_next_token_is (parser,
7779 CPP_OPEN_SQUARE)
7780 || c_parser_next_token_is (parser,
7781 CPP_DEREF))
7783 if (c_parser_next_token_is (parser, CPP_DEREF))
7785 loc = c_parser_peek_token (parser)->location;
7786 offsetof_ref = build_array_ref (loc,
7787 offsetof_ref,
7788 integer_zero_node);
7789 goto do_dot;
7791 else if (c_parser_next_token_is (parser, CPP_DOT))
7793 do_dot:
7794 c_parser_consume_token (parser);
7795 if (c_parser_next_token_is_not (parser,
7796 CPP_NAME))
7798 c_parser_error (parser, "expected identifier");
7799 break;
7801 c_token *comp_tok = c_parser_peek_token (parser);
7802 offsetof_ref = build_component_ref
7803 (loc, offsetof_ref, comp_tok->value,
7804 comp_tok->location);
7805 c_parser_consume_token (parser);
7807 else
7809 struct c_expr ce;
7810 tree idx;
7811 loc = c_parser_peek_token (parser)->location;
7812 c_parser_consume_token (parser);
7813 ce = c_parser_expression (parser);
7814 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7815 idx = ce.value;
7816 idx = c_fully_fold (idx, false, NULL);
7817 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7818 "expected %<]%>");
7819 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7823 else
7824 c_parser_error (parser, "expected identifier");
7825 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7826 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7827 "expected %<)%>");
7828 expr.value = fold_offsetof (offsetof_ref);
7829 set_c_expr_source_range (&expr, loc, end_loc);
7831 break;
7832 case RID_CHOOSE_EXPR:
7834 vec<c_expr_t, va_gc> *cexpr_list;
7835 c_expr_t *e1_p, *e2_p, *e3_p;
7836 tree c;
7837 location_t close_paren_loc;
7839 c_parser_consume_token (parser);
7840 if (!c_parser_get_builtin_args (parser,
7841 "__builtin_choose_expr",
7842 &cexpr_list, true,
7843 &close_paren_loc))
7845 expr.set_error ();
7846 break;
7849 if (vec_safe_length (cexpr_list) != 3)
7851 error_at (loc, "wrong number of arguments to "
7852 "%<__builtin_choose_expr%>");
7853 expr.set_error ();
7854 break;
7857 e1_p = &(*cexpr_list)[0];
7858 e2_p = &(*cexpr_list)[1];
7859 e3_p = &(*cexpr_list)[2];
7861 c = e1_p->value;
7862 mark_exp_read (e2_p->value);
7863 mark_exp_read (e3_p->value);
7864 if (TREE_CODE (c) != INTEGER_CST
7865 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7866 error_at (loc,
7867 "first argument to %<__builtin_choose_expr%> not"
7868 " a constant");
7869 constant_expression_warning (c);
7870 expr = integer_zerop (c) ? *e3_p : *e2_p;
7871 set_c_expr_source_range (&expr, loc, close_paren_loc);
7872 break;
7874 case RID_TYPES_COMPATIBLE_P:
7875 c_parser_consume_token (parser);
7876 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7878 expr.set_error ();
7879 break;
7881 t1 = c_parser_type_name (parser);
7882 if (t1 == NULL)
7884 expr.set_error ();
7885 break;
7887 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7889 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7890 expr.set_error ();
7891 break;
7893 t2 = c_parser_type_name (parser);
7894 if (t2 == NULL)
7896 expr.set_error ();
7897 break;
7900 location_t close_paren_loc = c_parser_peek_token (parser)->location;
7901 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7902 "expected %<)%>");
7903 tree e1, e2;
7904 e1 = groktypename (t1, NULL, NULL);
7905 e2 = groktypename (t2, NULL, NULL);
7906 if (e1 == error_mark_node || e2 == error_mark_node)
7908 expr.set_error ();
7909 break;
7912 e1 = TYPE_MAIN_VARIANT (e1);
7913 e2 = TYPE_MAIN_VARIANT (e2);
7915 expr.value
7916 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7917 set_c_expr_source_range (&expr, loc, close_paren_loc);
7919 break;
7920 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7922 vec<c_expr_t, va_gc> *cexpr_list;
7923 c_expr_t *e2_p;
7924 tree chain_value;
7925 location_t close_paren_loc;
7927 c_parser_consume_token (parser);
7928 if (!c_parser_get_builtin_args (parser,
7929 "__builtin_call_with_static_chain",
7930 &cexpr_list, false,
7931 &close_paren_loc))
7933 expr.set_error ();
7934 break;
7936 if (vec_safe_length (cexpr_list) != 2)
7938 error_at (loc, "wrong number of arguments to "
7939 "%<__builtin_call_with_static_chain%>");
7940 expr.set_error ();
7941 break;
7944 expr = (*cexpr_list)[0];
7945 e2_p = &(*cexpr_list)[1];
7946 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7947 chain_value = e2_p->value;
7948 mark_exp_read (chain_value);
7950 if (TREE_CODE (expr.value) != CALL_EXPR)
7951 error_at (loc, "first argument to "
7952 "%<__builtin_call_with_static_chain%> "
7953 "must be a call expression");
7954 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7955 error_at (loc, "second argument to "
7956 "%<__builtin_call_with_static_chain%> "
7957 "must be a pointer type");
7958 else
7959 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7960 set_c_expr_source_range (&expr, loc, close_paren_loc);
7961 break;
7963 case RID_BUILTIN_COMPLEX:
7965 vec<c_expr_t, va_gc> *cexpr_list;
7966 c_expr_t *e1_p, *e2_p;
7967 location_t close_paren_loc;
7969 c_parser_consume_token (parser);
7970 if (!c_parser_get_builtin_args (parser,
7971 "__builtin_complex",
7972 &cexpr_list, false,
7973 &close_paren_loc))
7975 expr.set_error ();
7976 break;
7979 if (vec_safe_length (cexpr_list) != 2)
7981 error_at (loc, "wrong number of arguments to "
7982 "%<__builtin_complex%>");
7983 expr.set_error ();
7984 break;
7987 e1_p = &(*cexpr_list)[0];
7988 e2_p = &(*cexpr_list)[1];
7990 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7991 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7992 e1_p->value = convert (TREE_TYPE (e1_p->value),
7993 TREE_OPERAND (e1_p->value, 0));
7994 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7995 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7996 e2_p->value = convert (TREE_TYPE (e2_p->value),
7997 TREE_OPERAND (e2_p->value, 0));
7998 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7999 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8000 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8001 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8003 error_at (loc, "%<__builtin_complex%> operand "
8004 "not of real binary floating-point type");
8005 expr.set_error ();
8006 break;
8008 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8009 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8011 error_at (loc,
8012 "%<__builtin_complex%> operands of different types");
8013 expr.set_error ();
8014 break;
8016 pedwarn_c90 (loc, OPT_Wpedantic,
8017 "ISO C90 does not support complex types");
8018 expr.value = build2_loc (loc, COMPLEX_EXPR,
8019 build_complex_type
8020 (TYPE_MAIN_VARIANT
8021 (TREE_TYPE (e1_p->value))),
8022 e1_p->value, e2_p->value);
8023 set_c_expr_source_range (&expr, loc, close_paren_loc);
8024 break;
8026 case RID_BUILTIN_SHUFFLE:
8028 vec<c_expr_t, va_gc> *cexpr_list;
8029 unsigned int i;
8030 c_expr_t *p;
8031 location_t close_paren_loc;
8033 c_parser_consume_token (parser);
8034 if (!c_parser_get_builtin_args (parser,
8035 "__builtin_shuffle",
8036 &cexpr_list, false,
8037 &close_paren_loc))
8039 expr.set_error ();
8040 break;
8043 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8044 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8046 if (vec_safe_length (cexpr_list) == 2)
8047 expr.value =
8048 c_build_vec_perm_expr
8049 (loc, (*cexpr_list)[0].value,
8050 NULL_TREE, (*cexpr_list)[1].value);
8052 else if (vec_safe_length (cexpr_list) == 3)
8053 expr.value =
8054 c_build_vec_perm_expr
8055 (loc, (*cexpr_list)[0].value,
8056 (*cexpr_list)[1].value,
8057 (*cexpr_list)[2].value);
8058 else
8060 error_at (loc, "wrong number of arguments to "
8061 "%<__builtin_shuffle%>");
8062 expr.set_error ();
8064 set_c_expr_source_range (&expr, loc, close_paren_loc);
8065 break;
8067 case RID_AT_SELECTOR:
8068 gcc_assert (c_dialect_objc ());
8069 c_parser_consume_token (parser);
8070 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8072 expr.set_error ();
8073 break;
8076 tree sel = c_parser_objc_selector_arg (parser);
8077 location_t close_loc = c_parser_peek_token (parser)->location;
8078 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8079 "expected %<)%>");
8080 expr.value = objc_build_selector_expr (loc, sel);
8081 set_c_expr_source_range (&expr, loc, close_loc);
8083 break;
8084 case RID_AT_PROTOCOL:
8085 gcc_assert (c_dialect_objc ());
8086 c_parser_consume_token (parser);
8087 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8089 expr.set_error ();
8090 break;
8092 if (c_parser_next_token_is_not (parser, CPP_NAME))
8094 c_parser_error (parser, "expected identifier");
8095 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8096 expr.set_error ();
8097 break;
8100 tree id = c_parser_peek_token (parser)->value;
8101 c_parser_consume_token (parser);
8102 location_t close_loc = c_parser_peek_token (parser)->location;
8103 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8104 "expected %<)%>");
8105 expr.value = objc_build_protocol_expr (id);
8106 set_c_expr_source_range (&expr, loc, close_loc);
8108 break;
8109 case RID_AT_ENCODE:
8110 /* Extension to support C-structures in the archiver. */
8111 gcc_assert (c_dialect_objc ());
8112 c_parser_consume_token (parser);
8113 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8115 expr.set_error ();
8116 break;
8118 t1 = c_parser_type_name (parser);
8119 if (t1 == NULL)
8121 expr.set_error ();
8122 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8123 break;
8126 location_t close_loc = c_parser_peek_token (parser)->location;
8127 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8128 "expected %<)%>");
8129 tree type = groktypename (t1, NULL, NULL);
8130 expr.value = objc_build_encode_expr (type);
8131 set_c_expr_source_range (&expr, loc, close_loc);
8133 break;
8134 case RID_GENERIC:
8135 expr = c_parser_generic_selection (parser);
8136 break;
8137 case RID_CILK_SPAWN:
8138 c_parser_consume_token (parser);
8139 if (!flag_cilkplus)
8141 error_at (loc, "-fcilkplus must be enabled to use "
8142 "%<_Cilk_spawn%>");
8143 expr = c_parser_cast_expression (parser, NULL);
8144 expr.set_error ();
8146 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8148 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
8149 "are not permitted");
8150 /* Now flush out all the _Cilk_spawns. */
8151 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8152 c_parser_consume_token (parser);
8153 expr = c_parser_cast_expression (parser, NULL);
8155 else
8157 expr = c_parser_cast_expression (parser, NULL);
8158 expr.value = build_cilk_spawn (loc, expr.value);
8160 break;
8161 default:
8162 c_parser_error (parser, "expected expression");
8163 expr.set_error ();
8164 break;
8166 break;
8167 case CPP_OPEN_SQUARE:
8168 if (c_dialect_objc ())
8170 tree receiver, args;
8171 c_parser_consume_token (parser);
8172 receiver = c_parser_objc_receiver (parser);
8173 args = c_parser_objc_message_args (parser);
8174 location_t close_loc = c_parser_peek_token (parser)->location;
8175 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8176 "expected %<]%>");
8177 expr.value = objc_build_message_expr (receiver, args);
8178 set_c_expr_source_range (&expr, loc, close_loc);
8179 break;
8181 /* Else fall through to report error. */
8182 /* FALLTHRU */
8183 default:
8184 c_parser_error (parser, "expected expression");
8185 expr.set_error ();
8186 break;
8188 return c_parser_postfix_expression_after_primary
8189 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8192 /* Parse a postfix expression after a parenthesized type name: the
8193 brace-enclosed initializer of a compound literal, possibly followed
8194 by some postfix operators. This is separate because it is not
8195 possible to tell until after the type name whether a cast
8196 expression has a cast or a compound literal, or whether the operand
8197 of sizeof is a parenthesized type name or starts with a compound
8198 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8199 location of the first token after the parentheses around the type
8200 name. */
8202 static struct c_expr
8203 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8204 struct c_type_name *type_name,
8205 location_t type_loc)
8207 tree type;
8208 struct c_expr init;
8209 bool non_const;
8210 struct c_expr expr;
8211 location_t start_loc;
8212 tree type_expr = NULL_TREE;
8213 bool type_expr_const = true;
8214 check_compound_literal_type (type_loc, type_name);
8215 start_init (NULL_TREE, NULL, 0);
8216 type = groktypename (type_name, &type_expr, &type_expr_const);
8217 start_loc = c_parser_peek_token (parser)->location;
8218 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8220 error_at (type_loc, "compound literal has variable size");
8221 type = error_mark_node;
8223 init = c_parser_braced_init (parser, type, false, NULL);
8224 finish_init ();
8225 maybe_warn_string_init (type_loc, type, init);
8227 if (type != error_mark_node
8228 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8229 && current_function_decl)
8231 error ("compound literal qualified by address-space qualifier");
8232 type = error_mark_node;
8235 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8236 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8237 ? CONSTRUCTOR_NON_CONST (init.value)
8238 : init.original_code == C_MAYBE_CONST_EXPR);
8239 non_const |= !type_expr_const;
8240 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8241 set_c_expr_source_range (&expr, init.src_range);
8242 expr.original_code = ERROR_MARK;
8243 expr.original_type = NULL;
8244 if (type != error_mark_node
8245 && expr.value != error_mark_node
8246 && type_expr)
8248 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8250 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8251 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8253 else
8255 gcc_assert (!non_const);
8256 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8257 type_expr, expr.value);
8260 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8263 /* Callback function for sizeof_pointer_memaccess_warning to compare
8264 types. */
8266 static bool
8267 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8269 return comptypes (type1, type2) == 1;
8272 /* Parse a postfix expression after the initial primary or compound
8273 literal; that is, parse a series of postfix operators.
8275 EXPR_LOC is the location of the primary expression. */
8277 static struct c_expr
8278 c_parser_postfix_expression_after_primary (c_parser *parser,
8279 location_t expr_loc,
8280 struct c_expr expr)
8282 struct c_expr orig_expr;
8283 tree ident, idx;
8284 location_t sizeof_arg_loc[3], comp_loc;
8285 tree sizeof_arg[3];
8286 unsigned int literal_zero_mask;
8287 unsigned int i;
8288 vec<tree, va_gc> *exprlist;
8289 vec<tree, va_gc> *origtypes = NULL;
8290 vec<location_t> arg_loc = vNULL;
8291 location_t start;
8292 location_t finish;
8294 while (true)
8296 location_t op_loc = c_parser_peek_token (parser)->location;
8297 switch (c_parser_peek_token (parser)->type)
8299 case CPP_OPEN_SQUARE:
8300 /* Array reference. */
8301 c_parser_consume_token (parser);
8302 if (flag_cilkplus
8303 && c_parser_peek_token (parser)->type == CPP_COLON)
8304 /* If we are here, then we have something like this:
8305 Array [ : ]
8307 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8308 expr.value);
8309 else
8311 idx = c_parser_expression (parser).value;
8312 /* Here we have 3 options:
8313 1. Array [EXPR] -- Normal Array call.
8314 2. Array [EXPR : EXPR] -- Array notation without stride.
8315 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8317 For 1, we just handle it just like a normal array expression.
8318 For 2 and 3 we handle it like we handle array notations. The
8319 idx value we have above becomes the initial/start index.
8321 if (flag_cilkplus
8322 && c_parser_peek_token (parser)->type == CPP_COLON)
8323 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8324 expr.value);
8325 else
8327 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8328 "expected %<]%>");
8329 start = expr.get_start ();
8330 finish = parser->tokens_buf[0].location;
8331 expr.value = build_array_ref (op_loc, expr.value, idx);
8332 set_c_expr_source_range (&expr, start, finish);
8335 expr.original_code = ERROR_MARK;
8336 expr.original_type = NULL;
8337 break;
8338 case CPP_OPEN_PAREN:
8339 /* Function call. */
8340 c_parser_consume_token (parser);
8341 for (i = 0; i < 3; i++)
8343 sizeof_arg[i] = NULL_TREE;
8344 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8346 literal_zero_mask = 0;
8347 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8348 exprlist = NULL;
8349 else
8350 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8351 sizeof_arg_loc, sizeof_arg,
8352 &arg_loc, &literal_zero_mask);
8353 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8354 "expected %<)%>");
8355 orig_expr = expr;
8356 mark_exp_read (expr.value);
8357 if (warn_sizeof_pointer_memaccess)
8358 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8359 expr.value, exprlist,
8360 sizeof_arg,
8361 sizeof_ptr_memacc_comptypes);
8362 if (TREE_CODE (expr.value) == FUNCTION_DECL
8363 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8364 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8365 && vec_safe_length (exprlist) == 3)
8367 tree arg0 = (*exprlist)[0];
8368 tree arg2 = (*exprlist)[2];
8369 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
8372 start = expr.get_start ();
8373 finish = parser->tokens_buf[0].get_finish ();
8374 expr.value
8375 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8376 exprlist, origtypes);
8377 set_c_expr_source_range (&expr, start, finish);
8379 expr.original_code = ERROR_MARK;
8380 if (TREE_CODE (expr.value) == INTEGER_CST
8381 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8382 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8383 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8384 expr.original_code = C_MAYBE_CONST_EXPR;
8385 expr.original_type = NULL;
8386 if (exprlist)
8388 release_tree_vector (exprlist);
8389 release_tree_vector (origtypes);
8391 arg_loc.release ();
8392 break;
8393 case CPP_DOT:
8394 /* Structure element reference. */
8395 c_parser_consume_token (parser);
8396 expr = default_function_array_conversion (expr_loc, expr);
8397 if (c_parser_next_token_is (parser, CPP_NAME))
8399 c_token *comp_tok = c_parser_peek_token (parser);
8400 ident = comp_tok->value;
8401 comp_loc = comp_tok->location;
8403 else
8405 c_parser_error (parser, "expected identifier");
8406 expr.set_error ();
8407 expr.original_code = ERROR_MARK;
8408 expr.original_type = NULL;
8409 return expr;
8411 start = expr.get_start ();
8412 finish = c_parser_peek_token (parser)->get_finish ();
8413 c_parser_consume_token (parser);
8414 expr.value = build_component_ref (op_loc, expr.value, ident,
8415 comp_loc);
8416 set_c_expr_source_range (&expr, start, finish);
8417 expr.original_code = ERROR_MARK;
8418 if (TREE_CODE (expr.value) != COMPONENT_REF)
8419 expr.original_type = NULL;
8420 else
8422 /* Remember the original type of a bitfield. */
8423 tree field = TREE_OPERAND (expr.value, 1);
8424 if (TREE_CODE (field) != FIELD_DECL)
8425 expr.original_type = NULL;
8426 else
8427 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8429 break;
8430 case CPP_DEREF:
8431 /* Structure element reference. */
8432 c_parser_consume_token (parser);
8433 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8434 if (c_parser_next_token_is (parser, CPP_NAME))
8436 c_token *comp_tok = c_parser_peek_token (parser);
8437 ident = comp_tok->value;
8438 comp_loc = comp_tok->location;
8440 else
8442 c_parser_error (parser, "expected identifier");
8443 expr.set_error ();
8444 expr.original_code = ERROR_MARK;
8445 expr.original_type = NULL;
8446 return expr;
8448 start = expr.get_start ();
8449 finish = c_parser_peek_token (parser)->get_finish ();
8450 c_parser_consume_token (parser);
8451 expr.value = build_component_ref (op_loc,
8452 build_indirect_ref (op_loc,
8453 expr.value,
8454 RO_ARROW),
8455 ident, comp_loc);
8456 set_c_expr_source_range (&expr, start, finish);
8457 expr.original_code = ERROR_MARK;
8458 if (TREE_CODE (expr.value) != COMPONENT_REF)
8459 expr.original_type = NULL;
8460 else
8462 /* Remember the original type of a bitfield. */
8463 tree field = TREE_OPERAND (expr.value, 1);
8464 if (TREE_CODE (field) != FIELD_DECL)
8465 expr.original_type = NULL;
8466 else
8467 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8469 break;
8470 case CPP_PLUS_PLUS:
8471 /* Postincrement. */
8472 start = expr.get_start ();
8473 finish = c_parser_peek_token (parser)->get_finish ();
8474 c_parser_consume_token (parser);
8475 /* If the expressions have array notations, we expand them. */
8476 if (flag_cilkplus
8477 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8478 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8479 else
8481 expr = default_function_array_read_conversion (expr_loc, expr);
8482 expr.value = build_unary_op (op_loc,
8483 POSTINCREMENT_EXPR, expr.value, 0);
8485 set_c_expr_source_range (&expr, start, finish);
8486 expr.original_code = ERROR_MARK;
8487 expr.original_type = NULL;
8488 break;
8489 case CPP_MINUS_MINUS:
8490 /* Postdecrement. */
8491 start = expr.get_start ();
8492 finish = c_parser_peek_token (parser)->get_finish ();
8493 c_parser_consume_token (parser);
8494 /* If the expressions have array notations, we expand them. */
8495 if (flag_cilkplus
8496 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8497 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8498 else
8500 expr = default_function_array_read_conversion (expr_loc, expr);
8501 expr.value = build_unary_op (op_loc,
8502 POSTDECREMENT_EXPR, expr.value, 0);
8504 set_c_expr_source_range (&expr, start, finish);
8505 expr.original_code = ERROR_MARK;
8506 expr.original_type = NULL;
8507 break;
8508 default:
8509 return expr;
8514 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8516 expression:
8517 assignment-expression
8518 expression , assignment-expression
8521 static struct c_expr
8522 c_parser_expression (c_parser *parser)
8524 location_t tloc = c_parser_peek_token (parser)->location;
8525 struct c_expr expr;
8526 expr = c_parser_expr_no_commas (parser, NULL);
8527 if (c_parser_next_token_is (parser, CPP_COMMA))
8528 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8529 while (c_parser_next_token_is (parser, CPP_COMMA))
8531 struct c_expr next;
8532 tree lhsval;
8533 location_t loc = c_parser_peek_token (parser)->location;
8534 location_t expr_loc;
8535 c_parser_consume_token (parser);
8536 expr_loc = c_parser_peek_token (parser)->location;
8537 lhsval = expr.value;
8538 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8539 lhsval = TREE_OPERAND (lhsval, 1);
8540 if (DECL_P (lhsval) || handled_component_p (lhsval))
8541 mark_exp_read (lhsval);
8542 next = c_parser_expr_no_commas (parser, NULL);
8543 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8544 expr.value = build_compound_expr (loc, expr.value, next.value);
8545 expr.original_code = COMPOUND_EXPR;
8546 expr.original_type = next.original_type;
8548 return expr;
8551 /* Parse an expression and convert functions or arrays to pointers and
8552 lvalues to rvalues. */
8554 static struct c_expr
8555 c_parser_expression_conv (c_parser *parser)
8557 struct c_expr expr;
8558 location_t loc = c_parser_peek_token (parser)->location;
8559 expr = c_parser_expression (parser);
8560 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8561 return expr;
8564 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8565 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8567 static inline void
8568 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8569 unsigned int idx)
8571 if (idx >= HOST_BITS_PER_INT)
8572 return;
8574 c_token *tok = c_parser_peek_token (parser);
8575 switch (tok->type)
8577 case CPP_NUMBER:
8578 case CPP_CHAR:
8579 case CPP_WCHAR:
8580 case CPP_CHAR16:
8581 case CPP_CHAR32:
8582 /* If a parameter is literal zero alone, remember it
8583 for -Wmemset-transposed-args warning. */
8584 if (integer_zerop (tok->value)
8585 && !TREE_OVERFLOW (tok->value)
8586 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8587 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8588 *literal_zero_mask |= 1U << idx;
8589 default:
8590 break;
8594 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8595 functions and arrays to pointers and lvalues to rvalues. If
8596 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8597 locations of function arguments into this vector.
8599 nonempty-expr-list:
8600 assignment-expression
8601 nonempty-expr-list , assignment-expression
8604 static vec<tree, va_gc> *
8605 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8606 vec<tree, va_gc> **p_orig_types,
8607 location_t *sizeof_arg_loc, tree *sizeof_arg,
8608 vec<location_t> *locations,
8609 unsigned int *literal_zero_mask)
8611 vec<tree, va_gc> *ret;
8612 vec<tree, va_gc> *orig_types;
8613 struct c_expr expr;
8614 location_t loc = c_parser_peek_token (parser)->location;
8615 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8616 unsigned int idx = 0;
8618 ret = make_tree_vector ();
8619 if (p_orig_types == NULL)
8620 orig_types = NULL;
8621 else
8622 orig_types = make_tree_vector ();
8624 if (sizeof_arg != NULL
8625 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8626 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8627 if (literal_zero_mask)
8628 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8629 expr = c_parser_expr_no_commas (parser, NULL);
8630 if (convert_p)
8631 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8632 if (fold_p)
8633 expr.value = c_fully_fold (expr.value, false, NULL);
8634 ret->quick_push (expr.value);
8635 if (orig_types)
8636 orig_types->quick_push (expr.original_type);
8637 if (locations)
8638 locations->safe_push (loc);
8639 if (sizeof_arg != NULL
8640 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8641 && expr.original_code == SIZEOF_EXPR)
8643 sizeof_arg[0] = c_last_sizeof_arg;
8644 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8646 while (c_parser_next_token_is (parser, CPP_COMMA))
8648 c_parser_consume_token (parser);
8649 loc = c_parser_peek_token (parser)->location;
8650 if (sizeof_arg != NULL
8651 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8652 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8653 else
8654 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8655 if (literal_zero_mask)
8656 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8657 expr = c_parser_expr_no_commas (parser, NULL);
8658 if (convert_p)
8659 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8660 if (fold_p)
8661 expr.value = c_fully_fold (expr.value, false, NULL);
8662 vec_safe_push (ret, expr.value);
8663 if (orig_types)
8664 vec_safe_push (orig_types, expr.original_type);
8665 if (locations)
8666 locations->safe_push (loc);
8667 if (++idx < 3
8668 && sizeof_arg != NULL
8669 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8670 && expr.original_code == SIZEOF_EXPR)
8672 sizeof_arg[idx] = c_last_sizeof_arg;
8673 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8676 if (orig_types)
8677 *p_orig_types = orig_types;
8678 return ret;
8681 /* Parse Objective-C-specific constructs. */
8683 /* Parse an objc-class-definition.
8685 objc-class-definition:
8686 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8687 objc-class-instance-variables[opt] objc-methodprotolist @end
8688 @implementation identifier objc-superclass[opt]
8689 objc-class-instance-variables[opt]
8690 @interface identifier ( identifier ) objc-protocol-refs[opt]
8691 objc-methodprotolist @end
8692 @interface identifier ( ) objc-protocol-refs[opt]
8693 objc-methodprotolist @end
8694 @implementation identifier ( identifier )
8696 objc-superclass:
8697 : identifier
8699 "@interface identifier (" must start "@interface identifier (
8700 identifier ) ...": objc-methodprotolist in the first production may
8701 not start with a parenthesized identifier as a declarator of a data
8702 definition with no declaration specifiers if the objc-superclass,
8703 objc-protocol-refs and objc-class-instance-variables are omitted. */
8705 static void
8706 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8708 bool iface_p;
8709 tree id1;
8710 tree superclass;
8711 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8712 iface_p = true;
8713 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8714 iface_p = false;
8715 else
8716 gcc_unreachable ();
8718 c_parser_consume_token (parser);
8719 if (c_parser_next_token_is_not (parser, CPP_NAME))
8721 c_parser_error (parser, "expected identifier");
8722 return;
8724 id1 = c_parser_peek_token (parser)->value;
8725 c_parser_consume_token (parser);
8726 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8728 /* We have a category or class extension. */
8729 tree id2;
8730 tree proto = NULL_TREE;
8731 c_parser_consume_token (parser);
8732 if (c_parser_next_token_is_not (parser, CPP_NAME))
8734 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8736 /* We have a class extension. */
8737 id2 = NULL_TREE;
8739 else
8741 c_parser_error (parser, "expected identifier or %<)%>");
8742 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8743 return;
8746 else
8748 id2 = c_parser_peek_token (parser)->value;
8749 c_parser_consume_token (parser);
8751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8752 if (!iface_p)
8754 objc_start_category_implementation (id1, id2);
8755 return;
8757 if (c_parser_next_token_is (parser, CPP_LESS))
8758 proto = c_parser_objc_protocol_refs (parser);
8759 objc_start_category_interface (id1, id2, proto, attributes);
8760 c_parser_objc_methodprotolist (parser);
8761 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8762 objc_finish_interface ();
8763 return;
8765 if (c_parser_next_token_is (parser, CPP_COLON))
8767 c_parser_consume_token (parser);
8768 if (c_parser_next_token_is_not (parser, CPP_NAME))
8770 c_parser_error (parser, "expected identifier");
8771 return;
8773 superclass = c_parser_peek_token (parser)->value;
8774 c_parser_consume_token (parser);
8776 else
8777 superclass = NULL_TREE;
8778 if (iface_p)
8780 tree proto = NULL_TREE;
8781 if (c_parser_next_token_is (parser, CPP_LESS))
8782 proto = c_parser_objc_protocol_refs (parser);
8783 objc_start_class_interface (id1, superclass, proto, attributes);
8785 else
8786 objc_start_class_implementation (id1, superclass);
8787 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8788 c_parser_objc_class_instance_variables (parser);
8789 if (iface_p)
8791 objc_continue_interface ();
8792 c_parser_objc_methodprotolist (parser);
8793 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8794 objc_finish_interface ();
8796 else
8798 objc_continue_implementation ();
8799 return;
8803 /* Parse objc-class-instance-variables.
8805 objc-class-instance-variables:
8806 { objc-instance-variable-decl-list[opt] }
8808 objc-instance-variable-decl-list:
8809 objc-visibility-spec
8810 objc-instance-variable-decl ;
8812 objc-instance-variable-decl-list objc-visibility-spec
8813 objc-instance-variable-decl-list objc-instance-variable-decl ;
8814 objc-instance-variable-decl-list ;
8816 objc-visibility-spec:
8817 @private
8818 @protected
8819 @public
8821 objc-instance-variable-decl:
8822 struct-declaration
8825 static void
8826 c_parser_objc_class_instance_variables (c_parser *parser)
8828 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8829 c_parser_consume_token (parser);
8830 while (c_parser_next_token_is_not (parser, CPP_EOF))
8832 tree decls;
8833 /* Parse any stray semicolon. */
8834 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8836 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8837 "extra semicolon");
8838 c_parser_consume_token (parser);
8839 continue;
8841 /* Stop if at the end of the instance variables. */
8842 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8844 c_parser_consume_token (parser);
8845 break;
8847 /* Parse any objc-visibility-spec. */
8848 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8850 c_parser_consume_token (parser);
8851 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8852 continue;
8854 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8856 c_parser_consume_token (parser);
8857 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8858 continue;
8860 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8862 c_parser_consume_token (parser);
8863 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8864 continue;
8866 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8868 c_parser_consume_token (parser);
8869 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8870 continue;
8872 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8874 c_parser_pragma (parser, pragma_external, NULL);
8875 continue;
8878 /* Parse some comma-separated declarations. */
8879 decls = c_parser_struct_declaration (parser);
8880 if (decls == NULL)
8882 /* There is a syntax error. We want to skip the offending
8883 tokens up to the next ';' (included) or '}'
8884 (excluded). */
8886 /* First, skip manually a ')' or ']'. This is because they
8887 reduce the nesting level, so c_parser_skip_until_found()
8888 wouldn't be able to skip past them. */
8889 c_token *token = c_parser_peek_token (parser);
8890 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8891 c_parser_consume_token (parser);
8893 /* Then, do the standard skipping. */
8894 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8896 /* We hopefully recovered. Start normal parsing again. */
8897 parser->error = false;
8898 continue;
8900 else
8902 /* Comma-separated instance variables are chained together
8903 in reverse order; add them one by one. */
8904 tree ivar = nreverse (decls);
8905 for (; ivar; ivar = DECL_CHAIN (ivar))
8906 objc_add_instance_variable (copy_node (ivar));
8908 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8912 /* Parse an objc-class-declaration.
8914 objc-class-declaration:
8915 @class identifier-list ;
8918 static void
8919 c_parser_objc_class_declaration (c_parser *parser)
8921 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8922 c_parser_consume_token (parser);
8923 /* Any identifiers, including those declared as type names, are OK
8924 here. */
8925 while (true)
8927 tree id;
8928 if (c_parser_next_token_is_not (parser, CPP_NAME))
8930 c_parser_error (parser, "expected identifier");
8931 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8932 parser->error = false;
8933 return;
8935 id = c_parser_peek_token (parser)->value;
8936 objc_declare_class (id);
8937 c_parser_consume_token (parser);
8938 if (c_parser_next_token_is (parser, CPP_COMMA))
8939 c_parser_consume_token (parser);
8940 else
8941 break;
8943 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8946 /* Parse an objc-alias-declaration.
8948 objc-alias-declaration:
8949 @compatibility_alias identifier identifier ;
8952 static void
8953 c_parser_objc_alias_declaration (c_parser *parser)
8955 tree id1, id2;
8956 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8957 c_parser_consume_token (parser);
8958 if (c_parser_next_token_is_not (parser, CPP_NAME))
8960 c_parser_error (parser, "expected identifier");
8961 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8962 return;
8964 id1 = c_parser_peek_token (parser)->value;
8965 c_parser_consume_token (parser);
8966 if (c_parser_next_token_is_not (parser, CPP_NAME))
8968 c_parser_error (parser, "expected identifier");
8969 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8970 return;
8972 id2 = c_parser_peek_token (parser)->value;
8973 c_parser_consume_token (parser);
8974 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8975 objc_declare_alias (id1, id2);
8978 /* Parse an objc-protocol-definition.
8980 objc-protocol-definition:
8981 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8982 @protocol identifier-list ;
8984 "@protocol identifier ;" should be resolved as "@protocol
8985 identifier-list ;": objc-methodprotolist may not start with a
8986 semicolon in the first alternative if objc-protocol-refs are
8987 omitted. */
8989 static void
8990 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8992 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8994 c_parser_consume_token (parser);
8995 if (c_parser_next_token_is_not (parser, CPP_NAME))
8997 c_parser_error (parser, "expected identifier");
8998 return;
9000 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9001 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9003 /* Any identifiers, including those declared as type names, are
9004 OK here. */
9005 while (true)
9007 tree id;
9008 if (c_parser_next_token_is_not (parser, CPP_NAME))
9010 c_parser_error (parser, "expected identifier");
9011 break;
9013 id = c_parser_peek_token (parser)->value;
9014 objc_declare_protocol (id, attributes);
9015 c_parser_consume_token (parser);
9016 if (c_parser_next_token_is (parser, CPP_COMMA))
9017 c_parser_consume_token (parser);
9018 else
9019 break;
9021 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9023 else
9025 tree id = c_parser_peek_token (parser)->value;
9026 tree proto = NULL_TREE;
9027 c_parser_consume_token (parser);
9028 if (c_parser_next_token_is (parser, CPP_LESS))
9029 proto = c_parser_objc_protocol_refs (parser);
9030 parser->objc_pq_context = true;
9031 objc_start_protocol (id, proto, attributes);
9032 c_parser_objc_methodprotolist (parser);
9033 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9034 parser->objc_pq_context = false;
9035 objc_finish_interface ();
9039 /* Parse an objc-method-type.
9041 objc-method-type:
9045 Return true if it is a class method (+) and false if it is
9046 an instance method (-).
9048 static inline bool
9049 c_parser_objc_method_type (c_parser *parser)
9051 switch (c_parser_peek_token (parser)->type)
9053 case CPP_PLUS:
9054 c_parser_consume_token (parser);
9055 return true;
9056 case CPP_MINUS:
9057 c_parser_consume_token (parser);
9058 return false;
9059 default:
9060 gcc_unreachable ();
9064 /* Parse an objc-method-definition.
9066 objc-method-definition:
9067 objc-method-type objc-method-decl ;[opt] compound-statement
9070 static void
9071 c_parser_objc_method_definition (c_parser *parser)
9073 bool is_class_method = c_parser_objc_method_type (parser);
9074 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9075 parser->objc_pq_context = true;
9076 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9077 &expr);
9078 if (decl == error_mark_node)
9079 return; /* Bail here. */
9081 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9083 c_parser_consume_token (parser);
9084 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9085 "extra semicolon in method definition specified");
9088 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9090 c_parser_error (parser, "expected %<{%>");
9091 return;
9094 parser->objc_pq_context = false;
9095 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9097 add_stmt (c_parser_compound_statement (parser));
9098 objc_finish_method_definition (current_function_decl);
9100 else
9102 /* This code is executed when we find a method definition
9103 outside of an @implementation context (or invalid for other
9104 reasons). Parse the method (to keep going) but do not emit
9105 any code.
9107 c_parser_compound_statement (parser);
9111 /* Parse an objc-methodprotolist.
9113 objc-methodprotolist:
9114 empty
9115 objc-methodprotolist objc-methodproto
9116 objc-methodprotolist declaration
9117 objc-methodprotolist ;
9118 @optional
9119 @required
9121 The declaration is a data definition, which may be missing
9122 declaration specifiers under the same rules and diagnostics as
9123 other data definitions outside functions, and the stray semicolon
9124 is diagnosed the same way as a stray semicolon outside a
9125 function. */
9127 static void
9128 c_parser_objc_methodprotolist (c_parser *parser)
9130 while (true)
9132 /* The list is terminated by @end. */
9133 switch (c_parser_peek_token (parser)->type)
9135 case CPP_SEMICOLON:
9136 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9137 "ISO C does not allow extra %<;%> outside of a function");
9138 c_parser_consume_token (parser);
9139 break;
9140 case CPP_PLUS:
9141 case CPP_MINUS:
9142 c_parser_objc_methodproto (parser);
9143 break;
9144 case CPP_PRAGMA:
9145 c_parser_pragma (parser, pragma_external, NULL);
9146 break;
9147 case CPP_EOF:
9148 return;
9149 default:
9150 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9151 return;
9152 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9153 c_parser_objc_at_property_declaration (parser);
9154 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9156 objc_set_method_opt (true);
9157 c_parser_consume_token (parser);
9159 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9161 objc_set_method_opt (false);
9162 c_parser_consume_token (parser);
9164 else
9165 c_parser_declaration_or_fndef (parser, false, false, true,
9166 false, true, NULL, vNULL);
9167 break;
9172 /* Parse an objc-methodproto.
9174 objc-methodproto:
9175 objc-method-type objc-method-decl ;
9178 static void
9179 c_parser_objc_methodproto (c_parser *parser)
9181 bool is_class_method = c_parser_objc_method_type (parser);
9182 tree decl, attributes = NULL_TREE;
9184 /* Remember protocol qualifiers in prototypes. */
9185 parser->objc_pq_context = true;
9186 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9187 NULL);
9188 /* Forget protocol qualifiers now. */
9189 parser->objc_pq_context = false;
9191 /* Do not allow the presence of attributes to hide an erroneous
9192 method implementation in the interface section. */
9193 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9195 c_parser_error (parser, "expected %<;%>");
9196 return;
9199 if (decl != error_mark_node)
9200 objc_add_method_declaration (is_class_method, decl, attributes);
9202 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9205 /* If we are at a position that method attributes may be present, check that
9206 there are not any parsed already (a syntax error) and then collect any
9207 specified at the current location. Finally, if new attributes were present,
9208 check that the next token is legal ( ';' for decls and '{' for defs). */
9210 static bool
9211 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9213 bool bad = false;
9214 if (*attributes)
9216 c_parser_error (parser,
9217 "method attributes must be specified at the end only");
9218 *attributes = NULL_TREE;
9219 bad = true;
9222 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9223 *attributes = c_parser_attributes (parser);
9225 /* If there were no attributes here, just report any earlier error. */
9226 if (*attributes == NULL_TREE || bad)
9227 return bad;
9229 /* If the attributes are followed by a ; or {, then just report any earlier
9230 error. */
9231 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9232 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9233 return bad;
9235 /* We've got attributes, but not at the end. */
9236 c_parser_error (parser,
9237 "expected %<;%> or %<{%> after method attribute definition");
9238 return true;
9241 /* Parse an objc-method-decl.
9243 objc-method-decl:
9244 ( objc-type-name ) objc-selector
9245 objc-selector
9246 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9247 objc-keyword-selector objc-optparmlist
9248 attributes
9250 objc-keyword-selector:
9251 objc-keyword-decl
9252 objc-keyword-selector objc-keyword-decl
9254 objc-keyword-decl:
9255 objc-selector : ( objc-type-name ) identifier
9256 objc-selector : identifier
9257 : ( objc-type-name ) identifier
9258 : identifier
9260 objc-optparmlist:
9261 objc-optparms objc-optellipsis
9263 objc-optparms:
9264 empty
9265 objc-opt-parms , parameter-declaration
9267 objc-optellipsis:
9268 empty
9269 , ...
9272 static tree
9273 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9274 tree *attributes, tree *expr)
9276 tree type = NULL_TREE;
9277 tree sel;
9278 tree parms = NULL_TREE;
9279 bool ellipsis = false;
9280 bool attr_err = false;
9282 *attributes = NULL_TREE;
9283 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9285 c_parser_consume_token (parser);
9286 type = c_parser_objc_type_name (parser);
9287 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9289 sel = c_parser_objc_selector (parser);
9290 /* If there is no selector, or a colon follows, we have an
9291 objc-keyword-selector. If there is a selector, and a colon does
9292 not follow, that selector ends the objc-method-decl. */
9293 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9295 tree tsel = sel;
9296 tree list = NULL_TREE;
9297 while (true)
9299 tree atype = NULL_TREE, id, keyworddecl;
9300 tree param_attr = NULL_TREE;
9301 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9302 break;
9303 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9305 c_parser_consume_token (parser);
9306 atype = c_parser_objc_type_name (parser);
9307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9308 "expected %<)%>");
9310 /* New ObjC allows attributes on method parameters. */
9311 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9312 param_attr = c_parser_attributes (parser);
9313 if (c_parser_next_token_is_not (parser, CPP_NAME))
9315 c_parser_error (parser, "expected identifier");
9316 return error_mark_node;
9318 id = c_parser_peek_token (parser)->value;
9319 c_parser_consume_token (parser);
9320 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9321 list = chainon (list, keyworddecl);
9322 tsel = c_parser_objc_selector (parser);
9323 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9324 break;
9327 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9329 /* Parse the optional parameter list. Optional Objective-C
9330 method parameters follow the C syntax, and may include '...'
9331 to denote a variable number of arguments. */
9332 parms = make_node (TREE_LIST);
9333 while (c_parser_next_token_is (parser, CPP_COMMA))
9335 struct c_parm *parm;
9336 c_parser_consume_token (parser);
9337 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9339 ellipsis = true;
9340 c_parser_consume_token (parser);
9341 attr_err |= c_parser_objc_maybe_method_attributes
9342 (parser, attributes) ;
9343 break;
9345 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9346 if (parm == NULL)
9347 break;
9348 parms = chainon (parms,
9349 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9351 sel = list;
9353 else
9354 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9356 if (sel == NULL)
9358 c_parser_error (parser, "objective-c method declaration is expected");
9359 return error_mark_node;
9362 if (attr_err)
9363 return error_mark_node;
9365 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9368 /* Parse an objc-type-name.
9370 objc-type-name:
9371 objc-type-qualifiers[opt] type-name
9372 objc-type-qualifiers[opt]
9374 objc-type-qualifiers:
9375 objc-type-qualifier
9376 objc-type-qualifiers objc-type-qualifier
9378 objc-type-qualifier: one of
9379 in out inout bycopy byref oneway
9382 static tree
9383 c_parser_objc_type_name (c_parser *parser)
9385 tree quals = NULL_TREE;
9386 struct c_type_name *type_name = NULL;
9387 tree type = NULL_TREE;
9388 while (true)
9390 c_token *token = c_parser_peek_token (parser);
9391 if (token->type == CPP_KEYWORD
9392 && (token->keyword == RID_IN
9393 || token->keyword == RID_OUT
9394 || token->keyword == RID_INOUT
9395 || token->keyword == RID_BYCOPY
9396 || token->keyword == RID_BYREF
9397 || token->keyword == RID_ONEWAY))
9399 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9400 c_parser_consume_token (parser);
9402 else
9403 break;
9405 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9406 type_name = c_parser_type_name (parser);
9407 if (type_name)
9408 type = groktypename (type_name, NULL, NULL);
9410 /* If the type is unknown, and error has already been produced and
9411 we need to recover from the error. In that case, use NULL_TREE
9412 for the type, as if no type had been specified; this will use the
9413 default type ('id') which is good for error recovery. */
9414 if (type == error_mark_node)
9415 type = NULL_TREE;
9417 return build_tree_list (quals, type);
9420 /* Parse objc-protocol-refs.
9422 objc-protocol-refs:
9423 < identifier-list >
9426 static tree
9427 c_parser_objc_protocol_refs (c_parser *parser)
9429 tree list = NULL_TREE;
9430 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9431 c_parser_consume_token (parser);
9432 /* Any identifiers, including those declared as type names, are OK
9433 here. */
9434 while (true)
9436 tree id;
9437 if (c_parser_next_token_is_not (parser, CPP_NAME))
9439 c_parser_error (parser, "expected identifier");
9440 break;
9442 id = c_parser_peek_token (parser)->value;
9443 list = chainon (list, build_tree_list (NULL_TREE, id));
9444 c_parser_consume_token (parser);
9445 if (c_parser_next_token_is (parser, CPP_COMMA))
9446 c_parser_consume_token (parser);
9447 else
9448 break;
9450 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9451 return list;
9454 /* Parse an objc-try-catch-finally-statement.
9456 objc-try-catch-finally-statement:
9457 @try compound-statement objc-catch-list[opt]
9458 @try compound-statement objc-catch-list[opt] @finally compound-statement
9460 objc-catch-list:
9461 @catch ( objc-catch-parameter-declaration ) compound-statement
9462 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9464 objc-catch-parameter-declaration:
9465 parameter-declaration
9466 '...'
9468 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9470 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9471 for C++. Keep them in sync. */
9473 static void
9474 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9476 location_t location;
9477 tree stmt;
9479 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9480 c_parser_consume_token (parser);
9481 location = c_parser_peek_token (parser)->location;
9482 objc_maybe_warn_exceptions (location);
9483 stmt = c_parser_compound_statement (parser);
9484 objc_begin_try_stmt (location, stmt);
9486 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9488 struct c_parm *parm;
9489 tree parameter_declaration = error_mark_node;
9490 bool seen_open_paren = false;
9492 c_parser_consume_token (parser);
9493 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9494 seen_open_paren = true;
9495 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9497 /* We have "@catch (...)" (where the '...' are literally
9498 what is in the code). Skip the '...'.
9499 parameter_declaration is set to NULL_TREE, and
9500 objc_being_catch_clauses() knows that that means
9501 '...'. */
9502 c_parser_consume_token (parser);
9503 parameter_declaration = NULL_TREE;
9505 else
9507 /* We have "@catch (NSException *exception)" or something
9508 like that. Parse the parameter declaration. */
9509 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9510 if (parm == NULL)
9511 parameter_declaration = error_mark_node;
9512 else
9513 parameter_declaration = grokparm (parm, NULL);
9515 if (seen_open_paren)
9516 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9517 else
9519 /* If there was no open parenthesis, we are recovering from
9520 an error, and we are trying to figure out what mistake
9521 the user has made. */
9523 /* If there is an immediate closing parenthesis, the user
9524 probably forgot the opening one (ie, they typed "@catch
9525 NSException *e)". Parse the closing parenthesis and keep
9526 going. */
9527 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9528 c_parser_consume_token (parser);
9530 /* If these is no immediate closing parenthesis, the user
9531 probably doesn't know that parenthesis are required at
9532 all (ie, they typed "@catch NSException *e"). So, just
9533 forget about the closing parenthesis and keep going. */
9535 objc_begin_catch_clause (parameter_declaration);
9536 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9537 c_parser_compound_statement_nostart (parser);
9538 objc_finish_catch_clause ();
9540 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9542 c_parser_consume_token (parser);
9543 location = c_parser_peek_token (parser)->location;
9544 stmt = c_parser_compound_statement (parser);
9545 objc_build_finally_clause (location, stmt);
9547 objc_finish_try_stmt ();
9550 /* Parse an objc-synchronized-statement.
9552 objc-synchronized-statement:
9553 @synchronized ( expression ) compound-statement
9556 static void
9557 c_parser_objc_synchronized_statement (c_parser *parser)
9559 location_t loc;
9560 tree expr, stmt;
9561 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9562 c_parser_consume_token (parser);
9563 loc = c_parser_peek_token (parser)->location;
9564 objc_maybe_warn_exceptions (loc);
9565 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9567 struct c_expr ce = c_parser_expression (parser);
9568 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9569 expr = ce.value;
9570 expr = c_fully_fold (expr, false, NULL);
9571 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9573 else
9574 expr = error_mark_node;
9575 stmt = c_parser_compound_statement (parser);
9576 objc_build_synchronized (loc, expr, stmt);
9579 /* Parse an objc-selector; return NULL_TREE without an error if the
9580 next token is not an objc-selector.
9582 objc-selector:
9583 identifier
9584 one of
9585 enum struct union if else while do for switch case default
9586 break continue return goto asm sizeof typeof __alignof
9587 unsigned long const short volatile signed restrict _Complex
9588 in out inout bycopy byref oneway int char float double void _Bool
9589 _Atomic
9591 ??? Why this selection of keywords but not, for example, storage
9592 class specifiers? */
9594 static tree
9595 c_parser_objc_selector (c_parser *parser)
9597 c_token *token = c_parser_peek_token (parser);
9598 tree value = token->value;
9599 if (token->type == CPP_NAME)
9601 c_parser_consume_token (parser);
9602 return value;
9604 if (token->type != CPP_KEYWORD)
9605 return NULL_TREE;
9606 switch (token->keyword)
9608 case RID_ENUM:
9609 case RID_STRUCT:
9610 case RID_UNION:
9611 case RID_IF:
9612 case RID_ELSE:
9613 case RID_WHILE:
9614 case RID_DO:
9615 case RID_FOR:
9616 case RID_SWITCH:
9617 case RID_CASE:
9618 case RID_DEFAULT:
9619 case RID_BREAK:
9620 case RID_CONTINUE:
9621 case RID_RETURN:
9622 case RID_GOTO:
9623 case RID_ASM:
9624 case RID_SIZEOF:
9625 case RID_TYPEOF:
9626 case RID_ALIGNOF:
9627 case RID_UNSIGNED:
9628 case RID_LONG:
9629 case RID_CONST:
9630 case RID_SHORT:
9631 case RID_VOLATILE:
9632 case RID_SIGNED:
9633 case RID_RESTRICT:
9634 case RID_COMPLEX:
9635 case RID_IN:
9636 case RID_OUT:
9637 case RID_INOUT:
9638 case RID_BYCOPY:
9639 case RID_BYREF:
9640 case RID_ONEWAY:
9641 case RID_INT:
9642 case RID_CHAR:
9643 case RID_FLOAT:
9644 case RID_DOUBLE:
9645 CASE_RID_FLOATN_NX:
9646 case RID_VOID:
9647 case RID_BOOL:
9648 case RID_ATOMIC:
9649 case RID_AUTO_TYPE:
9650 case RID_INT_N_0:
9651 case RID_INT_N_1:
9652 case RID_INT_N_2:
9653 case RID_INT_N_3:
9654 c_parser_consume_token (parser);
9655 return value;
9656 default:
9657 return NULL_TREE;
9661 /* Parse an objc-selector-arg.
9663 objc-selector-arg:
9664 objc-selector
9665 objc-keywordname-list
9667 objc-keywordname-list:
9668 objc-keywordname
9669 objc-keywordname-list objc-keywordname
9671 objc-keywordname:
9672 objc-selector :
9676 static tree
9677 c_parser_objc_selector_arg (c_parser *parser)
9679 tree sel = c_parser_objc_selector (parser);
9680 tree list = NULL_TREE;
9681 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9682 return sel;
9683 while (true)
9685 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9686 return list;
9687 list = chainon (list, build_tree_list (sel, NULL_TREE));
9688 sel = c_parser_objc_selector (parser);
9689 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9690 break;
9692 return list;
9695 /* Parse an objc-receiver.
9697 objc-receiver:
9698 expression
9699 class-name
9700 type-name
9703 static tree
9704 c_parser_objc_receiver (c_parser *parser)
9706 location_t loc = c_parser_peek_token (parser)->location;
9708 if (c_parser_peek_token (parser)->type == CPP_NAME
9709 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9710 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9712 tree id = c_parser_peek_token (parser)->value;
9713 c_parser_consume_token (parser);
9714 return objc_get_class_reference (id);
9716 struct c_expr ce = c_parser_expression (parser);
9717 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9718 return c_fully_fold (ce.value, false, NULL);
9721 /* Parse objc-message-args.
9723 objc-message-args:
9724 objc-selector
9725 objc-keywordarg-list
9727 objc-keywordarg-list:
9728 objc-keywordarg
9729 objc-keywordarg-list objc-keywordarg
9731 objc-keywordarg:
9732 objc-selector : objc-keywordexpr
9733 : objc-keywordexpr
9736 static tree
9737 c_parser_objc_message_args (c_parser *parser)
9739 tree sel = c_parser_objc_selector (parser);
9740 tree list = NULL_TREE;
9741 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9742 return sel;
9743 while (true)
9745 tree keywordexpr;
9746 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9747 return error_mark_node;
9748 keywordexpr = c_parser_objc_keywordexpr (parser);
9749 list = chainon (list, build_tree_list (sel, keywordexpr));
9750 sel = c_parser_objc_selector (parser);
9751 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9752 break;
9754 return list;
9757 /* Parse an objc-keywordexpr.
9759 objc-keywordexpr:
9760 nonempty-expr-list
9763 static tree
9764 c_parser_objc_keywordexpr (c_parser *parser)
9766 tree ret;
9767 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9768 NULL, NULL, NULL, NULL);
9769 if (vec_safe_length (expr_list) == 1)
9771 /* Just return the expression, remove a level of
9772 indirection. */
9773 ret = (*expr_list)[0];
9775 else
9777 /* We have a comma expression, we will collapse later. */
9778 ret = build_tree_list_vec (expr_list);
9780 release_tree_vector (expr_list);
9781 return ret;
9784 /* A check, needed in several places, that ObjC interface, implementation or
9785 method definitions are not prefixed by incorrect items. */
9786 static bool
9787 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9788 struct c_declspecs *specs)
9790 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9791 || specs->typespec_kind != ctsk_none)
9793 c_parser_error (parser,
9794 "no type or storage class may be specified here,");
9795 c_parser_skip_to_end_of_block_or_statement (parser);
9796 return true;
9798 return false;
9801 /* Parse an Objective-C @property declaration. The syntax is:
9803 objc-property-declaration:
9804 '@property' objc-property-attributes[opt] struct-declaration ;
9806 objc-property-attributes:
9807 '(' objc-property-attribute-list ')'
9809 objc-property-attribute-list:
9810 objc-property-attribute
9811 objc-property-attribute-list, objc-property-attribute
9813 objc-property-attribute
9814 'getter' = identifier
9815 'setter' = identifier
9816 'readonly'
9817 'readwrite'
9818 'assign'
9819 'retain'
9820 'copy'
9821 'nonatomic'
9823 For example:
9824 @property NSString *name;
9825 @property (readonly) id object;
9826 @property (retain, nonatomic, getter=getTheName) id name;
9827 @property int a, b, c;
9829 PS: This function is identical to cp_parser_objc_at_propery_declaration
9830 for C++. Keep them in sync. */
9831 static void
9832 c_parser_objc_at_property_declaration (c_parser *parser)
9834 /* The following variables hold the attributes of the properties as
9835 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9836 seen. When we see an attribute, we set them to 'true' (if they
9837 are boolean properties) or to the identifier (if they have an
9838 argument, ie, for getter and setter). Note that here we only
9839 parse the list of attributes, check the syntax and accumulate the
9840 attributes that we find. objc_add_property_declaration() will
9841 then process the information. */
9842 bool property_assign = false;
9843 bool property_copy = false;
9844 tree property_getter_ident = NULL_TREE;
9845 bool property_nonatomic = false;
9846 bool property_readonly = false;
9847 bool property_readwrite = false;
9848 bool property_retain = false;
9849 tree property_setter_ident = NULL_TREE;
9851 /* 'properties' is the list of properties that we read. Usually a
9852 single one, but maybe more (eg, in "@property int a, b, c;" there
9853 are three). */
9854 tree properties;
9855 location_t loc;
9857 loc = c_parser_peek_token (parser)->location;
9858 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9860 c_parser_consume_token (parser); /* Eat '@property'. */
9862 /* Parse the optional attribute list... */
9863 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9865 /* Eat the '(' */
9866 c_parser_consume_token (parser);
9868 /* Property attribute keywords are valid now. */
9869 parser->objc_property_attr_context = true;
9871 while (true)
9873 bool syntax_error = false;
9874 c_token *token = c_parser_peek_token (parser);
9875 enum rid keyword;
9877 if (token->type != CPP_KEYWORD)
9879 if (token->type == CPP_CLOSE_PAREN)
9880 c_parser_error (parser, "expected identifier");
9881 else
9883 c_parser_consume_token (parser);
9884 c_parser_error (parser, "unknown property attribute");
9886 break;
9888 keyword = token->keyword;
9889 c_parser_consume_token (parser);
9890 switch (keyword)
9892 case RID_ASSIGN: property_assign = true; break;
9893 case RID_COPY: property_copy = true; break;
9894 case RID_NONATOMIC: property_nonatomic = true; break;
9895 case RID_READONLY: property_readonly = true; break;
9896 case RID_READWRITE: property_readwrite = true; break;
9897 case RID_RETAIN: property_retain = true; break;
9899 case RID_GETTER:
9900 case RID_SETTER:
9901 if (c_parser_next_token_is_not (parser, CPP_EQ))
9903 if (keyword == RID_GETTER)
9904 c_parser_error (parser,
9905 "missing %<=%> (after %<getter%> attribute)");
9906 else
9907 c_parser_error (parser,
9908 "missing %<=%> (after %<setter%> attribute)");
9909 syntax_error = true;
9910 break;
9912 c_parser_consume_token (parser); /* eat the = */
9913 if (c_parser_next_token_is_not (parser, CPP_NAME))
9915 c_parser_error (parser, "expected identifier");
9916 syntax_error = true;
9917 break;
9919 if (keyword == RID_SETTER)
9921 if (property_setter_ident != NULL_TREE)
9922 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9923 else
9924 property_setter_ident = c_parser_peek_token (parser)->value;
9925 c_parser_consume_token (parser);
9926 if (c_parser_next_token_is_not (parser, CPP_COLON))
9927 c_parser_error (parser, "setter name must terminate with %<:%>");
9928 else
9929 c_parser_consume_token (parser);
9931 else
9933 if (property_getter_ident != NULL_TREE)
9934 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9935 else
9936 property_getter_ident = c_parser_peek_token (parser)->value;
9937 c_parser_consume_token (parser);
9939 break;
9940 default:
9941 c_parser_error (parser, "unknown property attribute");
9942 syntax_error = true;
9943 break;
9946 if (syntax_error)
9947 break;
9949 if (c_parser_next_token_is (parser, CPP_COMMA))
9950 c_parser_consume_token (parser);
9951 else
9952 break;
9954 parser->objc_property_attr_context = false;
9955 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9957 /* ... and the property declaration(s). */
9958 properties = c_parser_struct_declaration (parser);
9960 if (properties == error_mark_node)
9962 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9963 parser->error = false;
9964 return;
9967 if (properties == NULL_TREE)
9968 c_parser_error (parser, "expected identifier");
9969 else
9971 /* Comma-separated properties are chained together in
9972 reverse order; add them one by one. */
9973 properties = nreverse (properties);
9975 for (; properties; properties = TREE_CHAIN (properties))
9976 objc_add_property_declaration (loc, copy_node (properties),
9977 property_readonly, property_readwrite,
9978 property_assign, property_retain,
9979 property_copy, property_nonatomic,
9980 property_getter_ident, property_setter_ident);
9983 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9984 parser->error = false;
9987 /* Parse an Objective-C @synthesize declaration. The syntax is:
9989 objc-synthesize-declaration:
9990 @synthesize objc-synthesize-identifier-list ;
9992 objc-synthesize-identifier-list:
9993 objc-synthesize-identifier
9994 objc-synthesize-identifier-list, objc-synthesize-identifier
9996 objc-synthesize-identifier
9997 identifier
9998 identifier = identifier
10000 For example:
10001 @synthesize MyProperty;
10002 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10004 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10005 for C++. Keep them in sync.
10007 static void
10008 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10010 tree list = NULL_TREE;
10011 location_t loc;
10012 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10013 loc = c_parser_peek_token (parser)->location;
10015 c_parser_consume_token (parser);
10016 while (true)
10018 tree property, ivar;
10019 if (c_parser_next_token_is_not (parser, CPP_NAME))
10021 c_parser_error (parser, "expected identifier");
10022 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10023 /* Once we find the semicolon, we can resume normal parsing.
10024 We have to reset parser->error manually because
10025 c_parser_skip_until_found() won't reset it for us if the
10026 next token is precisely a semicolon. */
10027 parser->error = false;
10028 return;
10030 property = c_parser_peek_token (parser)->value;
10031 c_parser_consume_token (parser);
10032 if (c_parser_next_token_is (parser, CPP_EQ))
10034 c_parser_consume_token (parser);
10035 if (c_parser_next_token_is_not (parser, CPP_NAME))
10037 c_parser_error (parser, "expected identifier");
10038 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10039 parser->error = false;
10040 return;
10042 ivar = c_parser_peek_token (parser)->value;
10043 c_parser_consume_token (parser);
10045 else
10046 ivar = NULL_TREE;
10047 list = chainon (list, build_tree_list (ivar, property));
10048 if (c_parser_next_token_is (parser, CPP_COMMA))
10049 c_parser_consume_token (parser);
10050 else
10051 break;
10053 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10054 objc_add_synthesize_declaration (loc, list);
10057 /* Parse an Objective-C @dynamic declaration. The syntax is:
10059 objc-dynamic-declaration:
10060 @dynamic identifier-list ;
10062 For example:
10063 @dynamic MyProperty;
10064 @dynamic MyProperty, AnotherProperty;
10066 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10067 for C++. Keep them in sync.
10069 static void
10070 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10072 tree list = NULL_TREE;
10073 location_t loc;
10074 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10075 loc = c_parser_peek_token (parser)->location;
10077 c_parser_consume_token (parser);
10078 while (true)
10080 tree property;
10081 if (c_parser_next_token_is_not (parser, CPP_NAME))
10083 c_parser_error (parser, "expected identifier");
10084 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10085 parser->error = false;
10086 return;
10088 property = c_parser_peek_token (parser)->value;
10089 list = chainon (list, build_tree_list (NULL_TREE, property));
10090 c_parser_consume_token (parser);
10091 if (c_parser_next_token_is (parser, CPP_COMMA))
10092 c_parser_consume_token (parser);
10093 else
10094 break;
10096 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10097 objc_add_dynamic_declaration (loc, list);
10101 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10102 should be considered, statements. ALLOW_STMT is true if we're within
10103 the context of a function and such pragmas are to be allowed. Returns
10104 true if we actually parsed such a pragma. */
10106 static bool
10107 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10109 unsigned int id;
10111 id = c_parser_peek_token (parser)->pragma_kind;
10112 gcc_assert (id != PRAGMA_NONE);
10114 switch (id)
10116 case PRAGMA_OACC_DECLARE:
10117 c_parser_oacc_declare (parser);
10118 return false;
10120 case PRAGMA_OACC_ENTER_DATA:
10121 c_parser_oacc_enter_exit_data (parser, true);
10122 return false;
10124 case PRAGMA_OACC_EXIT_DATA:
10125 c_parser_oacc_enter_exit_data (parser, false);
10126 return false;
10128 case PRAGMA_OACC_ROUTINE:
10129 if (context != pragma_external)
10131 error_at (c_parser_peek_token (parser)->location,
10132 "%<#pragma acc routine%> must be at file scope");
10133 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10134 return false;
10136 c_parser_oacc_routine (parser, context);
10137 return false;
10139 case PRAGMA_OACC_UPDATE:
10140 if (context != pragma_compound)
10142 if (context == pragma_stmt)
10143 c_parser_error (parser, "%<#pragma acc update%> may only be "
10144 "used in compound statements");
10145 goto bad_stmt;
10147 c_parser_oacc_update (parser);
10148 return false;
10150 case PRAGMA_OMP_BARRIER:
10151 if (context != pragma_compound)
10153 if (context == pragma_stmt)
10154 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
10155 "used in compound statements");
10156 goto bad_stmt;
10158 c_parser_omp_barrier (parser);
10159 return false;
10161 case PRAGMA_OMP_FLUSH:
10162 if (context != pragma_compound)
10164 if (context == pragma_stmt)
10165 c_parser_error (parser, "%<#pragma omp flush%> may only be "
10166 "used in compound statements");
10167 goto bad_stmt;
10169 c_parser_omp_flush (parser);
10170 return false;
10172 case PRAGMA_OMP_TASKWAIT:
10173 if (context != pragma_compound)
10175 if (context == pragma_stmt)
10176 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
10177 "used in compound statements");
10178 goto bad_stmt;
10180 c_parser_omp_taskwait (parser);
10181 return false;
10183 case PRAGMA_OMP_TASKYIELD:
10184 if (context != pragma_compound)
10186 if (context == pragma_stmt)
10187 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
10188 "used in compound statements");
10189 goto bad_stmt;
10191 c_parser_omp_taskyield (parser);
10192 return false;
10194 case PRAGMA_OMP_CANCEL:
10195 if (context != pragma_compound)
10197 if (context == pragma_stmt)
10198 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
10199 "used in compound statements");
10200 goto bad_stmt;
10202 c_parser_omp_cancel (parser);
10203 return false;
10205 case PRAGMA_OMP_CANCELLATION_POINT:
10206 c_parser_omp_cancellation_point (parser, context);
10207 return false;
10209 case PRAGMA_OMP_THREADPRIVATE:
10210 c_parser_omp_threadprivate (parser);
10211 return false;
10213 case PRAGMA_OMP_TARGET:
10214 return c_parser_omp_target (parser, context, if_p);
10216 case PRAGMA_OMP_END_DECLARE_TARGET:
10217 c_parser_omp_end_declare_target (parser);
10218 return false;
10220 case PRAGMA_OMP_SECTION:
10221 error_at (c_parser_peek_token (parser)->location,
10222 "%<#pragma omp section%> may only be used in "
10223 "%<#pragma omp sections%> construct");
10224 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10225 return false;
10227 case PRAGMA_OMP_DECLARE:
10228 c_parser_omp_declare (parser, context);
10229 return false;
10231 case PRAGMA_OMP_ORDERED:
10232 return c_parser_omp_ordered (parser, context, if_p);
10234 case PRAGMA_IVDEP:
10235 c_parser_consume_pragma (parser);
10236 c_parser_skip_to_pragma_eol (parser);
10237 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10238 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10239 && !c_parser_next_token_is_keyword (parser, RID_DO))
10241 c_parser_error (parser, "for, while or do statement expected");
10242 return false;
10244 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10245 c_parser_for_statement (parser, true, if_p);
10246 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10247 c_parser_while_statement (parser, true, if_p);
10248 else
10249 c_parser_do_statement (parser, true);
10250 return false;
10252 case PRAGMA_GCC_PCH_PREPROCESS:
10253 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10254 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10255 return false;
10257 case PRAGMA_CILK_SIMD:
10258 if (!c_parser_cilk_verify_simd (parser, context))
10259 return false;
10260 c_parser_consume_pragma (parser);
10261 c_parser_cilk_simd (parser, if_p);
10262 return false;
10263 case PRAGMA_CILK_GRAINSIZE:
10264 if (!flag_cilkplus)
10266 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10267 " enabled");
10268 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10269 return false;
10271 if (context == pragma_external)
10273 error_at (c_parser_peek_token (parser)->location,
10274 "%<#pragma grainsize%> must be inside a function");
10275 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10276 return false;
10278 c_parser_cilk_grainsize (parser, if_p);
10279 return false;
10281 default:
10282 if (id < PRAGMA_FIRST_EXTERNAL)
10284 if (context != pragma_stmt && context != pragma_compound)
10286 bad_stmt:
10287 c_parser_error (parser, "expected declaration specifiers");
10288 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10289 return false;
10291 c_parser_omp_construct (parser, if_p);
10292 return true;
10294 break;
10297 c_parser_consume_pragma (parser);
10298 c_invoke_pragma_handler (id);
10300 /* Skip to EOL, but suppress any error message. Those will have been
10301 generated by the handler routine through calling error, as opposed
10302 to calling c_parser_error. */
10303 parser->error = true;
10304 c_parser_skip_to_pragma_eol (parser);
10306 return false;
10309 /* The interface the pragma parsers have to the lexer. */
10311 enum cpp_ttype
10312 pragma_lex (tree *value, location_t *loc)
10314 c_token *tok = c_parser_peek_token (the_parser);
10315 enum cpp_ttype ret = tok->type;
10317 *value = tok->value;
10318 if (loc)
10319 *loc = tok->location;
10321 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10322 ret = CPP_EOF;
10323 else
10325 if (ret == CPP_KEYWORD)
10326 ret = CPP_NAME;
10327 c_parser_consume_token (the_parser);
10330 return ret;
10333 static void
10334 c_parser_pragma_pch_preprocess (c_parser *parser)
10336 tree name = NULL;
10338 c_parser_consume_pragma (parser);
10339 if (c_parser_next_token_is (parser, CPP_STRING))
10341 name = c_parser_peek_token (parser)->value;
10342 c_parser_consume_token (parser);
10344 else
10345 c_parser_error (parser, "expected string literal");
10346 c_parser_skip_to_pragma_eol (parser);
10348 if (name)
10349 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10352 /* OpenACC and OpenMP parsing routines. */
10354 /* Returns name of the next clause.
10355 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10356 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10357 returned and the token is consumed. */
10359 static pragma_omp_clause
10360 c_parser_omp_clause_name (c_parser *parser)
10362 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10364 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10365 result = PRAGMA_OACC_CLAUSE_AUTO;
10366 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10367 result = PRAGMA_OMP_CLAUSE_IF;
10368 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10369 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10370 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10371 result = PRAGMA_OMP_CLAUSE_FOR;
10372 else if (c_parser_next_token_is (parser, CPP_NAME))
10374 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10376 switch (p[0])
10378 case 'a':
10379 if (!strcmp ("aligned", p))
10380 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10381 else if (!strcmp ("async", p))
10382 result = PRAGMA_OACC_CLAUSE_ASYNC;
10383 break;
10384 case 'c':
10385 if (!strcmp ("collapse", p))
10386 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10387 else if (!strcmp ("copy", p))
10388 result = PRAGMA_OACC_CLAUSE_COPY;
10389 else if (!strcmp ("copyin", p))
10390 result = PRAGMA_OMP_CLAUSE_COPYIN;
10391 else if (!strcmp ("copyout", p))
10392 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10393 else if (!strcmp ("copyprivate", p))
10394 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10395 else if (!strcmp ("create", p))
10396 result = PRAGMA_OACC_CLAUSE_CREATE;
10397 break;
10398 case 'd':
10399 if (!strcmp ("defaultmap", p))
10400 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10401 else if (!strcmp ("delete", p))
10402 result = PRAGMA_OACC_CLAUSE_DELETE;
10403 else if (!strcmp ("depend", p))
10404 result = PRAGMA_OMP_CLAUSE_DEPEND;
10405 else if (!strcmp ("device", p))
10406 result = PRAGMA_OMP_CLAUSE_DEVICE;
10407 else if (!strcmp ("deviceptr", p))
10408 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10409 else if (!strcmp ("device_resident", p))
10410 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10411 else if (!strcmp ("dist_schedule", p))
10412 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10413 break;
10414 case 'f':
10415 if (!strcmp ("final", p))
10416 result = PRAGMA_OMP_CLAUSE_FINAL;
10417 else if (!strcmp ("firstprivate", p))
10418 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10419 else if (!strcmp ("from", p))
10420 result = PRAGMA_OMP_CLAUSE_FROM;
10421 break;
10422 case 'g':
10423 if (!strcmp ("gang", p))
10424 result = PRAGMA_OACC_CLAUSE_GANG;
10425 else if (!strcmp ("grainsize", p))
10426 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10427 break;
10428 case 'h':
10429 if (!strcmp ("hint", p))
10430 result = PRAGMA_OMP_CLAUSE_HINT;
10431 else if (!strcmp ("host", p))
10432 result = PRAGMA_OACC_CLAUSE_HOST;
10433 break;
10434 case 'i':
10435 if (!strcmp ("inbranch", p))
10436 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10437 else if (!strcmp ("independent", p))
10438 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10439 else if (!strcmp ("is_device_ptr", p))
10440 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10441 break;
10442 case 'l':
10443 if (!strcmp ("lastprivate", p))
10444 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10445 else if (!strcmp ("linear", p))
10446 result = PRAGMA_OMP_CLAUSE_LINEAR;
10447 else if (!strcmp ("link", p))
10448 result = PRAGMA_OMP_CLAUSE_LINK;
10449 break;
10450 case 'm':
10451 if (!strcmp ("map", p))
10452 result = PRAGMA_OMP_CLAUSE_MAP;
10453 else if (!strcmp ("mergeable", p))
10454 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10455 else if (flag_cilkplus && !strcmp ("mask", p))
10456 result = PRAGMA_CILK_CLAUSE_MASK;
10457 break;
10458 case 'n':
10459 if (!strcmp ("nogroup", p))
10460 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10461 else if (!strcmp ("notinbranch", p))
10462 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10463 else if (!strcmp ("nowait", p))
10464 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10465 else if (!strcmp ("num_gangs", p))
10466 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10467 else if (!strcmp ("num_tasks", p))
10468 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10469 else if (!strcmp ("num_teams", p))
10470 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10471 else if (!strcmp ("num_threads", p))
10472 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10473 else if (!strcmp ("num_workers", p))
10474 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10475 else if (flag_cilkplus && !strcmp ("nomask", p))
10476 result = PRAGMA_CILK_CLAUSE_NOMASK;
10477 break;
10478 case 'o':
10479 if (!strcmp ("ordered", p))
10480 result = PRAGMA_OMP_CLAUSE_ORDERED;
10481 break;
10482 case 'p':
10483 if (!strcmp ("parallel", p))
10484 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10485 else if (!strcmp ("present", p))
10486 result = PRAGMA_OACC_CLAUSE_PRESENT;
10487 else if (!strcmp ("present_or_copy", p)
10488 || !strcmp ("pcopy", p))
10489 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10490 else if (!strcmp ("present_or_copyin", p)
10491 || !strcmp ("pcopyin", p))
10492 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10493 else if (!strcmp ("present_or_copyout", p)
10494 || !strcmp ("pcopyout", p))
10495 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10496 else if (!strcmp ("present_or_create", p)
10497 || !strcmp ("pcreate", p))
10498 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10499 else if (!strcmp ("priority", p))
10500 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10501 else if (!strcmp ("private", p))
10502 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10503 else if (!strcmp ("proc_bind", p))
10504 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10505 break;
10506 case 'r':
10507 if (!strcmp ("reduction", p))
10508 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10509 break;
10510 case 's':
10511 if (!strcmp ("safelen", p))
10512 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10513 else if (!strcmp ("schedule", p))
10514 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10515 else if (!strcmp ("sections", p))
10516 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10517 else if (!strcmp ("seq", p))
10518 result = PRAGMA_OACC_CLAUSE_SEQ;
10519 else if (!strcmp ("shared", p))
10520 result = PRAGMA_OMP_CLAUSE_SHARED;
10521 else if (!strcmp ("simd", p))
10522 result = PRAGMA_OMP_CLAUSE_SIMD;
10523 else if (!strcmp ("simdlen", p))
10524 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10525 else if (!strcmp ("self", p))
10526 result = PRAGMA_OACC_CLAUSE_SELF;
10527 break;
10528 case 't':
10529 if (!strcmp ("taskgroup", p))
10530 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10531 else if (!strcmp ("thread_limit", p))
10532 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10533 else if (!strcmp ("threads", p))
10534 result = PRAGMA_OMP_CLAUSE_THREADS;
10535 else if (!strcmp ("tile", p))
10536 result = PRAGMA_OACC_CLAUSE_TILE;
10537 else if (!strcmp ("to", p))
10538 result = PRAGMA_OMP_CLAUSE_TO;
10539 break;
10540 case 'u':
10541 if (!strcmp ("uniform", p))
10542 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10543 else if (!strcmp ("untied", p))
10544 result = PRAGMA_OMP_CLAUSE_UNTIED;
10545 else if (!strcmp ("use_device", p))
10546 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10547 else if (!strcmp ("use_device_ptr", p))
10548 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10549 break;
10550 case 'v':
10551 if (!strcmp ("vector", p))
10552 result = PRAGMA_OACC_CLAUSE_VECTOR;
10553 else if (!strcmp ("vector_length", p))
10554 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10555 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10556 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10557 break;
10558 case 'w':
10559 if (!strcmp ("wait", p))
10560 result = PRAGMA_OACC_CLAUSE_WAIT;
10561 else if (!strcmp ("worker", p))
10562 result = PRAGMA_OACC_CLAUSE_WORKER;
10563 break;
10567 if (result != PRAGMA_OMP_CLAUSE_NONE)
10568 c_parser_consume_token (parser);
10570 return result;
10573 /* Validate that a clause of the given type does not already exist. */
10575 static void
10576 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10577 const char *name)
10579 tree c;
10581 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10582 if (OMP_CLAUSE_CODE (c) == code)
10584 location_t loc = OMP_CLAUSE_LOCATION (c);
10585 error_at (loc, "too many %qs clauses", name);
10586 break;
10590 /* OpenACC 2.0
10591 Parse wait clause or wait directive parameters. */
10593 static tree
10594 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10596 vec<tree, va_gc> *args;
10597 tree t, args_tree;
10599 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10600 return list;
10602 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10604 if (args->length () == 0)
10606 c_parser_error (parser, "expected integer expression before ')'");
10607 release_tree_vector (args);
10608 return list;
10611 args_tree = build_tree_list_vec (args);
10613 for (t = args_tree; t; t = TREE_CHAIN (t))
10615 tree targ = TREE_VALUE (t);
10617 if (targ != error_mark_node)
10619 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10621 c_parser_error (parser, "expression must be integral");
10622 targ = error_mark_node;
10624 else
10626 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10628 OMP_CLAUSE_DECL (c) = targ;
10629 OMP_CLAUSE_CHAIN (c) = list;
10630 list = c;
10635 release_tree_vector (args);
10636 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10637 return list;
10640 /* OpenACC 2.0, OpenMP 2.5:
10641 variable-list:
10642 identifier
10643 variable-list , identifier
10645 If KIND is nonzero, create the appropriate node and install the
10646 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10647 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10649 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10650 return the list created. */
10652 static tree
10653 c_parser_omp_variable_list (c_parser *parser,
10654 location_t clause_loc,
10655 enum omp_clause_code kind, tree list)
10657 if (c_parser_next_token_is_not (parser, CPP_NAME)
10658 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10659 c_parser_error (parser, "expected identifier");
10661 while (c_parser_next_token_is (parser, CPP_NAME)
10662 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10664 tree t = lookup_name (c_parser_peek_token (parser)->value);
10666 if (t == NULL_TREE)
10668 undeclared_variable (c_parser_peek_token (parser)->location,
10669 c_parser_peek_token (parser)->value);
10670 t = error_mark_node;
10673 c_parser_consume_token (parser);
10675 if (t == error_mark_node)
10677 else if (kind != 0)
10679 switch (kind)
10681 case OMP_CLAUSE__CACHE_:
10682 /* The OpenACC cache directive explicitly only allows "array
10683 elements or subarrays". */
10684 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10686 c_parser_error (parser, "expected %<[%>");
10687 t = error_mark_node;
10688 break;
10690 /* FALLTHROUGH */
10691 case OMP_CLAUSE_MAP:
10692 case OMP_CLAUSE_FROM:
10693 case OMP_CLAUSE_TO:
10694 while (c_parser_next_token_is (parser, CPP_DOT))
10696 location_t op_loc = c_parser_peek_token (parser)->location;
10697 c_parser_consume_token (parser);
10698 if (!c_parser_next_token_is (parser, CPP_NAME))
10700 c_parser_error (parser, "expected identifier");
10701 t = error_mark_node;
10702 break;
10705 c_token *comp_tok = c_parser_peek_token (parser);
10706 tree ident = comp_tok->value;
10707 location_t comp_loc = comp_tok->location;
10708 c_parser_consume_token (parser);
10709 t = build_component_ref (op_loc, t, ident, comp_loc);
10711 /* FALLTHROUGH */
10712 case OMP_CLAUSE_DEPEND:
10713 case OMP_CLAUSE_REDUCTION:
10714 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10716 tree low_bound = NULL_TREE, length = NULL_TREE;
10718 c_parser_consume_token (parser);
10719 if (!c_parser_next_token_is (parser, CPP_COLON))
10721 low_bound = c_parser_expression (parser).value;
10722 mark_exp_read (low_bound);
10724 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10725 length = integer_one_node;
10726 else
10728 /* Look for `:'. */
10729 if (!c_parser_require (parser, CPP_COLON,
10730 "expected %<:%>"))
10732 t = error_mark_node;
10733 break;
10735 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10737 length = c_parser_expression (parser).value;
10738 mark_exp_read (length);
10741 /* Look for the closing `]'. */
10742 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10743 "expected %<]%>"))
10745 t = error_mark_node;
10746 break;
10749 t = tree_cons (low_bound, length, t);
10751 break;
10752 default:
10753 break;
10756 if (t != error_mark_node)
10758 tree u = build_omp_clause (clause_loc, kind);
10759 OMP_CLAUSE_DECL (u) = t;
10760 OMP_CLAUSE_CHAIN (u) = list;
10761 list = u;
10764 else
10765 list = tree_cons (t, NULL_TREE, list);
10767 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10768 break;
10770 c_parser_consume_token (parser);
10773 return list;
10776 /* Similarly, but expect leading and trailing parenthesis. This is a very
10777 common case for OpenACC and OpenMP clauses. */
10779 static tree
10780 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10781 tree list)
10783 /* The clauses location. */
10784 location_t loc = c_parser_peek_token (parser)->location;
10786 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10788 list = c_parser_omp_variable_list (parser, loc, kind, list);
10789 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10791 return list;
10794 /* OpenACC 2.0:
10795 copy ( variable-list )
10796 copyin ( variable-list )
10797 copyout ( variable-list )
10798 create ( variable-list )
10799 delete ( variable-list )
10800 present ( variable-list )
10801 present_or_copy ( variable-list )
10802 pcopy ( variable-list )
10803 present_or_copyin ( variable-list )
10804 pcopyin ( variable-list )
10805 present_or_copyout ( variable-list )
10806 pcopyout ( variable-list )
10807 present_or_create ( variable-list )
10808 pcreate ( variable-list ) */
10810 static tree
10811 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10812 tree list)
10814 enum gomp_map_kind kind;
10815 switch (c_kind)
10817 case PRAGMA_OACC_CLAUSE_COPY:
10818 kind = GOMP_MAP_FORCE_TOFROM;
10819 break;
10820 case PRAGMA_OACC_CLAUSE_COPYIN:
10821 kind = GOMP_MAP_FORCE_TO;
10822 break;
10823 case PRAGMA_OACC_CLAUSE_COPYOUT:
10824 kind = GOMP_MAP_FORCE_FROM;
10825 break;
10826 case PRAGMA_OACC_CLAUSE_CREATE:
10827 kind = GOMP_MAP_FORCE_ALLOC;
10828 break;
10829 case PRAGMA_OACC_CLAUSE_DELETE:
10830 kind = GOMP_MAP_DELETE;
10831 break;
10832 case PRAGMA_OACC_CLAUSE_DEVICE:
10833 kind = GOMP_MAP_FORCE_TO;
10834 break;
10835 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
10836 kind = GOMP_MAP_DEVICE_RESIDENT;
10837 break;
10838 case PRAGMA_OACC_CLAUSE_HOST:
10839 case PRAGMA_OACC_CLAUSE_SELF:
10840 kind = GOMP_MAP_FORCE_FROM;
10841 break;
10842 case PRAGMA_OACC_CLAUSE_LINK:
10843 kind = GOMP_MAP_LINK;
10844 break;
10845 case PRAGMA_OACC_CLAUSE_PRESENT:
10846 kind = GOMP_MAP_FORCE_PRESENT;
10847 break;
10848 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10849 kind = GOMP_MAP_TOFROM;
10850 break;
10851 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10852 kind = GOMP_MAP_TO;
10853 break;
10854 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10855 kind = GOMP_MAP_FROM;
10856 break;
10857 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10858 kind = GOMP_MAP_ALLOC;
10859 break;
10860 default:
10861 gcc_unreachable ();
10863 tree nl, c;
10864 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10866 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10867 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10869 return nl;
10872 /* OpenACC 2.0:
10873 deviceptr ( variable-list ) */
10875 static tree
10876 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10878 location_t loc = c_parser_peek_token (parser)->location;
10879 tree vars, t;
10881 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10882 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10883 variable-list must only allow for pointer variables. */
10884 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10885 for (t = vars; t && t; t = TREE_CHAIN (t))
10887 tree v = TREE_PURPOSE (t);
10889 /* FIXME diagnostics: Ideally we should keep individual
10890 locations for all the variables in the var list to make the
10891 following errors more precise. Perhaps
10892 c_parser_omp_var_list_parens() should construct a list of
10893 locations to go along with the var list. */
10895 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
10896 error_at (loc, "%qD is not a variable", v);
10897 else if (TREE_TYPE (v) == error_mark_node)
10899 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10900 error_at (loc, "%qD is not a pointer variable", v);
10902 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10903 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10904 OMP_CLAUSE_DECL (u) = v;
10905 OMP_CLAUSE_CHAIN (u) = list;
10906 list = u;
10909 return list;
10912 /* OpenACC 2.0, OpenMP 3.0:
10913 collapse ( constant-expression ) */
10915 static tree
10916 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10918 tree c, num = error_mark_node;
10919 HOST_WIDE_INT n;
10920 location_t loc;
10922 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10924 loc = c_parser_peek_token (parser)->location;
10925 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10927 num = c_parser_expr_no_commas (parser, NULL).value;
10928 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10930 if (num == error_mark_node)
10931 return list;
10932 mark_exp_read (num);
10933 num = c_fully_fold (num, false, NULL);
10934 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10935 || !tree_fits_shwi_p (num)
10936 || (n = tree_to_shwi (num)) <= 0
10937 || (int) n != n)
10939 error_at (loc,
10940 "collapse argument needs positive constant integer expression");
10941 return list;
10943 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10944 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10945 OMP_CLAUSE_CHAIN (c) = list;
10946 return c;
10949 /* OpenMP 2.5:
10950 copyin ( variable-list ) */
10952 static tree
10953 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10955 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10958 /* OpenMP 2.5:
10959 copyprivate ( variable-list ) */
10961 static tree
10962 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10964 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10967 /* OpenMP 2.5:
10968 default ( shared | none )
10970 OpenACC 2.0:
10971 default (none) */
10973 static tree
10974 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
10976 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10977 location_t loc = c_parser_peek_token (parser)->location;
10978 tree c;
10980 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10981 return list;
10982 if (c_parser_next_token_is (parser, CPP_NAME))
10984 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10986 switch (p[0])
10988 case 'n':
10989 if (strcmp ("none", p) != 0)
10990 goto invalid_kind;
10991 kind = OMP_CLAUSE_DEFAULT_NONE;
10992 break;
10994 case 's':
10995 if (strcmp ("shared", p) != 0 || is_oacc)
10996 goto invalid_kind;
10997 kind = OMP_CLAUSE_DEFAULT_SHARED;
10998 break;
11000 default:
11001 goto invalid_kind;
11004 c_parser_consume_token (parser);
11006 else
11008 invalid_kind:
11009 if (is_oacc)
11010 c_parser_error (parser, "expected %<none%>");
11011 else
11012 c_parser_error (parser, "expected %<none%> or %<shared%>");
11014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11016 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11017 return list;
11019 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11020 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11021 OMP_CLAUSE_CHAIN (c) = list;
11022 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11024 return c;
11027 /* OpenMP 2.5:
11028 firstprivate ( variable-list ) */
11030 static tree
11031 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11033 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11036 /* OpenMP 3.1:
11037 final ( expression ) */
11039 static tree
11040 c_parser_omp_clause_final (c_parser *parser, tree list)
11042 location_t loc = c_parser_peek_token (parser)->location;
11043 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11045 tree t = c_parser_paren_condition (parser);
11046 tree c;
11048 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11050 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11051 OMP_CLAUSE_FINAL_EXPR (c) = t;
11052 OMP_CLAUSE_CHAIN (c) = list;
11053 list = c;
11055 else
11056 c_parser_error (parser, "expected %<(%>");
11058 return list;
11061 /* OpenACC, OpenMP 2.5:
11062 if ( expression )
11064 OpenMP 4.5:
11065 if ( directive-name-modifier : expression )
11067 directive-name-modifier:
11068 parallel | task | taskloop | target data | target | target update
11069 | target enter data | target exit data */
11071 static tree
11072 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11074 location_t location = c_parser_peek_token (parser)->location;
11075 enum tree_code if_modifier = ERROR_MARK;
11077 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11078 return list;
11080 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11082 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11083 int n = 2;
11084 if (strcmp (p, "parallel") == 0)
11085 if_modifier = OMP_PARALLEL;
11086 else if (strcmp (p, "task") == 0)
11087 if_modifier = OMP_TASK;
11088 else if (strcmp (p, "taskloop") == 0)
11089 if_modifier = OMP_TASKLOOP;
11090 else if (strcmp (p, "target") == 0)
11092 if_modifier = OMP_TARGET;
11093 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11095 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11096 if (strcmp ("data", p) == 0)
11097 if_modifier = OMP_TARGET_DATA;
11098 else if (strcmp ("update", p) == 0)
11099 if_modifier = OMP_TARGET_UPDATE;
11100 else if (strcmp ("enter", p) == 0)
11101 if_modifier = OMP_TARGET_ENTER_DATA;
11102 else if (strcmp ("exit", p) == 0)
11103 if_modifier = OMP_TARGET_EXIT_DATA;
11104 if (if_modifier != OMP_TARGET)
11106 n = 3;
11107 c_parser_consume_token (parser);
11109 else
11111 location_t loc = c_parser_peek_2nd_token (parser)->location;
11112 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11113 "or %<exit%>");
11114 if_modifier = ERROR_MARK;
11116 if (if_modifier == OMP_TARGET_ENTER_DATA
11117 || if_modifier == OMP_TARGET_EXIT_DATA)
11119 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11121 p = IDENTIFIER_POINTER
11122 (c_parser_peek_2nd_token (parser)->value);
11123 if (strcmp ("data", p) == 0)
11124 n = 4;
11126 if (n == 4)
11127 c_parser_consume_token (parser);
11128 else
11130 location_t loc
11131 = c_parser_peek_2nd_token (parser)->location;
11132 error_at (loc, "expected %<data%>");
11133 if_modifier = ERROR_MARK;
11138 if (if_modifier != ERROR_MARK)
11140 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11142 c_parser_consume_token (parser);
11143 c_parser_consume_token (parser);
11145 else
11147 if (n > 2)
11149 location_t loc = c_parser_peek_2nd_token (parser)->location;
11150 error_at (loc, "expected %<:%>");
11152 if_modifier = ERROR_MARK;
11157 tree t = c_parser_condition (parser), c;
11158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11160 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11161 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11163 if (if_modifier != ERROR_MARK
11164 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11166 const char *p = NULL;
11167 switch (if_modifier)
11169 case OMP_PARALLEL: p = "parallel"; break;
11170 case OMP_TASK: p = "task"; break;
11171 case OMP_TASKLOOP: p = "taskloop"; break;
11172 case OMP_TARGET_DATA: p = "target data"; break;
11173 case OMP_TARGET: p = "target"; break;
11174 case OMP_TARGET_UPDATE: p = "target update"; break;
11175 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11176 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11177 default: gcc_unreachable ();
11179 error_at (location, "too many %<if%> clauses with %qs modifier",
11181 return list;
11183 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11185 if (!is_omp)
11186 error_at (location, "too many %<if%> clauses");
11187 else
11188 error_at (location, "too many %<if%> clauses without modifier");
11189 return list;
11191 else if (if_modifier == ERROR_MARK
11192 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11194 error_at (location, "if any %<if%> clause has modifier, then all "
11195 "%<if%> clauses have to use modifier");
11196 return list;
11200 c = build_omp_clause (location, OMP_CLAUSE_IF);
11201 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11202 OMP_CLAUSE_IF_EXPR (c) = t;
11203 OMP_CLAUSE_CHAIN (c) = list;
11204 return c;
11207 /* OpenMP 2.5:
11208 lastprivate ( variable-list ) */
11210 static tree
11211 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11213 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11216 /* OpenMP 3.1:
11217 mergeable */
11219 static tree
11220 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11222 tree c;
11224 /* FIXME: Should we allow duplicates? */
11225 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11227 c = build_omp_clause (c_parser_peek_token (parser)->location,
11228 OMP_CLAUSE_MERGEABLE);
11229 OMP_CLAUSE_CHAIN (c) = list;
11231 return c;
11234 /* OpenMP 2.5:
11235 nowait */
11237 static tree
11238 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11240 tree c;
11241 location_t loc = c_parser_peek_token (parser)->location;
11243 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11245 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11246 OMP_CLAUSE_CHAIN (c) = list;
11247 return c;
11250 /* OpenACC:
11251 num_gangs ( expression ) */
11253 static tree
11254 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11256 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11257 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11259 location_t expr_loc = c_parser_peek_token (parser)->location;
11260 tree c, t = c_parser_expression (parser).value;
11261 mark_exp_read (t);
11262 t = c_fully_fold (t, false, NULL);
11264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11266 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11268 c_parser_error (parser, "expected integer expression");
11269 return list;
11272 /* Attempt to statically determine when the number isn't positive. */
11273 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11274 build_int_cst (TREE_TYPE (t), 0));
11275 protected_set_expr_location (c, expr_loc);
11276 if (c == boolean_true_node)
11278 warning_at (expr_loc, 0,
11279 "%<num_gangs%> value must be positive");
11280 t = integer_one_node;
11283 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11285 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11286 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11287 OMP_CLAUSE_CHAIN (c) = list;
11288 list = c;
11291 return list;
11294 /* OpenMP 2.5:
11295 num_threads ( expression ) */
11297 static tree
11298 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11300 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11301 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11303 location_t expr_loc = c_parser_peek_token (parser)->location;
11304 tree c, t = c_parser_expression (parser).value;
11305 mark_exp_read (t);
11306 t = c_fully_fold (t, false, NULL);
11308 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11310 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11312 c_parser_error (parser, "expected integer expression");
11313 return list;
11316 /* Attempt to statically determine when the number isn't positive. */
11317 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11318 build_int_cst (TREE_TYPE (t), 0));
11319 protected_set_expr_location (c, expr_loc);
11320 if (c == boolean_true_node)
11322 warning_at (expr_loc, 0,
11323 "%<num_threads%> value must be positive");
11324 t = integer_one_node;
11327 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11329 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11330 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11331 OMP_CLAUSE_CHAIN (c) = list;
11332 list = c;
11335 return list;
11338 /* OpenMP 4.5:
11339 num_tasks ( expression ) */
11341 static tree
11342 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11344 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11345 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11347 location_t expr_loc = c_parser_peek_token (parser)->location;
11348 tree c, t = c_parser_expression (parser).value;
11349 mark_exp_read (t);
11350 t = c_fully_fold (t, false, NULL);
11352 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11354 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11356 c_parser_error (parser, "expected integer expression");
11357 return list;
11360 /* Attempt to statically determine when the number isn't positive. */
11361 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11362 build_int_cst (TREE_TYPE (t), 0));
11363 if (CAN_HAVE_LOCATION_P (c))
11364 SET_EXPR_LOCATION (c, expr_loc);
11365 if (c == boolean_true_node)
11367 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11368 t = integer_one_node;
11371 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11373 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11374 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11375 OMP_CLAUSE_CHAIN (c) = list;
11376 list = c;
11379 return list;
11382 /* OpenMP 4.5:
11383 grainsize ( expression ) */
11385 static tree
11386 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11388 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11389 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11391 location_t expr_loc = c_parser_peek_token (parser)->location;
11392 tree c, t = c_parser_expression (parser).value;
11393 mark_exp_read (t);
11394 t = c_fully_fold (t, false, NULL);
11396 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11398 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11400 c_parser_error (parser, "expected integer expression");
11401 return list;
11404 /* Attempt to statically determine when the number isn't positive. */
11405 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11406 build_int_cst (TREE_TYPE (t), 0));
11407 if (CAN_HAVE_LOCATION_P (c))
11408 SET_EXPR_LOCATION (c, expr_loc);
11409 if (c == boolean_true_node)
11411 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11412 t = integer_one_node;
11415 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11417 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11418 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11419 OMP_CLAUSE_CHAIN (c) = list;
11420 list = c;
11423 return list;
11426 /* OpenMP 4.5:
11427 priority ( expression ) */
11429 static tree
11430 c_parser_omp_clause_priority (c_parser *parser, tree list)
11432 location_t priority_loc = c_parser_peek_token (parser)->location;
11433 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11435 location_t expr_loc = c_parser_peek_token (parser)->location;
11436 tree c, t = c_parser_expression (parser).value;
11437 mark_exp_read (t);
11438 t = c_fully_fold (t, false, NULL);
11440 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11442 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11444 c_parser_error (parser, "expected integer expression");
11445 return list;
11448 /* Attempt to statically determine when the number isn't
11449 non-negative. */
11450 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11451 build_int_cst (TREE_TYPE (t), 0));
11452 if (CAN_HAVE_LOCATION_P (c))
11453 SET_EXPR_LOCATION (c, expr_loc);
11454 if (c == boolean_true_node)
11456 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11457 t = integer_one_node;
11460 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11462 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11463 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11464 OMP_CLAUSE_CHAIN (c) = list;
11465 list = c;
11468 return list;
11471 /* OpenMP 4.5:
11472 hint ( expression ) */
11474 static tree
11475 c_parser_omp_clause_hint (c_parser *parser, tree list)
11477 location_t hint_loc = c_parser_peek_token (parser)->location;
11478 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11480 tree c, t = c_parser_expression (parser).value;
11481 mark_exp_read (t);
11482 t = c_fully_fold (t, false, NULL);
11484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11486 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11488 c_parser_error (parser, "expected integer expression");
11489 return list;
11492 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11494 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11495 OMP_CLAUSE_HINT_EXPR (c) = t;
11496 OMP_CLAUSE_CHAIN (c) = list;
11497 list = c;
11500 return list;
11503 /* OpenMP 4.5:
11504 defaultmap ( tofrom : scalar ) */
11506 static tree
11507 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11509 location_t loc = c_parser_peek_token (parser)->location;
11510 tree c;
11511 const char *p;
11513 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11514 return list;
11515 if (!c_parser_next_token_is (parser, CPP_NAME))
11517 c_parser_error (parser, "expected %<tofrom%>");
11518 goto out_err;
11520 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11521 if (strcmp (p, "tofrom") != 0)
11523 c_parser_error (parser, "expected %<tofrom%>");
11524 goto out_err;
11526 c_parser_consume_token (parser);
11527 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11528 goto out_err;
11529 if (!c_parser_next_token_is (parser, CPP_NAME))
11531 c_parser_error (parser, "expected %<scalar%>");
11532 goto out_err;
11534 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11535 if (strcmp (p, "scalar") != 0)
11537 c_parser_error (parser, "expected %<scalar%>");
11538 goto out_err;
11540 c_parser_consume_token (parser);
11541 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11542 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11543 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11544 OMP_CLAUSE_CHAIN (c) = list;
11545 return c;
11547 out_err:
11548 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11549 return list;
11552 /* OpenACC 2.0:
11553 use_device ( variable-list )
11555 OpenMP 4.5:
11556 use_device_ptr ( variable-list ) */
11558 static tree
11559 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11561 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11562 list);
11565 /* OpenMP 4.5:
11566 is_device_ptr ( variable-list ) */
11568 static tree
11569 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11571 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11574 /* OpenACC:
11575 num_workers ( expression ) */
11577 static tree
11578 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11580 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11581 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11583 location_t expr_loc = c_parser_peek_token (parser)->location;
11584 tree c, t = c_parser_expression (parser).value;
11585 mark_exp_read (t);
11586 t = c_fully_fold (t, false, NULL);
11588 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11590 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11592 c_parser_error (parser, "expected integer expression");
11593 return list;
11596 /* Attempt to statically determine when the number isn't positive. */
11597 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11598 build_int_cst (TREE_TYPE (t), 0));
11599 protected_set_expr_location (c, expr_loc);
11600 if (c == boolean_true_node)
11602 warning_at (expr_loc, 0,
11603 "%<num_workers%> value must be positive");
11604 t = integer_one_node;
11607 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11609 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11610 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11611 OMP_CLAUSE_CHAIN (c) = list;
11612 list = c;
11615 return list;
11618 /* OpenACC:
11620 gang [( gang-arg-list )]
11621 worker [( [num:] int-expr )]
11622 vector [( [length:] int-expr )]
11624 where gang-arg is one of:
11626 [num:] int-expr
11627 static: size-expr
11629 and size-expr may be:
11632 int-expr
11635 static tree
11636 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11637 const char *str, tree list)
11639 const char *id = "num";
11640 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11641 location_t loc = c_parser_peek_token (parser)->location;
11643 if (kind == OMP_CLAUSE_VECTOR)
11644 id = "length";
11646 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11648 c_parser_consume_token (parser);
11652 c_token *next = c_parser_peek_token (parser);
11653 int idx = 0;
11655 /* Gang static argument. */
11656 if (kind == OMP_CLAUSE_GANG
11657 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11659 c_parser_consume_token (parser);
11661 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11662 goto cleanup_error;
11664 idx = 1;
11665 if (ops[idx] != NULL_TREE)
11667 c_parser_error (parser, "too many %<static%> arguments");
11668 goto cleanup_error;
11671 /* Check for the '*' argument. */
11672 if (c_parser_next_token_is (parser, CPP_MULT)
11673 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11674 || c_parser_peek_2nd_token (parser)->type
11675 == CPP_CLOSE_PAREN))
11677 c_parser_consume_token (parser);
11678 ops[idx] = integer_minus_one_node;
11680 if (c_parser_next_token_is (parser, CPP_COMMA))
11682 c_parser_consume_token (parser);
11683 continue;
11685 else
11686 break;
11689 /* Worker num: argument and vector length: arguments. */
11690 else if (c_parser_next_token_is (parser, CPP_NAME)
11691 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11692 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11694 c_parser_consume_token (parser); /* id */
11695 c_parser_consume_token (parser); /* ':' */
11698 /* Now collect the actual argument. */
11699 if (ops[idx] != NULL_TREE)
11701 c_parser_error (parser, "unexpected argument");
11702 goto cleanup_error;
11705 location_t expr_loc = c_parser_peek_token (parser)->location;
11706 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11707 if (expr == error_mark_node)
11708 goto cleanup_error;
11710 mark_exp_read (expr);
11711 expr = c_fully_fold (expr, false, NULL);
11713 /* Attempt to statically determine when the number isn't a
11714 positive integer. */
11716 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11718 c_parser_error (parser, "expected integer expression");
11719 return list;
11722 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11723 build_int_cst (TREE_TYPE (expr), 0));
11724 if (c == boolean_true_node)
11726 warning_at (loc, 0,
11727 "%<%s%> value must be positive", str);
11728 expr = integer_one_node;
11731 ops[idx] = expr;
11733 if (kind == OMP_CLAUSE_GANG
11734 && c_parser_next_token_is (parser, CPP_COMMA))
11736 c_parser_consume_token (parser);
11737 continue;
11739 break;
11741 while (1);
11743 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11744 goto cleanup_error;
11747 check_no_duplicate_clause (list, kind, str);
11749 c = build_omp_clause (loc, kind);
11751 if (ops[1])
11752 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11754 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11755 OMP_CLAUSE_CHAIN (c) = list;
11757 return c;
11759 cleanup_error:
11760 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11761 return list;
11764 /* OpenACC:
11765 auto
11766 independent
11767 nohost
11768 seq */
11770 static tree
11771 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11772 tree list)
11774 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11776 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11777 OMP_CLAUSE_CHAIN (c) = list;
11779 return c;
11782 /* OpenACC:
11783 async [( int-expr )] */
11785 static tree
11786 c_parser_oacc_clause_async (c_parser *parser, tree list)
11788 tree c, t;
11789 location_t loc = c_parser_peek_token (parser)->location;
11791 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11793 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11795 c_parser_consume_token (parser);
11797 t = c_parser_expression (parser).value;
11798 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11799 c_parser_error (parser, "expected integer expression");
11800 else if (t == error_mark_node
11801 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11802 return list;
11804 else
11805 t = c_fully_fold (t, false, NULL);
11807 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11809 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11810 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11811 OMP_CLAUSE_CHAIN (c) = list;
11812 list = c;
11814 return list;
11817 /* OpenACC 2.0:
11818 tile ( size-expr-list ) */
11820 static tree
11821 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11823 tree c, expr = error_mark_node;
11824 location_t loc, expr_loc;
11825 tree tile = NULL_TREE;
11827 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11829 loc = c_parser_peek_token (parser)->location;
11830 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11831 return list;
11835 if (c_parser_next_token_is (parser, CPP_MULT)
11836 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11837 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11839 c_parser_consume_token (parser);
11840 expr = integer_minus_one_node;
11842 else
11844 expr_loc = c_parser_peek_token (parser)->location;
11845 expr = c_parser_expr_no_commas (parser, NULL).value;
11847 if (expr == error_mark_node)
11849 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11850 "expected %<)%>");
11851 return list;
11854 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11856 c_parser_error (parser, "%<tile%> value must be integral");
11857 return list;
11860 mark_exp_read (expr);
11861 expr = c_fully_fold (expr, false, NULL);
11863 /* Attempt to statically determine when expr isn't positive. */
11864 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11865 build_int_cst (TREE_TYPE (expr), 0));
11866 protected_set_expr_location (c, expr_loc);
11867 if (c == boolean_true_node)
11869 warning_at (expr_loc, 0,"%<tile%> value must be positive");
11870 expr = integer_one_node;
11874 tile = tree_cons (NULL_TREE, expr, tile);
11875 if (c_parser_next_token_is (parser, CPP_COMMA))
11876 c_parser_consume_token (parser);
11878 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11880 /* Consume the trailing ')'. */
11881 c_parser_consume_token (parser);
11883 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11884 tile = nreverse (tile);
11885 OMP_CLAUSE_TILE_LIST (c) = tile;
11886 OMP_CLAUSE_CHAIN (c) = list;
11887 return c;
11890 /* OpenACC:
11891 wait ( int-expr-list ) */
11893 static tree
11894 c_parser_oacc_clause_wait (c_parser *parser, tree list)
11896 location_t clause_loc = c_parser_peek_token (parser)->location;
11898 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11899 list = c_parser_oacc_wait_list (parser, clause_loc, list);
11901 return list;
11904 /* OpenMP 2.5:
11905 ordered
11907 OpenMP 4.5:
11908 ordered ( constant-expression ) */
11910 static tree
11911 c_parser_omp_clause_ordered (c_parser *parser, tree list)
11913 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
11915 tree c, num = NULL_TREE;
11916 HOST_WIDE_INT n;
11917 location_t loc = c_parser_peek_token (parser)->location;
11918 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11920 c_parser_consume_token (parser);
11921 num = c_parser_expr_no_commas (parser, NULL).value;
11922 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11924 if (num == error_mark_node)
11925 return list;
11926 if (num)
11928 mark_exp_read (num);
11929 num = c_fully_fold (num, false, NULL);
11930 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11931 || !tree_fits_shwi_p (num)
11932 || (n = tree_to_shwi (num)) <= 0
11933 || (int) n != n)
11935 error_at (loc, "ordered argument needs positive "
11936 "constant integer expression");
11937 return list;
11940 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
11941 OMP_CLAUSE_ORDERED_EXPR (c) = num;
11942 OMP_CLAUSE_CHAIN (c) = list;
11943 return c;
11946 /* OpenMP 2.5:
11947 private ( variable-list ) */
11949 static tree
11950 c_parser_omp_clause_private (c_parser *parser, tree list)
11952 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
11955 /* OpenMP 2.5:
11956 reduction ( reduction-operator : variable-list )
11958 reduction-operator:
11959 One of: + * - & ^ | && ||
11961 OpenMP 3.1:
11963 reduction-operator:
11964 One of: + * - & ^ | && || max min
11966 OpenMP 4.0:
11968 reduction-operator:
11969 One of: + * - & ^ | && ||
11970 identifier */
11972 static tree
11973 c_parser_omp_clause_reduction (c_parser *parser, tree list)
11975 location_t clause_loc = c_parser_peek_token (parser)->location;
11976 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11978 enum tree_code code = ERROR_MARK;
11979 tree reduc_id = NULL_TREE;
11981 switch (c_parser_peek_token (parser)->type)
11983 case CPP_PLUS:
11984 code = PLUS_EXPR;
11985 break;
11986 case CPP_MULT:
11987 code = MULT_EXPR;
11988 break;
11989 case CPP_MINUS:
11990 code = MINUS_EXPR;
11991 break;
11992 case CPP_AND:
11993 code = BIT_AND_EXPR;
11994 break;
11995 case CPP_XOR:
11996 code = BIT_XOR_EXPR;
11997 break;
11998 case CPP_OR:
11999 code = BIT_IOR_EXPR;
12000 break;
12001 case CPP_AND_AND:
12002 code = TRUTH_ANDIF_EXPR;
12003 break;
12004 case CPP_OR_OR:
12005 code = TRUTH_ORIF_EXPR;
12006 break;
12007 case CPP_NAME:
12009 const char *p
12010 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12011 if (strcmp (p, "min") == 0)
12013 code = MIN_EXPR;
12014 break;
12016 if (strcmp (p, "max") == 0)
12018 code = MAX_EXPR;
12019 break;
12021 reduc_id = c_parser_peek_token (parser)->value;
12022 break;
12024 default:
12025 c_parser_error (parser,
12026 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12027 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
12028 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12029 return list;
12031 c_parser_consume_token (parser);
12032 reduc_id = c_omp_reduction_id (code, reduc_id);
12033 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12035 tree nl, c;
12037 nl = c_parser_omp_variable_list (parser, clause_loc,
12038 OMP_CLAUSE_REDUCTION, list);
12039 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12041 tree d = OMP_CLAUSE_DECL (c), type;
12042 if (TREE_CODE (d) != TREE_LIST)
12043 type = TREE_TYPE (d);
12044 else
12046 int cnt = 0;
12047 tree t;
12048 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12049 cnt++;
12050 type = TREE_TYPE (t);
12051 while (cnt > 0)
12053 if (TREE_CODE (type) != POINTER_TYPE
12054 && TREE_CODE (type) != ARRAY_TYPE)
12055 break;
12056 type = TREE_TYPE (type);
12057 cnt--;
12060 while (TREE_CODE (type) == ARRAY_TYPE)
12061 type = TREE_TYPE (type);
12062 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12063 if (code == ERROR_MARK
12064 || !(INTEGRAL_TYPE_P (type)
12065 || TREE_CODE (type) == REAL_TYPE
12066 || TREE_CODE (type) == COMPLEX_TYPE))
12067 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12068 = c_omp_reduction_lookup (reduc_id,
12069 TYPE_MAIN_VARIANT (type));
12072 list = nl;
12074 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12076 return list;
12079 /* OpenMP 2.5:
12080 schedule ( schedule-kind )
12081 schedule ( schedule-kind , expression )
12083 schedule-kind:
12084 static | dynamic | guided | runtime | auto
12086 OpenMP 4.5:
12087 schedule ( schedule-modifier : schedule-kind )
12088 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12090 schedule-modifier:
12091 simd
12092 monotonic
12093 nonmonotonic */
12095 static tree
12096 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12098 tree c, t;
12099 location_t loc = c_parser_peek_token (parser)->location;
12100 int modifiers = 0, nmodifiers = 0;
12102 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12103 return list;
12105 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12107 while (c_parser_next_token_is (parser, CPP_NAME))
12109 tree kind = c_parser_peek_token (parser)->value;
12110 const char *p = IDENTIFIER_POINTER (kind);
12111 if (strcmp ("simd", p) == 0)
12112 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12113 else if (strcmp ("monotonic", p) == 0)
12114 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12115 else if (strcmp ("nonmonotonic", p) == 0)
12116 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12117 else
12118 break;
12119 c_parser_consume_token (parser);
12120 if (nmodifiers++ == 0
12121 && c_parser_next_token_is (parser, CPP_COMMA))
12122 c_parser_consume_token (parser);
12123 else
12125 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12126 break;
12130 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12131 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12132 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12133 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12135 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12136 "specified");
12137 modifiers = 0;
12140 if (c_parser_next_token_is (parser, CPP_NAME))
12142 tree kind = c_parser_peek_token (parser)->value;
12143 const char *p = IDENTIFIER_POINTER (kind);
12145 switch (p[0])
12147 case 'd':
12148 if (strcmp ("dynamic", p) != 0)
12149 goto invalid_kind;
12150 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12151 break;
12153 case 'g':
12154 if (strcmp ("guided", p) != 0)
12155 goto invalid_kind;
12156 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12157 break;
12159 case 'r':
12160 if (strcmp ("runtime", p) != 0)
12161 goto invalid_kind;
12162 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12163 break;
12165 default:
12166 goto invalid_kind;
12169 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12170 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12171 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12172 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12173 else
12174 goto invalid_kind;
12176 c_parser_consume_token (parser);
12177 if (c_parser_next_token_is (parser, CPP_COMMA))
12179 location_t here;
12180 c_parser_consume_token (parser);
12182 here = c_parser_peek_token (parser)->location;
12183 t = c_parser_expr_no_commas (parser, NULL).value;
12184 mark_exp_read (t);
12185 t = c_fully_fold (t, false, NULL);
12187 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12188 error_at (here, "schedule %<runtime%> does not take "
12189 "a %<chunk_size%> parameter");
12190 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12191 error_at (here,
12192 "schedule %<auto%> does not take "
12193 "a %<chunk_size%> parameter");
12194 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12196 /* Attempt to statically determine when the number isn't
12197 positive. */
12198 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12199 build_int_cst (TREE_TYPE (t), 0));
12200 protected_set_expr_location (s, loc);
12201 if (s == boolean_true_node)
12203 warning_at (loc, 0,
12204 "chunk size value must be positive");
12205 t = integer_one_node;
12207 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12209 else
12210 c_parser_error (parser, "expected integer expression");
12212 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12214 else
12215 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12216 "expected %<,%> or %<)%>");
12218 OMP_CLAUSE_SCHEDULE_KIND (c)
12219 = (enum omp_clause_schedule_kind)
12220 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12222 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12223 OMP_CLAUSE_CHAIN (c) = list;
12224 return c;
12226 invalid_kind:
12227 c_parser_error (parser, "invalid schedule kind");
12228 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12229 return list;
12232 /* OpenMP 2.5:
12233 shared ( variable-list ) */
12235 static tree
12236 c_parser_omp_clause_shared (c_parser *parser, tree list)
12238 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12241 /* OpenMP 3.0:
12242 untied */
12244 static tree
12245 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12247 tree c;
12249 /* FIXME: Should we allow duplicates? */
12250 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12252 c = build_omp_clause (c_parser_peek_token (parser)->location,
12253 OMP_CLAUSE_UNTIED);
12254 OMP_CLAUSE_CHAIN (c) = list;
12256 return c;
12259 /* OpenACC:
12260 vector_length ( expression ) */
12262 static tree
12263 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12265 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12266 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12268 location_t expr_loc = c_parser_peek_token (parser)->location;
12269 tree c, t = c_parser_expression (parser).value;
12270 mark_exp_read (t);
12271 t = c_fully_fold (t, false, NULL);
12273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12275 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12277 c_parser_error (parser, "expected integer expression");
12278 return list;
12281 /* Attempt to statically determine when the number isn't positive. */
12282 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12283 build_int_cst (TREE_TYPE (t), 0));
12284 protected_set_expr_location (c, expr_loc);
12285 if (c == boolean_true_node)
12287 warning_at (expr_loc, 0,
12288 "%<vector_length%> value must be positive");
12289 t = integer_one_node;
12292 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12294 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12295 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12296 OMP_CLAUSE_CHAIN (c) = list;
12297 list = c;
12300 return list;
12303 /* OpenMP 4.0:
12304 inbranch
12305 notinbranch */
12307 static tree
12308 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12309 enum omp_clause_code code, tree list)
12311 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12313 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12314 OMP_CLAUSE_CHAIN (c) = list;
12316 return c;
12319 /* OpenMP 4.0:
12320 parallel
12322 sections
12323 taskgroup */
12325 static tree
12326 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12327 enum omp_clause_code code, tree list)
12329 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12330 OMP_CLAUSE_CHAIN (c) = list;
12332 return c;
12335 /* OpenMP 4.5:
12336 nogroup */
12338 static tree
12339 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12341 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12342 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12343 OMP_CLAUSE_NOGROUP);
12344 OMP_CLAUSE_CHAIN (c) = list;
12345 return c;
12348 /* OpenMP 4.5:
12349 simd
12350 threads */
12352 static tree
12353 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12354 enum omp_clause_code code, tree list)
12356 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12357 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12358 OMP_CLAUSE_CHAIN (c) = list;
12359 return c;
12362 /* OpenMP 4.0:
12363 num_teams ( expression ) */
12365 static tree
12366 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12368 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12369 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12371 location_t expr_loc = c_parser_peek_token (parser)->location;
12372 tree c, t = c_parser_expression (parser).value;
12373 mark_exp_read (t);
12374 t = c_fully_fold (t, false, NULL);
12376 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12378 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12380 c_parser_error (parser, "expected integer expression");
12381 return list;
12384 /* Attempt to statically determine when the number isn't positive. */
12385 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12386 build_int_cst (TREE_TYPE (t), 0));
12387 protected_set_expr_location (c, expr_loc);
12388 if (c == boolean_true_node)
12390 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12391 t = integer_one_node;
12394 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12396 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12397 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12398 OMP_CLAUSE_CHAIN (c) = list;
12399 list = c;
12402 return list;
12405 /* OpenMP 4.0:
12406 thread_limit ( expression ) */
12408 static tree
12409 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12411 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12412 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12414 location_t expr_loc = c_parser_peek_token (parser)->location;
12415 tree c, t = c_parser_expression (parser).value;
12416 mark_exp_read (t);
12417 t = c_fully_fold (t, false, NULL);
12419 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12421 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12423 c_parser_error (parser, "expected integer expression");
12424 return list;
12427 /* Attempt to statically determine when the number isn't positive. */
12428 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12429 build_int_cst (TREE_TYPE (t), 0));
12430 protected_set_expr_location (c, expr_loc);
12431 if (c == boolean_true_node)
12433 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12434 t = integer_one_node;
12437 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12438 "thread_limit");
12440 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12441 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12442 OMP_CLAUSE_CHAIN (c) = list;
12443 list = c;
12446 return list;
12449 /* OpenMP 4.0:
12450 aligned ( variable-list )
12451 aligned ( variable-list : constant-expression ) */
12453 static tree
12454 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12456 location_t clause_loc = c_parser_peek_token (parser)->location;
12457 tree nl, c;
12459 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12460 return list;
12462 nl = c_parser_omp_variable_list (parser, clause_loc,
12463 OMP_CLAUSE_ALIGNED, list);
12465 if (c_parser_next_token_is (parser, CPP_COLON))
12467 c_parser_consume_token (parser);
12468 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
12469 mark_exp_read (alignment);
12470 alignment = c_fully_fold (alignment, false, NULL);
12471 if (TREE_CODE (alignment) != INTEGER_CST
12472 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12473 || tree_int_cst_sgn (alignment) != 1)
12475 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12476 "be positive constant integer expression");
12477 alignment = NULL_TREE;
12480 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12481 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12485 return nl;
12488 /* OpenMP 4.0:
12489 linear ( variable-list )
12490 linear ( variable-list : expression )
12492 OpenMP 4.5:
12493 linear ( modifier ( variable-list ) )
12494 linear ( modifier ( variable-list ) : expression ) */
12496 static tree
12497 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12499 location_t clause_loc = c_parser_peek_token (parser)->location;
12500 tree nl, c, step;
12501 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12503 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12504 return list;
12506 if (!is_cilk_simd_fn
12507 && c_parser_next_token_is (parser, CPP_NAME))
12509 c_token *tok = c_parser_peek_token (parser);
12510 const char *p = IDENTIFIER_POINTER (tok->value);
12511 if (strcmp ("val", p) == 0)
12512 kind = OMP_CLAUSE_LINEAR_VAL;
12513 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12514 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12515 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12517 c_parser_consume_token (parser);
12518 c_parser_consume_token (parser);
12522 nl = c_parser_omp_variable_list (parser, clause_loc,
12523 OMP_CLAUSE_LINEAR, list);
12525 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12526 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12528 if (c_parser_next_token_is (parser, CPP_COLON))
12530 c_parser_consume_token (parser);
12531 step = c_parser_expression (parser).value;
12532 mark_exp_read (step);
12533 step = c_fully_fold (step, false, NULL);
12534 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12536 sorry ("using parameters for %<linear%> step is not supported yet");
12537 step = integer_one_node;
12539 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12541 error_at (clause_loc, "%<linear%> clause step expression must "
12542 "be integral");
12543 step = integer_one_node;
12547 else
12548 step = integer_one_node;
12550 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12552 OMP_CLAUSE_LINEAR_STEP (c) = step;
12553 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12556 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12557 return nl;
12560 /* OpenMP 4.0:
12561 safelen ( constant-expression ) */
12563 static tree
12564 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12566 location_t clause_loc = c_parser_peek_token (parser)->location;
12567 tree c, t;
12569 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12570 return list;
12572 t = c_parser_expr_no_commas (parser, NULL).value;
12573 mark_exp_read (t);
12574 t = c_fully_fold (t, false, NULL);
12575 if (TREE_CODE (t) != INTEGER_CST
12576 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12577 || tree_int_cst_sgn (t) != 1)
12579 error_at (clause_loc, "%<safelen%> clause expression must "
12580 "be positive constant integer expression");
12581 t = NULL_TREE;
12584 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12585 if (t == NULL_TREE || t == error_mark_node)
12586 return list;
12588 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12590 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12591 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12592 OMP_CLAUSE_CHAIN (c) = list;
12593 return c;
12596 /* OpenMP 4.0:
12597 simdlen ( constant-expression ) */
12599 static tree
12600 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12602 location_t clause_loc = c_parser_peek_token (parser)->location;
12603 tree c, t;
12605 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12606 return list;
12608 t = c_parser_expr_no_commas (parser, NULL).value;
12609 mark_exp_read (t);
12610 t = c_fully_fold (t, false, NULL);
12611 if (TREE_CODE (t) != INTEGER_CST
12612 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12613 || tree_int_cst_sgn (t) != 1)
12615 error_at (clause_loc, "%<simdlen%> clause expression must "
12616 "be positive constant integer expression");
12617 t = NULL_TREE;
12620 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12621 if (t == NULL_TREE || t == error_mark_node)
12622 return list;
12624 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12626 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12627 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12628 OMP_CLAUSE_CHAIN (c) = list;
12629 return c;
12632 /* OpenMP 4.5:
12633 vec:
12634 identifier [+/- integer]
12635 vec , identifier [+/- integer]
12638 static tree
12639 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12640 tree list)
12642 tree vec = NULL;
12643 if (c_parser_next_token_is_not (parser, CPP_NAME)
12644 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12646 c_parser_error (parser, "expected identifier");
12647 return list;
12650 while (c_parser_next_token_is (parser, CPP_NAME)
12651 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12653 tree t = lookup_name (c_parser_peek_token (parser)->value);
12654 tree addend = NULL;
12656 if (t == NULL_TREE)
12658 undeclared_variable (c_parser_peek_token (parser)->location,
12659 c_parser_peek_token (parser)->value);
12660 t = error_mark_node;
12663 c_parser_consume_token (parser);
12665 bool neg = false;
12666 if (c_parser_next_token_is (parser, CPP_MINUS))
12667 neg = true;
12668 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12670 addend = integer_zero_node;
12671 neg = false;
12672 goto add_to_vector;
12674 c_parser_consume_token (parser);
12676 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12678 c_parser_error (parser, "expected integer");
12679 return list;
12682 addend = c_parser_peek_token (parser)->value;
12683 if (TREE_CODE (addend) != INTEGER_CST)
12685 c_parser_error (parser, "expected integer");
12686 return list;
12688 c_parser_consume_token (parser);
12690 add_to_vector:
12691 if (t != error_mark_node)
12693 vec = tree_cons (addend, t, vec);
12694 if (neg)
12695 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12698 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12699 break;
12701 c_parser_consume_token (parser);
12704 if (vec == NULL_TREE)
12705 return list;
12707 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12708 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12709 OMP_CLAUSE_DECL (u) = nreverse (vec);
12710 OMP_CLAUSE_CHAIN (u) = list;
12711 return u;
12714 /* OpenMP 4.0:
12715 depend ( depend-kind: variable-list )
12717 depend-kind:
12718 in | out | inout
12720 OpenMP 4.5:
12721 depend ( source )
12723 depend ( sink : vec ) */
12725 static tree
12726 c_parser_omp_clause_depend (c_parser *parser, tree list)
12728 location_t clause_loc = c_parser_peek_token (parser)->location;
12729 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12730 tree nl, c;
12732 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12733 return list;
12735 if (c_parser_next_token_is (parser, CPP_NAME))
12737 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12738 if (strcmp ("in", p) == 0)
12739 kind = OMP_CLAUSE_DEPEND_IN;
12740 else if (strcmp ("inout", p) == 0)
12741 kind = OMP_CLAUSE_DEPEND_INOUT;
12742 else if (strcmp ("out", p) == 0)
12743 kind = OMP_CLAUSE_DEPEND_OUT;
12744 else if (strcmp ("source", p) == 0)
12745 kind = OMP_CLAUSE_DEPEND_SOURCE;
12746 else if (strcmp ("sink", p) == 0)
12747 kind = OMP_CLAUSE_DEPEND_SINK;
12748 else
12749 goto invalid_kind;
12751 else
12752 goto invalid_kind;
12754 c_parser_consume_token (parser);
12756 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12758 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12759 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12760 OMP_CLAUSE_DECL (c) = NULL_TREE;
12761 OMP_CLAUSE_CHAIN (c) = list;
12762 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12763 return c;
12766 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12767 goto resync_fail;
12769 if (kind == OMP_CLAUSE_DEPEND_SINK)
12770 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12771 else
12773 nl = c_parser_omp_variable_list (parser, clause_loc,
12774 OMP_CLAUSE_DEPEND, list);
12776 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12777 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12780 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12781 return nl;
12783 invalid_kind:
12784 c_parser_error (parser, "invalid depend kind");
12785 resync_fail:
12786 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12787 return list;
12790 /* OpenMP 4.0:
12791 map ( map-kind: variable-list )
12792 map ( variable-list )
12794 map-kind:
12795 alloc | to | from | tofrom
12797 OpenMP 4.5:
12798 map-kind:
12799 alloc | to | from | tofrom | release | delete
12801 map ( always [,] map-kind: variable-list ) */
12803 static tree
12804 c_parser_omp_clause_map (c_parser *parser, tree list)
12806 location_t clause_loc = c_parser_peek_token (parser)->location;
12807 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12808 int always = 0;
12809 enum c_id_kind always_id_kind = C_ID_NONE;
12810 location_t always_loc = UNKNOWN_LOCATION;
12811 tree always_id = NULL_TREE;
12812 tree nl, c;
12814 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12815 return list;
12817 if (c_parser_next_token_is (parser, CPP_NAME))
12819 c_token *tok = c_parser_peek_token (parser);
12820 const char *p = IDENTIFIER_POINTER (tok->value);
12821 always_id_kind = tok->id_kind;
12822 always_loc = tok->location;
12823 always_id = tok->value;
12824 if (strcmp ("always", p) == 0)
12826 c_token *sectok = c_parser_peek_2nd_token (parser);
12827 if (sectok->type == CPP_COMMA)
12829 c_parser_consume_token (parser);
12830 c_parser_consume_token (parser);
12831 always = 2;
12833 else if (sectok->type == CPP_NAME)
12835 p = IDENTIFIER_POINTER (sectok->value);
12836 if (strcmp ("alloc", p) == 0
12837 || strcmp ("to", p) == 0
12838 || strcmp ("from", p) == 0
12839 || strcmp ("tofrom", p) == 0
12840 || strcmp ("release", p) == 0
12841 || strcmp ("delete", p) == 0)
12843 c_parser_consume_token (parser);
12844 always = 1;
12850 if (c_parser_next_token_is (parser, CPP_NAME)
12851 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12853 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12854 if (strcmp ("alloc", p) == 0)
12855 kind = GOMP_MAP_ALLOC;
12856 else if (strcmp ("to", p) == 0)
12857 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12858 else if (strcmp ("from", p) == 0)
12859 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12860 else if (strcmp ("tofrom", p) == 0)
12861 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12862 else if (strcmp ("release", p) == 0)
12863 kind = GOMP_MAP_RELEASE;
12864 else if (strcmp ("delete", p) == 0)
12865 kind = GOMP_MAP_DELETE;
12866 else
12868 c_parser_error (parser, "invalid map kind");
12869 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12870 "expected %<)%>");
12871 return list;
12873 c_parser_consume_token (parser);
12874 c_parser_consume_token (parser);
12876 else if (always)
12878 if (always_id_kind != C_ID_ID)
12880 c_parser_error (parser, "expected identifier");
12881 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12882 return list;
12885 tree t = lookup_name (always_id);
12886 if (t == NULL_TREE)
12888 undeclared_variable (always_loc, always_id);
12889 t = error_mark_node;
12891 if (t != error_mark_node)
12893 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12894 OMP_CLAUSE_DECL (u) = t;
12895 OMP_CLAUSE_CHAIN (u) = list;
12896 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12897 list = u;
12899 if (always == 1)
12901 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12902 return list;
12906 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
12908 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12909 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12912 return nl;
12915 /* OpenMP 4.0:
12916 device ( expression ) */
12918 static tree
12919 c_parser_omp_clause_device (c_parser *parser, tree list)
12921 location_t clause_loc = c_parser_peek_token (parser)->location;
12922 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12924 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
12925 mark_exp_read (t);
12926 t = c_fully_fold (t, false, NULL);
12928 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12930 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12932 c_parser_error (parser, "expected integer expression");
12933 return list;
12936 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
12938 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
12939 OMP_CLAUSE_DEVICE_ID (c) = t;
12940 OMP_CLAUSE_CHAIN (c) = list;
12941 list = c;
12944 return list;
12947 /* OpenMP 4.0:
12948 dist_schedule ( static )
12949 dist_schedule ( static , expression ) */
12951 static tree
12952 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
12954 tree c, t = NULL_TREE;
12955 location_t loc = c_parser_peek_token (parser)->location;
12957 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12958 return list;
12960 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
12962 c_parser_error (parser, "invalid dist_schedule kind");
12963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12964 "expected %<)%>");
12965 return list;
12968 c_parser_consume_token (parser);
12969 if (c_parser_next_token_is (parser, CPP_COMMA))
12971 c_parser_consume_token (parser);
12973 t = c_parser_expr_no_commas (parser, NULL).value;
12974 mark_exp_read (t);
12975 t = c_fully_fold (t, false, NULL);
12976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12978 else
12979 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12980 "expected %<,%> or %<)%>");
12982 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12983 if (t == error_mark_node)
12984 return list;
12986 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
12987 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
12988 OMP_CLAUSE_CHAIN (c) = list;
12989 return c;
12992 /* OpenMP 4.0:
12993 proc_bind ( proc-bind-kind )
12995 proc-bind-kind:
12996 master | close | spread */
12998 static tree
12999 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13001 location_t clause_loc = c_parser_peek_token (parser)->location;
13002 enum omp_clause_proc_bind_kind kind;
13003 tree c;
13005 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13006 return list;
13008 if (c_parser_next_token_is (parser, CPP_NAME))
13010 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13011 if (strcmp ("master", p) == 0)
13012 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13013 else if (strcmp ("close", p) == 0)
13014 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13015 else if (strcmp ("spread", p) == 0)
13016 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13017 else
13018 goto invalid_kind;
13020 else
13021 goto invalid_kind;
13023 c_parser_consume_token (parser);
13024 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13025 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13026 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13027 OMP_CLAUSE_CHAIN (c) = list;
13028 return c;
13030 invalid_kind:
13031 c_parser_error (parser, "invalid proc_bind kind");
13032 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13033 return list;
13036 /* OpenMP 4.0:
13037 to ( variable-list ) */
13039 static tree
13040 c_parser_omp_clause_to (c_parser *parser, tree list)
13042 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13045 /* OpenMP 4.0:
13046 from ( variable-list ) */
13048 static tree
13049 c_parser_omp_clause_from (c_parser *parser, tree list)
13051 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13054 /* OpenMP 4.0:
13055 uniform ( variable-list ) */
13057 static tree
13058 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13060 /* The clauses location. */
13061 location_t loc = c_parser_peek_token (parser)->location;
13063 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13065 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13066 list);
13067 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13069 return list;
13072 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13073 is a bitmask in MASK. Return the list of clauses found. */
13075 static tree
13076 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13077 const char *where, bool finish_p = true)
13079 tree clauses = NULL;
13080 bool first = true;
13082 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13084 location_t here;
13085 pragma_omp_clause c_kind;
13086 const char *c_name;
13087 tree prev = clauses;
13089 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13090 c_parser_consume_token (parser);
13092 here = c_parser_peek_token (parser)->location;
13093 c_kind = c_parser_omp_clause_name (parser);
13095 switch (c_kind)
13097 case PRAGMA_OACC_CLAUSE_ASYNC:
13098 clauses = c_parser_oacc_clause_async (parser, clauses);
13099 c_name = "async";
13100 break;
13101 case PRAGMA_OACC_CLAUSE_AUTO:
13102 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13103 clauses);
13104 c_name = "auto";
13105 break;
13106 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13107 clauses = c_parser_omp_clause_collapse (parser, clauses);
13108 c_name = "collapse";
13109 break;
13110 case PRAGMA_OACC_CLAUSE_COPY:
13111 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13112 c_name = "copy";
13113 break;
13114 case PRAGMA_OACC_CLAUSE_COPYIN:
13115 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13116 c_name = "copyin";
13117 break;
13118 case PRAGMA_OACC_CLAUSE_COPYOUT:
13119 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13120 c_name = "copyout";
13121 break;
13122 case PRAGMA_OACC_CLAUSE_CREATE:
13123 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13124 c_name = "create";
13125 break;
13126 case PRAGMA_OACC_CLAUSE_DELETE:
13127 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13128 c_name = "delete";
13129 break;
13130 case PRAGMA_OMP_CLAUSE_DEFAULT:
13131 clauses = c_parser_omp_clause_default (parser, clauses, true);
13132 c_name = "default";
13133 break;
13134 case PRAGMA_OACC_CLAUSE_DEVICE:
13135 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13136 c_name = "device";
13137 break;
13138 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13139 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13140 c_name = "deviceptr";
13141 break;
13142 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13143 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13144 c_name = "device_resident";
13145 break;
13146 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13147 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13148 c_name = "firstprivate";
13149 break;
13150 case PRAGMA_OACC_CLAUSE_GANG:
13151 c_name = "gang";
13152 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13153 c_name, clauses);
13154 break;
13155 case PRAGMA_OACC_CLAUSE_HOST:
13156 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13157 c_name = "host";
13158 break;
13159 case PRAGMA_OACC_CLAUSE_IF:
13160 clauses = c_parser_omp_clause_if (parser, clauses, false);
13161 c_name = "if";
13162 break;
13163 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13164 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13165 clauses);
13166 c_name = "independent";
13167 break;
13168 case PRAGMA_OACC_CLAUSE_LINK:
13169 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13170 c_name = "link";
13171 break;
13172 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13173 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13174 c_name = "num_gangs";
13175 break;
13176 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13177 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13178 c_name = "num_workers";
13179 break;
13180 case PRAGMA_OACC_CLAUSE_PRESENT:
13181 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13182 c_name = "present";
13183 break;
13184 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13185 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13186 c_name = "present_or_copy";
13187 break;
13188 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13189 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13190 c_name = "present_or_copyin";
13191 break;
13192 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13193 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13194 c_name = "present_or_copyout";
13195 break;
13196 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13197 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13198 c_name = "present_or_create";
13199 break;
13200 case PRAGMA_OACC_CLAUSE_PRIVATE:
13201 clauses = c_parser_omp_clause_private (parser, clauses);
13202 c_name = "private";
13203 break;
13204 case PRAGMA_OACC_CLAUSE_REDUCTION:
13205 clauses = c_parser_omp_clause_reduction (parser, clauses);
13206 c_name = "reduction";
13207 break;
13208 case PRAGMA_OACC_CLAUSE_SELF:
13209 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13210 c_name = "self";
13211 break;
13212 case PRAGMA_OACC_CLAUSE_SEQ:
13213 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13214 clauses);
13215 c_name = "seq";
13216 break;
13217 case PRAGMA_OACC_CLAUSE_TILE:
13218 clauses = c_parser_oacc_clause_tile (parser, clauses);
13219 c_name = "tile";
13220 break;
13221 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13222 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13223 c_name = "use_device";
13224 break;
13225 case PRAGMA_OACC_CLAUSE_VECTOR:
13226 c_name = "vector";
13227 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13228 c_name, clauses);
13229 break;
13230 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13231 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13232 c_name = "vector_length";
13233 break;
13234 case PRAGMA_OACC_CLAUSE_WAIT:
13235 clauses = c_parser_oacc_clause_wait (parser, clauses);
13236 c_name = "wait";
13237 break;
13238 case PRAGMA_OACC_CLAUSE_WORKER:
13239 c_name = "worker";
13240 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13241 c_name, clauses);
13242 break;
13243 default:
13244 c_parser_error (parser, "expected %<#pragma acc%> clause");
13245 goto saw_error;
13248 first = false;
13250 if (((mask >> c_kind) & 1) == 0)
13252 /* Remove the invalid clause(s) from the list to avoid
13253 confusing the rest of the compiler. */
13254 clauses = prev;
13255 error_at (here, "%qs is not valid for %qs", c_name, where);
13259 saw_error:
13260 c_parser_skip_to_pragma_eol (parser);
13262 if (finish_p)
13263 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13265 return clauses;
13268 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13269 is a bitmask in MASK. Return the list of clauses found. */
13271 static tree
13272 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13273 const char *where, bool finish_p = true)
13275 tree clauses = NULL;
13276 bool first = true, cilk_simd_fn = false;
13278 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13280 location_t here;
13281 pragma_omp_clause c_kind;
13282 const char *c_name;
13283 tree prev = clauses;
13285 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13286 c_parser_consume_token (parser);
13288 here = c_parser_peek_token (parser)->location;
13289 c_kind = c_parser_omp_clause_name (parser);
13291 switch (c_kind)
13293 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13294 clauses = c_parser_omp_clause_collapse (parser, clauses);
13295 c_name = "collapse";
13296 break;
13297 case PRAGMA_OMP_CLAUSE_COPYIN:
13298 clauses = c_parser_omp_clause_copyin (parser, clauses);
13299 c_name = "copyin";
13300 break;
13301 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13302 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13303 c_name = "copyprivate";
13304 break;
13305 case PRAGMA_OMP_CLAUSE_DEFAULT:
13306 clauses = c_parser_omp_clause_default (parser, clauses, false);
13307 c_name = "default";
13308 break;
13309 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13310 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13311 c_name = "firstprivate";
13312 break;
13313 case PRAGMA_OMP_CLAUSE_FINAL:
13314 clauses = c_parser_omp_clause_final (parser, clauses);
13315 c_name = "final";
13316 break;
13317 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13318 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13319 c_name = "grainsize";
13320 break;
13321 case PRAGMA_OMP_CLAUSE_HINT:
13322 clauses = c_parser_omp_clause_hint (parser, clauses);
13323 c_name = "hint";
13324 break;
13325 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13326 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13327 c_name = "defaultmap";
13328 break;
13329 case PRAGMA_OMP_CLAUSE_IF:
13330 clauses = c_parser_omp_clause_if (parser, clauses, true);
13331 c_name = "if";
13332 break;
13333 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13334 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13335 c_name = "lastprivate";
13336 break;
13337 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13338 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13339 c_name = "mergeable";
13340 break;
13341 case PRAGMA_OMP_CLAUSE_NOWAIT:
13342 clauses = c_parser_omp_clause_nowait (parser, clauses);
13343 c_name = "nowait";
13344 break;
13345 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13346 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13347 c_name = "num_tasks";
13348 break;
13349 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13350 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13351 c_name = "num_threads";
13352 break;
13353 case PRAGMA_OMP_CLAUSE_ORDERED:
13354 clauses = c_parser_omp_clause_ordered (parser, clauses);
13355 c_name = "ordered";
13356 break;
13357 case PRAGMA_OMP_CLAUSE_PRIORITY:
13358 clauses = c_parser_omp_clause_priority (parser, clauses);
13359 c_name = "priority";
13360 break;
13361 case PRAGMA_OMP_CLAUSE_PRIVATE:
13362 clauses = c_parser_omp_clause_private (parser, clauses);
13363 c_name = "private";
13364 break;
13365 case PRAGMA_OMP_CLAUSE_REDUCTION:
13366 clauses = c_parser_omp_clause_reduction (parser, clauses);
13367 c_name = "reduction";
13368 break;
13369 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13370 clauses = c_parser_omp_clause_schedule (parser, clauses);
13371 c_name = "schedule";
13372 break;
13373 case PRAGMA_OMP_CLAUSE_SHARED:
13374 clauses = c_parser_omp_clause_shared (parser, clauses);
13375 c_name = "shared";
13376 break;
13377 case PRAGMA_OMP_CLAUSE_UNTIED:
13378 clauses = c_parser_omp_clause_untied (parser, clauses);
13379 c_name = "untied";
13380 break;
13381 case PRAGMA_OMP_CLAUSE_INBRANCH:
13382 case PRAGMA_CILK_CLAUSE_MASK:
13383 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13384 clauses);
13385 c_name = "inbranch";
13386 break;
13387 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13388 case PRAGMA_CILK_CLAUSE_NOMASK:
13389 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13390 clauses);
13391 c_name = "notinbranch";
13392 break;
13393 case PRAGMA_OMP_CLAUSE_PARALLEL:
13394 clauses
13395 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13396 clauses);
13397 c_name = "parallel";
13398 if (!first)
13400 clause_not_first:
13401 error_at (here, "%qs must be the first clause of %qs",
13402 c_name, where);
13403 clauses = prev;
13405 break;
13406 case PRAGMA_OMP_CLAUSE_FOR:
13407 clauses
13408 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13409 clauses);
13410 c_name = "for";
13411 if (!first)
13412 goto clause_not_first;
13413 break;
13414 case PRAGMA_OMP_CLAUSE_SECTIONS:
13415 clauses
13416 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13417 clauses);
13418 c_name = "sections";
13419 if (!first)
13420 goto clause_not_first;
13421 break;
13422 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13423 clauses
13424 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13425 clauses);
13426 c_name = "taskgroup";
13427 if (!first)
13428 goto clause_not_first;
13429 break;
13430 case PRAGMA_OMP_CLAUSE_LINK:
13431 clauses
13432 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13433 c_name = "link";
13434 break;
13435 case PRAGMA_OMP_CLAUSE_TO:
13436 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13437 clauses
13438 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13439 clauses);
13440 else
13441 clauses = c_parser_omp_clause_to (parser, clauses);
13442 c_name = "to";
13443 break;
13444 case PRAGMA_OMP_CLAUSE_FROM:
13445 clauses = c_parser_omp_clause_from (parser, clauses);
13446 c_name = "from";
13447 break;
13448 case PRAGMA_OMP_CLAUSE_UNIFORM:
13449 clauses = c_parser_omp_clause_uniform (parser, clauses);
13450 c_name = "uniform";
13451 break;
13452 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13453 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13454 c_name = "num_teams";
13455 break;
13456 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13457 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13458 c_name = "thread_limit";
13459 break;
13460 case PRAGMA_OMP_CLAUSE_ALIGNED:
13461 clauses = c_parser_omp_clause_aligned (parser, clauses);
13462 c_name = "aligned";
13463 break;
13464 case PRAGMA_OMP_CLAUSE_LINEAR:
13465 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13466 cilk_simd_fn = true;
13467 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13468 c_name = "linear";
13469 break;
13470 case PRAGMA_OMP_CLAUSE_DEPEND:
13471 clauses = c_parser_omp_clause_depend (parser, clauses);
13472 c_name = "depend";
13473 break;
13474 case PRAGMA_OMP_CLAUSE_MAP:
13475 clauses = c_parser_omp_clause_map (parser, clauses);
13476 c_name = "map";
13477 break;
13478 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13479 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13480 c_name = "use_device_ptr";
13481 break;
13482 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13483 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13484 c_name = "is_device_ptr";
13485 break;
13486 case PRAGMA_OMP_CLAUSE_DEVICE:
13487 clauses = c_parser_omp_clause_device (parser, clauses);
13488 c_name = "device";
13489 break;
13490 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13491 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13492 c_name = "dist_schedule";
13493 break;
13494 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13495 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13496 c_name = "proc_bind";
13497 break;
13498 case PRAGMA_OMP_CLAUSE_SAFELEN:
13499 clauses = c_parser_omp_clause_safelen (parser, clauses);
13500 c_name = "safelen";
13501 break;
13502 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13503 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13504 c_name = "simdlen";
13505 break;
13506 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13507 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13508 c_name = "simdlen";
13509 break;
13510 case PRAGMA_OMP_CLAUSE_NOGROUP:
13511 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13512 c_name = "nogroup";
13513 break;
13514 case PRAGMA_OMP_CLAUSE_THREADS:
13515 clauses
13516 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13517 clauses);
13518 c_name = "threads";
13519 break;
13520 case PRAGMA_OMP_CLAUSE_SIMD:
13521 clauses
13522 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13523 clauses);
13524 c_name = "simd";
13525 break;
13526 default:
13527 c_parser_error (parser, "expected %<#pragma omp%> clause");
13528 goto saw_error;
13531 first = false;
13533 if (((mask >> c_kind) & 1) == 0)
13535 /* Remove the invalid clause(s) from the list to avoid
13536 confusing the rest of the compiler. */
13537 clauses = prev;
13538 error_at (here, "%qs is not valid for %qs", c_name, where);
13542 saw_error:
13543 c_parser_skip_to_pragma_eol (parser);
13545 if (finish_p)
13547 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13548 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
13549 return c_finish_omp_clauses (clauses, C_ORT_OMP);
13552 return clauses;
13555 /* OpenACC 2.0, OpenMP 2.5:
13556 structured-block:
13557 statement
13559 In practice, we're also interested in adding the statement to an
13560 outer node. So it is convenient if we work around the fact that
13561 c_parser_statement calls add_stmt. */
13563 static tree
13564 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13566 tree stmt = push_stmt_list ();
13567 c_parser_statement (parser, if_p);
13568 return pop_stmt_list (stmt);
13571 /* OpenACC 2.0:
13572 # pragma acc cache (variable-list) new-line
13574 LOC is the location of the #pragma token.
13577 static tree
13578 c_parser_oacc_cache (location_t loc, c_parser *parser)
13580 tree stmt, clauses;
13582 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13583 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13585 c_parser_skip_to_pragma_eol (parser);
13587 stmt = make_node (OACC_CACHE);
13588 TREE_TYPE (stmt) = void_type_node;
13589 OACC_CACHE_CLAUSES (stmt) = clauses;
13590 SET_EXPR_LOCATION (stmt, loc);
13591 add_stmt (stmt);
13593 return stmt;
13596 /* OpenACC 2.0:
13597 # pragma acc data oacc-data-clause[optseq] new-line
13598 structured-block
13600 LOC is the location of the #pragma token.
13603 #define OACC_DATA_CLAUSE_MASK \
13604 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13616 static tree
13617 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13619 tree stmt, clauses, block;
13621 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13622 "#pragma acc data");
13624 block = c_begin_omp_parallel ();
13625 add_stmt (c_parser_omp_structured_block (parser, if_p));
13627 stmt = c_finish_oacc_data (loc, clauses, block);
13629 return stmt;
13632 /* OpenACC 2.0:
13633 # pragma acc declare oacc-data-clause[optseq] new-line
13636 #define OACC_DECLARE_CLAUSE_MASK \
13637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13650 static void
13651 c_parser_oacc_declare (c_parser *parser)
13653 location_t pragma_loc = c_parser_peek_token (parser)->location;
13654 tree clauses, stmt, t, decl;
13656 bool error = false;
13658 c_parser_consume_pragma (parser);
13660 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13661 "#pragma acc declare");
13662 if (!clauses)
13664 error_at (pragma_loc,
13665 "no valid clauses specified in %<#pragma acc declare%>");
13666 return;
13669 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13671 location_t loc = OMP_CLAUSE_LOCATION (t);
13672 decl = OMP_CLAUSE_DECL (t);
13673 if (!DECL_P (decl))
13675 error_at (loc, "array section in %<#pragma acc declare%>");
13676 error = true;
13677 continue;
13680 switch (OMP_CLAUSE_MAP_KIND (t))
13682 case GOMP_MAP_FIRSTPRIVATE_POINTER:
13683 case GOMP_MAP_FORCE_ALLOC:
13684 case GOMP_MAP_FORCE_TO:
13685 case GOMP_MAP_FORCE_DEVICEPTR:
13686 case GOMP_MAP_DEVICE_RESIDENT:
13687 break;
13689 case GOMP_MAP_LINK:
13690 if (!global_bindings_p ()
13691 && (TREE_STATIC (decl)
13692 || !DECL_EXTERNAL (decl)))
13694 error_at (loc,
13695 "%qD must be a global variable in"
13696 "%<#pragma acc declare link%>",
13697 decl);
13698 error = true;
13699 continue;
13701 break;
13703 default:
13704 if (global_bindings_p ())
13706 error_at (loc, "invalid OpenACC clause at file scope");
13707 error = true;
13708 continue;
13710 if (DECL_EXTERNAL (decl))
13712 error_at (loc,
13713 "invalid use of %<extern%> variable %qD "
13714 "in %<#pragma acc declare%>", decl);
13715 error = true;
13716 continue;
13718 else if (TREE_PUBLIC (decl))
13720 error_at (loc,
13721 "invalid use of %<global%> variable %qD "
13722 "in %<#pragma acc declare%>", decl);
13723 error = true;
13724 continue;
13726 break;
13729 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13730 || lookup_attribute ("omp declare target link",
13731 DECL_ATTRIBUTES (decl)))
13733 error_at (loc, "variable %qD used more than once with "
13734 "%<#pragma acc declare%>", decl);
13735 error = true;
13736 continue;
13739 if (!error)
13741 tree id;
13743 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13744 id = get_identifier ("omp declare target link");
13745 else
13746 id = get_identifier ("omp declare target");
13748 DECL_ATTRIBUTES (decl)
13749 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13751 if (global_bindings_p ())
13753 symtab_node *node = symtab_node::get (decl);
13754 if (node != NULL)
13756 node->offloadable = 1;
13757 if (ENABLE_OFFLOADING)
13759 g->have_offload = true;
13760 if (is_a <varpool_node *> (node))
13761 vec_safe_push (offload_vars, decl);
13768 if (error || global_bindings_p ())
13769 return;
13771 stmt = make_node (OACC_DECLARE);
13772 TREE_TYPE (stmt) = void_type_node;
13773 OACC_DECLARE_CLAUSES (stmt) = clauses;
13774 SET_EXPR_LOCATION (stmt, pragma_loc);
13776 add_stmt (stmt);
13778 return;
13781 /* OpenACC 2.0:
13782 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13786 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13789 LOC is the location of the #pragma token.
13792 #define OACC_ENTER_DATA_CLAUSE_MASK \
13793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13801 #define OACC_EXIT_DATA_CLAUSE_MASK \
13802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13808 static void
13809 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13811 location_t loc = c_parser_peek_token (parser)->location;
13812 tree clauses, stmt;
13814 c_parser_consume_pragma (parser);
13816 if (!c_parser_next_token_is (parser, CPP_NAME))
13818 c_parser_error (parser, enter
13819 ? "expected %<data%> in %<#pragma acc enter data%>"
13820 : "expected %<data%> in %<#pragma acc exit data%>");
13821 c_parser_skip_to_pragma_eol (parser);
13822 return;
13825 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13826 if (strcmp (p, "data") != 0)
13828 c_parser_error (parser, "invalid pragma");
13829 c_parser_skip_to_pragma_eol (parser);
13830 return;
13833 c_parser_consume_token (parser);
13835 if (enter)
13836 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13837 "#pragma acc enter data");
13838 else
13839 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13840 "#pragma acc exit data");
13842 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13844 error_at (loc, enter
13845 ? "%<#pragma acc enter data%> has no data movement clause"
13846 : "%<#pragma acc exit data%> has no data movement clause");
13847 return;
13850 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13851 TREE_TYPE (stmt) = void_type_node;
13852 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13853 SET_EXPR_LOCATION (stmt, loc);
13854 add_stmt (stmt);
13858 /* OpenACC 2.0:
13859 # pragma acc host_data oacc-data-clause[optseq] new-line
13860 structured-block
13863 #define OACC_HOST_DATA_CLAUSE_MASK \
13864 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13866 static tree
13867 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
13869 tree stmt, clauses, block;
13871 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13872 "#pragma acc host_data");
13874 block = c_begin_omp_parallel ();
13875 add_stmt (c_parser_omp_structured_block (parser, if_p));
13876 stmt = c_finish_oacc_host_data (loc, clauses, block);
13877 return stmt;
13881 /* OpenACC 2.0:
13883 # pragma acc loop oacc-loop-clause[optseq] new-line
13884 structured-block
13886 LOC is the location of the #pragma token.
13889 #define OACC_LOOP_CLAUSE_MASK \
13890 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
13898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
13900 static tree
13901 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
13902 omp_clause_mask mask, tree *cclauses, bool *if_p)
13904 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
13906 strcat (p_name, " loop");
13907 mask |= OACC_LOOP_CLAUSE_MASK;
13909 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
13910 cclauses == NULL);
13911 if (cclauses)
13913 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
13914 if (*cclauses)
13915 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
13916 if (clauses)
13917 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13920 tree block = c_begin_compound_stmt (true);
13921 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
13922 if_p);
13923 block = c_end_compound_stmt (loc, block, true);
13924 add_stmt (block);
13926 return stmt;
13929 /* OpenACC 2.0:
13930 # pragma acc kernels oacc-kernels-clause[optseq] new-line
13931 structured-block
13935 # pragma acc parallel oacc-parallel-clause[optseq] new-line
13936 structured-block
13938 LOC is the location of the #pragma token.
13941 #define OACC_KERNELS_CLAUSE_MASK \
13942 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13957 #define OACC_PARALLEL_CLAUSE_MASK \
13958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
13968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
13969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
13970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
13977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13979 static tree
13980 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
13981 enum pragma_kind p_kind, char *p_name,
13982 bool *if_p)
13984 omp_clause_mask mask;
13985 enum tree_code code;
13986 switch (p_kind)
13988 case PRAGMA_OACC_KERNELS:
13989 strcat (p_name, " kernels");
13990 mask = OACC_KERNELS_CLAUSE_MASK;
13991 code = OACC_KERNELS;
13992 break;
13993 case PRAGMA_OACC_PARALLEL:
13994 strcat (p_name, " parallel");
13995 mask = OACC_PARALLEL_CLAUSE_MASK;
13996 code = OACC_PARALLEL;
13997 break;
13998 default:
13999 gcc_unreachable ();
14002 if (c_parser_next_token_is (parser, CPP_NAME))
14004 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14005 if (strcmp (p, "loop") == 0)
14007 c_parser_consume_token (parser);
14008 tree block = c_begin_omp_parallel ();
14009 tree clauses;
14010 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14011 return c_finish_omp_construct (loc, code, block, clauses);
14015 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14017 tree block = c_begin_omp_parallel ();
14018 add_stmt (c_parser_omp_structured_block (parser, if_p));
14020 return c_finish_omp_construct (loc, code, block, clauses);
14023 /* OpenACC 2.0:
14024 # pragma acc routine oacc-routine-clause[optseq] new-line
14025 function-definition
14027 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14030 #define OACC_ROUTINE_CLAUSE_MASK \
14031 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14036 /* Parse an OpenACC routine directive. For named directives, we apply
14037 immediately to the named function. For unnamed ones we then parse
14038 a declaration or definition, which must be for a function. */
14040 static void
14041 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14043 gcc_checking_assert (context == pragma_external);
14045 oacc_routine_data data;
14046 data.error_seen = false;
14047 data.fndecl_seen = false;
14048 data.clauses = NULL_TREE;
14049 data.loc = c_parser_peek_token (parser)->location;
14051 c_parser_consume_pragma (parser);
14053 /* Look for optional '( name )'. */
14054 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14056 c_parser_consume_token (parser); /* '(' */
14058 tree decl = NULL_TREE;
14059 c_token *name_token = c_parser_peek_token (parser);
14060 location_t name_loc = name_token->location;
14061 if (name_token->type == CPP_NAME
14062 && (name_token->id_kind == C_ID_ID
14063 || name_token->id_kind == C_ID_TYPENAME))
14065 decl = lookup_name (name_token->value);
14066 if (!decl)
14067 error_at (name_loc,
14068 "%qE has not been declared", name_token->value);
14069 c_parser_consume_token (parser);
14071 else
14072 c_parser_error (parser, "expected function name");
14074 if (!decl
14075 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14077 c_parser_skip_to_pragma_eol (parser, false);
14078 return;
14081 data.clauses
14082 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14083 "#pragma acc routine");
14085 if (TREE_CODE (decl) != FUNCTION_DECL)
14087 error_at (name_loc, "%qD does not refer to a function", decl);
14088 return;
14091 c_finish_oacc_routine (&data, decl, false);
14093 else /* No optional '( name )'. */
14095 data.clauses
14096 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14097 "#pragma acc routine");
14099 /* Emit a helpful diagnostic if there's another pragma following this
14100 one. Also don't allow a static assertion declaration, as in the
14101 following we'll just parse a *single* "declaration or function
14102 definition", and the static assertion counts an one. */
14103 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14104 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14106 error_at (data.loc,
14107 "%<#pragma acc routine%> not immediately followed by"
14108 " function declaration or definition");
14109 /* ..., and then just keep going. */
14110 return;
14113 /* We only have to consider the pragma_external case here. */
14114 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14115 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14117 int ext = disable_extension_diagnostics ();
14119 c_parser_consume_token (parser);
14120 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14121 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14122 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14123 NULL, vNULL, &data);
14124 restore_extension_diagnostics (ext);
14126 else
14127 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14128 NULL, vNULL, &data);
14132 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14133 IS_DEFN is true if we're applying it to the definition. */
14135 static void
14136 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14137 bool is_defn)
14139 /* Keep going if we're in error reporting mode. */
14140 if (data->error_seen
14141 || fndecl == error_mark_node)
14142 return;
14144 if (data->fndecl_seen)
14146 error_at (data->loc,
14147 "%<#pragma acc routine%> not immediately followed by"
14148 " a single function declaration or definition");
14149 data->error_seen = true;
14150 return;
14152 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14154 error_at (data->loc,
14155 "%<#pragma acc routine%> not immediately followed by"
14156 " function declaration or definition");
14157 data->error_seen = true;
14158 return;
14161 if (get_oacc_fn_attrib (fndecl))
14163 error_at (data->loc,
14164 "%<#pragma acc routine%> already applied to %qD", fndecl);
14165 data->error_seen = true;
14166 return;
14169 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14171 error_at (data->loc,
14172 "%<#pragma acc routine%> must be applied before %s",
14173 TREE_USED (fndecl) ? "use" : "definition");
14174 data->error_seen = true;
14175 return;
14178 /* Process the routine's dimension clauses. */
14179 tree dims = build_oacc_routine_dims (data->clauses);
14180 replace_oacc_fn_attrib (fndecl, dims);
14182 /* Add an "omp declare target" attribute. */
14183 DECL_ATTRIBUTES (fndecl)
14184 = tree_cons (get_identifier ("omp declare target"),
14185 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14187 /* Remember that we've used this "#pragma acc routine". */
14188 data->fndecl_seen = true;
14191 /* OpenACC 2.0:
14192 # pragma acc update oacc-update-clause[optseq] new-line
14195 #define OACC_UPDATE_CLAUSE_MASK \
14196 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14203 static void
14204 c_parser_oacc_update (c_parser *parser)
14206 location_t loc = c_parser_peek_token (parser)->location;
14208 c_parser_consume_pragma (parser);
14210 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14211 "#pragma acc update");
14212 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14214 error_at (loc,
14215 "%<#pragma acc update%> must contain at least one "
14216 "%<device%> or %<host%> or %<self%> clause");
14217 return;
14220 if (parser->error)
14221 return;
14223 tree stmt = make_node (OACC_UPDATE);
14224 TREE_TYPE (stmt) = void_type_node;
14225 OACC_UPDATE_CLAUSES (stmt) = clauses;
14226 SET_EXPR_LOCATION (stmt, loc);
14227 add_stmt (stmt);
14230 /* OpenACC 2.0:
14231 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14233 LOC is the location of the #pragma token.
14236 #define OACC_WAIT_CLAUSE_MASK \
14237 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14239 static tree
14240 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14242 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14244 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14245 list = c_parser_oacc_wait_list (parser, loc, list);
14247 strcpy (p_name, " wait");
14248 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14249 stmt = c_finish_oacc_wait (loc, list, clauses);
14250 add_stmt (stmt);
14252 return stmt;
14255 /* OpenMP 2.5:
14256 # pragma omp atomic new-line
14257 expression-stmt
14259 expression-stmt:
14260 x binop= expr | x++ | ++x | x-- | --x
14261 binop:
14262 +, *, -, /, &, ^, |, <<, >>
14264 where x is an lvalue expression with scalar type.
14266 OpenMP 3.1:
14267 # pragma omp atomic new-line
14268 update-stmt
14270 # pragma omp atomic read new-line
14271 read-stmt
14273 # pragma omp atomic write new-line
14274 write-stmt
14276 # pragma omp atomic update new-line
14277 update-stmt
14279 # pragma omp atomic capture new-line
14280 capture-stmt
14282 # pragma omp atomic capture new-line
14283 capture-block
14285 read-stmt:
14286 v = x
14287 write-stmt:
14288 x = expr
14289 update-stmt:
14290 expression-stmt | x = x binop expr
14291 capture-stmt:
14292 v = expression-stmt
14293 capture-block:
14294 { v = x; update-stmt; } | { update-stmt; v = x; }
14296 OpenMP 4.0:
14297 update-stmt:
14298 expression-stmt | x = x binop expr | x = expr binop x
14299 capture-stmt:
14300 v = update-stmt
14301 capture-block:
14302 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14304 where x and v are lvalue expressions with scalar type.
14306 LOC is the location of the #pragma token. */
14308 static void
14309 c_parser_omp_atomic (location_t loc, c_parser *parser)
14311 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14312 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14313 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14314 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14315 struct c_expr expr;
14316 location_t eloc;
14317 bool structured_block = false;
14318 bool swapped = false;
14319 bool seq_cst = false;
14320 bool non_lvalue_p;
14322 if (c_parser_next_token_is (parser, CPP_NAME))
14324 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14325 if (!strcmp (p, "seq_cst"))
14327 seq_cst = true;
14328 c_parser_consume_token (parser);
14329 if (c_parser_next_token_is (parser, CPP_COMMA)
14330 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14331 c_parser_consume_token (parser);
14334 if (c_parser_next_token_is (parser, CPP_NAME))
14336 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14338 if (!strcmp (p, "read"))
14339 code = OMP_ATOMIC_READ;
14340 else if (!strcmp (p, "write"))
14341 code = NOP_EXPR;
14342 else if (!strcmp (p, "update"))
14343 code = OMP_ATOMIC;
14344 else if (!strcmp (p, "capture"))
14345 code = OMP_ATOMIC_CAPTURE_NEW;
14346 else
14347 p = NULL;
14348 if (p)
14349 c_parser_consume_token (parser);
14351 if (!seq_cst)
14353 if (c_parser_next_token_is (parser, CPP_COMMA)
14354 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14355 c_parser_consume_token (parser);
14357 if (c_parser_next_token_is (parser, CPP_NAME))
14359 const char *p
14360 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14361 if (!strcmp (p, "seq_cst"))
14363 seq_cst = true;
14364 c_parser_consume_token (parser);
14368 c_parser_skip_to_pragma_eol (parser);
14370 switch (code)
14372 case OMP_ATOMIC_READ:
14373 case NOP_EXPR: /* atomic write */
14374 v = c_parser_cast_expression (parser, NULL).value;
14375 non_lvalue_p = !lvalue_p (v);
14376 v = c_fully_fold (v, false, NULL);
14377 if (v == error_mark_node)
14378 goto saw_error;
14379 if (non_lvalue_p)
14380 v = non_lvalue (v);
14381 loc = c_parser_peek_token (parser)->location;
14382 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14383 goto saw_error;
14384 if (code == NOP_EXPR)
14386 lhs = c_parser_expression (parser).value;
14387 lhs = c_fully_fold (lhs, false, NULL);
14388 if (lhs == error_mark_node)
14389 goto saw_error;
14391 else
14393 lhs = c_parser_cast_expression (parser, NULL).value;
14394 non_lvalue_p = !lvalue_p (lhs);
14395 lhs = c_fully_fold (lhs, false, NULL);
14396 if (lhs == error_mark_node)
14397 goto saw_error;
14398 if (non_lvalue_p)
14399 lhs = non_lvalue (lhs);
14401 if (code == NOP_EXPR)
14403 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14404 opcode. */
14405 code = OMP_ATOMIC;
14406 rhs = lhs;
14407 lhs = v;
14408 v = NULL_TREE;
14410 goto done;
14411 case OMP_ATOMIC_CAPTURE_NEW:
14412 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14414 c_parser_consume_token (parser);
14415 structured_block = true;
14417 else
14419 v = c_parser_cast_expression (parser, NULL).value;
14420 non_lvalue_p = !lvalue_p (v);
14421 v = c_fully_fold (v, false, NULL);
14422 if (v == error_mark_node)
14423 goto saw_error;
14424 if (non_lvalue_p)
14425 v = non_lvalue (v);
14426 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14427 goto saw_error;
14429 break;
14430 default:
14431 break;
14434 /* For structured_block case we don't know yet whether
14435 old or new x should be captured. */
14436 restart:
14437 eloc = c_parser_peek_token (parser)->location;
14438 expr = c_parser_cast_expression (parser, NULL);
14439 lhs = expr.value;
14440 expr = default_function_array_conversion (eloc, expr);
14441 unfolded_lhs = expr.value;
14442 lhs = c_fully_fold (lhs, false, NULL);
14443 orig_lhs = lhs;
14444 switch (TREE_CODE (lhs))
14446 case ERROR_MARK:
14447 saw_error:
14448 c_parser_skip_to_end_of_block_or_statement (parser);
14449 if (structured_block)
14451 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14452 c_parser_consume_token (parser);
14453 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14455 c_parser_skip_to_end_of_block_or_statement (parser);
14456 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14457 c_parser_consume_token (parser);
14460 return;
14462 case POSTINCREMENT_EXPR:
14463 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14464 code = OMP_ATOMIC_CAPTURE_OLD;
14465 /* FALLTHROUGH */
14466 case PREINCREMENT_EXPR:
14467 lhs = TREE_OPERAND (lhs, 0);
14468 unfolded_lhs = NULL_TREE;
14469 opcode = PLUS_EXPR;
14470 rhs = integer_one_node;
14471 break;
14473 case POSTDECREMENT_EXPR:
14474 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14475 code = OMP_ATOMIC_CAPTURE_OLD;
14476 /* FALLTHROUGH */
14477 case PREDECREMENT_EXPR:
14478 lhs = TREE_OPERAND (lhs, 0);
14479 unfolded_lhs = NULL_TREE;
14480 opcode = MINUS_EXPR;
14481 rhs = integer_one_node;
14482 break;
14484 case COMPOUND_EXPR:
14485 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14486 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14487 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14488 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14489 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14490 (TREE_OPERAND (lhs, 1), 0), 0)))
14491 == BOOLEAN_TYPE)
14492 /* Undo effects of boolean_increment for post {in,de}crement. */
14493 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14494 /* FALLTHRU */
14495 case MODIFY_EXPR:
14496 if (TREE_CODE (lhs) == MODIFY_EXPR
14497 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14499 /* Undo effects of boolean_increment. */
14500 if (integer_onep (TREE_OPERAND (lhs, 1)))
14502 /* This is pre or post increment. */
14503 rhs = TREE_OPERAND (lhs, 1);
14504 lhs = TREE_OPERAND (lhs, 0);
14505 unfolded_lhs = NULL_TREE;
14506 opcode = NOP_EXPR;
14507 if (code == OMP_ATOMIC_CAPTURE_NEW
14508 && !structured_block
14509 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14510 code = OMP_ATOMIC_CAPTURE_OLD;
14511 break;
14513 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14514 && TREE_OPERAND (lhs, 0)
14515 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14517 /* This is pre or post decrement. */
14518 rhs = TREE_OPERAND (lhs, 1);
14519 lhs = TREE_OPERAND (lhs, 0);
14520 unfolded_lhs = NULL_TREE;
14521 opcode = NOP_EXPR;
14522 if (code == OMP_ATOMIC_CAPTURE_NEW
14523 && !structured_block
14524 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14525 code = OMP_ATOMIC_CAPTURE_OLD;
14526 break;
14529 /* FALLTHRU */
14530 default:
14531 if (!lvalue_p (unfolded_lhs))
14532 lhs = non_lvalue (lhs);
14533 switch (c_parser_peek_token (parser)->type)
14535 case CPP_MULT_EQ:
14536 opcode = MULT_EXPR;
14537 break;
14538 case CPP_DIV_EQ:
14539 opcode = TRUNC_DIV_EXPR;
14540 break;
14541 case CPP_PLUS_EQ:
14542 opcode = PLUS_EXPR;
14543 break;
14544 case CPP_MINUS_EQ:
14545 opcode = MINUS_EXPR;
14546 break;
14547 case CPP_LSHIFT_EQ:
14548 opcode = LSHIFT_EXPR;
14549 break;
14550 case CPP_RSHIFT_EQ:
14551 opcode = RSHIFT_EXPR;
14552 break;
14553 case CPP_AND_EQ:
14554 opcode = BIT_AND_EXPR;
14555 break;
14556 case CPP_OR_EQ:
14557 opcode = BIT_IOR_EXPR;
14558 break;
14559 case CPP_XOR_EQ:
14560 opcode = BIT_XOR_EXPR;
14561 break;
14562 case CPP_EQ:
14563 c_parser_consume_token (parser);
14564 eloc = c_parser_peek_token (parser)->location;
14565 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14566 rhs1 = expr.value;
14567 switch (TREE_CODE (rhs1))
14569 case MULT_EXPR:
14570 case TRUNC_DIV_EXPR:
14571 case RDIV_EXPR:
14572 case PLUS_EXPR:
14573 case MINUS_EXPR:
14574 case LSHIFT_EXPR:
14575 case RSHIFT_EXPR:
14576 case BIT_AND_EXPR:
14577 case BIT_IOR_EXPR:
14578 case BIT_XOR_EXPR:
14579 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14581 opcode = TREE_CODE (rhs1);
14582 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14583 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14584 goto stmt_done;
14586 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14588 opcode = TREE_CODE (rhs1);
14589 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14590 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14591 swapped = !commutative_tree_code (opcode);
14592 goto stmt_done;
14594 break;
14595 case ERROR_MARK:
14596 goto saw_error;
14597 default:
14598 break;
14600 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14602 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14604 code = OMP_ATOMIC_CAPTURE_OLD;
14605 v = lhs;
14606 lhs = NULL_TREE;
14607 expr = default_function_array_read_conversion (eloc, expr);
14608 unfolded_lhs1 = expr.value;
14609 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14610 rhs1 = NULL_TREE;
14611 c_parser_consume_token (parser);
14612 goto restart;
14614 if (structured_block)
14616 opcode = NOP_EXPR;
14617 expr = default_function_array_read_conversion (eloc, expr);
14618 rhs = c_fully_fold (expr.value, false, NULL);
14619 rhs1 = NULL_TREE;
14620 goto stmt_done;
14623 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14624 goto saw_error;
14625 default:
14626 c_parser_error (parser,
14627 "invalid operator for %<#pragma omp atomic%>");
14628 goto saw_error;
14631 /* Arrange to pass the location of the assignment operator to
14632 c_finish_omp_atomic. */
14633 loc = c_parser_peek_token (parser)->location;
14634 c_parser_consume_token (parser);
14635 eloc = c_parser_peek_token (parser)->location;
14636 expr = c_parser_expression (parser);
14637 expr = default_function_array_read_conversion (eloc, expr);
14638 rhs = expr.value;
14639 rhs = c_fully_fold (rhs, false, NULL);
14640 break;
14642 stmt_done:
14643 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14645 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14646 goto saw_error;
14647 v = c_parser_cast_expression (parser, NULL).value;
14648 non_lvalue_p = !lvalue_p (v);
14649 v = c_fully_fold (v, false, NULL);
14650 if (v == error_mark_node)
14651 goto saw_error;
14652 if (non_lvalue_p)
14653 v = non_lvalue (v);
14654 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14655 goto saw_error;
14656 eloc = c_parser_peek_token (parser)->location;
14657 expr = c_parser_cast_expression (parser, NULL);
14658 lhs1 = expr.value;
14659 expr = default_function_array_read_conversion (eloc, expr);
14660 unfolded_lhs1 = expr.value;
14661 lhs1 = c_fully_fold (lhs1, false, NULL);
14662 if (lhs1 == error_mark_node)
14663 goto saw_error;
14664 if (!lvalue_p (unfolded_lhs1))
14665 lhs1 = non_lvalue (lhs1);
14667 if (structured_block)
14669 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14670 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14672 done:
14673 if (unfolded_lhs && unfolded_lhs1
14674 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14676 error ("%<#pragma omp atomic capture%> uses two different "
14677 "expressions for memory");
14678 stmt = error_mark_node;
14680 else
14681 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14682 swapped, seq_cst);
14683 if (stmt != error_mark_node)
14684 add_stmt (stmt);
14686 if (!structured_block)
14687 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14691 /* OpenMP 2.5:
14692 # pragma omp barrier new-line
14695 static void
14696 c_parser_omp_barrier (c_parser *parser)
14698 location_t loc = c_parser_peek_token (parser)->location;
14699 c_parser_consume_pragma (parser);
14700 c_parser_skip_to_pragma_eol (parser);
14702 c_finish_omp_barrier (loc);
14705 /* OpenMP 2.5:
14706 # pragma omp critical [(name)] new-line
14707 structured-block
14709 OpenMP 4.5:
14710 # pragma omp critical [(name) [hint(expression)]] new-line
14712 LOC is the location of the #pragma itself. */
14714 #define OMP_CRITICAL_CLAUSE_MASK \
14715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14717 static tree
14718 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
14720 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14722 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14724 c_parser_consume_token (parser);
14725 if (c_parser_next_token_is (parser, CPP_NAME))
14727 name = c_parser_peek_token (parser)->value;
14728 c_parser_consume_token (parser);
14729 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14731 else
14732 c_parser_error (parser, "expected identifier");
14734 clauses = c_parser_omp_all_clauses (parser,
14735 OMP_CRITICAL_CLAUSE_MASK,
14736 "#pragma omp critical");
14738 else
14740 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14741 c_parser_error (parser, "expected %<(%> or end of line");
14742 c_parser_skip_to_pragma_eol (parser);
14745 stmt = c_parser_omp_structured_block (parser, if_p);
14746 return c_finish_omp_critical (loc, stmt, name, clauses);
14749 /* OpenMP 2.5:
14750 # pragma omp flush flush-vars[opt] new-line
14752 flush-vars:
14753 ( variable-list ) */
14755 static void
14756 c_parser_omp_flush (c_parser *parser)
14758 location_t loc = c_parser_peek_token (parser)->location;
14759 c_parser_consume_pragma (parser);
14760 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14761 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14762 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14763 c_parser_error (parser, "expected %<(%> or end of line");
14764 c_parser_skip_to_pragma_eol (parser);
14766 c_finish_omp_flush (loc);
14769 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14770 The real trick here is to determine the loop control variable early
14771 so that we can push a new decl if necessary to make it private.
14772 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14773 respectively. */
14775 static tree
14776 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14777 tree clauses, tree *cclauses, bool *if_p)
14779 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14780 tree declv, condv, incrv, initv, ret = NULL_TREE;
14781 tree pre_body = NULL_TREE, this_pre_body;
14782 tree ordered_cl = NULL_TREE;
14783 bool fail = false, open_brace_parsed = false;
14784 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14785 location_t for_loc;
14786 vec<tree, va_gc> *for_block = make_tree_vector ();
14788 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14789 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14790 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14791 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14792 && OMP_CLAUSE_ORDERED_EXPR (cl))
14794 ordered_cl = cl;
14795 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14798 if (ordered && ordered < collapse)
14800 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14801 "%<ordered%> clause parameter is less than %<collapse%>");
14802 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14803 = build_int_cst (NULL_TREE, collapse);
14804 ordered = collapse;
14806 if (ordered)
14808 for (tree *pc = &clauses; *pc; )
14809 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14811 error_at (OMP_CLAUSE_LOCATION (*pc),
14812 "%<linear%> clause may not be specified together "
14813 "with %<ordered%> clause with a parameter");
14814 *pc = OMP_CLAUSE_CHAIN (*pc);
14816 else
14817 pc = &OMP_CLAUSE_CHAIN (*pc);
14820 gcc_assert (collapse >= 1 && ordered >= 0);
14821 count = ordered ? ordered : collapse;
14823 declv = make_tree_vec (count);
14824 initv = make_tree_vec (count);
14825 condv = make_tree_vec (count);
14826 incrv = make_tree_vec (count);
14828 if (code != CILK_FOR
14829 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14831 c_parser_error (parser, "for statement expected");
14832 return NULL;
14834 if (code == CILK_FOR
14835 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14837 c_parser_error (parser, "_Cilk_for statement expected");
14838 return NULL;
14840 for_loc = c_parser_peek_token (parser)->location;
14841 c_parser_consume_token (parser);
14843 for (i = 0; i < count; i++)
14845 int bracecount = 0;
14847 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14848 goto pop_scopes;
14850 /* Parse the initialization declaration or expression. */
14851 if (c_parser_next_tokens_start_declaration (parser))
14853 if (i > 0)
14854 vec_safe_push (for_block, c_begin_compound_stmt (true));
14855 this_pre_body = push_stmt_list ();
14856 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14857 NULL, vNULL);
14858 if (this_pre_body)
14860 this_pre_body = pop_stmt_list (this_pre_body);
14861 if (pre_body)
14863 tree t = pre_body;
14864 pre_body = push_stmt_list ();
14865 add_stmt (t);
14866 add_stmt (this_pre_body);
14867 pre_body = pop_stmt_list (pre_body);
14869 else
14870 pre_body = this_pre_body;
14872 decl = check_for_loop_decls (for_loc, flag_isoc99);
14873 if (decl == NULL)
14874 goto error_init;
14875 if (DECL_INITIAL (decl) == error_mark_node)
14876 decl = error_mark_node;
14877 init = decl;
14879 else if (c_parser_next_token_is (parser, CPP_NAME)
14880 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
14882 struct c_expr decl_exp;
14883 struct c_expr init_exp;
14884 location_t init_loc;
14886 decl_exp = c_parser_postfix_expression (parser);
14887 decl = decl_exp.value;
14889 c_parser_require (parser, CPP_EQ, "expected %<=%>");
14891 init_loc = c_parser_peek_token (parser)->location;
14892 init_exp = c_parser_expr_no_commas (parser, NULL);
14893 init_exp = default_function_array_read_conversion (init_loc,
14894 init_exp);
14895 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
14896 NOP_EXPR, init_loc, init_exp.value,
14897 init_exp.original_type);
14898 init = c_process_expr_stmt (init_loc, init);
14900 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14902 else
14904 error_init:
14905 c_parser_error (parser,
14906 "expected iteration declaration or initialization");
14907 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14908 "expected %<)%>");
14909 fail = true;
14910 goto parse_next;
14913 /* Parse the loop condition. */
14914 cond = NULL_TREE;
14915 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
14917 location_t cond_loc = c_parser_peek_token (parser)->location;
14918 struct c_expr cond_expr
14919 = c_parser_binary_expression (parser, NULL, NULL_TREE);
14921 cond = cond_expr.value;
14922 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
14923 cond = c_fully_fold (cond, false, NULL);
14924 switch (cond_expr.original_code)
14926 case GT_EXPR:
14927 case GE_EXPR:
14928 case LT_EXPR:
14929 case LE_EXPR:
14930 break;
14931 case NE_EXPR:
14932 if (code == CILK_SIMD || code == CILK_FOR)
14933 break;
14934 /* FALLTHRU. */
14935 default:
14936 /* Can't be cond = error_mark_node, because we want to preserve
14937 the location until c_finish_omp_for. */
14938 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
14939 break;
14941 protected_set_expr_location (cond, cond_loc);
14943 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14945 /* Parse the increment expression. */
14946 incr = NULL_TREE;
14947 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
14949 location_t incr_loc = c_parser_peek_token (parser)->location;
14951 incr = c_process_expr_stmt (incr_loc,
14952 c_parser_expression (parser).value);
14954 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14956 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
14957 fail = true;
14958 else
14960 TREE_VEC_ELT (declv, i) = decl;
14961 TREE_VEC_ELT (initv, i) = init;
14962 TREE_VEC_ELT (condv, i) = cond;
14963 TREE_VEC_ELT (incrv, i) = incr;
14966 parse_next:
14967 if (i == count - 1)
14968 break;
14970 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
14971 in between the collapsed for loops to be still considered perfectly
14972 nested. Hopefully the final version clarifies this.
14973 For now handle (multiple) {'s and empty statements. */
14976 if (c_parser_next_token_is_keyword (parser, RID_FOR))
14978 c_parser_consume_token (parser);
14979 break;
14981 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14983 c_parser_consume_token (parser);
14984 bracecount++;
14986 else if (bracecount
14987 && c_parser_next_token_is (parser, CPP_SEMICOLON))
14988 c_parser_consume_token (parser);
14989 else
14991 c_parser_error (parser, "not enough perfectly nested loops");
14992 if (bracecount)
14994 open_brace_parsed = true;
14995 bracecount--;
14997 fail = true;
14998 count = 0;
14999 break;
15002 while (1);
15004 nbraces += bracecount;
15007 if (nbraces)
15008 if_p = NULL;
15010 save_break = c_break_label;
15011 if (code == CILK_SIMD)
15012 c_break_label = build_int_cst (size_type_node, 2);
15013 else
15014 c_break_label = size_one_node;
15015 save_cont = c_cont_label;
15016 c_cont_label = NULL_TREE;
15017 body = push_stmt_list ();
15019 if (open_brace_parsed)
15021 location_t here = c_parser_peek_token (parser)->location;
15022 stmt = c_begin_compound_stmt (true);
15023 c_parser_compound_statement_nostart (parser);
15024 add_stmt (c_end_compound_stmt (here, stmt, true));
15026 else
15027 add_stmt (c_parser_c99_block_statement (parser, if_p));
15028 if (c_cont_label)
15030 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15031 SET_EXPR_LOCATION (t, loc);
15032 add_stmt (t);
15035 body = pop_stmt_list (body);
15036 c_break_label = save_break;
15037 c_cont_label = save_cont;
15039 while (nbraces)
15041 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15043 c_parser_consume_token (parser);
15044 nbraces--;
15046 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15047 c_parser_consume_token (parser);
15048 else
15050 c_parser_error (parser, "collapsed loops not perfectly nested");
15051 while (nbraces)
15053 location_t here = c_parser_peek_token (parser)->location;
15054 stmt = c_begin_compound_stmt (true);
15055 add_stmt (body);
15056 c_parser_compound_statement_nostart (parser);
15057 body = c_end_compound_stmt (here, stmt, true);
15058 nbraces--;
15060 goto pop_scopes;
15064 /* Only bother calling c_finish_omp_for if we haven't already generated
15065 an error from the initialization parsing. */
15066 if (!fail)
15068 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15069 incrv, body, pre_body);
15071 /* Check for iterators appearing in lb, b or incr expressions. */
15072 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15073 stmt = NULL_TREE;
15075 if (stmt)
15077 add_stmt (stmt);
15079 if (cclauses != NULL
15080 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15082 tree *c;
15083 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15084 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15085 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15086 c = &OMP_CLAUSE_CHAIN (*c);
15087 else
15089 for (i = 0; i < count; i++)
15090 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15091 break;
15092 if (i == count)
15093 c = &OMP_CLAUSE_CHAIN (*c);
15094 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15096 error_at (loc,
15097 "iteration variable %qD should not be firstprivate",
15098 OMP_CLAUSE_DECL (*c));
15099 *c = OMP_CLAUSE_CHAIN (*c);
15101 else
15103 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15104 tree l = *c;
15105 *c = OMP_CLAUSE_CHAIN (*c);
15106 if (code == OMP_SIMD)
15108 OMP_CLAUSE_CHAIN (l)
15109 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15110 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15112 else
15114 OMP_CLAUSE_CHAIN (l) = clauses;
15115 clauses = l;
15120 OMP_FOR_CLAUSES (stmt) = clauses;
15122 ret = stmt;
15124 pop_scopes:
15125 while (!for_block->is_empty ())
15127 /* FIXME diagnostics: LOC below should be the actual location of
15128 this particular for block. We need to build a list of
15129 locations to go along with FOR_BLOCK. */
15130 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15131 add_stmt (stmt);
15133 release_tree_vector (for_block);
15134 return ret;
15137 /* Helper function for OpenMP parsing, split clauses and call
15138 finish_omp_clauses on each of the set of clauses afterwards. */
15140 static void
15141 omp_split_clauses (location_t loc, enum tree_code code,
15142 omp_clause_mask mask, tree clauses, tree *cclauses)
15144 int i;
15145 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15146 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15147 if (cclauses[i])
15148 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15151 /* OpenMP 4.0:
15152 #pragma omp simd simd-clause[optseq] new-line
15153 for-loop
15155 LOC is the location of the #pragma token.
15158 #define OMP_SIMD_CLAUSE_MASK \
15159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15168 static tree
15169 c_parser_omp_simd (location_t loc, c_parser *parser,
15170 char *p_name, omp_clause_mask mask, tree *cclauses,
15171 bool *if_p)
15173 tree block, clauses, ret;
15175 strcat (p_name, " simd");
15176 mask |= OMP_SIMD_CLAUSE_MASK;
15178 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15179 if (cclauses)
15181 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15182 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15183 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15184 OMP_CLAUSE_ORDERED);
15185 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15187 error_at (OMP_CLAUSE_LOCATION (c),
15188 "%<ordered%> clause with parameter may not be specified "
15189 "on %qs construct", p_name);
15190 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15194 block = c_begin_compound_stmt (true);
15195 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15196 block = c_end_compound_stmt (loc, block, true);
15197 add_stmt (block);
15199 return ret;
15202 /* OpenMP 2.5:
15203 #pragma omp for for-clause[optseq] new-line
15204 for-loop
15206 OpenMP 4.0:
15207 #pragma omp for simd for-simd-clause[optseq] new-line
15208 for-loop
15210 LOC is the location of the #pragma token.
15213 #define OMP_FOR_CLAUSE_MASK \
15214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15224 static tree
15225 c_parser_omp_for (location_t loc, c_parser *parser,
15226 char *p_name, omp_clause_mask mask, tree *cclauses,
15227 bool *if_p)
15229 tree block, clauses, ret;
15231 strcat (p_name, " for");
15232 mask |= OMP_FOR_CLAUSE_MASK;
15233 /* parallel for{, simd} disallows nowait clause, but for
15234 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15235 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15236 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15237 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15238 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15239 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15241 if (c_parser_next_token_is (parser, CPP_NAME))
15243 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15245 if (strcmp (p, "simd") == 0)
15247 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15248 if (cclauses == NULL)
15249 cclauses = cclauses_buf;
15251 c_parser_consume_token (parser);
15252 if (!flag_openmp) /* flag_openmp_simd */
15253 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15254 if_p);
15255 block = c_begin_compound_stmt (true);
15256 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15257 block = c_end_compound_stmt (loc, block, true);
15258 if (ret == NULL_TREE)
15259 return ret;
15260 ret = make_node (OMP_FOR);
15261 TREE_TYPE (ret) = void_type_node;
15262 OMP_FOR_BODY (ret) = block;
15263 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15264 SET_EXPR_LOCATION (ret, loc);
15265 add_stmt (ret);
15266 return ret;
15269 if (!flag_openmp) /* flag_openmp_simd */
15271 c_parser_skip_to_pragma_eol (parser, false);
15272 return NULL_TREE;
15275 /* Composite distribute parallel for disallows linear clause. */
15276 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15277 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15279 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15280 if (cclauses)
15282 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15283 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15286 block = c_begin_compound_stmt (true);
15287 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15288 block = c_end_compound_stmt (loc, block, true);
15289 add_stmt (block);
15291 return ret;
15294 /* OpenMP 2.5:
15295 # pragma omp master new-line
15296 structured-block
15298 LOC is the location of the #pragma token.
15301 static tree
15302 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15304 c_parser_skip_to_pragma_eol (parser);
15305 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15306 if_p));
15309 /* OpenMP 2.5:
15310 # pragma omp ordered new-line
15311 structured-block
15313 OpenMP 4.5:
15314 # pragma omp ordered ordered-clauses new-line
15315 structured-block
15317 # pragma omp ordered depend-clauses new-line */
15319 #define OMP_ORDERED_CLAUSE_MASK \
15320 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15323 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15324 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15326 static bool
15327 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15328 bool *if_p)
15330 location_t loc = c_parser_peek_token (parser)->location;
15331 c_parser_consume_pragma (parser);
15333 if (context != pragma_stmt && context != pragma_compound)
15335 c_parser_error (parser, "expected declaration specifiers");
15336 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
15337 return false;
15340 if (c_parser_next_token_is (parser, CPP_NAME))
15342 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15344 if (!strcmp ("depend", p))
15346 if (context == pragma_stmt)
15348 error_at (loc,
15349 "%<#pragma omp ordered%> with %<depend> clause may "
15350 "only be used in compound statements");
15351 c_parser_skip_to_pragma_eol (parser, false);
15352 return false;
15355 tree clauses
15356 = c_parser_omp_all_clauses (parser,
15357 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15358 "#pragma omp ordered");
15359 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15360 return false;
15364 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15365 "#pragma omp ordered");
15366 c_finish_omp_ordered (loc, clauses,
15367 c_parser_omp_structured_block (parser, if_p));
15368 return true;
15371 /* OpenMP 2.5:
15373 section-scope:
15374 { section-sequence }
15376 section-sequence:
15377 section-directive[opt] structured-block
15378 section-sequence section-directive structured-block
15380 SECTIONS_LOC is the location of the #pragma omp sections. */
15382 static tree
15383 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15385 tree stmt, substmt;
15386 bool error_suppress = false;
15387 location_t loc;
15389 loc = c_parser_peek_token (parser)->location;
15390 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15392 /* Avoid skipping until the end of the block. */
15393 parser->error = false;
15394 return NULL_TREE;
15397 stmt = push_stmt_list ();
15399 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15401 substmt = c_parser_omp_structured_block (parser, NULL);
15402 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15403 SET_EXPR_LOCATION (substmt, loc);
15404 add_stmt (substmt);
15407 while (1)
15409 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15410 break;
15411 if (c_parser_next_token_is (parser, CPP_EOF))
15412 break;
15414 loc = c_parser_peek_token (parser)->location;
15415 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15417 c_parser_consume_pragma (parser);
15418 c_parser_skip_to_pragma_eol (parser);
15419 error_suppress = false;
15421 else if (!error_suppress)
15423 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15424 error_suppress = true;
15427 substmt = c_parser_omp_structured_block (parser, NULL);
15428 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15429 SET_EXPR_LOCATION (substmt, loc);
15430 add_stmt (substmt);
15432 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15433 "expected %<#pragma omp section%> or %<}%>");
15435 substmt = pop_stmt_list (stmt);
15437 stmt = make_node (OMP_SECTIONS);
15438 SET_EXPR_LOCATION (stmt, sections_loc);
15439 TREE_TYPE (stmt) = void_type_node;
15440 OMP_SECTIONS_BODY (stmt) = substmt;
15442 return add_stmt (stmt);
15445 /* OpenMP 2.5:
15446 # pragma omp sections sections-clause[optseq] newline
15447 sections-scope
15449 LOC is the location of the #pragma token.
15452 #define OMP_SECTIONS_CLAUSE_MASK \
15453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15459 static tree
15460 c_parser_omp_sections (location_t loc, c_parser *parser,
15461 char *p_name, omp_clause_mask mask, tree *cclauses)
15463 tree block, clauses, ret;
15465 strcat (p_name, " sections");
15466 mask |= OMP_SECTIONS_CLAUSE_MASK;
15467 if (cclauses)
15468 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15470 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15471 if (cclauses)
15473 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15474 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15477 block = c_begin_compound_stmt (true);
15478 ret = c_parser_omp_sections_scope (loc, parser);
15479 if (ret)
15480 OMP_SECTIONS_CLAUSES (ret) = clauses;
15481 block = c_end_compound_stmt (loc, block, true);
15482 add_stmt (block);
15484 return ret;
15487 /* OpenMP 2.5:
15488 # pragma omp parallel parallel-clause[optseq] new-line
15489 structured-block
15490 # pragma omp parallel for parallel-for-clause[optseq] new-line
15491 structured-block
15492 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15493 structured-block
15495 OpenMP 4.0:
15496 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15497 structured-block
15499 LOC is the location of the #pragma token.
15502 #define OMP_PARALLEL_CLAUSE_MASK \
15503 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15513 static tree
15514 c_parser_omp_parallel (location_t loc, c_parser *parser,
15515 char *p_name, omp_clause_mask mask, tree *cclauses,
15516 bool *if_p)
15518 tree stmt, clauses, block;
15520 strcat (p_name, " parallel");
15521 mask |= OMP_PARALLEL_CLAUSE_MASK;
15522 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15523 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15524 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15525 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15527 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15529 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15530 if (cclauses == NULL)
15531 cclauses = cclauses_buf;
15533 c_parser_consume_token (parser);
15534 if (!flag_openmp) /* flag_openmp_simd */
15535 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15536 block = c_begin_omp_parallel ();
15537 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15538 stmt
15539 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15540 block);
15541 if (ret == NULL_TREE)
15542 return ret;
15543 OMP_PARALLEL_COMBINED (stmt) = 1;
15544 return stmt;
15546 /* When combined with distribute, parallel has to be followed by for.
15547 #pragma omp target parallel is allowed though. */
15548 else if (cclauses
15549 && (mask & (OMP_CLAUSE_MASK_1
15550 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15552 error_at (loc, "expected %<for%> after %qs", p_name);
15553 c_parser_skip_to_pragma_eol (parser);
15554 return NULL_TREE;
15556 else if (!flag_openmp) /* flag_openmp_simd */
15558 c_parser_skip_to_pragma_eol (parser, false);
15559 return NULL_TREE;
15561 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15563 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15564 if (strcmp (p, "sections") == 0)
15566 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15567 if (cclauses == NULL)
15568 cclauses = cclauses_buf;
15570 c_parser_consume_token (parser);
15571 block = c_begin_omp_parallel ();
15572 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15573 stmt = c_finish_omp_parallel (loc,
15574 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15575 block);
15576 OMP_PARALLEL_COMBINED (stmt) = 1;
15577 return stmt;
15581 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15582 if (cclauses)
15584 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15585 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15588 block = c_begin_omp_parallel ();
15589 c_parser_statement (parser, if_p);
15590 stmt = c_finish_omp_parallel (loc, clauses, block);
15592 return stmt;
15595 /* OpenMP 2.5:
15596 # pragma omp single single-clause[optseq] new-line
15597 structured-block
15599 LOC is the location of the #pragma.
15602 #define OMP_SINGLE_CLAUSE_MASK \
15603 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15608 static tree
15609 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15611 tree stmt = make_node (OMP_SINGLE);
15612 SET_EXPR_LOCATION (stmt, loc);
15613 TREE_TYPE (stmt) = void_type_node;
15615 OMP_SINGLE_CLAUSES (stmt)
15616 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15617 "#pragma omp single");
15618 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15620 return add_stmt (stmt);
15623 /* OpenMP 3.0:
15624 # pragma omp task task-clause[optseq] new-line
15626 LOC is the location of the #pragma.
15629 #define OMP_TASK_CLAUSE_MASK \
15630 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15641 static tree
15642 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
15644 tree clauses, block;
15646 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15647 "#pragma omp task");
15649 block = c_begin_omp_task ();
15650 c_parser_statement (parser, if_p);
15651 return c_finish_omp_task (loc, clauses, block);
15654 /* OpenMP 3.0:
15655 # pragma omp taskwait new-line
15658 static void
15659 c_parser_omp_taskwait (c_parser *parser)
15661 location_t loc = c_parser_peek_token (parser)->location;
15662 c_parser_consume_pragma (parser);
15663 c_parser_skip_to_pragma_eol (parser);
15665 c_finish_omp_taskwait (loc);
15668 /* OpenMP 3.1:
15669 # pragma omp taskyield new-line
15672 static void
15673 c_parser_omp_taskyield (c_parser *parser)
15675 location_t loc = c_parser_peek_token (parser)->location;
15676 c_parser_consume_pragma (parser);
15677 c_parser_skip_to_pragma_eol (parser);
15679 c_finish_omp_taskyield (loc);
15682 /* OpenMP 4.0:
15683 # pragma omp taskgroup new-line
15686 static tree
15687 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
15689 location_t loc = c_parser_peek_token (parser)->location;
15690 c_parser_skip_to_pragma_eol (parser);
15691 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
15692 if_p));
15695 /* OpenMP 4.0:
15696 # pragma omp cancel cancel-clause[optseq] new-line
15698 LOC is the location of the #pragma.
15701 #define OMP_CANCEL_CLAUSE_MASK \
15702 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15708 static void
15709 c_parser_omp_cancel (c_parser *parser)
15711 location_t loc = c_parser_peek_token (parser)->location;
15713 c_parser_consume_pragma (parser);
15714 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15715 "#pragma omp cancel");
15717 c_finish_omp_cancel (loc, clauses);
15720 /* OpenMP 4.0:
15721 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15723 LOC is the location of the #pragma.
15726 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15727 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15732 static void
15733 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
15735 location_t loc = c_parser_peek_token (parser)->location;
15736 tree clauses;
15737 bool point_seen = false;
15739 c_parser_consume_pragma (parser);
15740 if (c_parser_next_token_is (parser, CPP_NAME))
15742 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15743 if (strcmp (p, "point") == 0)
15745 c_parser_consume_token (parser);
15746 point_seen = true;
15749 if (!point_seen)
15751 c_parser_error (parser, "expected %<point%>");
15752 c_parser_skip_to_pragma_eol (parser);
15753 return;
15756 if (context != pragma_compound)
15758 if (context == pragma_stmt)
15759 error_at (loc, "%<#pragma omp cancellation point%> may only be used in"
15760 " compound statements");
15761 else
15762 c_parser_error (parser, "expected declaration specifiers");
15763 c_parser_skip_to_pragma_eol (parser, false);
15764 return;
15767 clauses
15768 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15769 "#pragma omp cancellation point");
15771 c_finish_omp_cancellation_point (loc, clauses);
15774 /* OpenMP 4.0:
15775 #pragma omp distribute distribute-clause[optseq] new-line
15776 for-loop */
15778 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15779 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15785 static tree
15786 c_parser_omp_distribute (location_t loc, c_parser *parser,
15787 char *p_name, omp_clause_mask mask, tree *cclauses,
15788 bool *if_p)
15790 tree clauses, block, ret;
15792 strcat (p_name, " distribute");
15793 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15795 if (c_parser_next_token_is (parser, CPP_NAME))
15797 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15798 bool simd = false;
15799 bool parallel = false;
15801 if (strcmp (p, "simd") == 0)
15802 simd = true;
15803 else
15804 parallel = strcmp (p, "parallel") == 0;
15805 if (parallel || simd)
15807 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15808 if (cclauses == NULL)
15809 cclauses = cclauses_buf;
15810 c_parser_consume_token (parser);
15811 if (!flag_openmp) /* flag_openmp_simd */
15813 if (simd)
15814 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15815 if_p);
15816 else
15817 return c_parser_omp_parallel (loc, parser, p_name, mask,
15818 cclauses, if_p);
15820 block = c_begin_compound_stmt (true);
15821 if (simd)
15822 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15823 if_p);
15824 else
15825 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
15826 if_p);
15827 block = c_end_compound_stmt (loc, block, true);
15828 if (ret == NULL)
15829 return ret;
15830 ret = make_node (OMP_DISTRIBUTE);
15831 TREE_TYPE (ret) = void_type_node;
15832 OMP_FOR_BODY (ret) = block;
15833 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15834 SET_EXPR_LOCATION (ret, loc);
15835 add_stmt (ret);
15836 return ret;
15839 if (!flag_openmp) /* flag_openmp_simd */
15841 c_parser_skip_to_pragma_eol (parser, false);
15842 return NULL_TREE;
15845 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15846 if (cclauses)
15848 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15849 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15852 block = c_begin_compound_stmt (true);
15853 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
15854 if_p);
15855 block = c_end_compound_stmt (loc, block, true);
15856 add_stmt (block);
15858 return ret;
15861 /* OpenMP 4.0:
15862 # pragma omp teams teams-clause[optseq] new-line
15863 structured-block */
15865 #define OMP_TEAMS_CLAUSE_MASK \
15866 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
15874 static tree
15875 c_parser_omp_teams (location_t loc, c_parser *parser,
15876 char *p_name, omp_clause_mask mask, tree *cclauses,
15877 bool *if_p)
15879 tree clauses, block, ret;
15881 strcat (p_name, " teams");
15882 mask |= OMP_TEAMS_CLAUSE_MASK;
15884 if (c_parser_next_token_is (parser, CPP_NAME))
15886 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15887 if (strcmp (p, "distribute") == 0)
15889 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15890 if (cclauses == NULL)
15891 cclauses = cclauses_buf;
15893 c_parser_consume_token (parser);
15894 if (!flag_openmp) /* flag_openmp_simd */
15895 return c_parser_omp_distribute (loc, parser, p_name, mask,
15896 cclauses, if_p);
15897 block = c_begin_compound_stmt (true);
15898 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
15899 if_p);
15900 block = c_end_compound_stmt (loc, block, true);
15901 if (ret == NULL)
15902 return ret;
15903 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15904 ret = make_node (OMP_TEAMS);
15905 TREE_TYPE (ret) = void_type_node;
15906 OMP_TEAMS_CLAUSES (ret) = clauses;
15907 OMP_TEAMS_BODY (ret) = block;
15908 OMP_TEAMS_COMBINED (ret) = 1;
15909 return add_stmt (ret);
15912 if (!flag_openmp) /* flag_openmp_simd */
15914 c_parser_skip_to_pragma_eol (parser, false);
15915 return NULL_TREE;
15918 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15919 if (cclauses)
15921 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
15922 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15925 tree stmt = make_node (OMP_TEAMS);
15926 TREE_TYPE (stmt) = void_type_node;
15927 OMP_TEAMS_CLAUSES (stmt) = clauses;
15928 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15930 return add_stmt (stmt);
15933 /* OpenMP 4.0:
15934 # pragma omp target data target-data-clause[optseq] new-line
15935 structured-block */
15937 #define OMP_TARGET_DATA_CLAUSE_MASK \
15938 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
15943 static tree
15944 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
15946 tree clauses
15947 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
15948 "#pragma omp target data");
15949 int map_seen = 0;
15950 for (tree *pc = &clauses; *pc;)
15952 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15953 switch (OMP_CLAUSE_MAP_KIND (*pc))
15955 case GOMP_MAP_TO:
15956 case GOMP_MAP_ALWAYS_TO:
15957 case GOMP_MAP_FROM:
15958 case GOMP_MAP_ALWAYS_FROM:
15959 case GOMP_MAP_TOFROM:
15960 case GOMP_MAP_ALWAYS_TOFROM:
15961 case GOMP_MAP_ALLOC:
15962 map_seen = 3;
15963 break;
15964 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15965 case GOMP_MAP_ALWAYS_POINTER:
15966 break;
15967 default:
15968 map_seen |= 1;
15969 error_at (OMP_CLAUSE_LOCATION (*pc),
15970 "%<#pragma omp target data%> with map-type other "
15971 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
15972 "on %<map%> clause");
15973 *pc = OMP_CLAUSE_CHAIN (*pc);
15974 continue;
15976 pc = &OMP_CLAUSE_CHAIN (*pc);
15979 if (map_seen != 3)
15981 if (map_seen == 0)
15982 error_at (loc,
15983 "%<#pragma omp target data%> must contain at least "
15984 "one %<map%> clause");
15985 return NULL_TREE;
15988 tree stmt = make_node (OMP_TARGET_DATA);
15989 TREE_TYPE (stmt) = void_type_node;
15990 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
15991 keep_next_level ();
15992 tree block = c_begin_compound_stmt (true);
15993 add_stmt (c_parser_omp_structured_block (parser, if_p));
15994 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
15996 SET_EXPR_LOCATION (stmt, loc);
15997 return add_stmt (stmt);
16000 /* OpenMP 4.0:
16001 # pragma omp target update target-update-clause[optseq] new-line */
16003 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16011 static bool
16012 c_parser_omp_target_update (location_t loc, c_parser *parser,
16013 enum pragma_context context)
16015 if (context == pragma_stmt)
16017 error_at (loc,
16018 "%<#pragma omp target update%> may only be "
16019 "used in compound statements");
16020 c_parser_skip_to_pragma_eol (parser, false);
16021 return false;
16024 tree clauses
16025 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16026 "#pragma omp target update");
16027 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16028 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16030 error_at (loc,
16031 "%<#pragma omp target update%> must contain at least one "
16032 "%<from%> or %<to%> clauses");
16033 return false;
16036 tree stmt = make_node (OMP_TARGET_UPDATE);
16037 TREE_TYPE (stmt) = void_type_node;
16038 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16039 SET_EXPR_LOCATION (stmt, loc);
16040 add_stmt (stmt);
16041 return false;
16044 /* OpenMP 4.5:
16045 # pragma omp target enter data target-data-clause[optseq] new-line */
16047 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16054 static tree
16055 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16056 enum pragma_context context)
16058 bool data_seen = false;
16059 if (c_parser_next_token_is (parser, CPP_NAME))
16061 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16062 if (strcmp (p, "data") == 0)
16064 c_parser_consume_token (parser);
16065 data_seen = true;
16068 if (!data_seen)
16070 c_parser_error (parser, "expected %<data%>");
16071 c_parser_skip_to_pragma_eol (parser);
16072 return NULL_TREE;
16075 if (context == pragma_stmt)
16077 error_at (loc,
16078 "%<#pragma omp target enter data%> may only be "
16079 "used in compound statements");
16080 c_parser_skip_to_pragma_eol (parser, false);
16081 return NULL_TREE;
16084 tree clauses
16085 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16086 "#pragma omp target enter data");
16087 int map_seen = 0;
16088 for (tree *pc = &clauses; *pc;)
16090 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16091 switch (OMP_CLAUSE_MAP_KIND (*pc))
16093 case GOMP_MAP_TO:
16094 case GOMP_MAP_ALWAYS_TO:
16095 case GOMP_MAP_ALLOC:
16096 map_seen = 3;
16097 break;
16098 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16099 case GOMP_MAP_ALWAYS_POINTER:
16100 break;
16101 default:
16102 map_seen |= 1;
16103 error_at (OMP_CLAUSE_LOCATION (*pc),
16104 "%<#pragma omp target enter data%> with map-type other "
16105 "than %<to%> or %<alloc%> on %<map%> clause");
16106 *pc = OMP_CLAUSE_CHAIN (*pc);
16107 continue;
16109 pc = &OMP_CLAUSE_CHAIN (*pc);
16112 if (map_seen != 3)
16114 if (map_seen == 0)
16115 error_at (loc,
16116 "%<#pragma omp target enter data%> must contain at least "
16117 "one %<map%> clause");
16118 return NULL_TREE;
16121 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16122 TREE_TYPE (stmt) = void_type_node;
16123 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16124 SET_EXPR_LOCATION (stmt, loc);
16125 add_stmt (stmt);
16126 return stmt;
16129 /* OpenMP 4.5:
16130 # pragma omp target exit data target-data-clause[optseq] new-line */
16132 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16139 static tree
16140 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16141 enum pragma_context context)
16143 bool data_seen = false;
16144 if (c_parser_next_token_is (parser, CPP_NAME))
16146 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16147 if (strcmp (p, "data") == 0)
16149 c_parser_consume_token (parser);
16150 data_seen = true;
16153 if (!data_seen)
16155 c_parser_error (parser, "expected %<data%>");
16156 c_parser_skip_to_pragma_eol (parser);
16157 return NULL_TREE;
16160 if (context == pragma_stmt)
16162 error_at (loc,
16163 "%<#pragma omp target exit data%> may only be "
16164 "used in compound statements");
16165 c_parser_skip_to_pragma_eol (parser, false);
16166 return NULL_TREE;
16169 tree clauses
16170 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16171 "#pragma omp target exit data");
16173 int map_seen = 0;
16174 for (tree *pc = &clauses; *pc;)
16176 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16177 switch (OMP_CLAUSE_MAP_KIND (*pc))
16179 case GOMP_MAP_FROM:
16180 case GOMP_MAP_ALWAYS_FROM:
16181 case GOMP_MAP_RELEASE:
16182 case GOMP_MAP_DELETE:
16183 map_seen = 3;
16184 break;
16185 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16186 case GOMP_MAP_ALWAYS_POINTER:
16187 break;
16188 default:
16189 map_seen |= 1;
16190 error_at (OMP_CLAUSE_LOCATION (*pc),
16191 "%<#pragma omp target exit data%> with map-type other "
16192 "than %<from%>, %<release> or %<delete%> on %<map%>"
16193 " clause");
16194 *pc = OMP_CLAUSE_CHAIN (*pc);
16195 continue;
16197 pc = &OMP_CLAUSE_CHAIN (*pc);
16200 if (map_seen != 3)
16202 if (map_seen == 0)
16203 error_at (loc,
16204 "%<#pragma omp target exit data%> must contain at least one "
16205 "%<map%> clause");
16206 return NULL_TREE;
16209 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16210 TREE_TYPE (stmt) = void_type_node;
16211 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16212 SET_EXPR_LOCATION (stmt, loc);
16213 add_stmt (stmt);
16214 return stmt;
16217 /* OpenMP 4.0:
16218 # pragma omp target target-clause[optseq] new-line
16219 structured-block */
16221 #define OMP_TARGET_CLAUSE_MASK \
16222 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16232 static bool
16233 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16235 location_t loc = c_parser_peek_token (parser)->location;
16236 c_parser_consume_pragma (parser);
16237 tree *pc = NULL, stmt, block;
16239 if (context != pragma_stmt && context != pragma_compound)
16241 c_parser_error (parser, "expected declaration specifiers");
16242 c_parser_skip_to_pragma_eol (parser);
16243 return false;
16246 if (c_parser_next_token_is (parser, CPP_NAME))
16248 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16249 enum tree_code ccode = ERROR_MARK;
16251 if (strcmp (p, "teams") == 0)
16252 ccode = OMP_TEAMS;
16253 else if (strcmp (p, "parallel") == 0)
16254 ccode = OMP_PARALLEL;
16255 else if (strcmp (p, "simd") == 0)
16256 ccode = OMP_SIMD;
16257 if (ccode != ERROR_MARK)
16259 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16260 char p_name[sizeof ("#pragma omp target teams distribute "
16261 "parallel for simd")];
16263 c_parser_consume_token (parser);
16264 strcpy (p_name, "#pragma omp target");
16265 if (!flag_openmp) /* flag_openmp_simd */
16267 tree stmt;
16268 switch (ccode)
16270 case OMP_TEAMS:
16271 stmt = c_parser_omp_teams (loc, parser, p_name,
16272 OMP_TARGET_CLAUSE_MASK,
16273 cclauses, if_p);
16274 break;
16275 case OMP_PARALLEL:
16276 stmt = c_parser_omp_parallel (loc, parser, p_name,
16277 OMP_TARGET_CLAUSE_MASK,
16278 cclauses, if_p);
16279 break;
16280 case OMP_SIMD:
16281 stmt = c_parser_omp_simd (loc, parser, p_name,
16282 OMP_TARGET_CLAUSE_MASK,
16283 cclauses, if_p);
16284 break;
16285 default:
16286 gcc_unreachable ();
16288 return stmt != NULL_TREE;
16290 keep_next_level ();
16291 tree block = c_begin_compound_stmt (true), ret;
16292 switch (ccode)
16294 case OMP_TEAMS:
16295 ret = c_parser_omp_teams (loc, parser, p_name,
16296 OMP_TARGET_CLAUSE_MASK, cclauses,
16297 if_p);
16298 break;
16299 case OMP_PARALLEL:
16300 ret = c_parser_omp_parallel (loc, parser, p_name,
16301 OMP_TARGET_CLAUSE_MASK, cclauses,
16302 if_p);
16303 break;
16304 case OMP_SIMD:
16305 ret = c_parser_omp_simd (loc, parser, p_name,
16306 OMP_TARGET_CLAUSE_MASK, cclauses,
16307 if_p);
16308 break;
16309 default:
16310 gcc_unreachable ();
16312 block = c_end_compound_stmt (loc, block, true);
16313 if (ret == NULL_TREE)
16314 return false;
16315 if (ccode == OMP_TEAMS)
16317 /* For combined target teams, ensure the num_teams and
16318 thread_limit clause expressions are evaluated on the host,
16319 before entering the target construct. */
16320 tree c;
16321 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16322 c; c = OMP_CLAUSE_CHAIN (c))
16323 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16324 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16325 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16327 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16328 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16329 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16330 expr, NULL_TREE, NULL_TREE);
16331 add_stmt (expr);
16332 OMP_CLAUSE_OPERAND (c, 0) = expr;
16333 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16334 OMP_CLAUSE_FIRSTPRIVATE);
16335 OMP_CLAUSE_DECL (tc) = tmp;
16336 OMP_CLAUSE_CHAIN (tc)
16337 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16338 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16341 tree stmt = make_node (OMP_TARGET);
16342 TREE_TYPE (stmt) = void_type_node;
16343 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16344 OMP_TARGET_BODY (stmt) = block;
16345 OMP_TARGET_COMBINED (stmt) = 1;
16346 add_stmt (stmt);
16347 pc = &OMP_TARGET_CLAUSES (stmt);
16348 goto check_clauses;
16350 else if (!flag_openmp) /* flag_openmp_simd */
16352 c_parser_skip_to_pragma_eol (parser, false);
16353 return false;
16355 else if (strcmp (p, "data") == 0)
16357 c_parser_consume_token (parser);
16358 c_parser_omp_target_data (loc, parser, if_p);
16359 return true;
16361 else if (strcmp (p, "enter") == 0)
16363 c_parser_consume_token (parser);
16364 c_parser_omp_target_enter_data (loc, parser, context);
16365 return false;
16367 else if (strcmp (p, "exit") == 0)
16369 c_parser_consume_token (parser);
16370 c_parser_omp_target_exit_data (loc, parser, context);
16371 return false;
16373 else if (strcmp (p, "update") == 0)
16375 c_parser_consume_token (parser);
16376 return c_parser_omp_target_update (loc, parser, context);
16380 stmt = make_node (OMP_TARGET);
16381 TREE_TYPE (stmt) = void_type_node;
16383 OMP_TARGET_CLAUSES (stmt)
16384 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16385 "#pragma omp target");
16386 pc = &OMP_TARGET_CLAUSES (stmt);
16387 keep_next_level ();
16388 block = c_begin_compound_stmt (true);
16389 add_stmt (c_parser_omp_structured_block (parser, if_p));
16390 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16392 SET_EXPR_LOCATION (stmt, loc);
16393 add_stmt (stmt);
16395 check_clauses:
16396 while (*pc)
16398 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16399 switch (OMP_CLAUSE_MAP_KIND (*pc))
16401 case GOMP_MAP_TO:
16402 case GOMP_MAP_ALWAYS_TO:
16403 case GOMP_MAP_FROM:
16404 case GOMP_MAP_ALWAYS_FROM:
16405 case GOMP_MAP_TOFROM:
16406 case GOMP_MAP_ALWAYS_TOFROM:
16407 case GOMP_MAP_ALLOC:
16408 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16409 case GOMP_MAP_ALWAYS_POINTER:
16410 break;
16411 default:
16412 error_at (OMP_CLAUSE_LOCATION (*pc),
16413 "%<#pragma omp target%> with map-type other "
16414 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16415 "on %<map%> clause");
16416 *pc = OMP_CLAUSE_CHAIN (*pc);
16417 continue;
16419 pc = &OMP_CLAUSE_CHAIN (*pc);
16421 return true;
16424 /* OpenMP 4.0:
16425 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16427 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16435 static void
16436 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16438 auto_vec<c_token> clauses;
16439 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16441 c_token *token = c_parser_peek_token (parser);
16442 if (token->type == CPP_EOF)
16444 c_parser_skip_to_pragma_eol (parser);
16445 return;
16447 clauses.safe_push (*token);
16448 c_parser_consume_token (parser);
16450 clauses.safe_push (*c_parser_peek_token (parser));
16451 c_parser_skip_to_pragma_eol (parser);
16453 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16455 if (c_parser_peek_token (parser)->pragma_kind
16456 != PRAGMA_OMP_DECLARE
16457 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16458 || strcmp (IDENTIFIER_POINTER
16459 (c_parser_peek_2nd_token (parser)->value),
16460 "simd") != 0)
16462 c_parser_error (parser,
16463 "%<#pragma omp declare simd%> must be followed by "
16464 "function declaration or definition or another "
16465 "%<#pragma omp declare simd%>");
16466 return;
16468 c_parser_consume_pragma (parser);
16469 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16471 c_token *token = c_parser_peek_token (parser);
16472 if (token->type == CPP_EOF)
16474 c_parser_skip_to_pragma_eol (parser);
16475 return;
16477 clauses.safe_push (*token);
16478 c_parser_consume_token (parser);
16480 clauses.safe_push (*c_parser_peek_token (parser));
16481 c_parser_skip_to_pragma_eol (parser);
16484 /* Make sure nothing tries to read past the end of the tokens. */
16485 c_token eof_token;
16486 memset (&eof_token, 0, sizeof (eof_token));
16487 eof_token.type = CPP_EOF;
16488 clauses.safe_push (eof_token);
16489 clauses.safe_push (eof_token);
16491 switch (context)
16493 case pragma_external:
16494 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16495 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16497 int ext = disable_extension_diagnostics ();
16499 c_parser_consume_token (parser);
16500 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16501 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16502 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16503 NULL, clauses);
16504 restore_extension_diagnostics (ext);
16506 else
16507 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16508 NULL, clauses);
16509 break;
16510 case pragma_struct:
16511 case pragma_param:
16512 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16513 "function declaration or definition");
16514 break;
16515 case pragma_compound:
16516 case pragma_stmt:
16517 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16518 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16520 int ext = disable_extension_diagnostics ();
16522 c_parser_consume_token (parser);
16523 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16524 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16525 if (c_parser_next_tokens_start_declaration (parser))
16527 c_parser_declaration_or_fndef (parser, true, true, true, true,
16528 true, NULL, clauses);
16529 restore_extension_diagnostics (ext);
16530 break;
16532 restore_extension_diagnostics (ext);
16534 else if (c_parser_next_tokens_start_declaration (parser))
16536 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16537 NULL, clauses);
16538 break;
16540 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16541 "function declaration or definition");
16542 break;
16543 default:
16544 gcc_unreachable ();
16548 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16549 and put that into "omp declare simd" attribute. */
16551 static void
16552 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16553 vec<c_token> clauses)
16555 if (flag_cilkplus
16556 && (clauses.exists ()
16557 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16558 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16560 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16561 "used in the same function marked as a Cilk Plus SIMD-enabled "
16562 "function");
16563 vec_free (parser->cilk_simd_fn_tokens);
16564 return;
16567 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16568 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16569 has already processed the tokens. */
16570 if (clauses.exists () && clauses[0].type == CPP_EOF)
16571 return;
16572 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16574 error ("%<#pragma omp declare simd%> not immediately followed by "
16575 "a function declaration or definition");
16576 clauses[0].type = CPP_EOF;
16577 return;
16579 if (clauses.exists () && clauses[0].type != CPP_NAME)
16581 error_at (DECL_SOURCE_LOCATION (fndecl),
16582 "%<#pragma omp declare simd%> not immediately followed by "
16583 "a single function declaration or definition");
16584 clauses[0].type = CPP_EOF;
16585 return;
16588 if (parms == NULL_TREE)
16589 parms = DECL_ARGUMENTS (fndecl);
16591 unsigned int tokens_avail = parser->tokens_avail;
16592 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16593 bool is_cilkplus_cilk_simd_fn = false;
16595 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16597 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16598 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16599 is_cilkplus_cilk_simd_fn = true;
16601 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16603 error_at (DECL_SOURCE_LOCATION (fndecl),
16604 "%<__simd__%> attribute cannot be used in the same "
16605 "function marked as a Cilk Plus SIMD-enabled function");
16606 vec_free (parser->cilk_simd_fn_tokens);
16607 return;
16611 else
16613 parser->tokens = clauses.address ();
16614 parser->tokens_avail = clauses.length ();
16617 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16618 while (parser->tokens_avail > 3)
16620 c_token *token = c_parser_peek_token (parser);
16621 if (!is_cilkplus_cilk_simd_fn)
16622 gcc_assert (token->type == CPP_NAME
16623 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16624 else
16625 gcc_assert (token->type == CPP_NAME
16626 && is_cilkplus_vector_p (token->value));
16627 c_parser_consume_token (parser);
16628 parser->in_pragma = true;
16630 tree c = NULL_TREE;
16631 if (is_cilkplus_cilk_simd_fn)
16632 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16633 "SIMD-enabled functions attribute");
16634 else
16635 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16636 "#pragma omp declare simd");
16637 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16638 if (c != NULL_TREE)
16639 c = tree_cons (NULL_TREE, c, NULL_TREE);
16640 if (is_cilkplus_cilk_simd_fn)
16642 tree k = build_tree_list (get_identifier ("cilk simd function"),
16643 NULL_TREE);
16644 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16645 DECL_ATTRIBUTES (fndecl) = k;
16647 c = build_tree_list (get_identifier ("omp declare simd"), c);
16648 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16649 DECL_ATTRIBUTES (fndecl) = c;
16652 parser->tokens = &parser->tokens_buf[0];
16653 parser->tokens_avail = tokens_avail;
16654 if (clauses.exists ())
16655 clauses[0].type = CPP_PRAGMA;
16657 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16658 vec_free (parser->cilk_simd_fn_tokens);
16662 /* OpenMP 4.0:
16663 # pragma omp declare target new-line
16664 declarations and definitions
16665 # pragma omp end declare target new-line
16667 OpenMP 4.5:
16668 # pragma omp declare target ( extended-list ) new-line
16670 # pragma omp declare target declare-target-clauses[seq] new-line */
16672 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16676 static void
16677 c_parser_omp_declare_target (c_parser *parser)
16679 location_t loc = c_parser_peek_token (parser)->location;
16680 tree clauses = NULL_TREE;
16681 if (c_parser_next_token_is (parser, CPP_NAME))
16682 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16683 "#pragma omp declare target");
16684 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16686 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16687 clauses);
16688 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
16689 c_parser_skip_to_pragma_eol (parser);
16691 else
16693 c_parser_skip_to_pragma_eol (parser);
16694 current_omp_declare_target_attribute++;
16695 return;
16697 if (current_omp_declare_target_attribute)
16698 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16699 "%<#pragma omp declare target%> without clauses and "
16700 "%<#pragma omp end declare target%>");
16701 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16703 tree t = OMP_CLAUSE_DECL (c), id;
16704 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16705 tree at2 = lookup_attribute ("omp declare target link",
16706 DECL_ATTRIBUTES (t));
16707 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16709 id = get_identifier ("omp declare target link");
16710 std::swap (at1, at2);
16712 else
16713 id = get_identifier ("omp declare target");
16714 if (at2)
16716 error_at (OMP_CLAUSE_LOCATION (c),
16717 "%qD specified both in declare target %<link%> and %<to%>"
16718 " clauses", t);
16719 continue;
16721 if (!at1)
16723 symtab_node *node = symtab_node::get (t);
16724 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16725 if (node != NULL)
16727 node->offloadable = 1;
16728 if (ENABLE_OFFLOADING)
16730 g->have_offload = true;
16731 if (is_a <varpool_node *> (node))
16732 vec_safe_push (offload_vars, t);
16739 static void
16740 c_parser_omp_end_declare_target (c_parser *parser)
16742 location_t loc = c_parser_peek_token (parser)->location;
16743 c_parser_consume_pragma (parser);
16744 if (c_parser_next_token_is (parser, CPP_NAME)
16745 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16746 "declare") == 0)
16748 c_parser_consume_token (parser);
16749 if (c_parser_next_token_is (parser, CPP_NAME)
16750 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16751 "target") == 0)
16752 c_parser_consume_token (parser);
16753 else
16755 c_parser_error (parser, "expected %<target%>");
16756 c_parser_skip_to_pragma_eol (parser);
16757 return;
16760 else
16762 c_parser_error (parser, "expected %<declare%>");
16763 c_parser_skip_to_pragma_eol (parser);
16764 return;
16766 c_parser_skip_to_pragma_eol (parser);
16767 if (!current_omp_declare_target_attribute)
16768 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16769 "%<#pragma omp declare target%>");
16770 else
16771 current_omp_declare_target_attribute--;
16775 /* OpenMP 4.0
16776 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16777 initializer-clause[opt] new-line
16779 initializer-clause:
16780 initializer (omp_priv = initializer)
16781 initializer (function-name (argument-list)) */
16783 static void
16784 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16786 unsigned int tokens_avail = 0, i;
16787 vec<tree> types = vNULL;
16788 vec<c_token> clauses = vNULL;
16789 enum tree_code reduc_code = ERROR_MARK;
16790 tree reduc_id = NULL_TREE;
16791 tree type;
16792 location_t rloc = c_parser_peek_token (parser)->location;
16794 if (context == pragma_struct || context == pragma_param)
16796 error ("%<#pragma omp declare reduction%> not at file or block scope");
16797 goto fail;
16800 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16801 goto fail;
16803 switch (c_parser_peek_token (parser)->type)
16805 case CPP_PLUS:
16806 reduc_code = PLUS_EXPR;
16807 break;
16808 case CPP_MULT:
16809 reduc_code = MULT_EXPR;
16810 break;
16811 case CPP_MINUS:
16812 reduc_code = MINUS_EXPR;
16813 break;
16814 case CPP_AND:
16815 reduc_code = BIT_AND_EXPR;
16816 break;
16817 case CPP_XOR:
16818 reduc_code = BIT_XOR_EXPR;
16819 break;
16820 case CPP_OR:
16821 reduc_code = BIT_IOR_EXPR;
16822 break;
16823 case CPP_AND_AND:
16824 reduc_code = TRUTH_ANDIF_EXPR;
16825 break;
16826 case CPP_OR_OR:
16827 reduc_code = TRUTH_ORIF_EXPR;
16828 break;
16829 case CPP_NAME:
16830 const char *p;
16831 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16832 if (strcmp (p, "min") == 0)
16834 reduc_code = MIN_EXPR;
16835 break;
16837 if (strcmp (p, "max") == 0)
16839 reduc_code = MAX_EXPR;
16840 break;
16842 reduc_id = c_parser_peek_token (parser)->value;
16843 break;
16844 default:
16845 c_parser_error (parser,
16846 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16847 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16848 goto fail;
16851 tree orig_reduc_id, reduc_decl;
16852 orig_reduc_id = reduc_id;
16853 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16854 reduc_decl = c_omp_reduction_decl (reduc_id);
16855 c_parser_consume_token (parser);
16857 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16858 goto fail;
16860 while (true)
16862 location_t loc = c_parser_peek_token (parser)->location;
16863 struct c_type_name *ctype = c_parser_type_name (parser);
16864 if (ctype != NULL)
16866 type = groktypename (ctype, NULL, NULL);
16867 if (type == error_mark_node)
16869 else if ((INTEGRAL_TYPE_P (type)
16870 || TREE_CODE (type) == REAL_TYPE
16871 || TREE_CODE (type) == COMPLEX_TYPE)
16872 && orig_reduc_id == NULL_TREE)
16873 error_at (loc, "predeclared arithmetic type in "
16874 "%<#pragma omp declare reduction%>");
16875 else if (TREE_CODE (type) == FUNCTION_TYPE
16876 || TREE_CODE (type) == ARRAY_TYPE)
16877 error_at (loc, "function or array type in "
16878 "%<#pragma omp declare reduction%>");
16879 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
16880 error_at (loc, "const, volatile or restrict qualified type in "
16881 "%<#pragma omp declare reduction%>");
16882 else
16884 tree t;
16885 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
16886 if (comptypes (TREE_PURPOSE (t), type))
16888 error_at (loc, "redeclaration of %qs "
16889 "%<#pragma omp declare reduction%> for "
16890 "type %qT",
16891 IDENTIFIER_POINTER (reduc_id)
16892 + sizeof ("omp declare reduction ") - 1,
16893 type);
16894 location_t ploc
16895 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
16896 0));
16897 error_at (ploc, "previous %<#pragma omp declare "
16898 "reduction%>");
16899 break;
16901 if (t == NULL_TREE)
16902 types.safe_push (type);
16904 if (c_parser_next_token_is (parser, CPP_COMMA))
16905 c_parser_consume_token (parser);
16906 else
16907 break;
16909 else
16910 break;
16913 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
16914 || types.is_empty ())
16916 fail:
16917 clauses.release ();
16918 types.release ();
16919 while (true)
16921 c_token *token = c_parser_peek_token (parser);
16922 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
16923 break;
16924 c_parser_consume_token (parser);
16926 c_parser_skip_to_pragma_eol (parser);
16927 return;
16930 if (types.length () > 1)
16932 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16934 c_token *token = c_parser_peek_token (parser);
16935 if (token->type == CPP_EOF)
16936 goto fail;
16937 clauses.safe_push (*token);
16938 c_parser_consume_token (parser);
16940 clauses.safe_push (*c_parser_peek_token (parser));
16941 c_parser_skip_to_pragma_eol (parser);
16943 /* Make sure nothing tries to read past the end of the tokens. */
16944 c_token eof_token;
16945 memset (&eof_token, 0, sizeof (eof_token));
16946 eof_token.type = CPP_EOF;
16947 clauses.safe_push (eof_token);
16948 clauses.safe_push (eof_token);
16951 int errs = errorcount;
16952 FOR_EACH_VEC_ELT (types, i, type)
16954 tokens_avail = parser->tokens_avail;
16955 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16956 if (!clauses.is_empty ())
16958 parser->tokens = clauses.address ();
16959 parser->tokens_avail = clauses.length ();
16960 parser->in_pragma = true;
16963 bool nested = current_function_decl != NULL_TREE;
16964 if (nested)
16965 c_push_function_context ();
16966 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
16967 reduc_id, default_function_type);
16968 current_function_decl = fndecl;
16969 allocate_struct_function (fndecl, true);
16970 push_scope ();
16971 tree stmt = push_stmt_list ();
16972 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
16973 warn about these. */
16974 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
16975 get_identifier ("omp_out"), type);
16976 DECL_ARTIFICIAL (omp_out) = 1;
16977 DECL_CONTEXT (omp_out) = fndecl;
16978 pushdecl (omp_out);
16979 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
16980 get_identifier ("omp_in"), type);
16981 DECL_ARTIFICIAL (omp_in) = 1;
16982 DECL_CONTEXT (omp_in) = fndecl;
16983 pushdecl (omp_in);
16984 struct c_expr combiner = c_parser_expression (parser);
16985 struct c_expr initializer;
16986 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
16987 bool bad = false;
16988 initializer.value = error_mark_node;
16989 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16990 bad = true;
16991 else if (c_parser_next_token_is (parser, CPP_NAME)
16992 && strcmp (IDENTIFIER_POINTER
16993 (c_parser_peek_token (parser)->value),
16994 "initializer") == 0)
16996 c_parser_consume_token (parser);
16997 pop_scope ();
16998 push_scope ();
16999 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17000 get_identifier ("omp_priv"), type);
17001 DECL_ARTIFICIAL (omp_priv) = 1;
17002 DECL_INITIAL (omp_priv) = error_mark_node;
17003 DECL_CONTEXT (omp_priv) = fndecl;
17004 pushdecl (omp_priv);
17005 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17006 get_identifier ("omp_orig"), type);
17007 DECL_ARTIFICIAL (omp_orig) = 1;
17008 DECL_CONTEXT (omp_orig) = fndecl;
17009 pushdecl (omp_orig);
17010 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17011 bad = true;
17012 else if (!c_parser_next_token_is (parser, CPP_NAME))
17014 c_parser_error (parser, "expected %<omp_priv%> or "
17015 "function-name");
17016 bad = true;
17018 else if (strcmp (IDENTIFIER_POINTER
17019 (c_parser_peek_token (parser)->value),
17020 "omp_priv") != 0)
17022 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17023 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17025 c_parser_error (parser, "expected function-name %<(%>");
17026 bad = true;
17028 else
17029 initializer = c_parser_postfix_expression (parser);
17030 if (initializer.value
17031 && TREE_CODE (initializer.value) == CALL_EXPR)
17033 int j;
17034 tree c = initializer.value;
17035 for (j = 0; j < call_expr_nargs (c); j++)
17037 tree a = CALL_EXPR_ARG (c, j);
17038 STRIP_NOPS (a);
17039 if (TREE_CODE (a) == ADDR_EXPR
17040 && TREE_OPERAND (a, 0) == omp_priv)
17041 break;
17043 if (j == call_expr_nargs (c))
17044 error ("one of the initializer call arguments should be "
17045 "%<&omp_priv%>");
17048 else
17050 c_parser_consume_token (parser);
17051 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17052 bad = true;
17053 else
17055 tree st = push_stmt_list ();
17056 start_init (omp_priv, NULL_TREE, 0);
17057 location_t loc = c_parser_peek_token (parser)->location;
17058 struct c_expr init = c_parser_initializer (parser);
17059 finish_init ();
17060 finish_decl (omp_priv, loc, init.value,
17061 init.original_type, NULL_TREE);
17062 pop_stmt_list (st);
17065 if (!bad
17066 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17067 bad = true;
17070 if (!bad)
17072 c_parser_skip_to_pragma_eol (parser);
17074 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17075 DECL_INITIAL (reduc_decl));
17076 DECL_INITIAL (reduc_decl) = t;
17077 DECL_SOURCE_LOCATION (omp_out) = rloc;
17078 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17079 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17080 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17081 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17082 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17083 if (omp_priv)
17085 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17086 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17087 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17088 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17089 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17090 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17091 walk_tree (&DECL_INITIAL (omp_priv),
17092 c_check_omp_declare_reduction_r,
17093 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17097 pop_stmt_list (stmt);
17098 pop_scope ();
17099 if (cfun->language != NULL)
17101 ggc_free (cfun->language);
17102 cfun->language = NULL;
17104 set_cfun (NULL);
17105 current_function_decl = NULL_TREE;
17106 if (nested)
17107 c_pop_function_context ();
17109 if (!clauses.is_empty ())
17111 parser->tokens = &parser->tokens_buf[0];
17112 parser->tokens_avail = tokens_avail;
17114 if (bad)
17115 goto fail;
17116 if (errs != errorcount)
17117 break;
17120 clauses.release ();
17121 types.release ();
17125 /* OpenMP 4.0
17126 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17127 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17128 initializer-clause[opt] new-line
17129 #pragma omp declare target new-line */
17131 static void
17132 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17134 c_parser_consume_pragma (parser);
17135 if (c_parser_next_token_is (parser, CPP_NAME))
17137 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17138 if (strcmp (p, "simd") == 0)
17140 /* c_parser_consume_token (parser); done in
17141 c_parser_omp_declare_simd. */
17142 c_parser_omp_declare_simd (parser, context);
17143 return;
17145 if (strcmp (p, "reduction") == 0)
17147 c_parser_consume_token (parser);
17148 c_parser_omp_declare_reduction (parser, context);
17149 return;
17151 if (!flag_openmp) /* flag_openmp_simd */
17153 c_parser_skip_to_pragma_eol (parser, false);
17154 return;
17156 if (strcmp (p, "target") == 0)
17158 c_parser_consume_token (parser);
17159 c_parser_omp_declare_target (parser);
17160 return;
17164 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17165 "or %<target%>");
17166 c_parser_skip_to_pragma_eol (parser);
17169 /* OpenMP 4.5:
17170 #pragma omp taskloop taskloop-clause[optseq] new-line
17171 for-loop
17173 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17174 for-loop */
17176 #define OMP_TASKLOOP_CLAUSE_MASK \
17177 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17192 static tree
17193 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17194 char *p_name, omp_clause_mask mask, tree *cclauses,
17195 bool *if_p)
17197 tree clauses, block, ret;
17199 strcat (p_name, " taskloop");
17200 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17202 if (c_parser_next_token_is (parser, CPP_NAME))
17204 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17206 if (strcmp (p, "simd") == 0)
17208 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17209 if (cclauses == NULL)
17210 cclauses = cclauses_buf;
17211 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17212 c_parser_consume_token (parser);
17213 if (!flag_openmp) /* flag_openmp_simd */
17214 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17215 if_p);
17216 block = c_begin_compound_stmt (true);
17217 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17218 block = c_end_compound_stmt (loc, block, true);
17219 if (ret == NULL)
17220 return ret;
17221 ret = make_node (OMP_TASKLOOP);
17222 TREE_TYPE (ret) = void_type_node;
17223 OMP_FOR_BODY (ret) = block;
17224 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17225 SET_EXPR_LOCATION (ret, loc);
17226 add_stmt (ret);
17227 return ret;
17230 if (!flag_openmp) /* flag_openmp_simd */
17232 c_parser_skip_to_pragma_eol (parser, false);
17233 return NULL_TREE;
17236 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17237 if (cclauses)
17239 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17240 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17243 block = c_begin_compound_stmt (true);
17244 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17245 block = c_end_compound_stmt (loc, block, true);
17246 add_stmt (block);
17248 return ret;
17251 /* Main entry point to parsing most OpenMP pragmas. */
17253 static void
17254 c_parser_omp_construct (c_parser *parser, bool *if_p)
17256 enum pragma_kind p_kind;
17257 location_t loc;
17258 tree stmt;
17259 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17260 omp_clause_mask mask (0);
17262 loc = c_parser_peek_token (parser)->location;
17263 p_kind = c_parser_peek_token (parser)->pragma_kind;
17264 c_parser_consume_pragma (parser);
17266 switch (p_kind)
17268 case PRAGMA_OACC_ATOMIC:
17269 c_parser_omp_atomic (loc, parser);
17270 return;
17271 case PRAGMA_OACC_CACHE:
17272 strcpy (p_name, "#pragma acc");
17273 stmt = c_parser_oacc_cache (loc, parser);
17274 break;
17275 case PRAGMA_OACC_DATA:
17276 stmt = c_parser_oacc_data (loc, parser, if_p);
17277 break;
17278 case PRAGMA_OACC_HOST_DATA:
17279 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17280 break;
17281 case PRAGMA_OACC_KERNELS:
17282 case PRAGMA_OACC_PARALLEL:
17283 strcpy (p_name, "#pragma acc");
17284 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17285 if_p);
17286 break;
17287 case PRAGMA_OACC_LOOP:
17288 strcpy (p_name, "#pragma acc");
17289 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17290 break;
17291 case PRAGMA_OACC_WAIT:
17292 strcpy (p_name, "#pragma wait");
17293 stmt = c_parser_oacc_wait (loc, parser, p_name);
17294 break;
17295 case PRAGMA_OMP_ATOMIC:
17296 c_parser_omp_atomic (loc, parser);
17297 return;
17298 case PRAGMA_OMP_CRITICAL:
17299 stmt = c_parser_omp_critical (loc, parser, if_p);
17300 break;
17301 case PRAGMA_OMP_DISTRIBUTE:
17302 strcpy (p_name, "#pragma omp");
17303 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17304 break;
17305 case PRAGMA_OMP_FOR:
17306 strcpy (p_name, "#pragma omp");
17307 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17308 break;
17309 case PRAGMA_OMP_MASTER:
17310 stmt = c_parser_omp_master (loc, parser, if_p);
17311 break;
17312 case PRAGMA_OMP_PARALLEL:
17313 strcpy (p_name, "#pragma omp");
17314 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17315 break;
17316 case PRAGMA_OMP_SECTIONS:
17317 strcpy (p_name, "#pragma omp");
17318 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17319 break;
17320 case PRAGMA_OMP_SIMD:
17321 strcpy (p_name, "#pragma omp");
17322 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17323 break;
17324 case PRAGMA_OMP_SINGLE:
17325 stmt = c_parser_omp_single (loc, parser, if_p);
17326 break;
17327 case PRAGMA_OMP_TASK:
17328 stmt = c_parser_omp_task (loc, parser, if_p);
17329 break;
17330 case PRAGMA_OMP_TASKGROUP:
17331 stmt = c_parser_omp_taskgroup (parser, if_p);
17332 break;
17333 case PRAGMA_OMP_TASKLOOP:
17334 strcpy (p_name, "#pragma omp");
17335 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17336 break;
17337 case PRAGMA_OMP_TEAMS:
17338 strcpy (p_name, "#pragma omp");
17339 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17340 break;
17341 default:
17342 gcc_unreachable ();
17345 if (stmt)
17346 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17350 /* OpenMP 2.5:
17351 # pragma omp threadprivate (variable-list) */
17353 static void
17354 c_parser_omp_threadprivate (c_parser *parser)
17356 tree vars, t;
17357 location_t loc;
17359 c_parser_consume_pragma (parser);
17360 loc = c_parser_peek_token (parser)->location;
17361 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17363 /* Mark every variable in VARS to be assigned thread local storage. */
17364 for (t = vars; t; t = TREE_CHAIN (t))
17366 tree v = TREE_PURPOSE (t);
17368 /* FIXME diagnostics: Ideally we should keep individual
17369 locations for all the variables in the var list to make the
17370 following errors more precise. Perhaps
17371 c_parser_omp_var_list_parens() should construct a list of
17372 locations to go along with the var list. */
17374 /* If V had already been marked threadprivate, it doesn't matter
17375 whether it had been used prior to this point. */
17376 if (!VAR_P (v))
17377 error_at (loc, "%qD is not a variable", v);
17378 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17379 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17380 else if (! is_global_var (v))
17381 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17382 else if (TREE_TYPE (v) == error_mark_node)
17384 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17385 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17386 else
17388 if (! DECL_THREAD_LOCAL_P (v))
17390 set_decl_tls_model (v, decl_default_tls_model (v));
17391 /* If rtl has been already set for this var, call
17392 make_decl_rtl once again, so that encode_section_info
17393 has a chance to look at the new decl flags. */
17394 if (DECL_RTL_SET_P (v))
17395 make_decl_rtl (v);
17397 C_DECL_THREADPRIVATE_P (v) = 1;
17401 c_parser_skip_to_pragma_eol (parser);
17404 /* Cilk Plus <#pragma simd> parsing routines. */
17406 /* Helper function for c_parser_pragma. Perform some sanity checking
17407 for <#pragma simd> constructs. Returns FALSE if there was a
17408 problem. */
17410 static bool
17411 c_parser_cilk_verify_simd (c_parser *parser,
17412 enum pragma_context context)
17414 if (!flag_cilkplus)
17416 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17417 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17418 return false;
17420 if (context == pragma_external)
17422 c_parser_error (parser,"pragma simd must be inside a function");
17423 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17424 return false;
17426 return true;
17429 /* Cilk Plus:
17430 This function is shared by SIMD-enabled functions and #pragma simd.
17431 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17432 CLAUSES is unused. The main purpose of this function is to parse a
17433 vectorlength attribute or clause and check for parse errors.
17434 When IS_SIMD_FN is true then the function is merely caching the tokens
17435 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17436 cache is cleared since there is no reason to continue.
17437 Syntax:
17438 vectorlength ( constant-expression ) */
17440 static tree
17441 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17442 bool is_simd_fn)
17444 if (is_simd_fn)
17445 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17446 else
17447 /* The vectorlength clause behaves exactly like OpenMP's safelen
17448 clause. Represent it in OpenMP terms. */
17449 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17451 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17452 return clauses;
17454 location_t loc = c_parser_peek_token (parser)->location;
17455 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17456 expr = c_fully_fold (expr, false, NULL);
17458 /* If expr is an error_mark_node then the above function would have
17459 emitted an error. No reason to do it twice. */
17460 if (expr == error_mark_node)
17462 else if (!TREE_TYPE (expr)
17463 || !TREE_CONSTANT (expr)
17464 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17466 error_at (loc, "vectorlength must be an integer constant");
17467 else if (wi::exact_log2 (expr) == -1)
17468 error_at (loc, "vectorlength must be a power of 2");
17469 else
17471 if (is_simd_fn)
17473 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17474 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17475 OMP_CLAUSE_CHAIN (u) = clauses;
17476 clauses = u;
17478 else
17480 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17481 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17482 OMP_CLAUSE_CHAIN (u) = clauses;
17483 clauses = u;
17487 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17489 return clauses;
17492 /* Cilk Plus:
17493 linear ( simd-linear-variable-list )
17495 simd-linear-variable-list:
17496 simd-linear-variable
17497 simd-linear-variable-list , simd-linear-variable
17499 simd-linear-variable:
17500 id-expression
17501 id-expression : simd-linear-step
17503 simd-linear-step:
17504 conditional-expression */
17506 static tree
17507 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17509 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17510 return clauses;
17512 location_t loc = c_parser_peek_token (parser)->location;
17514 if (c_parser_next_token_is_not (parser, CPP_NAME)
17515 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17516 c_parser_error (parser, "expected identifier");
17518 while (c_parser_next_token_is (parser, CPP_NAME)
17519 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17521 tree var = lookup_name (c_parser_peek_token (parser)->value);
17523 if (var == NULL)
17525 undeclared_variable (c_parser_peek_token (parser)->location,
17526 c_parser_peek_token (parser)->value);
17527 c_parser_consume_token (parser);
17529 else if (var == error_mark_node)
17530 c_parser_consume_token (parser);
17531 else
17533 tree step = integer_one_node;
17535 /* Parse the linear step if present. */
17536 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17538 c_parser_consume_token (parser);
17539 c_parser_consume_token (parser);
17541 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17542 expr = c_fully_fold (expr, false, NULL);
17544 if (TREE_TYPE (expr)
17545 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17546 && (TREE_CONSTANT (expr)
17547 || DECL_P (expr)))
17548 step = expr;
17549 else
17550 c_parser_error (parser,
17551 "step size must be an integer constant "
17552 "expression or an integer variable");
17554 else
17555 c_parser_consume_token (parser);
17557 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17558 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17559 OMP_CLAUSE_DECL (u) = var;
17560 OMP_CLAUSE_LINEAR_STEP (u) = step;
17561 OMP_CLAUSE_CHAIN (u) = clauses;
17562 clauses = u;
17565 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17566 break;
17568 c_parser_consume_token (parser);
17571 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17573 return clauses;
17576 /* Returns the name of the next clause. If the clause is not
17577 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17578 not consumed. Otherwise, the appropriate pragma_simd_clause is
17579 returned and the token is consumed. */
17581 static pragma_omp_clause
17582 c_parser_cilk_clause_name (c_parser *parser)
17584 pragma_omp_clause result;
17585 c_token *token = c_parser_peek_token (parser);
17587 if (!token->value || token->type != CPP_NAME)
17588 return PRAGMA_CILK_CLAUSE_NONE;
17590 const char *p = IDENTIFIER_POINTER (token->value);
17592 if (!strcmp (p, "vectorlength"))
17593 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17594 else if (!strcmp (p, "linear"))
17595 result = PRAGMA_CILK_CLAUSE_LINEAR;
17596 else if (!strcmp (p, "private"))
17597 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17598 else if (!strcmp (p, "firstprivate"))
17599 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17600 else if (!strcmp (p, "lastprivate"))
17601 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17602 else if (!strcmp (p, "reduction"))
17603 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17604 else
17605 return PRAGMA_CILK_CLAUSE_NONE;
17607 c_parser_consume_token (parser);
17608 return result;
17611 /* Parse all #<pragma simd> clauses. Return the list of clauses
17612 found. */
17614 static tree
17615 c_parser_cilk_all_clauses (c_parser *parser)
17617 tree clauses = NULL;
17619 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17621 pragma_omp_clause c_kind;
17623 c_kind = c_parser_cilk_clause_name (parser);
17625 switch (c_kind)
17627 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17628 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17629 break;
17630 case PRAGMA_CILK_CLAUSE_LINEAR:
17631 clauses = c_parser_cilk_clause_linear (parser, clauses);
17632 break;
17633 case PRAGMA_CILK_CLAUSE_PRIVATE:
17634 /* Use the OpenMP counterpart. */
17635 clauses = c_parser_omp_clause_private (parser, clauses);
17636 break;
17637 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17638 /* Use the OpenMP counterpart. */
17639 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17640 break;
17641 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17642 /* Use the OpenMP counterpart. */
17643 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17644 break;
17645 case PRAGMA_CILK_CLAUSE_REDUCTION:
17646 /* Use the OpenMP counterpart. */
17647 clauses = c_parser_omp_clause_reduction (parser, clauses);
17648 break;
17649 default:
17650 c_parser_error (parser, "expected %<#pragma simd%> clause");
17651 goto saw_error;
17655 saw_error:
17656 c_parser_skip_to_pragma_eol (parser);
17657 return c_finish_omp_clauses (clauses, C_ORT_CILK);
17660 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17661 Here is the correct syntax of this pragma:
17662 #pragma cilk grainsize = <EXP>
17665 static void
17666 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
17668 extern tree convert_to_integer (tree, tree);
17670 /* consume the 'grainsize' keyword. */
17671 c_parser_consume_pragma (parser);
17673 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17675 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17676 if (g_expr.value == error_mark_node)
17678 c_parser_skip_to_pragma_eol (parser);
17679 return;
17681 tree grain = convert_to_integer (long_integer_type_node,
17682 c_fully_fold (g_expr.value, false,
17683 NULL));
17684 c_parser_skip_to_pragma_eol (parser);
17685 c_token *token = c_parser_peek_token (parser);
17686 if (token && token->type == CPP_KEYWORD
17687 && token->keyword == RID_CILK_FOR)
17689 if (grain == NULL_TREE || grain == error_mark_node)
17690 grain = integer_zero_node;
17691 c_parser_cilk_for (parser, grain, if_p);
17693 else
17694 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17695 "%<_Cilk_for%>");
17697 else
17698 c_parser_skip_to_pragma_eol (parser);
17701 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17703 static void
17704 c_parser_cilk_simd (c_parser *parser, bool *if_p)
17706 tree clauses = c_parser_cilk_all_clauses (parser);
17707 tree block = c_begin_compound_stmt (true);
17708 location_t loc = c_parser_peek_token (parser)->location;
17709 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
17710 block = c_end_compound_stmt (loc, block, true);
17711 add_stmt (block);
17714 /* Create an artificial decl with TYPE and emit initialization of it with
17715 INIT. */
17717 static tree
17718 c_get_temp_regvar (tree type, tree init)
17720 location_t loc = EXPR_LOCATION (init);
17721 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17722 DECL_ARTIFICIAL (decl) = 1;
17723 DECL_IGNORED_P (decl) = 1;
17724 pushdecl (decl);
17725 tree t = build2 (INIT_EXPR, type, decl, init);
17726 add_stmt (t);
17727 return decl;
17730 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17731 GRAIN is the grain value passed in through pragma or 0. */
17733 static void
17734 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
17736 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17737 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17738 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17739 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
17741 tree block = c_begin_compound_stmt (true);
17742 tree sb = push_stmt_list ();
17743 location_t loc = c_parser_peek_token (parser)->location;
17744 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
17745 if_p);
17746 sb = pop_stmt_list (sb);
17748 if (omp_for)
17750 tree omp_par = make_node (OMP_PARALLEL);
17751 TREE_TYPE (omp_par) = void_type_node;
17752 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17753 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17754 TREE_SIDE_EFFECTS (bind) = 1;
17755 BIND_EXPR_BODY (bind) = sb;
17756 OMP_PARALLEL_BODY (omp_par) = bind;
17757 if (OMP_FOR_PRE_BODY (omp_for))
17759 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17760 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17762 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17763 tree decl = TREE_OPERAND (init, 0);
17764 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17765 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17766 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17767 if (TREE_CODE (t) != INTEGER_CST)
17769 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17770 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17771 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17772 OMP_CLAUSE_CHAIN (c) = clauses;
17773 clauses = c;
17775 if (TREE_CODE (incr) == MODIFY_EXPR)
17777 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17778 if (TREE_CODE (t) != INTEGER_CST)
17780 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17781 = c_get_temp_regvar (TREE_TYPE (t), t);
17782 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17783 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17784 OMP_CLAUSE_CHAIN (c) = clauses;
17785 clauses = c;
17788 t = TREE_OPERAND (init, 1);
17789 if (TREE_CODE (t) != INTEGER_CST)
17791 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17792 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17793 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17794 OMP_CLAUSE_CHAIN (c) = clauses;
17795 clauses = c;
17797 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17798 OMP_CLAUSE_DECL (c) = decl;
17799 OMP_CLAUSE_CHAIN (c) = clauses;
17800 clauses = c;
17801 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17802 OMP_CLAUSE_OPERAND (c, 0)
17803 = cilk_for_number_of_iterations (omp_for);
17804 OMP_CLAUSE_CHAIN (c) = clauses;
17805 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
17806 add_stmt (omp_par);
17809 block = c_end_compound_stmt (loc, block, true);
17810 add_stmt (block);
17814 /* Parse a transaction attribute (GCC Extension).
17816 transaction-attribute:
17817 attributes
17818 [ [ any-word ] ]
17820 The transactional memory language description is written for C++,
17821 and uses the C++0x attribute syntax. For compatibility, allow the
17822 bracket style for transactions in C as well. */
17824 static tree
17825 c_parser_transaction_attributes (c_parser *parser)
17827 tree attr_name, attr = NULL;
17829 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17830 return c_parser_attributes (parser);
17832 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17833 return NULL_TREE;
17834 c_parser_consume_token (parser);
17835 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17836 goto error1;
17838 attr_name = c_parser_attribute_any_word (parser);
17839 if (attr_name)
17841 c_parser_consume_token (parser);
17842 attr = build_tree_list (attr_name, NULL_TREE);
17844 else
17845 c_parser_error (parser, "expected identifier");
17847 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17848 error1:
17849 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17850 return attr;
17853 /* Parse a __transaction_atomic or __transaction_relaxed statement
17854 (GCC Extension).
17856 transaction-statement:
17857 __transaction_atomic transaction-attribute[opt] compound-statement
17858 __transaction_relaxed compound-statement
17860 Note that the only valid attribute is: "outer".
17863 static tree
17864 c_parser_transaction (c_parser *parser, enum rid keyword)
17866 unsigned int old_in = parser->in_transaction;
17867 unsigned int this_in = 1, new_in;
17868 location_t loc = c_parser_peek_token (parser)->location;
17869 tree stmt, attrs;
17871 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17872 || keyword == RID_TRANSACTION_RELAXED)
17873 && c_parser_next_token_is_keyword (parser, keyword));
17874 c_parser_consume_token (parser);
17876 if (keyword == RID_TRANSACTION_RELAXED)
17877 this_in |= TM_STMT_ATTR_RELAXED;
17878 else
17880 attrs = c_parser_transaction_attributes (parser);
17881 if (attrs)
17882 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
17885 /* Keep track if we're in the lexical scope of an outer transaction. */
17886 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
17888 parser->in_transaction = new_in;
17889 stmt = c_parser_compound_statement (parser);
17890 parser->in_transaction = old_in;
17892 if (flag_tm)
17893 stmt = c_finish_transaction (loc, stmt, this_in);
17894 else
17895 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17896 "%<__transaction_atomic%> without transactional memory support enabled"
17897 : "%<__transaction_relaxed %> "
17898 "without transactional memory support enabled"));
17900 return stmt;
17903 /* Parse a __transaction_atomic or __transaction_relaxed expression
17904 (GCC Extension).
17906 transaction-expression:
17907 __transaction_atomic ( expression )
17908 __transaction_relaxed ( expression )
17911 static struct c_expr
17912 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
17914 struct c_expr ret;
17915 unsigned int old_in = parser->in_transaction;
17916 unsigned int this_in = 1;
17917 location_t loc = c_parser_peek_token (parser)->location;
17918 tree attrs;
17920 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17921 || keyword == RID_TRANSACTION_RELAXED)
17922 && c_parser_next_token_is_keyword (parser, keyword));
17923 c_parser_consume_token (parser);
17925 if (keyword == RID_TRANSACTION_RELAXED)
17926 this_in |= TM_STMT_ATTR_RELAXED;
17927 else
17929 attrs = c_parser_transaction_attributes (parser);
17930 if (attrs)
17931 this_in |= parse_tm_stmt_attr (attrs, 0);
17934 parser->in_transaction = this_in;
17935 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17937 tree expr = c_parser_expression (parser).value;
17938 ret.original_type = TREE_TYPE (expr);
17939 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
17940 if (this_in & TM_STMT_ATTR_RELAXED)
17941 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
17942 SET_EXPR_LOCATION (ret.value, loc);
17943 ret.original_code = TRANSACTION_EXPR;
17944 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
17947 goto error;
17950 else
17952 error:
17953 ret.value = error_mark_node;
17954 ret.original_code = ERROR_MARK;
17955 ret.original_type = NULL;
17957 parser->in_transaction = old_in;
17959 if (!flag_tm)
17960 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17961 "%<__transaction_atomic%> without transactional memory support enabled"
17962 : "%<__transaction_relaxed %> "
17963 "without transactional memory support enabled"));
17965 set_c_expr_source_range (&ret, loc, loc);
17967 return ret;
17970 /* Parse a __transaction_cancel statement (GCC Extension).
17972 transaction-cancel-statement:
17973 __transaction_cancel transaction-attribute[opt] ;
17975 Note that the only valid attribute is "outer".
17978 static tree
17979 c_parser_transaction_cancel (c_parser *parser)
17981 location_t loc = c_parser_peek_token (parser)->location;
17982 tree attrs;
17983 bool is_outer = false;
17985 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
17986 c_parser_consume_token (parser);
17988 attrs = c_parser_transaction_attributes (parser);
17989 if (attrs)
17990 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
17992 if (!flag_tm)
17994 error_at (loc, "%<__transaction_cancel%> without "
17995 "transactional memory support enabled");
17996 goto ret_error;
17998 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18000 error_at (loc, "%<__transaction_cancel%> within a "
18001 "%<__transaction_relaxed%>");
18002 goto ret_error;
18004 else if (is_outer)
18006 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18007 && !is_tm_may_cancel_outer (current_function_decl))
18009 error_at (loc, "outer %<__transaction_cancel%> not "
18010 "within outer %<__transaction_atomic%>");
18011 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18012 goto ret_error;
18015 else if (parser->in_transaction == 0)
18017 error_at (loc, "%<__transaction_cancel%> not within "
18018 "%<__transaction_atomic%>");
18019 goto ret_error;
18022 return add_stmt (build_tm_abort_call (loc, is_outer));
18024 ret_error:
18025 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18028 /* Parse a single source file. */
18030 void
18031 c_parse_file (void)
18033 /* Use local storage to begin. If the first token is a pragma, parse it.
18034 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18035 which will cause garbage collection. */
18036 c_parser tparser;
18038 memset (&tparser, 0, sizeof tparser);
18039 tparser.tokens = &tparser.tokens_buf[0];
18040 the_parser = &tparser;
18042 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18043 c_parser_pragma_pch_preprocess (&tparser);
18045 the_parser = ggc_alloc<c_parser> ();
18046 *the_parser = tparser;
18047 if (tparser.tokens == &tparser.tokens_buf[0])
18048 the_parser->tokens = &the_parser->tokens_buf[0];
18050 /* Initialize EH, if we've been told to do so. */
18051 if (flag_exceptions)
18052 using_eh_for_cleanups ();
18054 c_parser_translation_unit (the_parser);
18055 the_parser = NULL;
18058 /* This function parses Cilk Plus array notation. The starting index is
18059 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
18060 return value of this function is a tree_node called VALUE_TREE of type
18061 ARRAY_NOTATION_REF. */
18063 static tree
18064 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
18065 tree array_value)
18067 c_token *token = NULL;
18068 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
18069 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
18070 tree array_type_domain = NULL_TREE;
18072 if (array_value == error_mark_node || initial_index == error_mark_node)
18074 /* No need to continue. If either of these 2 were true, then an error
18075 must be emitted already. Thus, no need to emit them twice. */
18076 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18077 return error_mark_node;
18080 array_type = TREE_TYPE (array_value);
18081 gcc_assert (array_type);
18082 if (TREE_CODE (array_type) != ARRAY_TYPE
18083 && TREE_CODE (array_type) != POINTER_TYPE)
18085 error_at (loc, "base of array section must be pointer or array type");
18086 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18087 return error_mark_node;
18089 type = TREE_TYPE (array_type);
18090 token = c_parser_peek_token (parser);
18092 if (token->type == CPP_EOF)
18094 c_parser_error (parser, "expected %<:%> or numeral");
18095 return value_tree;
18097 else if (token->type == CPP_COLON)
18099 if (!initial_index)
18101 /* If we are here, then we have a case like this A[:]. */
18102 c_parser_consume_token (parser);
18103 if (TREE_CODE (array_type) == POINTER_TYPE)
18105 error_at (loc, "start-index and length fields necessary for "
18106 "using array notations in pointers");
18107 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18108 return error_mark_node;
18110 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18112 error_at (loc, "array notations cannot be used with function "
18113 "type");
18114 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18115 return error_mark_node;
18117 array_type_domain = TYPE_DOMAIN (array_type);
18119 if (!array_type_domain)
18121 error_at (loc, "start-index and length fields necessary for "
18122 "using array notations in dimensionless arrays");
18123 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18124 return error_mark_node;
18127 start_index = TYPE_MINVAL (array_type_domain);
18128 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
18129 start_index);
18130 if (!TYPE_MAXVAL (array_type_domain)
18131 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
18133 error_at (loc, "start-index and length fields necessary for "
18134 "using array notations in variable-length arrays");
18135 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18136 return error_mark_node;
18138 end_index = TYPE_MAXVAL (array_type_domain);
18139 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18140 end_index, integer_one_node);
18141 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18142 stride = build_int_cst (integer_type_node, 1);
18143 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18145 else if (initial_index != error_mark_node)
18147 /* If we are here, then there should be 2 possibilities:
18148 1. Array [EXPR : EXPR]
18149 2. Array [EXPR : EXPR : EXPR]
18151 start_index = initial_index;
18153 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18155 error_at (loc, "array notations cannot be used with function "
18156 "type");
18157 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18158 return error_mark_node;
18160 c_parser_consume_token (parser); /* consume the ':' */
18161 struct c_expr ce = c_parser_expression (parser);
18162 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18163 end_index = ce.value;
18164 if (!end_index || end_index == error_mark_node)
18166 c_parser_skip_to_end_of_block_or_statement (parser);
18167 return error_mark_node;
18169 if (c_parser_peek_token (parser)->type == CPP_COLON)
18171 c_parser_consume_token (parser);
18172 ce = c_parser_expression (parser);
18173 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18174 stride = ce.value;
18175 if (!stride || stride == error_mark_node)
18177 c_parser_skip_to_end_of_block_or_statement (parser);
18178 return error_mark_node;
18182 else
18183 c_parser_error (parser, "expected array notation expression");
18185 else
18186 c_parser_error (parser, "expected array notation expression");
18188 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18190 value_tree = build_array_notation_ref (loc, array_value, start_index,
18191 end_index, stride, type);
18192 if (value_tree != error_mark_node)
18193 SET_EXPR_LOCATION (value_tree, loc);
18194 return value_tree;
18197 #include "gt-c-c-parser.h"