c-family: add name_hint/deferred_diagnostic
[official-gcc.git] / gcc / c / c-parser.c
blob4afa9ceb5e94df4b84359b4e26d196099430cb0b
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 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
71 /* We need to walk over decls with incomplete struct/union/enum types
72 after parsing the whole translation unit.
73 In finish_decl(), if the decl is static, has incomplete
74 struct/union/enum type, it is appeneded to incomplete_record_decls.
75 In c_parser_translation_unit(), we iterate over incomplete_record_decls
76 and report error if any of the decls are still incomplete. */
78 vec<tree> incomplete_record_decls;
80 void
81 set_c_expr_source_range (c_expr *expr,
82 location_t start, location_t finish)
84 expr->src_range.m_start = start;
85 expr->src_range.m_finish = finish;
86 if (expr->value)
87 set_source_range (expr->value, start, finish);
90 void
91 set_c_expr_source_range (c_expr *expr,
92 source_range src_range)
94 expr->src_range = src_range;
95 if (expr->value)
96 set_source_range (expr->value, src_range);
100 /* Initialization routine for this file. */
102 void
103 c_parse_init (void)
105 /* The only initialization required is of the reserved word
106 identifiers. */
107 unsigned int i;
108 tree id;
109 int mask = 0;
111 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
112 the c_token structure. */
113 gcc_assert (RID_MAX <= 255);
115 mask |= D_CXXONLY;
116 if (!flag_isoc99)
117 mask |= D_C99;
118 if (flag_no_asm)
120 mask |= D_ASM | D_EXT;
121 if (!flag_isoc99)
122 mask |= D_EXT89;
124 if (!c_dialect_objc ())
125 mask |= D_OBJC | D_CXX_OBJC;
127 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
128 for (i = 0; i < num_c_common_reswords; i++)
130 /* If a keyword is disabled, do not enter it into the table
131 and so create a canonical spelling that isn't a keyword. */
132 if (c_common_reswords[i].disable & mask)
134 if (warn_cxx_compat
135 && (c_common_reswords[i].disable & D_CXXWARN))
137 id = get_identifier (c_common_reswords[i].word);
138 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
139 C_IS_RESERVED_WORD (id) = 1;
141 continue;
144 id = get_identifier (c_common_reswords[i].word);
145 C_SET_RID_CODE (id, c_common_reswords[i].rid);
146 C_IS_RESERVED_WORD (id) = 1;
147 ridpointers [(int) c_common_reswords[i].rid] = id;
150 for (i = 0; i < NUM_INT_N_ENTS; i++)
152 /* We always create the symbols but they aren't always supported. */
153 char name[50];
154 sprintf (name, "__int%d", int_n_data[i].bitsize);
155 id = get_identifier (name);
156 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
157 C_IS_RESERVED_WORD (id) = 1;
161 /* A parser structure recording information about the state and
162 context of parsing. Includes lexer information with up to two
163 tokens of look-ahead; more are not needed for C. */
164 struct GTY(()) c_parser {
165 /* The look-ahead tokens. */
166 c_token * GTY((skip)) tokens;
167 /* Buffer for look-ahead tokens. */
168 c_token tokens_buf[4];
169 /* How many look-ahead tokens are available (0 - 4, or
170 more if parsing from pre-lexed tokens). */
171 unsigned int tokens_avail;
172 /* True if a syntax error is being recovered from; false otherwise.
173 c_parser_error sets this flag. It should clear this flag when
174 enough tokens have been consumed to recover from the error. */
175 BOOL_BITFIELD error : 1;
176 /* True if we're processing a pragma, and shouldn't automatically
177 consume CPP_PRAGMA_EOL. */
178 BOOL_BITFIELD in_pragma : 1;
179 /* True if we're parsing the outermost block of an if statement. */
180 BOOL_BITFIELD in_if_block : 1;
181 /* True if we want to lex an untranslated string. */
182 BOOL_BITFIELD lex_untranslated_string : 1;
184 /* Objective-C specific parser/lexer information. */
186 /* True if we are in a context where the Objective-C "PQ" keywords
187 are considered keywords. */
188 BOOL_BITFIELD objc_pq_context : 1;
189 /* True if we are parsing a (potential) Objective-C foreach
190 statement. This is set to true after we parsed 'for (' and while
191 we wait for 'in' or ';' to decide if it's a standard C for loop or an
192 Objective-C foreach loop. */
193 BOOL_BITFIELD objc_could_be_foreach_context : 1;
194 /* The following flag is needed to contextualize Objective-C lexical
195 analysis. In some cases (e.g., 'int NSObject;'), it is
196 undesirable to bind an identifier to an Objective-C class, even
197 if a class with that name exists. */
198 BOOL_BITFIELD objc_need_raw_identifier : 1;
199 /* Nonzero if we're processing a __transaction statement. The value
200 is 1 | TM_STMT_ATTR_*. */
201 unsigned int in_transaction : 4;
202 /* True if we are in a context where the Objective-C "Property attribute"
203 keywords are valid. */
204 BOOL_BITFIELD objc_property_attr_context : 1;
206 /* Cilk Plus specific parser/lexer information. */
208 /* Buffer to hold all the tokens from parsing the vector attribute for the
209 SIMD-enabled functions (formerly known as elemental functions). */
210 vec <c_token, va_gc> *cilk_simd_fn_tokens;
212 /* Location of the last consumed token. */
213 location_t last_token_location;
216 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
218 c_token *
219 c_parser_tokens_buf (c_parser *parser, unsigned n)
221 return &parser->tokens_buf[n];
224 /* Return the error state of PARSER. */
226 bool
227 c_parser_error (c_parser *parser)
229 return parser->error;
232 /* Set the error state of PARSER to ERR. */
234 void
235 c_parser_set_error (c_parser *parser, bool err)
237 parser->error = err;
241 /* The actual parser and external interface. ??? Does this need to be
242 garbage-collected? */
244 static GTY (()) c_parser *the_parser;
246 /* Read in and lex a single token, storing it in *TOKEN. */
248 static void
249 c_lex_one_token (c_parser *parser, c_token *token)
251 timevar_push (TV_LEX);
253 token->type = c_lex_with_flags (&token->value, &token->location,
254 &token->flags,
255 (parser->lex_untranslated_string
256 ? C_LEX_STRING_NO_TRANSLATE : 0));
257 token->id_kind = C_ID_NONE;
258 token->keyword = RID_MAX;
259 token->pragma_kind = PRAGMA_NONE;
261 switch (token->type)
263 case CPP_NAME:
265 tree decl;
267 bool objc_force_identifier = parser->objc_need_raw_identifier;
268 if (c_dialect_objc ())
269 parser->objc_need_raw_identifier = false;
271 if (C_IS_RESERVED_WORD (token->value))
273 enum rid rid_code = C_RID_CODE (token->value);
275 if (rid_code == RID_CXX_COMPAT_WARN)
277 warning_at (token->location,
278 OPT_Wc___compat,
279 "identifier %qE conflicts with C++ keyword",
280 token->value);
282 else if (rid_code >= RID_FIRST_ADDR_SPACE
283 && rid_code <= RID_LAST_ADDR_SPACE)
285 addr_space_t as;
286 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
287 targetm.addr_space.diagnose_usage (as, token->location);
288 token->id_kind = C_ID_ADDRSPACE;
289 token->keyword = rid_code;
290 break;
292 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
294 /* We found an Objective-C "pq" keyword (in, out,
295 inout, bycopy, byref, oneway). They need special
296 care because the interpretation depends on the
297 context. */
298 if (parser->objc_pq_context)
300 token->type = CPP_KEYWORD;
301 token->keyword = rid_code;
302 break;
304 else if (parser->objc_could_be_foreach_context
305 && rid_code == RID_IN)
307 /* We are in Objective-C, inside a (potential)
308 foreach context (which means after having
309 parsed 'for (', but before having parsed ';'),
310 and we found 'in'. We consider it the keyword
311 which terminates the declaration at the
312 beginning of a foreach-statement. Note that
313 this means you can't use 'in' for anything else
314 in that context; in particular, in Objective-C
315 you can't use 'in' as the name of the running
316 variable in a C for loop. We could potentially
317 try to add code here to disambiguate, but it
318 seems a reasonable limitation. */
319 token->type = CPP_KEYWORD;
320 token->keyword = rid_code;
321 break;
323 /* Else, "pq" keywords outside of the "pq" context are
324 not keywords, and we fall through to the code for
325 normal tokens. */
327 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
329 /* We found an Objective-C "property attribute"
330 keyword (getter, setter, readonly, etc). These are
331 only valid in the property context. */
332 if (parser->objc_property_attr_context)
334 token->type = CPP_KEYWORD;
335 token->keyword = rid_code;
336 break;
338 /* Else they are not special keywords.
341 else if (c_dialect_objc ()
342 && (OBJC_IS_AT_KEYWORD (rid_code)
343 || OBJC_IS_CXX_KEYWORD (rid_code)))
345 /* We found one of the Objective-C "@" keywords (defs,
346 selector, synchronized, etc) or one of the
347 Objective-C "cxx" keywords (class, private,
348 protected, public, try, catch, throw) without a
349 preceding '@' sign. Do nothing and fall through to
350 the code for normal tokens (in C++ we would still
351 consider the CXX ones keywords, but not in C). */
354 else
356 token->type = CPP_KEYWORD;
357 token->keyword = rid_code;
358 break;
362 decl = lookup_name (token->value);
363 if (decl)
365 if (TREE_CODE (decl) == TYPE_DECL)
367 token->id_kind = C_ID_TYPENAME;
368 break;
371 else if (c_dialect_objc ())
373 tree objc_interface_decl = objc_is_class_name (token->value);
374 /* Objective-C class names are in the same namespace as
375 variables and typedefs, and hence are shadowed by local
376 declarations. */
377 if (objc_interface_decl
378 && (!objc_force_identifier || global_bindings_p ()))
380 token->value = objc_interface_decl;
381 token->id_kind = C_ID_CLASSNAME;
382 break;
385 token->id_kind = C_ID_ID;
387 break;
388 case CPP_AT_NAME:
389 /* This only happens in Objective-C; it must be a keyword. */
390 token->type = CPP_KEYWORD;
391 switch (C_RID_CODE (token->value))
393 /* Replace 'class' with '@class', 'private' with '@private',
394 etc. This prevents confusion with the C++ keyword
395 'class', and makes the tokens consistent with other
396 Objective-C 'AT' keywords. For example '@class' is
397 reported as RID_AT_CLASS which is consistent with
398 '@synchronized', which is reported as
399 RID_AT_SYNCHRONIZED.
401 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
402 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
403 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
404 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
405 case RID_THROW: token->keyword = RID_AT_THROW; break;
406 case RID_TRY: token->keyword = RID_AT_TRY; break;
407 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
408 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
409 default: token->keyword = C_RID_CODE (token->value);
411 break;
412 case CPP_COLON:
413 case CPP_COMMA:
414 case CPP_CLOSE_PAREN:
415 case CPP_SEMICOLON:
416 /* These tokens may affect the interpretation of any identifiers
417 following, if doing Objective-C. */
418 if (c_dialect_objc ())
419 parser->objc_need_raw_identifier = false;
420 break;
421 case CPP_PRAGMA:
422 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
423 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
424 token->value = NULL;
425 break;
426 default:
427 break;
429 timevar_pop (TV_LEX);
432 /* Return a pointer to the next token from PARSER, reading it in if
433 necessary. */
435 c_token *
436 c_parser_peek_token (c_parser *parser)
438 if (parser->tokens_avail == 0)
440 c_lex_one_token (parser, &parser->tokens[0]);
441 parser->tokens_avail = 1;
443 return &parser->tokens[0];
446 /* Return a pointer to the next-but-one token from PARSER, reading it
447 in if necessary. The next token is already read in. */
449 c_token *
450 c_parser_peek_2nd_token (c_parser *parser)
452 if (parser->tokens_avail >= 2)
453 return &parser->tokens[1];
454 gcc_assert (parser->tokens_avail == 1);
455 gcc_assert (parser->tokens[0].type != CPP_EOF);
456 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
457 c_lex_one_token (parser, &parser->tokens[1]);
458 parser->tokens_avail = 2;
459 return &parser->tokens[1];
462 /* Return a pointer to the Nth token from PARSER, reading it
463 in if necessary. The N-1th token is already read in. */
465 c_token *
466 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
468 /* N is 1-based, not zero-based. */
469 gcc_assert (n > 0);
471 if (parser->tokens_avail >= n)
472 return &parser->tokens[n - 1];
473 gcc_assert (parser->tokens_avail == n - 1);
474 c_lex_one_token (parser, &parser->tokens[n - 1]);
475 parser->tokens_avail = n;
476 return &parser->tokens[n - 1];
479 bool
480 c_keyword_starts_typename (enum rid keyword)
482 switch (keyword)
484 case RID_UNSIGNED:
485 case RID_LONG:
486 case RID_SHORT:
487 case RID_SIGNED:
488 case RID_COMPLEX:
489 case RID_INT:
490 case RID_CHAR:
491 case RID_FLOAT:
492 case RID_DOUBLE:
493 case RID_VOID:
494 case RID_DFLOAT32:
495 case RID_DFLOAT64:
496 case RID_DFLOAT128:
497 CASE_RID_FLOATN_NX:
498 case RID_BOOL:
499 case RID_ENUM:
500 case RID_STRUCT:
501 case RID_UNION:
502 case RID_TYPEOF:
503 case RID_CONST:
504 case RID_ATOMIC:
505 case RID_VOLATILE:
506 case RID_RESTRICT:
507 case RID_ATTRIBUTE:
508 case RID_FRACT:
509 case RID_ACCUM:
510 case RID_SAT:
511 case RID_AUTO_TYPE:
512 return true;
513 default:
514 if (keyword >= RID_FIRST_INT_N
515 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
516 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
517 return true;
518 return false;
522 /* Return true if TOKEN can start a type name,
523 false otherwise. */
524 bool
525 c_token_starts_typename (c_token *token)
527 switch (token->type)
529 case CPP_NAME:
530 switch (token->id_kind)
532 case C_ID_ID:
533 return false;
534 case C_ID_ADDRSPACE:
535 return true;
536 case C_ID_TYPENAME:
537 return true;
538 case C_ID_CLASSNAME:
539 gcc_assert (c_dialect_objc ());
540 return true;
541 default:
542 gcc_unreachable ();
544 case CPP_KEYWORD:
545 return c_keyword_starts_typename (token->keyword);
546 case CPP_LESS:
547 if (c_dialect_objc ())
548 return true;
549 return false;
550 default:
551 return false;
555 /* Return true if the next token from PARSER can start a type name,
556 false otherwise. LA specifies how to do lookahead in order to
557 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
559 static inline bool
560 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
562 c_token *token = c_parser_peek_token (parser);
563 if (c_token_starts_typename (token))
564 return true;
566 /* Try a bit harder to detect an unknown typename. */
567 if (la != cla_prefer_id
568 && token->type == CPP_NAME
569 && token->id_kind == C_ID_ID
571 /* Do not try too hard when we could have "object in array". */
572 && !parser->objc_could_be_foreach_context
574 && (la == cla_prefer_type
575 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
576 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
578 /* Only unknown identifiers. */
579 && !lookup_name (token->value))
580 return true;
582 return false;
585 /* Return true if TOKEN is a type qualifier, false otherwise. */
586 static bool
587 c_token_is_qualifier (c_token *token)
589 switch (token->type)
591 case CPP_NAME:
592 switch (token->id_kind)
594 case C_ID_ADDRSPACE:
595 return true;
596 default:
597 return false;
599 case CPP_KEYWORD:
600 switch (token->keyword)
602 case RID_CONST:
603 case RID_VOLATILE:
604 case RID_RESTRICT:
605 case RID_ATTRIBUTE:
606 case RID_ATOMIC:
607 return true;
608 default:
609 return false;
611 case CPP_LESS:
612 return false;
613 default:
614 gcc_unreachable ();
618 /* Return true if the next token from PARSER is a type qualifier,
619 false otherwise. */
620 static inline bool
621 c_parser_next_token_is_qualifier (c_parser *parser)
623 c_token *token = c_parser_peek_token (parser);
624 return c_token_is_qualifier (token);
627 /* Return true if TOKEN can start declaration specifiers, false
628 otherwise. */
629 static bool
630 c_token_starts_declspecs (c_token *token)
632 switch (token->type)
634 case CPP_NAME:
635 switch (token->id_kind)
637 case C_ID_ID:
638 return false;
639 case C_ID_ADDRSPACE:
640 return true;
641 case C_ID_TYPENAME:
642 return true;
643 case C_ID_CLASSNAME:
644 gcc_assert (c_dialect_objc ());
645 return true;
646 default:
647 gcc_unreachable ();
649 case CPP_KEYWORD:
650 switch (token->keyword)
652 case RID_STATIC:
653 case RID_EXTERN:
654 case RID_REGISTER:
655 case RID_TYPEDEF:
656 case RID_INLINE:
657 case RID_NORETURN:
658 case RID_AUTO:
659 case RID_THREAD:
660 case RID_UNSIGNED:
661 case RID_LONG:
662 case RID_SHORT:
663 case RID_SIGNED:
664 case RID_COMPLEX:
665 case RID_INT:
666 case RID_CHAR:
667 case RID_FLOAT:
668 case RID_DOUBLE:
669 case RID_VOID:
670 case RID_DFLOAT32:
671 case RID_DFLOAT64:
672 case RID_DFLOAT128:
673 CASE_RID_FLOATN_NX:
674 case RID_BOOL:
675 case RID_ENUM:
676 case RID_STRUCT:
677 case RID_UNION:
678 case RID_TYPEOF:
679 case RID_CONST:
680 case RID_VOLATILE:
681 case RID_RESTRICT:
682 case RID_ATTRIBUTE:
683 case RID_FRACT:
684 case RID_ACCUM:
685 case RID_SAT:
686 case RID_ALIGNAS:
687 case RID_ATOMIC:
688 case RID_AUTO_TYPE:
689 return true;
690 default:
691 if (token->keyword >= RID_FIRST_INT_N
692 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
693 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
694 return true;
695 return false;
697 case CPP_LESS:
698 if (c_dialect_objc ())
699 return true;
700 return false;
701 default:
702 return false;
707 /* Return true if TOKEN can start declaration specifiers or a static
708 assertion, false otherwise. */
709 static bool
710 c_token_starts_declaration (c_token *token)
712 if (c_token_starts_declspecs (token)
713 || token->keyword == RID_STATIC_ASSERT)
714 return true;
715 else
716 return false;
719 /* Return true if the next token from PARSER can start declaration
720 specifiers, false otherwise. */
721 bool
722 c_parser_next_token_starts_declspecs (c_parser *parser)
724 c_token *token = c_parser_peek_token (parser);
726 /* In Objective-C, a classname normally starts a declspecs unless it
727 is immediately followed by a dot. In that case, it is the
728 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
729 setter/getter on the class. c_token_starts_declspecs() can't
730 differentiate between the two cases because it only checks the
731 current token, so we have a special check here. */
732 if (c_dialect_objc ()
733 && token->type == CPP_NAME
734 && token->id_kind == C_ID_CLASSNAME
735 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
736 return false;
738 return c_token_starts_declspecs (token);
741 /* Return true if the next tokens from PARSER can start declaration
742 specifiers or a static assertion, false otherwise. */
743 bool
744 c_parser_next_tokens_start_declaration (c_parser *parser)
746 c_token *token = c_parser_peek_token (parser);
748 /* Same as above. */
749 if (c_dialect_objc ()
750 && token->type == CPP_NAME
751 && token->id_kind == C_ID_CLASSNAME
752 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
753 return false;
755 /* Labels do not start declarations. */
756 if (token->type == CPP_NAME
757 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
758 return false;
760 if (c_token_starts_declaration (token))
761 return true;
763 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
764 return true;
766 return false;
769 /* Consume the next token from PARSER. */
771 void
772 c_parser_consume_token (c_parser *parser)
774 gcc_assert (parser->tokens_avail >= 1);
775 gcc_assert (parser->tokens[0].type != CPP_EOF);
776 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
777 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
778 parser->last_token_location = parser->tokens[0].location;
779 if (parser->tokens != &parser->tokens_buf[0])
780 parser->tokens++;
781 else if (parser->tokens_avail == 2)
782 parser->tokens[0] = parser->tokens[1];
783 parser->tokens_avail--;
786 /* Expect the current token to be a #pragma. Consume it and remember
787 that we've begun parsing a pragma. */
789 static void
790 c_parser_consume_pragma (c_parser *parser)
792 gcc_assert (!parser->in_pragma);
793 gcc_assert (parser->tokens_avail >= 1);
794 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
795 if (parser->tokens != &parser->tokens_buf[0])
796 parser->tokens++;
797 else if (parser->tokens_avail == 2)
798 parser->tokens[0] = parser->tokens[1];
799 parser->tokens_avail--;
800 parser->in_pragma = true;
803 /* Update the global input_location from TOKEN. */
804 static inline void
805 c_parser_set_source_position_from_token (c_token *token)
807 if (token->type != CPP_EOF)
809 input_location = token->location;
813 /* Helper function for c_parser_error.
814 Having peeked a token of kind TOK1_KIND that might signify
815 a conflict marker, peek successor tokens to determine
816 if we actually do have a conflict marker.
817 Specifically, we consider a run of 7 '<', '=' or '>' characters
818 at the start of a line as a conflict marker.
819 These come through the lexer as three pairs and a single,
820 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
821 If it returns true, *OUT_LOC is written to with the location/range
822 of the marker. */
824 static bool
825 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
826 location_t *out_loc)
828 c_token *token2 = c_parser_peek_2nd_token (parser);
829 if (token2->type != tok1_kind)
830 return false;
831 c_token *token3 = c_parser_peek_nth_token (parser, 3);
832 if (token3->type != tok1_kind)
833 return false;
834 c_token *token4 = c_parser_peek_nth_token (parser, 4);
835 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
836 return false;
838 /* It must be at the start of the line. */
839 location_t start_loc = c_parser_peek_token (parser)->location;
840 if (LOCATION_COLUMN (start_loc) != 1)
841 return false;
843 /* We have a conflict marker. Construct a location of the form:
844 <<<<<<<
845 ^~~~~~~
846 with start == caret, finishing at the end of the marker. */
847 location_t finish_loc = get_finish (token4->location);
848 *out_loc = make_location (start_loc, start_loc, finish_loc);
850 return true;
853 /* Issue a diagnostic of the form
854 FILE:LINE: MESSAGE before TOKEN
855 where TOKEN is the next token in the input stream of PARSER.
856 MESSAGE (specified by the caller) is usually of the form "expected
857 OTHER-TOKEN".
859 Use RICHLOC as the location of the diagnostic.
861 Do not issue a diagnostic if still recovering from an error.
863 Return true iff an error was actually emitted.
865 ??? This is taken from the C++ parser, but building up messages in
866 this way is not i18n-friendly and some other approach should be
867 used. */
869 static bool
870 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
871 rich_location *richloc)
873 c_token *token = c_parser_peek_token (parser);
874 if (parser->error)
875 return false;
876 parser->error = true;
877 if (!gmsgid)
878 return false;
880 /* If this is actually a conflict marker, report it as such. */
881 if (token->type == CPP_LSHIFT
882 || token->type == CPP_RSHIFT
883 || token->type == CPP_EQ_EQ)
885 location_t loc;
886 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
888 error_at (loc, "version control conflict marker in file");
889 return true;
893 c_parse_error (gmsgid,
894 /* Because c_parse_error does not understand
895 CPP_KEYWORD, keywords are treated like
896 identifiers. */
897 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
898 /* ??? The C parser does not save the cpp flags of a
899 token, we need to pass 0 here and we will not get
900 the source spelling of some tokens but rather the
901 canonical spelling. */
902 token->value, /*flags=*/0, richloc);
903 return true;
906 /* As c_parser_error_richloc, but issue the message at the
907 location of PARSER's next token, or at input_location
908 if the next token is EOF. */
910 bool
911 c_parser_error (c_parser *parser, const char *gmsgid)
913 c_token *token = c_parser_peek_token (parser);
914 c_parser_set_source_position_from_token (token);
915 rich_location richloc (line_table, input_location);
916 return c_parser_error_richloc (parser, gmsgid, &richloc);
919 /* Some tokens naturally come in pairs e.g.'(' and ')'.
920 This class is for tracking such a matching pair of symbols.
921 In particular, it tracks the location of the first token,
922 so that if the second token is missing, we can highlight the
923 location of the first token when notifying the user about the
924 problem. */
926 template <typename traits_t>
927 class token_pair
929 public:
930 /* token_pair's ctor. */
931 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
933 /* If the next token is the opening symbol for this pair, consume it and
934 return true.
935 Otherwise, issue an error and return false.
936 In either case, record the location of the opening token. */
938 bool require_open (c_parser *parser)
940 c_token *token = c_parser_peek_token (parser);
941 if (token)
942 m_open_loc = token->location;
944 return c_parser_require (parser, traits_t::open_token_type,
945 traits_t::open_gmsgid);
948 /* Consume the next token from PARSER, recording its location as
949 that of the opening token within the pair. */
951 void consume_open (c_parser *parser)
953 c_token *token = c_parser_peek_token (parser);
954 gcc_assert (token->type == traits_t::open_token_type);
955 m_open_loc = token->location;
956 c_parser_consume_token (parser);
959 /* If the next token is the closing symbol for this pair, consume it
960 and return true.
961 Otherwise, issue an error, highlighting the location of the
962 corresponding opening token, and return false. */
964 bool require_close (c_parser *parser) const
966 return c_parser_require (parser, traits_t::close_token_type,
967 traits_t::close_gmsgid, m_open_loc);
970 /* Like token_pair::require_close, except that tokens will be skipped
971 until the desired token is found. An error message is still produced
972 if the next token is not as expected. */
974 void skip_until_found_close (c_parser *parser) const
976 c_parser_skip_until_found (parser, traits_t::close_token_type,
977 traits_t::close_gmsgid, m_open_loc);
980 private:
981 location_t m_open_loc;
984 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
986 struct matching_paren_traits
988 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
989 static const char * const open_gmsgid;
990 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
991 static const char * const close_gmsgid;
994 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
995 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
997 /* "matching_parens" is a token_pair<T> class for tracking matching
998 pairs of parentheses. */
1000 typedef token_pair<matching_paren_traits> matching_parens;
1002 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1004 struct matching_brace_traits
1006 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1007 static const char * const open_gmsgid;
1008 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1009 static const char * const close_gmsgid;
1012 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1013 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1015 /* "matching_braces" is a token_pair<T> class for tracking matching
1016 pairs of braces. */
1018 typedef token_pair<matching_brace_traits> matching_braces;
1020 /* Get a description of the matching symbol to TYPE e.g. "(" for
1021 CPP_CLOSE_PAREN. */
1023 static const char *
1024 get_matching_symbol (enum cpp_ttype type)
1026 switch (type)
1028 default:
1029 gcc_unreachable ();
1030 return "";
1031 case CPP_CLOSE_PAREN:
1032 return "(";
1033 case CPP_CLOSE_BRACE:
1034 return "{";
1038 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1039 issue the error MSGID. If MSGID is NULL then a message has already
1040 been produced and no message will be produced this time. Returns
1041 true if found, false otherwise.
1043 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1044 within any error as the location of an "opening" token matching
1045 the close token TYPE (e.g. the location of the '(' when TYPE is
1046 CPP_CLOSE_PAREN).
1048 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1049 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1050 attempt to generate a fix-it hint for the problem.
1051 Otherwise msgid describes multiple token types (e.g.
1052 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1053 generate a fix-it hint. */
1055 bool
1056 c_parser_require (c_parser *parser,
1057 enum cpp_ttype type,
1058 const char *msgid,
1059 location_t matching_location,
1060 bool type_is_unique)
1062 if (c_parser_next_token_is (parser, type))
1064 c_parser_consume_token (parser);
1065 return true;
1067 else
1069 location_t next_token_loc = c_parser_peek_token (parser)->location;
1070 gcc_rich_location richloc (next_token_loc);
1072 /* Potentially supply a fix-it hint, suggesting to add the
1073 missing token immediately after the *previous* token.
1074 This may move the primary location within richloc. */
1075 if (!parser->error && type_is_unique)
1076 maybe_suggest_missing_token_insertion (&richloc, type,
1077 parser->last_token_location);
1079 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1080 Attempt to consolidate diagnostics by printing it as a
1081 secondary range within the main diagnostic. */
1082 bool added_matching_location = false;
1083 if (matching_location != UNKNOWN_LOCATION)
1084 added_matching_location
1085 = richloc.add_location_if_nearby (matching_location);
1087 if (c_parser_error_richloc (parser, msgid, &richloc))
1088 /* If we weren't able to consolidate matching_location, then
1089 print it as a secondary diagnostic. */
1090 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1091 inform (matching_location, "to match this %qs",
1092 get_matching_symbol (type));
1094 return false;
1098 /* If the next token is the indicated keyword, consume it. Otherwise,
1099 issue the error MSGID. Returns true if found, false otherwise. */
1101 static bool
1102 c_parser_require_keyword (c_parser *parser,
1103 enum rid keyword,
1104 const char *msgid)
1106 if (c_parser_next_token_is_keyword (parser, keyword))
1108 c_parser_consume_token (parser);
1109 return true;
1111 else
1113 c_parser_error (parser, msgid);
1114 return false;
1118 /* Like c_parser_require, except that tokens will be skipped until the
1119 desired token is found. An error message is still produced if the
1120 next token is not as expected. If MSGID is NULL then a message has
1121 already been produced and no message will be produced this
1122 time.
1124 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1125 within any error as the location of an "opening" token matching
1126 the close token TYPE (e.g. the location of the '(' when TYPE is
1127 CPP_CLOSE_PAREN). */
1129 void
1130 c_parser_skip_until_found (c_parser *parser,
1131 enum cpp_ttype type,
1132 const char *msgid,
1133 location_t matching_location)
1135 unsigned nesting_depth = 0;
1137 if (c_parser_require (parser, type, msgid, matching_location))
1138 return;
1140 /* Skip tokens until the desired token is found. */
1141 while (true)
1143 /* Peek at the next token. */
1144 c_token *token = c_parser_peek_token (parser);
1145 /* If we've reached the token we want, consume it and stop. */
1146 if (token->type == type && !nesting_depth)
1148 c_parser_consume_token (parser);
1149 break;
1152 /* If we've run out of tokens, stop. */
1153 if (token->type == CPP_EOF)
1154 return;
1155 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1156 return;
1157 if (token->type == CPP_OPEN_BRACE
1158 || token->type == CPP_OPEN_PAREN
1159 || token->type == CPP_OPEN_SQUARE)
1160 ++nesting_depth;
1161 else if (token->type == CPP_CLOSE_BRACE
1162 || token->type == CPP_CLOSE_PAREN
1163 || token->type == CPP_CLOSE_SQUARE)
1165 if (nesting_depth-- == 0)
1166 break;
1168 /* Consume this token. */
1169 c_parser_consume_token (parser);
1171 parser->error = false;
1174 /* Skip tokens until the end of a parameter is found, but do not
1175 consume the comma, semicolon or closing delimiter. */
1177 static void
1178 c_parser_skip_to_end_of_parameter (c_parser *parser)
1180 unsigned nesting_depth = 0;
1182 while (true)
1184 c_token *token = c_parser_peek_token (parser);
1185 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1186 && !nesting_depth)
1187 break;
1188 /* If we've run out of tokens, stop. */
1189 if (token->type == CPP_EOF)
1190 return;
1191 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1192 return;
1193 if (token->type == CPP_OPEN_BRACE
1194 || token->type == CPP_OPEN_PAREN
1195 || token->type == CPP_OPEN_SQUARE)
1196 ++nesting_depth;
1197 else if (token->type == CPP_CLOSE_BRACE
1198 || token->type == CPP_CLOSE_PAREN
1199 || token->type == CPP_CLOSE_SQUARE)
1201 if (nesting_depth-- == 0)
1202 break;
1204 /* Consume this token. */
1205 c_parser_consume_token (parser);
1207 parser->error = false;
1210 /* Expect to be at the end of the pragma directive and consume an
1211 end of line marker. */
1213 static void
1214 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1216 gcc_assert (parser->in_pragma);
1217 parser->in_pragma = false;
1219 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1220 c_parser_error (parser, "expected end of line");
1222 cpp_ttype token_type;
1225 c_token *token = c_parser_peek_token (parser);
1226 token_type = token->type;
1227 if (token_type == CPP_EOF)
1228 break;
1229 c_parser_consume_token (parser);
1231 while (token_type != CPP_PRAGMA_EOL);
1233 parser->error = false;
1236 /* Skip tokens until we have consumed an entire block, or until we
1237 have consumed a non-nested ';'. */
1239 static void
1240 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1242 unsigned nesting_depth = 0;
1243 bool save_error = parser->error;
1245 while (true)
1247 c_token *token;
1249 /* Peek at the next token. */
1250 token = c_parser_peek_token (parser);
1252 switch (token->type)
1254 case CPP_EOF:
1255 return;
1257 case CPP_PRAGMA_EOL:
1258 if (parser->in_pragma)
1259 return;
1260 break;
1262 case CPP_SEMICOLON:
1263 /* If the next token is a ';', we have reached the
1264 end of the statement. */
1265 if (!nesting_depth)
1267 /* Consume the ';'. */
1268 c_parser_consume_token (parser);
1269 goto finished;
1271 break;
1273 case CPP_CLOSE_BRACE:
1274 /* If the next token is a non-nested '}', then we have
1275 reached the end of the current block. */
1276 if (nesting_depth == 0 || --nesting_depth == 0)
1278 c_parser_consume_token (parser);
1279 goto finished;
1281 break;
1283 case CPP_OPEN_BRACE:
1284 /* If it the next token is a '{', then we are entering a new
1285 block. Consume the entire block. */
1286 ++nesting_depth;
1287 break;
1289 case CPP_PRAGMA:
1290 /* If we see a pragma, consume the whole thing at once. We
1291 have some safeguards against consuming pragmas willy-nilly.
1292 Normally, we'd expect to be here with parser->error set,
1293 which disables these safeguards. But it's possible to get
1294 here for secondary error recovery, after parser->error has
1295 been cleared. */
1296 c_parser_consume_pragma (parser);
1297 c_parser_skip_to_pragma_eol (parser);
1298 parser->error = save_error;
1299 continue;
1301 default:
1302 break;
1305 c_parser_consume_token (parser);
1308 finished:
1309 parser->error = false;
1312 /* CPP's options (initialized by c-opts.c). */
1313 extern cpp_options *cpp_opts;
1315 /* Save the warning flags which are controlled by __extension__. */
1317 static inline int
1318 disable_extension_diagnostics (void)
1320 int ret = (pedantic
1321 | (warn_pointer_arith << 1)
1322 | (warn_traditional << 2)
1323 | (flag_iso << 3)
1324 | (warn_long_long << 4)
1325 | (warn_cxx_compat << 5)
1326 | (warn_overlength_strings << 6)
1327 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1328 play tricks to properly restore it. */
1329 | ((warn_c90_c99_compat == 1) << 7)
1330 | ((warn_c90_c99_compat == -1) << 8)
1331 /* Similarly for warn_c99_c11_compat. */
1332 | ((warn_c99_c11_compat == 1) << 9)
1333 | ((warn_c99_c11_compat == -1) << 10)
1335 cpp_opts->cpp_pedantic = pedantic = 0;
1336 warn_pointer_arith = 0;
1337 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1338 flag_iso = 0;
1339 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1340 warn_cxx_compat = 0;
1341 warn_overlength_strings = 0;
1342 warn_c90_c99_compat = 0;
1343 warn_c99_c11_compat = 0;
1344 return ret;
1347 /* Restore the warning flags which are controlled by __extension__.
1348 FLAGS is the return value from disable_extension_diagnostics. */
1350 static inline void
1351 restore_extension_diagnostics (int flags)
1353 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1354 warn_pointer_arith = (flags >> 1) & 1;
1355 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1356 flag_iso = (flags >> 3) & 1;
1357 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1358 warn_cxx_compat = (flags >> 5) & 1;
1359 warn_overlength_strings = (flags >> 6) & 1;
1360 /* See above for why is this needed. */
1361 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1362 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1365 /* Helper data structure for parsing #pragma acc routine. */
1366 struct oacc_routine_data {
1367 bool error_seen; /* Set if error has been reported. */
1368 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1369 tree clauses;
1370 location_t loc;
1373 static void c_parser_external_declaration (c_parser *);
1374 static void c_parser_asm_definition (c_parser *);
1375 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1376 bool, bool, tree *, vec<c_token>,
1377 struct oacc_routine_data * = NULL,
1378 bool * = NULL);
1379 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1380 static void c_parser_static_assert_declaration (c_parser *);
1381 static struct c_typespec c_parser_enum_specifier (c_parser *);
1382 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1383 static tree c_parser_struct_declaration (c_parser *);
1384 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1385 static tree c_parser_alignas_specifier (c_parser *);
1386 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1387 c_dtr_syn, bool *);
1388 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1389 bool,
1390 struct c_declarator *);
1391 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1392 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1393 tree);
1394 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1395 static tree c_parser_simple_asm_expr (c_parser *);
1396 static tree c_parser_attributes (c_parser *);
1397 static struct c_expr c_parser_initializer (c_parser *);
1398 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1399 struct obstack *);
1400 static void c_parser_initelt (c_parser *, struct obstack *);
1401 static void c_parser_initval (c_parser *, struct c_expr *,
1402 struct obstack *);
1403 static tree c_parser_compound_statement (c_parser *);
1404 static void c_parser_compound_statement_nostart (c_parser *);
1405 static void c_parser_label (c_parser *);
1406 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1407 static void c_parser_statement_after_labels (c_parser *, bool *,
1408 vec<tree> * = NULL);
1409 static tree c_parser_c99_block_statement (c_parser *, bool *,
1410 location_t * = NULL);
1411 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1412 static void c_parser_switch_statement (c_parser *, bool *);
1413 static void c_parser_while_statement (c_parser *, bool, bool *);
1414 static void c_parser_do_statement (c_parser *, bool);
1415 static void c_parser_for_statement (c_parser *, bool, bool *);
1416 static tree c_parser_asm_statement (c_parser *);
1417 static tree c_parser_asm_operands (c_parser *);
1418 static tree c_parser_asm_goto_operands (c_parser *);
1419 static tree c_parser_asm_clobbers (c_parser *);
1420 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1421 tree = NULL_TREE);
1422 static struct c_expr c_parser_conditional_expression (c_parser *,
1423 struct c_expr *, tree);
1424 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1425 tree);
1426 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1427 static struct c_expr c_parser_unary_expression (c_parser *);
1428 static struct c_expr c_parser_sizeof_expression (c_parser *);
1429 static struct c_expr c_parser_alignof_expression (c_parser *);
1430 static struct c_expr c_parser_postfix_expression (c_parser *);
1431 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1432 struct c_type_name *,
1433 location_t);
1434 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1435 location_t loc,
1436 struct c_expr);
1437 static tree c_parser_transaction (c_parser *, enum rid);
1438 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1439 static tree c_parser_transaction_cancel (c_parser *);
1440 static struct c_expr c_parser_expression (c_parser *);
1441 static struct c_expr c_parser_expression_conv (c_parser *);
1442 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1443 vec<tree, va_gc> **, location_t *,
1444 tree *, vec<location_t> *,
1445 unsigned int * = NULL);
1446 static void c_parser_oacc_declare (c_parser *);
1447 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1448 static void c_parser_oacc_update (c_parser *);
1449 static void c_parser_omp_construct (c_parser *, bool *);
1450 static void c_parser_omp_threadprivate (c_parser *);
1451 static void c_parser_omp_barrier (c_parser *);
1452 static void c_parser_omp_flush (c_parser *);
1453 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1454 tree, tree *, bool *);
1455 static void c_parser_omp_taskwait (c_parser *);
1456 static void c_parser_omp_taskyield (c_parser *);
1457 static void c_parser_omp_cancel (c_parser *);
1459 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1460 pragma_stmt, pragma_compound };
1461 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1462 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1463 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1464 static void c_parser_omp_end_declare_target (c_parser *);
1465 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1466 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1467 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1469 /* These Objective-C parser functions are only ever called when
1470 compiling Objective-C. */
1471 static void c_parser_objc_class_definition (c_parser *, tree);
1472 static void c_parser_objc_class_instance_variables (c_parser *);
1473 static void c_parser_objc_class_declaration (c_parser *);
1474 static void c_parser_objc_alias_declaration (c_parser *);
1475 static void c_parser_objc_protocol_definition (c_parser *, tree);
1476 static bool c_parser_objc_method_type (c_parser *);
1477 static void c_parser_objc_method_definition (c_parser *);
1478 static void c_parser_objc_methodprotolist (c_parser *);
1479 static void c_parser_objc_methodproto (c_parser *);
1480 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1481 static tree c_parser_objc_type_name (c_parser *);
1482 static tree c_parser_objc_protocol_refs (c_parser *);
1483 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1484 static void c_parser_objc_synchronized_statement (c_parser *);
1485 static tree c_parser_objc_selector (c_parser *);
1486 static tree c_parser_objc_selector_arg (c_parser *);
1487 static tree c_parser_objc_receiver (c_parser *);
1488 static tree c_parser_objc_message_args (c_parser *);
1489 static tree c_parser_objc_keywordexpr (c_parser *);
1490 static void c_parser_objc_at_property_declaration (c_parser *);
1491 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1492 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1493 static bool c_parser_objc_diagnose_bad_element_prefix
1494 (c_parser *, struct c_declspecs *);
1496 /* Cilk Plus supporting routines. */
1497 static void c_parser_cilk_simd (c_parser *, bool *);
1498 static void c_parser_cilk_for (c_parser *, tree, bool *);
1499 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1500 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1501 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1502 static void c_parser_cilk_grainsize (c_parser *, bool *);
1504 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1506 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1508 translation-unit:
1509 external-declarations
1511 external-declarations:
1512 external-declaration
1513 external-declarations external-declaration
1515 GNU extensions:
1517 translation-unit:
1518 empty
1521 static void
1522 c_parser_translation_unit (c_parser *parser)
1524 if (c_parser_next_token_is (parser, CPP_EOF))
1526 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1527 "ISO C forbids an empty translation unit");
1529 else
1531 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1532 mark_valid_location_for_stdc_pragma (false);
1535 ggc_collect ();
1536 c_parser_external_declaration (parser);
1537 obstack_free (&parser_obstack, obstack_position);
1539 while (c_parser_next_token_is_not (parser, CPP_EOF));
1542 unsigned int i;
1543 tree decl;
1544 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1545 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1546 error ("storage size of %q+D isn%'t known", decl);
1549 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1551 external-declaration:
1552 function-definition
1553 declaration
1555 GNU extensions:
1557 external-declaration:
1558 asm-definition
1560 __extension__ external-declaration
1562 Objective-C:
1564 external-declaration:
1565 objc-class-definition
1566 objc-class-declaration
1567 objc-alias-declaration
1568 objc-protocol-definition
1569 objc-method-definition
1570 @end
1573 static void
1574 c_parser_external_declaration (c_parser *parser)
1576 int ext;
1577 switch (c_parser_peek_token (parser)->type)
1579 case CPP_KEYWORD:
1580 switch (c_parser_peek_token (parser)->keyword)
1582 case RID_EXTENSION:
1583 ext = disable_extension_diagnostics ();
1584 c_parser_consume_token (parser);
1585 c_parser_external_declaration (parser);
1586 restore_extension_diagnostics (ext);
1587 break;
1588 case RID_ASM:
1589 c_parser_asm_definition (parser);
1590 break;
1591 case RID_AT_INTERFACE:
1592 case RID_AT_IMPLEMENTATION:
1593 gcc_assert (c_dialect_objc ());
1594 c_parser_objc_class_definition (parser, NULL_TREE);
1595 break;
1596 case RID_AT_CLASS:
1597 gcc_assert (c_dialect_objc ());
1598 c_parser_objc_class_declaration (parser);
1599 break;
1600 case RID_AT_ALIAS:
1601 gcc_assert (c_dialect_objc ());
1602 c_parser_objc_alias_declaration (parser);
1603 break;
1604 case RID_AT_PROTOCOL:
1605 gcc_assert (c_dialect_objc ());
1606 c_parser_objc_protocol_definition (parser, NULL_TREE);
1607 break;
1608 case RID_AT_PROPERTY:
1609 gcc_assert (c_dialect_objc ());
1610 c_parser_objc_at_property_declaration (parser);
1611 break;
1612 case RID_AT_SYNTHESIZE:
1613 gcc_assert (c_dialect_objc ());
1614 c_parser_objc_at_synthesize_declaration (parser);
1615 break;
1616 case RID_AT_DYNAMIC:
1617 gcc_assert (c_dialect_objc ());
1618 c_parser_objc_at_dynamic_declaration (parser);
1619 break;
1620 case RID_AT_END:
1621 gcc_assert (c_dialect_objc ());
1622 c_parser_consume_token (parser);
1623 objc_finish_implementation ();
1624 break;
1625 default:
1626 goto decl_or_fndef;
1628 break;
1629 case CPP_SEMICOLON:
1630 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1631 "ISO C does not allow extra %<;%> outside of a function");
1632 c_parser_consume_token (parser);
1633 break;
1634 case CPP_PRAGMA:
1635 mark_valid_location_for_stdc_pragma (true);
1636 c_parser_pragma (parser, pragma_external, NULL);
1637 mark_valid_location_for_stdc_pragma (false);
1638 break;
1639 case CPP_PLUS:
1640 case CPP_MINUS:
1641 if (c_dialect_objc ())
1643 c_parser_objc_method_definition (parser);
1644 break;
1646 /* Else fall through, and yield a syntax error trying to parse
1647 as a declaration or function definition. */
1648 /* FALLTHRU */
1649 default:
1650 decl_or_fndef:
1651 /* A declaration or a function definition (or, in Objective-C,
1652 an @interface or @protocol with prefix attributes). We can
1653 only tell which after parsing the declaration specifiers, if
1654 any, and the first declarator. */
1655 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1656 NULL, vNULL);
1657 break;
1661 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1662 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1664 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1665 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1666 is accepted; otherwise (old-style parameter declarations) only other
1667 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1668 assertion is accepted; otherwise (old-style parameter declarations)
1669 it is not. If NESTED is true, we are inside a function or parsing
1670 old-style parameter declarations; any functions encountered are
1671 nested functions and declaration specifiers are required; otherwise
1672 we are at top level and functions are normal functions and
1673 declaration specifiers may be optional. If EMPTY_OK is true, empty
1674 declarations are OK (subject to all other constraints); otherwise
1675 (old-style parameter declarations) they are diagnosed. If
1676 START_ATTR_OK is true, the declaration specifiers may start with
1677 attributes; otherwise they may not.
1678 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1679 declaration when parsing an Objective-C foreach statement.
1680 FALLTHRU_ATTR_P is used to signal whether this function parsed
1681 "__attribute__((fallthrough));".
1683 declaration:
1684 declaration-specifiers init-declarator-list[opt] ;
1685 static_assert-declaration
1687 function-definition:
1688 declaration-specifiers[opt] declarator declaration-list[opt]
1689 compound-statement
1691 declaration-list:
1692 declaration
1693 declaration-list declaration
1695 init-declarator-list:
1696 init-declarator
1697 init-declarator-list , init-declarator
1699 init-declarator:
1700 declarator simple-asm-expr[opt] attributes[opt]
1701 declarator simple-asm-expr[opt] attributes[opt] = initializer
1703 GNU extensions:
1705 nested-function-definition:
1706 declaration-specifiers declarator declaration-list[opt]
1707 compound-statement
1709 attribute ;
1711 Objective-C:
1712 attributes objc-class-definition
1713 attributes objc-category-definition
1714 attributes objc-protocol-definition
1716 The simple-asm-expr and attributes are GNU extensions.
1718 This function does not handle __extension__; that is handled in its
1719 callers. ??? Following the old parser, __extension__ may start
1720 external declarations, declarations in functions and declarations
1721 at the start of "for" loops, but not old-style parameter
1722 declarations.
1724 C99 requires declaration specifiers in a function definition; the
1725 absence is diagnosed through the diagnosis of implicit int. In GNU
1726 C we also allow but diagnose declarations without declaration
1727 specifiers, but only at top level (elsewhere they conflict with
1728 other syntax).
1730 In Objective-C, declarations of the looping variable in a foreach
1731 statement are exceptionally terminated by 'in' (for example, 'for
1732 (NSObject *object in array) { ... }').
1734 OpenMP:
1736 declaration:
1737 threadprivate-directive
1739 GIMPLE:
1741 gimple-function-definition:
1742 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1743 declaration-list[opt] compound-statement
1745 rtl-function-definition:
1746 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1747 declaration-list[opt] compound-statement */
1749 static void
1750 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1751 bool static_assert_ok, bool empty_ok,
1752 bool nested, bool start_attr_ok,
1753 tree *objc_foreach_object_declaration,
1754 vec<c_token> omp_declare_simd_clauses,
1755 struct oacc_routine_data *oacc_routine_data,
1756 bool *fallthru_attr_p)
1758 struct c_declspecs *specs;
1759 tree prefix_attrs;
1760 tree all_prefix_attrs;
1761 bool diagnosed_no_specs = false;
1762 location_t here = c_parser_peek_token (parser)->location;
1764 if (static_assert_ok
1765 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1767 c_parser_static_assert_declaration (parser);
1768 return;
1770 specs = build_null_declspecs ();
1772 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1773 if (c_parser_peek_token (parser)->type == CPP_NAME
1774 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1775 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1776 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1777 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1779 tree name = c_parser_peek_token (parser)->value;
1781 /* Issue a warning about NAME being an unknown type name, perhaps
1782 with some kind of hint.
1783 If the user forgot a "struct" etc, suggest inserting
1784 it. Otherwise, attempt to look for misspellings. */
1785 gcc_rich_location richloc (here);
1786 if (tag_exists_p (RECORD_TYPE, name))
1788 /* This is not C++ with its implicit typedef. */
1789 richloc.add_fixit_insert_before ("struct ");
1790 error_at (&richloc,
1791 "unknown type name %qE;"
1792 " use %<struct%> keyword to refer to the type",
1793 name);
1795 else if (tag_exists_p (UNION_TYPE, name))
1797 richloc.add_fixit_insert_before ("union ");
1798 error_at (&richloc,
1799 "unknown type name %qE;"
1800 " use %<union%> keyword to refer to the type",
1801 name);
1803 else if (tag_exists_p (ENUMERAL_TYPE, name))
1805 richloc.add_fixit_insert_before ("enum ");
1806 error_at (&richloc,
1807 "unknown type name %qE;"
1808 " use %<enum%> keyword to refer to the type",
1809 name);
1811 else
1813 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1814 here);
1815 if (hint)
1817 richloc.add_fixit_replace (hint.suggestion ());
1818 error_at (&richloc,
1819 "unknown type name %qE; did you mean %qs?",
1820 name, hint.suggestion ());
1822 else
1823 error_at (here, "unknown type name %qE", name);
1826 /* Parse declspecs normally to get a correct pointer type, but avoid
1827 a further "fails to be a type name" error. Refuse nested functions
1828 since it is not how the user likely wants us to recover. */
1829 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1830 c_parser_peek_token (parser)->keyword = RID_VOID;
1831 c_parser_peek_token (parser)->value = error_mark_node;
1832 fndef_ok = !nested;
1835 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1836 true, true, cla_nonabstract_decl);
1837 if (parser->error)
1839 c_parser_skip_to_end_of_block_or_statement (parser);
1840 return;
1842 if (nested && !specs->declspecs_seen_p)
1844 c_parser_error (parser, "expected declaration specifiers");
1845 c_parser_skip_to_end_of_block_or_statement (parser);
1846 return;
1849 finish_declspecs (specs);
1850 bool auto_type_p = specs->typespec_word == cts_auto_type;
1851 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1853 if (auto_type_p)
1854 error_at (here, "%<__auto_type%> in empty declaration");
1855 else if (specs->typespec_kind == ctsk_none
1856 && attribute_fallthrough_p (specs->attrs))
1858 if (fallthru_attr_p != NULL)
1859 *fallthru_attr_p = true;
1860 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1861 void_type_node, 0);
1862 add_stmt (fn);
1864 else if (empty_ok)
1865 shadow_tag (specs);
1866 else
1868 shadow_tag_warned (specs, 1);
1869 pedwarn (here, 0, "empty declaration");
1871 c_parser_consume_token (parser);
1872 if (oacc_routine_data)
1873 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1874 return;
1877 /* Provide better error recovery. Note that a type name here is usually
1878 better diagnosed as a redeclaration. */
1879 if (empty_ok
1880 && specs->typespec_kind == ctsk_tagdef
1881 && c_parser_next_token_starts_declspecs (parser)
1882 && !c_parser_next_token_is (parser, CPP_NAME))
1884 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1885 parser->error = false;
1886 shadow_tag_warned (specs, 1);
1887 return;
1889 else if (c_dialect_objc () && !auto_type_p)
1891 /* Prefix attributes are an error on method decls. */
1892 switch (c_parser_peek_token (parser)->type)
1894 case CPP_PLUS:
1895 case CPP_MINUS:
1896 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1897 return;
1898 if (specs->attrs)
1900 warning_at (c_parser_peek_token (parser)->location,
1901 OPT_Wattributes,
1902 "prefix attributes are ignored for methods");
1903 specs->attrs = NULL_TREE;
1905 if (fndef_ok)
1906 c_parser_objc_method_definition (parser);
1907 else
1908 c_parser_objc_methodproto (parser);
1909 return;
1910 break;
1911 default:
1912 break;
1914 /* This is where we parse 'attributes @interface ...',
1915 'attributes @implementation ...', 'attributes @protocol ...'
1916 (where attributes could be, for example, __attribute__
1917 ((deprecated)).
1919 switch (c_parser_peek_token (parser)->keyword)
1921 case RID_AT_INTERFACE:
1923 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1924 return;
1925 c_parser_objc_class_definition (parser, specs->attrs);
1926 return;
1928 break;
1929 case RID_AT_IMPLEMENTATION:
1931 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1932 return;
1933 if (specs->attrs)
1935 warning_at (c_parser_peek_token (parser)->location,
1936 OPT_Wattributes,
1937 "prefix attributes are ignored for implementations");
1938 specs->attrs = NULL_TREE;
1940 c_parser_objc_class_definition (parser, NULL_TREE);
1941 return;
1943 break;
1944 case RID_AT_PROTOCOL:
1946 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1947 return;
1948 c_parser_objc_protocol_definition (parser, specs->attrs);
1949 return;
1951 break;
1952 case RID_AT_ALIAS:
1953 case RID_AT_CLASS:
1954 case RID_AT_END:
1955 case RID_AT_PROPERTY:
1956 if (specs->attrs)
1958 c_parser_error (parser, "unexpected attribute");
1959 specs->attrs = NULL;
1961 break;
1962 default:
1963 break;
1966 else if (attribute_fallthrough_p (specs->attrs))
1967 warning_at (here, OPT_Wattributes,
1968 "%<fallthrough%> attribute not followed by %<;%>");
1970 pending_xref_error ();
1971 prefix_attrs = specs->attrs;
1972 all_prefix_attrs = prefix_attrs;
1973 specs->attrs = NULL_TREE;
1974 while (true)
1976 struct c_declarator *declarator;
1977 bool dummy = false;
1978 timevar_id_t tv;
1979 tree fnbody = NULL_TREE;
1980 /* Declaring either one or more declarators (in which case we
1981 should diagnose if there were no declaration specifiers) or a
1982 function definition (in which case the diagnostic for
1983 implicit int suffices). */
1984 declarator = c_parser_declarator (parser,
1985 specs->typespec_kind != ctsk_none,
1986 C_DTR_NORMAL, &dummy);
1987 if (declarator == NULL)
1989 if (omp_declare_simd_clauses.exists ()
1990 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1991 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1992 omp_declare_simd_clauses);
1993 if (oacc_routine_data)
1994 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1995 c_parser_skip_to_end_of_block_or_statement (parser);
1996 return;
1998 if (auto_type_p && declarator->kind != cdk_id)
2000 error_at (here,
2001 "%<__auto_type%> requires a plain identifier"
2002 " as declarator");
2003 c_parser_skip_to_end_of_block_or_statement (parser);
2004 return;
2006 if (c_parser_next_token_is (parser, CPP_EQ)
2007 || c_parser_next_token_is (parser, CPP_COMMA)
2008 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2009 || c_parser_next_token_is_keyword (parser, RID_ASM)
2010 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2011 || c_parser_next_token_is_keyword (parser, RID_IN))
2013 tree asm_name = NULL_TREE;
2014 tree postfix_attrs = NULL_TREE;
2015 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2017 diagnosed_no_specs = true;
2018 pedwarn (here, 0, "data definition has no type or storage class");
2020 /* Having seen a data definition, there cannot now be a
2021 function definition. */
2022 fndef_ok = false;
2023 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2024 asm_name = c_parser_simple_asm_expr (parser);
2025 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2027 postfix_attrs = c_parser_attributes (parser);
2028 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2030 /* This means there is an attribute specifier after
2031 the declarator in a function definition. Provide
2032 some more information for the user. */
2033 error_at (here, "attributes should be specified before the "
2034 "declarator in a function definition");
2035 c_parser_skip_to_end_of_block_or_statement (parser);
2036 return;
2039 if (c_parser_next_token_is (parser, CPP_EQ))
2041 tree d;
2042 struct c_expr init;
2043 location_t init_loc;
2044 c_parser_consume_token (parser);
2045 if (auto_type_p)
2047 init_loc = c_parser_peek_token (parser)->location;
2048 rich_location richloc (line_table, init_loc);
2049 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2050 /* A parameter is initialized, which is invalid. Don't
2051 attempt to instrument the initializer. */
2052 int flag_sanitize_save = flag_sanitize;
2053 if (nested && !empty_ok)
2054 flag_sanitize = 0;
2055 init = c_parser_expr_no_commas (parser, NULL);
2056 flag_sanitize = flag_sanitize_save;
2057 if (TREE_CODE (init.value) == COMPONENT_REF
2058 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2059 error_at (here,
2060 "%<__auto_type%> used with a bit-field"
2061 " initializer");
2062 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2063 tree init_type = TREE_TYPE (init.value);
2064 /* As with typeof, remove all qualifiers from atomic types. */
2065 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2066 init_type
2067 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2068 bool vm_type = variably_modified_type_p (init_type,
2069 NULL_TREE);
2070 if (vm_type)
2071 init.value = save_expr (init.value);
2072 finish_init ();
2073 specs->typespec_kind = ctsk_typeof;
2074 specs->locations[cdw_typedef] = init_loc;
2075 specs->typedef_p = true;
2076 specs->type = init_type;
2077 if (vm_type)
2079 bool maybe_const = true;
2080 tree type_expr = c_fully_fold (init.value, false,
2081 &maybe_const);
2082 specs->expr_const_operands &= maybe_const;
2083 if (specs->expr)
2084 specs->expr = build2 (COMPOUND_EXPR,
2085 TREE_TYPE (type_expr),
2086 specs->expr, type_expr);
2087 else
2088 specs->expr = type_expr;
2090 d = start_decl (declarator, specs, true,
2091 chainon (postfix_attrs, all_prefix_attrs));
2092 if (!d)
2093 d = error_mark_node;
2094 if (omp_declare_simd_clauses.exists ()
2095 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2096 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2097 omp_declare_simd_clauses);
2099 else
2101 /* The declaration of the variable is in effect while
2102 its initializer is parsed. */
2103 d = start_decl (declarator, specs, true,
2104 chainon (postfix_attrs, all_prefix_attrs));
2105 if (!d)
2106 d = error_mark_node;
2107 if (omp_declare_simd_clauses.exists ()
2108 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2109 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2110 omp_declare_simd_clauses);
2111 init_loc = c_parser_peek_token (parser)->location;
2112 rich_location richloc (line_table, init_loc);
2113 start_init (d, asm_name, global_bindings_p (), &richloc);
2114 /* A parameter is initialized, which is invalid. Don't
2115 attempt to instrument the initializer. */
2116 int flag_sanitize_save = flag_sanitize;
2117 if (TREE_CODE (d) == PARM_DECL)
2118 flag_sanitize = 0;
2119 init = c_parser_initializer (parser);
2120 flag_sanitize = flag_sanitize_save;
2121 finish_init ();
2123 if (oacc_routine_data)
2124 c_finish_oacc_routine (oacc_routine_data, d, false);
2125 if (d != error_mark_node)
2127 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2128 finish_decl (d, init_loc, init.value,
2129 init.original_type, asm_name);
2132 else
2134 if (auto_type_p)
2136 error_at (here,
2137 "%<__auto_type%> requires an initialized "
2138 "data declaration");
2139 c_parser_skip_to_end_of_block_or_statement (parser);
2140 return;
2142 tree d = start_decl (declarator, specs, false,
2143 chainon (postfix_attrs,
2144 all_prefix_attrs));
2145 if (d && TREE_CODE (d) == FUNCTION_DECL)
2146 if (declarator->kind == cdk_function)
2147 if (DECL_ARGUMENTS (d) == NULL_TREE)
2148 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2149 if (omp_declare_simd_clauses.exists ()
2150 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2152 tree parms = NULL_TREE;
2153 if (d && TREE_CODE (d) == FUNCTION_DECL)
2155 struct c_declarator *ce = declarator;
2156 while (ce != NULL)
2157 if (ce->kind == cdk_function)
2159 parms = ce->u.arg_info->parms;
2160 break;
2162 else
2163 ce = ce->declarator;
2165 if (parms)
2166 temp_store_parm_decls (d, parms);
2167 c_finish_omp_declare_simd (parser, d, parms,
2168 omp_declare_simd_clauses);
2169 if (parms)
2170 temp_pop_parm_decls ();
2172 if (oacc_routine_data)
2173 c_finish_oacc_routine (oacc_routine_data, d, false);
2174 if (d)
2175 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2176 NULL_TREE, asm_name);
2178 if (c_parser_next_token_is_keyword (parser, RID_IN))
2180 if (d)
2181 *objc_foreach_object_declaration = d;
2182 else
2183 *objc_foreach_object_declaration = error_mark_node;
2186 if (c_parser_next_token_is (parser, CPP_COMMA))
2188 if (auto_type_p)
2190 error_at (here,
2191 "%<__auto_type%> may only be used with"
2192 " a single declarator");
2193 c_parser_skip_to_end_of_block_or_statement (parser);
2194 return;
2196 c_parser_consume_token (parser);
2197 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2198 all_prefix_attrs = chainon (c_parser_attributes (parser),
2199 prefix_attrs);
2200 else
2201 all_prefix_attrs = prefix_attrs;
2202 continue;
2204 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2206 c_parser_consume_token (parser);
2207 return;
2209 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2211 /* This can only happen in Objective-C: we found the
2212 'in' that terminates the declaration inside an
2213 Objective-C foreach statement. Do not consume the
2214 token, so that the caller can use it to determine
2215 that this indeed is a foreach context. */
2216 return;
2218 else
2220 c_parser_error (parser, "expected %<,%> or %<;%>");
2221 c_parser_skip_to_end_of_block_or_statement (parser);
2222 return;
2225 else if (auto_type_p)
2227 error_at (here,
2228 "%<__auto_type%> requires an initialized data declaration");
2229 c_parser_skip_to_end_of_block_or_statement (parser);
2230 return;
2232 else if (!fndef_ok)
2234 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2235 "%<asm%> or %<__attribute__%>");
2236 c_parser_skip_to_end_of_block_or_statement (parser);
2237 return;
2239 /* Function definition (nested or otherwise). */
2240 if (nested)
2242 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2243 c_push_function_context ();
2245 if (!start_function (specs, declarator, all_prefix_attrs))
2247 /* At this point we've consumed:
2248 declaration-specifiers declarator
2249 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2250 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2251 but the
2252 declaration-specifiers declarator
2253 aren't grokkable as a function definition, so we have
2254 an error. */
2255 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2256 if (c_parser_next_token_starts_declspecs (parser))
2258 /* If we have
2259 declaration-specifiers declarator decl-specs
2260 then assume we have a missing semicolon, which would
2261 give us:
2262 declaration-specifiers declarator decl-specs
2265 <~~~~~~~~~ declaration ~~~~~~~~~~>
2266 Use c_parser_require to get an error with a fix-it hint. */
2267 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2268 parser->error = false;
2270 else
2272 /* This can appear in many cases looking nothing like a
2273 function definition, so we don't give a more specific
2274 error suggesting there was one. */
2275 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2276 "or %<__attribute__%>");
2278 if (nested)
2279 c_pop_function_context ();
2280 break;
2283 if (DECL_DECLARED_INLINE_P (current_function_decl))
2284 tv = TV_PARSE_INLINE;
2285 else
2286 tv = TV_PARSE_FUNC;
2287 auto_timevar at (g_timer, tv);
2289 /* Parse old-style parameter declarations. ??? Attributes are
2290 not allowed to start declaration specifiers here because of a
2291 syntax conflict between a function declaration with attribute
2292 suffix and a function definition with an attribute prefix on
2293 first old-style parameter declaration. Following the old
2294 parser, they are not accepted on subsequent old-style
2295 parameter declarations either. However, there is no
2296 ambiguity after the first declaration, nor indeed on the
2297 first as long as we don't allow postfix attributes after a
2298 declarator with a nonempty identifier list in a definition;
2299 and postfix attributes have never been accepted here in
2300 function definitions either. */
2301 while (c_parser_next_token_is_not (parser, CPP_EOF)
2302 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2303 c_parser_declaration_or_fndef (parser, false, false, false,
2304 true, false, NULL, vNULL);
2305 store_parm_decls ();
2306 if (omp_declare_simd_clauses.exists ()
2307 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2308 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2309 omp_declare_simd_clauses);
2310 if (oacc_routine_data)
2311 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2312 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2313 = c_parser_peek_token (parser)->location;
2315 /* If the definition was marked with __GIMPLE then parse the
2316 function body as GIMPLE. */
2317 if (specs->gimple_p)
2319 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2320 bool saved = in_late_binary_op;
2321 in_late_binary_op = true;
2322 c_parser_parse_gimple_body (parser);
2323 in_late_binary_op = saved;
2325 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2326 consuming the function body. */
2327 else if (specs->rtl_p)
2329 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2331 /* Normally, store_parm_decls sets next_is_function_body,
2332 anticipating a function body. We need a push_scope/pop_scope
2333 pair to flush out this state, or subsequent function parsing
2334 will go wrong. */
2335 push_scope ();
2336 pop_scope ();
2338 finish_function ();
2339 return;
2341 else
2343 fnbody = c_parser_compound_statement (parser);
2344 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2345 fnbody = expand_array_notation_exprs (fnbody);
2347 tree fndecl = current_function_decl;
2348 if (nested)
2350 tree decl = current_function_decl;
2351 /* Mark nested functions as needing static-chain initially.
2352 lower_nested_functions will recompute it but the
2353 DECL_STATIC_CHAIN flag is also used before that happens,
2354 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2355 DECL_STATIC_CHAIN (decl) = 1;
2356 add_stmt (fnbody);
2357 finish_function ();
2358 c_pop_function_context ();
2359 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2361 else
2363 if (fnbody)
2364 add_stmt (fnbody);
2365 finish_function ();
2367 /* Get rid of the empty stmt list for GIMPLE. */
2368 if (specs->gimple_p)
2369 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2371 break;
2375 /* Parse an asm-definition (asm() outside a function body). This is a
2376 GNU extension.
2378 asm-definition:
2379 simple-asm-expr ;
2382 static void
2383 c_parser_asm_definition (c_parser *parser)
2385 tree asm_str = c_parser_simple_asm_expr (parser);
2386 if (asm_str)
2387 symtab->finalize_toplevel_asm (asm_str);
2388 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2391 /* Parse a static assertion (C11 6.7.10).
2393 static_assert-declaration:
2394 static_assert-declaration-no-semi ;
2397 static void
2398 c_parser_static_assert_declaration (c_parser *parser)
2400 c_parser_static_assert_declaration_no_semi (parser);
2401 if (parser->error
2402 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2403 c_parser_skip_to_end_of_block_or_statement (parser);
2406 /* Parse a static assertion (C11 6.7.10), without the trailing
2407 semicolon.
2409 static_assert-declaration-no-semi:
2410 _Static_assert ( constant-expression , string-literal )
2413 static void
2414 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2416 location_t assert_loc, value_loc;
2417 tree value;
2418 tree string;
2420 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2421 assert_loc = c_parser_peek_token (parser)->location;
2422 if (flag_isoc99)
2423 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2424 "ISO C99 does not support %<_Static_assert%>");
2425 else
2426 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2427 "ISO C90 does not support %<_Static_assert%>");
2428 c_parser_consume_token (parser);
2429 matching_parens parens;
2430 if (!parens.require_open (parser))
2431 return;
2432 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2433 value = c_parser_expr_no_commas (parser, NULL).value;
2434 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2435 parser->lex_untranslated_string = true;
2436 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2438 parser->lex_untranslated_string = false;
2439 return;
2441 switch (c_parser_peek_token (parser)->type)
2443 case CPP_STRING:
2444 case CPP_STRING16:
2445 case CPP_STRING32:
2446 case CPP_WSTRING:
2447 case CPP_UTF8STRING:
2448 string = c_parser_peek_token (parser)->value;
2449 c_parser_consume_token (parser);
2450 parser->lex_untranslated_string = false;
2451 break;
2452 default:
2453 c_parser_error (parser, "expected string literal");
2454 parser->lex_untranslated_string = false;
2455 return;
2457 parens.require_close (parser);
2459 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2461 error_at (value_loc, "expression in static assertion is not an integer");
2462 return;
2464 if (TREE_CODE (value) != INTEGER_CST)
2466 value = c_fully_fold (value, false, NULL);
2467 /* Strip no-op conversions. */
2468 STRIP_TYPE_NOPS (value);
2469 if (TREE_CODE (value) == INTEGER_CST)
2470 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2471 "is not an integer constant expression");
2473 if (TREE_CODE (value) != INTEGER_CST)
2475 error_at (value_loc, "expression in static assertion is not constant");
2476 return;
2478 constant_expression_warning (value);
2479 if (integer_zerop (value))
2480 error_at (assert_loc, "static assertion failed: %E", string);
2483 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2484 6.7, C11 6.7), adding them to SPECS (which may already include some).
2485 Storage class specifiers are accepted iff SCSPEC_OK; type
2486 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2487 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2488 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2490 declaration-specifiers:
2491 storage-class-specifier declaration-specifiers[opt]
2492 type-specifier declaration-specifiers[opt]
2493 type-qualifier declaration-specifiers[opt]
2494 function-specifier declaration-specifiers[opt]
2495 alignment-specifier declaration-specifiers[opt]
2497 Function specifiers (inline) are from C99, and are currently
2498 handled as storage class specifiers, as is __thread. Alignment
2499 specifiers are from C11.
2501 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2502 storage-class-specifier:
2503 typedef
2504 extern
2505 static
2506 auto
2507 register
2508 _Thread_local
2510 (_Thread_local is new in C11.)
2512 C99 6.7.4, C11 6.7.4:
2513 function-specifier:
2514 inline
2515 _Noreturn
2517 (_Noreturn is new in C11.)
2519 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2520 type-specifier:
2521 void
2522 char
2523 short
2525 long
2526 float
2527 double
2528 signed
2529 unsigned
2530 _Bool
2531 _Complex
2532 [_Imaginary removed in C99 TC2]
2533 struct-or-union-specifier
2534 enum-specifier
2535 typedef-name
2536 atomic-type-specifier
2538 (_Bool and _Complex are new in C99.)
2539 (atomic-type-specifier is new in C11.)
2541 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2543 type-qualifier:
2544 const
2545 restrict
2546 volatile
2547 address-space-qualifier
2548 _Atomic
2550 (restrict is new in C99.)
2551 (_Atomic is new in C11.)
2553 GNU extensions:
2555 declaration-specifiers:
2556 attributes declaration-specifiers[opt]
2558 type-qualifier:
2559 address-space
2561 address-space:
2562 identifier recognized by the target
2564 storage-class-specifier:
2565 __thread
2567 type-specifier:
2568 typeof-specifier
2569 __auto_type
2570 __intN
2571 _Decimal32
2572 _Decimal64
2573 _Decimal128
2574 _Fract
2575 _Accum
2576 _Sat
2578 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2579 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2581 atomic-type-specifier
2582 _Atomic ( type-name )
2584 Objective-C:
2586 type-specifier:
2587 class-name objc-protocol-refs[opt]
2588 typedef-name objc-protocol-refs
2589 objc-protocol-refs
2592 void
2593 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2594 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2595 bool alignspec_ok, bool auto_type_ok,
2596 enum c_lookahead_kind la)
2598 bool attrs_ok = start_attr_ok;
2599 bool seen_type = specs->typespec_kind != ctsk_none;
2601 if (!typespec_ok)
2602 gcc_assert (la == cla_prefer_id);
2604 while (c_parser_next_token_is (parser, CPP_NAME)
2605 || c_parser_next_token_is (parser, CPP_KEYWORD)
2606 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2608 struct c_typespec t;
2609 tree attrs;
2610 tree align;
2611 location_t loc = c_parser_peek_token (parser)->location;
2613 /* If we cannot accept a type, exit if the next token must start
2614 one. Also, if we already have seen a tagged definition,
2615 a typename would be an error anyway and likely the user
2616 has simply forgotten a semicolon, so we exit. */
2617 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2618 && c_parser_next_tokens_start_typename (parser, la)
2619 && !c_parser_next_token_is_qualifier (parser))
2620 break;
2622 if (c_parser_next_token_is (parser, CPP_NAME))
2624 c_token *name_token = c_parser_peek_token (parser);
2625 tree value = name_token->value;
2626 c_id_kind kind = name_token->id_kind;
2628 if (kind == C_ID_ADDRSPACE)
2630 addr_space_t as
2631 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2632 declspecs_add_addrspace (name_token->location, specs, as);
2633 c_parser_consume_token (parser);
2634 attrs_ok = true;
2635 continue;
2638 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2640 /* If we cannot accept a type, and the next token must start one,
2641 exit. Do the same if we already have seen a tagged definition,
2642 since it would be an error anyway and likely the user has simply
2643 forgotten a semicolon. */
2644 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2645 break;
2647 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2648 a C_ID_CLASSNAME. */
2649 c_parser_consume_token (parser);
2650 seen_type = true;
2651 attrs_ok = true;
2652 if (kind == C_ID_ID)
2654 error_at (loc, "unknown type name %qE", value);
2655 t.kind = ctsk_typedef;
2656 t.spec = error_mark_node;
2658 else if (kind == C_ID_TYPENAME
2659 && (!c_dialect_objc ()
2660 || c_parser_next_token_is_not (parser, CPP_LESS)))
2662 t.kind = ctsk_typedef;
2663 /* For a typedef name, record the meaning, not the name.
2664 In case of 'foo foo, bar;'. */
2665 t.spec = lookup_name (value);
2667 else
2669 tree proto = NULL_TREE;
2670 gcc_assert (c_dialect_objc ());
2671 t.kind = ctsk_objc;
2672 if (c_parser_next_token_is (parser, CPP_LESS))
2673 proto = c_parser_objc_protocol_refs (parser);
2674 t.spec = objc_get_protocol_qualified_type (value, proto);
2676 t.expr = NULL_TREE;
2677 t.expr_const_operands = true;
2678 declspecs_add_type (name_token->location, specs, t);
2679 continue;
2681 if (c_parser_next_token_is (parser, CPP_LESS))
2683 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2684 nisse@lysator.liu.se. */
2685 tree proto;
2686 gcc_assert (c_dialect_objc ());
2687 if (!typespec_ok || seen_type)
2688 break;
2689 proto = c_parser_objc_protocol_refs (parser);
2690 t.kind = ctsk_objc;
2691 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2692 t.expr = NULL_TREE;
2693 t.expr_const_operands = true;
2694 declspecs_add_type (loc, specs, t);
2695 continue;
2697 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2698 switch (c_parser_peek_token (parser)->keyword)
2700 case RID_STATIC:
2701 case RID_EXTERN:
2702 case RID_REGISTER:
2703 case RID_TYPEDEF:
2704 case RID_INLINE:
2705 case RID_NORETURN:
2706 case RID_AUTO:
2707 case RID_THREAD:
2708 if (!scspec_ok)
2709 goto out;
2710 attrs_ok = true;
2711 /* TODO: Distinguish between function specifiers (inline, noreturn)
2712 and storage class specifiers, either here or in
2713 declspecs_add_scspec. */
2714 declspecs_add_scspec (loc, specs,
2715 c_parser_peek_token (parser)->value);
2716 c_parser_consume_token (parser);
2717 break;
2718 case RID_AUTO_TYPE:
2719 if (!auto_type_ok)
2720 goto out;
2721 /* Fall through. */
2722 case RID_UNSIGNED:
2723 case RID_LONG:
2724 case RID_SHORT:
2725 case RID_SIGNED:
2726 case RID_COMPLEX:
2727 case RID_INT:
2728 case RID_CHAR:
2729 case RID_FLOAT:
2730 case RID_DOUBLE:
2731 case RID_VOID:
2732 case RID_DFLOAT32:
2733 case RID_DFLOAT64:
2734 case RID_DFLOAT128:
2735 CASE_RID_FLOATN_NX:
2736 case RID_BOOL:
2737 case RID_FRACT:
2738 case RID_ACCUM:
2739 case RID_SAT:
2740 case RID_INT_N_0:
2741 case RID_INT_N_1:
2742 case RID_INT_N_2:
2743 case RID_INT_N_3:
2744 if (!typespec_ok)
2745 goto out;
2746 attrs_ok = true;
2747 seen_type = true;
2748 if (c_dialect_objc ())
2749 parser->objc_need_raw_identifier = true;
2750 t.kind = ctsk_resword;
2751 t.spec = c_parser_peek_token (parser)->value;
2752 t.expr = NULL_TREE;
2753 t.expr_const_operands = true;
2754 declspecs_add_type (loc, specs, t);
2755 c_parser_consume_token (parser);
2756 break;
2757 case RID_ENUM:
2758 if (!typespec_ok)
2759 goto out;
2760 attrs_ok = true;
2761 seen_type = true;
2762 t = c_parser_enum_specifier (parser);
2763 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2764 declspecs_add_type (loc, specs, t);
2765 break;
2766 case RID_STRUCT:
2767 case RID_UNION:
2768 if (!typespec_ok)
2769 goto out;
2770 attrs_ok = true;
2771 seen_type = true;
2772 t = c_parser_struct_or_union_specifier (parser);
2773 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2774 declspecs_add_type (loc, specs, t);
2775 break;
2776 case RID_TYPEOF:
2777 /* ??? The old parser rejected typeof after other type
2778 specifiers, but is a syntax error the best way of
2779 handling this? */
2780 if (!typespec_ok || seen_type)
2781 goto out;
2782 attrs_ok = true;
2783 seen_type = true;
2784 t = c_parser_typeof_specifier (parser);
2785 declspecs_add_type (loc, specs, t);
2786 break;
2787 case RID_ATOMIC:
2788 /* C parser handling of Objective-C constructs needs
2789 checking for correct lvalue-to-rvalue conversions, and
2790 the code in build_modify_expr handling various
2791 Objective-C cases, and that in build_unary_op handling
2792 Objective-C cases for increment / decrement, also needs
2793 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2794 and objc_types_are_equivalent may also need updates. */
2795 if (c_dialect_objc ())
2796 sorry ("%<_Atomic%> in Objective-C");
2797 if (flag_isoc99)
2798 pedwarn_c99 (loc, OPT_Wpedantic,
2799 "ISO C99 does not support the %<_Atomic%> qualifier");
2800 else
2801 pedwarn_c99 (loc, OPT_Wpedantic,
2802 "ISO C90 does not support the %<_Atomic%> qualifier");
2803 attrs_ok = true;
2804 tree value;
2805 value = c_parser_peek_token (parser)->value;
2806 c_parser_consume_token (parser);
2807 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2809 /* _Atomic ( type-name ). */
2810 seen_type = true;
2811 c_parser_consume_token (parser);
2812 struct c_type_name *type = c_parser_type_name (parser);
2813 t.kind = ctsk_typeof;
2814 t.spec = error_mark_node;
2815 t.expr = NULL_TREE;
2816 t.expr_const_operands = true;
2817 if (type != NULL)
2818 t.spec = groktypename (type, &t.expr,
2819 &t.expr_const_operands);
2820 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2821 "expected %<)%>");
2822 if (t.spec != error_mark_node)
2824 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2825 error_at (loc, "%<_Atomic%>-qualified array type");
2826 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2827 error_at (loc, "%<_Atomic%>-qualified function type");
2828 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2829 error_at (loc, "%<_Atomic%> applied to a qualified type");
2830 else
2831 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2833 declspecs_add_type (loc, specs, t);
2835 else
2836 declspecs_add_qual (loc, specs, value);
2837 break;
2838 case RID_CONST:
2839 case RID_VOLATILE:
2840 case RID_RESTRICT:
2841 attrs_ok = true;
2842 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2843 c_parser_consume_token (parser);
2844 break;
2845 case RID_ATTRIBUTE:
2846 if (!attrs_ok)
2847 goto out;
2848 attrs = c_parser_attributes (parser);
2849 declspecs_add_attrs (loc, specs, attrs);
2850 break;
2851 case RID_ALIGNAS:
2852 if (!alignspec_ok)
2853 goto out;
2854 align = c_parser_alignas_specifier (parser);
2855 declspecs_add_alignas (loc, specs, align);
2856 break;
2857 case RID_GIMPLE:
2858 if (! flag_gimple)
2859 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2860 c_parser_consume_token (parser);
2861 specs->gimple_p = true;
2862 specs->locations[cdw_gimple] = loc;
2863 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2864 break;
2865 case RID_RTL:
2866 c_parser_consume_token (parser);
2867 specs->rtl_p = true;
2868 specs->locations[cdw_rtl] = loc;
2869 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2870 break;
2871 default:
2872 goto out;
2875 out: ;
2878 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2880 enum-specifier:
2881 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2882 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2883 enum attributes[opt] identifier
2885 The form with trailing comma is new in C99. The forms with
2886 attributes are GNU extensions. In GNU C, we accept any expression
2887 without commas in the syntax (assignment expressions, not just
2888 conditional expressions); assignment expressions will be diagnosed
2889 as non-constant.
2891 enumerator-list:
2892 enumerator
2893 enumerator-list , enumerator
2895 enumerator:
2896 enumeration-constant
2897 enumeration-constant = constant-expression
2899 GNU Extensions:
2901 enumerator:
2902 enumeration-constant attributes[opt]
2903 enumeration-constant attributes[opt] = constant-expression
2907 static struct c_typespec
2908 c_parser_enum_specifier (c_parser *parser)
2910 struct c_typespec ret;
2911 tree attrs;
2912 tree ident = NULL_TREE;
2913 location_t enum_loc;
2914 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2915 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2916 c_parser_consume_token (parser);
2917 attrs = c_parser_attributes (parser);
2918 enum_loc = c_parser_peek_token (parser)->location;
2919 /* Set the location in case we create a decl now. */
2920 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2921 if (c_parser_next_token_is (parser, CPP_NAME))
2923 ident = c_parser_peek_token (parser)->value;
2924 ident_loc = c_parser_peek_token (parser)->location;
2925 enum_loc = ident_loc;
2926 c_parser_consume_token (parser);
2928 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2930 /* Parse an enum definition. */
2931 struct c_enum_contents the_enum;
2932 tree type;
2933 tree postfix_attrs;
2934 /* We chain the enumerators in reverse order, then put them in
2935 forward order at the end. */
2936 tree values;
2937 timevar_push (TV_PARSE_ENUM);
2938 type = start_enum (enum_loc, &the_enum, ident);
2939 values = NULL_TREE;
2940 c_parser_consume_token (parser);
2941 while (true)
2943 tree enum_id;
2944 tree enum_value;
2945 tree enum_decl;
2946 bool seen_comma;
2947 c_token *token;
2948 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2949 location_t decl_loc, value_loc;
2950 if (c_parser_next_token_is_not (parser, CPP_NAME))
2952 /* Give a nicer error for "enum {}". */
2953 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2954 && !parser->error)
2956 error_at (c_parser_peek_token (parser)->location,
2957 "empty enum is invalid");
2958 parser->error = true;
2960 else
2961 c_parser_error (parser, "expected identifier");
2962 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2963 values = error_mark_node;
2964 break;
2966 token = c_parser_peek_token (parser);
2967 enum_id = token->value;
2968 /* Set the location in case we create a decl now. */
2969 c_parser_set_source_position_from_token (token);
2970 decl_loc = value_loc = token->location;
2971 c_parser_consume_token (parser);
2972 /* Parse any specified attributes. */
2973 tree enum_attrs = c_parser_attributes (parser);
2974 if (c_parser_next_token_is (parser, CPP_EQ))
2976 c_parser_consume_token (parser);
2977 value_loc = c_parser_peek_token (parser)->location;
2978 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2980 else
2981 enum_value = NULL_TREE;
2982 enum_decl = build_enumerator (decl_loc, value_loc,
2983 &the_enum, enum_id, enum_value);
2984 if (enum_attrs)
2985 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2986 TREE_CHAIN (enum_decl) = values;
2987 values = enum_decl;
2988 seen_comma = false;
2989 if (c_parser_next_token_is (parser, CPP_COMMA))
2991 comma_loc = c_parser_peek_token (parser)->location;
2992 seen_comma = true;
2993 c_parser_consume_token (parser);
2995 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2997 if (seen_comma)
2998 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2999 "comma at end of enumerator list");
3000 c_parser_consume_token (parser);
3001 break;
3003 if (!seen_comma)
3005 c_parser_error (parser, "expected %<,%> or %<}%>");
3006 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3007 values = error_mark_node;
3008 break;
3011 postfix_attrs = c_parser_attributes (parser);
3012 ret.spec = finish_enum (type, nreverse (values),
3013 chainon (attrs, postfix_attrs));
3014 ret.kind = ctsk_tagdef;
3015 ret.expr = NULL_TREE;
3016 ret.expr_const_operands = true;
3017 timevar_pop (TV_PARSE_ENUM);
3018 return ret;
3020 else if (!ident)
3022 c_parser_error (parser, "expected %<{%>");
3023 ret.spec = error_mark_node;
3024 ret.kind = ctsk_tagref;
3025 ret.expr = NULL_TREE;
3026 ret.expr_const_operands = true;
3027 return ret;
3029 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3030 /* In ISO C, enumerated types can be referred to only if already
3031 defined. */
3032 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3034 gcc_assert (ident);
3035 pedwarn (enum_loc, OPT_Wpedantic,
3036 "ISO C forbids forward references to %<enum%> types");
3038 return ret;
3041 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3043 struct-or-union-specifier:
3044 struct-or-union attributes[opt] identifier[opt]
3045 { struct-contents } attributes[opt]
3046 struct-or-union attributes[opt] identifier
3048 struct-contents:
3049 struct-declaration-list
3051 struct-declaration-list:
3052 struct-declaration ;
3053 struct-declaration-list struct-declaration ;
3055 GNU extensions:
3057 struct-contents:
3058 empty
3059 struct-declaration
3060 struct-declaration-list struct-declaration
3062 struct-declaration-list:
3063 struct-declaration-list ;
3066 (Note that in the syntax here, unlike that in ISO C, the semicolons
3067 are included here rather than in struct-declaration, in order to
3068 describe the syntax with extra semicolons and missing semicolon at
3069 end.)
3071 Objective-C:
3073 struct-declaration-list:
3074 @defs ( class-name )
3076 (Note this does not include a trailing semicolon, but can be
3077 followed by further declarations, and gets a pedwarn-if-pedantic
3078 when followed by a semicolon.) */
3080 static struct c_typespec
3081 c_parser_struct_or_union_specifier (c_parser *parser)
3083 struct c_typespec ret;
3084 tree attrs;
3085 tree ident = NULL_TREE;
3086 location_t struct_loc;
3087 location_t ident_loc = UNKNOWN_LOCATION;
3088 enum tree_code code;
3089 switch (c_parser_peek_token (parser)->keyword)
3091 case RID_STRUCT:
3092 code = RECORD_TYPE;
3093 break;
3094 case RID_UNION:
3095 code = UNION_TYPE;
3096 break;
3097 default:
3098 gcc_unreachable ();
3100 struct_loc = c_parser_peek_token (parser)->location;
3101 c_parser_consume_token (parser);
3102 attrs = c_parser_attributes (parser);
3104 /* Set the location in case we create a decl now. */
3105 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3107 if (c_parser_next_token_is (parser, CPP_NAME))
3109 ident = c_parser_peek_token (parser)->value;
3110 ident_loc = c_parser_peek_token (parser)->location;
3111 struct_loc = ident_loc;
3112 c_parser_consume_token (parser);
3114 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3116 /* Parse a struct or union definition. Start the scope of the
3117 tag before parsing components. */
3118 struct c_struct_parse_info *struct_info;
3119 tree type = start_struct (struct_loc, code, ident, &struct_info);
3120 tree postfix_attrs;
3121 /* We chain the components in reverse order, then put them in
3122 forward order at the end. Each struct-declaration may
3123 declare multiple components (comma-separated), so we must use
3124 chainon to join them, although when parsing each
3125 struct-declaration we can use TREE_CHAIN directly.
3127 The theory behind all this is that there will be more
3128 semicolon separated fields than comma separated fields, and
3129 so we'll be minimizing the number of node traversals required
3130 by chainon. */
3131 tree contents;
3132 timevar_push (TV_PARSE_STRUCT);
3133 contents = NULL_TREE;
3134 c_parser_consume_token (parser);
3135 /* Handle the Objective-C @defs construct,
3136 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3137 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3139 tree name;
3140 gcc_assert (c_dialect_objc ());
3141 c_parser_consume_token (parser);
3142 matching_parens parens;
3143 if (!parens.require_open (parser))
3144 goto end_at_defs;
3145 if (c_parser_next_token_is (parser, CPP_NAME)
3146 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3148 name = c_parser_peek_token (parser)->value;
3149 c_parser_consume_token (parser);
3151 else
3153 c_parser_error (parser, "expected class name");
3154 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3155 goto end_at_defs;
3157 parens.skip_until_found_close (parser);
3158 contents = nreverse (objc_get_class_ivars (name));
3160 end_at_defs:
3161 /* Parse the struct-declarations and semicolons. Problems with
3162 semicolons are diagnosed here; empty structures are diagnosed
3163 elsewhere. */
3164 while (true)
3166 tree decls;
3167 /* Parse any stray semicolon. */
3168 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3170 location_t semicolon_loc
3171 = c_parser_peek_token (parser)->location;
3172 gcc_rich_location richloc (semicolon_loc);
3173 richloc.add_fixit_remove ();
3174 pedwarn (&richloc, OPT_Wpedantic,
3175 "extra semicolon in struct or union specified");
3176 c_parser_consume_token (parser);
3177 continue;
3179 /* Stop if at the end of the struct or union contents. */
3180 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3182 c_parser_consume_token (parser);
3183 break;
3185 /* Accept #pragmas at struct scope. */
3186 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3188 c_parser_pragma (parser, pragma_struct, NULL);
3189 continue;
3191 /* Parse some comma-separated declarations, but not the
3192 trailing semicolon if any. */
3193 decls = c_parser_struct_declaration (parser);
3194 contents = chainon (decls, contents);
3195 /* If no semicolon follows, either we have a parse error or
3196 are at the end of the struct or union and should
3197 pedwarn. */
3198 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3199 c_parser_consume_token (parser);
3200 else
3202 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3203 pedwarn (c_parser_peek_token (parser)->location, 0,
3204 "no semicolon at end of struct or union");
3205 else if (parser->error
3206 || !c_parser_next_token_starts_declspecs (parser))
3208 c_parser_error (parser, "expected %<;%>");
3209 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3210 break;
3213 /* If we come here, we have already emitted an error
3214 for an expected `;', identifier or `(', and we also
3215 recovered already. Go on with the next field. */
3218 postfix_attrs = c_parser_attributes (parser);
3219 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3220 chainon (attrs, postfix_attrs), struct_info);
3221 ret.kind = ctsk_tagdef;
3222 ret.expr = NULL_TREE;
3223 ret.expr_const_operands = true;
3224 timevar_pop (TV_PARSE_STRUCT);
3225 return ret;
3227 else if (!ident)
3229 c_parser_error (parser, "expected %<{%>");
3230 ret.spec = error_mark_node;
3231 ret.kind = ctsk_tagref;
3232 ret.expr = NULL_TREE;
3233 ret.expr_const_operands = true;
3234 return ret;
3236 ret = parser_xref_tag (ident_loc, code, ident);
3237 return ret;
3240 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3241 *without* the trailing semicolon.
3243 struct-declaration:
3244 specifier-qualifier-list struct-declarator-list
3245 static_assert-declaration-no-semi
3247 specifier-qualifier-list:
3248 type-specifier specifier-qualifier-list[opt]
3249 type-qualifier specifier-qualifier-list[opt]
3250 attributes specifier-qualifier-list[opt]
3252 struct-declarator-list:
3253 struct-declarator
3254 struct-declarator-list , attributes[opt] struct-declarator
3256 struct-declarator:
3257 declarator attributes[opt]
3258 declarator[opt] : constant-expression attributes[opt]
3260 GNU extensions:
3262 struct-declaration:
3263 __extension__ struct-declaration
3264 specifier-qualifier-list
3266 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3267 of attributes where shown is a GNU extension. In GNU C, we accept
3268 any expression without commas in the syntax (assignment
3269 expressions, not just conditional expressions); assignment
3270 expressions will be diagnosed as non-constant. */
3272 static tree
3273 c_parser_struct_declaration (c_parser *parser)
3275 struct c_declspecs *specs;
3276 tree prefix_attrs;
3277 tree all_prefix_attrs;
3278 tree decls;
3279 location_t decl_loc;
3280 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3282 int ext;
3283 tree decl;
3284 ext = disable_extension_diagnostics ();
3285 c_parser_consume_token (parser);
3286 decl = c_parser_struct_declaration (parser);
3287 restore_extension_diagnostics (ext);
3288 return decl;
3290 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3292 c_parser_static_assert_declaration_no_semi (parser);
3293 return NULL_TREE;
3295 specs = build_null_declspecs ();
3296 decl_loc = c_parser_peek_token (parser)->location;
3297 /* Strictly by the standard, we shouldn't allow _Alignas here,
3298 but it appears to have been intended to allow it there, so
3299 we're keeping it as it is until WG14 reaches a conclusion
3300 of N1731.
3301 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3302 c_parser_declspecs (parser, specs, false, true, true,
3303 true, false, cla_nonabstract_decl);
3304 if (parser->error)
3305 return NULL_TREE;
3306 if (!specs->declspecs_seen_p)
3308 c_parser_error (parser, "expected specifier-qualifier-list");
3309 return NULL_TREE;
3311 finish_declspecs (specs);
3312 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3313 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3315 tree ret;
3316 if (specs->typespec_kind == ctsk_none)
3318 pedwarn (decl_loc, OPT_Wpedantic,
3319 "ISO C forbids member declarations with no members");
3320 shadow_tag_warned (specs, pedantic);
3321 ret = NULL_TREE;
3323 else
3325 /* Support for unnamed structs or unions as members of
3326 structs or unions (which is [a] useful and [b] supports
3327 MS P-SDK). */
3328 tree attrs = NULL;
3330 ret = grokfield (c_parser_peek_token (parser)->location,
3331 build_id_declarator (NULL_TREE), specs,
3332 NULL_TREE, &attrs);
3333 if (ret)
3334 decl_attributes (&ret, attrs, 0);
3336 return ret;
3339 /* Provide better error recovery. Note that a type name here is valid,
3340 and will be treated as a field name. */
3341 if (specs->typespec_kind == ctsk_tagdef
3342 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3343 && c_parser_next_token_starts_declspecs (parser)
3344 && !c_parser_next_token_is (parser, CPP_NAME))
3346 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3347 parser->error = false;
3348 return NULL_TREE;
3351 pending_xref_error ();
3352 prefix_attrs = specs->attrs;
3353 all_prefix_attrs = prefix_attrs;
3354 specs->attrs = NULL_TREE;
3355 decls = NULL_TREE;
3356 while (true)
3358 /* Declaring one or more declarators or un-named bit-fields. */
3359 struct c_declarator *declarator;
3360 bool dummy = false;
3361 if (c_parser_next_token_is (parser, CPP_COLON))
3362 declarator = build_id_declarator (NULL_TREE);
3363 else
3364 declarator = c_parser_declarator (parser,
3365 specs->typespec_kind != ctsk_none,
3366 C_DTR_NORMAL, &dummy);
3367 if (declarator == NULL)
3369 c_parser_skip_to_end_of_block_or_statement (parser);
3370 break;
3372 if (c_parser_next_token_is (parser, CPP_COLON)
3373 || c_parser_next_token_is (parser, CPP_COMMA)
3374 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3375 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3376 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3378 tree postfix_attrs = NULL_TREE;
3379 tree width = NULL_TREE;
3380 tree d;
3381 if (c_parser_next_token_is (parser, CPP_COLON))
3383 c_parser_consume_token (parser);
3384 width = c_parser_expr_no_commas (parser, NULL).value;
3386 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3387 postfix_attrs = c_parser_attributes (parser);
3388 d = grokfield (c_parser_peek_token (parser)->location,
3389 declarator, specs, width, &all_prefix_attrs);
3390 decl_attributes (&d, chainon (postfix_attrs,
3391 all_prefix_attrs), 0);
3392 DECL_CHAIN (d) = decls;
3393 decls = d;
3394 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3395 all_prefix_attrs = chainon (c_parser_attributes (parser),
3396 prefix_attrs);
3397 else
3398 all_prefix_attrs = prefix_attrs;
3399 if (c_parser_next_token_is (parser, CPP_COMMA))
3400 c_parser_consume_token (parser);
3401 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3402 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3404 /* Semicolon consumed in caller. */
3405 break;
3407 else
3409 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3410 break;
3413 else
3415 c_parser_error (parser,
3416 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3417 "%<__attribute__%>");
3418 break;
3421 return decls;
3424 /* Parse a typeof specifier (a GNU extension).
3426 typeof-specifier:
3427 typeof ( expression )
3428 typeof ( type-name )
3431 static struct c_typespec
3432 c_parser_typeof_specifier (c_parser *parser)
3434 struct c_typespec ret;
3435 ret.kind = ctsk_typeof;
3436 ret.spec = error_mark_node;
3437 ret.expr = NULL_TREE;
3438 ret.expr_const_operands = true;
3439 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3440 c_parser_consume_token (parser);
3441 c_inhibit_evaluation_warnings++;
3442 in_typeof++;
3443 matching_parens parens;
3444 if (!parens.require_open (parser))
3446 c_inhibit_evaluation_warnings--;
3447 in_typeof--;
3448 return ret;
3450 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3452 struct c_type_name *type = c_parser_type_name (parser);
3453 c_inhibit_evaluation_warnings--;
3454 in_typeof--;
3455 if (type != NULL)
3457 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3458 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3461 else
3463 bool was_vm;
3464 location_t here = c_parser_peek_token (parser)->location;
3465 struct c_expr expr = c_parser_expression (parser);
3466 c_inhibit_evaluation_warnings--;
3467 in_typeof--;
3468 if (TREE_CODE (expr.value) == COMPONENT_REF
3469 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3470 error_at (here, "%<typeof%> applied to a bit-field");
3471 mark_exp_read (expr.value);
3472 ret.spec = TREE_TYPE (expr.value);
3473 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3474 /* This is returned with the type so that when the type is
3475 evaluated, this can be evaluated. */
3476 if (was_vm)
3477 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3478 pop_maybe_used (was_vm);
3479 /* For use in macros such as those in <stdatomic.h>, remove all
3480 qualifiers from atomic types. (const can be an issue for more macros
3481 using typeof than just the <stdatomic.h> ones.) */
3482 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3483 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3485 parens.skip_until_found_close (parser);
3486 return ret;
3489 /* Parse an alignment-specifier.
3491 C11 6.7.5:
3493 alignment-specifier:
3494 _Alignas ( type-name )
3495 _Alignas ( constant-expression )
3498 static tree
3499 c_parser_alignas_specifier (c_parser * parser)
3501 tree ret = error_mark_node;
3502 location_t loc = c_parser_peek_token (parser)->location;
3503 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3504 c_parser_consume_token (parser);
3505 if (flag_isoc99)
3506 pedwarn_c99 (loc, OPT_Wpedantic,
3507 "ISO C99 does not support %<_Alignas%>");
3508 else
3509 pedwarn_c99 (loc, OPT_Wpedantic,
3510 "ISO C90 does not support %<_Alignas%>");
3511 matching_parens parens;
3512 if (!parens.require_open (parser))
3513 return ret;
3514 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3516 struct c_type_name *type = c_parser_type_name (parser);
3517 if (type != NULL)
3518 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3519 false, true, 1);
3521 else
3522 ret = c_parser_expr_no_commas (parser, NULL).value;
3523 parens.skip_until_found_close (parser);
3524 return ret;
3527 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3528 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3529 a typedef name may be redeclared; otherwise it may not. KIND
3530 indicates which kind of declarator is wanted. Returns a valid
3531 declarator except in the case of a syntax error in which case NULL is
3532 returned. *SEEN_ID is set to true if an identifier being declared is
3533 seen; this is used to diagnose bad forms of abstract array declarators
3534 and to determine whether an identifier list is syntactically permitted.
3536 declarator:
3537 pointer[opt] direct-declarator
3539 direct-declarator:
3540 identifier
3541 ( attributes[opt] declarator )
3542 direct-declarator array-declarator
3543 direct-declarator ( parameter-type-list )
3544 direct-declarator ( identifier-list[opt] )
3546 pointer:
3547 * type-qualifier-list[opt]
3548 * type-qualifier-list[opt] pointer
3550 type-qualifier-list:
3551 type-qualifier
3552 attributes
3553 type-qualifier-list type-qualifier
3554 type-qualifier-list attributes
3556 array-declarator:
3557 [ type-qualifier-list[opt] assignment-expression[opt] ]
3558 [ static type-qualifier-list[opt] assignment-expression ]
3559 [ type-qualifier-list static assignment-expression ]
3560 [ type-qualifier-list[opt] * ]
3562 parameter-type-list:
3563 parameter-list
3564 parameter-list , ...
3566 parameter-list:
3567 parameter-declaration
3568 parameter-list , parameter-declaration
3570 parameter-declaration:
3571 declaration-specifiers declarator attributes[opt]
3572 declaration-specifiers abstract-declarator[opt] attributes[opt]
3574 identifier-list:
3575 identifier
3576 identifier-list , identifier
3578 abstract-declarator:
3579 pointer
3580 pointer[opt] direct-abstract-declarator
3582 direct-abstract-declarator:
3583 ( attributes[opt] abstract-declarator )
3584 direct-abstract-declarator[opt] array-declarator
3585 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3587 GNU extensions:
3589 direct-declarator:
3590 direct-declarator ( parameter-forward-declarations
3591 parameter-type-list[opt] )
3593 direct-abstract-declarator:
3594 direct-abstract-declarator[opt] ( parameter-forward-declarations
3595 parameter-type-list[opt] )
3597 parameter-forward-declarations:
3598 parameter-list ;
3599 parameter-forward-declarations parameter-list ;
3601 The uses of attributes shown above are GNU extensions.
3603 Some forms of array declarator are not included in C99 in the
3604 syntax for abstract declarators; these are disallowed elsewhere.
3605 This may be a defect (DR#289).
3607 This function also accepts an omitted abstract declarator as being
3608 an abstract declarator, although not part of the formal syntax. */
3610 struct c_declarator *
3611 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3612 bool *seen_id)
3614 /* Parse any initial pointer part. */
3615 if (c_parser_next_token_is (parser, CPP_MULT))
3617 struct c_declspecs *quals_attrs = build_null_declspecs ();
3618 struct c_declarator *inner;
3619 c_parser_consume_token (parser);
3620 c_parser_declspecs (parser, quals_attrs, false, false, true,
3621 false, false, cla_prefer_id);
3622 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3623 if (inner == NULL)
3624 return NULL;
3625 else
3626 return make_pointer_declarator (quals_attrs, inner);
3628 /* Now we have a direct declarator, direct abstract declarator or
3629 nothing (which counts as a direct abstract declarator here). */
3630 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3633 /* Parse a direct declarator or direct abstract declarator; arguments
3634 as c_parser_declarator. */
3636 static struct c_declarator *
3637 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3638 bool *seen_id)
3640 /* The direct declarator must start with an identifier (possibly
3641 omitted) or a parenthesized declarator (possibly abstract). In
3642 an ordinary declarator, initial parentheses must start a
3643 parenthesized declarator. In an abstract declarator or parameter
3644 declarator, they could start a parenthesized declarator or a
3645 parameter list. To tell which, the open parenthesis and any
3646 following attributes must be read. If a declaration specifier
3647 follows, then it is a parameter list; if the specifier is a
3648 typedef name, there might be an ambiguity about redeclaring it,
3649 which is resolved in the direction of treating it as a typedef
3650 name. If a close parenthesis follows, it is also an empty
3651 parameter list, as the syntax does not permit empty abstract
3652 declarators. Otherwise, it is a parenthesized declarator (in
3653 which case the analysis may be repeated inside it, recursively).
3655 ??? There is an ambiguity in a parameter declaration "int
3656 (__attribute__((foo)) x)", where x is not a typedef name: it
3657 could be an abstract declarator for a function, or declare x with
3658 parentheses. The proper resolution of this ambiguity needs
3659 documenting. At present we follow an accident of the old
3660 parser's implementation, whereby the first parameter must have
3661 some declaration specifiers other than just attributes. Thus as
3662 a parameter declaration it is treated as a parenthesized
3663 parameter named x, and as an abstract declarator it is
3664 rejected.
3666 ??? Also following the old parser, attributes inside an empty
3667 parameter list are ignored, making it a list not yielding a
3668 prototype, rather than giving an error or making it have one
3669 parameter with implicit type int.
3671 ??? Also following the old parser, typedef names may be
3672 redeclared in declarators, but not Objective-C class names. */
3674 if (kind != C_DTR_ABSTRACT
3675 && c_parser_next_token_is (parser, CPP_NAME)
3676 && ((type_seen_p
3677 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3678 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3679 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3681 struct c_declarator *inner
3682 = build_id_declarator (c_parser_peek_token (parser)->value);
3683 *seen_id = true;
3684 inner->id_loc = c_parser_peek_token (parser)->location;
3685 c_parser_consume_token (parser);
3686 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3689 if (kind != C_DTR_NORMAL
3690 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3692 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3693 inner->id_loc = c_parser_peek_token (parser)->location;
3694 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3697 /* Either we are at the end of an abstract declarator, or we have
3698 parentheses. */
3700 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3702 tree attrs;
3703 struct c_declarator *inner;
3704 c_parser_consume_token (parser);
3705 attrs = c_parser_attributes (parser);
3706 if (kind != C_DTR_NORMAL
3707 && (c_parser_next_token_starts_declspecs (parser)
3708 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3710 struct c_arg_info *args
3711 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3712 attrs);
3713 if (args == NULL)
3714 return NULL;
3715 else
3717 inner
3718 = build_function_declarator (args,
3719 build_id_declarator (NULL_TREE));
3720 return c_parser_direct_declarator_inner (parser, *seen_id,
3721 inner);
3724 /* A parenthesized declarator. */
3725 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3726 if (inner != NULL && attrs != NULL)
3727 inner = build_attrs_declarator (attrs, inner);
3728 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3730 c_parser_consume_token (parser);
3731 if (inner == NULL)
3732 return NULL;
3733 else
3734 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3736 else
3738 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3739 "expected %<)%>");
3740 return NULL;
3743 else
3745 if (kind == C_DTR_NORMAL)
3747 c_parser_error (parser, "expected identifier or %<(%>");
3748 return NULL;
3750 else
3751 return build_id_declarator (NULL_TREE);
3755 /* Parse part of a direct declarator or direct abstract declarator,
3756 given that some (in INNER) has already been parsed; ID_PRESENT is
3757 true if an identifier is present, false for an abstract
3758 declarator. */
3760 static struct c_declarator *
3761 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3762 struct c_declarator *inner)
3764 /* Parse a sequence of array declarators and parameter lists. */
3765 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3767 location_t brace_loc = c_parser_peek_token (parser)->location;
3768 struct c_declarator *declarator;
3769 struct c_declspecs *quals_attrs = build_null_declspecs ();
3770 bool static_seen;
3771 bool star_seen;
3772 struct c_expr dimen;
3773 dimen.value = NULL_TREE;
3774 dimen.original_code = ERROR_MARK;
3775 dimen.original_type = NULL_TREE;
3776 c_parser_consume_token (parser);
3777 c_parser_declspecs (parser, quals_attrs, false, false, true,
3778 false, false, cla_prefer_id);
3779 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3780 if (static_seen)
3781 c_parser_consume_token (parser);
3782 if (static_seen && !quals_attrs->declspecs_seen_p)
3783 c_parser_declspecs (parser, quals_attrs, false, false, true,
3784 false, false, cla_prefer_id);
3785 if (!quals_attrs->declspecs_seen_p)
3786 quals_attrs = NULL;
3787 /* If "static" is present, there must be an array dimension.
3788 Otherwise, there may be a dimension, "*", or no
3789 dimension. */
3790 if (static_seen)
3792 star_seen = false;
3793 dimen = c_parser_expr_no_commas (parser, NULL);
3795 else
3797 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3799 dimen.value = NULL_TREE;
3800 star_seen = false;
3802 else if (flag_cilkplus
3803 && c_parser_next_token_is (parser, CPP_COLON))
3805 dimen.value = error_mark_node;
3806 star_seen = false;
3807 error_at (c_parser_peek_token (parser)->location,
3808 "array notations cannot be used in declaration");
3809 c_parser_consume_token (parser);
3811 else if (c_parser_next_token_is (parser, CPP_MULT))
3813 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3815 dimen.value = NULL_TREE;
3816 star_seen = true;
3817 c_parser_consume_token (parser);
3819 else
3821 star_seen = false;
3822 dimen = c_parser_expr_no_commas (parser, NULL);
3825 else
3827 star_seen = false;
3828 dimen = c_parser_expr_no_commas (parser, NULL);
3831 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3832 c_parser_consume_token (parser);
3833 else if (flag_cilkplus
3834 && c_parser_next_token_is (parser, CPP_COLON))
3836 error_at (c_parser_peek_token (parser)->location,
3837 "array notations cannot be used in declaration");
3838 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3839 return NULL;
3841 else
3843 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3844 "expected %<]%>");
3845 return NULL;
3847 if (dimen.value)
3848 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3849 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3850 static_seen, star_seen);
3851 if (declarator == NULL)
3852 return NULL;
3853 inner = set_array_declarator_inner (declarator, inner);
3854 return c_parser_direct_declarator_inner (parser, id_present, inner);
3856 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3858 tree attrs;
3859 struct c_arg_info *args;
3860 c_parser_consume_token (parser);
3861 attrs = c_parser_attributes (parser);
3862 args = c_parser_parms_declarator (parser, id_present, attrs);
3863 if (args == NULL)
3864 return NULL;
3865 else
3867 inner = build_function_declarator (args, inner);
3868 return c_parser_direct_declarator_inner (parser, id_present, inner);
3871 return inner;
3874 /* Parse a parameter list or identifier list, including the closing
3875 parenthesis but not the opening one. ATTRS are the attributes at
3876 the start of the list. ID_LIST_OK is true if an identifier list is
3877 acceptable; such a list must not have attributes at the start. */
3879 static struct c_arg_info *
3880 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3882 push_scope ();
3883 declare_parm_level ();
3884 /* If the list starts with an identifier, it is an identifier list.
3885 Otherwise, it is either a prototype list or an empty list. */
3886 if (id_list_ok
3887 && !attrs
3888 && c_parser_next_token_is (parser, CPP_NAME)
3889 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3891 /* Look ahead to detect typos in type names. */
3892 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3893 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3894 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3895 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3896 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3898 tree list = NULL_TREE, *nextp = &list;
3899 while (c_parser_next_token_is (parser, CPP_NAME)
3900 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3902 *nextp = build_tree_list (NULL_TREE,
3903 c_parser_peek_token (parser)->value);
3904 nextp = & TREE_CHAIN (*nextp);
3905 c_parser_consume_token (parser);
3906 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3907 break;
3908 c_parser_consume_token (parser);
3909 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3911 c_parser_error (parser, "expected identifier");
3912 break;
3915 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3917 struct c_arg_info *ret = build_arg_info ();
3918 ret->types = list;
3919 c_parser_consume_token (parser);
3920 pop_scope ();
3921 return ret;
3923 else
3925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3926 "expected %<)%>");
3927 pop_scope ();
3928 return NULL;
3931 else
3933 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3934 NULL);
3935 pop_scope ();
3936 return ret;
3940 /* Parse a parameter list (possibly empty), including the closing
3941 parenthesis but not the opening one. ATTRS are the attributes at
3942 the start of the list. EXPR is NULL or an expression that needs to
3943 be evaluated for the side effects of array size expressions in the
3944 parameters. */
3946 static struct c_arg_info *
3947 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3949 bool bad_parm = false;
3951 /* ??? Following the old parser, forward parameter declarations may
3952 use abstract declarators, and if no real parameter declarations
3953 follow the forward declarations then this is not diagnosed. Also
3954 note as above that attributes are ignored as the only contents of
3955 the parentheses, or as the only contents after forward
3956 declarations. */
3957 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3959 struct c_arg_info *ret = build_arg_info ();
3960 c_parser_consume_token (parser);
3961 return ret;
3963 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3965 struct c_arg_info *ret = build_arg_info ();
3967 if (flag_allow_parameterless_variadic_functions)
3969 /* F (...) is allowed. */
3970 ret->types = NULL_TREE;
3972 else
3974 /* Suppress -Wold-style-definition for this case. */
3975 ret->types = error_mark_node;
3976 error_at (c_parser_peek_token (parser)->location,
3977 "ISO C requires a named argument before %<...%>");
3979 c_parser_consume_token (parser);
3980 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3982 c_parser_consume_token (parser);
3983 return ret;
3985 else
3987 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3988 "expected %<)%>");
3989 return NULL;
3992 /* Nonempty list of parameters, either terminated with semicolon
3993 (forward declarations; recurse) or with close parenthesis (normal
3994 function) or with ", ... )" (variadic function). */
3995 while (true)
3997 /* Parse a parameter. */
3998 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3999 attrs = NULL_TREE;
4000 if (parm == NULL)
4001 bad_parm = true;
4002 else
4003 push_parm_decl (parm, &expr);
4004 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4006 tree new_attrs;
4007 c_parser_consume_token (parser);
4008 mark_forward_parm_decls ();
4009 new_attrs = c_parser_attributes (parser);
4010 return c_parser_parms_list_declarator (parser, new_attrs, expr);
4012 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4014 c_parser_consume_token (parser);
4015 if (bad_parm)
4016 return NULL;
4017 else
4018 return get_parm_info (false, expr);
4020 if (!c_parser_require (parser, CPP_COMMA,
4021 "expected %<;%>, %<,%> or %<)%>",
4022 UNKNOWN_LOCATION, false))
4024 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4025 return NULL;
4027 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4029 c_parser_consume_token (parser);
4030 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4032 c_parser_consume_token (parser);
4033 if (bad_parm)
4034 return NULL;
4035 else
4036 return get_parm_info (true, expr);
4038 else
4040 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4041 "expected %<)%>");
4042 return NULL;
4048 /* Parse a parameter declaration. ATTRS are the attributes at the
4049 start of the declaration if it is the first parameter. */
4051 static struct c_parm *
4052 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4054 struct c_declspecs *specs;
4055 struct c_declarator *declarator;
4056 tree prefix_attrs;
4057 tree postfix_attrs = NULL_TREE;
4058 bool dummy = false;
4060 /* Accept #pragmas between parameter declarations. */
4061 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4062 c_parser_pragma (parser, pragma_param, NULL);
4064 if (!c_parser_next_token_starts_declspecs (parser))
4066 c_token *token = c_parser_peek_token (parser);
4067 if (parser->error)
4068 return NULL;
4069 c_parser_set_source_position_from_token (token);
4070 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4072 name_hint hint = lookup_name_fuzzy (token->value,
4073 FUZZY_LOOKUP_TYPENAME,
4074 token->location);
4075 if (hint)
4077 gcc_rich_location richloc (token->location);
4078 richloc.add_fixit_replace (hint.suggestion ());
4079 error_at (&richloc,
4080 "unknown type name %qE; did you mean %qs?",
4081 token->value, hint.suggestion ());
4083 else
4084 error_at (token->location, "unknown type name %qE", token->value);
4085 parser->error = true;
4087 /* ??? In some Objective-C cases '...' isn't applicable so there
4088 should be a different message. */
4089 else
4090 c_parser_error (parser,
4091 "expected declaration specifiers or %<...%>");
4092 c_parser_skip_to_end_of_parameter (parser);
4093 return NULL;
4096 location_t start_loc = c_parser_peek_token (parser)->location;
4098 specs = build_null_declspecs ();
4099 if (attrs)
4101 declspecs_add_attrs (input_location, specs, attrs);
4102 attrs = NULL_TREE;
4104 c_parser_declspecs (parser, specs, true, true, true, true, false,
4105 cla_nonabstract_decl);
4106 finish_declspecs (specs);
4107 pending_xref_error ();
4108 prefix_attrs = specs->attrs;
4109 specs->attrs = NULL_TREE;
4110 declarator = c_parser_declarator (parser,
4111 specs->typespec_kind != ctsk_none,
4112 C_DTR_PARM, &dummy);
4113 if (declarator == NULL)
4115 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4116 return NULL;
4118 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4119 postfix_attrs = c_parser_attributes (parser);
4121 /* Generate a location for the parameter, ranging from the start of the
4122 initial token to the end of the final token.
4124 If we have a identifier, then use it for the caret location, e.g.
4126 extern int callee (int one, int (*two)(int, int), float three);
4127 ~~~~~~^~~~~~~~~~~~~~
4129 otherwise, reuse the start location for the caret location e.g.:
4131 extern int callee (int one, int (*)(int, int), float three);
4132 ^~~~~~~~~~~~~~~~~
4134 location_t end_loc = parser->last_token_location;
4136 /* Find any cdk_id declarator; determine if we have an identifier. */
4137 c_declarator *id_declarator = declarator;
4138 while (id_declarator && id_declarator->kind != cdk_id)
4139 id_declarator = id_declarator->declarator;
4140 location_t caret_loc = (id_declarator->u.id
4141 ? id_declarator->id_loc
4142 : start_loc);
4143 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4145 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4146 declarator, param_loc);
4149 /* Parse a string literal in an asm expression. It should not be
4150 translated, and wide string literals are an error although
4151 permitted by the syntax. This is a GNU extension.
4153 asm-string-literal:
4154 string-literal
4156 ??? At present, following the old parser, the caller needs to have
4157 set lex_untranslated_string to 1. It would be better to follow the
4158 C++ parser rather than using this kludge. */
4160 static tree
4161 c_parser_asm_string_literal (c_parser *parser)
4163 tree str;
4164 int save_flag = warn_overlength_strings;
4165 warn_overlength_strings = 0;
4166 if (c_parser_next_token_is (parser, CPP_STRING))
4168 str = c_parser_peek_token (parser)->value;
4169 c_parser_consume_token (parser);
4171 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4173 error_at (c_parser_peek_token (parser)->location,
4174 "wide string literal in %<asm%>");
4175 str = build_string (1, "");
4176 c_parser_consume_token (parser);
4178 else
4180 c_parser_error (parser, "expected string literal");
4181 str = NULL_TREE;
4183 warn_overlength_strings = save_flag;
4184 return str;
4187 /* Parse a simple asm expression. This is used in restricted
4188 contexts, where a full expression with inputs and outputs does not
4189 make sense. This is a GNU extension.
4191 simple-asm-expr:
4192 asm ( asm-string-literal )
4195 static tree
4196 c_parser_simple_asm_expr (c_parser *parser)
4198 tree str;
4199 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4200 /* ??? Follow the C++ parser rather than using the
4201 lex_untranslated_string kludge. */
4202 parser->lex_untranslated_string = true;
4203 c_parser_consume_token (parser);
4204 matching_parens parens;
4205 if (!parens.require_open (parser))
4207 parser->lex_untranslated_string = false;
4208 return NULL_TREE;
4210 str = c_parser_asm_string_literal (parser);
4211 parser->lex_untranslated_string = false;
4212 if (!parens.require_close (parser))
4214 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4215 return NULL_TREE;
4217 return str;
4220 static tree
4221 c_parser_attribute_any_word (c_parser *parser)
4223 tree attr_name = NULL_TREE;
4225 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4227 /* ??? See comment above about what keywords are accepted here. */
4228 bool ok;
4229 switch (c_parser_peek_token (parser)->keyword)
4231 case RID_STATIC:
4232 case RID_UNSIGNED:
4233 case RID_LONG:
4234 case RID_CONST:
4235 case RID_EXTERN:
4236 case RID_REGISTER:
4237 case RID_TYPEDEF:
4238 case RID_SHORT:
4239 case RID_INLINE:
4240 case RID_NORETURN:
4241 case RID_VOLATILE:
4242 case RID_SIGNED:
4243 case RID_AUTO:
4244 case RID_RESTRICT:
4245 case RID_COMPLEX:
4246 case RID_THREAD:
4247 case RID_INT:
4248 case RID_CHAR:
4249 case RID_FLOAT:
4250 case RID_DOUBLE:
4251 case RID_VOID:
4252 case RID_DFLOAT32:
4253 case RID_DFLOAT64:
4254 case RID_DFLOAT128:
4255 CASE_RID_FLOATN_NX:
4256 case RID_BOOL:
4257 case RID_FRACT:
4258 case RID_ACCUM:
4259 case RID_SAT:
4260 case RID_TRANSACTION_ATOMIC:
4261 case RID_TRANSACTION_CANCEL:
4262 case RID_ATOMIC:
4263 case RID_AUTO_TYPE:
4264 case RID_INT_N_0:
4265 case RID_INT_N_1:
4266 case RID_INT_N_2:
4267 case RID_INT_N_3:
4268 ok = true;
4269 break;
4270 default:
4271 ok = false;
4272 break;
4274 if (!ok)
4275 return NULL_TREE;
4277 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4278 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4280 else if (c_parser_next_token_is (parser, CPP_NAME))
4281 attr_name = c_parser_peek_token (parser)->value;
4283 return attr_name;
4286 #define CILK_SIMD_FN_CLAUSE_MASK \
4287 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
4288 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
4289 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
4290 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
4291 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
4293 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
4294 VEC_TOKEN is the "vector" token that is replaced with "simd" and
4295 pushed into the token list.
4296 Syntax:
4297 vector
4298 vector (<vector attributes>). */
4300 static void
4301 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
4303 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4305 int paren_scope = 0;
4306 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4307 /* Consume the "vector" token. */
4308 c_parser_consume_token (parser);
4310 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4312 c_parser_consume_token (parser);
4313 paren_scope++;
4315 while (paren_scope > 0)
4317 c_token *token = c_parser_peek_token (parser);
4318 if (token->type == CPP_OPEN_PAREN)
4319 paren_scope++;
4320 else if (token->type == CPP_CLOSE_PAREN)
4321 paren_scope--;
4322 /* Do not push the last ')' since we are not pushing the '('. */
4323 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4324 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4325 c_parser_consume_token (parser);
4328 /* Since we are converting an attribute to a pragma, we need to end the
4329 attribute with PRAGMA_EOL. */
4330 c_token eol_token;
4331 memset (&eol_token, 0, sizeof (eol_token));
4332 eol_token.type = CPP_PRAGMA_EOL;
4333 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4336 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4338 static void
4339 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4341 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4343 /* c_parser_attributes is called in several places, so if these EOF
4344 tokens are already inserted, then don't do them again. */
4345 if (last_token.type == CPP_EOF)
4346 return;
4348 /* Two CPP_EOF token are added as a safety net since the normal C
4349 front-end has two token look-ahead. */
4350 c_token eof_token;
4351 eof_token.type = CPP_EOF;
4352 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4353 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4356 /* Parse (possibly empty) attributes. This is a GNU extension.
4358 attributes:
4359 empty
4360 attributes attribute
4362 attribute:
4363 __attribute__ ( ( attribute-list ) )
4365 attribute-list:
4366 attrib
4367 attribute_list , attrib
4369 attrib:
4370 empty
4371 any-word
4372 any-word ( identifier )
4373 any-word ( identifier , nonempty-expr-list )
4374 any-word ( expr-list )
4376 where the "identifier" must not be declared as a type, and
4377 "any-word" may be any identifier (including one declared as a
4378 type), a reserved word storage class specifier, type specifier or
4379 type qualifier. ??? This still leaves out most reserved keywords
4380 (following the old parser), shouldn't we include them, and why not
4381 allow identifiers declared as types to start the arguments? */
4383 static tree
4384 c_parser_attributes (c_parser *parser)
4386 tree attrs = NULL_TREE;
4387 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4389 /* ??? Follow the C++ parser rather than using the
4390 lex_untranslated_string kludge. */
4391 parser->lex_untranslated_string = true;
4392 /* Consume the `__attribute__' keyword. */
4393 c_parser_consume_token (parser);
4394 /* Look for the two `(' tokens. */
4395 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4397 parser->lex_untranslated_string = false;
4398 return attrs;
4400 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4402 parser->lex_untranslated_string = false;
4403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4404 return attrs;
4406 /* Parse the attribute list. */
4407 while (c_parser_next_token_is (parser, CPP_COMMA)
4408 || c_parser_next_token_is (parser, CPP_NAME)
4409 || c_parser_next_token_is (parser, CPP_KEYWORD))
4411 tree attr, attr_name, attr_args;
4412 vec<tree, va_gc> *expr_list;
4413 if (c_parser_next_token_is (parser, CPP_COMMA))
4415 c_parser_consume_token (parser);
4416 continue;
4419 attr_name = c_parser_attribute_any_word (parser);
4420 if (attr_name == NULL)
4421 break;
4422 attr_name = canonicalize_attr_name (attr_name);
4423 if (is_cilkplus_vector_p (attr_name))
4425 c_token *v_token = c_parser_peek_token (parser);
4426 v_token->value = canonicalize_attr_name (v_token->value);
4427 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4428 /* If the next token isn't a comma, we're done. */
4429 if (!c_parser_next_token_is (parser, CPP_COMMA))
4430 break;
4431 continue;
4433 c_parser_consume_token (parser);
4434 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4436 attr = build_tree_list (attr_name, NULL_TREE);
4437 /* Add this attribute to the list. */
4438 attrs = chainon (attrs, attr);
4439 /* If the next token isn't a comma, we're done. */
4440 if (!c_parser_next_token_is (parser, CPP_COMMA))
4441 break;
4442 continue;
4444 c_parser_consume_token (parser);
4445 /* Parse the attribute contents. If they start with an
4446 identifier which is followed by a comma or close
4447 parenthesis, then the arguments start with that
4448 identifier; otherwise they are an expression list.
4449 In objective-c the identifier may be a classname. */
4450 if (c_parser_next_token_is (parser, CPP_NAME)
4451 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4452 || (c_dialect_objc ()
4453 && c_parser_peek_token (parser)->id_kind
4454 == C_ID_CLASSNAME))
4455 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4456 || (c_parser_peek_2nd_token (parser)->type
4457 == CPP_CLOSE_PAREN))
4458 && (attribute_takes_identifier_p (attr_name)
4459 || (c_dialect_objc ()
4460 && c_parser_peek_token (parser)->id_kind
4461 == C_ID_CLASSNAME)))
4463 tree arg1 = c_parser_peek_token (parser)->value;
4464 c_parser_consume_token (parser);
4465 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4466 attr_args = build_tree_list (NULL_TREE, arg1);
4467 else
4469 tree tree_list;
4470 c_parser_consume_token (parser);
4471 expr_list = c_parser_expr_list (parser, false, true,
4472 NULL, NULL, NULL, NULL);
4473 tree_list = build_tree_list_vec (expr_list);
4474 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4475 release_tree_vector (expr_list);
4478 else
4480 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4481 attr_args = NULL_TREE;
4482 else
4484 expr_list = c_parser_expr_list (parser, false, true,
4485 NULL, NULL, NULL, NULL);
4486 attr_args = build_tree_list_vec (expr_list);
4487 release_tree_vector (expr_list);
4491 attr = build_tree_list (attr_name, attr_args);
4492 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4493 c_parser_consume_token (parser);
4494 else
4496 parser->lex_untranslated_string = false;
4497 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4498 "expected %<)%>");
4499 return attrs;
4501 /* Add this attribute to the list. */
4502 attrs = chainon (attrs, attr);
4503 /* If the next token isn't a comma, we're done. */
4504 if (!c_parser_next_token_is (parser, CPP_COMMA))
4505 break;
4507 /* Look for the two `)' tokens. */
4508 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4509 c_parser_consume_token (parser);
4510 else
4512 parser->lex_untranslated_string = false;
4513 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4514 "expected %<)%>");
4515 return attrs;
4517 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4518 c_parser_consume_token (parser);
4519 else
4521 parser->lex_untranslated_string = false;
4522 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4523 "expected %<)%>");
4524 return attrs;
4526 parser->lex_untranslated_string = false;
4529 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4530 c_finish_cilk_simd_fn_tokens (parser);
4531 return attrs;
4534 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7).
4536 type-name:
4537 specifier-qualifier-list abstract-declarator[opt]
4540 struct c_type_name *
4541 c_parser_type_name (c_parser *parser)
4543 struct c_declspecs *specs = build_null_declspecs ();
4544 struct c_declarator *declarator;
4545 struct c_type_name *ret;
4546 bool dummy = false;
4547 c_parser_declspecs (parser, specs, false, true, true, false, false,
4548 cla_prefer_type);
4549 if (!specs->declspecs_seen_p)
4551 c_parser_error (parser, "expected specifier-qualifier-list");
4552 return NULL;
4554 if (specs->type != error_mark_node)
4556 pending_xref_error ();
4557 finish_declspecs (specs);
4559 declarator = c_parser_declarator (parser,
4560 specs->typespec_kind != ctsk_none,
4561 C_DTR_ABSTRACT, &dummy);
4562 if (declarator == NULL)
4563 return NULL;
4564 ret = XOBNEW (&parser_obstack, struct c_type_name);
4565 ret->specs = specs;
4566 ret->declarator = declarator;
4567 return ret;
4570 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4572 initializer:
4573 assignment-expression
4574 { initializer-list }
4575 { initializer-list , }
4577 initializer-list:
4578 designation[opt] initializer
4579 initializer-list , designation[opt] initializer
4581 designation:
4582 designator-list =
4584 designator-list:
4585 designator
4586 designator-list designator
4588 designator:
4589 array-designator
4590 . identifier
4592 array-designator:
4593 [ constant-expression ]
4595 GNU extensions:
4597 initializer:
4600 designation:
4601 array-designator
4602 identifier :
4604 array-designator:
4605 [ constant-expression ... constant-expression ]
4607 Any expression without commas is accepted in the syntax for the
4608 constant-expressions, with non-constant expressions rejected later.
4610 This function is only used for top-level initializers; for nested
4611 ones, see c_parser_initval. */
4613 static struct c_expr
4614 c_parser_initializer (c_parser *parser)
4616 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4617 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4618 else
4620 struct c_expr ret;
4621 location_t loc = c_parser_peek_token (parser)->location;
4622 ret = c_parser_expr_no_commas (parser, NULL);
4623 if (TREE_CODE (ret.value) != STRING_CST
4624 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4625 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4626 return ret;
4630 /* The location of the last comma within the current initializer list,
4631 or UNKNOWN_LOCATION if not within one. */
4633 location_t last_init_list_comma;
4635 /* Parse a braced initializer list. TYPE is the type specified for a
4636 compound literal, and NULL_TREE for other initializers and for
4637 nested braced lists. NESTED_P is true for nested braced lists,
4638 false for the list of a compound literal or the list that is the
4639 top-level initializer in a declaration. */
4641 static struct c_expr
4642 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4643 struct obstack *outer_obstack)
4645 struct c_expr ret;
4646 struct obstack braced_init_obstack;
4647 location_t brace_loc = c_parser_peek_token (parser)->location;
4648 gcc_obstack_init (&braced_init_obstack);
4649 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4650 matching_braces braces;
4651 braces.consume_open (parser);
4652 if (nested_p)
4654 finish_implicit_inits (brace_loc, outer_obstack);
4655 push_init_level (brace_loc, 0, &braced_init_obstack);
4657 else
4658 really_start_incremental_init (type);
4659 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4661 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4663 else
4665 /* Parse a non-empty initializer list, possibly with a trailing
4666 comma. */
4667 while (true)
4669 c_parser_initelt (parser, &braced_init_obstack);
4670 if (parser->error)
4671 break;
4672 if (c_parser_next_token_is (parser, CPP_COMMA))
4674 last_init_list_comma = c_parser_peek_token (parser)->location;
4675 c_parser_consume_token (parser);
4677 else
4678 break;
4679 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4680 break;
4683 c_token *next_tok = c_parser_peek_token (parser);
4684 if (next_tok->type != CPP_CLOSE_BRACE)
4686 ret.value = error_mark_node;
4687 ret.original_code = ERROR_MARK;
4688 ret.original_type = NULL;
4689 braces.skip_until_found_close (parser);
4690 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4691 obstack_free (&braced_init_obstack, NULL);
4692 return ret;
4694 location_t close_loc = next_tok->location;
4695 c_parser_consume_token (parser);
4696 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4697 obstack_free (&braced_init_obstack, NULL);
4698 set_c_expr_source_range (&ret, brace_loc, close_loc);
4699 return ret;
4702 /* Parse a nested initializer, including designators. */
4704 static void
4705 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4707 /* Parse any designator or designator list. A single array
4708 designator may have the subsequent "=" omitted in GNU C, but a
4709 longer list or a structure member designator may not. */
4710 if (c_parser_next_token_is (parser, CPP_NAME)
4711 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4713 /* Old-style structure member designator. */
4714 set_init_label (c_parser_peek_token (parser)->location,
4715 c_parser_peek_token (parser)->value,
4716 c_parser_peek_token (parser)->location,
4717 braced_init_obstack);
4718 /* Use the colon as the error location. */
4719 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4720 "obsolete use of designated initializer with %<:%>");
4721 c_parser_consume_token (parser);
4722 c_parser_consume_token (parser);
4724 else
4726 /* des_seen is 0 if there have been no designators, 1 if there
4727 has been a single array designator and 2 otherwise. */
4728 int des_seen = 0;
4729 /* Location of a designator. */
4730 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4731 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4732 || c_parser_next_token_is (parser, CPP_DOT))
4734 int des_prev = des_seen;
4735 if (!des_seen)
4736 des_loc = c_parser_peek_token (parser)->location;
4737 if (des_seen < 2)
4738 des_seen++;
4739 if (c_parser_next_token_is (parser, CPP_DOT))
4741 des_seen = 2;
4742 c_parser_consume_token (parser);
4743 if (c_parser_next_token_is (parser, CPP_NAME))
4745 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4746 c_parser_peek_token (parser)->location,
4747 braced_init_obstack);
4748 c_parser_consume_token (parser);
4750 else
4752 struct c_expr init;
4753 init.value = error_mark_node;
4754 init.original_code = ERROR_MARK;
4755 init.original_type = NULL;
4756 c_parser_error (parser, "expected identifier");
4757 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4758 process_init_element (input_location, init, false,
4759 braced_init_obstack);
4760 return;
4763 else
4765 tree first, second;
4766 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4767 location_t array_index_loc = UNKNOWN_LOCATION;
4768 /* ??? Following the old parser, [ objc-receiver
4769 objc-message-args ] is accepted as an initializer,
4770 being distinguished from a designator by what follows
4771 the first assignment expression inside the square
4772 brackets, but after a first array designator a
4773 subsequent square bracket is for Objective-C taken to
4774 start an expression, using the obsolete form of
4775 designated initializer without '=', rather than
4776 possibly being a second level of designation: in LALR
4777 terms, the '[' is shifted rather than reducing
4778 designator to designator-list. */
4779 if (des_prev == 1 && c_dialect_objc ())
4781 des_seen = des_prev;
4782 break;
4784 if (des_prev == 0 && c_dialect_objc ())
4786 /* This might be an array designator or an
4787 Objective-C message expression. If the former,
4788 continue parsing here; if the latter, parse the
4789 remainder of the initializer given the starting
4790 primary-expression. ??? It might make sense to
4791 distinguish when des_prev == 1 as well; see
4792 previous comment. */
4793 tree rec, args;
4794 struct c_expr mexpr;
4795 c_parser_consume_token (parser);
4796 if (c_parser_peek_token (parser)->type == CPP_NAME
4797 && ((c_parser_peek_token (parser)->id_kind
4798 == C_ID_TYPENAME)
4799 || (c_parser_peek_token (parser)->id_kind
4800 == C_ID_CLASSNAME)))
4802 /* Type name receiver. */
4803 tree id = c_parser_peek_token (parser)->value;
4804 c_parser_consume_token (parser);
4805 rec = objc_get_class_reference (id);
4806 goto parse_message_args;
4808 first = c_parser_expr_no_commas (parser, NULL).value;
4809 mark_exp_read (first);
4810 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4811 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4812 goto array_desig_after_first;
4813 /* Expression receiver. So far only one part
4814 without commas has been parsed; there might be
4815 more of the expression. */
4816 rec = first;
4817 while (c_parser_next_token_is (parser, CPP_COMMA))
4819 struct c_expr next;
4820 location_t comma_loc, exp_loc;
4821 comma_loc = c_parser_peek_token (parser)->location;
4822 c_parser_consume_token (parser);
4823 exp_loc = c_parser_peek_token (parser)->location;
4824 next = c_parser_expr_no_commas (parser, NULL);
4825 next = convert_lvalue_to_rvalue (exp_loc, next,
4826 true, true);
4827 rec = build_compound_expr (comma_loc, rec, next.value);
4829 parse_message_args:
4830 /* Now parse the objc-message-args. */
4831 args = c_parser_objc_message_args (parser);
4832 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4833 "expected %<]%>");
4834 mexpr.value
4835 = objc_build_message_expr (rec, args);
4836 mexpr.original_code = ERROR_MARK;
4837 mexpr.original_type = NULL;
4838 /* Now parse and process the remainder of the
4839 initializer, starting with this message
4840 expression as a primary-expression. */
4841 c_parser_initval (parser, &mexpr, braced_init_obstack);
4842 return;
4844 c_parser_consume_token (parser);
4845 array_index_loc = c_parser_peek_token (parser)->location;
4846 first = c_parser_expr_no_commas (parser, NULL).value;
4847 mark_exp_read (first);
4848 array_desig_after_first:
4849 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4851 ellipsis_loc = c_parser_peek_token (parser)->location;
4852 c_parser_consume_token (parser);
4853 second = c_parser_expr_no_commas (parser, NULL).value;
4854 mark_exp_read (second);
4856 else
4857 second = NULL_TREE;
4858 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4860 c_parser_consume_token (parser);
4861 set_init_index (array_index_loc, first, second,
4862 braced_init_obstack);
4863 if (second)
4864 pedwarn (ellipsis_loc, OPT_Wpedantic,
4865 "ISO C forbids specifying range of elements to initialize");
4867 else
4868 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4869 "expected %<]%>");
4872 if (des_seen >= 1)
4874 if (c_parser_next_token_is (parser, CPP_EQ))
4876 pedwarn_c90 (des_loc, OPT_Wpedantic,
4877 "ISO C90 forbids specifying subobject "
4878 "to initialize");
4879 c_parser_consume_token (parser);
4881 else
4883 if (des_seen == 1)
4884 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4885 "obsolete use of designated initializer without %<=%>");
4886 else
4888 struct c_expr init;
4889 init.value = error_mark_node;
4890 init.original_code = ERROR_MARK;
4891 init.original_type = NULL;
4892 c_parser_error (parser, "expected %<=%>");
4893 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4894 process_init_element (input_location, init, false,
4895 braced_init_obstack);
4896 return;
4901 c_parser_initval (parser, NULL, braced_init_obstack);
4904 /* Parse a nested initializer; as c_parser_initializer but parses
4905 initializers within braced lists, after any designators have been
4906 applied. If AFTER is not NULL then it is an Objective-C message
4907 expression which is the primary-expression starting the
4908 initializer. */
4910 static void
4911 c_parser_initval (c_parser *parser, struct c_expr *after,
4912 struct obstack * braced_init_obstack)
4914 struct c_expr init;
4915 gcc_assert (!after || c_dialect_objc ());
4916 location_t loc = c_parser_peek_token (parser)->location;
4918 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4919 init = c_parser_braced_init (parser, NULL_TREE, true,
4920 braced_init_obstack);
4921 else
4923 init = c_parser_expr_no_commas (parser, after);
4924 if (init.value != NULL_TREE
4925 && TREE_CODE (init.value) != STRING_CST
4926 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4927 init = convert_lvalue_to_rvalue (loc, init, true, true);
4929 process_init_element (loc, init, false, braced_init_obstack);
4932 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4933 C99 6.8.2, C11 6.8.2).
4935 compound-statement:
4936 { block-item-list[opt] }
4937 { label-declarations block-item-list }
4939 block-item-list:
4940 block-item
4941 block-item-list block-item
4943 block-item:
4944 nested-declaration
4945 statement
4947 nested-declaration:
4948 declaration
4950 GNU extensions:
4952 compound-statement:
4953 { label-declarations block-item-list }
4955 nested-declaration:
4956 __extension__ nested-declaration
4957 nested-function-definition
4959 label-declarations:
4960 label-declaration
4961 label-declarations label-declaration
4963 label-declaration:
4964 __label__ identifier-list ;
4966 Allowing the mixing of declarations and code is new in C99. The
4967 GNU syntax also permits (not shown above) labels at the end of
4968 compound statements, which yield an error. We don't allow labels
4969 on declarations; this might seem like a natural extension, but
4970 there would be a conflict between attributes on the label and
4971 prefix attributes on the declaration. ??? The syntax follows the
4972 old parser in requiring something after label declarations.
4973 Although they are erroneous if the labels declared aren't defined,
4974 is it useful for the syntax to be this way?
4976 OpenACC:
4978 block-item:
4979 openacc-directive
4981 openacc-directive:
4982 update-directive
4984 OpenMP:
4986 block-item:
4987 openmp-directive
4989 openmp-directive:
4990 barrier-directive
4991 flush-directive
4992 taskwait-directive
4993 taskyield-directive
4994 cancel-directive
4995 cancellation-point-directive */
4997 static tree
4998 c_parser_compound_statement (c_parser *parser)
5000 tree stmt;
5001 location_t brace_loc;
5002 brace_loc = c_parser_peek_token (parser)->location;
5003 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5005 /* Ensure a scope is entered and left anyway to avoid confusion
5006 if we have just prepared to enter a function body. */
5007 stmt = c_begin_compound_stmt (true);
5008 c_end_compound_stmt (brace_loc, stmt, true);
5009 return error_mark_node;
5011 stmt = c_begin_compound_stmt (true);
5012 c_parser_compound_statement_nostart (parser);
5014 /* If the compound stmt contains array notations, then we expand them. */
5015 if (flag_cilkplus && contains_array_notation_expr (stmt))
5016 stmt = expand_array_notation_exprs (stmt);
5017 return c_end_compound_stmt (brace_loc, stmt, true);
5020 /* Parse a compound statement except for the opening brace. This is
5021 used for parsing both compound statements and statement expressions
5022 (which follow different paths to handling the opening). */
5024 static void
5025 c_parser_compound_statement_nostart (c_parser *parser)
5027 bool last_stmt = false;
5028 bool last_label = false;
5029 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
5030 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5031 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5033 c_parser_consume_token (parser);
5034 return;
5036 mark_valid_location_for_stdc_pragma (true);
5037 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
5039 /* Read zero or more forward-declarations for labels that nested
5040 functions can jump to. */
5041 mark_valid_location_for_stdc_pragma (false);
5042 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
5044 label_loc = c_parser_peek_token (parser)->location;
5045 c_parser_consume_token (parser);
5046 /* Any identifiers, including those declared as type names,
5047 are OK here. */
5048 while (true)
5050 tree label;
5051 if (c_parser_next_token_is_not (parser, CPP_NAME))
5053 c_parser_error (parser, "expected identifier");
5054 break;
5056 label
5057 = declare_label (c_parser_peek_token (parser)->value);
5058 C_DECLARED_LABEL_FLAG (label) = 1;
5059 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
5060 c_parser_consume_token (parser);
5061 if (c_parser_next_token_is (parser, CPP_COMMA))
5062 c_parser_consume_token (parser);
5063 else
5064 break;
5066 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5068 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
5070 /* We must now have at least one statement, label or declaration. */
5071 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5073 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5074 c_parser_error (parser, "expected declaration or statement");
5075 c_parser_consume_token (parser);
5076 return;
5078 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
5080 location_t loc = c_parser_peek_token (parser)->location;
5081 if (c_parser_next_token_is_keyword (parser, RID_CASE)
5082 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5083 || (c_parser_next_token_is (parser, CPP_NAME)
5084 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5086 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5087 label_loc = c_parser_peek_2nd_token (parser)->location;
5088 else
5089 label_loc = c_parser_peek_token (parser)->location;
5090 last_label = true;
5091 last_stmt = false;
5092 mark_valid_location_for_stdc_pragma (false);
5093 c_parser_label (parser);
5095 else if (!last_label
5096 && c_parser_next_tokens_start_declaration (parser))
5098 last_label = false;
5099 mark_valid_location_for_stdc_pragma (false);
5100 bool fallthru_attr_p = false;
5101 c_parser_declaration_or_fndef (parser, true, true, true, true,
5102 true, NULL, vNULL, NULL,
5103 &fallthru_attr_p);
5104 if (last_stmt && !fallthru_attr_p)
5105 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5106 "ISO C90 forbids mixed declarations and code");
5107 last_stmt = fallthru_attr_p;
5109 else if (!last_label
5110 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5112 /* __extension__ can start a declaration, but is also an
5113 unary operator that can start an expression. Consume all
5114 but the last of a possible series of __extension__ to
5115 determine which. */
5116 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5117 && (c_parser_peek_2nd_token (parser)->keyword
5118 == RID_EXTENSION))
5119 c_parser_consume_token (parser);
5120 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5122 int ext;
5123 ext = disable_extension_diagnostics ();
5124 c_parser_consume_token (parser);
5125 last_label = false;
5126 mark_valid_location_for_stdc_pragma (false);
5127 c_parser_declaration_or_fndef (parser, true, true, true, true,
5128 true, NULL, vNULL);
5129 /* Following the old parser, __extension__ does not
5130 disable this diagnostic. */
5131 restore_extension_diagnostics (ext);
5132 if (last_stmt)
5133 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5134 "ISO C90 forbids mixed declarations and code");
5135 last_stmt = false;
5137 else
5138 goto statement;
5140 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5142 /* External pragmas, and some omp pragmas, are not associated
5143 with regular c code, and so are not to be considered statements
5144 syntactically. This ensures that the user doesn't put them
5145 places that would turn into syntax errors if the directive
5146 were ignored. */
5147 if (c_parser_pragma (parser,
5148 last_label ? pragma_stmt : pragma_compound,
5149 NULL))
5150 last_label = false, last_stmt = true;
5152 else if (c_parser_next_token_is (parser, CPP_EOF))
5154 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5155 c_parser_error (parser, "expected declaration or statement");
5156 return;
5158 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5160 if (parser->in_if_block)
5162 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5163 error_at (loc, "expected %<}%> before %<else%>");
5164 return;
5166 else
5168 error_at (loc, "%<else%> without a previous %<if%>");
5169 c_parser_consume_token (parser);
5170 continue;
5173 else
5175 statement:
5176 last_label = false;
5177 last_stmt = true;
5178 mark_valid_location_for_stdc_pragma (false);
5179 c_parser_statement_after_labels (parser, NULL);
5182 parser->error = false;
5184 if (last_label)
5185 error_at (label_loc, "label at end of compound statement");
5186 c_parser_consume_token (parser);
5187 /* Restore the value we started with. */
5188 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5191 /* Parse all consecutive labels. */
5193 static void
5194 c_parser_all_labels (c_parser *parser)
5196 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5197 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5198 || (c_parser_next_token_is (parser, CPP_NAME)
5199 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5200 c_parser_label (parser);
5203 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5205 label:
5206 identifier : attributes[opt]
5207 case constant-expression :
5208 default :
5210 GNU extensions:
5212 label:
5213 case constant-expression ... constant-expression :
5215 The use of attributes on labels is a GNU extension. The syntax in
5216 GNU C accepts any expressions without commas, non-constant
5217 expressions being rejected later. */
5219 static void
5220 c_parser_label (c_parser *parser)
5222 location_t loc1 = c_parser_peek_token (parser)->location;
5223 tree label = NULL_TREE;
5225 /* Remember whether this case or a user-defined label is allowed to fall
5226 through to. */
5227 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5229 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5231 tree exp1, exp2;
5232 c_parser_consume_token (parser);
5233 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5234 if (c_parser_next_token_is (parser, CPP_COLON))
5236 c_parser_consume_token (parser);
5237 label = do_case (loc1, exp1, NULL_TREE);
5239 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5241 c_parser_consume_token (parser);
5242 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5243 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5244 label = do_case (loc1, exp1, exp2);
5246 else
5247 c_parser_error (parser, "expected %<:%> or %<...%>");
5249 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5251 c_parser_consume_token (parser);
5252 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5253 label = do_case (loc1, NULL_TREE, NULL_TREE);
5255 else
5257 tree name = c_parser_peek_token (parser)->value;
5258 tree tlab;
5259 tree attrs;
5260 location_t loc2 = c_parser_peek_token (parser)->location;
5261 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5262 c_parser_consume_token (parser);
5263 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5264 c_parser_consume_token (parser);
5265 attrs = c_parser_attributes (parser);
5266 tlab = define_label (loc2, name);
5267 if (tlab)
5269 decl_attributes (&tlab, attrs, 0);
5270 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5273 if (label)
5275 if (TREE_CODE (label) == LABEL_EXPR)
5276 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5277 else
5278 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5280 /* Allow '__attribute__((fallthrough));'. */
5281 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5283 location_t loc = c_parser_peek_token (parser)->location;
5284 tree attrs = c_parser_attributes (parser);
5285 if (attribute_fallthrough_p (attrs))
5287 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5289 tree fn = build_call_expr_internal_loc (loc,
5290 IFN_FALLTHROUGH,
5291 void_type_node, 0);
5292 add_stmt (fn);
5294 else
5295 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5296 "not followed by %<;%>");
5298 else if (attrs != NULL_TREE)
5299 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5300 " can be applied to a null statement");
5302 if (c_parser_next_tokens_start_declaration (parser))
5304 error_at (c_parser_peek_token (parser)->location,
5305 "a label can only be part of a statement and "
5306 "a declaration is not a statement");
5307 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5308 /*static_assert_ok*/ true,
5309 /*empty_ok*/ true, /*nested*/ true,
5310 /*start_attr_ok*/ true, NULL,
5311 vNULL);
5316 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5318 statement:
5319 labeled-statement
5320 compound-statement
5321 expression-statement
5322 selection-statement
5323 iteration-statement
5324 jump-statement
5326 labeled-statement:
5327 label statement
5329 expression-statement:
5330 expression[opt] ;
5332 selection-statement:
5333 if-statement
5334 switch-statement
5336 iteration-statement:
5337 while-statement
5338 do-statement
5339 for-statement
5341 jump-statement:
5342 goto identifier ;
5343 continue ;
5344 break ;
5345 return expression[opt] ;
5347 GNU extensions:
5349 statement:
5350 asm-statement
5352 jump-statement:
5353 goto * expression ;
5355 expression-statement:
5356 attributes ;
5358 Objective-C:
5360 statement:
5361 objc-throw-statement
5362 objc-try-catch-statement
5363 objc-synchronized-statement
5365 objc-throw-statement:
5366 @throw expression ;
5367 @throw ;
5369 OpenACC:
5371 statement:
5372 openacc-construct
5374 openacc-construct:
5375 parallel-construct
5376 kernels-construct
5377 data-construct
5378 loop-construct
5380 parallel-construct:
5381 parallel-directive structured-block
5383 kernels-construct:
5384 kernels-directive structured-block
5386 data-construct:
5387 data-directive structured-block
5389 loop-construct:
5390 loop-directive structured-block
5392 OpenMP:
5394 statement:
5395 openmp-construct
5397 openmp-construct:
5398 parallel-construct
5399 for-construct
5400 simd-construct
5401 for-simd-construct
5402 sections-construct
5403 single-construct
5404 parallel-for-construct
5405 parallel-for-simd-construct
5406 parallel-sections-construct
5407 master-construct
5408 critical-construct
5409 atomic-construct
5410 ordered-construct
5412 parallel-construct:
5413 parallel-directive structured-block
5415 for-construct:
5416 for-directive iteration-statement
5418 simd-construct:
5419 simd-directive iteration-statements
5421 for-simd-construct:
5422 for-simd-directive iteration-statements
5424 sections-construct:
5425 sections-directive section-scope
5427 single-construct:
5428 single-directive structured-block
5430 parallel-for-construct:
5431 parallel-for-directive iteration-statement
5433 parallel-for-simd-construct:
5434 parallel-for-simd-directive iteration-statement
5436 parallel-sections-construct:
5437 parallel-sections-directive section-scope
5439 master-construct:
5440 master-directive structured-block
5442 critical-construct:
5443 critical-directive structured-block
5445 atomic-construct:
5446 atomic-directive expression-statement
5448 ordered-construct:
5449 ordered-directive structured-block
5451 Transactional Memory:
5453 statement:
5454 transaction-statement
5455 transaction-cancel-statement
5457 IF_P is used to track whether there's a (possibly labeled) if statement
5458 which is not enclosed in braces and has an else clause. This is used to
5459 implement -Wparentheses. */
5461 static void
5462 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5464 c_parser_all_labels (parser);
5465 if (loc_after_labels)
5466 *loc_after_labels = c_parser_peek_token (parser)->location;
5467 c_parser_statement_after_labels (parser, if_p, NULL);
5470 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5471 of if-else-if conditions.
5473 IF_P is used to track whether there's a (possibly labeled) if statement
5474 which is not enclosed in braces and has an else clause. This is used to
5475 implement -Wparentheses. */
5477 static void
5478 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5479 vec<tree> *chain)
5481 location_t loc = c_parser_peek_token (parser)->location;
5482 tree stmt = NULL_TREE;
5483 bool in_if_block = parser->in_if_block;
5484 parser->in_if_block = false;
5485 if (if_p != NULL)
5486 *if_p = false;
5487 switch (c_parser_peek_token (parser)->type)
5489 case CPP_OPEN_BRACE:
5490 add_stmt (c_parser_compound_statement (parser));
5491 break;
5492 case CPP_KEYWORD:
5493 switch (c_parser_peek_token (parser)->keyword)
5495 case RID_IF:
5496 c_parser_if_statement (parser, if_p, chain);
5497 break;
5498 case RID_SWITCH:
5499 c_parser_switch_statement (parser, if_p);
5500 break;
5501 case RID_WHILE:
5502 c_parser_while_statement (parser, false, if_p);
5503 break;
5504 case RID_DO:
5505 c_parser_do_statement (parser, false);
5506 break;
5507 case RID_FOR:
5508 c_parser_for_statement (parser, false, if_p);
5509 break;
5510 case RID_CILK_FOR:
5511 if (!flag_cilkplus)
5513 error_at (c_parser_peek_token (parser)->location,
5514 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5515 c_parser_skip_to_end_of_block_or_statement (parser);
5517 else
5518 c_parser_cilk_for (parser, integer_zero_node, if_p);
5519 break;
5520 case RID_CILK_SYNC:
5521 c_parser_consume_token (parser);
5522 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5523 if (!flag_cilkplus)
5524 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5525 else
5526 add_stmt (build_cilk_sync ());
5527 break;
5528 case RID_GOTO:
5529 c_parser_consume_token (parser);
5530 if (c_parser_next_token_is (parser, CPP_NAME))
5532 stmt = c_finish_goto_label (loc,
5533 c_parser_peek_token (parser)->value);
5534 c_parser_consume_token (parser);
5536 else if (c_parser_next_token_is (parser, CPP_MULT))
5538 struct c_expr val;
5540 c_parser_consume_token (parser);
5541 val = c_parser_expression (parser);
5542 if (check_no_cilk (val.value,
5543 "Cilk array notation cannot be used as a computed goto expression",
5544 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5545 loc))
5546 val.value = error_mark_node;
5547 val = convert_lvalue_to_rvalue (loc, val, false, true);
5548 stmt = c_finish_goto_ptr (loc, val.value);
5550 else
5551 c_parser_error (parser, "expected identifier or %<*%>");
5552 goto expect_semicolon;
5553 case RID_CONTINUE:
5554 c_parser_consume_token (parser);
5555 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5556 goto expect_semicolon;
5557 case RID_BREAK:
5558 c_parser_consume_token (parser);
5559 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5560 goto expect_semicolon;
5561 case RID_RETURN:
5562 c_parser_consume_token (parser);
5563 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5565 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5566 c_parser_consume_token (parser);
5568 else
5570 location_t xloc = c_parser_peek_token (parser)->location;
5571 struct c_expr expr = c_parser_expression_conv (parser);
5572 mark_exp_read (expr.value);
5573 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5574 expr.value, expr.original_type);
5575 goto expect_semicolon;
5577 break;
5578 case RID_ASM:
5579 stmt = c_parser_asm_statement (parser);
5580 break;
5581 case RID_TRANSACTION_ATOMIC:
5582 case RID_TRANSACTION_RELAXED:
5583 stmt = c_parser_transaction (parser,
5584 c_parser_peek_token (parser)->keyword);
5585 break;
5586 case RID_TRANSACTION_CANCEL:
5587 stmt = c_parser_transaction_cancel (parser);
5588 goto expect_semicolon;
5589 case RID_AT_THROW:
5590 gcc_assert (c_dialect_objc ());
5591 c_parser_consume_token (parser);
5592 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5594 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5595 c_parser_consume_token (parser);
5597 else
5599 struct c_expr expr = c_parser_expression (parser);
5600 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5601 if (check_no_cilk (expr.value,
5602 "Cilk array notation cannot be used for a throw expression",
5603 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5604 expr.value = error_mark_node;
5605 else
5607 expr.value = c_fully_fold (expr.value, false, NULL);
5608 stmt = objc_build_throw_stmt (loc, expr.value);
5610 goto expect_semicolon;
5612 break;
5613 case RID_AT_TRY:
5614 gcc_assert (c_dialect_objc ());
5615 c_parser_objc_try_catch_finally_statement (parser);
5616 break;
5617 case RID_AT_SYNCHRONIZED:
5618 gcc_assert (c_dialect_objc ());
5619 c_parser_objc_synchronized_statement (parser);
5620 break;
5621 case RID_ATTRIBUTE:
5623 /* Allow '__attribute__((fallthrough));'. */
5624 tree attrs = c_parser_attributes (parser);
5625 if (attribute_fallthrough_p (attrs))
5627 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5629 tree fn = build_call_expr_internal_loc (loc,
5630 IFN_FALLTHROUGH,
5631 void_type_node, 0);
5632 add_stmt (fn);
5633 /* Eat the ';'. */
5634 c_parser_consume_token (parser);
5636 else
5637 warning_at (loc, OPT_Wattributes,
5638 "%<fallthrough%> attribute not followed "
5639 "by %<;%>");
5641 else if (attrs != NULL_TREE)
5642 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5643 " can be applied to a null statement");
5644 break;
5646 default:
5647 goto expr_stmt;
5649 break;
5650 case CPP_SEMICOLON:
5651 c_parser_consume_token (parser);
5652 break;
5653 case CPP_CLOSE_PAREN:
5654 case CPP_CLOSE_SQUARE:
5655 /* Avoid infinite loop in error recovery:
5656 c_parser_skip_until_found stops at a closing nesting
5657 delimiter without consuming it, but here we need to consume
5658 it to proceed further. */
5659 c_parser_error (parser, "expected statement");
5660 c_parser_consume_token (parser);
5661 break;
5662 case CPP_PRAGMA:
5663 c_parser_pragma (parser, pragma_stmt, if_p);
5664 break;
5665 default:
5666 expr_stmt:
5667 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5668 expect_semicolon:
5669 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5670 break;
5672 /* Two cases cannot and do not have line numbers associated: If stmt
5673 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5674 cannot hold line numbers. But that's OK because the statement
5675 will either be changed to a MODIFY_EXPR during gimplification of
5676 the statement expr, or discarded. If stmt was compound, but
5677 without new variables, we will have skipped the creation of a
5678 BIND and will have a bare STATEMENT_LIST. But that's OK because
5679 (recursively) all of the component statements should already have
5680 line numbers assigned. ??? Can we discard no-op statements
5681 earlier? */
5682 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5683 protected_set_expr_location (stmt, loc);
5685 parser->in_if_block = in_if_block;
5688 /* Parse the condition from an if, do, while or for statements. */
5690 static tree
5691 c_parser_condition (c_parser *parser)
5693 location_t loc = c_parser_peek_token (parser)->location;
5694 tree cond;
5695 cond = c_parser_expression_conv (parser).value;
5696 cond = c_objc_common_truthvalue_conversion (loc, cond);
5697 cond = c_fully_fold (cond, false, NULL);
5698 if (warn_sequence_point)
5699 verify_sequence_points (cond);
5700 return cond;
5703 /* Parse a parenthesized condition from an if, do or while statement.
5705 condition:
5706 ( expression )
5708 static tree
5709 c_parser_paren_condition (c_parser *parser)
5711 tree cond;
5712 matching_parens parens;
5713 if (!parens.require_open (parser))
5714 return error_mark_node;
5715 cond = c_parser_condition (parser);
5716 parens.skip_until_found_close (parser);
5717 return cond;
5720 /* Parse a statement which is a block in C99.
5722 IF_P is used to track whether there's a (possibly labeled) if statement
5723 which is not enclosed in braces and has an else clause. This is used to
5724 implement -Wparentheses. */
5726 static tree
5727 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5728 location_t *loc_after_labels)
5730 tree block = c_begin_compound_stmt (flag_isoc99);
5731 location_t loc = c_parser_peek_token (parser)->location;
5732 c_parser_statement (parser, if_p, loc_after_labels);
5733 return c_end_compound_stmt (loc, block, flag_isoc99);
5736 /* Parse the body of an if statement. This is just parsing a
5737 statement but (a) it is a block in C99, (b) we track whether the
5738 body is an if statement for the sake of -Wparentheses warnings, (c)
5739 we handle an empty body specially for the sake of -Wempty-body
5740 warnings, and (d) we call parser_compound_statement directly
5741 because c_parser_statement_after_labels resets
5742 parser->in_if_block.
5744 IF_P is used to track whether there's a (possibly labeled) if statement
5745 which is not enclosed in braces and has an else clause. This is used to
5746 implement -Wparentheses. */
5748 static tree
5749 c_parser_if_body (c_parser *parser, bool *if_p,
5750 const token_indent_info &if_tinfo)
5752 tree block = c_begin_compound_stmt (flag_isoc99);
5753 location_t body_loc = c_parser_peek_token (parser)->location;
5754 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5755 token_indent_info body_tinfo
5756 = get_token_indent_info (c_parser_peek_token (parser));
5758 c_parser_all_labels (parser);
5759 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5761 location_t loc = c_parser_peek_token (parser)->location;
5762 add_stmt (build_empty_stmt (loc));
5763 c_parser_consume_token (parser);
5764 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5765 warning_at (loc, OPT_Wempty_body,
5766 "suggest braces around empty body in an %<if%> statement");
5768 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5769 add_stmt (c_parser_compound_statement (parser));
5770 else
5772 body_loc_after_labels = c_parser_peek_token (parser)->location;
5773 c_parser_statement_after_labels (parser, if_p);
5776 token_indent_info next_tinfo
5777 = get_token_indent_info (c_parser_peek_token (parser));
5778 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5779 if (body_loc_after_labels != UNKNOWN_LOCATION
5780 && next_tinfo.type != CPP_SEMICOLON)
5781 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5782 if_tinfo.location, RID_IF);
5784 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5787 /* Parse the else body of an if statement. This is just parsing a
5788 statement but (a) it is a block in C99, (b) we handle an empty body
5789 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5790 of if-else-if conditions. */
5792 static tree
5793 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5794 vec<tree> *chain)
5796 location_t body_loc = c_parser_peek_token (parser)->location;
5797 tree block = c_begin_compound_stmt (flag_isoc99);
5798 token_indent_info body_tinfo
5799 = get_token_indent_info (c_parser_peek_token (parser));
5800 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5802 c_parser_all_labels (parser);
5803 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5805 location_t loc = c_parser_peek_token (parser)->location;
5806 warning_at (loc,
5807 OPT_Wempty_body,
5808 "suggest braces around empty body in an %<else%> statement");
5809 add_stmt (build_empty_stmt (loc));
5810 c_parser_consume_token (parser);
5812 else
5814 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5815 body_loc_after_labels = c_parser_peek_token (parser)->location;
5816 c_parser_statement_after_labels (parser, NULL, chain);
5819 token_indent_info next_tinfo
5820 = get_token_indent_info (c_parser_peek_token (parser));
5821 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5822 if (body_loc_after_labels != UNKNOWN_LOCATION
5823 && next_tinfo.type != CPP_SEMICOLON)
5824 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5825 else_tinfo.location, RID_ELSE);
5827 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5830 /* We might need to reclassify any previously-lexed identifier, e.g.
5831 when we've left a for loop with an if-statement without else in the
5832 body - we might have used a wrong scope for the token. See PR67784. */
5834 static void
5835 c_parser_maybe_reclassify_token (c_parser *parser)
5837 if (c_parser_next_token_is (parser, CPP_NAME))
5839 c_token *token = c_parser_peek_token (parser);
5841 if (token->id_kind != C_ID_CLASSNAME)
5843 tree decl = lookup_name (token->value);
5845 token->id_kind = C_ID_ID;
5846 if (decl)
5848 if (TREE_CODE (decl) == TYPE_DECL)
5849 token->id_kind = C_ID_TYPENAME;
5851 else if (c_dialect_objc ())
5853 tree objc_interface_decl = objc_is_class_name (token->value);
5854 /* Objective-C class names are in the same namespace as
5855 variables and typedefs, and hence are shadowed by local
5856 declarations. */
5857 if (objc_interface_decl)
5859 token->value = objc_interface_decl;
5860 token->id_kind = C_ID_CLASSNAME;
5867 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5869 if-statement:
5870 if ( expression ) statement
5871 if ( expression ) statement else statement
5873 CHAIN is a vector of if-else-if conditions.
5874 IF_P is used to track whether there's a (possibly labeled) if statement
5875 which is not enclosed in braces and has an else clause. This is used to
5876 implement -Wparentheses. */
5878 static void
5879 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5881 tree block;
5882 location_t loc;
5883 tree cond;
5884 bool nested_if = false;
5885 tree first_body, second_body;
5886 bool in_if_block;
5887 tree if_stmt;
5889 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5890 token_indent_info if_tinfo
5891 = get_token_indent_info (c_parser_peek_token (parser));
5892 c_parser_consume_token (parser);
5893 block = c_begin_compound_stmt (flag_isoc99);
5894 loc = c_parser_peek_token (parser)->location;
5895 cond = c_parser_paren_condition (parser);
5896 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5898 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5899 cond = error_mark_node;
5901 in_if_block = parser->in_if_block;
5902 parser->in_if_block = true;
5903 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5904 parser->in_if_block = in_if_block;
5906 if (warn_duplicated_cond)
5907 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5909 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5911 token_indent_info else_tinfo
5912 = get_token_indent_info (c_parser_peek_token (parser));
5913 c_parser_consume_token (parser);
5914 if (warn_duplicated_cond)
5916 if (c_parser_next_token_is_keyword (parser, RID_IF)
5917 && chain == NULL)
5919 /* We've got "if (COND) else if (COND2)". Start the
5920 condition chain and add COND as the first element. */
5921 chain = new vec<tree> ();
5922 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5923 chain->safe_push (cond);
5925 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5927 /* This is if-else without subsequent if. Zap the condition
5928 chain; we would have already warned at this point. */
5929 delete chain;
5930 chain = NULL;
5933 second_body = c_parser_else_body (parser, else_tinfo, chain);
5934 /* Set IF_P to true to indicate that this if statement has an
5935 else clause. This may trigger the Wparentheses warning
5936 below when we get back up to the parent if statement. */
5937 if (if_p != NULL)
5938 *if_p = true;
5940 else
5942 second_body = NULL_TREE;
5944 /* Diagnose an ambiguous else if if-then-else is nested inside
5945 if-then. */
5946 if (nested_if)
5947 warning_at (loc, OPT_Wdangling_else,
5948 "suggest explicit braces to avoid ambiguous %<else%>");
5950 if (warn_duplicated_cond)
5952 /* This if statement does not have an else clause. We don't
5953 need the condition chain anymore. */
5954 delete chain;
5955 chain = NULL;
5958 c_finish_if_stmt (loc, cond, first_body, second_body);
5959 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5961 /* If the if statement contains array notations, then we expand them. */
5962 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5963 if_stmt = fix_conditional_array_notations (if_stmt);
5964 add_stmt (if_stmt);
5965 c_parser_maybe_reclassify_token (parser);
5968 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5970 switch-statement:
5971 switch (expression) statement
5974 static void
5975 c_parser_switch_statement (c_parser *parser, bool *if_p)
5977 struct c_expr ce;
5978 tree block, expr, body, save_break;
5979 location_t switch_loc = c_parser_peek_token (parser)->location;
5980 location_t switch_cond_loc;
5981 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5982 c_parser_consume_token (parser);
5983 block = c_begin_compound_stmt (flag_isoc99);
5984 bool explicit_cast_p = false;
5985 matching_parens parens;
5986 if (parens.require_open (parser))
5988 switch_cond_loc = c_parser_peek_token (parser)->location;
5989 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5990 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5991 explicit_cast_p = true;
5992 ce = c_parser_expression (parser);
5993 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5994 expr = ce.value;
5995 /* ??? expr has no valid location? */
5996 if (check_no_cilk (expr,
5997 "Cilk array notation cannot be used as a condition for switch statement",
5998 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5999 switch_cond_loc))
6000 expr = error_mark_node;
6001 parens.skip_until_found_close (parser);
6003 else
6005 switch_cond_loc = UNKNOWN_LOCATION;
6006 expr = error_mark_node;
6007 ce.original_type = error_mark_node;
6009 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
6010 save_break = c_break_label;
6011 c_break_label = NULL_TREE;
6012 location_t loc_after_labels;
6013 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
6014 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6015 location_t next_loc = c_parser_peek_token (parser)->location;
6016 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
6017 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
6018 RID_SWITCH);
6019 c_finish_case (body, ce.original_type);
6020 if (c_break_label)
6022 location_t here = c_parser_peek_token (parser)->location;
6023 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
6024 SET_EXPR_LOCATION (t, here);
6025 add_stmt (t);
6027 c_break_label = save_break;
6028 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
6029 c_parser_maybe_reclassify_token (parser);
6032 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6034 while-statement:
6035 while (expression) statement
6037 IF_P is used to track whether there's a (possibly labeled) if statement
6038 which is not enclosed in braces and has an else clause. This is used to
6039 implement -Wparentheses. */
6041 static void
6042 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
6044 tree block, cond, body, save_break, save_cont;
6045 location_t loc;
6046 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
6047 token_indent_info while_tinfo
6048 = get_token_indent_info (c_parser_peek_token (parser));
6049 c_parser_consume_token (parser);
6050 block = c_begin_compound_stmt (flag_isoc99);
6051 loc = c_parser_peek_token (parser)->location;
6052 cond = c_parser_paren_condition (parser);
6053 if (check_no_cilk (cond,
6054 "Cilk array notation cannot be used as a condition for while statement",
6055 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
6056 cond = error_mark_node;
6057 if (ivdep && cond != error_mark_node)
6058 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6059 build_int_cst (integer_type_node,
6060 annot_expr_ivdep_kind));
6061 save_break = c_break_label;
6062 c_break_label = NULL_TREE;
6063 save_cont = c_cont_label;
6064 c_cont_label = NULL_TREE;
6066 token_indent_info body_tinfo
6067 = get_token_indent_info (c_parser_peek_token (parser));
6069 location_t loc_after_labels;
6070 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6071 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6072 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
6073 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6074 c_parser_maybe_reclassify_token (parser);
6076 token_indent_info next_tinfo
6077 = get_token_indent_info (c_parser_peek_token (parser));
6078 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
6080 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6081 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6082 while_tinfo.location, RID_WHILE);
6084 c_break_label = save_break;
6085 c_cont_label = save_cont;
6088 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6090 do-statement:
6091 do statement while ( expression ) ;
6094 static void
6095 c_parser_do_statement (c_parser *parser, bool ivdep)
6097 tree block, cond, body, save_break, save_cont, new_break, new_cont;
6098 location_t loc;
6099 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
6100 c_parser_consume_token (parser);
6101 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6102 warning_at (c_parser_peek_token (parser)->location,
6103 OPT_Wempty_body,
6104 "suggest braces around empty body in %<do%> statement");
6105 block = c_begin_compound_stmt (flag_isoc99);
6106 loc = c_parser_peek_token (parser)->location;
6107 save_break = c_break_label;
6108 c_break_label = NULL_TREE;
6109 save_cont = c_cont_label;
6110 c_cont_label = NULL_TREE;
6111 body = c_parser_c99_block_statement (parser, NULL);
6112 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
6113 new_break = c_break_label;
6114 c_break_label = save_break;
6115 new_cont = c_cont_label;
6116 c_cont_label = save_cont;
6117 cond = c_parser_paren_condition (parser);
6118 if (check_no_cilk (cond,
6119 "Cilk array notation cannot be used as a condition for a do-while statement",
6120 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
6121 cond = error_mark_node;
6122 if (ivdep && cond != error_mark_node)
6123 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6124 build_int_cst (integer_type_node,
6125 annot_expr_ivdep_kind));
6126 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6127 c_parser_skip_to_end_of_block_or_statement (parser);
6128 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
6129 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6132 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6134 for-statement:
6135 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6136 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6138 The form with a declaration is new in C99.
6140 ??? In accordance with the old parser, the declaration may be a
6141 nested function, which is then rejected in check_for_loop_decls,
6142 but does it make any sense for this to be included in the grammar?
6143 Note in particular that the nested function does not include a
6144 trailing ';', whereas the "declaration" production includes one.
6145 Also, can we reject bad declarations earlier and cheaper than
6146 check_for_loop_decls?
6148 In Objective-C, there are two additional variants:
6150 foreach-statement:
6151 for ( expression in expresssion ) statement
6152 for ( declaration in expression ) statement
6154 This is inconsistent with C, because the second variant is allowed
6155 even if c99 is not enabled.
6157 The rest of the comment documents these Objective-C foreach-statement.
6159 Here is the canonical example of the first variant:
6160 for (object in array) { do something with object }
6161 we call the first expression ("object") the "object_expression" and
6162 the second expression ("array") the "collection_expression".
6163 object_expression must be an lvalue of type "id" (a generic Objective-C
6164 object) because the loop works by assigning to object_expression the
6165 various objects from the collection_expression. collection_expression
6166 must evaluate to something of type "id" which responds to the method
6167 countByEnumeratingWithState:objects:count:.
6169 The canonical example of the second variant is:
6170 for (id object in array) { do something with object }
6171 which is completely equivalent to
6173 id object;
6174 for (object in array) { do something with object }
6176 Note that initizializing 'object' in some way (eg, "for ((object =
6177 xxx) in array) { do something with object }") is possibly
6178 technically valid, but completely pointless as 'object' will be
6179 assigned to something else as soon as the loop starts. We should
6180 most likely reject it (TODO).
6182 The beginning of the Objective-C foreach-statement looks exactly
6183 like the beginning of the for-statement, and we can tell it is a
6184 foreach-statement only because the initial declaration or
6185 expression is terminated by 'in' instead of ';'.
6187 IF_P is used to track whether there's a (possibly labeled) if statement
6188 which is not enclosed in braces and has an else clause. This is used to
6189 implement -Wparentheses. */
6191 static void
6192 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
6194 tree block, cond, incr, save_break, save_cont, body;
6195 /* The following are only used when parsing an ObjC foreach statement. */
6196 tree object_expression;
6197 /* Silence the bogus uninitialized warning. */
6198 tree collection_expression = NULL;
6199 location_t loc = c_parser_peek_token (parser)->location;
6200 location_t for_loc = c_parser_peek_token (parser)->location;
6201 bool is_foreach_statement = false;
6202 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6203 token_indent_info for_tinfo
6204 = get_token_indent_info (c_parser_peek_token (parser));
6205 c_parser_consume_token (parser);
6206 /* Open a compound statement in Objective-C as well, just in case this is
6207 as foreach expression. */
6208 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6209 cond = error_mark_node;
6210 incr = error_mark_node;
6211 matching_parens parens;
6212 if (parens.require_open (parser))
6214 /* Parse the initialization declaration or expression. */
6215 object_expression = error_mark_node;
6216 parser->objc_could_be_foreach_context = c_dialect_objc ();
6217 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6219 parser->objc_could_be_foreach_context = false;
6220 c_parser_consume_token (parser);
6221 c_finish_expr_stmt (loc, NULL_TREE);
6223 else if (c_parser_next_tokens_start_declaration (parser))
6225 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6226 &object_expression, vNULL);
6227 parser->objc_could_be_foreach_context = false;
6229 if (c_parser_next_token_is_keyword (parser, RID_IN))
6231 c_parser_consume_token (parser);
6232 is_foreach_statement = true;
6233 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6234 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6236 else
6237 check_for_loop_decls (for_loc, flag_isoc99);
6239 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6241 /* __extension__ can start a declaration, but is also an
6242 unary operator that can start an expression. Consume all
6243 but the last of a possible series of __extension__ to
6244 determine which. */
6245 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6246 && (c_parser_peek_2nd_token (parser)->keyword
6247 == RID_EXTENSION))
6248 c_parser_consume_token (parser);
6249 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6251 int ext;
6252 ext = disable_extension_diagnostics ();
6253 c_parser_consume_token (parser);
6254 c_parser_declaration_or_fndef (parser, true, true, true, true,
6255 true, &object_expression, vNULL);
6256 parser->objc_could_be_foreach_context = false;
6258 restore_extension_diagnostics (ext);
6259 if (c_parser_next_token_is_keyword (parser, RID_IN))
6261 c_parser_consume_token (parser);
6262 is_foreach_statement = true;
6263 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6264 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6266 else
6267 check_for_loop_decls (for_loc, flag_isoc99);
6269 else
6270 goto init_expr;
6272 else
6274 init_expr:
6276 struct c_expr ce;
6277 tree init_expression;
6278 ce = c_parser_expression (parser);
6279 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
6280 level statement", but it works just fine, so allow it. */
6281 init_expression = ce.value;
6282 parser->objc_could_be_foreach_context = false;
6283 if (c_parser_next_token_is_keyword (parser, RID_IN))
6285 c_parser_consume_token (parser);
6286 is_foreach_statement = true;
6287 if (! lvalue_p (init_expression))
6288 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6289 object_expression = c_fully_fold (init_expression, false, NULL);
6291 else
6293 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6294 init_expression = ce.value;
6295 c_finish_expr_stmt (loc, init_expression);
6296 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6300 /* Parse the loop condition. In the case of a foreach
6301 statement, there is no loop condition. */
6302 gcc_assert (!parser->objc_could_be_foreach_context);
6303 if (!is_foreach_statement)
6305 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6307 if (ivdep)
6309 c_parser_error (parser, "missing loop condition in loop with "
6310 "%<GCC ivdep%> pragma");
6311 cond = error_mark_node;
6313 else
6315 c_parser_consume_token (parser);
6316 cond = NULL_TREE;
6319 else
6321 cond = c_parser_condition (parser);
6322 if (check_no_cilk (cond,
6323 "Cilk array notation cannot be used in a condition for a for-loop",
6324 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
6325 cond = error_mark_node;
6326 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6327 "expected %<;%>");
6329 if (ivdep && cond != error_mark_node)
6330 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6331 build_int_cst (integer_type_node,
6332 annot_expr_ivdep_kind));
6334 /* Parse the increment expression (the third expression in a
6335 for-statement). In the case of a foreach-statement, this is
6336 the expression that follows the 'in'. */
6337 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6339 if (is_foreach_statement)
6341 c_parser_error (parser, "missing collection in fast enumeration");
6342 collection_expression = error_mark_node;
6344 else
6345 incr = c_process_expr_stmt (loc, NULL_TREE);
6347 else
6349 if (is_foreach_statement)
6350 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6351 false, NULL);
6352 else
6354 struct c_expr ce = c_parser_expression (parser);
6355 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6356 incr = c_process_expr_stmt (loc, ce.value);
6359 parens.skip_until_found_close (parser);
6361 save_break = c_break_label;
6362 c_break_label = NULL_TREE;
6363 save_cont = c_cont_label;
6364 c_cont_label = NULL_TREE;
6366 token_indent_info body_tinfo
6367 = get_token_indent_info (c_parser_peek_token (parser));
6369 location_t loc_after_labels;
6370 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6371 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6373 if (is_foreach_statement)
6374 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6375 else
6376 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6377 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6378 c_parser_maybe_reclassify_token (parser);
6380 token_indent_info next_tinfo
6381 = get_token_indent_info (c_parser_peek_token (parser));
6382 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6384 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6385 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6386 for_tinfo.location, RID_FOR);
6388 c_break_label = save_break;
6389 c_cont_label = save_cont;
6392 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6393 statement with inputs, outputs, clobbers, and volatile tag
6394 allowed.
6396 asm-statement:
6397 asm type-qualifier[opt] ( asm-argument ) ;
6398 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6400 asm-argument:
6401 asm-string-literal
6402 asm-string-literal : asm-operands[opt]
6403 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6404 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6406 asm-goto-argument:
6407 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6408 : asm-goto-operands
6410 Qualifiers other than volatile are accepted in the syntax but
6411 warned for. */
6413 static tree
6414 c_parser_asm_statement (c_parser *parser)
6416 tree quals, str, outputs, inputs, clobbers, labels, ret;
6417 bool simple, is_goto;
6418 location_t asm_loc = c_parser_peek_token (parser)->location;
6419 int section, nsections;
6421 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6422 c_parser_consume_token (parser);
6423 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6425 quals = c_parser_peek_token (parser)->value;
6426 c_parser_consume_token (parser);
6428 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6429 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6431 warning_at (c_parser_peek_token (parser)->location,
6433 "%E qualifier ignored on asm",
6434 c_parser_peek_token (parser)->value);
6435 quals = NULL_TREE;
6436 c_parser_consume_token (parser);
6438 else
6439 quals = NULL_TREE;
6441 is_goto = false;
6442 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6444 c_parser_consume_token (parser);
6445 is_goto = true;
6448 /* ??? Follow the C++ parser rather than using the
6449 lex_untranslated_string kludge. */
6450 parser->lex_untranslated_string = true;
6451 ret = NULL;
6453 matching_parens parens;
6454 if (!parens.require_open (parser))
6455 goto error;
6457 str = c_parser_asm_string_literal (parser);
6458 if (str == NULL_TREE)
6459 goto error_close_paren;
6461 simple = true;
6462 outputs = NULL_TREE;
6463 inputs = NULL_TREE;
6464 clobbers = NULL_TREE;
6465 labels = NULL_TREE;
6467 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6468 goto done_asm;
6470 /* Parse each colon-delimited section of operands. */
6471 nsections = 3 + is_goto;
6472 for (section = 0; section < nsections; ++section)
6474 if (!c_parser_require (parser, CPP_COLON,
6475 is_goto
6476 ? G_("expected %<:%>")
6477 : G_("expected %<:%> or %<)%>"),
6478 UNKNOWN_LOCATION, is_goto))
6479 goto error_close_paren;
6481 /* Once past any colon, we're no longer a simple asm. */
6482 simple = false;
6484 if ((!c_parser_next_token_is (parser, CPP_COLON)
6485 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6486 || section == 3)
6487 switch (section)
6489 case 0:
6490 /* For asm goto, we don't allow output operands, but reserve
6491 the slot for a future extension that does allow them. */
6492 if (!is_goto)
6493 outputs = c_parser_asm_operands (parser);
6494 break;
6495 case 1:
6496 inputs = c_parser_asm_operands (parser);
6497 break;
6498 case 2:
6499 clobbers = c_parser_asm_clobbers (parser);
6500 break;
6501 case 3:
6502 labels = c_parser_asm_goto_operands (parser);
6503 break;
6504 default:
6505 gcc_unreachable ();
6508 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6509 goto done_asm;
6512 done_asm:
6513 if (!parens.require_close (parser))
6515 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6516 goto error;
6519 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6520 c_parser_skip_to_end_of_block_or_statement (parser);
6522 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6523 clobbers, labels, simple));
6525 error:
6526 parser->lex_untranslated_string = false;
6527 return ret;
6529 error_close_paren:
6530 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6531 goto error;
6534 /* Parse asm operands, a GNU extension.
6536 asm-operands:
6537 asm-operand
6538 asm-operands , asm-operand
6540 asm-operand:
6541 asm-string-literal ( expression )
6542 [ identifier ] asm-string-literal ( expression )
6545 static tree
6546 c_parser_asm_operands (c_parser *parser)
6548 tree list = NULL_TREE;
6549 while (true)
6551 tree name, str;
6552 struct c_expr expr;
6553 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6555 c_parser_consume_token (parser);
6556 if (c_parser_next_token_is (parser, CPP_NAME))
6558 tree id = c_parser_peek_token (parser)->value;
6559 c_parser_consume_token (parser);
6560 name = build_string (IDENTIFIER_LENGTH (id),
6561 IDENTIFIER_POINTER (id));
6563 else
6565 c_parser_error (parser, "expected identifier");
6566 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6567 return NULL_TREE;
6569 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6570 "expected %<]%>");
6572 else
6573 name = NULL_TREE;
6574 str = c_parser_asm_string_literal (parser);
6575 if (str == NULL_TREE)
6576 return NULL_TREE;
6577 parser->lex_untranslated_string = false;
6578 matching_parens parens;
6579 if (!parens.require_open (parser))
6581 parser->lex_untranslated_string = true;
6582 return NULL_TREE;
6584 expr = c_parser_expression (parser);
6585 mark_exp_read (expr.value);
6586 parser->lex_untranslated_string = true;
6587 if (!parens.require_close (parser))
6589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6590 return NULL_TREE;
6592 list = chainon (list, build_tree_list (build_tree_list (name, str),
6593 expr.value));
6594 if (c_parser_next_token_is (parser, CPP_COMMA))
6595 c_parser_consume_token (parser);
6596 else
6597 break;
6599 return list;
6602 /* Parse asm clobbers, a GNU extension.
6604 asm-clobbers:
6605 asm-string-literal
6606 asm-clobbers , asm-string-literal
6609 static tree
6610 c_parser_asm_clobbers (c_parser *parser)
6612 tree list = NULL_TREE;
6613 while (true)
6615 tree str = c_parser_asm_string_literal (parser);
6616 if (str)
6617 list = tree_cons (NULL_TREE, str, list);
6618 else
6619 return NULL_TREE;
6620 if (c_parser_next_token_is (parser, CPP_COMMA))
6621 c_parser_consume_token (parser);
6622 else
6623 break;
6625 return list;
6628 /* Parse asm goto labels, a GNU extension.
6630 asm-goto-operands:
6631 identifier
6632 asm-goto-operands , identifier
6635 static tree
6636 c_parser_asm_goto_operands (c_parser *parser)
6638 tree list = NULL_TREE;
6639 while (true)
6641 tree name, label;
6643 if (c_parser_next_token_is (parser, CPP_NAME))
6645 c_token *tok = c_parser_peek_token (parser);
6646 name = tok->value;
6647 label = lookup_label_for_goto (tok->location, name);
6648 c_parser_consume_token (parser);
6649 TREE_USED (label) = 1;
6651 else
6653 c_parser_error (parser, "expected identifier");
6654 return NULL_TREE;
6657 name = build_string (IDENTIFIER_LENGTH (name),
6658 IDENTIFIER_POINTER (name));
6659 list = tree_cons (name, label, list);
6660 if (c_parser_next_token_is (parser, CPP_COMMA))
6661 c_parser_consume_token (parser);
6662 else
6663 return nreverse (list);
6667 /* Parse an expression other than a compound expression; that is, an
6668 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6669 AFTER is not NULL then it is an Objective-C message expression which
6670 is the primary-expression starting the expression as an initializer.
6672 assignment-expression:
6673 conditional-expression
6674 unary-expression assignment-operator assignment-expression
6676 assignment-operator: one of
6677 = *= /= %= += -= <<= >>= &= ^= |=
6679 In GNU C we accept any conditional expression on the LHS and
6680 diagnose the invalid lvalue rather than producing a syntax
6681 error. */
6683 static struct c_expr
6684 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6685 tree omp_atomic_lhs)
6687 struct c_expr lhs, rhs, ret;
6688 enum tree_code code;
6689 location_t op_location, exp_location;
6690 gcc_assert (!after || c_dialect_objc ());
6691 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6692 op_location = c_parser_peek_token (parser)->location;
6693 switch (c_parser_peek_token (parser)->type)
6695 case CPP_EQ:
6696 code = NOP_EXPR;
6697 break;
6698 case CPP_MULT_EQ:
6699 code = MULT_EXPR;
6700 break;
6701 case CPP_DIV_EQ:
6702 code = TRUNC_DIV_EXPR;
6703 break;
6704 case CPP_MOD_EQ:
6705 code = TRUNC_MOD_EXPR;
6706 break;
6707 case CPP_PLUS_EQ:
6708 code = PLUS_EXPR;
6709 break;
6710 case CPP_MINUS_EQ:
6711 code = MINUS_EXPR;
6712 break;
6713 case CPP_LSHIFT_EQ:
6714 code = LSHIFT_EXPR;
6715 break;
6716 case CPP_RSHIFT_EQ:
6717 code = RSHIFT_EXPR;
6718 break;
6719 case CPP_AND_EQ:
6720 code = BIT_AND_EXPR;
6721 break;
6722 case CPP_XOR_EQ:
6723 code = BIT_XOR_EXPR;
6724 break;
6725 case CPP_OR_EQ:
6726 code = BIT_IOR_EXPR;
6727 break;
6728 default:
6729 return lhs;
6731 c_parser_consume_token (parser);
6732 exp_location = c_parser_peek_token (parser)->location;
6733 rhs = c_parser_expr_no_commas (parser, NULL);
6734 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6736 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6737 code, exp_location, rhs.value,
6738 rhs.original_type);
6739 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6740 if (code == NOP_EXPR)
6741 ret.original_code = MODIFY_EXPR;
6742 else
6744 TREE_NO_WARNING (ret.value) = 1;
6745 ret.original_code = ERROR_MARK;
6747 ret.original_type = NULL;
6748 return ret;
6751 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6752 AFTER is not NULL then it is an Objective-C message expression which is
6753 the primary-expression starting the expression as an initializer.
6755 conditional-expression:
6756 logical-OR-expression
6757 logical-OR-expression ? expression : conditional-expression
6759 GNU extensions:
6761 conditional-expression:
6762 logical-OR-expression ? : conditional-expression
6765 static struct c_expr
6766 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6767 tree omp_atomic_lhs)
6769 struct c_expr cond, exp1, exp2, ret;
6770 location_t start, cond_loc, colon_loc;
6772 gcc_assert (!after || c_dialect_objc ());
6774 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6776 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6777 return cond;
6778 if (cond.value != error_mark_node)
6779 start = cond.get_start ();
6780 else
6781 start = UNKNOWN_LOCATION;
6782 cond_loc = c_parser_peek_token (parser)->location;
6783 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6784 c_parser_consume_token (parser);
6785 if (c_parser_next_token_is (parser, CPP_COLON))
6787 tree eptype = NULL_TREE;
6789 location_t middle_loc = c_parser_peek_token (parser)->location;
6790 pedwarn (middle_loc, OPT_Wpedantic,
6791 "ISO C forbids omitting the middle term of a ?: expression");
6792 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6794 eptype = TREE_TYPE (cond.value);
6795 cond.value = TREE_OPERAND (cond.value, 0);
6797 tree e = cond.value;
6798 while (TREE_CODE (e) == COMPOUND_EXPR)
6799 e = TREE_OPERAND (e, 1);
6800 warn_for_omitted_condop (middle_loc, e);
6801 /* Make sure first operand is calculated only once. */
6802 exp1.value = save_expr (default_conversion (cond.value));
6803 if (eptype)
6804 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6805 exp1.original_type = NULL;
6806 exp1.src_range = cond.src_range;
6807 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6808 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6810 else
6812 cond.value
6813 = c_objc_common_truthvalue_conversion
6814 (cond_loc, default_conversion (cond.value));
6815 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6816 exp1 = c_parser_expression_conv (parser);
6817 mark_exp_read (exp1.value);
6818 c_inhibit_evaluation_warnings +=
6819 ((cond.value == truthvalue_true_node)
6820 - (cond.value == truthvalue_false_node));
6823 colon_loc = c_parser_peek_token (parser)->location;
6824 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6826 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6827 ret.value = error_mark_node;
6828 ret.original_code = ERROR_MARK;
6829 ret.original_type = NULL;
6830 return ret;
6833 location_t exp2_loc = c_parser_peek_token (parser)->location;
6834 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6835 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6837 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6838 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6839 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6840 ret.value = build_conditional_expr (colon_loc, cond.value,
6841 cond.original_code == C_MAYBE_CONST_EXPR,
6842 exp1.value, exp1.original_type, loc1,
6843 exp2.value, exp2.original_type, loc2);
6844 ret.original_code = ERROR_MARK;
6845 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6846 ret.original_type = NULL;
6847 else
6849 tree t1, t2;
6851 /* If both sides are enum type, the default conversion will have
6852 made the type of the result be an integer type. We want to
6853 remember the enum types we started with. */
6854 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6855 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6856 ret.original_type = ((t1 != error_mark_node
6857 && t2 != error_mark_node
6858 && (TYPE_MAIN_VARIANT (t1)
6859 == TYPE_MAIN_VARIANT (t2)))
6860 ? t1
6861 : NULL);
6863 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6864 return ret;
6867 /* Parse a binary expression; that is, a logical-OR-expression (C90
6868 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6869 NULL then it is an Objective-C message expression which is the
6870 primary-expression starting the expression as an initializer.
6872 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6873 when it should be the unfolded lhs. In a valid OpenMP source,
6874 one of the operands of the toplevel binary expression must be equal
6875 to it. In that case, just return a build2 created binary operation
6876 rather than result of parser_build_binary_op.
6878 multiplicative-expression:
6879 cast-expression
6880 multiplicative-expression * cast-expression
6881 multiplicative-expression / cast-expression
6882 multiplicative-expression % cast-expression
6884 additive-expression:
6885 multiplicative-expression
6886 additive-expression + multiplicative-expression
6887 additive-expression - multiplicative-expression
6889 shift-expression:
6890 additive-expression
6891 shift-expression << additive-expression
6892 shift-expression >> additive-expression
6894 relational-expression:
6895 shift-expression
6896 relational-expression < shift-expression
6897 relational-expression > shift-expression
6898 relational-expression <= shift-expression
6899 relational-expression >= shift-expression
6901 equality-expression:
6902 relational-expression
6903 equality-expression == relational-expression
6904 equality-expression != relational-expression
6906 AND-expression:
6907 equality-expression
6908 AND-expression & equality-expression
6910 exclusive-OR-expression:
6911 AND-expression
6912 exclusive-OR-expression ^ AND-expression
6914 inclusive-OR-expression:
6915 exclusive-OR-expression
6916 inclusive-OR-expression | exclusive-OR-expression
6918 logical-AND-expression:
6919 inclusive-OR-expression
6920 logical-AND-expression && inclusive-OR-expression
6922 logical-OR-expression:
6923 logical-AND-expression
6924 logical-OR-expression || logical-AND-expression
6927 static struct c_expr
6928 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6929 tree omp_atomic_lhs)
6931 /* A binary expression is parsed using operator-precedence parsing,
6932 with the operands being cast expressions. All the binary
6933 operators are left-associative. Thus a binary expression is of
6934 form:
6936 E0 op1 E1 op2 E2 ...
6938 which we represent on a stack. On the stack, the precedence
6939 levels are strictly increasing. When a new operator is
6940 encountered of higher precedence than that at the top of the
6941 stack, it is pushed; its LHS is the top expression, and its RHS
6942 is everything parsed until it is popped. When a new operator is
6943 encountered with precedence less than or equal to that at the top
6944 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6945 by the result of the operation until the operator at the top of
6946 the stack has lower precedence than the new operator or there is
6947 only one element on the stack; then the top expression is the LHS
6948 of the new operator. In the case of logical AND and OR
6949 expressions, we also need to adjust c_inhibit_evaluation_warnings
6950 as appropriate when the operators are pushed and popped. */
6952 struct {
6953 /* The expression at this stack level. */
6954 struct c_expr expr;
6955 /* The precedence of the operator on its left, PREC_NONE at the
6956 bottom of the stack. */
6957 enum c_parser_prec prec;
6958 /* The operation on its left. */
6959 enum tree_code op;
6960 /* The source location of this operation. */
6961 location_t loc;
6962 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6963 tree sizeof_arg;
6964 } stack[NUM_PRECS];
6965 int sp;
6966 /* Location of the binary operator. */
6967 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6968 #define POP \
6969 do { \
6970 switch (stack[sp].op) \
6972 case TRUTH_ANDIF_EXPR: \
6973 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6974 == truthvalue_false_node); \
6975 break; \
6976 case TRUTH_ORIF_EXPR: \
6977 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6978 == truthvalue_true_node); \
6979 break; \
6980 case TRUNC_DIV_EXPR: \
6981 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6982 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6984 tree type0 = stack[sp - 1].sizeof_arg; \
6985 tree type1 = stack[sp].sizeof_arg; \
6986 tree first_arg = type0; \
6987 if (!TYPE_P (type0)) \
6988 type0 = TREE_TYPE (type0); \
6989 if (!TYPE_P (type1)) \
6990 type1 = TREE_TYPE (type1); \
6991 if (POINTER_TYPE_P (type0) \
6992 && comptypes (TREE_TYPE (type0), type1) \
6993 && !(TREE_CODE (first_arg) == PARM_DECL \
6994 && C_ARRAY_PARAMETER (first_arg) \
6995 && warn_sizeof_array_argument)) \
6996 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6997 "division %<sizeof (%T) / sizeof (%T)%> does " \
6998 "not compute the number of array elements", \
6999 type0, type1)) \
7000 if (DECL_P (first_arg)) \
7001 inform (DECL_SOURCE_LOCATION (first_arg), \
7002 "first %<sizeof%> operand was declared here"); \
7004 break; \
7005 default: \
7006 break; \
7008 stack[sp - 1].expr \
7009 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
7010 stack[sp - 1].expr, true, true); \
7011 stack[sp].expr \
7012 = convert_lvalue_to_rvalue (stack[sp].loc, \
7013 stack[sp].expr, true, true); \
7014 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
7015 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
7016 && ((1 << stack[sp].prec) \
7017 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
7018 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
7019 && stack[sp].op != TRUNC_MOD_EXPR \
7020 && stack[0].expr.value != error_mark_node \
7021 && stack[1].expr.value != error_mark_node \
7022 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
7023 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
7024 stack[0].expr.value \
7025 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
7026 stack[0].expr.value, stack[1].expr.value); \
7027 else \
7028 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
7029 stack[sp].op, \
7030 stack[sp - 1].expr, \
7031 stack[sp].expr); \
7032 sp--; \
7033 } while (0)
7034 gcc_assert (!after || c_dialect_objc ());
7035 stack[0].loc = c_parser_peek_token (parser)->location;
7036 stack[0].expr = c_parser_cast_expression (parser, after);
7037 stack[0].prec = PREC_NONE;
7038 stack[0].sizeof_arg = c_last_sizeof_arg;
7039 sp = 0;
7040 while (true)
7042 enum c_parser_prec oprec;
7043 enum tree_code ocode;
7044 source_range src_range;
7045 if (parser->error)
7046 goto out;
7047 switch (c_parser_peek_token (parser)->type)
7049 case CPP_MULT:
7050 oprec = PREC_MULT;
7051 ocode = MULT_EXPR;
7052 break;
7053 case CPP_DIV:
7054 oprec = PREC_MULT;
7055 ocode = TRUNC_DIV_EXPR;
7056 break;
7057 case CPP_MOD:
7058 oprec = PREC_MULT;
7059 ocode = TRUNC_MOD_EXPR;
7060 break;
7061 case CPP_PLUS:
7062 oprec = PREC_ADD;
7063 ocode = PLUS_EXPR;
7064 break;
7065 case CPP_MINUS:
7066 oprec = PREC_ADD;
7067 ocode = MINUS_EXPR;
7068 break;
7069 case CPP_LSHIFT:
7070 oprec = PREC_SHIFT;
7071 ocode = LSHIFT_EXPR;
7072 break;
7073 case CPP_RSHIFT:
7074 oprec = PREC_SHIFT;
7075 ocode = RSHIFT_EXPR;
7076 break;
7077 case CPP_LESS:
7078 oprec = PREC_REL;
7079 ocode = LT_EXPR;
7080 break;
7081 case CPP_GREATER:
7082 oprec = PREC_REL;
7083 ocode = GT_EXPR;
7084 break;
7085 case CPP_LESS_EQ:
7086 oprec = PREC_REL;
7087 ocode = LE_EXPR;
7088 break;
7089 case CPP_GREATER_EQ:
7090 oprec = PREC_REL;
7091 ocode = GE_EXPR;
7092 break;
7093 case CPP_EQ_EQ:
7094 oprec = PREC_EQ;
7095 ocode = EQ_EXPR;
7096 break;
7097 case CPP_NOT_EQ:
7098 oprec = PREC_EQ;
7099 ocode = NE_EXPR;
7100 break;
7101 case CPP_AND:
7102 oprec = PREC_BITAND;
7103 ocode = BIT_AND_EXPR;
7104 break;
7105 case CPP_XOR:
7106 oprec = PREC_BITXOR;
7107 ocode = BIT_XOR_EXPR;
7108 break;
7109 case CPP_OR:
7110 oprec = PREC_BITOR;
7111 ocode = BIT_IOR_EXPR;
7112 break;
7113 case CPP_AND_AND:
7114 oprec = PREC_LOGAND;
7115 ocode = TRUTH_ANDIF_EXPR;
7116 break;
7117 case CPP_OR_OR:
7118 oprec = PREC_LOGOR;
7119 ocode = TRUTH_ORIF_EXPR;
7120 break;
7121 default:
7122 /* Not a binary operator, so end of the binary
7123 expression. */
7124 goto out;
7126 binary_loc = c_parser_peek_token (parser)->location;
7127 while (oprec <= stack[sp].prec)
7128 POP;
7129 c_parser_consume_token (parser);
7130 switch (ocode)
7132 case TRUTH_ANDIF_EXPR:
7133 src_range = stack[sp].expr.src_range;
7134 stack[sp].expr
7135 = convert_lvalue_to_rvalue (stack[sp].loc,
7136 stack[sp].expr, true, true);
7137 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7138 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7139 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7140 == truthvalue_false_node);
7141 set_c_expr_source_range (&stack[sp].expr, src_range);
7142 break;
7143 case TRUTH_ORIF_EXPR:
7144 src_range = stack[sp].expr.src_range;
7145 stack[sp].expr
7146 = convert_lvalue_to_rvalue (stack[sp].loc,
7147 stack[sp].expr, true, true);
7148 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7149 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7150 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7151 == truthvalue_true_node);
7152 set_c_expr_source_range (&stack[sp].expr, src_range);
7153 break;
7154 default:
7155 break;
7157 sp++;
7158 stack[sp].loc = binary_loc;
7159 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7160 stack[sp].prec = oprec;
7161 stack[sp].op = ocode;
7162 stack[sp].sizeof_arg = c_last_sizeof_arg;
7164 out:
7165 while (sp > 0)
7166 POP;
7167 return stack[0].expr;
7168 #undef POP
7171 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7172 is not NULL then it is an Objective-C message expression which is the
7173 primary-expression starting the expression as an initializer.
7175 cast-expression:
7176 unary-expression
7177 ( type-name ) unary-expression
7180 static struct c_expr
7181 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7183 location_t cast_loc = c_parser_peek_token (parser)->location;
7184 gcc_assert (!after || c_dialect_objc ());
7185 if (after)
7186 return c_parser_postfix_expression_after_primary (parser,
7187 cast_loc, *after);
7188 /* If the expression begins with a parenthesized type name, it may
7189 be either a cast or a compound literal; we need to see whether
7190 the next character is '{' to tell the difference. If not, it is
7191 an unary expression. Full detection of unknown typenames here
7192 would require a 3-token lookahead. */
7193 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7194 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7196 struct c_type_name *type_name;
7197 struct c_expr ret;
7198 struct c_expr expr;
7199 matching_parens parens;
7200 parens.consume_open (parser);
7201 type_name = c_parser_type_name (parser);
7202 parens.skip_until_found_close (parser);
7203 if (type_name == NULL)
7205 ret.value = error_mark_node;
7206 ret.original_code = ERROR_MARK;
7207 ret.original_type = NULL;
7208 return ret;
7211 /* Save casted types in the function's used types hash table. */
7212 used_types_insert (type_name->specs->type);
7214 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7215 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7216 cast_loc);
7218 location_t expr_loc = c_parser_peek_token (parser)->location;
7219 expr = c_parser_cast_expression (parser, NULL);
7220 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7222 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7223 if (ret.value && expr.value)
7224 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7225 ret.original_code = ERROR_MARK;
7226 ret.original_type = NULL;
7227 return ret;
7229 else
7230 return c_parser_unary_expression (parser);
7233 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7235 unary-expression:
7236 postfix-expression
7237 ++ unary-expression
7238 -- unary-expression
7239 unary-operator cast-expression
7240 sizeof unary-expression
7241 sizeof ( type-name )
7243 unary-operator: one of
7244 & * + - ~ !
7246 GNU extensions:
7248 unary-expression:
7249 __alignof__ unary-expression
7250 __alignof__ ( type-name )
7251 && identifier
7253 (C11 permits _Alignof with type names only.)
7255 unary-operator: one of
7256 __extension__ __real__ __imag__
7258 Transactional Memory:
7260 unary-expression:
7261 transaction-expression
7263 In addition, the GNU syntax treats ++ and -- as unary operators, so
7264 they may be applied to cast expressions with errors for non-lvalues
7265 given later. */
7267 static struct c_expr
7268 c_parser_unary_expression (c_parser *parser)
7270 int ext;
7271 struct c_expr ret, op;
7272 location_t op_loc = c_parser_peek_token (parser)->location;
7273 location_t exp_loc;
7274 location_t finish;
7275 ret.original_code = ERROR_MARK;
7276 ret.original_type = NULL;
7277 switch (c_parser_peek_token (parser)->type)
7279 case CPP_PLUS_PLUS:
7280 c_parser_consume_token (parser);
7281 exp_loc = c_parser_peek_token (parser)->location;
7282 op = c_parser_cast_expression (parser, NULL);
7284 /* If there is array notations in op, we expand them. */
7285 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
7286 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
7287 else
7289 op = default_function_array_read_conversion (exp_loc, op);
7290 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7292 case CPP_MINUS_MINUS:
7293 c_parser_consume_token (parser);
7294 exp_loc = c_parser_peek_token (parser)->location;
7295 op = c_parser_cast_expression (parser, NULL);
7297 /* If there is array notations in op, we expand them. */
7298 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
7299 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
7300 else
7302 op = default_function_array_read_conversion (exp_loc, op);
7303 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7305 case CPP_AND:
7306 c_parser_consume_token (parser);
7307 op = c_parser_cast_expression (parser, NULL);
7308 mark_exp_read (op.value);
7309 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7310 case CPP_MULT:
7312 c_parser_consume_token (parser);
7313 exp_loc = c_parser_peek_token (parser)->location;
7314 op = c_parser_cast_expression (parser, NULL);
7315 finish = op.get_finish ();
7316 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7317 location_t combined_loc = make_location (op_loc, op_loc, finish);
7318 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7319 ret.src_range.m_start = op_loc;
7320 ret.src_range.m_finish = finish;
7321 return ret;
7323 case CPP_PLUS:
7324 if (!c_dialect_objc () && !in_system_header_at (input_location))
7325 warning_at (op_loc,
7326 OPT_Wtraditional,
7327 "traditional C rejects the unary plus operator");
7328 c_parser_consume_token (parser);
7329 exp_loc = c_parser_peek_token (parser)->location;
7330 op = c_parser_cast_expression (parser, NULL);
7331 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7332 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7333 case CPP_MINUS:
7334 c_parser_consume_token (parser);
7335 exp_loc = c_parser_peek_token (parser)->location;
7336 op = c_parser_cast_expression (parser, NULL);
7337 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7338 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7339 case CPP_COMPL:
7340 c_parser_consume_token (parser);
7341 exp_loc = c_parser_peek_token (parser)->location;
7342 op = c_parser_cast_expression (parser, NULL);
7343 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7344 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7345 case CPP_NOT:
7346 c_parser_consume_token (parser);
7347 exp_loc = c_parser_peek_token (parser)->location;
7348 op = c_parser_cast_expression (parser, NULL);
7349 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7350 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7351 case CPP_AND_AND:
7352 /* Refer to the address of a label as a pointer. */
7353 c_parser_consume_token (parser);
7354 if (c_parser_next_token_is (parser, CPP_NAME))
7356 ret.value = finish_label_address_expr
7357 (c_parser_peek_token (parser)->value, op_loc);
7358 set_c_expr_source_range (&ret, op_loc,
7359 c_parser_peek_token (parser)->get_finish ());
7360 c_parser_consume_token (parser);
7362 else
7364 c_parser_error (parser, "expected identifier");
7365 ret.set_error ();
7367 return ret;
7368 case CPP_KEYWORD:
7369 switch (c_parser_peek_token (parser)->keyword)
7371 case RID_SIZEOF:
7372 return c_parser_sizeof_expression (parser);
7373 case RID_ALIGNOF:
7374 return c_parser_alignof_expression (parser);
7375 case RID_EXTENSION:
7376 c_parser_consume_token (parser);
7377 ext = disable_extension_diagnostics ();
7378 ret = c_parser_cast_expression (parser, NULL);
7379 restore_extension_diagnostics (ext);
7380 return ret;
7381 case RID_REALPART:
7382 c_parser_consume_token (parser);
7383 exp_loc = c_parser_peek_token (parser)->location;
7384 op = c_parser_cast_expression (parser, NULL);
7385 op = default_function_array_conversion (exp_loc, op);
7386 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7387 case RID_IMAGPART:
7388 c_parser_consume_token (parser);
7389 exp_loc = c_parser_peek_token (parser)->location;
7390 op = c_parser_cast_expression (parser, NULL);
7391 op = default_function_array_conversion (exp_loc, op);
7392 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7393 case RID_TRANSACTION_ATOMIC:
7394 case RID_TRANSACTION_RELAXED:
7395 return c_parser_transaction_expression (parser,
7396 c_parser_peek_token (parser)->keyword);
7397 default:
7398 return c_parser_postfix_expression (parser);
7400 default:
7401 return c_parser_postfix_expression (parser);
7405 /* Parse a sizeof expression. */
7407 static struct c_expr
7408 c_parser_sizeof_expression (c_parser *parser)
7410 struct c_expr expr;
7411 struct c_expr result;
7412 location_t expr_loc;
7413 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7415 location_t start;
7416 location_t finish = UNKNOWN_LOCATION;
7418 start = c_parser_peek_token (parser)->location;
7420 c_parser_consume_token (parser);
7421 c_inhibit_evaluation_warnings++;
7422 in_sizeof++;
7423 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7424 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7426 /* Either sizeof ( type-name ) or sizeof unary-expression
7427 starting with a compound literal. */
7428 struct c_type_name *type_name;
7429 matching_parens parens;
7430 parens.consume_open (parser);
7431 expr_loc = c_parser_peek_token (parser)->location;
7432 type_name = c_parser_type_name (parser);
7433 parens.skip_until_found_close (parser);
7434 finish = parser->tokens_buf[0].location;
7435 if (type_name == NULL)
7437 struct c_expr ret;
7438 c_inhibit_evaluation_warnings--;
7439 in_sizeof--;
7440 ret.value = error_mark_node;
7441 ret.original_code = ERROR_MARK;
7442 ret.original_type = NULL;
7443 return ret;
7445 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7447 expr = c_parser_postfix_expression_after_paren_type (parser,
7448 type_name,
7449 expr_loc);
7450 finish = expr.get_finish ();
7451 goto sizeof_expr;
7453 /* sizeof ( type-name ). */
7454 c_inhibit_evaluation_warnings--;
7455 in_sizeof--;
7456 result = c_expr_sizeof_type (expr_loc, type_name);
7458 else
7460 expr_loc = c_parser_peek_token (parser)->location;
7461 expr = c_parser_unary_expression (parser);
7462 finish = expr.get_finish ();
7463 sizeof_expr:
7464 c_inhibit_evaluation_warnings--;
7465 in_sizeof--;
7466 mark_exp_read (expr.value);
7467 if (TREE_CODE (expr.value) == COMPONENT_REF
7468 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7469 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7470 result = c_expr_sizeof_expr (expr_loc, expr);
7472 if (finish != UNKNOWN_LOCATION)
7473 set_c_expr_source_range (&result, start, finish);
7474 return result;
7477 /* Parse an alignof expression. */
7479 static struct c_expr
7480 c_parser_alignof_expression (c_parser *parser)
7482 struct c_expr expr;
7483 location_t start_loc = c_parser_peek_token (parser)->location;
7484 location_t end_loc;
7485 tree alignof_spelling = c_parser_peek_token (parser)->value;
7486 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7487 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7488 "_Alignof") == 0;
7489 /* A diagnostic is not required for the use of this identifier in
7490 the implementation namespace; only diagnose it for the C11
7491 spelling because of existing code using the other spellings. */
7492 if (is_c11_alignof)
7494 if (flag_isoc99)
7495 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7496 alignof_spelling);
7497 else
7498 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7499 alignof_spelling);
7501 c_parser_consume_token (parser);
7502 c_inhibit_evaluation_warnings++;
7503 in_alignof++;
7504 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7505 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7507 /* Either __alignof__ ( type-name ) or __alignof__
7508 unary-expression starting with a compound literal. */
7509 location_t loc;
7510 struct c_type_name *type_name;
7511 struct c_expr ret;
7512 matching_parens parens;
7513 parens.consume_open (parser);
7514 loc = c_parser_peek_token (parser)->location;
7515 type_name = c_parser_type_name (parser);
7516 end_loc = c_parser_peek_token (parser)->location;
7517 parens.skip_until_found_close (parser);
7518 if (type_name == NULL)
7520 struct c_expr ret;
7521 c_inhibit_evaluation_warnings--;
7522 in_alignof--;
7523 ret.value = error_mark_node;
7524 ret.original_code = ERROR_MARK;
7525 ret.original_type = NULL;
7526 return ret;
7528 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7530 expr = c_parser_postfix_expression_after_paren_type (parser,
7531 type_name,
7532 loc);
7533 goto alignof_expr;
7535 /* alignof ( type-name ). */
7536 c_inhibit_evaluation_warnings--;
7537 in_alignof--;
7538 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7539 NULL, NULL),
7540 false, is_c11_alignof, 1);
7541 ret.original_code = ERROR_MARK;
7542 ret.original_type = NULL;
7543 set_c_expr_source_range (&ret, start_loc, end_loc);
7544 return ret;
7546 else
7548 struct c_expr ret;
7549 expr = c_parser_unary_expression (parser);
7550 end_loc = expr.src_range.m_finish;
7551 alignof_expr:
7552 mark_exp_read (expr.value);
7553 c_inhibit_evaluation_warnings--;
7554 in_alignof--;
7555 if (is_c11_alignof)
7556 pedwarn (start_loc,
7557 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7558 alignof_spelling);
7559 ret.value = c_alignof_expr (start_loc, expr.value);
7560 ret.original_code = ERROR_MARK;
7561 ret.original_type = NULL;
7562 set_c_expr_source_range (&ret, start_loc, end_loc);
7563 return ret;
7567 /* Helper function to read arguments of builtins which are interfaces
7568 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7569 others. The name of the builtin is passed using BNAME parameter.
7570 Function returns true if there were no errors while parsing and
7571 stores the arguments in CEXPR_LIST. If it returns true,
7572 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7573 parenthesis. */
7574 static bool
7575 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7576 vec<c_expr_t, va_gc> **ret_cexpr_list,
7577 bool choose_expr_p,
7578 location_t *out_close_paren_loc)
7580 location_t loc = c_parser_peek_token (parser)->location;
7581 vec<c_expr_t, va_gc> *cexpr_list;
7582 c_expr_t expr;
7583 bool saved_force_folding_builtin_constant_p;
7585 *ret_cexpr_list = NULL;
7586 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7588 error_at (loc, "cannot take address of %qs", bname);
7589 return false;
7592 c_parser_consume_token (parser);
7594 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7596 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7597 c_parser_consume_token (parser);
7598 return true;
7601 saved_force_folding_builtin_constant_p
7602 = force_folding_builtin_constant_p;
7603 force_folding_builtin_constant_p |= choose_expr_p;
7604 expr = c_parser_expr_no_commas (parser, NULL);
7605 force_folding_builtin_constant_p
7606 = saved_force_folding_builtin_constant_p;
7607 vec_alloc (cexpr_list, 1);
7608 vec_safe_push (cexpr_list, expr);
7609 while (c_parser_next_token_is (parser, CPP_COMMA))
7611 c_parser_consume_token (parser);
7612 expr = c_parser_expr_no_commas (parser, NULL);
7613 vec_safe_push (cexpr_list, expr);
7616 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7617 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7618 return false;
7620 *ret_cexpr_list = cexpr_list;
7621 return true;
7624 /* This represents a single generic-association. */
7626 struct c_generic_association
7628 /* The location of the starting token of the type. */
7629 location_t type_location;
7630 /* The association's type, or NULL_TREE for 'default'. */
7631 tree type;
7632 /* The association's expression. */
7633 struct c_expr expression;
7636 /* Parse a generic-selection. (C11 6.5.1.1).
7638 generic-selection:
7639 _Generic ( assignment-expression , generic-assoc-list )
7641 generic-assoc-list:
7642 generic-association
7643 generic-assoc-list , generic-association
7645 generic-association:
7646 type-name : assignment-expression
7647 default : assignment-expression
7650 static struct c_expr
7651 c_parser_generic_selection (c_parser *parser)
7653 struct c_expr selector, error_expr;
7654 tree selector_type;
7655 struct c_generic_association matched_assoc;
7656 bool match_found = false;
7657 location_t generic_loc, selector_loc;
7659 error_expr.original_code = ERROR_MARK;
7660 error_expr.original_type = NULL;
7661 error_expr.set_error ();
7662 matched_assoc.type_location = UNKNOWN_LOCATION;
7663 matched_assoc.type = NULL_TREE;
7664 matched_assoc.expression = error_expr;
7666 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7667 generic_loc = c_parser_peek_token (parser)->location;
7668 c_parser_consume_token (parser);
7669 if (flag_isoc99)
7670 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7671 "ISO C99 does not support %<_Generic%>");
7672 else
7673 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7674 "ISO C90 does not support %<_Generic%>");
7676 matching_parens parens;
7677 if (!parens.require_open (parser))
7678 return error_expr;
7680 c_inhibit_evaluation_warnings++;
7681 selector_loc = c_parser_peek_token (parser)->location;
7682 selector = c_parser_expr_no_commas (parser, NULL);
7683 selector = default_function_array_conversion (selector_loc, selector);
7684 c_inhibit_evaluation_warnings--;
7686 if (selector.value == error_mark_node)
7688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7689 return selector;
7691 selector_type = TREE_TYPE (selector.value);
7692 /* In ISO C terms, rvalues (including the controlling expression of
7693 _Generic) do not have qualified types. */
7694 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7695 selector_type = TYPE_MAIN_VARIANT (selector_type);
7696 /* In ISO C terms, _Noreturn is not part of the type of expressions
7697 such as &abort, but in GCC it is represented internally as a type
7698 qualifier. */
7699 if (FUNCTION_POINTER_TYPE_P (selector_type)
7700 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7701 selector_type
7702 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7704 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7706 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7707 return error_expr;
7710 auto_vec<c_generic_association> associations;
7711 while (1)
7713 struct c_generic_association assoc, *iter;
7714 unsigned int ix;
7715 c_token *token = c_parser_peek_token (parser);
7717 assoc.type_location = token->location;
7718 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7720 c_parser_consume_token (parser);
7721 assoc.type = NULL_TREE;
7723 else
7725 struct c_type_name *type_name;
7727 type_name = c_parser_type_name (parser);
7728 if (type_name == NULL)
7730 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7731 return error_expr;
7733 assoc.type = groktypename (type_name, NULL, NULL);
7734 if (assoc.type == error_mark_node)
7736 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7737 return error_expr;
7740 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7741 error_at (assoc.type_location,
7742 "%<_Generic%> association has function type");
7743 else if (!COMPLETE_TYPE_P (assoc.type))
7744 error_at (assoc.type_location,
7745 "%<_Generic%> association has incomplete type");
7747 if (variably_modified_type_p (assoc.type, NULL_TREE))
7748 error_at (assoc.type_location,
7749 "%<_Generic%> association has "
7750 "variable length type");
7753 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7756 return error_expr;
7759 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7760 if (assoc.expression.value == error_mark_node)
7762 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7763 return error_expr;
7766 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7768 if (assoc.type == NULL_TREE)
7770 if (iter->type == NULL_TREE)
7772 error_at (assoc.type_location,
7773 "duplicate %<default%> case in %<_Generic%>");
7774 inform (iter->type_location, "original %<default%> is here");
7777 else if (iter->type != NULL_TREE)
7779 if (comptypes (assoc.type, iter->type))
7781 error_at (assoc.type_location,
7782 "%<_Generic%> specifies two compatible types");
7783 inform (iter->type_location, "compatible type is here");
7788 if (assoc.type == NULL_TREE)
7790 if (!match_found)
7792 matched_assoc = assoc;
7793 match_found = true;
7796 else if (comptypes (assoc.type, selector_type))
7798 if (!match_found || matched_assoc.type == NULL_TREE)
7800 matched_assoc = assoc;
7801 match_found = true;
7803 else
7805 error_at (assoc.type_location,
7806 "%<_Generic%> selector matches multiple associations");
7807 inform (matched_assoc.type_location,
7808 "other match is here");
7812 associations.safe_push (assoc);
7814 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7815 break;
7816 c_parser_consume_token (parser);
7819 if (!parens.require_close (parser))
7821 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7822 return error_expr;
7825 if (!match_found)
7827 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7828 "compatible with any association",
7829 selector_type);
7830 return error_expr;
7833 return matched_assoc.expression;
7836 /* Check the validity of a function pointer argument *EXPR (argument
7837 position POS) to __builtin_tgmath. Return the number of function
7838 arguments if possibly valid; return 0 having reported an error if
7839 not valid. */
7841 static unsigned int
7842 check_tgmath_function (c_expr *expr, unsigned int pos)
7844 tree type = TREE_TYPE (expr->value);
7845 if (!FUNCTION_POINTER_TYPE_P (type))
7847 error_at (expr->get_location (),
7848 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7849 pos);
7850 return 0;
7852 type = TREE_TYPE (type);
7853 if (!prototype_p (type))
7855 error_at (expr->get_location (),
7856 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7857 return 0;
7859 if (stdarg_p (type))
7861 error_at (expr->get_location (),
7862 "argument %u of %<__builtin_tgmath%> has variable arguments",
7863 pos);
7864 return 0;
7866 unsigned int nargs = 0;
7867 function_args_iterator iter;
7868 tree t;
7869 FOREACH_FUNCTION_ARGS (type, t, iter)
7871 if (t == void_type_node)
7872 break;
7873 nargs++;
7875 if (nargs == 0)
7877 error_at (expr->get_location (),
7878 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7879 return 0;
7881 return nargs;
7884 /* Ways in which a parameter or return value of a type-generic macro
7885 may vary between the different functions the macro may call. */
7886 enum tgmath_parm_kind
7888 tgmath_fixed, tgmath_real, tgmath_complex
7891 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7892 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7893 call c_parser_postfix_expression_after_paren_type on encountering them.
7895 postfix-expression:
7896 primary-expression
7897 postfix-expression [ expression ]
7898 postfix-expression ( argument-expression-list[opt] )
7899 postfix-expression . identifier
7900 postfix-expression -> identifier
7901 postfix-expression ++
7902 postfix-expression --
7903 ( type-name ) { initializer-list }
7904 ( type-name ) { initializer-list , }
7906 argument-expression-list:
7907 argument-expression
7908 argument-expression-list , argument-expression
7910 primary-expression:
7911 identifier
7912 constant
7913 string-literal
7914 ( expression )
7915 generic-selection
7917 GNU extensions:
7919 primary-expression:
7920 __func__
7921 (treated as a keyword in GNU C)
7922 __FUNCTION__
7923 __PRETTY_FUNCTION__
7924 ( compound-statement )
7925 __builtin_va_arg ( assignment-expression , type-name )
7926 __builtin_offsetof ( type-name , offsetof-member-designator )
7927 __builtin_choose_expr ( assignment-expression ,
7928 assignment-expression ,
7929 assignment-expression )
7930 __builtin_types_compatible_p ( type-name , type-name )
7931 __builtin_tgmath ( expr-list )
7932 __builtin_complex ( assignment-expression , assignment-expression )
7933 __builtin_shuffle ( assignment-expression , assignment-expression )
7934 __builtin_shuffle ( assignment-expression ,
7935 assignment-expression ,
7936 assignment-expression, )
7938 offsetof-member-designator:
7939 identifier
7940 offsetof-member-designator . identifier
7941 offsetof-member-designator [ expression ]
7943 Objective-C:
7945 primary-expression:
7946 [ objc-receiver objc-message-args ]
7947 @selector ( objc-selector-arg )
7948 @protocol ( identifier )
7949 @encode ( type-name )
7950 objc-string-literal
7951 Classname . identifier
7954 static struct c_expr
7955 c_parser_postfix_expression (c_parser *parser)
7957 struct c_expr expr, e1;
7958 struct c_type_name *t1, *t2;
7959 location_t loc = c_parser_peek_token (parser)->location;;
7960 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7961 expr.original_code = ERROR_MARK;
7962 expr.original_type = NULL;
7963 switch (c_parser_peek_token (parser)->type)
7965 case CPP_NUMBER:
7966 expr.value = c_parser_peek_token (parser)->value;
7967 set_c_expr_source_range (&expr, tok_range);
7968 loc = c_parser_peek_token (parser)->location;
7969 c_parser_consume_token (parser);
7970 if (TREE_CODE (expr.value) == FIXED_CST
7971 && !targetm.fixed_point_supported_p ())
7973 error_at (loc, "fixed-point types not supported for this target");
7974 expr.value = error_mark_node;
7976 break;
7977 case CPP_CHAR:
7978 case CPP_CHAR16:
7979 case CPP_CHAR32:
7980 case CPP_WCHAR:
7981 expr.value = c_parser_peek_token (parser)->value;
7982 /* For the purpose of warning when a pointer is compared with
7983 a zero character constant. */
7984 expr.original_type = char_type_node;
7985 set_c_expr_source_range (&expr, tok_range);
7986 c_parser_consume_token (parser);
7987 break;
7988 case CPP_STRING:
7989 case CPP_STRING16:
7990 case CPP_STRING32:
7991 case CPP_WSTRING:
7992 case CPP_UTF8STRING:
7993 expr.value = c_parser_peek_token (parser)->value;
7994 set_c_expr_source_range (&expr, tok_range);
7995 expr.original_code = STRING_CST;
7996 c_parser_consume_token (parser);
7997 break;
7998 case CPP_OBJC_STRING:
7999 gcc_assert (c_dialect_objc ());
8000 expr.value
8001 = objc_build_string_object (c_parser_peek_token (parser)->value);
8002 set_c_expr_source_range (&expr, tok_range);
8003 c_parser_consume_token (parser);
8004 break;
8005 case CPP_NAME:
8006 switch (c_parser_peek_token (parser)->id_kind)
8008 case C_ID_ID:
8010 tree id = c_parser_peek_token (parser)->value;
8011 c_parser_consume_token (parser);
8012 expr.value = build_external_ref (loc, id,
8013 (c_parser_peek_token (parser)->type
8014 == CPP_OPEN_PAREN),
8015 &expr.original_type);
8016 set_c_expr_source_range (&expr, tok_range);
8017 break;
8019 case C_ID_CLASSNAME:
8021 /* Here we parse the Objective-C 2.0 Class.name dot
8022 syntax. */
8023 tree class_name = c_parser_peek_token (parser)->value;
8024 tree component;
8025 c_parser_consume_token (parser);
8026 gcc_assert (c_dialect_objc ());
8027 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
8029 expr.set_error ();
8030 break;
8032 if (c_parser_next_token_is_not (parser, CPP_NAME))
8034 c_parser_error (parser, "expected identifier");
8035 expr.set_error ();
8036 break;
8038 c_token *component_tok = c_parser_peek_token (parser);
8039 component = component_tok->value;
8040 location_t end_loc = component_tok->get_finish ();
8041 c_parser_consume_token (parser);
8042 expr.value = objc_build_class_component_ref (class_name,
8043 component);
8044 set_c_expr_source_range (&expr, loc, end_loc);
8045 break;
8047 default:
8048 c_parser_error (parser, "expected expression");
8049 expr.set_error ();
8050 break;
8052 break;
8053 case CPP_OPEN_PAREN:
8054 /* A parenthesized expression, statement expression or compound
8055 literal. */
8056 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
8058 /* A statement expression. */
8059 tree stmt;
8060 location_t brace_loc;
8061 c_parser_consume_token (parser);
8062 brace_loc = c_parser_peek_token (parser)->location;
8063 c_parser_consume_token (parser);
8064 if (!building_stmt_list_p ())
8066 error_at (loc, "braced-group within expression allowed "
8067 "only inside a function");
8068 parser->error = true;
8069 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
8070 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8071 expr.set_error ();
8072 break;
8074 stmt = c_begin_stmt_expr ();
8075 c_parser_compound_statement_nostart (parser);
8076 location_t close_loc = c_parser_peek_token (parser)->location;
8077 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8078 "expected %<)%>");
8079 pedwarn (loc, OPT_Wpedantic,
8080 "ISO C forbids braced-groups within expressions");
8081 expr.value = c_finish_stmt_expr (brace_loc, stmt);
8082 set_c_expr_source_range (&expr, loc, close_loc);
8083 mark_exp_read (expr.value);
8085 else
8087 /* A parenthesized expression. */
8088 location_t loc_open_paren = c_parser_peek_token (parser)->location;
8089 c_parser_consume_token (parser);
8090 expr = c_parser_expression (parser);
8091 if (TREE_CODE (expr.value) == MODIFY_EXPR)
8092 TREE_NO_WARNING (expr.value) = 1;
8093 if (expr.original_code != C_MAYBE_CONST_EXPR
8094 && expr.original_code != SIZEOF_EXPR)
8095 expr.original_code = ERROR_MARK;
8096 /* Don't change EXPR.ORIGINAL_TYPE. */
8097 location_t loc_close_paren = c_parser_peek_token (parser)->location;
8098 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
8099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8100 "expected %<)%>", loc_open_paren);
8102 break;
8103 case CPP_KEYWORD:
8104 switch (c_parser_peek_token (parser)->keyword)
8106 case RID_FUNCTION_NAME:
8107 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8108 "%<__FUNCTION__%> predefined identifier");
8109 expr.value = fname_decl (loc,
8110 c_parser_peek_token (parser)->keyword,
8111 c_parser_peek_token (parser)->value);
8112 set_c_expr_source_range (&expr, loc, loc);
8113 c_parser_consume_token (parser);
8114 break;
8115 case RID_PRETTY_FUNCTION_NAME:
8116 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8117 "%<__PRETTY_FUNCTION__%> predefined identifier");
8118 expr.value = fname_decl (loc,
8119 c_parser_peek_token (parser)->keyword,
8120 c_parser_peek_token (parser)->value);
8121 set_c_expr_source_range (&expr, loc, loc);
8122 c_parser_consume_token (parser);
8123 break;
8124 case RID_C99_FUNCTION_NAME:
8125 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8126 "%<__func__%> predefined identifier");
8127 expr.value = fname_decl (loc,
8128 c_parser_peek_token (parser)->keyword,
8129 c_parser_peek_token (parser)->value);
8130 set_c_expr_source_range (&expr, loc, loc);
8131 c_parser_consume_token (parser);
8132 break;
8133 case RID_VA_ARG:
8135 location_t start_loc = loc;
8136 c_parser_consume_token (parser);
8137 matching_parens parens;
8138 if (!parens.require_open (parser))
8140 expr.set_error ();
8141 break;
8143 e1 = c_parser_expr_no_commas (parser, NULL);
8144 mark_exp_read (e1.value);
8145 e1.value = c_fully_fold (e1.value, false, NULL);
8146 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8149 expr.set_error ();
8150 break;
8152 loc = c_parser_peek_token (parser)->location;
8153 t1 = c_parser_type_name (parser);
8154 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8155 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8156 "expected %<)%>");
8157 if (t1 == NULL)
8159 expr.set_error ();
8161 else
8163 tree type_expr = NULL_TREE;
8164 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8165 groktypename (t1, &type_expr, NULL));
8166 if (type_expr)
8168 expr.value = build2 (C_MAYBE_CONST_EXPR,
8169 TREE_TYPE (expr.value), type_expr,
8170 expr.value);
8171 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8173 set_c_expr_source_range (&expr, start_loc, end_loc);
8176 break;
8177 case RID_OFFSETOF:
8179 c_parser_consume_token (parser);
8180 matching_parens parens;
8181 if (!parens.require_open (parser))
8183 expr.set_error ();
8184 break;
8186 t1 = c_parser_type_name (parser);
8187 if (t1 == NULL)
8188 parser->error = true;
8189 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8190 gcc_assert (parser->error);
8191 if (parser->error)
8193 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8194 expr.set_error ();
8195 break;
8197 tree type = groktypename (t1, NULL, NULL);
8198 tree offsetof_ref;
8199 if (type == error_mark_node)
8200 offsetof_ref = error_mark_node;
8201 else
8203 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8204 SET_EXPR_LOCATION (offsetof_ref, loc);
8206 /* Parse the second argument to __builtin_offsetof. We
8207 must have one identifier, and beyond that we want to
8208 accept sub structure and sub array references. */
8209 if (c_parser_next_token_is (parser, CPP_NAME))
8211 c_token *comp_tok = c_parser_peek_token (parser);
8212 offsetof_ref = build_component_ref
8213 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8214 c_parser_consume_token (parser);
8215 while (c_parser_next_token_is (parser, CPP_DOT)
8216 || c_parser_next_token_is (parser,
8217 CPP_OPEN_SQUARE)
8218 || c_parser_next_token_is (parser,
8219 CPP_DEREF))
8221 if (c_parser_next_token_is (parser, CPP_DEREF))
8223 loc = c_parser_peek_token (parser)->location;
8224 offsetof_ref = build_array_ref (loc,
8225 offsetof_ref,
8226 integer_zero_node);
8227 goto do_dot;
8229 else if (c_parser_next_token_is (parser, CPP_DOT))
8231 do_dot:
8232 c_parser_consume_token (parser);
8233 if (c_parser_next_token_is_not (parser,
8234 CPP_NAME))
8236 c_parser_error (parser, "expected identifier");
8237 break;
8239 c_token *comp_tok = c_parser_peek_token (parser);
8240 offsetof_ref = build_component_ref
8241 (loc, offsetof_ref, comp_tok->value,
8242 comp_tok->location);
8243 c_parser_consume_token (parser);
8245 else
8247 struct c_expr ce;
8248 tree idx;
8249 loc = c_parser_peek_token (parser)->location;
8250 c_parser_consume_token (parser);
8251 ce = c_parser_expression (parser);
8252 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8253 idx = ce.value;
8254 idx = c_fully_fold (idx, false, NULL);
8255 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8256 "expected %<]%>");
8257 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8261 else
8262 c_parser_error (parser, "expected identifier");
8263 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8265 "expected %<)%>");
8266 expr.value = fold_offsetof (offsetof_ref);
8267 set_c_expr_source_range (&expr, loc, end_loc);
8269 break;
8270 case RID_CHOOSE_EXPR:
8272 vec<c_expr_t, va_gc> *cexpr_list;
8273 c_expr_t *e1_p, *e2_p, *e3_p;
8274 tree c;
8275 location_t close_paren_loc;
8277 c_parser_consume_token (parser);
8278 if (!c_parser_get_builtin_args (parser,
8279 "__builtin_choose_expr",
8280 &cexpr_list, true,
8281 &close_paren_loc))
8283 expr.set_error ();
8284 break;
8287 if (vec_safe_length (cexpr_list) != 3)
8289 error_at (loc, "wrong number of arguments to "
8290 "%<__builtin_choose_expr%>");
8291 expr.set_error ();
8292 break;
8295 e1_p = &(*cexpr_list)[0];
8296 e2_p = &(*cexpr_list)[1];
8297 e3_p = &(*cexpr_list)[2];
8299 c = e1_p->value;
8300 mark_exp_read (e2_p->value);
8301 mark_exp_read (e3_p->value);
8302 if (TREE_CODE (c) != INTEGER_CST
8303 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8304 error_at (loc,
8305 "first argument to %<__builtin_choose_expr%> not"
8306 " a constant");
8307 constant_expression_warning (c);
8308 expr = integer_zerop (c) ? *e3_p : *e2_p;
8309 set_c_expr_source_range (&expr, loc, close_paren_loc);
8310 break;
8312 case RID_TYPES_COMPATIBLE_P:
8314 c_parser_consume_token (parser);
8315 matching_parens parens;
8316 if (!parens.require_open (parser))
8318 expr.set_error ();
8319 break;
8321 t1 = c_parser_type_name (parser);
8322 if (t1 == NULL)
8324 expr.set_error ();
8325 break;
8327 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8330 expr.set_error ();
8331 break;
8333 t2 = c_parser_type_name (parser);
8334 if (t2 == NULL)
8336 expr.set_error ();
8337 break;
8339 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8340 parens.skip_until_found_close (parser);
8341 tree e1, e2;
8342 e1 = groktypename (t1, NULL, NULL);
8343 e2 = groktypename (t2, NULL, NULL);
8344 if (e1 == error_mark_node || e2 == error_mark_node)
8346 expr.set_error ();
8347 break;
8350 e1 = TYPE_MAIN_VARIANT (e1);
8351 e2 = TYPE_MAIN_VARIANT (e2);
8353 expr.value
8354 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8355 set_c_expr_source_range (&expr, loc, close_paren_loc);
8357 break;
8358 case RID_BUILTIN_TGMATH:
8360 vec<c_expr_t, va_gc> *cexpr_list;
8361 location_t close_paren_loc;
8363 c_parser_consume_token (parser);
8364 if (!c_parser_get_builtin_args (parser,
8365 "__builtin_tgmath",
8366 &cexpr_list, false,
8367 &close_paren_loc))
8369 expr.set_error ();
8370 break;
8373 if (vec_safe_length (cexpr_list) < 3)
8375 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8376 expr.set_error ();
8377 break;
8380 unsigned int i;
8381 c_expr_t *p;
8382 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8383 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8384 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8385 if (nargs == 0)
8387 expr.set_error ();
8388 break;
8390 if (vec_safe_length (cexpr_list) < nargs)
8392 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8393 expr.set_error ();
8394 break;
8396 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8397 if (num_functions < 2)
8399 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8400 expr.set_error ();
8401 break;
8404 /* The first NUM_FUNCTIONS expressions are the function
8405 pointers. The remaining NARGS expressions are the
8406 arguments that are to be passed to one of those
8407 functions, chosen following <tgmath.h> rules. */
8408 for (unsigned int j = 1; j < num_functions; j++)
8410 unsigned int this_nargs
8411 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8412 if (this_nargs == 0)
8414 expr.set_error ();
8415 goto out;
8417 if (this_nargs != nargs)
8419 error_at ((*cexpr_list)[j].get_location (),
8420 "argument %u of %<__builtin_tgmath%> has "
8421 "wrong number of arguments", j + 1);
8422 expr.set_error ();
8423 goto out;
8427 /* The functions all have the same number of arguments.
8428 Determine whether arguments and return types vary in
8429 ways permitted for <tgmath.h> functions. */
8430 /* The first entry in each of these vectors is for the
8431 return type, subsequent entries for parameter
8432 types. */
8433 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8434 auto_vec<tree> parm_first (nargs + 1);
8435 auto_vec<bool> parm_complex (nargs + 1);
8436 auto_vec<bool> parm_varies (nargs + 1);
8437 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8438 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8439 parm_first.quick_push (first_ret);
8440 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8441 parm_varies.quick_push (false);
8442 function_args_iterator iter;
8443 tree t;
8444 unsigned int argpos;
8445 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8447 if (t == void_type_node)
8448 break;
8449 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8450 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8451 parm_varies.quick_push (false);
8453 for (unsigned int j = 1; j < num_functions; j++)
8455 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8456 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8457 if (ret != parm_first[0])
8459 parm_varies[0] = true;
8460 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8461 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8463 error_at ((*cexpr_list)[0].get_location (),
8464 "invalid type-generic return type for "
8465 "argument %u of %<__builtin_tgmath%>",
8467 expr.set_error ();
8468 goto out;
8470 if (!SCALAR_FLOAT_TYPE_P (ret)
8471 && !COMPLEX_FLOAT_TYPE_P (ret))
8473 error_at ((*cexpr_list)[j].get_location (),
8474 "invalid type-generic return type for "
8475 "argument %u of %<__builtin_tgmath%>",
8476 j + 1);
8477 expr.set_error ();
8478 goto out;
8481 if (TREE_CODE (ret) == COMPLEX_TYPE)
8482 parm_complex[0] = true;
8483 argpos = 1;
8484 FOREACH_FUNCTION_ARGS (type, t, iter)
8486 if (t == void_type_node)
8487 break;
8488 t = TYPE_MAIN_VARIANT (t);
8489 if (t != parm_first[argpos])
8491 parm_varies[argpos] = true;
8492 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8493 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8495 error_at ((*cexpr_list)[0].get_location (),
8496 "invalid type-generic type for "
8497 "argument %u of argument %u of "
8498 "%<__builtin_tgmath%>", argpos, 1);
8499 expr.set_error ();
8500 goto out;
8502 if (!SCALAR_FLOAT_TYPE_P (t)
8503 && !COMPLEX_FLOAT_TYPE_P (t))
8505 error_at ((*cexpr_list)[j].get_location (),
8506 "invalid type-generic type for "
8507 "argument %u of argument %u of "
8508 "%<__builtin_tgmath%>", argpos, j + 1);
8509 expr.set_error ();
8510 goto out;
8513 if (TREE_CODE (t) == COMPLEX_TYPE)
8514 parm_complex[argpos] = true;
8515 argpos++;
8518 enum tgmath_parm_kind max_variation = tgmath_fixed;
8519 for (unsigned int j = 0; j <= nargs; j++)
8521 enum tgmath_parm_kind this_kind;
8522 if (parm_varies[j])
8524 if (parm_complex[j])
8525 max_variation = this_kind = tgmath_complex;
8526 else
8528 this_kind = tgmath_real;
8529 if (max_variation != tgmath_complex)
8530 max_variation = tgmath_real;
8533 else
8534 this_kind = tgmath_fixed;
8535 parm_kind.quick_push (this_kind);
8537 if (max_variation == tgmath_fixed)
8539 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8540 "all have the same type");
8541 expr.set_error ();
8542 break;
8545 /* Identify a parameter (not the return type) that varies,
8546 including with complex types if any variation includes
8547 complex types; there must be at least one such
8548 parameter. */
8549 unsigned int tgarg = 0;
8550 for (unsigned int j = 1; j <= nargs; j++)
8551 if (parm_kind[j] == max_variation)
8553 tgarg = j;
8554 break;
8556 if (tgarg == 0)
8558 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8559 "lack type-generic parameter");
8560 expr.set_error ();
8561 break;
8564 /* Determine the type of the relevant parameter for each
8565 function. */
8566 auto_vec<tree> tg_type (num_functions);
8567 for (unsigned int j = 0; j < num_functions; j++)
8569 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8570 argpos = 1;
8571 FOREACH_FUNCTION_ARGS (type, t, iter)
8573 if (argpos == tgarg)
8575 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8576 break;
8578 argpos++;
8582 /* Verify that the corresponding types are different for
8583 all the listed functions. Also determine whether all
8584 the types are complex, whether all the types are
8585 standard or binary, and whether all the types are
8586 decimal. */
8587 bool all_complex = true;
8588 bool all_binary = true;
8589 bool all_decimal = true;
8590 hash_set<tree> tg_types;
8591 FOR_EACH_VEC_ELT (tg_type, i, t)
8593 if (TREE_CODE (t) == COMPLEX_TYPE)
8594 all_decimal = false;
8595 else
8597 all_complex = false;
8598 if (DECIMAL_FLOAT_TYPE_P (t))
8599 all_binary = false;
8600 else
8601 all_decimal = false;
8603 if (tg_types.add (t))
8605 error_at ((*cexpr_list)[i].get_location (),
8606 "duplicate type-generic parameter type for "
8607 "function argument %u of %<__builtin_tgmath%>",
8608 i + 1);
8609 expr.set_error ();
8610 goto out;
8614 /* Verify that other parameters and the return type whose
8615 types vary have their types varying in the correct
8616 way. */
8617 for (unsigned int j = 0; j < num_functions; j++)
8619 tree exp_type = tg_type[j];
8620 tree exp_real_type = exp_type;
8621 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8622 exp_real_type = TREE_TYPE (exp_type);
8623 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8624 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8625 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8626 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8628 error_at ((*cexpr_list)[j].get_location (),
8629 "bad return type for function argument %u "
8630 "of %<__builtin_tgmath%>", j + 1);
8631 expr.set_error ();
8632 goto out;
8634 argpos = 1;
8635 FOREACH_FUNCTION_ARGS (type, t, iter)
8637 if (t == void_type_node)
8638 break;
8639 t = TYPE_MAIN_VARIANT (t);
8640 if ((parm_kind[argpos] == tgmath_complex
8641 && t != exp_type)
8642 || (parm_kind[argpos] == tgmath_real
8643 && t != exp_real_type))
8645 error_at ((*cexpr_list)[j].get_location (),
8646 "bad type for argument %u of "
8647 "function argument %u of "
8648 "%<__builtin_tgmath%>", argpos, j + 1);
8649 expr.set_error ();
8650 goto out;
8652 argpos++;
8656 /* The functions listed are a valid set of functions for a
8657 <tgmath.h> macro to select between. Identify the
8658 matching function, if any. First, the argument types
8659 must be combined following <tgmath.h> rules. Integer
8660 types are treated as _Decimal64 if any type-generic
8661 argument is decimal, or if the only alternatives for
8662 type-generic arguments are of decimal types, and are
8663 otherwise treated as double (or _Complex double for
8664 complex integer types). After that adjustment, types
8665 are combined following the usual arithmetic
8666 conversions. If the function only accepts complex
8667 arguments, a complex type is produced. */
8668 bool arg_complex = all_complex;
8669 bool arg_binary = all_binary;
8670 bool arg_int_decimal = all_decimal;
8671 for (unsigned int j = 1; j <= nargs; j++)
8673 if (parm_kind[j] == tgmath_fixed)
8674 continue;
8675 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8676 tree type = TREE_TYPE (ce->value);
8677 if (!INTEGRAL_TYPE_P (type)
8678 && !SCALAR_FLOAT_TYPE_P (type)
8679 && TREE_CODE (type) != COMPLEX_TYPE)
8681 error_at (ce->get_location (),
8682 "invalid type of argument %u of type-generic "
8683 "function", j);
8684 expr.set_error ();
8685 goto out;
8687 if (DECIMAL_FLOAT_TYPE_P (type))
8689 arg_int_decimal = true;
8690 if (all_complex)
8692 error_at (ce->get_location (),
8693 "decimal floating-point argument %u to "
8694 "complex-only type-generic function", j);
8695 expr.set_error ();
8696 goto out;
8698 else if (all_binary)
8700 error_at (ce->get_location (),
8701 "decimal floating-point argument %u to "
8702 "binary-only type-generic function", j);
8703 expr.set_error ();
8704 goto out;
8706 else if (arg_complex)
8708 error_at (ce->get_location (),
8709 "both complex and decimal floating-point "
8710 "arguments to type-generic function");
8711 expr.set_error ();
8712 goto out;
8714 else if (arg_binary)
8716 error_at (ce->get_location (),
8717 "both binary and decimal floating-point "
8718 "arguments to type-generic function");
8719 expr.set_error ();
8720 goto out;
8723 else if (TREE_CODE (type) == COMPLEX_TYPE)
8725 arg_complex = true;
8726 if (COMPLEX_FLOAT_TYPE_P (type))
8727 arg_binary = true;
8728 if (all_decimal)
8730 error_at (ce->get_location (),
8731 "complex argument %u to "
8732 "decimal-only type-generic function", j);
8733 expr.set_error ();
8734 goto out;
8736 else if (arg_int_decimal)
8738 error_at (ce->get_location (),
8739 "both complex and decimal floating-point "
8740 "arguments to type-generic function");
8741 expr.set_error ();
8742 goto out;
8745 else if (SCALAR_FLOAT_TYPE_P (type))
8747 arg_binary = true;
8748 if (all_decimal)
8750 error_at (ce->get_location (),
8751 "binary argument %u to "
8752 "decimal-only type-generic function", j);
8753 expr.set_error ();
8754 goto out;
8756 else if (arg_int_decimal)
8758 error_at (ce->get_location (),
8759 "both binary and decimal floating-point "
8760 "arguments to type-generic function");
8761 expr.set_error ();
8762 goto out;
8766 tree arg_real = NULL_TREE;
8767 for (unsigned int j = 1; j <= nargs; j++)
8769 if (parm_kind[j] == tgmath_fixed)
8770 continue;
8771 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8772 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8773 if (TREE_CODE (type) == COMPLEX_TYPE)
8774 type = TREE_TYPE (type);
8775 if (INTEGRAL_TYPE_P (type))
8776 type = (arg_int_decimal
8777 ? dfloat64_type_node
8778 : double_type_node);
8779 if (arg_real == NULL_TREE)
8780 arg_real = type;
8781 else
8782 arg_real = common_type (arg_real, type);
8783 if (arg_real == error_mark_node)
8785 expr.set_error ();
8786 goto out;
8789 tree arg_type = (arg_complex
8790 ? build_complex_type (arg_real)
8791 : arg_real);
8793 /* Look for a function to call with type-generic parameter
8794 type ARG_TYPE. */
8795 c_expr_t *fn = NULL;
8796 for (unsigned int j = 0; j < num_functions; j++)
8798 if (tg_type[j] == arg_type)
8800 fn = &(*cexpr_list)[j];
8801 break;
8804 if (fn == NULL
8805 && parm_kind[0] == tgmath_fixed
8806 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8808 /* Presume this is a macro that rounds its result to a
8809 narrower type, and look for the first function with
8810 at least the range and precision of the argument
8811 type. */
8812 for (unsigned int j = 0; j < num_functions; j++)
8814 if (arg_complex
8815 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8816 continue;
8817 tree real_tg_type = (arg_complex
8818 ? TREE_TYPE (tg_type[j])
8819 : tg_type[j]);
8820 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8821 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8822 continue;
8823 scalar_float_mode arg_mode
8824 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8825 scalar_float_mode tg_mode
8826 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8827 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8828 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8829 if (arg_fmt->b == tg_fmt->b
8830 && arg_fmt->p <= tg_fmt->p
8831 && arg_fmt->emax <= tg_fmt->emax
8832 && (arg_fmt->emin - arg_fmt->p
8833 >= tg_fmt->emin - tg_fmt->p))
8835 fn = &(*cexpr_list)[j];
8836 break;
8840 if (fn == NULL)
8842 error_at (loc, "no matching function for type-generic call");
8843 expr.set_error ();
8844 break;
8847 /* Construct a call to FN. */
8848 vec<tree, va_gc> *args;
8849 vec_alloc (args, nargs);
8850 vec<tree, va_gc> *origtypes;
8851 vec_alloc (origtypes, nargs);
8852 auto_vec<location_t> arg_loc (nargs);
8853 for (unsigned int j = 0; j < nargs; j++)
8855 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8856 args->quick_push (ce->value);
8857 arg_loc.quick_push (ce->get_location ());
8858 origtypes->quick_push (ce->original_type);
8860 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8861 args, origtypes);
8862 set_c_expr_source_range (&expr, loc, close_paren_loc);
8863 break;
8865 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8867 vec<c_expr_t, va_gc> *cexpr_list;
8868 c_expr_t *e2_p;
8869 tree chain_value;
8870 location_t close_paren_loc;
8872 c_parser_consume_token (parser);
8873 if (!c_parser_get_builtin_args (parser,
8874 "__builtin_call_with_static_chain",
8875 &cexpr_list, false,
8876 &close_paren_loc))
8878 expr.set_error ();
8879 break;
8881 if (vec_safe_length (cexpr_list) != 2)
8883 error_at (loc, "wrong number of arguments to "
8884 "%<__builtin_call_with_static_chain%>");
8885 expr.set_error ();
8886 break;
8889 expr = (*cexpr_list)[0];
8890 e2_p = &(*cexpr_list)[1];
8891 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8892 chain_value = e2_p->value;
8893 mark_exp_read (chain_value);
8895 if (TREE_CODE (expr.value) != CALL_EXPR)
8896 error_at (loc, "first argument to "
8897 "%<__builtin_call_with_static_chain%> "
8898 "must be a call expression");
8899 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8900 error_at (loc, "second argument to "
8901 "%<__builtin_call_with_static_chain%> "
8902 "must be a pointer type");
8903 else
8904 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8905 set_c_expr_source_range (&expr, loc, close_paren_loc);
8906 break;
8908 case RID_BUILTIN_COMPLEX:
8910 vec<c_expr_t, va_gc> *cexpr_list;
8911 c_expr_t *e1_p, *e2_p;
8912 location_t close_paren_loc;
8914 c_parser_consume_token (parser);
8915 if (!c_parser_get_builtin_args (parser,
8916 "__builtin_complex",
8917 &cexpr_list, false,
8918 &close_paren_loc))
8920 expr.set_error ();
8921 break;
8924 if (vec_safe_length (cexpr_list) != 2)
8926 error_at (loc, "wrong number of arguments to "
8927 "%<__builtin_complex%>");
8928 expr.set_error ();
8929 break;
8932 e1_p = &(*cexpr_list)[0];
8933 e2_p = &(*cexpr_list)[1];
8935 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8936 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8937 e1_p->value = convert (TREE_TYPE (e1_p->value),
8938 TREE_OPERAND (e1_p->value, 0));
8939 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8940 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8941 e2_p->value = convert (TREE_TYPE (e2_p->value),
8942 TREE_OPERAND (e2_p->value, 0));
8943 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8944 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8945 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8946 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8948 error_at (loc, "%<__builtin_complex%> operand "
8949 "not of real binary floating-point type");
8950 expr.set_error ();
8951 break;
8953 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8954 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8956 error_at (loc,
8957 "%<__builtin_complex%> operands of different types");
8958 expr.set_error ();
8959 break;
8961 pedwarn_c90 (loc, OPT_Wpedantic,
8962 "ISO C90 does not support complex types");
8963 expr.value = build2_loc (loc, COMPLEX_EXPR,
8964 build_complex_type
8965 (TYPE_MAIN_VARIANT
8966 (TREE_TYPE (e1_p->value))),
8967 e1_p->value, e2_p->value);
8968 set_c_expr_source_range (&expr, loc, close_paren_loc);
8969 break;
8971 case RID_BUILTIN_SHUFFLE:
8973 vec<c_expr_t, va_gc> *cexpr_list;
8974 unsigned int i;
8975 c_expr_t *p;
8976 location_t close_paren_loc;
8978 c_parser_consume_token (parser);
8979 if (!c_parser_get_builtin_args (parser,
8980 "__builtin_shuffle",
8981 &cexpr_list, false,
8982 &close_paren_loc))
8984 expr.set_error ();
8985 break;
8988 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8989 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8991 if (vec_safe_length (cexpr_list) == 2)
8992 expr.value =
8993 c_build_vec_perm_expr
8994 (loc, (*cexpr_list)[0].value,
8995 NULL_TREE, (*cexpr_list)[1].value);
8997 else if (vec_safe_length (cexpr_list) == 3)
8998 expr.value =
8999 c_build_vec_perm_expr
9000 (loc, (*cexpr_list)[0].value,
9001 (*cexpr_list)[1].value,
9002 (*cexpr_list)[2].value);
9003 else
9005 error_at (loc, "wrong number of arguments to "
9006 "%<__builtin_shuffle%>");
9007 expr.set_error ();
9009 set_c_expr_source_range (&expr, loc, close_paren_loc);
9010 break;
9012 case RID_AT_SELECTOR:
9014 gcc_assert (c_dialect_objc ());
9015 c_parser_consume_token (parser);
9016 matching_parens parens;
9017 if (!parens.require_open (parser))
9019 expr.set_error ();
9020 break;
9022 tree sel = c_parser_objc_selector_arg (parser);
9023 location_t close_loc = c_parser_peek_token (parser)->location;
9024 parens.skip_until_found_close (parser);
9025 expr.value = objc_build_selector_expr (loc, sel);
9026 set_c_expr_source_range (&expr, loc, close_loc);
9028 break;
9029 case RID_AT_PROTOCOL:
9031 gcc_assert (c_dialect_objc ());
9032 c_parser_consume_token (parser);
9033 matching_parens parens;
9034 if (!parens.require_open (parser))
9036 expr.set_error ();
9037 break;
9039 if (c_parser_next_token_is_not (parser, CPP_NAME))
9041 c_parser_error (parser, "expected identifier");
9042 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9043 expr.set_error ();
9044 break;
9046 tree id = c_parser_peek_token (parser)->value;
9047 c_parser_consume_token (parser);
9048 location_t close_loc = c_parser_peek_token (parser)->location;
9049 parens.skip_until_found_close (parser);
9050 expr.value = objc_build_protocol_expr (id);
9051 set_c_expr_source_range (&expr, loc, close_loc);
9053 break;
9054 case RID_AT_ENCODE:
9056 /* Extension to support C-structures in the archiver. */
9057 gcc_assert (c_dialect_objc ());
9058 c_parser_consume_token (parser);
9059 matching_parens parens;
9060 if (!parens.require_open (parser))
9062 expr.set_error ();
9063 break;
9065 t1 = c_parser_type_name (parser);
9066 if (t1 == NULL)
9068 expr.set_error ();
9069 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9070 break;
9072 location_t close_loc = c_parser_peek_token (parser)->location;
9073 parens.skip_until_found_close (parser);
9074 tree type = groktypename (t1, NULL, NULL);
9075 expr.value = objc_build_encode_expr (type);
9076 set_c_expr_source_range (&expr, loc, close_loc);
9078 break;
9079 case RID_GENERIC:
9080 expr = c_parser_generic_selection (parser);
9081 break;
9082 case RID_CILK_SPAWN:
9083 c_parser_consume_token (parser);
9084 if (!flag_cilkplus)
9086 error_at (loc, "-fcilkplus must be enabled to use "
9087 "%<_Cilk_spawn%>");
9088 expr = c_parser_cast_expression (parser, NULL);
9089 expr.set_error ();
9091 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
9093 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
9094 "are not permitted");
9095 /* Now flush out all the _Cilk_spawns. */
9096 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
9097 c_parser_consume_token (parser);
9098 expr = c_parser_cast_expression (parser, NULL);
9100 else
9102 expr = c_parser_cast_expression (parser, NULL);
9103 expr.value = build_cilk_spawn (loc, expr.value);
9105 break;
9106 default:
9107 c_parser_error (parser, "expected expression");
9108 expr.set_error ();
9109 break;
9111 break;
9112 case CPP_OPEN_SQUARE:
9113 if (c_dialect_objc ())
9115 tree receiver, args;
9116 c_parser_consume_token (parser);
9117 receiver = c_parser_objc_receiver (parser);
9118 args = c_parser_objc_message_args (parser);
9119 location_t close_loc = c_parser_peek_token (parser)->location;
9120 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9121 "expected %<]%>");
9122 expr.value = objc_build_message_expr (receiver, args);
9123 set_c_expr_source_range (&expr, loc, close_loc);
9124 break;
9126 /* Else fall through to report error. */
9127 /* FALLTHRU */
9128 default:
9129 c_parser_error (parser, "expected expression");
9130 expr.set_error ();
9131 break;
9133 out:
9134 return c_parser_postfix_expression_after_primary
9135 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9138 /* Parse a postfix expression after a parenthesized type name: the
9139 brace-enclosed initializer of a compound literal, possibly followed
9140 by some postfix operators. This is separate because it is not
9141 possible to tell until after the type name whether a cast
9142 expression has a cast or a compound literal, or whether the operand
9143 of sizeof is a parenthesized type name or starts with a compound
9144 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9145 location of the first token after the parentheses around the type
9146 name. */
9148 static struct c_expr
9149 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9150 struct c_type_name *type_name,
9151 location_t type_loc)
9153 tree type;
9154 struct c_expr init;
9155 bool non_const;
9156 struct c_expr expr;
9157 location_t start_loc;
9158 tree type_expr = NULL_TREE;
9159 bool type_expr_const = true;
9160 check_compound_literal_type (type_loc, type_name);
9161 rich_location richloc (line_table, type_loc);
9162 start_init (NULL_TREE, NULL, 0, &richloc);
9163 type = groktypename (type_name, &type_expr, &type_expr_const);
9164 start_loc = c_parser_peek_token (parser)->location;
9165 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9167 error_at (type_loc, "compound literal has variable size");
9168 type = error_mark_node;
9170 init = c_parser_braced_init (parser, type, false, NULL);
9171 finish_init ();
9172 maybe_warn_string_init (type_loc, type, init);
9174 if (type != error_mark_node
9175 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9176 && current_function_decl)
9178 error ("compound literal qualified by address-space qualifier");
9179 type = error_mark_node;
9182 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9183 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9184 ? CONSTRUCTOR_NON_CONST (init.value)
9185 : init.original_code == C_MAYBE_CONST_EXPR);
9186 non_const |= !type_expr_const;
9187 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
9188 set_c_expr_source_range (&expr, init.src_range);
9189 expr.original_code = ERROR_MARK;
9190 expr.original_type = NULL;
9191 if (type != error_mark_node
9192 && expr.value != error_mark_node
9193 && type_expr)
9195 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9197 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9198 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9200 else
9202 gcc_assert (!non_const);
9203 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9204 type_expr, expr.value);
9207 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9210 /* Callback function for sizeof_pointer_memaccess_warning to compare
9211 types. */
9213 static bool
9214 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9216 return comptypes (type1, type2) == 1;
9219 /* Parse a postfix expression after the initial primary or compound
9220 literal; that is, parse a series of postfix operators.
9222 EXPR_LOC is the location of the primary expression. */
9224 static struct c_expr
9225 c_parser_postfix_expression_after_primary (c_parser *parser,
9226 location_t expr_loc,
9227 struct c_expr expr)
9229 struct c_expr orig_expr;
9230 tree ident, idx;
9231 location_t sizeof_arg_loc[3], comp_loc;
9232 tree sizeof_arg[3];
9233 unsigned int literal_zero_mask;
9234 unsigned int i;
9235 vec<tree, va_gc> *exprlist;
9236 vec<tree, va_gc> *origtypes = NULL;
9237 vec<location_t> arg_loc = vNULL;
9238 location_t start;
9239 location_t finish;
9241 while (true)
9243 location_t op_loc = c_parser_peek_token (parser)->location;
9244 switch (c_parser_peek_token (parser)->type)
9246 case CPP_OPEN_SQUARE:
9247 /* Array reference. */
9248 c_parser_consume_token (parser);
9249 if (flag_cilkplus
9250 && c_parser_peek_token (parser)->type == CPP_COLON)
9251 /* If we are here, then we have something like this:
9252 Array [ : ]
9254 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
9255 expr.value);
9256 else
9258 idx = c_parser_expression (parser).value;
9259 /* Here we have 3 options:
9260 1. Array [EXPR] -- Normal Array call.
9261 2. Array [EXPR : EXPR] -- Array notation without stride.
9262 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
9264 For 1, we just handle it just like a normal array expression.
9265 For 2 and 3 we handle it like we handle array notations. The
9266 idx value we have above becomes the initial/start index.
9268 if (flag_cilkplus
9269 && c_parser_peek_token (parser)->type == CPP_COLON)
9270 expr.value = c_parser_array_notation (expr_loc, parser, idx,
9271 expr.value);
9272 else
9274 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9275 "expected %<]%>");
9276 start = expr.get_start ();
9277 finish = parser->tokens_buf[0].location;
9278 expr.value = build_array_ref (op_loc, expr.value, idx);
9279 set_c_expr_source_range (&expr, start, finish);
9282 expr.original_code = ERROR_MARK;
9283 expr.original_type = NULL;
9284 break;
9285 case CPP_OPEN_PAREN:
9286 /* Function call. */
9287 c_parser_consume_token (parser);
9288 for (i = 0; i < 3; i++)
9290 sizeof_arg[i] = NULL_TREE;
9291 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9293 literal_zero_mask = 0;
9294 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9295 exprlist = NULL;
9296 else
9297 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9298 sizeof_arg_loc, sizeof_arg,
9299 &arg_loc, &literal_zero_mask);
9300 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9301 "expected %<)%>");
9302 orig_expr = expr;
9303 mark_exp_read (expr.value);
9304 if (warn_sizeof_pointer_memaccess)
9305 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9306 expr.value, exprlist,
9307 sizeof_arg,
9308 sizeof_ptr_memacc_comptypes);
9309 if (TREE_CODE (expr.value) == FUNCTION_DECL
9310 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9311 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9312 && vec_safe_length (exprlist) == 3)
9314 tree arg0 = (*exprlist)[0];
9315 tree arg2 = (*exprlist)[2];
9316 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9319 start = expr.get_start ();
9320 finish = parser->tokens_buf[0].get_finish ();
9321 expr.value
9322 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9323 exprlist, origtypes);
9324 set_c_expr_source_range (&expr, start, finish);
9326 expr.original_code = ERROR_MARK;
9327 if (TREE_CODE (expr.value) == INTEGER_CST
9328 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9329 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9330 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9331 expr.original_code = C_MAYBE_CONST_EXPR;
9332 expr.original_type = NULL;
9333 if (exprlist)
9335 release_tree_vector (exprlist);
9336 release_tree_vector (origtypes);
9338 arg_loc.release ();
9339 break;
9340 case CPP_DOT:
9341 /* Structure element reference. */
9342 c_parser_consume_token (parser);
9343 expr = default_function_array_conversion (expr_loc, expr);
9344 if (c_parser_next_token_is (parser, CPP_NAME))
9346 c_token *comp_tok = c_parser_peek_token (parser);
9347 ident = comp_tok->value;
9348 comp_loc = comp_tok->location;
9350 else
9352 c_parser_error (parser, "expected identifier");
9353 expr.set_error ();
9354 expr.original_code = ERROR_MARK;
9355 expr.original_type = NULL;
9356 return expr;
9358 start = expr.get_start ();
9359 finish = c_parser_peek_token (parser)->get_finish ();
9360 c_parser_consume_token (parser);
9361 expr.value = build_component_ref (op_loc, expr.value, ident,
9362 comp_loc);
9363 set_c_expr_source_range (&expr, start, finish);
9364 expr.original_code = ERROR_MARK;
9365 if (TREE_CODE (expr.value) != COMPONENT_REF)
9366 expr.original_type = NULL;
9367 else
9369 /* Remember the original type of a bitfield. */
9370 tree field = TREE_OPERAND (expr.value, 1);
9371 if (TREE_CODE (field) != FIELD_DECL)
9372 expr.original_type = NULL;
9373 else
9374 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9376 break;
9377 case CPP_DEREF:
9378 /* Structure element reference. */
9379 c_parser_consume_token (parser);
9380 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9381 if (c_parser_next_token_is (parser, CPP_NAME))
9383 c_token *comp_tok = c_parser_peek_token (parser);
9384 ident = comp_tok->value;
9385 comp_loc = comp_tok->location;
9387 else
9389 c_parser_error (parser, "expected identifier");
9390 expr.set_error ();
9391 expr.original_code = ERROR_MARK;
9392 expr.original_type = NULL;
9393 return expr;
9395 start = expr.get_start ();
9396 finish = c_parser_peek_token (parser)->get_finish ();
9397 c_parser_consume_token (parser);
9398 expr.value = build_component_ref (op_loc,
9399 build_indirect_ref (op_loc,
9400 expr.value,
9401 RO_ARROW),
9402 ident, comp_loc);
9403 set_c_expr_source_range (&expr, start, finish);
9404 expr.original_code = ERROR_MARK;
9405 if (TREE_CODE (expr.value) != COMPONENT_REF)
9406 expr.original_type = NULL;
9407 else
9409 /* Remember the original type of a bitfield. */
9410 tree field = TREE_OPERAND (expr.value, 1);
9411 if (TREE_CODE (field) != FIELD_DECL)
9412 expr.original_type = NULL;
9413 else
9414 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9416 break;
9417 case CPP_PLUS_PLUS:
9418 /* Postincrement. */
9419 start = expr.get_start ();
9420 finish = c_parser_peek_token (parser)->get_finish ();
9421 c_parser_consume_token (parser);
9422 /* If the expressions have array notations, we expand them. */
9423 if (flag_cilkplus
9424 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
9425 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
9426 else
9428 expr = default_function_array_read_conversion (expr_loc, expr);
9429 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9430 expr.value, false);
9432 set_c_expr_source_range (&expr, start, finish);
9433 expr.original_code = ERROR_MARK;
9434 expr.original_type = NULL;
9435 break;
9436 case CPP_MINUS_MINUS:
9437 /* Postdecrement. */
9438 start = expr.get_start ();
9439 finish = c_parser_peek_token (parser)->get_finish ();
9440 c_parser_consume_token (parser);
9441 /* If the expressions have array notations, we expand them. */
9442 if (flag_cilkplus
9443 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
9444 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
9445 else
9447 expr = default_function_array_read_conversion (expr_loc, expr);
9448 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9449 expr.value, false);
9451 set_c_expr_source_range (&expr, start, finish);
9452 expr.original_code = ERROR_MARK;
9453 expr.original_type = NULL;
9454 break;
9455 default:
9456 return expr;
9461 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9463 expression:
9464 assignment-expression
9465 expression , assignment-expression
9468 static struct c_expr
9469 c_parser_expression (c_parser *parser)
9471 location_t tloc = c_parser_peek_token (parser)->location;
9472 struct c_expr expr;
9473 expr = c_parser_expr_no_commas (parser, NULL);
9474 if (c_parser_next_token_is (parser, CPP_COMMA))
9475 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9476 while (c_parser_next_token_is (parser, CPP_COMMA))
9478 struct c_expr next;
9479 tree lhsval;
9480 location_t loc = c_parser_peek_token (parser)->location;
9481 location_t expr_loc;
9482 c_parser_consume_token (parser);
9483 expr_loc = c_parser_peek_token (parser)->location;
9484 lhsval = expr.value;
9485 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9486 lhsval = TREE_OPERAND (lhsval, 1);
9487 if (DECL_P (lhsval) || handled_component_p (lhsval))
9488 mark_exp_read (lhsval);
9489 next = c_parser_expr_no_commas (parser, NULL);
9490 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9491 expr.value = build_compound_expr (loc, expr.value, next.value);
9492 expr.original_code = COMPOUND_EXPR;
9493 expr.original_type = next.original_type;
9495 return expr;
9498 /* Parse an expression and convert functions or arrays to pointers and
9499 lvalues to rvalues. */
9501 static struct c_expr
9502 c_parser_expression_conv (c_parser *parser)
9504 struct c_expr expr;
9505 location_t loc = c_parser_peek_token (parser)->location;
9506 expr = c_parser_expression (parser);
9507 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9508 return expr;
9511 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9512 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9514 static inline void
9515 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9516 unsigned int idx)
9518 if (idx >= HOST_BITS_PER_INT)
9519 return;
9521 c_token *tok = c_parser_peek_token (parser);
9522 switch (tok->type)
9524 case CPP_NUMBER:
9525 case CPP_CHAR:
9526 case CPP_WCHAR:
9527 case CPP_CHAR16:
9528 case CPP_CHAR32:
9529 /* If a parameter is literal zero alone, remember it
9530 for -Wmemset-transposed-args warning. */
9531 if (integer_zerop (tok->value)
9532 && !TREE_OVERFLOW (tok->value)
9533 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9534 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9535 *literal_zero_mask |= 1U << idx;
9536 default:
9537 break;
9541 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9542 functions and arrays to pointers and lvalues to rvalues. If
9543 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9544 locations of function arguments into this vector.
9546 nonempty-expr-list:
9547 assignment-expression
9548 nonempty-expr-list , assignment-expression
9551 static vec<tree, va_gc> *
9552 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9553 vec<tree, va_gc> **p_orig_types,
9554 location_t *sizeof_arg_loc, tree *sizeof_arg,
9555 vec<location_t> *locations,
9556 unsigned int *literal_zero_mask)
9558 vec<tree, va_gc> *ret;
9559 vec<tree, va_gc> *orig_types;
9560 struct c_expr expr;
9561 unsigned int idx = 0;
9563 ret = make_tree_vector ();
9564 if (p_orig_types == NULL)
9565 orig_types = NULL;
9566 else
9567 orig_types = make_tree_vector ();
9569 if (literal_zero_mask)
9570 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9571 expr = c_parser_expr_no_commas (parser, NULL);
9572 if (convert_p)
9573 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9574 if (fold_p)
9575 expr.value = c_fully_fold (expr.value, false, NULL);
9576 ret->quick_push (expr.value);
9577 if (orig_types)
9578 orig_types->quick_push (expr.original_type);
9579 if (locations)
9580 locations->safe_push (expr.get_location ());
9581 if (sizeof_arg != NULL
9582 && expr.original_code == SIZEOF_EXPR)
9584 sizeof_arg[0] = c_last_sizeof_arg;
9585 sizeof_arg_loc[0] = c_last_sizeof_loc;
9587 while (c_parser_next_token_is (parser, CPP_COMMA))
9589 c_parser_consume_token (parser);
9590 if (literal_zero_mask)
9591 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9592 expr = c_parser_expr_no_commas (parser, NULL);
9593 if (convert_p)
9594 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9595 true);
9596 if (fold_p)
9597 expr.value = c_fully_fold (expr.value, false, NULL);
9598 vec_safe_push (ret, expr.value);
9599 if (orig_types)
9600 vec_safe_push (orig_types, expr.original_type);
9601 if (locations)
9602 locations->safe_push (expr.get_location ());
9603 if (++idx < 3
9604 && sizeof_arg != NULL
9605 && expr.original_code == SIZEOF_EXPR)
9607 sizeof_arg[idx] = c_last_sizeof_arg;
9608 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9611 if (orig_types)
9612 *p_orig_types = orig_types;
9613 return ret;
9616 /* Parse Objective-C-specific constructs. */
9618 /* Parse an objc-class-definition.
9620 objc-class-definition:
9621 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9622 objc-class-instance-variables[opt] objc-methodprotolist @end
9623 @implementation identifier objc-superclass[opt]
9624 objc-class-instance-variables[opt]
9625 @interface identifier ( identifier ) objc-protocol-refs[opt]
9626 objc-methodprotolist @end
9627 @interface identifier ( ) objc-protocol-refs[opt]
9628 objc-methodprotolist @end
9629 @implementation identifier ( identifier )
9631 objc-superclass:
9632 : identifier
9634 "@interface identifier (" must start "@interface identifier (
9635 identifier ) ...": objc-methodprotolist in the first production may
9636 not start with a parenthesized identifier as a declarator of a data
9637 definition with no declaration specifiers if the objc-superclass,
9638 objc-protocol-refs and objc-class-instance-variables are omitted. */
9640 static void
9641 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9643 bool iface_p;
9644 tree id1;
9645 tree superclass;
9646 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9647 iface_p = true;
9648 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9649 iface_p = false;
9650 else
9651 gcc_unreachable ();
9653 c_parser_consume_token (parser);
9654 if (c_parser_next_token_is_not (parser, CPP_NAME))
9656 c_parser_error (parser, "expected identifier");
9657 return;
9659 id1 = c_parser_peek_token (parser)->value;
9660 c_parser_consume_token (parser);
9661 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9663 /* We have a category or class extension. */
9664 tree id2;
9665 tree proto = NULL_TREE;
9666 matching_parens parens;
9667 parens.consume_open (parser);
9668 if (c_parser_next_token_is_not (parser, CPP_NAME))
9670 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9672 /* We have a class extension. */
9673 id2 = NULL_TREE;
9675 else
9677 c_parser_error (parser, "expected identifier or %<)%>");
9678 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9679 return;
9682 else
9684 id2 = c_parser_peek_token (parser)->value;
9685 c_parser_consume_token (parser);
9687 parens.skip_until_found_close (parser);
9688 if (!iface_p)
9690 objc_start_category_implementation (id1, id2);
9691 return;
9693 if (c_parser_next_token_is (parser, CPP_LESS))
9694 proto = c_parser_objc_protocol_refs (parser);
9695 objc_start_category_interface (id1, id2, proto, attributes);
9696 c_parser_objc_methodprotolist (parser);
9697 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9698 objc_finish_interface ();
9699 return;
9701 if (c_parser_next_token_is (parser, CPP_COLON))
9703 c_parser_consume_token (parser);
9704 if (c_parser_next_token_is_not (parser, CPP_NAME))
9706 c_parser_error (parser, "expected identifier");
9707 return;
9709 superclass = c_parser_peek_token (parser)->value;
9710 c_parser_consume_token (parser);
9712 else
9713 superclass = NULL_TREE;
9714 if (iface_p)
9716 tree proto = NULL_TREE;
9717 if (c_parser_next_token_is (parser, CPP_LESS))
9718 proto = c_parser_objc_protocol_refs (parser);
9719 objc_start_class_interface (id1, superclass, proto, attributes);
9721 else
9722 objc_start_class_implementation (id1, superclass);
9723 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9724 c_parser_objc_class_instance_variables (parser);
9725 if (iface_p)
9727 objc_continue_interface ();
9728 c_parser_objc_methodprotolist (parser);
9729 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9730 objc_finish_interface ();
9732 else
9734 objc_continue_implementation ();
9735 return;
9739 /* Parse objc-class-instance-variables.
9741 objc-class-instance-variables:
9742 { objc-instance-variable-decl-list[opt] }
9744 objc-instance-variable-decl-list:
9745 objc-visibility-spec
9746 objc-instance-variable-decl ;
9748 objc-instance-variable-decl-list objc-visibility-spec
9749 objc-instance-variable-decl-list objc-instance-variable-decl ;
9750 objc-instance-variable-decl-list ;
9752 objc-visibility-spec:
9753 @private
9754 @protected
9755 @public
9757 objc-instance-variable-decl:
9758 struct-declaration
9761 static void
9762 c_parser_objc_class_instance_variables (c_parser *parser)
9764 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9765 c_parser_consume_token (parser);
9766 while (c_parser_next_token_is_not (parser, CPP_EOF))
9768 tree decls;
9769 /* Parse any stray semicolon. */
9770 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9772 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9773 "extra semicolon");
9774 c_parser_consume_token (parser);
9775 continue;
9777 /* Stop if at the end of the instance variables. */
9778 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9780 c_parser_consume_token (parser);
9781 break;
9783 /* Parse any objc-visibility-spec. */
9784 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9786 c_parser_consume_token (parser);
9787 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9788 continue;
9790 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9792 c_parser_consume_token (parser);
9793 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9794 continue;
9796 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9798 c_parser_consume_token (parser);
9799 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9800 continue;
9802 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9804 c_parser_consume_token (parser);
9805 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9806 continue;
9808 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9810 c_parser_pragma (parser, pragma_external, NULL);
9811 continue;
9814 /* Parse some comma-separated declarations. */
9815 decls = c_parser_struct_declaration (parser);
9816 if (decls == NULL)
9818 /* There is a syntax error. We want to skip the offending
9819 tokens up to the next ';' (included) or '}'
9820 (excluded). */
9822 /* First, skip manually a ')' or ']'. This is because they
9823 reduce the nesting level, so c_parser_skip_until_found()
9824 wouldn't be able to skip past them. */
9825 c_token *token = c_parser_peek_token (parser);
9826 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9827 c_parser_consume_token (parser);
9829 /* Then, do the standard skipping. */
9830 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9832 /* We hopefully recovered. Start normal parsing again. */
9833 parser->error = false;
9834 continue;
9836 else
9838 /* Comma-separated instance variables are chained together
9839 in reverse order; add them one by one. */
9840 tree ivar = nreverse (decls);
9841 for (; ivar; ivar = DECL_CHAIN (ivar))
9842 objc_add_instance_variable (copy_node (ivar));
9844 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9848 /* Parse an objc-class-declaration.
9850 objc-class-declaration:
9851 @class identifier-list ;
9854 static void
9855 c_parser_objc_class_declaration (c_parser *parser)
9857 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9858 c_parser_consume_token (parser);
9859 /* Any identifiers, including those declared as type names, are OK
9860 here. */
9861 while (true)
9863 tree id;
9864 if (c_parser_next_token_is_not (parser, CPP_NAME))
9866 c_parser_error (parser, "expected identifier");
9867 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9868 parser->error = false;
9869 return;
9871 id = c_parser_peek_token (parser)->value;
9872 objc_declare_class (id);
9873 c_parser_consume_token (parser);
9874 if (c_parser_next_token_is (parser, CPP_COMMA))
9875 c_parser_consume_token (parser);
9876 else
9877 break;
9879 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9882 /* Parse an objc-alias-declaration.
9884 objc-alias-declaration:
9885 @compatibility_alias identifier identifier ;
9888 static void
9889 c_parser_objc_alias_declaration (c_parser *parser)
9891 tree id1, id2;
9892 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9893 c_parser_consume_token (parser);
9894 if (c_parser_next_token_is_not (parser, CPP_NAME))
9896 c_parser_error (parser, "expected identifier");
9897 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9898 return;
9900 id1 = c_parser_peek_token (parser)->value;
9901 c_parser_consume_token (parser);
9902 if (c_parser_next_token_is_not (parser, CPP_NAME))
9904 c_parser_error (parser, "expected identifier");
9905 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9906 return;
9908 id2 = c_parser_peek_token (parser)->value;
9909 c_parser_consume_token (parser);
9910 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9911 objc_declare_alias (id1, id2);
9914 /* Parse an objc-protocol-definition.
9916 objc-protocol-definition:
9917 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9918 @protocol identifier-list ;
9920 "@protocol identifier ;" should be resolved as "@protocol
9921 identifier-list ;": objc-methodprotolist may not start with a
9922 semicolon in the first alternative if objc-protocol-refs are
9923 omitted. */
9925 static void
9926 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9928 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9930 c_parser_consume_token (parser);
9931 if (c_parser_next_token_is_not (parser, CPP_NAME))
9933 c_parser_error (parser, "expected identifier");
9934 return;
9936 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9937 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9939 /* Any identifiers, including those declared as type names, are
9940 OK here. */
9941 while (true)
9943 tree id;
9944 if (c_parser_next_token_is_not (parser, CPP_NAME))
9946 c_parser_error (parser, "expected identifier");
9947 break;
9949 id = c_parser_peek_token (parser)->value;
9950 objc_declare_protocol (id, attributes);
9951 c_parser_consume_token (parser);
9952 if (c_parser_next_token_is (parser, CPP_COMMA))
9953 c_parser_consume_token (parser);
9954 else
9955 break;
9957 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9959 else
9961 tree id = c_parser_peek_token (parser)->value;
9962 tree proto = NULL_TREE;
9963 c_parser_consume_token (parser);
9964 if (c_parser_next_token_is (parser, CPP_LESS))
9965 proto = c_parser_objc_protocol_refs (parser);
9966 parser->objc_pq_context = true;
9967 objc_start_protocol (id, proto, attributes);
9968 c_parser_objc_methodprotolist (parser);
9969 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9970 parser->objc_pq_context = false;
9971 objc_finish_interface ();
9975 /* Parse an objc-method-type.
9977 objc-method-type:
9981 Return true if it is a class method (+) and false if it is
9982 an instance method (-).
9984 static inline bool
9985 c_parser_objc_method_type (c_parser *parser)
9987 switch (c_parser_peek_token (parser)->type)
9989 case CPP_PLUS:
9990 c_parser_consume_token (parser);
9991 return true;
9992 case CPP_MINUS:
9993 c_parser_consume_token (parser);
9994 return false;
9995 default:
9996 gcc_unreachable ();
10000 /* Parse an objc-method-definition.
10002 objc-method-definition:
10003 objc-method-type objc-method-decl ;[opt] compound-statement
10006 static void
10007 c_parser_objc_method_definition (c_parser *parser)
10009 bool is_class_method = c_parser_objc_method_type (parser);
10010 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
10011 parser->objc_pq_context = true;
10012 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10013 &expr);
10014 if (decl == error_mark_node)
10015 return; /* Bail here. */
10017 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10019 c_parser_consume_token (parser);
10020 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10021 "extra semicolon in method definition specified");
10024 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10026 c_parser_error (parser, "expected %<{%>");
10027 return;
10030 parser->objc_pq_context = false;
10031 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
10033 add_stmt (c_parser_compound_statement (parser));
10034 objc_finish_method_definition (current_function_decl);
10036 else
10038 /* This code is executed when we find a method definition
10039 outside of an @implementation context (or invalid for other
10040 reasons). Parse the method (to keep going) but do not emit
10041 any code.
10043 c_parser_compound_statement (parser);
10047 /* Parse an objc-methodprotolist.
10049 objc-methodprotolist:
10050 empty
10051 objc-methodprotolist objc-methodproto
10052 objc-methodprotolist declaration
10053 objc-methodprotolist ;
10054 @optional
10055 @required
10057 The declaration is a data definition, which may be missing
10058 declaration specifiers under the same rules and diagnostics as
10059 other data definitions outside functions, and the stray semicolon
10060 is diagnosed the same way as a stray semicolon outside a
10061 function. */
10063 static void
10064 c_parser_objc_methodprotolist (c_parser *parser)
10066 while (true)
10068 /* The list is terminated by @end. */
10069 switch (c_parser_peek_token (parser)->type)
10071 case CPP_SEMICOLON:
10072 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10073 "ISO C does not allow extra %<;%> outside of a function");
10074 c_parser_consume_token (parser);
10075 break;
10076 case CPP_PLUS:
10077 case CPP_MINUS:
10078 c_parser_objc_methodproto (parser);
10079 break;
10080 case CPP_PRAGMA:
10081 c_parser_pragma (parser, pragma_external, NULL);
10082 break;
10083 case CPP_EOF:
10084 return;
10085 default:
10086 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
10087 return;
10088 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
10089 c_parser_objc_at_property_declaration (parser);
10090 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
10092 objc_set_method_opt (true);
10093 c_parser_consume_token (parser);
10095 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
10097 objc_set_method_opt (false);
10098 c_parser_consume_token (parser);
10100 else
10101 c_parser_declaration_or_fndef (parser, false, false, true,
10102 false, true, NULL, vNULL);
10103 break;
10108 /* Parse an objc-methodproto.
10110 objc-methodproto:
10111 objc-method-type objc-method-decl ;
10114 static void
10115 c_parser_objc_methodproto (c_parser *parser)
10117 bool is_class_method = c_parser_objc_method_type (parser);
10118 tree decl, attributes = NULL_TREE;
10120 /* Remember protocol qualifiers in prototypes. */
10121 parser->objc_pq_context = true;
10122 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10123 NULL);
10124 /* Forget protocol qualifiers now. */
10125 parser->objc_pq_context = false;
10127 /* Do not allow the presence of attributes to hide an erroneous
10128 method implementation in the interface section. */
10129 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
10131 c_parser_error (parser, "expected %<;%>");
10132 return;
10135 if (decl != error_mark_node)
10136 objc_add_method_declaration (is_class_method, decl, attributes);
10138 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10141 /* If we are at a position that method attributes may be present, check that
10142 there are not any parsed already (a syntax error) and then collect any
10143 specified at the current location. Finally, if new attributes were present,
10144 check that the next token is legal ( ';' for decls and '{' for defs). */
10146 static bool
10147 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
10149 bool bad = false;
10150 if (*attributes)
10152 c_parser_error (parser,
10153 "method attributes must be specified at the end only");
10154 *attributes = NULL_TREE;
10155 bad = true;
10158 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10159 *attributes = c_parser_attributes (parser);
10161 /* If there were no attributes here, just report any earlier error. */
10162 if (*attributes == NULL_TREE || bad)
10163 return bad;
10165 /* If the attributes are followed by a ; or {, then just report any earlier
10166 error. */
10167 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10168 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10169 return bad;
10171 /* We've got attributes, but not at the end. */
10172 c_parser_error (parser,
10173 "expected %<;%> or %<{%> after method attribute definition");
10174 return true;
10177 /* Parse an objc-method-decl.
10179 objc-method-decl:
10180 ( objc-type-name ) objc-selector
10181 objc-selector
10182 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10183 objc-keyword-selector objc-optparmlist
10184 attributes
10186 objc-keyword-selector:
10187 objc-keyword-decl
10188 objc-keyword-selector objc-keyword-decl
10190 objc-keyword-decl:
10191 objc-selector : ( objc-type-name ) identifier
10192 objc-selector : identifier
10193 : ( objc-type-name ) identifier
10194 : identifier
10196 objc-optparmlist:
10197 objc-optparms objc-optellipsis
10199 objc-optparms:
10200 empty
10201 objc-opt-parms , parameter-declaration
10203 objc-optellipsis:
10204 empty
10205 , ...
10208 static tree
10209 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10210 tree *attributes, tree *expr)
10212 tree type = NULL_TREE;
10213 tree sel;
10214 tree parms = NULL_TREE;
10215 bool ellipsis = false;
10216 bool attr_err = false;
10218 *attributes = NULL_TREE;
10219 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10221 matching_parens parens;
10222 parens.consume_open (parser);
10223 type = c_parser_objc_type_name (parser);
10224 parens.skip_until_found_close (parser);
10226 sel = c_parser_objc_selector (parser);
10227 /* If there is no selector, or a colon follows, we have an
10228 objc-keyword-selector. If there is a selector, and a colon does
10229 not follow, that selector ends the objc-method-decl. */
10230 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10232 tree tsel = sel;
10233 tree list = NULL_TREE;
10234 while (true)
10236 tree atype = NULL_TREE, id, keyworddecl;
10237 tree param_attr = NULL_TREE;
10238 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10239 break;
10240 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10242 c_parser_consume_token (parser);
10243 atype = c_parser_objc_type_name (parser);
10244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10245 "expected %<)%>");
10247 /* New ObjC allows attributes on method parameters. */
10248 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10249 param_attr = c_parser_attributes (parser);
10250 if (c_parser_next_token_is_not (parser, CPP_NAME))
10252 c_parser_error (parser, "expected identifier");
10253 return error_mark_node;
10255 id = c_parser_peek_token (parser)->value;
10256 c_parser_consume_token (parser);
10257 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10258 list = chainon (list, keyworddecl);
10259 tsel = c_parser_objc_selector (parser);
10260 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10261 break;
10264 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10266 /* Parse the optional parameter list. Optional Objective-C
10267 method parameters follow the C syntax, and may include '...'
10268 to denote a variable number of arguments. */
10269 parms = make_node (TREE_LIST);
10270 while (c_parser_next_token_is (parser, CPP_COMMA))
10272 struct c_parm *parm;
10273 c_parser_consume_token (parser);
10274 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10276 ellipsis = true;
10277 c_parser_consume_token (parser);
10278 attr_err |= c_parser_objc_maybe_method_attributes
10279 (parser, attributes) ;
10280 break;
10282 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10283 if (parm == NULL)
10284 break;
10285 parms = chainon (parms,
10286 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10288 sel = list;
10290 else
10291 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10293 if (sel == NULL)
10295 c_parser_error (parser, "objective-c method declaration is expected");
10296 return error_mark_node;
10299 if (attr_err)
10300 return error_mark_node;
10302 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10305 /* Parse an objc-type-name.
10307 objc-type-name:
10308 objc-type-qualifiers[opt] type-name
10309 objc-type-qualifiers[opt]
10311 objc-type-qualifiers:
10312 objc-type-qualifier
10313 objc-type-qualifiers objc-type-qualifier
10315 objc-type-qualifier: one of
10316 in out inout bycopy byref oneway
10319 static tree
10320 c_parser_objc_type_name (c_parser *parser)
10322 tree quals = NULL_TREE;
10323 struct c_type_name *type_name = NULL;
10324 tree type = NULL_TREE;
10325 while (true)
10327 c_token *token = c_parser_peek_token (parser);
10328 if (token->type == CPP_KEYWORD
10329 && (token->keyword == RID_IN
10330 || token->keyword == RID_OUT
10331 || token->keyword == RID_INOUT
10332 || token->keyword == RID_BYCOPY
10333 || token->keyword == RID_BYREF
10334 || token->keyword == RID_ONEWAY))
10336 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10337 c_parser_consume_token (parser);
10339 else
10340 break;
10342 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10343 type_name = c_parser_type_name (parser);
10344 if (type_name)
10345 type = groktypename (type_name, NULL, NULL);
10347 /* If the type is unknown, and error has already been produced and
10348 we need to recover from the error. In that case, use NULL_TREE
10349 for the type, as if no type had been specified; this will use the
10350 default type ('id') which is good for error recovery. */
10351 if (type == error_mark_node)
10352 type = NULL_TREE;
10354 return build_tree_list (quals, type);
10357 /* Parse objc-protocol-refs.
10359 objc-protocol-refs:
10360 < identifier-list >
10363 static tree
10364 c_parser_objc_protocol_refs (c_parser *parser)
10366 tree list = NULL_TREE;
10367 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10368 c_parser_consume_token (parser);
10369 /* Any identifiers, including those declared as type names, are OK
10370 here. */
10371 while (true)
10373 tree id;
10374 if (c_parser_next_token_is_not (parser, CPP_NAME))
10376 c_parser_error (parser, "expected identifier");
10377 break;
10379 id = c_parser_peek_token (parser)->value;
10380 list = chainon (list, build_tree_list (NULL_TREE, id));
10381 c_parser_consume_token (parser);
10382 if (c_parser_next_token_is (parser, CPP_COMMA))
10383 c_parser_consume_token (parser);
10384 else
10385 break;
10387 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10388 return list;
10391 /* Parse an objc-try-catch-finally-statement.
10393 objc-try-catch-finally-statement:
10394 @try compound-statement objc-catch-list[opt]
10395 @try compound-statement objc-catch-list[opt] @finally compound-statement
10397 objc-catch-list:
10398 @catch ( objc-catch-parameter-declaration ) compound-statement
10399 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10401 objc-catch-parameter-declaration:
10402 parameter-declaration
10403 '...'
10405 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10407 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10408 for C++. Keep them in sync. */
10410 static void
10411 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10413 location_t location;
10414 tree stmt;
10416 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10417 c_parser_consume_token (parser);
10418 location = c_parser_peek_token (parser)->location;
10419 objc_maybe_warn_exceptions (location);
10420 stmt = c_parser_compound_statement (parser);
10421 objc_begin_try_stmt (location, stmt);
10423 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10425 struct c_parm *parm;
10426 tree parameter_declaration = error_mark_node;
10427 bool seen_open_paren = false;
10429 c_parser_consume_token (parser);
10430 matching_parens parens;
10431 if (!parens.require_open (parser))
10432 seen_open_paren = true;
10433 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10435 /* We have "@catch (...)" (where the '...' are literally
10436 what is in the code). Skip the '...'.
10437 parameter_declaration is set to NULL_TREE, and
10438 objc_being_catch_clauses() knows that that means
10439 '...'. */
10440 c_parser_consume_token (parser);
10441 parameter_declaration = NULL_TREE;
10443 else
10445 /* We have "@catch (NSException *exception)" or something
10446 like that. Parse the parameter declaration. */
10447 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10448 if (parm == NULL)
10449 parameter_declaration = error_mark_node;
10450 else
10451 parameter_declaration = grokparm (parm, NULL);
10453 if (seen_open_paren)
10454 parens.require_close (parser);
10455 else
10457 /* If there was no open parenthesis, we are recovering from
10458 an error, and we are trying to figure out what mistake
10459 the user has made. */
10461 /* If there is an immediate closing parenthesis, the user
10462 probably forgot the opening one (ie, they typed "@catch
10463 NSException *e)". Parse the closing parenthesis and keep
10464 going. */
10465 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10466 c_parser_consume_token (parser);
10468 /* If these is no immediate closing parenthesis, the user
10469 probably doesn't know that parenthesis are required at
10470 all (ie, they typed "@catch NSException *e"). So, just
10471 forget about the closing parenthesis and keep going. */
10473 objc_begin_catch_clause (parameter_declaration);
10474 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10475 c_parser_compound_statement_nostart (parser);
10476 objc_finish_catch_clause ();
10478 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10480 c_parser_consume_token (parser);
10481 location = c_parser_peek_token (parser)->location;
10482 stmt = c_parser_compound_statement (parser);
10483 objc_build_finally_clause (location, stmt);
10485 objc_finish_try_stmt ();
10488 /* Parse an objc-synchronized-statement.
10490 objc-synchronized-statement:
10491 @synchronized ( expression ) compound-statement
10494 static void
10495 c_parser_objc_synchronized_statement (c_parser *parser)
10497 location_t loc;
10498 tree expr, stmt;
10499 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10500 c_parser_consume_token (parser);
10501 loc = c_parser_peek_token (parser)->location;
10502 objc_maybe_warn_exceptions (loc);
10503 matching_parens parens;
10504 if (parens.require_open (parser))
10506 struct c_expr ce = c_parser_expression (parser);
10507 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10508 expr = ce.value;
10509 expr = c_fully_fold (expr, false, NULL);
10510 parens.skip_until_found_close (parser);
10512 else
10513 expr = error_mark_node;
10514 stmt = c_parser_compound_statement (parser);
10515 objc_build_synchronized (loc, expr, stmt);
10518 /* Parse an objc-selector; return NULL_TREE without an error if the
10519 next token is not an objc-selector.
10521 objc-selector:
10522 identifier
10523 one of
10524 enum struct union if else while do for switch case default
10525 break continue return goto asm sizeof typeof __alignof
10526 unsigned long const short volatile signed restrict _Complex
10527 in out inout bycopy byref oneway int char float double void _Bool
10528 _Atomic
10530 ??? Why this selection of keywords but not, for example, storage
10531 class specifiers? */
10533 static tree
10534 c_parser_objc_selector (c_parser *parser)
10536 c_token *token = c_parser_peek_token (parser);
10537 tree value = token->value;
10538 if (token->type == CPP_NAME)
10540 c_parser_consume_token (parser);
10541 return value;
10543 if (token->type != CPP_KEYWORD)
10544 return NULL_TREE;
10545 switch (token->keyword)
10547 case RID_ENUM:
10548 case RID_STRUCT:
10549 case RID_UNION:
10550 case RID_IF:
10551 case RID_ELSE:
10552 case RID_WHILE:
10553 case RID_DO:
10554 case RID_FOR:
10555 case RID_SWITCH:
10556 case RID_CASE:
10557 case RID_DEFAULT:
10558 case RID_BREAK:
10559 case RID_CONTINUE:
10560 case RID_RETURN:
10561 case RID_GOTO:
10562 case RID_ASM:
10563 case RID_SIZEOF:
10564 case RID_TYPEOF:
10565 case RID_ALIGNOF:
10566 case RID_UNSIGNED:
10567 case RID_LONG:
10568 case RID_CONST:
10569 case RID_SHORT:
10570 case RID_VOLATILE:
10571 case RID_SIGNED:
10572 case RID_RESTRICT:
10573 case RID_COMPLEX:
10574 case RID_IN:
10575 case RID_OUT:
10576 case RID_INOUT:
10577 case RID_BYCOPY:
10578 case RID_BYREF:
10579 case RID_ONEWAY:
10580 case RID_INT:
10581 case RID_CHAR:
10582 case RID_FLOAT:
10583 case RID_DOUBLE:
10584 CASE_RID_FLOATN_NX:
10585 case RID_VOID:
10586 case RID_BOOL:
10587 case RID_ATOMIC:
10588 case RID_AUTO_TYPE:
10589 case RID_INT_N_0:
10590 case RID_INT_N_1:
10591 case RID_INT_N_2:
10592 case RID_INT_N_3:
10593 c_parser_consume_token (parser);
10594 return value;
10595 default:
10596 return NULL_TREE;
10600 /* Parse an objc-selector-arg.
10602 objc-selector-arg:
10603 objc-selector
10604 objc-keywordname-list
10606 objc-keywordname-list:
10607 objc-keywordname
10608 objc-keywordname-list objc-keywordname
10610 objc-keywordname:
10611 objc-selector :
10615 static tree
10616 c_parser_objc_selector_arg (c_parser *parser)
10618 tree sel = c_parser_objc_selector (parser);
10619 tree list = NULL_TREE;
10620 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10621 return sel;
10622 while (true)
10624 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10625 return list;
10626 list = chainon (list, build_tree_list (sel, NULL_TREE));
10627 sel = c_parser_objc_selector (parser);
10628 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10629 break;
10631 return list;
10634 /* Parse an objc-receiver.
10636 objc-receiver:
10637 expression
10638 class-name
10639 type-name
10642 static tree
10643 c_parser_objc_receiver (c_parser *parser)
10645 location_t loc = c_parser_peek_token (parser)->location;
10647 if (c_parser_peek_token (parser)->type == CPP_NAME
10648 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10649 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10651 tree id = c_parser_peek_token (parser)->value;
10652 c_parser_consume_token (parser);
10653 return objc_get_class_reference (id);
10655 struct c_expr ce = c_parser_expression (parser);
10656 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10657 return c_fully_fold (ce.value, false, NULL);
10660 /* Parse objc-message-args.
10662 objc-message-args:
10663 objc-selector
10664 objc-keywordarg-list
10666 objc-keywordarg-list:
10667 objc-keywordarg
10668 objc-keywordarg-list objc-keywordarg
10670 objc-keywordarg:
10671 objc-selector : objc-keywordexpr
10672 : objc-keywordexpr
10675 static tree
10676 c_parser_objc_message_args (c_parser *parser)
10678 tree sel = c_parser_objc_selector (parser);
10679 tree list = NULL_TREE;
10680 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10681 return sel;
10682 while (true)
10684 tree keywordexpr;
10685 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10686 return error_mark_node;
10687 keywordexpr = c_parser_objc_keywordexpr (parser);
10688 list = chainon (list, build_tree_list (sel, keywordexpr));
10689 sel = c_parser_objc_selector (parser);
10690 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10691 break;
10693 return list;
10696 /* Parse an objc-keywordexpr.
10698 objc-keywordexpr:
10699 nonempty-expr-list
10702 static tree
10703 c_parser_objc_keywordexpr (c_parser *parser)
10705 tree ret;
10706 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10707 NULL, NULL, NULL, NULL);
10708 if (vec_safe_length (expr_list) == 1)
10710 /* Just return the expression, remove a level of
10711 indirection. */
10712 ret = (*expr_list)[0];
10714 else
10716 /* We have a comma expression, we will collapse later. */
10717 ret = build_tree_list_vec (expr_list);
10719 release_tree_vector (expr_list);
10720 return ret;
10723 /* A check, needed in several places, that ObjC interface, implementation or
10724 method definitions are not prefixed by incorrect items. */
10725 static bool
10726 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10727 struct c_declspecs *specs)
10729 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10730 || specs->typespec_kind != ctsk_none)
10732 c_parser_error (parser,
10733 "no type or storage class may be specified here,");
10734 c_parser_skip_to_end_of_block_or_statement (parser);
10735 return true;
10737 return false;
10740 /* Parse an Objective-C @property declaration. The syntax is:
10742 objc-property-declaration:
10743 '@property' objc-property-attributes[opt] struct-declaration ;
10745 objc-property-attributes:
10746 '(' objc-property-attribute-list ')'
10748 objc-property-attribute-list:
10749 objc-property-attribute
10750 objc-property-attribute-list, objc-property-attribute
10752 objc-property-attribute
10753 'getter' = identifier
10754 'setter' = identifier
10755 'readonly'
10756 'readwrite'
10757 'assign'
10758 'retain'
10759 'copy'
10760 'nonatomic'
10762 For example:
10763 @property NSString *name;
10764 @property (readonly) id object;
10765 @property (retain, nonatomic, getter=getTheName) id name;
10766 @property int a, b, c;
10768 PS: This function is identical to cp_parser_objc_at_propery_declaration
10769 for C++. Keep them in sync. */
10770 static void
10771 c_parser_objc_at_property_declaration (c_parser *parser)
10773 /* The following variables hold the attributes of the properties as
10774 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10775 seen. When we see an attribute, we set them to 'true' (if they
10776 are boolean properties) or to the identifier (if they have an
10777 argument, ie, for getter and setter). Note that here we only
10778 parse the list of attributes, check the syntax and accumulate the
10779 attributes that we find. objc_add_property_declaration() will
10780 then process the information. */
10781 bool property_assign = false;
10782 bool property_copy = false;
10783 tree property_getter_ident = NULL_TREE;
10784 bool property_nonatomic = false;
10785 bool property_readonly = false;
10786 bool property_readwrite = false;
10787 bool property_retain = false;
10788 tree property_setter_ident = NULL_TREE;
10790 /* 'properties' is the list of properties that we read. Usually a
10791 single one, but maybe more (eg, in "@property int a, b, c;" there
10792 are three). */
10793 tree properties;
10794 location_t loc;
10796 loc = c_parser_peek_token (parser)->location;
10797 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10799 c_parser_consume_token (parser); /* Eat '@property'. */
10801 /* Parse the optional attribute list... */
10802 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10804 matching_parens parens;
10806 /* Eat the '(' */
10807 parens.consume_open (parser);
10809 /* Property attribute keywords are valid now. */
10810 parser->objc_property_attr_context = true;
10812 while (true)
10814 bool syntax_error = false;
10815 c_token *token = c_parser_peek_token (parser);
10816 enum rid keyword;
10818 if (token->type != CPP_KEYWORD)
10820 if (token->type == CPP_CLOSE_PAREN)
10821 c_parser_error (parser, "expected identifier");
10822 else
10824 c_parser_consume_token (parser);
10825 c_parser_error (parser, "unknown property attribute");
10827 break;
10829 keyword = token->keyword;
10830 c_parser_consume_token (parser);
10831 switch (keyword)
10833 case RID_ASSIGN: property_assign = true; break;
10834 case RID_COPY: property_copy = true; break;
10835 case RID_NONATOMIC: property_nonatomic = true; break;
10836 case RID_READONLY: property_readonly = true; break;
10837 case RID_READWRITE: property_readwrite = true; break;
10838 case RID_RETAIN: property_retain = true; break;
10840 case RID_GETTER:
10841 case RID_SETTER:
10842 if (c_parser_next_token_is_not (parser, CPP_EQ))
10844 if (keyword == RID_GETTER)
10845 c_parser_error (parser,
10846 "missing %<=%> (after %<getter%> attribute)");
10847 else
10848 c_parser_error (parser,
10849 "missing %<=%> (after %<setter%> attribute)");
10850 syntax_error = true;
10851 break;
10853 c_parser_consume_token (parser); /* eat the = */
10854 if (c_parser_next_token_is_not (parser, CPP_NAME))
10856 c_parser_error (parser, "expected identifier");
10857 syntax_error = true;
10858 break;
10860 if (keyword == RID_SETTER)
10862 if (property_setter_ident != NULL_TREE)
10863 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10864 else
10865 property_setter_ident = c_parser_peek_token (parser)->value;
10866 c_parser_consume_token (parser);
10867 if (c_parser_next_token_is_not (parser, CPP_COLON))
10868 c_parser_error (parser, "setter name must terminate with %<:%>");
10869 else
10870 c_parser_consume_token (parser);
10872 else
10874 if (property_getter_ident != NULL_TREE)
10875 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10876 else
10877 property_getter_ident = c_parser_peek_token (parser)->value;
10878 c_parser_consume_token (parser);
10880 break;
10881 default:
10882 c_parser_error (parser, "unknown property attribute");
10883 syntax_error = true;
10884 break;
10887 if (syntax_error)
10888 break;
10890 if (c_parser_next_token_is (parser, CPP_COMMA))
10891 c_parser_consume_token (parser);
10892 else
10893 break;
10895 parser->objc_property_attr_context = false;
10896 parens.skip_until_found_close (parser);
10898 /* ... and the property declaration(s). */
10899 properties = c_parser_struct_declaration (parser);
10901 if (properties == error_mark_node)
10903 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10904 parser->error = false;
10905 return;
10908 if (properties == NULL_TREE)
10909 c_parser_error (parser, "expected identifier");
10910 else
10912 /* Comma-separated properties are chained together in
10913 reverse order; add them one by one. */
10914 properties = nreverse (properties);
10916 for (; properties; properties = TREE_CHAIN (properties))
10917 objc_add_property_declaration (loc, copy_node (properties),
10918 property_readonly, property_readwrite,
10919 property_assign, property_retain,
10920 property_copy, property_nonatomic,
10921 property_getter_ident, property_setter_ident);
10924 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10925 parser->error = false;
10928 /* Parse an Objective-C @synthesize declaration. The syntax is:
10930 objc-synthesize-declaration:
10931 @synthesize objc-synthesize-identifier-list ;
10933 objc-synthesize-identifier-list:
10934 objc-synthesize-identifier
10935 objc-synthesize-identifier-list, objc-synthesize-identifier
10937 objc-synthesize-identifier
10938 identifier
10939 identifier = identifier
10941 For example:
10942 @synthesize MyProperty;
10943 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10945 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10946 for C++. Keep them in sync.
10948 static void
10949 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10951 tree list = NULL_TREE;
10952 location_t loc;
10953 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10954 loc = c_parser_peek_token (parser)->location;
10956 c_parser_consume_token (parser);
10957 while (true)
10959 tree property, ivar;
10960 if (c_parser_next_token_is_not (parser, CPP_NAME))
10962 c_parser_error (parser, "expected identifier");
10963 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10964 /* Once we find the semicolon, we can resume normal parsing.
10965 We have to reset parser->error manually because
10966 c_parser_skip_until_found() won't reset it for us if the
10967 next token is precisely a semicolon. */
10968 parser->error = false;
10969 return;
10971 property = c_parser_peek_token (parser)->value;
10972 c_parser_consume_token (parser);
10973 if (c_parser_next_token_is (parser, CPP_EQ))
10975 c_parser_consume_token (parser);
10976 if (c_parser_next_token_is_not (parser, CPP_NAME))
10978 c_parser_error (parser, "expected identifier");
10979 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10980 parser->error = false;
10981 return;
10983 ivar = c_parser_peek_token (parser)->value;
10984 c_parser_consume_token (parser);
10986 else
10987 ivar = NULL_TREE;
10988 list = chainon (list, build_tree_list (ivar, property));
10989 if (c_parser_next_token_is (parser, CPP_COMMA))
10990 c_parser_consume_token (parser);
10991 else
10992 break;
10994 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10995 objc_add_synthesize_declaration (loc, list);
10998 /* Parse an Objective-C @dynamic declaration. The syntax is:
11000 objc-dynamic-declaration:
11001 @dynamic identifier-list ;
11003 For example:
11004 @dynamic MyProperty;
11005 @dynamic MyProperty, AnotherProperty;
11007 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
11008 for C++. Keep them in sync.
11010 static void
11011 c_parser_objc_at_dynamic_declaration (c_parser *parser)
11013 tree list = NULL_TREE;
11014 location_t loc;
11015 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
11016 loc = c_parser_peek_token (parser)->location;
11018 c_parser_consume_token (parser);
11019 while (true)
11021 tree property;
11022 if (c_parser_next_token_is_not (parser, CPP_NAME))
11024 c_parser_error (parser, "expected identifier");
11025 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11026 parser->error = false;
11027 return;
11029 property = c_parser_peek_token (parser)->value;
11030 list = chainon (list, build_tree_list (NULL_TREE, property));
11031 c_parser_consume_token (parser);
11032 if (c_parser_next_token_is (parser, CPP_COMMA))
11033 c_parser_consume_token (parser);
11034 else
11035 break;
11037 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11038 objc_add_dynamic_declaration (loc, list);
11042 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
11043 should be considered, statements. ALLOW_STMT is true if we're within
11044 the context of a function and such pragmas are to be allowed. Returns
11045 true if we actually parsed such a pragma. */
11047 static bool
11048 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
11050 unsigned int id;
11051 const char *construct = NULL;
11053 id = c_parser_peek_token (parser)->pragma_kind;
11054 gcc_assert (id != PRAGMA_NONE);
11056 switch (id)
11058 case PRAGMA_OACC_DECLARE:
11059 c_parser_oacc_declare (parser);
11060 return false;
11062 case PRAGMA_OACC_ENTER_DATA:
11063 if (context != pragma_compound)
11065 construct = "acc enter data";
11066 in_compound:
11067 if (context == pragma_stmt)
11069 error_at (c_parser_peek_token (parser)->location,
11070 "%<#pragma %s%> may only be used in compound "
11071 "statements", construct);
11072 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11073 return false;
11075 goto bad_stmt;
11077 c_parser_oacc_enter_exit_data (parser, true);
11078 return false;
11080 case PRAGMA_OACC_EXIT_DATA:
11081 if (context != pragma_compound)
11083 construct = "acc exit data";
11084 goto in_compound;
11086 c_parser_oacc_enter_exit_data (parser, false);
11087 return false;
11089 case PRAGMA_OACC_ROUTINE:
11090 if (context != pragma_external)
11092 error_at (c_parser_peek_token (parser)->location,
11093 "%<#pragma acc routine%> must be at file scope");
11094 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11095 return false;
11097 c_parser_oacc_routine (parser, context);
11098 return false;
11100 case PRAGMA_OACC_UPDATE:
11101 if (context != pragma_compound)
11103 construct = "acc update";
11104 goto in_compound;
11106 c_parser_oacc_update (parser);
11107 return false;
11109 case PRAGMA_OMP_BARRIER:
11110 if (context != pragma_compound)
11112 construct = "omp barrier";
11113 goto in_compound;
11115 c_parser_omp_barrier (parser);
11116 return false;
11118 case PRAGMA_OMP_FLUSH:
11119 if (context != pragma_compound)
11121 construct = "omp flush";
11122 goto in_compound;
11124 c_parser_omp_flush (parser);
11125 return false;
11127 case PRAGMA_OMP_TASKWAIT:
11128 if (context != pragma_compound)
11130 construct = "omp taskwait";
11131 goto in_compound;
11133 c_parser_omp_taskwait (parser);
11134 return false;
11136 case PRAGMA_OMP_TASKYIELD:
11137 if (context != pragma_compound)
11139 construct = "omp taskyield";
11140 goto in_compound;
11142 c_parser_omp_taskyield (parser);
11143 return false;
11145 case PRAGMA_OMP_CANCEL:
11146 if (context != pragma_compound)
11148 construct = "omp cancel";
11149 goto in_compound;
11151 c_parser_omp_cancel (parser);
11152 return false;
11154 case PRAGMA_OMP_CANCELLATION_POINT:
11155 c_parser_omp_cancellation_point (parser, context);
11156 return false;
11158 case PRAGMA_OMP_THREADPRIVATE:
11159 c_parser_omp_threadprivate (parser);
11160 return false;
11162 case PRAGMA_OMP_TARGET:
11163 return c_parser_omp_target (parser, context, if_p);
11165 case PRAGMA_OMP_END_DECLARE_TARGET:
11166 c_parser_omp_end_declare_target (parser);
11167 return false;
11169 case PRAGMA_OMP_SECTION:
11170 error_at (c_parser_peek_token (parser)->location,
11171 "%<#pragma omp section%> may only be used in "
11172 "%<#pragma omp sections%> construct");
11173 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11174 return false;
11176 case PRAGMA_OMP_DECLARE:
11177 c_parser_omp_declare (parser, context);
11178 return false;
11180 case PRAGMA_OMP_ORDERED:
11181 return c_parser_omp_ordered (parser, context, if_p);
11183 case PRAGMA_IVDEP:
11184 c_parser_consume_pragma (parser);
11185 c_parser_skip_to_pragma_eol (parser);
11186 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11187 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11188 && !c_parser_next_token_is_keyword (parser, RID_DO))
11190 c_parser_error (parser, "for, while or do statement expected");
11191 return false;
11193 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11194 c_parser_for_statement (parser, true, if_p);
11195 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11196 c_parser_while_statement (parser, true, if_p);
11197 else
11198 c_parser_do_statement (parser, true);
11199 return false;
11201 case PRAGMA_GCC_PCH_PREPROCESS:
11202 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11203 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11204 return false;
11206 case PRAGMA_CILK_SIMD:
11207 if (!c_parser_cilk_verify_simd (parser, context))
11208 return false;
11209 c_parser_consume_pragma (parser);
11210 c_parser_cilk_simd (parser, if_p);
11211 return false;
11212 case PRAGMA_CILK_GRAINSIZE:
11213 if (!flag_cilkplus)
11215 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
11216 " enabled");
11217 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11218 return false;
11220 if (context == pragma_external)
11222 error_at (c_parser_peek_token (parser)->location,
11223 "%<#pragma grainsize%> must be inside a function");
11224 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11225 return false;
11227 c_parser_cilk_grainsize (parser, if_p);
11228 return false;
11230 case PRAGMA_OACC_WAIT:
11231 if (context != pragma_compound)
11233 construct = "acc wait";
11234 goto in_compound;
11236 /* FALL THROUGH. */
11238 default:
11239 if (id < PRAGMA_FIRST_EXTERNAL)
11241 if (context != pragma_stmt && context != pragma_compound)
11243 bad_stmt:
11244 c_parser_error (parser, "expected declaration specifiers");
11245 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11246 return false;
11248 c_parser_omp_construct (parser, if_p);
11249 return true;
11251 break;
11254 c_parser_consume_pragma (parser);
11255 c_invoke_pragma_handler (id);
11257 /* Skip to EOL, but suppress any error message. Those will have been
11258 generated by the handler routine through calling error, as opposed
11259 to calling c_parser_error. */
11260 parser->error = true;
11261 c_parser_skip_to_pragma_eol (parser);
11263 return false;
11266 /* The interface the pragma parsers have to the lexer. */
11268 enum cpp_ttype
11269 pragma_lex (tree *value, location_t *loc)
11271 c_token *tok = c_parser_peek_token (the_parser);
11272 enum cpp_ttype ret = tok->type;
11274 *value = tok->value;
11275 if (loc)
11276 *loc = tok->location;
11278 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11279 ret = CPP_EOF;
11280 else
11282 if (ret == CPP_KEYWORD)
11283 ret = CPP_NAME;
11284 c_parser_consume_token (the_parser);
11287 return ret;
11290 static void
11291 c_parser_pragma_pch_preprocess (c_parser *parser)
11293 tree name = NULL;
11295 c_parser_consume_pragma (parser);
11296 if (c_parser_next_token_is (parser, CPP_STRING))
11298 name = c_parser_peek_token (parser)->value;
11299 c_parser_consume_token (parser);
11301 else
11302 c_parser_error (parser, "expected string literal");
11303 c_parser_skip_to_pragma_eol (parser);
11305 if (name)
11306 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11309 /* OpenACC and OpenMP parsing routines. */
11311 /* Returns name of the next clause.
11312 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11313 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11314 returned and the token is consumed. */
11316 static pragma_omp_clause
11317 c_parser_omp_clause_name (c_parser *parser)
11319 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11321 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11322 result = PRAGMA_OACC_CLAUSE_AUTO;
11323 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11324 result = PRAGMA_OMP_CLAUSE_IF;
11325 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11326 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11327 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11328 result = PRAGMA_OMP_CLAUSE_FOR;
11329 else if (c_parser_next_token_is (parser, CPP_NAME))
11331 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11333 switch (p[0])
11335 case 'a':
11336 if (!strcmp ("aligned", p))
11337 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11338 else if (!strcmp ("async", p))
11339 result = PRAGMA_OACC_CLAUSE_ASYNC;
11340 break;
11341 case 'c':
11342 if (!strcmp ("collapse", p))
11343 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11344 else if (!strcmp ("copy", p))
11345 result = PRAGMA_OACC_CLAUSE_COPY;
11346 else if (!strcmp ("copyin", p))
11347 result = PRAGMA_OMP_CLAUSE_COPYIN;
11348 else if (!strcmp ("copyout", p))
11349 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11350 else if (!strcmp ("copyprivate", p))
11351 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11352 else if (!strcmp ("create", p))
11353 result = PRAGMA_OACC_CLAUSE_CREATE;
11354 break;
11355 case 'd':
11356 if (!strcmp ("defaultmap", p))
11357 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11358 else if (!strcmp ("delete", p))
11359 result = PRAGMA_OACC_CLAUSE_DELETE;
11360 else if (!strcmp ("depend", p))
11361 result = PRAGMA_OMP_CLAUSE_DEPEND;
11362 else if (!strcmp ("device", p))
11363 result = PRAGMA_OMP_CLAUSE_DEVICE;
11364 else if (!strcmp ("deviceptr", p))
11365 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11366 else if (!strcmp ("device_resident", p))
11367 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11368 else if (!strcmp ("dist_schedule", p))
11369 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11370 break;
11371 case 'f':
11372 if (!strcmp ("final", p))
11373 result = PRAGMA_OMP_CLAUSE_FINAL;
11374 else if (!strcmp ("firstprivate", p))
11375 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11376 else if (!strcmp ("from", p))
11377 result = PRAGMA_OMP_CLAUSE_FROM;
11378 break;
11379 case 'g':
11380 if (!strcmp ("gang", p))
11381 result = PRAGMA_OACC_CLAUSE_GANG;
11382 else if (!strcmp ("grainsize", p))
11383 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11384 break;
11385 case 'h':
11386 if (!strcmp ("hint", p))
11387 result = PRAGMA_OMP_CLAUSE_HINT;
11388 else if (!strcmp ("host", p))
11389 result = PRAGMA_OACC_CLAUSE_HOST;
11390 break;
11391 case 'i':
11392 if (!strcmp ("inbranch", p))
11393 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11394 else if (!strcmp ("independent", p))
11395 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11396 else if (!strcmp ("is_device_ptr", p))
11397 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11398 break;
11399 case 'l':
11400 if (!strcmp ("lastprivate", p))
11401 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11402 else if (!strcmp ("linear", p))
11403 result = PRAGMA_OMP_CLAUSE_LINEAR;
11404 else if (!strcmp ("link", p))
11405 result = PRAGMA_OMP_CLAUSE_LINK;
11406 break;
11407 case 'm':
11408 if (!strcmp ("map", p))
11409 result = PRAGMA_OMP_CLAUSE_MAP;
11410 else if (!strcmp ("mergeable", p))
11411 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11412 else if (flag_cilkplus && !strcmp ("mask", p))
11413 result = PRAGMA_CILK_CLAUSE_MASK;
11414 break;
11415 case 'n':
11416 if (!strcmp ("nogroup", p))
11417 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11418 else if (!strcmp ("notinbranch", p))
11419 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11420 else if (!strcmp ("nowait", p))
11421 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11422 else if (!strcmp ("num_gangs", p))
11423 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11424 else if (!strcmp ("num_tasks", p))
11425 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11426 else if (!strcmp ("num_teams", p))
11427 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11428 else if (!strcmp ("num_threads", p))
11429 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11430 else if (!strcmp ("num_workers", p))
11431 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11432 else if (flag_cilkplus && !strcmp ("nomask", p))
11433 result = PRAGMA_CILK_CLAUSE_NOMASK;
11434 break;
11435 case 'o':
11436 if (!strcmp ("ordered", p))
11437 result = PRAGMA_OMP_CLAUSE_ORDERED;
11438 break;
11439 case 'p':
11440 if (!strcmp ("parallel", p))
11441 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11442 else if (!strcmp ("present", p))
11443 result = PRAGMA_OACC_CLAUSE_PRESENT;
11444 else if (!strcmp ("present_or_copy", p)
11445 || !strcmp ("pcopy", p))
11446 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11447 else if (!strcmp ("present_or_copyin", p)
11448 || !strcmp ("pcopyin", p))
11449 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11450 else if (!strcmp ("present_or_copyout", p)
11451 || !strcmp ("pcopyout", p))
11452 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11453 else if (!strcmp ("present_or_create", p)
11454 || !strcmp ("pcreate", p))
11455 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11456 else if (!strcmp ("priority", p))
11457 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11458 else if (!strcmp ("private", p))
11459 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11460 else if (!strcmp ("proc_bind", p))
11461 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11462 break;
11463 case 'r':
11464 if (!strcmp ("reduction", p))
11465 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11466 break;
11467 case 's':
11468 if (!strcmp ("safelen", p))
11469 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11470 else if (!strcmp ("schedule", p))
11471 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11472 else if (!strcmp ("sections", p))
11473 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11474 else if (!strcmp ("seq", p))
11475 result = PRAGMA_OACC_CLAUSE_SEQ;
11476 else if (!strcmp ("shared", p))
11477 result = PRAGMA_OMP_CLAUSE_SHARED;
11478 else if (!strcmp ("simd", p))
11479 result = PRAGMA_OMP_CLAUSE_SIMD;
11480 else if (!strcmp ("simdlen", p))
11481 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11482 else if (!strcmp ("self", p))
11483 result = PRAGMA_OACC_CLAUSE_SELF;
11484 break;
11485 case 't':
11486 if (!strcmp ("taskgroup", p))
11487 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11488 else if (!strcmp ("thread_limit", p))
11489 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11490 else if (!strcmp ("threads", p))
11491 result = PRAGMA_OMP_CLAUSE_THREADS;
11492 else if (!strcmp ("tile", p))
11493 result = PRAGMA_OACC_CLAUSE_TILE;
11494 else if (!strcmp ("to", p))
11495 result = PRAGMA_OMP_CLAUSE_TO;
11496 break;
11497 case 'u':
11498 if (!strcmp ("uniform", p))
11499 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11500 else if (!strcmp ("untied", p))
11501 result = PRAGMA_OMP_CLAUSE_UNTIED;
11502 else if (!strcmp ("use_device", p))
11503 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11504 else if (!strcmp ("use_device_ptr", p))
11505 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11506 break;
11507 case 'v':
11508 if (!strcmp ("vector", p))
11509 result = PRAGMA_OACC_CLAUSE_VECTOR;
11510 else if (!strcmp ("vector_length", p))
11511 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11512 else if (flag_cilkplus && !strcmp ("vectorlength", p))
11513 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
11514 break;
11515 case 'w':
11516 if (!strcmp ("wait", p))
11517 result = PRAGMA_OACC_CLAUSE_WAIT;
11518 else if (!strcmp ("worker", p))
11519 result = PRAGMA_OACC_CLAUSE_WORKER;
11520 break;
11524 if (result != PRAGMA_OMP_CLAUSE_NONE)
11525 c_parser_consume_token (parser);
11527 return result;
11530 /* Validate that a clause of the given type does not already exist. */
11532 static void
11533 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11534 const char *name)
11536 tree c;
11538 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11539 if (OMP_CLAUSE_CODE (c) == code)
11541 location_t loc = OMP_CLAUSE_LOCATION (c);
11542 error_at (loc, "too many %qs clauses", name);
11543 break;
11547 /* OpenACC 2.0
11548 Parse wait clause or wait directive parameters. */
11550 static tree
11551 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11553 vec<tree, va_gc> *args;
11554 tree t, args_tree;
11556 matching_parens parens;
11557 if (!parens.require_open (parser))
11558 return list;
11560 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11562 if (args->length () == 0)
11564 c_parser_error (parser, "expected integer expression before ')'");
11565 release_tree_vector (args);
11566 return list;
11569 args_tree = build_tree_list_vec (args);
11571 for (t = args_tree; t; t = TREE_CHAIN (t))
11573 tree targ = TREE_VALUE (t);
11575 if (targ != error_mark_node)
11577 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11579 c_parser_error (parser, "expression must be integral");
11580 targ = error_mark_node;
11582 else
11584 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11586 OMP_CLAUSE_DECL (c) = targ;
11587 OMP_CLAUSE_CHAIN (c) = list;
11588 list = c;
11593 release_tree_vector (args);
11594 parens.require_close (parser);
11595 return list;
11598 /* OpenACC 2.0, OpenMP 2.5:
11599 variable-list:
11600 identifier
11601 variable-list , identifier
11603 If KIND is nonzero, create the appropriate node and install the
11604 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11605 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11607 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11608 return the list created. */
11610 static tree
11611 c_parser_omp_variable_list (c_parser *parser,
11612 location_t clause_loc,
11613 enum omp_clause_code kind, tree list)
11615 if (c_parser_next_token_is_not (parser, CPP_NAME)
11616 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11617 c_parser_error (parser, "expected identifier");
11619 while (c_parser_next_token_is (parser, CPP_NAME)
11620 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11622 tree t = lookup_name (c_parser_peek_token (parser)->value);
11624 if (t == NULL_TREE)
11626 undeclared_variable (c_parser_peek_token (parser)->location,
11627 c_parser_peek_token (parser)->value);
11628 t = error_mark_node;
11631 c_parser_consume_token (parser);
11633 if (t == error_mark_node)
11635 else if (kind != 0)
11637 switch (kind)
11639 case OMP_CLAUSE__CACHE_:
11640 /* The OpenACC cache directive explicitly only allows "array
11641 elements or subarrays". */
11642 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11644 c_parser_error (parser, "expected %<[%>");
11645 t = error_mark_node;
11646 break;
11648 /* FALLTHROUGH */
11649 case OMP_CLAUSE_MAP:
11650 case OMP_CLAUSE_FROM:
11651 case OMP_CLAUSE_TO:
11652 while (c_parser_next_token_is (parser, CPP_DOT))
11654 location_t op_loc = c_parser_peek_token (parser)->location;
11655 c_parser_consume_token (parser);
11656 if (!c_parser_next_token_is (parser, CPP_NAME))
11658 c_parser_error (parser, "expected identifier");
11659 t = error_mark_node;
11660 break;
11663 c_token *comp_tok = c_parser_peek_token (parser);
11664 tree ident = comp_tok->value;
11665 location_t comp_loc = comp_tok->location;
11666 c_parser_consume_token (parser);
11667 t = build_component_ref (op_loc, t, ident, comp_loc);
11669 /* FALLTHROUGH */
11670 case OMP_CLAUSE_DEPEND:
11671 case OMP_CLAUSE_REDUCTION:
11672 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11674 tree low_bound = NULL_TREE, length = NULL_TREE;
11676 c_parser_consume_token (parser);
11677 if (!c_parser_next_token_is (parser, CPP_COLON))
11679 location_t expr_loc
11680 = c_parser_peek_token (parser)->location;
11681 c_expr expr = c_parser_expression (parser);
11682 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11683 false, true);
11684 low_bound = expr.value;
11686 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11687 length = integer_one_node;
11688 else
11690 /* Look for `:'. */
11691 if (!c_parser_require (parser, CPP_COLON,
11692 "expected %<:%>"))
11694 t = error_mark_node;
11695 break;
11697 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11699 location_t expr_loc
11700 = c_parser_peek_token (parser)->location;
11701 c_expr expr = c_parser_expression (parser);
11702 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11703 false, true);
11704 length = expr.value;
11707 /* Look for the closing `]'. */
11708 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11709 "expected %<]%>"))
11711 t = error_mark_node;
11712 break;
11715 t = tree_cons (low_bound, length, t);
11717 break;
11718 default:
11719 break;
11722 if (t != error_mark_node)
11724 tree u = build_omp_clause (clause_loc, kind);
11725 OMP_CLAUSE_DECL (u) = t;
11726 OMP_CLAUSE_CHAIN (u) = list;
11727 list = u;
11730 else
11731 list = tree_cons (t, NULL_TREE, list);
11733 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11734 break;
11736 c_parser_consume_token (parser);
11739 return list;
11742 /* Similarly, but expect leading and trailing parenthesis. This is a very
11743 common case for OpenACC and OpenMP clauses. */
11745 static tree
11746 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11747 tree list)
11749 /* The clauses location. */
11750 location_t loc = c_parser_peek_token (parser)->location;
11752 matching_parens parens;
11753 if (parens.require_open (parser))
11755 list = c_parser_omp_variable_list (parser, loc, kind, list);
11756 parens.skip_until_found_close (parser);
11758 return list;
11761 /* OpenACC 2.0:
11762 copy ( variable-list )
11763 copyin ( variable-list )
11764 copyout ( variable-list )
11765 create ( variable-list )
11766 delete ( variable-list )
11767 present ( variable-list )
11768 present_or_copy ( variable-list )
11769 pcopy ( variable-list )
11770 present_or_copyin ( variable-list )
11771 pcopyin ( variable-list )
11772 present_or_copyout ( variable-list )
11773 pcopyout ( variable-list )
11774 present_or_create ( variable-list )
11775 pcreate ( variable-list ) */
11777 static tree
11778 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11779 tree list)
11781 enum gomp_map_kind kind;
11782 switch (c_kind)
11784 case PRAGMA_OACC_CLAUSE_COPY:
11785 kind = GOMP_MAP_FORCE_TOFROM;
11786 break;
11787 case PRAGMA_OACC_CLAUSE_COPYIN:
11788 kind = GOMP_MAP_FORCE_TO;
11789 break;
11790 case PRAGMA_OACC_CLAUSE_COPYOUT:
11791 kind = GOMP_MAP_FORCE_FROM;
11792 break;
11793 case PRAGMA_OACC_CLAUSE_CREATE:
11794 kind = GOMP_MAP_FORCE_ALLOC;
11795 break;
11796 case PRAGMA_OACC_CLAUSE_DELETE:
11797 kind = GOMP_MAP_DELETE;
11798 break;
11799 case PRAGMA_OACC_CLAUSE_DEVICE:
11800 kind = GOMP_MAP_FORCE_TO;
11801 break;
11802 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11803 kind = GOMP_MAP_DEVICE_RESIDENT;
11804 break;
11805 case PRAGMA_OACC_CLAUSE_HOST:
11806 case PRAGMA_OACC_CLAUSE_SELF:
11807 kind = GOMP_MAP_FORCE_FROM;
11808 break;
11809 case PRAGMA_OACC_CLAUSE_LINK:
11810 kind = GOMP_MAP_LINK;
11811 break;
11812 case PRAGMA_OACC_CLAUSE_PRESENT:
11813 kind = GOMP_MAP_FORCE_PRESENT;
11814 break;
11815 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11816 kind = GOMP_MAP_TOFROM;
11817 break;
11818 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11819 kind = GOMP_MAP_TO;
11820 break;
11821 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11822 kind = GOMP_MAP_FROM;
11823 break;
11824 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11825 kind = GOMP_MAP_ALLOC;
11826 break;
11827 default:
11828 gcc_unreachable ();
11830 tree nl, c;
11831 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11833 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11834 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11836 return nl;
11839 /* OpenACC 2.0:
11840 deviceptr ( variable-list ) */
11842 static tree
11843 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11845 location_t loc = c_parser_peek_token (parser)->location;
11846 tree vars, t;
11848 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11849 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11850 variable-list must only allow for pointer variables. */
11851 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11852 for (t = vars; t && t; t = TREE_CHAIN (t))
11854 tree v = TREE_PURPOSE (t);
11856 /* FIXME diagnostics: Ideally we should keep individual
11857 locations for all the variables in the var list to make the
11858 following errors more precise. Perhaps
11859 c_parser_omp_var_list_parens() should construct a list of
11860 locations to go along with the var list. */
11862 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11863 error_at (loc, "%qD is not a variable", v);
11864 else if (TREE_TYPE (v) == error_mark_node)
11866 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11867 error_at (loc, "%qD is not a pointer variable", v);
11869 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11870 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11871 OMP_CLAUSE_DECL (u) = v;
11872 OMP_CLAUSE_CHAIN (u) = list;
11873 list = u;
11876 return list;
11879 /* OpenACC 2.0, OpenMP 3.0:
11880 collapse ( constant-expression ) */
11882 static tree
11883 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11885 tree c, num = error_mark_node;
11886 HOST_WIDE_INT n;
11887 location_t loc;
11889 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11890 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11892 loc = c_parser_peek_token (parser)->location;
11893 matching_parens parens;
11894 if (parens.require_open (parser))
11896 num = c_parser_expr_no_commas (parser, NULL).value;
11897 parens.skip_until_found_close (parser);
11899 if (num == error_mark_node)
11900 return list;
11901 mark_exp_read (num);
11902 num = c_fully_fold (num, false, NULL);
11903 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11904 || !tree_fits_shwi_p (num)
11905 || (n = tree_to_shwi (num)) <= 0
11906 || (int) n != n)
11908 error_at (loc,
11909 "collapse argument needs positive constant integer expression");
11910 return list;
11912 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11913 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11914 OMP_CLAUSE_CHAIN (c) = list;
11915 return c;
11918 /* OpenMP 2.5:
11919 copyin ( variable-list ) */
11921 static tree
11922 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11924 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11927 /* OpenMP 2.5:
11928 copyprivate ( variable-list ) */
11930 static tree
11931 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11933 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11936 /* OpenMP 2.5:
11937 default ( none | shared )
11939 OpenACC:
11940 default ( none | present ) */
11942 static tree
11943 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11945 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11946 location_t loc = c_parser_peek_token (parser)->location;
11947 tree c;
11949 matching_parens parens;
11950 if (!parens.require_open (parser))
11951 return list;
11952 if (c_parser_next_token_is (parser, CPP_NAME))
11954 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11956 switch (p[0])
11958 case 'n':
11959 if (strcmp ("none", p) != 0)
11960 goto invalid_kind;
11961 kind = OMP_CLAUSE_DEFAULT_NONE;
11962 break;
11964 case 'p':
11965 if (strcmp ("present", p) != 0 || !is_oacc)
11966 goto invalid_kind;
11967 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11968 break;
11970 case 's':
11971 if (strcmp ("shared", p) != 0 || is_oacc)
11972 goto invalid_kind;
11973 kind = OMP_CLAUSE_DEFAULT_SHARED;
11974 break;
11976 default:
11977 goto invalid_kind;
11980 c_parser_consume_token (parser);
11982 else
11984 invalid_kind:
11985 if (is_oacc)
11986 c_parser_error (parser, "expected %<none%> or %<present%>");
11987 else
11988 c_parser_error (parser, "expected %<none%> or %<shared%>");
11990 parens.skip_until_found_close (parser);
11992 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11993 return list;
11995 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11996 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11997 OMP_CLAUSE_CHAIN (c) = list;
11998 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
12000 return c;
12003 /* OpenMP 2.5:
12004 firstprivate ( variable-list ) */
12006 static tree
12007 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
12009 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
12012 /* OpenMP 3.1:
12013 final ( expression ) */
12015 static tree
12016 c_parser_omp_clause_final (c_parser *parser, tree list)
12018 location_t loc = c_parser_peek_token (parser)->location;
12019 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12021 tree t = c_parser_paren_condition (parser);
12022 tree c;
12024 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
12026 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
12027 OMP_CLAUSE_FINAL_EXPR (c) = t;
12028 OMP_CLAUSE_CHAIN (c) = list;
12029 list = c;
12031 else
12032 c_parser_error (parser, "expected %<(%>");
12034 return list;
12037 /* OpenACC, OpenMP 2.5:
12038 if ( expression )
12040 OpenMP 4.5:
12041 if ( directive-name-modifier : expression )
12043 directive-name-modifier:
12044 parallel | task | taskloop | target data | target | target update
12045 | target enter data | target exit data */
12047 static tree
12048 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
12050 location_t location = c_parser_peek_token (parser)->location;
12051 enum tree_code if_modifier = ERROR_MARK;
12053 matching_parens parens;
12054 if (!parens.require_open (parser))
12055 return list;
12057 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
12059 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12060 int n = 2;
12061 if (strcmp (p, "parallel") == 0)
12062 if_modifier = OMP_PARALLEL;
12063 else if (strcmp (p, "task") == 0)
12064 if_modifier = OMP_TASK;
12065 else if (strcmp (p, "taskloop") == 0)
12066 if_modifier = OMP_TASKLOOP;
12067 else if (strcmp (p, "target") == 0)
12069 if_modifier = OMP_TARGET;
12070 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12072 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
12073 if (strcmp ("data", p) == 0)
12074 if_modifier = OMP_TARGET_DATA;
12075 else if (strcmp ("update", p) == 0)
12076 if_modifier = OMP_TARGET_UPDATE;
12077 else if (strcmp ("enter", p) == 0)
12078 if_modifier = OMP_TARGET_ENTER_DATA;
12079 else if (strcmp ("exit", p) == 0)
12080 if_modifier = OMP_TARGET_EXIT_DATA;
12081 if (if_modifier != OMP_TARGET)
12083 n = 3;
12084 c_parser_consume_token (parser);
12086 else
12088 location_t loc = c_parser_peek_2nd_token (parser)->location;
12089 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
12090 "or %<exit%>");
12091 if_modifier = ERROR_MARK;
12093 if (if_modifier == OMP_TARGET_ENTER_DATA
12094 || if_modifier == OMP_TARGET_EXIT_DATA)
12096 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12098 p = IDENTIFIER_POINTER
12099 (c_parser_peek_2nd_token (parser)->value);
12100 if (strcmp ("data", p) == 0)
12101 n = 4;
12103 if (n == 4)
12104 c_parser_consume_token (parser);
12105 else
12107 location_t loc
12108 = c_parser_peek_2nd_token (parser)->location;
12109 error_at (loc, "expected %<data%>");
12110 if_modifier = ERROR_MARK;
12115 if (if_modifier != ERROR_MARK)
12117 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12119 c_parser_consume_token (parser);
12120 c_parser_consume_token (parser);
12122 else
12124 if (n > 2)
12126 location_t loc = c_parser_peek_2nd_token (parser)->location;
12127 error_at (loc, "expected %<:%>");
12129 if_modifier = ERROR_MARK;
12134 tree t = c_parser_condition (parser), c;
12135 parens.skip_until_found_close (parser);
12137 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12138 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12140 if (if_modifier != ERROR_MARK
12141 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12143 const char *p = NULL;
12144 switch (if_modifier)
12146 case OMP_PARALLEL: p = "parallel"; break;
12147 case OMP_TASK: p = "task"; break;
12148 case OMP_TASKLOOP: p = "taskloop"; break;
12149 case OMP_TARGET_DATA: p = "target data"; break;
12150 case OMP_TARGET: p = "target"; break;
12151 case OMP_TARGET_UPDATE: p = "target update"; break;
12152 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12153 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12154 default: gcc_unreachable ();
12156 error_at (location, "too many %<if%> clauses with %qs modifier",
12158 return list;
12160 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12162 if (!is_omp)
12163 error_at (location, "too many %<if%> clauses");
12164 else
12165 error_at (location, "too many %<if%> clauses without modifier");
12166 return list;
12168 else if (if_modifier == ERROR_MARK
12169 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12171 error_at (location, "if any %<if%> clause has modifier, then all "
12172 "%<if%> clauses have to use modifier");
12173 return list;
12177 c = build_omp_clause (location, OMP_CLAUSE_IF);
12178 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12179 OMP_CLAUSE_IF_EXPR (c) = t;
12180 OMP_CLAUSE_CHAIN (c) = list;
12181 return c;
12184 /* OpenMP 2.5:
12185 lastprivate ( variable-list ) */
12187 static tree
12188 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12190 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12193 /* OpenMP 3.1:
12194 mergeable */
12196 static tree
12197 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12199 tree c;
12201 /* FIXME: Should we allow duplicates? */
12202 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12204 c = build_omp_clause (c_parser_peek_token (parser)->location,
12205 OMP_CLAUSE_MERGEABLE);
12206 OMP_CLAUSE_CHAIN (c) = list;
12208 return c;
12211 /* OpenMP 2.5:
12212 nowait */
12214 static tree
12215 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12217 tree c;
12218 location_t loc = c_parser_peek_token (parser)->location;
12220 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12222 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12223 OMP_CLAUSE_CHAIN (c) = list;
12224 return c;
12227 /* OpenMP 2.5:
12228 num_threads ( expression ) */
12230 static tree
12231 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12233 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12234 matching_parens parens;
12235 if (parens.require_open (parser))
12237 location_t expr_loc = c_parser_peek_token (parser)->location;
12238 c_expr expr = c_parser_expression (parser);
12239 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12240 tree c, t = expr.value;
12241 t = c_fully_fold (t, false, NULL);
12243 parens.skip_until_found_close (parser);
12245 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12247 c_parser_error (parser, "expected integer expression");
12248 return list;
12251 /* Attempt to statically determine when the number isn't positive. */
12252 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12253 build_int_cst (TREE_TYPE (t), 0));
12254 protected_set_expr_location (c, expr_loc);
12255 if (c == boolean_true_node)
12257 warning_at (expr_loc, 0,
12258 "%<num_threads%> value must be positive");
12259 t = integer_one_node;
12262 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12264 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12265 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12266 OMP_CLAUSE_CHAIN (c) = list;
12267 list = c;
12270 return list;
12273 /* OpenMP 4.5:
12274 num_tasks ( expression ) */
12276 static tree
12277 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12279 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12280 matching_parens parens;
12281 if (parens.require_open (parser))
12283 location_t expr_loc = c_parser_peek_token (parser)->location;
12284 c_expr expr = c_parser_expression (parser);
12285 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12286 tree c, t = expr.value;
12287 t = c_fully_fold (t, false, NULL);
12289 parens.skip_until_found_close (parser);
12291 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12293 c_parser_error (parser, "expected integer expression");
12294 return list;
12297 /* Attempt to statically determine when the number isn't positive. */
12298 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12299 build_int_cst (TREE_TYPE (t), 0));
12300 if (CAN_HAVE_LOCATION_P (c))
12301 SET_EXPR_LOCATION (c, expr_loc);
12302 if (c == boolean_true_node)
12304 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12305 t = integer_one_node;
12308 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12310 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12311 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12312 OMP_CLAUSE_CHAIN (c) = list;
12313 list = c;
12316 return list;
12319 /* OpenMP 4.5:
12320 grainsize ( expression ) */
12322 static tree
12323 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12325 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12326 matching_parens parens;
12327 if (parens.require_open (parser))
12329 location_t expr_loc = c_parser_peek_token (parser)->location;
12330 c_expr expr = c_parser_expression (parser);
12331 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12332 tree c, t = expr.value;
12333 t = c_fully_fold (t, false, NULL);
12335 parens.skip_until_found_close (parser);
12337 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12339 c_parser_error (parser, "expected integer expression");
12340 return list;
12343 /* Attempt to statically determine when the number isn't positive. */
12344 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12345 build_int_cst (TREE_TYPE (t), 0));
12346 if (CAN_HAVE_LOCATION_P (c))
12347 SET_EXPR_LOCATION (c, expr_loc);
12348 if (c == boolean_true_node)
12350 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12351 t = integer_one_node;
12354 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12356 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12357 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12358 OMP_CLAUSE_CHAIN (c) = list;
12359 list = c;
12362 return list;
12365 /* OpenMP 4.5:
12366 priority ( expression ) */
12368 static tree
12369 c_parser_omp_clause_priority (c_parser *parser, tree list)
12371 location_t priority_loc = c_parser_peek_token (parser)->location;
12372 matching_parens parens;
12373 if (parens.require_open (parser))
12375 location_t expr_loc = c_parser_peek_token (parser)->location;
12376 c_expr expr = c_parser_expression (parser);
12377 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12378 tree c, t = expr.value;
12379 t = c_fully_fold (t, false, NULL);
12381 parens.skip_until_found_close (parser);
12383 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12385 c_parser_error (parser, "expected integer expression");
12386 return list;
12389 /* Attempt to statically determine when the number isn't
12390 non-negative. */
12391 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12392 build_int_cst (TREE_TYPE (t), 0));
12393 if (CAN_HAVE_LOCATION_P (c))
12394 SET_EXPR_LOCATION (c, expr_loc);
12395 if (c == boolean_true_node)
12397 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12398 t = integer_one_node;
12401 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12403 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12404 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12405 OMP_CLAUSE_CHAIN (c) = list;
12406 list = c;
12409 return list;
12412 /* OpenMP 4.5:
12413 hint ( expression ) */
12415 static tree
12416 c_parser_omp_clause_hint (c_parser *parser, tree list)
12418 location_t hint_loc = c_parser_peek_token (parser)->location;
12419 matching_parens parens;
12420 if (parens.require_open (parser))
12422 location_t expr_loc = c_parser_peek_token (parser)->location;
12423 c_expr expr = c_parser_expression (parser);
12424 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12425 tree c, t = expr.value;
12426 t = c_fully_fold (t, false, NULL);
12428 parens.skip_until_found_close (parser);
12430 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12432 c_parser_error (parser, "expected integer expression");
12433 return list;
12436 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12438 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12439 OMP_CLAUSE_HINT_EXPR (c) = t;
12440 OMP_CLAUSE_CHAIN (c) = list;
12441 list = c;
12444 return list;
12447 /* OpenMP 4.5:
12448 defaultmap ( tofrom : scalar ) */
12450 static tree
12451 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12453 location_t loc = c_parser_peek_token (parser)->location;
12454 tree c;
12455 const char *p;
12457 matching_parens parens;
12458 if (!parens.require_open (parser))
12459 return list;
12460 if (!c_parser_next_token_is (parser, CPP_NAME))
12462 c_parser_error (parser, "expected %<tofrom%>");
12463 goto out_err;
12465 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12466 if (strcmp (p, "tofrom") != 0)
12468 c_parser_error (parser, "expected %<tofrom%>");
12469 goto out_err;
12471 c_parser_consume_token (parser);
12472 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12473 goto out_err;
12474 if (!c_parser_next_token_is (parser, CPP_NAME))
12476 c_parser_error (parser, "expected %<scalar%>");
12477 goto out_err;
12479 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12480 if (strcmp (p, "scalar") != 0)
12482 c_parser_error (parser, "expected %<scalar%>");
12483 goto out_err;
12485 c_parser_consume_token (parser);
12486 parens.skip_until_found_close (parser);
12487 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12488 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12489 OMP_CLAUSE_CHAIN (c) = list;
12490 return c;
12492 out_err:
12493 parens.skip_until_found_close (parser);
12494 return list;
12497 /* OpenACC 2.0:
12498 use_device ( variable-list )
12500 OpenMP 4.5:
12501 use_device_ptr ( variable-list ) */
12503 static tree
12504 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12506 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12507 list);
12510 /* OpenMP 4.5:
12511 is_device_ptr ( variable-list ) */
12513 static tree
12514 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12516 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12519 /* OpenACC:
12520 num_gangs ( expression )
12521 num_workers ( expression )
12522 vector_length ( expression ) */
12524 static tree
12525 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12526 tree list)
12528 location_t loc = c_parser_peek_token (parser)->location;
12530 matching_parens parens;
12531 if (!parens.require_open (parser))
12532 return list;
12534 location_t expr_loc = c_parser_peek_token (parser)->location;
12535 c_expr expr = c_parser_expression (parser);
12536 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12537 tree c, t = expr.value;
12538 t = c_fully_fold (t, false, NULL);
12540 parens.skip_until_found_close (parser);
12542 if (t == error_mark_node)
12543 return list;
12544 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12546 error_at (expr_loc, "%qs expression must be integral",
12547 omp_clause_code_name[code]);
12548 return list;
12551 /* Attempt to statically determine when the number isn't positive. */
12552 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12553 build_int_cst (TREE_TYPE (t), 0));
12554 protected_set_expr_location (c, expr_loc);
12555 if (c == boolean_true_node)
12557 warning_at (expr_loc, 0,
12558 "%qs value must be positive",
12559 omp_clause_code_name[code]);
12560 t = integer_one_node;
12563 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12565 c = build_omp_clause (loc, code);
12566 OMP_CLAUSE_OPERAND (c, 0) = t;
12567 OMP_CLAUSE_CHAIN (c) = list;
12568 return c;
12571 /* OpenACC:
12573 gang [( gang-arg-list )]
12574 worker [( [num:] int-expr )]
12575 vector [( [length:] int-expr )]
12577 where gang-arg is one of:
12579 [num:] int-expr
12580 static: size-expr
12582 and size-expr may be:
12585 int-expr
12588 static tree
12589 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12590 const char *str, tree list)
12592 const char *id = "num";
12593 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12594 location_t loc = c_parser_peek_token (parser)->location;
12596 if (kind == OMP_CLAUSE_VECTOR)
12597 id = "length";
12599 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12601 c_parser_consume_token (parser);
12605 c_token *next = c_parser_peek_token (parser);
12606 int idx = 0;
12608 /* Gang static argument. */
12609 if (kind == OMP_CLAUSE_GANG
12610 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12612 c_parser_consume_token (parser);
12614 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12615 goto cleanup_error;
12617 idx = 1;
12618 if (ops[idx] != NULL_TREE)
12620 c_parser_error (parser, "too many %<static%> arguments");
12621 goto cleanup_error;
12624 /* Check for the '*' argument. */
12625 if (c_parser_next_token_is (parser, CPP_MULT)
12626 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12627 || c_parser_peek_2nd_token (parser)->type
12628 == CPP_CLOSE_PAREN))
12630 c_parser_consume_token (parser);
12631 ops[idx] = integer_minus_one_node;
12633 if (c_parser_next_token_is (parser, CPP_COMMA))
12635 c_parser_consume_token (parser);
12636 continue;
12638 else
12639 break;
12642 /* Worker num: argument and vector length: arguments. */
12643 else if (c_parser_next_token_is (parser, CPP_NAME)
12644 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12645 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12647 c_parser_consume_token (parser); /* id */
12648 c_parser_consume_token (parser); /* ':' */
12651 /* Now collect the actual argument. */
12652 if (ops[idx] != NULL_TREE)
12654 c_parser_error (parser, "unexpected argument");
12655 goto cleanup_error;
12658 location_t expr_loc = c_parser_peek_token (parser)->location;
12659 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12660 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12661 tree expr = cexpr.value;
12662 if (expr == error_mark_node)
12663 goto cleanup_error;
12665 expr = c_fully_fold (expr, false, NULL);
12667 /* Attempt to statically determine when the number isn't a
12668 positive integer. */
12670 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12672 c_parser_error (parser, "expected integer expression");
12673 return list;
12676 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12677 build_int_cst (TREE_TYPE (expr), 0));
12678 if (c == boolean_true_node)
12680 warning_at (loc, 0,
12681 "%qs value must be positive", str);
12682 expr = integer_one_node;
12685 ops[idx] = expr;
12687 if (kind == OMP_CLAUSE_GANG
12688 && c_parser_next_token_is (parser, CPP_COMMA))
12690 c_parser_consume_token (parser);
12691 continue;
12693 break;
12695 while (1);
12697 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12698 goto cleanup_error;
12701 check_no_duplicate_clause (list, kind, str);
12703 c = build_omp_clause (loc, kind);
12705 if (ops[1])
12706 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12708 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12709 OMP_CLAUSE_CHAIN (c) = list;
12711 return c;
12713 cleanup_error:
12714 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12715 return list;
12718 /* OpenACC:
12719 auto
12720 independent
12721 nohost
12722 seq */
12724 static tree
12725 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12726 tree list)
12728 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12730 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12731 OMP_CLAUSE_CHAIN (c) = list;
12733 return c;
12736 /* OpenACC:
12737 async [( int-expr )] */
12739 static tree
12740 c_parser_oacc_clause_async (c_parser *parser, tree list)
12742 tree c, t;
12743 location_t loc = c_parser_peek_token (parser)->location;
12745 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12747 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12749 c_parser_consume_token (parser);
12751 t = c_parser_expression (parser).value;
12752 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12753 c_parser_error (parser, "expected integer expression");
12754 else if (t == error_mark_node
12755 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12756 return list;
12758 else
12759 t = c_fully_fold (t, false, NULL);
12761 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12763 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12764 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12765 OMP_CLAUSE_CHAIN (c) = list;
12766 list = c;
12768 return list;
12771 /* OpenACC 2.0:
12772 tile ( size-expr-list ) */
12774 static tree
12775 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12777 tree c, expr = error_mark_node;
12778 location_t loc;
12779 tree tile = NULL_TREE;
12781 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12782 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12784 loc = c_parser_peek_token (parser)->location;
12785 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12786 return list;
12790 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12791 return list;
12793 if (c_parser_next_token_is (parser, CPP_MULT)
12794 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12795 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12797 c_parser_consume_token (parser);
12798 expr = integer_zero_node;
12800 else
12802 location_t expr_loc = c_parser_peek_token (parser)->location;
12803 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12804 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12805 expr = cexpr.value;
12807 if (expr == error_mark_node)
12809 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12810 "expected %<)%>");
12811 return list;
12814 expr = c_fully_fold (expr, false, NULL);
12816 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12817 || !tree_fits_shwi_p (expr)
12818 || tree_to_shwi (expr) <= 0)
12820 error_at (expr_loc, "%<tile%> argument needs positive"
12821 " integral constant");
12822 expr = integer_zero_node;
12826 tile = tree_cons (NULL_TREE, expr, tile);
12828 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12830 /* Consume the trailing ')'. */
12831 c_parser_consume_token (parser);
12833 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12834 tile = nreverse (tile);
12835 OMP_CLAUSE_TILE_LIST (c) = tile;
12836 OMP_CLAUSE_CHAIN (c) = list;
12837 return c;
12840 /* OpenACC:
12841 wait ( int-expr-list ) */
12843 static tree
12844 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12846 location_t clause_loc = c_parser_peek_token (parser)->location;
12848 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12849 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12851 return list;
12854 /* OpenMP 2.5:
12855 ordered
12857 OpenMP 4.5:
12858 ordered ( constant-expression ) */
12860 static tree
12861 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12863 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12865 tree c, num = NULL_TREE;
12866 HOST_WIDE_INT n;
12867 location_t loc = c_parser_peek_token (parser)->location;
12868 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12870 matching_parens parens;
12871 parens.consume_open (parser);
12872 num = c_parser_expr_no_commas (parser, NULL).value;
12873 parens.skip_until_found_close (parser);
12875 if (num == error_mark_node)
12876 return list;
12877 if (num)
12879 mark_exp_read (num);
12880 num = c_fully_fold (num, false, NULL);
12881 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12882 || !tree_fits_shwi_p (num)
12883 || (n = tree_to_shwi (num)) <= 0
12884 || (int) n != n)
12886 error_at (loc, "ordered argument needs positive "
12887 "constant integer expression");
12888 return list;
12891 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12892 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12893 OMP_CLAUSE_CHAIN (c) = list;
12894 return c;
12897 /* OpenMP 2.5:
12898 private ( variable-list ) */
12900 static tree
12901 c_parser_omp_clause_private (c_parser *parser, tree list)
12903 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12906 /* OpenMP 2.5:
12907 reduction ( reduction-operator : variable-list )
12909 reduction-operator:
12910 One of: + * - & ^ | && ||
12912 OpenMP 3.1:
12914 reduction-operator:
12915 One of: + * - & ^ | && || max min
12917 OpenMP 4.0:
12919 reduction-operator:
12920 One of: + * - & ^ | && ||
12921 identifier */
12923 static tree
12924 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12926 location_t clause_loc = c_parser_peek_token (parser)->location;
12927 matching_parens parens;
12928 if (parens.require_open (parser))
12930 enum tree_code code = ERROR_MARK;
12931 tree reduc_id = NULL_TREE;
12933 switch (c_parser_peek_token (parser)->type)
12935 case CPP_PLUS:
12936 code = PLUS_EXPR;
12937 break;
12938 case CPP_MULT:
12939 code = MULT_EXPR;
12940 break;
12941 case CPP_MINUS:
12942 code = MINUS_EXPR;
12943 break;
12944 case CPP_AND:
12945 code = BIT_AND_EXPR;
12946 break;
12947 case CPP_XOR:
12948 code = BIT_XOR_EXPR;
12949 break;
12950 case CPP_OR:
12951 code = BIT_IOR_EXPR;
12952 break;
12953 case CPP_AND_AND:
12954 code = TRUTH_ANDIF_EXPR;
12955 break;
12956 case CPP_OR_OR:
12957 code = TRUTH_ORIF_EXPR;
12958 break;
12959 case CPP_NAME:
12961 const char *p
12962 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12963 if (strcmp (p, "min") == 0)
12965 code = MIN_EXPR;
12966 break;
12968 if (strcmp (p, "max") == 0)
12970 code = MAX_EXPR;
12971 break;
12973 reduc_id = c_parser_peek_token (parser)->value;
12974 break;
12976 default:
12977 c_parser_error (parser,
12978 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12979 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12980 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12981 return list;
12983 c_parser_consume_token (parser);
12984 reduc_id = c_omp_reduction_id (code, reduc_id);
12985 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12987 tree nl, c;
12989 nl = c_parser_omp_variable_list (parser, clause_loc,
12990 OMP_CLAUSE_REDUCTION, list);
12991 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12993 tree d = OMP_CLAUSE_DECL (c), type;
12994 if (TREE_CODE (d) != TREE_LIST)
12995 type = TREE_TYPE (d);
12996 else
12998 int cnt = 0;
12999 tree t;
13000 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
13001 cnt++;
13002 type = TREE_TYPE (t);
13003 while (cnt > 0)
13005 if (TREE_CODE (type) != POINTER_TYPE
13006 && TREE_CODE (type) != ARRAY_TYPE)
13007 break;
13008 type = TREE_TYPE (type);
13009 cnt--;
13012 while (TREE_CODE (type) == ARRAY_TYPE)
13013 type = TREE_TYPE (type);
13014 OMP_CLAUSE_REDUCTION_CODE (c) = code;
13015 if (code == ERROR_MARK
13016 || !(INTEGRAL_TYPE_P (type)
13017 || TREE_CODE (type) == REAL_TYPE
13018 || TREE_CODE (type) == COMPLEX_TYPE))
13019 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
13020 = c_omp_reduction_lookup (reduc_id,
13021 TYPE_MAIN_VARIANT (type));
13024 list = nl;
13026 parens.skip_until_found_close (parser);
13028 return list;
13031 /* OpenMP 2.5:
13032 schedule ( schedule-kind )
13033 schedule ( schedule-kind , expression )
13035 schedule-kind:
13036 static | dynamic | guided | runtime | auto
13038 OpenMP 4.5:
13039 schedule ( schedule-modifier : schedule-kind )
13040 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
13042 schedule-modifier:
13043 simd
13044 monotonic
13045 nonmonotonic */
13047 static tree
13048 c_parser_omp_clause_schedule (c_parser *parser, tree list)
13050 tree c, t;
13051 location_t loc = c_parser_peek_token (parser)->location;
13052 int modifiers = 0, nmodifiers = 0;
13054 matching_parens parens;
13055 if (!parens.require_open (parser))
13056 return list;
13058 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
13060 while (c_parser_next_token_is (parser, CPP_NAME))
13062 tree kind = c_parser_peek_token (parser)->value;
13063 const char *p = IDENTIFIER_POINTER (kind);
13064 if (strcmp ("simd", p) == 0)
13065 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
13066 else if (strcmp ("monotonic", p) == 0)
13067 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
13068 else if (strcmp ("nonmonotonic", p) == 0)
13069 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
13070 else
13071 break;
13072 c_parser_consume_token (parser);
13073 if (nmodifiers++ == 0
13074 && c_parser_next_token_is (parser, CPP_COMMA))
13075 c_parser_consume_token (parser);
13076 else
13078 c_parser_require (parser, CPP_COLON, "expected %<:%>");
13079 break;
13083 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
13084 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13085 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
13086 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13088 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
13089 "specified");
13090 modifiers = 0;
13093 if (c_parser_next_token_is (parser, CPP_NAME))
13095 tree kind = c_parser_peek_token (parser)->value;
13096 const char *p = IDENTIFIER_POINTER (kind);
13098 switch (p[0])
13100 case 'd':
13101 if (strcmp ("dynamic", p) != 0)
13102 goto invalid_kind;
13103 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
13104 break;
13106 case 'g':
13107 if (strcmp ("guided", p) != 0)
13108 goto invalid_kind;
13109 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
13110 break;
13112 case 'r':
13113 if (strcmp ("runtime", p) != 0)
13114 goto invalid_kind;
13115 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
13116 break;
13118 default:
13119 goto invalid_kind;
13122 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13123 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13124 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13125 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13126 else
13127 goto invalid_kind;
13129 c_parser_consume_token (parser);
13130 if (c_parser_next_token_is (parser, CPP_COMMA))
13132 location_t here;
13133 c_parser_consume_token (parser);
13135 here = c_parser_peek_token (parser)->location;
13136 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13137 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13138 t = expr.value;
13139 t = c_fully_fold (t, false, NULL);
13141 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13142 error_at (here, "schedule %<runtime%> does not take "
13143 "a %<chunk_size%> parameter");
13144 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13145 error_at (here,
13146 "schedule %<auto%> does not take "
13147 "a %<chunk_size%> parameter");
13148 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13150 /* Attempt to statically determine when the number isn't
13151 positive. */
13152 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13153 build_int_cst (TREE_TYPE (t), 0));
13154 protected_set_expr_location (s, loc);
13155 if (s == boolean_true_node)
13157 warning_at (loc, 0,
13158 "chunk size value must be positive");
13159 t = integer_one_node;
13161 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13163 else
13164 c_parser_error (parser, "expected integer expression");
13166 parens.skip_until_found_close (parser);
13168 else
13169 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13170 "expected %<,%> or %<)%>");
13172 OMP_CLAUSE_SCHEDULE_KIND (c)
13173 = (enum omp_clause_schedule_kind)
13174 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13176 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13177 OMP_CLAUSE_CHAIN (c) = list;
13178 return c;
13180 invalid_kind:
13181 c_parser_error (parser, "invalid schedule kind");
13182 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13183 return list;
13186 /* OpenMP 2.5:
13187 shared ( variable-list ) */
13189 static tree
13190 c_parser_omp_clause_shared (c_parser *parser, tree list)
13192 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13195 /* OpenMP 3.0:
13196 untied */
13198 static tree
13199 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13201 tree c;
13203 /* FIXME: Should we allow duplicates? */
13204 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13206 c = build_omp_clause (c_parser_peek_token (parser)->location,
13207 OMP_CLAUSE_UNTIED);
13208 OMP_CLAUSE_CHAIN (c) = list;
13210 return c;
13213 /* OpenMP 4.0:
13214 inbranch
13215 notinbranch */
13217 static tree
13218 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13219 enum omp_clause_code code, tree list)
13221 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13223 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13224 OMP_CLAUSE_CHAIN (c) = list;
13226 return c;
13229 /* OpenMP 4.0:
13230 parallel
13232 sections
13233 taskgroup */
13235 static tree
13236 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13237 enum omp_clause_code code, tree list)
13239 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13240 OMP_CLAUSE_CHAIN (c) = list;
13242 return c;
13245 /* OpenMP 4.5:
13246 nogroup */
13248 static tree
13249 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13251 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13252 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13253 OMP_CLAUSE_NOGROUP);
13254 OMP_CLAUSE_CHAIN (c) = list;
13255 return c;
13258 /* OpenMP 4.5:
13259 simd
13260 threads */
13262 static tree
13263 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13264 enum omp_clause_code code, tree list)
13266 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13267 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13268 OMP_CLAUSE_CHAIN (c) = list;
13269 return c;
13272 /* OpenMP 4.0:
13273 num_teams ( expression ) */
13275 static tree
13276 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13278 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13279 matching_parens parens;
13280 if (parens.require_open (parser))
13282 location_t expr_loc = c_parser_peek_token (parser)->location;
13283 c_expr expr = c_parser_expression (parser);
13284 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13285 tree c, t = expr.value;
13286 t = c_fully_fold (t, false, NULL);
13288 parens.skip_until_found_close (parser);
13290 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13292 c_parser_error (parser, "expected integer expression");
13293 return list;
13296 /* Attempt to statically determine when the number isn't positive. */
13297 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13298 build_int_cst (TREE_TYPE (t), 0));
13299 protected_set_expr_location (c, expr_loc);
13300 if (c == boolean_true_node)
13302 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13303 t = integer_one_node;
13306 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13308 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13309 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13310 OMP_CLAUSE_CHAIN (c) = list;
13311 list = c;
13314 return list;
13317 /* OpenMP 4.0:
13318 thread_limit ( expression ) */
13320 static tree
13321 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13323 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13324 matching_parens parens;
13325 if (parens.require_open (parser))
13327 location_t expr_loc = c_parser_peek_token (parser)->location;
13328 c_expr expr = c_parser_expression (parser);
13329 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13330 tree c, t = expr.value;
13331 t = c_fully_fold (t, false, NULL);
13333 parens.skip_until_found_close (parser);
13335 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13337 c_parser_error (parser, "expected integer expression");
13338 return list;
13341 /* Attempt to statically determine when the number isn't positive. */
13342 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13343 build_int_cst (TREE_TYPE (t), 0));
13344 protected_set_expr_location (c, expr_loc);
13345 if (c == boolean_true_node)
13347 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13348 t = integer_one_node;
13351 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13352 "thread_limit");
13354 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13355 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13356 OMP_CLAUSE_CHAIN (c) = list;
13357 list = c;
13360 return list;
13363 /* OpenMP 4.0:
13364 aligned ( variable-list )
13365 aligned ( variable-list : constant-expression ) */
13367 static tree
13368 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13370 location_t clause_loc = c_parser_peek_token (parser)->location;
13371 tree nl, c;
13373 matching_parens parens;
13374 if (!parens.require_open (parser))
13375 return list;
13377 nl = c_parser_omp_variable_list (parser, clause_loc,
13378 OMP_CLAUSE_ALIGNED, list);
13380 if (c_parser_next_token_is (parser, CPP_COLON))
13382 c_parser_consume_token (parser);
13383 location_t expr_loc = c_parser_peek_token (parser)->location;
13384 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13385 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13386 tree alignment = expr.value;
13387 alignment = c_fully_fold (alignment, false, NULL);
13388 if (TREE_CODE (alignment) != INTEGER_CST
13389 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13390 || tree_int_cst_sgn (alignment) != 1)
13392 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13393 "be positive constant integer expression");
13394 alignment = NULL_TREE;
13397 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13398 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13401 parens.skip_until_found_close (parser);
13402 return nl;
13405 /* OpenMP 4.0:
13406 linear ( variable-list )
13407 linear ( variable-list : expression )
13409 OpenMP 4.5:
13410 linear ( modifier ( variable-list ) )
13411 linear ( modifier ( variable-list ) : expression ) */
13413 static tree
13414 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
13416 location_t clause_loc = c_parser_peek_token (parser)->location;
13417 tree nl, c, step;
13418 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13420 matching_parens parens;
13421 if (!parens.require_open (parser))
13422 return list;
13424 if (!is_cilk_simd_fn
13425 && c_parser_next_token_is (parser, CPP_NAME))
13427 c_token *tok = c_parser_peek_token (parser);
13428 const char *p = IDENTIFIER_POINTER (tok->value);
13429 if (strcmp ("val", p) == 0)
13430 kind = OMP_CLAUSE_LINEAR_VAL;
13431 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13432 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13433 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13435 c_parser_consume_token (parser);
13436 c_parser_consume_token (parser);
13440 nl = c_parser_omp_variable_list (parser, clause_loc,
13441 OMP_CLAUSE_LINEAR, list);
13443 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13444 parens.skip_until_found_close (parser);
13446 if (c_parser_next_token_is (parser, CPP_COLON))
13448 c_parser_consume_token (parser);
13449 location_t expr_loc = c_parser_peek_token (parser)->location;
13450 c_expr expr = c_parser_expression (parser);
13451 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13452 step = expr.value;
13453 step = c_fully_fold (step, false, NULL);
13454 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
13456 sorry ("using parameters for %<linear%> step is not supported yet");
13457 step = integer_one_node;
13459 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13461 error_at (clause_loc, "%<linear%> clause step expression must "
13462 "be integral");
13463 step = integer_one_node;
13467 else
13468 step = integer_one_node;
13470 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13472 OMP_CLAUSE_LINEAR_STEP (c) = step;
13473 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13476 parens.skip_until_found_close (parser);
13477 return nl;
13480 /* OpenMP 4.0:
13481 safelen ( constant-expression ) */
13483 static tree
13484 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13486 location_t clause_loc = c_parser_peek_token (parser)->location;
13487 tree c, t;
13489 matching_parens parens;
13490 if (!parens.require_open (parser))
13491 return list;
13493 location_t expr_loc = c_parser_peek_token (parser)->location;
13494 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13495 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13496 t = expr.value;
13497 t = c_fully_fold (t, false, NULL);
13498 if (TREE_CODE (t) != INTEGER_CST
13499 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13500 || tree_int_cst_sgn (t) != 1)
13502 error_at (clause_loc, "%<safelen%> clause expression must "
13503 "be positive constant integer expression");
13504 t = NULL_TREE;
13507 parens.skip_until_found_close (parser);
13508 if (t == NULL_TREE || t == error_mark_node)
13509 return list;
13511 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13513 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13514 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13515 OMP_CLAUSE_CHAIN (c) = list;
13516 return c;
13519 /* OpenMP 4.0:
13520 simdlen ( constant-expression ) */
13522 static tree
13523 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13525 location_t clause_loc = c_parser_peek_token (parser)->location;
13526 tree c, t;
13528 matching_parens parens;
13529 if (!parens.require_open (parser))
13530 return list;
13532 location_t expr_loc = c_parser_peek_token (parser)->location;
13533 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13534 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13535 t = expr.value;
13536 t = c_fully_fold (t, false, NULL);
13537 if (TREE_CODE (t) != INTEGER_CST
13538 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13539 || tree_int_cst_sgn (t) != 1)
13541 error_at (clause_loc, "%<simdlen%> clause expression must "
13542 "be positive constant integer expression");
13543 t = NULL_TREE;
13546 parens.skip_until_found_close (parser);
13547 if (t == NULL_TREE || t == error_mark_node)
13548 return list;
13550 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13552 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13553 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13554 OMP_CLAUSE_CHAIN (c) = list;
13555 return c;
13558 /* OpenMP 4.5:
13559 vec:
13560 identifier [+/- integer]
13561 vec , identifier [+/- integer]
13564 static tree
13565 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13566 tree list)
13568 tree vec = NULL;
13569 if (c_parser_next_token_is_not (parser, CPP_NAME)
13570 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13572 c_parser_error (parser, "expected identifier");
13573 return list;
13576 while (c_parser_next_token_is (parser, CPP_NAME)
13577 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13579 tree t = lookup_name (c_parser_peek_token (parser)->value);
13580 tree addend = NULL;
13582 if (t == NULL_TREE)
13584 undeclared_variable (c_parser_peek_token (parser)->location,
13585 c_parser_peek_token (parser)->value);
13586 t = error_mark_node;
13589 c_parser_consume_token (parser);
13591 bool neg = false;
13592 if (c_parser_next_token_is (parser, CPP_MINUS))
13593 neg = true;
13594 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13596 addend = integer_zero_node;
13597 neg = false;
13598 goto add_to_vector;
13600 c_parser_consume_token (parser);
13602 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13604 c_parser_error (parser, "expected integer");
13605 return list;
13608 addend = c_parser_peek_token (parser)->value;
13609 if (TREE_CODE (addend) != INTEGER_CST)
13611 c_parser_error (parser, "expected integer");
13612 return list;
13614 c_parser_consume_token (parser);
13616 add_to_vector:
13617 if (t != error_mark_node)
13619 vec = tree_cons (addend, t, vec);
13620 if (neg)
13621 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13624 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13625 break;
13627 c_parser_consume_token (parser);
13630 if (vec == NULL_TREE)
13631 return list;
13633 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13634 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13635 OMP_CLAUSE_DECL (u) = nreverse (vec);
13636 OMP_CLAUSE_CHAIN (u) = list;
13637 return u;
13640 /* OpenMP 4.0:
13641 depend ( depend-kind: variable-list )
13643 depend-kind:
13644 in | out | inout
13646 OpenMP 4.5:
13647 depend ( source )
13649 depend ( sink : vec ) */
13651 static tree
13652 c_parser_omp_clause_depend (c_parser *parser, tree list)
13654 location_t clause_loc = c_parser_peek_token (parser)->location;
13655 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13656 tree nl, c;
13658 matching_parens parens;
13659 if (!parens.require_open (parser))
13660 return list;
13662 if (c_parser_next_token_is (parser, CPP_NAME))
13664 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13665 if (strcmp ("in", p) == 0)
13666 kind = OMP_CLAUSE_DEPEND_IN;
13667 else if (strcmp ("inout", p) == 0)
13668 kind = OMP_CLAUSE_DEPEND_INOUT;
13669 else if (strcmp ("out", p) == 0)
13670 kind = OMP_CLAUSE_DEPEND_OUT;
13671 else if (strcmp ("source", p) == 0)
13672 kind = OMP_CLAUSE_DEPEND_SOURCE;
13673 else if (strcmp ("sink", p) == 0)
13674 kind = OMP_CLAUSE_DEPEND_SINK;
13675 else
13676 goto invalid_kind;
13678 else
13679 goto invalid_kind;
13681 c_parser_consume_token (parser);
13683 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13685 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13686 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13687 OMP_CLAUSE_DECL (c) = NULL_TREE;
13688 OMP_CLAUSE_CHAIN (c) = list;
13689 parens.skip_until_found_close (parser);
13690 return c;
13693 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13694 goto resync_fail;
13696 if (kind == OMP_CLAUSE_DEPEND_SINK)
13697 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13698 else
13700 nl = c_parser_omp_variable_list (parser, clause_loc,
13701 OMP_CLAUSE_DEPEND, list);
13703 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13704 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13707 parens.skip_until_found_close (parser);
13708 return nl;
13710 invalid_kind:
13711 c_parser_error (parser, "invalid depend kind");
13712 resync_fail:
13713 parens.skip_until_found_close (parser);
13714 return list;
13717 /* OpenMP 4.0:
13718 map ( map-kind: variable-list )
13719 map ( variable-list )
13721 map-kind:
13722 alloc | to | from | tofrom
13724 OpenMP 4.5:
13725 map-kind:
13726 alloc | to | from | tofrom | release | delete
13728 map ( always [,] map-kind: variable-list ) */
13730 static tree
13731 c_parser_omp_clause_map (c_parser *parser, tree list)
13733 location_t clause_loc = c_parser_peek_token (parser)->location;
13734 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13735 int always = 0;
13736 enum c_id_kind always_id_kind = C_ID_NONE;
13737 location_t always_loc = UNKNOWN_LOCATION;
13738 tree always_id = NULL_TREE;
13739 tree nl, c;
13741 matching_parens parens;
13742 if (!parens.require_open (parser))
13743 return list;
13745 if (c_parser_next_token_is (parser, CPP_NAME))
13747 c_token *tok = c_parser_peek_token (parser);
13748 const char *p = IDENTIFIER_POINTER (tok->value);
13749 always_id_kind = tok->id_kind;
13750 always_loc = tok->location;
13751 always_id = tok->value;
13752 if (strcmp ("always", p) == 0)
13754 c_token *sectok = c_parser_peek_2nd_token (parser);
13755 if (sectok->type == CPP_COMMA)
13757 c_parser_consume_token (parser);
13758 c_parser_consume_token (parser);
13759 always = 2;
13761 else if (sectok->type == CPP_NAME)
13763 p = IDENTIFIER_POINTER (sectok->value);
13764 if (strcmp ("alloc", p) == 0
13765 || strcmp ("to", p) == 0
13766 || strcmp ("from", p) == 0
13767 || strcmp ("tofrom", p) == 0
13768 || strcmp ("release", p) == 0
13769 || strcmp ("delete", p) == 0)
13771 c_parser_consume_token (parser);
13772 always = 1;
13778 if (c_parser_next_token_is (parser, CPP_NAME)
13779 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13781 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13782 if (strcmp ("alloc", p) == 0)
13783 kind = GOMP_MAP_ALLOC;
13784 else if (strcmp ("to", p) == 0)
13785 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13786 else if (strcmp ("from", p) == 0)
13787 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13788 else if (strcmp ("tofrom", p) == 0)
13789 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13790 else if (strcmp ("release", p) == 0)
13791 kind = GOMP_MAP_RELEASE;
13792 else if (strcmp ("delete", p) == 0)
13793 kind = GOMP_MAP_DELETE;
13794 else
13796 c_parser_error (parser, "invalid map kind");
13797 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13798 "expected %<)%>");
13799 return list;
13801 c_parser_consume_token (parser);
13802 c_parser_consume_token (parser);
13804 else if (always)
13806 if (always_id_kind != C_ID_ID)
13808 c_parser_error (parser, "expected identifier");
13809 parens.skip_until_found_close (parser);
13810 return list;
13813 tree t = lookup_name (always_id);
13814 if (t == NULL_TREE)
13816 undeclared_variable (always_loc, always_id);
13817 t = error_mark_node;
13819 if (t != error_mark_node)
13821 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13822 OMP_CLAUSE_DECL (u) = t;
13823 OMP_CLAUSE_CHAIN (u) = list;
13824 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13825 list = u;
13827 if (always == 1)
13829 parens.skip_until_found_close (parser);
13830 return list;
13834 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13836 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13837 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13839 parens.skip_until_found_close (parser);
13840 return nl;
13843 /* OpenMP 4.0:
13844 device ( expression ) */
13846 static tree
13847 c_parser_omp_clause_device (c_parser *parser, tree list)
13849 location_t clause_loc = c_parser_peek_token (parser)->location;
13850 matching_parens parens;
13851 if (parens.require_open (parser))
13853 location_t expr_loc = c_parser_peek_token (parser)->location;
13854 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13855 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13856 tree c, t = expr.value;
13857 t = c_fully_fold (t, false, NULL);
13859 parens.skip_until_found_close (parser);
13861 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13863 c_parser_error (parser, "expected integer expression");
13864 return list;
13867 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13869 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13870 OMP_CLAUSE_DEVICE_ID (c) = t;
13871 OMP_CLAUSE_CHAIN (c) = list;
13872 list = c;
13875 return list;
13878 /* OpenMP 4.0:
13879 dist_schedule ( static )
13880 dist_schedule ( static , expression ) */
13882 static tree
13883 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13885 tree c, t = NULL_TREE;
13886 location_t loc = c_parser_peek_token (parser)->location;
13888 matching_parens parens;
13889 if (!parens.require_open (parser))
13890 return list;
13892 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13894 c_parser_error (parser, "invalid dist_schedule kind");
13895 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13896 "expected %<)%>");
13897 return list;
13900 c_parser_consume_token (parser);
13901 if (c_parser_next_token_is (parser, CPP_COMMA))
13903 c_parser_consume_token (parser);
13905 location_t expr_loc = c_parser_peek_token (parser)->location;
13906 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13907 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13908 t = expr.value;
13909 t = c_fully_fold (t, false, NULL);
13910 parens.skip_until_found_close (parser);
13912 else
13913 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13914 "expected %<,%> or %<)%>");
13916 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13917 if (t == error_mark_node)
13918 return list;
13920 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13921 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13922 OMP_CLAUSE_CHAIN (c) = list;
13923 return c;
13926 /* OpenMP 4.0:
13927 proc_bind ( proc-bind-kind )
13929 proc-bind-kind:
13930 master | close | spread */
13932 static tree
13933 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13935 location_t clause_loc = c_parser_peek_token (parser)->location;
13936 enum omp_clause_proc_bind_kind kind;
13937 tree c;
13939 matching_parens parens;
13940 if (!parens.require_open (parser))
13941 return list;
13943 if (c_parser_next_token_is (parser, CPP_NAME))
13945 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13946 if (strcmp ("master", p) == 0)
13947 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13948 else if (strcmp ("close", p) == 0)
13949 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13950 else if (strcmp ("spread", p) == 0)
13951 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13952 else
13953 goto invalid_kind;
13955 else
13956 goto invalid_kind;
13958 c_parser_consume_token (parser);
13959 parens.skip_until_found_close (parser);
13960 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13961 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13962 OMP_CLAUSE_CHAIN (c) = list;
13963 return c;
13965 invalid_kind:
13966 c_parser_error (parser, "invalid proc_bind kind");
13967 parens.skip_until_found_close (parser);
13968 return list;
13971 /* OpenMP 4.0:
13972 to ( variable-list ) */
13974 static tree
13975 c_parser_omp_clause_to (c_parser *parser, tree list)
13977 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13980 /* OpenMP 4.0:
13981 from ( variable-list ) */
13983 static tree
13984 c_parser_omp_clause_from (c_parser *parser, tree list)
13986 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13989 /* OpenMP 4.0:
13990 uniform ( variable-list ) */
13992 static tree
13993 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13995 /* The clauses location. */
13996 location_t loc = c_parser_peek_token (parser)->location;
13998 matching_parens parens;
13999 if (parens.require_open (parser))
14001 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
14002 list);
14003 parens.skip_until_found_close (parser);
14005 return list;
14008 /* Parse all OpenACC clauses. The set clauses allowed by the directive
14009 is a bitmask in MASK. Return the list of clauses found. */
14011 static tree
14012 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
14013 const char *where, bool finish_p = true)
14015 tree clauses = NULL;
14016 bool first = true;
14018 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14020 location_t here;
14021 pragma_omp_clause c_kind;
14022 const char *c_name;
14023 tree prev = clauses;
14025 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14026 c_parser_consume_token (parser);
14028 here = c_parser_peek_token (parser)->location;
14029 c_kind = c_parser_omp_clause_name (parser);
14031 switch (c_kind)
14033 case PRAGMA_OACC_CLAUSE_ASYNC:
14034 clauses = c_parser_oacc_clause_async (parser, clauses);
14035 c_name = "async";
14036 break;
14037 case PRAGMA_OACC_CLAUSE_AUTO:
14038 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
14039 clauses);
14040 c_name = "auto";
14041 break;
14042 case PRAGMA_OACC_CLAUSE_COLLAPSE:
14043 clauses = c_parser_omp_clause_collapse (parser, clauses);
14044 c_name = "collapse";
14045 break;
14046 case PRAGMA_OACC_CLAUSE_COPY:
14047 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14048 c_name = "copy";
14049 break;
14050 case PRAGMA_OACC_CLAUSE_COPYIN:
14051 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14052 c_name = "copyin";
14053 break;
14054 case PRAGMA_OACC_CLAUSE_COPYOUT:
14055 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14056 c_name = "copyout";
14057 break;
14058 case PRAGMA_OACC_CLAUSE_CREATE:
14059 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14060 c_name = "create";
14061 break;
14062 case PRAGMA_OACC_CLAUSE_DELETE:
14063 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14064 c_name = "delete";
14065 break;
14066 case PRAGMA_OMP_CLAUSE_DEFAULT:
14067 clauses = c_parser_omp_clause_default (parser, clauses, true);
14068 c_name = "default";
14069 break;
14070 case PRAGMA_OACC_CLAUSE_DEVICE:
14071 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14072 c_name = "device";
14073 break;
14074 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
14075 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
14076 c_name = "deviceptr";
14077 break;
14078 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
14079 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14080 c_name = "device_resident";
14081 break;
14082 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
14083 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14084 c_name = "firstprivate";
14085 break;
14086 case PRAGMA_OACC_CLAUSE_GANG:
14087 c_name = "gang";
14088 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
14089 c_name, clauses);
14090 break;
14091 case PRAGMA_OACC_CLAUSE_HOST:
14092 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14093 c_name = "host";
14094 break;
14095 case PRAGMA_OACC_CLAUSE_IF:
14096 clauses = c_parser_omp_clause_if (parser, clauses, false);
14097 c_name = "if";
14098 break;
14099 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
14100 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
14101 clauses);
14102 c_name = "independent";
14103 break;
14104 case PRAGMA_OACC_CLAUSE_LINK:
14105 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14106 c_name = "link";
14107 break;
14108 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
14109 clauses = c_parser_oacc_single_int_clause (parser,
14110 OMP_CLAUSE_NUM_GANGS,
14111 clauses);
14112 c_name = "num_gangs";
14113 break;
14114 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
14115 clauses = c_parser_oacc_single_int_clause (parser,
14116 OMP_CLAUSE_NUM_WORKERS,
14117 clauses);
14118 c_name = "num_workers";
14119 break;
14120 case PRAGMA_OACC_CLAUSE_PRESENT:
14121 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14122 c_name = "present";
14123 break;
14124 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
14125 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14126 c_name = "present_or_copy";
14127 break;
14128 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
14129 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14130 c_name = "present_or_copyin";
14131 break;
14132 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
14133 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14134 c_name = "present_or_copyout";
14135 break;
14136 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
14137 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14138 c_name = "present_or_create";
14139 break;
14140 case PRAGMA_OACC_CLAUSE_PRIVATE:
14141 clauses = c_parser_omp_clause_private (parser, clauses);
14142 c_name = "private";
14143 break;
14144 case PRAGMA_OACC_CLAUSE_REDUCTION:
14145 clauses = c_parser_omp_clause_reduction (parser, clauses);
14146 c_name = "reduction";
14147 break;
14148 case PRAGMA_OACC_CLAUSE_SELF:
14149 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14150 c_name = "self";
14151 break;
14152 case PRAGMA_OACC_CLAUSE_SEQ:
14153 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14154 clauses);
14155 c_name = "seq";
14156 break;
14157 case PRAGMA_OACC_CLAUSE_TILE:
14158 clauses = c_parser_oacc_clause_tile (parser, clauses);
14159 c_name = "tile";
14160 break;
14161 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14162 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14163 c_name = "use_device";
14164 break;
14165 case PRAGMA_OACC_CLAUSE_VECTOR:
14166 c_name = "vector";
14167 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14168 c_name, clauses);
14169 break;
14170 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14171 clauses = c_parser_oacc_single_int_clause (parser,
14172 OMP_CLAUSE_VECTOR_LENGTH,
14173 clauses);
14174 c_name = "vector_length";
14175 break;
14176 case PRAGMA_OACC_CLAUSE_WAIT:
14177 clauses = c_parser_oacc_clause_wait (parser, clauses);
14178 c_name = "wait";
14179 break;
14180 case PRAGMA_OACC_CLAUSE_WORKER:
14181 c_name = "worker";
14182 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14183 c_name, clauses);
14184 break;
14185 default:
14186 c_parser_error (parser, "expected %<#pragma acc%> clause");
14187 goto saw_error;
14190 first = false;
14192 if (((mask >> c_kind) & 1) == 0)
14194 /* Remove the invalid clause(s) from the list to avoid
14195 confusing the rest of the compiler. */
14196 clauses = prev;
14197 error_at (here, "%qs is not valid for %qs", c_name, where);
14201 saw_error:
14202 c_parser_skip_to_pragma_eol (parser);
14204 if (finish_p)
14205 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14207 return clauses;
14210 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14211 is a bitmask in MASK. Return the list of clauses found. */
14213 static tree
14214 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14215 const char *where, bool finish_p = true)
14217 tree clauses = NULL;
14218 bool first = true, cilk_simd_fn = false;
14220 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14222 location_t here;
14223 pragma_omp_clause c_kind;
14224 const char *c_name;
14225 tree prev = clauses;
14227 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14228 c_parser_consume_token (parser);
14230 here = c_parser_peek_token (parser)->location;
14231 c_kind = c_parser_omp_clause_name (parser);
14233 switch (c_kind)
14235 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14236 clauses = c_parser_omp_clause_collapse (parser, clauses);
14237 c_name = "collapse";
14238 break;
14239 case PRAGMA_OMP_CLAUSE_COPYIN:
14240 clauses = c_parser_omp_clause_copyin (parser, clauses);
14241 c_name = "copyin";
14242 break;
14243 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14244 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14245 c_name = "copyprivate";
14246 break;
14247 case PRAGMA_OMP_CLAUSE_DEFAULT:
14248 clauses = c_parser_omp_clause_default (parser, clauses, false);
14249 c_name = "default";
14250 break;
14251 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14252 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14253 c_name = "firstprivate";
14254 break;
14255 case PRAGMA_OMP_CLAUSE_FINAL:
14256 clauses = c_parser_omp_clause_final (parser, clauses);
14257 c_name = "final";
14258 break;
14259 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14260 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14261 c_name = "grainsize";
14262 break;
14263 case PRAGMA_OMP_CLAUSE_HINT:
14264 clauses = c_parser_omp_clause_hint (parser, clauses);
14265 c_name = "hint";
14266 break;
14267 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14268 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14269 c_name = "defaultmap";
14270 break;
14271 case PRAGMA_OMP_CLAUSE_IF:
14272 clauses = c_parser_omp_clause_if (parser, clauses, true);
14273 c_name = "if";
14274 break;
14275 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14276 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14277 c_name = "lastprivate";
14278 break;
14279 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14280 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14281 c_name = "mergeable";
14282 break;
14283 case PRAGMA_OMP_CLAUSE_NOWAIT:
14284 clauses = c_parser_omp_clause_nowait (parser, clauses);
14285 c_name = "nowait";
14286 break;
14287 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14288 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14289 c_name = "num_tasks";
14290 break;
14291 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14292 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14293 c_name = "num_threads";
14294 break;
14295 case PRAGMA_OMP_CLAUSE_ORDERED:
14296 clauses = c_parser_omp_clause_ordered (parser, clauses);
14297 c_name = "ordered";
14298 break;
14299 case PRAGMA_OMP_CLAUSE_PRIORITY:
14300 clauses = c_parser_omp_clause_priority (parser, clauses);
14301 c_name = "priority";
14302 break;
14303 case PRAGMA_OMP_CLAUSE_PRIVATE:
14304 clauses = c_parser_omp_clause_private (parser, clauses);
14305 c_name = "private";
14306 break;
14307 case PRAGMA_OMP_CLAUSE_REDUCTION:
14308 clauses = c_parser_omp_clause_reduction (parser, clauses);
14309 c_name = "reduction";
14310 break;
14311 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14312 clauses = c_parser_omp_clause_schedule (parser, clauses);
14313 c_name = "schedule";
14314 break;
14315 case PRAGMA_OMP_CLAUSE_SHARED:
14316 clauses = c_parser_omp_clause_shared (parser, clauses);
14317 c_name = "shared";
14318 break;
14319 case PRAGMA_OMP_CLAUSE_UNTIED:
14320 clauses = c_parser_omp_clause_untied (parser, clauses);
14321 c_name = "untied";
14322 break;
14323 case PRAGMA_OMP_CLAUSE_INBRANCH:
14324 case PRAGMA_CILK_CLAUSE_MASK:
14325 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14326 clauses);
14327 c_name = "inbranch";
14328 break;
14329 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14330 case PRAGMA_CILK_CLAUSE_NOMASK:
14331 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14332 clauses);
14333 c_name = "notinbranch";
14334 break;
14335 case PRAGMA_OMP_CLAUSE_PARALLEL:
14336 clauses
14337 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14338 clauses);
14339 c_name = "parallel";
14340 if (!first)
14342 clause_not_first:
14343 error_at (here, "%qs must be the first clause of %qs",
14344 c_name, where);
14345 clauses = prev;
14347 break;
14348 case PRAGMA_OMP_CLAUSE_FOR:
14349 clauses
14350 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14351 clauses);
14352 c_name = "for";
14353 if (!first)
14354 goto clause_not_first;
14355 break;
14356 case PRAGMA_OMP_CLAUSE_SECTIONS:
14357 clauses
14358 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14359 clauses);
14360 c_name = "sections";
14361 if (!first)
14362 goto clause_not_first;
14363 break;
14364 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14365 clauses
14366 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14367 clauses);
14368 c_name = "taskgroup";
14369 if (!first)
14370 goto clause_not_first;
14371 break;
14372 case PRAGMA_OMP_CLAUSE_LINK:
14373 clauses
14374 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14375 c_name = "link";
14376 break;
14377 case PRAGMA_OMP_CLAUSE_TO:
14378 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14379 clauses
14380 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14381 clauses);
14382 else
14383 clauses = c_parser_omp_clause_to (parser, clauses);
14384 c_name = "to";
14385 break;
14386 case PRAGMA_OMP_CLAUSE_FROM:
14387 clauses = c_parser_omp_clause_from (parser, clauses);
14388 c_name = "from";
14389 break;
14390 case PRAGMA_OMP_CLAUSE_UNIFORM:
14391 clauses = c_parser_omp_clause_uniform (parser, clauses);
14392 c_name = "uniform";
14393 break;
14394 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14395 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14396 c_name = "num_teams";
14397 break;
14398 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14399 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14400 c_name = "thread_limit";
14401 break;
14402 case PRAGMA_OMP_CLAUSE_ALIGNED:
14403 clauses = c_parser_omp_clause_aligned (parser, clauses);
14404 c_name = "aligned";
14405 break;
14406 case PRAGMA_OMP_CLAUSE_LINEAR:
14407 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
14408 cilk_simd_fn = true;
14409 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
14410 c_name = "linear";
14411 break;
14412 case PRAGMA_OMP_CLAUSE_DEPEND:
14413 clauses = c_parser_omp_clause_depend (parser, clauses);
14414 c_name = "depend";
14415 break;
14416 case PRAGMA_OMP_CLAUSE_MAP:
14417 clauses = c_parser_omp_clause_map (parser, clauses);
14418 c_name = "map";
14419 break;
14420 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14421 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14422 c_name = "use_device_ptr";
14423 break;
14424 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14425 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14426 c_name = "is_device_ptr";
14427 break;
14428 case PRAGMA_OMP_CLAUSE_DEVICE:
14429 clauses = c_parser_omp_clause_device (parser, clauses);
14430 c_name = "device";
14431 break;
14432 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14433 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14434 c_name = "dist_schedule";
14435 break;
14436 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14437 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14438 c_name = "proc_bind";
14439 break;
14440 case PRAGMA_OMP_CLAUSE_SAFELEN:
14441 clauses = c_parser_omp_clause_safelen (parser, clauses);
14442 c_name = "safelen";
14443 break;
14444 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
14445 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
14446 c_name = "simdlen";
14447 break;
14448 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14449 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14450 c_name = "simdlen";
14451 break;
14452 case PRAGMA_OMP_CLAUSE_NOGROUP:
14453 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14454 c_name = "nogroup";
14455 break;
14456 case PRAGMA_OMP_CLAUSE_THREADS:
14457 clauses
14458 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14459 clauses);
14460 c_name = "threads";
14461 break;
14462 case PRAGMA_OMP_CLAUSE_SIMD:
14463 clauses
14464 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14465 clauses);
14466 c_name = "simd";
14467 break;
14468 default:
14469 c_parser_error (parser, "expected %<#pragma omp%> clause");
14470 goto saw_error;
14473 first = false;
14475 if (((mask >> c_kind) & 1) == 0)
14477 /* Remove the invalid clause(s) from the list to avoid
14478 confusing the rest of the compiler. */
14479 clauses = prev;
14480 error_at (here, "%qs is not valid for %qs", c_name, where);
14484 saw_error:
14485 c_parser_skip_to_pragma_eol (parser);
14487 if (finish_p)
14489 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14490 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14491 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14494 return clauses;
14497 /* OpenACC 2.0, OpenMP 2.5:
14498 structured-block:
14499 statement
14501 In practice, we're also interested in adding the statement to an
14502 outer node. So it is convenient if we work around the fact that
14503 c_parser_statement calls add_stmt. */
14505 static tree
14506 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14508 tree stmt = push_stmt_list ();
14509 c_parser_statement (parser, if_p);
14510 return pop_stmt_list (stmt);
14513 /* OpenACC 2.0:
14514 # pragma acc cache (variable-list) new-line
14516 LOC is the location of the #pragma token.
14519 static tree
14520 c_parser_oacc_cache (location_t loc, c_parser *parser)
14522 tree stmt, clauses;
14524 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14525 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14527 c_parser_skip_to_pragma_eol (parser);
14529 stmt = make_node (OACC_CACHE);
14530 TREE_TYPE (stmt) = void_type_node;
14531 OACC_CACHE_CLAUSES (stmt) = clauses;
14532 SET_EXPR_LOCATION (stmt, loc);
14533 add_stmt (stmt);
14535 return stmt;
14538 /* OpenACC 2.0:
14539 # pragma acc data oacc-data-clause[optseq] new-line
14540 structured-block
14542 LOC is the location of the #pragma token.
14545 #define OACC_DATA_CLAUSE_MASK \
14546 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14558 static tree
14559 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14561 tree stmt, clauses, block;
14563 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14564 "#pragma acc data");
14566 block = c_begin_omp_parallel ();
14567 add_stmt (c_parser_omp_structured_block (parser, if_p));
14569 stmt = c_finish_oacc_data (loc, clauses, block);
14571 return stmt;
14574 /* OpenACC 2.0:
14575 # pragma acc declare oacc-data-clause[optseq] new-line
14578 #define OACC_DECLARE_CLAUSE_MASK \
14579 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14592 static void
14593 c_parser_oacc_declare (c_parser *parser)
14595 location_t pragma_loc = c_parser_peek_token (parser)->location;
14596 tree clauses, stmt, t, decl;
14598 bool error = false;
14600 c_parser_consume_pragma (parser);
14602 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14603 "#pragma acc declare");
14604 if (!clauses)
14606 error_at (pragma_loc,
14607 "no valid clauses specified in %<#pragma acc declare%>");
14608 return;
14611 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14613 location_t loc = OMP_CLAUSE_LOCATION (t);
14614 decl = OMP_CLAUSE_DECL (t);
14615 if (!DECL_P (decl))
14617 error_at (loc, "array section in %<#pragma acc declare%>");
14618 error = true;
14619 continue;
14622 switch (OMP_CLAUSE_MAP_KIND (t))
14624 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14625 case GOMP_MAP_FORCE_ALLOC:
14626 case GOMP_MAP_FORCE_TO:
14627 case GOMP_MAP_FORCE_DEVICEPTR:
14628 case GOMP_MAP_DEVICE_RESIDENT:
14629 break;
14631 case GOMP_MAP_LINK:
14632 if (!global_bindings_p ()
14633 && (TREE_STATIC (decl)
14634 || !DECL_EXTERNAL (decl)))
14636 error_at (loc,
14637 "%qD must be a global variable in "
14638 "%<#pragma acc declare link%>",
14639 decl);
14640 error = true;
14641 continue;
14643 break;
14645 default:
14646 if (global_bindings_p ())
14648 error_at (loc, "invalid OpenACC clause at file scope");
14649 error = true;
14650 continue;
14652 if (DECL_EXTERNAL (decl))
14654 error_at (loc,
14655 "invalid use of %<extern%> variable %qD "
14656 "in %<#pragma acc declare%>", decl);
14657 error = true;
14658 continue;
14660 else if (TREE_PUBLIC (decl))
14662 error_at (loc,
14663 "invalid use of %<global%> variable %qD "
14664 "in %<#pragma acc declare%>", decl);
14665 error = true;
14666 continue;
14668 break;
14671 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14672 || lookup_attribute ("omp declare target link",
14673 DECL_ATTRIBUTES (decl)))
14675 error_at (loc, "variable %qD used more than once with "
14676 "%<#pragma acc declare%>", decl);
14677 error = true;
14678 continue;
14681 if (!error)
14683 tree id;
14685 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14686 id = get_identifier ("omp declare target link");
14687 else
14688 id = get_identifier ("omp declare target");
14690 DECL_ATTRIBUTES (decl)
14691 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14693 if (global_bindings_p ())
14695 symtab_node *node = symtab_node::get (decl);
14696 if (node != NULL)
14698 node->offloadable = 1;
14699 if (ENABLE_OFFLOADING)
14701 g->have_offload = true;
14702 if (is_a <varpool_node *> (node))
14703 vec_safe_push (offload_vars, decl);
14710 if (error || global_bindings_p ())
14711 return;
14713 stmt = make_node (OACC_DECLARE);
14714 TREE_TYPE (stmt) = void_type_node;
14715 OACC_DECLARE_CLAUSES (stmt) = clauses;
14716 SET_EXPR_LOCATION (stmt, pragma_loc);
14718 add_stmt (stmt);
14720 return;
14723 /* OpenACC 2.0:
14724 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14728 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14731 LOC is the location of the #pragma token.
14734 #define OACC_ENTER_DATA_CLAUSE_MASK \
14735 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14743 #define OACC_EXIT_DATA_CLAUSE_MASK \
14744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14750 static void
14751 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14753 location_t loc = c_parser_peek_token (parser)->location;
14754 tree clauses, stmt;
14755 const char *p = "";
14757 c_parser_consume_pragma (parser);
14759 if (c_parser_next_token_is (parser, CPP_NAME))
14761 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14762 c_parser_consume_token (parser);
14765 if (strcmp (p, "data") != 0)
14767 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14768 enter ? "enter" : "exit");
14769 parser->error = true;
14770 c_parser_skip_to_pragma_eol (parser);
14771 return;
14774 if (enter)
14775 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14776 "#pragma acc enter data");
14777 else
14778 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14779 "#pragma acc exit data");
14781 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14783 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14784 enter ? "enter" : "exit");
14785 return;
14788 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14789 TREE_TYPE (stmt) = void_type_node;
14790 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14791 SET_EXPR_LOCATION (stmt, loc);
14792 add_stmt (stmt);
14796 /* OpenACC 2.0:
14797 # pragma acc host_data oacc-data-clause[optseq] new-line
14798 structured-block
14801 #define OACC_HOST_DATA_CLAUSE_MASK \
14802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14804 static tree
14805 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14807 tree stmt, clauses, block;
14809 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14810 "#pragma acc host_data");
14812 block = c_begin_omp_parallel ();
14813 add_stmt (c_parser_omp_structured_block (parser, if_p));
14814 stmt = c_finish_oacc_host_data (loc, clauses, block);
14815 return stmt;
14819 /* OpenACC 2.0:
14821 # pragma acc loop oacc-loop-clause[optseq] new-line
14822 structured-block
14824 LOC is the location of the #pragma token.
14827 #define OACC_LOOP_CLAUSE_MASK \
14828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14838 static tree
14839 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14840 omp_clause_mask mask, tree *cclauses, bool *if_p)
14842 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14844 strcat (p_name, " loop");
14845 mask |= OACC_LOOP_CLAUSE_MASK;
14847 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14848 cclauses == NULL);
14849 if (cclauses)
14851 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14852 if (*cclauses)
14853 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14854 if (clauses)
14855 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14858 tree block = c_begin_compound_stmt (true);
14859 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14860 if_p);
14861 block = c_end_compound_stmt (loc, block, true);
14862 add_stmt (block);
14864 return stmt;
14867 /* OpenACC 2.0:
14868 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14869 structured-block
14873 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14874 structured-block
14876 LOC is the location of the #pragma token.
14879 #define OACC_KERNELS_CLAUSE_MASK \
14880 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14898 #define OACC_PARALLEL_CLAUSE_MASK \
14899 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14920 static tree
14921 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14922 enum pragma_kind p_kind, char *p_name,
14923 bool *if_p)
14925 omp_clause_mask mask;
14926 enum tree_code code;
14927 switch (p_kind)
14929 case PRAGMA_OACC_KERNELS:
14930 strcat (p_name, " kernels");
14931 mask = OACC_KERNELS_CLAUSE_MASK;
14932 code = OACC_KERNELS;
14933 break;
14934 case PRAGMA_OACC_PARALLEL:
14935 strcat (p_name, " parallel");
14936 mask = OACC_PARALLEL_CLAUSE_MASK;
14937 code = OACC_PARALLEL;
14938 break;
14939 default:
14940 gcc_unreachable ();
14943 if (c_parser_next_token_is (parser, CPP_NAME))
14945 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14946 if (strcmp (p, "loop") == 0)
14948 c_parser_consume_token (parser);
14949 tree block = c_begin_omp_parallel ();
14950 tree clauses;
14951 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14952 return c_finish_omp_construct (loc, code, block, clauses);
14956 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14958 tree block = c_begin_omp_parallel ();
14959 add_stmt (c_parser_omp_structured_block (parser, if_p));
14961 return c_finish_omp_construct (loc, code, block, clauses);
14964 /* OpenACC 2.0:
14965 # pragma acc routine oacc-routine-clause[optseq] new-line
14966 function-definition
14968 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14971 #define OACC_ROUTINE_CLAUSE_MASK \
14972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14977 /* Parse an OpenACC routine directive. For named directives, we apply
14978 immediately to the named function. For unnamed ones we then parse
14979 a declaration or definition, which must be for a function. */
14981 static void
14982 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14984 gcc_checking_assert (context == pragma_external);
14986 oacc_routine_data data;
14987 data.error_seen = false;
14988 data.fndecl_seen = false;
14989 data.clauses = NULL_TREE;
14990 data.loc = c_parser_peek_token (parser)->location;
14992 c_parser_consume_pragma (parser);
14994 /* Look for optional '( name )'. */
14995 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14997 c_parser_consume_token (parser); /* '(' */
14999 tree decl = NULL_TREE;
15000 c_token *name_token = c_parser_peek_token (parser);
15001 location_t name_loc = name_token->location;
15002 if (name_token->type == CPP_NAME
15003 && (name_token->id_kind == C_ID_ID
15004 || name_token->id_kind == C_ID_TYPENAME))
15006 decl = lookup_name (name_token->value);
15007 if (!decl)
15008 error_at (name_loc,
15009 "%qE has not been declared", name_token->value);
15010 c_parser_consume_token (parser);
15012 else
15013 c_parser_error (parser, "expected function name");
15015 if (!decl
15016 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15018 c_parser_skip_to_pragma_eol (parser, false);
15019 return;
15022 data.clauses
15023 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15024 "#pragma acc routine");
15026 if (TREE_CODE (decl) != FUNCTION_DECL)
15028 error_at (name_loc, "%qD does not refer to a function", decl);
15029 return;
15032 c_finish_oacc_routine (&data, decl, false);
15034 else /* No optional '( name )'. */
15036 data.clauses
15037 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15038 "#pragma acc routine");
15040 /* Emit a helpful diagnostic if there's another pragma following this
15041 one. Also don't allow a static assertion declaration, as in the
15042 following we'll just parse a *single* "declaration or function
15043 definition", and the static assertion counts an one. */
15044 if (c_parser_next_token_is (parser, CPP_PRAGMA)
15045 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
15047 error_at (data.loc,
15048 "%<#pragma acc routine%> not immediately followed by"
15049 " function declaration or definition");
15050 /* ..., and then just keep going. */
15051 return;
15054 /* We only have to consider the pragma_external case here. */
15055 if (c_parser_next_token_is (parser, CPP_KEYWORD)
15056 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
15058 int ext = disable_extension_diagnostics ();
15060 c_parser_consume_token (parser);
15061 while (c_parser_next_token_is (parser, CPP_KEYWORD)
15062 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
15063 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15064 NULL, vNULL, &data);
15065 restore_extension_diagnostics (ext);
15067 else
15068 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15069 NULL, vNULL, &data);
15073 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
15074 IS_DEFN is true if we're applying it to the definition. */
15076 static void
15077 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
15078 bool is_defn)
15080 /* Keep going if we're in error reporting mode. */
15081 if (data->error_seen
15082 || fndecl == error_mark_node)
15083 return;
15085 if (data->fndecl_seen)
15087 error_at (data->loc,
15088 "%<#pragma acc routine%> not immediately followed by"
15089 " a single function declaration or definition");
15090 data->error_seen = true;
15091 return;
15093 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
15095 error_at (data->loc,
15096 "%<#pragma acc routine%> not immediately followed by"
15097 " function declaration or definition");
15098 data->error_seen = true;
15099 return;
15102 if (oacc_get_fn_attrib (fndecl))
15104 error_at (data->loc,
15105 "%<#pragma acc routine%> already applied to %qD", fndecl);
15106 data->error_seen = true;
15107 return;
15110 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
15112 error_at (data->loc,
15113 TREE_USED (fndecl)
15114 ? G_("%<#pragma acc routine%> must be applied before use")
15115 : G_("%<#pragma acc routine%> must be applied before "
15116 "definition"));
15117 data->error_seen = true;
15118 return;
15121 /* Process the routine's dimension clauses. */
15122 tree dims = oacc_build_routine_dims (data->clauses);
15123 oacc_replace_fn_attrib (fndecl, dims);
15125 /* Add an "omp declare target" attribute. */
15126 DECL_ATTRIBUTES (fndecl)
15127 = tree_cons (get_identifier ("omp declare target"),
15128 NULL_TREE, DECL_ATTRIBUTES (fndecl));
15130 /* Remember that we've used this "#pragma acc routine". */
15131 data->fndecl_seen = true;
15134 /* OpenACC 2.0:
15135 # pragma acc update oacc-update-clause[optseq] new-line
15138 #define OACC_UPDATE_CLAUSE_MASK \
15139 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
15141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
15142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
15144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15146 static void
15147 c_parser_oacc_update (c_parser *parser)
15149 location_t loc = c_parser_peek_token (parser)->location;
15151 c_parser_consume_pragma (parser);
15153 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15154 "#pragma acc update");
15155 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15157 error_at (loc,
15158 "%<#pragma acc update%> must contain at least one "
15159 "%<device%> or %<host%> or %<self%> clause");
15160 return;
15163 if (parser->error)
15164 return;
15166 tree stmt = make_node (OACC_UPDATE);
15167 TREE_TYPE (stmt) = void_type_node;
15168 OACC_UPDATE_CLAUSES (stmt) = clauses;
15169 SET_EXPR_LOCATION (stmt, loc);
15170 add_stmt (stmt);
15173 /* OpenACC 2.0:
15174 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15176 LOC is the location of the #pragma token.
15179 #define OACC_WAIT_CLAUSE_MASK \
15180 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15182 static tree
15183 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15185 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15187 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15188 list = c_parser_oacc_wait_list (parser, loc, list);
15190 strcpy (p_name, " wait");
15191 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15192 stmt = c_finish_oacc_wait (loc, list, clauses);
15193 add_stmt (stmt);
15195 return stmt;
15198 /* OpenMP 2.5:
15199 # pragma omp atomic new-line
15200 expression-stmt
15202 expression-stmt:
15203 x binop= expr | x++ | ++x | x-- | --x
15204 binop:
15205 +, *, -, /, &, ^, |, <<, >>
15207 where x is an lvalue expression with scalar type.
15209 OpenMP 3.1:
15210 # pragma omp atomic new-line
15211 update-stmt
15213 # pragma omp atomic read new-line
15214 read-stmt
15216 # pragma omp atomic write new-line
15217 write-stmt
15219 # pragma omp atomic update new-line
15220 update-stmt
15222 # pragma omp atomic capture new-line
15223 capture-stmt
15225 # pragma omp atomic capture new-line
15226 capture-block
15228 read-stmt:
15229 v = x
15230 write-stmt:
15231 x = expr
15232 update-stmt:
15233 expression-stmt | x = x binop expr
15234 capture-stmt:
15235 v = expression-stmt
15236 capture-block:
15237 { v = x; update-stmt; } | { update-stmt; v = x; }
15239 OpenMP 4.0:
15240 update-stmt:
15241 expression-stmt | x = x binop expr | x = expr binop x
15242 capture-stmt:
15243 v = update-stmt
15244 capture-block:
15245 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15247 where x and v are lvalue expressions with scalar type.
15249 LOC is the location of the #pragma token. */
15251 static void
15252 c_parser_omp_atomic (location_t loc, c_parser *parser)
15254 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15255 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15256 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15257 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15258 struct c_expr expr;
15259 location_t eloc;
15260 bool structured_block = false;
15261 bool swapped = false;
15262 bool seq_cst = false;
15263 bool non_lvalue_p;
15265 if (c_parser_next_token_is (parser, CPP_NAME))
15267 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15268 if (!strcmp (p, "seq_cst"))
15270 seq_cst = true;
15271 c_parser_consume_token (parser);
15272 if (c_parser_next_token_is (parser, CPP_COMMA)
15273 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15274 c_parser_consume_token (parser);
15277 if (c_parser_next_token_is (parser, CPP_NAME))
15279 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15281 if (!strcmp (p, "read"))
15282 code = OMP_ATOMIC_READ;
15283 else if (!strcmp (p, "write"))
15284 code = NOP_EXPR;
15285 else if (!strcmp (p, "update"))
15286 code = OMP_ATOMIC;
15287 else if (!strcmp (p, "capture"))
15288 code = OMP_ATOMIC_CAPTURE_NEW;
15289 else
15290 p = NULL;
15291 if (p)
15292 c_parser_consume_token (parser);
15294 if (!seq_cst)
15296 if (c_parser_next_token_is (parser, CPP_COMMA)
15297 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15298 c_parser_consume_token (parser);
15300 if (c_parser_next_token_is (parser, CPP_NAME))
15302 const char *p
15303 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15304 if (!strcmp (p, "seq_cst"))
15306 seq_cst = true;
15307 c_parser_consume_token (parser);
15311 c_parser_skip_to_pragma_eol (parser);
15313 switch (code)
15315 case OMP_ATOMIC_READ:
15316 case NOP_EXPR: /* atomic write */
15317 v = c_parser_cast_expression (parser, NULL).value;
15318 non_lvalue_p = !lvalue_p (v);
15319 v = c_fully_fold (v, false, NULL, true);
15320 if (v == error_mark_node)
15321 goto saw_error;
15322 if (non_lvalue_p)
15323 v = non_lvalue (v);
15324 loc = c_parser_peek_token (parser)->location;
15325 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15326 goto saw_error;
15327 if (code == NOP_EXPR)
15329 lhs = c_parser_expression (parser).value;
15330 lhs = c_fully_fold (lhs, false, NULL);
15331 if (lhs == error_mark_node)
15332 goto saw_error;
15334 else
15336 lhs = c_parser_cast_expression (parser, NULL).value;
15337 non_lvalue_p = !lvalue_p (lhs);
15338 lhs = c_fully_fold (lhs, false, NULL, true);
15339 if (lhs == error_mark_node)
15340 goto saw_error;
15341 if (non_lvalue_p)
15342 lhs = non_lvalue (lhs);
15344 if (code == NOP_EXPR)
15346 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15347 opcode. */
15348 code = OMP_ATOMIC;
15349 rhs = lhs;
15350 lhs = v;
15351 v = NULL_TREE;
15353 goto done;
15354 case OMP_ATOMIC_CAPTURE_NEW:
15355 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15357 c_parser_consume_token (parser);
15358 structured_block = true;
15360 else
15362 v = c_parser_cast_expression (parser, NULL).value;
15363 non_lvalue_p = !lvalue_p (v);
15364 v = c_fully_fold (v, false, NULL, true);
15365 if (v == error_mark_node)
15366 goto saw_error;
15367 if (non_lvalue_p)
15368 v = non_lvalue (v);
15369 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15370 goto saw_error;
15372 break;
15373 default:
15374 break;
15377 /* For structured_block case we don't know yet whether
15378 old or new x should be captured. */
15379 restart:
15380 eloc = c_parser_peek_token (parser)->location;
15381 expr = c_parser_cast_expression (parser, NULL);
15382 lhs = expr.value;
15383 expr = default_function_array_conversion (eloc, expr);
15384 unfolded_lhs = expr.value;
15385 lhs = c_fully_fold (lhs, false, NULL, true);
15386 orig_lhs = lhs;
15387 switch (TREE_CODE (lhs))
15389 case ERROR_MARK:
15390 saw_error:
15391 c_parser_skip_to_end_of_block_or_statement (parser);
15392 if (structured_block)
15394 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15395 c_parser_consume_token (parser);
15396 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15398 c_parser_skip_to_end_of_block_or_statement (parser);
15399 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15400 c_parser_consume_token (parser);
15403 return;
15405 case POSTINCREMENT_EXPR:
15406 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15407 code = OMP_ATOMIC_CAPTURE_OLD;
15408 /* FALLTHROUGH */
15409 case PREINCREMENT_EXPR:
15410 lhs = TREE_OPERAND (lhs, 0);
15411 unfolded_lhs = NULL_TREE;
15412 opcode = PLUS_EXPR;
15413 rhs = integer_one_node;
15414 break;
15416 case POSTDECREMENT_EXPR:
15417 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15418 code = OMP_ATOMIC_CAPTURE_OLD;
15419 /* FALLTHROUGH */
15420 case PREDECREMENT_EXPR:
15421 lhs = TREE_OPERAND (lhs, 0);
15422 unfolded_lhs = NULL_TREE;
15423 opcode = MINUS_EXPR;
15424 rhs = integer_one_node;
15425 break;
15427 case COMPOUND_EXPR:
15428 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15429 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15430 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15431 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15432 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15433 (TREE_OPERAND (lhs, 1), 0), 0)))
15434 == BOOLEAN_TYPE)
15435 /* Undo effects of boolean_increment for post {in,de}crement. */
15436 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15437 /* FALLTHRU */
15438 case MODIFY_EXPR:
15439 if (TREE_CODE (lhs) == MODIFY_EXPR
15440 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15442 /* Undo effects of boolean_increment. */
15443 if (integer_onep (TREE_OPERAND (lhs, 1)))
15445 /* This is pre or post increment. */
15446 rhs = TREE_OPERAND (lhs, 1);
15447 lhs = TREE_OPERAND (lhs, 0);
15448 unfolded_lhs = NULL_TREE;
15449 opcode = NOP_EXPR;
15450 if (code == OMP_ATOMIC_CAPTURE_NEW
15451 && !structured_block
15452 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15453 code = OMP_ATOMIC_CAPTURE_OLD;
15454 break;
15456 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15457 && TREE_OPERAND (lhs, 0)
15458 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15460 /* This is pre or post decrement. */
15461 rhs = TREE_OPERAND (lhs, 1);
15462 lhs = TREE_OPERAND (lhs, 0);
15463 unfolded_lhs = NULL_TREE;
15464 opcode = NOP_EXPR;
15465 if (code == OMP_ATOMIC_CAPTURE_NEW
15466 && !structured_block
15467 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15468 code = OMP_ATOMIC_CAPTURE_OLD;
15469 break;
15472 /* FALLTHRU */
15473 default:
15474 if (!lvalue_p (unfolded_lhs))
15475 lhs = non_lvalue (lhs);
15476 switch (c_parser_peek_token (parser)->type)
15478 case CPP_MULT_EQ:
15479 opcode = MULT_EXPR;
15480 break;
15481 case CPP_DIV_EQ:
15482 opcode = TRUNC_DIV_EXPR;
15483 break;
15484 case CPP_PLUS_EQ:
15485 opcode = PLUS_EXPR;
15486 break;
15487 case CPP_MINUS_EQ:
15488 opcode = MINUS_EXPR;
15489 break;
15490 case CPP_LSHIFT_EQ:
15491 opcode = LSHIFT_EXPR;
15492 break;
15493 case CPP_RSHIFT_EQ:
15494 opcode = RSHIFT_EXPR;
15495 break;
15496 case CPP_AND_EQ:
15497 opcode = BIT_AND_EXPR;
15498 break;
15499 case CPP_OR_EQ:
15500 opcode = BIT_IOR_EXPR;
15501 break;
15502 case CPP_XOR_EQ:
15503 opcode = BIT_XOR_EXPR;
15504 break;
15505 case CPP_EQ:
15506 c_parser_consume_token (parser);
15507 eloc = c_parser_peek_token (parser)->location;
15508 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15509 rhs1 = expr.value;
15510 switch (TREE_CODE (rhs1))
15512 case MULT_EXPR:
15513 case TRUNC_DIV_EXPR:
15514 case RDIV_EXPR:
15515 case PLUS_EXPR:
15516 case MINUS_EXPR:
15517 case LSHIFT_EXPR:
15518 case RSHIFT_EXPR:
15519 case BIT_AND_EXPR:
15520 case BIT_IOR_EXPR:
15521 case BIT_XOR_EXPR:
15522 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15524 opcode = TREE_CODE (rhs1);
15525 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15526 true);
15527 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15528 true);
15529 goto stmt_done;
15531 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15533 opcode = TREE_CODE (rhs1);
15534 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15535 true);
15536 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15537 true);
15538 swapped = !commutative_tree_code (opcode);
15539 goto stmt_done;
15541 break;
15542 case ERROR_MARK:
15543 goto saw_error;
15544 default:
15545 break;
15547 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15549 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15551 code = OMP_ATOMIC_CAPTURE_OLD;
15552 v = lhs;
15553 lhs = NULL_TREE;
15554 expr = default_function_array_read_conversion (eloc, expr);
15555 unfolded_lhs1 = expr.value;
15556 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15557 rhs1 = NULL_TREE;
15558 c_parser_consume_token (parser);
15559 goto restart;
15561 if (structured_block)
15563 opcode = NOP_EXPR;
15564 expr = default_function_array_read_conversion (eloc, expr);
15565 rhs = c_fully_fold (expr.value, false, NULL, true);
15566 rhs1 = NULL_TREE;
15567 goto stmt_done;
15570 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15571 goto saw_error;
15572 default:
15573 c_parser_error (parser,
15574 "invalid operator for %<#pragma omp atomic%>");
15575 goto saw_error;
15578 /* Arrange to pass the location of the assignment operator to
15579 c_finish_omp_atomic. */
15580 loc = c_parser_peek_token (parser)->location;
15581 c_parser_consume_token (parser);
15582 eloc = c_parser_peek_token (parser)->location;
15583 expr = c_parser_expression (parser);
15584 expr = default_function_array_read_conversion (eloc, expr);
15585 rhs = expr.value;
15586 rhs = c_fully_fold (rhs, false, NULL, true);
15587 break;
15589 stmt_done:
15590 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15592 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15593 goto saw_error;
15594 v = c_parser_cast_expression (parser, NULL).value;
15595 non_lvalue_p = !lvalue_p (v);
15596 v = c_fully_fold (v, false, NULL, true);
15597 if (v == error_mark_node)
15598 goto saw_error;
15599 if (non_lvalue_p)
15600 v = non_lvalue (v);
15601 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15602 goto saw_error;
15603 eloc = c_parser_peek_token (parser)->location;
15604 expr = c_parser_cast_expression (parser, NULL);
15605 lhs1 = expr.value;
15606 expr = default_function_array_read_conversion (eloc, expr);
15607 unfolded_lhs1 = expr.value;
15608 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15609 if (lhs1 == error_mark_node)
15610 goto saw_error;
15611 if (!lvalue_p (unfolded_lhs1))
15612 lhs1 = non_lvalue (lhs1);
15614 if (structured_block)
15616 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15617 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15619 done:
15620 if (unfolded_lhs && unfolded_lhs1
15621 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15623 error ("%<#pragma omp atomic capture%> uses two different "
15624 "expressions for memory");
15625 stmt = error_mark_node;
15627 else
15628 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15629 swapped, seq_cst);
15630 if (stmt != error_mark_node)
15631 add_stmt (stmt);
15633 if (!structured_block)
15634 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15638 /* OpenMP 2.5:
15639 # pragma omp barrier new-line
15642 static void
15643 c_parser_omp_barrier (c_parser *parser)
15645 location_t loc = c_parser_peek_token (parser)->location;
15646 c_parser_consume_pragma (parser);
15647 c_parser_skip_to_pragma_eol (parser);
15649 c_finish_omp_barrier (loc);
15652 /* OpenMP 2.5:
15653 # pragma omp critical [(name)] new-line
15654 structured-block
15656 OpenMP 4.5:
15657 # pragma omp critical [(name) [hint(expression)]] new-line
15659 LOC is the location of the #pragma itself. */
15661 #define OMP_CRITICAL_CLAUSE_MASK \
15662 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15664 static tree
15665 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15667 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15669 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15671 c_parser_consume_token (parser);
15672 if (c_parser_next_token_is (parser, CPP_NAME))
15674 name = c_parser_peek_token (parser)->value;
15675 c_parser_consume_token (parser);
15676 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15678 else
15679 c_parser_error (parser, "expected identifier");
15681 clauses = c_parser_omp_all_clauses (parser,
15682 OMP_CRITICAL_CLAUSE_MASK,
15683 "#pragma omp critical");
15685 else
15687 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15688 c_parser_error (parser, "expected %<(%> or end of line");
15689 c_parser_skip_to_pragma_eol (parser);
15692 stmt = c_parser_omp_structured_block (parser, if_p);
15693 return c_finish_omp_critical (loc, stmt, name, clauses);
15696 /* OpenMP 2.5:
15697 # pragma omp flush flush-vars[opt] new-line
15699 flush-vars:
15700 ( variable-list ) */
15702 static void
15703 c_parser_omp_flush (c_parser *parser)
15705 location_t loc = c_parser_peek_token (parser)->location;
15706 c_parser_consume_pragma (parser);
15707 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15708 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15709 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15710 c_parser_error (parser, "expected %<(%> or end of line");
15711 c_parser_skip_to_pragma_eol (parser);
15713 c_finish_omp_flush (loc);
15716 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15717 The real trick here is to determine the loop control variable early
15718 so that we can push a new decl if necessary to make it private.
15719 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15720 respectively. */
15722 static tree
15723 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15724 tree clauses, tree *cclauses, bool *if_p)
15726 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15727 tree declv, condv, incrv, initv, ret = NULL_TREE;
15728 tree pre_body = NULL_TREE, this_pre_body;
15729 tree ordered_cl = NULL_TREE;
15730 bool fail = false, open_brace_parsed = false;
15731 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15732 location_t for_loc;
15733 bool tiling = false;
15734 vec<tree, va_gc> *for_block = make_tree_vector ();
15736 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15737 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15738 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15739 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15741 tiling = true;
15742 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15744 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15745 && OMP_CLAUSE_ORDERED_EXPR (cl))
15747 ordered_cl = cl;
15748 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15751 if (ordered && ordered < collapse)
15753 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15754 "%<ordered%> clause parameter is less than %<collapse%>");
15755 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15756 = build_int_cst (NULL_TREE, collapse);
15757 ordered = collapse;
15759 if (ordered)
15761 for (tree *pc = &clauses; *pc; )
15762 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15764 error_at (OMP_CLAUSE_LOCATION (*pc),
15765 "%<linear%> clause may not be specified together "
15766 "with %<ordered%> clause with a parameter");
15767 *pc = OMP_CLAUSE_CHAIN (*pc);
15769 else
15770 pc = &OMP_CLAUSE_CHAIN (*pc);
15773 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15774 count = ordered ? ordered : collapse;
15776 declv = make_tree_vec (count);
15777 initv = make_tree_vec (count);
15778 condv = make_tree_vec (count);
15779 incrv = make_tree_vec (count);
15781 if (code != CILK_FOR
15782 && !c_parser_next_token_is_keyword (parser, RID_FOR))
15784 c_parser_error (parser, "for statement expected");
15785 return NULL;
15787 if (code == CILK_FOR
15788 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
15790 c_parser_error (parser, "_Cilk_for statement expected");
15791 return NULL;
15793 for_loc = c_parser_peek_token (parser)->location;
15794 c_parser_consume_token (parser);
15796 for (i = 0; i < count; i++)
15798 int bracecount = 0;
15800 matching_parens parens;
15801 if (!parens.require_open (parser))
15802 goto pop_scopes;
15804 /* Parse the initialization declaration or expression. */
15805 if (c_parser_next_tokens_start_declaration (parser))
15807 if (i > 0)
15808 vec_safe_push (for_block, c_begin_compound_stmt (true));
15809 this_pre_body = push_stmt_list ();
15810 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15811 NULL, vNULL);
15812 if (this_pre_body)
15814 this_pre_body = pop_stmt_list (this_pre_body);
15815 if (pre_body)
15817 tree t = pre_body;
15818 pre_body = push_stmt_list ();
15819 add_stmt (t);
15820 add_stmt (this_pre_body);
15821 pre_body = pop_stmt_list (pre_body);
15823 else
15824 pre_body = this_pre_body;
15826 decl = check_for_loop_decls (for_loc, flag_isoc99);
15827 if (decl == NULL)
15828 goto error_init;
15829 if (DECL_INITIAL (decl) == error_mark_node)
15830 decl = error_mark_node;
15831 init = decl;
15833 else if (c_parser_next_token_is (parser, CPP_NAME)
15834 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15836 struct c_expr decl_exp;
15837 struct c_expr init_exp;
15838 location_t init_loc;
15840 decl_exp = c_parser_postfix_expression (parser);
15841 decl = decl_exp.value;
15843 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15845 init_loc = c_parser_peek_token (parser)->location;
15846 init_exp = c_parser_expr_no_commas (parser, NULL);
15847 init_exp = default_function_array_read_conversion (init_loc,
15848 init_exp);
15849 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15850 NOP_EXPR, init_loc, init_exp.value,
15851 init_exp.original_type);
15852 init = c_process_expr_stmt (init_loc, init);
15854 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15856 else
15858 error_init:
15859 c_parser_error (parser,
15860 "expected iteration declaration or initialization");
15861 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15862 "expected %<)%>");
15863 fail = true;
15864 goto parse_next;
15867 /* Parse the loop condition. */
15868 cond = NULL_TREE;
15869 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15871 location_t cond_loc = c_parser_peek_token (parser)->location;
15872 struct c_expr cond_expr
15873 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15875 cond = cond_expr.value;
15876 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15877 if (COMPARISON_CLASS_P (cond))
15879 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15880 op0 = c_fully_fold (op0, false, NULL);
15881 op1 = c_fully_fold (op1, false, NULL);
15882 TREE_OPERAND (cond, 0) = op0;
15883 TREE_OPERAND (cond, 1) = op1;
15885 switch (cond_expr.original_code)
15887 case GT_EXPR:
15888 case GE_EXPR:
15889 case LT_EXPR:
15890 case LE_EXPR:
15891 break;
15892 case NE_EXPR:
15893 if (code == CILK_SIMD || code == CILK_FOR)
15894 break;
15895 /* FALLTHRU. */
15896 default:
15897 /* Can't be cond = error_mark_node, because we want to preserve
15898 the location until c_finish_omp_for. */
15899 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15900 break;
15902 protected_set_expr_location (cond, cond_loc);
15904 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15906 /* Parse the increment expression. */
15907 incr = NULL_TREE;
15908 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15910 location_t incr_loc = c_parser_peek_token (parser)->location;
15912 incr = c_process_expr_stmt (incr_loc,
15913 c_parser_expression (parser).value);
15915 parens.skip_until_found_close (parser);
15917 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15918 fail = true;
15919 else
15921 TREE_VEC_ELT (declv, i) = decl;
15922 TREE_VEC_ELT (initv, i) = init;
15923 TREE_VEC_ELT (condv, i) = cond;
15924 TREE_VEC_ELT (incrv, i) = incr;
15927 parse_next:
15928 if (i == count - 1)
15929 break;
15931 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15932 in between the collapsed for loops to be still considered perfectly
15933 nested. Hopefully the final version clarifies this.
15934 For now handle (multiple) {'s and empty statements. */
15937 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15939 c_parser_consume_token (parser);
15940 break;
15942 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15944 c_parser_consume_token (parser);
15945 bracecount++;
15947 else if (bracecount
15948 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15949 c_parser_consume_token (parser);
15950 else
15952 c_parser_error (parser, "not enough perfectly nested loops");
15953 if (bracecount)
15955 open_brace_parsed = true;
15956 bracecount--;
15958 fail = true;
15959 count = 0;
15960 break;
15963 while (1);
15965 nbraces += bracecount;
15968 if (nbraces)
15969 if_p = NULL;
15971 save_break = c_break_label;
15972 if (code == CILK_SIMD)
15973 c_break_label = build_int_cst (size_type_node, 2);
15974 else
15975 c_break_label = size_one_node;
15976 save_cont = c_cont_label;
15977 c_cont_label = NULL_TREE;
15978 body = push_stmt_list ();
15980 if (open_brace_parsed)
15982 location_t here = c_parser_peek_token (parser)->location;
15983 stmt = c_begin_compound_stmt (true);
15984 c_parser_compound_statement_nostart (parser);
15985 add_stmt (c_end_compound_stmt (here, stmt, true));
15987 else
15988 add_stmt (c_parser_c99_block_statement (parser, if_p));
15989 if (c_cont_label)
15991 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15992 SET_EXPR_LOCATION (t, loc);
15993 add_stmt (t);
15996 body = pop_stmt_list (body);
15997 c_break_label = save_break;
15998 c_cont_label = save_cont;
16000 while (nbraces)
16002 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16004 c_parser_consume_token (parser);
16005 nbraces--;
16007 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
16008 c_parser_consume_token (parser);
16009 else
16011 c_parser_error (parser, "collapsed loops not perfectly nested");
16012 while (nbraces)
16014 location_t here = c_parser_peek_token (parser)->location;
16015 stmt = c_begin_compound_stmt (true);
16016 add_stmt (body);
16017 c_parser_compound_statement_nostart (parser);
16018 body = c_end_compound_stmt (here, stmt, true);
16019 nbraces--;
16021 goto pop_scopes;
16025 /* Only bother calling c_finish_omp_for if we haven't already generated
16026 an error from the initialization parsing. */
16027 if (!fail)
16029 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
16030 incrv, body, pre_body);
16032 /* Check for iterators appearing in lb, b or incr expressions. */
16033 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
16034 stmt = NULL_TREE;
16036 if (stmt)
16038 add_stmt (stmt);
16040 if (cclauses != NULL
16041 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
16043 tree *c;
16044 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
16045 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
16046 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
16047 c = &OMP_CLAUSE_CHAIN (*c);
16048 else
16050 for (i = 0; i < count; i++)
16051 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
16052 break;
16053 if (i == count)
16054 c = &OMP_CLAUSE_CHAIN (*c);
16055 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
16057 error_at (loc,
16058 "iteration variable %qD should not be firstprivate",
16059 OMP_CLAUSE_DECL (*c));
16060 *c = OMP_CLAUSE_CHAIN (*c);
16062 else
16064 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16065 tree l = *c;
16066 *c = OMP_CLAUSE_CHAIN (*c);
16067 if (code == OMP_SIMD)
16069 OMP_CLAUSE_CHAIN (l)
16070 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16071 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
16073 else
16075 OMP_CLAUSE_CHAIN (l) = clauses;
16076 clauses = l;
16081 OMP_FOR_CLAUSES (stmt) = clauses;
16083 ret = stmt;
16085 pop_scopes:
16086 while (!for_block->is_empty ())
16088 /* FIXME diagnostics: LOC below should be the actual location of
16089 this particular for block. We need to build a list of
16090 locations to go along with FOR_BLOCK. */
16091 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
16092 add_stmt (stmt);
16094 release_tree_vector (for_block);
16095 return ret;
16098 /* Helper function for OpenMP parsing, split clauses and call
16099 finish_omp_clauses on each of the set of clauses afterwards. */
16101 static void
16102 omp_split_clauses (location_t loc, enum tree_code code,
16103 omp_clause_mask mask, tree clauses, tree *cclauses)
16105 int i;
16106 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
16107 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
16108 if (cclauses[i])
16109 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
16112 /* OpenMP 4.0:
16113 #pragma omp simd simd-clause[optseq] new-line
16114 for-loop
16116 LOC is the location of the #pragma token.
16119 #define OMP_SIMD_CLAUSE_MASK \
16120 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
16121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16129 static tree
16130 c_parser_omp_simd (location_t loc, c_parser *parser,
16131 char *p_name, omp_clause_mask mask, tree *cclauses,
16132 bool *if_p)
16134 tree block, clauses, ret;
16136 strcat (p_name, " simd");
16137 mask |= OMP_SIMD_CLAUSE_MASK;
16139 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16140 if (cclauses)
16142 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
16143 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
16144 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
16145 OMP_CLAUSE_ORDERED);
16146 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
16148 error_at (OMP_CLAUSE_LOCATION (c),
16149 "%<ordered%> clause with parameter may not be specified "
16150 "on %qs construct", p_name);
16151 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
16155 block = c_begin_compound_stmt (true);
16156 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
16157 block = c_end_compound_stmt (loc, block, true);
16158 add_stmt (block);
16160 return ret;
16163 /* OpenMP 2.5:
16164 #pragma omp for for-clause[optseq] new-line
16165 for-loop
16167 OpenMP 4.0:
16168 #pragma omp for simd for-simd-clause[optseq] new-line
16169 for-loop
16171 LOC is the location of the #pragma token.
16174 #define OMP_FOR_CLAUSE_MASK \
16175 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
16181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
16182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16185 static tree
16186 c_parser_omp_for (location_t loc, c_parser *parser,
16187 char *p_name, omp_clause_mask mask, tree *cclauses,
16188 bool *if_p)
16190 tree block, clauses, ret;
16192 strcat (p_name, " for");
16193 mask |= OMP_FOR_CLAUSE_MASK;
16194 /* parallel for{, simd} disallows nowait clause, but for
16195 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16196 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16197 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16198 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16199 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16200 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16202 if (c_parser_next_token_is (parser, CPP_NAME))
16204 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16206 if (strcmp (p, "simd") == 0)
16208 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16209 if (cclauses == NULL)
16210 cclauses = cclauses_buf;
16212 c_parser_consume_token (parser);
16213 if (!flag_openmp) /* flag_openmp_simd */
16214 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16215 if_p);
16216 block = c_begin_compound_stmt (true);
16217 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16218 block = c_end_compound_stmt (loc, block, true);
16219 if (ret == NULL_TREE)
16220 return ret;
16221 ret = make_node (OMP_FOR);
16222 TREE_TYPE (ret) = void_type_node;
16223 OMP_FOR_BODY (ret) = block;
16224 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16225 SET_EXPR_LOCATION (ret, loc);
16226 add_stmt (ret);
16227 return ret;
16230 if (!flag_openmp) /* flag_openmp_simd */
16232 c_parser_skip_to_pragma_eol (parser, false);
16233 return NULL_TREE;
16236 /* Composite distribute parallel for disallows linear clause. */
16237 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16238 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16240 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16241 if (cclauses)
16243 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16244 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16247 block = c_begin_compound_stmt (true);
16248 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16249 block = c_end_compound_stmt (loc, block, true);
16250 add_stmt (block);
16252 return ret;
16255 /* OpenMP 2.5:
16256 # pragma omp master new-line
16257 structured-block
16259 LOC is the location of the #pragma token.
16262 static tree
16263 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16265 c_parser_skip_to_pragma_eol (parser);
16266 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16267 if_p));
16270 /* OpenMP 2.5:
16271 # pragma omp ordered new-line
16272 structured-block
16274 OpenMP 4.5:
16275 # pragma omp ordered ordered-clauses new-line
16276 structured-block
16278 # pragma omp ordered depend-clauses new-line */
16280 #define OMP_ORDERED_CLAUSE_MASK \
16281 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16284 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16285 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16287 static bool
16288 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16289 bool *if_p)
16291 location_t loc = c_parser_peek_token (parser)->location;
16292 c_parser_consume_pragma (parser);
16294 if (context != pragma_stmt && context != pragma_compound)
16296 c_parser_error (parser, "expected declaration specifiers");
16297 c_parser_skip_to_pragma_eol (parser, false);
16298 return false;
16301 if (c_parser_next_token_is (parser, CPP_NAME))
16303 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16305 if (!strcmp ("depend", p))
16307 if (!flag_openmp) /* flag_openmp_simd */
16309 c_parser_skip_to_pragma_eol (parser, false);
16310 return false;
16312 if (context == pragma_stmt)
16314 error_at (loc,
16315 "%<#pragma omp ordered%> with %<depend%> clause may "
16316 "only be used in compound statements");
16317 c_parser_skip_to_pragma_eol (parser, false);
16318 return false;
16321 tree clauses
16322 = c_parser_omp_all_clauses (parser,
16323 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16324 "#pragma omp ordered");
16325 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16326 return false;
16330 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16331 "#pragma omp ordered");
16333 if (!flag_openmp /* flag_openmp_simd */
16334 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16335 return false;
16337 c_finish_omp_ordered (loc, clauses,
16338 c_parser_omp_structured_block (parser, if_p));
16339 return true;
16342 /* OpenMP 2.5:
16344 section-scope:
16345 { section-sequence }
16347 section-sequence:
16348 section-directive[opt] structured-block
16349 section-sequence section-directive structured-block
16351 SECTIONS_LOC is the location of the #pragma omp sections. */
16353 static tree
16354 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16356 tree stmt, substmt;
16357 bool error_suppress = false;
16358 location_t loc;
16360 loc = c_parser_peek_token (parser)->location;
16361 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16363 /* Avoid skipping until the end of the block. */
16364 parser->error = false;
16365 return NULL_TREE;
16368 stmt = push_stmt_list ();
16370 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16372 substmt = c_parser_omp_structured_block (parser, NULL);
16373 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16374 SET_EXPR_LOCATION (substmt, loc);
16375 add_stmt (substmt);
16378 while (1)
16380 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16381 break;
16382 if (c_parser_next_token_is (parser, CPP_EOF))
16383 break;
16385 loc = c_parser_peek_token (parser)->location;
16386 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16388 c_parser_consume_pragma (parser);
16389 c_parser_skip_to_pragma_eol (parser);
16390 error_suppress = false;
16392 else if (!error_suppress)
16394 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16395 error_suppress = true;
16398 substmt = c_parser_omp_structured_block (parser, NULL);
16399 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16400 SET_EXPR_LOCATION (substmt, loc);
16401 add_stmt (substmt);
16403 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16404 "expected %<#pragma omp section%> or %<}%>");
16406 substmt = pop_stmt_list (stmt);
16408 stmt = make_node (OMP_SECTIONS);
16409 SET_EXPR_LOCATION (stmt, sections_loc);
16410 TREE_TYPE (stmt) = void_type_node;
16411 OMP_SECTIONS_BODY (stmt) = substmt;
16413 return add_stmt (stmt);
16416 /* OpenMP 2.5:
16417 # pragma omp sections sections-clause[optseq] newline
16418 sections-scope
16420 LOC is the location of the #pragma token.
16423 #define OMP_SECTIONS_CLAUSE_MASK \
16424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16430 static tree
16431 c_parser_omp_sections (location_t loc, c_parser *parser,
16432 char *p_name, omp_clause_mask mask, tree *cclauses)
16434 tree block, clauses, ret;
16436 strcat (p_name, " sections");
16437 mask |= OMP_SECTIONS_CLAUSE_MASK;
16438 if (cclauses)
16439 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16441 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16442 if (cclauses)
16444 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16445 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16448 block = c_begin_compound_stmt (true);
16449 ret = c_parser_omp_sections_scope (loc, parser);
16450 if (ret)
16451 OMP_SECTIONS_CLAUSES (ret) = clauses;
16452 block = c_end_compound_stmt (loc, block, true);
16453 add_stmt (block);
16455 return ret;
16458 /* OpenMP 2.5:
16459 # pragma omp parallel parallel-clause[optseq] new-line
16460 structured-block
16461 # pragma omp parallel for parallel-for-clause[optseq] new-line
16462 structured-block
16463 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16464 structured-block
16466 OpenMP 4.0:
16467 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16468 structured-block
16470 LOC is the location of the #pragma token.
16473 #define OMP_PARALLEL_CLAUSE_MASK \
16474 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16484 static tree
16485 c_parser_omp_parallel (location_t loc, c_parser *parser,
16486 char *p_name, omp_clause_mask mask, tree *cclauses,
16487 bool *if_p)
16489 tree stmt, clauses, block;
16491 strcat (p_name, " parallel");
16492 mask |= OMP_PARALLEL_CLAUSE_MASK;
16493 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16494 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16495 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16496 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16498 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16500 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16501 if (cclauses == NULL)
16502 cclauses = cclauses_buf;
16504 c_parser_consume_token (parser);
16505 if (!flag_openmp) /* flag_openmp_simd */
16506 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16507 block = c_begin_omp_parallel ();
16508 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16509 stmt
16510 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16511 block);
16512 if (ret == NULL_TREE)
16513 return ret;
16514 OMP_PARALLEL_COMBINED (stmt) = 1;
16515 return stmt;
16517 /* When combined with distribute, parallel has to be followed by for.
16518 #pragma omp target parallel is allowed though. */
16519 else if (cclauses
16520 && (mask & (OMP_CLAUSE_MASK_1
16521 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16523 error_at (loc, "expected %<for%> after %qs", p_name);
16524 c_parser_skip_to_pragma_eol (parser);
16525 return NULL_TREE;
16527 else if (!flag_openmp) /* flag_openmp_simd */
16529 c_parser_skip_to_pragma_eol (parser, false);
16530 return NULL_TREE;
16532 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16534 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16535 if (strcmp (p, "sections") == 0)
16537 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16538 if (cclauses == NULL)
16539 cclauses = cclauses_buf;
16541 c_parser_consume_token (parser);
16542 block = c_begin_omp_parallel ();
16543 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16544 stmt = c_finish_omp_parallel (loc,
16545 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16546 block);
16547 OMP_PARALLEL_COMBINED (stmt) = 1;
16548 return stmt;
16552 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16553 if (cclauses)
16555 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16556 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16559 block = c_begin_omp_parallel ();
16560 c_parser_statement (parser, if_p);
16561 stmt = c_finish_omp_parallel (loc, clauses, block);
16563 return stmt;
16566 /* OpenMP 2.5:
16567 # pragma omp single single-clause[optseq] new-line
16568 structured-block
16570 LOC is the location of the #pragma.
16573 #define OMP_SINGLE_CLAUSE_MASK \
16574 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16579 static tree
16580 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16582 tree stmt = make_node (OMP_SINGLE);
16583 SET_EXPR_LOCATION (stmt, loc);
16584 TREE_TYPE (stmt) = void_type_node;
16586 OMP_SINGLE_CLAUSES (stmt)
16587 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16588 "#pragma omp single");
16589 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16591 return add_stmt (stmt);
16594 /* OpenMP 3.0:
16595 # pragma omp task task-clause[optseq] new-line
16597 LOC is the location of the #pragma.
16600 #define OMP_TASK_CLAUSE_MASK \
16601 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16612 static tree
16613 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16615 tree clauses, block;
16617 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16618 "#pragma omp task");
16620 block = c_begin_omp_task ();
16621 c_parser_statement (parser, if_p);
16622 return c_finish_omp_task (loc, clauses, block);
16625 /* OpenMP 3.0:
16626 # pragma omp taskwait new-line
16629 static void
16630 c_parser_omp_taskwait (c_parser *parser)
16632 location_t loc = c_parser_peek_token (parser)->location;
16633 c_parser_consume_pragma (parser);
16634 c_parser_skip_to_pragma_eol (parser);
16636 c_finish_omp_taskwait (loc);
16639 /* OpenMP 3.1:
16640 # pragma omp taskyield new-line
16643 static void
16644 c_parser_omp_taskyield (c_parser *parser)
16646 location_t loc = c_parser_peek_token (parser)->location;
16647 c_parser_consume_pragma (parser);
16648 c_parser_skip_to_pragma_eol (parser);
16650 c_finish_omp_taskyield (loc);
16653 /* OpenMP 4.0:
16654 # pragma omp taskgroup new-line
16657 static tree
16658 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16660 location_t loc = c_parser_peek_token (parser)->location;
16661 c_parser_skip_to_pragma_eol (parser);
16662 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16663 if_p));
16666 /* OpenMP 4.0:
16667 # pragma omp cancel cancel-clause[optseq] new-line
16669 LOC is the location of the #pragma.
16672 #define OMP_CANCEL_CLAUSE_MASK \
16673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16679 static void
16680 c_parser_omp_cancel (c_parser *parser)
16682 location_t loc = c_parser_peek_token (parser)->location;
16684 c_parser_consume_pragma (parser);
16685 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16686 "#pragma omp cancel");
16688 c_finish_omp_cancel (loc, clauses);
16691 /* OpenMP 4.0:
16692 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16694 LOC is the location of the #pragma.
16697 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16703 static void
16704 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16706 location_t loc = c_parser_peek_token (parser)->location;
16707 tree clauses;
16708 bool point_seen = false;
16710 c_parser_consume_pragma (parser);
16711 if (c_parser_next_token_is (parser, CPP_NAME))
16713 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16714 if (strcmp (p, "point") == 0)
16716 c_parser_consume_token (parser);
16717 point_seen = true;
16720 if (!point_seen)
16722 c_parser_error (parser, "expected %<point%>");
16723 c_parser_skip_to_pragma_eol (parser);
16724 return;
16727 if (context != pragma_compound)
16729 if (context == pragma_stmt)
16730 error_at (loc,
16731 "%<#pragma %s%> may only be used in compound statements",
16732 "omp cancellation point");
16733 else
16734 c_parser_error (parser, "expected declaration specifiers");
16735 c_parser_skip_to_pragma_eol (parser, false);
16736 return;
16739 clauses
16740 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16741 "#pragma omp cancellation point");
16743 c_finish_omp_cancellation_point (loc, clauses);
16746 /* OpenMP 4.0:
16747 #pragma omp distribute distribute-clause[optseq] new-line
16748 for-loop */
16750 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16751 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16757 static tree
16758 c_parser_omp_distribute (location_t loc, c_parser *parser,
16759 char *p_name, omp_clause_mask mask, tree *cclauses,
16760 bool *if_p)
16762 tree clauses, block, ret;
16764 strcat (p_name, " distribute");
16765 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16767 if (c_parser_next_token_is (parser, CPP_NAME))
16769 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16770 bool simd = false;
16771 bool parallel = false;
16773 if (strcmp (p, "simd") == 0)
16774 simd = true;
16775 else
16776 parallel = strcmp (p, "parallel") == 0;
16777 if (parallel || simd)
16779 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16780 if (cclauses == NULL)
16781 cclauses = cclauses_buf;
16782 c_parser_consume_token (parser);
16783 if (!flag_openmp) /* flag_openmp_simd */
16785 if (simd)
16786 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16787 if_p);
16788 else
16789 return c_parser_omp_parallel (loc, parser, p_name, mask,
16790 cclauses, if_p);
16792 block = c_begin_compound_stmt (true);
16793 if (simd)
16794 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16795 if_p);
16796 else
16797 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16798 if_p);
16799 block = c_end_compound_stmt (loc, block, true);
16800 if (ret == NULL)
16801 return ret;
16802 ret = make_node (OMP_DISTRIBUTE);
16803 TREE_TYPE (ret) = void_type_node;
16804 OMP_FOR_BODY (ret) = block;
16805 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16806 SET_EXPR_LOCATION (ret, loc);
16807 add_stmt (ret);
16808 return ret;
16811 if (!flag_openmp) /* flag_openmp_simd */
16813 c_parser_skip_to_pragma_eol (parser, false);
16814 return NULL_TREE;
16817 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16818 if (cclauses)
16820 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16821 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16824 block = c_begin_compound_stmt (true);
16825 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16826 if_p);
16827 block = c_end_compound_stmt (loc, block, true);
16828 add_stmt (block);
16830 return ret;
16833 /* OpenMP 4.0:
16834 # pragma omp teams teams-clause[optseq] new-line
16835 structured-block */
16837 #define OMP_TEAMS_CLAUSE_MASK \
16838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16846 static tree
16847 c_parser_omp_teams (location_t loc, c_parser *parser,
16848 char *p_name, omp_clause_mask mask, tree *cclauses,
16849 bool *if_p)
16851 tree clauses, block, ret;
16853 strcat (p_name, " teams");
16854 mask |= OMP_TEAMS_CLAUSE_MASK;
16856 if (c_parser_next_token_is (parser, CPP_NAME))
16858 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16859 if (strcmp (p, "distribute") == 0)
16861 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16862 if (cclauses == NULL)
16863 cclauses = cclauses_buf;
16865 c_parser_consume_token (parser);
16866 if (!flag_openmp) /* flag_openmp_simd */
16867 return c_parser_omp_distribute (loc, parser, p_name, mask,
16868 cclauses, if_p);
16869 block = c_begin_compound_stmt (true);
16870 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16871 if_p);
16872 block = c_end_compound_stmt (loc, block, true);
16873 if (ret == NULL)
16874 return ret;
16875 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16876 ret = make_node (OMP_TEAMS);
16877 TREE_TYPE (ret) = void_type_node;
16878 OMP_TEAMS_CLAUSES (ret) = clauses;
16879 OMP_TEAMS_BODY (ret) = block;
16880 OMP_TEAMS_COMBINED (ret) = 1;
16881 return add_stmt (ret);
16884 if (!flag_openmp) /* flag_openmp_simd */
16886 c_parser_skip_to_pragma_eol (parser, false);
16887 return NULL_TREE;
16890 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16891 if (cclauses)
16893 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16894 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16897 tree stmt = make_node (OMP_TEAMS);
16898 TREE_TYPE (stmt) = void_type_node;
16899 OMP_TEAMS_CLAUSES (stmt) = clauses;
16900 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16902 return add_stmt (stmt);
16905 /* OpenMP 4.0:
16906 # pragma omp target data target-data-clause[optseq] new-line
16907 structured-block */
16909 #define OMP_TARGET_DATA_CLAUSE_MASK \
16910 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16915 static tree
16916 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16918 tree clauses
16919 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16920 "#pragma omp target data");
16921 int map_seen = 0;
16922 for (tree *pc = &clauses; *pc;)
16924 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16925 switch (OMP_CLAUSE_MAP_KIND (*pc))
16927 case GOMP_MAP_TO:
16928 case GOMP_MAP_ALWAYS_TO:
16929 case GOMP_MAP_FROM:
16930 case GOMP_MAP_ALWAYS_FROM:
16931 case GOMP_MAP_TOFROM:
16932 case GOMP_MAP_ALWAYS_TOFROM:
16933 case GOMP_MAP_ALLOC:
16934 map_seen = 3;
16935 break;
16936 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16937 case GOMP_MAP_ALWAYS_POINTER:
16938 break;
16939 default:
16940 map_seen |= 1;
16941 error_at (OMP_CLAUSE_LOCATION (*pc),
16942 "%<#pragma omp target data%> with map-type other "
16943 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16944 "on %<map%> clause");
16945 *pc = OMP_CLAUSE_CHAIN (*pc);
16946 continue;
16948 pc = &OMP_CLAUSE_CHAIN (*pc);
16951 if (map_seen != 3)
16953 if (map_seen == 0)
16954 error_at (loc,
16955 "%<#pragma omp target data%> must contain at least "
16956 "one %<map%> clause");
16957 return NULL_TREE;
16960 tree stmt = make_node (OMP_TARGET_DATA);
16961 TREE_TYPE (stmt) = void_type_node;
16962 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16963 keep_next_level ();
16964 tree block = c_begin_compound_stmt (true);
16965 add_stmt (c_parser_omp_structured_block (parser, if_p));
16966 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16968 SET_EXPR_LOCATION (stmt, loc);
16969 return add_stmt (stmt);
16972 /* OpenMP 4.0:
16973 # pragma omp target update target-update-clause[optseq] new-line */
16975 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16976 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16983 static bool
16984 c_parser_omp_target_update (location_t loc, c_parser *parser,
16985 enum pragma_context context)
16987 if (context == pragma_stmt)
16989 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16990 "omp target update");
16991 c_parser_skip_to_pragma_eol (parser, false);
16992 return false;
16995 tree clauses
16996 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16997 "#pragma omp target update");
16998 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16999 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
17001 error_at (loc,
17002 "%<#pragma omp target update%> must contain at least one "
17003 "%<from%> or %<to%> clauses");
17004 return false;
17007 tree stmt = make_node (OMP_TARGET_UPDATE);
17008 TREE_TYPE (stmt) = void_type_node;
17009 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
17010 SET_EXPR_LOCATION (stmt, loc);
17011 add_stmt (stmt);
17012 return false;
17015 /* OpenMP 4.5:
17016 # pragma omp target enter data target-data-clause[optseq] new-line */
17018 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
17019 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17025 static tree
17026 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
17027 enum pragma_context context)
17029 bool data_seen = false;
17030 if (c_parser_next_token_is (parser, CPP_NAME))
17032 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17033 if (strcmp (p, "data") == 0)
17035 c_parser_consume_token (parser);
17036 data_seen = true;
17039 if (!data_seen)
17041 c_parser_error (parser, "expected %<data%>");
17042 c_parser_skip_to_pragma_eol (parser);
17043 return NULL_TREE;
17046 if (context == pragma_stmt)
17048 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17049 "omp target enter data");
17050 c_parser_skip_to_pragma_eol (parser, false);
17051 return NULL_TREE;
17054 tree clauses
17055 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
17056 "#pragma omp target enter data");
17057 int map_seen = 0;
17058 for (tree *pc = &clauses; *pc;)
17060 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17061 switch (OMP_CLAUSE_MAP_KIND (*pc))
17063 case GOMP_MAP_TO:
17064 case GOMP_MAP_ALWAYS_TO:
17065 case GOMP_MAP_ALLOC:
17066 map_seen = 3;
17067 break;
17068 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17069 case GOMP_MAP_ALWAYS_POINTER:
17070 break;
17071 default:
17072 map_seen |= 1;
17073 error_at (OMP_CLAUSE_LOCATION (*pc),
17074 "%<#pragma omp target enter data%> with map-type other "
17075 "than %<to%> or %<alloc%> on %<map%> clause");
17076 *pc = OMP_CLAUSE_CHAIN (*pc);
17077 continue;
17079 pc = &OMP_CLAUSE_CHAIN (*pc);
17082 if (map_seen != 3)
17084 if (map_seen == 0)
17085 error_at (loc,
17086 "%<#pragma omp target enter data%> must contain at least "
17087 "one %<map%> clause");
17088 return NULL_TREE;
17091 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
17092 TREE_TYPE (stmt) = void_type_node;
17093 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
17094 SET_EXPR_LOCATION (stmt, loc);
17095 add_stmt (stmt);
17096 return stmt;
17099 /* OpenMP 4.5:
17100 # pragma omp target exit data target-data-clause[optseq] new-line */
17102 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
17103 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17109 static tree
17110 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
17111 enum pragma_context context)
17113 bool data_seen = false;
17114 if (c_parser_next_token_is (parser, CPP_NAME))
17116 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17117 if (strcmp (p, "data") == 0)
17119 c_parser_consume_token (parser);
17120 data_seen = true;
17123 if (!data_seen)
17125 c_parser_error (parser, "expected %<data%>");
17126 c_parser_skip_to_pragma_eol (parser);
17127 return NULL_TREE;
17130 if (context == pragma_stmt)
17132 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17133 "omp target exit data");
17134 c_parser_skip_to_pragma_eol (parser, false);
17135 return NULL_TREE;
17138 tree clauses
17139 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
17140 "#pragma omp target exit data");
17142 int map_seen = 0;
17143 for (tree *pc = &clauses; *pc;)
17145 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17146 switch (OMP_CLAUSE_MAP_KIND (*pc))
17148 case GOMP_MAP_FROM:
17149 case GOMP_MAP_ALWAYS_FROM:
17150 case GOMP_MAP_RELEASE:
17151 case GOMP_MAP_DELETE:
17152 map_seen = 3;
17153 break;
17154 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17155 case GOMP_MAP_ALWAYS_POINTER:
17156 break;
17157 default:
17158 map_seen |= 1;
17159 error_at (OMP_CLAUSE_LOCATION (*pc),
17160 "%<#pragma omp target exit data%> with map-type other "
17161 "than %<from%>, %<release%> or %<delete%> on %<map%>"
17162 " clause");
17163 *pc = OMP_CLAUSE_CHAIN (*pc);
17164 continue;
17166 pc = &OMP_CLAUSE_CHAIN (*pc);
17169 if (map_seen != 3)
17171 if (map_seen == 0)
17172 error_at (loc,
17173 "%<#pragma omp target exit data%> must contain at least one "
17174 "%<map%> clause");
17175 return NULL_TREE;
17178 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17179 TREE_TYPE (stmt) = void_type_node;
17180 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17181 SET_EXPR_LOCATION (stmt, loc);
17182 add_stmt (stmt);
17183 return stmt;
17186 /* OpenMP 4.0:
17187 # pragma omp target target-clause[optseq] new-line
17188 structured-block */
17190 #define OMP_TARGET_CLAUSE_MASK \
17191 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17201 static bool
17202 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17204 location_t loc = c_parser_peek_token (parser)->location;
17205 c_parser_consume_pragma (parser);
17206 tree *pc = NULL, stmt, block;
17208 if (context != pragma_stmt && context != pragma_compound)
17210 c_parser_error (parser, "expected declaration specifiers");
17211 c_parser_skip_to_pragma_eol (parser);
17212 return false;
17215 if (c_parser_next_token_is (parser, CPP_NAME))
17217 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17218 enum tree_code ccode = ERROR_MARK;
17220 if (strcmp (p, "teams") == 0)
17221 ccode = OMP_TEAMS;
17222 else if (strcmp (p, "parallel") == 0)
17223 ccode = OMP_PARALLEL;
17224 else if (strcmp (p, "simd") == 0)
17225 ccode = OMP_SIMD;
17226 if (ccode != ERROR_MARK)
17228 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17229 char p_name[sizeof ("#pragma omp target teams distribute "
17230 "parallel for simd")];
17232 c_parser_consume_token (parser);
17233 strcpy (p_name, "#pragma omp target");
17234 if (!flag_openmp) /* flag_openmp_simd */
17236 tree stmt;
17237 switch (ccode)
17239 case OMP_TEAMS:
17240 stmt = c_parser_omp_teams (loc, parser, p_name,
17241 OMP_TARGET_CLAUSE_MASK,
17242 cclauses, if_p);
17243 break;
17244 case OMP_PARALLEL:
17245 stmt = c_parser_omp_parallel (loc, parser, p_name,
17246 OMP_TARGET_CLAUSE_MASK,
17247 cclauses, if_p);
17248 break;
17249 case OMP_SIMD:
17250 stmt = c_parser_omp_simd (loc, parser, p_name,
17251 OMP_TARGET_CLAUSE_MASK,
17252 cclauses, if_p);
17253 break;
17254 default:
17255 gcc_unreachable ();
17257 return stmt != NULL_TREE;
17259 keep_next_level ();
17260 tree block = c_begin_compound_stmt (true), ret;
17261 switch (ccode)
17263 case OMP_TEAMS:
17264 ret = c_parser_omp_teams (loc, parser, p_name,
17265 OMP_TARGET_CLAUSE_MASK, cclauses,
17266 if_p);
17267 break;
17268 case OMP_PARALLEL:
17269 ret = c_parser_omp_parallel (loc, parser, p_name,
17270 OMP_TARGET_CLAUSE_MASK, cclauses,
17271 if_p);
17272 break;
17273 case OMP_SIMD:
17274 ret = c_parser_omp_simd (loc, parser, p_name,
17275 OMP_TARGET_CLAUSE_MASK, cclauses,
17276 if_p);
17277 break;
17278 default:
17279 gcc_unreachable ();
17281 block = c_end_compound_stmt (loc, block, true);
17282 if (ret == NULL_TREE)
17283 return false;
17284 if (ccode == OMP_TEAMS)
17286 /* For combined target teams, ensure the num_teams and
17287 thread_limit clause expressions are evaluated on the host,
17288 before entering the target construct. */
17289 tree c;
17290 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17291 c; c = OMP_CLAUSE_CHAIN (c))
17292 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17293 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17294 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17296 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17297 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17298 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17299 expr, NULL_TREE, NULL_TREE);
17300 add_stmt (expr);
17301 OMP_CLAUSE_OPERAND (c, 0) = expr;
17302 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17303 OMP_CLAUSE_FIRSTPRIVATE);
17304 OMP_CLAUSE_DECL (tc) = tmp;
17305 OMP_CLAUSE_CHAIN (tc)
17306 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17307 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17310 tree stmt = make_node (OMP_TARGET);
17311 TREE_TYPE (stmt) = void_type_node;
17312 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17313 OMP_TARGET_BODY (stmt) = block;
17314 OMP_TARGET_COMBINED (stmt) = 1;
17315 add_stmt (stmt);
17316 pc = &OMP_TARGET_CLAUSES (stmt);
17317 goto check_clauses;
17319 else if (!flag_openmp) /* flag_openmp_simd */
17321 c_parser_skip_to_pragma_eol (parser, false);
17322 return false;
17324 else if (strcmp (p, "data") == 0)
17326 c_parser_consume_token (parser);
17327 c_parser_omp_target_data (loc, parser, if_p);
17328 return true;
17330 else if (strcmp (p, "enter") == 0)
17332 c_parser_consume_token (parser);
17333 c_parser_omp_target_enter_data (loc, parser, context);
17334 return false;
17336 else if (strcmp (p, "exit") == 0)
17338 c_parser_consume_token (parser);
17339 c_parser_omp_target_exit_data (loc, parser, context);
17340 return false;
17342 else if (strcmp (p, "update") == 0)
17344 c_parser_consume_token (parser);
17345 return c_parser_omp_target_update (loc, parser, context);
17348 if (!flag_openmp) /* flag_openmp_simd */
17350 c_parser_skip_to_pragma_eol (parser, false);
17351 return false;
17354 stmt = make_node (OMP_TARGET);
17355 TREE_TYPE (stmt) = void_type_node;
17357 OMP_TARGET_CLAUSES (stmt)
17358 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17359 "#pragma omp target");
17360 pc = &OMP_TARGET_CLAUSES (stmt);
17361 keep_next_level ();
17362 block = c_begin_compound_stmt (true);
17363 add_stmt (c_parser_omp_structured_block (parser, if_p));
17364 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17366 SET_EXPR_LOCATION (stmt, loc);
17367 add_stmt (stmt);
17369 check_clauses:
17370 while (*pc)
17372 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17373 switch (OMP_CLAUSE_MAP_KIND (*pc))
17375 case GOMP_MAP_TO:
17376 case GOMP_MAP_ALWAYS_TO:
17377 case GOMP_MAP_FROM:
17378 case GOMP_MAP_ALWAYS_FROM:
17379 case GOMP_MAP_TOFROM:
17380 case GOMP_MAP_ALWAYS_TOFROM:
17381 case GOMP_MAP_ALLOC:
17382 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17383 case GOMP_MAP_ALWAYS_POINTER:
17384 break;
17385 default:
17386 error_at (OMP_CLAUSE_LOCATION (*pc),
17387 "%<#pragma omp target%> with map-type other "
17388 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17389 "on %<map%> clause");
17390 *pc = OMP_CLAUSE_CHAIN (*pc);
17391 continue;
17393 pc = &OMP_CLAUSE_CHAIN (*pc);
17395 return true;
17398 /* OpenMP 4.0:
17399 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17401 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17402 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17409 static void
17410 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17412 auto_vec<c_token> clauses;
17413 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17415 c_token *token = c_parser_peek_token (parser);
17416 if (token->type == CPP_EOF)
17418 c_parser_skip_to_pragma_eol (parser);
17419 return;
17421 clauses.safe_push (*token);
17422 c_parser_consume_token (parser);
17424 clauses.safe_push (*c_parser_peek_token (parser));
17425 c_parser_skip_to_pragma_eol (parser);
17427 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17429 if (c_parser_peek_token (parser)->pragma_kind
17430 != PRAGMA_OMP_DECLARE
17431 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17432 || strcmp (IDENTIFIER_POINTER
17433 (c_parser_peek_2nd_token (parser)->value),
17434 "simd") != 0)
17436 c_parser_error (parser,
17437 "%<#pragma omp declare simd%> must be followed by "
17438 "function declaration or definition or another "
17439 "%<#pragma omp declare simd%>");
17440 return;
17442 c_parser_consume_pragma (parser);
17443 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17445 c_token *token = c_parser_peek_token (parser);
17446 if (token->type == CPP_EOF)
17448 c_parser_skip_to_pragma_eol (parser);
17449 return;
17451 clauses.safe_push (*token);
17452 c_parser_consume_token (parser);
17454 clauses.safe_push (*c_parser_peek_token (parser));
17455 c_parser_skip_to_pragma_eol (parser);
17458 /* Make sure nothing tries to read past the end of the tokens. */
17459 c_token eof_token;
17460 memset (&eof_token, 0, sizeof (eof_token));
17461 eof_token.type = CPP_EOF;
17462 clauses.safe_push (eof_token);
17463 clauses.safe_push (eof_token);
17465 switch (context)
17467 case pragma_external:
17468 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17469 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17471 int ext = disable_extension_diagnostics ();
17473 c_parser_consume_token (parser);
17474 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17475 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17476 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17477 NULL, clauses);
17478 restore_extension_diagnostics (ext);
17480 else
17481 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17482 NULL, clauses);
17483 break;
17484 case pragma_struct:
17485 case pragma_param:
17486 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17487 "function declaration or definition");
17488 break;
17489 case pragma_compound:
17490 case pragma_stmt:
17491 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17492 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17494 int ext = disable_extension_diagnostics ();
17496 c_parser_consume_token (parser);
17497 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17498 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17499 if (c_parser_next_tokens_start_declaration (parser))
17501 c_parser_declaration_or_fndef (parser, true, true, true, true,
17502 true, NULL, clauses);
17503 restore_extension_diagnostics (ext);
17504 break;
17506 restore_extension_diagnostics (ext);
17508 else if (c_parser_next_tokens_start_declaration (parser))
17510 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17511 NULL, clauses);
17512 break;
17514 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17515 "function declaration or definition");
17516 break;
17517 default:
17518 gcc_unreachable ();
17522 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17523 and put that into "omp declare simd" attribute. */
17525 static void
17526 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17527 vec<c_token> clauses)
17529 if (flag_cilkplus
17530 && (clauses.exists ()
17531 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
17532 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
17534 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
17535 "used in the same function marked as a Cilk Plus SIMD-enabled "
17536 "function");
17537 vec_free (parser->cilk_simd_fn_tokens);
17538 return;
17541 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17542 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17543 has already processed the tokens. */
17544 if (clauses.exists () && clauses[0].type == CPP_EOF)
17545 return;
17546 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17548 error ("%<#pragma omp declare simd%> not immediately followed by "
17549 "a function declaration or definition");
17550 clauses[0].type = CPP_EOF;
17551 return;
17553 if (clauses.exists () && clauses[0].type != CPP_NAME)
17555 error_at (DECL_SOURCE_LOCATION (fndecl),
17556 "%<#pragma omp declare simd%> not immediately followed by "
17557 "a single function declaration or definition");
17558 clauses[0].type = CPP_EOF;
17559 return;
17562 if (parms == NULL_TREE)
17563 parms = DECL_ARGUMENTS (fndecl);
17565 unsigned int tokens_avail = parser->tokens_avail;
17566 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17567 bool is_cilkplus_cilk_simd_fn = false;
17569 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
17571 parser->tokens = parser->cilk_simd_fn_tokens->address ();
17572 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
17573 is_cilkplus_cilk_simd_fn = true;
17575 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
17577 error_at (DECL_SOURCE_LOCATION (fndecl),
17578 "%<__simd__%> attribute cannot be used in the same "
17579 "function marked as a Cilk Plus SIMD-enabled function");
17580 vec_free (parser->cilk_simd_fn_tokens);
17581 return;
17585 else
17587 parser->tokens = clauses.address ();
17588 parser->tokens_avail = clauses.length ();
17591 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17592 while (parser->tokens_avail > 3)
17594 c_token *token = c_parser_peek_token (parser);
17595 if (!is_cilkplus_cilk_simd_fn)
17596 gcc_assert (token->type == CPP_NAME
17597 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17598 else
17599 gcc_assert (token->type == CPP_NAME
17600 && is_cilkplus_vector_p (token->value));
17601 c_parser_consume_token (parser);
17602 parser->in_pragma = true;
17604 tree c = NULL_TREE;
17605 if (is_cilkplus_cilk_simd_fn)
17606 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
17607 "SIMD-enabled functions attribute");
17608 else
17609 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17610 "#pragma omp declare simd");
17611 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17612 if (c != NULL_TREE)
17613 c = tree_cons (NULL_TREE, c, NULL_TREE);
17614 if (is_cilkplus_cilk_simd_fn)
17616 tree k = build_tree_list (get_identifier ("cilk simd function"),
17617 NULL_TREE);
17618 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
17619 DECL_ATTRIBUTES (fndecl) = k;
17621 c = build_tree_list (get_identifier ("omp declare simd"), c);
17622 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17623 DECL_ATTRIBUTES (fndecl) = c;
17626 parser->tokens = &parser->tokens_buf[0];
17627 parser->tokens_avail = tokens_avail;
17628 if (clauses.exists ())
17629 clauses[0].type = CPP_PRAGMA;
17631 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
17632 vec_free (parser->cilk_simd_fn_tokens);
17636 /* OpenMP 4.0:
17637 # pragma omp declare target new-line
17638 declarations and definitions
17639 # pragma omp end declare target new-line
17641 OpenMP 4.5:
17642 # pragma omp declare target ( extended-list ) new-line
17644 # pragma omp declare target declare-target-clauses[seq] new-line */
17646 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17647 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17650 static void
17651 c_parser_omp_declare_target (c_parser *parser)
17653 location_t loc = c_parser_peek_token (parser)->location;
17654 tree clauses = NULL_TREE;
17655 if (c_parser_next_token_is (parser, CPP_NAME))
17656 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17657 "#pragma omp declare target");
17658 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17660 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17661 clauses);
17662 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17663 c_parser_skip_to_pragma_eol (parser);
17665 else
17667 c_parser_skip_to_pragma_eol (parser);
17668 current_omp_declare_target_attribute++;
17669 return;
17671 if (current_omp_declare_target_attribute)
17672 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17673 "%<#pragma omp declare target%> without clauses and "
17674 "%<#pragma omp end declare target%>");
17675 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17677 tree t = OMP_CLAUSE_DECL (c), id;
17678 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17679 tree at2 = lookup_attribute ("omp declare target link",
17680 DECL_ATTRIBUTES (t));
17681 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17683 id = get_identifier ("omp declare target link");
17684 std::swap (at1, at2);
17686 else
17687 id = get_identifier ("omp declare target");
17688 if (at2)
17690 error_at (OMP_CLAUSE_LOCATION (c),
17691 "%qD specified both in declare target %<link%> and %<to%>"
17692 " clauses", t);
17693 continue;
17695 if (!at1)
17697 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17698 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17699 continue;
17701 symtab_node *node = symtab_node::get (t);
17702 if (node != NULL)
17704 node->offloadable = 1;
17705 if (ENABLE_OFFLOADING)
17707 g->have_offload = true;
17708 if (is_a <varpool_node *> (node))
17709 vec_safe_push (offload_vars, t);
17716 static void
17717 c_parser_omp_end_declare_target (c_parser *parser)
17719 location_t loc = c_parser_peek_token (parser)->location;
17720 c_parser_consume_pragma (parser);
17721 if (c_parser_next_token_is (parser, CPP_NAME)
17722 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17723 "declare") == 0)
17725 c_parser_consume_token (parser);
17726 if (c_parser_next_token_is (parser, CPP_NAME)
17727 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17728 "target") == 0)
17729 c_parser_consume_token (parser);
17730 else
17732 c_parser_error (parser, "expected %<target%>");
17733 c_parser_skip_to_pragma_eol (parser);
17734 return;
17737 else
17739 c_parser_error (parser, "expected %<declare%>");
17740 c_parser_skip_to_pragma_eol (parser);
17741 return;
17743 c_parser_skip_to_pragma_eol (parser);
17744 if (!current_omp_declare_target_attribute)
17745 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17746 "%<#pragma omp declare target%>");
17747 else
17748 current_omp_declare_target_attribute--;
17752 /* OpenMP 4.0
17753 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17754 initializer-clause[opt] new-line
17756 initializer-clause:
17757 initializer (omp_priv = initializer)
17758 initializer (function-name (argument-list)) */
17760 static void
17761 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17763 unsigned int tokens_avail = 0, i;
17764 vec<tree> types = vNULL;
17765 vec<c_token> clauses = vNULL;
17766 enum tree_code reduc_code = ERROR_MARK;
17767 tree reduc_id = NULL_TREE;
17768 tree type;
17769 location_t rloc = c_parser_peek_token (parser)->location;
17771 if (context == pragma_struct || context == pragma_param)
17773 error ("%<#pragma omp declare reduction%> not at file or block scope");
17774 goto fail;
17777 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17778 goto fail;
17780 switch (c_parser_peek_token (parser)->type)
17782 case CPP_PLUS:
17783 reduc_code = PLUS_EXPR;
17784 break;
17785 case CPP_MULT:
17786 reduc_code = MULT_EXPR;
17787 break;
17788 case CPP_MINUS:
17789 reduc_code = MINUS_EXPR;
17790 break;
17791 case CPP_AND:
17792 reduc_code = BIT_AND_EXPR;
17793 break;
17794 case CPP_XOR:
17795 reduc_code = BIT_XOR_EXPR;
17796 break;
17797 case CPP_OR:
17798 reduc_code = BIT_IOR_EXPR;
17799 break;
17800 case CPP_AND_AND:
17801 reduc_code = TRUTH_ANDIF_EXPR;
17802 break;
17803 case CPP_OR_OR:
17804 reduc_code = TRUTH_ORIF_EXPR;
17805 break;
17806 case CPP_NAME:
17807 const char *p;
17808 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17809 if (strcmp (p, "min") == 0)
17811 reduc_code = MIN_EXPR;
17812 break;
17814 if (strcmp (p, "max") == 0)
17816 reduc_code = MAX_EXPR;
17817 break;
17819 reduc_id = c_parser_peek_token (parser)->value;
17820 break;
17821 default:
17822 c_parser_error (parser,
17823 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17824 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17825 goto fail;
17828 tree orig_reduc_id, reduc_decl;
17829 orig_reduc_id = reduc_id;
17830 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17831 reduc_decl = c_omp_reduction_decl (reduc_id);
17832 c_parser_consume_token (parser);
17834 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17835 goto fail;
17837 while (true)
17839 location_t loc = c_parser_peek_token (parser)->location;
17840 struct c_type_name *ctype = c_parser_type_name (parser);
17841 if (ctype != NULL)
17843 type = groktypename (ctype, NULL, NULL);
17844 if (type == error_mark_node)
17846 else if ((INTEGRAL_TYPE_P (type)
17847 || TREE_CODE (type) == REAL_TYPE
17848 || TREE_CODE (type) == COMPLEX_TYPE)
17849 && orig_reduc_id == NULL_TREE)
17850 error_at (loc, "predeclared arithmetic type in "
17851 "%<#pragma omp declare reduction%>");
17852 else if (TREE_CODE (type) == FUNCTION_TYPE
17853 || TREE_CODE (type) == ARRAY_TYPE)
17854 error_at (loc, "function or array type in "
17855 "%<#pragma omp declare reduction%>");
17856 else if (TYPE_ATOMIC (type))
17857 error_at (loc, "%<_Atomic%> qualified type in "
17858 "%<#pragma omp declare reduction%>");
17859 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17860 error_at (loc, "const, volatile or restrict qualified type in "
17861 "%<#pragma omp declare reduction%>");
17862 else
17864 tree t;
17865 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17866 if (comptypes (TREE_PURPOSE (t), type))
17868 error_at (loc, "redeclaration of %qs "
17869 "%<#pragma omp declare reduction%> for "
17870 "type %qT",
17871 IDENTIFIER_POINTER (reduc_id)
17872 + sizeof ("omp declare reduction ") - 1,
17873 type);
17874 location_t ploc
17875 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17876 0));
17877 error_at (ploc, "previous %<#pragma omp declare "
17878 "reduction%>");
17879 break;
17881 if (t == NULL_TREE)
17882 types.safe_push (type);
17884 if (c_parser_next_token_is (parser, CPP_COMMA))
17885 c_parser_consume_token (parser);
17886 else
17887 break;
17889 else
17890 break;
17893 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17894 || types.is_empty ())
17896 fail:
17897 clauses.release ();
17898 types.release ();
17899 while (true)
17901 c_token *token = c_parser_peek_token (parser);
17902 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17903 break;
17904 c_parser_consume_token (parser);
17906 c_parser_skip_to_pragma_eol (parser);
17907 return;
17910 if (types.length () > 1)
17912 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17914 c_token *token = c_parser_peek_token (parser);
17915 if (token->type == CPP_EOF)
17916 goto fail;
17917 clauses.safe_push (*token);
17918 c_parser_consume_token (parser);
17920 clauses.safe_push (*c_parser_peek_token (parser));
17921 c_parser_skip_to_pragma_eol (parser);
17923 /* Make sure nothing tries to read past the end of the tokens. */
17924 c_token eof_token;
17925 memset (&eof_token, 0, sizeof (eof_token));
17926 eof_token.type = CPP_EOF;
17927 clauses.safe_push (eof_token);
17928 clauses.safe_push (eof_token);
17931 int errs = errorcount;
17932 FOR_EACH_VEC_ELT (types, i, type)
17934 tokens_avail = parser->tokens_avail;
17935 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17936 if (!clauses.is_empty ())
17938 parser->tokens = clauses.address ();
17939 parser->tokens_avail = clauses.length ();
17940 parser->in_pragma = true;
17943 bool nested = current_function_decl != NULL_TREE;
17944 if (nested)
17945 c_push_function_context ();
17946 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17947 reduc_id, default_function_type);
17948 current_function_decl = fndecl;
17949 allocate_struct_function (fndecl, true);
17950 push_scope ();
17951 tree stmt = push_stmt_list ();
17952 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17953 warn about these. */
17954 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17955 get_identifier ("omp_out"), type);
17956 DECL_ARTIFICIAL (omp_out) = 1;
17957 DECL_CONTEXT (omp_out) = fndecl;
17958 pushdecl (omp_out);
17959 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17960 get_identifier ("omp_in"), type);
17961 DECL_ARTIFICIAL (omp_in) = 1;
17962 DECL_CONTEXT (omp_in) = fndecl;
17963 pushdecl (omp_in);
17964 struct c_expr combiner = c_parser_expression (parser);
17965 struct c_expr initializer;
17966 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17967 bool bad = false;
17968 initializer.value = error_mark_node;
17969 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17970 bad = true;
17971 else if (c_parser_next_token_is (parser, CPP_NAME)
17972 && strcmp (IDENTIFIER_POINTER
17973 (c_parser_peek_token (parser)->value),
17974 "initializer") == 0)
17976 c_parser_consume_token (parser);
17977 pop_scope ();
17978 push_scope ();
17979 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17980 get_identifier ("omp_priv"), type);
17981 DECL_ARTIFICIAL (omp_priv) = 1;
17982 DECL_INITIAL (omp_priv) = error_mark_node;
17983 DECL_CONTEXT (omp_priv) = fndecl;
17984 pushdecl (omp_priv);
17985 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17986 get_identifier ("omp_orig"), type);
17987 DECL_ARTIFICIAL (omp_orig) = 1;
17988 DECL_CONTEXT (omp_orig) = fndecl;
17989 pushdecl (omp_orig);
17990 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17991 bad = true;
17992 else if (!c_parser_next_token_is (parser, CPP_NAME))
17994 c_parser_error (parser, "expected %<omp_priv%> or "
17995 "function-name");
17996 bad = true;
17998 else if (strcmp (IDENTIFIER_POINTER
17999 (c_parser_peek_token (parser)->value),
18000 "omp_priv") != 0)
18002 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
18003 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
18005 c_parser_error (parser, "expected function-name %<(%>");
18006 bad = true;
18008 else
18009 initializer = c_parser_postfix_expression (parser);
18010 if (initializer.value
18011 && TREE_CODE (initializer.value) == CALL_EXPR)
18013 int j;
18014 tree c = initializer.value;
18015 for (j = 0; j < call_expr_nargs (c); j++)
18017 tree a = CALL_EXPR_ARG (c, j);
18018 STRIP_NOPS (a);
18019 if (TREE_CODE (a) == ADDR_EXPR
18020 && TREE_OPERAND (a, 0) == omp_priv)
18021 break;
18023 if (j == call_expr_nargs (c))
18024 error ("one of the initializer call arguments should be "
18025 "%<&omp_priv%>");
18028 else
18030 c_parser_consume_token (parser);
18031 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
18032 bad = true;
18033 else
18035 tree st = push_stmt_list ();
18036 location_t loc = c_parser_peek_token (parser)->location;
18037 rich_location richloc (line_table, loc);
18038 start_init (omp_priv, NULL_TREE, 0, &richloc);
18039 struct c_expr init = c_parser_initializer (parser);
18040 finish_init ();
18041 finish_decl (omp_priv, loc, init.value,
18042 init.original_type, NULL_TREE);
18043 pop_stmt_list (st);
18046 if (!bad
18047 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
18048 bad = true;
18051 if (!bad)
18053 c_parser_skip_to_pragma_eol (parser);
18055 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
18056 DECL_INITIAL (reduc_decl));
18057 DECL_INITIAL (reduc_decl) = t;
18058 DECL_SOURCE_LOCATION (omp_out) = rloc;
18059 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
18060 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
18061 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
18062 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
18063 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
18064 if (omp_priv)
18066 DECL_SOURCE_LOCATION (omp_priv) = rloc;
18067 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
18068 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
18069 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
18070 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
18071 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
18072 walk_tree (&DECL_INITIAL (omp_priv),
18073 c_check_omp_declare_reduction_r,
18074 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
18078 pop_stmt_list (stmt);
18079 pop_scope ();
18080 if (cfun->language != NULL)
18082 ggc_free (cfun->language);
18083 cfun->language = NULL;
18085 set_cfun (NULL);
18086 current_function_decl = NULL_TREE;
18087 if (nested)
18088 c_pop_function_context ();
18090 if (!clauses.is_empty ())
18092 parser->tokens = &parser->tokens_buf[0];
18093 parser->tokens_avail = tokens_avail;
18095 if (bad)
18096 goto fail;
18097 if (errs != errorcount)
18098 break;
18101 clauses.release ();
18102 types.release ();
18106 /* OpenMP 4.0
18107 #pragma omp declare simd declare-simd-clauses[optseq] new-line
18108 #pragma omp declare reduction (reduction-id : typename-list : expression) \
18109 initializer-clause[opt] new-line
18110 #pragma omp declare target new-line */
18112 static void
18113 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
18115 c_parser_consume_pragma (parser);
18116 if (c_parser_next_token_is (parser, CPP_NAME))
18118 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18119 if (strcmp (p, "simd") == 0)
18121 /* c_parser_consume_token (parser); done in
18122 c_parser_omp_declare_simd. */
18123 c_parser_omp_declare_simd (parser, context);
18124 return;
18126 if (strcmp (p, "reduction") == 0)
18128 c_parser_consume_token (parser);
18129 c_parser_omp_declare_reduction (parser, context);
18130 return;
18132 if (!flag_openmp) /* flag_openmp_simd */
18134 c_parser_skip_to_pragma_eol (parser, false);
18135 return;
18137 if (strcmp (p, "target") == 0)
18139 c_parser_consume_token (parser);
18140 c_parser_omp_declare_target (parser);
18141 return;
18145 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
18146 "or %<target%>");
18147 c_parser_skip_to_pragma_eol (parser);
18150 /* OpenMP 4.5:
18151 #pragma omp taskloop taskloop-clause[optseq] new-line
18152 for-loop
18154 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
18155 for-loop */
18157 #define OMP_TASKLOOP_CLAUSE_MASK \
18158 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
18159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
18163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
18164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
18165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
18167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
18169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
18170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
18171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
18173 static tree
18174 c_parser_omp_taskloop (location_t loc, c_parser *parser,
18175 char *p_name, omp_clause_mask mask, tree *cclauses,
18176 bool *if_p)
18178 tree clauses, block, ret;
18180 strcat (p_name, " taskloop");
18181 mask |= OMP_TASKLOOP_CLAUSE_MASK;
18183 if (c_parser_next_token_is (parser, CPP_NAME))
18185 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18187 if (strcmp (p, "simd") == 0)
18189 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18190 if (cclauses == NULL)
18191 cclauses = cclauses_buf;
18192 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
18193 c_parser_consume_token (parser);
18194 if (!flag_openmp) /* flag_openmp_simd */
18195 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
18196 if_p);
18197 block = c_begin_compound_stmt (true);
18198 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
18199 block = c_end_compound_stmt (loc, block, true);
18200 if (ret == NULL)
18201 return ret;
18202 ret = make_node (OMP_TASKLOOP);
18203 TREE_TYPE (ret) = void_type_node;
18204 OMP_FOR_BODY (ret) = block;
18205 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18206 SET_EXPR_LOCATION (ret, loc);
18207 add_stmt (ret);
18208 return ret;
18211 if (!flag_openmp) /* flag_openmp_simd */
18213 c_parser_skip_to_pragma_eol (parser, false);
18214 return NULL_TREE;
18217 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18218 if (cclauses)
18220 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
18221 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18224 block = c_begin_compound_stmt (true);
18225 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
18226 block = c_end_compound_stmt (loc, block, true);
18227 add_stmt (block);
18229 return ret;
18232 /* Main entry point to parsing most OpenMP pragmas. */
18234 static void
18235 c_parser_omp_construct (c_parser *parser, bool *if_p)
18237 enum pragma_kind p_kind;
18238 location_t loc;
18239 tree stmt;
18240 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18241 omp_clause_mask mask (0);
18243 loc = c_parser_peek_token (parser)->location;
18244 p_kind = c_parser_peek_token (parser)->pragma_kind;
18245 c_parser_consume_pragma (parser);
18247 switch (p_kind)
18249 case PRAGMA_OACC_ATOMIC:
18250 c_parser_omp_atomic (loc, parser);
18251 return;
18252 case PRAGMA_OACC_CACHE:
18253 strcpy (p_name, "#pragma acc");
18254 stmt = c_parser_oacc_cache (loc, parser);
18255 break;
18256 case PRAGMA_OACC_DATA:
18257 stmt = c_parser_oacc_data (loc, parser, if_p);
18258 break;
18259 case PRAGMA_OACC_HOST_DATA:
18260 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18261 break;
18262 case PRAGMA_OACC_KERNELS:
18263 case PRAGMA_OACC_PARALLEL:
18264 strcpy (p_name, "#pragma acc");
18265 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18266 if_p);
18267 break;
18268 case PRAGMA_OACC_LOOP:
18269 strcpy (p_name, "#pragma acc");
18270 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18271 break;
18272 case PRAGMA_OACC_WAIT:
18273 strcpy (p_name, "#pragma wait");
18274 stmt = c_parser_oacc_wait (loc, parser, p_name);
18275 break;
18276 case PRAGMA_OMP_ATOMIC:
18277 c_parser_omp_atomic (loc, parser);
18278 return;
18279 case PRAGMA_OMP_CRITICAL:
18280 stmt = c_parser_omp_critical (loc, parser, if_p);
18281 break;
18282 case PRAGMA_OMP_DISTRIBUTE:
18283 strcpy (p_name, "#pragma omp");
18284 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18285 break;
18286 case PRAGMA_OMP_FOR:
18287 strcpy (p_name, "#pragma omp");
18288 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18289 break;
18290 case PRAGMA_OMP_MASTER:
18291 stmt = c_parser_omp_master (loc, parser, if_p);
18292 break;
18293 case PRAGMA_OMP_PARALLEL:
18294 strcpy (p_name, "#pragma omp");
18295 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18296 break;
18297 case PRAGMA_OMP_SECTIONS:
18298 strcpy (p_name, "#pragma omp");
18299 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18300 break;
18301 case PRAGMA_OMP_SIMD:
18302 strcpy (p_name, "#pragma omp");
18303 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18304 break;
18305 case PRAGMA_OMP_SINGLE:
18306 stmt = c_parser_omp_single (loc, parser, if_p);
18307 break;
18308 case PRAGMA_OMP_TASK:
18309 stmt = c_parser_omp_task (loc, parser, if_p);
18310 break;
18311 case PRAGMA_OMP_TASKGROUP:
18312 stmt = c_parser_omp_taskgroup (parser, if_p);
18313 break;
18314 case PRAGMA_OMP_TASKLOOP:
18315 strcpy (p_name, "#pragma omp");
18316 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18317 break;
18318 case PRAGMA_OMP_TEAMS:
18319 strcpy (p_name, "#pragma omp");
18320 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18321 break;
18322 default:
18323 gcc_unreachable ();
18326 if (stmt)
18327 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18331 /* OpenMP 2.5:
18332 # pragma omp threadprivate (variable-list) */
18334 static void
18335 c_parser_omp_threadprivate (c_parser *parser)
18337 tree vars, t;
18338 location_t loc;
18340 c_parser_consume_pragma (parser);
18341 loc = c_parser_peek_token (parser)->location;
18342 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18344 /* Mark every variable in VARS to be assigned thread local storage. */
18345 for (t = vars; t; t = TREE_CHAIN (t))
18347 tree v = TREE_PURPOSE (t);
18349 /* FIXME diagnostics: Ideally we should keep individual
18350 locations for all the variables in the var list to make the
18351 following errors more precise. Perhaps
18352 c_parser_omp_var_list_parens() should construct a list of
18353 locations to go along with the var list. */
18355 /* If V had already been marked threadprivate, it doesn't matter
18356 whether it had been used prior to this point. */
18357 if (!VAR_P (v))
18358 error_at (loc, "%qD is not a variable", v);
18359 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18360 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18361 else if (! is_global_var (v))
18362 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18363 else if (TREE_TYPE (v) == error_mark_node)
18365 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18366 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18367 else
18369 if (! DECL_THREAD_LOCAL_P (v))
18371 set_decl_tls_model (v, decl_default_tls_model (v));
18372 /* If rtl has been already set for this var, call
18373 make_decl_rtl once again, so that encode_section_info
18374 has a chance to look at the new decl flags. */
18375 if (DECL_RTL_SET_P (v))
18376 make_decl_rtl (v);
18378 C_DECL_THREADPRIVATE_P (v) = 1;
18382 c_parser_skip_to_pragma_eol (parser);
18385 /* Cilk Plus <#pragma simd> parsing routines. */
18387 /* Helper function for c_parser_pragma. Perform some sanity checking
18388 for <#pragma simd> constructs. Returns FALSE if there was a
18389 problem. */
18391 static bool
18392 c_parser_cilk_verify_simd (c_parser *parser,
18393 enum pragma_context context)
18395 if (!flag_cilkplus)
18397 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
18398 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
18399 return false;
18401 if (context == pragma_external)
18403 c_parser_error (parser,"pragma simd must be inside a function");
18404 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
18405 return false;
18407 return true;
18410 /* Cilk Plus:
18411 This function is shared by SIMD-enabled functions and #pragma simd.
18412 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
18413 CLAUSES is unused. The main purpose of this function is to parse a
18414 vectorlength attribute or clause and check for parse errors.
18415 When IS_SIMD_FN is true then the function is merely caching the tokens
18416 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
18417 cache is cleared since there is no reason to continue.
18418 Syntax:
18419 vectorlength ( constant-expression ) */
18421 static tree
18422 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
18423 bool is_simd_fn)
18425 if (is_simd_fn)
18426 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
18427 else
18428 /* The vectorlength clause behaves exactly like OpenMP's safelen
18429 clause. Represent it in OpenMP terms. */
18430 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
18432 matching_parens parens;
18433 if (!parens.require_open (parser))
18434 return clauses;
18436 location_t loc = c_parser_peek_token (parser)->location;
18437 tree expr = c_parser_expr_no_commas (parser, NULL).value;
18438 expr = c_fully_fold (expr, false, NULL);
18440 /* If expr is an error_mark_node then the above function would have
18441 emitted an error. No reason to do it twice. */
18442 if (expr == error_mark_node)
18444 else if (!TREE_TYPE (expr)
18445 || !TREE_CONSTANT (expr)
18446 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
18448 error_at (loc, "vectorlength must be an integer constant");
18449 else if (wi::exact_log2 (wi::to_wide (expr)) == -1)
18450 error_at (loc, "vectorlength must be a power of 2");
18451 else
18453 if (is_simd_fn)
18455 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
18456 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
18457 OMP_CLAUSE_CHAIN (u) = clauses;
18458 clauses = u;
18460 else
18462 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
18463 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
18464 OMP_CLAUSE_CHAIN (u) = clauses;
18465 clauses = u;
18469 parens.require_close (parser);
18471 return clauses;
18474 /* Cilk Plus:
18475 linear ( simd-linear-variable-list )
18477 simd-linear-variable-list:
18478 simd-linear-variable
18479 simd-linear-variable-list , simd-linear-variable
18481 simd-linear-variable:
18482 id-expression
18483 id-expression : simd-linear-step
18485 simd-linear-step:
18486 conditional-expression */
18488 static tree
18489 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
18491 matching_parens parens;
18492 if (!parens.require_open (parser))
18493 return clauses;
18495 location_t loc = c_parser_peek_token (parser)->location;
18497 if (c_parser_next_token_is_not (parser, CPP_NAME)
18498 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
18499 c_parser_error (parser, "expected identifier");
18501 while (c_parser_next_token_is (parser, CPP_NAME)
18502 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
18504 tree var = lookup_name (c_parser_peek_token (parser)->value);
18506 if (var == NULL)
18508 undeclared_variable (c_parser_peek_token (parser)->location,
18509 c_parser_peek_token (parser)->value);
18510 c_parser_consume_token (parser);
18512 else if (var == error_mark_node)
18513 c_parser_consume_token (parser);
18514 else
18516 tree step = integer_one_node;
18518 /* Parse the linear step if present. */
18519 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
18521 c_parser_consume_token (parser);
18522 c_parser_consume_token (parser);
18524 tree expr = c_parser_expr_no_commas (parser, NULL).value;
18525 expr = c_fully_fold (expr, false, NULL);
18527 if (TREE_TYPE (expr)
18528 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
18529 && (TREE_CONSTANT (expr)
18530 || DECL_P (expr)))
18531 step = expr;
18532 else
18533 c_parser_error (parser,
18534 "step size must be an integer constant "
18535 "expression or an integer variable");
18537 else
18538 c_parser_consume_token (parser);
18540 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
18541 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
18542 OMP_CLAUSE_DECL (u) = var;
18543 OMP_CLAUSE_LINEAR_STEP (u) = step;
18544 OMP_CLAUSE_CHAIN (u) = clauses;
18545 clauses = u;
18548 if (c_parser_next_token_is_not (parser, CPP_COMMA))
18549 break;
18551 c_parser_consume_token (parser);
18554 parens.skip_until_found_close (parser);
18556 return clauses;
18559 /* Returns the name of the next clause. If the clause is not
18560 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
18561 not consumed. Otherwise, the appropriate pragma_simd_clause is
18562 returned and the token is consumed. */
18564 static pragma_omp_clause
18565 c_parser_cilk_clause_name (c_parser *parser)
18567 pragma_omp_clause result;
18568 c_token *token = c_parser_peek_token (parser);
18570 if (!token->value || token->type != CPP_NAME)
18571 return PRAGMA_CILK_CLAUSE_NONE;
18573 const char *p = IDENTIFIER_POINTER (token->value);
18575 if (!strcmp (p, "vectorlength"))
18576 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
18577 else if (!strcmp (p, "linear"))
18578 result = PRAGMA_CILK_CLAUSE_LINEAR;
18579 else if (!strcmp (p, "private"))
18580 result = PRAGMA_CILK_CLAUSE_PRIVATE;
18581 else if (!strcmp (p, "firstprivate"))
18582 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
18583 else if (!strcmp (p, "lastprivate"))
18584 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
18585 else if (!strcmp (p, "reduction"))
18586 result = PRAGMA_CILK_CLAUSE_REDUCTION;
18587 else
18588 return PRAGMA_CILK_CLAUSE_NONE;
18590 c_parser_consume_token (parser);
18591 return result;
18594 /* Parse all #<pragma simd> clauses. Return the list of clauses
18595 found. */
18597 static tree
18598 c_parser_cilk_all_clauses (c_parser *parser)
18600 tree clauses = NULL;
18602 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18604 pragma_omp_clause c_kind;
18606 c_kind = c_parser_cilk_clause_name (parser);
18608 switch (c_kind)
18610 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
18611 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
18612 break;
18613 case PRAGMA_CILK_CLAUSE_LINEAR:
18614 clauses = c_parser_cilk_clause_linear (parser, clauses);
18615 break;
18616 case PRAGMA_CILK_CLAUSE_PRIVATE:
18617 /* Use the OpenMP counterpart. */
18618 clauses = c_parser_omp_clause_private (parser, clauses);
18619 break;
18620 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
18621 /* Use the OpenMP counterpart. */
18622 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
18623 break;
18624 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
18625 /* Use the OpenMP counterpart. */
18626 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
18627 break;
18628 case PRAGMA_CILK_CLAUSE_REDUCTION:
18629 /* Use the OpenMP counterpart. */
18630 clauses = c_parser_omp_clause_reduction (parser, clauses);
18631 break;
18632 default:
18633 c_parser_error (parser, "expected %<#pragma simd%> clause");
18634 goto saw_error;
18638 saw_error:
18639 c_parser_skip_to_pragma_eol (parser);
18640 return c_finish_omp_clauses (clauses, C_ORT_CILK);
18643 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
18644 Here is the correct syntax of this pragma:
18645 #pragma cilk grainsize = <EXP>
18648 static void
18649 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
18651 extern tree convert_to_integer (tree, tree);
18653 /* consume the 'grainsize' keyword. */
18654 c_parser_consume_pragma (parser);
18656 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
18658 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
18659 if (g_expr.value == error_mark_node)
18661 c_parser_skip_to_pragma_eol (parser);
18662 return;
18664 tree grain = convert_to_integer (long_integer_type_node,
18665 c_fully_fold (g_expr.value, false,
18666 NULL));
18667 c_parser_skip_to_pragma_eol (parser);
18668 c_token *token = c_parser_peek_token (parser);
18669 if (token && token->type == CPP_KEYWORD
18670 && token->keyword == RID_CILK_FOR)
18672 if (grain == NULL_TREE || grain == error_mark_node)
18673 grain = integer_zero_node;
18674 c_parser_cilk_for (parser, grain, if_p);
18676 else
18677 warning (0, "%<#pragma cilk grainsize%> is not followed by "
18678 "%<_Cilk_for%>");
18680 else
18681 c_parser_skip_to_pragma_eol (parser);
18684 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
18686 static void
18687 c_parser_cilk_simd (c_parser *parser, bool *if_p)
18689 tree clauses = c_parser_cilk_all_clauses (parser);
18690 tree block = c_begin_compound_stmt (true);
18691 location_t loc = c_parser_peek_token (parser)->location;
18692 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
18693 block = c_end_compound_stmt (loc, block, true);
18694 add_stmt (block);
18697 /* Create an artificial decl with TYPE and emit initialization of it with
18698 INIT. */
18700 static tree
18701 c_get_temp_regvar (tree type, tree init)
18703 location_t loc = EXPR_LOCATION (init);
18704 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
18705 DECL_ARTIFICIAL (decl) = 1;
18706 DECL_IGNORED_P (decl) = 1;
18707 pushdecl (decl);
18708 tree t = build2 (INIT_EXPR, type, decl, init);
18709 add_stmt (t);
18710 return decl;
18713 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
18714 GRAIN is the grain value passed in through pragma or 0. */
18716 static void
18717 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
18719 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
18720 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
18721 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
18722 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
18724 tree block = c_begin_compound_stmt (true);
18725 tree sb = push_stmt_list ();
18726 location_t loc = c_parser_peek_token (parser)->location;
18727 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
18728 if_p);
18729 sb = pop_stmt_list (sb);
18731 if (omp_for)
18733 tree omp_par = make_node (OMP_PARALLEL);
18734 TREE_TYPE (omp_par) = void_type_node;
18735 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
18736 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
18737 TREE_SIDE_EFFECTS (bind) = 1;
18738 BIND_EXPR_BODY (bind) = sb;
18739 OMP_PARALLEL_BODY (omp_par) = bind;
18740 if (OMP_FOR_PRE_BODY (omp_for))
18742 add_stmt (OMP_FOR_PRE_BODY (omp_for));
18743 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
18745 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
18746 tree decl = TREE_OPERAND (init, 0);
18747 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
18748 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
18749 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
18750 if (TREE_CODE (t) != INTEGER_CST)
18752 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
18753 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18754 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
18755 OMP_CLAUSE_CHAIN (c) = clauses;
18756 clauses = c;
18758 if (TREE_CODE (incr) == MODIFY_EXPR)
18760 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
18761 if (TREE_CODE (t) != INTEGER_CST)
18763 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
18764 = c_get_temp_regvar (TREE_TYPE (t), t);
18765 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18766 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
18767 OMP_CLAUSE_CHAIN (c) = clauses;
18768 clauses = c;
18771 t = TREE_OPERAND (init, 1);
18772 if (TREE_CODE (t) != INTEGER_CST)
18774 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
18775 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18776 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
18777 OMP_CLAUSE_CHAIN (c) = clauses;
18778 clauses = c;
18780 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
18781 OMP_CLAUSE_DECL (c) = decl;
18782 OMP_CLAUSE_CHAIN (c) = clauses;
18783 clauses = c;
18784 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
18785 OMP_CLAUSE_OPERAND (c, 0)
18786 = cilk_for_number_of_iterations (omp_for);
18787 OMP_CLAUSE_CHAIN (c) = clauses;
18788 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
18789 add_stmt (omp_par);
18792 block = c_end_compound_stmt (loc, block, true);
18793 add_stmt (block);
18797 /* Parse a transaction attribute (GCC Extension).
18799 transaction-attribute:
18800 attributes
18801 [ [ any-word ] ]
18803 The transactional memory language description is written for C++,
18804 and uses the C++0x attribute syntax. For compatibility, allow the
18805 bracket style for transactions in C as well. */
18807 static tree
18808 c_parser_transaction_attributes (c_parser *parser)
18810 tree attr_name, attr = NULL;
18812 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18813 return c_parser_attributes (parser);
18815 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18816 return NULL_TREE;
18817 c_parser_consume_token (parser);
18818 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18819 goto error1;
18821 attr_name = c_parser_attribute_any_word (parser);
18822 if (attr_name)
18824 c_parser_consume_token (parser);
18825 attr = build_tree_list (attr_name, NULL_TREE);
18827 else
18828 c_parser_error (parser, "expected identifier");
18830 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18831 error1:
18832 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18833 return attr;
18836 /* Parse a __transaction_atomic or __transaction_relaxed statement
18837 (GCC Extension).
18839 transaction-statement:
18840 __transaction_atomic transaction-attribute[opt] compound-statement
18841 __transaction_relaxed compound-statement
18843 Note that the only valid attribute is: "outer".
18846 static tree
18847 c_parser_transaction (c_parser *parser, enum rid keyword)
18849 unsigned int old_in = parser->in_transaction;
18850 unsigned int this_in = 1, new_in;
18851 location_t loc = c_parser_peek_token (parser)->location;
18852 tree stmt, attrs;
18854 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18855 || keyword == RID_TRANSACTION_RELAXED)
18856 && c_parser_next_token_is_keyword (parser, keyword));
18857 c_parser_consume_token (parser);
18859 if (keyword == RID_TRANSACTION_RELAXED)
18860 this_in |= TM_STMT_ATTR_RELAXED;
18861 else
18863 attrs = c_parser_transaction_attributes (parser);
18864 if (attrs)
18865 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18868 /* Keep track if we're in the lexical scope of an outer transaction. */
18869 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18871 parser->in_transaction = new_in;
18872 stmt = c_parser_compound_statement (parser);
18873 parser->in_transaction = old_in;
18875 if (flag_tm)
18876 stmt = c_finish_transaction (loc, stmt, this_in);
18877 else
18878 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18879 "%<__transaction_atomic%> without transactional memory support enabled"
18880 : "%<__transaction_relaxed %> "
18881 "without transactional memory support enabled"));
18883 return stmt;
18886 /* Parse a __transaction_atomic or __transaction_relaxed expression
18887 (GCC Extension).
18889 transaction-expression:
18890 __transaction_atomic ( expression )
18891 __transaction_relaxed ( expression )
18894 static struct c_expr
18895 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18897 struct c_expr ret;
18898 unsigned int old_in = parser->in_transaction;
18899 unsigned int this_in = 1;
18900 location_t loc = c_parser_peek_token (parser)->location;
18901 tree attrs;
18903 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18904 || keyword == RID_TRANSACTION_RELAXED)
18905 && c_parser_next_token_is_keyword (parser, keyword));
18906 c_parser_consume_token (parser);
18908 if (keyword == RID_TRANSACTION_RELAXED)
18909 this_in |= TM_STMT_ATTR_RELAXED;
18910 else
18912 attrs = c_parser_transaction_attributes (parser);
18913 if (attrs)
18914 this_in |= parse_tm_stmt_attr (attrs, 0);
18917 parser->in_transaction = this_in;
18918 matching_parens parens;
18919 if (parens.require_open (parser))
18921 tree expr = c_parser_expression (parser).value;
18922 ret.original_type = TREE_TYPE (expr);
18923 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18924 if (this_in & TM_STMT_ATTR_RELAXED)
18925 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18926 SET_EXPR_LOCATION (ret.value, loc);
18927 ret.original_code = TRANSACTION_EXPR;
18928 if (!parens.require_close (parser))
18930 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18931 goto error;
18934 else
18936 error:
18937 ret.value = error_mark_node;
18938 ret.original_code = ERROR_MARK;
18939 ret.original_type = NULL;
18941 parser->in_transaction = old_in;
18943 if (!flag_tm)
18944 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18945 "%<__transaction_atomic%> without transactional memory support enabled"
18946 : "%<__transaction_relaxed %> "
18947 "without transactional memory support enabled"));
18949 set_c_expr_source_range (&ret, loc, loc);
18951 return ret;
18954 /* Parse a __transaction_cancel statement (GCC Extension).
18956 transaction-cancel-statement:
18957 __transaction_cancel transaction-attribute[opt] ;
18959 Note that the only valid attribute is "outer".
18962 static tree
18963 c_parser_transaction_cancel (c_parser *parser)
18965 location_t loc = c_parser_peek_token (parser)->location;
18966 tree attrs;
18967 bool is_outer = false;
18969 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18970 c_parser_consume_token (parser);
18972 attrs = c_parser_transaction_attributes (parser);
18973 if (attrs)
18974 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18976 if (!flag_tm)
18978 error_at (loc, "%<__transaction_cancel%> without "
18979 "transactional memory support enabled");
18980 goto ret_error;
18982 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18984 error_at (loc, "%<__transaction_cancel%> within a "
18985 "%<__transaction_relaxed%>");
18986 goto ret_error;
18988 else if (is_outer)
18990 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18991 && !is_tm_may_cancel_outer (current_function_decl))
18993 error_at (loc, "outer %<__transaction_cancel%> not "
18994 "within outer %<__transaction_atomic%>");
18995 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18996 goto ret_error;
18999 else if (parser->in_transaction == 0)
19001 error_at (loc, "%<__transaction_cancel%> not within "
19002 "%<__transaction_atomic%>");
19003 goto ret_error;
19006 return add_stmt (build_tm_abort_call (loc, is_outer));
19008 ret_error:
19009 return build1 (NOP_EXPR, void_type_node, error_mark_node);
19012 /* Parse a single source file. */
19014 void
19015 c_parse_file (void)
19017 /* Use local storage to begin. If the first token is a pragma, parse it.
19018 If it is #pragma GCC pch_preprocess, then this will load a PCH file
19019 which will cause garbage collection. */
19020 c_parser tparser;
19022 memset (&tparser, 0, sizeof tparser);
19023 tparser.tokens = &tparser.tokens_buf[0];
19024 the_parser = &tparser;
19026 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
19027 c_parser_pragma_pch_preprocess (&tparser);
19029 the_parser = ggc_alloc<c_parser> ();
19030 *the_parser = tparser;
19031 if (tparser.tokens == &tparser.tokens_buf[0])
19032 the_parser->tokens = &the_parser->tokens_buf[0];
19034 /* Initialize EH, if we've been told to do so. */
19035 if (flag_exceptions)
19036 using_eh_for_cleanups ();
19038 c_parser_translation_unit (the_parser);
19039 the_parser = NULL;
19042 /* This function parses Cilk Plus array notation. The starting index is
19043 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
19044 return value of this function is a tree_node called VALUE_TREE of type
19045 ARRAY_NOTATION_REF. */
19047 static tree
19048 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
19049 tree array_value)
19051 c_token *token = NULL;
19052 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
19053 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
19054 tree array_type_domain = NULL_TREE;
19056 if (array_value == error_mark_node || initial_index == error_mark_node)
19058 /* No need to continue. If either of these 2 were true, then an error
19059 must be emitted already. Thus, no need to emit them twice. */
19060 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19061 return error_mark_node;
19064 array_type = TREE_TYPE (array_value);
19065 gcc_assert (array_type);
19066 if (TREE_CODE (array_type) != ARRAY_TYPE
19067 && TREE_CODE (array_type) != POINTER_TYPE)
19069 error_at (loc, "base of array section must be pointer or array type");
19070 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19071 return error_mark_node;
19073 type = TREE_TYPE (array_type);
19074 token = c_parser_peek_token (parser);
19076 if (token->type == CPP_EOF)
19078 c_parser_error (parser, "expected %<:%> or numeral");
19079 return value_tree;
19081 else if (token->type == CPP_COLON)
19083 if (!initial_index)
19085 /* If we are here, then we have a case like this A[:]. */
19086 c_parser_consume_token (parser);
19087 if (TREE_CODE (array_type) == POINTER_TYPE)
19089 error_at (loc, "start-index and length fields necessary for "
19090 "using array notations in pointers");
19091 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19092 return error_mark_node;
19094 if (TREE_CODE (array_type) == FUNCTION_TYPE)
19096 error_at (loc, "array notations cannot be used with function "
19097 "type");
19098 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19099 return error_mark_node;
19101 array_type_domain = TYPE_DOMAIN (array_type);
19103 if (!array_type_domain)
19105 error_at (loc, "start-index and length fields necessary for "
19106 "using array notations in dimensionless arrays");
19107 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19108 return error_mark_node;
19111 start_index = TYPE_MIN_VALUE (array_type_domain);
19112 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
19113 start_index);
19114 if (!TYPE_MAX_VALUE (array_type_domain)
19115 || !TREE_CONSTANT (TYPE_MAX_VALUE (array_type_domain)))
19117 error_at (loc, "start-index and length fields necessary for "
19118 "using array notations in variable-length arrays");
19119 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19120 return error_mark_node;
19122 end_index = TYPE_MAX_VALUE (array_type_domain);
19123 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
19124 end_index, integer_one_node);
19125 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
19126 stride = build_int_cst (integer_type_node, 1);
19127 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
19129 else if (initial_index != error_mark_node)
19131 /* If we are here, then there should be 2 possibilities:
19132 1. Array [EXPR : EXPR]
19133 2. Array [EXPR : EXPR : EXPR]
19135 start_index = initial_index;
19137 if (TREE_CODE (array_type) == FUNCTION_TYPE)
19139 error_at (loc, "array notations cannot be used with function "
19140 "type");
19141 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19142 return error_mark_node;
19144 c_parser_consume_token (parser); /* consume the ':' */
19145 struct c_expr ce = c_parser_expression (parser);
19146 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
19147 end_index = ce.value;
19148 if (!end_index || end_index == error_mark_node)
19150 c_parser_skip_to_end_of_block_or_statement (parser);
19151 return error_mark_node;
19153 if (c_parser_peek_token (parser)->type == CPP_COLON)
19155 c_parser_consume_token (parser);
19156 ce = c_parser_expression (parser);
19157 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
19158 stride = ce.value;
19159 if (!stride || stride == error_mark_node)
19161 c_parser_skip_to_end_of_block_or_statement (parser);
19162 return error_mark_node;
19166 else
19167 c_parser_error (parser, "expected array notation expression");
19169 else
19170 c_parser_error (parser, "expected array notation expression");
19172 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
19174 value_tree = build_array_notation_ref (loc, array_value, start_index,
19175 end_index, stride, type);
19176 if (value_tree != error_mark_node)
19177 SET_EXPR_LOCATION (value_tree, loc);
19178 return value_tree;
19181 /* Parse the body of a function declaration marked with "__RTL".
19183 The RTL parser works on the level of characters read from a
19184 FILE *, whereas c_parser works at the level of tokens.
19185 Square this circle by consuming all of the tokens up to and
19186 including the closing brace, recording the start/end of the RTL
19187 fragment, and reopening the file and re-reading the relevant
19188 lines within the RTL parser.
19190 This requires the opening and closing braces of the C function
19191 to be on separate lines from the RTL they wrap.
19193 Take ownership of START_WITH_PASS, if non-NULL. */
19195 void
19196 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
19198 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
19200 free (start_with_pass);
19201 return;
19204 location_t start_loc = c_parser_peek_token (parser)->location;
19206 /* Consume all tokens, up to the closing brace, handling
19207 matching pairs of braces in the rtl dump. */
19208 int num_open_braces = 1;
19209 while (1)
19211 switch (c_parser_peek_token (parser)->type)
19213 case CPP_OPEN_BRACE:
19214 num_open_braces++;
19215 break;
19216 case CPP_CLOSE_BRACE:
19217 if (--num_open_braces == 0)
19218 goto found_closing_brace;
19219 break;
19220 case CPP_EOF:
19221 error_at (start_loc, "no closing brace");
19222 free (start_with_pass);
19223 return;
19224 default:
19225 break;
19227 c_parser_consume_token (parser);
19230 found_closing_brace:
19231 /* At the closing brace; record its location. */
19232 location_t end_loc = c_parser_peek_token (parser)->location;
19234 /* Consume the closing brace. */
19235 c_parser_consume_token (parser);
19237 /* Invoke the RTL parser. */
19238 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
19240 free (start_with_pass);
19241 return;
19244 /* If a pass name was provided for START_WITH_PASS, run the backend
19245 accordingly now, on the cfun created above, transferring
19246 ownership of START_WITH_PASS. */
19247 if (start_with_pass)
19248 run_rtl_passes (start_with_pass);
19251 #include "gt-c-c-parser.h"