PR c++/64767
[official-gcc.git] / gcc / c / c-parser.c
bloba3504d3eec9dd0f6c70372a3f209142cab9519dd
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "target.h"
42 #include "function.h"
43 #include "c-tree.h"
44 #include "timevar.h"
45 #include "stringpool.h"
46 #include "cgraph.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "varasm.h"
50 #include "trans-mem.h"
51 #include "c-family/c-pragma.h"
52 #include "c-lang.h"
53 #include "c-family/c-objc.h"
54 #include "plugin.h"
55 #include "omp-general.h"
56 #include "omp-offload.h"
57 #include "builtins.h"
58 #include "gomp-constants.h"
59 #include "c-family/c-indentation.h"
60 #include "gimple-expr.h"
61 #include "context.h"
62 #include "gcc-rich-location.h"
63 #include "c-parser.h"
64 #include "gimple-parser.h"
66 /* We need to walk over decls with incomplete struct/union/enum types
67 after parsing the whole translation unit.
68 In finish_decl(), if the decl is static, has incomplete
69 struct/union/enum type, it is appeneded to incomplete_record_decls.
70 In c_parser_translation_unit(), we iterate over incomplete_record_decls
71 and report error if any of the decls are still incomplete. */
73 vec<tree> incomplete_record_decls;
75 void
76 set_c_expr_source_range (c_expr *expr,
77 location_t start, location_t finish)
79 expr->src_range.m_start = start;
80 expr->src_range.m_finish = finish;
81 if (expr->value)
82 set_source_range (expr->value, start, finish);
85 void
86 set_c_expr_source_range (c_expr *expr,
87 source_range src_range)
89 expr->src_range = src_range;
90 if (expr->value)
91 set_source_range (expr->value, src_range);
95 /* Initialization routine for this file. */
97 void
98 c_parse_init (void)
100 /* The only initialization required is of the reserved word
101 identifiers. */
102 unsigned int i;
103 tree id;
104 int mask = 0;
106 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
107 the c_token structure. */
108 gcc_assert (RID_MAX <= 255);
110 mask |= D_CXXONLY;
111 if (!flag_isoc99)
112 mask |= D_C99;
113 if (flag_no_asm)
115 mask |= D_ASM | D_EXT;
116 if (!flag_isoc99)
117 mask |= D_EXT89;
119 if (!c_dialect_objc ())
120 mask |= D_OBJC | D_CXX_OBJC;
122 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
123 for (i = 0; i < num_c_common_reswords; i++)
125 /* If a keyword is disabled, do not enter it into the table
126 and so create a canonical spelling that isn't a keyword. */
127 if (c_common_reswords[i].disable & mask)
129 if (warn_cxx_compat
130 && (c_common_reswords[i].disable & D_CXXWARN))
132 id = get_identifier (c_common_reswords[i].word);
133 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
134 C_IS_RESERVED_WORD (id) = 1;
136 continue;
139 id = get_identifier (c_common_reswords[i].word);
140 C_SET_RID_CODE (id, c_common_reswords[i].rid);
141 C_IS_RESERVED_WORD (id) = 1;
142 ridpointers [(int) c_common_reswords[i].rid] = id;
145 for (i = 0; i < NUM_INT_N_ENTS; i++)
147 /* We always create the symbols but they aren't always supported. */
148 char name[50];
149 sprintf (name, "__int%d", int_n_data[i].bitsize);
150 id = get_identifier (name);
151 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
152 C_IS_RESERVED_WORD (id) = 1;
156 /* A parser structure recording information about the state and
157 context of parsing. Includes lexer information with up to two
158 tokens of look-ahead; more are not needed for C. */
159 struct GTY(()) c_parser {
160 /* The look-ahead tokens. */
161 c_token * GTY((skip)) tokens;
162 /* Buffer for look-ahead tokens. */
163 c_token tokens_buf[4];
164 /* How many look-ahead tokens are available (0 - 4, or
165 more if parsing from pre-lexed tokens). */
166 unsigned int tokens_avail;
167 /* True if a syntax error is being recovered from; false otherwise.
168 c_parser_error sets this flag. It should clear this flag when
169 enough tokens have been consumed to recover from the error. */
170 BOOL_BITFIELD error : 1;
171 /* True if we're processing a pragma, and shouldn't automatically
172 consume CPP_PRAGMA_EOL. */
173 BOOL_BITFIELD in_pragma : 1;
174 /* True if we're parsing the outermost block of an if statement. */
175 BOOL_BITFIELD in_if_block : 1;
176 /* True if we want to lex an untranslated string. */
177 BOOL_BITFIELD lex_untranslated_string : 1;
179 /* Objective-C specific parser/lexer information. */
181 /* True if we are in a context where the Objective-C "PQ" keywords
182 are considered keywords. */
183 BOOL_BITFIELD objc_pq_context : 1;
184 /* True if we are parsing a (potential) Objective-C foreach
185 statement. This is set to true after we parsed 'for (' and while
186 we wait for 'in' or ';' to decide if it's a standard C for loop or an
187 Objective-C foreach loop. */
188 BOOL_BITFIELD objc_could_be_foreach_context : 1;
189 /* The following flag is needed to contextualize Objective-C lexical
190 analysis. In some cases (e.g., 'int NSObject;'), it is
191 undesirable to bind an identifier to an Objective-C class, even
192 if a class with that name exists. */
193 BOOL_BITFIELD objc_need_raw_identifier : 1;
194 /* Nonzero if we're processing a __transaction statement. The value
195 is 1 | TM_STMT_ATTR_*. */
196 unsigned int in_transaction : 4;
197 /* True if we are in a context where the Objective-C "Property attribute"
198 keywords are valid. */
199 BOOL_BITFIELD objc_property_attr_context : 1;
201 /* Cilk Plus specific parser/lexer information. */
203 /* Buffer to hold all the tokens from parsing the vector attribute for the
204 SIMD-enabled functions (formerly known as elemental functions). */
205 vec <c_token, va_gc> *cilk_simd_fn_tokens;
208 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
210 c_token *
211 c_parser_tokens_buf (c_parser *parser, unsigned n)
213 return &parser->tokens_buf[n];
216 /* Return the error state of PARSER. */
218 bool
219 c_parser_error (c_parser *parser)
221 return parser->error;
224 /* Set the error state of PARSER to ERR. */
226 void
227 c_parser_set_error (c_parser *parser, bool err)
229 parser->error = err;
233 /* The actual parser and external interface. ??? Does this need to be
234 garbage-collected? */
236 static GTY (()) c_parser *the_parser;
238 /* Read in and lex a single token, storing it in *TOKEN. */
240 static void
241 c_lex_one_token (c_parser *parser, c_token *token)
243 timevar_push (TV_LEX);
245 token->type = c_lex_with_flags (&token->value, &token->location,
246 &token->flags,
247 (parser->lex_untranslated_string
248 ? C_LEX_STRING_NO_TRANSLATE : 0));
249 token->id_kind = C_ID_NONE;
250 token->keyword = RID_MAX;
251 token->pragma_kind = PRAGMA_NONE;
253 switch (token->type)
255 case CPP_NAME:
257 tree decl;
259 bool objc_force_identifier = parser->objc_need_raw_identifier;
260 if (c_dialect_objc ())
261 parser->objc_need_raw_identifier = false;
263 if (C_IS_RESERVED_WORD (token->value))
265 enum rid rid_code = C_RID_CODE (token->value);
267 if (rid_code == RID_CXX_COMPAT_WARN)
269 warning_at (token->location,
270 OPT_Wc___compat,
271 "identifier %qE conflicts with C++ keyword",
272 token->value);
274 else if (rid_code >= RID_FIRST_ADDR_SPACE
275 && rid_code <= RID_LAST_ADDR_SPACE)
277 addr_space_t as;
278 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
279 targetm.addr_space.diagnose_usage (as, token->location);
280 token->id_kind = C_ID_ADDRSPACE;
281 token->keyword = rid_code;
282 break;
284 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
286 /* We found an Objective-C "pq" keyword (in, out,
287 inout, bycopy, byref, oneway). They need special
288 care because the interpretation depends on the
289 context. */
290 if (parser->objc_pq_context)
292 token->type = CPP_KEYWORD;
293 token->keyword = rid_code;
294 break;
296 else if (parser->objc_could_be_foreach_context
297 && rid_code == RID_IN)
299 /* We are in Objective-C, inside a (potential)
300 foreach context (which means after having
301 parsed 'for (', but before having parsed ';'),
302 and we found 'in'. We consider it the keyword
303 which terminates the declaration at the
304 beginning of a foreach-statement. Note that
305 this means you can't use 'in' for anything else
306 in that context; in particular, in Objective-C
307 you can't use 'in' as the name of the running
308 variable in a C for loop. We could potentially
309 try to add code here to disambiguate, but it
310 seems a reasonable limitation. */
311 token->type = CPP_KEYWORD;
312 token->keyword = rid_code;
313 break;
315 /* Else, "pq" keywords outside of the "pq" context are
316 not keywords, and we fall through to the code for
317 normal tokens. */
319 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
321 /* We found an Objective-C "property attribute"
322 keyword (getter, setter, readonly, etc). These are
323 only valid in the property context. */
324 if (parser->objc_property_attr_context)
326 token->type = CPP_KEYWORD;
327 token->keyword = rid_code;
328 break;
330 /* Else they are not special keywords.
333 else if (c_dialect_objc ()
334 && (OBJC_IS_AT_KEYWORD (rid_code)
335 || OBJC_IS_CXX_KEYWORD (rid_code)))
337 /* We found one of the Objective-C "@" keywords (defs,
338 selector, synchronized, etc) or one of the
339 Objective-C "cxx" keywords (class, private,
340 protected, public, try, catch, throw) without a
341 preceding '@' sign. Do nothing and fall through to
342 the code for normal tokens (in C++ we would still
343 consider the CXX ones keywords, but not in C). */
346 else
348 token->type = CPP_KEYWORD;
349 token->keyword = rid_code;
350 break;
354 decl = lookup_name (token->value);
355 if (decl)
357 if (TREE_CODE (decl) == TYPE_DECL)
359 token->id_kind = C_ID_TYPENAME;
360 break;
363 else if (c_dialect_objc ())
365 tree objc_interface_decl = objc_is_class_name (token->value);
366 /* Objective-C class names are in the same namespace as
367 variables and typedefs, and hence are shadowed by local
368 declarations. */
369 if (objc_interface_decl
370 && (!objc_force_identifier || global_bindings_p ()))
372 token->value = objc_interface_decl;
373 token->id_kind = C_ID_CLASSNAME;
374 break;
377 token->id_kind = C_ID_ID;
379 break;
380 case CPP_AT_NAME:
381 /* This only happens in Objective-C; it must be a keyword. */
382 token->type = CPP_KEYWORD;
383 switch (C_RID_CODE (token->value))
385 /* Replace 'class' with '@class', 'private' with '@private',
386 etc. This prevents confusion with the C++ keyword
387 'class', and makes the tokens consistent with other
388 Objective-C 'AT' keywords. For example '@class' is
389 reported as RID_AT_CLASS which is consistent with
390 '@synchronized', which is reported as
391 RID_AT_SYNCHRONIZED.
393 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
394 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
395 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
396 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
397 case RID_THROW: token->keyword = RID_AT_THROW; break;
398 case RID_TRY: token->keyword = RID_AT_TRY; break;
399 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
400 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
401 default: token->keyword = C_RID_CODE (token->value);
403 break;
404 case CPP_COLON:
405 case CPP_COMMA:
406 case CPP_CLOSE_PAREN:
407 case CPP_SEMICOLON:
408 /* These tokens may affect the interpretation of any identifiers
409 following, if doing Objective-C. */
410 if (c_dialect_objc ())
411 parser->objc_need_raw_identifier = false;
412 break;
413 case CPP_PRAGMA:
414 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
415 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
416 token->value = NULL;
417 break;
418 default:
419 break;
421 timevar_pop (TV_LEX);
424 /* Return a pointer to the next token from PARSER, reading it in if
425 necessary. */
427 c_token *
428 c_parser_peek_token (c_parser *parser)
430 if (parser->tokens_avail == 0)
432 c_lex_one_token (parser, &parser->tokens[0]);
433 parser->tokens_avail = 1;
435 return &parser->tokens[0];
438 /* Return a pointer to the next-but-one token from PARSER, reading it
439 in if necessary. The next token is already read in. */
441 c_token *
442 c_parser_peek_2nd_token (c_parser *parser)
444 if (parser->tokens_avail >= 2)
445 return &parser->tokens[1];
446 gcc_assert (parser->tokens_avail == 1);
447 gcc_assert (parser->tokens[0].type != CPP_EOF);
448 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
449 c_lex_one_token (parser, &parser->tokens[1]);
450 parser->tokens_avail = 2;
451 return &parser->tokens[1];
454 /* Return a pointer to the Nth token from PARSER, reading it
455 in if necessary. The N-1th token is already read in. */
457 c_token *
458 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
460 /* N is 1-based, not zero-based. */
461 gcc_assert (n > 0);
463 if (parser->tokens_avail >= n)
464 return &parser->tokens[n - 1];
465 gcc_assert (parser->tokens_avail == n - 1);
466 c_lex_one_token (parser, &parser->tokens[n - 1]);
467 parser->tokens_avail = n;
468 return &parser->tokens[n - 1];
471 bool
472 c_keyword_starts_typename (enum rid keyword)
474 switch (keyword)
476 case RID_UNSIGNED:
477 case RID_LONG:
478 case RID_SHORT:
479 case RID_SIGNED:
480 case RID_COMPLEX:
481 case RID_INT:
482 case RID_CHAR:
483 case RID_FLOAT:
484 case RID_DOUBLE:
485 case RID_VOID:
486 case RID_DFLOAT32:
487 case RID_DFLOAT64:
488 case RID_DFLOAT128:
489 CASE_RID_FLOATN_NX:
490 case RID_BOOL:
491 case RID_ENUM:
492 case RID_STRUCT:
493 case RID_UNION:
494 case RID_TYPEOF:
495 case RID_CONST:
496 case RID_ATOMIC:
497 case RID_VOLATILE:
498 case RID_RESTRICT:
499 case RID_ATTRIBUTE:
500 case RID_FRACT:
501 case RID_ACCUM:
502 case RID_SAT:
503 case RID_AUTO_TYPE:
504 return true;
505 default:
506 if (keyword >= RID_FIRST_INT_N
507 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
508 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
509 return true;
510 return false;
514 /* Return true if TOKEN can start a type name,
515 false otherwise. */
516 bool
517 c_token_starts_typename (c_token *token)
519 switch (token->type)
521 case CPP_NAME:
522 switch (token->id_kind)
524 case C_ID_ID:
525 return false;
526 case C_ID_ADDRSPACE:
527 return true;
528 case C_ID_TYPENAME:
529 return true;
530 case C_ID_CLASSNAME:
531 gcc_assert (c_dialect_objc ());
532 return true;
533 default:
534 gcc_unreachable ();
536 case CPP_KEYWORD:
537 return c_keyword_starts_typename (token->keyword);
538 case CPP_LESS:
539 if (c_dialect_objc ())
540 return true;
541 return false;
542 default:
543 return false;
547 /* Return true if the next token from PARSER can start a type name,
548 false otherwise. LA specifies how to do lookahead in order to
549 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
551 static inline bool
552 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
554 c_token *token = c_parser_peek_token (parser);
555 if (c_token_starts_typename (token))
556 return true;
558 /* Try a bit harder to detect an unknown typename. */
559 if (la != cla_prefer_id
560 && token->type == CPP_NAME
561 && token->id_kind == C_ID_ID
563 /* Do not try too hard when we could have "object in array". */
564 && !parser->objc_could_be_foreach_context
566 && (la == cla_prefer_type
567 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
568 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
570 /* Only unknown identifiers. */
571 && !lookup_name (token->value))
572 return true;
574 return false;
577 /* Return true if TOKEN is a type qualifier, false otherwise. */
578 static bool
579 c_token_is_qualifier (c_token *token)
581 switch (token->type)
583 case CPP_NAME:
584 switch (token->id_kind)
586 case C_ID_ADDRSPACE:
587 return true;
588 default:
589 return false;
591 case CPP_KEYWORD:
592 switch (token->keyword)
594 case RID_CONST:
595 case RID_VOLATILE:
596 case RID_RESTRICT:
597 case RID_ATTRIBUTE:
598 case RID_ATOMIC:
599 return true;
600 default:
601 return false;
603 case CPP_LESS:
604 return false;
605 default:
606 gcc_unreachable ();
610 /* Return true if the next token from PARSER is a type qualifier,
611 false otherwise. */
612 static inline bool
613 c_parser_next_token_is_qualifier (c_parser *parser)
615 c_token *token = c_parser_peek_token (parser);
616 return c_token_is_qualifier (token);
619 /* Return true if TOKEN can start declaration specifiers, false
620 otherwise. */
621 static bool
622 c_token_starts_declspecs (c_token *token)
624 switch (token->type)
626 case CPP_NAME:
627 switch (token->id_kind)
629 case C_ID_ID:
630 return false;
631 case C_ID_ADDRSPACE:
632 return true;
633 case C_ID_TYPENAME:
634 return true;
635 case C_ID_CLASSNAME:
636 gcc_assert (c_dialect_objc ());
637 return true;
638 default:
639 gcc_unreachable ();
641 case CPP_KEYWORD:
642 switch (token->keyword)
644 case RID_STATIC:
645 case RID_EXTERN:
646 case RID_REGISTER:
647 case RID_TYPEDEF:
648 case RID_INLINE:
649 case RID_NORETURN:
650 case RID_AUTO:
651 case RID_THREAD:
652 case RID_UNSIGNED:
653 case RID_LONG:
654 case RID_SHORT:
655 case RID_SIGNED:
656 case RID_COMPLEX:
657 case RID_INT:
658 case RID_CHAR:
659 case RID_FLOAT:
660 case RID_DOUBLE:
661 case RID_VOID:
662 case RID_DFLOAT32:
663 case RID_DFLOAT64:
664 case RID_DFLOAT128:
665 CASE_RID_FLOATN_NX:
666 case RID_BOOL:
667 case RID_ENUM:
668 case RID_STRUCT:
669 case RID_UNION:
670 case RID_TYPEOF:
671 case RID_CONST:
672 case RID_VOLATILE:
673 case RID_RESTRICT:
674 case RID_ATTRIBUTE:
675 case RID_FRACT:
676 case RID_ACCUM:
677 case RID_SAT:
678 case RID_ALIGNAS:
679 case RID_ATOMIC:
680 case RID_AUTO_TYPE:
681 return true;
682 default:
683 if (token->keyword >= RID_FIRST_INT_N
684 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
685 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
686 return true;
687 return false;
689 case CPP_LESS:
690 if (c_dialect_objc ())
691 return true;
692 return false;
693 default:
694 return false;
699 /* Return true if TOKEN can start declaration specifiers or a static
700 assertion, false otherwise. */
701 static bool
702 c_token_starts_declaration (c_token *token)
704 if (c_token_starts_declspecs (token)
705 || token->keyword == RID_STATIC_ASSERT)
706 return true;
707 else
708 return false;
711 /* Return true if the next token from PARSER can start declaration
712 specifiers, false otherwise. */
713 bool
714 c_parser_next_token_starts_declspecs (c_parser *parser)
716 c_token *token = c_parser_peek_token (parser);
718 /* In Objective-C, a classname normally starts a declspecs unless it
719 is immediately followed by a dot. In that case, it is the
720 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
721 setter/getter on the class. c_token_starts_declspecs() can't
722 differentiate between the two cases because it only checks the
723 current token, so we have a special check here. */
724 if (c_dialect_objc ()
725 && token->type == CPP_NAME
726 && token->id_kind == C_ID_CLASSNAME
727 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
728 return false;
730 return c_token_starts_declspecs (token);
733 /* Return true if the next tokens from PARSER can start declaration
734 specifiers or a static assertion, false otherwise. */
735 bool
736 c_parser_next_tokens_start_declaration (c_parser *parser)
738 c_token *token = c_parser_peek_token (parser);
740 /* Same as above. */
741 if (c_dialect_objc ()
742 && token->type == CPP_NAME
743 && token->id_kind == C_ID_CLASSNAME
744 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
745 return false;
747 /* Labels do not start declarations. */
748 if (token->type == CPP_NAME
749 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
750 return false;
752 if (c_token_starts_declaration (token))
753 return true;
755 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
756 return true;
758 return false;
761 /* Consume the next token from PARSER. */
763 void
764 c_parser_consume_token (c_parser *parser)
766 gcc_assert (parser->tokens_avail >= 1);
767 gcc_assert (parser->tokens[0].type != CPP_EOF);
768 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
769 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
770 if (parser->tokens != &parser->tokens_buf[0])
771 parser->tokens++;
772 else if (parser->tokens_avail == 2)
773 parser->tokens[0] = parser->tokens[1];
774 parser->tokens_avail--;
777 /* Expect the current token to be a #pragma. Consume it and remember
778 that we've begun parsing a pragma. */
780 static void
781 c_parser_consume_pragma (c_parser *parser)
783 gcc_assert (!parser->in_pragma);
784 gcc_assert (parser->tokens_avail >= 1);
785 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
786 if (parser->tokens != &parser->tokens_buf[0])
787 parser->tokens++;
788 else if (parser->tokens_avail == 2)
789 parser->tokens[0] = parser->tokens[1];
790 parser->tokens_avail--;
791 parser->in_pragma = true;
794 /* Update the global input_location from TOKEN. */
795 static inline void
796 c_parser_set_source_position_from_token (c_token *token)
798 if (token->type != CPP_EOF)
800 input_location = token->location;
804 /* Helper function for c_parser_error.
805 Having peeked a token of kind TOK1_KIND that might signify
806 a conflict marker, peek successor tokens to determine
807 if we actually do have a conflict marker.
808 Specifically, we consider a run of 7 '<', '=' or '>' characters
809 at the start of a line as a conflict marker.
810 These come through the lexer as three pairs and a single,
811 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
812 If it returns true, *OUT_LOC is written to with the location/range
813 of the marker. */
815 static bool
816 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
817 location_t *out_loc)
819 c_token *token2 = c_parser_peek_2nd_token (parser);
820 if (token2->type != tok1_kind)
821 return false;
822 c_token *token3 = c_parser_peek_nth_token (parser, 3);
823 if (token3->type != tok1_kind)
824 return false;
825 c_token *token4 = c_parser_peek_nth_token (parser, 4);
826 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
827 return false;
829 /* It must be at the start of the line. */
830 location_t start_loc = c_parser_peek_token (parser)->location;
831 if (LOCATION_COLUMN (start_loc) != 1)
832 return false;
834 /* We have a conflict marker. Construct a location of the form:
835 <<<<<<<
836 ^~~~~~~
837 with start == caret, finishing at the end of the marker. */
838 location_t finish_loc = get_finish (token4->location);
839 *out_loc = make_location (start_loc, start_loc, finish_loc);
841 return true;
844 /* Issue a diagnostic of the form
845 FILE:LINE: MESSAGE before TOKEN
846 where TOKEN is the next token in the input stream of PARSER.
847 MESSAGE (specified by the caller) is usually of the form "expected
848 OTHER-TOKEN".
850 Do not issue a diagnostic if still recovering from an error.
852 ??? This is taken from the C++ parser, but building up messages in
853 this way is not i18n-friendly and some other approach should be
854 used. */
856 void
857 c_parser_error (c_parser *parser, const char *gmsgid)
859 c_token *token = c_parser_peek_token (parser);
860 if (parser->error)
861 return;
862 parser->error = true;
863 if (!gmsgid)
864 return;
866 /* If this is actually a conflict marker, report it as such. */
867 if (token->type == CPP_LSHIFT
868 || token->type == CPP_RSHIFT
869 || token->type == CPP_EQ_EQ)
871 location_t loc;
872 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
874 error_at (loc, "version control conflict marker in file");
875 return;
879 /* This diagnostic makes more sense if it is tagged to the line of
880 the token we just peeked at. */
881 c_parser_set_source_position_from_token (token);
882 c_parse_error (gmsgid,
883 /* Because c_parse_error does not understand
884 CPP_KEYWORD, keywords are treated like
885 identifiers. */
886 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
887 /* ??? The C parser does not save the cpp flags of a
888 token, we need to pass 0 here and we will not get
889 the source spelling of some tokens but rather the
890 canonical spelling. */
891 token->value, /*flags=*/0);
894 /* If the next token is of the indicated TYPE, consume it. Otherwise,
895 issue the error MSGID. If MSGID is NULL then a message has already
896 been produced and no message will be produced this time. Returns
897 true if found, false otherwise. */
899 bool
900 c_parser_require (c_parser *parser,
901 enum cpp_ttype type,
902 const char *msgid)
904 if (c_parser_next_token_is (parser, type))
906 c_parser_consume_token (parser);
907 return true;
909 else
911 c_parser_error (parser, msgid);
912 return false;
916 /* If the next token is the indicated keyword, consume it. Otherwise,
917 issue the error MSGID. Returns true if found, false otherwise. */
919 static bool
920 c_parser_require_keyword (c_parser *parser,
921 enum rid keyword,
922 const char *msgid)
924 if (c_parser_next_token_is_keyword (parser, keyword))
926 c_parser_consume_token (parser);
927 return true;
929 else
931 c_parser_error (parser, msgid);
932 return false;
936 /* Like c_parser_require, except that tokens will be skipped until the
937 desired token is found. An error message is still produced if the
938 next token is not as expected. If MSGID is NULL then a message has
939 already been produced and no message will be produced this
940 time. */
942 void
943 c_parser_skip_until_found (c_parser *parser,
944 enum cpp_ttype type,
945 const char *msgid)
947 unsigned nesting_depth = 0;
949 if (c_parser_require (parser, type, msgid))
950 return;
952 /* Skip tokens until the desired token is found. */
953 while (true)
955 /* Peek at the next token. */
956 c_token *token = c_parser_peek_token (parser);
957 /* If we've reached the token we want, consume it and stop. */
958 if (token->type == type && !nesting_depth)
960 c_parser_consume_token (parser);
961 break;
964 /* If we've run out of tokens, stop. */
965 if (token->type == CPP_EOF)
966 return;
967 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
968 return;
969 if (token->type == CPP_OPEN_BRACE
970 || token->type == CPP_OPEN_PAREN
971 || token->type == CPP_OPEN_SQUARE)
972 ++nesting_depth;
973 else if (token->type == CPP_CLOSE_BRACE
974 || token->type == CPP_CLOSE_PAREN
975 || token->type == CPP_CLOSE_SQUARE)
977 if (nesting_depth-- == 0)
978 break;
980 /* Consume this token. */
981 c_parser_consume_token (parser);
983 parser->error = false;
986 /* Skip tokens until the end of a parameter is found, but do not
987 consume the comma, semicolon or closing delimiter. */
989 static void
990 c_parser_skip_to_end_of_parameter (c_parser *parser)
992 unsigned nesting_depth = 0;
994 while (true)
996 c_token *token = c_parser_peek_token (parser);
997 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
998 && !nesting_depth)
999 break;
1000 /* If we've run out of tokens, stop. */
1001 if (token->type == CPP_EOF)
1002 return;
1003 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1004 return;
1005 if (token->type == CPP_OPEN_BRACE
1006 || token->type == CPP_OPEN_PAREN
1007 || token->type == CPP_OPEN_SQUARE)
1008 ++nesting_depth;
1009 else if (token->type == CPP_CLOSE_BRACE
1010 || token->type == CPP_CLOSE_PAREN
1011 || token->type == CPP_CLOSE_SQUARE)
1013 if (nesting_depth-- == 0)
1014 break;
1016 /* Consume this token. */
1017 c_parser_consume_token (parser);
1019 parser->error = false;
1022 /* Expect to be at the end of the pragma directive and consume an
1023 end of line marker. */
1025 static void
1026 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1028 gcc_assert (parser->in_pragma);
1029 parser->in_pragma = false;
1031 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1032 c_parser_error (parser, "expected end of line");
1034 cpp_ttype token_type;
1037 c_token *token = c_parser_peek_token (parser);
1038 token_type = token->type;
1039 if (token_type == CPP_EOF)
1040 break;
1041 c_parser_consume_token (parser);
1043 while (token_type != CPP_PRAGMA_EOL);
1045 parser->error = false;
1048 /* Skip tokens until we have consumed an entire block, or until we
1049 have consumed a non-nested ';'. */
1051 static void
1052 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1054 unsigned nesting_depth = 0;
1055 bool save_error = parser->error;
1057 while (true)
1059 c_token *token;
1061 /* Peek at the next token. */
1062 token = c_parser_peek_token (parser);
1064 switch (token->type)
1066 case CPP_EOF:
1067 return;
1069 case CPP_PRAGMA_EOL:
1070 if (parser->in_pragma)
1071 return;
1072 break;
1074 case CPP_SEMICOLON:
1075 /* If the next token is a ';', we have reached the
1076 end of the statement. */
1077 if (!nesting_depth)
1079 /* Consume the ';'. */
1080 c_parser_consume_token (parser);
1081 goto finished;
1083 break;
1085 case CPP_CLOSE_BRACE:
1086 /* If the next token is a non-nested '}', then we have
1087 reached the end of the current block. */
1088 if (nesting_depth == 0 || --nesting_depth == 0)
1090 c_parser_consume_token (parser);
1091 goto finished;
1093 break;
1095 case CPP_OPEN_BRACE:
1096 /* If it the next token is a '{', then we are entering a new
1097 block. Consume the entire block. */
1098 ++nesting_depth;
1099 break;
1101 case CPP_PRAGMA:
1102 /* If we see a pragma, consume the whole thing at once. We
1103 have some safeguards against consuming pragmas willy-nilly.
1104 Normally, we'd expect to be here with parser->error set,
1105 which disables these safeguards. But it's possible to get
1106 here for secondary error recovery, after parser->error has
1107 been cleared. */
1108 c_parser_consume_pragma (parser);
1109 c_parser_skip_to_pragma_eol (parser);
1110 parser->error = save_error;
1111 continue;
1113 default:
1114 break;
1117 c_parser_consume_token (parser);
1120 finished:
1121 parser->error = false;
1124 /* CPP's options (initialized by c-opts.c). */
1125 extern cpp_options *cpp_opts;
1127 /* Save the warning flags which are controlled by __extension__. */
1129 static inline int
1130 disable_extension_diagnostics (void)
1132 int ret = (pedantic
1133 | (warn_pointer_arith << 1)
1134 | (warn_traditional << 2)
1135 | (flag_iso << 3)
1136 | (warn_long_long << 4)
1137 | (warn_cxx_compat << 5)
1138 | (warn_overlength_strings << 6)
1139 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1140 play tricks to properly restore it. */
1141 | ((warn_c90_c99_compat == 1) << 7)
1142 | ((warn_c90_c99_compat == -1) << 8)
1143 /* Similarly for warn_c99_c11_compat. */
1144 | ((warn_c99_c11_compat == 1) << 9)
1145 | ((warn_c99_c11_compat == -1) << 10)
1147 cpp_opts->cpp_pedantic = pedantic = 0;
1148 warn_pointer_arith = 0;
1149 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1150 flag_iso = 0;
1151 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1152 warn_cxx_compat = 0;
1153 warn_overlength_strings = 0;
1154 warn_c90_c99_compat = 0;
1155 warn_c99_c11_compat = 0;
1156 return ret;
1159 /* Restore the warning flags which are controlled by __extension__.
1160 FLAGS is the return value from disable_extension_diagnostics. */
1162 static inline void
1163 restore_extension_diagnostics (int flags)
1165 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1166 warn_pointer_arith = (flags >> 1) & 1;
1167 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1168 flag_iso = (flags >> 3) & 1;
1169 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1170 warn_cxx_compat = (flags >> 5) & 1;
1171 warn_overlength_strings = (flags >> 6) & 1;
1172 /* See above for why is this needed. */
1173 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1174 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1177 /* Helper data structure for parsing #pragma acc routine. */
1178 struct oacc_routine_data {
1179 bool error_seen; /* Set if error has been reported. */
1180 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1181 tree clauses;
1182 location_t loc;
1185 static void c_parser_external_declaration (c_parser *);
1186 static void c_parser_asm_definition (c_parser *);
1187 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1188 bool, bool, tree *, vec<c_token>,
1189 struct oacc_routine_data * = NULL,
1190 bool * = NULL);
1191 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1192 static void c_parser_static_assert_declaration (c_parser *);
1193 static struct c_typespec c_parser_enum_specifier (c_parser *);
1194 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1195 static tree c_parser_struct_declaration (c_parser *);
1196 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1197 static tree c_parser_alignas_specifier (c_parser *);
1198 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1199 c_dtr_syn, bool *);
1200 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1201 bool,
1202 struct c_declarator *);
1203 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1204 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1205 tree);
1206 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1207 static tree c_parser_simple_asm_expr (c_parser *);
1208 static tree c_parser_attributes (c_parser *);
1209 static struct c_expr c_parser_initializer (c_parser *);
1210 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1211 struct obstack *);
1212 static void c_parser_initelt (c_parser *, struct obstack *);
1213 static void c_parser_initval (c_parser *, struct c_expr *,
1214 struct obstack *);
1215 static tree c_parser_compound_statement (c_parser *);
1216 static void c_parser_compound_statement_nostart (c_parser *);
1217 static void c_parser_label (c_parser *);
1218 static void c_parser_statement (c_parser *, bool *);
1219 static void c_parser_statement_after_labels (c_parser *, bool *,
1220 vec<tree> * = NULL);
1221 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1222 static void c_parser_switch_statement (c_parser *, bool *);
1223 static void c_parser_while_statement (c_parser *, bool, bool *);
1224 static void c_parser_do_statement (c_parser *, bool);
1225 static void c_parser_for_statement (c_parser *, bool, bool *);
1226 static tree c_parser_asm_statement (c_parser *);
1227 static tree c_parser_asm_operands (c_parser *);
1228 static tree c_parser_asm_goto_operands (c_parser *);
1229 static tree c_parser_asm_clobbers (c_parser *);
1230 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1231 tree = NULL_TREE);
1232 static struct c_expr c_parser_conditional_expression (c_parser *,
1233 struct c_expr *, tree);
1234 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1235 tree);
1236 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1237 static struct c_expr c_parser_unary_expression (c_parser *);
1238 static struct c_expr c_parser_sizeof_expression (c_parser *);
1239 static struct c_expr c_parser_alignof_expression (c_parser *);
1240 static struct c_expr c_parser_postfix_expression (c_parser *);
1241 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1242 struct c_type_name *,
1243 location_t);
1244 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1245 location_t loc,
1246 struct c_expr);
1247 static tree c_parser_transaction (c_parser *, enum rid);
1248 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1249 static tree c_parser_transaction_cancel (c_parser *);
1250 static struct c_expr c_parser_expression (c_parser *);
1251 static struct c_expr c_parser_expression_conv (c_parser *);
1252 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1253 vec<tree, va_gc> **, location_t *,
1254 tree *, vec<location_t> *,
1255 unsigned int * = NULL);
1256 static void c_parser_oacc_declare (c_parser *);
1257 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1258 static void c_parser_oacc_update (c_parser *);
1259 static void c_parser_omp_construct (c_parser *, bool *);
1260 static void c_parser_omp_threadprivate (c_parser *);
1261 static void c_parser_omp_barrier (c_parser *);
1262 static void c_parser_omp_flush (c_parser *);
1263 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1264 tree, tree *, bool *);
1265 static void c_parser_omp_taskwait (c_parser *);
1266 static void c_parser_omp_taskyield (c_parser *);
1267 static void c_parser_omp_cancel (c_parser *);
1269 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1270 pragma_stmt, pragma_compound };
1271 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1272 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1273 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1274 static void c_parser_omp_end_declare_target (c_parser *);
1275 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1276 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1277 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1279 /* These Objective-C parser functions are only ever called when
1280 compiling Objective-C. */
1281 static void c_parser_objc_class_definition (c_parser *, tree);
1282 static void c_parser_objc_class_instance_variables (c_parser *);
1283 static void c_parser_objc_class_declaration (c_parser *);
1284 static void c_parser_objc_alias_declaration (c_parser *);
1285 static void c_parser_objc_protocol_definition (c_parser *, tree);
1286 static bool c_parser_objc_method_type (c_parser *);
1287 static void c_parser_objc_method_definition (c_parser *);
1288 static void c_parser_objc_methodprotolist (c_parser *);
1289 static void c_parser_objc_methodproto (c_parser *);
1290 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1291 static tree c_parser_objc_type_name (c_parser *);
1292 static tree c_parser_objc_protocol_refs (c_parser *);
1293 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1294 static void c_parser_objc_synchronized_statement (c_parser *);
1295 static tree c_parser_objc_selector (c_parser *);
1296 static tree c_parser_objc_selector_arg (c_parser *);
1297 static tree c_parser_objc_receiver (c_parser *);
1298 static tree c_parser_objc_message_args (c_parser *);
1299 static tree c_parser_objc_keywordexpr (c_parser *);
1300 static void c_parser_objc_at_property_declaration (c_parser *);
1301 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1302 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1303 static bool c_parser_objc_diagnose_bad_element_prefix
1304 (c_parser *, struct c_declspecs *);
1306 /* Cilk Plus supporting routines. */
1307 static void c_parser_cilk_simd (c_parser *, bool *);
1308 static void c_parser_cilk_for (c_parser *, tree, bool *);
1309 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1310 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1311 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1312 static void c_parser_cilk_grainsize (c_parser *, bool *);
1314 /* Parse a translation unit (C90 6.7, C99 6.9).
1316 translation-unit:
1317 external-declarations
1319 external-declarations:
1320 external-declaration
1321 external-declarations external-declaration
1323 GNU extensions:
1325 translation-unit:
1326 empty
1329 static void
1330 c_parser_translation_unit (c_parser *parser)
1332 if (c_parser_next_token_is (parser, CPP_EOF))
1334 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1335 "ISO C forbids an empty translation unit");
1337 else
1339 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1340 mark_valid_location_for_stdc_pragma (false);
1343 ggc_collect ();
1344 c_parser_external_declaration (parser);
1345 obstack_free (&parser_obstack, obstack_position);
1347 while (c_parser_next_token_is_not (parser, CPP_EOF));
1350 unsigned int i;
1351 tree decl;
1352 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1353 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1354 error ("storage size of %q+D isn%'t known", decl);
1357 /* Parse an external declaration (C90 6.7, C99 6.9).
1359 external-declaration:
1360 function-definition
1361 declaration
1363 GNU extensions:
1365 external-declaration:
1366 asm-definition
1368 __extension__ external-declaration
1370 Objective-C:
1372 external-declaration:
1373 objc-class-definition
1374 objc-class-declaration
1375 objc-alias-declaration
1376 objc-protocol-definition
1377 objc-method-definition
1378 @end
1381 static void
1382 c_parser_external_declaration (c_parser *parser)
1384 int ext;
1385 switch (c_parser_peek_token (parser)->type)
1387 case CPP_KEYWORD:
1388 switch (c_parser_peek_token (parser)->keyword)
1390 case RID_EXTENSION:
1391 ext = disable_extension_diagnostics ();
1392 c_parser_consume_token (parser);
1393 c_parser_external_declaration (parser);
1394 restore_extension_diagnostics (ext);
1395 break;
1396 case RID_ASM:
1397 c_parser_asm_definition (parser);
1398 break;
1399 case RID_AT_INTERFACE:
1400 case RID_AT_IMPLEMENTATION:
1401 gcc_assert (c_dialect_objc ());
1402 c_parser_objc_class_definition (parser, NULL_TREE);
1403 break;
1404 case RID_AT_CLASS:
1405 gcc_assert (c_dialect_objc ());
1406 c_parser_objc_class_declaration (parser);
1407 break;
1408 case RID_AT_ALIAS:
1409 gcc_assert (c_dialect_objc ());
1410 c_parser_objc_alias_declaration (parser);
1411 break;
1412 case RID_AT_PROTOCOL:
1413 gcc_assert (c_dialect_objc ());
1414 c_parser_objc_protocol_definition (parser, NULL_TREE);
1415 break;
1416 case RID_AT_PROPERTY:
1417 gcc_assert (c_dialect_objc ());
1418 c_parser_objc_at_property_declaration (parser);
1419 break;
1420 case RID_AT_SYNTHESIZE:
1421 gcc_assert (c_dialect_objc ());
1422 c_parser_objc_at_synthesize_declaration (parser);
1423 break;
1424 case RID_AT_DYNAMIC:
1425 gcc_assert (c_dialect_objc ());
1426 c_parser_objc_at_dynamic_declaration (parser);
1427 break;
1428 case RID_AT_END:
1429 gcc_assert (c_dialect_objc ());
1430 c_parser_consume_token (parser);
1431 objc_finish_implementation ();
1432 break;
1433 default:
1434 goto decl_or_fndef;
1436 break;
1437 case CPP_SEMICOLON:
1438 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1439 "ISO C does not allow extra %<;%> outside of a function");
1440 c_parser_consume_token (parser);
1441 break;
1442 case CPP_PRAGMA:
1443 mark_valid_location_for_stdc_pragma (true);
1444 c_parser_pragma (parser, pragma_external, NULL);
1445 mark_valid_location_for_stdc_pragma (false);
1446 break;
1447 case CPP_PLUS:
1448 case CPP_MINUS:
1449 if (c_dialect_objc ())
1451 c_parser_objc_method_definition (parser);
1452 break;
1454 /* Else fall through, and yield a syntax error trying to parse
1455 as a declaration or function definition. */
1456 /* FALLTHRU */
1457 default:
1458 decl_or_fndef:
1459 /* A declaration or a function definition (or, in Objective-C,
1460 an @interface or @protocol with prefix attributes). We can
1461 only tell which after parsing the declaration specifiers, if
1462 any, and the first declarator. */
1463 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1464 NULL, vNULL);
1465 break;
1469 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1470 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1472 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1473 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1474 accepted; otherwise (old-style parameter declarations) only other
1475 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1476 assertion is accepted; otherwise (old-style parameter declarations)
1477 it is not. If NESTED is true, we are inside a function or parsing
1478 old-style parameter declarations; any functions encountered are
1479 nested functions and declaration specifiers are required; otherwise
1480 we are at top level and functions are normal functions and
1481 declaration specifiers may be optional. If EMPTY_OK is true, empty
1482 declarations are OK (subject to all other constraints); otherwise
1483 (old-style parameter declarations) they are diagnosed. If
1484 START_ATTR_OK is true, the declaration specifiers may start with
1485 attributes; otherwise they may not.
1486 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1487 declaration when parsing an Objective-C foreach statement.
1488 FALLTHRU_ATTR_P is used to signal whether this function parsed
1489 "__attribute__((fallthrough));".
1491 declaration:
1492 declaration-specifiers init-declarator-list[opt] ;
1493 static_assert-declaration
1495 function-definition:
1496 declaration-specifiers[opt] declarator declaration-list[opt]
1497 compound-statement
1499 declaration-list:
1500 declaration
1501 declaration-list declaration
1503 init-declarator-list:
1504 init-declarator
1505 init-declarator-list , init-declarator
1507 init-declarator:
1508 declarator simple-asm-expr[opt] attributes[opt]
1509 declarator simple-asm-expr[opt] attributes[opt] = initializer
1511 GNU extensions:
1513 nested-function-definition:
1514 declaration-specifiers declarator declaration-list[opt]
1515 compound-statement
1517 attribute ;
1519 Objective-C:
1520 attributes objc-class-definition
1521 attributes objc-category-definition
1522 attributes objc-protocol-definition
1524 The simple-asm-expr and attributes are GNU extensions.
1526 This function does not handle __extension__; that is handled in its
1527 callers. ??? Following the old parser, __extension__ may start
1528 external declarations, declarations in functions and declarations
1529 at the start of "for" loops, but not old-style parameter
1530 declarations.
1532 C99 requires declaration specifiers in a function definition; the
1533 absence is diagnosed through the diagnosis of implicit int. In GNU
1534 C we also allow but diagnose declarations without declaration
1535 specifiers, but only at top level (elsewhere they conflict with
1536 other syntax).
1538 In Objective-C, declarations of the looping variable in a foreach
1539 statement are exceptionally terminated by 'in' (for example, 'for
1540 (NSObject *object in array) { ... }').
1542 OpenMP:
1544 declaration:
1545 threadprivate-directive
1547 GIMPLE:
1549 gimple-function-definition:
1550 declaration-specifiers[opt] __GIMPLE (gimple-pass-list) declarator
1551 declaration-list[opt] compound-statement */
1553 static void
1554 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1555 bool static_assert_ok, bool empty_ok,
1556 bool nested, bool start_attr_ok,
1557 tree *objc_foreach_object_declaration,
1558 vec<c_token> omp_declare_simd_clauses,
1559 struct oacc_routine_data *oacc_routine_data,
1560 bool *fallthru_attr_p)
1562 struct c_declspecs *specs;
1563 tree prefix_attrs;
1564 tree all_prefix_attrs;
1565 bool diagnosed_no_specs = false;
1566 location_t here = c_parser_peek_token (parser)->location;
1568 if (static_assert_ok
1569 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1571 c_parser_static_assert_declaration (parser);
1572 return;
1574 specs = build_null_declspecs ();
1576 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1577 if (c_parser_peek_token (parser)->type == CPP_NAME
1578 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1579 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1580 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1581 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1583 tree name = c_parser_peek_token (parser)->value;
1585 /* Issue a warning about NAME being an unknown type name, perhaps
1586 with some kind of hint.
1587 If the user forgot a "struct" etc, suggest inserting
1588 it. Otherwise, attempt to look for misspellings. */
1589 gcc_rich_location richloc (here);
1590 if (tag_exists_p (RECORD_TYPE, name))
1592 /* This is not C++ with its implicit typedef. */
1593 richloc.add_fixit_insert_before ("struct ");
1594 error_at_rich_loc (&richloc,
1595 "unknown type name %qE;"
1596 " use %<struct%> keyword to refer to the type",
1597 name);
1599 else if (tag_exists_p (UNION_TYPE, name))
1601 richloc.add_fixit_insert_before ("union ");
1602 error_at_rich_loc (&richloc,
1603 "unknown type name %qE;"
1604 " use %<union%> keyword to refer to the type",
1605 name);
1607 else if (tag_exists_p (ENUMERAL_TYPE, name))
1609 richloc.add_fixit_insert_before ("enum ");
1610 error_at_rich_loc (&richloc,
1611 "unknown type name %qE;"
1612 " use %<enum%> keyword to refer to the type",
1613 name);
1615 else
1617 const char *hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME);
1618 if (hint)
1620 richloc.add_fixit_replace (hint);
1621 error_at_rich_loc (&richloc,
1622 "unknown type name %qE; did you mean %qs?",
1623 name, hint);
1625 else
1626 error_at (here, "unknown type name %qE", name);
1629 /* Parse declspecs normally to get a correct pointer type, but avoid
1630 a further "fails to be a type name" error. Refuse nested functions
1631 since it is not how the user likely wants us to recover. */
1632 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1633 c_parser_peek_token (parser)->keyword = RID_VOID;
1634 c_parser_peek_token (parser)->value = error_mark_node;
1635 fndef_ok = !nested;
1638 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1639 true, true, cla_nonabstract_decl);
1640 if (parser->error)
1642 c_parser_skip_to_end_of_block_or_statement (parser);
1643 return;
1645 if (nested && !specs->declspecs_seen_p)
1647 c_parser_error (parser, "expected declaration specifiers");
1648 c_parser_skip_to_end_of_block_or_statement (parser);
1649 return;
1652 finish_declspecs (specs);
1653 bool auto_type_p = specs->typespec_word == cts_auto_type;
1654 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1656 if (auto_type_p)
1657 error_at (here, "%<__auto_type%> in empty declaration");
1658 else if (specs->typespec_kind == ctsk_none
1659 && attribute_fallthrough_p (specs->attrs))
1661 if (fallthru_attr_p != NULL)
1662 *fallthru_attr_p = true;
1663 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1664 void_type_node, 0);
1665 add_stmt (fn);
1667 else if (empty_ok)
1668 shadow_tag (specs);
1669 else
1671 shadow_tag_warned (specs, 1);
1672 pedwarn (here, 0, "empty declaration");
1674 c_parser_consume_token (parser);
1675 if (oacc_routine_data)
1676 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1677 return;
1680 /* Provide better error recovery. Note that a type name here is usually
1681 better diagnosed as a redeclaration. */
1682 if (empty_ok
1683 && specs->typespec_kind == ctsk_tagdef
1684 && c_parser_next_token_starts_declspecs (parser)
1685 && !c_parser_next_token_is (parser, CPP_NAME))
1687 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1688 parser->error = false;
1689 shadow_tag_warned (specs, 1);
1690 return;
1692 else if (c_dialect_objc () && !auto_type_p)
1694 /* Prefix attributes are an error on method decls. */
1695 switch (c_parser_peek_token (parser)->type)
1697 case CPP_PLUS:
1698 case CPP_MINUS:
1699 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1700 return;
1701 if (specs->attrs)
1703 warning_at (c_parser_peek_token (parser)->location,
1704 OPT_Wattributes,
1705 "prefix attributes are ignored for methods");
1706 specs->attrs = NULL_TREE;
1708 if (fndef_ok)
1709 c_parser_objc_method_definition (parser);
1710 else
1711 c_parser_objc_methodproto (parser);
1712 return;
1713 break;
1714 default:
1715 break;
1717 /* This is where we parse 'attributes @interface ...',
1718 'attributes @implementation ...', 'attributes @protocol ...'
1719 (where attributes could be, for example, __attribute__
1720 ((deprecated)).
1722 switch (c_parser_peek_token (parser)->keyword)
1724 case RID_AT_INTERFACE:
1726 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1727 return;
1728 c_parser_objc_class_definition (parser, specs->attrs);
1729 return;
1731 break;
1732 case RID_AT_IMPLEMENTATION:
1734 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1735 return;
1736 if (specs->attrs)
1738 warning_at (c_parser_peek_token (parser)->location,
1739 OPT_Wattributes,
1740 "prefix attributes are ignored for implementations");
1741 specs->attrs = NULL_TREE;
1743 c_parser_objc_class_definition (parser, NULL_TREE);
1744 return;
1746 break;
1747 case RID_AT_PROTOCOL:
1749 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1750 return;
1751 c_parser_objc_protocol_definition (parser, specs->attrs);
1752 return;
1754 break;
1755 case RID_AT_ALIAS:
1756 case RID_AT_CLASS:
1757 case RID_AT_END:
1758 case RID_AT_PROPERTY:
1759 if (specs->attrs)
1761 c_parser_error (parser, "unexpected attribute");
1762 specs->attrs = NULL;
1764 break;
1765 default:
1766 break;
1769 else if (attribute_fallthrough_p (specs->attrs))
1770 warning_at (here, OPT_Wattributes,
1771 "%<fallthrough%> attribute not followed by %<;%>");
1773 pending_xref_error ();
1774 prefix_attrs = specs->attrs;
1775 all_prefix_attrs = prefix_attrs;
1776 specs->attrs = NULL_TREE;
1777 while (true)
1779 struct c_declarator *declarator;
1780 bool dummy = false;
1781 timevar_id_t tv;
1782 tree fnbody = NULL_TREE;
1783 /* Declaring either one or more declarators (in which case we
1784 should diagnose if there were no declaration specifiers) or a
1785 function definition (in which case the diagnostic for
1786 implicit int suffices). */
1787 declarator = c_parser_declarator (parser,
1788 specs->typespec_kind != ctsk_none,
1789 C_DTR_NORMAL, &dummy);
1790 if (declarator == NULL)
1792 if (omp_declare_simd_clauses.exists ()
1793 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1794 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1795 omp_declare_simd_clauses);
1796 if (oacc_routine_data)
1797 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1798 c_parser_skip_to_end_of_block_or_statement (parser);
1799 return;
1801 if (auto_type_p && declarator->kind != cdk_id)
1803 error_at (here,
1804 "%<__auto_type%> requires a plain identifier"
1805 " as declarator");
1806 c_parser_skip_to_end_of_block_or_statement (parser);
1807 return;
1809 if (c_parser_next_token_is (parser, CPP_EQ)
1810 || c_parser_next_token_is (parser, CPP_COMMA)
1811 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1812 || c_parser_next_token_is_keyword (parser, RID_ASM)
1813 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1814 || c_parser_next_token_is_keyword (parser, RID_IN))
1816 tree asm_name = NULL_TREE;
1817 tree postfix_attrs = NULL_TREE;
1818 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1820 diagnosed_no_specs = true;
1821 pedwarn (here, 0, "data definition has no type or storage class");
1823 /* Having seen a data definition, there cannot now be a
1824 function definition. */
1825 fndef_ok = false;
1826 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1827 asm_name = c_parser_simple_asm_expr (parser);
1828 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1830 postfix_attrs = c_parser_attributes (parser);
1831 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1833 /* This means there is an attribute specifier after
1834 the declarator in a function definition. Provide
1835 some more information for the user. */
1836 error_at (here, "attributes should be specified before the "
1837 "declarator in a function definition");
1838 c_parser_skip_to_end_of_block_or_statement (parser);
1839 return;
1842 if (c_parser_next_token_is (parser, CPP_EQ))
1844 tree d;
1845 struct c_expr init;
1846 location_t init_loc;
1847 c_parser_consume_token (parser);
1848 if (auto_type_p)
1850 init_loc = c_parser_peek_token (parser)->location;
1851 rich_location richloc (line_table, init_loc);
1852 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
1853 init = c_parser_expr_no_commas (parser, NULL);
1854 if (TREE_CODE (init.value) == COMPONENT_REF
1855 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1856 error_at (here,
1857 "%<__auto_type%> used with a bit-field"
1858 " initializer");
1859 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1860 tree init_type = TREE_TYPE (init.value);
1861 /* As with typeof, remove all qualifiers from atomic types. */
1862 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1863 init_type
1864 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1865 bool vm_type = variably_modified_type_p (init_type,
1866 NULL_TREE);
1867 if (vm_type)
1868 init.value = c_save_expr (init.value);
1869 finish_init ();
1870 specs->typespec_kind = ctsk_typeof;
1871 specs->locations[cdw_typedef] = init_loc;
1872 specs->typedef_p = true;
1873 specs->type = init_type;
1874 if (vm_type)
1876 bool maybe_const = true;
1877 tree type_expr = c_fully_fold (init.value, false,
1878 &maybe_const);
1879 specs->expr_const_operands &= maybe_const;
1880 if (specs->expr)
1881 specs->expr = build2 (COMPOUND_EXPR,
1882 TREE_TYPE (type_expr),
1883 specs->expr, type_expr);
1884 else
1885 specs->expr = type_expr;
1887 d = start_decl (declarator, specs, true,
1888 chainon (postfix_attrs, all_prefix_attrs));
1889 if (!d)
1890 d = error_mark_node;
1891 if (omp_declare_simd_clauses.exists ()
1892 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1893 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1894 omp_declare_simd_clauses);
1896 else
1898 /* The declaration of the variable is in effect while
1899 its initializer is parsed. */
1900 d = start_decl (declarator, specs, true,
1901 chainon (postfix_attrs, all_prefix_attrs));
1902 if (!d)
1903 d = error_mark_node;
1904 if (omp_declare_simd_clauses.exists ()
1905 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1906 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1907 omp_declare_simd_clauses);
1908 init_loc = c_parser_peek_token (parser)->location;
1909 rich_location richloc (line_table, init_loc);
1910 start_init (d, asm_name, global_bindings_p (), &richloc);
1911 init = c_parser_initializer (parser);
1912 finish_init ();
1914 if (oacc_routine_data)
1915 c_finish_oacc_routine (oacc_routine_data, d, false);
1916 if (d != error_mark_node)
1918 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1919 finish_decl (d, init_loc, init.value,
1920 init.original_type, asm_name);
1923 else
1925 if (auto_type_p)
1927 error_at (here,
1928 "%<__auto_type%> requires an initialized "
1929 "data declaration");
1930 c_parser_skip_to_end_of_block_or_statement (parser);
1931 return;
1933 tree d = start_decl (declarator, specs, false,
1934 chainon (postfix_attrs,
1935 all_prefix_attrs));
1936 if (omp_declare_simd_clauses.exists ()
1937 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1939 tree parms = NULL_TREE;
1940 if (d && TREE_CODE (d) == FUNCTION_DECL)
1942 struct c_declarator *ce = declarator;
1943 while (ce != NULL)
1944 if (ce->kind == cdk_function)
1946 parms = ce->u.arg_info->parms;
1947 break;
1949 else
1950 ce = ce->declarator;
1952 if (parms)
1953 temp_store_parm_decls (d, parms);
1954 c_finish_omp_declare_simd (parser, d, parms,
1955 omp_declare_simd_clauses);
1956 if (parms)
1957 temp_pop_parm_decls ();
1959 if (oacc_routine_data)
1960 c_finish_oacc_routine (oacc_routine_data, d, false);
1961 if (d)
1962 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1963 NULL_TREE, asm_name);
1965 if (c_parser_next_token_is_keyword (parser, RID_IN))
1967 if (d)
1968 *objc_foreach_object_declaration = d;
1969 else
1970 *objc_foreach_object_declaration = error_mark_node;
1973 if (c_parser_next_token_is (parser, CPP_COMMA))
1975 if (auto_type_p)
1977 error_at (here,
1978 "%<__auto_type%> may only be used with"
1979 " a single declarator");
1980 c_parser_skip_to_end_of_block_or_statement (parser);
1981 return;
1983 c_parser_consume_token (parser);
1984 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1985 all_prefix_attrs = chainon (c_parser_attributes (parser),
1986 prefix_attrs);
1987 else
1988 all_prefix_attrs = prefix_attrs;
1989 continue;
1991 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1993 c_parser_consume_token (parser);
1994 return;
1996 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1998 /* This can only happen in Objective-C: we found the
1999 'in' that terminates the declaration inside an
2000 Objective-C foreach statement. Do not consume the
2001 token, so that the caller can use it to determine
2002 that this indeed is a foreach context. */
2003 return;
2005 else
2007 c_parser_error (parser, "expected %<,%> or %<;%>");
2008 c_parser_skip_to_end_of_block_or_statement (parser);
2009 return;
2012 else if (auto_type_p)
2014 error_at (here,
2015 "%<__auto_type%> requires an initialized data declaration");
2016 c_parser_skip_to_end_of_block_or_statement (parser);
2017 return;
2019 else if (!fndef_ok)
2021 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2022 "%<asm%> or %<__attribute__%>");
2023 c_parser_skip_to_end_of_block_or_statement (parser);
2024 return;
2026 /* Function definition (nested or otherwise). */
2027 if (nested)
2029 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2030 c_push_function_context ();
2032 if (!start_function (specs, declarator, all_prefix_attrs))
2034 /* This can appear in many cases looking nothing like a
2035 function definition, so we don't give a more specific
2036 error suggesting there was one. */
2037 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2038 "or %<__attribute__%>");
2039 if (nested)
2040 c_pop_function_context ();
2041 break;
2044 if (DECL_DECLARED_INLINE_P (current_function_decl))
2045 tv = TV_PARSE_INLINE;
2046 else
2047 tv = TV_PARSE_FUNC;
2048 timevar_push (tv);
2050 /* Parse old-style parameter declarations. ??? Attributes are
2051 not allowed to start declaration specifiers here because of a
2052 syntax conflict between a function declaration with attribute
2053 suffix and a function definition with an attribute prefix on
2054 first old-style parameter declaration. Following the old
2055 parser, they are not accepted on subsequent old-style
2056 parameter declarations either. However, there is no
2057 ambiguity after the first declaration, nor indeed on the
2058 first as long as we don't allow postfix attributes after a
2059 declarator with a nonempty identifier list in a definition;
2060 and postfix attributes have never been accepted here in
2061 function definitions either. */
2062 while (c_parser_next_token_is_not (parser, CPP_EOF)
2063 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2064 c_parser_declaration_or_fndef (parser, false, false, false,
2065 true, false, NULL, vNULL);
2066 store_parm_decls ();
2067 if (omp_declare_simd_clauses.exists ()
2068 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2069 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2070 omp_declare_simd_clauses);
2071 if (oacc_routine_data)
2072 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2073 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2074 = c_parser_peek_token (parser)->location;
2076 /* If the definition was marked with __GIMPLE then parse the
2077 function body as GIMPLE. */
2078 if (specs->gimple_p)
2080 cfun->pass_startwith = specs->gimple_pass;
2081 bool saved = in_late_binary_op;
2082 in_late_binary_op = true;
2083 c_parser_parse_gimple_body (parser);
2084 in_late_binary_op = saved;
2086 else
2088 fnbody = c_parser_compound_statement (parser);
2089 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2090 fnbody = expand_array_notation_exprs (fnbody);
2092 tree fndecl = current_function_decl;
2093 if (nested)
2095 tree decl = current_function_decl;
2096 /* Mark nested functions as needing static-chain initially.
2097 lower_nested_functions will recompute it but the
2098 DECL_STATIC_CHAIN flag is also used before that happens,
2099 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2100 DECL_STATIC_CHAIN (decl) = 1;
2101 add_stmt (fnbody);
2102 finish_function ();
2103 c_pop_function_context ();
2104 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2106 else
2108 if (fnbody)
2109 add_stmt (fnbody);
2110 finish_function ();
2112 /* Get rid of the empty stmt list for GIMPLE. */
2113 if (specs->gimple_p)
2114 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2116 timevar_pop (tv);
2117 break;
2121 /* Parse an asm-definition (asm() outside a function body). This is a
2122 GNU extension.
2124 asm-definition:
2125 simple-asm-expr ;
2128 static void
2129 c_parser_asm_definition (c_parser *parser)
2131 tree asm_str = c_parser_simple_asm_expr (parser);
2132 if (asm_str)
2133 symtab->finalize_toplevel_asm (asm_str);
2134 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2137 /* Parse a static assertion (C11 6.7.10).
2139 static_assert-declaration:
2140 static_assert-declaration-no-semi ;
2143 static void
2144 c_parser_static_assert_declaration (c_parser *parser)
2146 c_parser_static_assert_declaration_no_semi (parser);
2147 if (parser->error
2148 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2149 c_parser_skip_to_end_of_block_or_statement (parser);
2152 /* Parse a static assertion (C11 6.7.10), without the trailing
2153 semicolon.
2155 static_assert-declaration-no-semi:
2156 _Static_assert ( constant-expression , string-literal )
2159 static void
2160 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2162 location_t assert_loc, value_loc;
2163 tree value;
2164 tree string;
2166 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2167 assert_loc = c_parser_peek_token (parser)->location;
2168 if (flag_isoc99)
2169 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2170 "ISO C99 does not support %<_Static_assert%>");
2171 else
2172 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2173 "ISO C90 does not support %<_Static_assert%>");
2174 c_parser_consume_token (parser);
2175 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2176 return;
2177 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2178 value = c_parser_expr_no_commas (parser, NULL).value;
2179 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2180 parser->lex_untranslated_string = true;
2181 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2183 parser->lex_untranslated_string = false;
2184 return;
2186 switch (c_parser_peek_token (parser)->type)
2188 case CPP_STRING:
2189 case CPP_STRING16:
2190 case CPP_STRING32:
2191 case CPP_WSTRING:
2192 case CPP_UTF8STRING:
2193 string = c_parser_peek_token (parser)->value;
2194 c_parser_consume_token (parser);
2195 parser->lex_untranslated_string = false;
2196 break;
2197 default:
2198 c_parser_error (parser, "expected string literal");
2199 parser->lex_untranslated_string = false;
2200 return;
2202 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2204 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2206 error_at (value_loc, "expression in static assertion is not an integer");
2207 return;
2209 if (TREE_CODE (value) != INTEGER_CST)
2211 value = c_fully_fold (value, false, NULL);
2212 /* Strip no-op conversions. */
2213 STRIP_TYPE_NOPS (value);
2214 if (TREE_CODE (value) == INTEGER_CST)
2215 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2216 "is not an integer constant expression");
2218 if (TREE_CODE (value) != INTEGER_CST)
2220 error_at (value_loc, "expression in static assertion is not constant");
2221 return;
2223 constant_expression_warning (value);
2224 if (integer_zerop (value))
2225 error_at (assert_loc, "static assertion failed: %E", string);
2228 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2229 6.7), adding them to SPECS (which may already include some).
2230 Storage class specifiers are accepted iff SCSPEC_OK; type
2231 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2232 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2233 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2235 declaration-specifiers:
2236 storage-class-specifier declaration-specifiers[opt]
2237 type-specifier declaration-specifiers[opt]
2238 type-qualifier declaration-specifiers[opt]
2239 function-specifier declaration-specifiers[opt]
2240 alignment-specifier declaration-specifiers[opt]
2242 Function specifiers (inline) are from C99, and are currently
2243 handled as storage class specifiers, as is __thread. Alignment
2244 specifiers are from C11.
2246 C90 6.5.1, C99 6.7.1:
2247 storage-class-specifier:
2248 typedef
2249 extern
2250 static
2251 auto
2252 register
2253 _Thread_local
2255 (_Thread_local is new in C11.)
2257 C99 6.7.4:
2258 function-specifier:
2259 inline
2260 _Noreturn
2262 (_Noreturn is new in C11.)
2264 C90 6.5.2, C99 6.7.2:
2265 type-specifier:
2266 void
2267 char
2268 short
2270 long
2271 float
2272 double
2273 signed
2274 unsigned
2275 _Bool
2276 _Complex
2277 [_Imaginary removed in C99 TC2]
2278 struct-or-union-specifier
2279 enum-specifier
2280 typedef-name
2281 atomic-type-specifier
2283 (_Bool and _Complex are new in C99.)
2284 (atomic-type-specifier is new in C11.)
2286 C90 6.5.3, C99 6.7.3:
2288 type-qualifier:
2289 const
2290 restrict
2291 volatile
2292 address-space-qualifier
2293 _Atomic
2295 (restrict is new in C99.)
2296 (_Atomic is new in C11.)
2298 GNU extensions:
2300 declaration-specifiers:
2301 attributes declaration-specifiers[opt]
2303 type-qualifier:
2304 address-space
2306 address-space:
2307 identifier recognized by the target
2309 storage-class-specifier:
2310 __thread
2312 type-specifier:
2313 typeof-specifier
2314 __auto_type
2315 __intN
2316 _Decimal32
2317 _Decimal64
2318 _Decimal128
2319 _Fract
2320 _Accum
2321 _Sat
2323 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2324 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2326 atomic-type-specifier
2327 _Atomic ( type-name )
2329 Objective-C:
2331 type-specifier:
2332 class-name objc-protocol-refs[opt]
2333 typedef-name objc-protocol-refs
2334 objc-protocol-refs
2337 void
2338 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2339 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2340 bool alignspec_ok, bool auto_type_ok,
2341 enum c_lookahead_kind la)
2343 bool attrs_ok = start_attr_ok;
2344 bool seen_type = specs->typespec_kind != ctsk_none;
2346 if (!typespec_ok)
2347 gcc_assert (la == cla_prefer_id);
2349 while (c_parser_next_token_is (parser, CPP_NAME)
2350 || c_parser_next_token_is (parser, CPP_KEYWORD)
2351 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2353 struct c_typespec t;
2354 tree attrs;
2355 tree align;
2356 location_t loc = c_parser_peek_token (parser)->location;
2358 /* If we cannot accept a type, exit if the next token must start
2359 one. Also, if we already have seen a tagged definition,
2360 a typename would be an error anyway and likely the user
2361 has simply forgotten a semicolon, so we exit. */
2362 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2363 && c_parser_next_tokens_start_typename (parser, la)
2364 && !c_parser_next_token_is_qualifier (parser))
2365 break;
2367 if (c_parser_next_token_is (parser, CPP_NAME))
2369 c_token *name_token = c_parser_peek_token (parser);
2370 tree value = name_token->value;
2371 c_id_kind kind = name_token->id_kind;
2373 if (kind == C_ID_ADDRSPACE)
2375 addr_space_t as
2376 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2377 declspecs_add_addrspace (name_token->location, specs, as);
2378 c_parser_consume_token (parser);
2379 attrs_ok = true;
2380 continue;
2383 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2385 /* If we cannot accept a type, and the next token must start one,
2386 exit. Do the same if we already have seen a tagged definition,
2387 since it would be an error anyway and likely the user has simply
2388 forgotten a semicolon. */
2389 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2390 break;
2392 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2393 a C_ID_CLASSNAME. */
2394 c_parser_consume_token (parser);
2395 seen_type = true;
2396 attrs_ok = true;
2397 if (kind == C_ID_ID)
2399 error_at (loc, "unknown type name %qE", value);
2400 t.kind = ctsk_typedef;
2401 t.spec = error_mark_node;
2403 else if (kind == C_ID_TYPENAME
2404 && (!c_dialect_objc ()
2405 || c_parser_next_token_is_not (parser, CPP_LESS)))
2407 t.kind = ctsk_typedef;
2408 /* For a typedef name, record the meaning, not the name.
2409 In case of 'foo foo, bar;'. */
2410 t.spec = lookup_name (value);
2412 else
2414 tree proto = NULL_TREE;
2415 gcc_assert (c_dialect_objc ());
2416 t.kind = ctsk_objc;
2417 if (c_parser_next_token_is (parser, CPP_LESS))
2418 proto = c_parser_objc_protocol_refs (parser);
2419 t.spec = objc_get_protocol_qualified_type (value, proto);
2421 t.expr = NULL_TREE;
2422 t.expr_const_operands = true;
2423 declspecs_add_type (name_token->location, specs, t);
2424 continue;
2426 if (c_parser_next_token_is (parser, CPP_LESS))
2428 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2429 nisse@lysator.liu.se. */
2430 tree proto;
2431 gcc_assert (c_dialect_objc ());
2432 if (!typespec_ok || seen_type)
2433 break;
2434 proto = c_parser_objc_protocol_refs (parser);
2435 t.kind = ctsk_objc;
2436 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2437 t.expr = NULL_TREE;
2438 t.expr_const_operands = true;
2439 declspecs_add_type (loc, specs, t);
2440 continue;
2442 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2443 switch (c_parser_peek_token (parser)->keyword)
2445 case RID_STATIC:
2446 case RID_EXTERN:
2447 case RID_REGISTER:
2448 case RID_TYPEDEF:
2449 case RID_INLINE:
2450 case RID_NORETURN:
2451 case RID_AUTO:
2452 case RID_THREAD:
2453 if (!scspec_ok)
2454 goto out;
2455 attrs_ok = true;
2456 /* TODO: Distinguish between function specifiers (inline, noreturn)
2457 and storage class specifiers, either here or in
2458 declspecs_add_scspec. */
2459 declspecs_add_scspec (loc, specs,
2460 c_parser_peek_token (parser)->value);
2461 c_parser_consume_token (parser);
2462 break;
2463 case RID_AUTO_TYPE:
2464 if (!auto_type_ok)
2465 goto out;
2466 /* Fall through. */
2467 case RID_UNSIGNED:
2468 case RID_LONG:
2469 case RID_SHORT:
2470 case RID_SIGNED:
2471 case RID_COMPLEX:
2472 case RID_INT:
2473 case RID_CHAR:
2474 case RID_FLOAT:
2475 case RID_DOUBLE:
2476 case RID_VOID:
2477 case RID_DFLOAT32:
2478 case RID_DFLOAT64:
2479 case RID_DFLOAT128:
2480 CASE_RID_FLOATN_NX:
2481 case RID_BOOL:
2482 case RID_FRACT:
2483 case RID_ACCUM:
2484 case RID_SAT:
2485 case RID_INT_N_0:
2486 case RID_INT_N_1:
2487 case RID_INT_N_2:
2488 case RID_INT_N_3:
2489 if (!typespec_ok)
2490 goto out;
2491 attrs_ok = true;
2492 seen_type = true;
2493 if (c_dialect_objc ())
2494 parser->objc_need_raw_identifier = true;
2495 t.kind = ctsk_resword;
2496 t.spec = c_parser_peek_token (parser)->value;
2497 t.expr = NULL_TREE;
2498 t.expr_const_operands = true;
2499 declspecs_add_type (loc, specs, t);
2500 c_parser_consume_token (parser);
2501 break;
2502 case RID_ENUM:
2503 if (!typespec_ok)
2504 goto out;
2505 attrs_ok = true;
2506 seen_type = true;
2507 t = c_parser_enum_specifier (parser);
2508 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2509 declspecs_add_type (loc, specs, t);
2510 break;
2511 case RID_STRUCT:
2512 case RID_UNION:
2513 if (!typespec_ok)
2514 goto out;
2515 attrs_ok = true;
2516 seen_type = true;
2517 t = c_parser_struct_or_union_specifier (parser);
2518 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2519 declspecs_add_type (loc, specs, t);
2520 break;
2521 case RID_TYPEOF:
2522 /* ??? The old parser rejected typeof after other type
2523 specifiers, but is a syntax error the best way of
2524 handling this? */
2525 if (!typespec_ok || seen_type)
2526 goto out;
2527 attrs_ok = true;
2528 seen_type = true;
2529 t = c_parser_typeof_specifier (parser);
2530 declspecs_add_type (loc, specs, t);
2531 break;
2532 case RID_ATOMIC:
2533 /* C parser handling of Objective-C constructs needs
2534 checking for correct lvalue-to-rvalue conversions, and
2535 the code in build_modify_expr handling various
2536 Objective-C cases, and that in build_unary_op handling
2537 Objective-C cases for increment / decrement, also needs
2538 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2539 and objc_types_are_equivalent may also need updates. */
2540 if (c_dialect_objc ())
2541 sorry ("%<_Atomic%> in Objective-C");
2542 if (flag_isoc99)
2543 pedwarn_c99 (loc, OPT_Wpedantic,
2544 "ISO C99 does not support the %<_Atomic%> qualifier");
2545 else
2546 pedwarn_c99 (loc, OPT_Wpedantic,
2547 "ISO C90 does not support the %<_Atomic%> qualifier");
2548 attrs_ok = true;
2549 tree value;
2550 value = c_parser_peek_token (parser)->value;
2551 c_parser_consume_token (parser);
2552 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2554 /* _Atomic ( type-name ). */
2555 seen_type = true;
2556 c_parser_consume_token (parser);
2557 struct c_type_name *type = c_parser_type_name (parser);
2558 t.kind = ctsk_typeof;
2559 t.spec = error_mark_node;
2560 t.expr = NULL_TREE;
2561 t.expr_const_operands = true;
2562 if (type != NULL)
2563 t.spec = groktypename (type, &t.expr,
2564 &t.expr_const_operands);
2565 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2566 "expected %<)%>");
2567 if (t.spec != error_mark_node)
2569 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2570 error_at (loc, "%<_Atomic%>-qualified array type");
2571 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2572 error_at (loc, "%<_Atomic%>-qualified function type");
2573 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2574 error_at (loc, "%<_Atomic%> applied to a qualified type");
2575 else
2576 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2578 declspecs_add_type (loc, specs, t);
2580 else
2581 declspecs_add_qual (loc, specs, value);
2582 break;
2583 case RID_CONST:
2584 case RID_VOLATILE:
2585 case RID_RESTRICT:
2586 attrs_ok = true;
2587 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2588 c_parser_consume_token (parser);
2589 break;
2590 case RID_ATTRIBUTE:
2591 if (!attrs_ok)
2592 goto out;
2593 attrs = c_parser_attributes (parser);
2594 declspecs_add_attrs (loc, specs, attrs);
2595 break;
2596 case RID_ALIGNAS:
2597 if (!alignspec_ok)
2598 goto out;
2599 align = c_parser_alignas_specifier (parser);
2600 declspecs_add_alignas (loc, specs, align);
2601 break;
2602 case RID_GIMPLE:
2603 if (! flag_gimple)
2604 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2605 c_parser_consume_token (parser);
2606 specs->gimple_p = true;
2607 specs->locations[cdw_gimple] = loc;
2608 specs->gimple_pass = c_parser_gimple_pass_list (parser);
2609 break;
2610 default:
2611 goto out;
2614 out: ;
2617 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2619 enum-specifier:
2620 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2621 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2622 enum attributes[opt] identifier
2624 The form with trailing comma is new in C99. The forms with
2625 attributes are GNU extensions. In GNU C, we accept any expression
2626 without commas in the syntax (assignment expressions, not just
2627 conditional expressions); assignment expressions will be diagnosed
2628 as non-constant.
2630 enumerator-list:
2631 enumerator
2632 enumerator-list , enumerator
2634 enumerator:
2635 enumeration-constant
2636 enumeration-constant = constant-expression
2638 GNU Extensions:
2640 enumerator:
2641 enumeration-constant attributes[opt]
2642 enumeration-constant attributes[opt] = constant-expression
2646 static struct c_typespec
2647 c_parser_enum_specifier (c_parser *parser)
2649 struct c_typespec ret;
2650 tree attrs;
2651 tree ident = NULL_TREE;
2652 location_t enum_loc;
2653 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2654 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2655 enum_loc = c_parser_peek_token (parser)->location;
2656 c_parser_consume_token (parser);
2657 attrs = c_parser_attributes (parser);
2658 enum_loc = c_parser_peek_token (parser)->location;
2659 /* Set the location in case we create a decl now. */
2660 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2661 if (c_parser_next_token_is (parser, CPP_NAME))
2663 ident = c_parser_peek_token (parser)->value;
2664 ident_loc = c_parser_peek_token (parser)->location;
2665 enum_loc = ident_loc;
2666 c_parser_consume_token (parser);
2668 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2670 /* Parse an enum definition. */
2671 struct c_enum_contents the_enum;
2672 tree type;
2673 tree postfix_attrs;
2674 /* We chain the enumerators in reverse order, then put them in
2675 forward order at the end. */
2676 tree values;
2677 timevar_push (TV_PARSE_ENUM);
2678 type = start_enum (enum_loc, &the_enum, ident);
2679 values = NULL_TREE;
2680 c_parser_consume_token (parser);
2681 while (true)
2683 tree enum_id;
2684 tree enum_value;
2685 tree enum_decl;
2686 bool seen_comma;
2687 c_token *token;
2688 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2689 location_t decl_loc, value_loc;
2690 if (c_parser_next_token_is_not (parser, CPP_NAME))
2692 /* Give a nicer error for "enum {}". */
2693 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2694 && !parser->error)
2696 error_at (c_parser_peek_token (parser)->location,
2697 "empty enum is invalid");
2698 parser->error = true;
2700 else
2701 c_parser_error (parser, "expected identifier");
2702 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2703 values = error_mark_node;
2704 break;
2706 token = c_parser_peek_token (parser);
2707 enum_id = token->value;
2708 /* Set the location in case we create a decl now. */
2709 c_parser_set_source_position_from_token (token);
2710 decl_loc = value_loc = token->location;
2711 c_parser_consume_token (parser);
2712 /* Parse any specified attributes. */
2713 tree enum_attrs = c_parser_attributes (parser);
2714 if (c_parser_next_token_is (parser, CPP_EQ))
2716 c_parser_consume_token (parser);
2717 value_loc = c_parser_peek_token (parser)->location;
2718 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2720 else
2721 enum_value = NULL_TREE;
2722 enum_decl = build_enumerator (decl_loc, value_loc,
2723 &the_enum, enum_id, enum_value);
2724 if (enum_attrs)
2725 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2726 TREE_CHAIN (enum_decl) = values;
2727 values = enum_decl;
2728 seen_comma = false;
2729 if (c_parser_next_token_is (parser, CPP_COMMA))
2731 comma_loc = c_parser_peek_token (parser)->location;
2732 seen_comma = true;
2733 c_parser_consume_token (parser);
2735 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2737 if (seen_comma)
2738 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2739 "comma at end of enumerator list");
2740 c_parser_consume_token (parser);
2741 break;
2743 if (!seen_comma)
2745 c_parser_error (parser, "expected %<,%> or %<}%>");
2746 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2747 values = error_mark_node;
2748 break;
2751 postfix_attrs = c_parser_attributes (parser);
2752 ret.spec = finish_enum (type, nreverse (values),
2753 chainon (attrs, postfix_attrs));
2754 ret.kind = ctsk_tagdef;
2755 ret.expr = NULL_TREE;
2756 ret.expr_const_operands = true;
2757 timevar_pop (TV_PARSE_ENUM);
2758 return ret;
2760 else if (!ident)
2762 c_parser_error (parser, "expected %<{%>");
2763 ret.spec = error_mark_node;
2764 ret.kind = ctsk_tagref;
2765 ret.expr = NULL_TREE;
2766 ret.expr_const_operands = true;
2767 return ret;
2769 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2770 /* In ISO C, enumerated types can be referred to only if already
2771 defined. */
2772 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2774 gcc_assert (ident);
2775 pedwarn (enum_loc, OPT_Wpedantic,
2776 "ISO C forbids forward references to %<enum%> types");
2778 return ret;
2781 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2783 struct-or-union-specifier:
2784 struct-or-union attributes[opt] identifier[opt]
2785 { struct-contents } attributes[opt]
2786 struct-or-union attributes[opt] identifier
2788 struct-contents:
2789 struct-declaration-list
2791 struct-declaration-list:
2792 struct-declaration ;
2793 struct-declaration-list struct-declaration ;
2795 GNU extensions:
2797 struct-contents:
2798 empty
2799 struct-declaration
2800 struct-declaration-list struct-declaration
2802 struct-declaration-list:
2803 struct-declaration-list ;
2806 (Note that in the syntax here, unlike that in ISO C, the semicolons
2807 are included here rather than in struct-declaration, in order to
2808 describe the syntax with extra semicolons and missing semicolon at
2809 end.)
2811 Objective-C:
2813 struct-declaration-list:
2814 @defs ( class-name )
2816 (Note this does not include a trailing semicolon, but can be
2817 followed by further declarations, and gets a pedwarn-if-pedantic
2818 when followed by a semicolon.) */
2820 static struct c_typespec
2821 c_parser_struct_or_union_specifier (c_parser *parser)
2823 struct c_typespec ret;
2824 tree attrs;
2825 tree ident = NULL_TREE;
2826 location_t struct_loc;
2827 location_t ident_loc = UNKNOWN_LOCATION;
2828 enum tree_code code;
2829 switch (c_parser_peek_token (parser)->keyword)
2831 case RID_STRUCT:
2832 code = RECORD_TYPE;
2833 break;
2834 case RID_UNION:
2835 code = UNION_TYPE;
2836 break;
2837 default:
2838 gcc_unreachable ();
2840 struct_loc = c_parser_peek_token (parser)->location;
2841 c_parser_consume_token (parser);
2842 attrs = c_parser_attributes (parser);
2844 /* Set the location in case we create a decl now. */
2845 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2847 if (c_parser_next_token_is (parser, CPP_NAME))
2849 ident = c_parser_peek_token (parser)->value;
2850 ident_loc = c_parser_peek_token (parser)->location;
2851 struct_loc = ident_loc;
2852 c_parser_consume_token (parser);
2854 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2856 /* Parse a struct or union definition. Start the scope of the
2857 tag before parsing components. */
2858 struct c_struct_parse_info *struct_info;
2859 tree type = start_struct (struct_loc, code, ident, &struct_info);
2860 tree postfix_attrs;
2861 /* We chain the components in reverse order, then put them in
2862 forward order at the end. Each struct-declaration may
2863 declare multiple components (comma-separated), so we must use
2864 chainon to join them, although when parsing each
2865 struct-declaration we can use TREE_CHAIN directly.
2867 The theory behind all this is that there will be more
2868 semicolon separated fields than comma separated fields, and
2869 so we'll be minimizing the number of node traversals required
2870 by chainon. */
2871 tree contents;
2872 timevar_push (TV_PARSE_STRUCT);
2873 contents = NULL_TREE;
2874 c_parser_consume_token (parser);
2875 /* Handle the Objective-C @defs construct,
2876 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2877 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2879 tree name;
2880 gcc_assert (c_dialect_objc ());
2881 c_parser_consume_token (parser);
2882 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2883 goto end_at_defs;
2884 if (c_parser_next_token_is (parser, CPP_NAME)
2885 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2887 name = c_parser_peek_token (parser)->value;
2888 c_parser_consume_token (parser);
2890 else
2892 c_parser_error (parser, "expected class name");
2893 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2894 goto end_at_defs;
2896 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2897 "expected %<)%>");
2898 contents = nreverse (objc_get_class_ivars (name));
2900 end_at_defs:
2901 /* Parse the struct-declarations and semicolons. Problems with
2902 semicolons are diagnosed here; empty structures are diagnosed
2903 elsewhere. */
2904 while (true)
2906 tree decls;
2907 /* Parse any stray semicolon. */
2908 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2910 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2911 "extra semicolon in struct or union specified");
2912 c_parser_consume_token (parser);
2913 continue;
2915 /* Stop if at the end of the struct or union contents. */
2916 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2918 c_parser_consume_token (parser);
2919 break;
2921 /* Accept #pragmas at struct scope. */
2922 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2924 c_parser_pragma (parser, pragma_struct, NULL);
2925 continue;
2927 /* Parse some comma-separated declarations, but not the
2928 trailing semicolon if any. */
2929 decls = c_parser_struct_declaration (parser);
2930 contents = chainon (decls, contents);
2931 /* If no semicolon follows, either we have a parse error or
2932 are at the end of the struct or union and should
2933 pedwarn. */
2934 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2935 c_parser_consume_token (parser);
2936 else
2938 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2939 pedwarn (c_parser_peek_token (parser)->location, 0,
2940 "no semicolon at end of struct or union");
2941 else if (parser->error
2942 || !c_parser_next_token_starts_declspecs (parser))
2944 c_parser_error (parser, "expected %<;%>");
2945 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2946 break;
2949 /* If we come here, we have already emitted an error
2950 for an expected `;', identifier or `(', and we also
2951 recovered already. Go on with the next field. */
2954 postfix_attrs = c_parser_attributes (parser);
2955 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2956 chainon (attrs, postfix_attrs), struct_info);
2957 ret.kind = ctsk_tagdef;
2958 ret.expr = NULL_TREE;
2959 ret.expr_const_operands = true;
2960 timevar_pop (TV_PARSE_STRUCT);
2961 return ret;
2963 else if (!ident)
2965 c_parser_error (parser, "expected %<{%>");
2966 ret.spec = error_mark_node;
2967 ret.kind = ctsk_tagref;
2968 ret.expr = NULL_TREE;
2969 ret.expr_const_operands = true;
2970 return ret;
2972 ret = parser_xref_tag (ident_loc, code, ident);
2973 return ret;
2976 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2977 the trailing semicolon.
2979 struct-declaration:
2980 specifier-qualifier-list struct-declarator-list
2981 static_assert-declaration-no-semi
2983 specifier-qualifier-list:
2984 type-specifier specifier-qualifier-list[opt]
2985 type-qualifier specifier-qualifier-list[opt]
2986 attributes specifier-qualifier-list[opt]
2988 struct-declarator-list:
2989 struct-declarator
2990 struct-declarator-list , attributes[opt] struct-declarator
2992 struct-declarator:
2993 declarator attributes[opt]
2994 declarator[opt] : constant-expression attributes[opt]
2996 GNU extensions:
2998 struct-declaration:
2999 __extension__ struct-declaration
3000 specifier-qualifier-list
3002 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3003 of attributes where shown is a GNU extension. In GNU C, we accept
3004 any expression without commas in the syntax (assignment
3005 expressions, not just conditional expressions); assignment
3006 expressions will be diagnosed as non-constant. */
3008 static tree
3009 c_parser_struct_declaration (c_parser *parser)
3011 struct c_declspecs *specs;
3012 tree prefix_attrs;
3013 tree all_prefix_attrs;
3014 tree decls;
3015 location_t decl_loc;
3016 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3018 int ext;
3019 tree decl;
3020 ext = disable_extension_diagnostics ();
3021 c_parser_consume_token (parser);
3022 decl = c_parser_struct_declaration (parser);
3023 restore_extension_diagnostics (ext);
3024 return decl;
3026 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3028 c_parser_static_assert_declaration_no_semi (parser);
3029 return NULL_TREE;
3031 specs = build_null_declspecs ();
3032 decl_loc = c_parser_peek_token (parser)->location;
3033 /* Strictly by the standard, we shouldn't allow _Alignas here,
3034 but it appears to have been intended to allow it there, so
3035 we're keeping it as it is until WG14 reaches a conclusion
3036 of N1731.
3037 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3038 c_parser_declspecs (parser, specs, false, true, true,
3039 true, false, cla_nonabstract_decl);
3040 if (parser->error)
3041 return NULL_TREE;
3042 if (!specs->declspecs_seen_p)
3044 c_parser_error (parser, "expected specifier-qualifier-list");
3045 return NULL_TREE;
3047 finish_declspecs (specs);
3048 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3049 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3051 tree ret;
3052 if (specs->typespec_kind == ctsk_none)
3054 pedwarn (decl_loc, OPT_Wpedantic,
3055 "ISO C forbids member declarations with no members");
3056 shadow_tag_warned (specs, pedantic);
3057 ret = NULL_TREE;
3059 else
3061 /* Support for unnamed structs or unions as members of
3062 structs or unions (which is [a] useful and [b] supports
3063 MS P-SDK). */
3064 tree attrs = NULL;
3066 ret = grokfield (c_parser_peek_token (parser)->location,
3067 build_id_declarator (NULL_TREE), specs,
3068 NULL_TREE, &attrs);
3069 if (ret)
3070 decl_attributes (&ret, attrs, 0);
3072 return ret;
3075 /* Provide better error recovery. Note that a type name here is valid,
3076 and will be treated as a field name. */
3077 if (specs->typespec_kind == ctsk_tagdef
3078 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3079 && c_parser_next_token_starts_declspecs (parser)
3080 && !c_parser_next_token_is (parser, CPP_NAME))
3082 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3083 parser->error = false;
3084 return NULL_TREE;
3087 pending_xref_error ();
3088 prefix_attrs = specs->attrs;
3089 all_prefix_attrs = prefix_attrs;
3090 specs->attrs = NULL_TREE;
3091 decls = NULL_TREE;
3092 while (true)
3094 /* Declaring one or more declarators or un-named bit-fields. */
3095 struct c_declarator *declarator;
3096 bool dummy = false;
3097 if (c_parser_next_token_is (parser, CPP_COLON))
3098 declarator = build_id_declarator (NULL_TREE);
3099 else
3100 declarator = c_parser_declarator (parser,
3101 specs->typespec_kind != ctsk_none,
3102 C_DTR_NORMAL, &dummy);
3103 if (declarator == NULL)
3105 c_parser_skip_to_end_of_block_or_statement (parser);
3106 break;
3108 if (c_parser_next_token_is (parser, CPP_COLON)
3109 || c_parser_next_token_is (parser, CPP_COMMA)
3110 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3111 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3112 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3114 tree postfix_attrs = NULL_TREE;
3115 tree width = NULL_TREE;
3116 tree d;
3117 if (c_parser_next_token_is (parser, CPP_COLON))
3119 c_parser_consume_token (parser);
3120 width = c_parser_expr_no_commas (parser, NULL).value;
3122 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3123 postfix_attrs = c_parser_attributes (parser);
3124 d = grokfield (c_parser_peek_token (parser)->location,
3125 declarator, specs, width, &all_prefix_attrs);
3126 decl_attributes (&d, chainon (postfix_attrs,
3127 all_prefix_attrs), 0);
3128 DECL_CHAIN (d) = decls;
3129 decls = d;
3130 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3131 all_prefix_attrs = chainon (c_parser_attributes (parser),
3132 prefix_attrs);
3133 else
3134 all_prefix_attrs = prefix_attrs;
3135 if (c_parser_next_token_is (parser, CPP_COMMA))
3136 c_parser_consume_token (parser);
3137 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3138 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3140 /* Semicolon consumed in caller. */
3141 break;
3143 else
3145 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3146 break;
3149 else
3151 c_parser_error (parser,
3152 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3153 "%<__attribute__%>");
3154 break;
3157 return decls;
3160 /* Parse a typeof specifier (a GNU extension).
3162 typeof-specifier:
3163 typeof ( expression )
3164 typeof ( type-name )
3167 static struct c_typespec
3168 c_parser_typeof_specifier (c_parser *parser)
3170 struct c_typespec ret;
3171 ret.kind = ctsk_typeof;
3172 ret.spec = error_mark_node;
3173 ret.expr = NULL_TREE;
3174 ret.expr_const_operands = true;
3175 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3176 c_parser_consume_token (parser);
3177 c_inhibit_evaluation_warnings++;
3178 in_typeof++;
3179 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3181 c_inhibit_evaluation_warnings--;
3182 in_typeof--;
3183 return ret;
3185 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3187 struct c_type_name *type = c_parser_type_name (parser);
3188 c_inhibit_evaluation_warnings--;
3189 in_typeof--;
3190 if (type != NULL)
3192 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3193 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3196 else
3198 bool was_vm;
3199 location_t here = c_parser_peek_token (parser)->location;
3200 struct c_expr expr = c_parser_expression (parser);
3201 c_inhibit_evaluation_warnings--;
3202 in_typeof--;
3203 if (TREE_CODE (expr.value) == COMPONENT_REF
3204 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3205 error_at (here, "%<typeof%> applied to a bit-field");
3206 mark_exp_read (expr.value);
3207 ret.spec = TREE_TYPE (expr.value);
3208 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3209 /* This is returned with the type so that when the type is
3210 evaluated, this can be evaluated. */
3211 if (was_vm)
3212 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3213 pop_maybe_used (was_vm);
3214 /* For use in macros such as those in <stdatomic.h>, remove all
3215 qualifiers from atomic types. (const can be an issue for more macros
3216 using typeof than just the <stdatomic.h> ones.) */
3217 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3218 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3220 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3221 return ret;
3224 /* Parse an alignment-specifier.
3226 C11 6.7.5:
3228 alignment-specifier:
3229 _Alignas ( type-name )
3230 _Alignas ( constant-expression )
3233 static tree
3234 c_parser_alignas_specifier (c_parser * parser)
3236 tree ret = error_mark_node;
3237 location_t loc = c_parser_peek_token (parser)->location;
3238 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3239 c_parser_consume_token (parser);
3240 if (flag_isoc99)
3241 pedwarn_c99 (loc, OPT_Wpedantic,
3242 "ISO C99 does not support %<_Alignas%>");
3243 else
3244 pedwarn_c99 (loc, OPT_Wpedantic,
3245 "ISO C90 does not support %<_Alignas%>");
3246 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3247 return ret;
3248 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3250 struct c_type_name *type = c_parser_type_name (parser);
3251 if (type != NULL)
3252 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3253 false, true, 1);
3255 else
3256 ret = c_parser_expr_no_commas (parser, NULL).value;
3257 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3258 return ret;
3261 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3262 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3263 be redeclared; otherwise it may not. KIND indicates which kind of
3264 declarator is wanted. Returns a valid declarator except in the
3265 case of a syntax error in which case NULL is returned. *SEEN_ID is
3266 set to true if an identifier being declared is seen; this is used
3267 to diagnose bad forms of abstract array declarators and to
3268 determine whether an identifier list is syntactically permitted.
3270 declarator:
3271 pointer[opt] direct-declarator
3273 direct-declarator:
3274 identifier
3275 ( attributes[opt] declarator )
3276 direct-declarator array-declarator
3277 direct-declarator ( parameter-type-list )
3278 direct-declarator ( identifier-list[opt] )
3280 pointer:
3281 * type-qualifier-list[opt]
3282 * type-qualifier-list[opt] pointer
3284 type-qualifier-list:
3285 type-qualifier
3286 attributes
3287 type-qualifier-list type-qualifier
3288 type-qualifier-list attributes
3290 array-declarator:
3291 [ type-qualifier-list[opt] assignment-expression[opt] ]
3292 [ static type-qualifier-list[opt] assignment-expression ]
3293 [ type-qualifier-list static assignment-expression ]
3294 [ type-qualifier-list[opt] * ]
3296 parameter-type-list:
3297 parameter-list
3298 parameter-list , ...
3300 parameter-list:
3301 parameter-declaration
3302 parameter-list , parameter-declaration
3304 parameter-declaration:
3305 declaration-specifiers declarator attributes[opt]
3306 declaration-specifiers abstract-declarator[opt] attributes[opt]
3308 identifier-list:
3309 identifier
3310 identifier-list , identifier
3312 abstract-declarator:
3313 pointer
3314 pointer[opt] direct-abstract-declarator
3316 direct-abstract-declarator:
3317 ( attributes[opt] abstract-declarator )
3318 direct-abstract-declarator[opt] array-declarator
3319 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3321 GNU extensions:
3323 direct-declarator:
3324 direct-declarator ( parameter-forward-declarations
3325 parameter-type-list[opt] )
3327 direct-abstract-declarator:
3328 direct-abstract-declarator[opt] ( parameter-forward-declarations
3329 parameter-type-list[opt] )
3331 parameter-forward-declarations:
3332 parameter-list ;
3333 parameter-forward-declarations parameter-list ;
3335 The uses of attributes shown above are GNU extensions.
3337 Some forms of array declarator are not included in C99 in the
3338 syntax for abstract declarators; these are disallowed elsewhere.
3339 This may be a defect (DR#289).
3341 This function also accepts an omitted abstract declarator as being
3342 an abstract declarator, although not part of the formal syntax. */
3344 struct c_declarator *
3345 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3346 bool *seen_id)
3348 /* Parse any initial pointer part. */
3349 if (c_parser_next_token_is (parser, CPP_MULT))
3351 struct c_declspecs *quals_attrs = build_null_declspecs ();
3352 struct c_declarator *inner;
3353 c_parser_consume_token (parser);
3354 c_parser_declspecs (parser, quals_attrs, false, false, true,
3355 false, false, cla_prefer_id);
3356 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3357 if (inner == NULL)
3358 return NULL;
3359 else
3360 return make_pointer_declarator (quals_attrs, inner);
3362 /* Now we have a direct declarator, direct abstract declarator or
3363 nothing (which counts as a direct abstract declarator here). */
3364 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3367 /* Parse a direct declarator or direct abstract declarator; arguments
3368 as c_parser_declarator. */
3370 static struct c_declarator *
3371 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3372 bool *seen_id)
3374 /* The direct declarator must start with an identifier (possibly
3375 omitted) or a parenthesized declarator (possibly abstract). In
3376 an ordinary declarator, initial parentheses must start a
3377 parenthesized declarator. In an abstract declarator or parameter
3378 declarator, they could start a parenthesized declarator or a
3379 parameter list. To tell which, the open parenthesis and any
3380 following attributes must be read. If a declaration specifier
3381 follows, then it is a parameter list; if the specifier is a
3382 typedef name, there might be an ambiguity about redeclaring it,
3383 which is resolved in the direction of treating it as a typedef
3384 name. If a close parenthesis follows, it is also an empty
3385 parameter list, as the syntax does not permit empty abstract
3386 declarators. Otherwise, it is a parenthesized declarator (in
3387 which case the analysis may be repeated inside it, recursively).
3389 ??? There is an ambiguity in a parameter declaration "int
3390 (__attribute__((foo)) x)", where x is not a typedef name: it
3391 could be an abstract declarator for a function, or declare x with
3392 parentheses. The proper resolution of this ambiguity needs
3393 documenting. At present we follow an accident of the old
3394 parser's implementation, whereby the first parameter must have
3395 some declaration specifiers other than just attributes. Thus as
3396 a parameter declaration it is treated as a parenthesized
3397 parameter named x, and as an abstract declarator it is
3398 rejected.
3400 ??? Also following the old parser, attributes inside an empty
3401 parameter list are ignored, making it a list not yielding a
3402 prototype, rather than giving an error or making it have one
3403 parameter with implicit type int.
3405 ??? Also following the old parser, typedef names may be
3406 redeclared in declarators, but not Objective-C class names. */
3408 if (kind != C_DTR_ABSTRACT
3409 && c_parser_next_token_is (parser, CPP_NAME)
3410 && ((type_seen_p
3411 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3412 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3413 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3415 struct c_declarator *inner
3416 = build_id_declarator (c_parser_peek_token (parser)->value);
3417 *seen_id = true;
3418 inner->id_loc = c_parser_peek_token (parser)->location;
3419 c_parser_consume_token (parser);
3420 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3423 if (kind != C_DTR_NORMAL
3424 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3426 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3427 inner->id_loc = c_parser_peek_token (parser)->location;
3428 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3431 /* Either we are at the end of an abstract declarator, or we have
3432 parentheses. */
3434 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3436 tree attrs;
3437 struct c_declarator *inner;
3438 c_parser_consume_token (parser);
3439 attrs = c_parser_attributes (parser);
3440 if (kind != C_DTR_NORMAL
3441 && (c_parser_next_token_starts_declspecs (parser)
3442 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3444 struct c_arg_info *args
3445 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3446 attrs);
3447 if (args == NULL)
3448 return NULL;
3449 else
3451 inner
3452 = build_function_declarator (args,
3453 build_id_declarator (NULL_TREE));
3454 return c_parser_direct_declarator_inner (parser, *seen_id,
3455 inner);
3458 /* A parenthesized declarator. */
3459 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3460 if (inner != NULL && attrs != NULL)
3461 inner = build_attrs_declarator (attrs, inner);
3462 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3464 c_parser_consume_token (parser);
3465 if (inner == NULL)
3466 return NULL;
3467 else
3468 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3470 else
3472 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3473 "expected %<)%>");
3474 return NULL;
3477 else
3479 if (kind == C_DTR_NORMAL)
3481 c_parser_error (parser, "expected identifier or %<(%>");
3482 return NULL;
3484 else
3485 return build_id_declarator (NULL_TREE);
3489 /* Parse part of a direct declarator or direct abstract declarator,
3490 given that some (in INNER) has already been parsed; ID_PRESENT is
3491 true if an identifier is present, false for an abstract
3492 declarator. */
3494 static struct c_declarator *
3495 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3496 struct c_declarator *inner)
3498 /* Parse a sequence of array declarators and parameter lists. */
3499 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3501 location_t brace_loc = c_parser_peek_token (parser)->location;
3502 struct c_declarator *declarator;
3503 struct c_declspecs *quals_attrs = build_null_declspecs ();
3504 bool static_seen;
3505 bool star_seen;
3506 struct c_expr dimen;
3507 dimen.value = NULL_TREE;
3508 dimen.original_code = ERROR_MARK;
3509 dimen.original_type = NULL_TREE;
3510 c_parser_consume_token (parser);
3511 c_parser_declspecs (parser, quals_attrs, false, false, true,
3512 false, false, cla_prefer_id);
3513 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3514 if (static_seen)
3515 c_parser_consume_token (parser);
3516 if (static_seen && !quals_attrs->declspecs_seen_p)
3517 c_parser_declspecs (parser, quals_attrs, false, false, true,
3518 false, false, cla_prefer_id);
3519 if (!quals_attrs->declspecs_seen_p)
3520 quals_attrs = NULL;
3521 /* If "static" is present, there must be an array dimension.
3522 Otherwise, there may be a dimension, "*", or no
3523 dimension. */
3524 if (static_seen)
3526 star_seen = false;
3527 dimen = c_parser_expr_no_commas (parser, NULL);
3529 else
3531 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3533 dimen.value = NULL_TREE;
3534 star_seen = false;
3536 else if (flag_cilkplus
3537 && c_parser_next_token_is (parser, CPP_COLON))
3539 dimen.value = error_mark_node;
3540 star_seen = false;
3541 error_at (c_parser_peek_token (parser)->location,
3542 "array notations cannot be used in declaration");
3543 c_parser_consume_token (parser);
3545 else if (c_parser_next_token_is (parser, CPP_MULT))
3547 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3549 dimen.value = NULL_TREE;
3550 star_seen = true;
3551 c_parser_consume_token (parser);
3553 else
3555 star_seen = false;
3556 dimen = c_parser_expr_no_commas (parser, NULL);
3559 else
3561 star_seen = false;
3562 dimen = c_parser_expr_no_commas (parser, NULL);
3565 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3566 c_parser_consume_token (parser);
3567 else if (flag_cilkplus
3568 && c_parser_next_token_is (parser, CPP_COLON))
3570 error_at (c_parser_peek_token (parser)->location,
3571 "array notations cannot be used in declaration");
3572 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3573 return NULL;
3575 else
3577 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3578 "expected %<]%>");
3579 return NULL;
3581 if (dimen.value)
3582 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3583 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3584 static_seen, star_seen);
3585 if (declarator == NULL)
3586 return NULL;
3587 inner = set_array_declarator_inner (declarator, inner);
3588 return c_parser_direct_declarator_inner (parser, id_present, inner);
3590 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3592 tree attrs;
3593 struct c_arg_info *args;
3594 c_parser_consume_token (parser);
3595 attrs = c_parser_attributes (parser);
3596 args = c_parser_parms_declarator (parser, id_present, attrs);
3597 if (args == NULL)
3598 return NULL;
3599 else
3601 inner = build_function_declarator (args, inner);
3602 return c_parser_direct_declarator_inner (parser, id_present, inner);
3605 return inner;
3608 /* Parse a parameter list or identifier list, including the closing
3609 parenthesis but not the opening one. ATTRS are the attributes at
3610 the start of the list. ID_LIST_OK is true if an identifier list is
3611 acceptable; such a list must not have attributes at the start. */
3613 static struct c_arg_info *
3614 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3616 push_scope ();
3617 declare_parm_level ();
3618 /* If the list starts with an identifier, it is an identifier list.
3619 Otherwise, it is either a prototype list or an empty list. */
3620 if (id_list_ok
3621 && !attrs
3622 && c_parser_next_token_is (parser, CPP_NAME)
3623 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3625 /* Look ahead to detect typos in type names. */
3626 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3627 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3628 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3629 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3630 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3632 tree list = NULL_TREE, *nextp = &list;
3633 while (c_parser_next_token_is (parser, CPP_NAME)
3634 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3636 *nextp = build_tree_list (NULL_TREE,
3637 c_parser_peek_token (parser)->value);
3638 nextp = & TREE_CHAIN (*nextp);
3639 c_parser_consume_token (parser);
3640 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3641 break;
3642 c_parser_consume_token (parser);
3643 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3645 c_parser_error (parser, "expected identifier");
3646 break;
3649 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3651 struct c_arg_info *ret = build_arg_info ();
3652 ret->types = list;
3653 c_parser_consume_token (parser);
3654 pop_scope ();
3655 return ret;
3657 else
3659 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3660 "expected %<)%>");
3661 pop_scope ();
3662 return NULL;
3665 else
3667 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3668 NULL);
3669 pop_scope ();
3670 return ret;
3674 /* Parse a parameter list (possibly empty), including the closing
3675 parenthesis but not the opening one. ATTRS are the attributes at
3676 the start of the list. EXPR is NULL or an expression that needs to
3677 be evaluated for the side effects of array size expressions in the
3678 parameters. */
3680 static struct c_arg_info *
3681 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3683 bool bad_parm = false;
3685 /* ??? Following the old parser, forward parameter declarations may
3686 use abstract declarators, and if no real parameter declarations
3687 follow the forward declarations then this is not diagnosed. Also
3688 note as above that attributes are ignored as the only contents of
3689 the parentheses, or as the only contents after forward
3690 declarations. */
3691 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3693 struct c_arg_info *ret = build_arg_info ();
3694 c_parser_consume_token (parser);
3695 return ret;
3697 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3699 struct c_arg_info *ret = build_arg_info ();
3701 if (flag_allow_parameterless_variadic_functions)
3703 /* F (...) is allowed. */
3704 ret->types = NULL_TREE;
3706 else
3708 /* Suppress -Wold-style-definition for this case. */
3709 ret->types = error_mark_node;
3710 error_at (c_parser_peek_token (parser)->location,
3711 "ISO C requires a named argument before %<...%>");
3713 c_parser_consume_token (parser);
3714 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3716 c_parser_consume_token (parser);
3717 return ret;
3719 else
3721 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3722 "expected %<)%>");
3723 return NULL;
3726 /* Nonempty list of parameters, either terminated with semicolon
3727 (forward declarations; recurse) or with close parenthesis (normal
3728 function) or with ", ... )" (variadic function). */
3729 while (true)
3731 /* Parse a parameter. */
3732 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3733 attrs = NULL_TREE;
3734 if (parm == NULL)
3735 bad_parm = true;
3736 else
3737 push_parm_decl (parm, &expr);
3738 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3740 tree new_attrs;
3741 c_parser_consume_token (parser);
3742 mark_forward_parm_decls ();
3743 new_attrs = c_parser_attributes (parser);
3744 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3746 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3748 c_parser_consume_token (parser);
3749 if (bad_parm)
3750 return NULL;
3751 else
3752 return get_parm_info (false, expr);
3754 if (!c_parser_require (parser, CPP_COMMA,
3755 "expected %<;%>, %<,%> or %<)%>"))
3757 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3758 return NULL;
3760 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3762 c_parser_consume_token (parser);
3763 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3765 c_parser_consume_token (parser);
3766 if (bad_parm)
3767 return NULL;
3768 else
3769 return get_parm_info (true, expr);
3771 else
3773 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3774 "expected %<)%>");
3775 return NULL;
3781 /* Parse a parameter declaration. ATTRS are the attributes at the
3782 start of the declaration if it is the first parameter. */
3784 static struct c_parm *
3785 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3787 struct c_declspecs *specs;
3788 struct c_declarator *declarator;
3789 tree prefix_attrs;
3790 tree postfix_attrs = NULL_TREE;
3791 bool dummy = false;
3793 /* Accept #pragmas between parameter declarations. */
3794 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3795 c_parser_pragma (parser, pragma_param, NULL);
3797 if (!c_parser_next_token_starts_declspecs (parser))
3799 c_token *token = c_parser_peek_token (parser);
3800 if (parser->error)
3801 return NULL;
3802 c_parser_set_source_position_from_token (token);
3803 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3805 const char *hint = lookup_name_fuzzy (token->value,
3806 FUZZY_LOOKUP_TYPENAME);
3807 if (hint)
3809 gcc_rich_location richloc (token->location);
3810 richloc.add_fixit_replace (hint);
3811 error_at_rich_loc (&richloc,
3812 "unknown type name %qE; did you mean %qs?",
3813 token->value, hint);
3815 else
3816 error_at (token->location, "unknown type name %qE", token->value);
3817 parser->error = true;
3819 /* ??? In some Objective-C cases '...' isn't applicable so there
3820 should be a different message. */
3821 else
3822 c_parser_error (parser,
3823 "expected declaration specifiers or %<...%>");
3824 c_parser_skip_to_end_of_parameter (parser);
3825 return NULL;
3827 specs = build_null_declspecs ();
3828 if (attrs)
3830 declspecs_add_attrs (input_location, specs, attrs);
3831 attrs = NULL_TREE;
3833 c_parser_declspecs (parser, specs, true, true, true, true, false,
3834 cla_nonabstract_decl);
3835 finish_declspecs (specs);
3836 pending_xref_error ();
3837 prefix_attrs = specs->attrs;
3838 specs->attrs = NULL_TREE;
3839 declarator = c_parser_declarator (parser,
3840 specs->typespec_kind != ctsk_none,
3841 C_DTR_PARM, &dummy);
3842 if (declarator == NULL)
3844 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3845 return NULL;
3847 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3848 postfix_attrs = c_parser_attributes (parser);
3849 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3850 declarator);
3853 /* Parse a string literal in an asm expression. It should not be
3854 translated, and wide string literals are an error although
3855 permitted by the syntax. This is a GNU extension.
3857 asm-string-literal:
3858 string-literal
3860 ??? At present, following the old parser, the caller needs to have
3861 set lex_untranslated_string to 1. It would be better to follow the
3862 C++ parser rather than using this kludge. */
3864 static tree
3865 c_parser_asm_string_literal (c_parser *parser)
3867 tree str;
3868 int save_flag = warn_overlength_strings;
3869 warn_overlength_strings = 0;
3870 if (c_parser_next_token_is (parser, CPP_STRING))
3872 str = c_parser_peek_token (parser)->value;
3873 c_parser_consume_token (parser);
3875 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3877 error_at (c_parser_peek_token (parser)->location,
3878 "wide string literal in %<asm%>");
3879 str = build_string (1, "");
3880 c_parser_consume_token (parser);
3882 else
3884 c_parser_error (parser, "expected string literal");
3885 str = NULL_TREE;
3887 warn_overlength_strings = save_flag;
3888 return str;
3891 /* Parse a simple asm expression. This is used in restricted
3892 contexts, where a full expression with inputs and outputs does not
3893 make sense. This is a GNU extension.
3895 simple-asm-expr:
3896 asm ( asm-string-literal )
3899 static tree
3900 c_parser_simple_asm_expr (c_parser *parser)
3902 tree str;
3903 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3904 /* ??? Follow the C++ parser rather than using the
3905 lex_untranslated_string kludge. */
3906 parser->lex_untranslated_string = true;
3907 c_parser_consume_token (parser);
3908 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3910 parser->lex_untranslated_string = false;
3911 return NULL_TREE;
3913 str = c_parser_asm_string_literal (parser);
3914 parser->lex_untranslated_string = false;
3915 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3917 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3918 return NULL_TREE;
3920 return str;
3923 static tree
3924 c_parser_attribute_any_word (c_parser *parser)
3926 tree attr_name = NULL_TREE;
3928 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3930 /* ??? See comment above about what keywords are accepted here. */
3931 bool ok;
3932 switch (c_parser_peek_token (parser)->keyword)
3934 case RID_STATIC:
3935 case RID_UNSIGNED:
3936 case RID_LONG:
3937 case RID_CONST:
3938 case RID_EXTERN:
3939 case RID_REGISTER:
3940 case RID_TYPEDEF:
3941 case RID_SHORT:
3942 case RID_INLINE:
3943 case RID_NORETURN:
3944 case RID_VOLATILE:
3945 case RID_SIGNED:
3946 case RID_AUTO:
3947 case RID_RESTRICT:
3948 case RID_COMPLEX:
3949 case RID_THREAD:
3950 case RID_INT:
3951 case RID_CHAR:
3952 case RID_FLOAT:
3953 case RID_DOUBLE:
3954 case RID_VOID:
3955 case RID_DFLOAT32:
3956 case RID_DFLOAT64:
3957 case RID_DFLOAT128:
3958 CASE_RID_FLOATN_NX:
3959 case RID_BOOL:
3960 case RID_FRACT:
3961 case RID_ACCUM:
3962 case RID_SAT:
3963 case RID_TRANSACTION_ATOMIC:
3964 case RID_TRANSACTION_CANCEL:
3965 case RID_ATOMIC:
3966 case RID_AUTO_TYPE:
3967 case RID_INT_N_0:
3968 case RID_INT_N_1:
3969 case RID_INT_N_2:
3970 case RID_INT_N_3:
3971 ok = true;
3972 break;
3973 default:
3974 ok = false;
3975 break;
3977 if (!ok)
3978 return NULL_TREE;
3980 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3981 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3983 else if (c_parser_next_token_is (parser, CPP_NAME))
3984 attr_name = c_parser_peek_token (parser)->value;
3986 return attr_name;
3989 #define CILK_SIMD_FN_CLAUSE_MASK \
3990 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3991 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3992 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3993 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3994 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3996 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3997 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3998 pushed into the token list.
3999 Syntax:
4000 vector
4001 vector (<vector attributes>). */
4003 static void
4004 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
4006 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4008 int paren_scope = 0;
4009 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4010 /* Consume the "vector" token. */
4011 c_parser_consume_token (parser);
4013 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4015 c_parser_consume_token (parser);
4016 paren_scope++;
4018 while (paren_scope > 0)
4020 c_token *token = c_parser_peek_token (parser);
4021 if (token->type == CPP_OPEN_PAREN)
4022 paren_scope++;
4023 else if (token->type == CPP_CLOSE_PAREN)
4024 paren_scope--;
4025 /* Do not push the last ')' since we are not pushing the '('. */
4026 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4027 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4028 c_parser_consume_token (parser);
4031 /* Since we are converting an attribute to a pragma, we need to end the
4032 attribute with PRAGMA_EOL. */
4033 c_token eol_token;
4034 memset (&eol_token, 0, sizeof (eol_token));
4035 eol_token.type = CPP_PRAGMA_EOL;
4036 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4039 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4041 static void
4042 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4044 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4046 /* c_parser_attributes is called in several places, so if these EOF
4047 tokens are already inserted, then don't do them again. */
4048 if (last_token.type == CPP_EOF)
4049 return;
4051 /* Two CPP_EOF token are added as a safety net since the normal C
4052 front-end has two token look-ahead. */
4053 c_token eof_token;
4054 eof_token.type = CPP_EOF;
4055 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4056 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4059 /* Parse (possibly empty) attributes. This is a GNU extension.
4061 attributes:
4062 empty
4063 attributes attribute
4065 attribute:
4066 __attribute__ ( ( attribute-list ) )
4068 attribute-list:
4069 attrib
4070 attribute_list , attrib
4072 attrib:
4073 empty
4074 any-word
4075 any-word ( identifier )
4076 any-word ( identifier , nonempty-expr-list )
4077 any-word ( expr-list )
4079 where the "identifier" must not be declared as a type, and
4080 "any-word" may be any identifier (including one declared as a
4081 type), a reserved word storage class specifier, type specifier or
4082 type qualifier. ??? This still leaves out most reserved keywords
4083 (following the old parser), shouldn't we include them, and why not
4084 allow identifiers declared as types to start the arguments? */
4086 static tree
4087 c_parser_attributes (c_parser *parser)
4089 tree attrs = NULL_TREE;
4090 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4092 /* ??? Follow the C++ parser rather than using the
4093 lex_untranslated_string kludge. */
4094 parser->lex_untranslated_string = true;
4095 /* Consume the `__attribute__' keyword. */
4096 c_parser_consume_token (parser);
4097 /* Look for the two `(' tokens. */
4098 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4100 parser->lex_untranslated_string = false;
4101 return attrs;
4103 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4105 parser->lex_untranslated_string = false;
4106 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4107 return attrs;
4109 /* Parse the attribute list. */
4110 while (c_parser_next_token_is (parser, CPP_COMMA)
4111 || c_parser_next_token_is (parser, CPP_NAME)
4112 || c_parser_next_token_is (parser, CPP_KEYWORD))
4114 tree attr, attr_name, attr_args;
4115 vec<tree, va_gc> *expr_list;
4116 if (c_parser_next_token_is (parser, CPP_COMMA))
4118 c_parser_consume_token (parser);
4119 continue;
4122 attr_name = c_parser_attribute_any_word (parser);
4123 if (attr_name == NULL)
4124 break;
4125 if (is_cilkplus_vector_p (attr_name))
4127 c_token *v_token = c_parser_peek_token (parser);
4128 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4129 /* If the next token isn't a comma, we're done. */
4130 if (!c_parser_next_token_is (parser, CPP_COMMA))
4131 break;
4132 continue;
4134 c_parser_consume_token (parser);
4135 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4137 attr = build_tree_list (attr_name, NULL_TREE);
4138 /* Add this attribute to the list. */
4139 attrs = chainon (attrs, attr);
4140 /* If the next token isn't a comma, we're done. */
4141 if (!c_parser_next_token_is (parser, CPP_COMMA))
4142 break;
4143 continue;
4145 c_parser_consume_token (parser);
4146 /* Parse the attribute contents. If they start with an
4147 identifier which is followed by a comma or close
4148 parenthesis, then the arguments start with that
4149 identifier; otherwise they are an expression list.
4150 In objective-c the identifier may be a classname. */
4151 if (c_parser_next_token_is (parser, CPP_NAME)
4152 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4153 || (c_dialect_objc ()
4154 && c_parser_peek_token (parser)->id_kind
4155 == C_ID_CLASSNAME))
4156 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4157 || (c_parser_peek_2nd_token (parser)->type
4158 == CPP_CLOSE_PAREN))
4159 && (attribute_takes_identifier_p (attr_name)
4160 || (c_dialect_objc ()
4161 && c_parser_peek_token (parser)->id_kind
4162 == C_ID_CLASSNAME)))
4164 tree arg1 = c_parser_peek_token (parser)->value;
4165 c_parser_consume_token (parser);
4166 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4167 attr_args = build_tree_list (NULL_TREE, arg1);
4168 else
4170 tree tree_list;
4171 c_parser_consume_token (parser);
4172 expr_list = c_parser_expr_list (parser, false, true,
4173 NULL, NULL, NULL, NULL);
4174 tree_list = build_tree_list_vec (expr_list);
4175 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4176 release_tree_vector (expr_list);
4179 else
4181 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4182 attr_args = NULL_TREE;
4183 else
4185 expr_list = c_parser_expr_list (parser, false, true,
4186 NULL, NULL, NULL, NULL);
4187 attr_args = build_tree_list_vec (expr_list);
4188 release_tree_vector (expr_list);
4191 attr = build_tree_list (attr_name, attr_args);
4192 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4193 c_parser_consume_token (parser);
4194 else
4196 parser->lex_untranslated_string = false;
4197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4198 "expected %<)%>");
4199 return attrs;
4201 /* Add this attribute to the list. */
4202 attrs = chainon (attrs, attr);
4203 /* If the next token isn't a comma, we're done. */
4204 if (!c_parser_next_token_is (parser, CPP_COMMA))
4205 break;
4207 /* Look for the two `)' tokens. */
4208 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4209 c_parser_consume_token (parser);
4210 else
4212 parser->lex_untranslated_string = false;
4213 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4214 "expected %<)%>");
4215 return attrs;
4217 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4218 c_parser_consume_token (parser);
4219 else
4221 parser->lex_untranslated_string = false;
4222 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4223 "expected %<)%>");
4224 return attrs;
4226 parser->lex_untranslated_string = false;
4229 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4230 c_finish_cilk_simd_fn_tokens (parser);
4231 return attrs;
4234 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4236 type-name:
4237 specifier-qualifier-list abstract-declarator[opt]
4240 struct c_type_name *
4241 c_parser_type_name (c_parser *parser)
4243 struct c_declspecs *specs = build_null_declspecs ();
4244 struct c_declarator *declarator;
4245 struct c_type_name *ret;
4246 bool dummy = false;
4247 c_parser_declspecs (parser, specs, false, true, true, false, false,
4248 cla_prefer_type);
4249 if (!specs->declspecs_seen_p)
4251 c_parser_error (parser, "expected specifier-qualifier-list");
4252 return NULL;
4254 if (specs->type != error_mark_node)
4256 pending_xref_error ();
4257 finish_declspecs (specs);
4259 declarator = c_parser_declarator (parser,
4260 specs->typespec_kind != ctsk_none,
4261 C_DTR_ABSTRACT, &dummy);
4262 if (declarator == NULL)
4263 return NULL;
4264 ret = XOBNEW (&parser_obstack, struct c_type_name);
4265 ret->specs = specs;
4266 ret->declarator = declarator;
4267 return ret;
4270 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4272 initializer:
4273 assignment-expression
4274 { initializer-list }
4275 { initializer-list , }
4277 initializer-list:
4278 designation[opt] initializer
4279 initializer-list , designation[opt] initializer
4281 designation:
4282 designator-list =
4284 designator-list:
4285 designator
4286 designator-list designator
4288 designator:
4289 array-designator
4290 . identifier
4292 array-designator:
4293 [ constant-expression ]
4295 GNU extensions:
4297 initializer:
4300 designation:
4301 array-designator
4302 identifier :
4304 array-designator:
4305 [ constant-expression ... constant-expression ]
4307 Any expression without commas is accepted in the syntax for the
4308 constant-expressions, with non-constant expressions rejected later.
4310 This function is only used for top-level initializers; for nested
4311 ones, see c_parser_initval. */
4313 static struct c_expr
4314 c_parser_initializer (c_parser *parser)
4316 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4317 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4318 else
4320 struct c_expr ret;
4321 location_t loc = c_parser_peek_token (parser)->location;
4322 ret = c_parser_expr_no_commas (parser, NULL);
4323 if (TREE_CODE (ret.value) != STRING_CST
4324 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4325 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4326 return ret;
4330 /* The location of the last comma within the current initializer list,
4331 or UNKNOWN_LOCATION if not within one. */
4333 location_t last_init_list_comma;
4335 /* Parse a braced initializer list. TYPE is the type specified for a
4336 compound literal, and NULL_TREE for other initializers and for
4337 nested braced lists. NESTED_P is true for nested braced lists,
4338 false for the list of a compound literal or the list that is the
4339 top-level initializer in a declaration. */
4341 static struct c_expr
4342 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4343 struct obstack *outer_obstack)
4345 struct c_expr ret;
4346 struct obstack braced_init_obstack;
4347 location_t brace_loc = c_parser_peek_token (parser)->location;
4348 gcc_obstack_init (&braced_init_obstack);
4349 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4350 c_parser_consume_token (parser);
4351 if (nested_p)
4353 finish_implicit_inits (brace_loc, outer_obstack);
4354 push_init_level (brace_loc, 0, &braced_init_obstack);
4356 else
4357 really_start_incremental_init (type);
4358 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4360 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4362 else
4364 /* Parse a non-empty initializer list, possibly with a trailing
4365 comma. */
4366 while (true)
4368 c_parser_initelt (parser, &braced_init_obstack);
4369 if (parser->error)
4370 break;
4371 if (c_parser_next_token_is (parser, CPP_COMMA))
4373 last_init_list_comma = c_parser_peek_token (parser)->location;
4374 c_parser_consume_token (parser);
4376 else
4377 break;
4378 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4379 break;
4382 c_token *next_tok = c_parser_peek_token (parser);
4383 if (next_tok->type != CPP_CLOSE_BRACE)
4385 ret.value = error_mark_node;
4386 ret.original_code = ERROR_MARK;
4387 ret.original_type = NULL;
4388 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4389 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4390 obstack_free (&braced_init_obstack, NULL);
4391 return ret;
4393 location_t close_loc = next_tok->location;
4394 c_parser_consume_token (parser);
4395 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4396 obstack_free (&braced_init_obstack, NULL);
4397 set_c_expr_source_range (&ret, brace_loc, close_loc);
4398 return ret;
4401 /* Parse a nested initializer, including designators. */
4403 static void
4404 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4406 /* Parse any designator or designator list. A single array
4407 designator may have the subsequent "=" omitted in GNU C, but a
4408 longer list or a structure member designator may not. */
4409 if (c_parser_next_token_is (parser, CPP_NAME)
4410 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4412 /* Old-style structure member designator. */
4413 set_init_label (c_parser_peek_token (parser)->location,
4414 c_parser_peek_token (parser)->value,
4415 c_parser_peek_token (parser)->location,
4416 braced_init_obstack);
4417 /* Use the colon as the error location. */
4418 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4419 "obsolete use of designated initializer with %<:%>");
4420 c_parser_consume_token (parser);
4421 c_parser_consume_token (parser);
4423 else
4425 /* des_seen is 0 if there have been no designators, 1 if there
4426 has been a single array designator and 2 otherwise. */
4427 int des_seen = 0;
4428 /* Location of a designator. */
4429 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4430 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4431 || c_parser_next_token_is (parser, CPP_DOT))
4433 int des_prev = des_seen;
4434 if (!des_seen)
4435 des_loc = c_parser_peek_token (parser)->location;
4436 if (des_seen < 2)
4437 des_seen++;
4438 if (c_parser_next_token_is (parser, CPP_DOT))
4440 des_seen = 2;
4441 c_parser_consume_token (parser);
4442 if (c_parser_next_token_is (parser, CPP_NAME))
4444 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4445 c_parser_peek_token (parser)->location,
4446 braced_init_obstack);
4447 c_parser_consume_token (parser);
4449 else
4451 struct c_expr init;
4452 init.value = error_mark_node;
4453 init.original_code = ERROR_MARK;
4454 init.original_type = NULL;
4455 c_parser_error (parser, "expected identifier");
4456 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4457 process_init_element (input_location, init, false,
4458 braced_init_obstack);
4459 return;
4462 else
4464 tree first, second;
4465 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4466 location_t array_index_loc = UNKNOWN_LOCATION;
4467 /* ??? Following the old parser, [ objc-receiver
4468 objc-message-args ] is accepted as an initializer,
4469 being distinguished from a designator by what follows
4470 the first assignment expression inside the square
4471 brackets, but after a first array designator a
4472 subsequent square bracket is for Objective-C taken to
4473 start an expression, using the obsolete form of
4474 designated initializer without '=', rather than
4475 possibly being a second level of designation: in LALR
4476 terms, the '[' is shifted rather than reducing
4477 designator to designator-list. */
4478 if (des_prev == 1 && c_dialect_objc ())
4480 des_seen = des_prev;
4481 break;
4483 if (des_prev == 0 && c_dialect_objc ())
4485 /* This might be an array designator or an
4486 Objective-C message expression. If the former,
4487 continue parsing here; if the latter, parse the
4488 remainder of the initializer given the starting
4489 primary-expression. ??? It might make sense to
4490 distinguish when des_prev == 1 as well; see
4491 previous comment. */
4492 tree rec, args;
4493 struct c_expr mexpr;
4494 c_parser_consume_token (parser);
4495 if (c_parser_peek_token (parser)->type == CPP_NAME
4496 && ((c_parser_peek_token (parser)->id_kind
4497 == C_ID_TYPENAME)
4498 || (c_parser_peek_token (parser)->id_kind
4499 == C_ID_CLASSNAME)))
4501 /* Type name receiver. */
4502 tree id = c_parser_peek_token (parser)->value;
4503 c_parser_consume_token (parser);
4504 rec = objc_get_class_reference (id);
4505 goto parse_message_args;
4507 first = c_parser_expr_no_commas (parser, NULL).value;
4508 mark_exp_read (first);
4509 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4510 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4511 goto array_desig_after_first;
4512 /* Expression receiver. So far only one part
4513 without commas has been parsed; there might be
4514 more of the expression. */
4515 rec = first;
4516 while (c_parser_next_token_is (parser, CPP_COMMA))
4518 struct c_expr next;
4519 location_t comma_loc, exp_loc;
4520 comma_loc = c_parser_peek_token (parser)->location;
4521 c_parser_consume_token (parser);
4522 exp_loc = c_parser_peek_token (parser)->location;
4523 next = c_parser_expr_no_commas (parser, NULL);
4524 next = convert_lvalue_to_rvalue (exp_loc, next,
4525 true, true);
4526 rec = build_compound_expr (comma_loc, rec, next.value);
4528 parse_message_args:
4529 /* Now parse the objc-message-args. */
4530 args = c_parser_objc_message_args (parser);
4531 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4532 "expected %<]%>");
4533 mexpr.value
4534 = objc_build_message_expr (rec, args);
4535 mexpr.original_code = ERROR_MARK;
4536 mexpr.original_type = NULL;
4537 /* Now parse and process the remainder of the
4538 initializer, starting with this message
4539 expression as a primary-expression. */
4540 c_parser_initval (parser, &mexpr, braced_init_obstack);
4541 return;
4543 c_parser_consume_token (parser);
4544 array_index_loc = c_parser_peek_token (parser)->location;
4545 first = c_parser_expr_no_commas (parser, NULL).value;
4546 mark_exp_read (first);
4547 array_desig_after_first:
4548 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4550 ellipsis_loc = c_parser_peek_token (parser)->location;
4551 c_parser_consume_token (parser);
4552 second = c_parser_expr_no_commas (parser, NULL).value;
4553 mark_exp_read (second);
4555 else
4556 second = NULL_TREE;
4557 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4559 c_parser_consume_token (parser);
4560 set_init_index (array_index_loc, first, second,
4561 braced_init_obstack);
4562 if (second)
4563 pedwarn (ellipsis_loc, OPT_Wpedantic,
4564 "ISO C forbids specifying range of elements to initialize");
4566 else
4567 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4568 "expected %<]%>");
4571 if (des_seen >= 1)
4573 if (c_parser_next_token_is (parser, CPP_EQ))
4575 pedwarn_c90 (des_loc, OPT_Wpedantic,
4576 "ISO C90 forbids specifying subobject "
4577 "to initialize");
4578 c_parser_consume_token (parser);
4580 else
4582 if (des_seen == 1)
4583 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4584 "obsolete use of designated initializer without %<=%>");
4585 else
4587 struct c_expr init;
4588 init.value = error_mark_node;
4589 init.original_code = ERROR_MARK;
4590 init.original_type = NULL;
4591 c_parser_error (parser, "expected %<=%>");
4592 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4593 process_init_element (input_location, init, false,
4594 braced_init_obstack);
4595 return;
4600 c_parser_initval (parser, NULL, braced_init_obstack);
4603 /* Parse a nested initializer; as c_parser_initializer but parses
4604 initializers within braced lists, after any designators have been
4605 applied. If AFTER is not NULL then it is an Objective-C message
4606 expression which is the primary-expression starting the
4607 initializer. */
4609 static void
4610 c_parser_initval (c_parser *parser, struct c_expr *after,
4611 struct obstack * braced_init_obstack)
4613 struct c_expr init;
4614 gcc_assert (!after || c_dialect_objc ());
4615 location_t loc = c_parser_peek_token (parser)->location;
4617 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4618 init = c_parser_braced_init (parser, NULL_TREE, true,
4619 braced_init_obstack);
4620 else
4622 init = c_parser_expr_no_commas (parser, after);
4623 if (init.value != NULL_TREE
4624 && TREE_CODE (init.value) != STRING_CST
4625 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4626 init = convert_lvalue_to_rvalue (loc, init, true, true);
4628 process_init_element (loc, init, false, braced_init_obstack);
4631 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4632 C99 6.8.2).
4634 compound-statement:
4635 { block-item-list[opt] }
4636 { label-declarations block-item-list }
4638 block-item-list:
4639 block-item
4640 block-item-list block-item
4642 block-item:
4643 nested-declaration
4644 statement
4646 nested-declaration:
4647 declaration
4649 GNU extensions:
4651 compound-statement:
4652 { label-declarations block-item-list }
4654 nested-declaration:
4655 __extension__ nested-declaration
4656 nested-function-definition
4658 label-declarations:
4659 label-declaration
4660 label-declarations label-declaration
4662 label-declaration:
4663 __label__ identifier-list ;
4665 Allowing the mixing of declarations and code is new in C99. The
4666 GNU syntax also permits (not shown above) labels at the end of
4667 compound statements, which yield an error. We don't allow labels
4668 on declarations; this might seem like a natural extension, but
4669 there would be a conflict between attributes on the label and
4670 prefix attributes on the declaration. ??? The syntax follows the
4671 old parser in requiring something after label declarations.
4672 Although they are erroneous if the labels declared aren't defined,
4673 is it useful for the syntax to be this way?
4675 OpenACC:
4677 block-item:
4678 openacc-directive
4680 openacc-directive:
4681 update-directive
4683 OpenMP:
4685 block-item:
4686 openmp-directive
4688 openmp-directive:
4689 barrier-directive
4690 flush-directive
4691 taskwait-directive
4692 taskyield-directive
4693 cancel-directive
4694 cancellation-point-directive */
4696 static tree
4697 c_parser_compound_statement (c_parser *parser)
4699 tree stmt;
4700 location_t brace_loc;
4701 brace_loc = c_parser_peek_token (parser)->location;
4702 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4704 /* Ensure a scope is entered and left anyway to avoid confusion
4705 if we have just prepared to enter a function body. */
4706 stmt = c_begin_compound_stmt (true);
4707 c_end_compound_stmt (brace_loc, stmt, true);
4708 return error_mark_node;
4710 stmt = c_begin_compound_stmt (true);
4711 c_parser_compound_statement_nostart (parser);
4713 /* If the compound stmt contains array notations, then we expand them. */
4714 if (flag_cilkplus && contains_array_notation_expr (stmt))
4715 stmt = expand_array_notation_exprs (stmt);
4716 return c_end_compound_stmt (brace_loc, stmt, true);
4719 /* Parse a compound statement except for the opening brace. This is
4720 used for parsing both compound statements and statement expressions
4721 (which follow different paths to handling the opening). */
4723 static void
4724 c_parser_compound_statement_nostart (c_parser *parser)
4726 bool last_stmt = false;
4727 bool last_label = false;
4728 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4729 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4730 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4732 c_parser_consume_token (parser);
4733 return;
4735 mark_valid_location_for_stdc_pragma (true);
4736 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4738 /* Read zero or more forward-declarations for labels that nested
4739 functions can jump to. */
4740 mark_valid_location_for_stdc_pragma (false);
4741 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4743 label_loc = c_parser_peek_token (parser)->location;
4744 c_parser_consume_token (parser);
4745 /* Any identifiers, including those declared as type names,
4746 are OK here. */
4747 while (true)
4749 tree label;
4750 if (c_parser_next_token_is_not (parser, CPP_NAME))
4752 c_parser_error (parser, "expected identifier");
4753 break;
4755 label
4756 = declare_label (c_parser_peek_token (parser)->value);
4757 C_DECLARED_LABEL_FLAG (label) = 1;
4758 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4759 c_parser_consume_token (parser);
4760 if (c_parser_next_token_is (parser, CPP_COMMA))
4761 c_parser_consume_token (parser);
4762 else
4763 break;
4765 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4767 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4769 /* We must now have at least one statement, label or declaration. */
4770 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4772 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4773 c_parser_error (parser, "expected declaration or statement");
4774 c_parser_consume_token (parser);
4775 return;
4777 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4779 location_t loc = c_parser_peek_token (parser)->location;
4780 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4781 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4782 || (c_parser_next_token_is (parser, CPP_NAME)
4783 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4785 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4786 label_loc = c_parser_peek_2nd_token (parser)->location;
4787 else
4788 label_loc = c_parser_peek_token (parser)->location;
4789 last_label = true;
4790 last_stmt = false;
4791 mark_valid_location_for_stdc_pragma (false);
4792 c_parser_label (parser);
4794 else if (!last_label
4795 && c_parser_next_tokens_start_declaration (parser))
4797 last_label = false;
4798 mark_valid_location_for_stdc_pragma (false);
4799 bool fallthru_attr_p = false;
4800 c_parser_declaration_or_fndef (parser, true, true, true, true,
4801 true, NULL, vNULL, NULL,
4802 &fallthru_attr_p);
4803 if (last_stmt && !fallthru_attr_p)
4804 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4805 "ISO C90 forbids mixed declarations and code");
4806 last_stmt = fallthru_attr_p;
4808 else if (!last_label
4809 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4811 /* __extension__ can start a declaration, but is also an
4812 unary operator that can start an expression. Consume all
4813 but the last of a possible series of __extension__ to
4814 determine which. */
4815 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4816 && (c_parser_peek_2nd_token (parser)->keyword
4817 == RID_EXTENSION))
4818 c_parser_consume_token (parser);
4819 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4821 int ext;
4822 ext = disable_extension_diagnostics ();
4823 c_parser_consume_token (parser);
4824 last_label = false;
4825 mark_valid_location_for_stdc_pragma (false);
4826 c_parser_declaration_or_fndef (parser, true, true, true, true,
4827 true, NULL, vNULL);
4828 /* Following the old parser, __extension__ does not
4829 disable this diagnostic. */
4830 restore_extension_diagnostics (ext);
4831 if (last_stmt)
4832 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4833 "ISO C90 forbids mixed declarations and code");
4834 last_stmt = false;
4836 else
4837 goto statement;
4839 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4841 /* External pragmas, and some omp pragmas, are not associated
4842 with regular c code, and so are not to be considered statements
4843 syntactically. This ensures that the user doesn't put them
4844 places that would turn into syntax errors if the directive
4845 were ignored. */
4846 if (c_parser_pragma (parser,
4847 last_label ? pragma_stmt : pragma_compound,
4848 NULL))
4849 last_label = false, last_stmt = true;
4851 else if (c_parser_next_token_is (parser, CPP_EOF))
4853 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4854 c_parser_error (parser, "expected declaration or statement");
4855 return;
4857 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4859 if (parser->in_if_block)
4861 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4862 error_at (loc, """expected %<}%> before %<else%>");
4863 return;
4865 else
4867 error_at (loc, "%<else%> without a previous %<if%>");
4868 c_parser_consume_token (parser);
4869 continue;
4872 else
4874 statement:
4875 last_label = false;
4876 last_stmt = true;
4877 mark_valid_location_for_stdc_pragma (false);
4878 c_parser_statement_after_labels (parser, NULL);
4881 parser->error = false;
4883 if (last_label)
4884 error_at (label_loc, "label at end of compound statement");
4885 c_parser_consume_token (parser);
4886 /* Restore the value we started with. */
4887 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4890 /* Parse all consecutive labels. */
4892 static void
4893 c_parser_all_labels (c_parser *parser)
4895 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4896 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4897 || (c_parser_next_token_is (parser, CPP_NAME)
4898 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4899 c_parser_label (parser);
4902 /* Parse a label (C90 6.6.1, C99 6.8.1).
4904 label:
4905 identifier : attributes[opt]
4906 case constant-expression :
4907 default :
4909 GNU extensions:
4911 label:
4912 case constant-expression ... constant-expression :
4914 The use of attributes on labels is a GNU extension. The syntax in
4915 GNU C accepts any expressions without commas, non-constant
4916 expressions being rejected later. */
4918 static void
4919 c_parser_label (c_parser *parser)
4921 location_t loc1 = c_parser_peek_token (parser)->location;
4922 tree label = NULL_TREE;
4924 /* Remember whether this case or a user-defined label is allowed to fall
4925 through to. */
4926 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
4928 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4930 tree exp1, exp2;
4931 c_parser_consume_token (parser);
4932 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4933 if (c_parser_next_token_is (parser, CPP_COLON))
4935 c_parser_consume_token (parser);
4936 label = do_case (loc1, exp1, NULL_TREE);
4938 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4940 c_parser_consume_token (parser);
4941 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4942 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4943 label = do_case (loc1, exp1, exp2);
4945 else
4946 c_parser_error (parser, "expected %<:%> or %<...%>");
4948 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4950 c_parser_consume_token (parser);
4951 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4952 label = do_case (loc1, NULL_TREE, NULL_TREE);
4954 else
4956 tree name = c_parser_peek_token (parser)->value;
4957 tree tlab;
4958 tree attrs;
4959 location_t loc2 = c_parser_peek_token (parser)->location;
4960 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4961 c_parser_consume_token (parser);
4962 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4963 c_parser_consume_token (parser);
4964 attrs = c_parser_attributes (parser);
4965 tlab = define_label (loc2, name);
4966 if (tlab)
4968 decl_attributes (&tlab, attrs, 0);
4969 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4972 if (label)
4974 if (TREE_CODE (label) == LABEL_EXPR)
4975 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
4976 else
4977 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
4979 /* Allow '__attribute__((fallthrough));'. */
4980 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4982 location_t loc = c_parser_peek_token (parser)->location;
4983 tree attrs = c_parser_attributes (parser);
4984 if (attribute_fallthrough_p (attrs))
4986 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4988 tree fn = build_call_expr_internal_loc (loc,
4989 IFN_FALLTHROUGH,
4990 void_type_node, 0);
4991 add_stmt (fn);
4993 else
4994 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
4995 "not followed by %<;%>");
4997 else if (attrs != NULL_TREE)
4998 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
4999 " can be applied to a null statement");
5001 if (c_parser_next_tokens_start_declaration (parser))
5003 error_at (c_parser_peek_token (parser)->location,
5004 "a label can only be part of a statement and "
5005 "a declaration is not a statement");
5006 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5007 /*static_assert_ok*/ true,
5008 /*empty_ok*/ true, /*nested*/ true,
5009 /*start_attr_ok*/ true, NULL,
5010 vNULL);
5015 /* Parse a statement (C90 6.6, C99 6.8).
5017 statement:
5018 labeled-statement
5019 compound-statement
5020 expression-statement
5021 selection-statement
5022 iteration-statement
5023 jump-statement
5025 labeled-statement:
5026 label statement
5028 expression-statement:
5029 expression[opt] ;
5031 selection-statement:
5032 if-statement
5033 switch-statement
5035 iteration-statement:
5036 while-statement
5037 do-statement
5038 for-statement
5040 jump-statement:
5041 goto identifier ;
5042 continue ;
5043 break ;
5044 return expression[opt] ;
5046 GNU extensions:
5048 statement:
5049 asm-statement
5051 jump-statement:
5052 goto * expression ;
5054 expression-statement:
5055 attributes ;
5057 Objective-C:
5059 statement:
5060 objc-throw-statement
5061 objc-try-catch-statement
5062 objc-synchronized-statement
5064 objc-throw-statement:
5065 @throw expression ;
5066 @throw ;
5068 OpenACC:
5070 statement:
5071 openacc-construct
5073 openacc-construct:
5074 parallel-construct
5075 kernels-construct
5076 data-construct
5077 loop-construct
5079 parallel-construct:
5080 parallel-directive structured-block
5082 kernels-construct:
5083 kernels-directive structured-block
5085 data-construct:
5086 data-directive structured-block
5088 loop-construct:
5089 loop-directive structured-block
5091 OpenMP:
5093 statement:
5094 openmp-construct
5096 openmp-construct:
5097 parallel-construct
5098 for-construct
5099 simd-construct
5100 for-simd-construct
5101 sections-construct
5102 single-construct
5103 parallel-for-construct
5104 parallel-for-simd-construct
5105 parallel-sections-construct
5106 master-construct
5107 critical-construct
5108 atomic-construct
5109 ordered-construct
5111 parallel-construct:
5112 parallel-directive structured-block
5114 for-construct:
5115 for-directive iteration-statement
5117 simd-construct:
5118 simd-directive iteration-statements
5120 for-simd-construct:
5121 for-simd-directive iteration-statements
5123 sections-construct:
5124 sections-directive section-scope
5126 single-construct:
5127 single-directive structured-block
5129 parallel-for-construct:
5130 parallel-for-directive iteration-statement
5132 parallel-for-simd-construct:
5133 parallel-for-simd-directive iteration-statement
5135 parallel-sections-construct:
5136 parallel-sections-directive section-scope
5138 master-construct:
5139 master-directive structured-block
5141 critical-construct:
5142 critical-directive structured-block
5144 atomic-construct:
5145 atomic-directive expression-statement
5147 ordered-construct:
5148 ordered-directive structured-block
5150 Transactional Memory:
5152 statement:
5153 transaction-statement
5154 transaction-cancel-statement
5156 IF_P is used to track whether there's a (possibly labeled) if statement
5157 which is not enclosed in braces and has an else clause. This is used to
5158 implement -Wparentheses. */
5160 static void
5161 c_parser_statement (c_parser *parser, bool *if_p)
5163 c_parser_all_labels (parser);
5164 c_parser_statement_after_labels (parser, if_p, NULL);
5167 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5168 of if-else-if conditions.
5170 IF_P is used to track whether there's a (possibly labeled) if statement
5171 which is not enclosed in braces and has an else clause. This is used to
5172 implement -Wparentheses. */
5174 static void
5175 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5176 vec<tree> *chain)
5178 location_t loc = c_parser_peek_token (parser)->location;
5179 tree stmt = NULL_TREE;
5180 bool in_if_block = parser->in_if_block;
5181 parser->in_if_block = false;
5182 if (if_p != NULL)
5183 *if_p = false;
5184 switch (c_parser_peek_token (parser)->type)
5186 case CPP_OPEN_BRACE:
5187 add_stmt (c_parser_compound_statement (parser));
5188 break;
5189 case CPP_KEYWORD:
5190 switch (c_parser_peek_token (parser)->keyword)
5192 case RID_IF:
5193 c_parser_if_statement (parser, if_p, chain);
5194 break;
5195 case RID_SWITCH:
5196 c_parser_switch_statement (parser, if_p);
5197 break;
5198 case RID_WHILE:
5199 c_parser_while_statement (parser, false, if_p);
5200 break;
5201 case RID_DO:
5202 c_parser_do_statement (parser, false);
5203 break;
5204 case RID_FOR:
5205 c_parser_for_statement (parser, false, if_p);
5206 break;
5207 case RID_CILK_FOR:
5208 if (!flag_cilkplus)
5210 error_at (c_parser_peek_token (parser)->location,
5211 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5212 c_parser_skip_to_end_of_block_or_statement (parser);
5214 else
5215 c_parser_cilk_for (parser, integer_zero_node, if_p);
5216 break;
5217 case RID_CILK_SYNC:
5218 c_parser_consume_token (parser);
5219 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5220 if (!flag_cilkplus)
5221 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5222 else
5223 add_stmt (build_cilk_sync ());
5224 break;
5225 case RID_GOTO:
5226 c_parser_consume_token (parser);
5227 if (c_parser_next_token_is (parser, CPP_NAME))
5229 stmt = c_finish_goto_label (loc,
5230 c_parser_peek_token (parser)->value);
5231 c_parser_consume_token (parser);
5233 else if (c_parser_next_token_is (parser, CPP_MULT))
5235 struct c_expr val;
5237 c_parser_consume_token (parser);
5238 val = c_parser_expression (parser);
5239 if (check_no_cilk (val.value,
5240 "Cilk array notation cannot be used as a computed goto expression",
5241 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5242 loc))
5243 val.value = error_mark_node;
5244 val = convert_lvalue_to_rvalue (loc, val, false, true);
5245 stmt = c_finish_goto_ptr (loc, val.value);
5247 else
5248 c_parser_error (parser, "expected identifier or %<*%>");
5249 goto expect_semicolon;
5250 case RID_CONTINUE:
5251 c_parser_consume_token (parser);
5252 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5253 goto expect_semicolon;
5254 case RID_BREAK:
5255 c_parser_consume_token (parser);
5256 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5257 goto expect_semicolon;
5258 case RID_RETURN:
5259 c_parser_consume_token (parser);
5260 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5262 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5263 c_parser_consume_token (parser);
5265 else
5267 location_t xloc = c_parser_peek_token (parser)->location;
5268 struct c_expr expr = c_parser_expression_conv (parser);
5269 mark_exp_read (expr.value);
5270 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5271 expr.value, expr.original_type);
5272 goto expect_semicolon;
5274 break;
5275 case RID_ASM:
5276 stmt = c_parser_asm_statement (parser);
5277 break;
5278 case RID_TRANSACTION_ATOMIC:
5279 case RID_TRANSACTION_RELAXED:
5280 stmt = c_parser_transaction (parser,
5281 c_parser_peek_token (parser)->keyword);
5282 break;
5283 case RID_TRANSACTION_CANCEL:
5284 stmt = c_parser_transaction_cancel (parser);
5285 goto expect_semicolon;
5286 case RID_AT_THROW:
5287 gcc_assert (c_dialect_objc ());
5288 c_parser_consume_token (parser);
5289 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5291 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5292 c_parser_consume_token (parser);
5294 else
5296 struct c_expr expr = c_parser_expression (parser);
5297 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5298 if (check_no_cilk (expr.value,
5299 "Cilk array notation cannot be used for a throw expression",
5300 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5301 expr.value = error_mark_node;
5302 else
5304 expr.value = c_fully_fold (expr.value, false, NULL);
5305 stmt = objc_build_throw_stmt (loc, expr.value);
5307 goto expect_semicolon;
5309 break;
5310 case RID_AT_TRY:
5311 gcc_assert (c_dialect_objc ());
5312 c_parser_objc_try_catch_finally_statement (parser);
5313 break;
5314 case RID_AT_SYNCHRONIZED:
5315 gcc_assert (c_dialect_objc ());
5316 c_parser_objc_synchronized_statement (parser);
5317 break;
5318 case RID_ATTRIBUTE:
5320 /* Allow '__attribute__((fallthrough));'. */
5321 tree attrs = c_parser_attributes (parser);
5322 if (attribute_fallthrough_p (attrs))
5324 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5326 tree fn = build_call_expr_internal_loc (loc,
5327 IFN_FALLTHROUGH,
5328 void_type_node, 0);
5329 add_stmt (fn);
5330 /* Eat the ';'. */
5331 c_parser_consume_token (parser);
5333 else
5334 warning_at (loc, OPT_Wattributes,
5335 "%<fallthrough%> attribute not followed "
5336 "by %<;%>");
5338 else if (attrs != NULL_TREE)
5339 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5340 " can be applied to a null statement");
5341 break;
5343 default:
5344 goto expr_stmt;
5346 break;
5347 case CPP_SEMICOLON:
5348 c_parser_consume_token (parser);
5349 break;
5350 case CPP_CLOSE_PAREN:
5351 case CPP_CLOSE_SQUARE:
5352 /* Avoid infinite loop in error recovery:
5353 c_parser_skip_until_found stops at a closing nesting
5354 delimiter without consuming it, but here we need to consume
5355 it to proceed further. */
5356 c_parser_error (parser, "expected statement");
5357 c_parser_consume_token (parser);
5358 break;
5359 case CPP_PRAGMA:
5360 c_parser_pragma (parser, pragma_stmt, if_p);
5361 break;
5362 default:
5363 expr_stmt:
5364 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5365 expect_semicolon:
5366 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5367 break;
5369 /* Two cases cannot and do not have line numbers associated: If stmt
5370 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5371 cannot hold line numbers. But that's OK because the statement
5372 will either be changed to a MODIFY_EXPR during gimplification of
5373 the statement expr, or discarded. If stmt was compound, but
5374 without new variables, we will have skipped the creation of a
5375 BIND and will have a bare STATEMENT_LIST. But that's OK because
5376 (recursively) all of the component statements should already have
5377 line numbers assigned. ??? Can we discard no-op statements
5378 earlier? */
5379 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5380 protected_set_expr_location (stmt, loc);
5382 parser->in_if_block = in_if_block;
5385 /* Parse the condition from an if, do, while or for statements. */
5387 static tree
5388 c_parser_condition (c_parser *parser)
5390 location_t loc = c_parser_peek_token (parser)->location;
5391 tree cond;
5392 cond = c_parser_expression_conv (parser).value;
5393 cond = c_objc_common_truthvalue_conversion (loc, cond);
5394 cond = c_fully_fold (cond, false, NULL);
5395 if (warn_sequence_point)
5396 verify_sequence_points (cond);
5397 return cond;
5400 /* Parse a parenthesized condition from an if, do or while statement.
5402 condition:
5403 ( expression )
5405 static tree
5406 c_parser_paren_condition (c_parser *parser)
5408 tree cond;
5409 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5410 return error_mark_node;
5411 cond = c_parser_condition (parser);
5412 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5413 return cond;
5416 /* Parse a statement which is a block in C99.
5418 IF_P is used to track whether there's a (possibly labeled) if statement
5419 which is not enclosed in braces and has an else clause. This is used to
5420 implement -Wparentheses. */
5422 static tree
5423 c_parser_c99_block_statement (c_parser *parser, bool *if_p)
5425 tree block = c_begin_compound_stmt (flag_isoc99);
5426 location_t loc = c_parser_peek_token (parser)->location;
5427 c_parser_statement (parser, if_p);
5428 return c_end_compound_stmt (loc, block, flag_isoc99);
5431 /* Parse the body of an if statement. This is just parsing a
5432 statement but (a) it is a block in C99, (b) we track whether the
5433 body is an if statement for the sake of -Wparentheses warnings, (c)
5434 we handle an empty body specially for the sake of -Wempty-body
5435 warnings, and (d) we call parser_compound_statement directly
5436 because c_parser_statement_after_labels resets
5437 parser->in_if_block.
5439 IF_P is used to track whether there's a (possibly labeled) if statement
5440 which is not enclosed in braces and has an else clause. This is used to
5441 implement -Wparentheses. */
5443 static tree
5444 c_parser_if_body (c_parser *parser, bool *if_p,
5445 const token_indent_info &if_tinfo)
5447 tree block = c_begin_compound_stmt (flag_isoc99);
5448 location_t body_loc = c_parser_peek_token (parser)->location;
5449 token_indent_info body_tinfo
5450 = get_token_indent_info (c_parser_peek_token (parser));
5452 c_parser_all_labels (parser);
5453 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5455 location_t loc = c_parser_peek_token (parser)->location;
5456 add_stmt (build_empty_stmt (loc));
5457 c_parser_consume_token (parser);
5458 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5459 warning_at (loc, OPT_Wempty_body,
5460 "suggest braces around empty body in an %<if%> statement");
5462 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5463 add_stmt (c_parser_compound_statement (parser));
5464 else
5465 c_parser_statement_after_labels (parser, if_p);
5467 token_indent_info next_tinfo
5468 = get_token_indent_info (c_parser_peek_token (parser));
5469 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5471 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5474 /* Parse the else body of an if statement. This is just parsing a
5475 statement but (a) it is a block in C99, (b) we handle an empty body
5476 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5477 of if-else-if conditions. */
5479 static tree
5480 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5481 vec<tree> *chain)
5483 location_t body_loc = c_parser_peek_token (parser)->location;
5484 tree block = c_begin_compound_stmt (flag_isoc99);
5485 token_indent_info body_tinfo
5486 = get_token_indent_info (c_parser_peek_token (parser));
5488 c_parser_all_labels (parser);
5489 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5491 location_t loc = c_parser_peek_token (parser)->location;
5492 warning_at (loc,
5493 OPT_Wempty_body,
5494 "suggest braces around empty body in an %<else%> statement");
5495 add_stmt (build_empty_stmt (loc));
5496 c_parser_consume_token (parser);
5498 else
5499 c_parser_statement_after_labels (parser, NULL, chain);
5501 token_indent_info next_tinfo
5502 = get_token_indent_info (c_parser_peek_token (parser));
5503 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5505 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5508 /* We might need to reclassify any previously-lexed identifier, e.g.
5509 when we've left a for loop with an if-statement without else in the
5510 body - we might have used a wrong scope for the token. See PR67784. */
5512 static void
5513 c_parser_maybe_reclassify_token (c_parser *parser)
5515 if (c_parser_next_token_is (parser, CPP_NAME))
5517 c_token *token = c_parser_peek_token (parser);
5519 if (token->id_kind != C_ID_CLASSNAME)
5521 tree decl = lookup_name (token->value);
5523 token->id_kind = C_ID_ID;
5524 if (decl)
5526 if (TREE_CODE (decl) == TYPE_DECL)
5527 token->id_kind = C_ID_TYPENAME;
5529 else if (c_dialect_objc ())
5531 tree objc_interface_decl = objc_is_class_name (token->value);
5532 /* Objective-C class names are in the same namespace as
5533 variables and typedefs, and hence are shadowed by local
5534 declarations. */
5535 if (objc_interface_decl)
5537 token->value = objc_interface_decl;
5538 token->id_kind = C_ID_CLASSNAME;
5545 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5547 if-statement:
5548 if ( expression ) statement
5549 if ( expression ) statement else statement
5551 CHAIN is a vector of if-else-if conditions.
5552 IF_P is used to track whether there's a (possibly labeled) if statement
5553 which is not enclosed in braces and has an else clause. This is used to
5554 implement -Wparentheses. */
5556 static void
5557 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5559 tree block;
5560 location_t loc;
5561 tree cond;
5562 bool nested_if = false;
5563 tree first_body, second_body;
5564 bool in_if_block;
5565 tree if_stmt;
5567 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5568 token_indent_info if_tinfo
5569 = get_token_indent_info (c_parser_peek_token (parser));
5570 c_parser_consume_token (parser);
5571 block = c_begin_compound_stmt (flag_isoc99);
5572 loc = c_parser_peek_token (parser)->location;
5573 cond = c_parser_paren_condition (parser);
5574 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5576 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5577 cond = error_mark_node;
5579 in_if_block = parser->in_if_block;
5580 parser->in_if_block = true;
5581 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5582 parser->in_if_block = in_if_block;
5584 if (warn_duplicated_cond)
5585 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5587 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5589 token_indent_info else_tinfo
5590 = get_token_indent_info (c_parser_peek_token (parser));
5591 c_parser_consume_token (parser);
5592 if (warn_duplicated_cond)
5594 if (c_parser_next_token_is_keyword (parser, RID_IF)
5595 && chain == NULL)
5597 /* We've got "if (COND) else if (COND2)". Start the
5598 condition chain and add COND as the first element. */
5599 chain = new vec<tree> ();
5600 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5601 chain->safe_push (cond);
5603 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5605 /* This is if-else without subsequent if. Zap the condition
5606 chain; we would have already warned at this point. */
5607 delete chain;
5608 chain = NULL;
5611 second_body = c_parser_else_body (parser, else_tinfo, chain);
5612 /* Set IF_P to true to indicate that this if statement has an
5613 else clause. This may trigger the Wparentheses warning
5614 below when we get back up to the parent if statement. */
5615 if (if_p != NULL)
5616 *if_p = true;
5618 else
5620 second_body = NULL_TREE;
5622 /* Diagnose an ambiguous else if if-then-else is nested inside
5623 if-then. */
5624 if (nested_if)
5625 warning_at (loc, OPT_Wdangling_else,
5626 "suggest explicit braces to avoid ambiguous %<else%>");
5628 if (warn_duplicated_cond)
5630 /* This if statement does not have an else clause. We don't
5631 need the condition chain anymore. */
5632 delete chain;
5633 chain = NULL;
5636 c_finish_if_stmt (loc, cond, first_body, second_body);
5637 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5639 /* If the if statement contains array notations, then we expand them. */
5640 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5641 if_stmt = fix_conditional_array_notations (if_stmt);
5642 add_stmt (if_stmt);
5643 c_parser_maybe_reclassify_token (parser);
5646 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5648 switch-statement:
5649 switch (expression) statement
5652 static void
5653 c_parser_switch_statement (c_parser *parser, bool *if_p)
5655 struct c_expr ce;
5656 tree block, expr, body, save_break;
5657 location_t switch_loc = c_parser_peek_token (parser)->location;
5658 location_t switch_cond_loc;
5659 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5660 c_parser_consume_token (parser);
5661 block = c_begin_compound_stmt (flag_isoc99);
5662 bool explicit_cast_p = false;
5663 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5665 switch_cond_loc = c_parser_peek_token (parser)->location;
5666 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5667 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5668 explicit_cast_p = true;
5669 ce = c_parser_expression (parser);
5670 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5671 expr = ce.value;
5672 /* ??? expr has no valid location? */
5673 if (check_no_cilk (expr,
5674 "Cilk array notation cannot be used as a condition for switch statement",
5675 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5676 switch_cond_loc))
5677 expr = error_mark_node;
5678 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5680 else
5682 switch_cond_loc = UNKNOWN_LOCATION;
5683 expr = error_mark_node;
5684 ce.original_type = error_mark_node;
5686 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5687 save_break = c_break_label;
5688 c_break_label = NULL_TREE;
5689 body = c_parser_c99_block_statement (parser, if_p);
5690 c_finish_case (body, ce.original_type);
5691 if (c_break_label)
5693 location_t here = c_parser_peek_token (parser)->location;
5694 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5695 SET_EXPR_LOCATION (t, here);
5696 add_stmt (t);
5698 c_break_label = save_break;
5699 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5700 c_parser_maybe_reclassify_token (parser);
5703 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5705 while-statement:
5706 while (expression) statement
5708 IF_P is used to track whether there's a (possibly labeled) if statement
5709 which is not enclosed in braces and has an else clause. This is used to
5710 implement -Wparentheses. */
5712 static void
5713 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5715 tree block, cond, body, save_break, save_cont;
5716 location_t loc;
5717 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5718 token_indent_info while_tinfo
5719 = get_token_indent_info (c_parser_peek_token (parser));
5720 c_parser_consume_token (parser);
5721 block = c_begin_compound_stmt (flag_isoc99);
5722 loc = c_parser_peek_token (parser)->location;
5723 cond = c_parser_paren_condition (parser);
5724 if (check_no_cilk (cond,
5725 "Cilk array notation cannot be used as a condition for while statement",
5726 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5727 cond = error_mark_node;
5728 if (ivdep && cond != error_mark_node)
5729 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5730 build_int_cst (integer_type_node,
5731 annot_expr_ivdep_kind));
5732 save_break = c_break_label;
5733 c_break_label = NULL_TREE;
5734 save_cont = c_cont_label;
5735 c_cont_label = NULL_TREE;
5737 token_indent_info body_tinfo
5738 = get_token_indent_info (c_parser_peek_token (parser));
5740 body = c_parser_c99_block_statement (parser, if_p);
5741 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5742 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5743 c_parser_maybe_reclassify_token (parser);
5745 token_indent_info next_tinfo
5746 = get_token_indent_info (c_parser_peek_token (parser));
5747 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5749 c_break_label = save_break;
5750 c_cont_label = save_cont;
5753 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5755 do-statement:
5756 do statement while ( expression ) ;
5759 static void
5760 c_parser_do_statement (c_parser *parser, bool ivdep)
5762 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5763 location_t loc;
5764 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5765 c_parser_consume_token (parser);
5766 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5767 warning_at (c_parser_peek_token (parser)->location,
5768 OPT_Wempty_body,
5769 "suggest braces around empty body in %<do%> statement");
5770 block = c_begin_compound_stmt (flag_isoc99);
5771 loc = c_parser_peek_token (parser)->location;
5772 save_break = c_break_label;
5773 c_break_label = NULL_TREE;
5774 save_cont = c_cont_label;
5775 c_cont_label = NULL_TREE;
5776 body = c_parser_c99_block_statement (parser, NULL);
5777 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5778 new_break = c_break_label;
5779 c_break_label = save_break;
5780 new_cont = c_cont_label;
5781 c_cont_label = save_cont;
5782 cond = c_parser_paren_condition (parser);
5783 if (check_no_cilk (cond,
5784 "Cilk array notation cannot be used as a condition for a do-while statement",
5785 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5786 cond = error_mark_node;
5787 if (ivdep && cond != error_mark_node)
5788 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5789 build_int_cst (integer_type_node,
5790 annot_expr_ivdep_kind));
5791 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5792 c_parser_skip_to_end_of_block_or_statement (parser);
5793 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5794 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5797 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5799 for-statement:
5800 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5801 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5803 The form with a declaration is new in C99.
5805 ??? In accordance with the old parser, the declaration may be a
5806 nested function, which is then rejected in check_for_loop_decls,
5807 but does it make any sense for this to be included in the grammar?
5808 Note in particular that the nested function does not include a
5809 trailing ';', whereas the "declaration" production includes one.
5810 Also, can we reject bad declarations earlier and cheaper than
5811 check_for_loop_decls?
5813 In Objective-C, there are two additional variants:
5815 foreach-statement:
5816 for ( expression in expresssion ) statement
5817 for ( declaration in expression ) statement
5819 This is inconsistent with C, because the second variant is allowed
5820 even if c99 is not enabled.
5822 The rest of the comment documents these Objective-C foreach-statement.
5824 Here is the canonical example of the first variant:
5825 for (object in array) { do something with object }
5826 we call the first expression ("object") the "object_expression" and
5827 the second expression ("array") the "collection_expression".
5828 object_expression must be an lvalue of type "id" (a generic Objective-C
5829 object) because the loop works by assigning to object_expression the
5830 various objects from the collection_expression. collection_expression
5831 must evaluate to something of type "id" which responds to the method
5832 countByEnumeratingWithState:objects:count:.
5834 The canonical example of the second variant is:
5835 for (id object in array) { do something with object }
5836 which is completely equivalent to
5838 id object;
5839 for (object in array) { do something with object }
5841 Note that initizializing 'object' in some way (eg, "for ((object =
5842 xxx) in array) { do something with object }") is possibly
5843 technically valid, but completely pointless as 'object' will be
5844 assigned to something else as soon as the loop starts. We should
5845 most likely reject it (TODO).
5847 The beginning of the Objective-C foreach-statement looks exactly
5848 like the beginning of the for-statement, and we can tell it is a
5849 foreach-statement only because the initial declaration or
5850 expression is terminated by 'in' instead of ';'.
5852 IF_P is used to track whether there's a (possibly labeled) if statement
5853 which is not enclosed in braces and has an else clause. This is used to
5854 implement -Wparentheses. */
5856 static void
5857 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
5859 tree block, cond, incr, save_break, save_cont, body;
5860 /* The following are only used when parsing an ObjC foreach statement. */
5861 tree object_expression;
5862 /* Silence the bogus uninitialized warning. */
5863 tree collection_expression = NULL;
5864 location_t loc = c_parser_peek_token (parser)->location;
5865 location_t for_loc = c_parser_peek_token (parser)->location;
5866 bool is_foreach_statement = false;
5867 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5868 token_indent_info for_tinfo
5869 = get_token_indent_info (c_parser_peek_token (parser));
5870 c_parser_consume_token (parser);
5871 /* Open a compound statement in Objective-C as well, just in case this is
5872 as foreach expression. */
5873 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5874 cond = error_mark_node;
5875 incr = error_mark_node;
5876 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5878 /* Parse the initialization declaration or expression. */
5879 object_expression = error_mark_node;
5880 parser->objc_could_be_foreach_context = c_dialect_objc ();
5881 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5883 parser->objc_could_be_foreach_context = false;
5884 c_parser_consume_token (parser);
5885 c_finish_expr_stmt (loc, NULL_TREE);
5887 else if (c_parser_next_tokens_start_declaration (parser))
5889 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5890 &object_expression, vNULL);
5891 parser->objc_could_be_foreach_context = false;
5893 if (c_parser_next_token_is_keyword (parser, RID_IN))
5895 c_parser_consume_token (parser);
5896 is_foreach_statement = true;
5897 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5898 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5900 else
5901 check_for_loop_decls (for_loc, flag_isoc99);
5903 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5905 /* __extension__ can start a declaration, but is also an
5906 unary operator that can start an expression. Consume all
5907 but the last of a possible series of __extension__ to
5908 determine which. */
5909 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5910 && (c_parser_peek_2nd_token (parser)->keyword
5911 == RID_EXTENSION))
5912 c_parser_consume_token (parser);
5913 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5915 int ext;
5916 ext = disable_extension_diagnostics ();
5917 c_parser_consume_token (parser);
5918 c_parser_declaration_or_fndef (parser, true, true, true, true,
5919 true, &object_expression, vNULL);
5920 parser->objc_could_be_foreach_context = false;
5922 restore_extension_diagnostics (ext);
5923 if (c_parser_next_token_is_keyword (parser, RID_IN))
5925 c_parser_consume_token (parser);
5926 is_foreach_statement = true;
5927 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5928 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5930 else
5931 check_for_loop_decls (for_loc, flag_isoc99);
5933 else
5934 goto init_expr;
5936 else
5938 init_expr:
5940 struct c_expr ce;
5941 tree init_expression;
5942 ce = c_parser_expression (parser);
5943 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5944 level statement", but it works just fine, so allow it. */
5945 init_expression = ce.value;
5946 parser->objc_could_be_foreach_context = false;
5947 if (c_parser_next_token_is_keyword (parser, RID_IN))
5949 c_parser_consume_token (parser);
5950 is_foreach_statement = true;
5951 if (! lvalue_p (init_expression))
5952 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5953 object_expression = c_fully_fold (init_expression, false, NULL);
5955 else
5957 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5958 init_expression = ce.value;
5959 c_finish_expr_stmt (loc, init_expression);
5960 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5964 /* Parse the loop condition. In the case of a foreach
5965 statement, there is no loop condition. */
5966 gcc_assert (!parser->objc_could_be_foreach_context);
5967 if (!is_foreach_statement)
5969 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5971 if (ivdep)
5973 c_parser_error (parser, "missing loop condition in loop with "
5974 "%<GCC ivdep%> pragma");
5975 cond = error_mark_node;
5977 else
5979 c_parser_consume_token (parser);
5980 cond = NULL_TREE;
5983 else
5985 cond = c_parser_condition (parser);
5986 if (check_no_cilk (cond,
5987 "Cilk array notation cannot be used in a condition for a for-loop",
5988 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5989 cond = error_mark_node;
5990 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5991 "expected %<;%>");
5993 if (ivdep && cond != error_mark_node)
5994 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5995 build_int_cst (integer_type_node,
5996 annot_expr_ivdep_kind));
5998 /* Parse the increment expression (the third expression in a
5999 for-statement). In the case of a foreach-statement, this is
6000 the expression that follows the 'in'. */
6001 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6003 if (is_foreach_statement)
6005 c_parser_error (parser, "missing collection in fast enumeration");
6006 collection_expression = error_mark_node;
6008 else
6009 incr = c_process_expr_stmt (loc, NULL_TREE);
6011 else
6013 if (is_foreach_statement)
6014 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6015 false, NULL);
6016 else
6018 struct c_expr ce = c_parser_expression (parser);
6019 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6020 incr = c_process_expr_stmt (loc, ce.value);
6023 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6025 save_break = c_break_label;
6026 c_break_label = NULL_TREE;
6027 save_cont = c_cont_label;
6028 c_cont_label = NULL_TREE;
6030 token_indent_info body_tinfo
6031 = get_token_indent_info (c_parser_peek_token (parser));
6033 body = c_parser_c99_block_statement (parser, if_p);
6035 if (is_foreach_statement)
6036 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6037 else
6038 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6039 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6040 c_parser_maybe_reclassify_token (parser);
6042 token_indent_info next_tinfo
6043 = get_token_indent_info (c_parser_peek_token (parser));
6044 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6046 c_break_label = save_break;
6047 c_cont_label = save_cont;
6050 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6051 statement with inputs, outputs, clobbers, and volatile tag
6052 allowed.
6054 asm-statement:
6055 asm type-qualifier[opt] ( asm-argument ) ;
6056 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6058 asm-argument:
6059 asm-string-literal
6060 asm-string-literal : asm-operands[opt]
6061 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6062 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6064 asm-goto-argument:
6065 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6066 : asm-goto-operands
6068 Qualifiers other than volatile are accepted in the syntax but
6069 warned for. */
6071 static tree
6072 c_parser_asm_statement (c_parser *parser)
6074 tree quals, str, outputs, inputs, clobbers, labels, ret;
6075 bool simple, is_goto;
6076 location_t asm_loc = c_parser_peek_token (parser)->location;
6077 int section, nsections;
6079 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6080 c_parser_consume_token (parser);
6081 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6083 quals = c_parser_peek_token (parser)->value;
6084 c_parser_consume_token (parser);
6086 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6087 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6089 warning_at (c_parser_peek_token (parser)->location,
6091 "%E qualifier ignored on asm",
6092 c_parser_peek_token (parser)->value);
6093 quals = NULL_TREE;
6094 c_parser_consume_token (parser);
6096 else
6097 quals = NULL_TREE;
6099 is_goto = false;
6100 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6102 c_parser_consume_token (parser);
6103 is_goto = true;
6106 /* ??? Follow the C++ parser rather than using the
6107 lex_untranslated_string kludge. */
6108 parser->lex_untranslated_string = true;
6109 ret = NULL;
6111 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6112 goto error;
6114 str = c_parser_asm_string_literal (parser);
6115 if (str == NULL_TREE)
6116 goto error_close_paren;
6118 simple = true;
6119 outputs = NULL_TREE;
6120 inputs = NULL_TREE;
6121 clobbers = NULL_TREE;
6122 labels = NULL_TREE;
6124 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6125 goto done_asm;
6127 /* Parse each colon-delimited section of operands. */
6128 nsections = 3 + is_goto;
6129 for (section = 0; section < nsections; ++section)
6131 if (!c_parser_require (parser, CPP_COLON,
6132 is_goto
6133 ? "expected %<:%>"
6134 : "expected %<:%> or %<)%>"))
6135 goto error_close_paren;
6137 /* Once past any colon, we're no longer a simple asm. */
6138 simple = false;
6140 if ((!c_parser_next_token_is (parser, CPP_COLON)
6141 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6142 || section == 3)
6143 switch (section)
6145 case 0:
6146 /* For asm goto, we don't allow output operands, but reserve
6147 the slot for a future extension that does allow them. */
6148 if (!is_goto)
6149 outputs = c_parser_asm_operands (parser);
6150 break;
6151 case 1:
6152 inputs = c_parser_asm_operands (parser);
6153 break;
6154 case 2:
6155 clobbers = c_parser_asm_clobbers (parser);
6156 break;
6157 case 3:
6158 labels = c_parser_asm_goto_operands (parser);
6159 break;
6160 default:
6161 gcc_unreachable ();
6164 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6165 goto done_asm;
6168 done_asm:
6169 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6171 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6172 goto error;
6175 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6176 c_parser_skip_to_end_of_block_or_statement (parser);
6178 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6179 clobbers, labels, simple));
6181 error:
6182 parser->lex_untranslated_string = false;
6183 return ret;
6185 error_close_paren:
6186 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6187 goto error;
6190 /* Parse asm operands, a GNU extension.
6192 asm-operands:
6193 asm-operand
6194 asm-operands , asm-operand
6196 asm-operand:
6197 asm-string-literal ( expression )
6198 [ identifier ] asm-string-literal ( expression )
6201 static tree
6202 c_parser_asm_operands (c_parser *parser)
6204 tree list = NULL_TREE;
6205 while (true)
6207 tree name, str;
6208 struct c_expr expr;
6209 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6211 c_parser_consume_token (parser);
6212 if (c_parser_next_token_is (parser, CPP_NAME))
6214 tree id = c_parser_peek_token (parser)->value;
6215 c_parser_consume_token (parser);
6216 name = build_string (IDENTIFIER_LENGTH (id),
6217 IDENTIFIER_POINTER (id));
6219 else
6221 c_parser_error (parser, "expected identifier");
6222 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6223 return NULL_TREE;
6225 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6226 "expected %<]%>");
6228 else
6229 name = NULL_TREE;
6230 str = c_parser_asm_string_literal (parser);
6231 if (str == NULL_TREE)
6232 return NULL_TREE;
6233 parser->lex_untranslated_string = false;
6234 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6236 parser->lex_untranslated_string = true;
6237 return NULL_TREE;
6239 expr = c_parser_expression (parser);
6240 mark_exp_read (expr.value);
6241 parser->lex_untranslated_string = true;
6242 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6245 return NULL_TREE;
6247 list = chainon (list, build_tree_list (build_tree_list (name, str),
6248 expr.value));
6249 if (c_parser_next_token_is (parser, CPP_COMMA))
6250 c_parser_consume_token (parser);
6251 else
6252 break;
6254 return list;
6257 /* Parse asm clobbers, a GNU extension.
6259 asm-clobbers:
6260 asm-string-literal
6261 asm-clobbers , asm-string-literal
6264 static tree
6265 c_parser_asm_clobbers (c_parser *parser)
6267 tree list = NULL_TREE;
6268 while (true)
6270 tree str = c_parser_asm_string_literal (parser);
6271 if (str)
6272 list = tree_cons (NULL_TREE, str, list);
6273 else
6274 return NULL_TREE;
6275 if (c_parser_next_token_is (parser, CPP_COMMA))
6276 c_parser_consume_token (parser);
6277 else
6278 break;
6280 return list;
6283 /* Parse asm goto labels, a GNU extension.
6285 asm-goto-operands:
6286 identifier
6287 asm-goto-operands , identifier
6290 static tree
6291 c_parser_asm_goto_operands (c_parser *parser)
6293 tree list = NULL_TREE;
6294 while (true)
6296 tree name, label;
6298 if (c_parser_next_token_is (parser, CPP_NAME))
6300 c_token *tok = c_parser_peek_token (parser);
6301 name = tok->value;
6302 label = lookup_label_for_goto (tok->location, name);
6303 c_parser_consume_token (parser);
6304 TREE_USED (label) = 1;
6306 else
6308 c_parser_error (parser, "expected identifier");
6309 return NULL_TREE;
6312 name = build_string (IDENTIFIER_LENGTH (name),
6313 IDENTIFIER_POINTER (name));
6314 list = tree_cons (name, label, list);
6315 if (c_parser_next_token_is (parser, CPP_COMMA))
6316 c_parser_consume_token (parser);
6317 else
6318 return nreverse (list);
6322 /* Parse an expression other than a compound expression; that is, an
6323 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6324 NULL then it is an Objective-C message expression which is the
6325 primary-expression starting the expression as an initializer.
6327 assignment-expression:
6328 conditional-expression
6329 unary-expression assignment-operator assignment-expression
6331 assignment-operator: one of
6332 = *= /= %= += -= <<= >>= &= ^= |=
6334 In GNU C we accept any conditional expression on the LHS and
6335 diagnose the invalid lvalue rather than producing a syntax
6336 error. */
6338 static struct c_expr
6339 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6340 tree omp_atomic_lhs)
6342 struct c_expr lhs, rhs, ret;
6343 enum tree_code code;
6344 location_t op_location, exp_location;
6345 gcc_assert (!after || c_dialect_objc ());
6346 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6347 op_location = c_parser_peek_token (parser)->location;
6348 switch (c_parser_peek_token (parser)->type)
6350 case CPP_EQ:
6351 code = NOP_EXPR;
6352 break;
6353 case CPP_MULT_EQ:
6354 code = MULT_EXPR;
6355 break;
6356 case CPP_DIV_EQ:
6357 code = TRUNC_DIV_EXPR;
6358 break;
6359 case CPP_MOD_EQ:
6360 code = TRUNC_MOD_EXPR;
6361 break;
6362 case CPP_PLUS_EQ:
6363 code = PLUS_EXPR;
6364 break;
6365 case CPP_MINUS_EQ:
6366 code = MINUS_EXPR;
6367 break;
6368 case CPP_LSHIFT_EQ:
6369 code = LSHIFT_EXPR;
6370 break;
6371 case CPP_RSHIFT_EQ:
6372 code = RSHIFT_EXPR;
6373 break;
6374 case CPP_AND_EQ:
6375 code = BIT_AND_EXPR;
6376 break;
6377 case CPP_XOR_EQ:
6378 code = BIT_XOR_EXPR;
6379 break;
6380 case CPP_OR_EQ:
6381 code = BIT_IOR_EXPR;
6382 break;
6383 default:
6384 return lhs;
6386 c_parser_consume_token (parser);
6387 exp_location = c_parser_peek_token (parser)->location;
6388 rhs = c_parser_expr_no_commas (parser, NULL);
6389 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6391 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6392 code, exp_location, rhs.value,
6393 rhs.original_type);
6394 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6395 if (code == NOP_EXPR)
6396 ret.original_code = MODIFY_EXPR;
6397 else
6399 TREE_NO_WARNING (ret.value) = 1;
6400 ret.original_code = ERROR_MARK;
6402 ret.original_type = NULL;
6403 return ret;
6406 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6407 is not NULL then it is an Objective-C message expression which is
6408 the primary-expression starting the expression as an initializer.
6410 conditional-expression:
6411 logical-OR-expression
6412 logical-OR-expression ? expression : conditional-expression
6414 GNU extensions:
6416 conditional-expression:
6417 logical-OR-expression ? : conditional-expression
6420 static struct c_expr
6421 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6422 tree omp_atomic_lhs)
6424 struct c_expr cond, exp1, exp2, ret;
6425 location_t start, cond_loc, colon_loc, middle_loc;
6427 gcc_assert (!after || c_dialect_objc ());
6429 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6431 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6432 return cond;
6433 if (cond.value != error_mark_node)
6434 start = cond.get_start ();
6435 else
6436 start = UNKNOWN_LOCATION;
6437 cond_loc = c_parser_peek_token (parser)->location;
6438 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6439 c_parser_consume_token (parser);
6440 if (c_parser_next_token_is (parser, CPP_COLON))
6442 tree eptype = NULL_TREE;
6444 middle_loc = c_parser_peek_token (parser)->location;
6445 pedwarn (middle_loc, OPT_Wpedantic,
6446 "ISO C forbids omitting the middle term of a ?: expression");
6447 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6449 eptype = TREE_TYPE (cond.value);
6450 cond.value = TREE_OPERAND (cond.value, 0);
6452 tree e = cond.value;
6453 while (TREE_CODE (e) == COMPOUND_EXPR)
6454 e = TREE_OPERAND (e, 1);
6455 warn_for_omitted_condop (middle_loc, e);
6456 /* Make sure first operand is calculated only once. */
6457 exp1.value = c_save_expr (default_conversion (cond.value));
6458 if (eptype)
6459 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6460 exp1.original_type = NULL;
6461 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6462 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6464 else
6466 cond.value
6467 = c_objc_common_truthvalue_conversion
6468 (cond_loc, default_conversion (cond.value));
6469 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6470 exp1 = c_parser_expression_conv (parser);
6471 mark_exp_read (exp1.value);
6472 c_inhibit_evaluation_warnings +=
6473 ((cond.value == truthvalue_true_node)
6474 - (cond.value == truthvalue_false_node));
6477 colon_loc = c_parser_peek_token (parser)->location;
6478 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6480 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6481 ret.value = error_mark_node;
6482 ret.original_code = ERROR_MARK;
6483 ret.original_type = NULL;
6484 return ret;
6487 location_t exp2_loc = c_parser_peek_token (parser)->location;
6488 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6489 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6491 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6492 ret.value = build_conditional_expr (colon_loc, cond.value,
6493 cond.original_code == C_MAYBE_CONST_EXPR,
6494 exp1.value, exp1.original_type,
6495 exp2.value, exp2.original_type);
6496 ret.original_code = ERROR_MARK;
6497 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6498 ret.original_type = NULL;
6499 else
6501 tree t1, t2;
6503 /* If both sides are enum type, the default conversion will have
6504 made the type of the result be an integer type. We want to
6505 remember the enum types we started with. */
6506 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6507 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6508 ret.original_type = ((t1 != error_mark_node
6509 && t2 != error_mark_node
6510 && (TYPE_MAIN_VARIANT (t1)
6511 == TYPE_MAIN_VARIANT (t2)))
6512 ? t1
6513 : NULL);
6515 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6516 return ret;
6519 /* Parse a binary expression; that is, a logical-OR-expression (C90
6520 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6521 an Objective-C message expression which is the primary-expression
6522 starting the expression as an initializer.
6524 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6525 when it should be the unfolded lhs. In a valid OpenMP source,
6526 one of the operands of the toplevel binary expression must be equal
6527 to it. In that case, just return a build2 created binary operation
6528 rather than result of parser_build_binary_op.
6530 multiplicative-expression:
6531 cast-expression
6532 multiplicative-expression * cast-expression
6533 multiplicative-expression / cast-expression
6534 multiplicative-expression % cast-expression
6536 additive-expression:
6537 multiplicative-expression
6538 additive-expression + multiplicative-expression
6539 additive-expression - multiplicative-expression
6541 shift-expression:
6542 additive-expression
6543 shift-expression << additive-expression
6544 shift-expression >> additive-expression
6546 relational-expression:
6547 shift-expression
6548 relational-expression < shift-expression
6549 relational-expression > shift-expression
6550 relational-expression <= shift-expression
6551 relational-expression >= shift-expression
6553 equality-expression:
6554 relational-expression
6555 equality-expression == relational-expression
6556 equality-expression != relational-expression
6558 AND-expression:
6559 equality-expression
6560 AND-expression & equality-expression
6562 exclusive-OR-expression:
6563 AND-expression
6564 exclusive-OR-expression ^ AND-expression
6566 inclusive-OR-expression:
6567 exclusive-OR-expression
6568 inclusive-OR-expression | exclusive-OR-expression
6570 logical-AND-expression:
6571 inclusive-OR-expression
6572 logical-AND-expression && inclusive-OR-expression
6574 logical-OR-expression:
6575 logical-AND-expression
6576 logical-OR-expression || logical-AND-expression
6579 static struct c_expr
6580 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6581 tree omp_atomic_lhs)
6583 /* A binary expression is parsed using operator-precedence parsing,
6584 with the operands being cast expressions. All the binary
6585 operators are left-associative. Thus a binary expression is of
6586 form:
6588 E0 op1 E1 op2 E2 ...
6590 which we represent on a stack. On the stack, the precedence
6591 levels are strictly increasing. When a new operator is
6592 encountered of higher precedence than that at the top of the
6593 stack, it is pushed; its LHS is the top expression, and its RHS
6594 is everything parsed until it is popped. When a new operator is
6595 encountered with precedence less than or equal to that at the top
6596 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6597 by the result of the operation until the operator at the top of
6598 the stack has lower precedence than the new operator or there is
6599 only one element on the stack; then the top expression is the LHS
6600 of the new operator. In the case of logical AND and OR
6601 expressions, we also need to adjust c_inhibit_evaluation_warnings
6602 as appropriate when the operators are pushed and popped. */
6604 struct {
6605 /* The expression at this stack level. */
6606 struct c_expr expr;
6607 /* The precedence of the operator on its left, PREC_NONE at the
6608 bottom of the stack. */
6609 enum c_parser_prec prec;
6610 /* The operation on its left. */
6611 enum tree_code op;
6612 /* The source location of this operation. */
6613 location_t loc;
6614 } stack[NUM_PRECS];
6615 int sp;
6616 /* Location of the binary operator. */
6617 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6618 #define POP \
6619 do { \
6620 switch (stack[sp].op) \
6622 case TRUTH_ANDIF_EXPR: \
6623 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6624 == truthvalue_false_node); \
6625 break; \
6626 case TRUTH_ORIF_EXPR: \
6627 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6628 == truthvalue_true_node); \
6629 break; \
6630 default: \
6631 break; \
6633 stack[sp - 1].expr \
6634 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6635 stack[sp - 1].expr, true, true); \
6636 stack[sp].expr \
6637 = convert_lvalue_to_rvalue (stack[sp].loc, \
6638 stack[sp].expr, true, true); \
6639 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6640 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6641 && ((1 << stack[sp].prec) \
6642 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6643 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6644 && stack[sp].op != TRUNC_MOD_EXPR \
6645 && stack[0].expr.value != error_mark_node \
6646 && stack[1].expr.value != error_mark_node \
6647 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6648 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6649 stack[0].expr.value \
6650 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6651 stack[0].expr.value, stack[1].expr.value); \
6652 else \
6653 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6654 stack[sp].op, \
6655 stack[sp - 1].expr, \
6656 stack[sp].expr); \
6657 sp--; \
6658 } while (0)
6659 gcc_assert (!after || c_dialect_objc ());
6660 stack[0].loc = c_parser_peek_token (parser)->location;
6661 stack[0].expr = c_parser_cast_expression (parser, after);
6662 stack[0].prec = PREC_NONE;
6663 sp = 0;
6664 while (true)
6666 enum c_parser_prec oprec;
6667 enum tree_code ocode;
6668 source_range src_range;
6669 if (parser->error)
6670 goto out;
6671 switch (c_parser_peek_token (parser)->type)
6673 case CPP_MULT:
6674 oprec = PREC_MULT;
6675 ocode = MULT_EXPR;
6676 break;
6677 case CPP_DIV:
6678 oprec = PREC_MULT;
6679 ocode = TRUNC_DIV_EXPR;
6680 break;
6681 case CPP_MOD:
6682 oprec = PREC_MULT;
6683 ocode = TRUNC_MOD_EXPR;
6684 break;
6685 case CPP_PLUS:
6686 oprec = PREC_ADD;
6687 ocode = PLUS_EXPR;
6688 break;
6689 case CPP_MINUS:
6690 oprec = PREC_ADD;
6691 ocode = MINUS_EXPR;
6692 break;
6693 case CPP_LSHIFT:
6694 oprec = PREC_SHIFT;
6695 ocode = LSHIFT_EXPR;
6696 break;
6697 case CPP_RSHIFT:
6698 oprec = PREC_SHIFT;
6699 ocode = RSHIFT_EXPR;
6700 break;
6701 case CPP_LESS:
6702 oprec = PREC_REL;
6703 ocode = LT_EXPR;
6704 break;
6705 case CPP_GREATER:
6706 oprec = PREC_REL;
6707 ocode = GT_EXPR;
6708 break;
6709 case CPP_LESS_EQ:
6710 oprec = PREC_REL;
6711 ocode = LE_EXPR;
6712 break;
6713 case CPP_GREATER_EQ:
6714 oprec = PREC_REL;
6715 ocode = GE_EXPR;
6716 break;
6717 case CPP_EQ_EQ:
6718 oprec = PREC_EQ;
6719 ocode = EQ_EXPR;
6720 break;
6721 case CPP_NOT_EQ:
6722 oprec = PREC_EQ;
6723 ocode = NE_EXPR;
6724 break;
6725 case CPP_AND:
6726 oprec = PREC_BITAND;
6727 ocode = BIT_AND_EXPR;
6728 break;
6729 case CPP_XOR:
6730 oprec = PREC_BITXOR;
6731 ocode = BIT_XOR_EXPR;
6732 break;
6733 case CPP_OR:
6734 oprec = PREC_BITOR;
6735 ocode = BIT_IOR_EXPR;
6736 break;
6737 case CPP_AND_AND:
6738 oprec = PREC_LOGAND;
6739 ocode = TRUTH_ANDIF_EXPR;
6740 break;
6741 case CPP_OR_OR:
6742 oprec = PREC_LOGOR;
6743 ocode = TRUTH_ORIF_EXPR;
6744 break;
6745 default:
6746 /* Not a binary operator, so end of the binary
6747 expression. */
6748 goto out;
6750 binary_loc = c_parser_peek_token (parser)->location;
6751 while (oprec <= stack[sp].prec)
6752 POP;
6753 c_parser_consume_token (parser);
6754 switch (ocode)
6756 case TRUTH_ANDIF_EXPR:
6757 src_range = stack[sp].expr.src_range;
6758 stack[sp].expr
6759 = convert_lvalue_to_rvalue (stack[sp].loc,
6760 stack[sp].expr, true, true);
6761 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6762 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6763 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6764 == truthvalue_false_node);
6765 set_c_expr_source_range (&stack[sp].expr, src_range);
6766 break;
6767 case TRUTH_ORIF_EXPR:
6768 src_range = stack[sp].expr.src_range;
6769 stack[sp].expr
6770 = convert_lvalue_to_rvalue (stack[sp].loc,
6771 stack[sp].expr, true, true);
6772 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6773 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6774 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6775 == truthvalue_true_node);
6776 set_c_expr_source_range (&stack[sp].expr, src_range);
6777 break;
6778 default:
6779 break;
6781 sp++;
6782 stack[sp].loc = binary_loc;
6783 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6784 stack[sp].prec = oprec;
6785 stack[sp].op = ocode;
6787 out:
6788 while (sp > 0)
6789 POP;
6790 return stack[0].expr;
6791 #undef POP
6794 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6795 NULL then it is an Objective-C message expression which is the
6796 primary-expression starting the expression as an initializer.
6798 cast-expression:
6799 unary-expression
6800 ( type-name ) unary-expression
6803 static struct c_expr
6804 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6806 location_t cast_loc = c_parser_peek_token (parser)->location;
6807 gcc_assert (!after || c_dialect_objc ());
6808 if (after)
6809 return c_parser_postfix_expression_after_primary (parser,
6810 cast_loc, *after);
6811 /* If the expression begins with a parenthesized type name, it may
6812 be either a cast or a compound literal; we need to see whether
6813 the next character is '{' to tell the difference. If not, it is
6814 an unary expression. Full detection of unknown typenames here
6815 would require a 3-token lookahead. */
6816 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6817 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6819 struct c_type_name *type_name;
6820 struct c_expr ret;
6821 struct c_expr expr;
6822 c_parser_consume_token (parser);
6823 type_name = c_parser_type_name (parser);
6824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6825 if (type_name == NULL)
6827 ret.value = error_mark_node;
6828 ret.original_code = ERROR_MARK;
6829 ret.original_type = NULL;
6830 return ret;
6833 /* Save casted types in the function's used types hash table. */
6834 used_types_insert (type_name->specs->type);
6836 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6837 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6838 cast_loc);
6840 location_t expr_loc = c_parser_peek_token (parser)->location;
6841 expr = c_parser_cast_expression (parser, NULL);
6842 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6844 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6845 if (ret.value && expr.value)
6846 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6847 ret.original_code = ERROR_MARK;
6848 ret.original_type = NULL;
6849 return ret;
6851 else
6852 return c_parser_unary_expression (parser);
6855 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6857 unary-expression:
6858 postfix-expression
6859 ++ unary-expression
6860 -- unary-expression
6861 unary-operator cast-expression
6862 sizeof unary-expression
6863 sizeof ( type-name )
6865 unary-operator: one of
6866 & * + - ~ !
6868 GNU extensions:
6870 unary-expression:
6871 __alignof__ unary-expression
6872 __alignof__ ( type-name )
6873 && identifier
6875 (C11 permits _Alignof with type names only.)
6877 unary-operator: one of
6878 __extension__ __real__ __imag__
6880 Transactional Memory:
6882 unary-expression:
6883 transaction-expression
6885 In addition, the GNU syntax treats ++ and -- as unary operators, so
6886 they may be applied to cast expressions with errors for non-lvalues
6887 given later. */
6889 static struct c_expr
6890 c_parser_unary_expression (c_parser *parser)
6892 int ext;
6893 struct c_expr ret, op;
6894 location_t op_loc = c_parser_peek_token (parser)->location;
6895 location_t exp_loc;
6896 location_t finish;
6897 ret.original_code = ERROR_MARK;
6898 ret.original_type = NULL;
6899 switch (c_parser_peek_token (parser)->type)
6901 case CPP_PLUS_PLUS:
6902 c_parser_consume_token (parser);
6903 exp_loc = c_parser_peek_token (parser)->location;
6904 op = c_parser_cast_expression (parser, NULL);
6906 /* If there is array notations in op, we expand them. */
6907 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6908 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6909 else
6911 op = default_function_array_read_conversion (exp_loc, op);
6912 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6914 case CPP_MINUS_MINUS:
6915 c_parser_consume_token (parser);
6916 exp_loc = c_parser_peek_token (parser)->location;
6917 op = c_parser_cast_expression (parser, NULL);
6919 /* If there is array notations in op, we expand them. */
6920 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6921 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6922 else
6924 op = default_function_array_read_conversion (exp_loc, op);
6925 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6927 case CPP_AND:
6928 c_parser_consume_token (parser);
6929 op = c_parser_cast_expression (parser, NULL);
6930 mark_exp_read (op.value);
6931 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6932 case CPP_MULT:
6934 c_parser_consume_token (parser);
6935 exp_loc = c_parser_peek_token (parser)->location;
6936 op = c_parser_cast_expression (parser, NULL);
6937 finish = op.get_finish ();
6938 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6939 location_t combined_loc = make_location (op_loc, op_loc, finish);
6940 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
6941 ret.src_range.m_start = op_loc;
6942 ret.src_range.m_finish = finish;
6943 return ret;
6945 case CPP_PLUS:
6946 if (!c_dialect_objc () && !in_system_header_at (input_location))
6947 warning_at (op_loc,
6948 OPT_Wtraditional,
6949 "traditional C rejects the unary plus operator");
6950 c_parser_consume_token (parser);
6951 exp_loc = c_parser_peek_token (parser)->location;
6952 op = c_parser_cast_expression (parser, NULL);
6953 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6954 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6955 case CPP_MINUS:
6956 c_parser_consume_token (parser);
6957 exp_loc = c_parser_peek_token (parser)->location;
6958 op = c_parser_cast_expression (parser, NULL);
6959 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6960 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6961 case CPP_COMPL:
6962 c_parser_consume_token (parser);
6963 exp_loc = c_parser_peek_token (parser)->location;
6964 op = c_parser_cast_expression (parser, NULL);
6965 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6966 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6967 case CPP_NOT:
6968 c_parser_consume_token (parser);
6969 exp_loc = c_parser_peek_token (parser)->location;
6970 op = c_parser_cast_expression (parser, NULL);
6971 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6972 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6973 case CPP_AND_AND:
6974 /* Refer to the address of a label as a pointer. */
6975 c_parser_consume_token (parser);
6976 if (c_parser_next_token_is (parser, CPP_NAME))
6978 ret.value = finish_label_address_expr
6979 (c_parser_peek_token (parser)->value, op_loc);
6980 set_c_expr_source_range (&ret, op_loc,
6981 c_parser_peek_token (parser)->get_finish ());
6982 c_parser_consume_token (parser);
6984 else
6986 c_parser_error (parser, "expected identifier");
6987 ret.value = error_mark_node;
6989 return ret;
6990 case CPP_KEYWORD:
6991 switch (c_parser_peek_token (parser)->keyword)
6993 case RID_SIZEOF:
6994 return c_parser_sizeof_expression (parser);
6995 case RID_ALIGNOF:
6996 return c_parser_alignof_expression (parser);
6997 case RID_EXTENSION:
6998 c_parser_consume_token (parser);
6999 ext = disable_extension_diagnostics ();
7000 ret = c_parser_cast_expression (parser, NULL);
7001 restore_extension_diagnostics (ext);
7002 return ret;
7003 case RID_REALPART:
7004 c_parser_consume_token (parser);
7005 exp_loc = c_parser_peek_token (parser)->location;
7006 op = c_parser_cast_expression (parser, NULL);
7007 op = default_function_array_conversion (exp_loc, op);
7008 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7009 case RID_IMAGPART:
7010 c_parser_consume_token (parser);
7011 exp_loc = c_parser_peek_token (parser)->location;
7012 op = c_parser_cast_expression (parser, NULL);
7013 op = default_function_array_conversion (exp_loc, op);
7014 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7015 case RID_TRANSACTION_ATOMIC:
7016 case RID_TRANSACTION_RELAXED:
7017 return c_parser_transaction_expression (parser,
7018 c_parser_peek_token (parser)->keyword);
7019 default:
7020 return c_parser_postfix_expression (parser);
7022 default:
7023 return c_parser_postfix_expression (parser);
7027 /* Parse a sizeof expression. */
7029 static struct c_expr
7030 c_parser_sizeof_expression (c_parser *parser)
7032 struct c_expr expr;
7033 struct c_expr result;
7034 location_t expr_loc;
7035 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7037 location_t start;
7038 location_t finish = UNKNOWN_LOCATION;
7040 start = c_parser_peek_token (parser)->location;
7042 c_parser_consume_token (parser);
7043 c_inhibit_evaluation_warnings++;
7044 in_sizeof++;
7045 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7046 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7048 /* Either sizeof ( type-name ) or sizeof unary-expression
7049 starting with a compound literal. */
7050 struct c_type_name *type_name;
7051 c_parser_consume_token (parser);
7052 expr_loc = c_parser_peek_token (parser)->location;
7053 type_name = c_parser_type_name (parser);
7054 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7055 finish = parser->tokens_buf[0].location;
7056 if (type_name == NULL)
7058 struct c_expr ret;
7059 c_inhibit_evaluation_warnings--;
7060 in_sizeof--;
7061 ret.value = error_mark_node;
7062 ret.original_code = ERROR_MARK;
7063 ret.original_type = NULL;
7064 return ret;
7066 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7068 expr = c_parser_postfix_expression_after_paren_type (parser,
7069 type_name,
7070 expr_loc);
7071 finish = expr.get_finish ();
7072 goto sizeof_expr;
7074 /* sizeof ( type-name ). */
7075 c_inhibit_evaluation_warnings--;
7076 in_sizeof--;
7077 result = c_expr_sizeof_type (expr_loc, type_name);
7079 else
7081 expr_loc = c_parser_peek_token (parser)->location;
7082 expr = c_parser_unary_expression (parser);
7083 finish = expr.get_finish ();
7084 sizeof_expr:
7085 c_inhibit_evaluation_warnings--;
7086 in_sizeof--;
7087 mark_exp_read (expr.value);
7088 if (TREE_CODE (expr.value) == COMPONENT_REF
7089 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7090 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7091 result = c_expr_sizeof_expr (expr_loc, expr);
7093 if (finish != UNKNOWN_LOCATION)
7094 set_c_expr_source_range (&result, start, finish);
7095 return result;
7098 /* Parse an alignof expression. */
7100 static struct c_expr
7101 c_parser_alignof_expression (c_parser *parser)
7103 struct c_expr expr;
7104 location_t start_loc = c_parser_peek_token (parser)->location;
7105 location_t end_loc;
7106 tree alignof_spelling = c_parser_peek_token (parser)->value;
7107 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7108 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7109 "_Alignof") == 0;
7110 /* A diagnostic is not required for the use of this identifier in
7111 the implementation namespace; only diagnose it for the C11
7112 spelling because of existing code using the other spellings. */
7113 if (is_c11_alignof)
7115 if (flag_isoc99)
7116 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7117 alignof_spelling);
7118 else
7119 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7120 alignof_spelling);
7122 c_parser_consume_token (parser);
7123 c_inhibit_evaluation_warnings++;
7124 in_alignof++;
7125 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7126 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7128 /* Either __alignof__ ( type-name ) or __alignof__
7129 unary-expression starting with a compound literal. */
7130 location_t loc;
7131 struct c_type_name *type_name;
7132 struct c_expr ret;
7133 c_parser_consume_token (parser);
7134 loc = c_parser_peek_token (parser)->location;
7135 type_name = c_parser_type_name (parser);
7136 end_loc = c_parser_peek_token (parser)->location;
7137 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7138 if (type_name == NULL)
7140 struct c_expr ret;
7141 c_inhibit_evaluation_warnings--;
7142 in_alignof--;
7143 ret.value = error_mark_node;
7144 ret.original_code = ERROR_MARK;
7145 ret.original_type = NULL;
7146 return ret;
7148 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7150 expr = c_parser_postfix_expression_after_paren_type (parser,
7151 type_name,
7152 loc);
7153 goto alignof_expr;
7155 /* alignof ( type-name ). */
7156 c_inhibit_evaluation_warnings--;
7157 in_alignof--;
7158 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7159 NULL, NULL),
7160 false, is_c11_alignof, 1);
7161 ret.original_code = ERROR_MARK;
7162 ret.original_type = NULL;
7163 set_c_expr_source_range (&ret, start_loc, end_loc);
7164 return ret;
7166 else
7168 struct c_expr ret;
7169 expr = c_parser_unary_expression (parser);
7170 end_loc = expr.src_range.m_finish;
7171 alignof_expr:
7172 mark_exp_read (expr.value);
7173 c_inhibit_evaluation_warnings--;
7174 in_alignof--;
7175 if (is_c11_alignof)
7176 pedwarn (start_loc,
7177 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7178 alignof_spelling);
7179 ret.value = c_alignof_expr (start_loc, expr.value);
7180 ret.original_code = ERROR_MARK;
7181 ret.original_type = NULL;
7182 set_c_expr_source_range (&ret, start_loc, end_loc);
7183 return ret;
7187 /* Helper function to read arguments of builtins which are interfaces
7188 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7189 others. The name of the builtin is passed using BNAME parameter.
7190 Function returns true if there were no errors while parsing and
7191 stores the arguments in CEXPR_LIST. If it returns true,
7192 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7193 parenthesis. */
7194 static bool
7195 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7196 vec<c_expr_t, va_gc> **ret_cexpr_list,
7197 bool choose_expr_p,
7198 location_t *out_close_paren_loc)
7200 location_t loc = c_parser_peek_token (parser)->location;
7201 vec<c_expr_t, va_gc> *cexpr_list;
7202 c_expr_t expr;
7203 bool saved_force_folding_builtin_constant_p;
7205 *ret_cexpr_list = NULL;
7206 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7208 error_at (loc, "cannot take address of %qs", bname);
7209 return false;
7212 c_parser_consume_token (parser);
7214 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7216 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7217 c_parser_consume_token (parser);
7218 return true;
7221 saved_force_folding_builtin_constant_p
7222 = force_folding_builtin_constant_p;
7223 force_folding_builtin_constant_p |= choose_expr_p;
7224 expr = c_parser_expr_no_commas (parser, NULL);
7225 force_folding_builtin_constant_p
7226 = saved_force_folding_builtin_constant_p;
7227 vec_alloc (cexpr_list, 1);
7228 vec_safe_push (cexpr_list, expr);
7229 while (c_parser_next_token_is (parser, CPP_COMMA))
7231 c_parser_consume_token (parser);
7232 expr = c_parser_expr_no_commas (parser, NULL);
7233 vec_safe_push (cexpr_list, expr);
7236 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7237 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7238 return false;
7240 *ret_cexpr_list = cexpr_list;
7241 return true;
7244 /* This represents a single generic-association. */
7246 struct c_generic_association
7248 /* The location of the starting token of the type. */
7249 location_t type_location;
7250 /* The association's type, or NULL_TREE for 'default'. */
7251 tree type;
7252 /* The association's expression. */
7253 struct c_expr expression;
7256 /* Parse a generic-selection. (C11 6.5.1.1).
7258 generic-selection:
7259 _Generic ( assignment-expression , generic-assoc-list )
7261 generic-assoc-list:
7262 generic-association
7263 generic-assoc-list , generic-association
7265 generic-association:
7266 type-name : assignment-expression
7267 default : assignment-expression
7270 static struct c_expr
7271 c_parser_generic_selection (c_parser *parser)
7273 struct c_expr selector, error_expr;
7274 tree selector_type;
7275 struct c_generic_association matched_assoc;
7276 bool match_found = false;
7277 location_t generic_loc, selector_loc;
7279 error_expr.original_code = ERROR_MARK;
7280 error_expr.original_type = NULL;
7281 error_expr.set_error ();
7282 matched_assoc.type_location = UNKNOWN_LOCATION;
7283 matched_assoc.type = NULL_TREE;
7284 matched_assoc.expression = error_expr;
7286 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7287 generic_loc = c_parser_peek_token (parser)->location;
7288 c_parser_consume_token (parser);
7289 if (flag_isoc99)
7290 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7291 "ISO C99 does not support %<_Generic%>");
7292 else
7293 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7294 "ISO C90 does not support %<_Generic%>");
7296 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7297 return error_expr;
7299 c_inhibit_evaluation_warnings++;
7300 selector_loc = c_parser_peek_token (parser)->location;
7301 selector = c_parser_expr_no_commas (parser, NULL);
7302 selector = default_function_array_conversion (selector_loc, selector);
7303 c_inhibit_evaluation_warnings--;
7305 if (selector.value == error_mark_node)
7307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7308 return selector;
7310 selector_type = TREE_TYPE (selector.value);
7311 /* In ISO C terms, rvalues (including the controlling expression of
7312 _Generic) do not have qualified types. */
7313 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7314 selector_type = TYPE_MAIN_VARIANT (selector_type);
7315 /* In ISO C terms, _Noreturn is not part of the type of expressions
7316 such as &abort, but in GCC it is represented internally as a type
7317 qualifier. */
7318 if (FUNCTION_POINTER_TYPE_P (selector_type)
7319 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7320 selector_type
7321 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7323 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7326 return error_expr;
7329 auto_vec<c_generic_association> associations;
7330 while (1)
7332 struct c_generic_association assoc, *iter;
7333 unsigned int ix;
7334 c_token *token = c_parser_peek_token (parser);
7336 assoc.type_location = token->location;
7337 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7339 c_parser_consume_token (parser);
7340 assoc.type = NULL_TREE;
7342 else
7344 struct c_type_name *type_name;
7346 type_name = c_parser_type_name (parser);
7347 if (type_name == NULL)
7349 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7350 return error_expr;
7352 assoc.type = groktypename (type_name, NULL, NULL);
7353 if (assoc.type == error_mark_node)
7355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7356 return error_expr;
7359 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7360 error_at (assoc.type_location,
7361 "%<_Generic%> association has function type");
7362 else if (!COMPLETE_TYPE_P (assoc.type))
7363 error_at (assoc.type_location,
7364 "%<_Generic%> association has incomplete type");
7366 if (variably_modified_type_p (assoc.type, NULL_TREE))
7367 error_at (assoc.type_location,
7368 "%<_Generic%> association has "
7369 "variable length type");
7372 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7374 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7375 return error_expr;
7378 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7379 if (assoc.expression.value == error_mark_node)
7381 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7382 return error_expr;
7385 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7387 if (assoc.type == NULL_TREE)
7389 if (iter->type == NULL_TREE)
7391 error_at (assoc.type_location,
7392 "duplicate %<default%> case in %<_Generic%>");
7393 inform (iter->type_location, "original %<default%> is here");
7396 else if (iter->type != NULL_TREE)
7398 if (comptypes (assoc.type, iter->type))
7400 error_at (assoc.type_location,
7401 "%<_Generic%> specifies two compatible types");
7402 inform (iter->type_location, "compatible type is here");
7407 if (assoc.type == NULL_TREE)
7409 if (!match_found)
7411 matched_assoc = assoc;
7412 match_found = true;
7415 else if (comptypes (assoc.type, selector_type))
7417 if (!match_found || matched_assoc.type == NULL_TREE)
7419 matched_assoc = assoc;
7420 match_found = true;
7422 else
7424 error_at (assoc.type_location,
7425 "%<_Generic> selector matches multiple associations");
7426 inform (matched_assoc.type_location,
7427 "other match is here");
7431 associations.safe_push (assoc);
7433 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7434 break;
7435 c_parser_consume_token (parser);
7438 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7440 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7441 return error_expr;
7444 if (!match_found)
7446 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7447 "compatible with any association",
7448 selector_type);
7449 return error_expr;
7452 return matched_assoc.expression;
7455 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7457 postfix-expression:
7458 primary-expression
7459 postfix-expression [ expression ]
7460 postfix-expression ( argument-expression-list[opt] )
7461 postfix-expression . identifier
7462 postfix-expression -> identifier
7463 postfix-expression ++
7464 postfix-expression --
7465 ( type-name ) { initializer-list }
7466 ( type-name ) { initializer-list , }
7468 argument-expression-list:
7469 argument-expression
7470 argument-expression-list , argument-expression
7472 primary-expression:
7473 identifier
7474 constant
7475 string-literal
7476 ( expression )
7477 generic-selection
7479 GNU extensions:
7481 primary-expression:
7482 __func__
7483 (treated as a keyword in GNU C)
7484 __FUNCTION__
7485 __PRETTY_FUNCTION__
7486 ( compound-statement )
7487 __builtin_va_arg ( assignment-expression , type-name )
7488 __builtin_offsetof ( type-name , offsetof-member-designator )
7489 __builtin_choose_expr ( assignment-expression ,
7490 assignment-expression ,
7491 assignment-expression )
7492 __builtin_types_compatible_p ( type-name , type-name )
7493 __builtin_complex ( assignment-expression , assignment-expression )
7494 __builtin_shuffle ( assignment-expression , assignment-expression )
7495 __builtin_shuffle ( assignment-expression ,
7496 assignment-expression ,
7497 assignment-expression, )
7499 offsetof-member-designator:
7500 identifier
7501 offsetof-member-designator . identifier
7502 offsetof-member-designator [ expression ]
7504 Objective-C:
7506 primary-expression:
7507 [ objc-receiver objc-message-args ]
7508 @selector ( objc-selector-arg )
7509 @protocol ( identifier )
7510 @encode ( type-name )
7511 objc-string-literal
7512 Classname . identifier
7515 static struct c_expr
7516 c_parser_postfix_expression (c_parser *parser)
7518 struct c_expr expr, e1;
7519 struct c_type_name *t1, *t2;
7520 location_t loc = c_parser_peek_token (parser)->location;;
7521 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7522 expr.original_code = ERROR_MARK;
7523 expr.original_type = NULL;
7524 switch (c_parser_peek_token (parser)->type)
7526 case CPP_NUMBER:
7527 expr.value = c_parser_peek_token (parser)->value;
7528 set_c_expr_source_range (&expr, tok_range);
7529 loc = c_parser_peek_token (parser)->location;
7530 c_parser_consume_token (parser);
7531 if (TREE_CODE (expr.value) == FIXED_CST
7532 && !targetm.fixed_point_supported_p ())
7534 error_at (loc, "fixed-point types not supported for this target");
7535 expr.value = error_mark_node;
7537 break;
7538 case CPP_CHAR:
7539 case CPP_CHAR16:
7540 case CPP_CHAR32:
7541 case CPP_WCHAR:
7542 expr.value = c_parser_peek_token (parser)->value;
7543 /* For the purpose of warning when a pointer is compared with
7544 a zero character constant. */
7545 expr.original_type = char_type_node;
7546 set_c_expr_source_range (&expr, tok_range);
7547 c_parser_consume_token (parser);
7548 break;
7549 case CPP_STRING:
7550 case CPP_STRING16:
7551 case CPP_STRING32:
7552 case CPP_WSTRING:
7553 case CPP_UTF8STRING:
7554 expr.value = c_parser_peek_token (parser)->value;
7555 set_c_expr_source_range (&expr, tok_range);
7556 expr.original_code = STRING_CST;
7557 c_parser_consume_token (parser);
7558 break;
7559 case CPP_OBJC_STRING:
7560 gcc_assert (c_dialect_objc ());
7561 expr.value
7562 = objc_build_string_object (c_parser_peek_token (parser)->value);
7563 set_c_expr_source_range (&expr, tok_range);
7564 c_parser_consume_token (parser);
7565 break;
7566 case CPP_NAME:
7567 switch (c_parser_peek_token (parser)->id_kind)
7569 case C_ID_ID:
7571 tree id = c_parser_peek_token (parser)->value;
7572 c_parser_consume_token (parser);
7573 expr.value = build_external_ref (loc, id,
7574 (c_parser_peek_token (parser)->type
7575 == CPP_OPEN_PAREN),
7576 &expr.original_type);
7577 set_c_expr_source_range (&expr, tok_range);
7578 break;
7580 case C_ID_CLASSNAME:
7582 /* Here we parse the Objective-C 2.0 Class.name dot
7583 syntax. */
7584 tree class_name = c_parser_peek_token (parser)->value;
7585 tree component;
7586 c_parser_consume_token (parser);
7587 gcc_assert (c_dialect_objc ());
7588 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7590 expr.set_error ();
7591 break;
7593 if (c_parser_next_token_is_not (parser, CPP_NAME))
7595 c_parser_error (parser, "expected identifier");
7596 expr.set_error ();
7597 break;
7599 c_token *component_tok = c_parser_peek_token (parser);
7600 component = component_tok->value;
7601 location_t end_loc = component_tok->get_finish ();
7602 c_parser_consume_token (parser);
7603 expr.value = objc_build_class_component_ref (class_name,
7604 component);
7605 set_c_expr_source_range (&expr, loc, end_loc);
7606 break;
7608 default:
7609 c_parser_error (parser, "expected expression");
7610 expr.set_error ();
7611 break;
7613 break;
7614 case CPP_OPEN_PAREN:
7615 /* A parenthesized expression, statement expression or compound
7616 literal. */
7617 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7619 /* A statement expression. */
7620 tree stmt;
7621 location_t brace_loc;
7622 c_parser_consume_token (parser);
7623 brace_loc = c_parser_peek_token (parser)->location;
7624 c_parser_consume_token (parser);
7625 if (!building_stmt_list_p ())
7627 error_at (loc, "braced-group within expression allowed "
7628 "only inside a function");
7629 parser->error = true;
7630 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7632 expr.set_error ();
7633 break;
7635 stmt = c_begin_stmt_expr ();
7636 c_parser_compound_statement_nostart (parser);
7637 location_t close_loc = c_parser_peek_token (parser)->location;
7638 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7639 "expected %<)%>");
7640 pedwarn (loc, OPT_Wpedantic,
7641 "ISO C forbids braced-groups within expressions");
7642 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7643 set_c_expr_source_range (&expr, loc, close_loc);
7644 mark_exp_read (expr.value);
7646 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7648 /* A compound literal. ??? Can we actually get here rather
7649 than going directly to
7650 c_parser_postfix_expression_after_paren_type from
7651 elsewhere? */
7652 location_t loc;
7653 struct c_type_name *type_name;
7654 c_parser_consume_token (parser);
7655 loc = c_parser_peek_token (parser)->location;
7656 type_name = c_parser_type_name (parser);
7657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7658 "expected %<)%>");
7659 if (type_name == NULL)
7661 expr.set_error ();
7663 else
7664 expr = c_parser_postfix_expression_after_paren_type (parser,
7665 type_name,
7666 loc);
7668 else
7670 /* A parenthesized expression. */
7671 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7672 c_parser_consume_token (parser);
7673 expr = c_parser_expression (parser);
7674 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7675 TREE_NO_WARNING (expr.value) = 1;
7676 if (expr.original_code != C_MAYBE_CONST_EXPR)
7677 expr.original_code = ERROR_MARK;
7678 /* Don't change EXPR.ORIGINAL_TYPE. */
7679 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7680 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7681 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7682 "expected %<)%>");
7684 break;
7685 case CPP_KEYWORD:
7686 switch (c_parser_peek_token (parser)->keyword)
7688 case RID_FUNCTION_NAME:
7689 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7690 "%<__FUNCTION__%> predefined identifier");
7691 expr.value = fname_decl (loc,
7692 c_parser_peek_token (parser)->keyword,
7693 c_parser_peek_token (parser)->value);
7694 set_c_expr_source_range (&expr, loc, loc);
7695 c_parser_consume_token (parser);
7696 break;
7697 case RID_PRETTY_FUNCTION_NAME:
7698 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7699 "%<__PRETTY_FUNCTION__%> predefined identifier");
7700 expr.value = fname_decl (loc,
7701 c_parser_peek_token (parser)->keyword,
7702 c_parser_peek_token (parser)->value);
7703 set_c_expr_source_range (&expr, loc, loc);
7704 c_parser_consume_token (parser);
7705 break;
7706 case RID_C99_FUNCTION_NAME:
7707 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7708 "%<__func__%> predefined identifier");
7709 expr.value = fname_decl (loc,
7710 c_parser_peek_token (parser)->keyword,
7711 c_parser_peek_token (parser)->value);
7712 set_c_expr_source_range (&expr, loc, loc);
7713 c_parser_consume_token (parser);
7714 break;
7715 case RID_VA_ARG:
7717 location_t start_loc = loc;
7718 c_parser_consume_token (parser);
7719 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7721 expr.set_error ();
7722 break;
7724 e1 = c_parser_expr_no_commas (parser, NULL);
7725 mark_exp_read (e1.value);
7726 e1.value = c_fully_fold (e1.value, false, NULL);
7727 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7729 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7730 expr.set_error ();
7731 break;
7733 loc = c_parser_peek_token (parser)->location;
7734 t1 = c_parser_type_name (parser);
7735 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7736 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7737 "expected %<)%>");
7738 if (t1 == NULL)
7740 expr.set_error ();
7742 else
7744 tree type_expr = NULL_TREE;
7745 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7746 groktypename (t1, &type_expr, NULL));
7747 if (type_expr)
7749 expr.value = build2 (C_MAYBE_CONST_EXPR,
7750 TREE_TYPE (expr.value), type_expr,
7751 expr.value);
7752 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7754 set_c_expr_source_range (&expr, start_loc, end_loc);
7757 break;
7758 case RID_OFFSETOF:
7759 c_parser_consume_token (parser);
7760 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7762 expr.set_error ();
7763 break;
7765 t1 = c_parser_type_name (parser);
7766 if (t1 == NULL)
7767 parser->error = true;
7768 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7769 gcc_assert (parser->error);
7770 if (parser->error)
7772 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7773 expr.set_error ();
7774 break;
7778 tree type = groktypename (t1, NULL, NULL);
7779 tree offsetof_ref;
7780 if (type == error_mark_node)
7781 offsetof_ref = error_mark_node;
7782 else
7784 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7785 SET_EXPR_LOCATION (offsetof_ref, loc);
7787 /* Parse the second argument to __builtin_offsetof. We
7788 must have one identifier, and beyond that we want to
7789 accept sub structure and sub array references. */
7790 if (c_parser_next_token_is (parser, CPP_NAME))
7792 c_token *comp_tok = c_parser_peek_token (parser);
7793 offsetof_ref = build_component_ref
7794 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
7795 c_parser_consume_token (parser);
7796 while (c_parser_next_token_is (parser, CPP_DOT)
7797 || c_parser_next_token_is (parser,
7798 CPP_OPEN_SQUARE)
7799 || c_parser_next_token_is (parser,
7800 CPP_DEREF))
7802 if (c_parser_next_token_is (parser, CPP_DEREF))
7804 loc = c_parser_peek_token (parser)->location;
7805 offsetof_ref = build_array_ref (loc,
7806 offsetof_ref,
7807 integer_zero_node);
7808 goto do_dot;
7810 else if (c_parser_next_token_is (parser, CPP_DOT))
7812 do_dot:
7813 c_parser_consume_token (parser);
7814 if (c_parser_next_token_is_not (parser,
7815 CPP_NAME))
7817 c_parser_error (parser, "expected identifier");
7818 break;
7820 c_token *comp_tok = c_parser_peek_token (parser);
7821 offsetof_ref = build_component_ref
7822 (loc, offsetof_ref, comp_tok->value,
7823 comp_tok->location);
7824 c_parser_consume_token (parser);
7826 else
7828 struct c_expr ce;
7829 tree idx;
7830 loc = c_parser_peek_token (parser)->location;
7831 c_parser_consume_token (parser);
7832 ce = c_parser_expression (parser);
7833 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7834 idx = ce.value;
7835 idx = c_fully_fold (idx, false, NULL);
7836 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7837 "expected %<]%>");
7838 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7842 else
7843 c_parser_error (parser, "expected identifier");
7844 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7845 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7846 "expected %<)%>");
7847 expr.value = fold_offsetof (offsetof_ref);
7848 set_c_expr_source_range (&expr, loc, end_loc);
7850 break;
7851 case RID_CHOOSE_EXPR:
7853 vec<c_expr_t, va_gc> *cexpr_list;
7854 c_expr_t *e1_p, *e2_p, *e3_p;
7855 tree c;
7856 location_t close_paren_loc;
7858 c_parser_consume_token (parser);
7859 if (!c_parser_get_builtin_args (parser,
7860 "__builtin_choose_expr",
7861 &cexpr_list, true,
7862 &close_paren_loc))
7864 expr.set_error ();
7865 break;
7868 if (vec_safe_length (cexpr_list) != 3)
7870 error_at (loc, "wrong number of arguments to "
7871 "%<__builtin_choose_expr%>");
7872 expr.set_error ();
7873 break;
7876 e1_p = &(*cexpr_list)[0];
7877 e2_p = &(*cexpr_list)[1];
7878 e3_p = &(*cexpr_list)[2];
7880 c = e1_p->value;
7881 mark_exp_read (e2_p->value);
7882 mark_exp_read (e3_p->value);
7883 if (TREE_CODE (c) != INTEGER_CST
7884 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7885 error_at (loc,
7886 "first argument to %<__builtin_choose_expr%> not"
7887 " a constant");
7888 constant_expression_warning (c);
7889 expr = integer_zerop (c) ? *e3_p : *e2_p;
7890 set_c_expr_source_range (&expr, loc, close_paren_loc);
7891 break;
7893 case RID_TYPES_COMPATIBLE_P:
7894 c_parser_consume_token (parser);
7895 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7897 expr.set_error ();
7898 break;
7900 t1 = c_parser_type_name (parser);
7901 if (t1 == NULL)
7903 expr.set_error ();
7904 break;
7906 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7908 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7909 expr.set_error ();
7910 break;
7912 t2 = c_parser_type_name (parser);
7913 if (t2 == NULL)
7915 expr.set_error ();
7916 break;
7919 location_t close_paren_loc = c_parser_peek_token (parser)->location;
7920 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7921 "expected %<)%>");
7922 tree e1, e2;
7923 e1 = groktypename (t1, NULL, NULL);
7924 e2 = groktypename (t2, NULL, NULL);
7925 if (e1 == error_mark_node || e2 == error_mark_node)
7927 expr.set_error ();
7928 break;
7931 e1 = TYPE_MAIN_VARIANT (e1);
7932 e2 = TYPE_MAIN_VARIANT (e2);
7934 expr.value
7935 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7936 set_c_expr_source_range (&expr, loc, close_paren_loc);
7938 break;
7939 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7941 vec<c_expr_t, va_gc> *cexpr_list;
7942 c_expr_t *e2_p;
7943 tree chain_value;
7944 location_t close_paren_loc;
7946 c_parser_consume_token (parser);
7947 if (!c_parser_get_builtin_args (parser,
7948 "__builtin_call_with_static_chain",
7949 &cexpr_list, false,
7950 &close_paren_loc))
7952 expr.set_error ();
7953 break;
7955 if (vec_safe_length (cexpr_list) != 2)
7957 error_at (loc, "wrong number of arguments to "
7958 "%<__builtin_call_with_static_chain%>");
7959 expr.set_error ();
7960 break;
7963 expr = (*cexpr_list)[0];
7964 e2_p = &(*cexpr_list)[1];
7965 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7966 chain_value = e2_p->value;
7967 mark_exp_read (chain_value);
7969 if (TREE_CODE (expr.value) != CALL_EXPR)
7970 error_at (loc, "first argument to "
7971 "%<__builtin_call_with_static_chain%> "
7972 "must be a call expression");
7973 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7974 error_at (loc, "second argument to "
7975 "%<__builtin_call_with_static_chain%> "
7976 "must be a pointer type");
7977 else
7978 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7979 set_c_expr_source_range (&expr, loc, close_paren_loc);
7980 break;
7982 case RID_BUILTIN_COMPLEX:
7984 vec<c_expr_t, va_gc> *cexpr_list;
7985 c_expr_t *e1_p, *e2_p;
7986 location_t close_paren_loc;
7988 c_parser_consume_token (parser);
7989 if (!c_parser_get_builtin_args (parser,
7990 "__builtin_complex",
7991 &cexpr_list, false,
7992 &close_paren_loc))
7994 expr.set_error ();
7995 break;
7998 if (vec_safe_length (cexpr_list) != 2)
8000 error_at (loc, "wrong number of arguments to "
8001 "%<__builtin_complex%>");
8002 expr.set_error ();
8003 break;
8006 e1_p = &(*cexpr_list)[0];
8007 e2_p = &(*cexpr_list)[1];
8009 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8010 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8011 e1_p->value = convert (TREE_TYPE (e1_p->value),
8012 TREE_OPERAND (e1_p->value, 0));
8013 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8014 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8015 e2_p->value = convert (TREE_TYPE (e2_p->value),
8016 TREE_OPERAND (e2_p->value, 0));
8017 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8018 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8019 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8020 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8022 error_at (loc, "%<__builtin_complex%> operand "
8023 "not of real binary floating-point type");
8024 expr.set_error ();
8025 break;
8027 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8028 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8030 error_at (loc,
8031 "%<__builtin_complex%> operands of different types");
8032 expr.set_error ();
8033 break;
8035 pedwarn_c90 (loc, OPT_Wpedantic,
8036 "ISO C90 does not support complex types");
8037 expr.value = build2_loc (loc, COMPLEX_EXPR,
8038 build_complex_type
8039 (TYPE_MAIN_VARIANT
8040 (TREE_TYPE (e1_p->value))),
8041 e1_p->value, e2_p->value);
8042 set_c_expr_source_range (&expr, loc, close_paren_loc);
8043 break;
8045 case RID_BUILTIN_SHUFFLE:
8047 vec<c_expr_t, va_gc> *cexpr_list;
8048 unsigned int i;
8049 c_expr_t *p;
8050 location_t close_paren_loc;
8052 c_parser_consume_token (parser);
8053 if (!c_parser_get_builtin_args (parser,
8054 "__builtin_shuffle",
8055 &cexpr_list, false,
8056 &close_paren_loc))
8058 expr.set_error ();
8059 break;
8062 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8063 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8065 if (vec_safe_length (cexpr_list) == 2)
8066 expr.value =
8067 c_build_vec_perm_expr
8068 (loc, (*cexpr_list)[0].value,
8069 NULL_TREE, (*cexpr_list)[1].value);
8071 else if (vec_safe_length (cexpr_list) == 3)
8072 expr.value =
8073 c_build_vec_perm_expr
8074 (loc, (*cexpr_list)[0].value,
8075 (*cexpr_list)[1].value,
8076 (*cexpr_list)[2].value);
8077 else
8079 error_at (loc, "wrong number of arguments to "
8080 "%<__builtin_shuffle%>");
8081 expr.set_error ();
8083 set_c_expr_source_range (&expr, loc, close_paren_loc);
8084 break;
8086 case RID_AT_SELECTOR:
8087 gcc_assert (c_dialect_objc ());
8088 c_parser_consume_token (parser);
8089 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8091 expr.set_error ();
8092 break;
8095 tree sel = c_parser_objc_selector_arg (parser);
8096 location_t close_loc = c_parser_peek_token (parser)->location;
8097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8098 "expected %<)%>");
8099 expr.value = objc_build_selector_expr (loc, sel);
8100 set_c_expr_source_range (&expr, loc, close_loc);
8102 break;
8103 case RID_AT_PROTOCOL:
8104 gcc_assert (c_dialect_objc ());
8105 c_parser_consume_token (parser);
8106 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8108 expr.set_error ();
8109 break;
8111 if (c_parser_next_token_is_not (parser, CPP_NAME))
8113 c_parser_error (parser, "expected identifier");
8114 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8115 expr.set_error ();
8116 break;
8119 tree id = c_parser_peek_token (parser)->value;
8120 c_parser_consume_token (parser);
8121 location_t close_loc = c_parser_peek_token (parser)->location;
8122 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8123 "expected %<)%>");
8124 expr.value = objc_build_protocol_expr (id);
8125 set_c_expr_source_range (&expr, loc, close_loc);
8127 break;
8128 case RID_AT_ENCODE:
8129 /* Extension to support C-structures in the archiver. */
8130 gcc_assert (c_dialect_objc ());
8131 c_parser_consume_token (parser);
8132 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8134 expr.set_error ();
8135 break;
8137 t1 = c_parser_type_name (parser);
8138 if (t1 == NULL)
8140 expr.set_error ();
8141 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8142 break;
8145 location_t close_loc = c_parser_peek_token (parser)->location;
8146 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8147 "expected %<)%>");
8148 tree type = groktypename (t1, NULL, NULL);
8149 expr.value = objc_build_encode_expr (type);
8150 set_c_expr_source_range (&expr, loc, close_loc);
8152 break;
8153 case RID_GENERIC:
8154 expr = c_parser_generic_selection (parser);
8155 break;
8156 case RID_CILK_SPAWN:
8157 c_parser_consume_token (parser);
8158 if (!flag_cilkplus)
8160 error_at (loc, "-fcilkplus must be enabled to use "
8161 "%<_Cilk_spawn%>");
8162 expr = c_parser_cast_expression (parser, NULL);
8163 expr.set_error ();
8165 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8167 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
8168 "are not permitted");
8169 /* Now flush out all the _Cilk_spawns. */
8170 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8171 c_parser_consume_token (parser);
8172 expr = c_parser_cast_expression (parser, NULL);
8174 else
8176 expr = c_parser_cast_expression (parser, NULL);
8177 expr.value = build_cilk_spawn (loc, expr.value);
8179 break;
8180 default:
8181 c_parser_error (parser, "expected expression");
8182 expr.set_error ();
8183 break;
8185 break;
8186 case CPP_OPEN_SQUARE:
8187 if (c_dialect_objc ())
8189 tree receiver, args;
8190 c_parser_consume_token (parser);
8191 receiver = c_parser_objc_receiver (parser);
8192 args = c_parser_objc_message_args (parser);
8193 location_t close_loc = c_parser_peek_token (parser)->location;
8194 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8195 "expected %<]%>");
8196 expr.value = objc_build_message_expr (receiver, args);
8197 set_c_expr_source_range (&expr, loc, close_loc);
8198 break;
8200 /* Else fall through to report error. */
8201 /* FALLTHRU */
8202 default:
8203 c_parser_error (parser, "expected expression");
8204 expr.set_error ();
8205 break;
8207 return c_parser_postfix_expression_after_primary
8208 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8211 /* Parse a postfix expression after a parenthesized type name: the
8212 brace-enclosed initializer of a compound literal, possibly followed
8213 by some postfix operators. This is separate because it is not
8214 possible to tell until after the type name whether a cast
8215 expression has a cast or a compound literal, or whether the operand
8216 of sizeof is a parenthesized type name or starts with a compound
8217 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8218 location of the first token after the parentheses around the type
8219 name. */
8221 static struct c_expr
8222 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8223 struct c_type_name *type_name,
8224 location_t type_loc)
8226 tree type;
8227 struct c_expr init;
8228 bool non_const;
8229 struct c_expr expr;
8230 location_t start_loc;
8231 tree type_expr = NULL_TREE;
8232 bool type_expr_const = true;
8233 check_compound_literal_type (type_loc, type_name);
8234 rich_location richloc (line_table, type_loc);
8235 start_init (NULL_TREE, NULL, 0, &richloc);
8236 type = groktypename (type_name, &type_expr, &type_expr_const);
8237 start_loc = c_parser_peek_token (parser)->location;
8238 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8240 error_at (type_loc, "compound literal has variable size");
8241 type = error_mark_node;
8243 init = c_parser_braced_init (parser, type, false, NULL);
8244 finish_init ();
8245 maybe_warn_string_init (type_loc, type, init);
8247 if (type != error_mark_node
8248 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8249 && current_function_decl)
8251 error ("compound literal qualified by address-space qualifier");
8252 type = error_mark_node;
8255 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8256 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8257 ? CONSTRUCTOR_NON_CONST (init.value)
8258 : init.original_code == C_MAYBE_CONST_EXPR);
8259 non_const |= !type_expr_const;
8260 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8261 set_c_expr_source_range (&expr, init.src_range);
8262 expr.original_code = ERROR_MARK;
8263 expr.original_type = NULL;
8264 if (type != error_mark_node
8265 && expr.value != error_mark_node
8266 && type_expr)
8268 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8270 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8271 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8273 else
8275 gcc_assert (!non_const);
8276 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8277 type_expr, expr.value);
8280 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8283 /* Callback function for sizeof_pointer_memaccess_warning to compare
8284 types. */
8286 static bool
8287 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8289 return comptypes (type1, type2) == 1;
8292 /* Parse a postfix expression after the initial primary or compound
8293 literal; that is, parse a series of postfix operators.
8295 EXPR_LOC is the location of the primary expression. */
8297 static struct c_expr
8298 c_parser_postfix_expression_after_primary (c_parser *parser,
8299 location_t expr_loc,
8300 struct c_expr expr)
8302 struct c_expr orig_expr;
8303 tree ident, idx;
8304 location_t sizeof_arg_loc[3], comp_loc;
8305 tree sizeof_arg[3];
8306 unsigned int literal_zero_mask;
8307 unsigned int i;
8308 vec<tree, va_gc> *exprlist;
8309 vec<tree, va_gc> *origtypes = NULL;
8310 vec<location_t> arg_loc = vNULL;
8311 location_t start;
8312 location_t finish;
8314 while (true)
8316 location_t op_loc = c_parser_peek_token (parser)->location;
8317 switch (c_parser_peek_token (parser)->type)
8319 case CPP_OPEN_SQUARE:
8320 /* Array reference. */
8321 c_parser_consume_token (parser);
8322 if (flag_cilkplus
8323 && c_parser_peek_token (parser)->type == CPP_COLON)
8324 /* If we are here, then we have something like this:
8325 Array [ : ]
8327 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8328 expr.value);
8329 else
8331 idx = c_parser_expression (parser).value;
8332 /* Here we have 3 options:
8333 1. Array [EXPR] -- Normal Array call.
8334 2. Array [EXPR : EXPR] -- Array notation without stride.
8335 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8337 For 1, we just handle it just like a normal array expression.
8338 For 2 and 3 we handle it like we handle array notations. The
8339 idx value we have above becomes the initial/start index.
8341 if (flag_cilkplus
8342 && c_parser_peek_token (parser)->type == CPP_COLON)
8343 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8344 expr.value);
8345 else
8347 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8348 "expected %<]%>");
8349 start = expr.get_start ();
8350 finish = parser->tokens_buf[0].location;
8351 expr.value = build_array_ref (op_loc, expr.value, idx);
8352 set_c_expr_source_range (&expr, start, finish);
8355 expr.original_code = ERROR_MARK;
8356 expr.original_type = NULL;
8357 break;
8358 case CPP_OPEN_PAREN:
8359 /* Function call. */
8360 c_parser_consume_token (parser);
8361 for (i = 0; i < 3; i++)
8363 sizeof_arg[i] = NULL_TREE;
8364 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8366 literal_zero_mask = 0;
8367 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8368 exprlist = NULL;
8369 else
8370 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8371 sizeof_arg_loc, sizeof_arg,
8372 &arg_loc, &literal_zero_mask);
8373 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8374 "expected %<)%>");
8375 orig_expr = expr;
8376 mark_exp_read (expr.value);
8377 if (warn_sizeof_pointer_memaccess)
8378 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8379 expr.value, exprlist,
8380 sizeof_arg,
8381 sizeof_ptr_memacc_comptypes);
8382 if (TREE_CODE (expr.value) == FUNCTION_DECL
8383 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8384 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8385 && vec_safe_length (exprlist) == 3)
8387 tree arg0 = (*exprlist)[0];
8388 tree arg2 = (*exprlist)[2];
8389 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
8392 if (TREE_CODE (expr.value) == FUNCTION_DECL && warn_restrict)
8394 unsigned i;
8395 tree arg;
8396 FOR_EACH_VEC_SAFE_ELT (exprlist, i, arg)
8397 TREE_VISITED (arg) = 0;
8399 unsigned param_pos = 0;
8400 function_args_iterator iter;
8401 tree t;
8402 FOREACH_FUNCTION_ARGS (TREE_TYPE (expr.value), t, iter)
8404 if (POINTER_TYPE_P (t) && TYPE_RESTRICT (t)
8405 && !TYPE_READONLY (TREE_TYPE (t)))
8406 warn_for_restrict (param_pos, exprlist);
8407 param_pos++;
8410 FOR_EACH_VEC_SAFE_ELT (exprlist, i, arg)
8411 TREE_VISITED (arg) = 0;
8414 start = expr.get_start ();
8415 finish = parser->tokens_buf[0].get_finish ();
8416 expr.value
8417 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8418 exprlist, origtypes);
8419 set_c_expr_source_range (&expr, start, finish);
8421 expr.original_code = ERROR_MARK;
8422 if (TREE_CODE (expr.value) == INTEGER_CST
8423 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8424 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8425 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8426 expr.original_code = C_MAYBE_CONST_EXPR;
8427 expr.original_type = NULL;
8428 if (exprlist)
8430 release_tree_vector (exprlist);
8431 release_tree_vector (origtypes);
8433 arg_loc.release ();
8434 break;
8435 case CPP_DOT:
8436 /* Structure element reference. */
8437 c_parser_consume_token (parser);
8438 expr = default_function_array_conversion (expr_loc, expr);
8439 if (c_parser_next_token_is (parser, CPP_NAME))
8441 c_token *comp_tok = c_parser_peek_token (parser);
8442 ident = comp_tok->value;
8443 comp_loc = comp_tok->location;
8445 else
8447 c_parser_error (parser, "expected identifier");
8448 expr.set_error ();
8449 expr.original_code = ERROR_MARK;
8450 expr.original_type = NULL;
8451 return expr;
8453 start = expr.get_start ();
8454 finish = c_parser_peek_token (parser)->get_finish ();
8455 c_parser_consume_token (parser);
8456 expr.value = build_component_ref (op_loc, expr.value, ident,
8457 comp_loc);
8458 set_c_expr_source_range (&expr, start, finish);
8459 expr.original_code = ERROR_MARK;
8460 if (TREE_CODE (expr.value) != COMPONENT_REF)
8461 expr.original_type = NULL;
8462 else
8464 /* Remember the original type of a bitfield. */
8465 tree field = TREE_OPERAND (expr.value, 1);
8466 if (TREE_CODE (field) != FIELD_DECL)
8467 expr.original_type = NULL;
8468 else
8469 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8471 break;
8472 case CPP_DEREF:
8473 /* Structure element reference. */
8474 c_parser_consume_token (parser);
8475 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8476 if (c_parser_next_token_is (parser, CPP_NAME))
8478 c_token *comp_tok = c_parser_peek_token (parser);
8479 ident = comp_tok->value;
8480 comp_loc = comp_tok->location;
8482 else
8484 c_parser_error (parser, "expected identifier");
8485 expr.set_error ();
8486 expr.original_code = ERROR_MARK;
8487 expr.original_type = NULL;
8488 return expr;
8490 start = expr.get_start ();
8491 finish = c_parser_peek_token (parser)->get_finish ();
8492 c_parser_consume_token (parser);
8493 expr.value = build_component_ref (op_loc,
8494 build_indirect_ref (op_loc,
8495 expr.value,
8496 RO_ARROW),
8497 ident, comp_loc);
8498 set_c_expr_source_range (&expr, start, finish);
8499 expr.original_code = ERROR_MARK;
8500 if (TREE_CODE (expr.value) != COMPONENT_REF)
8501 expr.original_type = NULL;
8502 else
8504 /* Remember the original type of a bitfield. */
8505 tree field = TREE_OPERAND (expr.value, 1);
8506 if (TREE_CODE (field) != FIELD_DECL)
8507 expr.original_type = NULL;
8508 else
8509 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8511 break;
8512 case CPP_PLUS_PLUS:
8513 /* Postincrement. */
8514 start = expr.get_start ();
8515 finish = c_parser_peek_token (parser)->get_finish ();
8516 c_parser_consume_token (parser);
8517 /* If the expressions have array notations, we expand them. */
8518 if (flag_cilkplus
8519 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8520 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8521 else
8523 expr = default_function_array_read_conversion (expr_loc, expr);
8524 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
8525 expr.value, false);
8527 set_c_expr_source_range (&expr, start, finish);
8528 expr.original_code = ERROR_MARK;
8529 expr.original_type = NULL;
8530 break;
8531 case CPP_MINUS_MINUS:
8532 /* Postdecrement. */
8533 start = expr.get_start ();
8534 finish = c_parser_peek_token (parser)->get_finish ();
8535 c_parser_consume_token (parser);
8536 /* If the expressions have array notations, we expand them. */
8537 if (flag_cilkplus
8538 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8539 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8540 else
8542 expr = default_function_array_read_conversion (expr_loc, expr);
8543 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
8544 expr.value, false);
8546 set_c_expr_source_range (&expr, start, finish);
8547 expr.original_code = ERROR_MARK;
8548 expr.original_type = NULL;
8549 break;
8550 default:
8551 return expr;
8556 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8558 expression:
8559 assignment-expression
8560 expression , assignment-expression
8563 static struct c_expr
8564 c_parser_expression (c_parser *parser)
8566 location_t tloc = c_parser_peek_token (parser)->location;
8567 struct c_expr expr;
8568 expr = c_parser_expr_no_commas (parser, NULL);
8569 if (c_parser_next_token_is (parser, CPP_COMMA))
8570 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8571 while (c_parser_next_token_is (parser, CPP_COMMA))
8573 struct c_expr next;
8574 tree lhsval;
8575 location_t loc = c_parser_peek_token (parser)->location;
8576 location_t expr_loc;
8577 c_parser_consume_token (parser);
8578 expr_loc = c_parser_peek_token (parser)->location;
8579 lhsval = expr.value;
8580 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8581 lhsval = TREE_OPERAND (lhsval, 1);
8582 if (DECL_P (lhsval) || handled_component_p (lhsval))
8583 mark_exp_read (lhsval);
8584 next = c_parser_expr_no_commas (parser, NULL);
8585 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8586 expr.value = build_compound_expr (loc, expr.value, next.value);
8587 expr.original_code = COMPOUND_EXPR;
8588 expr.original_type = next.original_type;
8590 return expr;
8593 /* Parse an expression and convert functions or arrays to pointers and
8594 lvalues to rvalues. */
8596 static struct c_expr
8597 c_parser_expression_conv (c_parser *parser)
8599 struct c_expr expr;
8600 location_t loc = c_parser_peek_token (parser)->location;
8601 expr = c_parser_expression (parser);
8602 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8603 return expr;
8606 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8607 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8609 static inline void
8610 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8611 unsigned int idx)
8613 if (idx >= HOST_BITS_PER_INT)
8614 return;
8616 c_token *tok = c_parser_peek_token (parser);
8617 switch (tok->type)
8619 case CPP_NUMBER:
8620 case CPP_CHAR:
8621 case CPP_WCHAR:
8622 case CPP_CHAR16:
8623 case CPP_CHAR32:
8624 /* If a parameter is literal zero alone, remember it
8625 for -Wmemset-transposed-args warning. */
8626 if (integer_zerop (tok->value)
8627 && !TREE_OVERFLOW (tok->value)
8628 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8629 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8630 *literal_zero_mask |= 1U << idx;
8631 default:
8632 break;
8636 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8637 functions and arrays to pointers and lvalues to rvalues. If
8638 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8639 locations of function arguments into this vector.
8641 nonempty-expr-list:
8642 assignment-expression
8643 nonempty-expr-list , assignment-expression
8646 static vec<tree, va_gc> *
8647 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8648 vec<tree, va_gc> **p_orig_types,
8649 location_t *sizeof_arg_loc, tree *sizeof_arg,
8650 vec<location_t> *locations,
8651 unsigned int *literal_zero_mask)
8653 vec<tree, va_gc> *ret;
8654 vec<tree, va_gc> *orig_types;
8655 struct c_expr expr;
8656 location_t loc = c_parser_peek_token (parser)->location;
8657 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8658 unsigned int idx = 0;
8660 ret = make_tree_vector ();
8661 if (p_orig_types == NULL)
8662 orig_types = NULL;
8663 else
8664 orig_types = make_tree_vector ();
8666 if (sizeof_arg != NULL
8667 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8668 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8669 if (literal_zero_mask)
8670 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8671 expr = c_parser_expr_no_commas (parser, NULL);
8672 if (convert_p)
8673 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8674 if (fold_p)
8675 expr.value = c_fully_fold (expr.value, false, NULL);
8676 ret->quick_push (expr.value);
8677 if (orig_types)
8678 orig_types->quick_push (expr.original_type);
8679 if (locations)
8680 locations->safe_push (loc);
8681 if (sizeof_arg != NULL
8682 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8683 && expr.original_code == SIZEOF_EXPR)
8685 sizeof_arg[0] = c_last_sizeof_arg;
8686 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8688 while (c_parser_next_token_is (parser, CPP_COMMA))
8690 c_parser_consume_token (parser);
8691 loc = c_parser_peek_token (parser)->location;
8692 if (sizeof_arg != NULL
8693 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8694 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8695 else
8696 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8697 if (literal_zero_mask)
8698 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8699 expr = c_parser_expr_no_commas (parser, NULL);
8700 if (convert_p)
8701 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8702 if (fold_p)
8703 expr.value = c_fully_fold (expr.value, false, NULL);
8704 vec_safe_push (ret, expr.value);
8705 if (orig_types)
8706 vec_safe_push (orig_types, expr.original_type);
8707 if (locations)
8708 locations->safe_push (loc);
8709 if (++idx < 3
8710 && sizeof_arg != NULL
8711 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8712 && expr.original_code == SIZEOF_EXPR)
8714 sizeof_arg[idx] = c_last_sizeof_arg;
8715 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8718 if (orig_types)
8719 *p_orig_types = orig_types;
8720 return ret;
8723 /* Parse Objective-C-specific constructs. */
8725 /* Parse an objc-class-definition.
8727 objc-class-definition:
8728 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8729 objc-class-instance-variables[opt] objc-methodprotolist @end
8730 @implementation identifier objc-superclass[opt]
8731 objc-class-instance-variables[opt]
8732 @interface identifier ( identifier ) objc-protocol-refs[opt]
8733 objc-methodprotolist @end
8734 @interface identifier ( ) objc-protocol-refs[opt]
8735 objc-methodprotolist @end
8736 @implementation identifier ( identifier )
8738 objc-superclass:
8739 : identifier
8741 "@interface identifier (" must start "@interface identifier (
8742 identifier ) ...": objc-methodprotolist in the first production may
8743 not start with a parenthesized identifier as a declarator of a data
8744 definition with no declaration specifiers if the objc-superclass,
8745 objc-protocol-refs and objc-class-instance-variables are omitted. */
8747 static void
8748 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8750 bool iface_p;
8751 tree id1;
8752 tree superclass;
8753 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8754 iface_p = true;
8755 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8756 iface_p = false;
8757 else
8758 gcc_unreachable ();
8760 c_parser_consume_token (parser);
8761 if (c_parser_next_token_is_not (parser, CPP_NAME))
8763 c_parser_error (parser, "expected identifier");
8764 return;
8766 id1 = c_parser_peek_token (parser)->value;
8767 c_parser_consume_token (parser);
8768 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8770 /* We have a category or class extension. */
8771 tree id2;
8772 tree proto = NULL_TREE;
8773 c_parser_consume_token (parser);
8774 if (c_parser_next_token_is_not (parser, CPP_NAME))
8776 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8778 /* We have a class extension. */
8779 id2 = NULL_TREE;
8781 else
8783 c_parser_error (parser, "expected identifier or %<)%>");
8784 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8785 return;
8788 else
8790 id2 = c_parser_peek_token (parser)->value;
8791 c_parser_consume_token (parser);
8793 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8794 if (!iface_p)
8796 objc_start_category_implementation (id1, id2);
8797 return;
8799 if (c_parser_next_token_is (parser, CPP_LESS))
8800 proto = c_parser_objc_protocol_refs (parser);
8801 objc_start_category_interface (id1, id2, proto, attributes);
8802 c_parser_objc_methodprotolist (parser);
8803 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8804 objc_finish_interface ();
8805 return;
8807 if (c_parser_next_token_is (parser, CPP_COLON))
8809 c_parser_consume_token (parser);
8810 if (c_parser_next_token_is_not (parser, CPP_NAME))
8812 c_parser_error (parser, "expected identifier");
8813 return;
8815 superclass = c_parser_peek_token (parser)->value;
8816 c_parser_consume_token (parser);
8818 else
8819 superclass = NULL_TREE;
8820 if (iface_p)
8822 tree proto = NULL_TREE;
8823 if (c_parser_next_token_is (parser, CPP_LESS))
8824 proto = c_parser_objc_protocol_refs (parser);
8825 objc_start_class_interface (id1, superclass, proto, attributes);
8827 else
8828 objc_start_class_implementation (id1, superclass);
8829 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8830 c_parser_objc_class_instance_variables (parser);
8831 if (iface_p)
8833 objc_continue_interface ();
8834 c_parser_objc_methodprotolist (parser);
8835 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8836 objc_finish_interface ();
8838 else
8840 objc_continue_implementation ();
8841 return;
8845 /* Parse objc-class-instance-variables.
8847 objc-class-instance-variables:
8848 { objc-instance-variable-decl-list[opt] }
8850 objc-instance-variable-decl-list:
8851 objc-visibility-spec
8852 objc-instance-variable-decl ;
8854 objc-instance-variable-decl-list objc-visibility-spec
8855 objc-instance-variable-decl-list objc-instance-variable-decl ;
8856 objc-instance-variable-decl-list ;
8858 objc-visibility-spec:
8859 @private
8860 @protected
8861 @public
8863 objc-instance-variable-decl:
8864 struct-declaration
8867 static void
8868 c_parser_objc_class_instance_variables (c_parser *parser)
8870 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8871 c_parser_consume_token (parser);
8872 while (c_parser_next_token_is_not (parser, CPP_EOF))
8874 tree decls;
8875 /* Parse any stray semicolon. */
8876 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8878 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8879 "extra semicolon");
8880 c_parser_consume_token (parser);
8881 continue;
8883 /* Stop if at the end of the instance variables. */
8884 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8886 c_parser_consume_token (parser);
8887 break;
8889 /* Parse any objc-visibility-spec. */
8890 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8892 c_parser_consume_token (parser);
8893 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8894 continue;
8896 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8898 c_parser_consume_token (parser);
8899 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8900 continue;
8902 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8904 c_parser_consume_token (parser);
8905 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8906 continue;
8908 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8910 c_parser_consume_token (parser);
8911 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8912 continue;
8914 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8916 c_parser_pragma (parser, pragma_external, NULL);
8917 continue;
8920 /* Parse some comma-separated declarations. */
8921 decls = c_parser_struct_declaration (parser);
8922 if (decls == NULL)
8924 /* There is a syntax error. We want to skip the offending
8925 tokens up to the next ';' (included) or '}'
8926 (excluded). */
8928 /* First, skip manually a ')' or ']'. This is because they
8929 reduce the nesting level, so c_parser_skip_until_found()
8930 wouldn't be able to skip past them. */
8931 c_token *token = c_parser_peek_token (parser);
8932 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8933 c_parser_consume_token (parser);
8935 /* Then, do the standard skipping. */
8936 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8938 /* We hopefully recovered. Start normal parsing again. */
8939 parser->error = false;
8940 continue;
8942 else
8944 /* Comma-separated instance variables are chained together
8945 in reverse order; add them one by one. */
8946 tree ivar = nreverse (decls);
8947 for (; ivar; ivar = DECL_CHAIN (ivar))
8948 objc_add_instance_variable (copy_node (ivar));
8950 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8954 /* Parse an objc-class-declaration.
8956 objc-class-declaration:
8957 @class identifier-list ;
8960 static void
8961 c_parser_objc_class_declaration (c_parser *parser)
8963 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8964 c_parser_consume_token (parser);
8965 /* Any identifiers, including those declared as type names, are OK
8966 here. */
8967 while (true)
8969 tree id;
8970 if (c_parser_next_token_is_not (parser, CPP_NAME))
8972 c_parser_error (parser, "expected identifier");
8973 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8974 parser->error = false;
8975 return;
8977 id = c_parser_peek_token (parser)->value;
8978 objc_declare_class (id);
8979 c_parser_consume_token (parser);
8980 if (c_parser_next_token_is (parser, CPP_COMMA))
8981 c_parser_consume_token (parser);
8982 else
8983 break;
8985 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8988 /* Parse an objc-alias-declaration.
8990 objc-alias-declaration:
8991 @compatibility_alias identifier identifier ;
8994 static void
8995 c_parser_objc_alias_declaration (c_parser *parser)
8997 tree id1, id2;
8998 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8999 c_parser_consume_token (parser);
9000 if (c_parser_next_token_is_not (parser, CPP_NAME))
9002 c_parser_error (parser, "expected identifier");
9003 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9004 return;
9006 id1 = c_parser_peek_token (parser)->value;
9007 c_parser_consume_token (parser);
9008 if (c_parser_next_token_is_not (parser, CPP_NAME))
9010 c_parser_error (parser, "expected identifier");
9011 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9012 return;
9014 id2 = c_parser_peek_token (parser)->value;
9015 c_parser_consume_token (parser);
9016 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9017 objc_declare_alias (id1, id2);
9020 /* Parse an objc-protocol-definition.
9022 objc-protocol-definition:
9023 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9024 @protocol identifier-list ;
9026 "@protocol identifier ;" should be resolved as "@protocol
9027 identifier-list ;": objc-methodprotolist may not start with a
9028 semicolon in the first alternative if objc-protocol-refs are
9029 omitted. */
9031 static void
9032 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9034 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9036 c_parser_consume_token (parser);
9037 if (c_parser_next_token_is_not (parser, CPP_NAME))
9039 c_parser_error (parser, "expected identifier");
9040 return;
9042 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9043 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9045 /* Any identifiers, including those declared as type names, are
9046 OK here. */
9047 while (true)
9049 tree id;
9050 if (c_parser_next_token_is_not (parser, CPP_NAME))
9052 c_parser_error (parser, "expected identifier");
9053 break;
9055 id = c_parser_peek_token (parser)->value;
9056 objc_declare_protocol (id, attributes);
9057 c_parser_consume_token (parser);
9058 if (c_parser_next_token_is (parser, CPP_COMMA))
9059 c_parser_consume_token (parser);
9060 else
9061 break;
9063 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9065 else
9067 tree id = c_parser_peek_token (parser)->value;
9068 tree proto = NULL_TREE;
9069 c_parser_consume_token (parser);
9070 if (c_parser_next_token_is (parser, CPP_LESS))
9071 proto = c_parser_objc_protocol_refs (parser);
9072 parser->objc_pq_context = true;
9073 objc_start_protocol (id, proto, attributes);
9074 c_parser_objc_methodprotolist (parser);
9075 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9076 parser->objc_pq_context = false;
9077 objc_finish_interface ();
9081 /* Parse an objc-method-type.
9083 objc-method-type:
9087 Return true if it is a class method (+) and false if it is
9088 an instance method (-).
9090 static inline bool
9091 c_parser_objc_method_type (c_parser *parser)
9093 switch (c_parser_peek_token (parser)->type)
9095 case CPP_PLUS:
9096 c_parser_consume_token (parser);
9097 return true;
9098 case CPP_MINUS:
9099 c_parser_consume_token (parser);
9100 return false;
9101 default:
9102 gcc_unreachable ();
9106 /* Parse an objc-method-definition.
9108 objc-method-definition:
9109 objc-method-type objc-method-decl ;[opt] compound-statement
9112 static void
9113 c_parser_objc_method_definition (c_parser *parser)
9115 bool is_class_method = c_parser_objc_method_type (parser);
9116 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9117 parser->objc_pq_context = true;
9118 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9119 &expr);
9120 if (decl == error_mark_node)
9121 return; /* Bail here. */
9123 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9125 c_parser_consume_token (parser);
9126 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9127 "extra semicolon in method definition specified");
9130 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9132 c_parser_error (parser, "expected %<{%>");
9133 return;
9136 parser->objc_pq_context = false;
9137 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9139 add_stmt (c_parser_compound_statement (parser));
9140 objc_finish_method_definition (current_function_decl);
9142 else
9144 /* This code is executed when we find a method definition
9145 outside of an @implementation context (or invalid for other
9146 reasons). Parse the method (to keep going) but do not emit
9147 any code.
9149 c_parser_compound_statement (parser);
9153 /* Parse an objc-methodprotolist.
9155 objc-methodprotolist:
9156 empty
9157 objc-methodprotolist objc-methodproto
9158 objc-methodprotolist declaration
9159 objc-methodprotolist ;
9160 @optional
9161 @required
9163 The declaration is a data definition, which may be missing
9164 declaration specifiers under the same rules and diagnostics as
9165 other data definitions outside functions, and the stray semicolon
9166 is diagnosed the same way as a stray semicolon outside a
9167 function. */
9169 static void
9170 c_parser_objc_methodprotolist (c_parser *parser)
9172 while (true)
9174 /* The list is terminated by @end. */
9175 switch (c_parser_peek_token (parser)->type)
9177 case CPP_SEMICOLON:
9178 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9179 "ISO C does not allow extra %<;%> outside of a function");
9180 c_parser_consume_token (parser);
9181 break;
9182 case CPP_PLUS:
9183 case CPP_MINUS:
9184 c_parser_objc_methodproto (parser);
9185 break;
9186 case CPP_PRAGMA:
9187 c_parser_pragma (parser, pragma_external, NULL);
9188 break;
9189 case CPP_EOF:
9190 return;
9191 default:
9192 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9193 return;
9194 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9195 c_parser_objc_at_property_declaration (parser);
9196 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9198 objc_set_method_opt (true);
9199 c_parser_consume_token (parser);
9201 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9203 objc_set_method_opt (false);
9204 c_parser_consume_token (parser);
9206 else
9207 c_parser_declaration_or_fndef (parser, false, false, true,
9208 false, true, NULL, vNULL);
9209 break;
9214 /* Parse an objc-methodproto.
9216 objc-methodproto:
9217 objc-method-type objc-method-decl ;
9220 static void
9221 c_parser_objc_methodproto (c_parser *parser)
9223 bool is_class_method = c_parser_objc_method_type (parser);
9224 tree decl, attributes = NULL_TREE;
9226 /* Remember protocol qualifiers in prototypes. */
9227 parser->objc_pq_context = true;
9228 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9229 NULL);
9230 /* Forget protocol qualifiers now. */
9231 parser->objc_pq_context = false;
9233 /* Do not allow the presence of attributes to hide an erroneous
9234 method implementation in the interface section. */
9235 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9237 c_parser_error (parser, "expected %<;%>");
9238 return;
9241 if (decl != error_mark_node)
9242 objc_add_method_declaration (is_class_method, decl, attributes);
9244 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9247 /* If we are at a position that method attributes may be present, check that
9248 there are not any parsed already (a syntax error) and then collect any
9249 specified at the current location. Finally, if new attributes were present,
9250 check that the next token is legal ( ';' for decls and '{' for defs). */
9252 static bool
9253 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9255 bool bad = false;
9256 if (*attributes)
9258 c_parser_error (parser,
9259 "method attributes must be specified at the end only");
9260 *attributes = NULL_TREE;
9261 bad = true;
9264 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9265 *attributes = c_parser_attributes (parser);
9267 /* If there were no attributes here, just report any earlier error. */
9268 if (*attributes == NULL_TREE || bad)
9269 return bad;
9271 /* If the attributes are followed by a ; or {, then just report any earlier
9272 error. */
9273 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9274 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9275 return bad;
9277 /* We've got attributes, but not at the end. */
9278 c_parser_error (parser,
9279 "expected %<;%> or %<{%> after method attribute definition");
9280 return true;
9283 /* Parse an objc-method-decl.
9285 objc-method-decl:
9286 ( objc-type-name ) objc-selector
9287 objc-selector
9288 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9289 objc-keyword-selector objc-optparmlist
9290 attributes
9292 objc-keyword-selector:
9293 objc-keyword-decl
9294 objc-keyword-selector objc-keyword-decl
9296 objc-keyword-decl:
9297 objc-selector : ( objc-type-name ) identifier
9298 objc-selector : identifier
9299 : ( objc-type-name ) identifier
9300 : identifier
9302 objc-optparmlist:
9303 objc-optparms objc-optellipsis
9305 objc-optparms:
9306 empty
9307 objc-opt-parms , parameter-declaration
9309 objc-optellipsis:
9310 empty
9311 , ...
9314 static tree
9315 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9316 tree *attributes, tree *expr)
9318 tree type = NULL_TREE;
9319 tree sel;
9320 tree parms = NULL_TREE;
9321 bool ellipsis = false;
9322 bool attr_err = false;
9324 *attributes = NULL_TREE;
9325 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9327 c_parser_consume_token (parser);
9328 type = c_parser_objc_type_name (parser);
9329 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9331 sel = c_parser_objc_selector (parser);
9332 /* If there is no selector, or a colon follows, we have an
9333 objc-keyword-selector. If there is a selector, and a colon does
9334 not follow, that selector ends the objc-method-decl. */
9335 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9337 tree tsel = sel;
9338 tree list = NULL_TREE;
9339 while (true)
9341 tree atype = NULL_TREE, id, keyworddecl;
9342 tree param_attr = NULL_TREE;
9343 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9344 break;
9345 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9347 c_parser_consume_token (parser);
9348 atype = c_parser_objc_type_name (parser);
9349 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9350 "expected %<)%>");
9352 /* New ObjC allows attributes on method parameters. */
9353 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9354 param_attr = c_parser_attributes (parser);
9355 if (c_parser_next_token_is_not (parser, CPP_NAME))
9357 c_parser_error (parser, "expected identifier");
9358 return error_mark_node;
9360 id = c_parser_peek_token (parser)->value;
9361 c_parser_consume_token (parser);
9362 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9363 list = chainon (list, keyworddecl);
9364 tsel = c_parser_objc_selector (parser);
9365 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9366 break;
9369 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9371 /* Parse the optional parameter list. Optional Objective-C
9372 method parameters follow the C syntax, and may include '...'
9373 to denote a variable number of arguments. */
9374 parms = make_node (TREE_LIST);
9375 while (c_parser_next_token_is (parser, CPP_COMMA))
9377 struct c_parm *parm;
9378 c_parser_consume_token (parser);
9379 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9381 ellipsis = true;
9382 c_parser_consume_token (parser);
9383 attr_err |= c_parser_objc_maybe_method_attributes
9384 (parser, attributes) ;
9385 break;
9387 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9388 if (parm == NULL)
9389 break;
9390 parms = chainon (parms,
9391 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9393 sel = list;
9395 else
9396 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9398 if (sel == NULL)
9400 c_parser_error (parser, "objective-c method declaration is expected");
9401 return error_mark_node;
9404 if (attr_err)
9405 return error_mark_node;
9407 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9410 /* Parse an objc-type-name.
9412 objc-type-name:
9413 objc-type-qualifiers[opt] type-name
9414 objc-type-qualifiers[opt]
9416 objc-type-qualifiers:
9417 objc-type-qualifier
9418 objc-type-qualifiers objc-type-qualifier
9420 objc-type-qualifier: one of
9421 in out inout bycopy byref oneway
9424 static tree
9425 c_parser_objc_type_name (c_parser *parser)
9427 tree quals = NULL_TREE;
9428 struct c_type_name *type_name = NULL;
9429 tree type = NULL_TREE;
9430 while (true)
9432 c_token *token = c_parser_peek_token (parser);
9433 if (token->type == CPP_KEYWORD
9434 && (token->keyword == RID_IN
9435 || token->keyword == RID_OUT
9436 || token->keyword == RID_INOUT
9437 || token->keyword == RID_BYCOPY
9438 || token->keyword == RID_BYREF
9439 || token->keyword == RID_ONEWAY))
9441 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9442 c_parser_consume_token (parser);
9444 else
9445 break;
9447 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9448 type_name = c_parser_type_name (parser);
9449 if (type_name)
9450 type = groktypename (type_name, NULL, NULL);
9452 /* If the type is unknown, and error has already been produced and
9453 we need to recover from the error. In that case, use NULL_TREE
9454 for the type, as if no type had been specified; this will use the
9455 default type ('id') which is good for error recovery. */
9456 if (type == error_mark_node)
9457 type = NULL_TREE;
9459 return build_tree_list (quals, type);
9462 /* Parse objc-protocol-refs.
9464 objc-protocol-refs:
9465 < identifier-list >
9468 static tree
9469 c_parser_objc_protocol_refs (c_parser *parser)
9471 tree list = NULL_TREE;
9472 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9473 c_parser_consume_token (parser);
9474 /* Any identifiers, including those declared as type names, are OK
9475 here. */
9476 while (true)
9478 tree id;
9479 if (c_parser_next_token_is_not (parser, CPP_NAME))
9481 c_parser_error (parser, "expected identifier");
9482 break;
9484 id = c_parser_peek_token (parser)->value;
9485 list = chainon (list, build_tree_list (NULL_TREE, id));
9486 c_parser_consume_token (parser);
9487 if (c_parser_next_token_is (parser, CPP_COMMA))
9488 c_parser_consume_token (parser);
9489 else
9490 break;
9492 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9493 return list;
9496 /* Parse an objc-try-catch-finally-statement.
9498 objc-try-catch-finally-statement:
9499 @try compound-statement objc-catch-list[opt]
9500 @try compound-statement objc-catch-list[opt] @finally compound-statement
9502 objc-catch-list:
9503 @catch ( objc-catch-parameter-declaration ) compound-statement
9504 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9506 objc-catch-parameter-declaration:
9507 parameter-declaration
9508 '...'
9510 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9512 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9513 for C++. Keep them in sync. */
9515 static void
9516 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9518 location_t location;
9519 tree stmt;
9521 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9522 c_parser_consume_token (parser);
9523 location = c_parser_peek_token (parser)->location;
9524 objc_maybe_warn_exceptions (location);
9525 stmt = c_parser_compound_statement (parser);
9526 objc_begin_try_stmt (location, stmt);
9528 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9530 struct c_parm *parm;
9531 tree parameter_declaration = error_mark_node;
9532 bool seen_open_paren = false;
9534 c_parser_consume_token (parser);
9535 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9536 seen_open_paren = true;
9537 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9539 /* We have "@catch (...)" (where the '...' are literally
9540 what is in the code). Skip the '...'.
9541 parameter_declaration is set to NULL_TREE, and
9542 objc_being_catch_clauses() knows that that means
9543 '...'. */
9544 c_parser_consume_token (parser);
9545 parameter_declaration = NULL_TREE;
9547 else
9549 /* We have "@catch (NSException *exception)" or something
9550 like that. Parse the parameter declaration. */
9551 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9552 if (parm == NULL)
9553 parameter_declaration = error_mark_node;
9554 else
9555 parameter_declaration = grokparm (parm, NULL);
9557 if (seen_open_paren)
9558 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9559 else
9561 /* If there was no open parenthesis, we are recovering from
9562 an error, and we are trying to figure out what mistake
9563 the user has made. */
9565 /* If there is an immediate closing parenthesis, the user
9566 probably forgot the opening one (ie, they typed "@catch
9567 NSException *e)". Parse the closing parenthesis and keep
9568 going. */
9569 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9570 c_parser_consume_token (parser);
9572 /* If these is no immediate closing parenthesis, the user
9573 probably doesn't know that parenthesis are required at
9574 all (ie, they typed "@catch NSException *e"). So, just
9575 forget about the closing parenthesis and keep going. */
9577 objc_begin_catch_clause (parameter_declaration);
9578 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9579 c_parser_compound_statement_nostart (parser);
9580 objc_finish_catch_clause ();
9582 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9584 c_parser_consume_token (parser);
9585 location = c_parser_peek_token (parser)->location;
9586 stmt = c_parser_compound_statement (parser);
9587 objc_build_finally_clause (location, stmt);
9589 objc_finish_try_stmt ();
9592 /* Parse an objc-synchronized-statement.
9594 objc-synchronized-statement:
9595 @synchronized ( expression ) compound-statement
9598 static void
9599 c_parser_objc_synchronized_statement (c_parser *parser)
9601 location_t loc;
9602 tree expr, stmt;
9603 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9604 c_parser_consume_token (parser);
9605 loc = c_parser_peek_token (parser)->location;
9606 objc_maybe_warn_exceptions (loc);
9607 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9609 struct c_expr ce = c_parser_expression (parser);
9610 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9611 expr = ce.value;
9612 expr = c_fully_fold (expr, false, NULL);
9613 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9615 else
9616 expr = error_mark_node;
9617 stmt = c_parser_compound_statement (parser);
9618 objc_build_synchronized (loc, expr, stmt);
9621 /* Parse an objc-selector; return NULL_TREE without an error if the
9622 next token is not an objc-selector.
9624 objc-selector:
9625 identifier
9626 one of
9627 enum struct union if else while do for switch case default
9628 break continue return goto asm sizeof typeof __alignof
9629 unsigned long const short volatile signed restrict _Complex
9630 in out inout bycopy byref oneway int char float double void _Bool
9631 _Atomic
9633 ??? Why this selection of keywords but not, for example, storage
9634 class specifiers? */
9636 static tree
9637 c_parser_objc_selector (c_parser *parser)
9639 c_token *token = c_parser_peek_token (parser);
9640 tree value = token->value;
9641 if (token->type == CPP_NAME)
9643 c_parser_consume_token (parser);
9644 return value;
9646 if (token->type != CPP_KEYWORD)
9647 return NULL_TREE;
9648 switch (token->keyword)
9650 case RID_ENUM:
9651 case RID_STRUCT:
9652 case RID_UNION:
9653 case RID_IF:
9654 case RID_ELSE:
9655 case RID_WHILE:
9656 case RID_DO:
9657 case RID_FOR:
9658 case RID_SWITCH:
9659 case RID_CASE:
9660 case RID_DEFAULT:
9661 case RID_BREAK:
9662 case RID_CONTINUE:
9663 case RID_RETURN:
9664 case RID_GOTO:
9665 case RID_ASM:
9666 case RID_SIZEOF:
9667 case RID_TYPEOF:
9668 case RID_ALIGNOF:
9669 case RID_UNSIGNED:
9670 case RID_LONG:
9671 case RID_CONST:
9672 case RID_SHORT:
9673 case RID_VOLATILE:
9674 case RID_SIGNED:
9675 case RID_RESTRICT:
9676 case RID_COMPLEX:
9677 case RID_IN:
9678 case RID_OUT:
9679 case RID_INOUT:
9680 case RID_BYCOPY:
9681 case RID_BYREF:
9682 case RID_ONEWAY:
9683 case RID_INT:
9684 case RID_CHAR:
9685 case RID_FLOAT:
9686 case RID_DOUBLE:
9687 CASE_RID_FLOATN_NX:
9688 case RID_VOID:
9689 case RID_BOOL:
9690 case RID_ATOMIC:
9691 case RID_AUTO_TYPE:
9692 case RID_INT_N_0:
9693 case RID_INT_N_1:
9694 case RID_INT_N_2:
9695 case RID_INT_N_3:
9696 c_parser_consume_token (parser);
9697 return value;
9698 default:
9699 return NULL_TREE;
9703 /* Parse an objc-selector-arg.
9705 objc-selector-arg:
9706 objc-selector
9707 objc-keywordname-list
9709 objc-keywordname-list:
9710 objc-keywordname
9711 objc-keywordname-list objc-keywordname
9713 objc-keywordname:
9714 objc-selector :
9718 static tree
9719 c_parser_objc_selector_arg (c_parser *parser)
9721 tree sel = c_parser_objc_selector (parser);
9722 tree list = NULL_TREE;
9723 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9724 return sel;
9725 while (true)
9727 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9728 return list;
9729 list = chainon (list, build_tree_list (sel, NULL_TREE));
9730 sel = c_parser_objc_selector (parser);
9731 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9732 break;
9734 return list;
9737 /* Parse an objc-receiver.
9739 objc-receiver:
9740 expression
9741 class-name
9742 type-name
9745 static tree
9746 c_parser_objc_receiver (c_parser *parser)
9748 location_t loc = c_parser_peek_token (parser)->location;
9750 if (c_parser_peek_token (parser)->type == CPP_NAME
9751 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9752 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9754 tree id = c_parser_peek_token (parser)->value;
9755 c_parser_consume_token (parser);
9756 return objc_get_class_reference (id);
9758 struct c_expr ce = c_parser_expression (parser);
9759 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9760 return c_fully_fold (ce.value, false, NULL);
9763 /* Parse objc-message-args.
9765 objc-message-args:
9766 objc-selector
9767 objc-keywordarg-list
9769 objc-keywordarg-list:
9770 objc-keywordarg
9771 objc-keywordarg-list objc-keywordarg
9773 objc-keywordarg:
9774 objc-selector : objc-keywordexpr
9775 : objc-keywordexpr
9778 static tree
9779 c_parser_objc_message_args (c_parser *parser)
9781 tree sel = c_parser_objc_selector (parser);
9782 tree list = NULL_TREE;
9783 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9784 return sel;
9785 while (true)
9787 tree keywordexpr;
9788 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9789 return error_mark_node;
9790 keywordexpr = c_parser_objc_keywordexpr (parser);
9791 list = chainon (list, build_tree_list (sel, keywordexpr));
9792 sel = c_parser_objc_selector (parser);
9793 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9794 break;
9796 return list;
9799 /* Parse an objc-keywordexpr.
9801 objc-keywordexpr:
9802 nonempty-expr-list
9805 static tree
9806 c_parser_objc_keywordexpr (c_parser *parser)
9808 tree ret;
9809 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9810 NULL, NULL, NULL, NULL);
9811 if (vec_safe_length (expr_list) == 1)
9813 /* Just return the expression, remove a level of
9814 indirection. */
9815 ret = (*expr_list)[0];
9817 else
9819 /* We have a comma expression, we will collapse later. */
9820 ret = build_tree_list_vec (expr_list);
9822 release_tree_vector (expr_list);
9823 return ret;
9826 /* A check, needed in several places, that ObjC interface, implementation or
9827 method definitions are not prefixed by incorrect items. */
9828 static bool
9829 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9830 struct c_declspecs *specs)
9832 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9833 || specs->typespec_kind != ctsk_none)
9835 c_parser_error (parser,
9836 "no type or storage class may be specified here,");
9837 c_parser_skip_to_end_of_block_or_statement (parser);
9838 return true;
9840 return false;
9843 /* Parse an Objective-C @property declaration. The syntax is:
9845 objc-property-declaration:
9846 '@property' objc-property-attributes[opt] struct-declaration ;
9848 objc-property-attributes:
9849 '(' objc-property-attribute-list ')'
9851 objc-property-attribute-list:
9852 objc-property-attribute
9853 objc-property-attribute-list, objc-property-attribute
9855 objc-property-attribute
9856 'getter' = identifier
9857 'setter' = identifier
9858 'readonly'
9859 'readwrite'
9860 'assign'
9861 'retain'
9862 'copy'
9863 'nonatomic'
9865 For example:
9866 @property NSString *name;
9867 @property (readonly) id object;
9868 @property (retain, nonatomic, getter=getTheName) id name;
9869 @property int a, b, c;
9871 PS: This function is identical to cp_parser_objc_at_propery_declaration
9872 for C++. Keep them in sync. */
9873 static void
9874 c_parser_objc_at_property_declaration (c_parser *parser)
9876 /* The following variables hold the attributes of the properties as
9877 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9878 seen. When we see an attribute, we set them to 'true' (if they
9879 are boolean properties) or to the identifier (if they have an
9880 argument, ie, for getter and setter). Note that here we only
9881 parse the list of attributes, check the syntax and accumulate the
9882 attributes that we find. objc_add_property_declaration() will
9883 then process the information. */
9884 bool property_assign = false;
9885 bool property_copy = false;
9886 tree property_getter_ident = NULL_TREE;
9887 bool property_nonatomic = false;
9888 bool property_readonly = false;
9889 bool property_readwrite = false;
9890 bool property_retain = false;
9891 tree property_setter_ident = NULL_TREE;
9893 /* 'properties' is the list of properties that we read. Usually a
9894 single one, but maybe more (eg, in "@property int a, b, c;" there
9895 are three). */
9896 tree properties;
9897 location_t loc;
9899 loc = c_parser_peek_token (parser)->location;
9900 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9902 c_parser_consume_token (parser); /* Eat '@property'. */
9904 /* Parse the optional attribute list... */
9905 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9907 /* Eat the '(' */
9908 c_parser_consume_token (parser);
9910 /* Property attribute keywords are valid now. */
9911 parser->objc_property_attr_context = true;
9913 while (true)
9915 bool syntax_error = false;
9916 c_token *token = c_parser_peek_token (parser);
9917 enum rid keyword;
9919 if (token->type != CPP_KEYWORD)
9921 if (token->type == CPP_CLOSE_PAREN)
9922 c_parser_error (parser, "expected identifier");
9923 else
9925 c_parser_consume_token (parser);
9926 c_parser_error (parser, "unknown property attribute");
9928 break;
9930 keyword = token->keyword;
9931 c_parser_consume_token (parser);
9932 switch (keyword)
9934 case RID_ASSIGN: property_assign = true; break;
9935 case RID_COPY: property_copy = true; break;
9936 case RID_NONATOMIC: property_nonatomic = true; break;
9937 case RID_READONLY: property_readonly = true; break;
9938 case RID_READWRITE: property_readwrite = true; break;
9939 case RID_RETAIN: property_retain = true; break;
9941 case RID_GETTER:
9942 case RID_SETTER:
9943 if (c_parser_next_token_is_not (parser, CPP_EQ))
9945 if (keyword == RID_GETTER)
9946 c_parser_error (parser,
9947 "missing %<=%> (after %<getter%> attribute)");
9948 else
9949 c_parser_error (parser,
9950 "missing %<=%> (after %<setter%> attribute)");
9951 syntax_error = true;
9952 break;
9954 c_parser_consume_token (parser); /* eat the = */
9955 if (c_parser_next_token_is_not (parser, CPP_NAME))
9957 c_parser_error (parser, "expected identifier");
9958 syntax_error = true;
9959 break;
9961 if (keyword == RID_SETTER)
9963 if (property_setter_ident != NULL_TREE)
9964 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9965 else
9966 property_setter_ident = c_parser_peek_token (parser)->value;
9967 c_parser_consume_token (parser);
9968 if (c_parser_next_token_is_not (parser, CPP_COLON))
9969 c_parser_error (parser, "setter name must terminate with %<:%>");
9970 else
9971 c_parser_consume_token (parser);
9973 else
9975 if (property_getter_ident != NULL_TREE)
9976 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9977 else
9978 property_getter_ident = c_parser_peek_token (parser)->value;
9979 c_parser_consume_token (parser);
9981 break;
9982 default:
9983 c_parser_error (parser, "unknown property attribute");
9984 syntax_error = true;
9985 break;
9988 if (syntax_error)
9989 break;
9991 if (c_parser_next_token_is (parser, CPP_COMMA))
9992 c_parser_consume_token (parser);
9993 else
9994 break;
9996 parser->objc_property_attr_context = false;
9997 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9999 /* ... and the property declaration(s). */
10000 properties = c_parser_struct_declaration (parser);
10002 if (properties == error_mark_node)
10004 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10005 parser->error = false;
10006 return;
10009 if (properties == NULL_TREE)
10010 c_parser_error (parser, "expected identifier");
10011 else
10013 /* Comma-separated properties are chained together in
10014 reverse order; add them one by one. */
10015 properties = nreverse (properties);
10017 for (; properties; properties = TREE_CHAIN (properties))
10018 objc_add_property_declaration (loc, copy_node (properties),
10019 property_readonly, property_readwrite,
10020 property_assign, property_retain,
10021 property_copy, property_nonatomic,
10022 property_getter_ident, property_setter_ident);
10025 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10026 parser->error = false;
10029 /* Parse an Objective-C @synthesize declaration. The syntax is:
10031 objc-synthesize-declaration:
10032 @synthesize objc-synthesize-identifier-list ;
10034 objc-synthesize-identifier-list:
10035 objc-synthesize-identifier
10036 objc-synthesize-identifier-list, objc-synthesize-identifier
10038 objc-synthesize-identifier
10039 identifier
10040 identifier = identifier
10042 For example:
10043 @synthesize MyProperty;
10044 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10046 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10047 for C++. Keep them in sync.
10049 static void
10050 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10052 tree list = NULL_TREE;
10053 location_t loc;
10054 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10055 loc = c_parser_peek_token (parser)->location;
10057 c_parser_consume_token (parser);
10058 while (true)
10060 tree property, ivar;
10061 if (c_parser_next_token_is_not (parser, CPP_NAME))
10063 c_parser_error (parser, "expected identifier");
10064 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10065 /* Once we find the semicolon, we can resume normal parsing.
10066 We have to reset parser->error manually because
10067 c_parser_skip_until_found() won't reset it for us if the
10068 next token is precisely a semicolon. */
10069 parser->error = false;
10070 return;
10072 property = c_parser_peek_token (parser)->value;
10073 c_parser_consume_token (parser);
10074 if (c_parser_next_token_is (parser, CPP_EQ))
10076 c_parser_consume_token (parser);
10077 if (c_parser_next_token_is_not (parser, CPP_NAME))
10079 c_parser_error (parser, "expected identifier");
10080 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10081 parser->error = false;
10082 return;
10084 ivar = c_parser_peek_token (parser)->value;
10085 c_parser_consume_token (parser);
10087 else
10088 ivar = NULL_TREE;
10089 list = chainon (list, build_tree_list (ivar, property));
10090 if (c_parser_next_token_is (parser, CPP_COMMA))
10091 c_parser_consume_token (parser);
10092 else
10093 break;
10095 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10096 objc_add_synthesize_declaration (loc, list);
10099 /* Parse an Objective-C @dynamic declaration. The syntax is:
10101 objc-dynamic-declaration:
10102 @dynamic identifier-list ;
10104 For example:
10105 @dynamic MyProperty;
10106 @dynamic MyProperty, AnotherProperty;
10108 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10109 for C++. Keep them in sync.
10111 static void
10112 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10114 tree list = NULL_TREE;
10115 location_t loc;
10116 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10117 loc = c_parser_peek_token (parser)->location;
10119 c_parser_consume_token (parser);
10120 while (true)
10122 tree property;
10123 if (c_parser_next_token_is_not (parser, CPP_NAME))
10125 c_parser_error (parser, "expected identifier");
10126 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10127 parser->error = false;
10128 return;
10130 property = c_parser_peek_token (parser)->value;
10131 list = chainon (list, build_tree_list (NULL_TREE, property));
10132 c_parser_consume_token (parser);
10133 if (c_parser_next_token_is (parser, CPP_COMMA))
10134 c_parser_consume_token (parser);
10135 else
10136 break;
10138 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10139 objc_add_dynamic_declaration (loc, list);
10143 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10144 should be considered, statements. ALLOW_STMT is true if we're within
10145 the context of a function and such pragmas are to be allowed. Returns
10146 true if we actually parsed such a pragma. */
10148 static bool
10149 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10151 unsigned int id;
10153 id = c_parser_peek_token (parser)->pragma_kind;
10154 gcc_assert (id != PRAGMA_NONE);
10156 switch (id)
10158 case PRAGMA_OACC_DECLARE:
10159 c_parser_oacc_declare (parser);
10160 return false;
10162 case PRAGMA_OACC_ENTER_DATA:
10163 if (context != pragma_compound)
10165 if (context == pragma_stmt)
10166 c_parser_error (parser, "%<#pragma acc enter data%> may only be "
10167 "used in compound statements");
10168 goto bad_stmt;
10170 c_parser_oacc_enter_exit_data (parser, true);
10171 return false;
10173 case PRAGMA_OACC_EXIT_DATA:
10174 if (context != pragma_compound)
10176 if (context == pragma_stmt)
10177 c_parser_error (parser, "%<#pragma acc exit data%> may only be "
10178 "used in compound statements");
10179 goto bad_stmt;
10181 c_parser_oacc_enter_exit_data (parser, false);
10182 return false;
10184 case PRAGMA_OACC_ROUTINE:
10185 if (context != pragma_external)
10187 error_at (c_parser_peek_token (parser)->location,
10188 "%<#pragma acc routine%> must be at file scope");
10189 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10190 return false;
10192 c_parser_oacc_routine (parser, context);
10193 return false;
10195 case PRAGMA_OACC_UPDATE:
10196 if (context != pragma_compound)
10198 if (context == pragma_stmt)
10199 c_parser_error (parser, "%<#pragma acc update%> may only be "
10200 "used in compound statements");
10201 goto bad_stmt;
10203 c_parser_oacc_update (parser);
10204 return false;
10206 case PRAGMA_OMP_BARRIER:
10207 if (context != pragma_compound)
10209 if (context == pragma_stmt)
10210 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
10211 "used in compound statements");
10212 goto bad_stmt;
10214 c_parser_omp_barrier (parser);
10215 return false;
10217 case PRAGMA_OMP_FLUSH:
10218 if (context != pragma_compound)
10220 if (context == pragma_stmt)
10221 c_parser_error (parser, "%<#pragma omp flush%> may only be "
10222 "used in compound statements");
10223 goto bad_stmt;
10225 c_parser_omp_flush (parser);
10226 return false;
10228 case PRAGMA_OMP_TASKWAIT:
10229 if (context != pragma_compound)
10231 if (context == pragma_stmt)
10232 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
10233 "used in compound statements");
10234 goto bad_stmt;
10236 c_parser_omp_taskwait (parser);
10237 return false;
10239 case PRAGMA_OMP_TASKYIELD:
10240 if (context != pragma_compound)
10242 if (context == pragma_stmt)
10243 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
10244 "used in compound statements");
10245 goto bad_stmt;
10247 c_parser_omp_taskyield (parser);
10248 return false;
10250 case PRAGMA_OMP_CANCEL:
10251 if (context != pragma_compound)
10253 if (context == pragma_stmt)
10254 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
10255 "used in compound statements");
10256 goto bad_stmt;
10258 c_parser_omp_cancel (parser);
10259 return false;
10261 case PRAGMA_OMP_CANCELLATION_POINT:
10262 c_parser_omp_cancellation_point (parser, context);
10263 return false;
10265 case PRAGMA_OMP_THREADPRIVATE:
10266 c_parser_omp_threadprivate (parser);
10267 return false;
10269 case PRAGMA_OMP_TARGET:
10270 return c_parser_omp_target (parser, context, if_p);
10272 case PRAGMA_OMP_END_DECLARE_TARGET:
10273 c_parser_omp_end_declare_target (parser);
10274 return false;
10276 case PRAGMA_OMP_SECTION:
10277 error_at (c_parser_peek_token (parser)->location,
10278 "%<#pragma omp section%> may only be used in "
10279 "%<#pragma omp sections%> construct");
10280 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10281 return false;
10283 case PRAGMA_OMP_DECLARE:
10284 c_parser_omp_declare (parser, context);
10285 return false;
10287 case PRAGMA_OMP_ORDERED:
10288 return c_parser_omp_ordered (parser, context, if_p);
10290 case PRAGMA_IVDEP:
10291 c_parser_consume_pragma (parser);
10292 c_parser_skip_to_pragma_eol (parser);
10293 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10294 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10295 && !c_parser_next_token_is_keyword (parser, RID_DO))
10297 c_parser_error (parser, "for, while or do statement expected");
10298 return false;
10300 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10301 c_parser_for_statement (parser, true, if_p);
10302 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10303 c_parser_while_statement (parser, true, if_p);
10304 else
10305 c_parser_do_statement (parser, true);
10306 return false;
10308 case PRAGMA_GCC_PCH_PREPROCESS:
10309 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10310 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10311 return false;
10313 case PRAGMA_CILK_SIMD:
10314 if (!c_parser_cilk_verify_simd (parser, context))
10315 return false;
10316 c_parser_consume_pragma (parser);
10317 c_parser_cilk_simd (parser, if_p);
10318 return false;
10319 case PRAGMA_CILK_GRAINSIZE:
10320 if (!flag_cilkplus)
10322 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10323 " enabled");
10324 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10325 return false;
10327 if (context == pragma_external)
10329 error_at (c_parser_peek_token (parser)->location,
10330 "%<#pragma grainsize%> must be inside a function");
10331 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10332 return false;
10334 c_parser_cilk_grainsize (parser, if_p);
10335 return false;
10337 case PRAGMA_OACC_WAIT:
10338 if (context != pragma_compound)
10340 if (context == pragma_stmt)
10341 c_parser_error (parser, "%<#pragma acc enter data%> may only be "
10342 "used in compound statements");
10343 goto bad_stmt;
10345 /* FALL THROUGH. */
10347 default:
10348 if (id < PRAGMA_FIRST_EXTERNAL)
10350 if (context != pragma_stmt && context != pragma_compound)
10352 bad_stmt:
10353 c_parser_error (parser, "expected declaration specifiers");
10354 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10355 return false;
10357 c_parser_omp_construct (parser, if_p);
10358 return true;
10360 break;
10363 c_parser_consume_pragma (parser);
10364 c_invoke_pragma_handler (id);
10366 /* Skip to EOL, but suppress any error message. Those will have been
10367 generated by the handler routine through calling error, as opposed
10368 to calling c_parser_error. */
10369 parser->error = true;
10370 c_parser_skip_to_pragma_eol (parser);
10372 return false;
10375 /* The interface the pragma parsers have to the lexer. */
10377 enum cpp_ttype
10378 pragma_lex (tree *value, location_t *loc)
10380 c_token *tok = c_parser_peek_token (the_parser);
10381 enum cpp_ttype ret = tok->type;
10383 *value = tok->value;
10384 if (loc)
10385 *loc = tok->location;
10387 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10388 ret = CPP_EOF;
10389 else
10391 if (ret == CPP_KEYWORD)
10392 ret = CPP_NAME;
10393 c_parser_consume_token (the_parser);
10396 return ret;
10399 static void
10400 c_parser_pragma_pch_preprocess (c_parser *parser)
10402 tree name = NULL;
10404 c_parser_consume_pragma (parser);
10405 if (c_parser_next_token_is (parser, CPP_STRING))
10407 name = c_parser_peek_token (parser)->value;
10408 c_parser_consume_token (parser);
10410 else
10411 c_parser_error (parser, "expected string literal");
10412 c_parser_skip_to_pragma_eol (parser);
10414 if (name)
10415 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10418 /* OpenACC and OpenMP parsing routines. */
10420 /* Returns name of the next clause.
10421 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10422 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10423 returned and the token is consumed. */
10425 static pragma_omp_clause
10426 c_parser_omp_clause_name (c_parser *parser)
10428 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10430 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10431 result = PRAGMA_OACC_CLAUSE_AUTO;
10432 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10433 result = PRAGMA_OMP_CLAUSE_IF;
10434 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10435 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10436 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10437 result = PRAGMA_OMP_CLAUSE_FOR;
10438 else if (c_parser_next_token_is (parser, CPP_NAME))
10440 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10442 switch (p[0])
10444 case 'a':
10445 if (!strcmp ("aligned", p))
10446 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10447 else if (!strcmp ("async", p))
10448 result = PRAGMA_OACC_CLAUSE_ASYNC;
10449 break;
10450 case 'c':
10451 if (!strcmp ("collapse", p))
10452 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10453 else if (!strcmp ("copy", p))
10454 result = PRAGMA_OACC_CLAUSE_COPY;
10455 else if (!strcmp ("copyin", p))
10456 result = PRAGMA_OMP_CLAUSE_COPYIN;
10457 else if (!strcmp ("copyout", p))
10458 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10459 else if (!strcmp ("copyprivate", p))
10460 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10461 else if (!strcmp ("create", p))
10462 result = PRAGMA_OACC_CLAUSE_CREATE;
10463 break;
10464 case 'd':
10465 if (!strcmp ("defaultmap", p))
10466 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10467 else if (!strcmp ("delete", p))
10468 result = PRAGMA_OACC_CLAUSE_DELETE;
10469 else if (!strcmp ("depend", p))
10470 result = PRAGMA_OMP_CLAUSE_DEPEND;
10471 else if (!strcmp ("device", p))
10472 result = PRAGMA_OMP_CLAUSE_DEVICE;
10473 else if (!strcmp ("deviceptr", p))
10474 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10475 else if (!strcmp ("device_resident", p))
10476 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10477 else if (!strcmp ("dist_schedule", p))
10478 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10479 break;
10480 case 'f':
10481 if (!strcmp ("final", p))
10482 result = PRAGMA_OMP_CLAUSE_FINAL;
10483 else if (!strcmp ("firstprivate", p))
10484 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10485 else if (!strcmp ("from", p))
10486 result = PRAGMA_OMP_CLAUSE_FROM;
10487 break;
10488 case 'g':
10489 if (!strcmp ("gang", p))
10490 result = PRAGMA_OACC_CLAUSE_GANG;
10491 else if (!strcmp ("grainsize", p))
10492 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10493 break;
10494 case 'h':
10495 if (!strcmp ("hint", p))
10496 result = PRAGMA_OMP_CLAUSE_HINT;
10497 else if (!strcmp ("host", p))
10498 result = PRAGMA_OACC_CLAUSE_HOST;
10499 break;
10500 case 'i':
10501 if (!strcmp ("inbranch", p))
10502 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10503 else if (!strcmp ("independent", p))
10504 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10505 else if (!strcmp ("is_device_ptr", p))
10506 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10507 break;
10508 case 'l':
10509 if (!strcmp ("lastprivate", p))
10510 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10511 else if (!strcmp ("linear", p))
10512 result = PRAGMA_OMP_CLAUSE_LINEAR;
10513 else if (!strcmp ("link", p))
10514 result = PRAGMA_OMP_CLAUSE_LINK;
10515 break;
10516 case 'm':
10517 if (!strcmp ("map", p))
10518 result = PRAGMA_OMP_CLAUSE_MAP;
10519 else if (!strcmp ("mergeable", p))
10520 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10521 else if (flag_cilkplus && !strcmp ("mask", p))
10522 result = PRAGMA_CILK_CLAUSE_MASK;
10523 break;
10524 case 'n':
10525 if (!strcmp ("nogroup", p))
10526 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10527 else if (!strcmp ("notinbranch", p))
10528 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10529 else if (!strcmp ("nowait", p))
10530 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10531 else if (!strcmp ("num_gangs", p))
10532 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10533 else if (!strcmp ("num_tasks", p))
10534 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10535 else if (!strcmp ("num_teams", p))
10536 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10537 else if (!strcmp ("num_threads", p))
10538 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10539 else if (!strcmp ("num_workers", p))
10540 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10541 else if (flag_cilkplus && !strcmp ("nomask", p))
10542 result = PRAGMA_CILK_CLAUSE_NOMASK;
10543 break;
10544 case 'o':
10545 if (!strcmp ("ordered", p))
10546 result = PRAGMA_OMP_CLAUSE_ORDERED;
10547 break;
10548 case 'p':
10549 if (!strcmp ("parallel", p))
10550 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10551 else if (!strcmp ("present", p))
10552 result = PRAGMA_OACC_CLAUSE_PRESENT;
10553 else if (!strcmp ("present_or_copy", p)
10554 || !strcmp ("pcopy", p))
10555 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10556 else if (!strcmp ("present_or_copyin", p)
10557 || !strcmp ("pcopyin", p))
10558 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10559 else if (!strcmp ("present_or_copyout", p)
10560 || !strcmp ("pcopyout", p))
10561 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10562 else if (!strcmp ("present_or_create", p)
10563 || !strcmp ("pcreate", p))
10564 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10565 else if (!strcmp ("priority", p))
10566 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10567 else if (!strcmp ("private", p))
10568 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10569 else if (!strcmp ("proc_bind", p))
10570 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10571 break;
10572 case 'r':
10573 if (!strcmp ("reduction", p))
10574 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10575 break;
10576 case 's':
10577 if (!strcmp ("safelen", p))
10578 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10579 else if (!strcmp ("schedule", p))
10580 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10581 else if (!strcmp ("sections", p))
10582 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10583 else if (!strcmp ("seq", p))
10584 result = PRAGMA_OACC_CLAUSE_SEQ;
10585 else if (!strcmp ("shared", p))
10586 result = PRAGMA_OMP_CLAUSE_SHARED;
10587 else if (!strcmp ("simd", p))
10588 result = PRAGMA_OMP_CLAUSE_SIMD;
10589 else if (!strcmp ("simdlen", p))
10590 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10591 else if (!strcmp ("self", p))
10592 result = PRAGMA_OACC_CLAUSE_SELF;
10593 break;
10594 case 't':
10595 if (!strcmp ("taskgroup", p))
10596 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10597 else if (!strcmp ("thread_limit", p))
10598 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10599 else if (!strcmp ("threads", p))
10600 result = PRAGMA_OMP_CLAUSE_THREADS;
10601 else if (!strcmp ("tile", p))
10602 result = PRAGMA_OACC_CLAUSE_TILE;
10603 else if (!strcmp ("to", p))
10604 result = PRAGMA_OMP_CLAUSE_TO;
10605 break;
10606 case 'u':
10607 if (!strcmp ("uniform", p))
10608 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10609 else if (!strcmp ("untied", p))
10610 result = PRAGMA_OMP_CLAUSE_UNTIED;
10611 else if (!strcmp ("use_device", p))
10612 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10613 else if (!strcmp ("use_device_ptr", p))
10614 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10615 break;
10616 case 'v':
10617 if (!strcmp ("vector", p))
10618 result = PRAGMA_OACC_CLAUSE_VECTOR;
10619 else if (!strcmp ("vector_length", p))
10620 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10621 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10622 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10623 break;
10624 case 'w':
10625 if (!strcmp ("wait", p))
10626 result = PRAGMA_OACC_CLAUSE_WAIT;
10627 else if (!strcmp ("worker", p))
10628 result = PRAGMA_OACC_CLAUSE_WORKER;
10629 break;
10633 if (result != PRAGMA_OMP_CLAUSE_NONE)
10634 c_parser_consume_token (parser);
10636 return result;
10639 /* Validate that a clause of the given type does not already exist. */
10641 static void
10642 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10643 const char *name)
10645 tree c;
10647 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10648 if (OMP_CLAUSE_CODE (c) == code)
10650 location_t loc = OMP_CLAUSE_LOCATION (c);
10651 error_at (loc, "too many %qs clauses", name);
10652 break;
10656 /* OpenACC 2.0
10657 Parse wait clause or wait directive parameters. */
10659 static tree
10660 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10662 vec<tree, va_gc> *args;
10663 tree t, args_tree;
10665 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10666 return list;
10668 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10670 if (args->length () == 0)
10672 c_parser_error (parser, "expected integer expression before ')'");
10673 release_tree_vector (args);
10674 return list;
10677 args_tree = build_tree_list_vec (args);
10679 for (t = args_tree; t; t = TREE_CHAIN (t))
10681 tree targ = TREE_VALUE (t);
10683 if (targ != error_mark_node)
10685 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10687 c_parser_error (parser, "expression must be integral");
10688 targ = error_mark_node;
10690 else
10692 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10694 OMP_CLAUSE_DECL (c) = targ;
10695 OMP_CLAUSE_CHAIN (c) = list;
10696 list = c;
10701 release_tree_vector (args);
10702 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10703 return list;
10706 /* OpenACC 2.0, OpenMP 2.5:
10707 variable-list:
10708 identifier
10709 variable-list , identifier
10711 If KIND is nonzero, create the appropriate node and install the
10712 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10713 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10715 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10716 return the list created. */
10718 static tree
10719 c_parser_omp_variable_list (c_parser *parser,
10720 location_t clause_loc,
10721 enum omp_clause_code kind, tree list)
10723 if (c_parser_next_token_is_not (parser, CPP_NAME)
10724 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10725 c_parser_error (parser, "expected identifier");
10727 while (c_parser_next_token_is (parser, CPP_NAME)
10728 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10730 tree t = lookup_name (c_parser_peek_token (parser)->value);
10732 if (t == NULL_TREE)
10734 undeclared_variable (c_parser_peek_token (parser)->location,
10735 c_parser_peek_token (parser)->value);
10736 t = error_mark_node;
10739 c_parser_consume_token (parser);
10741 if (t == error_mark_node)
10743 else if (kind != 0)
10745 switch (kind)
10747 case OMP_CLAUSE__CACHE_:
10748 /* The OpenACC cache directive explicitly only allows "array
10749 elements or subarrays". */
10750 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10752 c_parser_error (parser, "expected %<[%>");
10753 t = error_mark_node;
10754 break;
10756 /* FALLTHROUGH */
10757 case OMP_CLAUSE_MAP:
10758 case OMP_CLAUSE_FROM:
10759 case OMP_CLAUSE_TO:
10760 while (c_parser_next_token_is (parser, CPP_DOT))
10762 location_t op_loc = c_parser_peek_token (parser)->location;
10763 c_parser_consume_token (parser);
10764 if (!c_parser_next_token_is (parser, CPP_NAME))
10766 c_parser_error (parser, "expected identifier");
10767 t = error_mark_node;
10768 break;
10771 c_token *comp_tok = c_parser_peek_token (parser);
10772 tree ident = comp_tok->value;
10773 location_t comp_loc = comp_tok->location;
10774 c_parser_consume_token (parser);
10775 t = build_component_ref (op_loc, t, ident, comp_loc);
10777 /* FALLTHROUGH */
10778 case OMP_CLAUSE_DEPEND:
10779 case OMP_CLAUSE_REDUCTION:
10780 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10782 tree low_bound = NULL_TREE, length = NULL_TREE;
10784 c_parser_consume_token (parser);
10785 if (!c_parser_next_token_is (parser, CPP_COLON))
10787 location_t expr_loc
10788 = c_parser_peek_token (parser)->location;
10789 c_expr expr = c_parser_expression (parser);
10790 expr = convert_lvalue_to_rvalue (expr_loc, expr,
10791 false, true);
10792 low_bound = expr.value;
10794 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10795 length = integer_one_node;
10796 else
10798 /* Look for `:'. */
10799 if (!c_parser_require (parser, CPP_COLON,
10800 "expected %<:%>"))
10802 t = error_mark_node;
10803 break;
10805 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10807 location_t expr_loc
10808 = c_parser_peek_token (parser)->location;
10809 c_expr expr = c_parser_expression (parser);
10810 expr = convert_lvalue_to_rvalue (expr_loc, expr,
10811 false, true);
10812 length = expr.value;
10815 /* Look for the closing `]'. */
10816 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10817 "expected %<]%>"))
10819 t = error_mark_node;
10820 break;
10823 t = tree_cons (low_bound, length, t);
10825 break;
10826 default:
10827 break;
10830 if (t != error_mark_node)
10832 tree u = build_omp_clause (clause_loc, kind);
10833 OMP_CLAUSE_DECL (u) = t;
10834 OMP_CLAUSE_CHAIN (u) = list;
10835 list = u;
10838 else
10839 list = tree_cons (t, NULL_TREE, list);
10841 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10842 break;
10844 c_parser_consume_token (parser);
10847 return list;
10850 /* Similarly, but expect leading and trailing parenthesis. This is a very
10851 common case for OpenACC and OpenMP clauses. */
10853 static tree
10854 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10855 tree list)
10857 /* The clauses location. */
10858 location_t loc = c_parser_peek_token (parser)->location;
10860 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10862 list = c_parser_omp_variable_list (parser, loc, kind, list);
10863 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10865 return list;
10868 /* OpenACC 2.0:
10869 copy ( variable-list )
10870 copyin ( variable-list )
10871 copyout ( variable-list )
10872 create ( variable-list )
10873 delete ( variable-list )
10874 present ( variable-list )
10875 present_or_copy ( variable-list )
10876 pcopy ( variable-list )
10877 present_or_copyin ( variable-list )
10878 pcopyin ( variable-list )
10879 present_or_copyout ( variable-list )
10880 pcopyout ( variable-list )
10881 present_or_create ( variable-list )
10882 pcreate ( variable-list ) */
10884 static tree
10885 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10886 tree list)
10888 enum gomp_map_kind kind;
10889 switch (c_kind)
10891 case PRAGMA_OACC_CLAUSE_COPY:
10892 kind = GOMP_MAP_FORCE_TOFROM;
10893 break;
10894 case PRAGMA_OACC_CLAUSE_COPYIN:
10895 kind = GOMP_MAP_FORCE_TO;
10896 break;
10897 case PRAGMA_OACC_CLAUSE_COPYOUT:
10898 kind = GOMP_MAP_FORCE_FROM;
10899 break;
10900 case PRAGMA_OACC_CLAUSE_CREATE:
10901 kind = GOMP_MAP_FORCE_ALLOC;
10902 break;
10903 case PRAGMA_OACC_CLAUSE_DELETE:
10904 kind = GOMP_MAP_DELETE;
10905 break;
10906 case PRAGMA_OACC_CLAUSE_DEVICE:
10907 kind = GOMP_MAP_FORCE_TO;
10908 break;
10909 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
10910 kind = GOMP_MAP_DEVICE_RESIDENT;
10911 break;
10912 case PRAGMA_OACC_CLAUSE_HOST:
10913 case PRAGMA_OACC_CLAUSE_SELF:
10914 kind = GOMP_MAP_FORCE_FROM;
10915 break;
10916 case PRAGMA_OACC_CLAUSE_LINK:
10917 kind = GOMP_MAP_LINK;
10918 break;
10919 case PRAGMA_OACC_CLAUSE_PRESENT:
10920 kind = GOMP_MAP_FORCE_PRESENT;
10921 break;
10922 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10923 kind = GOMP_MAP_TOFROM;
10924 break;
10925 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10926 kind = GOMP_MAP_TO;
10927 break;
10928 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10929 kind = GOMP_MAP_FROM;
10930 break;
10931 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10932 kind = GOMP_MAP_ALLOC;
10933 break;
10934 default:
10935 gcc_unreachable ();
10937 tree nl, c;
10938 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10940 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10941 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10943 return nl;
10946 /* OpenACC 2.0:
10947 deviceptr ( variable-list ) */
10949 static tree
10950 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10952 location_t loc = c_parser_peek_token (parser)->location;
10953 tree vars, t;
10955 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10956 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10957 variable-list must only allow for pointer variables. */
10958 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10959 for (t = vars; t && t; t = TREE_CHAIN (t))
10961 tree v = TREE_PURPOSE (t);
10963 /* FIXME diagnostics: Ideally we should keep individual
10964 locations for all the variables in the var list to make the
10965 following errors more precise. Perhaps
10966 c_parser_omp_var_list_parens() should construct a list of
10967 locations to go along with the var list. */
10969 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
10970 error_at (loc, "%qD is not a variable", v);
10971 else if (TREE_TYPE (v) == error_mark_node)
10973 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10974 error_at (loc, "%qD is not a pointer variable", v);
10976 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10977 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10978 OMP_CLAUSE_DECL (u) = v;
10979 OMP_CLAUSE_CHAIN (u) = list;
10980 list = u;
10983 return list;
10986 /* OpenACC 2.0, OpenMP 3.0:
10987 collapse ( constant-expression ) */
10989 static tree
10990 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10992 tree c, num = error_mark_node;
10993 HOST_WIDE_INT n;
10994 location_t loc;
10996 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10998 loc = c_parser_peek_token (parser)->location;
10999 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11001 num = c_parser_expr_no_commas (parser, NULL).value;
11002 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11004 if (num == error_mark_node)
11005 return list;
11006 mark_exp_read (num);
11007 num = c_fully_fold (num, false, NULL);
11008 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11009 || !tree_fits_shwi_p (num)
11010 || (n = tree_to_shwi (num)) <= 0
11011 || (int) n != n)
11013 error_at (loc,
11014 "collapse argument needs positive constant integer expression");
11015 return list;
11017 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11018 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11019 OMP_CLAUSE_CHAIN (c) = list;
11020 return c;
11023 /* OpenMP 2.5:
11024 copyin ( variable-list ) */
11026 static tree
11027 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11029 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11032 /* OpenMP 2.5:
11033 copyprivate ( variable-list ) */
11035 static tree
11036 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11038 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11041 /* OpenMP 2.5:
11042 default ( shared | none )
11044 OpenACC 2.0:
11045 default (none) */
11047 static tree
11048 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11050 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11051 location_t loc = c_parser_peek_token (parser)->location;
11052 tree c;
11054 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11055 return list;
11056 if (c_parser_next_token_is (parser, CPP_NAME))
11058 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11060 switch (p[0])
11062 case 'n':
11063 if (strcmp ("none", p) != 0)
11064 goto invalid_kind;
11065 kind = OMP_CLAUSE_DEFAULT_NONE;
11066 break;
11068 case 's':
11069 if (strcmp ("shared", p) != 0 || is_oacc)
11070 goto invalid_kind;
11071 kind = OMP_CLAUSE_DEFAULT_SHARED;
11072 break;
11074 default:
11075 goto invalid_kind;
11078 c_parser_consume_token (parser);
11080 else
11082 invalid_kind:
11083 if (is_oacc)
11084 c_parser_error (parser, "expected %<none%>");
11085 else
11086 c_parser_error (parser, "expected %<none%> or %<shared%>");
11088 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11090 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11091 return list;
11093 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11094 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11095 OMP_CLAUSE_CHAIN (c) = list;
11096 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11098 return c;
11101 /* OpenMP 2.5:
11102 firstprivate ( variable-list ) */
11104 static tree
11105 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11107 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11110 /* OpenMP 3.1:
11111 final ( expression ) */
11113 static tree
11114 c_parser_omp_clause_final (c_parser *parser, tree list)
11116 location_t loc = c_parser_peek_token (parser)->location;
11117 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11119 tree t = c_parser_paren_condition (parser);
11120 tree c;
11122 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11124 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11125 OMP_CLAUSE_FINAL_EXPR (c) = t;
11126 OMP_CLAUSE_CHAIN (c) = list;
11127 list = c;
11129 else
11130 c_parser_error (parser, "expected %<(%>");
11132 return list;
11135 /* OpenACC, OpenMP 2.5:
11136 if ( expression )
11138 OpenMP 4.5:
11139 if ( directive-name-modifier : expression )
11141 directive-name-modifier:
11142 parallel | task | taskloop | target data | target | target update
11143 | target enter data | target exit data */
11145 static tree
11146 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11148 location_t location = c_parser_peek_token (parser)->location;
11149 enum tree_code if_modifier = ERROR_MARK;
11151 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11152 return list;
11154 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11156 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11157 int n = 2;
11158 if (strcmp (p, "parallel") == 0)
11159 if_modifier = OMP_PARALLEL;
11160 else if (strcmp (p, "task") == 0)
11161 if_modifier = OMP_TASK;
11162 else if (strcmp (p, "taskloop") == 0)
11163 if_modifier = OMP_TASKLOOP;
11164 else if (strcmp (p, "target") == 0)
11166 if_modifier = OMP_TARGET;
11167 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11169 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11170 if (strcmp ("data", p) == 0)
11171 if_modifier = OMP_TARGET_DATA;
11172 else if (strcmp ("update", p) == 0)
11173 if_modifier = OMP_TARGET_UPDATE;
11174 else if (strcmp ("enter", p) == 0)
11175 if_modifier = OMP_TARGET_ENTER_DATA;
11176 else if (strcmp ("exit", p) == 0)
11177 if_modifier = OMP_TARGET_EXIT_DATA;
11178 if (if_modifier != OMP_TARGET)
11180 n = 3;
11181 c_parser_consume_token (parser);
11183 else
11185 location_t loc = c_parser_peek_2nd_token (parser)->location;
11186 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11187 "or %<exit%>");
11188 if_modifier = ERROR_MARK;
11190 if (if_modifier == OMP_TARGET_ENTER_DATA
11191 || if_modifier == OMP_TARGET_EXIT_DATA)
11193 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11195 p = IDENTIFIER_POINTER
11196 (c_parser_peek_2nd_token (parser)->value);
11197 if (strcmp ("data", p) == 0)
11198 n = 4;
11200 if (n == 4)
11201 c_parser_consume_token (parser);
11202 else
11204 location_t loc
11205 = c_parser_peek_2nd_token (parser)->location;
11206 error_at (loc, "expected %<data%>");
11207 if_modifier = ERROR_MARK;
11212 if (if_modifier != ERROR_MARK)
11214 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11216 c_parser_consume_token (parser);
11217 c_parser_consume_token (parser);
11219 else
11221 if (n > 2)
11223 location_t loc = c_parser_peek_2nd_token (parser)->location;
11224 error_at (loc, "expected %<:%>");
11226 if_modifier = ERROR_MARK;
11231 tree t = c_parser_condition (parser), c;
11232 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11234 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11235 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11237 if (if_modifier != ERROR_MARK
11238 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11240 const char *p = NULL;
11241 switch (if_modifier)
11243 case OMP_PARALLEL: p = "parallel"; break;
11244 case OMP_TASK: p = "task"; break;
11245 case OMP_TASKLOOP: p = "taskloop"; break;
11246 case OMP_TARGET_DATA: p = "target data"; break;
11247 case OMP_TARGET: p = "target"; break;
11248 case OMP_TARGET_UPDATE: p = "target update"; break;
11249 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11250 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11251 default: gcc_unreachable ();
11253 error_at (location, "too many %<if%> clauses with %qs modifier",
11255 return list;
11257 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11259 if (!is_omp)
11260 error_at (location, "too many %<if%> clauses");
11261 else
11262 error_at (location, "too many %<if%> clauses without modifier");
11263 return list;
11265 else if (if_modifier == ERROR_MARK
11266 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11268 error_at (location, "if any %<if%> clause has modifier, then all "
11269 "%<if%> clauses have to use modifier");
11270 return list;
11274 c = build_omp_clause (location, OMP_CLAUSE_IF);
11275 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11276 OMP_CLAUSE_IF_EXPR (c) = t;
11277 OMP_CLAUSE_CHAIN (c) = list;
11278 return c;
11281 /* OpenMP 2.5:
11282 lastprivate ( variable-list ) */
11284 static tree
11285 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11287 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11290 /* OpenMP 3.1:
11291 mergeable */
11293 static tree
11294 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11296 tree c;
11298 /* FIXME: Should we allow duplicates? */
11299 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11301 c = build_omp_clause (c_parser_peek_token (parser)->location,
11302 OMP_CLAUSE_MERGEABLE);
11303 OMP_CLAUSE_CHAIN (c) = list;
11305 return c;
11308 /* OpenMP 2.5:
11309 nowait */
11311 static tree
11312 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11314 tree c;
11315 location_t loc = c_parser_peek_token (parser)->location;
11317 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11319 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11320 OMP_CLAUSE_CHAIN (c) = list;
11321 return c;
11324 /* OpenACC:
11325 num_gangs ( expression ) */
11327 static tree
11328 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11330 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11331 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11333 location_t expr_loc = c_parser_peek_token (parser)->location;
11334 c_expr expr = c_parser_expression (parser);
11335 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11336 tree c, t = expr.value;
11337 t = c_fully_fold (t, false, NULL);
11339 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11341 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11343 c_parser_error (parser, "expected integer expression");
11344 return list;
11347 /* Attempt to statically determine when the number isn't positive. */
11348 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11349 build_int_cst (TREE_TYPE (t), 0));
11350 protected_set_expr_location (c, expr_loc);
11351 if (c == boolean_true_node)
11353 warning_at (expr_loc, 0,
11354 "%<num_gangs%> value must be positive");
11355 t = integer_one_node;
11358 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11360 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11361 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11362 OMP_CLAUSE_CHAIN (c) = list;
11363 list = c;
11366 return list;
11369 /* OpenMP 2.5:
11370 num_threads ( expression ) */
11372 static tree
11373 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11375 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11376 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11378 location_t expr_loc = c_parser_peek_token (parser)->location;
11379 c_expr expr = c_parser_expression (parser);
11380 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11381 tree c, t = expr.value;
11382 t = c_fully_fold (t, false, NULL);
11384 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11386 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11388 c_parser_error (parser, "expected integer expression");
11389 return list;
11392 /* Attempt to statically determine when the number isn't positive. */
11393 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11394 build_int_cst (TREE_TYPE (t), 0));
11395 protected_set_expr_location (c, expr_loc);
11396 if (c == boolean_true_node)
11398 warning_at (expr_loc, 0,
11399 "%<num_threads%> value must be positive");
11400 t = integer_one_node;
11403 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11405 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11406 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11407 OMP_CLAUSE_CHAIN (c) = list;
11408 list = c;
11411 return list;
11414 /* OpenMP 4.5:
11415 num_tasks ( expression ) */
11417 static tree
11418 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11420 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11421 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11423 location_t expr_loc = c_parser_peek_token (parser)->location;
11424 c_expr expr = c_parser_expression (parser);
11425 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11426 tree c, t = expr.value;
11427 t = c_fully_fold (t, false, NULL);
11429 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11431 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11433 c_parser_error (parser, "expected integer expression");
11434 return list;
11437 /* Attempt to statically determine when the number isn't positive. */
11438 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11439 build_int_cst (TREE_TYPE (t), 0));
11440 if (CAN_HAVE_LOCATION_P (c))
11441 SET_EXPR_LOCATION (c, expr_loc);
11442 if (c == boolean_true_node)
11444 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11445 t = integer_one_node;
11448 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11450 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11451 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11452 OMP_CLAUSE_CHAIN (c) = list;
11453 list = c;
11456 return list;
11459 /* OpenMP 4.5:
11460 grainsize ( expression ) */
11462 static tree
11463 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11465 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11466 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11468 location_t expr_loc = c_parser_peek_token (parser)->location;
11469 c_expr expr = c_parser_expression (parser);
11470 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11471 tree c, t = expr.value;
11472 t = c_fully_fold (t, false, NULL);
11474 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11476 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11478 c_parser_error (parser, "expected integer expression");
11479 return list;
11482 /* Attempt to statically determine when the number isn't positive. */
11483 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11484 build_int_cst (TREE_TYPE (t), 0));
11485 if (CAN_HAVE_LOCATION_P (c))
11486 SET_EXPR_LOCATION (c, expr_loc);
11487 if (c == boolean_true_node)
11489 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11490 t = integer_one_node;
11493 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11495 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11496 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11497 OMP_CLAUSE_CHAIN (c) = list;
11498 list = c;
11501 return list;
11504 /* OpenMP 4.5:
11505 priority ( expression ) */
11507 static tree
11508 c_parser_omp_clause_priority (c_parser *parser, tree list)
11510 location_t priority_loc = c_parser_peek_token (parser)->location;
11511 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11513 location_t expr_loc = c_parser_peek_token (parser)->location;
11514 c_expr expr = c_parser_expression (parser);
11515 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11516 tree c, t = expr.value;
11517 t = c_fully_fold (t, false, NULL);
11519 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11521 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11523 c_parser_error (parser, "expected integer expression");
11524 return list;
11527 /* Attempt to statically determine when the number isn't
11528 non-negative. */
11529 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11530 build_int_cst (TREE_TYPE (t), 0));
11531 if (CAN_HAVE_LOCATION_P (c))
11532 SET_EXPR_LOCATION (c, expr_loc);
11533 if (c == boolean_true_node)
11535 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11536 t = integer_one_node;
11539 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11541 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11542 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11543 OMP_CLAUSE_CHAIN (c) = list;
11544 list = c;
11547 return list;
11550 /* OpenMP 4.5:
11551 hint ( expression ) */
11553 static tree
11554 c_parser_omp_clause_hint (c_parser *parser, tree list)
11556 location_t hint_loc = c_parser_peek_token (parser)->location;
11557 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11559 location_t expr_loc = c_parser_peek_token (parser)->location;
11560 c_expr expr = c_parser_expression (parser);
11561 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11562 tree c, t = expr.value;
11563 t = c_fully_fold (t, false, NULL);
11565 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11567 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11569 c_parser_error (parser, "expected integer expression");
11570 return list;
11573 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11575 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11576 OMP_CLAUSE_HINT_EXPR (c) = t;
11577 OMP_CLAUSE_CHAIN (c) = list;
11578 list = c;
11581 return list;
11584 /* OpenMP 4.5:
11585 defaultmap ( tofrom : scalar ) */
11587 static tree
11588 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11590 location_t loc = c_parser_peek_token (parser)->location;
11591 tree c;
11592 const char *p;
11594 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11595 return list;
11596 if (!c_parser_next_token_is (parser, CPP_NAME))
11598 c_parser_error (parser, "expected %<tofrom%>");
11599 goto out_err;
11601 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11602 if (strcmp (p, "tofrom") != 0)
11604 c_parser_error (parser, "expected %<tofrom%>");
11605 goto out_err;
11607 c_parser_consume_token (parser);
11608 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11609 goto out_err;
11610 if (!c_parser_next_token_is (parser, CPP_NAME))
11612 c_parser_error (parser, "expected %<scalar%>");
11613 goto out_err;
11615 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11616 if (strcmp (p, "scalar") != 0)
11618 c_parser_error (parser, "expected %<scalar%>");
11619 goto out_err;
11621 c_parser_consume_token (parser);
11622 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11623 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11624 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11625 OMP_CLAUSE_CHAIN (c) = list;
11626 return c;
11628 out_err:
11629 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11630 return list;
11633 /* OpenACC 2.0:
11634 use_device ( variable-list )
11636 OpenMP 4.5:
11637 use_device_ptr ( variable-list ) */
11639 static tree
11640 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11642 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11643 list);
11646 /* OpenMP 4.5:
11647 is_device_ptr ( variable-list ) */
11649 static tree
11650 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11652 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11655 /* OpenACC:
11656 num_workers ( expression ) */
11658 static tree
11659 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11661 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11662 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11664 location_t expr_loc = c_parser_peek_token (parser)->location;
11665 c_expr expr = c_parser_expression (parser);
11666 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11667 tree c, t = expr.value;
11668 t = c_fully_fold (t, false, NULL);
11670 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11672 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11674 c_parser_error (parser, "expected integer expression");
11675 return list;
11678 /* Attempt to statically determine when the number isn't positive. */
11679 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11680 build_int_cst (TREE_TYPE (t), 0));
11681 protected_set_expr_location (c, expr_loc);
11682 if (c == boolean_true_node)
11684 warning_at (expr_loc, 0,
11685 "%<num_workers%> value must be positive");
11686 t = integer_one_node;
11689 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11691 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11692 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11693 OMP_CLAUSE_CHAIN (c) = list;
11694 list = c;
11697 return list;
11700 /* OpenACC:
11702 gang [( gang-arg-list )]
11703 worker [( [num:] int-expr )]
11704 vector [( [length:] int-expr )]
11706 where gang-arg is one of:
11708 [num:] int-expr
11709 static: size-expr
11711 and size-expr may be:
11714 int-expr
11717 static tree
11718 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11719 const char *str, tree list)
11721 const char *id = "num";
11722 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11723 location_t loc = c_parser_peek_token (parser)->location;
11725 if (kind == OMP_CLAUSE_VECTOR)
11726 id = "length";
11728 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11730 c_parser_consume_token (parser);
11734 c_token *next = c_parser_peek_token (parser);
11735 int idx = 0;
11737 /* Gang static argument. */
11738 if (kind == OMP_CLAUSE_GANG
11739 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11741 c_parser_consume_token (parser);
11743 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11744 goto cleanup_error;
11746 idx = 1;
11747 if (ops[idx] != NULL_TREE)
11749 c_parser_error (parser, "too many %<static%> arguments");
11750 goto cleanup_error;
11753 /* Check for the '*' argument. */
11754 if (c_parser_next_token_is (parser, CPP_MULT)
11755 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11756 || c_parser_peek_2nd_token (parser)->type
11757 == CPP_CLOSE_PAREN))
11759 c_parser_consume_token (parser);
11760 ops[idx] = integer_minus_one_node;
11762 if (c_parser_next_token_is (parser, CPP_COMMA))
11764 c_parser_consume_token (parser);
11765 continue;
11767 else
11768 break;
11771 /* Worker num: argument and vector length: arguments. */
11772 else if (c_parser_next_token_is (parser, CPP_NAME)
11773 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11774 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11776 c_parser_consume_token (parser); /* id */
11777 c_parser_consume_token (parser); /* ':' */
11780 /* Now collect the actual argument. */
11781 if (ops[idx] != NULL_TREE)
11783 c_parser_error (parser, "unexpected argument");
11784 goto cleanup_error;
11787 location_t expr_loc = c_parser_peek_token (parser)->location;
11788 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11789 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11790 tree expr = cexpr.value;
11791 if (expr == error_mark_node)
11792 goto cleanup_error;
11794 expr = c_fully_fold (expr, false, NULL);
11796 /* Attempt to statically determine when the number isn't a
11797 positive integer. */
11799 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11801 c_parser_error (parser, "expected integer expression");
11802 return list;
11805 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11806 build_int_cst (TREE_TYPE (expr), 0));
11807 if (c == boolean_true_node)
11809 warning_at (loc, 0,
11810 "%<%s%> value must be positive", str);
11811 expr = integer_one_node;
11814 ops[idx] = expr;
11816 if (kind == OMP_CLAUSE_GANG
11817 && c_parser_next_token_is (parser, CPP_COMMA))
11819 c_parser_consume_token (parser);
11820 continue;
11822 break;
11824 while (1);
11826 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11827 goto cleanup_error;
11830 check_no_duplicate_clause (list, kind, str);
11832 c = build_omp_clause (loc, kind);
11834 if (ops[1])
11835 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11837 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11838 OMP_CLAUSE_CHAIN (c) = list;
11840 return c;
11842 cleanup_error:
11843 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11844 return list;
11847 /* OpenACC:
11848 auto
11849 independent
11850 nohost
11851 seq */
11853 static tree
11854 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11855 tree list)
11857 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11859 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11860 OMP_CLAUSE_CHAIN (c) = list;
11862 return c;
11865 /* OpenACC:
11866 async [( int-expr )] */
11868 static tree
11869 c_parser_oacc_clause_async (c_parser *parser, tree list)
11871 tree c, t;
11872 location_t loc = c_parser_peek_token (parser)->location;
11874 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11876 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11878 c_parser_consume_token (parser);
11880 t = c_parser_expression (parser).value;
11881 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11882 c_parser_error (parser, "expected integer expression");
11883 else if (t == error_mark_node
11884 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11885 return list;
11887 else
11888 t = c_fully_fold (t, false, NULL);
11890 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11892 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11893 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11894 OMP_CLAUSE_CHAIN (c) = list;
11895 list = c;
11897 return list;
11900 /* OpenACC 2.0:
11901 tile ( size-expr-list ) */
11903 static tree
11904 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11906 tree c, expr = error_mark_node;
11907 location_t loc, expr_loc;
11908 tree tile = NULL_TREE;
11910 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11912 loc = c_parser_peek_token (parser)->location;
11913 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11914 return list;
11918 if (c_parser_next_token_is (parser, CPP_MULT)
11919 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11920 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11922 c_parser_consume_token (parser);
11923 expr = integer_minus_one_node;
11925 else
11927 expr_loc = c_parser_peek_token (parser)->location;
11928 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11929 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11930 expr = cexpr.value;
11932 if (expr == error_mark_node)
11934 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11935 "expected %<)%>");
11936 return list;
11939 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11941 c_parser_error (parser, "%<tile%> value must be integral");
11942 return list;
11945 expr = c_fully_fold (expr, false, NULL);
11947 /* Attempt to statically determine when expr isn't positive. */
11948 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11949 build_int_cst (TREE_TYPE (expr), 0));
11950 protected_set_expr_location (c, expr_loc);
11951 if (c == boolean_true_node)
11953 warning_at (expr_loc, 0,"%<tile%> value must be positive");
11954 expr = integer_one_node;
11958 tile = tree_cons (NULL_TREE, expr, tile);
11959 if (c_parser_next_token_is (parser, CPP_COMMA))
11960 c_parser_consume_token (parser);
11962 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11964 /* Consume the trailing ')'. */
11965 c_parser_consume_token (parser);
11967 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11968 tile = nreverse (tile);
11969 OMP_CLAUSE_TILE_LIST (c) = tile;
11970 OMP_CLAUSE_CHAIN (c) = list;
11971 return c;
11974 /* OpenACC:
11975 wait ( int-expr-list ) */
11977 static tree
11978 c_parser_oacc_clause_wait (c_parser *parser, tree list)
11980 location_t clause_loc = c_parser_peek_token (parser)->location;
11982 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11983 list = c_parser_oacc_wait_list (parser, clause_loc, list);
11985 return list;
11988 /* OpenMP 2.5:
11989 ordered
11991 OpenMP 4.5:
11992 ordered ( constant-expression ) */
11994 static tree
11995 c_parser_omp_clause_ordered (c_parser *parser, tree list)
11997 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
11999 tree c, num = NULL_TREE;
12000 HOST_WIDE_INT n;
12001 location_t loc = c_parser_peek_token (parser)->location;
12002 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12004 c_parser_consume_token (parser);
12005 num = c_parser_expr_no_commas (parser, NULL).value;
12006 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12008 if (num == error_mark_node)
12009 return list;
12010 if (num)
12012 mark_exp_read (num);
12013 num = c_fully_fold (num, false, NULL);
12014 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12015 || !tree_fits_shwi_p (num)
12016 || (n = tree_to_shwi (num)) <= 0
12017 || (int) n != n)
12019 error_at (loc, "ordered argument needs positive "
12020 "constant integer expression");
12021 return list;
12024 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12025 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12026 OMP_CLAUSE_CHAIN (c) = list;
12027 return c;
12030 /* OpenMP 2.5:
12031 private ( variable-list ) */
12033 static tree
12034 c_parser_omp_clause_private (c_parser *parser, tree list)
12036 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12039 /* OpenMP 2.5:
12040 reduction ( reduction-operator : variable-list )
12042 reduction-operator:
12043 One of: + * - & ^ | && ||
12045 OpenMP 3.1:
12047 reduction-operator:
12048 One of: + * - & ^ | && || max min
12050 OpenMP 4.0:
12052 reduction-operator:
12053 One of: + * - & ^ | && ||
12054 identifier */
12056 static tree
12057 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12059 location_t clause_loc = c_parser_peek_token (parser)->location;
12060 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12062 enum tree_code code = ERROR_MARK;
12063 tree reduc_id = NULL_TREE;
12065 switch (c_parser_peek_token (parser)->type)
12067 case CPP_PLUS:
12068 code = PLUS_EXPR;
12069 break;
12070 case CPP_MULT:
12071 code = MULT_EXPR;
12072 break;
12073 case CPP_MINUS:
12074 code = MINUS_EXPR;
12075 break;
12076 case CPP_AND:
12077 code = BIT_AND_EXPR;
12078 break;
12079 case CPP_XOR:
12080 code = BIT_XOR_EXPR;
12081 break;
12082 case CPP_OR:
12083 code = BIT_IOR_EXPR;
12084 break;
12085 case CPP_AND_AND:
12086 code = TRUTH_ANDIF_EXPR;
12087 break;
12088 case CPP_OR_OR:
12089 code = TRUTH_ORIF_EXPR;
12090 break;
12091 case CPP_NAME:
12093 const char *p
12094 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12095 if (strcmp (p, "min") == 0)
12097 code = MIN_EXPR;
12098 break;
12100 if (strcmp (p, "max") == 0)
12102 code = MAX_EXPR;
12103 break;
12105 reduc_id = c_parser_peek_token (parser)->value;
12106 break;
12108 default:
12109 c_parser_error (parser,
12110 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12111 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
12112 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12113 return list;
12115 c_parser_consume_token (parser);
12116 reduc_id = c_omp_reduction_id (code, reduc_id);
12117 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12119 tree nl, c;
12121 nl = c_parser_omp_variable_list (parser, clause_loc,
12122 OMP_CLAUSE_REDUCTION, list);
12123 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12125 tree d = OMP_CLAUSE_DECL (c), type;
12126 if (TREE_CODE (d) != TREE_LIST)
12127 type = TREE_TYPE (d);
12128 else
12130 int cnt = 0;
12131 tree t;
12132 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12133 cnt++;
12134 type = TREE_TYPE (t);
12135 while (cnt > 0)
12137 if (TREE_CODE (type) != POINTER_TYPE
12138 && TREE_CODE (type) != ARRAY_TYPE)
12139 break;
12140 type = TREE_TYPE (type);
12141 cnt--;
12144 while (TREE_CODE (type) == ARRAY_TYPE)
12145 type = TREE_TYPE (type);
12146 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12147 if (code == ERROR_MARK
12148 || !(INTEGRAL_TYPE_P (type)
12149 || TREE_CODE (type) == REAL_TYPE
12150 || TREE_CODE (type) == COMPLEX_TYPE))
12151 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12152 = c_omp_reduction_lookup (reduc_id,
12153 TYPE_MAIN_VARIANT (type));
12156 list = nl;
12158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12160 return list;
12163 /* OpenMP 2.5:
12164 schedule ( schedule-kind )
12165 schedule ( schedule-kind , expression )
12167 schedule-kind:
12168 static | dynamic | guided | runtime | auto
12170 OpenMP 4.5:
12171 schedule ( schedule-modifier : schedule-kind )
12172 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12174 schedule-modifier:
12175 simd
12176 monotonic
12177 nonmonotonic */
12179 static tree
12180 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12182 tree c, t;
12183 location_t loc = c_parser_peek_token (parser)->location;
12184 int modifiers = 0, nmodifiers = 0;
12186 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12187 return list;
12189 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12191 while (c_parser_next_token_is (parser, CPP_NAME))
12193 tree kind = c_parser_peek_token (parser)->value;
12194 const char *p = IDENTIFIER_POINTER (kind);
12195 if (strcmp ("simd", p) == 0)
12196 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12197 else if (strcmp ("monotonic", p) == 0)
12198 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12199 else if (strcmp ("nonmonotonic", p) == 0)
12200 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12201 else
12202 break;
12203 c_parser_consume_token (parser);
12204 if (nmodifiers++ == 0
12205 && c_parser_next_token_is (parser, CPP_COMMA))
12206 c_parser_consume_token (parser);
12207 else
12209 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12210 break;
12214 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12215 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12216 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12217 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12219 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12220 "specified");
12221 modifiers = 0;
12224 if (c_parser_next_token_is (parser, CPP_NAME))
12226 tree kind = c_parser_peek_token (parser)->value;
12227 const char *p = IDENTIFIER_POINTER (kind);
12229 switch (p[0])
12231 case 'd':
12232 if (strcmp ("dynamic", p) != 0)
12233 goto invalid_kind;
12234 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12235 break;
12237 case 'g':
12238 if (strcmp ("guided", p) != 0)
12239 goto invalid_kind;
12240 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12241 break;
12243 case 'r':
12244 if (strcmp ("runtime", p) != 0)
12245 goto invalid_kind;
12246 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12247 break;
12249 default:
12250 goto invalid_kind;
12253 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12254 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12255 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12256 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12257 else
12258 goto invalid_kind;
12260 c_parser_consume_token (parser);
12261 if (c_parser_next_token_is (parser, CPP_COMMA))
12263 location_t here;
12264 c_parser_consume_token (parser);
12266 here = c_parser_peek_token (parser)->location;
12267 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12268 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12269 t = expr.value;
12270 t = c_fully_fold (t, false, NULL);
12272 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12273 error_at (here, "schedule %<runtime%> does not take "
12274 "a %<chunk_size%> parameter");
12275 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12276 error_at (here,
12277 "schedule %<auto%> does not take "
12278 "a %<chunk_size%> parameter");
12279 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12281 /* Attempt to statically determine when the number isn't
12282 positive. */
12283 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12284 build_int_cst (TREE_TYPE (t), 0));
12285 protected_set_expr_location (s, loc);
12286 if (s == boolean_true_node)
12288 warning_at (loc, 0,
12289 "chunk size value must be positive");
12290 t = integer_one_node;
12292 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12294 else
12295 c_parser_error (parser, "expected integer expression");
12297 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12299 else
12300 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12301 "expected %<,%> or %<)%>");
12303 OMP_CLAUSE_SCHEDULE_KIND (c)
12304 = (enum omp_clause_schedule_kind)
12305 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12307 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12308 OMP_CLAUSE_CHAIN (c) = list;
12309 return c;
12311 invalid_kind:
12312 c_parser_error (parser, "invalid schedule kind");
12313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12314 return list;
12317 /* OpenMP 2.5:
12318 shared ( variable-list ) */
12320 static tree
12321 c_parser_omp_clause_shared (c_parser *parser, tree list)
12323 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12326 /* OpenMP 3.0:
12327 untied */
12329 static tree
12330 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12332 tree c;
12334 /* FIXME: Should we allow duplicates? */
12335 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12337 c = build_omp_clause (c_parser_peek_token (parser)->location,
12338 OMP_CLAUSE_UNTIED);
12339 OMP_CLAUSE_CHAIN (c) = list;
12341 return c;
12344 /* OpenACC:
12345 vector_length ( expression ) */
12347 static tree
12348 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12350 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12351 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12353 location_t expr_loc = c_parser_peek_token (parser)->location;
12354 c_expr expr = c_parser_expression (parser);
12355 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12356 tree c, t = expr.value;
12357 t = c_fully_fold (t, false, NULL);
12359 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12361 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12363 c_parser_error (parser, "expected integer expression");
12364 return list;
12367 /* Attempt to statically determine when the number isn't positive. */
12368 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12369 build_int_cst (TREE_TYPE (t), 0));
12370 protected_set_expr_location (c, expr_loc);
12371 if (c == boolean_true_node)
12373 warning_at (expr_loc, 0,
12374 "%<vector_length%> value must be positive");
12375 t = integer_one_node;
12378 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12380 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12381 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12382 OMP_CLAUSE_CHAIN (c) = list;
12383 list = c;
12386 return list;
12389 /* OpenMP 4.0:
12390 inbranch
12391 notinbranch */
12393 static tree
12394 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12395 enum omp_clause_code code, tree list)
12397 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12399 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12400 OMP_CLAUSE_CHAIN (c) = list;
12402 return c;
12405 /* OpenMP 4.0:
12406 parallel
12408 sections
12409 taskgroup */
12411 static tree
12412 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12413 enum omp_clause_code code, tree list)
12415 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12416 OMP_CLAUSE_CHAIN (c) = list;
12418 return c;
12421 /* OpenMP 4.5:
12422 nogroup */
12424 static tree
12425 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12427 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12428 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12429 OMP_CLAUSE_NOGROUP);
12430 OMP_CLAUSE_CHAIN (c) = list;
12431 return c;
12434 /* OpenMP 4.5:
12435 simd
12436 threads */
12438 static tree
12439 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12440 enum omp_clause_code code, tree list)
12442 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12443 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12444 OMP_CLAUSE_CHAIN (c) = list;
12445 return c;
12448 /* OpenMP 4.0:
12449 num_teams ( expression ) */
12451 static tree
12452 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12454 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12455 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12457 location_t expr_loc = c_parser_peek_token (parser)->location;
12458 c_expr expr = c_parser_expression (parser);
12459 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12460 tree c, t = expr.value;
12461 t = c_fully_fold (t, false, NULL);
12463 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12465 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12467 c_parser_error (parser, "expected integer expression");
12468 return list;
12471 /* Attempt to statically determine when the number isn't positive. */
12472 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12473 build_int_cst (TREE_TYPE (t), 0));
12474 protected_set_expr_location (c, expr_loc);
12475 if (c == boolean_true_node)
12477 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12478 t = integer_one_node;
12481 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12483 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12484 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12485 OMP_CLAUSE_CHAIN (c) = list;
12486 list = c;
12489 return list;
12492 /* OpenMP 4.0:
12493 thread_limit ( expression ) */
12495 static tree
12496 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12498 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12499 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12501 location_t expr_loc = c_parser_peek_token (parser)->location;
12502 c_expr expr = c_parser_expression (parser);
12503 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12504 tree c, t = expr.value;
12505 t = c_fully_fold (t, false, NULL);
12507 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12509 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12511 c_parser_error (parser, "expected integer expression");
12512 return list;
12515 /* Attempt to statically determine when the number isn't positive. */
12516 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12517 build_int_cst (TREE_TYPE (t), 0));
12518 protected_set_expr_location (c, expr_loc);
12519 if (c == boolean_true_node)
12521 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12522 t = integer_one_node;
12525 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12526 "thread_limit");
12528 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12529 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12530 OMP_CLAUSE_CHAIN (c) = list;
12531 list = c;
12534 return list;
12537 /* OpenMP 4.0:
12538 aligned ( variable-list )
12539 aligned ( variable-list : constant-expression ) */
12541 static tree
12542 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12544 location_t clause_loc = c_parser_peek_token (parser)->location;
12545 tree nl, c;
12547 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12548 return list;
12550 nl = c_parser_omp_variable_list (parser, clause_loc,
12551 OMP_CLAUSE_ALIGNED, list);
12553 if (c_parser_next_token_is (parser, CPP_COLON))
12555 c_parser_consume_token (parser);
12556 location_t expr_loc = c_parser_peek_token (parser)->location;
12557 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12558 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12559 tree alignment = expr.value;
12560 alignment = c_fully_fold (alignment, false, NULL);
12561 if (TREE_CODE (alignment) != INTEGER_CST
12562 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12563 || tree_int_cst_sgn (alignment) != 1)
12565 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12566 "be positive constant integer expression");
12567 alignment = NULL_TREE;
12570 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12571 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12575 return nl;
12578 /* OpenMP 4.0:
12579 linear ( variable-list )
12580 linear ( variable-list : expression )
12582 OpenMP 4.5:
12583 linear ( modifier ( variable-list ) )
12584 linear ( modifier ( variable-list ) : expression ) */
12586 static tree
12587 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12589 location_t clause_loc = c_parser_peek_token (parser)->location;
12590 tree nl, c, step;
12591 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12593 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12594 return list;
12596 if (!is_cilk_simd_fn
12597 && c_parser_next_token_is (parser, CPP_NAME))
12599 c_token *tok = c_parser_peek_token (parser);
12600 const char *p = IDENTIFIER_POINTER (tok->value);
12601 if (strcmp ("val", p) == 0)
12602 kind = OMP_CLAUSE_LINEAR_VAL;
12603 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12604 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12605 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12607 c_parser_consume_token (parser);
12608 c_parser_consume_token (parser);
12612 nl = c_parser_omp_variable_list (parser, clause_loc,
12613 OMP_CLAUSE_LINEAR, list);
12615 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12616 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12618 if (c_parser_next_token_is (parser, CPP_COLON))
12620 c_parser_consume_token (parser);
12621 location_t expr_loc = c_parser_peek_token (parser)->location;
12622 c_expr expr = c_parser_expression (parser);
12623 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12624 step = expr.value;
12625 step = c_fully_fold (step, false, NULL);
12626 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12628 sorry ("using parameters for %<linear%> step is not supported yet");
12629 step = integer_one_node;
12631 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12633 error_at (clause_loc, "%<linear%> clause step expression must "
12634 "be integral");
12635 step = integer_one_node;
12639 else
12640 step = integer_one_node;
12642 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12644 OMP_CLAUSE_LINEAR_STEP (c) = step;
12645 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12648 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12649 return nl;
12652 /* OpenMP 4.0:
12653 safelen ( constant-expression ) */
12655 static tree
12656 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12658 location_t clause_loc = c_parser_peek_token (parser)->location;
12659 tree c, t;
12661 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12662 return list;
12664 location_t expr_loc = c_parser_peek_token (parser)->location;
12665 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12666 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12667 t = expr.value;
12668 t = c_fully_fold (t, false, NULL);
12669 if (TREE_CODE (t) != INTEGER_CST
12670 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12671 || tree_int_cst_sgn (t) != 1)
12673 error_at (clause_loc, "%<safelen%> clause expression must "
12674 "be positive constant integer expression");
12675 t = NULL_TREE;
12678 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12679 if (t == NULL_TREE || t == error_mark_node)
12680 return list;
12682 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12684 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12685 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12686 OMP_CLAUSE_CHAIN (c) = list;
12687 return c;
12690 /* OpenMP 4.0:
12691 simdlen ( constant-expression ) */
12693 static tree
12694 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12696 location_t clause_loc = c_parser_peek_token (parser)->location;
12697 tree c, t;
12699 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12700 return list;
12702 location_t expr_loc = c_parser_peek_token (parser)->location;
12703 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12704 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12705 t = expr.value;
12706 t = c_fully_fold (t, false, NULL);
12707 if (TREE_CODE (t) != INTEGER_CST
12708 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12709 || tree_int_cst_sgn (t) != 1)
12711 error_at (clause_loc, "%<simdlen%> clause expression must "
12712 "be positive constant integer expression");
12713 t = NULL_TREE;
12716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12717 if (t == NULL_TREE || t == error_mark_node)
12718 return list;
12720 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12722 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12723 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12724 OMP_CLAUSE_CHAIN (c) = list;
12725 return c;
12728 /* OpenMP 4.5:
12729 vec:
12730 identifier [+/- integer]
12731 vec , identifier [+/- integer]
12734 static tree
12735 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12736 tree list)
12738 tree vec = NULL;
12739 if (c_parser_next_token_is_not (parser, CPP_NAME)
12740 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12742 c_parser_error (parser, "expected identifier");
12743 return list;
12746 while (c_parser_next_token_is (parser, CPP_NAME)
12747 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12749 tree t = lookup_name (c_parser_peek_token (parser)->value);
12750 tree addend = NULL;
12752 if (t == NULL_TREE)
12754 undeclared_variable (c_parser_peek_token (parser)->location,
12755 c_parser_peek_token (parser)->value);
12756 t = error_mark_node;
12759 c_parser_consume_token (parser);
12761 bool neg = false;
12762 if (c_parser_next_token_is (parser, CPP_MINUS))
12763 neg = true;
12764 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12766 addend = integer_zero_node;
12767 neg = false;
12768 goto add_to_vector;
12770 c_parser_consume_token (parser);
12772 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12774 c_parser_error (parser, "expected integer");
12775 return list;
12778 addend = c_parser_peek_token (parser)->value;
12779 if (TREE_CODE (addend) != INTEGER_CST)
12781 c_parser_error (parser, "expected integer");
12782 return list;
12784 c_parser_consume_token (parser);
12786 add_to_vector:
12787 if (t != error_mark_node)
12789 vec = tree_cons (addend, t, vec);
12790 if (neg)
12791 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12794 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12795 break;
12797 c_parser_consume_token (parser);
12800 if (vec == NULL_TREE)
12801 return list;
12803 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12804 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12805 OMP_CLAUSE_DECL (u) = nreverse (vec);
12806 OMP_CLAUSE_CHAIN (u) = list;
12807 return u;
12810 /* OpenMP 4.0:
12811 depend ( depend-kind: variable-list )
12813 depend-kind:
12814 in | out | inout
12816 OpenMP 4.5:
12817 depend ( source )
12819 depend ( sink : vec ) */
12821 static tree
12822 c_parser_omp_clause_depend (c_parser *parser, tree list)
12824 location_t clause_loc = c_parser_peek_token (parser)->location;
12825 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12826 tree nl, c;
12828 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12829 return list;
12831 if (c_parser_next_token_is (parser, CPP_NAME))
12833 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12834 if (strcmp ("in", p) == 0)
12835 kind = OMP_CLAUSE_DEPEND_IN;
12836 else if (strcmp ("inout", p) == 0)
12837 kind = OMP_CLAUSE_DEPEND_INOUT;
12838 else if (strcmp ("out", p) == 0)
12839 kind = OMP_CLAUSE_DEPEND_OUT;
12840 else if (strcmp ("source", p) == 0)
12841 kind = OMP_CLAUSE_DEPEND_SOURCE;
12842 else if (strcmp ("sink", p) == 0)
12843 kind = OMP_CLAUSE_DEPEND_SINK;
12844 else
12845 goto invalid_kind;
12847 else
12848 goto invalid_kind;
12850 c_parser_consume_token (parser);
12852 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12854 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12855 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12856 OMP_CLAUSE_DECL (c) = NULL_TREE;
12857 OMP_CLAUSE_CHAIN (c) = list;
12858 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12859 return c;
12862 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12863 goto resync_fail;
12865 if (kind == OMP_CLAUSE_DEPEND_SINK)
12866 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12867 else
12869 nl = c_parser_omp_variable_list (parser, clause_loc,
12870 OMP_CLAUSE_DEPEND, list);
12872 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12873 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12876 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12877 return nl;
12879 invalid_kind:
12880 c_parser_error (parser, "invalid depend kind");
12881 resync_fail:
12882 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12883 return list;
12886 /* OpenMP 4.0:
12887 map ( map-kind: variable-list )
12888 map ( variable-list )
12890 map-kind:
12891 alloc | to | from | tofrom
12893 OpenMP 4.5:
12894 map-kind:
12895 alloc | to | from | tofrom | release | delete
12897 map ( always [,] map-kind: variable-list ) */
12899 static tree
12900 c_parser_omp_clause_map (c_parser *parser, tree list)
12902 location_t clause_loc = c_parser_peek_token (parser)->location;
12903 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12904 int always = 0;
12905 enum c_id_kind always_id_kind = C_ID_NONE;
12906 location_t always_loc = UNKNOWN_LOCATION;
12907 tree always_id = NULL_TREE;
12908 tree nl, c;
12910 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12911 return list;
12913 if (c_parser_next_token_is (parser, CPP_NAME))
12915 c_token *tok = c_parser_peek_token (parser);
12916 const char *p = IDENTIFIER_POINTER (tok->value);
12917 always_id_kind = tok->id_kind;
12918 always_loc = tok->location;
12919 always_id = tok->value;
12920 if (strcmp ("always", p) == 0)
12922 c_token *sectok = c_parser_peek_2nd_token (parser);
12923 if (sectok->type == CPP_COMMA)
12925 c_parser_consume_token (parser);
12926 c_parser_consume_token (parser);
12927 always = 2;
12929 else if (sectok->type == CPP_NAME)
12931 p = IDENTIFIER_POINTER (sectok->value);
12932 if (strcmp ("alloc", p) == 0
12933 || strcmp ("to", p) == 0
12934 || strcmp ("from", p) == 0
12935 || strcmp ("tofrom", p) == 0
12936 || strcmp ("release", p) == 0
12937 || strcmp ("delete", p) == 0)
12939 c_parser_consume_token (parser);
12940 always = 1;
12946 if (c_parser_next_token_is (parser, CPP_NAME)
12947 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12949 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12950 if (strcmp ("alloc", p) == 0)
12951 kind = GOMP_MAP_ALLOC;
12952 else if (strcmp ("to", p) == 0)
12953 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12954 else if (strcmp ("from", p) == 0)
12955 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12956 else if (strcmp ("tofrom", p) == 0)
12957 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12958 else if (strcmp ("release", p) == 0)
12959 kind = GOMP_MAP_RELEASE;
12960 else if (strcmp ("delete", p) == 0)
12961 kind = GOMP_MAP_DELETE;
12962 else
12964 c_parser_error (parser, "invalid map kind");
12965 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12966 "expected %<)%>");
12967 return list;
12969 c_parser_consume_token (parser);
12970 c_parser_consume_token (parser);
12972 else if (always)
12974 if (always_id_kind != C_ID_ID)
12976 c_parser_error (parser, "expected identifier");
12977 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12978 return list;
12981 tree t = lookup_name (always_id);
12982 if (t == NULL_TREE)
12984 undeclared_variable (always_loc, always_id);
12985 t = error_mark_node;
12987 if (t != error_mark_node)
12989 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12990 OMP_CLAUSE_DECL (u) = t;
12991 OMP_CLAUSE_CHAIN (u) = list;
12992 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12993 list = u;
12995 if (always == 1)
12997 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12998 return list;
13002 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13004 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13005 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13007 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13008 return nl;
13011 /* OpenMP 4.0:
13012 device ( expression ) */
13014 static tree
13015 c_parser_omp_clause_device (c_parser *parser, tree list)
13017 location_t clause_loc = c_parser_peek_token (parser)->location;
13018 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13020 location_t expr_loc = c_parser_peek_token (parser)->location;
13021 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13022 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13023 tree c, t = expr.value;
13024 t = c_fully_fold (t, false, NULL);
13026 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13028 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13030 c_parser_error (parser, "expected integer expression");
13031 return list;
13034 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13036 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13037 OMP_CLAUSE_DEVICE_ID (c) = t;
13038 OMP_CLAUSE_CHAIN (c) = list;
13039 list = c;
13042 return list;
13045 /* OpenMP 4.0:
13046 dist_schedule ( static )
13047 dist_schedule ( static , expression ) */
13049 static tree
13050 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13052 tree c, t = NULL_TREE;
13053 location_t loc = c_parser_peek_token (parser)->location;
13055 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13056 return list;
13058 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13060 c_parser_error (parser, "invalid dist_schedule kind");
13061 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13062 "expected %<)%>");
13063 return list;
13066 c_parser_consume_token (parser);
13067 if (c_parser_next_token_is (parser, CPP_COMMA))
13069 c_parser_consume_token (parser);
13071 location_t expr_loc = c_parser_peek_token (parser)->location;
13072 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13073 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13074 t = expr.value;
13075 t = c_fully_fold (t, false, NULL);
13076 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13078 else
13079 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13080 "expected %<,%> or %<)%>");
13082 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13083 if (t == error_mark_node)
13084 return list;
13086 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13087 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13088 OMP_CLAUSE_CHAIN (c) = list;
13089 return c;
13092 /* OpenMP 4.0:
13093 proc_bind ( proc-bind-kind )
13095 proc-bind-kind:
13096 master | close | spread */
13098 static tree
13099 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13101 location_t clause_loc = c_parser_peek_token (parser)->location;
13102 enum omp_clause_proc_bind_kind kind;
13103 tree c;
13105 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13106 return list;
13108 if (c_parser_next_token_is (parser, CPP_NAME))
13110 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13111 if (strcmp ("master", p) == 0)
13112 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13113 else if (strcmp ("close", p) == 0)
13114 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13115 else if (strcmp ("spread", p) == 0)
13116 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13117 else
13118 goto invalid_kind;
13120 else
13121 goto invalid_kind;
13123 c_parser_consume_token (parser);
13124 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13125 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13126 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13127 OMP_CLAUSE_CHAIN (c) = list;
13128 return c;
13130 invalid_kind:
13131 c_parser_error (parser, "invalid proc_bind kind");
13132 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13133 return list;
13136 /* OpenMP 4.0:
13137 to ( variable-list ) */
13139 static tree
13140 c_parser_omp_clause_to (c_parser *parser, tree list)
13142 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13145 /* OpenMP 4.0:
13146 from ( variable-list ) */
13148 static tree
13149 c_parser_omp_clause_from (c_parser *parser, tree list)
13151 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13154 /* OpenMP 4.0:
13155 uniform ( variable-list ) */
13157 static tree
13158 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13160 /* The clauses location. */
13161 location_t loc = c_parser_peek_token (parser)->location;
13163 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13165 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13166 list);
13167 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13169 return list;
13172 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13173 is a bitmask in MASK. Return the list of clauses found. */
13175 static tree
13176 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13177 const char *where, bool finish_p = true)
13179 tree clauses = NULL;
13180 bool first = true;
13182 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13184 location_t here;
13185 pragma_omp_clause c_kind;
13186 const char *c_name;
13187 tree prev = clauses;
13189 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13190 c_parser_consume_token (parser);
13192 here = c_parser_peek_token (parser)->location;
13193 c_kind = c_parser_omp_clause_name (parser);
13195 switch (c_kind)
13197 case PRAGMA_OACC_CLAUSE_ASYNC:
13198 clauses = c_parser_oacc_clause_async (parser, clauses);
13199 c_name = "async";
13200 break;
13201 case PRAGMA_OACC_CLAUSE_AUTO:
13202 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13203 clauses);
13204 c_name = "auto";
13205 break;
13206 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13207 clauses = c_parser_omp_clause_collapse (parser, clauses);
13208 c_name = "collapse";
13209 break;
13210 case PRAGMA_OACC_CLAUSE_COPY:
13211 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13212 c_name = "copy";
13213 break;
13214 case PRAGMA_OACC_CLAUSE_COPYIN:
13215 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13216 c_name = "copyin";
13217 break;
13218 case PRAGMA_OACC_CLAUSE_COPYOUT:
13219 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13220 c_name = "copyout";
13221 break;
13222 case PRAGMA_OACC_CLAUSE_CREATE:
13223 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13224 c_name = "create";
13225 break;
13226 case PRAGMA_OACC_CLAUSE_DELETE:
13227 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13228 c_name = "delete";
13229 break;
13230 case PRAGMA_OMP_CLAUSE_DEFAULT:
13231 clauses = c_parser_omp_clause_default (parser, clauses, true);
13232 c_name = "default";
13233 break;
13234 case PRAGMA_OACC_CLAUSE_DEVICE:
13235 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13236 c_name = "device";
13237 break;
13238 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13239 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13240 c_name = "deviceptr";
13241 break;
13242 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13243 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13244 c_name = "device_resident";
13245 break;
13246 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13247 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13248 c_name = "firstprivate";
13249 break;
13250 case PRAGMA_OACC_CLAUSE_GANG:
13251 c_name = "gang";
13252 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13253 c_name, clauses);
13254 break;
13255 case PRAGMA_OACC_CLAUSE_HOST:
13256 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13257 c_name = "host";
13258 break;
13259 case PRAGMA_OACC_CLAUSE_IF:
13260 clauses = c_parser_omp_clause_if (parser, clauses, false);
13261 c_name = "if";
13262 break;
13263 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13264 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13265 clauses);
13266 c_name = "independent";
13267 break;
13268 case PRAGMA_OACC_CLAUSE_LINK:
13269 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13270 c_name = "link";
13271 break;
13272 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13273 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13274 c_name = "num_gangs";
13275 break;
13276 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13277 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13278 c_name = "num_workers";
13279 break;
13280 case PRAGMA_OACC_CLAUSE_PRESENT:
13281 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13282 c_name = "present";
13283 break;
13284 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13285 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13286 c_name = "present_or_copy";
13287 break;
13288 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13289 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13290 c_name = "present_or_copyin";
13291 break;
13292 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13293 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13294 c_name = "present_or_copyout";
13295 break;
13296 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13297 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13298 c_name = "present_or_create";
13299 break;
13300 case PRAGMA_OACC_CLAUSE_PRIVATE:
13301 clauses = c_parser_omp_clause_private (parser, clauses);
13302 c_name = "private";
13303 break;
13304 case PRAGMA_OACC_CLAUSE_REDUCTION:
13305 clauses = c_parser_omp_clause_reduction (parser, clauses);
13306 c_name = "reduction";
13307 break;
13308 case PRAGMA_OACC_CLAUSE_SELF:
13309 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13310 c_name = "self";
13311 break;
13312 case PRAGMA_OACC_CLAUSE_SEQ:
13313 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13314 clauses);
13315 c_name = "seq";
13316 break;
13317 case PRAGMA_OACC_CLAUSE_TILE:
13318 clauses = c_parser_oacc_clause_tile (parser, clauses);
13319 c_name = "tile";
13320 break;
13321 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13322 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13323 c_name = "use_device";
13324 break;
13325 case PRAGMA_OACC_CLAUSE_VECTOR:
13326 c_name = "vector";
13327 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13328 c_name, clauses);
13329 break;
13330 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13331 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13332 c_name = "vector_length";
13333 break;
13334 case PRAGMA_OACC_CLAUSE_WAIT:
13335 clauses = c_parser_oacc_clause_wait (parser, clauses);
13336 c_name = "wait";
13337 break;
13338 case PRAGMA_OACC_CLAUSE_WORKER:
13339 c_name = "worker";
13340 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13341 c_name, clauses);
13342 break;
13343 default:
13344 c_parser_error (parser, "expected %<#pragma acc%> clause");
13345 goto saw_error;
13348 first = false;
13350 if (((mask >> c_kind) & 1) == 0)
13352 /* Remove the invalid clause(s) from the list to avoid
13353 confusing the rest of the compiler. */
13354 clauses = prev;
13355 error_at (here, "%qs is not valid for %qs", c_name, where);
13359 saw_error:
13360 c_parser_skip_to_pragma_eol (parser);
13362 if (finish_p)
13363 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13365 return clauses;
13368 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13369 is a bitmask in MASK. Return the list of clauses found. */
13371 static tree
13372 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13373 const char *where, bool finish_p = true)
13375 tree clauses = NULL;
13376 bool first = true, cilk_simd_fn = false;
13378 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13380 location_t here;
13381 pragma_omp_clause c_kind;
13382 const char *c_name;
13383 tree prev = clauses;
13385 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13386 c_parser_consume_token (parser);
13388 here = c_parser_peek_token (parser)->location;
13389 c_kind = c_parser_omp_clause_name (parser);
13391 switch (c_kind)
13393 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13394 clauses = c_parser_omp_clause_collapse (parser, clauses);
13395 c_name = "collapse";
13396 break;
13397 case PRAGMA_OMP_CLAUSE_COPYIN:
13398 clauses = c_parser_omp_clause_copyin (parser, clauses);
13399 c_name = "copyin";
13400 break;
13401 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13402 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13403 c_name = "copyprivate";
13404 break;
13405 case PRAGMA_OMP_CLAUSE_DEFAULT:
13406 clauses = c_parser_omp_clause_default (parser, clauses, false);
13407 c_name = "default";
13408 break;
13409 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13410 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13411 c_name = "firstprivate";
13412 break;
13413 case PRAGMA_OMP_CLAUSE_FINAL:
13414 clauses = c_parser_omp_clause_final (parser, clauses);
13415 c_name = "final";
13416 break;
13417 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13418 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13419 c_name = "grainsize";
13420 break;
13421 case PRAGMA_OMP_CLAUSE_HINT:
13422 clauses = c_parser_omp_clause_hint (parser, clauses);
13423 c_name = "hint";
13424 break;
13425 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13426 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13427 c_name = "defaultmap";
13428 break;
13429 case PRAGMA_OMP_CLAUSE_IF:
13430 clauses = c_parser_omp_clause_if (parser, clauses, true);
13431 c_name = "if";
13432 break;
13433 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13434 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13435 c_name = "lastprivate";
13436 break;
13437 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13438 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13439 c_name = "mergeable";
13440 break;
13441 case PRAGMA_OMP_CLAUSE_NOWAIT:
13442 clauses = c_parser_omp_clause_nowait (parser, clauses);
13443 c_name = "nowait";
13444 break;
13445 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13446 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13447 c_name = "num_tasks";
13448 break;
13449 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13450 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13451 c_name = "num_threads";
13452 break;
13453 case PRAGMA_OMP_CLAUSE_ORDERED:
13454 clauses = c_parser_omp_clause_ordered (parser, clauses);
13455 c_name = "ordered";
13456 break;
13457 case PRAGMA_OMP_CLAUSE_PRIORITY:
13458 clauses = c_parser_omp_clause_priority (parser, clauses);
13459 c_name = "priority";
13460 break;
13461 case PRAGMA_OMP_CLAUSE_PRIVATE:
13462 clauses = c_parser_omp_clause_private (parser, clauses);
13463 c_name = "private";
13464 break;
13465 case PRAGMA_OMP_CLAUSE_REDUCTION:
13466 clauses = c_parser_omp_clause_reduction (parser, clauses);
13467 c_name = "reduction";
13468 break;
13469 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13470 clauses = c_parser_omp_clause_schedule (parser, clauses);
13471 c_name = "schedule";
13472 break;
13473 case PRAGMA_OMP_CLAUSE_SHARED:
13474 clauses = c_parser_omp_clause_shared (parser, clauses);
13475 c_name = "shared";
13476 break;
13477 case PRAGMA_OMP_CLAUSE_UNTIED:
13478 clauses = c_parser_omp_clause_untied (parser, clauses);
13479 c_name = "untied";
13480 break;
13481 case PRAGMA_OMP_CLAUSE_INBRANCH:
13482 case PRAGMA_CILK_CLAUSE_MASK:
13483 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13484 clauses);
13485 c_name = "inbranch";
13486 break;
13487 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13488 case PRAGMA_CILK_CLAUSE_NOMASK:
13489 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13490 clauses);
13491 c_name = "notinbranch";
13492 break;
13493 case PRAGMA_OMP_CLAUSE_PARALLEL:
13494 clauses
13495 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13496 clauses);
13497 c_name = "parallel";
13498 if (!first)
13500 clause_not_first:
13501 error_at (here, "%qs must be the first clause of %qs",
13502 c_name, where);
13503 clauses = prev;
13505 break;
13506 case PRAGMA_OMP_CLAUSE_FOR:
13507 clauses
13508 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13509 clauses);
13510 c_name = "for";
13511 if (!first)
13512 goto clause_not_first;
13513 break;
13514 case PRAGMA_OMP_CLAUSE_SECTIONS:
13515 clauses
13516 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13517 clauses);
13518 c_name = "sections";
13519 if (!first)
13520 goto clause_not_first;
13521 break;
13522 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13523 clauses
13524 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13525 clauses);
13526 c_name = "taskgroup";
13527 if (!first)
13528 goto clause_not_first;
13529 break;
13530 case PRAGMA_OMP_CLAUSE_LINK:
13531 clauses
13532 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13533 c_name = "link";
13534 break;
13535 case PRAGMA_OMP_CLAUSE_TO:
13536 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13537 clauses
13538 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13539 clauses);
13540 else
13541 clauses = c_parser_omp_clause_to (parser, clauses);
13542 c_name = "to";
13543 break;
13544 case PRAGMA_OMP_CLAUSE_FROM:
13545 clauses = c_parser_omp_clause_from (parser, clauses);
13546 c_name = "from";
13547 break;
13548 case PRAGMA_OMP_CLAUSE_UNIFORM:
13549 clauses = c_parser_omp_clause_uniform (parser, clauses);
13550 c_name = "uniform";
13551 break;
13552 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13553 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13554 c_name = "num_teams";
13555 break;
13556 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13557 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13558 c_name = "thread_limit";
13559 break;
13560 case PRAGMA_OMP_CLAUSE_ALIGNED:
13561 clauses = c_parser_omp_clause_aligned (parser, clauses);
13562 c_name = "aligned";
13563 break;
13564 case PRAGMA_OMP_CLAUSE_LINEAR:
13565 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13566 cilk_simd_fn = true;
13567 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13568 c_name = "linear";
13569 break;
13570 case PRAGMA_OMP_CLAUSE_DEPEND:
13571 clauses = c_parser_omp_clause_depend (parser, clauses);
13572 c_name = "depend";
13573 break;
13574 case PRAGMA_OMP_CLAUSE_MAP:
13575 clauses = c_parser_omp_clause_map (parser, clauses);
13576 c_name = "map";
13577 break;
13578 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13579 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13580 c_name = "use_device_ptr";
13581 break;
13582 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13583 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13584 c_name = "is_device_ptr";
13585 break;
13586 case PRAGMA_OMP_CLAUSE_DEVICE:
13587 clauses = c_parser_omp_clause_device (parser, clauses);
13588 c_name = "device";
13589 break;
13590 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13591 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13592 c_name = "dist_schedule";
13593 break;
13594 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13595 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13596 c_name = "proc_bind";
13597 break;
13598 case PRAGMA_OMP_CLAUSE_SAFELEN:
13599 clauses = c_parser_omp_clause_safelen (parser, clauses);
13600 c_name = "safelen";
13601 break;
13602 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13603 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13604 c_name = "simdlen";
13605 break;
13606 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13607 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13608 c_name = "simdlen";
13609 break;
13610 case PRAGMA_OMP_CLAUSE_NOGROUP:
13611 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13612 c_name = "nogroup";
13613 break;
13614 case PRAGMA_OMP_CLAUSE_THREADS:
13615 clauses
13616 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13617 clauses);
13618 c_name = "threads";
13619 break;
13620 case PRAGMA_OMP_CLAUSE_SIMD:
13621 clauses
13622 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13623 clauses);
13624 c_name = "simd";
13625 break;
13626 default:
13627 c_parser_error (parser, "expected %<#pragma omp%> clause");
13628 goto saw_error;
13631 first = false;
13633 if (((mask >> c_kind) & 1) == 0)
13635 /* Remove the invalid clause(s) from the list to avoid
13636 confusing the rest of the compiler. */
13637 clauses = prev;
13638 error_at (here, "%qs is not valid for %qs", c_name, where);
13642 saw_error:
13643 c_parser_skip_to_pragma_eol (parser);
13645 if (finish_p)
13647 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13648 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
13649 return c_finish_omp_clauses (clauses, C_ORT_OMP);
13652 return clauses;
13655 /* OpenACC 2.0, OpenMP 2.5:
13656 structured-block:
13657 statement
13659 In practice, we're also interested in adding the statement to an
13660 outer node. So it is convenient if we work around the fact that
13661 c_parser_statement calls add_stmt. */
13663 static tree
13664 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13666 tree stmt = push_stmt_list ();
13667 c_parser_statement (parser, if_p);
13668 return pop_stmt_list (stmt);
13671 /* OpenACC 2.0:
13672 # pragma acc cache (variable-list) new-line
13674 LOC is the location of the #pragma token.
13677 static tree
13678 c_parser_oacc_cache (location_t loc, c_parser *parser)
13680 tree stmt, clauses;
13682 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13683 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13685 c_parser_skip_to_pragma_eol (parser);
13687 stmt = make_node (OACC_CACHE);
13688 TREE_TYPE (stmt) = void_type_node;
13689 OACC_CACHE_CLAUSES (stmt) = clauses;
13690 SET_EXPR_LOCATION (stmt, loc);
13691 add_stmt (stmt);
13693 return stmt;
13696 /* OpenACC 2.0:
13697 # pragma acc data oacc-data-clause[optseq] new-line
13698 structured-block
13700 LOC is the location of the #pragma token.
13703 #define OACC_DATA_CLAUSE_MASK \
13704 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13716 static tree
13717 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13719 tree stmt, clauses, block;
13721 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13722 "#pragma acc data");
13724 block = c_begin_omp_parallel ();
13725 add_stmt (c_parser_omp_structured_block (parser, if_p));
13727 stmt = c_finish_oacc_data (loc, clauses, block);
13729 return stmt;
13732 /* OpenACC 2.0:
13733 # pragma acc declare oacc-data-clause[optseq] new-line
13736 #define OACC_DECLARE_CLAUSE_MASK \
13737 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13750 static void
13751 c_parser_oacc_declare (c_parser *parser)
13753 location_t pragma_loc = c_parser_peek_token (parser)->location;
13754 tree clauses, stmt, t, decl;
13756 bool error = false;
13758 c_parser_consume_pragma (parser);
13760 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13761 "#pragma acc declare");
13762 if (!clauses)
13764 error_at (pragma_loc,
13765 "no valid clauses specified in %<#pragma acc declare%>");
13766 return;
13769 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13771 location_t loc = OMP_CLAUSE_LOCATION (t);
13772 decl = OMP_CLAUSE_DECL (t);
13773 if (!DECL_P (decl))
13775 error_at (loc, "array section in %<#pragma acc declare%>");
13776 error = true;
13777 continue;
13780 switch (OMP_CLAUSE_MAP_KIND (t))
13782 case GOMP_MAP_FIRSTPRIVATE_POINTER:
13783 case GOMP_MAP_FORCE_ALLOC:
13784 case GOMP_MAP_FORCE_TO:
13785 case GOMP_MAP_FORCE_DEVICEPTR:
13786 case GOMP_MAP_DEVICE_RESIDENT:
13787 break;
13789 case GOMP_MAP_LINK:
13790 if (!global_bindings_p ()
13791 && (TREE_STATIC (decl)
13792 || !DECL_EXTERNAL (decl)))
13794 error_at (loc,
13795 "%qD must be a global variable in"
13796 "%<#pragma acc declare link%>",
13797 decl);
13798 error = true;
13799 continue;
13801 break;
13803 default:
13804 if (global_bindings_p ())
13806 error_at (loc, "invalid OpenACC clause at file scope");
13807 error = true;
13808 continue;
13810 if (DECL_EXTERNAL (decl))
13812 error_at (loc,
13813 "invalid use of %<extern%> variable %qD "
13814 "in %<#pragma acc declare%>", decl);
13815 error = true;
13816 continue;
13818 else if (TREE_PUBLIC (decl))
13820 error_at (loc,
13821 "invalid use of %<global%> variable %qD "
13822 "in %<#pragma acc declare%>", decl);
13823 error = true;
13824 continue;
13826 break;
13829 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13830 || lookup_attribute ("omp declare target link",
13831 DECL_ATTRIBUTES (decl)))
13833 error_at (loc, "variable %qD used more than once with "
13834 "%<#pragma acc declare%>", decl);
13835 error = true;
13836 continue;
13839 if (!error)
13841 tree id;
13843 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13844 id = get_identifier ("omp declare target link");
13845 else
13846 id = get_identifier ("omp declare target");
13848 DECL_ATTRIBUTES (decl)
13849 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13851 if (global_bindings_p ())
13853 symtab_node *node = symtab_node::get (decl);
13854 if (node != NULL)
13856 node->offloadable = 1;
13857 if (ENABLE_OFFLOADING)
13859 g->have_offload = true;
13860 if (is_a <varpool_node *> (node))
13861 vec_safe_push (offload_vars, decl);
13868 if (error || global_bindings_p ())
13869 return;
13871 stmt = make_node (OACC_DECLARE);
13872 TREE_TYPE (stmt) = void_type_node;
13873 OACC_DECLARE_CLAUSES (stmt) = clauses;
13874 SET_EXPR_LOCATION (stmt, pragma_loc);
13876 add_stmt (stmt);
13878 return;
13881 /* OpenACC 2.0:
13882 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13886 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13889 LOC is the location of the #pragma token.
13892 #define OACC_ENTER_DATA_CLAUSE_MASK \
13893 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13901 #define OACC_EXIT_DATA_CLAUSE_MASK \
13902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13908 static void
13909 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13911 location_t loc = c_parser_peek_token (parser)->location;
13912 tree clauses, stmt;
13913 const char *p = "";
13915 c_parser_consume_pragma (parser);
13917 if (c_parser_next_token_is (parser, CPP_NAME))
13919 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13920 c_parser_consume_token (parser);
13923 if (strcmp (p, "data") != 0)
13925 error_at (loc, enter
13926 ? "expected %<data%> after %<#pragma acc enter%>"
13927 : "expected %<data%> after %<#pragma acc exit%>");
13928 parser->error = true;
13929 c_parser_skip_to_pragma_eol (parser);
13930 return;
13933 if (enter)
13934 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13935 "#pragma acc enter data");
13936 else
13937 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13938 "#pragma acc exit data");
13940 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13942 error_at (loc, enter
13943 ? "%<#pragma acc enter data%> has no data movement clause"
13944 : "%<#pragma acc exit data%> has no data movement clause");
13945 return;
13948 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13949 TREE_TYPE (stmt) = void_type_node;
13950 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13951 SET_EXPR_LOCATION (stmt, loc);
13952 add_stmt (stmt);
13956 /* OpenACC 2.0:
13957 # pragma acc host_data oacc-data-clause[optseq] new-line
13958 structured-block
13961 #define OACC_HOST_DATA_CLAUSE_MASK \
13962 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13964 static tree
13965 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
13967 tree stmt, clauses, block;
13969 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13970 "#pragma acc host_data");
13972 block = c_begin_omp_parallel ();
13973 add_stmt (c_parser_omp_structured_block (parser, if_p));
13974 stmt = c_finish_oacc_host_data (loc, clauses, block);
13975 return stmt;
13979 /* OpenACC 2.0:
13981 # pragma acc loop oacc-loop-clause[optseq] new-line
13982 structured-block
13984 LOC is the location of the #pragma token.
13987 #define OACC_LOOP_CLAUSE_MASK \
13988 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
13996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
13998 static tree
13999 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14000 omp_clause_mask mask, tree *cclauses, bool *if_p)
14002 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14004 strcat (p_name, " loop");
14005 mask |= OACC_LOOP_CLAUSE_MASK;
14007 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14008 cclauses == NULL);
14009 if (cclauses)
14011 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14012 if (*cclauses)
14013 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14014 if (clauses)
14015 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14018 tree block = c_begin_compound_stmt (true);
14019 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14020 if_p);
14021 block = c_end_compound_stmt (loc, block, true);
14022 add_stmt (block);
14024 return stmt;
14027 /* OpenACC 2.0:
14028 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14029 structured-block
14033 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14034 structured-block
14036 LOC is the location of the #pragma token.
14039 #define OACC_KERNELS_CLAUSE_MASK \
14040 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14055 #define OACC_PARALLEL_CLAUSE_MASK \
14056 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14077 static tree
14078 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14079 enum pragma_kind p_kind, char *p_name,
14080 bool *if_p)
14082 omp_clause_mask mask;
14083 enum tree_code code;
14084 switch (p_kind)
14086 case PRAGMA_OACC_KERNELS:
14087 strcat (p_name, " kernels");
14088 mask = OACC_KERNELS_CLAUSE_MASK;
14089 code = OACC_KERNELS;
14090 break;
14091 case PRAGMA_OACC_PARALLEL:
14092 strcat (p_name, " parallel");
14093 mask = OACC_PARALLEL_CLAUSE_MASK;
14094 code = OACC_PARALLEL;
14095 break;
14096 default:
14097 gcc_unreachable ();
14100 if (c_parser_next_token_is (parser, CPP_NAME))
14102 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14103 if (strcmp (p, "loop") == 0)
14105 c_parser_consume_token (parser);
14106 tree block = c_begin_omp_parallel ();
14107 tree clauses;
14108 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14109 return c_finish_omp_construct (loc, code, block, clauses);
14113 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14115 tree block = c_begin_omp_parallel ();
14116 add_stmt (c_parser_omp_structured_block (parser, if_p));
14118 return c_finish_omp_construct (loc, code, block, clauses);
14121 /* OpenACC 2.0:
14122 # pragma acc routine oacc-routine-clause[optseq] new-line
14123 function-definition
14125 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14128 #define OACC_ROUTINE_CLAUSE_MASK \
14129 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14134 /* Parse an OpenACC routine directive. For named directives, we apply
14135 immediately to the named function. For unnamed ones we then parse
14136 a declaration or definition, which must be for a function. */
14138 static void
14139 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14141 gcc_checking_assert (context == pragma_external);
14143 oacc_routine_data data;
14144 data.error_seen = false;
14145 data.fndecl_seen = false;
14146 data.clauses = NULL_TREE;
14147 data.loc = c_parser_peek_token (parser)->location;
14149 c_parser_consume_pragma (parser);
14151 /* Look for optional '( name )'. */
14152 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14154 c_parser_consume_token (parser); /* '(' */
14156 tree decl = NULL_TREE;
14157 c_token *name_token = c_parser_peek_token (parser);
14158 location_t name_loc = name_token->location;
14159 if (name_token->type == CPP_NAME
14160 && (name_token->id_kind == C_ID_ID
14161 || name_token->id_kind == C_ID_TYPENAME))
14163 decl = lookup_name (name_token->value);
14164 if (!decl)
14165 error_at (name_loc,
14166 "%qE has not been declared", name_token->value);
14167 c_parser_consume_token (parser);
14169 else
14170 c_parser_error (parser, "expected function name");
14172 if (!decl
14173 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14175 c_parser_skip_to_pragma_eol (parser, false);
14176 return;
14179 data.clauses
14180 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14181 "#pragma acc routine");
14183 if (TREE_CODE (decl) != FUNCTION_DECL)
14185 error_at (name_loc, "%qD does not refer to a function", decl);
14186 return;
14189 c_finish_oacc_routine (&data, decl, false);
14191 else /* No optional '( name )'. */
14193 data.clauses
14194 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14195 "#pragma acc routine");
14197 /* Emit a helpful diagnostic if there's another pragma following this
14198 one. Also don't allow a static assertion declaration, as in the
14199 following we'll just parse a *single* "declaration or function
14200 definition", and the static assertion counts an one. */
14201 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14202 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14204 error_at (data.loc,
14205 "%<#pragma acc routine%> not immediately followed by"
14206 " function declaration or definition");
14207 /* ..., and then just keep going. */
14208 return;
14211 /* We only have to consider the pragma_external case here. */
14212 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14213 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14215 int ext = disable_extension_diagnostics ();
14217 c_parser_consume_token (parser);
14218 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14219 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14220 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14221 NULL, vNULL, &data);
14222 restore_extension_diagnostics (ext);
14224 else
14225 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14226 NULL, vNULL, &data);
14230 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14231 IS_DEFN is true if we're applying it to the definition. */
14233 static void
14234 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14235 bool is_defn)
14237 /* Keep going if we're in error reporting mode. */
14238 if (data->error_seen
14239 || fndecl == error_mark_node)
14240 return;
14242 if (data->fndecl_seen)
14244 error_at (data->loc,
14245 "%<#pragma acc routine%> not immediately followed by"
14246 " a single function declaration or definition");
14247 data->error_seen = true;
14248 return;
14250 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14252 error_at (data->loc,
14253 "%<#pragma acc routine%> not immediately followed by"
14254 " function declaration or definition");
14255 data->error_seen = true;
14256 return;
14259 if (oacc_get_fn_attrib (fndecl))
14261 error_at (data->loc,
14262 "%<#pragma acc routine%> already applied to %qD", fndecl);
14263 data->error_seen = true;
14264 return;
14267 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14269 error_at (data->loc,
14270 "%<#pragma acc routine%> must be applied before %s",
14271 TREE_USED (fndecl) ? "use" : "definition");
14272 data->error_seen = true;
14273 return;
14276 /* Process the routine's dimension clauses. */
14277 tree dims = oacc_build_routine_dims (data->clauses);
14278 oacc_replace_fn_attrib (fndecl, dims);
14280 /* Add an "omp declare target" attribute. */
14281 DECL_ATTRIBUTES (fndecl)
14282 = tree_cons (get_identifier ("omp declare target"),
14283 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14285 /* Remember that we've used this "#pragma acc routine". */
14286 data->fndecl_seen = true;
14289 /* OpenACC 2.0:
14290 # pragma acc update oacc-update-clause[optseq] new-line
14293 #define OACC_UPDATE_CLAUSE_MASK \
14294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14301 static void
14302 c_parser_oacc_update (c_parser *parser)
14304 location_t loc = c_parser_peek_token (parser)->location;
14306 c_parser_consume_pragma (parser);
14308 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14309 "#pragma acc update");
14310 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14312 error_at (loc,
14313 "%<#pragma acc update%> must contain at least one "
14314 "%<device%> or %<host%> or %<self%> clause");
14315 return;
14318 if (parser->error)
14319 return;
14321 tree stmt = make_node (OACC_UPDATE);
14322 TREE_TYPE (stmt) = void_type_node;
14323 OACC_UPDATE_CLAUSES (stmt) = clauses;
14324 SET_EXPR_LOCATION (stmt, loc);
14325 add_stmt (stmt);
14328 /* OpenACC 2.0:
14329 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14331 LOC is the location of the #pragma token.
14334 #define OACC_WAIT_CLAUSE_MASK \
14335 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14337 static tree
14338 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14340 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14342 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14343 list = c_parser_oacc_wait_list (parser, loc, list);
14345 strcpy (p_name, " wait");
14346 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14347 stmt = c_finish_oacc_wait (loc, list, clauses);
14348 add_stmt (stmt);
14350 return stmt;
14353 /* OpenMP 2.5:
14354 # pragma omp atomic new-line
14355 expression-stmt
14357 expression-stmt:
14358 x binop= expr | x++ | ++x | x-- | --x
14359 binop:
14360 +, *, -, /, &, ^, |, <<, >>
14362 where x is an lvalue expression with scalar type.
14364 OpenMP 3.1:
14365 # pragma omp atomic new-line
14366 update-stmt
14368 # pragma omp atomic read new-line
14369 read-stmt
14371 # pragma omp atomic write new-line
14372 write-stmt
14374 # pragma omp atomic update new-line
14375 update-stmt
14377 # pragma omp atomic capture new-line
14378 capture-stmt
14380 # pragma omp atomic capture new-line
14381 capture-block
14383 read-stmt:
14384 v = x
14385 write-stmt:
14386 x = expr
14387 update-stmt:
14388 expression-stmt | x = x binop expr
14389 capture-stmt:
14390 v = expression-stmt
14391 capture-block:
14392 { v = x; update-stmt; } | { update-stmt; v = x; }
14394 OpenMP 4.0:
14395 update-stmt:
14396 expression-stmt | x = x binop expr | x = expr binop x
14397 capture-stmt:
14398 v = update-stmt
14399 capture-block:
14400 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14402 where x and v are lvalue expressions with scalar type.
14404 LOC is the location of the #pragma token. */
14406 static void
14407 c_parser_omp_atomic (location_t loc, c_parser *parser)
14409 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14410 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14411 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14412 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14413 struct c_expr expr;
14414 location_t eloc;
14415 bool structured_block = false;
14416 bool swapped = false;
14417 bool seq_cst = false;
14418 bool non_lvalue_p;
14420 if (c_parser_next_token_is (parser, CPP_NAME))
14422 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14423 if (!strcmp (p, "seq_cst"))
14425 seq_cst = true;
14426 c_parser_consume_token (parser);
14427 if (c_parser_next_token_is (parser, CPP_COMMA)
14428 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14429 c_parser_consume_token (parser);
14432 if (c_parser_next_token_is (parser, CPP_NAME))
14434 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14436 if (!strcmp (p, "read"))
14437 code = OMP_ATOMIC_READ;
14438 else if (!strcmp (p, "write"))
14439 code = NOP_EXPR;
14440 else if (!strcmp (p, "update"))
14441 code = OMP_ATOMIC;
14442 else if (!strcmp (p, "capture"))
14443 code = OMP_ATOMIC_CAPTURE_NEW;
14444 else
14445 p = NULL;
14446 if (p)
14447 c_parser_consume_token (parser);
14449 if (!seq_cst)
14451 if (c_parser_next_token_is (parser, CPP_COMMA)
14452 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14453 c_parser_consume_token (parser);
14455 if (c_parser_next_token_is (parser, CPP_NAME))
14457 const char *p
14458 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14459 if (!strcmp (p, "seq_cst"))
14461 seq_cst = true;
14462 c_parser_consume_token (parser);
14466 c_parser_skip_to_pragma_eol (parser);
14468 switch (code)
14470 case OMP_ATOMIC_READ:
14471 case NOP_EXPR: /* atomic write */
14472 v = c_parser_cast_expression (parser, NULL).value;
14473 non_lvalue_p = !lvalue_p (v);
14474 v = c_fully_fold (v, false, NULL);
14475 if (v == error_mark_node)
14476 goto saw_error;
14477 if (non_lvalue_p)
14478 v = non_lvalue (v);
14479 loc = c_parser_peek_token (parser)->location;
14480 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14481 goto saw_error;
14482 if (code == NOP_EXPR)
14484 lhs = c_parser_expression (parser).value;
14485 lhs = c_fully_fold (lhs, false, NULL);
14486 if (lhs == error_mark_node)
14487 goto saw_error;
14489 else
14491 lhs = c_parser_cast_expression (parser, NULL).value;
14492 non_lvalue_p = !lvalue_p (lhs);
14493 lhs = c_fully_fold (lhs, false, NULL);
14494 if (lhs == error_mark_node)
14495 goto saw_error;
14496 if (non_lvalue_p)
14497 lhs = non_lvalue (lhs);
14499 if (code == NOP_EXPR)
14501 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14502 opcode. */
14503 code = OMP_ATOMIC;
14504 rhs = lhs;
14505 lhs = v;
14506 v = NULL_TREE;
14508 goto done;
14509 case OMP_ATOMIC_CAPTURE_NEW:
14510 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14512 c_parser_consume_token (parser);
14513 structured_block = true;
14515 else
14517 v = c_parser_cast_expression (parser, NULL).value;
14518 non_lvalue_p = !lvalue_p (v);
14519 v = c_fully_fold (v, false, NULL);
14520 if (v == error_mark_node)
14521 goto saw_error;
14522 if (non_lvalue_p)
14523 v = non_lvalue (v);
14524 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14525 goto saw_error;
14527 break;
14528 default:
14529 break;
14532 /* For structured_block case we don't know yet whether
14533 old or new x should be captured. */
14534 restart:
14535 eloc = c_parser_peek_token (parser)->location;
14536 expr = c_parser_cast_expression (parser, NULL);
14537 lhs = expr.value;
14538 expr = default_function_array_conversion (eloc, expr);
14539 unfolded_lhs = expr.value;
14540 lhs = c_fully_fold (lhs, false, NULL);
14541 orig_lhs = lhs;
14542 switch (TREE_CODE (lhs))
14544 case ERROR_MARK:
14545 saw_error:
14546 c_parser_skip_to_end_of_block_or_statement (parser);
14547 if (structured_block)
14549 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14550 c_parser_consume_token (parser);
14551 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14553 c_parser_skip_to_end_of_block_or_statement (parser);
14554 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14555 c_parser_consume_token (parser);
14558 return;
14560 case POSTINCREMENT_EXPR:
14561 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14562 code = OMP_ATOMIC_CAPTURE_OLD;
14563 /* FALLTHROUGH */
14564 case PREINCREMENT_EXPR:
14565 lhs = TREE_OPERAND (lhs, 0);
14566 unfolded_lhs = NULL_TREE;
14567 opcode = PLUS_EXPR;
14568 rhs = integer_one_node;
14569 break;
14571 case POSTDECREMENT_EXPR:
14572 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14573 code = OMP_ATOMIC_CAPTURE_OLD;
14574 /* FALLTHROUGH */
14575 case PREDECREMENT_EXPR:
14576 lhs = TREE_OPERAND (lhs, 0);
14577 unfolded_lhs = NULL_TREE;
14578 opcode = MINUS_EXPR;
14579 rhs = integer_one_node;
14580 break;
14582 case COMPOUND_EXPR:
14583 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14584 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14585 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14586 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14587 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14588 (TREE_OPERAND (lhs, 1), 0), 0)))
14589 == BOOLEAN_TYPE)
14590 /* Undo effects of boolean_increment for post {in,de}crement. */
14591 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14592 /* FALLTHRU */
14593 case MODIFY_EXPR:
14594 if (TREE_CODE (lhs) == MODIFY_EXPR
14595 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14597 /* Undo effects of boolean_increment. */
14598 if (integer_onep (TREE_OPERAND (lhs, 1)))
14600 /* This is pre or post increment. */
14601 rhs = TREE_OPERAND (lhs, 1);
14602 lhs = TREE_OPERAND (lhs, 0);
14603 unfolded_lhs = NULL_TREE;
14604 opcode = NOP_EXPR;
14605 if (code == OMP_ATOMIC_CAPTURE_NEW
14606 && !structured_block
14607 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14608 code = OMP_ATOMIC_CAPTURE_OLD;
14609 break;
14611 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14612 && TREE_OPERAND (lhs, 0)
14613 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14615 /* This is pre or post decrement. */
14616 rhs = TREE_OPERAND (lhs, 1);
14617 lhs = TREE_OPERAND (lhs, 0);
14618 unfolded_lhs = NULL_TREE;
14619 opcode = NOP_EXPR;
14620 if (code == OMP_ATOMIC_CAPTURE_NEW
14621 && !structured_block
14622 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14623 code = OMP_ATOMIC_CAPTURE_OLD;
14624 break;
14627 /* FALLTHRU */
14628 default:
14629 if (!lvalue_p (unfolded_lhs))
14630 lhs = non_lvalue (lhs);
14631 switch (c_parser_peek_token (parser)->type)
14633 case CPP_MULT_EQ:
14634 opcode = MULT_EXPR;
14635 break;
14636 case CPP_DIV_EQ:
14637 opcode = TRUNC_DIV_EXPR;
14638 break;
14639 case CPP_PLUS_EQ:
14640 opcode = PLUS_EXPR;
14641 break;
14642 case CPP_MINUS_EQ:
14643 opcode = MINUS_EXPR;
14644 break;
14645 case CPP_LSHIFT_EQ:
14646 opcode = LSHIFT_EXPR;
14647 break;
14648 case CPP_RSHIFT_EQ:
14649 opcode = RSHIFT_EXPR;
14650 break;
14651 case CPP_AND_EQ:
14652 opcode = BIT_AND_EXPR;
14653 break;
14654 case CPP_OR_EQ:
14655 opcode = BIT_IOR_EXPR;
14656 break;
14657 case CPP_XOR_EQ:
14658 opcode = BIT_XOR_EXPR;
14659 break;
14660 case CPP_EQ:
14661 c_parser_consume_token (parser);
14662 eloc = c_parser_peek_token (parser)->location;
14663 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14664 rhs1 = expr.value;
14665 switch (TREE_CODE (rhs1))
14667 case MULT_EXPR:
14668 case TRUNC_DIV_EXPR:
14669 case RDIV_EXPR:
14670 case PLUS_EXPR:
14671 case MINUS_EXPR:
14672 case LSHIFT_EXPR:
14673 case RSHIFT_EXPR:
14674 case BIT_AND_EXPR:
14675 case BIT_IOR_EXPR:
14676 case BIT_XOR_EXPR:
14677 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14679 opcode = TREE_CODE (rhs1);
14680 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14681 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14682 goto stmt_done;
14684 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14686 opcode = TREE_CODE (rhs1);
14687 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14688 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14689 swapped = !commutative_tree_code (opcode);
14690 goto stmt_done;
14692 break;
14693 case ERROR_MARK:
14694 goto saw_error;
14695 default:
14696 break;
14698 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14700 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14702 code = OMP_ATOMIC_CAPTURE_OLD;
14703 v = lhs;
14704 lhs = NULL_TREE;
14705 expr = default_function_array_read_conversion (eloc, expr);
14706 unfolded_lhs1 = expr.value;
14707 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14708 rhs1 = NULL_TREE;
14709 c_parser_consume_token (parser);
14710 goto restart;
14712 if (structured_block)
14714 opcode = NOP_EXPR;
14715 expr = default_function_array_read_conversion (eloc, expr);
14716 rhs = c_fully_fold (expr.value, false, NULL);
14717 rhs1 = NULL_TREE;
14718 goto stmt_done;
14721 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14722 goto saw_error;
14723 default:
14724 c_parser_error (parser,
14725 "invalid operator for %<#pragma omp atomic%>");
14726 goto saw_error;
14729 /* Arrange to pass the location of the assignment operator to
14730 c_finish_omp_atomic. */
14731 loc = c_parser_peek_token (parser)->location;
14732 c_parser_consume_token (parser);
14733 eloc = c_parser_peek_token (parser)->location;
14734 expr = c_parser_expression (parser);
14735 expr = default_function_array_read_conversion (eloc, expr);
14736 rhs = expr.value;
14737 rhs = c_fully_fold (rhs, false, NULL);
14738 break;
14740 stmt_done:
14741 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14743 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14744 goto saw_error;
14745 v = c_parser_cast_expression (parser, NULL).value;
14746 non_lvalue_p = !lvalue_p (v);
14747 v = c_fully_fold (v, false, NULL);
14748 if (v == error_mark_node)
14749 goto saw_error;
14750 if (non_lvalue_p)
14751 v = non_lvalue (v);
14752 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14753 goto saw_error;
14754 eloc = c_parser_peek_token (parser)->location;
14755 expr = c_parser_cast_expression (parser, NULL);
14756 lhs1 = expr.value;
14757 expr = default_function_array_read_conversion (eloc, expr);
14758 unfolded_lhs1 = expr.value;
14759 lhs1 = c_fully_fold (lhs1, false, NULL);
14760 if (lhs1 == error_mark_node)
14761 goto saw_error;
14762 if (!lvalue_p (unfolded_lhs1))
14763 lhs1 = non_lvalue (lhs1);
14765 if (structured_block)
14767 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14768 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14770 done:
14771 if (unfolded_lhs && unfolded_lhs1
14772 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14774 error ("%<#pragma omp atomic capture%> uses two different "
14775 "expressions for memory");
14776 stmt = error_mark_node;
14778 else
14779 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14780 swapped, seq_cst);
14781 if (stmt != error_mark_node)
14782 add_stmt (stmt);
14784 if (!structured_block)
14785 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14789 /* OpenMP 2.5:
14790 # pragma omp barrier new-line
14793 static void
14794 c_parser_omp_barrier (c_parser *parser)
14796 location_t loc = c_parser_peek_token (parser)->location;
14797 c_parser_consume_pragma (parser);
14798 c_parser_skip_to_pragma_eol (parser);
14800 c_finish_omp_barrier (loc);
14803 /* OpenMP 2.5:
14804 # pragma omp critical [(name)] new-line
14805 structured-block
14807 OpenMP 4.5:
14808 # pragma omp critical [(name) [hint(expression)]] new-line
14810 LOC is the location of the #pragma itself. */
14812 #define OMP_CRITICAL_CLAUSE_MASK \
14813 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14815 static tree
14816 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
14818 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14820 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14822 c_parser_consume_token (parser);
14823 if (c_parser_next_token_is (parser, CPP_NAME))
14825 name = c_parser_peek_token (parser)->value;
14826 c_parser_consume_token (parser);
14827 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14829 else
14830 c_parser_error (parser, "expected identifier");
14832 clauses = c_parser_omp_all_clauses (parser,
14833 OMP_CRITICAL_CLAUSE_MASK,
14834 "#pragma omp critical");
14836 else
14838 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14839 c_parser_error (parser, "expected %<(%> or end of line");
14840 c_parser_skip_to_pragma_eol (parser);
14843 stmt = c_parser_omp_structured_block (parser, if_p);
14844 return c_finish_omp_critical (loc, stmt, name, clauses);
14847 /* OpenMP 2.5:
14848 # pragma omp flush flush-vars[opt] new-line
14850 flush-vars:
14851 ( variable-list ) */
14853 static void
14854 c_parser_omp_flush (c_parser *parser)
14856 location_t loc = c_parser_peek_token (parser)->location;
14857 c_parser_consume_pragma (parser);
14858 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14859 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14860 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14861 c_parser_error (parser, "expected %<(%> or end of line");
14862 c_parser_skip_to_pragma_eol (parser);
14864 c_finish_omp_flush (loc);
14867 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14868 The real trick here is to determine the loop control variable early
14869 so that we can push a new decl if necessary to make it private.
14870 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14871 respectively. */
14873 static tree
14874 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14875 tree clauses, tree *cclauses, bool *if_p)
14877 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14878 tree declv, condv, incrv, initv, ret = NULL_TREE;
14879 tree pre_body = NULL_TREE, this_pre_body;
14880 tree ordered_cl = NULL_TREE;
14881 bool fail = false, open_brace_parsed = false;
14882 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14883 location_t for_loc;
14884 vec<tree, va_gc> *for_block = make_tree_vector ();
14886 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14887 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14888 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14889 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14890 && OMP_CLAUSE_ORDERED_EXPR (cl))
14892 ordered_cl = cl;
14893 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14896 if (ordered && ordered < collapse)
14898 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14899 "%<ordered%> clause parameter is less than %<collapse%>");
14900 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14901 = build_int_cst (NULL_TREE, collapse);
14902 ordered = collapse;
14904 if (ordered)
14906 for (tree *pc = &clauses; *pc; )
14907 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14909 error_at (OMP_CLAUSE_LOCATION (*pc),
14910 "%<linear%> clause may not be specified together "
14911 "with %<ordered%> clause with a parameter");
14912 *pc = OMP_CLAUSE_CHAIN (*pc);
14914 else
14915 pc = &OMP_CLAUSE_CHAIN (*pc);
14918 gcc_assert (collapse >= 1 && ordered >= 0);
14919 count = ordered ? ordered : collapse;
14921 declv = make_tree_vec (count);
14922 initv = make_tree_vec (count);
14923 condv = make_tree_vec (count);
14924 incrv = make_tree_vec (count);
14926 if (code != CILK_FOR
14927 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14929 c_parser_error (parser, "for statement expected");
14930 return NULL;
14932 if (code == CILK_FOR
14933 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14935 c_parser_error (parser, "_Cilk_for statement expected");
14936 return NULL;
14938 for_loc = c_parser_peek_token (parser)->location;
14939 c_parser_consume_token (parser);
14941 for (i = 0; i < count; i++)
14943 int bracecount = 0;
14945 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14946 goto pop_scopes;
14948 /* Parse the initialization declaration or expression. */
14949 if (c_parser_next_tokens_start_declaration (parser))
14951 if (i > 0)
14952 vec_safe_push (for_block, c_begin_compound_stmt (true));
14953 this_pre_body = push_stmt_list ();
14954 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14955 NULL, vNULL);
14956 if (this_pre_body)
14958 this_pre_body = pop_stmt_list (this_pre_body);
14959 if (pre_body)
14961 tree t = pre_body;
14962 pre_body = push_stmt_list ();
14963 add_stmt (t);
14964 add_stmt (this_pre_body);
14965 pre_body = pop_stmt_list (pre_body);
14967 else
14968 pre_body = this_pre_body;
14970 decl = check_for_loop_decls (for_loc, flag_isoc99);
14971 if (decl == NULL)
14972 goto error_init;
14973 if (DECL_INITIAL (decl) == error_mark_node)
14974 decl = error_mark_node;
14975 init = decl;
14977 else if (c_parser_next_token_is (parser, CPP_NAME)
14978 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
14980 struct c_expr decl_exp;
14981 struct c_expr init_exp;
14982 location_t init_loc;
14984 decl_exp = c_parser_postfix_expression (parser);
14985 decl = decl_exp.value;
14987 c_parser_require (parser, CPP_EQ, "expected %<=%>");
14989 init_loc = c_parser_peek_token (parser)->location;
14990 init_exp = c_parser_expr_no_commas (parser, NULL);
14991 init_exp = default_function_array_read_conversion (init_loc,
14992 init_exp);
14993 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
14994 NOP_EXPR, init_loc, init_exp.value,
14995 init_exp.original_type);
14996 init = c_process_expr_stmt (init_loc, init);
14998 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15000 else
15002 error_init:
15003 c_parser_error (parser,
15004 "expected iteration declaration or initialization");
15005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15006 "expected %<)%>");
15007 fail = true;
15008 goto parse_next;
15011 /* Parse the loop condition. */
15012 cond = NULL_TREE;
15013 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15015 location_t cond_loc = c_parser_peek_token (parser)->location;
15016 struct c_expr cond_expr
15017 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15019 cond = cond_expr.value;
15020 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15021 cond = c_fully_fold (cond, false, NULL);
15022 switch (cond_expr.original_code)
15024 case GT_EXPR:
15025 case GE_EXPR:
15026 case LT_EXPR:
15027 case LE_EXPR:
15028 break;
15029 case NE_EXPR:
15030 if (code == CILK_SIMD || code == CILK_FOR)
15031 break;
15032 /* FALLTHRU. */
15033 default:
15034 /* Can't be cond = error_mark_node, because we want to preserve
15035 the location until c_finish_omp_for. */
15036 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15037 break;
15039 protected_set_expr_location (cond, cond_loc);
15041 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15043 /* Parse the increment expression. */
15044 incr = NULL_TREE;
15045 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15047 location_t incr_loc = c_parser_peek_token (parser)->location;
15049 incr = c_process_expr_stmt (incr_loc,
15050 c_parser_expression (parser).value);
15052 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15054 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15055 fail = true;
15056 else
15058 TREE_VEC_ELT (declv, i) = decl;
15059 TREE_VEC_ELT (initv, i) = init;
15060 TREE_VEC_ELT (condv, i) = cond;
15061 TREE_VEC_ELT (incrv, i) = incr;
15064 parse_next:
15065 if (i == count - 1)
15066 break;
15068 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15069 in between the collapsed for loops to be still considered perfectly
15070 nested. Hopefully the final version clarifies this.
15071 For now handle (multiple) {'s and empty statements. */
15074 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15076 c_parser_consume_token (parser);
15077 break;
15079 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15081 c_parser_consume_token (parser);
15082 bracecount++;
15084 else if (bracecount
15085 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15086 c_parser_consume_token (parser);
15087 else
15089 c_parser_error (parser, "not enough perfectly nested loops");
15090 if (bracecount)
15092 open_brace_parsed = true;
15093 bracecount--;
15095 fail = true;
15096 count = 0;
15097 break;
15100 while (1);
15102 nbraces += bracecount;
15105 if (nbraces)
15106 if_p = NULL;
15108 save_break = c_break_label;
15109 if (code == CILK_SIMD)
15110 c_break_label = build_int_cst (size_type_node, 2);
15111 else
15112 c_break_label = size_one_node;
15113 save_cont = c_cont_label;
15114 c_cont_label = NULL_TREE;
15115 body = push_stmt_list ();
15117 if (open_brace_parsed)
15119 location_t here = c_parser_peek_token (parser)->location;
15120 stmt = c_begin_compound_stmt (true);
15121 c_parser_compound_statement_nostart (parser);
15122 add_stmt (c_end_compound_stmt (here, stmt, true));
15124 else
15125 add_stmt (c_parser_c99_block_statement (parser, if_p));
15126 if (c_cont_label)
15128 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15129 SET_EXPR_LOCATION (t, loc);
15130 add_stmt (t);
15133 body = pop_stmt_list (body);
15134 c_break_label = save_break;
15135 c_cont_label = save_cont;
15137 while (nbraces)
15139 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15141 c_parser_consume_token (parser);
15142 nbraces--;
15144 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15145 c_parser_consume_token (parser);
15146 else
15148 c_parser_error (parser, "collapsed loops not perfectly nested");
15149 while (nbraces)
15151 location_t here = c_parser_peek_token (parser)->location;
15152 stmt = c_begin_compound_stmt (true);
15153 add_stmt (body);
15154 c_parser_compound_statement_nostart (parser);
15155 body = c_end_compound_stmt (here, stmt, true);
15156 nbraces--;
15158 goto pop_scopes;
15162 /* Only bother calling c_finish_omp_for if we haven't already generated
15163 an error from the initialization parsing. */
15164 if (!fail)
15166 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15167 incrv, body, pre_body);
15169 /* Check for iterators appearing in lb, b or incr expressions. */
15170 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15171 stmt = NULL_TREE;
15173 if (stmt)
15175 add_stmt (stmt);
15177 if (cclauses != NULL
15178 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15180 tree *c;
15181 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15182 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15183 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15184 c = &OMP_CLAUSE_CHAIN (*c);
15185 else
15187 for (i = 0; i < count; i++)
15188 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15189 break;
15190 if (i == count)
15191 c = &OMP_CLAUSE_CHAIN (*c);
15192 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15194 error_at (loc,
15195 "iteration variable %qD should not be firstprivate",
15196 OMP_CLAUSE_DECL (*c));
15197 *c = OMP_CLAUSE_CHAIN (*c);
15199 else
15201 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15202 tree l = *c;
15203 *c = OMP_CLAUSE_CHAIN (*c);
15204 if (code == OMP_SIMD)
15206 OMP_CLAUSE_CHAIN (l)
15207 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15208 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15210 else
15212 OMP_CLAUSE_CHAIN (l) = clauses;
15213 clauses = l;
15218 OMP_FOR_CLAUSES (stmt) = clauses;
15220 ret = stmt;
15222 pop_scopes:
15223 while (!for_block->is_empty ())
15225 /* FIXME diagnostics: LOC below should be the actual location of
15226 this particular for block. We need to build a list of
15227 locations to go along with FOR_BLOCK. */
15228 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15229 add_stmt (stmt);
15231 release_tree_vector (for_block);
15232 return ret;
15235 /* Helper function for OpenMP parsing, split clauses and call
15236 finish_omp_clauses on each of the set of clauses afterwards. */
15238 static void
15239 omp_split_clauses (location_t loc, enum tree_code code,
15240 omp_clause_mask mask, tree clauses, tree *cclauses)
15242 int i;
15243 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15244 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15245 if (cclauses[i])
15246 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15249 /* OpenMP 4.0:
15250 #pragma omp simd simd-clause[optseq] new-line
15251 for-loop
15253 LOC is the location of the #pragma token.
15256 #define OMP_SIMD_CLAUSE_MASK \
15257 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15266 static tree
15267 c_parser_omp_simd (location_t loc, c_parser *parser,
15268 char *p_name, omp_clause_mask mask, tree *cclauses,
15269 bool *if_p)
15271 tree block, clauses, ret;
15273 strcat (p_name, " simd");
15274 mask |= OMP_SIMD_CLAUSE_MASK;
15276 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15277 if (cclauses)
15279 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15280 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15281 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15282 OMP_CLAUSE_ORDERED);
15283 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15285 error_at (OMP_CLAUSE_LOCATION (c),
15286 "%<ordered%> clause with parameter may not be specified "
15287 "on %qs construct", p_name);
15288 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15292 block = c_begin_compound_stmt (true);
15293 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15294 block = c_end_compound_stmt (loc, block, true);
15295 add_stmt (block);
15297 return ret;
15300 /* OpenMP 2.5:
15301 #pragma omp for for-clause[optseq] new-line
15302 for-loop
15304 OpenMP 4.0:
15305 #pragma omp for simd for-simd-clause[optseq] new-line
15306 for-loop
15308 LOC is the location of the #pragma token.
15311 #define OMP_FOR_CLAUSE_MASK \
15312 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15322 static tree
15323 c_parser_omp_for (location_t loc, c_parser *parser,
15324 char *p_name, omp_clause_mask mask, tree *cclauses,
15325 bool *if_p)
15327 tree block, clauses, ret;
15329 strcat (p_name, " for");
15330 mask |= OMP_FOR_CLAUSE_MASK;
15331 /* parallel for{, simd} disallows nowait clause, but for
15332 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15333 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15334 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15335 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15336 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15337 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15339 if (c_parser_next_token_is (parser, CPP_NAME))
15341 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15343 if (strcmp (p, "simd") == 0)
15345 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15346 if (cclauses == NULL)
15347 cclauses = cclauses_buf;
15349 c_parser_consume_token (parser);
15350 if (!flag_openmp) /* flag_openmp_simd */
15351 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15352 if_p);
15353 block = c_begin_compound_stmt (true);
15354 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15355 block = c_end_compound_stmt (loc, block, true);
15356 if (ret == NULL_TREE)
15357 return ret;
15358 ret = make_node (OMP_FOR);
15359 TREE_TYPE (ret) = void_type_node;
15360 OMP_FOR_BODY (ret) = block;
15361 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15362 SET_EXPR_LOCATION (ret, loc);
15363 add_stmt (ret);
15364 return ret;
15367 if (!flag_openmp) /* flag_openmp_simd */
15369 c_parser_skip_to_pragma_eol (parser, false);
15370 return NULL_TREE;
15373 /* Composite distribute parallel for disallows linear clause. */
15374 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15375 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15377 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15378 if (cclauses)
15380 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15381 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15384 block = c_begin_compound_stmt (true);
15385 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15386 block = c_end_compound_stmt (loc, block, true);
15387 add_stmt (block);
15389 return ret;
15392 /* OpenMP 2.5:
15393 # pragma omp master new-line
15394 structured-block
15396 LOC is the location of the #pragma token.
15399 static tree
15400 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15402 c_parser_skip_to_pragma_eol (parser);
15403 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15404 if_p));
15407 /* OpenMP 2.5:
15408 # pragma omp ordered new-line
15409 structured-block
15411 OpenMP 4.5:
15412 # pragma omp ordered ordered-clauses new-line
15413 structured-block
15415 # pragma omp ordered depend-clauses new-line */
15417 #define OMP_ORDERED_CLAUSE_MASK \
15418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15421 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15422 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15424 static bool
15425 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15426 bool *if_p)
15428 location_t loc = c_parser_peek_token (parser)->location;
15429 c_parser_consume_pragma (parser);
15431 if (context != pragma_stmt && context != pragma_compound)
15433 c_parser_error (parser, "expected declaration specifiers");
15434 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
15435 return false;
15438 if (c_parser_next_token_is (parser, CPP_NAME))
15440 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15442 if (!strcmp ("depend", p))
15444 if (context == pragma_stmt)
15446 error_at (loc,
15447 "%<#pragma omp ordered%> with %<depend> clause may "
15448 "only be used in compound statements");
15449 c_parser_skip_to_pragma_eol (parser, false);
15450 return false;
15453 tree clauses
15454 = c_parser_omp_all_clauses (parser,
15455 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15456 "#pragma omp ordered");
15457 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15458 return false;
15462 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15463 "#pragma omp ordered");
15464 c_finish_omp_ordered (loc, clauses,
15465 c_parser_omp_structured_block (parser, if_p));
15466 return true;
15469 /* OpenMP 2.5:
15471 section-scope:
15472 { section-sequence }
15474 section-sequence:
15475 section-directive[opt] structured-block
15476 section-sequence section-directive structured-block
15478 SECTIONS_LOC is the location of the #pragma omp sections. */
15480 static tree
15481 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15483 tree stmt, substmt;
15484 bool error_suppress = false;
15485 location_t loc;
15487 loc = c_parser_peek_token (parser)->location;
15488 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15490 /* Avoid skipping until the end of the block. */
15491 parser->error = false;
15492 return NULL_TREE;
15495 stmt = push_stmt_list ();
15497 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15499 substmt = c_parser_omp_structured_block (parser, NULL);
15500 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15501 SET_EXPR_LOCATION (substmt, loc);
15502 add_stmt (substmt);
15505 while (1)
15507 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15508 break;
15509 if (c_parser_next_token_is (parser, CPP_EOF))
15510 break;
15512 loc = c_parser_peek_token (parser)->location;
15513 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15515 c_parser_consume_pragma (parser);
15516 c_parser_skip_to_pragma_eol (parser);
15517 error_suppress = false;
15519 else if (!error_suppress)
15521 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15522 error_suppress = true;
15525 substmt = c_parser_omp_structured_block (parser, NULL);
15526 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15527 SET_EXPR_LOCATION (substmt, loc);
15528 add_stmt (substmt);
15530 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15531 "expected %<#pragma omp section%> or %<}%>");
15533 substmt = pop_stmt_list (stmt);
15535 stmt = make_node (OMP_SECTIONS);
15536 SET_EXPR_LOCATION (stmt, sections_loc);
15537 TREE_TYPE (stmt) = void_type_node;
15538 OMP_SECTIONS_BODY (stmt) = substmt;
15540 return add_stmt (stmt);
15543 /* OpenMP 2.5:
15544 # pragma omp sections sections-clause[optseq] newline
15545 sections-scope
15547 LOC is the location of the #pragma token.
15550 #define OMP_SECTIONS_CLAUSE_MASK \
15551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15557 static tree
15558 c_parser_omp_sections (location_t loc, c_parser *parser,
15559 char *p_name, omp_clause_mask mask, tree *cclauses)
15561 tree block, clauses, ret;
15563 strcat (p_name, " sections");
15564 mask |= OMP_SECTIONS_CLAUSE_MASK;
15565 if (cclauses)
15566 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15568 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15569 if (cclauses)
15571 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15572 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15575 block = c_begin_compound_stmt (true);
15576 ret = c_parser_omp_sections_scope (loc, parser);
15577 if (ret)
15578 OMP_SECTIONS_CLAUSES (ret) = clauses;
15579 block = c_end_compound_stmt (loc, block, true);
15580 add_stmt (block);
15582 return ret;
15585 /* OpenMP 2.5:
15586 # pragma omp parallel parallel-clause[optseq] new-line
15587 structured-block
15588 # pragma omp parallel for parallel-for-clause[optseq] new-line
15589 structured-block
15590 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15591 structured-block
15593 OpenMP 4.0:
15594 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15595 structured-block
15597 LOC is the location of the #pragma token.
15600 #define OMP_PARALLEL_CLAUSE_MASK \
15601 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15611 static tree
15612 c_parser_omp_parallel (location_t loc, c_parser *parser,
15613 char *p_name, omp_clause_mask mask, tree *cclauses,
15614 bool *if_p)
15616 tree stmt, clauses, block;
15618 strcat (p_name, " parallel");
15619 mask |= OMP_PARALLEL_CLAUSE_MASK;
15620 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15621 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15622 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15623 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15625 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15627 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15628 if (cclauses == NULL)
15629 cclauses = cclauses_buf;
15631 c_parser_consume_token (parser);
15632 if (!flag_openmp) /* flag_openmp_simd */
15633 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15634 block = c_begin_omp_parallel ();
15635 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15636 stmt
15637 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15638 block);
15639 if (ret == NULL_TREE)
15640 return ret;
15641 OMP_PARALLEL_COMBINED (stmt) = 1;
15642 return stmt;
15644 /* When combined with distribute, parallel has to be followed by for.
15645 #pragma omp target parallel is allowed though. */
15646 else if (cclauses
15647 && (mask & (OMP_CLAUSE_MASK_1
15648 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15650 error_at (loc, "expected %<for%> after %qs", p_name);
15651 c_parser_skip_to_pragma_eol (parser);
15652 return NULL_TREE;
15654 else if (!flag_openmp) /* flag_openmp_simd */
15656 c_parser_skip_to_pragma_eol (parser, false);
15657 return NULL_TREE;
15659 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15661 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15662 if (strcmp (p, "sections") == 0)
15664 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15665 if (cclauses == NULL)
15666 cclauses = cclauses_buf;
15668 c_parser_consume_token (parser);
15669 block = c_begin_omp_parallel ();
15670 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15671 stmt = c_finish_omp_parallel (loc,
15672 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15673 block);
15674 OMP_PARALLEL_COMBINED (stmt) = 1;
15675 return stmt;
15679 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15680 if (cclauses)
15682 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15683 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15686 block = c_begin_omp_parallel ();
15687 c_parser_statement (parser, if_p);
15688 stmt = c_finish_omp_parallel (loc, clauses, block);
15690 return stmt;
15693 /* OpenMP 2.5:
15694 # pragma omp single single-clause[optseq] new-line
15695 structured-block
15697 LOC is the location of the #pragma.
15700 #define OMP_SINGLE_CLAUSE_MASK \
15701 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15706 static tree
15707 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15709 tree stmt = make_node (OMP_SINGLE);
15710 SET_EXPR_LOCATION (stmt, loc);
15711 TREE_TYPE (stmt) = void_type_node;
15713 OMP_SINGLE_CLAUSES (stmt)
15714 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15715 "#pragma omp single");
15716 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15718 return add_stmt (stmt);
15721 /* OpenMP 3.0:
15722 # pragma omp task task-clause[optseq] new-line
15724 LOC is the location of the #pragma.
15727 #define OMP_TASK_CLAUSE_MASK \
15728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15739 static tree
15740 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
15742 tree clauses, block;
15744 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15745 "#pragma omp task");
15747 block = c_begin_omp_task ();
15748 c_parser_statement (parser, if_p);
15749 return c_finish_omp_task (loc, clauses, block);
15752 /* OpenMP 3.0:
15753 # pragma omp taskwait new-line
15756 static void
15757 c_parser_omp_taskwait (c_parser *parser)
15759 location_t loc = c_parser_peek_token (parser)->location;
15760 c_parser_consume_pragma (parser);
15761 c_parser_skip_to_pragma_eol (parser);
15763 c_finish_omp_taskwait (loc);
15766 /* OpenMP 3.1:
15767 # pragma omp taskyield new-line
15770 static void
15771 c_parser_omp_taskyield (c_parser *parser)
15773 location_t loc = c_parser_peek_token (parser)->location;
15774 c_parser_consume_pragma (parser);
15775 c_parser_skip_to_pragma_eol (parser);
15777 c_finish_omp_taskyield (loc);
15780 /* OpenMP 4.0:
15781 # pragma omp taskgroup new-line
15784 static tree
15785 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
15787 location_t loc = c_parser_peek_token (parser)->location;
15788 c_parser_skip_to_pragma_eol (parser);
15789 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
15790 if_p));
15793 /* OpenMP 4.0:
15794 # pragma omp cancel cancel-clause[optseq] new-line
15796 LOC is the location of the #pragma.
15799 #define OMP_CANCEL_CLAUSE_MASK \
15800 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15806 static void
15807 c_parser_omp_cancel (c_parser *parser)
15809 location_t loc = c_parser_peek_token (parser)->location;
15811 c_parser_consume_pragma (parser);
15812 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15813 "#pragma omp cancel");
15815 c_finish_omp_cancel (loc, clauses);
15818 /* OpenMP 4.0:
15819 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15821 LOC is the location of the #pragma.
15824 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15825 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15830 static void
15831 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
15833 location_t loc = c_parser_peek_token (parser)->location;
15834 tree clauses;
15835 bool point_seen = false;
15837 c_parser_consume_pragma (parser);
15838 if (c_parser_next_token_is (parser, CPP_NAME))
15840 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15841 if (strcmp (p, "point") == 0)
15843 c_parser_consume_token (parser);
15844 point_seen = true;
15847 if (!point_seen)
15849 c_parser_error (parser, "expected %<point%>");
15850 c_parser_skip_to_pragma_eol (parser);
15851 return;
15854 if (context != pragma_compound)
15856 if (context == pragma_stmt)
15857 error_at (loc, "%<#pragma omp cancellation point%> may only be used in"
15858 " compound statements");
15859 else
15860 c_parser_error (parser, "expected declaration specifiers");
15861 c_parser_skip_to_pragma_eol (parser, false);
15862 return;
15865 clauses
15866 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15867 "#pragma omp cancellation point");
15869 c_finish_omp_cancellation_point (loc, clauses);
15872 /* OpenMP 4.0:
15873 #pragma omp distribute distribute-clause[optseq] new-line
15874 for-loop */
15876 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15883 static tree
15884 c_parser_omp_distribute (location_t loc, c_parser *parser,
15885 char *p_name, omp_clause_mask mask, tree *cclauses,
15886 bool *if_p)
15888 tree clauses, block, ret;
15890 strcat (p_name, " distribute");
15891 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15893 if (c_parser_next_token_is (parser, CPP_NAME))
15895 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15896 bool simd = false;
15897 bool parallel = false;
15899 if (strcmp (p, "simd") == 0)
15900 simd = true;
15901 else
15902 parallel = strcmp (p, "parallel") == 0;
15903 if (parallel || simd)
15905 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15906 if (cclauses == NULL)
15907 cclauses = cclauses_buf;
15908 c_parser_consume_token (parser);
15909 if (!flag_openmp) /* flag_openmp_simd */
15911 if (simd)
15912 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15913 if_p);
15914 else
15915 return c_parser_omp_parallel (loc, parser, p_name, mask,
15916 cclauses, if_p);
15918 block = c_begin_compound_stmt (true);
15919 if (simd)
15920 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15921 if_p);
15922 else
15923 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
15924 if_p);
15925 block = c_end_compound_stmt (loc, block, true);
15926 if (ret == NULL)
15927 return ret;
15928 ret = make_node (OMP_DISTRIBUTE);
15929 TREE_TYPE (ret) = void_type_node;
15930 OMP_FOR_BODY (ret) = block;
15931 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15932 SET_EXPR_LOCATION (ret, loc);
15933 add_stmt (ret);
15934 return ret;
15937 if (!flag_openmp) /* flag_openmp_simd */
15939 c_parser_skip_to_pragma_eol (parser, false);
15940 return NULL_TREE;
15943 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15944 if (cclauses)
15946 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15947 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15950 block = c_begin_compound_stmt (true);
15951 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
15952 if_p);
15953 block = c_end_compound_stmt (loc, block, true);
15954 add_stmt (block);
15956 return ret;
15959 /* OpenMP 4.0:
15960 # pragma omp teams teams-clause[optseq] new-line
15961 structured-block */
15963 #define OMP_TEAMS_CLAUSE_MASK \
15964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
15972 static tree
15973 c_parser_omp_teams (location_t loc, c_parser *parser,
15974 char *p_name, omp_clause_mask mask, tree *cclauses,
15975 bool *if_p)
15977 tree clauses, block, ret;
15979 strcat (p_name, " teams");
15980 mask |= OMP_TEAMS_CLAUSE_MASK;
15982 if (c_parser_next_token_is (parser, CPP_NAME))
15984 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15985 if (strcmp (p, "distribute") == 0)
15987 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15988 if (cclauses == NULL)
15989 cclauses = cclauses_buf;
15991 c_parser_consume_token (parser);
15992 if (!flag_openmp) /* flag_openmp_simd */
15993 return c_parser_omp_distribute (loc, parser, p_name, mask,
15994 cclauses, if_p);
15995 block = c_begin_compound_stmt (true);
15996 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
15997 if_p);
15998 block = c_end_compound_stmt (loc, block, true);
15999 if (ret == NULL)
16000 return ret;
16001 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16002 ret = make_node (OMP_TEAMS);
16003 TREE_TYPE (ret) = void_type_node;
16004 OMP_TEAMS_CLAUSES (ret) = clauses;
16005 OMP_TEAMS_BODY (ret) = block;
16006 OMP_TEAMS_COMBINED (ret) = 1;
16007 return add_stmt (ret);
16010 if (!flag_openmp) /* flag_openmp_simd */
16012 c_parser_skip_to_pragma_eol (parser, false);
16013 return NULL_TREE;
16016 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16017 if (cclauses)
16019 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16020 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16023 tree stmt = make_node (OMP_TEAMS);
16024 TREE_TYPE (stmt) = void_type_node;
16025 OMP_TEAMS_CLAUSES (stmt) = clauses;
16026 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16028 return add_stmt (stmt);
16031 /* OpenMP 4.0:
16032 # pragma omp target data target-data-clause[optseq] new-line
16033 structured-block */
16035 #define OMP_TARGET_DATA_CLAUSE_MASK \
16036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16041 static tree
16042 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16044 tree clauses
16045 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16046 "#pragma omp target data");
16047 int map_seen = 0;
16048 for (tree *pc = &clauses; *pc;)
16050 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16051 switch (OMP_CLAUSE_MAP_KIND (*pc))
16053 case GOMP_MAP_TO:
16054 case GOMP_MAP_ALWAYS_TO:
16055 case GOMP_MAP_FROM:
16056 case GOMP_MAP_ALWAYS_FROM:
16057 case GOMP_MAP_TOFROM:
16058 case GOMP_MAP_ALWAYS_TOFROM:
16059 case GOMP_MAP_ALLOC:
16060 map_seen = 3;
16061 break;
16062 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16063 case GOMP_MAP_ALWAYS_POINTER:
16064 break;
16065 default:
16066 map_seen |= 1;
16067 error_at (OMP_CLAUSE_LOCATION (*pc),
16068 "%<#pragma omp target data%> with map-type other "
16069 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16070 "on %<map%> clause");
16071 *pc = OMP_CLAUSE_CHAIN (*pc);
16072 continue;
16074 pc = &OMP_CLAUSE_CHAIN (*pc);
16077 if (map_seen != 3)
16079 if (map_seen == 0)
16080 error_at (loc,
16081 "%<#pragma omp target data%> must contain at least "
16082 "one %<map%> clause");
16083 return NULL_TREE;
16086 tree stmt = make_node (OMP_TARGET_DATA);
16087 TREE_TYPE (stmt) = void_type_node;
16088 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16089 keep_next_level ();
16090 tree block = c_begin_compound_stmt (true);
16091 add_stmt (c_parser_omp_structured_block (parser, if_p));
16092 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16094 SET_EXPR_LOCATION (stmt, loc);
16095 return add_stmt (stmt);
16098 /* OpenMP 4.0:
16099 # pragma omp target update target-update-clause[optseq] new-line */
16101 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16102 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16109 static bool
16110 c_parser_omp_target_update (location_t loc, c_parser *parser,
16111 enum pragma_context context)
16113 if (context == pragma_stmt)
16115 error_at (loc,
16116 "%<#pragma omp target update%> may only be "
16117 "used in compound statements");
16118 c_parser_skip_to_pragma_eol (parser, false);
16119 return false;
16122 tree clauses
16123 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16124 "#pragma omp target update");
16125 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16126 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16128 error_at (loc,
16129 "%<#pragma omp target update%> must contain at least one "
16130 "%<from%> or %<to%> clauses");
16131 return false;
16134 tree stmt = make_node (OMP_TARGET_UPDATE);
16135 TREE_TYPE (stmt) = void_type_node;
16136 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16137 SET_EXPR_LOCATION (stmt, loc);
16138 add_stmt (stmt);
16139 return false;
16142 /* OpenMP 4.5:
16143 # pragma omp target enter data target-data-clause[optseq] new-line */
16145 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16146 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16152 static tree
16153 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16154 enum pragma_context context)
16156 bool data_seen = false;
16157 if (c_parser_next_token_is (parser, CPP_NAME))
16159 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16160 if (strcmp (p, "data") == 0)
16162 c_parser_consume_token (parser);
16163 data_seen = true;
16166 if (!data_seen)
16168 c_parser_error (parser, "expected %<data%>");
16169 c_parser_skip_to_pragma_eol (parser);
16170 return NULL_TREE;
16173 if (context == pragma_stmt)
16175 error_at (loc,
16176 "%<#pragma omp target enter data%> may only be "
16177 "used in compound statements");
16178 c_parser_skip_to_pragma_eol (parser, false);
16179 return NULL_TREE;
16182 tree clauses
16183 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16184 "#pragma omp target enter data");
16185 int map_seen = 0;
16186 for (tree *pc = &clauses; *pc;)
16188 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16189 switch (OMP_CLAUSE_MAP_KIND (*pc))
16191 case GOMP_MAP_TO:
16192 case GOMP_MAP_ALWAYS_TO:
16193 case GOMP_MAP_ALLOC:
16194 map_seen = 3;
16195 break;
16196 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16197 case GOMP_MAP_ALWAYS_POINTER:
16198 break;
16199 default:
16200 map_seen |= 1;
16201 error_at (OMP_CLAUSE_LOCATION (*pc),
16202 "%<#pragma omp target enter data%> with map-type other "
16203 "than %<to%> or %<alloc%> on %<map%> clause");
16204 *pc = OMP_CLAUSE_CHAIN (*pc);
16205 continue;
16207 pc = &OMP_CLAUSE_CHAIN (*pc);
16210 if (map_seen != 3)
16212 if (map_seen == 0)
16213 error_at (loc,
16214 "%<#pragma omp target enter data%> must contain at least "
16215 "one %<map%> clause");
16216 return NULL_TREE;
16219 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16220 TREE_TYPE (stmt) = void_type_node;
16221 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16222 SET_EXPR_LOCATION (stmt, loc);
16223 add_stmt (stmt);
16224 return stmt;
16227 /* OpenMP 4.5:
16228 # pragma omp target exit data target-data-clause[optseq] new-line */
16230 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16231 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16237 static tree
16238 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16239 enum pragma_context context)
16241 bool data_seen = false;
16242 if (c_parser_next_token_is (parser, CPP_NAME))
16244 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16245 if (strcmp (p, "data") == 0)
16247 c_parser_consume_token (parser);
16248 data_seen = true;
16251 if (!data_seen)
16253 c_parser_error (parser, "expected %<data%>");
16254 c_parser_skip_to_pragma_eol (parser);
16255 return NULL_TREE;
16258 if (context == pragma_stmt)
16260 error_at (loc,
16261 "%<#pragma omp target exit data%> may only be "
16262 "used in compound statements");
16263 c_parser_skip_to_pragma_eol (parser, false);
16264 return NULL_TREE;
16267 tree clauses
16268 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16269 "#pragma omp target exit data");
16271 int map_seen = 0;
16272 for (tree *pc = &clauses; *pc;)
16274 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16275 switch (OMP_CLAUSE_MAP_KIND (*pc))
16277 case GOMP_MAP_FROM:
16278 case GOMP_MAP_ALWAYS_FROM:
16279 case GOMP_MAP_RELEASE:
16280 case GOMP_MAP_DELETE:
16281 map_seen = 3;
16282 break;
16283 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16284 case GOMP_MAP_ALWAYS_POINTER:
16285 break;
16286 default:
16287 map_seen |= 1;
16288 error_at (OMP_CLAUSE_LOCATION (*pc),
16289 "%<#pragma omp target exit data%> with map-type other "
16290 "than %<from%>, %<release> or %<delete%> on %<map%>"
16291 " clause");
16292 *pc = OMP_CLAUSE_CHAIN (*pc);
16293 continue;
16295 pc = &OMP_CLAUSE_CHAIN (*pc);
16298 if (map_seen != 3)
16300 if (map_seen == 0)
16301 error_at (loc,
16302 "%<#pragma omp target exit data%> must contain at least one "
16303 "%<map%> clause");
16304 return NULL_TREE;
16307 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16308 TREE_TYPE (stmt) = void_type_node;
16309 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16310 SET_EXPR_LOCATION (stmt, loc);
16311 add_stmt (stmt);
16312 return stmt;
16315 /* OpenMP 4.0:
16316 # pragma omp target target-clause[optseq] new-line
16317 structured-block */
16319 #define OMP_TARGET_CLAUSE_MASK \
16320 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16330 static bool
16331 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16333 location_t loc = c_parser_peek_token (parser)->location;
16334 c_parser_consume_pragma (parser);
16335 tree *pc = NULL, stmt, block;
16337 if (context != pragma_stmt && context != pragma_compound)
16339 c_parser_error (parser, "expected declaration specifiers");
16340 c_parser_skip_to_pragma_eol (parser);
16341 return false;
16344 if (c_parser_next_token_is (parser, CPP_NAME))
16346 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16347 enum tree_code ccode = ERROR_MARK;
16349 if (strcmp (p, "teams") == 0)
16350 ccode = OMP_TEAMS;
16351 else if (strcmp (p, "parallel") == 0)
16352 ccode = OMP_PARALLEL;
16353 else if (strcmp (p, "simd") == 0)
16354 ccode = OMP_SIMD;
16355 if (ccode != ERROR_MARK)
16357 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16358 char p_name[sizeof ("#pragma omp target teams distribute "
16359 "parallel for simd")];
16361 c_parser_consume_token (parser);
16362 strcpy (p_name, "#pragma omp target");
16363 if (!flag_openmp) /* flag_openmp_simd */
16365 tree stmt;
16366 switch (ccode)
16368 case OMP_TEAMS:
16369 stmt = c_parser_omp_teams (loc, parser, p_name,
16370 OMP_TARGET_CLAUSE_MASK,
16371 cclauses, if_p);
16372 break;
16373 case OMP_PARALLEL:
16374 stmt = c_parser_omp_parallel (loc, parser, p_name,
16375 OMP_TARGET_CLAUSE_MASK,
16376 cclauses, if_p);
16377 break;
16378 case OMP_SIMD:
16379 stmt = c_parser_omp_simd (loc, parser, p_name,
16380 OMP_TARGET_CLAUSE_MASK,
16381 cclauses, if_p);
16382 break;
16383 default:
16384 gcc_unreachable ();
16386 return stmt != NULL_TREE;
16388 keep_next_level ();
16389 tree block = c_begin_compound_stmt (true), ret;
16390 switch (ccode)
16392 case OMP_TEAMS:
16393 ret = c_parser_omp_teams (loc, parser, p_name,
16394 OMP_TARGET_CLAUSE_MASK, cclauses,
16395 if_p);
16396 break;
16397 case OMP_PARALLEL:
16398 ret = c_parser_omp_parallel (loc, parser, p_name,
16399 OMP_TARGET_CLAUSE_MASK, cclauses,
16400 if_p);
16401 break;
16402 case OMP_SIMD:
16403 ret = c_parser_omp_simd (loc, parser, p_name,
16404 OMP_TARGET_CLAUSE_MASK, cclauses,
16405 if_p);
16406 break;
16407 default:
16408 gcc_unreachable ();
16410 block = c_end_compound_stmt (loc, block, true);
16411 if (ret == NULL_TREE)
16412 return false;
16413 if (ccode == OMP_TEAMS)
16415 /* For combined target teams, ensure the num_teams and
16416 thread_limit clause expressions are evaluated on the host,
16417 before entering the target construct. */
16418 tree c;
16419 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16420 c; c = OMP_CLAUSE_CHAIN (c))
16421 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16422 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16423 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16425 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16426 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16427 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16428 expr, NULL_TREE, NULL_TREE);
16429 add_stmt (expr);
16430 OMP_CLAUSE_OPERAND (c, 0) = expr;
16431 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16432 OMP_CLAUSE_FIRSTPRIVATE);
16433 OMP_CLAUSE_DECL (tc) = tmp;
16434 OMP_CLAUSE_CHAIN (tc)
16435 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16436 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16439 tree stmt = make_node (OMP_TARGET);
16440 TREE_TYPE (stmt) = void_type_node;
16441 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16442 OMP_TARGET_BODY (stmt) = block;
16443 OMP_TARGET_COMBINED (stmt) = 1;
16444 add_stmt (stmt);
16445 pc = &OMP_TARGET_CLAUSES (stmt);
16446 goto check_clauses;
16448 else if (!flag_openmp) /* flag_openmp_simd */
16450 c_parser_skip_to_pragma_eol (parser, false);
16451 return false;
16453 else if (strcmp (p, "data") == 0)
16455 c_parser_consume_token (parser);
16456 c_parser_omp_target_data (loc, parser, if_p);
16457 return true;
16459 else if (strcmp (p, "enter") == 0)
16461 c_parser_consume_token (parser);
16462 c_parser_omp_target_enter_data (loc, parser, context);
16463 return false;
16465 else if (strcmp (p, "exit") == 0)
16467 c_parser_consume_token (parser);
16468 c_parser_omp_target_exit_data (loc, parser, context);
16469 return false;
16471 else if (strcmp (p, "update") == 0)
16473 c_parser_consume_token (parser);
16474 return c_parser_omp_target_update (loc, parser, context);
16478 stmt = make_node (OMP_TARGET);
16479 TREE_TYPE (stmt) = void_type_node;
16481 OMP_TARGET_CLAUSES (stmt)
16482 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16483 "#pragma omp target");
16484 pc = &OMP_TARGET_CLAUSES (stmt);
16485 keep_next_level ();
16486 block = c_begin_compound_stmt (true);
16487 add_stmt (c_parser_omp_structured_block (parser, if_p));
16488 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16490 SET_EXPR_LOCATION (stmt, loc);
16491 add_stmt (stmt);
16493 check_clauses:
16494 while (*pc)
16496 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16497 switch (OMP_CLAUSE_MAP_KIND (*pc))
16499 case GOMP_MAP_TO:
16500 case GOMP_MAP_ALWAYS_TO:
16501 case GOMP_MAP_FROM:
16502 case GOMP_MAP_ALWAYS_FROM:
16503 case GOMP_MAP_TOFROM:
16504 case GOMP_MAP_ALWAYS_TOFROM:
16505 case GOMP_MAP_ALLOC:
16506 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16507 case GOMP_MAP_ALWAYS_POINTER:
16508 break;
16509 default:
16510 error_at (OMP_CLAUSE_LOCATION (*pc),
16511 "%<#pragma omp target%> with map-type other "
16512 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16513 "on %<map%> clause");
16514 *pc = OMP_CLAUSE_CHAIN (*pc);
16515 continue;
16517 pc = &OMP_CLAUSE_CHAIN (*pc);
16519 return true;
16522 /* OpenMP 4.0:
16523 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16525 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16526 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16533 static void
16534 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16536 auto_vec<c_token> clauses;
16537 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16539 c_token *token = c_parser_peek_token (parser);
16540 if (token->type == CPP_EOF)
16542 c_parser_skip_to_pragma_eol (parser);
16543 return;
16545 clauses.safe_push (*token);
16546 c_parser_consume_token (parser);
16548 clauses.safe_push (*c_parser_peek_token (parser));
16549 c_parser_skip_to_pragma_eol (parser);
16551 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16553 if (c_parser_peek_token (parser)->pragma_kind
16554 != PRAGMA_OMP_DECLARE
16555 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16556 || strcmp (IDENTIFIER_POINTER
16557 (c_parser_peek_2nd_token (parser)->value),
16558 "simd") != 0)
16560 c_parser_error (parser,
16561 "%<#pragma omp declare simd%> must be followed by "
16562 "function declaration or definition or another "
16563 "%<#pragma omp declare simd%>");
16564 return;
16566 c_parser_consume_pragma (parser);
16567 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16569 c_token *token = c_parser_peek_token (parser);
16570 if (token->type == CPP_EOF)
16572 c_parser_skip_to_pragma_eol (parser);
16573 return;
16575 clauses.safe_push (*token);
16576 c_parser_consume_token (parser);
16578 clauses.safe_push (*c_parser_peek_token (parser));
16579 c_parser_skip_to_pragma_eol (parser);
16582 /* Make sure nothing tries to read past the end of the tokens. */
16583 c_token eof_token;
16584 memset (&eof_token, 0, sizeof (eof_token));
16585 eof_token.type = CPP_EOF;
16586 clauses.safe_push (eof_token);
16587 clauses.safe_push (eof_token);
16589 switch (context)
16591 case pragma_external:
16592 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16593 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16595 int ext = disable_extension_diagnostics ();
16597 c_parser_consume_token (parser);
16598 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16599 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16600 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16601 NULL, clauses);
16602 restore_extension_diagnostics (ext);
16604 else
16605 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16606 NULL, clauses);
16607 break;
16608 case pragma_struct:
16609 case pragma_param:
16610 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16611 "function declaration or definition");
16612 break;
16613 case pragma_compound:
16614 case pragma_stmt:
16615 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16616 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16618 int ext = disable_extension_diagnostics ();
16620 c_parser_consume_token (parser);
16621 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16622 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16623 if (c_parser_next_tokens_start_declaration (parser))
16625 c_parser_declaration_or_fndef (parser, true, true, true, true,
16626 true, NULL, clauses);
16627 restore_extension_diagnostics (ext);
16628 break;
16630 restore_extension_diagnostics (ext);
16632 else if (c_parser_next_tokens_start_declaration (parser))
16634 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16635 NULL, clauses);
16636 break;
16638 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16639 "function declaration or definition");
16640 break;
16641 default:
16642 gcc_unreachable ();
16646 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16647 and put that into "omp declare simd" attribute. */
16649 static void
16650 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16651 vec<c_token> clauses)
16653 if (flag_cilkplus
16654 && (clauses.exists ()
16655 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16656 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16658 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16659 "used in the same function marked as a Cilk Plus SIMD-enabled "
16660 "function");
16661 vec_free (parser->cilk_simd_fn_tokens);
16662 return;
16665 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16666 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16667 has already processed the tokens. */
16668 if (clauses.exists () && clauses[0].type == CPP_EOF)
16669 return;
16670 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16672 error ("%<#pragma omp declare simd%> not immediately followed by "
16673 "a function declaration or definition");
16674 clauses[0].type = CPP_EOF;
16675 return;
16677 if (clauses.exists () && clauses[0].type != CPP_NAME)
16679 error_at (DECL_SOURCE_LOCATION (fndecl),
16680 "%<#pragma omp declare simd%> not immediately followed by "
16681 "a single function declaration or definition");
16682 clauses[0].type = CPP_EOF;
16683 return;
16686 if (parms == NULL_TREE)
16687 parms = DECL_ARGUMENTS (fndecl);
16689 unsigned int tokens_avail = parser->tokens_avail;
16690 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16691 bool is_cilkplus_cilk_simd_fn = false;
16693 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16695 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16696 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16697 is_cilkplus_cilk_simd_fn = true;
16699 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16701 error_at (DECL_SOURCE_LOCATION (fndecl),
16702 "%<__simd__%> attribute cannot be used in the same "
16703 "function marked as a Cilk Plus SIMD-enabled function");
16704 vec_free (parser->cilk_simd_fn_tokens);
16705 return;
16709 else
16711 parser->tokens = clauses.address ();
16712 parser->tokens_avail = clauses.length ();
16715 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16716 while (parser->tokens_avail > 3)
16718 c_token *token = c_parser_peek_token (parser);
16719 if (!is_cilkplus_cilk_simd_fn)
16720 gcc_assert (token->type == CPP_NAME
16721 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16722 else
16723 gcc_assert (token->type == CPP_NAME
16724 && is_cilkplus_vector_p (token->value));
16725 c_parser_consume_token (parser);
16726 parser->in_pragma = true;
16728 tree c = NULL_TREE;
16729 if (is_cilkplus_cilk_simd_fn)
16730 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16731 "SIMD-enabled functions attribute");
16732 else
16733 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16734 "#pragma omp declare simd");
16735 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16736 if (c != NULL_TREE)
16737 c = tree_cons (NULL_TREE, c, NULL_TREE);
16738 if (is_cilkplus_cilk_simd_fn)
16740 tree k = build_tree_list (get_identifier ("cilk simd function"),
16741 NULL_TREE);
16742 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16743 DECL_ATTRIBUTES (fndecl) = k;
16745 c = build_tree_list (get_identifier ("omp declare simd"), c);
16746 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16747 DECL_ATTRIBUTES (fndecl) = c;
16750 parser->tokens = &parser->tokens_buf[0];
16751 parser->tokens_avail = tokens_avail;
16752 if (clauses.exists ())
16753 clauses[0].type = CPP_PRAGMA;
16755 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16756 vec_free (parser->cilk_simd_fn_tokens);
16760 /* OpenMP 4.0:
16761 # pragma omp declare target new-line
16762 declarations and definitions
16763 # pragma omp end declare target new-line
16765 OpenMP 4.5:
16766 # pragma omp declare target ( extended-list ) new-line
16768 # pragma omp declare target declare-target-clauses[seq] new-line */
16770 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16771 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16774 static void
16775 c_parser_omp_declare_target (c_parser *parser)
16777 location_t loc = c_parser_peek_token (parser)->location;
16778 tree clauses = NULL_TREE;
16779 if (c_parser_next_token_is (parser, CPP_NAME))
16780 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16781 "#pragma omp declare target");
16782 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16784 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16785 clauses);
16786 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
16787 c_parser_skip_to_pragma_eol (parser);
16789 else
16791 c_parser_skip_to_pragma_eol (parser);
16792 current_omp_declare_target_attribute++;
16793 return;
16795 if (current_omp_declare_target_attribute)
16796 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16797 "%<#pragma omp declare target%> without clauses and "
16798 "%<#pragma omp end declare target%>");
16799 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16801 tree t = OMP_CLAUSE_DECL (c), id;
16802 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16803 tree at2 = lookup_attribute ("omp declare target link",
16804 DECL_ATTRIBUTES (t));
16805 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16807 id = get_identifier ("omp declare target link");
16808 std::swap (at1, at2);
16810 else
16811 id = get_identifier ("omp declare target");
16812 if (at2)
16814 error_at (OMP_CLAUSE_LOCATION (c),
16815 "%qD specified both in declare target %<link%> and %<to%>"
16816 " clauses", t);
16817 continue;
16819 if (!at1)
16821 symtab_node *node = symtab_node::get (t);
16822 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16823 if (node != NULL)
16825 node->offloadable = 1;
16826 if (ENABLE_OFFLOADING)
16828 g->have_offload = true;
16829 if (is_a <varpool_node *> (node))
16830 vec_safe_push (offload_vars, t);
16837 static void
16838 c_parser_omp_end_declare_target (c_parser *parser)
16840 location_t loc = c_parser_peek_token (parser)->location;
16841 c_parser_consume_pragma (parser);
16842 if (c_parser_next_token_is (parser, CPP_NAME)
16843 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16844 "declare") == 0)
16846 c_parser_consume_token (parser);
16847 if (c_parser_next_token_is (parser, CPP_NAME)
16848 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16849 "target") == 0)
16850 c_parser_consume_token (parser);
16851 else
16853 c_parser_error (parser, "expected %<target%>");
16854 c_parser_skip_to_pragma_eol (parser);
16855 return;
16858 else
16860 c_parser_error (parser, "expected %<declare%>");
16861 c_parser_skip_to_pragma_eol (parser);
16862 return;
16864 c_parser_skip_to_pragma_eol (parser);
16865 if (!current_omp_declare_target_attribute)
16866 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16867 "%<#pragma omp declare target%>");
16868 else
16869 current_omp_declare_target_attribute--;
16873 /* OpenMP 4.0
16874 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16875 initializer-clause[opt] new-line
16877 initializer-clause:
16878 initializer (omp_priv = initializer)
16879 initializer (function-name (argument-list)) */
16881 static void
16882 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16884 unsigned int tokens_avail = 0, i;
16885 vec<tree> types = vNULL;
16886 vec<c_token> clauses = vNULL;
16887 enum tree_code reduc_code = ERROR_MARK;
16888 tree reduc_id = NULL_TREE;
16889 tree type;
16890 location_t rloc = c_parser_peek_token (parser)->location;
16892 if (context == pragma_struct || context == pragma_param)
16894 error ("%<#pragma omp declare reduction%> not at file or block scope");
16895 goto fail;
16898 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16899 goto fail;
16901 switch (c_parser_peek_token (parser)->type)
16903 case CPP_PLUS:
16904 reduc_code = PLUS_EXPR;
16905 break;
16906 case CPP_MULT:
16907 reduc_code = MULT_EXPR;
16908 break;
16909 case CPP_MINUS:
16910 reduc_code = MINUS_EXPR;
16911 break;
16912 case CPP_AND:
16913 reduc_code = BIT_AND_EXPR;
16914 break;
16915 case CPP_XOR:
16916 reduc_code = BIT_XOR_EXPR;
16917 break;
16918 case CPP_OR:
16919 reduc_code = BIT_IOR_EXPR;
16920 break;
16921 case CPP_AND_AND:
16922 reduc_code = TRUTH_ANDIF_EXPR;
16923 break;
16924 case CPP_OR_OR:
16925 reduc_code = TRUTH_ORIF_EXPR;
16926 break;
16927 case CPP_NAME:
16928 const char *p;
16929 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16930 if (strcmp (p, "min") == 0)
16932 reduc_code = MIN_EXPR;
16933 break;
16935 if (strcmp (p, "max") == 0)
16937 reduc_code = MAX_EXPR;
16938 break;
16940 reduc_id = c_parser_peek_token (parser)->value;
16941 break;
16942 default:
16943 c_parser_error (parser,
16944 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16945 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16946 goto fail;
16949 tree orig_reduc_id, reduc_decl;
16950 orig_reduc_id = reduc_id;
16951 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16952 reduc_decl = c_omp_reduction_decl (reduc_id);
16953 c_parser_consume_token (parser);
16955 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16956 goto fail;
16958 while (true)
16960 location_t loc = c_parser_peek_token (parser)->location;
16961 struct c_type_name *ctype = c_parser_type_name (parser);
16962 if (ctype != NULL)
16964 type = groktypename (ctype, NULL, NULL);
16965 if (type == error_mark_node)
16967 else if ((INTEGRAL_TYPE_P (type)
16968 || TREE_CODE (type) == REAL_TYPE
16969 || TREE_CODE (type) == COMPLEX_TYPE)
16970 && orig_reduc_id == NULL_TREE)
16971 error_at (loc, "predeclared arithmetic type in "
16972 "%<#pragma omp declare reduction%>");
16973 else if (TREE_CODE (type) == FUNCTION_TYPE
16974 || TREE_CODE (type) == ARRAY_TYPE)
16975 error_at (loc, "function or array type in "
16976 "%<#pragma omp declare reduction%>");
16977 else if (TYPE_ATOMIC (type))
16978 error_at (loc, "%<_Atomic%> qualified type in "
16979 "%<#pragma omp declare reduction%>");
16980 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
16981 error_at (loc, "const, volatile or restrict qualified type in "
16982 "%<#pragma omp declare reduction%>");
16983 else
16985 tree t;
16986 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
16987 if (comptypes (TREE_PURPOSE (t), type))
16989 error_at (loc, "redeclaration of %qs "
16990 "%<#pragma omp declare reduction%> for "
16991 "type %qT",
16992 IDENTIFIER_POINTER (reduc_id)
16993 + sizeof ("omp declare reduction ") - 1,
16994 type);
16995 location_t ploc
16996 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
16997 0));
16998 error_at (ploc, "previous %<#pragma omp declare "
16999 "reduction%>");
17000 break;
17002 if (t == NULL_TREE)
17003 types.safe_push (type);
17005 if (c_parser_next_token_is (parser, CPP_COMMA))
17006 c_parser_consume_token (parser);
17007 else
17008 break;
17010 else
17011 break;
17014 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17015 || types.is_empty ())
17017 fail:
17018 clauses.release ();
17019 types.release ();
17020 while (true)
17022 c_token *token = c_parser_peek_token (parser);
17023 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17024 break;
17025 c_parser_consume_token (parser);
17027 c_parser_skip_to_pragma_eol (parser);
17028 return;
17031 if (types.length () > 1)
17033 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17035 c_token *token = c_parser_peek_token (parser);
17036 if (token->type == CPP_EOF)
17037 goto fail;
17038 clauses.safe_push (*token);
17039 c_parser_consume_token (parser);
17041 clauses.safe_push (*c_parser_peek_token (parser));
17042 c_parser_skip_to_pragma_eol (parser);
17044 /* Make sure nothing tries to read past the end of the tokens. */
17045 c_token eof_token;
17046 memset (&eof_token, 0, sizeof (eof_token));
17047 eof_token.type = CPP_EOF;
17048 clauses.safe_push (eof_token);
17049 clauses.safe_push (eof_token);
17052 int errs = errorcount;
17053 FOR_EACH_VEC_ELT (types, i, type)
17055 tokens_avail = parser->tokens_avail;
17056 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17057 if (!clauses.is_empty ())
17059 parser->tokens = clauses.address ();
17060 parser->tokens_avail = clauses.length ();
17061 parser->in_pragma = true;
17064 bool nested = current_function_decl != NULL_TREE;
17065 if (nested)
17066 c_push_function_context ();
17067 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17068 reduc_id, default_function_type);
17069 current_function_decl = fndecl;
17070 allocate_struct_function (fndecl, true);
17071 push_scope ();
17072 tree stmt = push_stmt_list ();
17073 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17074 warn about these. */
17075 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17076 get_identifier ("omp_out"), type);
17077 DECL_ARTIFICIAL (omp_out) = 1;
17078 DECL_CONTEXT (omp_out) = fndecl;
17079 pushdecl (omp_out);
17080 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17081 get_identifier ("omp_in"), type);
17082 DECL_ARTIFICIAL (omp_in) = 1;
17083 DECL_CONTEXT (omp_in) = fndecl;
17084 pushdecl (omp_in);
17085 struct c_expr combiner = c_parser_expression (parser);
17086 struct c_expr initializer;
17087 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17088 bool bad = false;
17089 initializer.value = error_mark_node;
17090 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17091 bad = true;
17092 else if (c_parser_next_token_is (parser, CPP_NAME)
17093 && strcmp (IDENTIFIER_POINTER
17094 (c_parser_peek_token (parser)->value),
17095 "initializer") == 0)
17097 c_parser_consume_token (parser);
17098 pop_scope ();
17099 push_scope ();
17100 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17101 get_identifier ("omp_priv"), type);
17102 DECL_ARTIFICIAL (omp_priv) = 1;
17103 DECL_INITIAL (omp_priv) = error_mark_node;
17104 DECL_CONTEXT (omp_priv) = fndecl;
17105 pushdecl (omp_priv);
17106 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17107 get_identifier ("omp_orig"), type);
17108 DECL_ARTIFICIAL (omp_orig) = 1;
17109 DECL_CONTEXT (omp_orig) = fndecl;
17110 pushdecl (omp_orig);
17111 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17112 bad = true;
17113 else if (!c_parser_next_token_is (parser, CPP_NAME))
17115 c_parser_error (parser, "expected %<omp_priv%> or "
17116 "function-name");
17117 bad = true;
17119 else if (strcmp (IDENTIFIER_POINTER
17120 (c_parser_peek_token (parser)->value),
17121 "omp_priv") != 0)
17123 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17124 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17126 c_parser_error (parser, "expected function-name %<(%>");
17127 bad = true;
17129 else
17130 initializer = c_parser_postfix_expression (parser);
17131 if (initializer.value
17132 && TREE_CODE (initializer.value) == CALL_EXPR)
17134 int j;
17135 tree c = initializer.value;
17136 for (j = 0; j < call_expr_nargs (c); j++)
17138 tree a = CALL_EXPR_ARG (c, j);
17139 STRIP_NOPS (a);
17140 if (TREE_CODE (a) == ADDR_EXPR
17141 && TREE_OPERAND (a, 0) == omp_priv)
17142 break;
17144 if (j == call_expr_nargs (c))
17145 error ("one of the initializer call arguments should be "
17146 "%<&omp_priv%>");
17149 else
17151 c_parser_consume_token (parser);
17152 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17153 bad = true;
17154 else
17156 tree st = push_stmt_list ();
17157 location_t loc = c_parser_peek_token (parser)->location;
17158 rich_location richloc (line_table, loc);
17159 start_init (omp_priv, NULL_TREE, 0, &richloc);
17160 struct c_expr init = c_parser_initializer (parser);
17161 finish_init ();
17162 finish_decl (omp_priv, loc, init.value,
17163 init.original_type, NULL_TREE);
17164 pop_stmt_list (st);
17167 if (!bad
17168 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17169 bad = true;
17172 if (!bad)
17174 c_parser_skip_to_pragma_eol (parser);
17176 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17177 DECL_INITIAL (reduc_decl));
17178 DECL_INITIAL (reduc_decl) = t;
17179 DECL_SOURCE_LOCATION (omp_out) = rloc;
17180 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17181 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17182 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17183 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17184 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17185 if (omp_priv)
17187 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17188 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17189 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17190 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17191 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17192 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17193 walk_tree (&DECL_INITIAL (omp_priv),
17194 c_check_omp_declare_reduction_r,
17195 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17199 pop_stmt_list (stmt);
17200 pop_scope ();
17201 if (cfun->language != NULL)
17203 ggc_free (cfun->language);
17204 cfun->language = NULL;
17206 set_cfun (NULL);
17207 current_function_decl = NULL_TREE;
17208 if (nested)
17209 c_pop_function_context ();
17211 if (!clauses.is_empty ())
17213 parser->tokens = &parser->tokens_buf[0];
17214 parser->tokens_avail = tokens_avail;
17216 if (bad)
17217 goto fail;
17218 if (errs != errorcount)
17219 break;
17222 clauses.release ();
17223 types.release ();
17227 /* OpenMP 4.0
17228 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17229 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17230 initializer-clause[opt] new-line
17231 #pragma omp declare target new-line */
17233 static void
17234 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17236 c_parser_consume_pragma (parser);
17237 if (c_parser_next_token_is (parser, CPP_NAME))
17239 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17240 if (strcmp (p, "simd") == 0)
17242 /* c_parser_consume_token (parser); done in
17243 c_parser_omp_declare_simd. */
17244 c_parser_omp_declare_simd (parser, context);
17245 return;
17247 if (strcmp (p, "reduction") == 0)
17249 c_parser_consume_token (parser);
17250 c_parser_omp_declare_reduction (parser, context);
17251 return;
17253 if (!flag_openmp) /* flag_openmp_simd */
17255 c_parser_skip_to_pragma_eol (parser, false);
17256 return;
17258 if (strcmp (p, "target") == 0)
17260 c_parser_consume_token (parser);
17261 c_parser_omp_declare_target (parser);
17262 return;
17266 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17267 "or %<target%>");
17268 c_parser_skip_to_pragma_eol (parser);
17271 /* OpenMP 4.5:
17272 #pragma omp taskloop taskloop-clause[optseq] new-line
17273 for-loop
17275 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17276 for-loop */
17278 #define OMP_TASKLOOP_CLAUSE_MASK \
17279 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17294 static tree
17295 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17296 char *p_name, omp_clause_mask mask, tree *cclauses,
17297 bool *if_p)
17299 tree clauses, block, ret;
17301 strcat (p_name, " taskloop");
17302 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17304 if (c_parser_next_token_is (parser, CPP_NAME))
17306 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17308 if (strcmp (p, "simd") == 0)
17310 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17311 if (cclauses == NULL)
17312 cclauses = cclauses_buf;
17313 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17314 c_parser_consume_token (parser);
17315 if (!flag_openmp) /* flag_openmp_simd */
17316 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17317 if_p);
17318 block = c_begin_compound_stmt (true);
17319 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17320 block = c_end_compound_stmt (loc, block, true);
17321 if (ret == NULL)
17322 return ret;
17323 ret = make_node (OMP_TASKLOOP);
17324 TREE_TYPE (ret) = void_type_node;
17325 OMP_FOR_BODY (ret) = block;
17326 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17327 SET_EXPR_LOCATION (ret, loc);
17328 add_stmt (ret);
17329 return ret;
17332 if (!flag_openmp) /* flag_openmp_simd */
17334 c_parser_skip_to_pragma_eol (parser, false);
17335 return NULL_TREE;
17338 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17339 if (cclauses)
17341 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17342 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17345 block = c_begin_compound_stmt (true);
17346 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17347 block = c_end_compound_stmt (loc, block, true);
17348 add_stmt (block);
17350 return ret;
17353 /* Main entry point to parsing most OpenMP pragmas. */
17355 static void
17356 c_parser_omp_construct (c_parser *parser, bool *if_p)
17358 enum pragma_kind p_kind;
17359 location_t loc;
17360 tree stmt;
17361 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17362 omp_clause_mask mask (0);
17364 loc = c_parser_peek_token (parser)->location;
17365 p_kind = c_parser_peek_token (parser)->pragma_kind;
17366 c_parser_consume_pragma (parser);
17368 switch (p_kind)
17370 case PRAGMA_OACC_ATOMIC:
17371 c_parser_omp_atomic (loc, parser);
17372 return;
17373 case PRAGMA_OACC_CACHE:
17374 strcpy (p_name, "#pragma acc");
17375 stmt = c_parser_oacc_cache (loc, parser);
17376 break;
17377 case PRAGMA_OACC_DATA:
17378 stmt = c_parser_oacc_data (loc, parser, if_p);
17379 break;
17380 case PRAGMA_OACC_HOST_DATA:
17381 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17382 break;
17383 case PRAGMA_OACC_KERNELS:
17384 case PRAGMA_OACC_PARALLEL:
17385 strcpy (p_name, "#pragma acc");
17386 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17387 if_p);
17388 break;
17389 case PRAGMA_OACC_LOOP:
17390 strcpy (p_name, "#pragma acc");
17391 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17392 break;
17393 case PRAGMA_OACC_WAIT:
17394 strcpy (p_name, "#pragma wait");
17395 stmt = c_parser_oacc_wait (loc, parser, p_name);
17396 break;
17397 case PRAGMA_OMP_ATOMIC:
17398 c_parser_omp_atomic (loc, parser);
17399 return;
17400 case PRAGMA_OMP_CRITICAL:
17401 stmt = c_parser_omp_critical (loc, parser, if_p);
17402 break;
17403 case PRAGMA_OMP_DISTRIBUTE:
17404 strcpy (p_name, "#pragma omp");
17405 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17406 break;
17407 case PRAGMA_OMP_FOR:
17408 strcpy (p_name, "#pragma omp");
17409 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17410 break;
17411 case PRAGMA_OMP_MASTER:
17412 stmt = c_parser_omp_master (loc, parser, if_p);
17413 break;
17414 case PRAGMA_OMP_PARALLEL:
17415 strcpy (p_name, "#pragma omp");
17416 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17417 break;
17418 case PRAGMA_OMP_SECTIONS:
17419 strcpy (p_name, "#pragma omp");
17420 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17421 break;
17422 case PRAGMA_OMP_SIMD:
17423 strcpy (p_name, "#pragma omp");
17424 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17425 break;
17426 case PRAGMA_OMP_SINGLE:
17427 stmt = c_parser_omp_single (loc, parser, if_p);
17428 break;
17429 case PRAGMA_OMP_TASK:
17430 stmt = c_parser_omp_task (loc, parser, if_p);
17431 break;
17432 case PRAGMA_OMP_TASKGROUP:
17433 stmt = c_parser_omp_taskgroup (parser, if_p);
17434 break;
17435 case PRAGMA_OMP_TASKLOOP:
17436 strcpy (p_name, "#pragma omp");
17437 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17438 break;
17439 case PRAGMA_OMP_TEAMS:
17440 strcpy (p_name, "#pragma omp");
17441 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17442 break;
17443 default:
17444 gcc_unreachable ();
17447 if (stmt)
17448 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17452 /* OpenMP 2.5:
17453 # pragma omp threadprivate (variable-list) */
17455 static void
17456 c_parser_omp_threadprivate (c_parser *parser)
17458 tree vars, t;
17459 location_t loc;
17461 c_parser_consume_pragma (parser);
17462 loc = c_parser_peek_token (parser)->location;
17463 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17465 /* Mark every variable in VARS to be assigned thread local storage. */
17466 for (t = vars; t; t = TREE_CHAIN (t))
17468 tree v = TREE_PURPOSE (t);
17470 /* FIXME diagnostics: Ideally we should keep individual
17471 locations for all the variables in the var list to make the
17472 following errors more precise. Perhaps
17473 c_parser_omp_var_list_parens() should construct a list of
17474 locations to go along with the var list. */
17476 /* If V had already been marked threadprivate, it doesn't matter
17477 whether it had been used prior to this point. */
17478 if (!VAR_P (v))
17479 error_at (loc, "%qD is not a variable", v);
17480 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17481 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17482 else if (! is_global_var (v))
17483 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17484 else if (TREE_TYPE (v) == error_mark_node)
17486 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17487 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17488 else
17490 if (! DECL_THREAD_LOCAL_P (v))
17492 set_decl_tls_model (v, decl_default_tls_model (v));
17493 /* If rtl has been already set for this var, call
17494 make_decl_rtl once again, so that encode_section_info
17495 has a chance to look at the new decl flags. */
17496 if (DECL_RTL_SET_P (v))
17497 make_decl_rtl (v);
17499 C_DECL_THREADPRIVATE_P (v) = 1;
17503 c_parser_skip_to_pragma_eol (parser);
17506 /* Cilk Plus <#pragma simd> parsing routines. */
17508 /* Helper function for c_parser_pragma. Perform some sanity checking
17509 for <#pragma simd> constructs. Returns FALSE if there was a
17510 problem. */
17512 static bool
17513 c_parser_cilk_verify_simd (c_parser *parser,
17514 enum pragma_context context)
17516 if (!flag_cilkplus)
17518 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17519 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17520 return false;
17522 if (context == pragma_external)
17524 c_parser_error (parser,"pragma simd must be inside a function");
17525 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17526 return false;
17528 return true;
17531 /* Cilk Plus:
17532 This function is shared by SIMD-enabled functions and #pragma simd.
17533 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17534 CLAUSES is unused. The main purpose of this function is to parse a
17535 vectorlength attribute or clause and check for parse errors.
17536 When IS_SIMD_FN is true then the function is merely caching the tokens
17537 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17538 cache is cleared since there is no reason to continue.
17539 Syntax:
17540 vectorlength ( constant-expression ) */
17542 static tree
17543 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17544 bool is_simd_fn)
17546 if (is_simd_fn)
17547 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17548 else
17549 /* The vectorlength clause behaves exactly like OpenMP's safelen
17550 clause. Represent it in OpenMP terms. */
17551 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17553 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17554 return clauses;
17556 location_t loc = c_parser_peek_token (parser)->location;
17557 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17558 expr = c_fully_fold (expr, false, NULL);
17560 /* If expr is an error_mark_node then the above function would have
17561 emitted an error. No reason to do it twice. */
17562 if (expr == error_mark_node)
17564 else if (!TREE_TYPE (expr)
17565 || !TREE_CONSTANT (expr)
17566 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17568 error_at (loc, "vectorlength must be an integer constant");
17569 else if (wi::exact_log2 (expr) == -1)
17570 error_at (loc, "vectorlength must be a power of 2");
17571 else
17573 if (is_simd_fn)
17575 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17576 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17577 OMP_CLAUSE_CHAIN (u) = clauses;
17578 clauses = u;
17580 else
17582 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17583 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17584 OMP_CLAUSE_CHAIN (u) = clauses;
17585 clauses = u;
17589 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17591 return clauses;
17594 /* Cilk Plus:
17595 linear ( simd-linear-variable-list )
17597 simd-linear-variable-list:
17598 simd-linear-variable
17599 simd-linear-variable-list , simd-linear-variable
17601 simd-linear-variable:
17602 id-expression
17603 id-expression : simd-linear-step
17605 simd-linear-step:
17606 conditional-expression */
17608 static tree
17609 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17611 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17612 return clauses;
17614 location_t loc = c_parser_peek_token (parser)->location;
17616 if (c_parser_next_token_is_not (parser, CPP_NAME)
17617 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17618 c_parser_error (parser, "expected identifier");
17620 while (c_parser_next_token_is (parser, CPP_NAME)
17621 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17623 tree var = lookup_name (c_parser_peek_token (parser)->value);
17625 if (var == NULL)
17627 undeclared_variable (c_parser_peek_token (parser)->location,
17628 c_parser_peek_token (parser)->value);
17629 c_parser_consume_token (parser);
17631 else if (var == error_mark_node)
17632 c_parser_consume_token (parser);
17633 else
17635 tree step = integer_one_node;
17637 /* Parse the linear step if present. */
17638 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17640 c_parser_consume_token (parser);
17641 c_parser_consume_token (parser);
17643 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17644 expr = c_fully_fold (expr, false, NULL);
17646 if (TREE_TYPE (expr)
17647 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17648 && (TREE_CONSTANT (expr)
17649 || DECL_P (expr)))
17650 step = expr;
17651 else
17652 c_parser_error (parser,
17653 "step size must be an integer constant "
17654 "expression or an integer variable");
17656 else
17657 c_parser_consume_token (parser);
17659 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17660 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17661 OMP_CLAUSE_DECL (u) = var;
17662 OMP_CLAUSE_LINEAR_STEP (u) = step;
17663 OMP_CLAUSE_CHAIN (u) = clauses;
17664 clauses = u;
17667 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17668 break;
17670 c_parser_consume_token (parser);
17673 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17675 return clauses;
17678 /* Returns the name of the next clause. If the clause is not
17679 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17680 not consumed. Otherwise, the appropriate pragma_simd_clause is
17681 returned and the token is consumed. */
17683 static pragma_omp_clause
17684 c_parser_cilk_clause_name (c_parser *parser)
17686 pragma_omp_clause result;
17687 c_token *token = c_parser_peek_token (parser);
17689 if (!token->value || token->type != CPP_NAME)
17690 return PRAGMA_CILK_CLAUSE_NONE;
17692 const char *p = IDENTIFIER_POINTER (token->value);
17694 if (!strcmp (p, "vectorlength"))
17695 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17696 else if (!strcmp (p, "linear"))
17697 result = PRAGMA_CILK_CLAUSE_LINEAR;
17698 else if (!strcmp (p, "private"))
17699 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17700 else if (!strcmp (p, "firstprivate"))
17701 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17702 else if (!strcmp (p, "lastprivate"))
17703 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17704 else if (!strcmp (p, "reduction"))
17705 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17706 else
17707 return PRAGMA_CILK_CLAUSE_NONE;
17709 c_parser_consume_token (parser);
17710 return result;
17713 /* Parse all #<pragma simd> clauses. Return the list of clauses
17714 found. */
17716 static tree
17717 c_parser_cilk_all_clauses (c_parser *parser)
17719 tree clauses = NULL;
17721 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17723 pragma_omp_clause c_kind;
17725 c_kind = c_parser_cilk_clause_name (parser);
17727 switch (c_kind)
17729 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17730 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17731 break;
17732 case PRAGMA_CILK_CLAUSE_LINEAR:
17733 clauses = c_parser_cilk_clause_linear (parser, clauses);
17734 break;
17735 case PRAGMA_CILK_CLAUSE_PRIVATE:
17736 /* Use the OpenMP counterpart. */
17737 clauses = c_parser_omp_clause_private (parser, clauses);
17738 break;
17739 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17740 /* Use the OpenMP counterpart. */
17741 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17742 break;
17743 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17744 /* Use the OpenMP counterpart. */
17745 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17746 break;
17747 case PRAGMA_CILK_CLAUSE_REDUCTION:
17748 /* Use the OpenMP counterpart. */
17749 clauses = c_parser_omp_clause_reduction (parser, clauses);
17750 break;
17751 default:
17752 c_parser_error (parser, "expected %<#pragma simd%> clause");
17753 goto saw_error;
17757 saw_error:
17758 c_parser_skip_to_pragma_eol (parser);
17759 return c_finish_omp_clauses (clauses, C_ORT_CILK);
17762 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17763 Here is the correct syntax of this pragma:
17764 #pragma cilk grainsize = <EXP>
17767 static void
17768 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
17770 extern tree convert_to_integer (tree, tree);
17772 /* consume the 'grainsize' keyword. */
17773 c_parser_consume_pragma (parser);
17775 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17777 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17778 if (g_expr.value == error_mark_node)
17780 c_parser_skip_to_pragma_eol (parser);
17781 return;
17783 tree grain = convert_to_integer (long_integer_type_node,
17784 c_fully_fold (g_expr.value, false,
17785 NULL));
17786 c_parser_skip_to_pragma_eol (parser);
17787 c_token *token = c_parser_peek_token (parser);
17788 if (token && token->type == CPP_KEYWORD
17789 && token->keyword == RID_CILK_FOR)
17791 if (grain == NULL_TREE || grain == error_mark_node)
17792 grain = integer_zero_node;
17793 c_parser_cilk_for (parser, grain, if_p);
17795 else
17796 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17797 "%<_Cilk_for%>");
17799 else
17800 c_parser_skip_to_pragma_eol (parser);
17803 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17805 static void
17806 c_parser_cilk_simd (c_parser *parser, bool *if_p)
17808 tree clauses = c_parser_cilk_all_clauses (parser);
17809 tree block = c_begin_compound_stmt (true);
17810 location_t loc = c_parser_peek_token (parser)->location;
17811 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
17812 block = c_end_compound_stmt (loc, block, true);
17813 add_stmt (block);
17816 /* Create an artificial decl with TYPE and emit initialization of it with
17817 INIT. */
17819 static tree
17820 c_get_temp_regvar (tree type, tree init)
17822 location_t loc = EXPR_LOCATION (init);
17823 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17824 DECL_ARTIFICIAL (decl) = 1;
17825 DECL_IGNORED_P (decl) = 1;
17826 pushdecl (decl);
17827 tree t = build2 (INIT_EXPR, type, decl, init);
17828 add_stmt (t);
17829 return decl;
17832 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17833 GRAIN is the grain value passed in through pragma or 0. */
17835 static void
17836 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
17838 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17839 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17840 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17841 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
17843 tree block = c_begin_compound_stmt (true);
17844 tree sb = push_stmt_list ();
17845 location_t loc = c_parser_peek_token (parser)->location;
17846 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
17847 if_p);
17848 sb = pop_stmt_list (sb);
17850 if (omp_for)
17852 tree omp_par = make_node (OMP_PARALLEL);
17853 TREE_TYPE (omp_par) = void_type_node;
17854 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17855 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17856 TREE_SIDE_EFFECTS (bind) = 1;
17857 BIND_EXPR_BODY (bind) = sb;
17858 OMP_PARALLEL_BODY (omp_par) = bind;
17859 if (OMP_FOR_PRE_BODY (omp_for))
17861 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17862 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17864 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17865 tree decl = TREE_OPERAND (init, 0);
17866 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17867 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17868 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17869 if (TREE_CODE (t) != INTEGER_CST)
17871 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17872 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17873 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17874 OMP_CLAUSE_CHAIN (c) = clauses;
17875 clauses = c;
17877 if (TREE_CODE (incr) == MODIFY_EXPR)
17879 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17880 if (TREE_CODE (t) != INTEGER_CST)
17882 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17883 = c_get_temp_regvar (TREE_TYPE (t), t);
17884 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17885 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17886 OMP_CLAUSE_CHAIN (c) = clauses;
17887 clauses = c;
17890 t = TREE_OPERAND (init, 1);
17891 if (TREE_CODE (t) != INTEGER_CST)
17893 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17894 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17895 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17896 OMP_CLAUSE_CHAIN (c) = clauses;
17897 clauses = c;
17899 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17900 OMP_CLAUSE_DECL (c) = decl;
17901 OMP_CLAUSE_CHAIN (c) = clauses;
17902 clauses = c;
17903 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17904 OMP_CLAUSE_OPERAND (c, 0)
17905 = cilk_for_number_of_iterations (omp_for);
17906 OMP_CLAUSE_CHAIN (c) = clauses;
17907 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
17908 add_stmt (omp_par);
17911 block = c_end_compound_stmt (loc, block, true);
17912 add_stmt (block);
17916 /* Parse a transaction attribute (GCC Extension).
17918 transaction-attribute:
17919 attributes
17920 [ [ any-word ] ]
17922 The transactional memory language description is written for C++,
17923 and uses the C++0x attribute syntax. For compatibility, allow the
17924 bracket style for transactions in C as well. */
17926 static tree
17927 c_parser_transaction_attributes (c_parser *parser)
17929 tree attr_name, attr = NULL;
17931 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17932 return c_parser_attributes (parser);
17934 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17935 return NULL_TREE;
17936 c_parser_consume_token (parser);
17937 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17938 goto error1;
17940 attr_name = c_parser_attribute_any_word (parser);
17941 if (attr_name)
17943 c_parser_consume_token (parser);
17944 attr = build_tree_list (attr_name, NULL_TREE);
17946 else
17947 c_parser_error (parser, "expected identifier");
17949 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17950 error1:
17951 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17952 return attr;
17955 /* Parse a __transaction_atomic or __transaction_relaxed statement
17956 (GCC Extension).
17958 transaction-statement:
17959 __transaction_atomic transaction-attribute[opt] compound-statement
17960 __transaction_relaxed compound-statement
17962 Note that the only valid attribute is: "outer".
17965 static tree
17966 c_parser_transaction (c_parser *parser, enum rid keyword)
17968 unsigned int old_in = parser->in_transaction;
17969 unsigned int this_in = 1, new_in;
17970 location_t loc = c_parser_peek_token (parser)->location;
17971 tree stmt, attrs;
17973 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17974 || keyword == RID_TRANSACTION_RELAXED)
17975 && c_parser_next_token_is_keyword (parser, keyword));
17976 c_parser_consume_token (parser);
17978 if (keyword == RID_TRANSACTION_RELAXED)
17979 this_in |= TM_STMT_ATTR_RELAXED;
17980 else
17982 attrs = c_parser_transaction_attributes (parser);
17983 if (attrs)
17984 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
17987 /* Keep track if we're in the lexical scope of an outer transaction. */
17988 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
17990 parser->in_transaction = new_in;
17991 stmt = c_parser_compound_statement (parser);
17992 parser->in_transaction = old_in;
17994 if (flag_tm)
17995 stmt = c_finish_transaction (loc, stmt, this_in);
17996 else
17997 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17998 "%<__transaction_atomic%> without transactional memory support enabled"
17999 : "%<__transaction_relaxed %> "
18000 "without transactional memory support enabled"));
18002 return stmt;
18005 /* Parse a __transaction_atomic or __transaction_relaxed expression
18006 (GCC Extension).
18008 transaction-expression:
18009 __transaction_atomic ( expression )
18010 __transaction_relaxed ( expression )
18013 static struct c_expr
18014 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18016 struct c_expr ret;
18017 unsigned int old_in = parser->in_transaction;
18018 unsigned int this_in = 1;
18019 location_t loc = c_parser_peek_token (parser)->location;
18020 tree attrs;
18022 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18023 || keyword == RID_TRANSACTION_RELAXED)
18024 && c_parser_next_token_is_keyword (parser, keyword));
18025 c_parser_consume_token (parser);
18027 if (keyword == RID_TRANSACTION_RELAXED)
18028 this_in |= TM_STMT_ATTR_RELAXED;
18029 else
18031 attrs = c_parser_transaction_attributes (parser);
18032 if (attrs)
18033 this_in |= parse_tm_stmt_attr (attrs, 0);
18036 parser->in_transaction = this_in;
18037 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18039 tree expr = c_parser_expression (parser).value;
18040 ret.original_type = TREE_TYPE (expr);
18041 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18042 if (this_in & TM_STMT_ATTR_RELAXED)
18043 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18044 SET_EXPR_LOCATION (ret.value, loc);
18045 ret.original_code = TRANSACTION_EXPR;
18046 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
18048 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18049 goto error;
18052 else
18054 error:
18055 ret.value = error_mark_node;
18056 ret.original_code = ERROR_MARK;
18057 ret.original_type = NULL;
18059 parser->in_transaction = old_in;
18061 if (!flag_tm)
18062 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18063 "%<__transaction_atomic%> without transactional memory support enabled"
18064 : "%<__transaction_relaxed %> "
18065 "without transactional memory support enabled"));
18067 set_c_expr_source_range (&ret, loc, loc);
18069 return ret;
18072 /* Parse a __transaction_cancel statement (GCC Extension).
18074 transaction-cancel-statement:
18075 __transaction_cancel transaction-attribute[opt] ;
18077 Note that the only valid attribute is "outer".
18080 static tree
18081 c_parser_transaction_cancel (c_parser *parser)
18083 location_t loc = c_parser_peek_token (parser)->location;
18084 tree attrs;
18085 bool is_outer = false;
18087 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18088 c_parser_consume_token (parser);
18090 attrs = c_parser_transaction_attributes (parser);
18091 if (attrs)
18092 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18094 if (!flag_tm)
18096 error_at (loc, "%<__transaction_cancel%> without "
18097 "transactional memory support enabled");
18098 goto ret_error;
18100 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18102 error_at (loc, "%<__transaction_cancel%> within a "
18103 "%<__transaction_relaxed%>");
18104 goto ret_error;
18106 else if (is_outer)
18108 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18109 && !is_tm_may_cancel_outer (current_function_decl))
18111 error_at (loc, "outer %<__transaction_cancel%> not "
18112 "within outer %<__transaction_atomic%>");
18113 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18114 goto ret_error;
18117 else if (parser->in_transaction == 0)
18119 error_at (loc, "%<__transaction_cancel%> not within "
18120 "%<__transaction_atomic%>");
18121 goto ret_error;
18124 return add_stmt (build_tm_abort_call (loc, is_outer));
18126 ret_error:
18127 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18130 /* Parse a single source file. */
18132 void
18133 c_parse_file (void)
18135 /* Use local storage to begin. If the first token is a pragma, parse it.
18136 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18137 which will cause garbage collection. */
18138 c_parser tparser;
18140 memset (&tparser, 0, sizeof tparser);
18141 tparser.tokens = &tparser.tokens_buf[0];
18142 the_parser = &tparser;
18144 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18145 c_parser_pragma_pch_preprocess (&tparser);
18147 the_parser = ggc_alloc<c_parser> ();
18148 *the_parser = tparser;
18149 if (tparser.tokens == &tparser.tokens_buf[0])
18150 the_parser->tokens = &the_parser->tokens_buf[0];
18152 /* Initialize EH, if we've been told to do so. */
18153 if (flag_exceptions)
18154 using_eh_for_cleanups ();
18156 c_parser_translation_unit (the_parser);
18157 the_parser = NULL;
18160 /* This function parses Cilk Plus array notation. The starting index is
18161 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
18162 return value of this function is a tree_node called VALUE_TREE of type
18163 ARRAY_NOTATION_REF. */
18165 static tree
18166 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
18167 tree array_value)
18169 c_token *token = NULL;
18170 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
18171 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
18172 tree array_type_domain = NULL_TREE;
18174 if (array_value == error_mark_node || initial_index == error_mark_node)
18176 /* No need to continue. If either of these 2 were true, then an error
18177 must be emitted already. Thus, no need to emit them twice. */
18178 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18179 return error_mark_node;
18182 array_type = TREE_TYPE (array_value);
18183 gcc_assert (array_type);
18184 if (TREE_CODE (array_type) != ARRAY_TYPE
18185 && TREE_CODE (array_type) != POINTER_TYPE)
18187 error_at (loc, "base of array section must be pointer or array type");
18188 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18189 return error_mark_node;
18191 type = TREE_TYPE (array_type);
18192 token = c_parser_peek_token (parser);
18194 if (token->type == CPP_EOF)
18196 c_parser_error (parser, "expected %<:%> or numeral");
18197 return value_tree;
18199 else if (token->type == CPP_COLON)
18201 if (!initial_index)
18203 /* If we are here, then we have a case like this A[:]. */
18204 c_parser_consume_token (parser);
18205 if (TREE_CODE (array_type) == POINTER_TYPE)
18207 error_at (loc, "start-index and length fields necessary for "
18208 "using array notations in pointers");
18209 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18210 return error_mark_node;
18212 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18214 error_at (loc, "array notations cannot be used with function "
18215 "type");
18216 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18217 return error_mark_node;
18219 array_type_domain = TYPE_DOMAIN (array_type);
18221 if (!array_type_domain)
18223 error_at (loc, "start-index and length fields necessary for "
18224 "using array notations in dimensionless arrays");
18225 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18226 return error_mark_node;
18229 start_index = TYPE_MINVAL (array_type_domain);
18230 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
18231 start_index);
18232 if (!TYPE_MAXVAL (array_type_domain)
18233 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
18235 error_at (loc, "start-index and length fields necessary for "
18236 "using array notations in variable-length arrays");
18237 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18238 return error_mark_node;
18240 end_index = TYPE_MAXVAL (array_type_domain);
18241 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18242 end_index, integer_one_node);
18243 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18244 stride = build_int_cst (integer_type_node, 1);
18245 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18247 else if (initial_index != error_mark_node)
18249 /* If we are here, then there should be 2 possibilities:
18250 1. Array [EXPR : EXPR]
18251 2. Array [EXPR : EXPR : EXPR]
18253 start_index = initial_index;
18255 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18257 error_at (loc, "array notations cannot be used with function "
18258 "type");
18259 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18260 return error_mark_node;
18262 c_parser_consume_token (parser); /* consume the ':' */
18263 struct c_expr ce = c_parser_expression (parser);
18264 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18265 end_index = ce.value;
18266 if (!end_index || end_index == error_mark_node)
18268 c_parser_skip_to_end_of_block_or_statement (parser);
18269 return error_mark_node;
18271 if (c_parser_peek_token (parser)->type == CPP_COLON)
18273 c_parser_consume_token (parser);
18274 ce = c_parser_expression (parser);
18275 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18276 stride = ce.value;
18277 if (!stride || stride == error_mark_node)
18279 c_parser_skip_to_end_of_block_or_statement (parser);
18280 return error_mark_node;
18284 else
18285 c_parser_error (parser, "expected array notation expression");
18287 else
18288 c_parser_error (parser, "expected array notation expression");
18290 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18292 value_tree = build_array_notation_ref (loc, array_value, start_index,
18293 end_index, stride, type);
18294 if (value_tree != error_mark_node)
18295 SET_EXPR_LOCATION (value_tree, loc);
18296 return value_tree;
18299 #include "gt-c-c-parser.h"