gcc/ChangeLog:
[official-gcc.git] / gcc / c / c-parser.c
blob5c152ab28b0c623b1b4cf2f21b2fb61e64f3db87
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2017 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-general.h"
56 #include "omp-offload.h"
57 #include "builtins.h"
58 #include "gomp-constants.h"
59 #include "c-family/c-indentation.h"
60 #include "gimple-expr.h"
61 #include "context.h"
62 #include "gcc-rich-location.h"
63 #include "c-parser.h"
64 #include "gimple-parser.h"
65 #include "read-rtl-function.h"
66 #include "run-rtl-passes.h"
68 /* We need to walk over decls with incomplete struct/union/enum types
69 after parsing the whole translation unit.
70 In finish_decl(), if the decl is static, has incomplete
71 struct/union/enum type, it is appeneded to incomplete_record_decls.
72 In c_parser_translation_unit(), we iterate over incomplete_record_decls
73 and report error if any of the decls are still incomplete. */
75 vec<tree> incomplete_record_decls;
77 void
78 set_c_expr_source_range (c_expr *expr,
79 location_t start, location_t finish)
81 expr->src_range.m_start = start;
82 expr->src_range.m_finish = finish;
83 if (expr->value)
84 set_source_range (expr->value, start, finish);
87 void
88 set_c_expr_source_range (c_expr *expr,
89 source_range src_range)
91 expr->src_range = src_range;
92 if (expr->value)
93 set_source_range (expr->value, src_range);
97 /* Initialization routine for this file. */
99 void
100 c_parse_init (void)
102 /* The only initialization required is of the reserved word
103 identifiers. */
104 unsigned int i;
105 tree id;
106 int mask = 0;
108 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
109 the c_token structure. */
110 gcc_assert (RID_MAX <= 255);
112 mask |= D_CXXONLY;
113 if (!flag_isoc99)
114 mask |= D_C99;
115 if (flag_no_asm)
117 mask |= D_ASM | D_EXT;
118 if (!flag_isoc99)
119 mask |= D_EXT89;
121 if (!c_dialect_objc ())
122 mask |= D_OBJC | D_CXX_OBJC;
124 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
125 for (i = 0; i < num_c_common_reswords; i++)
127 /* If a keyword is disabled, do not enter it into the table
128 and so create a canonical spelling that isn't a keyword. */
129 if (c_common_reswords[i].disable & mask)
131 if (warn_cxx_compat
132 && (c_common_reswords[i].disable & D_CXXWARN))
134 id = get_identifier (c_common_reswords[i].word);
135 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
136 C_IS_RESERVED_WORD (id) = 1;
138 continue;
141 id = get_identifier (c_common_reswords[i].word);
142 C_SET_RID_CODE (id, c_common_reswords[i].rid);
143 C_IS_RESERVED_WORD (id) = 1;
144 ridpointers [(int) c_common_reswords[i].rid] = id;
147 for (i = 0; i < NUM_INT_N_ENTS; i++)
149 /* We always create the symbols but they aren't always supported. */
150 char name[50];
151 sprintf (name, "__int%d", int_n_data[i].bitsize);
152 id = get_identifier (name);
153 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
154 C_IS_RESERVED_WORD (id) = 1;
158 /* A parser structure recording information about the state and
159 context of parsing. Includes lexer information with up to two
160 tokens of look-ahead; more are not needed for C. */
161 struct GTY(()) c_parser {
162 /* The look-ahead tokens. */
163 c_token * GTY((skip)) tokens;
164 /* Buffer for look-ahead tokens. */
165 c_token tokens_buf[4];
166 /* How many look-ahead tokens are available (0 - 4, or
167 more if parsing from pre-lexed tokens). */
168 unsigned int tokens_avail;
169 /* True if a syntax error is being recovered from; false otherwise.
170 c_parser_error sets this flag. It should clear this flag when
171 enough tokens have been consumed to recover from the error. */
172 BOOL_BITFIELD error : 1;
173 /* True if we're processing a pragma, and shouldn't automatically
174 consume CPP_PRAGMA_EOL. */
175 BOOL_BITFIELD in_pragma : 1;
176 /* True if we're parsing the outermost block of an if statement. */
177 BOOL_BITFIELD in_if_block : 1;
178 /* True if we want to lex an untranslated string. */
179 BOOL_BITFIELD lex_untranslated_string : 1;
181 /* Objective-C specific parser/lexer information. */
183 /* True if we are in a context where the Objective-C "PQ" keywords
184 are considered keywords. */
185 BOOL_BITFIELD objc_pq_context : 1;
186 /* True if we are parsing a (potential) Objective-C foreach
187 statement. This is set to true after we parsed 'for (' and while
188 we wait for 'in' or ';' to decide if it's a standard C for loop or an
189 Objective-C foreach loop. */
190 BOOL_BITFIELD objc_could_be_foreach_context : 1;
191 /* The following flag is needed to contextualize Objective-C lexical
192 analysis. In some cases (e.g., 'int NSObject;'), it is
193 undesirable to bind an identifier to an Objective-C class, even
194 if a class with that name exists. */
195 BOOL_BITFIELD objc_need_raw_identifier : 1;
196 /* Nonzero if we're processing a __transaction statement. The value
197 is 1 | TM_STMT_ATTR_*. */
198 unsigned int in_transaction : 4;
199 /* True if we are in a context where the Objective-C "Property attribute"
200 keywords are valid. */
201 BOOL_BITFIELD objc_property_attr_context : 1;
203 /* Cilk Plus specific parser/lexer information. */
205 /* Buffer to hold all the tokens from parsing the vector attribute for the
206 SIMD-enabled functions (formerly known as elemental functions). */
207 vec <c_token, va_gc> *cilk_simd_fn_tokens;
210 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
212 c_token *
213 c_parser_tokens_buf (c_parser *parser, unsigned n)
215 return &parser->tokens_buf[n];
218 /* Return the error state of PARSER. */
220 bool
221 c_parser_error (c_parser *parser)
223 return parser->error;
226 /* Set the error state of PARSER to ERR. */
228 void
229 c_parser_set_error (c_parser *parser, bool err)
231 parser->error = err;
235 /* The actual parser and external interface. ??? Does this need to be
236 garbage-collected? */
238 static GTY (()) c_parser *the_parser;
240 /* Read in and lex a single token, storing it in *TOKEN. */
242 static void
243 c_lex_one_token (c_parser *parser, c_token *token)
245 timevar_push (TV_LEX);
247 token->type = c_lex_with_flags (&token->value, &token->location,
248 &token->flags,
249 (parser->lex_untranslated_string
250 ? C_LEX_STRING_NO_TRANSLATE : 0));
251 token->id_kind = C_ID_NONE;
252 token->keyword = RID_MAX;
253 token->pragma_kind = PRAGMA_NONE;
255 switch (token->type)
257 case CPP_NAME:
259 tree decl;
261 bool objc_force_identifier = parser->objc_need_raw_identifier;
262 if (c_dialect_objc ())
263 parser->objc_need_raw_identifier = false;
265 if (C_IS_RESERVED_WORD (token->value))
267 enum rid rid_code = C_RID_CODE (token->value);
269 if (rid_code == RID_CXX_COMPAT_WARN)
271 warning_at (token->location,
272 OPT_Wc___compat,
273 "identifier %qE conflicts with C++ keyword",
274 token->value);
276 else if (rid_code >= RID_FIRST_ADDR_SPACE
277 && rid_code <= RID_LAST_ADDR_SPACE)
279 addr_space_t as;
280 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
281 targetm.addr_space.diagnose_usage (as, token->location);
282 token->id_kind = C_ID_ADDRSPACE;
283 token->keyword = rid_code;
284 break;
286 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
288 /* We found an Objective-C "pq" keyword (in, out,
289 inout, bycopy, byref, oneway). They need special
290 care because the interpretation depends on the
291 context. */
292 if (parser->objc_pq_context)
294 token->type = CPP_KEYWORD;
295 token->keyword = rid_code;
296 break;
298 else if (parser->objc_could_be_foreach_context
299 && rid_code == RID_IN)
301 /* We are in Objective-C, inside a (potential)
302 foreach context (which means after having
303 parsed 'for (', but before having parsed ';'),
304 and we found 'in'. We consider it the keyword
305 which terminates the declaration at the
306 beginning of a foreach-statement. Note that
307 this means you can't use 'in' for anything else
308 in that context; in particular, in Objective-C
309 you can't use 'in' as the name of the running
310 variable in a C for loop. We could potentially
311 try to add code here to disambiguate, but it
312 seems a reasonable limitation. */
313 token->type = CPP_KEYWORD;
314 token->keyword = rid_code;
315 break;
317 /* Else, "pq" keywords outside of the "pq" context are
318 not keywords, and we fall through to the code for
319 normal tokens. */
321 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
323 /* We found an Objective-C "property attribute"
324 keyword (getter, setter, readonly, etc). These are
325 only valid in the property context. */
326 if (parser->objc_property_attr_context)
328 token->type = CPP_KEYWORD;
329 token->keyword = rid_code;
330 break;
332 /* Else they are not special keywords.
335 else if (c_dialect_objc ()
336 && (OBJC_IS_AT_KEYWORD (rid_code)
337 || OBJC_IS_CXX_KEYWORD (rid_code)))
339 /* We found one of the Objective-C "@" keywords (defs,
340 selector, synchronized, etc) or one of the
341 Objective-C "cxx" keywords (class, private,
342 protected, public, try, catch, throw) without a
343 preceding '@' sign. Do nothing and fall through to
344 the code for normal tokens (in C++ we would still
345 consider the CXX ones keywords, but not in C). */
348 else
350 token->type = CPP_KEYWORD;
351 token->keyword = rid_code;
352 break;
356 decl = lookup_name (token->value);
357 if (decl)
359 if (TREE_CODE (decl) == TYPE_DECL)
361 token->id_kind = C_ID_TYPENAME;
362 break;
365 else if (c_dialect_objc ())
367 tree objc_interface_decl = objc_is_class_name (token->value);
368 /* Objective-C class names are in the same namespace as
369 variables and typedefs, and hence are shadowed by local
370 declarations. */
371 if (objc_interface_decl
372 && (!objc_force_identifier || global_bindings_p ()))
374 token->value = objc_interface_decl;
375 token->id_kind = C_ID_CLASSNAME;
376 break;
379 token->id_kind = C_ID_ID;
381 break;
382 case CPP_AT_NAME:
383 /* This only happens in Objective-C; it must be a keyword. */
384 token->type = CPP_KEYWORD;
385 switch (C_RID_CODE (token->value))
387 /* Replace 'class' with '@class', 'private' with '@private',
388 etc. This prevents confusion with the C++ keyword
389 'class', and makes the tokens consistent with other
390 Objective-C 'AT' keywords. For example '@class' is
391 reported as RID_AT_CLASS which is consistent with
392 '@synchronized', which is reported as
393 RID_AT_SYNCHRONIZED.
395 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
396 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
397 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
398 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
399 case RID_THROW: token->keyword = RID_AT_THROW; break;
400 case RID_TRY: token->keyword = RID_AT_TRY; break;
401 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
402 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
403 default: token->keyword = C_RID_CODE (token->value);
405 break;
406 case CPP_COLON:
407 case CPP_COMMA:
408 case CPP_CLOSE_PAREN:
409 case CPP_SEMICOLON:
410 /* These tokens may affect the interpretation of any identifiers
411 following, if doing Objective-C. */
412 if (c_dialect_objc ())
413 parser->objc_need_raw_identifier = false;
414 break;
415 case CPP_PRAGMA:
416 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
417 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
418 token->value = NULL;
419 break;
420 default:
421 break;
423 timevar_pop (TV_LEX);
426 /* Return a pointer to the next token from PARSER, reading it in if
427 necessary. */
429 c_token *
430 c_parser_peek_token (c_parser *parser)
432 if (parser->tokens_avail == 0)
434 c_lex_one_token (parser, &parser->tokens[0]);
435 parser->tokens_avail = 1;
437 return &parser->tokens[0];
440 /* Return a pointer to the next-but-one token from PARSER, reading it
441 in if necessary. The next token is already read in. */
443 c_token *
444 c_parser_peek_2nd_token (c_parser *parser)
446 if (parser->tokens_avail >= 2)
447 return &parser->tokens[1];
448 gcc_assert (parser->tokens_avail == 1);
449 gcc_assert (parser->tokens[0].type != CPP_EOF);
450 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
451 c_lex_one_token (parser, &parser->tokens[1]);
452 parser->tokens_avail = 2;
453 return &parser->tokens[1];
456 /* Return a pointer to the Nth token from PARSER, reading it
457 in if necessary. The N-1th token is already read in. */
459 c_token *
460 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
462 /* N is 1-based, not zero-based. */
463 gcc_assert (n > 0);
465 if (parser->tokens_avail >= n)
466 return &parser->tokens[n - 1];
467 gcc_assert (parser->tokens_avail == n - 1);
468 c_lex_one_token (parser, &parser->tokens[n - 1]);
469 parser->tokens_avail = n;
470 return &parser->tokens[n - 1];
473 bool
474 c_keyword_starts_typename (enum rid keyword)
476 switch (keyword)
478 case RID_UNSIGNED:
479 case RID_LONG:
480 case RID_SHORT:
481 case RID_SIGNED:
482 case RID_COMPLEX:
483 case RID_INT:
484 case RID_CHAR:
485 case RID_FLOAT:
486 case RID_DOUBLE:
487 case RID_VOID:
488 case RID_DFLOAT32:
489 case RID_DFLOAT64:
490 case RID_DFLOAT128:
491 CASE_RID_FLOATN_NX:
492 case RID_BOOL:
493 case RID_ENUM:
494 case RID_STRUCT:
495 case RID_UNION:
496 case RID_TYPEOF:
497 case RID_CONST:
498 case RID_ATOMIC:
499 case RID_VOLATILE:
500 case RID_RESTRICT:
501 case RID_ATTRIBUTE:
502 case RID_FRACT:
503 case RID_ACCUM:
504 case RID_SAT:
505 case RID_AUTO_TYPE:
506 return true;
507 default:
508 if (keyword >= RID_FIRST_INT_N
509 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
510 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
511 return true;
512 return false;
516 /* Return true if TOKEN can start a type name,
517 false otherwise. */
518 bool
519 c_token_starts_typename (c_token *token)
521 switch (token->type)
523 case CPP_NAME:
524 switch (token->id_kind)
526 case C_ID_ID:
527 return false;
528 case C_ID_ADDRSPACE:
529 return true;
530 case C_ID_TYPENAME:
531 return true;
532 case C_ID_CLASSNAME:
533 gcc_assert (c_dialect_objc ());
534 return true;
535 default:
536 gcc_unreachable ();
538 case CPP_KEYWORD:
539 return c_keyword_starts_typename (token->keyword);
540 case CPP_LESS:
541 if (c_dialect_objc ())
542 return true;
543 return false;
544 default:
545 return false;
549 /* Return true if the next token from PARSER can start a type name,
550 false otherwise. LA specifies how to do lookahead in order to
551 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
553 static inline bool
554 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
556 c_token *token = c_parser_peek_token (parser);
557 if (c_token_starts_typename (token))
558 return true;
560 /* Try a bit harder to detect an unknown typename. */
561 if (la != cla_prefer_id
562 && token->type == CPP_NAME
563 && token->id_kind == C_ID_ID
565 /* Do not try too hard when we could have "object in array". */
566 && !parser->objc_could_be_foreach_context
568 && (la == cla_prefer_type
569 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
570 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
572 /* Only unknown identifiers. */
573 && !lookup_name (token->value))
574 return true;
576 return false;
579 /* Return true if TOKEN is a type qualifier, false otherwise. */
580 static bool
581 c_token_is_qualifier (c_token *token)
583 switch (token->type)
585 case CPP_NAME:
586 switch (token->id_kind)
588 case C_ID_ADDRSPACE:
589 return true;
590 default:
591 return false;
593 case CPP_KEYWORD:
594 switch (token->keyword)
596 case RID_CONST:
597 case RID_VOLATILE:
598 case RID_RESTRICT:
599 case RID_ATTRIBUTE:
600 case RID_ATOMIC:
601 return true;
602 default:
603 return false;
605 case CPP_LESS:
606 return false;
607 default:
608 gcc_unreachable ();
612 /* Return true if the next token from PARSER is a type qualifier,
613 false otherwise. */
614 static inline bool
615 c_parser_next_token_is_qualifier (c_parser *parser)
617 c_token *token = c_parser_peek_token (parser);
618 return c_token_is_qualifier (token);
621 /* Return true if TOKEN can start declaration specifiers, false
622 otherwise. */
623 static bool
624 c_token_starts_declspecs (c_token *token)
626 switch (token->type)
628 case CPP_NAME:
629 switch (token->id_kind)
631 case C_ID_ID:
632 return false;
633 case C_ID_ADDRSPACE:
634 return true;
635 case C_ID_TYPENAME:
636 return true;
637 case C_ID_CLASSNAME:
638 gcc_assert (c_dialect_objc ());
639 return true;
640 default:
641 gcc_unreachable ();
643 case CPP_KEYWORD:
644 switch (token->keyword)
646 case RID_STATIC:
647 case RID_EXTERN:
648 case RID_REGISTER:
649 case RID_TYPEDEF:
650 case RID_INLINE:
651 case RID_NORETURN:
652 case RID_AUTO:
653 case RID_THREAD:
654 case RID_UNSIGNED:
655 case RID_LONG:
656 case RID_SHORT:
657 case RID_SIGNED:
658 case RID_COMPLEX:
659 case RID_INT:
660 case RID_CHAR:
661 case RID_FLOAT:
662 case RID_DOUBLE:
663 case RID_VOID:
664 case RID_DFLOAT32:
665 case RID_DFLOAT64:
666 case RID_DFLOAT128:
667 CASE_RID_FLOATN_NX:
668 case RID_BOOL:
669 case RID_ENUM:
670 case RID_STRUCT:
671 case RID_UNION:
672 case RID_TYPEOF:
673 case RID_CONST:
674 case RID_VOLATILE:
675 case RID_RESTRICT:
676 case RID_ATTRIBUTE:
677 case RID_FRACT:
678 case RID_ACCUM:
679 case RID_SAT:
680 case RID_ALIGNAS:
681 case RID_ATOMIC:
682 case RID_AUTO_TYPE:
683 return true;
684 default:
685 if (token->keyword >= RID_FIRST_INT_N
686 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
687 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
688 return true;
689 return false;
691 case CPP_LESS:
692 if (c_dialect_objc ())
693 return true;
694 return false;
695 default:
696 return false;
701 /* Return true if TOKEN can start declaration specifiers or a static
702 assertion, false otherwise. */
703 static bool
704 c_token_starts_declaration (c_token *token)
706 if (c_token_starts_declspecs (token)
707 || token->keyword == RID_STATIC_ASSERT)
708 return true;
709 else
710 return false;
713 /* Return true if the next token from PARSER can start declaration
714 specifiers, false otherwise. */
715 bool
716 c_parser_next_token_starts_declspecs (c_parser *parser)
718 c_token *token = c_parser_peek_token (parser);
720 /* In Objective-C, a classname normally starts a declspecs unless it
721 is immediately followed by a dot. In that case, it is the
722 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
723 setter/getter on the class. c_token_starts_declspecs() can't
724 differentiate between the two cases because it only checks the
725 current token, so we have a special check here. */
726 if (c_dialect_objc ()
727 && token->type == CPP_NAME
728 && token->id_kind == C_ID_CLASSNAME
729 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
730 return false;
732 return c_token_starts_declspecs (token);
735 /* Return true if the next tokens from PARSER can start declaration
736 specifiers or a static assertion, false otherwise. */
737 bool
738 c_parser_next_tokens_start_declaration (c_parser *parser)
740 c_token *token = c_parser_peek_token (parser);
742 /* Same as above. */
743 if (c_dialect_objc ()
744 && token->type == CPP_NAME
745 && token->id_kind == C_ID_CLASSNAME
746 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
747 return false;
749 /* Labels do not start declarations. */
750 if (token->type == CPP_NAME
751 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
752 return false;
754 if (c_token_starts_declaration (token))
755 return true;
757 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
758 return true;
760 return false;
763 /* Consume the next token from PARSER. */
765 void
766 c_parser_consume_token (c_parser *parser)
768 gcc_assert (parser->tokens_avail >= 1);
769 gcc_assert (parser->tokens[0].type != CPP_EOF);
770 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
771 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
772 if (parser->tokens != &parser->tokens_buf[0])
773 parser->tokens++;
774 else if (parser->tokens_avail == 2)
775 parser->tokens[0] = parser->tokens[1];
776 parser->tokens_avail--;
779 /* Expect the current token to be a #pragma. Consume it and remember
780 that we've begun parsing a pragma. */
782 static void
783 c_parser_consume_pragma (c_parser *parser)
785 gcc_assert (!parser->in_pragma);
786 gcc_assert (parser->tokens_avail >= 1);
787 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
788 if (parser->tokens != &parser->tokens_buf[0])
789 parser->tokens++;
790 else if (parser->tokens_avail == 2)
791 parser->tokens[0] = parser->tokens[1];
792 parser->tokens_avail--;
793 parser->in_pragma = true;
796 /* Update the global input_location from TOKEN. */
797 static inline void
798 c_parser_set_source_position_from_token (c_token *token)
800 if (token->type != CPP_EOF)
802 input_location = token->location;
806 /* Helper function for c_parser_error.
807 Having peeked a token of kind TOK1_KIND that might signify
808 a conflict marker, peek successor tokens to determine
809 if we actually do have a conflict marker.
810 Specifically, we consider a run of 7 '<', '=' or '>' characters
811 at the start of a line as a conflict marker.
812 These come through the lexer as three pairs and a single,
813 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
814 If it returns true, *OUT_LOC is written to with the location/range
815 of the marker. */
817 static bool
818 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
819 location_t *out_loc)
821 c_token *token2 = c_parser_peek_2nd_token (parser);
822 if (token2->type != tok1_kind)
823 return false;
824 c_token *token3 = c_parser_peek_nth_token (parser, 3);
825 if (token3->type != tok1_kind)
826 return false;
827 c_token *token4 = c_parser_peek_nth_token (parser, 4);
828 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
829 return false;
831 /* It must be at the start of the line. */
832 location_t start_loc = c_parser_peek_token (parser)->location;
833 if (LOCATION_COLUMN (start_loc) != 1)
834 return false;
836 /* We have a conflict marker. Construct a location of the form:
837 <<<<<<<
838 ^~~~~~~
839 with start == caret, finishing at the end of the marker. */
840 location_t finish_loc = get_finish (token4->location);
841 *out_loc = make_location (start_loc, start_loc, finish_loc);
843 return true;
846 /* Issue a diagnostic of the form
847 FILE:LINE: MESSAGE before TOKEN
848 where TOKEN is the next token in the input stream of PARSER.
849 MESSAGE (specified by the caller) is usually of the form "expected
850 OTHER-TOKEN".
852 Do not issue a diagnostic if still recovering from an error.
854 ??? This is taken from the C++ parser, but building up messages in
855 this way is not i18n-friendly and some other approach should be
856 used. */
858 void
859 c_parser_error (c_parser *parser, const char *gmsgid)
861 c_token *token = c_parser_peek_token (parser);
862 if (parser->error)
863 return;
864 parser->error = true;
865 if (!gmsgid)
866 return;
868 /* If this is actually a conflict marker, report it as such. */
869 if (token->type == CPP_LSHIFT
870 || token->type == CPP_RSHIFT
871 || token->type == CPP_EQ_EQ)
873 location_t loc;
874 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
876 error_at (loc, "version control conflict marker in file");
877 return;
881 /* This diagnostic makes more sense if it is tagged to the line of
882 the token we just peeked at. */
883 c_parser_set_source_position_from_token (token);
884 c_parse_error (gmsgid,
885 /* Because c_parse_error does not understand
886 CPP_KEYWORD, keywords are treated like
887 identifiers. */
888 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
889 /* ??? The C parser does not save the cpp flags of a
890 token, we need to pass 0 here and we will not get
891 the source spelling of some tokens but rather the
892 canonical spelling. */
893 token->value, /*flags=*/0);
896 /* If the next token is of the indicated TYPE, consume it. Otherwise,
897 issue the error MSGID. If MSGID is NULL then a message has already
898 been produced and no message will be produced this time. Returns
899 true if found, false otherwise. */
901 bool
902 c_parser_require (c_parser *parser,
903 enum cpp_ttype type,
904 const char *msgid)
906 if (c_parser_next_token_is (parser, type))
908 c_parser_consume_token (parser);
909 return true;
911 else
913 c_parser_error (parser, msgid);
914 return false;
918 /* If the next token is the indicated keyword, consume it. Otherwise,
919 issue the error MSGID. Returns true if found, false otherwise. */
921 static bool
922 c_parser_require_keyword (c_parser *parser,
923 enum rid keyword,
924 const char *msgid)
926 if (c_parser_next_token_is_keyword (parser, keyword))
928 c_parser_consume_token (parser);
929 return true;
931 else
933 c_parser_error (parser, msgid);
934 return false;
938 /* Like c_parser_require, except that tokens will be skipped until the
939 desired token is found. An error message is still produced if the
940 next token is not as expected. If MSGID is NULL then a message has
941 already been produced and no message will be produced this
942 time. */
944 void
945 c_parser_skip_until_found (c_parser *parser,
946 enum cpp_ttype type,
947 const char *msgid)
949 unsigned nesting_depth = 0;
951 if (c_parser_require (parser, type, msgid))
952 return;
954 /* Skip tokens until the desired token is found. */
955 while (true)
957 /* Peek at the next token. */
958 c_token *token = c_parser_peek_token (parser);
959 /* If we've reached the token we want, consume it and stop. */
960 if (token->type == type && !nesting_depth)
962 c_parser_consume_token (parser);
963 break;
966 /* If we've run out of tokens, stop. */
967 if (token->type == CPP_EOF)
968 return;
969 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
970 return;
971 if (token->type == CPP_OPEN_BRACE
972 || token->type == CPP_OPEN_PAREN
973 || token->type == CPP_OPEN_SQUARE)
974 ++nesting_depth;
975 else if (token->type == CPP_CLOSE_BRACE
976 || token->type == CPP_CLOSE_PAREN
977 || token->type == CPP_CLOSE_SQUARE)
979 if (nesting_depth-- == 0)
980 break;
982 /* Consume this token. */
983 c_parser_consume_token (parser);
985 parser->error = false;
988 /* Skip tokens until the end of a parameter is found, but do not
989 consume the comma, semicolon or closing delimiter. */
991 static void
992 c_parser_skip_to_end_of_parameter (c_parser *parser)
994 unsigned nesting_depth = 0;
996 while (true)
998 c_token *token = c_parser_peek_token (parser);
999 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1000 && !nesting_depth)
1001 break;
1002 /* If we've run out of tokens, stop. */
1003 if (token->type == CPP_EOF)
1004 return;
1005 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1006 return;
1007 if (token->type == CPP_OPEN_BRACE
1008 || token->type == CPP_OPEN_PAREN
1009 || token->type == CPP_OPEN_SQUARE)
1010 ++nesting_depth;
1011 else if (token->type == CPP_CLOSE_BRACE
1012 || token->type == CPP_CLOSE_PAREN
1013 || token->type == CPP_CLOSE_SQUARE)
1015 if (nesting_depth-- == 0)
1016 break;
1018 /* Consume this token. */
1019 c_parser_consume_token (parser);
1021 parser->error = false;
1024 /* Expect to be at the end of the pragma directive and consume an
1025 end of line marker. */
1027 static void
1028 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1030 gcc_assert (parser->in_pragma);
1031 parser->in_pragma = false;
1033 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1034 c_parser_error (parser, "expected end of line");
1036 cpp_ttype token_type;
1039 c_token *token = c_parser_peek_token (parser);
1040 token_type = token->type;
1041 if (token_type == CPP_EOF)
1042 break;
1043 c_parser_consume_token (parser);
1045 while (token_type != CPP_PRAGMA_EOL);
1047 parser->error = false;
1050 /* Skip tokens until we have consumed an entire block, or until we
1051 have consumed a non-nested ';'. */
1053 static void
1054 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1056 unsigned nesting_depth = 0;
1057 bool save_error = parser->error;
1059 while (true)
1061 c_token *token;
1063 /* Peek at the next token. */
1064 token = c_parser_peek_token (parser);
1066 switch (token->type)
1068 case CPP_EOF:
1069 return;
1071 case CPP_PRAGMA_EOL:
1072 if (parser->in_pragma)
1073 return;
1074 break;
1076 case CPP_SEMICOLON:
1077 /* If the next token is a ';', we have reached the
1078 end of the statement. */
1079 if (!nesting_depth)
1081 /* Consume the ';'. */
1082 c_parser_consume_token (parser);
1083 goto finished;
1085 break;
1087 case CPP_CLOSE_BRACE:
1088 /* If the next token is a non-nested '}', then we have
1089 reached the end of the current block. */
1090 if (nesting_depth == 0 || --nesting_depth == 0)
1092 c_parser_consume_token (parser);
1093 goto finished;
1095 break;
1097 case CPP_OPEN_BRACE:
1098 /* If it the next token is a '{', then we are entering a new
1099 block. Consume the entire block. */
1100 ++nesting_depth;
1101 break;
1103 case CPP_PRAGMA:
1104 /* If we see a pragma, consume the whole thing at once. We
1105 have some safeguards against consuming pragmas willy-nilly.
1106 Normally, we'd expect to be here with parser->error set,
1107 which disables these safeguards. But it's possible to get
1108 here for secondary error recovery, after parser->error has
1109 been cleared. */
1110 c_parser_consume_pragma (parser);
1111 c_parser_skip_to_pragma_eol (parser);
1112 parser->error = save_error;
1113 continue;
1115 default:
1116 break;
1119 c_parser_consume_token (parser);
1122 finished:
1123 parser->error = false;
1126 /* CPP's options (initialized by c-opts.c). */
1127 extern cpp_options *cpp_opts;
1129 /* Save the warning flags which are controlled by __extension__. */
1131 static inline int
1132 disable_extension_diagnostics (void)
1134 int ret = (pedantic
1135 | (warn_pointer_arith << 1)
1136 | (warn_traditional << 2)
1137 | (flag_iso << 3)
1138 | (warn_long_long << 4)
1139 | (warn_cxx_compat << 5)
1140 | (warn_overlength_strings << 6)
1141 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1142 play tricks to properly restore it. */
1143 | ((warn_c90_c99_compat == 1) << 7)
1144 | ((warn_c90_c99_compat == -1) << 8)
1145 /* Similarly for warn_c99_c11_compat. */
1146 | ((warn_c99_c11_compat == 1) << 9)
1147 | ((warn_c99_c11_compat == -1) << 10)
1149 cpp_opts->cpp_pedantic = pedantic = 0;
1150 warn_pointer_arith = 0;
1151 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1152 flag_iso = 0;
1153 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1154 warn_cxx_compat = 0;
1155 warn_overlength_strings = 0;
1156 warn_c90_c99_compat = 0;
1157 warn_c99_c11_compat = 0;
1158 return ret;
1161 /* Restore the warning flags which are controlled by __extension__.
1162 FLAGS is the return value from disable_extension_diagnostics. */
1164 static inline void
1165 restore_extension_diagnostics (int flags)
1167 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1168 warn_pointer_arith = (flags >> 1) & 1;
1169 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1170 flag_iso = (flags >> 3) & 1;
1171 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1172 warn_cxx_compat = (flags >> 5) & 1;
1173 warn_overlength_strings = (flags >> 6) & 1;
1174 /* See above for why is this needed. */
1175 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1176 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1179 /* Helper data structure for parsing #pragma acc routine. */
1180 struct oacc_routine_data {
1181 bool error_seen; /* Set if error has been reported. */
1182 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1183 tree clauses;
1184 location_t loc;
1187 static void c_parser_external_declaration (c_parser *);
1188 static void c_parser_asm_definition (c_parser *);
1189 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1190 bool, bool, tree *, vec<c_token>,
1191 struct oacc_routine_data * = NULL,
1192 bool * = NULL);
1193 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1194 static void c_parser_static_assert_declaration (c_parser *);
1195 static struct c_typespec c_parser_enum_specifier (c_parser *);
1196 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1197 static tree c_parser_struct_declaration (c_parser *);
1198 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1199 static tree c_parser_alignas_specifier (c_parser *);
1200 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1201 c_dtr_syn, bool *);
1202 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1203 bool,
1204 struct c_declarator *);
1205 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1206 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1207 tree);
1208 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1209 static tree c_parser_simple_asm_expr (c_parser *);
1210 static tree c_parser_attributes (c_parser *);
1211 static struct c_expr c_parser_initializer (c_parser *);
1212 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1213 struct obstack *);
1214 static void c_parser_initelt (c_parser *, struct obstack *);
1215 static void c_parser_initval (c_parser *, struct c_expr *,
1216 struct obstack *);
1217 static tree c_parser_compound_statement (c_parser *);
1218 static void c_parser_compound_statement_nostart (c_parser *);
1219 static void c_parser_label (c_parser *);
1220 static void c_parser_statement (c_parser *, bool *);
1221 static void c_parser_statement_after_labels (c_parser *, bool *,
1222 vec<tree> * = NULL);
1223 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1224 static void c_parser_switch_statement (c_parser *, bool *);
1225 static void c_parser_while_statement (c_parser *, bool, bool *);
1226 static void c_parser_do_statement (c_parser *, bool);
1227 static void c_parser_for_statement (c_parser *, bool, bool *);
1228 static tree c_parser_asm_statement (c_parser *);
1229 static tree c_parser_asm_operands (c_parser *);
1230 static tree c_parser_asm_goto_operands (c_parser *);
1231 static tree c_parser_asm_clobbers (c_parser *);
1232 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1233 tree = NULL_TREE);
1234 static struct c_expr c_parser_conditional_expression (c_parser *,
1235 struct c_expr *, tree);
1236 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1237 tree);
1238 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1239 static struct c_expr c_parser_unary_expression (c_parser *);
1240 static struct c_expr c_parser_sizeof_expression (c_parser *);
1241 static struct c_expr c_parser_alignof_expression (c_parser *);
1242 static struct c_expr c_parser_postfix_expression (c_parser *);
1243 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1244 struct c_type_name *,
1245 location_t);
1246 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1247 location_t loc,
1248 struct c_expr);
1249 static tree c_parser_transaction (c_parser *, enum rid);
1250 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1251 static tree c_parser_transaction_cancel (c_parser *);
1252 static struct c_expr c_parser_expression (c_parser *);
1253 static struct c_expr c_parser_expression_conv (c_parser *);
1254 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1255 vec<tree, va_gc> **, location_t *,
1256 tree *, vec<location_t> *,
1257 unsigned int * = NULL);
1258 static void c_parser_oacc_declare (c_parser *);
1259 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1260 static void c_parser_oacc_update (c_parser *);
1261 static void c_parser_omp_construct (c_parser *, bool *);
1262 static void c_parser_omp_threadprivate (c_parser *);
1263 static void c_parser_omp_barrier (c_parser *);
1264 static void c_parser_omp_flush (c_parser *);
1265 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1266 tree, tree *, bool *);
1267 static void c_parser_omp_taskwait (c_parser *);
1268 static void c_parser_omp_taskyield (c_parser *);
1269 static void c_parser_omp_cancel (c_parser *);
1271 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1272 pragma_stmt, pragma_compound };
1273 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1274 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1275 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1276 static void c_parser_omp_end_declare_target (c_parser *);
1277 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1278 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1279 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1281 /* These Objective-C parser functions are only ever called when
1282 compiling Objective-C. */
1283 static void c_parser_objc_class_definition (c_parser *, tree);
1284 static void c_parser_objc_class_instance_variables (c_parser *);
1285 static void c_parser_objc_class_declaration (c_parser *);
1286 static void c_parser_objc_alias_declaration (c_parser *);
1287 static void c_parser_objc_protocol_definition (c_parser *, tree);
1288 static bool c_parser_objc_method_type (c_parser *);
1289 static void c_parser_objc_method_definition (c_parser *);
1290 static void c_parser_objc_methodprotolist (c_parser *);
1291 static void c_parser_objc_methodproto (c_parser *);
1292 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1293 static tree c_parser_objc_type_name (c_parser *);
1294 static tree c_parser_objc_protocol_refs (c_parser *);
1295 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1296 static void c_parser_objc_synchronized_statement (c_parser *);
1297 static tree c_parser_objc_selector (c_parser *);
1298 static tree c_parser_objc_selector_arg (c_parser *);
1299 static tree c_parser_objc_receiver (c_parser *);
1300 static tree c_parser_objc_message_args (c_parser *);
1301 static tree c_parser_objc_keywordexpr (c_parser *);
1302 static void c_parser_objc_at_property_declaration (c_parser *);
1303 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1304 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1305 static bool c_parser_objc_diagnose_bad_element_prefix
1306 (c_parser *, struct c_declspecs *);
1308 /* Cilk Plus supporting routines. */
1309 static void c_parser_cilk_simd (c_parser *, bool *);
1310 static void c_parser_cilk_for (c_parser *, tree, bool *);
1311 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1312 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1313 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1314 static void c_parser_cilk_grainsize (c_parser *, bool *);
1316 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1318 /* Parse a translation unit (C90 6.7, C99 6.9).
1320 translation-unit:
1321 external-declarations
1323 external-declarations:
1324 external-declaration
1325 external-declarations external-declaration
1327 GNU extensions:
1329 translation-unit:
1330 empty
1333 static void
1334 c_parser_translation_unit (c_parser *parser)
1336 if (c_parser_next_token_is (parser, CPP_EOF))
1338 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1339 "ISO C forbids an empty translation unit");
1341 else
1343 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1344 mark_valid_location_for_stdc_pragma (false);
1347 ggc_collect ();
1348 c_parser_external_declaration (parser);
1349 obstack_free (&parser_obstack, obstack_position);
1351 while (c_parser_next_token_is_not (parser, CPP_EOF));
1354 unsigned int i;
1355 tree decl;
1356 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1357 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1358 error ("storage size of %q+D isn%'t known", decl);
1361 /* Parse an external declaration (C90 6.7, C99 6.9).
1363 external-declaration:
1364 function-definition
1365 declaration
1367 GNU extensions:
1369 external-declaration:
1370 asm-definition
1372 __extension__ external-declaration
1374 Objective-C:
1376 external-declaration:
1377 objc-class-definition
1378 objc-class-declaration
1379 objc-alias-declaration
1380 objc-protocol-definition
1381 objc-method-definition
1382 @end
1385 static void
1386 c_parser_external_declaration (c_parser *parser)
1388 int ext;
1389 switch (c_parser_peek_token (parser)->type)
1391 case CPP_KEYWORD:
1392 switch (c_parser_peek_token (parser)->keyword)
1394 case RID_EXTENSION:
1395 ext = disable_extension_diagnostics ();
1396 c_parser_consume_token (parser);
1397 c_parser_external_declaration (parser);
1398 restore_extension_diagnostics (ext);
1399 break;
1400 case RID_ASM:
1401 c_parser_asm_definition (parser);
1402 break;
1403 case RID_AT_INTERFACE:
1404 case RID_AT_IMPLEMENTATION:
1405 gcc_assert (c_dialect_objc ());
1406 c_parser_objc_class_definition (parser, NULL_TREE);
1407 break;
1408 case RID_AT_CLASS:
1409 gcc_assert (c_dialect_objc ());
1410 c_parser_objc_class_declaration (parser);
1411 break;
1412 case RID_AT_ALIAS:
1413 gcc_assert (c_dialect_objc ());
1414 c_parser_objc_alias_declaration (parser);
1415 break;
1416 case RID_AT_PROTOCOL:
1417 gcc_assert (c_dialect_objc ());
1418 c_parser_objc_protocol_definition (parser, NULL_TREE);
1419 break;
1420 case RID_AT_PROPERTY:
1421 gcc_assert (c_dialect_objc ());
1422 c_parser_objc_at_property_declaration (parser);
1423 break;
1424 case RID_AT_SYNTHESIZE:
1425 gcc_assert (c_dialect_objc ());
1426 c_parser_objc_at_synthesize_declaration (parser);
1427 break;
1428 case RID_AT_DYNAMIC:
1429 gcc_assert (c_dialect_objc ());
1430 c_parser_objc_at_dynamic_declaration (parser);
1431 break;
1432 case RID_AT_END:
1433 gcc_assert (c_dialect_objc ());
1434 c_parser_consume_token (parser);
1435 objc_finish_implementation ();
1436 break;
1437 default:
1438 goto decl_or_fndef;
1440 break;
1441 case CPP_SEMICOLON:
1442 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1443 "ISO C does not allow extra %<;%> outside of a function");
1444 c_parser_consume_token (parser);
1445 break;
1446 case CPP_PRAGMA:
1447 mark_valid_location_for_stdc_pragma (true);
1448 c_parser_pragma (parser, pragma_external, NULL);
1449 mark_valid_location_for_stdc_pragma (false);
1450 break;
1451 case CPP_PLUS:
1452 case CPP_MINUS:
1453 if (c_dialect_objc ())
1455 c_parser_objc_method_definition (parser);
1456 break;
1458 /* Else fall through, and yield a syntax error trying to parse
1459 as a declaration or function definition. */
1460 /* FALLTHRU */
1461 default:
1462 decl_or_fndef:
1463 /* A declaration or a function definition (or, in Objective-C,
1464 an @interface or @protocol with prefix attributes). We can
1465 only tell which after parsing the declaration specifiers, if
1466 any, and the first declarator. */
1467 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1468 NULL, vNULL);
1469 break;
1473 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1474 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1476 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1477 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1478 accepted; otherwise (old-style parameter declarations) only other
1479 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1480 assertion is accepted; otherwise (old-style parameter declarations)
1481 it is not. If NESTED is true, we are inside a function or parsing
1482 old-style parameter declarations; any functions encountered are
1483 nested functions and declaration specifiers are required; otherwise
1484 we are at top level and functions are normal functions and
1485 declaration specifiers may be optional. If EMPTY_OK is true, empty
1486 declarations are OK (subject to all other constraints); otherwise
1487 (old-style parameter declarations) they are diagnosed. If
1488 START_ATTR_OK is true, the declaration specifiers may start with
1489 attributes; otherwise they may not.
1490 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1491 declaration when parsing an Objective-C foreach statement.
1492 FALLTHRU_ATTR_P is used to signal whether this function parsed
1493 "__attribute__((fallthrough));".
1495 declaration:
1496 declaration-specifiers init-declarator-list[opt] ;
1497 static_assert-declaration
1499 function-definition:
1500 declaration-specifiers[opt] declarator declaration-list[opt]
1501 compound-statement
1503 declaration-list:
1504 declaration
1505 declaration-list declaration
1507 init-declarator-list:
1508 init-declarator
1509 init-declarator-list , init-declarator
1511 init-declarator:
1512 declarator simple-asm-expr[opt] attributes[opt]
1513 declarator simple-asm-expr[opt] attributes[opt] = initializer
1515 GNU extensions:
1517 nested-function-definition:
1518 declaration-specifiers declarator declaration-list[opt]
1519 compound-statement
1521 attribute ;
1523 Objective-C:
1524 attributes objc-class-definition
1525 attributes objc-category-definition
1526 attributes objc-protocol-definition
1528 The simple-asm-expr and attributes are GNU extensions.
1530 This function does not handle __extension__; that is handled in its
1531 callers. ??? Following the old parser, __extension__ may start
1532 external declarations, declarations in functions and declarations
1533 at the start of "for" loops, but not old-style parameter
1534 declarations.
1536 C99 requires declaration specifiers in a function definition; the
1537 absence is diagnosed through the diagnosis of implicit int. In GNU
1538 C we also allow but diagnose declarations without declaration
1539 specifiers, but only at top level (elsewhere they conflict with
1540 other syntax).
1542 In Objective-C, declarations of the looping variable in a foreach
1543 statement are exceptionally terminated by 'in' (for example, 'for
1544 (NSObject *object in array) { ... }').
1546 OpenMP:
1548 declaration:
1549 threadprivate-directive
1551 GIMPLE:
1553 gimple-function-definition:
1554 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1555 declaration-list[opt] compound-statement
1557 rtl-function-definition:
1558 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1559 declaration-list[opt] compound-statement */
1561 static void
1562 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1563 bool static_assert_ok, bool empty_ok,
1564 bool nested, bool start_attr_ok,
1565 tree *objc_foreach_object_declaration,
1566 vec<c_token> omp_declare_simd_clauses,
1567 struct oacc_routine_data *oacc_routine_data,
1568 bool *fallthru_attr_p)
1570 struct c_declspecs *specs;
1571 tree prefix_attrs;
1572 tree all_prefix_attrs;
1573 bool diagnosed_no_specs = false;
1574 location_t here = c_parser_peek_token (parser)->location;
1576 if (static_assert_ok
1577 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1579 c_parser_static_assert_declaration (parser);
1580 return;
1582 specs = build_null_declspecs ();
1584 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1585 if (c_parser_peek_token (parser)->type == CPP_NAME
1586 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1587 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1588 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1589 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1591 tree name = c_parser_peek_token (parser)->value;
1593 /* Issue a warning about NAME being an unknown type name, perhaps
1594 with some kind of hint.
1595 If the user forgot a "struct" etc, suggest inserting
1596 it. Otherwise, attempt to look for misspellings. */
1597 gcc_rich_location richloc (here);
1598 if (tag_exists_p (RECORD_TYPE, name))
1600 /* This is not C++ with its implicit typedef. */
1601 richloc.add_fixit_insert_before ("struct ");
1602 error_at_rich_loc (&richloc,
1603 "unknown type name %qE;"
1604 " use %<struct%> keyword to refer to the type",
1605 name);
1607 else if (tag_exists_p (UNION_TYPE, name))
1609 richloc.add_fixit_insert_before ("union ");
1610 error_at_rich_loc (&richloc,
1611 "unknown type name %qE;"
1612 " use %<union%> keyword to refer to the type",
1613 name);
1615 else if (tag_exists_p (ENUMERAL_TYPE, name))
1617 richloc.add_fixit_insert_before ("enum ");
1618 error_at_rich_loc (&richloc,
1619 "unknown type name %qE;"
1620 " use %<enum%> keyword to refer to the type",
1621 name);
1623 else
1625 const char *hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME);
1626 if (hint)
1628 richloc.add_fixit_replace (hint);
1629 error_at_rich_loc (&richloc,
1630 "unknown type name %qE; did you mean %qs?",
1631 name, hint);
1633 else
1634 error_at (here, "unknown type name %qE", name);
1637 /* Parse declspecs normally to get a correct pointer type, but avoid
1638 a further "fails to be a type name" error. Refuse nested functions
1639 since it is not how the user likely wants us to recover. */
1640 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1641 c_parser_peek_token (parser)->keyword = RID_VOID;
1642 c_parser_peek_token (parser)->value = error_mark_node;
1643 fndef_ok = !nested;
1646 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1647 true, true, cla_nonabstract_decl);
1648 if (parser->error)
1650 c_parser_skip_to_end_of_block_or_statement (parser);
1651 return;
1653 if (nested && !specs->declspecs_seen_p)
1655 c_parser_error (parser, "expected declaration specifiers");
1656 c_parser_skip_to_end_of_block_or_statement (parser);
1657 return;
1660 finish_declspecs (specs);
1661 bool auto_type_p = specs->typespec_word == cts_auto_type;
1662 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1664 if (auto_type_p)
1665 error_at (here, "%<__auto_type%> in empty declaration");
1666 else if (specs->typespec_kind == ctsk_none
1667 && attribute_fallthrough_p (specs->attrs))
1669 if (fallthru_attr_p != NULL)
1670 *fallthru_attr_p = true;
1671 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1672 void_type_node, 0);
1673 add_stmt (fn);
1675 else if (empty_ok)
1676 shadow_tag (specs);
1677 else
1679 shadow_tag_warned (specs, 1);
1680 pedwarn (here, 0, "empty declaration");
1682 c_parser_consume_token (parser);
1683 if (oacc_routine_data)
1684 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1685 return;
1688 /* Provide better error recovery. Note that a type name here is usually
1689 better diagnosed as a redeclaration. */
1690 if (empty_ok
1691 && specs->typespec_kind == ctsk_tagdef
1692 && c_parser_next_token_starts_declspecs (parser)
1693 && !c_parser_next_token_is (parser, CPP_NAME))
1695 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1696 parser->error = false;
1697 shadow_tag_warned (specs, 1);
1698 return;
1700 else if (c_dialect_objc () && !auto_type_p)
1702 /* Prefix attributes are an error on method decls. */
1703 switch (c_parser_peek_token (parser)->type)
1705 case CPP_PLUS:
1706 case CPP_MINUS:
1707 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1708 return;
1709 if (specs->attrs)
1711 warning_at (c_parser_peek_token (parser)->location,
1712 OPT_Wattributes,
1713 "prefix attributes are ignored for methods");
1714 specs->attrs = NULL_TREE;
1716 if (fndef_ok)
1717 c_parser_objc_method_definition (parser);
1718 else
1719 c_parser_objc_methodproto (parser);
1720 return;
1721 break;
1722 default:
1723 break;
1725 /* This is where we parse 'attributes @interface ...',
1726 'attributes @implementation ...', 'attributes @protocol ...'
1727 (where attributes could be, for example, __attribute__
1728 ((deprecated)).
1730 switch (c_parser_peek_token (parser)->keyword)
1732 case RID_AT_INTERFACE:
1734 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1735 return;
1736 c_parser_objc_class_definition (parser, specs->attrs);
1737 return;
1739 break;
1740 case RID_AT_IMPLEMENTATION:
1742 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1743 return;
1744 if (specs->attrs)
1746 warning_at (c_parser_peek_token (parser)->location,
1747 OPT_Wattributes,
1748 "prefix attributes are ignored for implementations");
1749 specs->attrs = NULL_TREE;
1751 c_parser_objc_class_definition (parser, NULL_TREE);
1752 return;
1754 break;
1755 case RID_AT_PROTOCOL:
1757 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1758 return;
1759 c_parser_objc_protocol_definition (parser, specs->attrs);
1760 return;
1762 break;
1763 case RID_AT_ALIAS:
1764 case RID_AT_CLASS:
1765 case RID_AT_END:
1766 case RID_AT_PROPERTY:
1767 if (specs->attrs)
1769 c_parser_error (parser, "unexpected attribute");
1770 specs->attrs = NULL;
1772 break;
1773 default:
1774 break;
1777 else if (attribute_fallthrough_p (specs->attrs))
1778 warning_at (here, OPT_Wattributes,
1779 "%<fallthrough%> attribute not followed by %<;%>");
1781 pending_xref_error ();
1782 prefix_attrs = specs->attrs;
1783 all_prefix_attrs = prefix_attrs;
1784 specs->attrs = NULL_TREE;
1785 while (true)
1787 struct c_declarator *declarator;
1788 bool dummy = false;
1789 timevar_id_t tv;
1790 tree fnbody = NULL_TREE;
1791 /* Declaring either one or more declarators (in which case we
1792 should diagnose if there were no declaration specifiers) or a
1793 function definition (in which case the diagnostic for
1794 implicit int suffices). */
1795 declarator = c_parser_declarator (parser,
1796 specs->typespec_kind != ctsk_none,
1797 C_DTR_NORMAL, &dummy);
1798 if (declarator == NULL)
1800 if (omp_declare_simd_clauses.exists ()
1801 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1802 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1803 omp_declare_simd_clauses);
1804 if (oacc_routine_data)
1805 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1806 c_parser_skip_to_end_of_block_or_statement (parser);
1807 return;
1809 if (auto_type_p && declarator->kind != cdk_id)
1811 error_at (here,
1812 "%<__auto_type%> requires a plain identifier"
1813 " as declarator");
1814 c_parser_skip_to_end_of_block_or_statement (parser);
1815 return;
1817 if (c_parser_next_token_is (parser, CPP_EQ)
1818 || c_parser_next_token_is (parser, CPP_COMMA)
1819 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1820 || c_parser_next_token_is_keyword (parser, RID_ASM)
1821 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1822 || c_parser_next_token_is_keyword (parser, RID_IN))
1824 tree asm_name = NULL_TREE;
1825 tree postfix_attrs = NULL_TREE;
1826 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1828 diagnosed_no_specs = true;
1829 pedwarn (here, 0, "data definition has no type or storage class");
1831 /* Having seen a data definition, there cannot now be a
1832 function definition. */
1833 fndef_ok = false;
1834 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1835 asm_name = c_parser_simple_asm_expr (parser);
1836 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1838 postfix_attrs = c_parser_attributes (parser);
1839 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1841 /* This means there is an attribute specifier after
1842 the declarator in a function definition. Provide
1843 some more information for the user. */
1844 error_at (here, "attributes should be specified before the "
1845 "declarator in a function definition");
1846 c_parser_skip_to_end_of_block_or_statement (parser);
1847 return;
1850 if (c_parser_next_token_is (parser, CPP_EQ))
1852 tree d;
1853 struct c_expr init;
1854 location_t init_loc;
1855 c_parser_consume_token (parser);
1856 if (auto_type_p)
1858 init_loc = c_parser_peek_token (parser)->location;
1859 rich_location richloc (line_table, init_loc);
1860 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
1861 init = c_parser_expr_no_commas (parser, NULL);
1862 if (TREE_CODE (init.value) == COMPONENT_REF
1863 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1864 error_at (here,
1865 "%<__auto_type%> used with a bit-field"
1866 " initializer");
1867 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1868 tree init_type = TREE_TYPE (init.value);
1869 /* As with typeof, remove all qualifiers from atomic types. */
1870 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1871 init_type
1872 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1873 bool vm_type = variably_modified_type_p (init_type,
1874 NULL_TREE);
1875 if (vm_type)
1876 init.value = c_save_expr (init.value);
1877 finish_init ();
1878 specs->typespec_kind = ctsk_typeof;
1879 specs->locations[cdw_typedef] = init_loc;
1880 specs->typedef_p = true;
1881 specs->type = init_type;
1882 if (vm_type)
1884 bool maybe_const = true;
1885 tree type_expr = c_fully_fold (init.value, false,
1886 &maybe_const);
1887 specs->expr_const_operands &= maybe_const;
1888 if (specs->expr)
1889 specs->expr = build2 (COMPOUND_EXPR,
1890 TREE_TYPE (type_expr),
1891 specs->expr, type_expr);
1892 else
1893 specs->expr = type_expr;
1895 d = start_decl (declarator, specs, true,
1896 chainon (postfix_attrs, all_prefix_attrs));
1897 if (!d)
1898 d = error_mark_node;
1899 if (omp_declare_simd_clauses.exists ()
1900 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1901 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1902 omp_declare_simd_clauses);
1904 else
1906 /* The declaration of the variable is in effect while
1907 its initializer is parsed. */
1908 d = start_decl (declarator, specs, true,
1909 chainon (postfix_attrs, all_prefix_attrs));
1910 if (!d)
1911 d = error_mark_node;
1912 if (omp_declare_simd_clauses.exists ()
1913 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1914 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1915 omp_declare_simd_clauses);
1916 init_loc = c_parser_peek_token (parser)->location;
1917 rich_location richloc (line_table, init_loc);
1918 start_init (d, asm_name, global_bindings_p (), &richloc);
1919 init = c_parser_initializer (parser);
1920 finish_init ();
1922 if (oacc_routine_data)
1923 c_finish_oacc_routine (oacc_routine_data, d, false);
1924 if (d != error_mark_node)
1926 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1927 finish_decl (d, init_loc, init.value,
1928 init.original_type, asm_name);
1931 else
1933 if (auto_type_p)
1935 error_at (here,
1936 "%<__auto_type%> requires an initialized "
1937 "data declaration");
1938 c_parser_skip_to_end_of_block_or_statement (parser);
1939 return;
1941 tree d = start_decl (declarator, specs, false,
1942 chainon (postfix_attrs,
1943 all_prefix_attrs));
1944 if (omp_declare_simd_clauses.exists ()
1945 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1947 tree parms = NULL_TREE;
1948 if (d && TREE_CODE (d) == FUNCTION_DECL)
1950 struct c_declarator *ce = declarator;
1951 while (ce != NULL)
1952 if (ce->kind == cdk_function)
1954 parms = ce->u.arg_info->parms;
1955 break;
1957 else
1958 ce = ce->declarator;
1960 if (parms)
1961 temp_store_parm_decls (d, parms);
1962 c_finish_omp_declare_simd (parser, d, parms,
1963 omp_declare_simd_clauses);
1964 if (parms)
1965 temp_pop_parm_decls ();
1967 if (oacc_routine_data)
1968 c_finish_oacc_routine (oacc_routine_data, d, false);
1969 if (d)
1970 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1971 NULL_TREE, asm_name);
1973 if (c_parser_next_token_is_keyword (parser, RID_IN))
1975 if (d)
1976 *objc_foreach_object_declaration = d;
1977 else
1978 *objc_foreach_object_declaration = error_mark_node;
1981 if (c_parser_next_token_is (parser, CPP_COMMA))
1983 if (auto_type_p)
1985 error_at (here,
1986 "%<__auto_type%> may only be used with"
1987 " a single declarator");
1988 c_parser_skip_to_end_of_block_or_statement (parser);
1989 return;
1991 c_parser_consume_token (parser);
1992 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1993 all_prefix_attrs = chainon (c_parser_attributes (parser),
1994 prefix_attrs);
1995 else
1996 all_prefix_attrs = prefix_attrs;
1997 continue;
1999 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2001 c_parser_consume_token (parser);
2002 return;
2004 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2006 /* This can only happen in Objective-C: we found the
2007 'in' that terminates the declaration inside an
2008 Objective-C foreach statement. Do not consume the
2009 token, so that the caller can use it to determine
2010 that this indeed is a foreach context. */
2011 return;
2013 else
2015 c_parser_error (parser, "expected %<,%> or %<;%>");
2016 c_parser_skip_to_end_of_block_or_statement (parser);
2017 return;
2020 else if (auto_type_p)
2022 error_at (here,
2023 "%<__auto_type%> requires an initialized data declaration");
2024 c_parser_skip_to_end_of_block_or_statement (parser);
2025 return;
2027 else if (!fndef_ok)
2029 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2030 "%<asm%> or %<__attribute__%>");
2031 c_parser_skip_to_end_of_block_or_statement (parser);
2032 return;
2034 /* Function definition (nested or otherwise). */
2035 if (nested)
2037 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2038 c_push_function_context ();
2040 if (!start_function (specs, declarator, all_prefix_attrs))
2042 /* This can appear in many cases looking nothing like a
2043 function definition, so we don't give a more specific
2044 error suggesting there was one. */
2045 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2046 "or %<__attribute__%>");
2047 if (nested)
2048 c_pop_function_context ();
2049 break;
2052 if (DECL_DECLARED_INLINE_P (current_function_decl))
2053 tv = TV_PARSE_INLINE;
2054 else
2055 tv = TV_PARSE_FUNC;
2056 auto_timevar at (g_timer, tv);
2058 /* Parse old-style parameter declarations. ??? Attributes are
2059 not allowed to start declaration specifiers here because of a
2060 syntax conflict between a function declaration with attribute
2061 suffix and a function definition with an attribute prefix on
2062 first old-style parameter declaration. Following the old
2063 parser, they are not accepted on subsequent old-style
2064 parameter declarations either. However, there is no
2065 ambiguity after the first declaration, nor indeed on the
2066 first as long as we don't allow postfix attributes after a
2067 declarator with a nonempty identifier list in a definition;
2068 and postfix attributes have never been accepted here in
2069 function definitions either. */
2070 while (c_parser_next_token_is_not (parser, CPP_EOF)
2071 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2072 c_parser_declaration_or_fndef (parser, false, false, false,
2073 true, false, NULL, vNULL);
2074 store_parm_decls ();
2075 if (omp_declare_simd_clauses.exists ()
2076 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2077 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2078 omp_declare_simd_clauses);
2079 if (oacc_routine_data)
2080 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2081 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2082 = c_parser_peek_token (parser)->location;
2084 /* If the definition was marked with __GIMPLE then parse the
2085 function body as GIMPLE. */
2086 if (specs->gimple_p)
2088 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2089 bool saved = in_late_binary_op;
2090 in_late_binary_op = true;
2091 c_parser_parse_gimple_body (parser);
2092 in_late_binary_op = saved;
2094 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2095 consuming the function body. */
2096 else if (specs->rtl_p)
2098 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2100 /* Normally, store_parm_decls sets next_is_function_body,
2101 anticipating a function body. We need a push_scope/pop_scope
2102 pair to flush out this state, or subsequent function parsing
2103 will go wrong. */
2104 push_scope ();
2105 pop_scope ();
2107 finish_function ();
2108 return;
2110 else
2112 fnbody = c_parser_compound_statement (parser);
2113 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2114 fnbody = expand_array_notation_exprs (fnbody);
2116 tree fndecl = current_function_decl;
2117 if (nested)
2119 tree decl = current_function_decl;
2120 /* Mark nested functions as needing static-chain initially.
2121 lower_nested_functions will recompute it but the
2122 DECL_STATIC_CHAIN flag is also used before that happens,
2123 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2124 DECL_STATIC_CHAIN (decl) = 1;
2125 add_stmt (fnbody);
2126 finish_function ();
2127 c_pop_function_context ();
2128 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2130 else
2132 if (fnbody)
2133 add_stmt (fnbody);
2134 finish_function ();
2136 /* Get rid of the empty stmt list for GIMPLE. */
2137 if (specs->gimple_p)
2138 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2140 break;
2144 /* Parse an asm-definition (asm() outside a function body). This is a
2145 GNU extension.
2147 asm-definition:
2148 simple-asm-expr ;
2151 static void
2152 c_parser_asm_definition (c_parser *parser)
2154 tree asm_str = c_parser_simple_asm_expr (parser);
2155 if (asm_str)
2156 symtab->finalize_toplevel_asm (asm_str);
2157 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2160 /* Parse a static assertion (C11 6.7.10).
2162 static_assert-declaration:
2163 static_assert-declaration-no-semi ;
2166 static void
2167 c_parser_static_assert_declaration (c_parser *parser)
2169 c_parser_static_assert_declaration_no_semi (parser);
2170 if (parser->error
2171 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2172 c_parser_skip_to_end_of_block_or_statement (parser);
2175 /* Parse a static assertion (C11 6.7.10), without the trailing
2176 semicolon.
2178 static_assert-declaration-no-semi:
2179 _Static_assert ( constant-expression , string-literal )
2182 static void
2183 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2185 location_t assert_loc, value_loc;
2186 tree value;
2187 tree string;
2189 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2190 assert_loc = c_parser_peek_token (parser)->location;
2191 if (flag_isoc99)
2192 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2193 "ISO C99 does not support %<_Static_assert%>");
2194 else
2195 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2196 "ISO C90 does not support %<_Static_assert%>");
2197 c_parser_consume_token (parser);
2198 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2199 return;
2200 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2201 value = c_parser_expr_no_commas (parser, NULL).value;
2202 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2203 parser->lex_untranslated_string = true;
2204 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2206 parser->lex_untranslated_string = false;
2207 return;
2209 switch (c_parser_peek_token (parser)->type)
2211 case CPP_STRING:
2212 case CPP_STRING16:
2213 case CPP_STRING32:
2214 case CPP_WSTRING:
2215 case CPP_UTF8STRING:
2216 string = c_parser_peek_token (parser)->value;
2217 c_parser_consume_token (parser);
2218 parser->lex_untranslated_string = false;
2219 break;
2220 default:
2221 c_parser_error (parser, "expected string literal");
2222 parser->lex_untranslated_string = false;
2223 return;
2225 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2227 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2229 error_at (value_loc, "expression in static assertion is not an integer");
2230 return;
2232 if (TREE_CODE (value) != INTEGER_CST)
2234 value = c_fully_fold (value, false, NULL);
2235 /* Strip no-op conversions. */
2236 STRIP_TYPE_NOPS (value);
2237 if (TREE_CODE (value) == INTEGER_CST)
2238 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2239 "is not an integer constant expression");
2241 if (TREE_CODE (value) != INTEGER_CST)
2243 error_at (value_loc, "expression in static assertion is not constant");
2244 return;
2246 constant_expression_warning (value);
2247 if (integer_zerop (value))
2248 error_at (assert_loc, "static assertion failed: %E", string);
2251 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2252 6.7), adding them to SPECS (which may already include some).
2253 Storage class specifiers are accepted iff SCSPEC_OK; type
2254 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2255 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2256 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2258 declaration-specifiers:
2259 storage-class-specifier declaration-specifiers[opt]
2260 type-specifier declaration-specifiers[opt]
2261 type-qualifier declaration-specifiers[opt]
2262 function-specifier declaration-specifiers[opt]
2263 alignment-specifier declaration-specifiers[opt]
2265 Function specifiers (inline) are from C99, and are currently
2266 handled as storage class specifiers, as is __thread. Alignment
2267 specifiers are from C11.
2269 C90 6.5.1, C99 6.7.1:
2270 storage-class-specifier:
2271 typedef
2272 extern
2273 static
2274 auto
2275 register
2276 _Thread_local
2278 (_Thread_local is new in C11.)
2280 C99 6.7.4:
2281 function-specifier:
2282 inline
2283 _Noreturn
2285 (_Noreturn is new in C11.)
2287 C90 6.5.2, C99 6.7.2:
2288 type-specifier:
2289 void
2290 char
2291 short
2293 long
2294 float
2295 double
2296 signed
2297 unsigned
2298 _Bool
2299 _Complex
2300 [_Imaginary removed in C99 TC2]
2301 struct-or-union-specifier
2302 enum-specifier
2303 typedef-name
2304 atomic-type-specifier
2306 (_Bool and _Complex are new in C99.)
2307 (atomic-type-specifier is new in C11.)
2309 C90 6.5.3, C99 6.7.3:
2311 type-qualifier:
2312 const
2313 restrict
2314 volatile
2315 address-space-qualifier
2316 _Atomic
2318 (restrict is new in C99.)
2319 (_Atomic is new in C11.)
2321 GNU extensions:
2323 declaration-specifiers:
2324 attributes declaration-specifiers[opt]
2326 type-qualifier:
2327 address-space
2329 address-space:
2330 identifier recognized by the target
2332 storage-class-specifier:
2333 __thread
2335 type-specifier:
2336 typeof-specifier
2337 __auto_type
2338 __intN
2339 _Decimal32
2340 _Decimal64
2341 _Decimal128
2342 _Fract
2343 _Accum
2344 _Sat
2346 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2347 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2349 atomic-type-specifier
2350 _Atomic ( type-name )
2352 Objective-C:
2354 type-specifier:
2355 class-name objc-protocol-refs[opt]
2356 typedef-name objc-protocol-refs
2357 objc-protocol-refs
2360 void
2361 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2362 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2363 bool alignspec_ok, bool auto_type_ok,
2364 enum c_lookahead_kind la)
2366 bool attrs_ok = start_attr_ok;
2367 bool seen_type = specs->typespec_kind != ctsk_none;
2369 if (!typespec_ok)
2370 gcc_assert (la == cla_prefer_id);
2372 while (c_parser_next_token_is (parser, CPP_NAME)
2373 || c_parser_next_token_is (parser, CPP_KEYWORD)
2374 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2376 struct c_typespec t;
2377 tree attrs;
2378 tree align;
2379 location_t loc = c_parser_peek_token (parser)->location;
2381 /* If we cannot accept a type, exit if the next token must start
2382 one. Also, if we already have seen a tagged definition,
2383 a typename would be an error anyway and likely the user
2384 has simply forgotten a semicolon, so we exit. */
2385 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2386 && c_parser_next_tokens_start_typename (parser, la)
2387 && !c_parser_next_token_is_qualifier (parser))
2388 break;
2390 if (c_parser_next_token_is (parser, CPP_NAME))
2392 c_token *name_token = c_parser_peek_token (parser);
2393 tree value = name_token->value;
2394 c_id_kind kind = name_token->id_kind;
2396 if (kind == C_ID_ADDRSPACE)
2398 addr_space_t as
2399 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2400 declspecs_add_addrspace (name_token->location, specs, as);
2401 c_parser_consume_token (parser);
2402 attrs_ok = true;
2403 continue;
2406 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2408 /* If we cannot accept a type, and the next token must start one,
2409 exit. Do the same if we already have seen a tagged definition,
2410 since it would be an error anyway and likely the user has simply
2411 forgotten a semicolon. */
2412 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2413 break;
2415 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2416 a C_ID_CLASSNAME. */
2417 c_parser_consume_token (parser);
2418 seen_type = true;
2419 attrs_ok = true;
2420 if (kind == C_ID_ID)
2422 error_at (loc, "unknown type name %qE", value);
2423 t.kind = ctsk_typedef;
2424 t.spec = error_mark_node;
2426 else if (kind == C_ID_TYPENAME
2427 && (!c_dialect_objc ()
2428 || c_parser_next_token_is_not (parser, CPP_LESS)))
2430 t.kind = ctsk_typedef;
2431 /* For a typedef name, record the meaning, not the name.
2432 In case of 'foo foo, bar;'. */
2433 t.spec = lookup_name (value);
2435 else
2437 tree proto = NULL_TREE;
2438 gcc_assert (c_dialect_objc ());
2439 t.kind = ctsk_objc;
2440 if (c_parser_next_token_is (parser, CPP_LESS))
2441 proto = c_parser_objc_protocol_refs (parser);
2442 t.spec = objc_get_protocol_qualified_type (value, proto);
2444 t.expr = NULL_TREE;
2445 t.expr_const_operands = true;
2446 declspecs_add_type (name_token->location, specs, t);
2447 continue;
2449 if (c_parser_next_token_is (parser, CPP_LESS))
2451 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2452 nisse@lysator.liu.se. */
2453 tree proto;
2454 gcc_assert (c_dialect_objc ());
2455 if (!typespec_ok || seen_type)
2456 break;
2457 proto = c_parser_objc_protocol_refs (parser);
2458 t.kind = ctsk_objc;
2459 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2460 t.expr = NULL_TREE;
2461 t.expr_const_operands = true;
2462 declspecs_add_type (loc, specs, t);
2463 continue;
2465 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2466 switch (c_parser_peek_token (parser)->keyword)
2468 case RID_STATIC:
2469 case RID_EXTERN:
2470 case RID_REGISTER:
2471 case RID_TYPEDEF:
2472 case RID_INLINE:
2473 case RID_NORETURN:
2474 case RID_AUTO:
2475 case RID_THREAD:
2476 if (!scspec_ok)
2477 goto out;
2478 attrs_ok = true;
2479 /* TODO: Distinguish between function specifiers (inline, noreturn)
2480 and storage class specifiers, either here or in
2481 declspecs_add_scspec. */
2482 declspecs_add_scspec (loc, specs,
2483 c_parser_peek_token (parser)->value);
2484 c_parser_consume_token (parser);
2485 break;
2486 case RID_AUTO_TYPE:
2487 if (!auto_type_ok)
2488 goto out;
2489 /* Fall through. */
2490 case RID_UNSIGNED:
2491 case RID_LONG:
2492 case RID_SHORT:
2493 case RID_SIGNED:
2494 case RID_COMPLEX:
2495 case RID_INT:
2496 case RID_CHAR:
2497 case RID_FLOAT:
2498 case RID_DOUBLE:
2499 case RID_VOID:
2500 case RID_DFLOAT32:
2501 case RID_DFLOAT64:
2502 case RID_DFLOAT128:
2503 CASE_RID_FLOATN_NX:
2504 case RID_BOOL:
2505 case RID_FRACT:
2506 case RID_ACCUM:
2507 case RID_SAT:
2508 case RID_INT_N_0:
2509 case RID_INT_N_1:
2510 case RID_INT_N_2:
2511 case RID_INT_N_3:
2512 if (!typespec_ok)
2513 goto out;
2514 attrs_ok = true;
2515 seen_type = true;
2516 if (c_dialect_objc ())
2517 parser->objc_need_raw_identifier = true;
2518 t.kind = ctsk_resword;
2519 t.spec = c_parser_peek_token (parser)->value;
2520 t.expr = NULL_TREE;
2521 t.expr_const_operands = true;
2522 declspecs_add_type (loc, specs, t);
2523 c_parser_consume_token (parser);
2524 break;
2525 case RID_ENUM:
2526 if (!typespec_ok)
2527 goto out;
2528 attrs_ok = true;
2529 seen_type = true;
2530 t = c_parser_enum_specifier (parser);
2531 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2532 declspecs_add_type (loc, specs, t);
2533 break;
2534 case RID_STRUCT:
2535 case RID_UNION:
2536 if (!typespec_ok)
2537 goto out;
2538 attrs_ok = true;
2539 seen_type = true;
2540 t = c_parser_struct_or_union_specifier (parser);
2541 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2542 declspecs_add_type (loc, specs, t);
2543 break;
2544 case RID_TYPEOF:
2545 /* ??? The old parser rejected typeof after other type
2546 specifiers, but is a syntax error the best way of
2547 handling this? */
2548 if (!typespec_ok || seen_type)
2549 goto out;
2550 attrs_ok = true;
2551 seen_type = true;
2552 t = c_parser_typeof_specifier (parser);
2553 declspecs_add_type (loc, specs, t);
2554 break;
2555 case RID_ATOMIC:
2556 /* C parser handling of Objective-C constructs needs
2557 checking for correct lvalue-to-rvalue conversions, and
2558 the code in build_modify_expr handling various
2559 Objective-C cases, and that in build_unary_op handling
2560 Objective-C cases for increment / decrement, also needs
2561 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2562 and objc_types_are_equivalent may also need updates. */
2563 if (c_dialect_objc ())
2564 sorry ("%<_Atomic%> in Objective-C");
2565 if (flag_isoc99)
2566 pedwarn_c99 (loc, OPT_Wpedantic,
2567 "ISO C99 does not support the %<_Atomic%> qualifier");
2568 else
2569 pedwarn_c99 (loc, OPT_Wpedantic,
2570 "ISO C90 does not support the %<_Atomic%> qualifier");
2571 attrs_ok = true;
2572 tree value;
2573 value = c_parser_peek_token (parser)->value;
2574 c_parser_consume_token (parser);
2575 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2577 /* _Atomic ( type-name ). */
2578 seen_type = true;
2579 c_parser_consume_token (parser);
2580 struct c_type_name *type = c_parser_type_name (parser);
2581 t.kind = ctsk_typeof;
2582 t.spec = error_mark_node;
2583 t.expr = NULL_TREE;
2584 t.expr_const_operands = true;
2585 if (type != NULL)
2586 t.spec = groktypename (type, &t.expr,
2587 &t.expr_const_operands);
2588 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2589 "expected %<)%>");
2590 if (t.spec != error_mark_node)
2592 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2593 error_at (loc, "%<_Atomic%>-qualified array type");
2594 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2595 error_at (loc, "%<_Atomic%>-qualified function type");
2596 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2597 error_at (loc, "%<_Atomic%> applied to a qualified type");
2598 else
2599 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2601 declspecs_add_type (loc, specs, t);
2603 else
2604 declspecs_add_qual (loc, specs, value);
2605 break;
2606 case RID_CONST:
2607 case RID_VOLATILE:
2608 case RID_RESTRICT:
2609 attrs_ok = true;
2610 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2611 c_parser_consume_token (parser);
2612 break;
2613 case RID_ATTRIBUTE:
2614 if (!attrs_ok)
2615 goto out;
2616 attrs = c_parser_attributes (parser);
2617 declspecs_add_attrs (loc, specs, attrs);
2618 break;
2619 case RID_ALIGNAS:
2620 if (!alignspec_ok)
2621 goto out;
2622 align = c_parser_alignas_specifier (parser);
2623 declspecs_add_alignas (loc, specs, align);
2624 break;
2625 case RID_GIMPLE:
2626 if (! flag_gimple)
2627 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2628 c_parser_consume_token (parser);
2629 specs->gimple_p = true;
2630 specs->locations[cdw_gimple] = loc;
2631 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2632 break;
2633 case RID_RTL:
2634 c_parser_consume_token (parser);
2635 specs->rtl_p = true;
2636 specs->locations[cdw_rtl] = loc;
2637 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2638 break;
2639 default:
2640 goto out;
2643 out: ;
2646 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2648 enum-specifier:
2649 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2650 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2651 enum attributes[opt] identifier
2653 The form with trailing comma is new in C99. The forms with
2654 attributes are GNU extensions. In GNU C, we accept any expression
2655 without commas in the syntax (assignment expressions, not just
2656 conditional expressions); assignment expressions will be diagnosed
2657 as non-constant.
2659 enumerator-list:
2660 enumerator
2661 enumerator-list , enumerator
2663 enumerator:
2664 enumeration-constant
2665 enumeration-constant = constant-expression
2667 GNU Extensions:
2669 enumerator:
2670 enumeration-constant attributes[opt]
2671 enumeration-constant attributes[opt] = constant-expression
2675 static struct c_typespec
2676 c_parser_enum_specifier (c_parser *parser)
2678 struct c_typespec ret;
2679 tree attrs;
2680 tree ident = NULL_TREE;
2681 location_t enum_loc;
2682 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2683 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2684 enum_loc = c_parser_peek_token (parser)->location;
2685 c_parser_consume_token (parser);
2686 attrs = c_parser_attributes (parser);
2687 enum_loc = c_parser_peek_token (parser)->location;
2688 /* Set the location in case we create a decl now. */
2689 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2690 if (c_parser_next_token_is (parser, CPP_NAME))
2692 ident = c_parser_peek_token (parser)->value;
2693 ident_loc = c_parser_peek_token (parser)->location;
2694 enum_loc = ident_loc;
2695 c_parser_consume_token (parser);
2697 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2699 /* Parse an enum definition. */
2700 struct c_enum_contents the_enum;
2701 tree type;
2702 tree postfix_attrs;
2703 /* We chain the enumerators in reverse order, then put them in
2704 forward order at the end. */
2705 tree values;
2706 timevar_push (TV_PARSE_ENUM);
2707 type = start_enum (enum_loc, &the_enum, ident);
2708 values = NULL_TREE;
2709 c_parser_consume_token (parser);
2710 while (true)
2712 tree enum_id;
2713 tree enum_value;
2714 tree enum_decl;
2715 bool seen_comma;
2716 c_token *token;
2717 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2718 location_t decl_loc, value_loc;
2719 if (c_parser_next_token_is_not (parser, CPP_NAME))
2721 /* Give a nicer error for "enum {}". */
2722 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2723 && !parser->error)
2725 error_at (c_parser_peek_token (parser)->location,
2726 "empty enum is invalid");
2727 parser->error = true;
2729 else
2730 c_parser_error (parser, "expected identifier");
2731 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2732 values = error_mark_node;
2733 break;
2735 token = c_parser_peek_token (parser);
2736 enum_id = token->value;
2737 /* Set the location in case we create a decl now. */
2738 c_parser_set_source_position_from_token (token);
2739 decl_loc = value_loc = token->location;
2740 c_parser_consume_token (parser);
2741 /* Parse any specified attributes. */
2742 tree enum_attrs = c_parser_attributes (parser);
2743 if (c_parser_next_token_is (parser, CPP_EQ))
2745 c_parser_consume_token (parser);
2746 value_loc = c_parser_peek_token (parser)->location;
2747 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2749 else
2750 enum_value = NULL_TREE;
2751 enum_decl = build_enumerator (decl_loc, value_loc,
2752 &the_enum, enum_id, enum_value);
2753 if (enum_attrs)
2754 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2755 TREE_CHAIN (enum_decl) = values;
2756 values = enum_decl;
2757 seen_comma = false;
2758 if (c_parser_next_token_is (parser, CPP_COMMA))
2760 comma_loc = c_parser_peek_token (parser)->location;
2761 seen_comma = true;
2762 c_parser_consume_token (parser);
2764 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2766 if (seen_comma)
2767 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2768 "comma at end of enumerator list");
2769 c_parser_consume_token (parser);
2770 break;
2772 if (!seen_comma)
2774 c_parser_error (parser, "expected %<,%> or %<}%>");
2775 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2776 values = error_mark_node;
2777 break;
2780 postfix_attrs = c_parser_attributes (parser);
2781 ret.spec = finish_enum (type, nreverse (values),
2782 chainon (attrs, postfix_attrs));
2783 ret.kind = ctsk_tagdef;
2784 ret.expr = NULL_TREE;
2785 ret.expr_const_operands = true;
2786 timevar_pop (TV_PARSE_ENUM);
2787 return ret;
2789 else if (!ident)
2791 c_parser_error (parser, "expected %<{%>");
2792 ret.spec = error_mark_node;
2793 ret.kind = ctsk_tagref;
2794 ret.expr = NULL_TREE;
2795 ret.expr_const_operands = true;
2796 return ret;
2798 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2799 /* In ISO C, enumerated types can be referred to only if already
2800 defined. */
2801 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2803 gcc_assert (ident);
2804 pedwarn (enum_loc, OPT_Wpedantic,
2805 "ISO C forbids forward references to %<enum%> types");
2807 return ret;
2810 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2812 struct-or-union-specifier:
2813 struct-or-union attributes[opt] identifier[opt]
2814 { struct-contents } attributes[opt]
2815 struct-or-union attributes[opt] identifier
2817 struct-contents:
2818 struct-declaration-list
2820 struct-declaration-list:
2821 struct-declaration ;
2822 struct-declaration-list struct-declaration ;
2824 GNU extensions:
2826 struct-contents:
2827 empty
2828 struct-declaration
2829 struct-declaration-list struct-declaration
2831 struct-declaration-list:
2832 struct-declaration-list ;
2835 (Note that in the syntax here, unlike that in ISO C, the semicolons
2836 are included here rather than in struct-declaration, in order to
2837 describe the syntax with extra semicolons and missing semicolon at
2838 end.)
2840 Objective-C:
2842 struct-declaration-list:
2843 @defs ( class-name )
2845 (Note this does not include a trailing semicolon, but can be
2846 followed by further declarations, and gets a pedwarn-if-pedantic
2847 when followed by a semicolon.) */
2849 static struct c_typespec
2850 c_parser_struct_or_union_specifier (c_parser *parser)
2852 struct c_typespec ret;
2853 tree attrs;
2854 tree ident = NULL_TREE;
2855 location_t struct_loc;
2856 location_t ident_loc = UNKNOWN_LOCATION;
2857 enum tree_code code;
2858 switch (c_parser_peek_token (parser)->keyword)
2860 case RID_STRUCT:
2861 code = RECORD_TYPE;
2862 break;
2863 case RID_UNION:
2864 code = UNION_TYPE;
2865 break;
2866 default:
2867 gcc_unreachable ();
2869 struct_loc = c_parser_peek_token (parser)->location;
2870 c_parser_consume_token (parser);
2871 attrs = c_parser_attributes (parser);
2873 /* Set the location in case we create a decl now. */
2874 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2876 if (c_parser_next_token_is (parser, CPP_NAME))
2878 ident = c_parser_peek_token (parser)->value;
2879 ident_loc = c_parser_peek_token (parser)->location;
2880 struct_loc = ident_loc;
2881 c_parser_consume_token (parser);
2883 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2885 /* Parse a struct or union definition. Start the scope of the
2886 tag before parsing components. */
2887 struct c_struct_parse_info *struct_info;
2888 tree type = start_struct (struct_loc, code, ident, &struct_info);
2889 tree postfix_attrs;
2890 /* We chain the components in reverse order, then put them in
2891 forward order at the end. Each struct-declaration may
2892 declare multiple components (comma-separated), so we must use
2893 chainon to join them, although when parsing each
2894 struct-declaration we can use TREE_CHAIN directly.
2896 The theory behind all this is that there will be more
2897 semicolon separated fields than comma separated fields, and
2898 so we'll be minimizing the number of node traversals required
2899 by chainon. */
2900 tree contents;
2901 timevar_push (TV_PARSE_STRUCT);
2902 contents = NULL_TREE;
2903 c_parser_consume_token (parser);
2904 /* Handle the Objective-C @defs construct,
2905 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2906 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2908 tree name;
2909 gcc_assert (c_dialect_objc ());
2910 c_parser_consume_token (parser);
2911 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2912 goto end_at_defs;
2913 if (c_parser_next_token_is (parser, CPP_NAME)
2914 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2916 name = c_parser_peek_token (parser)->value;
2917 c_parser_consume_token (parser);
2919 else
2921 c_parser_error (parser, "expected class name");
2922 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2923 goto end_at_defs;
2925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2926 "expected %<)%>");
2927 contents = nreverse (objc_get_class_ivars (name));
2929 end_at_defs:
2930 /* Parse the struct-declarations and semicolons. Problems with
2931 semicolons are diagnosed here; empty structures are diagnosed
2932 elsewhere. */
2933 while (true)
2935 tree decls;
2936 /* Parse any stray semicolon. */
2937 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2939 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2940 "extra semicolon in struct or union specified");
2941 c_parser_consume_token (parser);
2942 continue;
2944 /* Stop if at the end of the struct or union contents. */
2945 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2947 c_parser_consume_token (parser);
2948 break;
2950 /* Accept #pragmas at struct scope. */
2951 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2953 c_parser_pragma (parser, pragma_struct, NULL);
2954 continue;
2956 /* Parse some comma-separated declarations, but not the
2957 trailing semicolon if any. */
2958 decls = c_parser_struct_declaration (parser);
2959 contents = chainon (decls, contents);
2960 /* If no semicolon follows, either we have a parse error or
2961 are at the end of the struct or union and should
2962 pedwarn. */
2963 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2964 c_parser_consume_token (parser);
2965 else
2967 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2968 pedwarn (c_parser_peek_token (parser)->location, 0,
2969 "no semicolon at end of struct or union");
2970 else if (parser->error
2971 || !c_parser_next_token_starts_declspecs (parser))
2973 c_parser_error (parser, "expected %<;%>");
2974 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2975 break;
2978 /* If we come here, we have already emitted an error
2979 for an expected `;', identifier or `(', and we also
2980 recovered already. Go on with the next field. */
2983 postfix_attrs = c_parser_attributes (parser);
2984 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2985 chainon (attrs, postfix_attrs), struct_info);
2986 ret.kind = ctsk_tagdef;
2987 ret.expr = NULL_TREE;
2988 ret.expr_const_operands = true;
2989 timevar_pop (TV_PARSE_STRUCT);
2990 return ret;
2992 else if (!ident)
2994 c_parser_error (parser, "expected %<{%>");
2995 ret.spec = error_mark_node;
2996 ret.kind = ctsk_tagref;
2997 ret.expr = NULL_TREE;
2998 ret.expr_const_operands = true;
2999 return ret;
3001 ret = parser_xref_tag (ident_loc, code, ident);
3002 return ret;
3005 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
3006 the trailing semicolon.
3008 struct-declaration:
3009 specifier-qualifier-list struct-declarator-list
3010 static_assert-declaration-no-semi
3012 specifier-qualifier-list:
3013 type-specifier specifier-qualifier-list[opt]
3014 type-qualifier specifier-qualifier-list[opt]
3015 attributes specifier-qualifier-list[opt]
3017 struct-declarator-list:
3018 struct-declarator
3019 struct-declarator-list , attributes[opt] struct-declarator
3021 struct-declarator:
3022 declarator attributes[opt]
3023 declarator[opt] : constant-expression attributes[opt]
3025 GNU extensions:
3027 struct-declaration:
3028 __extension__ struct-declaration
3029 specifier-qualifier-list
3031 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3032 of attributes where shown is a GNU extension. In GNU C, we accept
3033 any expression without commas in the syntax (assignment
3034 expressions, not just conditional expressions); assignment
3035 expressions will be diagnosed as non-constant. */
3037 static tree
3038 c_parser_struct_declaration (c_parser *parser)
3040 struct c_declspecs *specs;
3041 tree prefix_attrs;
3042 tree all_prefix_attrs;
3043 tree decls;
3044 location_t decl_loc;
3045 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3047 int ext;
3048 tree decl;
3049 ext = disable_extension_diagnostics ();
3050 c_parser_consume_token (parser);
3051 decl = c_parser_struct_declaration (parser);
3052 restore_extension_diagnostics (ext);
3053 return decl;
3055 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3057 c_parser_static_assert_declaration_no_semi (parser);
3058 return NULL_TREE;
3060 specs = build_null_declspecs ();
3061 decl_loc = c_parser_peek_token (parser)->location;
3062 /* Strictly by the standard, we shouldn't allow _Alignas here,
3063 but it appears to have been intended to allow it there, so
3064 we're keeping it as it is until WG14 reaches a conclusion
3065 of N1731.
3066 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3067 c_parser_declspecs (parser, specs, false, true, true,
3068 true, false, cla_nonabstract_decl);
3069 if (parser->error)
3070 return NULL_TREE;
3071 if (!specs->declspecs_seen_p)
3073 c_parser_error (parser, "expected specifier-qualifier-list");
3074 return NULL_TREE;
3076 finish_declspecs (specs);
3077 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3078 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3080 tree ret;
3081 if (specs->typespec_kind == ctsk_none)
3083 pedwarn (decl_loc, OPT_Wpedantic,
3084 "ISO C forbids member declarations with no members");
3085 shadow_tag_warned (specs, pedantic);
3086 ret = NULL_TREE;
3088 else
3090 /* Support for unnamed structs or unions as members of
3091 structs or unions (which is [a] useful and [b] supports
3092 MS P-SDK). */
3093 tree attrs = NULL;
3095 ret = grokfield (c_parser_peek_token (parser)->location,
3096 build_id_declarator (NULL_TREE), specs,
3097 NULL_TREE, &attrs);
3098 if (ret)
3099 decl_attributes (&ret, attrs, 0);
3101 return ret;
3104 /* Provide better error recovery. Note that a type name here is valid,
3105 and will be treated as a field name. */
3106 if (specs->typespec_kind == ctsk_tagdef
3107 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3108 && c_parser_next_token_starts_declspecs (parser)
3109 && !c_parser_next_token_is (parser, CPP_NAME))
3111 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3112 parser->error = false;
3113 return NULL_TREE;
3116 pending_xref_error ();
3117 prefix_attrs = specs->attrs;
3118 all_prefix_attrs = prefix_attrs;
3119 specs->attrs = NULL_TREE;
3120 decls = NULL_TREE;
3121 while (true)
3123 /* Declaring one or more declarators or un-named bit-fields. */
3124 struct c_declarator *declarator;
3125 bool dummy = false;
3126 if (c_parser_next_token_is (parser, CPP_COLON))
3127 declarator = build_id_declarator (NULL_TREE);
3128 else
3129 declarator = c_parser_declarator (parser,
3130 specs->typespec_kind != ctsk_none,
3131 C_DTR_NORMAL, &dummy);
3132 if (declarator == NULL)
3134 c_parser_skip_to_end_of_block_or_statement (parser);
3135 break;
3137 if (c_parser_next_token_is (parser, CPP_COLON)
3138 || c_parser_next_token_is (parser, CPP_COMMA)
3139 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3140 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3141 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3143 tree postfix_attrs = NULL_TREE;
3144 tree width = NULL_TREE;
3145 tree d;
3146 if (c_parser_next_token_is (parser, CPP_COLON))
3148 c_parser_consume_token (parser);
3149 width = c_parser_expr_no_commas (parser, NULL).value;
3151 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3152 postfix_attrs = c_parser_attributes (parser);
3153 d = grokfield (c_parser_peek_token (parser)->location,
3154 declarator, specs, width, &all_prefix_attrs);
3155 decl_attributes (&d, chainon (postfix_attrs,
3156 all_prefix_attrs), 0);
3157 DECL_CHAIN (d) = decls;
3158 decls = d;
3159 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3160 all_prefix_attrs = chainon (c_parser_attributes (parser),
3161 prefix_attrs);
3162 else
3163 all_prefix_attrs = prefix_attrs;
3164 if (c_parser_next_token_is (parser, CPP_COMMA))
3165 c_parser_consume_token (parser);
3166 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3167 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3169 /* Semicolon consumed in caller. */
3170 break;
3172 else
3174 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3175 break;
3178 else
3180 c_parser_error (parser,
3181 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3182 "%<__attribute__%>");
3183 break;
3186 return decls;
3189 /* Parse a typeof specifier (a GNU extension).
3191 typeof-specifier:
3192 typeof ( expression )
3193 typeof ( type-name )
3196 static struct c_typespec
3197 c_parser_typeof_specifier (c_parser *parser)
3199 struct c_typespec ret;
3200 ret.kind = ctsk_typeof;
3201 ret.spec = error_mark_node;
3202 ret.expr = NULL_TREE;
3203 ret.expr_const_operands = true;
3204 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3205 c_parser_consume_token (parser);
3206 c_inhibit_evaluation_warnings++;
3207 in_typeof++;
3208 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3210 c_inhibit_evaluation_warnings--;
3211 in_typeof--;
3212 return ret;
3214 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3216 struct c_type_name *type = c_parser_type_name (parser);
3217 c_inhibit_evaluation_warnings--;
3218 in_typeof--;
3219 if (type != NULL)
3221 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3222 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3225 else
3227 bool was_vm;
3228 location_t here = c_parser_peek_token (parser)->location;
3229 struct c_expr expr = c_parser_expression (parser);
3230 c_inhibit_evaluation_warnings--;
3231 in_typeof--;
3232 if (TREE_CODE (expr.value) == COMPONENT_REF
3233 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3234 error_at (here, "%<typeof%> applied to a bit-field");
3235 mark_exp_read (expr.value);
3236 ret.spec = TREE_TYPE (expr.value);
3237 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3238 /* This is returned with the type so that when the type is
3239 evaluated, this can be evaluated. */
3240 if (was_vm)
3241 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3242 pop_maybe_used (was_vm);
3243 /* For use in macros such as those in <stdatomic.h>, remove all
3244 qualifiers from atomic types. (const can be an issue for more macros
3245 using typeof than just the <stdatomic.h> ones.) */
3246 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3247 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3249 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3250 return ret;
3253 /* Parse an alignment-specifier.
3255 C11 6.7.5:
3257 alignment-specifier:
3258 _Alignas ( type-name )
3259 _Alignas ( constant-expression )
3262 static tree
3263 c_parser_alignas_specifier (c_parser * parser)
3265 tree ret = error_mark_node;
3266 location_t loc = c_parser_peek_token (parser)->location;
3267 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3268 c_parser_consume_token (parser);
3269 if (flag_isoc99)
3270 pedwarn_c99 (loc, OPT_Wpedantic,
3271 "ISO C99 does not support %<_Alignas%>");
3272 else
3273 pedwarn_c99 (loc, OPT_Wpedantic,
3274 "ISO C90 does not support %<_Alignas%>");
3275 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3276 return ret;
3277 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3279 struct c_type_name *type = c_parser_type_name (parser);
3280 if (type != NULL)
3281 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3282 false, true, 1);
3284 else
3285 ret = c_parser_expr_no_commas (parser, NULL).value;
3286 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3287 return ret;
3290 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3291 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3292 be redeclared; otherwise it may not. KIND indicates which kind of
3293 declarator is wanted. Returns a valid declarator except in the
3294 case of a syntax error in which case NULL is returned. *SEEN_ID is
3295 set to true if an identifier being declared is seen; this is used
3296 to diagnose bad forms of abstract array declarators and to
3297 determine whether an identifier list is syntactically permitted.
3299 declarator:
3300 pointer[opt] direct-declarator
3302 direct-declarator:
3303 identifier
3304 ( attributes[opt] declarator )
3305 direct-declarator array-declarator
3306 direct-declarator ( parameter-type-list )
3307 direct-declarator ( identifier-list[opt] )
3309 pointer:
3310 * type-qualifier-list[opt]
3311 * type-qualifier-list[opt] pointer
3313 type-qualifier-list:
3314 type-qualifier
3315 attributes
3316 type-qualifier-list type-qualifier
3317 type-qualifier-list attributes
3319 array-declarator:
3320 [ type-qualifier-list[opt] assignment-expression[opt] ]
3321 [ static type-qualifier-list[opt] assignment-expression ]
3322 [ type-qualifier-list static assignment-expression ]
3323 [ type-qualifier-list[opt] * ]
3325 parameter-type-list:
3326 parameter-list
3327 parameter-list , ...
3329 parameter-list:
3330 parameter-declaration
3331 parameter-list , parameter-declaration
3333 parameter-declaration:
3334 declaration-specifiers declarator attributes[opt]
3335 declaration-specifiers abstract-declarator[opt] attributes[opt]
3337 identifier-list:
3338 identifier
3339 identifier-list , identifier
3341 abstract-declarator:
3342 pointer
3343 pointer[opt] direct-abstract-declarator
3345 direct-abstract-declarator:
3346 ( attributes[opt] abstract-declarator )
3347 direct-abstract-declarator[opt] array-declarator
3348 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3350 GNU extensions:
3352 direct-declarator:
3353 direct-declarator ( parameter-forward-declarations
3354 parameter-type-list[opt] )
3356 direct-abstract-declarator:
3357 direct-abstract-declarator[opt] ( parameter-forward-declarations
3358 parameter-type-list[opt] )
3360 parameter-forward-declarations:
3361 parameter-list ;
3362 parameter-forward-declarations parameter-list ;
3364 The uses of attributes shown above are GNU extensions.
3366 Some forms of array declarator are not included in C99 in the
3367 syntax for abstract declarators; these are disallowed elsewhere.
3368 This may be a defect (DR#289).
3370 This function also accepts an omitted abstract declarator as being
3371 an abstract declarator, although not part of the formal syntax. */
3373 struct c_declarator *
3374 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3375 bool *seen_id)
3377 /* Parse any initial pointer part. */
3378 if (c_parser_next_token_is (parser, CPP_MULT))
3380 struct c_declspecs *quals_attrs = build_null_declspecs ();
3381 struct c_declarator *inner;
3382 c_parser_consume_token (parser);
3383 c_parser_declspecs (parser, quals_attrs, false, false, true,
3384 false, false, cla_prefer_id);
3385 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3386 if (inner == NULL)
3387 return NULL;
3388 else
3389 return make_pointer_declarator (quals_attrs, inner);
3391 /* Now we have a direct declarator, direct abstract declarator or
3392 nothing (which counts as a direct abstract declarator here). */
3393 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3396 /* Parse a direct declarator or direct abstract declarator; arguments
3397 as c_parser_declarator. */
3399 static struct c_declarator *
3400 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3401 bool *seen_id)
3403 /* The direct declarator must start with an identifier (possibly
3404 omitted) or a parenthesized declarator (possibly abstract). In
3405 an ordinary declarator, initial parentheses must start a
3406 parenthesized declarator. In an abstract declarator or parameter
3407 declarator, they could start a parenthesized declarator or a
3408 parameter list. To tell which, the open parenthesis and any
3409 following attributes must be read. If a declaration specifier
3410 follows, then it is a parameter list; if the specifier is a
3411 typedef name, there might be an ambiguity about redeclaring it,
3412 which is resolved in the direction of treating it as a typedef
3413 name. If a close parenthesis follows, it is also an empty
3414 parameter list, as the syntax does not permit empty abstract
3415 declarators. Otherwise, it is a parenthesized declarator (in
3416 which case the analysis may be repeated inside it, recursively).
3418 ??? There is an ambiguity in a parameter declaration "int
3419 (__attribute__((foo)) x)", where x is not a typedef name: it
3420 could be an abstract declarator for a function, or declare x with
3421 parentheses. The proper resolution of this ambiguity needs
3422 documenting. At present we follow an accident of the old
3423 parser's implementation, whereby the first parameter must have
3424 some declaration specifiers other than just attributes. Thus as
3425 a parameter declaration it is treated as a parenthesized
3426 parameter named x, and as an abstract declarator it is
3427 rejected.
3429 ??? Also following the old parser, attributes inside an empty
3430 parameter list are ignored, making it a list not yielding a
3431 prototype, rather than giving an error or making it have one
3432 parameter with implicit type int.
3434 ??? Also following the old parser, typedef names may be
3435 redeclared in declarators, but not Objective-C class names. */
3437 if (kind != C_DTR_ABSTRACT
3438 && c_parser_next_token_is (parser, CPP_NAME)
3439 && ((type_seen_p
3440 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3441 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3442 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3444 struct c_declarator *inner
3445 = build_id_declarator (c_parser_peek_token (parser)->value);
3446 *seen_id = true;
3447 inner->id_loc = c_parser_peek_token (parser)->location;
3448 c_parser_consume_token (parser);
3449 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3452 if (kind != C_DTR_NORMAL
3453 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3455 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3456 inner->id_loc = c_parser_peek_token (parser)->location;
3457 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3460 /* Either we are at the end of an abstract declarator, or we have
3461 parentheses. */
3463 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3465 tree attrs;
3466 struct c_declarator *inner;
3467 c_parser_consume_token (parser);
3468 attrs = c_parser_attributes (parser);
3469 if (kind != C_DTR_NORMAL
3470 && (c_parser_next_token_starts_declspecs (parser)
3471 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3473 struct c_arg_info *args
3474 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3475 attrs);
3476 if (args == NULL)
3477 return NULL;
3478 else
3480 inner
3481 = build_function_declarator (args,
3482 build_id_declarator (NULL_TREE));
3483 return c_parser_direct_declarator_inner (parser, *seen_id,
3484 inner);
3487 /* A parenthesized declarator. */
3488 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3489 if (inner != NULL && attrs != NULL)
3490 inner = build_attrs_declarator (attrs, inner);
3491 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3493 c_parser_consume_token (parser);
3494 if (inner == NULL)
3495 return NULL;
3496 else
3497 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3499 else
3501 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3502 "expected %<)%>");
3503 return NULL;
3506 else
3508 if (kind == C_DTR_NORMAL)
3510 c_parser_error (parser, "expected identifier or %<(%>");
3511 return NULL;
3513 else
3514 return build_id_declarator (NULL_TREE);
3518 /* Parse part of a direct declarator or direct abstract declarator,
3519 given that some (in INNER) has already been parsed; ID_PRESENT is
3520 true if an identifier is present, false for an abstract
3521 declarator. */
3523 static struct c_declarator *
3524 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3525 struct c_declarator *inner)
3527 /* Parse a sequence of array declarators and parameter lists. */
3528 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3530 location_t brace_loc = c_parser_peek_token (parser)->location;
3531 struct c_declarator *declarator;
3532 struct c_declspecs *quals_attrs = build_null_declspecs ();
3533 bool static_seen;
3534 bool star_seen;
3535 struct c_expr dimen;
3536 dimen.value = NULL_TREE;
3537 dimen.original_code = ERROR_MARK;
3538 dimen.original_type = NULL_TREE;
3539 c_parser_consume_token (parser);
3540 c_parser_declspecs (parser, quals_attrs, false, false, true,
3541 false, false, cla_prefer_id);
3542 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3543 if (static_seen)
3544 c_parser_consume_token (parser);
3545 if (static_seen && !quals_attrs->declspecs_seen_p)
3546 c_parser_declspecs (parser, quals_attrs, false, false, true,
3547 false, false, cla_prefer_id);
3548 if (!quals_attrs->declspecs_seen_p)
3549 quals_attrs = NULL;
3550 /* If "static" is present, there must be an array dimension.
3551 Otherwise, there may be a dimension, "*", or no
3552 dimension. */
3553 if (static_seen)
3555 star_seen = false;
3556 dimen = c_parser_expr_no_commas (parser, NULL);
3558 else
3560 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3562 dimen.value = NULL_TREE;
3563 star_seen = false;
3565 else if (flag_cilkplus
3566 && c_parser_next_token_is (parser, CPP_COLON))
3568 dimen.value = error_mark_node;
3569 star_seen = false;
3570 error_at (c_parser_peek_token (parser)->location,
3571 "array notations cannot be used in declaration");
3572 c_parser_consume_token (parser);
3574 else if (c_parser_next_token_is (parser, CPP_MULT))
3576 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3578 dimen.value = NULL_TREE;
3579 star_seen = true;
3580 c_parser_consume_token (parser);
3582 else
3584 star_seen = false;
3585 dimen = c_parser_expr_no_commas (parser, NULL);
3588 else
3590 star_seen = false;
3591 dimen = c_parser_expr_no_commas (parser, NULL);
3594 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3595 c_parser_consume_token (parser);
3596 else if (flag_cilkplus
3597 && c_parser_next_token_is (parser, CPP_COLON))
3599 error_at (c_parser_peek_token (parser)->location,
3600 "array notations cannot be used in declaration");
3601 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3602 return NULL;
3604 else
3606 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3607 "expected %<]%>");
3608 return NULL;
3610 if (dimen.value)
3611 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3612 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3613 static_seen, star_seen);
3614 if (declarator == NULL)
3615 return NULL;
3616 inner = set_array_declarator_inner (declarator, inner);
3617 return c_parser_direct_declarator_inner (parser, id_present, inner);
3619 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3621 tree attrs;
3622 struct c_arg_info *args;
3623 c_parser_consume_token (parser);
3624 attrs = c_parser_attributes (parser);
3625 args = c_parser_parms_declarator (parser, id_present, attrs);
3626 if (args == NULL)
3627 return NULL;
3628 else
3630 inner = build_function_declarator (args, inner);
3631 return c_parser_direct_declarator_inner (parser, id_present, inner);
3634 return inner;
3637 /* Parse a parameter list or identifier list, including the closing
3638 parenthesis but not the opening one. ATTRS are the attributes at
3639 the start of the list. ID_LIST_OK is true if an identifier list is
3640 acceptable; such a list must not have attributes at the start. */
3642 static struct c_arg_info *
3643 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3645 push_scope ();
3646 declare_parm_level ();
3647 /* If the list starts with an identifier, it is an identifier list.
3648 Otherwise, it is either a prototype list or an empty list. */
3649 if (id_list_ok
3650 && !attrs
3651 && c_parser_next_token_is (parser, CPP_NAME)
3652 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3654 /* Look ahead to detect typos in type names. */
3655 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3656 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3657 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3658 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3659 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3661 tree list = NULL_TREE, *nextp = &list;
3662 while (c_parser_next_token_is (parser, CPP_NAME)
3663 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3665 *nextp = build_tree_list (NULL_TREE,
3666 c_parser_peek_token (parser)->value);
3667 nextp = & TREE_CHAIN (*nextp);
3668 c_parser_consume_token (parser);
3669 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3670 break;
3671 c_parser_consume_token (parser);
3672 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3674 c_parser_error (parser, "expected identifier");
3675 break;
3678 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3680 struct c_arg_info *ret = build_arg_info ();
3681 ret->types = list;
3682 c_parser_consume_token (parser);
3683 pop_scope ();
3684 return ret;
3686 else
3688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3689 "expected %<)%>");
3690 pop_scope ();
3691 return NULL;
3694 else
3696 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3697 NULL);
3698 pop_scope ();
3699 return ret;
3703 /* Parse a parameter list (possibly empty), including the closing
3704 parenthesis but not the opening one. ATTRS are the attributes at
3705 the start of the list. EXPR is NULL or an expression that needs to
3706 be evaluated for the side effects of array size expressions in the
3707 parameters. */
3709 static struct c_arg_info *
3710 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3712 bool bad_parm = false;
3714 /* ??? Following the old parser, forward parameter declarations may
3715 use abstract declarators, and if no real parameter declarations
3716 follow the forward declarations then this is not diagnosed. Also
3717 note as above that attributes are ignored as the only contents of
3718 the parentheses, or as the only contents after forward
3719 declarations. */
3720 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3722 struct c_arg_info *ret = build_arg_info ();
3723 c_parser_consume_token (parser);
3724 return ret;
3726 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3728 struct c_arg_info *ret = build_arg_info ();
3730 if (flag_allow_parameterless_variadic_functions)
3732 /* F (...) is allowed. */
3733 ret->types = NULL_TREE;
3735 else
3737 /* Suppress -Wold-style-definition for this case. */
3738 ret->types = error_mark_node;
3739 error_at (c_parser_peek_token (parser)->location,
3740 "ISO C requires a named argument before %<...%>");
3742 c_parser_consume_token (parser);
3743 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3745 c_parser_consume_token (parser);
3746 return ret;
3748 else
3750 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3751 "expected %<)%>");
3752 return NULL;
3755 /* Nonempty list of parameters, either terminated with semicolon
3756 (forward declarations; recurse) or with close parenthesis (normal
3757 function) or with ", ... )" (variadic function). */
3758 while (true)
3760 /* Parse a parameter. */
3761 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3762 attrs = NULL_TREE;
3763 if (parm == NULL)
3764 bad_parm = true;
3765 else
3766 push_parm_decl (parm, &expr);
3767 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3769 tree new_attrs;
3770 c_parser_consume_token (parser);
3771 mark_forward_parm_decls ();
3772 new_attrs = c_parser_attributes (parser);
3773 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3775 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3777 c_parser_consume_token (parser);
3778 if (bad_parm)
3779 return NULL;
3780 else
3781 return get_parm_info (false, expr);
3783 if (!c_parser_require (parser, CPP_COMMA,
3784 "expected %<;%>, %<,%> or %<)%>"))
3786 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3787 return NULL;
3789 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3791 c_parser_consume_token (parser);
3792 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3794 c_parser_consume_token (parser);
3795 if (bad_parm)
3796 return NULL;
3797 else
3798 return get_parm_info (true, expr);
3800 else
3802 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3803 "expected %<)%>");
3804 return NULL;
3810 /* Parse a parameter declaration. ATTRS are the attributes at the
3811 start of the declaration if it is the first parameter. */
3813 static struct c_parm *
3814 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3816 struct c_declspecs *specs;
3817 struct c_declarator *declarator;
3818 tree prefix_attrs;
3819 tree postfix_attrs = NULL_TREE;
3820 bool dummy = false;
3822 /* Accept #pragmas between parameter declarations. */
3823 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3824 c_parser_pragma (parser, pragma_param, NULL);
3826 if (!c_parser_next_token_starts_declspecs (parser))
3828 c_token *token = c_parser_peek_token (parser);
3829 if (parser->error)
3830 return NULL;
3831 c_parser_set_source_position_from_token (token);
3832 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3834 const char *hint = lookup_name_fuzzy (token->value,
3835 FUZZY_LOOKUP_TYPENAME);
3836 if (hint)
3838 gcc_rich_location richloc (token->location);
3839 richloc.add_fixit_replace (hint);
3840 error_at_rich_loc (&richloc,
3841 "unknown type name %qE; did you mean %qs?",
3842 token->value, hint);
3844 else
3845 error_at (token->location, "unknown type name %qE", token->value);
3846 parser->error = true;
3848 /* ??? In some Objective-C cases '...' isn't applicable so there
3849 should be a different message. */
3850 else
3851 c_parser_error (parser,
3852 "expected declaration specifiers or %<...%>");
3853 c_parser_skip_to_end_of_parameter (parser);
3854 return NULL;
3856 specs = build_null_declspecs ();
3857 if (attrs)
3859 declspecs_add_attrs (input_location, specs, attrs);
3860 attrs = NULL_TREE;
3862 c_parser_declspecs (parser, specs, true, true, true, true, false,
3863 cla_nonabstract_decl);
3864 finish_declspecs (specs);
3865 pending_xref_error ();
3866 prefix_attrs = specs->attrs;
3867 specs->attrs = NULL_TREE;
3868 declarator = c_parser_declarator (parser,
3869 specs->typespec_kind != ctsk_none,
3870 C_DTR_PARM, &dummy);
3871 if (declarator == NULL)
3873 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3874 return NULL;
3876 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3877 postfix_attrs = c_parser_attributes (parser);
3878 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3879 declarator);
3882 /* Parse a string literal in an asm expression. It should not be
3883 translated, and wide string literals are an error although
3884 permitted by the syntax. This is a GNU extension.
3886 asm-string-literal:
3887 string-literal
3889 ??? At present, following the old parser, the caller needs to have
3890 set lex_untranslated_string to 1. It would be better to follow the
3891 C++ parser rather than using this kludge. */
3893 static tree
3894 c_parser_asm_string_literal (c_parser *parser)
3896 tree str;
3897 int save_flag = warn_overlength_strings;
3898 warn_overlength_strings = 0;
3899 if (c_parser_next_token_is (parser, CPP_STRING))
3901 str = c_parser_peek_token (parser)->value;
3902 c_parser_consume_token (parser);
3904 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3906 error_at (c_parser_peek_token (parser)->location,
3907 "wide string literal in %<asm%>");
3908 str = build_string (1, "");
3909 c_parser_consume_token (parser);
3911 else
3913 c_parser_error (parser, "expected string literal");
3914 str = NULL_TREE;
3916 warn_overlength_strings = save_flag;
3917 return str;
3920 /* Parse a simple asm expression. This is used in restricted
3921 contexts, where a full expression with inputs and outputs does not
3922 make sense. This is a GNU extension.
3924 simple-asm-expr:
3925 asm ( asm-string-literal )
3928 static tree
3929 c_parser_simple_asm_expr (c_parser *parser)
3931 tree str;
3932 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3933 /* ??? Follow the C++ parser rather than using the
3934 lex_untranslated_string kludge. */
3935 parser->lex_untranslated_string = true;
3936 c_parser_consume_token (parser);
3937 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3939 parser->lex_untranslated_string = false;
3940 return NULL_TREE;
3942 str = c_parser_asm_string_literal (parser);
3943 parser->lex_untranslated_string = false;
3944 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3947 return NULL_TREE;
3949 return str;
3952 static tree
3953 c_parser_attribute_any_word (c_parser *parser)
3955 tree attr_name = NULL_TREE;
3957 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3959 /* ??? See comment above about what keywords are accepted here. */
3960 bool ok;
3961 switch (c_parser_peek_token (parser)->keyword)
3963 case RID_STATIC:
3964 case RID_UNSIGNED:
3965 case RID_LONG:
3966 case RID_CONST:
3967 case RID_EXTERN:
3968 case RID_REGISTER:
3969 case RID_TYPEDEF:
3970 case RID_SHORT:
3971 case RID_INLINE:
3972 case RID_NORETURN:
3973 case RID_VOLATILE:
3974 case RID_SIGNED:
3975 case RID_AUTO:
3976 case RID_RESTRICT:
3977 case RID_COMPLEX:
3978 case RID_THREAD:
3979 case RID_INT:
3980 case RID_CHAR:
3981 case RID_FLOAT:
3982 case RID_DOUBLE:
3983 case RID_VOID:
3984 case RID_DFLOAT32:
3985 case RID_DFLOAT64:
3986 case RID_DFLOAT128:
3987 CASE_RID_FLOATN_NX:
3988 case RID_BOOL:
3989 case RID_FRACT:
3990 case RID_ACCUM:
3991 case RID_SAT:
3992 case RID_TRANSACTION_ATOMIC:
3993 case RID_TRANSACTION_CANCEL:
3994 case RID_ATOMIC:
3995 case RID_AUTO_TYPE:
3996 case RID_INT_N_0:
3997 case RID_INT_N_1:
3998 case RID_INT_N_2:
3999 case RID_INT_N_3:
4000 ok = true;
4001 break;
4002 default:
4003 ok = false;
4004 break;
4006 if (!ok)
4007 return NULL_TREE;
4009 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4010 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4012 else if (c_parser_next_token_is (parser, CPP_NAME))
4013 attr_name = c_parser_peek_token (parser)->value;
4015 return attr_name;
4018 #define CILK_SIMD_FN_CLAUSE_MASK \
4019 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
4020 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
4021 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
4022 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
4023 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
4025 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
4026 VEC_TOKEN is the "vector" token that is replaced with "simd" and
4027 pushed into the token list.
4028 Syntax:
4029 vector
4030 vector (<vector attributes>). */
4032 static void
4033 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
4035 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4037 int paren_scope = 0;
4038 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4039 /* Consume the "vector" token. */
4040 c_parser_consume_token (parser);
4042 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4044 c_parser_consume_token (parser);
4045 paren_scope++;
4047 while (paren_scope > 0)
4049 c_token *token = c_parser_peek_token (parser);
4050 if (token->type == CPP_OPEN_PAREN)
4051 paren_scope++;
4052 else if (token->type == CPP_CLOSE_PAREN)
4053 paren_scope--;
4054 /* Do not push the last ')' since we are not pushing the '('. */
4055 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4056 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4057 c_parser_consume_token (parser);
4060 /* Since we are converting an attribute to a pragma, we need to end the
4061 attribute with PRAGMA_EOL. */
4062 c_token eol_token;
4063 memset (&eol_token, 0, sizeof (eol_token));
4064 eol_token.type = CPP_PRAGMA_EOL;
4065 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4068 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4070 static void
4071 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4073 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4075 /* c_parser_attributes is called in several places, so if these EOF
4076 tokens are already inserted, then don't do them again. */
4077 if (last_token.type == CPP_EOF)
4078 return;
4080 /* Two CPP_EOF token are added as a safety net since the normal C
4081 front-end has two token look-ahead. */
4082 c_token eof_token;
4083 eof_token.type = CPP_EOF;
4084 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4085 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4088 /* Parse (possibly empty) attributes. This is a GNU extension.
4090 attributes:
4091 empty
4092 attributes attribute
4094 attribute:
4095 __attribute__ ( ( attribute-list ) )
4097 attribute-list:
4098 attrib
4099 attribute_list , attrib
4101 attrib:
4102 empty
4103 any-word
4104 any-word ( identifier )
4105 any-word ( identifier , nonempty-expr-list )
4106 any-word ( expr-list )
4108 where the "identifier" must not be declared as a type, and
4109 "any-word" may be any identifier (including one declared as a
4110 type), a reserved word storage class specifier, type specifier or
4111 type qualifier. ??? This still leaves out most reserved keywords
4112 (following the old parser), shouldn't we include them, and why not
4113 allow identifiers declared as types to start the arguments? */
4115 static tree
4116 c_parser_attributes (c_parser *parser)
4118 tree attrs = NULL_TREE;
4119 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4121 /* ??? Follow the C++ parser rather than using the
4122 lex_untranslated_string kludge. */
4123 parser->lex_untranslated_string = true;
4124 /* Consume the `__attribute__' keyword. */
4125 c_parser_consume_token (parser);
4126 /* Look for the two `(' tokens. */
4127 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4129 parser->lex_untranslated_string = false;
4130 return attrs;
4132 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4134 parser->lex_untranslated_string = false;
4135 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4136 return attrs;
4138 /* Parse the attribute list. */
4139 while (c_parser_next_token_is (parser, CPP_COMMA)
4140 || c_parser_next_token_is (parser, CPP_NAME)
4141 || c_parser_next_token_is (parser, CPP_KEYWORD))
4143 tree attr, attr_name, attr_args;
4144 vec<tree, va_gc> *expr_list;
4145 if (c_parser_next_token_is (parser, CPP_COMMA))
4147 c_parser_consume_token (parser);
4148 continue;
4151 attr_name = c_parser_attribute_any_word (parser);
4152 if (attr_name == NULL)
4153 break;
4154 if (is_cilkplus_vector_p (attr_name))
4156 c_token *v_token = c_parser_peek_token (parser);
4157 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4158 /* If the next token isn't a comma, we're done. */
4159 if (!c_parser_next_token_is (parser, CPP_COMMA))
4160 break;
4161 continue;
4163 c_parser_consume_token (parser);
4164 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4166 attr = build_tree_list (attr_name, NULL_TREE);
4167 /* Add this attribute to the list. */
4168 attrs = chainon (attrs, attr);
4169 /* If the next token isn't a comma, we're done. */
4170 if (!c_parser_next_token_is (parser, CPP_COMMA))
4171 break;
4172 continue;
4174 c_parser_consume_token (parser);
4175 /* Parse the attribute contents. If they start with an
4176 identifier which is followed by a comma or close
4177 parenthesis, then the arguments start with that
4178 identifier; otherwise they are an expression list.
4179 In objective-c the identifier may be a classname. */
4180 if (c_parser_next_token_is (parser, CPP_NAME)
4181 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4182 || (c_dialect_objc ()
4183 && c_parser_peek_token (parser)->id_kind
4184 == C_ID_CLASSNAME))
4185 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4186 || (c_parser_peek_2nd_token (parser)->type
4187 == CPP_CLOSE_PAREN))
4188 && (attribute_takes_identifier_p (attr_name)
4189 || (c_dialect_objc ()
4190 && c_parser_peek_token (parser)->id_kind
4191 == C_ID_CLASSNAME)))
4193 tree arg1 = c_parser_peek_token (parser)->value;
4194 c_parser_consume_token (parser);
4195 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4196 attr_args = build_tree_list (NULL_TREE, arg1);
4197 else
4199 tree tree_list;
4200 c_parser_consume_token (parser);
4201 expr_list = c_parser_expr_list (parser, false, true,
4202 NULL, NULL, NULL, NULL);
4203 tree_list = build_tree_list_vec (expr_list);
4204 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4205 release_tree_vector (expr_list);
4208 else
4210 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4211 attr_args = NULL_TREE;
4212 else
4214 expr_list = c_parser_expr_list (parser, false, true,
4215 NULL, NULL, NULL, NULL);
4216 attr_args = build_tree_list_vec (expr_list);
4217 release_tree_vector (expr_list);
4220 attr = build_tree_list (attr_name, attr_args);
4221 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4222 c_parser_consume_token (parser);
4223 else
4225 parser->lex_untranslated_string = false;
4226 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4227 "expected %<)%>");
4228 return attrs;
4230 /* Add this attribute to the list. */
4231 attrs = chainon (attrs, attr);
4232 /* If the next token isn't a comma, we're done. */
4233 if (!c_parser_next_token_is (parser, CPP_COMMA))
4234 break;
4236 /* Look for the two `)' tokens. */
4237 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4238 c_parser_consume_token (parser);
4239 else
4241 parser->lex_untranslated_string = false;
4242 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4243 "expected %<)%>");
4244 return attrs;
4246 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4247 c_parser_consume_token (parser);
4248 else
4250 parser->lex_untranslated_string = false;
4251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4252 "expected %<)%>");
4253 return attrs;
4255 parser->lex_untranslated_string = false;
4258 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4259 c_finish_cilk_simd_fn_tokens (parser);
4260 return attrs;
4263 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4265 type-name:
4266 specifier-qualifier-list abstract-declarator[opt]
4269 struct c_type_name *
4270 c_parser_type_name (c_parser *parser)
4272 struct c_declspecs *specs = build_null_declspecs ();
4273 struct c_declarator *declarator;
4274 struct c_type_name *ret;
4275 bool dummy = false;
4276 c_parser_declspecs (parser, specs, false, true, true, false, false,
4277 cla_prefer_type);
4278 if (!specs->declspecs_seen_p)
4280 c_parser_error (parser, "expected specifier-qualifier-list");
4281 return NULL;
4283 if (specs->type != error_mark_node)
4285 pending_xref_error ();
4286 finish_declspecs (specs);
4288 declarator = c_parser_declarator (parser,
4289 specs->typespec_kind != ctsk_none,
4290 C_DTR_ABSTRACT, &dummy);
4291 if (declarator == NULL)
4292 return NULL;
4293 ret = XOBNEW (&parser_obstack, struct c_type_name);
4294 ret->specs = specs;
4295 ret->declarator = declarator;
4296 return ret;
4299 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4301 initializer:
4302 assignment-expression
4303 { initializer-list }
4304 { initializer-list , }
4306 initializer-list:
4307 designation[opt] initializer
4308 initializer-list , designation[opt] initializer
4310 designation:
4311 designator-list =
4313 designator-list:
4314 designator
4315 designator-list designator
4317 designator:
4318 array-designator
4319 . identifier
4321 array-designator:
4322 [ constant-expression ]
4324 GNU extensions:
4326 initializer:
4329 designation:
4330 array-designator
4331 identifier :
4333 array-designator:
4334 [ constant-expression ... constant-expression ]
4336 Any expression without commas is accepted in the syntax for the
4337 constant-expressions, with non-constant expressions rejected later.
4339 This function is only used for top-level initializers; for nested
4340 ones, see c_parser_initval. */
4342 static struct c_expr
4343 c_parser_initializer (c_parser *parser)
4345 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4346 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4347 else
4349 struct c_expr ret;
4350 location_t loc = c_parser_peek_token (parser)->location;
4351 ret = c_parser_expr_no_commas (parser, NULL);
4352 if (TREE_CODE (ret.value) != STRING_CST
4353 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4354 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4355 return ret;
4359 /* The location of the last comma within the current initializer list,
4360 or UNKNOWN_LOCATION if not within one. */
4362 location_t last_init_list_comma;
4364 /* Parse a braced initializer list. TYPE is the type specified for a
4365 compound literal, and NULL_TREE for other initializers and for
4366 nested braced lists. NESTED_P is true for nested braced lists,
4367 false for the list of a compound literal or the list that is the
4368 top-level initializer in a declaration. */
4370 static struct c_expr
4371 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4372 struct obstack *outer_obstack)
4374 struct c_expr ret;
4375 struct obstack braced_init_obstack;
4376 location_t brace_loc = c_parser_peek_token (parser)->location;
4377 gcc_obstack_init (&braced_init_obstack);
4378 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4379 c_parser_consume_token (parser);
4380 if (nested_p)
4382 finish_implicit_inits (brace_loc, outer_obstack);
4383 push_init_level (brace_loc, 0, &braced_init_obstack);
4385 else
4386 really_start_incremental_init (type);
4387 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4389 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4391 else
4393 /* Parse a non-empty initializer list, possibly with a trailing
4394 comma. */
4395 while (true)
4397 c_parser_initelt (parser, &braced_init_obstack);
4398 if (parser->error)
4399 break;
4400 if (c_parser_next_token_is (parser, CPP_COMMA))
4402 last_init_list_comma = c_parser_peek_token (parser)->location;
4403 c_parser_consume_token (parser);
4405 else
4406 break;
4407 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4408 break;
4411 c_token *next_tok = c_parser_peek_token (parser);
4412 if (next_tok->type != CPP_CLOSE_BRACE)
4414 ret.value = error_mark_node;
4415 ret.original_code = ERROR_MARK;
4416 ret.original_type = NULL;
4417 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4418 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4419 obstack_free (&braced_init_obstack, NULL);
4420 return ret;
4422 location_t close_loc = next_tok->location;
4423 c_parser_consume_token (parser);
4424 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4425 obstack_free (&braced_init_obstack, NULL);
4426 set_c_expr_source_range (&ret, brace_loc, close_loc);
4427 return ret;
4430 /* Parse a nested initializer, including designators. */
4432 static void
4433 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4435 /* Parse any designator or designator list. A single array
4436 designator may have the subsequent "=" omitted in GNU C, but a
4437 longer list or a structure member designator may not. */
4438 if (c_parser_next_token_is (parser, CPP_NAME)
4439 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4441 /* Old-style structure member designator. */
4442 set_init_label (c_parser_peek_token (parser)->location,
4443 c_parser_peek_token (parser)->value,
4444 c_parser_peek_token (parser)->location,
4445 braced_init_obstack);
4446 /* Use the colon as the error location. */
4447 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4448 "obsolete use of designated initializer with %<:%>");
4449 c_parser_consume_token (parser);
4450 c_parser_consume_token (parser);
4452 else
4454 /* des_seen is 0 if there have been no designators, 1 if there
4455 has been a single array designator and 2 otherwise. */
4456 int des_seen = 0;
4457 /* Location of a designator. */
4458 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4459 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4460 || c_parser_next_token_is (parser, CPP_DOT))
4462 int des_prev = des_seen;
4463 if (!des_seen)
4464 des_loc = c_parser_peek_token (parser)->location;
4465 if (des_seen < 2)
4466 des_seen++;
4467 if (c_parser_next_token_is (parser, CPP_DOT))
4469 des_seen = 2;
4470 c_parser_consume_token (parser);
4471 if (c_parser_next_token_is (parser, CPP_NAME))
4473 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4474 c_parser_peek_token (parser)->location,
4475 braced_init_obstack);
4476 c_parser_consume_token (parser);
4478 else
4480 struct c_expr init;
4481 init.value = error_mark_node;
4482 init.original_code = ERROR_MARK;
4483 init.original_type = NULL;
4484 c_parser_error (parser, "expected identifier");
4485 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4486 process_init_element (input_location, init, false,
4487 braced_init_obstack);
4488 return;
4491 else
4493 tree first, second;
4494 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4495 location_t array_index_loc = UNKNOWN_LOCATION;
4496 /* ??? Following the old parser, [ objc-receiver
4497 objc-message-args ] is accepted as an initializer,
4498 being distinguished from a designator by what follows
4499 the first assignment expression inside the square
4500 brackets, but after a first array designator a
4501 subsequent square bracket is for Objective-C taken to
4502 start an expression, using the obsolete form of
4503 designated initializer without '=', rather than
4504 possibly being a second level of designation: in LALR
4505 terms, the '[' is shifted rather than reducing
4506 designator to designator-list. */
4507 if (des_prev == 1 && c_dialect_objc ())
4509 des_seen = des_prev;
4510 break;
4512 if (des_prev == 0 && c_dialect_objc ())
4514 /* This might be an array designator or an
4515 Objective-C message expression. If the former,
4516 continue parsing here; if the latter, parse the
4517 remainder of the initializer given the starting
4518 primary-expression. ??? It might make sense to
4519 distinguish when des_prev == 1 as well; see
4520 previous comment. */
4521 tree rec, args;
4522 struct c_expr mexpr;
4523 c_parser_consume_token (parser);
4524 if (c_parser_peek_token (parser)->type == CPP_NAME
4525 && ((c_parser_peek_token (parser)->id_kind
4526 == C_ID_TYPENAME)
4527 || (c_parser_peek_token (parser)->id_kind
4528 == C_ID_CLASSNAME)))
4530 /* Type name receiver. */
4531 tree id = c_parser_peek_token (parser)->value;
4532 c_parser_consume_token (parser);
4533 rec = objc_get_class_reference (id);
4534 goto parse_message_args;
4536 first = c_parser_expr_no_commas (parser, NULL).value;
4537 mark_exp_read (first);
4538 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4539 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4540 goto array_desig_after_first;
4541 /* Expression receiver. So far only one part
4542 without commas has been parsed; there might be
4543 more of the expression. */
4544 rec = first;
4545 while (c_parser_next_token_is (parser, CPP_COMMA))
4547 struct c_expr next;
4548 location_t comma_loc, exp_loc;
4549 comma_loc = c_parser_peek_token (parser)->location;
4550 c_parser_consume_token (parser);
4551 exp_loc = c_parser_peek_token (parser)->location;
4552 next = c_parser_expr_no_commas (parser, NULL);
4553 next = convert_lvalue_to_rvalue (exp_loc, next,
4554 true, true);
4555 rec = build_compound_expr (comma_loc, rec, next.value);
4557 parse_message_args:
4558 /* Now parse the objc-message-args. */
4559 args = c_parser_objc_message_args (parser);
4560 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4561 "expected %<]%>");
4562 mexpr.value
4563 = objc_build_message_expr (rec, args);
4564 mexpr.original_code = ERROR_MARK;
4565 mexpr.original_type = NULL;
4566 /* Now parse and process the remainder of the
4567 initializer, starting with this message
4568 expression as a primary-expression. */
4569 c_parser_initval (parser, &mexpr, braced_init_obstack);
4570 return;
4572 c_parser_consume_token (parser);
4573 array_index_loc = c_parser_peek_token (parser)->location;
4574 first = c_parser_expr_no_commas (parser, NULL).value;
4575 mark_exp_read (first);
4576 array_desig_after_first:
4577 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4579 ellipsis_loc = c_parser_peek_token (parser)->location;
4580 c_parser_consume_token (parser);
4581 second = c_parser_expr_no_commas (parser, NULL).value;
4582 mark_exp_read (second);
4584 else
4585 second = NULL_TREE;
4586 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4588 c_parser_consume_token (parser);
4589 set_init_index (array_index_loc, first, second,
4590 braced_init_obstack);
4591 if (second)
4592 pedwarn (ellipsis_loc, OPT_Wpedantic,
4593 "ISO C forbids specifying range of elements to initialize");
4595 else
4596 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4597 "expected %<]%>");
4600 if (des_seen >= 1)
4602 if (c_parser_next_token_is (parser, CPP_EQ))
4604 pedwarn_c90 (des_loc, OPT_Wpedantic,
4605 "ISO C90 forbids specifying subobject "
4606 "to initialize");
4607 c_parser_consume_token (parser);
4609 else
4611 if (des_seen == 1)
4612 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4613 "obsolete use of designated initializer without %<=%>");
4614 else
4616 struct c_expr init;
4617 init.value = error_mark_node;
4618 init.original_code = ERROR_MARK;
4619 init.original_type = NULL;
4620 c_parser_error (parser, "expected %<=%>");
4621 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4622 process_init_element (input_location, init, false,
4623 braced_init_obstack);
4624 return;
4629 c_parser_initval (parser, NULL, braced_init_obstack);
4632 /* Parse a nested initializer; as c_parser_initializer but parses
4633 initializers within braced lists, after any designators have been
4634 applied. If AFTER is not NULL then it is an Objective-C message
4635 expression which is the primary-expression starting the
4636 initializer. */
4638 static void
4639 c_parser_initval (c_parser *parser, struct c_expr *after,
4640 struct obstack * braced_init_obstack)
4642 struct c_expr init;
4643 gcc_assert (!after || c_dialect_objc ());
4644 location_t loc = c_parser_peek_token (parser)->location;
4646 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4647 init = c_parser_braced_init (parser, NULL_TREE, true,
4648 braced_init_obstack);
4649 else
4651 init = c_parser_expr_no_commas (parser, after);
4652 if (init.value != NULL_TREE
4653 && TREE_CODE (init.value) != STRING_CST
4654 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4655 init = convert_lvalue_to_rvalue (loc, init, true, true);
4657 process_init_element (loc, init, false, braced_init_obstack);
4660 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4661 C99 6.8.2).
4663 compound-statement:
4664 { block-item-list[opt] }
4665 { label-declarations block-item-list }
4667 block-item-list:
4668 block-item
4669 block-item-list block-item
4671 block-item:
4672 nested-declaration
4673 statement
4675 nested-declaration:
4676 declaration
4678 GNU extensions:
4680 compound-statement:
4681 { label-declarations block-item-list }
4683 nested-declaration:
4684 __extension__ nested-declaration
4685 nested-function-definition
4687 label-declarations:
4688 label-declaration
4689 label-declarations label-declaration
4691 label-declaration:
4692 __label__ identifier-list ;
4694 Allowing the mixing of declarations and code is new in C99. The
4695 GNU syntax also permits (not shown above) labels at the end of
4696 compound statements, which yield an error. We don't allow labels
4697 on declarations; this might seem like a natural extension, but
4698 there would be a conflict between attributes on the label and
4699 prefix attributes on the declaration. ??? The syntax follows the
4700 old parser in requiring something after label declarations.
4701 Although they are erroneous if the labels declared aren't defined,
4702 is it useful for the syntax to be this way?
4704 OpenACC:
4706 block-item:
4707 openacc-directive
4709 openacc-directive:
4710 update-directive
4712 OpenMP:
4714 block-item:
4715 openmp-directive
4717 openmp-directive:
4718 barrier-directive
4719 flush-directive
4720 taskwait-directive
4721 taskyield-directive
4722 cancel-directive
4723 cancellation-point-directive */
4725 static tree
4726 c_parser_compound_statement (c_parser *parser)
4728 tree stmt;
4729 location_t brace_loc;
4730 brace_loc = c_parser_peek_token (parser)->location;
4731 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4733 /* Ensure a scope is entered and left anyway to avoid confusion
4734 if we have just prepared to enter a function body. */
4735 stmt = c_begin_compound_stmt (true);
4736 c_end_compound_stmt (brace_loc, stmt, true);
4737 return error_mark_node;
4739 stmt = c_begin_compound_stmt (true);
4740 c_parser_compound_statement_nostart (parser);
4742 /* If the compound stmt contains array notations, then we expand them. */
4743 if (flag_cilkplus && contains_array_notation_expr (stmt))
4744 stmt = expand_array_notation_exprs (stmt);
4745 return c_end_compound_stmt (brace_loc, stmt, true);
4748 /* Parse a compound statement except for the opening brace. This is
4749 used for parsing both compound statements and statement expressions
4750 (which follow different paths to handling the opening). */
4752 static void
4753 c_parser_compound_statement_nostart (c_parser *parser)
4755 bool last_stmt = false;
4756 bool last_label = false;
4757 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4758 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4759 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4761 c_parser_consume_token (parser);
4762 return;
4764 mark_valid_location_for_stdc_pragma (true);
4765 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4767 /* Read zero or more forward-declarations for labels that nested
4768 functions can jump to. */
4769 mark_valid_location_for_stdc_pragma (false);
4770 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4772 label_loc = c_parser_peek_token (parser)->location;
4773 c_parser_consume_token (parser);
4774 /* Any identifiers, including those declared as type names,
4775 are OK here. */
4776 while (true)
4778 tree label;
4779 if (c_parser_next_token_is_not (parser, CPP_NAME))
4781 c_parser_error (parser, "expected identifier");
4782 break;
4784 label
4785 = declare_label (c_parser_peek_token (parser)->value);
4786 C_DECLARED_LABEL_FLAG (label) = 1;
4787 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4788 c_parser_consume_token (parser);
4789 if (c_parser_next_token_is (parser, CPP_COMMA))
4790 c_parser_consume_token (parser);
4791 else
4792 break;
4794 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4796 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4798 /* We must now have at least one statement, label or declaration. */
4799 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4801 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4802 c_parser_error (parser, "expected declaration or statement");
4803 c_parser_consume_token (parser);
4804 return;
4806 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4808 location_t loc = c_parser_peek_token (parser)->location;
4809 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4810 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4811 || (c_parser_next_token_is (parser, CPP_NAME)
4812 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4814 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4815 label_loc = c_parser_peek_2nd_token (parser)->location;
4816 else
4817 label_loc = c_parser_peek_token (parser)->location;
4818 last_label = true;
4819 last_stmt = false;
4820 mark_valid_location_for_stdc_pragma (false);
4821 c_parser_label (parser);
4823 else if (!last_label
4824 && c_parser_next_tokens_start_declaration (parser))
4826 last_label = false;
4827 mark_valid_location_for_stdc_pragma (false);
4828 bool fallthru_attr_p = false;
4829 c_parser_declaration_or_fndef (parser, true, true, true, true,
4830 true, NULL, vNULL, NULL,
4831 &fallthru_attr_p);
4832 if (last_stmt && !fallthru_attr_p)
4833 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4834 "ISO C90 forbids mixed declarations and code");
4835 last_stmt = fallthru_attr_p;
4837 else if (!last_label
4838 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4840 /* __extension__ can start a declaration, but is also an
4841 unary operator that can start an expression. Consume all
4842 but the last of a possible series of __extension__ to
4843 determine which. */
4844 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4845 && (c_parser_peek_2nd_token (parser)->keyword
4846 == RID_EXTENSION))
4847 c_parser_consume_token (parser);
4848 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4850 int ext;
4851 ext = disable_extension_diagnostics ();
4852 c_parser_consume_token (parser);
4853 last_label = false;
4854 mark_valid_location_for_stdc_pragma (false);
4855 c_parser_declaration_or_fndef (parser, true, true, true, true,
4856 true, NULL, vNULL);
4857 /* Following the old parser, __extension__ does not
4858 disable this diagnostic. */
4859 restore_extension_diagnostics (ext);
4860 if (last_stmt)
4861 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4862 "ISO C90 forbids mixed declarations and code");
4863 last_stmt = false;
4865 else
4866 goto statement;
4868 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4870 /* External pragmas, and some omp pragmas, are not associated
4871 with regular c code, and so are not to be considered statements
4872 syntactically. This ensures that the user doesn't put them
4873 places that would turn into syntax errors if the directive
4874 were ignored. */
4875 if (c_parser_pragma (parser,
4876 last_label ? pragma_stmt : pragma_compound,
4877 NULL))
4878 last_label = false, last_stmt = true;
4880 else if (c_parser_next_token_is (parser, CPP_EOF))
4882 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4883 c_parser_error (parser, "expected declaration or statement");
4884 return;
4886 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4888 if (parser->in_if_block)
4890 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4891 error_at (loc, """expected %<}%> before %<else%>");
4892 return;
4894 else
4896 error_at (loc, "%<else%> without a previous %<if%>");
4897 c_parser_consume_token (parser);
4898 continue;
4901 else
4903 statement:
4904 last_label = false;
4905 last_stmt = true;
4906 mark_valid_location_for_stdc_pragma (false);
4907 c_parser_statement_after_labels (parser, NULL);
4910 parser->error = false;
4912 if (last_label)
4913 error_at (label_loc, "label at end of compound statement");
4914 c_parser_consume_token (parser);
4915 /* Restore the value we started with. */
4916 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4919 /* Parse all consecutive labels. */
4921 static void
4922 c_parser_all_labels (c_parser *parser)
4924 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4925 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4926 || (c_parser_next_token_is (parser, CPP_NAME)
4927 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4928 c_parser_label (parser);
4931 /* Parse a label (C90 6.6.1, C99 6.8.1).
4933 label:
4934 identifier : attributes[opt]
4935 case constant-expression :
4936 default :
4938 GNU extensions:
4940 label:
4941 case constant-expression ... constant-expression :
4943 The use of attributes on labels is a GNU extension. The syntax in
4944 GNU C accepts any expressions without commas, non-constant
4945 expressions being rejected later. */
4947 static void
4948 c_parser_label (c_parser *parser)
4950 location_t loc1 = c_parser_peek_token (parser)->location;
4951 tree label = NULL_TREE;
4953 /* Remember whether this case or a user-defined label is allowed to fall
4954 through to. */
4955 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
4957 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4959 tree exp1, exp2;
4960 c_parser_consume_token (parser);
4961 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4962 if (c_parser_next_token_is (parser, CPP_COLON))
4964 c_parser_consume_token (parser);
4965 label = do_case (loc1, exp1, NULL_TREE);
4967 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4969 c_parser_consume_token (parser);
4970 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4971 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4972 label = do_case (loc1, exp1, exp2);
4974 else
4975 c_parser_error (parser, "expected %<:%> or %<...%>");
4977 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4979 c_parser_consume_token (parser);
4980 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4981 label = do_case (loc1, NULL_TREE, NULL_TREE);
4983 else
4985 tree name = c_parser_peek_token (parser)->value;
4986 tree tlab;
4987 tree attrs;
4988 location_t loc2 = c_parser_peek_token (parser)->location;
4989 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4990 c_parser_consume_token (parser);
4991 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4992 c_parser_consume_token (parser);
4993 attrs = c_parser_attributes (parser);
4994 tlab = define_label (loc2, name);
4995 if (tlab)
4997 decl_attributes (&tlab, attrs, 0);
4998 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5001 if (label)
5003 if (TREE_CODE (label) == LABEL_EXPR)
5004 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5005 else
5006 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5008 /* Allow '__attribute__((fallthrough));'. */
5009 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5011 location_t loc = c_parser_peek_token (parser)->location;
5012 tree attrs = c_parser_attributes (parser);
5013 if (attribute_fallthrough_p (attrs))
5015 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5017 tree fn = build_call_expr_internal_loc (loc,
5018 IFN_FALLTHROUGH,
5019 void_type_node, 0);
5020 add_stmt (fn);
5022 else
5023 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5024 "not followed by %<;%>");
5026 else if (attrs != NULL_TREE)
5027 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5028 " can be applied to a null statement");
5030 if (c_parser_next_tokens_start_declaration (parser))
5032 error_at (c_parser_peek_token (parser)->location,
5033 "a label can only be part of a statement and "
5034 "a declaration is not a statement");
5035 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5036 /*static_assert_ok*/ true,
5037 /*empty_ok*/ true, /*nested*/ true,
5038 /*start_attr_ok*/ true, NULL,
5039 vNULL);
5044 /* Parse a statement (C90 6.6, C99 6.8).
5046 statement:
5047 labeled-statement
5048 compound-statement
5049 expression-statement
5050 selection-statement
5051 iteration-statement
5052 jump-statement
5054 labeled-statement:
5055 label statement
5057 expression-statement:
5058 expression[opt] ;
5060 selection-statement:
5061 if-statement
5062 switch-statement
5064 iteration-statement:
5065 while-statement
5066 do-statement
5067 for-statement
5069 jump-statement:
5070 goto identifier ;
5071 continue ;
5072 break ;
5073 return expression[opt] ;
5075 GNU extensions:
5077 statement:
5078 asm-statement
5080 jump-statement:
5081 goto * expression ;
5083 expression-statement:
5084 attributes ;
5086 Objective-C:
5088 statement:
5089 objc-throw-statement
5090 objc-try-catch-statement
5091 objc-synchronized-statement
5093 objc-throw-statement:
5094 @throw expression ;
5095 @throw ;
5097 OpenACC:
5099 statement:
5100 openacc-construct
5102 openacc-construct:
5103 parallel-construct
5104 kernels-construct
5105 data-construct
5106 loop-construct
5108 parallel-construct:
5109 parallel-directive structured-block
5111 kernels-construct:
5112 kernels-directive structured-block
5114 data-construct:
5115 data-directive structured-block
5117 loop-construct:
5118 loop-directive structured-block
5120 OpenMP:
5122 statement:
5123 openmp-construct
5125 openmp-construct:
5126 parallel-construct
5127 for-construct
5128 simd-construct
5129 for-simd-construct
5130 sections-construct
5131 single-construct
5132 parallel-for-construct
5133 parallel-for-simd-construct
5134 parallel-sections-construct
5135 master-construct
5136 critical-construct
5137 atomic-construct
5138 ordered-construct
5140 parallel-construct:
5141 parallel-directive structured-block
5143 for-construct:
5144 for-directive iteration-statement
5146 simd-construct:
5147 simd-directive iteration-statements
5149 for-simd-construct:
5150 for-simd-directive iteration-statements
5152 sections-construct:
5153 sections-directive section-scope
5155 single-construct:
5156 single-directive structured-block
5158 parallel-for-construct:
5159 parallel-for-directive iteration-statement
5161 parallel-for-simd-construct:
5162 parallel-for-simd-directive iteration-statement
5164 parallel-sections-construct:
5165 parallel-sections-directive section-scope
5167 master-construct:
5168 master-directive structured-block
5170 critical-construct:
5171 critical-directive structured-block
5173 atomic-construct:
5174 atomic-directive expression-statement
5176 ordered-construct:
5177 ordered-directive structured-block
5179 Transactional Memory:
5181 statement:
5182 transaction-statement
5183 transaction-cancel-statement
5185 IF_P is used to track whether there's a (possibly labeled) if statement
5186 which is not enclosed in braces and has an else clause. This is used to
5187 implement -Wparentheses. */
5189 static void
5190 c_parser_statement (c_parser *parser, bool *if_p)
5192 c_parser_all_labels (parser);
5193 c_parser_statement_after_labels (parser, if_p, NULL);
5196 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5197 of if-else-if conditions.
5199 IF_P is used to track whether there's a (possibly labeled) if statement
5200 which is not enclosed in braces and has an else clause. This is used to
5201 implement -Wparentheses. */
5203 static void
5204 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5205 vec<tree> *chain)
5207 location_t loc = c_parser_peek_token (parser)->location;
5208 tree stmt = NULL_TREE;
5209 bool in_if_block = parser->in_if_block;
5210 parser->in_if_block = false;
5211 if (if_p != NULL)
5212 *if_p = false;
5213 switch (c_parser_peek_token (parser)->type)
5215 case CPP_OPEN_BRACE:
5216 add_stmt (c_parser_compound_statement (parser));
5217 break;
5218 case CPP_KEYWORD:
5219 switch (c_parser_peek_token (parser)->keyword)
5221 case RID_IF:
5222 c_parser_if_statement (parser, if_p, chain);
5223 break;
5224 case RID_SWITCH:
5225 c_parser_switch_statement (parser, if_p);
5226 break;
5227 case RID_WHILE:
5228 c_parser_while_statement (parser, false, if_p);
5229 break;
5230 case RID_DO:
5231 c_parser_do_statement (parser, false);
5232 break;
5233 case RID_FOR:
5234 c_parser_for_statement (parser, false, if_p);
5235 break;
5236 case RID_CILK_FOR:
5237 if (!flag_cilkplus)
5239 error_at (c_parser_peek_token (parser)->location,
5240 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5241 c_parser_skip_to_end_of_block_or_statement (parser);
5243 else
5244 c_parser_cilk_for (parser, integer_zero_node, if_p);
5245 break;
5246 case RID_CILK_SYNC:
5247 c_parser_consume_token (parser);
5248 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5249 if (!flag_cilkplus)
5250 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5251 else
5252 add_stmt (build_cilk_sync ());
5253 break;
5254 case RID_GOTO:
5255 c_parser_consume_token (parser);
5256 if (c_parser_next_token_is (parser, CPP_NAME))
5258 stmt = c_finish_goto_label (loc,
5259 c_parser_peek_token (parser)->value);
5260 c_parser_consume_token (parser);
5262 else if (c_parser_next_token_is (parser, CPP_MULT))
5264 struct c_expr val;
5266 c_parser_consume_token (parser);
5267 val = c_parser_expression (parser);
5268 if (check_no_cilk (val.value,
5269 "Cilk array notation cannot be used as a computed goto expression",
5270 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5271 loc))
5272 val.value = error_mark_node;
5273 val = convert_lvalue_to_rvalue (loc, val, false, true);
5274 stmt = c_finish_goto_ptr (loc, val.value);
5276 else
5277 c_parser_error (parser, "expected identifier or %<*%>");
5278 goto expect_semicolon;
5279 case RID_CONTINUE:
5280 c_parser_consume_token (parser);
5281 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5282 goto expect_semicolon;
5283 case RID_BREAK:
5284 c_parser_consume_token (parser);
5285 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5286 goto expect_semicolon;
5287 case RID_RETURN:
5288 c_parser_consume_token (parser);
5289 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5291 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5292 c_parser_consume_token (parser);
5294 else
5296 location_t xloc = c_parser_peek_token (parser)->location;
5297 struct c_expr expr = c_parser_expression_conv (parser);
5298 mark_exp_read (expr.value);
5299 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5300 expr.value, expr.original_type);
5301 goto expect_semicolon;
5303 break;
5304 case RID_ASM:
5305 stmt = c_parser_asm_statement (parser);
5306 break;
5307 case RID_TRANSACTION_ATOMIC:
5308 case RID_TRANSACTION_RELAXED:
5309 stmt = c_parser_transaction (parser,
5310 c_parser_peek_token (parser)->keyword);
5311 break;
5312 case RID_TRANSACTION_CANCEL:
5313 stmt = c_parser_transaction_cancel (parser);
5314 goto expect_semicolon;
5315 case RID_AT_THROW:
5316 gcc_assert (c_dialect_objc ());
5317 c_parser_consume_token (parser);
5318 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5320 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5321 c_parser_consume_token (parser);
5323 else
5325 struct c_expr expr = c_parser_expression (parser);
5326 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5327 if (check_no_cilk (expr.value,
5328 "Cilk array notation cannot be used for a throw expression",
5329 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5330 expr.value = error_mark_node;
5331 else
5333 expr.value = c_fully_fold (expr.value, false, NULL);
5334 stmt = objc_build_throw_stmt (loc, expr.value);
5336 goto expect_semicolon;
5338 break;
5339 case RID_AT_TRY:
5340 gcc_assert (c_dialect_objc ());
5341 c_parser_objc_try_catch_finally_statement (parser);
5342 break;
5343 case RID_AT_SYNCHRONIZED:
5344 gcc_assert (c_dialect_objc ());
5345 c_parser_objc_synchronized_statement (parser);
5346 break;
5347 case RID_ATTRIBUTE:
5349 /* Allow '__attribute__((fallthrough));'. */
5350 tree attrs = c_parser_attributes (parser);
5351 if (attribute_fallthrough_p (attrs))
5353 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5355 tree fn = build_call_expr_internal_loc (loc,
5356 IFN_FALLTHROUGH,
5357 void_type_node, 0);
5358 add_stmt (fn);
5359 /* Eat the ';'. */
5360 c_parser_consume_token (parser);
5362 else
5363 warning_at (loc, OPT_Wattributes,
5364 "%<fallthrough%> attribute not followed "
5365 "by %<;%>");
5367 else if (attrs != NULL_TREE)
5368 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5369 " can be applied to a null statement");
5370 break;
5372 default:
5373 goto expr_stmt;
5375 break;
5376 case CPP_SEMICOLON:
5377 c_parser_consume_token (parser);
5378 break;
5379 case CPP_CLOSE_PAREN:
5380 case CPP_CLOSE_SQUARE:
5381 /* Avoid infinite loop in error recovery:
5382 c_parser_skip_until_found stops at a closing nesting
5383 delimiter without consuming it, but here we need to consume
5384 it to proceed further. */
5385 c_parser_error (parser, "expected statement");
5386 c_parser_consume_token (parser);
5387 break;
5388 case CPP_PRAGMA:
5389 c_parser_pragma (parser, pragma_stmt, if_p);
5390 break;
5391 default:
5392 expr_stmt:
5393 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5394 expect_semicolon:
5395 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5396 break;
5398 /* Two cases cannot and do not have line numbers associated: If stmt
5399 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5400 cannot hold line numbers. But that's OK because the statement
5401 will either be changed to a MODIFY_EXPR during gimplification of
5402 the statement expr, or discarded. If stmt was compound, but
5403 without new variables, we will have skipped the creation of a
5404 BIND and will have a bare STATEMENT_LIST. But that's OK because
5405 (recursively) all of the component statements should already have
5406 line numbers assigned. ??? Can we discard no-op statements
5407 earlier? */
5408 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5409 protected_set_expr_location (stmt, loc);
5411 parser->in_if_block = in_if_block;
5414 /* Parse the condition from an if, do, while or for statements. */
5416 static tree
5417 c_parser_condition (c_parser *parser)
5419 location_t loc = c_parser_peek_token (parser)->location;
5420 tree cond;
5421 cond = c_parser_expression_conv (parser).value;
5422 cond = c_objc_common_truthvalue_conversion (loc, cond);
5423 cond = c_fully_fold (cond, false, NULL);
5424 if (warn_sequence_point)
5425 verify_sequence_points (cond);
5426 return cond;
5429 /* Parse a parenthesized condition from an if, do or while statement.
5431 condition:
5432 ( expression )
5434 static tree
5435 c_parser_paren_condition (c_parser *parser)
5437 tree cond;
5438 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5439 return error_mark_node;
5440 cond = c_parser_condition (parser);
5441 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5442 return cond;
5445 /* Parse a statement which is a block in C99.
5447 IF_P is used to track whether there's a (possibly labeled) if statement
5448 which is not enclosed in braces and has an else clause. This is used to
5449 implement -Wparentheses. */
5451 static tree
5452 c_parser_c99_block_statement (c_parser *parser, bool *if_p)
5454 tree block = c_begin_compound_stmt (flag_isoc99);
5455 location_t loc = c_parser_peek_token (parser)->location;
5456 c_parser_statement (parser, if_p);
5457 return c_end_compound_stmt (loc, block, flag_isoc99);
5460 /* Parse the body of an if statement. This is just parsing a
5461 statement but (a) it is a block in C99, (b) we track whether the
5462 body is an if statement for the sake of -Wparentheses warnings, (c)
5463 we handle an empty body specially for the sake of -Wempty-body
5464 warnings, and (d) we call parser_compound_statement directly
5465 because c_parser_statement_after_labels resets
5466 parser->in_if_block.
5468 IF_P is used to track whether there's a (possibly labeled) if statement
5469 which is not enclosed in braces and has an else clause. This is used to
5470 implement -Wparentheses. */
5472 static tree
5473 c_parser_if_body (c_parser *parser, bool *if_p,
5474 const token_indent_info &if_tinfo)
5476 tree block = c_begin_compound_stmt (flag_isoc99);
5477 location_t body_loc = c_parser_peek_token (parser)->location;
5478 token_indent_info body_tinfo
5479 = get_token_indent_info (c_parser_peek_token (parser));
5481 c_parser_all_labels (parser);
5482 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5484 location_t loc = c_parser_peek_token (parser)->location;
5485 add_stmt (build_empty_stmt (loc));
5486 c_parser_consume_token (parser);
5487 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5488 warning_at (loc, OPT_Wempty_body,
5489 "suggest braces around empty body in an %<if%> statement");
5491 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5492 add_stmt (c_parser_compound_statement (parser));
5493 else
5494 c_parser_statement_after_labels (parser, if_p);
5496 token_indent_info next_tinfo
5497 = get_token_indent_info (c_parser_peek_token (parser));
5498 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5500 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5503 /* Parse the else body of an if statement. This is just parsing a
5504 statement but (a) it is a block in C99, (b) we handle an empty body
5505 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5506 of if-else-if conditions. */
5508 static tree
5509 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5510 vec<tree> *chain)
5512 location_t body_loc = c_parser_peek_token (parser)->location;
5513 tree block = c_begin_compound_stmt (flag_isoc99);
5514 token_indent_info body_tinfo
5515 = get_token_indent_info (c_parser_peek_token (parser));
5517 c_parser_all_labels (parser);
5518 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5520 location_t loc = c_parser_peek_token (parser)->location;
5521 warning_at (loc,
5522 OPT_Wempty_body,
5523 "suggest braces around empty body in an %<else%> statement");
5524 add_stmt (build_empty_stmt (loc));
5525 c_parser_consume_token (parser);
5527 else
5528 c_parser_statement_after_labels (parser, NULL, chain);
5530 token_indent_info next_tinfo
5531 = get_token_indent_info (c_parser_peek_token (parser));
5532 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5534 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5537 /* We might need to reclassify any previously-lexed identifier, e.g.
5538 when we've left a for loop with an if-statement without else in the
5539 body - we might have used a wrong scope for the token. See PR67784. */
5541 static void
5542 c_parser_maybe_reclassify_token (c_parser *parser)
5544 if (c_parser_next_token_is (parser, CPP_NAME))
5546 c_token *token = c_parser_peek_token (parser);
5548 if (token->id_kind != C_ID_CLASSNAME)
5550 tree decl = lookup_name (token->value);
5552 token->id_kind = C_ID_ID;
5553 if (decl)
5555 if (TREE_CODE (decl) == TYPE_DECL)
5556 token->id_kind = C_ID_TYPENAME;
5558 else if (c_dialect_objc ())
5560 tree objc_interface_decl = objc_is_class_name (token->value);
5561 /* Objective-C class names are in the same namespace as
5562 variables and typedefs, and hence are shadowed by local
5563 declarations. */
5564 if (objc_interface_decl)
5566 token->value = objc_interface_decl;
5567 token->id_kind = C_ID_CLASSNAME;
5574 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5576 if-statement:
5577 if ( expression ) statement
5578 if ( expression ) statement else statement
5580 CHAIN is a vector of if-else-if conditions.
5581 IF_P is used to track whether there's a (possibly labeled) if statement
5582 which is not enclosed in braces and has an else clause. This is used to
5583 implement -Wparentheses. */
5585 static void
5586 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5588 tree block;
5589 location_t loc;
5590 tree cond;
5591 bool nested_if = false;
5592 tree first_body, second_body;
5593 bool in_if_block;
5594 tree if_stmt;
5596 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5597 token_indent_info if_tinfo
5598 = get_token_indent_info (c_parser_peek_token (parser));
5599 c_parser_consume_token (parser);
5600 block = c_begin_compound_stmt (flag_isoc99);
5601 loc = c_parser_peek_token (parser)->location;
5602 cond = c_parser_paren_condition (parser);
5603 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5605 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5606 cond = error_mark_node;
5608 in_if_block = parser->in_if_block;
5609 parser->in_if_block = true;
5610 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5611 parser->in_if_block = in_if_block;
5613 if (warn_duplicated_cond)
5614 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5616 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5618 token_indent_info else_tinfo
5619 = get_token_indent_info (c_parser_peek_token (parser));
5620 c_parser_consume_token (parser);
5621 if (warn_duplicated_cond)
5623 if (c_parser_next_token_is_keyword (parser, RID_IF)
5624 && chain == NULL)
5626 /* We've got "if (COND) else if (COND2)". Start the
5627 condition chain and add COND as the first element. */
5628 chain = new vec<tree> ();
5629 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5630 chain->safe_push (cond);
5632 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5634 /* This is if-else without subsequent if. Zap the condition
5635 chain; we would have already warned at this point. */
5636 delete chain;
5637 chain = NULL;
5640 second_body = c_parser_else_body (parser, else_tinfo, chain);
5641 /* Set IF_P to true to indicate that this if statement has an
5642 else clause. This may trigger the Wparentheses warning
5643 below when we get back up to the parent if statement. */
5644 if (if_p != NULL)
5645 *if_p = true;
5647 else
5649 second_body = NULL_TREE;
5651 /* Diagnose an ambiguous else if if-then-else is nested inside
5652 if-then. */
5653 if (nested_if)
5654 warning_at (loc, OPT_Wdangling_else,
5655 "suggest explicit braces to avoid ambiguous %<else%>");
5657 if (warn_duplicated_cond)
5659 /* This if statement does not have an else clause. We don't
5660 need the condition chain anymore. */
5661 delete chain;
5662 chain = NULL;
5665 c_finish_if_stmt (loc, cond, first_body, second_body);
5666 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5668 /* If the if statement contains array notations, then we expand them. */
5669 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5670 if_stmt = fix_conditional_array_notations (if_stmt);
5671 add_stmt (if_stmt);
5672 c_parser_maybe_reclassify_token (parser);
5675 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5677 switch-statement:
5678 switch (expression) statement
5681 static void
5682 c_parser_switch_statement (c_parser *parser, bool *if_p)
5684 struct c_expr ce;
5685 tree block, expr, body, save_break;
5686 location_t switch_loc = c_parser_peek_token (parser)->location;
5687 location_t switch_cond_loc;
5688 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5689 c_parser_consume_token (parser);
5690 block = c_begin_compound_stmt (flag_isoc99);
5691 bool explicit_cast_p = false;
5692 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5694 switch_cond_loc = c_parser_peek_token (parser)->location;
5695 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5696 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5697 explicit_cast_p = true;
5698 ce = c_parser_expression (parser);
5699 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5700 expr = ce.value;
5701 /* ??? expr has no valid location? */
5702 if (check_no_cilk (expr,
5703 "Cilk array notation cannot be used as a condition for switch statement",
5704 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5705 switch_cond_loc))
5706 expr = error_mark_node;
5707 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5709 else
5711 switch_cond_loc = UNKNOWN_LOCATION;
5712 expr = error_mark_node;
5713 ce.original_type = error_mark_node;
5715 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5716 save_break = c_break_label;
5717 c_break_label = NULL_TREE;
5718 body = c_parser_c99_block_statement (parser, if_p);
5719 c_finish_case (body, ce.original_type);
5720 if (c_break_label)
5722 location_t here = c_parser_peek_token (parser)->location;
5723 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5724 SET_EXPR_LOCATION (t, here);
5725 add_stmt (t);
5727 c_break_label = save_break;
5728 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5729 c_parser_maybe_reclassify_token (parser);
5732 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5734 while-statement:
5735 while (expression) statement
5737 IF_P is used to track whether there's a (possibly labeled) if statement
5738 which is not enclosed in braces and has an else clause. This is used to
5739 implement -Wparentheses. */
5741 static void
5742 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5744 tree block, cond, body, save_break, save_cont;
5745 location_t loc;
5746 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5747 token_indent_info while_tinfo
5748 = get_token_indent_info (c_parser_peek_token (parser));
5749 c_parser_consume_token (parser);
5750 block = c_begin_compound_stmt (flag_isoc99);
5751 loc = c_parser_peek_token (parser)->location;
5752 cond = c_parser_paren_condition (parser);
5753 if (check_no_cilk (cond,
5754 "Cilk array notation cannot be used as a condition for while statement",
5755 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5756 cond = error_mark_node;
5757 if (ivdep && cond != error_mark_node)
5758 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5759 build_int_cst (integer_type_node,
5760 annot_expr_ivdep_kind));
5761 save_break = c_break_label;
5762 c_break_label = NULL_TREE;
5763 save_cont = c_cont_label;
5764 c_cont_label = NULL_TREE;
5766 token_indent_info body_tinfo
5767 = get_token_indent_info (c_parser_peek_token (parser));
5769 body = c_parser_c99_block_statement (parser, if_p);
5770 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5771 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5772 c_parser_maybe_reclassify_token (parser);
5774 token_indent_info next_tinfo
5775 = get_token_indent_info (c_parser_peek_token (parser));
5776 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5778 c_break_label = save_break;
5779 c_cont_label = save_cont;
5782 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5784 do-statement:
5785 do statement while ( expression ) ;
5788 static void
5789 c_parser_do_statement (c_parser *parser, bool ivdep)
5791 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5792 location_t loc;
5793 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5794 c_parser_consume_token (parser);
5795 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5796 warning_at (c_parser_peek_token (parser)->location,
5797 OPT_Wempty_body,
5798 "suggest braces around empty body in %<do%> statement");
5799 block = c_begin_compound_stmt (flag_isoc99);
5800 loc = c_parser_peek_token (parser)->location;
5801 save_break = c_break_label;
5802 c_break_label = NULL_TREE;
5803 save_cont = c_cont_label;
5804 c_cont_label = NULL_TREE;
5805 body = c_parser_c99_block_statement (parser, NULL);
5806 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5807 new_break = c_break_label;
5808 c_break_label = save_break;
5809 new_cont = c_cont_label;
5810 c_cont_label = save_cont;
5811 cond = c_parser_paren_condition (parser);
5812 if (check_no_cilk (cond,
5813 "Cilk array notation cannot be used as a condition for a do-while statement",
5814 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5815 cond = error_mark_node;
5816 if (ivdep && cond != error_mark_node)
5817 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5818 build_int_cst (integer_type_node,
5819 annot_expr_ivdep_kind));
5820 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5821 c_parser_skip_to_end_of_block_or_statement (parser);
5822 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5823 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5826 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5828 for-statement:
5829 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5830 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5832 The form with a declaration is new in C99.
5834 ??? In accordance with the old parser, the declaration may be a
5835 nested function, which is then rejected in check_for_loop_decls,
5836 but does it make any sense for this to be included in the grammar?
5837 Note in particular that the nested function does not include a
5838 trailing ';', whereas the "declaration" production includes one.
5839 Also, can we reject bad declarations earlier and cheaper than
5840 check_for_loop_decls?
5842 In Objective-C, there are two additional variants:
5844 foreach-statement:
5845 for ( expression in expresssion ) statement
5846 for ( declaration in expression ) statement
5848 This is inconsistent with C, because the second variant is allowed
5849 even if c99 is not enabled.
5851 The rest of the comment documents these Objective-C foreach-statement.
5853 Here is the canonical example of the first variant:
5854 for (object in array) { do something with object }
5855 we call the first expression ("object") the "object_expression" and
5856 the second expression ("array") the "collection_expression".
5857 object_expression must be an lvalue of type "id" (a generic Objective-C
5858 object) because the loop works by assigning to object_expression the
5859 various objects from the collection_expression. collection_expression
5860 must evaluate to something of type "id" which responds to the method
5861 countByEnumeratingWithState:objects:count:.
5863 The canonical example of the second variant is:
5864 for (id object in array) { do something with object }
5865 which is completely equivalent to
5867 id object;
5868 for (object in array) { do something with object }
5870 Note that initizializing 'object' in some way (eg, "for ((object =
5871 xxx) in array) { do something with object }") is possibly
5872 technically valid, but completely pointless as 'object' will be
5873 assigned to something else as soon as the loop starts. We should
5874 most likely reject it (TODO).
5876 The beginning of the Objective-C foreach-statement looks exactly
5877 like the beginning of the for-statement, and we can tell it is a
5878 foreach-statement only because the initial declaration or
5879 expression is terminated by 'in' instead of ';'.
5881 IF_P is used to track whether there's a (possibly labeled) if statement
5882 which is not enclosed in braces and has an else clause. This is used to
5883 implement -Wparentheses. */
5885 static void
5886 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
5888 tree block, cond, incr, save_break, save_cont, body;
5889 /* The following are only used when parsing an ObjC foreach statement. */
5890 tree object_expression;
5891 /* Silence the bogus uninitialized warning. */
5892 tree collection_expression = NULL;
5893 location_t loc = c_parser_peek_token (parser)->location;
5894 location_t for_loc = c_parser_peek_token (parser)->location;
5895 bool is_foreach_statement = false;
5896 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5897 token_indent_info for_tinfo
5898 = get_token_indent_info (c_parser_peek_token (parser));
5899 c_parser_consume_token (parser);
5900 /* Open a compound statement in Objective-C as well, just in case this is
5901 as foreach expression. */
5902 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5903 cond = error_mark_node;
5904 incr = error_mark_node;
5905 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5907 /* Parse the initialization declaration or expression. */
5908 object_expression = error_mark_node;
5909 parser->objc_could_be_foreach_context = c_dialect_objc ();
5910 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5912 parser->objc_could_be_foreach_context = false;
5913 c_parser_consume_token (parser);
5914 c_finish_expr_stmt (loc, NULL_TREE);
5916 else if (c_parser_next_tokens_start_declaration (parser))
5918 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5919 &object_expression, vNULL);
5920 parser->objc_could_be_foreach_context = false;
5922 if (c_parser_next_token_is_keyword (parser, RID_IN))
5924 c_parser_consume_token (parser);
5925 is_foreach_statement = true;
5926 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5927 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5929 else
5930 check_for_loop_decls (for_loc, flag_isoc99);
5932 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5934 /* __extension__ can start a declaration, but is also an
5935 unary operator that can start an expression. Consume all
5936 but the last of a possible series of __extension__ to
5937 determine which. */
5938 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5939 && (c_parser_peek_2nd_token (parser)->keyword
5940 == RID_EXTENSION))
5941 c_parser_consume_token (parser);
5942 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5944 int ext;
5945 ext = disable_extension_diagnostics ();
5946 c_parser_consume_token (parser);
5947 c_parser_declaration_or_fndef (parser, true, true, true, true,
5948 true, &object_expression, vNULL);
5949 parser->objc_could_be_foreach_context = false;
5951 restore_extension_diagnostics (ext);
5952 if (c_parser_next_token_is_keyword (parser, RID_IN))
5954 c_parser_consume_token (parser);
5955 is_foreach_statement = true;
5956 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5957 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5959 else
5960 check_for_loop_decls (for_loc, flag_isoc99);
5962 else
5963 goto init_expr;
5965 else
5967 init_expr:
5969 struct c_expr ce;
5970 tree init_expression;
5971 ce = c_parser_expression (parser);
5972 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5973 level statement", but it works just fine, so allow it. */
5974 init_expression = ce.value;
5975 parser->objc_could_be_foreach_context = false;
5976 if (c_parser_next_token_is_keyword (parser, RID_IN))
5978 c_parser_consume_token (parser);
5979 is_foreach_statement = true;
5980 if (! lvalue_p (init_expression))
5981 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5982 object_expression = c_fully_fold (init_expression, false, NULL);
5984 else
5986 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5987 init_expression = ce.value;
5988 c_finish_expr_stmt (loc, init_expression);
5989 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5993 /* Parse the loop condition. In the case of a foreach
5994 statement, there is no loop condition. */
5995 gcc_assert (!parser->objc_could_be_foreach_context);
5996 if (!is_foreach_statement)
5998 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6000 if (ivdep)
6002 c_parser_error (parser, "missing loop condition in loop with "
6003 "%<GCC ivdep%> pragma");
6004 cond = error_mark_node;
6006 else
6008 c_parser_consume_token (parser);
6009 cond = NULL_TREE;
6012 else
6014 cond = c_parser_condition (parser);
6015 if (check_no_cilk (cond,
6016 "Cilk array notation cannot be used in a condition for a for-loop",
6017 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
6018 cond = error_mark_node;
6019 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6020 "expected %<;%>");
6022 if (ivdep && cond != error_mark_node)
6023 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6024 build_int_cst (integer_type_node,
6025 annot_expr_ivdep_kind));
6027 /* Parse the increment expression (the third expression in a
6028 for-statement). In the case of a foreach-statement, this is
6029 the expression that follows the 'in'. */
6030 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6032 if (is_foreach_statement)
6034 c_parser_error (parser, "missing collection in fast enumeration");
6035 collection_expression = error_mark_node;
6037 else
6038 incr = c_process_expr_stmt (loc, NULL_TREE);
6040 else
6042 if (is_foreach_statement)
6043 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6044 false, NULL);
6045 else
6047 struct c_expr ce = c_parser_expression (parser);
6048 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6049 incr = c_process_expr_stmt (loc, ce.value);
6052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6054 save_break = c_break_label;
6055 c_break_label = NULL_TREE;
6056 save_cont = c_cont_label;
6057 c_cont_label = NULL_TREE;
6059 token_indent_info body_tinfo
6060 = get_token_indent_info (c_parser_peek_token (parser));
6062 body = c_parser_c99_block_statement (parser, if_p);
6064 if (is_foreach_statement)
6065 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6066 else
6067 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6068 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6069 c_parser_maybe_reclassify_token (parser);
6071 token_indent_info next_tinfo
6072 = get_token_indent_info (c_parser_peek_token (parser));
6073 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6075 c_break_label = save_break;
6076 c_cont_label = save_cont;
6079 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6080 statement with inputs, outputs, clobbers, and volatile tag
6081 allowed.
6083 asm-statement:
6084 asm type-qualifier[opt] ( asm-argument ) ;
6085 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6087 asm-argument:
6088 asm-string-literal
6089 asm-string-literal : asm-operands[opt]
6090 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6091 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6093 asm-goto-argument:
6094 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6095 : asm-goto-operands
6097 Qualifiers other than volatile are accepted in the syntax but
6098 warned for. */
6100 static tree
6101 c_parser_asm_statement (c_parser *parser)
6103 tree quals, str, outputs, inputs, clobbers, labels, ret;
6104 bool simple, is_goto;
6105 location_t asm_loc = c_parser_peek_token (parser)->location;
6106 int section, nsections;
6108 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6109 c_parser_consume_token (parser);
6110 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6112 quals = c_parser_peek_token (parser)->value;
6113 c_parser_consume_token (parser);
6115 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6116 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6118 warning_at (c_parser_peek_token (parser)->location,
6120 "%E qualifier ignored on asm",
6121 c_parser_peek_token (parser)->value);
6122 quals = NULL_TREE;
6123 c_parser_consume_token (parser);
6125 else
6126 quals = NULL_TREE;
6128 is_goto = false;
6129 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6131 c_parser_consume_token (parser);
6132 is_goto = true;
6135 /* ??? Follow the C++ parser rather than using the
6136 lex_untranslated_string kludge. */
6137 parser->lex_untranslated_string = true;
6138 ret = NULL;
6140 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6141 goto error;
6143 str = c_parser_asm_string_literal (parser);
6144 if (str == NULL_TREE)
6145 goto error_close_paren;
6147 simple = true;
6148 outputs = NULL_TREE;
6149 inputs = NULL_TREE;
6150 clobbers = NULL_TREE;
6151 labels = NULL_TREE;
6153 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6154 goto done_asm;
6156 /* Parse each colon-delimited section of operands. */
6157 nsections = 3 + is_goto;
6158 for (section = 0; section < nsections; ++section)
6160 if (!c_parser_require (parser, CPP_COLON,
6161 is_goto
6162 ? "expected %<:%>"
6163 : "expected %<:%> or %<)%>"))
6164 goto error_close_paren;
6166 /* Once past any colon, we're no longer a simple asm. */
6167 simple = false;
6169 if ((!c_parser_next_token_is (parser, CPP_COLON)
6170 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6171 || section == 3)
6172 switch (section)
6174 case 0:
6175 /* For asm goto, we don't allow output operands, but reserve
6176 the slot for a future extension that does allow them. */
6177 if (!is_goto)
6178 outputs = c_parser_asm_operands (parser);
6179 break;
6180 case 1:
6181 inputs = c_parser_asm_operands (parser);
6182 break;
6183 case 2:
6184 clobbers = c_parser_asm_clobbers (parser);
6185 break;
6186 case 3:
6187 labels = c_parser_asm_goto_operands (parser);
6188 break;
6189 default:
6190 gcc_unreachable ();
6193 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6194 goto done_asm;
6197 done_asm:
6198 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6200 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6201 goto error;
6204 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6205 c_parser_skip_to_end_of_block_or_statement (parser);
6207 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6208 clobbers, labels, simple));
6210 error:
6211 parser->lex_untranslated_string = false;
6212 return ret;
6214 error_close_paren:
6215 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6216 goto error;
6219 /* Parse asm operands, a GNU extension.
6221 asm-operands:
6222 asm-operand
6223 asm-operands , asm-operand
6225 asm-operand:
6226 asm-string-literal ( expression )
6227 [ identifier ] asm-string-literal ( expression )
6230 static tree
6231 c_parser_asm_operands (c_parser *parser)
6233 tree list = NULL_TREE;
6234 while (true)
6236 tree name, str;
6237 struct c_expr expr;
6238 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6240 c_parser_consume_token (parser);
6241 if (c_parser_next_token_is (parser, CPP_NAME))
6243 tree id = c_parser_peek_token (parser)->value;
6244 c_parser_consume_token (parser);
6245 name = build_string (IDENTIFIER_LENGTH (id),
6246 IDENTIFIER_POINTER (id));
6248 else
6250 c_parser_error (parser, "expected identifier");
6251 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6252 return NULL_TREE;
6254 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6255 "expected %<]%>");
6257 else
6258 name = NULL_TREE;
6259 str = c_parser_asm_string_literal (parser);
6260 if (str == NULL_TREE)
6261 return NULL_TREE;
6262 parser->lex_untranslated_string = false;
6263 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6265 parser->lex_untranslated_string = true;
6266 return NULL_TREE;
6268 expr = c_parser_expression (parser);
6269 mark_exp_read (expr.value);
6270 parser->lex_untranslated_string = true;
6271 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6274 return NULL_TREE;
6276 list = chainon (list, build_tree_list (build_tree_list (name, str),
6277 expr.value));
6278 if (c_parser_next_token_is (parser, CPP_COMMA))
6279 c_parser_consume_token (parser);
6280 else
6281 break;
6283 return list;
6286 /* Parse asm clobbers, a GNU extension.
6288 asm-clobbers:
6289 asm-string-literal
6290 asm-clobbers , asm-string-literal
6293 static tree
6294 c_parser_asm_clobbers (c_parser *parser)
6296 tree list = NULL_TREE;
6297 while (true)
6299 tree str = c_parser_asm_string_literal (parser);
6300 if (str)
6301 list = tree_cons (NULL_TREE, str, list);
6302 else
6303 return NULL_TREE;
6304 if (c_parser_next_token_is (parser, CPP_COMMA))
6305 c_parser_consume_token (parser);
6306 else
6307 break;
6309 return list;
6312 /* Parse asm goto labels, a GNU extension.
6314 asm-goto-operands:
6315 identifier
6316 asm-goto-operands , identifier
6319 static tree
6320 c_parser_asm_goto_operands (c_parser *parser)
6322 tree list = NULL_TREE;
6323 while (true)
6325 tree name, label;
6327 if (c_parser_next_token_is (parser, CPP_NAME))
6329 c_token *tok = c_parser_peek_token (parser);
6330 name = tok->value;
6331 label = lookup_label_for_goto (tok->location, name);
6332 c_parser_consume_token (parser);
6333 TREE_USED (label) = 1;
6335 else
6337 c_parser_error (parser, "expected identifier");
6338 return NULL_TREE;
6341 name = build_string (IDENTIFIER_LENGTH (name),
6342 IDENTIFIER_POINTER (name));
6343 list = tree_cons (name, label, list);
6344 if (c_parser_next_token_is (parser, CPP_COMMA))
6345 c_parser_consume_token (parser);
6346 else
6347 return nreverse (list);
6351 /* Parse an expression other than a compound expression; that is, an
6352 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6353 NULL then it is an Objective-C message expression which is the
6354 primary-expression starting the expression as an initializer.
6356 assignment-expression:
6357 conditional-expression
6358 unary-expression assignment-operator assignment-expression
6360 assignment-operator: one of
6361 = *= /= %= += -= <<= >>= &= ^= |=
6363 In GNU C we accept any conditional expression on the LHS and
6364 diagnose the invalid lvalue rather than producing a syntax
6365 error. */
6367 static struct c_expr
6368 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6369 tree omp_atomic_lhs)
6371 struct c_expr lhs, rhs, ret;
6372 enum tree_code code;
6373 location_t op_location, exp_location;
6374 gcc_assert (!after || c_dialect_objc ());
6375 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6376 op_location = c_parser_peek_token (parser)->location;
6377 switch (c_parser_peek_token (parser)->type)
6379 case CPP_EQ:
6380 code = NOP_EXPR;
6381 break;
6382 case CPP_MULT_EQ:
6383 code = MULT_EXPR;
6384 break;
6385 case CPP_DIV_EQ:
6386 code = TRUNC_DIV_EXPR;
6387 break;
6388 case CPP_MOD_EQ:
6389 code = TRUNC_MOD_EXPR;
6390 break;
6391 case CPP_PLUS_EQ:
6392 code = PLUS_EXPR;
6393 break;
6394 case CPP_MINUS_EQ:
6395 code = MINUS_EXPR;
6396 break;
6397 case CPP_LSHIFT_EQ:
6398 code = LSHIFT_EXPR;
6399 break;
6400 case CPP_RSHIFT_EQ:
6401 code = RSHIFT_EXPR;
6402 break;
6403 case CPP_AND_EQ:
6404 code = BIT_AND_EXPR;
6405 break;
6406 case CPP_XOR_EQ:
6407 code = BIT_XOR_EXPR;
6408 break;
6409 case CPP_OR_EQ:
6410 code = BIT_IOR_EXPR;
6411 break;
6412 default:
6413 return lhs;
6415 c_parser_consume_token (parser);
6416 exp_location = c_parser_peek_token (parser)->location;
6417 rhs = c_parser_expr_no_commas (parser, NULL);
6418 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6420 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6421 code, exp_location, rhs.value,
6422 rhs.original_type);
6423 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6424 if (code == NOP_EXPR)
6425 ret.original_code = MODIFY_EXPR;
6426 else
6428 TREE_NO_WARNING (ret.value) = 1;
6429 ret.original_code = ERROR_MARK;
6431 ret.original_type = NULL;
6432 return ret;
6435 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6436 is not NULL then it is an Objective-C message expression which is
6437 the primary-expression starting the expression as an initializer.
6439 conditional-expression:
6440 logical-OR-expression
6441 logical-OR-expression ? expression : conditional-expression
6443 GNU extensions:
6445 conditional-expression:
6446 logical-OR-expression ? : conditional-expression
6449 static struct c_expr
6450 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6451 tree omp_atomic_lhs)
6453 struct c_expr cond, exp1, exp2, ret;
6454 location_t start, cond_loc, colon_loc, middle_loc;
6456 gcc_assert (!after || c_dialect_objc ());
6458 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6460 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6461 return cond;
6462 if (cond.value != error_mark_node)
6463 start = cond.get_start ();
6464 else
6465 start = UNKNOWN_LOCATION;
6466 cond_loc = c_parser_peek_token (parser)->location;
6467 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6468 c_parser_consume_token (parser);
6469 if (c_parser_next_token_is (parser, CPP_COLON))
6471 tree eptype = NULL_TREE;
6473 middle_loc = c_parser_peek_token (parser)->location;
6474 pedwarn (middle_loc, OPT_Wpedantic,
6475 "ISO C forbids omitting the middle term of a ?: expression");
6476 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6478 eptype = TREE_TYPE (cond.value);
6479 cond.value = TREE_OPERAND (cond.value, 0);
6481 tree e = cond.value;
6482 while (TREE_CODE (e) == COMPOUND_EXPR)
6483 e = TREE_OPERAND (e, 1);
6484 warn_for_omitted_condop (middle_loc, e);
6485 /* Make sure first operand is calculated only once. */
6486 exp1.value = c_save_expr (default_conversion (cond.value));
6487 if (eptype)
6488 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6489 exp1.original_type = NULL;
6490 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6491 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6493 else
6495 cond.value
6496 = c_objc_common_truthvalue_conversion
6497 (cond_loc, default_conversion (cond.value));
6498 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6499 exp1 = c_parser_expression_conv (parser);
6500 mark_exp_read (exp1.value);
6501 c_inhibit_evaluation_warnings +=
6502 ((cond.value == truthvalue_true_node)
6503 - (cond.value == truthvalue_false_node));
6506 colon_loc = c_parser_peek_token (parser)->location;
6507 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6509 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6510 ret.value = error_mark_node;
6511 ret.original_code = ERROR_MARK;
6512 ret.original_type = NULL;
6513 return ret;
6516 location_t exp2_loc = c_parser_peek_token (parser)->location;
6517 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6518 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6520 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6521 ret.value = build_conditional_expr (colon_loc, cond.value,
6522 cond.original_code == C_MAYBE_CONST_EXPR,
6523 exp1.value, exp1.original_type,
6524 exp2.value, exp2.original_type);
6525 ret.original_code = ERROR_MARK;
6526 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6527 ret.original_type = NULL;
6528 else
6530 tree t1, t2;
6532 /* If both sides are enum type, the default conversion will have
6533 made the type of the result be an integer type. We want to
6534 remember the enum types we started with. */
6535 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6536 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6537 ret.original_type = ((t1 != error_mark_node
6538 && t2 != error_mark_node
6539 && (TYPE_MAIN_VARIANT (t1)
6540 == TYPE_MAIN_VARIANT (t2)))
6541 ? t1
6542 : NULL);
6544 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6545 return ret;
6548 /* Parse a binary expression; that is, a logical-OR-expression (C90
6549 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6550 an Objective-C message expression which is the primary-expression
6551 starting the expression as an initializer.
6553 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6554 when it should be the unfolded lhs. In a valid OpenMP source,
6555 one of the operands of the toplevel binary expression must be equal
6556 to it. In that case, just return a build2 created binary operation
6557 rather than result of parser_build_binary_op.
6559 multiplicative-expression:
6560 cast-expression
6561 multiplicative-expression * cast-expression
6562 multiplicative-expression / cast-expression
6563 multiplicative-expression % cast-expression
6565 additive-expression:
6566 multiplicative-expression
6567 additive-expression + multiplicative-expression
6568 additive-expression - multiplicative-expression
6570 shift-expression:
6571 additive-expression
6572 shift-expression << additive-expression
6573 shift-expression >> additive-expression
6575 relational-expression:
6576 shift-expression
6577 relational-expression < shift-expression
6578 relational-expression > shift-expression
6579 relational-expression <= shift-expression
6580 relational-expression >= shift-expression
6582 equality-expression:
6583 relational-expression
6584 equality-expression == relational-expression
6585 equality-expression != relational-expression
6587 AND-expression:
6588 equality-expression
6589 AND-expression & equality-expression
6591 exclusive-OR-expression:
6592 AND-expression
6593 exclusive-OR-expression ^ AND-expression
6595 inclusive-OR-expression:
6596 exclusive-OR-expression
6597 inclusive-OR-expression | exclusive-OR-expression
6599 logical-AND-expression:
6600 inclusive-OR-expression
6601 logical-AND-expression && inclusive-OR-expression
6603 logical-OR-expression:
6604 logical-AND-expression
6605 logical-OR-expression || logical-AND-expression
6608 static struct c_expr
6609 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6610 tree omp_atomic_lhs)
6612 /* A binary expression is parsed using operator-precedence parsing,
6613 with the operands being cast expressions. All the binary
6614 operators are left-associative. Thus a binary expression is of
6615 form:
6617 E0 op1 E1 op2 E2 ...
6619 which we represent on a stack. On the stack, the precedence
6620 levels are strictly increasing. When a new operator is
6621 encountered of higher precedence than that at the top of the
6622 stack, it is pushed; its LHS is the top expression, and its RHS
6623 is everything parsed until it is popped. When a new operator is
6624 encountered with precedence less than or equal to that at the top
6625 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6626 by the result of the operation until the operator at the top of
6627 the stack has lower precedence than the new operator or there is
6628 only one element on the stack; then the top expression is the LHS
6629 of the new operator. In the case of logical AND and OR
6630 expressions, we also need to adjust c_inhibit_evaluation_warnings
6631 as appropriate when the operators are pushed and popped. */
6633 struct {
6634 /* The expression at this stack level. */
6635 struct c_expr expr;
6636 /* The precedence of the operator on its left, PREC_NONE at the
6637 bottom of the stack. */
6638 enum c_parser_prec prec;
6639 /* The operation on its left. */
6640 enum tree_code op;
6641 /* The source location of this operation. */
6642 location_t loc;
6643 } stack[NUM_PRECS];
6644 int sp;
6645 /* Location of the binary operator. */
6646 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6647 #define POP \
6648 do { \
6649 switch (stack[sp].op) \
6651 case TRUTH_ANDIF_EXPR: \
6652 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6653 == truthvalue_false_node); \
6654 break; \
6655 case TRUTH_ORIF_EXPR: \
6656 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6657 == truthvalue_true_node); \
6658 break; \
6659 default: \
6660 break; \
6662 stack[sp - 1].expr \
6663 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6664 stack[sp - 1].expr, true, true); \
6665 stack[sp].expr \
6666 = convert_lvalue_to_rvalue (stack[sp].loc, \
6667 stack[sp].expr, true, true); \
6668 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6669 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6670 && ((1 << stack[sp].prec) \
6671 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6672 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6673 && stack[sp].op != TRUNC_MOD_EXPR \
6674 && stack[0].expr.value != error_mark_node \
6675 && stack[1].expr.value != error_mark_node \
6676 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6677 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6678 stack[0].expr.value \
6679 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6680 stack[0].expr.value, stack[1].expr.value); \
6681 else \
6682 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6683 stack[sp].op, \
6684 stack[sp - 1].expr, \
6685 stack[sp].expr); \
6686 sp--; \
6687 } while (0)
6688 gcc_assert (!after || c_dialect_objc ());
6689 stack[0].loc = c_parser_peek_token (parser)->location;
6690 stack[0].expr = c_parser_cast_expression (parser, after);
6691 stack[0].prec = PREC_NONE;
6692 sp = 0;
6693 while (true)
6695 enum c_parser_prec oprec;
6696 enum tree_code ocode;
6697 source_range src_range;
6698 if (parser->error)
6699 goto out;
6700 switch (c_parser_peek_token (parser)->type)
6702 case CPP_MULT:
6703 oprec = PREC_MULT;
6704 ocode = MULT_EXPR;
6705 break;
6706 case CPP_DIV:
6707 oprec = PREC_MULT;
6708 ocode = TRUNC_DIV_EXPR;
6709 break;
6710 case CPP_MOD:
6711 oprec = PREC_MULT;
6712 ocode = TRUNC_MOD_EXPR;
6713 break;
6714 case CPP_PLUS:
6715 oprec = PREC_ADD;
6716 ocode = PLUS_EXPR;
6717 break;
6718 case CPP_MINUS:
6719 oprec = PREC_ADD;
6720 ocode = MINUS_EXPR;
6721 break;
6722 case CPP_LSHIFT:
6723 oprec = PREC_SHIFT;
6724 ocode = LSHIFT_EXPR;
6725 break;
6726 case CPP_RSHIFT:
6727 oprec = PREC_SHIFT;
6728 ocode = RSHIFT_EXPR;
6729 break;
6730 case CPP_LESS:
6731 oprec = PREC_REL;
6732 ocode = LT_EXPR;
6733 break;
6734 case CPP_GREATER:
6735 oprec = PREC_REL;
6736 ocode = GT_EXPR;
6737 break;
6738 case CPP_LESS_EQ:
6739 oprec = PREC_REL;
6740 ocode = LE_EXPR;
6741 break;
6742 case CPP_GREATER_EQ:
6743 oprec = PREC_REL;
6744 ocode = GE_EXPR;
6745 break;
6746 case CPP_EQ_EQ:
6747 oprec = PREC_EQ;
6748 ocode = EQ_EXPR;
6749 break;
6750 case CPP_NOT_EQ:
6751 oprec = PREC_EQ;
6752 ocode = NE_EXPR;
6753 break;
6754 case CPP_AND:
6755 oprec = PREC_BITAND;
6756 ocode = BIT_AND_EXPR;
6757 break;
6758 case CPP_XOR:
6759 oprec = PREC_BITXOR;
6760 ocode = BIT_XOR_EXPR;
6761 break;
6762 case CPP_OR:
6763 oprec = PREC_BITOR;
6764 ocode = BIT_IOR_EXPR;
6765 break;
6766 case CPP_AND_AND:
6767 oprec = PREC_LOGAND;
6768 ocode = TRUTH_ANDIF_EXPR;
6769 break;
6770 case CPP_OR_OR:
6771 oprec = PREC_LOGOR;
6772 ocode = TRUTH_ORIF_EXPR;
6773 break;
6774 default:
6775 /* Not a binary operator, so end of the binary
6776 expression. */
6777 goto out;
6779 binary_loc = c_parser_peek_token (parser)->location;
6780 while (oprec <= stack[sp].prec)
6781 POP;
6782 c_parser_consume_token (parser);
6783 switch (ocode)
6785 case TRUTH_ANDIF_EXPR:
6786 src_range = stack[sp].expr.src_range;
6787 stack[sp].expr
6788 = convert_lvalue_to_rvalue (stack[sp].loc,
6789 stack[sp].expr, true, true);
6790 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6791 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6792 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6793 == truthvalue_false_node);
6794 set_c_expr_source_range (&stack[sp].expr, src_range);
6795 break;
6796 case TRUTH_ORIF_EXPR:
6797 src_range = stack[sp].expr.src_range;
6798 stack[sp].expr
6799 = convert_lvalue_to_rvalue (stack[sp].loc,
6800 stack[sp].expr, true, true);
6801 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6802 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6803 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6804 == truthvalue_true_node);
6805 set_c_expr_source_range (&stack[sp].expr, src_range);
6806 break;
6807 default:
6808 break;
6810 sp++;
6811 stack[sp].loc = binary_loc;
6812 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6813 stack[sp].prec = oprec;
6814 stack[sp].op = ocode;
6816 out:
6817 while (sp > 0)
6818 POP;
6819 return stack[0].expr;
6820 #undef POP
6823 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6824 NULL then it is an Objective-C message expression which is the
6825 primary-expression starting the expression as an initializer.
6827 cast-expression:
6828 unary-expression
6829 ( type-name ) unary-expression
6832 static struct c_expr
6833 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6835 location_t cast_loc = c_parser_peek_token (parser)->location;
6836 gcc_assert (!after || c_dialect_objc ());
6837 if (after)
6838 return c_parser_postfix_expression_after_primary (parser,
6839 cast_loc, *after);
6840 /* If the expression begins with a parenthesized type name, it may
6841 be either a cast or a compound literal; we need to see whether
6842 the next character is '{' to tell the difference. If not, it is
6843 an unary expression. Full detection of unknown typenames here
6844 would require a 3-token lookahead. */
6845 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6846 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6848 struct c_type_name *type_name;
6849 struct c_expr ret;
6850 struct c_expr expr;
6851 c_parser_consume_token (parser);
6852 type_name = c_parser_type_name (parser);
6853 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6854 if (type_name == NULL)
6856 ret.value = error_mark_node;
6857 ret.original_code = ERROR_MARK;
6858 ret.original_type = NULL;
6859 return ret;
6862 /* Save casted types in the function's used types hash table. */
6863 used_types_insert (type_name->specs->type);
6865 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6866 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6867 cast_loc);
6869 location_t expr_loc = c_parser_peek_token (parser)->location;
6870 expr = c_parser_cast_expression (parser, NULL);
6871 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6873 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6874 if (ret.value && expr.value)
6875 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6876 ret.original_code = ERROR_MARK;
6877 ret.original_type = NULL;
6878 return ret;
6880 else
6881 return c_parser_unary_expression (parser);
6884 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6886 unary-expression:
6887 postfix-expression
6888 ++ unary-expression
6889 -- unary-expression
6890 unary-operator cast-expression
6891 sizeof unary-expression
6892 sizeof ( type-name )
6894 unary-operator: one of
6895 & * + - ~ !
6897 GNU extensions:
6899 unary-expression:
6900 __alignof__ unary-expression
6901 __alignof__ ( type-name )
6902 && identifier
6904 (C11 permits _Alignof with type names only.)
6906 unary-operator: one of
6907 __extension__ __real__ __imag__
6909 Transactional Memory:
6911 unary-expression:
6912 transaction-expression
6914 In addition, the GNU syntax treats ++ and -- as unary operators, so
6915 they may be applied to cast expressions with errors for non-lvalues
6916 given later. */
6918 static struct c_expr
6919 c_parser_unary_expression (c_parser *parser)
6921 int ext;
6922 struct c_expr ret, op;
6923 location_t op_loc = c_parser_peek_token (parser)->location;
6924 location_t exp_loc;
6925 location_t finish;
6926 ret.original_code = ERROR_MARK;
6927 ret.original_type = NULL;
6928 switch (c_parser_peek_token (parser)->type)
6930 case CPP_PLUS_PLUS:
6931 c_parser_consume_token (parser);
6932 exp_loc = c_parser_peek_token (parser)->location;
6933 op = c_parser_cast_expression (parser, NULL);
6935 /* If there is array notations in op, we expand them. */
6936 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6937 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6938 else
6940 op = default_function_array_read_conversion (exp_loc, op);
6941 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6943 case CPP_MINUS_MINUS:
6944 c_parser_consume_token (parser);
6945 exp_loc = c_parser_peek_token (parser)->location;
6946 op = c_parser_cast_expression (parser, NULL);
6948 /* If there is array notations in op, we expand them. */
6949 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6950 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6951 else
6953 op = default_function_array_read_conversion (exp_loc, op);
6954 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6956 case CPP_AND:
6957 c_parser_consume_token (parser);
6958 op = c_parser_cast_expression (parser, NULL);
6959 mark_exp_read (op.value);
6960 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6961 case CPP_MULT:
6963 c_parser_consume_token (parser);
6964 exp_loc = c_parser_peek_token (parser)->location;
6965 op = c_parser_cast_expression (parser, NULL);
6966 finish = op.get_finish ();
6967 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6968 location_t combined_loc = make_location (op_loc, op_loc, finish);
6969 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
6970 ret.src_range.m_start = op_loc;
6971 ret.src_range.m_finish = finish;
6972 return ret;
6974 case CPP_PLUS:
6975 if (!c_dialect_objc () && !in_system_header_at (input_location))
6976 warning_at (op_loc,
6977 OPT_Wtraditional,
6978 "traditional C rejects the unary plus operator");
6979 c_parser_consume_token (parser);
6980 exp_loc = c_parser_peek_token (parser)->location;
6981 op = c_parser_cast_expression (parser, NULL);
6982 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6983 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6984 case CPP_MINUS:
6985 c_parser_consume_token (parser);
6986 exp_loc = c_parser_peek_token (parser)->location;
6987 op = c_parser_cast_expression (parser, NULL);
6988 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6989 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6990 case CPP_COMPL:
6991 c_parser_consume_token (parser);
6992 exp_loc = c_parser_peek_token (parser)->location;
6993 op = c_parser_cast_expression (parser, NULL);
6994 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6995 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6996 case CPP_NOT:
6997 c_parser_consume_token (parser);
6998 exp_loc = c_parser_peek_token (parser)->location;
6999 op = c_parser_cast_expression (parser, NULL);
7000 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7001 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7002 case CPP_AND_AND:
7003 /* Refer to the address of a label as a pointer. */
7004 c_parser_consume_token (parser);
7005 if (c_parser_next_token_is (parser, CPP_NAME))
7007 ret.value = finish_label_address_expr
7008 (c_parser_peek_token (parser)->value, op_loc);
7009 set_c_expr_source_range (&ret, op_loc,
7010 c_parser_peek_token (parser)->get_finish ());
7011 c_parser_consume_token (parser);
7013 else
7015 c_parser_error (parser, "expected identifier");
7016 ret.value = error_mark_node;
7018 return ret;
7019 case CPP_KEYWORD:
7020 switch (c_parser_peek_token (parser)->keyword)
7022 case RID_SIZEOF:
7023 return c_parser_sizeof_expression (parser);
7024 case RID_ALIGNOF:
7025 return c_parser_alignof_expression (parser);
7026 case RID_EXTENSION:
7027 c_parser_consume_token (parser);
7028 ext = disable_extension_diagnostics ();
7029 ret = c_parser_cast_expression (parser, NULL);
7030 restore_extension_diagnostics (ext);
7031 return ret;
7032 case RID_REALPART:
7033 c_parser_consume_token (parser);
7034 exp_loc = c_parser_peek_token (parser)->location;
7035 op = c_parser_cast_expression (parser, NULL);
7036 op = default_function_array_conversion (exp_loc, op);
7037 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7038 case RID_IMAGPART:
7039 c_parser_consume_token (parser);
7040 exp_loc = c_parser_peek_token (parser)->location;
7041 op = c_parser_cast_expression (parser, NULL);
7042 op = default_function_array_conversion (exp_loc, op);
7043 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7044 case RID_TRANSACTION_ATOMIC:
7045 case RID_TRANSACTION_RELAXED:
7046 return c_parser_transaction_expression (parser,
7047 c_parser_peek_token (parser)->keyword);
7048 default:
7049 return c_parser_postfix_expression (parser);
7051 default:
7052 return c_parser_postfix_expression (parser);
7056 /* Parse a sizeof expression. */
7058 static struct c_expr
7059 c_parser_sizeof_expression (c_parser *parser)
7061 struct c_expr expr;
7062 struct c_expr result;
7063 location_t expr_loc;
7064 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7066 location_t start;
7067 location_t finish = UNKNOWN_LOCATION;
7069 start = c_parser_peek_token (parser)->location;
7071 c_parser_consume_token (parser);
7072 c_inhibit_evaluation_warnings++;
7073 in_sizeof++;
7074 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7075 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7077 /* Either sizeof ( type-name ) or sizeof unary-expression
7078 starting with a compound literal. */
7079 struct c_type_name *type_name;
7080 c_parser_consume_token (parser);
7081 expr_loc = c_parser_peek_token (parser)->location;
7082 type_name = c_parser_type_name (parser);
7083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7084 finish = parser->tokens_buf[0].location;
7085 if (type_name == NULL)
7087 struct c_expr ret;
7088 c_inhibit_evaluation_warnings--;
7089 in_sizeof--;
7090 ret.value = error_mark_node;
7091 ret.original_code = ERROR_MARK;
7092 ret.original_type = NULL;
7093 return ret;
7095 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7097 expr = c_parser_postfix_expression_after_paren_type (parser,
7098 type_name,
7099 expr_loc);
7100 finish = expr.get_finish ();
7101 goto sizeof_expr;
7103 /* sizeof ( type-name ). */
7104 c_inhibit_evaluation_warnings--;
7105 in_sizeof--;
7106 result = c_expr_sizeof_type (expr_loc, type_name);
7108 else
7110 expr_loc = c_parser_peek_token (parser)->location;
7111 expr = c_parser_unary_expression (parser);
7112 finish = expr.get_finish ();
7113 sizeof_expr:
7114 c_inhibit_evaluation_warnings--;
7115 in_sizeof--;
7116 mark_exp_read (expr.value);
7117 if (TREE_CODE (expr.value) == COMPONENT_REF
7118 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7119 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7120 result = c_expr_sizeof_expr (expr_loc, expr);
7122 if (finish != UNKNOWN_LOCATION)
7123 set_c_expr_source_range (&result, start, finish);
7124 return result;
7127 /* Parse an alignof expression. */
7129 static struct c_expr
7130 c_parser_alignof_expression (c_parser *parser)
7132 struct c_expr expr;
7133 location_t start_loc = c_parser_peek_token (parser)->location;
7134 location_t end_loc;
7135 tree alignof_spelling = c_parser_peek_token (parser)->value;
7136 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7137 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7138 "_Alignof") == 0;
7139 /* A diagnostic is not required for the use of this identifier in
7140 the implementation namespace; only diagnose it for the C11
7141 spelling because of existing code using the other spellings. */
7142 if (is_c11_alignof)
7144 if (flag_isoc99)
7145 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7146 alignof_spelling);
7147 else
7148 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7149 alignof_spelling);
7151 c_parser_consume_token (parser);
7152 c_inhibit_evaluation_warnings++;
7153 in_alignof++;
7154 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7155 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7157 /* Either __alignof__ ( type-name ) or __alignof__
7158 unary-expression starting with a compound literal. */
7159 location_t loc;
7160 struct c_type_name *type_name;
7161 struct c_expr ret;
7162 c_parser_consume_token (parser);
7163 loc = c_parser_peek_token (parser)->location;
7164 type_name = c_parser_type_name (parser);
7165 end_loc = c_parser_peek_token (parser)->location;
7166 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7167 if (type_name == NULL)
7169 struct c_expr ret;
7170 c_inhibit_evaluation_warnings--;
7171 in_alignof--;
7172 ret.value = error_mark_node;
7173 ret.original_code = ERROR_MARK;
7174 ret.original_type = NULL;
7175 return ret;
7177 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7179 expr = c_parser_postfix_expression_after_paren_type (parser,
7180 type_name,
7181 loc);
7182 goto alignof_expr;
7184 /* alignof ( type-name ). */
7185 c_inhibit_evaluation_warnings--;
7186 in_alignof--;
7187 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7188 NULL, NULL),
7189 false, is_c11_alignof, 1);
7190 ret.original_code = ERROR_MARK;
7191 ret.original_type = NULL;
7192 set_c_expr_source_range (&ret, start_loc, end_loc);
7193 return ret;
7195 else
7197 struct c_expr ret;
7198 expr = c_parser_unary_expression (parser);
7199 end_loc = expr.src_range.m_finish;
7200 alignof_expr:
7201 mark_exp_read (expr.value);
7202 c_inhibit_evaluation_warnings--;
7203 in_alignof--;
7204 if (is_c11_alignof)
7205 pedwarn (start_loc,
7206 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7207 alignof_spelling);
7208 ret.value = c_alignof_expr (start_loc, expr.value);
7209 ret.original_code = ERROR_MARK;
7210 ret.original_type = NULL;
7211 set_c_expr_source_range (&ret, start_loc, end_loc);
7212 return ret;
7216 /* Helper function to read arguments of builtins which are interfaces
7217 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7218 others. The name of the builtin is passed using BNAME parameter.
7219 Function returns true if there were no errors while parsing and
7220 stores the arguments in CEXPR_LIST. If it returns true,
7221 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7222 parenthesis. */
7223 static bool
7224 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7225 vec<c_expr_t, va_gc> **ret_cexpr_list,
7226 bool choose_expr_p,
7227 location_t *out_close_paren_loc)
7229 location_t loc = c_parser_peek_token (parser)->location;
7230 vec<c_expr_t, va_gc> *cexpr_list;
7231 c_expr_t expr;
7232 bool saved_force_folding_builtin_constant_p;
7234 *ret_cexpr_list = NULL;
7235 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7237 error_at (loc, "cannot take address of %qs", bname);
7238 return false;
7241 c_parser_consume_token (parser);
7243 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7245 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7246 c_parser_consume_token (parser);
7247 return true;
7250 saved_force_folding_builtin_constant_p
7251 = force_folding_builtin_constant_p;
7252 force_folding_builtin_constant_p |= choose_expr_p;
7253 expr = c_parser_expr_no_commas (parser, NULL);
7254 force_folding_builtin_constant_p
7255 = saved_force_folding_builtin_constant_p;
7256 vec_alloc (cexpr_list, 1);
7257 vec_safe_push (cexpr_list, expr);
7258 while (c_parser_next_token_is (parser, CPP_COMMA))
7260 c_parser_consume_token (parser);
7261 expr = c_parser_expr_no_commas (parser, NULL);
7262 vec_safe_push (cexpr_list, expr);
7265 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7266 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7267 return false;
7269 *ret_cexpr_list = cexpr_list;
7270 return true;
7273 /* This represents a single generic-association. */
7275 struct c_generic_association
7277 /* The location of the starting token of the type. */
7278 location_t type_location;
7279 /* The association's type, or NULL_TREE for 'default'. */
7280 tree type;
7281 /* The association's expression. */
7282 struct c_expr expression;
7285 /* Parse a generic-selection. (C11 6.5.1.1).
7287 generic-selection:
7288 _Generic ( assignment-expression , generic-assoc-list )
7290 generic-assoc-list:
7291 generic-association
7292 generic-assoc-list , generic-association
7294 generic-association:
7295 type-name : assignment-expression
7296 default : assignment-expression
7299 static struct c_expr
7300 c_parser_generic_selection (c_parser *parser)
7302 struct c_expr selector, error_expr;
7303 tree selector_type;
7304 struct c_generic_association matched_assoc;
7305 bool match_found = false;
7306 location_t generic_loc, selector_loc;
7308 error_expr.original_code = ERROR_MARK;
7309 error_expr.original_type = NULL;
7310 error_expr.set_error ();
7311 matched_assoc.type_location = UNKNOWN_LOCATION;
7312 matched_assoc.type = NULL_TREE;
7313 matched_assoc.expression = error_expr;
7315 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7316 generic_loc = c_parser_peek_token (parser)->location;
7317 c_parser_consume_token (parser);
7318 if (flag_isoc99)
7319 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7320 "ISO C99 does not support %<_Generic%>");
7321 else
7322 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7323 "ISO C90 does not support %<_Generic%>");
7325 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7326 return error_expr;
7328 c_inhibit_evaluation_warnings++;
7329 selector_loc = c_parser_peek_token (parser)->location;
7330 selector = c_parser_expr_no_commas (parser, NULL);
7331 selector = default_function_array_conversion (selector_loc, selector);
7332 c_inhibit_evaluation_warnings--;
7334 if (selector.value == error_mark_node)
7336 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7337 return selector;
7339 selector_type = TREE_TYPE (selector.value);
7340 /* In ISO C terms, rvalues (including the controlling expression of
7341 _Generic) do not have qualified types. */
7342 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7343 selector_type = TYPE_MAIN_VARIANT (selector_type);
7344 /* In ISO C terms, _Noreturn is not part of the type of expressions
7345 such as &abort, but in GCC it is represented internally as a type
7346 qualifier. */
7347 if (FUNCTION_POINTER_TYPE_P (selector_type)
7348 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7349 selector_type
7350 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7352 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7354 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7355 return error_expr;
7358 auto_vec<c_generic_association> associations;
7359 while (1)
7361 struct c_generic_association assoc, *iter;
7362 unsigned int ix;
7363 c_token *token = c_parser_peek_token (parser);
7365 assoc.type_location = token->location;
7366 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7368 c_parser_consume_token (parser);
7369 assoc.type = NULL_TREE;
7371 else
7373 struct c_type_name *type_name;
7375 type_name = c_parser_type_name (parser);
7376 if (type_name == NULL)
7378 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7379 return error_expr;
7381 assoc.type = groktypename (type_name, NULL, NULL);
7382 if (assoc.type == error_mark_node)
7384 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7385 return error_expr;
7388 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7389 error_at (assoc.type_location,
7390 "%<_Generic%> association has function type");
7391 else if (!COMPLETE_TYPE_P (assoc.type))
7392 error_at (assoc.type_location,
7393 "%<_Generic%> association has incomplete type");
7395 if (variably_modified_type_p (assoc.type, NULL_TREE))
7396 error_at (assoc.type_location,
7397 "%<_Generic%> association has "
7398 "variable length type");
7401 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7404 return error_expr;
7407 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7408 if (assoc.expression.value == error_mark_node)
7410 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7411 return error_expr;
7414 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7416 if (assoc.type == NULL_TREE)
7418 if (iter->type == NULL_TREE)
7420 error_at (assoc.type_location,
7421 "duplicate %<default%> case in %<_Generic%>");
7422 inform (iter->type_location, "original %<default%> is here");
7425 else if (iter->type != NULL_TREE)
7427 if (comptypes (assoc.type, iter->type))
7429 error_at (assoc.type_location,
7430 "%<_Generic%> specifies two compatible types");
7431 inform (iter->type_location, "compatible type is here");
7436 if (assoc.type == NULL_TREE)
7438 if (!match_found)
7440 matched_assoc = assoc;
7441 match_found = true;
7444 else if (comptypes (assoc.type, selector_type))
7446 if (!match_found || matched_assoc.type == NULL_TREE)
7448 matched_assoc = assoc;
7449 match_found = true;
7451 else
7453 error_at (assoc.type_location,
7454 "%<_Generic> selector matches multiple associations");
7455 inform (matched_assoc.type_location,
7456 "other match is here");
7460 associations.safe_push (assoc);
7462 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7463 break;
7464 c_parser_consume_token (parser);
7467 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7469 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7470 return error_expr;
7473 if (!match_found)
7475 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7476 "compatible with any association",
7477 selector_type);
7478 return error_expr;
7481 return matched_assoc.expression;
7484 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7486 postfix-expression:
7487 primary-expression
7488 postfix-expression [ expression ]
7489 postfix-expression ( argument-expression-list[opt] )
7490 postfix-expression . identifier
7491 postfix-expression -> identifier
7492 postfix-expression ++
7493 postfix-expression --
7494 ( type-name ) { initializer-list }
7495 ( type-name ) { initializer-list , }
7497 argument-expression-list:
7498 argument-expression
7499 argument-expression-list , argument-expression
7501 primary-expression:
7502 identifier
7503 constant
7504 string-literal
7505 ( expression )
7506 generic-selection
7508 GNU extensions:
7510 primary-expression:
7511 __func__
7512 (treated as a keyword in GNU C)
7513 __FUNCTION__
7514 __PRETTY_FUNCTION__
7515 ( compound-statement )
7516 __builtin_va_arg ( assignment-expression , type-name )
7517 __builtin_offsetof ( type-name , offsetof-member-designator )
7518 __builtin_choose_expr ( assignment-expression ,
7519 assignment-expression ,
7520 assignment-expression )
7521 __builtin_types_compatible_p ( type-name , type-name )
7522 __builtin_complex ( assignment-expression , assignment-expression )
7523 __builtin_shuffle ( assignment-expression , assignment-expression )
7524 __builtin_shuffle ( assignment-expression ,
7525 assignment-expression ,
7526 assignment-expression, )
7528 offsetof-member-designator:
7529 identifier
7530 offsetof-member-designator . identifier
7531 offsetof-member-designator [ expression ]
7533 Objective-C:
7535 primary-expression:
7536 [ objc-receiver objc-message-args ]
7537 @selector ( objc-selector-arg )
7538 @protocol ( identifier )
7539 @encode ( type-name )
7540 objc-string-literal
7541 Classname . identifier
7544 static struct c_expr
7545 c_parser_postfix_expression (c_parser *parser)
7547 struct c_expr expr, e1;
7548 struct c_type_name *t1, *t2;
7549 location_t loc = c_parser_peek_token (parser)->location;;
7550 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7551 expr.original_code = ERROR_MARK;
7552 expr.original_type = NULL;
7553 switch (c_parser_peek_token (parser)->type)
7555 case CPP_NUMBER:
7556 expr.value = c_parser_peek_token (parser)->value;
7557 set_c_expr_source_range (&expr, tok_range);
7558 loc = c_parser_peek_token (parser)->location;
7559 c_parser_consume_token (parser);
7560 if (TREE_CODE (expr.value) == FIXED_CST
7561 && !targetm.fixed_point_supported_p ())
7563 error_at (loc, "fixed-point types not supported for this target");
7564 expr.value = error_mark_node;
7566 break;
7567 case CPP_CHAR:
7568 case CPP_CHAR16:
7569 case CPP_CHAR32:
7570 case CPP_WCHAR:
7571 expr.value = c_parser_peek_token (parser)->value;
7572 /* For the purpose of warning when a pointer is compared with
7573 a zero character constant. */
7574 expr.original_type = char_type_node;
7575 set_c_expr_source_range (&expr, tok_range);
7576 c_parser_consume_token (parser);
7577 break;
7578 case CPP_STRING:
7579 case CPP_STRING16:
7580 case CPP_STRING32:
7581 case CPP_WSTRING:
7582 case CPP_UTF8STRING:
7583 expr.value = c_parser_peek_token (parser)->value;
7584 set_c_expr_source_range (&expr, tok_range);
7585 expr.original_code = STRING_CST;
7586 c_parser_consume_token (parser);
7587 break;
7588 case CPP_OBJC_STRING:
7589 gcc_assert (c_dialect_objc ());
7590 expr.value
7591 = objc_build_string_object (c_parser_peek_token (parser)->value);
7592 set_c_expr_source_range (&expr, tok_range);
7593 c_parser_consume_token (parser);
7594 break;
7595 case CPP_NAME:
7596 switch (c_parser_peek_token (parser)->id_kind)
7598 case C_ID_ID:
7600 tree id = c_parser_peek_token (parser)->value;
7601 c_parser_consume_token (parser);
7602 expr.value = build_external_ref (loc, id,
7603 (c_parser_peek_token (parser)->type
7604 == CPP_OPEN_PAREN),
7605 &expr.original_type);
7606 set_c_expr_source_range (&expr, tok_range);
7607 break;
7609 case C_ID_CLASSNAME:
7611 /* Here we parse the Objective-C 2.0 Class.name dot
7612 syntax. */
7613 tree class_name = c_parser_peek_token (parser)->value;
7614 tree component;
7615 c_parser_consume_token (parser);
7616 gcc_assert (c_dialect_objc ());
7617 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7619 expr.set_error ();
7620 break;
7622 if (c_parser_next_token_is_not (parser, CPP_NAME))
7624 c_parser_error (parser, "expected identifier");
7625 expr.set_error ();
7626 break;
7628 c_token *component_tok = c_parser_peek_token (parser);
7629 component = component_tok->value;
7630 location_t end_loc = component_tok->get_finish ();
7631 c_parser_consume_token (parser);
7632 expr.value = objc_build_class_component_ref (class_name,
7633 component);
7634 set_c_expr_source_range (&expr, loc, end_loc);
7635 break;
7637 default:
7638 c_parser_error (parser, "expected expression");
7639 expr.set_error ();
7640 break;
7642 break;
7643 case CPP_OPEN_PAREN:
7644 /* A parenthesized expression, statement expression or compound
7645 literal. */
7646 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7648 /* A statement expression. */
7649 tree stmt;
7650 location_t brace_loc;
7651 c_parser_consume_token (parser);
7652 brace_loc = c_parser_peek_token (parser)->location;
7653 c_parser_consume_token (parser);
7654 if (!building_stmt_list_p ())
7656 error_at (loc, "braced-group within expression allowed "
7657 "only inside a function");
7658 parser->error = true;
7659 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7660 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7661 expr.set_error ();
7662 break;
7664 stmt = c_begin_stmt_expr ();
7665 c_parser_compound_statement_nostart (parser);
7666 location_t close_loc = c_parser_peek_token (parser)->location;
7667 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7668 "expected %<)%>");
7669 pedwarn (loc, OPT_Wpedantic,
7670 "ISO C forbids braced-groups within expressions");
7671 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7672 set_c_expr_source_range (&expr, loc, close_loc);
7673 mark_exp_read (expr.value);
7675 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7677 /* A compound literal. ??? Can we actually get here rather
7678 than going directly to
7679 c_parser_postfix_expression_after_paren_type from
7680 elsewhere? */
7681 location_t loc;
7682 struct c_type_name *type_name;
7683 c_parser_consume_token (parser);
7684 loc = c_parser_peek_token (parser)->location;
7685 type_name = c_parser_type_name (parser);
7686 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7687 "expected %<)%>");
7688 if (type_name == NULL)
7690 expr.set_error ();
7692 else
7693 expr = c_parser_postfix_expression_after_paren_type (parser,
7694 type_name,
7695 loc);
7697 else
7699 /* A parenthesized expression. */
7700 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7701 c_parser_consume_token (parser);
7702 expr = c_parser_expression (parser);
7703 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7704 TREE_NO_WARNING (expr.value) = 1;
7705 if (expr.original_code != C_MAYBE_CONST_EXPR)
7706 expr.original_code = ERROR_MARK;
7707 /* Don't change EXPR.ORIGINAL_TYPE. */
7708 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7709 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7710 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7711 "expected %<)%>");
7713 break;
7714 case CPP_KEYWORD:
7715 switch (c_parser_peek_token (parser)->keyword)
7717 case RID_FUNCTION_NAME:
7718 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7719 "%<__FUNCTION__%> predefined identifier");
7720 expr.value = fname_decl (loc,
7721 c_parser_peek_token (parser)->keyword,
7722 c_parser_peek_token (parser)->value);
7723 set_c_expr_source_range (&expr, loc, loc);
7724 c_parser_consume_token (parser);
7725 break;
7726 case RID_PRETTY_FUNCTION_NAME:
7727 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7728 "%<__PRETTY_FUNCTION__%> predefined identifier");
7729 expr.value = fname_decl (loc,
7730 c_parser_peek_token (parser)->keyword,
7731 c_parser_peek_token (parser)->value);
7732 set_c_expr_source_range (&expr, loc, loc);
7733 c_parser_consume_token (parser);
7734 break;
7735 case RID_C99_FUNCTION_NAME:
7736 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7737 "%<__func__%> predefined identifier");
7738 expr.value = fname_decl (loc,
7739 c_parser_peek_token (parser)->keyword,
7740 c_parser_peek_token (parser)->value);
7741 set_c_expr_source_range (&expr, loc, loc);
7742 c_parser_consume_token (parser);
7743 break;
7744 case RID_VA_ARG:
7746 location_t start_loc = loc;
7747 c_parser_consume_token (parser);
7748 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7750 expr.set_error ();
7751 break;
7753 e1 = c_parser_expr_no_commas (parser, NULL);
7754 mark_exp_read (e1.value);
7755 e1.value = c_fully_fold (e1.value, false, NULL);
7756 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7758 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7759 expr.set_error ();
7760 break;
7762 loc = c_parser_peek_token (parser)->location;
7763 t1 = c_parser_type_name (parser);
7764 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7765 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7766 "expected %<)%>");
7767 if (t1 == NULL)
7769 expr.set_error ();
7771 else
7773 tree type_expr = NULL_TREE;
7774 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7775 groktypename (t1, &type_expr, NULL));
7776 if (type_expr)
7778 expr.value = build2 (C_MAYBE_CONST_EXPR,
7779 TREE_TYPE (expr.value), type_expr,
7780 expr.value);
7781 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7783 set_c_expr_source_range (&expr, start_loc, end_loc);
7786 break;
7787 case RID_OFFSETOF:
7788 c_parser_consume_token (parser);
7789 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7791 expr.set_error ();
7792 break;
7794 t1 = c_parser_type_name (parser);
7795 if (t1 == NULL)
7796 parser->error = true;
7797 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7798 gcc_assert (parser->error);
7799 if (parser->error)
7801 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7802 expr.set_error ();
7803 break;
7807 tree type = groktypename (t1, NULL, NULL);
7808 tree offsetof_ref;
7809 if (type == error_mark_node)
7810 offsetof_ref = error_mark_node;
7811 else
7813 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7814 SET_EXPR_LOCATION (offsetof_ref, loc);
7816 /* Parse the second argument to __builtin_offsetof. We
7817 must have one identifier, and beyond that we want to
7818 accept sub structure and sub array references. */
7819 if (c_parser_next_token_is (parser, CPP_NAME))
7821 c_token *comp_tok = c_parser_peek_token (parser);
7822 offsetof_ref = build_component_ref
7823 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
7824 c_parser_consume_token (parser);
7825 while (c_parser_next_token_is (parser, CPP_DOT)
7826 || c_parser_next_token_is (parser,
7827 CPP_OPEN_SQUARE)
7828 || c_parser_next_token_is (parser,
7829 CPP_DEREF))
7831 if (c_parser_next_token_is (parser, CPP_DEREF))
7833 loc = c_parser_peek_token (parser)->location;
7834 offsetof_ref = build_array_ref (loc,
7835 offsetof_ref,
7836 integer_zero_node);
7837 goto do_dot;
7839 else if (c_parser_next_token_is (parser, CPP_DOT))
7841 do_dot:
7842 c_parser_consume_token (parser);
7843 if (c_parser_next_token_is_not (parser,
7844 CPP_NAME))
7846 c_parser_error (parser, "expected identifier");
7847 break;
7849 c_token *comp_tok = c_parser_peek_token (parser);
7850 offsetof_ref = build_component_ref
7851 (loc, offsetof_ref, comp_tok->value,
7852 comp_tok->location);
7853 c_parser_consume_token (parser);
7855 else
7857 struct c_expr ce;
7858 tree idx;
7859 loc = c_parser_peek_token (parser)->location;
7860 c_parser_consume_token (parser);
7861 ce = c_parser_expression (parser);
7862 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7863 idx = ce.value;
7864 idx = c_fully_fold (idx, false, NULL);
7865 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7866 "expected %<]%>");
7867 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7871 else
7872 c_parser_error (parser, "expected identifier");
7873 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7874 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7875 "expected %<)%>");
7876 expr.value = fold_offsetof (offsetof_ref);
7877 set_c_expr_source_range (&expr, loc, end_loc);
7879 break;
7880 case RID_CHOOSE_EXPR:
7882 vec<c_expr_t, va_gc> *cexpr_list;
7883 c_expr_t *e1_p, *e2_p, *e3_p;
7884 tree c;
7885 location_t close_paren_loc;
7887 c_parser_consume_token (parser);
7888 if (!c_parser_get_builtin_args (parser,
7889 "__builtin_choose_expr",
7890 &cexpr_list, true,
7891 &close_paren_loc))
7893 expr.set_error ();
7894 break;
7897 if (vec_safe_length (cexpr_list) != 3)
7899 error_at (loc, "wrong number of arguments to "
7900 "%<__builtin_choose_expr%>");
7901 expr.set_error ();
7902 break;
7905 e1_p = &(*cexpr_list)[0];
7906 e2_p = &(*cexpr_list)[1];
7907 e3_p = &(*cexpr_list)[2];
7909 c = e1_p->value;
7910 mark_exp_read (e2_p->value);
7911 mark_exp_read (e3_p->value);
7912 if (TREE_CODE (c) != INTEGER_CST
7913 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7914 error_at (loc,
7915 "first argument to %<__builtin_choose_expr%> not"
7916 " a constant");
7917 constant_expression_warning (c);
7918 expr = integer_zerop (c) ? *e3_p : *e2_p;
7919 set_c_expr_source_range (&expr, loc, close_paren_loc);
7920 break;
7922 case RID_TYPES_COMPATIBLE_P:
7923 c_parser_consume_token (parser);
7924 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7926 expr.set_error ();
7927 break;
7929 t1 = c_parser_type_name (parser);
7930 if (t1 == NULL)
7932 expr.set_error ();
7933 break;
7935 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7937 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7938 expr.set_error ();
7939 break;
7941 t2 = c_parser_type_name (parser);
7942 if (t2 == NULL)
7944 expr.set_error ();
7945 break;
7948 location_t close_paren_loc = c_parser_peek_token (parser)->location;
7949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7950 "expected %<)%>");
7951 tree e1, e2;
7952 e1 = groktypename (t1, NULL, NULL);
7953 e2 = groktypename (t2, NULL, NULL);
7954 if (e1 == error_mark_node || e2 == error_mark_node)
7956 expr.set_error ();
7957 break;
7960 e1 = TYPE_MAIN_VARIANT (e1);
7961 e2 = TYPE_MAIN_VARIANT (e2);
7963 expr.value
7964 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7965 set_c_expr_source_range (&expr, loc, close_paren_loc);
7967 break;
7968 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7970 vec<c_expr_t, va_gc> *cexpr_list;
7971 c_expr_t *e2_p;
7972 tree chain_value;
7973 location_t close_paren_loc;
7975 c_parser_consume_token (parser);
7976 if (!c_parser_get_builtin_args (parser,
7977 "__builtin_call_with_static_chain",
7978 &cexpr_list, false,
7979 &close_paren_loc))
7981 expr.set_error ();
7982 break;
7984 if (vec_safe_length (cexpr_list) != 2)
7986 error_at (loc, "wrong number of arguments to "
7987 "%<__builtin_call_with_static_chain%>");
7988 expr.set_error ();
7989 break;
7992 expr = (*cexpr_list)[0];
7993 e2_p = &(*cexpr_list)[1];
7994 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7995 chain_value = e2_p->value;
7996 mark_exp_read (chain_value);
7998 if (TREE_CODE (expr.value) != CALL_EXPR)
7999 error_at (loc, "first argument to "
8000 "%<__builtin_call_with_static_chain%> "
8001 "must be a call expression");
8002 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8003 error_at (loc, "second argument to "
8004 "%<__builtin_call_with_static_chain%> "
8005 "must be a pointer type");
8006 else
8007 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8008 set_c_expr_source_range (&expr, loc, close_paren_loc);
8009 break;
8011 case RID_BUILTIN_COMPLEX:
8013 vec<c_expr_t, va_gc> *cexpr_list;
8014 c_expr_t *e1_p, *e2_p;
8015 location_t close_paren_loc;
8017 c_parser_consume_token (parser);
8018 if (!c_parser_get_builtin_args (parser,
8019 "__builtin_complex",
8020 &cexpr_list, false,
8021 &close_paren_loc))
8023 expr.set_error ();
8024 break;
8027 if (vec_safe_length (cexpr_list) != 2)
8029 error_at (loc, "wrong number of arguments to "
8030 "%<__builtin_complex%>");
8031 expr.set_error ();
8032 break;
8035 e1_p = &(*cexpr_list)[0];
8036 e2_p = &(*cexpr_list)[1];
8038 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8039 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8040 e1_p->value = convert (TREE_TYPE (e1_p->value),
8041 TREE_OPERAND (e1_p->value, 0));
8042 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8043 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8044 e2_p->value = convert (TREE_TYPE (e2_p->value),
8045 TREE_OPERAND (e2_p->value, 0));
8046 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8047 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8048 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8049 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8051 error_at (loc, "%<__builtin_complex%> operand "
8052 "not of real binary floating-point type");
8053 expr.set_error ();
8054 break;
8056 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8057 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8059 error_at (loc,
8060 "%<__builtin_complex%> operands of different types");
8061 expr.set_error ();
8062 break;
8064 pedwarn_c90 (loc, OPT_Wpedantic,
8065 "ISO C90 does not support complex types");
8066 expr.value = build2_loc (loc, COMPLEX_EXPR,
8067 build_complex_type
8068 (TYPE_MAIN_VARIANT
8069 (TREE_TYPE (e1_p->value))),
8070 e1_p->value, e2_p->value);
8071 set_c_expr_source_range (&expr, loc, close_paren_loc);
8072 break;
8074 case RID_BUILTIN_SHUFFLE:
8076 vec<c_expr_t, va_gc> *cexpr_list;
8077 unsigned int i;
8078 c_expr_t *p;
8079 location_t close_paren_loc;
8081 c_parser_consume_token (parser);
8082 if (!c_parser_get_builtin_args (parser,
8083 "__builtin_shuffle",
8084 &cexpr_list, false,
8085 &close_paren_loc))
8087 expr.set_error ();
8088 break;
8091 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8092 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8094 if (vec_safe_length (cexpr_list) == 2)
8095 expr.value =
8096 c_build_vec_perm_expr
8097 (loc, (*cexpr_list)[0].value,
8098 NULL_TREE, (*cexpr_list)[1].value);
8100 else if (vec_safe_length (cexpr_list) == 3)
8101 expr.value =
8102 c_build_vec_perm_expr
8103 (loc, (*cexpr_list)[0].value,
8104 (*cexpr_list)[1].value,
8105 (*cexpr_list)[2].value);
8106 else
8108 error_at (loc, "wrong number of arguments to "
8109 "%<__builtin_shuffle%>");
8110 expr.set_error ();
8112 set_c_expr_source_range (&expr, loc, close_paren_loc);
8113 break;
8115 case RID_AT_SELECTOR:
8116 gcc_assert (c_dialect_objc ());
8117 c_parser_consume_token (parser);
8118 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8120 expr.set_error ();
8121 break;
8124 tree sel = c_parser_objc_selector_arg (parser);
8125 location_t close_loc = c_parser_peek_token (parser)->location;
8126 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8127 "expected %<)%>");
8128 expr.value = objc_build_selector_expr (loc, sel);
8129 set_c_expr_source_range (&expr, loc, close_loc);
8131 break;
8132 case RID_AT_PROTOCOL:
8133 gcc_assert (c_dialect_objc ());
8134 c_parser_consume_token (parser);
8135 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8137 expr.set_error ();
8138 break;
8140 if (c_parser_next_token_is_not (parser, CPP_NAME))
8142 c_parser_error (parser, "expected identifier");
8143 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8144 expr.set_error ();
8145 break;
8148 tree id = c_parser_peek_token (parser)->value;
8149 c_parser_consume_token (parser);
8150 location_t close_loc = c_parser_peek_token (parser)->location;
8151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8152 "expected %<)%>");
8153 expr.value = objc_build_protocol_expr (id);
8154 set_c_expr_source_range (&expr, loc, close_loc);
8156 break;
8157 case RID_AT_ENCODE:
8158 /* Extension to support C-structures in the archiver. */
8159 gcc_assert (c_dialect_objc ());
8160 c_parser_consume_token (parser);
8161 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8163 expr.set_error ();
8164 break;
8166 t1 = c_parser_type_name (parser);
8167 if (t1 == NULL)
8169 expr.set_error ();
8170 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8171 break;
8174 location_t close_loc = c_parser_peek_token (parser)->location;
8175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8176 "expected %<)%>");
8177 tree type = groktypename (t1, NULL, NULL);
8178 expr.value = objc_build_encode_expr (type);
8179 set_c_expr_source_range (&expr, loc, close_loc);
8181 break;
8182 case RID_GENERIC:
8183 expr = c_parser_generic_selection (parser);
8184 break;
8185 case RID_CILK_SPAWN:
8186 c_parser_consume_token (parser);
8187 if (!flag_cilkplus)
8189 error_at (loc, "-fcilkplus must be enabled to use "
8190 "%<_Cilk_spawn%>");
8191 expr = c_parser_cast_expression (parser, NULL);
8192 expr.set_error ();
8194 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8196 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
8197 "are not permitted");
8198 /* Now flush out all the _Cilk_spawns. */
8199 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8200 c_parser_consume_token (parser);
8201 expr = c_parser_cast_expression (parser, NULL);
8203 else
8205 expr = c_parser_cast_expression (parser, NULL);
8206 expr.value = build_cilk_spawn (loc, expr.value);
8208 break;
8209 default:
8210 c_parser_error (parser, "expected expression");
8211 expr.set_error ();
8212 break;
8214 break;
8215 case CPP_OPEN_SQUARE:
8216 if (c_dialect_objc ())
8218 tree receiver, args;
8219 c_parser_consume_token (parser);
8220 receiver = c_parser_objc_receiver (parser);
8221 args = c_parser_objc_message_args (parser);
8222 location_t close_loc = c_parser_peek_token (parser)->location;
8223 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8224 "expected %<]%>");
8225 expr.value = objc_build_message_expr (receiver, args);
8226 set_c_expr_source_range (&expr, loc, close_loc);
8227 break;
8229 /* Else fall through to report error. */
8230 /* FALLTHRU */
8231 default:
8232 c_parser_error (parser, "expected expression");
8233 expr.set_error ();
8234 break;
8236 return c_parser_postfix_expression_after_primary
8237 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8240 /* Parse a postfix expression after a parenthesized type name: the
8241 brace-enclosed initializer of a compound literal, possibly followed
8242 by some postfix operators. This is separate because it is not
8243 possible to tell until after the type name whether a cast
8244 expression has a cast or a compound literal, or whether the operand
8245 of sizeof is a parenthesized type name or starts with a compound
8246 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8247 location of the first token after the parentheses around the type
8248 name. */
8250 static struct c_expr
8251 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8252 struct c_type_name *type_name,
8253 location_t type_loc)
8255 tree type;
8256 struct c_expr init;
8257 bool non_const;
8258 struct c_expr expr;
8259 location_t start_loc;
8260 tree type_expr = NULL_TREE;
8261 bool type_expr_const = true;
8262 check_compound_literal_type (type_loc, type_name);
8263 rich_location richloc (line_table, type_loc);
8264 start_init (NULL_TREE, NULL, 0, &richloc);
8265 type = groktypename (type_name, &type_expr, &type_expr_const);
8266 start_loc = c_parser_peek_token (parser)->location;
8267 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8269 error_at (type_loc, "compound literal has variable size");
8270 type = error_mark_node;
8272 init = c_parser_braced_init (parser, type, false, NULL);
8273 finish_init ();
8274 maybe_warn_string_init (type_loc, type, init);
8276 if (type != error_mark_node
8277 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8278 && current_function_decl)
8280 error ("compound literal qualified by address-space qualifier");
8281 type = error_mark_node;
8284 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8285 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8286 ? CONSTRUCTOR_NON_CONST (init.value)
8287 : init.original_code == C_MAYBE_CONST_EXPR);
8288 non_const |= !type_expr_const;
8289 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8290 set_c_expr_source_range (&expr, init.src_range);
8291 expr.original_code = ERROR_MARK;
8292 expr.original_type = NULL;
8293 if (type != error_mark_node
8294 && expr.value != error_mark_node
8295 && type_expr)
8297 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8299 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8300 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8302 else
8304 gcc_assert (!non_const);
8305 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8306 type_expr, expr.value);
8309 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8312 /* Callback function for sizeof_pointer_memaccess_warning to compare
8313 types. */
8315 static bool
8316 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8318 return comptypes (type1, type2) == 1;
8321 /* Parse a postfix expression after the initial primary or compound
8322 literal; that is, parse a series of postfix operators.
8324 EXPR_LOC is the location of the primary expression. */
8326 static struct c_expr
8327 c_parser_postfix_expression_after_primary (c_parser *parser,
8328 location_t expr_loc,
8329 struct c_expr expr)
8331 struct c_expr orig_expr;
8332 tree ident, idx;
8333 location_t sizeof_arg_loc[3], comp_loc;
8334 tree sizeof_arg[3];
8335 unsigned int literal_zero_mask;
8336 unsigned int i;
8337 vec<tree, va_gc> *exprlist;
8338 vec<tree, va_gc> *origtypes = NULL;
8339 vec<location_t> arg_loc = vNULL;
8340 location_t start;
8341 location_t finish;
8343 while (true)
8345 location_t op_loc = c_parser_peek_token (parser)->location;
8346 switch (c_parser_peek_token (parser)->type)
8348 case CPP_OPEN_SQUARE:
8349 /* Array reference. */
8350 c_parser_consume_token (parser);
8351 if (flag_cilkplus
8352 && c_parser_peek_token (parser)->type == CPP_COLON)
8353 /* If we are here, then we have something like this:
8354 Array [ : ]
8356 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8357 expr.value);
8358 else
8360 idx = c_parser_expression (parser).value;
8361 /* Here we have 3 options:
8362 1. Array [EXPR] -- Normal Array call.
8363 2. Array [EXPR : EXPR] -- Array notation without stride.
8364 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8366 For 1, we just handle it just like a normal array expression.
8367 For 2 and 3 we handle it like we handle array notations. The
8368 idx value we have above becomes the initial/start index.
8370 if (flag_cilkplus
8371 && c_parser_peek_token (parser)->type == CPP_COLON)
8372 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8373 expr.value);
8374 else
8376 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8377 "expected %<]%>");
8378 start = expr.get_start ();
8379 finish = parser->tokens_buf[0].location;
8380 expr.value = build_array_ref (op_loc, expr.value, idx);
8381 set_c_expr_source_range (&expr, start, finish);
8384 expr.original_code = ERROR_MARK;
8385 expr.original_type = NULL;
8386 break;
8387 case CPP_OPEN_PAREN:
8388 /* Function call. */
8389 c_parser_consume_token (parser);
8390 for (i = 0; i < 3; i++)
8392 sizeof_arg[i] = NULL_TREE;
8393 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8395 literal_zero_mask = 0;
8396 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8397 exprlist = NULL;
8398 else
8399 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8400 sizeof_arg_loc, sizeof_arg,
8401 &arg_loc, &literal_zero_mask);
8402 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8403 "expected %<)%>");
8404 orig_expr = expr;
8405 mark_exp_read (expr.value);
8406 if (warn_sizeof_pointer_memaccess)
8407 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8408 expr.value, exprlist,
8409 sizeof_arg,
8410 sizeof_ptr_memacc_comptypes);
8411 if (TREE_CODE (expr.value) == FUNCTION_DECL
8412 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8413 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8414 && vec_safe_length (exprlist) == 3)
8416 tree arg0 = (*exprlist)[0];
8417 tree arg2 = (*exprlist)[2];
8418 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
8421 if (TREE_CODE (expr.value) == FUNCTION_DECL && warn_restrict)
8423 unsigned i;
8424 tree arg;
8425 FOR_EACH_VEC_SAFE_ELT (exprlist, i, arg)
8426 TREE_VISITED (arg) = 0;
8428 unsigned param_pos = 0;
8429 function_args_iterator iter;
8430 tree t;
8431 FOREACH_FUNCTION_ARGS (TREE_TYPE (expr.value), t, iter)
8433 if (POINTER_TYPE_P (t) && TYPE_RESTRICT (t)
8434 && !TYPE_READONLY (TREE_TYPE (t)))
8435 warn_for_restrict (param_pos, exprlist);
8436 param_pos++;
8439 FOR_EACH_VEC_SAFE_ELT (exprlist, i, arg)
8440 TREE_VISITED (arg) = 0;
8443 start = expr.get_start ();
8444 finish = parser->tokens_buf[0].get_finish ();
8445 expr.value
8446 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8447 exprlist, origtypes);
8448 set_c_expr_source_range (&expr, start, finish);
8450 expr.original_code = ERROR_MARK;
8451 if (TREE_CODE (expr.value) == INTEGER_CST
8452 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8453 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8454 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8455 expr.original_code = C_MAYBE_CONST_EXPR;
8456 expr.original_type = NULL;
8457 if (exprlist)
8459 release_tree_vector (exprlist);
8460 release_tree_vector (origtypes);
8462 arg_loc.release ();
8463 break;
8464 case CPP_DOT:
8465 /* Structure element reference. */
8466 c_parser_consume_token (parser);
8467 expr = default_function_array_conversion (expr_loc, expr);
8468 if (c_parser_next_token_is (parser, CPP_NAME))
8470 c_token *comp_tok = c_parser_peek_token (parser);
8471 ident = comp_tok->value;
8472 comp_loc = comp_tok->location;
8474 else
8476 c_parser_error (parser, "expected identifier");
8477 expr.set_error ();
8478 expr.original_code = ERROR_MARK;
8479 expr.original_type = NULL;
8480 return expr;
8482 start = expr.get_start ();
8483 finish = c_parser_peek_token (parser)->get_finish ();
8484 c_parser_consume_token (parser);
8485 expr.value = build_component_ref (op_loc, expr.value, ident,
8486 comp_loc);
8487 set_c_expr_source_range (&expr, start, finish);
8488 expr.original_code = ERROR_MARK;
8489 if (TREE_CODE (expr.value) != COMPONENT_REF)
8490 expr.original_type = NULL;
8491 else
8493 /* Remember the original type of a bitfield. */
8494 tree field = TREE_OPERAND (expr.value, 1);
8495 if (TREE_CODE (field) != FIELD_DECL)
8496 expr.original_type = NULL;
8497 else
8498 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8500 break;
8501 case CPP_DEREF:
8502 /* Structure element reference. */
8503 c_parser_consume_token (parser);
8504 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8505 if (c_parser_next_token_is (parser, CPP_NAME))
8507 c_token *comp_tok = c_parser_peek_token (parser);
8508 ident = comp_tok->value;
8509 comp_loc = comp_tok->location;
8511 else
8513 c_parser_error (parser, "expected identifier");
8514 expr.set_error ();
8515 expr.original_code = ERROR_MARK;
8516 expr.original_type = NULL;
8517 return expr;
8519 start = expr.get_start ();
8520 finish = c_parser_peek_token (parser)->get_finish ();
8521 c_parser_consume_token (parser);
8522 expr.value = build_component_ref (op_loc,
8523 build_indirect_ref (op_loc,
8524 expr.value,
8525 RO_ARROW),
8526 ident, comp_loc);
8527 set_c_expr_source_range (&expr, start, finish);
8528 expr.original_code = ERROR_MARK;
8529 if (TREE_CODE (expr.value) != COMPONENT_REF)
8530 expr.original_type = NULL;
8531 else
8533 /* Remember the original type of a bitfield. */
8534 tree field = TREE_OPERAND (expr.value, 1);
8535 if (TREE_CODE (field) != FIELD_DECL)
8536 expr.original_type = NULL;
8537 else
8538 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8540 break;
8541 case CPP_PLUS_PLUS:
8542 /* Postincrement. */
8543 start = expr.get_start ();
8544 finish = c_parser_peek_token (parser)->get_finish ();
8545 c_parser_consume_token (parser);
8546 /* If the expressions have array notations, we expand them. */
8547 if (flag_cilkplus
8548 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8549 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8550 else
8552 expr = default_function_array_read_conversion (expr_loc, expr);
8553 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
8554 expr.value, false);
8556 set_c_expr_source_range (&expr, start, finish);
8557 expr.original_code = ERROR_MARK;
8558 expr.original_type = NULL;
8559 break;
8560 case CPP_MINUS_MINUS:
8561 /* Postdecrement. */
8562 start = expr.get_start ();
8563 finish = c_parser_peek_token (parser)->get_finish ();
8564 c_parser_consume_token (parser);
8565 /* If the expressions have array notations, we expand them. */
8566 if (flag_cilkplus
8567 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8568 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8569 else
8571 expr = default_function_array_read_conversion (expr_loc, expr);
8572 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
8573 expr.value, false);
8575 set_c_expr_source_range (&expr, start, finish);
8576 expr.original_code = ERROR_MARK;
8577 expr.original_type = NULL;
8578 break;
8579 default:
8580 return expr;
8585 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8587 expression:
8588 assignment-expression
8589 expression , assignment-expression
8592 static struct c_expr
8593 c_parser_expression (c_parser *parser)
8595 location_t tloc = c_parser_peek_token (parser)->location;
8596 struct c_expr expr;
8597 expr = c_parser_expr_no_commas (parser, NULL);
8598 if (c_parser_next_token_is (parser, CPP_COMMA))
8599 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8600 while (c_parser_next_token_is (parser, CPP_COMMA))
8602 struct c_expr next;
8603 tree lhsval;
8604 location_t loc = c_parser_peek_token (parser)->location;
8605 location_t expr_loc;
8606 c_parser_consume_token (parser);
8607 expr_loc = c_parser_peek_token (parser)->location;
8608 lhsval = expr.value;
8609 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8610 lhsval = TREE_OPERAND (lhsval, 1);
8611 if (DECL_P (lhsval) || handled_component_p (lhsval))
8612 mark_exp_read (lhsval);
8613 next = c_parser_expr_no_commas (parser, NULL);
8614 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8615 expr.value = build_compound_expr (loc, expr.value, next.value);
8616 expr.original_code = COMPOUND_EXPR;
8617 expr.original_type = next.original_type;
8619 return expr;
8622 /* Parse an expression and convert functions or arrays to pointers and
8623 lvalues to rvalues. */
8625 static struct c_expr
8626 c_parser_expression_conv (c_parser *parser)
8628 struct c_expr expr;
8629 location_t loc = c_parser_peek_token (parser)->location;
8630 expr = c_parser_expression (parser);
8631 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8632 return expr;
8635 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8636 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8638 static inline void
8639 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8640 unsigned int idx)
8642 if (idx >= HOST_BITS_PER_INT)
8643 return;
8645 c_token *tok = c_parser_peek_token (parser);
8646 switch (tok->type)
8648 case CPP_NUMBER:
8649 case CPP_CHAR:
8650 case CPP_WCHAR:
8651 case CPP_CHAR16:
8652 case CPP_CHAR32:
8653 /* If a parameter is literal zero alone, remember it
8654 for -Wmemset-transposed-args warning. */
8655 if (integer_zerop (tok->value)
8656 && !TREE_OVERFLOW (tok->value)
8657 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8658 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8659 *literal_zero_mask |= 1U << idx;
8660 default:
8661 break;
8665 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8666 functions and arrays to pointers and lvalues to rvalues. If
8667 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8668 locations of function arguments into this vector.
8670 nonempty-expr-list:
8671 assignment-expression
8672 nonempty-expr-list , assignment-expression
8675 static vec<tree, va_gc> *
8676 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8677 vec<tree, va_gc> **p_orig_types,
8678 location_t *sizeof_arg_loc, tree *sizeof_arg,
8679 vec<location_t> *locations,
8680 unsigned int *literal_zero_mask)
8682 vec<tree, va_gc> *ret;
8683 vec<tree, va_gc> *orig_types;
8684 struct c_expr expr;
8685 location_t loc = c_parser_peek_token (parser)->location;
8686 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8687 unsigned int idx = 0;
8689 ret = make_tree_vector ();
8690 if (p_orig_types == NULL)
8691 orig_types = NULL;
8692 else
8693 orig_types = make_tree_vector ();
8695 if (sizeof_arg != NULL
8696 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8697 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8698 if (literal_zero_mask)
8699 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8700 expr = c_parser_expr_no_commas (parser, NULL);
8701 if (convert_p)
8702 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8703 if (fold_p)
8704 expr.value = c_fully_fold (expr.value, false, NULL);
8705 ret->quick_push (expr.value);
8706 if (orig_types)
8707 orig_types->quick_push (expr.original_type);
8708 if (locations)
8709 locations->safe_push (loc);
8710 if (sizeof_arg != NULL
8711 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8712 && expr.original_code == SIZEOF_EXPR)
8714 sizeof_arg[0] = c_last_sizeof_arg;
8715 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8717 while (c_parser_next_token_is (parser, CPP_COMMA))
8719 c_parser_consume_token (parser);
8720 loc = c_parser_peek_token (parser)->location;
8721 if (sizeof_arg != NULL
8722 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8723 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8724 else
8725 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8726 if (literal_zero_mask)
8727 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8728 expr = c_parser_expr_no_commas (parser, NULL);
8729 if (convert_p)
8730 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8731 if (fold_p)
8732 expr.value = c_fully_fold (expr.value, false, NULL);
8733 vec_safe_push (ret, expr.value);
8734 if (orig_types)
8735 vec_safe_push (orig_types, expr.original_type);
8736 if (locations)
8737 locations->safe_push (loc);
8738 if (++idx < 3
8739 && sizeof_arg != NULL
8740 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8741 && expr.original_code == SIZEOF_EXPR)
8743 sizeof_arg[idx] = c_last_sizeof_arg;
8744 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8747 if (orig_types)
8748 *p_orig_types = orig_types;
8749 return ret;
8752 /* Parse Objective-C-specific constructs. */
8754 /* Parse an objc-class-definition.
8756 objc-class-definition:
8757 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8758 objc-class-instance-variables[opt] objc-methodprotolist @end
8759 @implementation identifier objc-superclass[opt]
8760 objc-class-instance-variables[opt]
8761 @interface identifier ( identifier ) objc-protocol-refs[opt]
8762 objc-methodprotolist @end
8763 @interface identifier ( ) objc-protocol-refs[opt]
8764 objc-methodprotolist @end
8765 @implementation identifier ( identifier )
8767 objc-superclass:
8768 : identifier
8770 "@interface identifier (" must start "@interface identifier (
8771 identifier ) ...": objc-methodprotolist in the first production may
8772 not start with a parenthesized identifier as a declarator of a data
8773 definition with no declaration specifiers if the objc-superclass,
8774 objc-protocol-refs and objc-class-instance-variables are omitted. */
8776 static void
8777 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8779 bool iface_p;
8780 tree id1;
8781 tree superclass;
8782 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8783 iface_p = true;
8784 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8785 iface_p = false;
8786 else
8787 gcc_unreachable ();
8789 c_parser_consume_token (parser);
8790 if (c_parser_next_token_is_not (parser, CPP_NAME))
8792 c_parser_error (parser, "expected identifier");
8793 return;
8795 id1 = c_parser_peek_token (parser)->value;
8796 c_parser_consume_token (parser);
8797 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8799 /* We have a category or class extension. */
8800 tree id2;
8801 tree proto = NULL_TREE;
8802 c_parser_consume_token (parser);
8803 if (c_parser_next_token_is_not (parser, CPP_NAME))
8805 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8807 /* We have a class extension. */
8808 id2 = NULL_TREE;
8810 else
8812 c_parser_error (parser, "expected identifier or %<)%>");
8813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8814 return;
8817 else
8819 id2 = c_parser_peek_token (parser)->value;
8820 c_parser_consume_token (parser);
8822 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8823 if (!iface_p)
8825 objc_start_category_implementation (id1, id2);
8826 return;
8828 if (c_parser_next_token_is (parser, CPP_LESS))
8829 proto = c_parser_objc_protocol_refs (parser);
8830 objc_start_category_interface (id1, id2, proto, attributes);
8831 c_parser_objc_methodprotolist (parser);
8832 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8833 objc_finish_interface ();
8834 return;
8836 if (c_parser_next_token_is (parser, CPP_COLON))
8838 c_parser_consume_token (parser);
8839 if (c_parser_next_token_is_not (parser, CPP_NAME))
8841 c_parser_error (parser, "expected identifier");
8842 return;
8844 superclass = c_parser_peek_token (parser)->value;
8845 c_parser_consume_token (parser);
8847 else
8848 superclass = NULL_TREE;
8849 if (iface_p)
8851 tree proto = NULL_TREE;
8852 if (c_parser_next_token_is (parser, CPP_LESS))
8853 proto = c_parser_objc_protocol_refs (parser);
8854 objc_start_class_interface (id1, superclass, proto, attributes);
8856 else
8857 objc_start_class_implementation (id1, superclass);
8858 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8859 c_parser_objc_class_instance_variables (parser);
8860 if (iface_p)
8862 objc_continue_interface ();
8863 c_parser_objc_methodprotolist (parser);
8864 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8865 objc_finish_interface ();
8867 else
8869 objc_continue_implementation ();
8870 return;
8874 /* Parse objc-class-instance-variables.
8876 objc-class-instance-variables:
8877 { objc-instance-variable-decl-list[opt] }
8879 objc-instance-variable-decl-list:
8880 objc-visibility-spec
8881 objc-instance-variable-decl ;
8883 objc-instance-variable-decl-list objc-visibility-spec
8884 objc-instance-variable-decl-list objc-instance-variable-decl ;
8885 objc-instance-variable-decl-list ;
8887 objc-visibility-spec:
8888 @private
8889 @protected
8890 @public
8892 objc-instance-variable-decl:
8893 struct-declaration
8896 static void
8897 c_parser_objc_class_instance_variables (c_parser *parser)
8899 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8900 c_parser_consume_token (parser);
8901 while (c_parser_next_token_is_not (parser, CPP_EOF))
8903 tree decls;
8904 /* Parse any stray semicolon. */
8905 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8907 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8908 "extra semicolon");
8909 c_parser_consume_token (parser);
8910 continue;
8912 /* Stop if at the end of the instance variables. */
8913 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8915 c_parser_consume_token (parser);
8916 break;
8918 /* Parse any objc-visibility-spec. */
8919 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8921 c_parser_consume_token (parser);
8922 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8923 continue;
8925 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8927 c_parser_consume_token (parser);
8928 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8929 continue;
8931 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8933 c_parser_consume_token (parser);
8934 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8935 continue;
8937 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8939 c_parser_consume_token (parser);
8940 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8941 continue;
8943 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8945 c_parser_pragma (parser, pragma_external, NULL);
8946 continue;
8949 /* Parse some comma-separated declarations. */
8950 decls = c_parser_struct_declaration (parser);
8951 if (decls == NULL)
8953 /* There is a syntax error. We want to skip the offending
8954 tokens up to the next ';' (included) or '}'
8955 (excluded). */
8957 /* First, skip manually a ')' or ']'. This is because they
8958 reduce the nesting level, so c_parser_skip_until_found()
8959 wouldn't be able to skip past them. */
8960 c_token *token = c_parser_peek_token (parser);
8961 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8962 c_parser_consume_token (parser);
8964 /* Then, do the standard skipping. */
8965 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8967 /* We hopefully recovered. Start normal parsing again. */
8968 parser->error = false;
8969 continue;
8971 else
8973 /* Comma-separated instance variables are chained together
8974 in reverse order; add them one by one. */
8975 tree ivar = nreverse (decls);
8976 for (; ivar; ivar = DECL_CHAIN (ivar))
8977 objc_add_instance_variable (copy_node (ivar));
8979 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8983 /* Parse an objc-class-declaration.
8985 objc-class-declaration:
8986 @class identifier-list ;
8989 static void
8990 c_parser_objc_class_declaration (c_parser *parser)
8992 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8993 c_parser_consume_token (parser);
8994 /* Any identifiers, including those declared as type names, are OK
8995 here. */
8996 while (true)
8998 tree id;
8999 if (c_parser_next_token_is_not (parser, CPP_NAME))
9001 c_parser_error (parser, "expected identifier");
9002 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9003 parser->error = false;
9004 return;
9006 id = c_parser_peek_token (parser)->value;
9007 objc_declare_class (id);
9008 c_parser_consume_token (parser);
9009 if (c_parser_next_token_is (parser, CPP_COMMA))
9010 c_parser_consume_token (parser);
9011 else
9012 break;
9014 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9017 /* Parse an objc-alias-declaration.
9019 objc-alias-declaration:
9020 @compatibility_alias identifier identifier ;
9023 static void
9024 c_parser_objc_alias_declaration (c_parser *parser)
9026 tree id1, id2;
9027 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9028 c_parser_consume_token (parser);
9029 if (c_parser_next_token_is_not (parser, CPP_NAME))
9031 c_parser_error (parser, "expected identifier");
9032 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9033 return;
9035 id1 = c_parser_peek_token (parser)->value;
9036 c_parser_consume_token (parser);
9037 if (c_parser_next_token_is_not (parser, CPP_NAME))
9039 c_parser_error (parser, "expected identifier");
9040 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9041 return;
9043 id2 = c_parser_peek_token (parser)->value;
9044 c_parser_consume_token (parser);
9045 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9046 objc_declare_alias (id1, id2);
9049 /* Parse an objc-protocol-definition.
9051 objc-protocol-definition:
9052 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9053 @protocol identifier-list ;
9055 "@protocol identifier ;" should be resolved as "@protocol
9056 identifier-list ;": objc-methodprotolist may not start with a
9057 semicolon in the first alternative if objc-protocol-refs are
9058 omitted. */
9060 static void
9061 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9063 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9065 c_parser_consume_token (parser);
9066 if (c_parser_next_token_is_not (parser, CPP_NAME))
9068 c_parser_error (parser, "expected identifier");
9069 return;
9071 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9072 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9074 /* Any identifiers, including those declared as type names, are
9075 OK here. */
9076 while (true)
9078 tree id;
9079 if (c_parser_next_token_is_not (parser, CPP_NAME))
9081 c_parser_error (parser, "expected identifier");
9082 break;
9084 id = c_parser_peek_token (parser)->value;
9085 objc_declare_protocol (id, attributes);
9086 c_parser_consume_token (parser);
9087 if (c_parser_next_token_is (parser, CPP_COMMA))
9088 c_parser_consume_token (parser);
9089 else
9090 break;
9092 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9094 else
9096 tree id = c_parser_peek_token (parser)->value;
9097 tree proto = NULL_TREE;
9098 c_parser_consume_token (parser);
9099 if (c_parser_next_token_is (parser, CPP_LESS))
9100 proto = c_parser_objc_protocol_refs (parser);
9101 parser->objc_pq_context = true;
9102 objc_start_protocol (id, proto, attributes);
9103 c_parser_objc_methodprotolist (parser);
9104 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9105 parser->objc_pq_context = false;
9106 objc_finish_interface ();
9110 /* Parse an objc-method-type.
9112 objc-method-type:
9116 Return true if it is a class method (+) and false if it is
9117 an instance method (-).
9119 static inline bool
9120 c_parser_objc_method_type (c_parser *parser)
9122 switch (c_parser_peek_token (parser)->type)
9124 case CPP_PLUS:
9125 c_parser_consume_token (parser);
9126 return true;
9127 case CPP_MINUS:
9128 c_parser_consume_token (parser);
9129 return false;
9130 default:
9131 gcc_unreachable ();
9135 /* Parse an objc-method-definition.
9137 objc-method-definition:
9138 objc-method-type objc-method-decl ;[opt] compound-statement
9141 static void
9142 c_parser_objc_method_definition (c_parser *parser)
9144 bool is_class_method = c_parser_objc_method_type (parser);
9145 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9146 parser->objc_pq_context = true;
9147 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9148 &expr);
9149 if (decl == error_mark_node)
9150 return; /* Bail here. */
9152 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9154 c_parser_consume_token (parser);
9155 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9156 "extra semicolon in method definition specified");
9159 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9161 c_parser_error (parser, "expected %<{%>");
9162 return;
9165 parser->objc_pq_context = false;
9166 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9168 add_stmt (c_parser_compound_statement (parser));
9169 objc_finish_method_definition (current_function_decl);
9171 else
9173 /* This code is executed when we find a method definition
9174 outside of an @implementation context (or invalid for other
9175 reasons). Parse the method (to keep going) but do not emit
9176 any code.
9178 c_parser_compound_statement (parser);
9182 /* Parse an objc-methodprotolist.
9184 objc-methodprotolist:
9185 empty
9186 objc-methodprotolist objc-methodproto
9187 objc-methodprotolist declaration
9188 objc-methodprotolist ;
9189 @optional
9190 @required
9192 The declaration is a data definition, which may be missing
9193 declaration specifiers under the same rules and diagnostics as
9194 other data definitions outside functions, and the stray semicolon
9195 is diagnosed the same way as a stray semicolon outside a
9196 function. */
9198 static void
9199 c_parser_objc_methodprotolist (c_parser *parser)
9201 while (true)
9203 /* The list is terminated by @end. */
9204 switch (c_parser_peek_token (parser)->type)
9206 case CPP_SEMICOLON:
9207 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9208 "ISO C does not allow extra %<;%> outside of a function");
9209 c_parser_consume_token (parser);
9210 break;
9211 case CPP_PLUS:
9212 case CPP_MINUS:
9213 c_parser_objc_methodproto (parser);
9214 break;
9215 case CPP_PRAGMA:
9216 c_parser_pragma (parser, pragma_external, NULL);
9217 break;
9218 case CPP_EOF:
9219 return;
9220 default:
9221 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9222 return;
9223 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9224 c_parser_objc_at_property_declaration (parser);
9225 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9227 objc_set_method_opt (true);
9228 c_parser_consume_token (parser);
9230 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9232 objc_set_method_opt (false);
9233 c_parser_consume_token (parser);
9235 else
9236 c_parser_declaration_or_fndef (parser, false, false, true,
9237 false, true, NULL, vNULL);
9238 break;
9243 /* Parse an objc-methodproto.
9245 objc-methodproto:
9246 objc-method-type objc-method-decl ;
9249 static void
9250 c_parser_objc_methodproto (c_parser *parser)
9252 bool is_class_method = c_parser_objc_method_type (parser);
9253 tree decl, attributes = NULL_TREE;
9255 /* Remember protocol qualifiers in prototypes. */
9256 parser->objc_pq_context = true;
9257 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9258 NULL);
9259 /* Forget protocol qualifiers now. */
9260 parser->objc_pq_context = false;
9262 /* Do not allow the presence of attributes to hide an erroneous
9263 method implementation in the interface section. */
9264 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9266 c_parser_error (parser, "expected %<;%>");
9267 return;
9270 if (decl != error_mark_node)
9271 objc_add_method_declaration (is_class_method, decl, attributes);
9273 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9276 /* If we are at a position that method attributes may be present, check that
9277 there are not any parsed already (a syntax error) and then collect any
9278 specified at the current location. Finally, if new attributes were present,
9279 check that the next token is legal ( ';' for decls and '{' for defs). */
9281 static bool
9282 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9284 bool bad = false;
9285 if (*attributes)
9287 c_parser_error (parser,
9288 "method attributes must be specified at the end only");
9289 *attributes = NULL_TREE;
9290 bad = true;
9293 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9294 *attributes = c_parser_attributes (parser);
9296 /* If there were no attributes here, just report any earlier error. */
9297 if (*attributes == NULL_TREE || bad)
9298 return bad;
9300 /* If the attributes are followed by a ; or {, then just report any earlier
9301 error. */
9302 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9303 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9304 return bad;
9306 /* We've got attributes, but not at the end. */
9307 c_parser_error (parser,
9308 "expected %<;%> or %<{%> after method attribute definition");
9309 return true;
9312 /* Parse an objc-method-decl.
9314 objc-method-decl:
9315 ( objc-type-name ) objc-selector
9316 objc-selector
9317 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9318 objc-keyword-selector objc-optparmlist
9319 attributes
9321 objc-keyword-selector:
9322 objc-keyword-decl
9323 objc-keyword-selector objc-keyword-decl
9325 objc-keyword-decl:
9326 objc-selector : ( objc-type-name ) identifier
9327 objc-selector : identifier
9328 : ( objc-type-name ) identifier
9329 : identifier
9331 objc-optparmlist:
9332 objc-optparms objc-optellipsis
9334 objc-optparms:
9335 empty
9336 objc-opt-parms , parameter-declaration
9338 objc-optellipsis:
9339 empty
9340 , ...
9343 static tree
9344 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9345 tree *attributes, tree *expr)
9347 tree type = NULL_TREE;
9348 tree sel;
9349 tree parms = NULL_TREE;
9350 bool ellipsis = false;
9351 bool attr_err = false;
9353 *attributes = NULL_TREE;
9354 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9356 c_parser_consume_token (parser);
9357 type = c_parser_objc_type_name (parser);
9358 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9360 sel = c_parser_objc_selector (parser);
9361 /* If there is no selector, or a colon follows, we have an
9362 objc-keyword-selector. If there is a selector, and a colon does
9363 not follow, that selector ends the objc-method-decl. */
9364 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9366 tree tsel = sel;
9367 tree list = NULL_TREE;
9368 while (true)
9370 tree atype = NULL_TREE, id, keyworddecl;
9371 tree param_attr = NULL_TREE;
9372 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9373 break;
9374 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9376 c_parser_consume_token (parser);
9377 atype = c_parser_objc_type_name (parser);
9378 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9379 "expected %<)%>");
9381 /* New ObjC allows attributes on method parameters. */
9382 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9383 param_attr = c_parser_attributes (parser);
9384 if (c_parser_next_token_is_not (parser, CPP_NAME))
9386 c_parser_error (parser, "expected identifier");
9387 return error_mark_node;
9389 id = c_parser_peek_token (parser)->value;
9390 c_parser_consume_token (parser);
9391 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9392 list = chainon (list, keyworddecl);
9393 tsel = c_parser_objc_selector (parser);
9394 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9395 break;
9398 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9400 /* Parse the optional parameter list. Optional Objective-C
9401 method parameters follow the C syntax, and may include '...'
9402 to denote a variable number of arguments. */
9403 parms = make_node (TREE_LIST);
9404 while (c_parser_next_token_is (parser, CPP_COMMA))
9406 struct c_parm *parm;
9407 c_parser_consume_token (parser);
9408 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9410 ellipsis = true;
9411 c_parser_consume_token (parser);
9412 attr_err |= c_parser_objc_maybe_method_attributes
9413 (parser, attributes) ;
9414 break;
9416 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9417 if (parm == NULL)
9418 break;
9419 parms = chainon (parms,
9420 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9422 sel = list;
9424 else
9425 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9427 if (sel == NULL)
9429 c_parser_error (parser, "objective-c method declaration is expected");
9430 return error_mark_node;
9433 if (attr_err)
9434 return error_mark_node;
9436 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9439 /* Parse an objc-type-name.
9441 objc-type-name:
9442 objc-type-qualifiers[opt] type-name
9443 objc-type-qualifiers[opt]
9445 objc-type-qualifiers:
9446 objc-type-qualifier
9447 objc-type-qualifiers objc-type-qualifier
9449 objc-type-qualifier: one of
9450 in out inout bycopy byref oneway
9453 static tree
9454 c_parser_objc_type_name (c_parser *parser)
9456 tree quals = NULL_TREE;
9457 struct c_type_name *type_name = NULL;
9458 tree type = NULL_TREE;
9459 while (true)
9461 c_token *token = c_parser_peek_token (parser);
9462 if (token->type == CPP_KEYWORD
9463 && (token->keyword == RID_IN
9464 || token->keyword == RID_OUT
9465 || token->keyword == RID_INOUT
9466 || token->keyword == RID_BYCOPY
9467 || token->keyword == RID_BYREF
9468 || token->keyword == RID_ONEWAY))
9470 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9471 c_parser_consume_token (parser);
9473 else
9474 break;
9476 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9477 type_name = c_parser_type_name (parser);
9478 if (type_name)
9479 type = groktypename (type_name, NULL, NULL);
9481 /* If the type is unknown, and error has already been produced and
9482 we need to recover from the error. In that case, use NULL_TREE
9483 for the type, as if no type had been specified; this will use the
9484 default type ('id') which is good for error recovery. */
9485 if (type == error_mark_node)
9486 type = NULL_TREE;
9488 return build_tree_list (quals, type);
9491 /* Parse objc-protocol-refs.
9493 objc-protocol-refs:
9494 < identifier-list >
9497 static tree
9498 c_parser_objc_protocol_refs (c_parser *parser)
9500 tree list = NULL_TREE;
9501 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9502 c_parser_consume_token (parser);
9503 /* Any identifiers, including those declared as type names, are OK
9504 here. */
9505 while (true)
9507 tree id;
9508 if (c_parser_next_token_is_not (parser, CPP_NAME))
9510 c_parser_error (parser, "expected identifier");
9511 break;
9513 id = c_parser_peek_token (parser)->value;
9514 list = chainon (list, build_tree_list (NULL_TREE, id));
9515 c_parser_consume_token (parser);
9516 if (c_parser_next_token_is (parser, CPP_COMMA))
9517 c_parser_consume_token (parser);
9518 else
9519 break;
9521 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9522 return list;
9525 /* Parse an objc-try-catch-finally-statement.
9527 objc-try-catch-finally-statement:
9528 @try compound-statement objc-catch-list[opt]
9529 @try compound-statement objc-catch-list[opt] @finally compound-statement
9531 objc-catch-list:
9532 @catch ( objc-catch-parameter-declaration ) compound-statement
9533 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9535 objc-catch-parameter-declaration:
9536 parameter-declaration
9537 '...'
9539 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9541 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9542 for C++. Keep them in sync. */
9544 static void
9545 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9547 location_t location;
9548 tree stmt;
9550 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9551 c_parser_consume_token (parser);
9552 location = c_parser_peek_token (parser)->location;
9553 objc_maybe_warn_exceptions (location);
9554 stmt = c_parser_compound_statement (parser);
9555 objc_begin_try_stmt (location, stmt);
9557 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9559 struct c_parm *parm;
9560 tree parameter_declaration = error_mark_node;
9561 bool seen_open_paren = false;
9563 c_parser_consume_token (parser);
9564 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9565 seen_open_paren = true;
9566 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9568 /* We have "@catch (...)" (where the '...' are literally
9569 what is in the code). Skip the '...'.
9570 parameter_declaration is set to NULL_TREE, and
9571 objc_being_catch_clauses() knows that that means
9572 '...'. */
9573 c_parser_consume_token (parser);
9574 parameter_declaration = NULL_TREE;
9576 else
9578 /* We have "@catch (NSException *exception)" or something
9579 like that. Parse the parameter declaration. */
9580 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9581 if (parm == NULL)
9582 parameter_declaration = error_mark_node;
9583 else
9584 parameter_declaration = grokparm (parm, NULL);
9586 if (seen_open_paren)
9587 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9588 else
9590 /* If there was no open parenthesis, we are recovering from
9591 an error, and we are trying to figure out what mistake
9592 the user has made. */
9594 /* If there is an immediate closing parenthesis, the user
9595 probably forgot the opening one (ie, they typed "@catch
9596 NSException *e)". Parse the closing parenthesis and keep
9597 going. */
9598 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9599 c_parser_consume_token (parser);
9601 /* If these is no immediate closing parenthesis, the user
9602 probably doesn't know that parenthesis are required at
9603 all (ie, they typed "@catch NSException *e"). So, just
9604 forget about the closing parenthesis and keep going. */
9606 objc_begin_catch_clause (parameter_declaration);
9607 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9608 c_parser_compound_statement_nostart (parser);
9609 objc_finish_catch_clause ();
9611 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9613 c_parser_consume_token (parser);
9614 location = c_parser_peek_token (parser)->location;
9615 stmt = c_parser_compound_statement (parser);
9616 objc_build_finally_clause (location, stmt);
9618 objc_finish_try_stmt ();
9621 /* Parse an objc-synchronized-statement.
9623 objc-synchronized-statement:
9624 @synchronized ( expression ) compound-statement
9627 static void
9628 c_parser_objc_synchronized_statement (c_parser *parser)
9630 location_t loc;
9631 tree expr, stmt;
9632 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9633 c_parser_consume_token (parser);
9634 loc = c_parser_peek_token (parser)->location;
9635 objc_maybe_warn_exceptions (loc);
9636 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9638 struct c_expr ce = c_parser_expression (parser);
9639 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9640 expr = ce.value;
9641 expr = c_fully_fold (expr, false, NULL);
9642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9644 else
9645 expr = error_mark_node;
9646 stmt = c_parser_compound_statement (parser);
9647 objc_build_synchronized (loc, expr, stmt);
9650 /* Parse an objc-selector; return NULL_TREE without an error if the
9651 next token is not an objc-selector.
9653 objc-selector:
9654 identifier
9655 one of
9656 enum struct union if else while do for switch case default
9657 break continue return goto asm sizeof typeof __alignof
9658 unsigned long const short volatile signed restrict _Complex
9659 in out inout bycopy byref oneway int char float double void _Bool
9660 _Atomic
9662 ??? Why this selection of keywords but not, for example, storage
9663 class specifiers? */
9665 static tree
9666 c_parser_objc_selector (c_parser *parser)
9668 c_token *token = c_parser_peek_token (parser);
9669 tree value = token->value;
9670 if (token->type == CPP_NAME)
9672 c_parser_consume_token (parser);
9673 return value;
9675 if (token->type != CPP_KEYWORD)
9676 return NULL_TREE;
9677 switch (token->keyword)
9679 case RID_ENUM:
9680 case RID_STRUCT:
9681 case RID_UNION:
9682 case RID_IF:
9683 case RID_ELSE:
9684 case RID_WHILE:
9685 case RID_DO:
9686 case RID_FOR:
9687 case RID_SWITCH:
9688 case RID_CASE:
9689 case RID_DEFAULT:
9690 case RID_BREAK:
9691 case RID_CONTINUE:
9692 case RID_RETURN:
9693 case RID_GOTO:
9694 case RID_ASM:
9695 case RID_SIZEOF:
9696 case RID_TYPEOF:
9697 case RID_ALIGNOF:
9698 case RID_UNSIGNED:
9699 case RID_LONG:
9700 case RID_CONST:
9701 case RID_SHORT:
9702 case RID_VOLATILE:
9703 case RID_SIGNED:
9704 case RID_RESTRICT:
9705 case RID_COMPLEX:
9706 case RID_IN:
9707 case RID_OUT:
9708 case RID_INOUT:
9709 case RID_BYCOPY:
9710 case RID_BYREF:
9711 case RID_ONEWAY:
9712 case RID_INT:
9713 case RID_CHAR:
9714 case RID_FLOAT:
9715 case RID_DOUBLE:
9716 CASE_RID_FLOATN_NX:
9717 case RID_VOID:
9718 case RID_BOOL:
9719 case RID_ATOMIC:
9720 case RID_AUTO_TYPE:
9721 case RID_INT_N_0:
9722 case RID_INT_N_1:
9723 case RID_INT_N_2:
9724 case RID_INT_N_3:
9725 c_parser_consume_token (parser);
9726 return value;
9727 default:
9728 return NULL_TREE;
9732 /* Parse an objc-selector-arg.
9734 objc-selector-arg:
9735 objc-selector
9736 objc-keywordname-list
9738 objc-keywordname-list:
9739 objc-keywordname
9740 objc-keywordname-list objc-keywordname
9742 objc-keywordname:
9743 objc-selector :
9747 static tree
9748 c_parser_objc_selector_arg (c_parser *parser)
9750 tree sel = c_parser_objc_selector (parser);
9751 tree list = NULL_TREE;
9752 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9753 return sel;
9754 while (true)
9756 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9757 return list;
9758 list = chainon (list, build_tree_list (sel, NULL_TREE));
9759 sel = c_parser_objc_selector (parser);
9760 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9761 break;
9763 return list;
9766 /* Parse an objc-receiver.
9768 objc-receiver:
9769 expression
9770 class-name
9771 type-name
9774 static tree
9775 c_parser_objc_receiver (c_parser *parser)
9777 location_t loc = c_parser_peek_token (parser)->location;
9779 if (c_parser_peek_token (parser)->type == CPP_NAME
9780 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9781 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9783 tree id = c_parser_peek_token (parser)->value;
9784 c_parser_consume_token (parser);
9785 return objc_get_class_reference (id);
9787 struct c_expr ce = c_parser_expression (parser);
9788 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9789 return c_fully_fold (ce.value, false, NULL);
9792 /* Parse objc-message-args.
9794 objc-message-args:
9795 objc-selector
9796 objc-keywordarg-list
9798 objc-keywordarg-list:
9799 objc-keywordarg
9800 objc-keywordarg-list objc-keywordarg
9802 objc-keywordarg:
9803 objc-selector : objc-keywordexpr
9804 : objc-keywordexpr
9807 static tree
9808 c_parser_objc_message_args (c_parser *parser)
9810 tree sel = c_parser_objc_selector (parser);
9811 tree list = NULL_TREE;
9812 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9813 return sel;
9814 while (true)
9816 tree keywordexpr;
9817 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9818 return error_mark_node;
9819 keywordexpr = c_parser_objc_keywordexpr (parser);
9820 list = chainon (list, build_tree_list (sel, keywordexpr));
9821 sel = c_parser_objc_selector (parser);
9822 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9823 break;
9825 return list;
9828 /* Parse an objc-keywordexpr.
9830 objc-keywordexpr:
9831 nonempty-expr-list
9834 static tree
9835 c_parser_objc_keywordexpr (c_parser *parser)
9837 tree ret;
9838 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9839 NULL, NULL, NULL, NULL);
9840 if (vec_safe_length (expr_list) == 1)
9842 /* Just return the expression, remove a level of
9843 indirection. */
9844 ret = (*expr_list)[0];
9846 else
9848 /* We have a comma expression, we will collapse later. */
9849 ret = build_tree_list_vec (expr_list);
9851 release_tree_vector (expr_list);
9852 return ret;
9855 /* A check, needed in several places, that ObjC interface, implementation or
9856 method definitions are not prefixed by incorrect items. */
9857 static bool
9858 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9859 struct c_declspecs *specs)
9861 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9862 || specs->typespec_kind != ctsk_none)
9864 c_parser_error (parser,
9865 "no type or storage class may be specified here,");
9866 c_parser_skip_to_end_of_block_or_statement (parser);
9867 return true;
9869 return false;
9872 /* Parse an Objective-C @property declaration. The syntax is:
9874 objc-property-declaration:
9875 '@property' objc-property-attributes[opt] struct-declaration ;
9877 objc-property-attributes:
9878 '(' objc-property-attribute-list ')'
9880 objc-property-attribute-list:
9881 objc-property-attribute
9882 objc-property-attribute-list, objc-property-attribute
9884 objc-property-attribute
9885 'getter' = identifier
9886 'setter' = identifier
9887 'readonly'
9888 'readwrite'
9889 'assign'
9890 'retain'
9891 'copy'
9892 'nonatomic'
9894 For example:
9895 @property NSString *name;
9896 @property (readonly) id object;
9897 @property (retain, nonatomic, getter=getTheName) id name;
9898 @property int a, b, c;
9900 PS: This function is identical to cp_parser_objc_at_propery_declaration
9901 for C++. Keep them in sync. */
9902 static void
9903 c_parser_objc_at_property_declaration (c_parser *parser)
9905 /* The following variables hold the attributes of the properties as
9906 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9907 seen. When we see an attribute, we set them to 'true' (if they
9908 are boolean properties) or to the identifier (if they have an
9909 argument, ie, for getter and setter). Note that here we only
9910 parse the list of attributes, check the syntax and accumulate the
9911 attributes that we find. objc_add_property_declaration() will
9912 then process the information. */
9913 bool property_assign = false;
9914 bool property_copy = false;
9915 tree property_getter_ident = NULL_TREE;
9916 bool property_nonatomic = false;
9917 bool property_readonly = false;
9918 bool property_readwrite = false;
9919 bool property_retain = false;
9920 tree property_setter_ident = NULL_TREE;
9922 /* 'properties' is the list of properties that we read. Usually a
9923 single one, but maybe more (eg, in "@property int a, b, c;" there
9924 are three). */
9925 tree properties;
9926 location_t loc;
9928 loc = c_parser_peek_token (parser)->location;
9929 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9931 c_parser_consume_token (parser); /* Eat '@property'. */
9933 /* Parse the optional attribute list... */
9934 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9936 /* Eat the '(' */
9937 c_parser_consume_token (parser);
9939 /* Property attribute keywords are valid now. */
9940 parser->objc_property_attr_context = true;
9942 while (true)
9944 bool syntax_error = false;
9945 c_token *token = c_parser_peek_token (parser);
9946 enum rid keyword;
9948 if (token->type != CPP_KEYWORD)
9950 if (token->type == CPP_CLOSE_PAREN)
9951 c_parser_error (parser, "expected identifier");
9952 else
9954 c_parser_consume_token (parser);
9955 c_parser_error (parser, "unknown property attribute");
9957 break;
9959 keyword = token->keyword;
9960 c_parser_consume_token (parser);
9961 switch (keyword)
9963 case RID_ASSIGN: property_assign = true; break;
9964 case RID_COPY: property_copy = true; break;
9965 case RID_NONATOMIC: property_nonatomic = true; break;
9966 case RID_READONLY: property_readonly = true; break;
9967 case RID_READWRITE: property_readwrite = true; break;
9968 case RID_RETAIN: property_retain = true; break;
9970 case RID_GETTER:
9971 case RID_SETTER:
9972 if (c_parser_next_token_is_not (parser, CPP_EQ))
9974 if (keyword == RID_GETTER)
9975 c_parser_error (parser,
9976 "missing %<=%> (after %<getter%> attribute)");
9977 else
9978 c_parser_error (parser,
9979 "missing %<=%> (after %<setter%> attribute)");
9980 syntax_error = true;
9981 break;
9983 c_parser_consume_token (parser); /* eat the = */
9984 if (c_parser_next_token_is_not (parser, CPP_NAME))
9986 c_parser_error (parser, "expected identifier");
9987 syntax_error = true;
9988 break;
9990 if (keyword == RID_SETTER)
9992 if (property_setter_ident != NULL_TREE)
9993 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9994 else
9995 property_setter_ident = c_parser_peek_token (parser)->value;
9996 c_parser_consume_token (parser);
9997 if (c_parser_next_token_is_not (parser, CPP_COLON))
9998 c_parser_error (parser, "setter name must terminate with %<:%>");
9999 else
10000 c_parser_consume_token (parser);
10002 else
10004 if (property_getter_ident != NULL_TREE)
10005 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10006 else
10007 property_getter_ident = c_parser_peek_token (parser)->value;
10008 c_parser_consume_token (parser);
10010 break;
10011 default:
10012 c_parser_error (parser, "unknown property attribute");
10013 syntax_error = true;
10014 break;
10017 if (syntax_error)
10018 break;
10020 if (c_parser_next_token_is (parser, CPP_COMMA))
10021 c_parser_consume_token (parser);
10022 else
10023 break;
10025 parser->objc_property_attr_context = false;
10026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10028 /* ... and the property declaration(s). */
10029 properties = c_parser_struct_declaration (parser);
10031 if (properties == error_mark_node)
10033 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10034 parser->error = false;
10035 return;
10038 if (properties == NULL_TREE)
10039 c_parser_error (parser, "expected identifier");
10040 else
10042 /* Comma-separated properties are chained together in
10043 reverse order; add them one by one. */
10044 properties = nreverse (properties);
10046 for (; properties; properties = TREE_CHAIN (properties))
10047 objc_add_property_declaration (loc, copy_node (properties),
10048 property_readonly, property_readwrite,
10049 property_assign, property_retain,
10050 property_copy, property_nonatomic,
10051 property_getter_ident, property_setter_ident);
10054 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10055 parser->error = false;
10058 /* Parse an Objective-C @synthesize declaration. The syntax is:
10060 objc-synthesize-declaration:
10061 @synthesize objc-synthesize-identifier-list ;
10063 objc-synthesize-identifier-list:
10064 objc-synthesize-identifier
10065 objc-synthesize-identifier-list, objc-synthesize-identifier
10067 objc-synthesize-identifier
10068 identifier
10069 identifier = identifier
10071 For example:
10072 @synthesize MyProperty;
10073 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10075 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10076 for C++. Keep them in sync.
10078 static void
10079 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10081 tree list = NULL_TREE;
10082 location_t loc;
10083 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10084 loc = c_parser_peek_token (parser)->location;
10086 c_parser_consume_token (parser);
10087 while (true)
10089 tree property, ivar;
10090 if (c_parser_next_token_is_not (parser, CPP_NAME))
10092 c_parser_error (parser, "expected identifier");
10093 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10094 /* Once we find the semicolon, we can resume normal parsing.
10095 We have to reset parser->error manually because
10096 c_parser_skip_until_found() won't reset it for us if the
10097 next token is precisely a semicolon. */
10098 parser->error = false;
10099 return;
10101 property = c_parser_peek_token (parser)->value;
10102 c_parser_consume_token (parser);
10103 if (c_parser_next_token_is (parser, CPP_EQ))
10105 c_parser_consume_token (parser);
10106 if (c_parser_next_token_is_not (parser, CPP_NAME))
10108 c_parser_error (parser, "expected identifier");
10109 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10110 parser->error = false;
10111 return;
10113 ivar = c_parser_peek_token (parser)->value;
10114 c_parser_consume_token (parser);
10116 else
10117 ivar = NULL_TREE;
10118 list = chainon (list, build_tree_list (ivar, property));
10119 if (c_parser_next_token_is (parser, CPP_COMMA))
10120 c_parser_consume_token (parser);
10121 else
10122 break;
10124 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10125 objc_add_synthesize_declaration (loc, list);
10128 /* Parse an Objective-C @dynamic declaration. The syntax is:
10130 objc-dynamic-declaration:
10131 @dynamic identifier-list ;
10133 For example:
10134 @dynamic MyProperty;
10135 @dynamic MyProperty, AnotherProperty;
10137 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10138 for C++. Keep them in sync.
10140 static void
10141 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10143 tree list = NULL_TREE;
10144 location_t loc;
10145 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10146 loc = c_parser_peek_token (parser)->location;
10148 c_parser_consume_token (parser);
10149 while (true)
10151 tree property;
10152 if (c_parser_next_token_is_not (parser, CPP_NAME))
10154 c_parser_error (parser, "expected identifier");
10155 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10156 parser->error = false;
10157 return;
10159 property = c_parser_peek_token (parser)->value;
10160 list = chainon (list, build_tree_list (NULL_TREE, property));
10161 c_parser_consume_token (parser);
10162 if (c_parser_next_token_is (parser, CPP_COMMA))
10163 c_parser_consume_token (parser);
10164 else
10165 break;
10167 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10168 objc_add_dynamic_declaration (loc, list);
10172 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10173 should be considered, statements. ALLOW_STMT is true if we're within
10174 the context of a function and such pragmas are to be allowed. Returns
10175 true if we actually parsed such a pragma. */
10177 static bool
10178 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10180 unsigned int id;
10182 id = c_parser_peek_token (parser)->pragma_kind;
10183 gcc_assert (id != PRAGMA_NONE);
10185 switch (id)
10187 case PRAGMA_OACC_DECLARE:
10188 c_parser_oacc_declare (parser);
10189 return false;
10191 case PRAGMA_OACC_ENTER_DATA:
10192 if (context != pragma_compound)
10194 if (context == pragma_stmt)
10195 c_parser_error (parser, "%<#pragma acc enter data%> may only be "
10196 "used in compound statements");
10197 goto bad_stmt;
10199 c_parser_oacc_enter_exit_data (parser, true);
10200 return false;
10202 case PRAGMA_OACC_EXIT_DATA:
10203 if (context != pragma_compound)
10205 if (context == pragma_stmt)
10206 c_parser_error (parser, "%<#pragma acc exit data%> may only be "
10207 "used in compound statements");
10208 goto bad_stmt;
10210 c_parser_oacc_enter_exit_data (parser, false);
10211 return false;
10213 case PRAGMA_OACC_ROUTINE:
10214 if (context != pragma_external)
10216 error_at (c_parser_peek_token (parser)->location,
10217 "%<#pragma acc routine%> must be at file scope");
10218 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10219 return false;
10221 c_parser_oacc_routine (parser, context);
10222 return false;
10224 case PRAGMA_OACC_UPDATE:
10225 if (context != pragma_compound)
10227 if (context == pragma_stmt)
10228 c_parser_error (parser, "%<#pragma acc update%> may only be "
10229 "used in compound statements");
10230 goto bad_stmt;
10232 c_parser_oacc_update (parser);
10233 return false;
10235 case PRAGMA_OMP_BARRIER:
10236 if (context != pragma_compound)
10238 if (context == pragma_stmt)
10239 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
10240 "used in compound statements");
10241 goto bad_stmt;
10243 c_parser_omp_barrier (parser);
10244 return false;
10246 case PRAGMA_OMP_FLUSH:
10247 if (context != pragma_compound)
10249 if (context == pragma_stmt)
10250 c_parser_error (parser, "%<#pragma omp flush%> may only be "
10251 "used in compound statements");
10252 goto bad_stmt;
10254 c_parser_omp_flush (parser);
10255 return false;
10257 case PRAGMA_OMP_TASKWAIT:
10258 if (context != pragma_compound)
10260 if (context == pragma_stmt)
10261 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
10262 "used in compound statements");
10263 goto bad_stmt;
10265 c_parser_omp_taskwait (parser);
10266 return false;
10268 case PRAGMA_OMP_TASKYIELD:
10269 if (context != pragma_compound)
10271 if (context == pragma_stmt)
10272 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
10273 "used in compound statements");
10274 goto bad_stmt;
10276 c_parser_omp_taskyield (parser);
10277 return false;
10279 case PRAGMA_OMP_CANCEL:
10280 if (context != pragma_compound)
10282 if (context == pragma_stmt)
10283 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
10284 "used in compound statements");
10285 goto bad_stmt;
10287 c_parser_omp_cancel (parser);
10288 return false;
10290 case PRAGMA_OMP_CANCELLATION_POINT:
10291 c_parser_omp_cancellation_point (parser, context);
10292 return false;
10294 case PRAGMA_OMP_THREADPRIVATE:
10295 c_parser_omp_threadprivate (parser);
10296 return false;
10298 case PRAGMA_OMP_TARGET:
10299 return c_parser_omp_target (parser, context, if_p);
10301 case PRAGMA_OMP_END_DECLARE_TARGET:
10302 c_parser_omp_end_declare_target (parser);
10303 return false;
10305 case PRAGMA_OMP_SECTION:
10306 error_at (c_parser_peek_token (parser)->location,
10307 "%<#pragma omp section%> may only be used in "
10308 "%<#pragma omp sections%> construct");
10309 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10310 return false;
10312 case PRAGMA_OMP_DECLARE:
10313 c_parser_omp_declare (parser, context);
10314 return false;
10316 case PRAGMA_OMP_ORDERED:
10317 return c_parser_omp_ordered (parser, context, if_p);
10319 case PRAGMA_IVDEP:
10320 c_parser_consume_pragma (parser);
10321 c_parser_skip_to_pragma_eol (parser);
10322 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10323 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10324 && !c_parser_next_token_is_keyword (parser, RID_DO))
10326 c_parser_error (parser, "for, while or do statement expected");
10327 return false;
10329 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10330 c_parser_for_statement (parser, true, if_p);
10331 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10332 c_parser_while_statement (parser, true, if_p);
10333 else
10334 c_parser_do_statement (parser, true);
10335 return false;
10337 case PRAGMA_GCC_PCH_PREPROCESS:
10338 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10339 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10340 return false;
10342 case PRAGMA_CILK_SIMD:
10343 if (!c_parser_cilk_verify_simd (parser, context))
10344 return false;
10345 c_parser_consume_pragma (parser);
10346 c_parser_cilk_simd (parser, if_p);
10347 return false;
10348 case PRAGMA_CILK_GRAINSIZE:
10349 if (!flag_cilkplus)
10351 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10352 " enabled");
10353 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10354 return false;
10356 if (context == pragma_external)
10358 error_at (c_parser_peek_token (parser)->location,
10359 "%<#pragma grainsize%> must be inside a function");
10360 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10361 return false;
10363 c_parser_cilk_grainsize (parser, if_p);
10364 return false;
10366 case PRAGMA_OACC_WAIT:
10367 if (context != pragma_compound)
10369 if (context == pragma_stmt)
10370 c_parser_error (parser, "%<#pragma acc enter data%> may only be "
10371 "used in compound statements");
10372 goto bad_stmt;
10374 /* FALL THROUGH. */
10376 default:
10377 if (id < PRAGMA_FIRST_EXTERNAL)
10379 if (context != pragma_stmt && context != pragma_compound)
10381 bad_stmt:
10382 c_parser_error (parser, "expected declaration specifiers");
10383 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10384 return false;
10386 c_parser_omp_construct (parser, if_p);
10387 return true;
10389 break;
10392 c_parser_consume_pragma (parser);
10393 c_invoke_pragma_handler (id);
10395 /* Skip to EOL, but suppress any error message. Those will have been
10396 generated by the handler routine through calling error, as opposed
10397 to calling c_parser_error. */
10398 parser->error = true;
10399 c_parser_skip_to_pragma_eol (parser);
10401 return false;
10404 /* The interface the pragma parsers have to the lexer. */
10406 enum cpp_ttype
10407 pragma_lex (tree *value, location_t *loc)
10409 c_token *tok = c_parser_peek_token (the_parser);
10410 enum cpp_ttype ret = tok->type;
10412 *value = tok->value;
10413 if (loc)
10414 *loc = tok->location;
10416 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10417 ret = CPP_EOF;
10418 else
10420 if (ret == CPP_KEYWORD)
10421 ret = CPP_NAME;
10422 c_parser_consume_token (the_parser);
10425 return ret;
10428 static void
10429 c_parser_pragma_pch_preprocess (c_parser *parser)
10431 tree name = NULL;
10433 c_parser_consume_pragma (parser);
10434 if (c_parser_next_token_is (parser, CPP_STRING))
10436 name = c_parser_peek_token (parser)->value;
10437 c_parser_consume_token (parser);
10439 else
10440 c_parser_error (parser, "expected string literal");
10441 c_parser_skip_to_pragma_eol (parser);
10443 if (name)
10444 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10447 /* OpenACC and OpenMP parsing routines. */
10449 /* Returns name of the next clause.
10450 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10451 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10452 returned and the token is consumed. */
10454 static pragma_omp_clause
10455 c_parser_omp_clause_name (c_parser *parser)
10457 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10459 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10460 result = PRAGMA_OACC_CLAUSE_AUTO;
10461 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10462 result = PRAGMA_OMP_CLAUSE_IF;
10463 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10464 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10465 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10466 result = PRAGMA_OMP_CLAUSE_FOR;
10467 else if (c_parser_next_token_is (parser, CPP_NAME))
10469 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10471 switch (p[0])
10473 case 'a':
10474 if (!strcmp ("aligned", p))
10475 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10476 else if (!strcmp ("async", p))
10477 result = PRAGMA_OACC_CLAUSE_ASYNC;
10478 break;
10479 case 'c':
10480 if (!strcmp ("collapse", p))
10481 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10482 else if (!strcmp ("copy", p))
10483 result = PRAGMA_OACC_CLAUSE_COPY;
10484 else if (!strcmp ("copyin", p))
10485 result = PRAGMA_OMP_CLAUSE_COPYIN;
10486 else if (!strcmp ("copyout", p))
10487 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10488 else if (!strcmp ("copyprivate", p))
10489 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10490 else if (!strcmp ("create", p))
10491 result = PRAGMA_OACC_CLAUSE_CREATE;
10492 break;
10493 case 'd':
10494 if (!strcmp ("defaultmap", p))
10495 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10496 else if (!strcmp ("delete", p))
10497 result = PRAGMA_OACC_CLAUSE_DELETE;
10498 else if (!strcmp ("depend", p))
10499 result = PRAGMA_OMP_CLAUSE_DEPEND;
10500 else if (!strcmp ("device", p))
10501 result = PRAGMA_OMP_CLAUSE_DEVICE;
10502 else if (!strcmp ("deviceptr", p))
10503 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10504 else if (!strcmp ("device_resident", p))
10505 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10506 else if (!strcmp ("dist_schedule", p))
10507 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10508 break;
10509 case 'f':
10510 if (!strcmp ("final", p))
10511 result = PRAGMA_OMP_CLAUSE_FINAL;
10512 else if (!strcmp ("firstprivate", p))
10513 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10514 else if (!strcmp ("from", p))
10515 result = PRAGMA_OMP_CLAUSE_FROM;
10516 break;
10517 case 'g':
10518 if (!strcmp ("gang", p))
10519 result = PRAGMA_OACC_CLAUSE_GANG;
10520 else if (!strcmp ("grainsize", p))
10521 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10522 break;
10523 case 'h':
10524 if (!strcmp ("hint", p))
10525 result = PRAGMA_OMP_CLAUSE_HINT;
10526 else if (!strcmp ("host", p))
10527 result = PRAGMA_OACC_CLAUSE_HOST;
10528 break;
10529 case 'i':
10530 if (!strcmp ("inbranch", p))
10531 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10532 else if (!strcmp ("independent", p))
10533 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10534 else if (!strcmp ("is_device_ptr", p))
10535 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10536 break;
10537 case 'l':
10538 if (!strcmp ("lastprivate", p))
10539 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10540 else if (!strcmp ("linear", p))
10541 result = PRAGMA_OMP_CLAUSE_LINEAR;
10542 else if (!strcmp ("link", p))
10543 result = PRAGMA_OMP_CLAUSE_LINK;
10544 break;
10545 case 'm':
10546 if (!strcmp ("map", p))
10547 result = PRAGMA_OMP_CLAUSE_MAP;
10548 else if (!strcmp ("mergeable", p))
10549 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10550 else if (flag_cilkplus && !strcmp ("mask", p))
10551 result = PRAGMA_CILK_CLAUSE_MASK;
10552 break;
10553 case 'n':
10554 if (!strcmp ("nogroup", p))
10555 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10556 else if (!strcmp ("notinbranch", p))
10557 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10558 else if (!strcmp ("nowait", p))
10559 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10560 else if (!strcmp ("num_gangs", p))
10561 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10562 else if (!strcmp ("num_tasks", p))
10563 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10564 else if (!strcmp ("num_teams", p))
10565 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10566 else if (!strcmp ("num_threads", p))
10567 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10568 else if (!strcmp ("num_workers", p))
10569 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10570 else if (flag_cilkplus && !strcmp ("nomask", p))
10571 result = PRAGMA_CILK_CLAUSE_NOMASK;
10572 break;
10573 case 'o':
10574 if (!strcmp ("ordered", p))
10575 result = PRAGMA_OMP_CLAUSE_ORDERED;
10576 break;
10577 case 'p':
10578 if (!strcmp ("parallel", p))
10579 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10580 else if (!strcmp ("present", p))
10581 result = PRAGMA_OACC_CLAUSE_PRESENT;
10582 else if (!strcmp ("present_or_copy", p)
10583 || !strcmp ("pcopy", p))
10584 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10585 else if (!strcmp ("present_or_copyin", p)
10586 || !strcmp ("pcopyin", p))
10587 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10588 else if (!strcmp ("present_or_copyout", p)
10589 || !strcmp ("pcopyout", p))
10590 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10591 else if (!strcmp ("present_or_create", p)
10592 || !strcmp ("pcreate", p))
10593 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10594 else if (!strcmp ("priority", p))
10595 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10596 else if (!strcmp ("private", p))
10597 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10598 else if (!strcmp ("proc_bind", p))
10599 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10600 break;
10601 case 'r':
10602 if (!strcmp ("reduction", p))
10603 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10604 break;
10605 case 's':
10606 if (!strcmp ("safelen", p))
10607 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10608 else if (!strcmp ("schedule", p))
10609 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10610 else if (!strcmp ("sections", p))
10611 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10612 else if (!strcmp ("seq", p))
10613 result = PRAGMA_OACC_CLAUSE_SEQ;
10614 else if (!strcmp ("shared", p))
10615 result = PRAGMA_OMP_CLAUSE_SHARED;
10616 else if (!strcmp ("simd", p))
10617 result = PRAGMA_OMP_CLAUSE_SIMD;
10618 else if (!strcmp ("simdlen", p))
10619 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10620 else if (!strcmp ("self", p))
10621 result = PRAGMA_OACC_CLAUSE_SELF;
10622 break;
10623 case 't':
10624 if (!strcmp ("taskgroup", p))
10625 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10626 else if (!strcmp ("thread_limit", p))
10627 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10628 else if (!strcmp ("threads", p))
10629 result = PRAGMA_OMP_CLAUSE_THREADS;
10630 else if (!strcmp ("tile", p))
10631 result = PRAGMA_OACC_CLAUSE_TILE;
10632 else if (!strcmp ("to", p))
10633 result = PRAGMA_OMP_CLAUSE_TO;
10634 break;
10635 case 'u':
10636 if (!strcmp ("uniform", p))
10637 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10638 else if (!strcmp ("untied", p))
10639 result = PRAGMA_OMP_CLAUSE_UNTIED;
10640 else if (!strcmp ("use_device", p))
10641 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10642 else if (!strcmp ("use_device_ptr", p))
10643 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10644 break;
10645 case 'v':
10646 if (!strcmp ("vector", p))
10647 result = PRAGMA_OACC_CLAUSE_VECTOR;
10648 else if (!strcmp ("vector_length", p))
10649 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10650 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10651 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10652 break;
10653 case 'w':
10654 if (!strcmp ("wait", p))
10655 result = PRAGMA_OACC_CLAUSE_WAIT;
10656 else if (!strcmp ("worker", p))
10657 result = PRAGMA_OACC_CLAUSE_WORKER;
10658 break;
10662 if (result != PRAGMA_OMP_CLAUSE_NONE)
10663 c_parser_consume_token (parser);
10665 return result;
10668 /* Validate that a clause of the given type does not already exist. */
10670 static void
10671 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10672 const char *name)
10674 tree c;
10676 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10677 if (OMP_CLAUSE_CODE (c) == code)
10679 location_t loc = OMP_CLAUSE_LOCATION (c);
10680 error_at (loc, "too many %qs clauses", name);
10681 break;
10685 /* OpenACC 2.0
10686 Parse wait clause or wait directive parameters. */
10688 static tree
10689 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10691 vec<tree, va_gc> *args;
10692 tree t, args_tree;
10694 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10695 return list;
10697 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10699 if (args->length () == 0)
10701 c_parser_error (parser, "expected integer expression before ')'");
10702 release_tree_vector (args);
10703 return list;
10706 args_tree = build_tree_list_vec (args);
10708 for (t = args_tree; t; t = TREE_CHAIN (t))
10710 tree targ = TREE_VALUE (t);
10712 if (targ != error_mark_node)
10714 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10716 c_parser_error (parser, "expression must be integral");
10717 targ = error_mark_node;
10719 else
10721 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10723 OMP_CLAUSE_DECL (c) = targ;
10724 OMP_CLAUSE_CHAIN (c) = list;
10725 list = c;
10730 release_tree_vector (args);
10731 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10732 return list;
10735 /* OpenACC 2.0, OpenMP 2.5:
10736 variable-list:
10737 identifier
10738 variable-list , identifier
10740 If KIND is nonzero, create the appropriate node and install the
10741 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10742 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10744 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10745 return the list created. */
10747 static tree
10748 c_parser_omp_variable_list (c_parser *parser,
10749 location_t clause_loc,
10750 enum omp_clause_code kind, tree list)
10752 if (c_parser_next_token_is_not (parser, CPP_NAME)
10753 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10754 c_parser_error (parser, "expected identifier");
10756 while (c_parser_next_token_is (parser, CPP_NAME)
10757 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10759 tree t = lookup_name (c_parser_peek_token (parser)->value);
10761 if (t == NULL_TREE)
10763 undeclared_variable (c_parser_peek_token (parser)->location,
10764 c_parser_peek_token (parser)->value);
10765 t = error_mark_node;
10768 c_parser_consume_token (parser);
10770 if (t == error_mark_node)
10772 else if (kind != 0)
10774 switch (kind)
10776 case OMP_CLAUSE__CACHE_:
10777 /* The OpenACC cache directive explicitly only allows "array
10778 elements or subarrays". */
10779 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10781 c_parser_error (parser, "expected %<[%>");
10782 t = error_mark_node;
10783 break;
10785 /* FALLTHROUGH */
10786 case OMP_CLAUSE_MAP:
10787 case OMP_CLAUSE_FROM:
10788 case OMP_CLAUSE_TO:
10789 while (c_parser_next_token_is (parser, CPP_DOT))
10791 location_t op_loc = c_parser_peek_token (parser)->location;
10792 c_parser_consume_token (parser);
10793 if (!c_parser_next_token_is (parser, CPP_NAME))
10795 c_parser_error (parser, "expected identifier");
10796 t = error_mark_node;
10797 break;
10800 c_token *comp_tok = c_parser_peek_token (parser);
10801 tree ident = comp_tok->value;
10802 location_t comp_loc = comp_tok->location;
10803 c_parser_consume_token (parser);
10804 t = build_component_ref (op_loc, t, ident, comp_loc);
10806 /* FALLTHROUGH */
10807 case OMP_CLAUSE_DEPEND:
10808 case OMP_CLAUSE_REDUCTION:
10809 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10811 tree low_bound = NULL_TREE, length = NULL_TREE;
10813 c_parser_consume_token (parser);
10814 if (!c_parser_next_token_is (parser, CPP_COLON))
10816 location_t expr_loc
10817 = c_parser_peek_token (parser)->location;
10818 c_expr expr = c_parser_expression (parser);
10819 expr = convert_lvalue_to_rvalue (expr_loc, expr,
10820 false, true);
10821 low_bound = expr.value;
10823 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10824 length = integer_one_node;
10825 else
10827 /* Look for `:'. */
10828 if (!c_parser_require (parser, CPP_COLON,
10829 "expected %<:%>"))
10831 t = error_mark_node;
10832 break;
10834 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10836 location_t expr_loc
10837 = c_parser_peek_token (parser)->location;
10838 c_expr expr = c_parser_expression (parser);
10839 expr = convert_lvalue_to_rvalue (expr_loc, expr,
10840 false, true);
10841 length = expr.value;
10844 /* Look for the closing `]'. */
10845 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10846 "expected %<]%>"))
10848 t = error_mark_node;
10849 break;
10852 t = tree_cons (low_bound, length, t);
10854 break;
10855 default:
10856 break;
10859 if (t != error_mark_node)
10861 tree u = build_omp_clause (clause_loc, kind);
10862 OMP_CLAUSE_DECL (u) = t;
10863 OMP_CLAUSE_CHAIN (u) = list;
10864 list = u;
10867 else
10868 list = tree_cons (t, NULL_TREE, list);
10870 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10871 break;
10873 c_parser_consume_token (parser);
10876 return list;
10879 /* Similarly, but expect leading and trailing parenthesis. This is a very
10880 common case for OpenACC and OpenMP clauses. */
10882 static tree
10883 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10884 tree list)
10886 /* The clauses location. */
10887 location_t loc = c_parser_peek_token (parser)->location;
10889 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10891 list = c_parser_omp_variable_list (parser, loc, kind, list);
10892 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10894 return list;
10897 /* OpenACC 2.0:
10898 copy ( variable-list )
10899 copyin ( variable-list )
10900 copyout ( variable-list )
10901 create ( variable-list )
10902 delete ( variable-list )
10903 present ( variable-list )
10904 present_or_copy ( variable-list )
10905 pcopy ( variable-list )
10906 present_or_copyin ( variable-list )
10907 pcopyin ( variable-list )
10908 present_or_copyout ( variable-list )
10909 pcopyout ( variable-list )
10910 present_or_create ( variable-list )
10911 pcreate ( variable-list ) */
10913 static tree
10914 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10915 tree list)
10917 enum gomp_map_kind kind;
10918 switch (c_kind)
10920 case PRAGMA_OACC_CLAUSE_COPY:
10921 kind = GOMP_MAP_FORCE_TOFROM;
10922 break;
10923 case PRAGMA_OACC_CLAUSE_COPYIN:
10924 kind = GOMP_MAP_FORCE_TO;
10925 break;
10926 case PRAGMA_OACC_CLAUSE_COPYOUT:
10927 kind = GOMP_MAP_FORCE_FROM;
10928 break;
10929 case PRAGMA_OACC_CLAUSE_CREATE:
10930 kind = GOMP_MAP_FORCE_ALLOC;
10931 break;
10932 case PRAGMA_OACC_CLAUSE_DELETE:
10933 kind = GOMP_MAP_DELETE;
10934 break;
10935 case PRAGMA_OACC_CLAUSE_DEVICE:
10936 kind = GOMP_MAP_FORCE_TO;
10937 break;
10938 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
10939 kind = GOMP_MAP_DEVICE_RESIDENT;
10940 break;
10941 case PRAGMA_OACC_CLAUSE_HOST:
10942 case PRAGMA_OACC_CLAUSE_SELF:
10943 kind = GOMP_MAP_FORCE_FROM;
10944 break;
10945 case PRAGMA_OACC_CLAUSE_LINK:
10946 kind = GOMP_MAP_LINK;
10947 break;
10948 case PRAGMA_OACC_CLAUSE_PRESENT:
10949 kind = GOMP_MAP_FORCE_PRESENT;
10950 break;
10951 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10952 kind = GOMP_MAP_TOFROM;
10953 break;
10954 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10955 kind = GOMP_MAP_TO;
10956 break;
10957 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10958 kind = GOMP_MAP_FROM;
10959 break;
10960 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10961 kind = GOMP_MAP_ALLOC;
10962 break;
10963 default:
10964 gcc_unreachable ();
10966 tree nl, c;
10967 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10969 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10970 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10972 return nl;
10975 /* OpenACC 2.0:
10976 deviceptr ( variable-list ) */
10978 static tree
10979 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10981 location_t loc = c_parser_peek_token (parser)->location;
10982 tree vars, t;
10984 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10985 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10986 variable-list must only allow for pointer variables. */
10987 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10988 for (t = vars; t && t; t = TREE_CHAIN (t))
10990 tree v = TREE_PURPOSE (t);
10992 /* FIXME diagnostics: Ideally we should keep individual
10993 locations for all the variables in the var list to make the
10994 following errors more precise. Perhaps
10995 c_parser_omp_var_list_parens() should construct a list of
10996 locations to go along with the var list. */
10998 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
10999 error_at (loc, "%qD is not a variable", v);
11000 else if (TREE_TYPE (v) == error_mark_node)
11002 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11003 error_at (loc, "%qD is not a pointer variable", v);
11005 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11006 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11007 OMP_CLAUSE_DECL (u) = v;
11008 OMP_CLAUSE_CHAIN (u) = list;
11009 list = u;
11012 return list;
11015 /* OpenACC 2.0, OpenMP 3.0:
11016 collapse ( constant-expression ) */
11018 static tree
11019 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11021 tree c, num = error_mark_node;
11022 HOST_WIDE_INT n;
11023 location_t loc;
11025 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11027 loc = c_parser_peek_token (parser)->location;
11028 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11030 num = c_parser_expr_no_commas (parser, NULL).value;
11031 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11033 if (num == error_mark_node)
11034 return list;
11035 mark_exp_read (num);
11036 num = c_fully_fold (num, false, NULL);
11037 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11038 || !tree_fits_shwi_p (num)
11039 || (n = tree_to_shwi (num)) <= 0
11040 || (int) n != n)
11042 error_at (loc,
11043 "collapse argument needs positive constant integer expression");
11044 return list;
11046 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11047 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11048 OMP_CLAUSE_CHAIN (c) = list;
11049 return c;
11052 /* OpenMP 2.5:
11053 copyin ( variable-list ) */
11055 static tree
11056 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11058 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11061 /* OpenMP 2.5:
11062 copyprivate ( variable-list ) */
11064 static tree
11065 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11067 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11070 /* OpenMP 2.5:
11071 default ( shared | none )
11073 OpenACC 2.0:
11074 default (none) */
11076 static tree
11077 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11079 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11080 location_t loc = c_parser_peek_token (parser)->location;
11081 tree c;
11083 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11084 return list;
11085 if (c_parser_next_token_is (parser, CPP_NAME))
11087 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11089 switch (p[0])
11091 case 'n':
11092 if (strcmp ("none", p) != 0)
11093 goto invalid_kind;
11094 kind = OMP_CLAUSE_DEFAULT_NONE;
11095 break;
11097 case 's':
11098 if (strcmp ("shared", p) != 0 || is_oacc)
11099 goto invalid_kind;
11100 kind = OMP_CLAUSE_DEFAULT_SHARED;
11101 break;
11103 default:
11104 goto invalid_kind;
11107 c_parser_consume_token (parser);
11109 else
11111 invalid_kind:
11112 if (is_oacc)
11113 c_parser_error (parser, "expected %<none%>");
11114 else
11115 c_parser_error (parser, "expected %<none%> or %<shared%>");
11117 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11119 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11120 return list;
11122 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11123 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11124 OMP_CLAUSE_CHAIN (c) = list;
11125 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11127 return c;
11130 /* OpenMP 2.5:
11131 firstprivate ( variable-list ) */
11133 static tree
11134 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11136 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11139 /* OpenMP 3.1:
11140 final ( expression ) */
11142 static tree
11143 c_parser_omp_clause_final (c_parser *parser, tree list)
11145 location_t loc = c_parser_peek_token (parser)->location;
11146 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11148 tree t = c_parser_paren_condition (parser);
11149 tree c;
11151 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11153 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11154 OMP_CLAUSE_FINAL_EXPR (c) = t;
11155 OMP_CLAUSE_CHAIN (c) = list;
11156 list = c;
11158 else
11159 c_parser_error (parser, "expected %<(%>");
11161 return list;
11164 /* OpenACC, OpenMP 2.5:
11165 if ( expression )
11167 OpenMP 4.5:
11168 if ( directive-name-modifier : expression )
11170 directive-name-modifier:
11171 parallel | task | taskloop | target data | target | target update
11172 | target enter data | target exit data */
11174 static tree
11175 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11177 location_t location = c_parser_peek_token (parser)->location;
11178 enum tree_code if_modifier = ERROR_MARK;
11180 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11181 return list;
11183 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11185 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11186 int n = 2;
11187 if (strcmp (p, "parallel") == 0)
11188 if_modifier = OMP_PARALLEL;
11189 else if (strcmp (p, "task") == 0)
11190 if_modifier = OMP_TASK;
11191 else if (strcmp (p, "taskloop") == 0)
11192 if_modifier = OMP_TASKLOOP;
11193 else if (strcmp (p, "target") == 0)
11195 if_modifier = OMP_TARGET;
11196 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11198 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11199 if (strcmp ("data", p) == 0)
11200 if_modifier = OMP_TARGET_DATA;
11201 else if (strcmp ("update", p) == 0)
11202 if_modifier = OMP_TARGET_UPDATE;
11203 else if (strcmp ("enter", p) == 0)
11204 if_modifier = OMP_TARGET_ENTER_DATA;
11205 else if (strcmp ("exit", p) == 0)
11206 if_modifier = OMP_TARGET_EXIT_DATA;
11207 if (if_modifier != OMP_TARGET)
11209 n = 3;
11210 c_parser_consume_token (parser);
11212 else
11214 location_t loc = c_parser_peek_2nd_token (parser)->location;
11215 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11216 "or %<exit%>");
11217 if_modifier = ERROR_MARK;
11219 if (if_modifier == OMP_TARGET_ENTER_DATA
11220 || if_modifier == OMP_TARGET_EXIT_DATA)
11222 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11224 p = IDENTIFIER_POINTER
11225 (c_parser_peek_2nd_token (parser)->value);
11226 if (strcmp ("data", p) == 0)
11227 n = 4;
11229 if (n == 4)
11230 c_parser_consume_token (parser);
11231 else
11233 location_t loc
11234 = c_parser_peek_2nd_token (parser)->location;
11235 error_at (loc, "expected %<data%>");
11236 if_modifier = ERROR_MARK;
11241 if (if_modifier != ERROR_MARK)
11243 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11245 c_parser_consume_token (parser);
11246 c_parser_consume_token (parser);
11248 else
11250 if (n > 2)
11252 location_t loc = c_parser_peek_2nd_token (parser)->location;
11253 error_at (loc, "expected %<:%>");
11255 if_modifier = ERROR_MARK;
11260 tree t = c_parser_condition (parser), c;
11261 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11263 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11264 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11266 if (if_modifier != ERROR_MARK
11267 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11269 const char *p = NULL;
11270 switch (if_modifier)
11272 case OMP_PARALLEL: p = "parallel"; break;
11273 case OMP_TASK: p = "task"; break;
11274 case OMP_TASKLOOP: p = "taskloop"; break;
11275 case OMP_TARGET_DATA: p = "target data"; break;
11276 case OMP_TARGET: p = "target"; break;
11277 case OMP_TARGET_UPDATE: p = "target update"; break;
11278 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11279 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11280 default: gcc_unreachable ();
11282 error_at (location, "too many %<if%> clauses with %qs modifier",
11284 return list;
11286 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11288 if (!is_omp)
11289 error_at (location, "too many %<if%> clauses");
11290 else
11291 error_at (location, "too many %<if%> clauses without modifier");
11292 return list;
11294 else if (if_modifier == ERROR_MARK
11295 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11297 error_at (location, "if any %<if%> clause has modifier, then all "
11298 "%<if%> clauses have to use modifier");
11299 return list;
11303 c = build_omp_clause (location, OMP_CLAUSE_IF);
11304 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11305 OMP_CLAUSE_IF_EXPR (c) = t;
11306 OMP_CLAUSE_CHAIN (c) = list;
11307 return c;
11310 /* OpenMP 2.5:
11311 lastprivate ( variable-list ) */
11313 static tree
11314 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11316 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11319 /* OpenMP 3.1:
11320 mergeable */
11322 static tree
11323 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11325 tree c;
11327 /* FIXME: Should we allow duplicates? */
11328 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11330 c = build_omp_clause (c_parser_peek_token (parser)->location,
11331 OMP_CLAUSE_MERGEABLE);
11332 OMP_CLAUSE_CHAIN (c) = list;
11334 return c;
11337 /* OpenMP 2.5:
11338 nowait */
11340 static tree
11341 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11343 tree c;
11344 location_t loc = c_parser_peek_token (parser)->location;
11346 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11348 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11349 OMP_CLAUSE_CHAIN (c) = list;
11350 return c;
11353 /* OpenACC:
11354 num_gangs ( expression ) */
11356 static tree
11357 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11359 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11360 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11362 location_t expr_loc = c_parser_peek_token (parser)->location;
11363 c_expr expr = c_parser_expression (parser);
11364 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11365 tree c, t = expr.value;
11366 t = c_fully_fold (t, false, NULL);
11368 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11370 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11372 c_parser_error (parser, "expected integer expression");
11373 return list;
11376 /* Attempt to statically determine when the number isn't positive. */
11377 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11378 build_int_cst (TREE_TYPE (t), 0));
11379 protected_set_expr_location (c, expr_loc);
11380 if (c == boolean_true_node)
11382 warning_at (expr_loc, 0,
11383 "%<num_gangs%> value must be positive");
11384 t = integer_one_node;
11387 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11389 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11390 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11391 OMP_CLAUSE_CHAIN (c) = list;
11392 list = c;
11395 return list;
11398 /* OpenMP 2.5:
11399 num_threads ( expression ) */
11401 static tree
11402 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11404 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11405 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11407 location_t expr_loc = c_parser_peek_token (parser)->location;
11408 c_expr expr = c_parser_expression (parser);
11409 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11410 tree c, t = expr.value;
11411 t = c_fully_fold (t, false, NULL);
11413 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11415 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11417 c_parser_error (parser, "expected integer expression");
11418 return list;
11421 /* Attempt to statically determine when the number isn't positive. */
11422 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11423 build_int_cst (TREE_TYPE (t), 0));
11424 protected_set_expr_location (c, expr_loc);
11425 if (c == boolean_true_node)
11427 warning_at (expr_loc, 0,
11428 "%<num_threads%> value must be positive");
11429 t = integer_one_node;
11432 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11434 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11435 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11436 OMP_CLAUSE_CHAIN (c) = list;
11437 list = c;
11440 return list;
11443 /* OpenMP 4.5:
11444 num_tasks ( expression ) */
11446 static tree
11447 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11449 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11450 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11452 location_t expr_loc = c_parser_peek_token (parser)->location;
11453 c_expr expr = c_parser_expression (parser);
11454 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11455 tree c, t = expr.value;
11456 t = c_fully_fold (t, false, NULL);
11458 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11460 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11462 c_parser_error (parser, "expected integer expression");
11463 return list;
11466 /* Attempt to statically determine when the number isn't positive. */
11467 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11468 build_int_cst (TREE_TYPE (t), 0));
11469 if (CAN_HAVE_LOCATION_P (c))
11470 SET_EXPR_LOCATION (c, expr_loc);
11471 if (c == boolean_true_node)
11473 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11474 t = integer_one_node;
11477 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11479 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11480 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11481 OMP_CLAUSE_CHAIN (c) = list;
11482 list = c;
11485 return list;
11488 /* OpenMP 4.5:
11489 grainsize ( expression ) */
11491 static tree
11492 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11494 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11495 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11497 location_t expr_loc = c_parser_peek_token (parser)->location;
11498 c_expr expr = c_parser_expression (parser);
11499 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11500 tree c, t = expr.value;
11501 t = c_fully_fold (t, false, NULL);
11503 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11505 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11507 c_parser_error (parser, "expected integer expression");
11508 return list;
11511 /* Attempt to statically determine when the number isn't positive. */
11512 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11513 build_int_cst (TREE_TYPE (t), 0));
11514 if (CAN_HAVE_LOCATION_P (c))
11515 SET_EXPR_LOCATION (c, expr_loc);
11516 if (c == boolean_true_node)
11518 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11519 t = integer_one_node;
11522 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11524 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11525 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11526 OMP_CLAUSE_CHAIN (c) = list;
11527 list = c;
11530 return list;
11533 /* OpenMP 4.5:
11534 priority ( expression ) */
11536 static tree
11537 c_parser_omp_clause_priority (c_parser *parser, tree list)
11539 location_t priority_loc = c_parser_peek_token (parser)->location;
11540 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11542 location_t expr_loc = c_parser_peek_token (parser)->location;
11543 c_expr expr = c_parser_expression (parser);
11544 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11545 tree c, t = expr.value;
11546 t = c_fully_fold (t, false, NULL);
11548 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11550 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11552 c_parser_error (parser, "expected integer expression");
11553 return list;
11556 /* Attempt to statically determine when the number isn't
11557 non-negative. */
11558 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11559 build_int_cst (TREE_TYPE (t), 0));
11560 if (CAN_HAVE_LOCATION_P (c))
11561 SET_EXPR_LOCATION (c, expr_loc);
11562 if (c == boolean_true_node)
11564 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11565 t = integer_one_node;
11568 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11570 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11571 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11572 OMP_CLAUSE_CHAIN (c) = list;
11573 list = c;
11576 return list;
11579 /* OpenMP 4.5:
11580 hint ( expression ) */
11582 static tree
11583 c_parser_omp_clause_hint (c_parser *parser, tree list)
11585 location_t hint_loc = c_parser_peek_token (parser)->location;
11586 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11588 location_t expr_loc = c_parser_peek_token (parser)->location;
11589 c_expr expr = c_parser_expression (parser);
11590 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11591 tree c, t = expr.value;
11592 t = c_fully_fold (t, false, NULL);
11594 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11596 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11598 c_parser_error (parser, "expected integer expression");
11599 return list;
11602 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11604 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11605 OMP_CLAUSE_HINT_EXPR (c) = t;
11606 OMP_CLAUSE_CHAIN (c) = list;
11607 list = c;
11610 return list;
11613 /* OpenMP 4.5:
11614 defaultmap ( tofrom : scalar ) */
11616 static tree
11617 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11619 location_t loc = c_parser_peek_token (parser)->location;
11620 tree c;
11621 const char *p;
11623 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11624 return list;
11625 if (!c_parser_next_token_is (parser, CPP_NAME))
11627 c_parser_error (parser, "expected %<tofrom%>");
11628 goto out_err;
11630 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11631 if (strcmp (p, "tofrom") != 0)
11633 c_parser_error (parser, "expected %<tofrom%>");
11634 goto out_err;
11636 c_parser_consume_token (parser);
11637 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11638 goto out_err;
11639 if (!c_parser_next_token_is (parser, CPP_NAME))
11641 c_parser_error (parser, "expected %<scalar%>");
11642 goto out_err;
11644 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11645 if (strcmp (p, "scalar") != 0)
11647 c_parser_error (parser, "expected %<scalar%>");
11648 goto out_err;
11650 c_parser_consume_token (parser);
11651 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11652 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11653 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11654 OMP_CLAUSE_CHAIN (c) = list;
11655 return c;
11657 out_err:
11658 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11659 return list;
11662 /* OpenACC 2.0:
11663 use_device ( variable-list )
11665 OpenMP 4.5:
11666 use_device_ptr ( variable-list ) */
11668 static tree
11669 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11671 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11672 list);
11675 /* OpenMP 4.5:
11676 is_device_ptr ( variable-list ) */
11678 static tree
11679 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11681 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11684 /* OpenACC:
11685 num_workers ( expression ) */
11687 static tree
11688 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11690 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11691 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11693 location_t expr_loc = c_parser_peek_token (parser)->location;
11694 c_expr expr = c_parser_expression (parser);
11695 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11696 tree c, t = expr.value;
11697 t = c_fully_fold (t, false, NULL);
11699 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11701 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11703 c_parser_error (parser, "expected integer expression");
11704 return list;
11707 /* Attempt to statically determine when the number isn't positive. */
11708 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11709 build_int_cst (TREE_TYPE (t), 0));
11710 protected_set_expr_location (c, expr_loc);
11711 if (c == boolean_true_node)
11713 warning_at (expr_loc, 0,
11714 "%<num_workers%> value must be positive");
11715 t = integer_one_node;
11718 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11720 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11721 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11722 OMP_CLAUSE_CHAIN (c) = list;
11723 list = c;
11726 return list;
11729 /* OpenACC:
11731 gang [( gang-arg-list )]
11732 worker [( [num:] int-expr )]
11733 vector [( [length:] int-expr )]
11735 where gang-arg is one of:
11737 [num:] int-expr
11738 static: size-expr
11740 and size-expr may be:
11743 int-expr
11746 static tree
11747 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11748 const char *str, tree list)
11750 const char *id = "num";
11751 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11752 location_t loc = c_parser_peek_token (parser)->location;
11754 if (kind == OMP_CLAUSE_VECTOR)
11755 id = "length";
11757 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11759 c_parser_consume_token (parser);
11763 c_token *next = c_parser_peek_token (parser);
11764 int idx = 0;
11766 /* Gang static argument. */
11767 if (kind == OMP_CLAUSE_GANG
11768 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11770 c_parser_consume_token (parser);
11772 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11773 goto cleanup_error;
11775 idx = 1;
11776 if (ops[idx] != NULL_TREE)
11778 c_parser_error (parser, "too many %<static%> arguments");
11779 goto cleanup_error;
11782 /* Check for the '*' argument. */
11783 if (c_parser_next_token_is (parser, CPP_MULT)
11784 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11785 || c_parser_peek_2nd_token (parser)->type
11786 == CPP_CLOSE_PAREN))
11788 c_parser_consume_token (parser);
11789 ops[idx] = integer_minus_one_node;
11791 if (c_parser_next_token_is (parser, CPP_COMMA))
11793 c_parser_consume_token (parser);
11794 continue;
11796 else
11797 break;
11800 /* Worker num: argument and vector length: arguments. */
11801 else if (c_parser_next_token_is (parser, CPP_NAME)
11802 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11803 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11805 c_parser_consume_token (parser); /* id */
11806 c_parser_consume_token (parser); /* ':' */
11809 /* Now collect the actual argument. */
11810 if (ops[idx] != NULL_TREE)
11812 c_parser_error (parser, "unexpected argument");
11813 goto cleanup_error;
11816 location_t expr_loc = c_parser_peek_token (parser)->location;
11817 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11818 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11819 tree expr = cexpr.value;
11820 if (expr == error_mark_node)
11821 goto cleanup_error;
11823 expr = c_fully_fold (expr, false, NULL);
11825 /* Attempt to statically determine when the number isn't a
11826 positive integer. */
11828 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11830 c_parser_error (parser, "expected integer expression");
11831 return list;
11834 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11835 build_int_cst (TREE_TYPE (expr), 0));
11836 if (c == boolean_true_node)
11838 warning_at (loc, 0,
11839 "%<%s%> value must be positive", str);
11840 expr = integer_one_node;
11843 ops[idx] = expr;
11845 if (kind == OMP_CLAUSE_GANG
11846 && c_parser_next_token_is (parser, CPP_COMMA))
11848 c_parser_consume_token (parser);
11849 continue;
11851 break;
11853 while (1);
11855 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11856 goto cleanup_error;
11859 check_no_duplicate_clause (list, kind, str);
11861 c = build_omp_clause (loc, kind);
11863 if (ops[1])
11864 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11866 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11867 OMP_CLAUSE_CHAIN (c) = list;
11869 return c;
11871 cleanup_error:
11872 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11873 return list;
11876 /* OpenACC:
11877 auto
11878 independent
11879 nohost
11880 seq */
11882 static tree
11883 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11884 tree list)
11886 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11888 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11889 OMP_CLAUSE_CHAIN (c) = list;
11891 return c;
11894 /* OpenACC:
11895 async [( int-expr )] */
11897 static tree
11898 c_parser_oacc_clause_async (c_parser *parser, tree list)
11900 tree c, t;
11901 location_t loc = c_parser_peek_token (parser)->location;
11903 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11905 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11907 c_parser_consume_token (parser);
11909 t = c_parser_expression (parser).value;
11910 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11911 c_parser_error (parser, "expected integer expression");
11912 else if (t == error_mark_node
11913 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11914 return list;
11916 else
11917 t = c_fully_fold (t, false, NULL);
11919 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11921 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11922 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11923 OMP_CLAUSE_CHAIN (c) = list;
11924 list = c;
11926 return list;
11929 /* OpenACC 2.0:
11930 tile ( size-expr-list ) */
11932 static tree
11933 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11935 tree c, expr = error_mark_node;
11936 location_t loc, expr_loc;
11937 tree tile = NULL_TREE;
11939 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11941 loc = c_parser_peek_token (parser)->location;
11942 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11943 return list;
11947 if (c_parser_next_token_is (parser, CPP_MULT)
11948 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11949 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11951 c_parser_consume_token (parser);
11952 expr = integer_minus_one_node;
11954 else
11956 expr_loc = c_parser_peek_token (parser)->location;
11957 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11958 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11959 expr = cexpr.value;
11961 if (expr == error_mark_node)
11963 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11964 "expected %<)%>");
11965 return list;
11968 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11970 c_parser_error (parser, "%<tile%> value must be integral");
11971 return list;
11974 expr = c_fully_fold (expr, false, NULL);
11976 /* Attempt to statically determine when expr isn't positive. */
11977 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11978 build_int_cst (TREE_TYPE (expr), 0));
11979 protected_set_expr_location (c, expr_loc);
11980 if (c == boolean_true_node)
11982 warning_at (expr_loc, 0,"%<tile%> value must be positive");
11983 expr = integer_one_node;
11987 tile = tree_cons (NULL_TREE, expr, tile);
11988 if (c_parser_next_token_is (parser, CPP_COMMA))
11989 c_parser_consume_token (parser);
11991 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11993 /* Consume the trailing ')'. */
11994 c_parser_consume_token (parser);
11996 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11997 tile = nreverse (tile);
11998 OMP_CLAUSE_TILE_LIST (c) = tile;
11999 OMP_CLAUSE_CHAIN (c) = list;
12000 return c;
12003 /* OpenACC:
12004 wait ( int-expr-list ) */
12006 static tree
12007 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12009 location_t clause_loc = c_parser_peek_token (parser)->location;
12011 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12012 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12014 return list;
12017 /* OpenMP 2.5:
12018 ordered
12020 OpenMP 4.5:
12021 ordered ( constant-expression ) */
12023 static tree
12024 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12026 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12028 tree c, num = NULL_TREE;
12029 HOST_WIDE_INT n;
12030 location_t loc = c_parser_peek_token (parser)->location;
12031 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12033 c_parser_consume_token (parser);
12034 num = c_parser_expr_no_commas (parser, NULL).value;
12035 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12037 if (num == error_mark_node)
12038 return list;
12039 if (num)
12041 mark_exp_read (num);
12042 num = c_fully_fold (num, false, NULL);
12043 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12044 || !tree_fits_shwi_p (num)
12045 || (n = tree_to_shwi (num)) <= 0
12046 || (int) n != n)
12048 error_at (loc, "ordered argument needs positive "
12049 "constant integer expression");
12050 return list;
12053 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12054 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12055 OMP_CLAUSE_CHAIN (c) = list;
12056 return c;
12059 /* OpenMP 2.5:
12060 private ( variable-list ) */
12062 static tree
12063 c_parser_omp_clause_private (c_parser *parser, tree list)
12065 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12068 /* OpenMP 2.5:
12069 reduction ( reduction-operator : variable-list )
12071 reduction-operator:
12072 One of: + * - & ^ | && ||
12074 OpenMP 3.1:
12076 reduction-operator:
12077 One of: + * - & ^ | && || max min
12079 OpenMP 4.0:
12081 reduction-operator:
12082 One of: + * - & ^ | && ||
12083 identifier */
12085 static tree
12086 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12088 location_t clause_loc = c_parser_peek_token (parser)->location;
12089 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12091 enum tree_code code = ERROR_MARK;
12092 tree reduc_id = NULL_TREE;
12094 switch (c_parser_peek_token (parser)->type)
12096 case CPP_PLUS:
12097 code = PLUS_EXPR;
12098 break;
12099 case CPP_MULT:
12100 code = MULT_EXPR;
12101 break;
12102 case CPP_MINUS:
12103 code = MINUS_EXPR;
12104 break;
12105 case CPP_AND:
12106 code = BIT_AND_EXPR;
12107 break;
12108 case CPP_XOR:
12109 code = BIT_XOR_EXPR;
12110 break;
12111 case CPP_OR:
12112 code = BIT_IOR_EXPR;
12113 break;
12114 case CPP_AND_AND:
12115 code = TRUTH_ANDIF_EXPR;
12116 break;
12117 case CPP_OR_OR:
12118 code = TRUTH_ORIF_EXPR;
12119 break;
12120 case CPP_NAME:
12122 const char *p
12123 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12124 if (strcmp (p, "min") == 0)
12126 code = MIN_EXPR;
12127 break;
12129 if (strcmp (p, "max") == 0)
12131 code = MAX_EXPR;
12132 break;
12134 reduc_id = c_parser_peek_token (parser)->value;
12135 break;
12137 default:
12138 c_parser_error (parser,
12139 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12140 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
12141 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12142 return list;
12144 c_parser_consume_token (parser);
12145 reduc_id = c_omp_reduction_id (code, reduc_id);
12146 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12148 tree nl, c;
12150 nl = c_parser_omp_variable_list (parser, clause_loc,
12151 OMP_CLAUSE_REDUCTION, list);
12152 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12154 tree d = OMP_CLAUSE_DECL (c), type;
12155 if (TREE_CODE (d) != TREE_LIST)
12156 type = TREE_TYPE (d);
12157 else
12159 int cnt = 0;
12160 tree t;
12161 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12162 cnt++;
12163 type = TREE_TYPE (t);
12164 while (cnt > 0)
12166 if (TREE_CODE (type) != POINTER_TYPE
12167 && TREE_CODE (type) != ARRAY_TYPE)
12168 break;
12169 type = TREE_TYPE (type);
12170 cnt--;
12173 while (TREE_CODE (type) == ARRAY_TYPE)
12174 type = TREE_TYPE (type);
12175 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12176 if (code == ERROR_MARK
12177 || !(INTEGRAL_TYPE_P (type)
12178 || TREE_CODE (type) == REAL_TYPE
12179 || TREE_CODE (type) == COMPLEX_TYPE))
12180 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12181 = c_omp_reduction_lookup (reduc_id,
12182 TYPE_MAIN_VARIANT (type));
12185 list = nl;
12187 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12189 return list;
12192 /* OpenMP 2.5:
12193 schedule ( schedule-kind )
12194 schedule ( schedule-kind , expression )
12196 schedule-kind:
12197 static | dynamic | guided | runtime | auto
12199 OpenMP 4.5:
12200 schedule ( schedule-modifier : schedule-kind )
12201 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12203 schedule-modifier:
12204 simd
12205 monotonic
12206 nonmonotonic */
12208 static tree
12209 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12211 tree c, t;
12212 location_t loc = c_parser_peek_token (parser)->location;
12213 int modifiers = 0, nmodifiers = 0;
12215 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12216 return list;
12218 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12220 while (c_parser_next_token_is (parser, CPP_NAME))
12222 tree kind = c_parser_peek_token (parser)->value;
12223 const char *p = IDENTIFIER_POINTER (kind);
12224 if (strcmp ("simd", p) == 0)
12225 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12226 else if (strcmp ("monotonic", p) == 0)
12227 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12228 else if (strcmp ("nonmonotonic", p) == 0)
12229 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12230 else
12231 break;
12232 c_parser_consume_token (parser);
12233 if (nmodifiers++ == 0
12234 && c_parser_next_token_is (parser, CPP_COMMA))
12235 c_parser_consume_token (parser);
12236 else
12238 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12239 break;
12243 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12244 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12245 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12246 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12248 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12249 "specified");
12250 modifiers = 0;
12253 if (c_parser_next_token_is (parser, CPP_NAME))
12255 tree kind = c_parser_peek_token (parser)->value;
12256 const char *p = IDENTIFIER_POINTER (kind);
12258 switch (p[0])
12260 case 'd':
12261 if (strcmp ("dynamic", p) != 0)
12262 goto invalid_kind;
12263 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12264 break;
12266 case 'g':
12267 if (strcmp ("guided", p) != 0)
12268 goto invalid_kind;
12269 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12270 break;
12272 case 'r':
12273 if (strcmp ("runtime", p) != 0)
12274 goto invalid_kind;
12275 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12276 break;
12278 default:
12279 goto invalid_kind;
12282 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12283 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12284 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12285 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12286 else
12287 goto invalid_kind;
12289 c_parser_consume_token (parser);
12290 if (c_parser_next_token_is (parser, CPP_COMMA))
12292 location_t here;
12293 c_parser_consume_token (parser);
12295 here = c_parser_peek_token (parser)->location;
12296 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12297 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12298 t = expr.value;
12299 t = c_fully_fold (t, false, NULL);
12301 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12302 error_at (here, "schedule %<runtime%> does not take "
12303 "a %<chunk_size%> parameter");
12304 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12305 error_at (here,
12306 "schedule %<auto%> does not take "
12307 "a %<chunk_size%> parameter");
12308 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12310 /* Attempt to statically determine when the number isn't
12311 positive. */
12312 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12313 build_int_cst (TREE_TYPE (t), 0));
12314 protected_set_expr_location (s, loc);
12315 if (s == boolean_true_node)
12317 warning_at (loc, 0,
12318 "chunk size value must be positive");
12319 t = integer_one_node;
12321 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12323 else
12324 c_parser_error (parser, "expected integer expression");
12326 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12328 else
12329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12330 "expected %<,%> or %<)%>");
12332 OMP_CLAUSE_SCHEDULE_KIND (c)
12333 = (enum omp_clause_schedule_kind)
12334 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12336 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12337 OMP_CLAUSE_CHAIN (c) = list;
12338 return c;
12340 invalid_kind:
12341 c_parser_error (parser, "invalid schedule kind");
12342 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12343 return list;
12346 /* OpenMP 2.5:
12347 shared ( variable-list ) */
12349 static tree
12350 c_parser_omp_clause_shared (c_parser *parser, tree list)
12352 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12355 /* OpenMP 3.0:
12356 untied */
12358 static tree
12359 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12361 tree c;
12363 /* FIXME: Should we allow duplicates? */
12364 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12366 c = build_omp_clause (c_parser_peek_token (parser)->location,
12367 OMP_CLAUSE_UNTIED);
12368 OMP_CLAUSE_CHAIN (c) = list;
12370 return c;
12373 /* OpenACC:
12374 vector_length ( expression ) */
12376 static tree
12377 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12379 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12380 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12382 location_t expr_loc = c_parser_peek_token (parser)->location;
12383 c_expr expr = c_parser_expression (parser);
12384 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12385 tree c, t = expr.value;
12386 t = c_fully_fold (t, false, NULL);
12388 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12390 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12392 c_parser_error (parser, "expected integer expression");
12393 return list;
12396 /* Attempt to statically determine when the number isn't positive. */
12397 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12398 build_int_cst (TREE_TYPE (t), 0));
12399 protected_set_expr_location (c, expr_loc);
12400 if (c == boolean_true_node)
12402 warning_at (expr_loc, 0,
12403 "%<vector_length%> value must be positive");
12404 t = integer_one_node;
12407 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12409 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12410 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12411 OMP_CLAUSE_CHAIN (c) = list;
12412 list = c;
12415 return list;
12418 /* OpenMP 4.0:
12419 inbranch
12420 notinbranch */
12422 static tree
12423 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12424 enum omp_clause_code code, tree list)
12426 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12428 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12429 OMP_CLAUSE_CHAIN (c) = list;
12431 return c;
12434 /* OpenMP 4.0:
12435 parallel
12437 sections
12438 taskgroup */
12440 static tree
12441 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12442 enum omp_clause_code code, tree list)
12444 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12445 OMP_CLAUSE_CHAIN (c) = list;
12447 return c;
12450 /* OpenMP 4.5:
12451 nogroup */
12453 static tree
12454 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12456 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12457 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12458 OMP_CLAUSE_NOGROUP);
12459 OMP_CLAUSE_CHAIN (c) = list;
12460 return c;
12463 /* OpenMP 4.5:
12464 simd
12465 threads */
12467 static tree
12468 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12469 enum omp_clause_code code, tree list)
12471 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12472 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12473 OMP_CLAUSE_CHAIN (c) = list;
12474 return c;
12477 /* OpenMP 4.0:
12478 num_teams ( expression ) */
12480 static tree
12481 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12483 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12484 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12486 location_t expr_loc = c_parser_peek_token (parser)->location;
12487 c_expr expr = c_parser_expression (parser);
12488 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12489 tree c, t = expr.value;
12490 t = c_fully_fold (t, false, NULL);
12492 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12494 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12496 c_parser_error (parser, "expected integer expression");
12497 return list;
12500 /* Attempt to statically determine when the number isn't positive. */
12501 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12502 build_int_cst (TREE_TYPE (t), 0));
12503 protected_set_expr_location (c, expr_loc);
12504 if (c == boolean_true_node)
12506 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12507 t = integer_one_node;
12510 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12512 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12513 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12514 OMP_CLAUSE_CHAIN (c) = list;
12515 list = c;
12518 return list;
12521 /* OpenMP 4.0:
12522 thread_limit ( expression ) */
12524 static tree
12525 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12527 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12528 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12530 location_t expr_loc = c_parser_peek_token (parser)->location;
12531 c_expr expr = c_parser_expression (parser);
12532 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12533 tree c, t = expr.value;
12534 t = c_fully_fold (t, false, NULL);
12536 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12538 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12540 c_parser_error (parser, "expected integer expression");
12541 return list;
12544 /* Attempt to statically determine when the number isn't positive. */
12545 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12546 build_int_cst (TREE_TYPE (t), 0));
12547 protected_set_expr_location (c, expr_loc);
12548 if (c == boolean_true_node)
12550 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12551 t = integer_one_node;
12554 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12555 "thread_limit");
12557 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12558 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12559 OMP_CLAUSE_CHAIN (c) = list;
12560 list = c;
12563 return list;
12566 /* OpenMP 4.0:
12567 aligned ( variable-list )
12568 aligned ( variable-list : constant-expression ) */
12570 static tree
12571 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12573 location_t clause_loc = c_parser_peek_token (parser)->location;
12574 tree nl, c;
12576 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12577 return list;
12579 nl = c_parser_omp_variable_list (parser, clause_loc,
12580 OMP_CLAUSE_ALIGNED, list);
12582 if (c_parser_next_token_is (parser, CPP_COLON))
12584 c_parser_consume_token (parser);
12585 location_t expr_loc = c_parser_peek_token (parser)->location;
12586 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12587 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12588 tree alignment = expr.value;
12589 alignment = c_fully_fold (alignment, false, NULL);
12590 if (TREE_CODE (alignment) != INTEGER_CST
12591 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12592 || tree_int_cst_sgn (alignment) != 1)
12594 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12595 "be positive constant integer expression");
12596 alignment = NULL_TREE;
12599 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12600 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12603 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12604 return nl;
12607 /* OpenMP 4.0:
12608 linear ( variable-list )
12609 linear ( variable-list : expression )
12611 OpenMP 4.5:
12612 linear ( modifier ( variable-list ) )
12613 linear ( modifier ( variable-list ) : expression ) */
12615 static tree
12616 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12618 location_t clause_loc = c_parser_peek_token (parser)->location;
12619 tree nl, c, step;
12620 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12622 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12623 return list;
12625 if (!is_cilk_simd_fn
12626 && c_parser_next_token_is (parser, CPP_NAME))
12628 c_token *tok = c_parser_peek_token (parser);
12629 const char *p = IDENTIFIER_POINTER (tok->value);
12630 if (strcmp ("val", p) == 0)
12631 kind = OMP_CLAUSE_LINEAR_VAL;
12632 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12633 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12634 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12636 c_parser_consume_token (parser);
12637 c_parser_consume_token (parser);
12641 nl = c_parser_omp_variable_list (parser, clause_loc,
12642 OMP_CLAUSE_LINEAR, list);
12644 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12645 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12647 if (c_parser_next_token_is (parser, CPP_COLON))
12649 c_parser_consume_token (parser);
12650 location_t expr_loc = c_parser_peek_token (parser)->location;
12651 c_expr expr = c_parser_expression (parser);
12652 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12653 step = expr.value;
12654 step = c_fully_fold (step, false, NULL);
12655 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12657 sorry ("using parameters for %<linear%> step is not supported yet");
12658 step = integer_one_node;
12660 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12662 error_at (clause_loc, "%<linear%> clause step expression must "
12663 "be integral");
12664 step = integer_one_node;
12668 else
12669 step = integer_one_node;
12671 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12673 OMP_CLAUSE_LINEAR_STEP (c) = step;
12674 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12678 return nl;
12681 /* OpenMP 4.0:
12682 safelen ( constant-expression ) */
12684 static tree
12685 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12687 location_t clause_loc = c_parser_peek_token (parser)->location;
12688 tree c, t;
12690 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12691 return list;
12693 location_t expr_loc = c_parser_peek_token (parser)->location;
12694 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12695 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12696 t = expr.value;
12697 t = c_fully_fold (t, false, NULL);
12698 if (TREE_CODE (t) != INTEGER_CST
12699 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12700 || tree_int_cst_sgn (t) != 1)
12702 error_at (clause_loc, "%<safelen%> clause expression must "
12703 "be positive constant integer expression");
12704 t = NULL_TREE;
12707 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12708 if (t == NULL_TREE || t == error_mark_node)
12709 return list;
12711 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12713 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12714 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12715 OMP_CLAUSE_CHAIN (c) = list;
12716 return c;
12719 /* OpenMP 4.0:
12720 simdlen ( constant-expression ) */
12722 static tree
12723 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12725 location_t clause_loc = c_parser_peek_token (parser)->location;
12726 tree c, t;
12728 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12729 return list;
12731 location_t expr_loc = c_parser_peek_token (parser)->location;
12732 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12733 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12734 t = expr.value;
12735 t = c_fully_fold (t, false, NULL);
12736 if (TREE_CODE (t) != INTEGER_CST
12737 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12738 || tree_int_cst_sgn (t) != 1)
12740 error_at (clause_loc, "%<simdlen%> clause expression must "
12741 "be positive constant integer expression");
12742 t = NULL_TREE;
12745 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12746 if (t == NULL_TREE || t == error_mark_node)
12747 return list;
12749 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12751 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12752 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12753 OMP_CLAUSE_CHAIN (c) = list;
12754 return c;
12757 /* OpenMP 4.5:
12758 vec:
12759 identifier [+/- integer]
12760 vec , identifier [+/- integer]
12763 static tree
12764 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12765 tree list)
12767 tree vec = NULL;
12768 if (c_parser_next_token_is_not (parser, CPP_NAME)
12769 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12771 c_parser_error (parser, "expected identifier");
12772 return list;
12775 while (c_parser_next_token_is (parser, CPP_NAME)
12776 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12778 tree t = lookup_name (c_parser_peek_token (parser)->value);
12779 tree addend = NULL;
12781 if (t == NULL_TREE)
12783 undeclared_variable (c_parser_peek_token (parser)->location,
12784 c_parser_peek_token (parser)->value);
12785 t = error_mark_node;
12788 c_parser_consume_token (parser);
12790 bool neg = false;
12791 if (c_parser_next_token_is (parser, CPP_MINUS))
12792 neg = true;
12793 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12795 addend = integer_zero_node;
12796 neg = false;
12797 goto add_to_vector;
12799 c_parser_consume_token (parser);
12801 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12803 c_parser_error (parser, "expected integer");
12804 return list;
12807 addend = c_parser_peek_token (parser)->value;
12808 if (TREE_CODE (addend) != INTEGER_CST)
12810 c_parser_error (parser, "expected integer");
12811 return list;
12813 c_parser_consume_token (parser);
12815 add_to_vector:
12816 if (t != error_mark_node)
12818 vec = tree_cons (addend, t, vec);
12819 if (neg)
12820 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12823 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12824 break;
12826 c_parser_consume_token (parser);
12829 if (vec == NULL_TREE)
12830 return list;
12832 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12833 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12834 OMP_CLAUSE_DECL (u) = nreverse (vec);
12835 OMP_CLAUSE_CHAIN (u) = list;
12836 return u;
12839 /* OpenMP 4.0:
12840 depend ( depend-kind: variable-list )
12842 depend-kind:
12843 in | out | inout
12845 OpenMP 4.5:
12846 depend ( source )
12848 depend ( sink : vec ) */
12850 static tree
12851 c_parser_omp_clause_depend (c_parser *parser, tree list)
12853 location_t clause_loc = c_parser_peek_token (parser)->location;
12854 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12855 tree nl, c;
12857 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12858 return list;
12860 if (c_parser_next_token_is (parser, CPP_NAME))
12862 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12863 if (strcmp ("in", p) == 0)
12864 kind = OMP_CLAUSE_DEPEND_IN;
12865 else if (strcmp ("inout", p) == 0)
12866 kind = OMP_CLAUSE_DEPEND_INOUT;
12867 else if (strcmp ("out", p) == 0)
12868 kind = OMP_CLAUSE_DEPEND_OUT;
12869 else if (strcmp ("source", p) == 0)
12870 kind = OMP_CLAUSE_DEPEND_SOURCE;
12871 else if (strcmp ("sink", p) == 0)
12872 kind = OMP_CLAUSE_DEPEND_SINK;
12873 else
12874 goto invalid_kind;
12876 else
12877 goto invalid_kind;
12879 c_parser_consume_token (parser);
12881 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12883 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12884 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12885 OMP_CLAUSE_DECL (c) = NULL_TREE;
12886 OMP_CLAUSE_CHAIN (c) = list;
12887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12888 return c;
12891 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12892 goto resync_fail;
12894 if (kind == OMP_CLAUSE_DEPEND_SINK)
12895 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12896 else
12898 nl = c_parser_omp_variable_list (parser, clause_loc,
12899 OMP_CLAUSE_DEPEND, list);
12901 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12902 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12906 return nl;
12908 invalid_kind:
12909 c_parser_error (parser, "invalid depend kind");
12910 resync_fail:
12911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12912 return list;
12915 /* OpenMP 4.0:
12916 map ( map-kind: variable-list )
12917 map ( variable-list )
12919 map-kind:
12920 alloc | to | from | tofrom
12922 OpenMP 4.5:
12923 map-kind:
12924 alloc | to | from | tofrom | release | delete
12926 map ( always [,] map-kind: variable-list ) */
12928 static tree
12929 c_parser_omp_clause_map (c_parser *parser, tree list)
12931 location_t clause_loc = c_parser_peek_token (parser)->location;
12932 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12933 int always = 0;
12934 enum c_id_kind always_id_kind = C_ID_NONE;
12935 location_t always_loc = UNKNOWN_LOCATION;
12936 tree always_id = NULL_TREE;
12937 tree nl, c;
12939 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12940 return list;
12942 if (c_parser_next_token_is (parser, CPP_NAME))
12944 c_token *tok = c_parser_peek_token (parser);
12945 const char *p = IDENTIFIER_POINTER (tok->value);
12946 always_id_kind = tok->id_kind;
12947 always_loc = tok->location;
12948 always_id = tok->value;
12949 if (strcmp ("always", p) == 0)
12951 c_token *sectok = c_parser_peek_2nd_token (parser);
12952 if (sectok->type == CPP_COMMA)
12954 c_parser_consume_token (parser);
12955 c_parser_consume_token (parser);
12956 always = 2;
12958 else if (sectok->type == CPP_NAME)
12960 p = IDENTIFIER_POINTER (sectok->value);
12961 if (strcmp ("alloc", p) == 0
12962 || strcmp ("to", p) == 0
12963 || strcmp ("from", p) == 0
12964 || strcmp ("tofrom", p) == 0
12965 || strcmp ("release", p) == 0
12966 || strcmp ("delete", p) == 0)
12968 c_parser_consume_token (parser);
12969 always = 1;
12975 if (c_parser_next_token_is (parser, CPP_NAME)
12976 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12978 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12979 if (strcmp ("alloc", p) == 0)
12980 kind = GOMP_MAP_ALLOC;
12981 else if (strcmp ("to", p) == 0)
12982 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12983 else if (strcmp ("from", p) == 0)
12984 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12985 else if (strcmp ("tofrom", p) == 0)
12986 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12987 else if (strcmp ("release", p) == 0)
12988 kind = GOMP_MAP_RELEASE;
12989 else if (strcmp ("delete", p) == 0)
12990 kind = GOMP_MAP_DELETE;
12991 else
12993 c_parser_error (parser, "invalid map kind");
12994 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12995 "expected %<)%>");
12996 return list;
12998 c_parser_consume_token (parser);
12999 c_parser_consume_token (parser);
13001 else if (always)
13003 if (always_id_kind != C_ID_ID)
13005 c_parser_error (parser, "expected identifier");
13006 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13007 return list;
13010 tree t = lookup_name (always_id);
13011 if (t == NULL_TREE)
13013 undeclared_variable (always_loc, always_id);
13014 t = error_mark_node;
13016 if (t != error_mark_node)
13018 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13019 OMP_CLAUSE_DECL (u) = t;
13020 OMP_CLAUSE_CHAIN (u) = list;
13021 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13022 list = u;
13024 if (always == 1)
13026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13027 return list;
13031 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13033 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13034 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13036 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13037 return nl;
13040 /* OpenMP 4.0:
13041 device ( expression ) */
13043 static tree
13044 c_parser_omp_clause_device (c_parser *parser, tree list)
13046 location_t clause_loc = c_parser_peek_token (parser)->location;
13047 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13049 location_t expr_loc = c_parser_peek_token (parser)->location;
13050 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13051 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13052 tree c, t = expr.value;
13053 t = c_fully_fold (t, false, NULL);
13055 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13057 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13059 c_parser_error (parser, "expected integer expression");
13060 return list;
13063 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13065 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13066 OMP_CLAUSE_DEVICE_ID (c) = t;
13067 OMP_CLAUSE_CHAIN (c) = list;
13068 list = c;
13071 return list;
13074 /* OpenMP 4.0:
13075 dist_schedule ( static )
13076 dist_schedule ( static , expression ) */
13078 static tree
13079 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13081 tree c, t = NULL_TREE;
13082 location_t loc = c_parser_peek_token (parser)->location;
13084 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13085 return list;
13087 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13089 c_parser_error (parser, "invalid dist_schedule kind");
13090 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13091 "expected %<)%>");
13092 return list;
13095 c_parser_consume_token (parser);
13096 if (c_parser_next_token_is (parser, CPP_COMMA))
13098 c_parser_consume_token (parser);
13100 location_t expr_loc = c_parser_peek_token (parser)->location;
13101 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13102 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13103 t = expr.value;
13104 t = c_fully_fold (t, false, NULL);
13105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13107 else
13108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13109 "expected %<,%> or %<)%>");
13111 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13112 if (t == error_mark_node)
13113 return list;
13115 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13116 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13117 OMP_CLAUSE_CHAIN (c) = list;
13118 return c;
13121 /* OpenMP 4.0:
13122 proc_bind ( proc-bind-kind )
13124 proc-bind-kind:
13125 master | close | spread */
13127 static tree
13128 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13130 location_t clause_loc = c_parser_peek_token (parser)->location;
13131 enum omp_clause_proc_bind_kind kind;
13132 tree c;
13134 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13135 return list;
13137 if (c_parser_next_token_is (parser, CPP_NAME))
13139 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13140 if (strcmp ("master", p) == 0)
13141 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13142 else if (strcmp ("close", p) == 0)
13143 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13144 else if (strcmp ("spread", p) == 0)
13145 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13146 else
13147 goto invalid_kind;
13149 else
13150 goto invalid_kind;
13152 c_parser_consume_token (parser);
13153 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13154 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13155 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13156 OMP_CLAUSE_CHAIN (c) = list;
13157 return c;
13159 invalid_kind:
13160 c_parser_error (parser, "invalid proc_bind kind");
13161 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13162 return list;
13165 /* OpenMP 4.0:
13166 to ( variable-list ) */
13168 static tree
13169 c_parser_omp_clause_to (c_parser *parser, tree list)
13171 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13174 /* OpenMP 4.0:
13175 from ( variable-list ) */
13177 static tree
13178 c_parser_omp_clause_from (c_parser *parser, tree list)
13180 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13183 /* OpenMP 4.0:
13184 uniform ( variable-list ) */
13186 static tree
13187 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13189 /* The clauses location. */
13190 location_t loc = c_parser_peek_token (parser)->location;
13192 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13194 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13195 list);
13196 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13198 return list;
13201 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13202 is a bitmask in MASK. Return the list of clauses found. */
13204 static tree
13205 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13206 const char *where, bool finish_p = true)
13208 tree clauses = NULL;
13209 bool first = true;
13211 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13213 location_t here;
13214 pragma_omp_clause c_kind;
13215 const char *c_name;
13216 tree prev = clauses;
13218 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13219 c_parser_consume_token (parser);
13221 here = c_parser_peek_token (parser)->location;
13222 c_kind = c_parser_omp_clause_name (parser);
13224 switch (c_kind)
13226 case PRAGMA_OACC_CLAUSE_ASYNC:
13227 clauses = c_parser_oacc_clause_async (parser, clauses);
13228 c_name = "async";
13229 break;
13230 case PRAGMA_OACC_CLAUSE_AUTO:
13231 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13232 clauses);
13233 c_name = "auto";
13234 break;
13235 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13236 clauses = c_parser_omp_clause_collapse (parser, clauses);
13237 c_name = "collapse";
13238 break;
13239 case PRAGMA_OACC_CLAUSE_COPY:
13240 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13241 c_name = "copy";
13242 break;
13243 case PRAGMA_OACC_CLAUSE_COPYIN:
13244 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13245 c_name = "copyin";
13246 break;
13247 case PRAGMA_OACC_CLAUSE_COPYOUT:
13248 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13249 c_name = "copyout";
13250 break;
13251 case PRAGMA_OACC_CLAUSE_CREATE:
13252 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13253 c_name = "create";
13254 break;
13255 case PRAGMA_OACC_CLAUSE_DELETE:
13256 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13257 c_name = "delete";
13258 break;
13259 case PRAGMA_OMP_CLAUSE_DEFAULT:
13260 clauses = c_parser_omp_clause_default (parser, clauses, true);
13261 c_name = "default";
13262 break;
13263 case PRAGMA_OACC_CLAUSE_DEVICE:
13264 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13265 c_name = "device";
13266 break;
13267 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13268 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13269 c_name = "deviceptr";
13270 break;
13271 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13272 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13273 c_name = "device_resident";
13274 break;
13275 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13276 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13277 c_name = "firstprivate";
13278 break;
13279 case PRAGMA_OACC_CLAUSE_GANG:
13280 c_name = "gang";
13281 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13282 c_name, clauses);
13283 break;
13284 case PRAGMA_OACC_CLAUSE_HOST:
13285 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13286 c_name = "host";
13287 break;
13288 case PRAGMA_OACC_CLAUSE_IF:
13289 clauses = c_parser_omp_clause_if (parser, clauses, false);
13290 c_name = "if";
13291 break;
13292 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13293 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13294 clauses);
13295 c_name = "independent";
13296 break;
13297 case PRAGMA_OACC_CLAUSE_LINK:
13298 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13299 c_name = "link";
13300 break;
13301 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13302 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13303 c_name = "num_gangs";
13304 break;
13305 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13306 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13307 c_name = "num_workers";
13308 break;
13309 case PRAGMA_OACC_CLAUSE_PRESENT:
13310 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13311 c_name = "present";
13312 break;
13313 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13314 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13315 c_name = "present_or_copy";
13316 break;
13317 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13318 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13319 c_name = "present_or_copyin";
13320 break;
13321 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13322 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13323 c_name = "present_or_copyout";
13324 break;
13325 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13326 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13327 c_name = "present_or_create";
13328 break;
13329 case PRAGMA_OACC_CLAUSE_PRIVATE:
13330 clauses = c_parser_omp_clause_private (parser, clauses);
13331 c_name = "private";
13332 break;
13333 case PRAGMA_OACC_CLAUSE_REDUCTION:
13334 clauses = c_parser_omp_clause_reduction (parser, clauses);
13335 c_name = "reduction";
13336 break;
13337 case PRAGMA_OACC_CLAUSE_SELF:
13338 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13339 c_name = "self";
13340 break;
13341 case PRAGMA_OACC_CLAUSE_SEQ:
13342 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13343 clauses);
13344 c_name = "seq";
13345 break;
13346 case PRAGMA_OACC_CLAUSE_TILE:
13347 clauses = c_parser_oacc_clause_tile (parser, clauses);
13348 c_name = "tile";
13349 break;
13350 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13351 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13352 c_name = "use_device";
13353 break;
13354 case PRAGMA_OACC_CLAUSE_VECTOR:
13355 c_name = "vector";
13356 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13357 c_name, clauses);
13358 break;
13359 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13360 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13361 c_name = "vector_length";
13362 break;
13363 case PRAGMA_OACC_CLAUSE_WAIT:
13364 clauses = c_parser_oacc_clause_wait (parser, clauses);
13365 c_name = "wait";
13366 break;
13367 case PRAGMA_OACC_CLAUSE_WORKER:
13368 c_name = "worker";
13369 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13370 c_name, clauses);
13371 break;
13372 default:
13373 c_parser_error (parser, "expected %<#pragma acc%> clause");
13374 goto saw_error;
13377 first = false;
13379 if (((mask >> c_kind) & 1) == 0)
13381 /* Remove the invalid clause(s) from the list to avoid
13382 confusing the rest of the compiler. */
13383 clauses = prev;
13384 error_at (here, "%qs is not valid for %qs", c_name, where);
13388 saw_error:
13389 c_parser_skip_to_pragma_eol (parser);
13391 if (finish_p)
13392 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13394 return clauses;
13397 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13398 is a bitmask in MASK. Return the list of clauses found. */
13400 static tree
13401 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13402 const char *where, bool finish_p = true)
13404 tree clauses = NULL;
13405 bool first = true, cilk_simd_fn = false;
13407 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13409 location_t here;
13410 pragma_omp_clause c_kind;
13411 const char *c_name;
13412 tree prev = clauses;
13414 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13415 c_parser_consume_token (parser);
13417 here = c_parser_peek_token (parser)->location;
13418 c_kind = c_parser_omp_clause_name (parser);
13420 switch (c_kind)
13422 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13423 clauses = c_parser_omp_clause_collapse (parser, clauses);
13424 c_name = "collapse";
13425 break;
13426 case PRAGMA_OMP_CLAUSE_COPYIN:
13427 clauses = c_parser_omp_clause_copyin (parser, clauses);
13428 c_name = "copyin";
13429 break;
13430 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13431 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13432 c_name = "copyprivate";
13433 break;
13434 case PRAGMA_OMP_CLAUSE_DEFAULT:
13435 clauses = c_parser_omp_clause_default (parser, clauses, false);
13436 c_name = "default";
13437 break;
13438 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13439 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13440 c_name = "firstprivate";
13441 break;
13442 case PRAGMA_OMP_CLAUSE_FINAL:
13443 clauses = c_parser_omp_clause_final (parser, clauses);
13444 c_name = "final";
13445 break;
13446 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13447 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13448 c_name = "grainsize";
13449 break;
13450 case PRAGMA_OMP_CLAUSE_HINT:
13451 clauses = c_parser_omp_clause_hint (parser, clauses);
13452 c_name = "hint";
13453 break;
13454 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13455 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13456 c_name = "defaultmap";
13457 break;
13458 case PRAGMA_OMP_CLAUSE_IF:
13459 clauses = c_parser_omp_clause_if (parser, clauses, true);
13460 c_name = "if";
13461 break;
13462 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13463 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13464 c_name = "lastprivate";
13465 break;
13466 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13467 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13468 c_name = "mergeable";
13469 break;
13470 case PRAGMA_OMP_CLAUSE_NOWAIT:
13471 clauses = c_parser_omp_clause_nowait (parser, clauses);
13472 c_name = "nowait";
13473 break;
13474 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13475 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13476 c_name = "num_tasks";
13477 break;
13478 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13479 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13480 c_name = "num_threads";
13481 break;
13482 case PRAGMA_OMP_CLAUSE_ORDERED:
13483 clauses = c_parser_omp_clause_ordered (parser, clauses);
13484 c_name = "ordered";
13485 break;
13486 case PRAGMA_OMP_CLAUSE_PRIORITY:
13487 clauses = c_parser_omp_clause_priority (parser, clauses);
13488 c_name = "priority";
13489 break;
13490 case PRAGMA_OMP_CLAUSE_PRIVATE:
13491 clauses = c_parser_omp_clause_private (parser, clauses);
13492 c_name = "private";
13493 break;
13494 case PRAGMA_OMP_CLAUSE_REDUCTION:
13495 clauses = c_parser_omp_clause_reduction (parser, clauses);
13496 c_name = "reduction";
13497 break;
13498 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13499 clauses = c_parser_omp_clause_schedule (parser, clauses);
13500 c_name = "schedule";
13501 break;
13502 case PRAGMA_OMP_CLAUSE_SHARED:
13503 clauses = c_parser_omp_clause_shared (parser, clauses);
13504 c_name = "shared";
13505 break;
13506 case PRAGMA_OMP_CLAUSE_UNTIED:
13507 clauses = c_parser_omp_clause_untied (parser, clauses);
13508 c_name = "untied";
13509 break;
13510 case PRAGMA_OMP_CLAUSE_INBRANCH:
13511 case PRAGMA_CILK_CLAUSE_MASK:
13512 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13513 clauses);
13514 c_name = "inbranch";
13515 break;
13516 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13517 case PRAGMA_CILK_CLAUSE_NOMASK:
13518 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13519 clauses);
13520 c_name = "notinbranch";
13521 break;
13522 case PRAGMA_OMP_CLAUSE_PARALLEL:
13523 clauses
13524 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13525 clauses);
13526 c_name = "parallel";
13527 if (!first)
13529 clause_not_first:
13530 error_at (here, "%qs must be the first clause of %qs",
13531 c_name, where);
13532 clauses = prev;
13534 break;
13535 case PRAGMA_OMP_CLAUSE_FOR:
13536 clauses
13537 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13538 clauses);
13539 c_name = "for";
13540 if (!first)
13541 goto clause_not_first;
13542 break;
13543 case PRAGMA_OMP_CLAUSE_SECTIONS:
13544 clauses
13545 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13546 clauses);
13547 c_name = "sections";
13548 if (!first)
13549 goto clause_not_first;
13550 break;
13551 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13552 clauses
13553 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13554 clauses);
13555 c_name = "taskgroup";
13556 if (!first)
13557 goto clause_not_first;
13558 break;
13559 case PRAGMA_OMP_CLAUSE_LINK:
13560 clauses
13561 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13562 c_name = "link";
13563 break;
13564 case PRAGMA_OMP_CLAUSE_TO:
13565 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13566 clauses
13567 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13568 clauses);
13569 else
13570 clauses = c_parser_omp_clause_to (parser, clauses);
13571 c_name = "to";
13572 break;
13573 case PRAGMA_OMP_CLAUSE_FROM:
13574 clauses = c_parser_omp_clause_from (parser, clauses);
13575 c_name = "from";
13576 break;
13577 case PRAGMA_OMP_CLAUSE_UNIFORM:
13578 clauses = c_parser_omp_clause_uniform (parser, clauses);
13579 c_name = "uniform";
13580 break;
13581 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13582 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13583 c_name = "num_teams";
13584 break;
13585 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13586 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13587 c_name = "thread_limit";
13588 break;
13589 case PRAGMA_OMP_CLAUSE_ALIGNED:
13590 clauses = c_parser_omp_clause_aligned (parser, clauses);
13591 c_name = "aligned";
13592 break;
13593 case PRAGMA_OMP_CLAUSE_LINEAR:
13594 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13595 cilk_simd_fn = true;
13596 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13597 c_name = "linear";
13598 break;
13599 case PRAGMA_OMP_CLAUSE_DEPEND:
13600 clauses = c_parser_omp_clause_depend (parser, clauses);
13601 c_name = "depend";
13602 break;
13603 case PRAGMA_OMP_CLAUSE_MAP:
13604 clauses = c_parser_omp_clause_map (parser, clauses);
13605 c_name = "map";
13606 break;
13607 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13608 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13609 c_name = "use_device_ptr";
13610 break;
13611 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13612 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13613 c_name = "is_device_ptr";
13614 break;
13615 case PRAGMA_OMP_CLAUSE_DEVICE:
13616 clauses = c_parser_omp_clause_device (parser, clauses);
13617 c_name = "device";
13618 break;
13619 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13620 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13621 c_name = "dist_schedule";
13622 break;
13623 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13624 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13625 c_name = "proc_bind";
13626 break;
13627 case PRAGMA_OMP_CLAUSE_SAFELEN:
13628 clauses = c_parser_omp_clause_safelen (parser, clauses);
13629 c_name = "safelen";
13630 break;
13631 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13632 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13633 c_name = "simdlen";
13634 break;
13635 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13636 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13637 c_name = "simdlen";
13638 break;
13639 case PRAGMA_OMP_CLAUSE_NOGROUP:
13640 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13641 c_name = "nogroup";
13642 break;
13643 case PRAGMA_OMP_CLAUSE_THREADS:
13644 clauses
13645 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13646 clauses);
13647 c_name = "threads";
13648 break;
13649 case PRAGMA_OMP_CLAUSE_SIMD:
13650 clauses
13651 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13652 clauses);
13653 c_name = "simd";
13654 break;
13655 default:
13656 c_parser_error (parser, "expected %<#pragma omp%> clause");
13657 goto saw_error;
13660 first = false;
13662 if (((mask >> c_kind) & 1) == 0)
13664 /* Remove the invalid clause(s) from the list to avoid
13665 confusing the rest of the compiler. */
13666 clauses = prev;
13667 error_at (here, "%qs is not valid for %qs", c_name, where);
13671 saw_error:
13672 c_parser_skip_to_pragma_eol (parser);
13674 if (finish_p)
13676 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13677 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
13678 return c_finish_omp_clauses (clauses, C_ORT_OMP);
13681 return clauses;
13684 /* OpenACC 2.0, OpenMP 2.5:
13685 structured-block:
13686 statement
13688 In practice, we're also interested in adding the statement to an
13689 outer node. So it is convenient if we work around the fact that
13690 c_parser_statement calls add_stmt. */
13692 static tree
13693 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13695 tree stmt = push_stmt_list ();
13696 c_parser_statement (parser, if_p);
13697 return pop_stmt_list (stmt);
13700 /* OpenACC 2.0:
13701 # pragma acc cache (variable-list) new-line
13703 LOC is the location of the #pragma token.
13706 static tree
13707 c_parser_oacc_cache (location_t loc, c_parser *parser)
13709 tree stmt, clauses;
13711 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13712 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13714 c_parser_skip_to_pragma_eol (parser);
13716 stmt = make_node (OACC_CACHE);
13717 TREE_TYPE (stmt) = void_type_node;
13718 OACC_CACHE_CLAUSES (stmt) = clauses;
13719 SET_EXPR_LOCATION (stmt, loc);
13720 add_stmt (stmt);
13722 return stmt;
13725 /* OpenACC 2.0:
13726 # pragma acc data oacc-data-clause[optseq] new-line
13727 structured-block
13729 LOC is the location of the #pragma token.
13732 #define OACC_DATA_CLAUSE_MASK \
13733 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13745 static tree
13746 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13748 tree stmt, clauses, block;
13750 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13751 "#pragma acc data");
13753 block = c_begin_omp_parallel ();
13754 add_stmt (c_parser_omp_structured_block (parser, if_p));
13756 stmt = c_finish_oacc_data (loc, clauses, block);
13758 return stmt;
13761 /* OpenACC 2.0:
13762 # pragma acc declare oacc-data-clause[optseq] new-line
13765 #define OACC_DECLARE_CLAUSE_MASK \
13766 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13779 static void
13780 c_parser_oacc_declare (c_parser *parser)
13782 location_t pragma_loc = c_parser_peek_token (parser)->location;
13783 tree clauses, stmt, t, decl;
13785 bool error = false;
13787 c_parser_consume_pragma (parser);
13789 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13790 "#pragma acc declare");
13791 if (!clauses)
13793 error_at (pragma_loc,
13794 "no valid clauses specified in %<#pragma acc declare%>");
13795 return;
13798 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13800 location_t loc = OMP_CLAUSE_LOCATION (t);
13801 decl = OMP_CLAUSE_DECL (t);
13802 if (!DECL_P (decl))
13804 error_at (loc, "array section in %<#pragma acc declare%>");
13805 error = true;
13806 continue;
13809 switch (OMP_CLAUSE_MAP_KIND (t))
13811 case GOMP_MAP_FIRSTPRIVATE_POINTER:
13812 case GOMP_MAP_FORCE_ALLOC:
13813 case GOMP_MAP_FORCE_TO:
13814 case GOMP_MAP_FORCE_DEVICEPTR:
13815 case GOMP_MAP_DEVICE_RESIDENT:
13816 break;
13818 case GOMP_MAP_LINK:
13819 if (!global_bindings_p ()
13820 && (TREE_STATIC (decl)
13821 || !DECL_EXTERNAL (decl)))
13823 error_at (loc,
13824 "%qD must be a global variable in"
13825 "%<#pragma acc declare link%>",
13826 decl);
13827 error = true;
13828 continue;
13830 break;
13832 default:
13833 if (global_bindings_p ())
13835 error_at (loc, "invalid OpenACC clause at file scope");
13836 error = true;
13837 continue;
13839 if (DECL_EXTERNAL (decl))
13841 error_at (loc,
13842 "invalid use of %<extern%> variable %qD "
13843 "in %<#pragma acc declare%>", decl);
13844 error = true;
13845 continue;
13847 else if (TREE_PUBLIC (decl))
13849 error_at (loc,
13850 "invalid use of %<global%> variable %qD "
13851 "in %<#pragma acc declare%>", decl);
13852 error = true;
13853 continue;
13855 break;
13858 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13859 || lookup_attribute ("omp declare target link",
13860 DECL_ATTRIBUTES (decl)))
13862 error_at (loc, "variable %qD used more than once with "
13863 "%<#pragma acc declare%>", decl);
13864 error = true;
13865 continue;
13868 if (!error)
13870 tree id;
13872 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13873 id = get_identifier ("omp declare target link");
13874 else
13875 id = get_identifier ("omp declare target");
13877 DECL_ATTRIBUTES (decl)
13878 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13880 if (global_bindings_p ())
13882 symtab_node *node = symtab_node::get (decl);
13883 if (node != NULL)
13885 node->offloadable = 1;
13886 if (ENABLE_OFFLOADING)
13888 g->have_offload = true;
13889 if (is_a <varpool_node *> (node))
13890 vec_safe_push (offload_vars, decl);
13897 if (error || global_bindings_p ())
13898 return;
13900 stmt = make_node (OACC_DECLARE);
13901 TREE_TYPE (stmt) = void_type_node;
13902 OACC_DECLARE_CLAUSES (stmt) = clauses;
13903 SET_EXPR_LOCATION (stmt, pragma_loc);
13905 add_stmt (stmt);
13907 return;
13910 /* OpenACC 2.0:
13911 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13915 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13918 LOC is the location of the #pragma token.
13921 #define OACC_ENTER_DATA_CLAUSE_MASK \
13922 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13930 #define OACC_EXIT_DATA_CLAUSE_MASK \
13931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13937 static void
13938 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13940 location_t loc = c_parser_peek_token (parser)->location;
13941 tree clauses, stmt;
13942 const char *p = "";
13944 c_parser_consume_pragma (parser);
13946 if (c_parser_next_token_is (parser, CPP_NAME))
13948 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13949 c_parser_consume_token (parser);
13952 if (strcmp (p, "data") != 0)
13954 error_at (loc, enter
13955 ? "expected %<data%> after %<#pragma acc enter%>"
13956 : "expected %<data%> after %<#pragma acc exit%>");
13957 parser->error = true;
13958 c_parser_skip_to_pragma_eol (parser);
13959 return;
13962 if (enter)
13963 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13964 "#pragma acc enter data");
13965 else
13966 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13967 "#pragma acc exit data");
13969 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13971 error_at (loc, enter
13972 ? "%<#pragma acc enter data%> has no data movement clause"
13973 : "%<#pragma acc exit data%> has no data movement clause");
13974 return;
13977 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13978 TREE_TYPE (stmt) = void_type_node;
13979 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13980 SET_EXPR_LOCATION (stmt, loc);
13981 add_stmt (stmt);
13985 /* OpenACC 2.0:
13986 # pragma acc host_data oacc-data-clause[optseq] new-line
13987 structured-block
13990 #define OACC_HOST_DATA_CLAUSE_MASK \
13991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13993 static tree
13994 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
13996 tree stmt, clauses, block;
13998 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13999 "#pragma acc host_data");
14001 block = c_begin_omp_parallel ();
14002 add_stmt (c_parser_omp_structured_block (parser, if_p));
14003 stmt = c_finish_oacc_host_data (loc, clauses, block);
14004 return stmt;
14008 /* OpenACC 2.0:
14010 # pragma acc loop oacc-loop-clause[optseq] new-line
14011 structured-block
14013 LOC is the location of the #pragma token.
14016 #define OACC_LOOP_CLAUSE_MASK \
14017 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14027 static tree
14028 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14029 omp_clause_mask mask, tree *cclauses, bool *if_p)
14031 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14033 strcat (p_name, " loop");
14034 mask |= OACC_LOOP_CLAUSE_MASK;
14036 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14037 cclauses == NULL);
14038 if (cclauses)
14040 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14041 if (*cclauses)
14042 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14043 if (clauses)
14044 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14047 tree block = c_begin_compound_stmt (true);
14048 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14049 if_p);
14050 block = c_end_compound_stmt (loc, block, true);
14051 add_stmt (block);
14053 return stmt;
14056 /* OpenACC 2.0:
14057 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14058 structured-block
14062 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14063 structured-block
14065 LOC is the location of the #pragma token.
14068 #define OACC_KERNELS_CLAUSE_MASK \
14069 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14084 #define OACC_PARALLEL_CLAUSE_MASK \
14085 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14106 static tree
14107 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14108 enum pragma_kind p_kind, char *p_name,
14109 bool *if_p)
14111 omp_clause_mask mask;
14112 enum tree_code code;
14113 switch (p_kind)
14115 case PRAGMA_OACC_KERNELS:
14116 strcat (p_name, " kernels");
14117 mask = OACC_KERNELS_CLAUSE_MASK;
14118 code = OACC_KERNELS;
14119 break;
14120 case PRAGMA_OACC_PARALLEL:
14121 strcat (p_name, " parallel");
14122 mask = OACC_PARALLEL_CLAUSE_MASK;
14123 code = OACC_PARALLEL;
14124 break;
14125 default:
14126 gcc_unreachable ();
14129 if (c_parser_next_token_is (parser, CPP_NAME))
14131 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14132 if (strcmp (p, "loop") == 0)
14134 c_parser_consume_token (parser);
14135 tree block = c_begin_omp_parallel ();
14136 tree clauses;
14137 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14138 return c_finish_omp_construct (loc, code, block, clauses);
14142 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14144 tree block = c_begin_omp_parallel ();
14145 add_stmt (c_parser_omp_structured_block (parser, if_p));
14147 return c_finish_omp_construct (loc, code, block, clauses);
14150 /* OpenACC 2.0:
14151 # pragma acc routine oacc-routine-clause[optseq] new-line
14152 function-definition
14154 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14157 #define OACC_ROUTINE_CLAUSE_MASK \
14158 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14163 /* Parse an OpenACC routine directive. For named directives, we apply
14164 immediately to the named function. For unnamed ones we then parse
14165 a declaration or definition, which must be for a function. */
14167 static void
14168 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14170 gcc_checking_assert (context == pragma_external);
14172 oacc_routine_data data;
14173 data.error_seen = false;
14174 data.fndecl_seen = false;
14175 data.clauses = NULL_TREE;
14176 data.loc = c_parser_peek_token (parser)->location;
14178 c_parser_consume_pragma (parser);
14180 /* Look for optional '( name )'. */
14181 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14183 c_parser_consume_token (parser); /* '(' */
14185 tree decl = NULL_TREE;
14186 c_token *name_token = c_parser_peek_token (parser);
14187 location_t name_loc = name_token->location;
14188 if (name_token->type == CPP_NAME
14189 && (name_token->id_kind == C_ID_ID
14190 || name_token->id_kind == C_ID_TYPENAME))
14192 decl = lookup_name (name_token->value);
14193 if (!decl)
14194 error_at (name_loc,
14195 "%qE has not been declared", name_token->value);
14196 c_parser_consume_token (parser);
14198 else
14199 c_parser_error (parser, "expected function name");
14201 if (!decl
14202 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14204 c_parser_skip_to_pragma_eol (parser, false);
14205 return;
14208 data.clauses
14209 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14210 "#pragma acc routine");
14212 if (TREE_CODE (decl) != FUNCTION_DECL)
14214 error_at (name_loc, "%qD does not refer to a function", decl);
14215 return;
14218 c_finish_oacc_routine (&data, decl, false);
14220 else /* No optional '( name )'. */
14222 data.clauses
14223 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14224 "#pragma acc routine");
14226 /* Emit a helpful diagnostic if there's another pragma following this
14227 one. Also don't allow a static assertion declaration, as in the
14228 following we'll just parse a *single* "declaration or function
14229 definition", and the static assertion counts an one. */
14230 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14231 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14233 error_at (data.loc,
14234 "%<#pragma acc routine%> not immediately followed by"
14235 " function declaration or definition");
14236 /* ..., and then just keep going. */
14237 return;
14240 /* We only have to consider the pragma_external case here. */
14241 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14242 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14244 int ext = disable_extension_diagnostics ();
14246 c_parser_consume_token (parser);
14247 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14248 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14249 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14250 NULL, vNULL, &data);
14251 restore_extension_diagnostics (ext);
14253 else
14254 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14255 NULL, vNULL, &data);
14259 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14260 IS_DEFN is true if we're applying it to the definition. */
14262 static void
14263 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14264 bool is_defn)
14266 /* Keep going if we're in error reporting mode. */
14267 if (data->error_seen
14268 || fndecl == error_mark_node)
14269 return;
14271 if (data->fndecl_seen)
14273 error_at (data->loc,
14274 "%<#pragma acc routine%> not immediately followed by"
14275 " a single function declaration or definition");
14276 data->error_seen = true;
14277 return;
14279 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14281 error_at (data->loc,
14282 "%<#pragma acc routine%> not immediately followed by"
14283 " function declaration or definition");
14284 data->error_seen = true;
14285 return;
14288 if (oacc_get_fn_attrib (fndecl))
14290 error_at (data->loc,
14291 "%<#pragma acc routine%> already applied to %qD", fndecl);
14292 data->error_seen = true;
14293 return;
14296 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14298 error_at (data->loc,
14299 "%<#pragma acc routine%> must be applied before %s",
14300 TREE_USED (fndecl) ? "use" : "definition");
14301 data->error_seen = true;
14302 return;
14305 /* Process the routine's dimension clauses. */
14306 tree dims = oacc_build_routine_dims (data->clauses);
14307 oacc_replace_fn_attrib (fndecl, dims);
14309 /* Add an "omp declare target" attribute. */
14310 DECL_ATTRIBUTES (fndecl)
14311 = tree_cons (get_identifier ("omp declare target"),
14312 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14314 /* Remember that we've used this "#pragma acc routine". */
14315 data->fndecl_seen = true;
14318 /* OpenACC 2.0:
14319 # pragma acc update oacc-update-clause[optseq] new-line
14322 #define OACC_UPDATE_CLAUSE_MASK \
14323 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14330 static void
14331 c_parser_oacc_update (c_parser *parser)
14333 location_t loc = c_parser_peek_token (parser)->location;
14335 c_parser_consume_pragma (parser);
14337 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14338 "#pragma acc update");
14339 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14341 error_at (loc,
14342 "%<#pragma acc update%> must contain at least one "
14343 "%<device%> or %<host%> or %<self%> clause");
14344 return;
14347 if (parser->error)
14348 return;
14350 tree stmt = make_node (OACC_UPDATE);
14351 TREE_TYPE (stmt) = void_type_node;
14352 OACC_UPDATE_CLAUSES (stmt) = clauses;
14353 SET_EXPR_LOCATION (stmt, loc);
14354 add_stmt (stmt);
14357 /* OpenACC 2.0:
14358 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14360 LOC is the location of the #pragma token.
14363 #define OACC_WAIT_CLAUSE_MASK \
14364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14366 static tree
14367 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14369 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14371 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14372 list = c_parser_oacc_wait_list (parser, loc, list);
14374 strcpy (p_name, " wait");
14375 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14376 stmt = c_finish_oacc_wait (loc, list, clauses);
14377 add_stmt (stmt);
14379 return stmt;
14382 /* OpenMP 2.5:
14383 # pragma omp atomic new-line
14384 expression-stmt
14386 expression-stmt:
14387 x binop= expr | x++ | ++x | x-- | --x
14388 binop:
14389 +, *, -, /, &, ^, |, <<, >>
14391 where x is an lvalue expression with scalar type.
14393 OpenMP 3.1:
14394 # pragma omp atomic new-line
14395 update-stmt
14397 # pragma omp atomic read new-line
14398 read-stmt
14400 # pragma omp atomic write new-line
14401 write-stmt
14403 # pragma omp atomic update new-line
14404 update-stmt
14406 # pragma omp atomic capture new-line
14407 capture-stmt
14409 # pragma omp atomic capture new-line
14410 capture-block
14412 read-stmt:
14413 v = x
14414 write-stmt:
14415 x = expr
14416 update-stmt:
14417 expression-stmt | x = x binop expr
14418 capture-stmt:
14419 v = expression-stmt
14420 capture-block:
14421 { v = x; update-stmt; } | { update-stmt; v = x; }
14423 OpenMP 4.0:
14424 update-stmt:
14425 expression-stmt | x = x binop expr | x = expr binop x
14426 capture-stmt:
14427 v = update-stmt
14428 capture-block:
14429 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14431 where x and v are lvalue expressions with scalar type.
14433 LOC is the location of the #pragma token. */
14435 static void
14436 c_parser_omp_atomic (location_t loc, c_parser *parser)
14438 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14439 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14440 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14441 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14442 struct c_expr expr;
14443 location_t eloc;
14444 bool structured_block = false;
14445 bool swapped = false;
14446 bool seq_cst = false;
14447 bool non_lvalue_p;
14449 if (c_parser_next_token_is (parser, CPP_NAME))
14451 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14452 if (!strcmp (p, "seq_cst"))
14454 seq_cst = true;
14455 c_parser_consume_token (parser);
14456 if (c_parser_next_token_is (parser, CPP_COMMA)
14457 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14458 c_parser_consume_token (parser);
14461 if (c_parser_next_token_is (parser, CPP_NAME))
14463 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14465 if (!strcmp (p, "read"))
14466 code = OMP_ATOMIC_READ;
14467 else if (!strcmp (p, "write"))
14468 code = NOP_EXPR;
14469 else if (!strcmp (p, "update"))
14470 code = OMP_ATOMIC;
14471 else if (!strcmp (p, "capture"))
14472 code = OMP_ATOMIC_CAPTURE_NEW;
14473 else
14474 p = NULL;
14475 if (p)
14476 c_parser_consume_token (parser);
14478 if (!seq_cst)
14480 if (c_parser_next_token_is (parser, CPP_COMMA)
14481 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14482 c_parser_consume_token (parser);
14484 if (c_parser_next_token_is (parser, CPP_NAME))
14486 const char *p
14487 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14488 if (!strcmp (p, "seq_cst"))
14490 seq_cst = true;
14491 c_parser_consume_token (parser);
14495 c_parser_skip_to_pragma_eol (parser);
14497 switch (code)
14499 case OMP_ATOMIC_READ:
14500 case NOP_EXPR: /* atomic write */
14501 v = c_parser_cast_expression (parser, NULL).value;
14502 non_lvalue_p = !lvalue_p (v);
14503 v = c_fully_fold (v, false, NULL);
14504 if (v == error_mark_node)
14505 goto saw_error;
14506 if (non_lvalue_p)
14507 v = non_lvalue (v);
14508 loc = c_parser_peek_token (parser)->location;
14509 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14510 goto saw_error;
14511 if (code == NOP_EXPR)
14513 lhs = c_parser_expression (parser).value;
14514 lhs = c_fully_fold (lhs, false, NULL);
14515 if (lhs == error_mark_node)
14516 goto saw_error;
14518 else
14520 lhs = c_parser_cast_expression (parser, NULL).value;
14521 non_lvalue_p = !lvalue_p (lhs);
14522 lhs = c_fully_fold (lhs, false, NULL);
14523 if (lhs == error_mark_node)
14524 goto saw_error;
14525 if (non_lvalue_p)
14526 lhs = non_lvalue (lhs);
14528 if (code == NOP_EXPR)
14530 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14531 opcode. */
14532 code = OMP_ATOMIC;
14533 rhs = lhs;
14534 lhs = v;
14535 v = NULL_TREE;
14537 goto done;
14538 case OMP_ATOMIC_CAPTURE_NEW:
14539 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14541 c_parser_consume_token (parser);
14542 structured_block = true;
14544 else
14546 v = c_parser_cast_expression (parser, NULL).value;
14547 non_lvalue_p = !lvalue_p (v);
14548 v = c_fully_fold (v, false, NULL);
14549 if (v == error_mark_node)
14550 goto saw_error;
14551 if (non_lvalue_p)
14552 v = non_lvalue (v);
14553 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14554 goto saw_error;
14556 break;
14557 default:
14558 break;
14561 /* For structured_block case we don't know yet whether
14562 old or new x should be captured. */
14563 restart:
14564 eloc = c_parser_peek_token (parser)->location;
14565 expr = c_parser_cast_expression (parser, NULL);
14566 lhs = expr.value;
14567 expr = default_function_array_conversion (eloc, expr);
14568 unfolded_lhs = expr.value;
14569 lhs = c_fully_fold (lhs, false, NULL);
14570 orig_lhs = lhs;
14571 switch (TREE_CODE (lhs))
14573 case ERROR_MARK:
14574 saw_error:
14575 c_parser_skip_to_end_of_block_or_statement (parser);
14576 if (structured_block)
14578 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14579 c_parser_consume_token (parser);
14580 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14582 c_parser_skip_to_end_of_block_or_statement (parser);
14583 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14584 c_parser_consume_token (parser);
14587 return;
14589 case POSTINCREMENT_EXPR:
14590 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14591 code = OMP_ATOMIC_CAPTURE_OLD;
14592 /* FALLTHROUGH */
14593 case PREINCREMENT_EXPR:
14594 lhs = TREE_OPERAND (lhs, 0);
14595 unfolded_lhs = NULL_TREE;
14596 opcode = PLUS_EXPR;
14597 rhs = integer_one_node;
14598 break;
14600 case POSTDECREMENT_EXPR:
14601 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14602 code = OMP_ATOMIC_CAPTURE_OLD;
14603 /* FALLTHROUGH */
14604 case PREDECREMENT_EXPR:
14605 lhs = TREE_OPERAND (lhs, 0);
14606 unfolded_lhs = NULL_TREE;
14607 opcode = MINUS_EXPR;
14608 rhs = integer_one_node;
14609 break;
14611 case COMPOUND_EXPR:
14612 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14613 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14614 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14615 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14616 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14617 (TREE_OPERAND (lhs, 1), 0), 0)))
14618 == BOOLEAN_TYPE)
14619 /* Undo effects of boolean_increment for post {in,de}crement. */
14620 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14621 /* FALLTHRU */
14622 case MODIFY_EXPR:
14623 if (TREE_CODE (lhs) == MODIFY_EXPR
14624 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14626 /* Undo effects of boolean_increment. */
14627 if (integer_onep (TREE_OPERAND (lhs, 1)))
14629 /* This is pre or post increment. */
14630 rhs = TREE_OPERAND (lhs, 1);
14631 lhs = TREE_OPERAND (lhs, 0);
14632 unfolded_lhs = NULL_TREE;
14633 opcode = NOP_EXPR;
14634 if (code == OMP_ATOMIC_CAPTURE_NEW
14635 && !structured_block
14636 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14637 code = OMP_ATOMIC_CAPTURE_OLD;
14638 break;
14640 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14641 && TREE_OPERAND (lhs, 0)
14642 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14644 /* This is pre or post decrement. */
14645 rhs = TREE_OPERAND (lhs, 1);
14646 lhs = TREE_OPERAND (lhs, 0);
14647 unfolded_lhs = NULL_TREE;
14648 opcode = NOP_EXPR;
14649 if (code == OMP_ATOMIC_CAPTURE_NEW
14650 && !structured_block
14651 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14652 code = OMP_ATOMIC_CAPTURE_OLD;
14653 break;
14656 /* FALLTHRU */
14657 default:
14658 if (!lvalue_p (unfolded_lhs))
14659 lhs = non_lvalue (lhs);
14660 switch (c_parser_peek_token (parser)->type)
14662 case CPP_MULT_EQ:
14663 opcode = MULT_EXPR;
14664 break;
14665 case CPP_DIV_EQ:
14666 opcode = TRUNC_DIV_EXPR;
14667 break;
14668 case CPP_PLUS_EQ:
14669 opcode = PLUS_EXPR;
14670 break;
14671 case CPP_MINUS_EQ:
14672 opcode = MINUS_EXPR;
14673 break;
14674 case CPP_LSHIFT_EQ:
14675 opcode = LSHIFT_EXPR;
14676 break;
14677 case CPP_RSHIFT_EQ:
14678 opcode = RSHIFT_EXPR;
14679 break;
14680 case CPP_AND_EQ:
14681 opcode = BIT_AND_EXPR;
14682 break;
14683 case CPP_OR_EQ:
14684 opcode = BIT_IOR_EXPR;
14685 break;
14686 case CPP_XOR_EQ:
14687 opcode = BIT_XOR_EXPR;
14688 break;
14689 case CPP_EQ:
14690 c_parser_consume_token (parser);
14691 eloc = c_parser_peek_token (parser)->location;
14692 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14693 rhs1 = expr.value;
14694 switch (TREE_CODE (rhs1))
14696 case MULT_EXPR:
14697 case TRUNC_DIV_EXPR:
14698 case RDIV_EXPR:
14699 case PLUS_EXPR:
14700 case MINUS_EXPR:
14701 case LSHIFT_EXPR:
14702 case RSHIFT_EXPR:
14703 case BIT_AND_EXPR:
14704 case BIT_IOR_EXPR:
14705 case BIT_XOR_EXPR:
14706 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14708 opcode = TREE_CODE (rhs1);
14709 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14710 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14711 goto stmt_done;
14713 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14715 opcode = TREE_CODE (rhs1);
14716 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14717 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14718 swapped = !commutative_tree_code (opcode);
14719 goto stmt_done;
14721 break;
14722 case ERROR_MARK:
14723 goto saw_error;
14724 default:
14725 break;
14727 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14729 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14731 code = OMP_ATOMIC_CAPTURE_OLD;
14732 v = lhs;
14733 lhs = NULL_TREE;
14734 expr = default_function_array_read_conversion (eloc, expr);
14735 unfolded_lhs1 = expr.value;
14736 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14737 rhs1 = NULL_TREE;
14738 c_parser_consume_token (parser);
14739 goto restart;
14741 if (structured_block)
14743 opcode = NOP_EXPR;
14744 expr = default_function_array_read_conversion (eloc, expr);
14745 rhs = c_fully_fold (expr.value, false, NULL);
14746 rhs1 = NULL_TREE;
14747 goto stmt_done;
14750 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14751 goto saw_error;
14752 default:
14753 c_parser_error (parser,
14754 "invalid operator for %<#pragma omp atomic%>");
14755 goto saw_error;
14758 /* Arrange to pass the location of the assignment operator to
14759 c_finish_omp_atomic. */
14760 loc = c_parser_peek_token (parser)->location;
14761 c_parser_consume_token (parser);
14762 eloc = c_parser_peek_token (parser)->location;
14763 expr = c_parser_expression (parser);
14764 expr = default_function_array_read_conversion (eloc, expr);
14765 rhs = expr.value;
14766 rhs = c_fully_fold (rhs, false, NULL);
14767 break;
14769 stmt_done:
14770 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14772 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14773 goto saw_error;
14774 v = c_parser_cast_expression (parser, NULL).value;
14775 non_lvalue_p = !lvalue_p (v);
14776 v = c_fully_fold (v, false, NULL);
14777 if (v == error_mark_node)
14778 goto saw_error;
14779 if (non_lvalue_p)
14780 v = non_lvalue (v);
14781 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14782 goto saw_error;
14783 eloc = c_parser_peek_token (parser)->location;
14784 expr = c_parser_cast_expression (parser, NULL);
14785 lhs1 = expr.value;
14786 expr = default_function_array_read_conversion (eloc, expr);
14787 unfolded_lhs1 = expr.value;
14788 lhs1 = c_fully_fold (lhs1, false, NULL);
14789 if (lhs1 == error_mark_node)
14790 goto saw_error;
14791 if (!lvalue_p (unfolded_lhs1))
14792 lhs1 = non_lvalue (lhs1);
14794 if (structured_block)
14796 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14797 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14799 done:
14800 if (unfolded_lhs && unfolded_lhs1
14801 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14803 error ("%<#pragma omp atomic capture%> uses two different "
14804 "expressions for memory");
14805 stmt = error_mark_node;
14807 else
14808 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14809 swapped, seq_cst);
14810 if (stmt != error_mark_node)
14811 add_stmt (stmt);
14813 if (!structured_block)
14814 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14818 /* OpenMP 2.5:
14819 # pragma omp barrier new-line
14822 static void
14823 c_parser_omp_barrier (c_parser *parser)
14825 location_t loc = c_parser_peek_token (parser)->location;
14826 c_parser_consume_pragma (parser);
14827 c_parser_skip_to_pragma_eol (parser);
14829 c_finish_omp_barrier (loc);
14832 /* OpenMP 2.5:
14833 # pragma omp critical [(name)] new-line
14834 structured-block
14836 OpenMP 4.5:
14837 # pragma omp critical [(name) [hint(expression)]] new-line
14839 LOC is the location of the #pragma itself. */
14841 #define OMP_CRITICAL_CLAUSE_MASK \
14842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14844 static tree
14845 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
14847 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14849 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14851 c_parser_consume_token (parser);
14852 if (c_parser_next_token_is (parser, CPP_NAME))
14854 name = c_parser_peek_token (parser)->value;
14855 c_parser_consume_token (parser);
14856 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14858 else
14859 c_parser_error (parser, "expected identifier");
14861 clauses = c_parser_omp_all_clauses (parser,
14862 OMP_CRITICAL_CLAUSE_MASK,
14863 "#pragma omp critical");
14865 else
14867 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14868 c_parser_error (parser, "expected %<(%> or end of line");
14869 c_parser_skip_to_pragma_eol (parser);
14872 stmt = c_parser_omp_structured_block (parser, if_p);
14873 return c_finish_omp_critical (loc, stmt, name, clauses);
14876 /* OpenMP 2.5:
14877 # pragma omp flush flush-vars[opt] new-line
14879 flush-vars:
14880 ( variable-list ) */
14882 static void
14883 c_parser_omp_flush (c_parser *parser)
14885 location_t loc = c_parser_peek_token (parser)->location;
14886 c_parser_consume_pragma (parser);
14887 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14888 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14889 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14890 c_parser_error (parser, "expected %<(%> or end of line");
14891 c_parser_skip_to_pragma_eol (parser);
14893 c_finish_omp_flush (loc);
14896 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14897 The real trick here is to determine the loop control variable early
14898 so that we can push a new decl if necessary to make it private.
14899 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14900 respectively. */
14902 static tree
14903 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14904 tree clauses, tree *cclauses, bool *if_p)
14906 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14907 tree declv, condv, incrv, initv, ret = NULL_TREE;
14908 tree pre_body = NULL_TREE, this_pre_body;
14909 tree ordered_cl = NULL_TREE;
14910 bool fail = false, open_brace_parsed = false;
14911 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14912 location_t for_loc;
14913 vec<tree, va_gc> *for_block = make_tree_vector ();
14915 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14916 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14917 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14918 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14919 && OMP_CLAUSE_ORDERED_EXPR (cl))
14921 ordered_cl = cl;
14922 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14925 if (ordered && ordered < collapse)
14927 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14928 "%<ordered%> clause parameter is less than %<collapse%>");
14929 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14930 = build_int_cst (NULL_TREE, collapse);
14931 ordered = collapse;
14933 if (ordered)
14935 for (tree *pc = &clauses; *pc; )
14936 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14938 error_at (OMP_CLAUSE_LOCATION (*pc),
14939 "%<linear%> clause may not be specified together "
14940 "with %<ordered%> clause with a parameter");
14941 *pc = OMP_CLAUSE_CHAIN (*pc);
14943 else
14944 pc = &OMP_CLAUSE_CHAIN (*pc);
14947 gcc_assert (collapse >= 1 && ordered >= 0);
14948 count = ordered ? ordered : collapse;
14950 declv = make_tree_vec (count);
14951 initv = make_tree_vec (count);
14952 condv = make_tree_vec (count);
14953 incrv = make_tree_vec (count);
14955 if (code != CILK_FOR
14956 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14958 c_parser_error (parser, "for statement expected");
14959 return NULL;
14961 if (code == CILK_FOR
14962 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14964 c_parser_error (parser, "_Cilk_for statement expected");
14965 return NULL;
14967 for_loc = c_parser_peek_token (parser)->location;
14968 c_parser_consume_token (parser);
14970 for (i = 0; i < count; i++)
14972 int bracecount = 0;
14974 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14975 goto pop_scopes;
14977 /* Parse the initialization declaration or expression. */
14978 if (c_parser_next_tokens_start_declaration (parser))
14980 if (i > 0)
14981 vec_safe_push (for_block, c_begin_compound_stmt (true));
14982 this_pre_body = push_stmt_list ();
14983 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14984 NULL, vNULL);
14985 if (this_pre_body)
14987 this_pre_body = pop_stmt_list (this_pre_body);
14988 if (pre_body)
14990 tree t = pre_body;
14991 pre_body = push_stmt_list ();
14992 add_stmt (t);
14993 add_stmt (this_pre_body);
14994 pre_body = pop_stmt_list (pre_body);
14996 else
14997 pre_body = this_pre_body;
14999 decl = check_for_loop_decls (for_loc, flag_isoc99);
15000 if (decl == NULL)
15001 goto error_init;
15002 if (DECL_INITIAL (decl) == error_mark_node)
15003 decl = error_mark_node;
15004 init = decl;
15006 else if (c_parser_next_token_is (parser, CPP_NAME)
15007 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15009 struct c_expr decl_exp;
15010 struct c_expr init_exp;
15011 location_t init_loc;
15013 decl_exp = c_parser_postfix_expression (parser);
15014 decl = decl_exp.value;
15016 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15018 init_loc = c_parser_peek_token (parser)->location;
15019 init_exp = c_parser_expr_no_commas (parser, NULL);
15020 init_exp = default_function_array_read_conversion (init_loc,
15021 init_exp);
15022 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15023 NOP_EXPR, init_loc, init_exp.value,
15024 init_exp.original_type);
15025 init = c_process_expr_stmt (init_loc, init);
15027 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15029 else
15031 error_init:
15032 c_parser_error (parser,
15033 "expected iteration declaration or initialization");
15034 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15035 "expected %<)%>");
15036 fail = true;
15037 goto parse_next;
15040 /* Parse the loop condition. */
15041 cond = NULL_TREE;
15042 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15044 location_t cond_loc = c_parser_peek_token (parser)->location;
15045 struct c_expr cond_expr
15046 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15048 cond = cond_expr.value;
15049 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15050 cond = c_fully_fold (cond, false, NULL);
15051 switch (cond_expr.original_code)
15053 case GT_EXPR:
15054 case GE_EXPR:
15055 case LT_EXPR:
15056 case LE_EXPR:
15057 break;
15058 case NE_EXPR:
15059 if (code == CILK_SIMD || code == CILK_FOR)
15060 break;
15061 /* FALLTHRU. */
15062 default:
15063 /* Can't be cond = error_mark_node, because we want to preserve
15064 the location until c_finish_omp_for. */
15065 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15066 break;
15068 protected_set_expr_location (cond, cond_loc);
15070 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15072 /* Parse the increment expression. */
15073 incr = NULL_TREE;
15074 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15076 location_t incr_loc = c_parser_peek_token (parser)->location;
15078 incr = c_process_expr_stmt (incr_loc,
15079 c_parser_expression (parser).value);
15081 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15083 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15084 fail = true;
15085 else
15087 TREE_VEC_ELT (declv, i) = decl;
15088 TREE_VEC_ELT (initv, i) = init;
15089 TREE_VEC_ELT (condv, i) = cond;
15090 TREE_VEC_ELT (incrv, i) = incr;
15093 parse_next:
15094 if (i == count - 1)
15095 break;
15097 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15098 in between the collapsed for loops to be still considered perfectly
15099 nested. Hopefully the final version clarifies this.
15100 For now handle (multiple) {'s and empty statements. */
15103 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15105 c_parser_consume_token (parser);
15106 break;
15108 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15110 c_parser_consume_token (parser);
15111 bracecount++;
15113 else if (bracecount
15114 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15115 c_parser_consume_token (parser);
15116 else
15118 c_parser_error (parser, "not enough perfectly nested loops");
15119 if (bracecount)
15121 open_brace_parsed = true;
15122 bracecount--;
15124 fail = true;
15125 count = 0;
15126 break;
15129 while (1);
15131 nbraces += bracecount;
15134 if (nbraces)
15135 if_p = NULL;
15137 save_break = c_break_label;
15138 if (code == CILK_SIMD)
15139 c_break_label = build_int_cst (size_type_node, 2);
15140 else
15141 c_break_label = size_one_node;
15142 save_cont = c_cont_label;
15143 c_cont_label = NULL_TREE;
15144 body = push_stmt_list ();
15146 if (open_brace_parsed)
15148 location_t here = c_parser_peek_token (parser)->location;
15149 stmt = c_begin_compound_stmt (true);
15150 c_parser_compound_statement_nostart (parser);
15151 add_stmt (c_end_compound_stmt (here, stmt, true));
15153 else
15154 add_stmt (c_parser_c99_block_statement (parser, if_p));
15155 if (c_cont_label)
15157 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15158 SET_EXPR_LOCATION (t, loc);
15159 add_stmt (t);
15162 body = pop_stmt_list (body);
15163 c_break_label = save_break;
15164 c_cont_label = save_cont;
15166 while (nbraces)
15168 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15170 c_parser_consume_token (parser);
15171 nbraces--;
15173 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15174 c_parser_consume_token (parser);
15175 else
15177 c_parser_error (parser, "collapsed loops not perfectly nested");
15178 while (nbraces)
15180 location_t here = c_parser_peek_token (parser)->location;
15181 stmt = c_begin_compound_stmt (true);
15182 add_stmt (body);
15183 c_parser_compound_statement_nostart (parser);
15184 body = c_end_compound_stmt (here, stmt, true);
15185 nbraces--;
15187 goto pop_scopes;
15191 /* Only bother calling c_finish_omp_for if we haven't already generated
15192 an error from the initialization parsing. */
15193 if (!fail)
15195 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15196 incrv, body, pre_body);
15198 /* Check for iterators appearing in lb, b or incr expressions. */
15199 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15200 stmt = NULL_TREE;
15202 if (stmt)
15204 add_stmt (stmt);
15206 if (cclauses != NULL
15207 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15209 tree *c;
15210 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15211 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15212 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15213 c = &OMP_CLAUSE_CHAIN (*c);
15214 else
15216 for (i = 0; i < count; i++)
15217 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15218 break;
15219 if (i == count)
15220 c = &OMP_CLAUSE_CHAIN (*c);
15221 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15223 error_at (loc,
15224 "iteration variable %qD should not be firstprivate",
15225 OMP_CLAUSE_DECL (*c));
15226 *c = OMP_CLAUSE_CHAIN (*c);
15228 else
15230 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15231 tree l = *c;
15232 *c = OMP_CLAUSE_CHAIN (*c);
15233 if (code == OMP_SIMD)
15235 OMP_CLAUSE_CHAIN (l)
15236 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15237 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15239 else
15241 OMP_CLAUSE_CHAIN (l) = clauses;
15242 clauses = l;
15247 OMP_FOR_CLAUSES (stmt) = clauses;
15249 ret = stmt;
15251 pop_scopes:
15252 while (!for_block->is_empty ())
15254 /* FIXME diagnostics: LOC below should be the actual location of
15255 this particular for block. We need to build a list of
15256 locations to go along with FOR_BLOCK. */
15257 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15258 add_stmt (stmt);
15260 release_tree_vector (for_block);
15261 return ret;
15264 /* Helper function for OpenMP parsing, split clauses and call
15265 finish_omp_clauses on each of the set of clauses afterwards. */
15267 static void
15268 omp_split_clauses (location_t loc, enum tree_code code,
15269 omp_clause_mask mask, tree clauses, tree *cclauses)
15271 int i;
15272 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15273 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15274 if (cclauses[i])
15275 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15278 /* OpenMP 4.0:
15279 #pragma omp simd simd-clause[optseq] new-line
15280 for-loop
15282 LOC is the location of the #pragma token.
15285 #define OMP_SIMD_CLAUSE_MASK \
15286 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15295 static tree
15296 c_parser_omp_simd (location_t loc, c_parser *parser,
15297 char *p_name, omp_clause_mask mask, tree *cclauses,
15298 bool *if_p)
15300 tree block, clauses, ret;
15302 strcat (p_name, " simd");
15303 mask |= OMP_SIMD_CLAUSE_MASK;
15305 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15306 if (cclauses)
15308 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15309 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15310 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15311 OMP_CLAUSE_ORDERED);
15312 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15314 error_at (OMP_CLAUSE_LOCATION (c),
15315 "%<ordered%> clause with parameter may not be specified "
15316 "on %qs construct", p_name);
15317 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15321 block = c_begin_compound_stmt (true);
15322 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15323 block = c_end_compound_stmt (loc, block, true);
15324 add_stmt (block);
15326 return ret;
15329 /* OpenMP 2.5:
15330 #pragma omp for for-clause[optseq] new-line
15331 for-loop
15333 OpenMP 4.0:
15334 #pragma omp for simd for-simd-clause[optseq] new-line
15335 for-loop
15337 LOC is the location of the #pragma token.
15340 #define OMP_FOR_CLAUSE_MASK \
15341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15351 static tree
15352 c_parser_omp_for (location_t loc, c_parser *parser,
15353 char *p_name, omp_clause_mask mask, tree *cclauses,
15354 bool *if_p)
15356 tree block, clauses, ret;
15358 strcat (p_name, " for");
15359 mask |= OMP_FOR_CLAUSE_MASK;
15360 /* parallel for{, simd} disallows nowait clause, but for
15361 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15362 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15363 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15364 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15365 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15366 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15368 if (c_parser_next_token_is (parser, CPP_NAME))
15370 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15372 if (strcmp (p, "simd") == 0)
15374 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15375 if (cclauses == NULL)
15376 cclauses = cclauses_buf;
15378 c_parser_consume_token (parser);
15379 if (!flag_openmp) /* flag_openmp_simd */
15380 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15381 if_p);
15382 block = c_begin_compound_stmt (true);
15383 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15384 block = c_end_compound_stmt (loc, block, true);
15385 if (ret == NULL_TREE)
15386 return ret;
15387 ret = make_node (OMP_FOR);
15388 TREE_TYPE (ret) = void_type_node;
15389 OMP_FOR_BODY (ret) = block;
15390 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15391 SET_EXPR_LOCATION (ret, loc);
15392 add_stmt (ret);
15393 return ret;
15396 if (!flag_openmp) /* flag_openmp_simd */
15398 c_parser_skip_to_pragma_eol (parser, false);
15399 return NULL_TREE;
15402 /* Composite distribute parallel for disallows linear clause. */
15403 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15404 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15406 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15407 if (cclauses)
15409 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15410 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15413 block = c_begin_compound_stmt (true);
15414 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15415 block = c_end_compound_stmt (loc, block, true);
15416 add_stmt (block);
15418 return ret;
15421 /* OpenMP 2.5:
15422 # pragma omp master new-line
15423 structured-block
15425 LOC is the location of the #pragma token.
15428 static tree
15429 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15431 c_parser_skip_to_pragma_eol (parser);
15432 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15433 if_p));
15436 /* OpenMP 2.5:
15437 # pragma omp ordered new-line
15438 structured-block
15440 OpenMP 4.5:
15441 # pragma omp ordered ordered-clauses new-line
15442 structured-block
15444 # pragma omp ordered depend-clauses new-line */
15446 #define OMP_ORDERED_CLAUSE_MASK \
15447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15450 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15451 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15453 static bool
15454 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15455 bool *if_p)
15457 location_t loc = c_parser_peek_token (parser)->location;
15458 c_parser_consume_pragma (parser);
15460 if (context != pragma_stmt && context != pragma_compound)
15462 c_parser_error (parser, "expected declaration specifiers");
15463 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
15464 return false;
15467 if (c_parser_next_token_is (parser, CPP_NAME))
15469 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15471 if (!strcmp ("depend", p))
15473 if (context == pragma_stmt)
15475 error_at (loc,
15476 "%<#pragma omp ordered%> with %<depend> clause may "
15477 "only be used in compound statements");
15478 c_parser_skip_to_pragma_eol (parser, false);
15479 return false;
15482 tree clauses
15483 = c_parser_omp_all_clauses (parser,
15484 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15485 "#pragma omp ordered");
15486 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15487 return false;
15491 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15492 "#pragma omp ordered");
15493 c_finish_omp_ordered (loc, clauses,
15494 c_parser_omp_structured_block (parser, if_p));
15495 return true;
15498 /* OpenMP 2.5:
15500 section-scope:
15501 { section-sequence }
15503 section-sequence:
15504 section-directive[opt] structured-block
15505 section-sequence section-directive structured-block
15507 SECTIONS_LOC is the location of the #pragma omp sections. */
15509 static tree
15510 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15512 tree stmt, substmt;
15513 bool error_suppress = false;
15514 location_t loc;
15516 loc = c_parser_peek_token (parser)->location;
15517 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15519 /* Avoid skipping until the end of the block. */
15520 parser->error = false;
15521 return NULL_TREE;
15524 stmt = push_stmt_list ();
15526 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15528 substmt = c_parser_omp_structured_block (parser, NULL);
15529 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15530 SET_EXPR_LOCATION (substmt, loc);
15531 add_stmt (substmt);
15534 while (1)
15536 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15537 break;
15538 if (c_parser_next_token_is (parser, CPP_EOF))
15539 break;
15541 loc = c_parser_peek_token (parser)->location;
15542 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15544 c_parser_consume_pragma (parser);
15545 c_parser_skip_to_pragma_eol (parser);
15546 error_suppress = false;
15548 else if (!error_suppress)
15550 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15551 error_suppress = true;
15554 substmt = c_parser_omp_structured_block (parser, NULL);
15555 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15556 SET_EXPR_LOCATION (substmt, loc);
15557 add_stmt (substmt);
15559 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15560 "expected %<#pragma omp section%> or %<}%>");
15562 substmt = pop_stmt_list (stmt);
15564 stmt = make_node (OMP_SECTIONS);
15565 SET_EXPR_LOCATION (stmt, sections_loc);
15566 TREE_TYPE (stmt) = void_type_node;
15567 OMP_SECTIONS_BODY (stmt) = substmt;
15569 return add_stmt (stmt);
15572 /* OpenMP 2.5:
15573 # pragma omp sections sections-clause[optseq] newline
15574 sections-scope
15576 LOC is the location of the #pragma token.
15579 #define OMP_SECTIONS_CLAUSE_MASK \
15580 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15586 static tree
15587 c_parser_omp_sections (location_t loc, c_parser *parser,
15588 char *p_name, omp_clause_mask mask, tree *cclauses)
15590 tree block, clauses, ret;
15592 strcat (p_name, " sections");
15593 mask |= OMP_SECTIONS_CLAUSE_MASK;
15594 if (cclauses)
15595 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15597 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15598 if (cclauses)
15600 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15601 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15604 block = c_begin_compound_stmt (true);
15605 ret = c_parser_omp_sections_scope (loc, parser);
15606 if (ret)
15607 OMP_SECTIONS_CLAUSES (ret) = clauses;
15608 block = c_end_compound_stmt (loc, block, true);
15609 add_stmt (block);
15611 return ret;
15614 /* OpenMP 2.5:
15615 # pragma omp parallel parallel-clause[optseq] new-line
15616 structured-block
15617 # pragma omp parallel for parallel-for-clause[optseq] new-line
15618 structured-block
15619 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15620 structured-block
15622 OpenMP 4.0:
15623 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15624 structured-block
15626 LOC is the location of the #pragma token.
15629 #define OMP_PARALLEL_CLAUSE_MASK \
15630 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15640 static tree
15641 c_parser_omp_parallel (location_t loc, c_parser *parser,
15642 char *p_name, omp_clause_mask mask, tree *cclauses,
15643 bool *if_p)
15645 tree stmt, clauses, block;
15647 strcat (p_name, " parallel");
15648 mask |= OMP_PARALLEL_CLAUSE_MASK;
15649 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15650 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15651 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15652 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15654 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15656 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15657 if (cclauses == NULL)
15658 cclauses = cclauses_buf;
15660 c_parser_consume_token (parser);
15661 if (!flag_openmp) /* flag_openmp_simd */
15662 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15663 block = c_begin_omp_parallel ();
15664 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15665 stmt
15666 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15667 block);
15668 if (ret == NULL_TREE)
15669 return ret;
15670 OMP_PARALLEL_COMBINED (stmt) = 1;
15671 return stmt;
15673 /* When combined with distribute, parallel has to be followed by for.
15674 #pragma omp target parallel is allowed though. */
15675 else if (cclauses
15676 && (mask & (OMP_CLAUSE_MASK_1
15677 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15679 error_at (loc, "expected %<for%> after %qs", p_name);
15680 c_parser_skip_to_pragma_eol (parser);
15681 return NULL_TREE;
15683 else if (!flag_openmp) /* flag_openmp_simd */
15685 c_parser_skip_to_pragma_eol (parser, false);
15686 return NULL_TREE;
15688 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15690 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15691 if (strcmp (p, "sections") == 0)
15693 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15694 if (cclauses == NULL)
15695 cclauses = cclauses_buf;
15697 c_parser_consume_token (parser);
15698 block = c_begin_omp_parallel ();
15699 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15700 stmt = c_finish_omp_parallel (loc,
15701 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15702 block);
15703 OMP_PARALLEL_COMBINED (stmt) = 1;
15704 return stmt;
15708 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15709 if (cclauses)
15711 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15712 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15715 block = c_begin_omp_parallel ();
15716 c_parser_statement (parser, if_p);
15717 stmt = c_finish_omp_parallel (loc, clauses, block);
15719 return stmt;
15722 /* OpenMP 2.5:
15723 # pragma omp single single-clause[optseq] new-line
15724 structured-block
15726 LOC is the location of the #pragma.
15729 #define OMP_SINGLE_CLAUSE_MASK \
15730 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15735 static tree
15736 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15738 tree stmt = make_node (OMP_SINGLE);
15739 SET_EXPR_LOCATION (stmt, loc);
15740 TREE_TYPE (stmt) = void_type_node;
15742 OMP_SINGLE_CLAUSES (stmt)
15743 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15744 "#pragma omp single");
15745 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15747 return add_stmt (stmt);
15750 /* OpenMP 3.0:
15751 # pragma omp task task-clause[optseq] new-line
15753 LOC is the location of the #pragma.
15756 #define OMP_TASK_CLAUSE_MASK \
15757 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15768 static tree
15769 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
15771 tree clauses, block;
15773 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15774 "#pragma omp task");
15776 block = c_begin_omp_task ();
15777 c_parser_statement (parser, if_p);
15778 return c_finish_omp_task (loc, clauses, block);
15781 /* OpenMP 3.0:
15782 # pragma omp taskwait new-line
15785 static void
15786 c_parser_omp_taskwait (c_parser *parser)
15788 location_t loc = c_parser_peek_token (parser)->location;
15789 c_parser_consume_pragma (parser);
15790 c_parser_skip_to_pragma_eol (parser);
15792 c_finish_omp_taskwait (loc);
15795 /* OpenMP 3.1:
15796 # pragma omp taskyield new-line
15799 static void
15800 c_parser_omp_taskyield (c_parser *parser)
15802 location_t loc = c_parser_peek_token (parser)->location;
15803 c_parser_consume_pragma (parser);
15804 c_parser_skip_to_pragma_eol (parser);
15806 c_finish_omp_taskyield (loc);
15809 /* OpenMP 4.0:
15810 # pragma omp taskgroup new-line
15813 static tree
15814 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
15816 location_t loc = c_parser_peek_token (parser)->location;
15817 c_parser_skip_to_pragma_eol (parser);
15818 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
15819 if_p));
15822 /* OpenMP 4.0:
15823 # pragma omp cancel cancel-clause[optseq] new-line
15825 LOC is the location of the #pragma.
15828 #define OMP_CANCEL_CLAUSE_MASK \
15829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15835 static void
15836 c_parser_omp_cancel (c_parser *parser)
15838 location_t loc = c_parser_peek_token (parser)->location;
15840 c_parser_consume_pragma (parser);
15841 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15842 "#pragma omp cancel");
15844 c_finish_omp_cancel (loc, clauses);
15847 /* OpenMP 4.0:
15848 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15850 LOC is the location of the #pragma.
15853 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15854 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15859 static void
15860 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
15862 location_t loc = c_parser_peek_token (parser)->location;
15863 tree clauses;
15864 bool point_seen = false;
15866 c_parser_consume_pragma (parser);
15867 if (c_parser_next_token_is (parser, CPP_NAME))
15869 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15870 if (strcmp (p, "point") == 0)
15872 c_parser_consume_token (parser);
15873 point_seen = true;
15876 if (!point_seen)
15878 c_parser_error (parser, "expected %<point%>");
15879 c_parser_skip_to_pragma_eol (parser);
15880 return;
15883 if (context != pragma_compound)
15885 if (context == pragma_stmt)
15886 error_at (loc, "%<#pragma omp cancellation point%> may only be used in"
15887 " compound statements");
15888 else
15889 c_parser_error (parser, "expected declaration specifiers");
15890 c_parser_skip_to_pragma_eol (parser, false);
15891 return;
15894 clauses
15895 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15896 "#pragma omp cancellation point");
15898 c_finish_omp_cancellation_point (loc, clauses);
15901 /* OpenMP 4.0:
15902 #pragma omp distribute distribute-clause[optseq] new-line
15903 for-loop */
15905 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15906 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15912 static tree
15913 c_parser_omp_distribute (location_t loc, c_parser *parser,
15914 char *p_name, omp_clause_mask mask, tree *cclauses,
15915 bool *if_p)
15917 tree clauses, block, ret;
15919 strcat (p_name, " distribute");
15920 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15922 if (c_parser_next_token_is (parser, CPP_NAME))
15924 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15925 bool simd = false;
15926 bool parallel = false;
15928 if (strcmp (p, "simd") == 0)
15929 simd = true;
15930 else
15931 parallel = strcmp (p, "parallel") == 0;
15932 if (parallel || simd)
15934 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15935 if (cclauses == NULL)
15936 cclauses = cclauses_buf;
15937 c_parser_consume_token (parser);
15938 if (!flag_openmp) /* flag_openmp_simd */
15940 if (simd)
15941 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15942 if_p);
15943 else
15944 return c_parser_omp_parallel (loc, parser, p_name, mask,
15945 cclauses, if_p);
15947 block = c_begin_compound_stmt (true);
15948 if (simd)
15949 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15950 if_p);
15951 else
15952 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
15953 if_p);
15954 block = c_end_compound_stmt (loc, block, true);
15955 if (ret == NULL)
15956 return ret;
15957 ret = make_node (OMP_DISTRIBUTE);
15958 TREE_TYPE (ret) = void_type_node;
15959 OMP_FOR_BODY (ret) = block;
15960 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15961 SET_EXPR_LOCATION (ret, loc);
15962 add_stmt (ret);
15963 return ret;
15966 if (!flag_openmp) /* flag_openmp_simd */
15968 c_parser_skip_to_pragma_eol (parser, false);
15969 return NULL_TREE;
15972 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15973 if (cclauses)
15975 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15976 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15979 block = c_begin_compound_stmt (true);
15980 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
15981 if_p);
15982 block = c_end_compound_stmt (loc, block, true);
15983 add_stmt (block);
15985 return ret;
15988 /* OpenMP 4.0:
15989 # pragma omp teams teams-clause[optseq] new-line
15990 structured-block */
15992 #define OMP_TEAMS_CLAUSE_MASK \
15993 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16001 static tree
16002 c_parser_omp_teams (location_t loc, c_parser *parser,
16003 char *p_name, omp_clause_mask mask, tree *cclauses,
16004 bool *if_p)
16006 tree clauses, block, ret;
16008 strcat (p_name, " teams");
16009 mask |= OMP_TEAMS_CLAUSE_MASK;
16011 if (c_parser_next_token_is (parser, CPP_NAME))
16013 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16014 if (strcmp (p, "distribute") == 0)
16016 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16017 if (cclauses == NULL)
16018 cclauses = cclauses_buf;
16020 c_parser_consume_token (parser);
16021 if (!flag_openmp) /* flag_openmp_simd */
16022 return c_parser_omp_distribute (loc, parser, p_name, mask,
16023 cclauses, if_p);
16024 block = c_begin_compound_stmt (true);
16025 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16026 if_p);
16027 block = c_end_compound_stmt (loc, block, true);
16028 if (ret == NULL)
16029 return ret;
16030 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16031 ret = make_node (OMP_TEAMS);
16032 TREE_TYPE (ret) = void_type_node;
16033 OMP_TEAMS_CLAUSES (ret) = clauses;
16034 OMP_TEAMS_BODY (ret) = block;
16035 OMP_TEAMS_COMBINED (ret) = 1;
16036 return add_stmt (ret);
16039 if (!flag_openmp) /* flag_openmp_simd */
16041 c_parser_skip_to_pragma_eol (parser, false);
16042 return NULL_TREE;
16045 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16046 if (cclauses)
16048 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16049 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16052 tree stmt = make_node (OMP_TEAMS);
16053 TREE_TYPE (stmt) = void_type_node;
16054 OMP_TEAMS_CLAUSES (stmt) = clauses;
16055 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16057 return add_stmt (stmt);
16060 /* OpenMP 4.0:
16061 # pragma omp target data target-data-clause[optseq] new-line
16062 structured-block */
16064 #define OMP_TARGET_DATA_CLAUSE_MASK \
16065 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16070 static tree
16071 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16073 tree clauses
16074 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16075 "#pragma omp target data");
16076 int map_seen = 0;
16077 for (tree *pc = &clauses; *pc;)
16079 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16080 switch (OMP_CLAUSE_MAP_KIND (*pc))
16082 case GOMP_MAP_TO:
16083 case GOMP_MAP_ALWAYS_TO:
16084 case GOMP_MAP_FROM:
16085 case GOMP_MAP_ALWAYS_FROM:
16086 case GOMP_MAP_TOFROM:
16087 case GOMP_MAP_ALWAYS_TOFROM:
16088 case GOMP_MAP_ALLOC:
16089 map_seen = 3;
16090 break;
16091 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16092 case GOMP_MAP_ALWAYS_POINTER:
16093 break;
16094 default:
16095 map_seen |= 1;
16096 error_at (OMP_CLAUSE_LOCATION (*pc),
16097 "%<#pragma omp target data%> with map-type other "
16098 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16099 "on %<map%> clause");
16100 *pc = OMP_CLAUSE_CHAIN (*pc);
16101 continue;
16103 pc = &OMP_CLAUSE_CHAIN (*pc);
16106 if (map_seen != 3)
16108 if (map_seen == 0)
16109 error_at (loc,
16110 "%<#pragma omp target data%> must contain at least "
16111 "one %<map%> clause");
16112 return NULL_TREE;
16115 tree stmt = make_node (OMP_TARGET_DATA);
16116 TREE_TYPE (stmt) = void_type_node;
16117 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16118 keep_next_level ();
16119 tree block = c_begin_compound_stmt (true);
16120 add_stmt (c_parser_omp_structured_block (parser, if_p));
16121 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16123 SET_EXPR_LOCATION (stmt, loc);
16124 return add_stmt (stmt);
16127 /* OpenMP 4.0:
16128 # pragma omp target update target-update-clause[optseq] new-line */
16130 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16131 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16138 static bool
16139 c_parser_omp_target_update (location_t loc, c_parser *parser,
16140 enum pragma_context context)
16142 if (context == pragma_stmt)
16144 error_at (loc,
16145 "%<#pragma omp target update%> may only be "
16146 "used in compound statements");
16147 c_parser_skip_to_pragma_eol (parser, false);
16148 return false;
16151 tree clauses
16152 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16153 "#pragma omp target update");
16154 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16155 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16157 error_at (loc,
16158 "%<#pragma omp target update%> must contain at least one "
16159 "%<from%> or %<to%> clauses");
16160 return false;
16163 tree stmt = make_node (OMP_TARGET_UPDATE);
16164 TREE_TYPE (stmt) = void_type_node;
16165 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16166 SET_EXPR_LOCATION (stmt, loc);
16167 add_stmt (stmt);
16168 return false;
16171 /* OpenMP 4.5:
16172 # pragma omp target enter data target-data-clause[optseq] new-line */
16174 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16175 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16181 static tree
16182 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16183 enum pragma_context context)
16185 bool data_seen = false;
16186 if (c_parser_next_token_is (parser, CPP_NAME))
16188 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16189 if (strcmp (p, "data") == 0)
16191 c_parser_consume_token (parser);
16192 data_seen = true;
16195 if (!data_seen)
16197 c_parser_error (parser, "expected %<data%>");
16198 c_parser_skip_to_pragma_eol (parser);
16199 return NULL_TREE;
16202 if (context == pragma_stmt)
16204 error_at (loc,
16205 "%<#pragma omp target enter data%> may only be "
16206 "used in compound statements");
16207 c_parser_skip_to_pragma_eol (parser, false);
16208 return NULL_TREE;
16211 tree clauses
16212 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16213 "#pragma omp target enter data");
16214 int map_seen = 0;
16215 for (tree *pc = &clauses; *pc;)
16217 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16218 switch (OMP_CLAUSE_MAP_KIND (*pc))
16220 case GOMP_MAP_TO:
16221 case GOMP_MAP_ALWAYS_TO:
16222 case GOMP_MAP_ALLOC:
16223 map_seen = 3;
16224 break;
16225 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16226 case GOMP_MAP_ALWAYS_POINTER:
16227 break;
16228 default:
16229 map_seen |= 1;
16230 error_at (OMP_CLAUSE_LOCATION (*pc),
16231 "%<#pragma omp target enter data%> with map-type other "
16232 "than %<to%> or %<alloc%> on %<map%> clause");
16233 *pc = OMP_CLAUSE_CHAIN (*pc);
16234 continue;
16236 pc = &OMP_CLAUSE_CHAIN (*pc);
16239 if (map_seen != 3)
16241 if (map_seen == 0)
16242 error_at (loc,
16243 "%<#pragma omp target enter data%> must contain at least "
16244 "one %<map%> clause");
16245 return NULL_TREE;
16248 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16249 TREE_TYPE (stmt) = void_type_node;
16250 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16251 SET_EXPR_LOCATION (stmt, loc);
16252 add_stmt (stmt);
16253 return stmt;
16256 /* OpenMP 4.5:
16257 # pragma omp target exit data target-data-clause[optseq] new-line */
16259 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16260 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16266 static tree
16267 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16268 enum pragma_context context)
16270 bool data_seen = false;
16271 if (c_parser_next_token_is (parser, CPP_NAME))
16273 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16274 if (strcmp (p, "data") == 0)
16276 c_parser_consume_token (parser);
16277 data_seen = true;
16280 if (!data_seen)
16282 c_parser_error (parser, "expected %<data%>");
16283 c_parser_skip_to_pragma_eol (parser);
16284 return NULL_TREE;
16287 if (context == pragma_stmt)
16289 error_at (loc,
16290 "%<#pragma omp target exit data%> may only be "
16291 "used in compound statements");
16292 c_parser_skip_to_pragma_eol (parser, false);
16293 return NULL_TREE;
16296 tree clauses
16297 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16298 "#pragma omp target exit data");
16300 int map_seen = 0;
16301 for (tree *pc = &clauses; *pc;)
16303 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16304 switch (OMP_CLAUSE_MAP_KIND (*pc))
16306 case GOMP_MAP_FROM:
16307 case GOMP_MAP_ALWAYS_FROM:
16308 case GOMP_MAP_RELEASE:
16309 case GOMP_MAP_DELETE:
16310 map_seen = 3;
16311 break;
16312 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16313 case GOMP_MAP_ALWAYS_POINTER:
16314 break;
16315 default:
16316 map_seen |= 1;
16317 error_at (OMP_CLAUSE_LOCATION (*pc),
16318 "%<#pragma omp target exit data%> with map-type other "
16319 "than %<from%>, %<release> or %<delete%> on %<map%>"
16320 " clause");
16321 *pc = OMP_CLAUSE_CHAIN (*pc);
16322 continue;
16324 pc = &OMP_CLAUSE_CHAIN (*pc);
16327 if (map_seen != 3)
16329 if (map_seen == 0)
16330 error_at (loc,
16331 "%<#pragma omp target exit data%> must contain at least one "
16332 "%<map%> clause");
16333 return NULL_TREE;
16336 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16337 TREE_TYPE (stmt) = void_type_node;
16338 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16339 SET_EXPR_LOCATION (stmt, loc);
16340 add_stmt (stmt);
16341 return stmt;
16344 /* OpenMP 4.0:
16345 # pragma omp target target-clause[optseq] new-line
16346 structured-block */
16348 #define OMP_TARGET_CLAUSE_MASK \
16349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16359 static bool
16360 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16362 location_t loc = c_parser_peek_token (parser)->location;
16363 c_parser_consume_pragma (parser);
16364 tree *pc = NULL, stmt, block;
16366 if (context != pragma_stmt && context != pragma_compound)
16368 c_parser_error (parser, "expected declaration specifiers");
16369 c_parser_skip_to_pragma_eol (parser);
16370 return false;
16373 if (c_parser_next_token_is (parser, CPP_NAME))
16375 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16376 enum tree_code ccode = ERROR_MARK;
16378 if (strcmp (p, "teams") == 0)
16379 ccode = OMP_TEAMS;
16380 else if (strcmp (p, "parallel") == 0)
16381 ccode = OMP_PARALLEL;
16382 else if (strcmp (p, "simd") == 0)
16383 ccode = OMP_SIMD;
16384 if (ccode != ERROR_MARK)
16386 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16387 char p_name[sizeof ("#pragma omp target teams distribute "
16388 "parallel for simd")];
16390 c_parser_consume_token (parser);
16391 strcpy (p_name, "#pragma omp target");
16392 if (!flag_openmp) /* flag_openmp_simd */
16394 tree stmt;
16395 switch (ccode)
16397 case OMP_TEAMS:
16398 stmt = c_parser_omp_teams (loc, parser, p_name,
16399 OMP_TARGET_CLAUSE_MASK,
16400 cclauses, if_p);
16401 break;
16402 case OMP_PARALLEL:
16403 stmt = c_parser_omp_parallel (loc, parser, p_name,
16404 OMP_TARGET_CLAUSE_MASK,
16405 cclauses, if_p);
16406 break;
16407 case OMP_SIMD:
16408 stmt = c_parser_omp_simd (loc, parser, p_name,
16409 OMP_TARGET_CLAUSE_MASK,
16410 cclauses, if_p);
16411 break;
16412 default:
16413 gcc_unreachable ();
16415 return stmt != NULL_TREE;
16417 keep_next_level ();
16418 tree block = c_begin_compound_stmt (true), ret;
16419 switch (ccode)
16421 case OMP_TEAMS:
16422 ret = c_parser_omp_teams (loc, parser, p_name,
16423 OMP_TARGET_CLAUSE_MASK, cclauses,
16424 if_p);
16425 break;
16426 case OMP_PARALLEL:
16427 ret = c_parser_omp_parallel (loc, parser, p_name,
16428 OMP_TARGET_CLAUSE_MASK, cclauses,
16429 if_p);
16430 break;
16431 case OMP_SIMD:
16432 ret = c_parser_omp_simd (loc, parser, p_name,
16433 OMP_TARGET_CLAUSE_MASK, cclauses,
16434 if_p);
16435 break;
16436 default:
16437 gcc_unreachable ();
16439 block = c_end_compound_stmt (loc, block, true);
16440 if (ret == NULL_TREE)
16441 return false;
16442 if (ccode == OMP_TEAMS)
16444 /* For combined target teams, ensure the num_teams and
16445 thread_limit clause expressions are evaluated on the host,
16446 before entering the target construct. */
16447 tree c;
16448 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16449 c; c = OMP_CLAUSE_CHAIN (c))
16450 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16451 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16452 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16454 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16455 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16456 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16457 expr, NULL_TREE, NULL_TREE);
16458 add_stmt (expr);
16459 OMP_CLAUSE_OPERAND (c, 0) = expr;
16460 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16461 OMP_CLAUSE_FIRSTPRIVATE);
16462 OMP_CLAUSE_DECL (tc) = tmp;
16463 OMP_CLAUSE_CHAIN (tc)
16464 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16465 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16468 tree stmt = make_node (OMP_TARGET);
16469 TREE_TYPE (stmt) = void_type_node;
16470 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16471 OMP_TARGET_BODY (stmt) = block;
16472 OMP_TARGET_COMBINED (stmt) = 1;
16473 add_stmt (stmt);
16474 pc = &OMP_TARGET_CLAUSES (stmt);
16475 goto check_clauses;
16477 else if (!flag_openmp) /* flag_openmp_simd */
16479 c_parser_skip_to_pragma_eol (parser, false);
16480 return false;
16482 else if (strcmp (p, "data") == 0)
16484 c_parser_consume_token (parser);
16485 c_parser_omp_target_data (loc, parser, if_p);
16486 return true;
16488 else if (strcmp (p, "enter") == 0)
16490 c_parser_consume_token (parser);
16491 c_parser_omp_target_enter_data (loc, parser, context);
16492 return false;
16494 else if (strcmp (p, "exit") == 0)
16496 c_parser_consume_token (parser);
16497 c_parser_omp_target_exit_data (loc, parser, context);
16498 return false;
16500 else if (strcmp (p, "update") == 0)
16502 c_parser_consume_token (parser);
16503 return c_parser_omp_target_update (loc, parser, context);
16507 stmt = make_node (OMP_TARGET);
16508 TREE_TYPE (stmt) = void_type_node;
16510 OMP_TARGET_CLAUSES (stmt)
16511 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16512 "#pragma omp target");
16513 pc = &OMP_TARGET_CLAUSES (stmt);
16514 keep_next_level ();
16515 block = c_begin_compound_stmt (true);
16516 add_stmt (c_parser_omp_structured_block (parser, if_p));
16517 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16519 SET_EXPR_LOCATION (stmt, loc);
16520 add_stmt (stmt);
16522 check_clauses:
16523 while (*pc)
16525 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16526 switch (OMP_CLAUSE_MAP_KIND (*pc))
16528 case GOMP_MAP_TO:
16529 case GOMP_MAP_ALWAYS_TO:
16530 case GOMP_MAP_FROM:
16531 case GOMP_MAP_ALWAYS_FROM:
16532 case GOMP_MAP_TOFROM:
16533 case GOMP_MAP_ALWAYS_TOFROM:
16534 case GOMP_MAP_ALLOC:
16535 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16536 case GOMP_MAP_ALWAYS_POINTER:
16537 break;
16538 default:
16539 error_at (OMP_CLAUSE_LOCATION (*pc),
16540 "%<#pragma omp target%> with map-type other "
16541 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16542 "on %<map%> clause");
16543 *pc = OMP_CLAUSE_CHAIN (*pc);
16544 continue;
16546 pc = &OMP_CLAUSE_CHAIN (*pc);
16548 return true;
16551 /* OpenMP 4.0:
16552 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16554 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16555 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16562 static void
16563 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16565 auto_vec<c_token> clauses;
16566 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16568 c_token *token = c_parser_peek_token (parser);
16569 if (token->type == CPP_EOF)
16571 c_parser_skip_to_pragma_eol (parser);
16572 return;
16574 clauses.safe_push (*token);
16575 c_parser_consume_token (parser);
16577 clauses.safe_push (*c_parser_peek_token (parser));
16578 c_parser_skip_to_pragma_eol (parser);
16580 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16582 if (c_parser_peek_token (parser)->pragma_kind
16583 != PRAGMA_OMP_DECLARE
16584 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16585 || strcmp (IDENTIFIER_POINTER
16586 (c_parser_peek_2nd_token (parser)->value),
16587 "simd") != 0)
16589 c_parser_error (parser,
16590 "%<#pragma omp declare simd%> must be followed by "
16591 "function declaration or definition or another "
16592 "%<#pragma omp declare simd%>");
16593 return;
16595 c_parser_consume_pragma (parser);
16596 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16598 c_token *token = c_parser_peek_token (parser);
16599 if (token->type == CPP_EOF)
16601 c_parser_skip_to_pragma_eol (parser);
16602 return;
16604 clauses.safe_push (*token);
16605 c_parser_consume_token (parser);
16607 clauses.safe_push (*c_parser_peek_token (parser));
16608 c_parser_skip_to_pragma_eol (parser);
16611 /* Make sure nothing tries to read past the end of the tokens. */
16612 c_token eof_token;
16613 memset (&eof_token, 0, sizeof (eof_token));
16614 eof_token.type = CPP_EOF;
16615 clauses.safe_push (eof_token);
16616 clauses.safe_push (eof_token);
16618 switch (context)
16620 case pragma_external:
16621 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16622 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16624 int ext = disable_extension_diagnostics ();
16626 c_parser_consume_token (parser);
16627 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16628 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16629 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16630 NULL, clauses);
16631 restore_extension_diagnostics (ext);
16633 else
16634 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16635 NULL, clauses);
16636 break;
16637 case pragma_struct:
16638 case pragma_param:
16639 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16640 "function declaration or definition");
16641 break;
16642 case pragma_compound:
16643 case pragma_stmt:
16644 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16645 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16647 int ext = disable_extension_diagnostics ();
16649 c_parser_consume_token (parser);
16650 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16651 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16652 if (c_parser_next_tokens_start_declaration (parser))
16654 c_parser_declaration_or_fndef (parser, true, true, true, true,
16655 true, NULL, clauses);
16656 restore_extension_diagnostics (ext);
16657 break;
16659 restore_extension_diagnostics (ext);
16661 else if (c_parser_next_tokens_start_declaration (parser))
16663 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16664 NULL, clauses);
16665 break;
16667 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16668 "function declaration or definition");
16669 break;
16670 default:
16671 gcc_unreachable ();
16675 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16676 and put that into "omp declare simd" attribute. */
16678 static void
16679 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16680 vec<c_token> clauses)
16682 if (flag_cilkplus
16683 && (clauses.exists ()
16684 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16685 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16687 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16688 "used in the same function marked as a Cilk Plus SIMD-enabled "
16689 "function");
16690 vec_free (parser->cilk_simd_fn_tokens);
16691 return;
16694 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16695 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16696 has already processed the tokens. */
16697 if (clauses.exists () && clauses[0].type == CPP_EOF)
16698 return;
16699 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16701 error ("%<#pragma omp declare simd%> not immediately followed by "
16702 "a function declaration or definition");
16703 clauses[0].type = CPP_EOF;
16704 return;
16706 if (clauses.exists () && clauses[0].type != CPP_NAME)
16708 error_at (DECL_SOURCE_LOCATION (fndecl),
16709 "%<#pragma omp declare simd%> not immediately followed by "
16710 "a single function declaration or definition");
16711 clauses[0].type = CPP_EOF;
16712 return;
16715 if (parms == NULL_TREE)
16716 parms = DECL_ARGUMENTS (fndecl);
16718 unsigned int tokens_avail = parser->tokens_avail;
16719 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16720 bool is_cilkplus_cilk_simd_fn = false;
16722 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16724 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16725 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16726 is_cilkplus_cilk_simd_fn = true;
16728 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16730 error_at (DECL_SOURCE_LOCATION (fndecl),
16731 "%<__simd__%> attribute cannot be used in the same "
16732 "function marked as a Cilk Plus SIMD-enabled function");
16733 vec_free (parser->cilk_simd_fn_tokens);
16734 return;
16738 else
16740 parser->tokens = clauses.address ();
16741 parser->tokens_avail = clauses.length ();
16744 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16745 while (parser->tokens_avail > 3)
16747 c_token *token = c_parser_peek_token (parser);
16748 if (!is_cilkplus_cilk_simd_fn)
16749 gcc_assert (token->type == CPP_NAME
16750 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16751 else
16752 gcc_assert (token->type == CPP_NAME
16753 && is_cilkplus_vector_p (token->value));
16754 c_parser_consume_token (parser);
16755 parser->in_pragma = true;
16757 tree c = NULL_TREE;
16758 if (is_cilkplus_cilk_simd_fn)
16759 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16760 "SIMD-enabled functions attribute");
16761 else
16762 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16763 "#pragma omp declare simd");
16764 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16765 if (c != NULL_TREE)
16766 c = tree_cons (NULL_TREE, c, NULL_TREE);
16767 if (is_cilkplus_cilk_simd_fn)
16769 tree k = build_tree_list (get_identifier ("cilk simd function"),
16770 NULL_TREE);
16771 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16772 DECL_ATTRIBUTES (fndecl) = k;
16774 c = build_tree_list (get_identifier ("omp declare simd"), c);
16775 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16776 DECL_ATTRIBUTES (fndecl) = c;
16779 parser->tokens = &parser->tokens_buf[0];
16780 parser->tokens_avail = tokens_avail;
16781 if (clauses.exists ())
16782 clauses[0].type = CPP_PRAGMA;
16784 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16785 vec_free (parser->cilk_simd_fn_tokens);
16789 /* OpenMP 4.0:
16790 # pragma omp declare target new-line
16791 declarations and definitions
16792 # pragma omp end declare target new-line
16794 OpenMP 4.5:
16795 # pragma omp declare target ( extended-list ) new-line
16797 # pragma omp declare target declare-target-clauses[seq] new-line */
16799 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16800 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16803 static void
16804 c_parser_omp_declare_target (c_parser *parser)
16806 location_t loc = c_parser_peek_token (parser)->location;
16807 tree clauses = NULL_TREE;
16808 if (c_parser_next_token_is (parser, CPP_NAME))
16809 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16810 "#pragma omp declare target");
16811 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16813 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16814 clauses);
16815 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
16816 c_parser_skip_to_pragma_eol (parser);
16818 else
16820 c_parser_skip_to_pragma_eol (parser);
16821 current_omp_declare_target_attribute++;
16822 return;
16824 if (current_omp_declare_target_attribute)
16825 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16826 "%<#pragma omp declare target%> without clauses and "
16827 "%<#pragma omp end declare target%>");
16828 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16830 tree t = OMP_CLAUSE_DECL (c), id;
16831 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16832 tree at2 = lookup_attribute ("omp declare target link",
16833 DECL_ATTRIBUTES (t));
16834 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16836 id = get_identifier ("omp declare target link");
16837 std::swap (at1, at2);
16839 else
16840 id = get_identifier ("omp declare target");
16841 if (at2)
16843 error_at (OMP_CLAUSE_LOCATION (c),
16844 "%qD specified both in declare target %<link%> and %<to%>"
16845 " clauses", t);
16846 continue;
16848 if (!at1)
16850 symtab_node *node = symtab_node::get (t);
16851 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16852 if (node != NULL)
16854 node->offloadable = 1;
16855 if (ENABLE_OFFLOADING)
16857 g->have_offload = true;
16858 if (is_a <varpool_node *> (node))
16859 vec_safe_push (offload_vars, t);
16866 static void
16867 c_parser_omp_end_declare_target (c_parser *parser)
16869 location_t loc = c_parser_peek_token (parser)->location;
16870 c_parser_consume_pragma (parser);
16871 if (c_parser_next_token_is (parser, CPP_NAME)
16872 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16873 "declare") == 0)
16875 c_parser_consume_token (parser);
16876 if (c_parser_next_token_is (parser, CPP_NAME)
16877 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16878 "target") == 0)
16879 c_parser_consume_token (parser);
16880 else
16882 c_parser_error (parser, "expected %<target%>");
16883 c_parser_skip_to_pragma_eol (parser);
16884 return;
16887 else
16889 c_parser_error (parser, "expected %<declare%>");
16890 c_parser_skip_to_pragma_eol (parser);
16891 return;
16893 c_parser_skip_to_pragma_eol (parser);
16894 if (!current_omp_declare_target_attribute)
16895 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16896 "%<#pragma omp declare target%>");
16897 else
16898 current_omp_declare_target_attribute--;
16902 /* OpenMP 4.0
16903 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16904 initializer-clause[opt] new-line
16906 initializer-clause:
16907 initializer (omp_priv = initializer)
16908 initializer (function-name (argument-list)) */
16910 static void
16911 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16913 unsigned int tokens_avail = 0, i;
16914 vec<tree> types = vNULL;
16915 vec<c_token> clauses = vNULL;
16916 enum tree_code reduc_code = ERROR_MARK;
16917 tree reduc_id = NULL_TREE;
16918 tree type;
16919 location_t rloc = c_parser_peek_token (parser)->location;
16921 if (context == pragma_struct || context == pragma_param)
16923 error ("%<#pragma omp declare reduction%> not at file or block scope");
16924 goto fail;
16927 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16928 goto fail;
16930 switch (c_parser_peek_token (parser)->type)
16932 case CPP_PLUS:
16933 reduc_code = PLUS_EXPR;
16934 break;
16935 case CPP_MULT:
16936 reduc_code = MULT_EXPR;
16937 break;
16938 case CPP_MINUS:
16939 reduc_code = MINUS_EXPR;
16940 break;
16941 case CPP_AND:
16942 reduc_code = BIT_AND_EXPR;
16943 break;
16944 case CPP_XOR:
16945 reduc_code = BIT_XOR_EXPR;
16946 break;
16947 case CPP_OR:
16948 reduc_code = BIT_IOR_EXPR;
16949 break;
16950 case CPP_AND_AND:
16951 reduc_code = TRUTH_ANDIF_EXPR;
16952 break;
16953 case CPP_OR_OR:
16954 reduc_code = TRUTH_ORIF_EXPR;
16955 break;
16956 case CPP_NAME:
16957 const char *p;
16958 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16959 if (strcmp (p, "min") == 0)
16961 reduc_code = MIN_EXPR;
16962 break;
16964 if (strcmp (p, "max") == 0)
16966 reduc_code = MAX_EXPR;
16967 break;
16969 reduc_id = c_parser_peek_token (parser)->value;
16970 break;
16971 default:
16972 c_parser_error (parser,
16973 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16974 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16975 goto fail;
16978 tree orig_reduc_id, reduc_decl;
16979 orig_reduc_id = reduc_id;
16980 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16981 reduc_decl = c_omp_reduction_decl (reduc_id);
16982 c_parser_consume_token (parser);
16984 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16985 goto fail;
16987 while (true)
16989 location_t loc = c_parser_peek_token (parser)->location;
16990 struct c_type_name *ctype = c_parser_type_name (parser);
16991 if (ctype != NULL)
16993 type = groktypename (ctype, NULL, NULL);
16994 if (type == error_mark_node)
16996 else if ((INTEGRAL_TYPE_P (type)
16997 || TREE_CODE (type) == REAL_TYPE
16998 || TREE_CODE (type) == COMPLEX_TYPE)
16999 && orig_reduc_id == NULL_TREE)
17000 error_at (loc, "predeclared arithmetic type in "
17001 "%<#pragma omp declare reduction%>");
17002 else if (TREE_CODE (type) == FUNCTION_TYPE
17003 || TREE_CODE (type) == ARRAY_TYPE)
17004 error_at (loc, "function or array type in "
17005 "%<#pragma omp declare reduction%>");
17006 else if (TYPE_ATOMIC (type))
17007 error_at (loc, "%<_Atomic%> qualified type in "
17008 "%<#pragma omp declare reduction%>");
17009 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17010 error_at (loc, "const, volatile or restrict qualified type in "
17011 "%<#pragma omp declare reduction%>");
17012 else
17014 tree t;
17015 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17016 if (comptypes (TREE_PURPOSE (t), type))
17018 error_at (loc, "redeclaration of %qs "
17019 "%<#pragma omp declare reduction%> for "
17020 "type %qT",
17021 IDENTIFIER_POINTER (reduc_id)
17022 + sizeof ("omp declare reduction ") - 1,
17023 type);
17024 location_t ploc
17025 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17026 0));
17027 error_at (ploc, "previous %<#pragma omp declare "
17028 "reduction%>");
17029 break;
17031 if (t == NULL_TREE)
17032 types.safe_push (type);
17034 if (c_parser_next_token_is (parser, CPP_COMMA))
17035 c_parser_consume_token (parser);
17036 else
17037 break;
17039 else
17040 break;
17043 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17044 || types.is_empty ())
17046 fail:
17047 clauses.release ();
17048 types.release ();
17049 while (true)
17051 c_token *token = c_parser_peek_token (parser);
17052 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17053 break;
17054 c_parser_consume_token (parser);
17056 c_parser_skip_to_pragma_eol (parser);
17057 return;
17060 if (types.length () > 1)
17062 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17064 c_token *token = c_parser_peek_token (parser);
17065 if (token->type == CPP_EOF)
17066 goto fail;
17067 clauses.safe_push (*token);
17068 c_parser_consume_token (parser);
17070 clauses.safe_push (*c_parser_peek_token (parser));
17071 c_parser_skip_to_pragma_eol (parser);
17073 /* Make sure nothing tries to read past the end of the tokens. */
17074 c_token eof_token;
17075 memset (&eof_token, 0, sizeof (eof_token));
17076 eof_token.type = CPP_EOF;
17077 clauses.safe_push (eof_token);
17078 clauses.safe_push (eof_token);
17081 int errs = errorcount;
17082 FOR_EACH_VEC_ELT (types, i, type)
17084 tokens_avail = parser->tokens_avail;
17085 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17086 if (!clauses.is_empty ())
17088 parser->tokens = clauses.address ();
17089 parser->tokens_avail = clauses.length ();
17090 parser->in_pragma = true;
17093 bool nested = current_function_decl != NULL_TREE;
17094 if (nested)
17095 c_push_function_context ();
17096 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17097 reduc_id, default_function_type);
17098 current_function_decl = fndecl;
17099 allocate_struct_function (fndecl, true);
17100 push_scope ();
17101 tree stmt = push_stmt_list ();
17102 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17103 warn about these. */
17104 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17105 get_identifier ("omp_out"), type);
17106 DECL_ARTIFICIAL (omp_out) = 1;
17107 DECL_CONTEXT (omp_out) = fndecl;
17108 pushdecl (omp_out);
17109 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17110 get_identifier ("omp_in"), type);
17111 DECL_ARTIFICIAL (omp_in) = 1;
17112 DECL_CONTEXT (omp_in) = fndecl;
17113 pushdecl (omp_in);
17114 struct c_expr combiner = c_parser_expression (parser);
17115 struct c_expr initializer;
17116 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17117 bool bad = false;
17118 initializer.value = error_mark_node;
17119 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17120 bad = true;
17121 else if (c_parser_next_token_is (parser, CPP_NAME)
17122 && strcmp (IDENTIFIER_POINTER
17123 (c_parser_peek_token (parser)->value),
17124 "initializer") == 0)
17126 c_parser_consume_token (parser);
17127 pop_scope ();
17128 push_scope ();
17129 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17130 get_identifier ("omp_priv"), type);
17131 DECL_ARTIFICIAL (omp_priv) = 1;
17132 DECL_INITIAL (omp_priv) = error_mark_node;
17133 DECL_CONTEXT (omp_priv) = fndecl;
17134 pushdecl (omp_priv);
17135 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17136 get_identifier ("omp_orig"), type);
17137 DECL_ARTIFICIAL (omp_orig) = 1;
17138 DECL_CONTEXT (omp_orig) = fndecl;
17139 pushdecl (omp_orig);
17140 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17141 bad = true;
17142 else if (!c_parser_next_token_is (parser, CPP_NAME))
17144 c_parser_error (parser, "expected %<omp_priv%> or "
17145 "function-name");
17146 bad = true;
17148 else if (strcmp (IDENTIFIER_POINTER
17149 (c_parser_peek_token (parser)->value),
17150 "omp_priv") != 0)
17152 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17153 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17155 c_parser_error (parser, "expected function-name %<(%>");
17156 bad = true;
17158 else
17159 initializer = c_parser_postfix_expression (parser);
17160 if (initializer.value
17161 && TREE_CODE (initializer.value) == CALL_EXPR)
17163 int j;
17164 tree c = initializer.value;
17165 for (j = 0; j < call_expr_nargs (c); j++)
17167 tree a = CALL_EXPR_ARG (c, j);
17168 STRIP_NOPS (a);
17169 if (TREE_CODE (a) == ADDR_EXPR
17170 && TREE_OPERAND (a, 0) == omp_priv)
17171 break;
17173 if (j == call_expr_nargs (c))
17174 error ("one of the initializer call arguments should be "
17175 "%<&omp_priv%>");
17178 else
17180 c_parser_consume_token (parser);
17181 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17182 bad = true;
17183 else
17185 tree st = push_stmt_list ();
17186 location_t loc = c_parser_peek_token (parser)->location;
17187 rich_location richloc (line_table, loc);
17188 start_init (omp_priv, NULL_TREE, 0, &richloc);
17189 struct c_expr init = c_parser_initializer (parser);
17190 finish_init ();
17191 finish_decl (omp_priv, loc, init.value,
17192 init.original_type, NULL_TREE);
17193 pop_stmt_list (st);
17196 if (!bad
17197 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17198 bad = true;
17201 if (!bad)
17203 c_parser_skip_to_pragma_eol (parser);
17205 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17206 DECL_INITIAL (reduc_decl));
17207 DECL_INITIAL (reduc_decl) = t;
17208 DECL_SOURCE_LOCATION (omp_out) = rloc;
17209 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17210 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17211 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17212 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17213 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17214 if (omp_priv)
17216 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17217 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17218 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17219 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17220 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17221 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17222 walk_tree (&DECL_INITIAL (omp_priv),
17223 c_check_omp_declare_reduction_r,
17224 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17228 pop_stmt_list (stmt);
17229 pop_scope ();
17230 if (cfun->language != NULL)
17232 ggc_free (cfun->language);
17233 cfun->language = NULL;
17235 set_cfun (NULL);
17236 current_function_decl = NULL_TREE;
17237 if (nested)
17238 c_pop_function_context ();
17240 if (!clauses.is_empty ())
17242 parser->tokens = &parser->tokens_buf[0];
17243 parser->tokens_avail = tokens_avail;
17245 if (bad)
17246 goto fail;
17247 if (errs != errorcount)
17248 break;
17251 clauses.release ();
17252 types.release ();
17256 /* OpenMP 4.0
17257 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17258 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17259 initializer-clause[opt] new-line
17260 #pragma omp declare target new-line */
17262 static void
17263 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17265 c_parser_consume_pragma (parser);
17266 if (c_parser_next_token_is (parser, CPP_NAME))
17268 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17269 if (strcmp (p, "simd") == 0)
17271 /* c_parser_consume_token (parser); done in
17272 c_parser_omp_declare_simd. */
17273 c_parser_omp_declare_simd (parser, context);
17274 return;
17276 if (strcmp (p, "reduction") == 0)
17278 c_parser_consume_token (parser);
17279 c_parser_omp_declare_reduction (parser, context);
17280 return;
17282 if (!flag_openmp) /* flag_openmp_simd */
17284 c_parser_skip_to_pragma_eol (parser, false);
17285 return;
17287 if (strcmp (p, "target") == 0)
17289 c_parser_consume_token (parser);
17290 c_parser_omp_declare_target (parser);
17291 return;
17295 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17296 "or %<target%>");
17297 c_parser_skip_to_pragma_eol (parser);
17300 /* OpenMP 4.5:
17301 #pragma omp taskloop taskloop-clause[optseq] new-line
17302 for-loop
17304 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17305 for-loop */
17307 #define OMP_TASKLOOP_CLAUSE_MASK \
17308 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17323 static tree
17324 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17325 char *p_name, omp_clause_mask mask, tree *cclauses,
17326 bool *if_p)
17328 tree clauses, block, ret;
17330 strcat (p_name, " taskloop");
17331 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17333 if (c_parser_next_token_is (parser, CPP_NAME))
17335 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17337 if (strcmp (p, "simd") == 0)
17339 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17340 if (cclauses == NULL)
17341 cclauses = cclauses_buf;
17342 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17343 c_parser_consume_token (parser);
17344 if (!flag_openmp) /* flag_openmp_simd */
17345 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17346 if_p);
17347 block = c_begin_compound_stmt (true);
17348 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17349 block = c_end_compound_stmt (loc, block, true);
17350 if (ret == NULL)
17351 return ret;
17352 ret = make_node (OMP_TASKLOOP);
17353 TREE_TYPE (ret) = void_type_node;
17354 OMP_FOR_BODY (ret) = block;
17355 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17356 SET_EXPR_LOCATION (ret, loc);
17357 add_stmt (ret);
17358 return ret;
17361 if (!flag_openmp) /* flag_openmp_simd */
17363 c_parser_skip_to_pragma_eol (parser, false);
17364 return NULL_TREE;
17367 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17368 if (cclauses)
17370 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17371 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17374 block = c_begin_compound_stmt (true);
17375 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17376 block = c_end_compound_stmt (loc, block, true);
17377 add_stmt (block);
17379 return ret;
17382 /* Main entry point to parsing most OpenMP pragmas. */
17384 static void
17385 c_parser_omp_construct (c_parser *parser, bool *if_p)
17387 enum pragma_kind p_kind;
17388 location_t loc;
17389 tree stmt;
17390 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17391 omp_clause_mask mask (0);
17393 loc = c_parser_peek_token (parser)->location;
17394 p_kind = c_parser_peek_token (parser)->pragma_kind;
17395 c_parser_consume_pragma (parser);
17397 switch (p_kind)
17399 case PRAGMA_OACC_ATOMIC:
17400 c_parser_omp_atomic (loc, parser);
17401 return;
17402 case PRAGMA_OACC_CACHE:
17403 strcpy (p_name, "#pragma acc");
17404 stmt = c_parser_oacc_cache (loc, parser);
17405 break;
17406 case PRAGMA_OACC_DATA:
17407 stmt = c_parser_oacc_data (loc, parser, if_p);
17408 break;
17409 case PRAGMA_OACC_HOST_DATA:
17410 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17411 break;
17412 case PRAGMA_OACC_KERNELS:
17413 case PRAGMA_OACC_PARALLEL:
17414 strcpy (p_name, "#pragma acc");
17415 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17416 if_p);
17417 break;
17418 case PRAGMA_OACC_LOOP:
17419 strcpy (p_name, "#pragma acc");
17420 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17421 break;
17422 case PRAGMA_OACC_WAIT:
17423 strcpy (p_name, "#pragma wait");
17424 stmt = c_parser_oacc_wait (loc, parser, p_name);
17425 break;
17426 case PRAGMA_OMP_ATOMIC:
17427 c_parser_omp_atomic (loc, parser);
17428 return;
17429 case PRAGMA_OMP_CRITICAL:
17430 stmt = c_parser_omp_critical (loc, parser, if_p);
17431 break;
17432 case PRAGMA_OMP_DISTRIBUTE:
17433 strcpy (p_name, "#pragma omp");
17434 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17435 break;
17436 case PRAGMA_OMP_FOR:
17437 strcpy (p_name, "#pragma omp");
17438 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17439 break;
17440 case PRAGMA_OMP_MASTER:
17441 stmt = c_parser_omp_master (loc, parser, if_p);
17442 break;
17443 case PRAGMA_OMP_PARALLEL:
17444 strcpy (p_name, "#pragma omp");
17445 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17446 break;
17447 case PRAGMA_OMP_SECTIONS:
17448 strcpy (p_name, "#pragma omp");
17449 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17450 break;
17451 case PRAGMA_OMP_SIMD:
17452 strcpy (p_name, "#pragma omp");
17453 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17454 break;
17455 case PRAGMA_OMP_SINGLE:
17456 stmt = c_parser_omp_single (loc, parser, if_p);
17457 break;
17458 case PRAGMA_OMP_TASK:
17459 stmt = c_parser_omp_task (loc, parser, if_p);
17460 break;
17461 case PRAGMA_OMP_TASKGROUP:
17462 stmt = c_parser_omp_taskgroup (parser, if_p);
17463 break;
17464 case PRAGMA_OMP_TASKLOOP:
17465 strcpy (p_name, "#pragma omp");
17466 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17467 break;
17468 case PRAGMA_OMP_TEAMS:
17469 strcpy (p_name, "#pragma omp");
17470 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17471 break;
17472 default:
17473 gcc_unreachable ();
17476 if (stmt)
17477 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17481 /* OpenMP 2.5:
17482 # pragma omp threadprivate (variable-list) */
17484 static void
17485 c_parser_omp_threadprivate (c_parser *parser)
17487 tree vars, t;
17488 location_t loc;
17490 c_parser_consume_pragma (parser);
17491 loc = c_parser_peek_token (parser)->location;
17492 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17494 /* Mark every variable in VARS to be assigned thread local storage. */
17495 for (t = vars; t; t = TREE_CHAIN (t))
17497 tree v = TREE_PURPOSE (t);
17499 /* FIXME diagnostics: Ideally we should keep individual
17500 locations for all the variables in the var list to make the
17501 following errors more precise. Perhaps
17502 c_parser_omp_var_list_parens() should construct a list of
17503 locations to go along with the var list. */
17505 /* If V had already been marked threadprivate, it doesn't matter
17506 whether it had been used prior to this point. */
17507 if (!VAR_P (v))
17508 error_at (loc, "%qD is not a variable", v);
17509 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17510 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17511 else if (! is_global_var (v))
17512 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17513 else if (TREE_TYPE (v) == error_mark_node)
17515 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17516 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17517 else
17519 if (! DECL_THREAD_LOCAL_P (v))
17521 set_decl_tls_model (v, decl_default_tls_model (v));
17522 /* If rtl has been already set for this var, call
17523 make_decl_rtl once again, so that encode_section_info
17524 has a chance to look at the new decl flags. */
17525 if (DECL_RTL_SET_P (v))
17526 make_decl_rtl (v);
17528 C_DECL_THREADPRIVATE_P (v) = 1;
17532 c_parser_skip_to_pragma_eol (parser);
17535 /* Cilk Plus <#pragma simd> parsing routines. */
17537 /* Helper function for c_parser_pragma. Perform some sanity checking
17538 for <#pragma simd> constructs. Returns FALSE if there was a
17539 problem. */
17541 static bool
17542 c_parser_cilk_verify_simd (c_parser *parser,
17543 enum pragma_context context)
17545 if (!flag_cilkplus)
17547 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17548 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17549 return false;
17551 if (context == pragma_external)
17553 c_parser_error (parser,"pragma simd must be inside a function");
17554 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17555 return false;
17557 return true;
17560 /* Cilk Plus:
17561 This function is shared by SIMD-enabled functions and #pragma simd.
17562 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17563 CLAUSES is unused. The main purpose of this function is to parse a
17564 vectorlength attribute or clause and check for parse errors.
17565 When IS_SIMD_FN is true then the function is merely caching the tokens
17566 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17567 cache is cleared since there is no reason to continue.
17568 Syntax:
17569 vectorlength ( constant-expression ) */
17571 static tree
17572 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17573 bool is_simd_fn)
17575 if (is_simd_fn)
17576 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17577 else
17578 /* The vectorlength clause behaves exactly like OpenMP's safelen
17579 clause. Represent it in OpenMP terms. */
17580 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17582 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17583 return clauses;
17585 location_t loc = c_parser_peek_token (parser)->location;
17586 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17587 expr = c_fully_fold (expr, false, NULL);
17589 /* If expr is an error_mark_node then the above function would have
17590 emitted an error. No reason to do it twice. */
17591 if (expr == error_mark_node)
17593 else if (!TREE_TYPE (expr)
17594 || !TREE_CONSTANT (expr)
17595 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17597 error_at (loc, "vectorlength must be an integer constant");
17598 else if (wi::exact_log2 (expr) == -1)
17599 error_at (loc, "vectorlength must be a power of 2");
17600 else
17602 if (is_simd_fn)
17604 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17605 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17606 OMP_CLAUSE_CHAIN (u) = clauses;
17607 clauses = u;
17609 else
17611 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17612 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17613 OMP_CLAUSE_CHAIN (u) = clauses;
17614 clauses = u;
17618 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17620 return clauses;
17623 /* Cilk Plus:
17624 linear ( simd-linear-variable-list )
17626 simd-linear-variable-list:
17627 simd-linear-variable
17628 simd-linear-variable-list , simd-linear-variable
17630 simd-linear-variable:
17631 id-expression
17632 id-expression : simd-linear-step
17634 simd-linear-step:
17635 conditional-expression */
17637 static tree
17638 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17640 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17641 return clauses;
17643 location_t loc = c_parser_peek_token (parser)->location;
17645 if (c_parser_next_token_is_not (parser, CPP_NAME)
17646 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17647 c_parser_error (parser, "expected identifier");
17649 while (c_parser_next_token_is (parser, CPP_NAME)
17650 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17652 tree var = lookup_name (c_parser_peek_token (parser)->value);
17654 if (var == NULL)
17656 undeclared_variable (c_parser_peek_token (parser)->location,
17657 c_parser_peek_token (parser)->value);
17658 c_parser_consume_token (parser);
17660 else if (var == error_mark_node)
17661 c_parser_consume_token (parser);
17662 else
17664 tree step = integer_one_node;
17666 /* Parse the linear step if present. */
17667 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17669 c_parser_consume_token (parser);
17670 c_parser_consume_token (parser);
17672 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17673 expr = c_fully_fold (expr, false, NULL);
17675 if (TREE_TYPE (expr)
17676 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17677 && (TREE_CONSTANT (expr)
17678 || DECL_P (expr)))
17679 step = expr;
17680 else
17681 c_parser_error (parser,
17682 "step size must be an integer constant "
17683 "expression or an integer variable");
17685 else
17686 c_parser_consume_token (parser);
17688 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17689 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17690 OMP_CLAUSE_DECL (u) = var;
17691 OMP_CLAUSE_LINEAR_STEP (u) = step;
17692 OMP_CLAUSE_CHAIN (u) = clauses;
17693 clauses = u;
17696 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17697 break;
17699 c_parser_consume_token (parser);
17702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17704 return clauses;
17707 /* Returns the name of the next clause. If the clause is not
17708 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17709 not consumed. Otherwise, the appropriate pragma_simd_clause is
17710 returned and the token is consumed. */
17712 static pragma_omp_clause
17713 c_parser_cilk_clause_name (c_parser *parser)
17715 pragma_omp_clause result;
17716 c_token *token = c_parser_peek_token (parser);
17718 if (!token->value || token->type != CPP_NAME)
17719 return PRAGMA_CILK_CLAUSE_NONE;
17721 const char *p = IDENTIFIER_POINTER (token->value);
17723 if (!strcmp (p, "vectorlength"))
17724 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17725 else if (!strcmp (p, "linear"))
17726 result = PRAGMA_CILK_CLAUSE_LINEAR;
17727 else if (!strcmp (p, "private"))
17728 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17729 else if (!strcmp (p, "firstprivate"))
17730 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17731 else if (!strcmp (p, "lastprivate"))
17732 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17733 else if (!strcmp (p, "reduction"))
17734 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17735 else
17736 return PRAGMA_CILK_CLAUSE_NONE;
17738 c_parser_consume_token (parser);
17739 return result;
17742 /* Parse all #<pragma simd> clauses. Return the list of clauses
17743 found. */
17745 static tree
17746 c_parser_cilk_all_clauses (c_parser *parser)
17748 tree clauses = NULL;
17750 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17752 pragma_omp_clause c_kind;
17754 c_kind = c_parser_cilk_clause_name (parser);
17756 switch (c_kind)
17758 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17759 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17760 break;
17761 case PRAGMA_CILK_CLAUSE_LINEAR:
17762 clauses = c_parser_cilk_clause_linear (parser, clauses);
17763 break;
17764 case PRAGMA_CILK_CLAUSE_PRIVATE:
17765 /* Use the OpenMP counterpart. */
17766 clauses = c_parser_omp_clause_private (parser, clauses);
17767 break;
17768 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17769 /* Use the OpenMP counterpart. */
17770 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17771 break;
17772 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17773 /* Use the OpenMP counterpart. */
17774 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17775 break;
17776 case PRAGMA_CILK_CLAUSE_REDUCTION:
17777 /* Use the OpenMP counterpart. */
17778 clauses = c_parser_omp_clause_reduction (parser, clauses);
17779 break;
17780 default:
17781 c_parser_error (parser, "expected %<#pragma simd%> clause");
17782 goto saw_error;
17786 saw_error:
17787 c_parser_skip_to_pragma_eol (parser);
17788 return c_finish_omp_clauses (clauses, C_ORT_CILK);
17791 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17792 Here is the correct syntax of this pragma:
17793 #pragma cilk grainsize = <EXP>
17796 static void
17797 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
17799 extern tree convert_to_integer (tree, tree);
17801 /* consume the 'grainsize' keyword. */
17802 c_parser_consume_pragma (parser);
17804 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17806 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17807 if (g_expr.value == error_mark_node)
17809 c_parser_skip_to_pragma_eol (parser);
17810 return;
17812 tree grain = convert_to_integer (long_integer_type_node,
17813 c_fully_fold (g_expr.value, false,
17814 NULL));
17815 c_parser_skip_to_pragma_eol (parser);
17816 c_token *token = c_parser_peek_token (parser);
17817 if (token && token->type == CPP_KEYWORD
17818 && token->keyword == RID_CILK_FOR)
17820 if (grain == NULL_TREE || grain == error_mark_node)
17821 grain = integer_zero_node;
17822 c_parser_cilk_for (parser, grain, if_p);
17824 else
17825 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17826 "%<_Cilk_for%>");
17828 else
17829 c_parser_skip_to_pragma_eol (parser);
17832 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17834 static void
17835 c_parser_cilk_simd (c_parser *parser, bool *if_p)
17837 tree clauses = c_parser_cilk_all_clauses (parser);
17838 tree block = c_begin_compound_stmt (true);
17839 location_t loc = c_parser_peek_token (parser)->location;
17840 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
17841 block = c_end_compound_stmt (loc, block, true);
17842 add_stmt (block);
17845 /* Create an artificial decl with TYPE and emit initialization of it with
17846 INIT. */
17848 static tree
17849 c_get_temp_regvar (tree type, tree init)
17851 location_t loc = EXPR_LOCATION (init);
17852 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17853 DECL_ARTIFICIAL (decl) = 1;
17854 DECL_IGNORED_P (decl) = 1;
17855 pushdecl (decl);
17856 tree t = build2 (INIT_EXPR, type, decl, init);
17857 add_stmt (t);
17858 return decl;
17861 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17862 GRAIN is the grain value passed in through pragma or 0. */
17864 static void
17865 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
17867 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17868 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17869 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17870 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
17872 tree block = c_begin_compound_stmt (true);
17873 tree sb = push_stmt_list ();
17874 location_t loc = c_parser_peek_token (parser)->location;
17875 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
17876 if_p);
17877 sb = pop_stmt_list (sb);
17879 if (omp_for)
17881 tree omp_par = make_node (OMP_PARALLEL);
17882 TREE_TYPE (omp_par) = void_type_node;
17883 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17884 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17885 TREE_SIDE_EFFECTS (bind) = 1;
17886 BIND_EXPR_BODY (bind) = sb;
17887 OMP_PARALLEL_BODY (omp_par) = bind;
17888 if (OMP_FOR_PRE_BODY (omp_for))
17890 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17891 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17893 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17894 tree decl = TREE_OPERAND (init, 0);
17895 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17896 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17897 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17898 if (TREE_CODE (t) != INTEGER_CST)
17900 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17901 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17902 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17903 OMP_CLAUSE_CHAIN (c) = clauses;
17904 clauses = c;
17906 if (TREE_CODE (incr) == MODIFY_EXPR)
17908 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17909 if (TREE_CODE (t) != INTEGER_CST)
17911 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17912 = c_get_temp_regvar (TREE_TYPE (t), t);
17913 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17914 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17915 OMP_CLAUSE_CHAIN (c) = clauses;
17916 clauses = c;
17919 t = TREE_OPERAND (init, 1);
17920 if (TREE_CODE (t) != INTEGER_CST)
17922 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17923 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17924 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17925 OMP_CLAUSE_CHAIN (c) = clauses;
17926 clauses = c;
17928 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17929 OMP_CLAUSE_DECL (c) = decl;
17930 OMP_CLAUSE_CHAIN (c) = clauses;
17931 clauses = c;
17932 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17933 OMP_CLAUSE_OPERAND (c, 0)
17934 = cilk_for_number_of_iterations (omp_for);
17935 OMP_CLAUSE_CHAIN (c) = clauses;
17936 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
17937 add_stmt (omp_par);
17940 block = c_end_compound_stmt (loc, block, true);
17941 add_stmt (block);
17945 /* Parse a transaction attribute (GCC Extension).
17947 transaction-attribute:
17948 attributes
17949 [ [ any-word ] ]
17951 The transactional memory language description is written for C++,
17952 and uses the C++0x attribute syntax. For compatibility, allow the
17953 bracket style for transactions in C as well. */
17955 static tree
17956 c_parser_transaction_attributes (c_parser *parser)
17958 tree attr_name, attr = NULL;
17960 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17961 return c_parser_attributes (parser);
17963 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17964 return NULL_TREE;
17965 c_parser_consume_token (parser);
17966 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17967 goto error1;
17969 attr_name = c_parser_attribute_any_word (parser);
17970 if (attr_name)
17972 c_parser_consume_token (parser);
17973 attr = build_tree_list (attr_name, NULL_TREE);
17975 else
17976 c_parser_error (parser, "expected identifier");
17978 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17979 error1:
17980 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17981 return attr;
17984 /* Parse a __transaction_atomic or __transaction_relaxed statement
17985 (GCC Extension).
17987 transaction-statement:
17988 __transaction_atomic transaction-attribute[opt] compound-statement
17989 __transaction_relaxed compound-statement
17991 Note that the only valid attribute is: "outer".
17994 static tree
17995 c_parser_transaction (c_parser *parser, enum rid keyword)
17997 unsigned int old_in = parser->in_transaction;
17998 unsigned int this_in = 1, new_in;
17999 location_t loc = c_parser_peek_token (parser)->location;
18000 tree stmt, attrs;
18002 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18003 || keyword == RID_TRANSACTION_RELAXED)
18004 && c_parser_next_token_is_keyword (parser, keyword));
18005 c_parser_consume_token (parser);
18007 if (keyword == RID_TRANSACTION_RELAXED)
18008 this_in |= TM_STMT_ATTR_RELAXED;
18009 else
18011 attrs = c_parser_transaction_attributes (parser);
18012 if (attrs)
18013 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18016 /* Keep track if we're in the lexical scope of an outer transaction. */
18017 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18019 parser->in_transaction = new_in;
18020 stmt = c_parser_compound_statement (parser);
18021 parser->in_transaction = old_in;
18023 if (flag_tm)
18024 stmt = c_finish_transaction (loc, stmt, this_in);
18025 else
18026 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18027 "%<__transaction_atomic%> without transactional memory support enabled"
18028 : "%<__transaction_relaxed %> "
18029 "without transactional memory support enabled"));
18031 return stmt;
18034 /* Parse a __transaction_atomic or __transaction_relaxed expression
18035 (GCC Extension).
18037 transaction-expression:
18038 __transaction_atomic ( expression )
18039 __transaction_relaxed ( expression )
18042 static struct c_expr
18043 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18045 struct c_expr ret;
18046 unsigned int old_in = parser->in_transaction;
18047 unsigned int this_in = 1;
18048 location_t loc = c_parser_peek_token (parser)->location;
18049 tree attrs;
18051 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18052 || keyword == RID_TRANSACTION_RELAXED)
18053 && c_parser_next_token_is_keyword (parser, keyword));
18054 c_parser_consume_token (parser);
18056 if (keyword == RID_TRANSACTION_RELAXED)
18057 this_in |= TM_STMT_ATTR_RELAXED;
18058 else
18060 attrs = c_parser_transaction_attributes (parser);
18061 if (attrs)
18062 this_in |= parse_tm_stmt_attr (attrs, 0);
18065 parser->in_transaction = this_in;
18066 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18068 tree expr = c_parser_expression (parser).value;
18069 ret.original_type = TREE_TYPE (expr);
18070 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18071 if (this_in & TM_STMT_ATTR_RELAXED)
18072 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18073 SET_EXPR_LOCATION (ret.value, loc);
18074 ret.original_code = TRANSACTION_EXPR;
18075 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
18077 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18078 goto error;
18081 else
18083 error:
18084 ret.value = error_mark_node;
18085 ret.original_code = ERROR_MARK;
18086 ret.original_type = NULL;
18088 parser->in_transaction = old_in;
18090 if (!flag_tm)
18091 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18092 "%<__transaction_atomic%> without transactional memory support enabled"
18093 : "%<__transaction_relaxed %> "
18094 "without transactional memory support enabled"));
18096 set_c_expr_source_range (&ret, loc, loc);
18098 return ret;
18101 /* Parse a __transaction_cancel statement (GCC Extension).
18103 transaction-cancel-statement:
18104 __transaction_cancel transaction-attribute[opt] ;
18106 Note that the only valid attribute is "outer".
18109 static tree
18110 c_parser_transaction_cancel (c_parser *parser)
18112 location_t loc = c_parser_peek_token (parser)->location;
18113 tree attrs;
18114 bool is_outer = false;
18116 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18117 c_parser_consume_token (parser);
18119 attrs = c_parser_transaction_attributes (parser);
18120 if (attrs)
18121 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18123 if (!flag_tm)
18125 error_at (loc, "%<__transaction_cancel%> without "
18126 "transactional memory support enabled");
18127 goto ret_error;
18129 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18131 error_at (loc, "%<__transaction_cancel%> within a "
18132 "%<__transaction_relaxed%>");
18133 goto ret_error;
18135 else if (is_outer)
18137 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18138 && !is_tm_may_cancel_outer (current_function_decl))
18140 error_at (loc, "outer %<__transaction_cancel%> not "
18141 "within outer %<__transaction_atomic%>");
18142 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18143 goto ret_error;
18146 else if (parser->in_transaction == 0)
18148 error_at (loc, "%<__transaction_cancel%> not within "
18149 "%<__transaction_atomic%>");
18150 goto ret_error;
18153 return add_stmt (build_tm_abort_call (loc, is_outer));
18155 ret_error:
18156 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18159 /* Parse a single source file. */
18161 void
18162 c_parse_file (void)
18164 /* Use local storage to begin. If the first token is a pragma, parse it.
18165 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18166 which will cause garbage collection. */
18167 c_parser tparser;
18169 memset (&tparser, 0, sizeof tparser);
18170 tparser.tokens = &tparser.tokens_buf[0];
18171 the_parser = &tparser;
18173 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18174 c_parser_pragma_pch_preprocess (&tparser);
18176 the_parser = ggc_alloc<c_parser> ();
18177 *the_parser = tparser;
18178 if (tparser.tokens == &tparser.tokens_buf[0])
18179 the_parser->tokens = &the_parser->tokens_buf[0];
18181 /* Initialize EH, if we've been told to do so. */
18182 if (flag_exceptions)
18183 using_eh_for_cleanups ();
18185 c_parser_translation_unit (the_parser);
18186 the_parser = NULL;
18189 /* This function parses Cilk Plus array notation. The starting index is
18190 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
18191 return value of this function is a tree_node called VALUE_TREE of type
18192 ARRAY_NOTATION_REF. */
18194 static tree
18195 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
18196 tree array_value)
18198 c_token *token = NULL;
18199 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
18200 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
18201 tree array_type_domain = NULL_TREE;
18203 if (array_value == error_mark_node || initial_index == error_mark_node)
18205 /* No need to continue. If either of these 2 were true, then an error
18206 must be emitted already. Thus, no need to emit them twice. */
18207 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18208 return error_mark_node;
18211 array_type = TREE_TYPE (array_value);
18212 gcc_assert (array_type);
18213 if (TREE_CODE (array_type) != ARRAY_TYPE
18214 && TREE_CODE (array_type) != POINTER_TYPE)
18216 error_at (loc, "base of array section must be pointer or array type");
18217 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18218 return error_mark_node;
18220 type = TREE_TYPE (array_type);
18221 token = c_parser_peek_token (parser);
18223 if (token->type == CPP_EOF)
18225 c_parser_error (parser, "expected %<:%> or numeral");
18226 return value_tree;
18228 else if (token->type == CPP_COLON)
18230 if (!initial_index)
18232 /* If we are here, then we have a case like this A[:]. */
18233 c_parser_consume_token (parser);
18234 if (TREE_CODE (array_type) == POINTER_TYPE)
18236 error_at (loc, "start-index and length fields necessary for "
18237 "using array notations in pointers");
18238 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18239 return error_mark_node;
18241 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18243 error_at (loc, "array notations cannot be used with function "
18244 "type");
18245 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18246 return error_mark_node;
18248 array_type_domain = TYPE_DOMAIN (array_type);
18250 if (!array_type_domain)
18252 error_at (loc, "start-index and length fields necessary for "
18253 "using array notations in dimensionless arrays");
18254 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18255 return error_mark_node;
18258 start_index = TYPE_MINVAL (array_type_domain);
18259 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
18260 start_index);
18261 if (!TYPE_MAXVAL (array_type_domain)
18262 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
18264 error_at (loc, "start-index and length fields necessary for "
18265 "using array notations in variable-length arrays");
18266 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18267 return error_mark_node;
18269 end_index = TYPE_MAXVAL (array_type_domain);
18270 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18271 end_index, integer_one_node);
18272 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18273 stride = build_int_cst (integer_type_node, 1);
18274 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18276 else if (initial_index != error_mark_node)
18278 /* If we are here, then there should be 2 possibilities:
18279 1. Array [EXPR : EXPR]
18280 2. Array [EXPR : EXPR : EXPR]
18282 start_index = initial_index;
18284 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18286 error_at (loc, "array notations cannot be used with function "
18287 "type");
18288 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18289 return error_mark_node;
18291 c_parser_consume_token (parser); /* consume the ':' */
18292 struct c_expr ce = c_parser_expression (parser);
18293 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18294 end_index = ce.value;
18295 if (!end_index || end_index == error_mark_node)
18297 c_parser_skip_to_end_of_block_or_statement (parser);
18298 return error_mark_node;
18300 if (c_parser_peek_token (parser)->type == CPP_COLON)
18302 c_parser_consume_token (parser);
18303 ce = c_parser_expression (parser);
18304 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18305 stride = ce.value;
18306 if (!stride || stride == error_mark_node)
18308 c_parser_skip_to_end_of_block_or_statement (parser);
18309 return error_mark_node;
18313 else
18314 c_parser_error (parser, "expected array notation expression");
18316 else
18317 c_parser_error (parser, "expected array notation expression");
18319 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18321 value_tree = build_array_notation_ref (loc, array_value, start_index,
18322 end_index, stride, type);
18323 if (value_tree != error_mark_node)
18324 SET_EXPR_LOCATION (value_tree, loc);
18325 return value_tree;
18328 /* Parse the body of a function declaration marked with "__RTL".
18330 The RTL parser works on the level of characters read from a
18331 FILE *, whereas c_parser works at the level of tokens.
18332 Square this circle by consuming all of the tokens up to and
18333 including the closing brace, recording the start/end of the RTL
18334 fragment, and reopening the file and re-reading the relevant
18335 lines within the RTL parser.
18337 This requires the opening and closing braces of the C function
18338 to be on separate lines from the RTL they wrap.
18340 Take ownership of START_WITH_PASS, if non-NULL. */
18342 void
18343 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18345 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18347 free (start_with_pass);
18348 return;
18351 location_t start_loc = c_parser_peek_token (parser)->location;
18353 /* Consume all tokens, up to the closing brace, handling
18354 matching pairs of braces in the rtl dump. */
18355 int num_open_braces = 1;
18356 while (1)
18358 switch (c_parser_peek_token (parser)->type)
18360 case CPP_OPEN_BRACE:
18361 num_open_braces++;
18362 break;
18363 case CPP_CLOSE_BRACE:
18364 if (--num_open_braces == 0)
18365 goto found_closing_brace;
18366 break;
18367 case CPP_EOF:
18368 error_at (start_loc, "no closing brace");
18369 free (start_with_pass);
18370 return;
18371 default:
18372 break;
18374 c_parser_consume_token (parser);
18377 found_closing_brace:
18378 /* At the closing brace; record its location. */
18379 location_t end_loc = c_parser_peek_token (parser)->location;
18381 /* Consume the closing brace. */
18382 c_parser_consume_token (parser);
18384 /* Invoke the RTL parser. */
18385 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18387 free (start_with_pass);
18388 return;
18391 /* If a pass name was provided for START_WITH_PASS, run the backend
18392 accordingly now, on the cfun created above, transferring
18393 ownership of START_WITH_PASS. */
18394 if (start_with_pass)
18395 run_rtl_passes (start_with_pass);
18398 #include "gt-c-c-parser.h"