C: fixits for named initializers
[official-gcc.git] / gcc / c / c-parser.c
blobff32479685e517c9ab5c31324b8d4f3bb12bad97
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "target.h"
42 #include "function.h"
43 #include "c-tree.h"
44 #include "timevar.h"
45 #include "stringpool.h"
46 #include "cgraph.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "varasm.h"
50 #include "trans-mem.h"
51 #include "c-family/c-pragma.h"
52 #include "c-lang.h"
53 #include "c-family/c-objc.h"
54 #include "plugin.h"
55 #include "omp-low.h"
56 #include "builtins.h"
57 #include "gomp-constants.h"
58 #include "c-family/c-indentation.h"
59 #include "gimple-expr.h"
60 #include "context.h"
62 /* We need to walk over decls with incomplete struct/union/enum types
63 after parsing the whole translation unit.
64 In finish_decl(), if the decl is static, has incomplete
65 struct/union/enum type, it is appeneded to incomplete_record_decls.
66 In c_parser_translation_unit(), we iterate over incomplete_record_decls
67 and report error if any of the decls are still incomplete. */
69 vec<tree> incomplete_record_decls = vNULL;
71 void
72 set_c_expr_source_range (c_expr *expr,
73 location_t start, location_t finish)
75 expr->src_range.m_start = start;
76 expr->src_range.m_finish = finish;
77 if (expr->value)
78 set_source_range (expr->value, start, finish);
81 void
82 set_c_expr_source_range (c_expr *expr,
83 source_range src_range)
85 expr->src_range = src_range;
86 if (expr->value)
87 set_source_range (expr->value, src_range);
91 /* Initialization routine for this file. */
93 void
94 c_parse_init (void)
96 /* The only initialization required is of the reserved word
97 identifiers. */
98 unsigned int i;
99 tree id;
100 int mask = 0;
102 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
103 the c_token structure. */
104 gcc_assert (RID_MAX <= 255);
106 mask |= D_CXXONLY;
107 if (!flag_isoc99)
108 mask |= D_C99;
109 if (flag_no_asm)
111 mask |= D_ASM | D_EXT;
112 if (!flag_isoc99)
113 mask |= D_EXT89;
115 if (!c_dialect_objc ())
116 mask |= D_OBJC | D_CXX_OBJC;
118 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
119 for (i = 0; i < num_c_common_reswords; i++)
121 /* If a keyword is disabled, do not enter it into the table
122 and so create a canonical spelling that isn't a keyword. */
123 if (c_common_reswords[i].disable & mask)
125 if (warn_cxx_compat
126 && (c_common_reswords[i].disable & D_CXXWARN))
128 id = get_identifier (c_common_reswords[i].word);
129 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
130 C_IS_RESERVED_WORD (id) = 1;
132 continue;
135 id = get_identifier (c_common_reswords[i].word);
136 C_SET_RID_CODE (id, c_common_reswords[i].rid);
137 C_IS_RESERVED_WORD (id) = 1;
138 ridpointers [(int) c_common_reswords[i].rid] = id;
141 for (i = 0; i < NUM_INT_N_ENTS; i++)
143 /* We always create the symbols but they aren't always supported. */
144 char name[50];
145 sprintf (name, "__int%d", int_n_data[i].bitsize);
146 id = get_identifier (name);
147 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
148 C_IS_RESERVED_WORD (id) = 1;
152 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
153 and the C parser. Unlike the C++ lexer, the parser structure
154 stores the lexer information instead of using a separate structure.
155 Identifiers are separated into ordinary identifiers, type names,
156 keywords and some other Objective-C types of identifiers, and some
157 look-ahead is maintained.
159 ??? It might be a good idea to lex the whole file up front (as for
160 C++). It would then be possible to share more of the C and C++
161 lexer code, if desired. */
163 /* More information about the type of a CPP_NAME token. */
164 enum c_id_kind {
165 /* An ordinary identifier. */
166 C_ID_ID,
167 /* An identifier declared as a typedef name. */
168 C_ID_TYPENAME,
169 /* An identifier declared as an Objective-C class name. */
170 C_ID_CLASSNAME,
171 /* An address space identifier. */
172 C_ID_ADDRSPACE,
173 /* Not an identifier. */
174 C_ID_NONE
177 /* A single C token after string literal concatenation and conversion
178 of preprocessing tokens to tokens. */
179 struct GTY (()) c_token {
180 /* The kind of token. */
181 ENUM_BITFIELD (cpp_ttype) type : 8;
182 /* If this token is a CPP_NAME, this value indicates whether also
183 declared as some kind of type. Otherwise, it is C_ID_NONE. */
184 ENUM_BITFIELD (c_id_kind) id_kind : 8;
185 /* If this token is a keyword, this value indicates which keyword.
186 Otherwise, this value is RID_MAX. */
187 ENUM_BITFIELD (rid) keyword : 8;
188 /* If this token is a CPP_PRAGMA, this indicates the pragma that
189 was seen. Otherwise it is PRAGMA_NONE. */
190 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
191 /* The location at which this token was found. */
192 location_t location;
193 /* The value associated with this token, if any. */
194 tree value;
196 source_range get_range () const
198 return get_range_from_loc (line_table, location);
201 location_t get_finish () const
203 return get_range ().m_finish;
207 /* A parser structure recording information about the state and
208 context of parsing. Includes lexer information with up to two
209 tokens of look-ahead; more are not needed for C. */
210 struct GTY(()) c_parser {
211 /* The look-ahead tokens. */
212 c_token * GTY((skip)) tokens;
213 /* Buffer for look-ahead tokens. */
214 c_token tokens_buf[4];
215 /* How many look-ahead tokens are available (0 - 4, or
216 more if parsing from pre-lexed tokens). */
217 unsigned int tokens_avail;
218 /* True if a syntax error is being recovered from; false otherwise.
219 c_parser_error sets this flag. It should clear this flag when
220 enough tokens have been consumed to recover from the error. */
221 BOOL_BITFIELD error : 1;
222 /* True if we're processing a pragma, and shouldn't automatically
223 consume CPP_PRAGMA_EOL. */
224 BOOL_BITFIELD in_pragma : 1;
225 /* True if we're parsing the outermost block of an if statement. */
226 BOOL_BITFIELD in_if_block : 1;
227 /* True if we want to lex an untranslated string. */
228 BOOL_BITFIELD lex_untranslated_string : 1;
230 /* Objective-C specific parser/lexer information. */
232 /* True if we are in a context where the Objective-C "PQ" keywords
233 are considered keywords. */
234 BOOL_BITFIELD objc_pq_context : 1;
235 /* True if we are parsing a (potential) Objective-C foreach
236 statement. This is set to true after we parsed 'for (' and while
237 we wait for 'in' or ';' to decide if it's a standard C for loop or an
238 Objective-C foreach loop. */
239 BOOL_BITFIELD objc_could_be_foreach_context : 1;
240 /* The following flag is needed to contextualize Objective-C lexical
241 analysis. In some cases (e.g., 'int NSObject;'), it is
242 undesirable to bind an identifier to an Objective-C class, even
243 if a class with that name exists. */
244 BOOL_BITFIELD objc_need_raw_identifier : 1;
245 /* Nonzero if we're processing a __transaction statement. The value
246 is 1 | TM_STMT_ATTR_*. */
247 unsigned int in_transaction : 4;
248 /* True if we are in a context where the Objective-C "Property attribute"
249 keywords are valid. */
250 BOOL_BITFIELD objc_property_attr_context : 1;
252 /* Cilk Plus specific parser/lexer information. */
254 /* Buffer to hold all the tokens from parsing the vector attribute for the
255 SIMD-enabled functions (formerly known as elemental functions). */
256 vec <c_token, va_gc> *cilk_simd_fn_tokens;
260 /* The actual parser and external interface. ??? Does this need to be
261 garbage-collected? */
263 static GTY (()) c_parser *the_parser;
265 /* Read in and lex a single token, storing it in *TOKEN. */
267 static void
268 c_lex_one_token (c_parser *parser, c_token *token)
270 timevar_push (TV_LEX);
272 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
273 (parser->lex_untranslated_string
274 ? C_LEX_STRING_NO_TRANSLATE : 0));
275 token->id_kind = C_ID_NONE;
276 token->keyword = RID_MAX;
277 token->pragma_kind = PRAGMA_NONE;
279 switch (token->type)
281 case CPP_NAME:
283 tree decl;
285 bool objc_force_identifier = parser->objc_need_raw_identifier;
286 if (c_dialect_objc ())
287 parser->objc_need_raw_identifier = false;
289 if (C_IS_RESERVED_WORD (token->value))
291 enum rid rid_code = C_RID_CODE (token->value);
293 if (rid_code == RID_CXX_COMPAT_WARN)
295 warning_at (token->location,
296 OPT_Wc___compat,
297 "identifier %qE conflicts with C++ keyword",
298 token->value);
300 else if (rid_code >= RID_FIRST_ADDR_SPACE
301 && rid_code <= RID_LAST_ADDR_SPACE)
303 token->id_kind = C_ID_ADDRSPACE;
304 token->keyword = rid_code;
305 break;
307 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
309 /* We found an Objective-C "pq" keyword (in, out,
310 inout, bycopy, byref, oneway). They need special
311 care because the interpretation depends on the
312 context. */
313 if (parser->objc_pq_context)
315 token->type = CPP_KEYWORD;
316 token->keyword = rid_code;
317 break;
319 else if (parser->objc_could_be_foreach_context
320 && rid_code == RID_IN)
322 /* We are in Objective-C, inside a (potential)
323 foreach context (which means after having
324 parsed 'for (', but before having parsed ';'),
325 and we found 'in'. We consider it the keyword
326 which terminates the declaration at the
327 beginning of a foreach-statement. Note that
328 this means you can't use 'in' for anything else
329 in that context; in particular, in Objective-C
330 you can't use 'in' as the name of the running
331 variable in a C for loop. We could potentially
332 try to add code here to disambiguate, but it
333 seems a reasonable limitation. */
334 token->type = CPP_KEYWORD;
335 token->keyword = rid_code;
336 break;
338 /* Else, "pq" keywords outside of the "pq" context are
339 not keywords, and we fall through to the code for
340 normal tokens. */
342 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
344 /* We found an Objective-C "property attribute"
345 keyword (getter, setter, readonly, etc). These are
346 only valid in the property context. */
347 if (parser->objc_property_attr_context)
349 token->type = CPP_KEYWORD;
350 token->keyword = rid_code;
351 break;
353 /* Else they are not special keywords.
356 else if (c_dialect_objc ()
357 && (OBJC_IS_AT_KEYWORD (rid_code)
358 || OBJC_IS_CXX_KEYWORD (rid_code)))
360 /* We found one of the Objective-C "@" keywords (defs,
361 selector, synchronized, etc) or one of the
362 Objective-C "cxx" keywords (class, private,
363 protected, public, try, catch, throw) without a
364 preceding '@' sign. Do nothing and fall through to
365 the code for normal tokens (in C++ we would still
366 consider the CXX ones keywords, but not in C). */
369 else
371 token->type = CPP_KEYWORD;
372 token->keyword = rid_code;
373 break;
377 decl = lookup_name (token->value);
378 if (decl)
380 if (TREE_CODE (decl) == TYPE_DECL)
382 token->id_kind = C_ID_TYPENAME;
383 break;
386 else if (c_dialect_objc ())
388 tree objc_interface_decl = objc_is_class_name (token->value);
389 /* Objective-C class names are in the same namespace as
390 variables and typedefs, and hence are shadowed by local
391 declarations. */
392 if (objc_interface_decl
393 && (!objc_force_identifier || global_bindings_p ()))
395 token->value = objc_interface_decl;
396 token->id_kind = C_ID_CLASSNAME;
397 break;
400 token->id_kind = C_ID_ID;
402 break;
403 case CPP_AT_NAME:
404 /* This only happens in Objective-C; it must be a keyword. */
405 token->type = CPP_KEYWORD;
406 switch (C_RID_CODE (token->value))
408 /* Replace 'class' with '@class', 'private' with '@private',
409 etc. This prevents confusion with the C++ keyword
410 'class', and makes the tokens consistent with other
411 Objective-C 'AT' keywords. For example '@class' is
412 reported as RID_AT_CLASS which is consistent with
413 '@synchronized', which is reported as
414 RID_AT_SYNCHRONIZED.
416 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
417 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
418 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
419 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
420 case RID_THROW: token->keyword = RID_AT_THROW; break;
421 case RID_TRY: token->keyword = RID_AT_TRY; break;
422 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
423 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
424 default: token->keyword = C_RID_CODE (token->value);
426 break;
427 case CPP_COLON:
428 case CPP_COMMA:
429 case CPP_CLOSE_PAREN:
430 case CPP_SEMICOLON:
431 /* These tokens may affect the interpretation of any identifiers
432 following, if doing Objective-C. */
433 if (c_dialect_objc ())
434 parser->objc_need_raw_identifier = false;
435 break;
436 case CPP_PRAGMA:
437 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
438 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
439 token->value = NULL;
440 break;
441 default:
442 break;
444 timevar_pop (TV_LEX);
447 /* Return a pointer to the next token from PARSER, reading it in if
448 necessary. */
450 static inline c_token *
451 c_parser_peek_token (c_parser *parser)
453 if (parser->tokens_avail == 0)
455 c_lex_one_token (parser, &parser->tokens[0]);
456 parser->tokens_avail = 1;
458 return &parser->tokens[0];
461 /* Return true if the next token from PARSER has the indicated
462 TYPE. */
464 static inline bool
465 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
467 return c_parser_peek_token (parser)->type == type;
470 /* Return true if the next token from PARSER does not have the
471 indicated TYPE. */
473 static inline bool
474 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
476 return !c_parser_next_token_is (parser, type);
479 /* Return true if the next token from PARSER is the indicated
480 KEYWORD. */
482 static inline bool
483 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
485 return c_parser_peek_token (parser)->keyword == keyword;
488 /* Return a pointer to the next-but-one token from PARSER, reading it
489 in if necessary. The next token is already read in. */
491 static c_token *
492 c_parser_peek_2nd_token (c_parser *parser)
494 if (parser->tokens_avail >= 2)
495 return &parser->tokens[1];
496 gcc_assert (parser->tokens_avail == 1);
497 gcc_assert (parser->tokens[0].type != CPP_EOF);
498 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
499 c_lex_one_token (parser, &parser->tokens[1]);
500 parser->tokens_avail = 2;
501 return &parser->tokens[1];
504 /* Return a pointer to the Nth token from PARSER, reading it
505 in if necessary. The N-1th token is already read in. */
507 static c_token *
508 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
510 /* N is 1-based, not zero-based. */
511 gcc_assert (n > 0);
513 if (parser->tokens_avail >= n)
514 return &parser->tokens[n - 1];
515 gcc_assert (parser->tokens_avail == n - 1);
516 c_lex_one_token (parser, &parser->tokens[n - 1]);
517 parser->tokens_avail = n;
518 return &parser->tokens[n - 1];
521 /* Return true if TOKEN can start a type name,
522 false otherwise. */
523 static bool
524 c_token_starts_typename (c_token *token)
526 switch (token->type)
528 case CPP_NAME:
529 switch (token->id_kind)
531 case C_ID_ID:
532 return false;
533 case C_ID_ADDRSPACE:
534 return true;
535 case C_ID_TYPENAME:
536 return true;
537 case C_ID_CLASSNAME:
538 gcc_assert (c_dialect_objc ());
539 return true;
540 default:
541 gcc_unreachable ();
543 case CPP_KEYWORD:
544 switch (token->keyword)
546 case RID_UNSIGNED:
547 case RID_LONG:
548 case RID_SHORT:
549 case RID_SIGNED:
550 case RID_COMPLEX:
551 case RID_INT:
552 case RID_CHAR:
553 case RID_FLOAT:
554 case RID_DOUBLE:
555 case RID_VOID:
556 case RID_DFLOAT32:
557 case RID_DFLOAT64:
558 case RID_DFLOAT128:
559 case RID_BOOL:
560 case RID_ENUM:
561 case RID_STRUCT:
562 case RID_UNION:
563 case RID_TYPEOF:
564 case RID_CONST:
565 case RID_ATOMIC:
566 case RID_VOLATILE:
567 case RID_RESTRICT:
568 case RID_ATTRIBUTE:
569 case RID_FRACT:
570 case RID_ACCUM:
571 case RID_SAT:
572 case RID_AUTO_TYPE:
573 return true;
574 default:
575 if (token->keyword >= RID_FIRST_INT_N
576 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
577 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
578 return true;
579 return false;
581 case CPP_LESS:
582 if (c_dialect_objc ())
583 return true;
584 return false;
585 default:
586 return false;
590 enum c_lookahead_kind {
591 /* Always treat unknown identifiers as typenames. */
592 cla_prefer_type,
594 /* Could be parsing a nonabstract declarator. Only treat an identifier
595 as a typename if followed by another identifier or a star. */
596 cla_nonabstract_decl,
598 /* Never treat identifiers as typenames. */
599 cla_prefer_id
602 /* Return true if the next token from PARSER can start a type name,
603 false otherwise. LA specifies how to do lookahead in order to
604 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
606 static inline bool
607 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
609 c_token *token = c_parser_peek_token (parser);
610 if (c_token_starts_typename (token))
611 return true;
613 /* Try a bit harder to detect an unknown typename. */
614 if (la != cla_prefer_id
615 && token->type == CPP_NAME
616 && token->id_kind == C_ID_ID
618 /* Do not try too hard when we could have "object in array". */
619 && !parser->objc_could_be_foreach_context
621 && (la == cla_prefer_type
622 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
623 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
625 /* Only unknown identifiers. */
626 && !lookup_name (token->value))
627 return true;
629 return false;
632 /* Return true if TOKEN is a type qualifier, false otherwise. */
633 static bool
634 c_token_is_qualifier (c_token *token)
636 switch (token->type)
638 case CPP_NAME:
639 switch (token->id_kind)
641 case C_ID_ADDRSPACE:
642 return true;
643 default:
644 return false;
646 case CPP_KEYWORD:
647 switch (token->keyword)
649 case RID_CONST:
650 case RID_VOLATILE:
651 case RID_RESTRICT:
652 case RID_ATTRIBUTE:
653 case RID_ATOMIC:
654 return true;
655 default:
656 return false;
658 case CPP_LESS:
659 return false;
660 default:
661 gcc_unreachable ();
665 /* Return true if the next token from PARSER is a type qualifier,
666 false otherwise. */
667 static inline bool
668 c_parser_next_token_is_qualifier (c_parser *parser)
670 c_token *token = c_parser_peek_token (parser);
671 return c_token_is_qualifier (token);
674 /* Return true if TOKEN can start declaration specifiers, false
675 otherwise. */
676 static bool
677 c_token_starts_declspecs (c_token *token)
679 switch (token->type)
681 case CPP_NAME:
682 switch (token->id_kind)
684 case C_ID_ID:
685 return false;
686 case C_ID_ADDRSPACE:
687 return true;
688 case C_ID_TYPENAME:
689 return true;
690 case C_ID_CLASSNAME:
691 gcc_assert (c_dialect_objc ());
692 return true;
693 default:
694 gcc_unreachable ();
696 case CPP_KEYWORD:
697 switch (token->keyword)
699 case RID_STATIC:
700 case RID_EXTERN:
701 case RID_REGISTER:
702 case RID_TYPEDEF:
703 case RID_INLINE:
704 case RID_NORETURN:
705 case RID_AUTO:
706 case RID_THREAD:
707 case RID_UNSIGNED:
708 case RID_LONG:
709 case RID_SHORT:
710 case RID_SIGNED:
711 case RID_COMPLEX:
712 case RID_INT:
713 case RID_CHAR:
714 case RID_FLOAT:
715 case RID_DOUBLE:
716 case RID_VOID:
717 case RID_DFLOAT32:
718 case RID_DFLOAT64:
719 case RID_DFLOAT128:
720 case RID_BOOL:
721 case RID_ENUM:
722 case RID_STRUCT:
723 case RID_UNION:
724 case RID_TYPEOF:
725 case RID_CONST:
726 case RID_VOLATILE:
727 case RID_RESTRICT:
728 case RID_ATTRIBUTE:
729 case RID_FRACT:
730 case RID_ACCUM:
731 case RID_SAT:
732 case RID_ALIGNAS:
733 case RID_ATOMIC:
734 case RID_AUTO_TYPE:
735 return true;
736 default:
737 if (token->keyword >= RID_FIRST_INT_N
738 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
739 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
740 return true;
741 return false;
743 case CPP_LESS:
744 if (c_dialect_objc ())
745 return true;
746 return false;
747 default:
748 return false;
753 /* Return true if TOKEN can start declaration specifiers or a static
754 assertion, false otherwise. */
755 static bool
756 c_token_starts_declaration (c_token *token)
758 if (c_token_starts_declspecs (token)
759 || token->keyword == RID_STATIC_ASSERT)
760 return true;
761 else
762 return false;
765 /* Return true if the next token from PARSER can start declaration
766 specifiers, false otherwise. */
767 static inline bool
768 c_parser_next_token_starts_declspecs (c_parser *parser)
770 c_token *token = c_parser_peek_token (parser);
772 /* In Objective-C, a classname normally starts a declspecs unless it
773 is immediately followed by a dot. In that case, it is the
774 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
775 setter/getter on the class. c_token_starts_declspecs() can't
776 differentiate between the two cases because it only checks the
777 current token, so we have a special check here. */
778 if (c_dialect_objc ()
779 && token->type == CPP_NAME
780 && token->id_kind == C_ID_CLASSNAME
781 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
782 return false;
784 return c_token_starts_declspecs (token);
787 /* Return true if the next tokens from PARSER can start declaration
788 specifiers or a static assertion, false otherwise. */
789 static inline bool
790 c_parser_next_tokens_start_declaration (c_parser *parser)
792 c_token *token = c_parser_peek_token (parser);
794 /* Same as above. */
795 if (c_dialect_objc ()
796 && token->type == CPP_NAME
797 && token->id_kind == C_ID_CLASSNAME
798 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
799 return false;
801 /* Labels do not start declarations. */
802 if (token->type == CPP_NAME
803 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
804 return false;
806 if (c_token_starts_declaration (token))
807 return true;
809 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
810 return true;
812 return false;
815 /* Consume the next token from PARSER. */
817 static void
818 c_parser_consume_token (c_parser *parser)
820 gcc_assert (parser->tokens_avail >= 1);
821 gcc_assert (parser->tokens[0].type != CPP_EOF);
822 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
823 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
824 if (parser->tokens != &parser->tokens_buf[0])
825 parser->tokens++;
826 else if (parser->tokens_avail == 2)
827 parser->tokens[0] = parser->tokens[1];
828 parser->tokens_avail--;
831 /* Expect the current token to be a #pragma. Consume it and remember
832 that we've begun parsing a pragma. */
834 static void
835 c_parser_consume_pragma (c_parser *parser)
837 gcc_assert (!parser->in_pragma);
838 gcc_assert (parser->tokens_avail >= 1);
839 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
840 if (parser->tokens != &parser->tokens_buf[0])
841 parser->tokens++;
842 else if (parser->tokens_avail == 2)
843 parser->tokens[0] = parser->tokens[1];
844 parser->tokens_avail--;
845 parser->in_pragma = true;
848 /* Update the global input_location from TOKEN. */
849 static inline void
850 c_parser_set_source_position_from_token (c_token *token)
852 if (token->type != CPP_EOF)
854 input_location = token->location;
858 /* Helper function for c_parser_error.
859 Having peeked a token of kind TOK1_KIND that might signify
860 a conflict marker, peek successor tokens to determine
861 if we actually do have a conflict marker.
862 Specifically, we consider a run of 7 '<', '=' or '>' characters
863 at the start of a line as a conflict marker.
864 These come through the lexer as three pairs and a single,
865 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
866 If it returns true, *OUT_LOC is written to with the location/range
867 of the marker. */
869 static bool
870 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
871 location_t *out_loc)
873 c_token *token2 = c_parser_peek_2nd_token (parser);
874 if (token2->type != tok1_kind)
875 return false;
876 c_token *token3 = c_parser_peek_nth_token (parser, 3);
877 if (token3->type != tok1_kind)
878 return false;
879 c_token *token4 = c_parser_peek_nth_token (parser, 4);
880 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
881 return false;
883 /* It must be at the start of the line. */
884 location_t start_loc = c_parser_peek_token (parser)->location;
885 if (LOCATION_COLUMN (start_loc) != 1)
886 return false;
888 /* We have a conflict marker. Construct a location of the form:
889 <<<<<<<
890 ^~~~~~~
891 with start == caret, finishing at the end of the marker. */
892 location_t finish_loc = get_finish (token4->location);
893 *out_loc = make_location (start_loc, start_loc, finish_loc);
895 return true;
898 /* Issue a diagnostic of the form
899 FILE:LINE: MESSAGE before TOKEN
900 where TOKEN is the next token in the input stream of PARSER.
901 MESSAGE (specified by the caller) is usually of the form "expected
902 OTHER-TOKEN".
904 Do not issue a diagnostic if still recovering from an error.
906 ??? This is taken from the C++ parser, but building up messages in
907 this way is not i18n-friendly and some other approach should be
908 used. */
910 static void
911 c_parser_error (c_parser *parser, const char *gmsgid)
913 c_token *token = c_parser_peek_token (parser);
914 if (parser->error)
915 return;
916 parser->error = true;
917 if (!gmsgid)
918 return;
920 /* If this is actually a conflict marker, report it as such. */
921 if (token->type == CPP_LSHIFT
922 || token->type == CPP_RSHIFT
923 || token->type == CPP_EQ_EQ)
925 location_t loc;
926 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
928 error_at (loc, "version control conflict marker in file");
929 return;
933 /* This diagnostic makes more sense if it is tagged to the line of
934 the token we just peeked at. */
935 c_parser_set_source_position_from_token (token);
936 c_parse_error (gmsgid,
937 /* Because c_parse_error does not understand
938 CPP_KEYWORD, keywords are treated like
939 identifiers. */
940 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
941 /* ??? The C parser does not save the cpp flags of a
942 token, we need to pass 0 here and we will not get
943 the source spelling of some tokens but rather the
944 canonical spelling. */
945 token->value, /*flags=*/0);
948 /* If the next token is of the indicated TYPE, consume it. Otherwise,
949 issue the error MSGID. If MSGID is NULL then a message has already
950 been produced and no message will be produced this time. Returns
951 true if found, false otherwise. */
953 static bool
954 c_parser_require (c_parser *parser,
955 enum cpp_ttype type,
956 const char *msgid)
958 if (c_parser_next_token_is (parser, type))
960 c_parser_consume_token (parser);
961 return true;
963 else
965 c_parser_error (parser, msgid);
966 return false;
970 /* If the next token is the indicated keyword, consume it. Otherwise,
971 issue the error MSGID. Returns true if found, false otherwise. */
973 static bool
974 c_parser_require_keyword (c_parser *parser,
975 enum rid keyword,
976 const char *msgid)
978 if (c_parser_next_token_is_keyword (parser, keyword))
980 c_parser_consume_token (parser);
981 return true;
983 else
985 c_parser_error (parser, msgid);
986 return false;
990 /* Like c_parser_require, except that tokens will be skipped until the
991 desired token is found. An error message is still produced if the
992 next token is not as expected. If MSGID is NULL then a message has
993 already been produced and no message will be produced this
994 time. */
996 static void
997 c_parser_skip_until_found (c_parser *parser,
998 enum cpp_ttype type,
999 const char *msgid)
1001 unsigned nesting_depth = 0;
1003 if (c_parser_require (parser, type, msgid))
1004 return;
1006 /* Skip tokens until the desired token is found. */
1007 while (true)
1009 /* Peek at the next token. */
1010 c_token *token = c_parser_peek_token (parser);
1011 /* If we've reached the token we want, consume it and stop. */
1012 if (token->type == type && !nesting_depth)
1014 c_parser_consume_token (parser);
1015 break;
1018 /* If we've run out of tokens, stop. */
1019 if (token->type == CPP_EOF)
1020 return;
1021 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1022 return;
1023 if (token->type == CPP_OPEN_BRACE
1024 || token->type == CPP_OPEN_PAREN
1025 || token->type == CPP_OPEN_SQUARE)
1026 ++nesting_depth;
1027 else if (token->type == CPP_CLOSE_BRACE
1028 || token->type == CPP_CLOSE_PAREN
1029 || token->type == CPP_CLOSE_SQUARE)
1031 if (nesting_depth-- == 0)
1032 break;
1034 /* Consume this token. */
1035 c_parser_consume_token (parser);
1037 parser->error = false;
1040 /* Skip tokens until the end of a parameter is found, but do not
1041 consume the comma, semicolon or closing delimiter. */
1043 static void
1044 c_parser_skip_to_end_of_parameter (c_parser *parser)
1046 unsigned nesting_depth = 0;
1048 while (true)
1050 c_token *token = c_parser_peek_token (parser);
1051 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1052 && !nesting_depth)
1053 break;
1054 /* If we've run out of tokens, stop. */
1055 if (token->type == CPP_EOF)
1056 return;
1057 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1058 return;
1059 if (token->type == CPP_OPEN_BRACE
1060 || token->type == CPP_OPEN_PAREN
1061 || token->type == CPP_OPEN_SQUARE)
1062 ++nesting_depth;
1063 else if (token->type == CPP_CLOSE_BRACE
1064 || token->type == CPP_CLOSE_PAREN
1065 || token->type == CPP_CLOSE_SQUARE)
1067 if (nesting_depth-- == 0)
1068 break;
1070 /* Consume this token. */
1071 c_parser_consume_token (parser);
1073 parser->error = false;
1076 /* Expect to be at the end of the pragma directive and consume an
1077 end of line marker. */
1079 static void
1080 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1082 gcc_assert (parser->in_pragma);
1083 parser->in_pragma = false;
1085 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1086 c_parser_error (parser, "expected end of line");
1088 cpp_ttype token_type;
1091 c_token *token = c_parser_peek_token (parser);
1092 token_type = token->type;
1093 if (token_type == CPP_EOF)
1094 break;
1095 c_parser_consume_token (parser);
1097 while (token_type != CPP_PRAGMA_EOL);
1099 parser->error = false;
1102 /* Skip tokens until we have consumed an entire block, or until we
1103 have consumed a non-nested ';'. */
1105 static void
1106 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1108 unsigned nesting_depth = 0;
1109 bool save_error = parser->error;
1111 while (true)
1113 c_token *token;
1115 /* Peek at the next token. */
1116 token = c_parser_peek_token (parser);
1118 switch (token->type)
1120 case CPP_EOF:
1121 return;
1123 case CPP_PRAGMA_EOL:
1124 if (parser->in_pragma)
1125 return;
1126 break;
1128 case CPP_SEMICOLON:
1129 /* If the next token is a ';', we have reached the
1130 end of the statement. */
1131 if (!nesting_depth)
1133 /* Consume the ';'. */
1134 c_parser_consume_token (parser);
1135 goto finished;
1137 break;
1139 case CPP_CLOSE_BRACE:
1140 /* If the next token is a non-nested '}', then we have
1141 reached the end of the current block. */
1142 if (nesting_depth == 0 || --nesting_depth == 0)
1144 c_parser_consume_token (parser);
1145 goto finished;
1147 break;
1149 case CPP_OPEN_BRACE:
1150 /* If it the next token is a '{', then we are entering a new
1151 block. Consume the entire block. */
1152 ++nesting_depth;
1153 break;
1155 case CPP_PRAGMA:
1156 /* If we see a pragma, consume the whole thing at once. We
1157 have some safeguards against consuming pragmas willy-nilly.
1158 Normally, we'd expect to be here with parser->error set,
1159 which disables these safeguards. But it's possible to get
1160 here for secondary error recovery, after parser->error has
1161 been cleared. */
1162 c_parser_consume_pragma (parser);
1163 c_parser_skip_to_pragma_eol (parser);
1164 parser->error = save_error;
1165 continue;
1167 default:
1168 break;
1171 c_parser_consume_token (parser);
1174 finished:
1175 parser->error = false;
1178 /* CPP's options (initialized by c-opts.c). */
1179 extern cpp_options *cpp_opts;
1181 /* Save the warning flags which are controlled by __extension__. */
1183 static inline int
1184 disable_extension_diagnostics (void)
1186 int ret = (pedantic
1187 | (warn_pointer_arith << 1)
1188 | (warn_traditional << 2)
1189 | (flag_iso << 3)
1190 | (warn_long_long << 4)
1191 | (warn_cxx_compat << 5)
1192 | (warn_overlength_strings << 6)
1193 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1194 play tricks to properly restore it. */
1195 | ((warn_c90_c99_compat == 1) << 7)
1196 | ((warn_c90_c99_compat == -1) << 8)
1197 /* Similarly for warn_c99_c11_compat. */
1198 | ((warn_c99_c11_compat == 1) << 9)
1199 | ((warn_c99_c11_compat == -1) << 10)
1201 cpp_opts->cpp_pedantic = pedantic = 0;
1202 warn_pointer_arith = 0;
1203 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1204 flag_iso = 0;
1205 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1206 warn_cxx_compat = 0;
1207 warn_overlength_strings = 0;
1208 warn_c90_c99_compat = 0;
1209 warn_c99_c11_compat = 0;
1210 return ret;
1213 /* Restore the warning flags which are controlled by __extension__.
1214 FLAGS is the return value from disable_extension_diagnostics. */
1216 static inline void
1217 restore_extension_diagnostics (int flags)
1219 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1220 warn_pointer_arith = (flags >> 1) & 1;
1221 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1222 flag_iso = (flags >> 3) & 1;
1223 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1224 warn_cxx_compat = (flags >> 5) & 1;
1225 warn_overlength_strings = (flags >> 6) & 1;
1226 /* See above for why is this needed. */
1227 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1228 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1231 /* Possibly kinds of declarator to parse. */
1232 enum c_dtr_syn {
1233 /* A normal declarator with an identifier. */
1234 C_DTR_NORMAL,
1235 /* An abstract declarator (maybe empty). */
1236 C_DTR_ABSTRACT,
1237 /* A parameter declarator: may be either, but after a type name does
1238 not redeclare a typedef name as an identifier if it can
1239 alternatively be interpreted as a typedef name; see DR#009,
1240 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1241 following DR#249. For example, given a typedef T, "int T" and
1242 "int *T" are valid parameter declarations redeclaring T, while
1243 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1244 abstract declarators rather than involving redundant parentheses;
1245 the same applies with attributes inside the parentheses before
1246 "T". */
1247 C_DTR_PARM
1250 /* The binary operation precedence levels, where 0 is a dummy lowest level
1251 used for the bottom of the stack. */
1252 enum c_parser_prec {
1253 PREC_NONE,
1254 PREC_LOGOR,
1255 PREC_LOGAND,
1256 PREC_BITOR,
1257 PREC_BITXOR,
1258 PREC_BITAND,
1259 PREC_EQ,
1260 PREC_REL,
1261 PREC_SHIFT,
1262 PREC_ADD,
1263 PREC_MULT,
1264 NUM_PRECS
1267 static void c_parser_external_declaration (c_parser *);
1268 static void c_parser_asm_definition (c_parser *);
1269 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1270 bool, bool, tree *, vec<c_token>,
1271 tree = NULL_TREE);
1272 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1273 static void c_parser_static_assert_declaration (c_parser *);
1274 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1275 bool, bool, bool, enum c_lookahead_kind);
1276 static struct c_typespec c_parser_enum_specifier (c_parser *);
1277 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1278 static tree c_parser_struct_declaration (c_parser *);
1279 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1280 static tree c_parser_alignas_specifier (c_parser *);
1281 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1282 bool *);
1283 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1284 c_dtr_syn, bool *);
1285 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1286 bool,
1287 struct c_declarator *);
1288 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1289 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1290 tree);
1291 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1292 static tree c_parser_simple_asm_expr (c_parser *);
1293 static tree c_parser_attributes (c_parser *);
1294 static struct c_type_name *c_parser_type_name (c_parser *);
1295 static struct c_expr c_parser_initializer (c_parser *);
1296 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1297 struct obstack *);
1298 static void c_parser_initelt (c_parser *, struct obstack *);
1299 static void c_parser_initval (c_parser *, struct c_expr *,
1300 struct obstack *);
1301 static tree c_parser_compound_statement (c_parser *);
1302 static void c_parser_compound_statement_nostart (c_parser *);
1303 static void c_parser_label (c_parser *);
1304 static void c_parser_statement (c_parser *, bool *);
1305 static void c_parser_statement_after_labels (c_parser *, bool *,
1306 vec<tree> * = NULL);
1307 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1308 static void c_parser_switch_statement (c_parser *, bool *);
1309 static void c_parser_while_statement (c_parser *, bool, bool *);
1310 static void c_parser_do_statement (c_parser *, bool);
1311 static void c_parser_for_statement (c_parser *, bool, bool *);
1312 static tree c_parser_asm_statement (c_parser *);
1313 static tree c_parser_asm_operands (c_parser *);
1314 static tree c_parser_asm_goto_operands (c_parser *);
1315 static tree c_parser_asm_clobbers (c_parser *);
1316 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1317 tree = NULL_TREE);
1318 static struct c_expr c_parser_conditional_expression (c_parser *,
1319 struct c_expr *, tree);
1320 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1321 tree);
1322 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1323 static struct c_expr c_parser_unary_expression (c_parser *);
1324 static struct c_expr c_parser_sizeof_expression (c_parser *);
1325 static struct c_expr c_parser_alignof_expression (c_parser *);
1326 static struct c_expr c_parser_postfix_expression (c_parser *);
1327 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1328 struct c_type_name *,
1329 location_t);
1330 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1331 location_t loc,
1332 struct c_expr);
1333 static tree c_parser_transaction (c_parser *, enum rid);
1334 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1335 static tree c_parser_transaction_cancel (c_parser *);
1336 static struct c_expr c_parser_expression (c_parser *);
1337 static struct c_expr c_parser_expression_conv (c_parser *);
1338 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1339 vec<tree, va_gc> **, location_t *,
1340 tree *, vec<location_t> *,
1341 unsigned int * = NULL);
1342 static void c_parser_oacc_declare (c_parser *);
1343 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1344 static void c_parser_oacc_update (c_parser *);
1345 static void c_parser_omp_construct (c_parser *, bool *);
1346 static void c_parser_omp_threadprivate (c_parser *);
1347 static void c_parser_omp_barrier (c_parser *);
1348 static void c_parser_omp_flush (c_parser *);
1349 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1350 tree, tree *, bool *);
1351 static void c_parser_omp_taskwait (c_parser *);
1352 static void c_parser_omp_taskyield (c_parser *);
1353 static void c_parser_omp_cancel (c_parser *);
1354 static void c_parser_omp_cancellation_point (c_parser *);
1356 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1357 pragma_stmt, pragma_compound };
1358 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1359 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1360 static void c_parser_omp_end_declare_target (c_parser *);
1361 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1362 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1363 static void c_parser_oacc_routine (c_parser *parser, enum pragma_context);
1365 /* These Objective-C parser functions are only ever called when
1366 compiling Objective-C. */
1367 static void c_parser_objc_class_definition (c_parser *, tree);
1368 static void c_parser_objc_class_instance_variables (c_parser *);
1369 static void c_parser_objc_class_declaration (c_parser *);
1370 static void c_parser_objc_alias_declaration (c_parser *);
1371 static void c_parser_objc_protocol_definition (c_parser *, tree);
1372 static bool c_parser_objc_method_type (c_parser *);
1373 static void c_parser_objc_method_definition (c_parser *);
1374 static void c_parser_objc_methodprotolist (c_parser *);
1375 static void c_parser_objc_methodproto (c_parser *);
1376 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1377 static tree c_parser_objc_type_name (c_parser *);
1378 static tree c_parser_objc_protocol_refs (c_parser *);
1379 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1380 static void c_parser_objc_synchronized_statement (c_parser *);
1381 static tree c_parser_objc_selector (c_parser *);
1382 static tree c_parser_objc_selector_arg (c_parser *);
1383 static tree c_parser_objc_receiver (c_parser *);
1384 static tree c_parser_objc_message_args (c_parser *);
1385 static tree c_parser_objc_keywordexpr (c_parser *);
1386 static void c_parser_objc_at_property_declaration (c_parser *);
1387 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1388 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1389 static bool c_parser_objc_diagnose_bad_element_prefix
1390 (c_parser *, struct c_declspecs *);
1392 /* Cilk Plus supporting routines. */
1393 static void c_parser_cilk_simd (c_parser *, bool *);
1394 static void c_parser_cilk_for (c_parser *, tree, bool *);
1395 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1396 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1397 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1398 static void c_parser_cilk_grainsize (c_parser *, bool *);
1400 /* Parse a translation unit (C90 6.7, C99 6.9).
1402 translation-unit:
1403 external-declarations
1405 external-declarations:
1406 external-declaration
1407 external-declarations external-declaration
1409 GNU extensions:
1411 translation-unit:
1412 empty
1415 static void
1416 c_parser_translation_unit (c_parser *parser)
1418 if (c_parser_next_token_is (parser, CPP_EOF))
1420 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1421 "ISO C forbids an empty translation unit");
1423 else
1425 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1426 mark_valid_location_for_stdc_pragma (false);
1429 ggc_collect ();
1430 c_parser_external_declaration (parser);
1431 obstack_free (&parser_obstack, obstack_position);
1433 while (c_parser_next_token_is_not (parser, CPP_EOF));
1436 unsigned int i;
1437 tree decl;
1438 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1439 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1440 error ("storage size of %q+D isn%'t known", decl);
1443 /* Parse an external declaration (C90 6.7, C99 6.9).
1445 external-declaration:
1446 function-definition
1447 declaration
1449 GNU extensions:
1451 external-declaration:
1452 asm-definition
1454 __extension__ external-declaration
1456 Objective-C:
1458 external-declaration:
1459 objc-class-definition
1460 objc-class-declaration
1461 objc-alias-declaration
1462 objc-protocol-definition
1463 objc-method-definition
1464 @end
1467 static void
1468 c_parser_external_declaration (c_parser *parser)
1470 int ext;
1471 switch (c_parser_peek_token (parser)->type)
1473 case CPP_KEYWORD:
1474 switch (c_parser_peek_token (parser)->keyword)
1476 case RID_EXTENSION:
1477 ext = disable_extension_diagnostics ();
1478 c_parser_consume_token (parser);
1479 c_parser_external_declaration (parser);
1480 restore_extension_diagnostics (ext);
1481 break;
1482 case RID_ASM:
1483 c_parser_asm_definition (parser);
1484 break;
1485 case RID_AT_INTERFACE:
1486 case RID_AT_IMPLEMENTATION:
1487 gcc_assert (c_dialect_objc ());
1488 c_parser_objc_class_definition (parser, NULL_TREE);
1489 break;
1490 case RID_AT_CLASS:
1491 gcc_assert (c_dialect_objc ());
1492 c_parser_objc_class_declaration (parser);
1493 break;
1494 case RID_AT_ALIAS:
1495 gcc_assert (c_dialect_objc ());
1496 c_parser_objc_alias_declaration (parser);
1497 break;
1498 case RID_AT_PROTOCOL:
1499 gcc_assert (c_dialect_objc ());
1500 c_parser_objc_protocol_definition (parser, NULL_TREE);
1501 break;
1502 case RID_AT_PROPERTY:
1503 gcc_assert (c_dialect_objc ());
1504 c_parser_objc_at_property_declaration (parser);
1505 break;
1506 case RID_AT_SYNTHESIZE:
1507 gcc_assert (c_dialect_objc ());
1508 c_parser_objc_at_synthesize_declaration (parser);
1509 break;
1510 case RID_AT_DYNAMIC:
1511 gcc_assert (c_dialect_objc ());
1512 c_parser_objc_at_dynamic_declaration (parser);
1513 break;
1514 case RID_AT_END:
1515 gcc_assert (c_dialect_objc ());
1516 c_parser_consume_token (parser);
1517 objc_finish_implementation ();
1518 break;
1519 default:
1520 goto decl_or_fndef;
1522 break;
1523 case CPP_SEMICOLON:
1524 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1525 "ISO C does not allow extra %<;%> outside of a function");
1526 c_parser_consume_token (parser);
1527 break;
1528 case CPP_PRAGMA:
1529 mark_valid_location_for_stdc_pragma (true);
1530 c_parser_pragma (parser, pragma_external, NULL);
1531 mark_valid_location_for_stdc_pragma (false);
1532 break;
1533 case CPP_PLUS:
1534 case CPP_MINUS:
1535 if (c_dialect_objc ())
1537 c_parser_objc_method_definition (parser);
1538 break;
1540 /* Else fall through, and yield a syntax error trying to parse
1541 as a declaration or function definition. */
1542 default:
1543 decl_or_fndef:
1544 /* A declaration or a function definition (or, in Objective-C,
1545 an @interface or @protocol with prefix attributes). We can
1546 only tell which after parsing the declaration specifiers, if
1547 any, and the first declarator. */
1548 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1549 NULL, vNULL);
1550 break;
1554 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1555 static void c_finish_oacc_routine (c_parser *, tree, tree, bool, bool, bool);
1557 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1558 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1559 accepted; otherwise (old-style parameter declarations) only other
1560 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1561 assertion is accepted; otherwise (old-style parameter declarations)
1562 it is not. If NESTED is true, we are inside a function or parsing
1563 old-style parameter declarations; any functions encountered are
1564 nested functions and declaration specifiers are required; otherwise
1565 we are at top level and functions are normal functions and
1566 declaration specifiers may be optional. If EMPTY_OK is true, empty
1567 declarations are OK (subject to all other constraints); otherwise
1568 (old-style parameter declarations) they are diagnosed. If
1569 START_ATTR_OK is true, the declaration specifiers may start with
1570 attributes; otherwise they may not.
1571 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1572 declaration when parsing an Objective-C foreach statement.
1574 declaration:
1575 declaration-specifiers init-declarator-list[opt] ;
1576 static_assert-declaration
1578 function-definition:
1579 declaration-specifiers[opt] declarator declaration-list[opt]
1580 compound-statement
1582 declaration-list:
1583 declaration
1584 declaration-list declaration
1586 init-declarator-list:
1587 init-declarator
1588 init-declarator-list , init-declarator
1590 init-declarator:
1591 declarator simple-asm-expr[opt] attributes[opt]
1592 declarator simple-asm-expr[opt] attributes[opt] = initializer
1594 GNU extensions:
1596 nested-function-definition:
1597 declaration-specifiers declarator declaration-list[opt]
1598 compound-statement
1600 Objective-C:
1601 attributes objc-class-definition
1602 attributes objc-category-definition
1603 attributes objc-protocol-definition
1605 The simple-asm-expr and attributes are GNU extensions.
1607 This function does not handle __extension__; that is handled in its
1608 callers. ??? Following the old parser, __extension__ may start
1609 external declarations, declarations in functions and declarations
1610 at the start of "for" loops, but not old-style parameter
1611 declarations.
1613 C99 requires declaration specifiers in a function definition; the
1614 absence is diagnosed through the diagnosis of implicit int. In GNU
1615 C we also allow but diagnose declarations without declaration
1616 specifiers, but only at top level (elsewhere they conflict with
1617 other syntax).
1619 In Objective-C, declarations of the looping variable in a foreach
1620 statement are exceptionally terminated by 'in' (for example, 'for
1621 (NSObject *object in array) { ... }').
1623 OpenMP:
1625 declaration:
1626 threadprivate-directive */
1628 static void
1629 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1630 bool static_assert_ok, bool empty_ok,
1631 bool nested, bool start_attr_ok,
1632 tree *objc_foreach_object_declaration,
1633 vec<c_token> omp_declare_simd_clauses,
1634 tree oacc_routine_clauses)
1636 struct c_declspecs *specs;
1637 tree prefix_attrs;
1638 tree all_prefix_attrs;
1639 bool diagnosed_no_specs = false;
1640 location_t here = c_parser_peek_token (parser)->location;
1642 if (static_assert_ok
1643 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1645 c_parser_static_assert_declaration (parser);
1646 return;
1648 specs = build_null_declspecs ();
1650 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1651 if (c_parser_peek_token (parser)->type == CPP_NAME
1652 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1653 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1654 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1655 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1657 tree name = c_parser_peek_token (parser)->value;
1658 error_at (here, "unknown type name %qE", name);
1659 /* Give a hint to the user. This is not C++ with its implicit
1660 typedef. */
1661 if (tag_exists_p (RECORD_TYPE, name))
1662 inform (here, "use %<struct%> keyword to refer to the type");
1663 else if (tag_exists_p (UNION_TYPE, name))
1664 inform (here, "use %<union%> keyword to refer to the type");
1665 else if (tag_exists_p (ENUMERAL_TYPE, name))
1666 inform (here, "use %<enum%> keyword to refer to the type");
1668 /* Parse declspecs normally to get a correct pointer type, but avoid
1669 a further "fails to be a type name" error. Refuse nested functions
1670 since it is not how the user likely wants us to recover. */
1671 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1672 c_parser_peek_token (parser)->keyword = RID_VOID;
1673 c_parser_peek_token (parser)->value = error_mark_node;
1674 fndef_ok = !nested;
1677 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1678 true, true, cla_nonabstract_decl);
1679 if (parser->error)
1681 c_parser_skip_to_end_of_block_or_statement (parser);
1682 return;
1684 if (nested && !specs->declspecs_seen_p)
1686 c_parser_error (parser, "expected declaration specifiers");
1687 c_parser_skip_to_end_of_block_or_statement (parser);
1688 return;
1690 finish_declspecs (specs);
1691 bool auto_type_p = specs->typespec_word == cts_auto_type;
1692 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1694 if (auto_type_p)
1695 error_at (here, "%<__auto_type%> in empty declaration");
1696 else if (empty_ok)
1697 shadow_tag (specs);
1698 else
1700 shadow_tag_warned (specs, 1);
1701 pedwarn (here, 0, "empty declaration");
1703 c_parser_consume_token (parser);
1704 if (oacc_routine_clauses)
1705 c_finish_oacc_routine (parser, NULL_TREE,
1706 oacc_routine_clauses, false, true, false);
1707 return;
1710 /* Provide better error recovery. Note that a type name here is usually
1711 better diagnosed as a redeclaration. */
1712 if (empty_ok
1713 && specs->typespec_kind == ctsk_tagdef
1714 && c_parser_next_token_starts_declspecs (parser)
1715 && !c_parser_next_token_is (parser, CPP_NAME))
1717 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1718 parser->error = false;
1719 shadow_tag_warned (specs, 1);
1720 return;
1722 else if (c_dialect_objc () && !auto_type_p)
1724 /* Prefix attributes are an error on method decls. */
1725 switch (c_parser_peek_token (parser)->type)
1727 case CPP_PLUS:
1728 case CPP_MINUS:
1729 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1730 return;
1731 if (specs->attrs)
1733 warning_at (c_parser_peek_token (parser)->location,
1734 OPT_Wattributes,
1735 "prefix attributes are ignored for methods");
1736 specs->attrs = NULL_TREE;
1738 if (fndef_ok)
1739 c_parser_objc_method_definition (parser);
1740 else
1741 c_parser_objc_methodproto (parser);
1742 return;
1743 break;
1744 default:
1745 break;
1747 /* This is where we parse 'attributes @interface ...',
1748 'attributes @implementation ...', 'attributes @protocol ...'
1749 (where attributes could be, for example, __attribute__
1750 ((deprecated)).
1752 switch (c_parser_peek_token (parser)->keyword)
1754 case RID_AT_INTERFACE:
1756 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1757 return;
1758 c_parser_objc_class_definition (parser, specs->attrs);
1759 return;
1761 break;
1762 case RID_AT_IMPLEMENTATION:
1764 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1765 return;
1766 if (specs->attrs)
1768 warning_at (c_parser_peek_token (parser)->location,
1769 OPT_Wattributes,
1770 "prefix attributes are ignored for implementations");
1771 specs->attrs = NULL_TREE;
1773 c_parser_objc_class_definition (parser, NULL_TREE);
1774 return;
1776 break;
1777 case RID_AT_PROTOCOL:
1779 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1780 return;
1781 c_parser_objc_protocol_definition (parser, specs->attrs);
1782 return;
1784 break;
1785 case RID_AT_ALIAS:
1786 case RID_AT_CLASS:
1787 case RID_AT_END:
1788 case RID_AT_PROPERTY:
1789 if (specs->attrs)
1791 c_parser_error (parser, "unexpected attribute");
1792 specs->attrs = NULL;
1794 break;
1795 default:
1796 break;
1800 pending_xref_error ();
1801 prefix_attrs = specs->attrs;
1802 all_prefix_attrs = prefix_attrs;
1803 specs->attrs = NULL_TREE;
1804 for (bool first = true;; first = false)
1806 struct c_declarator *declarator;
1807 bool dummy = false;
1808 timevar_id_t tv;
1809 tree fnbody;
1810 /* Declaring either one or more declarators (in which case we
1811 should diagnose if there were no declaration specifiers) or a
1812 function definition (in which case the diagnostic for
1813 implicit int suffices). */
1814 declarator = c_parser_declarator (parser,
1815 specs->typespec_kind != ctsk_none,
1816 C_DTR_NORMAL, &dummy);
1817 if (declarator == NULL)
1819 if (omp_declare_simd_clauses.exists ()
1820 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1821 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1822 omp_declare_simd_clauses);
1823 if (oacc_routine_clauses)
1824 c_finish_oacc_routine (parser, NULL_TREE,
1825 oacc_routine_clauses,
1826 false, first, false);
1827 c_parser_skip_to_end_of_block_or_statement (parser);
1828 return;
1830 if (auto_type_p && declarator->kind != cdk_id)
1832 error_at (here,
1833 "%<__auto_type%> requires a plain identifier"
1834 " as declarator");
1835 c_parser_skip_to_end_of_block_or_statement (parser);
1836 return;
1838 if (c_parser_next_token_is (parser, CPP_EQ)
1839 || c_parser_next_token_is (parser, CPP_COMMA)
1840 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1841 || c_parser_next_token_is_keyword (parser, RID_ASM)
1842 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1843 || c_parser_next_token_is_keyword (parser, RID_IN))
1845 tree asm_name = NULL_TREE;
1846 tree postfix_attrs = NULL_TREE;
1847 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1849 diagnosed_no_specs = true;
1850 pedwarn (here, 0, "data definition has no type or storage class");
1852 /* Having seen a data definition, there cannot now be a
1853 function definition. */
1854 fndef_ok = false;
1855 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1856 asm_name = c_parser_simple_asm_expr (parser);
1857 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1859 postfix_attrs = c_parser_attributes (parser);
1860 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1862 /* This means there is an attribute specifier after
1863 the declarator in a function definition. Provide
1864 some more information for the user. */
1865 error_at (here, "attributes should be specified before the "
1866 "declarator in a function definition");
1867 c_parser_skip_to_end_of_block_or_statement (parser);
1868 return;
1871 if (c_parser_next_token_is (parser, CPP_EQ))
1873 tree d;
1874 struct c_expr init;
1875 location_t init_loc;
1876 c_parser_consume_token (parser);
1877 if (auto_type_p)
1879 start_init (NULL_TREE, asm_name, global_bindings_p ());
1880 init_loc = c_parser_peek_token (parser)->location;
1881 init = c_parser_expr_no_commas (parser, NULL);
1882 if (TREE_CODE (init.value) == COMPONENT_REF
1883 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1884 error_at (here,
1885 "%<__auto_type%> used with a bit-field"
1886 " initializer");
1887 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1888 tree init_type = TREE_TYPE (init.value);
1889 /* As with typeof, remove all qualifiers from atomic types. */
1890 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1891 init_type
1892 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1893 bool vm_type = variably_modified_type_p (init_type,
1894 NULL_TREE);
1895 if (vm_type)
1896 init.value = c_save_expr (init.value);
1897 finish_init ();
1898 specs->typespec_kind = ctsk_typeof;
1899 specs->locations[cdw_typedef] = init_loc;
1900 specs->typedef_p = true;
1901 specs->type = init_type;
1902 if (vm_type)
1904 bool maybe_const = true;
1905 tree type_expr = c_fully_fold (init.value, false,
1906 &maybe_const);
1907 specs->expr_const_operands &= maybe_const;
1908 if (specs->expr)
1909 specs->expr = build2 (COMPOUND_EXPR,
1910 TREE_TYPE (type_expr),
1911 specs->expr, type_expr);
1912 else
1913 specs->expr = type_expr;
1915 d = start_decl (declarator, specs, true,
1916 chainon (postfix_attrs, all_prefix_attrs));
1917 if (!d)
1918 d = error_mark_node;
1919 if (omp_declare_simd_clauses.exists ()
1920 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1921 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1922 omp_declare_simd_clauses);
1924 else
1926 /* The declaration of the variable is in effect while
1927 its initializer is parsed. */
1928 d = start_decl (declarator, specs, true,
1929 chainon (postfix_attrs, all_prefix_attrs));
1930 if (!d)
1931 d = error_mark_node;
1932 if (omp_declare_simd_clauses.exists ()
1933 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1934 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1935 omp_declare_simd_clauses);
1936 start_init (d, asm_name, global_bindings_p ());
1937 init_loc = c_parser_peek_token (parser)->location;
1938 init = c_parser_initializer (parser);
1939 finish_init ();
1941 if (oacc_routine_clauses)
1942 c_finish_oacc_routine (parser, d, oacc_routine_clauses,
1943 false, first, false);
1944 if (d != error_mark_node)
1946 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1947 finish_decl (d, init_loc, init.value,
1948 init.original_type, asm_name);
1951 else
1953 if (auto_type_p)
1955 error_at (here,
1956 "%<__auto_type%> requires an initialized "
1957 "data declaration");
1958 c_parser_skip_to_end_of_block_or_statement (parser);
1959 return;
1961 tree d = start_decl (declarator, specs, false,
1962 chainon (postfix_attrs,
1963 all_prefix_attrs));
1964 if (omp_declare_simd_clauses.exists ()
1965 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1967 tree parms = NULL_TREE;
1968 if (d && TREE_CODE (d) == FUNCTION_DECL)
1970 struct c_declarator *ce = declarator;
1971 while (ce != NULL)
1972 if (ce->kind == cdk_function)
1974 parms = ce->u.arg_info->parms;
1975 break;
1977 else
1978 ce = ce->declarator;
1980 if (parms)
1981 temp_store_parm_decls (d, parms);
1982 c_finish_omp_declare_simd (parser, d, parms,
1983 omp_declare_simd_clauses);
1984 if (parms)
1985 temp_pop_parm_decls ();
1987 if (oacc_routine_clauses)
1988 c_finish_oacc_routine (parser, d, oacc_routine_clauses,
1989 false, first, false);
1990 if (d)
1991 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1992 NULL_TREE, asm_name);
1994 if (c_parser_next_token_is_keyword (parser, RID_IN))
1996 if (d)
1997 *objc_foreach_object_declaration = d;
1998 else
1999 *objc_foreach_object_declaration = error_mark_node;
2002 if (c_parser_next_token_is (parser, CPP_COMMA))
2004 if (auto_type_p)
2006 error_at (here,
2007 "%<__auto_type%> may only be used with"
2008 " a single declarator");
2009 c_parser_skip_to_end_of_block_or_statement (parser);
2010 return;
2012 c_parser_consume_token (parser);
2013 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2014 all_prefix_attrs = chainon (c_parser_attributes (parser),
2015 prefix_attrs);
2016 else
2017 all_prefix_attrs = prefix_attrs;
2018 continue;
2020 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2022 c_parser_consume_token (parser);
2023 return;
2025 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2027 /* This can only happen in Objective-C: we found the
2028 'in' that terminates the declaration inside an
2029 Objective-C foreach statement. Do not consume the
2030 token, so that the caller can use it to determine
2031 that this indeed is a foreach context. */
2032 return;
2034 else
2036 c_parser_error (parser, "expected %<,%> or %<;%>");
2037 c_parser_skip_to_end_of_block_or_statement (parser);
2038 return;
2041 else if (auto_type_p)
2043 error_at (here,
2044 "%<__auto_type%> requires an initialized data declaration");
2045 c_parser_skip_to_end_of_block_or_statement (parser);
2046 return;
2048 else if (!fndef_ok)
2050 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2051 "%<asm%> or %<__attribute__%>");
2052 c_parser_skip_to_end_of_block_or_statement (parser);
2053 return;
2055 /* Function definition (nested or otherwise). */
2056 if (nested)
2058 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2059 c_push_function_context ();
2061 if (!start_function (specs, declarator, all_prefix_attrs))
2063 /* This can appear in many cases looking nothing like a
2064 function definition, so we don't give a more specific
2065 error suggesting there was one. */
2066 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2067 "or %<__attribute__%>");
2068 if (nested)
2069 c_pop_function_context ();
2070 break;
2073 if (DECL_DECLARED_INLINE_P (current_function_decl))
2074 tv = TV_PARSE_INLINE;
2075 else
2076 tv = TV_PARSE_FUNC;
2077 timevar_push (tv);
2079 /* Parse old-style parameter declarations. ??? Attributes are
2080 not allowed to start declaration specifiers here because of a
2081 syntax conflict between a function declaration with attribute
2082 suffix and a function definition with an attribute prefix on
2083 first old-style parameter declaration. Following the old
2084 parser, they are not accepted on subsequent old-style
2085 parameter declarations either. However, there is no
2086 ambiguity after the first declaration, nor indeed on the
2087 first as long as we don't allow postfix attributes after a
2088 declarator with a nonempty identifier list in a definition;
2089 and postfix attributes have never been accepted here in
2090 function definitions either. */
2091 while (c_parser_next_token_is_not (parser, CPP_EOF)
2092 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2093 c_parser_declaration_or_fndef (parser, false, false, false,
2094 true, false, NULL, vNULL);
2095 store_parm_decls ();
2096 if (omp_declare_simd_clauses.exists ()
2097 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2098 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2099 omp_declare_simd_clauses);
2100 if (oacc_routine_clauses)
2101 c_finish_oacc_routine (parser, current_function_decl,
2102 oacc_routine_clauses, false, first, true);
2103 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2104 = c_parser_peek_token (parser)->location;
2105 fnbody = c_parser_compound_statement (parser);
2106 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2107 fnbody = expand_array_notation_exprs (fnbody);
2108 if (nested)
2110 tree decl = current_function_decl;
2111 /* Mark nested functions as needing static-chain initially.
2112 lower_nested_functions will recompute it but the
2113 DECL_STATIC_CHAIN flag is also used before that happens,
2114 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2115 DECL_STATIC_CHAIN (decl) = 1;
2116 add_stmt (fnbody);
2117 finish_function ();
2118 c_pop_function_context ();
2119 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2121 else
2123 add_stmt (fnbody);
2124 finish_function ();
2127 timevar_pop (tv);
2128 break;
2132 /* Parse an asm-definition (asm() outside a function body). This is a
2133 GNU extension.
2135 asm-definition:
2136 simple-asm-expr ;
2139 static void
2140 c_parser_asm_definition (c_parser *parser)
2142 tree asm_str = c_parser_simple_asm_expr (parser);
2143 if (asm_str)
2144 symtab->finalize_toplevel_asm (asm_str);
2145 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2148 /* Parse a static assertion (C11 6.7.10).
2150 static_assert-declaration:
2151 static_assert-declaration-no-semi ;
2154 static void
2155 c_parser_static_assert_declaration (c_parser *parser)
2157 c_parser_static_assert_declaration_no_semi (parser);
2158 if (parser->error
2159 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2160 c_parser_skip_to_end_of_block_or_statement (parser);
2163 /* Parse a static assertion (C11 6.7.10), without the trailing
2164 semicolon.
2166 static_assert-declaration-no-semi:
2167 _Static_assert ( constant-expression , string-literal )
2170 static void
2171 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2173 location_t assert_loc, value_loc;
2174 tree value;
2175 tree string;
2177 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2178 assert_loc = c_parser_peek_token (parser)->location;
2179 if (flag_isoc99)
2180 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2181 "ISO C99 does not support %<_Static_assert%>");
2182 else
2183 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2184 "ISO C90 does not support %<_Static_assert%>");
2185 c_parser_consume_token (parser);
2186 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2187 return;
2188 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2189 value = c_parser_expr_no_commas (parser, NULL).value;
2190 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2191 parser->lex_untranslated_string = true;
2192 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2194 parser->lex_untranslated_string = false;
2195 return;
2197 switch (c_parser_peek_token (parser)->type)
2199 case CPP_STRING:
2200 case CPP_STRING16:
2201 case CPP_STRING32:
2202 case CPP_WSTRING:
2203 case CPP_UTF8STRING:
2204 string = c_parser_peek_token (parser)->value;
2205 c_parser_consume_token (parser);
2206 parser->lex_untranslated_string = false;
2207 break;
2208 default:
2209 c_parser_error (parser, "expected string literal");
2210 parser->lex_untranslated_string = false;
2211 return;
2213 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2215 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2217 error_at (value_loc, "expression in static assertion is not an integer");
2218 return;
2220 if (TREE_CODE (value) != INTEGER_CST)
2222 value = c_fully_fold (value, false, NULL);
2223 /* Strip no-op conversions. */
2224 STRIP_TYPE_NOPS (value);
2225 if (TREE_CODE (value) == INTEGER_CST)
2226 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2227 "is not an integer constant expression");
2229 if (TREE_CODE (value) != INTEGER_CST)
2231 error_at (value_loc, "expression in static assertion is not constant");
2232 return;
2234 constant_expression_warning (value);
2235 if (integer_zerop (value))
2236 error_at (assert_loc, "static assertion failed: %E", string);
2239 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2240 6.7), adding them to SPECS (which may already include some).
2241 Storage class specifiers are accepted iff SCSPEC_OK; type
2242 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2243 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2244 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2246 declaration-specifiers:
2247 storage-class-specifier declaration-specifiers[opt]
2248 type-specifier declaration-specifiers[opt]
2249 type-qualifier declaration-specifiers[opt]
2250 function-specifier declaration-specifiers[opt]
2251 alignment-specifier declaration-specifiers[opt]
2253 Function specifiers (inline) are from C99, and are currently
2254 handled as storage class specifiers, as is __thread. Alignment
2255 specifiers are from C11.
2257 C90 6.5.1, C99 6.7.1:
2258 storage-class-specifier:
2259 typedef
2260 extern
2261 static
2262 auto
2263 register
2264 _Thread_local
2266 (_Thread_local is new in C11.)
2268 C99 6.7.4:
2269 function-specifier:
2270 inline
2271 _Noreturn
2273 (_Noreturn is new in C11.)
2275 C90 6.5.2, C99 6.7.2:
2276 type-specifier:
2277 void
2278 char
2279 short
2281 long
2282 float
2283 double
2284 signed
2285 unsigned
2286 _Bool
2287 _Complex
2288 [_Imaginary removed in C99 TC2]
2289 struct-or-union-specifier
2290 enum-specifier
2291 typedef-name
2292 atomic-type-specifier
2294 (_Bool and _Complex are new in C99.)
2295 (atomic-type-specifier is new in C11.)
2297 C90 6.5.3, C99 6.7.3:
2299 type-qualifier:
2300 const
2301 restrict
2302 volatile
2303 address-space-qualifier
2304 _Atomic
2306 (restrict is new in C99.)
2307 (_Atomic is new in C11.)
2309 GNU extensions:
2311 declaration-specifiers:
2312 attributes declaration-specifiers[opt]
2314 type-qualifier:
2315 address-space
2317 address-space:
2318 identifier recognized by the target
2320 storage-class-specifier:
2321 __thread
2323 type-specifier:
2324 typeof-specifier
2325 __auto_type
2326 __intN
2327 _Decimal32
2328 _Decimal64
2329 _Decimal128
2330 _Fract
2331 _Accum
2332 _Sat
2334 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2335 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2337 atomic-type-specifier
2338 _Atomic ( type-name )
2340 Objective-C:
2342 type-specifier:
2343 class-name objc-protocol-refs[opt]
2344 typedef-name objc-protocol-refs
2345 objc-protocol-refs
2348 static void
2349 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2350 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2351 bool alignspec_ok, bool auto_type_ok,
2352 enum c_lookahead_kind la)
2354 bool attrs_ok = start_attr_ok;
2355 bool seen_type = specs->typespec_kind != ctsk_none;
2357 if (!typespec_ok)
2358 gcc_assert (la == cla_prefer_id);
2360 while (c_parser_next_token_is (parser, CPP_NAME)
2361 || c_parser_next_token_is (parser, CPP_KEYWORD)
2362 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2364 struct c_typespec t;
2365 tree attrs;
2366 tree align;
2367 location_t loc = c_parser_peek_token (parser)->location;
2369 /* If we cannot accept a type, exit if the next token must start
2370 one. Also, if we already have seen a tagged definition,
2371 a typename would be an error anyway and likely the user
2372 has simply forgotten a semicolon, so we exit. */
2373 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2374 && c_parser_next_tokens_start_typename (parser, la)
2375 && !c_parser_next_token_is_qualifier (parser))
2376 break;
2378 if (c_parser_next_token_is (parser, CPP_NAME))
2380 c_token *name_token = c_parser_peek_token (parser);
2381 tree value = name_token->value;
2382 c_id_kind kind = name_token->id_kind;
2384 if (kind == C_ID_ADDRSPACE)
2386 addr_space_t as
2387 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2388 declspecs_add_addrspace (name_token->location, specs, as);
2389 c_parser_consume_token (parser);
2390 attrs_ok = true;
2391 continue;
2394 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2396 /* If we cannot accept a type, and the next token must start one,
2397 exit. Do the same if we already have seen a tagged definition,
2398 since it would be an error anyway and likely the user has simply
2399 forgotten a semicolon. */
2400 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2401 break;
2403 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2404 a C_ID_CLASSNAME. */
2405 c_parser_consume_token (parser);
2406 seen_type = true;
2407 attrs_ok = true;
2408 if (kind == C_ID_ID)
2410 error_at (loc, "unknown type name %qE", value);
2411 t.kind = ctsk_typedef;
2412 t.spec = error_mark_node;
2414 else if (kind == C_ID_TYPENAME
2415 && (!c_dialect_objc ()
2416 || c_parser_next_token_is_not (parser, CPP_LESS)))
2418 t.kind = ctsk_typedef;
2419 /* For a typedef name, record the meaning, not the name.
2420 In case of 'foo foo, bar;'. */
2421 t.spec = lookup_name (value);
2423 else
2425 tree proto = NULL_TREE;
2426 gcc_assert (c_dialect_objc ());
2427 t.kind = ctsk_objc;
2428 if (c_parser_next_token_is (parser, CPP_LESS))
2429 proto = c_parser_objc_protocol_refs (parser);
2430 t.spec = objc_get_protocol_qualified_type (value, proto);
2432 t.expr = NULL_TREE;
2433 t.expr_const_operands = true;
2434 declspecs_add_type (name_token->location, specs, t);
2435 continue;
2437 if (c_parser_next_token_is (parser, CPP_LESS))
2439 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2440 nisse@lysator.liu.se. */
2441 tree proto;
2442 gcc_assert (c_dialect_objc ());
2443 if (!typespec_ok || seen_type)
2444 break;
2445 proto = c_parser_objc_protocol_refs (parser);
2446 t.kind = ctsk_objc;
2447 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2448 t.expr = NULL_TREE;
2449 t.expr_const_operands = true;
2450 declspecs_add_type (loc, specs, t);
2451 continue;
2453 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2454 switch (c_parser_peek_token (parser)->keyword)
2456 case RID_STATIC:
2457 case RID_EXTERN:
2458 case RID_REGISTER:
2459 case RID_TYPEDEF:
2460 case RID_INLINE:
2461 case RID_NORETURN:
2462 case RID_AUTO:
2463 case RID_THREAD:
2464 if (!scspec_ok)
2465 goto out;
2466 attrs_ok = true;
2467 /* TODO: Distinguish between function specifiers (inline, noreturn)
2468 and storage class specifiers, either here or in
2469 declspecs_add_scspec. */
2470 declspecs_add_scspec (loc, specs,
2471 c_parser_peek_token (parser)->value);
2472 c_parser_consume_token (parser);
2473 break;
2474 case RID_AUTO_TYPE:
2475 if (!auto_type_ok)
2476 goto out;
2477 /* Fall through. */
2478 case RID_UNSIGNED:
2479 case RID_LONG:
2480 case RID_SHORT:
2481 case RID_SIGNED:
2482 case RID_COMPLEX:
2483 case RID_INT:
2484 case RID_CHAR:
2485 case RID_FLOAT:
2486 case RID_DOUBLE:
2487 case RID_VOID:
2488 case RID_DFLOAT32:
2489 case RID_DFLOAT64:
2490 case RID_DFLOAT128:
2491 case RID_BOOL:
2492 case RID_FRACT:
2493 case RID_ACCUM:
2494 case RID_SAT:
2495 case RID_INT_N_0:
2496 case RID_INT_N_1:
2497 case RID_INT_N_2:
2498 case RID_INT_N_3:
2499 if (!typespec_ok)
2500 goto out;
2501 attrs_ok = true;
2502 seen_type = true;
2503 if (c_dialect_objc ())
2504 parser->objc_need_raw_identifier = true;
2505 t.kind = ctsk_resword;
2506 t.spec = c_parser_peek_token (parser)->value;
2507 t.expr = NULL_TREE;
2508 t.expr_const_operands = true;
2509 declspecs_add_type (loc, specs, t);
2510 c_parser_consume_token (parser);
2511 break;
2512 case RID_ENUM:
2513 if (!typespec_ok)
2514 goto out;
2515 attrs_ok = true;
2516 seen_type = true;
2517 t = c_parser_enum_specifier (parser);
2518 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2519 declspecs_add_type (loc, specs, t);
2520 break;
2521 case RID_STRUCT:
2522 case RID_UNION:
2523 if (!typespec_ok)
2524 goto out;
2525 attrs_ok = true;
2526 seen_type = true;
2527 t = c_parser_struct_or_union_specifier (parser);
2528 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2529 declspecs_add_type (loc, specs, t);
2530 break;
2531 case RID_TYPEOF:
2532 /* ??? The old parser rejected typeof after other type
2533 specifiers, but is a syntax error the best way of
2534 handling this? */
2535 if (!typespec_ok || seen_type)
2536 goto out;
2537 attrs_ok = true;
2538 seen_type = true;
2539 t = c_parser_typeof_specifier (parser);
2540 declspecs_add_type (loc, specs, t);
2541 break;
2542 case RID_ATOMIC:
2543 /* C parser handling of Objective-C constructs needs
2544 checking for correct lvalue-to-rvalue conversions, and
2545 the code in build_modify_expr handling various
2546 Objective-C cases, and that in build_unary_op handling
2547 Objective-C cases for increment / decrement, also needs
2548 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2549 and objc_types_are_equivalent may also need updates. */
2550 if (c_dialect_objc ())
2551 sorry ("%<_Atomic%> in Objective-C");
2552 /* C parser handling of OpenMP constructs needs checking for
2553 correct lvalue-to-rvalue conversions. */
2554 if (flag_openmp)
2555 sorry ("%<_Atomic%> with OpenMP");
2556 if (flag_isoc99)
2557 pedwarn_c99 (loc, OPT_Wpedantic,
2558 "ISO C99 does not support the %<_Atomic%> qualifier");
2559 else
2560 pedwarn_c99 (loc, OPT_Wpedantic,
2561 "ISO C90 does not support the %<_Atomic%> qualifier");
2562 attrs_ok = true;
2563 tree value;
2564 value = c_parser_peek_token (parser)->value;
2565 c_parser_consume_token (parser);
2566 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2568 /* _Atomic ( type-name ). */
2569 seen_type = true;
2570 c_parser_consume_token (parser);
2571 struct c_type_name *type = c_parser_type_name (parser);
2572 t.kind = ctsk_typeof;
2573 t.spec = error_mark_node;
2574 t.expr = NULL_TREE;
2575 t.expr_const_operands = true;
2576 if (type != NULL)
2577 t.spec = groktypename (type, &t.expr,
2578 &t.expr_const_operands);
2579 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2580 "expected %<)%>");
2581 if (t.spec != error_mark_node)
2583 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2584 error_at (loc, "%<_Atomic%>-qualified array type");
2585 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2586 error_at (loc, "%<_Atomic%>-qualified function type");
2587 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2588 error_at (loc, "%<_Atomic%> applied to a qualified type");
2589 else
2590 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2592 declspecs_add_type (loc, specs, t);
2594 else
2595 declspecs_add_qual (loc, specs, value);
2596 break;
2597 case RID_CONST:
2598 case RID_VOLATILE:
2599 case RID_RESTRICT:
2600 attrs_ok = true;
2601 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2602 c_parser_consume_token (parser);
2603 break;
2604 case RID_ATTRIBUTE:
2605 if (!attrs_ok)
2606 goto out;
2607 attrs = c_parser_attributes (parser);
2608 declspecs_add_attrs (loc, specs, attrs);
2609 break;
2610 case RID_ALIGNAS:
2611 if (!alignspec_ok)
2612 goto out;
2613 align = c_parser_alignas_specifier (parser);
2614 declspecs_add_alignas (loc, specs, align);
2615 break;
2616 default:
2617 goto out;
2620 out: ;
2623 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2625 enum-specifier:
2626 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2627 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2628 enum attributes[opt] identifier
2630 The form with trailing comma is new in C99. The forms with
2631 attributes are GNU extensions. In GNU C, we accept any expression
2632 without commas in the syntax (assignment expressions, not just
2633 conditional expressions); assignment expressions will be diagnosed
2634 as non-constant.
2636 enumerator-list:
2637 enumerator
2638 enumerator-list , enumerator
2640 enumerator:
2641 enumeration-constant
2642 enumeration-constant = constant-expression
2644 GNU Extensions:
2646 enumerator:
2647 enumeration-constant attributes[opt]
2648 enumeration-constant attributes[opt] = constant-expression
2652 static struct c_typespec
2653 c_parser_enum_specifier (c_parser *parser)
2655 struct c_typespec ret;
2656 tree attrs;
2657 tree ident = NULL_TREE;
2658 location_t enum_loc;
2659 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2660 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2661 enum_loc = c_parser_peek_token (parser)->location;
2662 c_parser_consume_token (parser);
2663 attrs = c_parser_attributes (parser);
2664 enum_loc = c_parser_peek_token (parser)->location;
2665 /* Set the location in case we create a decl now. */
2666 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2667 if (c_parser_next_token_is (parser, CPP_NAME))
2669 ident = c_parser_peek_token (parser)->value;
2670 ident_loc = c_parser_peek_token (parser)->location;
2671 enum_loc = ident_loc;
2672 c_parser_consume_token (parser);
2674 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2676 /* Parse an enum definition. */
2677 struct c_enum_contents the_enum;
2678 tree type;
2679 tree postfix_attrs;
2680 /* We chain the enumerators in reverse order, then put them in
2681 forward order at the end. */
2682 tree values;
2683 timevar_push (TV_PARSE_ENUM);
2684 type = start_enum (enum_loc, &the_enum, ident);
2685 values = NULL_TREE;
2686 c_parser_consume_token (parser);
2687 while (true)
2689 tree enum_id;
2690 tree enum_value;
2691 tree enum_decl;
2692 bool seen_comma;
2693 c_token *token;
2694 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2695 location_t decl_loc, value_loc;
2696 if (c_parser_next_token_is_not (parser, CPP_NAME))
2698 /* Give a nicer error for "enum {}". */
2699 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2700 && !parser->error)
2702 error_at (c_parser_peek_token (parser)->location,
2703 "empty enum is invalid");
2704 parser->error = true;
2706 else
2707 c_parser_error (parser, "expected identifier");
2708 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2709 values = error_mark_node;
2710 break;
2712 token = c_parser_peek_token (parser);
2713 enum_id = token->value;
2714 /* Set the location in case we create a decl now. */
2715 c_parser_set_source_position_from_token (token);
2716 decl_loc = value_loc = token->location;
2717 c_parser_consume_token (parser);
2718 /* Parse any specified attributes. */
2719 tree enum_attrs = c_parser_attributes (parser);
2720 if (c_parser_next_token_is (parser, CPP_EQ))
2722 c_parser_consume_token (parser);
2723 value_loc = c_parser_peek_token (parser)->location;
2724 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2726 else
2727 enum_value = NULL_TREE;
2728 enum_decl = build_enumerator (decl_loc, value_loc,
2729 &the_enum, enum_id, enum_value);
2730 if (enum_attrs)
2731 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2732 TREE_CHAIN (enum_decl) = values;
2733 values = enum_decl;
2734 seen_comma = false;
2735 if (c_parser_next_token_is (parser, CPP_COMMA))
2737 comma_loc = c_parser_peek_token (parser)->location;
2738 seen_comma = true;
2739 c_parser_consume_token (parser);
2741 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2743 if (seen_comma)
2744 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2745 "comma at end of enumerator list");
2746 c_parser_consume_token (parser);
2747 break;
2749 if (!seen_comma)
2751 c_parser_error (parser, "expected %<,%> or %<}%>");
2752 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2753 values = error_mark_node;
2754 break;
2757 postfix_attrs = c_parser_attributes (parser);
2758 ret.spec = finish_enum (type, nreverse (values),
2759 chainon (attrs, postfix_attrs));
2760 ret.kind = ctsk_tagdef;
2761 ret.expr = NULL_TREE;
2762 ret.expr_const_operands = true;
2763 timevar_pop (TV_PARSE_ENUM);
2764 return ret;
2766 else if (!ident)
2768 c_parser_error (parser, "expected %<{%>");
2769 ret.spec = error_mark_node;
2770 ret.kind = ctsk_tagref;
2771 ret.expr = NULL_TREE;
2772 ret.expr_const_operands = true;
2773 return ret;
2775 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2776 /* In ISO C, enumerated types can be referred to only if already
2777 defined. */
2778 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2780 gcc_assert (ident);
2781 pedwarn (enum_loc, OPT_Wpedantic,
2782 "ISO C forbids forward references to %<enum%> types");
2784 return ret;
2787 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2789 struct-or-union-specifier:
2790 struct-or-union attributes[opt] identifier[opt]
2791 { struct-contents } attributes[opt]
2792 struct-or-union attributes[opt] identifier
2794 struct-contents:
2795 struct-declaration-list
2797 struct-declaration-list:
2798 struct-declaration ;
2799 struct-declaration-list struct-declaration ;
2801 GNU extensions:
2803 struct-contents:
2804 empty
2805 struct-declaration
2806 struct-declaration-list struct-declaration
2808 struct-declaration-list:
2809 struct-declaration-list ;
2812 (Note that in the syntax here, unlike that in ISO C, the semicolons
2813 are included here rather than in struct-declaration, in order to
2814 describe the syntax with extra semicolons and missing semicolon at
2815 end.)
2817 Objective-C:
2819 struct-declaration-list:
2820 @defs ( class-name )
2822 (Note this does not include a trailing semicolon, but can be
2823 followed by further declarations, and gets a pedwarn-if-pedantic
2824 when followed by a semicolon.) */
2826 static struct c_typespec
2827 c_parser_struct_or_union_specifier (c_parser *parser)
2829 struct c_typespec ret;
2830 tree attrs;
2831 tree ident = NULL_TREE;
2832 location_t struct_loc;
2833 location_t ident_loc = UNKNOWN_LOCATION;
2834 enum tree_code code;
2835 switch (c_parser_peek_token (parser)->keyword)
2837 case RID_STRUCT:
2838 code = RECORD_TYPE;
2839 break;
2840 case RID_UNION:
2841 code = UNION_TYPE;
2842 break;
2843 default:
2844 gcc_unreachable ();
2846 struct_loc = c_parser_peek_token (parser)->location;
2847 c_parser_consume_token (parser);
2848 attrs = c_parser_attributes (parser);
2850 /* Set the location in case we create a decl now. */
2851 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2853 if (c_parser_next_token_is (parser, CPP_NAME))
2855 ident = c_parser_peek_token (parser)->value;
2856 ident_loc = c_parser_peek_token (parser)->location;
2857 struct_loc = ident_loc;
2858 c_parser_consume_token (parser);
2860 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2862 /* Parse a struct or union definition. Start the scope of the
2863 tag before parsing components. */
2864 struct c_struct_parse_info *struct_info;
2865 tree type = start_struct (struct_loc, code, ident, &struct_info);
2866 tree postfix_attrs;
2867 /* We chain the components in reverse order, then put them in
2868 forward order at the end. Each struct-declaration may
2869 declare multiple components (comma-separated), so we must use
2870 chainon to join them, although when parsing each
2871 struct-declaration we can use TREE_CHAIN directly.
2873 The theory behind all this is that there will be more
2874 semicolon separated fields than comma separated fields, and
2875 so we'll be minimizing the number of node traversals required
2876 by chainon. */
2877 tree contents;
2878 timevar_push (TV_PARSE_STRUCT);
2879 contents = NULL_TREE;
2880 c_parser_consume_token (parser);
2881 /* Handle the Objective-C @defs construct,
2882 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2883 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2885 tree name;
2886 gcc_assert (c_dialect_objc ());
2887 c_parser_consume_token (parser);
2888 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2889 goto end_at_defs;
2890 if (c_parser_next_token_is (parser, CPP_NAME)
2891 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2893 name = c_parser_peek_token (parser)->value;
2894 c_parser_consume_token (parser);
2896 else
2898 c_parser_error (parser, "expected class name");
2899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2900 goto end_at_defs;
2902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2903 "expected %<)%>");
2904 contents = nreverse (objc_get_class_ivars (name));
2906 end_at_defs:
2907 /* Parse the struct-declarations and semicolons. Problems with
2908 semicolons are diagnosed here; empty structures are diagnosed
2909 elsewhere. */
2910 while (true)
2912 tree decls;
2913 /* Parse any stray semicolon. */
2914 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2916 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2917 "extra semicolon in struct or union specified");
2918 c_parser_consume_token (parser);
2919 continue;
2921 /* Stop if at the end of the struct or union contents. */
2922 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2924 c_parser_consume_token (parser);
2925 break;
2927 /* Accept #pragmas at struct scope. */
2928 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2930 c_parser_pragma (parser, pragma_struct, NULL);
2931 continue;
2933 /* Parse some comma-separated declarations, but not the
2934 trailing semicolon if any. */
2935 decls = c_parser_struct_declaration (parser);
2936 contents = chainon (decls, contents);
2937 /* If no semicolon follows, either we have a parse error or
2938 are at the end of the struct or union and should
2939 pedwarn. */
2940 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2941 c_parser_consume_token (parser);
2942 else
2944 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2945 pedwarn (c_parser_peek_token (parser)->location, 0,
2946 "no semicolon at end of struct or union");
2947 else if (parser->error
2948 || !c_parser_next_token_starts_declspecs (parser))
2950 c_parser_error (parser, "expected %<;%>");
2951 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2952 break;
2955 /* If we come here, we have already emitted an error
2956 for an expected `;', identifier or `(', and we also
2957 recovered already. Go on with the next field. */
2960 postfix_attrs = c_parser_attributes (parser);
2961 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2962 chainon (attrs, postfix_attrs), struct_info);
2963 ret.kind = ctsk_tagdef;
2964 ret.expr = NULL_TREE;
2965 ret.expr_const_operands = true;
2966 timevar_pop (TV_PARSE_STRUCT);
2967 return ret;
2969 else if (!ident)
2971 c_parser_error (parser, "expected %<{%>");
2972 ret.spec = error_mark_node;
2973 ret.kind = ctsk_tagref;
2974 ret.expr = NULL_TREE;
2975 ret.expr_const_operands = true;
2976 return ret;
2978 ret = parser_xref_tag (ident_loc, code, ident);
2979 return ret;
2982 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2983 the trailing semicolon.
2985 struct-declaration:
2986 specifier-qualifier-list struct-declarator-list
2987 static_assert-declaration-no-semi
2989 specifier-qualifier-list:
2990 type-specifier specifier-qualifier-list[opt]
2991 type-qualifier specifier-qualifier-list[opt]
2992 attributes specifier-qualifier-list[opt]
2994 struct-declarator-list:
2995 struct-declarator
2996 struct-declarator-list , attributes[opt] struct-declarator
2998 struct-declarator:
2999 declarator attributes[opt]
3000 declarator[opt] : constant-expression attributes[opt]
3002 GNU extensions:
3004 struct-declaration:
3005 __extension__ struct-declaration
3006 specifier-qualifier-list
3008 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3009 of attributes where shown is a GNU extension. In GNU C, we accept
3010 any expression without commas in the syntax (assignment
3011 expressions, not just conditional expressions); assignment
3012 expressions will be diagnosed as non-constant. */
3014 static tree
3015 c_parser_struct_declaration (c_parser *parser)
3017 struct c_declspecs *specs;
3018 tree prefix_attrs;
3019 tree all_prefix_attrs;
3020 tree decls;
3021 location_t decl_loc;
3022 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3024 int ext;
3025 tree decl;
3026 ext = disable_extension_diagnostics ();
3027 c_parser_consume_token (parser);
3028 decl = c_parser_struct_declaration (parser);
3029 restore_extension_diagnostics (ext);
3030 return decl;
3032 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3034 c_parser_static_assert_declaration_no_semi (parser);
3035 return NULL_TREE;
3037 specs = build_null_declspecs ();
3038 decl_loc = c_parser_peek_token (parser)->location;
3039 /* Strictly by the standard, we shouldn't allow _Alignas here,
3040 but it appears to have been intended to allow it there, so
3041 we're keeping it as it is until WG14 reaches a conclusion
3042 of N1731.
3043 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3044 c_parser_declspecs (parser, specs, false, true, true,
3045 true, false, cla_nonabstract_decl);
3046 if (parser->error)
3047 return NULL_TREE;
3048 if (!specs->declspecs_seen_p)
3050 c_parser_error (parser, "expected specifier-qualifier-list");
3051 return NULL_TREE;
3053 finish_declspecs (specs);
3054 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3055 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3057 tree ret;
3058 if (specs->typespec_kind == ctsk_none)
3060 pedwarn (decl_loc, OPT_Wpedantic,
3061 "ISO C forbids member declarations with no members");
3062 shadow_tag_warned (specs, pedantic);
3063 ret = NULL_TREE;
3065 else
3067 /* Support for unnamed structs or unions as members of
3068 structs or unions (which is [a] useful and [b] supports
3069 MS P-SDK). */
3070 tree attrs = NULL;
3072 ret = grokfield (c_parser_peek_token (parser)->location,
3073 build_id_declarator (NULL_TREE), specs,
3074 NULL_TREE, &attrs);
3075 if (ret)
3076 decl_attributes (&ret, attrs, 0);
3078 return ret;
3081 /* Provide better error recovery. Note that a type name here is valid,
3082 and will be treated as a field name. */
3083 if (specs->typespec_kind == ctsk_tagdef
3084 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3085 && c_parser_next_token_starts_declspecs (parser)
3086 && !c_parser_next_token_is (parser, CPP_NAME))
3088 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3089 parser->error = false;
3090 return NULL_TREE;
3093 pending_xref_error ();
3094 prefix_attrs = specs->attrs;
3095 all_prefix_attrs = prefix_attrs;
3096 specs->attrs = NULL_TREE;
3097 decls = NULL_TREE;
3098 while (true)
3100 /* Declaring one or more declarators or un-named bit-fields. */
3101 struct c_declarator *declarator;
3102 bool dummy = false;
3103 if (c_parser_next_token_is (parser, CPP_COLON))
3104 declarator = build_id_declarator (NULL_TREE);
3105 else
3106 declarator = c_parser_declarator (parser,
3107 specs->typespec_kind != ctsk_none,
3108 C_DTR_NORMAL, &dummy);
3109 if (declarator == NULL)
3111 c_parser_skip_to_end_of_block_or_statement (parser);
3112 break;
3114 if (c_parser_next_token_is (parser, CPP_COLON)
3115 || c_parser_next_token_is (parser, CPP_COMMA)
3116 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3117 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3118 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3120 tree postfix_attrs = NULL_TREE;
3121 tree width = NULL_TREE;
3122 tree d;
3123 if (c_parser_next_token_is (parser, CPP_COLON))
3125 c_parser_consume_token (parser);
3126 width = c_parser_expr_no_commas (parser, NULL).value;
3128 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3129 postfix_attrs = c_parser_attributes (parser);
3130 d = grokfield (c_parser_peek_token (parser)->location,
3131 declarator, specs, width, &all_prefix_attrs);
3132 decl_attributes (&d, chainon (postfix_attrs,
3133 all_prefix_attrs), 0);
3134 DECL_CHAIN (d) = decls;
3135 decls = d;
3136 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3137 all_prefix_attrs = chainon (c_parser_attributes (parser),
3138 prefix_attrs);
3139 else
3140 all_prefix_attrs = prefix_attrs;
3141 if (c_parser_next_token_is (parser, CPP_COMMA))
3142 c_parser_consume_token (parser);
3143 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3144 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3146 /* Semicolon consumed in caller. */
3147 break;
3149 else
3151 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3152 break;
3155 else
3157 c_parser_error (parser,
3158 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3159 "%<__attribute__%>");
3160 break;
3163 return decls;
3166 /* Parse a typeof specifier (a GNU extension).
3168 typeof-specifier:
3169 typeof ( expression )
3170 typeof ( type-name )
3173 static struct c_typespec
3174 c_parser_typeof_specifier (c_parser *parser)
3176 struct c_typespec ret;
3177 ret.kind = ctsk_typeof;
3178 ret.spec = error_mark_node;
3179 ret.expr = NULL_TREE;
3180 ret.expr_const_operands = true;
3181 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3182 c_parser_consume_token (parser);
3183 c_inhibit_evaluation_warnings++;
3184 in_typeof++;
3185 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3187 c_inhibit_evaluation_warnings--;
3188 in_typeof--;
3189 return ret;
3191 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3193 struct c_type_name *type = c_parser_type_name (parser);
3194 c_inhibit_evaluation_warnings--;
3195 in_typeof--;
3196 if (type != NULL)
3198 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3199 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3202 else
3204 bool was_vm;
3205 location_t here = c_parser_peek_token (parser)->location;
3206 struct c_expr expr = c_parser_expression (parser);
3207 c_inhibit_evaluation_warnings--;
3208 in_typeof--;
3209 if (TREE_CODE (expr.value) == COMPONENT_REF
3210 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3211 error_at (here, "%<typeof%> applied to a bit-field");
3212 mark_exp_read (expr.value);
3213 ret.spec = TREE_TYPE (expr.value);
3214 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3215 /* This is returned with the type so that when the type is
3216 evaluated, this can be evaluated. */
3217 if (was_vm)
3218 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3219 pop_maybe_used (was_vm);
3220 /* For use in macros such as those in <stdatomic.h>, remove all
3221 qualifiers from atomic types. (const can be an issue for more macros
3222 using typeof than just the <stdatomic.h> ones.) */
3223 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3224 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3226 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3227 return ret;
3230 /* Parse an alignment-specifier.
3232 C11 6.7.5:
3234 alignment-specifier:
3235 _Alignas ( type-name )
3236 _Alignas ( constant-expression )
3239 static tree
3240 c_parser_alignas_specifier (c_parser * parser)
3242 tree ret = error_mark_node;
3243 location_t loc = c_parser_peek_token (parser)->location;
3244 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3245 c_parser_consume_token (parser);
3246 if (flag_isoc99)
3247 pedwarn_c99 (loc, OPT_Wpedantic,
3248 "ISO C99 does not support %<_Alignas%>");
3249 else
3250 pedwarn_c99 (loc, OPT_Wpedantic,
3251 "ISO C90 does not support %<_Alignas%>");
3252 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3253 return ret;
3254 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3256 struct c_type_name *type = c_parser_type_name (parser);
3257 if (type != NULL)
3258 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3259 false, true, 1);
3261 else
3262 ret = c_parser_expr_no_commas (parser, NULL).value;
3263 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3264 return ret;
3267 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3268 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3269 be redeclared; otherwise it may not. KIND indicates which kind of
3270 declarator is wanted. Returns a valid declarator except in the
3271 case of a syntax error in which case NULL is returned. *SEEN_ID is
3272 set to true if an identifier being declared is seen; this is used
3273 to diagnose bad forms of abstract array declarators and to
3274 determine whether an identifier list is syntactically permitted.
3276 declarator:
3277 pointer[opt] direct-declarator
3279 direct-declarator:
3280 identifier
3281 ( attributes[opt] declarator )
3282 direct-declarator array-declarator
3283 direct-declarator ( parameter-type-list )
3284 direct-declarator ( identifier-list[opt] )
3286 pointer:
3287 * type-qualifier-list[opt]
3288 * type-qualifier-list[opt] pointer
3290 type-qualifier-list:
3291 type-qualifier
3292 attributes
3293 type-qualifier-list type-qualifier
3294 type-qualifier-list attributes
3296 array-declarator:
3297 [ type-qualifier-list[opt] assignment-expression[opt] ]
3298 [ static type-qualifier-list[opt] assignment-expression ]
3299 [ type-qualifier-list static assignment-expression ]
3300 [ type-qualifier-list[opt] * ]
3302 parameter-type-list:
3303 parameter-list
3304 parameter-list , ...
3306 parameter-list:
3307 parameter-declaration
3308 parameter-list , parameter-declaration
3310 parameter-declaration:
3311 declaration-specifiers declarator attributes[opt]
3312 declaration-specifiers abstract-declarator[opt] attributes[opt]
3314 identifier-list:
3315 identifier
3316 identifier-list , identifier
3318 abstract-declarator:
3319 pointer
3320 pointer[opt] direct-abstract-declarator
3322 direct-abstract-declarator:
3323 ( attributes[opt] abstract-declarator )
3324 direct-abstract-declarator[opt] array-declarator
3325 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3327 GNU extensions:
3329 direct-declarator:
3330 direct-declarator ( parameter-forward-declarations
3331 parameter-type-list[opt] )
3333 direct-abstract-declarator:
3334 direct-abstract-declarator[opt] ( parameter-forward-declarations
3335 parameter-type-list[opt] )
3337 parameter-forward-declarations:
3338 parameter-list ;
3339 parameter-forward-declarations parameter-list ;
3341 The uses of attributes shown above are GNU extensions.
3343 Some forms of array declarator are not included in C99 in the
3344 syntax for abstract declarators; these are disallowed elsewhere.
3345 This may be a defect (DR#289).
3347 This function also accepts an omitted abstract declarator as being
3348 an abstract declarator, although not part of the formal syntax. */
3350 static struct c_declarator *
3351 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3352 bool *seen_id)
3354 /* Parse any initial pointer part. */
3355 if (c_parser_next_token_is (parser, CPP_MULT))
3357 struct c_declspecs *quals_attrs = build_null_declspecs ();
3358 struct c_declarator *inner;
3359 c_parser_consume_token (parser);
3360 c_parser_declspecs (parser, quals_attrs, false, false, true,
3361 false, false, cla_prefer_id);
3362 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3363 if (inner == NULL)
3364 return NULL;
3365 else
3366 return make_pointer_declarator (quals_attrs, inner);
3368 /* Now we have a direct declarator, direct abstract declarator or
3369 nothing (which counts as a direct abstract declarator here). */
3370 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3373 /* Parse a direct declarator or direct abstract declarator; arguments
3374 as c_parser_declarator. */
3376 static struct c_declarator *
3377 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3378 bool *seen_id)
3380 /* The direct declarator must start with an identifier (possibly
3381 omitted) or a parenthesized declarator (possibly abstract). In
3382 an ordinary declarator, initial parentheses must start a
3383 parenthesized declarator. In an abstract declarator or parameter
3384 declarator, they could start a parenthesized declarator or a
3385 parameter list. To tell which, the open parenthesis and any
3386 following attributes must be read. If a declaration specifier
3387 follows, then it is a parameter list; if the specifier is a
3388 typedef name, there might be an ambiguity about redeclaring it,
3389 which is resolved in the direction of treating it as a typedef
3390 name. If a close parenthesis follows, it is also an empty
3391 parameter list, as the syntax does not permit empty abstract
3392 declarators. Otherwise, it is a parenthesized declarator (in
3393 which case the analysis may be repeated inside it, recursively).
3395 ??? There is an ambiguity in a parameter declaration "int
3396 (__attribute__((foo)) x)", where x is not a typedef name: it
3397 could be an abstract declarator for a function, or declare x with
3398 parentheses. The proper resolution of this ambiguity needs
3399 documenting. At present we follow an accident of the old
3400 parser's implementation, whereby the first parameter must have
3401 some declaration specifiers other than just attributes. Thus as
3402 a parameter declaration it is treated as a parenthesized
3403 parameter named x, and as an abstract declarator it is
3404 rejected.
3406 ??? Also following the old parser, attributes inside an empty
3407 parameter list are ignored, making it a list not yielding a
3408 prototype, rather than giving an error or making it have one
3409 parameter with implicit type int.
3411 ??? Also following the old parser, typedef names may be
3412 redeclared in declarators, but not Objective-C class names. */
3414 if (kind != C_DTR_ABSTRACT
3415 && c_parser_next_token_is (parser, CPP_NAME)
3416 && ((type_seen_p
3417 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3418 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3419 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3421 struct c_declarator *inner
3422 = build_id_declarator (c_parser_peek_token (parser)->value);
3423 *seen_id = true;
3424 inner->id_loc = c_parser_peek_token (parser)->location;
3425 c_parser_consume_token (parser);
3426 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3429 if (kind != C_DTR_NORMAL
3430 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3432 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3433 inner->id_loc = c_parser_peek_token (parser)->location;
3434 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3437 /* Either we are at the end of an abstract declarator, or we have
3438 parentheses. */
3440 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3442 tree attrs;
3443 struct c_declarator *inner;
3444 c_parser_consume_token (parser);
3445 attrs = c_parser_attributes (parser);
3446 if (kind != C_DTR_NORMAL
3447 && (c_parser_next_token_starts_declspecs (parser)
3448 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3450 struct c_arg_info *args
3451 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3452 attrs);
3453 if (args == NULL)
3454 return NULL;
3455 else
3457 inner
3458 = build_function_declarator (args,
3459 build_id_declarator (NULL_TREE));
3460 return c_parser_direct_declarator_inner (parser, *seen_id,
3461 inner);
3464 /* A parenthesized declarator. */
3465 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3466 if (inner != NULL && attrs != NULL)
3467 inner = build_attrs_declarator (attrs, inner);
3468 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3470 c_parser_consume_token (parser);
3471 if (inner == NULL)
3472 return NULL;
3473 else
3474 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3476 else
3478 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3479 "expected %<)%>");
3480 return NULL;
3483 else
3485 if (kind == C_DTR_NORMAL)
3487 c_parser_error (parser, "expected identifier or %<(%>");
3488 return NULL;
3490 else
3491 return build_id_declarator (NULL_TREE);
3495 /* Parse part of a direct declarator or direct abstract declarator,
3496 given that some (in INNER) has already been parsed; ID_PRESENT is
3497 true if an identifier is present, false for an abstract
3498 declarator. */
3500 static struct c_declarator *
3501 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3502 struct c_declarator *inner)
3504 /* Parse a sequence of array declarators and parameter lists. */
3505 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3507 location_t brace_loc = c_parser_peek_token (parser)->location;
3508 struct c_declarator *declarator;
3509 struct c_declspecs *quals_attrs = build_null_declspecs ();
3510 bool static_seen;
3511 bool star_seen;
3512 struct c_expr dimen;
3513 dimen.value = NULL_TREE;
3514 dimen.original_code = ERROR_MARK;
3515 dimen.original_type = NULL_TREE;
3516 c_parser_consume_token (parser);
3517 c_parser_declspecs (parser, quals_attrs, false, false, true,
3518 false, false, cla_prefer_id);
3519 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3520 if (static_seen)
3521 c_parser_consume_token (parser);
3522 if (static_seen && !quals_attrs->declspecs_seen_p)
3523 c_parser_declspecs (parser, quals_attrs, false, false, true,
3524 false, false, cla_prefer_id);
3525 if (!quals_attrs->declspecs_seen_p)
3526 quals_attrs = NULL;
3527 /* If "static" is present, there must be an array dimension.
3528 Otherwise, there may be a dimension, "*", or no
3529 dimension. */
3530 if (static_seen)
3532 star_seen = false;
3533 dimen = c_parser_expr_no_commas (parser, NULL);
3535 else
3537 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3539 dimen.value = NULL_TREE;
3540 star_seen = false;
3542 else if (flag_cilkplus
3543 && c_parser_next_token_is (parser, CPP_COLON))
3545 dimen.value = error_mark_node;
3546 star_seen = false;
3547 error_at (c_parser_peek_token (parser)->location,
3548 "array notations cannot be used in declaration");
3549 c_parser_consume_token (parser);
3551 else if (c_parser_next_token_is (parser, CPP_MULT))
3553 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3555 dimen.value = NULL_TREE;
3556 star_seen = true;
3557 c_parser_consume_token (parser);
3559 else
3561 star_seen = false;
3562 dimen = c_parser_expr_no_commas (parser, NULL);
3565 else
3567 star_seen = false;
3568 dimen = c_parser_expr_no_commas (parser, NULL);
3571 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3572 c_parser_consume_token (parser);
3573 else if (flag_cilkplus
3574 && c_parser_next_token_is (parser, CPP_COLON))
3576 error_at (c_parser_peek_token (parser)->location,
3577 "array notations cannot be used in declaration");
3578 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3579 return NULL;
3581 else
3583 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3584 "expected %<]%>");
3585 return NULL;
3587 if (dimen.value)
3588 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3589 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3590 static_seen, star_seen);
3591 if (declarator == NULL)
3592 return NULL;
3593 inner = set_array_declarator_inner (declarator, inner);
3594 return c_parser_direct_declarator_inner (parser, id_present, inner);
3596 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3598 tree attrs;
3599 struct c_arg_info *args;
3600 c_parser_consume_token (parser);
3601 attrs = c_parser_attributes (parser);
3602 args = c_parser_parms_declarator (parser, id_present, attrs);
3603 if (args == NULL)
3604 return NULL;
3605 else
3607 inner = build_function_declarator (args, inner);
3608 return c_parser_direct_declarator_inner (parser, id_present, inner);
3611 return inner;
3614 /* Parse a parameter list or identifier list, including the closing
3615 parenthesis but not the opening one. ATTRS are the attributes at
3616 the start of the list. ID_LIST_OK is true if an identifier list is
3617 acceptable; such a list must not have attributes at the start. */
3619 static struct c_arg_info *
3620 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3622 push_scope ();
3623 declare_parm_level ();
3624 /* If the list starts with an identifier, it is an identifier list.
3625 Otherwise, it is either a prototype list or an empty list. */
3626 if (id_list_ok
3627 && !attrs
3628 && c_parser_next_token_is (parser, CPP_NAME)
3629 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3631 /* Look ahead to detect typos in type names. */
3632 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3633 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3634 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3635 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3637 tree list = NULL_TREE, *nextp = &list;
3638 while (c_parser_next_token_is (parser, CPP_NAME)
3639 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3641 *nextp = build_tree_list (NULL_TREE,
3642 c_parser_peek_token (parser)->value);
3643 nextp = & TREE_CHAIN (*nextp);
3644 c_parser_consume_token (parser);
3645 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3646 break;
3647 c_parser_consume_token (parser);
3648 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3650 c_parser_error (parser, "expected identifier");
3651 break;
3654 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3656 struct c_arg_info *ret = build_arg_info ();
3657 ret->types = list;
3658 c_parser_consume_token (parser);
3659 pop_scope ();
3660 return ret;
3662 else
3664 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3665 "expected %<)%>");
3666 pop_scope ();
3667 return NULL;
3670 else
3672 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3673 NULL);
3674 pop_scope ();
3675 return ret;
3679 /* Parse a parameter list (possibly empty), including the closing
3680 parenthesis but not the opening one. ATTRS are the attributes at
3681 the start of the list. EXPR is NULL or an expression that needs to
3682 be evaluated for the side effects of array size expressions in the
3683 parameters. */
3685 static struct c_arg_info *
3686 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3688 bool bad_parm = false;
3690 /* ??? Following the old parser, forward parameter declarations may
3691 use abstract declarators, and if no real parameter declarations
3692 follow the forward declarations then this is not diagnosed. Also
3693 note as above that attributes are ignored as the only contents of
3694 the parentheses, or as the only contents after forward
3695 declarations. */
3696 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3698 struct c_arg_info *ret = build_arg_info ();
3699 c_parser_consume_token (parser);
3700 return ret;
3702 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3704 struct c_arg_info *ret = build_arg_info ();
3706 if (flag_allow_parameterless_variadic_functions)
3708 /* F (...) is allowed. */
3709 ret->types = NULL_TREE;
3711 else
3713 /* Suppress -Wold-style-definition for this case. */
3714 ret->types = error_mark_node;
3715 error_at (c_parser_peek_token (parser)->location,
3716 "ISO C requires a named argument before %<...%>");
3718 c_parser_consume_token (parser);
3719 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3721 c_parser_consume_token (parser);
3722 return ret;
3724 else
3726 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3727 "expected %<)%>");
3728 return NULL;
3731 /* Nonempty list of parameters, either terminated with semicolon
3732 (forward declarations; recurse) or with close parenthesis (normal
3733 function) or with ", ... )" (variadic function). */
3734 while (true)
3736 /* Parse a parameter. */
3737 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3738 attrs = NULL_TREE;
3739 if (parm == NULL)
3740 bad_parm = true;
3741 else
3742 push_parm_decl (parm, &expr);
3743 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3745 tree new_attrs;
3746 c_parser_consume_token (parser);
3747 mark_forward_parm_decls ();
3748 new_attrs = c_parser_attributes (parser);
3749 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3751 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3753 c_parser_consume_token (parser);
3754 if (bad_parm)
3755 return NULL;
3756 else
3757 return get_parm_info (false, expr);
3759 if (!c_parser_require (parser, CPP_COMMA,
3760 "expected %<;%>, %<,%> or %<)%>"))
3762 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3763 return NULL;
3765 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3767 c_parser_consume_token (parser);
3768 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3770 c_parser_consume_token (parser);
3771 if (bad_parm)
3772 return NULL;
3773 else
3774 return get_parm_info (true, expr);
3776 else
3778 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3779 "expected %<)%>");
3780 return NULL;
3786 /* Parse a parameter declaration. ATTRS are the attributes at the
3787 start of the declaration if it is the first parameter. */
3789 static struct c_parm *
3790 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3792 struct c_declspecs *specs;
3793 struct c_declarator *declarator;
3794 tree prefix_attrs;
3795 tree postfix_attrs = NULL_TREE;
3796 bool dummy = false;
3798 /* Accept #pragmas between parameter declarations. */
3799 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3800 c_parser_pragma (parser, pragma_param, NULL);
3802 if (!c_parser_next_token_starts_declspecs (parser))
3804 c_token *token = c_parser_peek_token (parser);
3805 if (parser->error)
3806 return NULL;
3807 c_parser_set_source_position_from_token (token);
3808 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3810 error_at (token->location, "unknown type name %qE", token->value);
3811 parser->error = true;
3813 /* ??? In some Objective-C cases '...' isn't applicable so there
3814 should be a different message. */
3815 else
3816 c_parser_error (parser,
3817 "expected declaration specifiers or %<...%>");
3818 c_parser_skip_to_end_of_parameter (parser);
3819 return NULL;
3821 specs = build_null_declspecs ();
3822 if (attrs)
3824 declspecs_add_attrs (input_location, specs, attrs);
3825 attrs = NULL_TREE;
3827 c_parser_declspecs (parser, specs, true, true, true, true, false,
3828 cla_nonabstract_decl);
3829 finish_declspecs (specs);
3830 pending_xref_error ();
3831 prefix_attrs = specs->attrs;
3832 specs->attrs = NULL_TREE;
3833 declarator = c_parser_declarator (parser,
3834 specs->typespec_kind != ctsk_none,
3835 C_DTR_PARM, &dummy);
3836 if (declarator == NULL)
3838 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3839 return NULL;
3841 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3842 postfix_attrs = c_parser_attributes (parser);
3843 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3844 declarator);
3847 /* Parse a string literal in an asm expression. It should not be
3848 translated, and wide string literals are an error although
3849 permitted by the syntax. This is a GNU extension.
3851 asm-string-literal:
3852 string-literal
3854 ??? At present, following the old parser, the caller needs to have
3855 set lex_untranslated_string to 1. It would be better to follow the
3856 C++ parser rather than using this kludge. */
3858 static tree
3859 c_parser_asm_string_literal (c_parser *parser)
3861 tree str;
3862 int save_flag = warn_overlength_strings;
3863 warn_overlength_strings = 0;
3864 if (c_parser_next_token_is (parser, CPP_STRING))
3866 str = c_parser_peek_token (parser)->value;
3867 c_parser_consume_token (parser);
3869 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3871 error_at (c_parser_peek_token (parser)->location,
3872 "wide string literal in %<asm%>");
3873 str = build_string (1, "");
3874 c_parser_consume_token (parser);
3876 else
3878 c_parser_error (parser, "expected string literal");
3879 str = NULL_TREE;
3881 warn_overlength_strings = save_flag;
3882 return str;
3885 /* Parse a simple asm expression. This is used in restricted
3886 contexts, where a full expression with inputs and outputs does not
3887 make sense. This is a GNU extension.
3889 simple-asm-expr:
3890 asm ( asm-string-literal )
3893 static tree
3894 c_parser_simple_asm_expr (c_parser *parser)
3896 tree str;
3897 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3898 /* ??? Follow the C++ parser rather than using the
3899 lex_untranslated_string kludge. */
3900 parser->lex_untranslated_string = true;
3901 c_parser_consume_token (parser);
3902 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3904 parser->lex_untranslated_string = false;
3905 return NULL_TREE;
3907 str = c_parser_asm_string_literal (parser);
3908 parser->lex_untranslated_string = false;
3909 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3911 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3912 return NULL_TREE;
3914 return str;
3917 static tree
3918 c_parser_attribute_any_word (c_parser *parser)
3920 tree attr_name = NULL_TREE;
3922 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3924 /* ??? See comment above about what keywords are accepted here. */
3925 bool ok;
3926 switch (c_parser_peek_token (parser)->keyword)
3928 case RID_STATIC:
3929 case RID_UNSIGNED:
3930 case RID_LONG:
3931 case RID_CONST:
3932 case RID_EXTERN:
3933 case RID_REGISTER:
3934 case RID_TYPEDEF:
3935 case RID_SHORT:
3936 case RID_INLINE:
3937 case RID_NORETURN:
3938 case RID_VOLATILE:
3939 case RID_SIGNED:
3940 case RID_AUTO:
3941 case RID_RESTRICT:
3942 case RID_COMPLEX:
3943 case RID_THREAD:
3944 case RID_INT:
3945 case RID_CHAR:
3946 case RID_FLOAT:
3947 case RID_DOUBLE:
3948 case RID_VOID:
3949 case RID_DFLOAT32:
3950 case RID_DFLOAT64:
3951 case RID_DFLOAT128:
3952 case RID_BOOL:
3953 case RID_FRACT:
3954 case RID_ACCUM:
3955 case RID_SAT:
3956 case RID_TRANSACTION_ATOMIC:
3957 case RID_TRANSACTION_CANCEL:
3958 case RID_ATOMIC:
3959 case RID_AUTO_TYPE:
3960 case RID_INT_N_0:
3961 case RID_INT_N_1:
3962 case RID_INT_N_2:
3963 case RID_INT_N_3:
3964 ok = true;
3965 break;
3966 default:
3967 ok = false;
3968 break;
3970 if (!ok)
3971 return NULL_TREE;
3973 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3974 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3976 else if (c_parser_next_token_is (parser, CPP_NAME))
3977 attr_name = c_parser_peek_token (parser)->value;
3979 return attr_name;
3982 #define CILK_SIMD_FN_CLAUSE_MASK \
3983 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3984 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3985 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3986 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3987 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3989 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3990 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3991 pushed into the token list.
3992 Syntax:
3993 vector
3994 vector (<vector attributes>). */
3996 static void
3997 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3999 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4001 int paren_scope = 0;
4002 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4003 /* Consume the "vector" token. */
4004 c_parser_consume_token (parser);
4006 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4008 c_parser_consume_token (parser);
4009 paren_scope++;
4011 while (paren_scope > 0)
4013 c_token *token = c_parser_peek_token (parser);
4014 if (token->type == CPP_OPEN_PAREN)
4015 paren_scope++;
4016 else if (token->type == CPP_CLOSE_PAREN)
4017 paren_scope--;
4018 /* Do not push the last ')' since we are not pushing the '('. */
4019 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4020 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4021 c_parser_consume_token (parser);
4024 /* Since we are converting an attribute to a pragma, we need to end the
4025 attribute with PRAGMA_EOL. */
4026 c_token eol_token;
4027 memset (&eol_token, 0, sizeof (eol_token));
4028 eol_token.type = CPP_PRAGMA_EOL;
4029 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4032 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4034 static void
4035 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4037 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4039 /* c_parser_attributes is called in several places, so if these EOF
4040 tokens are already inserted, then don't do them again. */
4041 if (last_token.type == CPP_EOF)
4042 return;
4044 /* Two CPP_EOF token are added as a safety net since the normal C
4045 front-end has two token look-ahead. */
4046 c_token eof_token;
4047 eof_token.type = CPP_EOF;
4048 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4049 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4052 /* Parse (possibly empty) attributes. This is a GNU extension.
4054 attributes:
4055 empty
4056 attributes attribute
4058 attribute:
4059 __attribute__ ( ( attribute-list ) )
4061 attribute-list:
4062 attrib
4063 attribute_list , attrib
4065 attrib:
4066 empty
4067 any-word
4068 any-word ( identifier )
4069 any-word ( identifier , nonempty-expr-list )
4070 any-word ( expr-list )
4072 where the "identifier" must not be declared as a type, and
4073 "any-word" may be any identifier (including one declared as a
4074 type), a reserved word storage class specifier, type specifier or
4075 type qualifier. ??? This still leaves out most reserved keywords
4076 (following the old parser), shouldn't we include them, and why not
4077 allow identifiers declared as types to start the arguments? */
4079 static tree
4080 c_parser_attributes (c_parser *parser)
4082 tree attrs = NULL_TREE;
4083 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4085 /* ??? Follow the C++ parser rather than using the
4086 lex_untranslated_string kludge. */
4087 parser->lex_untranslated_string = true;
4088 /* Consume the `__attribute__' keyword. */
4089 c_parser_consume_token (parser);
4090 /* Look for the two `(' tokens. */
4091 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4093 parser->lex_untranslated_string = false;
4094 return attrs;
4096 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4098 parser->lex_untranslated_string = false;
4099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4100 return attrs;
4102 /* Parse the attribute list. */
4103 while (c_parser_next_token_is (parser, CPP_COMMA)
4104 || c_parser_next_token_is (parser, CPP_NAME)
4105 || c_parser_next_token_is (parser, CPP_KEYWORD))
4107 tree attr, attr_name, attr_args;
4108 vec<tree, va_gc> *expr_list;
4109 if (c_parser_next_token_is (parser, CPP_COMMA))
4111 c_parser_consume_token (parser);
4112 continue;
4115 attr_name = c_parser_attribute_any_word (parser);
4116 if (attr_name == NULL)
4117 break;
4118 if (is_cilkplus_vector_p (attr_name))
4120 c_token *v_token = c_parser_peek_token (parser);
4121 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4122 /* If the next token isn't a comma, we're done. */
4123 if (!c_parser_next_token_is (parser, CPP_COMMA))
4124 break;
4125 continue;
4127 c_parser_consume_token (parser);
4128 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4130 attr = build_tree_list (attr_name, NULL_TREE);
4131 /* Add this attribute to the list. */
4132 attrs = chainon (attrs, attr);
4133 /* If the next token isn't a comma, we're done. */
4134 if (!c_parser_next_token_is (parser, CPP_COMMA))
4135 break;
4136 continue;
4138 c_parser_consume_token (parser);
4139 /* Parse the attribute contents. If they start with an
4140 identifier which is followed by a comma or close
4141 parenthesis, then the arguments start with that
4142 identifier; otherwise they are an expression list.
4143 In objective-c the identifier may be a classname. */
4144 if (c_parser_next_token_is (parser, CPP_NAME)
4145 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4146 || (c_dialect_objc ()
4147 && c_parser_peek_token (parser)->id_kind
4148 == C_ID_CLASSNAME))
4149 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4150 || (c_parser_peek_2nd_token (parser)->type
4151 == CPP_CLOSE_PAREN))
4152 && (attribute_takes_identifier_p (attr_name)
4153 || (c_dialect_objc ()
4154 && c_parser_peek_token (parser)->id_kind
4155 == C_ID_CLASSNAME)))
4157 tree arg1 = c_parser_peek_token (parser)->value;
4158 c_parser_consume_token (parser);
4159 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4160 attr_args = build_tree_list (NULL_TREE, arg1);
4161 else
4163 tree tree_list;
4164 c_parser_consume_token (parser);
4165 expr_list = c_parser_expr_list (parser, false, true,
4166 NULL, NULL, NULL, NULL);
4167 tree_list = build_tree_list_vec (expr_list);
4168 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4169 release_tree_vector (expr_list);
4172 else
4174 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4175 attr_args = NULL_TREE;
4176 else
4178 expr_list = c_parser_expr_list (parser, false, true,
4179 NULL, NULL, NULL, NULL);
4180 attr_args = build_tree_list_vec (expr_list);
4181 release_tree_vector (expr_list);
4184 attr = build_tree_list (attr_name, attr_args);
4185 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4186 c_parser_consume_token (parser);
4187 else
4189 parser->lex_untranslated_string = false;
4190 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4191 "expected %<)%>");
4192 return attrs;
4194 /* Add this attribute to the list. */
4195 attrs = chainon (attrs, attr);
4196 /* If the next token isn't a comma, we're done. */
4197 if (!c_parser_next_token_is (parser, CPP_COMMA))
4198 break;
4200 /* Look for the two `)' tokens. */
4201 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4202 c_parser_consume_token (parser);
4203 else
4205 parser->lex_untranslated_string = false;
4206 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4207 "expected %<)%>");
4208 return attrs;
4210 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4211 c_parser_consume_token (parser);
4212 else
4214 parser->lex_untranslated_string = false;
4215 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4216 "expected %<)%>");
4217 return attrs;
4219 parser->lex_untranslated_string = false;
4222 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4223 c_finish_cilk_simd_fn_tokens (parser);
4224 return attrs;
4227 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4229 type-name:
4230 specifier-qualifier-list abstract-declarator[opt]
4233 static struct c_type_name *
4234 c_parser_type_name (c_parser *parser)
4236 struct c_declspecs *specs = build_null_declspecs ();
4237 struct c_declarator *declarator;
4238 struct c_type_name *ret;
4239 bool dummy = false;
4240 c_parser_declspecs (parser, specs, false, true, true, false, false,
4241 cla_prefer_type);
4242 if (!specs->declspecs_seen_p)
4244 c_parser_error (parser, "expected specifier-qualifier-list");
4245 return NULL;
4247 if (specs->type != error_mark_node)
4249 pending_xref_error ();
4250 finish_declspecs (specs);
4252 declarator = c_parser_declarator (parser,
4253 specs->typespec_kind != ctsk_none,
4254 C_DTR_ABSTRACT, &dummy);
4255 if (declarator == NULL)
4256 return NULL;
4257 ret = XOBNEW (&parser_obstack, struct c_type_name);
4258 ret->specs = specs;
4259 ret->declarator = declarator;
4260 return ret;
4263 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4265 initializer:
4266 assignment-expression
4267 { initializer-list }
4268 { initializer-list , }
4270 initializer-list:
4271 designation[opt] initializer
4272 initializer-list , designation[opt] initializer
4274 designation:
4275 designator-list =
4277 designator-list:
4278 designator
4279 designator-list designator
4281 designator:
4282 array-designator
4283 . identifier
4285 array-designator:
4286 [ constant-expression ]
4288 GNU extensions:
4290 initializer:
4293 designation:
4294 array-designator
4295 identifier :
4297 array-designator:
4298 [ constant-expression ... constant-expression ]
4300 Any expression without commas is accepted in the syntax for the
4301 constant-expressions, with non-constant expressions rejected later.
4303 This function is only used for top-level initializers; for nested
4304 ones, see c_parser_initval. */
4306 static struct c_expr
4307 c_parser_initializer (c_parser *parser)
4309 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4310 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4311 else
4313 struct c_expr ret;
4314 location_t loc = c_parser_peek_token (parser)->location;
4315 ret = c_parser_expr_no_commas (parser, NULL);
4316 if (TREE_CODE (ret.value) != STRING_CST
4317 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4318 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4319 return ret;
4323 /* Parse a braced initializer list. TYPE is the type specified for a
4324 compound literal, and NULL_TREE for other initializers and for
4325 nested braced lists. NESTED_P is true for nested braced lists,
4326 false for the list of a compound literal or the list that is the
4327 top-level initializer in a declaration. */
4329 static struct c_expr
4330 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4331 struct obstack *outer_obstack)
4333 struct c_expr ret;
4334 struct obstack braced_init_obstack;
4335 location_t brace_loc = c_parser_peek_token (parser)->location;
4336 gcc_obstack_init (&braced_init_obstack);
4337 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4338 c_parser_consume_token (parser);
4339 if (nested_p)
4341 finish_implicit_inits (brace_loc, outer_obstack);
4342 push_init_level (brace_loc, 0, &braced_init_obstack);
4344 else
4345 really_start_incremental_init (type);
4346 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4348 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4350 else
4352 /* Parse a non-empty initializer list, possibly with a trailing
4353 comma. */
4354 while (true)
4356 c_parser_initelt (parser, &braced_init_obstack);
4357 if (parser->error)
4358 break;
4359 if (c_parser_next_token_is (parser, CPP_COMMA))
4360 c_parser_consume_token (parser);
4361 else
4362 break;
4363 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4364 break;
4367 c_token *next_tok = c_parser_peek_token (parser);
4368 if (next_tok->type != CPP_CLOSE_BRACE)
4370 ret.value = error_mark_node;
4371 ret.original_code = ERROR_MARK;
4372 ret.original_type = NULL;
4373 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4374 pop_init_level (brace_loc, 0, &braced_init_obstack);
4375 obstack_free (&braced_init_obstack, NULL);
4376 return ret;
4378 location_t close_loc = next_tok->location;
4379 c_parser_consume_token (parser);
4380 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4381 obstack_free (&braced_init_obstack, NULL);
4382 set_c_expr_source_range (&ret, brace_loc, close_loc);
4383 return ret;
4386 /* Parse a nested initializer, including designators. */
4388 static void
4389 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4391 /* Parse any designator or designator list. A single array
4392 designator may have the subsequent "=" omitted in GNU C, but a
4393 longer list or a structure member designator may not. */
4394 if (c_parser_next_token_is (parser, CPP_NAME)
4395 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4397 /* Old-style structure member designator. */
4398 set_init_label (c_parser_peek_token (parser)->location,
4399 c_parser_peek_token (parser)->value,
4400 c_parser_peek_token (parser)->location,
4401 braced_init_obstack);
4402 /* Use the colon as the error location. */
4403 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4404 "obsolete use of designated initializer with %<:%>");
4405 c_parser_consume_token (parser);
4406 c_parser_consume_token (parser);
4408 else
4410 /* des_seen is 0 if there have been no designators, 1 if there
4411 has been a single array designator and 2 otherwise. */
4412 int des_seen = 0;
4413 /* Location of a designator. */
4414 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4415 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4416 || c_parser_next_token_is (parser, CPP_DOT))
4418 int des_prev = des_seen;
4419 if (!des_seen)
4420 des_loc = c_parser_peek_token (parser)->location;
4421 if (des_seen < 2)
4422 des_seen++;
4423 if (c_parser_next_token_is (parser, CPP_DOT))
4425 des_seen = 2;
4426 c_parser_consume_token (parser);
4427 if (c_parser_next_token_is (parser, CPP_NAME))
4429 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4430 c_parser_peek_token (parser)->location,
4431 braced_init_obstack);
4432 c_parser_consume_token (parser);
4434 else
4436 struct c_expr init;
4437 init.value = error_mark_node;
4438 init.original_code = ERROR_MARK;
4439 init.original_type = NULL;
4440 c_parser_error (parser, "expected identifier");
4441 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4442 process_init_element (input_location, init, false,
4443 braced_init_obstack);
4444 return;
4447 else
4449 tree first, second;
4450 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4451 location_t array_index_loc = UNKNOWN_LOCATION;
4452 /* ??? Following the old parser, [ objc-receiver
4453 objc-message-args ] is accepted as an initializer,
4454 being distinguished from a designator by what follows
4455 the first assignment expression inside the square
4456 brackets, but after a first array designator a
4457 subsequent square bracket is for Objective-C taken to
4458 start an expression, using the obsolete form of
4459 designated initializer without '=', rather than
4460 possibly being a second level of designation: in LALR
4461 terms, the '[' is shifted rather than reducing
4462 designator to designator-list. */
4463 if (des_prev == 1 && c_dialect_objc ())
4465 des_seen = des_prev;
4466 break;
4468 if (des_prev == 0 && c_dialect_objc ())
4470 /* This might be an array designator or an
4471 Objective-C message expression. If the former,
4472 continue parsing here; if the latter, parse the
4473 remainder of the initializer given the starting
4474 primary-expression. ??? It might make sense to
4475 distinguish when des_prev == 1 as well; see
4476 previous comment. */
4477 tree rec, args;
4478 struct c_expr mexpr;
4479 c_parser_consume_token (parser);
4480 if (c_parser_peek_token (parser)->type == CPP_NAME
4481 && ((c_parser_peek_token (parser)->id_kind
4482 == C_ID_TYPENAME)
4483 || (c_parser_peek_token (parser)->id_kind
4484 == C_ID_CLASSNAME)))
4486 /* Type name receiver. */
4487 tree id = c_parser_peek_token (parser)->value;
4488 c_parser_consume_token (parser);
4489 rec = objc_get_class_reference (id);
4490 goto parse_message_args;
4492 first = c_parser_expr_no_commas (parser, NULL).value;
4493 mark_exp_read (first);
4494 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4495 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4496 goto array_desig_after_first;
4497 /* Expression receiver. So far only one part
4498 without commas has been parsed; there might be
4499 more of the expression. */
4500 rec = first;
4501 while (c_parser_next_token_is (parser, CPP_COMMA))
4503 struct c_expr next;
4504 location_t comma_loc, exp_loc;
4505 comma_loc = c_parser_peek_token (parser)->location;
4506 c_parser_consume_token (parser);
4507 exp_loc = c_parser_peek_token (parser)->location;
4508 next = c_parser_expr_no_commas (parser, NULL);
4509 next = convert_lvalue_to_rvalue (exp_loc, next,
4510 true, true);
4511 rec = build_compound_expr (comma_loc, rec, next.value);
4513 parse_message_args:
4514 /* Now parse the objc-message-args. */
4515 args = c_parser_objc_message_args (parser);
4516 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4517 "expected %<]%>");
4518 mexpr.value
4519 = objc_build_message_expr (rec, args);
4520 mexpr.original_code = ERROR_MARK;
4521 mexpr.original_type = NULL;
4522 /* Now parse and process the remainder of the
4523 initializer, starting with this message
4524 expression as a primary-expression. */
4525 c_parser_initval (parser, &mexpr, braced_init_obstack);
4526 return;
4528 c_parser_consume_token (parser);
4529 array_index_loc = c_parser_peek_token (parser)->location;
4530 first = c_parser_expr_no_commas (parser, NULL).value;
4531 mark_exp_read (first);
4532 array_desig_after_first:
4533 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4535 ellipsis_loc = c_parser_peek_token (parser)->location;
4536 c_parser_consume_token (parser);
4537 second = c_parser_expr_no_commas (parser, NULL).value;
4538 mark_exp_read (second);
4540 else
4541 second = NULL_TREE;
4542 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4544 c_parser_consume_token (parser);
4545 set_init_index (array_index_loc, first, second,
4546 braced_init_obstack);
4547 if (second)
4548 pedwarn (ellipsis_loc, OPT_Wpedantic,
4549 "ISO C forbids specifying range of elements to initialize");
4551 else
4552 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4553 "expected %<]%>");
4556 if (des_seen >= 1)
4558 if (c_parser_next_token_is (parser, CPP_EQ))
4560 pedwarn_c90 (des_loc, OPT_Wpedantic,
4561 "ISO C90 forbids specifying subobject "
4562 "to initialize");
4563 c_parser_consume_token (parser);
4565 else
4567 if (des_seen == 1)
4568 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4569 "obsolete use of designated initializer without %<=%>");
4570 else
4572 struct c_expr init;
4573 init.value = error_mark_node;
4574 init.original_code = ERROR_MARK;
4575 init.original_type = NULL;
4576 c_parser_error (parser, "expected %<=%>");
4577 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4578 process_init_element (input_location, init, false,
4579 braced_init_obstack);
4580 return;
4585 c_parser_initval (parser, NULL, braced_init_obstack);
4588 /* Parse a nested initializer; as c_parser_initializer but parses
4589 initializers within braced lists, after any designators have been
4590 applied. If AFTER is not NULL then it is an Objective-C message
4591 expression which is the primary-expression starting the
4592 initializer. */
4594 static void
4595 c_parser_initval (c_parser *parser, struct c_expr *after,
4596 struct obstack * braced_init_obstack)
4598 struct c_expr init;
4599 gcc_assert (!after || c_dialect_objc ());
4600 location_t loc = c_parser_peek_token (parser)->location;
4602 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4603 init = c_parser_braced_init (parser, NULL_TREE, true,
4604 braced_init_obstack);
4605 else
4607 init = c_parser_expr_no_commas (parser, after);
4608 if (init.value != NULL_TREE
4609 && TREE_CODE (init.value) != STRING_CST
4610 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4611 init = convert_lvalue_to_rvalue (loc, init, true, true);
4613 process_init_element (loc, init, false, braced_init_obstack);
4616 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4617 C99 6.8.2).
4619 compound-statement:
4620 { block-item-list[opt] }
4621 { label-declarations block-item-list }
4623 block-item-list:
4624 block-item
4625 block-item-list block-item
4627 block-item:
4628 nested-declaration
4629 statement
4631 nested-declaration:
4632 declaration
4634 GNU extensions:
4636 compound-statement:
4637 { label-declarations block-item-list }
4639 nested-declaration:
4640 __extension__ nested-declaration
4641 nested-function-definition
4643 label-declarations:
4644 label-declaration
4645 label-declarations label-declaration
4647 label-declaration:
4648 __label__ identifier-list ;
4650 Allowing the mixing of declarations and code is new in C99. The
4651 GNU syntax also permits (not shown above) labels at the end of
4652 compound statements, which yield an error. We don't allow labels
4653 on declarations; this might seem like a natural extension, but
4654 there would be a conflict between attributes on the label and
4655 prefix attributes on the declaration. ??? The syntax follows the
4656 old parser in requiring something after label declarations.
4657 Although they are erroneous if the labels declared aren't defined,
4658 is it useful for the syntax to be this way?
4660 OpenACC:
4662 block-item:
4663 openacc-directive
4665 openacc-directive:
4666 update-directive
4668 OpenMP:
4670 block-item:
4671 openmp-directive
4673 openmp-directive:
4674 barrier-directive
4675 flush-directive
4676 taskwait-directive
4677 taskyield-directive
4678 cancel-directive
4679 cancellation-point-directive */
4681 static tree
4682 c_parser_compound_statement (c_parser *parser)
4684 tree stmt;
4685 location_t brace_loc;
4686 brace_loc = c_parser_peek_token (parser)->location;
4687 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4689 /* Ensure a scope is entered and left anyway to avoid confusion
4690 if we have just prepared to enter a function body. */
4691 stmt = c_begin_compound_stmt (true);
4692 c_end_compound_stmt (brace_loc, stmt, true);
4693 return error_mark_node;
4695 stmt = c_begin_compound_stmt (true);
4696 c_parser_compound_statement_nostart (parser);
4698 /* If the compound stmt contains array notations, then we expand them. */
4699 if (flag_cilkplus && contains_array_notation_expr (stmt))
4700 stmt = expand_array_notation_exprs (stmt);
4701 return c_end_compound_stmt (brace_loc, stmt, true);
4704 /* Parse a compound statement except for the opening brace. This is
4705 used for parsing both compound statements and statement expressions
4706 (which follow different paths to handling the opening). */
4708 static void
4709 c_parser_compound_statement_nostart (c_parser *parser)
4711 bool last_stmt = false;
4712 bool last_label = false;
4713 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4714 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4715 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4717 c_parser_consume_token (parser);
4718 return;
4720 mark_valid_location_for_stdc_pragma (true);
4721 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4723 /* Read zero or more forward-declarations for labels that nested
4724 functions can jump to. */
4725 mark_valid_location_for_stdc_pragma (false);
4726 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4728 label_loc = c_parser_peek_token (parser)->location;
4729 c_parser_consume_token (parser);
4730 /* Any identifiers, including those declared as type names,
4731 are OK here. */
4732 while (true)
4734 tree label;
4735 if (c_parser_next_token_is_not (parser, CPP_NAME))
4737 c_parser_error (parser, "expected identifier");
4738 break;
4740 label
4741 = declare_label (c_parser_peek_token (parser)->value);
4742 C_DECLARED_LABEL_FLAG (label) = 1;
4743 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4744 c_parser_consume_token (parser);
4745 if (c_parser_next_token_is (parser, CPP_COMMA))
4746 c_parser_consume_token (parser);
4747 else
4748 break;
4750 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4752 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4754 /* We must now have at least one statement, label or declaration. */
4755 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4757 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4758 c_parser_error (parser, "expected declaration or statement");
4759 c_parser_consume_token (parser);
4760 return;
4762 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4764 location_t loc = c_parser_peek_token (parser)->location;
4765 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4766 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4767 || (c_parser_next_token_is (parser, CPP_NAME)
4768 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4770 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4771 label_loc = c_parser_peek_2nd_token (parser)->location;
4772 else
4773 label_loc = c_parser_peek_token (parser)->location;
4774 last_label = true;
4775 last_stmt = false;
4776 mark_valid_location_for_stdc_pragma (false);
4777 c_parser_label (parser);
4779 else if (!last_label
4780 && c_parser_next_tokens_start_declaration (parser))
4782 last_label = false;
4783 mark_valid_location_for_stdc_pragma (false);
4784 c_parser_declaration_or_fndef (parser, true, true, true, true,
4785 true, NULL, vNULL);
4786 if (last_stmt)
4787 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4788 "ISO C90 forbids mixed declarations and code");
4789 last_stmt = false;
4791 else if (!last_label
4792 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4794 /* __extension__ can start a declaration, but is also an
4795 unary operator that can start an expression. Consume all
4796 but the last of a possible series of __extension__ to
4797 determine which. */
4798 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4799 && (c_parser_peek_2nd_token (parser)->keyword
4800 == RID_EXTENSION))
4801 c_parser_consume_token (parser);
4802 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4804 int ext;
4805 ext = disable_extension_diagnostics ();
4806 c_parser_consume_token (parser);
4807 last_label = false;
4808 mark_valid_location_for_stdc_pragma (false);
4809 c_parser_declaration_or_fndef (parser, true, true, true, true,
4810 true, NULL, vNULL);
4811 /* Following the old parser, __extension__ does not
4812 disable this diagnostic. */
4813 restore_extension_diagnostics (ext);
4814 if (last_stmt)
4815 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4816 "ISO C90 forbids mixed declarations and code");
4817 last_stmt = false;
4819 else
4820 goto statement;
4822 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4824 /* External pragmas, and some omp pragmas, are not associated
4825 with regular c code, and so are not to be considered statements
4826 syntactically. This ensures that the user doesn't put them
4827 places that would turn into syntax errors if the directive
4828 were ignored. */
4829 if (c_parser_pragma (parser,
4830 last_label ? pragma_stmt : pragma_compound,
4831 NULL))
4832 last_label = false, last_stmt = true;
4834 else if (c_parser_next_token_is (parser, CPP_EOF))
4836 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4837 c_parser_error (parser, "expected declaration or statement");
4838 return;
4840 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4842 if (parser->in_if_block)
4844 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4845 error_at (loc, """expected %<}%> before %<else%>");
4846 return;
4848 else
4850 error_at (loc, "%<else%> without a previous %<if%>");
4851 c_parser_consume_token (parser);
4852 continue;
4855 else
4857 statement:
4858 last_label = false;
4859 last_stmt = true;
4860 mark_valid_location_for_stdc_pragma (false);
4861 c_parser_statement_after_labels (parser, NULL);
4864 parser->error = false;
4866 if (last_label)
4867 error_at (label_loc, "label at end of compound statement");
4868 c_parser_consume_token (parser);
4869 /* Restore the value we started with. */
4870 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4873 /* Parse all consecutive labels. */
4875 static void
4876 c_parser_all_labels (c_parser *parser)
4878 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4879 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4880 || (c_parser_next_token_is (parser, CPP_NAME)
4881 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4882 c_parser_label (parser);
4885 /* Parse a label (C90 6.6.1, C99 6.8.1).
4887 label:
4888 identifier : attributes[opt]
4889 case constant-expression :
4890 default :
4892 GNU extensions:
4894 label:
4895 case constant-expression ... constant-expression :
4897 The use of attributes on labels is a GNU extension. The syntax in
4898 GNU C accepts any expressions without commas, non-constant
4899 expressions being rejected later. */
4901 static void
4902 c_parser_label (c_parser *parser)
4904 location_t loc1 = c_parser_peek_token (parser)->location;
4905 tree label = NULL_TREE;
4906 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4908 tree exp1, exp2;
4909 c_parser_consume_token (parser);
4910 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4911 if (c_parser_next_token_is (parser, CPP_COLON))
4913 c_parser_consume_token (parser);
4914 label = do_case (loc1, exp1, NULL_TREE);
4916 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4918 c_parser_consume_token (parser);
4919 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4920 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4921 label = do_case (loc1, exp1, exp2);
4923 else
4924 c_parser_error (parser, "expected %<:%> or %<...%>");
4926 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4928 c_parser_consume_token (parser);
4929 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4930 label = do_case (loc1, NULL_TREE, NULL_TREE);
4932 else
4934 tree name = c_parser_peek_token (parser)->value;
4935 tree tlab;
4936 tree attrs;
4937 location_t loc2 = c_parser_peek_token (parser)->location;
4938 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4939 c_parser_consume_token (parser);
4940 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4941 c_parser_consume_token (parser);
4942 attrs = c_parser_attributes (parser);
4943 tlab = define_label (loc2, name);
4944 if (tlab)
4946 decl_attributes (&tlab, attrs, 0);
4947 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4950 if (label)
4952 if (c_parser_next_tokens_start_declaration (parser))
4954 error_at (c_parser_peek_token (parser)->location,
4955 "a label can only be part of a statement and "
4956 "a declaration is not a statement");
4957 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4958 /*static_assert_ok*/ true,
4959 /*empty_ok*/ true, /*nested*/ true,
4960 /*start_attr_ok*/ true, NULL,
4961 vNULL);
4966 /* Parse a statement (C90 6.6, C99 6.8).
4968 statement:
4969 labeled-statement
4970 compound-statement
4971 expression-statement
4972 selection-statement
4973 iteration-statement
4974 jump-statement
4976 labeled-statement:
4977 label statement
4979 expression-statement:
4980 expression[opt] ;
4982 selection-statement:
4983 if-statement
4984 switch-statement
4986 iteration-statement:
4987 while-statement
4988 do-statement
4989 for-statement
4991 jump-statement:
4992 goto identifier ;
4993 continue ;
4994 break ;
4995 return expression[opt] ;
4997 GNU extensions:
4999 statement:
5000 asm-statement
5002 jump-statement:
5003 goto * expression ;
5005 Objective-C:
5007 statement:
5008 objc-throw-statement
5009 objc-try-catch-statement
5010 objc-synchronized-statement
5012 objc-throw-statement:
5013 @throw expression ;
5014 @throw ;
5016 OpenACC:
5018 statement:
5019 openacc-construct
5021 openacc-construct:
5022 parallel-construct
5023 kernels-construct
5024 data-construct
5025 loop-construct
5027 parallel-construct:
5028 parallel-directive structured-block
5030 kernels-construct:
5031 kernels-directive structured-block
5033 data-construct:
5034 data-directive structured-block
5036 loop-construct:
5037 loop-directive structured-block
5039 OpenMP:
5041 statement:
5042 openmp-construct
5044 openmp-construct:
5045 parallel-construct
5046 for-construct
5047 simd-construct
5048 for-simd-construct
5049 sections-construct
5050 single-construct
5051 parallel-for-construct
5052 parallel-for-simd-construct
5053 parallel-sections-construct
5054 master-construct
5055 critical-construct
5056 atomic-construct
5057 ordered-construct
5059 parallel-construct:
5060 parallel-directive structured-block
5062 for-construct:
5063 for-directive iteration-statement
5065 simd-construct:
5066 simd-directive iteration-statements
5068 for-simd-construct:
5069 for-simd-directive iteration-statements
5071 sections-construct:
5072 sections-directive section-scope
5074 single-construct:
5075 single-directive structured-block
5077 parallel-for-construct:
5078 parallel-for-directive iteration-statement
5080 parallel-for-simd-construct:
5081 parallel-for-simd-directive iteration-statement
5083 parallel-sections-construct:
5084 parallel-sections-directive section-scope
5086 master-construct:
5087 master-directive structured-block
5089 critical-construct:
5090 critical-directive structured-block
5092 atomic-construct:
5093 atomic-directive expression-statement
5095 ordered-construct:
5096 ordered-directive structured-block
5098 Transactional Memory:
5100 statement:
5101 transaction-statement
5102 transaction-cancel-statement
5104 IF_P is used to track whether there's a (possibly labeled) if statement
5105 which is not enclosed in braces and has an else clause. This is used to
5106 implement -Wparentheses. */
5108 static void
5109 c_parser_statement (c_parser *parser, bool *if_p)
5111 c_parser_all_labels (parser);
5112 c_parser_statement_after_labels (parser, if_p, NULL);
5115 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5116 of if-else-if conditions.
5118 IF_P is used to track whether there's a (possibly labeled) if statement
5119 which is not enclosed in braces and has an else clause. This is used to
5120 implement -Wparentheses. */
5122 static void
5123 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5124 vec<tree> *chain)
5126 location_t loc = c_parser_peek_token (parser)->location;
5127 tree stmt = NULL_TREE;
5128 bool in_if_block = parser->in_if_block;
5129 parser->in_if_block = false;
5130 if (if_p != NULL)
5131 *if_p = false;
5132 switch (c_parser_peek_token (parser)->type)
5134 case CPP_OPEN_BRACE:
5135 add_stmt (c_parser_compound_statement (parser));
5136 break;
5137 case CPP_KEYWORD:
5138 switch (c_parser_peek_token (parser)->keyword)
5140 case RID_IF:
5141 c_parser_if_statement (parser, if_p, chain);
5142 break;
5143 case RID_SWITCH:
5144 c_parser_switch_statement (parser, if_p);
5145 break;
5146 case RID_WHILE:
5147 c_parser_while_statement (parser, false, if_p);
5148 break;
5149 case RID_DO:
5150 c_parser_do_statement (parser, false);
5151 break;
5152 case RID_FOR:
5153 c_parser_for_statement (parser, false, if_p);
5154 break;
5155 case RID_CILK_FOR:
5156 if (!flag_cilkplus)
5158 error_at (c_parser_peek_token (parser)->location,
5159 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5160 c_parser_skip_to_end_of_block_or_statement (parser);
5162 else
5163 c_parser_cilk_for (parser, integer_zero_node, if_p);
5164 break;
5165 case RID_CILK_SYNC:
5166 c_parser_consume_token (parser);
5167 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5168 if (!flag_cilkplus)
5169 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5170 else
5171 add_stmt (build_cilk_sync ());
5172 break;
5173 case RID_GOTO:
5174 c_parser_consume_token (parser);
5175 if (c_parser_next_token_is (parser, CPP_NAME))
5177 stmt = c_finish_goto_label (loc,
5178 c_parser_peek_token (parser)->value);
5179 c_parser_consume_token (parser);
5181 else if (c_parser_next_token_is (parser, CPP_MULT))
5183 struct c_expr val;
5185 c_parser_consume_token (parser);
5186 val = c_parser_expression (parser);
5187 if (check_no_cilk (val.value,
5188 "Cilk array notation cannot be used as a computed goto expression",
5189 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5190 loc))
5191 val.value = error_mark_node;
5192 val = convert_lvalue_to_rvalue (loc, val, false, true);
5193 stmt = c_finish_goto_ptr (loc, val.value);
5195 else
5196 c_parser_error (parser, "expected identifier or %<*%>");
5197 goto expect_semicolon;
5198 case RID_CONTINUE:
5199 c_parser_consume_token (parser);
5200 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5201 goto expect_semicolon;
5202 case RID_BREAK:
5203 c_parser_consume_token (parser);
5204 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5205 goto expect_semicolon;
5206 case RID_RETURN:
5207 c_parser_consume_token (parser);
5208 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5210 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5211 c_parser_consume_token (parser);
5213 else
5215 location_t xloc = c_parser_peek_token (parser)->location;
5216 struct c_expr expr = c_parser_expression_conv (parser);
5217 mark_exp_read (expr.value);
5218 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5219 expr.value, expr.original_type);
5220 goto expect_semicolon;
5222 break;
5223 case RID_ASM:
5224 stmt = c_parser_asm_statement (parser);
5225 break;
5226 case RID_TRANSACTION_ATOMIC:
5227 case RID_TRANSACTION_RELAXED:
5228 stmt = c_parser_transaction (parser,
5229 c_parser_peek_token (parser)->keyword);
5230 break;
5231 case RID_TRANSACTION_CANCEL:
5232 stmt = c_parser_transaction_cancel (parser);
5233 goto expect_semicolon;
5234 case RID_AT_THROW:
5235 gcc_assert (c_dialect_objc ());
5236 c_parser_consume_token (parser);
5237 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5239 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5240 c_parser_consume_token (parser);
5242 else
5244 struct c_expr expr = c_parser_expression (parser);
5245 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5246 if (check_no_cilk (expr.value,
5247 "Cilk array notation cannot be used for a throw expression",
5248 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5249 expr.value = error_mark_node;
5250 else
5252 expr.value = c_fully_fold (expr.value, false, NULL);
5253 stmt = objc_build_throw_stmt (loc, expr.value);
5255 goto expect_semicolon;
5257 break;
5258 case RID_AT_TRY:
5259 gcc_assert (c_dialect_objc ());
5260 c_parser_objc_try_catch_finally_statement (parser);
5261 break;
5262 case RID_AT_SYNCHRONIZED:
5263 gcc_assert (c_dialect_objc ());
5264 c_parser_objc_synchronized_statement (parser);
5265 break;
5266 default:
5267 goto expr_stmt;
5269 break;
5270 case CPP_SEMICOLON:
5271 c_parser_consume_token (parser);
5272 break;
5273 case CPP_CLOSE_PAREN:
5274 case CPP_CLOSE_SQUARE:
5275 /* Avoid infinite loop in error recovery:
5276 c_parser_skip_until_found stops at a closing nesting
5277 delimiter without consuming it, but here we need to consume
5278 it to proceed further. */
5279 c_parser_error (parser, "expected statement");
5280 c_parser_consume_token (parser);
5281 break;
5282 case CPP_PRAGMA:
5283 c_parser_pragma (parser, pragma_stmt, if_p);
5284 break;
5285 default:
5286 expr_stmt:
5287 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5288 expect_semicolon:
5289 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5290 break;
5292 /* Two cases cannot and do not have line numbers associated: If stmt
5293 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5294 cannot hold line numbers. But that's OK because the statement
5295 will either be changed to a MODIFY_EXPR during gimplification of
5296 the statement expr, or discarded. If stmt was compound, but
5297 without new variables, we will have skipped the creation of a
5298 BIND and will have a bare STATEMENT_LIST. But that's OK because
5299 (recursively) all of the component statements should already have
5300 line numbers assigned. ??? Can we discard no-op statements
5301 earlier? */
5302 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5303 protected_set_expr_location (stmt, loc);
5305 parser->in_if_block = in_if_block;
5308 /* Parse the condition from an if, do, while or for statements. */
5310 static tree
5311 c_parser_condition (c_parser *parser)
5313 location_t loc = c_parser_peek_token (parser)->location;
5314 tree cond;
5315 cond = c_parser_expression_conv (parser).value;
5316 cond = c_objc_common_truthvalue_conversion (loc, cond);
5317 cond = c_fully_fold (cond, false, NULL);
5318 if (warn_sequence_point)
5319 verify_sequence_points (cond);
5320 return cond;
5323 /* Parse a parenthesized condition from an if, do or while statement.
5325 condition:
5326 ( expression )
5328 static tree
5329 c_parser_paren_condition (c_parser *parser)
5331 tree cond;
5332 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5333 return error_mark_node;
5334 cond = c_parser_condition (parser);
5335 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5336 return cond;
5339 /* Parse a statement which is a block in C99.
5341 IF_P is used to track whether there's a (possibly labeled) if statement
5342 which is not enclosed in braces and has an else clause. This is used to
5343 implement -Wparentheses. */
5345 static tree
5346 c_parser_c99_block_statement (c_parser *parser, bool *if_p)
5348 tree block = c_begin_compound_stmt (flag_isoc99);
5349 location_t loc = c_parser_peek_token (parser)->location;
5350 c_parser_statement (parser, if_p);
5351 return c_end_compound_stmt (loc, block, flag_isoc99);
5354 /* Parse the body of an if statement. This is just parsing a
5355 statement but (a) it is a block in C99, (b) we track whether the
5356 body is an if statement for the sake of -Wparentheses warnings, (c)
5357 we handle an empty body specially for the sake of -Wempty-body
5358 warnings, and (d) we call parser_compound_statement directly
5359 because c_parser_statement_after_labels resets
5360 parser->in_if_block.
5362 IF_P is used to track whether there's a (possibly labeled) if statement
5363 which is not enclosed in braces and has an else clause. This is used to
5364 implement -Wparentheses. */
5366 static tree
5367 c_parser_if_body (c_parser *parser, bool *if_p,
5368 const token_indent_info &if_tinfo)
5370 tree block = c_begin_compound_stmt (flag_isoc99);
5371 location_t body_loc = c_parser_peek_token (parser)->location;
5372 token_indent_info body_tinfo
5373 = get_token_indent_info (c_parser_peek_token (parser));
5375 c_parser_all_labels (parser);
5376 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5378 location_t loc = c_parser_peek_token (parser)->location;
5379 add_stmt (build_empty_stmt (loc));
5380 c_parser_consume_token (parser);
5381 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5382 warning_at (loc, OPT_Wempty_body,
5383 "suggest braces around empty body in an %<if%> statement");
5385 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5386 add_stmt (c_parser_compound_statement (parser));
5387 else
5388 c_parser_statement_after_labels (parser, if_p);
5390 token_indent_info next_tinfo
5391 = get_token_indent_info (c_parser_peek_token (parser));
5392 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5394 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5397 /* Parse the else body of an if statement. This is just parsing a
5398 statement but (a) it is a block in C99, (b) we handle an empty body
5399 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5400 of if-else-if conditions. */
5402 static tree
5403 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5404 vec<tree> *chain)
5406 location_t body_loc = c_parser_peek_token (parser)->location;
5407 tree block = c_begin_compound_stmt (flag_isoc99);
5408 token_indent_info body_tinfo
5409 = get_token_indent_info (c_parser_peek_token (parser));
5411 c_parser_all_labels (parser);
5412 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5414 location_t loc = c_parser_peek_token (parser)->location;
5415 warning_at (loc,
5416 OPT_Wempty_body,
5417 "suggest braces around empty body in an %<else%> statement");
5418 add_stmt (build_empty_stmt (loc));
5419 c_parser_consume_token (parser);
5421 else
5422 c_parser_statement_after_labels (parser, NULL, chain);
5424 token_indent_info next_tinfo
5425 = get_token_indent_info (c_parser_peek_token (parser));
5426 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5428 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5431 /* We might need to reclassify any previously-lexed identifier, e.g.
5432 when we've left a for loop with an if-statement without else in the
5433 body - we might have used a wrong scope for the token. See PR67784. */
5435 static void
5436 c_parser_maybe_reclassify_token (c_parser *parser)
5438 if (c_parser_next_token_is (parser, CPP_NAME))
5440 c_token *token = c_parser_peek_token (parser);
5442 if (token->id_kind != C_ID_CLASSNAME)
5444 tree decl = lookup_name (token->value);
5446 token->id_kind = C_ID_ID;
5447 if (decl)
5449 if (TREE_CODE (decl) == TYPE_DECL)
5450 token->id_kind = C_ID_TYPENAME;
5452 else if (c_dialect_objc ())
5454 tree objc_interface_decl = objc_is_class_name (token->value);
5455 /* Objective-C class names are in the same namespace as
5456 variables and typedefs, and hence are shadowed by local
5457 declarations. */
5458 if (objc_interface_decl)
5460 token->value = objc_interface_decl;
5461 token->id_kind = C_ID_CLASSNAME;
5468 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5470 if-statement:
5471 if ( expression ) statement
5472 if ( expression ) statement else statement
5474 CHAIN is a vector of if-else-if conditions.
5475 IF_P is used to track whether there's a (possibly labeled) if statement
5476 which is not enclosed in braces and has an else clause. This is used to
5477 implement -Wparentheses. */
5479 static void
5480 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5482 tree block;
5483 location_t loc;
5484 tree cond;
5485 bool nested_if = false;
5486 tree first_body, second_body;
5487 bool in_if_block;
5488 tree if_stmt;
5490 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5491 token_indent_info if_tinfo
5492 = get_token_indent_info (c_parser_peek_token (parser));
5493 c_parser_consume_token (parser);
5494 block = c_begin_compound_stmt (flag_isoc99);
5495 loc = c_parser_peek_token (parser)->location;
5496 cond = c_parser_paren_condition (parser);
5497 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5499 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5500 cond = error_mark_node;
5502 in_if_block = parser->in_if_block;
5503 parser->in_if_block = true;
5504 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5505 parser->in_if_block = in_if_block;
5507 if (warn_duplicated_cond)
5508 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5510 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5512 token_indent_info else_tinfo
5513 = get_token_indent_info (c_parser_peek_token (parser));
5514 c_parser_consume_token (parser);
5515 if (warn_duplicated_cond)
5517 if (c_parser_next_token_is_keyword (parser, RID_IF)
5518 && chain == NULL)
5520 /* We've got "if (COND) else if (COND2)". Start the
5521 condition chain and add COND as the first element. */
5522 chain = new vec<tree> ();
5523 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5524 chain->safe_push (cond);
5526 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5528 /* This is if-else without subsequent if. Zap the condition
5529 chain; we would have already warned at this point. */
5530 delete chain;
5531 chain = NULL;
5534 second_body = c_parser_else_body (parser, else_tinfo, chain);
5535 /* Set IF_P to true to indicate that this if statement has an
5536 else clause. This may trigger the Wparentheses warning
5537 below when we get back up to the parent if statement. */
5538 if (if_p != NULL)
5539 *if_p = true;
5541 else
5543 second_body = NULL_TREE;
5545 /* Diagnose an ambiguous else if if-then-else is nested inside
5546 if-then. */
5547 if (nested_if)
5548 warning_at (loc, OPT_Wdangling_else,
5549 "suggest explicit braces to avoid ambiguous %<else%>");
5551 if (warn_duplicated_cond)
5553 /* This if statement does not have an else clause. We don't
5554 need the condition chain anymore. */
5555 delete chain;
5556 chain = NULL;
5559 c_finish_if_stmt (loc, cond, first_body, second_body);
5560 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5562 /* If the if statement contains array notations, then we expand them. */
5563 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5564 if_stmt = fix_conditional_array_notations (if_stmt);
5565 add_stmt (if_stmt);
5566 c_parser_maybe_reclassify_token (parser);
5569 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5571 switch-statement:
5572 switch (expression) statement
5575 static void
5576 c_parser_switch_statement (c_parser *parser, bool *if_p)
5578 struct c_expr ce;
5579 tree block, expr, body, save_break;
5580 location_t switch_loc = c_parser_peek_token (parser)->location;
5581 location_t switch_cond_loc;
5582 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5583 c_parser_consume_token (parser);
5584 block = c_begin_compound_stmt (flag_isoc99);
5585 bool explicit_cast_p = false;
5586 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5588 switch_cond_loc = c_parser_peek_token (parser)->location;
5589 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5590 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5591 explicit_cast_p = true;
5592 ce = c_parser_expression (parser);
5593 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5594 expr = ce.value;
5595 /* ??? expr has no valid location? */
5596 if (check_no_cilk (expr,
5597 "Cilk array notation cannot be used as a condition for switch statement",
5598 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5599 switch_cond_loc))
5600 expr = error_mark_node;
5601 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5603 else
5605 switch_cond_loc = UNKNOWN_LOCATION;
5606 expr = error_mark_node;
5608 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5609 save_break = c_break_label;
5610 c_break_label = NULL_TREE;
5611 body = c_parser_c99_block_statement (parser, if_p);
5612 c_finish_case (body, ce.original_type);
5613 if (c_break_label)
5615 location_t here = c_parser_peek_token (parser)->location;
5616 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5617 SET_EXPR_LOCATION (t, here);
5618 add_stmt (t);
5620 c_break_label = save_break;
5621 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5622 c_parser_maybe_reclassify_token (parser);
5625 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5627 while-statement:
5628 while (expression) statement
5630 IF_P is used to track whether there's a (possibly labeled) if statement
5631 which is not enclosed in braces and has an else clause. This is used to
5632 implement -Wparentheses. */
5634 static void
5635 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5637 tree block, cond, body, save_break, save_cont;
5638 location_t loc;
5639 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5640 token_indent_info while_tinfo
5641 = get_token_indent_info (c_parser_peek_token (parser));
5642 c_parser_consume_token (parser);
5643 block = c_begin_compound_stmt (flag_isoc99);
5644 loc = c_parser_peek_token (parser)->location;
5645 cond = c_parser_paren_condition (parser);
5646 if (check_no_cilk (cond,
5647 "Cilk array notation cannot be used as a condition for while statement",
5648 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5649 cond = error_mark_node;
5650 if (ivdep && cond != error_mark_node)
5651 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5652 build_int_cst (integer_type_node,
5653 annot_expr_ivdep_kind));
5654 save_break = c_break_label;
5655 c_break_label = NULL_TREE;
5656 save_cont = c_cont_label;
5657 c_cont_label = NULL_TREE;
5659 token_indent_info body_tinfo
5660 = get_token_indent_info (c_parser_peek_token (parser));
5662 body = c_parser_c99_block_statement (parser, if_p);
5663 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5664 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5665 c_parser_maybe_reclassify_token (parser);
5667 token_indent_info next_tinfo
5668 = get_token_indent_info (c_parser_peek_token (parser));
5669 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5671 c_break_label = save_break;
5672 c_cont_label = save_cont;
5675 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5677 do-statement:
5678 do statement while ( expression ) ;
5681 static void
5682 c_parser_do_statement (c_parser *parser, bool ivdep)
5684 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5685 location_t loc;
5686 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5687 c_parser_consume_token (parser);
5688 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5689 warning_at (c_parser_peek_token (parser)->location,
5690 OPT_Wempty_body,
5691 "suggest braces around empty body in %<do%> statement");
5692 block = c_begin_compound_stmt (flag_isoc99);
5693 loc = c_parser_peek_token (parser)->location;
5694 save_break = c_break_label;
5695 c_break_label = NULL_TREE;
5696 save_cont = c_cont_label;
5697 c_cont_label = NULL_TREE;
5698 body = c_parser_c99_block_statement (parser, NULL);
5699 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5700 new_break = c_break_label;
5701 c_break_label = save_break;
5702 new_cont = c_cont_label;
5703 c_cont_label = save_cont;
5704 cond = c_parser_paren_condition (parser);
5705 if (check_no_cilk (cond,
5706 "Cilk array notation cannot be used as a condition for a do-while statement",
5707 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5708 cond = error_mark_node;
5709 if (ivdep && cond != error_mark_node)
5710 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5711 build_int_cst (integer_type_node,
5712 annot_expr_ivdep_kind));
5713 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5714 c_parser_skip_to_end_of_block_or_statement (parser);
5715 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5716 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5719 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5721 for-statement:
5722 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5723 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5725 The form with a declaration is new in C99.
5727 ??? In accordance with the old parser, the declaration may be a
5728 nested function, which is then rejected in check_for_loop_decls,
5729 but does it make any sense for this to be included in the grammar?
5730 Note in particular that the nested function does not include a
5731 trailing ';', whereas the "declaration" production includes one.
5732 Also, can we reject bad declarations earlier and cheaper than
5733 check_for_loop_decls?
5735 In Objective-C, there are two additional variants:
5737 foreach-statement:
5738 for ( expression in expresssion ) statement
5739 for ( declaration in expression ) statement
5741 This is inconsistent with C, because the second variant is allowed
5742 even if c99 is not enabled.
5744 The rest of the comment documents these Objective-C foreach-statement.
5746 Here is the canonical example of the first variant:
5747 for (object in array) { do something with object }
5748 we call the first expression ("object") the "object_expression" and
5749 the second expression ("array") the "collection_expression".
5750 object_expression must be an lvalue of type "id" (a generic Objective-C
5751 object) because the loop works by assigning to object_expression the
5752 various objects from the collection_expression. collection_expression
5753 must evaluate to something of type "id" which responds to the method
5754 countByEnumeratingWithState:objects:count:.
5756 The canonical example of the second variant is:
5757 for (id object in array) { do something with object }
5758 which is completely equivalent to
5760 id object;
5761 for (object in array) { do something with object }
5763 Note that initizializing 'object' in some way (eg, "for ((object =
5764 xxx) in array) { do something with object }") is possibly
5765 technically valid, but completely pointless as 'object' will be
5766 assigned to something else as soon as the loop starts. We should
5767 most likely reject it (TODO).
5769 The beginning of the Objective-C foreach-statement looks exactly
5770 like the beginning of the for-statement, and we can tell it is a
5771 foreach-statement only because the initial declaration or
5772 expression is terminated by 'in' instead of ';'.
5774 IF_P is used to track whether there's a (possibly labeled) if statement
5775 which is not enclosed in braces and has an else clause. This is used to
5776 implement -Wparentheses. */
5778 static void
5779 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
5781 tree block, cond, incr, save_break, save_cont, body;
5782 /* The following are only used when parsing an ObjC foreach statement. */
5783 tree object_expression;
5784 /* Silence the bogus uninitialized warning. */
5785 tree collection_expression = NULL;
5786 location_t loc = c_parser_peek_token (parser)->location;
5787 location_t for_loc = c_parser_peek_token (parser)->location;
5788 bool is_foreach_statement = false;
5789 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5790 token_indent_info for_tinfo
5791 = get_token_indent_info (c_parser_peek_token (parser));
5792 c_parser_consume_token (parser);
5793 /* Open a compound statement in Objective-C as well, just in case this is
5794 as foreach expression. */
5795 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5796 cond = error_mark_node;
5797 incr = error_mark_node;
5798 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5800 /* Parse the initialization declaration or expression. */
5801 object_expression = error_mark_node;
5802 parser->objc_could_be_foreach_context = c_dialect_objc ();
5803 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5805 parser->objc_could_be_foreach_context = false;
5806 c_parser_consume_token (parser);
5807 c_finish_expr_stmt (loc, NULL_TREE);
5809 else if (c_parser_next_tokens_start_declaration (parser))
5811 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5812 &object_expression, vNULL);
5813 parser->objc_could_be_foreach_context = false;
5815 if (c_parser_next_token_is_keyword (parser, RID_IN))
5817 c_parser_consume_token (parser);
5818 is_foreach_statement = true;
5819 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5820 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5822 else
5823 check_for_loop_decls (for_loc, flag_isoc99);
5825 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5827 /* __extension__ can start a declaration, but is also an
5828 unary operator that can start an expression. Consume all
5829 but the last of a possible series of __extension__ to
5830 determine which. */
5831 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5832 && (c_parser_peek_2nd_token (parser)->keyword
5833 == RID_EXTENSION))
5834 c_parser_consume_token (parser);
5835 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5837 int ext;
5838 ext = disable_extension_diagnostics ();
5839 c_parser_consume_token (parser);
5840 c_parser_declaration_or_fndef (parser, true, true, true, true,
5841 true, &object_expression, vNULL);
5842 parser->objc_could_be_foreach_context = false;
5844 restore_extension_diagnostics (ext);
5845 if (c_parser_next_token_is_keyword (parser, RID_IN))
5847 c_parser_consume_token (parser);
5848 is_foreach_statement = true;
5849 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5850 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5852 else
5853 check_for_loop_decls (for_loc, flag_isoc99);
5855 else
5856 goto init_expr;
5858 else
5860 init_expr:
5862 struct c_expr ce;
5863 tree init_expression;
5864 ce = c_parser_expression (parser);
5865 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5866 level statement", but it works just fine, so allow it. */
5867 init_expression = ce.value;
5868 parser->objc_could_be_foreach_context = false;
5869 if (c_parser_next_token_is_keyword (parser, RID_IN))
5871 c_parser_consume_token (parser);
5872 is_foreach_statement = true;
5873 if (! lvalue_p (init_expression))
5874 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5875 object_expression = c_fully_fold (init_expression, false, NULL);
5877 else
5879 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5880 init_expression = ce.value;
5881 c_finish_expr_stmt (loc, init_expression);
5882 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5886 /* Parse the loop condition. In the case of a foreach
5887 statement, there is no loop condition. */
5888 gcc_assert (!parser->objc_could_be_foreach_context);
5889 if (!is_foreach_statement)
5891 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5893 if (ivdep)
5895 c_parser_error (parser, "missing loop condition in loop with "
5896 "%<GCC ivdep%> pragma");
5897 cond = error_mark_node;
5899 else
5901 c_parser_consume_token (parser);
5902 cond = NULL_TREE;
5905 else
5907 cond = c_parser_condition (parser);
5908 if (check_no_cilk (cond,
5909 "Cilk array notation cannot be used in a condition for a for-loop",
5910 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5911 cond = error_mark_node;
5912 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5913 "expected %<;%>");
5915 if (ivdep && cond != error_mark_node)
5916 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5917 build_int_cst (integer_type_node,
5918 annot_expr_ivdep_kind));
5920 /* Parse the increment expression (the third expression in a
5921 for-statement). In the case of a foreach-statement, this is
5922 the expression that follows the 'in'. */
5923 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5925 if (is_foreach_statement)
5927 c_parser_error (parser, "missing collection in fast enumeration");
5928 collection_expression = error_mark_node;
5930 else
5931 incr = c_process_expr_stmt (loc, NULL_TREE);
5933 else
5935 if (is_foreach_statement)
5936 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5937 false, NULL);
5938 else
5940 struct c_expr ce = c_parser_expression (parser);
5941 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5942 incr = c_process_expr_stmt (loc, ce.value);
5945 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5947 save_break = c_break_label;
5948 c_break_label = NULL_TREE;
5949 save_cont = c_cont_label;
5950 c_cont_label = NULL_TREE;
5952 token_indent_info body_tinfo
5953 = get_token_indent_info (c_parser_peek_token (parser));
5955 body = c_parser_c99_block_statement (parser, if_p);
5957 if (is_foreach_statement)
5958 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5959 else
5960 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5961 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5962 c_parser_maybe_reclassify_token (parser);
5964 token_indent_info next_tinfo
5965 = get_token_indent_info (c_parser_peek_token (parser));
5966 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
5968 c_break_label = save_break;
5969 c_cont_label = save_cont;
5972 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5973 statement with inputs, outputs, clobbers, and volatile tag
5974 allowed.
5976 asm-statement:
5977 asm type-qualifier[opt] ( asm-argument ) ;
5978 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5980 asm-argument:
5981 asm-string-literal
5982 asm-string-literal : asm-operands[opt]
5983 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5984 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5986 asm-goto-argument:
5987 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5988 : asm-goto-operands
5990 Qualifiers other than volatile are accepted in the syntax but
5991 warned for. */
5993 static tree
5994 c_parser_asm_statement (c_parser *parser)
5996 tree quals, str, outputs, inputs, clobbers, labels, ret;
5997 bool simple, is_goto;
5998 location_t asm_loc = c_parser_peek_token (parser)->location;
5999 int section, nsections;
6001 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6002 c_parser_consume_token (parser);
6003 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6005 quals = c_parser_peek_token (parser)->value;
6006 c_parser_consume_token (parser);
6008 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6009 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6011 warning_at (c_parser_peek_token (parser)->location,
6013 "%E qualifier ignored on asm",
6014 c_parser_peek_token (parser)->value);
6015 quals = NULL_TREE;
6016 c_parser_consume_token (parser);
6018 else
6019 quals = NULL_TREE;
6021 is_goto = false;
6022 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6024 c_parser_consume_token (parser);
6025 is_goto = true;
6028 /* ??? Follow the C++ parser rather than using the
6029 lex_untranslated_string kludge. */
6030 parser->lex_untranslated_string = true;
6031 ret = NULL;
6033 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6034 goto error;
6036 str = c_parser_asm_string_literal (parser);
6037 if (str == NULL_TREE)
6038 goto error_close_paren;
6040 simple = true;
6041 outputs = NULL_TREE;
6042 inputs = NULL_TREE;
6043 clobbers = NULL_TREE;
6044 labels = NULL_TREE;
6046 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6047 goto done_asm;
6049 /* Parse each colon-delimited section of operands. */
6050 nsections = 3 + is_goto;
6051 for (section = 0; section < nsections; ++section)
6053 if (!c_parser_require (parser, CPP_COLON,
6054 is_goto
6055 ? "expected %<:%>"
6056 : "expected %<:%> or %<)%>"))
6057 goto error_close_paren;
6059 /* Once past any colon, we're no longer a simple asm. */
6060 simple = false;
6062 if ((!c_parser_next_token_is (parser, CPP_COLON)
6063 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6064 || section == 3)
6065 switch (section)
6067 case 0:
6068 /* For asm goto, we don't allow output operands, but reserve
6069 the slot for a future extension that does allow them. */
6070 if (!is_goto)
6071 outputs = c_parser_asm_operands (parser);
6072 break;
6073 case 1:
6074 inputs = c_parser_asm_operands (parser);
6075 break;
6076 case 2:
6077 clobbers = c_parser_asm_clobbers (parser);
6078 break;
6079 case 3:
6080 labels = c_parser_asm_goto_operands (parser);
6081 break;
6082 default:
6083 gcc_unreachable ();
6086 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6087 goto done_asm;
6090 done_asm:
6091 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6093 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6094 goto error;
6097 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6098 c_parser_skip_to_end_of_block_or_statement (parser);
6100 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6101 clobbers, labels, simple));
6103 error:
6104 parser->lex_untranslated_string = false;
6105 return ret;
6107 error_close_paren:
6108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6109 goto error;
6112 /* Parse asm operands, a GNU extension.
6114 asm-operands:
6115 asm-operand
6116 asm-operands , asm-operand
6118 asm-operand:
6119 asm-string-literal ( expression )
6120 [ identifier ] asm-string-literal ( expression )
6123 static tree
6124 c_parser_asm_operands (c_parser *parser)
6126 tree list = NULL_TREE;
6127 while (true)
6129 tree name, str;
6130 struct c_expr expr;
6131 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6133 c_parser_consume_token (parser);
6134 if (c_parser_next_token_is (parser, CPP_NAME))
6136 tree id = c_parser_peek_token (parser)->value;
6137 c_parser_consume_token (parser);
6138 name = build_string (IDENTIFIER_LENGTH (id),
6139 IDENTIFIER_POINTER (id));
6141 else
6143 c_parser_error (parser, "expected identifier");
6144 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6145 return NULL_TREE;
6147 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6148 "expected %<]%>");
6150 else
6151 name = NULL_TREE;
6152 str = c_parser_asm_string_literal (parser);
6153 if (str == NULL_TREE)
6154 return NULL_TREE;
6155 parser->lex_untranslated_string = false;
6156 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6158 parser->lex_untranslated_string = true;
6159 return NULL_TREE;
6161 expr = c_parser_expression (parser);
6162 mark_exp_read (expr.value);
6163 parser->lex_untranslated_string = true;
6164 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6166 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6167 return NULL_TREE;
6169 list = chainon (list, build_tree_list (build_tree_list (name, str),
6170 expr.value));
6171 if (c_parser_next_token_is (parser, CPP_COMMA))
6172 c_parser_consume_token (parser);
6173 else
6174 break;
6176 return list;
6179 /* Parse asm clobbers, a GNU extension.
6181 asm-clobbers:
6182 asm-string-literal
6183 asm-clobbers , asm-string-literal
6186 static tree
6187 c_parser_asm_clobbers (c_parser *parser)
6189 tree list = NULL_TREE;
6190 while (true)
6192 tree str = c_parser_asm_string_literal (parser);
6193 if (str)
6194 list = tree_cons (NULL_TREE, str, list);
6195 else
6196 return NULL_TREE;
6197 if (c_parser_next_token_is (parser, CPP_COMMA))
6198 c_parser_consume_token (parser);
6199 else
6200 break;
6202 return list;
6205 /* Parse asm goto labels, a GNU extension.
6207 asm-goto-operands:
6208 identifier
6209 asm-goto-operands , identifier
6212 static tree
6213 c_parser_asm_goto_operands (c_parser *parser)
6215 tree list = NULL_TREE;
6216 while (true)
6218 tree name, label;
6220 if (c_parser_next_token_is (parser, CPP_NAME))
6222 c_token *tok = c_parser_peek_token (parser);
6223 name = tok->value;
6224 label = lookup_label_for_goto (tok->location, name);
6225 c_parser_consume_token (parser);
6226 TREE_USED (label) = 1;
6228 else
6230 c_parser_error (parser, "expected identifier");
6231 return NULL_TREE;
6234 name = build_string (IDENTIFIER_LENGTH (name),
6235 IDENTIFIER_POINTER (name));
6236 list = tree_cons (name, label, list);
6237 if (c_parser_next_token_is (parser, CPP_COMMA))
6238 c_parser_consume_token (parser);
6239 else
6240 return nreverse (list);
6244 /* Parse an expression other than a compound expression; that is, an
6245 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6246 NULL then it is an Objective-C message expression which is the
6247 primary-expression starting the expression as an initializer.
6249 assignment-expression:
6250 conditional-expression
6251 unary-expression assignment-operator assignment-expression
6253 assignment-operator: one of
6254 = *= /= %= += -= <<= >>= &= ^= |=
6256 In GNU C we accept any conditional expression on the LHS and
6257 diagnose the invalid lvalue rather than producing a syntax
6258 error. */
6260 static struct c_expr
6261 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6262 tree omp_atomic_lhs)
6264 struct c_expr lhs, rhs, ret;
6265 enum tree_code code;
6266 location_t op_location, exp_location;
6267 gcc_assert (!after || c_dialect_objc ());
6268 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6269 op_location = c_parser_peek_token (parser)->location;
6270 switch (c_parser_peek_token (parser)->type)
6272 case CPP_EQ:
6273 code = NOP_EXPR;
6274 break;
6275 case CPP_MULT_EQ:
6276 code = MULT_EXPR;
6277 break;
6278 case CPP_DIV_EQ:
6279 code = TRUNC_DIV_EXPR;
6280 break;
6281 case CPP_MOD_EQ:
6282 code = TRUNC_MOD_EXPR;
6283 break;
6284 case CPP_PLUS_EQ:
6285 code = PLUS_EXPR;
6286 break;
6287 case CPP_MINUS_EQ:
6288 code = MINUS_EXPR;
6289 break;
6290 case CPP_LSHIFT_EQ:
6291 code = LSHIFT_EXPR;
6292 break;
6293 case CPP_RSHIFT_EQ:
6294 code = RSHIFT_EXPR;
6295 break;
6296 case CPP_AND_EQ:
6297 code = BIT_AND_EXPR;
6298 break;
6299 case CPP_XOR_EQ:
6300 code = BIT_XOR_EXPR;
6301 break;
6302 case CPP_OR_EQ:
6303 code = BIT_IOR_EXPR;
6304 break;
6305 default:
6306 return lhs;
6308 c_parser_consume_token (parser);
6309 exp_location = c_parser_peek_token (parser)->location;
6310 rhs = c_parser_expr_no_commas (parser, NULL);
6311 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6313 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6314 code, exp_location, rhs.value,
6315 rhs.original_type);
6316 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6317 if (code == NOP_EXPR)
6318 ret.original_code = MODIFY_EXPR;
6319 else
6321 TREE_NO_WARNING (ret.value) = 1;
6322 ret.original_code = ERROR_MARK;
6324 ret.original_type = NULL;
6325 return ret;
6328 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6329 is not NULL then it is an Objective-C message expression which is
6330 the primary-expression starting the expression as an initializer.
6332 conditional-expression:
6333 logical-OR-expression
6334 logical-OR-expression ? expression : conditional-expression
6336 GNU extensions:
6338 conditional-expression:
6339 logical-OR-expression ? : conditional-expression
6342 static struct c_expr
6343 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6344 tree omp_atomic_lhs)
6346 struct c_expr cond, exp1, exp2, ret;
6347 location_t start, cond_loc, colon_loc, middle_loc;
6349 gcc_assert (!after || c_dialect_objc ());
6351 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6353 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6354 return cond;
6355 if (cond.value != error_mark_node)
6356 start = cond.get_start ();
6357 else
6358 start = UNKNOWN_LOCATION;
6359 cond_loc = c_parser_peek_token (parser)->location;
6360 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6361 c_parser_consume_token (parser);
6362 if (c_parser_next_token_is (parser, CPP_COLON))
6364 tree eptype = NULL_TREE;
6366 middle_loc = c_parser_peek_token (parser)->location;
6367 pedwarn (middle_loc, OPT_Wpedantic,
6368 "ISO C forbids omitting the middle term of a ?: expression");
6369 warn_for_omitted_condop (middle_loc, cond.value);
6370 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6372 eptype = TREE_TYPE (cond.value);
6373 cond.value = TREE_OPERAND (cond.value, 0);
6375 /* Make sure first operand is calculated only once. */
6376 exp1.value = c_save_expr (default_conversion (cond.value));
6377 if (eptype)
6378 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6379 exp1.original_type = NULL;
6380 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6381 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6383 else
6385 cond.value
6386 = c_objc_common_truthvalue_conversion
6387 (cond_loc, default_conversion (cond.value));
6388 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6389 exp1 = c_parser_expression_conv (parser);
6390 mark_exp_read (exp1.value);
6391 c_inhibit_evaluation_warnings +=
6392 ((cond.value == truthvalue_true_node)
6393 - (cond.value == truthvalue_false_node));
6396 colon_loc = c_parser_peek_token (parser)->location;
6397 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6399 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6400 ret.value = error_mark_node;
6401 ret.original_code = ERROR_MARK;
6402 ret.original_type = NULL;
6403 return ret;
6406 location_t exp2_loc = c_parser_peek_token (parser)->location;
6407 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6408 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6410 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6411 ret.value = build_conditional_expr (colon_loc, cond.value,
6412 cond.original_code == C_MAYBE_CONST_EXPR,
6413 exp1.value, exp1.original_type,
6414 exp2.value, exp2.original_type);
6415 ret.original_code = ERROR_MARK;
6416 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6417 ret.original_type = NULL;
6418 else
6420 tree t1, t2;
6422 /* If both sides are enum type, the default conversion will have
6423 made the type of the result be an integer type. We want to
6424 remember the enum types we started with. */
6425 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6426 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6427 ret.original_type = ((t1 != error_mark_node
6428 && t2 != error_mark_node
6429 && (TYPE_MAIN_VARIANT (t1)
6430 == TYPE_MAIN_VARIANT (t2)))
6431 ? t1
6432 : NULL);
6434 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6435 return ret;
6438 /* Parse a binary expression; that is, a logical-OR-expression (C90
6439 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6440 an Objective-C message expression which is the primary-expression
6441 starting the expression as an initializer.
6443 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6444 when it should be the unfolded lhs. In a valid OpenMP source,
6445 one of the operands of the toplevel binary expression must be equal
6446 to it. In that case, just return a build2 created binary operation
6447 rather than result of parser_build_binary_op.
6449 multiplicative-expression:
6450 cast-expression
6451 multiplicative-expression * cast-expression
6452 multiplicative-expression / cast-expression
6453 multiplicative-expression % cast-expression
6455 additive-expression:
6456 multiplicative-expression
6457 additive-expression + multiplicative-expression
6458 additive-expression - multiplicative-expression
6460 shift-expression:
6461 additive-expression
6462 shift-expression << additive-expression
6463 shift-expression >> additive-expression
6465 relational-expression:
6466 shift-expression
6467 relational-expression < shift-expression
6468 relational-expression > shift-expression
6469 relational-expression <= shift-expression
6470 relational-expression >= shift-expression
6472 equality-expression:
6473 relational-expression
6474 equality-expression == relational-expression
6475 equality-expression != relational-expression
6477 AND-expression:
6478 equality-expression
6479 AND-expression & equality-expression
6481 exclusive-OR-expression:
6482 AND-expression
6483 exclusive-OR-expression ^ AND-expression
6485 inclusive-OR-expression:
6486 exclusive-OR-expression
6487 inclusive-OR-expression | exclusive-OR-expression
6489 logical-AND-expression:
6490 inclusive-OR-expression
6491 logical-AND-expression && inclusive-OR-expression
6493 logical-OR-expression:
6494 logical-AND-expression
6495 logical-OR-expression || logical-AND-expression
6498 static struct c_expr
6499 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6500 tree omp_atomic_lhs)
6502 /* A binary expression is parsed using operator-precedence parsing,
6503 with the operands being cast expressions. All the binary
6504 operators are left-associative. Thus a binary expression is of
6505 form:
6507 E0 op1 E1 op2 E2 ...
6509 which we represent on a stack. On the stack, the precedence
6510 levels are strictly increasing. When a new operator is
6511 encountered of higher precedence than that at the top of the
6512 stack, it is pushed; its LHS is the top expression, and its RHS
6513 is everything parsed until it is popped. When a new operator is
6514 encountered with precedence less than or equal to that at the top
6515 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6516 by the result of the operation until the operator at the top of
6517 the stack has lower precedence than the new operator or there is
6518 only one element on the stack; then the top expression is the LHS
6519 of the new operator. In the case of logical AND and OR
6520 expressions, we also need to adjust c_inhibit_evaluation_warnings
6521 as appropriate when the operators are pushed and popped. */
6523 struct {
6524 /* The expression at this stack level. */
6525 struct c_expr expr;
6526 /* The precedence of the operator on its left, PREC_NONE at the
6527 bottom of the stack. */
6528 enum c_parser_prec prec;
6529 /* The operation on its left. */
6530 enum tree_code op;
6531 /* The source location of this operation. */
6532 location_t loc;
6533 } stack[NUM_PRECS];
6534 int sp;
6535 /* Location of the binary operator. */
6536 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6537 #define POP \
6538 do { \
6539 switch (stack[sp].op) \
6541 case TRUTH_ANDIF_EXPR: \
6542 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6543 == truthvalue_false_node); \
6544 break; \
6545 case TRUTH_ORIF_EXPR: \
6546 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6547 == truthvalue_true_node); \
6548 break; \
6549 default: \
6550 break; \
6552 stack[sp - 1].expr \
6553 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6554 stack[sp - 1].expr, true, true); \
6555 stack[sp].expr \
6556 = convert_lvalue_to_rvalue (stack[sp].loc, \
6557 stack[sp].expr, true, true); \
6558 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6559 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6560 && ((1 << stack[sp].prec) \
6561 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6562 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6563 && stack[sp].op != TRUNC_MOD_EXPR \
6564 && stack[0].expr.value != error_mark_node \
6565 && stack[1].expr.value != error_mark_node \
6566 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6567 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6568 stack[0].expr.value \
6569 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6570 stack[0].expr.value, stack[1].expr.value); \
6571 else \
6572 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6573 stack[sp].op, \
6574 stack[sp - 1].expr, \
6575 stack[sp].expr); \
6576 sp--; \
6577 } while (0)
6578 gcc_assert (!after || c_dialect_objc ());
6579 stack[0].loc = c_parser_peek_token (parser)->location;
6580 stack[0].expr = c_parser_cast_expression (parser, after);
6581 stack[0].prec = PREC_NONE;
6582 sp = 0;
6583 while (true)
6585 enum c_parser_prec oprec;
6586 enum tree_code ocode;
6587 source_range src_range;
6588 if (parser->error)
6589 goto out;
6590 switch (c_parser_peek_token (parser)->type)
6592 case CPP_MULT:
6593 oprec = PREC_MULT;
6594 ocode = MULT_EXPR;
6595 break;
6596 case CPP_DIV:
6597 oprec = PREC_MULT;
6598 ocode = TRUNC_DIV_EXPR;
6599 break;
6600 case CPP_MOD:
6601 oprec = PREC_MULT;
6602 ocode = TRUNC_MOD_EXPR;
6603 break;
6604 case CPP_PLUS:
6605 oprec = PREC_ADD;
6606 ocode = PLUS_EXPR;
6607 break;
6608 case CPP_MINUS:
6609 oprec = PREC_ADD;
6610 ocode = MINUS_EXPR;
6611 break;
6612 case CPP_LSHIFT:
6613 oprec = PREC_SHIFT;
6614 ocode = LSHIFT_EXPR;
6615 break;
6616 case CPP_RSHIFT:
6617 oprec = PREC_SHIFT;
6618 ocode = RSHIFT_EXPR;
6619 break;
6620 case CPP_LESS:
6621 oprec = PREC_REL;
6622 ocode = LT_EXPR;
6623 break;
6624 case CPP_GREATER:
6625 oprec = PREC_REL;
6626 ocode = GT_EXPR;
6627 break;
6628 case CPP_LESS_EQ:
6629 oprec = PREC_REL;
6630 ocode = LE_EXPR;
6631 break;
6632 case CPP_GREATER_EQ:
6633 oprec = PREC_REL;
6634 ocode = GE_EXPR;
6635 break;
6636 case CPP_EQ_EQ:
6637 oprec = PREC_EQ;
6638 ocode = EQ_EXPR;
6639 break;
6640 case CPP_NOT_EQ:
6641 oprec = PREC_EQ;
6642 ocode = NE_EXPR;
6643 break;
6644 case CPP_AND:
6645 oprec = PREC_BITAND;
6646 ocode = BIT_AND_EXPR;
6647 break;
6648 case CPP_XOR:
6649 oprec = PREC_BITXOR;
6650 ocode = BIT_XOR_EXPR;
6651 break;
6652 case CPP_OR:
6653 oprec = PREC_BITOR;
6654 ocode = BIT_IOR_EXPR;
6655 break;
6656 case CPP_AND_AND:
6657 oprec = PREC_LOGAND;
6658 ocode = TRUTH_ANDIF_EXPR;
6659 break;
6660 case CPP_OR_OR:
6661 oprec = PREC_LOGOR;
6662 ocode = TRUTH_ORIF_EXPR;
6663 break;
6664 default:
6665 /* Not a binary operator, so end of the binary
6666 expression. */
6667 goto out;
6669 binary_loc = c_parser_peek_token (parser)->location;
6670 while (oprec <= stack[sp].prec)
6671 POP;
6672 c_parser_consume_token (parser);
6673 switch (ocode)
6675 case TRUTH_ANDIF_EXPR:
6676 src_range = stack[sp].expr.src_range;
6677 stack[sp].expr
6678 = convert_lvalue_to_rvalue (stack[sp].loc,
6679 stack[sp].expr, true, true);
6680 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6681 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6682 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6683 == truthvalue_false_node);
6684 set_c_expr_source_range (&stack[sp].expr, src_range);
6685 break;
6686 case TRUTH_ORIF_EXPR:
6687 src_range = stack[sp].expr.src_range;
6688 stack[sp].expr
6689 = convert_lvalue_to_rvalue (stack[sp].loc,
6690 stack[sp].expr, true, true);
6691 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6692 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6693 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6694 == truthvalue_true_node);
6695 set_c_expr_source_range (&stack[sp].expr, src_range);
6696 break;
6697 default:
6698 break;
6700 sp++;
6701 stack[sp].loc = binary_loc;
6702 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6703 stack[sp].prec = oprec;
6704 stack[sp].op = ocode;
6706 out:
6707 while (sp > 0)
6708 POP;
6709 return stack[0].expr;
6710 #undef POP
6713 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6714 NULL then it is an Objective-C message expression which is the
6715 primary-expression starting the expression as an initializer.
6717 cast-expression:
6718 unary-expression
6719 ( type-name ) unary-expression
6722 static struct c_expr
6723 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6725 location_t cast_loc = c_parser_peek_token (parser)->location;
6726 gcc_assert (!after || c_dialect_objc ());
6727 if (after)
6728 return c_parser_postfix_expression_after_primary (parser,
6729 cast_loc, *after);
6730 /* If the expression begins with a parenthesized type name, it may
6731 be either a cast or a compound literal; we need to see whether
6732 the next character is '{' to tell the difference. If not, it is
6733 an unary expression. Full detection of unknown typenames here
6734 would require a 3-token lookahead. */
6735 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6736 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6738 struct c_type_name *type_name;
6739 struct c_expr ret;
6740 struct c_expr expr;
6741 c_parser_consume_token (parser);
6742 type_name = c_parser_type_name (parser);
6743 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6744 if (type_name == NULL)
6746 ret.value = error_mark_node;
6747 ret.original_code = ERROR_MARK;
6748 ret.original_type = NULL;
6749 return ret;
6752 /* Save casted types in the function's used types hash table. */
6753 used_types_insert (type_name->specs->type);
6755 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6756 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6757 cast_loc);
6759 location_t expr_loc = c_parser_peek_token (parser)->location;
6760 expr = c_parser_cast_expression (parser, NULL);
6761 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6763 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6764 if (ret.value && expr.value)
6765 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6766 ret.original_code = ERROR_MARK;
6767 ret.original_type = NULL;
6768 return ret;
6770 else
6771 return c_parser_unary_expression (parser);
6774 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6776 unary-expression:
6777 postfix-expression
6778 ++ unary-expression
6779 -- unary-expression
6780 unary-operator cast-expression
6781 sizeof unary-expression
6782 sizeof ( type-name )
6784 unary-operator: one of
6785 & * + - ~ !
6787 GNU extensions:
6789 unary-expression:
6790 __alignof__ unary-expression
6791 __alignof__ ( type-name )
6792 && identifier
6794 (C11 permits _Alignof with type names only.)
6796 unary-operator: one of
6797 __extension__ __real__ __imag__
6799 Transactional Memory:
6801 unary-expression:
6802 transaction-expression
6804 In addition, the GNU syntax treats ++ and -- as unary operators, so
6805 they may be applied to cast expressions with errors for non-lvalues
6806 given later. */
6808 static struct c_expr
6809 c_parser_unary_expression (c_parser *parser)
6811 int ext;
6812 struct c_expr ret, op;
6813 location_t op_loc = c_parser_peek_token (parser)->location;
6814 location_t exp_loc;
6815 location_t finish;
6816 ret.original_code = ERROR_MARK;
6817 ret.original_type = NULL;
6818 switch (c_parser_peek_token (parser)->type)
6820 case CPP_PLUS_PLUS:
6821 c_parser_consume_token (parser);
6822 exp_loc = c_parser_peek_token (parser)->location;
6823 op = c_parser_cast_expression (parser, NULL);
6825 /* If there is array notations in op, we expand them. */
6826 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6827 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6828 else
6830 op = default_function_array_read_conversion (exp_loc, op);
6831 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6833 case CPP_MINUS_MINUS:
6834 c_parser_consume_token (parser);
6835 exp_loc = c_parser_peek_token (parser)->location;
6836 op = c_parser_cast_expression (parser, NULL);
6838 /* If there is array notations in op, we expand them. */
6839 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6840 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6841 else
6843 op = default_function_array_read_conversion (exp_loc, op);
6844 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6846 case CPP_AND:
6847 c_parser_consume_token (parser);
6848 op = c_parser_cast_expression (parser, NULL);
6849 mark_exp_read (op.value);
6850 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6851 case CPP_MULT:
6853 c_parser_consume_token (parser);
6854 exp_loc = c_parser_peek_token (parser)->location;
6855 op = c_parser_cast_expression (parser, NULL);
6856 finish = op.get_finish ();
6857 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6858 location_t combined_loc = make_location (op_loc, op_loc, finish);
6859 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
6860 ret.src_range.m_start = op_loc;
6861 ret.src_range.m_finish = finish;
6862 return ret;
6864 case CPP_PLUS:
6865 if (!c_dialect_objc () && !in_system_header_at (input_location))
6866 warning_at (op_loc,
6867 OPT_Wtraditional,
6868 "traditional C rejects the unary plus operator");
6869 c_parser_consume_token (parser);
6870 exp_loc = c_parser_peek_token (parser)->location;
6871 op = c_parser_cast_expression (parser, NULL);
6872 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6873 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6874 case CPP_MINUS:
6875 c_parser_consume_token (parser);
6876 exp_loc = c_parser_peek_token (parser)->location;
6877 op = c_parser_cast_expression (parser, NULL);
6878 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6879 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6880 case CPP_COMPL:
6881 c_parser_consume_token (parser);
6882 exp_loc = c_parser_peek_token (parser)->location;
6883 op = c_parser_cast_expression (parser, NULL);
6884 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6885 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6886 case CPP_NOT:
6887 c_parser_consume_token (parser);
6888 exp_loc = c_parser_peek_token (parser)->location;
6889 op = c_parser_cast_expression (parser, NULL);
6890 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6891 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6892 case CPP_AND_AND:
6893 /* Refer to the address of a label as a pointer. */
6894 c_parser_consume_token (parser);
6895 if (c_parser_next_token_is (parser, CPP_NAME))
6897 ret.value = finish_label_address_expr
6898 (c_parser_peek_token (parser)->value, op_loc);
6899 set_c_expr_source_range (&ret, op_loc,
6900 c_parser_peek_token (parser)->get_finish ());
6901 c_parser_consume_token (parser);
6903 else
6905 c_parser_error (parser, "expected identifier");
6906 ret.value = error_mark_node;
6908 return ret;
6909 case CPP_KEYWORD:
6910 switch (c_parser_peek_token (parser)->keyword)
6912 case RID_SIZEOF:
6913 return c_parser_sizeof_expression (parser);
6914 case RID_ALIGNOF:
6915 return c_parser_alignof_expression (parser);
6916 case RID_EXTENSION:
6917 c_parser_consume_token (parser);
6918 ext = disable_extension_diagnostics ();
6919 ret = c_parser_cast_expression (parser, NULL);
6920 restore_extension_diagnostics (ext);
6921 return ret;
6922 case RID_REALPART:
6923 c_parser_consume_token (parser);
6924 exp_loc = c_parser_peek_token (parser)->location;
6925 op = c_parser_cast_expression (parser, NULL);
6926 op = default_function_array_conversion (exp_loc, op);
6927 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6928 case RID_IMAGPART:
6929 c_parser_consume_token (parser);
6930 exp_loc = c_parser_peek_token (parser)->location;
6931 op = c_parser_cast_expression (parser, NULL);
6932 op = default_function_array_conversion (exp_loc, op);
6933 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6934 case RID_TRANSACTION_ATOMIC:
6935 case RID_TRANSACTION_RELAXED:
6936 return c_parser_transaction_expression (parser,
6937 c_parser_peek_token (parser)->keyword);
6938 default:
6939 return c_parser_postfix_expression (parser);
6941 default:
6942 return c_parser_postfix_expression (parser);
6946 /* Parse a sizeof expression. */
6948 static struct c_expr
6949 c_parser_sizeof_expression (c_parser *parser)
6951 struct c_expr expr;
6952 struct c_expr result;
6953 location_t expr_loc;
6954 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6956 location_t start;
6957 location_t finish = UNKNOWN_LOCATION;
6959 start = c_parser_peek_token (parser)->location;
6961 c_parser_consume_token (parser);
6962 c_inhibit_evaluation_warnings++;
6963 in_sizeof++;
6964 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6965 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6967 /* Either sizeof ( type-name ) or sizeof unary-expression
6968 starting with a compound literal. */
6969 struct c_type_name *type_name;
6970 c_parser_consume_token (parser);
6971 expr_loc = c_parser_peek_token (parser)->location;
6972 type_name = c_parser_type_name (parser);
6973 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6974 finish = parser->tokens_buf[0].location;
6975 if (type_name == NULL)
6977 struct c_expr ret;
6978 c_inhibit_evaluation_warnings--;
6979 in_sizeof--;
6980 ret.value = error_mark_node;
6981 ret.original_code = ERROR_MARK;
6982 ret.original_type = NULL;
6983 return ret;
6985 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6987 expr = c_parser_postfix_expression_after_paren_type (parser,
6988 type_name,
6989 expr_loc);
6990 finish = expr.get_finish ();
6991 goto sizeof_expr;
6993 /* sizeof ( type-name ). */
6994 c_inhibit_evaluation_warnings--;
6995 in_sizeof--;
6996 result = c_expr_sizeof_type (expr_loc, type_name);
6998 else
7000 expr_loc = c_parser_peek_token (parser)->location;
7001 expr = c_parser_unary_expression (parser);
7002 finish = expr.get_finish ();
7003 sizeof_expr:
7004 c_inhibit_evaluation_warnings--;
7005 in_sizeof--;
7006 mark_exp_read (expr.value);
7007 if (TREE_CODE (expr.value) == COMPONENT_REF
7008 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7009 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7010 result = c_expr_sizeof_expr (expr_loc, expr);
7012 if (finish != UNKNOWN_LOCATION)
7013 set_c_expr_source_range (&result, start, finish);
7014 return result;
7017 /* Parse an alignof expression. */
7019 static struct c_expr
7020 c_parser_alignof_expression (c_parser *parser)
7022 struct c_expr expr;
7023 location_t start_loc = c_parser_peek_token (parser)->location;
7024 location_t end_loc;
7025 tree alignof_spelling = c_parser_peek_token (parser)->value;
7026 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7027 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7028 "_Alignof") == 0;
7029 /* A diagnostic is not required for the use of this identifier in
7030 the implementation namespace; only diagnose it for the C11
7031 spelling because of existing code using the other spellings. */
7032 if (is_c11_alignof)
7034 if (flag_isoc99)
7035 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7036 alignof_spelling);
7037 else
7038 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7039 alignof_spelling);
7041 c_parser_consume_token (parser);
7042 c_inhibit_evaluation_warnings++;
7043 in_alignof++;
7044 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7045 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7047 /* Either __alignof__ ( type-name ) or __alignof__
7048 unary-expression starting with a compound literal. */
7049 location_t loc;
7050 struct c_type_name *type_name;
7051 struct c_expr ret;
7052 c_parser_consume_token (parser);
7053 loc = c_parser_peek_token (parser)->location;
7054 type_name = c_parser_type_name (parser);
7055 end_loc = c_parser_peek_token (parser)->location;
7056 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7057 if (type_name == NULL)
7059 struct c_expr ret;
7060 c_inhibit_evaluation_warnings--;
7061 in_alignof--;
7062 ret.value = error_mark_node;
7063 ret.original_code = ERROR_MARK;
7064 ret.original_type = NULL;
7065 return ret;
7067 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7069 expr = c_parser_postfix_expression_after_paren_type (parser,
7070 type_name,
7071 loc);
7072 goto alignof_expr;
7074 /* alignof ( type-name ). */
7075 c_inhibit_evaluation_warnings--;
7076 in_alignof--;
7077 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7078 NULL, NULL),
7079 false, is_c11_alignof, 1);
7080 ret.original_code = ERROR_MARK;
7081 ret.original_type = NULL;
7082 set_c_expr_source_range (&ret, start_loc, end_loc);
7083 return ret;
7085 else
7087 struct c_expr ret;
7088 expr = c_parser_unary_expression (parser);
7089 end_loc = expr.src_range.m_finish;
7090 alignof_expr:
7091 mark_exp_read (expr.value);
7092 c_inhibit_evaluation_warnings--;
7093 in_alignof--;
7094 pedwarn (start_loc,
7095 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7096 alignof_spelling);
7097 ret.value = c_alignof_expr (start_loc, expr.value);
7098 ret.original_code = ERROR_MARK;
7099 ret.original_type = NULL;
7100 set_c_expr_source_range (&ret, start_loc, end_loc);
7101 return ret;
7105 /* Helper function to read arguments of builtins which are interfaces
7106 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7107 others. The name of the builtin is passed using BNAME parameter.
7108 Function returns true if there were no errors while parsing and
7109 stores the arguments in CEXPR_LIST. If it returns true,
7110 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7111 parenthesis. */
7112 static bool
7113 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7114 vec<c_expr_t, va_gc> **ret_cexpr_list,
7115 bool choose_expr_p,
7116 location_t *out_close_paren_loc)
7118 location_t loc = c_parser_peek_token (parser)->location;
7119 vec<c_expr_t, va_gc> *cexpr_list;
7120 c_expr_t expr;
7121 bool saved_force_folding_builtin_constant_p;
7123 *ret_cexpr_list = NULL;
7124 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7126 error_at (loc, "cannot take address of %qs", bname);
7127 return false;
7130 c_parser_consume_token (parser);
7132 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7134 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7135 c_parser_consume_token (parser);
7136 return true;
7139 saved_force_folding_builtin_constant_p
7140 = force_folding_builtin_constant_p;
7141 force_folding_builtin_constant_p |= choose_expr_p;
7142 expr = c_parser_expr_no_commas (parser, NULL);
7143 force_folding_builtin_constant_p
7144 = saved_force_folding_builtin_constant_p;
7145 vec_alloc (cexpr_list, 1);
7146 vec_safe_push (cexpr_list, expr);
7147 while (c_parser_next_token_is (parser, CPP_COMMA))
7149 c_parser_consume_token (parser);
7150 expr = c_parser_expr_no_commas (parser, NULL);
7151 vec_safe_push (cexpr_list, expr);
7154 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7155 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7156 return false;
7158 *ret_cexpr_list = cexpr_list;
7159 return true;
7162 /* This represents a single generic-association. */
7164 struct c_generic_association
7166 /* The location of the starting token of the type. */
7167 location_t type_location;
7168 /* The association's type, or NULL_TREE for 'default'. */
7169 tree type;
7170 /* The association's expression. */
7171 struct c_expr expression;
7174 /* Parse a generic-selection. (C11 6.5.1.1).
7176 generic-selection:
7177 _Generic ( assignment-expression , generic-assoc-list )
7179 generic-assoc-list:
7180 generic-association
7181 generic-assoc-list , generic-association
7183 generic-association:
7184 type-name : assignment-expression
7185 default : assignment-expression
7188 static struct c_expr
7189 c_parser_generic_selection (c_parser *parser)
7191 vec<c_generic_association> associations = vNULL;
7192 struct c_expr selector, error_expr;
7193 tree selector_type;
7194 struct c_generic_association matched_assoc;
7195 bool match_found = false;
7196 location_t generic_loc, selector_loc;
7198 error_expr.original_code = ERROR_MARK;
7199 error_expr.original_type = NULL;
7200 error_expr.set_error ();
7201 matched_assoc.type_location = UNKNOWN_LOCATION;
7202 matched_assoc.type = NULL_TREE;
7203 matched_assoc.expression = error_expr;
7205 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7206 generic_loc = c_parser_peek_token (parser)->location;
7207 c_parser_consume_token (parser);
7208 if (flag_isoc99)
7209 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7210 "ISO C99 does not support %<_Generic%>");
7211 else
7212 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7213 "ISO C90 does not support %<_Generic%>");
7215 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7216 return error_expr;
7218 c_inhibit_evaluation_warnings++;
7219 selector_loc = c_parser_peek_token (parser)->location;
7220 selector = c_parser_expr_no_commas (parser, NULL);
7221 selector = default_function_array_conversion (selector_loc, selector);
7222 c_inhibit_evaluation_warnings--;
7224 if (selector.value == error_mark_node)
7226 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7227 return selector;
7229 selector_type = TREE_TYPE (selector.value);
7230 /* In ISO C terms, rvalues (including the controlling expression of
7231 _Generic) do not have qualified types. */
7232 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7233 selector_type = TYPE_MAIN_VARIANT (selector_type);
7234 /* In ISO C terms, _Noreturn is not part of the type of expressions
7235 such as &abort, but in GCC it is represented internally as a type
7236 qualifier. */
7237 if (FUNCTION_POINTER_TYPE_P (selector_type)
7238 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7239 selector_type
7240 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7242 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7245 return error_expr;
7248 while (1)
7250 struct c_generic_association assoc, *iter;
7251 unsigned int ix;
7252 c_token *token = c_parser_peek_token (parser);
7254 assoc.type_location = token->location;
7255 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7257 c_parser_consume_token (parser);
7258 assoc.type = NULL_TREE;
7260 else
7262 struct c_type_name *type_name;
7264 type_name = c_parser_type_name (parser);
7265 if (type_name == NULL)
7267 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7268 goto error_exit;
7270 assoc.type = groktypename (type_name, NULL, NULL);
7271 if (assoc.type == error_mark_node)
7273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7274 goto error_exit;
7277 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7278 error_at (assoc.type_location,
7279 "%<_Generic%> association has function type");
7280 else if (!COMPLETE_TYPE_P (assoc.type))
7281 error_at (assoc.type_location,
7282 "%<_Generic%> association has incomplete type");
7284 if (variably_modified_type_p (assoc.type, NULL_TREE))
7285 error_at (assoc.type_location,
7286 "%<_Generic%> association has "
7287 "variable length type");
7290 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7292 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7293 goto error_exit;
7296 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7297 if (assoc.expression.value == error_mark_node)
7299 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7300 goto error_exit;
7303 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7305 if (assoc.type == NULL_TREE)
7307 if (iter->type == NULL_TREE)
7309 error_at (assoc.type_location,
7310 "duplicate %<default%> case in %<_Generic%>");
7311 inform (iter->type_location, "original %<default%> is here");
7314 else if (iter->type != NULL_TREE)
7316 if (comptypes (assoc.type, iter->type))
7318 error_at (assoc.type_location,
7319 "%<_Generic%> specifies two compatible types");
7320 inform (iter->type_location, "compatible type is here");
7325 if (assoc.type == NULL_TREE)
7327 if (!match_found)
7329 matched_assoc = assoc;
7330 match_found = true;
7333 else if (comptypes (assoc.type, selector_type))
7335 if (!match_found || matched_assoc.type == NULL_TREE)
7337 matched_assoc = assoc;
7338 match_found = true;
7340 else
7342 error_at (assoc.type_location,
7343 "%<_Generic> selector matches multiple associations");
7344 inform (matched_assoc.type_location,
7345 "other match is here");
7349 associations.safe_push (assoc);
7351 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7352 break;
7353 c_parser_consume_token (parser);
7356 associations.release ();
7358 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7360 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7361 return error_expr;
7364 if (!match_found)
7366 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7367 "compatible with any association",
7368 selector_type);
7369 return error_expr;
7372 return matched_assoc.expression;
7374 error_exit:
7375 associations.release ();
7376 return error_expr;
7379 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7381 postfix-expression:
7382 primary-expression
7383 postfix-expression [ expression ]
7384 postfix-expression ( argument-expression-list[opt] )
7385 postfix-expression . identifier
7386 postfix-expression -> identifier
7387 postfix-expression ++
7388 postfix-expression --
7389 ( type-name ) { initializer-list }
7390 ( type-name ) { initializer-list , }
7392 argument-expression-list:
7393 argument-expression
7394 argument-expression-list , argument-expression
7396 primary-expression:
7397 identifier
7398 constant
7399 string-literal
7400 ( expression )
7401 generic-selection
7403 GNU extensions:
7405 primary-expression:
7406 __func__
7407 (treated as a keyword in GNU C)
7408 __FUNCTION__
7409 __PRETTY_FUNCTION__
7410 ( compound-statement )
7411 __builtin_va_arg ( assignment-expression , type-name )
7412 __builtin_offsetof ( type-name , offsetof-member-designator )
7413 __builtin_choose_expr ( assignment-expression ,
7414 assignment-expression ,
7415 assignment-expression )
7416 __builtin_types_compatible_p ( type-name , type-name )
7417 __builtin_complex ( assignment-expression , assignment-expression )
7418 __builtin_shuffle ( assignment-expression , assignment-expression )
7419 __builtin_shuffle ( assignment-expression ,
7420 assignment-expression ,
7421 assignment-expression, )
7423 offsetof-member-designator:
7424 identifier
7425 offsetof-member-designator . identifier
7426 offsetof-member-designator [ expression ]
7428 Objective-C:
7430 primary-expression:
7431 [ objc-receiver objc-message-args ]
7432 @selector ( objc-selector-arg )
7433 @protocol ( identifier )
7434 @encode ( type-name )
7435 objc-string-literal
7436 Classname . identifier
7439 static struct c_expr
7440 c_parser_postfix_expression (c_parser *parser)
7442 struct c_expr expr, e1;
7443 struct c_type_name *t1, *t2;
7444 location_t loc = c_parser_peek_token (parser)->location;;
7445 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7446 expr.original_code = ERROR_MARK;
7447 expr.original_type = NULL;
7448 switch (c_parser_peek_token (parser)->type)
7450 case CPP_NUMBER:
7451 expr.value = c_parser_peek_token (parser)->value;
7452 set_c_expr_source_range (&expr, tok_range);
7453 loc = c_parser_peek_token (parser)->location;
7454 c_parser_consume_token (parser);
7455 if (TREE_CODE (expr.value) == FIXED_CST
7456 && !targetm.fixed_point_supported_p ())
7458 error_at (loc, "fixed-point types not supported for this target");
7459 expr.value = error_mark_node;
7461 break;
7462 case CPP_CHAR:
7463 case CPP_CHAR16:
7464 case CPP_CHAR32:
7465 case CPP_WCHAR:
7466 expr.value = c_parser_peek_token (parser)->value;
7467 set_c_expr_source_range (&expr, tok_range);
7468 c_parser_consume_token (parser);
7469 break;
7470 case CPP_STRING:
7471 case CPP_STRING16:
7472 case CPP_STRING32:
7473 case CPP_WSTRING:
7474 case CPP_UTF8STRING:
7475 expr.value = c_parser_peek_token (parser)->value;
7476 set_c_expr_source_range (&expr, tok_range);
7477 expr.original_code = STRING_CST;
7478 c_parser_consume_token (parser);
7479 break;
7480 case CPP_OBJC_STRING:
7481 gcc_assert (c_dialect_objc ());
7482 expr.value
7483 = objc_build_string_object (c_parser_peek_token (parser)->value);
7484 set_c_expr_source_range (&expr, tok_range);
7485 c_parser_consume_token (parser);
7486 break;
7487 case CPP_NAME:
7488 switch (c_parser_peek_token (parser)->id_kind)
7490 case C_ID_ID:
7492 tree id = c_parser_peek_token (parser)->value;
7493 c_parser_consume_token (parser);
7494 expr.value = build_external_ref (loc, id,
7495 (c_parser_peek_token (parser)->type
7496 == CPP_OPEN_PAREN),
7497 &expr.original_type);
7498 set_c_expr_source_range (&expr, tok_range);
7499 break;
7501 case C_ID_CLASSNAME:
7503 /* Here we parse the Objective-C 2.0 Class.name dot
7504 syntax. */
7505 tree class_name = c_parser_peek_token (parser)->value;
7506 tree component;
7507 c_parser_consume_token (parser);
7508 gcc_assert (c_dialect_objc ());
7509 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7511 expr.set_error ();
7512 break;
7514 if (c_parser_next_token_is_not (parser, CPP_NAME))
7516 c_parser_error (parser, "expected identifier");
7517 expr.set_error ();
7518 break;
7520 c_token *component_tok = c_parser_peek_token (parser);
7521 component = component_tok->value;
7522 location_t end_loc = component_tok->get_finish ();
7523 c_parser_consume_token (parser);
7524 expr.value = objc_build_class_component_ref (class_name,
7525 component);
7526 set_c_expr_source_range (&expr, loc, end_loc);
7527 break;
7529 default:
7530 c_parser_error (parser, "expected expression");
7531 expr.set_error ();
7532 break;
7534 break;
7535 case CPP_OPEN_PAREN:
7536 /* A parenthesized expression, statement expression or compound
7537 literal. */
7538 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7540 /* A statement expression. */
7541 tree stmt;
7542 location_t brace_loc;
7543 c_parser_consume_token (parser);
7544 brace_loc = c_parser_peek_token (parser)->location;
7545 c_parser_consume_token (parser);
7546 if (!building_stmt_list_p ())
7548 error_at (loc, "braced-group within expression allowed "
7549 "only inside a function");
7550 parser->error = true;
7551 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7552 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7553 expr.set_error ();
7554 break;
7556 stmt = c_begin_stmt_expr ();
7557 c_parser_compound_statement_nostart (parser);
7558 location_t close_loc = c_parser_peek_token (parser)->location;
7559 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7560 "expected %<)%>");
7561 pedwarn (loc, OPT_Wpedantic,
7562 "ISO C forbids braced-groups within expressions");
7563 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7564 set_c_expr_source_range (&expr, loc, close_loc);
7565 mark_exp_read (expr.value);
7567 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7569 /* A compound literal. ??? Can we actually get here rather
7570 than going directly to
7571 c_parser_postfix_expression_after_paren_type from
7572 elsewhere? */
7573 location_t loc;
7574 struct c_type_name *type_name;
7575 c_parser_consume_token (parser);
7576 loc = c_parser_peek_token (parser)->location;
7577 type_name = c_parser_type_name (parser);
7578 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7579 "expected %<)%>");
7580 if (type_name == NULL)
7582 expr.set_error ();
7584 else
7585 expr = c_parser_postfix_expression_after_paren_type (parser,
7586 type_name,
7587 loc);
7589 else
7591 /* A parenthesized expression. */
7592 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7593 c_parser_consume_token (parser);
7594 expr = c_parser_expression (parser);
7595 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7596 TREE_NO_WARNING (expr.value) = 1;
7597 if (expr.original_code != C_MAYBE_CONST_EXPR)
7598 expr.original_code = ERROR_MARK;
7599 /* Don't change EXPR.ORIGINAL_TYPE. */
7600 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7601 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7602 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7603 "expected %<)%>");
7605 break;
7606 case CPP_KEYWORD:
7607 switch (c_parser_peek_token (parser)->keyword)
7609 case RID_FUNCTION_NAME:
7610 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7611 "%<__FUNCTION__%> predefined identifier");
7612 expr.value = fname_decl (loc,
7613 c_parser_peek_token (parser)->keyword,
7614 c_parser_peek_token (parser)->value);
7615 set_c_expr_source_range (&expr, loc, loc);
7616 c_parser_consume_token (parser);
7617 break;
7618 case RID_PRETTY_FUNCTION_NAME:
7619 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7620 "%<__PRETTY_FUNCTION__%> predefined identifier");
7621 expr.value = fname_decl (loc,
7622 c_parser_peek_token (parser)->keyword,
7623 c_parser_peek_token (parser)->value);
7624 set_c_expr_source_range (&expr, loc, loc);
7625 c_parser_consume_token (parser);
7626 break;
7627 case RID_C99_FUNCTION_NAME:
7628 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7629 "%<__func__%> predefined identifier");
7630 expr.value = fname_decl (loc,
7631 c_parser_peek_token (parser)->keyword,
7632 c_parser_peek_token (parser)->value);
7633 set_c_expr_source_range (&expr, loc, loc);
7634 c_parser_consume_token (parser);
7635 break;
7636 case RID_VA_ARG:
7638 location_t start_loc = loc;
7639 c_parser_consume_token (parser);
7640 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7642 expr.set_error ();
7643 break;
7645 e1 = c_parser_expr_no_commas (parser, NULL);
7646 mark_exp_read (e1.value);
7647 e1.value = c_fully_fold (e1.value, false, NULL);
7648 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7650 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7651 expr.set_error ();
7652 break;
7654 loc = c_parser_peek_token (parser)->location;
7655 t1 = c_parser_type_name (parser);
7656 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7658 "expected %<)%>");
7659 if (t1 == NULL)
7661 expr.set_error ();
7663 else
7665 tree type_expr = NULL_TREE;
7666 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7667 groktypename (t1, &type_expr, NULL));
7668 if (type_expr)
7670 expr.value = build2 (C_MAYBE_CONST_EXPR,
7671 TREE_TYPE (expr.value), type_expr,
7672 expr.value);
7673 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7675 set_c_expr_source_range (&expr, start_loc, end_loc);
7678 break;
7679 case RID_OFFSETOF:
7680 c_parser_consume_token (parser);
7681 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7683 expr.set_error ();
7684 break;
7686 t1 = c_parser_type_name (parser);
7687 if (t1 == NULL)
7688 parser->error = true;
7689 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7690 gcc_assert (parser->error);
7691 if (parser->error)
7693 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7694 expr.set_error ();
7695 break;
7699 tree type = groktypename (t1, NULL, NULL);
7700 tree offsetof_ref;
7701 if (type == error_mark_node)
7702 offsetof_ref = error_mark_node;
7703 else
7705 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7706 SET_EXPR_LOCATION (offsetof_ref, loc);
7708 /* Parse the second argument to __builtin_offsetof. We
7709 must have one identifier, and beyond that we want to
7710 accept sub structure and sub array references. */
7711 if (c_parser_next_token_is (parser, CPP_NAME))
7713 c_token *comp_tok = c_parser_peek_token (parser);
7714 offsetof_ref = build_component_ref
7715 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
7716 c_parser_consume_token (parser);
7717 while (c_parser_next_token_is (parser, CPP_DOT)
7718 || c_parser_next_token_is (parser,
7719 CPP_OPEN_SQUARE)
7720 || c_parser_next_token_is (parser,
7721 CPP_DEREF))
7723 if (c_parser_next_token_is (parser, CPP_DEREF))
7725 loc = c_parser_peek_token (parser)->location;
7726 offsetof_ref = build_array_ref (loc,
7727 offsetof_ref,
7728 integer_zero_node);
7729 goto do_dot;
7731 else if (c_parser_next_token_is (parser, CPP_DOT))
7733 do_dot:
7734 c_parser_consume_token (parser);
7735 if (c_parser_next_token_is_not (parser,
7736 CPP_NAME))
7738 c_parser_error (parser, "expected identifier");
7739 break;
7741 c_token *comp_tok = c_parser_peek_token (parser);
7742 offsetof_ref = build_component_ref
7743 (loc, offsetof_ref, comp_tok->value,
7744 comp_tok->location);
7745 c_parser_consume_token (parser);
7747 else
7749 struct c_expr ce;
7750 tree idx;
7751 loc = c_parser_peek_token (parser)->location;
7752 c_parser_consume_token (parser);
7753 ce = c_parser_expression (parser);
7754 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7755 idx = ce.value;
7756 idx = c_fully_fold (idx, false, NULL);
7757 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7758 "expected %<]%>");
7759 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7763 else
7764 c_parser_error (parser, "expected identifier");
7765 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7767 "expected %<)%>");
7768 expr.value = fold_offsetof (offsetof_ref);
7769 set_c_expr_source_range (&expr, loc, end_loc);
7771 break;
7772 case RID_CHOOSE_EXPR:
7774 vec<c_expr_t, va_gc> *cexpr_list;
7775 c_expr_t *e1_p, *e2_p, *e3_p;
7776 tree c;
7777 location_t close_paren_loc;
7779 c_parser_consume_token (parser);
7780 if (!c_parser_get_builtin_args (parser,
7781 "__builtin_choose_expr",
7782 &cexpr_list, true,
7783 &close_paren_loc))
7785 expr.set_error ();
7786 break;
7789 if (vec_safe_length (cexpr_list) != 3)
7791 error_at (loc, "wrong number of arguments to "
7792 "%<__builtin_choose_expr%>");
7793 expr.set_error ();
7794 break;
7797 e1_p = &(*cexpr_list)[0];
7798 e2_p = &(*cexpr_list)[1];
7799 e3_p = &(*cexpr_list)[2];
7801 c = e1_p->value;
7802 mark_exp_read (e2_p->value);
7803 mark_exp_read (e3_p->value);
7804 if (TREE_CODE (c) != INTEGER_CST
7805 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7806 error_at (loc,
7807 "first argument to %<__builtin_choose_expr%> not"
7808 " a constant");
7809 constant_expression_warning (c);
7810 expr = integer_zerop (c) ? *e3_p : *e2_p;
7811 set_c_expr_source_range (&expr, loc, close_paren_loc);
7812 break;
7814 case RID_TYPES_COMPATIBLE_P:
7815 c_parser_consume_token (parser);
7816 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7818 expr.set_error ();
7819 break;
7821 t1 = c_parser_type_name (parser);
7822 if (t1 == NULL)
7824 expr.set_error ();
7825 break;
7827 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7829 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7830 expr.set_error ();
7831 break;
7833 t2 = c_parser_type_name (parser);
7834 if (t2 == NULL)
7836 expr.set_error ();
7837 break;
7840 location_t close_paren_loc = c_parser_peek_token (parser)->location;
7841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7842 "expected %<)%>");
7843 tree e1, e2;
7844 e1 = groktypename (t1, NULL, NULL);
7845 e2 = groktypename (t2, NULL, NULL);
7846 if (e1 == error_mark_node || e2 == error_mark_node)
7848 expr.set_error ();
7849 break;
7852 e1 = TYPE_MAIN_VARIANT (e1);
7853 e2 = TYPE_MAIN_VARIANT (e2);
7855 expr.value
7856 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7857 set_c_expr_source_range (&expr, loc, close_paren_loc);
7859 break;
7860 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7862 vec<c_expr_t, va_gc> *cexpr_list;
7863 c_expr_t *e2_p;
7864 tree chain_value;
7865 location_t close_paren_loc;
7867 c_parser_consume_token (parser);
7868 if (!c_parser_get_builtin_args (parser,
7869 "__builtin_call_with_static_chain",
7870 &cexpr_list, false,
7871 &close_paren_loc))
7873 expr.set_error ();
7874 break;
7876 if (vec_safe_length (cexpr_list) != 2)
7878 error_at (loc, "wrong number of arguments to "
7879 "%<__builtin_call_with_static_chain%>");
7880 expr.set_error ();
7881 break;
7884 expr = (*cexpr_list)[0];
7885 e2_p = &(*cexpr_list)[1];
7886 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7887 chain_value = e2_p->value;
7888 mark_exp_read (chain_value);
7890 if (TREE_CODE (expr.value) != CALL_EXPR)
7891 error_at (loc, "first argument to "
7892 "%<__builtin_call_with_static_chain%> "
7893 "must be a call expression");
7894 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7895 error_at (loc, "second argument to "
7896 "%<__builtin_call_with_static_chain%> "
7897 "must be a pointer type");
7898 else
7899 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7900 set_c_expr_source_range (&expr, loc, close_paren_loc);
7901 break;
7903 case RID_BUILTIN_COMPLEX:
7905 vec<c_expr_t, va_gc> *cexpr_list;
7906 c_expr_t *e1_p, *e2_p;
7907 location_t close_paren_loc;
7909 c_parser_consume_token (parser);
7910 if (!c_parser_get_builtin_args (parser,
7911 "__builtin_complex",
7912 &cexpr_list, false,
7913 &close_paren_loc))
7915 expr.set_error ();
7916 break;
7919 if (vec_safe_length (cexpr_list) != 2)
7921 error_at (loc, "wrong number of arguments to "
7922 "%<__builtin_complex%>");
7923 expr.set_error ();
7924 break;
7927 e1_p = &(*cexpr_list)[0];
7928 e2_p = &(*cexpr_list)[1];
7930 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7931 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7932 e1_p->value = convert (TREE_TYPE (e1_p->value),
7933 TREE_OPERAND (e1_p->value, 0));
7934 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7935 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7936 e2_p->value = convert (TREE_TYPE (e2_p->value),
7937 TREE_OPERAND (e2_p->value, 0));
7938 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7939 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7940 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7941 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7943 error_at (loc, "%<__builtin_complex%> operand "
7944 "not of real binary floating-point type");
7945 expr.set_error ();
7946 break;
7948 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7949 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7951 error_at (loc,
7952 "%<__builtin_complex%> operands of different types");
7953 expr.set_error ();
7954 break;
7956 pedwarn_c90 (loc, OPT_Wpedantic,
7957 "ISO C90 does not support complex types");
7958 expr.value = build2_loc (loc, COMPLEX_EXPR,
7959 build_complex_type
7960 (TYPE_MAIN_VARIANT
7961 (TREE_TYPE (e1_p->value))),
7962 e1_p->value, e2_p->value);
7963 set_c_expr_source_range (&expr, loc, close_paren_loc);
7964 break;
7966 case RID_BUILTIN_SHUFFLE:
7968 vec<c_expr_t, va_gc> *cexpr_list;
7969 unsigned int i;
7970 c_expr_t *p;
7971 location_t close_paren_loc;
7973 c_parser_consume_token (parser);
7974 if (!c_parser_get_builtin_args (parser,
7975 "__builtin_shuffle",
7976 &cexpr_list, false,
7977 &close_paren_loc))
7979 expr.set_error ();
7980 break;
7983 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7984 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7986 if (vec_safe_length (cexpr_list) == 2)
7987 expr.value =
7988 c_build_vec_perm_expr
7989 (loc, (*cexpr_list)[0].value,
7990 NULL_TREE, (*cexpr_list)[1].value);
7992 else if (vec_safe_length (cexpr_list) == 3)
7993 expr.value =
7994 c_build_vec_perm_expr
7995 (loc, (*cexpr_list)[0].value,
7996 (*cexpr_list)[1].value,
7997 (*cexpr_list)[2].value);
7998 else
8000 error_at (loc, "wrong number of arguments to "
8001 "%<__builtin_shuffle%>");
8002 expr.set_error ();
8004 set_c_expr_source_range (&expr, loc, close_paren_loc);
8005 break;
8007 case RID_AT_SELECTOR:
8008 gcc_assert (c_dialect_objc ());
8009 c_parser_consume_token (parser);
8010 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8012 expr.set_error ();
8013 break;
8016 tree sel = c_parser_objc_selector_arg (parser);
8017 location_t close_loc = c_parser_peek_token (parser)->location;
8018 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8019 "expected %<)%>");
8020 expr.value = objc_build_selector_expr (loc, sel);
8021 set_c_expr_source_range (&expr, loc, close_loc);
8023 break;
8024 case RID_AT_PROTOCOL:
8025 gcc_assert (c_dialect_objc ());
8026 c_parser_consume_token (parser);
8027 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8029 expr.set_error ();
8030 break;
8032 if (c_parser_next_token_is_not (parser, CPP_NAME))
8034 c_parser_error (parser, "expected identifier");
8035 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8036 expr.set_error ();
8037 break;
8040 tree id = c_parser_peek_token (parser)->value;
8041 c_parser_consume_token (parser);
8042 location_t close_loc = c_parser_peek_token (parser)->location;
8043 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8044 "expected %<)%>");
8045 expr.value = objc_build_protocol_expr (id);
8046 set_c_expr_source_range (&expr, loc, close_loc);
8048 break;
8049 case RID_AT_ENCODE:
8050 /* Extension to support C-structures in the archiver. */
8051 gcc_assert (c_dialect_objc ());
8052 c_parser_consume_token (parser);
8053 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8055 expr.set_error ();
8056 break;
8058 t1 = c_parser_type_name (parser);
8059 if (t1 == NULL)
8061 expr.set_error ();
8062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8063 break;
8066 location_t close_loc = c_parser_peek_token (parser)->location;
8067 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8068 "expected %<)%>");
8069 tree type = groktypename (t1, NULL, NULL);
8070 expr.value = objc_build_encode_expr (type);
8071 set_c_expr_source_range (&expr, loc, close_loc);
8073 break;
8074 case RID_GENERIC:
8075 expr = c_parser_generic_selection (parser);
8076 break;
8077 case RID_CILK_SPAWN:
8078 c_parser_consume_token (parser);
8079 if (!flag_cilkplus)
8081 error_at (loc, "-fcilkplus must be enabled to use "
8082 "%<_Cilk_spawn%>");
8083 expr = c_parser_cast_expression (parser, NULL);
8084 expr.set_error ();
8086 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8088 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
8089 "are not permitted");
8090 /* Now flush out all the _Cilk_spawns. */
8091 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8092 c_parser_consume_token (parser);
8093 expr = c_parser_cast_expression (parser, NULL);
8095 else
8097 expr = c_parser_cast_expression (parser, NULL);
8098 expr.value = build_cilk_spawn (loc, expr.value);
8100 break;
8101 default:
8102 c_parser_error (parser, "expected expression");
8103 expr.set_error ();
8104 break;
8106 break;
8107 case CPP_OPEN_SQUARE:
8108 if (c_dialect_objc ())
8110 tree receiver, args;
8111 c_parser_consume_token (parser);
8112 receiver = c_parser_objc_receiver (parser);
8113 args = c_parser_objc_message_args (parser);
8114 location_t close_loc = c_parser_peek_token (parser)->location;
8115 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8116 "expected %<]%>");
8117 expr.value = objc_build_message_expr (receiver, args);
8118 set_c_expr_source_range (&expr, loc, close_loc);
8119 break;
8121 /* Else fall through to report error. */
8122 default:
8123 c_parser_error (parser, "expected expression");
8124 expr.set_error ();
8125 break;
8127 return c_parser_postfix_expression_after_primary
8128 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8131 /* Parse a postfix expression after a parenthesized type name: the
8132 brace-enclosed initializer of a compound literal, possibly followed
8133 by some postfix operators. This is separate because it is not
8134 possible to tell until after the type name whether a cast
8135 expression has a cast or a compound literal, or whether the operand
8136 of sizeof is a parenthesized type name or starts with a compound
8137 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8138 location of the first token after the parentheses around the type
8139 name. */
8141 static struct c_expr
8142 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8143 struct c_type_name *type_name,
8144 location_t type_loc)
8146 tree type;
8147 struct c_expr init;
8148 bool non_const;
8149 struct c_expr expr;
8150 location_t start_loc;
8151 tree type_expr = NULL_TREE;
8152 bool type_expr_const = true;
8153 check_compound_literal_type (type_loc, type_name);
8154 start_init (NULL_TREE, NULL, 0);
8155 type = groktypename (type_name, &type_expr, &type_expr_const);
8156 start_loc = c_parser_peek_token (parser)->location;
8157 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8159 error_at (type_loc, "compound literal has variable size");
8160 type = error_mark_node;
8162 init = c_parser_braced_init (parser, type, false, NULL);
8163 finish_init ();
8164 maybe_warn_string_init (type_loc, type, init);
8166 if (type != error_mark_node
8167 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8168 && current_function_decl)
8170 error ("compound literal qualified by address-space qualifier");
8171 type = error_mark_node;
8174 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8175 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8176 ? CONSTRUCTOR_NON_CONST (init.value)
8177 : init.original_code == C_MAYBE_CONST_EXPR);
8178 non_const |= !type_expr_const;
8179 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8180 set_c_expr_source_range (&expr, init.src_range);
8181 expr.original_code = ERROR_MARK;
8182 expr.original_type = NULL;
8183 if (type != error_mark_node && type_expr)
8185 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8187 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8188 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8190 else
8192 gcc_assert (!non_const);
8193 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8194 type_expr, expr.value);
8197 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8200 /* Callback function for sizeof_pointer_memaccess_warning to compare
8201 types. */
8203 static bool
8204 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8206 return comptypes (type1, type2) == 1;
8209 /* Parse a postfix expression after the initial primary or compound
8210 literal; that is, parse a series of postfix operators.
8212 EXPR_LOC is the location of the primary expression. */
8214 static struct c_expr
8215 c_parser_postfix_expression_after_primary (c_parser *parser,
8216 location_t expr_loc,
8217 struct c_expr expr)
8219 struct c_expr orig_expr;
8220 tree ident, idx;
8221 location_t sizeof_arg_loc[3], comp_loc;
8222 tree sizeof_arg[3];
8223 unsigned int literal_zero_mask;
8224 unsigned int i;
8225 vec<tree, va_gc> *exprlist;
8226 vec<tree, va_gc> *origtypes = NULL;
8227 vec<location_t> arg_loc = vNULL;
8228 location_t start;
8229 location_t finish;
8231 while (true)
8233 location_t op_loc = c_parser_peek_token (parser)->location;
8234 switch (c_parser_peek_token (parser)->type)
8236 case CPP_OPEN_SQUARE:
8237 /* Array reference. */
8238 c_parser_consume_token (parser);
8239 if (flag_cilkplus
8240 && c_parser_peek_token (parser)->type == CPP_COLON)
8241 /* If we are here, then we have something like this:
8242 Array [ : ]
8244 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8245 expr.value);
8246 else
8248 idx = c_parser_expression (parser).value;
8249 /* Here we have 3 options:
8250 1. Array [EXPR] -- Normal Array call.
8251 2. Array [EXPR : EXPR] -- Array notation without stride.
8252 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8254 For 1, we just handle it just like a normal array expression.
8255 For 2 and 3 we handle it like we handle array notations. The
8256 idx value we have above becomes the initial/start index.
8258 if (flag_cilkplus
8259 && c_parser_peek_token (parser)->type == CPP_COLON)
8260 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8261 expr.value);
8262 else
8264 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8265 "expected %<]%>");
8266 start = expr.get_start ();
8267 finish = parser->tokens_buf[0].location;
8268 expr.value = build_array_ref (op_loc, expr.value, idx);
8269 set_c_expr_source_range (&expr, start, finish);
8272 expr.original_code = ERROR_MARK;
8273 expr.original_type = NULL;
8274 break;
8275 case CPP_OPEN_PAREN:
8276 /* Function call. */
8277 c_parser_consume_token (parser);
8278 for (i = 0; i < 3; i++)
8280 sizeof_arg[i] = NULL_TREE;
8281 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8283 literal_zero_mask = 0;
8284 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8285 exprlist = NULL;
8286 else
8287 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8288 sizeof_arg_loc, sizeof_arg,
8289 &arg_loc, &literal_zero_mask);
8290 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8291 "expected %<)%>");
8292 orig_expr = expr;
8293 mark_exp_read (expr.value);
8294 if (warn_sizeof_pointer_memaccess)
8295 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8296 expr.value, exprlist,
8297 sizeof_arg,
8298 sizeof_ptr_memacc_comptypes);
8299 if (TREE_CODE (expr.value) == FUNCTION_DECL
8300 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8301 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8302 && vec_safe_length (exprlist) == 3)
8304 tree arg0 = (*exprlist)[0];
8305 tree arg2 = (*exprlist)[2];
8306 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
8309 start = expr.get_start ();
8310 finish = parser->tokens_buf[0].get_finish ();
8311 expr.value
8312 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8313 exprlist, origtypes);
8314 set_c_expr_source_range (&expr, start, finish);
8316 expr.original_code = ERROR_MARK;
8317 if (TREE_CODE (expr.value) == INTEGER_CST
8318 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8319 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8320 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8321 expr.original_code = C_MAYBE_CONST_EXPR;
8322 expr.original_type = NULL;
8323 if (exprlist)
8325 release_tree_vector (exprlist);
8326 release_tree_vector (origtypes);
8328 arg_loc.release ();
8329 break;
8330 case CPP_DOT:
8331 /* Structure element reference. */
8332 c_parser_consume_token (parser);
8333 expr = default_function_array_conversion (expr_loc, expr);
8334 if (c_parser_next_token_is (parser, CPP_NAME))
8336 c_token *comp_tok = c_parser_peek_token (parser);
8337 ident = comp_tok->value;
8338 comp_loc = comp_tok->location;
8340 else
8342 c_parser_error (parser, "expected identifier");
8343 expr.set_error ();
8344 expr.original_code = ERROR_MARK;
8345 expr.original_type = NULL;
8346 return expr;
8348 start = expr.get_start ();
8349 finish = c_parser_peek_token (parser)->get_finish ();
8350 c_parser_consume_token (parser);
8351 expr.value = build_component_ref (op_loc, expr.value, ident,
8352 comp_loc);
8353 set_c_expr_source_range (&expr, start, finish);
8354 expr.original_code = ERROR_MARK;
8355 if (TREE_CODE (expr.value) != COMPONENT_REF)
8356 expr.original_type = NULL;
8357 else
8359 /* Remember the original type of a bitfield. */
8360 tree field = TREE_OPERAND (expr.value, 1);
8361 if (TREE_CODE (field) != FIELD_DECL)
8362 expr.original_type = NULL;
8363 else
8364 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8366 break;
8367 case CPP_DEREF:
8368 /* Structure element reference. */
8369 c_parser_consume_token (parser);
8370 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8371 if (c_parser_next_token_is (parser, CPP_NAME))
8373 c_token *comp_tok = c_parser_peek_token (parser);
8374 ident = comp_tok->value;
8375 comp_loc = comp_tok->location;
8377 else
8379 c_parser_error (parser, "expected identifier");
8380 expr.set_error ();
8381 expr.original_code = ERROR_MARK;
8382 expr.original_type = NULL;
8383 return expr;
8385 start = expr.get_start ();
8386 finish = c_parser_peek_token (parser)->get_finish ();
8387 c_parser_consume_token (parser);
8388 expr.value = build_component_ref (op_loc,
8389 build_indirect_ref (op_loc,
8390 expr.value,
8391 RO_ARROW),
8392 ident, comp_loc);
8393 set_c_expr_source_range (&expr, start, finish);
8394 expr.original_code = ERROR_MARK;
8395 if (TREE_CODE (expr.value) != COMPONENT_REF)
8396 expr.original_type = NULL;
8397 else
8399 /* Remember the original type of a bitfield. */
8400 tree field = TREE_OPERAND (expr.value, 1);
8401 if (TREE_CODE (field) != FIELD_DECL)
8402 expr.original_type = NULL;
8403 else
8404 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8406 break;
8407 case CPP_PLUS_PLUS:
8408 /* Postincrement. */
8409 start = expr.get_start ();
8410 finish = c_parser_peek_token (parser)->get_finish ();
8411 c_parser_consume_token (parser);
8412 /* If the expressions have array notations, we expand them. */
8413 if (flag_cilkplus
8414 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8415 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8416 else
8418 expr = default_function_array_read_conversion (expr_loc, expr);
8419 expr.value = build_unary_op (op_loc,
8420 POSTINCREMENT_EXPR, expr.value, 0);
8422 set_c_expr_source_range (&expr, start, finish);
8423 expr.original_code = ERROR_MARK;
8424 expr.original_type = NULL;
8425 break;
8426 case CPP_MINUS_MINUS:
8427 /* Postdecrement. */
8428 start = expr.get_start ();
8429 finish = c_parser_peek_token (parser)->get_finish ();
8430 c_parser_consume_token (parser);
8431 /* If the expressions have array notations, we expand them. */
8432 if (flag_cilkplus
8433 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8434 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8435 else
8437 expr = default_function_array_read_conversion (expr_loc, expr);
8438 expr.value = build_unary_op (op_loc,
8439 POSTDECREMENT_EXPR, expr.value, 0);
8441 set_c_expr_source_range (&expr, start, finish);
8442 expr.original_code = ERROR_MARK;
8443 expr.original_type = NULL;
8444 break;
8445 default:
8446 return expr;
8451 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8453 expression:
8454 assignment-expression
8455 expression , assignment-expression
8458 static struct c_expr
8459 c_parser_expression (c_parser *parser)
8461 location_t tloc = c_parser_peek_token (parser)->location;
8462 struct c_expr expr;
8463 expr = c_parser_expr_no_commas (parser, NULL);
8464 if (c_parser_next_token_is (parser, CPP_COMMA))
8465 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8466 while (c_parser_next_token_is (parser, CPP_COMMA))
8468 struct c_expr next;
8469 tree lhsval;
8470 location_t loc = c_parser_peek_token (parser)->location;
8471 location_t expr_loc;
8472 c_parser_consume_token (parser);
8473 expr_loc = c_parser_peek_token (parser)->location;
8474 lhsval = expr.value;
8475 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8476 lhsval = TREE_OPERAND (lhsval, 1);
8477 if (DECL_P (lhsval) || handled_component_p (lhsval))
8478 mark_exp_read (lhsval);
8479 next = c_parser_expr_no_commas (parser, NULL);
8480 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8481 expr.value = build_compound_expr (loc, expr.value, next.value);
8482 expr.original_code = COMPOUND_EXPR;
8483 expr.original_type = next.original_type;
8485 return expr;
8488 /* Parse an expression and convert functions or arrays to pointers and
8489 lvalues to rvalues. */
8491 static struct c_expr
8492 c_parser_expression_conv (c_parser *parser)
8494 struct c_expr expr;
8495 location_t loc = c_parser_peek_token (parser)->location;
8496 expr = c_parser_expression (parser);
8497 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8498 return expr;
8501 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8502 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8504 static inline void
8505 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8506 unsigned int idx)
8508 if (idx >= HOST_BITS_PER_INT)
8509 return;
8511 c_token *tok = c_parser_peek_token (parser);
8512 switch (tok->type)
8514 case CPP_NUMBER:
8515 case CPP_CHAR:
8516 case CPP_WCHAR:
8517 case CPP_CHAR16:
8518 case CPP_CHAR32:
8519 /* If a parameter is literal zero alone, remember it
8520 for -Wmemset-transposed-args warning. */
8521 if (integer_zerop (tok->value)
8522 && !TREE_OVERFLOW (tok->value)
8523 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8524 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8525 *literal_zero_mask |= 1U << idx;
8526 default:
8527 break;
8531 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8532 functions and arrays to pointers and lvalues to rvalues. If
8533 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8534 locations of function arguments into this vector.
8536 nonempty-expr-list:
8537 assignment-expression
8538 nonempty-expr-list , assignment-expression
8541 static vec<tree, va_gc> *
8542 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8543 vec<tree, va_gc> **p_orig_types,
8544 location_t *sizeof_arg_loc, tree *sizeof_arg,
8545 vec<location_t> *locations,
8546 unsigned int *literal_zero_mask)
8548 vec<tree, va_gc> *ret;
8549 vec<tree, va_gc> *orig_types;
8550 struct c_expr expr;
8551 location_t loc = c_parser_peek_token (parser)->location;
8552 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8553 unsigned int idx = 0;
8555 ret = make_tree_vector ();
8556 if (p_orig_types == NULL)
8557 orig_types = NULL;
8558 else
8559 orig_types = make_tree_vector ();
8561 if (sizeof_arg != NULL
8562 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8563 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8564 if (literal_zero_mask)
8565 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8566 expr = c_parser_expr_no_commas (parser, NULL);
8567 if (convert_p)
8568 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8569 if (fold_p)
8570 expr.value = c_fully_fold (expr.value, false, NULL);
8571 ret->quick_push (expr.value);
8572 if (orig_types)
8573 orig_types->quick_push (expr.original_type);
8574 if (locations)
8575 locations->safe_push (loc);
8576 if (sizeof_arg != NULL
8577 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8578 && expr.original_code == SIZEOF_EXPR)
8580 sizeof_arg[0] = c_last_sizeof_arg;
8581 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8583 while (c_parser_next_token_is (parser, CPP_COMMA))
8585 c_parser_consume_token (parser);
8586 loc = c_parser_peek_token (parser)->location;
8587 if (sizeof_arg != NULL
8588 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8589 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8590 else
8591 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8592 if (literal_zero_mask)
8593 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8594 expr = c_parser_expr_no_commas (parser, NULL);
8595 if (convert_p)
8596 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8597 if (fold_p)
8598 expr.value = c_fully_fold (expr.value, false, NULL);
8599 vec_safe_push (ret, expr.value);
8600 if (orig_types)
8601 vec_safe_push (orig_types, expr.original_type);
8602 if (locations)
8603 locations->safe_push (loc);
8604 if (++idx < 3
8605 && sizeof_arg != NULL
8606 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8607 && expr.original_code == SIZEOF_EXPR)
8609 sizeof_arg[idx] = c_last_sizeof_arg;
8610 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8613 if (orig_types)
8614 *p_orig_types = orig_types;
8615 return ret;
8618 /* Parse Objective-C-specific constructs. */
8620 /* Parse an objc-class-definition.
8622 objc-class-definition:
8623 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8624 objc-class-instance-variables[opt] objc-methodprotolist @end
8625 @implementation identifier objc-superclass[opt]
8626 objc-class-instance-variables[opt]
8627 @interface identifier ( identifier ) objc-protocol-refs[opt]
8628 objc-methodprotolist @end
8629 @interface identifier ( ) objc-protocol-refs[opt]
8630 objc-methodprotolist @end
8631 @implementation identifier ( identifier )
8633 objc-superclass:
8634 : identifier
8636 "@interface identifier (" must start "@interface identifier (
8637 identifier ) ...": objc-methodprotolist in the first production may
8638 not start with a parenthesized identifier as a declarator of a data
8639 definition with no declaration specifiers if the objc-superclass,
8640 objc-protocol-refs and objc-class-instance-variables are omitted. */
8642 static void
8643 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8645 bool iface_p;
8646 tree id1;
8647 tree superclass;
8648 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8649 iface_p = true;
8650 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8651 iface_p = false;
8652 else
8653 gcc_unreachable ();
8655 c_parser_consume_token (parser);
8656 if (c_parser_next_token_is_not (parser, CPP_NAME))
8658 c_parser_error (parser, "expected identifier");
8659 return;
8661 id1 = c_parser_peek_token (parser)->value;
8662 c_parser_consume_token (parser);
8663 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8665 /* We have a category or class extension. */
8666 tree id2;
8667 tree proto = NULL_TREE;
8668 c_parser_consume_token (parser);
8669 if (c_parser_next_token_is_not (parser, CPP_NAME))
8671 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8673 /* We have a class extension. */
8674 id2 = NULL_TREE;
8676 else
8678 c_parser_error (parser, "expected identifier or %<)%>");
8679 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8680 return;
8683 else
8685 id2 = c_parser_peek_token (parser)->value;
8686 c_parser_consume_token (parser);
8688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8689 if (!iface_p)
8691 objc_start_category_implementation (id1, id2);
8692 return;
8694 if (c_parser_next_token_is (parser, CPP_LESS))
8695 proto = c_parser_objc_protocol_refs (parser);
8696 objc_start_category_interface (id1, id2, proto, attributes);
8697 c_parser_objc_methodprotolist (parser);
8698 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8699 objc_finish_interface ();
8700 return;
8702 if (c_parser_next_token_is (parser, CPP_COLON))
8704 c_parser_consume_token (parser);
8705 if (c_parser_next_token_is_not (parser, CPP_NAME))
8707 c_parser_error (parser, "expected identifier");
8708 return;
8710 superclass = c_parser_peek_token (parser)->value;
8711 c_parser_consume_token (parser);
8713 else
8714 superclass = NULL_TREE;
8715 if (iface_p)
8717 tree proto = NULL_TREE;
8718 if (c_parser_next_token_is (parser, CPP_LESS))
8719 proto = c_parser_objc_protocol_refs (parser);
8720 objc_start_class_interface (id1, superclass, proto, attributes);
8722 else
8723 objc_start_class_implementation (id1, superclass);
8724 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8725 c_parser_objc_class_instance_variables (parser);
8726 if (iface_p)
8728 objc_continue_interface ();
8729 c_parser_objc_methodprotolist (parser);
8730 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8731 objc_finish_interface ();
8733 else
8735 objc_continue_implementation ();
8736 return;
8740 /* Parse objc-class-instance-variables.
8742 objc-class-instance-variables:
8743 { objc-instance-variable-decl-list[opt] }
8745 objc-instance-variable-decl-list:
8746 objc-visibility-spec
8747 objc-instance-variable-decl ;
8749 objc-instance-variable-decl-list objc-visibility-spec
8750 objc-instance-variable-decl-list objc-instance-variable-decl ;
8751 objc-instance-variable-decl-list ;
8753 objc-visibility-spec:
8754 @private
8755 @protected
8756 @public
8758 objc-instance-variable-decl:
8759 struct-declaration
8762 static void
8763 c_parser_objc_class_instance_variables (c_parser *parser)
8765 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8766 c_parser_consume_token (parser);
8767 while (c_parser_next_token_is_not (parser, CPP_EOF))
8769 tree decls;
8770 /* Parse any stray semicolon. */
8771 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8773 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8774 "extra semicolon");
8775 c_parser_consume_token (parser);
8776 continue;
8778 /* Stop if at the end of the instance variables. */
8779 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8781 c_parser_consume_token (parser);
8782 break;
8784 /* Parse any objc-visibility-spec. */
8785 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8787 c_parser_consume_token (parser);
8788 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8789 continue;
8791 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8793 c_parser_consume_token (parser);
8794 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8795 continue;
8797 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8799 c_parser_consume_token (parser);
8800 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8801 continue;
8803 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8805 c_parser_consume_token (parser);
8806 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8807 continue;
8809 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8811 c_parser_pragma (parser, pragma_external, NULL);
8812 continue;
8815 /* Parse some comma-separated declarations. */
8816 decls = c_parser_struct_declaration (parser);
8817 if (decls == NULL)
8819 /* There is a syntax error. We want to skip the offending
8820 tokens up to the next ';' (included) or '}'
8821 (excluded). */
8823 /* First, skip manually a ')' or ']'. This is because they
8824 reduce the nesting level, so c_parser_skip_until_found()
8825 wouldn't be able to skip past them. */
8826 c_token *token = c_parser_peek_token (parser);
8827 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8828 c_parser_consume_token (parser);
8830 /* Then, do the standard skipping. */
8831 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8833 /* We hopefully recovered. Start normal parsing again. */
8834 parser->error = false;
8835 continue;
8837 else
8839 /* Comma-separated instance variables are chained together
8840 in reverse order; add them one by one. */
8841 tree ivar = nreverse (decls);
8842 for (; ivar; ivar = DECL_CHAIN (ivar))
8843 objc_add_instance_variable (copy_node (ivar));
8845 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8849 /* Parse an objc-class-declaration.
8851 objc-class-declaration:
8852 @class identifier-list ;
8855 static void
8856 c_parser_objc_class_declaration (c_parser *parser)
8858 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8859 c_parser_consume_token (parser);
8860 /* Any identifiers, including those declared as type names, are OK
8861 here. */
8862 while (true)
8864 tree id;
8865 if (c_parser_next_token_is_not (parser, CPP_NAME))
8867 c_parser_error (parser, "expected identifier");
8868 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8869 parser->error = false;
8870 return;
8872 id = c_parser_peek_token (parser)->value;
8873 objc_declare_class (id);
8874 c_parser_consume_token (parser);
8875 if (c_parser_next_token_is (parser, CPP_COMMA))
8876 c_parser_consume_token (parser);
8877 else
8878 break;
8880 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8883 /* Parse an objc-alias-declaration.
8885 objc-alias-declaration:
8886 @compatibility_alias identifier identifier ;
8889 static void
8890 c_parser_objc_alias_declaration (c_parser *parser)
8892 tree id1, id2;
8893 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8894 c_parser_consume_token (parser);
8895 if (c_parser_next_token_is_not (parser, CPP_NAME))
8897 c_parser_error (parser, "expected identifier");
8898 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8899 return;
8901 id1 = c_parser_peek_token (parser)->value;
8902 c_parser_consume_token (parser);
8903 if (c_parser_next_token_is_not (parser, CPP_NAME))
8905 c_parser_error (parser, "expected identifier");
8906 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8907 return;
8909 id2 = c_parser_peek_token (parser)->value;
8910 c_parser_consume_token (parser);
8911 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8912 objc_declare_alias (id1, id2);
8915 /* Parse an objc-protocol-definition.
8917 objc-protocol-definition:
8918 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8919 @protocol identifier-list ;
8921 "@protocol identifier ;" should be resolved as "@protocol
8922 identifier-list ;": objc-methodprotolist may not start with a
8923 semicolon in the first alternative if objc-protocol-refs are
8924 omitted. */
8926 static void
8927 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8929 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8931 c_parser_consume_token (parser);
8932 if (c_parser_next_token_is_not (parser, CPP_NAME))
8934 c_parser_error (parser, "expected identifier");
8935 return;
8937 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8938 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8940 /* Any identifiers, including those declared as type names, are
8941 OK here. */
8942 while (true)
8944 tree id;
8945 if (c_parser_next_token_is_not (parser, CPP_NAME))
8947 c_parser_error (parser, "expected identifier");
8948 break;
8950 id = c_parser_peek_token (parser)->value;
8951 objc_declare_protocol (id, attributes);
8952 c_parser_consume_token (parser);
8953 if (c_parser_next_token_is (parser, CPP_COMMA))
8954 c_parser_consume_token (parser);
8955 else
8956 break;
8958 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8960 else
8962 tree id = c_parser_peek_token (parser)->value;
8963 tree proto = NULL_TREE;
8964 c_parser_consume_token (parser);
8965 if (c_parser_next_token_is (parser, CPP_LESS))
8966 proto = c_parser_objc_protocol_refs (parser);
8967 parser->objc_pq_context = true;
8968 objc_start_protocol (id, proto, attributes);
8969 c_parser_objc_methodprotolist (parser);
8970 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8971 parser->objc_pq_context = false;
8972 objc_finish_interface ();
8976 /* Parse an objc-method-type.
8978 objc-method-type:
8982 Return true if it is a class method (+) and false if it is
8983 an instance method (-).
8985 static inline bool
8986 c_parser_objc_method_type (c_parser *parser)
8988 switch (c_parser_peek_token (parser)->type)
8990 case CPP_PLUS:
8991 c_parser_consume_token (parser);
8992 return true;
8993 case CPP_MINUS:
8994 c_parser_consume_token (parser);
8995 return false;
8996 default:
8997 gcc_unreachable ();
9001 /* Parse an objc-method-definition.
9003 objc-method-definition:
9004 objc-method-type objc-method-decl ;[opt] compound-statement
9007 static void
9008 c_parser_objc_method_definition (c_parser *parser)
9010 bool is_class_method = c_parser_objc_method_type (parser);
9011 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9012 parser->objc_pq_context = true;
9013 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9014 &expr);
9015 if (decl == error_mark_node)
9016 return; /* Bail here. */
9018 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9020 c_parser_consume_token (parser);
9021 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9022 "extra semicolon in method definition specified");
9025 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9027 c_parser_error (parser, "expected %<{%>");
9028 return;
9031 parser->objc_pq_context = false;
9032 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9034 add_stmt (c_parser_compound_statement (parser));
9035 objc_finish_method_definition (current_function_decl);
9037 else
9039 /* This code is executed when we find a method definition
9040 outside of an @implementation context (or invalid for other
9041 reasons). Parse the method (to keep going) but do not emit
9042 any code.
9044 c_parser_compound_statement (parser);
9048 /* Parse an objc-methodprotolist.
9050 objc-methodprotolist:
9051 empty
9052 objc-methodprotolist objc-methodproto
9053 objc-methodprotolist declaration
9054 objc-methodprotolist ;
9055 @optional
9056 @required
9058 The declaration is a data definition, which may be missing
9059 declaration specifiers under the same rules and diagnostics as
9060 other data definitions outside functions, and the stray semicolon
9061 is diagnosed the same way as a stray semicolon outside a
9062 function. */
9064 static void
9065 c_parser_objc_methodprotolist (c_parser *parser)
9067 while (true)
9069 /* The list is terminated by @end. */
9070 switch (c_parser_peek_token (parser)->type)
9072 case CPP_SEMICOLON:
9073 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9074 "ISO C does not allow extra %<;%> outside of a function");
9075 c_parser_consume_token (parser);
9076 break;
9077 case CPP_PLUS:
9078 case CPP_MINUS:
9079 c_parser_objc_methodproto (parser);
9080 break;
9081 case CPP_PRAGMA:
9082 c_parser_pragma (parser, pragma_external, NULL);
9083 break;
9084 case CPP_EOF:
9085 return;
9086 default:
9087 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9088 return;
9089 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9090 c_parser_objc_at_property_declaration (parser);
9091 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9093 objc_set_method_opt (true);
9094 c_parser_consume_token (parser);
9096 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9098 objc_set_method_opt (false);
9099 c_parser_consume_token (parser);
9101 else
9102 c_parser_declaration_or_fndef (parser, false, false, true,
9103 false, true, NULL, vNULL);
9104 break;
9109 /* Parse an objc-methodproto.
9111 objc-methodproto:
9112 objc-method-type objc-method-decl ;
9115 static void
9116 c_parser_objc_methodproto (c_parser *parser)
9118 bool is_class_method = c_parser_objc_method_type (parser);
9119 tree decl, attributes = NULL_TREE;
9121 /* Remember protocol qualifiers in prototypes. */
9122 parser->objc_pq_context = true;
9123 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9124 NULL);
9125 /* Forget protocol qualifiers now. */
9126 parser->objc_pq_context = false;
9128 /* Do not allow the presence of attributes to hide an erroneous
9129 method implementation in the interface section. */
9130 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9132 c_parser_error (parser, "expected %<;%>");
9133 return;
9136 if (decl != error_mark_node)
9137 objc_add_method_declaration (is_class_method, decl, attributes);
9139 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9142 /* If we are at a position that method attributes may be present, check that
9143 there are not any parsed already (a syntax error) and then collect any
9144 specified at the current location. Finally, if new attributes were present,
9145 check that the next token is legal ( ';' for decls and '{' for defs). */
9147 static bool
9148 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9150 bool bad = false;
9151 if (*attributes)
9153 c_parser_error (parser,
9154 "method attributes must be specified at the end only");
9155 *attributes = NULL_TREE;
9156 bad = true;
9159 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9160 *attributes = c_parser_attributes (parser);
9162 /* If there were no attributes here, just report any earlier error. */
9163 if (*attributes == NULL_TREE || bad)
9164 return bad;
9166 /* If the attributes are followed by a ; or {, then just report any earlier
9167 error. */
9168 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9169 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9170 return bad;
9172 /* We've got attributes, but not at the end. */
9173 c_parser_error (parser,
9174 "expected %<;%> or %<{%> after method attribute definition");
9175 return true;
9178 /* Parse an objc-method-decl.
9180 objc-method-decl:
9181 ( objc-type-name ) objc-selector
9182 objc-selector
9183 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9184 objc-keyword-selector objc-optparmlist
9185 attributes
9187 objc-keyword-selector:
9188 objc-keyword-decl
9189 objc-keyword-selector objc-keyword-decl
9191 objc-keyword-decl:
9192 objc-selector : ( objc-type-name ) identifier
9193 objc-selector : identifier
9194 : ( objc-type-name ) identifier
9195 : identifier
9197 objc-optparmlist:
9198 objc-optparms objc-optellipsis
9200 objc-optparms:
9201 empty
9202 objc-opt-parms , parameter-declaration
9204 objc-optellipsis:
9205 empty
9206 , ...
9209 static tree
9210 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9211 tree *attributes, tree *expr)
9213 tree type = NULL_TREE;
9214 tree sel;
9215 tree parms = NULL_TREE;
9216 bool ellipsis = false;
9217 bool attr_err = false;
9219 *attributes = NULL_TREE;
9220 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9222 c_parser_consume_token (parser);
9223 type = c_parser_objc_type_name (parser);
9224 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9226 sel = c_parser_objc_selector (parser);
9227 /* If there is no selector, or a colon follows, we have an
9228 objc-keyword-selector. If there is a selector, and a colon does
9229 not follow, that selector ends the objc-method-decl. */
9230 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9232 tree tsel = sel;
9233 tree list = NULL_TREE;
9234 while (true)
9236 tree atype = NULL_TREE, id, keyworddecl;
9237 tree param_attr = NULL_TREE;
9238 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9239 break;
9240 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9242 c_parser_consume_token (parser);
9243 atype = c_parser_objc_type_name (parser);
9244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9245 "expected %<)%>");
9247 /* New ObjC allows attributes on method parameters. */
9248 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9249 param_attr = c_parser_attributes (parser);
9250 if (c_parser_next_token_is_not (parser, CPP_NAME))
9252 c_parser_error (parser, "expected identifier");
9253 return error_mark_node;
9255 id = c_parser_peek_token (parser)->value;
9256 c_parser_consume_token (parser);
9257 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9258 list = chainon (list, keyworddecl);
9259 tsel = c_parser_objc_selector (parser);
9260 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9261 break;
9264 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9266 /* Parse the optional parameter list. Optional Objective-C
9267 method parameters follow the C syntax, and may include '...'
9268 to denote a variable number of arguments. */
9269 parms = make_node (TREE_LIST);
9270 while (c_parser_next_token_is (parser, CPP_COMMA))
9272 struct c_parm *parm;
9273 c_parser_consume_token (parser);
9274 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9276 ellipsis = true;
9277 c_parser_consume_token (parser);
9278 attr_err |= c_parser_objc_maybe_method_attributes
9279 (parser, attributes) ;
9280 break;
9282 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9283 if (parm == NULL)
9284 break;
9285 parms = chainon (parms,
9286 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9288 sel = list;
9290 else
9291 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9293 if (sel == NULL)
9295 c_parser_error (parser, "objective-c method declaration is expected");
9296 return error_mark_node;
9299 if (attr_err)
9300 return error_mark_node;
9302 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9305 /* Parse an objc-type-name.
9307 objc-type-name:
9308 objc-type-qualifiers[opt] type-name
9309 objc-type-qualifiers[opt]
9311 objc-type-qualifiers:
9312 objc-type-qualifier
9313 objc-type-qualifiers objc-type-qualifier
9315 objc-type-qualifier: one of
9316 in out inout bycopy byref oneway
9319 static tree
9320 c_parser_objc_type_name (c_parser *parser)
9322 tree quals = NULL_TREE;
9323 struct c_type_name *type_name = NULL;
9324 tree type = NULL_TREE;
9325 while (true)
9327 c_token *token = c_parser_peek_token (parser);
9328 if (token->type == CPP_KEYWORD
9329 && (token->keyword == RID_IN
9330 || token->keyword == RID_OUT
9331 || token->keyword == RID_INOUT
9332 || token->keyword == RID_BYCOPY
9333 || token->keyword == RID_BYREF
9334 || token->keyword == RID_ONEWAY))
9336 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9337 c_parser_consume_token (parser);
9339 else
9340 break;
9342 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9343 type_name = c_parser_type_name (parser);
9344 if (type_name)
9345 type = groktypename (type_name, NULL, NULL);
9347 /* If the type is unknown, and error has already been produced and
9348 we need to recover from the error. In that case, use NULL_TREE
9349 for the type, as if no type had been specified; this will use the
9350 default type ('id') which is good for error recovery. */
9351 if (type == error_mark_node)
9352 type = NULL_TREE;
9354 return build_tree_list (quals, type);
9357 /* Parse objc-protocol-refs.
9359 objc-protocol-refs:
9360 < identifier-list >
9363 static tree
9364 c_parser_objc_protocol_refs (c_parser *parser)
9366 tree list = NULL_TREE;
9367 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9368 c_parser_consume_token (parser);
9369 /* Any identifiers, including those declared as type names, are OK
9370 here. */
9371 while (true)
9373 tree id;
9374 if (c_parser_next_token_is_not (parser, CPP_NAME))
9376 c_parser_error (parser, "expected identifier");
9377 break;
9379 id = c_parser_peek_token (parser)->value;
9380 list = chainon (list, build_tree_list (NULL_TREE, id));
9381 c_parser_consume_token (parser);
9382 if (c_parser_next_token_is (parser, CPP_COMMA))
9383 c_parser_consume_token (parser);
9384 else
9385 break;
9387 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9388 return list;
9391 /* Parse an objc-try-catch-finally-statement.
9393 objc-try-catch-finally-statement:
9394 @try compound-statement objc-catch-list[opt]
9395 @try compound-statement objc-catch-list[opt] @finally compound-statement
9397 objc-catch-list:
9398 @catch ( objc-catch-parameter-declaration ) compound-statement
9399 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9401 objc-catch-parameter-declaration:
9402 parameter-declaration
9403 '...'
9405 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9407 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9408 for C++. Keep them in sync. */
9410 static void
9411 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9413 location_t location;
9414 tree stmt;
9416 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9417 c_parser_consume_token (parser);
9418 location = c_parser_peek_token (parser)->location;
9419 objc_maybe_warn_exceptions (location);
9420 stmt = c_parser_compound_statement (parser);
9421 objc_begin_try_stmt (location, stmt);
9423 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9425 struct c_parm *parm;
9426 tree parameter_declaration = error_mark_node;
9427 bool seen_open_paren = false;
9429 c_parser_consume_token (parser);
9430 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9431 seen_open_paren = true;
9432 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9434 /* We have "@catch (...)" (where the '...' are literally
9435 what is in the code). Skip the '...'.
9436 parameter_declaration is set to NULL_TREE, and
9437 objc_being_catch_clauses() knows that that means
9438 '...'. */
9439 c_parser_consume_token (parser);
9440 parameter_declaration = NULL_TREE;
9442 else
9444 /* We have "@catch (NSException *exception)" or something
9445 like that. Parse the parameter declaration. */
9446 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9447 if (parm == NULL)
9448 parameter_declaration = error_mark_node;
9449 else
9450 parameter_declaration = grokparm (parm, NULL);
9452 if (seen_open_paren)
9453 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9454 else
9456 /* If there was no open parenthesis, we are recovering from
9457 an error, and we are trying to figure out what mistake
9458 the user has made. */
9460 /* If there is an immediate closing parenthesis, the user
9461 probably forgot the opening one (ie, they typed "@catch
9462 NSException *e)". Parse the closing parenthesis and keep
9463 going. */
9464 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9465 c_parser_consume_token (parser);
9467 /* If these is no immediate closing parenthesis, the user
9468 probably doesn't know that parenthesis are required at
9469 all (ie, they typed "@catch NSException *e"). So, just
9470 forget about the closing parenthesis and keep going. */
9472 objc_begin_catch_clause (parameter_declaration);
9473 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9474 c_parser_compound_statement_nostart (parser);
9475 objc_finish_catch_clause ();
9477 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9479 c_parser_consume_token (parser);
9480 location = c_parser_peek_token (parser)->location;
9481 stmt = c_parser_compound_statement (parser);
9482 objc_build_finally_clause (location, stmt);
9484 objc_finish_try_stmt ();
9487 /* Parse an objc-synchronized-statement.
9489 objc-synchronized-statement:
9490 @synchronized ( expression ) compound-statement
9493 static void
9494 c_parser_objc_synchronized_statement (c_parser *parser)
9496 location_t loc;
9497 tree expr, stmt;
9498 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9499 c_parser_consume_token (parser);
9500 loc = c_parser_peek_token (parser)->location;
9501 objc_maybe_warn_exceptions (loc);
9502 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9504 struct c_expr ce = c_parser_expression (parser);
9505 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9506 expr = ce.value;
9507 expr = c_fully_fold (expr, false, NULL);
9508 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9510 else
9511 expr = error_mark_node;
9512 stmt = c_parser_compound_statement (parser);
9513 objc_build_synchronized (loc, expr, stmt);
9516 /* Parse an objc-selector; return NULL_TREE without an error if the
9517 next token is not an objc-selector.
9519 objc-selector:
9520 identifier
9521 one of
9522 enum struct union if else while do for switch case default
9523 break continue return goto asm sizeof typeof __alignof
9524 unsigned long const short volatile signed restrict _Complex
9525 in out inout bycopy byref oneway int char float double void _Bool
9526 _Atomic
9528 ??? Why this selection of keywords but not, for example, storage
9529 class specifiers? */
9531 static tree
9532 c_parser_objc_selector (c_parser *parser)
9534 c_token *token = c_parser_peek_token (parser);
9535 tree value = token->value;
9536 if (token->type == CPP_NAME)
9538 c_parser_consume_token (parser);
9539 return value;
9541 if (token->type != CPP_KEYWORD)
9542 return NULL_TREE;
9543 switch (token->keyword)
9545 case RID_ENUM:
9546 case RID_STRUCT:
9547 case RID_UNION:
9548 case RID_IF:
9549 case RID_ELSE:
9550 case RID_WHILE:
9551 case RID_DO:
9552 case RID_FOR:
9553 case RID_SWITCH:
9554 case RID_CASE:
9555 case RID_DEFAULT:
9556 case RID_BREAK:
9557 case RID_CONTINUE:
9558 case RID_RETURN:
9559 case RID_GOTO:
9560 case RID_ASM:
9561 case RID_SIZEOF:
9562 case RID_TYPEOF:
9563 case RID_ALIGNOF:
9564 case RID_UNSIGNED:
9565 case RID_LONG:
9566 case RID_CONST:
9567 case RID_SHORT:
9568 case RID_VOLATILE:
9569 case RID_SIGNED:
9570 case RID_RESTRICT:
9571 case RID_COMPLEX:
9572 case RID_IN:
9573 case RID_OUT:
9574 case RID_INOUT:
9575 case RID_BYCOPY:
9576 case RID_BYREF:
9577 case RID_ONEWAY:
9578 case RID_INT:
9579 case RID_CHAR:
9580 case RID_FLOAT:
9581 case RID_DOUBLE:
9582 case RID_VOID:
9583 case RID_BOOL:
9584 case RID_ATOMIC:
9585 case RID_AUTO_TYPE:
9586 case RID_INT_N_0:
9587 case RID_INT_N_1:
9588 case RID_INT_N_2:
9589 case RID_INT_N_3:
9590 c_parser_consume_token (parser);
9591 return value;
9592 default:
9593 return NULL_TREE;
9597 /* Parse an objc-selector-arg.
9599 objc-selector-arg:
9600 objc-selector
9601 objc-keywordname-list
9603 objc-keywordname-list:
9604 objc-keywordname
9605 objc-keywordname-list objc-keywordname
9607 objc-keywordname:
9608 objc-selector :
9612 static tree
9613 c_parser_objc_selector_arg (c_parser *parser)
9615 tree sel = c_parser_objc_selector (parser);
9616 tree list = NULL_TREE;
9617 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9618 return sel;
9619 while (true)
9621 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9622 return list;
9623 list = chainon (list, build_tree_list (sel, NULL_TREE));
9624 sel = c_parser_objc_selector (parser);
9625 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9626 break;
9628 return list;
9631 /* Parse an objc-receiver.
9633 objc-receiver:
9634 expression
9635 class-name
9636 type-name
9639 static tree
9640 c_parser_objc_receiver (c_parser *parser)
9642 location_t loc = c_parser_peek_token (parser)->location;
9644 if (c_parser_peek_token (parser)->type == CPP_NAME
9645 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9646 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9648 tree id = c_parser_peek_token (parser)->value;
9649 c_parser_consume_token (parser);
9650 return objc_get_class_reference (id);
9652 struct c_expr ce = c_parser_expression (parser);
9653 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9654 return c_fully_fold (ce.value, false, NULL);
9657 /* Parse objc-message-args.
9659 objc-message-args:
9660 objc-selector
9661 objc-keywordarg-list
9663 objc-keywordarg-list:
9664 objc-keywordarg
9665 objc-keywordarg-list objc-keywordarg
9667 objc-keywordarg:
9668 objc-selector : objc-keywordexpr
9669 : objc-keywordexpr
9672 static tree
9673 c_parser_objc_message_args (c_parser *parser)
9675 tree sel = c_parser_objc_selector (parser);
9676 tree list = NULL_TREE;
9677 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9678 return sel;
9679 while (true)
9681 tree keywordexpr;
9682 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9683 return error_mark_node;
9684 keywordexpr = c_parser_objc_keywordexpr (parser);
9685 list = chainon (list, build_tree_list (sel, keywordexpr));
9686 sel = c_parser_objc_selector (parser);
9687 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9688 break;
9690 return list;
9693 /* Parse an objc-keywordexpr.
9695 objc-keywordexpr:
9696 nonempty-expr-list
9699 static tree
9700 c_parser_objc_keywordexpr (c_parser *parser)
9702 tree ret;
9703 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9704 NULL, NULL, NULL, NULL);
9705 if (vec_safe_length (expr_list) == 1)
9707 /* Just return the expression, remove a level of
9708 indirection. */
9709 ret = (*expr_list)[0];
9711 else
9713 /* We have a comma expression, we will collapse later. */
9714 ret = build_tree_list_vec (expr_list);
9716 release_tree_vector (expr_list);
9717 return ret;
9720 /* A check, needed in several places, that ObjC interface, implementation or
9721 method definitions are not prefixed by incorrect items. */
9722 static bool
9723 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9724 struct c_declspecs *specs)
9726 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9727 || specs->typespec_kind != ctsk_none)
9729 c_parser_error (parser,
9730 "no type or storage class may be specified here,");
9731 c_parser_skip_to_end_of_block_or_statement (parser);
9732 return true;
9734 return false;
9737 /* Parse an Objective-C @property declaration. The syntax is:
9739 objc-property-declaration:
9740 '@property' objc-property-attributes[opt] struct-declaration ;
9742 objc-property-attributes:
9743 '(' objc-property-attribute-list ')'
9745 objc-property-attribute-list:
9746 objc-property-attribute
9747 objc-property-attribute-list, objc-property-attribute
9749 objc-property-attribute
9750 'getter' = identifier
9751 'setter' = identifier
9752 'readonly'
9753 'readwrite'
9754 'assign'
9755 'retain'
9756 'copy'
9757 'nonatomic'
9759 For example:
9760 @property NSString *name;
9761 @property (readonly) id object;
9762 @property (retain, nonatomic, getter=getTheName) id name;
9763 @property int a, b, c;
9765 PS: This function is identical to cp_parser_objc_at_propery_declaration
9766 for C++. Keep them in sync. */
9767 static void
9768 c_parser_objc_at_property_declaration (c_parser *parser)
9770 /* The following variables hold the attributes of the properties as
9771 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9772 seen. When we see an attribute, we set them to 'true' (if they
9773 are boolean properties) or to the identifier (if they have an
9774 argument, ie, for getter and setter). Note that here we only
9775 parse the list of attributes, check the syntax and accumulate the
9776 attributes that we find. objc_add_property_declaration() will
9777 then process the information. */
9778 bool property_assign = false;
9779 bool property_copy = false;
9780 tree property_getter_ident = NULL_TREE;
9781 bool property_nonatomic = false;
9782 bool property_readonly = false;
9783 bool property_readwrite = false;
9784 bool property_retain = false;
9785 tree property_setter_ident = NULL_TREE;
9787 /* 'properties' is the list of properties that we read. Usually a
9788 single one, but maybe more (eg, in "@property int a, b, c;" there
9789 are three). */
9790 tree properties;
9791 location_t loc;
9793 loc = c_parser_peek_token (parser)->location;
9794 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9796 c_parser_consume_token (parser); /* Eat '@property'. */
9798 /* Parse the optional attribute list... */
9799 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9801 /* Eat the '(' */
9802 c_parser_consume_token (parser);
9804 /* Property attribute keywords are valid now. */
9805 parser->objc_property_attr_context = true;
9807 while (true)
9809 bool syntax_error = false;
9810 c_token *token = c_parser_peek_token (parser);
9811 enum rid keyword;
9813 if (token->type != CPP_KEYWORD)
9815 if (token->type == CPP_CLOSE_PAREN)
9816 c_parser_error (parser, "expected identifier");
9817 else
9819 c_parser_consume_token (parser);
9820 c_parser_error (parser, "unknown property attribute");
9822 break;
9824 keyword = token->keyword;
9825 c_parser_consume_token (parser);
9826 switch (keyword)
9828 case RID_ASSIGN: property_assign = true; break;
9829 case RID_COPY: property_copy = true; break;
9830 case RID_NONATOMIC: property_nonatomic = true; break;
9831 case RID_READONLY: property_readonly = true; break;
9832 case RID_READWRITE: property_readwrite = true; break;
9833 case RID_RETAIN: property_retain = true; break;
9835 case RID_GETTER:
9836 case RID_SETTER:
9837 if (c_parser_next_token_is_not (parser, CPP_EQ))
9839 if (keyword == RID_GETTER)
9840 c_parser_error (parser,
9841 "missing %<=%> (after %<getter%> attribute)");
9842 else
9843 c_parser_error (parser,
9844 "missing %<=%> (after %<setter%> attribute)");
9845 syntax_error = true;
9846 break;
9848 c_parser_consume_token (parser); /* eat the = */
9849 if (c_parser_next_token_is_not (parser, CPP_NAME))
9851 c_parser_error (parser, "expected identifier");
9852 syntax_error = true;
9853 break;
9855 if (keyword == RID_SETTER)
9857 if (property_setter_ident != NULL_TREE)
9858 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9859 else
9860 property_setter_ident = c_parser_peek_token (parser)->value;
9861 c_parser_consume_token (parser);
9862 if (c_parser_next_token_is_not (parser, CPP_COLON))
9863 c_parser_error (parser, "setter name must terminate with %<:%>");
9864 else
9865 c_parser_consume_token (parser);
9867 else
9869 if (property_getter_ident != NULL_TREE)
9870 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9871 else
9872 property_getter_ident = c_parser_peek_token (parser)->value;
9873 c_parser_consume_token (parser);
9875 break;
9876 default:
9877 c_parser_error (parser, "unknown property attribute");
9878 syntax_error = true;
9879 break;
9882 if (syntax_error)
9883 break;
9885 if (c_parser_next_token_is (parser, CPP_COMMA))
9886 c_parser_consume_token (parser);
9887 else
9888 break;
9890 parser->objc_property_attr_context = false;
9891 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9893 /* ... and the property declaration(s). */
9894 properties = c_parser_struct_declaration (parser);
9896 if (properties == error_mark_node)
9898 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9899 parser->error = false;
9900 return;
9903 if (properties == NULL_TREE)
9904 c_parser_error (parser, "expected identifier");
9905 else
9907 /* Comma-separated properties are chained together in
9908 reverse order; add them one by one. */
9909 properties = nreverse (properties);
9911 for (; properties; properties = TREE_CHAIN (properties))
9912 objc_add_property_declaration (loc, copy_node (properties),
9913 property_readonly, property_readwrite,
9914 property_assign, property_retain,
9915 property_copy, property_nonatomic,
9916 property_getter_ident, property_setter_ident);
9919 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9920 parser->error = false;
9923 /* Parse an Objective-C @synthesize declaration. The syntax is:
9925 objc-synthesize-declaration:
9926 @synthesize objc-synthesize-identifier-list ;
9928 objc-synthesize-identifier-list:
9929 objc-synthesize-identifier
9930 objc-synthesize-identifier-list, objc-synthesize-identifier
9932 objc-synthesize-identifier
9933 identifier
9934 identifier = identifier
9936 For example:
9937 @synthesize MyProperty;
9938 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9940 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9941 for C++. Keep them in sync.
9943 static void
9944 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9946 tree list = NULL_TREE;
9947 location_t loc;
9948 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9949 loc = c_parser_peek_token (parser)->location;
9951 c_parser_consume_token (parser);
9952 while (true)
9954 tree property, ivar;
9955 if (c_parser_next_token_is_not (parser, CPP_NAME))
9957 c_parser_error (parser, "expected identifier");
9958 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9959 /* Once we find the semicolon, we can resume normal parsing.
9960 We have to reset parser->error manually because
9961 c_parser_skip_until_found() won't reset it for us if the
9962 next token is precisely a semicolon. */
9963 parser->error = false;
9964 return;
9966 property = c_parser_peek_token (parser)->value;
9967 c_parser_consume_token (parser);
9968 if (c_parser_next_token_is (parser, CPP_EQ))
9970 c_parser_consume_token (parser);
9971 if (c_parser_next_token_is_not (parser, CPP_NAME))
9973 c_parser_error (parser, "expected identifier");
9974 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9975 parser->error = false;
9976 return;
9978 ivar = c_parser_peek_token (parser)->value;
9979 c_parser_consume_token (parser);
9981 else
9982 ivar = NULL_TREE;
9983 list = chainon (list, build_tree_list (ivar, property));
9984 if (c_parser_next_token_is (parser, CPP_COMMA))
9985 c_parser_consume_token (parser);
9986 else
9987 break;
9989 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9990 objc_add_synthesize_declaration (loc, list);
9993 /* Parse an Objective-C @dynamic declaration. The syntax is:
9995 objc-dynamic-declaration:
9996 @dynamic identifier-list ;
9998 For example:
9999 @dynamic MyProperty;
10000 @dynamic MyProperty, AnotherProperty;
10002 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10003 for C++. Keep them in sync.
10005 static void
10006 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10008 tree list = NULL_TREE;
10009 location_t loc;
10010 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10011 loc = c_parser_peek_token (parser)->location;
10013 c_parser_consume_token (parser);
10014 while (true)
10016 tree property;
10017 if (c_parser_next_token_is_not (parser, CPP_NAME))
10019 c_parser_error (parser, "expected identifier");
10020 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10021 parser->error = false;
10022 return;
10024 property = c_parser_peek_token (parser)->value;
10025 list = chainon (list, build_tree_list (NULL_TREE, property));
10026 c_parser_consume_token (parser);
10027 if (c_parser_next_token_is (parser, CPP_COMMA))
10028 c_parser_consume_token (parser);
10029 else
10030 break;
10032 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10033 objc_add_dynamic_declaration (loc, list);
10037 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10038 should be considered, statements. ALLOW_STMT is true if we're within
10039 the context of a function and such pragmas are to be allowed. Returns
10040 true if we actually parsed such a pragma. */
10042 static bool
10043 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10045 unsigned int id;
10047 id = c_parser_peek_token (parser)->pragma_kind;
10048 gcc_assert (id != PRAGMA_NONE);
10050 switch (id)
10052 case PRAGMA_OACC_DECLARE:
10053 c_parser_oacc_declare (parser);
10054 return false;
10056 case PRAGMA_OACC_ENTER_DATA:
10057 c_parser_oacc_enter_exit_data (parser, true);
10058 return false;
10060 case PRAGMA_OACC_EXIT_DATA:
10061 c_parser_oacc_enter_exit_data (parser, false);
10062 return false;
10064 case PRAGMA_OACC_ROUTINE:
10065 c_parser_oacc_routine (parser, context);
10066 return false;
10068 case PRAGMA_OACC_UPDATE:
10069 if (context != pragma_compound)
10071 if (context == pragma_stmt)
10072 c_parser_error (parser, "%<#pragma acc update%> may only be "
10073 "used in compound statements");
10074 goto bad_stmt;
10076 c_parser_oacc_update (parser);
10077 return false;
10079 case PRAGMA_OMP_BARRIER:
10080 if (context != pragma_compound)
10082 if (context == pragma_stmt)
10083 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
10084 "used in compound statements");
10085 goto bad_stmt;
10087 c_parser_omp_barrier (parser);
10088 return false;
10090 case PRAGMA_OMP_FLUSH:
10091 if (context != pragma_compound)
10093 if (context == pragma_stmt)
10094 c_parser_error (parser, "%<#pragma omp flush%> may only be "
10095 "used in compound statements");
10096 goto bad_stmt;
10098 c_parser_omp_flush (parser);
10099 return false;
10101 case PRAGMA_OMP_TASKWAIT:
10102 if (context != pragma_compound)
10104 if (context == pragma_stmt)
10105 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
10106 "used in compound statements");
10107 goto bad_stmt;
10109 c_parser_omp_taskwait (parser);
10110 return false;
10112 case PRAGMA_OMP_TASKYIELD:
10113 if (context != pragma_compound)
10115 if (context == pragma_stmt)
10116 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
10117 "used in compound statements");
10118 goto bad_stmt;
10120 c_parser_omp_taskyield (parser);
10121 return false;
10123 case PRAGMA_OMP_CANCEL:
10124 if (context != pragma_compound)
10126 if (context == pragma_stmt)
10127 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
10128 "used in compound statements");
10129 goto bad_stmt;
10131 c_parser_omp_cancel (parser);
10132 return false;
10134 case PRAGMA_OMP_CANCELLATION_POINT:
10135 if (context != pragma_compound)
10137 if (context == pragma_stmt)
10138 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
10139 "only be used in compound statements");
10140 goto bad_stmt;
10142 c_parser_omp_cancellation_point (parser);
10143 return false;
10145 case PRAGMA_OMP_THREADPRIVATE:
10146 c_parser_omp_threadprivate (parser);
10147 return false;
10149 case PRAGMA_OMP_TARGET:
10150 return c_parser_omp_target (parser, context, if_p);
10152 case PRAGMA_OMP_END_DECLARE_TARGET:
10153 c_parser_omp_end_declare_target (parser);
10154 return false;
10156 case PRAGMA_OMP_SECTION:
10157 error_at (c_parser_peek_token (parser)->location,
10158 "%<#pragma omp section%> may only be used in "
10159 "%<#pragma omp sections%> construct");
10160 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10161 return false;
10163 case PRAGMA_OMP_DECLARE_REDUCTION:
10164 c_parser_omp_declare (parser, context);
10165 return false;
10167 case PRAGMA_OMP_ORDERED:
10168 return c_parser_omp_ordered (parser, context, if_p);
10170 case PRAGMA_IVDEP:
10171 c_parser_consume_pragma (parser);
10172 c_parser_skip_to_pragma_eol (parser);
10173 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10174 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10175 && !c_parser_next_token_is_keyword (parser, RID_DO))
10177 c_parser_error (parser, "for, while or do statement expected");
10178 return false;
10180 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10181 c_parser_for_statement (parser, true, if_p);
10182 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10183 c_parser_while_statement (parser, true, if_p);
10184 else
10185 c_parser_do_statement (parser, true);
10186 return false;
10188 case PRAGMA_GCC_PCH_PREPROCESS:
10189 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10190 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10191 return false;
10193 case PRAGMA_CILK_SIMD:
10194 if (!c_parser_cilk_verify_simd (parser, context))
10195 return false;
10196 c_parser_consume_pragma (parser);
10197 c_parser_cilk_simd (parser, if_p);
10198 return false;
10199 case PRAGMA_CILK_GRAINSIZE:
10200 if (!flag_cilkplus)
10202 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10203 " enabled");
10204 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10205 return false;
10207 if (context == pragma_external)
10209 error_at (c_parser_peek_token (parser)->location,
10210 "%<#pragma grainsize%> must be inside a function");
10211 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10212 return false;
10214 c_parser_cilk_grainsize (parser, if_p);
10215 return false;
10217 default:
10218 if (id < PRAGMA_FIRST_EXTERNAL)
10220 if (context != pragma_stmt && context != pragma_compound)
10222 bad_stmt:
10223 c_parser_error (parser, "expected declaration specifiers");
10224 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10225 return false;
10227 c_parser_omp_construct (parser, if_p);
10228 return true;
10230 break;
10233 c_parser_consume_pragma (parser);
10234 c_invoke_pragma_handler (id);
10236 /* Skip to EOL, but suppress any error message. Those will have been
10237 generated by the handler routine through calling error, as opposed
10238 to calling c_parser_error. */
10239 parser->error = true;
10240 c_parser_skip_to_pragma_eol (parser);
10242 return false;
10245 /* The interface the pragma parsers have to the lexer. */
10247 enum cpp_ttype
10248 pragma_lex (tree *value, location_t *loc)
10250 c_token *tok = c_parser_peek_token (the_parser);
10251 enum cpp_ttype ret = tok->type;
10253 *value = tok->value;
10254 if (loc)
10255 *loc = tok->location;
10257 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10258 ret = CPP_EOF;
10259 else
10261 if (ret == CPP_KEYWORD)
10262 ret = CPP_NAME;
10263 c_parser_consume_token (the_parser);
10266 return ret;
10269 static void
10270 c_parser_pragma_pch_preprocess (c_parser *parser)
10272 tree name = NULL;
10274 c_parser_consume_pragma (parser);
10275 if (c_parser_next_token_is (parser, CPP_STRING))
10277 name = c_parser_peek_token (parser)->value;
10278 c_parser_consume_token (parser);
10280 else
10281 c_parser_error (parser, "expected string literal");
10282 c_parser_skip_to_pragma_eol (parser);
10284 if (name)
10285 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10288 /* OpenACC and OpenMP parsing routines. */
10290 /* Returns name of the next clause.
10291 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10292 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10293 returned and the token is consumed. */
10295 static pragma_omp_clause
10296 c_parser_omp_clause_name (c_parser *parser)
10298 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10300 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10301 result = PRAGMA_OACC_CLAUSE_AUTO;
10302 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10303 result = PRAGMA_OMP_CLAUSE_IF;
10304 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10305 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10306 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10307 result = PRAGMA_OMP_CLAUSE_FOR;
10308 else if (c_parser_next_token_is (parser, CPP_NAME))
10310 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10312 switch (p[0])
10314 case 'a':
10315 if (!strcmp ("aligned", p))
10316 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10317 else if (!strcmp ("async", p))
10318 result = PRAGMA_OACC_CLAUSE_ASYNC;
10319 break;
10320 case 'c':
10321 if (!strcmp ("collapse", p))
10322 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10323 else if (!strcmp ("copy", p))
10324 result = PRAGMA_OACC_CLAUSE_COPY;
10325 else if (!strcmp ("copyin", p))
10326 result = PRAGMA_OMP_CLAUSE_COPYIN;
10327 else if (!strcmp ("copyout", p))
10328 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10329 else if (!strcmp ("copyprivate", p))
10330 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10331 else if (!strcmp ("create", p))
10332 result = PRAGMA_OACC_CLAUSE_CREATE;
10333 break;
10334 case 'd':
10335 if (!strcmp ("defaultmap", p))
10336 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10337 else if (!strcmp ("delete", p))
10338 result = PRAGMA_OACC_CLAUSE_DELETE;
10339 else if (!strcmp ("depend", p))
10340 result = PRAGMA_OMP_CLAUSE_DEPEND;
10341 else if (!strcmp ("device", p))
10342 result = PRAGMA_OMP_CLAUSE_DEVICE;
10343 else if (!strcmp ("deviceptr", p))
10344 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10345 else if (!strcmp ("device_resident", p))
10346 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10347 else if (!strcmp ("dist_schedule", p))
10348 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10349 break;
10350 case 'f':
10351 if (!strcmp ("final", p))
10352 result = PRAGMA_OMP_CLAUSE_FINAL;
10353 else if (!strcmp ("firstprivate", p))
10354 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10355 else if (!strcmp ("from", p))
10356 result = PRAGMA_OMP_CLAUSE_FROM;
10357 break;
10358 case 'g':
10359 if (!strcmp ("gang", p))
10360 result = PRAGMA_OACC_CLAUSE_GANG;
10361 else if (!strcmp ("grainsize", p))
10362 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10363 break;
10364 case 'h':
10365 if (!strcmp ("hint", p))
10366 result = PRAGMA_OMP_CLAUSE_HINT;
10367 else if (!strcmp ("host", p))
10368 result = PRAGMA_OACC_CLAUSE_HOST;
10369 break;
10370 case 'i':
10371 if (!strcmp ("inbranch", p))
10372 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10373 else if (!strcmp ("independent", p))
10374 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10375 else if (!strcmp ("is_device_ptr", p))
10376 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10377 break;
10378 case 'l':
10379 if (!strcmp ("lastprivate", p))
10380 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10381 else if (!strcmp ("linear", p))
10382 result = PRAGMA_OMP_CLAUSE_LINEAR;
10383 else if (!strcmp ("link", p))
10384 result = PRAGMA_OMP_CLAUSE_LINK;
10385 break;
10386 case 'm':
10387 if (!strcmp ("map", p))
10388 result = PRAGMA_OMP_CLAUSE_MAP;
10389 else if (!strcmp ("mergeable", p))
10390 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10391 else if (flag_cilkplus && !strcmp ("mask", p))
10392 result = PRAGMA_CILK_CLAUSE_MASK;
10393 break;
10394 case 'n':
10395 if (!strcmp ("nogroup", p))
10396 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10397 else if (!strcmp ("notinbranch", p))
10398 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10399 else if (!strcmp ("nowait", p))
10400 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10401 else if (!strcmp ("num_gangs", p))
10402 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10403 else if (!strcmp ("num_tasks", p))
10404 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10405 else if (!strcmp ("num_teams", p))
10406 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10407 else if (!strcmp ("num_threads", p))
10408 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10409 else if (!strcmp ("num_workers", p))
10410 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10411 else if (flag_cilkplus && !strcmp ("nomask", p))
10412 result = PRAGMA_CILK_CLAUSE_NOMASK;
10413 break;
10414 case 'o':
10415 if (!strcmp ("ordered", p))
10416 result = PRAGMA_OMP_CLAUSE_ORDERED;
10417 break;
10418 case 'p':
10419 if (!strcmp ("parallel", p))
10420 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10421 else if (!strcmp ("present", p))
10422 result = PRAGMA_OACC_CLAUSE_PRESENT;
10423 else if (!strcmp ("present_or_copy", p)
10424 || !strcmp ("pcopy", p))
10425 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10426 else if (!strcmp ("present_or_copyin", p)
10427 || !strcmp ("pcopyin", p))
10428 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10429 else if (!strcmp ("present_or_copyout", p)
10430 || !strcmp ("pcopyout", p))
10431 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10432 else if (!strcmp ("present_or_create", p)
10433 || !strcmp ("pcreate", p))
10434 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10435 else if (!strcmp ("priority", p))
10436 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10437 else if (!strcmp ("private", p))
10438 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10439 else if (!strcmp ("proc_bind", p))
10440 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10441 break;
10442 case 'r':
10443 if (!strcmp ("reduction", p))
10444 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10445 break;
10446 case 's':
10447 if (!strcmp ("safelen", p))
10448 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10449 else if (!strcmp ("schedule", p))
10450 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10451 else if (!strcmp ("sections", p))
10452 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10453 else if (!strcmp ("seq", p))
10454 result = PRAGMA_OACC_CLAUSE_SEQ;
10455 else if (!strcmp ("shared", p))
10456 result = PRAGMA_OMP_CLAUSE_SHARED;
10457 else if (!strcmp ("simd", p))
10458 result = PRAGMA_OMP_CLAUSE_SIMD;
10459 else if (!strcmp ("simdlen", p))
10460 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10461 else if (!strcmp ("self", p))
10462 result = PRAGMA_OACC_CLAUSE_SELF;
10463 break;
10464 case 't':
10465 if (!strcmp ("taskgroup", p))
10466 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10467 else if (!strcmp ("thread_limit", p))
10468 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10469 else if (!strcmp ("threads", p))
10470 result = PRAGMA_OMP_CLAUSE_THREADS;
10471 else if (!strcmp ("tile", p))
10472 result = PRAGMA_OACC_CLAUSE_TILE;
10473 else if (!strcmp ("to", p))
10474 result = PRAGMA_OMP_CLAUSE_TO;
10475 break;
10476 case 'u':
10477 if (!strcmp ("uniform", p))
10478 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10479 else if (!strcmp ("untied", p))
10480 result = PRAGMA_OMP_CLAUSE_UNTIED;
10481 else if (!strcmp ("use_device", p))
10482 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10483 else if (!strcmp ("use_device_ptr", p))
10484 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10485 break;
10486 case 'v':
10487 if (!strcmp ("vector", p))
10488 result = PRAGMA_OACC_CLAUSE_VECTOR;
10489 else if (!strcmp ("vector_length", p))
10490 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10491 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10492 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10493 break;
10494 case 'w':
10495 if (!strcmp ("wait", p))
10496 result = PRAGMA_OACC_CLAUSE_WAIT;
10497 else if (!strcmp ("worker", p))
10498 result = PRAGMA_OACC_CLAUSE_WORKER;
10499 break;
10503 if (result != PRAGMA_OMP_CLAUSE_NONE)
10504 c_parser_consume_token (parser);
10506 return result;
10509 /* Validate that a clause of the given type does not already exist. */
10511 static void
10512 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10513 const char *name)
10515 tree c;
10517 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10518 if (OMP_CLAUSE_CODE (c) == code)
10520 location_t loc = OMP_CLAUSE_LOCATION (c);
10521 error_at (loc, "too many %qs clauses", name);
10522 break;
10526 /* OpenACC 2.0
10527 Parse wait clause or wait directive parameters. */
10529 static tree
10530 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10532 vec<tree, va_gc> *args;
10533 tree t, args_tree;
10535 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10536 return list;
10538 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10540 if (args->length () == 0)
10542 c_parser_error (parser, "expected integer expression before ')'");
10543 release_tree_vector (args);
10544 return list;
10547 args_tree = build_tree_list_vec (args);
10549 for (t = args_tree; t; t = TREE_CHAIN (t))
10551 tree targ = TREE_VALUE (t);
10553 if (targ != error_mark_node)
10555 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10557 c_parser_error (parser, "expression must be integral");
10558 targ = error_mark_node;
10560 else
10562 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10564 OMP_CLAUSE_DECL (c) = targ;
10565 OMP_CLAUSE_CHAIN (c) = list;
10566 list = c;
10571 release_tree_vector (args);
10572 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10573 return list;
10576 /* OpenACC 2.0, OpenMP 2.5:
10577 variable-list:
10578 identifier
10579 variable-list , identifier
10581 If KIND is nonzero, create the appropriate node and install the
10582 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10583 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10585 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10586 return the list created. */
10588 static tree
10589 c_parser_omp_variable_list (c_parser *parser,
10590 location_t clause_loc,
10591 enum omp_clause_code kind, tree list)
10593 if (c_parser_next_token_is_not (parser, CPP_NAME)
10594 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10595 c_parser_error (parser, "expected identifier");
10597 while (c_parser_next_token_is (parser, CPP_NAME)
10598 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10600 tree t = lookup_name (c_parser_peek_token (parser)->value);
10602 if (t == NULL_TREE)
10604 undeclared_variable (c_parser_peek_token (parser)->location,
10605 c_parser_peek_token (parser)->value);
10606 t = error_mark_node;
10609 c_parser_consume_token (parser);
10611 if (t == error_mark_node)
10613 else if (kind != 0)
10615 switch (kind)
10617 case OMP_CLAUSE__CACHE_:
10618 /* The OpenACC cache directive explicitly only allows "array
10619 elements or subarrays". */
10620 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10622 c_parser_error (parser, "expected %<[%>");
10623 t = error_mark_node;
10624 break;
10626 /* FALLTHROUGH */
10627 case OMP_CLAUSE_MAP:
10628 case OMP_CLAUSE_FROM:
10629 case OMP_CLAUSE_TO:
10630 while (c_parser_next_token_is (parser, CPP_DOT))
10632 location_t op_loc = c_parser_peek_token (parser)->location;
10633 c_parser_consume_token (parser);
10634 if (!c_parser_next_token_is (parser, CPP_NAME))
10636 c_parser_error (parser, "expected identifier");
10637 t = error_mark_node;
10638 break;
10641 c_token *comp_tok = c_parser_peek_token (parser);
10642 tree ident = comp_tok->value;
10643 location_t comp_loc = comp_tok->location;
10644 c_parser_consume_token (parser);
10645 t = build_component_ref (op_loc, t, ident, comp_loc);
10647 /* FALLTHROUGH */
10648 case OMP_CLAUSE_DEPEND:
10649 case OMP_CLAUSE_REDUCTION:
10650 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10652 tree low_bound = NULL_TREE, length = NULL_TREE;
10654 c_parser_consume_token (parser);
10655 if (!c_parser_next_token_is (parser, CPP_COLON))
10657 low_bound = c_parser_expression (parser).value;
10658 mark_exp_read (low_bound);
10660 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10661 length = integer_one_node;
10662 else
10664 /* Look for `:'. */
10665 if (!c_parser_require (parser, CPP_COLON,
10666 "expected %<:%>"))
10668 t = error_mark_node;
10669 break;
10671 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10673 length = c_parser_expression (parser).value;
10674 mark_exp_read (length);
10677 /* Look for the closing `]'. */
10678 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10679 "expected %<]%>"))
10681 t = error_mark_node;
10682 break;
10685 t = tree_cons (low_bound, length, t);
10687 break;
10688 default:
10689 break;
10692 if (t != error_mark_node)
10694 tree u = build_omp_clause (clause_loc, kind);
10695 OMP_CLAUSE_DECL (u) = t;
10696 OMP_CLAUSE_CHAIN (u) = list;
10697 list = u;
10700 else
10701 list = tree_cons (t, NULL_TREE, list);
10703 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10704 break;
10706 c_parser_consume_token (parser);
10709 return list;
10712 /* Similarly, but expect leading and trailing parenthesis. This is a very
10713 common case for OpenACC and OpenMP clauses. */
10715 static tree
10716 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10717 tree list)
10719 /* The clauses location. */
10720 location_t loc = c_parser_peek_token (parser)->location;
10722 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10724 list = c_parser_omp_variable_list (parser, loc, kind, list);
10725 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10727 return list;
10730 /* OpenACC 2.0:
10731 copy ( variable-list )
10732 copyin ( variable-list )
10733 copyout ( variable-list )
10734 create ( variable-list )
10735 delete ( variable-list )
10736 present ( variable-list )
10737 present_or_copy ( variable-list )
10738 pcopy ( variable-list )
10739 present_or_copyin ( variable-list )
10740 pcopyin ( variable-list )
10741 present_or_copyout ( variable-list )
10742 pcopyout ( variable-list )
10743 present_or_create ( variable-list )
10744 pcreate ( variable-list ) */
10746 static tree
10747 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10748 tree list)
10750 enum gomp_map_kind kind;
10751 switch (c_kind)
10753 case PRAGMA_OACC_CLAUSE_COPY:
10754 kind = GOMP_MAP_FORCE_TOFROM;
10755 break;
10756 case PRAGMA_OACC_CLAUSE_COPYIN:
10757 kind = GOMP_MAP_FORCE_TO;
10758 break;
10759 case PRAGMA_OACC_CLAUSE_COPYOUT:
10760 kind = GOMP_MAP_FORCE_FROM;
10761 break;
10762 case PRAGMA_OACC_CLAUSE_CREATE:
10763 kind = GOMP_MAP_FORCE_ALLOC;
10764 break;
10765 case PRAGMA_OACC_CLAUSE_DELETE:
10766 kind = GOMP_MAP_DELETE;
10767 break;
10768 case PRAGMA_OACC_CLAUSE_DEVICE:
10769 kind = GOMP_MAP_FORCE_TO;
10770 break;
10771 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
10772 kind = GOMP_MAP_DEVICE_RESIDENT;
10773 break;
10774 case PRAGMA_OACC_CLAUSE_HOST:
10775 case PRAGMA_OACC_CLAUSE_SELF:
10776 kind = GOMP_MAP_FORCE_FROM;
10777 break;
10778 case PRAGMA_OACC_CLAUSE_LINK:
10779 kind = GOMP_MAP_LINK;
10780 break;
10781 case PRAGMA_OACC_CLAUSE_PRESENT:
10782 kind = GOMP_MAP_FORCE_PRESENT;
10783 break;
10784 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10785 kind = GOMP_MAP_TOFROM;
10786 break;
10787 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10788 kind = GOMP_MAP_TO;
10789 break;
10790 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10791 kind = GOMP_MAP_FROM;
10792 break;
10793 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10794 kind = GOMP_MAP_ALLOC;
10795 break;
10796 default:
10797 gcc_unreachable ();
10799 tree nl, c;
10800 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10802 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10803 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10805 return nl;
10808 /* OpenACC 2.0:
10809 deviceptr ( variable-list ) */
10811 static tree
10812 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10814 location_t loc = c_parser_peek_token (parser)->location;
10815 tree vars, t;
10817 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10818 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10819 variable-list must only allow for pointer variables. */
10820 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10821 for (t = vars; t && t; t = TREE_CHAIN (t))
10823 tree v = TREE_PURPOSE (t);
10825 /* FIXME diagnostics: Ideally we should keep individual
10826 locations for all the variables in the var list to make the
10827 following errors more precise. Perhaps
10828 c_parser_omp_var_list_parens() should construct a list of
10829 locations to go along with the var list. */
10831 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
10832 error_at (loc, "%qD is not a variable", v);
10833 else if (TREE_TYPE (v) == error_mark_node)
10835 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10836 error_at (loc, "%qD is not a pointer variable", v);
10838 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10839 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10840 OMP_CLAUSE_DECL (u) = v;
10841 OMP_CLAUSE_CHAIN (u) = list;
10842 list = u;
10845 return list;
10848 /* OpenACC 2.0, OpenMP 3.0:
10849 collapse ( constant-expression ) */
10851 static tree
10852 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10854 tree c, num = error_mark_node;
10855 HOST_WIDE_INT n;
10856 location_t loc;
10858 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10860 loc = c_parser_peek_token (parser)->location;
10861 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10863 num = c_parser_expr_no_commas (parser, NULL).value;
10864 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10866 if (num == error_mark_node)
10867 return list;
10868 mark_exp_read (num);
10869 num = c_fully_fold (num, false, NULL);
10870 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10871 || !tree_fits_shwi_p (num)
10872 || (n = tree_to_shwi (num)) <= 0
10873 || (int) n != n)
10875 error_at (loc,
10876 "collapse argument needs positive constant integer expression");
10877 return list;
10879 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10880 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10881 OMP_CLAUSE_CHAIN (c) = list;
10882 return c;
10885 /* OpenMP 2.5:
10886 copyin ( variable-list ) */
10888 static tree
10889 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10891 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10894 /* OpenMP 2.5:
10895 copyprivate ( variable-list ) */
10897 static tree
10898 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10900 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10903 /* OpenMP 2.5:
10904 default ( shared | none )
10906 OpenACC 2.0:
10907 default (none) */
10909 static tree
10910 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
10912 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10913 location_t loc = c_parser_peek_token (parser)->location;
10914 tree c;
10916 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10917 return list;
10918 if (c_parser_next_token_is (parser, CPP_NAME))
10920 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10922 switch (p[0])
10924 case 'n':
10925 if (strcmp ("none", p) != 0)
10926 goto invalid_kind;
10927 kind = OMP_CLAUSE_DEFAULT_NONE;
10928 break;
10930 case 's':
10931 if (strcmp ("shared", p) != 0 || is_oacc)
10932 goto invalid_kind;
10933 kind = OMP_CLAUSE_DEFAULT_SHARED;
10934 break;
10936 default:
10937 goto invalid_kind;
10940 c_parser_consume_token (parser);
10942 else
10944 invalid_kind:
10945 if (is_oacc)
10946 c_parser_error (parser, "expected %<none%>");
10947 else
10948 c_parser_error (parser, "expected %<none%> or %<shared%>");
10950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10952 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10953 return list;
10955 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10956 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10957 OMP_CLAUSE_CHAIN (c) = list;
10958 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10960 return c;
10963 /* OpenMP 2.5:
10964 firstprivate ( variable-list ) */
10966 static tree
10967 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10969 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10972 /* OpenMP 3.1:
10973 final ( expression ) */
10975 static tree
10976 c_parser_omp_clause_final (c_parser *parser, tree list)
10978 location_t loc = c_parser_peek_token (parser)->location;
10979 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10981 tree t = c_parser_paren_condition (parser);
10982 tree c;
10984 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10986 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10987 OMP_CLAUSE_FINAL_EXPR (c) = t;
10988 OMP_CLAUSE_CHAIN (c) = list;
10989 list = c;
10991 else
10992 c_parser_error (parser, "expected %<(%>");
10994 return list;
10997 /* OpenACC, OpenMP 2.5:
10998 if ( expression )
11000 OpenMP 4.5:
11001 if ( directive-name-modifier : expression )
11003 directive-name-modifier:
11004 parallel | task | taskloop | target data | target | target update
11005 | target enter data | target exit data */
11007 static tree
11008 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11010 location_t location = c_parser_peek_token (parser)->location;
11011 enum tree_code if_modifier = ERROR_MARK;
11013 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11014 return list;
11016 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11018 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11019 int n = 2;
11020 if (strcmp (p, "parallel") == 0)
11021 if_modifier = OMP_PARALLEL;
11022 else if (strcmp (p, "task") == 0)
11023 if_modifier = OMP_TASK;
11024 else if (strcmp (p, "taskloop") == 0)
11025 if_modifier = OMP_TASKLOOP;
11026 else if (strcmp (p, "target") == 0)
11028 if_modifier = OMP_TARGET;
11029 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11031 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11032 if (strcmp ("data", p) == 0)
11033 if_modifier = OMP_TARGET_DATA;
11034 else if (strcmp ("update", p) == 0)
11035 if_modifier = OMP_TARGET_UPDATE;
11036 else if (strcmp ("enter", p) == 0)
11037 if_modifier = OMP_TARGET_ENTER_DATA;
11038 else if (strcmp ("exit", p) == 0)
11039 if_modifier = OMP_TARGET_EXIT_DATA;
11040 if (if_modifier != OMP_TARGET)
11042 n = 3;
11043 c_parser_consume_token (parser);
11045 else
11047 location_t loc = c_parser_peek_2nd_token (parser)->location;
11048 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11049 "or %<exit%>");
11050 if_modifier = ERROR_MARK;
11052 if (if_modifier == OMP_TARGET_ENTER_DATA
11053 || if_modifier == OMP_TARGET_EXIT_DATA)
11055 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11057 p = IDENTIFIER_POINTER
11058 (c_parser_peek_2nd_token (parser)->value);
11059 if (strcmp ("data", p) == 0)
11060 n = 4;
11062 if (n == 4)
11063 c_parser_consume_token (parser);
11064 else
11066 location_t loc
11067 = c_parser_peek_2nd_token (parser)->location;
11068 error_at (loc, "expected %<data%>");
11069 if_modifier = ERROR_MARK;
11074 if (if_modifier != ERROR_MARK)
11076 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11078 c_parser_consume_token (parser);
11079 c_parser_consume_token (parser);
11081 else
11083 if (n > 2)
11085 location_t loc = c_parser_peek_2nd_token (parser)->location;
11086 error_at (loc, "expected %<:%>");
11088 if_modifier = ERROR_MARK;
11093 tree t = c_parser_condition (parser), c;
11094 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11096 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11097 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11099 if (if_modifier != ERROR_MARK
11100 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11102 const char *p = NULL;
11103 switch (if_modifier)
11105 case OMP_PARALLEL: p = "parallel"; break;
11106 case OMP_TASK: p = "task"; break;
11107 case OMP_TASKLOOP: p = "taskloop"; break;
11108 case OMP_TARGET_DATA: p = "target data"; break;
11109 case OMP_TARGET: p = "target"; break;
11110 case OMP_TARGET_UPDATE: p = "target update"; break;
11111 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11112 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11113 default: gcc_unreachable ();
11115 error_at (location, "too many %<if%> clauses with %qs modifier",
11117 return list;
11119 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11121 if (!is_omp)
11122 error_at (location, "too many %<if%> clauses");
11123 else
11124 error_at (location, "too many %<if%> clauses without modifier");
11125 return list;
11127 else if (if_modifier == ERROR_MARK
11128 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11130 error_at (location, "if any %<if%> clause has modifier, then all "
11131 "%<if%> clauses have to use modifier");
11132 return list;
11136 c = build_omp_clause (location, OMP_CLAUSE_IF);
11137 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11138 OMP_CLAUSE_IF_EXPR (c) = t;
11139 OMP_CLAUSE_CHAIN (c) = list;
11140 return c;
11143 /* OpenMP 2.5:
11144 lastprivate ( variable-list ) */
11146 static tree
11147 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11149 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11152 /* OpenMP 3.1:
11153 mergeable */
11155 static tree
11156 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11158 tree c;
11160 /* FIXME: Should we allow duplicates? */
11161 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11163 c = build_omp_clause (c_parser_peek_token (parser)->location,
11164 OMP_CLAUSE_MERGEABLE);
11165 OMP_CLAUSE_CHAIN (c) = list;
11167 return c;
11170 /* OpenMP 2.5:
11171 nowait */
11173 static tree
11174 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11176 tree c;
11177 location_t loc = c_parser_peek_token (parser)->location;
11179 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11181 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11182 OMP_CLAUSE_CHAIN (c) = list;
11183 return c;
11186 /* OpenACC:
11187 num_gangs ( expression ) */
11189 static tree
11190 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11192 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11193 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11195 location_t expr_loc = c_parser_peek_token (parser)->location;
11196 tree c, t = c_parser_expression (parser).value;
11197 mark_exp_read (t);
11198 t = c_fully_fold (t, false, NULL);
11200 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11202 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11204 c_parser_error (parser, "expected integer expression");
11205 return list;
11208 /* Attempt to statically determine when the number isn't positive. */
11209 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11210 build_int_cst (TREE_TYPE (t), 0));
11211 protected_set_expr_location (c, expr_loc);
11212 if (c == boolean_true_node)
11214 warning_at (expr_loc, 0,
11215 "%<num_gangs%> value must be positive");
11216 t = integer_one_node;
11219 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11221 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11222 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11223 OMP_CLAUSE_CHAIN (c) = list;
11224 list = c;
11227 return list;
11230 /* OpenMP 2.5:
11231 num_threads ( expression ) */
11233 static tree
11234 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11236 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11237 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11239 location_t expr_loc = c_parser_peek_token (parser)->location;
11240 tree c, t = c_parser_expression (parser).value;
11241 mark_exp_read (t);
11242 t = c_fully_fold (t, false, NULL);
11244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11246 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11248 c_parser_error (parser, "expected integer expression");
11249 return list;
11252 /* Attempt to statically determine when the number isn't positive. */
11253 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11254 build_int_cst (TREE_TYPE (t), 0));
11255 protected_set_expr_location (c, expr_loc);
11256 if (c == boolean_true_node)
11258 warning_at (expr_loc, 0,
11259 "%<num_threads%> value must be positive");
11260 t = integer_one_node;
11263 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11265 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11266 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11267 OMP_CLAUSE_CHAIN (c) = list;
11268 list = c;
11271 return list;
11274 /* OpenMP 4.5:
11275 num_tasks ( expression ) */
11277 static tree
11278 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11280 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11281 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11283 location_t expr_loc = c_parser_peek_token (parser)->location;
11284 tree c, t = c_parser_expression (parser).value;
11285 mark_exp_read (t);
11286 t = c_fully_fold (t, false, NULL);
11288 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11290 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11292 c_parser_error (parser, "expected integer expression");
11293 return list;
11296 /* Attempt to statically determine when the number isn't positive. */
11297 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11298 build_int_cst (TREE_TYPE (t), 0));
11299 if (CAN_HAVE_LOCATION_P (c))
11300 SET_EXPR_LOCATION (c, expr_loc);
11301 if (c == boolean_true_node)
11303 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11304 t = integer_one_node;
11307 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11309 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11310 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11311 OMP_CLAUSE_CHAIN (c) = list;
11312 list = c;
11315 return list;
11318 /* OpenMP 4.5:
11319 grainsize ( expression ) */
11321 static tree
11322 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11324 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11325 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11327 location_t expr_loc = c_parser_peek_token (parser)->location;
11328 tree c, t = c_parser_expression (parser).value;
11329 mark_exp_read (t);
11330 t = c_fully_fold (t, false, NULL);
11332 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11334 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11336 c_parser_error (parser, "expected integer expression");
11337 return list;
11340 /* Attempt to statically determine when the number isn't positive. */
11341 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11342 build_int_cst (TREE_TYPE (t), 0));
11343 if (CAN_HAVE_LOCATION_P (c))
11344 SET_EXPR_LOCATION (c, expr_loc);
11345 if (c == boolean_true_node)
11347 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11348 t = integer_one_node;
11351 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11353 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11354 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11355 OMP_CLAUSE_CHAIN (c) = list;
11356 list = c;
11359 return list;
11362 /* OpenMP 4.5:
11363 priority ( expression ) */
11365 static tree
11366 c_parser_omp_clause_priority (c_parser *parser, tree list)
11368 location_t priority_loc = c_parser_peek_token (parser)->location;
11369 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11371 location_t expr_loc = c_parser_peek_token (parser)->location;
11372 tree c, t = c_parser_expression (parser).value;
11373 mark_exp_read (t);
11374 t = c_fully_fold (t, false, NULL);
11376 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11378 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11380 c_parser_error (parser, "expected integer expression");
11381 return list;
11384 /* Attempt to statically determine when the number isn't
11385 non-negative. */
11386 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11387 build_int_cst (TREE_TYPE (t), 0));
11388 if (CAN_HAVE_LOCATION_P (c))
11389 SET_EXPR_LOCATION (c, expr_loc);
11390 if (c == boolean_true_node)
11392 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11393 t = integer_one_node;
11396 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11398 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11399 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11400 OMP_CLAUSE_CHAIN (c) = list;
11401 list = c;
11404 return list;
11407 /* OpenMP 4.5:
11408 hint ( expression ) */
11410 static tree
11411 c_parser_omp_clause_hint (c_parser *parser, tree list)
11413 location_t hint_loc = c_parser_peek_token (parser)->location;
11414 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11416 tree c, t = c_parser_expression (parser).value;
11417 mark_exp_read (t);
11418 t = c_fully_fold (t, false, NULL);
11420 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11422 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11424 c_parser_error (parser, "expected integer expression");
11425 return list;
11428 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11430 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11431 OMP_CLAUSE_HINT_EXPR (c) = t;
11432 OMP_CLAUSE_CHAIN (c) = list;
11433 list = c;
11436 return list;
11439 /* OpenMP 4.5:
11440 defaultmap ( tofrom : scalar ) */
11442 static tree
11443 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11445 location_t loc = c_parser_peek_token (parser)->location;
11446 tree c;
11447 const char *p;
11449 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11450 return list;
11451 if (!c_parser_next_token_is (parser, CPP_NAME))
11453 c_parser_error (parser, "expected %<tofrom%>");
11454 goto out_err;
11456 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11457 if (strcmp (p, "tofrom") != 0)
11459 c_parser_error (parser, "expected %<tofrom%>");
11460 goto out_err;
11462 c_parser_consume_token (parser);
11463 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11464 goto out_err;
11465 if (!c_parser_next_token_is (parser, CPP_NAME))
11467 c_parser_error (parser, "expected %<scalar%>");
11468 goto out_err;
11470 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11471 if (strcmp (p, "scalar") != 0)
11473 c_parser_error (parser, "expected %<scalar%>");
11474 goto out_err;
11476 c_parser_consume_token (parser);
11477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11478 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11479 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11480 OMP_CLAUSE_CHAIN (c) = list;
11481 return c;
11483 out_err:
11484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11485 return list;
11488 /* OpenACC 2.0:
11489 use_device ( variable-list )
11491 OpenMP 4.5:
11492 use_device_ptr ( variable-list ) */
11494 static tree
11495 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11497 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11498 list);
11501 /* OpenMP 4.5:
11502 is_device_ptr ( variable-list ) */
11504 static tree
11505 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11507 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11510 /* OpenACC:
11511 num_workers ( expression ) */
11513 static tree
11514 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11516 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11517 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11519 location_t expr_loc = c_parser_peek_token (parser)->location;
11520 tree c, t = c_parser_expression (parser).value;
11521 mark_exp_read (t);
11522 t = c_fully_fold (t, false, NULL);
11524 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11526 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11528 c_parser_error (parser, "expected integer expression");
11529 return list;
11532 /* Attempt to statically determine when the number isn't positive. */
11533 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11534 build_int_cst (TREE_TYPE (t), 0));
11535 protected_set_expr_location (c, expr_loc);
11536 if (c == boolean_true_node)
11538 warning_at (expr_loc, 0,
11539 "%<num_workers%> value must be positive");
11540 t = integer_one_node;
11543 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11545 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11546 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11547 OMP_CLAUSE_CHAIN (c) = list;
11548 list = c;
11551 return list;
11554 /* OpenACC:
11556 gang [( gang-arg-list )]
11557 worker [( [num:] int-expr )]
11558 vector [( [length:] int-expr )]
11560 where gang-arg is one of:
11562 [num:] int-expr
11563 static: size-expr
11565 and size-expr may be:
11568 int-expr
11571 static tree
11572 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11573 const char *str, tree list)
11575 const char *id = "num";
11576 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11577 location_t loc = c_parser_peek_token (parser)->location;
11579 if (kind == OMP_CLAUSE_VECTOR)
11580 id = "length";
11582 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11584 c_parser_consume_token (parser);
11588 c_token *next = c_parser_peek_token (parser);
11589 int idx = 0;
11591 /* Gang static argument. */
11592 if (kind == OMP_CLAUSE_GANG
11593 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11595 c_parser_consume_token (parser);
11597 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11598 goto cleanup_error;
11600 idx = 1;
11601 if (ops[idx] != NULL_TREE)
11603 c_parser_error (parser, "too many %<static%> arguments");
11604 goto cleanup_error;
11607 /* Check for the '*' argument. */
11608 if (c_parser_next_token_is (parser, CPP_MULT)
11609 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11610 || c_parser_peek_2nd_token (parser)->type
11611 == CPP_CLOSE_PAREN))
11613 c_parser_consume_token (parser);
11614 ops[idx] = integer_minus_one_node;
11616 if (c_parser_next_token_is (parser, CPP_COMMA))
11618 c_parser_consume_token (parser);
11619 continue;
11621 else
11622 break;
11625 /* Worker num: argument and vector length: arguments. */
11626 else if (c_parser_next_token_is (parser, CPP_NAME)
11627 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11628 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11630 c_parser_consume_token (parser); /* id */
11631 c_parser_consume_token (parser); /* ':' */
11634 /* Now collect the actual argument. */
11635 if (ops[idx] != NULL_TREE)
11637 c_parser_error (parser, "unexpected argument");
11638 goto cleanup_error;
11641 location_t expr_loc = c_parser_peek_token (parser)->location;
11642 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11643 if (expr == error_mark_node)
11644 goto cleanup_error;
11646 mark_exp_read (expr);
11647 expr = c_fully_fold (expr, false, NULL);
11649 /* Attempt to statically determine when the number isn't a
11650 positive integer. */
11652 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11654 c_parser_error (parser, "expected integer expression");
11655 return list;
11658 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11659 build_int_cst (TREE_TYPE (expr), 0));
11660 if (c == boolean_true_node)
11662 warning_at (loc, 0,
11663 "%<%s%> value must be positive", str);
11664 expr = integer_one_node;
11667 ops[idx] = expr;
11669 if (kind == OMP_CLAUSE_GANG
11670 && c_parser_next_token_is (parser, CPP_COMMA))
11672 c_parser_consume_token (parser);
11673 continue;
11675 break;
11677 while (1);
11679 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11680 goto cleanup_error;
11683 check_no_duplicate_clause (list, kind, str);
11685 c = build_omp_clause (loc, kind);
11687 if (ops[1])
11688 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11690 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11691 OMP_CLAUSE_CHAIN (c) = list;
11693 return c;
11695 cleanup_error:
11696 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11697 return list;
11700 /* OpenACC:
11701 auto
11702 independent
11703 nohost
11704 seq */
11706 static tree
11707 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11708 tree list)
11710 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11712 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11713 OMP_CLAUSE_CHAIN (c) = list;
11715 return c;
11718 /* OpenACC:
11719 async [( int-expr )] */
11721 static tree
11722 c_parser_oacc_clause_async (c_parser *parser, tree list)
11724 tree c, t;
11725 location_t loc = c_parser_peek_token (parser)->location;
11727 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11729 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11731 c_parser_consume_token (parser);
11733 t = c_parser_expression (parser).value;
11734 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11735 c_parser_error (parser, "expected integer expression");
11736 else if (t == error_mark_node
11737 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11738 return list;
11740 else
11741 t = c_fully_fold (t, false, NULL);
11743 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11745 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11746 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11747 OMP_CLAUSE_CHAIN (c) = list;
11748 list = c;
11750 return list;
11753 /* OpenACC 2.0:
11754 tile ( size-expr-list ) */
11756 static tree
11757 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11759 tree c, expr = error_mark_node;
11760 location_t loc, expr_loc;
11761 tree tile = NULL_TREE;
11763 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11765 loc = c_parser_peek_token (parser)->location;
11766 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11767 return list;
11771 if (c_parser_next_token_is (parser, CPP_MULT)
11772 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11773 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11775 c_parser_consume_token (parser);
11776 expr = integer_minus_one_node;
11778 else
11780 expr_loc = c_parser_peek_token (parser)->location;
11781 expr = c_parser_expr_no_commas (parser, NULL).value;
11783 if (expr == error_mark_node)
11785 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11786 "expected %<)%>");
11787 return list;
11790 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11792 c_parser_error (parser, "%<tile%> value must be integral");
11793 return list;
11796 mark_exp_read (expr);
11797 expr = c_fully_fold (expr, false, NULL);
11799 /* Attempt to statically determine when expr isn't positive. */
11800 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11801 build_int_cst (TREE_TYPE (expr), 0));
11802 protected_set_expr_location (c, expr_loc);
11803 if (c == boolean_true_node)
11805 warning_at (expr_loc, 0,"%<tile%> value must be positive");
11806 expr = integer_one_node;
11810 tile = tree_cons (NULL_TREE, expr, tile);
11811 if (c_parser_next_token_is (parser, CPP_COMMA))
11812 c_parser_consume_token (parser);
11814 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11816 /* Consume the trailing ')'. */
11817 c_parser_consume_token (parser);
11819 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11820 tile = nreverse (tile);
11821 OMP_CLAUSE_TILE_LIST (c) = tile;
11822 OMP_CLAUSE_CHAIN (c) = list;
11823 return c;
11826 /* OpenACC:
11827 wait ( int-expr-list ) */
11829 static tree
11830 c_parser_oacc_clause_wait (c_parser *parser, tree list)
11832 location_t clause_loc = c_parser_peek_token (parser)->location;
11834 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11835 list = c_parser_oacc_wait_list (parser, clause_loc, list);
11837 return list;
11840 /* OpenMP 2.5:
11841 ordered
11843 OpenMP 4.5:
11844 ordered ( constant-expression ) */
11846 static tree
11847 c_parser_omp_clause_ordered (c_parser *parser, tree list)
11849 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
11851 tree c, num = NULL_TREE;
11852 HOST_WIDE_INT n;
11853 location_t loc = c_parser_peek_token (parser)->location;
11854 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11856 c_parser_consume_token (parser);
11857 num = c_parser_expr_no_commas (parser, NULL).value;
11858 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11860 if (num == error_mark_node)
11861 return list;
11862 if (num)
11864 mark_exp_read (num);
11865 num = c_fully_fold (num, false, NULL);
11866 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11867 || !tree_fits_shwi_p (num)
11868 || (n = tree_to_shwi (num)) <= 0
11869 || (int) n != n)
11871 error_at (loc, "ordered argument needs positive "
11872 "constant integer expression");
11873 return list;
11876 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
11877 OMP_CLAUSE_ORDERED_EXPR (c) = num;
11878 OMP_CLAUSE_CHAIN (c) = list;
11879 return c;
11882 /* OpenMP 2.5:
11883 private ( variable-list ) */
11885 static tree
11886 c_parser_omp_clause_private (c_parser *parser, tree list)
11888 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
11891 /* OpenMP 2.5:
11892 reduction ( reduction-operator : variable-list )
11894 reduction-operator:
11895 One of: + * - & ^ | && ||
11897 OpenMP 3.1:
11899 reduction-operator:
11900 One of: + * - & ^ | && || max min
11902 OpenMP 4.0:
11904 reduction-operator:
11905 One of: + * - & ^ | && ||
11906 identifier */
11908 static tree
11909 c_parser_omp_clause_reduction (c_parser *parser, tree list)
11911 location_t clause_loc = c_parser_peek_token (parser)->location;
11912 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11914 enum tree_code code = ERROR_MARK;
11915 tree reduc_id = NULL_TREE;
11917 switch (c_parser_peek_token (parser)->type)
11919 case CPP_PLUS:
11920 code = PLUS_EXPR;
11921 break;
11922 case CPP_MULT:
11923 code = MULT_EXPR;
11924 break;
11925 case CPP_MINUS:
11926 code = MINUS_EXPR;
11927 break;
11928 case CPP_AND:
11929 code = BIT_AND_EXPR;
11930 break;
11931 case CPP_XOR:
11932 code = BIT_XOR_EXPR;
11933 break;
11934 case CPP_OR:
11935 code = BIT_IOR_EXPR;
11936 break;
11937 case CPP_AND_AND:
11938 code = TRUTH_ANDIF_EXPR;
11939 break;
11940 case CPP_OR_OR:
11941 code = TRUTH_ORIF_EXPR;
11942 break;
11943 case CPP_NAME:
11945 const char *p
11946 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11947 if (strcmp (p, "min") == 0)
11949 code = MIN_EXPR;
11950 break;
11952 if (strcmp (p, "max") == 0)
11954 code = MAX_EXPR;
11955 break;
11957 reduc_id = c_parser_peek_token (parser)->value;
11958 break;
11960 default:
11961 c_parser_error (parser,
11962 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
11963 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
11964 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11965 return list;
11967 c_parser_consume_token (parser);
11968 reduc_id = c_omp_reduction_id (code, reduc_id);
11969 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11971 tree nl, c;
11973 nl = c_parser_omp_variable_list (parser, clause_loc,
11974 OMP_CLAUSE_REDUCTION, list);
11975 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11977 tree d = OMP_CLAUSE_DECL (c), type;
11978 if (TREE_CODE (d) != TREE_LIST)
11979 type = TREE_TYPE (d);
11980 else
11982 int cnt = 0;
11983 tree t;
11984 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
11985 cnt++;
11986 type = TREE_TYPE (t);
11987 while (cnt > 0)
11989 if (TREE_CODE (type) != POINTER_TYPE
11990 && TREE_CODE (type) != ARRAY_TYPE)
11991 break;
11992 type = TREE_TYPE (type);
11993 cnt--;
11996 while (TREE_CODE (type) == ARRAY_TYPE)
11997 type = TREE_TYPE (type);
11998 OMP_CLAUSE_REDUCTION_CODE (c) = code;
11999 if (code == ERROR_MARK
12000 || !(INTEGRAL_TYPE_P (type)
12001 || TREE_CODE (type) == REAL_TYPE
12002 || TREE_CODE (type) == COMPLEX_TYPE))
12003 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12004 = c_omp_reduction_lookup (reduc_id,
12005 TYPE_MAIN_VARIANT (type));
12008 list = nl;
12010 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12012 return list;
12015 /* OpenMP 2.5:
12016 schedule ( schedule-kind )
12017 schedule ( schedule-kind , expression )
12019 schedule-kind:
12020 static | dynamic | guided | runtime | auto
12022 OpenMP 4.5:
12023 schedule ( schedule-modifier : schedule-kind )
12024 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12026 schedule-modifier:
12027 simd
12028 monotonic
12029 nonmonotonic */
12031 static tree
12032 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12034 tree c, t;
12035 location_t loc = c_parser_peek_token (parser)->location;
12036 int modifiers = 0, nmodifiers = 0;
12038 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12039 return list;
12041 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12043 while (c_parser_next_token_is (parser, CPP_NAME))
12045 tree kind = c_parser_peek_token (parser)->value;
12046 const char *p = IDENTIFIER_POINTER (kind);
12047 if (strcmp ("simd", p) == 0)
12048 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12049 else if (strcmp ("monotonic", p) == 0)
12050 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12051 else if (strcmp ("nonmonotonic", p) == 0)
12052 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12053 else
12054 break;
12055 c_parser_consume_token (parser);
12056 if (nmodifiers++ == 0
12057 && c_parser_next_token_is (parser, CPP_COMMA))
12058 c_parser_consume_token (parser);
12059 else
12061 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12062 break;
12066 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12067 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12068 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12069 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12071 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12072 "specified");
12073 modifiers = 0;
12076 if (c_parser_next_token_is (parser, CPP_NAME))
12078 tree kind = c_parser_peek_token (parser)->value;
12079 const char *p = IDENTIFIER_POINTER (kind);
12081 switch (p[0])
12083 case 'd':
12084 if (strcmp ("dynamic", p) != 0)
12085 goto invalid_kind;
12086 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12087 break;
12089 case 'g':
12090 if (strcmp ("guided", p) != 0)
12091 goto invalid_kind;
12092 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12093 break;
12095 case 'r':
12096 if (strcmp ("runtime", p) != 0)
12097 goto invalid_kind;
12098 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12099 break;
12101 default:
12102 goto invalid_kind;
12105 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12106 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12107 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12108 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12109 else
12110 goto invalid_kind;
12112 c_parser_consume_token (parser);
12113 if (c_parser_next_token_is (parser, CPP_COMMA))
12115 location_t here;
12116 c_parser_consume_token (parser);
12118 here = c_parser_peek_token (parser)->location;
12119 t = c_parser_expr_no_commas (parser, NULL).value;
12120 mark_exp_read (t);
12121 t = c_fully_fold (t, false, NULL);
12123 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12124 error_at (here, "schedule %<runtime%> does not take "
12125 "a %<chunk_size%> parameter");
12126 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12127 error_at (here,
12128 "schedule %<auto%> does not take "
12129 "a %<chunk_size%> parameter");
12130 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12132 /* Attempt to statically determine when the number isn't
12133 positive. */
12134 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12135 build_int_cst (TREE_TYPE (t), 0));
12136 protected_set_expr_location (s, loc);
12137 if (s == boolean_true_node)
12139 warning_at (loc, 0,
12140 "chunk size value must be positive");
12141 t = integer_one_node;
12143 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12145 else
12146 c_parser_error (parser, "expected integer expression");
12148 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12150 else
12151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12152 "expected %<,%> or %<)%>");
12154 OMP_CLAUSE_SCHEDULE_KIND (c)
12155 = (enum omp_clause_schedule_kind)
12156 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12158 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12159 OMP_CLAUSE_CHAIN (c) = list;
12160 return c;
12162 invalid_kind:
12163 c_parser_error (parser, "invalid schedule kind");
12164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12165 return list;
12168 /* OpenMP 2.5:
12169 shared ( variable-list ) */
12171 static tree
12172 c_parser_omp_clause_shared (c_parser *parser, tree list)
12174 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12177 /* OpenMP 3.0:
12178 untied */
12180 static tree
12181 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12183 tree c;
12185 /* FIXME: Should we allow duplicates? */
12186 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12188 c = build_omp_clause (c_parser_peek_token (parser)->location,
12189 OMP_CLAUSE_UNTIED);
12190 OMP_CLAUSE_CHAIN (c) = list;
12192 return c;
12195 /* OpenACC:
12196 vector_length ( expression ) */
12198 static tree
12199 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12201 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12202 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12204 location_t expr_loc = c_parser_peek_token (parser)->location;
12205 tree c, t = c_parser_expression (parser).value;
12206 mark_exp_read (t);
12207 t = c_fully_fold (t, false, NULL);
12209 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12211 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12213 c_parser_error (parser, "expected integer expression");
12214 return list;
12217 /* Attempt to statically determine when the number isn't positive. */
12218 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12219 build_int_cst (TREE_TYPE (t), 0));
12220 protected_set_expr_location (c, expr_loc);
12221 if (c == boolean_true_node)
12223 warning_at (expr_loc, 0,
12224 "%<vector_length%> value must be positive");
12225 t = integer_one_node;
12228 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12230 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12231 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12232 OMP_CLAUSE_CHAIN (c) = list;
12233 list = c;
12236 return list;
12239 /* OpenMP 4.0:
12240 inbranch
12241 notinbranch */
12243 static tree
12244 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12245 enum omp_clause_code code, tree list)
12247 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12249 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12250 OMP_CLAUSE_CHAIN (c) = list;
12252 return c;
12255 /* OpenMP 4.0:
12256 parallel
12258 sections
12259 taskgroup */
12261 static tree
12262 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12263 enum omp_clause_code code, tree list)
12265 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12266 OMP_CLAUSE_CHAIN (c) = list;
12268 return c;
12271 /* OpenMP 4.5:
12272 nogroup */
12274 static tree
12275 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12277 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12278 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12279 OMP_CLAUSE_NOGROUP);
12280 OMP_CLAUSE_CHAIN (c) = list;
12281 return c;
12284 /* OpenMP 4.5:
12285 simd
12286 threads */
12288 static tree
12289 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12290 enum omp_clause_code code, tree list)
12292 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12293 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12294 OMP_CLAUSE_CHAIN (c) = list;
12295 return c;
12298 /* OpenMP 4.0:
12299 num_teams ( expression ) */
12301 static tree
12302 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12304 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12305 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12307 location_t expr_loc = c_parser_peek_token (parser)->location;
12308 tree c, t = c_parser_expression (parser).value;
12309 mark_exp_read (t);
12310 t = c_fully_fold (t, false, NULL);
12312 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12314 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12316 c_parser_error (parser, "expected integer expression");
12317 return list;
12320 /* Attempt to statically determine when the number isn't positive. */
12321 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12322 build_int_cst (TREE_TYPE (t), 0));
12323 protected_set_expr_location (c, expr_loc);
12324 if (c == boolean_true_node)
12326 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12327 t = integer_one_node;
12330 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12332 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12333 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12334 OMP_CLAUSE_CHAIN (c) = list;
12335 list = c;
12338 return list;
12341 /* OpenMP 4.0:
12342 thread_limit ( expression ) */
12344 static tree
12345 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12347 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12348 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12350 location_t expr_loc = c_parser_peek_token (parser)->location;
12351 tree c, t = c_parser_expression (parser).value;
12352 mark_exp_read (t);
12353 t = c_fully_fold (t, false, NULL);
12355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12357 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12359 c_parser_error (parser, "expected integer expression");
12360 return list;
12363 /* Attempt to statically determine when the number isn't positive. */
12364 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12365 build_int_cst (TREE_TYPE (t), 0));
12366 protected_set_expr_location (c, expr_loc);
12367 if (c == boolean_true_node)
12369 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12370 t = integer_one_node;
12373 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12374 "thread_limit");
12376 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12377 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12378 OMP_CLAUSE_CHAIN (c) = list;
12379 list = c;
12382 return list;
12385 /* OpenMP 4.0:
12386 aligned ( variable-list )
12387 aligned ( variable-list : constant-expression ) */
12389 static tree
12390 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12392 location_t clause_loc = c_parser_peek_token (parser)->location;
12393 tree nl, c;
12395 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12396 return list;
12398 nl = c_parser_omp_variable_list (parser, clause_loc,
12399 OMP_CLAUSE_ALIGNED, list);
12401 if (c_parser_next_token_is (parser, CPP_COLON))
12403 c_parser_consume_token (parser);
12404 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
12405 mark_exp_read (alignment);
12406 alignment = c_fully_fold (alignment, false, NULL);
12407 if (TREE_CODE (alignment) != INTEGER_CST
12408 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12409 || tree_int_cst_sgn (alignment) != 1)
12411 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12412 "be positive constant integer expression");
12413 alignment = NULL_TREE;
12416 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12417 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12420 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12421 return nl;
12424 /* OpenMP 4.0:
12425 linear ( variable-list )
12426 linear ( variable-list : expression )
12428 OpenMP 4.5:
12429 linear ( modifier ( variable-list ) )
12430 linear ( modifier ( variable-list ) : expression ) */
12432 static tree
12433 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12435 location_t clause_loc = c_parser_peek_token (parser)->location;
12436 tree nl, c, step;
12437 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12439 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12440 return list;
12442 if (!is_cilk_simd_fn
12443 && c_parser_next_token_is (parser, CPP_NAME))
12445 c_token *tok = c_parser_peek_token (parser);
12446 const char *p = IDENTIFIER_POINTER (tok->value);
12447 if (strcmp ("val", p) == 0)
12448 kind = OMP_CLAUSE_LINEAR_VAL;
12449 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12450 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12451 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12453 c_parser_consume_token (parser);
12454 c_parser_consume_token (parser);
12458 nl = c_parser_omp_variable_list (parser, clause_loc,
12459 OMP_CLAUSE_LINEAR, list);
12461 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12462 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12464 if (c_parser_next_token_is (parser, CPP_COLON))
12466 c_parser_consume_token (parser);
12467 step = c_parser_expression (parser).value;
12468 mark_exp_read (step);
12469 step = c_fully_fold (step, false, NULL);
12470 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12472 sorry ("using parameters for %<linear%> step is not supported yet");
12473 step = integer_one_node;
12475 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12477 error_at (clause_loc, "%<linear%> clause step expression must "
12478 "be integral");
12479 step = integer_one_node;
12483 else
12484 step = integer_one_node;
12486 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12488 OMP_CLAUSE_LINEAR_STEP (c) = step;
12489 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12492 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12493 return nl;
12496 /* OpenMP 4.0:
12497 safelen ( constant-expression ) */
12499 static tree
12500 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12502 location_t clause_loc = c_parser_peek_token (parser)->location;
12503 tree c, t;
12505 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12506 return list;
12508 t = c_parser_expr_no_commas (parser, NULL).value;
12509 mark_exp_read (t);
12510 t = c_fully_fold (t, false, NULL);
12511 if (TREE_CODE (t) != INTEGER_CST
12512 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12513 || tree_int_cst_sgn (t) != 1)
12515 error_at (clause_loc, "%<safelen%> clause expression must "
12516 "be positive constant integer expression");
12517 t = NULL_TREE;
12520 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12521 if (t == NULL_TREE || t == error_mark_node)
12522 return list;
12524 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12526 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12527 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12528 OMP_CLAUSE_CHAIN (c) = list;
12529 return c;
12532 /* OpenMP 4.0:
12533 simdlen ( constant-expression ) */
12535 static tree
12536 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12538 location_t clause_loc = c_parser_peek_token (parser)->location;
12539 tree c, t;
12541 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12542 return list;
12544 t = c_parser_expr_no_commas (parser, NULL).value;
12545 mark_exp_read (t);
12546 t = c_fully_fold (t, false, NULL);
12547 if (TREE_CODE (t) != INTEGER_CST
12548 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12549 || tree_int_cst_sgn (t) != 1)
12551 error_at (clause_loc, "%<simdlen%> clause expression must "
12552 "be positive constant integer expression");
12553 t = NULL_TREE;
12556 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12557 if (t == NULL_TREE || t == error_mark_node)
12558 return list;
12560 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12562 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12563 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12564 OMP_CLAUSE_CHAIN (c) = list;
12565 return c;
12568 /* OpenMP 4.5:
12569 vec:
12570 identifier [+/- integer]
12571 vec , identifier [+/- integer]
12574 static tree
12575 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12576 tree list)
12578 tree vec = NULL;
12579 if (c_parser_next_token_is_not (parser, CPP_NAME)
12580 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12582 c_parser_error (parser, "expected identifier");
12583 return list;
12586 while (c_parser_next_token_is (parser, CPP_NAME)
12587 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12589 tree t = lookup_name (c_parser_peek_token (parser)->value);
12590 tree addend = NULL;
12592 if (t == NULL_TREE)
12594 undeclared_variable (c_parser_peek_token (parser)->location,
12595 c_parser_peek_token (parser)->value);
12596 t = error_mark_node;
12599 c_parser_consume_token (parser);
12601 bool neg = false;
12602 if (c_parser_next_token_is (parser, CPP_MINUS))
12603 neg = true;
12604 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12606 addend = integer_zero_node;
12607 neg = false;
12608 goto add_to_vector;
12610 c_parser_consume_token (parser);
12612 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12614 c_parser_error (parser, "expected integer");
12615 return list;
12618 addend = c_parser_peek_token (parser)->value;
12619 if (TREE_CODE (addend) != INTEGER_CST)
12621 c_parser_error (parser, "expected integer");
12622 return list;
12624 c_parser_consume_token (parser);
12626 add_to_vector:
12627 if (t != error_mark_node)
12629 vec = tree_cons (addend, t, vec);
12630 if (neg)
12631 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12634 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12635 break;
12637 c_parser_consume_token (parser);
12640 if (vec == NULL_TREE)
12641 return list;
12643 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12644 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12645 OMP_CLAUSE_DECL (u) = nreverse (vec);
12646 OMP_CLAUSE_CHAIN (u) = list;
12647 return u;
12650 /* OpenMP 4.0:
12651 depend ( depend-kind: variable-list )
12653 depend-kind:
12654 in | out | inout
12656 OpenMP 4.5:
12657 depend ( source )
12659 depend ( sink : vec ) */
12661 static tree
12662 c_parser_omp_clause_depend (c_parser *parser, tree list)
12664 location_t clause_loc = c_parser_peek_token (parser)->location;
12665 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12666 tree nl, c;
12668 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12669 return list;
12671 if (c_parser_next_token_is (parser, CPP_NAME))
12673 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12674 if (strcmp ("in", p) == 0)
12675 kind = OMP_CLAUSE_DEPEND_IN;
12676 else if (strcmp ("inout", p) == 0)
12677 kind = OMP_CLAUSE_DEPEND_INOUT;
12678 else if (strcmp ("out", p) == 0)
12679 kind = OMP_CLAUSE_DEPEND_OUT;
12680 else if (strcmp ("source", p) == 0)
12681 kind = OMP_CLAUSE_DEPEND_SOURCE;
12682 else if (strcmp ("sink", p) == 0)
12683 kind = OMP_CLAUSE_DEPEND_SINK;
12684 else
12685 goto invalid_kind;
12687 else
12688 goto invalid_kind;
12690 c_parser_consume_token (parser);
12692 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12694 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12695 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12696 OMP_CLAUSE_DECL (c) = NULL_TREE;
12697 OMP_CLAUSE_CHAIN (c) = list;
12698 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12699 return c;
12702 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12703 goto resync_fail;
12705 if (kind == OMP_CLAUSE_DEPEND_SINK)
12706 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12707 else
12709 nl = c_parser_omp_variable_list (parser, clause_loc,
12710 OMP_CLAUSE_DEPEND, list);
12712 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12713 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12717 return nl;
12719 invalid_kind:
12720 c_parser_error (parser, "invalid depend kind");
12721 resync_fail:
12722 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12723 return list;
12726 /* OpenMP 4.0:
12727 map ( map-kind: variable-list )
12728 map ( variable-list )
12730 map-kind:
12731 alloc | to | from | tofrom
12733 OpenMP 4.5:
12734 map-kind:
12735 alloc | to | from | tofrom | release | delete
12737 map ( always [,] map-kind: variable-list ) */
12739 static tree
12740 c_parser_omp_clause_map (c_parser *parser, tree list)
12742 location_t clause_loc = c_parser_peek_token (parser)->location;
12743 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12744 int always = 0;
12745 enum c_id_kind always_id_kind = C_ID_NONE;
12746 location_t always_loc = UNKNOWN_LOCATION;
12747 tree always_id = NULL_TREE;
12748 tree nl, c;
12750 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12751 return list;
12753 if (c_parser_next_token_is (parser, CPP_NAME))
12755 c_token *tok = c_parser_peek_token (parser);
12756 const char *p = IDENTIFIER_POINTER (tok->value);
12757 always_id_kind = tok->id_kind;
12758 always_loc = tok->location;
12759 always_id = tok->value;
12760 if (strcmp ("always", p) == 0)
12762 c_token *sectok = c_parser_peek_2nd_token (parser);
12763 if (sectok->type == CPP_COMMA)
12765 c_parser_consume_token (parser);
12766 c_parser_consume_token (parser);
12767 always = 2;
12769 else if (sectok->type == CPP_NAME)
12771 p = IDENTIFIER_POINTER (sectok->value);
12772 if (strcmp ("alloc", p) == 0
12773 || strcmp ("to", p) == 0
12774 || strcmp ("from", p) == 0
12775 || strcmp ("tofrom", p) == 0
12776 || strcmp ("release", p) == 0
12777 || strcmp ("delete", p) == 0)
12779 c_parser_consume_token (parser);
12780 always = 1;
12786 if (c_parser_next_token_is (parser, CPP_NAME)
12787 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12789 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12790 if (strcmp ("alloc", p) == 0)
12791 kind = GOMP_MAP_ALLOC;
12792 else if (strcmp ("to", p) == 0)
12793 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12794 else if (strcmp ("from", p) == 0)
12795 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12796 else if (strcmp ("tofrom", p) == 0)
12797 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12798 else if (strcmp ("release", p) == 0)
12799 kind = GOMP_MAP_RELEASE;
12800 else if (strcmp ("delete", p) == 0)
12801 kind = GOMP_MAP_DELETE;
12802 else
12804 c_parser_error (parser, "invalid map kind");
12805 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12806 "expected %<)%>");
12807 return list;
12809 c_parser_consume_token (parser);
12810 c_parser_consume_token (parser);
12812 else if (always)
12814 if (always_id_kind != C_ID_ID)
12816 c_parser_error (parser, "expected identifier");
12817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12818 return list;
12821 tree t = lookup_name (always_id);
12822 if (t == NULL_TREE)
12824 undeclared_variable (always_loc, always_id);
12825 t = error_mark_node;
12827 if (t != error_mark_node)
12829 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12830 OMP_CLAUSE_DECL (u) = t;
12831 OMP_CLAUSE_CHAIN (u) = list;
12832 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12833 list = u;
12835 if (always == 1)
12837 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12838 return list;
12842 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
12844 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12845 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12847 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12848 return nl;
12851 /* OpenMP 4.0:
12852 device ( expression ) */
12854 static tree
12855 c_parser_omp_clause_device (c_parser *parser, tree list)
12857 location_t clause_loc = c_parser_peek_token (parser)->location;
12858 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12860 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
12861 mark_exp_read (t);
12862 t = c_fully_fold (t, false, NULL);
12864 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12866 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12868 c_parser_error (parser, "expected integer expression");
12869 return list;
12872 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
12874 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
12875 OMP_CLAUSE_DEVICE_ID (c) = t;
12876 OMP_CLAUSE_CHAIN (c) = list;
12877 list = c;
12880 return list;
12883 /* OpenMP 4.0:
12884 dist_schedule ( static )
12885 dist_schedule ( static , expression ) */
12887 static tree
12888 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
12890 tree c, t = NULL_TREE;
12891 location_t loc = c_parser_peek_token (parser)->location;
12893 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12894 return list;
12896 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
12898 c_parser_error (parser, "invalid dist_schedule kind");
12899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12900 "expected %<)%>");
12901 return list;
12904 c_parser_consume_token (parser);
12905 if (c_parser_next_token_is (parser, CPP_COMMA))
12907 c_parser_consume_token (parser);
12909 t = c_parser_expr_no_commas (parser, NULL).value;
12910 mark_exp_read (t);
12911 t = c_fully_fold (t, false, NULL);
12912 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12914 else
12915 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12916 "expected %<,%> or %<)%>");
12918 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12919 if (t == error_mark_node)
12920 return list;
12922 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
12923 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
12924 OMP_CLAUSE_CHAIN (c) = list;
12925 return c;
12928 /* OpenMP 4.0:
12929 proc_bind ( proc-bind-kind )
12931 proc-bind-kind:
12932 master | close | spread */
12934 static tree
12935 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
12937 location_t clause_loc = c_parser_peek_token (parser)->location;
12938 enum omp_clause_proc_bind_kind kind;
12939 tree c;
12941 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12942 return list;
12944 if (c_parser_next_token_is (parser, CPP_NAME))
12946 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12947 if (strcmp ("master", p) == 0)
12948 kind = OMP_CLAUSE_PROC_BIND_MASTER;
12949 else if (strcmp ("close", p) == 0)
12950 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
12951 else if (strcmp ("spread", p) == 0)
12952 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
12953 else
12954 goto invalid_kind;
12956 else
12957 goto invalid_kind;
12959 c_parser_consume_token (parser);
12960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12961 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
12962 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
12963 OMP_CLAUSE_CHAIN (c) = list;
12964 return c;
12966 invalid_kind:
12967 c_parser_error (parser, "invalid proc_bind kind");
12968 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12969 return list;
12972 /* OpenMP 4.0:
12973 to ( variable-list ) */
12975 static tree
12976 c_parser_omp_clause_to (c_parser *parser, tree list)
12978 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
12981 /* OpenMP 4.0:
12982 from ( variable-list ) */
12984 static tree
12985 c_parser_omp_clause_from (c_parser *parser, tree list)
12987 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
12990 /* OpenMP 4.0:
12991 uniform ( variable-list ) */
12993 static tree
12994 c_parser_omp_clause_uniform (c_parser *parser, tree list)
12996 /* The clauses location. */
12997 location_t loc = c_parser_peek_token (parser)->location;
12999 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13001 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13002 list);
13003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13005 return list;
13008 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13009 is a bitmask in MASK. Return the list of clauses found. */
13011 static tree
13012 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13013 const char *where, bool finish_p = true)
13015 tree clauses = NULL;
13016 bool first = true;
13018 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13020 location_t here;
13021 pragma_omp_clause c_kind;
13022 const char *c_name;
13023 tree prev = clauses;
13025 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13026 c_parser_consume_token (parser);
13028 here = c_parser_peek_token (parser)->location;
13029 c_kind = c_parser_omp_clause_name (parser);
13031 switch (c_kind)
13033 case PRAGMA_OACC_CLAUSE_ASYNC:
13034 clauses = c_parser_oacc_clause_async (parser, clauses);
13035 c_name = "async";
13036 break;
13037 case PRAGMA_OACC_CLAUSE_AUTO:
13038 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13039 clauses);
13040 c_name = "auto";
13041 break;
13042 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13043 clauses = c_parser_omp_clause_collapse (parser, clauses);
13044 c_name = "collapse";
13045 break;
13046 case PRAGMA_OACC_CLAUSE_COPY:
13047 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13048 c_name = "copy";
13049 break;
13050 case PRAGMA_OACC_CLAUSE_COPYIN:
13051 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13052 c_name = "copyin";
13053 break;
13054 case PRAGMA_OACC_CLAUSE_COPYOUT:
13055 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13056 c_name = "copyout";
13057 break;
13058 case PRAGMA_OACC_CLAUSE_CREATE:
13059 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13060 c_name = "create";
13061 break;
13062 case PRAGMA_OACC_CLAUSE_DELETE:
13063 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13064 c_name = "delete";
13065 break;
13066 case PRAGMA_OMP_CLAUSE_DEFAULT:
13067 clauses = c_parser_omp_clause_default (parser, clauses, true);
13068 c_name = "default";
13069 break;
13070 case PRAGMA_OACC_CLAUSE_DEVICE:
13071 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13072 c_name = "device";
13073 break;
13074 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13075 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13076 c_name = "deviceptr";
13077 break;
13078 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13079 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13080 c_name = "device_resident";
13081 break;
13082 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13083 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13084 c_name = "firstprivate";
13085 break;
13086 case PRAGMA_OACC_CLAUSE_GANG:
13087 c_name = "gang";
13088 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13089 c_name, clauses);
13090 break;
13091 case PRAGMA_OACC_CLAUSE_HOST:
13092 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13093 c_name = "host";
13094 break;
13095 case PRAGMA_OACC_CLAUSE_IF:
13096 clauses = c_parser_omp_clause_if (parser, clauses, false);
13097 c_name = "if";
13098 break;
13099 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13100 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13101 clauses);
13102 c_name = "independent";
13103 break;
13104 case PRAGMA_OACC_CLAUSE_LINK:
13105 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13106 c_name = "link";
13107 break;
13108 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13109 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13110 c_name = "num_gangs";
13111 break;
13112 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13113 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13114 c_name = "num_workers";
13115 break;
13116 case PRAGMA_OACC_CLAUSE_PRESENT:
13117 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13118 c_name = "present";
13119 break;
13120 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13121 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13122 c_name = "present_or_copy";
13123 break;
13124 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13125 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13126 c_name = "present_or_copyin";
13127 break;
13128 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13129 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13130 c_name = "present_or_copyout";
13131 break;
13132 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13133 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13134 c_name = "present_or_create";
13135 break;
13136 case PRAGMA_OACC_CLAUSE_PRIVATE:
13137 clauses = c_parser_omp_clause_private (parser, clauses);
13138 c_name = "private";
13139 break;
13140 case PRAGMA_OACC_CLAUSE_REDUCTION:
13141 clauses = c_parser_omp_clause_reduction (parser, clauses);
13142 c_name = "reduction";
13143 break;
13144 case PRAGMA_OACC_CLAUSE_SELF:
13145 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13146 c_name = "self";
13147 break;
13148 case PRAGMA_OACC_CLAUSE_SEQ:
13149 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13150 clauses);
13151 c_name = "seq";
13152 break;
13153 case PRAGMA_OACC_CLAUSE_TILE:
13154 clauses = c_parser_oacc_clause_tile (parser, clauses);
13155 c_name = "tile";
13156 break;
13157 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13158 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13159 c_name = "use_device";
13160 break;
13161 case PRAGMA_OACC_CLAUSE_VECTOR:
13162 c_name = "vector";
13163 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13164 c_name, clauses);
13165 break;
13166 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13167 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13168 c_name = "vector_length";
13169 break;
13170 case PRAGMA_OACC_CLAUSE_WAIT:
13171 clauses = c_parser_oacc_clause_wait (parser, clauses);
13172 c_name = "wait";
13173 break;
13174 case PRAGMA_OACC_CLAUSE_WORKER:
13175 c_name = "worker";
13176 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13177 c_name, clauses);
13178 break;
13179 default:
13180 c_parser_error (parser, "expected %<#pragma acc%> clause");
13181 goto saw_error;
13184 first = false;
13186 if (((mask >> c_kind) & 1) == 0)
13188 /* Remove the invalid clause(s) from the list to avoid
13189 confusing the rest of the compiler. */
13190 clauses = prev;
13191 error_at (here, "%qs is not valid for %qs", c_name, where);
13195 saw_error:
13196 c_parser_skip_to_pragma_eol (parser);
13198 if (finish_p)
13199 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13201 return clauses;
13204 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13205 is a bitmask in MASK. Return the list of clauses found. */
13207 static tree
13208 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13209 const char *where, bool finish_p = true)
13211 tree clauses = NULL;
13212 bool first = true, cilk_simd_fn = false;
13214 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13216 location_t here;
13217 pragma_omp_clause c_kind;
13218 const char *c_name;
13219 tree prev = clauses;
13221 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13222 c_parser_consume_token (parser);
13224 here = c_parser_peek_token (parser)->location;
13225 c_kind = c_parser_omp_clause_name (parser);
13227 switch (c_kind)
13229 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13230 clauses = c_parser_omp_clause_collapse (parser, clauses);
13231 c_name = "collapse";
13232 break;
13233 case PRAGMA_OMP_CLAUSE_COPYIN:
13234 clauses = c_parser_omp_clause_copyin (parser, clauses);
13235 c_name = "copyin";
13236 break;
13237 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13238 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13239 c_name = "copyprivate";
13240 break;
13241 case PRAGMA_OMP_CLAUSE_DEFAULT:
13242 clauses = c_parser_omp_clause_default (parser, clauses, false);
13243 c_name = "default";
13244 break;
13245 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13246 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13247 c_name = "firstprivate";
13248 break;
13249 case PRAGMA_OMP_CLAUSE_FINAL:
13250 clauses = c_parser_omp_clause_final (parser, clauses);
13251 c_name = "final";
13252 break;
13253 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13254 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13255 c_name = "grainsize";
13256 break;
13257 case PRAGMA_OMP_CLAUSE_HINT:
13258 clauses = c_parser_omp_clause_hint (parser, clauses);
13259 c_name = "hint";
13260 break;
13261 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13262 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13263 c_name = "defaultmap";
13264 break;
13265 case PRAGMA_OMP_CLAUSE_IF:
13266 clauses = c_parser_omp_clause_if (parser, clauses, true);
13267 c_name = "if";
13268 break;
13269 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13270 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13271 c_name = "lastprivate";
13272 break;
13273 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13274 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13275 c_name = "mergeable";
13276 break;
13277 case PRAGMA_OMP_CLAUSE_NOWAIT:
13278 clauses = c_parser_omp_clause_nowait (parser, clauses);
13279 c_name = "nowait";
13280 break;
13281 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13282 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13283 c_name = "num_tasks";
13284 break;
13285 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13286 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13287 c_name = "num_threads";
13288 break;
13289 case PRAGMA_OMP_CLAUSE_ORDERED:
13290 clauses = c_parser_omp_clause_ordered (parser, clauses);
13291 c_name = "ordered";
13292 break;
13293 case PRAGMA_OMP_CLAUSE_PRIORITY:
13294 clauses = c_parser_omp_clause_priority (parser, clauses);
13295 c_name = "priority";
13296 break;
13297 case PRAGMA_OMP_CLAUSE_PRIVATE:
13298 clauses = c_parser_omp_clause_private (parser, clauses);
13299 c_name = "private";
13300 break;
13301 case PRAGMA_OMP_CLAUSE_REDUCTION:
13302 clauses = c_parser_omp_clause_reduction (parser, clauses);
13303 c_name = "reduction";
13304 break;
13305 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13306 clauses = c_parser_omp_clause_schedule (parser, clauses);
13307 c_name = "schedule";
13308 break;
13309 case PRAGMA_OMP_CLAUSE_SHARED:
13310 clauses = c_parser_omp_clause_shared (parser, clauses);
13311 c_name = "shared";
13312 break;
13313 case PRAGMA_OMP_CLAUSE_UNTIED:
13314 clauses = c_parser_omp_clause_untied (parser, clauses);
13315 c_name = "untied";
13316 break;
13317 case PRAGMA_OMP_CLAUSE_INBRANCH:
13318 case PRAGMA_CILK_CLAUSE_MASK:
13319 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13320 clauses);
13321 c_name = "inbranch";
13322 break;
13323 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13324 case PRAGMA_CILK_CLAUSE_NOMASK:
13325 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13326 clauses);
13327 c_name = "notinbranch";
13328 break;
13329 case PRAGMA_OMP_CLAUSE_PARALLEL:
13330 clauses
13331 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13332 clauses);
13333 c_name = "parallel";
13334 if (!first)
13336 clause_not_first:
13337 error_at (here, "%qs must be the first clause of %qs",
13338 c_name, where);
13339 clauses = prev;
13341 break;
13342 case PRAGMA_OMP_CLAUSE_FOR:
13343 clauses
13344 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13345 clauses);
13346 c_name = "for";
13347 if (!first)
13348 goto clause_not_first;
13349 break;
13350 case PRAGMA_OMP_CLAUSE_SECTIONS:
13351 clauses
13352 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13353 clauses);
13354 c_name = "sections";
13355 if (!first)
13356 goto clause_not_first;
13357 break;
13358 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13359 clauses
13360 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13361 clauses);
13362 c_name = "taskgroup";
13363 if (!first)
13364 goto clause_not_first;
13365 break;
13366 case PRAGMA_OMP_CLAUSE_LINK:
13367 clauses
13368 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13369 c_name = "link";
13370 break;
13371 case PRAGMA_OMP_CLAUSE_TO:
13372 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13373 clauses
13374 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13375 clauses);
13376 else
13377 clauses = c_parser_omp_clause_to (parser, clauses);
13378 c_name = "to";
13379 break;
13380 case PRAGMA_OMP_CLAUSE_FROM:
13381 clauses = c_parser_omp_clause_from (parser, clauses);
13382 c_name = "from";
13383 break;
13384 case PRAGMA_OMP_CLAUSE_UNIFORM:
13385 clauses = c_parser_omp_clause_uniform (parser, clauses);
13386 c_name = "uniform";
13387 break;
13388 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13389 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13390 c_name = "num_teams";
13391 break;
13392 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13393 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13394 c_name = "thread_limit";
13395 break;
13396 case PRAGMA_OMP_CLAUSE_ALIGNED:
13397 clauses = c_parser_omp_clause_aligned (parser, clauses);
13398 c_name = "aligned";
13399 break;
13400 case PRAGMA_OMP_CLAUSE_LINEAR:
13401 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13402 cilk_simd_fn = true;
13403 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13404 c_name = "linear";
13405 break;
13406 case PRAGMA_OMP_CLAUSE_DEPEND:
13407 clauses = c_parser_omp_clause_depend (parser, clauses);
13408 c_name = "depend";
13409 break;
13410 case PRAGMA_OMP_CLAUSE_MAP:
13411 clauses = c_parser_omp_clause_map (parser, clauses);
13412 c_name = "map";
13413 break;
13414 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13415 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13416 c_name = "use_device_ptr";
13417 break;
13418 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13419 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13420 c_name = "is_device_ptr";
13421 break;
13422 case PRAGMA_OMP_CLAUSE_DEVICE:
13423 clauses = c_parser_omp_clause_device (parser, clauses);
13424 c_name = "device";
13425 break;
13426 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13427 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13428 c_name = "dist_schedule";
13429 break;
13430 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13431 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13432 c_name = "proc_bind";
13433 break;
13434 case PRAGMA_OMP_CLAUSE_SAFELEN:
13435 clauses = c_parser_omp_clause_safelen (parser, clauses);
13436 c_name = "safelen";
13437 break;
13438 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13439 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13440 c_name = "simdlen";
13441 break;
13442 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13443 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13444 c_name = "simdlen";
13445 break;
13446 case PRAGMA_OMP_CLAUSE_NOGROUP:
13447 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13448 c_name = "nogroup";
13449 break;
13450 case PRAGMA_OMP_CLAUSE_THREADS:
13451 clauses
13452 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13453 clauses);
13454 c_name = "threads";
13455 break;
13456 case PRAGMA_OMP_CLAUSE_SIMD:
13457 clauses
13458 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13459 clauses);
13460 c_name = "simd";
13461 break;
13462 default:
13463 c_parser_error (parser, "expected %<#pragma omp%> clause");
13464 goto saw_error;
13467 first = false;
13469 if (((mask >> c_kind) & 1) == 0)
13471 /* Remove the invalid clause(s) from the list to avoid
13472 confusing the rest of the compiler. */
13473 clauses = prev;
13474 error_at (here, "%qs is not valid for %qs", c_name, where);
13478 saw_error:
13479 c_parser_skip_to_pragma_eol (parser);
13481 if (finish_p)
13483 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13484 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
13485 return c_finish_omp_clauses (clauses, C_ORT_OMP);
13488 return clauses;
13491 /* OpenACC 2.0, OpenMP 2.5:
13492 structured-block:
13493 statement
13495 In practice, we're also interested in adding the statement to an
13496 outer node. So it is convenient if we work around the fact that
13497 c_parser_statement calls add_stmt. */
13499 static tree
13500 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13502 tree stmt = push_stmt_list ();
13503 c_parser_statement (parser, if_p);
13504 return pop_stmt_list (stmt);
13507 /* OpenACC 2.0:
13508 # pragma acc cache (variable-list) new-line
13510 LOC is the location of the #pragma token.
13513 static tree
13514 c_parser_oacc_cache (location_t loc, c_parser *parser)
13516 tree stmt, clauses;
13518 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13519 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13521 c_parser_skip_to_pragma_eol (parser);
13523 stmt = make_node (OACC_CACHE);
13524 TREE_TYPE (stmt) = void_type_node;
13525 OACC_CACHE_CLAUSES (stmt) = clauses;
13526 SET_EXPR_LOCATION (stmt, loc);
13527 add_stmt (stmt);
13529 return stmt;
13532 /* OpenACC 2.0:
13533 # pragma acc data oacc-data-clause[optseq] new-line
13534 structured-block
13536 LOC is the location of the #pragma token.
13539 #define OACC_DATA_CLAUSE_MASK \
13540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13552 static tree
13553 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13555 tree stmt, clauses, block;
13557 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13558 "#pragma acc data");
13560 block = c_begin_omp_parallel ();
13561 add_stmt (c_parser_omp_structured_block (parser, if_p));
13563 stmt = c_finish_oacc_data (loc, clauses, block);
13565 return stmt;
13568 /* OpenACC 2.0:
13569 # pragma acc declare oacc-data-clause[optseq] new-line
13572 #define OACC_DECLARE_CLAUSE_MASK \
13573 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13586 static void
13587 c_parser_oacc_declare (c_parser *parser)
13589 location_t pragma_loc = c_parser_peek_token (parser)->location;
13590 tree clauses, stmt, t, decl;
13592 bool error = false;
13594 c_parser_consume_pragma (parser);
13596 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13597 "#pragma acc declare");
13598 if (!clauses)
13600 error_at (pragma_loc,
13601 "no valid clauses specified in %<#pragma acc declare%>");
13602 return;
13605 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13607 location_t loc = OMP_CLAUSE_LOCATION (t);
13608 decl = OMP_CLAUSE_DECL (t);
13609 if (!DECL_P (decl))
13611 error_at (loc, "array section in %<#pragma acc declare%>");
13612 error = true;
13613 continue;
13616 switch (OMP_CLAUSE_MAP_KIND (t))
13618 case GOMP_MAP_FIRSTPRIVATE_POINTER:
13619 case GOMP_MAP_FORCE_ALLOC:
13620 case GOMP_MAP_FORCE_TO:
13621 case GOMP_MAP_FORCE_DEVICEPTR:
13622 case GOMP_MAP_DEVICE_RESIDENT:
13623 break;
13625 case GOMP_MAP_POINTER:
13626 /* Generated by c_finish_omp_clauses from array sections;
13627 avoid spurious diagnostics. */
13628 break;
13630 case GOMP_MAP_LINK:
13631 if (!global_bindings_p ()
13632 && (TREE_STATIC (decl)
13633 || !DECL_EXTERNAL (decl)))
13635 error_at (loc,
13636 "%qD must be a global variable in"
13637 "%<#pragma acc declare link%>",
13638 decl);
13639 error = true;
13640 continue;
13642 break;
13644 default:
13645 if (global_bindings_p ())
13647 error_at (loc, "invalid OpenACC clause at file scope");
13648 error = true;
13649 continue;
13651 if (DECL_EXTERNAL (decl))
13653 error_at (loc,
13654 "invalid use of %<extern%> variable %qD "
13655 "in %<#pragma acc declare%>", decl);
13656 error = true;
13657 continue;
13659 else if (TREE_PUBLIC (decl))
13661 error_at (loc,
13662 "invalid use of %<global%> variable %qD "
13663 "in %<#pragma acc declare%>", decl);
13664 error = true;
13665 continue;
13667 break;
13670 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13671 || lookup_attribute ("omp declare target link",
13672 DECL_ATTRIBUTES (decl)))
13674 error_at (loc, "variable %qD used more than once with "
13675 "%<#pragma acc declare%>", decl);
13676 error = true;
13677 continue;
13680 if (!error)
13682 tree id;
13684 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13685 id = get_identifier ("omp declare target link");
13686 else
13687 id = get_identifier ("omp declare target");
13689 DECL_ATTRIBUTES (decl)
13690 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13692 if (global_bindings_p ())
13694 symtab_node *node = symtab_node::get (decl);
13695 if (node != NULL)
13697 node->offloadable = 1;
13698 if (ENABLE_OFFLOADING)
13700 g->have_offload = true;
13701 if (is_a <varpool_node *> (node))
13702 vec_safe_push (offload_vars, decl);
13709 if (error || global_bindings_p ())
13710 return;
13712 stmt = make_node (OACC_DECLARE);
13713 TREE_TYPE (stmt) = void_type_node;
13714 OACC_DECLARE_CLAUSES (stmt) = clauses;
13715 SET_EXPR_LOCATION (stmt, pragma_loc);
13717 add_stmt (stmt);
13719 return;
13722 /* OpenACC 2.0:
13723 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13727 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13730 LOC is the location of the #pragma token.
13733 #define OACC_ENTER_DATA_CLAUSE_MASK \
13734 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13742 #define OACC_EXIT_DATA_CLAUSE_MASK \
13743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13749 static void
13750 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13752 location_t loc = c_parser_peek_token (parser)->location;
13753 tree clauses, stmt;
13755 c_parser_consume_pragma (parser);
13757 if (!c_parser_next_token_is (parser, CPP_NAME))
13759 c_parser_error (parser, enter
13760 ? "expected %<data%> in %<#pragma acc enter data%>"
13761 : "expected %<data%> in %<#pragma acc exit data%>");
13762 c_parser_skip_to_pragma_eol (parser);
13763 return;
13766 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13767 if (strcmp (p, "data") != 0)
13769 c_parser_error (parser, "invalid pragma");
13770 c_parser_skip_to_pragma_eol (parser);
13771 return;
13774 c_parser_consume_token (parser);
13776 if (enter)
13777 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13778 "#pragma acc enter data");
13779 else
13780 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13781 "#pragma acc exit data");
13783 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13785 error_at (loc, enter
13786 ? "%<#pragma acc enter data%> has no data movement clause"
13787 : "%<#pragma acc exit data%> has no data movement clause");
13788 return;
13791 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13792 TREE_TYPE (stmt) = void_type_node;
13793 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13794 SET_EXPR_LOCATION (stmt, loc);
13795 add_stmt (stmt);
13799 /* OpenACC 2.0:
13800 # pragma acc host_data oacc-data-clause[optseq] new-line
13801 structured-block
13804 #define OACC_HOST_DATA_CLAUSE_MASK \
13805 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13807 static tree
13808 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
13810 tree stmt, clauses, block;
13812 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13813 "#pragma acc host_data");
13815 block = c_begin_omp_parallel ();
13816 add_stmt (c_parser_omp_structured_block (parser, if_p));
13817 stmt = c_finish_oacc_host_data (loc, clauses, block);
13818 return stmt;
13822 /* OpenACC 2.0:
13824 # pragma acc loop oacc-loop-clause[optseq] new-line
13825 structured-block
13827 LOC is the location of the #pragma token.
13830 #define OACC_LOOP_CLAUSE_MASK \
13831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
13839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
13841 static tree
13842 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
13843 omp_clause_mask mask, tree *cclauses, bool *if_p)
13845 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
13847 strcat (p_name, " loop");
13848 mask |= OACC_LOOP_CLAUSE_MASK;
13850 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
13851 cclauses == NULL);
13852 if (cclauses)
13854 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
13855 if (*cclauses)
13856 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
13857 if (clauses)
13858 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13861 tree block = c_begin_compound_stmt (true);
13862 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
13863 if_p);
13864 block = c_end_compound_stmt (loc, block, true);
13865 add_stmt (block);
13867 return stmt;
13870 /* OpenACC 2.0:
13871 # pragma acc kernels oacc-kernels-clause[optseq] new-line
13872 structured-block
13876 # pragma acc parallel oacc-parallel-clause[optseq] new-line
13877 structured-block
13879 LOC is the location of the #pragma token.
13882 #define OACC_KERNELS_CLAUSE_MASK \
13883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13898 #define OACC_PARALLEL_CLAUSE_MASK \
13899 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
13909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
13910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
13911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
13918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13920 static tree
13921 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
13922 enum pragma_kind p_kind, char *p_name,
13923 bool *if_p)
13925 omp_clause_mask mask;
13926 enum tree_code code;
13927 switch (p_kind)
13929 case PRAGMA_OACC_KERNELS:
13930 strcat (p_name, " kernels");
13931 mask = OACC_KERNELS_CLAUSE_MASK;
13932 code = OACC_KERNELS;
13933 break;
13934 case PRAGMA_OACC_PARALLEL:
13935 strcat (p_name, " parallel");
13936 mask = OACC_PARALLEL_CLAUSE_MASK;
13937 code = OACC_PARALLEL;
13938 break;
13939 default:
13940 gcc_unreachable ();
13943 if (c_parser_next_token_is (parser, CPP_NAME))
13945 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13946 if (strcmp (p, "loop") == 0)
13948 c_parser_consume_token (parser);
13949 tree block = c_begin_omp_parallel ();
13950 tree clauses;
13951 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
13952 return c_finish_omp_construct (loc, code, block, clauses);
13956 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
13958 tree block = c_begin_omp_parallel ();
13959 add_stmt (c_parser_omp_structured_block (parser, if_p));
13961 return c_finish_omp_construct (loc, code, block, clauses);
13964 /* OpenACC 2.0:
13965 # pragma acc routine oacc-routine-clause[optseq] new-line
13966 function-definition
13968 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
13971 #define OACC_ROUTINE_CLAUSE_MASK \
13972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
13977 /* Parse an OpenACC routine directive. For named directives, we apply
13978 immediately to the named function. For unnamed ones we then parse
13979 a declaration or definition, which must be for a function. */
13981 static void
13982 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
13984 tree decl = NULL_TREE;
13985 /* Create a dummy claue, to record location. */
13986 tree c_head = build_omp_clause (c_parser_peek_token (parser)->location,
13987 OMP_CLAUSE_SEQ);
13989 if (context != pragma_external)
13990 c_parser_error (parser, "%<#pragma acc routine%> not at file scope");
13992 c_parser_consume_pragma (parser);
13994 /* Scan for optional '( name )'. */
13995 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13997 c_parser_consume_token (parser);
13999 c_token *token = c_parser_peek_token (parser);
14000 if (token->type == CPP_NAME && (token->id_kind == C_ID_ID
14001 || token->id_kind == C_ID_TYPENAME))
14003 decl = lookup_name (token->value);
14004 if (!decl)
14005 error_at (token->location, "%qE has not been declared",
14006 token->value);
14007 c_parser_consume_token (parser);
14009 else
14010 c_parser_error (parser, "expected function name");
14012 if (!decl
14013 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14015 c_parser_skip_to_pragma_eol (parser, false);
14016 return;
14020 /* Build a chain of clauses. */
14021 parser->in_pragma = true;
14022 tree clauses = c_parser_oacc_all_clauses
14023 (parser, OACC_ROUTINE_CLAUSE_MASK, "#pragma acc routine");
14025 /* Force clauses to be non-null, by attaching context to it. */
14026 clauses = tree_cons (c_head, clauses, NULL_TREE);
14028 if (decl)
14029 c_finish_oacc_routine (parser, decl, clauses, true, true, false);
14030 else if (c_parser_peek_token (parser)->type == CPP_PRAGMA)
14031 /* This will emit an error. */
14032 c_finish_oacc_routine (parser, NULL_TREE, clauses, false, true, false);
14033 else
14034 c_parser_declaration_or_fndef (parser, true, false, false, false,
14035 true, NULL, vNULL, clauses);
14038 /* Finalize an OpenACC routine pragma, applying it to FNDECL. CLAUSES
14039 are the parsed clauses. IS_DEFN is true if we're applying it to
14040 the definition (so expect FNDEF to look somewhat defined. */
14042 static void
14043 c_finish_oacc_routine (c_parser *ARG_UNUSED (parser), tree fndecl,
14044 tree clauses, bool named, bool first, bool is_defn)
14046 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
14048 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL || !first)
14050 if (fndecl != error_mark_node)
14051 error_at (loc, "%<#pragma acc routine%> %s",
14052 named ? "does not refer to a function"
14053 : "not followed by single function");
14054 return;
14057 if (get_oacc_fn_attrib (fndecl))
14058 error_at (loc, "%<#pragma acc routine%> already applied to %D", fndecl);
14060 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14061 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
14062 TREE_USED (fndecl) ? "use" : "definition");
14064 /* Process for function attrib */
14065 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
14066 replace_oacc_fn_attrib (fndecl, dims);
14068 /* Also attach as a declare. */
14069 DECL_ATTRIBUTES (fndecl)
14070 = tree_cons (get_identifier ("omp declare target"),
14071 clauses, DECL_ATTRIBUTES (fndecl));
14074 /* OpenACC 2.0:
14075 # pragma acc update oacc-update-clause[optseq] new-line
14078 #define OACC_UPDATE_CLAUSE_MASK \
14079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14086 static void
14087 c_parser_oacc_update (c_parser *parser)
14089 location_t loc = c_parser_peek_token (parser)->location;
14091 c_parser_consume_pragma (parser);
14093 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14094 "#pragma acc update");
14095 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14097 error_at (loc,
14098 "%<#pragma acc update%> must contain at least one "
14099 "%<device%> or %<host%> or %<self%> clause");
14100 return;
14103 if (parser->error)
14104 return;
14106 tree stmt = make_node (OACC_UPDATE);
14107 TREE_TYPE (stmt) = void_type_node;
14108 OACC_UPDATE_CLAUSES (stmt) = clauses;
14109 SET_EXPR_LOCATION (stmt, loc);
14110 add_stmt (stmt);
14113 /* OpenACC 2.0:
14114 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14116 LOC is the location of the #pragma token.
14119 #define OACC_WAIT_CLAUSE_MASK \
14120 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14122 static tree
14123 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14125 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14127 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14128 list = c_parser_oacc_wait_list (parser, loc, list);
14130 strcpy (p_name, " wait");
14131 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14132 stmt = c_finish_oacc_wait (loc, list, clauses);
14133 add_stmt (stmt);
14135 return stmt;
14138 /* OpenMP 2.5:
14139 # pragma omp atomic new-line
14140 expression-stmt
14142 expression-stmt:
14143 x binop= expr | x++ | ++x | x-- | --x
14144 binop:
14145 +, *, -, /, &, ^, |, <<, >>
14147 where x is an lvalue expression with scalar type.
14149 OpenMP 3.1:
14150 # pragma omp atomic new-line
14151 update-stmt
14153 # pragma omp atomic read new-line
14154 read-stmt
14156 # pragma omp atomic write new-line
14157 write-stmt
14159 # pragma omp atomic update new-line
14160 update-stmt
14162 # pragma omp atomic capture new-line
14163 capture-stmt
14165 # pragma omp atomic capture new-line
14166 capture-block
14168 read-stmt:
14169 v = x
14170 write-stmt:
14171 x = expr
14172 update-stmt:
14173 expression-stmt | x = x binop expr
14174 capture-stmt:
14175 v = expression-stmt
14176 capture-block:
14177 { v = x; update-stmt; } | { update-stmt; v = x; }
14179 OpenMP 4.0:
14180 update-stmt:
14181 expression-stmt | x = x binop expr | x = expr binop x
14182 capture-stmt:
14183 v = update-stmt
14184 capture-block:
14185 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14187 where x and v are lvalue expressions with scalar type.
14189 LOC is the location of the #pragma token. */
14191 static void
14192 c_parser_omp_atomic (location_t loc, c_parser *parser)
14194 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14195 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14196 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14197 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14198 struct c_expr expr;
14199 location_t eloc;
14200 bool structured_block = false;
14201 bool swapped = false;
14202 bool seq_cst = false;
14203 bool non_lvalue_p;
14205 if (c_parser_next_token_is (parser, CPP_NAME))
14207 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14208 if (!strcmp (p, "seq_cst"))
14210 seq_cst = true;
14211 c_parser_consume_token (parser);
14212 if (c_parser_next_token_is (parser, CPP_COMMA)
14213 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14214 c_parser_consume_token (parser);
14217 if (c_parser_next_token_is (parser, CPP_NAME))
14219 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14221 if (!strcmp (p, "read"))
14222 code = OMP_ATOMIC_READ;
14223 else if (!strcmp (p, "write"))
14224 code = NOP_EXPR;
14225 else if (!strcmp (p, "update"))
14226 code = OMP_ATOMIC;
14227 else if (!strcmp (p, "capture"))
14228 code = OMP_ATOMIC_CAPTURE_NEW;
14229 else
14230 p = NULL;
14231 if (p)
14232 c_parser_consume_token (parser);
14234 if (!seq_cst)
14236 if (c_parser_next_token_is (parser, CPP_COMMA)
14237 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14238 c_parser_consume_token (parser);
14240 if (c_parser_next_token_is (parser, CPP_NAME))
14242 const char *p
14243 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14244 if (!strcmp (p, "seq_cst"))
14246 seq_cst = true;
14247 c_parser_consume_token (parser);
14251 c_parser_skip_to_pragma_eol (parser);
14253 switch (code)
14255 case OMP_ATOMIC_READ:
14256 case NOP_EXPR: /* atomic write */
14257 v = c_parser_cast_expression (parser, NULL).value;
14258 non_lvalue_p = !lvalue_p (v);
14259 v = c_fully_fold (v, false, NULL);
14260 if (v == error_mark_node)
14261 goto saw_error;
14262 if (non_lvalue_p)
14263 v = non_lvalue (v);
14264 loc = c_parser_peek_token (parser)->location;
14265 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14266 goto saw_error;
14267 if (code == NOP_EXPR)
14269 lhs = c_parser_expression (parser).value;
14270 lhs = c_fully_fold (lhs, false, NULL);
14271 if (lhs == error_mark_node)
14272 goto saw_error;
14274 else
14276 lhs = c_parser_cast_expression (parser, NULL).value;
14277 non_lvalue_p = !lvalue_p (lhs);
14278 lhs = c_fully_fold (lhs, false, NULL);
14279 if (lhs == error_mark_node)
14280 goto saw_error;
14281 if (non_lvalue_p)
14282 lhs = non_lvalue (lhs);
14284 if (code == NOP_EXPR)
14286 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14287 opcode. */
14288 code = OMP_ATOMIC;
14289 rhs = lhs;
14290 lhs = v;
14291 v = NULL_TREE;
14293 goto done;
14294 case OMP_ATOMIC_CAPTURE_NEW:
14295 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14297 c_parser_consume_token (parser);
14298 structured_block = true;
14300 else
14302 v = c_parser_cast_expression (parser, NULL).value;
14303 non_lvalue_p = !lvalue_p (v);
14304 v = c_fully_fold (v, false, NULL);
14305 if (v == error_mark_node)
14306 goto saw_error;
14307 if (non_lvalue_p)
14308 v = non_lvalue (v);
14309 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14310 goto saw_error;
14312 break;
14313 default:
14314 break;
14317 /* For structured_block case we don't know yet whether
14318 old or new x should be captured. */
14319 restart:
14320 eloc = c_parser_peek_token (parser)->location;
14321 expr = c_parser_cast_expression (parser, NULL);
14322 lhs = expr.value;
14323 expr = default_function_array_conversion (eloc, expr);
14324 unfolded_lhs = expr.value;
14325 lhs = c_fully_fold (lhs, false, NULL);
14326 orig_lhs = lhs;
14327 switch (TREE_CODE (lhs))
14329 case ERROR_MARK:
14330 saw_error:
14331 c_parser_skip_to_end_of_block_or_statement (parser);
14332 if (structured_block)
14334 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14335 c_parser_consume_token (parser);
14336 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14338 c_parser_skip_to_end_of_block_or_statement (parser);
14339 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14340 c_parser_consume_token (parser);
14343 return;
14345 case POSTINCREMENT_EXPR:
14346 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14347 code = OMP_ATOMIC_CAPTURE_OLD;
14348 /* FALLTHROUGH */
14349 case PREINCREMENT_EXPR:
14350 lhs = TREE_OPERAND (lhs, 0);
14351 unfolded_lhs = NULL_TREE;
14352 opcode = PLUS_EXPR;
14353 rhs = integer_one_node;
14354 break;
14356 case POSTDECREMENT_EXPR:
14357 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14358 code = OMP_ATOMIC_CAPTURE_OLD;
14359 /* FALLTHROUGH */
14360 case PREDECREMENT_EXPR:
14361 lhs = TREE_OPERAND (lhs, 0);
14362 unfolded_lhs = NULL_TREE;
14363 opcode = MINUS_EXPR;
14364 rhs = integer_one_node;
14365 break;
14367 case COMPOUND_EXPR:
14368 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14369 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14370 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14371 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14372 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14373 (TREE_OPERAND (lhs, 1), 0), 0)))
14374 == BOOLEAN_TYPE)
14375 /* Undo effects of boolean_increment for post {in,de}crement. */
14376 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14377 /* FALLTHRU */
14378 case MODIFY_EXPR:
14379 if (TREE_CODE (lhs) == MODIFY_EXPR
14380 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14382 /* Undo effects of boolean_increment. */
14383 if (integer_onep (TREE_OPERAND (lhs, 1)))
14385 /* This is pre or post increment. */
14386 rhs = TREE_OPERAND (lhs, 1);
14387 lhs = TREE_OPERAND (lhs, 0);
14388 unfolded_lhs = NULL_TREE;
14389 opcode = NOP_EXPR;
14390 if (code == OMP_ATOMIC_CAPTURE_NEW
14391 && !structured_block
14392 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14393 code = OMP_ATOMIC_CAPTURE_OLD;
14394 break;
14396 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14397 && TREE_OPERAND (lhs, 0)
14398 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14400 /* This is pre or post decrement. */
14401 rhs = TREE_OPERAND (lhs, 1);
14402 lhs = TREE_OPERAND (lhs, 0);
14403 unfolded_lhs = NULL_TREE;
14404 opcode = NOP_EXPR;
14405 if (code == OMP_ATOMIC_CAPTURE_NEW
14406 && !structured_block
14407 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14408 code = OMP_ATOMIC_CAPTURE_OLD;
14409 break;
14412 /* FALLTHRU */
14413 default:
14414 if (!lvalue_p (unfolded_lhs))
14415 lhs = non_lvalue (lhs);
14416 switch (c_parser_peek_token (parser)->type)
14418 case CPP_MULT_EQ:
14419 opcode = MULT_EXPR;
14420 break;
14421 case CPP_DIV_EQ:
14422 opcode = TRUNC_DIV_EXPR;
14423 break;
14424 case CPP_PLUS_EQ:
14425 opcode = PLUS_EXPR;
14426 break;
14427 case CPP_MINUS_EQ:
14428 opcode = MINUS_EXPR;
14429 break;
14430 case CPP_LSHIFT_EQ:
14431 opcode = LSHIFT_EXPR;
14432 break;
14433 case CPP_RSHIFT_EQ:
14434 opcode = RSHIFT_EXPR;
14435 break;
14436 case CPP_AND_EQ:
14437 opcode = BIT_AND_EXPR;
14438 break;
14439 case CPP_OR_EQ:
14440 opcode = BIT_IOR_EXPR;
14441 break;
14442 case CPP_XOR_EQ:
14443 opcode = BIT_XOR_EXPR;
14444 break;
14445 case CPP_EQ:
14446 c_parser_consume_token (parser);
14447 eloc = c_parser_peek_token (parser)->location;
14448 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14449 rhs1 = expr.value;
14450 switch (TREE_CODE (rhs1))
14452 case MULT_EXPR:
14453 case TRUNC_DIV_EXPR:
14454 case RDIV_EXPR:
14455 case PLUS_EXPR:
14456 case MINUS_EXPR:
14457 case LSHIFT_EXPR:
14458 case RSHIFT_EXPR:
14459 case BIT_AND_EXPR:
14460 case BIT_IOR_EXPR:
14461 case BIT_XOR_EXPR:
14462 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14464 opcode = TREE_CODE (rhs1);
14465 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14466 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14467 goto stmt_done;
14469 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14471 opcode = TREE_CODE (rhs1);
14472 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14473 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14474 swapped = !commutative_tree_code (opcode);
14475 goto stmt_done;
14477 break;
14478 case ERROR_MARK:
14479 goto saw_error;
14480 default:
14481 break;
14483 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14485 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14487 code = OMP_ATOMIC_CAPTURE_OLD;
14488 v = lhs;
14489 lhs = NULL_TREE;
14490 expr = default_function_array_read_conversion (eloc, expr);
14491 unfolded_lhs1 = expr.value;
14492 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14493 rhs1 = NULL_TREE;
14494 c_parser_consume_token (parser);
14495 goto restart;
14497 if (structured_block)
14499 opcode = NOP_EXPR;
14500 expr = default_function_array_read_conversion (eloc, expr);
14501 rhs = c_fully_fold (expr.value, false, NULL);
14502 rhs1 = NULL_TREE;
14503 goto stmt_done;
14506 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14507 goto saw_error;
14508 default:
14509 c_parser_error (parser,
14510 "invalid operator for %<#pragma omp atomic%>");
14511 goto saw_error;
14514 /* Arrange to pass the location of the assignment operator to
14515 c_finish_omp_atomic. */
14516 loc = c_parser_peek_token (parser)->location;
14517 c_parser_consume_token (parser);
14518 eloc = c_parser_peek_token (parser)->location;
14519 expr = c_parser_expression (parser);
14520 expr = default_function_array_read_conversion (eloc, expr);
14521 rhs = expr.value;
14522 rhs = c_fully_fold (rhs, false, NULL);
14523 break;
14525 stmt_done:
14526 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14528 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14529 goto saw_error;
14530 v = c_parser_cast_expression (parser, NULL).value;
14531 non_lvalue_p = !lvalue_p (v);
14532 v = c_fully_fold (v, false, NULL);
14533 if (v == error_mark_node)
14534 goto saw_error;
14535 if (non_lvalue_p)
14536 v = non_lvalue (v);
14537 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14538 goto saw_error;
14539 eloc = c_parser_peek_token (parser)->location;
14540 expr = c_parser_cast_expression (parser, NULL);
14541 lhs1 = expr.value;
14542 expr = default_function_array_read_conversion (eloc, expr);
14543 unfolded_lhs1 = expr.value;
14544 lhs1 = c_fully_fold (lhs1, false, NULL);
14545 if (lhs1 == error_mark_node)
14546 goto saw_error;
14547 if (!lvalue_p (unfolded_lhs1))
14548 lhs1 = non_lvalue (lhs1);
14550 if (structured_block)
14552 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14553 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14555 done:
14556 if (unfolded_lhs && unfolded_lhs1
14557 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14559 error ("%<#pragma omp atomic capture%> uses two different "
14560 "expressions for memory");
14561 stmt = error_mark_node;
14563 else
14564 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14565 swapped, seq_cst);
14566 if (stmt != error_mark_node)
14567 add_stmt (stmt);
14569 if (!structured_block)
14570 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14574 /* OpenMP 2.5:
14575 # pragma omp barrier new-line
14578 static void
14579 c_parser_omp_barrier (c_parser *parser)
14581 location_t loc = c_parser_peek_token (parser)->location;
14582 c_parser_consume_pragma (parser);
14583 c_parser_skip_to_pragma_eol (parser);
14585 c_finish_omp_barrier (loc);
14588 /* OpenMP 2.5:
14589 # pragma omp critical [(name)] new-line
14590 structured-block
14592 OpenMP 4.5:
14593 # pragma omp critical [(name) [hint(expression)]] new-line
14595 LOC is the location of the #pragma itself. */
14597 #define OMP_CRITICAL_CLAUSE_MASK \
14598 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14600 static tree
14601 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
14603 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14605 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14607 c_parser_consume_token (parser);
14608 if (c_parser_next_token_is (parser, CPP_NAME))
14610 name = c_parser_peek_token (parser)->value;
14611 c_parser_consume_token (parser);
14612 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14614 else
14615 c_parser_error (parser, "expected identifier");
14617 clauses = c_parser_omp_all_clauses (parser,
14618 OMP_CRITICAL_CLAUSE_MASK,
14619 "#pragma omp critical");
14621 else
14623 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14624 c_parser_error (parser, "expected %<(%> or end of line");
14625 c_parser_skip_to_pragma_eol (parser);
14628 stmt = c_parser_omp_structured_block (parser, if_p);
14629 return c_finish_omp_critical (loc, stmt, name, clauses);
14632 /* OpenMP 2.5:
14633 # pragma omp flush flush-vars[opt] new-line
14635 flush-vars:
14636 ( variable-list ) */
14638 static void
14639 c_parser_omp_flush (c_parser *parser)
14641 location_t loc = c_parser_peek_token (parser)->location;
14642 c_parser_consume_pragma (parser);
14643 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14644 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14645 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14646 c_parser_error (parser, "expected %<(%> or end of line");
14647 c_parser_skip_to_pragma_eol (parser);
14649 c_finish_omp_flush (loc);
14652 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14653 The real trick here is to determine the loop control variable early
14654 so that we can push a new decl if necessary to make it private.
14655 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14656 respectively. */
14658 static tree
14659 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14660 tree clauses, tree *cclauses, bool *if_p)
14662 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14663 tree declv, condv, incrv, initv, ret = NULL_TREE;
14664 tree pre_body = NULL_TREE, this_pre_body;
14665 tree ordered_cl = NULL_TREE;
14666 bool fail = false, open_brace_parsed = false;
14667 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14668 location_t for_loc;
14669 vec<tree, va_gc> *for_block = make_tree_vector ();
14671 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14672 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14673 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14674 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14675 && OMP_CLAUSE_ORDERED_EXPR (cl))
14677 ordered_cl = cl;
14678 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14681 if (ordered && ordered < collapse)
14683 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14684 "%<ordered%> clause parameter is less than %<collapse%>");
14685 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14686 = build_int_cst (NULL_TREE, collapse);
14687 ordered = collapse;
14689 if (ordered)
14691 for (tree *pc = &clauses; *pc; )
14692 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14694 error_at (OMP_CLAUSE_LOCATION (*pc),
14695 "%<linear%> clause may not be specified together "
14696 "with %<ordered%> clause with a parameter");
14697 *pc = OMP_CLAUSE_CHAIN (*pc);
14699 else
14700 pc = &OMP_CLAUSE_CHAIN (*pc);
14703 gcc_assert (collapse >= 1 && ordered >= 0);
14704 count = ordered ? ordered : collapse;
14706 declv = make_tree_vec (count);
14707 initv = make_tree_vec (count);
14708 condv = make_tree_vec (count);
14709 incrv = make_tree_vec (count);
14711 if (code != CILK_FOR
14712 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14714 c_parser_error (parser, "for statement expected");
14715 return NULL;
14717 if (code == CILK_FOR
14718 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14720 c_parser_error (parser, "_Cilk_for statement expected");
14721 return NULL;
14723 for_loc = c_parser_peek_token (parser)->location;
14724 c_parser_consume_token (parser);
14726 for (i = 0; i < count; i++)
14728 int bracecount = 0;
14730 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14731 goto pop_scopes;
14733 /* Parse the initialization declaration or expression. */
14734 if (c_parser_next_tokens_start_declaration (parser))
14736 if (i > 0)
14737 vec_safe_push (for_block, c_begin_compound_stmt (true));
14738 this_pre_body = push_stmt_list ();
14739 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14740 NULL, vNULL);
14741 if (this_pre_body)
14743 this_pre_body = pop_stmt_list (this_pre_body);
14744 if (pre_body)
14746 tree t = pre_body;
14747 pre_body = push_stmt_list ();
14748 add_stmt (t);
14749 add_stmt (this_pre_body);
14750 pre_body = pop_stmt_list (pre_body);
14752 else
14753 pre_body = this_pre_body;
14755 decl = check_for_loop_decls (for_loc, flag_isoc99);
14756 if (decl == NULL)
14757 goto error_init;
14758 if (DECL_INITIAL (decl) == error_mark_node)
14759 decl = error_mark_node;
14760 init = decl;
14762 else if (c_parser_next_token_is (parser, CPP_NAME)
14763 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
14765 struct c_expr decl_exp;
14766 struct c_expr init_exp;
14767 location_t init_loc;
14769 decl_exp = c_parser_postfix_expression (parser);
14770 decl = decl_exp.value;
14772 c_parser_require (parser, CPP_EQ, "expected %<=%>");
14774 init_loc = c_parser_peek_token (parser)->location;
14775 init_exp = c_parser_expr_no_commas (parser, NULL);
14776 init_exp = default_function_array_read_conversion (init_loc,
14777 init_exp);
14778 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
14779 NOP_EXPR, init_loc, init_exp.value,
14780 init_exp.original_type);
14781 init = c_process_expr_stmt (init_loc, init);
14783 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14785 else
14787 error_init:
14788 c_parser_error (parser,
14789 "expected iteration declaration or initialization");
14790 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14791 "expected %<)%>");
14792 fail = true;
14793 goto parse_next;
14796 /* Parse the loop condition. */
14797 cond = NULL_TREE;
14798 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
14800 location_t cond_loc = c_parser_peek_token (parser)->location;
14801 struct c_expr cond_expr
14802 = c_parser_binary_expression (parser, NULL, NULL_TREE);
14804 cond = cond_expr.value;
14805 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
14806 cond = c_fully_fold (cond, false, NULL);
14807 switch (cond_expr.original_code)
14809 case GT_EXPR:
14810 case GE_EXPR:
14811 case LT_EXPR:
14812 case LE_EXPR:
14813 break;
14814 case NE_EXPR:
14815 if (code == CILK_SIMD || code == CILK_FOR)
14816 break;
14817 /* FALLTHRU. */
14818 default:
14819 /* Can't be cond = error_mark_node, because we want to preserve
14820 the location until c_finish_omp_for. */
14821 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
14822 break;
14824 protected_set_expr_location (cond, cond_loc);
14826 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14828 /* Parse the increment expression. */
14829 incr = NULL_TREE;
14830 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
14832 location_t incr_loc = c_parser_peek_token (parser)->location;
14834 incr = c_process_expr_stmt (incr_loc,
14835 c_parser_expression (parser).value);
14837 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14839 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
14840 fail = true;
14841 else
14843 TREE_VEC_ELT (declv, i) = decl;
14844 TREE_VEC_ELT (initv, i) = init;
14845 TREE_VEC_ELT (condv, i) = cond;
14846 TREE_VEC_ELT (incrv, i) = incr;
14849 parse_next:
14850 if (i == count - 1)
14851 break;
14853 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
14854 in between the collapsed for loops to be still considered perfectly
14855 nested. Hopefully the final version clarifies this.
14856 For now handle (multiple) {'s and empty statements. */
14859 if (c_parser_next_token_is_keyword (parser, RID_FOR))
14861 c_parser_consume_token (parser);
14862 break;
14864 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14866 c_parser_consume_token (parser);
14867 bracecount++;
14869 else if (bracecount
14870 && c_parser_next_token_is (parser, CPP_SEMICOLON))
14871 c_parser_consume_token (parser);
14872 else
14874 c_parser_error (parser, "not enough perfectly nested loops");
14875 if (bracecount)
14877 open_brace_parsed = true;
14878 bracecount--;
14880 fail = true;
14881 count = 0;
14882 break;
14885 while (1);
14887 nbraces += bracecount;
14890 if (nbraces)
14891 if_p = NULL;
14893 save_break = c_break_label;
14894 if (code == CILK_SIMD)
14895 c_break_label = build_int_cst (size_type_node, 2);
14896 else
14897 c_break_label = size_one_node;
14898 save_cont = c_cont_label;
14899 c_cont_label = NULL_TREE;
14900 body = push_stmt_list ();
14902 if (open_brace_parsed)
14904 location_t here = c_parser_peek_token (parser)->location;
14905 stmt = c_begin_compound_stmt (true);
14906 c_parser_compound_statement_nostart (parser);
14907 add_stmt (c_end_compound_stmt (here, stmt, true));
14909 else
14910 add_stmt (c_parser_c99_block_statement (parser, if_p));
14911 if (c_cont_label)
14913 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
14914 SET_EXPR_LOCATION (t, loc);
14915 add_stmt (t);
14918 body = pop_stmt_list (body);
14919 c_break_label = save_break;
14920 c_cont_label = save_cont;
14922 while (nbraces)
14924 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14926 c_parser_consume_token (parser);
14927 nbraces--;
14929 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
14930 c_parser_consume_token (parser);
14931 else
14933 c_parser_error (parser, "collapsed loops not perfectly nested");
14934 while (nbraces)
14936 location_t here = c_parser_peek_token (parser)->location;
14937 stmt = c_begin_compound_stmt (true);
14938 add_stmt (body);
14939 c_parser_compound_statement_nostart (parser);
14940 body = c_end_compound_stmt (here, stmt, true);
14941 nbraces--;
14943 goto pop_scopes;
14947 /* Only bother calling c_finish_omp_for if we haven't already generated
14948 an error from the initialization parsing. */
14949 if (!fail)
14951 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
14952 incrv, body, pre_body);
14954 /* Check for iterators appearing in lb, b or incr expressions. */
14955 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
14956 stmt = NULL_TREE;
14958 if (stmt)
14960 add_stmt (stmt);
14962 if (cclauses != NULL
14963 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
14965 tree *c;
14966 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
14967 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
14968 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
14969 c = &OMP_CLAUSE_CHAIN (*c);
14970 else
14972 for (i = 0; i < count; i++)
14973 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
14974 break;
14975 if (i == count)
14976 c = &OMP_CLAUSE_CHAIN (*c);
14977 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
14979 error_at (loc,
14980 "iteration variable %qD should not be firstprivate",
14981 OMP_CLAUSE_DECL (*c));
14982 *c = OMP_CLAUSE_CHAIN (*c);
14984 else
14986 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14987 tree l = *c;
14988 *c = OMP_CLAUSE_CHAIN (*c);
14989 if (code == OMP_SIMD)
14991 OMP_CLAUSE_CHAIN (l)
14992 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14993 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
14995 else
14997 OMP_CLAUSE_CHAIN (l) = clauses;
14998 clauses = l;
15003 OMP_FOR_CLAUSES (stmt) = clauses;
15005 ret = stmt;
15007 pop_scopes:
15008 while (!for_block->is_empty ())
15010 /* FIXME diagnostics: LOC below should be the actual location of
15011 this particular for block. We need to build a list of
15012 locations to go along with FOR_BLOCK. */
15013 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15014 add_stmt (stmt);
15016 release_tree_vector (for_block);
15017 return ret;
15020 /* Helper function for OpenMP parsing, split clauses and call
15021 finish_omp_clauses on each of the set of clauses afterwards. */
15023 static void
15024 omp_split_clauses (location_t loc, enum tree_code code,
15025 omp_clause_mask mask, tree clauses, tree *cclauses)
15027 int i;
15028 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15029 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15030 if (cclauses[i])
15031 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15034 /* OpenMP 4.0:
15035 #pragma omp simd simd-clause[optseq] new-line
15036 for-loop
15038 LOC is the location of the #pragma token.
15041 #define OMP_SIMD_CLAUSE_MASK \
15042 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15051 static tree
15052 c_parser_omp_simd (location_t loc, c_parser *parser,
15053 char *p_name, omp_clause_mask mask, tree *cclauses,
15054 bool *if_p)
15056 tree block, clauses, ret;
15058 strcat (p_name, " simd");
15059 mask |= OMP_SIMD_CLAUSE_MASK;
15061 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15062 if (cclauses)
15064 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15065 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15066 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15067 OMP_CLAUSE_ORDERED);
15068 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15070 error_at (OMP_CLAUSE_LOCATION (c),
15071 "%<ordered%> clause with parameter may not be specified "
15072 "on %qs construct", p_name);
15073 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15077 block = c_begin_compound_stmt (true);
15078 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15079 block = c_end_compound_stmt (loc, block, true);
15080 add_stmt (block);
15082 return ret;
15085 /* OpenMP 2.5:
15086 #pragma omp for for-clause[optseq] new-line
15087 for-loop
15089 OpenMP 4.0:
15090 #pragma omp for simd for-simd-clause[optseq] new-line
15091 for-loop
15093 LOC is the location of the #pragma token.
15096 #define OMP_FOR_CLAUSE_MASK \
15097 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15107 static tree
15108 c_parser_omp_for (location_t loc, c_parser *parser,
15109 char *p_name, omp_clause_mask mask, tree *cclauses,
15110 bool *if_p)
15112 tree block, clauses, ret;
15114 strcat (p_name, " for");
15115 mask |= OMP_FOR_CLAUSE_MASK;
15116 /* parallel for{, simd} disallows nowait clause, but for
15117 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15118 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15119 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15120 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15121 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15122 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15124 if (c_parser_next_token_is (parser, CPP_NAME))
15126 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15128 if (strcmp (p, "simd") == 0)
15130 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15131 if (cclauses == NULL)
15132 cclauses = cclauses_buf;
15134 c_parser_consume_token (parser);
15135 if (!flag_openmp) /* flag_openmp_simd */
15136 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15137 if_p);
15138 block = c_begin_compound_stmt (true);
15139 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15140 block = c_end_compound_stmt (loc, block, true);
15141 if (ret == NULL_TREE)
15142 return ret;
15143 ret = make_node (OMP_FOR);
15144 TREE_TYPE (ret) = void_type_node;
15145 OMP_FOR_BODY (ret) = block;
15146 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15147 SET_EXPR_LOCATION (ret, loc);
15148 add_stmt (ret);
15149 return ret;
15152 if (!flag_openmp) /* flag_openmp_simd */
15154 c_parser_skip_to_pragma_eol (parser, false);
15155 return NULL_TREE;
15158 /* Composite distribute parallel for disallows linear clause. */
15159 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15160 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15162 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15163 if (cclauses)
15165 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15166 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15169 block = c_begin_compound_stmt (true);
15170 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15171 block = c_end_compound_stmt (loc, block, true);
15172 add_stmt (block);
15174 return ret;
15177 /* OpenMP 2.5:
15178 # pragma omp master new-line
15179 structured-block
15181 LOC is the location of the #pragma token.
15184 static tree
15185 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15187 c_parser_skip_to_pragma_eol (parser);
15188 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15189 if_p));
15192 /* OpenMP 2.5:
15193 # pragma omp ordered new-line
15194 structured-block
15196 OpenMP 4.5:
15197 # pragma omp ordered ordered-clauses new-line
15198 structured-block
15200 # pragma omp ordered depend-clauses new-line */
15202 #define OMP_ORDERED_CLAUSE_MASK \
15203 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15206 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15207 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15209 static bool
15210 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15211 bool *if_p)
15213 location_t loc = c_parser_peek_token (parser)->location;
15214 c_parser_consume_pragma (parser);
15216 if (context != pragma_stmt && context != pragma_compound)
15218 c_parser_error (parser, "expected declaration specifiers");
15219 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
15220 return false;
15223 if (c_parser_next_token_is (parser, CPP_NAME))
15225 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15227 if (!strcmp ("depend", p))
15229 if (context == pragma_stmt)
15231 error_at (loc,
15232 "%<#pragma omp ordered%> with %<depend> clause may "
15233 "only be used in compound statements");
15234 c_parser_skip_to_pragma_eol (parser, false);
15235 return false;
15238 tree clauses
15239 = c_parser_omp_all_clauses (parser,
15240 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15241 "#pragma omp ordered");
15242 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15243 return false;
15247 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15248 "#pragma omp ordered");
15249 c_finish_omp_ordered (loc, clauses,
15250 c_parser_omp_structured_block (parser, if_p));
15251 return true;
15254 /* OpenMP 2.5:
15256 section-scope:
15257 { section-sequence }
15259 section-sequence:
15260 section-directive[opt] structured-block
15261 section-sequence section-directive structured-block
15263 SECTIONS_LOC is the location of the #pragma omp sections. */
15265 static tree
15266 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15268 tree stmt, substmt;
15269 bool error_suppress = false;
15270 location_t loc;
15272 loc = c_parser_peek_token (parser)->location;
15273 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15275 /* Avoid skipping until the end of the block. */
15276 parser->error = false;
15277 return NULL_TREE;
15280 stmt = push_stmt_list ();
15282 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15284 substmt = c_parser_omp_structured_block (parser, NULL);
15285 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15286 SET_EXPR_LOCATION (substmt, loc);
15287 add_stmt (substmt);
15290 while (1)
15292 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15293 break;
15294 if (c_parser_next_token_is (parser, CPP_EOF))
15295 break;
15297 loc = c_parser_peek_token (parser)->location;
15298 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15300 c_parser_consume_pragma (parser);
15301 c_parser_skip_to_pragma_eol (parser);
15302 error_suppress = false;
15304 else if (!error_suppress)
15306 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15307 error_suppress = true;
15310 substmt = c_parser_omp_structured_block (parser, NULL);
15311 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15312 SET_EXPR_LOCATION (substmt, loc);
15313 add_stmt (substmt);
15315 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15316 "expected %<#pragma omp section%> or %<}%>");
15318 substmt = pop_stmt_list (stmt);
15320 stmt = make_node (OMP_SECTIONS);
15321 SET_EXPR_LOCATION (stmt, sections_loc);
15322 TREE_TYPE (stmt) = void_type_node;
15323 OMP_SECTIONS_BODY (stmt) = substmt;
15325 return add_stmt (stmt);
15328 /* OpenMP 2.5:
15329 # pragma omp sections sections-clause[optseq] newline
15330 sections-scope
15332 LOC is the location of the #pragma token.
15335 #define OMP_SECTIONS_CLAUSE_MASK \
15336 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15342 static tree
15343 c_parser_omp_sections (location_t loc, c_parser *parser,
15344 char *p_name, omp_clause_mask mask, tree *cclauses)
15346 tree block, clauses, ret;
15348 strcat (p_name, " sections");
15349 mask |= OMP_SECTIONS_CLAUSE_MASK;
15350 if (cclauses)
15351 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15353 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15354 if (cclauses)
15356 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15357 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15360 block = c_begin_compound_stmt (true);
15361 ret = c_parser_omp_sections_scope (loc, parser);
15362 if (ret)
15363 OMP_SECTIONS_CLAUSES (ret) = clauses;
15364 block = c_end_compound_stmt (loc, block, true);
15365 add_stmt (block);
15367 return ret;
15370 /* OpenMP 2.5:
15371 # pragma omp parallel parallel-clause[optseq] new-line
15372 structured-block
15373 # pragma omp parallel for parallel-for-clause[optseq] new-line
15374 structured-block
15375 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15376 structured-block
15378 OpenMP 4.0:
15379 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15380 structured-block
15382 LOC is the location of the #pragma token.
15385 #define OMP_PARALLEL_CLAUSE_MASK \
15386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15396 static tree
15397 c_parser_omp_parallel (location_t loc, c_parser *parser,
15398 char *p_name, omp_clause_mask mask, tree *cclauses,
15399 bool *if_p)
15401 tree stmt, clauses, block;
15403 strcat (p_name, " parallel");
15404 mask |= OMP_PARALLEL_CLAUSE_MASK;
15405 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15406 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15407 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15408 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15410 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15412 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15413 if (cclauses == NULL)
15414 cclauses = cclauses_buf;
15416 c_parser_consume_token (parser);
15417 if (!flag_openmp) /* flag_openmp_simd */
15418 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15419 block = c_begin_omp_parallel ();
15420 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15421 stmt
15422 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15423 block);
15424 if (ret == NULL_TREE)
15425 return ret;
15426 OMP_PARALLEL_COMBINED (stmt) = 1;
15427 return stmt;
15429 /* When combined with distribute, parallel has to be followed by for.
15430 #pragma omp target parallel is allowed though. */
15431 else if (cclauses
15432 && (mask & (OMP_CLAUSE_MASK_1
15433 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15435 error_at (loc, "expected %<for%> after %qs", p_name);
15436 c_parser_skip_to_pragma_eol (parser);
15437 return NULL_TREE;
15439 else if (!flag_openmp) /* flag_openmp_simd */
15441 c_parser_skip_to_pragma_eol (parser, false);
15442 return NULL_TREE;
15444 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15446 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15447 if (strcmp (p, "sections") == 0)
15449 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15450 if (cclauses == NULL)
15451 cclauses = cclauses_buf;
15453 c_parser_consume_token (parser);
15454 block = c_begin_omp_parallel ();
15455 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15456 stmt = c_finish_omp_parallel (loc,
15457 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15458 block);
15459 OMP_PARALLEL_COMBINED (stmt) = 1;
15460 return stmt;
15464 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15465 if (cclauses)
15467 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15468 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15471 block = c_begin_omp_parallel ();
15472 c_parser_statement (parser, if_p);
15473 stmt = c_finish_omp_parallel (loc, clauses, block);
15475 return stmt;
15478 /* OpenMP 2.5:
15479 # pragma omp single single-clause[optseq] new-line
15480 structured-block
15482 LOC is the location of the #pragma.
15485 #define OMP_SINGLE_CLAUSE_MASK \
15486 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15491 static tree
15492 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15494 tree stmt = make_node (OMP_SINGLE);
15495 SET_EXPR_LOCATION (stmt, loc);
15496 TREE_TYPE (stmt) = void_type_node;
15498 OMP_SINGLE_CLAUSES (stmt)
15499 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15500 "#pragma omp single");
15501 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15503 return add_stmt (stmt);
15506 /* OpenMP 3.0:
15507 # pragma omp task task-clause[optseq] new-line
15509 LOC is the location of the #pragma.
15512 #define OMP_TASK_CLAUSE_MASK \
15513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15524 static tree
15525 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
15527 tree clauses, block;
15529 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15530 "#pragma omp task");
15532 block = c_begin_omp_task ();
15533 c_parser_statement (parser, if_p);
15534 return c_finish_omp_task (loc, clauses, block);
15537 /* OpenMP 3.0:
15538 # pragma omp taskwait new-line
15541 static void
15542 c_parser_omp_taskwait (c_parser *parser)
15544 location_t loc = c_parser_peek_token (parser)->location;
15545 c_parser_consume_pragma (parser);
15546 c_parser_skip_to_pragma_eol (parser);
15548 c_finish_omp_taskwait (loc);
15551 /* OpenMP 3.1:
15552 # pragma omp taskyield new-line
15555 static void
15556 c_parser_omp_taskyield (c_parser *parser)
15558 location_t loc = c_parser_peek_token (parser)->location;
15559 c_parser_consume_pragma (parser);
15560 c_parser_skip_to_pragma_eol (parser);
15562 c_finish_omp_taskyield (loc);
15565 /* OpenMP 4.0:
15566 # pragma omp taskgroup new-line
15569 static tree
15570 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
15572 location_t loc = c_parser_peek_token (parser)->location;
15573 c_parser_skip_to_pragma_eol (parser);
15574 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
15575 if_p));
15578 /* OpenMP 4.0:
15579 # pragma omp cancel cancel-clause[optseq] new-line
15581 LOC is the location of the #pragma.
15584 #define OMP_CANCEL_CLAUSE_MASK \
15585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15591 static void
15592 c_parser_omp_cancel (c_parser *parser)
15594 location_t loc = c_parser_peek_token (parser)->location;
15596 c_parser_consume_pragma (parser);
15597 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15598 "#pragma omp cancel");
15600 c_finish_omp_cancel (loc, clauses);
15603 /* OpenMP 4.0:
15604 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15606 LOC is the location of the #pragma.
15609 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15610 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15615 static void
15616 c_parser_omp_cancellation_point (c_parser *parser)
15618 location_t loc = c_parser_peek_token (parser)->location;
15619 tree clauses;
15620 bool point_seen = false;
15622 c_parser_consume_pragma (parser);
15623 if (c_parser_next_token_is (parser, CPP_NAME))
15625 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15626 if (strcmp (p, "point") == 0)
15628 c_parser_consume_token (parser);
15629 point_seen = true;
15632 if (!point_seen)
15634 c_parser_error (parser, "expected %<point%>");
15635 c_parser_skip_to_pragma_eol (parser);
15636 return;
15639 clauses
15640 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15641 "#pragma omp cancellation point");
15643 c_finish_omp_cancellation_point (loc, clauses);
15646 /* OpenMP 4.0:
15647 #pragma omp distribute distribute-clause[optseq] new-line
15648 for-loop */
15650 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15651 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15657 static tree
15658 c_parser_omp_distribute (location_t loc, c_parser *parser,
15659 char *p_name, omp_clause_mask mask, tree *cclauses,
15660 bool *if_p)
15662 tree clauses, block, ret;
15664 strcat (p_name, " distribute");
15665 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15667 if (c_parser_next_token_is (parser, CPP_NAME))
15669 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15670 bool simd = false;
15671 bool parallel = false;
15673 if (strcmp (p, "simd") == 0)
15674 simd = true;
15675 else
15676 parallel = strcmp (p, "parallel") == 0;
15677 if (parallel || simd)
15679 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15680 if (cclauses == NULL)
15681 cclauses = cclauses_buf;
15682 c_parser_consume_token (parser);
15683 if (!flag_openmp) /* flag_openmp_simd */
15685 if (simd)
15686 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15687 if_p);
15688 else
15689 return c_parser_omp_parallel (loc, parser, p_name, mask,
15690 cclauses, if_p);
15692 block = c_begin_compound_stmt (true);
15693 if (simd)
15694 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15695 if_p);
15696 else
15697 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
15698 if_p);
15699 block = c_end_compound_stmt (loc, block, true);
15700 if (ret == NULL)
15701 return ret;
15702 ret = make_node (OMP_DISTRIBUTE);
15703 TREE_TYPE (ret) = void_type_node;
15704 OMP_FOR_BODY (ret) = block;
15705 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15706 SET_EXPR_LOCATION (ret, loc);
15707 add_stmt (ret);
15708 return ret;
15711 if (!flag_openmp) /* flag_openmp_simd */
15713 c_parser_skip_to_pragma_eol (parser, false);
15714 return NULL_TREE;
15717 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15718 if (cclauses)
15720 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15721 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15724 block = c_begin_compound_stmt (true);
15725 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
15726 if_p);
15727 block = c_end_compound_stmt (loc, block, true);
15728 add_stmt (block);
15730 return ret;
15733 /* OpenMP 4.0:
15734 # pragma omp teams teams-clause[optseq] new-line
15735 structured-block */
15737 #define OMP_TEAMS_CLAUSE_MASK \
15738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
15746 static tree
15747 c_parser_omp_teams (location_t loc, c_parser *parser,
15748 char *p_name, omp_clause_mask mask, tree *cclauses,
15749 bool *if_p)
15751 tree clauses, block, ret;
15753 strcat (p_name, " teams");
15754 mask |= OMP_TEAMS_CLAUSE_MASK;
15756 if (c_parser_next_token_is (parser, CPP_NAME))
15758 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15759 if (strcmp (p, "distribute") == 0)
15761 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15762 if (cclauses == NULL)
15763 cclauses = cclauses_buf;
15765 c_parser_consume_token (parser);
15766 if (!flag_openmp) /* flag_openmp_simd */
15767 return c_parser_omp_distribute (loc, parser, p_name, mask,
15768 cclauses, if_p);
15769 block = c_begin_compound_stmt (true);
15770 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
15771 if_p);
15772 block = c_end_compound_stmt (loc, block, true);
15773 if (ret == NULL)
15774 return ret;
15775 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15776 ret = make_node (OMP_TEAMS);
15777 TREE_TYPE (ret) = void_type_node;
15778 OMP_TEAMS_CLAUSES (ret) = clauses;
15779 OMP_TEAMS_BODY (ret) = block;
15780 OMP_TEAMS_COMBINED (ret) = 1;
15781 return add_stmt (ret);
15784 if (!flag_openmp) /* flag_openmp_simd */
15786 c_parser_skip_to_pragma_eol (parser, false);
15787 return NULL_TREE;
15790 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15791 if (cclauses)
15793 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
15794 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15797 tree stmt = make_node (OMP_TEAMS);
15798 TREE_TYPE (stmt) = void_type_node;
15799 OMP_TEAMS_CLAUSES (stmt) = clauses;
15800 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15802 return add_stmt (stmt);
15805 /* OpenMP 4.0:
15806 # pragma omp target data target-data-clause[optseq] new-line
15807 structured-block */
15809 #define OMP_TARGET_DATA_CLAUSE_MASK \
15810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
15815 static tree
15816 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
15818 tree clauses
15819 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
15820 "#pragma omp target data");
15821 int map_seen = 0;
15822 for (tree *pc = &clauses; *pc;)
15824 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15825 switch (OMP_CLAUSE_MAP_KIND (*pc))
15827 case GOMP_MAP_TO:
15828 case GOMP_MAP_ALWAYS_TO:
15829 case GOMP_MAP_FROM:
15830 case GOMP_MAP_ALWAYS_FROM:
15831 case GOMP_MAP_TOFROM:
15832 case GOMP_MAP_ALWAYS_TOFROM:
15833 case GOMP_MAP_ALLOC:
15834 map_seen = 3;
15835 break;
15836 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15837 case GOMP_MAP_ALWAYS_POINTER:
15838 break;
15839 default:
15840 map_seen |= 1;
15841 error_at (OMP_CLAUSE_LOCATION (*pc),
15842 "%<#pragma omp target data%> with map-type other "
15843 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
15844 "on %<map%> clause");
15845 *pc = OMP_CLAUSE_CHAIN (*pc);
15846 continue;
15848 pc = &OMP_CLAUSE_CHAIN (*pc);
15851 if (map_seen != 3)
15853 if (map_seen == 0)
15854 error_at (loc,
15855 "%<#pragma omp target data%> must contain at least "
15856 "one %<map%> clause");
15857 return NULL_TREE;
15860 tree stmt = make_node (OMP_TARGET_DATA);
15861 TREE_TYPE (stmt) = void_type_node;
15862 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
15863 keep_next_level ();
15864 tree block = c_begin_compound_stmt (true);
15865 add_stmt (c_parser_omp_structured_block (parser, if_p));
15866 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
15868 SET_EXPR_LOCATION (stmt, loc);
15869 return add_stmt (stmt);
15872 /* OpenMP 4.0:
15873 # pragma omp target update target-update-clause[optseq] new-line */
15875 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
15876 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
15877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
15878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15883 static bool
15884 c_parser_omp_target_update (location_t loc, c_parser *parser,
15885 enum pragma_context context)
15887 if (context == pragma_stmt)
15889 error_at (loc,
15890 "%<#pragma omp target update%> may only be "
15891 "used in compound statements");
15892 c_parser_skip_to_pragma_eol (parser, false);
15893 return false;
15896 tree clauses
15897 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
15898 "#pragma omp target update");
15899 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
15900 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
15902 error_at (loc,
15903 "%<#pragma omp target update%> must contain at least one "
15904 "%<from%> or %<to%> clauses");
15905 return false;
15908 tree stmt = make_node (OMP_TARGET_UPDATE);
15909 TREE_TYPE (stmt) = void_type_node;
15910 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
15911 SET_EXPR_LOCATION (stmt, loc);
15912 add_stmt (stmt);
15913 return false;
15916 /* OpenMP 4.5:
15917 # pragma omp target enter data target-data-clause[optseq] new-line */
15919 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
15920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15926 static tree
15927 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
15928 enum pragma_context context)
15930 bool data_seen = false;
15931 if (c_parser_next_token_is (parser, CPP_NAME))
15933 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15934 if (strcmp (p, "data") == 0)
15936 c_parser_consume_token (parser);
15937 data_seen = true;
15940 if (!data_seen)
15942 c_parser_error (parser, "expected %<data%>");
15943 c_parser_skip_to_pragma_eol (parser);
15944 return NULL_TREE;
15947 if (context == pragma_stmt)
15949 error_at (loc,
15950 "%<#pragma omp target enter data%> may only be "
15951 "used in compound statements");
15952 c_parser_skip_to_pragma_eol (parser, false);
15953 return NULL_TREE;
15956 tree clauses
15957 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
15958 "#pragma omp target enter data");
15959 int map_seen = 0;
15960 for (tree *pc = &clauses; *pc;)
15962 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15963 switch (OMP_CLAUSE_MAP_KIND (*pc))
15965 case GOMP_MAP_TO:
15966 case GOMP_MAP_ALWAYS_TO:
15967 case GOMP_MAP_ALLOC:
15968 map_seen = 3;
15969 break;
15970 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15971 case GOMP_MAP_ALWAYS_POINTER:
15972 break;
15973 default:
15974 map_seen |= 1;
15975 error_at (OMP_CLAUSE_LOCATION (*pc),
15976 "%<#pragma omp target enter data%> with map-type other "
15977 "than %<to%> or %<alloc%> on %<map%> clause");
15978 *pc = OMP_CLAUSE_CHAIN (*pc);
15979 continue;
15981 pc = &OMP_CLAUSE_CHAIN (*pc);
15984 if (map_seen != 3)
15986 if (map_seen == 0)
15987 error_at (loc,
15988 "%<#pragma omp target enter data%> must contain at least "
15989 "one %<map%> clause");
15990 return NULL_TREE;
15993 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
15994 TREE_TYPE (stmt) = void_type_node;
15995 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
15996 SET_EXPR_LOCATION (stmt, loc);
15997 add_stmt (stmt);
15998 return stmt;
16001 /* OpenMP 4.5:
16002 # pragma omp target exit data target-data-clause[optseq] new-line */
16004 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16005 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16011 static tree
16012 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16013 enum pragma_context context)
16015 bool data_seen = false;
16016 if (c_parser_next_token_is (parser, CPP_NAME))
16018 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16019 if (strcmp (p, "data") == 0)
16021 c_parser_consume_token (parser);
16022 data_seen = true;
16025 if (!data_seen)
16027 c_parser_error (parser, "expected %<data%>");
16028 c_parser_skip_to_pragma_eol (parser);
16029 return NULL_TREE;
16032 if (context == pragma_stmt)
16034 error_at (loc,
16035 "%<#pragma omp target exit data%> may only be "
16036 "used in compound statements");
16037 c_parser_skip_to_pragma_eol (parser, false);
16038 return NULL_TREE;
16041 tree clauses
16042 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16043 "#pragma omp target exit data");
16045 int map_seen = 0;
16046 for (tree *pc = &clauses; *pc;)
16048 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16049 switch (OMP_CLAUSE_MAP_KIND (*pc))
16051 case GOMP_MAP_FROM:
16052 case GOMP_MAP_ALWAYS_FROM:
16053 case GOMP_MAP_RELEASE:
16054 case GOMP_MAP_DELETE:
16055 map_seen = 3;
16056 break;
16057 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16058 case GOMP_MAP_ALWAYS_POINTER:
16059 break;
16060 default:
16061 map_seen |= 1;
16062 error_at (OMP_CLAUSE_LOCATION (*pc),
16063 "%<#pragma omp target exit data%> with map-type other "
16064 "than %<from%>, %<release> or %<delete%> on %<map%>"
16065 " clause");
16066 *pc = OMP_CLAUSE_CHAIN (*pc);
16067 continue;
16069 pc = &OMP_CLAUSE_CHAIN (*pc);
16072 if (map_seen != 3)
16074 if (map_seen == 0)
16075 error_at (loc,
16076 "%<#pragma omp target exit data%> must contain at least one "
16077 "%<map%> clause");
16078 return NULL_TREE;
16081 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16082 TREE_TYPE (stmt) = void_type_node;
16083 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16084 SET_EXPR_LOCATION (stmt, loc);
16085 add_stmt (stmt);
16086 return stmt;
16089 /* OpenMP 4.0:
16090 # pragma omp target target-clause[optseq] new-line
16091 structured-block */
16093 #define OMP_TARGET_CLAUSE_MASK \
16094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16104 static bool
16105 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16107 location_t loc = c_parser_peek_token (parser)->location;
16108 c_parser_consume_pragma (parser);
16109 tree *pc = NULL, stmt, block;
16111 if (context != pragma_stmt && context != pragma_compound)
16113 c_parser_error (parser, "expected declaration specifiers");
16114 c_parser_skip_to_pragma_eol (parser);
16115 return false;
16118 if (c_parser_next_token_is (parser, CPP_NAME))
16120 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16121 enum tree_code ccode = ERROR_MARK;
16123 if (strcmp (p, "teams") == 0)
16124 ccode = OMP_TEAMS;
16125 else if (strcmp (p, "parallel") == 0)
16126 ccode = OMP_PARALLEL;
16127 else if (strcmp (p, "simd") == 0)
16128 ccode = OMP_SIMD;
16129 if (ccode != ERROR_MARK)
16131 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16132 char p_name[sizeof ("#pragma omp target teams distribute "
16133 "parallel for simd")];
16135 c_parser_consume_token (parser);
16136 strcpy (p_name, "#pragma omp target");
16137 if (!flag_openmp) /* flag_openmp_simd */
16139 tree stmt;
16140 switch (ccode)
16142 case OMP_TEAMS:
16143 stmt = c_parser_omp_teams (loc, parser, p_name,
16144 OMP_TARGET_CLAUSE_MASK,
16145 cclauses, if_p);
16146 break;
16147 case OMP_PARALLEL:
16148 stmt = c_parser_omp_parallel (loc, parser, p_name,
16149 OMP_TARGET_CLAUSE_MASK,
16150 cclauses, if_p);
16151 break;
16152 case OMP_SIMD:
16153 stmt = c_parser_omp_simd (loc, parser, p_name,
16154 OMP_TARGET_CLAUSE_MASK,
16155 cclauses, if_p);
16156 break;
16157 default:
16158 gcc_unreachable ();
16160 return stmt != NULL_TREE;
16162 keep_next_level ();
16163 tree block = c_begin_compound_stmt (true), ret;
16164 switch (ccode)
16166 case OMP_TEAMS:
16167 ret = c_parser_omp_teams (loc, parser, p_name,
16168 OMP_TARGET_CLAUSE_MASK, cclauses,
16169 if_p);
16170 break;
16171 case OMP_PARALLEL:
16172 ret = c_parser_omp_parallel (loc, parser, p_name,
16173 OMP_TARGET_CLAUSE_MASK, cclauses,
16174 if_p);
16175 break;
16176 case OMP_SIMD:
16177 ret = c_parser_omp_simd (loc, parser, p_name,
16178 OMP_TARGET_CLAUSE_MASK, cclauses,
16179 if_p);
16180 break;
16181 default:
16182 gcc_unreachable ();
16184 block = c_end_compound_stmt (loc, block, true);
16185 if (ret == NULL_TREE)
16186 return false;
16187 if (ccode == OMP_TEAMS)
16189 /* For combined target teams, ensure the num_teams and
16190 thread_limit clause expressions are evaluated on the host,
16191 before entering the target construct. */
16192 tree c;
16193 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16194 c; c = OMP_CLAUSE_CHAIN (c))
16195 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16196 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16197 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16199 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16200 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16201 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16202 expr, NULL_TREE, NULL_TREE);
16203 add_stmt (expr);
16204 OMP_CLAUSE_OPERAND (c, 0) = expr;
16205 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16206 OMP_CLAUSE_FIRSTPRIVATE);
16207 OMP_CLAUSE_DECL (tc) = tmp;
16208 OMP_CLAUSE_CHAIN (tc)
16209 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16210 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16213 tree stmt = make_node (OMP_TARGET);
16214 TREE_TYPE (stmt) = void_type_node;
16215 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16216 OMP_TARGET_BODY (stmt) = block;
16217 OMP_TARGET_COMBINED (stmt) = 1;
16218 add_stmt (stmt);
16219 pc = &OMP_TARGET_CLAUSES (stmt);
16220 goto check_clauses;
16222 else if (!flag_openmp) /* flag_openmp_simd */
16224 c_parser_skip_to_pragma_eol (parser, false);
16225 return false;
16227 else if (strcmp (p, "data") == 0)
16229 c_parser_consume_token (parser);
16230 c_parser_omp_target_data (loc, parser, if_p);
16231 return true;
16233 else if (strcmp (p, "enter") == 0)
16235 c_parser_consume_token (parser);
16236 c_parser_omp_target_enter_data (loc, parser, context);
16237 return false;
16239 else if (strcmp (p, "exit") == 0)
16241 c_parser_consume_token (parser);
16242 c_parser_omp_target_exit_data (loc, parser, context);
16243 return false;
16245 else if (strcmp (p, "update") == 0)
16247 c_parser_consume_token (parser);
16248 return c_parser_omp_target_update (loc, parser, context);
16252 stmt = make_node (OMP_TARGET);
16253 TREE_TYPE (stmt) = void_type_node;
16255 OMP_TARGET_CLAUSES (stmt)
16256 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16257 "#pragma omp target");
16258 pc = &OMP_TARGET_CLAUSES (stmt);
16259 keep_next_level ();
16260 block = c_begin_compound_stmt (true);
16261 add_stmt (c_parser_omp_structured_block (parser, if_p));
16262 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16264 SET_EXPR_LOCATION (stmt, loc);
16265 add_stmt (stmt);
16267 check_clauses:
16268 while (*pc)
16270 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16271 switch (OMP_CLAUSE_MAP_KIND (*pc))
16273 case GOMP_MAP_TO:
16274 case GOMP_MAP_ALWAYS_TO:
16275 case GOMP_MAP_FROM:
16276 case GOMP_MAP_ALWAYS_FROM:
16277 case GOMP_MAP_TOFROM:
16278 case GOMP_MAP_ALWAYS_TOFROM:
16279 case GOMP_MAP_ALLOC:
16280 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16281 case GOMP_MAP_ALWAYS_POINTER:
16282 break;
16283 default:
16284 error_at (OMP_CLAUSE_LOCATION (*pc),
16285 "%<#pragma omp target%> with map-type other "
16286 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16287 "on %<map%> clause");
16288 *pc = OMP_CLAUSE_CHAIN (*pc);
16289 continue;
16291 pc = &OMP_CLAUSE_CHAIN (*pc);
16293 return true;
16296 /* OpenMP 4.0:
16297 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16299 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16300 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16307 static void
16308 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16310 vec<c_token> clauses = vNULL;
16311 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16313 c_token *token = c_parser_peek_token (parser);
16314 if (token->type == CPP_EOF)
16316 c_parser_skip_to_pragma_eol (parser);
16317 clauses.release ();
16318 return;
16320 clauses.safe_push (*token);
16321 c_parser_consume_token (parser);
16323 clauses.safe_push (*c_parser_peek_token (parser));
16324 c_parser_skip_to_pragma_eol (parser);
16326 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16328 if (c_parser_peek_token (parser)->pragma_kind
16329 != PRAGMA_OMP_DECLARE_REDUCTION
16330 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16331 || strcmp (IDENTIFIER_POINTER
16332 (c_parser_peek_2nd_token (parser)->value),
16333 "simd") != 0)
16335 c_parser_error (parser,
16336 "%<#pragma omp declare simd%> must be followed by "
16337 "function declaration or definition or another "
16338 "%<#pragma omp declare simd%>");
16339 clauses.release ();
16340 return;
16342 c_parser_consume_pragma (parser);
16343 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16345 c_token *token = c_parser_peek_token (parser);
16346 if (token->type == CPP_EOF)
16348 c_parser_skip_to_pragma_eol (parser);
16349 clauses.release ();
16350 return;
16352 clauses.safe_push (*token);
16353 c_parser_consume_token (parser);
16355 clauses.safe_push (*c_parser_peek_token (parser));
16356 c_parser_skip_to_pragma_eol (parser);
16359 /* Make sure nothing tries to read past the end of the tokens. */
16360 c_token eof_token;
16361 memset (&eof_token, 0, sizeof (eof_token));
16362 eof_token.type = CPP_EOF;
16363 clauses.safe_push (eof_token);
16364 clauses.safe_push (eof_token);
16366 switch (context)
16368 case pragma_external:
16369 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16370 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16372 int ext = disable_extension_diagnostics ();
16374 c_parser_consume_token (parser);
16375 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16376 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16377 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16378 NULL, clauses);
16379 restore_extension_diagnostics (ext);
16381 else
16382 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16383 NULL, clauses);
16384 break;
16385 case pragma_struct:
16386 case pragma_param:
16387 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16388 "function declaration or definition");
16389 break;
16390 case pragma_compound:
16391 case pragma_stmt:
16392 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16393 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16395 int ext = disable_extension_diagnostics ();
16397 c_parser_consume_token (parser);
16398 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16399 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16400 if (c_parser_next_tokens_start_declaration (parser))
16402 c_parser_declaration_or_fndef (parser, true, true, true, true,
16403 true, NULL, clauses);
16404 restore_extension_diagnostics (ext);
16405 break;
16407 restore_extension_diagnostics (ext);
16409 else if (c_parser_next_tokens_start_declaration (parser))
16411 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16412 NULL, clauses);
16413 break;
16415 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16416 "function declaration or definition");
16417 break;
16418 default:
16419 gcc_unreachable ();
16421 clauses.release ();
16424 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16425 and put that into "omp declare simd" attribute. */
16427 static void
16428 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16429 vec<c_token> clauses)
16431 if (flag_cilkplus
16432 && (clauses.exists ()
16433 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16434 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16436 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16437 "used in the same function marked as a Cilk Plus SIMD-enabled "
16438 "function");
16439 vec_free (parser->cilk_simd_fn_tokens);
16440 return;
16443 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16444 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16445 has already processed the tokens. */
16446 if (clauses.exists () && clauses[0].type == CPP_EOF)
16447 return;
16448 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16450 error ("%<#pragma omp declare simd%> not immediately followed by "
16451 "a function declaration or definition");
16452 clauses[0].type = CPP_EOF;
16453 return;
16455 if (clauses.exists () && clauses[0].type != CPP_NAME)
16457 error_at (DECL_SOURCE_LOCATION (fndecl),
16458 "%<#pragma omp declare simd%> not immediately followed by "
16459 "a single function declaration or definition");
16460 clauses[0].type = CPP_EOF;
16461 return;
16464 if (parms == NULL_TREE)
16465 parms = DECL_ARGUMENTS (fndecl);
16467 unsigned int tokens_avail = parser->tokens_avail;
16468 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16469 bool is_cilkplus_cilk_simd_fn = false;
16471 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16473 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16474 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16475 is_cilkplus_cilk_simd_fn = true;
16477 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16479 error_at (DECL_SOURCE_LOCATION (fndecl),
16480 "%<__simd__%> attribute cannot be used in the same "
16481 "function marked as a Cilk Plus SIMD-enabled function");
16482 vec_free (parser->cilk_simd_fn_tokens);
16483 return;
16487 else
16489 parser->tokens = clauses.address ();
16490 parser->tokens_avail = clauses.length ();
16493 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16494 while (parser->tokens_avail > 3)
16496 c_token *token = c_parser_peek_token (parser);
16497 if (!is_cilkplus_cilk_simd_fn)
16498 gcc_assert (token->type == CPP_NAME
16499 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16500 else
16501 gcc_assert (token->type == CPP_NAME
16502 && is_cilkplus_vector_p (token->value));
16503 c_parser_consume_token (parser);
16504 parser->in_pragma = true;
16506 tree c = NULL_TREE;
16507 if (is_cilkplus_cilk_simd_fn)
16508 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16509 "SIMD-enabled functions attribute");
16510 else
16511 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16512 "#pragma omp declare simd");
16513 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16514 if (c != NULL_TREE)
16515 c = tree_cons (NULL_TREE, c, NULL_TREE);
16516 if (is_cilkplus_cilk_simd_fn)
16518 tree k = build_tree_list (get_identifier ("cilk simd function"),
16519 NULL_TREE);
16520 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16521 DECL_ATTRIBUTES (fndecl) = k;
16523 c = build_tree_list (get_identifier ("omp declare simd"), c);
16524 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16525 DECL_ATTRIBUTES (fndecl) = c;
16528 parser->tokens = &parser->tokens_buf[0];
16529 parser->tokens_avail = tokens_avail;
16530 if (clauses.exists ())
16531 clauses[0].type = CPP_PRAGMA;
16533 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16534 vec_free (parser->cilk_simd_fn_tokens);
16538 /* OpenMP 4.0:
16539 # pragma omp declare target new-line
16540 declarations and definitions
16541 # pragma omp end declare target new-line
16543 OpenMP 4.5:
16544 # pragma omp declare target ( extended-list ) new-line
16546 # pragma omp declare target declare-target-clauses[seq] new-line */
16548 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16549 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16552 static void
16553 c_parser_omp_declare_target (c_parser *parser)
16555 location_t loc = c_parser_peek_token (parser)->location;
16556 tree clauses = NULL_TREE;
16557 if (c_parser_next_token_is (parser, CPP_NAME))
16558 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16559 "#pragma omp declare target");
16560 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16562 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16563 clauses);
16564 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
16565 c_parser_skip_to_pragma_eol (parser);
16567 else
16569 c_parser_skip_to_pragma_eol (parser);
16570 current_omp_declare_target_attribute++;
16571 return;
16573 if (current_omp_declare_target_attribute)
16574 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16575 "%<#pragma omp declare target%> without clauses and "
16576 "%<#pragma omp end declare target%>");
16577 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16579 tree t = OMP_CLAUSE_DECL (c), id;
16580 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16581 tree at2 = lookup_attribute ("omp declare target link",
16582 DECL_ATTRIBUTES (t));
16583 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16585 id = get_identifier ("omp declare target link");
16586 std::swap (at1, at2);
16588 else
16589 id = get_identifier ("omp declare target");
16590 if (at2)
16592 error_at (OMP_CLAUSE_LOCATION (c),
16593 "%qD specified both in declare target %<link%> and %<to%>"
16594 " clauses", t);
16595 continue;
16597 if (!at1)
16599 symtab_node *node = symtab_node::get (t);
16600 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16601 if (node != NULL)
16603 node->offloadable = 1;
16604 if (ENABLE_OFFLOADING)
16606 g->have_offload = true;
16607 if (is_a <varpool_node *> (node))
16608 vec_safe_push (offload_vars, t);
16615 static void
16616 c_parser_omp_end_declare_target (c_parser *parser)
16618 location_t loc = c_parser_peek_token (parser)->location;
16619 c_parser_consume_pragma (parser);
16620 if (c_parser_next_token_is (parser, CPP_NAME)
16621 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16622 "declare") == 0)
16624 c_parser_consume_token (parser);
16625 if (c_parser_next_token_is (parser, CPP_NAME)
16626 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16627 "target") == 0)
16628 c_parser_consume_token (parser);
16629 else
16631 c_parser_error (parser, "expected %<target%>");
16632 c_parser_skip_to_pragma_eol (parser);
16633 return;
16636 else
16638 c_parser_error (parser, "expected %<declare%>");
16639 c_parser_skip_to_pragma_eol (parser);
16640 return;
16642 c_parser_skip_to_pragma_eol (parser);
16643 if (!current_omp_declare_target_attribute)
16644 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16645 "%<#pragma omp declare target%>");
16646 else
16647 current_omp_declare_target_attribute--;
16651 /* OpenMP 4.0
16652 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16653 initializer-clause[opt] new-line
16655 initializer-clause:
16656 initializer (omp_priv = initializer)
16657 initializer (function-name (argument-list)) */
16659 static void
16660 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16662 unsigned int tokens_avail = 0, i;
16663 vec<tree> types = vNULL;
16664 vec<c_token> clauses = vNULL;
16665 enum tree_code reduc_code = ERROR_MARK;
16666 tree reduc_id = NULL_TREE;
16667 tree type;
16668 location_t rloc = c_parser_peek_token (parser)->location;
16670 if (context == pragma_struct || context == pragma_param)
16672 error ("%<#pragma omp declare reduction%> not at file or block scope");
16673 goto fail;
16676 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16677 goto fail;
16679 switch (c_parser_peek_token (parser)->type)
16681 case CPP_PLUS:
16682 reduc_code = PLUS_EXPR;
16683 break;
16684 case CPP_MULT:
16685 reduc_code = MULT_EXPR;
16686 break;
16687 case CPP_MINUS:
16688 reduc_code = MINUS_EXPR;
16689 break;
16690 case CPP_AND:
16691 reduc_code = BIT_AND_EXPR;
16692 break;
16693 case CPP_XOR:
16694 reduc_code = BIT_XOR_EXPR;
16695 break;
16696 case CPP_OR:
16697 reduc_code = BIT_IOR_EXPR;
16698 break;
16699 case CPP_AND_AND:
16700 reduc_code = TRUTH_ANDIF_EXPR;
16701 break;
16702 case CPP_OR_OR:
16703 reduc_code = TRUTH_ORIF_EXPR;
16704 break;
16705 case CPP_NAME:
16706 const char *p;
16707 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16708 if (strcmp (p, "min") == 0)
16710 reduc_code = MIN_EXPR;
16711 break;
16713 if (strcmp (p, "max") == 0)
16715 reduc_code = MAX_EXPR;
16716 break;
16718 reduc_id = c_parser_peek_token (parser)->value;
16719 break;
16720 default:
16721 c_parser_error (parser,
16722 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16723 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16724 goto fail;
16727 tree orig_reduc_id, reduc_decl;
16728 orig_reduc_id = reduc_id;
16729 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16730 reduc_decl = c_omp_reduction_decl (reduc_id);
16731 c_parser_consume_token (parser);
16733 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16734 goto fail;
16736 while (true)
16738 location_t loc = c_parser_peek_token (parser)->location;
16739 struct c_type_name *ctype = c_parser_type_name (parser);
16740 if (ctype != NULL)
16742 type = groktypename (ctype, NULL, NULL);
16743 if (type == error_mark_node)
16745 else if ((INTEGRAL_TYPE_P (type)
16746 || TREE_CODE (type) == REAL_TYPE
16747 || TREE_CODE (type) == COMPLEX_TYPE)
16748 && orig_reduc_id == NULL_TREE)
16749 error_at (loc, "predeclared arithmetic type in "
16750 "%<#pragma omp declare reduction%>");
16751 else if (TREE_CODE (type) == FUNCTION_TYPE
16752 || TREE_CODE (type) == ARRAY_TYPE)
16753 error_at (loc, "function or array type in "
16754 "%<#pragma omp declare reduction%>");
16755 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
16756 error_at (loc, "const, volatile or restrict qualified type in "
16757 "%<#pragma omp declare reduction%>");
16758 else
16760 tree t;
16761 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
16762 if (comptypes (TREE_PURPOSE (t), type))
16764 error_at (loc, "redeclaration of %qs "
16765 "%<#pragma omp declare reduction%> for "
16766 "type %qT",
16767 IDENTIFIER_POINTER (reduc_id)
16768 + sizeof ("omp declare reduction ") - 1,
16769 type);
16770 location_t ploc
16771 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
16772 0));
16773 error_at (ploc, "previous %<#pragma omp declare "
16774 "reduction%>");
16775 break;
16777 if (t == NULL_TREE)
16778 types.safe_push (type);
16780 if (c_parser_next_token_is (parser, CPP_COMMA))
16781 c_parser_consume_token (parser);
16782 else
16783 break;
16785 else
16786 break;
16789 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
16790 || types.is_empty ())
16792 fail:
16793 clauses.release ();
16794 types.release ();
16795 while (true)
16797 c_token *token = c_parser_peek_token (parser);
16798 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
16799 break;
16800 c_parser_consume_token (parser);
16802 c_parser_skip_to_pragma_eol (parser);
16803 return;
16806 if (types.length () > 1)
16808 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16810 c_token *token = c_parser_peek_token (parser);
16811 if (token->type == CPP_EOF)
16812 goto fail;
16813 clauses.safe_push (*token);
16814 c_parser_consume_token (parser);
16816 clauses.safe_push (*c_parser_peek_token (parser));
16817 c_parser_skip_to_pragma_eol (parser);
16819 /* Make sure nothing tries to read past the end of the tokens. */
16820 c_token eof_token;
16821 memset (&eof_token, 0, sizeof (eof_token));
16822 eof_token.type = CPP_EOF;
16823 clauses.safe_push (eof_token);
16824 clauses.safe_push (eof_token);
16827 int errs = errorcount;
16828 FOR_EACH_VEC_ELT (types, i, type)
16830 tokens_avail = parser->tokens_avail;
16831 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16832 if (!clauses.is_empty ())
16834 parser->tokens = clauses.address ();
16835 parser->tokens_avail = clauses.length ();
16836 parser->in_pragma = true;
16839 bool nested = current_function_decl != NULL_TREE;
16840 if (nested)
16841 c_push_function_context ();
16842 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
16843 reduc_id, default_function_type);
16844 current_function_decl = fndecl;
16845 allocate_struct_function (fndecl, true);
16846 push_scope ();
16847 tree stmt = push_stmt_list ();
16848 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
16849 warn about these. */
16850 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
16851 get_identifier ("omp_out"), type);
16852 DECL_ARTIFICIAL (omp_out) = 1;
16853 DECL_CONTEXT (omp_out) = fndecl;
16854 pushdecl (omp_out);
16855 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
16856 get_identifier ("omp_in"), type);
16857 DECL_ARTIFICIAL (omp_in) = 1;
16858 DECL_CONTEXT (omp_in) = fndecl;
16859 pushdecl (omp_in);
16860 struct c_expr combiner = c_parser_expression (parser);
16861 struct c_expr initializer;
16862 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
16863 bool bad = false;
16864 initializer.value = error_mark_node;
16865 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16866 bad = true;
16867 else if (c_parser_next_token_is (parser, CPP_NAME)
16868 && strcmp (IDENTIFIER_POINTER
16869 (c_parser_peek_token (parser)->value),
16870 "initializer") == 0)
16872 c_parser_consume_token (parser);
16873 pop_scope ();
16874 push_scope ();
16875 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
16876 get_identifier ("omp_priv"), type);
16877 DECL_ARTIFICIAL (omp_priv) = 1;
16878 DECL_INITIAL (omp_priv) = error_mark_node;
16879 DECL_CONTEXT (omp_priv) = fndecl;
16880 pushdecl (omp_priv);
16881 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
16882 get_identifier ("omp_orig"), type);
16883 DECL_ARTIFICIAL (omp_orig) = 1;
16884 DECL_CONTEXT (omp_orig) = fndecl;
16885 pushdecl (omp_orig);
16886 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16887 bad = true;
16888 else if (!c_parser_next_token_is (parser, CPP_NAME))
16890 c_parser_error (parser, "expected %<omp_priv%> or "
16891 "function-name");
16892 bad = true;
16894 else if (strcmp (IDENTIFIER_POINTER
16895 (c_parser_peek_token (parser)->value),
16896 "omp_priv") != 0)
16898 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
16899 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
16901 c_parser_error (parser, "expected function-name %<(%>");
16902 bad = true;
16904 else
16905 initializer = c_parser_postfix_expression (parser);
16906 if (initializer.value
16907 && TREE_CODE (initializer.value) == CALL_EXPR)
16909 int j;
16910 tree c = initializer.value;
16911 for (j = 0; j < call_expr_nargs (c); j++)
16913 tree a = CALL_EXPR_ARG (c, j);
16914 STRIP_NOPS (a);
16915 if (TREE_CODE (a) == ADDR_EXPR
16916 && TREE_OPERAND (a, 0) == omp_priv)
16917 break;
16919 if (j == call_expr_nargs (c))
16920 error ("one of the initializer call arguments should be "
16921 "%<&omp_priv%>");
16924 else
16926 c_parser_consume_token (parser);
16927 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16928 bad = true;
16929 else
16931 tree st = push_stmt_list ();
16932 start_init (omp_priv, NULL_TREE, 0);
16933 location_t loc = c_parser_peek_token (parser)->location;
16934 struct c_expr init = c_parser_initializer (parser);
16935 finish_init ();
16936 finish_decl (omp_priv, loc, init.value,
16937 init.original_type, NULL_TREE);
16938 pop_stmt_list (st);
16941 if (!bad
16942 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16943 bad = true;
16946 if (!bad)
16948 c_parser_skip_to_pragma_eol (parser);
16950 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
16951 DECL_INITIAL (reduc_decl));
16952 DECL_INITIAL (reduc_decl) = t;
16953 DECL_SOURCE_LOCATION (omp_out) = rloc;
16954 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
16955 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
16956 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
16957 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
16958 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
16959 if (omp_priv)
16961 DECL_SOURCE_LOCATION (omp_priv) = rloc;
16962 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
16963 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
16964 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
16965 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
16966 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16967 walk_tree (&DECL_INITIAL (omp_priv),
16968 c_check_omp_declare_reduction_r,
16969 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16973 pop_stmt_list (stmt);
16974 pop_scope ();
16975 if (cfun->language != NULL)
16977 ggc_free (cfun->language);
16978 cfun->language = NULL;
16980 set_cfun (NULL);
16981 current_function_decl = NULL_TREE;
16982 if (nested)
16983 c_pop_function_context ();
16985 if (!clauses.is_empty ())
16987 parser->tokens = &parser->tokens_buf[0];
16988 parser->tokens_avail = tokens_avail;
16990 if (bad)
16991 goto fail;
16992 if (errs != errorcount)
16993 break;
16996 clauses.release ();
16997 types.release ();
17001 /* OpenMP 4.0
17002 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17003 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17004 initializer-clause[opt] new-line
17005 #pragma omp declare target new-line */
17007 static void
17008 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17010 c_parser_consume_pragma (parser);
17011 if (c_parser_next_token_is (parser, CPP_NAME))
17013 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17014 if (strcmp (p, "simd") == 0)
17016 /* c_parser_consume_token (parser); done in
17017 c_parser_omp_declare_simd. */
17018 c_parser_omp_declare_simd (parser, context);
17019 return;
17021 if (strcmp (p, "reduction") == 0)
17023 c_parser_consume_token (parser);
17024 c_parser_omp_declare_reduction (parser, context);
17025 return;
17027 if (!flag_openmp) /* flag_openmp_simd */
17029 c_parser_skip_to_pragma_eol (parser, false);
17030 return;
17032 if (strcmp (p, "target") == 0)
17034 c_parser_consume_token (parser);
17035 c_parser_omp_declare_target (parser);
17036 return;
17040 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17041 "or %<target%>");
17042 c_parser_skip_to_pragma_eol (parser);
17045 /* OpenMP 4.5:
17046 #pragma omp taskloop taskloop-clause[optseq] new-line
17047 for-loop
17049 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17050 for-loop */
17052 #define OMP_TASKLOOP_CLAUSE_MASK \
17053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17068 static tree
17069 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17070 char *p_name, omp_clause_mask mask, tree *cclauses,
17071 bool *if_p)
17073 tree clauses, block, ret;
17075 strcat (p_name, " taskloop");
17076 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17078 if (c_parser_next_token_is (parser, CPP_NAME))
17080 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17082 if (strcmp (p, "simd") == 0)
17084 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17085 if (cclauses == NULL)
17086 cclauses = cclauses_buf;
17087 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17088 c_parser_consume_token (parser);
17089 if (!flag_openmp) /* flag_openmp_simd */
17090 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17091 if_p);
17092 block = c_begin_compound_stmt (true);
17093 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17094 block = c_end_compound_stmt (loc, block, true);
17095 if (ret == NULL)
17096 return ret;
17097 ret = make_node (OMP_TASKLOOP);
17098 TREE_TYPE (ret) = void_type_node;
17099 OMP_FOR_BODY (ret) = block;
17100 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17101 SET_EXPR_LOCATION (ret, loc);
17102 add_stmt (ret);
17103 return ret;
17106 if (!flag_openmp) /* flag_openmp_simd */
17108 c_parser_skip_to_pragma_eol (parser, false);
17109 return NULL_TREE;
17112 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17113 if (cclauses)
17115 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17116 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17119 block = c_begin_compound_stmt (true);
17120 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17121 block = c_end_compound_stmt (loc, block, true);
17122 add_stmt (block);
17124 return ret;
17127 /* Main entry point to parsing most OpenMP pragmas. */
17129 static void
17130 c_parser_omp_construct (c_parser *parser, bool *if_p)
17132 enum pragma_kind p_kind;
17133 location_t loc;
17134 tree stmt;
17135 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17136 omp_clause_mask mask (0);
17138 loc = c_parser_peek_token (parser)->location;
17139 p_kind = c_parser_peek_token (parser)->pragma_kind;
17140 c_parser_consume_pragma (parser);
17142 switch (p_kind)
17144 case PRAGMA_OACC_ATOMIC:
17145 c_parser_omp_atomic (loc, parser);
17146 return;
17147 case PRAGMA_OACC_CACHE:
17148 strcpy (p_name, "#pragma acc");
17149 stmt = c_parser_oacc_cache (loc, parser);
17150 break;
17151 case PRAGMA_OACC_DATA:
17152 stmt = c_parser_oacc_data (loc, parser, if_p);
17153 break;
17154 case PRAGMA_OACC_HOST_DATA:
17155 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17156 break;
17157 case PRAGMA_OACC_KERNELS:
17158 case PRAGMA_OACC_PARALLEL:
17159 strcpy (p_name, "#pragma acc");
17160 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17161 if_p);
17162 break;
17163 case PRAGMA_OACC_LOOP:
17164 strcpy (p_name, "#pragma acc");
17165 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17166 break;
17167 case PRAGMA_OACC_WAIT:
17168 strcpy (p_name, "#pragma wait");
17169 stmt = c_parser_oacc_wait (loc, parser, p_name);
17170 break;
17171 case PRAGMA_OMP_ATOMIC:
17172 c_parser_omp_atomic (loc, parser);
17173 return;
17174 case PRAGMA_OMP_CRITICAL:
17175 stmt = c_parser_omp_critical (loc, parser, if_p);
17176 break;
17177 case PRAGMA_OMP_DISTRIBUTE:
17178 strcpy (p_name, "#pragma omp");
17179 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17180 break;
17181 case PRAGMA_OMP_FOR:
17182 strcpy (p_name, "#pragma omp");
17183 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17184 break;
17185 case PRAGMA_OMP_MASTER:
17186 stmt = c_parser_omp_master (loc, parser, if_p);
17187 break;
17188 case PRAGMA_OMP_PARALLEL:
17189 strcpy (p_name, "#pragma omp");
17190 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17191 break;
17192 case PRAGMA_OMP_SECTIONS:
17193 strcpy (p_name, "#pragma omp");
17194 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17195 break;
17196 case PRAGMA_OMP_SIMD:
17197 strcpy (p_name, "#pragma omp");
17198 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17199 break;
17200 case PRAGMA_OMP_SINGLE:
17201 stmt = c_parser_omp_single (loc, parser, if_p);
17202 break;
17203 case PRAGMA_OMP_TASK:
17204 stmt = c_parser_omp_task (loc, parser, if_p);
17205 break;
17206 case PRAGMA_OMP_TASKGROUP:
17207 stmt = c_parser_omp_taskgroup (parser, if_p);
17208 break;
17209 case PRAGMA_OMP_TASKLOOP:
17210 strcpy (p_name, "#pragma omp");
17211 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17212 break;
17213 case PRAGMA_OMP_TEAMS:
17214 strcpy (p_name, "#pragma omp");
17215 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17216 break;
17217 default:
17218 gcc_unreachable ();
17221 if (stmt)
17222 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17226 /* OpenMP 2.5:
17227 # pragma omp threadprivate (variable-list) */
17229 static void
17230 c_parser_omp_threadprivate (c_parser *parser)
17232 tree vars, t;
17233 location_t loc;
17235 c_parser_consume_pragma (parser);
17236 loc = c_parser_peek_token (parser)->location;
17237 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17239 /* Mark every variable in VARS to be assigned thread local storage. */
17240 for (t = vars; t; t = TREE_CHAIN (t))
17242 tree v = TREE_PURPOSE (t);
17244 /* FIXME diagnostics: Ideally we should keep individual
17245 locations for all the variables in the var list to make the
17246 following errors more precise. Perhaps
17247 c_parser_omp_var_list_parens() should construct a list of
17248 locations to go along with the var list. */
17250 /* If V had already been marked threadprivate, it doesn't matter
17251 whether it had been used prior to this point. */
17252 if (!VAR_P (v))
17253 error_at (loc, "%qD is not a variable", v);
17254 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17255 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17256 else if (! is_global_var (v))
17257 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17258 else if (TREE_TYPE (v) == error_mark_node)
17260 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17261 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17262 else
17264 if (! DECL_THREAD_LOCAL_P (v))
17266 set_decl_tls_model (v, decl_default_tls_model (v));
17267 /* If rtl has been already set for this var, call
17268 make_decl_rtl once again, so that encode_section_info
17269 has a chance to look at the new decl flags. */
17270 if (DECL_RTL_SET_P (v))
17271 make_decl_rtl (v);
17273 C_DECL_THREADPRIVATE_P (v) = 1;
17277 c_parser_skip_to_pragma_eol (parser);
17280 /* Cilk Plus <#pragma simd> parsing routines. */
17282 /* Helper function for c_parser_pragma. Perform some sanity checking
17283 for <#pragma simd> constructs. Returns FALSE if there was a
17284 problem. */
17286 static bool
17287 c_parser_cilk_verify_simd (c_parser *parser,
17288 enum pragma_context context)
17290 if (!flag_cilkplus)
17292 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17293 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17294 return false;
17296 if (context == pragma_external)
17298 c_parser_error (parser,"pragma simd must be inside a function");
17299 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17300 return false;
17302 return true;
17305 /* Cilk Plus:
17306 This function is shared by SIMD-enabled functions and #pragma simd.
17307 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17308 CLAUSES is unused. The main purpose of this function is to parse a
17309 vectorlength attribute or clause and check for parse errors.
17310 When IS_SIMD_FN is true then the function is merely caching the tokens
17311 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17312 cache is cleared since there is no reason to continue.
17313 Syntax:
17314 vectorlength ( constant-expression ) */
17316 static tree
17317 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17318 bool is_simd_fn)
17320 if (is_simd_fn)
17321 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17322 else
17323 /* The vectorlength clause behaves exactly like OpenMP's safelen
17324 clause. Represent it in OpenMP terms. */
17325 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17327 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17328 return clauses;
17330 location_t loc = c_parser_peek_token (parser)->location;
17331 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17332 expr = c_fully_fold (expr, false, NULL);
17334 /* If expr is an error_mark_node then the above function would have
17335 emitted an error. No reason to do it twice. */
17336 if (expr == error_mark_node)
17338 else if (!TREE_TYPE (expr)
17339 || !TREE_CONSTANT (expr)
17340 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17342 error_at (loc, "vectorlength must be an integer constant");
17343 else if (wi::exact_log2 (expr) == -1)
17344 error_at (loc, "vectorlength must be a power of 2");
17345 else
17347 if (is_simd_fn)
17349 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17350 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17351 OMP_CLAUSE_CHAIN (u) = clauses;
17352 clauses = u;
17354 else
17356 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17357 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17358 OMP_CLAUSE_CHAIN (u) = clauses;
17359 clauses = u;
17363 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17365 return clauses;
17368 /* Cilk Plus:
17369 linear ( simd-linear-variable-list )
17371 simd-linear-variable-list:
17372 simd-linear-variable
17373 simd-linear-variable-list , simd-linear-variable
17375 simd-linear-variable:
17376 id-expression
17377 id-expression : simd-linear-step
17379 simd-linear-step:
17380 conditional-expression */
17382 static tree
17383 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17385 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17386 return clauses;
17388 location_t loc = c_parser_peek_token (parser)->location;
17390 if (c_parser_next_token_is_not (parser, CPP_NAME)
17391 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17392 c_parser_error (parser, "expected identifier");
17394 while (c_parser_next_token_is (parser, CPP_NAME)
17395 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17397 tree var = lookup_name (c_parser_peek_token (parser)->value);
17399 if (var == NULL)
17401 undeclared_variable (c_parser_peek_token (parser)->location,
17402 c_parser_peek_token (parser)->value);
17403 c_parser_consume_token (parser);
17405 else if (var == error_mark_node)
17406 c_parser_consume_token (parser);
17407 else
17409 tree step = integer_one_node;
17411 /* Parse the linear step if present. */
17412 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17414 c_parser_consume_token (parser);
17415 c_parser_consume_token (parser);
17417 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17418 expr = c_fully_fold (expr, false, NULL);
17420 if (TREE_TYPE (expr)
17421 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17422 && (TREE_CONSTANT (expr)
17423 || DECL_P (expr)))
17424 step = expr;
17425 else
17426 c_parser_error (parser,
17427 "step size must be an integer constant "
17428 "expression or an integer variable");
17430 else
17431 c_parser_consume_token (parser);
17433 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17434 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17435 OMP_CLAUSE_DECL (u) = var;
17436 OMP_CLAUSE_LINEAR_STEP (u) = step;
17437 OMP_CLAUSE_CHAIN (u) = clauses;
17438 clauses = u;
17441 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17442 break;
17444 c_parser_consume_token (parser);
17447 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17449 return clauses;
17452 /* Returns the name of the next clause. If the clause is not
17453 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17454 not consumed. Otherwise, the appropriate pragma_simd_clause is
17455 returned and the token is consumed. */
17457 static pragma_omp_clause
17458 c_parser_cilk_clause_name (c_parser *parser)
17460 pragma_omp_clause result;
17461 c_token *token = c_parser_peek_token (parser);
17463 if (!token->value || token->type != CPP_NAME)
17464 return PRAGMA_CILK_CLAUSE_NONE;
17466 const char *p = IDENTIFIER_POINTER (token->value);
17468 if (!strcmp (p, "vectorlength"))
17469 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17470 else if (!strcmp (p, "linear"))
17471 result = PRAGMA_CILK_CLAUSE_LINEAR;
17472 else if (!strcmp (p, "private"))
17473 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17474 else if (!strcmp (p, "firstprivate"))
17475 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17476 else if (!strcmp (p, "lastprivate"))
17477 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17478 else if (!strcmp (p, "reduction"))
17479 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17480 else
17481 return PRAGMA_CILK_CLAUSE_NONE;
17483 c_parser_consume_token (parser);
17484 return result;
17487 /* Parse all #<pragma simd> clauses. Return the list of clauses
17488 found. */
17490 static tree
17491 c_parser_cilk_all_clauses (c_parser *parser)
17493 tree clauses = NULL;
17495 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17497 pragma_omp_clause c_kind;
17499 c_kind = c_parser_cilk_clause_name (parser);
17501 switch (c_kind)
17503 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17504 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17505 break;
17506 case PRAGMA_CILK_CLAUSE_LINEAR:
17507 clauses = c_parser_cilk_clause_linear (parser, clauses);
17508 break;
17509 case PRAGMA_CILK_CLAUSE_PRIVATE:
17510 /* Use the OpenMP counterpart. */
17511 clauses = c_parser_omp_clause_private (parser, clauses);
17512 break;
17513 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17514 /* Use the OpenMP counterpart. */
17515 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17516 break;
17517 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17518 /* Use the OpenMP counterpart. */
17519 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17520 break;
17521 case PRAGMA_CILK_CLAUSE_REDUCTION:
17522 /* Use the OpenMP counterpart. */
17523 clauses = c_parser_omp_clause_reduction (parser, clauses);
17524 break;
17525 default:
17526 c_parser_error (parser, "expected %<#pragma simd%> clause");
17527 goto saw_error;
17531 saw_error:
17532 c_parser_skip_to_pragma_eol (parser);
17533 return c_finish_omp_clauses (clauses, C_ORT_CILK);
17536 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17537 Here is the correct syntax of this pragma:
17538 #pragma cilk grainsize = <EXP>
17541 static void
17542 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
17544 extern tree convert_to_integer (tree, tree);
17546 /* consume the 'grainsize' keyword. */
17547 c_parser_consume_pragma (parser);
17549 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17551 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17552 if (g_expr.value == error_mark_node)
17554 c_parser_skip_to_pragma_eol (parser);
17555 return;
17557 tree grain = convert_to_integer (long_integer_type_node,
17558 c_fully_fold (g_expr.value, false,
17559 NULL));
17560 c_parser_skip_to_pragma_eol (parser);
17561 c_token *token = c_parser_peek_token (parser);
17562 if (token && token->type == CPP_KEYWORD
17563 && token->keyword == RID_CILK_FOR)
17565 if (grain == NULL_TREE || grain == error_mark_node)
17566 grain = integer_zero_node;
17567 c_parser_cilk_for (parser, grain, if_p);
17569 else
17570 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17571 "%<_Cilk_for%>");
17573 else
17574 c_parser_skip_to_pragma_eol (parser);
17577 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17579 static void
17580 c_parser_cilk_simd (c_parser *parser, bool *if_p)
17582 tree clauses = c_parser_cilk_all_clauses (parser);
17583 tree block = c_begin_compound_stmt (true);
17584 location_t loc = c_parser_peek_token (parser)->location;
17585 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
17586 block = c_end_compound_stmt (loc, block, true);
17587 add_stmt (block);
17590 /* Create an artificial decl with TYPE and emit initialization of it with
17591 INIT. */
17593 static tree
17594 c_get_temp_regvar (tree type, tree init)
17596 location_t loc = EXPR_LOCATION (init);
17597 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17598 DECL_ARTIFICIAL (decl) = 1;
17599 DECL_IGNORED_P (decl) = 1;
17600 pushdecl (decl);
17601 tree t = build2 (INIT_EXPR, type, decl, init);
17602 add_stmt (t);
17603 return decl;
17606 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17607 GRAIN is the grain value passed in through pragma or 0. */
17609 static void
17610 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
17612 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17613 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17614 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17615 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
17617 tree block = c_begin_compound_stmt (true);
17618 tree sb = push_stmt_list ();
17619 location_t loc = c_parser_peek_token (parser)->location;
17620 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
17621 if_p);
17622 sb = pop_stmt_list (sb);
17624 if (omp_for)
17626 tree omp_par = make_node (OMP_PARALLEL);
17627 TREE_TYPE (omp_par) = void_type_node;
17628 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17629 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17630 TREE_SIDE_EFFECTS (bind) = 1;
17631 BIND_EXPR_BODY (bind) = sb;
17632 OMP_PARALLEL_BODY (omp_par) = bind;
17633 if (OMP_FOR_PRE_BODY (omp_for))
17635 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17636 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17638 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17639 tree decl = TREE_OPERAND (init, 0);
17640 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17641 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17642 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17643 if (TREE_CODE (t) != INTEGER_CST)
17645 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17646 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17647 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17648 OMP_CLAUSE_CHAIN (c) = clauses;
17649 clauses = c;
17651 if (TREE_CODE (incr) == MODIFY_EXPR)
17653 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17654 if (TREE_CODE (t) != INTEGER_CST)
17656 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17657 = c_get_temp_regvar (TREE_TYPE (t), t);
17658 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17659 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17660 OMP_CLAUSE_CHAIN (c) = clauses;
17661 clauses = c;
17664 t = TREE_OPERAND (init, 1);
17665 if (TREE_CODE (t) != INTEGER_CST)
17667 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17668 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17669 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17670 OMP_CLAUSE_CHAIN (c) = clauses;
17671 clauses = c;
17673 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17674 OMP_CLAUSE_DECL (c) = decl;
17675 OMP_CLAUSE_CHAIN (c) = clauses;
17676 clauses = c;
17677 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17678 OMP_CLAUSE_OPERAND (c, 0)
17679 = cilk_for_number_of_iterations (omp_for);
17680 OMP_CLAUSE_CHAIN (c) = clauses;
17681 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
17682 add_stmt (omp_par);
17685 block = c_end_compound_stmt (loc, block, true);
17686 add_stmt (block);
17690 /* Parse a transaction attribute (GCC Extension).
17692 transaction-attribute:
17693 attributes
17694 [ [ any-word ] ]
17696 The transactional memory language description is written for C++,
17697 and uses the C++0x attribute syntax. For compatibility, allow the
17698 bracket style for transactions in C as well. */
17700 static tree
17701 c_parser_transaction_attributes (c_parser *parser)
17703 tree attr_name, attr = NULL;
17705 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17706 return c_parser_attributes (parser);
17708 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17709 return NULL_TREE;
17710 c_parser_consume_token (parser);
17711 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17712 goto error1;
17714 attr_name = c_parser_attribute_any_word (parser);
17715 if (attr_name)
17717 c_parser_consume_token (parser);
17718 attr = build_tree_list (attr_name, NULL_TREE);
17720 else
17721 c_parser_error (parser, "expected identifier");
17723 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17724 error1:
17725 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17726 return attr;
17729 /* Parse a __transaction_atomic or __transaction_relaxed statement
17730 (GCC Extension).
17732 transaction-statement:
17733 __transaction_atomic transaction-attribute[opt] compound-statement
17734 __transaction_relaxed compound-statement
17736 Note that the only valid attribute is: "outer".
17739 static tree
17740 c_parser_transaction (c_parser *parser, enum rid keyword)
17742 unsigned int old_in = parser->in_transaction;
17743 unsigned int this_in = 1, new_in;
17744 location_t loc = c_parser_peek_token (parser)->location;
17745 tree stmt, attrs;
17747 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17748 || keyword == RID_TRANSACTION_RELAXED)
17749 && c_parser_next_token_is_keyword (parser, keyword));
17750 c_parser_consume_token (parser);
17752 if (keyword == RID_TRANSACTION_RELAXED)
17753 this_in |= TM_STMT_ATTR_RELAXED;
17754 else
17756 attrs = c_parser_transaction_attributes (parser);
17757 if (attrs)
17758 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
17761 /* Keep track if we're in the lexical scope of an outer transaction. */
17762 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
17764 parser->in_transaction = new_in;
17765 stmt = c_parser_compound_statement (parser);
17766 parser->in_transaction = old_in;
17768 if (flag_tm)
17769 stmt = c_finish_transaction (loc, stmt, this_in);
17770 else
17771 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17772 "%<__transaction_atomic%> without transactional memory support enabled"
17773 : "%<__transaction_relaxed %> "
17774 "without transactional memory support enabled"));
17776 return stmt;
17779 /* Parse a __transaction_atomic or __transaction_relaxed expression
17780 (GCC Extension).
17782 transaction-expression:
17783 __transaction_atomic ( expression )
17784 __transaction_relaxed ( expression )
17787 static struct c_expr
17788 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
17790 struct c_expr ret;
17791 unsigned int old_in = parser->in_transaction;
17792 unsigned int this_in = 1;
17793 location_t loc = c_parser_peek_token (parser)->location;
17794 tree attrs;
17796 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17797 || keyword == RID_TRANSACTION_RELAXED)
17798 && c_parser_next_token_is_keyword (parser, keyword));
17799 c_parser_consume_token (parser);
17801 if (keyword == RID_TRANSACTION_RELAXED)
17802 this_in |= TM_STMT_ATTR_RELAXED;
17803 else
17805 attrs = c_parser_transaction_attributes (parser);
17806 if (attrs)
17807 this_in |= parse_tm_stmt_attr (attrs, 0);
17810 parser->in_transaction = this_in;
17811 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17813 tree expr = c_parser_expression (parser).value;
17814 ret.original_type = TREE_TYPE (expr);
17815 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
17816 if (this_in & TM_STMT_ATTR_RELAXED)
17817 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
17818 SET_EXPR_LOCATION (ret.value, loc);
17819 ret.original_code = TRANSACTION_EXPR;
17820 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17822 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
17823 goto error;
17826 else
17828 error:
17829 ret.value = error_mark_node;
17830 ret.original_code = ERROR_MARK;
17831 ret.original_type = NULL;
17833 parser->in_transaction = old_in;
17835 if (!flag_tm)
17836 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17837 "%<__transaction_atomic%> without transactional memory support enabled"
17838 : "%<__transaction_relaxed %> "
17839 "without transactional memory support enabled"));
17841 set_c_expr_source_range (&ret, loc, loc);
17843 return ret;
17846 /* Parse a __transaction_cancel statement (GCC Extension).
17848 transaction-cancel-statement:
17849 __transaction_cancel transaction-attribute[opt] ;
17851 Note that the only valid attribute is "outer".
17854 static tree
17855 c_parser_transaction_cancel (c_parser *parser)
17857 location_t loc = c_parser_peek_token (parser)->location;
17858 tree attrs;
17859 bool is_outer = false;
17861 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
17862 c_parser_consume_token (parser);
17864 attrs = c_parser_transaction_attributes (parser);
17865 if (attrs)
17866 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
17868 if (!flag_tm)
17870 error_at (loc, "%<__transaction_cancel%> without "
17871 "transactional memory support enabled");
17872 goto ret_error;
17874 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
17876 error_at (loc, "%<__transaction_cancel%> within a "
17877 "%<__transaction_relaxed%>");
17878 goto ret_error;
17880 else if (is_outer)
17882 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
17883 && !is_tm_may_cancel_outer (current_function_decl))
17885 error_at (loc, "outer %<__transaction_cancel%> not "
17886 "within outer %<__transaction_atomic%>");
17887 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
17888 goto ret_error;
17891 else if (parser->in_transaction == 0)
17893 error_at (loc, "%<__transaction_cancel%> not within "
17894 "%<__transaction_atomic%>");
17895 goto ret_error;
17898 return add_stmt (build_tm_abort_call (loc, is_outer));
17900 ret_error:
17901 return build1 (NOP_EXPR, void_type_node, error_mark_node);
17904 /* Parse a single source file. */
17906 void
17907 c_parse_file (void)
17909 /* Use local storage to begin. If the first token is a pragma, parse it.
17910 If it is #pragma GCC pch_preprocess, then this will load a PCH file
17911 which will cause garbage collection. */
17912 c_parser tparser;
17914 memset (&tparser, 0, sizeof tparser);
17915 tparser.tokens = &tparser.tokens_buf[0];
17916 the_parser = &tparser;
17918 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
17919 c_parser_pragma_pch_preprocess (&tparser);
17921 the_parser = ggc_alloc<c_parser> ();
17922 *the_parser = tparser;
17923 if (tparser.tokens == &tparser.tokens_buf[0])
17924 the_parser->tokens = &the_parser->tokens_buf[0];
17926 /* Initialize EH, if we've been told to do so. */
17927 if (flag_exceptions)
17928 using_eh_for_cleanups ();
17930 c_parser_translation_unit (the_parser);
17931 the_parser = NULL;
17934 /* This function parses Cilk Plus array notation. The starting index is
17935 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
17936 return value of this function is a tree_node called VALUE_TREE of type
17937 ARRAY_NOTATION_REF. */
17939 static tree
17940 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
17941 tree array_value)
17943 c_token *token = NULL;
17944 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
17945 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
17946 tree array_type_domain = NULL_TREE;
17948 if (array_value == error_mark_node || initial_index == error_mark_node)
17950 /* No need to continue. If either of these 2 were true, then an error
17951 must be emitted already. Thus, no need to emit them twice. */
17952 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17953 return error_mark_node;
17956 array_type = TREE_TYPE (array_value);
17957 gcc_assert (array_type);
17958 if (TREE_CODE (array_type) != ARRAY_TYPE
17959 && TREE_CODE (array_type) != POINTER_TYPE)
17961 error_at (loc, "base of array section must be pointer or array type");
17962 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17963 return error_mark_node;
17965 type = TREE_TYPE (array_type);
17966 token = c_parser_peek_token (parser);
17968 if (token->type == CPP_EOF)
17970 c_parser_error (parser, "expected %<:%> or numeral");
17971 return value_tree;
17973 else if (token->type == CPP_COLON)
17975 if (!initial_index)
17977 /* If we are here, then we have a case like this A[:]. */
17978 c_parser_consume_token (parser);
17979 if (TREE_CODE (array_type) == POINTER_TYPE)
17981 error_at (loc, "start-index and length fields necessary for "
17982 "using array notations in pointers");
17983 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17984 return error_mark_node;
17986 if (TREE_CODE (array_type) == FUNCTION_TYPE)
17988 error_at (loc, "array notations cannot be used with function "
17989 "type");
17990 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17991 return error_mark_node;
17993 array_type_domain = TYPE_DOMAIN (array_type);
17995 if (!array_type_domain)
17997 error_at (loc, "start-index and length fields necessary for "
17998 "using array notations in dimensionless arrays");
17999 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18000 return error_mark_node;
18003 start_index = TYPE_MINVAL (array_type_domain);
18004 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
18005 start_index);
18006 if (!TYPE_MAXVAL (array_type_domain)
18007 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
18009 error_at (loc, "start-index and length fields necessary for "
18010 "using array notations in variable-length arrays");
18011 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18012 return error_mark_node;
18014 end_index = TYPE_MAXVAL (array_type_domain);
18015 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18016 end_index, integer_one_node);
18017 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18018 stride = build_int_cst (integer_type_node, 1);
18019 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18021 else if (initial_index != error_mark_node)
18023 /* If we are here, then there should be 2 possibilities:
18024 1. Array [EXPR : EXPR]
18025 2. Array [EXPR : EXPR : EXPR]
18027 start_index = initial_index;
18029 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18031 error_at (loc, "array notations cannot be used with function "
18032 "type");
18033 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18034 return error_mark_node;
18036 c_parser_consume_token (parser); /* consume the ':' */
18037 struct c_expr ce = c_parser_expression (parser);
18038 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18039 end_index = ce.value;
18040 if (!end_index || end_index == error_mark_node)
18042 c_parser_skip_to_end_of_block_or_statement (parser);
18043 return error_mark_node;
18045 if (c_parser_peek_token (parser)->type == CPP_COLON)
18047 c_parser_consume_token (parser);
18048 ce = c_parser_expression (parser);
18049 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18050 stride = ce.value;
18051 if (!stride || stride == error_mark_node)
18053 c_parser_skip_to_end_of_block_or_statement (parser);
18054 return error_mark_node;
18058 else
18059 c_parser_error (parser, "expected array notation expression");
18061 else
18062 c_parser_error (parser, "expected array notation expression");
18064 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18066 value_tree = build_array_notation_ref (loc, array_value, start_index,
18067 end_index, stride, type);
18068 if (value_tree != error_mark_node)
18069 SET_EXPR_LOCATION (value_tree, loc);
18070 return value_tree;
18073 #include "gt-c-c-parser.h"