Update .po files.
[official-gcc.git] / gcc / c / c-parser.c
blob36c44ab5b56e8265bc14af452fe84bfcedfa028a
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 *);
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 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3436 /* Either we are at the end of an abstract declarator, or we have
3437 parentheses. */
3439 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3441 tree attrs;
3442 struct c_declarator *inner;
3443 c_parser_consume_token (parser);
3444 attrs = c_parser_attributes (parser);
3445 if (kind != C_DTR_NORMAL
3446 && (c_parser_next_token_starts_declspecs (parser)
3447 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3449 struct c_arg_info *args
3450 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3451 attrs);
3452 if (args == NULL)
3453 return NULL;
3454 else
3456 inner
3457 = build_function_declarator (args,
3458 build_id_declarator (NULL_TREE));
3459 return c_parser_direct_declarator_inner (parser, *seen_id,
3460 inner);
3463 /* A parenthesized declarator. */
3464 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3465 if (inner != NULL && attrs != NULL)
3466 inner = build_attrs_declarator (attrs, inner);
3467 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3469 c_parser_consume_token (parser);
3470 if (inner == NULL)
3471 return NULL;
3472 else
3473 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3475 else
3477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3478 "expected %<)%>");
3479 return NULL;
3482 else
3484 if (kind == C_DTR_NORMAL)
3486 c_parser_error (parser, "expected identifier or %<(%>");
3487 return NULL;
3489 else
3490 return build_id_declarator (NULL_TREE);
3494 /* Parse part of a direct declarator or direct abstract declarator,
3495 given that some (in INNER) has already been parsed; ID_PRESENT is
3496 true if an identifier is present, false for an abstract
3497 declarator. */
3499 static struct c_declarator *
3500 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3501 struct c_declarator *inner)
3503 /* Parse a sequence of array declarators and parameter lists. */
3504 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3506 location_t brace_loc = c_parser_peek_token (parser)->location;
3507 struct c_declarator *declarator;
3508 struct c_declspecs *quals_attrs = build_null_declspecs ();
3509 bool static_seen;
3510 bool star_seen;
3511 struct c_expr dimen;
3512 dimen.value = NULL_TREE;
3513 dimen.original_code = ERROR_MARK;
3514 dimen.original_type = NULL_TREE;
3515 c_parser_consume_token (parser);
3516 c_parser_declspecs (parser, quals_attrs, false, false, true,
3517 false, false, cla_prefer_id);
3518 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3519 if (static_seen)
3520 c_parser_consume_token (parser);
3521 if (static_seen && !quals_attrs->declspecs_seen_p)
3522 c_parser_declspecs (parser, quals_attrs, false, false, true,
3523 false, false, cla_prefer_id);
3524 if (!quals_attrs->declspecs_seen_p)
3525 quals_attrs = NULL;
3526 /* If "static" is present, there must be an array dimension.
3527 Otherwise, there may be a dimension, "*", or no
3528 dimension. */
3529 if (static_seen)
3531 star_seen = false;
3532 dimen = c_parser_expr_no_commas (parser, NULL);
3534 else
3536 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3538 dimen.value = NULL_TREE;
3539 star_seen = false;
3541 else if (flag_cilkplus
3542 && c_parser_next_token_is (parser, CPP_COLON))
3544 dimen.value = error_mark_node;
3545 star_seen = false;
3546 error_at (c_parser_peek_token (parser)->location,
3547 "array notations cannot be used in declaration");
3548 c_parser_consume_token (parser);
3550 else if (c_parser_next_token_is (parser, CPP_MULT))
3552 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3554 dimen.value = NULL_TREE;
3555 star_seen = true;
3556 c_parser_consume_token (parser);
3558 else
3560 star_seen = false;
3561 dimen = c_parser_expr_no_commas (parser, NULL);
3564 else
3566 star_seen = false;
3567 dimen = c_parser_expr_no_commas (parser, NULL);
3570 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3571 c_parser_consume_token (parser);
3572 else if (flag_cilkplus
3573 && c_parser_next_token_is (parser, CPP_COLON))
3575 error_at (c_parser_peek_token (parser)->location,
3576 "array notations cannot be used in declaration");
3577 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3578 return NULL;
3580 else
3582 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3583 "expected %<]%>");
3584 return NULL;
3586 if (dimen.value)
3587 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3588 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3589 static_seen, star_seen);
3590 if (declarator == NULL)
3591 return NULL;
3592 inner = set_array_declarator_inner (declarator, inner);
3593 return c_parser_direct_declarator_inner (parser, id_present, inner);
3595 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3597 tree attrs;
3598 struct c_arg_info *args;
3599 c_parser_consume_token (parser);
3600 attrs = c_parser_attributes (parser);
3601 args = c_parser_parms_declarator (parser, id_present, attrs);
3602 if (args == NULL)
3603 return NULL;
3604 else
3606 inner = build_function_declarator (args, inner);
3607 return c_parser_direct_declarator_inner (parser, id_present, inner);
3610 return inner;
3613 /* Parse a parameter list or identifier list, including the closing
3614 parenthesis but not the opening one. ATTRS are the attributes at
3615 the start of the list. ID_LIST_OK is true if an identifier list is
3616 acceptable; such a list must not have attributes at the start. */
3618 static struct c_arg_info *
3619 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3621 push_scope ();
3622 declare_parm_level ();
3623 /* If the list starts with an identifier, it is an identifier list.
3624 Otherwise, it is either a prototype list or an empty list. */
3625 if (id_list_ok
3626 && !attrs
3627 && c_parser_next_token_is (parser, CPP_NAME)
3628 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3630 /* Look ahead to detect typos in type names. */
3631 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3632 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3633 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3634 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3636 tree list = NULL_TREE, *nextp = &list;
3637 while (c_parser_next_token_is (parser, CPP_NAME)
3638 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3640 *nextp = build_tree_list (NULL_TREE,
3641 c_parser_peek_token (parser)->value);
3642 nextp = & TREE_CHAIN (*nextp);
3643 c_parser_consume_token (parser);
3644 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3645 break;
3646 c_parser_consume_token (parser);
3647 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3649 c_parser_error (parser, "expected identifier");
3650 break;
3653 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3655 struct c_arg_info *ret = build_arg_info ();
3656 ret->types = list;
3657 c_parser_consume_token (parser);
3658 pop_scope ();
3659 return ret;
3661 else
3663 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3664 "expected %<)%>");
3665 pop_scope ();
3666 return NULL;
3669 else
3671 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3672 NULL);
3673 pop_scope ();
3674 return ret;
3678 /* Parse a parameter list (possibly empty), including the closing
3679 parenthesis but not the opening one. ATTRS are the attributes at
3680 the start of the list. EXPR is NULL or an expression that needs to
3681 be evaluated for the side effects of array size expressions in the
3682 parameters. */
3684 static struct c_arg_info *
3685 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3687 bool bad_parm = false;
3689 /* ??? Following the old parser, forward parameter declarations may
3690 use abstract declarators, and if no real parameter declarations
3691 follow the forward declarations then this is not diagnosed. Also
3692 note as above that attributes are ignored as the only contents of
3693 the parentheses, or as the only contents after forward
3694 declarations. */
3695 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3697 struct c_arg_info *ret = build_arg_info ();
3698 c_parser_consume_token (parser);
3699 return ret;
3701 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3703 struct c_arg_info *ret = build_arg_info ();
3705 if (flag_allow_parameterless_variadic_functions)
3707 /* F (...) is allowed. */
3708 ret->types = NULL_TREE;
3710 else
3712 /* Suppress -Wold-style-definition for this case. */
3713 ret->types = error_mark_node;
3714 error_at (c_parser_peek_token (parser)->location,
3715 "ISO C requires a named argument before %<...%>");
3717 c_parser_consume_token (parser);
3718 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3720 c_parser_consume_token (parser);
3721 return ret;
3723 else
3725 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3726 "expected %<)%>");
3727 return NULL;
3730 /* Nonempty list of parameters, either terminated with semicolon
3731 (forward declarations; recurse) or with close parenthesis (normal
3732 function) or with ", ... )" (variadic function). */
3733 while (true)
3735 /* Parse a parameter. */
3736 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3737 attrs = NULL_TREE;
3738 if (parm == NULL)
3739 bad_parm = true;
3740 else
3741 push_parm_decl (parm, &expr);
3742 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3744 tree new_attrs;
3745 c_parser_consume_token (parser);
3746 mark_forward_parm_decls ();
3747 new_attrs = c_parser_attributes (parser);
3748 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3750 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3752 c_parser_consume_token (parser);
3753 if (bad_parm)
3754 return NULL;
3755 else
3756 return get_parm_info (false, expr);
3758 if (!c_parser_require (parser, CPP_COMMA,
3759 "expected %<;%>, %<,%> or %<)%>"))
3761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3762 return NULL;
3764 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3766 c_parser_consume_token (parser);
3767 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3769 c_parser_consume_token (parser);
3770 if (bad_parm)
3771 return NULL;
3772 else
3773 return get_parm_info (true, expr);
3775 else
3777 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3778 "expected %<)%>");
3779 return NULL;
3785 /* Parse a parameter declaration. ATTRS are the attributes at the
3786 start of the declaration if it is the first parameter. */
3788 static struct c_parm *
3789 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3791 struct c_declspecs *specs;
3792 struct c_declarator *declarator;
3793 tree prefix_attrs;
3794 tree postfix_attrs = NULL_TREE;
3795 bool dummy = false;
3797 /* Accept #pragmas between parameter declarations. */
3798 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3799 c_parser_pragma (parser, pragma_param, NULL);
3801 if (!c_parser_next_token_starts_declspecs (parser))
3803 c_token *token = c_parser_peek_token (parser);
3804 if (parser->error)
3805 return NULL;
3806 c_parser_set_source_position_from_token (token);
3807 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3809 error_at (token->location, "unknown type name %qE", token->value);
3810 parser->error = true;
3812 /* ??? In some Objective-C cases '...' isn't applicable so there
3813 should be a different message. */
3814 else
3815 c_parser_error (parser,
3816 "expected declaration specifiers or %<...%>");
3817 c_parser_skip_to_end_of_parameter (parser);
3818 return NULL;
3820 specs = build_null_declspecs ();
3821 if (attrs)
3823 declspecs_add_attrs (input_location, specs, attrs);
3824 attrs = NULL_TREE;
3826 c_parser_declspecs (parser, specs, true, true, true, true, false,
3827 cla_nonabstract_decl);
3828 finish_declspecs (specs);
3829 pending_xref_error ();
3830 prefix_attrs = specs->attrs;
3831 specs->attrs = NULL_TREE;
3832 declarator = c_parser_declarator (parser,
3833 specs->typespec_kind != ctsk_none,
3834 C_DTR_PARM, &dummy);
3835 if (declarator == NULL)
3837 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3838 return NULL;
3840 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3841 postfix_attrs = c_parser_attributes (parser);
3842 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3843 declarator);
3846 /* Parse a string literal in an asm expression. It should not be
3847 translated, and wide string literals are an error although
3848 permitted by the syntax. This is a GNU extension.
3850 asm-string-literal:
3851 string-literal
3853 ??? At present, following the old parser, the caller needs to have
3854 set lex_untranslated_string to 1. It would be better to follow the
3855 C++ parser rather than using this kludge. */
3857 static tree
3858 c_parser_asm_string_literal (c_parser *parser)
3860 tree str;
3861 int save_flag = warn_overlength_strings;
3862 warn_overlength_strings = 0;
3863 if (c_parser_next_token_is (parser, CPP_STRING))
3865 str = c_parser_peek_token (parser)->value;
3866 c_parser_consume_token (parser);
3868 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3870 error_at (c_parser_peek_token (parser)->location,
3871 "wide string literal in %<asm%>");
3872 str = build_string (1, "");
3873 c_parser_consume_token (parser);
3875 else
3877 c_parser_error (parser, "expected string literal");
3878 str = NULL_TREE;
3880 warn_overlength_strings = save_flag;
3881 return str;
3884 /* Parse a simple asm expression. This is used in restricted
3885 contexts, where a full expression with inputs and outputs does not
3886 make sense. This is a GNU extension.
3888 simple-asm-expr:
3889 asm ( asm-string-literal )
3892 static tree
3893 c_parser_simple_asm_expr (c_parser *parser)
3895 tree str;
3896 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3897 /* ??? Follow the C++ parser rather than using the
3898 lex_untranslated_string kludge. */
3899 parser->lex_untranslated_string = true;
3900 c_parser_consume_token (parser);
3901 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3903 parser->lex_untranslated_string = false;
3904 return NULL_TREE;
3906 str = c_parser_asm_string_literal (parser);
3907 parser->lex_untranslated_string = false;
3908 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3910 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3911 return NULL_TREE;
3913 return str;
3916 static tree
3917 c_parser_attribute_any_word (c_parser *parser)
3919 tree attr_name = NULL_TREE;
3921 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3923 /* ??? See comment above about what keywords are accepted here. */
3924 bool ok;
3925 switch (c_parser_peek_token (parser)->keyword)
3927 case RID_STATIC:
3928 case RID_UNSIGNED:
3929 case RID_LONG:
3930 case RID_CONST:
3931 case RID_EXTERN:
3932 case RID_REGISTER:
3933 case RID_TYPEDEF:
3934 case RID_SHORT:
3935 case RID_INLINE:
3936 case RID_NORETURN:
3937 case RID_VOLATILE:
3938 case RID_SIGNED:
3939 case RID_AUTO:
3940 case RID_RESTRICT:
3941 case RID_COMPLEX:
3942 case RID_THREAD:
3943 case RID_INT:
3944 case RID_CHAR:
3945 case RID_FLOAT:
3946 case RID_DOUBLE:
3947 case RID_VOID:
3948 case RID_DFLOAT32:
3949 case RID_DFLOAT64:
3950 case RID_DFLOAT128:
3951 case RID_BOOL:
3952 case RID_FRACT:
3953 case RID_ACCUM:
3954 case RID_SAT:
3955 case RID_TRANSACTION_ATOMIC:
3956 case RID_TRANSACTION_CANCEL:
3957 case RID_ATOMIC:
3958 case RID_AUTO_TYPE:
3959 case RID_INT_N_0:
3960 case RID_INT_N_1:
3961 case RID_INT_N_2:
3962 case RID_INT_N_3:
3963 ok = true;
3964 break;
3965 default:
3966 ok = false;
3967 break;
3969 if (!ok)
3970 return NULL_TREE;
3972 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3973 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3975 else if (c_parser_next_token_is (parser, CPP_NAME))
3976 attr_name = c_parser_peek_token (parser)->value;
3978 return attr_name;
3981 #define CILK_SIMD_FN_CLAUSE_MASK \
3982 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3983 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3984 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3985 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3986 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3988 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3989 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3990 pushed into the token list.
3991 Syntax:
3992 vector
3993 vector (<vector attributes>). */
3995 static void
3996 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3998 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4000 int paren_scope = 0;
4001 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4002 /* Consume the "vector" token. */
4003 c_parser_consume_token (parser);
4005 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4007 c_parser_consume_token (parser);
4008 paren_scope++;
4010 while (paren_scope > 0)
4012 c_token *token = c_parser_peek_token (parser);
4013 if (token->type == CPP_OPEN_PAREN)
4014 paren_scope++;
4015 else if (token->type == CPP_CLOSE_PAREN)
4016 paren_scope--;
4017 /* Do not push the last ')' since we are not pushing the '('. */
4018 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4019 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4020 c_parser_consume_token (parser);
4023 /* Since we are converting an attribute to a pragma, we need to end the
4024 attribute with PRAGMA_EOL. */
4025 c_token eol_token;
4026 memset (&eol_token, 0, sizeof (eol_token));
4027 eol_token.type = CPP_PRAGMA_EOL;
4028 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4031 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4033 static void
4034 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4036 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4038 /* c_parser_attributes is called in several places, so if these EOF
4039 tokens are already inserted, then don't do them again. */
4040 if (last_token.type == CPP_EOF)
4041 return;
4043 /* Two CPP_EOF token are added as a safety net since the normal C
4044 front-end has two token look-ahead. */
4045 c_token eof_token;
4046 eof_token.type = CPP_EOF;
4047 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4048 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4051 /* Parse (possibly empty) attributes. This is a GNU extension.
4053 attributes:
4054 empty
4055 attributes attribute
4057 attribute:
4058 __attribute__ ( ( attribute-list ) )
4060 attribute-list:
4061 attrib
4062 attribute_list , attrib
4064 attrib:
4065 empty
4066 any-word
4067 any-word ( identifier )
4068 any-word ( identifier , nonempty-expr-list )
4069 any-word ( expr-list )
4071 where the "identifier" must not be declared as a type, and
4072 "any-word" may be any identifier (including one declared as a
4073 type), a reserved word storage class specifier, type specifier or
4074 type qualifier. ??? This still leaves out most reserved keywords
4075 (following the old parser), shouldn't we include them, and why not
4076 allow identifiers declared as types to start the arguments? */
4078 static tree
4079 c_parser_attributes (c_parser *parser)
4081 tree attrs = NULL_TREE;
4082 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4084 /* ??? Follow the C++ parser rather than using the
4085 lex_untranslated_string kludge. */
4086 parser->lex_untranslated_string = true;
4087 /* Consume the `__attribute__' keyword. */
4088 c_parser_consume_token (parser);
4089 /* Look for the two `(' tokens. */
4090 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4092 parser->lex_untranslated_string = false;
4093 return attrs;
4095 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4097 parser->lex_untranslated_string = false;
4098 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4099 return attrs;
4101 /* Parse the attribute list. */
4102 while (c_parser_next_token_is (parser, CPP_COMMA)
4103 || c_parser_next_token_is (parser, CPP_NAME)
4104 || c_parser_next_token_is (parser, CPP_KEYWORD))
4106 tree attr, attr_name, attr_args;
4107 vec<tree, va_gc> *expr_list;
4108 if (c_parser_next_token_is (parser, CPP_COMMA))
4110 c_parser_consume_token (parser);
4111 continue;
4114 attr_name = c_parser_attribute_any_word (parser);
4115 if (attr_name == NULL)
4116 break;
4117 if (is_cilkplus_vector_p (attr_name))
4119 c_token *v_token = c_parser_peek_token (parser);
4120 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4121 /* If the next token isn't a comma, we're done. */
4122 if (!c_parser_next_token_is (parser, CPP_COMMA))
4123 break;
4124 continue;
4126 c_parser_consume_token (parser);
4127 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4129 attr = build_tree_list (attr_name, NULL_TREE);
4130 /* Add this attribute to the list. */
4131 attrs = chainon (attrs, attr);
4132 /* If the next token isn't a comma, we're done. */
4133 if (!c_parser_next_token_is (parser, CPP_COMMA))
4134 break;
4135 continue;
4137 c_parser_consume_token (parser);
4138 /* Parse the attribute contents. If they start with an
4139 identifier which is followed by a comma or close
4140 parenthesis, then the arguments start with that
4141 identifier; otherwise they are an expression list.
4142 In objective-c the identifier may be a classname. */
4143 if (c_parser_next_token_is (parser, CPP_NAME)
4144 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4145 || (c_dialect_objc ()
4146 && c_parser_peek_token (parser)->id_kind
4147 == C_ID_CLASSNAME))
4148 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4149 || (c_parser_peek_2nd_token (parser)->type
4150 == CPP_CLOSE_PAREN))
4151 && (attribute_takes_identifier_p (attr_name)
4152 || (c_dialect_objc ()
4153 && c_parser_peek_token (parser)->id_kind
4154 == C_ID_CLASSNAME)))
4156 tree arg1 = c_parser_peek_token (parser)->value;
4157 c_parser_consume_token (parser);
4158 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4159 attr_args = build_tree_list (NULL_TREE, arg1);
4160 else
4162 tree tree_list;
4163 c_parser_consume_token (parser);
4164 expr_list = c_parser_expr_list (parser, false, true,
4165 NULL, NULL, NULL, NULL);
4166 tree_list = build_tree_list_vec (expr_list);
4167 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4168 release_tree_vector (expr_list);
4171 else
4173 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4174 attr_args = NULL_TREE;
4175 else
4177 expr_list = c_parser_expr_list (parser, false, true,
4178 NULL, NULL, NULL, NULL);
4179 attr_args = build_tree_list_vec (expr_list);
4180 release_tree_vector (expr_list);
4183 attr = build_tree_list (attr_name, attr_args);
4184 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4185 c_parser_consume_token (parser);
4186 else
4188 parser->lex_untranslated_string = false;
4189 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4190 "expected %<)%>");
4191 return attrs;
4193 /* Add this attribute to the list. */
4194 attrs = chainon (attrs, attr);
4195 /* If the next token isn't a comma, we're done. */
4196 if (!c_parser_next_token_is (parser, CPP_COMMA))
4197 break;
4199 /* Look for the two `)' tokens. */
4200 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4201 c_parser_consume_token (parser);
4202 else
4204 parser->lex_untranslated_string = false;
4205 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4206 "expected %<)%>");
4207 return attrs;
4209 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4210 c_parser_consume_token (parser);
4211 else
4213 parser->lex_untranslated_string = false;
4214 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4215 "expected %<)%>");
4216 return attrs;
4218 parser->lex_untranslated_string = false;
4221 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4222 c_finish_cilk_simd_fn_tokens (parser);
4223 return attrs;
4226 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4228 type-name:
4229 specifier-qualifier-list abstract-declarator[opt]
4232 static struct c_type_name *
4233 c_parser_type_name (c_parser *parser)
4235 struct c_declspecs *specs = build_null_declspecs ();
4236 struct c_declarator *declarator;
4237 struct c_type_name *ret;
4238 bool dummy = false;
4239 c_parser_declspecs (parser, specs, false, true, true, false, false,
4240 cla_prefer_type);
4241 if (!specs->declspecs_seen_p)
4243 c_parser_error (parser, "expected specifier-qualifier-list");
4244 return NULL;
4246 if (specs->type != error_mark_node)
4248 pending_xref_error ();
4249 finish_declspecs (specs);
4251 declarator = c_parser_declarator (parser,
4252 specs->typespec_kind != ctsk_none,
4253 C_DTR_ABSTRACT, &dummy);
4254 if (declarator == NULL)
4255 return NULL;
4256 ret = XOBNEW (&parser_obstack, struct c_type_name);
4257 ret->specs = specs;
4258 ret->declarator = declarator;
4259 return ret;
4262 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4264 initializer:
4265 assignment-expression
4266 { initializer-list }
4267 { initializer-list , }
4269 initializer-list:
4270 designation[opt] initializer
4271 initializer-list , designation[opt] initializer
4273 designation:
4274 designator-list =
4276 designator-list:
4277 designator
4278 designator-list designator
4280 designator:
4281 array-designator
4282 . identifier
4284 array-designator:
4285 [ constant-expression ]
4287 GNU extensions:
4289 initializer:
4292 designation:
4293 array-designator
4294 identifier :
4296 array-designator:
4297 [ constant-expression ... constant-expression ]
4299 Any expression without commas is accepted in the syntax for the
4300 constant-expressions, with non-constant expressions rejected later.
4302 This function is only used for top-level initializers; for nested
4303 ones, see c_parser_initval. */
4305 static struct c_expr
4306 c_parser_initializer (c_parser *parser)
4308 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4309 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4310 else
4312 struct c_expr ret;
4313 location_t loc = c_parser_peek_token (parser)->location;
4314 ret = c_parser_expr_no_commas (parser, NULL);
4315 if (TREE_CODE (ret.value) != STRING_CST
4316 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4317 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4318 return ret;
4322 /* Parse a braced initializer list. TYPE is the type specified for a
4323 compound literal, and NULL_TREE for other initializers and for
4324 nested braced lists. NESTED_P is true for nested braced lists,
4325 false for the list of a compound literal or the list that is the
4326 top-level initializer in a declaration. */
4328 static struct c_expr
4329 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4330 struct obstack *outer_obstack)
4332 struct c_expr ret;
4333 struct obstack braced_init_obstack;
4334 location_t brace_loc = c_parser_peek_token (parser)->location;
4335 gcc_obstack_init (&braced_init_obstack);
4336 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4337 c_parser_consume_token (parser);
4338 if (nested_p)
4340 finish_implicit_inits (brace_loc, outer_obstack);
4341 push_init_level (brace_loc, 0, &braced_init_obstack);
4343 else
4344 really_start_incremental_init (type);
4345 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4347 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4349 else
4351 /* Parse a non-empty initializer list, possibly with a trailing
4352 comma. */
4353 while (true)
4355 c_parser_initelt (parser, &braced_init_obstack);
4356 if (parser->error)
4357 break;
4358 if (c_parser_next_token_is (parser, CPP_COMMA))
4359 c_parser_consume_token (parser);
4360 else
4361 break;
4362 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4363 break;
4366 c_token *next_tok = c_parser_peek_token (parser);
4367 if (next_tok->type != CPP_CLOSE_BRACE)
4369 ret.value = error_mark_node;
4370 ret.original_code = ERROR_MARK;
4371 ret.original_type = NULL;
4372 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4373 pop_init_level (brace_loc, 0, &braced_init_obstack);
4374 obstack_free (&braced_init_obstack, NULL);
4375 return ret;
4377 location_t close_loc = next_tok->location;
4378 c_parser_consume_token (parser);
4379 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4380 obstack_free (&braced_init_obstack, NULL);
4381 set_c_expr_source_range (&ret, brace_loc, close_loc);
4382 return ret;
4385 /* Parse a nested initializer, including designators. */
4387 static void
4388 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4390 /* Parse any designator or designator list. A single array
4391 designator may have the subsequent "=" omitted in GNU C, but a
4392 longer list or a structure member designator may not. */
4393 if (c_parser_next_token_is (parser, CPP_NAME)
4394 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4396 /* Old-style structure member designator. */
4397 set_init_label (c_parser_peek_token (parser)->location,
4398 c_parser_peek_token (parser)->value,
4399 braced_init_obstack);
4400 /* Use the colon as the error location. */
4401 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4402 "obsolete use of designated initializer with %<:%>");
4403 c_parser_consume_token (parser);
4404 c_parser_consume_token (parser);
4406 else
4408 /* des_seen is 0 if there have been no designators, 1 if there
4409 has been a single array designator and 2 otherwise. */
4410 int des_seen = 0;
4411 /* Location of a designator. */
4412 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4413 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4414 || c_parser_next_token_is (parser, CPP_DOT))
4416 int des_prev = des_seen;
4417 if (!des_seen)
4418 des_loc = c_parser_peek_token (parser)->location;
4419 if (des_seen < 2)
4420 des_seen++;
4421 if (c_parser_next_token_is (parser, CPP_DOT))
4423 des_seen = 2;
4424 c_parser_consume_token (parser);
4425 if (c_parser_next_token_is (parser, CPP_NAME))
4427 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4428 braced_init_obstack);
4429 c_parser_consume_token (parser);
4431 else
4433 struct c_expr init;
4434 init.value = error_mark_node;
4435 init.original_code = ERROR_MARK;
4436 init.original_type = NULL;
4437 c_parser_error (parser, "expected identifier");
4438 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4439 process_init_element (input_location, init, false,
4440 braced_init_obstack);
4441 return;
4444 else
4446 tree first, second;
4447 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4448 location_t array_index_loc = UNKNOWN_LOCATION;
4449 /* ??? Following the old parser, [ objc-receiver
4450 objc-message-args ] is accepted as an initializer,
4451 being distinguished from a designator by what follows
4452 the first assignment expression inside the square
4453 brackets, but after a first array designator a
4454 subsequent square bracket is for Objective-C taken to
4455 start an expression, using the obsolete form of
4456 designated initializer without '=', rather than
4457 possibly being a second level of designation: in LALR
4458 terms, the '[' is shifted rather than reducing
4459 designator to designator-list. */
4460 if (des_prev == 1 && c_dialect_objc ())
4462 des_seen = des_prev;
4463 break;
4465 if (des_prev == 0 && c_dialect_objc ())
4467 /* This might be an array designator or an
4468 Objective-C message expression. If the former,
4469 continue parsing here; if the latter, parse the
4470 remainder of the initializer given the starting
4471 primary-expression. ??? It might make sense to
4472 distinguish when des_prev == 1 as well; see
4473 previous comment. */
4474 tree rec, args;
4475 struct c_expr mexpr;
4476 c_parser_consume_token (parser);
4477 if (c_parser_peek_token (parser)->type == CPP_NAME
4478 && ((c_parser_peek_token (parser)->id_kind
4479 == C_ID_TYPENAME)
4480 || (c_parser_peek_token (parser)->id_kind
4481 == C_ID_CLASSNAME)))
4483 /* Type name receiver. */
4484 tree id = c_parser_peek_token (parser)->value;
4485 c_parser_consume_token (parser);
4486 rec = objc_get_class_reference (id);
4487 goto parse_message_args;
4489 first = c_parser_expr_no_commas (parser, NULL).value;
4490 mark_exp_read (first);
4491 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4492 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4493 goto array_desig_after_first;
4494 /* Expression receiver. So far only one part
4495 without commas has been parsed; there might be
4496 more of the expression. */
4497 rec = first;
4498 while (c_parser_next_token_is (parser, CPP_COMMA))
4500 struct c_expr next;
4501 location_t comma_loc, exp_loc;
4502 comma_loc = c_parser_peek_token (parser)->location;
4503 c_parser_consume_token (parser);
4504 exp_loc = c_parser_peek_token (parser)->location;
4505 next = c_parser_expr_no_commas (parser, NULL);
4506 next = convert_lvalue_to_rvalue (exp_loc, next,
4507 true, true);
4508 rec = build_compound_expr (comma_loc, rec, next.value);
4510 parse_message_args:
4511 /* Now parse the objc-message-args. */
4512 args = c_parser_objc_message_args (parser);
4513 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4514 "expected %<]%>");
4515 mexpr.value
4516 = objc_build_message_expr (rec, args);
4517 mexpr.original_code = ERROR_MARK;
4518 mexpr.original_type = NULL;
4519 /* Now parse and process the remainder of the
4520 initializer, starting with this message
4521 expression as a primary-expression. */
4522 c_parser_initval (parser, &mexpr, braced_init_obstack);
4523 return;
4525 c_parser_consume_token (parser);
4526 array_index_loc = c_parser_peek_token (parser)->location;
4527 first = c_parser_expr_no_commas (parser, NULL).value;
4528 mark_exp_read (first);
4529 array_desig_after_first:
4530 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4532 ellipsis_loc = c_parser_peek_token (parser)->location;
4533 c_parser_consume_token (parser);
4534 second = c_parser_expr_no_commas (parser, NULL).value;
4535 mark_exp_read (second);
4537 else
4538 second = NULL_TREE;
4539 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4541 c_parser_consume_token (parser);
4542 set_init_index (array_index_loc, first, second,
4543 braced_init_obstack);
4544 if (second)
4545 pedwarn (ellipsis_loc, OPT_Wpedantic,
4546 "ISO C forbids specifying range of elements to initialize");
4548 else
4549 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4550 "expected %<]%>");
4553 if (des_seen >= 1)
4555 if (c_parser_next_token_is (parser, CPP_EQ))
4557 pedwarn_c90 (des_loc, OPT_Wpedantic,
4558 "ISO C90 forbids specifying subobject "
4559 "to initialize");
4560 c_parser_consume_token (parser);
4562 else
4564 if (des_seen == 1)
4565 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4566 "obsolete use of designated initializer without %<=%>");
4567 else
4569 struct c_expr init;
4570 init.value = error_mark_node;
4571 init.original_code = ERROR_MARK;
4572 init.original_type = NULL;
4573 c_parser_error (parser, "expected %<=%>");
4574 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4575 process_init_element (input_location, init, false,
4576 braced_init_obstack);
4577 return;
4582 c_parser_initval (parser, NULL, braced_init_obstack);
4585 /* Parse a nested initializer; as c_parser_initializer but parses
4586 initializers within braced lists, after any designators have been
4587 applied. If AFTER is not NULL then it is an Objective-C message
4588 expression which is the primary-expression starting the
4589 initializer. */
4591 static void
4592 c_parser_initval (c_parser *parser, struct c_expr *after,
4593 struct obstack * braced_init_obstack)
4595 struct c_expr init;
4596 gcc_assert (!after || c_dialect_objc ());
4597 location_t loc = c_parser_peek_token (parser)->location;
4599 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4600 init = c_parser_braced_init (parser, NULL_TREE, true,
4601 braced_init_obstack);
4602 else
4604 init = c_parser_expr_no_commas (parser, after);
4605 if (init.value != NULL_TREE
4606 && TREE_CODE (init.value) != STRING_CST
4607 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4608 init = convert_lvalue_to_rvalue (loc, init, true, true);
4610 process_init_element (loc, init, false, braced_init_obstack);
4613 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4614 C99 6.8.2).
4616 compound-statement:
4617 { block-item-list[opt] }
4618 { label-declarations block-item-list }
4620 block-item-list:
4621 block-item
4622 block-item-list block-item
4624 block-item:
4625 nested-declaration
4626 statement
4628 nested-declaration:
4629 declaration
4631 GNU extensions:
4633 compound-statement:
4634 { label-declarations block-item-list }
4636 nested-declaration:
4637 __extension__ nested-declaration
4638 nested-function-definition
4640 label-declarations:
4641 label-declaration
4642 label-declarations label-declaration
4644 label-declaration:
4645 __label__ identifier-list ;
4647 Allowing the mixing of declarations and code is new in C99. The
4648 GNU syntax also permits (not shown above) labels at the end of
4649 compound statements, which yield an error. We don't allow labels
4650 on declarations; this might seem like a natural extension, but
4651 there would be a conflict between attributes on the label and
4652 prefix attributes on the declaration. ??? The syntax follows the
4653 old parser in requiring something after label declarations.
4654 Although they are erroneous if the labels declared aren't defined,
4655 is it useful for the syntax to be this way?
4657 OpenACC:
4659 block-item:
4660 openacc-directive
4662 openacc-directive:
4663 update-directive
4665 OpenMP:
4667 block-item:
4668 openmp-directive
4670 openmp-directive:
4671 barrier-directive
4672 flush-directive
4673 taskwait-directive
4674 taskyield-directive
4675 cancel-directive
4676 cancellation-point-directive */
4678 static tree
4679 c_parser_compound_statement (c_parser *parser)
4681 tree stmt;
4682 location_t brace_loc;
4683 brace_loc = c_parser_peek_token (parser)->location;
4684 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4686 /* Ensure a scope is entered and left anyway to avoid confusion
4687 if we have just prepared to enter a function body. */
4688 stmt = c_begin_compound_stmt (true);
4689 c_end_compound_stmt (brace_loc, stmt, true);
4690 return error_mark_node;
4692 stmt = c_begin_compound_stmt (true);
4693 c_parser_compound_statement_nostart (parser);
4695 /* If the compound stmt contains array notations, then we expand them. */
4696 if (flag_cilkplus && contains_array_notation_expr (stmt))
4697 stmt = expand_array_notation_exprs (stmt);
4698 return c_end_compound_stmt (brace_loc, stmt, true);
4701 /* Parse a compound statement except for the opening brace. This is
4702 used for parsing both compound statements and statement expressions
4703 (which follow different paths to handling the opening). */
4705 static void
4706 c_parser_compound_statement_nostart (c_parser *parser)
4708 bool last_stmt = false;
4709 bool last_label = false;
4710 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4711 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4712 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4714 c_parser_consume_token (parser);
4715 return;
4717 mark_valid_location_for_stdc_pragma (true);
4718 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4720 /* Read zero or more forward-declarations for labels that nested
4721 functions can jump to. */
4722 mark_valid_location_for_stdc_pragma (false);
4723 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4725 label_loc = c_parser_peek_token (parser)->location;
4726 c_parser_consume_token (parser);
4727 /* Any identifiers, including those declared as type names,
4728 are OK here. */
4729 while (true)
4731 tree label;
4732 if (c_parser_next_token_is_not (parser, CPP_NAME))
4734 c_parser_error (parser, "expected identifier");
4735 break;
4737 label
4738 = declare_label (c_parser_peek_token (parser)->value);
4739 C_DECLARED_LABEL_FLAG (label) = 1;
4740 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4741 c_parser_consume_token (parser);
4742 if (c_parser_next_token_is (parser, CPP_COMMA))
4743 c_parser_consume_token (parser);
4744 else
4745 break;
4747 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4749 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4751 /* We must now have at least one statement, label or declaration. */
4752 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4754 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4755 c_parser_error (parser, "expected declaration or statement");
4756 c_parser_consume_token (parser);
4757 return;
4759 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4761 location_t loc = c_parser_peek_token (parser)->location;
4762 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4763 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4764 || (c_parser_next_token_is (parser, CPP_NAME)
4765 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4767 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4768 label_loc = c_parser_peek_2nd_token (parser)->location;
4769 else
4770 label_loc = c_parser_peek_token (parser)->location;
4771 last_label = true;
4772 last_stmt = false;
4773 mark_valid_location_for_stdc_pragma (false);
4774 c_parser_label (parser);
4776 else if (!last_label
4777 && c_parser_next_tokens_start_declaration (parser))
4779 last_label = false;
4780 mark_valid_location_for_stdc_pragma (false);
4781 c_parser_declaration_or_fndef (parser, true, true, true, true,
4782 true, NULL, vNULL);
4783 if (last_stmt)
4784 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4785 "ISO C90 forbids mixed declarations and code");
4786 last_stmt = false;
4788 else if (!last_label
4789 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4791 /* __extension__ can start a declaration, but is also an
4792 unary operator that can start an expression. Consume all
4793 but the last of a possible series of __extension__ to
4794 determine which. */
4795 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4796 && (c_parser_peek_2nd_token (parser)->keyword
4797 == RID_EXTENSION))
4798 c_parser_consume_token (parser);
4799 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4801 int ext;
4802 ext = disable_extension_diagnostics ();
4803 c_parser_consume_token (parser);
4804 last_label = false;
4805 mark_valid_location_for_stdc_pragma (false);
4806 c_parser_declaration_or_fndef (parser, true, true, true, true,
4807 true, NULL, vNULL);
4808 /* Following the old parser, __extension__ does not
4809 disable this diagnostic. */
4810 restore_extension_diagnostics (ext);
4811 if (last_stmt)
4812 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4813 "ISO C90 forbids mixed declarations and code");
4814 last_stmt = false;
4816 else
4817 goto statement;
4819 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4821 /* External pragmas, and some omp pragmas, are not associated
4822 with regular c code, and so are not to be considered statements
4823 syntactically. This ensures that the user doesn't put them
4824 places that would turn into syntax errors if the directive
4825 were ignored. */
4826 if (c_parser_pragma (parser,
4827 last_label ? pragma_stmt : pragma_compound,
4828 NULL))
4829 last_label = false, last_stmt = true;
4831 else if (c_parser_next_token_is (parser, CPP_EOF))
4833 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4834 c_parser_error (parser, "expected declaration or statement");
4835 return;
4837 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4839 if (parser->in_if_block)
4841 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4842 error_at (loc, """expected %<}%> before %<else%>");
4843 return;
4845 else
4847 error_at (loc, "%<else%> without a previous %<if%>");
4848 c_parser_consume_token (parser);
4849 continue;
4852 else
4854 statement:
4855 last_label = false;
4856 last_stmt = true;
4857 mark_valid_location_for_stdc_pragma (false);
4858 c_parser_statement_after_labels (parser, NULL);
4861 parser->error = false;
4863 if (last_label)
4864 error_at (label_loc, "label at end of compound statement");
4865 c_parser_consume_token (parser);
4866 /* Restore the value we started with. */
4867 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4870 /* Parse all consecutive labels. */
4872 static void
4873 c_parser_all_labels (c_parser *parser)
4875 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4876 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4877 || (c_parser_next_token_is (parser, CPP_NAME)
4878 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4879 c_parser_label (parser);
4882 /* Parse a label (C90 6.6.1, C99 6.8.1).
4884 label:
4885 identifier : attributes[opt]
4886 case constant-expression :
4887 default :
4889 GNU extensions:
4891 label:
4892 case constant-expression ... constant-expression :
4894 The use of attributes on labels is a GNU extension. The syntax in
4895 GNU C accepts any expressions without commas, non-constant
4896 expressions being rejected later. */
4898 static void
4899 c_parser_label (c_parser *parser)
4901 location_t loc1 = c_parser_peek_token (parser)->location;
4902 tree label = NULL_TREE;
4903 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4905 tree exp1, exp2;
4906 c_parser_consume_token (parser);
4907 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4908 if (c_parser_next_token_is (parser, CPP_COLON))
4910 c_parser_consume_token (parser);
4911 label = do_case (loc1, exp1, NULL_TREE);
4913 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4915 c_parser_consume_token (parser);
4916 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4917 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4918 label = do_case (loc1, exp1, exp2);
4920 else
4921 c_parser_error (parser, "expected %<:%> or %<...%>");
4923 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4925 c_parser_consume_token (parser);
4926 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4927 label = do_case (loc1, NULL_TREE, NULL_TREE);
4929 else
4931 tree name = c_parser_peek_token (parser)->value;
4932 tree tlab;
4933 tree attrs;
4934 location_t loc2 = c_parser_peek_token (parser)->location;
4935 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4936 c_parser_consume_token (parser);
4937 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4938 c_parser_consume_token (parser);
4939 attrs = c_parser_attributes (parser);
4940 tlab = define_label (loc2, name);
4941 if (tlab)
4943 decl_attributes (&tlab, attrs, 0);
4944 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4947 if (label)
4949 if (c_parser_next_tokens_start_declaration (parser))
4951 error_at (c_parser_peek_token (parser)->location,
4952 "a label can only be part of a statement and "
4953 "a declaration is not a statement");
4954 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4955 /*static_assert_ok*/ true,
4956 /*empty_ok*/ true, /*nested*/ true,
4957 /*start_attr_ok*/ true, NULL,
4958 vNULL);
4963 /* Parse a statement (C90 6.6, C99 6.8).
4965 statement:
4966 labeled-statement
4967 compound-statement
4968 expression-statement
4969 selection-statement
4970 iteration-statement
4971 jump-statement
4973 labeled-statement:
4974 label statement
4976 expression-statement:
4977 expression[opt] ;
4979 selection-statement:
4980 if-statement
4981 switch-statement
4983 iteration-statement:
4984 while-statement
4985 do-statement
4986 for-statement
4988 jump-statement:
4989 goto identifier ;
4990 continue ;
4991 break ;
4992 return expression[opt] ;
4994 GNU extensions:
4996 statement:
4997 asm-statement
4999 jump-statement:
5000 goto * expression ;
5002 Objective-C:
5004 statement:
5005 objc-throw-statement
5006 objc-try-catch-statement
5007 objc-synchronized-statement
5009 objc-throw-statement:
5010 @throw expression ;
5011 @throw ;
5013 OpenACC:
5015 statement:
5016 openacc-construct
5018 openacc-construct:
5019 parallel-construct
5020 kernels-construct
5021 data-construct
5022 loop-construct
5024 parallel-construct:
5025 parallel-directive structured-block
5027 kernels-construct:
5028 kernels-directive structured-block
5030 data-construct:
5031 data-directive structured-block
5033 loop-construct:
5034 loop-directive structured-block
5036 OpenMP:
5038 statement:
5039 openmp-construct
5041 openmp-construct:
5042 parallel-construct
5043 for-construct
5044 simd-construct
5045 for-simd-construct
5046 sections-construct
5047 single-construct
5048 parallel-for-construct
5049 parallel-for-simd-construct
5050 parallel-sections-construct
5051 master-construct
5052 critical-construct
5053 atomic-construct
5054 ordered-construct
5056 parallel-construct:
5057 parallel-directive structured-block
5059 for-construct:
5060 for-directive iteration-statement
5062 simd-construct:
5063 simd-directive iteration-statements
5065 for-simd-construct:
5066 for-simd-directive iteration-statements
5068 sections-construct:
5069 sections-directive section-scope
5071 single-construct:
5072 single-directive structured-block
5074 parallel-for-construct:
5075 parallel-for-directive iteration-statement
5077 parallel-for-simd-construct:
5078 parallel-for-simd-directive iteration-statement
5080 parallel-sections-construct:
5081 parallel-sections-directive section-scope
5083 master-construct:
5084 master-directive structured-block
5086 critical-construct:
5087 critical-directive structured-block
5089 atomic-construct:
5090 atomic-directive expression-statement
5092 ordered-construct:
5093 ordered-directive structured-block
5095 Transactional Memory:
5097 statement:
5098 transaction-statement
5099 transaction-cancel-statement
5101 IF_P is used to track whether there's a (possibly labeled) if statement
5102 which is not enclosed in braces and has an else clause. This is used to
5103 implement -Wparentheses. */
5105 static void
5106 c_parser_statement (c_parser *parser, bool *if_p)
5108 c_parser_all_labels (parser);
5109 c_parser_statement_after_labels (parser, if_p, NULL);
5112 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5113 of if-else-if conditions.
5115 IF_P is used to track whether there's a (possibly labeled) if statement
5116 which is not enclosed in braces and has an else clause. This is used to
5117 implement -Wparentheses. */
5119 static void
5120 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5121 vec<tree> *chain)
5123 location_t loc = c_parser_peek_token (parser)->location;
5124 tree stmt = NULL_TREE;
5125 bool in_if_block = parser->in_if_block;
5126 parser->in_if_block = false;
5127 if (if_p != NULL)
5128 *if_p = false;
5129 switch (c_parser_peek_token (parser)->type)
5131 case CPP_OPEN_BRACE:
5132 add_stmt (c_parser_compound_statement (parser));
5133 break;
5134 case CPP_KEYWORD:
5135 switch (c_parser_peek_token (parser)->keyword)
5137 case RID_IF:
5138 c_parser_if_statement (parser, if_p, chain);
5139 break;
5140 case RID_SWITCH:
5141 c_parser_switch_statement (parser);
5142 break;
5143 case RID_WHILE:
5144 c_parser_while_statement (parser, false, if_p);
5145 break;
5146 case RID_DO:
5147 c_parser_do_statement (parser, false);
5148 break;
5149 case RID_FOR:
5150 c_parser_for_statement (parser, false, if_p);
5151 break;
5152 case RID_CILK_FOR:
5153 if (!flag_cilkplus)
5155 error_at (c_parser_peek_token (parser)->location,
5156 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5157 c_parser_skip_to_end_of_block_or_statement (parser);
5159 else
5160 c_parser_cilk_for (parser, integer_zero_node, if_p);
5161 break;
5162 case RID_CILK_SYNC:
5163 c_parser_consume_token (parser);
5164 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5165 if (!flag_cilkplus)
5166 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5167 else
5168 add_stmt (build_cilk_sync ());
5169 break;
5170 case RID_GOTO:
5171 c_parser_consume_token (parser);
5172 if (c_parser_next_token_is (parser, CPP_NAME))
5174 stmt = c_finish_goto_label (loc,
5175 c_parser_peek_token (parser)->value);
5176 c_parser_consume_token (parser);
5178 else if (c_parser_next_token_is (parser, CPP_MULT))
5180 struct c_expr val;
5182 c_parser_consume_token (parser);
5183 val = c_parser_expression (parser);
5184 if (check_no_cilk (val.value,
5185 "Cilk array notation cannot be used as a computed goto expression",
5186 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5187 loc))
5188 val.value = error_mark_node;
5189 val = convert_lvalue_to_rvalue (loc, val, false, true);
5190 stmt = c_finish_goto_ptr (loc, val.value);
5192 else
5193 c_parser_error (parser, "expected identifier or %<*%>");
5194 goto expect_semicolon;
5195 case RID_CONTINUE:
5196 c_parser_consume_token (parser);
5197 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5198 goto expect_semicolon;
5199 case RID_BREAK:
5200 c_parser_consume_token (parser);
5201 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5202 goto expect_semicolon;
5203 case RID_RETURN:
5204 c_parser_consume_token (parser);
5205 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5207 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5208 c_parser_consume_token (parser);
5210 else
5212 location_t xloc = c_parser_peek_token (parser)->location;
5213 struct c_expr expr = c_parser_expression_conv (parser);
5214 mark_exp_read (expr.value);
5215 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5216 expr.value, expr.original_type);
5217 goto expect_semicolon;
5219 break;
5220 case RID_ASM:
5221 stmt = c_parser_asm_statement (parser);
5222 break;
5223 case RID_TRANSACTION_ATOMIC:
5224 case RID_TRANSACTION_RELAXED:
5225 stmt = c_parser_transaction (parser,
5226 c_parser_peek_token (parser)->keyword);
5227 break;
5228 case RID_TRANSACTION_CANCEL:
5229 stmt = c_parser_transaction_cancel (parser);
5230 goto expect_semicolon;
5231 case RID_AT_THROW:
5232 gcc_assert (c_dialect_objc ());
5233 c_parser_consume_token (parser);
5234 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5236 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5237 c_parser_consume_token (parser);
5239 else
5241 struct c_expr expr = c_parser_expression (parser);
5242 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5243 if (check_no_cilk (expr.value,
5244 "Cilk array notation cannot be used for a throw expression",
5245 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5246 expr.value = error_mark_node;
5247 else
5249 expr.value = c_fully_fold (expr.value, false, NULL);
5250 stmt = objc_build_throw_stmt (loc, expr.value);
5252 goto expect_semicolon;
5254 break;
5255 case RID_AT_TRY:
5256 gcc_assert (c_dialect_objc ());
5257 c_parser_objc_try_catch_finally_statement (parser);
5258 break;
5259 case RID_AT_SYNCHRONIZED:
5260 gcc_assert (c_dialect_objc ());
5261 c_parser_objc_synchronized_statement (parser);
5262 break;
5263 default:
5264 goto expr_stmt;
5266 break;
5267 case CPP_SEMICOLON:
5268 c_parser_consume_token (parser);
5269 break;
5270 case CPP_CLOSE_PAREN:
5271 case CPP_CLOSE_SQUARE:
5272 /* Avoid infinite loop in error recovery:
5273 c_parser_skip_until_found stops at a closing nesting
5274 delimiter without consuming it, but here we need to consume
5275 it to proceed further. */
5276 c_parser_error (parser, "expected statement");
5277 c_parser_consume_token (parser);
5278 break;
5279 case CPP_PRAGMA:
5280 c_parser_pragma (parser, pragma_stmt, if_p);
5281 break;
5282 default:
5283 expr_stmt:
5284 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5285 expect_semicolon:
5286 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5287 break;
5289 /* Two cases cannot and do not have line numbers associated: If stmt
5290 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5291 cannot hold line numbers. But that's OK because the statement
5292 will either be changed to a MODIFY_EXPR during gimplification of
5293 the statement expr, or discarded. If stmt was compound, but
5294 without new variables, we will have skipped the creation of a
5295 BIND and will have a bare STATEMENT_LIST. But that's OK because
5296 (recursively) all of the component statements should already have
5297 line numbers assigned. ??? Can we discard no-op statements
5298 earlier? */
5299 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5300 protected_set_expr_location (stmt, loc);
5302 parser->in_if_block = in_if_block;
5305 /* Parse the condition from an if, do, while or for statements. */
5307 static tree
5308 c_parser_condition (c_parser *parser)
5310 location_t loc = c_parser_peek_token (parser)->location;
5311 tree cond;
5312 cond = c_parser_expression_conv (parser).value;
5313 cond = c_objc_common_truthvalue_conversion (loc, cond);
5314 cond = c_fully_fold (cond, false, NULL);
5315 if (warn_sequence_point)
5316 verify_sequence_points (cond);
5317 return cond;
5320 /* Parse a parenthesized condition from an if, do or while statement.
5322 condition:
5323 ( expression )
5325 static tree
5326 c_parser_paren_condition (c_parser *parser)
5328 tree cond;
5329 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5330 return error_mark_node;
5331 cond = c_parser_condition (parser);
5332 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5333 return cond;
5336 /* Parse a statement which is a block in C99.
5338 IF_P is used to track whether there's a (possibly labeled) if statement
5339 which is not enclosed in braces and has an else clause. This is used to
5340 implement -Wparentheses. */
5342 static tree
5343 c_parser_c99_block_statement (c_parser *parser, bool *if_p)
5345 tree block = c_begin_compound_stmt (flag_isoc99);
5346 location_t loc = c_parser_peek_token (parser)->location;
5347 c_parser_statement (parser, if_p);
5348 return c_end_compound_stmt (loc, block, flag_isoc99);
5351 /* Parse the body of an if statement. This is just parsing a
5352 statement but (a) it is a block in C99, (b) we track whether the
5353 body is an if statement for the sake of -Wparentheses warnings, (c)
5354 we handle an empty body specially for the sake of -Wempty-body
5355 warnings, and (d) we call parser_compound_statement directly
5356 because c_parser_statement_after_labels resets
5357 parser->in_if_block.
5359 IF_P is used to track whether there's a (possibly labeled) if statement
5360 which is not enclosed in braces and has an else clause. This is used to
5361 implement -Wparentheses. */
5363 static tree
5364 c_parser_if_body (c_parser *parser, bool *if_p,
5365 const token_indent_info &if_tinfo)
5367 tree block = c_begin_compound_stmt (flag_isoc99);
5368 location_t body_loc = c_parser_peek_token (parser)->location;
5369 token_indent_info body_tinfo
5370 = get_token_indent_info (c_parser_peek_token (parser));
5372 c_parser_all_labels (parser);
5373 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5375 location_t loc = c_parser_peek_token (parser)->location;
5376 add_stmt (build_empty_stmt (loc));
5377 c_parser_consume_token (parser);
5378 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5379 warning_at (loc, OPT_Wempty_body,
5380 "suggest braces around empty body in an %<if%> statement");
5382 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5383 add_stmt (c_parser_compound_statement (parser));
5384 else
5385 c_parser_statement_after_labels (parser, if_p);
5387 token_indent_info next_tinfo
5388 = get_token_indent_info (c_parser_peek_token (parser));
5389 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5391 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5394 /* Parse the else body of an if statement. This is just parsing a
5395 statement but (a) it is a block in C99, (b) we handle an empty body
5396 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5397 of if-else-if conditions. */
5399 static tree
5400 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5401 vec<tree> *chain)
5403 location_t body_loc = c_parser_peek_token (parser)->location;
5404 tree block = c_begin_compound_stmt (flag_isoc99);
5405 token_indent_info body_tinfo
5406 = get_token_indent_info (c_parser_peek_token (parser));
5408 c_parser_all_labels (parser);
5409 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5411 location_t loc = c_parser_peek_token (parser)->location;
5412 warning_at (loc,
5413 OPT_Wempty_body,
5414 "suggest braces around empty body in an %<else%> statement");
5415 add_stmt (build_empty_stmt (loc));
5416 c_parser_consume_token (parser);
5418 else
5419 c_parser_statement_after_labels (parser, NULL, chain);
5421 token_indent_info next_tinfo
5422 = get_token_indent_info (c_parser_peek_token (parser));
5423 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5425 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5428 /* We might need to reclassify any previously-lexed identifier, e.g.
5429 when we've left a for loop with an if-statement without else in the
5430 body - we might have used a wrong scope for the token. See PR67784. */
5432 static void
5433 c_parser_maybe_reclassify_token (c_parser *parser)
5435 if (c_parser_next_token_is (parser, CPP_NAME))
5437 c_token *token = c_parser_peek_token (parser);
5439 if (token->id_kind != C_ID_CLASSNAME)
5441 tree decl = lookup_name (token->value);
5443 token->id_kind = C_ID_ID;
5444 if (decl)
5446 if (TREE_CODE (decl) == TYPE_DECL)
5447 token->id_kind = C_ID_TYPENAME;
5449 else if (c_dialect_objc ())
5451 tree objc_interface_decl = objc_is_class_name (token->value);
5452 /* Objective-C class names are in the same namespace as
5453 variables and typedefs, and hence are shadowed by local
5454 declarations. */
5455 if (objc_interface_decl)
5457 token->value = objc_interface_decl;
5458 token->id_kind = C_ID_CLASSNAME;
5465 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5467 if-statement:
5468 if ( expression ) statement
5469 if ( expression ) statement else statement
5471 CHAIN is a vector of if-else-if conditions.
5472 IF_P is used to track whether there's a (possibly labeled) if statement
5473 which is not enclosed in braces and has an else clause. This is used to
5474 implement -Wparentheses. */
5476 static void
5477 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5479 tree block;
5480 location_t loc;
5481 tree cond;
5482 bool nested_if = false;
5483 tree first_body, second_body;
5484 bool in_if_block;
5485 tree if_stmt;
5487 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5488 token_indent_info if_tinfo
5489 = get_token_indent_info (c_parser_peek_token (parser));
5490 c_parser_consume_token (parser);
5491 block = c_begin_compound_stmt (flag_isoc99);
5492 loc = c_parser_peek_token (parser)->location;
5493 cond = c_parser_paren_condition (parser);
5494 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5496 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5497 cond = error_mark_node;
5499 in_if_block = parser->in_if_block;
5500 parser->in_if_block = true;
5501 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5502 parser->in_if_block = in_if_block;
5504 if (warn_duplicated_cond)
5505 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5507 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5509 token_indent_info else_tinfo
5510 = get_token_indent_info (c_parser_peek_token (parser));
5511 c_parser_consume_token (parser);
5512 if (warn_duplicated_cond)
5514 if (c_parser_next_token_is_keyword (parser, RID_IF)
5515 && chain == NULL)
5517 /* We've got "if (COND) else if (COND2)". Start the
5518 condition chain and add COND as the first element. */
5519 chain = new vec<tree> ();
5520 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5521 chain->safe_push (cond);
5523 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5525 /* This is if-else without subsequent if. Zap the condition
5526 chain; we would have already warned at this point. */
5527 delete chain;
5528 chain = NULL;
5531 second_body = c_parser_else_body (parser, else_tinfo, chain);
5532 /* Set IF_P to true to indicate that this if statement has an
5533 else clause. This may trigger the Wparentheses warning
5534 below when we get back up to the parent if statement. */
5535 if (if_p != NULL)
5536 *if_p = true;
5538 else
5540 second_body = NULL_TREE;
5542 /* Diagnose an ambiguous else if if-then-else is nested inside
5543 if-then. */
5544 if (nested_if)
5545 warning_at (loc, OPT_Wparentheses,
5546 "suggest explicit braces to avoid ambiguous %<else%>");
5548 if (warn_duplicated_cond)
5550 /* This if statement does not have an else clause. We don't
5551 need the condition chain anymore. */
5552 delete chain;
5553 chain = NULL;
5556 c_finish_if_stmt (loc, cond, first_body, second_body);
5557 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5559 /* If the if statement contains array notations, then we expand them. */
5560 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5561 if_stmt = fix_conditional_array_notations (if_stmt);
5562 add_stmt (if_stmt);
5563 c_parser_maybe_reclassify_token (parser);
5566 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5568 switch-statement:
5569 switch (expression) statement
5572 static void
5573 c_parser_switch_statement (c_parser *parser)
5575 struct c_expr ce;
5576 tree block, expr, body, save_break;
5577 location_t switch_loc = c_parser_peek_token (parser)->location;
5578 location_t switch_cond_loc;
5579 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5580 c_parser_consume_token (parser);
5581 block = c_begin_compound_stmt (flag_isoc99);
5582 bool explicit_cast_p = false;
5583 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5585 switch_cond_loc = c_parser_peek_token (parser)->location;
5586 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5587 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5588 explicit_cast_p = true;
5589 ce = c_parser_expression (parser);
5590 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5591 expr = ce.value;
5592 /* ??? expr has no valid location? */
5593 if (check_no_cilk (expr,
5594 "Cilk array notation cannot be used as a condition for switch statement",
5595 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5596 switch_cond_loc))
5597 expr = error_mark_node;
5598 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5600 else
5602 switch_cond_loc = UNKNOWN_LOCATION;
5603 expr = error_mark_node;
5605 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5606 save_break = c_break_label;
5607 c_break_label = NULL_TREE;
5608 body = c_parser_c99_block_statement (parser, NULL/*if??*/);
5609 c_finish_case (body, ce.original_type);
5610 if (c_break_label)
5612 location_t here = c_parser_peek_token (parser)->location;
5613 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5614 SET_EXPR_LOCATION (t, here);
5615 add_stmt (t);
5617 c_break_label = save_break;
5618 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5619 c_parser_maybe_reclassify_token (parser);
5622 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5624 while-statement:
5625 while (expression) statement
5627 IF_P is used to track whether there's a (possibly labeled) if statement
5628 which is not enclosed in braces and has an else clause. This is used to
5629 implement -Wparentheses. */
5631 static void
5632 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5634 tree block, cond, body, save_break, save_cont;
5635 location_t loc;
5636 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5637 token_indent_info while_tinfo
5638 = get_token_indent_info (c_parser_peek_token (parser));
5639 c_parser_consume_token (parser);
5640 block = c_begin_compound_stmt (flag_isoc99);
5641 loc = c_parser_peek_token (parser)->location;
5642 cond = c_parser_paren_condition (parser);
5643 if (check_no_cilk (cond,
5644 "Cilk array notation cannot be used as a condition for while statement",
5645 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5646 cond = error_mark_node;
5647 if (ivdep && cond != error_mark_node)
5648 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5649 build_int_cst (integer_type_node,
5650 annot_expr_ivdep_kind));
5651 save_break = c_break_label;
5652 c_break_label = NULL_TREE;
5653 save_cont = c_cont_label;
5654 c_cont_label = NULL_TREE;
5656 token_indent_info body_tinfo
5657 = get_token_indent_info (c_parser_peek_token (parser));
5659 body = c_parser_c99_block_statement (parser, if_p);
5660 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5661 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5662 c_parser_maybe_reclassify_token (parser);
5664 token_indent_info next_tinfo
5665 = get_token_indent_info (c_parser_peek_token (parser));
5666 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5668 c_break_label = save_break;
5669 c_cont_label = save_cont;
5672 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5674 do-statement:
5675 do statement while ( expression ) ;
5678 static void
5679 c_parser_do_statement (c_parser *parser, bool ivdep)
5681 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5682 location_t loc;
5683 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5684 c_parser_consume_token (parser);
5685 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5686 warning_at (c_parser_peek_token (parser)->location,
5687 OPT_Wempty_body,
5688 "suggest braces around empty body in %<do%> statement");
5689 block = c_begin_compound_stmt (flag_isoc99);
5690 loc = c_parser_peek_token (parser)->location;
5691 save_break = c_break_label;
5692 c_break_label = NULL_TREE;
5693 save_cont = c_cont_label;
5694 c_cont_label = NULL_TREE;
5695 body = c_parser_c99_block_statement (parser, NULL);
5696 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5697 new_break = c_break_label;
5698 c_break_label = save_break;
5699 new_cont = c_cont_label;
5700 c_cont_label = save_cont;
5701 cond = c_parser_paren_condition (parser);
5702 if (check_no_cilk (cond,
5703 "Cilk array notation cannot be used as a condition for a do-while statement",
5704 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5705 cond = error_mark_node;
5706 if (ivdep && cond != error_mark_node)
5707 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5708 build_int_cst (integer_type_node,
5709 annot_expr_ivdep_kind));
5710 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5711 c_parser_skip_to_end_of_block_or_statement (parser);
5712 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5713 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5716 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5718 for-statement:
5719 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5720 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5722 The form with a declaration is new in C99.
5724 ??? In accordance with the old parser, the declaration may be a
5725 nested function, which is then rejected in check_for_loop_decls,
5726 but does it make any sense for this to be included in the grammar?
5727 Note in particular that the nested function does not include a
5728 trailing ';', whereas the "declaration" production includes one.
5729 Also, can we reject bad declarations earlier and cheaper than
5730 check_for_loop_decls?
5732 In Objective-C, there are two additional variants:
5734 foreach-statement:
5735 for ( expression in expresssion ) statement
5736 for ( declaration in expression ) statement
5738 This is inconsistent with C, because the second variant is allowed
5739 even if c99 is not enabled.
5741 The rest of the comment documents these Objective-C foreach-statement.
5743 Here is the canonical example of the first variant:
5744 for (object in array) { do something with object }
5745 we call the first expression ("object") the "object_expression" and
5746 the second expression ("array") the "collection_expression".
5747 object_expression must be an lvalue of type "id" (a generic Objective-C
5748 object) because the loop works by assigning to object_expression the
5749 various objects from the collection_expression. collection_expression
5750 must evaluate to something of type "id" which responds to the method
5751 countByEnumeratingWithState:objects:count:.
5753 The canonical example of the second variant is:
5754 for (id object in array) { do something with object }
5755 which is completely equivalent to
5757 id object;
5758 for (object in array) { do something with object }
5760 Note that initizializing 'object' in some way (eg, "for ((object =
5761 xxx) in array) { do something with object }") is possibly
5762 technically valid, but completely pointless as 'object' will be
5763 assigned to something else as soon as the loop starts. We should
5764 most likely reject it (TODO).
5766 The beginning of the Objective-C foreach-statement looks exactly
5767 like the beginning of the for-statement, and we can tell it is a
5768 foreach-statement only because the initial declaration or
5769 expression is terminated by 'in' instead of ';'.
5771 IF_P is used to track whether there's a (possibly labeled) if statement
5772 which is not enclosed in braces and has an else clause. This is used to
5773 implement -Wparentheses. */
5775 static void
5776 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
5778 tree block, cond, incr, save_break, save_cont, body;
5779 /* The following are only used when parsing an ObjC foreach statement. */
5780 tree object_expression;
5781 /* Silence the bogus uninitialized warning. */
5782 tree collection_expression = NULL;
5783 location_t loc = c_parser_peek_token (parser)->location;
5784 location_t for_loc = c_parser_peek_token (parser)->location;
5785 bool is_foreach_statement = false;
5786 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5787 token_indent_info for_tinfo
5788 = get_token_indent_info (c_parser_peek_token (parser));
5789 c_parser_consume_token (parser);
5790 /* Open a compound statement in Objective-C as well, just in case this is
5791 as foreach expression. */
5792 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5793 cond = error_mark_node;
5794 incr = error_mark_node;
5795 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5797 /* Parse the initialization declaration or expression. */
5798 object_expression = error_mark_node;
5799 parser->objc_could_be_foreach_context = c_dialect_objc ();
5800 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5802 parser->objc_could_be_foreach_context = false;
5803 c_parser_consume_token (parser);
5804 c_finish_expr_stmt (loc, NULL_TREE);
5806 else if (c_parser_next_tokens_start_declaration (parser))
5808 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5809 &object_expression, vNULL);
5810 parser->objc_could_be_foreach_context = false;
5812 if (c_parser_next_token_is_keyword (parser, RID_IN))
5814 c_parser_consume_token (parser);
5815 is_foreach_statement = true;
5816 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5817 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5819 else
5820 check_for_loop_decls (for_loc, flag_isoc99);
5822 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5824 /* __extension__ can start a declaration, but is also an
5825 unary operator that can start an expression. Consume all
5826 but the last of a possible series of __extension__ to
5827 determine which. */
5828 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5829 && (c_parser_peek_2nd_token (parser)->keyword
5830 == RID_EXTENSION))
5831 c_parser_consume_token (parser);
5832 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5834 int ext;
5835 ext = disable_extension_diagnostics ();
5836 c_parser_consume_token (parser);
5837 c_parser_declaration_or_fndef (parser, true, true, true, true,
5838 true, &object_expression, vNULL);
5839 parser->objc_could_be_foreach_context = false;
5841 restore_extension_diagnostics (ext);
5842 if (c_parser_next_token_is_keyword (parser, RID_IN))
5844 c_parser_consume_token (parser);
5845 is_foreach_statement = true;
5846 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5847 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5849 else
5850 check_for_loop_decls (for_loc, flag_isoc99);
5852 else
5853 goto init_expr;
5855 else
5857 init_expr:
5859 struct c_expr ce;
5860 tree init_expression;
5861 ce = c_parser_expression (parser);
5862 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5863 level statement", but it works just fine, so allow it. */
5864 init_expression = ce.value;
5865 parser->objc_could_be_foreach_context = false;
5866 if (c_parser_next_token_is_keyword (parser, RID_IN))
5868 c_parser_consume_token (parser);
5869 is_foreach_statement = true;
5870 if (! lvalue_p (init_expression))
5871 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5872 object_expression = c_fully_fold (init_expression, false, NULL);
5874 else
5876 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5877 init_expression = ce.value;
5878 c_finish_expr_stmt (loc, init_expression);
5879 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5883 /* Parse the loop condition. In the case of a foreach
5884 statement, there is no loop condition. */
5885 gcc_assert (!parser->objc_could_be_foreach_context);
5886 if (!is_foreach_statement)
5888 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5890 if (ivdep)
5892 c_parser_error (parser, "missing loop condition in loop with "
5893 "%<GCC ivdep%> pragma");
5894 cond = error_mark_node;
5896 else
5898 c_parser_consume_token (parser);
5899 cond = NULL_TREE;
5902 else
5904 cond = c_parser_condition (parser);
5905 if (check_no_cilk (cond,
5906 "Cilk array notation cannot be used in a condition for a for-loop",
5907 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5908 cond = error_mark_node;
5909 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5910 "expected %<;%>");
5912 if (ivdep && cond != error_mark_node)
5913 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5914 build_int_cst (integer_type_node,
5915 annot_expr_ivdep_kind));
5917 /* Parse the increment expression (the third expression in a
5918 for-statement). In the case of a foreach-statement, this is
5919 the expression that follows the 'in'. */
5920 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5922 if (is_foreach_statement)
5924 c_parser_error (parser, "missing collection in fast enumeration");
5925 collection_expression = error_mark_node;
5927 else
5928 incr = c_process_expr_stmt (loc, NULL_TREE);
5930 else
5932 if (is_foreach_statement)
5933 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5934 false, NULL);
5935 else
5937 struct c_expr ce = c_parser_expression (parser);
5938 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5939 incr = c_process_expr_stmt (loc, ce.value);
5942 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5944 save_break = c_break_label;
5945 c_break_label = NULL_TREE;
5946 save_cont = c_cont_label;
5947 c_cont_label = NULL_TREE;
5949 token_indent_info body_tinfo
5950 = get_token_indent_info (c_parser_peek_token (parser));
5952 body = c_parser_c99_block_statement (parser, if_p);
5954 if (is_foreach_statement)
5955 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5956 else
5957 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5958 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5959 c_parser_maybe_reclassify_token (parser);
5961 token_indent_info next_tinfo
5962 = get_token_indent_info (c_parser_peek_token (parser));
5963 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
5965 c_break_label = save_break;
5966 c_cont_label = save_cont;
5969 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5970 statement with inputs, outputs, clobbers, and volatile tag
5971 allowed.
5973 asm-statement:
5974 asm type-qualifier[opt] ( asm-argument ) ;
5975 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5977 asm-argument:
5978 asm-string-literal
5979 asm-string-literal : asm-operands[opt]
5980 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5981 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5983 asm-goto-argument:
5984 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5985 : asm-goto-operands
5987 Qualifiers other than volatile are accepted in the syntax but
5988 warned for. */
5990 static tree
5991 c_parser_asm_statement (c_parser *parser)
5993 tree quals, str, outputs, inputs, clobbers, labels, ret;
5994 bool simple, is_goto;
5995 location_t asm_loc = c_parser_peek_token (parser)->location;
5996 int section, nsections;
5998 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5999 c_parser_consume_token (parser);
6000 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6002 quals = c_parser_peek_token (parser)->value;
6003 c_parser_consume_token (parser);
6005 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6006 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6008 warning_at (c_parser_peek_token (parser)->location,
6010 "%E qualifier ignored on asm",
6011 c_parser_peek_token (parser)->value);
6012 quals = NULL_TREE;
6013 c_parser_consume_token (parser);
6015 else
6016 quals = NULL_TREE;
6018 is_goto = false;
6019 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6021 c_parser_consume_token (parser);
6022 is_goto = true;
6025 /* ??? Follow the C++ parser rather than using the
6026 lex_untranslated_string kludge. */
6027 parser->lex_untranslated_string = true;
6028 ret = NULL;
6030 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6031 goto error;
6033 str = c_parser_asm_string_literal (parser);
6034 if (str == NULL_TREE)
6035 goto error_close_paren;
6037 simple = true;
6038 outputs = NULL_TREE;
6039 inputs = NULL_TREE;
6040 clobbers = NULL_TREE;
6041 labels = NULL_TREE;
6043 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6044 goto done_asm;
6046 /* Parse each colon-delimited section of operands. */
6047 nsections = 3 + is_goto;
6048 for (section = 0; section < nsections; ++section)
6050 if (!c_parser_require (parser, CPP_COLON,
6051 is_goto
6052 ? "expected %<:%>"
6053 : "expected %<:%> or %<)%>"))
6054 goto error_close_paren;
6056 /* Once past any colon, we're no longer a simple asm. */
6057 simple = false;
6059 if ((!c_parser_next_token_is (parser, CPP_COLON)
6060 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6061 || section == 3)
6062 switch (section)
6064 case 0:
6065 /* For asm goto, we don't allow output operands, but reserve
6066 the slot for a future extension that does allow them. */
6067 if (!is_goto)
6068 outputs = c_parser_asm_operands (parser);
6069 break;
6070 case 1:
6071 inputs = c_parser_asm_operands (parser);
6072 break;
6073 case 2:
6074 clobbers = c_parser_asm_clobbers (parser);
6075 break;
6076 case 3:
6077 labels = c_parser_asm_goto_operands (parser);
6078 break;
6079 default:
6080 gcc_unreachable ();
6083 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6084 goto done_asm;
6087 done_asm:
6088 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6090 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6091 goto error;
6094 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6095 c_parser_skip_to_end_of_block_or_statement (parser);
6097 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6098 clobbers, labels, simple));
6100 error:
6101 parser->lex_untranslated_string = false;
6102 return ret;
6104 error_close_paren:
6105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6106 goto error;
6109 /* Parse asm operands, a GNU extension.
6111 asm-operands:
6112 asm-operand
6113 asm-operands , asm-operand
6115 asm-operand:
6116 asm-string-literal ( expression )
6117 [ identifier ] asm-string-literal ( expression )
6120 static tree
6121 c_parser_asm_operands (c_parser *parser)
6123 tree list = NULL_TREE;
6124 while (true)
6126 tree name, str;
6127 struct c_expr expr;
6128 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6130 c_parser_consume_token (parser);
6131 if (c_parser_next_token_is (parser, CPP_NAME))
6133 tree id = c_parser_peek_token (parser)->value;
6134 c_parser_consume_token (parser);
6135 name = build_string (IDENTIFIER_LENGTH (id),
6136 IDENTIFIER_POINTER (id));
6138 else
6140 c_parser_error (parser, "expected identifier");
6141 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6142 return NULL_TREE;
6144 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6145 "expected %<]%>");
6147 else
6148 name = NULL_TREE;
6149 str = c_parser_asm_string_literal (parser);
6150 if (str == NULL_TREE)
6151 return NULL_TREE;
6152 parser->lex_untranslated_string = false;
6153 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6155 parser->lex_untranslated_string = true;
6156 return NULL_TREE;
6158 expr = c_parser_expression (parser);
6159 mark_exp_read (expr.value);
6160 parser->lex_untranslated_string = true;
6161 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6163 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6164 return NULL_TREE;
6166 list = chainon (list, build_tree_list (build_tree_list (name, str),
6167 expr.value));
6168 if (c_parser_next_token_is (parser, CPP_COMMA))
6169 c_parser_consume_token (parser);
6170 else
6171 break;
6173 return list;
6176 /* Parse asm clobbers, a GNU extension.
6178 asm-clobbers:
6179 asm-string-literal
6180 asm-clobbers , asm-string-literal
6183 static tree
6184 c_parser_asm_clobbers (c_parser *parser)
6186 tree list = NULL_TREE;
6187 while (true)
6189 tree str = c_parser_asm_string_literal (parser);
6190 if (str)
6191 list = tree_cons (NULL_TREE, str, list);
6192 else
6193 return NULL_TREE;
6194 if (c_parser_next_token_is (parser, CPP_COMMA))
6195 c_parser_consume_token (parser);
6196 else
6197 break;
6199 return list;
6202 /* Parse asm goto labels, a GNU extension.
6204 asm-goto-operands:
6205 identifier
6206 asm-goto-operands , identifier
6209 static tree
6210 c_parser_asm_goto_operands (c_parser *parser)
6212 tree list = NULL_TREE;
6213 while (true)
6215 tree name, label;
6217 if (c_parser_next_token_is (parser, CPP_NAME))
6219 c_token *tok = c_parser_peek_token (parser);
6220 name = tok->value;
6221 label = lookup_label_for_goto (tok->location, name);
6222 c_parser_consume_token (parser);
6223 TREE_USED (label) = 1;
6225 else
6227 c_parser_error (parser, "expected identifier");
6228 return NULL_TREE;
6231 name = build_string (IDENTIFIER_LENGTH (name),
6232 IDENTIFIER_POINTER (name));
6233 list = tree_cons (name, label, list);
6234 if (c_parser_next_token_is (parser, CPP_COMMA))
6235 c_parser_consume_token (parser);
6236 else
6237 return nreverse (list);
6241 /* Parse an expression other than a compound expression; that is, an
6242 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6243 NULL then it is an Objective-C message expression which is the
6244 primary-expression starting the expression as an initializer.
6246 assignment-expression:
6247 conditional-expression
6248 unary-expression assignment-operator assignment-expression
6250 assignment-operator: one of
6251 = *= /= %= += -= <<= >>= &= ^= |=
6253 In GNU C we accept any conditional expression on the LHS and
6254 diagnose the invalid lvalue rather than producing a syntax
6255 error. */
6257 static struct c_expr
6258 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6259 tree omp_atomic_lhs)
6261 struct c_expr lhs, rhs, ret;
6262 enum tree_code code;
6263 location_t op_location, exp_location;
6264 gcc_assert (!after || c_dialect_objc ());
6265 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6266 op_location = c_parser_peek_token (parser)->location;
6267 switch (c_parser_peek_token (parser)->type)
6269 case CPP_EQ:
6270 code = NOP_EXPR;
6271 break;
6272 case CPP_MULT_EQ:
6273 code = MULT_EXPR;
6274 break;
6275 case CPP_DIV_EQ:
6276 code = TRUNC_DIV_EXPR;
6277 break;
6278 case CPP_MOD_EQ:
6279 code = TRUNC_MOD_EXPR;
6280 break;
6281 case CPP_PLUS_EQ:
6282 code = PLUS_EXPR;
6283 break;
6284 case CPP_MINUS_EQ:
6285 code = MINUS_EXPR;
6286 break;
6287 case CPP_LSHIFT_EQ:
6288 code = LSHIFT_EXPR;
6289 break;
6290 case CPP_RSHIFT_EQ:
6291 code = RSHIFT_EXPR;
6292 break;
6293 case CPP_AND_EQ:
6294 code = BIT_AND_EXPR;
6295 break;
6296 case CPP_XOR_EQ:
6297 code = BIT_XOR_EXPR;
6298 break;
6299 case CPP_OR_EQ:
6300 code = BIT_IOR_EXPR;
6301 break;
6302 default:
6303 return lhs;
6305 c_parser_consume_token (parser);
6306 exp_location = c_parser_peek_token (parser)->location;
6307 rhs = c_parser_expr_no_commas (parser, NULL);
6308 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6310 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6311 code, exp_location, rhs.value,
6312 rhs.original_type);
6313 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6314 if (code == NOP_EXPR)
6315 ret.original_code = MODIFY_EXPR;
6316 else
6318 TREE_NO_WARNING (ret.value) = 1;
6319 ret.original_code = ERROR_MARK;
6321 ret.original_type = NULL;
6322 return ret;
6325 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6326 is not NULL then it is an Objective-C message expression which is
6327 the primary-expression starting the expression as an initializer.
6329 conditional-expression:
6330 logical-OR-expression
6331 logical-OR-expression ? expression : conditional-expression
6333 GNU extensions:
6335 conditional-expression:
6336 logical-OR-expression ? : conditional-expression
6339 static struct c_expr
6340 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6341 tree omp_atomic_lhs)
6343 struct c_expr cond, exp1, exp2, ret;
6344 location_t start, cond_loc, colon_loc, middle_loc;
6346 gcc_assert (!after || c_dialect_objc ());
6348 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6350 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6351 return cond;
6352 if (cond.value != error_mark_node)
6353 start = cond.get_start ();
6354 else
6355 start = UNKNOWN_LOCATION;
6356 cond_loc = c_parser_peek_token (parser)->location;
6357 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6358 c_parser_consume_token (parser);
6359 if (c_parser_next_token_is (parser, CPP_COLON))
6361 tree eptype = NULL_TREE;
6363 middle_loc = c_parser_peek_token (parser)->location;
6364 pedwarn (middle_loc, OPT_Wpedantic,
6365 "ISO C forbids omitting the middle term of a ?: expression");
6366 warn_for_omitted_condop (middle_loc, cond.value);
6367 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6369 eptype = TREE_TYPE (cond.value);
6370 cond.value = TREE_OPERAND (cond.value, 0);
6372 /* Make sure first operand is calculated only once. */
6373 exp1.value = c_save_expr (default_conversion (cond.value));
6374 if (eptype)
6375 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6376 exp1.original_type = NULL;
6377 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6378 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6380 else
6382 cond.value
6383 = c_objc_common_truthvalue_conversion
6384 (cond_loc, default_conversion (cond.value));
6385 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6386 exp1 = c_parser_expression_conv (parser);
6387 mark_exp_read (exp1.value);
6388 c_inhibit_evaluation_warnings +=
6389 ((cond.value == truthvalue_true_node)
6390 - (cond.value == truthvalue_false_node));
6393 colon_loc = c_parser_peek_token (parser)->location;
6394 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6396 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6397 ret.value = error_mark_node;
6398 ret.original_code = ERROR_MARK;
6399 ret.original_type = NULL;
6400 return ret;
6403 location_t exp2_loc = c_parser_peek_token (parser)->location;
6404 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6405 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6407 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6408 ret.value = build_conditional_expr (colon_loc, cond.value,
6409 cond.original_code == C_MAYBE_CONST_EXPR,
6410 exp1.value, exp1.original_type,
6411 exp2.value, exp2.original_type);
6412 ret.original_code = ERROR_MARK;
6413 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6414 ret.original_type = NULL;
6415 else
6417 tree t1, t2;
6419 /* If both sides are enum type, the default conversion will have
6420 made the type of the result be an integer type. We want to
6421 remember the enum types we started with. */
6422 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6423 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6424 ret.original_type = ((t1 != error_mark_node
6425 && t2 != error_mark_node
6426 && (TYPE_MAIN_VARIANT (t1)
6427 == TYPE_MAIN_VARIANT (t2)))
6428 ? t1
6429 : NULL);
6431 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6432 return ret;
6435 /* Parse a binary expression; that is, a logical-OR-expression (C90
6436 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6437 an Objective-C message expression which is the primary-expression
6438 starting the expression as an initializer.
6440 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6441 when it should be the unfolded lhs. In a valid OpenMP source,
6442 one of the operands of the toplevel binary expression must be equal
6443 to it. In that case, just return a build2 created binary operation
6444 rather than result of parser_build_binary_op.
6446 multiplicative-expression:
6447 cast-expression
6448 multiplicative-expression * cast-expression
6449 multiplicative-expression / cast-expression
6450 multiplicative-expression % cast-expression
6452 additive-expression:
6453 multiplicative-expression
6454 additive-expression + multiplicative-expression
6455 additive-expression - multiplicative-expression
6457 shift-expression:
6458 additive-expression
6459 shift-expression << additive-expression
6460 shift-expression >> additive-expression
6462 relational-expression:
6463 shift-expression
6464 relational-expression < shift-expression
6465 relational-expression > shift-expression
6466 relational-expression <= shift-expression
6467 relational-expression >= shift-expression
6469 equality-expression:
6470 relational-expression
6471 equality-expression == relational-expression
6472 equality-expression != relational-expression
6474 AND-expression:
6475 equality-expression
6476 AND-expression & equality-expression
6478 exclusive-OR-expression:
6479 AND-expression
6480 exclusive-OR-expression ^ AND-expression
6482 inclusive-OR-expression:
6483 exclusive-OR-expression
6484 inclusive-OR-expression | exclusive-OR-expression
6486 logical-AND-expression:
6487 inclusive-OR-expression
6488 logical-AND-expression && inclusive-OR-expression
6490 logical-OR-expression:
6491 logical-AND-expression
6492 logical-OR-expression || logical-AND-expression
6495 static struct c_expr
6496 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6497 tree omp_atomic_lhs)
6499 /* A binary expression is parsed using operator-precedence parsing,
6500 with the operands being cast expressions. All the binary
6501 operators are left-associative. Thus a binary expression is of
6502 form:
6504 E0 op1 E1 op2 E2 ...
6506 which we represent on a stack. On the stack, the precedence
6507 levels are strictly increasing. When a new operator is
6508 encountered of higher precedence than that at the top of the
6509 stack, it is pushed; its LHS is the top expression, and its RHS
6510 is everything parsed until it is popped. When a new operator is
6511 encountered with precedence less than or equal to that at the top
6512 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6513 by the result of the operation until the operator at the top of
6514 the stack has lower precedence than the new operator or there is
6515 only one element on the stack; then the top expression is the LHS
6516 of the new operator. In the case of logical AND and OR
6517 expressions, we also need to adjust c_inhibit_evaluation_warnings
6518 as appropriate when the operators are pushed and popped. */
6520 struct {
6521 /* The expression at this stack level. */
6522 struct c_expr expr;
6523 /* The precedence of the operator on its left, PREC_NONE at the
6524 bottom of the stack. */
6525 enum c_parser_prec prec;
6526 /* The operation on its left. */
6527 enum tree_code op;
6528 /* The source location of this operation. */
6529 location_t loc;
6530 } stack[NUM_PRECS];
6531 int sp;
6532 /* Location of the binary operator. */
6533 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6534 #define POP \
6535 do { \
6536 switch (stack[sp].op) \
6538 case TRUTH_ANDIF_EXPR: \
6539 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6540 == truthvalue_false_node); \
6541 break; \
6542 case TRUTH_ORIF_EXPR: \
6543 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6544 == truthvalue_true_node); \
6545 break; \
6546 default: \
6547 break; \
6549 stack[sp - 1].expr \
6550 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6551 stack[sp - 1].expr, true, true); \
6552 stack[sp].expr \
6553 = convert_lvalue_to_rvalue (stack[sp].loc, \
6554 stack[sp].expr, true, true); \
6555 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6556 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6557 && ((1 << stack[sp].prec) \
6558 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6559 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6560 && stack[sp].op != TRUNC_MOD_EXPR \
6561 && stack[0].expr.value != error_mark_node \
6562 && stack[1].expr.value != error_mark_node \
6563 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6564 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6565 stack[0].expr.value \
6566 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6567 stack[0].expr.value, stack[1].expr.value); \
6568 else \
6569 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6570 stack[sp].op, \
6571 stack[sp - 1].expr, \
6572 stack[sp].expr); \
6573 sp--; \
6574 } while (0)
6575 gcc_assert (!after || c_dialect_objc ());
6576 stack[0].loc = c_parser_peek_token (parser)->location;
6577 stack[0].expr = c_parser_cast_expression (parser, after);
6578 stack[0].prec = PREC_NONE;
6579 sp = 0;
6580 while (true)
6582 enum c_parser_prec oprec;
6583 enum tree_code ocode;
6584 source_range src_range;
6585 if (parser->error)
6586 goto out;
6587 switch (c_parser_peek_token (parser)->type)
6589 case CPP_MULT:
6590 oprec = PREC_MULT;
6591 ocode = MULT_EXPR;
6592 break;
6593 case CPP_DIV:
6594 oprec = PREC_MULT;
6595 ocode = TRUNC_DIV_EXPR;
6596 break;
6597 case CPP_MOD:
6598 oprec = PREC_MULT;
6599 ocode = TRUNC_MOD_EXPR;
6600 break;
6601 case CPP_PLUS:
6602 oprec = PREC_ADD;
6603 ocode = PLUS_EXPR;
6604 break;
6605 case CPP_MINUS:
6606 oprec = PREC_ADD;
6607 ocode = MINUS_EXPR;
6608 break;
6609 case CPP_LSHIFT:
6610 oprec = PREC_SHIFT;
6611 ocode = LSHIFT_EXPR;
6612 break;
6613 case CPP_RSHIFT:
6614 oprec = PREC_SHIFT;
6615 ocode = RSHIFT_EXPR;
6616 break;
6617 case CPP_LESS:
6618 oprec = PREC_REL;
6619 ocode = LT_EXPR;
6620 break;
6621 case CPP_GREATER:
6622 oprec = PREC_REL;
6623 ocode = GT_EXPR;
6624 break;
6625 case CPP_LESS_EQ:
6626 oprec = PREC_REL;
6627 ocode = LE_EXPR;
6628 break;
6629 case CPP_GREATER_EQ:
6630 oprec = PREC_REL;
6631 ocode = GE_EXPR;
6632 break;
6633 case CPP_EQ_EQ:
6634 oprec = PREC_EQ;
6635 ocode = EQ_EXPR;
6636 break;
6637 case CPP_NOT_EQ:
6638 oprec = PREC_EQ;
6639 ocode = NE_EXPR;
6640 break;
6641 case CPP_AND:
6642 oprec = PREC_BITAND;
6643 ocode = BIT_AND_EXPR;
6644 break;
6645 case CPP_XOR:
6646 oprec = PREC_BITXOR;
6647 ocode = BIT_XOR_EXPR;
6648 break;
6649 case CPP_OR:
6650 oprec = PREC_BITOR;
6651 ocode = BIT_IOR_EXPR;
6652 break;
6653 case CPP_AND_AND:
6654 oprec = PREC_LOGAND;
6655 ocode = TRUTH_ANDIF_EXPR;
6656 break;
6657 case CPP_OR_OR:
6658 oprec = PREC_LOGOR;
6659 ocode = TRUTH_ORIF_EXPR;
6660 break;
6661 default:
6662 /* Not a binary operator, so end of the binary
6663 expression. */
6664 goto out;
6666 binary_loc = c_parser_peek_token (parser)->location;
6667 while (oprec <= stack[sp].prec)
6668 POP;
6669 c_parser_consume_token (parser);
6670 switch (ocode)
6672 case TRUTH_ANDIF_EXPR:
6673 src_range = stack[sp].expr.src_range;
6674 stack[sp].expr
6675 = convert_lvalue_to_rvalue (stack[sp].loc,
6676 stack[sp].expr, true, true);
6677 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6678 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6679 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6680 == truthvalue_false_node);
6681 set_c_expr_source_range (&stack[sp].expr, src_range);
6682 break;
6683 case TRUTH_ORIF_EXPR:
6684 src_range = stack[sp].expr.src_range;
6685 stack[sp].expr
6686 = convert_lvalue_to_rvalue (stack[sp].loc,
6687 stack[sp].expr, true, true);
6688 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6689 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6690 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6691 == truthvalue_true_node);
6692 set_c_expr_source_range (&stack[sp].expr, src_range);
6693 break;
6694 default:
6695 break;
6697 sp++;
6698 stack[sp].loc = binary_loc;
6699 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6700 stack[sp].prec = oprec;
6701 stack[sp].op = ocode;
6703 out:
6704 while (sp > 0)
6705 POP;
6706 return stack[0].expr;
6707 #undef POP
6710 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6711 NULL then it is an Objective-C message expression which is the
6712 primary-expression starting the expression as an initializer.
6714 cast-expression:
6715 unary-expression
6716 ( type-name ) unary-expression
6719 static struct c_expr
6720 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6722 location_t cast_loc = c_parser_peek_token (parser)->location;
6723 gcc_assert (!after || c_dialect_objc ());
6724 if (after)
6725 return c_parser_postfix_expression_after_primary (parser,
6726 cast_loc, *after);
6727 /* If the expression begins with a parenthesized type name, it may
6728 be either a cast or a compound literal; we need to see whether
6729 the next character is '{' to tell the difference. If not, it is
6730 an unary expression. Full detection of unknown typenames here
6731 would require a 3-token lookahead. */
6732 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6733 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6735 struct c_type_name *type_name;
6736 struct c_expr ret;
6737 struct c_expr expr;
6738 c_parser_consume_token (parser);
6739 type_name = c_parser_type_name (parser);
6740 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6741 if (type_name == NULL)
6743 ret.value = error_mark_node;
6744 ret.original_code = ERROR_MARK;
6745 ret.original_type = NULL;
6746 return ret;
6749 /* Save casted types in the function's used types hash table. */
6750 used_types_insert (type_name->specs->type);
6752 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6753 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6754 cast_loc);
6756 location_t expr_loc = c_parser_peek_token (parser)->location;
6757 expr = c_parser_cast_expression (parser, NULL);
6758 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6760 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6761 if (ret.value && expr.value)
6762 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6763 ret.original_code = ERROR_MARK;
6764 ret.original_type = NULL;
6765 return ret;
6767 else
6768 return c_parser_unary_expression (parser);
6771 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6773 unary-expression:
6774 postfix-expression
6775 ++ unary-expression
6776 -- unary-expression
6777 unary-operator cast-expression
6778 sizeof unary-expression
6779 sizeof ( type-name )
6781 unary-operator: one of
6782 & * + - ~ !
6784 GNU extensions:
6786 unary-expression:
6787 __alignof__ unary-expression
6788 __alignof__ ( type-name )
6789 && identifier
6791 (C11 permits _Alignof with type names only.)
6793 unary-operator: one of
6794 __extension__ __real__ __imag__
6796 Transactional Memory:
6798 unary-expression:
6799 transaction-expression
6801 In addition, the GNU syntax treats ++ and -- as unary operators, so
6802 they may be applied to cast expressions with errors for non-lvalues
6803 given later. */
6805 static struct c_expr
6806 c_parser_unary_expression (c_parser *parser)
6808 int ext;
6809 struct c_expr ret, op;
6810 location_t op_loc = c_parser_peek_token (parser)->location;
6811 location_t exp_loc;
6812 location_t finish;
6813 ret.original_code = ERROR_MARK;
6814 ret.original_type = NULL;
6815 switch (c_parser_peek_token (parser)->type)
6817 case CPP_PLUS_PLUS:
6818 c_parser_consume_token (parser);
6819 exp_loc = c_parser_peek_token (parser)->location;
6820 op = c_parser_cast_expression (parser, NULL);
6822 /* If there is array notations in op, we expand them. */
6823 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6824 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6825 else
6827 op = default_function_array_read_conversion (exp_loc, op);
6828 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6830 case CPP_MINUS_MINUS:
6831 c_parser_consume_token (parser);
6832 exp_loc = c_parser_peek_token (parser)->location;
6833 op = c_parser_cast_expression (parser, NULL);
6835 /* If there is array notations in op, we expand them. */
6836 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6837 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6838 else
6840 op = default_function_array_read_conversion (exp_loc, op);
6841 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6843 case CPP_AND:
6844 c_parser_consume_token (parser);
6845 op = c_parser_cast_expression (parser, NULL);
6846 mark_exp_read (op.value);
6847 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6848 case CPP_MULT:
6850 c_parser_consume_token (parser);
6851 exp_loc = c_parser_peek_token (parser)->location;
6852 op = c_parser_cast_expression (parser, NULL);
6853 finish = op.get_finish ();
6854 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6855 location_t combined_loc = make_location (op_loc, op_loc, finish);
6856 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
6857 ret.src_range.m_start = op_loc;
6858 ret.src_range.m_finish = finish;
6859 return ret;
6861 case CPP_PLUS:
6862 if (!c_dialect_objc () && !in_system_header_at (input_location))
6863 warning_at (op_loc,
6864 OPT_Wtraditional,
6865 "traditional C rejects the unary plus operator");
6866 c_parser_consume_token (parser);
6867 exp_loc = c_parser_peek_token (parser)->location;
6868 op = c_parser_cast_expression (parser, NULL);
6869 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6870 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6871 case CPP_MINUS:
6872 c_parser_consume_token (parser);
6873 exp_loc = c_parser_peek_token (parser)->location;
6874 op = c_parser_cast_expression (parser, NULL);
6875 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6876 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6877 case CPP_COMPL:
6878 c_parser_consume_token (parser);
6879 exp_loc = c_parser_peek_token (parser)->location;
6880 op = c_parser_cast_expression (parser, NULL);
6881 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6882 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6883 case CPP_NOT:
6884 c_parser_consume_token (parser);
6885 exp_loc = c_parser_peek_token (parser)->location;
6886 op = c_parser_cast_expression (parser, NULL);
6887 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6888 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6889 case CPP_AND_AND:
6890 /* Refer to the address of a label as a pointer. */
6891 c_parser_consume_token (parser);
6892 if (c_parser_next_token_is (parser, CPP_NAME))
6894 ret.value = finish_label_address_expr
6895 (c_parser_peek_token (parser)->value, op_loc);
6896 set_c_expr_source_range (&ret, op_loc,
6897 c_parser_peek_token (parser)->get_finish ());
6898 c_parser_consume_token (parser);
6900 else
6902 c_parser_error (parser, "expected identifier");
6903 ret.value = error_mark_node;
6905 return ret;
6906 case CPP_KEYWORD:
6907 switch (c_parser_peek_token (parser)->keyword)
6909 case RID_SIZEOF:
6910 return c_parser_sizeof_expression (parser);
6911 case RID_ALIGNOF:
6912 return c_parser_alignof_expression (parser);
6913 case RID_EXTENSION:
6914 c_parser_consume_token (parser);
6915 ext = disable_extension_diagnostics ();
6916 ret = c_parser_cast_expression (parser, NULL);
6917 restore_extension_diagnostics (ext);
6918 return ret;
6919 case RID_REALPART:
6920 c_parser_consume_token (parser);
6921 exp_loc = c_parser_peek_token (parser)->location;
6922 op = c_parser_cast_expression (parser, NULL);
6923 op = default_function_array_conversion (exp_loc, op);
6924 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6925 case RID_IMAGPART:
6926 c_parser_consume_token (parser);
6927 exp_loc = c_parser_peek_token (parser)->location;
6928 op = c_parser_cast_expression (parser, NULL);
6929 op = default_function_array_conversion (exp_loc, op);
6930 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6931 case RID_TRANSACTION_ATOMIC:
6932 case RID_TRANSACTION_RELAXED:
6933 return c_parser_transaction_expression (parser,
6934 c_parser_peek_token (parser)->keyword);
6935 default:
6936 return c_parser_postfix_expression (parser);
6938 default:
6939 return c_parser_postfix_expression (parser);
6943 /* Parse a sizeof expression. */
6945 static struct c_expr
6946 c_parser_sizeof_expression (c_parser *parser)
6948 struct c_expr expr;
6949 struct c_expr result;
6950 location_t expr_loc;
6951 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6953 location_t start;
6954 location_t finish = UNKNOWN_LOCATION;
6956 start = c_parser_peek_token (parser)->location;
6958 c_parser_consume_token (parser);
6959 c_inhibit_evaluation_warnings++;
6960 in_sizeof++;
6961 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6962 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6964 /* Either sizeof ( type-name ) or sizeof unary-expression
6965 starting with a compound literal. */
6966 struct c_type_name *type_name;
6967 c_parser_consume_token (parser);
6968 expr_loc = c_parser_peek_token (parser)->location;
6969 type_name = c_parser_type_name (parser);
6970 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6971 finish = parser->tokens_buf[0].location;
6972 if (type_name == NULL)
6974 struct c_expr ret;
6975 c_inhibit_evaluation_warnings--;
6976 in_sizeof--;
6977 ret.value = error_mark_node;
6978 ret.original_code = ERROR_MARK;
6979 ret.original_type = NULL;
6980 return ret;
6982 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6984 expr = c_parser_postfix_expression_after_paren_type (parser,
6985 type_name,
6986 expr_loc);
6987 finish = expr.get_finish ();
6988 goto sizeof_expr;
6990 /* sizeof ( type-name ). */
6991 c_inhibit_evaluation_warnings--;
6992 in_sizeof--;
6993 result = c_expr_sizeof_type (expr_loc, type_name);
6995 else
6997 expr_loc = c_parser_peek_token (parser)->location;
6998 expr = c_parser_unary_expression (parser);
6999 finish = expr.get_finish ();
7000 sizeof_expr:
7001 c_inhibit_evaluation_warnings--;
7002 in_sizeof--;
7003 mark_exp_read (expr.value);
7004 if (TREE_CODE (expr.value) == COMPONENT_REF
7005 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7006 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7007 result = c_expr_sizeof_expr (expr_loc, expr);
7009 if (finish != UNKNOWN_LOCATION)
7010 set_c_expr_source_range (&result, start, finish);
7011 return result;
7014 /* Parse an alignof expression. */
7016 static struct c_expr
7017 c_parser_alignof_expression (c_parser *parser)
7019 struct c_expr expr;
7020 location_t start_loc = c_parser_peek_token (parser)->location;
7021 location_t end_loc;
7022 tree alignof_spelling = c_parser_peek_token (parser)->value;
7023 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7024 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7025 "_Alignof") == 0;
7026 /* A diagnostic is not required for the use of this identifier in
7027 the implementation namespace; only diagnose it for the C11
7028 spelling because of existing code using the other spellings. */
7029 if (is_c11_alignof)
7031 if (flag_isoc99)
7032 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7033 alignof_spelling);
7034 else
7035 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7036 alignof_spelling);
7038 c_parser_consume_token (parser);
7039 c_inhibit_evaluation_warnings++;
7040 in_alignof++;
7041 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7042 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7044 /* Either __alignof__ ( type-name ) or __alignof__
7045 unary-expression starting with a compound literal. */
7046 location_t loc;
7047 struct c_type_name *type_name;
7048 struct c_expr ret;
7049 c_parser_consume_token (parser);
7050 loc = c_parser_peek_token (parser)->location;
7051 type_name = c_parser_type_name (parser);
7052 end_loc = c_parser_peek_token (parser)->location;
7053 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7054 if (type_name == NULL)
7056 struct c_expr ret;
7057 c_inhibit_evaluation_warnings--;
7058 in_alignof--;
7059 ret.value = error_mark_node;
7060 ret.original_code = ERROR_MARK;
7061 ret.original_type = NULL;
7062 return ret;
7064 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7066 expr = c_parser_postfix_expression_after_paren_type (parser,
7067 type_name,
7068 loc);
7069 goto alignof_expr;
7071 /* alignof ( type-name ). */
7072 c_inhibit_evaluation_warnings--;
7073 in_alignof--;
7074 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7075 NULL, NULL),
7076 false, is_c11_alignof, 1);
7077 ret.original_code = ERROR_MARK;
7078 ret.original_type = NULL;
7079 set_c_expr_source_range (&ret, start_loc, end_loc);
7080 return ret;
7082 else
7084 struct c_expr ret;
7085 expr = c_parser_unary_expression (parser);
7086 end_loc = expr.src_range.m_finish;
7087 alignof_expr:
7088 mark_exp_read (expr.value);
7089 c_inhibit_evaluation_warnings--;
7090 in_alignof--;
7091 pedwarn (start_loc,
7092 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7093 alignof_spelling);
7094 ret.value = c_alignof_expr (start_loc, expr.value);
7095 ret.original_code = ERROR_MARK;
7096 ret.original_type = NULL;
7097 set_c_expr_source_range (&ret, start_loc, end_loc);
7098 return ret;
7102 /* Helper function to read arguments of builtins which are interfaces
7103 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7104 others. The name of the builtin is passed using BNAME parameter.
7105 Function returns true if there were no errors while parsing and
7106 stores the arguments in CEXPR_LIST. If it returns true,
7107 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7108 parenthesis. */
7109 static bool
7110 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7111 vec<c_expr_t, va_gc> **ret_cexpr_list,
7112 bool choose_expr_p,
7113 location_t *out_close_paren_loc)
7115 location_t loc = c_parser_peek_token (parser)->location;
7116 vec<c_expr_t, va_gc> *cexpr_list;
7117 c_expr_t expr;
7118 bool saved_force_folding_builtin_constant_p;
7120 *ret_cexpr_list = NULL;
7121 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7123 error_at (loc, "cannot take address of %qs", bname);
7124 return false;
7127 c_parser_consume_token (parser);
7129 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7131 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7132 c_parser_consume_token (parser);
7133 return true;
7136 saved_force_folding_builtin_constant_p
7137 = force_folding_builtin_constant_p;
7138 force_folding_builtin_constant_p |= choose_expr_p;
7139 expr = c_parser_expr_no_commas (parser, NULL);
7140 force_folding_builtin_constant_p
7141 = saved_force_folding_builtin_constant_p;
7142 vec_alloc (cexpr_list, 1);
7143 vec_safe_push (cexpr_list, expr);
7144 while (c_parser_next_token_is (parser, CPP_COMMA))
7146 c_parser_consume_token (parser);
7147 expr = c_parser_expr_no_commas (parser, NULL);
7148 vec_safe_push (cexpr_list, expr);
7151 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7152 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7153 return false;
7155 *ret_cexpr_list = cexpr_list;
7156 return true;
7159 /* This represents a single generic-association. */
7161 struct c_generic_association
7163 /* The location of the starting token of the type. */
7164 location_t type_location;
7165 /* The association's type, or NULL_TREE for 'default'. */
7166 tree type;
7167 /* The association's expression. */
7168 struct c_expr expression;
7171 /* Parse a generic-selection. (C11 6.5.1.1).
7173 generic-selection:
7174 _Generic ( assignment-expression , generic-assoc-list )
7176 generic-assoc-list:
7177 generic-association
7178 generic-assoc-list , generic-association
7180 generic-association:
7181 type-name : assignment-expression
7182 default : assignment-expression
7185 static struct c_expr
7186 c_parser_generic_selection (c_parser *parser)
7188 vec<c_generic_association> associations = vNULL;
7189 struct c_expr selector, error_expr;
7190 tree selector_type;
7191 struct c_generic_association matched_assoc;
7192 bool match_found = false;
7193 location_t generic_loc, selector_loc;
7195 error_expr.original_code = ERROR_MARK;
7196 error_expr.original_type = NULL;
7197 error_expr.value = error_mark_node;
7198 matched_assoc.type_location = UNKNOWN_LOCATION;
7199 matched_assoc.type = NULL_TREE;
7200 matched_assoc.expression = error_expr;
7202 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7203 generic_loc = c_parser_peek_token (parser)->location;
7204 c_parser_consume_token (parser);
7205 if (flag_isoc99)
7206 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7207 "ISO C99 does not support %<_Generic%>");
7208 else
7209 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7210 "ISO C90 does not support %<_Generic%>");
7212 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7213 return error_expr;
7215 c_inhibit_evaluation_warnings++;
7216 selector_loc = c_parser_peek_token (parser)->location;
7217 selector = c_parser_expr_no_commas (parser, NULL);
7218 selector = default_function_array_conversion (selector_loc, selector);
7219 c_inhibit_evaluation_warnings--;
7221 if (selector.value == error_mark_node)
7223 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7224 return selector;
7226 selector_type = TREE_TYPE (selector.value);
7227 /* In ISO C terms, rvalues (including the controlling expression of
7228 _Generic) do not have qualified types. */
7229 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7230 selector_type = TYPE_MAIN_VARIANT (selector_type);
7231 /* In ISO C terms, _Noreturn is not part of the type of expressions
7232 such as &abort, but in GCC it is represented internally as a type
7233 qualifier. */
7234 if (FUNCTION_POINTER_TYPE_P (selector_type)
7235 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7236 selector_type
7237 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7239 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7241 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7242 return error_expr;
7245 while (1)
7247 struct c_generic_association assoc, *iter;
7248 unsigned int ix;
7249 c_token *token = c_parser_peek_token (parser);
7251 assoc.type_location = token->location;
7252 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7254 c_parser_consume_token (parser);
7255 assoc.type = NULL_TREE;
7257 else
7259 struct c_type_name *type_name;
7261 type_name = c_parser_type_name (parser);
7262 if (type_name == NULL)
7264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7265 goto error_exit;
7267 assoc.type = groktypename (type_name, NULL, NULL);
7268 if (assoc.type == error_mark_node)
7270 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7271 goto error_exit;
7274 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7275 error_at (assoc.type_location,
7276 "%<_Generic%> association has function type");
7277 else if (!COMPLETE_TYPE_P (assoc.type))
7278 error_at (assoc.type_location,
7279 "%<_Generic%> association has incomplete type");
7281 if (variably_modified_type_p (assoc.type, NULL_TREE))
7282 error_at (assoc.type_location,
7283 "%<_Generic%> association has "
7284 "variable length type");
7287 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7289 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7290 goto error_exit;
7293 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7294 if (assoc.expression.value == error_mark_node)
7296 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7297 goto error_exit;
7300 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7302 if (assoc.type == NULL_TREE)
7304 if (iter->type == NULL_TREE)
7306 error_at (assoc.type_location,
7307 "duplicate %<default%> case in %<_Generic%>");
7308 inform (iter->type_location, "original %<default%> is here");
7311 else if (iter->type != NULL_TREE)
7313 if (comptypes (assoc.type, iter->type))
7315 error_at (assoc.type_location,
7316 "%<_Generic%> specifies two compatible types");
7317 inform (iter->type_location, "compatible type is here");
7322 if (assoc.type == NULL_TREE)
7324 if (!match_found)
7326 matched_assoc = assoc;
7327 match_found = true;
7330 else if (comptypes (assoc.type, selector_type))
7332 if (!match_found || matched_assoc.type == NULL_TREE)
7334 matched_assoc = assoc;
7335 match_found = true;
7337 else
7339 error_at (assoc.type_location,
7340 "%<_Generic> selector matches multiple associations");
7341 inform (matched_assoc.type_location,
7342 "other match is here");
7346 associations.safe_push (assoc);
7348 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7349 break;
7350 c_parser_consume_token (parser);
7353 associations.release ();
7355 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7357 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7358 return error_expr;
7361 if (!match_found)
7363 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7364 "compatible with any association",
7365 selector_type);
7366 return error_expr;
7369 return matched_assoc.expression;
7371 error_exit:
7372 associations.release ();
7373 return error_expr;
7376 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7378 postfix-expression:
7379 primary-expression
7380 postfix-expression [ expression ]
7381 postfix-expression ( argument-expression-list[opt] )
7382 postfix-expression . identifier
7383 postfix-expression -> identifier
7384 postfix-expression ++
7385 postfix-expression --
7386 ( type-name ) { initializer-list }
7387 ( type-name ) { initializer-list , }
7389 argument-expression-list:
7390 argument-expression
7391 argument-expression-list , argument-expression
7393 primary-expression:
7394 identifier
7395 constant
7396 string-literal
7397 ( expression )
7398 generic-selection
7400 GNU extensions:
7402 primary-expression:
7403 __func__
7404 (treated as a keyword in GNU C)
7405 __FUNCTION__
7406 __PRETTY_FUNCTION__
7407 ( compound-statement )
7408 __builtin_va_arg ( assignment-expression , type-name )
7409 __builtin_offsetof ( type-name , offsetof-member-designator )
7410 __builtin_choose_expr ( assignment-expression ,
7411 assignment-expression ,
7412 assignment-expression )
7413 __builtin_types_compatible_p ( type-name , type-name )
7414 __builtin_complex ( assignment-expression , assignment-expression )
7415 __builtin_shuffle ( assignment-expression , assignment-expression )
7416 __builtin_shuffle ( assignment-expression ,
7417 assignment-expression ,
7418 assignment-expression, )
7420 offsetof-member-designator:
7421 identifier
7422 offsetof-member-designator . identifier
7423 offsetof-member-designator [ expression ]
7425 Objective-C:
7427 primary-expression:
7428 [ objc-receiver objc-message-args ]
7429 @selector ( objc-selector-arg )
7430 @protocol ( identifier )
7431 @encode ( type-name )
7432 objc-string-literal
7433 Classname . identifier
7436 static struct c_expr
7437 c_parser_postfix_expression (c_parser *parser)
7439 struct c_expr expr, e1;
7440 struct c_type_name *t1, *t2;
7441 location_t loc = c_parser_peek_token (parser)->location;;
7442 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7443 expr.original_code = ERROR_MARK;
7444 expr.original_type = NULL;
7445 switch (c_parser_peek_token (parser)->type)
7447 case CPP_NUMBER:
7448 expr.value = c_parser_peek_token (parser)->value;
7449 set_c_expr_source_range (&expr, tok_range);
7450 loc = c_parser_peek_token (parser)->location;
7451 c_parser_consume_token (parser);
7452 if (TREE_CODE (expr.value) == FIXED_CST
7453 && !targetm.fixed_point_supported_p ())
7455 error_at (loc, "fixed-point types not supported for this target");
7456 expr.value = error_mark_node;
7458 break;
7459 case CPP_CHAR:
7460 case CPP_CHAR16:
7461 case CPP_CHAR32:
7462 case CPP_WCHAR:
7463 expr.value = c_parser_peek_token (parser)->value;
7464 set_c_expr_source_range (&expr, tok_range);
7465 c_parser_consume_token (parser);
7466 break;
7467 case CPP_STRING:
7468 case CPP_STRING16:
7469 case CPP_STRING32:
7470 case CPP_WSTRING:
7471 case CPP_UTF8STRING:
7472 expr.value = c_parser_peek_token (parser)->value;
7473 set_c_expr_source_range (&expr, tok_range);
7474 expr.original_code = STRING_CST;
7475 c_parser_consume_token (parser);
7476 break;
7477 case CPP_OBJC_STRING:
7478 gcc_assert (c_dialect_objc ());
7479 expr.value
7480 = objc_build_string_object (c_parser_peek_token (parser)->value);
7481 set_c_expr_source_range (&expr, tok_range);
7482 c_parser_consume_token (parser);
7483 break;
7484 case CPP_NAME:
7485 switch (c_parser_peek_token (parser)->id_kind)
7487 case C_ID_ID:
7489 tree id = c_parser_peek_token (parser)->value;
7490 c_parser_consume_token (parser);
7491 expr.value = build_external_ref (loc, id,
7492 (c_parser_peek_token (parser)->type
7493 == CPP_OPEN_PAREN),
7494 &expr.original_type);
7495 set_c_expr_source_range (&expr, tok_range);
7496 break;
7498 case C_ID_CLASSNAME:
7500 /* Here we parse the Objective-C 2.0 Class.name dot
7501 syntax. */
7502 tree class_name = c_parser_peek_token (parser)->value;
7503 tree component;
7504 c_parser_consume_token (parser);
7505 gcc_assert (c_dialect_objc ());
7506 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7508 expr.value = error_mark_node;
7509 break;
7511 if (c_parser_next_token_is_not (parser, CPP_NAME))
7513 c_parser_error (parser, "expected identifier");
7514 expr.value = error_mark_node;
7515 break;
7517 c_token *component_tok = c_parser_peek_token (parser);
7518 component = component_tok->value;
7519 location_t end_loc = component_tok->get_finish ();
7520 c_parser_consume_token (parser);
7521 expr.value = objc_build_class_component_ref (class_name,
7522 component);
7523 set_c_expr_source_range (&expr, loc, end_loc);
7524 break;
7526 default:
7527 c_parser_error (parser, "expected expression");
7528 expr.value = error_mark_node;
7529 break;
7531 break;
7532 case CPP_OPEN_PAREN:
7533 /* A parenthesized expression, statement expression or compound
7534 literal. */
7535 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7537 /* A statement expression. */
7538 tree stmt;
7539 location_t brace_loc;
7540 c_parser_consume_token (parser);
7541 brace_loc = c_parser_peek_token (parser)->location;
7542 c_parser_consume_token (parser);
7543 if (!building_stmt_list_p ())
7545 error_at (loc, "braced-group within expression allowed "
7546 "only inside a function");
7547 parser->error = true;
7548 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7549 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7550 expr.value = error_mark_node;
7551 break;
7553 stmt = c_begin_stmt_expr ();
7554 c_parser_compound_statement_nostart (parser);
7555 location_t close_loc = c_parser_peek_token (parser)->location;
7556 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7557 "expected %<)%>");
7558 pedwarn (loc, OPT_Wpedantic,
7559 "ISO C forbids braced-groups within expressions");
7560 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7561 set_c_expr_source_range (&expr, loc, close_loc);
7562 mark_exp_read (expr.value);
7564 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7566 /* A compound literal. ??? Can we actually get here rather
7567 than going directly to
7568 c_parser_postfix_expression_after_paren_type from
7569 elsewhere? */
7570 location_t loc;
7571 struct c_type_name *type_name;
7572 c_parser_consume_token (parser);
7573 loc = c_parser_peek_token (parser)->location;
7574 type_name = c_parser_type_name (parser);
7575 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7576 "expected %<)%>");
7577 if (type_name == NULL)
7579 expr.value = error_mark_node;
7581 else
7582 expr = c_parser_postfix_expression_after_paren_type (parser,
7583 type_name,
7584 loc);
7586 else
7588 /* A parenthesized expression. */
7589 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7590 c_parser_consume_token (parser);
7591 expr = c_parser_expression (parser);
7592 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7593 TREE_NO_WARNING (expr.value) = 1;
7594 if (expr.original_code != C_MAYBE_CONST_EXPR)
7595 expr.original_code = ERROR_MARK;
7596 /* Don't change EXPR.ORIGINAL_TYPE. */
7597 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7598 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7599 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7600 "expected %<)%>");
7602 break;
7603 case CPP_KEYWORD:
7604 switch (c_parser_peek_token (parser)->keyword)
7606 case RID_FUNCTION_NAME:
7607 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7608 "%<__FUNCTION__%> predefined identifier");
7609 expr.value = fname_decl (loc,
7610 c_parser_peek_token (parser)->keyword,
7611 c_parser_peek_token (parser)->value);
7612 set_c_expr_source_range (&expr, loc, loc);
7613 c_parser_consume_token (parser);
7614 break;
7615 case RID_PRETTY_FUNCTION_NAME:
7616 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7617 "%<__PRETTY_FUNCTION__%> predefined identifier");
7618 expr.value = fname_decl (loc,
7619 c_parser_peek_token (parser)->keyword,
7620 c_parser_peek_token (parser)->value);
7621 set_c_expr_source_range (&expr, loc, loc);
7622 c_parser_consume_token (parser);
7623 break;
7624 case RID_C99_FUNCTION_NAME:
7625 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7626 "%<__func__%> predefined identifier");
7627 expr.value = fname_decl (loc,
7628 c_parser_peek_token (parser)->keyword,
7629 c_parser_peek_token (parser)->value);
7630 set_c_expr_source_range (&expr, loc, loc);
7631 c_parser_consume_token (parser);
7632 break;
7633 case RID_VA_ARG:
7635 location_t start_loc = loc;
7636 c_parser_consume_token (parser);
7637 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7639 expr.value = error_mark_node;
7640 break;
7642 e1 = c_parser_expr_no_commas (parser, NULL);
7643 mark_exp_read (e1.value);
7644 e1.value = c_fully_fold (e1.value, false, NULL);
7645 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7647 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7648 expr.value = error_mark_node;
7649 break;
7651 loc = c_parser_peek_token (parser)->location;
7652 t1 = c_parser_type_name (parser);
7653 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7654 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7655 "expected %<)%>");
7656 if (t1 == NULL)
7658 expr.value = error_mark_node;
7660 else
7662 tree type_expr = NULL_TREE;
7663 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7664 groktypename (t1, &type_expr, NULL));
7665 if (type_expr)
7667 expr.value = build2 (C_MAYBE_CONST_EXPR,
7668 TREE_TYPE (expr.value), type_expr,
7669 expr.value);
7670 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7672 set_c_expr_source_range (&expr, start_loc, end_loc);
7675 break;
7676 case RID_OFFSETOF:
7677 c_parser_consume_token (parser);
7678 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7680 expr.value = error_mark_node;
7681 break;
7683 t1 = c_parser_type_name (parser);
7684 if (t1 == NULL)
7685 parser->error = true;
7686 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7687 gcc_assert (parser->error);
7688 if (parser->error)
7690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7691 expr.value = error_mark_node;
7692 break;
7696 tree type = groktypename (t1, NULL, NULL);
7697 tree offsetof_ref;
7698 if (type == error_mark_node)
7699 offsetof_ref = error_mark_node;
7700 else
7702 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7703 SET_EXPR_LOCATION (offsetof_ref, loc);
7705 /* Parse the second argument to __builtin_offsetof. We
7706 must have one identifier, and beyond that we want to
7707 accept sub structure and sub array references. */
7708 if (c_parser_next_token_is (parser, CPP_NAME))
7710 offsetof_ref = build_component_ref
7711 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7712 c_parser_consume_token (parser);
7713 while (c_parser_next_token_is (parser, CPP_DOT)
7714 || c_parser_next_token_is (parser,
7715 CPP_OPEN_SQUARE)
7716 || c_parser_next_token_is (parser,
7717 CPP_DEREF))
7719 if (c_parser_next_token_is (parser, CPP_DEREF))
7721 loc = c_parser_peek_token (parser)->location;
7722 offsetof_ref = build_array_ref (loc,
7723 offsetof_ref,
7724 integer_zero_node);
7725 goto do_dot;
7727 else if (c_parser_next_token_is (parser, CPP_DOT))
7729 do_dot:
7730 c_parser_consume_token (parser);
7731 if (c_parser_next_token_is_not (parser,
7732 CPP_NAME))
7734 c_parser_error (parser, "expected identifier");
7735 break;
7737 offsetof_ref = build_component_ref
7738 (loc, offsetof_ref,
7739 c_parser_peek_token (parser)->value);
7740 c_parser_consume_token (parser);
7742 else
7744 struct c_expr ce;
7745 tree idx;
7746 loc = c_parser_peek_token (parser)->location;
7747 c_parser_consume_token (parser);
7748 ce = c_parser_expression (parser);
7749 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7750 idx = ce.value;
7751 idx = c_fully_fold (idx, false, NULL);
7752 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7753 "expected %<]%>");
7754 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7758 else
7759 c_parser_error (parser, "expected identifier");
7760 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7762 "expected %<)%>");
7763 expr.value = fold_offsetof (offsetof_ref);
7764 set_c_expr_source_range (&expr, loc, end_loc);
7766 break;
7767 case RID_CHOOSE_EXPR:
7769 vec<c_expr_t, va_gc> *cexpr_list;
7770 c_expr_t *e1_p, *e2_p, *e3_p;
7771 tree c;
7772 location_t close_paren_loc;
7774 c_parser_consume_token (parser);
7775 if (!c_parser_get_builtin_args (parser,
7776 "__builtin_choose_expr",
7777 &cexpr_list, true,
7778 &close_paren_loc))
7780 expr.value = error_mark_node;
7781 break;
7784 if (vec_safe_length (cexpr_list) != 3)
7786 error_at (loc, "wrong number of arguments to "
7787 "%<__builtin_choose_expr%>");
7788 expr.value = error_mark_node;
7789 break;
7792 e1_p = &(*cexpr_list)[0];
7793 e2_p = &(*cexpr_list)[1];
7794 e3_p = &(*cexpr_list)[2];
7796 c = e1_p->value;
7797 mark_exp_read (e2_p->value);
7798 mark_exp_read (e3_p->value);
7799 if (TREE_CODE (c) != INTEGER_CST
7800 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7801 error_at (loc,
7802 "first argument to %<__builtin_choose_expr%> not"
7803 " a constant");
7804 constant_expression_warning (c);
7805 expr = integer_zerop (c) ? *e3_p : *e2_p;
7806 set_c_expr_source_range (&expr, loc, close_paren_loc);
7807 break;
7809 case RID_TYPES_COMPATIBLE_P:
7810 c_parser_consume_token (parser);
7811 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7813 expr.value = error_mark_node;
7814 break;
7816 t1 = c_parser_type_name (parser);
7817 if (t1 == NULL)
7819 expr.value = error_mark_node;
7820 break;
7822 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7825 expr.value = error_mark_node;
7826 break;
7828 t2 = c_parser_type_name (parser);
7829 if (t2 == NULL)
7831 expr.value = error_mark_node;
7832 break;
7835 location_t close_paren_loc = c_parser_peek_token (parser)->location;
7836 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7837 "expected %<)%>");
7838 tree e1, e2;
7839 e1 = groktypename (t1, NULL, NULL);
7840 e2 = groktypename (t2, NULL, NULL);
7841 if (e1 == error_mark_node || e2 == error_mark_node)
7843 expr.value = error_mark_node;
7844 break;
7847 e1 = TYPE_MAIN_VARIANT (e1);
7848 e2 = TYPE_MAIN_VARIANT (e2);
7850 expr.value
7851 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7852 set_c_expr_source_range (&expr, loc, close_paren_loc);
7854 break;
7855 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7857 vec<c_expr_t, va_gc> *cexpr_list;
7858 c_expr_t *e2_p;
7859 tree chain_value;
7860 location_t close_paren_loc;
7862 c_parser_consume_token (parser);
7863 if (!c_parser_get_builtin_args (parser,
7864 "__builtin_call_with_static_chain",
7865 &cexpr_list, false,
7866 &close_paren_loc))
7868 expr.value = error_mark_node;
7869 break;
7871 if (vec_safe_length (cexpr_list) != 2)
7873 error_at (loc, "wrong number of arguments to "
7874 "%<__builtin_call_with_static_chain%>");
7875 expr.value = error_mark_node;
7876 break;
7879 expr = (*cexpr_list)[0];
7880 e2_p = &(*cexpr_list)[1];
7881 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7882 chain_value = e2_p->value;
7883 mark_exp_read (chain_value);
7885 if (TREE_CODE (expr.value) != CALL_EXPR)
7886 error_at (loc, "first argument to "
7887 "%<__builtin_call_with_static_chain%> "
7888 "must be a call expression");
7889 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7890 error_at (loc, "second argument to "
7891 "%<__builtin_call_with_static_chain%> "
7892 "must be a pointer type");
7893 else
7894 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7895 set_c_expr_source_range (&expr, loc, close_paren_loc);
7896 break;
7898 case RID_BUILTIN_COMPLEX:
7900 vec<c_expr_t, va_gc> *cexpr_list;
7901 c_expr_t *e1_p, *e2_p;
7902 location_t close_paren_loc;
7904 c_parser_consume_token (parser);
7905 if (!c_parser_get_builtin_args (parser,
7906 "__builtin_complex",
7907 &cexpr_list, false,
7908 &close_paren_loc))
7910 expr.value = error_mark_node;
7911 break;
7914 if (vec_safe_length (cexpr_list) != 2)
7916 error_at (loc, "wrong number of arguments to "
7917 "%<__builtin_complex%>");
7918 expr.value = error_mark_node;
7919 break;
7922 e1_p = &(*cexpr_list)[0];
7923 e2_p = &(*cexpr_list)[1];
7925 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7926 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7927 e1_p->value = convert (TREE_TYPE (e1_p->value),
7928 TREE_OPERAND (e1_p->value, 0));
7929 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7930 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7931 e2_p->value = convert (TREE_TYPE (e2_p->value),
7932 TREE_OPERAND (e2_p->value, 0));
7933 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7934 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7935 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7936 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7938 error_at (loc, "%<__builtin_complex%> operand "
7939 "not of real binary floating-point type");
7940 expr.value = error_mark_node;
7941 break;
7943 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7944 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7946 error_at (loc,
7947 "%<__builtin_complex%> operands of different types");
7948 expr.value = error_mark_node;
7949 break;
7951 pedwarn_c90 (loc, OPT_Wpedantic,
7952 "ISO C90 does not support complex types");
7953 expr.value = build2_loc (loc, COMPLEX_EXPR,
7954 build_complex_type
7955 (TYPE_MAIN_VARIANT
7956 (TREE_TYPE (e1_p->value))),
7957 e1_p->value, e2_p->value);
7958 set_c_expr_source_range (&expr, loc, close_paren_loc);
7959 break;
7961 case RID_BUILTIN_SHUFFLE:
7963 vec<c_expr_t, va_gc> *cexpr_list;
7964 unsigned int i;
7965 c_expr_t *p;
7966 location_t close_paren_loc;
7968 c_parser_consume_token (parser);
7969 if (!c_parser_get_builtin_args (parser,
7970 "__builtin_shuffle",
7971 &cexpr_list, false,
7972 &close_paren_loc))
7974 expr.value = error_mark_node;
7975 break;
7978 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7979 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7981 if (vec_safe_length (cexpr_list) == 2)
7982 expr.value =
7983 c_build_vec_perm_expr
7984 (loc, (*cexpr_list)[0].value,
7985 NULL_TREE, (*cexpr_list)[1].value);
7987 else if (vec_safe_length (cexpr_list) == 3)
7988 expr.value =
7989 c_build_vec_perm_expr
7990 (loc, (*cexpr_list)[0].value,
7991 (*cexpr_list)[1].value,
7992 (*cexpr_list)[2].value);
7993 else
7995 error_at (loc, "wrong number of arguments to "
7996 "%<__builtin_shuffle%>");
7997 expr.value = error_mark_node;
7999 set_c_expr_source_range (&expr, loc, close_paren_loc);
8000 break;
8002 case RID_AT_SELECTOR:
8003 gcc_assert (c_dialect_objc ());
8004 c_parser_consume_token (parser);
8005 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8007 expr.value = error_mark_node;
8008 break;
8011 tree sel = c_parser_objc_selector_arg (parser);
8012 location_t close_loc = c_parser_peek_token (parser)->location;
8013 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8014 "expected %<)%>");
8015 expr.value = objc_build_selector_expr (loc, sel);
8016 set_c_expr_source_range (&expr, loc, close_loc);
8018 break;
8019 case RID_AT_PROTOCOL:
8020 gcc_assert (c_dialect_objc ());
8021 c_parser_consume_token (parser);
8022 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8024 expr.value = error_mark_node;
8025 break;
8027 if (c_parser_next_token_is_not (parser, CPP_NAME))
8029 c_parser_error (parser, "expected identifier");
8030 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8031 expr.value = error_mark_node;
8032 break;
8035 tree id = c_parser_peek_token (parser)->value;
8036 c_parser_consume_token (parser);
8037 location_t close_loc = c_parser_peek_token (parser)->location;
8038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8039 "expected %<)%>");
8040 expr.value = objc_build_protocol_expr (id);
8041 set_c_expr_source_range (&expr, loc, close_loc);
8043 break;
8044 case RID_AT_ENCODE:
8045 /* Extension to support C-structures in the archiver. */
8046 gcc_assert (c_dialect_objc ());
8047 c_parser_consume_token (parser);
8048 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8050 expr.value = error_mark_node;
8051 break;
8053 t1 = c_parser_type_name (parser);
8054 if (t1 == NULL)
8056 expr.value = error_mark_node;
8057 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8058 break;
8061 location_t close_loc = c_parser_peek_token (parser)->location;
8062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8063 "expected %<)%>");
8064 tree type = groktypename (t1, NULL, NULL);
8065 expr.value = objc_build_encode_expr (type);
8066 set_c_expr_source_range (&expr, loc, close_loc);
8068 break;
8069 case RID_GENERIC:
8070 expr = c_parser_generic_selection (parser);
8071 break;
8072 case RID_CILK_SPAWN:
8073 c_parser_consume_token (parser);
8074 if (!flag_cilkplus)
8076 error_at (loc, "-fcilkplus must be enabled to use "
8077 "%<_Cilk_spawn%>");
8078 expr = c_parser_cast_expression (parser, NULL);
8079 expr.value = error_mark_node;
8081 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8083 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
8084 "are not permitted");
8085 /* Now flush out all the _Cilk_spawns. */
8086 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8087 c_parser_consume_token (parser);
8088 expr = c_parser_cast_expression (parser, NULL);
8090 else
8092 expr = c_parser_cast_expression (parser, NULL);
8093 expr.value = build_cilk_spawn (loc, expr.value);
8095 break;
8096 default:
8097 c_parser_error (parser, "expected expression");
8098 expr.value = error_mark_node;
8099 break;
8101 break;
8102 case CPP_OPEN_SQUARE:
8103 if (c_dialect_objc ())
8105 tree receiver, args;
8106 c_parser_consume_token (parser);
8107 receiver = c_parser_objc_receiver (parser);
8108 args = c_parser_objc_message_args (parser);
8109 location_t close_loc = c_parser_peek_token (parser)->location;
8110 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8111 "expected %<]%>");
8112 expr.value = objc_build_message_expr (receiver, args);
8113 set_c_expr_source_range (&expr, loc, close_loc);
8114 break;
8116 /* Else fall through to report error. */
8117 default:
8118 c_parser_error (parser, "expected expression");
8119 expr.value = error_mark_node;
8120 break;
8122 return c_parser_postfix_expression_after_primary
8123 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8126 /* Parse a postfix expression after a parenthesized type name: the
8127 brace-enclosed initializer of a compound literal, possibly followed
8128 by some postfix operators. This is separate because it is not
8129 possible to tell until after the type name whether a cast
8130 expression has a cast or a compound literal, or whether the operand
8131 of sizeof is a parenthesized type name or starts with a compound
8132 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8133 location of the first token after the parentheses around the type
8134 name. */
8136 static struct c_expr
8137 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8138 struct c_type_name *type_name,
8139 location_t type_loc)
8141 tree type;
8142 struct c_expr init;
8143 bool non_const;
8144 struct c_expr expr;
8145 location_t start_loc;
8146 tree type_expr = NULL_TREE;
8147 bool type_expr_const = true;
8148 check_compound_literal_type (type_loc, type_name);
8149 start_init (NULL_TREE, NULL, 0);
8150 type = groktypename (type_name, &type_expr, &type_expr_const);
8151 start_loc = c_parser_peek_token (parser)->location;
8152 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8154 error_at (type_loc, "compound literal has variable size");
8155 type = error_mark_node;
8157 init = c_parser_braced_init (parser, type, false, NULL);
8158 finish_init ();
8159 maybe_warn_string_init (type_loc, type, init);
8161 if (type != error_mark_node
8162 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8163 && current_function_decl)
8165 error ("compound literal qualified by address-space qualifier");
8166 type = error_mark_node;
8169 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8170 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8171 ? CONSTRUCTOR_NON_CONST (init.value)
8172 : init.original_code == C_MAYBE_CONST_EXPR);
8173 non_const |= !type_expr_const;
8174 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8175 set_c_expr_source_range (&expr, init.src_range);
8176 expr.original_code = ERROR_MARK;
8177 expr.original_type = NULL;
8178 if (type != error_mark_node && type_expr)
8180 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8182 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8183 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8185 else
8187 gcc_assert (!non_const);
8188 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8189 type_expr, expr.value);
8192 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8195 /* Callback function for sizeof_pointer_memaccess_warning to compare
8196 types. */
8198 static bool
8199 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8201 return comptypes (type1, type2) == 1;
8204 /* Parse a postfix expression after the initial primary or compound
8205 literal; that is, parse a series of postfix operators.
8207 EXPR_LOC is the location of the primary expression. */
8209 static struct c_expr
8210 c_parser_postfix_expression_after_primary (c_parser *parser,
8211 location_t expr_loc,
8212 struct c_expr expr)
8214 struct c_expr orig_expr;
8215 tree ident, idx;
8216 location_t sizeof_arg_loc[3];
8217 tree sizeof_arg[3];
8218 unsigned int literal_zero_mask;
8219 unsigned int i;
8220 vec<tree, va_gc> *exprlist;
8221 vec<tree, va_gc> *origtypes = NULL;
8222 vec<location_t> arg_loc = vNULL;
8223 location_t start;
8224 location_t finish;
8226 while (true)
8228 location_t op_loc = c_parser_peek_token (parser)->location;
8229 switch (c_parser_peek_token (parser)->type)
8231 case CPP_OPEN_SQUARE:
8232 /* Array reference. */
8233 c_parser_consume_token (parser);
8234 if (flag_cilkplus
8235 && c_parser_peek_token (parser)->type == CPP_COLON)
8236 /* If we are here, then we have something like this:
8237 Array [ : ]
8239 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8240 expr.value);
8241 else
8243 idx = c_parser_expression (parser).value;
8244 /* Here we have 3 options:
8245 1. Array [EXPR] -- Normal Array call.
8246 2. Array [EXPR : EXPR] -- Array notation without stride.
8247 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8249 For 1, we just handle it just like a normal array expression.
8250 For 2 and 3 we handle it like we handle array notations. The
8251 idx value we have above becomes the initial/start index.
8253 if (flag_cilkplus
8254 && c_parser_peek_token (parser)->type == CPP_COLON)
8255 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8256 expr.value);
8257 else
8259 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8260 "expected %<]%>");
8261 start = expr.get_start ();
8262 finish = parser->tokens_buf[0].location;
8263 expr.value = build_array_ref (op_loc, expr.value, idx);
8264 set_c_expr_source_range (&expr, start, finish);
8267 expr.original_code = ERROR_MARK;
8268 expr.original_type = NULL;
8269 break;
8270 case CPP_OPEN_PAREN:
8271 /* Function call. */
8272 c_parser_consume_token (parser);
8273 for (i = 0; i < 3; i++)
8275 sizeof_arg[i] = NULL_TREE;
8276 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8278 literal_zero_mask = 0;
8279 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8280 exprlist = NULL;
8281 else
8282 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8283 sizeof_arg_loc, sizeof_arg,
8284 &arg_loc, &literal_zero_mask);
8285 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8286 "expected %<)%>");
8287 orig_expr = expr;
8288 mark_exp_read (expr.value);
8289 if (warn_sizeof_pointer_memaccess)
8290 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8291 expr.value, exprlist,
8292 sizeof_arg,
8293 sizeof_ptr_memacc_comptypes);
8294 if (TREE_CODE (expr.value) == FUNCTION_DECL
8295 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8296 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8297 && vec_safe_length (exprlist) == 3)
8299 tree arg0 = (*exprlist)[0];
8300 tree arg2 = (*exprlist)[2];
8301 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
8304 start = expr.get_start ();
8305 finish = parser->tokens_buf[0].get_finish ();
8306 expr.value
8307 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8308 exprlist, origtypes);
8309 set_c_expr_source_range (&expr, start, finish);
8311 expr.original_code = ERROR_MARK;
8312 if (TREE_CODE (expr.value) == INTEGER_CST
8313 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8314 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8315 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8316 expr.original_code = C_MAYBE_CONST_EXPR;
8317 expr.original_type = NULL;
8318 if (exprlist)
8320 release_tree_vector (exprlist);
8321 release_tree_vector (origtypes);
8323 arg_loc.release ();
8324 break;
8325 case CPP_DOT:
8326 /* Structure element reference. */
8327 c_parser_consume_token (parser);
8328 expr = default_function_array_conversion (expr_loc, expr);
8329 if (c_parser_next_token_is (parser, CPP_NAME))
8330 ident = c_parser_peek_token (parser)->value;
8331 else
8333 c_parser_error (parser, "expected identifier");
8334 expr.value = error_mark_node;
8335 expr.original_code = ERROR_MARK;
8336 expr.original_type = NULL;
8337 return expr;
8339 start = expr.get_start ();
8340 finish = c_parser_peek_token (parser)->get_finish ();
8341 c_parser_consume_token (parser);
8342 expr.value = build_component_ref (op_loc, expr.value, ident);
8343 set_c_expr_source_range (&expr, start, finish);
8344 expr.original_code = ERROR_MARK;
8345 if (TREE_CODE (expr.value) != COMPONENT_REF)
8346 expr.original_type = NULL;
8347 else
8349 /* Remember the original type of a bitfield. */
8350 tree field = TREE_OPERAND (expr.value, 1);
8351 if (TREE_CODE (field) != FIELD_DECL)
8352 expr.original_type = NULL;
8353 else
8354 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8356 break;
8357 case CPP_DEREF:
8358 /* Structure element reference. */
8359 c_parser_consume_token (parser);
8360 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8361 if (c_parser_next_token_is (parser, CPP_NAME))
8362 ident = c_parser_peek_token (parser)->value;
8363 else
8365 c_parser_error (parser, "expected identifier");
8366 expr.value = error_mark_node;
8367 expr.original_code = ERROR_MARK;
8368 expr.original_type = NULL;
8369 return expr;
8371 start = expr.get_start ();
8372 finish = c_parser_peek_token (parser)->get_finish ();
8373 c_parser_consume_token (parser);
8374 expr.value = build_component_ref (op_loc,
8375 build_indirect_ref (op_loc,
8376 expr.value,
8377 RO_ARROW),
8378 ident);
8379 set_c_expr_source_range (&expr, start, finish);
8380 expr.original_code = ERROR_MARK;
8381 if (TREE_CODE (expr.value) != COMPONENT_REF)
8382 expr.original_type = NULL;
8383 else
8385 /* Remember the original type of a bitfield. */
8386 tree field = TREE_OPERAND (expr.value, 1);
8387 if (TREE_CODE (field) != FIELD_DECL)
8388 expr.original_type = NULL;
8389 else
8390 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8392 break;
8393 case CPP_PLUS_PLUS:
8394 /* Postincrement. */
8395 start = expr.get_start ();
8396 finish = c_parser_peek_token (parser)->get_finish ();
8397 c_parser_consume_token (parser);
8398 /* If the expressions have array notations, we expand them. */
8399 if (flag_cilkplus
8400 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8401 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8402 else
8404 expr = default_function_array_read_conversion (expr_loc, expr);
8405 expr.value = build_unary_op (op_loc,
8406 POSTINCREMENT_EXPR, expr.value, 0);
8408 set_c_expr_source_range (&expr, start, finish);
8409 expr.original_code = ERROR_MARK;
8410 expr.original_type = NULL;
8411 break;
8412 case CPP_MINUS_MINUS:
8413 /* Postdecrement. */
8414 start = expr.get_start ();
8415 finish = c_parser_peek_token (parser)->get_finish ();
8416 c_parser_consume_token (parser);
8417 /* If the expressions have array notations, we expand them. */
8418 if (flag_cilkplus
8419 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8420 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8421 else
8423 expr = default_function_array_read_conversion (expr_loc, expr);
8424 expr.value = build_unary_op (op_loc,
8425 POSTDECREMENT_EXPR, expr.value, 0);
8427 set_c_expr_source_range (&expr, start, finish);
8428 expr.original_code = ERROR_MARK;
8429 expr.original_type = NULL;
8430 break;
8431 default:
8432 return expr;
8437 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8439 expression:
8440 assignment-expression
8441 expression , assignment-expression
8444 static struct c_expr
8445 c_parser_expression (c_parser *parser)
8447 location_t tloc = c_parser_peek_token (parser)->location;
8448 struct c_expr expr;
8449 expr = c_parser_expr_no_commas (parser, NULL);
8450 if (c_parser_next_token_is (parser, CPP_COMMA))
8451 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8452 while (c_parser_next_token_is (parser, CPP_COMMA))
8454 struct c_expr next;
8455 tree lhsval;
8456 location_t loc = c_parser_peek_token (parser)->location;
8457 location_t expr_loc;
8458 c_parser_consume_token (parser);
8459 expr_loc = c_parser_peek_token (parser)->location;
8460 lhsval = expr.value;
8461 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8462 lhsval = TREE_OPERAND (lhsval, 1);
8463 if (DECL_P (lhsval) || handled_component_p (lhsval))
8464 mark_exp_read (lhsval);
8465 next = c_parser_expr_no_commas (parser, NULL);
8466 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8467 expr.value = build_compound_expr (loc, expr.value, next.value);
8468 expr.original_code = COMPOUND_EXPR;
8469 expr.original_type = next.original_type;
8471 return expr;
8474 /* Parse an expression and convert functions or arrays to pointers and
8475 lvalues to rvalues. */
8477 static struct c_expr
8478 c_parser_expression_conv (c_parser *parser)
8480 struct c_expr expr;
8481 location_t loc = c_parser_peek_token (parser)->location;
8482 expr = c_parser_expression (parser);
8483 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8484 return expr;
8487 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8488 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8490 static inline void
8491 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8492 unsigned int idx)
8494 if (idx >= HOST_BITS_PER_INT)
8495 return;
8497 c_token *tok = c_parser_peek_token (parser);
8498 switch (tok->type)
8500 case CPP_NUMBER:
8501 case CPP_CHAR:
8502 case CPP_WCHAR:
8503 case CPP_CHAR16:
8504 case CPP_CHAR32:
8505 /* If a parameter is literal zero alone, remember it
8506 for -Wmemset-transposed-args warning. */
8507 if (integer_zerop (tok->value)
8508 && !TREE_OVERFLOW (tok->value)
8509 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8510 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8511 *literal_zero_mask |= 1U << idx;
8512 default:
8513 break;
8517 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8518 functions and arrays to pointers and lvalues to rvalues. If
8519 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8520 locations of function arguments into this vector.
8522 nonempty-expr-list:
8523 assignment-expression
8524 nonempty-expr-list , assignment-expression
8527 static vec<tree, va_gc> *
8528 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8529 vec<tree, va_gc> **p_orig_types,
8530 location_t *sizeof_arg_loc, tree *sizeof_arg,
8531 vec<location_t> *locations,
8532 unsigned int *literal_zero_mask)
8534 vec<tree, va_gc> *ret;
8535 vec<tree, va_gc> *orig_types;
8536 struct c_expr expr;
8537 location_t loc = c_parser_peek_token (parser)->location;
8538 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8539 unsigned int idx = 0;
8541 ret = make_tree_vector ();
8542 if (p_orig_types == NULL)
8543 orig_types = NULL;
8544 else
8545 orig_types = make_tree_vector ();
8547 if (sizeof_arg != NULL
8548 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8549 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8550 if (literal_zero_mask)
8551 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8552 expr = c_parser_expr_no_commas (parser, NULL);
8553 if (convert_p)
8554 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8555 if (fold_p)
8556 expr.value = c_fully_fold (expr.value, false, NULL);
8557 ret->quick_push (expr.value);
8558 if (orig_types)
8559 orig_types->quick_push (expr.original_type);
8560 if (locations)
8561 locations->safe_push (loc);
8562 if (sizeof_arg != NULL
8563 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8564 && expr.original_code == SIZEOF_EXPR)
8566 sizeof_arg[0] = c_last_sizeof_arg;
8567 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8569 while (c_parser_next_token_is (parser, CPP_COMMA))
8571 c_parser_consume_token (parser);
8572 loc = c_parser_peek_token (parser)->location;
8573 if (sizeof_arg != NULL
8574 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8575 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8576 else
8577 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8578 if (literal_zero_mask)
8579 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8580 expr = c_parser_expr_no_commas (parser, NULL);
8581 if (convert_p)
8582 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8583 if (fold_p)
8584 expr.value = c_fully_fold (expr.value, false, NULL);
8585 vec_safe_push (ret, expr.value);
8586 if (orig_types)
8587 vec_safe_push (orig_types, expr.original_type);
8588 if (locations)
8589 locations->safe_push (loc);
8590 if (++idx < 3
8591 && sizeof_arg != NULL
8592 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8593 && expr.original_code == SIZEOF_EXPR)
8595 sizeof_arg[idx] = c_last_sizeof_arg;
8596 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8599 if (orig_types)
8600 *p_orig_types = orig_types;
8601 return ret;
8604 /* Parse Objective-C-specific constructs. */
8606 /* Parse an objc-class-definition.
8608 objc-class-definition:
8609 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8610 objc-class-instance-variables[opt] objc-methodprotolist @end
8611 @implementation identifier objc-superclass[opt]
8612 objc-class-instance-variables[opt]
8613 @interface identifier ( identifier ) objc-protocol-refs[opt]
8614 objc-methodprotolist @end
8615 @interface identifier ( ) objc-protocol-refs[opt]
8616 objc-methodprotolist @end
8617 @implementation identifier ( identifier )
8619 objc-superclass:
8620 : identifier
8622 "@interface identifier (" must start "@interface identifier (
8623 identifier ) ...": objc-methodprotolist in the first production may
8624 not start with a parenthesized identifier as a declarator of a data
8625 definition with no declaration specifiers if the objc-superclass,
8626 objc-protocol-refs and objc-class-instance-variables are omitted. */
8628 static void
8629 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8631 bool iface_p;
8632 tree id1;
8633 tree superclass;
8634 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8635 iface_p = true;
8636 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8637 iface_p = false;
8638 else
8639 gcc_unreachable ();
8641 c_parser_consume_token (parser);
8642 if (c_parser_next_token_is_not (parser, CPP_NAME))
8644 c_parser_error (parser, "expected identifier");
8645 return;
8647 id1 = c_parser_peek_token (parser)->value;
8648 c_parser_consume_token (parser);
8649 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8651 /* We have a category or class extension. */
8652 tree id2;
8653 tree proto = NULL_TREE;
8654 c_parser_consume_token (parser);
8655 if (c_parser_next_token_is_not (parser, CPP_NAME))
8657 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8659 /* We have a class extension. */
8660 id2 = NULL_TREE;
8662 else
8664 c_parser_error (parser, "expected identifier or %<)%>");
8665 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8666 return;
8669 else
8671 id2 = c_parser_peek_token (parser)->value;
8672 c_parser_consume_token (parser);
8674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8675 if (!iface_p)
8677 objc_start_category_implementation (id1, id2);
8678 return;
8680 if (c_parser_next_token_is (parser, CPP_LESS))
8681 proto = c_parser_objc_protocol_refs (parser);
8682 objc_start_category_interface (id1, id2, proto, attributes);
8683 c_parser_objc_methodprotolist (parser);
8684 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8685 objc_finish_interface ();
8686 return;
8688 if (c_parser_next_token_is (parser, CPP_COLON))
8690 c_parser_consume_token (parser);
8691 if (c_parser_next_token_is_not (parser, CPP_NAME))
8693 c_parser_error (parser, "expected identifier");
8694 return;
8696 superclass = c_parser_peek_token (parser)->value;
8697 c_parser_consume_token (parser);
8699 else
8700 superclass = NULL_TREE;
8701 if (iface_p)
8703 tree proto = NULL_TREE;
8704 if (c_parser_next_token_is (parser, CPP_LESS))
8705 proto = c_parser_objc_protocol_refs (parser);
8706 objc_start_class_interface (id1, superclass, proto, attributes);
8708 else
8709 objc_start_class_implementation (id1, superclass);
8710 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8711 c_parser_objc_class_instance_variables (parser);
8712 if (iface_p)
8714 objc_continue_interface ();
8715 c_parser_objc_methodprotolist (parser);
8716 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8717 objc_finish_interface ();
8719 else
8721 objc_continue_implementation ();
8722 return;
8726 /* Parse objc-class-instance-variables.
8728 objc-class-instance-variables:
8729 { objc-instance-variable-decl-list[opt] }
8731 objc-instance-variable-decl-list:
8732 objc-visibility-spec
8733 objc-instance-variable-decl ;
8735 objc-instance-variable-decl-list objc-visibility-spec
8736 objc-instance-variable-decl-list objc-instance-variable-decl ;
8737 objc-instance-variable-decl-list ;
8739 objc-visibility-spec:
8740 @private
8741 @protected
8742 @public
8744 objc-instance-variable-decl:
8745 struct-declaration
8748 static void
8749 c_parser_objc_class_instance_variables (c_parser *parser)
8751 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8752 c_parser_consume_token (parser);
8753 while (c_parser_next_token_is_not (parser, CPP_EOF))
8755 tree decls;
8756 /* Parse any stray semicolon. */
8757 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8759 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8760 "extra semicolon");
8761 c_parser_consume_token (parser);
8762 continue;
8764 /* Stop if at the end of the instance variables. */
8765 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8767 c_parser_consume_token (parser);
8768 break;
8770 /* Parse any objc-visibility-spec. */
8771 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8773 c_parser_consume_token (parser);
8774 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8775 continue;
8777 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8779 c_parser_consume_token (parser);
8780 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8781 continue;
8783 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8785 c_parser_consume_token (parser);
8786 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8787 continue;
8789 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8791 c_parser_consume_token (parser);
8792 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8793 continue;
8795 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8797 c_parser_pragma (parser, pragma_external, NULL);
8798 continue;
8801 /* Parse some comma-separated declarations. */
8802 decls = c_parser_struct_declaration (parser);
8803 if (decls == NULL)
8805 /* There is a syntax error. We want to skip the offending
8806 tokens up to the next ';' (included) or '}'
8807 (excluded). */
8809 /* First, skip manually a ')' or ']'. This is because they
8810 reduce the nesting level, so c_parser_skip_until_found()
8811 wouldn't be able to skip past them. */
8812 c_token *token = c_parser_peek_token (parser);
8813 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8814 c_parser_consume_token (parser);
8816 /* Then, do the standard skipping. */
8817 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8819 /* We hopefully recovered. Start normal parsing again. */
8820 parser->error = false;
8821 continue;
8823 else
8825 /* Comma-separated instance variables are chained together
8826 in reverse order; add them one by one. */
8827 tree ivar = nreverse (decls);
8828 for (; ivar; ivar = DECL_CHAIN (ivar))
8829 objc_add_instance_variable (copy_node (ivar));
8831 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8835 /* Parse an objc-class-declaration.
8837 objc-class-declaration:
8838 @class identifier-list ;
8841 static void
8842 c_parser_objc_class_declaration (c_parser *parser)
8844 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8845 c_parser_consume_token (parser);
8846 /* Any identifiers, including those declared as type names, are OK
8847 here. */
8848 while (true)
8850 tree id;
8851 if (c_parser_next_token_is_not (parser, CPP_NAME))
8853 c_parser_error (parser, "expected identifier");
8854 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8855 parser->error = false;
8856 return;
8858 id = c_parser_peek_token (parser)->value;
8859 objc_declare_class (id);
8860 c_parser_consume_token (parser);
8861 if (c_parser_next_token_is (parser, CPP_COMMA))
8862 c_parser_consume_token (parser);
8863 else
8864 break;
8866 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8869 /* Parse an objc-alias-declaration.
8871 objc-alias-declaration:
8872 @compatibility_alias identifier identifier ;
8875 static void
8876 c_parser_objc_alias_declaration (c_parser *parser)
8878 tree id1, id2;
8879 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8880 c_parser_consume_token (parser);
8881 if (c_parser_next_token_is_not (parser, CPP_NAME))
8883 c_parser_error (parser, "expected identifier");
8884 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8885 return;
8887 id1 = c_parser_peek_token (parser)->value;
8888 c_parser_consume_token (parser);
8889 if (c_parser_next_token_is_not (parser, CPP_NAME))
8891 c_parser_error (parser, "expected identifier");
8892 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8893 return;
8895 id2 = c_parser_peek_token (parser)->value;
8896 c_parser_consume_token (parser);
8897 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8898 objc_declare_alias (id1, id2);
8901 /* Parse an objc-protocol-definition.
8903 objc-protocol-definition:
8904 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8905 @protocol identifier-list ;
8907 "@protocol identifier ;" should be resolved as "@protocol
8908 identifier-list ;": objc-methodprotolist may not start with a
8909 semicolon in the first alternative if objc-protocol-refs are
8910 omitted. */
8912 static void
8913 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8915 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8917 c_parser_consume_token (parser);
8918 if (c_parser_next_token_is_not (parser, CPP_NAME))
8920 c_parser_error (parser, "expected identifier");
8921 return;
8923 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8924 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8926 /* Any identifiers, including those declared as type names, are
8927 OK here. */
8928 while (true)
8930 tree id;
8931 if (c_parser_next_token_is_not (parser, CPP_NAME))
8933 c_parser_error (parser, "expected identifier");
8934 break;
8936 id = c_parser_peek_token (parser)->value;
8937 objc_declare_protocol (id, attributes);
8938 c_parser_consume_token (parser);
8939 if (c_parser_next_token_is (parser, CPP_COMMA))
8940 c_parser_consume_token (parser);
8941 else
8942 break;
8944 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8946 else
8948 tree id = c_parser_peek_token (parser)->value;
8949 tree proto = NULL_TREE;
8950 c_parser_consume_token (parser);
8951 if (c_parser_next_token_is (parser, CPP_LESS))
8952 proto = c_parser_objc_protocol_refs (parser);
8953 parser->objc_pq_context = true;
8954 objc_start_protocol (id, proto, attributes);
8955 c_parser_objc_methodprotolist (parser);
8956 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8957 parser->objc_pq_context = false;
8958 objc_finish_interface ();
8962 /* Parse an objc-method-type.
8964 objc-method-type:
8968 Return true if it is a class method (+) and false if it is
8969 an instance method (-).
8971 static inline bool
8972 c_parser_objc_method_type (c_parser *parser)
8974 switch (c_parser_peek_token (parser)->type)
8976 case CPP_PLUS:
8977 c_parser_consume_token (parser);
8978 return true;
8979 case CPP_MINUS:
8980 c_parser_consume_token (parser);
8981 return false;
8982 default:
8983 gcc_unreachable ();
8987 /* Parse an objc-method-definition.
8989 objc-method-definition:
8990 objc-method-type objc-method-decl ;[opt] compound-statement
8993 static void
8994 c_parser_objc_method_definition (c_parser *parser)
8996 bool is_class_method = c_parser_objc_method_type (parser);
8997 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8998 parser->objc_pq_context = true;
8999 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9000 &expr);
9001 if (decl == error_mark_node)
9002 return; /* Bail here. */
9004 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9006 c_parser_consume_token (parser);
9007 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9008 "extra semicolon in method definition specified");
9011 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9013 c_parser_error (parser, "expected %<{%>");
9014 return;
9017 parser->objc_pq_context = false;
9018 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9020 add_stmt (c_parser_compound_statement (parser));
9021 objc_finish_method_definition (current_function_decl);
9023 else
9025 /* This code is executed when we find a method definition
9026 outside of an @implementation context (or invalid for other
9027 reasons). Parse the method (to keep going) but do not emit
9028 any code.
9030 c_parser_compound_statement (parser);
9034 /* Parse an objc-methodprotolist.
9036 objc-methodprotolist:
9037 empty
9038 objc-methodprotolist objc-methodproto
9039 objc-methodprotolist declaration
9040 objc-methodprotolist ;
9041 @optional
9042 @required
9044 The declaration is a data definition, which may be missing
9045 declaration specifiers under the same rules and diagnostics as
9046 other data definitions outside functions, and the stray semicolon
9047 is diagnosed the same way as a stray semicolon outside a
9048 function. */
9050 static void
9051 c_parser_objc_methodprotolist (c_parser *parser)
9053 while (true)
9055 /* The list is terminated by @end. */
9056 switch (c_parser_peek_token (parser)->type)
9058 case CPP_SEMICOLON:
9059 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9060 "ISO C does not allow extra %<;%> outside of a function");
9061 c_parser_consume_token (parser);
9062 break;
9063 case CPP_PLUS:
9064 case CPP_MINUS:
9065 c_parser_objc_methodproto (parser);
9066 break;
9067 case CPP_PRAGMA:
9068 c_parser_pragma (parser, pragma_external, NULL);
9069 break;
9070 case CPP_EOF:
9071 return;
9072 default:
9073 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9074 return;
9075 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9076 c_parser_objc_at_property_declaration (parser);
9077 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9079 objc_set_method_opt (true);
9080 c_parser_consume_token (parser);
9082 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9084 objc_set_method_opt (false);
9085 c_parser_consume_token (parser);
9087 else
9088 c_parser_declaration_or_fndef (parser, false, false, true,
9089 false, true, NULL, vNULL);
9090 break;
9095 /* Parse an objc-methodproto.
9097 objc-methodproto:
9098 objc-method-type objc-method-decl ;
9101 static void
9102 c_parser_objc_methodproto (c_parser *parser)
9104 bool is_class_method = c_parser_objc_method_type (parser);
9105 tree decl, attributes = NULL_TREE;
9107 /* Remember protocol qualifiers in prototypes. */
9108 parser->objc_pq_context = true;
9109 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9110 NULL);
9111 /* Forget protocol qualifiers now. */
9112 parser->objc_pq_context = false;
9114 /* Do not allow the presence of attributes to hide an erroneous
9115 method implementation in the interface section. */
9116 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9118 c_parser_error (parser, "expected %<;%>");
9119 return;
9122 if (decl != error_mark_node)
9123 objc_add_method_declaration (is_class_method, decl, attributes);
9125 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9128 /* If we are at a position that method attributes may be present, check that
9129 there are not any parsed already (a syntax error) and then collect any
9130 specified at the current location. Finally, if new attributes were present,
9131 check that the next token is legal ( ';' for decls and '{' for defs). */
9133 static bool
9134 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9136 bool bad = false;
9137 if (*attributes)
9139 c_parser_error (parser,
9140 "method attributes must be specified at the end only");
9141 *attributes = NULL_TREE;
9142 bad = true;
9145 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9146 *attributes = c_parser_attributes (parser);
9148 /* If there were no attributes here, just report any earlier error. */
9149 if (*attributes == NULL_TREE || bad)
9150 return bad;
9152 /* If the attributes are followed by a ; or {, then just report any earlier
9153 error. */
9154 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9155 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9156 return bad;
9158 /* We've got attributes, but not at the end. */
9159 c_parser_error (parser,
9160 "expected %<;%> or %<{%> after method attribute definition");
9161 return true;
9164 /* Parse an objc-method-decl.
9166 objc-method-decl:
9167 ( objc-type-name ) objc-selector
9168 objc-selector
9169 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9170 objc-keyword-selector objc-optparmlist
9171 attributes
9173 objc-keyword-selector:
9174 objc-keyword-decl
9175 objc-keyword-selector objc-keyword-decl
9177 objc-keyword-decl:
9178 objc-selector : ( objc-type-name ) identifier
9179 objc-selector : identifier
9180 : ( objc-type-name ) identifier
9181 : identifier
9183 objc-optparmlist:
9184 objc-optparms objc-optellipsis
9186 objc-optparms:
9187 empty
9188 objc-opt-parms , parameter-declaration
9190 objc-optellipsis:
9191 empty
9192 , ...
9195 static tree
9196 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9197 tree *attributes, tree *expr)
9199 tree type = NULL_TREE;
9200 tree sel;
9201 tree parms = NULL_TREE;
9202 bool ellipsis = false;
9203 bool attr_err = false;
9205 *attributes = NULL_TREE;
9206 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9208 c_parser_consume_token (parser);
9209 type = c_parser_objc_type_name (parser);
9210 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9212 sel = c_parser_objc_selector (parser);
9213 /* If there is no selector, or a colon follows, we have an
9214 objc-keyword-selector. If there is a selector, and a colon does
9215 not follow, that selector ends the objc-method-decl. */
9216 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9218 tree tsel = sel;
9219 tree list = NULL_TREE;
9220 while (true)
9222 tree atype = NULL_TREE, id, keyworddecl;
9223 tree param_attr = NULL_TREE;
9224 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9225 break;
9226 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9228 c_parser_consume_token (parser);
9229 atype = c_parser_objc_type_name (parser);
9230 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9231 "expected %<)%>");
9233 /* New ObjC allows attributes on method parameters. */
9234 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9235 param_attr = c_parser_attributes (parser);
9236 if (c_parser_next_token_is_not (parser, CPP_NAME))
9238 c_parser_error (parser, "expected identifier");
9239 return error_mark_node;
9241 id = c_parser_peek_token (parser)->value;
9242 c_parser_consume_token (parser);
9243 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9244 list = chainon (list, keyworddecl);
9245 tsel = c_parser_objc_selector (parser);
9246 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9247 break;
9250 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9252 /* Parse the optional parameter list. Optional Objective-C
9253 method parameters follow the C syntax, and may include '...'
9254 to denote a variable number of arguments. */
9255 parms = make_node (TREE_LIST);
9256 while (c_parser_next_token_is (parser, CPP_COMMA))
9258 struct c_parm *parm;
9259 c_parser_consume_token (parser);
9260 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9262 ellipsis = true;
9263 c_parser_consume_token (parser);
9264 attr_err |= c_parser_objc_maybe_method_attributes
9265 (parser, attributes) ;
9266 break;
9268 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9269 if (parm == NULL)
9270 break;
9271 parms = chainon (parms,
9272 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9274 sel = list;
9276 else
9277 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9279 if (sel == NULL)
9281 c_parser_error (parser, "objective-c method declaration is expected");
9282 return error_mark_node;
9285 if (attr_err)
9286 return error_mark_node;
9288 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9291 /* Parse an objc-type-name.
9293 objc-type-name:
9294 objc-type-qualifiers[opt] type-name
9295 objc-type-qualifiers[opt]
9297 objc-type-qualifiers:
9298 objc-type-qualifier
9299 objc-type-qualifiers objc-type-qualifier
9301 objc-type-qualifier: one of
9302 in out inout bycopy byref oneway
9305 static tree
9306 c_parser_objc_type_name (c_parser *parser)
9308 tree quals = NULL_TREE;
9309 struct c_type_name *type_name = NULL;
9310 tree type = NULL_TREE;
9311 while (true)
9313 c_token *token = c_parser_peek_token (parser);
9314 if (token->type == CPP_KEYWORD
9315 && (token->keyword == RID_IN
9316 || token->keyword == RID_OUT
9317 || token->keyword == RID_INOUT
9318 || token->keyword == RID_BYCOPY
9319 || token->keyword == RID_BYREF
9320 || token->keyword == RID_ONEWAY))
9322 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9323 c_parser_consume_token (parser);
9325 else
9326 break;
9328 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9329 type_name = c_parser_type_name (parser);
9330 if (type_name)
9331 type = groktypename (type_name, NULL, NULL);
9333 /* If the type is unknown, and error has already been produced and
9334 we need to recover from the error. In that case, use NULL_TREE
9335 for the type, as if no type had been specified; this will use the
9336 default type ('id') which is good for error recovery. */
9337 if (type == error_mark_node)
9338 type = NULL_TREE;
9340 return build_tree_list (quals, type);
9343 /* Parse objc-protocol-refs.
9345 objc-protocol-refs:
9346 < identifier-list >
9349 static tree
9350 c_parser_objc_protocol_refs (c_parser *parser)
9352 tree list = NULL_TREE;
9353 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9354 c_parser_consume_token (parser);
9355 /* Any identifiers, including those declared as type names, are OK
9356 here. */
9357 while (true)
9359 tree id;
9360 if (c_parser_next_token_is_not (parser, CPP_NAME))
9362 c_parser_error (parser, "expected identifier");
9363 break;
9365 id = c_parser_peek_token (parser)->value;
9366 list = chainon (list, build_tree_list (NULL_TREE, id));
9367 c_parser_consume_token (parser);
9368 if (c_parser_next_token_is (parser, CPP_COMMA))
9369 c_parser_consume_token (parser);
9370 else
9371 break;
9373 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9374 return list;
9377 /* Parse an objc-try-catch-finally-statement.
9379 objc-try-catch-finally-statement:
9380 @try compound-statement objc-catch-list[opt]
9381 @try compound-statement objc-catch-list[opt] @finally compound-statement
9383 objc-catch-list:
9384 @catch ( objc-catch-parameter-declaration ) compound-statement
9385 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9387 objc-catch-parameter-declaration:
9388 parameter-declaration
9389 '...'
9391 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9393 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9394 for C++. Keep them in sync. */
9396 static void
9397 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9399 location_t location;
9400 tree stmt;
9402 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9403 c_parser_consume_token (parser);
9404 location = c_parser_peek_token (parser)->location;
9405 objc_maybe_warn_exceptions (location);
9406 stmt = c_parser_compound_statement (parser);
9407 objc_begin_try_stmt (location, stmt);
9409 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9411 struct c_parm *parm;
9412 tree parameter_declaration = error_mark_node;
9413 bool seen_open_paren = false;
9415 c_parser_consume_token (parser);
9416 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9417 seen_open_paren = true;
9418 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9420 /* We have "@catch (...)" (where the '...' are literally
9421 what is in the code). Skip the '...'.
9422 parameter_declaration is set to NULL_TREE, and
9423 objc_being_catch_clauses() knows that that means
9424 '...'. */
9425 c_parser_consume_token (parser);
9426 parameter_declaration = NULL_TREE;
9428 else
9430 /* We have "@catch (NSException *exception)" or something
9431 like that. Parse the parameter declaration. */
9432 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9433 if (parm == NULL)
9434 parameter_declaration = error_mark_node;
9435 else
9436 parameter_declaration = grokparm (parm, NULL);
9438 if (seen_open_paren)
9439 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9440 else
9442 /* If there was no open parenthesis, we are recovering from
9443 an error, and we are trying to figure out what mistake
9444 the user has made. */
9446 /* If there is an immediate closing parenthesis, the user
9447 probably forgot the opening one (ie, they typed "@catch
9448 NSException *e)". Parse the closing parenthesis and keep
9449 going. */
9450 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9451 c_parser_consume_token (parser);
9453 /* If these is no immediate closing parenthesis, the user
9454 probably doesn't know that parenthesis are required at
9455 all (ie, they typed "@catch NSException *e"). So, just
9456 forget about the closing parenthesis and keep going. */
9458 objc_begin_catch_clause (parameter_declaration);
9459 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9460 c_parser_compound_statement_nostart (parser);
9461 objc_finish_catch_clause ();
9463 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9465 c_parser_consume_token (parser);
9466 location = c_parser_peek_token (parser)->location;
9467 stmt = c_parser_compound_statement (parser);
9468 objc_build_finally_clause (location, stmt);
9470 objc_finish_try_stmt ();
9473 /* Parse an objc-synchronized-statement.
9475 objc-synchronized-statement:
9476 @synchronized ( expression ) compound-statement
9479 static void
9480 c_parser_objc_synchronized_statement (c_parser *parser)
9482 location_t loc;
9483 tree expr, stmt;
9484 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9485 c_parser_consume_token (parser);
9486 loc = c_parser_peek_token (parser)->location;
9487 objc_maybe_warn_exceptions (loc);
9488 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9490 struct c_expr ce = c_parser_expression (parser);
9491 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9492 expr = ce.value;
9493 expr = c_fully_fold (expr, false, NULL);
9494 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9496 else
9497 expr = error_mark_node;
9498 stmt = c_parser_compound_statement (parser);
9499 objc_build_synchronized (loc, expr, stmt);
9502 /* Parse an objc-selector; return NULL_TREE without an error if the
9503 next token is not an objc-selector.
9505 objc-selector:
9506 identifier
9507 one of
9508 enum struct union if else while do for switch case default
9509 break continue return goto asm sizeof typeof __alignof
9510 unsigned long const short volatile signed restrict _Complex
9511 in out inout bycopy byref oneway int char float double void _Bool
9512 _Atomic
9514 ??? Why this selection of keywords but not, for example, storage
9515 class specifiers? */
9517 static tree
9518 c_parser_objc_selector (c_parser *parser)
9520 c_token *token = c_parser_peek_token (parser);
9521 tree value = token->value;
9522 if (token->type == CPP_NAME)
9524 c_parser_consume_token (parser);
9525 return value;
9527 if (token->type != CPP_KEYWORD)
9528 return NULL_TREE;
9529 switch (token->keyword)
9531 case RID_ENUM:
9532 case RID_STRUCT:
9533 case RID_UNION:
9534 case RID_IF:
9535 case RID_ELSE:
9536 case RID_WHILE:
9537 case RID_DO:
9538 case RID_FOR:
9539 case RID_SWITCH:
9540 case RID_CASE:
9541 case RID_DEFAULT:
9542 case RID_BREAK:
9543 case RID_CONTINUE:
9544 case RID_RETURN:
9545 case RID_GOTO:
9546 case RID_ASM:
9547 case RID_SIZEOF:
9548 case RID_TYPEOF:
9549 case RID_ALIGNOF:
9550 case RID_UNSIGNED:
9551 case RID_LONG:
9552 case RID_CONST:
9553 case RID_SHORT:
9554 case RID_VOLATILE:
9555 case RID_SIGNED:
9556 case RID_RESTRICT:
9557 case RID_COMPLEX:
9558 case RID_IN:
9559 case RID_OUT:
9560 case RID_INOUT:
9561 case RID_BYCOPY:
9562 case RID_BYREF:
9563 case RID_ONEWAY:
9564 case RID_INT:
9565 case RID_CHAR:
9566 case RID_FLOAT:
9567 case RID_DOUBLE:
9568 case RID_VOID:
9569 case RID_BOOL:
9570 case RID_ATOMIC:
9571 case RID_AUTO_TYPE:
9572 case RID_INT_N_0:
9573 case RID_INT_N_1:
9574 case RID_INT_N_2:
9575 case RID_INT_N_3:
9576 c_parser_consume_token (parser);
9577 return value;
9578 default:
9579 return NULL_TREE;
9583 /* Parse an objc-selector-arg.
9585 objc-selector-arg:
9586 objc-selector
9587 objc-keywordname-list
9589 objc-keywordname-list:
9590 objc-keywordname
9591 objc-keywordname-list objc-keywordname
9593 objc-keywordname:
9594 objc-selector :
9598 static tree
9599 c_parser_objc_selector_arg (c_parser *parser)
9601 tree sel = c_parser_objc_selector (parser);
9602 tree list = NULL_TREE;
9603 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9604 return sel;
9605 while (true)
9607 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9608 return list;
9609 list = chainon (list, build_tree_list (sel, NULL_TREE));
9610 sel = c_parser_objc_selector (parser);
9611 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9612 break;
9614 return list;
9617 /* Parse an objc-receiver.
9619 objc-receiver:
9620 expression
9621 class-name
9622 type-name
9625 static tree
9626 c_parser_objc_receiver (c_parser *parser)
9628 location_t loc = c_parser_peek_token (parser)->location;
9630 if (c_parser_peek_token (parser)->type == CPP_NAME
9631 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9632 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9634 tree id = c_parser_peek_token (parser)->value;
9635 c_parser_consume_token (parser);
9636 return objc_get_class_reference (id);
9638 struct c_expr ce = c_parser_expression (parser);
9639 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9640 return c_fully_fold (ce.value, false, NULL);
9643 /* Parse objc-message-args.
9645 objc-message-args:
9646 objc-selector
9647 objc-keywordarg-list
9649 objc-keywordarg-list:
9650 objc-keywordarg
9651 objc-keywordarg-list objc-keywordarg
9653 objc-keywordarg:
9654 objc-selector : objc-keywordexpr
9655 : objc-keywordexpr
9658 static tree
9659 c_parser_objc_message_args (c_parser *parser)
9661 tree sel = c_parser_objc_selector (parser);
9662 tree list = NULL_TREE;
9663 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9664 return sel;
9665 while (true)
9667 tree keywordexpr;
9668 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9669 return error_mark_node;
9670 keywordexpr = c_parser_objc_keywordexpr (parser);
9671 list = chainon (list, build_tree_list (sel, keywordexpr));
9672 sel = c_parser_objc_selector (parser);
9673 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9674 break;
9676 return list;
9679 /* Parse an objc-keywordexpr.
9681 objc-keywordexpr:
9682 nonempty-expr-list
9685 static tree
9686 c_parser_objc_keywordexpr (c_parser *parser)
9688 tree ret;
9689 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9690 NULL, NULL, NULL, NULL);
9691 if (vec_safe_length (expr_list) == 1)
9693 /* Just return the expression, remove a level of
9694 indirection. */
9695 ret = (*expr_list)[0];
9697 else
9699 /* We have a comma expression, we will collapse later. */
9700 ret = build_tree_list_vec (expr_list);
9702 release_tree_vector (expr_list);
9703 return ret;
9706 /* A check, needed in several places, that ObjC interface, implementation or
9707 method definitions are not prefixed by incorrect items. */
9708 static bool
9709 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9710 struct c_declspecs *specs)
9712 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9713 || specs->typespec_kind != ctsk_none)
9715 c_parser_error (parser,
9716 "no type or storage class may be specified here,");
9717 c_parser_skip_to_end_of_block_or_statement (parser);
9718 return true;
9720 return false;
9723 /* Parse an Objective-C @property declaration. The syntax is:
9725 objc-property-declaration:
9726 '@property' objc-property-attributes[opt] struct-declaration ;
9728 objc-property-attributes:
9729 '(' objc-property-attribute-list ')'
9731 objc-property-attribute-list:
9732 objc-property-attribute
9733 objc-property-attribute-list, objc-property-attribute
9735 objc-property-attribute
9736 'getter' = identifier
9737 'setter' = identifier
9738 'readonly'
9739 'readwrite'
9740 'assign'
9741 'retain'
9742 'copy'
9743 'nonatomic'
9745 For example:
9746 @property NSString *name;
9747 @property (readonly) id object;
9748 @property (retain, nonatomic, getter=getTheName) id name;
9749 @property int a, b, c;
9751 PS: This function is identical to cp_parser_objc_at_propery_declaration
9752 for C++. Keep them in sync. */
9753 static void
9754 c_parser_objc_at_property_declaration (c_parser *parser)
9756 /* The following variables hold the attributes of the properties as
9757 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9758 seen. When we see an attribute, we set them to 'true' (if they
9759 are boolean properties) or to the identifier (if they have an
9760 argument, ie, for getter and setter). Note that here we only
9761 parse the list of attributes, check the syntax and accumulate the
9762 attributes that we find. objc_add_property_declaration() will
9763 then process the information. */
9764 bool property_assign = false;
9765 bool property_copy = false;
9766 tree property_getter_ident = NULL_TREE;
9767 bool property_nonatomic = false;
9768 bool property_readonly = false;
9769 bool property_readwrite = false;
9770 bool property_retain = false;
9771 tree property_setter_ident = NULL_TREE;
9773 /* 'properties' is the list of properties that we read. Usually a
9774 single one, but maybe more (eg, in "@property int a, b, c;" there
9775 are three). */
9776 tree properties;
9777 location_t loc;
9779 loc = c_parser_peek_token (parser)->location;
9780 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9782 c_parser_consume_token (parser); /* Eat '@property'. */
9784 /* Parse the optional attribute list... */
9785 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9787 /* Eat the '(' */
9788 c_parser_consume_token (parser);
9790 /* Property attribute keywords are valid now. */
9791 parser->objc_property_attr_context = true;
9793 while (true)
9795 bool syntax_error = false;
9796 c_token *token = c_parser_peek_token (parser);
9797 enum rid keyword;
9799 if (token->type != CPP_KEYWORD)
9801 if (token->type == CPP_CLOSE_PAREN)
9802 c_parser_error (parser, "expected identifier");
9803 else
9805 c_parser_consume_token (parser);
9806 c_parser_error (parser, "unknown property attribute");
9808 break;
9810 keyword = token->keyword;
9811 c_parser_consume_token (parser);
9812 switch (keyword)
9814 case RID_ASSIGN: property_assign = true; break;
9815 case RID_COPY: property_copy = true; break;
9816 case RID_NONATOMIC: property_nonatomic = true; break;
9817 case RID_READONLY: property_readonly = true; break;
9818 case RID_READWRITE: property_readwrite = true; break;
9819 case RID_RETAIN: property_retain = true; break;
9821 case RID_GETTER:
9822 case RID_SETTER:
9823 if (c_parser_next_token_is_not (parser, CPP_EQ))
9825 if (keyword == RID_GETTER)
9826 c_parser_error (parser,
9827 "missing %<=%> (after %<getter%> attribute)");
9828 else
9829 c_parser_error (parser,
9830 "missing %<=%> (after %<setter%> attribute)");
9831 syntax_error = true;
9832 break;
9834 c_parser_consume_token (parser); /* eat the = */
9835 if (c_parser_next_token_is_not (parser, CPP_NAME))
9837 c_parser_error (parser, "expected identifier");
9838 syntax_error = true;
9839 break;
9841 if (keyword == RID_SETTER)
9843 if (property_setter_ident != NULL_TREE)
9844 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9845 else
9846 property_setter_ident = c_parser_peek_token (parser)->value;
9847 c_parser_consume_token (parser);
9848 if (c_parser_next_token_is_not (parser, CPP_COLON))
9849 c_parser_error (parser, "setter name must terminate with %<:%>");
9850 else
9851 c_parser_consume_token (parser);
9853 else
9855 if (property_getter_ident != NULL_TREE)
9856 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9857 else
9858 property_getter_ident = c_parser_peek_token (parser)->value;
9859 c_parser_consume_token (parser);
9861 break;
9862 default:
9863 c_parser_error (parser, "unknown property attribute");
9864 syntax_error = true;
9865 break;
9868 if (syntax_error)
9869 break;
9871 if (c_parser_next_token_is (parser, CPP_COMMA))
9872 c_parser_consume_token (parser);
9873 else
9874 break;
9876 parser->objc_property_attr_context = false;
9877 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9879 /* ... and the property declaration(s). */
9880 properties = c_parser_struct_declaration (parser);
9882 if (properties == error_mark_node)
9884 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9885 parser->error = false;
9886 return;
9889 if (properties == NULL_TREE)
9890 c_parser_error (parser, "expected identifier");
9891 else
9893 /* Comma-separated properties are chained together in
9894 reverse order; add them one by one. */
9895 properties = nreverse (properties);
9897 for (; properties; properties = TREE_CHAIN (properties))
9898 objc_add_property_declaration (loc, copy_node (properties),
9899 property_readonly, property_readwrite,
9900 property_assign, property_retain,
9901 property_copy, property_nonatomic,
9902 property_getter_ident, property_setter_ident);
9905 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9906 parser->error = false;
9909 /* Parse an Objective-C @synthesize declaration. The syntax is:
9911 objc-synthesize-declaration:
9912 @synthesize objc-synthesize-identifier-list ;
9914 objc-synthesize-identifier-list:
9915 objc-synthesize-identifier
9916 objc-synthesize-identifier-list, objc-synthesize-identifier
9918 objc-synthesize-identifier
9919 identifier
9920 identifier = identifier
9922 For example:
9923 @synthesize MyProperty;
9924 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9926 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9927 for C++. Keep them in sync.
9929 static void
9930 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9932 tree list = NULL_TREE;
9933 location_t loc;
9934 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9935 loc = c_parser_peek_token (parser)->location;
9937 c_parser_consume_token (parser);
9938 while (true)
9940 tree property, ivar;
9941 if (c_parser_next_token_is_not (parser, CPP_NAME))
9943 c_parser_error (parser, "expected identifier");
9944 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9945 /* Once we find the semicolon, we can resume normal parsing.
9946 We have to reset parser->error manually because
9947 c_parser_skip_until_found() won't reset it for us if the
9948 next token is precisely a semicolon. */
9949 parser->error = false;
9950 return;
9952 property = c_parser_peek_token (parser)->value;
9953 c_parser_consume_token (parser);
9954 if (c_parser_next_token_is (parser, CPP_EQ))
9956 c_parser_consume_token (parser);
9957 if (c_parser_next_token_is_not (parser, CPP_NAME))
9959 c_parser_error (parser, "expected identifier");
9960 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9961 parser->error = false;
9962 return;
9964 ivar = c_parser_peek_token (parser)->value;
9965 c_parser_consume_token (parser);
9967 else
9968 ivar = NULL_TREE;
9969 list = chainon (list, build_tree_list (ivar, property));
9970 if (c_parser_next_token_is (parser, CPP_COMMA))
9971 c_parser_consume_token (parser);
9972 else
9973 break;
9975 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9976 objc_add_synthesize_declaration (loc, list);
9979 /* Parse an Objective-C @dynamic declaration. The syntax is:
9981 objc-dynamic-declaration:
9982 @dynamic identifier-list ;
9984 For example:
9985 @dynamic MyProperty;
9986 @dynamic MyProperty, AnotherProperty;
9988 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9989 for C++. Keep them in sync.
9991 static void
9992 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9994 tree list = NULL_TREE;
9995 location_t loc;
9996 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9997 loc = c_parser_peek_token (parser)->location;
9999 c_parser_consume_token (parser);
10000 while (true)
10002 tree property;
10003 if (c_parser_next_token_is_not (parser, CPP_NAME))
10005 c_parser_error (parser, "expected identifier");
10006 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10007 parser->error = false;
10008 return;
10010 property = c_parser_peek_token (parser)->value;
10011 list = chainon (list, build_tree_list (NULL_TREE, property));
10012 c_parser_consume_token (parser);
10013 if (c_parser_next_token_is (parser, CPP_COMMA))
10014 c_parser_consume_token (parser);
10015 else
10016 break;
10018 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10019 objc_add_dynamic_declaration (loc, list);
10023 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10024 should be considered, statements. ALLOW_STMT is true if we're within
10025 the context of a function and such pragmas are to be allowed. Returns
10026 true if we actually parsed such a pragma. */
10028 static bool
10029 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10031 unsigned int id;
10033 id = c_parser_peek_token (parser)->pragma_kind;
10034 gcc_assert (id != PRAGMA_NONE);
10036 switch (id)
10038 case PRAGMA_OACC_DECLARE:
10039 c_parser_oacc_declare (parser);
10040 return false;
10042 case PRAGMA_OACC_ENTER_DATA:
10043 c_parser_oacc_enter_exit_data (parser, true);
10044 return false;
10046 case PRAGMA_OACC_EXIT_DATA:
10047 c_parser_oacc_enter_exit_data (parser, false);
10048 return false;
10050 case PRAGMA_OACC_ROUTINE:
10051 c_parser_oacc_routine (parser, context);
10052 return false;
10054 case PRAGMA_OACC_UPDATE:
10055 if (context != pragma_compound)
10057 if (context == pragma_stmt)
10058 c_parser_error (parser, "%<#pragma acc update%> may only be "
10059 "used in compound statements");
10060 goto bad_stmt;
10062 c_parser_oacc_update (parser);
10063 return false;
10065 case PRAGMA_OMP_BARRIER:
10066 if (context != pragma_compound)
10068 if (context == pragma_stmt)
10069 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
10070 "used in compound statements");
10071 goto bad_stmt;
10073 c_parser_omp_barrier (parser);
10074 return false;
10076 case PRAGMA_OMP_FLUSH:
10077 if (context != pragma_compound)
10079 if (context == pragma_stmt)
10080 c_parser_error (parser, "%<#pragma omp flush%> may only be "
10081 "used in compound statements");
10082 goto bad_stmt;
10084 c_parser_omp_flush (parser);
10085 return false;
10087 case PRAGMA_OMP_TASKWAIT:
10088 if (context != pragma_compound)
10090 if (context == pragma_stmt)
10091 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
10092 "used in compound statements");
10093 goto bad_stmt;
10095 c_parser_omp_taskwait (parser);
10096 return false;
10098 case PRAGMA_OMP_TASKYIELD:
10099 if (context != pragma_compound)
10101 if (context == pragma_stmt)
10102 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
10103 "used in compound statements");
10104 goto bad_stmt;
10106 c_parser_omp_taskyield (parser);
10107 return false;
10109 case PRAGMA_OMP_CANCEL:
10110 if (context != pragma_compound)
10112 if (context == pragma_stmt)
10113 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
10114 "used in compound statements");
10115 goto bad_stmt;
10117 c_parser_omp_cancel (parser);
10118 return false;
10120 case PRAGMA_OMP_CANCELLATION_POINT:
10121 if (context != pragma_compound)
10123 if (context == pragma_stmt)
10124 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
10125 "only be used in compound statements");
10126 goto bad_stmt;
10128 c_parser_omp_cancellation_point (parser);
10129 return false;
10131 case PRAGMA_OMP_THREADPRIVATE:
10132 c_parser_omp_threadprivate (parser);
10133 return false;
10135 case PRAGMA_OMP_TARGET:
10136 return c_parser_omp_target (parser, context, if_p);
10138 case PRAGMA_OMP_END_DECLARE_TARGET:
10139 c_parser_omp_end_declare_target (parser);
10140 return false;
10142 case PRAGMA_OMP_SECTION:
10143 error_at (c_parser_peek_token (parser)->location,
10144 "%<#pragma omp section%> may only be used in "
10145 "%<#pragma omp sections%> construct");
10146 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10147 return false;
10149 case PRAGMA_OMP_DECLARE_REDUCTION:
10150 c_parser_omp_declare (parser, context);
10151 return false;
10153 case PRAGMA_OMP_ORDERED:
10154 return c_parser_omp_ordered (parser, context, if_p);
10156 case PRAGMA_IVDEP:
10157 c_parser_consume_pragma (parser);
10158 c_parser_skip_to_pragma_eol (parser);
10159 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10160 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10161 && !c_parser_next_token_is_keyword (parser, RID_DO))
10163 c_parser_error (parser, "for, while or do statement expected");
10164 return false;
10166 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10167 c_parser_for_statement (parser, true, if_p);
10168 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10169 c_parser_while_statement (parser, true, if_p);
10170 else
10171 c_parser_do_statement (parser, true);
10172 return false;
10174 case PRAGMA_GCC_PCH_PREPROCESS:
10175 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10176 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10177 return false;
10179 case PRAGMA_CILK_SIMD:
10180 if (!c_parser_cilk_verify_simd (parser, context))
10181 return false;
10182 c_parser_consume_pragma (parser);
10183 c_parser_cilk_simd (parser, if_p);
10184 return false;
10185 case PRAGMA_CILK_GRAINSIZE:
10186 if (!flag_cilkplus)
10188 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10189 " enabled");
10190 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10191 return false;
10193 if (context == pragma_external)
10195 error_at (c_parser_peek_token (parser)->location,
10196 "%<#pragma grainsize%> must be inside a function");
10197 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10198 return false;
10200 c_parser_cilk_grainsize (parser, if_p);
10201 return false;
10203 default:
10204 if (id < PRAGMA_FIRST_EXTERNAL)
10206 if (context != pragma_stmt && context != pragma_compound)
10208 bad_stmt:
10209 c_parser_error (parser, "expected declaration specifiers");
10210 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10211 return false;
10213 c_parser_omp_construct (parser, if_p);
10214 return true;
10216 break;
10219 c_parser_consume_pragma (parser);
10220 c_invoke_pragma_handler (id);
10222 /* Skip to EOL, but suppress any error message. Those will have been
10223 generated by the handler routine through calling error, as opposed
10224 to calling c_parser_error. */
10225 parser->error = true;
10226 c_parser_skip_to_pragma_eol (parser);
10228 return false;
10231 /* The interface the pragma parsers have to the lexer. */
10233 enum cpp_ttype
10234 pragma_lex (tree *value, location_t *loc)
10236 c_token *tok = c_parser_peek_token (the_parser);
10237 enum cpp_ttype ret = tok->type;
10239 *value = tok->value;
10240 if (loc)
10241 *loc = tok->location;
10243 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10244 ret = CPP_EOF;
10245 else
10247 if (ret == CPP_KEYWORD)
10248 ret = CPP_NAME;
10249 c_parser_consume_token (the_parser);
10252 return ret;
10255 static void
10256 c_parser_pragma_pch_preprocess (c_parser *parser)
10258 tree name = NULL;
10260 c_parser_consume_pragma (parser);
10261 if (c_parser_next_token_is (parser, CPP_STRING))
10263 name = c_parser_peek_token (parser)->value;
10264 c_parser_consume_token (parser);
10266 else
10267 c_parser_error (parser, "expected string literal");
10268 c_parser_skip_to_pragma_eol (parser);
10270 if (name)
10271 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10274 /* OpenACC and OpenMP parsing routines. */
10276 /* Returns name of the next clause.
10277 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10278 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10279 returned and the token is consumed. */
10281 static pragma_omp_clause
10282 c_parser_omp_clause_name (c_parser *parser)
10284 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10286 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10287 result = PRAGMA_OACC_CLAUSE_AUTO;
10288 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10289 result = PRAGMA_OMP_CLAUSE_IF;
10290 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10291 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10292 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10293 result = PRAGMA_OMP_CLAUSE_FOR;
10294 else if (c_parser_next_token_is (parser, CPP_NAME))
10296 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10298 switch (p[0])
10300 case 'a':
10301 if (!strcmp ("aligned", p))
10302 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10303 else if (!strcmp ("async", p))
10304 result = PRAGMA_OACC_CLAUSE_ASYNC;
10305 break;
10306 case 'c':
10307 if (!strcmp ("collapse", p))
10308 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10309 else if (!strcmp ("copy", p))
10310 result = PRAGMA_OACC_CLAUSE_COPY;
10311 else if (!strcmp ("copyin", p))
10312 result = PRAGMA_OMP_CLAUSE_COPYIN;
10313 else if (!strcmp ("copyout", p))
10314 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10315 else if (!strcmp ("copyprivate", p))
10316 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10317 else if (!strcmp ("create", p))
10318 result = PRAGMA_OACC_CLAUSE_CREATE;
10319 break;
10320 case 'd':
10321 if (!strcmp ("defaultmap", p))
10322 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10323 else if (!strcmp ("delete", p))
10324 result = PRAGMA_OACC_CLAUSE_DELETE;
10325 else if (!strcmp ("depend", p))
10326 result = PRAGMA_OMP_CLAUSE_DEPEND;
10327 else if (!strcmp ("device", p))
10328 result = PRAGMA_OMP_CLAUSE_DEVICE;
10329 else if (!strcmp ("deviceptr", p))
10330 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10331 else if (!strcmp ("device_resident", p))
10332 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10333 else if (!strcmp ("dist_schedule", p))
10334 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10335 break;
10336 case 'f':
10337 if (!strcmp ("final", p))
10338 result = PRAGMA_OMP_CLAUSE_FINAL;
10339 else if (!strcmp ("firstprivate", p))
10340 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10341 else if (!strcmp ("from", p))
10342 result = PRAGMA_OMP_CLAUSE_FROM;
10343 break;
10344 case 'g':
10345 if (!strcmp ("gang", p))
10346 result = PRAGMA_OACC_CLAUSE_GANG;
10347 else if (!strcmp ("grainsize", p))
10348 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10349 break;
10350 case 'h':
10351 if (!strcmp ("hint", p))
10352 result = PRAGMA_OMP_CLAUSE_HINT;
10353 else if (!strcmp ("host", p))
10354 result = PRAGMA_OACC_CLAUSE_HOST;
10355 break;
10356 case 'i':
10357 if (!strcmp ("inbranch", p))
10358 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10359 else if (!strcmp ("independent", p))
10360 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10361 else if (!strcmp ("is_device_ptr", p))
10362 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10363 break;
10364 case 'l':
10365 if (!strcmp ("lastprivate", p))
10366 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10367 else if (!strcmp ("linear", p))
10368 result = PRAGMA_OMP_CLAUSE_LINEAR;
10369 else if (!strcmp ("link", p))
10370 result = PRAGMA_OMP_CLAUSE_LINK;
10371 break;
10372 case 'm':
10373 if (!strcmp ("map", p))
10374 result = PRAGMA_OMP_CLAUSE_MAP;
10375 else if (!strcmp ("mergeable", p))
10376 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10377 else if (flag_cilkplus && !strcmp ("mask", p))
10378 result = PRAGMA_CILK_CLAUSE_MASK;
10379 break;
10380 case 'n':
10381 if (!strcmp ("nogroup", p))
10382 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10383 else if (!strcmp ("notinbranch", p))
10384 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10385 else if (!strcmp ("nowait", p))
10386 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10387 else if (!strcmp ("num_gangs", p))
10388 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10389 else if (!strcmp ("num_tasks", p))
10390 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10391 else if (!strcmp ("num_teams", p))
10392 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10393 else if (!strcmp ("num_threads", p))
10394 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10395 else if (!strcmp ("num_workers", p))
10396 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10397 else if (flag_cilkplus && !strcmp ("nomask", p))
10398 result = PRAGMA_CILK_CLAUSE_NOMASK;
10399 break;
10400 case 'o':
10401 if (!strcmp ("ordered", p))
10402 result = PRAGMA_OMP_CLAUSE_ORDERED;
10403 break;
10404 case 'p':
10405 if (!strcmp ("parallel", p))
10406 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10407 else if (!strcmp ("present", p))
10408 result = PRAGMA_OACC_CLAUSE_PRESENT;
10409 else if (!strcmp ("present_or_copy", p)
10410 || !strcmp ("pcopy", p))
10411 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10412 else if (!strcmp ("present_or_copyin", p)
10413 || !strcmp ("pcopyin", p))
10414 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10415 else if (!strcmp ("present_or_copyout", p)
10416 || !strcmp ("pcopyout", p))
10417 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10418 else if (!strcmp ("present_or_create", p)
10419 || !strcmp ("pcreate", p))
10420 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10421 else if (!strcmp ("priority", p))
10422 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10423 else if (!strcmp ("private", p))
10424 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10425 else if (!strcmp ("proc_bind", p))
10426 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10427 break;
10428 case 'r':
10429 if (!strcmp ("reduction", p))
10430 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10431 break;
10432 case 's':
10433 if (!strcmp ("safelen", p))
10434 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10435 else if (!strcmp ("schedule", p))
10436 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10437 else if (!strcmp ("sections", p))
10438 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10439 else if (!strcmp ("seq", p))
10440 result = PRAGMA_OACC_CLAUSE_SEQ;
10441 else if (!strcmp ("shared", p))
10442 result = PRAGMA_OMP_CLAUSE_SHARED;
10443 else if (!strcmp ("simd", p))
10444 result = PRAGMA_OMP_CLAUSE_SIMD;
10445 else if (!strcmp ("simdlen", p))
10446 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10447 else if (!strcmp ("self", p))
10448 result = PRAGMA_OACC_CLAUSE_SELF;
10449 break;
10450 case 't':
10451 if (!strcmp ("taskgroup", p))
10452 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10453 else if (!strcmp ("thread_limit", p))
10454 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10455 else if (!strcmp ("threads", p))
10456 result = PRAGMA_OMP_CLAUSE_THREADS;
10457 else if (!strcmp ("tile", p))
10458 result = PRAGMA_OACC_CLAUSE_TILE;
10459 else if (!strcmp ("to", p))
10460 result = PRAGMA_OMP_CLAUSE_TO;
10461 break;
10462 case 'u':
10463 if (!strcmp ("uniform", p))
10464 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10465 else if (!strcmp ("untied", p))
10466 result = PRAGMA_OMP_CLAUSE_UNTIED;
10467 else if (!strcmp ("use_device", p))
10468 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10469 else if (!strcmp ("use_device_ptr", p))
10470 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10471 break;
10472 case 'v':
10473 if (!strcmp ("vector", p))
10474 result = PRAGMA_OACC_CLAUSE_VECTOR;
10475 else if (!strcmp ("vector_length", p))
10476 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10477 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10478 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10479 break;
10480 case 'w':
10481 if (!strcmp ("wait", p))
10482 result = PRAGMA_OACC_CLAUSE_WAIT;
10483 else if (!strcmp ("worker", p))
10484 result = PRAGMA_OACC_CLAUSE_WORKER;
10485 break;
10489 if (result != PRAGMA_OMP_CLAUSE_NONE)
10490 c_parser_consume_token (parser);
10492 return result;
10495 /* Validate that a clause of the given type does not already exist. */
10497 static void
10498 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10499 const char *name)
10501 tree c;
10503 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10504 if (OMP_CLAUSE_CODE (c) == code)
10506 location_t loc = OMP_CLAUSE_LOCATION (c);
10507 error_at (loc, "too many %qs clauses", name);
10508 break;
10512 /* OpenACC 2.0
10513 Parse wait clause or wait directive parameters. */
10515 static tree
10516 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10518 vec<tree, va_gc> *args;
10519 tree t, args_tree;
10521 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10522 return list;
10524 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10526 if (args->length () == 0)
10528 c_parser_error (parser, "expected integer expression before ')'");
10529 release_tree_vector (args);
10530 return list;
10533 args_tree = build_tree_list_vec (args);
10535 for (t = args_tree; t; t = TREE_CHAIN (t))
10537 tree targ = TREE_VALUE (t);
10539 if (targ != error_mark_node)
10541 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10543 c_parser_error (parser, "expression must be integral");
10544 targ = error_mark_node;
10546 else
10548 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10550 OMP_CLAUSE_DECL (c) = targ;
10551 OMP_CLAUSE_CHAIN (c) = list;
10552 list = c;
10557 release_tree_vector (args);
10558 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10559 return list;
10562 /* OpenACC 2.0, OpenMP 2.5:
10563 variable-list:
10564 identifier
10565 variable-list , identifier
10567 If KIND is nonzero, create the appropriate node and install the
10568 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10569 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10571 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10572 return the list created. */
10574 static tree
10575 c_parser_omp_variable_list (c_parser *parser,
10576 location_t clause_loc,
10577 enum omp_clause_code kind, tree list)
10579 if (c_parser_next_token_is_not (parser, CPP_NAME)
10580 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10581 c_parser_error (parser, "expected identifier");
10583 while (c_parser_next_token_is (parser, CPP_NAME)
10584 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10586 tree t = lookup_name (c_parser_peek_token (parser)->value);
10588 if (t == NULL_TREE)
10590 undeclared_variable (c_parser_peek_token (parser)->location,
10591 c_parser_peek_token (parser)->value);
10592 t = error_mark_node;
10595 c_parser_consume_token (parser);
10597 if (t == error_mark_node)
10599 else if (kind != 0)
10601 switch (kind)
10603 case OMP_CLAUSE__CACHE_:
10604 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10606 c_parser_error (parser, "expected %<[%>");
10607 t = error_mark_node;
10608 break;
10610 /* FALLTHROUGH */
10611 case OMP_CLAUSE_MAP:
10612 case OMP_CLAUSE_FROM:
10613 case OMP_CLAUSE_TO:
10614 while (c_parser_next_token_is (parser, CPP_DOT))
10616 location_t op_loc = c_parser_peek_token (parser)->location;
10617 c_parser_consume_token (parser);
10618 if (!c_parser_next_token_is (parser, CPP_NAME))
10620 c_parser_error (parser, "expected identifier");
10621 t = error_mark_node;
10622 break;
10624 tree ident = c_parser_peek_token (parser)->value;
10625 c_parser_consume_token (parser);
10626 t = build_component_ref (op_loc, t, ident);
10628 /* FALLTHROUGH */
10629 case OMP_CLAUSE_DEPEND:
10630 case OMP_CLAUSE_REDUCTION:
10631 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10633 tree low_bound = NULL_TREE, length = NULL_TREE;
10635 c_parser_consume_token (parser);
10636 if (!c_parser_next_token_is (parser, CPP_COLON))
10638 low_bound = c_parser_expression (parser).value;
10639 mark_exp_read (low_bound);
10641 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10642 length = integer_one_node;
10643 else
10645 /* Look for `:'. */
10646 if (!c_parser_require (parser, CPP_COLON,
10647 "expected %<:%>"))
10649 t = error_mark_node;
10650 break;
10652 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10654 length = c_parser_expression (parser).value;
10655 mark_exp_read (length);
10658 /* Look for the closing `]'. */
10659 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10660 "expected %<]%>"))
10662 t = error_mark_node;
10663 break;
10666 if (kind == OMP_CLAUSE__CACHE_)
10668 if (TREE_CODE (low_bound) != INTEGER_CST
10669 && !TREE_READONLY (low_bound))
10671 error_at (clause_loc,
10672 "%qD is not a constant", low_bound);
10673 t = error_mark_node;
10676 if (TREE_CODE (length) != INTEGER_CST
10677 && !TREE_READONLY (length))
10679 error_at (clause_loc,
10680 "%qD is not a constant", length);
10681 t = error_mark_node;
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)
12131 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12132 else
12133 c_parser_error (parser, "expected integer expression");
12135 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12137 else
12138 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12139 "expected %<,%> or %<)%>");
12141 OMP_CLAUSE_SCHEDULE_KIND (c)
12142 = (enum omp_clause_schedule_kind)
12143 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12145 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12146 OMP_CLAUSE_CHAIN (c) = list;
12147 return c;
12149 invalid_kind:
12150 c_parser_error (parser, "invalid schedule kind");
12151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12152 return list;
12155 /* OpenMP 2.5:
12156 shared ( variable-list ) */
12158 static tree
12159 c_parser_omp_clause_shared (c_parser *parser, tree list)
12161 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12164 /* OpenMP 3.0:
12165 untied */
12167 static tree
12168 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12170 tree c;
12172 /* FIXME: Should we allow duplicates? */
12173 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12175 c = build_omp_clause (c_parser_peek_token (parser)->location,
12176 OMP_CLAUSE_UNTIED);
12177 OMP_CLAUSE_CHAIN (c) = list;
12179 return c;
12182 /* OpenACC:
12183 vector_length ( expression ) */
12185 static tree
12186 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12188 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12189 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12191 location_t expr_loc = c_parser_peek_token (parser)->location;
12192 tree c, t = c_parser_expression (parser).value;
12193 mark_exp_read (t);
12194 t = c_fully_fold (t, false, NULL);
12196 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12198 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12200 c_parser_error (parser, "expected integer expression");
12201 return list;
12204 /* Attempt to statically determine when the number isn't positive. */
12205 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12206 build_int_cst (TREE_TYPE (t), 0));
12207 protected_set_expr_location (c, expr_loc);
12208 if (c == boolean_true_node)
12210 warning_at (expr_loc, 0,
12211 "%<vector_length%> value must be positive");
12212 t = integer_one_node;
12215 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12217 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12218 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12219 OMP_CLAUSE_CHAIN (c) = list;
12220 list = c;
12223 return list;
12226 /* OpenMP 4.0:
12227 inbranch
12228 notinbranch */
12230 static tree
12231 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12232 enum omp_clause_code code, tree list)
12234 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12236 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12237 OMP_CLAUSE_CHAIN (c) = list;
12239 return c;
12242 /* OpenMP 4.0:
12243 parallel
12245 sections
12246 taskgroup */
12248 static tree
12249 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12250 enum omp_clause_code code, tree list)
12252 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12253 OMP_CLAUSE_CHAIN (c) = list;
12255 return c;
12258 /* OpenMP 4.5:
12259 nogroup */
12261 static tree
12262 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12264 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12265 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12266 OMP_CLAUSE_NOGROUP);
12267 OMP_CLAUSE_CHAIN (c) = list;
12268 return c;
12271 /* OpenMP 4.5:
12272 simd
12273 threads */
12275 static tree
12276 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12277 enum omp_clause_code code, tree list)
12279 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12280 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12281 OMP_CLAUSE_CHAIN (c) = list;
12282 return c;
12285 /* OpenMP 4.0:
12286 num_teams ( expression ) */
12288 static tree
12289 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12291 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12292 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12294 location_t expr_loc = c_parser_peek_token (parser)->location;
12295 tree c, t = c_parser_expression (parser).value;
12296 mark_exp_read (t);
12297 t = c_fully_fold (t, false, NULL);
12299 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12301 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12303 c_parser_error (parser, "expected integer expression");
12304 return list;
12307 /* Attempt to statically determine when the number isn't positive. */
12308 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12309 build_int_cst (TREE_TYPE (t), 0));
12310 protected_set_expr_location (c, expr_loc);
12311 if (c == boolean_true_node)
12313 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12314 t = integer_one_node;
12317 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12319 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12320 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12321 OMP_CLAUSE_CHAIN (c) = list;
12322 list = c;
12325 return list;
12328 /* OpenMP 4.0:
12329 thread_limit ( expression ) */
12331 static tree
12332 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12334 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12335 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12337 location_t expr_loc = c_parser_peek_token (parser)->location;
12338 tree c, t = c_parser_expression (parser).value;
12339 mark_exp_read (t);
12340 t = c_fully_fold (t, false, NULL);
12342 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12344 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12346 c_parser_error (parser, "expected integer expression");
12347 return list;
12350 /* Attempt to statically determine when the number isn't positive. */
12351 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12352 build_int_cst (TREE_TYPE (t), 0));
12353 protected_set_expr_location (c, expr_loc);
12354 if (c == boolean_true_node)
12356 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12357 t = integer_one_node;
12360 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12361 "thread_limit");
12363 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12364 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12365 OMP_CLAUSE_CHAIN (c) = list;
12366 list = c;
12369 return list;
12372 /* OpenMP 4.0:
12373 aligned ( variable-list )
12374 aligned ( variable-list : constant-expression ) */
12376 static tree
12377 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12379 location_t clause_loc = c_parser_peek_token (parser)->location;
12380 tree nl, c;
12382 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12383 return list;
12385 nl = c_parser_omp_variable_list (parser, clause_loc,
12386 OMP_CLAUSE_ALIGNED, list);
12388 if (c_parser_next_token_is (parser, CPP_COLON))
12390 c_parser_consume_token (parser);
12391 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
12392 mark_exp_read (alignment);
12393 alignment = c_fully_fold (alignment, false, NULL);
12394 if (TREE_CODE (alignment) != INTEGER_CST
12395 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12396 || tree_int_cst_sgn (alignment) != 1)
12398 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12399 "be positive constant integer expression");
12400 alignment = NULL_TREE;
12403 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12404 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12407 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12408 return nl;
12411 /* OpenMP 4.0:
12412 linear ( variable-list )
12413 linear ( variable-list : expression )
12415 OpenMP 4.5:
12416 linear ( modifier ( variable-list ) )
12417 linear ( modifier ( variable-list ) : expression ) */
12419 static tree
12420 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12422 location_t clause_loc = c_parser_peek_token (parser)->location;
12423 tree nl, c, step;
12424 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12426 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12427 return list;
12429 if (!is_cilk_simd_fn
12430 && c_parser_next_token_is (parser, CPP_NAME))
12432 c_token *tok = c_parser_peek_token (parser);
12433 const char *p = IDENTIFIER_POINTER (tok->value);
12434 if (strcmp ("val", p) == 0)
12435 kind = OMP_CLAUSE_LINEAR_VAL;
12436 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12437 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12438 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12440 c_parser_consume_token (parser);
12441 c_parser_consume_token (parser);
12445 nl = c_parser_omp_variable_list (parser, clause_loc,
12446 OMP_CLAUSE_LINEAR, list);
12448 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12449 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12451 if (c_parser_next_token_is (parser, CPP_COLON))
12453 c_parser_consume_token (parser);
12454 step = c_parser_expression (parser).value;
12455 mark_exp_read (step);
12456 step = c_fully_fold (step, false, NULL);
12457 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12459 sorry ("using parameters for %<linear%> step is not supported yet");
12460 step = integer_one_node;
12462 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12464 error_at (clause_loc, "%<linear%> clause step expression must "
12465 "be integral");
12466 step = integer_one_node;
12470 else
12471 step = integer_one_node;
12473 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12475 OMP_CLAUSE_LINEAR_STEP (c) = step;
12476 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12479 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12480 return nl;
12483 /* OpenMP 4.0:
12484 safelen ( constant-expression ) */
12486 static tree
12487 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12489 location_t clause_loc = c_parser_peek_token (parser)->location;
12490 tree c, t;
12492 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12493 return list;
12495 t = c_parser_expr_no_commas (parser, NULL).value;
12496 mark_exp_read (t);
12497 t = c_fully_fold (t, false, NULL);
12498 if (TREE_CODE (t) != INTEGER_CST
12499 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12500 || tree_int_cst_sgn (t) != 1)
12502 error_at (clause_loc, "%<safelen%> clause expression must "
12503 "be positive constant integer expression");
12504 t = NULL_TREE;
12507 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12508 if (t == NULL_TREE || t == error_mark_node)
12509 return list;
12511 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12513 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12514 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12515 OMP_CLAUSE_CHAIN (c) = list;
12516 return c;
12519 /* OpenMP 4.0:
12520 simdlen ( constant-expression ) */
12522 static tree
12523 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12525 location_t clause_loc = c_parser_peek_token (parser)->location;
12526 tree c, t;
12528 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12529 return list;
12531 t = c_parser_expr_no_commas (parser, NULL).value;
12532 mark_exp_read (t);
12533 t = c_fully_fold (t, false, NULL);
12534 if (TREE_CODE (t) != INTEGER_CST
12535 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12536 || tree_int_cst_sgn (t) != 1)
12538 error_at (clause_loc, "%<simdlen%> clause expression must "
12539 "be positive constant integer expression");
12540 t = NULL_TREE;
12543 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12544 if (t == NULL_TREE || t == error_mark_node)
12545 return list;
12547 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12549 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12550 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12551 OMP_CLAUSE_CHAIN (c) = list;
12552 return c;
12555 /* OpenMP 4.5:
12556 vec:
12557 identifier [+/- integer]
12558 vec , identifier [+/- integer]
12561 static tree
12562 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12563 tree list)
12565 tree vec = NULL;
12566 if (c_parser_next_token_is_not (parser, CPP_NAME)
12567 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12569 c_parser_error (parser, "expected identifier");
12570 return list;
12573 while (c_parser_next_token_is (parser, CPP_NAME)
12574 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12576 tree t = lookup_name (c_parser_peek_token (parser)->value);
12577 tree addend = NULL;
12579 if (t == NULL_TREE)
12581 undeclared_variable (c_parser_peek_token (parser)->location,
12582 c_parser_peek_token (parser)->value);
12583 t = error_mark_node;
12586 c_parser_consume_token (parser);
12588 bool neg = false;
12589 if (c_parser_next_token_is (parser, CPP_MINUS))
12590 neg = true;
12591 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12593 addend = integer_zero_node;
12594 neg = false;
12595 goto add_to_vector;
12597 c_parser_consume_token (parser);
12599 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12601 c_parser_error (parser, "expected integer");
12602 return list;
12605 addend = c_parser_peek_token (parser)->value;
12606 if (TREE_CODE (addend) != INTEGER_CST)
12608 c_parser_error (parser, "expected integer");
12609 return list;
12611 c_parser_consume_token (parser);
12613 add_to_vector:
12614 if (t != error_mark_node)
12616 vec = tree_cons (addend, t, vec);
12617 if (neg)
12618 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12621 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12622 break;
12624 c_parser_consume_token (parser);
12627 if (vec == NULL_TREE)
12628 return list;
12630 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12631 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12632 OMP_CLAUSE_DECL (u) = nreverse (vec);
12633 OMP_CLAUSE_CHAIN (u) = list;
12634 return u;
12637 /* OpenMP 4.0:
12638 depend ( depend-kind: variable-list )
12640 depend-kind:
12641 in | out | inout
12643 OpenMP 4.5:
12644 depend ( source )
12646 depend ( sink : vec ) */
12648 static tree
12649 c_parser_omp_clause_depend (c_parser *parser, tree list)
12651 location_t clause_loc = c_parser_peek_token (parser)->location;
12652 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12653 tree nl, c;
12655 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12656 return list;
12658 if (c_parser_next_token_is (parser, CPP_NAME))
12660 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12661 if (strcmp ("in", p) == 0)
12662 kind = OMP_CLAUSE_DEPEND_IN;
12663 else if (strcmp ("inout", p) == 0)
12664 kind = OMP_CLAUSE_DEPEND_INOUT;
12665 else if (strcmp ("out", p) == 0)
12666 kind = OMP_CLAUSE_DEPEND_OUT;
12667 else if (strcmp ("source", p) == 0)
12668 kind = OMP_CLAUSE_DEPEND_SOURCE;
12669 else if (strcmp ("sink", p) == 0)
12670 kind = OMP_CLAUSE_DEPEND_SINK;
12671 else
12672 goto invalid_kind;
12674 else
12675 goto invalid_kind;
12677 c_parser_consume_token (parser);
12679 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12681 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12682 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12683 OMP_CLAUSE_DECL (c) = NULL_TREE;
12684 OMP_CLAUSE_CHAIN (c) = list;
12685 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12686 return c;
12689 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12690 goto resync_fail;
12692 if (kind == OMP_CLAUSE_DEPEND_SINK)
12693 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12694 else
12696 nl = c_parser_omp_variable_list (parser, clause_loc,
12697 OMP_CLAUSE_DEPEND, list);
12699 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12700 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12703 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12704 return nl;
12706 invalid_kind:
12707 c_parser_error (parser, "invalid depend kind");
12708 resync_fail:
12709 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12710 return list;
12713 /* OpenMP 4.0:
12714 map ( map-kind: variable-list )
12715 map ( variable-list )
12717 map-kind:
12718 alloc | to | from | tofrom
12720 OpenMP 4.5:
12721 map-kind:
12722 alloc | to | from | tofrom | release | delete
12724 map ( always [,] map-kind: variable-list ) */
12726 static tree
12727 c_parser_omp_clause_map (c_parser *parser, tree list)
12729 location_t clause_loc = c_parser_peek_token (parser)->location;
12730 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12731 int always = 0;
12732 enum c_id_kind always_id_kind = C_ID_NONE;
12733 location_t always_loc = UNKNOWN_LOCATION;
12734 tree always_id = NULL_TREE;
12735 tree nl, c;
12737 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12738 return list;
12740 if (c_parser_next_token_is (parser, CPP_NAME))
12742 c_token *tok = c_parser_peek_token (parser);
12743 const char *p = IDENTIFIER_POINTER (tok->value);
12744 always_id_kind = tok->id_kind;
12745 always_loc = tok->location;
12746 always_id = tok->value;
12747 if (strcmp ("always", p) == 0)
12749 c_token *sectok = c_parser_peek_2nd_token (parser);
12750 if (sectok->type == CPP_COMMA)
12752 c_parser_consume_token (parser);
12753 c_parser_consume_token (parser);
12754 always = 2;
12756 else if (sectok->type == CPP_NAME)
12758 p = IDENTIFIER_POINTER (sectok->value);
12759 if (strcmp ("alloc", p) == 0
12760 || strcmp ("to", p) == 0
12761 || strcmp ("from", p) == 0
12762 || strcmp ("tofrom", p) == 0
12763 || strcmp ("release", p) == 0
12764 || strcmp ("delete", p) == 0)
12766 c_parser_consume_token (parser);
12767 always = 1;
12773 if (c_parser_next_token_is (parser, CPP_NAME)
12774 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12776 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12777 if (strcmp ("alloc", p) == 0)
12778 kind = GOMP_MAP_ALLOC;
12779 else if (strcmp ("to", p) == 0)
12780 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12781 else if (strcmp ("from", p) == 0)
12782 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12783 else if (strcmp ("tofrom", p) == 0)
12784 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12785 else if (strcmp ("release", p) == 0)
12786 kind = GOMP_MAP_RELEASE;
12787 else if (strcmp ("delete", p) == 0)
12788 kind = GOMP_MAP_DELETE;
12789 else
12791 c_parser_error (parser, "invalid map kind");
12792 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12793 "expected %<)%>");
12794 return list;
12796 c_parser_consume_token (parser);
12797 c_parser_consume_token (parser);
12799 else if (always)
12801 if (always_id_kind != C_ID_ID)
12803 c_parser_error (parser, "expected identifier");
12804 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12805 return list;
12808 tree t = lookup_name (always_id);
12809 if (t == NULL_TREE)
12811 undeclared_variable (always_loc, always_id);
12812 t = error_mark_node;
12814 if (t != error_mark_node)
12816 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12817 OMP_CLAUSE_DECL (u) = t;
12818 OMP_CLAUSE_CHAIN (u) = list;
12819 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12820 list = u;
12822 if (always == 1)
12824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12825 return list;
12829 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
12831 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12832 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12834 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12835 return nl;
12838 /* OpenMP 4.0:
12839 device ( expression ) */
12841 static tree
12842 c_parser_omp_clause_device (c_parser *parser, tree list)
12844 location_t clause_loc = c_parser_peek_token (parser)->location;
12845 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12847 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
12848 mark_exp_read (t);
12849 t = c_fully_fold (t, false, NULL);
12851 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12853 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12855 c_parser_error (parser, "expected integer expression");
12856 return list;
12859 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
12861 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
12862 OMP_CLAUSE_DEVICE_ID (c) = t;
12863 OMP_CLAUSE_CHAIN (c) = list;
12864 list = c;
12867 return list;
12870 /* OpenMP 4.0:
12871 dist_schedule ( static )
12872 dist_schedule ( static , expression ) */
12874 static tree
12875 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
12877 tree c, t = NULL_TREE;
12878 location_t loc = c_parser_peek_token (parser)->location;
12880 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12881 return list;
12883 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
12885 c_parser_error (parser, "invalid dist_schedule kind");
12886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12887 "expected %<)%>");
12888 return list;
12891 c_parser_consume_token (parser);
12892 if (c_parser_next_token_is (parser, CPP_COMMA))
12894 c_parser_consume_token (parser);
12896 t = c_parser_expr_no_commas (parser, NULL).value;
12897 mark_exp_read (t);
12898 t = c_fully_fold (t, false, NULL);
12899 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12901 else
12902 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12903 "expected %<,%> or %<)%>");
12905 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12906 if (t == error_mark_node)
12907 return list;
12909 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
12910 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
12911 OMP_CLAUSE_CHAIN (c) = list;
12912 return c;
12915 /* OpenMP 4.0:
12916 proc_bind ( proc-bind-kind )
12918 proc-bind-kind:
12919 master | close | spread */
12921 static tree
12922 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
12924 location_t clause_loc = c_parser_peek_token (parser)->location;
12925 enum omp_clause_proc_bind_kind kind;
12926 tree c;
12928 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12929 return list;
12931 if (c_parser_next_token_is (parser, CPP_NAME))
12933 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12934 if (strcmp ("master", p) == 0)
12935 kind = OMP_CLAUSE_PROC_BIND_MASTER;
12936 else if (strcmp ("close", p) == 0)
12937 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
12938 else if (strcmp ("spread", p) == 0)
12939 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
12940 else
12941 goto invalid_kind;
12943 else
12944 goto invalid_kind;
12946 c_parser_consume_token (parser);
12947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12948 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
12949 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
12950 OMP_CLAUSE_CHAIN (c) = list;
12951 return c;
12953 invalid_kind:
12954 c_parser_error (parser, "invalid proc_bind kind");
12955 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12956 return list;
12959 /* OpenMP 4.0:
12960 to ( variable-list ) */
12962 static tree
12963 c_parser_omp_clause_to (c_parser *parser, tree list)
12965 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
12968 /* OpenMP 4.0:
12969 from ( variable-list ) */
12971 static tree
12972 c_parser_omp_clause_from (c_parser *parser, tree list)
12974 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
12977 /* OpenMP 4.0:
12978 uniform ( variable-list ) */
12980 static tree
12981 c_parser_omp_clause_uniform (c_parser *parser, tree list)
12983 /* The clauses location. */
12984 location_t loc = c_parser_peek_token (parser)->location;
12986 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12988 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
12989 list);
12990 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12992 return list;
12995 /* Parse all OpenACC clauses. The set clauses allowed by the directive
12996 is a bitmask in MASK. Return the list of clauses found. */
12998 static tree
12999 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13000 const char *where, bool finish_p = true)
13002 tree clauses = NULL;
13003 bool first = true;
13005 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13007 location_t here;
13008 pragma_omp_clause c_kind;
13009 const char *c_name;
13010 tree prev = clauses;
13012 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13013 c_parser_consume_token (parser);
13015 here = c_parser_peek_token (parser)->location;
13016 c_kind = c_parser_omp_clause_name (parser);
13018 switch (c_kind)
13020 case PRAGMA_OACC_CLAUSE_ASYNC:
13021 clauses = c_parser_oacc_clause_async (parser, clauses);
13022 c_name = "async";
13023 break;
13024 case PRAGMA_OACC_CLAUSE_AUTO:
13025 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13026 clauses);
13027 c_name = "auto";
13028 break;
13029 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13030 clauses = c_parser_omp_clause_collapse (parser, clauses);
13031 c_name = "collapse";
13032 break;
13033 case PRAGMA_OACC_CLAUSE_COPY:
13034 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13035 c_name = "copy";
13036 break;
13037 case PRAGMA_OACC_CLAUSE_COPYIN:
13038 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13039 c_name = "copyin";
13040 break;
13041 case PRAGMA_OACC_CLAUSE_COPYOUT:
13042 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13043 c_name = "copyout";
13044 break;
13045 case PRAGMA_OACC_CLAUSE_CREATE:
13046 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13047 c_name = "create";
13048 break;
13049 case PRAGMA_OACC_CLAUSE_DELETE:
13050 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13051 c_name = "delete";
13052 break;
13053 case PRAGMA_OMP_CLAUSE_DEFAULT:
13054 clauses = c_parser_omp_clause_default (parser, clauses, true);
13055 c_name = "default";
13056 break;
13057 case PRAGMA_OACC_CLAUSE_DEVICE:
13058 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13059 c_name = "device";
13060 break;
13061 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13062 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13063 c_name = "deviceptr";
13064 break;
13065 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13066 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13067 c_name = "device_resident";
13068 break;
13069 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13070 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13071 c_name = "firstprivate";
13072 break;
13073 case PRAGMA_OACC_CLAUSE_GANG:
13074 c_name = "gang";
13075 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13076 c_name, clauses);
13077 break;
13078 case PRAGMA_OACC_CLAUSE_HOST:
13079 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13080 c_name = "host";
13081 break;
13082 case PRAGMA_OACC_CLAUSE_IF:
13083 clauses = c_parser_omp_clause_if (parser, clauses, false);
13084 c_name = "if";
13085 break;
13086 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13087 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13088 clauses);
13089 c_name = "independent";
13090 break;
13091 case PRAGMA_OACC_CLAUSE_LINK:
13092 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13093 c_name = "link";
13094 break;
13095 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13096 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13097 c_name = "num_gangs";
13098 break;
13099 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13100 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13101 c_name = "num_workers";
13102 break;
13103 case PRAGMA_OACC_CLAUSE_PRESENT:
13104 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13105 c_name = "present";
13106 break;
13107 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13108 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13109 c_name = "present_or_copy";
13110 break;
13111 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13112 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13113 c_name = "present_or_copyin";
13114 break;
13115 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13116 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13117 c_name = "present_or_copyout";
13118 break;
13119 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13120 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13121 c_name = "present_or_create";
13122 break;
13123 case PRAGMA_OACC_CLAUSE_PRIVATE:
13124 clauses = c_parser_omp_clause_private (parser, clauses);
13125 c_name = "private";
13126 break;
13127 case PRAGMA_OACC_CLAUSE_REDUCTION:
13128 clauses = c_parser_omp_clause_reduction (parser, clauses);
13129 c_name = "reduction";
13130 break;
13131 case PRAGMA_OACC_CLAUSE_SELF:
13132 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13133 c_name = "self";
13134 break;
13135 case PRAGMA_OACC_CLAUSE_SEQ:
13136 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13137 clauses);
13138 c_name = "seq";
13139 break;
13140 case PRAGMA_OACC_CLAUSE_TILE:
13141 clauses = c_parser_oacc_clause_tile (parser, clauses);
13142 c_name = "tile";
13143 break;
13144 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13145 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13146 c_name = "use_device";
13147 break;
13148 case PRAGMA_OACC_CLAUSE_VECTOR:
13149 c_name = "vector";
13150 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13151 c_name, clauses);
13152 break;
13153 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13154 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13155 c_name = "vector_length";
13156 break;
13157 case PRAGMA_OACC_CLAUSE_WAIT:
13158 clauses = c_parser_oacc_clause_wait (parser, clauses);
13159 c_name = "wait";
13160 break;
13161 case PRAGMA_OACC_CLAUSE_WORKER:
13162 c_name = "worker";
13163 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13164 c_name, clauses);
13165 break;
13166 default:
13167 c_parser_error (parser, "expected %<#pragma acc%> clause");
13168 goto saw_error;
13171 first = false;
13173 if (((mask >> c_kind) & 1) == 0)
13175 /* Remove the invalid clause(s) from the list to avoid
13176 confusing the rest of the compiler. */
13177 clauses = prev;
13178 error_at (here, "%qs is not valid for %qs", c_name, where);
13182 saw_error:
13183 c_parser_skip_to_pragma_eol (parser);
13185 if (finish_p)
13186 return c_finish_omp_clauses (clauses, false);
13188 return clauses;
13191 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13192 is a bitmask in MASK. Return the list of clauses found. */
13194 static tree
13195 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13196 const char *where, bool finish_p = true)
13198 tree clauses = NULL;
13199 bool first = true, cilk_simd_fn = false;
13201 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13203 location_t here;
13204 pragma_omp_clause c_kind;
13205 const char *c_name;
13206 tree prev = clauses;
13208 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13209 c_parser_consume_token (parser);
13211 here = c_parser_peek_token (parser)->location;
13212 c_kind = c_parser_omp_clause_name (parser);
13214 switch (c_kind)
13216 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13217 clauses = c_parser_omp_clause_collapse (parser, clauses);
13218 c_name = "collapse";
13219 break;
13220 case PRAGMA_OMP_CLAUSE_COPYIN:
13221 clauses = c_parser_omp_clause_copyin (parser, clauses);
13222 c_name = "copyin";
13223 break;
13224 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13225 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13226 c_name = "copyprivate";
13227 break;
13228 case PRAGMA_OMP_CLAUSE_DEFAULT:
13229 clauses = c_parser_omp_clause_default (parser, clauses, false);
13230 c_name = "default";
13231 break;
13232 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13233 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13234 c_name = "firstprivate";
13235 break;
13236 case PRAGMA_OMP_CLAUSE_FINAL:
13237 clauses = c_parser_omp_clause_final (parser, clauses);
13238 c_name = "final";
13239 break;
13240 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13241 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13242 c_name = "grainsize";
13243 break;
13244 case PRAGMA_OMP_CLAUSE_HINT:
13245 clauses = c_parser_omp_clause_hint (parser, clauses);
13246 c_name = "hint";
13247 break;
13248 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13249 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13250 c_name = "defaultmap";
13251 break;
13252 case PRAGMA_OMP_CLAUSE_IF:
13253 clauses = c_parser_omp_clause_if (parser, clauses, true);
13254 c_name = "if";
13255 break;
13256 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13257 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13258 c_name = "lastprivate";
13259 break;
13260 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13261 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13262 c_name = "mergeable";
13263 break;
13264 case PRAGMA_OMP_CLAUSE_NOWAIT:
13265 clauses = c_parser_omp_clause_nowait (parser, clauses);
13266 c_name = "nowait";
13267 break;
13268 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13269 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13270 c_name = "num_tasks";
13271 break;
13272 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13273 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13274 c_name = "num_threads";
13275 break;
13276 case PRAGMA_OMP_CLAUSE_ORDERED:
13277 clauses = c_parser_omp_clause_ordered (parser, clauses);
13278 c_name = "ordered";
13279 break;
13280 case PRAGMA_OMP_CLAUSE_PRIORITY:
13281 clauses = c_parser_omp_clause_priority (parser, clauses);
13282 c_name = "priority";
13283 break;
13284 case PRAGMA_OMP_CLAUSE_PRIVATE:
13285 clauses = c_parser_omp_clause_private (parser, clauses);
13286 c_name = "private";
13287 break;
13288 case PRAGMA_OMP_CLAUSE_REDUCTION:
13289 clauses = c_parser_omp_clause_reduction (parser, clauses);
13290 c_name = "reduction";
13291 break;
13292 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13293 clauses = c_parser_omp_clause_schedule (parser, clauses);
13294 c_name = "schedule";
13295 break;
13296 case PRAGMA_OMP_CLAUSE_SHARED:
13297 clauses = c_parser_omp_clause_shared (parser, clauses);
13298 c_name = "shared";
13299 break;
13300 case PRAGMA_OMP_CLAUSE_UNTIED:
13301 clauses = c_parser_omp_clause_untied (parser, clauses);
13302 c_name = "untied";
13303 break;
13304 case PRAGMA_OMP_CLAUSE_INBRANCH:
13305 case PRAGMA_CILK_CLAUSE_MASK:
13306 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13307 clauses);
13308 c_name = "inbranch";
13309 break;
13310 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13311 case PRAGMA_CILK_CLAUSE_NOMASK:
13312 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13313 clauses);
13314 c_name = "notinbranch";
13315 break;
13316 case PRAGMA_OMP_CLAUSE_PARALLEL:
13317 clauses
13318 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13319 clauses);
13320 c_name = "parallel";
13321 if (!first)
13323 clause_not_first:
13324 error_at (here, "%qs must be the first clause of %qs",
13325 c_name, where);
13326 clauses = prev;
13328 break;
13329 case PRAGMA_OMP_CLAUSE_FOR:
13330 clauses
13331 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13332 clauses);
13333 c_name = "for";
13334 if (!first)
13335 goto clause_not_first;
13336 break;
13337 case PRAGMA_OMP_CLAUSE_SECTIONS:
13338 clauses
13339 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13340 clauses);
13341 c_name = "sections";
13342 if (!first)
13343 goto clause_not_first;
13344 break;
13345 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13346 clauses
13347 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13348 clauses);
13349 c_name = "taskgroup";
13350 if (!first)
13351 goto clause_not_first;
13352 break;
13353 case PRAGMA_OMP_CLAUSE_LINK:
13354 clauses
13355 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13356 c_name = "link";
13357 break;
13358 case PRAGMA_OMP_CLAUSE_TO:
13359 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13360 clauses
13361 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13362 clauses);
13363 else
13364 clauses = c_parser_omp_clause_to (parser, clauses);
13365 c_name = "to";
13366 break;
13367 case PRAGMA_OMP_CLAUSE_FROM:
13368 clauses = c_parser_omp_clause_from (parser, clauses);
13369 c_name = "from";
13370 break;
13371 case PRAGMA_OMP_CLAUSE_UNIFORM:
13372 clauses = c_parser_omp_clause_uniform (parser, clauses);
13373 c_name = "uniform";
13374 break;
13375 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13376 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13377 c_name = "num_teams";
13378 break;
13379 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13380 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13381 c_name = "thread_limit";
13382 break;
13383 case PRAGMA_OMP_CLAUSE_ALIGNED:
13384 clauses = c_parser_omp_clause_aligned (parser, clauses);
13385 c_name = "aligned";
13386 break;
13387 case PRAGMA_OMP_CLAUSE_LINEAR:
13388 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13389 cilk_simd_fn = true;
13390 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13391 c_name = "linear";
13392 break;
13393 case PRAGMA_OMP_CLAUSE_DEPEND:
13394 clauses = c_parser_omp_clause_depend (parser, clauses);
13395 c_name = "depend";
13396 break;
13397 case PRAGMA_OMP_CLAUSE_MAP:
13398 clauses = c_parser_omp_clause_map (parser, clauses);
13399 c_name = "map";
13400 break;
13401 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13402 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13403 c_name = "use_device_ptr";
13404 break;
13405 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13406 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13407 c_name = "is_device_ptr";
13408 break;
13409 case PRAGMA_OMP_CLAUSE_DEVICE:
13410 clauses = c_parser_omp_clause_device (parser, clauses);
13411 c_name = "device";
13412 break;
13413 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13414 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13415 c_name = "dist_schedule";
13416 break;
13417 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13418 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13419 c_name = "proc_bind";
13420 break;
13421 case PRAGMA_OMP_CLAUSE_SAFELEN:
13422 clauses = c_parser_omp_clause_safelen (parser, clauses);
13423 c_name = "safelen";
13424 break;
13425 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13426 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13427 c_name = "simdlen";
13428 break;
13429 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13430 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13431 c_name = "simdlen";
13432 break;
13433 case PRAGMA_OMP_CLAUSE_NOGROUP:
13434 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13435 c_name = "nogroup";
13436 break;
13437 case PRAGMA_OMP_CLAUSE_THREADS:
13438 clauses
13439 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13440 clauses);
13441 c_name = "threads";
13442 break;
13443 case PRAGMA_OMP_CLAUSE_SIMD:
13444 clauses
13445 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13446 clauses);
13447 c_name = "simd";
13448 break;
13449 default:
13450 c_parser_error (parser, "expected %<#pragma omp%> clause");
13451 goto saw_error;
13454 first = false;
13456 if (((mask >> c_kind) & 1) == 0)
13458 /* Remove the invalid clause(s) from the list to avoid
13459 confusing the rest of the compiler. */
13460 clauses = prev;
13461 error_at (here, "%qs is not valid for %qs", c_name, where);
13465 saw_error:
13466 c_parser_skip_to_pragma_eol (parser);
13468 if (finish_p)
13470 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13471 return c_finish_omp_clauses (clauses, true, true);
13472 return c_finish_omp_clauses (clauses, true);
13475 return clauses;
13478 /* OpenACC 2.0, OpenMP 2.5:
13479 structured-block:
13480 statement
13482 In practice, we're also interested in adding the statement to an
13483 outer node. So it is convenient if we work around the fact that
13484 c_parser_statement calls add_stmt. */
13486 static tree
13487 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13489 tree stmt = push_stmt_list ();
13490 c_parser_statement (parser, if_p);
13491 return pop_stmt_list (stmt);
13494 /* OpenACC 2.0:
13495 # pragma acc cache (variable-list) new-line
13497 LOC is the location of the #pragma token.
13500 static tree
13501 c_parser_oacc_cache (location_t loc, c_parser *parser)
13503 tree stmt, clauses;
13505 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13506 clauses = c_finish_omp_clauses (clauses, false);
13508 c_parser_skip_to_pragma_eol (parser);
13510 stmt = make_node (OACC_CACHE);
13511 TREE_TYPE (stmt) = void_type_node;
13512 OACC_CACHE_CLAUSES (stmt) = clauses;
13513 SET_EXPR_LOCATION (stmt, loc);
13514 add_stmt (stmt);
13516 return stmt;
13519 /* OpenACC 2.0:
13520 # pragma acc data oacc-data-clause[optseq] new-line
13521 structured-block
13523 LOC is the location of the #pragma token.
13526 #define OACC_DATA_CLAUSE_MASK \
13527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13539 static tree
13540 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13542 tree stmt, clauses, block;
13544 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13545 "#pragma acc data");
13547 block = c_begin_omp_parallel ();
13548 add_stmt (c_parser_omp_structured_block (parser, if_p));
13550 stmt = c_finish_oacc_data (loc, clauses, block);
13552 return stmt;
13555 /* OpenACC 2.0:
13556 # pragma acc declare oacc-data-clause[optseq] new-line
13559 #define OACC_DECLARE_CLAUSE_MASK \
13560 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13573 static void
13574 c_parser_oacc_declare (c_parser *parser)
13576 location_t pragma_loc = c_parser_peek_token (parser)->location;
13577 tree clauses, stmt, t, decl;
13579 bool error = false;
13581 c_parser_consume_pragma (parser);
13583 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13584 "#pragma acc declare");
13585 if (!clauses)
13587 error_at (pragma_loc,
13588 "no valid clauses specified in %<#pragma acc declare%>");
13589 return;
13592 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13594 location_t loc = OMP_CLAUSE_LOCATION (t);
13595 decl = OMP_CLAUSE_DECL (t);
13596 if (!DECL_P (decl))
13598 error_at (loc, "array section in %<#pragma acc declare%>");
13599 error = true;
13600 continue;
13603 switch (OMP_CLAUSE_MAP_KIND (t))
13605 case GOMP_MAP_FORCE_ALLOC:
13606 case GOMP_MAP_FORCE_TO:
13607 case GOMP_MAP_FORCE_DEVICEPTR:
13608 case GOMP_MAP_DEVICE_RESIDENT:
13609 break;
13611 case GOMP_MAP_POINTER:
13612 /* Generated by c_finish_omp_clauses from array sections;
13613 avoid spurious diagnostics. */
13614 break;
13616 case GOMP_MAP_LINK:
13617 if (!global_bindings_p ()
13618 && (TREE_STATIC (decl)
13619 || !DECL_EXTERNAL (decl)))
13621 error_at (loc,
13622 "%qD must be a global variable in"
13623 "%<#pragma acc declare link%>",
13624 decl);
13625 error = true;
13626 continue;
13628 break;
13630 default:
13631 if (global_bindings_p ())
13633 error_at (loc, "invalid OpenACC clause at file scope");
13634 error = true;
13635 continue;
13637 if (DECL_EXTERNAL (decl))
13639 error_at (loc,
13640 "invalid use of %<extern%> variable %qD "
13641 "in %<#pragma acc declare%>", decl);
13642 error = true;
13643 continue;
13645 else if (TREE_PUBLIC (decl))
13647 error_at (loc,
13648 "invalid use of %<global%> variable %qD "
13649 "in %<#pragma acc declare%>", decl);
13650 error = true;
13651 continue;
13653 break;
13656 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13657 || lookup_attribute ("omp declare target link",
13658 DECL_ATTRIBUTES (decl)))
13660 error_at (loc, "variable %qD used more than once with "
13661 "%<#pragma acc declare%>", decl);
13662 error = true;
13663 continue;
13666 if (!error)
13668 tree id;
13670 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13671 id = get_identifier ("omp declare target link");
13672 else
13673 id = get_identifier ("omp declare target");
13675 DECL_ATTRIBUTES (decl)
13676 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13678 if (global_bindings_p ())
13680 symtab_node *node = symtab_node::get (decl);
13681 if (node != NULL)
13683 node->offloadable = 1;
13684 if (ENABLE_OFFLOADING)
13686 g->have_offload = true;
13687 if (is_a <varpool_node *> (node))
13688 vec_safe_push (offload_vars, decl);
13695 if (error || global_bindings_p ())
13696 return;
13698 stmt = make_node (OACC_DECLARE);
13699 TREE_TYPE (stmt) = void_type_node;
13700 OACC_DECLARE_CLAUSES (stmt) = clauses;
13701 SET_EXPR_LOCATION (stmt, pragma_loc);
13703 add_stmt (stmt);
13705 return;
13708 /* OpenACC 2.0:
13709 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13713 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13716 LOC is the location of the #pragma token.
13719 #define OACC_ENTER_DATA_CLAUSE_MASK \
13720 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13728 #define OACC_EXIT_DATA_CLAUSE_MASK \
13729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13735 static void
13736 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13738 location_t loc = c_parser_peek_token (parser)->location;
13739 tree clauses, stmt;
13741 c_parser_consume_pragma (parser);
13743 if (!c_parser_next_token_is (parser, CPP_NAME))
13745 c_parser_error (parser, enter
13746 ? "expected %<data%> in %<#pragma acc enter data%>"
13747 : "expected %<data%> in %<#pragma acc exit data%>");
13748 c_parser_skip_to_pragma_eol (parser);
13749 return;
13752 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13753 if (strcmp (p, "data") != 0)
13755 c_parser_error (parser, "invalid pragma");
13756 c_parser_skip_to_pragma_eol (parser);
13757 return;
13760 c_parser_consume_token (parser);
13762 if (enter)
13763 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13764 "#pragma acc enter data");
13765 else
13766 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13767 "#pragma acc exit data");
13769 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13771 error_at (loc, enter
13772 ? "%<#pragma acc enter data%> has no data movement clause"
13773 : "%<#pragma acc exit data%> has no data movement clause");
13774 return;
13777 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13778 TREE_TYPE (stmt) = void_type_node;
13779 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13780 SET_EXPR_LOCATION (stmt, loc);
13781 add_stmt (stmt);
13785 /* OpenACC 2.0:
13786 # pragma acc host_data oacc-data-clause[optseq] new-line
13787 structured-block
13790 #define OACC_HOST_DATA_CLAUSE_MASK \
13791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13793 static tree
13794 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
13796 tree stmt, clauses, block;
13798 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13799 "#pragma acc host_data");
13801 block = c_begin_omp_parallel ();
13802 add_stmt (c_parser_omp_structured_block (parser, if_p));
13803 stmt = c_finish_oacc_host_data (loc, clauses, block);
13804 return stmt;
13808 /* OpenACC 2.0:
13810 # pragma acc loop oacc-loop-clause[optseq] new-line
13811 structured-block
13813 LOC is the location of the #pragma token.
13816 #define OACC_LOOP_CLAUSE_MASK \
13817 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
13825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
13827 static tree
13828 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
13829 omp_clause_mask mask, tree *cclauses, bool *if_p)
13831 strcat (p_name, " loop");
13832 mask |= OACC_LOOP_CLAUSE_MASK;
13834 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
13835 cclauses == NULL);
13836 if (cclauses)
13838 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
13839 if (*cclauses)
13840 *cclauses = c_finish_omp_clauses (*cclauses, false);
13841 if (clauses)
13842 clauses = c_finish_omp_clauses (clauses, false);
13845 tree block = c_begin_compound_stmt (true);
13846 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
13847 if_p);
13848 block = c_end_compound_stmt (loc, block, true);
13849 add_stmt (block);
13851 return stmt;
13854 /* OpenACC 2.0:
13855 # pragma acc kernels oacc-kernels-clause[optseq] new-line
13856 structured-block
13860 # pragma acc parallel oacc-parallel-clause[optseq] new-line
13861 structured-block
13863 LOC is the location of the #pragma token.
13866 #define OACC_KERNELS_CLAUSE_MASK \
13867 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13882 #define OACC_PARALLEL_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_PRIVATE) \
13892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
13893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
13894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
13895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
13902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13904 static tree
13905 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
13906 enum pragma_kind p_kind, char *p_name,
13907 bool *if_p)
13909 omp_clause_mask mask;
13910 enum tree_code code;
13911 switch (p_kind)
13913 case PRAGMA_OACC_KERNELS:
13914 strcat (p_name, " kernels");
13915 mask = OACC_KERNELS_CLAUSE_MASK;
13916 code = OACC_KERNELS;
13917 break;
13918 case PRAGMA_OACC_PARALLEL:
13919 strcat (p_name, " parallel");
13920 mask = OACC_PARALLEL_CLAUSE_MASK;
13921 code = OACC_PARALLEL;
13922 break;
13923 default:
13924 gcc_unreachable ();
13927 if (c_parser_next_token_is (parser, CPP_NAME))
13929 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13930 if (strcmp (p, "loop") == 0)
13932 c_parser_consume_token (parser);
13933 mask |= OACC_LOOP_CLAUSE_MASK;
13935 tree block = c_begin_omp_parallel ();
13936 tree clauses;
13937 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
13938 return c_finish_omp_construct (loc, code, block, clauses);
13942 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
13944 tree block = c_begin_omp_parallel ();
13945 add_stmt (c_parser_omp_structured_block (parser, if_p));
13947 return c_finish_omp_construct (loc, code, block, clauses);
13950 /* OpenACC 2.0:
13951 # pragma acc routine oacc-routine-clause[optseq] new-line
13952 function-definition
13954 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
13957 #define OACC_ROUTINE_CLAUSE_MASK \
13958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
13963 /* Parse an OpenACC routine directive. For named directives, we apply
13964 immediately to the named function. For unnamed ones we then parse
13965 a declaration or definition, which must be for a function. */
13967 static void
13968 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
13970 tree decl = NULL_TREE;
13971 /* Create a dummy claue, to record location. */
13972 tree c_head = build_omp_clause (c_parser_peek_token (parser)->location,
13973 OMP_CLAUSE_SEQ);
13975 if (context != pragma_external)
13976 c_parser_error (parser, "%<#pragma acc routine%> not at file scope");
13978 c_parser_consume_pragma (parser);
13980 /* Scan for optional '( name )'. */
13981 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13983 c_parser_consume_token (parser);
13985 c_token *token = c_parser_peek_token (parser);
13987 if (token->type == CPP_NAME && (token->id_kind == C_ID_ID
13988 || token->id_kind == C_ID_TYPENAME))
13990 decl = lookup_name (token->value);
13991 if (!decl)
13993 error_at (token->location, "%qE has not been declared",
13994 token->value);
13995 decl = error_mark_node;
13998 else
13999 c_parser_error (parser, "expected function name");
14001 if (token->type != CPP_CLOSE_PAREN)
14002 c_parser_consume_token (parser);
14004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
14007 /* Build a chain of clauses. */
14008 parser->in_pragma = true;
14009 tree clauses = c_parser_oacc_all_clauses
14010 (parser, OACC_ROUTINE_CLAUSE_MASK, "#pragma acc routine");
14012 /* Force clauses to be non-null, by attaching context to it. */
14013 clauses = tree_cons (c_head, clauses, NULL_TREE);
14015 if (decl)
14016 c_finish_oacc_routine (parser, decl, clauses, true, true, false);
14017 else if (c_parser_peek_token (parser)->type == CPP_PRAGMA)
14018 /* This will emit an error. */
14019 c_finish_oacc_routine (parser, NULL_TREE, clauses, false, true, false);
14020 else
14021 c_parser_declaration_or_fndef (parser, true, false, false, false,
14022 true, NULL, vNULL, clauses);
14025 /* Finalize an OpenACC routine pragma, applying it to FNDECL. CLAUSES
14026 are the parsed clauses. IS_DEFN is true if we're applying it to
14027 the definition (so expect FNDEF to look somewhat defined. */
14029 static void
14030 c_finish_oacc_routine (c_parser *ARG_UNUSED (parser), tree fndecl,
14031 tree clauses, bool named, bool first, bool is_defn)
14033 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
14035 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL || !first)
14037 if (fndecl != error_mark_node)
14038 error_at (loc, "%<#pragma acc routine%> %s",
14039 named ? "does not refer to a function"
14040 : "not followed by single function");
14041 return;
14044 if (get_oacc_fn_attrib (fndecl))
14045 error_at (loc, "%<#pragma acc routine%> already applied to %D", fndecl);
14047 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14048 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
14049 TREE_USED (fndecl) ? "use" : "definition");
14051 /* Process for function attrib */
14052 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
14053 replace_oacc_fn_attrib (fndecl, dims);
14055 /* Also attach as a declare. */
14056 DECL_ATTRIBUTES (fndecl)
14057 = tree_cons (get_identifier ("omp declare target"),
14058 clauses, DECL_ATTRIBUTES (fndecl));
14061 /* OpenACC 2.0:
14062 # pragma acc update oacc-update-clause[optseq] new-line
14065 #define OACC_UPDATE_CLAUSE_MASK \
14066 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14073 static void
14074 c_parser_oacc_update (c_parser *parser)
14076 location_t loc = c_parser_peek_token (parser)->location;
14078 c_parser_consume_pragma (parser);
14080 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14081 "#pragma acc update");
14082 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14084 error_at (loc,
14085 "%<#pragma acc update%> must contain at least one "
14086 "%<device%> or %<host%> or %<self%> clause");
14087 return;
14090 if (parser->error)
14091 return;
14093 tree stmt = make_node (OACC_UPDATE);
14094 TREE_TYPE (stmt) = void_type_node;
14095 OACC_UPDATE_CLAUSES (stmt) = clauses;
14096 SET_EXPR_LOCATION (stmt, loc);
14097 add_stmt (stmt);
14100 /* OpenACC 2.0:
14101 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14103 LOC is the location of the #pragma token.
14106 #define OACC_WAIT_CLAUSE_MASK \
14107 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14109 static tree
14110 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14112 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14114 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14115 list = c_parser_oacc_wait_list (parser, loc, list);
14117 strcpy (p_name, " wait");
14118 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14119 stmt = c_finish_oacc_wait (loc, list, clauses);
14120 add_stmt (stmt);
14122 return stmt;
14125 /* OpenMP 2.5:
14126 # pragma omp atomic new-line
14127 expression-stmt
14129 expression-stmt:
14130 x binop= expr | x++ | ++x | x-- | --x
14131 binop:
14132 +, *, -, /, &, ^, |, <<, >>
14134 where x is an lvalue expression with scalar type.
14136 OpenMP 3.1:
14137 # pragma omp atomic new-line
14138 update-stmt
14140 # pragma omp atomic read new-line
14141 read-stmt
14143 # pragma omp atomic write new-line
14144 write-stmt
14146 # pragma omp atomic update new-line
14147 update-stmt
14149 # pragma omp atomic capture new-line
14150 capture-stmt
14152 # pragma omp atomic capture new-line
14153 capture-block
14155 read-stmt:
14156 v = x
14157 write-stmt:
14158 x = expr
14159 update-stmt:
14160 expression-stmt | x = x binop expr
14161 capture-stmt:
14162 v = expression-stmt
14163 capture-block:
14164 { v = x; update-stmt; } | { update-stmt; v = x; }
14166 OpenMP 4.0:
14167 update-stmt:
14168 expression-stmt | x = x binop expr | x = expr binop x
14169 capture-stmt:
14170 v = update-stmt
14171 capture-block:
14172 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14174 where x and v are lvalue expressions with scalar type.
14176 LOC is the location of the #pragma token. */
14178 static void
14179 c_parser_omp_atomic (location_t loc, c_parser *parser)
14181 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14182 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14183 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14184 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14185 struct c_expr expr;
14186 location_t eloc;
14187 bool structured_block = false;
14188 bool swapped = false;
14189 bool seq_cst = false;
14190 bool non_lvalue_p;
14192 if (c_parser_next_token_is (parser, CPP_NAME))
14194 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14195 if (!strcmp (p, "seq_cst"))
14197 seq_cst = true;
14198 c_parser_consume_token (parser);
14199 if (c_parser_next_token_is (parser, CPP_COMMA)
14200 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14201 c_parser_consume_token (parser);
14204 if (c_parser_next_token_is (parser, CPP_NAME))
14206 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14208 if (!strcmp (p, "read"))
14209 code = OMP_ATOMIC_READ;
14210 else if (!strcmp (p, "write"))
14211 code = NOP_EXPR;
14212 else if (!strcmp (p, "update"))
14213 code = OMP_ATOMIC;
14214 else if (!strcmp (p, "capture"))
14215 code = OMP_ATOMIC_CAPTURE_NEW;
14216 else
14217 p = NULL;
14218 if (p)
14219 c_parser_consume_token (parser);
14221 if (!seq_cst)
14223 if (c_parser_next_token_is (parser, CPP_COMMA)
14224 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14225 c_parser_consume_token (parser);
14227 if (c_parser_next_token_is (parser, CPP_NAME))
14229 const char *p
14230 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14231 if (!strcmp (p, "seq_cst"))
14233 seq_cst = true;
14234 c_parser_consume_token (parser);
14238 c_parser_skip_to_pragma_eol (parser);
14240 switch (code)
14242 case OMP_ATOMIC_READ:
14243 case NOP_EXPR: /* atomic write */
14244 v = c_parser_cast_expression (parser, NULL).value;
14245 non_lvalue_p = !lvalue_p (v);
14246 v = c_fully_fold (v, false, NULL);
14247 if (v == error_mark_node)
14248 goto saw_error;
14249 if (non_lvalue_p)
14250 v = non_lvalue (v);
14251 loc = c_parser_peek_token (parser)->location;
14252 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14253 goto saw_error;
14254 if (code == NOP_EXPR)
14256 lhs = c_parser_expression (parser).value;
14257 lhs = c_fully_fold (lhs, false, NULL);
14258 if (lhs == error_mark_node)
14259 goto saw_error;
14261 else
14263 lhs = c_parser_cast_expression (parser, NULL).value;
14264 non_lvalue_p = !lvalue_p (lhs);
14265 lhs = c_fully_fold (lhs, false, NULL);
14266 if (lhs == error_mark_node)
14267 goto saw_error;
14268 if (non_lvalue_p)
14269 lhs = non_lvalue (lhs);
14271 if (code == NOP_EXPR)
14273 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14274 opcode. */
14275 code = OMP_ATOMIC;
14276 rhs = lhs;
14277 lhs = v;
14278 v = NULL_TREE;
14280 goto done;
14281 case OMP_ATOMIC_CAPTURE_NEW:
14282 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14284 c_parser_consume_token (parser);
14285 structured_block = true;
14287 else
14289 v = c_parser_cast_expression (parser, NULL).value;
14290 non_lvalue_p = !lvalue_p (v);
14291 v = c_fully_fold (v, false, NULL);
14292 if (v == error_mark_node)
14293 goto saw_error;
14294 if (non_lvalue_p)
14295 v = non_lvalue (v);
14296 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14297 goto saw_error;
14299 break;
14300 default:
14301 break;
14304 /* For structured_block case we don't know yet whether
14305 old or new x should be captured. */
14306 restart:
14307 eloc = c_parser_peek_token (parser)->location;
14308 expr = c_parser_cast_expression (parser, NULL);
14309 lhs = expr.value;
14310 expr = default_function_array_conversion (eloc, expr);
14311 unfolded_lhs = expr.value;
14312 lhs = c_fully_fold (lhs, false, NULL);
14313 orig_lhs = lhs;
14314 switch (TREE_CODE (lhs))
14316 case ERROR_MARK:
14317 saw_error:
14318 c_parser_skip_to_end_of_block_or_statement (parser);
14319 if (structured_block)
14321 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14322 c_parser_consume_token (parser);
14323 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14325 c_parser_skip_to_end_of_block_or_statement (parser);
14326 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14327 c_parser_consume_token (parser);
14330 return;
14332 case POSTINCREMENT_EXPR:
14333 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14334 code = OMP_ATOMIC_CAPTURE_OLD;
14335 /* FALLTHROUGH */
14336 case PREINCREMENT_EXPR:
14337 lhs = TREE_OPERAND (lhs, 0);
14338 unfolded_lhs = NULL_TREE;
14339 opcode = PLUS_EXPR;
14340 rhs = integer_one_node;
14341 break;
14343 case POSTDECREMENT_EXPR:
14344 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14345 code = OMP_ATOMIC_CAPTURE_OLD;
14346 /* FALLTHROUGH */
14347 case PREDECREMENT_EXPR:
14348 lhs = TREE_OPERAND (lhs, 0);
14349 unfolded_lhs = NULL_TREE;
14350 opcode = MINUS_EXPR;
14351 rhs = integer_one_node;
14352 break;
14354 case COMPOUND_EXPR:
14355 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14356 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14357 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14358 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14359 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14360 (TREE_OPERAND (lhs, 1), 0), 0)))
14361 == BOOLEAN_TYPE)
14362 /* Undo effects of boolean_increment for post {in,de}crement. */
14363 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14364 /* FALLTHRU */
14365 case MODIFY_EXPR:
14366 if (TREE_CODE (lhs) == MODIFY_EXPR
14367 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14369 /* Undo effects of boolean_increment. */
14370 if (integer_onep (TREE_OPERAND (lhs, 1)))
14372 /* This is pre or post increment. */
14373 rhs = TREE_OPERAND (lhs, 1);
14374 lhs = TREE_OPERAND (lhs, 0);
14375 unfolded_lhs = NULL_TREE;
14376 opcode = NOP_EXPR;
14377 if (code == OMP_ATOMIC_CAPTURE_NEW
14378 && !structured_block
14379 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14380 code = OMP_ATOMIC_CAPTURE_OLD;
14381 break;
14383 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14384 && TREE_OPERAND (lhs, 0)
14385 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14387 /* This is pre or post decrement. */
14388 rhs = TREE_OPERAND (lhs, 1);
14389 lhs = TREE_OPERAND (lhs, 0);
14390 unfolded_lhs = NULL_TREE;
14391 opcode = NOP_EXPR;
14392 if (code == OMP_ATOMIC_CAPTURE_NEW
14393 && !structured_block
14394 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14395 code = OMP_ATOMIC_CAPTURE_OLD;
14396 break;
14399 /* FALLTHRU */
14400 default:
14401 if (!lvalue_p (unfolded_lhs))
14402 lhs = non_lvalue (lhs);
14403 switch (c_parser_peek_token (parser)->type)
14405 case CPP_MULT_EQ:
14406 opcode = MULT_EXPR;
14407 break;
14408 case CPP_DIV_EQ:
14409 opcode = TRUNC_DIV_EXPR;
14410 break;
14411 case CPP_PLUS_EQ:
14412 opcode = PLUS_EXPR;
14413 break;
14414 case CPP_MINUS_EQ:
14415 opcode = MINUS_EXPR;
14416 break;
14417 case CPP_LSHIFT_EQ:
14418 opcode = LSHIFT_EXPR;
14419 break;
14420 case CPP_RSHIFT_EQ:
14421 opcode = RSHIFT_EXPR;
14422 break;
14423 case CPP_AND_EQ:
14424 opcode = BIT_AND_EXPR;
14425 break;
14426 case CPP_OR_EQ:
14427 opcode = BIT_IOR_EXPR;
14428 break;
14429 case CPP_XOR_EQ:
14430 opcode = BIT_XOR_EXPR;
14431 break;
14432 case CPP_EQ:
14433 c_parser_consume_token (parser);
14434 eloc = c_parser_peek_token (parser)->location;
14435 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14436 rhs1 = expr.value;
14437 switch (TREE_CODE (rhs1))
14439 case MULT_EXPR:
14440 case TRUNC_DIV_EXPR:
14441 case RDIV_EXPR:
14442 case PLUS_EXPR:
14443 case MINUS_EXPR:
14444 case LSHIFT_EXPR:
14445 case RSHIFT_EXPR:
14446 case BIT_AND_EXPR:
14447 case BIT_IOR_EXPR:
14448 case BIT_XOR_EXPR:
14449 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14451 opcode = TREE_CODE (rhs1);
14452 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14453 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14454 goto stmt_done;
14456 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14458 opcode = TREE_CODE (rhs1);
14459 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14460 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14461 swapped = !commutative_tree_code (opcode);
14462 goto stmt_done;
14464 break;
14465 case ERROR_MARK:
14466 goto saw_error;
14467 default:
14468 break;
14470 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14472 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14474 code = OMP_ATOMIC_CAPTURE_OLD;
14475 v = lhs;
14476 lhs = NULL_TREE;
14477 expr = default_function_array_read_conversion (eloc, expr);
14478 unfolded_lhs1 = expr.value;
14479 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14480 rhs1 = NULL_TREE;
14481 c_parser_consume_token (parser);
14482 goto restart;
14484 if (structured_block)
14486 opcode = NOP_EXPR;
14487 expr = default_function_array_read_conversion (eloc, expr);
14488 rhs = c_fully_fold (expr.value, false, NULL);
14489 rhs1 = NULL_TREE;
14490 goto stmt_done;
14493 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14494 goto saw_error;
14495 default:
14496 c_parser_error (parser,
14497 "invalid operator for %<#pragma omp atomic%>");
14498 goto saw_error;
14501 /* Arrange to pass the location of the assignment operator to
14502 c_finish_omp_atomic. */
14503 loc = c_parser_peek_token (parser)->location;
14504 c_parser_consume_token (parser);
14505 eloc = c_parser_peek_token (parser)->location;
14506 expr = c_parser_expression (parser);
14507 expr = default_function_array_read_conversion (eloc, expr);
14508 rhs = expr.value;
14509 rhs = c_fully_fold (rhs, false, NULL);
14510 break;
14512 stmt_done:
14513 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14515 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14516 goto saw_error;
14517 v = c_parser_cast_expression (parser, NULL).value;
14518 non_lvalue_p = !lvalue_p (v);
14519 v = c_fully_fold (v, false, NULL);
14520 if (v == error_mark_node)
14521 goto saw_error;
14522 if (non_lvalue_p)
14523 v = non_lvalue (v);
14524 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14525 goto saw_error;
14526 eloc = c_parser_peek_token (parser)->location;
14527 expr = c_parser_cast_expression (parser, NULL);
14528 lhs1 = expr.value;
14529 expr = default_function_array_read_conversion (eloc, expr);
14530 unfolded_lhs1 = expr.value;
14531 lhs1 = c_fully_fold (lhs1, false, NULL);
14532 if (lhs1 == error_mark_node)
14533 goto saw_error;
14534 if (!lvalue_p (unfolded_lhs1))
14535 lhs1 = non_lvalue (lhs1);
14537 if (structured_block)
14539 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14540 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14542 done:
14543 if (unfolded_lhs && unfolded_lhs1
14544 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14546 error ("%<#pragma omp atomic capture%> uses two different "
14547 "expressions for memory");
14548 stmt = error_mark_node;
14550 else
14551 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14552 swapped, seq_cst);
14553 if (stmt != error_mark_node)
14554 add_stmt (stmt);
14556 if (!structured_block)
14557 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14561 /* OpenMP 2.5:
14562 # pragma omp barrier new-line
14565 static void
14566 c_parser_omp_barrier (c_parser *parser)
14568 location_t loc = c_parser_peek_token (parser)->location;
14569 c_parser_consume_pragma (parser);
14570 c_parser_skip_to_pragma_eol (parser);
14572 c_finish_omp_barrier (loc);
14575 /* OpenMP 2.5:
14576 # pragma omp critical [(name)] new-line
14577 structured-block
14579 OpenMP 4.5:
14580 # pragma omp critical [(name) [hint(expression)]] new-line
14582 LOC is the location of the #pragma itself. */
14584 #define OMP_CRITICAL_CLAUSE_MASK \
14585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14587 static tree
14588 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
14590 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14592 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14594 c_parser_consume_token (parser);
14595 if (c_parser_next_token_is (parser, CPP_NAME))
14597 name = c_parser_peek_token (parser)->value;
14598 c_parser_consume_token (parser);
14599 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14601 else
14602 c_parser_error (parser, "expected identifier");
14604 clauses = c_parser_omp_all_clauses (parser,
14605 OMP_CRITICAL_CLAUSE_MASK,
14606 "#pragma omp critical");
14608 else
14610 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14611 c_parser_error (parser, "expected %<(%> or end of line");
14612 c_parser_skip_to_pragma_eol (parser);
14615 stmt = c_parser_omp_structured_block (parser, if_p);
14616 return c_finish_omp_critical (loc, stmt, name, clauses);
14619 /* OpenMP 2.5:
14620 # pragma omp flush flush-vars[opt] new-line
14622 flush-vars:
14623 ( variable-list ) */
14625 static void
14626 c_parser_omp_flush (c_parser *parser)
14628 location_t loc = c_parser_peek_token (parser)->location;
14629 c_parser_consume_pragma (parser);
14630 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14631 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14632 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14633 c_parser_error (parser, "expected %<(%> or end of line");
14634 c_parser_skip_to_pragma_eol (parser);
14636 c_finish_omp_flush (loc);
14639 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14640 The real trick here is to determine the loop control variable early
14641 so that we can push a new decl if necessary to make it private.
14642 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14643 respectively. */
14645 static tree
14646 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14647 tree clauses, tree *cclauses, bool *if_p)
14649 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14650 tree declv, condv, incrv, initv, ret = NULL_TREE;
14651 tree pre_body = NULL_TREE, this_pre_body;
14652 tree ordered_cl = NULL_TREE;
14653 bool fail = false, open_brace_parsed = false;
14654 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14655 location_t for_loc;
14656 vec<tree, va_gc> *for_block = make_tree_vector ();
14658 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14659 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14660 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14661 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14662 && OMP_CLAUSE_ORDERED_EXPR (cl))
14664 ordered_cl = cl;
14665 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14668 if (ordered && ordered < collapse)
14670 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14671 "%<ordered%> clause parameter is less than %<collapse%>");
14672 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14673 = build_int_cst (NULL_TREE, collapse);
14674 ordered = collapse;
14676 if (ordered)
14678 for (tree *pc = &clauses; *pc; )
14679 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14681 error_at (OMP_CLAUSE_LOCATION (*pc),
14682 "%<linear%> clause may not be specified together "
14683 "with %<ordered%> clause with a parameter");
14684 *pc = OMP_CLAUSE_CHAIN (*pc);
14686 else
14687 pc = &OMP_CLAUSE_CHAIN (*pc);
14690 gcc_assert (collapse >= 1 && ordered >= 0);
14691 count = ordered ? ordered : collapse;
14693 declv = make_tree_vec (count);
14694 initv = make_tree_vec (count);
14695 condv = make_tree_vec (count);
14696 incrv = make_tree_vec (count);
14698 if (code != CILK_FOR
14699 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14701 c_parser_error (parser, "for statement expected");
14702 return NULL;
14704 if (code == CILK_FOR
14705 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14707 c_parser_error (parser, "_Cilk_for statement expected");
14708 return NULL;
14710 for_loc = c_parser_peek_token (parser)->location;
14711 c_parser_consume_token (parser);
14713 for (i = 0; i < count; i++)
14715 int bracecount = 0;
14717 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14718 goto pop_scopes;
14720 /* Parse the initialization declaration or expression. */
14721 if (c_parser_next_tokens_start_declaration (parser))
14723 if (i > 0)
14724 vec_safe_push (for_block, c_begin_compound_stmt (true));
14725 this_pre_body = push_stmt_list ();
14726 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14727 NULL, vNULL);
14728 if (this_pre_body)
14730 this_pre_body = pop_stmt_list (this_pre_body);
14731 if (pre_body)
14733 tree t = pre_body;
14734 pre_body = push_stmt_list ();
14735 add_stmt (t);
14736 add_stmt (this_pre_body);
14737 pre_body = pop_stmt_list (pre_body);
14739 else
14740 pre_body = this_pre_body;
14742 decl = check_for_loop_decls (for_loc, flag_isoc99);
14743 if (decl == NULL)
14744 goto error_init;
14745 if (DECL_INITIAL (decl) == error_mark_node)
14746 decl = error_mark_node;
14747 init = decl;
14749 else if (c_parser_next_token_is (parser, CPP_NAME)
14750 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
14752 struct c_expr decl_exp;
14753 struct c_expr init_exp;
14754 location_t init_loc;
14756 decl_exp = c_parser_postfix_expression (parser);
14757 decl = decl_exp.value;
14759 c_parser_require (parser, CPP_EQ, "expected %<=%>");
14761 init_loc = c_parser_peek_token (parser)->location;
14762 init_exp = c_parser_expr_no_commas (parser, NULL);
14763 init_exp = default_function_array_read_conversion (init_loc,
14764 init_exp);
14765 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
14766 NOP_EXPR, init_loc, init_exp.value,
14767 init_exp.original_type);
14768 init = c_process_expr_stmt (init_loc, init);
14770 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14772 else
14774 error_init:
14775 c_parser_error (parser,
14776 "expected iteration declaration or initialization");
14777 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14778 "expected %<)%>");
14779 fail = true;
14780 goto parse_next;
14783 /* Parse the loop condition. */
14784 cond = NULL_TREE;
14785 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
14787 location_t cond_loc = c_parser_peek_token (parser)->location;
14788 struct c_expr cond_expr
14789 = c_parser_binary_expression (parser, NULL, NULL_TREE);
14791 cond = cond_expr.value;
14792 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
14793 cond = c_fully_fold (cond, false, NULL);
14794 switch (cond_expr.original_code)
14796 case GT_EXPR:
14797 case GE_EXPR:
14798 case LT_EXPR:
14799 case LE_EXPR:
14800 break;
14801 case NE_EXPR:
14802 if (code == CILK_SIMD || code == CILK_FOR)
14803 break;
14804 /* FALLTHRU. */
14805 default:
14806 /* Can't be cond = error_mark_node, because we want to preserve
14807 the location until c_finish_omp_for. */
14808 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
14809 break;
14811 protected_set_expr_location (cond, cond_loc);
14813 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14815 /* Parse the increment expression. */
14816 incr = NULL_TREE;
14817 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
14819 location_t incr_loc = c_parser_peek_token (parser)->location;
14821 incr = c_process_expr_stmt (incr_loc,
14822 c_parser_expression (parser).value);
14824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14826 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
14827 fail = true;
14828 else
14830 TREE_VEC_ELT (declv, i) = decl;
14831 TREE_VEC_ELT (initv, i) = init;
14832 TREE_VEC_ELT (condv, i) = cond;
14833 TREE_VEC_ELT (incrv, i) = incr;
14836 parse_next:
14837 if (i == count - 1)
14838 break;
14840 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
14841 in between the collapsed for loops to be still considered perfectly
14842 nested. Hopefully the final version clarifies this.
14843 For now handle (multiple) {'s and empty statements. */
14846 if (c_parser_next_token_is_keyword (parser, RID_FOR))
14848 c_parser_consume_token (parser);
14849 break;
14851 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14853 c_parser_consume_token (parser);
14854 bracecount++;
14856 else if (bracecount
14857 && c_parser_next_token_is (parser, CPP_SEMICOLON))
14858 c_parser_consume_token (parser);
14859 else
14861 c_parser_error (parser, "not enough perfectly nested loops");
14862 if (bracecount)
14864 open_brace_parsed = true;
14865 bracecount--;
14867 fail = true;
14868 count = 0;
14869 break;
14872 while (1);
14874 nbraces += bracecount;
14877 if (nbraces)
14878 if_p = NULL;
14880 save_break = c_break_label;
14881 if (code == CILK_SIMD)
14882 c_break_label = build_int_cst (size_type_node, 2);
14883 else
14884 c_break_label = size_one_node;
14885 save_cont = c_cont_label;
14886 c_cont_label = NULL_TREE;
14887 body = push_stmt_list ();
14889 if (open_brace_parsed)
14891 location_t here = c_parser_peek_token (parser)->location;
14892 stmt = c_begin_compound_stmt (true);
14893 c_parser_compound_statement_nostart (parser);
14894 add_stmt (c_end_compound_stmt (here, stmt, true));
14896 else
14897 add_stmt (c_parser_c99_block_statement (parser, if_p));
14898 if (c_cont_label)
14900 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
14901 SET_EXPR_LOCATION (t, loc);
14902 add_stmt (t);
14905 body = pop_stmt_list (body);
14906 c_break_label = save_break;
14907 c_cont_label = save_cont;
14909 while (nbraces)
14911 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14913 c_parser_consume_token (parser);
14914 nbraces--;
14916 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
14917 c_parser_consume_token (parser);
14918 else
14920 c_parser_error (parser, "collapsed loops not perfectly nested");
14921 while (nbraces)
14923 location_t here = c_parser_peek_token (parser)->location;
14924 stmt = c_begin_compound_stmt (true);
14925 add_stmt (body);
14926 c_parser_compound_statement_nostart (parser);
14927 body = c_end_compound_stmt (here, stmt, true);
14928 nbraces--;
14930 goto pop_scopes;
14934 /* Only bother calling c_finish_omp_for if we haven't already generated
14935 an error from the initialization parsing. */
14936 if (!fail)
14938 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
14939 incrv, body, pre_body);
14941 /* Check for iterators appearing in lb, b or incr expressions. */
14942 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
14943 stmt = NULL_TREE;
14945 if (stmt)
14947 add_stmt (stmt);
14949 if (cclauses != NULL
14950 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
14952 tree *c;
14953 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
14954 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
14955 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
14956 c = &OMP_CLAUSE_CHAIN (*c);
14957 else
14959 for (i = 0; i < count; i++)
14960 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
14961 break;
14962 if (i == count)
14963 c = &OMP_CLAUSE_CHAIN (*c);
14964 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
14966 error_at (loc,
14967 "iteration variable %qD should not be firstprivate",
14968 OMP_CLAUSE_DECL (*c));
14969 *c = OMP_CLAUSE_CHAIN (*c);
14971 else
14973 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14974 tree l = *c;
14975 *c = OMP_CLAUSE_CHAIN (*c);
14976 if (code == OMP_SIMD)
14978 OMP_CLAUSE_CHAIN (l)
14979 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14980 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
14982 else
14984 OMP_CLAUSE_CHAIN (l) = clauses;
14985 clauses = l;
14990 OMP_FOR_CLAUSES (stmt) = clauses;
14992 ret = stmt;
14994 pop_scopes:
14995 while (!for_block->is_empty ())
14997 /* FIXME diagnostics: LOC below should be the actual location of
14998 this particular for block. We need to build a list of
14999 locations to go along with FOR_BLOCK. */
15000 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15001 add_stmt (stmt);
15003 release_tree_vector (for_block);
15004 return ret;
15007 /* Helper function for OpenMP parsing, split clauses and call
15008 finish_omp_clauses on each of the set of clauses afterwards. */
15010 static void
15011 omp_split_clauses (location_t loc, enum tree_code code,
15012 omp_clause_mask mask, tree clauses, tree *cclauses)
15014 int i;
15015 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15016 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15017 if (cclauses[i])
15018 cclauses[i] = c_finish_omp_clauses (cclauses[i], true);
15021 /* OpenMP 4.0:
15022 #pragma omp simd simd-clause[optseq] new-line
15023 for-loop
15025 LOC is the location of the #pragma token.
15028 #define OMP_SIMD_CLAUSE_MASK \
15029 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15038 static tree
15039 c_parser_omp_simd (location_t loc, c_parser *parser,
15040 char *p_name, omp_clause_mask mask, tree *cclauses,
15041 bool *if_p)
15043 tree block, clauses, ret;
15045 strcat (p_name, " simd");
15046 mask |= OMP_SIMD_CLAUSE_MASK;
15048 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15049 if (cclauses)
15051 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15052 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15053 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15054 OMP_CLAUSE_ORDERED);
15055 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15057 error_at (OMP_CLAUSE_LOCATION (c),
15058 "%<ordered%> clause with parameter may not be specified "
15059 "on %qs construct", p_name);
15060 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15064 block = c_begin_compound_stmt (true);
15065 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15066 block = c_end_compound_stmt (loc, block, true);
15067 add_stmt (block);
15069 return ret;
15072 /* OpenMP 2.5:
15073 #pragma omp for for-clause[optseq] new-line
15074 for-loop
15076 OpenMP 4.0:
15077 #pragma omp for simd for-simd-clause[optseq] new-line
15078 for-loop
15080 LOC is the location of the #pragma token.
15083 #define OMP_FOR_CLAUSE_MASK \
15084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15094 static tree
15095 c_parser_omp_for (location_t loc, c_parser *parser,
15096 char *p_name, omp_clause_mask mask, tree *cclauses,
15097 bool *if_p)
15099 tree block, clauses, ret;
15101 strcat (p_name, " for");
15102 mask |= OMP_FOR_CLAUSE_MASK;
15103 if (cclauses)
15104 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15105 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15106 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15107 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15109 if (c_parser_next_token_is (parser, CPP_NAME))
15111 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15113 if (strcmp (p, "simd") == 0)
15115 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15116 if (cclauses == NULL)
15117 cclauses = cclauses_buf;
15119 c_parser_consume_token (parser);
15120 if (!flag_openmp) /* flag_openmp_simd */
15121 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15122 if_p);
15123 block = c_begin_compound_stmt (true);
15124 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15125 block = c_end_compound_stmt (loc, block, true);
15126 if (ret == NULL_TREE)
15127 return ret;
15128 ret = make_node (OMP_FOR);
15129 TREE_TYPE (ret) = void_type_node;
15130 OMP_FOR_BODY (ret) = block;
15131 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15132 SET_EXPR_LOCATION (ret, loc);
15133 add_stmt (ret);
15134 return ret;
15137 if (!flag_openmp) /* flag_openmp_simd */
15139 c_parser_skip_to_pragma_eol (parser, false);
15140 return NULL_TREE;
15143 /* Composite distribute parallel for disallows linear clause. */
15144 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15145 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15147 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15148 if (cclauses)
15150 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15151 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15154 block = c_begin_compound_stmt (true);
15155 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15156 block = c_end_compound_stmt (loc, block, true);
15157 add_stmt (block);
15159 return ret;
15162 /* OpenMP 2.5:
15163 # pragma omp master new-line
15164 structured-block
15166 LOC is the location of the #pragma token.
15169 static tree
15170 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15172 c_parser_skip_to_pragma_eol (parser);
15173 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15174 if_p));
15177 /* OpenMP 2.5:
15178 # pragma omp ordered new-line
15179 structured-block
15181 OpenMP 4.5:
15182 # pragma omp ordered ordered-clauses new-line
15183 structured-block
15185 # pragma omp ordered depend-clauses new-line */
15187 #define OMP_ORDERED_CLAUSE_MASK \
15188 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15191 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15192 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15194 static bool
15195 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15196 bool *if_p)
15198 location_t loc = c_parser_peek_token (parser)->location;
15199 c_parser_consume_pragma (parser);
15201 if (context != pragma_stmt && context != pragma_compound)
15203 c_parser_error (parser, "expected declaration specifiers");
15204 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
15205 return false;
15208 if (c_parser_next_token_is (parser, CPP_NAME))
15210 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15212 if (!strcmp ("depend", p))
15214 if (context == pragma_stmt)
15216 error_at (loc,
15217 "%<#pragma omp ordered%> with %<depend> clause may "
15218 "only be used in compound statements");
15219 c_parser_skip_to_pragma_eol (parser, false);
15220 return false;
15223 tree clauses
15224 = c_parser_omp_all_clauses (parser,
15225 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15226 "#pragma omp ordered");
15227 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15228 return false;
15232 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15233 "#pragma omp ordered");
15234 c_finish_omp_ordered (loc, clauses,
15235 c_parser_omp_structured_block (parser, if_p));
15236 return true;
15239 /* OpenMP 2.5:
15241 section-scope:
15242 { section-sequence }
15244 section-sequence:
15245 section-directive[opt] structured-block
15246 section-sequence section-directive structured-block
15248 SECTIONS_LOC is the location of the #pragma omp sections. */
15250 static tree
15251 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15253 tree stmt, substmt;
15254 bool error_suppress = false;
15255 location_t loc;
15257 loc = c_parser_peek_token (parser)->location;
15258 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15260 /* Avoid skipping until the end of the block. */
15261 parser->error = false;
15262 return NULL_TREE;
15265 stmt = push_stmt_list ();
15267 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15269 substmt = c_parser_omp_structured_block (parser, NULL);
15270 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15271 SET_EXPR_LOCATION (substmt, loc);
15272 add_stmt (substmt);
15275 while (1)
15277 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15278 break;
15279 if (c_parser_next_token_is (parser, CPP_EOF))
15280 break;
15282 loc = c_parser_peek_token (parser)->location;
15283 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15285 c_parser_consume_pragma (parser);
15286 c_parser_skip_to_pragma_eol (parser);
15287 error_suppress = false;
15289 else if (!error_suppress)
15291 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15292 error_suppress = true;
15295 substmt = c_parser_omp_structured_block (parser, NULL);
15296 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15297 SET_EXPR_LOCATION (substmt, loc);
15298 add_stmt (substmt);
15300 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15301 "expected %<#pragma omp section%> or %<}%>");
15303 substmt = pop_stmt_list (stmt);
15305 stmt = make_node (OMP_SECTIONS);
15306 SET_EXPR_LOCATION (stmt, sections_loc);
15307 TREE_TYPE (stmt) = void_type_node;
15308 OMP_SECTIONS_BODY (stmt) = substmt;
15310 return add_stmt (stmt);
15313 /* OpenMP 2.5:
15314 # pragma omp sections sections-clause[optseq] newline
15315 sections-scope
15317 LOC is the location of the #pragma token.
15320 #define OMP_SECTIONS_CLAUSE_MASK \
15321 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15327 static tree
15328 c_parser_omp_sections (location_t loc, c_parser *parser,
15329 char *p_name, omp_clause_mask mask, tree *cclauses)
15331 tree block, clauses, ret;
15333 strcat (p_name, " sections");
15334 mask |= OMP_SECTIONS_CLAUSE_MASK;
15335 if (cclauses)
15336 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15338 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15339 if (cclauses)
15341 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15342 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15345 block = c_begin_compound_stmt (true);
15346 ret = c_parser_omp_sections_scope (loc, parser);
15347 if (ret)
15348 OMP_SECTIONS_CLAUSES (ret) = clauses;
15349 block = c_end_compound_stmt (loc, block, true);
15350 add_stmt (block);
15352 return ret;
15355 /* OpenMP 2.5:
15356 # pragma omp parallel parallel-clause[optseq] new-line
15357 structured-block
15358 # pragma omp parallel for parallel-for-clause[optseq] new-line
15359 structured-block
15360 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15361 structured-block
15363 OpenMP 4.0:
15364 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15365 structured-block
15367 LOC is the location of the #pragma token.
15370 #define OMP_PARALLEL_CLAUSE_MASK \
15371 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15381 static tree
15382 c_parser_omp_parallel (location_t loc, c_parser *parser,
15383 char *p_name, omp_clause_mask mask, tree *cclauses,
15384 bool *if_p)
15386 tree stmt, clauses, block;
15388 strcat (p_name, " parallel");
15389 mask |= OMP_PARALLEL_CLAUSE_MASK;
15390 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15391 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15392 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15393 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15395 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15397 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15398 if (cclauses == NULL)
15399 cclauses = cclauses_buf;
15401 c_parser_consume_token (parser);
15402 if (!flag_openmp) /* flag_openmp_simd */
15403 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15404 block = c_begin_omp_parallel ();
15405 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15406 stmt
15407 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15408 block);
15409 if (ret == NULL_TREE)
15410 return ret;
15411 OMP_PARALLEL_COMBINED (stmt) = 1;
15412 return stmt;
15414 /* When combined with distribute, parallel has to be followed by for.
15415 #pragma omp target parallel is allowed though. */
15416 else if (cclauses
15417 && (mask & (OMP_CLAUSE_MASK_1
15418 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15420 error_at (loc, "expected %<for%> after %qs", p_name);
15421 c_parser_skip_to_pragma_eol (parser);
15422 return NULL_TREE;
15424 else if (!flag_openmp) /* flag_openmp_simd */
15426 c_parser_skip_to_pragma_eol (parser, false);
15427 return NULL_TREE;
15429 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15431 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15432 if (strcmp (p, "sections") == 0)
15434 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15435 if (cclauses == NULL)
15436 cclauses = cclauses_buf;
15438 c_parser_consume_token (parser);
15439 block = c_begin_omp_parallel ();
15440 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15441 stmt = c_finish_omp_parallel (loc,
15442 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15443 block);
15444 OMP_PARALLEL_COMBINED (stmt) = 1;
15445 return stmt;
15449 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15450 if (cclauses)
15452 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15453 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15456 block = c_begin_omp_parallel ();
15457 c_parser_statement (parser, if_p);
15458 stmt = c_finish_omp_parallel (loc, clauses, block);
15460 return stmt;
15463 /* OpenMP 2.5:
15464 # pragma omp single single-clause[optseq] new-line
15465 structured-block
15467 LOC is the location of the #pragma.
15470 #define OMP_SINGLE_CLAUSE_MASK \
15471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15476 static tree
15477 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15479 tree stmt = make_node (OMP_SINGLE);
15480 SET_EXPR_LOCATION (stmt, loc);
15481 TREE_TYPE (stmt) = void_type_node;
15483 OMP_SINGLE_CLAUSES (stmt)
15484 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15485 "#pragma omp single");
15486 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15488 return add_stmt (stmt);
15491 /* OpenMP 3.0:
15492 # pragma omp task task-clause[optseq] new-line
15494 LOC is the location of the #pragma.
15497 #define OMP_TASK_CLAUSE_MASK \
15498 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15509 static tree
15510 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
15512 tree clauses, block;
15514 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15515 "#pragma omp task");
15517 block = c_begin_omp_task ();
15518 c_parser_statement (parser, if_p);
15519 return c_finish_omp_task (loc, clauses, block);
15522 /* OpenMP 3.0:
15523 # pragma omp taskwait new-line
15526 static void
15527 c_parser_omp_taskwait (c_parser *parser)
15529 location_t loc = c_parser_peek_token (parser)->location;
15530 c_parser_consume_pragma (parser);
15531 c_parser_skip_to_pragma_eol (parser);
15533 c_finish_omp_taskwait (loc);
15536 /* OpenMP 3.1:
15537 # pragma omp taskyield new-line
15540 static void
15541 c_parser_omp_taskyield (c_parser *parser)
15543 location_t loc = c_parser_peek_token (parser)->location;
15544 c_parser_consume_pragma (parser);
15545 c_parser_skip_to_pragma_eol (parser);
15547 c_finish_omp_taskyield (loc);
15550 /* OpenMP 4.0:
15551 # pragma omp taskgroup new-line
15554 static tree
15555 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
15557 location_t loc = c_parser_peek_token (parser)->location;
15558 c_parser_skip_to_pragma_eol (parser);
15559 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
15560 if_p));
15563 /* OpenMP 4.0:
15564 # pragma omp cancel cancel-clause[optseq] new-line
15566 LOC is the location of the #pragma.
15569 #define OMP_CANCEL_CLAUSE_MASK \
15570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15576 static void
15577 c_parser_omp_cancel (c_parser *parser)
15579 location_t loc = c_parser_peek_token (parser)->location;
15581 c_parser_consume_pragma (parser);
15582 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15583 "#pragma omp cancel");
15585 c_finish_omp_cancel (loc, clauses);
15588 /* OpenMP 4.0:
15589 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15591 LOC is the location of the #pragma.
15594 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15595 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15600 static void
15601 c_parser_omp_cancellation_point (c_parser *parser)
15603 location_t loc = c_parser_peek_token (parser)->location;
15604 tree clauses;
15605 bool point_seen = false;
15607 c_parser_consume_pragma (parser);
15608 if (c_parser_next_token_is (parser, CPP_NAME))
15610 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15611 if (strcmp (p, "point") == 0)
15613 c_parser_consume_token (parser);
15614 point_seen = true;
15617 if (!point_seen)
15619 c_parser_error (parser, "expected %<point%>");
15620 c_parser_skip_to_pragma_eol (parser);
15621 return;
15624 clauses
15625 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15626 "#pragma omp cancellation point");
15628 c_finish_omp_cancellation_point (loc, clauses);
15631 /* OpenMP 4.0:
15632 #pragma omp distribute distribute-clause[optseq] new-line
15633 for-loop */
15635 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15642 static tree
15643 c_parser_omp_distribute (location_t loc, c_parser *parser,
15644 char *p_name, omp_clause_mask mask, tree *cclauses,
15645 bool *if_p)
15647 tree clauses, block, ret;
15649 strcat (p_name, " distribute");
15650 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15652 if (c_parser_next_token_is (parser, CPP_NAME))
15654 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15655 bool simd = false;
15656 bool parallel = false;
15658 if (strcmp (p, "simd") == 0)
15659 simd = true;
15660 else
15661 parallel = strcmp (p, "parallel") == 0;
15662 if (parallel || simd)
15664 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15665 if (cclauses == NULL)
15666 cclauses = cclauses_buf;
15667 c_parser_consume_token (parser);
15668 if (!flag_openmp) /* flag_openmp_simd */
15670 if (simd)
15671 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15672 if_p);
15673 else
15674 return c_parser_omp_parallel (loc, parser, p_name, mask,
15675 cclauses, if_p);
15677 block = c_begin_compound_stmt (true);
15678 if (simd)
15679 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15680 if_p);
15681 else
15682 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
15683 if_p);
15684 block = c_end_compound_stmt (loc, block, true);
15685 if (ret == NULL)
15686 return ret;
15687 ret = make_node (OMP_DISTRIBUTE);
15688 TREE_TYPE (ret) = void_type_node;
15689 OMP_FOR_BODY (ret) = block;
15690 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15691 SET_EXPR_LOCATION (ret, loc);
15692 add_stmt (ret);
15693 return ret;
15696 if (!flag_openmp) /* flag_openmp_simd */
15698 c_parser_skip_to_pragma_eol (parser, false);
15699 return NULL_TREE;
15702 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15703 if (cclauses)
15705 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15706 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15709 block = c_begin_compound_stmt (true);
15710 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
15711 if_p);
15712 block = c_end_compound_stmt (loc, block, true);
15713 add_stmt (block);
15715 return ret;
15718 /* OpenMP 4.0:
15719 # pragma omp teams teams-clause[optseq] new-line
15720 structured-block */
15722 #define OMP_TEAMS_CLAUSE_MASK \
15723 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
15731 static tree
15732 c_parser_omp_teams (location_t loc, c_parser *parser,
15733 char *p_name, omp_clause_mask mask, tree *cclauses,
15734 bool *if_p)
15736 tree clauses, block, ret;
15738 strcat (p_name, " teams");
15739 mask |= OMP_TEAMS_CLAUSE_MASK;
15741 if (c_parser_next_token_is (parser, CPP_NAME))
15743 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15744 if (strcmp (p, "distribute") == 0)
15746 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15747 if (cclauses == NULL)
15748 cclauses = cclauses_buf;
15750 c_parser_consume_token (parser);
15751 if (!flag_openmp) /* flag_openmp_simd */
15752 return c_parser_omp_distribute (loc, parser, p_name, mask,
15753 cclauses, if_p);
15754 block = c_begin_compound_stmt (true);
15755 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
15756 if_p);
15757 block = c_end_compound_stmt (loc, block, true);
15758 if (ret == NULL)
15759 return ret;
15760 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15761 ret = make_node (OMP_TEAMS);
15762 TREE_TYPE (ret) = void_type_node;
15763 OMP_TEAMS_CLAUSES (ret) = clauses;
15764 OMP_TEAMS_BODY (ret) = block;
15765 OMP_TEAMS_COMBINED (ret) = 1;
15766 return add_stmt (ret);
15769 if (!flag_openmp) /* flag_openmp_simd */
15771 c_parser_skip_to_pragma_eol (parser, false);
15772 return NULL_TREE;
15775 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15776 if (cclauses)
15778 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
15779 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15782 tree stmt = make_node (OMP_TEAMS);
15783 TREE_TYPE (stmt) = void_type_node;
15784 OMP_TEAMS_CLAUSES (stmt) = clauses;
15785 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15787 return add_stmt (stmt);
15790 /* OpenMP 4.0:
15791 # pragma omp target data target-data-clause[optseq] new-line
15792 structured-block */
15794 #define OMP_TARGET_DATA_CLAUSE_MASK \
15795 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
15800 static tree
15801 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
15803 tree clauses
15804 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
15805 "#pragma omp target data");
15806 int map_seen = 0;
15807 for (tree *pc = &clauses; *pc;)
15809 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15810 switch (OMP_CLAUSE_MAP_KIND (*pc))
15812 case GOMP_MAP_TO:
15813 case GOMP_MAP_ALWAYS_TO:
15814 case GOMP_MAP_FROM:
15815 case GOMP_MAP_ALWAYS_FROM:
15816 case GOMP_MAP_TOFROM:
15817 case GOMP_MAP_ALWAYS_TOFROM:
15818 case GOMP_MAP_ALLOC:
15819 map_seen = 3;
15820 break;
15821 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15822 case GOMP_MAP_ALWAYS_POINTER:
15823 break;
15824 default:
15825 map_seen |= 1;
15826 error_at (OMP_CLAUSE_LOCATION (*pc),
15827 "%<#pragma omp target data%> with map-type other "
15828 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
15829 "on %<map%> clause");
15830 *pc = OMP_CLAUSE_CHAIN (*pc);
15831 continue;
15833 pc = &OMP_CLAUSE_CHAIN (*pc);
15836 if (map_seen != 3)
15838 if (map_seen == 0)
15839 error_at (loc,
15840 "%<#pragma omp target data%> must contain at least "
15841 "one %<map%> clause");
15842 return NULL_TREE;
15845 tree stmt = make_node (OMP_TARGET_DATA);
15846 TREE_TYPE (stmt) = void_type_node;
15847 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
15848 keep_next_level ();
15849 tree block = c_begin_compound_stmt (true);
15850 add_stmt (c_parser_omp_structured_block (parser, if_p));
15851 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
15853 SET_EXPR_LOCATION (stmt, loc);
15854 return add_stmt (stmt);
15857 /* OpenMP 4.0:
15858 # pragma omp target update target-update-clause[optseq] new-line */
15860 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
15861 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
15862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
15863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15868 static bool
15869 c_parser_omp_target_update (location_t loc, c_parser *parser,
15870 enum pragma_context context)
15872 if (context == pragma_stmt)
15874 error_at (loc,
15875 "%<#pragma omp target update%> may only be "
15876 "used in compound statements");
15877 c_parser_skip_to_pragma_eol (parser, false);
15878 return false;
15881 tree clauses
15882 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
15883 "#pragma omp target update");
15884 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
15885 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
15887 error_at (loc,
15888 "%<#pragma omp target update%> must contain at least one "
15889 "%<from%> or %<to%> clauses");
15890 return false;
15893 tree stmt = make_node (OMP_TARGET_UPDATE);
15894 TREE_TYPE (stmt) = void_type_node;
15895 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
15896 SET_EXPR_LOCATION (stmt, loc);
15897 add_stmt (stmt);
15898 return false;
15901 /* OpenMP 4.5:
15902 # pragma omp target enter data target-data-clause[optseq] new-line */
15904 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
15905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15911 static tree
15912 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
15913 enum pragma_context context)
15915 bool data_seen = false;
15916 if (c_parser_next_token_is (parser, CPP_NAME))
15918 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15919 if (strcmp (p, "data") == 0)
15921 c_parser_consume_token (parser);
15922 data_seen = true;
15925 if (!data_seen)
15927 c_parser_error (parser, "expected %<data%>");
15928 c_parser_skip_to_pragma_eol (parser);
15929 return NULL_TREE;
15932 if (context == pragma_stmt)
15934 error_at (loc,
15935 "%<#pragma omp target enter data%> may only be "
15936 "used in compound statements");
15937 c_parser_skip_to_pragma_eol (parser, false);
15938 return NULL_TREE;
15941 tree clauses
15942 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
15943 "#pragma omp target enter data");
15944 int map_seen = 0;
15945 for (tree *pc = &clauses; *pc;)
15947 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15948 switch (OMP_CLAUSE_MAP_KIND (*pc))
15950 case GOMP_MAP_TO:
15951 case GOMP_MAP_ALWAYS_TO:
15952 case GOMP_MAP_ALLOC:
15953 map_seen = 3;
15954 break;
15955 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15956 case GOMP_MAP_ALWAYS_POINTER:
15957 break;
15958 default:
15959 map_seen |= 1;
15960 error_at (OMP_CLAUSE_LOCATION (*pc),
15961 "%<#pragma omp target enter data%> with map-type other "
15962 "than %<to%> or %<alloc%> on %<map%> clause");
15963 *pc = OMP_CLAUSE_CHAIN (*pc);
15964 continue;
15966 pc = &OMP_CLAUSE_CHAIN (*pc);
15969 if (map_seen != 3)
15971 if (map_seen == 0)
15972 error_at (loc,
15973 "%<#pragma omp target enter data%> must contain at least "
15974 "one %<map%> clause");
15975 return NULL_TREE;
15978 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
15979 TREE_TYPE (stmt) = void_type_node;
15980 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
15981 SET_EXPR_LOCATION (stmt, loc);
15982 add_stmt (stmt);
15983 return stmt;
15986 /* OpenMP 4.5:
15987 # pragma omp target exit data target-data-clause[optseq] new-line */
15989 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
15990 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15996 static tree
15997 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
15998 enum pragma_context context)
16000 bool data_seen = false;
16001 if (c_parser_next_token_is (parser, CPP_NAME))
16003 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16004 if (strcmp (p, "data") == 0)
16006 c_parser_consume_token (parser);
16007 data_seen = true;
16010 if (!data_seen)
16012 c_parser_error (parser, "expected %<data%>");
16013 c_parser_skip_to_pragma_eol (parser);
16014 return NULL_TREE;
16017 if (context == pragma_stmt)
16019 error_at (loc,
16020 "%<#pragma omp target exit data%> may only be "
16021 "used in compound statements");
16022 c_parser_skip_to_pragma_eol (parser, false);
16023 return NULL_TREE;
16026 tree clauses
16027 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16028 "#pragma omp target exit data");
16030 int map_seen = 0;
16031 for (tree *pc = &clauses; *pc;)
16033 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16034 switch (OMP_CLAUSE_MAP_KIND (*pc))
16036 case GOMP_MAP_FROM:
16037 case GOMP_MAP_ALWAYS_FROM:
16038 case GOMP_MAP_RELEASE:
16039 case GOMP_MAP_DELETE:
16040 map_seen = 3;
16041 break;
16042 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16043 case GOMP_MAP_ALWAYS_POINTER:
16044 break;
16045 default:
16046 map_seen |= 1;
16047 error_at (OMP_CLAUSE_LOCATION (*pc),
16048 "%<#pragma omp target exit data%> with map-type other "
16049 "than %<from%>, %<release> or %<delete%> on %<map%>"
16050 " clause");
16051 *pc = OMP_CLAUSE_CHAIN (*pc);
16052 continue;
16054 pc = &OMP_CLAUSE_CHAIN (*pc);
16057 if (map_seen != 3)
16059 if (map_seen == 0)
16060 error_at (loc,
16061 "%<#pragma omp target exit data%> must contain at least one "
16062 "%<map%> clause");
16063 return NULL_TREE;
16066 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16067 TREE_TYPE (stmt) = void_type_node;
16068 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16069 SET_EXPR_LOCATION (stmt, loc);
16070 add_stmt (stmt);
16071 return stmt;
16074 /* OpenMP 4.0:
16075 # pragma omp target target-clause[optseq] new-line
16076 structured-block */
16078 #define OMP_TARGET_CLAUSE_MASK \
16079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16089 static bool
16090 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16092 location_t loc = c_parser_peek_token (parser)->location;
16093 c_parser_consume_pragma (parser);
16094 tree *pc = NULL, stmt, block;
16096 if (context != pragma_stmt && context != pragma_compound)
16098 c_parser_error (parser, "expected declaration specifiers");
16099 c_parser_skip_to_pragma_eol (parser);
16100 return false;
16103 if (c_parser_next_token_is (parser, CPP_NAME))
16105 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16106 enum tree_code ccode = ERROR_MARK;
16108 if (strcmp (p, "teams") == 0)
16109 ccode = OMP_TEAMS;
16110 else if (strcmp (p, "parallel") == 0)
16111 ccode = OMP_PARALLEL;
16112 else if (strcmp (p, "simd") == 0)
16113 ccode = OMP_SIMD;
16114 if (ccode != ERROR_MARK)
16116 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16117 char p_name[sizeof ("#pragma omp target teams distribute "
16118 "parallel for simd")];
16120 c_parser_consume_token (parser);
16121 strcpy (p_name, "#pragma omp target");
16122 if (!flag_openmp) /* flag_openmp_simd */
16124 tree stmt;
16125 switch (ccode)
16127 case OMP_TEAMS:
16128 stmt = c_parser_omp_teams (loc, parser, p_name,
16129 OMP_TARGET_CLAUSE_MASK,
16130 cclauses, if_p);
16131 break;
16132 case OMP_PARALLEL:
16133 stmt = c_parser_omp_parallel (loc, parser, p_name,
16134 OMP_TARGET_CLAUSE_MASK,
16135 cclauses, if_p);
16136 break;
16137 case OMP_SIMD:
16138 stmt = c_parser_omp_simd (loc, parser, p_name,
16139 OMP_TARGET_CLAUSE_MASK,
16140 cclauses, if_p);
16141 break;
16142 default:
16143 gcc_unreachable ();
16145 return stmt != NULL_TREE;
16147 keep_next_level ();
16148 tree block = c_begin_compound_stmt (true), ret;
16149 switch (ccode)
16151 case OMP_TEAMS:
16152 ret = c_parser_omp_teams (loc, parser, p_name,
16153 OMP_TARGET_CLAUSE_MASK, cclauses,
16154 if_p);
16155 break;
16156 case OMP_PARALLEL:
16157 ret = c_parser_omp_parallel (loc, parser, p_name,
16158 OMP_TARGET_CLAUSE_MASK, cclauses,
16159 if_p);
16160 break;
16161 case OMP_SIMD:
16162 ret = c_parser_omp_simd (loc, parser, p_name,
16163 OMP_TARGET_CLAUSE_MASK, cclauses,
16164 if_p);
16165 break;
16166 default:
16167 gcc_unreachable ();
16169 block = c_end_compound_stmt (loc, block, true);
16170 if (ret == NULL_TREE)
16171 return false;
16172 if (ccode == OMP_TEAMS)
16174 /* For combined target teams, ensure the num_teams and
16175 thread_limit clause expressions are evaluated on the host,
16176 before entering the target construct. */
16177 tree c;
16178 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16179 c; c = OMP_CLAUSE_CHAIN (c))
16180 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16181 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16182 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16184 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16185 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16186 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16187 expr, NULL_TREE, NULL_TREE);
16188 add_stmt (expr);
16189 OMP_CLAUSE_OPERAND (c, 0) = expr;
16190 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16191 OMP_CLAUSE_FIRSTPRIVATE);
16192 OMP_CLAUSE_DECL (tc) = tmp;
16193 OMP_CLAUSE_CHAIN (tc)
16194 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16195 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16198 tree stmt = make_node (OMP_TARGET);
16199 TREE_TYPE (stmt) = void_type_node;
16200 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16201 OMP_TARGET_BODY (stmt) = block;
16202 OMP_TARGET_COMBINED (stmt) = 1;
16203 add_stmt (stmt);
16204 pc = &OMP_TARGET_CLAUSES (stmt);
16205 goto check_clauses;
16207 else if (!flag_openmp) /* flag_openmp_simd */
16209 c_parser_skip_to_pragma_eol (parser, false);
16210 return false;
16212 else if (strcmp (p, "data") == 0)
16214 c_parser_consume_token (parser);
16215 c_parser_omp_target_data (loc, parser, if_p);
16216 return true;
16218 else if (strcmp (p, "enter") == 0)
16220 c_parser_consume_token (parser);
16221 c_parser_omp_target_enter_data (loc, parser, context);
16222 return false;
16224 else if (strcmp (p, "exit") == 0)
16226 c_parser_consume_token (parser);
16227 c_parser_omp_target_exit_data (loc, parser, context);
16228 return false;
16230 else if (strcmp (p, "update") == 0)
16232 c_parser_consume_token (parser);
16233 return c_parser_omp_target_update (loc, parser, context);
16237 stmt = make_node (OMP_TARGET);
16238 TREE_TYPE (stmt) = void_type_node;
16240 OMP_TARGET_CLAUSES (stmt)
16241 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16242 "#pragma omp target");
16243 pc = &OMP_TARGET_CLAUSES (stmt);
16244 keep_next_level ();
16245 block = c_begin_compound_stmt (true);
16246 add_stmt (c_parser_omp_structured_block (parser, if_p));
16247 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16249 SET_EXPR_LOCATION (stmt, loc);
16250 add_stmt (stmt);
16252 check_clauses:
16253 while (*pc)
16255 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16256 switch (OMP_CLAUSE_MAP_KIND (*pc))
16258 case GOMP_MAP_TO:
16259 case GOMP_MAP_ALWAYS_TO:
16260 case GOMP_MAP_FROM:
16261 case GOMP_MAP_ALWAYS_FROM:
16262 case GOMP_MAP_TOFROM:
16263 case GOMP_MAP_ALWAYS_TOFROM:
16264 case GOMP_MAP_ALLOC:
16265 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16266 case GOMP_MAP_ALWAYS_POINTER:
16267 break;
16268 default:
16269 error_at (OMP_CLAUSE_LOCATION (*pc),
16270 "%<#pragma omp target%> with map-type other "
16271 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16272 "on %<map%> clause");
16273 *pc = OMP_CLAUSE_CHAIN (*pc);
16274 continue;
16276 pc = &OMP_CLAUSE_CHAIN (*pc);
16278 return true;
16281 /* OpenMP 4.0:
16282 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16284 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16292 static void
16293 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16295 vec<c_token> clauses = vNULL;
16296 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16298 c_token *token = c_parser_peek_token (parser);
16299 if (token->type == CPP_EOF)
16301 c_parser_skip_to_pragma_eol (parser);
16302 clauses.release ();
16303 return;
16305 clauses.safe_push (*token);
16306 c_parser_consume_token (parser);
16308 clauses.safe_push (*c_parser_peek_token (parser));
16309 c_parser_skip_to_pragma_eol (parser);
16311 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16313 if (c_parser_peek_token (parser)->pragma_kind
16314 != PRAGMA_OMP_DECLARE_REDUCTION
16315 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16316 || strcmp (IDENTIFIER_POINTER
16317 (c_parser_peek_2nd_token (parser)->value),
16318 "simd") != 0)
16320 c_parser_error (parser,
16321 "%<#pragma omp declare simd%> must be followed by "
16322 "function declaration or definition or another "
16323 "%<#pragma omp declare simd%>");
16324 clauses.release ();
16325 return;
16327 c_parser_consume_pragma (parser);
16328 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16330 c_token *token = c_parser_peek_token (parser);
16331 if (token->type == CPP_EOF)
16333 c_parser_skip_to_pragma_eol (parser);
16334 clauses.release ();
16335 return;
16337 clauses.safe_push (*token);
16338 c_parser_consume_token (parser);
16340 clauses.safe_push (*c_parser_peek_token (parser));
16341 c_parser_skip_to_pragma_eol (parser);
16344 /* Make sure nothing tries to read past the end of the tokens. */
16345 c_token eof_token;
16346 memset (&eof_token, 0, sizeof (eof_token));
16347 eof_token.type = CPP_EOF;
16348 clauses.safe_push (eof_token);
16349 clauses.safe_push (eof_token);
16351 switch (context)
16353 case pragma_external:
16354 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16355 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16357 int ext = disable_extension_diagnostics ();
16359 c_parser_consume_token (parser);
16360 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16361 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16362 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16363 NULL, clauses);
16364 restore_extension_diagnostics (ext);
16366 else
16367 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16368 NULL, clauses);
16369 break;
16370 case pragma_struct:
16371 case pragma_param:
16372 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16373 "function declaration or definition");
16374 break;
16375 case pragma_compound:
16376 case pragma_stmt:
16377 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16378 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16380 int ext = disable_extension_diagnostics ();
16382 c_parser_consume_token (parser);
16383 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16384 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16385 if (c_parser_next_tokens_start_declaration (parser))
16387 c_parser_declaration_or_fndef (parser, true, true, true, true,
16388 true, NULL, clauses);
16389 restore_extension_diagnostics (ext);
16390 break;
16392 restore_extension_diagnostics (ext);
16394 else if (c_parser_next_tokens_start_declaration (parser))
16396 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16397 NULL, clauses);
16398 break;
16400 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16401 "function declaration or definition");
16402 break;
16403 default:
16404 gcc_unreachable ();
16406 clauses.release ();
16409 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16410 and put that into "omp declare simd" attribute. */
16412 static void
16413 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16414 vec<c_token> clauses)
16416 if (flag_cilkplus
16417 && (clauses.exists ()
16418 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16419 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16421 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16422 "used in the same function marked as a Cilk Plus SIMD-enabled "
16423 "function");
16424 vec_free (parser->cilk_simd_fn_tokens);
16425 return;
16428 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16429 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16430 has already processed the tokens. */
16431 if (clauses.exists () && clauses[0].type == CPP_EOF)
16432 return;
16433 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16435 error ("%<#pragma omp declare simd%> not immediately followed by "
16436 "a function declaration or definition");
16437 clauses[0].type = CPP_EOF;
16438 return;
16440 if (clauses.exists () && clauses[0].type != CPP_NAME)
16442 error_at (DECL_SOURCE_LOCATION (fndecl),
16443 "%<#pragma omp declare simd%> not immediately followed by "
16444 "a single function declaration or definition");
16445 clauses[0].type = CPP_EOF;
16446 return;
16449 if (parms == NULL_TREE)
16450 parms = DECL_ARGUMENTS (fndecl);
16452 unsigned int tokens_avail = parser->tokens_avail;
16453 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16454 bool is_cilkplus_cilk_simd_fn = false;
16456 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16458 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16459 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16460 is_cilkplus_cilk_simd_fn = true;
16462 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16464 error_at (DECL_SOURCE_LOCATION (fndecl),
16465 "%<__simd__%> attribute cannot be used in the same "
16466 "function marked as a Cilk Plus SIMD-enabled function");
16467 vec_free (parser->cilk_simd_fn_tokens);
16468 return;
16472 else
16474 parser->tokens = clauses.address ();
16475 parser->tokens_avail = clauses.length ();
16478 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16479 while (parser->tokens_avail > 3)
16481 c_token *token = c_parser_peek_token (parser);
16482 if (!is_cilkplus_cilk_simd_fn)
16483 gcc_assert (token->type == CPP_NAME
16484 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16485 else
16486 gcc_assert (token->type == CPP_NAME
16487 && is_cilkplus_vector_p (token->value));
16488 c_parser_consume_token (parser);
16489 parser->in_pragma = true;
16491 tree c = NULL_TREE;
16492 if (is_cilkplus_cilk_simd_fn)
16493 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16494 "SIMD-enabled functions attribute");
16495 else
16496 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16497 "#pragma omp declare simd");
16498 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16499 if (c != NULL_TREE)
16500 c = tree_cons (NULL_TREE, c, NULL_TREE);
16501 if (is_cilkplus_cilk_simd_fn)
16503 tree k = build_tree_list (get_identifier ("cilk simd function"),
16504 NULL_TREE);
16505 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16506 DECL_ATTRIBUTES (fndecl) = k;
16508 c = build_tree_list (get_identifier ("omp declare simd"), c);
16509 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16510 DECL_ATTRIBUTES (fndecl) = c;
16513 parser->tokens = &parser->tokens_buf[0];
16514 parser->tokens_avail = tokens_avail;
16515 if (clauses.exists ())
16516 clauses[0].type = CPP_PRAGMA;
16518 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16519 vec_free (parser->cilk_simd_fn_tokens);
16523 /* OpenMP 4.0:
16524 # pragma omp declare target new-line
16525 declarations and definitions
16526 # pragma omp end declare target new-line
16528 OpenMP 4.5:
16529 # pragma omp declare target ( extended-list ) new-line
16531 # pragma omp declare target declare-target-clauses[seq] new-line */
16533 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16534 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16537 static void
16538 c_parser_omp_declare_target (c_parser *parser)
16540 location_t loc = c_parser_peek_token (parser)->location;
16541 tree clauses = NULL_TREE;
16542 if (c_parser_next_token_is (parser, CPP_NAME))
16543 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16544 "#pragma omp declare target");
16545 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16547 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16548 clauses);
16549 clauses = c_finish_omp_clauses (clauses, true);
16550 c_parser_skip_to_pragma_eol (parser);
16552 else
16554 c_parser_skip_to_pragma_eol (parser);
16555 current_omp_declare_target_attribute++;
16556 return;
16558 if (current_omp_declare_target_attribute)
16559 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16560 "%<#pragma omp declare target%> without clauses and "
16561 "%<#pragma omp end declare target%>");
16562 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16564 tree t = OMP_CLAUSE_DECL (c), id;
16565 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16566 tree at2 = lookup_attribute ("omp declare target link",
16567 DECL_ATTRIBUTES (t));
16568 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16570 id = get_identifier ("omp declare target link");
16571 std::swap (at1, at2);
16573 else
16574 id = get_identifier ("omp declare target");
16575 if (at2)
16577 error_at (OMP_CLAUSE_LOCATION (c),
16578 "%qD specified both in declare target %<link%> and %<to%>"
16579 " clauses", t);
16580 continue;
16582 if (!at1)
16584 symtab_node *node = symtab_node::get (t);
16585 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16586 if (node != NULL)
16588 node->offloadable = 1;
16589 if (ENABLE_OFFLOADING)
16591 g->have_offload = true;
16592 if (is_a <varpool_node *> (node))
16593 vec_safe_push (offload_vars, t);
16600 static void
16601 c_parser_omp_end_declare_target (c_parser *parser)
16603 location_t loc = c_parser_peek_token (parser)->location;
16604 c_parser_consume_pragma (parser);
16605 if (c_parser_next_token_is (parser, CPP_NAME)
16606 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16607 "declare") == 0)
16609 c_parser_consume_token (parser);
16610 if (c_parser_next_token_is (parser, CPP_NAME)
16611 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16612 "target") == 0)
16613 c_parser_consume_token (parser);
16614 else
16616 c_parser_error (parser, "expected %<target%>");
16617 c_parser_skip_to_pragma_eol (parser);
16618 return;
16621 else
16623 c_parser_error (parser, "expected %<declare%>");
16624 c_parser_skip_to_pragma_eol (parser);
16625 return;
16627 c_parser_skip_to_pragma_eol (parser);
16628 if (!current_omp_declare_target_attribute)
16629 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16630 "%<#pragma omp declare target%>");
16631 else
16632 current_omp_declare_target_attribute--;
16636 /* OpenMP 4.0
16637 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16638 initializer-clause[opt] new-line
16640 initializer-clause:
16641 initializer (omp_priv = initializer)
16642 initializer (function-name (argument-list)) */
16644 static void
16645 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16647 unsigned int tokens_avail = 0, i;
16648 vec<tree> types = vNULL;
16649 vec<c_token> clauses = vNULL;
16650 enum tree_code reduc_code = ERROR_MARK;
16651 tree reduc_id = NULL_TREE;
16652 tree type;
16653 location_t rloc = c_parser_peek_token (parser)->location;
16655 if (context == pragma_struct || context == pragma_param)
16657 error ("%<#pragma omp declare reduction%> not at file or block scope");
16658 goto fail;
16661 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16662 goto fail;
16664 switch (c_parser_peek_token (parser)->type)
16666 case CPP_PLUS:
16667 reduc_code = PLUS_EXPR;
16668 break;
16669 case CPP_MULT:
16670 reduc_code = MULT_EXPR;
16671 break;
16672 case CPP_MINUS:
16673 reduc_code = MINUS_EXPR;
16674 break;
16675 case CPP_AND:
16676 reduc_code = BIT_AND_EXPR;
16677 break;
16678 case CPP_XOR:
16679 reduc_code = BIT_XOR_EXPR;
16680 break;
16681 case CPP_OR:
16682 reduc_code = BIT_IOR_EXPR;
16683 break;
16684 case CPP_AND_AND:
16685 reduc_code = TRUTH_ANDIF_EXPR;
16686 break;
16687 case CPP_OR_OR:
16688 reduc_code = TRUTH_ORIF_EXPR;
16689 break;
16690 case CPP_NAME:
16691 const char *p;
16692 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16693 if (strcmp (p, "min") == 0)
16695 reduc_code = MIN_EXPR;
16696 break;
16698 if (strcmp (p, "max") == 0)
16700 reduc_code = MAX_EXPR;
16701 break;
16703 reduc_id = c_parser_peek_token (parser)->value;
16704 break;
16705 default:
16706 c_parser_error (parser,
16707 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16708 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16709 goto fail;
16712 tree orig_reduc_id, reduc_decl;
16713 orig_reduc_id = reduc_id;
16714 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16715 reduc_decl = c_omp_reduction_decl (reduc_id);
16716 c_parser_consume_token (parser);
16718 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16719 goto fail;
16721 while (true)
16723 location_t loc = c_parser_peek_token (parser)->location;
16724 struct c_type_name *ctype = c_parser_type_name (parser);
16725 if (ctype != NULL)
16727 type = groktypename (ctype, NULL, NULL);
16728 if (type == error_mark_node)
16730 else if ((INTEGRAL_TYPE_P (type)
16731 || TREE_CODE (type) == REAL_TYPE
16732 || TREE_CODE (type) == COMPLEX_TYPE)
16733 && orig_reduc_id == NULL_TREE)
16734 error_at (loc, "predeclared arithmetic type in "
16735 "%<#pragma omp declare reduction%>");
16736 else if (TREE_CODE (type) == FUNCTION_TYPE
16737 || TREE_CODE (type) == ARRAY_TYPE)
16738 error_at (loc, "function or array type in "
16739 "%<#pragma omp declare reduction%>");
16740 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
16741 error_at (loc, "const, volatile or restrict qualified type in "
16742 "%<#pragma omp declare reduction%>");
16743 else
16745 tree t;
16746 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
16747 if (comptypes (TREE_PURPOSE (t), type))
16749 error_at (loc, "redeclaration of %qs "
16750 "%<#pragma omp declare reduction%> for "
16751 "type %qT",
16752 IDENTIFIER_POINTER (reduc_id)
16753 + sizeof ("omp declare reduction ") - 1,
16754 type);
16755 location_t ploc
16756 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
16757 0));
16758 error_at (ploc, "previous %<#pragma omp declare "
16759 "reduction%>");
16760 break;
16762 if (t == NULL_TREE)
16763 types.safe_push (type);
16765 if (c_parser_next_token_is (parser, CPP_COMMA))
16766 c_parser_consume_token (parser);
16767 else
16768 break;
16770 else
16771 break;
16774 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
16775 || types.is_empty ())
16777 fail:
16778 clauses.release ();
16779 types.release ();
16780 while (true)
16782 c_token *token = c_parser_peek_token (parser);
16783 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
16784 break;
16785 c_parser_consume_token (parser);
16787 c_parser_skip_to_pragma_eol (parser);
16788 return;
16791 if (types.length () > 1)
16793 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16795 c_token *token = c_parser_peek_token (parser);
16796 if (token->type == CPP_EOF)
16797 goto fail;
16798 clauses.safe_push (*token);
16799 c_parser_consume_token (parser);
16801 clauses.safe_push (*c_parser_peek_token (parser));
16802 c_parser_skip_to_pragma_eol (parser);
16804 /* Make sure nothing tries to read past the end of the tokens. */
16805 c_token eof_token;
16806 memset (&eof_token, 0, sizeof (eof_token));
16807 eof_token.type = CPP_EOF;
16808 clauses.safe_push (eof_token);
16809 clauses.safe_push (eof_token);
16812 int errs = errorcount;
16813 FOR_EACH_VEC_ELT (types, i, type)
16815 tokens_avail = parser->tokens_avail;
16816 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16817 if (!clauses.is_empty ())
16819 parser->tokens = clauses.address ();
16820 parser->tokens_avail = clauses.length ();
16821 parser->in_pragma = true;
16824 bool nested = current_function_decl != NULL_TREE;
16825 if (nested)
16826 c_push_function_context ();
16827 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
16828 reduc_id, default_function_type);
16829 current_function_decl = fndecl;
16830 allocate_struct_function (fndecl, true);
16831 push_scope ();
16832 tree stmt = push_stmt_list ();
16833 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
16834 warn about these. */
16835 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
16836 get_identifier ("omp_out"), type);
16837 DECL_ARTIFICIAL (omp_out) = 1;
16838 DECL_CONTEXT (omp_out) = fndecl;
16839 pushdecl (omp_out);
16840 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
16841 get_identifier ("omp_in"), type);
16842 DECL_ARTIFICIAL (omp_in) = 1;
16843 DECL_CONTEXT (omp_in) = fndecl;
16844 pushdecl (omp_in);
16845 struct c_expr combiner = c_parser_expression (parser);
16846 struct c_expr initializer;
16847 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
16848 bool bad = false;
16849 initializer.value = error_mark_node;
16850 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16851 bad = true;
16852 else if (c_parser_next_token_is (parser, CPP_NAME)
16853 && strcmp (IDENTIFIER_POINTER
16854 (c_parser_peek_token (parser)->value),
16855 "initializer") == 0)
16857 c_parser_consume_token (parser);
16858 pop_scope ();
16859 push_scope ();
16860 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
16861 get_identifier ("omp_priv"), type);
16862 DECL_ARTIFICIAL (omp_priv) = 1;
16863 DECL_INITIAL (omp_priv) = error_mark_node;
16864 DECL_CONTEXT (omp_priv) = fndecl;
16865 pushdecl (omp_priv);
16866 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
16867 get_identifier ("omp_orig"), type);
16868 DECL_ARTIFICIAL (omp_orig) = 1;
16869 DECL_CONTEXT (omp_orig) = fndecl;
16870 pushdecl (omp_orig);
16871 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16872 bad = true;
16873 else if (!c_parser_next_token_is (parser, CPP_NAME))
16875 c_parser_error (parser, "expected %<omp_priv%> or "
16876 "function-name");
16877 bad = true;
16879 else if (strcmp (IDENTIFIER_POINTER
16880 (c_parser_peek_token (parser)->value),
16881 "omp_priv") != 0)
16883 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
16884 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
16886 c_parser_error (parser, "expected function-name %<(%>");
16887 bad = true;
16889 else
16890 initializer = c_parser_postfix_expression (parser);
16891 if (initializer.value
16892 && TREE_CODE (initializer.value) == CALL_EXPR)
16894 int j;
16895 tree c = initializer.value;
16896 for (j = 0; j < call_expr_nargs (c); j++)
16898 tree a = CALL_EXPR_ARG (c, j);
16899 STRIP_NOPS (a);
16900 if (TREE_CODE (a) == ADDR_EXPR
16901 && TREE_OPERAND (a, 0) == omp_priv)
16902 break;
16904 if (j == call_expr_nargs (c))
16905 error ("one of the initializer call arguments should be "
16906 "%<&omp_priv%>");
16909 else
16911 c_parser_consume_token (parser);
16912 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16913 bad = true;
16914 else
16916 tree st = push_stmt_list ();
16917 start_init (omp_priv, NULL_TREE, 0);
16918 location_t loc = c_parser_peek_token (parser)->location;
16919 struct c_expr init = c_parser_initializer (parser);
16920 finish_init ();
16921 finish_decl (omp_priv, loc, init.value,
16922 init.original_type, NULL_TREE);
16923 pop_stmt_list (st);
16926 if (!bad
16927 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16928 bad = true;
16931 if (!bad)
16933 c_parser_skip_to_pragma_eol (parser);
16935 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
16936 DECL_INITIAL (reduc_decl));
16937 DECL_INITIAL (reduc_decl) = t;
16938 DECL_SOURCE_LOCATION (omp_out) = rloc;
16939 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
16940 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
16941 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
16942 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
16943 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
16944 if (omp_priv)
16946 DECL_SOURCE_LOCATION (omp_priv) = rloc;
16947 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
16948 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
16949 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
16950 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
16951 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16952 walk_tree (&DECL_INITIAL (omp_priv),
16953 c_check_omp_declare_reduction_r,
16954 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16958 pop_stmt_list (stmt);
16959 pop_scope ();
16960 if (cfun->language != NULL)
16962 ggc_free (cfun->language);
16963 cfun->language = NULL;
16965 set_cfun (NULL);
16966 current_function_decl = NULL_TREE;
16967 if (nested)
16968 c_pop_function_context ();
16970 if (!clauses.is_empty ())
16972 parser->tokens = &parser->tokens_buf[0];
16973 parser->tokens_avail = tokens_avail;
16975 if (bad)
16976 goto fail;
16977 if (errs != errorcount)
16978 break;
16981 clauses.release ();
16982 types.release ();
16986 /* OpenMP 4.0
16987 #pragma omp declare simd declare-simd-clauses[optseq] new-line
16988 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16989 initializer-clause[opt] new-line
16990 #pragma omp declare target new-line */
16992 static void
16993 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
16995 c_parser_consume_pragma (parser);
16996 if (c_parser_next_token_is (parser, CPP_NAME))
16998 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16999 if (strcmp (p, "simd") == 0)
17001 /* c_parser_consume_token (parser); done in
17002 c_parser_omp_declare_simd. */
17003 c_parser_omp_declare_simd (parser, context);
17004 return;
17006 if (strcmp (p, "reduction") == 0)
17008 c_parser_consume_token (parser);
17009 c_parser_omp_declare_reduction (parser, context);
17010 return;
17012 if (!flag_openmp) /* flag_openmp_simd */
17014 c_parser_skip_to_pragma_eol (parser, false);
17015 return;
17017 if (strcmp (p, "target") == 0)
17019 c_parser_consume_token (parser);
17020 c_parser_omp_declare_target (parser);
17021 return;
17025 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17026 "or %<target%>");
17027 c_parser_skip_to_pragma_eol (parser);
17030 /* OpenMP 4.5:
17031 #pragma omp taskloop taskloop-clause[optseq] new-line
17032 for-loop
17034 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17035 for-loop */
17037 #define OMP_TASKLOOP_CLAUSE_MASK \
17038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17053 static tree
17054 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17055 char *p_name, omp_clause_mask mask, tree *cclauses,
17056 bool *if_p)
17058 tree clauses, block, ret;
17060 strcat (p_name, " taskloop");
17061 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17063 if (c_parser_next_token_is (parser, CPP_NAME))
17065 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17067 if (strcmp (p, "simd") == 0)
17069 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17070 if (cclauses == NULL)
17071 cclauses = cclauses_buf;
17072 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17073 c_parser_consume_token (parser);
17074 if (!flag_openmp) /* flag_openmp_simd */
17075 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17076 if_p);
17077 block = c_begin_compound_stmt (true);
17078 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17079 block = c_end_compound_stmt (loc, block, true);
17080 if (ret == NULL)
17081 return ret;
17082 ret = make_node (OMP_TASKLOOP);
17083 TREE_TYPE (ret) = void_type_node;
17084 OMP_FOR_BODY (ret) = block;
17085 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17086 SET_EXPR_LOCATION (ret, loc);
17087 add_stmt (ret);
17088 return ret;
17091 if (!flag_openmp) /* flag_openmp_simd */
17093 c_parser_skip_to_pragma_eol (parser, false);
17094 return NULL_TREE;
17097 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17098 if (cclauses)
17100 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17101 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17104 block = c_begin_compound_stmt (true);
17105 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17106 block = c_end_compound_stmt (loc, block, true);
17107 add_stmt (block);
17109 return ret;
17112 /* Main entry point to parsing most OpenMP pragmas. */
17114 static void
17115 c_parser_omp_construct (c_parser *parser, bool *if_p)
17117 enum pragma_kind p_kind;
17118 location_t loc;
17119 tree stmt;
17120 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17121 omp_clause_mask mask (0);
17123 loc = c_parser_peek_token (parser)->location;
17124 p_kind = c_parser_peek_token (parser)->pragma_kind;
17125 c_parser_consume_pragma (parser);
17127 switch (p_kind)
17129 case PRAGMA_OACC_ATOMIC:
17130 c_parser_omp_atomic (loc, parser);
17131 return;
17132 case PRAGMA_OACC_CACHE:
17133 strcpy (p_name, "#pragma acc");
17134 stmt = c_parser_oacc_cache (loc, parser);
17135 break;
17136 case PRAGMA_OACC_DATA:
17137 stmt = c_parser_oacc_data (loc, parser, if_p);
17138 break;
17139 case PRAGMA_OACC_HOST_DATA:
17140 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17141 break;
17142 case PRAGMA_OACC_KERNELS:
17143 case PRAGMA_OACC_PARALLEL:
17144 strcpy (p_name, "#pragma acc");
17145 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17146 if_p);
17147 break;
17148 case PRAGMA_OACC_LOOP:
17149 strcpy (p_name, "#pragma acc");
17150 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17151 break;
17152 case PRAGMA_OACC_WAIT:
17153 strcpy (p_name, "#pragma wait");
17154 stmt = c_parser_oacc_wait (loc, parser, p_name);
17155 break;
17156 case PRAGMA_OMP_ATOMIC:
17157 c_parser_omp_atomic (loc, parser);
17158 return;
17159 case PRAGMA_OMP_CRITICAL:
17160 stmt = c_parser_omp_critical (loc, parser, if_p);
17161 break;
17162 case PRAGMA_OMP_DISTRIBUTE:
17163 strcpy (p_name, "#pragma omp");
17164 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17165 break;
17166 case PRAGMA_OMP_FOR:
17167 strcpy (p_name, "#pragma omp");
17168 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17169 break;
17170 case PRAGMA_OMP_MASTER:
17171 stmt = c_parser_omp_master (loc, parser, if_p);
17172 break;
17173 case PRAGMA_OMP_PARALLEL:
17174 strcpy (p_name, "#pragma omp");
17175 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17176 break;
17177 case PRAGMA_OMP_SECTIONS:
17178 strcpy (p_name, "#pragma omp");
17179 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17180 break;
17181 case PRAGMA_OMP_SIMD:
17182 strcpy (p_name, "#pragma omp");
17183 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17184 break;
17185 case PRAGMA_OMP_SINGLE:
17186 stmt = c_parser_omp_single (loc, parser, if_p);
17187 break;
17188 case PRAGMA_OMP_TASK:
17189 stmt = c_parser_omp_task (loc, parser, if_p);
17190 break;
17191 case PRAGMA_OMP_TASKGROUP:
17192 stmt = c_parser_omp_taskgroup (parser, if_p);
17193 break;
17194 case PRAGMA_OMP_TASKLOOP:
17195 strcpy (p_name, "#pragma omp");
17196 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17197 break;
17198 case PRAGMA_OMP_TEAMS:
17199 strcpy (p_name, "#pragma omp");
17200 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17201 break;
17202 default:
17203 gcc_unreachable ();
17206 if (stmt)
17207 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17211 /* OpenMP 2.5:
17212 # pragma omp threadprivate (variable-list) */
17214 static void
17215 c_parser_omp_threadprivate (c_parser *parser)
17217 tree vars, t;
17218 location_t loc;
17220 c_parser_consume_pragma (parser);
17221 loc = c_parser_peek_token (parser)->location;
17222 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17224 /* Mark every variable in VARS to be assigned thread local storage. */
17225 for (t = vars; t; t = TREE_CHAIN (t))
17227 tree v = TREE_PURPOSE (t);
17229 /* FIXME diagnostics: Ideally we should keep individual
17230 locations for all the variables in the var list to make the
17231 following errors more precise. Perhaps
17232 c_parser_omp_var_list_parens() should construct a list of
17233 locations to go along with the var list. */
17235 /* If V had already been marked threadprivate, it doesn't matter
17236 whether it had been used prior to this point. */
17237 if (!VAR_P (v))
17238 error_at (loc, "%qD is not a variable", v);
17239 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17240 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17241 else if (! is_global_var (v))
17242 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17243 else if (TREE_TYPE (v) == error_mark_node)
17245 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17246 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17247 else
17249 if (! DECL_THREAD_LOCAL_P (v))
17251 set_decl_tls_model (v, decl_default_tls_model (v));
17252 /* If rtl has been already set for this var, call
17253 make_decl_rtl once again, so that encode_section_info
17254 has a chance to look at the new decl flags. */
17255 if (DECL_RTL_SET_P (v))
17256 make_decl_rtl (v);
17258 C_DECL_THREADPRIVATE_P (v) = 1;
17262 c_parser_skip_to_pragma_eol (parser);
17265 /* Cilk Plus <#pragma simd> parsing routines. */
17267 /* Helper function for c_parser_pragma. Perform some sanity checking
17268 for <#pragma simd> constructs. Returns FALSE if there was a
17269 problem. */
17271 static bool
17272 c_parser_cilk_verify_simd (c_parser *parser,
17273 enum pragma_context context)
17275 if (!flag_cilkplus)
17277 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17278 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17279 return false;
17281 if (context == pragma_external)
17283 c_parser_error (parser,"pragma simd must be inside a function");
17284 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17285 return false;
17287 return true;
17290 /* Cilk Plus:
17291 This function is shared by SIMD-enabled functions and #pragma simd.
17292 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17293 CLAUSES is unused. The main purpose of this function is to parse a
17294 vectorlength attribute or clause and check for parse errors.
17295 When IS_SIMD_FN is true then the function is merely caching the tokens
17296 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17297 cache is cleared since there is no reason to continue.
17298 Syntax:
17299 vectorlength ( constant-expression ) */
17301 static tree
17302 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17303 bool is_simd_fn)
17305 if (is_simd_fn)
17306 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17307 else
17308 /* The vectorlength clause behaves exactly like OpenMP's safelen
17309 clause. Represent it in OpenMP terms. */
17310 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17312 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17313 return clauses;
17315 location_t loc = c_parser_peek_token (parser)->location;
17316 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17317 expr = c_fully_fold (expr, false, NULL);
17319 /* If expr is an error_mark_node then the above function would have
17320 emitted an error. No reason to do it twice. */
17321 if (expr == error_mark_node)
17323 else if (!TREE_TYPE (expr)
17324 || !TREE_CONSTANT (expr)
17325 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17327 error_at (loc, "vectorlength must be an integer constant");
17328 else if (wi::exact_log2 (expr) == -1)
17329 error_at (loc, "vectorlength must be a power of 2");
17330 else
17332 if (is_simd_fn)
17334 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17335 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17336 OMP_CLAUSE_CHAIN (u) = clauses;
17337 clauses = u;
17339 else
17341 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17342 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17343 OMP_CLAUSE_CHAIN (u) = clauses;
17344 clauses = u;
17348 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17350 return clauses;
17353 /* Cilk Plus:
17354 linear ( simd-linear-variable-list )
17356 simd-linear-variable-list:
17357 simd-linear-variable
17358 simd-linear-variable-list , simd-linear-variable
17360 simd-linear-variable:
17361 id-expression
17362 id-expression : simd-linear-step
17364 simd-linear-step:
17365 conditional-expression */
17367 static tree
17368 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17370 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17371 return clauses;
17373 location_t loc = c_parser_peek_token (parser)->location;
17375 if (c_parser_next_token_is_not (parser, CPP_NAME)
17376 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17377 c_parser_error (parser, "expected identifier");
17379 while (c_parser_next_token_is (parser, CPP_NAME)
17380 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17382 tree var = lookup_name (c_parser_peek_token (parser)->value);
17384 if (var == NULL)
17386 undeclared_variable (c_parser_peek_token (parser)->location,
17387 c_parser_peek_token (parser)->value);
17388 c_parser_consume_token (parser);
17390 else if (var == error_mark_node)
17391 c_parser_consume_token (parser);
17392 else
17394 tree step = integer_one_node;
17396 /* Parse the linear step if present. */
17397 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17399 c_parser_consume_token (parser);
17400 c_parser_consume_token (parser);
17402 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17403 expr = c_fully_fold (expr, false, NULL);
17405 if (TREE_TYPE (expr)
17406 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17407 && (TREE_CONSTANT (expr)
17408 || DECL_P (expr)))
17409 step = expr;
17410 else
17411 c_parser_error (parser,
17412 "step size must be an integer constant "
17413 "expression or an integer variable");
17415 else
17416 c_parser_consume_token (parser);
17418 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17419 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17420 OMP_CLAUSE_DECL (u) = var;
17421 OMP_CLAUSE_LINEAR_STEP (u) = step;
17422 OMP_CLAUSE_CHAIN (u) = clauses;
17423 clauses = u;
17426 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17427 break;
17429 c_parser_consume_token (parser);
17432 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17434 return clauses;
17437 /* Returns the name of the next clause. If the clause is not
17438 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17439 not consumed. Otherwise, the appropriate pragma_simd_clause is
17440 returned and the token is consumed. */
17442 static pragma_omp_clause
17443 c_parser_cilk_clause_name (c_parser *parser)
17445 pragma_omp_clause result;
17446 c_token *token = c_parser_peek_token (parser);
17448 if (!token->value || token->type != CPP_NAME)
17449 return PRAGMA_CILK_CLAUSE_NONE;
17451 const char *p = IDENTIFIER_POINTER (token->value);
17453 if (!strcmp (p, "vectorlength"))
17454 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17455 else if (!strcmp (p, "linear"))
17456 result = PRAGMA_CILK_CLAUSE_LINEAR;
17457 else if (!strcmp (p, "private"))
17458 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17459 else if (!strcmp (p, "firstprivate"))
17460 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17461 else if (!strcmp (p, "lastprivate"))
17462 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17463 else if (!strcmp (p, "reduction"))
17464 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17465 else
17466 return PRAGMA_CILK_CLAUSE_NONE;
17468 c_parser_consume_token (parser);
17469 return result;
17472 /* Parse all #<pragma simd> clauses. Return the list of clauses
17473 found. */
17475 static tree
17476 c_parser_cilk_all_clauses (c_parser *parser)
17478 tree clauses = NULL;
17480 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17482 pragma_omp_clause c_kind;
17484 c_kind = c_parser_cilk_clause_name (parser);
17486 switch (c_kind)
17488 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17489 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17490 break;
17491 case PRAGMA_CILK_CLAUSE_LINEAR:
17492 clauses = c_parser_cilk_clause_linear (parser, clauses);
17493 break;
17494 case PRAGMA_CILK_CLAUSE_PRIVATE:
17495 /* Use the OpenMP counterpart. */
17496 clauses = c_parser_omp_clause_private (parser, clauses);
17497 break;
17498 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17499 /* Use the OpenMP counterpart. */
17500 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17501 break;
17502 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17503 /* Use the OpenMP counterpart. */
17504 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17505 break;
17506 case PRAGMA_CILK_CLAUSE_REDUCTION:
17507 /* Use the OpenMP counterpart. */
17508 clauses = c_parser_omp_clause_reduction (parser, clauses);
17509 break;
17510 default:
17511 c_parser_error (parser, "expected %<#pragma simd%> clause");
17512 goto saw_error;
17516 saw_error:
17517 c_parser_skip_to_pragma_eol (parser);
17518 return c_finish_omp_clauses (clauses, false, false, true);
17521 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17522 Here is the correct syntax of this pragma:
17523 #pragma cilk grainsize = <EXP>
17526 static void
17527 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
17529 extern tree convert_to_integer (tree, tree);
17531 /* consume the 'grainsize' keyword. */
17532 c_parser_consume_pragma (parser);
17534 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17536 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17537 if (g_expr.value == error_mark_node)
17539 c_parser_skip_to_pragma_eol (parser);
17540 return;
17542 tree grain = convert_to_integer (long_integer_type_node,
17543 c_fully_fold (g_expr.value, false,
17544 NULL));
17545 c_parser_skip_to_pragma_eol (parser);
17546 c_token *token = c_parser_peek_token (parser);
17547 if (token && token->type == CPP_KEYWORD
17548 && token->keyword == RID_CILK_FOR)
17550 if (grain == NULL_TREE || grain == error_mark_node)
17551 grain = integer_zero_node;
17552 c_parser_cilk_for (parser, grain, if_p);
17554 else
17555 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17556 "%<_Cilk_for%>");
17558 else
17559 c_parser_skip_to_pragma_eol (parser);
17562 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17564 static void
17565 c_parser_cilk_simd (c_parser *parser, bool *if_p)
17567 tree clauses = c_parser_cilk_all_clauses (parser);
17568 tree block = c_begin_compound_stmt (true);
17569 location_t loc = c_parser_peek_token (parser)->location;
17570 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
17571 block = c_end_compound_stmt (loc, block, true);
17572 add_stmt (block);
17575 /* Create an artificial decl with TYPE and emit initialization of it with
17576 INIT. */
17578 static tree
17579 c_get_temp_regvar (tree type, tree init)
17581 location_t loc = EXPR_LOCATION (init);
17582 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17583 DECL_ARTIFICIAL (decl) = 1;
17584 DECL_IGNORED_P (decl) = 1;
17585 pushdecl (decl);
17586 tree t = build2 (INIT_EXPR, type, decl, init);
17587 add_stmt (t);
17588 return decl;
17591 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17592 GRAIN is the grain value passed in through pragma or 0. */
17594 static void
17595 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
17597 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17598 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17599 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17600 clauses = c_finish_omp_clauses (clauses, false);
17602 tree block = c_begin_compound_stmt (true);
17603 tree sb = push_stmt_list ();
17604 location_t loc = c_parser_peek_token (parser)->location;
17605 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
17606 if_p);
17607 sb = pop_stmt_list (sb);
17609 if (omp_for)
17611 tree omp_par = make_node (OMP_PARALLEL);
17612 TREE_TYPE (omp_par) = void_type_node;
17613 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17614 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17615 TREE_SIDE_EFFECTS (bind) = 1;
17616 BIND_EXPR_BODY (bind) = sb;
17617 OMP_PARALLEL_BODY (omp_par) = bind;
17618 if (OMP_FOR_PRE_BODY (omp_for))
17620 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17621 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17623 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17624 tree decl = TREE_OPERAND (init, 0);
17625 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17626 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17627 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17628 if (TREE_CODE (t) != INTEGER_CST)
17630 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17631 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17632 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17633 OMP_CLAUSE_CHAIN (c) = clauses;
17634 clauses = c;
17636 if (TREE_CODE (incr) == MODIFY_EXPR)
17638 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17639 if (TREE_CODE (t) != INTEGER_CST)
17641 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17642 = c_get_temp_regvar (TREE_TYPE (t), t);
17643 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17644 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17645 OMP_CLAUSE_CHAIN (c) = clauses;
17646 clauses = c;
17649 t = TREE_OPERAND (init, 1);
17650 if (TREE_CODE (t) != INTEGER_CST)
17652 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17653 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17654 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17655 OMP_CLAUSE_CHAIN (c) = clauses;
17656 clauses = c;
17658 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17659 OMP_CLAUSE_DECL (c) = decl;
17660 OMP_CLAUSE_CHAIN (c) = clauses;
17661 clauses = c;
17662 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17663 OMP_CLAUSE_OPERAND (c, 0)
17664 = cilk_for_number_of_iterations (omp_for);
17665 OMP_CLAUSE_CHAIN (c) = clauses;
17666 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, true);
17667 add_stmt (omp_par);
17670 block = c_end_compound_stmt (loc, block, true);
17671 add_stmt (block);
17675 /* Parse a transaction attribute (GCC Extension).
17677 transaction-attribute:
17678 attributes
17679 [ [ any-word ] ]
17681 The transactional memory language description is written for C++,
17682 and uses the C++0x attribute syntax. For compatibility, allow the
17683 bracket style for transactions in C as well. */
17685 static tree
17686 c_parser_transaction_attributes (c_parser *parser)
17688 tree attr_name, attr = NULL;
17690 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17691 return c_parser_attributes (parser);
17693 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17694 return NULL_TREE;
17695 c_parser_consume_token (parser);
17696 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17697 goto error1;
17699 attr_name = c_parser_attribute_any_word (parser);
17700 if (attr_name)
17702 c_parser_consume_token (parser);
17703 attr = build_tree_list (attr_name, NULL_TREE);
17705 else
17706 c_parser_error (parser, "expected identifier");
17708 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17709 error1:
17710 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17711 return attr;
17714 /* Parse a __transaction_atomic or __transaction_relaxed statement
17715 (GCC Extension).
17717 transaction-statement:
17718 __transaction_atomic transaction-attribute[opt] compound-statement
17719 __transaction_relaxed compound-statement
17721 Note that the only valid attribute is: "outer".
17724 static tree
17725 c_parser_transaction (c_parser *parser, enum rid keyword)
17727 unsigned int old_in = parser->in_transaction;
17728 unsigned int this_in = 1, new_in;
17729 location_t loc = c_parser_peek_token (parser)->location;
17730 tree stmt, attrs;
17732 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17733 || keyword == RID_TRANSACTION_RELAXED)
17734 && c_parser_next_token_is_keyword (parser, keyword));
17735 c_parser_consume_token (parser);
17737 if (keyword == RID_TRANSACTION_RELAXED)
17738 this_in |= TM_STMT_ATTR_RELAXED;
17739 else
17741 attrs = c_parser_transaction_attributes (parser);
17742 if (attrs)
17743 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
17746 /* Keep track if we're in the lexical scope of an outer transaction. */
17747 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
17749 parser->in_transaction = new_in;
17750 stmt = c_parser_compound_statement (parser);
17751 parser->in_transaction = old_in;
17753 if (flag_tm)
17754 stmt = c_finish_transaction (loc, stmt, this_in);
17755 else
17756 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17757 "%<__transaction_atomic%> without transactional memory support enabled"
17758 : "%<__transaction_relaxed %> "
17759 "without transactional memory support enabled"));
17761 return stmt;
17764 /* Parse a __transaction_atomic or __transaction_relaxed expression
17765 (GCC Extension).
17767 transaction-expression:
17768 __transaction_atomic ( expression )
17769 __transaction_relaxed ( expression )
17772 static struct c_expr
17773 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
17775 struct c_expr ret;
17776 unsigned int old_in = parser->in_transaction;
17777 unsigned int this_in = 1;
17778 location_t loc = c_parser_peek_token (parser)->location;
17779 tree attrs;
17781 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17782 || keyword == RID_TRANSACTION_RELAXED)
17783 && c_parser_next_token_is_keyword (parser, keyword));
17784 c_parser_consume_token (parser);
17786 if (keyword == RID_TRANSACTION_RELAXED)
17787 this_in |= TM_STMT_ATTR_RELAXED;
17788 else
17790 attrs = c_parser_transaction_attributes (parser);
17791 if (attrs)
17792 this_in |= parse_tm_stmt_attr (attrs, 0);
17795 parser->in_transaction = this_in;
17796 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17798 tree expr = c_parser_expression (parser).value;
17799 ret.original_type = TREE_TYPE (expr);
17800 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
17801 if (this_in & TM_STMT_ATTR_RELAXED)
17802 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
17803 SET_EXPR_LOCATION (ret.value, loc);
17804 ret.original_code = TRANSACTION_EXPR;
17805 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17807 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
17808 goto error;
17811 else
17813 error:
17814 ret.value = error_mark_node;
17815 ret.original_code = ERROR_MARK;
17816 ret.original_type = NULL;
17818 parser->in_transaction = old_in;
17820 if (!flag_tm)
17821 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17822 "%<__transaction_atomic%> without transactional memory support enabled"
17823 : "%<__transaction_relaxed %> "
17824 "without transactional memory support enabled"));
17826 set_c_expr_source_range (&ret, loc, loc);
17828 return ret;
17831 /* Parse a __transaction_cancel statement (GCC Extension).
17833 transaction-cancel-statement:
17834 __transaction_cancel transaction-attribute[opt] ;
17836 Note that the only valid attribute is "outer".
17839 static tree
17840 c_parser_transaction_cancel (c_parser *parser)
17842 location_t loc = c_parser_peek_token (parser)->location;
17843 tree attrs;
17844 bool is_outer = false;
17846 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
17847 c_parser_consume_token (parser);
17849 attrs = c_parser_transaction_attributes (parser);
17850 if (attrs)
17851 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
17853 if (!flag_tm)
17855 error_at (loc, "%<__transaction_cancel%> without "
17856 "transactional memory support enabled");
17857 goto ret_error;
17859 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
17861 error_at (loc, "%<__transaction_cancel%> within a "
17862 "%<__transaction_relaxed%>");
17863 goto ret_error;
17865 else if (is_outer)
17867 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
17868 && !is_tm_may_cancel_outer (current_function_decl))
17870 error_at (loc, "outer %<__transaction_cancel%> not "
17871 "within outer %<__transaction_atomic%>");
17872 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
17873 goto ret_error;
17876 else if (parser->in_transaction == 0)
17878 error_at (loc, "%<__transaction_cancel%> not within "
17879 "%<__transaction_atomic%>");
17880 goto ret_error;
17883 return add_stmt (build_tm_abort_call (loc, is_outer));
17885 ret_error:
17886 return build1 (NOP_EXPR, void_type_node, error_mark_node);
17889 /* Parse a single source file. */
17891 void
17892 c_parse_file (void)
17894 /* Use local storage to begin. If the first token is a pragma, parse it.
17895 If it is #pragma GCC pch_preprocess, then this will load a PCH file
17896 which will cause garbage collection. */
17897 c_parser tparser;
17899 memset (&tparser, 0, sizeof tparser);
17900 tparser.tokens = &tparser.tokens_buf[0];
17901 the_parser = &tparser;
17903 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
17904 c_parser_pragma_pch_preprocess (&tparser);
17906 the_parser = ggc_alloc<c_parser> ();
17907 *the_parser = tparser;
17908 if (tparser.tokens == &tparser.tokens_buf[0])
17909 the_parser->tokens = &the_parser->tokens_buf[0];
17911 /* Initialize EH, if we've been told to do so. */
17912 if (flag_exceptions)
17913 using_eh_for_cleanups ();
17915 c_parser_translation_unit (the_parser);
17916 the_parser = NULL;
17919 /* This function parses Cilk Plus array notation. The starting index is
17920 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
17921 return value of this function is a tree_node called VALUE_TREE of type
17922 ARRAY_NOTATION_REF. */
17924 static tree
17925 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
17926 tree array_value)
17928 c_token *token = NULL;
17929 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
17930 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
17931 tree array_type_domain = NULL_TREE;
17933 if (array_value == error_mark_node || initial_index == error_mark_node)
17935 /* No need to continue. If either of these 2 were true, then an error
17936 must be emitted already. Thus, no need to emit them twice. */
17937 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17938 return error_mark_node;
17941 array_type = TREE_TYPE (array_value);
17942 gcc_assert (array_type);
17943 if (TREE_CODE (array_type) != ARRAY_TYPE
17944 && TREE_CODE (array_type) != POINTER_TYPE)
17946 error_at (loc, "base of array section must be pointer or array type");
17947 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17948 return error_mark_node;
17950 type = TREE_TYPE (array_type);
17951 token = c_parser_peek_token (parser);
17953 if (token->type == CPP_EOF)
17955 c_parser_error (parser, "expected %<:%> or numeral");
17956 return value_tree;
17958 else if (token->type == CPP_COLON)
17960 if (!initial_index)
17962 /* If we are here, then we have a case like this A[:]. */
17963 c_parser_consume_token (parser);
17964 if (TREE_CODE (array_type) == POINTER_TYPE)
17966 error_at (loc, "start-index and length fields necessary for "
17967 "using array notations in pointers");
17968 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17969 return error_mark_node;
17971 if (TREE_CODE (array_type) == FUNCTION_TYPE)
17973 error_at (loc, "array notations cannot be used with function "
17974 "type");
17975 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17976 return error_mark_node;
17978 array_type_domain = TYPE_DOMAIN (array_type);
17980 if (!array_type_domain)
17982 error_at (loc, "start-index and length fields necessary for "
17983 "using array notations in dimensionless arrays");
17984 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17985 return error_mark_node;
17988 start_index = TYPE_MINVAL (array_type_domain);
17989 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
17990 start_index);
17991 if (!TYPE_MAXVAL (array_type_domain)
17992 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
17994 error_at (loc, "start-index and length fields necessary for "
17995 "using array notations in variable-length arrays");
17996 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17997 return error_mark_node;
17999 end_index = TYPE_MAXVAL (array_type_domain);
18000 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18001 end_index, integer_one_node);
18002 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18003 stride = build_int_cst (integer_type_node, 1);
18004 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18006 else if (initial_index != error_mark_node)
18008 /* If we are here, then there should be 2 possibilities:
18009 1. Array [EXPR : EXPR]
18010 2. Array [EXPR : EXPR : EXPR]
18012 start_index = initial_index;
18014 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18016 error_at (loc, "array notations cannot be used with function "
18017 "type");
18018 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18019 return error_mark_node;
18021 c_parser_consume_token (parser); /* consume the ':' */
18022 struct c_expr ce = c_parser_expression (parser);
18023 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18024 end_index = ce.value;
18025 if (!end_index || end_index == error_mark_node)
18027 c_parser_skip_to_end_of_block_or_statement (parser);
18028 return error_mark_node;
18030 if (c_parser_peek_token (parser)->type == CPP_COLON)
18032 c_parser_consume_token (parser);
18033 ce = c_parser_expression (parser);
18034 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18035 stride = ce.value;
18036 if (!stride || stride == error_mark_node)
18038 c_parser_skip_to_end_of_block_or_statement (parser);
18039 return error_mark_node;
18043 else
18044 c_parser_error (parser, "expected array notation expression");
18046 else
18047 c_parser_error (parser, "expected array notation expression");
18049 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18051 value_tree = build_array_notation_ref (loc, array_value, start_index,
18052 end_index, stride, type);
18053 if (value_tree != error_mark_node)
18054 SET_EXPR_LOCATION (value_tree, loc);
18055 return value_tree;
18058 #include "gt-c-c-parser.h"