2017-02-17 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / c / c-parser.c
blob968c1dc7696946a0760f0ca10a2c5586d291f8cf
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");
11026 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11028 loc = c_parser_peek_token (parser)->location;
11029 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11031 num = c_parser_expr_no_commas (parser, NULL).value;
11032 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11034 if (num == error_mark_node)
11035 return list;
11036 mark_exp_read (num);
11037 num = c_fully_fold (num, false, NULL);
11038 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11039 || !tree_fits_shwi_p (num)
11040 || (n = tree_to_shwi (num)) <= 0
11041 || (int) n != n)
11043 error_at (loc,
11044 "collapse argument needs positive constant integer expression");
11045 return list;
11047 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11048 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11049 OMP_CLAUSE_CHAIN (c) = list;
11050 return c;
11053 /* OpenMP 2.5:
11054 copyin ( variable-list ) */
11056 static tree
11057 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11059 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11062 /* OpenMP 2.5:
11063 copyprivate ( variable-list ) */
11065 static tree
11066 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11068 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11071 /* OpenMP 2.5:
11072 default ( shared | none )
11074 OpenACC 2.0:
11075 default (none) */
11077 static tree
11078 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11080 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11081 location_t loc = c_parser_peek_token (parser)->location;
11082 tree c;
11084 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11085 return list;
11086 if (c_parser_next_token_is (parser, CPP_NAME))
11088 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11090 switch (p[0])
11092 case 'n':
11093 if (strcmp ("none", p) != 0)
11094 goto invalid_kind;
11095 kind = OMP_CLAUSE_DEFAULT_NONE;
11096 break;
11098 case 's':
11099 if (strcmp ("shared", p) != 0 || is_oacc)
11100 goto invalid_kind;
11101 kind = OMP_CLAUSE_DEFAULT_SHARED;
11102 break;
11104 default:
11105 goto invalid_kind;
11108 c_parser_consume_token (parser);
11110 else
11112 invalid_kind:
11113 if (is_oacc)
11114 c_parser_error (parser, "expected %<none%>");
11115 else
11116 c_parser_error (parser, "expected %<none%> or %<shared%>");
11118 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11120 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11121 return list;
11123 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11124 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11125 OMP_CLAUSE_CHAIN (c) = list;
11126 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11128 return c;
11131 /* OpenMP 2.5:
11132 firstprivate ( variable-list ) */
11134 static tree
11135 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11137 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11140 /* OpenMP 3.1:
11141 final ( expression ) */
11143 static tree
11144 c_parser_omp_clause_final (c_parser *parser, tree list)
11146 location_t loc = c_parser_peek_token (parser)->location;
11147 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11149 tree t = c_parser_paren_condition (parser);
11150 tree c;
11152 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11154 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11155 OMP_CLAUSE_FINAL_EXPR (c) = t;
11156 OMP_CLAUSE_CHAIN (c) = list;
11157 list = c;
11159 else
11160 c_parser_error (parser, "expected %<(%>");
11162 return list;
11165 /* OpenACC, OpenMP 2.5:
11166 if ( expression )
11168 OpenMP 4.5:
11169 if ( directive-name-modifier : expression )
11171 directive-name-modifier:
11172 parallel | task | taskloop | target data | target | target update
11173 | target enter data | target exit data */
11175 static tree
11176 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11178 location_t location = c_parser_peek_token (parser)->location;
11179 enum tree_code if_modifier = ERROR_MARK;
11181 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11182 return list;
11184 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11186 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11187 int n = 2;
11188 if (strcmp (p, "parallel") == 0)
11189 if_modifier = OMP_PARALLEL;
11190 else if (strcmp (p, "task") == 0)
11191 if_modifier = OMP_TASK;
11192 else if (strcmp (p, "taskloop") == 0)
11193 if_modifier = OMP_TASKLOOP;
11194 else if (strcmp (p, "target") == 0)
11196 if_modifier = OMP_TARGET;
11197 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11199 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11200 if (strcmp ("data", p) == 0)
11201 if_modifier = OMP_TARGET_DATA;
11202 else if (strcmp ("update", p) == 0)
11203 if_modifier = OMP_TARGET_UPDATE;
11204 else if (strcmp ("enter", p) == 0)
11205 if_modifier = OMP_TARGET_ENTER_DATA;
11206 else if (strcmp ("exit", p) == 0)
11207 if_modifier = OMP_TARGET_EXIT_DATA;
11208 if (if_modifier != OMP_TARGET)
11210 n = 3;
11211 c_parser_consume_token (parser);
11213 else
11215 location_t loc = c_parser_peek_2nd_token (parser)->location;
11216 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11217 "or %<exit%>");
11218 if_modifier = ERROR_MARK;
11220 if (if_modifier == OMP_TARGET_ENTER_DATA
11221 || if_modifier == OMP_TARGET_EXIT_DATA)
11223 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11225 p = IDENTIFIER_POINTER
11226 (c_parser_peek_2nd_token (parser)->value);
11227 if (strcmp ("data", p) == 0)
11228 n = 4;
11230 if (n == 4)
11231 c_parser_consume_token (parser);
11232 else
11234 location_t loc
11235 = c_parser_peek_2nd_token (parser)->location;
11236 error_at (loc, "expected %<data%>");
11237 if_modifier = ERROR_MARK;
11242 if (if_modifier != ERROR_MARK)
11244 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11246 c_parser_consume_token (parser);
11247 c_parser_consume_token (parser);
11249 else
11251 if (n > 2)
11253 location_t loc = c_parser_peek_2nd_token (parser)->location;
11254 error_at (loc, "expected %<:%>");
11256 if_modifier = ERROR_MARK;
11261 tree t = c_parser_condition (parser), c;
11262 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11264 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11265 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11267 if (if_modifier != ERROR_MARK
11268 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11270 const char *p = NULL;
11271 switch (if_modifier)
11273 case OMP_PARALLEL: p = "parallel"; break;
11274 case OMP_TASK: p = "task"; break;
11275 case OMP_TASKLOOP: p = "taskloop"; break;
11276 case OMP_TARGET_DATA: p = "target data"; break;
11277 case OMP_TARGET: p = "target"; break;
11278 case OMP_TARGET_UPDATE: p = "target update"; break;
11279 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11280 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11281 default: gcc_unreachable ();
11283 error_at (location, "too many %<if%> clauses with %qs modifier",
11285 return list;
11287 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11289 if (!is_omp)
11290 error_at (location, "too many %<if%> clauses");
11291 else
11292 error_at (location, "too many %<if%> clauses without modifier");
11293 return list;
11295 else if (if_modifier == ERROR_MARK
11296 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11298 error_at (location, "if any %<if%> clause has modifier, then all "
11299 "%<if%> clauses have to use modifier");
11300 return list;
11304 c = build_omp_clause (location, OMP_CLAUSE_IF);
11305 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11306 OMP_CLAUSE_IF_EXPR (c) = t;
11307 OMP_CLAUSE_CHAIN (c) = list;
11308 return c;
11311 /* OpenMP 2.5:
11312 lastprivate ( variable-list ) */
11314 static tree
11315 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11317 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11320 /* OpenMP 3.1:
11321 mergeable */
11323 static tree
11324 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11326 tree c;
11328 /* FIXME: Should we allow duplicates? */
11329 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11331 c = build_omp_clause (c_parser_peek_token (parser)->location,
11332 OMP_CLAUSE_MERGEABLE);
11333 OMP_CLAUSE_CHAIN (c) = list;
11335 return c;
11338 /* OpenMP 2.5:
11339 nowait */
11341 static tree
11342 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11344 tree c;
11345 location_t loc = c_parser_peek_token (parser)->location;
11347 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11349 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11350 OMP_CLAUSE_CHAIN (c) = list;
11351 return c;
11354 /* OpenACC:
11355 num_gangs ( expression ) */
11357 static tree
11358 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11360 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11361 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11363 location_t expr_loc = c_parser_peek_token (parser)->location;
11364 c_expr expr = c_parser_expression (parser);
11365 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11366 tree c, t = expr.value;
11367 t = c_fully_fold (t, false, NULL);
11369 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11371 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11373 c_parser_error (parser, "expected integer expression");
11374 return list;
11377 /* Attempt to statically determine when the number isn't positive. */
11378 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11379 build_int_cst (TREE_TYPE (t), 0));
11380 protected_set_expr_location (c, expr_loc);
11381 if (c == boolean_true_node)
11383 warning_at (expr_loc, 0,
11384 "%<num_gangs%> value must be positive");
11385 t = integer_one_node;
11388 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11390 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11391 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11392 OMP_CLAUSE_CHAIN (c) = list;
11393 list = c;
11396 return list;
11399 /* OpenMP 2.5:
11400 num_threads ( expression ) */
11402 static tree
11403 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11405 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11406 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11408 location_t expr_loc = c_parser_peek_token (parser)->location;
11409 c_expr expr = c_parser_expression (parser);
11410 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11411 tree c, t = expr.value;
11412 t = c_fully_fold (t, false, NULL);
11414 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11416 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11418 c_parser_error (parser, "expected integer expression");
11419 return list;
11422 /* Attempt to statically determine when the number isn't positive. */
11423 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11424 build_int_cst (TREE_TYPE (t), 0));
11425 protected_set_expr_location (c, expr_loc);
11426 if (c == boolean_true_node)
11428 warning_at (expr_loc, 0,
11429 "%<num_threads%> value must be positive");
11430 t = integer_one_node;
11433 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11435 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11436 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11437 OMP_CLAUSE_CHAIN (c) = list;
11438 list = c;
11441 return list;
11444 /* OpenMP 4.5:
11445 num_tasks ( expression ) */
11447 static tree
11448 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11450 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11451 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11453 location_t expr_loc = c_parser_peek_token (parser)->location;
11454 c_expr expr = c_parser_expression (parser);
11455 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11456 tree c, t = expr.value;
11457 t = c_fully_fold (t, false, NULL);
11459 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11461 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11463 c_parser_error (parser, "expected integer expression");
11464 return list;
11467 /* Attempt to statically determine when the number isn't positive. */
11468 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11469 build_int_cst (TREE_TYPE (t), 0));
11470 if (CAN_HAVE_LOCATION_P (c))
11471 SET_EXPR_LOCATION (c, expr_loc);
11472 if (c == boolean_true_node)
11474 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11475 t = integer_one_node;
11478 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11480 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11481 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11482 OMP_CLAUSE_CHAIN (c) = list;
11483 list = c;
11486 return list;
11489 /* OpenMP 4.5:
11490 grainsize ( expression ) */
11492 static tree
11493 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11495 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11496 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11498 location_t expr_loc = c_parser_peek_token (parser)->location;
11499 c_expr expr = c_parser_expression (parser);
11500 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11501 tree c, t = expr.value;
11502 t = c_fully_fold (t, false, NULL);
11504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11506 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11508 c_parser_error (parser, "expected integer expression");
11509 return list;
11512 /* Attempt to statically determine when the number isn't positive. */
11513 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11514 build_int_cst (TREE_TYPE (t), 0));
11515 if (CAN_HAVE_LOCATION_P (c))
11516 SET_EXPR_LOCATION (c, expr_loc);
11517 if (c == boolean_true_node)
11519 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11520 t = integer_one_node;
11523 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11525 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11526 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11527 OMP_CLAUSE_CHAIN (c) = list;
11528 list = c;
11531 return list;
11534 /* OpenMP 4.5:
11535 priority ( expression ) */
11537 static tree
11538 c_parser_omp_clause_priority (c_parser *parser, tree list)
11540 location_t priority_loc = c_parser_peek_token (parser)->location;
11541 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11543 location_t expr_loc = c_parser_peek_token (parser)->location;
11544 c_expr expr = c_parser_expression (parser);
11545 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11546 tree c, t = expr.value;
11547 t = c_fully_fold (t, false, NULL);
11549 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11551 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11553 c_parser_error (parser, "expected integer expression");
11554 return list;
11557 /* Attempt to statically determine when the number isn't
11558 non-negative. */
11559 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11560 build_int_cst (TREE_TYPE (t), 0));
11561 if (CAN_HAVE_LOCATION_P (c))
11562 SET_EXPR_LOCATION (c, expr_loc);
11563 if (c == boolean_true_node)
11565 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11566 t = integer_one_node;
11569 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11571 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11572 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11573 OMP_CLAUSE_CHAIN (c) = list;
11574 list = c;
11577 return list;
11580 /* OpenMP 4.5:
11581 hint ( expression ) */
11583 static tree
11584 c_parser_omp_clause_hint (c_parser *parser, tree list)
11586 location_t hint_loc = c_parser_peek_token (parser)->location;
11587 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11589 location_t expr_loc = c_parser_peek_token (parser)->location;
11590 c_expr expr = c_parser_expression (parser);
11591 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11592 tree c, t = expr.value;
11593 t = c_fully_fold (t, false, NULL);
11595 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11597 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11599 c_parser_error (parser, "expected integer expression");
11600 return list;
11603 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11605 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11606 OMP_CLAUSE_HINT_EXPR (c) = t;
11607 OMP_CLAUSE_CHAIN (c) = list;
11608 list = c;
11611 return list;
11614 /* OpenMP 4.5:
11615 defaultmap ( tofrom : scalar ) */
11617 static tree
11618 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11620 location_t loc = c_parser_peek_token (parser)->location;
11621 tree c;
11622 const char *p;
11624 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11625 return list;
11626 if (!c_parser_next_token_is (parser, CPP_NAME))
11628 c_parser_error (parser, "expected %<tofrom%>");
11629 goto out_err;
11631 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11632 if (strcmp (p, "tofrom") != 0)
11634 c_parser_error (parser, "expected %<tofrom%>");
11635 goto out_err;
11637 c_parser_consume_token (parser);
11638 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11639 goto out_err;
11640 if (!c_parser_next_token_is (parser, CPP_NAME))
11642 c_parser_error (parser, "expected %<scalar%>");
11643 goto out_err;
11645 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11646 if (strcmp (p, "scalar") != 0)
11648 c_parser_error (parser, "expected %<scalar%>");
11649 goto out_err;
11651 c_parser_consume_token (parser);
11652 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11653 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11654 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11655 OMP_CLAUSE_CHAIN (c) = list;
11656 return c;
11658 out_err:
11659 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11660 return list;
11663 /* OpenACC 2.0:
11664 use_device ( variable-list )
11666 OpenMP 4.5:
11667 use_device_ptr ( variable-list ) */
11669 static tree
11670 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11672 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11673 list);
11676 /* OpenMP 4.5:
11677 is_device_ptr ( variable-list ) */
11679 static tree
11680 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11682 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11685 /* OpenACC:
11686 num_workers ( expression ) */
11688 static tree
11689 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11691 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11692 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11694 location_t expr_loc = c_parser_peek_token (parser)->location;
11695 c_expr expr = c_parser_expression (parser);
11696 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11697 tree c, t = expr.value;
11698 t = c_fully_fold (t, false, NULL);
11700 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11702 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11704 c_parser_error (parser, "expected integer expression");
11705 return list;
11708 /* Attempt to statically determine when the number isn't positive. */
11709 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11710 build_int_cst (TREE_TYPE (t), 0));
11711 protected_set_expr_location (c, expr_loc);
11712 if (c == boolean_true_node)
11714 warning_at (expr_loc, 0,
11715 "%<num_workers%> value must be positive");
11716 t = integer_one_node;
11719 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11721 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11722 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11723 OMP_CLAUSE_CHAIN (c) = list;
11724 list = c;
11727 return list;
11730 /* OpenACC:
11732 gang [( gang-arg-list )]
11733 worker [( [num:] int-expr )]
11734 vector [( [length:] int-expr )]
11736 where gang-arg is one of:
11738 [num:] int-expr
11739 static: size-expr
11741 and size-expr may be:
11744 int-expr
11747 static tree
11748 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11749 const char *str, tree list)
11751 const char *id = "num";
11752 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11753 location_t loc = c_parser_peek_token (parser)->location;
11755 if (kind == OMP_CLAUSE_VECTOR)
11756 id = "length";
11758 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11760 c_parser_consume_token (parser);
11764 c_token *next = c_parser_peek_token (parser);
11765 int idx = 0;
11767 /* Gang static argument. */
11768 if (kind == OMP_CLAUSE_GANG
11769 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11771 c_parser_consume_token (parser);
11773 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11774 goto cleanup_error;
11776 idx = 1;
11777 if (ops[idx] != NULL_TREE)
11779 c_parser_error (parser, "too many %<static%> arguments");
11780 goto cleanup_error;
11783 /* Check for the '*' argument. */
11784 if (c_parser_next_token_is (parser, CPP_MULT)
11785 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11786 || c_parser_peek_2nd_token (parser)->type
11787 == CPP_CLOSE_PAREN))
11789 c_parser_consume_token (parser);
11790 ops[idx] = integer_minus_one_node;
11792 if (c_parser_next_token_is (parser, CPP_COMMA))
11794 c_parser_consume_token (parser);
11795 continue;
11797 else
11798 break;
11801 /* Worker num: argument and vector length: arguments. */
11802 else if (c_parser_next_token_is (parser, CPP_NAME)
11803 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11804 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11806 c_parser_consume_token (parser); /* id */
11807 c_parser_consume_token (parser); /* ':' */
11810 /* Now collect the actual argument. */
11811 if (ops[idx] != NULL_TREE)
11813 c_parser_error (parser, "unexpected argument");
11814 goto cleanup_error;
11817 location_t expr_loc = c_parser_peek_token (parser)->location;
11818 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11819 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11820 tree expr = cexpr.value;
11821 if (expr == error_mark_node)
11822 goto cleanup_error;
11824 expr = c_fully_fold (expr, false, NULL);
11826 /* Attempt to statically determine when the number isn't a
11827 positive integer. */
11829 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11831 c_parser_error (parser, "expected integer expression");
11832 return list;
11835 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11836 build_int_cst (TREE_TYPE (expr), 0));
11837 if (c == boolean_true_node)
11839 warning_at (loc, 0,
11840 "%<%s%> value must be positive", str);
11841 expr = integer_one_node;
11844 ops[idx] = expr;
11846 if (kind == OMP_CLAUSE_GANG
11847 && c_parser_next_token_is (parser, CPP_COMMA))
11849 c_parser_consume_token (parser);
11850 continue;
11852 break;
11854 while (1);
11856 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11857 goto cleanup_error;
11860 check_no_duplicate_clause (list, kind, str);
11862 c = build_omp_clause (loc, kind);
11864 if (ops[1])
11865 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11867 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11868 OMP_CLAUSE_CHAIN (c) = list;
11870 return c;
11872 cleanup_error:
11873 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11874 return list;
11877 /* OpenACC:
11878 auto
11879 independent
11880 nohost
11881 seq */
11883 static tree
11884 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11885 tree list)
11887 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11889 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11890 OMP_CLAUSE_CHAIN (c) = list;
11892 return c;
11895 /* OpenACC:
11896 async [( int-expr )] */
11898 static tree
11899 c_parser_oacc_clause_async (c_parser *parser, tree list)
11901 tree c, t;
11902 location_t loc = c_parser_peek_token (parser)->location;
11904 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11906 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11908 c_parser_consume_token (parser);
11910 t = c_parser_expression (parser).value;
11911 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11912 c_parser_error (parser, "expected integer expression");
11913 else if (t == error_mark_node
11914 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11915 return list;
11917 else
11918 t = c_fully_fold (t, false, NULL);
11920 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11922 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11923 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11924 OMP_CLAUSE_CHAIN (c) = list;
11925 list = c;
11927 return list;
11930 /* OpenACC 2.0:
11931 tile ( size-expr-list ) */
11933 static tree
11934 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11936 tree c, expr = error_mark_node;
11937 location_t loc;
11938 tree tile = NULL_TREE;
11940 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11941 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11943 loc = c_parser_peek_token (parser)->location;
11944 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11945 return list;
11949 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
11950 return list;
11952 if (c_parser_next_token_is (parser, CPP_MULT)
11953 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11954 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11956 c_parser_consume_token (parser);
11957 expr = integer_zero_node;
11959 else
11961 location_t expr_loc = c_parser_peek_token (parser)->location;
11962 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11963 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11964 expr = cexpr.value;
11966 if (expr == error_mark_node)
11968 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11969 "expected %<)%>");
11970 return list;
11973 expr = c_fully_fold (expr, false, NULL);
11975 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
11976 || !tree_fits_shwi_p (expr)
11977 || tree_to_shwi (expr) <= 0)
11979 error_at (expr_loc, "%<tile%> argument needs positive"
11980 " integral constant");
11981 expr = integer_zero_node;
11985 tile = tree_cons (NULL_TREE, expr, tile);
11987 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11989 /* Consume the trailing ')'. */
11990 c_parser_consume_token (parser);
11992 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11993 tile = nreverse (tile);
11994 OMP_CLAUSE_TILE_LIST (c) = tile;
11995 OMP_CLAUSE_CHAIN (c) = list;
11996 return c;
11999 /* OpenACC:
12000 wait ( int-expr-list ) */
12002 static tree
12003 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12005 location_t clause_loc = c_parser_peek_token (parser)->location;
12007 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12008 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12010 return list;
12013 /* OpenMP 2.5:
12014 ordered
12016 OpenMP 4.5:
12017 ordered ( constant-expression ) */
12019 static tree
12020 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12022 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12024 tree c, num = NULL_TREE;
12025 HOST_WIDE_INT n;
12026 location_t loc = c_parser_peek_token (parser)->location;
12027 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12029 c_parser_consume_token (parser);
12030 num = c_parser_expr_no_commas (parser, NULL).value;
12031 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12033 if (num == error_mark_node)
12034 return list;
12035 if (num)
12037 mark_exp_read (num);
12038 num = c_fully_fold (num, false, NULL);
12039 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12040 || !tree_fits_shwi_p (num)
12041 || (n = tree_to_shwi (num)) <= 0
12042 || (int) n != n)
12044 error_at (loc, "ordered argument needs positive "
12045 "constant integer expression");
12046 return list;
12049 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12050 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12051 OMP_CLAUSE_CHAIN (c) = list;
12052 return c;
12055 /* OpenMP 2.5:
12056 private ( variable-list ) */
12058 static tree
12059 c_parser_omp_clause_private (c_parser *parser, tree list)
12061 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12064 /* OpenMP 2.5:
12065 reduction ( reduction-operator : variable-list )
12067 reduction-operator:
12068 One of: + * - & ^ | && ||
12070 OpenMP 3.1:
12072 reduction-operator:
12073 One of: + * - & ^ | && || max min
12075 OpenMP 4.0:
12077 reduction-operator:
12078 One of: + * - & ^ | && ||
12079 identifier */
12081 static tree
12082 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12084 location_t clause_loc = c_parser_peek_token (parser)->location;
12085 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12087 enum tree_code code = ERROR_MARK;
12088 tree reduc_id = NULL_TREE;
12090 switch (c_parser_peek_token (parser)->type)
12092 case CPP_PLUS:
12093 code = PLUS_EXPR;
12094 break;
12095 case CPP_MULT:
12096 code = MULT_EXPR;
12097 break;
12098 case CPP_MINUS:
12099 code = MINUS_EXPR;
12100 break;
12101 case CPP_AND:
12102 code = BIT_AND_EXPR;
12103 break;
12104 case CPP_XOR:
12105 code = BIT_XOR_EXPR;
12106 break;
12107 case CPP_OR:
12108 code = BIT_IOR_EXPR;
12109 break;
12110 case CPP_AND_AND:
12111 code = TRUTH_ANDIF_EXPR;
12112 break;
12113 case CPP_OR_OR:
12114 code = TRUTH_ORIF_EXPR;
12115 break;
12116 case CPP_NAME:
12118 const char *p
12119 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12120 if (strcmp (p, "min") == 0)
12122 code = MIN_EXPR;
12123 break;
12125 if (strcmp (p, "max") == 0)
12127 code = MAX_EXPR;
12128 break;
12130 reduc_id = c_parser_peek_token (parser)->value;
12131 break;
12133 default:
12134 c_parser_error (parser,
12135 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12136 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
12137 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12138 return list;
12140 c_parser_consume_token (parser);
12141 reduc_id = c_omp_reduction_id (code, reduc_id);
12142 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12144 tree nl, c;
12146 nl = c_parser_omp_variable_list (parser, clause_loc,
12147 OMP_CLAUSE_REDUCTION, list);
12148 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12150 tree d = OMP_CLAUSE_DECL (c), type;
12151 if (TREE_CODE (d) != TREE_LIST)
12152 type = TREE_TYPE (d);
12153 else
12155 int cnt = 0;
12156 tree t;
12157 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12158 cnt++;
12159 type = TREE_TYPE (t);
12160 while (cnt > 0)
12162 if (TREE_CODE (type) != POINTER_TYPE
12163 && TREE_CODE (type) != ARRAY_TYPE)
12164 break;
12165 type = TREE_TYPE (type);
12166 cnt--;
12169 while (TREE_CODE (type) == ARRAY_TYPE)
12170 type = TREE_TYPE (type);
12171 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12172 if (code == ERROR_MARK
12173 || !(INTEGRAL_TYPE_P (type)
12174 || TREE_CODE (type) == REAL_TYPE
12175 || TREE_CODE (type) == COMPLEX_TYPE))
12176 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12177 = c_omp_reduction_lookup (reduc_id,
12178 TYPE_MAIN_VARIANT (type));
12181 list = nl;
12183 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12185 return list;
12188 /* OpenMP 2.5:
12189 schedule ( schedule-kind )
12190 schedule ( schedule-kind , expression )
12192 schedule-kind:
12193 static | dynamic | guided | runtime | auto
12195 OpenMP 4.5:
12196 schedule ( schedule-modifier : schedule-kind )
12197 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12199 schedule-modifier:
12200 simd
12201 monotonic
12202 nonmonotonic */
12204 static tree
12205 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12207 tree c, t;
12208 location_t loc = c_parser_peek_token (parser)->location;
12209 int modifiers = 0, nmodifiers = 0;
12211 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12212 return list;
12214 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12216 while (c_parser_next_token_is (parser, CPP_NAME))
12218 tree kind = c_parser_peek_token (parser)->value;
12219 const char *p = IDENTIFIER_POINTER (kind);
12220 if (strcmp ("simd", p) == 0)
12221 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12222 else if (strcmp ("monotonic", p) == 0)
12223 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12224 else if (strcmp ("nonmonotonic", p) == 0)
12225 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12226 else
12227 break;
12228 c_parser_consume_token (parser);
12229 if (nmodifiers++ == 0
12230 && c_parser_next_token_is (parser, CPP_COMMA))
12231 c_parser_consume_token (parser);
12232 else
12234 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12235 break;
12239 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12240 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12241 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12242 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12244 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12245 "specified");
12246 modifiers = 0;
12249 if (c_parser_next_token_is (parser, CPP_NAME))
12251 tree kind = c_parser_peek_token (parser)->value;
12252 const char *p = IDENTIFIER_POINTER (kind);
12254 switch (p[0])
12256 case 'd':
12257 if (strcmp ("dynamic", p) != 0)
12258 goto invalid_kind;
12259 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12260 break;
12262 case 'g':
12263 if (strcmp ("guided", p) != 0)
12264 goto invalid_kind;
12265 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12266 break;
12268 case 'r':
12269 if (strcmp ("runtime", p) != 0)
12270 goto invalid_kind;
12271 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12272 break;
12274 default:
12275 goto invalid_kind;
12278 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12279 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12280 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12281 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12282 else
12283 goto invalid_kind;
12285 c_parser_consume_token (parser);
12286 if (c_parser_next_token_is (parser, CPP_COMMA))
12288 location_t here;
12289 c_parser_consume_token (parser);
12291 here = c_parser_peek_token (parser)->location;
12292 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12293 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12294 t = expr.value;
12295 t = c_fully_fold (t, false, NULL);
12297 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12298 error_at (here, "schedule %<runtime%> does not take "
12299 "a %<chunk_size%> parameter");
12300 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12301 error_at (here,
12302 "schedule %<auto%> does not take "
12303 "a %<chunk_size%> parameter");
12304 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12306 /* Attempt to statically determine when the number isn't
12307 positive. */
12308 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12309 build_int_cst (TREE_TYPE (t), 0));
12310 protected_set_expr_location (s, loc);
12311 if (s == boolean_true_node)
12313 warning_at (loc, 0,
12314 "chunk size value must be positive");
12315 t = integer_one_node;
12317 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12319 else
12320 c_parser_error (parser, "expected integer expression");
12322 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12324 else
12325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12326 "expected %<,%> or %<)%>");
12328 OMP_CLAUSE_SCHEDULE_KIND (c)
12329 = (enum omp_clause_schedule_kind)
12330 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12332 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12333 OMP_CLAUSE_CHAIN (c) = list;
12334 return c;
12336 invalid_kind:
12337 c_parser_error (parser, "invalid schedule kind");
12338 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12339 return list;
12342 /* OpenMP 2.5:
12343 shared ( variable-list ) */
12345 static tree
12346 c_parser_omp_clause_shared (c_parser *parser, tree list)
12348 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12351 /* OpenMP 3.0:
12352 untied */
12354 static tree
12355 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12357 tree c;
12359 /* FIXME: Should we allow duplicates? */
12360 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12362 c = build_omp_clause (c_parser_peek_token (parser)->location,
12363 OMP_CLAUSE_UNTIED);
12364 OMP_CLAUSE_CHAIN (c) = list;
12366 return c;
12369 /* OpenACC:
12370 vector_length ( expression ) */
12372 static tree
12373 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12375 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12376 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12378 location_t expr_loc = c_parser_peek_token (parser)->location;
12379 c_expr expr = c_parser_expression (parser);
12380 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12381 tree c, t = expr.value;
12382 t = c_fully_fold (t, false, NULL);
12384 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12386 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12388 c_parser_error (parser, "expected integer expression");
12389 return list;
12392 /* Attempt to statically determine when the number isn't positive. */
12393 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12394 build_int_cst (TREE_TYPE (t), 0));
12395 protected_set_expr_location (c, expr_loc);
12396 if (c == boolean_true_node)
12398 warning_at (expr_loc, 0,
12399 "%<vector_length%> value must be positive");
12400 t = integer_one_node;
12403 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12405 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12406 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12407 OMP_CLAUSE_CHAIN (c) = list;
12408 list = c;
12411 return list;
12414 /* OpenMP 4.0:
12415 inbranch
12416 notinbranch */
12418 static tree
12419 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12420 enum omp_clause_code code, tree list)
12422 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12424 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12425 OMP_CLAUSE_CHAIN (c) = list;
12427 return c;
12430 /* OpenMP 4.0:
12431 parallel
12433 sections
12434 taskgroup */
12436 static tree
12437 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12438 enum omp_clause_code code, tree list)
12440 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12441 OMP_CLAUSE_CHAIN (c) = list;
12443 return c;
12446 /* OpenMP 4.5:
12447 nogroup */
12449 static tree
12450 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12452 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12453 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12454 OMP_CLAUSE_NOGROUP);
12455 OMP_CLAUSE_CHAIN (c) = list;
12456 return c;
12459 /* OpenMP 4.5:
12460 simd
12461 threads */
12463 static tree
12464 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12465 enum omp_clause_code code, tree list)
12467 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12468 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12469 OMP_CLAUSE_CHAIN (c) = list;
12470 return c;
12473 /* OpenMP 4.0:
12474 num_teams ( expression ) */
12476 static tree
12477 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12479 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12480 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12482 location_t expr_loc = c_parser_peek_token (parser)->location;
12483 c_expr expr = c_parser_expression (parser);
12484 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12485 tree c, t = expr.value;
12486 t = c_fully_fold (t, false, NULL);
12488 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12490 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12492 c_parser_error (parser, "expected integer expression");
12493 return list;
12496 /* Attempt to statically determine when the number isn't positive. */
12497 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12498 build_int_cst (TREE_TYPE (t), 0));
12499 protected_set_expr_location (c, expr_loc);
12500 if (c == boolean_true_node)
12502 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12503 t = integer_one_node;
12506 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12508 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12509 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12510 OMP_CLAUSE_CHAIN (c) = list;
12511 list = c;
12514 return list;
12517 /* OpenMP 4.0:
12518 thread_limit ( expression ) */
12520 static tree
12521 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12523 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12524 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12526 location_t expr_loc = c_parser_peek_token (parser)->location;
12527 c_expr expr = c_parser_expression (parser);
12528 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12529 tree c, t = expr.value;
12530 t = c_fully_fold (t, false, NULL);
12532 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12534 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12536 c_parser_error (parser, "expected integer expression");
12537 return list;
12540 /* Attempt to statically determine when the number isn't positive. */
12541 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12542 build_int_cst (TREE_TYPE (t), 0));
12543 protected_set_expr_location (c, expr_loc);
12544 if (c == boolean_true_node)
12546 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12547 t = integer_one_node;
12550 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12551 "thread_limit");
12553 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12554 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12555 OMP_CLAUSE_CHAIN (c) = list;
12556 list = c;
12559 return list;
12562 /* OpenMP 4.0:
12563 aligned ( variable-list )
12564 aligned ( variable-list : constant-expression ) */
12566 static tree
12567 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12569 location_t clause_loc = c_parser_peek_token (parser)->location;
12570 tree nl, c;
12572 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12573 return list;
12575 nl = c_parser_omp_variable_list (parser, clause_loc,
12576 OMP_CLAUSE_ALIGNED, list);
12578 if (c_parser_next_token_is (parser, CPP_COLON))
12580 c_parser_consume_token (parser);
12581 location_t expr_loc = c_parser_peek_token (parser)->location;
12582 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12583 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12584 tree alignment = expr.value;
12585 alignment = c_fully_fold (alignment, false, NULL);
12586 if (TREE_CODE (alignment) != INTEGER_CST
12587 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12588 || tree_int_cst_sgn (alignment) != 1)
12590 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12591 "be positive constant integer expression");
12592 alignment = NULL_TREE;
12595 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12596 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12600 return nl;
12603 /* OpenMP 4.0:
12604 linear ( variable-list )
12605 linear ( variable-list : expression )
12607 OpenMP 4.5:
12608 linear ( modifier ( variable-list ) )
12609 linear ( modifier ( variable-list ) : expression ) */
12611 static tree
12612 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12614 location_t clause_loc = c_parser_peek_token (parser)->location;
12615 tree nl, c, step;
12616 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12618 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12619 return list;
12621 if (!is_cilk_simd_fn
12622 && c_parser_next_token_is (parser, CPP_NAME))
12624 c_token *tok = c_parser_peek_token (parser);
12625 const char *p = IDENTIFIER_POINTER (tok->value);
12626 if (strcmp ("val", p) == 0)
12627 kind = OMP_CLAUSE_LINEAR_VAL;
12628 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12629 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12630 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12632 c_parser_consume_token (parser);
12633 c_parser_consume_token (parser);
12637 nl = c_parser_omp_variable_list (parser, clause_loc,
12638 OMP_CLAUSE_LINEAR, list);
12640 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12641 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12643 if (c_parser_next_token_is (parser, CPP_COLON))
12645 c_parser_consume_token (parser);
12646 location_t expr_loc = c_parser_peek_token (parser)->location;
12647 c_expr expr = c_parser_expression (parser);
12648 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12649 step = expr.value;
12650 step = c_fully_fold (step, false, NULL);
12651 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12653 sorry ("using parameters for %<linear%> step is not supported yet");
12654 step = integer_one_node;
12656 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12658 error_at (clause_loc, "%<linear%> clause step expression must "
12659 "be integral");
12660 step = integer_one_node;
12664 else
12665 step = integer_one_node;
12667 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12669 OMP_CLAUSE_LINEAR_STEP (c) = step;
12670 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12673 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12674 return nl;
12677 /* OpenMP 4.0:
12678 safelen ( constant-expression ) */
12680 static tree
12681 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12683 location_t clause_loc = c_parser_peek_token (parser)->location;
12684 tree c, t;
12686 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12687 return list;
12689 location_t expr_loc = c_parser_peek_token (parser)->location;
12690 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12691 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12692 t = expr.value;
12693 t = c_fully_fold (t, false, NULL);
12694 if (TREE_CODE (t) != INTEGER_CST
12695 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12696 || tree_int_cst_sgn (t) != 1)
12698 error_at (clause_loc, "%<safelen%> clause expression must "
12699 "be positive constant integer expression");
12700 t = NULL_TREE;
12703 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12704 if (t == NULL_TREE || t == error_mark_node)
12705 return list;
12707 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12709 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12710 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12711 OMP_CLAUSE_CHAIN (c) = list;
12712 return c;
12715 /* OpenMP 4.0:
12716 simdlen ( constant-expression ) */
12718 static tree
12719 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12721 location_t clause_loc = c_parser_peek_token (parser)->location;
12722 tree c, t;
12724 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12725 return list;
12727 location_t expr_loc = c_parser_peek_token (parser)->location;
12728 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12729 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12730 t = expr.value;
12731 t = c_fully_fold (t, false, NULL);
12732 if (TREE_CODE (t) != INTEGER_CST
12733 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12734 || tree_int_cst_sgn (t) != 1)
12736 error_at (clause_loc, "%<simdlen%> clause expression must "
12737 "be positive constant integer expression");
12738 t = NULL_TREE;
12741 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12742 if (t == NULL_TREE || t == error_mark_node)
12743 return list;
12745 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12747 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12748 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12749 OMP_CLAUSE_CHAIN (c) = list;
12750 return c;
12753 /* OpenMP 4.5:
12754 vec:
12755 identifier [+/- integer]
12756 vec , identifier [+/- integer]
12759 static tree
12760 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12761 tree list)
12763 tree vec = NULL;
12764 if (c_parser_next_token_is_not (parser, CPP_NAME)
12765 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12767 c_parser_error (parser, "expected identifier");
12768 return list;
12771 while (c_parser_next_token_is (parser, CPP_NAME)
12772 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12774 tree t = lookup_name (c_parser_peek_token (parser)->value);
12775 tree addend = NULL;
12777 if (t == NULL_TREE)
12779 undeclared_variable (c_parser_peek_token (parser)->location,
12780 c_parser_peek_token (parser)->value);
12781 t = error_mark_node;
12784 c_parser_consume_token (parser);
12786 bool neg = false;
12787 if (c_parser_next_token_is (parser, CPP_MINUS))
12788 neg = true;
12789 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12791 addend = integer_zero_node;
12792 neg = false;
12793 goto add_to_vector;
12795 c_parser_consume_token (parser);
12797 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12799 c_parser_error (parser, "expected integer");
12800 return list;
12803 addend = c_parser_peek_token (parser)->value;
12804 if (TREE_CODE (addend) != INTEGER_CST)
12806 c_parser_error (parser, "expected integer");
12807 return list;
12809 c_parser_consume_token (parser);
12811 add_to_vector:
12812 if (t != error_mark_node)
12814 vec = tree_cons (addend, t, vec);
12815 if (neg)
12816 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12819 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12820 break;
12822 c_parser_consume_token (parser);
12825 if (vec == NULL_TREE)
12826 return list;
12828 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12829 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12830 OMP_CLAUSE_DECL (u) = nreverse (vec);
12831 OMP_CLAUSE_CHAIN (u) = list;
12832 return u;
12835 /* OpenMP 4.0:
12836 depend ( depend-kind: variable-list )
12838 depend-kind:
12839 in | out | inout
12841 OpenMP 4.5:
12842 depend ( source )
12844 depend ( sink : vec ) */
12846 static tree
12847 c_parser_omp_clause_depend (c_parser *parser, tree list)
12849 location_t clause_loc = c_parser_peek_token (parser)->location;
12850 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12851 tree nl, c;
12853 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12854 return list;
12856 if (c_parser_next_token_is (parser, CPP_NAME))
12858 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12859 if (strcmp ("in", p) == 0)
12860 kind = OMP_CLAUSE_DEPEND_IN;
12861 else if (strcmp ("inout", p) == 0)
12862 kind = OMP_CLAUSE_DEPEND_INOUT;
12863 else if (strcmp ("out", p) == 0)
12864 kind = OMP_CLAUSE_DEPEND_OUT;
12865 else if (strcmp ("source", p) == 0)
12866 kind = OMP_CLAUSE_DEPEND_SOURCE;
12867 else if (strcmp ("sink", p) == 0)
12868 kind = OMP_CLAUSE_DEPEND_SINK;
12869 else
12870 goto invalid_kind;
12872 else
12873 goto invalid_kind;
12875 c_parser_consume_token (parser);
12877 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12879 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12880 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12881 OMP_CLAUSE_DECL (c) = NULL_TREE;
12882 OMP_CLAUSE_CHAIN (c) = list;
12883 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12884 return c;
12887 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12888 goto resync_fail;
12890 if (kind == OMP_CLAUSE_DEPEND_SINK)
12891 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12892 else
12894 nl = c_parser_omp_variable_list (parser, clause_loc,
12895 OMP_CLAUSE_DEPEND, list);
12897 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12898 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12901 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12902 return nl;
12904 invalid_kind:
12905 c_parser_error (parser, "invalid depend kind");
12906 resync_fail:
12907 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12908 return list;
12911 /* OpenMP 4.0:
12912 map ( map-kind: variable-list )
12913 map ( variable-list )
12915 map-kind:
12916 alloc | to | from | tofrom
12918 OpenMP 4.5:
12919 map-kind:
12920 alloc | to | from | tofrom | release | delete
12922 map ( always [,] map-kind: variable-list ) */
12924 static tree
12925 c_parser_omp_clause_map (c_parser *parser, tree list)
12927 location_t clause_loc = c_parser_peek_token (parser)->location;
12928 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12929 int always = 0;
12930 enum c_id_kind always_id_kind = C_ID_NONE;
12931 location_t always_loc = UNKNOWN_LOCATION;
12932 tree always_id = NULL_TREE;
12933 tree nl, c;
12935 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12936 return list;
12938 if (c_parser_next_token_is (parser, CPP_NAME))
12940 c_token *tok = c_parser_peek_token (parser);
12941 const char *p = IDENTIFIER_POINTER (tok->value);
12942 always_id_kind = tok->id_kind;
12943 always_loc = tok->location;
12944 always_id = tok->value;
12945 if (strcmp ("always", p) == 0)
12947 c_token *sectok = c_parser_peek_2nd_token (parser);
12948 if (sectok->type == CPP_COMMA)
12950 c_parser_consume_token (parser);
12951 c_parser_consume_token (parser);
12952 always = 2;
12954 else if (sectok->type == CPP_NAME)
12956 p = IDENTIFIER_POINTER (sectok->value);
12957 if (strcmp ("alloc", p) == 0
12958 || strcmp ("to", p) == 0
12959 || strcmp ("from", p) == 0
12960 || strcmp ("tofrom", p) == 0
12961 || strcmp ("release", p) == 0
12962 || strcmp ("delete", p) == 0)
12964 c_parser_consume_token (parser);
12965 always = 1;
12971 if (c_parser_next_token_is (parser, CPP_NAME)
12972 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12974 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12975 if (strcmp ("alloc", p) == 0)
12976 kind = GOMP_MAP_ALLOC;
12977 else if (strcmp ("to", p) == 0)
12978 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12979 else if (strcmp ("from", p) == 0)
12980 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12981 else if (strcmp ("tofrom", p) == 0)
12982 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12983 else if (strcmp ("release", p) == 0)
12984 kind = GOMP_MAP_RELEASE;
12985 else if (strcmp ("delete", p) == 0)
12986 kind = GOMP_MAP_DELETE;
12987 else
12989 c_parser_error (parser, "invalid map kind");
12990 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12991 "expected %<)%>");
12992 return list;
12994 c_parser_consume_token (parser);
12995 c_parser_consume_token (parser);
12997 else if (always)
12999 if (always_id_kind != C_ID_ID)
13001 c_parser_error (parser, "expected identifier");
13002 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13003 return list;
13006 tree t = lookup_name (always_id);
13007 if (t == NULL_TREE)
13009 undeclared_variable (always_loc, always_id);
13010 t = error_mark_node;
13012 if (t != error_mark_node)
13014 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13015 OMP_CLAUSE_DECL (u) = t;
13016 OMP_CLAUSE_CHAIN (u) = list;
13017 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13018 list = u;
13020 if (always == 1)
13022 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13023 return list;
13027 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13029 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13030 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13032 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13033 return nl;
13036 /* OpenMP 4.0:
13037 device ( expression ) */
13039 static tree
13040 c_parser_omp_clause_device (c_parser *parser, tree list)
13042 location_t clause_loc = c_parser_peek_token (parser)->location;
13043 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13045 location_t expr_loc = c_parser_peek_token (parser)->location;
13046 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13047 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13048 tree c, t = expr.value;
13049 t = c_fully_fold (t, false, NULL);
13051 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13053 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13055 c_parser_error (parser, "expected integer expression");
13056 return list;
13059 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13061 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13062 OMP_CLAUSE_DEVICE_ID (c) = t;
13063 OMP_CLAUSE_CHAIN (c) = list;
13064 list = c;
13067 return list;
13070 /* OpenMP 4.0:
13071 dist_schedule ( static )
13072 dist_schedule ( static , expression ) */
13074 static tree
13075 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13077 tree c, t = NULL_TREE;
13078 location_t loc = c_parser_peek_token (parser)->location;
13080 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13081 return list;
13083 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13085 c_parser_error (parser, "invalid dist_schedule kind");
13086 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13087 "expected %<)%>");
13088 return list;
13091 c_parser_consume_token (parser);
13092 if (c_parser_next_token_is (parser, CPP_COMMA))
13094 c_parser_consume_token (parser);
13096 location_t expr_loc = c_parser_peek_token (parser)->location;
13097 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13098 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13099 t = expr.value;
13100 t = c_fully_fold (t, false, NULL);
13101 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13103 else
13104 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13105 "expected %<,%> or %<)%>");
13107 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13108 if (t == error_mark_node)
13109 return list;
13111 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13112 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13113 OMP_CLAUSE_CHAIN (c) = list;
13114 return c;
13117 /* OpenMP 4.0:
13118 proc_bind ( proc-bind-kind )
13120 proc-bind-kind:
13121 master | close | spread */
13123 static tree
13124 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13126 location_t clause_loc = c_parser_peek_token (parser)->location;
13127 enum omp_clause_proc_bind_kind kind;
13128 tree c;
13130 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13131 return list;
13133 if (c_parser_next_token_is (parser, CPP_NAME))
13135 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13136 if (strcmp ("master", p) == 0)
13137 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13138 else if (strcmp ("close", p) == 0)
13139 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13140 else if (strcmp ("spread", p) == 0)
13141 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13142 else
13143 goto invalid_kind;
13145 else
13146 goto invalid_kind;
13148 c_parser_consume_token (parser);
13149 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13150 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13151 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13152 OMP_CLAUSE_CHAIN (c) = list;
13153 return c;
13155 invalid_kind:
13156 c_parser_error (parser, "invalid proc_bind kind");
13157 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13158 return list;
13161 /* OpenMP 4.0:
13162 to ( variable-list ) */
13164 static tree
13165 c_parser_omp_clause_to (c_parser *parser, tree list)
13167 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13170 /* OpenMP 4.0:
13171 from ( variable-list ) */
13173 static tree
13174 c_parser_omp_clause_from (c_parser *parser, tree list)
13176 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13179 /* OpenMP 4.0:
13180 uniform ( variable-list ) */
13182 static tree
13183 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13185 /* The clauses location. */
13186 location_t loc = c_parser_peek_token (parser)->location;
13188 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13190 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13191 list);
13192 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13194 return list;
13197 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13198 is a bitmask in MASK. Return the list of clauses found. */
13200 static tree
13201 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13202 const char *where, bool finish_p = true)
13204 tree clauses = NULL;
13205 bool first = true;
13207 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13209 location_t here;
13210 pragma_omp_clause c_kind;
13211 const char *c_name;
13212 tree prev = clauses;
13214 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13215 c_parser_consume_token (parser);
13217 here = c_parser_peek_token (parser)->location;
13218 c_kind = c_parser_omp_clause_name (parser);
13220 switch (c_kind)
13222 case PRAGMA_OACC_CLAUSE_ASYNC:
13223 clauses = c_parser_oacc_clause_async (parser, clauses);
13224 c_name = "async";
13225 break;
13226 case PRAGMA_OACC_CLAUSE_AUTO:
13227 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13228 clauses);
13229 c_name = "auto";
13230 break;
13231 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13232 clauses = c_parser_omp_clause_collapse (parser, clauses);
13233 c_name = "collapse";
13234 break;
13235 case PRAGMA_OACC_CLAUSE_COPY:
13236 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13237 c_name = "copy";
13238 break;
13239 case PRAGMA_OACC_CLAUSE_COPYIN:
13240 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13241 c_name = "copyin";
13242 break;
13243 case PRAGMA_OACC_CLAUSE_COPYOUT:
13244 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13245 c_name = "copyout";
13246 break;
13247 case PRAGMA_OACC_CLAUSE_CREATE:
13248 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13249 c_name = "create";
13250 break;
13251 case PRAGMA_OACC_CLAUSE_DELETE:
13252 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13253 c_name = "delete";
13254 break;
13255 case PRAGMA_OMP_CLAUSE_DEFAULT:
13256 clauses = c_parser_omp_clause_default (parser, clauses, true);
13257 c_name = "default";
13258 break;
13259 case PRAGMA_OACC_CLAUSE_DEVICE:
13260 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13261 c_name = "device";
13262 break;
13263 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13264 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13265 c_name = "deviceptr";
13266 break;
13267 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13268 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13269 c_name = "device_resident";
13270 break;
13271 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13272 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13273 c_name = "firstprivate";
13274 break;
13275 case PRAGMA_OACC_CLAUSE_GANG:
13276 c_name = "gang";
13277 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13278 c_name, clauses);
13279 break;
13280 case PRAGMA_OACC_CLAUSE_HOST:
13281 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13282 c_name = "host";
13283 break;
13284 case PRAGMA_OACC_CLAUSE_IF:
13285 clauses = c_parser_omp_clause_if (parser, clauses, false);
13286 c_name = "if";
13287 break;
13288 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13289 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13290 clauses);
13291 c_name = "independent";
13292 break;
13293 case PRAGMA_OACC_CLAUSE_LINK:
13294 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13295 c_name = "link";
13296 break;
13297 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13298 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13299 c_name = "num_gangs";
13300 break;
13301 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13302 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13303 c_name = "num_workers";
13304 break;
13305 case PRAGMA_OACC_CLAUSE_PRESENT:
13306 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13307 c_name = "present";
13308 break;
13309 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13310 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13311 c_name = "present_or_copy";
13312 break;
13313 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13314 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13315 c_name = "present_or_copyin";
13316 break;
13317 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13318 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13319 c_name = "present_or_copyout";
13320 break;
13321 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13322 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13323 c_name = "present_or_create";
13324 break;
13325 case PRAGMA_OACC_CLAUSE_PRIVATE:
13326 clauses = c_parser_omp_clause_private (parser, clauses);
13327 c_name = "private";
13328 break;
13329 case PRAGMA_OACC_CLAUSE_REDUCTION:
13330 clauses = c_parser_omp_clause_reduction (parser, clauses);
13331 c_name = "reduction";
13332 break;
13333 case PRAGMA_OACC_CLAUSE_SELF:
13334 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13335 c_name = "self";
13336 break;
13337 case PRAGMA_OACC_CLAUSE_SEQ:
13338 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13339 clauses);
13340 c_name = "seq";
13341 break;
13342 case PRAGMA_OACC_CLAUSE_TILE:
13343 clauses = c_parser_oacc_clause_tile (parser, clauses);
13344 c_name = "tile";
13345 break;
13346 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13347 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13348 c_name = "use_device";
13349 break;
13350 case PRAGMA_OACC_CLAUSE_VECTOR:
13351 c_name = "vector";
13352 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13353 c_name, clauses);
13354 break;
13355 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13356 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13357 c_name = "vector_length";
13358 break;
13359 case PRAGMA_OACC_CLAUSE_WAIT:
13360 clauses = c_parser_oacc_clause_wait (parser, clauses);
13361 c_name = "wait";
13362 break;
13363 case PRAGMA_OACC_CLAUSE_WORKER:
13364 c_name = "worker";
13365 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13366 c_name, clauses);
13367 break;
13368 default:
13369 c_parser_error (parser, "expected %<#pragma acc%> clause");
13370 goto saw_error;
13373 first = false;
13375 if (((mask >> c_kind) & 1) == 0)
13377 /* Remove the invalid clause(s) from the list to avoid
13378 confusing the rest of the compiler. */
13379 clauses = prev;
13380 error_at (here, "%qs is not valid for %qs", c_name, where);
13384 saw_error:
13385 c_parser_skip_to_pragma_eol (parser);
13387 if (finish_p)
13388 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13390 return clauses;
13393 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13394 is a bitmask in MASK. Return the list of clauses found. */
13396 static tree
13397 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13398 const char *where, bool finish_p = true)
13400 tree clauses = NULL;
13401 bool first = true, cilk_simd_fn = false;
13403 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13405 location_t here;
13406 pragma_omp_clause c_kind;
13407 const char *c_name;
13408 tree prev = clauses;
13410 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13411 c_parser_consume_token (parser);
13413 here = c_parser_peek_token (parser)->location;
13414 c_kind = c_parser_omp_clause_name (parser);
13416 switch (c_kind)
13418 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13419 clauses = c_parser_omp_clause_collapse (parser, clauses);
13420 c_name = "collapse";
13421 break;
13422 case PRAGMA_OMP_CLAUSE_COPYIN:
13423 clauses = c_parser_omp_clause_copyin (parser, clauses);
13424 c_name = "copyin";
13425 break;
13426 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13427 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13428 c_name = "copyprivate";
13429 break;
13430 case PRAGMA_OMP_CLAUSE_DEFAULT:
13431 clauses = c_parser_omp_clause_default (parser, clauses, false);
13432 c_name = "default";
13433 break;
13434 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13435 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13436 c_name = "firstprivate";
13437 break;
13438 case PRAGMA_OMP_CLAUSE_FINAL:
13439 clauses = c_parser_omp_clause_final (parser, clauses);
13440 c_name = "final";
13441 break;
13442 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13443 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13444 c_name = "grainsize";
13445 break;
13446 case PRAGMA_OMP_CLAUSE_HINT:
13447 clauses = c_parser_omp_clause_hint (parser, clauses);
13448 c_name = "hint";
13449 break;
13450 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13451 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13452 c_name = "defaultmap";
13453 break;
13454 case PRAGMA_OMP_CLAUSE_IF:
13455 clauses = c_parser_omp_clause_if (parser, clauses, true);
13456 c_name = "if";
13457 break;
13458 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13459 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13460 c_name = "lastprivate";
13461 break;
13462 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13463 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13464 c_name = "mergeable";
13465 break;
13466 case PRAGMA_OMP_CLAUSE_NOWAIT:
13467 clauses = c_parser_omp_clause_nowait (parser, clauses);
13468 c_name = "nowait";
13469 break;
13470 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13471 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13472 c_name = "num_tasks";
13473 break;
13474 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13475 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13476 c_name = "num_threads";
13477 break;
13478 case PRAGMA_OMP_CLAUSE_ORDERED:
13479 clauses = c_parser_omp_clause_ordered (parser, clauses);
13480 c_name = "ordered";
13481 break;
13482 case PRAGMA_OMP_CLAUSE_PRIORITY:
13483 clauses = c_parser_omp_clause_priority (parser, clauses);
13484 c_name = "priority";
13485 break;
13486 case PRAGMA_OMP_CLAUSE_PRIVATE:
13487 clauses = c_parser_omp_clause_private (parser, clauses);
13488 c_name = "private";
13489 break;
13490 case PRAGMA_OMP_CLAUSE_REDUCTION:
13491 clauses = c_parser_omp_clause_reduction (parser, clauses);
13492 c_name = "reduction";
13493 break;
13494 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13495 clauses = c_parser_omp_clause_schedule (parser, clauses);
13496 c_name = "schedule";
13497 break;
13498 case PRAGMA_OMP_CLAUSE_SHARED:
13499 clauses = c_parser_omp_clause_shared (parser, clauses);
13500 c_name = "shared";
13501 break;
13502 case PRAGMA_OMP_CLAUSE_UNTIED:
13503 clauses = c_parser_omp_clause_untied (parser, clauses);
13504 c_name = "untied";
13505 break;
13506 case PRAGMA_OMP_CLAUSE_INBRANCH:
13507 case PRAGMA_CILK_CLAUSE_MASK:
13508 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13509 clauses);
13510 c_name = "inbranch";
13511 break;
13512 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13513 case PRAGMA_CILK_CLAUSE_NOMASK:
13514 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13515 clauses);
13516 c_name = "notinbranch";
13517 break;
13518 case PRAGMA_OMP_CLAUSE_PARALLEL:
13519 clauses
13520 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13521 clauses);
13522 c_name = "parallel";
13523 if (!first)
13525 clause_not_first:
13526 error_at (here, "%qs must be the first clause of %qs",
13527 c_name, where);
13528 clauses = prev;
13530 break;
13531 case PRAGMA_OMP_CLAUSE_FOR:
13532 clauses
13533 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13534 clauses);
13535 c_name = "for";
13536 if (!first)
13537 goto clause_not_first;
13538 break;
13539 case PRAGMA_OMP_CLAUSE_SECTIONS:
13540 clauses
13541 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13542 clauses);
13543 c_name = "sections";
13544 if (!first)
13545 goto clause_not_first;
13546 break;
13547 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13548 clauses
13549 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13550 clauses);
13551 c_name = "taskgroup";
13552 if (!first)
13553 goto clause_not_first;
13554 break;
13555 case PRAGMA_OMP_CLAUSE_LINK:
13556 clauses
13557 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13558 c_name = "link";
13559 break;
13560 case PRAGMA_OMP_CLAUSE_TO:
13561 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13562 clauses
13563 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13564 clauses);
13565 else
13566 clauses = c_parser_omp_clause_to (parser, clauses);
13567 c_name = "to";
13568 break;
13569 case PRAGMA_OMP_CLAUSE_FROM:
13570 clauses = c_parser_omp_clause_from (parser, clauses);
13571 c_name = "from";
13572 break;
13573 case PRAGMA_OMP_CLAUSE_UNIFORM:
13574 clauses = c_parser_omp_clause_uniform (parser, clauses);
13575 c_name = "uniform";
13576 break;
13577 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13578 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13579 c_name = "num_teams";
13580 break;
13581 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13582 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13583 c_name = "thread_limit";
13584 break;
13585 case PRAGMA_OMP_CLAUSE_ALIGNED:
13586 clauses = c_parser_omp_clause_aligned (parser, clauses);
13587 c_name = "aligned";
13588 break;
13589 case PRAGMA_OMP_CLAUSE_LINEAR:
13590 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13591 cilk_simd_fn = true;
13592 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13593 c_name = "linear";
13594 break;
13595 case PRAGMA_OMP_CLAUSE_DEPEND:
13596 clauses = c_parser_omp_clause_depend (parser, clauses);
13597 c_name = "depend";
13598 break;
13599 case PRAGMA_OMP_CLAUSE_MAP:
13600 clauses = c_parser_omp_clause_map (parser, clauses);
13601 c_name = "map";
13602 break;
13603 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13604 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13605 c_name = "use_device_ptr";
13606 break;
13607 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13608 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13609 c_name = "is_device_ptr";
13610 break;
13611 case PRAGMA_OMP_CLAUSE_DEVICE:
13612 clauses = c_parser_omp_clause_device (parser, clauses);
13613 c_name = "device";
13614 break;
13615 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13616 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13617 c_name = "dist_schedule";
13618 break;
13619 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13620 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13621 c_name = "proc_bind";
13622 break;
13623 case PRAGMA_OMP_CLAUSE_SAFELEN:
13624 clauses = c_parser_omp_clause_safelen (parser, clauses);
13625 c_name = "safelen";
13626 break;
13627 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13628 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13629 c_name = "simdlen";
13630 break;
13631 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13632 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13633 c_name = "simdlen";
13634 break;
13635 case PRAGMA_OMP_CLAUSE_NOGROUP:
13636 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13637 c_name = "nogroup";
13638 break;
13639 case PRAGMA_OMP_CLAUSE_THREADS:
13640 clauses
13641 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13642 clauses);
13643 c_name = "threads";
13644 break;
13645 case PRAGMA_OMP_CLAUSE_SIMD:
13646 clauses
13647 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13648 clauses);
13649 c_name = "simd";
13650 break;
13651 default:
13652 c_parser_error (parser, "expected %<#pragma omp%> clause");
13653 goto saw_error;
13656 first = false;
13658 if (((mask >> c_kind) & 1) == 0)
13660 /* Remove the invalid clause(s) from the list to avoid
13661 confusing the rest of the compiler. */
13662 clauses = prev;
13663 error_at (here, "%qs is not valid for %qs", c_name, where);
13667 saw_error:
13668 c_parser_skip_to_pragma_eol (parser);
13670 if (finish_p)
13672 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13673 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
13674 return c_finish_omp_clauses (clauses, C_ORT_OMP);
13677 return clauses;
13680 /* OpenACC 2.0, OpenMP 2.5:
13681 structured-block:
13682 statement
13684 In practice, we're also interested in adding the statement to an
13685 outer node. So it is convenient if we work around the fact that
13686 c_parser_statement calls add_stmt. */
13688 static tree
13689 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13691 tree stmt = push_stmt_list ();
13692 c_parser_statement (parser, if_p);
13693 return pop_stmt_list (stmt);
13696 /* OpenACC 2.0:
13697 # pragma acc cache (variable-list) new-line
13699 LOC is the location of the #pragma token.
13702 static tree
13703 c_parser_oacc_cache (location_t loc, c_parser *parser)
13705 tree stmt, clauses;
13707 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13708 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13710 c_parser_skip_to_pragma_eol (parser);
13712 stmt = make_node (OACC_CACHE);
13713 TREE_TYPE (stmt) = void_type_node;
13714 OACC_CACHE_CLAUSES (stmt) = clauses;
13715 SET_EXPR_LOCATION (stmt, loc);
13716 add_stmt (stmt);
13718 return stmt;
13721 /* OpenACC 2.0:
13722 # pragma acc data oacc-data-clause[optseq] new-line
13723 structured-block
13725 LOC is the location of the #pragma token.
13728 #define OACC_DATA_CLAUSE_MASK \
13729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13741 static tree
13742 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13744 tree stmt, clauses, block;
13746 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13747 "#pragma acc data");
13749 block = c_begin_omp_parallel ();
13750 add_stmt (c_parser_omp_structured_block (parser, if_p));
13752 stmt = c_finish_oacc_data (loc, clauses, block);
13754 return stmt;
13757 /* OpenACC 2.0:
13758 # pragma acc declare oacc-data-clause[optseq] new-line
13761 #define OACC_DECLARE_CLAUSE_MASK \
13762 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13775 static void
13776 c_parser_oacc_declare (c_parser *parser)
13778 location_t pragma_loc = c_parser_peek_token (parser)->location;
13779 tree clauses, stmt, t, decl;
13781 bool error = false;
13783 c_parser_consume_pragma (parser);
13785 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13786 "#pragma acc declare");
13787 if (!clauses)
13789 error_at (pragma_loc,
13790 "no valid clauses specified in %<#pragma acc declare%>");
13791 return;
13794 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13796 location_t loc = OMP_CLAUSE_LOCATION (t);
13797 decl = OMP_CLAUSE_DECL (t);
13798 if (!DECL_P (decl))
13800 error_at (loc, "array section in %<#pragma acc declare%>");
13801 error = true;
13802 continue;
13805 switch (OMP_CLAUSE_MAP_KIND (t))
13807 case GOMP_MAP_FIRSTPRIVATE_POINTER:
13808 case GOMP_MAP_FORCE_ALLOC:
13809 case GOMP_MAP_FORCE_TO:
13810 case GOMP_MAP_FORCE_DEVICEPTR:
13811 case GOMP_MAP_DEVICE_RESIDENT:
13812 break;
13814 case GOMP_MAP_LINK:
13815 if (!global_bindings_p ()
13816 && (TREE_STATIC (decl)
13817 || !DECL_EXTERNAL (decl)))
13819 error_at (loc,
13820 "%qD must be a global variable in "
13821 "%<#pragma acc declare link%>",
13822 decl);
13823 error = true;
13824 continue;
13826 break;
13828 default:
13829 if (global_bindings_p ())
13831 error_at (loc, "invalid OpenACC clause at file scope");
13832 error = true;
13833 continue;
13835 if (DECL_EXTERNAL (decl))
13837 error_at (loc,
13838 "invalid use of %<extern%> variable %qD "
13839 "in %<#pragma acc declare%>", decl);
13840 error = true;
13841 continue;
13843 else if (TREE_PUBLIC (decl))
13845 error_at (loc,
13846 "invalid use of %<global%> variable %qD "
13847 "in %<#pragma acc declare%>", decl);
13848 error = true;
13849 continue;
13851 break;
13854 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13855 || lookup_attribute ("omp declare target link",
13856 DECL_ATTRIBUTES (decl)))
13858 error_at (loc, "variable %qD used more than once with "
13859 "%<#pragma acc declare%>", decl);
13860 error = true;
13861 continue;
13864 if (!error)
13866 tree id;
13868 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13869 id = get_identifier ("omp declare target link");
13870 else
13871 id = get_identifier ("omp declare target");
13873 DECL_ATTRIBUTES (decl)
13874 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13876 if (global_bindings_p ())
13878 symtab_node *node = symtab_node::get (decl);
13879 if (node != NULL)
13881 node->offloadable = 1;
13882 if (ENABLE_OFFLOADING)
13884 g->have_offload = true;
13885 if (is_a <varpool_node *> (node))
13886 vec_safe_push (offload_vars, decl);
13893 if (error || global_bindings_p ())
13894 return;
13896 stmt = make_node (OACC_DECLARE);
13897 TREE_TYPE (stmt) = void_type_node;
13898 OACC_DECLARE_CLAUSES (stmt) = clauses;
13899 SET_EXPR_LOCATION (stmt, pragma_loc);
13901 add_stmt (stmt);
13903 return;
13906 /* OpenACC 2.0:
13907 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13911 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13914 LOC is the location of the #pragma token.
13917 #define OACC_ENTER_DATA_CLAUSE_MASK \
13918 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13926 #define OACC_EXIT_DATA_CLAUSE_MASK \
13927 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13933 static void
13934 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13936 location_t loc = c_parser_peek_token (parser)->location;
13937 tree clauses, stmt;
13938 const char *p = "";
13940 c_parser_consume_pragma (parser);
13942 if (c_parser_next_token_is (parser, CPP_NAME))
13944 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13945 c_parser_consume_token (parser);
13948 if (strcmp (p, "data") != 0)
13950 error_at (loc, enter
13951 ? "expected %<data%> after %<#pragma acc enter%>"
13952 : "expected %<data%> after %<#pragma acc exit%>");
13953 parser->error = true;
13954 c_parser_skip_to_pragma_eol (parser);
13955 return;
13958 if (enter)
13959 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13960 "#pragma acc enter data");
13961 else
13962 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13963 "#pragma acc exit data");
13965 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13967 error_at (loc, enter
13968 ? "%<#pragma acc enter data%> has no data movement clause"
13969 : "%<#pragma acc exit data%> has no data movement clause");
13970 return;
13973 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13974 TREE_TYPE (stmt) = void_type_node;
13975 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13976 SET_EXPR_LOCATION (stmt, loc);
13977 add_stmt (stmt);
13981 /* OpenACC 2.0:
13982 # pragma acc host_data oacc-data-clause[optseq] new-line
13983 structured-block
13986 #define OACC_HOST_DATA_CLAUSE_MASK \
13987 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13989 static tree
13990 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
13992 tree stmt, clauses, block;
13994 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13995 "#pragma acc host_data");
13997 block = c_begin_omp_parallel ();
13998 add_stmt (c_parser_omp_structured_block (parser, if_p));
13999 stmt = c_finish_oacc_host_data (loc, clauses, block);
14000 return stmt;
14004 /* OpenACC 2.0:
14006 # pragma acc loop oacc-loop-clause[optseq] new-line
14007 structured-block
14009 LOC is the location of the #pragma token.
14012 #define OACC_LOOP_CLAUSE_MASK \
14013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14023 static tree
14024 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14025 omp_clause_mask mask, tree *cclauses, bool *if_p)
14027 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14029 strcat (p_name, " loop");
14030 mask |= OACC_LOOP_CLAUSE_MASK;
14032 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14033 cclauses == NULL);
14034 if (cclauses)
14036 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14037 if (*cclauses)
14038 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14039 if (clauses)
14040 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14043 tree block = c_begin_compound_stmt (true);
14044 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14045 if_p);
14046 block = c_end_compound_stmt (loc, block, true);
14047 add_stmt (block);
14049 return stmt;
14052 /* OpenACC 2.0:
14053 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14054 structured-block
14058 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14059 structured-block
14061 LOC is the location of the #pragma token.
14064 #define OACC_KERNELS_CLAUSE_MASK \
14065 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14080 #define OACC_PARALLEL_CLAUSE_MASK \
14081 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14102 static tree
14103 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14104 enum pragma_kind p_kind, char *p_name,
14105 bool *if_p)
14107 omp_clause_mask mask;
14108 enum tree_code code;
14109 switch (p_kind)
14111 case PRAGMA_OACC_KERNELS:
14112 strcat (p_name, " kernels");
14113 mask = OACC_KERNELS_CLAUSE_MASK;
14114 code = OACC_KERNELS;
14115 break;
14116 case PRAGMA_OACC_PARALLEL:
14117 strcat (p_name, " parallel");
14118 mask = OACC_PARALLEL_CLAUSE_MASK;
14119 code = OACC_PARALLEL;
14120 break;
14121 default:
14122 gcc_unreachable ();
14125 if (c_parser_next_token_is (parser, CPP_NAME))
14127 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14128 if (strcmp (p, "loop") == 0)
14130 c_parser_consume_token (parser);
14131 tree block = c_begin_omp_parallel ();
14132 tree clauses;
14133 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14134 return c_finish_omp_construct (loc, code, block, clauses);
14138 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14140 tree block = c_begin_omp_parallel ();
14141 add_stmt (c_parser_omp_structured_block (parser, if_p));
14143 return c_finish_omp_construct (loc, code, block, clauses);
14146 /* OpenACC 2.0:
14147 # pragma acc routine oacc-routine-clause[optseq] new-line
14148 function-definition
14150 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14153 #define OACC_ROUTINE_CLAUSE_MASK \
14154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14159 /* Parse an OpenACC routine directive. For named directives, we apply
14160 immediately to the named function. For unnamed ones we then parse
14161 a declaration or definition, which must be for a function. */
14163 static void
14164 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14166 gcc_checking_assert (context == pragma_external);
14168 oacc_routine_data data;
14169 data.error_seen = false;
14170 data.fndecl_seen = false;
14171 data.clauses = NULL_TREE;
14172 data.loc = c_parser_peek_token (parser)->location;
14174 c_parser_consume_pragma (parser);
14176 /* Look for optional '( name )'. */
14177 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14179 c_parser_consume_token (parser); /* '(' */
14181 tree decl = NULL_TREE;
14182 c_token *name_token = c_parser_peek_token (parser);
14183 location_t name_loc = name_token->location;
14184 if (name_token->type == CPP_NAME
14185 && (name_token->id_kind == C_ID_ID
14186 || name_token->id_kind == C_ID_TYPENAME))
14188 decl = lookup_name (name_token->value);
14189 if (!decl)
14190 error_at (name_loc,
14191 "%qE has not been declared", name_token->value);
14192 c_parser_consume_token (parser);
14194 else
14195 c_parser_error (parser, "expected function name");
14197 if (!decl
14198 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14200 c_parser_skip_to_pragma_eol (parser, false);
14201 return;
14204 data.clauses
14205 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14206 "#pragma acc routine");
14208 if (TREE_CODE (decl) != FUNCTION_DECL)
14210 error_at (name_loc, "%qD does not refer to a function", decl);
14211 return;
14214 c_finish_oacc_routine (&data, decl, false);
14216 else /* No optional '( name )'. */
14218 data.clauses
14219 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14220 "#pragma acc routine");
14222 /* Emit a helpful diagnostic if there's another pragma following this
14223 one. Also don't allow a static assertion declaration, as in the
14224 following we'll just parse a *single* "declaration or function
14225 definition", and the static assertion counts an one. */
14226 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14227 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14229 error_at (data.loc,
14230 "%<#pragma acc routine%> not immediately followed by"
14231 " function declaration or definition");
14232 /* ..., and then just keep going. */
14233 return;
14236 /* We only have to consider the pragma_external case here. */
14237 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14238 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14240 int ext = disable_extension_diagnostics ();
14242 c_parser_consume_token (parser);
14243 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14244 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14245 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14246 NULL, vNULL, &data);
14247 restore_extension_diagnostics (ext);
14249 else
14250 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14251 NULL, vNULL, &data);
14255 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14256 IS_DEFN is true if we're applying it to the definition. */
14258 static void
14259 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14260 bool is_defn)
14262 /* Keep going if we're in error reporting mode. */
14263 if (data->error_seen
14264 || fndecl == error_mark_node)
14265 return;
14267 if (data->fndecl_seen)
14269 error_at (data->loc,
14270 "%<#pragma acc routine%> not immediately followed by"
14271 " a single function declaration or definition");
14272 data->error_seen = true;
14273 return;
14275 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14277 error_at (data->loc,
14278 "%<#pragma acc routine%> not immediately followed by"
14279 " function declaration or definition");
14280 data->error_seen = true;
14281 return;
14284 if (oacc_get_fn_attrib (fndecl))
14286 error_at (data->loc,
14287 "%<#pragma acc routine%> already applied to %qD", fndecl);
14288 data->error_seen = true;
14289 return;
14292 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14294 error_at (data->loc,
14295 "%<#pragma acc routine%> must be applied before %s",
14296 TREE_USED (fndecl) ? "use" : "definition");
14297 data->error_seen = true;
14298 return;
14301 /* Process the routine's dimension clauses. */
14302 tree dims = oacc_build_routine_dims (data->clauses);
14303 oacc_replace_fn_attrib (fndecl, dims);
14305 /* Add an "omp declare target" attribute. */
14306 DECL_ATTRIBUTES (fndecl)
14307 = tree_cons (get_identifier ("omp declare target"),
14308 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14310 /* Remember that we've used this "#pragma acc routine". */
14311 data->fndecl_seen = true;
14314 /* OpenACC 2.0:
14315 # pragma acc update oacc-update-clause[optseq] new-line
14318 #define OACC_UPDATE_CLAUSE_MASK \
14319 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14326 static void
14327 c_parser_oacc_update (c_parser *parser)
14329 location_t loc = c_parser_peek_token (parser)->location;
14331 c_parser_consume_pragma (parser);
14333 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14334 "#pragma acc update");
14335 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14337 error_at (loc,
14338 "%<#pragma acc update%> must contain at least one "
14339 "%<device%> or %<host%> or %<self%> clause");
14340 return;
14343 if (parser->error)
14344 return;
14346 tree stmt = make_node (OACC_UPDATE);
14347 TREE_TYPE (stmt) = void_type_node;
14348 OACC_UPDATE_CLAUSES (stmt) = clauses;
14349 SET_EXPR_LOCATION (stmt, loc);
14350 add_stmt (stmt);
14353 /* OpenACC 2.0:
14354 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14356 LOC is the location of the #pragma token.
14359 #define OACC_WAIT_CLAUSE_MASK \
14360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14362 static tree
14363 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14365 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14367 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14368 list = c_parser_oacc_wait_list (parser, loc, list);
14370 strcpy (p_name, " wait");
14371 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14372 stmt = c_finish_oacc_wait (loc, list, clauses);
14373 add_stmt (stmt);
14375 return stmt;
14378 /* OpenMP 2.5:
14379 # pragma omp atomic new-line
14380 expression-stmt
14382 expression-stmt:
14383 x binop= expr | x++ | ++x | x-- | --x
14384 binop:
14385 +, *, -, /, &, ^, |, <<, >>
14387 where x is an lvalue expression with scalar type.
14389 OpenMP 3.1:
14390 # pragma omp atomic new-line
14391 update-stmt
14393 # pragma omp atomic read new-line
14394 read-stmt
14396 # pragma omp atomic write new-line
14397 write-stmt
14399 # pragma omp atomic update new-line
14400 update-stmt
14402 # pragma omp atomic capture new-line
14403 capture-stmt
14405 # pragma omp atomic capture new-line
14406 capture-block
14408 read-stmt:
14409 v = x
14410 write-stmt:
14411 x = expr
14412 update-stmt:
14413 expression-stmt | x = x binop expr
14414 capture-stmt:
14415 v = expression-stmt
14416 capture-block:
14417 { v = x; update-stmt; } | { update-stmt; v = x; }
14419 OpenMP 4.0:
14420 update-stmt:
14421 expression-stmt | x = x binop expr | x = expr binop x
14422 capture-stmt:
14423 v = update-stmt
14424 capture-block:
14425 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14427 where x and v are lvalue expressions with scalar type.
14429 LOC is the location of the #pragma token. */
14431 static void
14432 c_parser_omp_atomic (location_t loc, c_parser *parser)
14434 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14435 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14436 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14437 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14438 struct c_expr expr;
14439 location_t eloc;
14440 bool structured_block = false;
14441 bool swapped = false;
14442 bool seq_cst = false;
14443 bool non_lvalue_p;
14445 if (c_parser_next_token_is (parser, CPP_NAME))
14447 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14448 if (!strcmp (p, "seq_cst"))
14450 seq_cst = true;
14451 c_parser_consume_token (parser);
14452 if (c_parser_next_token_is (parser, CPP_COMMA)
14453 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14454 c_parser_consume_token (parser);
14457 if (c_parser_next_token_is (parser, CPP_NAME))
14459 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14461 if (!strcmp (p, "read"))
14462 code = OMP_ATOMIC_READ;
14463 else if (!strcmp (p, "write"))
14464 code = NOP_EXPR;
14465 else if (!strcmp (p, "update"))
14466 code = OMP_ATOMIC;
14467 else if (!strcmp (p, "capture"))
14468 code = OMP_ATOMIC_CAPTURE_NEW;
14469 else
14470 p = NULL;
14471 if (p)
14472 c_parser_consume_token (parser);
14474 if (!seq_cst)
14476 if (c_parser_next_token_is (parser, CPP_COMMA)
14477 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14478 c_parser_consume_token (parser);
14480 if (c_parser_next_token_is (parser, CPP_NAME))
14482 const char *p
14483 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14484 if (!strcmp (p, "seq_cst"))
14486 seq_cst = true;
14487 c_parser_consume_token (parser);
14491 c_parser_skip_to_pragma_eol (parser);
14493 switch (code)
14495 case OMP_ATOMIC_READ:
14496 case NOP_EXPR: /* atomic write */
14497 v = c_parser_cast_expression (parser, NULL).value;
14498 non_lvalue_p = !lvalue_p (v);
14499 v = c_fully_fold (v, false, NULL);
14500 if (v == error_mark_node)
14501 goto saw_error;
14502 if (non_lvalue_p)
14503 v = non_lvalue (v);
14504 loc = c_parser_peek_token (parser)->location;
14505 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14506 goto saw_error;
14507 if (code == NOP_EXPR)
14509 lhs = c_parser_expression (parser).value;
14510 lhs = c_fully_fold (lhs, false, NULL);
14511 if (lhs == error_mark_node)
14512 goto saw_error;
14514 else
14516 lhs = c_parser_cast_expression (parser, NULL).value;
14517 non_lvalue_p = !lvalue_p (lhs);
14518 lhs = c_fully_fold (lhs, false, NULL);
14519 if (lhs == error_mark_node)
14520 goto saw_error;
14521 if (non_lvalue_p)
14522 lhs = non_lvalue (lhs);
14524 if (code == NOP_EXPR)
14526 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14527 opcode. */
14528 code = OMP_ATOMIC;
14529 rhs = lhs;
14530 lhs = v;
14531 v = NULL_TREE;
14533 goto done;
14534 case OMP_ATOMIC_CAPTURE_NEW:
14535 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14537 c_parser_consume_token (parser);
14538 structured_block = true;
14540 else
14542 v = c_parser_cast_expression (parser, NULL).value;
14543 non_lvalue_p = !lvalue_p (v);
14544 v = c_fully_fold (v, false, NULL);
14545 if (v == error_mark_node)
14546 goto saw_error;
14547 if (non_lvalue_p)
14548 v = non_lvalue (v);
14549 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14550 goto saw_error;
14552 break;
14553 default:
14554 break;
14557 /* For structured_block case we don't know yet whether
14558 old or new x should be captured. */
14559 restart:
14560 eloc = c_parser_peek_token (parser)->location;
14561 expr = c_parser_cast_expression (parser, NULL);
14562 lhs = expr.value;
14563 expr = default_function_array_conversion (eloc, expr);
14564 unfolded_lhs = expr.value;
14565 lhs = c_fully_fold (lhs, false, NULL);
14566 orig_lhs = lhs;
14567 switch (TREE_CODE (lhs))
14569 case ERROR_MARK:
14570 saw_error:
14571 c_parser_skip_to_end_of_block_or_statement (parser);
14572 if (structured_block)
14574 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14575 c_parser_consume_token (parser);
14576 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14578 c_parser_skip_to_end_of_block_or_statement (parser);
14579 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14580 c_parser_consume_token (parser);
14583 return;
14585 case POSTINCREMENT_EXPR:
14586 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14587 code = OMP_ATOMIC_CAPTURE_OLD;
14588 /* FALLTHROUGH */
14589 case PREINCREMENT_EXPR:
14590 lhs = TREE_OPERAND (lhs, 0);
14591 unfolded_lhs = NULL_TREE;
14592 opcode = PLUS_EXPR;
14593 rhs = integer_one_node;
14594 break;
14596 case POSTDECREMENT_EXPR:
14597 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14598 code = OMP_ATOMIC_CAPTURE_OLD;
14599 /* FALLTHROUGH */
14600 case PREDECREMENT_EXPR:
14601 lhs = TREE_OPERAND (lhs, 0);
14602 unfolded_lhs = NULL_TREE;
14603 opcode = MINUS_EXPR;
14604 rhs = integer_one_node;
14605 break;
14607 case COMPOUND_EXPR:
14608 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14609 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14610 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14611 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14612 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14613 (TREE_OPERAND (lhs, 1), 0), 0)))
14614 == BOOLEAN_TYPE)
14615 /* Undo effects of boolean_increment for post {in,de}crement. */
14616 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14617 /* FALLTHRU */
14618 case MODIFY_EXPR:
14619 if (TREE_CODE (lhs) == MODIFY_EXPR
14620 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14622 /* Undo effects of boolean_increment. */
14623 if (integer_onep (TREE_OPERAND (lhs, 1)))
14625 /* This is pre or post increment. */
14626 rhs = TREE_OPERAND (lhs, 1);
14627 lhs = TREE_OPERAND (lhs, 0);
14628 unfolded_lhs = NULL_TREE;
14629 opcode = NOP_EXPR;
14630 if (code == OMP_ATOMIC_CAPTURE_NEW
14631 && !structured_block
14632 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14633 code = OMP_ATOMIC_CAPTURE_OLD;
14634 break;
14636 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14637 && TREE_OPERAND (lhs, 0)
14638 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14640 /* This is pre or post decrement. */
14641 rhs = TREE_OPERAND (lhs, 1);
14642 lhs = TREE_OPERAND (lhs, 0);
14643 unfolded_lhs = NULL_TREE;
14644 opcode = NOP_EXPR;
14645 if (code == OMP_ATOMIC_CAPTURE_NEW
14646 && !structured_block
14647 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14648 code = OMP_ATOMIC_CAPTURE_OLD;
14649 break;
14652 /* FALLTHRU */
14653 default:
14654 if (!lvalue_p (unfolded_lhs))
14655 lhs = non_lvalue (lhs);
14656 switch (c_parser_peek_token (parser)->type)
14658 case CPP_MULT_EQ:
14659 opcode = MULT_EXPR;
14660 break;
14661 case CPP_DIV_EQ:
14662 opcode = TRUNC_DIV_EXPR;
14663 break;
14664 case CPP_PLUS_EQ:
14665 opcode = PLUS_EXPR;
14666 break;
14667 case CPP_MINUS_EQ:
14668 opcode = MINUS_EXPR;
14669 break;
14670 case CPP_LSHIFT_EQ:
14671 opcode = LSHIFT_EXPR;
14672 break;
14673 case CPP_RSHIFT_EQ:
14674 opcode = RSHIFT_EXPR;
14675 break;
14676 case CPP_AND_EQ:
14677 opcode = BIT_AND_EXPR;
14678 break;
14679 case CPP_OR_EQ:
14680 opcode = BIT_IOR_EXPR;
14681 break;
14682 case CPP_XOR_EQ:
14683 opcode = BIT_XOR_EXPR;
14684 break;
14685 case CPP_EQ:
14686 c_parser_consume_token (parser);
14687 eloc = c_parser_peek_token (parser)->location;
14688 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14689 rhs1 = expr.value;
14690 switch (TREE_CODE (rhs1))
14692 case MULT_EXPR:
14693 case TRUNC_DIV_EXPR:
14694 case RDIV_EXPR:
14695 case PLUS_EXPR:
14696 case MINUS_EXPR:
14697 case LSHIFT_EXPR:
14698 case RSHIFT_EXPR:
14699 case BIT_AND_EXPR:
14700 case BIT_IOR_EXPR:
14701 case BIT_XOR_EXPR:
14702 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14704 opcode = TREE_CODE (rhs1);
14705 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14706 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14707 goto stmt_done;
14709 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14711 opcode = TREE_CODE (rhs1);
14712 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14713 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14714 swapped = !commutative_tree_code (opcode);
14715 goto stmt_done;
14717 break;
14718 case ERROR_MARK:
14719 goto saw_error;
14720 default:
14721 break;
14723 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14725 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14727 code = OMP_ATOMIC_CAPTURE_OLD;
14728 v = lhs;
14729 lhs = NULL_TREE;
14730 expr = default_function_array_read_conversion (eloc, expr);
14731 unfolded_lhs1 = expr.value;
14732 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14733 rhs1 = NULL_TREE;
14734 c_parser_consume_token (parser);
14735 goto restart;
14737 if (structured_block)
14739 opcode = NOP_EXPR;
14740 expr = default_function_array_read_conversion (eloc, expr);
14741 rhs = c_fully_fold (expr.value, false, NULL);
14742 rhs1 = NULL_TREE;
14743 goto stmt_done;
14746 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14747 goto saw_error;
14748 default:
14749 c_parser_error (parser,
14750 "invalid operator for %<#pragma omp atomic%>");
14751 goto saw_error;
14754 /* Arrange to pass the location of the assignment operator to
14755 c_finish_omp_atomic. */
14756 loc = c_parser_peek_token (parser)->location;
14757 c_parser_consume_token (parser);
14758 eloc = c_parser_peek_token (parser)->location;
14759 expr = c_parser_expression (parser);
14760 expr = default_function_array_read_conversion (eloc, expr);
14761 rhs = expr.value;
14762 rhs = c_fully_fold (rhs, false, NULL);
14763 break;
14765 stmt_done:
14766 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14768 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14769 goto saw_error;
14770 v = c_parser_cast_expression (parser, NULL).value;
14771 non_lvalue_p = !lvalue_p (v);
14772 v = c_fully_fold (v, false, NULL);
14773 if (v == error_mark_node)
14774 goto saw_error;
14775 if (non_lvalue_p)
14776 v = non_lvalue (v);
14777 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14778 goto saw_error;
14779 eloc = c_parser_peek_token (parser)->location;
14780 expr = c_parser_cast_expression (parser, NULL);
14781 lhs1 = expr.value;
14782 expr = default_function_array_read_conversion (eloc, expr);
14783 unfolded_lhs1 = expr.value;
14784 lhs1 = c_fully_fold (lhs1, false, NULL);
14785 if (lhs1 == error_mark_node)
14786 goto saw_error;
14787 if (!lvalue_p (unfolded_lhs1))
14788 lhs1 = non_lvalue (lhs1);
14790 if (structured_block)
14792 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14793 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14795 done:
14796 if (unfolded_lhs && unfolded_lhs1
14797 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14799 error ("%<#pragma omp atomic capture%> uses two different "
14800 "expressions for memory");
14801 stmt = error_mark_node;
14803 else
14804 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14805 swapped, seq_cst);
14806 if (stmt != error_mark_node)
14807 add_stmt (stmt);
14809 if (!structured_block)
14810 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14814 /* OpenMP 2.5:
14815 # pragma omp barrier new-line
14818 static void
14819 c_parser_omp_barrier (c_parser *parser)
14821 location_t loc = c_parser_peek_token (parser)->location;
14822 c_parser_consume_pragma (parser);
14823 c_parser_skip_to_pragma_eol (parser);
14825 c_finish_omp_barrier (loc);
14828 /* OpenMP 2.5:
14829 # pragma omp critical [(name)] new-line
14830 structured-block
14832 OpenMP 4.5:
14833 # pragma omp critical [(name) [hint(expression)]] new-line
14835 LOC is the location of the #pragma itself. */
14837 #define OMP_CRITICAL_CLAUSE_MASK \
14838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14840 static tree
14841 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
14843 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14845 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14847 c_parser_consume_token (parser);
14848 if (c_parser_next_token_is (parser, CPP_NAME))
14850 name = c_parser_peek_token (parser)->value;
14851 c_parser_consume_token (parser);
14852 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14854 else
14855 c_parser_error (parser, "expected identifier");
14857 clauses = c_parser_omp_all_clauses (parser,
14858 OMP_CRITICAL_CLAUSE_MASK,
14859 "#pragma omp critical");
14861 else
14863 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14864 c_parser_error (parser, "expected %<(%> or end of line");
14865 c_parser_skip_to_pragma_eol (parser);
14868 stmt = c_parser_omp_structured_block (parser, if_p);
14869 return c_finish_omp_critical (loc, stmt, name, clauses);
14872 /* OpenMP 2.5:
14873 # pragma omp flush flush-vars[opt] new-line
14875 flush-vars:
14876 ( variable-list ) */
14878 static void
14879 c_parser_omp_flush (c_parser *parser)
14881 location_t loc = c_parser_peek_token (parser)->location;
14882 c_parser_consume_pragma (parser);
14883 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14884 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14885 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14886 c_parser_error (parser, "expected %<(%> or end of line");
14887 c_parser_skip_to_pragma_eol (parser);
14889 c_finish_omp_flush (loc);
14892 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14893 The real trick here is to determine the loop control variable early
14894 so that we can push a new decl if necessary to make it private.
14895 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14896 respectively. */
14898 static tree
14899 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14900 tree clauses, tree *cclauses, bool *if_p)
14902 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14903 tree declv, condv, incrv, initv, ret = NULL_TREE;
14904 tree pre_body = NULL_TREE, this_pre_body;
14905 tree ordered_cl = NULL_TREE;
14906 bool fail = false, open_brace_parsed = false;
14907 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14908 location_t for_loc;
14909 bool tiling = false;
14910 vec<tree, va_gc> *for_block = make_tree_vector ();
14912 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14913 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14914 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14915 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
14917 tiling = true;
14918 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
14920 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14921 && OMP_CLAUSE_ORDERED_EXPR (cl))
14923 ordered_cl = cl;
14924 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14927 if (ordered && ordered < collapse)
14929 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14930 "%<ordered%> clause parameter is less than %<collapse%>");
14931 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14932 = build_int_cst (NULL_TREE, collapse);
14933 ordered = collapse;
14935 if (ordered)
14937 for (tree *pc = &clauses; *pc; )
14938 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14940 error_at (OMP_CLAUSE_LOCATION (*pc),
14941 "%<linear%> clause may not be specified together "
14942 "with %<ordered%> clause with a parameter");
14943 *pc = OMP_CLAUSE_CHAIN (*pc);
14945 else
14946 pc = &OMP_CLAUSE_CHAIN (*pc);
14949 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
14950 count = ordered ? ordered : collapse;
14952 declv = make_tree_vec (count);
14953 initv = make_tree_vec (count);
14954 condv = make_tree_vec (count);
14955 incrv = make_tree_vec (count);
14957 if (code != CILK_FOR
14958 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14960 c_parser_error (parser, "for statement expected");
14961 return NULL;
14963 if (code == CILK_FOR
14964 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14966 c_parser_error (parser, "_Cilk_for statement expected");
14967 return NULL;
14969 for_loc = c_parser_peek_token (parser)->location;
14970 c_parser_consume_token (parser);
14972 for (i = 0; i < count; i++)
14974 int bracecount = 0;
14976 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14977 goto pop_scopes;
14979 /* Parse the initialization declaration or expression. */
14980 if (c_parser_next_tokens_start_declaration (parser))
14982 if (i > 0)
14983 vec_safe_push (for_block, c_begin_compound_stmt (true));
14984 this_pre_body = push_stmt_list ();
14985 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14986 NULL, vNULL);
14987 if (this_pre_body)
14989 this_pre_body = pop_stmt_list (this_pre_body);
14990 if (pre_body)
14992 tree t = pre_body;
14993 pre_body = push_stmt_list ();
14994 add_stmt (t);
14995 add_stmt (this_pre_body);
14996 pre_body = pop_stmt_list (pre_body);
14998 else
14999 pre_body = this_pre_body;
15001 decl = check_for_loop_decls (for_loc, flag_isoc99);
15002 if (decl == NULL)
15003 goto error_init;
15004 if (DECL_INITIAL (decl) == error_mark_node)
15005 decl = error_mark_node;
15006 init = decl;
15008 else if (c_parser_next_token_is (parser, CPP_NAME)
15009 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15011 struct c_expr decl_exp;
15012 struct c_expr init_exp;
15013 location_t init_loc;
15015 decl_exp = c_parser_postfix_expression (parser);
15016 decl = decl_exp.value;
15018 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15020 init_loc = c_parser_peek_token (parser)->location;
15021 init_exp = c_parser_expr_no_commas (parser, NULL);
15022 init_exp = default_function_array_read_conversion (init_loc,
15023 init_exp);
15024 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15025 NOP_EXPR, init_loc, init_exp.value,
15026 init_exp.original_type);
15027 init = c_process_expr_stmt (init_loc, init);
15029 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15031 else
15033 error_init:
15034 c_parser_error (parser,
15035 "expected iteration declaration or initialization");
15036 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15037 "expected %<)%>");
15038 fail = true;
15039 goto parse_next;
15042 /* Parse the loop condition. */
15043 cond = NULL_TREE;
15044 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15046 location_t cond_loc = c_parser_peek_token (parser)->location;
15047 struct c_expr cond_expr
15048 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15050 cond = cond_expr.value;
15051 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15052 cond = c_fully_fold (cond, false, NULL);
15053 switch (cond_expr.original_code)
15055 case GT_EXPR:
15056 case GE_EXPR:
15057 case LT_EXPR:
15058 case LE_EXPR:
15059 break;
15060 case NE_EXPR:
15061 if (code == CILK_SIMD || code == CILK_FOR)
15062 break;
15063 /* FALLTHRU. */
15064 default:
15065 /* Can't be cond = error_mark_node, because we want to preserve
15066 the location until c_finish_omp_for. */
15067 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15068 break;
15070 protected_set_expr_location (cond, cond_loc);
15072 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15074 /* Parse the increment expression. */
15075 incr = NULL_TREE;
15076 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15078 location_t incr_loc = c_parser_peek_token (parser)->location;
15080 incr = c_process_expr_stmt (incr_loc,
15081 c_parser_expression (parser).value);
15083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15085 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15086 fail = true;
15087 else
15089 TREE_VEC_ELT (declv, i) = decl;
15090 TREE_VEC_ELT (initv, i) = init;
15091 TREE_VEC_ELT (condv, i) = cond;
15092 TREE_VEC_ELT (incrv, i) = incr;
15095 parse_next:
15096 if (i == count - 1)
15097 break;
15099 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15100 in between the collapsed for loops to be still considered perfectly
15101 nested. Hopefully the final version clarifies this.
15102 For now handle (multiple) {'s and empty statements. */
15105 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15107 c_parser_consume_token (parser);
15108 break;
15110 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15112 c_parser_consume_token (parser);
15113 bracecount++;
15115 else if (bracecount
15116 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15117 c_parser_consume_token (parser);
15118 else
15120 c_parser_error (parser, "not enough perfectly nested loops");
15121 if (bracecount)
15123 open_brace_parsed = true;
15124 bracecount--;
15126 fail = true;
15127 count = 0;
15128 break;
15131 while (1);
15133 nbraces += bracecount;
15136 if (nbraces)
15137 if_p = NULL;
15139 save_break = c_break_label;
15140 if (code == CILK_SIMD)
15141 c_break_label = build_int_cst (size_type_node, 2);
15142 else
15143 c_break_label = size_one_node;
15144 save_cont = c_cont_label;
15145 c_cont_label = NULL_TREE;
15146 body = push_stmt_list ();
15148 if (open_brace_parsed)
15150 location_t here = c_parser_peek_token (parser)->location;
15151 stmt = c_begin_compound_stmt (true);
15152 c_parser_compound_statement_nostart (parser);
15153 add_stmt (c_end_compound_stmt (here, stmt, true));
15155 else
15156 add_stmt (c_parser_c99_block_statement (parser, if_p));
15157 if (c_cont_label)
15159 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15160 SET_EXPR_LOCATION (t, loc);
15161 add_stmt (t);
15164 body = pop_stmt_list (body);
15165 c_break_label = save_break;
15166 c_cont_label = save_cont;
15168 while (nbraces)
15170 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15172 c_parser_consume_token (parser);
15173 nbraces--;
15175 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15176 c_parser_consume_token (parser);
15177 else
15179 c_parser_error (parser, "collapsed loops not perfectly nested");
15180 while (nbraces)
15182 location_t here = c_parser_peek_token (parser)->location;
15183 stmt = c_begin_compound_stmt (true);
15184 add_stmt (body);
15185 c_parser_compound_statement_nostart (parser);
15186 body = c_end_compound_stmt (here, stmt, true);
15187 nbraces--;
15189 goto pop_scopes;
15193 /* Only bother calling c_finish_omp_for if we haven't already generated
15194 an error from the initialization parsing. */
15195 if (!fail)
15197 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15198 incrv, body, pre_body);
15200 /* Check for iterators appearing in lb, b or incr expressions. */
15201 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15202 stmt = NULL_TREE;
15204 if (stmt)
15206 add_stmt (stmt);
15208 if (cclauses != NULL
15209 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15211 tree *c;
15212 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15213 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15214 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15215 c = &OMP_CLAUSE_CHAIN (*c);
15216 else
15218 for (i = 0; i < count; i++)
15219 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15220 break;
15221 if (i == count)
15222 c = &OMP_CLAUSE_CHAIN (*c);
15223 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15225 error_at (loc,
15226 "iteration variable %qD should not be firstprivate",
15227 OMP_CLAUSE_DECL (*c));
15228 *c = OMP_CLAUSE_CHAIN (*c);
15230 else
15232 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15233 tree l = *c;
15234 *c = OMP_CLAUSE_CHAIN (*c);
15235 if (code == OMP_SIMD)
15237 OMP_CLAUSE_CHAIN (l)
15238 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15239 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15241 else
15243 OMP_CLAUSE_CHAIN (l) = clauses;
15244 clauses = l;
15249 OMP_FOR_CLAUSES (stmt) = clauses;
15251 ret = stmt;
15253 pop_scopes:
15254 while (!for_block->is_empty ())
15256 /* FIXME diagnostics: LOC below should be the actual location of
15257 this particular for block. We need to build a list of
15258 locations to go along with FOR_BLOCK. */
15259 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15260 add_stmt (stmt);
15262 release_tree_vector (for_block);
15263 return ret;
15266 /* Helper function for OpenMP parsing, split clauses and call
15267 finish_omp_clauses on each of the set of clauses afterwards. */
15269 static void
15270 omp_split_clauses (location_t loc, enum tree_code code,
15271 omp_clause_mask mask, tree clauses, tree *cclauses)
15273 int i;
15274 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15275 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15276 if (cclauses[i])
15277 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15280 /* OpenMP 4.0:
15281 #pragma omp simd simd-clause[optseq] new-line
15282 for-loop
15284 LOC is the location of the #pragma token.
15287 #define OMP_SIMD_CLAUSE_MASK \
15288 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15297 static tree
15298 c_parser_omp_simd (location_t loc, c_parser *parser,
15299 char *p_name, omp_clause_mask mask, tree *cclauses,
15300 bool *if_p)
15302 tree block, clauses, ret;
15304 strcat (p_name, " simd");
15305 mask |= OMP_SIMD_CLAUSE_MASK;
15307 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15308 if (cclauses)
15310 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15311 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15312 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15313 OMP_CLAUSE_ORDERED);
15314 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15316 error_at (OMP_CLAUSE_LOCATION (c),
15317 "%<ordered%> clause with parameter may not be specified "
15318 "on %qs construct", p_name);
15319 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15323 block = c_begin_compound_stmt (true);
15324 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15325 block = c_end_compound_stmt (loc, block, true);
15326 add_stmt (block);
15328 return ret;
15331 /* OpenMP 2.5:
15332 #pragma omp for for-clause[optseq] new-line
15333 for-loop
15335 OpenMP 4.0:
15336 #pragma omp for simd for-simd-clause[optseq] new-line
15337 for-loop
15339 LOC is the location of the #pragma token.
15342 #define OMP_FOR_CLAUSE_MASK \
15343 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15353 static tree
15354 c_parser_omp_for (location_t loc, c_parser *parser,
15355 char *p_name, omp_clause_mask mask, tree *cclauses,
15356 bool *if_p)
15358 tree block, clauses, ret;
15360 strcat (p_name, " for");
15361 mask |= OMP_FOR_CLAUSE_MASK;
15362 /* parallel for{, simd} disallows nowait clause, but for
15363 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15364 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15365 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15366 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15367 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15368 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15370 if (c_parser_next_token_is (parser, CPP_NAME))
15372 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15374 if (strcmp (p, "simd") == 0)
15376 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15377 if (cclauses == NULL)
15378 cclauses = cclauses_buf;
15380 c_parser_consume_token (parser);
15381 if (!flag_openmp) /* flag_openmp_simd */
15382 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15383 if_p);
15384 block = c_begin_compound_stmt (true);
15385 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15386 block = c_end_compound_stmt (loc, block, true);
15387 if (ret == NULL_TREE)
15388 return ret;
15389 ret = make_node (OMP_FOR);
15390 TREE_TYPE (ret) = void_type_node;
15391 OMP_FOR_BODY (ret) = block;
15392 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15393 SET_EXPR_LOCATION (ret, loc);
15394 add_stmt (ret);
15395 return ret;
15398 if (!flag_openmp) /* flag_openmp_simd */
15400 c_parser_skip_to_pragma_eol (parser, false);
15401 return NULL_TREE;
15404 /* Composite distribute parallel for disallows linear clause. */
15405 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15406 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15408 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15409 if (cclauses)
15411 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15412 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15415 block = c_begin_compound_stmt (true);
15416 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15417 block = c_end_compound_stmt (loc, block, true);
15418 add_stmt (block);
15420 return ret;
15423 /* OpenMP 2.5:
15424 # pragma omp master new-line
15425 structured-block
15427 LOC is the location of the #pragma token.
15430 static tree
15431 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15433 c_parser_skip_to_pragma_eol (parser);
15434 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15435 if_p));
15438 /* OpenMP 2.5:
15439 # pragma omp ordered new-line
15440 structured-block
15442 OpenMP 4.5:
15443 # pragma omp ordered ordered-clauses new-line
15444 structured-block
15446 # pragma omp ordered depend-clauses new-line */
15448 #define OMP_ORDERED_CLAUSE_MASK \
15449 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15452 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15453 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15455 static bool
15456 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15457 bool *if_p)
15459 location_t loc = c_parser_peek_token (parser)->location;
15460 c_parser_consume_pragma (parser);
15462 if (context != pragma_stmt && context != pragma_compound)
15464 c_parser_error (parser, "expected declaration specifiers");
15465 c_parser_skip_to_pragma_eol (parser, false);
15466 return false;
15469 if (c_parser_next_token_is (parser, CPP_NAME))
15471 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15473 if (!strcmp ("depend", p))
15475 if (context == pragma_stmt)
15477 error_at (loc,
15478 "%<#pragma omp ordered%> with %<depend> clause may "
15479 "only be used in compound statements");
15480 c_parser_skip_to_pragma_eol (parser, false);
15481 return false;
15484 tree clauses
15485 = c_parser_omp_all_clauses (parser,
15486 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15487 "#pragma omp ordered");
15488 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15489 return false;
15493 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15494 "#pragma omp ordered");
15495 c_finish_omp_ordered (loc, clauses,
15496 c_parser_omp_structured_block (parser, if_p));
15497 return true;
15500 /* OpenMP 2.5:
15502 section-scope:
15503 { section-sequence }
15505 section-sequence:
15506 section-directive[opt] structured-block
15507 section-sequence section-directive structured-block
15509 SECTIONS_LOC is the location of the #pragma omp sections. */
15511 static tree
15512 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15514 tree stmt, substmt;
15515 bool error_suppress = false;
15516 location_t loc;
15518 loc = c_parser_peek_token (parser)->location;
15519 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15521 /* Avoid skipping until the end of the block. */
15522 parser->error = false;
15523 return NULL_TREE;
15526 stmt = push_stmt_list ();
15528 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15530 substmt = c_parser_omp_structured_block (parser, NULL);
15531 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15532 SET_EXPR_LOCATION (substmt, loc);
15533 add_stmt (substmt);
15536 while (1)
15538 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15539 break;
15540 if (c_parser_next_token_is (parser, CPP_EOF))
15541 break;
15543 loc = c_parser_peek_token (parser)->location;
15544 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15546 c_parser_consume_pragma (parser);
15547 c_parser_skip_to_pragma_eol (parser);
15548 error_suppress = false;
15550 else if (!error_suppress)
15552 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15553 error_suppress = true;
15556 substmt = c_parser_omp_structured_block (parser, NULL);
15557 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15558 SET_EXPR_LOCATION (substmt, loc);
15559 add_stmt (substmt);
15561 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15562 "expected %<#pragma omp section%> or %<}%>");
15564 substmt = pop_stmt_list (stmt);
15566 stmt = make_node (OMP_SECTIONS);
15567 SET_EXPR_LOCATION (stmt, sections_loc);
15568 TREE_TYPE (stmt) = void_type_node;
15569 OMP_SECTIONS_BODY (stmt) = substmt;
15571 return add_stmt (stmt);
15574 /* OpenMP 2.5:
15575 # pragma omp sections sections-clause[optseq] newline
15576 sections-scope
15578 LOC is the location of the #pragma token.
15581 #define OMP_SECTIONS_CLAUSE_MASK \
15582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15588 static tree
15589 c_parser_omp_sections (location_t loc, c_parser *parser,
15590 char *p_name, omp_clause_mask mask, tree *cclauses)
15592 tree block, clauses, ret;
15594 strcat (p_name, " sections");
15595 mask |= OMP_SECTIONS_CLAUSE_MASK;
15596 if (cclauses)
15597 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15599 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15600 if (cclauses)
15602 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15603 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15606 block = c_begin_compound_stmt (true);
15607 ret = c_parser_omp_sections_scope (loc, parser);
15608 if (ret)
15609 OMP_SECTIONS_CLAUSES (ret) = clauses;
15610 block = c_end_compound_stmt (loc, block, true);
15611 add_stmt (block);
15613 return ret;
15616 /* OpenMP 2.5:
15617 # pragma omp parallel parallel-clause[optseq] new-line
15618 structured-block
15619 # pragma omp parallel for parallel-for-clause[optseq] new-line
15620 structured-block
15621 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15622 structured-block
15624 OpenMP 4.0:
15625 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15626 structured-block
15628 LOC is the location of the #pragma token.
15631 #define OMP_PARALLEL_CLAUSE_MASK \
15632 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15642 static tree
15643 c_parser_omp_parallel (location_t loc, c_parser *parser,
15644 char *p_name, omp_clause_mask mask, tree *cclauses,
15645 bool *if_p)
15647 tree stmt, clauses, block;
15649 strcat (p_name, " parallel");
15650 mask |= OMP_PARALLEL_CLAUSE_MASK;
15651 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15652 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15653 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15654 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15656 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15658 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15659 if (cclauses == NULL)
15660 cclauses = cclauses_buf;
15662 c_parser_consume_token (parser);
15663 if (!flag_openmp) /* flag_openmp_simd */
15664 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15665 block = c_begin_omp_parallel ();
15666 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15667 stmt
15668 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15669 block);
15670 if (ret == NULL_TREE)
15671 return ret;
15672 OMP_PARALLEL_COMBINED (stmt) = 1;
15673 return stmt;
15675 /* When combined with distribute, parallel has to be followed by for.
15676 #pragma omp target parallel is allowed though. */
15677 else if (cclauses
15678 && (mask & (OMP_CLAUSE_MASK_1
15679 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15681 error_at (loc, "expected %<for%> after %qs", p_name);
15682 c_parser_skip_to_pragma_eol (parser);
15683 return NULL_TREE;
15685 else if (!flag_openmp) /* flag_openmp_simd */
15687 c_parser_skip_to_pragma_eol (parser, false);
15688 return NULL_TREE;
15690 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15692 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15693 if (strcmp (p, "sections") == 0)
15695 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15696 if (cclauses == NULL)
15697 cclauses = cclauses_buf;
15699 c_parser_consume_token (parser);
15700 block = c_begin_omp_parallel ();
15701 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15702 stmt = c_finish_omp_parallel (loc,
15703 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15704 block);
15705 OMP_PARALLEL_COMBINED (stmt) = 1;
15706 return stmt;
15710 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15711 if (cclauses)
15713 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15714 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15717 block = c_begin_omp_parallel ();
15718 c_parser_statement (parser, if_p);
15719 stmt = c_finish_omp_parallel (loc, clauses, block);
15721 return stmt;
15724 /* OpenMP 2.5:
15725 # pragma omp single single-clause[optseq] new-line
15726 structured-block
15728 LOC is the location of the #pragma.
15731 #define OMP_SINGLE_CLAUSE_MASK \
15732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15737 static tree
15738 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15740 tree stmt = make_node (OMP_SINGLE);
15741 SET_EXPR_LOCATION (stmt, loc);
15742 TREE_TYPE (stmt) = void_type_node;
15744 OMP_SINGLE_CLAUSES (stmt)
15745 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15746 "#pragma omp single");
15747 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15749 return add_stmt (stmt);
15752 /* OpenMP 3.0:
15753 # pragma omp task task-clause[optseq] new-line
15755 LOC is the location of the #pragma.
15758 #define OMP_TASK_CLAUSE_MASK \
15759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15770 static tree
15771 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
15773 tree clauses, block;
15775 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15776 "#pragma omp task");
15778 block = c_begin_omp_task ();
15779 c_parser_statement (parser, if_p);
15780 return c_finish_omp_task (loc, clauses, block);
15783 /* OpenMP 3.0:
15784 # pragma omp taskwait new-line
15787 static void
15788 c_parser_omp_taskwait (c_parser *parser)
15790 location_t loc = c_parser_peek_token (parser)->location;
15791 c_parser_consume_pragma (parser);
15792 c_parser_skip_to_pragma_eol (parser);
15794 c_finish_omp_taskwait (loc);
15797 /* OpenMP 3.1:
15798 # pragma omp taskyield new-line
15801 static void
15802 c_parser_omp_taskyield (c_parser *parser)
15804 location_t loc = c_parser_peek_token (parser)->location;
15805 c_parser_consume_pragma (parser);
15806 c_parser_skip_to_pragma_eol (parser);
15808 c_finish_omp_taskyield (loc);
15811 /* OpenMP 4.0:
15812 # pragma omp taskgroup new-line
15815 static tree
15816 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
15818 location_t loc = c_parser_peek_token (parser)->location;
15819 c_parser_skip_to_pragma_eol (parser);
15820 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
15821 if_p));
15824 /* OpenMP 4.0:
15825 # pragma omp cancel cancel-clause[optseq] new-line
15827 LOC is the location of the #pragma.
15830 #define OMP_CANCEL_CLAUSE_MASK \
15831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15837 static void
15838 c_parser_omp_cancel (c_parser *parser)
15840 location_t loc = c_parser_peek_token (parser)->location;
15842 c_parser_consume_pragma (parser);
15843 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15844 "#pragma omp cancel");
15846 c_finish_omp_cancel (loc, clauses);
15849 /* OpenMP 4.0:
15850 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15852 LOC is the location of the #pragma.
15855 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15856 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15861 static void
15862 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
15864 location_t loc = c_parser_peek_token (parser)->location;
15865 tree clauses;
15866 bool point_seen = false;
15868 c_parser_consume_pragma (parser);
15869 if (c_parser_next_token_is (parser, CPP_NAME))
15871 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15872 if (strcmp (p, "point") == 0)
15874 c_parser_consume_token (parser);
15875 point_seen = true;
15878 if (!point_seen)
15880 c_parser_error (parser, "expected %<point%>");
15881 c_parser_skip_to_pragma_eol (parser);
15882 return;
15885 if (context != pragma_compound)
15887 if (context == pragma_stmt)
15888 error_at (loc, "%<#pragma omp cancellation point%> may only be used in"
15889 " compound statements");
15890 else
15891 c_parser_error (parser, "expected declaration specifiers");
15892 c_parser_skip_to_pragma_eol (parser, false);
15893 return;
15896 clauses
15897 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15898 "#pragma omp cancellation point");
15900 c_finish_omp_cancellation_point (loc, clauses);
15903 /* OpenMP 4.0:
15904 #pragma omp distribute distribute-clause[optseq] new-line
15905 for-loop */
15907 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15908 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15914 static tree
15915 c_parser_omp_distribute (location_t loc, c_parser *parser,
15916 char *p_name, omp_clause_mask mask, tree *cclauses,
15917 bool *if_p)
15919 tree clauses, block, ret;
15921 strcat (p_name, " distribute");
15922 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15924 if (c_parser_next_token_is (parser, CPP_NAME))
15926 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15927 bool simd = false;
15928 bool parallel = false;
15930 if (strcmp (p, "simd") == 0)
15931 simd = true;
15932 else
15933 parallel = strcmp (p, "parallel") == 0;
15934 if (parallel || simd)
15936 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15937 if (cclauses == NULL)
15938 cclauses = cclauses_buf;
15939 c_parser_consume_token (parser);
15940 if (!flag_openmp) /* flag_openmp_simd */
15942 if (simd)
15943 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15944 if_p);
15945 else
15946 return c_parser_omp_parallel (loc, parser, p_name, mask,
15947 cclauses, if_p);
15949 block = c_begin_compound_stmt (true);
15950 if (simd)
15951 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15952 if_p);
15953 else
15954 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
15955 if_p);
15956 block = c_end_compound_stmt (loc, block, true);
15957 if (ret == NULL)
15958 return ret;
15959 ret = make_node (OMP_DISTRIBUTE);
15960 TREE_TYPE (ret) = void_type_node;
15961 OMP_FOR_BODY (ret) = block;
15962 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15963 SET_EXPR_LOCATION (ret, loc);
15964 add_stmt (ret);
15965 return ret;
15968 if (!flag_openmp) /* flag_openmp_simd */
15970 c_parser_skip_to_pragma_eol (parser, false);
15971 return NULL_TREE;
15974 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15975 if (cclauses)
15977 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15978 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15981 block = c_begin_compound_stmt (true);
15982 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
15983 if_p);
15984 block = c_end_compound_stmt (loc, block, true);
15985 add_stmt (block);
15987 return ret;
15990 /* OpenMP 4.0:
15991 # pragma omp teams teams-clause[optseq] new-line
15992 structured-block */
15994 #define OMP_TEAMS_CLAUSE_MASK \
15995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16003 static tree
16004 c_parser_omp_teams (location_t loc, c_parser *parser,
16005 char *p_name, omp_clause_mask mask, tree *cclauses,
16006 bool *if_p)
16008 tree clauses, block, ret;
16010 strcat (p_name, " teams");
16011 mask |= OMP_TEAMS_CLAUSE_MASK;
16013 if (c_parser_next_token_is (parser, CPP_NAME))
16015 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16016 if (strcmp (p, "distribute") == 0)
16018 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16019 if (cclauses == NULL)
16020 cclauses = cclauses_buf;
16022 c_parser_consume_token (parser);
16023 if (!flag_openmp) /* flag_openmp_simd */
16024 return c_parser_omp_distribute (loc, parser, p_name, mask,
16025 cclauses, if_p);
16026 block = c_begin_compound_stmt (true);
16027 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16028 if_p);
16029 block = c_end_compound_stmt (loc, block, true);
16030 if (ret == NULL)
16031 return ret;
16032 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16033 ret = make_node (OMP_TEAMS);
16034 TREE_TYPE (ret) = void_type_node;
16035 OMP_TEAMS_CLAUSES (ret) = clauses;
16036 OMP_TEAMS_BODY (ret) = block;
16037 OMP_TEAMS_COMBINED (ret) = 1;
16038 return add_stmt (ret);
16041 if (!flag_openmp) /* flag_openmp_simd */
16043 c_parser_skip_to_pragma_eol (parser, false);
16044 return NULL_TREE;
16047 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16048 if (cclauses)
16050 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16051 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16054 tree stmt = make_node (OMP_TEAMS);
16055 TREE_TYPE (stmt) = void_type_node;
16056 OMP_TEAMS_CLAUSES (stmt) = clauses;
16057 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16059 return add_stmt (stmt);
16062 /* OpenMP 4.0:
16063 # pragma omp target data target-data-clause[optseq] new-line
16064 structured-block */
16066 #define OMP_TARGET_DATA_CLAUSE_MASK \
16067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16072 static tree
16073 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16075 tree clauses
16076 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16077 "#pragma omp target data");
16078 int map_seen = 0;
16079 for (tree *pc = &clauses; *pc;)
16081 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16082 switch (OMP_CLAUSE_MAP_KIND (*pc))
16084 case GOMP_MAP_TO:
16085 case GOMP_MAP_ALWAYS_TO:
16086 case GOMP_MAP_FROM:
16087 case GOMP_MAP_ALWAYS_FROM:
16088 case GOMP_MAP_TOFROM:
16089 case GOMP_MAP_ALWAYS_TOFROM:
16090 case GOMP_MAP_ALLOC:
16091 map_seen = 3;
16092 break;
16093 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16094 case GOMP_MAP_ALWAYS_POINTER:
16095 break;
16096 default:
16097 map_seen |= 1;
16098 error_at (OMP_CLAUSE_LOCATION (*pc),
16099 "%<#pragma omp target data%> with map-type other "
16100 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16101 "on %<map%> clause");
16102 *pc = OMP_CLAUSE_CHAIN (*pc);
16103 continue;
16105 pc = &OMP_CLAUSE_CHAIN (*pc);
16108 if (map_seen != 3)
16110 if (map_seen == 0)
16111 error_at (loc,
16112 "%<#pragma omp target data%> must contain at least "
16113 "one %<map%> clause");
16114 return NULL_TREE;
16117 tree stmt = make_node (OMP_TARGET_DATA);
16118 TREE_TYPE (stmt) = void_type_node;
16119 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16120 keep_next_level ();
16121 tree block = c_begin_compound_stmt (true);
16122 add_stmt (c_parser_omp_structured_block (parser, if_p));
16123 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16125 SET_EXPR_LOCATION (stmt, loc);
16126 return add_stmt (stmt);
16129 /* OpenMP 4.0:
16130 # pragma omp target update target-update-clause[optseq] new-line */
16132 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16140 static bool
16141 c_parser_omp_target_update (location_t loc, c_parser *parser,
16142 enum pragma_context context)
16144 if (context == pragma_stmt)
16146 error_at (loc,
16147 "%<#pragma omp target update%> may only be "
16148 "used in compound statements");
16149 c_parser_skip_to_pragma_eol (parser, false);
16150 return false;
16153 tree clauses
16154 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16155 "#pragma omp target update");
16156 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16157 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16159 error_at (loc,
16160 "%<#pragma omp target update%> must contain at least one "
16161 "%<from%> or %<to%> clauses");
16162 return false;
16165 tree stmt = make_node (OMP_TARGET_UPDATE);
16166 TREE_TYPE (stmt) = void_type_node;
16167 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16168 SET_EXPR_LOCATION (stmt, loc);
16169 add_stmt (stmt);
16170 return false;
16173 /* OpenMP 4.5:
16174 # pragma omp target enter data target-data-clause[optseq] new-line */
16176 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16177 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16183 static tree
16184 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16185 enum pragma_context context)
16187 bool data_seen = false;
16188 if (c_parser_next_token_is (parser, CPP_NAME))
16190 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16191 if (strcmp (p, "data") == 0)
16193 c_parser_consume_token (parser);
16194 data_seen = true;
16197 if (!data_seen)
16199 c_parser_error (parser, "expected %<data%>");
16200 c_parser_skip_to_pragma_eol (parser);
16201 return NULL_TREE;
16204 if (context == pragma_stmt)
16206 error_at (loc,
16207 "%<#pragma omp target enter data%> may only be "
16208 "used in compound statements");
16209 c_parser_skip_to_pragma_eol (parser, false);
16210 return NULL_TREE;
16213 tree clauses
16214 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16215 "#pragma omp target enter data");
16216 int map_seen = 0;
16217 for (tree *pc = &clauses; *pc;)
16219 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16220 switch (OMP_CLAUSE_MAP_KIND (*pc))
16222 case GOMP_MAP_TO:
16223 case GOMP_MAP_ALWAYS_TO:
16224 case GOMP_MAP_ALLOC:
16225 map_seen = 3;
16226 break;
16227 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16228 case GOMP_MAP_ALWAYS_POINTER:
16229 break;
16230 default:
16231 map_seen |= 1;
16232 error_at (OMP_CLAUSE_LOCATION (*pc),
16233 "%<#pragma omp target enter data%> with map-type other "
16234 "than %<to%> or %<alloc%> on %<map%> clause");
16235 *pc = OMP_CLAUSE_CHAIN (*pc);
16236 continue;
16238 pc = &OMP_CLAUSE_CHAIN (*pc);
16241 if (map_seen != 3)
16243 if (map_seen == 0)
16244 error_at (loc,
16245 "%<#pragma omp target enter data%> must contain at least "
16246 "one %<map%> clause");
16247 return NULL_TREE;
16250 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16251 TREE_TYPE (stmt) = void_type_node;
16252 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16253 SET_EXPR_LOCATION (stmt, loc);
16254 add_stmt (stmt);
16255 return stmt;
16258 /* OpenMP 4.5:
16259 # pragma omp target exit data target-data-clause[optseq] new-line */
16261 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16262 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16268 static tree
16269 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16270 enum pragma_context context)
16272 bool data_seen = false;
16273 if (c_parser_next_token_is (parser, CPP_NAME))
16275 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16276 if (strcmp (p, "data") == 0)
16278 c_parser_consume_token (parser);
16279 data_seen = true;
16282 if (!data_seen)
16284 c_parser_error (parser, "expected %<data%>");
16285 c_parser_skip_to_pragma_eol (parser);
16286 return NULL_TREE;
16289 if (context == pragma_stmt)
16291 error_at (loc,
16292 "%<#pragma omp target exit data%> may only be "
16293 "used in compound statements");
16294 c_parser_skip_to_pragma_eol (parser, false);
16295 return NULL_TREE;
16298 tree clauses
16299 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16300 "#pragma omp target exit data");
16302 int map_seen = 0;
16303 for (tree *pc = &clauses; *pc;)
16305 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16306 switch (OMP_CLAUSE_MAP_KIND (*pc))
16308 case GOMP_MAP_FROM:
16309 case GOMP_MAP_ALWAYS_FROM:
16310 case GOMP_MAP_RELEASE:
16311 case GOMP_MAP_DELETE:
16312 map_seen = 3;
16313 break;
16314 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16315 case GOMP_MAP_ALWAYS_POINTER:
16316 break;
16317 default:
16318 map_seen |= 1;
16319 error_at (OMP_CLAUSE_LOCATION (*pc),
16320 "%<#pragma omp target exit data%> with map-type other "
16321 "than %<from%>, %<release> or %<delete%> on %<map%>"
16322 " clause");
16323 *pc = OMP_CLAUSE_CHAIN (*pc);
16324 continue;
16326 pc = &OMP_CLAUSE_CHAIN (*pc);
16329 if (map_seen != 3)
16331 if (map_seen == 0)
16332 error_at (loc,
16333 "%<#pragma omp target exit data%> must contain at least one "
16334 "%<map%> clause");
16335 return NULL_TREE;
16338 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16339 TREE_TYPE (stmt) = void_type_node;
16340 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16341 SET_EXPR_LOCATION (stmt, loc);
16342 add_stmt (stmt);
16343 return stmt;
16346 /* OpenMP 4.0:
16347 # pragma omp target target-clause[optseq] new-line
16348 structured-block */
16350 #define OMP_TARGET_CLAUSE_MASK \
16351 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16361 static bool
16362 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16364 location_t loc = c_parser_peek_token (parser)->location;
16365 c_parser_consume_pragma (parser);
16366 tree *pc = NULL, stmt, block;
16368 if (context != pragma_stmt && context != pragma_compound)
16370 c_parser_error (parser, "expected declaration specifiers");
16371 c_parser_skip_to_pragma_eol (parser);
16372 return false;
16375 if (c_parser_next_token_is (parser, CPP_NAME))
16377 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16378 enum tree_code ccode = ERROR_MARK;
16380 if (strcmp (p, "teams") == 0)
16381 ccode = OMP_TEAMS;
16382 else if (strcmp (p, "parallel") == 0)
16383 ccode = OMP_PARALLEL;
16384 else if (strcmp (p, "simd") == 0)
16385 ccode = OMP_SIMD;
16386 if (ccode != ERROR_MARK)
16388 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16389 char p_name[sizeof ("#pragma omp target teams distribute "
16390 "parallel for simd")];
16392 c_parser_consume_token (parser);
16393 strcpy (p_name, "#pragma omp target");
16394 if (!flag_openmp) /* flag_openmp_simd */
16396 tree stmt;
16397 switch (ccode)
16399 case OMP_TEAMS:
16400 stmt = c_parser_omp_teams (loc, parser, p_name,
16401 OMP_TARGET_CLAUSE_MASK,
16402 cclauses, if_p);
16403 break;
16404 case OMP_PARALLEL:
16405 stmt = c_parser_omp_parallel (loc, parser, p_name,
16406 OMP_TARGET_CLAUSE_MASK,
16407 cclauses, if_p);
16408 break;
16409 case OMP_SIMD:
16410 stmt = c_parser_omp_simd (loc, parser, p_name,
16411 OMP_TARGET_CLAUSE_MASK,
16412 cclauses, if_p);
16413 break;
16414 default:
16415 gcc_unreachable ();
16417 return stmt != NULL_TREE;
16419 keep_next_level ();
16420 tree block = c_begin_compound_stmt (true), ret;
16421 switch (ccode)
16423 case OMP_TEAMS:
16424 ret = c_parser_omp_teams (loc, parser, p_name,
16425 OMP_TARGET_CLAUSE_MASK, cclauses,
16426 if_p);
16427 break;
16428 case OMP_PARALLEL:
16429 ret = c_parser_omp_parallel (loc, parser, p_name,
16430 OMP_TARGET_CLAUSE_MASK, cclauses,
16431 if_p);
16432 break;
16433 case OMP_SIMD:
16434 ret = c_parser_omp_simd (loc, parser, p_name,
16435 OMP_TARGET_CLAUSE_MASK, cclauses,
16436 if_p);
16437 break;
16438 default:
16439 gcc_unreachable ();
16441 block = c_end_compound_stmt (loc, block, true);
16442 if (ret == NULL_TREE)
16443 return false;
16444 if (ccode == OMP_TEAMS)
16446 /* For combined target teams, ensure the num_teams and
16447 thread_limit clause expressions are evaluated on the host,
16448 before entering the target construct. */
16449 tree c;
16450 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16451 c; c = OMP_CLAUSE_CHAIN (c))
16452 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16453 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16454 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16456 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16457 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16458 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16459 expr, NULL_TREE, NULL_TREE);
16460 add_stmt (expr);
16461 OMP_CLAUSE_OPERAND (c, 0) = expr;
16462 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16463 OMP_CLAUSE_FIRSTPRIVATE);
16464 OMP_CLAUSE_DECL (tc) = tmp;
16465 OMP_CLAUSE_CHAIN (tc)
16466 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16467 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16470 tree stmt = make_node (OMP_TARGET);
16471 TREE_TYPE (stmt) = void_type_node;
16472 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16473 OMP_TARGET_BODY (stmt) = block;
16474 OMP_TARGET_COMBINED (stmt) = 1;
16475 add_stmt (stmt);
16476 pc = &OMP_TARGET_CLAUSES (stmt);
16477 goto check_clauses;
16479 else if (!flag_openmp) /* flag_openmp_simd */
16481 c_parser_skip_to_pragma_eol (parser, false);
16482 return false;
16484 else if (strcmp (p, "data") == 0)
16486 c_parser_consume_token (parser);
16487 c_parser_omp_target_data (loc, parser, if_p);
16488 return true;
16490 else if (strcmp (p, "enter") == 0)
16492 c_parser_consume_token (parser);
16493 c_parser_omp_target_enter_data (loc, parser, context);
16494 return false;
16496 else if (strcmp (p, "exit") == 0)
16498 c_parser_consume_token (parser);
16499 c_parser_omp_target_exit_data (loc, parser, context);
16500 return false;
16502 else if (strcmp (p, "update") == 0)
16504 c_parser_consume_token (parser);
16505 return c_parser_omp_target_update (loc, parser, context);
16508 if (!flag_openmp) /* flag_openmp_simd */
16510 c_parser_skip_to_pragma_eol (parser, false);
16511 return false;
16514 stmt = make_node (OMP_TARGET);
16515 TREE_TYPE (stmt) = void_type_node;
16517 OMP_TARGET_CLAUSES (stmt)
16518 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16519 "#pragma omp target");
16520 pc = &OMP_TARGET_CLAUSES (stmt);
16521 keep_next_level ();
16522 block = c_begin_compound_stmt (true);
16523 add_stmt (c_parser_omp_structured_block (parser, if_p));
16524 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16526 SET_EXPR_LOCATION (stmt, loc);
16527 add_stmt (stmt);
16529 check_clauses:
16530 while (*pc)
16532 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16533 switch (OMP_CLAUSE_MAP_KIND (*pc))
16535 case GOMP_MAP_TO:
16536 case GOMP_MAP_ALWAYS_TO:
16537 case GOMP_MAP_FROM:
16538 case GOMP_MAP_ALWAYS_FROM:
16539 case GOMP_MAP_TOFROM:
16540 case GOMP_MAP_ALWAYS_TOFROM:
16541 case GOMP_MAP_ALLOC:
16542 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16543 case GOMP_MAP_ALWAYS_POINTER:
16544 break;
16545 default:
16546 error_at (OMP_CLAUSE_LOCATION (*pc),
16547 "%<#pragma omp target%> with map-type other "
16548 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16549 "on %<map%> clause");
16550 *pc = OMP_CLAUSE_CHAIN (*pc);
16551 continue;
16553 pc = &OMP_CLAUSE_CHAIN (*pc);
16555 return true;
16558 /* OpenMP 4.0:
16559 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16561 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16569 static void
16570 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16572 auto_vec<c_token> clauses;
16573 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16575 c_token *token = c_parser_peek_token (parser);
16576 if (token->type == CPP_EOF)
16578 c_parser_skip_to_pragma_eol (parser);
16579 return;
16581 clauses.safe_push (*token);
16582 c_parser_consume_token (parser);
16584 clauses.safe_push (*c_parser_peek_token (parser));
16585 c_parser_skip_to_pragma_eol (parser);
16587 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16589 if (c_parser_peek_token (parser)->pragma_kind
16590 != PRAGMA_OMP_DECLARE
16591 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16592 || strcmp (IDENTIFIER_POINTER
16593 (c_parser_peek_2nd_token (parser)->value),
16594 "simd") != 0)
16596 c_parser_error (parser,
16597 "%<#pragma omp declare simd%> must be followed by "
16598 "function declaration or definition or another "
16599 "%<#pragma omp declare simd%>");
16600 return;
16602 c_parser_consume_pragma (parser);
16603 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16605 c_token *token = c_parser_peek_token (parser);
16606 if (token->type == CPP_EOF)
16608 c_parser_skip_to_pragma_eol (parser);
16609 return;
16611 clauses.safe_push (*token);
16612 c_parser_consume_token (parser);
16614 clauses.safe_push (*c_parser_peek_token (parser));
16615 c_parser_skip_to_pragma_eol (parser);
16618 /* Make sure nothing tries to read past the end of the tokens. */
16619 c_token eof_token;
16620 memset (&eof_token, 0, sizeof (eof_token));
16621 eof_token.type = CPP_EOF;
16622 clauses.safe_push (eof_token);
16623 clauses.safe_push (eof_token);
16625 switch (context)
16627 case pragma_external:
16628 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16629 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16631 int ext = disable_extension_diagnostics ();
16633 c_parser_consume_token (parser);
16634 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16635 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16636 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16637 NULL, clauses);
16638 restore_extension_diagnostics (ext);
16640 else
16641 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16642 NULL, clauses);
16643 break;
16644 case pragma_struct:
16645 case pragma_param:
16646 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16647 "function declaration or definition");
16648 break;
16649 case pragma_compound:
16650 case pragma_stmt:
16651 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16652 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16654 int ext = disable_extension_diagnostics ();
16656 c_parser_consume_token (parser);
16657 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16658 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16659 if (c_parser_next_tokens_start_declaration (parser))
16661 c_parser_declaration_or_fndef (parser, true, true, true, true,
16662 true, NULL, clauses);
16663 restore_extension_diagnostics (ext);
16664 break;
16666 restore_extension_diagnostics (ext);
16668 else if (c_parser_next_tokens_start_declaration (parser))
16670 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16671 NULL, clauses);
16672 break;
16674 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16675 "function declaration or definition");
16676 break;
16677 default:
16678 gcc_unreachable ();
16682 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16683 and put that into "omp declare simd" attribute. */
16685 static void
16686 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16687 vec<c_token> clauses)
16689 if (flag_cilkplus
16690 && (clauses.exists ()
16691 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16692 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16694 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16695 "used in the same function marked as a Cilk Plus SIMD-enabled "
16696 "function");
16697 vec_free (parser->cilk_simd_fn_tokens);
16698 return;
16701 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16702 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16703 has already processed the tokens. */
16704 if (clauses.exists () && clauses[0].type == CPP_EOF)
16705 return;
16706 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16708 error ("%<#pragma omp declare simd%> not immediately followed by "
16709 "a function declaration or definition");
16710 clauses[0].type = CPP_EOF;
16711 return;
16713 if (clauses.exists () && clauses[0].type != CPP_NAME)
16715 error_at (DECL_SOURCE_LOCATION (fndecl),
16716 "%<#pragma omp declare simd%> not immediately followed by "
16717 "a single function declaration or definition");
16718 clauses[0].type = CPP_EOF;
16719 return;
16722 if (parms == NULL_TREE)
16723 parms = DECL_ARGUMENTS (fndecl);
16725 unsigned int tokens_avail = parser->tokens_avail;
16726 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16727 bool is_cilkplus_cilk_simd_fn = false;
16729 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16731 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16732 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16733 is_cilkplus_cilk_simd_fn = true;
16735 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16737 error_at (DECL_SOURCE_LOCATION (fndecl),
16738 "%<__simd__%> attribute cannot be used in the same "
16739 "function marked as a Cilk Plus SIMD-enabled function");
16740 vec_free (parser->cilk_simd_fn_tokens);
16741 return;
16745 else
16747 parser->tokens = clauses.address ();
16748 parser->tokens_avail = clauses.length ();
16751 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16752 while (parser->tokens_avail > 3)
16754 c_token *token = c_parser_peek_token (parser);
16755 if (!is_cilkplus_cilk_simd_fn)
16756 gcc_assert (token->type == CPP_NAME
16757 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16758 else
16759 gcc_assert (token->type == CPP_NAME
16760 && is_cilkplus_vector_p (token->value));
16761 c_parser_consume_token (parser);
16762 parser->in_pragma = true;
16764 tree c = NULL_TREE;
16765 if (is_cilkplus_cilk_simd_fn)
16766 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16767 "SIMD-enabled functions attribute");
16768 else
16769 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16770 "#pragma omp declare simd");
16771 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16772 if (c != NULL_TREE)
16773 c = tree_cons (NULL_TREE, c, NULL_TREE);
16774 if (is_cilkplus_cilk_simd_fn)
16776 tree k = build_tree_list (get_identifier ("cilk simd function"),
16777 NULL_TREE);
16778 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16779 DECL_ATTRIBUTES (fndecl) = k;
16781 c = build_tree_list (get_identifier ("omp declare simd"), c);
16782 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16783 DECL_ATTRIBUTES (fndecl) = c;
16786 parser->tokens = &parser->tokens_buf[0];
16787 parser->tokens_avail = tokens_avail;
16788 if (clauses.exists ())
16789 clauses[0].type = CPP_PRAGMA;
16791 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16792 vec_free (parser->cilk_simd_fn_tokens);
16796 /* OpenMP 4.0:
16797 # pragma omp declare target new-line
16798 declarations and definitions
16799 # pragma omp end declare target new-line
16801 OpenMP 4.5:
16802 # pragma omp declare target ( extended-list ) new-line
16804 # pragma omp declare target declare-target-clauses[seq] new-line */
16806 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16810 static void
16811 c_parser_omp_declare_target (c_parser *parser)
16813 location_t loc = c_parser_peek_token (parser)->location;
16814 tree clauses = NULL_TREE;
16815 if (c_parser_next_token_is (parser, CPP_NAME))
16816 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16817 "#pragma omp declare target");
16818 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16820 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16821 clauses);
16822 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
16823 c_parser_skip_to_pragma_eol (parser);
16825 else
16827 c_parser_skip_to_pragma_eol (parser);
16828 current_omp_declare_target_attribute++;
16829 return;
16831 if (current_omp_declare_target_attribute)
16832 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16833 "%<#pragma omp declare target%> without clauses and "
16834 "%<#pragma omp end declare target%>");
16835 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16837 tree t = OMP_CLAUSE_DECL (c), id;
16838 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16839 tree at2 = lookup_attribute ("omp declare target link",
16840 DECL_ATTRIBUTES (t));
16841 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16843 id = get_identifier ("omp declare target link");
16844 std::swap (at1, at2);
16846 else
16847 id = get_identifier ("omp declare target");
16848 if (at2)
16850 error_at (OMP_CLAUSE_LOCATION (c),
16851 "%qD specified both in declare target %<link%> and %<to%>"
16852 " clauses", t);
16853 continue;
16855 if (!at1)
16857 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16858 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
16859 continue;
16861 symtab_node *node = symtab_node::get (t);
16862 if (node != NULL)
16864 node->offloadable = 1;
16865 if (ENABLE_OFFLOADING)
16867 g->have_offload = true;
16868 if (is_a <varpool_node *> (node))
16869 vec_safe_push (offload_vars, t);
16876 static void
16877 c_parser_omp_end_declare_target (c_parser *parser)
16879 location_t loc = c_parser_peek_token (parser)->location;
16880 c_parser_consume_pragma (parser);
16881 if (c_parser_next_token_is (parser, CPP_NAME)
16882 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16883 "declare") == 0)
16885 c_parser_consume_token (parser);
16886 if (c_parser_next_token_is (parser, CPP_NAME)
16887 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16888 "target") == 0)
16889 c_parser_consume_token (parser);
16890 else
16892 c_parser_error (parser, "expected %<target%>");
16893 c_parser_skip_to_pragma_eol (parser);
16894 return;
16897 else
16899 c_parser_error (parser, "expected %<declare%>");
16900 c_parser_skip_to_pragma_eol (parser);
16901 return;
16903 c_parser_skip_to_pragma_eol (parser);
16904 if (!current_omp_declare_target_attribute)
16905 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16906 "%<#pragma omp declare target%>");
16907 else
16908 current_omp_declare_target_attribute--;
16912 /* OpenMP 4.0
16913 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16914 initializer-clause[opt] new-line
16916 initializer-clause:
16917 initializer (omp_priv = initializer)
16918 initializer (function-name (argument-list)) */
16920 static void
16921 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16923 unsigned int tokens_avail = 0, i;
16924 vec<tree> types = vNULL;
16925 vec<c_token> clauses = vNULL;
16926 enum tree_code reduc_code = ERROR_MARK;
16927 tree reduc_id = NULL_TREE;
16928 tree type;
16929 location_t rloc = c_parser_peek_token (parser)->location;
16931 if (context == pragma_struct || context == pragma_param)
16933 error ("%<#pragma omp declare reduction%> not at file or block scope");
16934 goto fail;
16937 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16938 goto fail;
16940 switch (c_parser_peek_token (parser)->type)
16942 case CPP_PLUS:
16943 reduc_code = PLUS_EXPR;
16944 break;
16945 case CPP_MULT:
16946 reduc_code = MULT_EXPR;
16947 break;
16948 case CPP_MINUS:
16949 reduc_code = MINUS_EXPR;
16950 break;
16951 case CPP_AND:
16952 reduc_code = BIT_AND_EXPR;
16953 break;
16954 case CPP_XOR:
16955 reduc_code = BIT_XOR_EXPR;
16956 break;
16957 case CPP_OR:
16958 reduc_code = BIT_IOR_EXPR;
16959 break;
16960 case CPP_AND_AND:
16961 reduc_code = TRUTH_ANDIF_EXPR;
16962 break;
16963 case CPP_OR_OR:
16964 reduc_code = TRUTH_ORIF_EXPR;
16965 break;
16966 case CPP_NAME:
16967 const char *p;
16968 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16969 if (strcmp (p, "min") == 0)
16971 reduc_code = MIN_EXPR;
16972 break;
16974 if (strcmp (p, "max") == 0)
16976 reduc_code = MAX_EXPR;
16977 break;
16979 reduc_id = c_parser_peek_token (parser)->value;
16980 break;
16981 default:
16982 c_parser_error (parser,
16983 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16984 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16985 goto fail;
16988 tree orig_reduc_id, reduc_decl;
16989 orig_reduc_id = reduc_id;
16990 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16991 reduc_decl = c_omp_reduction_decl (reduc_id);
16992 c_parser_consume_token (parser);
16994 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16995 goto fail;
16997 while (true)
16999 location_t loc = c_parser_peek_token (parser)->location;
17000 struct c_type_name *ctype = c_parser_type_name (parser);
17001 if (ctype != NULL)
17003 type = groktypename (ctype, NULL, NULL);
17004 if (type == error_mark_node)
17006 else if ((INTEGRAL_TYPE_P (type)
17007 || TREE_CODE (type) == REAL_TYPE
17008 || TREE_CODE (type) == COMPLEX_TYPE)
17009 && orig_reduc_id == NULL_TREE)
17010 error_at (loc, "predeclared arithmetic type in "
17011 "%<#pragma omp declare reduction%>");
17012 else if (TREE_CODE (type) == FUNCTION_TYPE
17013 || TREE_CODE (type) == ARRAY_TYPE)
17014 error_at (loc, "function or array type in "
17015 "%<#pragma omp declare reduction%>");
17016 else if (TYPE_ATOMIC (type))
17017 error_at (loc, "%<_Atomic%> qualified type in "
17018 "%<#pragma omp declare reduction%>");
17019 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17020 error_at (loc, "const, volatile or restrict qualified type in "
17021 "%<#pragma omp declare reduction%>");
17022 else
17024 tree t;
17025 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17026 if (comptypes (TREE_PURPOSE (t), type))
17028 error_at (loc, "redeclaration of %qs "
17029 "%<#pragma omp declare reduction%> for "
17030 "type %qT",
17031 IDENTIFIER_POINTER (reduc_id)
17032 + sizeof ("omp declare reduction ") - 1,
17033 type);
17034 location_t ploc
17035 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17036 0));
17037 error_at (ploc, "previous %<#pragma omp declare "
17038 "reduction%>");
17039 break;
17041 if (t == NULL_TREE)
17042 types.safe_push (type);
17044 if (c_parser_next_token_is (parser, CPP_COMMA))
17045 c_parser_consume_token (parser);
17046 else
17047 break;
17049 else
17050 break;
17053 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17054 || types.is_empty ())
17056 fail:
17057 clauses.release ();
17058 types.release ();
17059 while (true)
17061 c_token *token = c_parser_peek_token (parser);
17062 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17063 break;
17064 c_parser_consume_token (parser);
17066 c_parser_skip_to_pragma_eol (parser);
17067 return;
17070 if (types.length () > 1)
17072 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17074 c_token *token = c_parser_peek_token (parser);
17075 if (token->type == CPP_EOF)
17076 goto fail;
17077 clauses.safe_push (*token);
17078 c_parser_consume_token (parser);
17080 clauses.safe_push (*c_parser_peek_token (parser));
17081 c_parser_skip_to_pragma_eol (parser);
17083 /* Make sure nothing tries to read past the end of the tokens. */
17084 c_token eof_token;
17085 memset (&eof_token, 0, sizeof (eof_token));
17086 eof_token.type = CPP_EOF;
17087 clauses.safe_push (eof_token);
17088 clauses.safe_push (eof_token);
17091 int errs = errorcount;
17092 FOR_EACH_VEC_ELT (types, i, type)
17094 tokens_avail = parser->tokens_avail;
17095 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17096 if (!clauses.is_empty ())
17098 parser->tokens = clauses.address ();
17099 parser->tokens_avail = clauses.length ();
17100 parser->in_pragma = true;
17103 bool nested = current_function_decl != NULL_TREE;
17104 if (nested)
17105 c_push_function_context ();
17106 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17107 reduc_id, default_function_type);
17108 current_function_decl = fndecl;
17109 allocate_struct_function (fndecl, true);
17110 push_scope ();
17111 tree stmt = push_stmt_list ();
17112 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17113 warn about these. */
17114 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17115 get_identifier ("omp_out"), type);
17116 DECL_ARTIFICIAL (omp_out) = 1;
17117 DECL_CONTEXT (omp_out) = fndecl;
17118 pushdecl (omp_out);
17119 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17120 get_identifier ("omp_in"), type);
17121 DECL_ARTIFICIAL (omp_in) = 1;
17122 DECL_CONTEXT (omp_in) = fndecl;
17123 pushdecl (omp_in);
17124 struct c_expr combiner = c_parser_expression (parser);
17125 struct c_expr initializer;
17126 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17127 bool bad = false;
17128 initializer.value = error_mark_node;
17129 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17130 bad = true;
17131 else if (c_parser_next_token_is (parser, CPP_NAME)
17132 && strcmp (IDENTIFIER_POINTER
17133 (c_parser_peek_token (parser)->value),
17134 "initializer") == 0)
17136 c_parser_consume_token (parser);
17137 pop_scope ();
17138 push_scope ();
17139 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17140 get_identifier ("omp_priv"), type);
17141 DECL_ARTIFICIAL (omp_priv) = 1;
17142 DECL_INITIAL (omp_priv) = error_mark_node;
17143 DECL_CONTEXT (omp_priv) = fndecl;
17144 pushdecl (omp_priv);
17145 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17146 get_identifier ("omp_orig"), type);
17147 DECL_ARTIFICIAL (omp_orig) = 1;
17148 DECL_CONTEXT (omp_orig) = fndecl;
17149 pushdecl (omp_orig);
17150 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17151 bad = true;
17152 else if (!c_parser_next_token_is (parser, CPP_NAME))
17154 c_parser_error (parser, "expected %<omp_priv%> or "
17155 "function-name");
17156 bad = true;
17158 else if (strcmp (IDENTIFIER_POINTER
17159 (c_parser_peek_token (parser)->value),
17160 "omp_priv") != 0)
17162 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17163 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17165 c_parser_error (parser, "expected function-name %<(%>");
17166 bad = true;
17168 else
17169 initializer = c_parser_postfix_expression (parser);
17170 if (initializer.value
17171 && TREE_CODE (initializer.value) == CALL_EXPR)
17173 int j;
17174 tree c = initializer.value;
17175 for (j = 0; j < call_expr_nargs (c); j++)
17177 tree a = CALL_EXPR_ARG (c, j);
17178 STRIP_NOPS (a);
17179 if (TREE_CODE (a) == ADDR_EXPR
17180 && TREE_OPERAND (a, 0) == omp_priv)
17181 break;
17183 if (j == call_expr_nargs (c))
17184 error ("one of the initializer call arguments should be "
17185 "%<&omp_priv%>");
17188 else
17190 c_parser_consume_token (parser);
17191 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17192 bad = true;
17193 else
17195 tree st = push_stmt_list ();
17196 location_t loc = c_parser_peek_token (parser)->location;
17197 rich_location richloc (line_table, loc);
17198 start_init (omp_priv, NULL_TREE, 0, &richloc);
17199 struct c_expr init = c_parser_initializer (parser);
17200 finish_init ();
17201 finish_decl (omp_priv, loc, init.value,
17202 init.original_type, NULL_TREE);
17203 pop_stmt_list (st);
17206 if (!bad
17207 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17208 bad = true;
17211 if (!bad)
17213 c_parser_skip_to_pragma_eol (parser);
17215 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17216 DECL_INITIAL (reduc_decl));
17217 DECL_INITIAL (reduc_decl) = t;
17218 DECL_SOURCE_LOCATION (omp_out) = rloc;
17219 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17220 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17221 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17222 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17223 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17224 if (omp_priv)
17226 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17227 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17228 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17229 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17230 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17231 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17232 walk_tree (&DECL_INITIAL (omp_priv),
17233 c_check_omp_declare_reduction_r,
17234 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17238 pop_stmt_list (stmt);
17239 pop_scope ();
17240 if (cfun->language != NULL)
17242 ggc_free (cfun->language);
17243 cfun->language = NULL;
17245 set_cfun (NULL);
17246 current_function_decl = NULL_TREE;
17247 if (nested)
17248 c_pop_function_context ();
17250 if (!clauses.is_empty ())
17252 parser->tokens = &parser->tokens_buf[0];
17253 parser->tokens_avail = tokens_avail;
17255 if (bad)
17256 goto fail;
17257 if (errs != errorcount)
17258 break;
17261 clauses.release ();
17262 types.release ();
17266 /* OpenMP 4.0
17267 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17268 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17269 initializer-clause[opt] new-line
17270 #pragma omp declare target new-line */
17272 static void
17273 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17275 c_parser_consume_pragma (parser);
17276 if (c_parser_next_token_is (parser, CPP_NAME))
17278 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17279 if (strcmp (p, "simd") == 0)
17281 /* c_parser_consume_token (parser); done in
17282 c_parser_omp_declare_simd. */
17283 c_parser_omp_declare_simd (parser, context);
17284 return;
17286 if (strcmp (p, "reduction") == 0)
17288 c_parser_consume_token (parser);
17289 c_parser_omp_declare_reduction (parser, context);
17290 return;
17292 if (!flag_openmp) /* flag_openmp_simd */
17294 c_parser_skip_to_pragma_eol (parser, false);
17295 return;
17297 if (strcmp (p, "target") == 0)
17299 c_parser_consume_token (parser);
17300 c_parser_omp_declare_target (parser);
17301 return;
17305 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17306 "or %<target%>");
17307 c_parser_skip_to_pragma_eol (parser);
17310 /* OpenMP 4.5:
17311 #pragma omp taskloop taskloop-clause[optseq] new-line
17312 for-loop
17314 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17315 for-loop */
17317 #define OMP_TASKLOOP_CLAUSE_MASK \
17318 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17333 static tree
17334 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17335 char *p_name, omp_clause_mask mask, tree *cclauses,
17336 bool *if_p)
17338 tree clauses, block, ret;
17340 strcat (p_name, " taskloop");
17341 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17343 if (c_parser_next_token_is (parser, CPP_NAME))
17345 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17347 if (strcmp (p, "simd") == 0)
17349 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17350 if (cclauses == NULL)
17351 cclauses = cclauses_buf;
17352 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17353 c_parser_consume_token (parser);
17354 if (!flag_openmp) /* flag_openmp_simd */
17355 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17356 if_p);
17357 block = c_begin_compound_stmt (true);
17358 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17359 block = c_end_compound_stmt (loc, block, true);
17360 if (ret == NULL)
17361 return ret;
17362 ret = make_node (OMP_TASKLOOP);
17363 TREE_TYPE (ret) = void_type_node;
17364 OMP_FOR_BODY (ret) = block;
17365 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17366 SET_EXPR_LOCATION (ret, loc);
17367 add_stmt (ret);
17368 return ret;
17371 if (!flag_openmp) /* flag_openmp_simd */
17373 c_parser_skip_to_pragma_eol (parser, false);
17374 return NULL_TREE;
17377 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17378 if (cclauses)
17380 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17381 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17384 block = c_begin_compound_stmt (true);
17385 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17386 block = c_end_compound_stmt (loc, block, true);
17387 add_stmt (block);
17389 return ret;
17392 /* Main entry point to parsing most OpenMP pragmas. */
17394 static void
17395 c_parser_omp_construct (c_parser *parser, bool *if_p)
17397 enum pragma_kind p_kind;
17398 location_t loc;
17399 tree stmt;
17400 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17401 omp_clause_mask mask (0);
17403 loc = c_parser_peek_token (parser)->location;
17404 p_kind = c_parser_peek_token (parser)->pragma_kind;
17405 c_parser_consume_pragma (parser);
17407 switch (p_kind)
17409 case PRAGMA_OACC_ATOMIC:
17410 c_parser_omp_atomic (loc, parser);
17411 return;
17412 case PRAGMA_OACC_CACHE:
17413 strcpy (p_name, "#pragma acc");
17414 stmt = c_parser_oacc_cache (loc, parser);
17415 break;
17416 case PRAGMA_OACC_DATA:
17417 stmt = c_parser_oacc_data (loc, parser, if_p);
17418 break;
17419 case PRAGMA_OACC_HOST_DATA:
17420 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17421 break;
17422 case PRAGMA_OACC_KERNELS:
17423 case PRAGMA_OACC_PARALLEL:
17424 strcpy (p_name, "#pragma acc");
17425 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17426 if_p);
17427 break;
17428 case PRAGMA_OACC_LOOP:
17429 strcpy (p_name, "#pragma acc");
17430 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17431 break;
17432 case PRAGMA_OACC_WAIT:
17433 strcpy (p_name, "#pragma wait");
17434 stmt = c_parser_oacc_wait (loc, parser, p_name);
17435 break;
17436 case PRAGMA_OMP_ATOMIC:
17437 c_parser_omp_atomic (loc, parser);
17438 return;
17439 case PRAGMA_OMP_CRITICAL:
17440 stmt = c_parser_omp_critical (loc, parser, if_p);
17441 break;
17442 case PRAGMA_OMP_DISTRIBUTE:
17443 strcpy (p_name, "#pragma omp");
17444 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17445 break;
17446 case PRAGMA_OMP_FOR:
17447 strcpy (p_name, "#pragma omp");
17448 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17449 break;
17450 case PRAGMA_OMP_MASTER:
17451 stmt = c_parser_omp_master (loc, parser, if_p);
17452 break;
17453 case PRAGMA_OMP_PARALLEL:
17454 strcpy (p_name, "#pragma omp");
17455 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17456 break;
17457 case PRAGMA_OMP_SECTIONS:
17458 strcpy (p_name, "#pragma omp");
17459 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17460 break;
17461 case PRAGMA_OMP_SIMD:
17462 strcpy (p_name, "#pragma omp");
17463 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17464 break;
17465 case PRAGMA_OMP_SINGLE:
17466 stmt = c_parser_omp_single (loc, parser, if_p);
17467 break;
17468 case PRAGMA_OMP_TASK:
17469 stmt = c_parser_omp_task (loc, parser, if_p);
17470 break;
17471 case PRAGMA_OMP_TASKGROUP:
17472 stmt = c_parser_omp_taskgroup (parser, if_p);
17473 break;
17474 case PRAGMA_OMP_TASKLOOP:
17475 strcpy (p_name, "#pragma omp");
17476 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17477 break;
17478 case PRAGMA_OMP_TEAMS:
17479 strcpy (p_name, "#pragma omp");
17480 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17481 break;
17482 default:
17483 gcc_unreachable ();
17486 if (stmt)
17487 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17491 /* OpenMP 2.5:
17492 # pragma omp threadprivate (variable-list) */
17494 static void
17495 c_parser_omp_threadprivate (c_parser *parser)
17497 tree vars, t;
17498 location_t loc;
17500 c_parser_consume_pragma (parser);
17501 loc = c_parser_peek_token (parser)->location;
17502 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17504 /* Mark every variable in VARS to be assigned thread local storage. */
17505 for (t = vars; t; t = TREE_CHAIN (t))
17507 tree v = TREE_PURPOSE (t);
17509 /* FIXME diagnostics: Ideally we should keep individual
17510 locations for all the variables in the var list to make the
17511 following errors more precise. Perhaps
17512 c_parser_omp_var_list_parens() should construct a list of
17513 locations to go along with the var list. */
17515 /* If V had already been marked threadprivate, it doesn't matter
17516 whether it had been used prior to this point. */
17517 if (!VAR_P (v))
17518 error_at (loc, "%qD is not a variable", v);
17519 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17520 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17521 else if (! is_global_var (v))
17522 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17523 else if (TREE_TYPE (v) == error_mark_node)
17525 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17526 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17527 else
17529 if (! DECL_THREAD_LOCAL_P (v))
17531 set_decl_tls_model (v, decl_default_tls_model (v));
17532 /* If rtl has been already set for this var, call
17533 make_decl_rtl once again, so that encode_section_info
17534 has a chance to look at the new decl flags. */
17535 if (DECL_RTL_SET_P (v))
17536 make_decl_rtl (v);
17538 C_DECL_THREADPRIVATE_P (v) = 1;
17542 c_parser_skip_to_pragma_eol (parser);
17545 /* Cilk Plus <#pragma simd> parsing routines. */
17547 /* Helper function for c_parser_pragma. Perform some sanity checking
17548 for <#pragma simd> constructs. Returns FALSE if there was a
17549 problem. */
17551 static bool
17552 c_parser_cilk_verify_simd (c_parser *parser,
17553 enum pragma_context context)
17555 if (!flag_cilkplus)
17557 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17558 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17559 return false;
17561 if (context == pragma_external)
17563 c_parser_error (parser,"pragma simd must be inside a function");
17564 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17565 return false;
17567 return true;
17570 /* Cilk Plus:
17571 This function is shared by SIMD-enabled functions and #pragma simd.
17572 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17573 CLAUSES is unused. The main purpose of this function is to parse a
17574 vectorlength attribute or clause and check for parse errors.
17575 When IS_SIMD_FN is true then the function is merely caching the tokens
17576 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17577 cache is cleared since there is no reason to continue.
17578 Syntax:
17579 vectorlength ( constant-expression ) */
17581 static tree
17582 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17583 bool is_simd_fn)
17585 if (is_simd_fn)
17586 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17587 else
17588 /* The vectorlength clause behaves exactly like OpenMP's safelen
17589 clause. Represent it in OpenMP terms. */
17590 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17592 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17593 return clauses;
17595 location_t loc = c_parser_peek_token (parser)->location;
17596 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17597 expr = c_fully_fold (expr, false, NULL);
17599 /* If expr is an error_mark_node then the above function would have
17600 emitted an error. No reason to do it twice. */
17601 if (expr == error_mark_node)
17603 else if (!TREE_TYPE (expr)
17604 || !TREE_CONSTANT (expr)
17605 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17607 error_at (loc, "vectorlength must be an integer constant");
17608 else if (wi::exact_log2 (expr) == -1)
17609 error_at (loc, "vectorlength must be a power of 2");
17610 else
17612 if (is_simd_fn)
17614 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17615 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17616 OMP_CLAUSE_CHAIN (u) = clauses;
17617 clauses = u;
17619 else
17621 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17622 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17623 OMP_CLAUSE_CHAIN (u) = clauses;
17624 clauses = u;
17628 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17630 return clauses;
17633 /* Cilk Plus:
17634 linear ( simd-linear-variable-list )
17636 simd-linear-variable-list:
17637 simd-linear-variable
17638 simd-linear-variable-list , simd-linear-variable
17640 simd-linear-variable:
17641 id-expression
17642 id-expression : simd-linear-step
17644 simd-linear-step:
17645 conditional-expression */
17647 static tree
17648 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17650 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17651 return clauses;
17653 location_t loc = c_parser_peek_token (parser)->location;
17655 if (c_parser_next_token_is_not (parser, CPP_NAME)
17656 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17657 c_parser_error (parser, "expected identifier");
17659 while (c_parser_next_token_is (parser, CPP_NAME)
17660 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17662 tree var = lookup_name (c_parser_peek_token (parser)->value);
17664 if (var == NULL)
17666 undeclared_variable (c_parser_peek_token (parser)->location,
17667 c_parser_peek_token (parser)->value);
17668 c_parser_consume_token (parser);
17670 else if (var == error_mark_node)
17671 c_parser_consume_token (parser);
17672 else
17674 tree step = integer_one_node;
17676 /* Parse the linear step if present. */
17677 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17679 c_parser_consume_token (parser);
17680 c_parser_consume_token (parser);
17682 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17683 expr = c_fully_fold (expr, false, NULL);
17685 if (TREE_TYPE (expr)
17686 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17687 && (TREE_CONSTANT (expr)
17688 || DECL_P (expr)))
17689 step = expr;
17690 else
17691 c_parser_error (parser,
17692 "step size must be an integer constant "
17693 "expression or an integer variable");
17695 else
17696 c_parser_consume_token (parser);
17698 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17699 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17700 OMP_CLAUSE_DECL (u) = var;
17701 OMP_CLAUSE_LINEAR_STEP (u) = step;
17702 OMP_CLAUSE_CHAIN (u) = clauses;
17703 clauses = u;
17706 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17707 break;
17709 c_parser_consume_token (parser);
17712 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17714 return clauses;
17717 /* Returns the name of the next clause. If the clause is not
17718 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17719 not consumed. Otherwise, the appropriate pragma_simd_clause is
17720 returned and the token is consumed. */
17722 static pragma_omp_clause
17723 c_parser_cilk_clause_name (c_parser *parser)
17725 pragma_omp_clause result;
17726 c_token *token = c_parser_peek_token (parser);
17728 if (!token->value || token->type != CPP_NAME)
17729 return PRAGMA_CILK_CLAUSE_NONE;
17731 const char *p = IDENTIFIER_POINTER (token->value);
17733 if (!strcmp (p, "vectorlength"))
17734 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17735 else if (!strcmp (p, "linear"))
17736 result = PRAGMA_CILK_CLAUSE_LINEAR;
17737 else if (!strcmp (p, "private"))
17738 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17739 else if (!strcmp (p, "firstprivate"))
17740 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17741 else if (!strcmp (p, "lastprivate"))
17742 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17743 else if (!strcmp (p, "reduction"))
17744 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17745 else
17746 return PRAGMA_CILK_CLAUSE_NONE;
17748 c_parser_consume_token (parser);
17749 return result;
17752 /* Parse all #<pragma simd> clauses. Return the list of clauses
17753 found. */
17755 static tree
17756 c_parser_cilk_all_clauses (c_parser *parser)
17758 tree clauses = NULL;
17760 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17762 pragma_omp_clause c_kind;
17764 c_kind = c_parser_cilk_clause_name (parser);
17766 switch (c_kind)
17768 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17769 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17770 break;
17771 case PRAGMA_CILK_CLAUSE_LINEAR:
17772 clauses = c_parser_cilk_clause_linear (parser, clauses);
17773 break;
17774 case PRAGMA_CILK_CLAUSE_PRIVATE:
17775 /* Use the OpenMP counterpart. */
17776 clauses = c_parser_omp_clause_private (parser, clauses);
17777 break;
17778 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17779 /* Use the OpenMP counterpart. */
17780 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17781 break;
17782 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17783 /* Use the OpenMP counterpart. */
17784 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17785 break;
17786 case PRAGMA_CILK_CLAUSE_REDUCTION:
17787 /* Use the OpenMP counterpart. */
17788 clauses = c_parser_omp_clause_reduction (parser, clauses);
17789 break;
17790 default:
17791 c_parser_error (parser, "expected %<#pragma simd%> clause");
17792 goto saw_error;
17796 saw_error:
17797 c_parser_skip_to_pragma_eol (parser);
17798 return c_finish_omp_clauses (clauses, C_ORT_CILK);
17801 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17802 Here is the correct syntax of this pragma:
17803 #pragma cilk grainsize = <EXP>
17806 static void
17807 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
17809 extern tree convert_to_integer (tree, tree);
17811 /* consume the 'grainsize' keyword. */
17812 c_parser_consume_pragma (parser);
17814 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17816 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17817 if (g_expr.value == error_mark_node)
17819 c_parser_skip_to_pragma_eol (parser);
17820 return;
17822 tree grain = convert_to_integer (long_integer_type_node,
17823 c_fully_fold (g_expr.value, false,
17824 NULL));
17825 c_parser_skip_to_pragma_eol (parser);
17826 c_token *token = c_parser_peek_token (parser);
17827 if (token && token->type == CPP_KEYWORD
17828 && token->keyword == RID_CILK_FOR)
17830 if (grain == NULL_TREE || grain == error_mark_node)
17831 grain = integer_zero_node;
17832 c_parser_cilk_for (parser, grain, if_p);
17834 else
17835 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17836 "%<_Cilk_for%>");
17838 else
17839 c_parser_skip_to_pragma_eol (parser);
17842 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17844 static void
17845 c_parser_cilk_simd (c_parser *parser, bool *if_p)
17847 tree clauses = c_parser_cilk_all_clauses (parser);
17848 tree block = c_begin_compound_stmt (true);
17849 location_t loc = c_parser_peek_token (parser)->location;
17850 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
17851 block = c_end_compound_stmt (loc, block, true);
17852 add_stmt (block);
17855 /* Create an artificial decl with TYPE and emit initialization of it with
17856 INIT. */
17858 static tree
17859 c_get_temp_regvar (tree type, tree init)
17861 location_t loc = EXPR_LOCATION (init);
17862 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17863 DECL_ARTIFICIAL (decl) = 1;
17864 DECL_IGNORED_P (decl) = 1;
17865 pushdecl (decl);
17866 tree t = build2 (INIT_EXPR, type, decl, init);
17867 add_stmt (t);
17868 return decl;
17871 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17872 GRAIN is the grain value passed in through pragma or 0. */
17874 static void
17875 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
17877 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17878 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17879 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17880 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
17882 tree block = c_begin_compound_stmt (true);
17883 tree sb = push_stmt_list ();
17884 location_t loc = c_parser_peek_token (parser)->location;
17885 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
17886 if_p);
17887 sb = pop_stmt_list (sb);
17889 if (omp_for)
17891 tree omp_par = make_node (OMP_PARALLEL);
17892 TREE_TYPE (omp_par) = void_type_node;
17893 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17894 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17895 TREE_SIDE_EFFECTS (bind) = 1;
17896 BIND_EXPR_BODY (bind) = sb;
17897 OMP_PARALLEL_BODY (omp_par) = bind;
17898 if (OMP_FOR_PRE_BODY (omp_for))
17900 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17901 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17903 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17904 tree decl = TREE_OPERAND (init, 0);
17905 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17906 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17907 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17908 if (TREE_CODE (t) != INTEGER_CST)
17910 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17911 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17912 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17913 OMP_CLAUSE_CHAIN (c) = clauses;
17914 clauses = c;
17916 if (TREE_CODE (incr) == MODIFY_EXPR)
17918 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17919 if (TREE_CODE (t) != INTEGER_CST)
17921 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17922 = 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 (TREE_OPERAND (incr, 1), 1);
17925 OMP_CLAUSE_CHAIN (c) = clauses;
17926 clauses = c;
17929 t = TREE_OPERAND (init, 1);
17930 if (TREE_CODE (t) != INTEGER_CST)
17932 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17933 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17934 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17935 OMP_CLAUSE_CHAIN (c) = clauses;
17936 clauses = c;
17938 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17939 OMP_CLAUSE_DECL (c) = decl;
17940 OMP_CLAUSE_CHAIN (c) = clauses;
17941 clauses = c;
17942 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17943 OMP_CLAUSE_OPERAND (c, 0)
17944 = cilk_for_number_of_iterations (omp_for);
17945 OMP_CLAUSE_CHAIN (c) = clauses;
17946 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
17947 add_stmt (omp_par);
17950 block = c_end_compound_stmt (loc, block, true);
17951 add_stmt (block);
17955 /* Parse a transaction attribute (GCC Extension).
17957 transaction-attribute:
17958 attributes
17959 [ [ any-word ] ]
17961 The transactional memory language description is written for C++,
17962 and uses the C++0x attribute syntax. For compatibility, allow the
17963 bracket style for transactions in C as well. */
17965 static tree
17966 c_parser_transaction_attributes (c_parser *parser)
17968 tree attr_name, attr = NULL;
17970 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17971 return c_parser_attributes (parser);
17973 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17974 return NULL_TREE;
17975 c_parser_consume_token (parser);
17976 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17977 goto error1;
17979 attr_name = c_parser_attribute_any_word (parser);
17980 if (attr_name)
17982 c_parser_consume_token (parser);
17983 attr = build_tree_list (attr_name, NULL_TREE);
17985 else
17986 c_parser_error (parser, "expected identifier");
17988 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17989 error1:
17990 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17991 return attr;
17994 /* Parse a __transaction_atomic or __transaction_relaxed statement
17995 (GCC Extension).
17997 transaction-statement:
17998 __transaction_atomic transaction-attribute[opt] compound-statement
17999 __transaction_relaxed compound-statement
18001 Note that the only valid attribute is: "outer".
18004 static tree
18005 c_parser_transaction (c_parser *parser, enum rid keyword)
18007 unsigned int old_in = parser->in_transaction;
18008 unsigned int this_in = 1, new_in;
18009 location_t loc = c_parser_peek_token (parser)->location;
18010 tree stmt, attrs;
18012 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18013 || keyword == RID_TRANSACTION_RELAXED)
18014 && c_parser_next_token_is_keyword (parser, keyword));
18015 c_parser_consume_token (parser);
18017 if (keyword == RID_TRANSACTION_RELAXED)
18018 this_in |= TM_STMT_ATTR_RELAXED;
18019 else
18021 attrs = c_parser_transaction_attributes (parser);
18022 if (attrs)
18023 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18026 /* Keep track if we're in the lexical scope of an outer transaction. */
18027 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18029 parser->in_transaction = new_in;
18030 stmt = c_parser_compound_statement (parser);
18031 parser->in_transaction = old_in;
18033 if (flag_tm)
18034 stmt = c_finish_transaction (loc, stmt, this_in);
18035 else
18036 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18037 "%<__transaction_atomic%> without transactional memory support enabled"
18038 : "%<__transaction_relaxed %> "
18039 "without transactional memory support enabled"));
18041 return stmt;
18044 /* Parse a __transaction_atomic or __transaction_relaxed expression
18045 (GCC Extension).
18047 transaction-expression:
18048 __transaction_atomic ( expression )
18049 __transaction_relaxed ( expression )
18052 static struct c_expr
18053 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18055 struct c_expr ret;
18056 unsigned int old_in = parser->in_transaction;
18057 unsigned int this_in = 1;
18058 location_t loc = c_parser_peek_token (parser)->location;
18059 tree attrs;
18061 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18062 || keyword == RID_TRANSACTION_RELAXED)
18063 && c_parser_next_token_is_keyword (parser, keyword));
18064 c_parser_consume_token (parser);
18066 if (keyword == RID_TRANSACTION_RELAXED)
18067 this_in |= TM_STMT_ATTR_RELAXED;
18068 else
18070 attrs = c_parser_transaction_attributes (parser);
18071 if (attrs)
18072 this_in |= parse_tm_stmt_attr (attrs, 0);
18075 parser->in_transaction = this_in;
18076 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18078 tree expr = c_parser_expression (parser).value;
18079 ret.original_type = TREE_TYPE (expr);
18080 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18081 if (this_in & TM_STMT_ATTR_RELAXED)
18082 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18083 SET_EXPR_LOCATION (ret.value, loc);
18084 ret.original_code = TRANSACTION_EXPR;
18085 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
18087 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18088 goto error;
18091 else
18093 error:
18094 ret.value = error_mark_node;
18095 ret.original_code = ERROR_MARK;
18096 ret.original_type = NULL;
18098 parser->in_transaction = old_in;
18100 if (!flag_tm)
18101 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18102 "%<__transaction_atomic%> without transactional memory support enabled"
18103 : "%<__transaction_relaxed %> "
18104 "without transactional memory support enabled"));
18106 set_c_expr_source_range (&ret, loc, loc);
18108 return ret;
18111 /* Parse a __transaction_cancel statement (GCC Extension).
18113 transaction-cancel-statement:
18114 __transaction_cancel transaction-attribute[opt] ;
18116 Note that the only valid attribute is "outer".
18119 static tree
18120 c_parser_transaction_cancel (c_parser *parser)
18122 location_t loc = c_parser_peek_token (parser)->location;
18123 tree attrs;
18124 bool is_outer = false;
18126 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18127 c_parser_consume_token (parser);
18129 attrs = c_parser_transaction_attributes (parser);
18130 if (attrs)
18131 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18133 if (!flag_tm)
18135 error_at (loc, "%<__transaction_cancel%> without "
18136 "transactional memory support enabled");
18137 goto ret_error;
18139 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18141 error_at (loc, "%<__transaction_cancel%> within a "
18142 "%<__transaction_relaxed%>");
18143 goto ret_error;
18145 else if (is_outer)
18147 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18148 && !is_tm_may_cancel_outer (current_function_decl))
18150 error_at (loc, "outer %<__transaction_cancel%> not "
18151 "within outer %<__transaction_atomic%>");
18152 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18153 goto ret_error;
18156 else if (parser->in_transaction == 0)
18158 error_at (loc, "%<__transaction_cancel%> not within "
18159 "%<__transaction_atomic%>");
18160 goto ret_error;
18163 return add_stmt (build_tm_abort_call (loc, is_outer));
18165 ret_error:
18166 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18169 /* Parse a single source file. */
18171 void
18172 c_parse_file (void)
18174 /* Use local storage to begin. If the first token is a pragma, parse it.
18175 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18176 which will cause garbage collection. */
18177 c_parser tparser;
18179 memset (&tparser, 0, sizeof tparser);
18180 tparser.tokens = &tparser.tokens_buf[0];
18181 the_parser = &tparser;
18183 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18184 c_parser_pragma_pch_preprocess (&tparser);
18186 the_parser = ggc_alloc<c_parser> ();
18187 *the_parser = tparser;
18188 if (tparser.tokens == &tparser.tokens_buf[0])
18189 the_parser->tokens = &the_parser->tokens_buf[0];
18191 /* Initialize EH, if we've been told to do so. */
18192 if (flag_exceptions)
18193 using_eh_for_cleanups ();
18195 c_parser_translation_unit (the_parser);
18196 the_parser = NULL;
18199 /* This function parses Cilk Plus array notation. The starting index is
18200 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
18201 return value of this function is a tree_node called VALUE_TREE of type
18202 ARRAY_NOTATION_REF. */
18204 static tree
18205 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
18206 tree array_value)
18208 c_token *token = NULL;
18209 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
18210 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
18211 tree array_type_domain = NULL_TREE;
18213 if (array_value == error_mark_node || initial_index == error_mark_node)
18215 /* No need to continue. If either of these 2 were true, then an error
18216 must be emitted already. Thus, no need to emit them twice. */
18217 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18218 return error_mark_node;
18221 array_type = TREE_TYPE (array_value);
18222 gcc_assert (array_type);
18223 if (TREE_CODE (array_type) != ARRAY_TYPE
18224 && TREE_CODE (array_type) != POINTER_TYPE)
18226 error_at (loc, "base of array section must be pointer or array type");
18227 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18228 return error_mark_node;
18230 type = TREE_TYPE (array_type);
18231 token = c_parser_peek_token (parser);
18233 if (token->type == CPP_EOF)
18235 c_parser_error (parser, "expected %<:%> or numeral");
18236 return value_tree;
18238 else if (token->type == CPP_COLON)
18240 if (!initial_index)
18242 /* If we are here, then we have a case like this A[:]. */
18243 c_parser_consume_token (parser);
18244 if (TREE_CODE (array_type) == POINTER_TYPE)
18246 error_at (loc, "start-index and length fields necessary for "
18247 "using array notations in pointers");
18248 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18249 return error_mark_node;
18251 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18253 error_at (loc, "array notations cannot be used with function "
18254 "type");
18255 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18256 return error_mark_node;
18258 array_type_domain = TYPE_DOMAIN (array_type);
18260 if (!array_type_domain)
18262 error_at (loc, "start-index and length fields necessary for "
18263 "using array notations in dimensionless arrays");
18264 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18265 return error_mark_node;
18268 start_index = TYPE_MINVAL (array_type_domain);
18269 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
18270 start_index);
18271 if (!TYPE_MAXVAL (array_type_domain)
18272 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
18274 error_at (loc, "start-index and length fields necessary for "
18275 "using array notations in variable-length arrays");
18276 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18277 return error_mark_node;
18279 end_index = TYPE_MAXVAL (array_type_domain);
18280 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18281 end_index, integer_one_node);
18282 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18283 stride = build_int_cst (integer_type_node, 1);
18284 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18286 else if (initial_index != error_mark_node)
18288 /* If we are here, then there should be 2 possibilities:
18289 1. Array [EXPR : EXPR]
18290 2. Array [EXPR : EXPR : EXPR]
18292 start_index = initial_index;
18294 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18296 error_at (loc, "array notations cannot be used with function "
18297 "type");
18298 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18299 return error_mark_node;
18301 c_parser_consume_token (parser); /* consume the ':' */
18302 struct c_expr ce = c_parser_expression (parser);
18303 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18304 end_index = ce.value;
18305 if (!end_index || end_index == error_mark_node)
18307 c_parser_skip_to_end_of_block_or_statement (parser);
18308 return error_mark_node;
18310 if (c_parser_peek_token (parser)->type == CPP_COLON)
18312 c_parser_consume_token (parser);
18313 ce = c_parser_expression (parser);
18314 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18315 stride = ce.value;
18316 if (!stride || stride == error_mark_node)
18318 c_parser_skip_to_end_of_block_or_statement (parser);
18319 return error_mark_node;
18323 else
18324 c_parser_error (parser, "expected array notation expression");
18326 else
18327 c_parser_error (parser, "expected array notation expression");
18329 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18331 value_tree = build_array_notation_ref (loc, array_value, start_index,
18332 end_index, stride, type);
18333 if (value_tree != error_mark_node)
18334 SET_EXPR_LOCATION (value_tree, loc);
18335 return value_tree;
18338 /* Parse the body of a function declaration marked with "__RTL".
18340 The RTL parser works on the level of characters read from a
18341 FILE *, whereas c_parser works at the level of tokens.
18342 Square this circle by consuming all of the tokens up to and
18343 including the closing brace, recording the start/end of the RTL
18344 fragment, and reopening the file and re-reading the relevant
18345 lines within the RTL parser.
18347 This requires the opening and closing braces of the C function
18348 to be on separate lines from the RTL they wrap.
18350 Take ownership of START_WITH_PASS, if non-NULL. */
18352 void
18353 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18355 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18357 free (start_with_pass);
18358 return;
18361 location_t start_loc = c_parser_peek_token (parser)->location;
18363 /* Consume all tokens, up to the closing brace, handling
18364 matching pairs of braces in the rtl dump. */
18365 int num_open_braces = 1;
18366 while (1)
18368 switch (c_parser_peek_token (parser)->type)
18370 case CPP_OPEN_BRACE:
18371 num_open_braces++;
18372 break;
18373 case CPP_CLOSE_BRACE:
18374 if (--num_open_braces == 0)
18375 goto found_closing_brace;
18376 break;
18377 case CPP_EOF:
18378 error_at (start_loc, "no closing brace");
18379 free (start_with_pass);
18380 return;
18381 default:
18382 break;
18384 c_parser_consume_token (parser);
18387 found_closing_brace:
18388 /* At the closing brace; record its location. */
18389 location_t end_loc = c_parser_peek_token (parser)->location;
18391 /* Consume the closing brace. */
18392 c_parser_consume_token (parser);
18394 /* Invoke the RTL parser. */
18395 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18397 free (start_with_pass);
18398 return;
18401 /* If a pass name was provided for START_WITH_PASS, run the backend
18402 accordingly now, on the cfun created above, transferring
18403 ownership of START_WITH_PASS. */
18404 if (start_with_pass)
18405 run_rtl_passes (start_with_pass);
18408 #include "gt-c-c-parser.h"