S/390: Testsuite: Add asm scan patterns for -m31.
[official-gcc.git] / gcc / c / c-parser.c
blob0259f66aeb4d2b319bbe8e7c3496cefc37aef693
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2015 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 void
63 set_c_expr_source_range (c_expr *expr,
64 location_t start, location_t finish)
66 expr->src_range.m_start = start;
67 expr->src_range.m_finish = finish;
68 if (expr->value)
69 set_source_range (expr->value, start, finish);
72 void
73 set_c_expr_source_range (c_expr *expr,
74 source_range src_range)
76 expr->src_range = src_range;
77 if (expr->value)
78 set_source_range (expr->value, src_range);
82 /* Initialization routine for this file. */
84 void
85 c_parse_init (void)
87 /* The only initialization required is of the reserved word
88 identifiers. */
89 unsigned int i;
90 tree id;
91 int mask = 0;
93 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
94 the c_token structure. */
95 gcc_assert (RID_MAX <= 255);
97 mask |= D_CXXONLY;
98 if (!flag_isoc99)
99 mask |= D_C99;
100 if (flag_no_asm)
102 mask |= D_ASM | D_EXT;
103 if (!flag_isoc99)
104 mask |= D_EXT89;
106 if (!c_dialect_objc ())
107 mask |= D_OBJC | D_CXX_OBJC;
109 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
110 for (i = 0; i < num_c_common_reswords; i++)
112 /* If a keyword is disabled, do not enter it into the table
113 and so create a canonical spelling that isn't a keyword. */
114 if (c_common_reswords[i].disable & mask)
116 if (warn_cxx_compat
117 && (c_common_reswords[i].disable & D_CXXWARN))
119 id = get_identifier (c_common_reswords[i].word);
120 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
121 C_IS_RESERVED_WORD (id) = 1;
123 continue;
126 id = get_identifier (c_common_reswords[i].word);
127 C_SET_RID_CODE (id, c_common_reswords[i].rid);
128 C_IS_RESERVED_WORD (id) = 1;
129 ridpointers [(int) c_common_reswords[i].rid] = id;
132 for (i = 0; i < NUM_INT_N_ENTS; i++)
134 /* We always create the symbols but they aren't always supported. */
135 char name[50];
136 sprintf (name, "__int%d", int_n_data[i].bitsize);
137 id = get_identifier (name);
138 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
139 C_IS_RESERVED_WORD (id) = 1;
143 /* The C lexer intermediates between the lexer in cpplib and c-lex.c
144 and the C parser. Unlike the C++ lexer, the parser structure
145 stores the lexer information instead of using a separate structure.
146 Identifiers are separated into ordinary identifiers, type names,
147 keywords and some other Objective-C types of identifiers, and some
148 look-ahead is maintained.
150 ??? It might be a good idea to lex the whole file up front (as for
151 C++). It would then be possible to share more of the C and C++
152 lexer code, if desired. */
154 /* More information about the type of a CPP_NAME token. */
155 enum c_id_kind {
156 /* An ordinary identifier. */
157 C_ID_ID,
158 /* An identifier declared as a typedef name. */
159 C_ID_TYPENAME,
160 /* An identifier declared as an Objective-C class name. */
161 C_ID_CLASSNAME,
162 /* An address space identifier. */
163 C_ID_ADDRSPACE,
164 /* Not an identifier. */
165 C_ID_NONE
168 /* A single C token after string literal concatenation and conversion
169 of preprocessing tokens to tokens. */
170 struct GTY (()) c_token {
171 /* The kind of token. */
172 ENUM_BITFIELD (cpp_ttype) type : 8;
173 /* If this token is a CPP_NAME, this value indicates whether also
174 declared as some kind of type. Otherwise, it is C_ID_NONE. */
175 ENUM_BITFIELD (c_id_kind) id_kind : 8;
176 /* If this token is a keyword, this value indicates which keyword.
177 Otherwise, this value is RID_MAX. */
178 ENUM_BITFIELD (rid) keyword : 8;
179 /* If this token is a CPP_PRAGMA, this indicates the pragma that
180 was seen. Otherwise it is PRAGMA_NONE. */
181 ENUM_BITFIELD (pragma_kind) pragma_kind : 8;
182 /* The location at which this token was found. */
183 location_t location;
184 /* The value associated with this token, if any. */
185 tree value;
187 source_range get_range () const
189 return get_range_from_loc (line_table, location);
192 location_t get_finish () const
194 return get_range ().m_finish;
198 /* A parser structure recording information about the state and
199 context of parsing. Includes lexer information with up to two
200 tokens of look-ahead; more are not needed for C. */
201 struct GTY(()) c_parser {
202 /* The look-ahead tokens. */
203 c_token * GTY((skip)) tokens;
204 /* Buffer for look-ahead tokens. */
205 c_token tokens_buf[2];
206 /* How many look-ahead tokens are available (0, 1 or 2, or
207 more if parsing from pre-lexed tokens). */
208 unsigned int tokens_avail;
209 /* True if a syntax error is being recovered from; false otherwise.
210 c_parser_error sets this flag. It should clear this flag when
211 enough tokens have been consumed to recover from the error. */
212 BOOL_BITFIELD error : 1;
213 /* True if we're processing a pragma, and shouldn't automatically
214 consume CPP_PRAGMA_EOL. */
215 BOOL_BITFIELD in_pragma : 1;
216 /* True if we're parsing the outermost block of an if statement. */
217 BOOL_BITFIELD in_if_block : 1;
218 /* True if we want to lex an untranslated string. */
219 BOOL_BITFIELD lex_untranslated_string : 1;
221 /* Objective-C specific parser/lexer information. */
223 /* True if we are in a context where the Objective-C "PQ" keywords
224 are considered keywords. */
225 BOOL_BITFIELD objc_pq_context : 1;
226 /* True if we are parsing a (potential) Objective-C foreach
227 statement. This is set to true after we parsed 'for (' and while
228 we wait for 'in' or ';' to decide if it's a standard C for loop or an
229 Objective-C foreach loop. */
230 BOOL_BITFIELD objc_could_be_foreach_context : 1;
231 /* The following flag is needed to contextualize Objective-C lexical
232 analysis. In some cases (e.g., 'int NSObject;'), it is
233 undesirable to bind an identifier to an Objective-C class, even
234 if a class with that name exists. */
235 BOOL_BITFIELD objc_need_raw_identifier : 1;
236 /* Nonzero if we're processing a __transaction statement. The value
237 is 1 | TM_STMT_ATTR_*. */
238 unsigned int in_transaction : 4;
239 /* True if we are in a context where the Objective-C "Property attribute"
240 keywords are valid. */
241 BOOL_BITFIELD objc_property_attr_context : 1;
243 /* Cilk Plus specific parser/lexer information. */
245 /* Buffer to hold all the tokens from parsing the vector attribute for the
246 SIMD-enabled functions (formerly known as elemental functions). */
247 vec <c_token, va_gc> *cilk_simd_fn_tokens;
251 /* The actual parser and external interface. ??? Does this need to be
252 garbage-collected? */
254 static GTY (()) c_parser *the_parser;
256 /* Read in and lex a single token, storing it in *TOKEN. */
258 static void
259 c_lex_one_token (c_parser *parser, c_token *token)
261 timevar_push (TV_LEX);
263 token->type = c_lex_with_flags (&token->value, &token->location, NULL,
264 (parser->lex_untranslated_string
265 ? C_LEX_STRING_NO_TRANSLATE : 0));
266 token->id_kind = C_ID_NONE;
267 token->keyword = RID_MAX;
268 token->pragma_kind = PRAGMA_NONE;
270 switch (token->type)
272 case CPP_NAME:
274 tree decl;
276 bool objc_force_identifier = parser->objc_need_raw_identifier;
277 if (c_dialect_objc ())
278 parser->objc_need_raw_identifier = false;
280 if (C_IS_RESERVED_WORD (token->value))
282 enum rid rid_code = C_RID_CODE (token->value);
284 if (rid_code == RID_CXX_COMPAT_WARN)
286 warning_at (token->location,
287 OPT_Wc___compat,
288 "identifier %qE conflicts with C++ keyword",
289 token->value);
291 else if (rid_code >= RID_FIRST_ADDR_SPACE
292 && rid_code <= RID_LAST_ADDR_SPACE)
294 token->id_kind = C_ID_ADDRSPACE;
295 token->keyword = rid_code;
296 break;
298 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
300 /* We found an Objective-C "pq" keyword (in, out,
301 inout, bycopy, byref, oneway). They need special
302 care because the interpretation depends on the
303 context. */
304 if (parser->objc_pq_context)
306 token->type = CPP_KEYWORD;
307 token->keyword = rid_code;
308 break;
310 else if (parser->objc_could_be_foreach_context
311 && rid_code == RID_IN)
313 /* We are in Objective-C, inside a (potential)
314 foreach context (which means after having
315 parsed 'for (', but before having parsed ';'),
316 and we found 'in'. We consider it the keyword
317 which terminates the declaration at the
318 beginning of a foreach-statement. Note that
319 this means you can't use 'in' for anything else
320 in that context; in particular, in Objective-C
321 you can't use 'in' as the name of the running
322 variable in a C for loop. We could potentially
323 try to add code here to disambiguate, but it
324 seems a reasonable limitation. */
325 token->type = CPP_KEYWORD;
326 token->keyword = rid_code;
327 break;
329 /* Else, "pq" keywords outside of the "pq" context are
330 not keywords, and we fall through to the code for
331 normal tokens. */
333 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
335 /* We found an Objective-C "property attribute"
336 keyword (getter, setter, readonly, etc). These are
337 only valid in the property context. */
338 if (parser->objc_property_attr_context)
340 token->type = CPP_KEYWORD;
341 token->keyword = rid_code;
342 break;
344 /* Else they are not special keywords.
347 else if (c_dialect_objc ()
348 && (OBJC_IS_AT_KEYWORD (rid_code)
349 || OBJC_IS_CXX_KEYWORD (rid_code)))
351 /* We found one of the Objective-C "@" keywords (defs,
352 selector, synchronized, etc) or one of the
353 Objective-C "cxx" keywords (class, private,
354 protected, public, try, catch, throw) without a
355 preceding '@' sign. Do nothing and fall through to
356 the code for normal tokens (in C++ we would still
357 consider the CXX ones keywords, but not in C). */
360 else
362 token->type = CPP_KEYWORD;
363 token->keyword = rid_code;
364 break;
368 decl = lookup_name (token->value);
369 if (decl)
371 if (TREE_CODE (decl) == TYPE_DECL)
373 token->id_kind = C_ID_TYPENAME;
374 break;
377 else if (c_dialect_objc ())
379 tree objc_interface_decl = objc_is_class_name (token->value);
380 /* Objective-C class names are in the same namespace as
381 variables and typedefs, and hence are shadowed by local
382 declarations. */
383 if (objc_interface_decl
384 && (!objc_force_identifier || global_bindings_p ()))
386 token->value = objc_interface_decl;
387 token->id_kind = C_ID_CLASSNAME;
388 break;
391 token->id_kind = C_ID_ID;
393 break;
394 case CPP_AT_NAME:
395 /* This only happens in Objective-C; it must be a keyword. */
396 token->type = CPP_KEYWORD;
397 switch (C_RID_CODE (token->value))
399 /* Replace 'class' with '@class', 'private' with '@private',
400 etc. This prevents confusion with the C++ keyword
401 'class', and makes the tokens consistent with other
402 Objective-C 'AT' keywords. For example '@class' is
403 reported as RID_AT_CLASS which is consistent with
404 '@synchronized', which is reported as
405 RID_AT_SYNCHRONIZED.
407 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
408 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
409 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
410 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
411 case RID_THROW: token->keyword = RID_AT_THROW; break;
412 case RID_TRY: token->keyword = RID_AT_TRY; break;
413 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
414 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
415 default: token->keyword = C_RID_CODE (token->value);
417 break;
418 case CPP_COLON:
419 case CPP_COMMA:
420 case CPP_CLOSE_PAREN:
421 case CPP_SEMICOLON:
422 /* These tokens may affect the interpretation of any identifiers
423 following, if doing Objective-C. */
424 if (c_dialect_objc ())
425 parser->objc_need_raw_identifier = false;
426 break;
427 case CPP_PRAGMA:
428 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
429 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
430 token->value = NULL;
431 break;
432 default:
433 break;
435 timevar_pop (TV_LEX);
438 /* Return a pointer to the next token from PARSER, reading it in if
439 necessary. */
441 static inline c_token *
442 c_parser_peek_token (c_parser *parser)
444 if (parser->tokens_avail == 0)
446 c_lex_one_token (parser, &parser->tokens[0]);
447 parser->tokens_avail = 1;
449 return &parser->tokens[0];
452 /* Return true if the next token from PARSER has the indicated
453 TYPE. */
455 static inline bool
456 c_parser_next_token_is (c_parser *parser, enum cpp_ttype type)
458 return c_parser_peek_token (parser)->type == type;
461 /* Return true if the next token from PARSER does not have the
462 indicated TYPE. */
464 static inline bool
465 c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type)
467 return !c_parser_next_token_is (parser, type);
470 /* Return true if the next token from PARSER is the indicated
471 KEYWORD. */
473 static inline bool
474 c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword)
476 return c_parser_peek_token (parser)->keyword == keyword;
479 /* Return a pointer to the next-but-one token from PARSER, reading it
480 in if necessary. The next token is already read in. */
482 static c_token *
483 c_parser_peek_2nd_token (c_parser *parser)
485 if (parser->tokens_avail >= 2)
486 return &parser->tokens[1];
487 gcc_assert (parser->tokens_avail == 1);
488 gcc_assert (parser->tokens[0].type != CPP_EOF);
489 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
490 c_lex_one_token (parser, &parser->tokens[1]);
491 parser->tokens_avail = 2;
492 return &parser->tokens[1];
495 /* Return true if TOKEN can start a type name,
496 false otherwise. */
497 static bool
498 c_token_starts_typename (c_token *token)
500 switch (token->type)
502 case CPP_NAME:
503 switch (token->id_kind)
505 case C_ID_ID:
506 return false;
507 case C_ID_ADDRSPACE:
508 return true;
509 case C_ID_TYPENAME:
510 return true;
511 case C_ID_CLASSNAME:
512 gcc_assert (c_dialect_objc ());
513 return true;
514 default:
515 gcc_unreachable ();
517 case CPP_KEYWORD:
518 switch (token->keyword)
520 case RID_UNSIGNED:
521 case RID_LONG:
522 case RID_SHORT:
523 case RID_SIGNED:
524 case RID_COMPLEX:
525 case RID_INT:
526 case RID_CHAR:
527 case RID_FLOAT:
528 case RID_DOUBLE:
529 case RID_VOID:
530 case RID_DFLOAT32:
531 case RID_DFLOAT64:
532 case RID_DFLOAT128:
533 case RID_BOOL:
534 case RID_ENUM:
535 case RID_STRUCT:
536 case RID_UNION:
537 case RID_TYPEOF:
538 case RID_CONST:
539 case RID_ATOMIC:
540 case RID_VOLATILE:
541 case RID_RESTRICT:
542 case RID_ATTRIBUTE:
543 case RID_FRACT:
544 case RID_ACCUM:
545 case RID_SAT:
546 case RID_AUTO_TYPE:
547 return true;
548 default:
549 if (token->keyword >= RID_FIRST_INT_N
550 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
551 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
552 return true;
553 return false;
555 case CPP_LESS:
556 if (c_dialect_objc ())
557 return true;
558 return false;
559 default:
560 return false;
564 enum c_lookahead_kind {
565 /* Always treat unknown identifiers as typenames. */
566 cla_prefer_type,
568 /* Could be parsing a nonabstract declarator. Only treat an identifier
569 as a typename if followed by another identifier or a star. */
570 cla_nonabstract_decl,
572 /* Never treat identifiers as typenames. */
573 cla_prefer_id
576 /* Return true if the next token from PARSER can start a type name,
577 false otherwise. LA specifies how to do lookahead in order to
578 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
580 static inline bool
581 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
583 c_token *token = c_parser_peek_token (parser);
584 if (c_token_starts_typename (token))
585 return true;
587 /* Try a bit harder to detect an unknown typename. */
588 if (la != cla_prefer_id
589 && token->type == CPP_NAME
590 && token->id_kind == C_ID_ID
592 /* Do not try too hard when we could have "object in array". */
593 && !parser->objc_could_be_foreach_context
595 && (la == cla_prefer_type
596 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
597 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
599 /* Only unknown identifiers. */
600 && !lookup_name (token->value))
601 return true;
603 return false;
606 /* Return true if TOKEN is a type qualifier, false otherwise. */
607 static bool
608 c_token_is_qualifier (c_token *token)
610 switch (token->type)
612 case CPP_NAME:
613 switch (token->id_kind)
615 case C_ID_ADDRSPACE:
616 return true;
617 default:
618 return false;
620 case CPP_KEYWORD:
621 switch (token->keyword)
623 case RID_CONST:
624 case RID_VOLATILE:
625 case RID_RESTRICT:
626 case RID_ATTRIBUTE:
627 case RID_ATOMIC:
628 return true;
629 default:
630 return false;
632 case CPP_LESS:
633 return false;
634 default:
635 gcc_unreachable ();
639 /* Return true if the next token from PARSER is a type qualifier,
640 false otherwise. */
641 static inline bool
642 c_parser_next_token_is_qualifier (c_parser *parser)
644 c_token *token = c_parser_peek_token (parser);
645 return c_token_is_qualifier (token);
648 /* Return true if TOKEN can start declaration specifiers, false
649 otherwise. */
650 static bool
651 c_token_starts_declspecs (c_token *token)
653 switch (token->type)
655 case CPP_NAME:
656 switch (token->id_kind)
658 case C_ID_ID:
659 return false;
660 case C_ID_ADDRSPACE:
661 return true;
662 case C_ID_TYPENAME:
663 return true;
664 case C_ID_CLASSNAME:
665 gcc_assert (c_dialect_objc ());
666 return true;
667 default:
668 gcc_unreachable ();
670 case CPP_KEYWORD:
671 switch (token->keyword)
673 case RID_STATIC:
674 case RID_EXTERN:
675 case RID_REGISTER:
676 case RID_TYPEDEF:
677 case RID_INLINE:
678 case RID_NORETURN:
679 case RID_AUTO:
680 case RID_THREAD:
681 case RID_UNSIGNED:
682 case RID_LONG:
683 case RID_SHORT:
684 case RID_SIGNED:
685 case RID_COMPLEX:
686 case RID_INT:
687 case RID_CHAR:
688 case RID_FLOAT:
689 case RID_DOUBLE:
690 case RID_VOID:
691 case RID_DFLOAT32:
692 case RID_DFLOAT64:
693 case RID_DFLOAT128:
694 case RID_BOOL:
695 case RID_ENUM:
696 case RID_STRUCT:
697 case RID_UNION:
698 case RID_TYPEOF:
699 case RID_CONST:
700 case RID_VOLATILE:
701 case RID_RESTRICT:
702 case RID_ATTRIBUTE:
703 case RID_FRACT:
704 case RID_ACCUM:
705 case RID_SAT:
706 case RID_ALIGNAS:
707 case RID_ATOMIC:
708 case RID_AUTO_TYPE:
709 return true;
710 default:
711 if (token->keyword >= RID_FIRST_INT_N
712 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
713 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
714 return true;
715 return false;
717 case CPP_LESS:
718 if (c_dialect_objc ())
719 return true;
720 return false;
721 default:
722 return false;
727 /* Return true if TOKEN can start declaration specifiers or a static
728 assertion, false otherwise. */
729 static bool
730 c_token_starts_declaration (c_token *token)
732 if (c_token_starts_declspecs (token)
733 || token->keyword == RID_STATIC_ASSERT)
734 return true;
735 else
736 return false;
739 /* Return true if the next token from PARSER can start declaration
740 specifiers, false otherwise. */
741 static inline bool
742 c_parser_next_token_starts_declspecs (c_parser *parser)
744 c_token *token = c_parser_peek_token (parser);
746 /* In Objective-C, a classname normally starts a declspecs unless it
747 is immediately followed by a dot. In that case, it is the
748 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
749 setter/getter on the class. c_token_starts_declspecs() can't
750 differentiate between the two cases because it only checks the
751 current token, so we have a special check here. */
752 if (c_dialect_objc ()
753 && token->type == CPP_NAME
754 && token->id_kind == C_ID_CLASSNAME
755 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
756 return false;
758 return c_token_starts_declspecs (token);
761 /* Return true if the next tokens from PARSER can start declaration
762 specifiers or a static assertion, false otherwise. */
763 static inline bool
764 c_parser_next_tokens_start_declaration (c_parser *parser)
766 c_token *token = c_parser_peek_token (parser);
768 /* Same as above. */
769 if (c_dialect_objc ()
770 && token->type == CPP_NAME
771 && token->id_kind == C_ID_CLASSNAME
772 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
773 return false;
775 /* Labels do not start declarations. */
776 if (token->type == CPP_NAME
777 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
778 return false;
780 if (c_token_starts_declaration (token))
781 return true;
783 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
784 return true;
786 return false;
789 /* Consume the next token from PARSER. */
791 static void
792 c_parser_consume_token (c_parser *parser)
794 gcc_assert (parser->tokens_avail >= 1);
795 gcc_assert (parser->tokens[0].type != CPP_EOF);
796 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
797 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
798 if (parser->tokens != &parser->tokens_buf[0])
799 parser->tokens++;
800 else if (parser->tokens_avail == 2)
801 parser->tokens[0] = parser->tokens[1];
802 parser->tokens_avail--;
805 /* Expect the current token to be a #pragma. Consume it and remember
806 that we've begun parsing a pragma. */
808 static void
809 c_parser_consume_pragma (c_parser *parser)
811 gcc_assert (!parser->in_pragma);
812 gcc_assert (parser->tokens_avail >= 1);
813 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
814 if (parser->tokens != &parser->tokens_buf[0])
815 parser->tokens++;
816 else if (parser->tokens_avail == 2)
817 parser->tokens[0] = parser->tokens[1];
818 parser->tokens_avail--;
819 parser->in_pragma = true;
822 /* Update the global input_location from TOKEN. */
823 static inline void
824 c_parser_set_source_position_from_token (c_token *token)
826 if (token->type != CPP_EOF)
828 input_location = token->location;
832 /* Issue a diagnostic of the form
833 FILE:LINE: MESSAGE before TOKEN
834 where TOKEN is the next token in the input stream of PARSER.
835 MESSAGE (specified by the caller) is usually of the form "expected
836 OTHER-TOKEN".
838 Do not issue a diagnostic if still recovering from an error.
840 ??? This is taken from the C++ parser, but building up messages in
841 this way is not i18n-friendly and some other approach should be
842 used. */
844 static void
845 c_parser_error (c_parser *parser, const char *gmsgid)
847 c_token *token = c_parser_peek_token (parser);
848 if (parser->error)
849 return;
850 parser->error = true;
851 if (!gmsgid)
852 return;
853 /* This diagnostic makes more sense if it is tagged to the line of
854 the token we just peeked at. */
855 c_parser_set_source_position_from_token (token);
856 c_parse_error (gmsgid,
857 /* Because c_parse_error does not understand
858 CPP_KEYWORD, keywords are treated like
859 identifiers. */
860 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
861 /* ??? The C parser does not save the cpp flags of a
862 token, we need to pass 0 here and we will not get
863 the source spelling of some tokens but rather the
864 canonical spelling. */
865 token->value, /*flags=*/0);
868 /* If the next token is of the indicated TYPE, consume it. Otherwise,
869 issue the error MSGID. If MSGID is NULL then a message has already
870 been produced and no message will be produced this time. Returns
871 true if found, false otherwise. */
873 static bool
874 c_parser_require (c_parser *parser,
875 enum cpp_ttype type,
876 const char *msgid)
878 if (c_parser_next_token_is (parser, type))
880 c_parser_consume_token (parser);
881 return true;
883 else
885 c_parser_error (parser, msgid);
886 return false;
890 /* If the next token is the indicated keyword, consume it. Otherwise,
891 issue the error MSGID. Returns true if found, false otherwise. */
893 static bool
894 c_parser_require_keyword (c_parser *parser,
895 enum rid keyword,
896 const char *msgid)
898 if (c_parser_next_token_is_keyword (parser, keyword))
900 c_parser_consume_token (parser);
901 return true;
903 else
905 c_parser_error (parser, msgid);
906 return false;
910 /* Like c_parser_require, except that tokens will be skipped until the
911 desired token is found. An error message is still produced if the
912 next token is not as expected. If MSGID is NULL then a message has
913 already been produced and no message will be produced this
914 time. */
916 static void
917 c_parser_skip_until_found (c_parser *parser,
918 enum cpp_ttype type,
919 const char *msgid)
921 unsigned nesting_depth = 0;
923 if (c_parser_require (parser, type, msgid))
924 return;
926 /* Skip tokens until the desired token is found. */
927 while (true)
929 /* Peek at the next token. */
930 c_token *token = c_parser_peek_token (parser);
931 /* If we've reached the token we want, consume it and stop. */
932 if (token->type == type && !nesting_depth)
934 c_parser_consume_token (parser);
935 break;
938 /* If we've run out of tokens, stop. */
939 if (token->type == CPP_EOF)
940 return;
941 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
942 return;
943 if (token->type == CPP_OPEN_BRACE
944 || token->type == CPP_OPEN_PAREN
945 || token->type == CPP_OPEN_SQUARE)
946 ++nesting_depth;
947 else if (token->type == CPP_CLOSE_BRACE
948 || token->type == CPP_CLOSE_PAREN
949 || token->type == CPP_CLOSE_SQUARE)
951 if (nesting_depth-- == 0)
952 break;
954 /* Consume this token. */
955 c_parser_consume_token (parser);
957 parser->error = false;
960 /* Skip tokens until the end of a parameter is found, but do not
961 consume the comma, semicolon or closing delimiter. */
963 static void
964 c_parser_skip_to_end_of_parameter (c_parser *parser)
966 unsigned nesting_depth = 0;
968 while (true)
970 c_token *token = c_parser_peek_token (parser);
971 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
972 && !nesting_depth)
973 break;
974 /* If we've run out of tokens, stop. */
975 if (token->type == CPP_EOF)
976 return;
977 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
978 return;
979 if (token->type == CPP_OPEN_BRACE
980 || token->type == CPP_OPEN_PAREN
981 || token->type == CPP_OPEN_SQUARE)
982 ++nesting_depth;
983 else if (token->type == CPP_CLOSE_BRACE
984 || token->type == CPP_CLOSE_PAREN
985 || token->type == CPP_CLOSE_SQUARE)
987 if (nesting_depth-- == 0)
988 break;
990 /* Consume this token. */
991 c_parser_consume_token (parser);
993 parser->error = false;
996 /* Expect to be at the end of the pragma directive and consume an
997 end of line marker. */
999 static void
1000 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1002 gcc_assert (parser->in_pragma);
1003 parser->in_pragma = false;
1005 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1006 c_parser_error (parser, "expected end of line");
1008 cpp_ttype token_type;
1011 c_token *token = c_parser_peek_token (parser);
1012 token_type = token->type;
1013 if (token_type == CPP_EOF)
1014 break;
1015 c_parser_consume_token (parser);
1017 while (token_type != CPP_PRAGMA_EOL);
1019 parser->error = false;
1022 /* Skip tokens until we have consumed an entire block, or until we
1023 have consumed a non-nested ';'. */
1025 static void
1026 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1028 unsigned nesting_depth = 0;
1029 bool save_error = parser->error;
1031 while (true)
1033 c_token *token;
1035 /* Peek at the next token. */
1036 token = c_parser_peek_token (parser);
1038 switch (token->type)
1040 case CPP_EOF:
1041 return;
1043 case CPP_PRAGMA_EOL:
1044 if (parser->in_pragma)
1045 return;
1046 break;
1048 case CPP_SEMICOLON:
1049 /* If the next token is a ';', we have reached the
1050 end of the statement. */
1051 if (!nesting_depth)
1053 /* Consume the ';'. */
1054 c_parser_consume_token (parser);
1055 goto finished;
1057 break;
1059 case CPP_CLOSE_BRACE:
1060 /* If the next token is a non-nested '}', then we have
1061 reached the end of the current block. */
1062 if (nesting_depth == 0 || --nesting_depth == 0)
1064 c_parser_consume_token (parser);
1065 goto finished;
1067 break;
1069 case CPP_OPEN_BRACE:
1070 /* If it the next token is a '{', then we are entering a new
1071 block. Consume the entire block. */
1072 ++nesting_depth;
1073 break;
1075 case CPP_PRAGMA:
1076 /* If we see a pragma, consume the whole thing at once. We
1077 have some safeguards against consuming pragmas willy-nilly.
1078 Normally, we'd expect to be here with parser->error set,
1079 which disables these safeguards. But it's possible to get
1080 here for secondary error recovery, after parser->error has
1081 been cleared. */
1082 c_parser_consume_pragma (parser);
1083 c_parser_skip_to_pragma_eol (parser);
1084 parser->error = save_error;
1085 continue;
1087 default:
1088 break;
1091 c_parser_consume_token (parser);
1094 finished:
1095 parser->error = false;
1098 /* CPP's options (initialized by c-opts.c). */
1099 extern cpp_options *cpp_opts;
1101 /* Save the warning flags which are controlled by __extension__. */
1103 static inline int
1104 disable_extension_diagnostics (void)
1106 int ret = (pedantic
1107 | (warn_pointer_arith << 1)
1108 | (warn_traditional << 2)
1109 | (flag_iso << 3)
1110 | (warn_long_long << 4)
1111 | (warn_cxx_compat << 5)
1112 | (warn_overlength_strings << 6)
1113 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1114 play tricks to properly restore it. */
1115 | ((warn_c90_c99_compat == 1) << 7)
1116 | ((warn_c90_c99_compat == -1) << 8)
1117 /* Similarly for warn_c99_c11_compat. */
1118 | ((warn_c99_c11_compat == 1) << 9)
1119 | ((warn_c99_c11_compat == -1) << 10)
1121 cpp_opts->cpp_pedantic = pedantic = 0;
1122 warn_pointer_arith = 0;
1123 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1124 flag_iso = 0;
1125 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1126 warn_cxx_compat = 0;
1127 warn_overlength_strings = 0;
1128 warn_c90_c99_compat = 0;
1129 warn_c99_c11_compat = 0;
1130 return ret;
1133 /* Restore the warning flags which are controlled by __extension__.
1134 FLAGS is the return value from disable_extension_diagnostics. */
1136 static inline void
1137 restore_extension_diagnostics (int flags)
1139 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1140 warn_pointer_arith = (flags >> 1) & 1;
1141 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1142 flag_iso = (flags >> 3) & 1;
1143 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1144 warn_cxx_compat = (flags >> 5) & 1;
1145 warn_overlength_strings = (flags >> 6) & 1;
1146 /* See above for why is this needed. */
1147 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1148 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1151 /* Possibly kinds of declarator to parse. */
1152 enum c_dtr_syn {
1153 /* A normal declarator with an identifier. */
1154 C_DTR_NORMAL,
1155 /* An abstract declarator (maybe empty). */
1156 C_DTR_ABSTRACT,
1157 /* A parameter declarator: may be either, but after a type name does
1158 not redeclare a typedef name as an identifier if it can
1159 alternatively be interpreted as a typedef name; see DR#009,
1160 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2
1161 following DR#249. For example, given a typedef T, "int T" and
1162 "int *T" are valid parameter declarations redeclaring T, while
1163 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are
1164 abstract declarators rather than involving redundant parentheses;
1165 the same applies with attributes inside the parentheses before
1166 "T". */
1167 C_DTR_PARM
1170 /* The binary operation precedence levels, where 0 is a dummy lowest level
1171 used for the bottom of the stack. */
1172 enum c_parser_prec {
1173 PREC_NONE,
1174 PREC_LOGOR,
1175 PREC_LOGAND,
1176 PREC_BITOR,
1177 PREC_BITXOR,
1178 PREC_BITAND,
1179 PREC_EQ,
1180 PREC_REL,
1181 PREC_SHIFT,
1182 PREC_ADD,
1183 PREC_MULT,
1184 NUM_PRECS
1187 static void c_parser_external_declaration (c_parser *);
1188 static void c_parser_asm_definition (c_parser *);
1189 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1190 bool, bool, tree *, vec<c_token>,
1191 tree = NULL_TREE);
1192 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1193 static void c_parser_static_assert_declaration (c_parser *);
1194 static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool,
1195 bool, bool, bool, enum c_lookahead_kind);
1196 static struct c_typespec c_parser_enum_specifier (c_parser *);
1197 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1198 static tree c_parser_struct_declaration (c_parser *);
1199 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1200 static tree c_parser_alignas_specifier (c_parser *);
1201 static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn,
1202 bool *);
1203 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1204 c_dtr_syn, bool *);
1205 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1206 bool,
1207 struct c_declarator *);
1208 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1209 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1210 tree);
1211 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1212 static tree c_parser_simple_asm_expr (c_parser *);
1213 static tree c_parser_attributes (c_parser *);
1214 static struct c_type_name *c_parser_type_name (c_parser *);
1215 static struct c_expr c_parser_initializer (c_parser *);
1216 static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
1217 static void c_parser_initelt (c_parser *, struct obstack *);
1218 static void c_parser_initval (c_parser *, struct c_expr *,
1219 struct obstack *);
1220 static tree c_parser_compound_statement (c_parser *);
1221 static void c_parser_compound_statement_nostart (c_parser *);
1222 static void c_parser_label (c_parser *);
1223 static void c_parser_statement (c_parser *);
1224 static void c_parser_statement_after_labels (c_parser *, vec<tree> * = NULL);
1225 static void c_parser_if_statement (c_parser *, vec<tree> *);
1226 static void c_parser_switch_statement (c_parser *);
1227 static void c_parser_while_statement (c_parser *, bool);
1228 static void c_parser_do_statement (c_parser *, bool);
1229 static void c_parser_for_statement (c_parser *, bool);
1230 static tree c_parser_asm_statement (c_parser *);
1231 static tree c_parser_asm_operands (c_parser *);
1232 static tree c_parser_asm_goto_operands (c_parser *);
1233 static tree c_parser_asm_clobbers (c_parser *);
1234 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1235 tree = NULL_TREE);
1236 static struct c_expr c_parser_conditional_expression (c_parser *,
1237 struct c_expr *, tree);
1238 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1239 tree);
1240 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1241 static struct c_expr c_parser_unary_expression (c_parser *);
1242 static struct c_expr c_parser_sizeof_expression (c_parser *);
1243 static struct c_expr c_parser_alignof_expression (c_parser *);
1244 static struct c_expr c_parser_postfix_expression (c_parser *);
1245 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1246 struct c_type_name *,
1247 location_t);
1248 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1249 location_t loc,
1250 struct c_expr);
1251 static tree c_parser_transaction (c_parser *, enum rid);
1252 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1253 static tree c_parser_transaction_cancel (c_parser *);
1254 static struct c_expr c_parser_expression (c_parser *);
1255 static struct c_expr c_parser_expression_conv (c_parser *);
1256 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1257 vec<tree, va_gc> **, location_t *,
1258 tree *, vec<location_t> *,
1259 unsigned int * = NULL);
1260 static void c_parser_oacc_declare (c_parser *);
1261 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1262 static void c_parser_oacc_update (c_parser *);
1263 static void c_parser_omp_construct (c_parser *);
1264 static void c_parser_omp_threadprivate (c_parser *);
1265 static void c_parser_omp_barrier (c_parser *);
1266 static void c_parser_omp_flush (c_parser *);
1267 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1268 tree, tree *);
1269 static void c_parser_omp_taskwait (c_parser *);
1270 static void c_parser_omp_taskyield (c_parser *);
1271 static void c_parser_omp_cancel (c_parser *);
1272 static void c_parser_omp_cancellation_point (c_parser *);
1274 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1275 pragma_stmt, pragma_compound };
1276 static bool c_parser_pragma (c_parser *, enum pragma_context);
1277 static bool c_parser_omp_target (c_parser *, enum pragma_context);
1278 static void c_parser_omp_end_declare_target (c_parser *);
1279 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1280 static bool c_parser_omp_ordered (c_parser *, enum pragma_context);
1281 static void c_parser_oacc_routine (c_parser *parser, enum pragma_context);
1283 /* These Objective-C parser functions are only ever called when
1284 compiling Objective-C. */
1285 static void c_parser_objc_class_definition (c_parser *, tree);
1286 static void c_parser_objc_class_instance_variables (c_parser *);
1287 static void c_parser_objc_class_declaration (c_parser *);
1288 static void c_parser_objc_alias_declaration (c_parser *);
1289 static void c_parser_objc_protocol_definition (c_parser *, tree);
1290 static bool c_parser_objc_method_type (c_parser *);
1291 static void c_parser_objc_method_definition (c_parser *);
1292 static void c_parser_objc_methodprotolist (c_parser *);
1293 static void c_parser_objc_methodproto (c_parser *);
1294 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1295 static tree c_parser_objc_type_name (c_parser *);
1296 static tree c_parser_objc_protocol_refs (c_parser *);
1297 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1298 static void c_parser_objc_synchronized_statement (c_parser *);
1299 static tree c_parser_objc_selector (c_parser *);
1300 static tree c_parser_objc_selector_arg (c_parser *);
1301 static tree c_parser_objc_receiver (c_parser *);
1302 static tree c_parser_objc_message_args (c_parser *);
1303 static tree c_parser_objc_keywordexpr (c_parser *);
1304 static void c_parser_objc_at_property_declaration (c_parser *);
1305 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1306 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1307 static bool c_parser_objc_diagnose_bad_element_prefix
1308 (c_parser *, struct c_declspecs *);
1310 /* Cilk Plus supporting routines. */
1311 static void c_parser_cilk_simd (c_parser *);
1312 static void c_parser_cilk_for (c_parser *, tree);
1313 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1314 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1315 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1316 static void c_parser_cilk_grainsize (c_parser *);
1318 /* Parse a translation unit (C90 6.7, C99 6.9).
1320 translation-unit:
1321 external-declarations
1323 external-declarations:
1324 external-declaration
1325 external-declarations external-declaration
1327 GNU extensions:
1329 translation-unit:
1330 empty
1333 static void
1334 c_parser_translation_unit (c_parser *parser)
1336 if (c_parser_next_token_is (parser, CPP_EOF))
1338 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1339 "ISO C forbids an empty translation unit");
1341 else
1343 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1344 mark_valid_location_for_stdc_pragma (false);
1347 ggc_collect ();
1348 c_parser_external_declaration (parser);
1349 obstack_free (&parser_obstack, obstack_position);
1351 while (c_parser_next_token_is_not (parser, CPP_EOF));
1355 /* Parse an external declaration (C90 6.7, C99 6.9).
1357 external-declaration:
1358 function-definition
1359 declaration
1361 GNU extensions:
1363 external-declaration:
1364 asm-definition
1366 __extension__ external-declaration
1368 Objective-C:
1370 external-declaration:
1371 objc-class-definition
1372 objc-class-declaration
1373 objc-alias-declaration
1374 objc-protocol-definition
1375 objc-method-definition
1376 @end
1379 static void
1380 c_parser_external_declaration (c_parser *parser)
1382 int ext;
1383 switch (c_parser_peek_token (parser)->type)
1385 case CPP_KEYWORD:
1386 switch (c_parser_peek_token (parser)->keyword)
1388 case RID_EXTENSION:
1389 ext = disable_extension_diagnostics ();
1390 c_parser_consume_token (parser);
1391 c_parser_external_declaration (parser);
1392 restore_extension_diagnostics (ext);
1393 break;
1394 case RID_ASM:
1395 c_parser_asm_definition (parser);
1396 break;
1397 case RID_AT_INTERFACE:
1398 case RID_AT_IMPLEMENTATION:
1399 gcc_assert (c_dialect_objc ());
1400 c_parser_objc_class_definition (parser, NULL_TREE);
1401 break;
1402 case RID_AT_CLASS:
1403 gcc_assert (c_dialect_objc ());
1404 c_parser_objc_class_declaration (parser);
1405 break;
1406 case RID_AT_ALIAS:
1407 gcc_assert (c_dialect_objc ());
1408 c_parser_objc_alias_declaration (parser);
1409 break;
1410 case RID_AT_PROTOCOL:
1411 gcc_assert (c_dialect_objc ());
1412 c_parser_objc_protocol_definition (parser, NULL_TREE);
1413 break;
1414 case RID_AT_PROPERTY:
1415 gcc_assert (c_dialect_objc ());
1416 c_parser_objc_at_property_declaration (parser);
1417 break;
1418 case RID_AT_SYNTHESIZE:
1419 gcc_assert (c_dialect_objc ());
1420 c_parser_objc_at_synthesize_declaration (parser);
1421 break;
1422 case RID_AT_DYNAMIC:
1423 gcc_assert (c_dialect_objc ());
1424 c_parser_objc_at_dynamic_declaration (parser);
1425 break;
1426 case RID_AT_END:
1427 gcc_assert (c_dialect_objc ());
1428 c_parser_consume_token (parser);
1429 objc_finish_implementation ();
1430 break;
1431 default:
1432 goto decl_or_fndef;
1434 break;
1435 case CPP_SEMICOLON:
1436 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1437 "ISO C does not allow extra %<;%> outside of a function");
1438 c_parser_consume_token (parser);
1439 break;
1440 case CPP_PRAGMA:
1441 mark_valid_location_for_stdc_pragma (true);
1442 c_parser_pragma (parser, pragma_external);
1443 mark_valid_location_for_stdc_pragma (false);
1444 break;
1445 case CPP_PLUS:
1446 case CPP_MINUS:
1447 if (c_dialect_objc ())
1449 c_parser_objc_method_definition (parser);
1450 break;
1452 /* Else fall through, and yield a syntax error trying to parse
1453 as a declaration or function definition. */
1454 default:
1455 decl_or_fndef:
1456 /* A declaration or a function definition (or, in Objective-C,
1457 an @interface or @protocol with prefix attributes). We can
1458 only tell which after parsing the declaration specifiers, if
1459 any, and the first declarator. */
1460 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1461 NULL, vNULL);
1462 break;
1466 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1467 static void c_finish_oacc_routine (c_parser *, tree, tree, bool, bool, bool);
1469 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1470 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1471 accepted; otherwise (old-style parameter declarations) only other
1472 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1473 assertion is accepted; otherwise (old-style parameter declarations)
1474 it is not. If NESTED is true, we are inside a function or parsing
1475 old-style parameter declarations; any functions encountered are
1476 nested functions and declaration specifiers are required; otherwise
1477 we are at top level and functions are normal functions and
1478 declaration specifiers may be optional. If EMPTY_OK is true, empty
1479 declarations are OK (subject to all other constraints); otherwise
1480 (old-style parameter declarations) they are diagnosed. If
1481 START_ATTR_OK is true, the declaration specifiers may start with
1482 attributes; otherwise they may not.
1483 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1484 declaration when parsing an Objective-C foreach statement.
1486 declaration:
1487 declaration-specifiers init-declarator-list[opt] ;
1488 static_assert-declaration
1490 function-definition:
1491 declaration-specifiers[opt] declarator declaration-list[opt]
1492 compound-statement
1494 declaration-list:
1495 declaration
1496 declaration-list declaration
1498 init-declarator-list:
1499 init-declarator
1500 init-declarator-list , init-declarator
1502 init-declarator:
1503 declarator simple-asm-expr[opt] attributes[opt]
1504 declarator simple-asm-expr[opt] attributes[opt] = initializer
1506 GNU extensions:
1508 nested-function-definition:
1509 declaration-specifiers declarator declaration-list[opt]
1510 compound-statement
1512 Objective-C:
1513 attributes objc-class-definition
1514 attributes objc-category-definition
1515 attributes objc-protocol-definition
1517 The simple-asm-expr and attributes are GNU extensions.
1519 This function does not handle __extension__; that is handled in its
1520 callers. ??? Following the old parser, __extension__ may start
1521 external declarations, declarations in functions and declarations
1522 at the start of "for" loops, but not old-style parameter
1523 declarations.
1525 C99 requires declaration specifiers in a function definition; the
1526 absence is diagnosed through the diagnosis of implicit int. In GNU
1527 C we also allow but diagnose declarations without declaration
1528 specifiers, but only at top level (elsewhere they conflict with
1529 other syntax).
1531 In Objective-C, declarations of the looping variable in a foreach
1532 statement are exceptionally terminated by 'in' (for example, 'for
1533 (NSObject *object in array) { ... }').
1535 OpenMP:
1537 declaration:
1538 threadprivate-directive */
1540 static void
1541 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1542 bool static_assert_ok, bool empty_ok,
1543 bool nested, bool start_attr_ok,
1544 tree *objc_foreach_object_declaration,
1545 vec<c_token> omp_declare_simd_clauses,
1546 tree oacc_routine_clauses)
1548 struct c_declspecs *specs;
1549 tree prefix_attrs;
1550 tree all_prefix_attrs;
1551 bool diagnosed_no_specs = false;
1552 location_t here = c_parser_peek_token (parser)->location;
1554 if (static_assert_ok
1555 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1557 c_parser_static_assert_declaration (parser);
1558 return;
1560 specs = build_null_declspecs ();
1562 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1563 if (c_parser_peek_token (parser)->type == CPP_NAME
1564 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1565 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1566 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1567 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1569 tree name = c_parser_peek_token (parser)->value;
1570 error_at (here, "unknown type name %qE", name);
1571 /* Give a hint to the user. This is not C++ with its implicit
1572 typedef. */
1573 if (tag_exists_p (RECORD_TYPE, name))
1574 inform (here, "use %<struct%> keyword to refer to the type");
1575 else if (tag_exists_p (UNION_TYPE, name))
1576 inform (here, "use %<union%> keyword to refer to the type");
1577 else if (tag_exists_p (ENUMERAL_TYPE, name))
1578 inform (here, "use %<enum%> keyword to refer to the type");
1580 /* Parse declspecs normally to get a correct pointer type, but avoid
1581 a further "fails to be a type name" error. Refuse nested functions
1582 since it is not how the user likely wants us to recover. */
1583 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1584 c_parser_peek_token (parser)->keyword = RID_VOID;
1585 c_parser_peek_token (parser)->value = error_mark_node;
1586 fndef_ok = !nested;
1589 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1590 true, true, cla_nonabstract_decl);
1591 if (parser->error)
1593 c_parser_skip_to_end_of_block_or_statement (parser);
1594 return;
1596 if (nested && !specs->declspecs_seen_p)
1598 c_parser_error (parser, "expected declaration specifiers");
1599 c_parser_skip_to_end_of_block_or_statement (parser);
1600 return;
1602 finish_declspecs (specs);
1603 bool auto_type_p = specs->typespec_word == cts_auto_type;
1604 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1606 if (auto_type_p)
1607 error_at (here, "%<__auto_type%> in empty declaration");
1608 else if (empty_ok)
1609 shadow_tag (specs);
1610 else
1612 shadow_tag_warned (specs, 1);
1613 pedwarn (here, 0, "empty declaration");
1615 c_parser_consume_token (parser);
1616 if (oacc_routine_clauses)
1617 c_finish_oacc_routine (parser, NULL_TREE,
1618 oacc_routine_clauses, false, true, false);
1619 return;
1622 /* Provide better error recovery. Note that a type name here is usually
1623 better diagnosed as a redeclaration. */
1624 if (empty_ok
1625 && specs->typespec_kind == ctsk_tagdef
1626 && c_parser_next_token_starts_declspecs (parser)
1627 && !c_parser_next_token_is (parser, CPP_NAME))
1629 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1630 parser->error = false;
1631 shadow_tag_warned (specs, 1);
1632 return;
1634 else if (c_dialect_objc () && !auto_type_p)
1636 /* Prefix attributes are an error on method decls. */
1637 switch (c_parser_peek_token (parser)->type)
1639 case CPP_PLUS:
1640 case CPP_MINUS:
1641 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1642 return;
1643 if (specs->attrs)
1645 warning_at (c_parser_peek_token (parser)->location,
1646 OPT_Wattributes,
1647 "prefix attributes are ignored for methods");
1648 specs->attrs = NULL_TREE;
1650 if (fndef_ok)
1651 c_parser_objc_method_definition (parser);
1652 else
1653 c_parser_objc_methodproto (parser);
1654 return;
1655 break;
1656 default:
1657 break;
1659 /* This is where we parse 'attributes @interface ...',
1660 'attributes @implementation ...', 'attributes @protocol ...'
1661 (where attributes could be, for example, __attribute__
1662 ((deprecated)).
1664 switch (c_parser_peek_token (parser)->keyword)
1666 case RID_AT_INTERFACE:
1668 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1669 return;
1670 c_parser_objc_class_definition (parser, specs->attrs);
1671 return;
1673 break;
1674 case RID_AT_IMPLEMENTATION:
1676 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1677 return;
1678 if (specs->attrs)
1680 warning_at (c_parser_peek_token (parser)->location,
1681 OPT_Wattributes,
1682 "prefix attributes are ignored for implementations");
1683 specs->attrs = NULL_TREE;
1685 c_parser_objc_class_definition (parser, NULL_TREE);
1686 return;
1688 break;
1689 case RID_AT_PROTOCOL:
1691 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1692 return;
1693 c_parser_objc_protocol_definition (parser, specs->attrs);
1694 return;
1696 break;
1697 case RID_AT_ALIAS:
1698 case RID_AT_CLASS:
1699 case RID_AT_END:
1700 case RID_AT_PROPERTY:
1701 if (specs->attrs)
1703 c_parser_error (parser, "unexpected attribute");
1704 specs->attrs = NULL;
1706 break;
1707 default:
1708 break;
1712 pending_xref_error ();
1713 prefix_attrs = specs->attrs;
1714 all_prefix_attrs = prefix_attrs;
1715 specs->attrs = NULL_TREE;
1716 for (bool first = true;; first = false)
1718 struct c_declarator *declarator;
1719 bool dummy = false;
1720 timevar_id_t tv;
1721 tree fnbody;
1722 /* Declaring either one or more declarators (in which case we
1723 should diagnose if there were no declaration specifiers) or a
1724 function definition (in which case the diagnostic for
1725 implicit int suffices). */
1726 declarator = c_parser_declarator (parser,
1727 specs->typespec_kind != ctsk_none,
1728 C_DTR_NORMAL, &dummy);
1729 if (declarator == NULL)
1731 if (omp_declare_simd_clauses.exists ()
1732 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1733 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1734 omp_declare_simd_clauses);
1735 if (oacc_routine_clauses)
1736 c_finish_oacc_routine (parser, NULL_TREE,
1737 oacc_routine_clauses,
1738 false, first, false);
1739 c_parser_skip_to_end_of_block_or_statement (parser);
1740 return;
1742 if (auto_type_p && declarator->kind != cdk_id)
1744 error_at (here,
1745 "%<__auto_type%> requires a plain identifier"
1746 " as declarator");
1747 c_parser_skip_to_end_of_block_or_statement (parser);
1748 return;
1750 if (c_parser_next_token_is (parser, CPP_EQ)
1751 || c_parser_next_token_is (parser, CPP_COMMA)
1752 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1753 || c_parser_next_token_is_keyword (parser, RID_ASM)
1754 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1755 || c_parser_next_token_is_keyword (parser, RID_IN))
1757 tree asm_name = NULL_TREE;
1758 tree postfix_attrs = NULL_TREE;
1759 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1761 diagnosed_no_specs = true;
1762 pedwarn (here, 0, "data definition has no type or storage class");
1764 /* Having seen a data definition, there cannot now be a
1765 function definition. */
1766 fndef_ok = false;
1767 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1768 asm_name = c_parser_simple_asm_expr (parser);
1769 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1771 postfix_attrs = c_parser_attributes (parser);
1772 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1774 /* This means there is an attribute specifier after
1775 the declarator in a function definition. Provide
1776 some more information for the user. */
1777 error_at (here, "attributes should be specified before the "
1778 "declarator in a function definition");
1779 c_parser_skip_to_end_of_block_or_statement (parser);
1780 return;
1783 if (c_parser_next_token_is (parser, CPP_EQ))
1785 tree d;
1786 struct c_expr init;
1787 location_t init_loc;
1788 c_parser_consume_token (parser);
1789 if (auto_type_p)
1791 start_init (NULL_TREE, asm_name, global_bindings_p ());
1792 init_loc = c_parser_peek_token (parser)->location;
1793 init = c_parser_expr_no_commas (parser, NULL);
1794 if (TREE_CODE (init.value) == COMPONENT_REF
1795 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1796 error_at (here,
1797 "%<__auto_type%> used with a bit-field"
1798 " initializer");
1799 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1800 tree init_type = TREE_TYPE (init.value);
1801 /* As with typeof, remove all qualifiers from atomic types. */
1802 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1803 init_type
1804 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1805 bool vm_type = variably_modified_type_p (init_type,
1806 NULL_TREE);
1807 if (vm_type)
1808 init.value = c_save_expr (init.value);
1809 finish_init ();
1810 specs->typespec_kind = ctsk_typeof;
1811 specs->locations[cdw_typedef] = init_loc;
1812 specs->typedef_p = true;
1813 specs->type = init_type;
1814 if (vm_type)
1816 bool maybe_const = true;
1817 tree type_expr = c_fully_fold (init.value, false,
1818 &maybe_const);
1819 specs->expr_const_operands &= maybe_const;
1820 if (specs->expr)
1821 specs->expr = build2 (COMPOUND_EXPR,
1822 TREE_TYPE (type_expr),
1823 specs->expr, type_expr);
1824 else
1825 specs->expr = type_expr;
1827 d = start_decl (declarator, specs, true,
1828 chainon (postfix_attrs, all_prefix_attrs));
1829 if (!d)
1830 d = error_mark_node;
1831 if (omp_declare_simd_clauses.exists ()
1832 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1833 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1834 omp_declare_simd_clauses);
1836 else
1838 /* The declaration of the variable is in effect while
1839 its initializer is parsed. */
1840 d = start_decl (declarator, specs, true,
1841 chainon (postfix_attrs, all_prefix_attrs));
1842 if (!d)
1843 d = error_mark_node;
1844 if (omp_declare_simd_clauses.exists ()
1845 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1846 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1847 omp_declare_simd_clauses);
1848 start_init (d, asm_name, global_bindings_p ());
1849 init_loc = c_parser_peek_token (parser)->location;
1850 init = c_parser_initializer (parser);
1851 finish_init ();
1853 if (oacc_routine_clauses)
1854 c_finish_oacc_routine (parser, d, oacc_routine_clauses,
1855 false, first, false);
1856 if (d != error_mark_node)
1858 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1859 finish_decl (d, init_loc, init.value,
1860 init.original_type, asm_name);
1863 else
1865 if (auto_type_p)
1867 error_at (here,
1868 "%<__auto_type%> requires an initialized "
1869 "data declaration");
1870 c_parser_skip_to_end_of_block_or_statement (parser);
1871 return;
1873 tree d = start_decl (declarator, specs, false,
1874 chainon (postfix_attrs,
1875 all_prefix_attrs));
1876 if (omp_declare_simd_clauses.exists ()
1877 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1879 tree parms = NULL_TREE;
1880 if (d && TREE_CODE (d) == FUNCTION_DECL)
1882 struct c_declarator *ce = declarator;
1883 while (ce != NULL)
1884 if (ce->kind == cdk_function)
1886 parms = ce->u.arg_info->parms;
1887 break;
1889 else
1890 ce = ce->declarator;
1892 if (parms)
1893 temp_store_parm_decls (d, parms);
1894 c_finish_omp_declare_simd (parser, d, parms,
1895 omp_declare_simd_clauses);
1896 if (parms)
1897 temp_pop_parm_decls ();
1899 if (oacc_routine_clauses)
1900 c_finish_oacc_routine (parser, d, oacc_routine_clauses,
1901 false, first, false);
1902 if (d)
1903 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1904 NULL_TREE, asm_name);
1906 if (c_parser_next_token_is_keyword (parser, RID_IN))
1908 if (d)
1909 *objc_foreach_object_declaration = d;
1910 else
1911 *objc_foreach_object_declaration = error_mark_node;
1914 if (c_parser_next_token_is (parser, CPP_COMMA))
1916 if (auto_type_p)
1918 error_at (here,
1919 "%<__auto_type%> may only be used with"
1920 " a single declarator");
1921 c_parser_skip_to_end_of_block_or_statement (parser);
1922 return;
1924 c_parser_consume_token (parser);
1925 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1926 all_prefix_attrs = chainon (c_parser_attributes (parser),
1927 prefix_attrs);
1928 else
1929 all_prefix_attrs = prefix_attrs;
1930 continue;
1932 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1934 c_parser_consume_token (parser);
1935 return;
1937 else if (c_parser_next_token_is_keyword (parser, RID_IN))
1939 /* This can only happen in Objective-C: we found the
1940 'in' that terminates the declaration inside an
1941 Objective-C foreach statement. Do not consume the
1942 token, so that the caller can use it to determine
1943 that this indeed is a foreach context. */
1944 return;
1946 else
1948 c_parser_error (parser, "expected %<,%> or %<;%>");
1949 c_parser_skip_to_end_of_block_or_statement (parser);
1950 return;
1953 else if (auto_type_p)
1955 error_at (here,
1956 "%<__auto_type%> requires an initialized data declaration");
1957 c_parser_skip_to_end_of_block_or_statement (parser);
1958 return;
1960 else if (!fndef_ok)
1962 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
1963 "%<asm%> or %<__attribute__%>");
1964 c_parser_skip_to_end_of_block_or_statement (parser);
1965 return;
1967 /* Function definition (nested or otherwise). */
1968 if (nested)
1970 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
1971 c_push_function_context ();
1973 if (!start_function (specs, declarator, all_prefix_attrs))
1975 /* This can appear in many cases looking nothing like a
1976 function definition, so we don't give a more specific
1977 error suggesting there was one. */
1978 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
1979 "or %<__attribute__%>");
1980 if (nested)
1981 c_pop_function_context ();
1982 break;
1985 if (DECL_DECLARED_INLINE_P (current_function_decl))
1986 tv = TV_PARSE_INLINE;
1987 else
1988 tv = TV_PARSE_FUNC;
1989 timevar_push (tv);
1991 /* Parse old-style parameter declarations. ??? Attributes are
1992 not allowed to start declaration specifiers here because of a
1993 syntax conflict between a function declaration with attribute
1994 suffix and a function definition with an attribute prefix on
1995 first old-style parameter declaration. Following the old
1996 parser, they are not accepted on subsequent old-style
1997 parameter declarations either. However, there is no
1998 ambiguity after the first declaration, nor indeed on the
1999 first as long as we don't allow postfix attributes after a
2000 declarator with a nonempty identifier list in a definition;
2001 and postfix attributes have never been accepted here in
2002 function definitions either. */
2003 while (c_parser_next_token_is_not (parser, CPP_EOF)
2004 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2005 c_parser_declaration_or_fndef (parser, false, false, false,
2006 true, false, NULL, vNULL);
2007 store_parm_decls ();
2008 if (omp_declare_simd_clauses.exists ()
2009 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2010 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2011 omp_declare_simd_clauses);
2012 if (oacc_routine_clauses)
2013 c_finish_oacc_routine (parser, current_function_decl,
2014 oacc_routine_clauses, false, first, true);
2015 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2016 = c_parser_peek_token (parser)->location;
2017 fnbody = c_parser_compound_statement (parser);
2018 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2019 fnbody = expand_array_notation_exprs (fnbody);
2020 if (nested)
2022 tree decl = current_function_decl;
2023 /* Mark nested functions as needing static-chain initially.
2024 lower_nested_functions will recompute it but the
2025 DECL_STATIC_CHAIN flag is also used before that happens,
2026 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2027 DECL_STATIC_CHAIN (decl) = 1;
2028 add_stmt (fnbody);
2029 finish_function ();
2030 c_pop_function_context ();
2031 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2033 else
2035 add_stmt (fnbody);
2036 finish_function ();
2039 timevar_pop (tv);
2040 break;
2044 /* Parse an asm-definition (asm() outside a function body). This is a
2045 GNU extension.
2047 asm-definition:
2048 simple-asm-expr ;
2051 static void
2052 c_parser_asm_definition (c_parser *parser)
2054 tree asm_str = c_parser_simple_asm_expr (parser);
2055 if (asm_str)
2056 symtab->finalize_toplevel_asm (asm_str);
2057 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2060 /* Parse a static assertion (C11 6.7.10).
2062 static_assert-declaration:
2063 static_assert-declaration-no-semi ;
2066 static void
2067 c_parser_static_assert_declaration (c_parser *parser)
2069 c_parser_static_assert_declaration_no_semi (parser);
2070 if (parser->error
2071 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2072 c_parser_skip_to_end_of_block_or_statement (parser);
2075 /* Parse a static assertion (C11 6.7.10), without the trailing
2076 semicolon.
2078 static_assert-declaration-no-semi:
2079 _Static_assert ( constant-expression , string-literal )
2082 static void
2083 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2085 location_t assert_loc, value_loc;
2086 tree value;
2087 tree string;
2089 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2090 assert_loc = c_parser_peek_token (parser)->location;
2091 if (flag_isoc99)
2092 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2093 "ISO C99 does not support %<_Static_assert%>");
2094 else
2095 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2096 "ISO C90 does not support %<_Static_assert%>");
2097 c_parser_consume_token (parser);
2098 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2099 return;
2100 value_loc = c_parser_peek_token (parser)->location;
2101 value = c_parser_expr_no_commas (parser, NULL).value;
2102 parser->lex_untranslated_string = true;
2103 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2105 parser->lex_untranslated_string = false;
2106 return;
2108 switch (c_parser_peek_token (parser)->type)
2110 case CPP_STRING:
2111 case CPP_STRING16:
2112 case CPP_STRING32:
2113 case CPP_WSTRING:
2114 case CPP_UTF8STRING:
2115 string = c_parser_peek_token (parser)->value;
2116 c_parser_consume_token (parser);
2117 parser->lex_untranslated_string = false;
2118 break;
2119 default:
2120 c_parser_error (parser, "expected string literal");
2121 parser->lex_untranslated_string = false;
2122 return;
2124 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2126 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2128 error_at (value_loc, "expression in static assertion is not an integer");
2129 return;
2131 if (TREE_CODE (value) != INTEGER_CST)
2133 value = c_fully_fold (value, false, NULL);
2134 /* Strip no-op conversions. */
2135 STRIP_TYPE_NOPS (value);
2136 if (TREE_CODE (value) == INTEGER_CST)
2137 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2138 "is not an integer constant expression");
2140 if (TREE_CODE (value) != INTEGER_CST)
2142 error_at (value_loc, "expression in static assertion is not constant");
2143 return;
2145 constant_expression_warning (value);
2146 if (integer_zerop (value))
2147 error_at (assert_loc, "static assertion failed: %E", string);
2150 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2151 6.7), adding them to SPECS (which may already include some).
2152 Storage class specifiers are accepted iff SCSPEC_OK; type
2153 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2154 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2155 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2157 declaration-specifiers:
2158 storage-class-specifier declaration-specifiers[opt]
2159 type-specifier declaration-specifiers[opt]
2160 type-qualifier declaration-specifiers[opt]
2161 function-specifier declaration-specifiers[opt]
2162 alignment-specifier declaration-specifiers[opt]
2164 Function specifiers (inline) are from C99, and are currently
2165 handled as storage class specifiers, as is __thread. Alignment
2166 specifiers are from C11.
2168 C90 6.5.1, C99 6.7.1:
2169 storage-class-specifier:
2170 typedef
2171 extern
2172 static
2173 auto
2174 register
2175 _Thread_local
2177 (_Thread_local is new in C11.)
2179 C99 6.7.4:
2180 function-specifier:
2181 inline
2182 _Noreturn
2184 (_Noreturn is new in C11.)
2186 C90 6.5.2, C99 6.7.2:
2187 type-specifier:
2188 void
2189 char
2190 short
2192 long
2193 float
2194 double
2195 signed
2196 unsigned
2197 _Bool
2198 _Complex
2199 [_Imaginary removed in C99 TC2]
2200 struct-or-union-specifier
2201 enum-specifier
2202 typedef-name
2203 atomic-type-specifier
2205 (_Bool and _Complex are new in C99.)
2206 (atomic-type-specifier is new in C11.)
2208 C90 6.5.3, C99 6.7.3:
2210 type-qualifier:
2211 const
2212 restrict
2213 volatile
2214 address-space-qualifier
2215 _Atomic
2217 (restrict is new in C99.)
2218 (_Atomic is new in C11.)
2220 GNU extensions:
2222 declaration-specifiers:
2223 attributes declaration-specifiers[opt]
2225 type-qualifier:
2226 address-space
2228 address-space:
2229 identifier recognized by the target
2231 storage-class-specifier:
2232 __thread
2234 type-specifier:
2235 typeof-specifier
2236 __auto_type
2237 __intN
2238 _Decimal32
2239 _Decimal64
2240 _Decimal128
2241 _Fract
2242 _Accum
2243 _Sat
2245 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2246 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2248 atomic-type-specifier
2249 _Atomic ( type-name )
2251 Objective-C:
2253 type-specifier:
2254 class-name objc-protocol-refs[opt]
2255 typedef-name objc-protocol-refs
2256 objc-protocol-refs
2259 static void
2260 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2261 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2262 bool alignspec_ok, bool auto_type_ok,
2263 enum c_lookahead_kind la)
2265 bool attrs_ok = start_attr_ok;
2266 bool seen_type = specs->typespec_kind != ctsk_none;
2268 if (!typespec_ok)
2269 gcc_assert (la == cla_prefer_id);
2271 while (c_parser_next_token_is (parser, CPP_NAME)
2272 || c_parser_next_token_is (parser, CPP_KEYWORD)
2273 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2275 struct c_typespec t;
2276 tree attrs;
2277 tree align;
2278 location_t loc = c_parser_peek_token (parser)->location;
2280 /* If we cannot accept a type, exit if the next token must start
2281 one. Also, if we already have seen a tagged definition,
2282 a typename would be an error anyway and likely the user
2283 has simply forgotten a semicolon, so we exit. */
2284 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2285 && c_parser_next_tokens_start_typename (parser, la)
2286 && !c_parser_next_token_is_qualifier (parser))
2287 break;
2289 if (c_parser_next_token_is (parser, CPP_NAME))
2291 c_token *name_token = c_parser_peek_token (parser);
2292 tree value = name_token->value;
2293 c_id_kind kind = name_token->id_kind;
2295 if (kind == C_ID_ADDRSPACE)
2297 addr_space_t as
2298 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2299 declspecs_add_addrspace (name_token->location, specs, as);
2300 c_parser_consume_token (parser);
2301 attrs_ok = true;
2302 continue;
2305 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2307 /* If we cannot accept a type, and the next token must start one,
2308 exit. Do the same if we already have seen a tagged definition,
2309 since it would be an error anyway and likely the user has simply
2310 forgotten a semicolon. */
2311 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2312 break;
2314 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2315 a C_ID_CLASSNAME. */
2316 c_parser_consume_token (parser);
2317 seen_type = true;
2318 attrs_ok = true;
2319 if (kind == C_ID_ID)
2321 error_at (loc, "unknown type name %qE", value);
2322 t.kind = ctsk_typedef;
2323 t.spec = error_mark_node;
2325 else if (kind == C_ID_TYPENAME
2326 && (!c_dialect_objc ()
2327 || c_parser_next_token_is_not (parser, CPP_LESS)))
2329 t.kind = ctsk_typedef;
2330 /* For a typedef name, record the meaning, not the name.
2331 In case of 'foo foo, bar;'. */
2332 t.spec = lookup_name (value);
2334 else
2336 tree proto = NULL_TREE;
2337 gcc_assert (c_dialect_objc ());
2338 t.kind = ctsk_objc;
2339 if (c_parser_next_token_is (parser, CPP_LESS))
2340 proto = c_parser_objc_protocol_refs (parser);
2341 t.spec = objc_get_protocol_qualified_type (value, proto);
2343 t.expr = NULL_TREE;
2344 t.expr_const_operands = true;
2345 declspecs_add_type (name_token->location, specs, t);
2346 continue;
2348 if (c_parser_next_token_is (parser, CPP_LESS))
2350 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2351 nisse@lysator.liu.se. */
2352 tree proto;
2353 gcc_assert (c_dialect_objc ());
2354 if (!typespec_ok || seen_type)
2355 break;
2356 proto = c_parser_objc_protocol_refs (parser);
2357 t.kind = ctsk_objc;
2358 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2359 t.expr = NULL_TREE;
2360 t.expr_const_operands = true;
2361 declspecs_add_type (loc, specs, t);
2362 continue;
2364 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2365 switch (c_parser_peek_token (parser)->keyword)
2367 case RID_STATIC:
2368 case RID_EXTERN:
2369 case RID_REGISTER:
2370 case RID_TYPEDEF:
2371 case RID_INLINE:
2372 case RID_NORETURN:
2373 case RID_AUTO:
2374 case RID_THREAD:
2375 if (!scspec_ok)
2376 goto out;
2377 attrs_ok = true;
2378 /* TODO: Distinguish between function specifiers (inline, noreturn)
2379 and storage class specifiers, either here or in
2380 declspecs_add_scspec. */
2381 declspecs_add_scspec (loc, specs,
2382 c_parser_peek_token (parser)->value);
2383 c_parser_consume_token (parser);
2384 break;
2385 case RID_AUTO_TYPE:
2386 if (!auto_type_ok)
2387 goto out;
2388 /* Fall through. */
2389 case RID_UNSIGNED:
2390 case RID_LONG:
2391 case RID_SHORT:
2392 case RID_SIGNED:
2393 case RID_COMPLEX:
2394 case RID_INT:
2395 case RID_CHAR:
2396 case RID_FLOAT:
2397 case RID_DOUBLE:
2398 case RID_VOID:
2399 case RID_DFLOAT32:
2400 case RID_DFLOAT64:
2401 case RID_DFLOAT128:
2402 case RID_BOOL:
2403 case RID_FRACT:
2404 case RID_ACCUM:
2405 case RID_SAT:
2406 case RID_INT_N_0:
2407 case RID_INT_N_1:
2408 case RID_INT_N_2:
2409 case RID_INT_N_3:
2410 if (!typespec_ok)
2411 goto out;
2412 attrs_ok = true;
2413 seen_type = true;
2414 if (c_dialect_objc ())
2415 parser->objc_need_raw_identifier = true;
2416 t.kind = ctsk_resword;
2417 t.spec = c_parser_peek_token (parser)->value;
2418 t.expr = NULL_TREE;
2419 t.expr_const_operands = true;
2420 declspecs_add_type (loc, specs, t);
2421 c_parser_consume_token (parser);
2422 break;
2423 case RID_ENUM:
2424 if (!typespec_ok)
2425 goto out;
2426 attrs_ok = true;
2427 seen_type = true;
2428 t = c_parser_enum_specifier (parser);
2429 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2430 declspecs_add_type (loc, specs, t);
2431 break;
2432 case RID_STRUCT:
2433 case RID_UNION:
2434 if (!typespec_ok)
2435 goto out;
2436 attrs_ok = true;
2437 seen_type = true;
2438 t = c_parser_struct_or_union_specifier (parser);
2439 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2440 declspecs_add_type (loc, specs, t);
2441 break;
2442 case RID_TYPEOF:
2443 /* ??? The old parser rejected typeof after other type
2444 specifiers, but is a syntax error the best way of
2445 handling this? */
2446 if (!typespec_ok || seen_type)
2447 goto out;
2448 attrs_ok = true;
2449 seen_type = true;
2450 t = c_parser_typeof_specifier (parser);
2451 declspecs_add_type (loc, specs, t);
2452 break;
2453 case RID_ATOMIC:
2454 /* C parser handling of Objective-C constructs needs
2455 checking for correct lvalue-to-rvalue conversions, and
2456 the code in build_modify_expr handling various
2457 Objective-C cases, and that in build_unary_op handling
2458 Objective-C cases for increment / decrement, also needs
2459 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2460 and objc_types_are_equivalent may also need updates. */
2461 if (c_dialect_objc ())
2462 sorry ("%<_Atomic%> in Objective-C");
2463 /* C parser handling of OpenMP constructs needs checking for
2464 correct lvalue-to-rvalue conversions. */
2465 if (flag_openmp)
2466 sorry ("%<_Atomic%> with OpenMP");
2467 if (flag_isoc99)
2468 pedwarn_c99 (loc, OPT_Wpedantic,
2469 "ISO C99 does not support the %<_Atomic%> qualifier");
2470 else
2471 pedwarn_c99 (loc, OPT_Wpedantic,
2472 "ISO C90 does not support the %<_Atomic%> qualifier");
2473 attrs_ok = true;
2474 tree value;
2475 value = c_parser_peek_token (parser)->value;
2476 c_parser_consume_token (parser);
2477 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2479 /* _Atomic ( type-name ). */
2480 seen_type = true;
2481 c_parser_consume_token (parser);
2482 struct c_type_name *type = c_parser_type_name (parser);
2483 t.kind = ctsk_typeof;
2484 t.spec = error_mark_node;
2485 t.expr = NULL_TREE;
2486 t.expr_const_operands = true;
2487 if (type != NULL)
2488 t.spec = groktypename (type, &t.expr,
2489 &t.expr_const_operands);
2490 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2491 "expected %<)%>");
2492 if (t.spec != error_mark_node)
2494 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2495 error_at (loc, "%<_Atomic%>-qualified array type");
2496 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2497 error_at (loc, "%<_Atomic%>-qualified function type");
2498 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2499 error_at (loc, "%<_Atomic%> applied to a qualified type");
2500 else
2501 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2503 declspecs_add_type (loc, specs, t);
2505 else
2506 declspecs_add_qual (loc, specs, value);
2507 break;
2508 case RID_CONST:
2509 case RID_VOLATILE:
2510 case RID_RESTRICT:
2511 attrs_ok = true;
2512 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2513 c_parser_consume_token (parser);
2514 break;
2515 case RID_ATTRIBUTE:
2516 if (!attrs_ok)
2517 goto out;
2518 attrs = c_parser_attributes (parser);
2519 declspecs_add_attrs (loc, specs, attrs);
2520 break;
2521 case RID_ALIGNAS:
2522 if (!alignspec_ok)
2523 goto out;
2524 align = c_parser_alignas_specifier (parser);
2525 declspecs_add_alignas (loc, specs, align);
2526 break;
2527 default:
2528 goto out;
2531 out: ;
2534 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2536 enum-specifier:
2537 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2538 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2539 enum attributes[opt] identifier
2541 The form with trailing comma is new in C99. The forms with
2542 attributes are GNU extensions. In GNU C, we accept any expression
2543 without commas in the syntax (assignment expressions, not just
2544 conditional expressions); assignment expressions will be diagnosed
2545 as non-constant.
2547 enumerator-list:
2548 enumerator
2549 enumerator-list , enumerator
2551 enumerator:
2552 enumeration-constant
2553 enumeration-constant = constant-expression
2555 GNU Extensions:
2557 enumerator:
2558 enumeration-constant attributes[opt]
2559 enumeration-constant attributes[opt] = constant-expression
2563 static struct c_typespec
2564 c_parser_enum_specifier (c_parser *parser)
2566 struct c_typespec ret;
2567 tree attrs;
2568 tree ident = NULL_TREE;
2569 location_t enum_loc;
2570 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2571 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2572 enum_loc = c_parser_peek_token (parser)->location;
2573 c_parser_consume_token (parser);
2574 attrs = c_parser_attributes (parser);
2575 enum_loc = c_parser_peek_token (parser)->location;
2576 /* Set the location in case we create a decl now. */
2577 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2578 if (c_parser_next_token_is (parser, CPP_NAME))
2580 ident = c_parser_peek_token (parser)->value;
2581 ident_loc = c_parser_peek_token (parser)->location;
2582 enum_loc = ident_loc;
2583 c_parser_consume_token (parser);
2585 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2587 /* Parse an enum definition. */
2588 struct c_enum_contents the_enum;
2589 tree type;
2590 tree postfix_attrs;
2591 /* We chain the enumerators in reverse order, then put them in
2592 forward order at the end. */
2593 tree values;
2594 timevar_push (TV_PARSE_ENUM);
2595 type = start_enum (enum_loc, &the_enum, ident);
2596 values = NULL_TREE;
2597 c_parser_consume_token (parser);
2598 while (true)
2600 tree enum_id;
2601 tree enum_value;
2602 tree enum_decl;
2603 bool seen_comma;
2604 c_token *token;
2605 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2606 location_t decl_loc, value_loc;
2607 if (c_parser_next_token_is_not (parser, CPP_NAME))
2609 /* Give a nicer error for "enum {}". */
2610 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2611 && !parser->error)
2613 error_at (c_parser_peek_token (parser)->location,
2614 "empty enum is invalid");
2615 parser->error = true;
2617 else
2618 c_parser_error (parser, "expected identifier");
2619 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2620 values = error_mark_node;
2621 break;
2623 token = c_parser_peek_token (parser);
2624 enum_id = token->value;
2625 /* Set the location in case we create a decl now. */
2626 c_parser_set_source_position_from_token (token);
2627 decl_loc = value_loc = token->location;
2628 c_parser_consume_token (parser);
2629 /* Parse any specified attributes. */
2630 tree enum_attrs = c_parser_attributes (parser);
2631 if (c_parser_next_token_is (parser, CPP_EQ))
2633 c_parser_consume_token (parser);
2634 value_loc = c_parser_peek_token (parser)->location;
2635 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2637 else
2638 enum_value = NULL_TREE;
2639 enum_decl = build_enumerator (decl_loc, value_loc,
2640 &the_enum, enum_id, enum_value);
2641 if (enum_attrs)
2642 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2643 TREE_CHAIN (enum_decl) = values;
2644 values = enum_decl;
2645 seen_comma = false;
2646 if (c_parser_next_token_is (parser, CPP_COMMA))
2648 comma_loc = c_parser_peek_token (parser)->location;
2649 seen_comma = true;
2650 c_parser_consume_token (parser);
2652 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2654 if (seen_comma)
2655 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2656 "comma at end of enumerator list");
2657 c_parser_consume_token (parser);
2658 break;
2660 if (!seen_comma)
2662 c_parser_error (parser, "expected %<,%> or %<}%>");
2663 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2664 values = error_mark_node;
2665 break;
2668 postfix_attrs = c_parser_attributes (parser);
2669 ret.spec = finish_enum (type, nreverse (values),
2670 chainon (attrs, postfix_attrs));
2671 ret.kind = ctsk_tagdef;
2672 ret.expr = NULL_TREE;
2673 ret.expr_const_operands = true;
2674 timevar_pop (TV_PARSE_ENUM);
2675 return ret;
2677 else if (!ident)
2679 c_parser_error (parser, "expected %<{%>");
2680 ret.spec = error_mark_node;
2681 ret.kind = ctsk_tagref;
2682 ret.expr = NULL_TREE;
2683 ret.expr_const_operands = true;
2684 return ret;
2686 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2687 /* In ISO C, enumerated types can be referred to only if already
2688 defined. */
2689 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2691 gcc_assert (ident);
2692 pedwarn (enum_loc, OPT_Wpedantic,
2693 "ISO C forbids forward references to %<enum%> types");
2695 return ret;
2698 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2700 struct-or-union-specifier:
2701 struct-or-union attributes[opt] identifier[opt]
2702 { struct-contents } attributes[opt]
2703 struct-or-union attributes[opt] identifier
2705 struct-contents:
2706 struct-declaration-list
2708 struct-declaration-list:
2709 struct-declaration ;
2710 struct-declaration-list struct-declaration ;
2712 GNU extensions:
2714 struct-contents:
2715 empty
2716 struct-declaration
2717 struct-declaration-list struct-declaration
2719 struct-declaration-list:
2720 struct-declaration-list ;
2723 (Note that in the syntax here, unlike that in ISO C, the semicolons
2724 are included here rather than in struct-declaration, in order to
2725 describe the syntax with extra semicolons and missing semicolon at
2726 end.)
2728 Objective-C:
2730 struct-declaration-list:
2731 @defs ( class-name )
2733 (Note this does not include a trailing semicolon, but can be
2734 followed by further declarations, and gets a pedwarn-if-pedantic
2735 when followed by a semicolon.) */
2737 static struct c_typespec
2738 c_parser_struct_or_union_specifier (c_parser *parser)
2740 struct c_typespec ret;
2741 tree attrs;
2742 tree ident = NULL_TREE;
2743 location_t struct_loc;
2744 location_t ident_loc = UNKNOWN_LOCATION;
2745 enum tree_code code;
2746 switch (c_parser_peek_token (parser)->keyword)
2748 case RID_STRUCT:
2749 code = RECORD_TYPE;
2750 break;
2751 case RID_UNION:
2752 code = UNION_TYPE;
2753 break;
2754 default:
2755 gcc_unreachable ();
2757 struct_loc = c_parser_peek_token (parser)->location;
2758 c_parser_consume_token (parser);
2759 attrs = c_parser_attributes (parser);
2761 /* Set the location in case we create a decl now. */
2762 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2764 if (c_parser_next_token_is (parser, CPP_NAME))
2766 ident = c_parser_peek_token (parser)->value;
2767 ident_loc = c_parser_peek_token (parser)->location;
2768 struct_loc = ident_loc;
2769 c_parser_consume_token (parser);
2771 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2773 /* Parse a struct or union definition. Start the scope of the
2774 tag before parsing components. */
2775 struct c_struct_parse_info *struct_info;
2776 tree type = start_struct (struct_loc, code, ident, &struct_info);
2777 tree postfix_attrs;
2778 /* We chain the components in reverse order, then put them in
2779 forward order at the end. Each struct-declaration may
2780 declare multiple components (comma-separated), so we must use
2781 chainon to join them, although when parsing each
2782 struct-declaration we can use TREE_CHAIN directly.
2784 The theory behind all this is that there will be more
2785 semicolon separated fields than comma separated fields, and
2786 so we'll be minimizing the number of node traversals required
2787 by chainon. */
2788 tree contents;
2789 timevar_push (TV_PARSE_STRUCT);
2790 contents = NULL_TREE;
2791 c_parser_consume_token (parser);
2792 /* Handle the Objective-C @defs construct,
2793 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2794 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2796 tree name;
2797 gcc_assert (c_dialect_objc ());
2798 c_parser_consume_token (parser);
2799 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2800 goto end_at_defs;
2801 if (c_parser_next_token_is (parser, CPP_NAME)
2802 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2804 name = c_parser_peek_token (parser)->value;
2805 c_parser_consume_token (parser);
2807 else
2809 c_parser_error (parser, "expected class name");
2810 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2811 goto end_at_defs;
2813 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2814 "expected %<)%>");
2815 contents = nreverse (objc_get_class_ivars (name));
2817 end_at_defs:
2818 /* Parse the struct-declarations and semicolons. Problems with
2819 semicolons are diagnosed here; empty structures are diagnosed
2820 elsewhere. */
2821 while (true)
2823 tree decls;
2824 /* Parse any stray semicolon. */
2825 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2827 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2828 "extra semicolon in struct or union specified");
2829 c_parser_consume_token (parser);
2830 continue;
2832 /* Stop if at the end of the struct or union contents. */
2833 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2835 c_parser_consume_token (parser);
2836 break;
2838 /* Accept #pragmas at struct scope. */
2839 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2841 c_parser_pragma (parser, pragma_struct);
2842 continue;
2844 /* Parse some comma-separated declarations, but not the
2845 trailing semicolon if any. */
2846 decls = c_parser_struct_declaration (parser);
2847 contents = chainon (decls, contents);
2848 /* If no semicolon follows, either we have a parse error or
2849 are at the end of the struct or union and should
2850 pedwarn. */
2851 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2852 c_parser_consume_token (parser);
2853 else
2855 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2856 pedwarn (c_parser_peek_token (parser)->location, 0,
2857 "no semicolon at end of struct or union");
2858 else if (parser->error
2859 || !c_parser_next_token_starts_declspecs (parser))
2861 c_parser_error (parser, "expected %<;%>");
2862 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2863 break;
2866 /* If we come here, we have already emitted an error
2867 for an expected `;', identifier or `(', and we also
2868 recovered already. Go on with the next field. */
2871 postfix_attrs = c_parser_attributes (parser);
2872 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2873 chainon (attrs, postfix_attrs), struct_info);
2874 ret.kind = ctsk_tagdef;
2875 ret.expr = NULL_TREE;
2876 ret.expr_const_operands = true;
2877 timevar_pop (TV_PARSE_STRUCT);
2878 return ret;
2880 else if (!ident)
2882 c_parser_error (parser, "expected %<{%>");
2883 ret.spec = error_mark_node;
2884 ret.kind = ctsk_tagref;
2885 ret.expr = NULL_TREE;
2886 ret.expr_const_operands = true;
2887 return ret;
2889 ret = parser_xref_tag (ident_loc, code, ident);
2890 return ret;
2893 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
2894 the trailing semicolon.
2896 struct-declaration:
2897 specifier-qualifier-list struct-declarator-list
2898 static_assert-declaration-no-semi
2900 specifier-qualifier-list:
2901 type-specifier specifier-qualifier-list[opt]
2902 type-qualifier specifier-qualifier-list[opt]
2903 attributes specifier-qualifier-list[opt]
2905 struct-declarator-list:
2906 struct-declarator
2907 struct-declarator-list , attributes[opt] struct-declarator
2909 struct-declarator:
2910 declarator attributes[opt]
2911 declarator[opt] : constant-expression attributes[opt]
2913 GNU extensions:
2915 struct-declaration:
2916 __extension__ struct-declaration
2917 specifier-qualifier-list
2919 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
2920 of attributes where shown is a GNU extension. In GNU C, we accept
2921 any expression without commas in the syntax (assignment
2922 expressions, not just conditional expressions); assignment
2923 expressions will be diagnosed as non-constant. */
2925 static tree
2926 c_parser_struct_declaration (c_parser *parser)
2928 struct c_declspecs *specs;
2929 tree prefix_attrs;
2930 tree all_prefix_attrs;
2931 tree decls;
2932 location_t decl_loc;
2933 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
2935 int ext;
2936 tree decl;
2937 ext = disable_extension_diagnostics ();
2938 c_parser_consume_token (parser);
2939 decl = c_parser_struct_declaration (parser);
2940 restore_extension_diagnostics (ext);
2941 return decl;
2943 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
2945 c_parser_static_assert_declaration_no_semi (parser);
2946 return NULL_TREE;
2948 specs = build_null_declspecs ();
2949 decl_loc = c_parser_peek_token (parser)->location;
2950 /* Strictly by the standard, we shouldn't allow _Alignas here,
2951 but it appears to have been intended to allow it there, so
2952 we're keeping it as it is until WG14 reaches a conclusion
2953 of N1731.
2954 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
2955 c_parser_declspecs (parser, specs, false, true, true,
2956 true, false, cla_nonabstract_decl);
2957 if (parser->error)
2958 return NULL_TREE;
2959 if (!specs->declspecs_seen_p)
2961 c_parser_error (parser, "expected specifier-qualifier-list");
2962 return NULL_TREE;
2964 finish_declspecs (specs);
2965 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
2966 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2968 tree ret;
2969 if (specs->typespec_kind == ctsk_none)
2971 pedwarn (decl_loc, OPT_Wpedantic,
2972 "ISO C forbids member declarations with no members");
2973 shadow_tag_warned (specs, pedantic);
2974 ret = NULL_TREE;
2976 else
2978 /* Support for unnamed structs or unions as members of
2979 structs or unions (which is [a] useful and [b] supports
2980 MS P-SDK). */
2981 tree attrs = NULL;
2983 ret = grokfield (c_parser_peek_token (parser)->location,
2984 build_id_declarator (NULL_TREE), specs,
2985 NULL_TREE, &attrs);
2986 if (ret)
2987 decl_attributes (&ret, attrs, 0);
2989 return ret;
2992 /* Provide better error recovery. Note that a type name here is valid,
2993 and will be treated as a field name. */
2994 if (specs->typespec_kind == ctsk_tagdef
2995 && TREE_CODE (specs->type) != ENUMERAL_TYPE
2996 && c_parser_next_token_starts_declspecs (parser)
2997 && !c_parser_next_token_is (parser, CPP_NAME))
2999 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3000 parser->error = false;
3001 return NULL_TREE;
3004 pending_xref_error ();
3005 prefix_attrs = specs->attrs;
3006 all_prefix_attrs = prefix_attrs;
3007 specs->attrs = NULL_TREE;
3008 decls = NULL_TREE;
3009 while (true)
3011 /* Declaring one or more declarators or un-named bit-fields. */
3012 struct c_declarator *declarator;
3013 bool dummy = false;
3014 if (c_parser_next_token_is (parser, CPP_COLON))
3015 declarator = build_id_declarator (NULL_TREE);
3016 else
3017 declarator = c_parser_declarator (parser,
3018 specs->typespec_kind != ctsk_none,
3019 C_DTR_NORMAL, &dummy);
3020 if (declarator == NULL)
3022 c_parser_skip_to_end_of_block_or_statement (parser);
3023 break;
3025 if (c_parser_next_token_is (parser, CPP_COLON)
3026 || c_parser_next_token_is (parser, CPP_COMMA)
3027 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3028 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3029 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3031 tree postfix_attrs = NULL_TREE;
3032 tree width = NULL_TREE;
3033 tree d;
3034 if (c_parser_next_token_is (parser, CPP_COLON))
3036 c_parser_consume_token (parser);
3037 width = c_parser_expr_no_commas (parser, NULL).value;
3039 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3040 postfix_attrs = c_parser_attributes (parser);
3041 d = grokfield (c_parser_peek_token (parser)->location,
3042 declarator, specs, width, &all_prefix_attrs);
3043 decl_attributes (&d, chainon (postfix_attrs,
3044 all_prefix_attrs), 0);
3045 DECL_CHAIN (d) = decls;
3046 decls = d;
3047 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3048 all_prefix_attrs = chainon (c_parser_attributes (parser),
3049 prefix_attrs);
3050 else
3051 all_prefix_attrs = prefix_attrs;
3052 if (c_parser_next_token_is (parser, CPP_COMMA))
3053 c_parser_consume_token (parser);
3054 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3055 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3057 /* Semicolon consumed in caller. */
3058 break;
3060 else
3062 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3063 break;
3066 else
3068 c_parser_error (parser,
3069 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3070 "%<__attribute__%>");
3071 break;
3074 return decls;
3077 /* Parse a typeof specifier (a GNU extension).
3079 typeof-specifier:
3080 typeof ( expression )
3081 typeof ( type-name )
3084 static struct c_typespec
3085 c_parser_typeof_specifier (c_parser *parser)
3087 struct c_typespec ret;
3088 ret.kind = ctsk_typeof;
3089 ret.spec = error_mark_node;
3090 ret.expr = NULL_TREE;
3091 ret.expr_const_operands = true;
3092 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3093 c_parser_consume_token (parser);
3094 c_inhibit_evaluation_warnings++;
3095 in_typeof++;
3096 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3098 c_inhibit_evaluation_warnings--;
3099 in_typeof--;
3100 return ret;
3102 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3104 struct c_type_name *type = c_parser_type_name (parser);
3105 c_inhibit_evaluation_warnings--;
3106 in_typeof--;
3107 if (type != NULL)
3109 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3110 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3113 else
3115 bool was_vm;
3116 location_t here = c_parser_peek_token (parser)->location;
3117 struct c_expr expr = c_parser_expression (parser);
3118 c_inhibit_evaluation_warnings--;
3119 in_typeof--;
3120 if (TREE_CODE (expr.value) == COMPONENT_REF
3121 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3122 error_at (here, "%<typeof%> applied to a bit-field");
3123 mark_exp_read (expr.value);
3124 ret.spec = TREE_TYPE (expr.value);
3125 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3126 /* This is returned with the type so that when the type is
3127 evaluated, this can be evaluated. */
3128 if (was_vm)
3129 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3130 pop_maybe_used (was_vm);
3131 /* For use in macros such as those in <stdatomic.h>, remove all
3132 qualifiers from atomic types. (const can be an issue for more macros
3133 using typeof than just the <stdatomic.h> ones.) */
3134 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3135 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3137 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3138 return ret;
3141 /* Parse an alignment-specifier.
3143 C11 6.7.5:
3145 alignment-specifier:
3146 _Alignas ( type-name )
3147 _Alignas ( constant-expression )
3150 static tree
3151 c_parser_alignas_specifier (c_parser * parser)
3153 tree ret = error_mark_node;
3154 location_t loc = c_parser_peek_token (parser)->location;
3155 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3156 c_parser_consume_token (parser);
3157 if (flag_isoc99)
3158 pedwarn_c99 (loc, OPT_Wpedantic,
3159 "ISO C99 does not support %<_Alignas%>");
3160 else
3161 pedwarn_c99 (loc, OPT_Wpedantic,
3162 "ISO C90 does not support %<_Alignas%>");
3163 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3164 return ret;
3165 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3167 struct c_type_name *type = c_parser_type_name (parser);
3168 if (type != NULL)
3169 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3170 false, true, 1);
3172 else
3173 ret = c_parser_expr_no_commas (parser, NULL).value;
3174 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3175 return ret;
3178 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3179 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3180 be redeclared; otherwise it may not. KIND indicates which kind of
3181 declarator is wanted. Returns a valid declarator except in the
3182 case of a syntax error in which case NULL is returned. *SEEN_ID is
3183 set to true if an identifier being declared is seen; this is used
3184 to diagnose bad forms of abstract array declarators and to
3185 determine whether an identifier list is syntactically permitted.
3187 declarator:
3188 pointer[opt] direct-declarator
3190 direct-declarator:
3191 identifier
3192 ( attributes[opt] declarator )
3193 direct-declarator array-declarator
3194 direct-declarator ( parameter-type-list )
3195 direct-declarator ( identifier-list[opt] )
3197 pointer:
3198 * type-qualifier-list[opt]
3199 * type-qualifier-list[opt] pointer
3201 type-qualifier-list:
3202 type-qualifier
3203 attributes
3204 type-qualifier-list type-qualifier
3205 type-qualifier-list attributes
3207 array-declarator:
3208 [ type-qualifier-list[opt] assignment-expression[opt] ]
3209 [ static type-qualifier-list[opt] assignment-expression ]
3210 [ type-qualifier-list static assignment-expression ]
3211 [ type-qualifier-list[opt] * ]
3213 parameter-type-list:
3214 parameter-list
3215 parameter-list , ...
3217 parameter-list:
3218 parameter-declaration
3219 parameter-list , parameter-declaration
3221 parameter-declaration:
3222 declaration-specifiers declarator attributes[opt]
3223 declaration-specifiers abstract-declarator[opt] attributes[opt]
3225 identifier-list:
3226 identifier
3227 identifier-list , identifier
3229 abstract-declarator:
3230 pointer
3231 pointer[opt] direct-abstract-declarator
3233 direct-abstract-declarator:
3234 ( attributes[opt] abstract-declarator )
3235 direct-abstract-declarator[opt] array-declarator
3236 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3238 GNU extensions:
3240 direct-declarator:
3241 direct-declarator ( parameter-forward-declarations
3242 parameter-type-list[opt] )
3244 direct-abstract-declarator:
3245 direct-abstract-declarator[opt] ( parameter-forward-declarations
3246 parameter-type-list[opt] )
3248 parameter-forward-declarations:
3249 parameter-list ;
3250 parameter-forward-declarations parameter-list ;
3252 The uses of attributes shown above are GNU extensions.
3254 Some forms of array declarator are not included in C99 in the
3255 syntax for abstract declarators; these are disallowed elsewhere.
3256 This may be a defect (DR#289).
3258 This function also accepts an omitted abstract declarator as being
3259 an abstract declarator, although not part of the formal syntax. */
3261 static struct c_declarator *
3262 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3263 bool *seen_id)
3265 /* Parse any initial pointer part. */
3266 if (c_parser_next_token_is (parser, CPP_MULT))
3268 struct c_declspecs *quals_attrs = build_null_declspecs ();
3269 struct c_declarator *inner;
3270 c_parser_consume_token (parser);
3271 c_parser_declspecs (parser, quals_attrs, false, false, true,
3272 false, false, cla_prefer_id);
3273 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3274 if (inner == NULL)
3275 return NULL;
3276 else
3277 return make_pointer_declarator (quals_attrs, inner);
3279 /* Now we have a direct declarator, direct abstract declarator or
3280 nothing (which counts as a direct abstract declarator here). */
3281 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3284 /* Parse a direct declarator or direct abstract declarator; arguments
3285 as c_parser_declarator. */
3287 static struct c_declarator *
3288 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3289 bool *seen_id)
3291 /* The direct declarator must start with an identifier (possibly
3292 omitted) or a parenthesized declarator (possibly abstract). In
3293 an ordinary declarator, initial parentheses must start a
3294 parenthesized declarator. In an abstract declarator or parameter
3295 declarator, they could start a parenthesized declarator or a
3296 parameter list. To tell which, the open parenthesis and any
3297 following attributes must be read. If a declaration specifier
3298 follows, then it is a parameter list; if the specifier is a
3299 typedef name, there might be an ambiguity about redeclaring it,
3300 which is resolved in the direction of treating it as a typedef
3301 name. If a close parenthesis follows, it is also an empty
3302 parameter list, as the syntax does not permit empty abstract
3303 declarators. Otherwise, it is a parenthesized declarator (in
3304 which case the analysis may be repeated inside it, recursively).
3306 ??? There is an ambiguity in a parameter declaration "int
3307 (__attribute__((foo)) x)", where x is not a typedef name: it
3308 could be an abstract declarator for a function, or declare x with
3309 parentheses. The proper resolution of this ambiguity needs
3310 documenting. At present we follow an accident of the old
3311 parser's implementation, whereby the first parameter must have
3312 some declaration specifiers other than just attributes. Thus as
3313 a parameter declaration it is treated as a parenthesized
3314 parameter named x, and as an abstract declarator it is
3315 rejected.
3317 ??? Also following the old parser, attributes inside an empty
3318 parameter list are ignored, making it a list not yielding a
3319 prototype, rather than giving an error or making it have one
3320 parameter with implicit type int.
3322 ??? Also following the old parser, typedef names may be
3323 redeclared in declarators, but not Objective-C class names. */
3325 if (kind != C_DTR_ABSTRACT
3326 && c_parser_next_token_is (parser, CPP_NAME)
3327 && ((type_seen_p
3328 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3329 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3330 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3332 struct c_declarator *inner
3333 = build_id_declarator (c_parser_peek_token (parser)->value);
3334 *seen_id = true;
3335 inner->id_loc = c_parser_peek_token (parser)->location;
3336 c_parser_consume_token (parser);
3337 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3340 if (kind != C_DTR_NORMAL
3341 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3343 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3344 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3347 /* Either we are at the end of an abstract declarator, or we have
3348 parentheses. */
3350 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3352 tree attrs;
3353 struct c_declarator *inner;
3354 c_parser_consume_token (parser);
3355 attrs = c_parser_attributes (parser);
3356 if (kind != C_DTR_NORMAL
3357 && (c_parser_next_token_starts_declspecs (parser)
3358 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3360 struct c_arg_info *args
3361 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3362 attrs);
3363 if (args == NULL)
3364 return NULL;
3365 else
3367 inner
3368 = build_function_declarator (args,
3369 build_id_declarator (NULL_TREE));
3370 return c_parser_direct_declarator_inner (parser, *seen_id,
3371 inner);
3374 /* A parenthesized declarator. */
3375 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3376 if (inner != NULL && attrs != NULL)
3377 inner = build_attrs_declarator (attrs, inner);
3378 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3380 c_parser_consume_token (parser);
3381 if (inner == NULL)
3382 return NULL;
3383 else
3384 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3386 else
3388 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3389 "expected %<)%>");
3390 return NULL;
3393 else
3395 if (kind == C_DTR_NORMAL)
3397 c_parser_error (parser, "expected identifier or %<(%>");
3398 return NULL;
3400 else
3401 return build_id_declarator (NULL_TREE);
3405 /* Parse part of a direct declarator or direct abstract declarator,
3406 given that some (in INNER) has already been parsed; ID_PRESENT is
3407 true if an identifier is present, false for an abstract
3408 declarator. */
3410 static struct c_declarator *
3411 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3412 struct c_declarator *inner)
3414 /* Parse a sequence of array declarators and parameter lists. */
3415 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3417 location_t brace_loc = c_parser_peek_token (parser)->location;
3418 struct c_declarator *declarator;
3419 struct c_declspecs *quals_attrs = build_null_declspecs ();
3420 bool static_seen;
3421 bool star_seen;
3422 struct c_expr dimen;
3423 dimen.value = NULL_TREE;
3424 dimen.original_code = ERROR_MARK;
3425 dimen.original_type = NULL_TREE;
3426 c_parser_consume_token (parser);
3427 c_parser_declspecs (parser, quals_attrs, false, false, true,
3428 false, false, cla_prefer_id);
3429 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3430 if (static_seen)
3431 c_parser_consume_token (parser);
3432 if (static_seen && !quals_attrs->declspecs_seen_p)
3433 c_parser_declspecs (parser, quals_attrs, false, false, true,
3434 false, false, cla_prefer_id);
3435 if (!quals_attrs->declspecs_seen_p)
3436 quals_attrs = NULL;
3437 /* If "static" is present, there must be an array dimension.
3438 Otherwise, there may be a dimension, "*", or no
3439 dimension. */
3440 if (static_seen)
3442 star_seen = false;
3443 dimen = c_parser_expr_no_commas (parser, NULL);
3445 else
3447 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3449 dimen.value = NULL_TREE;
3450 star_seen = false;
3452 else if (flag_cilkplus
3453 && c_parser_next_token_is (parser, CPP_COLON))
3455 dimen.value = error_mark_node;
3456 star_seen = false;
3457 error_at (c_parser_peek_token (parser)->location,
3458 "array notations cannot be used in declaration");
3459 c_parser_consume_token (parser);
3461 else if (c_parser_next_token_is (parser, CPP_MULT))
3463 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3465 dimen.value = NULL_TREE;
3466 star_seen = true;
3467 c_parser_consume_token (parser);
3469 else
3471 star_seen = false;
3472 dimen = c_parser_expr_no_commas (parser, NULL);
3475 else
3477 star_seen = false;
3478 dimen = c_parser_expr_no_commas (parser, NULL);
3481 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3482 c_parser_consume_token (parser);
3483 else if (flag_cilkplus
3484 && c_parser_next_token_is (parser, CPP_COLON))
3486 error_at (c_parser_peek_token (parser)->location,
3487 "array notations cannot be used in declaration");
3488 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3489 return NULL;
3491 else
3493 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3494 "expected %<]%>");
3495 return NULL;
3497 if (dimen.value)
3498 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3499 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3500 static_seen, star_seen);
3501 if (declarator == NULL)
3502 return NULL;
3503 inner = set_array_declarator_inner (declarator, inner);
3504 return c_parser_direct_declarator_inner (parser, id_present, inner);
3506 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3508 tree attrs;
3509 struct c_arg_info *args;
3510 c_parser_consume_token (parser);
3511 attrs = c_parser_attributes (parser);
3512 args = c_parser_parms_declarator (parser, id_present, attrs);
3513 if (args == NULL)
3514 return NULL;
3515 else
3517 inner = build_function_declarator (args, inner);
3518 return c_parser_direct_declarator_inner (parser, id_present, inner);
3521 return inner;
3524 /* Parse a parameter list or identifier list, including the closing
3525 parenthesis but not the opening one. ATTRS are the attributes at
3526 the start of the list. ID_LIST_OK is true if an identifier list is
3527 acceptable; such a list must not have attributes at the start. */
3529 static struct c_arg_info *
3530 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3532 push_scope ();
3533 declare_parm_level ();
3534 /* If the list starts with an identifier, it is an identifier list.
3535 Otherwise, it is either a prototype list or an empty list. */
3536 if (id_list_ok
3537 && !attrs
3538 && c_parser_next_token_is (parser, CPP_NAME)
3539 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3541 /* Look ahead to detect typos in type names. */
3542 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3543 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3544 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3545 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE)
3547 tree list = NULL_TREE, *nextp = &list;
3548 while (c_parser_next_token_is (parser, CPP_NAME)
3549 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3551 *nextp = build_tree_list (NULL_TREE,
3552 c_parser_peek_token (parser)->value);
3553 nextp = & TREE_CHAIN (*nextp);
3554 c_parser_consume_token (parser);
3555 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3556 break;
3557 c_parser_consume_token (parser);
3558 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3560 c_parser_error (parser, "expected identifier");
3561 break;
3564 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3566 struct c_arg_info *ret = build_arg_info ();
3567 ret->types = list;
3568 c_parser_consume_token (parser);
3569 pop_scope ();
3570 return ret;
3572 else
3574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3575 "expected %<)%>");
3576 pop_scope ();
3577 return NULL;
3580 else
3582 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3583 NULL);
3584 pop_scope ();
3585 return ret;
3589 /* Parse a parameter list (possibly empty), including the closing
3590 parenthesis but not the opening one. ATTRS are the attributes at
3591 the start of the list. EXPR is NULL or an expression that needs to
3592 be evaluated for the side effects of array size expressions in the
3593 parameters. */
3595 static struct c_arg_info *
3596 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3598 bool bad_parm = false;
3600 /* ??? Following the old parser, forward parameter declarations may
3601 use abstract declarators, and if no real parameter declarations
3602 follow the forward declarations then this is not diagnosed. Also
3603 note as above that attributes are ignored as the only contents of
3604 the parentheses, or as the only contents after forward
3605 declarations. */
3606 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3608 struct c_arg_info *ret = build_arg_info ();
3609 c_parser_consume_token (parser);
3610 return ret;
3612 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3614 struct c_arg_info *ret = build_arg_info ();
3616 if (flag_allow_parameterless_variadic_functions)
3618 /* F (...) is allowed. */
3619 ret->types = NULL_TREE;
3621 else
3623 /* Suppress -Wold-style-definition for this case. */
3624 ret->types = error_mark_node;
3625 error_at (c_parser_peek_token (parser)->location,
3626 "ISO C requires a named argument before %<...%>");
3628 c_parser_consume_token (parser);
3629 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3631 c_parser_consume_token (parser);
3632 return ret;
3634 else
3636 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3637 "expected %<)%>");
3638 return NULL;
3641 /* Nonempty list of parameters, either terminated with semicolon
3642 (forward declarations; recurse) or with close parenthesis (normal
3643 function) or with ", ... )" (variadic function). */
3644 while (true)
3646 /* Parse a parameter. */
3647 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3648 attrs = NULL_TREE;
3649 if (parm == NULL)
3650 bad_parm = true;
3651 else
3652 push_parm_decl (parm, &expr);
3653 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3655 tree new_attrs;
3656 c_parser_consume_token (parser);
3657 mark_forward_parm_decls ();
3658 new_attrs = c_parser_attributes (parser);
3659 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3661 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3663 c_parser_consume_token (parser);
3664 if (bad_parm)
3665 return NULL;
3666 else
3667 return get_parm_info (false, expr);
3669 if (!c_parser_require (parser, CPP_COMMA,
3670 "expected %<;%>, %<,%> or %<)%>"))
3672 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3673 return NULL;
3675 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3677 c_parser_consume_token (parser);
3678 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3680 c_parser_consume_token (parser);
3681 if (bad_parm)
3682 return NULL;
3683 else
3684 return get_parm_info (true, expr);
3686 else
3688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3689 "expected %<)%>");
3690 return NULL;
3696 /* Parse a parameter declaration. ATTRS are the attributes at the
3697 start of the declaration if it is the first parameter. */
3699 static struct c_parm *
3700 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3702 struct c_declspecs *specs;
3703 struct c_declarator *declarator;
3704 tree prefix_attrs;
3705 tree postfix_attrs = NULL_TREE;
3706 bool dummy = false;
3708 /* Accept #pragmas between parameter declarations. */
3709 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3710 c_parser_pragma (parser, pragma_param);
3712 if (!c_parser_next_token_starts_declspecs (parser))
3714 c_token *token = c_parser_peek_token (parser);
3715 if (parser->error)
3716 return NULL;
3717 c_parser_set_source_position_from_token (token);
3718 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3720 error_at (token->location, "unknown type name %qE", token->value);
3721 parser->error = true;
3723 /* ??? In some Objective-C cases '...' isn't applicable so there
3724 should be a different message. */
3725 else
3726 c_parser_error (parser,
3727 "expected declaration specifiers or %<...%>");
3728 c_parser_skip_to_end_of_parameter (parser);
3729 return NULL;
3731 specs = build_null_declspecs ();
3732 if (attrs)
3734 declspecs_add_attrs (input_location, specs, attrs);
3735 attrs = NULL_TREE;
3737 c_parser_declspecs (parser, specs, true, true, true, true, false,
3738 cla_nonabstract_decl);
3739 finish_declspecs (specs);
3740 pending_xref_error ();
3741 prefix_attrs = specs->attrs;
3742 specs->attrs = NULL_TREE;
3743 declarator = c_parser_declarator (parser,
3744 specs->typespec_kind != ctsk_none,
3745 C_DTR_PARM, &dummy);
3746 if (declarator == NULL)
3748 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3749 return NULL;
3751 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3752 postfix_attrs = c_parser_attributes (parser);
3753 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3754 declarator);
3757 /* Parse a string literal in an asm expression. It should not be
3758 translated, and wide string literals are an error although
3759 permitted by the syntax. This is a GNU extension.
3761 asm-string-literal:
3762 string-literal
3764 ??? At present, following the old parser, the caller needs to have
3765 set lex_untranslated_string to 1. It would be better to follow the
3766 C++ parser rather than using this kludge. */
3768 static tree
3769 c_parser_asm_string_literal (c_parser *parser)
3771 tree str;
3772 int save_flag = warn_overlength_strings;
3773 warn_overlength_strings = 0;
3774 if (c_parser_next_token_is (parser, CPP_STRING))
3776 str = c_parser_peek_token (parser)->value;
3777 c_parser_consume_token (parser);
3779 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3781 error_at (c_parser_peek_token (parser)->location,
3782 "wide string literal in %<asm%>");
3783 str = build_string (1, "");
3784 c_parser_consume_token (parser);
3786 else
3788 c_parser_error (parser, "expected string literal");
3789 str = NULL_TREE;
3791 warn_overlength_strings = save_flag;
3792 return str;
3795 /* Parse a simple asm expression. This is used in restricted
3796 contexts, where a full expression with inputs and outputs does not
3797 make sense. This is a GNU extension.
3799 simple-asm-expr:
3800 asm ( asm-string-literal )
3803 static tree
3804 c_parser_simple_asm_expr (c_parser *parser)
3806 tree str;
3807 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3808 /* ??? Follow the C++ parser rather than using the
3809 lex_untranslated_string kludge. */
3810 parser->lex_untranslated_string = true;
3811 c_parser_consume_token (parser);
3812 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3814 parser->lex_untranslated_string = false;
3815 return NULL_TREE;
3817 str = c_parser_asm_string_literal (parser);
3818 parser->lex_untranslated_string = false;
3819 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3821 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3822 return NULL_TREE;
3824 return str;
3827 static tree
3828 c_parser_attribute_any_word (c_parser *parser)
3830 tree attr_name = NULL_TREE;
3832 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3834 /* ??? See comment above about what keywords are accepted here. */
3835 bool ok;
3836 switch (c_parser_peek_token (parser)->keyword)
3838 case RID_STATIC:
3839 case RID_UNSIGNED:
3840 case RID_LONG:
3841 case RID_CONST:
3842 case RID_EXTERN:
3843 case RID_REGISTER:
3844 case RID_TYPEDEF:
3845 case RID_SHORT:
3846 case RID_INLINE:
3847 case RID_NORETURN:
3848 case RID_VOLATILE:
3849 case RID_SIGNED:
3850 case RID_AUTO:
3851 case RID_RESTRICT:
3852 case RID_COMPLEX:
3853 case RID_THREAD:
3854 case RID_INT:
3855 case RID_CHAR:
3856 case RID_FLOAT:
3857 case RID_DOUBLE:
3858 case RID_VOID:
3859 case RID_DFLOAT32:
3860 case RID_DFLOAT64:
3861 case RID_DFLOAT128:
3862 case RID_BOOL:
3863 case RID_FRACT:
3864 case RID_ACCUM:
3865 case RID_SAT:
3866 case RID_TRANSACTION_ATOMIC:
3867 case RID_TRANSACTION_CANCEL:
3868 case RID_ATOMIC:
3869 case RID_AUTO_TYPE:
3870 case RID_INT_N_0:
3871 case RID_INT_N_1:
3872 case RID_INT_N_2:
3873 case RID_INT_N_3:
3874 ok = true;
3875 break;
3876 default:
3877 ok = false;
3878 break;
3880 if (!ok)
3881 return NULL_TREE;
3883 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
3884 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
3886 else if (c_parser_next_token_is (parser, CPP_NAME))
3887 attr_name = c_parser_peek_token (parser)->value;
3889 return attr_name;
3892 #define CILK_SIMD_FN_CLAUSE_MASK \
3893 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
3894 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
3895 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
3896 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
3897 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
3899 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
3900 VEC_TOKEN is the "vector" token that is replaced with "simd" and
3901 pushed into the token list.
3902 Syntax:
3903 vector
3904 vector (<vector attributes>). */
3906 static void
3907 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
3909 gcc_assert (is_cilkplus_vector_p (vec_token.value));
3911 int paren_scope = 0;
3912 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
3913 /* Consume the "vector" token. */
3914 c_parser_consume_token (parser);
3916 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3918 c_parser_consume_token (parser);
3919 paren_scope++;
3921 while (paren_scope > 0)
3923 c_token *token = c_parser_peek_token (parser);
3924 if (token->type == CPP_OPEN_PAREN)
3925 paren_scope++;
3926 else if (token->type == CPP_CLOSE_PAREN)
3927 paren_scope--;
3928 /* Do not push the last ')' since we are not pushing the '('. */
3929 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
3930 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
3931 c_parser_consume_token (parser);
3934 /* Since we are converting an attribute to a pragma, we need to end the
3935 attribute with PRAGMA_EOL. */
3936 c_token eol_token;
3937 memset (&eol_token, 0, sizeof (eol_token));
3938 eol_token.type = CPP_PRAGMA_EOL;
3939 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
3942 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
3944 static void
3945 c_finish_cilk_simd_fn_tokens (c_parser *parser)
3947 c_token last_token = parser->cilk_simd_fn_tokens->last ();
3949 /* c_parser_attributes is called in several places, so if these EOF
3950 tokens are already inserted, then don't do them again. */
3951 if (last_token.type == CPP_EOF)
3952 return;
3954 /* Two CPP_EOF token are added as a safety net since the normal C
3955 front-end has two token look-ahead. */
3956 c_token eof_token;
3957 eof_token.type = CPP_EOF;
3958 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3959 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
3962 /* Parse (possibly empty) attributes. This is a GNU extension.
3964 attributes:
3965 empty
3966 attributes attribute
3968 attribute:
3969 __attribute__ ( ( attribute-list ) )
3971 attribute-list:
3972 attrib
3973 attribute_list , attrib
3975 attrib:
3976 empty
3977 any-word
3978 any-word ( identifier )
3979 any-word ( identifier , nonempty-expr-list )
3980 any-word ( expr-list )
3982 where the "identifier" must not be declared as a type, and
3983 "any-word" may be any identifier (including one declared as a
3984 type), a reserved word storage class specifier, type specifier or
3985 type qualifier. ??? This still leaves out most reserved keywords
3986 (following the old parser), shouldn't we include them, and why not
3987 allow identifiers declared as types to start the arguments? */
3989 static tree
3990 c_parser_attributes (c_parser *parser)
3992 tree attrs = NULL_TREE;
3993 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3995 /* ??? Follow the C++ parser rather than using the
3996 lex_untranslated_string kludge. */
3997 parser->lex_untranslated_string = true;
3998 /* Consume the `__attribute__' keyword. */
3999 c_parser_consume_token (parser);
4000 /* Look for the two `(' tokens. */
4001 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4003 parser->lex_untranslated_string = false;
4004 return attrs;
4006 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4008 parser->lex_untranslated_string = false;
4009 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4010 return attrs;
4012 /* Parse the attribute list. */
4013 while (c_parser_next_token_is (parser, CPP_COMMA)
4014 || c_parser_next_token_is (parser, CPP_NAME)
4015 || c_parser_next_token_is (parser, CPP_KEYWORD))
4017 tree attr, attr_name, attr_args;
4018 vec<tree, va_gc> *expr_list;
4019 if (c_parser_next_token_is (parser, CPP_COMMA))
4021 c_parser_consume_token (parser);
4022 continue;
4025 attr_name = c_parser_attribute_any_word (parser);
4026 if (attr_name == NULL)
4027 break;
4028 if (is_cilkplus_vector_p (attr_name))
4030 c_token *v_token = c_parser_peek_token (parser);
4031 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4032 /* If the next token isn't a comma, we're done. */
4033 if (!c_parser_next_token_is (parser, CPP_COMMA))
4034 break;
4035 continue;
4037 c_parser_consume_token (parser);
4038 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4040 attr = build_tree_list (attr_name, NULL_TREE);
4041 /* Add this attribute to the list. */
4042 attrs = chainon (attrs, attr);
4043 /* If the next token isn't a comma, we're done. */
4044 if (!c_parser_next_token_is (parser, CPP_COMMA))
4045 break;
4046 continue;
4048 c_parser_consume_token (parser);
4049 /* Parse the attribute contents. If they start with an
4050 identifier which is followed by a comma or close
4051 parenthesis, then the arguments start with that
4052 identifier; otherwise they are an expression list.
4053 In objective-c the identifier may be a classname. */
4054 if (c_parser_next_token_is (parser, CPP_NAME)
4055 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4056 || (c_dialect_objc ()
4057 && c_parser_peek_token (parser)->id_kind
4058 == C_ID_CLASSNAME))
4059 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4060 || (c_parser_peek_2nd_token (parser)->type
4061 == CPP_CLOSE_PAREN))
4062 && (attribute_takes_identifier_p (attr_name)
4063 || (c_dialect_objc ()
4064 && c_parser_peek_token (parser)->id_kind
4065 == C_ID_CLASSNAME)))
4067 tree arg1 = c_parser_peek_token (parser)->value;
4068 c_parser_consume_token (parser);
4069 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4070 attr_args = build_tree_list (NULL_TREE, arg1);
4071 else
4073 tree tree_list;
4074 c_parser_consume_token (parser);
4075 expr_list = c_parser_expr_list (parser, false, true,
4076 NULL, NULL, NULL, NULL);
4077 tree_list = build_tree_list_vec (expr_list);
4078 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4079 release_tree_vector (expr_list);
4082 else
4084 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4085 attr_args = NULL_TREE;
4086 else
4088 expr_list = c_parser_expr_list (parser, false, true,
4089 NULL, NULL, NULL, NULL);
4090 attr_args = build_tree_list_vec (expr_list);
4091 release_tree_vector (expr_list);
4094 attr = build_tree_list (attr_name, attr_args);
4095 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4096 c_parser_consume_token (parser);
4097 else
4099 parser->lex_untranslated_string = false;
4100 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4101 "expected %<)%>");
4102 return attrs;
4104 /* Add this attribute to the list. */
4105 attrs = chainon (attrs, attr);
4106 /* If the next token isn't a comma, we're done. */
4107 if (!c_parser_next_token_is (parser, CPP_COMMA))
4108 break;
4110 /* Look for the two `)' tokens. */
4111 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4112 c_parser_consume_token (parser);
4113 else
4115 parser->lex_untranslated_string = false;
4116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4117 "expected %<)%>");
4118 return attrs;
4120 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4121 c_parser_consume_token (parser);
4122 else
4124 parser->lex_untranslated_string = false;
4125 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4126 "expected %<)%>");
4127 return attrs;
4129 parser->lex_untranslated_string = false;
4132 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4133 c_finish_cilk_simd_fn_tokens (parser);
4134 return attrs;
4137 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4139 type-name:
4140 specifier-qualifier-list abstract-declarator[opt]
4143 static struct c_type_name *
4144 c_parser_type_name (c_parser *parser)
4146 struct c_declspecs *specs = build_null_declspecs ();
4147 struct c_declarator *declarator;
4148 struct c_type_name *ret;
4149 bool dummy = false;
4150 c_parser_declspecs (parser, specs, false, true, true, false, false,
4151 cla_prefer_type);
4152 if (!specs->declspecs_seen_p)
4154 c_parser_error (parser, "expected specifier-qualifier-list");
4155 return NULL;
4157 if (specs->type != error_mark_node)
4159 pending_xref_error ();
4160 finish_declspecs (specs);
4162 declarator = c_parser_declarator (parser,
4163 specs->typespec_kind != ctsk_none,
4164 C_DTR_ABSTRACT, &dummy);
4165 if (declarator == NULL)
4166 return NULL;
4167 ret = XOBNEW (&parser_obstack, struct c_type_name);
4168 ret->specs = specs;
4169 ret->declarator = declarator;
4170 return ret;
4173 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4175 initializer:
4176 assignment-expression
4177 { initializer-list }
4178 { initializer-list , }
4180 initializer-list:
4181 designation[opt] initializer
4182 initializer-list , designation[opt] initializer
4184 designation:
4185 designator-list =
4187 designator-list:
4188 designator
4189 designator-list designator
4191 designator:
4192 array-designator
4193 . identifier
4195 array-designator:
4196 [ constant-expression ]
4198 GNU extensions:
4200 initializer:
4203 designation:
4204 array-designator
4205 identifier :
4207 array-designator:
4208 [ constant-expression ... constant-expression ]
4210 Any expression without commas is accepted in the syntax for the
4211 constant-expressions, with non-constant expressions rejected later.
4213 This function is only used for top-level initializers; for nested
4214 ones, see c_parser_initval. */
4216 static struct c_expr
4217 c_parser_initializer (c_parser *parser)
4219 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4220 return c_parser_braced_init (parser, NULL_TREE, false);
4221 else
4223 struct c_expr ret;
4224 location_t loc = c_parser_peek_token (parser)->location;
4225 ret = c_parser_expr_no_commas (parser, NULL);
4226 if (TREE_CODE (ret.value) != STRING_CST
4227 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4228 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4229 return ret;
4233 /* Parse a braced initializer list. TYPE is the type specified for a
4234 compound literal, and NULL_TREE for other initializers and for
4235 nested braced lists. NESTED_P is true for nested braced lists,
4236 false for the list of a compound literal or the list that is the
4237 top-level initializer in a declaration. */
4239 static struct c_expr
4240 c_parser_braced_init (c_parser *parser, tree type, bool nested_p)
4242 struct c_expr ret;
4243 struct obstack braced_init_obstack;
4244 location_t brace_loc = c_parser_peek_token (parser)->location;
4245 gcc_obstack_init (&braced_init_obstack);
4246 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4247 c_parser_consume_token (parser);
4248 if (nested_p)
4249 push_init_level (brace_loc, 0, &braced_init_obstack);
4250 else
4251 really_start_incremental_init (type);
4252 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4254 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4256 else
4258 /* Parse a non-empty initializer list, possibly with a trailing
4259 comma. */
4260 while (true)
4262 c_parser_initelt (parser, &braced_init_obstack);
4263 if (parser->error)
4264 break;
4265 if (c_parser_next_token_is (parser, CPP_COMMA))
4266 c_parser_consume_token (parser);
4267 else
4268 break;
4269 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4270 break;
4273 c_token *next_tok = c_parser_peek_token (parser);
4274 if (next_tok->type != CPP_CLOSE_BRACE)
4276 ret.value = error_mark_node;
4277 ret.original_code = ERROR_MARK;
4278 ret.original_type = NULL;
4279 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4280 pop_init_level (brace_loc, 0, &braced_init_obstack);
4281 obstack_free (&braced_init_obstack, NULL);
4282 return ret;
4284 location_t close_loc = next_tok->location;
4285 c_parser_consume_token (parser);
4286 ret = pop_init_level (brace_loc, 0, &braced_init_obstack);
4287 obstack_free (&braced_init_obstack, NULL);
4288 set_c_expr_source_range (&ret, brace_loc, close_loc);
4289 return ret;
4292 /* Parse a nested initializer, including designators. */
4294 static void
4295 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4297 /* Parse any designator or designator list. A single array
4298 designator may have the subsequent "=" omitted in GNU C, but a
4299 longer list or a structure member designator may not. */
4300 if (c_parser_next_token_is (parser, CPP_NAME)
4301 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4303 /* Old-style structure member designator. */
4304 set_init_label (c_parser_peek_token (parser)->location,
4305 c_parser_peek_token (parser)->value,
4306 braced_init_obstack);
4307 /* Use the colon as the error location. */
4308 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4309 "obsolete use of designated initializer with %<:%>");
4310 c_parser_consume_token (parser);
4311 c_parser_consume_token (parser);
4313 else
4315 /* des_seen is 0 if there have been no designators, 1 if there
4316 has been a single array designator and 2 otherwise. */
4317 int des_seen = 0;
4318 /* Location of a designator. */
4319 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4320 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4321 || c_parser_next_token_is (parser, CPP_DOT))
4323 int des_prev = des_seen;
4324 if (!des_seen)
4325 des_loc = c_parser_peek_token (parser)->location;
4326 if (des_seen < 2)
4327 des_seen++;
4328 if (c_parser_next_token_is (parser, CPP_DOT))
4330 des_seen = 2;
4331 c_parser_consume_token (parser);
4332 if (c_parser_next_token_is (parser, CPP_NAME))
4334 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4335 braced_init_obstack);
4336 c_parser_consume_token (parser);
4338 else
4340 struct c_expr init;
4341 init.value = error_mark_node;
4342 init.original_code = ERROR_MARK;
4343 init.original_type = NULL;
4344 c_parser_error (parser, "expected identifier");
4345 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4346 process_init_element (input_location, init, false,
4347 braced_init_obstack);
4348 return;
4351 else
4353 tree first, second;
4354 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4355 location_t array_index_loc = UNKNOWN_LOCATION;
4356 /* ??? Following the old parser, [ objc-receiver
4357 objc-message-args ] is accepted as an initializer,
4358 being distinguished from a designator by what follows
4359 the first assignment expression inside the square
4360 brackets, but after a first array designator a
4361 subsequent square bracket is for Objective-C taken to
4362 start an expression, using the obsolete form of
4363 designated initializer without '=', rather than
4364 possibly being a second level of designation: in LALR
4365 terms, the '[' is shifted rather than reducing
4366 designator to designator-list. */
4367 if (des_prev == 1 && c_dialect_objc ())
4369 des_seen = des_prev;
4370 break;
4372 if (des_prev == 0 && c_dialect_objc ())
4374 /* This might be an array designator or an
4375 Objective-C message expression. If the former,
4376 continue parsing here; if the latter, parse the
4377 remainder of the initializer given the starting
4378 primary-expression. ??? It might make sense to
4379 distinguish when des_prev == 1 as well; see
4380 previous comment. */
4381 tree rec, args;
4382 struct c_expr mexpr;
4383 c_parser_consume_token (parser);
4384 if (c_parser_peek_token (parser)->type == CPP_NAME
4385 && ((c_parser_peek_token (parser)->id_kind
4386 == C_ID_TYPENAME)
4387 || (c_parser_peek_token (parser)->id_kind
4388 == C_ID_CLASSNAME)))
4390 /* Type name receiver. */
4391 tree id = c_parser_peek_token (parser)->value;
4392 c_parser_consume_token (parser);
4393 rec = objc_get_class_reference (id);
4394 goto parse_message_args;
4396 first = c_parser_expr_no_commas (parser, NULL).value;
4397 mark_exp_read (first);
4398 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4399 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4400 goto array_desig_after_first;
4401 /* Expression receiver. So far only one part
4402 without commas has been parsed; there might be
4403 more of the expression. */
4404 rec = first;
4405 while (c_parser_next_token_is (parser, CPP_COMMA))
4407 struct c_expr next;
4408 location_t comma_loc, exp_loc;
4409 comma_loc = c_parser_peek_token (parser)->location;
4410 c_parser_consume_token (parser);
4411 exp_loc = c_parser_peek_token (parser)->location;
4412 next = c_parser_expr_no_commas (parser, NULL);
4413 next = convert_lvalue_to_rvalue (exp_loc, next,
4414 true, true);
4415 rec = build_compound_expr (comma_loc, rec, next.value);
4417 parse_message_args:
4418 /* Now parse the objc-message-args. */
4419 args = c_parser_objc_message_args (parser);
4420 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4421 "expected %<]%>");
4422 mexpr.value
4423 = objc_build_message_expr (rec, args);
4424 mexpr.original_code = ERROR_MARK;
4425 mexpr.original_type = NULL;
4426 /* Now parse and process the remainder of the
4427 initializer, starting with this message
4428 expression as a primary-expression. */
4429 c_parser_initval (parser, &mexpr, braced_init_obstack);
4430 return;
4432 c_parser_consume_token (parser);
4433 array_index_loc = c_parser_peek_token (parser)->location;
4434 first = c_parser_expr_no_commas (parser, NULL).value;
4435 mark_exp_read (first);
4436 array_desig_after_first:
4437 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4439 ellipsis_loc = c_parser_peek_token (parser)->location;
4440 c_parser_consume_token (parser);
4441 second = c_parser_expr_no_commas (parser, NULL).value;
4442 mark_exp_read (second);
4444 else
4445 second = NULL_TREE;
4446 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4448 c_parser_consume_token (parser);
4449 set_init_index (array_index_loc, first, second,
4450 braced_init_obstack);
4451 if (second)
4452 pedwarn (ellipsis_loc, OPT_Wpedantic,
4453 "ISO C forbids specifying range of elements to initialize");
4455 else
4456 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4457 "expected %<]%>");
4460 if (des_seen >= 1)
4462 if (c_parser_next_token_is (parser, CPP_EQ))
4464 pedwarn_c90 (des_loc, OPT_Wpedantic,
4465 "ISO C90 forbids specifying subobject "
4466 "to initialize");
4467 c_parser_consume_token (parser);
4469 else
4471 if (des_seen == 1)
4472 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4473 "obsolete use of designated initializer without %<=%>");
4474 else
4476 struct c_expr init;
4477 init.value = error_mark_node;
4478 init.original_code = ERROR_MARK;
4479 init.original_type = NULL;
4480 c_parser_error (parser, "expected %<=%>");
4481 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4482 process_init_element (input_location, init, false,
4483 braced_init_obstack);
4484 return;
4489 c_parser_initval (parser, NULL, braced_init_obstack);
4492 /* Parse a nested initializer; as c_parser_initializer but parses
4493 initializers within braced lists, after any designators have been
4494 applied. If AFTER is not NULL then it is an Objective-C message
4495 expression which is the primary-expression starting the
4496 initializer. */
4498 static void
4499 c_parser_initval (c_parser *parser, struct c_expr *after,
4500 struct obstack * braced_init_obstack)
4502 struct c_expr init;
4503 gcc_assert (!after || c_dialect_objc ());
4504 location_t loc = c_parser_peek_token (parser)->location;
4506 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4507 init = c_parser_braced_init (parser, NULL_TREE, true);
4508 else
4510 init = c_parser_expr_no_commas (parser, after);
4511 if (init.value != NULL_TREE
4512 && TREE_CODE (init.value) != STRING_CST
4513 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4514 init = convert_lvalue_to_rvalue (loc, init, true, true);
4516 process_init_element (loc, init, false, braced_init_obstack);
4519 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4520 C99 6.8.2).
4522 compound-statement:
4523 { block-item-list[opt] }
4524 { label-declarations block-item-list }
4526 block-item-list:
4527 block-item
4528 block-item-list block-item
4530 block-item:
4531 nested-declaration
4532 statement
4534 nested-declaration:
4535 declaration
4537 GNU extensions:
4539 compound-statement:
4540 { label-declarations block-item-list }
4542 nested-declaration:
4543 __extension__ nested-declaration
4544 nested-function-definition
4546 label-declarations:
4547 label-declaration
4548 label-declarations label-declaration
4550 label-declaration:
4551 __label__ identifier-list ;
4553 Allowing the mixing of declarations and code is new in C99. The
4554 GNU syntax also permits (not shown above) labels at the end of
4555 compound statements, which yield an error. We don't allow labels
4556 on declarations; this might seem like a natural extension, but
4557 there would be a conflict between attributes on the label and
4558 prefix attributes on the declaration. ??? The syntax follows the
4559 old parser in requiring something after label declarations.
4560 Although they are erroneous if the labels declared aren't defined,
4561 is it useful for the syntax to be this way?
4563 OpenACC:
4565 block-item:
4566 openacc-directive
4568 openacc-directive:
4569 update-directive
4571 OpenMP:
4573 block-item:
4574 openmp-directive
4576 openmp-directive:
4577 barrier-directive
4578 flush-directive
4579 taskwait-directive
4580 taskyield-directive
4581 cancel-directive
4582 cancellation-point-directive */
4584 static tree
4585 c_parser_compound_statement (c_parser *parser)
4587 tree stmt;
4588 location_t brace_loc;
4589 brace_loc = c_parser_peek_token (parser)->location;
4590 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4592 /* Ensure a scope is entered and left anyway to avoid confusion
4593 if we have just prepared to enter a function body. */
4594 stmt = c_begin_compound_stmt (true);
4595 c_end_compound_stmt (brace_loc, stmt, true);
4596 return error_mark_node;
4598 stmt = c_begin_compound_stmt (true);
4599 c_parser_compound_statement_nostart (parser);
4601 /* If the compound stmt contains array notations, then we expand them. */
4602 if (flag_cilkplus && contains_array_notation_expr (stmt))
4603 stmt = expand_array_notation_exprs (stmt);
4604 return c_end_compound_stmt (brace_loc, stmt, true);
4607 /* Parse a compound statement except for the opening brace. This is
4608 used for parsing both compound statements and statement expressions
4609 (which follow different paths to handling the opening). */
4611 static void
4612 c_parser_compound_statement_nostart (c_parser *parser)
4614 bool last_stmt = false;
4615 bool last_label = false;
4616 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4617 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4618 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4620 c_parser_consume_token (parser);
4621 return;
4623 mark_valid_location_for_stdc_pragma (true);
4624 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4626 /* Read zero or more forward-declarations for labels that nested
4627 functions can jump to. */
4628 mark_valid_location_for_stdc_pragma (false);
4629 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4631 label_loc = c_parser_peek_token (parser)->location;
4632 c_parser_consume_token (parser);
4633 /* Any identifiers, including those declared as type names,
4634 are OK here. */
4635 while (true)
4637 tree label;
4638 if (c_parser_next_token_is_not (parser, CPP_NAME))
4640 c_parser_error (parser, "expected identifier");
4641 break;
4643 label
4644 = declare_label (c_parser_peek_token (parser)->value);
4645 C_DECLARED_LABEL_FLAG (label) = 1;
4646 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4647 c_parser_consume_token (parser);
4648 if (c_parser_next_token_is (parser, CPP_COMMA))
4649 c_parser_consume_token (parser);
4650 else
4651 break;
4653 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4655 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4657 /* We must now have at least one statement, label or declaration. */
4658 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4660 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4661 c_parser_error (parser, "expected declaration or statement");
4662 c_parser_consume_token (parser);
4663 return;
4665 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4667 location_t loc = c_parser_peek_token (parser)->location;
4668 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4669 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4670 || (c_parser_next_token_is (parser, CPP_NAME)
4671 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4673 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4674 label_loc = c_parser_peek_2nd_token (parser)->location;
4675 else
4676 label_loc = c_parser_peek_token (parser)->location;
4677 last_label = true;
4678 last_stmt = false;
4679 mark_valid_location_for_stdc_pragma (false);
4680 c_parser_label (parser);
4682 else if (!last_label
4683 && c_parser_next_tokens_start_declaration (parser))
4685 last_label = false;
4686 mark_valid_location_for_stdc_pragma (false);
4687 c_parser_declaration_or_fndef (parser, true, true, true, true,
4688 true, NULL, vNULL);
4689 if (last_stmt)
4690 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4691 "ISO C90 forbids mixed declarations and code");
4692 last_stmt = false;
4694 else if (!last_label
4695 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4697 /* __extension__ can start a declaration, but is also an
4698 unary operator that can start an expression. Consume all
4699 but the last of a possible series of __extension__ to
4700 determine which. */
4701 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4702 && (c_parser_peek_2nd_token (parser)->keyword
4703 == RID_EXTENSION))
4704 c_parser_consume_token (parser);
4705 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4707 int ext;
4708 ext = disable_extension_diagnostics ();
4709 c_parser_consume_token (parser);
4710 last_label = false;
4711 mark_valid_location_for_stdc_pragma (false);
4712 c_parser_declaration_or_fndef (parser, true, true, true, true,
4713 true, NULL, vNULL);
4714 /* Following the old parser, __extension__ does not
4715 disable this diagnostic. */
4716 restore_extension_diagnostics (ext);
4717 if (last_stmt)
4718 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4719 "ISO C90 forbids mixed declarations and code");
4720 last_stmt = false;
4722 else
4723 goto statement;
4725 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4727 /* External pragmas, and some omp pragmas, are not associated
4728 with regular c code, and so are not to be considered statements
4729 syntactically. This ensures that the user doesn't put them
4730 places that would turn into syntax errors if the directive
4731 were ignored. */
4732 if (c_parser_pragma (parser,
4733 last_label ? pragma_stmt : pragma_compound))
4734 last_label = false, last_stmt = true;
4736 else if (c_parser_next_token_is (parser, CPP_EOF))
4738 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4739 c_parser_error (parser, "expected declaration or statement");
4740 return;
4742 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4744 if (parser->in_if_block)
4746 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4747 error_at (loc, """expected %<}%> before %<else%>");
4748 return;
4750 else
4752 error_at (loc, "%<else%> without a previous %<if%>");
4753 c_parser_consume_token (parser);
4754 continue;
4757 else
4759 statement:
4760 last_label = false;
4761 last_stmt = true;
4762 mark_valid_location_for_stdc_pragma (false);
4763 c_parser_statement_after_labels (parser);
4766 parser->error = false;
4768 if (last_label)
4769 error_at (label_loc, "label at end of compound statement");
4770 c_parser_consume_token (parser);
4771 /* Restore the value we started with. */
4772 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4775 /* Parse all consecutive labels. */
4777 static void
4778 c_parser_all_labels (c_parser *parser)
4780 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4781 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4782 || (c_parser_next_token_is (parser, CPP_NAME)
4783 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4784 c_parser_label (parser);
4787 /* Parse a label (C90 6.6.1, C99 6.8.1).
4789 label:
4790 identifier : attributes[opt]
4791 case constant-expression :
4792 default :
4794 GNU extensions:
4796 label:
4797 case constant-expression ... constant-expression :
4799 The use of attributes on labels is a GNU extension. The syntax in
4800 GNU C accepts any expressions without commas, non-constant
4801 expressions being rejected later. */
4803 static void
4804 c_parser_label (c_parser *parser)
4806 location_t loc1 = c_parser_peek_token (parser)->location;
4807 tree label = NULL_TREE;
4808 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4810 tree exp1, exp2;
4811 c_parser_consume_token (parser);
4812 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4813 if (c_parser_next_token_is (parser, CPP_COLON))
4815 c_parser_consume_token (parser);
4816 label = do_case (loc1, exp1, NULL_TREE);
4818 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4820 c_parser_consume_token (parser);
4821 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4822 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4823 label = do_case (loc1, exp1, exp2);
4825 else
4826 c_parser_error (parser, "expected %<:%> or %<...%>");
4828 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4830 c_parser_consume_token (parser);
4831 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4832 label = do_case (loc1, NULL_TREE, NULL_TREE);
4834 else
4836 tree name = c_parser_peek_token (parser)->value;
4837 tree tlab;
4838 tree attrs;
4839 location_t loc2 = c_parser_peek_token (parser)->location;
4840 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4841 c_parser_consume_token (parser);
4842 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4843 c_parser_consume_token (parser);
4844 attrs = c_parser_attributes (parser);
4845 tlab = define_label (loc2, name);
4846 if (tlab)
4848 decl_attributes (&tlab, attrs, 0);
4849 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
4852 if (label)
4854 if (c_parser_next_tokens_start_declaration (parser))
4856 error_at (c_parser_peek_token (parser)->location,
4857 "a label can only be part of a statement and "
4858 "a declaration is not a statement");
4859 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
4860 /*static_assert_ok*/ true,
4861 /*empty_ok*/ true, /*nested*/ true,
4862 /*start_attr_ok*/ true, NULL,
4863 vNULL);
4868 /* Parse a statement (C90 6.6, C99 6.8).
4870 statement:
4871 labeled-statement
4872 compound-statement
4873 expression-statement
4874 selection-statement
4875 iteration-statement
4876 jump-statement
4878 labeled-statement:
4879 label statement
4881 expression-statement:
4882 expression[opt] ;
4884 selection-statement:
4885 if-statement
4886 switch-statement
4888 iteration-statement:
4889 while-statement
4890 do-statement
4891 for-statement
4893 jump-statement:
4894 goto identifier ;
4895 continue ;
4896 break ;
4897 return expression[opt] ;
4899 GNU extensions:
4901 statement:
4902 asm-statement
4904 jump-statement:
4905 goto * expression ;
4907 Objective-C:
4909 statement:
4910 objc-throw-statement
4911 objc-try-catch-statement
4912 objc-synchronized-statement
4914 objc-throw-statement:
4915 @throw expression ;
4916 @throw ;
4918 OpenACC:
4920 statement:
4921 openacc-construct
4923 openacc-construct:
4924 parallel-construct
4925 kernels-construct
4926 data-construct
4927 loop-construct
4929 parallel-construct:
4930 parallel-directive structured-block
4932 kernels-construct:
4933 kernels-directive structured-block
4935 data-construct:
4936 data-directive structured-block
4938 loop-construct:
4939 loop-directive structured-block
4941 OpenMP:
4943 statement:
4944 openmp-construct
4946 openmp-construct:
4947 parallel-construct
4948 for-construct
4949 simd-construct
4950 for-simd-construct
4951 sections-construct
4952 single-construct
4953 parallel-for-construct
4954 parallel-for-simd-construct
4955 parallel-sections-construct
4956 master-construct
4957 critical-construct
4958 atomic-construct
4959 ordered-construct
4961 parallel-construct:
4962 parallel-directive structured-block
4964 for-construct:
4965 for-directive iteration-statement
4967 simd-construct:
4968 simd-directive iteration-statements
4970 for-simd-construct:
4971 for-simd-directive iteration-statements
4973 sections-construct:
4974 sections-directive section-scope
4976 single-construct:
4977 single-directive structured-block
4979 parallel-for-construct:
4980 parallel-for-directive iteration-statement
4982 parallel-for-simd-construct:
4983 parallel-for-simd-directive iteration-statement
4985 parallel-sections-construct:
4986 parallel-sections-directive section-scope
4988 master-construct:
4989 master-directive structured-block
4991 critical-construct:
4992 critical-directive structured-block
4994 atomic-construct:
4995 atomic-directive expression-statement
4997 ordered-construct:
4998 ordered-directive structured-block
5000 Transactional Memory:
5002 statement:
5003 transaction-statement
5004 transaction-cancel-statement
5007 static void
5008 c_parser_statement (c_parser *parser)
5010 c_parser_all_labels (parser);
5011 c_parser_statement_after_labels (parser);
5014 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5015 of if-else-if conditions. */
5017 static void
5018 c_parser_statement_after_labels (c_parser *parser, vec<tree> *chain)
5020 location_t loc = c_parser_peek_token (parser)->location;
5021 tree stmt = NULL_TREE;
5022 bool in_if_block = parser->in_if_block;
5023 parser->in_if_block = false;
5024 switch (c_parser_peek_token (parser)->type)
5026 case CPP_OPEN_BRACE:
5027 add_stmt (c_parser_compound_statement (parser));
5028 break;
5029 case CPP_KEYWORD:
5030 switch (c_parser_peek_token (parser)->keyword)
5032 case RID_IF:
5033 c_parser_if_statement (parser, chain);
5034 break;
5035 case RID_SWITCH:
5036 c_parser_switch_statement (parser);
5037 break;
5038 case RID_WHILE:
5039 c_parser_while_statement (parser, false);
5040 break;
5041 case RID_DO:
5042 c_parser_do_statement (parser, false);
5043 break;
5044 case RID_FOR:
5045 c_parser_for_statement (parser, false);
5046 break;
5047 case RID_CILK_FOR:
5048 if (!flag_cilkplus)
5050 error_at (c_parser_peek_token (parser)->location,
5051 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5052 c_parser_skip_to_end_of_block_or_statement (parser);
5054 else
5055 c_parser_cilk_for (parser, integer_zero_node);
5056 break;
5057 case RID_CILK_SYNC:
5058 c_parser_consume_token (parser);
5059 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5060 if (!flag_cilkplus)
5061 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5062 else
5063 add_stmt (build_cilk_sync ());
5064 break;
5065 case RID_GOTO:
5066 c_parser_consume_token (parser);
5067 if (c_parser_next_token_is (parser, CPP_NAME))
5069 stmt = c_finish_goto_label (loc,
5070 c_parser_peek_token (parser)->value);
5071 c_parser_consume_token (parser);
5073 else if (c_parser_next_token_is (parser, CPP_MULT))
5075 struct c_expr val;
5077 c_parser_consume_token (parser);
5078 val = c_parser_expression (parser);
5079 if (check_no_cilk (val.value,
5080 "Cilk array notation cannot be used as a computed goto expression",
5081 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5082 loc))
5083 val.value = error_mark_node;
5084 val = convert_lvalue_to_rvalue (loc, val, false, true);
5085 stmt = c_finish_goto_ptr (loc, val.value);
5087 else
5088 c_parser_error (parser, "expected identifier or %<*%>");
5089 goto expect_semicolon;
5090 case RID_CONTINUE:
5091 c_parser_consume_token (parser);
5092 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5093 goto expect_semicolon;
5094 case RID_BREAK:
5095 c_parser_consume_token (parser);
5096 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5097 goto expect_semicolon;
5098 case RID_RETURN:
5099 c_parser_consume_token (parser);
5100 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5102 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5103 c_parser_consume_token (parser);
5105 else
5107 location_t xloc = c_parser_peek_token (parser)->location;
5108 struct c_expr expr = c_parser_expression_conv (parser);
5109 mark_exp_read (expr.value);
5110 stmt = c_finish_return (xloc, expr.value, expr.original_type);
5111 goto expect_semicolon;
5113 break;
5114 case RID_ASM:
5115 stmt = c_parser_asm_statement (parser);
5116 break;
5117 case RID_TRANSACTION_ATOMIC:
5118 case RID_TRANSACTION_RELAXED:
5119 stmt = c_parser_transaction (parser,
5120 c_parser_peek_token (parser)->keyword);
5121 break;
5122 case RID_TRANSACTION_CANCEL:
5123 stmt = c_parser_transaction_cancel (parser);
5124 goto expect_semicolon;
5125 case RID_AT_THROW:
5126 gcc_assert (c_dialect_objc ());
5127 c_parser_consume_token (parser);
5128 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5130 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5131 c_parser_consume_token (parser);
5133 else
5135 struct c_expr expr = c_parser_expression (parser);
5136 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5137 if (check_no_cilk (expr.value,
5138 "Cilk array notation cannot be used for a throw expression",
5139 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5140 expr.value = error_mark_node;
5141 else
5143 expr.value = c_fully_fold (expr.value, false, NULL);
5144 stmt = objc_build_throw_stmt (loc, expr.value);
5146 goto expect_semicolon;
5148 break;
5149 case RID_AT_TRY:
5150 gcc_assert (c_dialect_objc ());
5151 c_parser_objc_try_catch_finally_statement (parser);
5152 break;
5153 case RID_AT_SYNCHRONIZED:
5154 gcc_assert (c_dialect_objc ());
5155 c_parser_objc_synchronized_statement (parser);
5156 break;
5157 default:
5158 goto expr_stmt;
5160 break;
5161 case CPP_SEMICOLON:
5162 c_parser_consume_token (parser);
5163 break;
5164 case CPP_CLOSE_PAREN:
5165 case CPP_CLOSE_SQUARE:
5166 /* Avoid infinite loop in error recovery:
5167 c_parser_skip_until_found stops at a closing nesting
5168 delimiter without consuming it, but here we need to consume
5169 it to proceed further. */
5170 c_parser_error (parser, "expected statement");
5171 c_parser_consume_token (parser);
5172 break;
5173 case CPP_PRAGMA:
5174 c_parser_pragma (parser, pragma_stmt);
5175 break;
5176 default:
5177 expr_stmt:
5178 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5179 expect_semicolon:
5180 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5181 break;
5183 /* Two cases cannot and do not have line numbers associated: If stmt
5184 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5185 cannot hold line numbers. But that's OK because the statement
5186 will either be changed to a MODIFY_EXPR during gimplification of
5187 the statement expr, or discarded. If stmt was compound, but
5188 without new variables, we will have skipped the creation of a
5189 BIND and will have a bare STATEMENT_LIST. But that's OK because
5190 (recursively) all of the component statements should already have
5191 line numbers assigned. ??? Can we discard no-op statements
5192 earlier? */
5193 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5194 protected_set_expr_location (stmt, loc);
5196 parser->in_if_block = in_if_block;
5199 /* Parse the condition from an if, do, while or for statements. */
5201 static tree
5202 c_parser_condition (c_parser *parser)
5204 location_t loc = c_parser_peek_token (parser)->location;
5205 tree cond;
5206 cond = c_parser_expression_conv (parser).value;
5207 cond = c_objc_common_truthvalue_conversion (loc, cond);
5208 cond = c_fully_fold (cond, false, NULL);
5209 if (warn_sequence_point)
5210 verify_sequence_points (cond);
5211 return cond;
5214 /* Parse a parenthesized condition from an if, do or while statement.
5216 condition:
5217 ( expression )
5219 static tree
5220 c_parser_paren_condition (c_parser *parser)
5222 tree cond;
5223 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5224 return error_mark_node;
5225 cond = c_parser_condition (parser);
5226 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5227 return cond;
5230 /* Parse a statement which is a block in C99. */
5232 static tree
5233 c_parser_c99_block_statement (c_parser *parser)
5235 tree block = c_begin_compound_stmt (flag_isoc99);
5236 location_t loc = c_parser_peek_token (parser)->location;
5237 c_parser_statement (parser);
5238 return c_end_compound_stmt (loc, block, flag_isoc99);
5241 /* Parse the body of an if statement. This is just parsing a
5242 statement but (a) it is a block in C99, (b) we track whether the
5243 body is an if statement for the sake of -Wparentheses warnings, (c)
5244 we handle an empty body specially for the sake of -Wempty-body
5245 warnings, and (d) we call parser_compound_statement directly
5246 because c_parser_statement_after_labels resets
5247 parser->in_if_block. */
5249 static tree
5250 c_parser_if_body (c_parser *parser, bool *if_p,
5251 const token_indent_info &if_tinfo)
5253 tree block = c_begin_compound_stmt (flag_isoc99);
5254 location_t body_loc = c_parser_peek_token (parser)->location;
5255 token_indent_info body_tinfo
5256 = get_token_indent_info (c_parser_peek_token (parser));
5258 c_parser_all_labels (parser);
5259 *if_p = c_parser_next_token_is_keyword (parser, RID_IF);
5260 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5262 location_t loc = c_parser_peek_token (parser)->location;
5263 add_stmt (build_empty_stmt (loc));
5264 c_parser_consume_token (parser);
5265 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5266 warning_at (loc, OPT_Wempty_body,
5267 "suggest braces around empty body in an %<if%> statement");
5269 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5270 add_stmt (c_parser_compound_statement (parser));
5271 else
5272 c_parser_statement_after_labels (parser);
5274 token_indent_info next_tinfo
5275 = get_token_indent_info (c_parser_peek_token (parser));
5276 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5278 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5281 /* Parse the else body of an if statement. This is just parsing a
5282 statement but (a) it is a block in C99, (b) we handle an empty body
5283 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5284 of if-else-if conditions. */
5286 static tree
5287 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5288 vec<tree> *chain)
5290 location_t body_loc = c_parser_peek_token (parser)->location;
5291 tree block = c_begin_compound_stmt (flag_isoc99);
5292 token_indent_info body_tinfo
5293 = get_token_indent_info (c_parser_peek_token (parser));
5295 c_parser_all_labels (parser);
5296 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5298 location_t loc = c_parser_peek_token (parser)->location;
5299 warning_at (loc,
5300 OPT_Wempty_body,
5301 "suggest braces around empty body in an %<else%> statement");
5302 add_stmt (build_empty_stmt (loc));
5303 c_parser_consume_token (parser);
5305 else
5306 c_parser_statement_after_labels (parser, chain);
5308 token_indent_info next_tinfo
5309 = get_token_indent_info (c_parser_peek_token (parser));
5310 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5312 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5315 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5317 if-statement:
5318 if ( expression ) statement
5319 if ( expression ) statement else statement
5321 CHAIN is a vector of if-else-if conditions. */
5323 static void
5324 c_parser_if_statement (c_parser *parser, vec<tree> *chain)
5326 tree block;
5327 location_t loc;
5328 tree cond;
5329 bool first_if = false;
5330 tree first_body, second_body;
5331 bool in_if_block;
5332 tree if_stmt;
5334 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5335 token_indent_info if_tinfo
5336 = get_token_indent_info (c_parser_peek_token (parser));
5337 c_parser_consume_token (parser);
5338 block = c_begin_compound_stmt (flag_isoc99);
5339 loc = c_parser_peek_token (parser)->location;
5340 cond = c_parser_paren_condition (parser);
5341 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5343 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5344 cond = error_mark_node;
5346 in_if_block = parser->in_if_block;
5347 parser->in_if_block = true;
5348 first_body = c_parser_if_body (parser, &first_if, if_tinfo);
5349 parser->in_if_block = in_if_block;
5351 if (warn_duplicated_cond)
5352 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5354 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5356 token_indent_info else_tinfo
5357 = get_token_indent_info (c_parser_peek_token (parser));
5358 c_parser_consume_token (parser);
5359 if (warn_duplicated_cond)
5361 if (c_parser_next_token_is_keyword (parser, RID_IF)
5362 && chain == NULL)
5364 /* We've got "if (COND) else if (COND2)". Start the
5365 condition chain and add COND as the first element. */
5366 chain = new vec<tree> ();
5367 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5368 chain->safe_push (cond);
5370 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5372 /* This is if-else without subsequent if. Zap the condition
5373 chain; we would have already warned at this point. */
5374 delete chain;
5375 chain = NULL;
5378 second_body = c_parser_else_body (parser, else_tinfo, chain);
5380 else
5382 second_body = NULL_TREE;
5383 if (warn_duplicated_cond)
5385 /* This if statement does not have an else clause. We don't
5386 need the condition chain anymore. */
5387 delete chain;
5388 chain = NULL;
5391 c_finish_if_stmt (loc, cond, first_body, second_body, first_if);
5392 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5394 /* If the if statement contains array notations, then we expand them. */
5395 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5396 if_stmt = fix_conditional_array_notations (if_stmt);
5397 add_stmt (if_stmt);
5400 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5402 switch-statement:
5403 switch (expression) statement
5406 static void
5407 c_parser_switch_statement (c_parser *parser)
5409 struct c_expr ce;
5410 tree block, expr, body, save_break;
5411 location_t switch_loc = c_parser_peek_token (parser)->location;
5412 location_t switch_cond_loc;
5413 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5414 c_parser_consume_token (parser);
5415 block = c_begin_compound_stmt (flag_isoc99);
5416 bool explicit_cast_p = false;
5417 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5419 switch_cond_loc = c_parser_peek_token (parser)->location;
5420 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5421 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5422 explicit_cast_p = true;
5423 ce = c_parser_expression (parser);
5424 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5425 expr = ce.value;
5426 /* ??? expr has no valid location? */
5427 if (check_no_cilk (expr,
5428 "Cilk array notation cannot be used as a condition for switch statement",
5429 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5430 switch_cond_loc))
5431 expr = error_mark_node;
5432 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5434 else
5436 switch_cond_loc = UNKNOWN_LOCATION;
5437 expr = error_mark_node;
5439 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5440 save_break = c_break_label;
5441 c_break_label = NULL_TREE;
5442 body = c_parser_c99_block_statement (parser);
5443 c_finish_case (body, ce.original_type);
5444 if (c_break_label)
5446 location_t here = c_parser_peek_token (parser)->location;
5447 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5448 SET_EXPR_LOCATION (t, here);
5449 add_stmt (t);
5451 c_break_label = save_break;
5452 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5455 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5457 while-statement:
5458 while (expression) statement
5461 static void
5462 c_parser_while_statement (c_parser *parser, bool ivdep)
5464 tree block, cond, body, save_break, save_cont;
5465 location_t loc;
5466 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5467 token_indent_info while_tinfo
5468 = get_token_indent_info (c_parser_peek_token (parser));
5469 c_parser_consume_token (parser);
5470 block = c_begin_compound_stmt (flag_isoc99);
5471 loc = c_parser_peek_token (parser)->location;
5472 cond = c_parser_paren_condition (parser);
5473 if (check_no_cilk (cond,
5474 "Cilk array notation cannot be used as a condition for while statement",
5475 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5476 cond = error_mark_node;
5477 if (ivdep && cond != error_mark_node)
5478 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5479 build_int_cst (integer_type_node,
5480 annot_expr_ivdep_kind));
5481 save_break = c_break_label;
5482 c_break_label = NULL_TREE;
5483 save_cont = c_cont_label;
5484 c_cont_label = NULL_TREE;
5486 token_indent_info body_tinfo
5487 = get_token_indent_info (c_parser_peek_token (parser));
5489 body = c_parser_c99_block_statement (parser);
5490 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5491 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5493 token_indent_info next_tinfo
5494 = get_token_indent_info (c_parser_peek_token (parser));
5495 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5497 c_break_label = save_break;
5498 c_cont_label = save_cont;
5501 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5503 do-statement:
5504 do statement while ( expression ) ;
5507 static void
5508 c_parser_do_statement (c_parser *parser, bool ivdep)
5510 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5511 location_t loc;
5512 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5513 c_parser_consume_token (parser);
5514 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5515 warning_at (c_parser_peek_token (parser)->location,
5516 OPT_Wempty_body,
5517 "suggest braces around empty body in %<do%> statement");
5518 block = c_begin_compound_stmt (flag_isoc99);
5519 loc = c_parser_peek_token (parser)->location;
5520 save_break = c_break_label;
5521 c_break_label = NULL_TREE;
5522 save_cont = c_cont_label;
5523 c_cont_label = NULL_TREE;
5524 body = c_parser_c99_block_statement (parser);
5525 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5526 new_break = c_break_label;
5527 c_break_label = save_break;
5528 new_cont = c_cont_label;
5529 c_cont_label = save_cont;
5530 cond = c_parser_paren_condition (parser);
5531 if (check_no_cilk (cond,
5532 "Cilk array notation cannot be used as a condition for a do-while statement",
5533 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5534 cond = error_mark_node;
5535 if (ivdep && cond != error_mark_node)
5536 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5537 build_int_cst (integer_type_node,
5538 annot_expr_ivdep_kind));
5539 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5540 c_parser_skip_to_end_of_block_or_statement (parser);
5541 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5542 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5545 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5547 for-statement:
5548 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5549 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5551 The form with a declaration is new in C99.
5553 ??? In accordance with the old parser, the declaration may be a
5554 nested function, which is then rejected in check_for_loop_decls,
5555 but does it make any sense for this to be included in the grammar?
5556 Note in particular that the nested function does not include a
5557 trailing ';', whereas the "declaration" production includes one.
5558 Also, can we reject bad declarations earlier and cheaper than
5559 check_for_loop_decls?
5561 In Objective-C, there are two additional variants:
5563 foreach-statement:
5564 for ( expression in expresssion ) statement
5565 for ( declaration in expression ) statement
5567 This is inconsistent with C, because the second variant is allowed
5568 even if c99 is not enabled.
5570 The rest of the comment documents these Objective-C foreach-statement.
5572 Here is the canonical example of the first variant:
5573 for (object in array) { do something with object }
5574 we call the first expression ("object") the "object_expression" and
5575 the second expression ("array") the "collection_expression".
5576 object_expression must be an lvalue of type "id" (a generic Objective-C
5577 object) because the loop works by assigning to object_expression the
5578 various objects from the collection_expression. collection_expression
5579 must evaluate to something of type "id" which responds to the method
5580 countByEnumeratingWithState:objects:count:.
5582 The canonical example of the second variant is:
5583 for (id object in array) { do something with object }
5584 which is completely equivalent to
5586 id object;
5587 for (object in array) { do something with object }
5589 Note that initizializing 'object' in some way (eg, "for ((object =
5590 xxx) in array) { do something with object }") is possibly
5591 technically valid, but completely pointless as 'object' will be
5592 assigned to something else as soon as the loop starts. We should
5593 most likely reject it (TODO).
5595 The beginning of the Objective-C foreach-statement looks exactly
5596 like the beginning of the for-statement, and we can tell it is a
5597 foreach-statement only because the initial declaration or
5598 expression is terminated by 'in' instead of ';'.
5601 static void
5602 c_parser_for_statement (c_parser *parser, bool ivdep)
5604 tree block, cond, incr, save_break, save_cont, body;
5605 /* The following are only used when parsing an ObjC foreach statement. */
5606 tree object_expression;
5607 /* Silence the bogus uninitialized warning. */
5608 tree collection_expression = NULL;
5609 location_t loc = c_parser_peek_token (parser)->location;
5610 location_t for_loc = c_parser_peek_token (parser)->location;
5611 bool is_foreach_statement = false;
5612 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5613 token_indent_info for_tinfo
5614 = get_token_indent_info (c_parser_peek_token (parser));
5615 c_parser_consume_token (parser);
5616 /* Open a compound statement in Objective-C as well, just in case this is
5617 as foreach expression. */
5618 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5619 cond = error_mark_node;
5620 incr = error_mark_node;
5621 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5623 /* Parse the initialization declaration or expression. */
5624 object_expression = error_mark_node;
5625 parser->objc_could_be_foreach_context = c_dialect_objc ();
5626 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5628 parser->objc_could_be_foreach_context = false;
5629 c_parser_consume_token (parser);
5630 c_finish_expr_stmt (loc, NULL_TREE);
5632 else if (c_parser_next_tokens_start_declaration (parser))
5634 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5635 &object_expression, vNULL);
5636 parser->objc_could_be_foreach_context = false;
5638 if (c_parser_next_token_is_keyword (parser, RID_IN))
5640 c_parser_consume_token (parser);
5641 is_foreach_statement = true;
5642 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5643 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5645 else
5646 check_for_loop_decls (for_loc, flag_isoc99);
5648 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5650 /* __extension__ can start a declaration, but is also an
5651 unary operator that can start an expression. Consume all
5652 but the last of a possible series of __extension__ to
5653 determine which. */
5654 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5655 && (c_parser_peek_2nd_token (parser)->keyword
5656 == RID_EXTENSION))
5657 c_parser_consume_token (parser);
5658 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5660 int ext;
5661 ext = disable_extension_diagnostics ();
5662 c_parser_consume_token (parser);
5663 c_parser_declaration_or_fndef (parser, true, true, true, true,
5664 true, &object_expression, vNULL);
5665 parser->objc_could_be_foreach_context = false;
5667 restore_extension_diagnostics (ext);
5668 if (c_parser_next_token_is_keyword (parser, RID_IN))
5670 c_parser_consume_token (parser);
5671 is_foreach_statement = true;
5672 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5673 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5675 else
5676 check_for_loop_decls (for_loc, flag_isoc99);
5678 else
5679 goto init_expr;
5681 else
5683 init_expr:
5685 struct c_expr ce;
5686 tree init_expression;
5687 ce = c_parser_expression (parser);
5688 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5689 level statement", but it works just fine, so allow it. */
5690 init_expression = ce.value;
5691 parser->objc_could_be_foreach_context = false;
5692 if (c_parser_next_token_is_keyword (parser, RID_IN))
5694 c_parser_consume_token (parser);
5695 is_foreach_statement = true;
5696 if (! lvalue_p (init_expression))
5697 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5698 object_expression = c_fully_fold (init_expression, false, NULL);
5700 else
5702 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5703 init_expression = ce.value;
5704 c_finish_expr_stmt (loc, init_expression);
5705 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5709 /* Parse the loop condition. In the case of a foreach
5710 statement, there is no loop condition. */
5711 gcc_assert (!parser->objc_could_be_foreach_context);
5712 if (!is_foreach_statement)
5714 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5716 if (ivdep)
5718 c_parser_error (parser, "missing loop condition in loop with "
5719 "%<GCC ivdep%> pragma");
5720 cond = error_mark_node;
5722 else
5724 c_parser_consume_token (parser);
5725 cond = NULL_TREE;
5728 else
5730 cond = c_parser_condition (parser);
5731 if (check_no_cilk (cond,
5732 "Cilk array notation cannot be used in a condition for a for-loop",
5733 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
5734 cond = error_mark_node;
5735 c_parser_skip_until_found (parser, CPP_SEMICOLON,
5736 "expected %<;%>");
5738 if (ivdep && cond != error_mark_node)
5739 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5740 build_int_cst (integer_type_node,
5741 annot_expr_ivdep_kind));
5743 /* Parse the increment expression (the third expression in a
5744 for-statement). In the case of a foreach-statement, this is
5745 the expression that follows the 'in'. */
5746 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5748 if (is_foreach_statement)
5750 c_parser_error (parser, "missing collection in fast enumeration");
5751 collection_expression = error_mark_node;
5753 else
5754 incr = c_process_expr_stmt (loc, NULL_TREE);
5756 else
5758 if (is_foreach_statement)
5759 collection_expression = c_fully_fold (c_parser_expression (parser).value,
5760 false, NULL);
5761 else
5763 struct c_expr ce = c_parser_expression (parser);
5764 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5765 incr = c_process_expr_stmt (loc, ce.value);
5768 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5770 save_break = c_break_label;
5771 c_break_label = NULL_TREE;
5772 save_cont = c_cont_label;
5773 c_cont_label = NULL_TREE;
5775 token_indent_info body_tinfo
5776 = get_token_indent_info (c_parser_peek_token (parser));
5778 body = c_parser_c99_block_statement (parser);
5780 if (is_foreach_statement)
5781 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
5782 else
5783 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
5784 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
5786 /* We might need to reclassify any previously-lexed identifier, e.g.
5787 when we've left a for loop with an if-statement without else in the
5788 body - we might have used a wrong scope for the token. See PR67784. */
5789 if (c_parser_next_token_is (parser, CPP_NAME))
5791 c_token *token = c_parser_peek_token (parser);
5792 tree decl = lookup_name (token->value);
5793 if (decl == NULL_TREE || VAR_P (decl))
5794 /* If DECL is null, we don't know what this token might be. Treat
5795 it as an ID for better diagnostics; we'll error later on. */
5796 token->id_kind = C_ID_ID;
5797 else if (TREE_CODE (decl) == TYPE_DECL)
5798 token->id_kind = C_ID_TYPENAME;
5801 token_indent_info next_tinfo
5802 = get_token_indent_info (c_parser_peek_token (parser));
5803 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
5805 c_break_label = save_break;
5806 c_cont_label = save_cont;
5809 /* Parse an asm statement, a GNU extension. This is a full-blown asm
5810 statement with inputs, outputs, clobbers, and volatile tag
5811 allowed.
5813 asm-statement:
5814 asm type-qualifier[opt] ( asm-argument ) ;
5815 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
5817 asm-argument:
5818 asm-string-literal
5819 asm-string-literal : asm-operands[opt]
5820 asm-string-literal : asm-operands[opt] : asm-operands[opt]
5821 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
5823 asm-goto-argument:
5824 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
5825 : asm-goto-operands
5827 Qualifiers other than volatile are accepted in the syntax but
5828 warned for. */
5830 static tree
5831 c_parser_asm_statement (c_parser *parser)
5833 tree quals, str, outputs, inputs, clobbers, labels, ret;
5834 bool simple, is_goto;
5835 location_t asm_loc = c_parser_peek_token (parser)->location;
5836 int section, nsections;
5838 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
5839 c_parser_consume_token (parser);
5840 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
5842 quals = c_parser_peek_token (parser)->value;
5843 c_parser_consume_token (parser);
5845 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
5846 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
5848 warning_at (c_parser_peek_token (parser)->location,
5850 "%E qualifier ignored on asm",
5851 c_parser_peek_token (parser)->value);
5852 quals = NULL_TREE;
5853 c_parser_consume_token (parser);
5855 else
5856 quals = NULL_TREE;
5858 is_goto = false;
5859 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
5861 c_parser_consume_token (parser);
5862 is_goto = true;
5865 /* ??? Follow the C++ parser rather than using the
5866 lex_untranslated_string kludge. */
5867 parser->lex_untranslated_string = true;
5868 ret = NULL;
5870 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5871 goto error;
5873 str = c_parser_asm_string_literal (parser);
5874 if (str == NULL_TREE)
5875 goto error_close_paren;
5877 simple = true;
5878 outputs = NULL_TREE;
5879 inputs = NULL_TREE;
5880 clobbers = NULL_TREE;
5881 labels = NULL_TREE;
5883 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5884 goto done_asm;
5886 /* Parse each colon-delimited section of operands. */
5887 nsections = 3 + is_goto;
5888 for (section = 0; section < nsections; ++section)
5890 if (!c_parser_require (parser, CPP_COLON,
5891 is_goto
5892 ? "expected %<:%>"
5893 : "expected %<:%> or %<)%>"))
5894 goto error_close_paren;
5896 /* Once past any colon, we're no longer a simple asm. */
5897 simple = false;
5899 if ((!c_parser_next_token_is (parser, CPP_COLON)
5900 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
5901 || section == 3)
5902 switch (section)
5904 case 0:
5905 /* For asm goto, we don't allow output operands, but reserve
5906 the slot for a future extension that does allow them. */
5907 if (!is_goto)
5908 outputs = c_parser_asm_operands (parser);
5909 break;
5910 case 1:
5911 inputs = c_parser_asm_operands (parser);
5912 break;
5913 case 2:
5914 clobbers = c_parser_asm_clobbers (parser);
5915 break;
5916 case 3:
5917 labels = c_parser_asm_goto_operands (parser);
5918 break;
5919 default:
5920 gcc_unreachable ();
5923 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
5924 goto done_asm;
5927 done_asm:
5928 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
5930 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5931 goto error;
5934 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5935 c_parser_skip_to_end_of_block_or_statement (parser);
5937 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
5938 clobbers, labels, simple));
5940 error:
5941 parser->lex_untranslated_string = false;
5942 return ret;
5944 error_close_paren:
5945 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
5946 goto error;
5949 /* Parse asm operands, a GNU extension.
5951 asm-operands:
5952 asm-operand
5953 asm-operands , asm-operand
5955 asm-operand:
5956 asm-string-literal ( expression )
5957 [ identifier ] asm-string-literal ( expression )
5960 static tree
5961 c_parser_asm_operands (c_parser *parser)
5963 tree list = NULL_TREE;
5964 while (true)
5966 tree name, str;
5967 struct c_expr expr;
5968 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
5970 c_parser_consume_token (parser);
5971 if (c_parser_next_token_is (parser, CPP_NAME))
5973 tree id = c_parser_peek_token (parser)->value;
5974 c_parser_consume_token (parser);
5975 name = build_string (IDENTIFIER_LENGTH (id),
5976 IDENTIFIER_POINTER (id));
5978 else
5980 c_parser_error (parser, "expected identifier");
5981 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
5982 return NULL_TREE;
5984 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
5985 "expected %<]%>");
5987 else
5988 name = NULL_TREE;
5989 str = c_parser_asm_string_literal (parser);
5990 if (str == NULL_TREE)
5991 return NULL_TREE;
5992 parser->lex_untranslated_string = false;
5993 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5995 parser->lex_untranslated_string = true;
5996 return NULL_TREE;
5998 expr = c_parser_expression (parser);
5999 mark_exp_read (expr.value);
6000 parser->lex_untranslated_string = true;
6001 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6003 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6004 return NULL_TREE;
6006 list = chainon (list, build_tree_list (build_tree_list (name, str),
6007 expr.value));
6008 if (c_parser_next_token_is (parser, CPP_COMMA))
6009 c_parser_consume_token (parser);
6010 else
6011 break;
6013 return list;
6016 /* Parse asm clobbers, a GNU extension.
6018 asm-clobbers:
6019 asm-string-literal
6020 asm-clobbers , asm-string-literal
6023 static tree
6024 c_parser_asm_clobbers (c_parser *parser)
6026 tree list = NULL_TREE;
6027 while (true)
6029 tree str = c_parser_asm_string_literal (parser);
6030 if (str)
6031 list = tree_cons (NULL_TREE, str, list);
6032 else
6033 return NULL_TREE;
6034 if (c_parser_next_token_is (parser, CPP_COMMA))
6035 c_parser_consume_token (parser);
6036 else
6037 break;
6039 return list;
6042 /* Parse asm goto labels, a GNU extension.
6044 asm-goto-operands:
6045 identifier
6046 asm-goto-operands , identifier
6049 static tree
6050 c_parser_asm_goto_operands (c_parser *parser)
6052 tree list = NULL_TREE;
6053 while (true)
6055 tree name, label;
6057 if (c_parser_next_token_is (parser, CPP_NAME))
6059 c_token *tok = c_parser_peek_token (parser);
6060 name = tok->value;
6061 label = lookup_label_for_goto (tok->location, name);
6062 c_parser_consume_token (parser);
6063 TREE_USED (label) = 1;
6065 else
6067 c_parser_error (parser, "expected identifier");
6068 return NULL_TREE;
6071 name = build_string (IDENTIFIER_LENGTH (name),
6072 IDENTIFIER_POINTER (name));
6073 list = tree_cons (name, label, list);
6074 if (c_parser_next_token_is (parser, CPP_COMMA))
6075 c_parser_consume_token (parser);
6076 else
6077 return nreverse (list);
6081 /* Parse an expression other than a compound expression; that is, an
6082 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6083 NULL then it is an Objective-C message expression which is the
6084 primary-expression starting the expression as an initializer.
6086 assignment-expression:
6087 conditional-expression
6088 unary-expression assignment-operator assignment-expression
6090 assignment-operator: one of
6091 = *= /= %= += -= <<= >>= &= ^= |=
6093 In GNU C we accept any conditional expression on the LHS and
6094 diagnose the invalid lvalue rather than producing a syntax
6095 error. */
6097 static struct c_expr
6098 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6099 tree omp_atomic_lhs)
6101 struct c_expr lhs, rhs, ret;
6102 enum tree_code code;
6103 location_t op_location, exp_location;
6104 gcc_assert (!after || c_dialect_objc ());
6105 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6106 op_location = c_parser_peek_token (parser)->location;
6107 switch (c_parser_peek_token (parser)->type)
6109 case CPP_EQ:
6110 code = NOP_EXPR;
6111 break;
6112 case CPP_MULT_EQ:
6113 code = MULT_EXPR;
6114 break;
6115 case CPP_DIV_EQ:
6116 code = TRUNC_DIV_EXPR;
6117 break;
6118 case CPP_MOD_EQ:
6119 code = TRUNC_MOD_EXPR;
6120 break;
6121 case CPP_PLUS_EQ:
6122 code = PLUS_EXPR;
6123 break;
6124 case CPP_MINUS_EQ:
6125 code = MINUS_EXPR;
6126 break;
6127 case CPP_LSHIFT_EQ:
6128 code = LSHIFT_EXPR;
6129 break;
6130 case CPP_RSHIFT_EQ:
6131 code = RSHIFT_EXPR;
6132 break;
6133 case CPP_AND_EQ:
6134 code = BIT_AND_EXPR;
6135 break;
6136 case CPP_XOR_EQ:
6137 code = BIT_XOR_EXPR;
6138 break;
6139 case CPP_OR_EQ:
6140 code = BIT_IOR_EXPR;
6141 break;
6142 default:
6143 return lhs;
6145 c_parser_consume_token (parser);
6146 exp_location = c_parser_peek_token (parser)->location;
6147 rhs = c_parser_expr_no_commas (parser, NULL);
6148 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6150 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6151 code, exp_location, rhs.value,
6152 rhs.original_type);
6153 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6154 if (code == NOP_EXPR)
6155 ret.original_code = MODIFY_EXPR;
6156 else
6158 TREE_NO_WARNING (ret.value) = 1;
6159 ret.original_code = ERROR_MARK;
6161 ret.original_type = NULL;
6162 return ret;
6165 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6166 is not NULL then it is an Objective-C message expression which is
6167 the primary-expression starting the expression as an initializer.
6169 conditional-expression:
6170 logical-OR-expression
6171 logical-OR-expression ? expression : conditional-expression
6173 GNU extensions:
6175 conditional-expression:
6176 logical-OR-expression ? : conditional-expression
6179 static struct c_expr
6180 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6181 tree omp_atomic_lhs)
6183 struct c_expr cond, exp1, exp2, ret;
6184 location_t start, cond_loc, colon_loc, middle_loc;
6186 gcc_assert (!after || c_dialect_objc ());
6188 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6190 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6191 return cond;
6192 if (cond.value != error_mark_node)
6193 start = cond.get_start ();
6194 else
6195 start = UNKNOWN_LOCATION;
6196 cond_loc = c_parser_peek_token (parser)->location;
6197 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6198 c_parser_consume_token (parser);
6199 if (c_parser_next_token_is (parser, CPP_COLON))
6201 tree eptype = NULL_TREE;
6203 middle_loc = c_parser_peek_token (parser)->location;
6204 pedwarn (middle_loc, OPT_Wpedantic,
6205 "ISO C forbids omitting the middle term of a ?: expression");
6206 warn_for_omitted_condop (middle_loc, cond.value);
6207 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6209 eptype = TREE_TYPE (cond.value);
6210 cond.value = TREE_OPERAND (cond.value, 0);
6212 /* Make sure first operand is calculated only once. */
6213 exp1.value = c_save_expr (default_conversion (cond.value));
6214 if (eptype)
6215 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6216 exp1.original_type = NULL;
6217 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6218 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6220 else
6222 cond.value
6223 = c_objc_common_truthvalue_conversion
6224 (cond_loc, default_conversion (cond.value));
6225 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6226 exp1 = c_parser_expression_conv (parser);
6227 mark_exp_read (exp1.value);
6228 c_inhibit_evaluation_warnings +=
6229 ((cond.value == truthvalue_true_node)
6230 - (cond.value == truthvalue_false_node));
6233 colon_loc = c_parser_peek_token (parser)->location;
6234 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6236 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6237 ret.value = error_mark_node;
6238 ret.original_code = ERROR_MARK;
6239 ret.original_type = NULL;
6240 return ret;
6243 location_t exp2_loc = c_parser_peek_token (parser)->location;
6244 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6245 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6247 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6248 ret.value = build_conditional_expr (colon_loc, cond.value,
6249 cond.original_code == C_MAYBE_CONST_EXPR,
6250 exp1.value, exp1.original_type,
6251 exp2.value, exp2.original_type);
6252 ret.original_code = ERROR_MARK;
6253 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6254 ret.original_type = NULL;
6255 else
6257 tree t1, t2;
6259 /* If both sides are enum type, the default conversion will have
6260 made the type of the result be an integer type. We want to
6261 remember the enum types we started with. */
6262 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6263 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6264 ret.original_type = ((t1 != error_mark_node
6265 && t2 != error_mark_node
6266 && (TYPE_MAIN_VARIANT (t1)
6267 == TYPE_MAIN_VARIANT (t2)))
6268 ? t1
6269 : NULL);
6271 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6272 return ret;
6275 /* Parse a binary expression; that is, a logical-OR-expression (C90
6276 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6277 an Objective-C message expression which is the primary-expression
6278 starting the expression as an initializer.
6280 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6281 when it should be the unfolded lhs. In a valid OpenMP source,
6282 one of the operands of the toplevel binary expression must be equal
6283 to it. In that case, just return a build2 created binary operation
6284 rather than result of parser_build_binary_op.
6286 multiplicative-expression:
6287 cast-expression
6288 multiplicative-expression * cast-expression
6289 multiplicative-expression / cast-expression
6290 multiplicative-expression % cast-expression
6292 additive-expression:
6293 multiplicative-expression
6294 additive-expression + multiplicative-expression
6295 additive-expression - multiplicative-expression
6297 shift-expression:
6298 additive-expression
6299 shift-expression << additive-expression
6300 shift-expression >> additive-expression
6302 relational-expression:
6303 shift-expression
6304 relational-expression < shift-expression
6305 relational-expression > shift-expression
6306 relational-expression <= shift-expression
6307 relational-expression >= shift-expression
6309 equality-expression:
6310 relational-expression
6311 equality-expression == relational-expression
6312 equality-expression != relational-expression
6314 AND-expression:
6315 equality-expression
6316 AND-expression & equality-expression
6318 exclusive-OR-expression:
6319 AND-expression
6320 exclusive-OR-expression ^ AND-expression
6322 inclusive-OR-expression:
6323 exclusive-OR-expression
6324 inclusive-OR-expression | exclusive-OR-expression
6326 logical-AND-expression:
6327 inclusive-OR-expression
6328 logical-AND-expression && inclusive-OR-expression
6330 logical-OR-expression:
6331 logical-AND-expression
6332 logical-OR-expression || logical-AND-expression
6335 static struct c_expr
6336 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6337 tree omp_atomic_lhs)
6339 /* A binary expression is parsed using operator-precedence parsing,
6340 with the operands being cast expressions. All the binary
6341 operators are left-associative. Thus a binary expression is of
6342 form:
6344 E0 op1 E1 op2 E2 ...
6346 which we represent on a stack. On the stack, the precedence
6347 levels are strictly increasing. When a new operator is
6348 encountered of higher precedence than that at the top of the
6349 stack, it is pushed; its LHS is the top expression, and its RHS
6350 is everything parsed until it is popped. When a new operator is
6351 encountered with precedence less than or equal to that at the top
6352 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6353 by the result of the operation until the operator at the top of
6354 the stack has lower precedence than the new operator or there is
6355 only one element on the stack; then the top expression is the LHS
6356 of the new operator. In the case of logical AND and OR
6357 expressions, we also need to adjust c_inhibit_evaluation_warnings
6358 as appropriate when the operators are pushed and popped. */
6360 struct {
6361 /* The expression at this stack level. */
6362 struct c_expr expr;
6363 /* The precedence of the operator on its left, PREC_NONE at the
6364 bottom of the stack. */
6365 enum c_parser_prec prec;
6366 /* The operation on its left. */
6367 enum tree_code op;
6368 /* The source location of this operation. */
6369 location_t loc;
6370 } stack[NUM_PRECS];
6371 int sp;
6372 /* Location of the binary operator. */
6373 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6374 #define POP \
6375 do { \
6376 switch (stack[sp].op) \
6378 case TRUTH_ANDIF_EXPR: \
6379 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6380 == truthvalue_false_node); \
6381 break; \
6382 case TRUTH_ORIF_EXPR: \
6383 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6384 == truthvalue_true_node); \
6385 break; \
6386 default: \
6387 break; \
6389 stack[sp - 1].expr \
6390 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6391 stack[sp - 1].expr, true, true); \
6392 stack[sp].expr \
6393 = convert_lvalue_to_rvalue (stack[sp].loc, \
6394 stack[sp].expr, true, true); \
6395 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6396 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6397 && ((1 << stack[sp].prec) \
6398 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6399 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6400 && stack[sp].op != TRUNC_MOD_EXPR \
6401 && stack[0].expr.value != error_mark_node \
6402 && stack[1].expr.value != error_mark_node \
6403 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6404 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6405 stack[0].expr.value \
6406 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6407 stack[0].expr.value, stack[1].expr.value); \
6408 else \
6409 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6410 stack[sp].op, \
6411 stack[sp - 1].expr, \
6412 stack[sp].expr); \
6413 sp--; \
6414 } while (0)
6415 gcc_assert (!after || c_dialect_objc ());
6416 stack[0].loc = c_parser_peek_token (parser)->location;
6417 stack[0].expr = c_parser_cast_expression (parser, after);
6418 stack[0].prec = PREC_NONE;
6419 sp = 0;
6420 while (true)
6422 enum c_parser_prec oprec;
6423 enum tree_code ocode;
6424 source_range src_range;
6425 if (parser->error)
6426 goto out;
6427 switch (c_parser_peek_token (parser)->type)
6429 case CPP_MULT:
6430 oprec = PREC_MULT;
6431 ocode = MULT_EXPR;
6432 break;
6433 case CPP_DIV:
6434 oprec = PREC_MULT;
6435 ocode = TRUNC_DIV_EXPR;
6436 break;
6437 case CPP_MOD:
6438 oprec = PREC_MULT;
6439 ocode = TRUNC_MOD_EXPR;
6440 break;
6441 case CPP_PLUS:
6442 oprec = PREC_ADD;
6443 ocode = PLUS_EXPR;
6444 break;
6445 case CPP_MINUS:
6446 oprec = PREC_ADD;
6447 ocode = MINUS_EXPR;
6448 break;
6449 case CPP_LSHIFT:
6450 oprec = PREC_SHIFT;
6451 ocode = LSHIFT_EXPR;
6452 break;
6453 case CPP_RSHIFT:
6454 oprec = PREC_SHIFT;
6455 ocode = RSHIFT_EXPR;
6456 break;
6457 case CPP_LESS:
6458 oprec = PREC_REL;
6459 ocode = LT_EXPR;
6460 break;
6461 case CPP_GREATER:
6462 oprec = PREC_REL;
6463 ocode = GT_EXPR;
6464 break;
6465 case CPP_LESS_EQ:
6466 oprec = PREC_REL;
6467 ocode = LE_EXPR;
6468 break;
6469 case CPP_GREATER_EQ:
6470 oprec = PREC_REL;
6471 ocode = GE_EXPR;
6472 break;
6473 case CPP_EQ_EQ:
6474 oprec = PREC_EQ;
6475 ocode = EQ_EXPR;
6476 break;
6477 case CPP_NOT_EQ:
6478 oprec = PREC_EQ;
6479 ocode = NE_EXPR;
6480 break;
6481 case CPP_AND:
6482 oprec = PREC_BITAND;
6483 ocode = BIT_AND_EXPR;
6484 break;
6485 case CPP_XOR:
6486 oprec = PREC_BITXOR;
6487 ocode = BIT_XOR_EXPR;
6488 break;
6489 case CPP_OR:
6490 oprec = PREC_BITOR;
6491 ocode = BIT_IOR_EXPR;
6492 break;
6493 case CPP_AND_AND:
6494 oprec = PREC_LOGAND;
6495 ocode = TRUTH_ANDIF_EXPR;
6496 break;
6497 case CPP_OR_OR:
6498 oprec = PREC_LOGOR;
6499 ocode = TRUTH_ORIF_EXPR;
6500 break;
6501 default:
6502 /* Not a binary operator, so end of the binary
6503 expression. */
6504 goto out;
6506 binary_loc = c_parser_peek_token (parser)->location;
6507 while (oprec <= stack[sp].prec)
6508 POP;
6509 c_parser_consume_token (parser);
6510 switch (ocode)
6512 case TRUTH_ANDIF_EXPR:
6513 src_range = stack[sp].expr.src_range;
6514 stack[sp].expr
6515 = convert_lvalue_to_rvalue (stack[sp].loc,
6516 stack[sp].expr, true, true);
6517 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6518 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6519 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6520 == truthvalue_false_node);
6521 set_c_expr_source_range (&stack[sp].expr, src_range);
6522 break;
6523 case TRUTH_ORIF_EXPR:
6524 src_range = stack[sp].expr.src_range;
6525 stack[sp].expr
6526 = convert_lvalue_to_rvalue (stack[sp].loc,
6527 stack[sp].expr, true, true);
6528 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6529 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6530 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6531 == truthvalue_true_node);
6532 set_c_expr_source_range (&stack[sp].expr, src_range);
6533 break;
6534 default:
6535 break;
6537 sp++;
6538 stack[sp].loc = binary_loc;
6539 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6540 stack[sp].prec = oprec;
6541 stack[sp].op = ocode;
6543 out:
6544 while (sp > 0)
6545 POP;
6546 return stack[0].expr;
6547 #undef POP
6550 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6551 NULL then it is an Objective-C message expression which is the
6552 primary-expression starting the expression as an initializer.
6554 cast-expression:
6555 unary-expression
6556 ( type-name ) unary-expression
6559 static struct c_expr
6560 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6562 location_t cast_loc = c_parser_peek_token (parser)->location;
6563 gcc_assert (!after || c_dialect_objc ());
6564 if (after)
6565 return c_parser_postfix_expression_after_primary (parser,
6566 cast_loc, *after);
6567 /* If the expression begins with a parenthesized type name, it may
6568 be either a cast or a compound literal; we need to see whether
6569 the next character is '{' to tell the difference. If not, it is
6570 an unary expression. Full detection of unknown typenames here
6571 would require a 3-token lookahead. */
6572 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6573 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6575 struct c_type_name *type_name;
6576 struct c_expr ret;
6577 struct c_expr expr;
6578 c_parser_consume_token (parser);
6579 type_name = c_parser_type_name (parser);
6580 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6581 if (type_name == NULL)
6583 ret.value = error_mark_node;
6584 ret.original_code = ERROR_MARK;
6585 ret.original_type = NULL;
6586 return ret;
6589 /* Save casted types in the function's used types hash table. */
6590 used_types_insert (type_name->specs->type);
6592 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6593 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6594 cast_loc);
6596 location_t expr_loc = c_parser_peek_token (parser)->location;
6597 expr = c_parser_cast_expression (parser, NULL);
6598 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6600 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6601 if (ret.value && expr.value)
6602 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6603 ret.original_code = ERROR_MARK;
6604 ret.original_type = NULL;
6605 return ret;
6607 else
6608 return c_parser_unary_expression (parser);
6611 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6613 unary-expression:
6614 postfix-expression
6615 ++ unary-expression
6616 -- unary-expression
6617 unary-operator cast-expression
6618 sizeof unary-expression
6619 sizeof ( type-name )
6621 unary-operator: one of
6622 & * + - ~ !
6624 GNU extensions:
6626 unary-expression:
6627 __alignof__ unary-expression
6628 __alignof__ ( type-name )
6629 && identifier
6631 (C11 permits _Alignof with type names only.)
6633 unary-operator: one of
6634 __extension__ __real__ __imag__
6636 Transactional Memory:
6638 unary-expression:
6639 transaction-expression
6641 In addition, the GNU syntax treats ++ and -- as unary operators, so
6642 they may be applied to cast expressions with errors for non-lvalues
6643 given later. */
6645 static struct c_expr
6646 c_parser_unary_expression (c_parser *parser)
6648 int ext;
6649 struct c_expr ret, op;
6650 location_t op_loc = c_parser_peek_token (parser)->location;
6651 location_t exp_loc;
6652 location_t finish;
6653 ret.original_code = ERROR_MARK;
6654 ret.original_type = NULL;
6655 switch (c_parser_peek_token (parser)->type)
6657 case CPP_PLUS_PLUS:
6658 c_parser_consume_token (parser);
6659 exp_loc = c_parser_peek_token (parser)->location;
6660 op = c_parser_cast_expression (parser, NULL);
6662 /* If there is array notations in op, we expand them. */
6663 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6664 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6665 else
6667 op = default_function_array_read_conversion (exp_loc, op);
6668 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6670 case CPP_MINUS_MINUS:
6671 c_parser_consume_token (parser);
6672 exp_loc = c_parser_peek_token (parser)->location;
6673 op = c_parser_cast_expression (parser, NULL);
6675 /* If there is array notations in op, we expand them. */
6676 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6677 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6678 else
6680 op = default_function_array_read_conversion (exp_loc, op);
6681 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6683 case CPP_AND:
6684 c_parser_consume_token (parser);
6685 op = c_parser_cast_expression (parser, NULL);
6686 mark_exp_read (op.value);
6687 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6688 case CPP_MULT:
6689 c_parser_consume_token (parser);
6690 exp_loc = c_parser_peek_token (parser)->location;
6691 op = c_parser_cast_expression (parser, NULL);
6692 finish = op.get_finish ();
6693 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6694 ret.value = build_indirect_ref (op_loc, op.value, RO_UNARY_STAR);
6695 set_c_expr_source_range (&ret, op_loc, finish);
6696 return ret;
6697 case CPP_PLUS:
6698 if (!c_dialect_objc () && !in_system_header_at (input_location))
6699 warning_at (op_loc,
6700 OPT_Wtraditional,
6701 "traditional C rejects the unary plus operator");
6702 c_parser_consume_token (parser);
6703 exp_loc = c_parser_peek_token (parser)->location;
6704 op = c_parser_cast_expression (parser, NULL);
6705 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6706 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6707 case CPP_MINUS:
6708 c_parser_consume_token (parser);
6709 exp_loc = c_parser_peek_token (parser)->location;
6710 op = c_parser_cast_expression (parser, NULL);
6711 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6712 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6713 case CPP_COMPL:
6714 c_parser_consume_token (parser);
6715 exp_loc = c_parser_peek_token (parser)->location;
6716 op = c_parser_cast_expression (parser, NULL);
6717 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6718 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6719 case CPP_NOT:
6720 c_parser_consume_token (parser);
6721 exp_loc = c_parser_peek_token (parser)->location;
6722 op = c_parser_cast_expression (parser, NULL);
6723 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6724 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
6725 case CPP_AND_AND:
6726 /* Refer to the address of a label as a pointer. */
6727 c_parser_consume_token (parser);
6728 if (c_parser_next_token_is (parser, CPP_NAME))
6730 ret.value = finish_label_address_expr
6731 (c_parser_peek_token (parser)->value, op_loc);
6732 set_c_expr_source_range (&ret, op_loc,
6733 c_parser_peek_token (parser)->get_finish ());
6734 c_parser_consume_token (parser);
6736 else
6738 c_parser_error (parser, "expected identifier");
6739 ret.value = error_mark_node;
6741 return ret;
6742 case CPP_KEYWORD:
6743 switch (c_parser_peek_token (parser)->keyword)
6745 case RID_SIZEOF:
6746 return c_parser_sizeof_expression (parser);
6747 case RID_ALIGNOF:
6748 return c_parser_alignof_expression (parser);
6749 case RID_EXTENSION:
6750 c_parser_consume_token (parser);
6751 ext = disable_extension_diagnostics ();
6752 ret = c_parser_cast_expression (parser, NULL);
6753 restore_extension_diagnostics (ext);
6754 return ret;
6755 case RID_REALPART:
6756 c_parser_consume_token (parser);
6757 exp_loc = c_parser_peek_token (parser)->location;
6758 op = c_parser_cast_expression (parser, NULL);
6759 op = default_function_array_conversion (exp_loc, op);
6760 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
6761 case RID_IMAGPART:
6762 c_parser_consume_token (parser);
6763 exp_loc = c_parser_peek_token (parser)->location;
6764 op = c_parser_cast_expression (parser, NULL);
6765 op = default_function_array_conversion (exp_loc, op);
6766 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
6767 case RID_TRANSACTION_ATOMIC:
6768 case RID_TRANSACTION_RELAXED:
6769 return c_parser_transaction_expression (parser,
6770 c_parser_peek_token (parser)->keyword);
6771 default:
6772 return c_parser_postfix_expression (parser);
6774 default:
6775 return c_parser_postfix_expression (parser);
6779 /* Parse a sizeof expression. */
6781 static struct c_expr
6782 c_parser_sizeof_expression (c_parser *parser)
6784 struct c_expr expr;
6785 struct c_expr result;
6786 location_t expr_loc;
6787 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
6789 location_t start;
6790 location_t finish = UNKNOWN_LOCATION;
6792 start = c_parser_peek_token (parser)->location;
6794 c_parser_consume_token (parser);
6795 c_inhibit_evaluation_warnings++;
6796 in_sizeof++;
6797 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6798 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6800 /* Either sizeof ( type-name ) or sizeof unary-expression
6801 starting with a compound literal. */
6802 struct c_type_name *type_name;
6803 c_parser_consume_token (parser);
6804 expr_loc = c_parser_peek_token (parser)->location;
6805 type_name = c_parser_type_name (parser);
6806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6807 finish = parser->tokens_buf[0].location;
6808 if (type_name == NULL)
6810 struct c_expr ret;
6811 c_inhibit_evaluation_warnings--;
6812 in_sizeof--;
6813 ret.value = error_mark_node;
6814 ret.original_code = ERROR_MARK;
6815 ret.original_type = NULL;
6816 return ret;
6818 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6820 expr = c_parser_postfix_expression_after_paren_type (parser,
6821 type_name,
6822 expr_loc);
6823 finish = expr.get_finish ();
6824 goto sizeof_expr;
6826 /* sizeof ( type-name ). */
6827 c_inhibit_evaluation_warnings--;
6828 in_sizeof--;
6829 result = c_expr_sizeof_type (expr_loc, type_name);
6831 else
6833 expr_loc = c_parser_peek_token (parser)->location;
6834 expr = c_parser_unary_expression (parser);
6835 finish = expr.get_finish ();
6836 sizeof_expr:
6837 c_inhibit_evaluation_warnings--;
6838 in_sizeof--;
6839 mark_exp_read (expr.value);
6840 if (TREE_CODE (expr.value) == COMPONENT_REF
6841 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
6842 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
6843 result = c_expr_sizeof_expr (expr_loc, expr);
6845 if (finish != UNKNOWN_LOCATION)
6846 set_c_expr_source_range (&result, start, finish);
6847 return result;
6850 /* Parse an alignof expression. */
6852 static struct c_expr
6853 c_parser_alignof_expression (c_parser *parser)
6855 struct c_expr expr;
6856 location_t loc = c_parser_peek_token (parser)->location;
6857 tree alignof_spelling = c_parser_peek_token (parser)->value;
6858 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
6859 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
6860 "_Alignof") == 0;
6861 /* A diagnostic is not required for the use of this identifier in
6862 the implementation namespace; only diagnose it for the C11
6863 spelling because of existing code using the other spellings. */
6864 if (is_c11_alignof)
6866 if (flag_isoc99)
6867 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C99 does not support %qE",
6868 alignof_spelling);
6869 else
6870 pedwarn_c99 (loc, OPT_Wpedantic, "ISO C90 does not support %qE",
6871 alignof_spelling);
6873 c_parser_consume_token (parser);
6874 c_inhibit_evaluation_warnings++;
6875 in_alignof++;
6876 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6877 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6879 /* Either __alignof__ ( type-name ) or __alignof__
6880 unary-expression starting with a compound literal. */
6881 location_t loc;
6882 struct c_type_name *type_name;
6883 struct c_expr ret;
6884 c_parser_consume_token (parser);
6885 loc = c_parser_peek_token (parser)->location;
6886 type_name = c_parser_type_name (parser);
6887 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6888 if (type_name == NULL)
6890 struct c_expr ret;
6891 c_inhibit_evaluation_warnings--;
6892 in_alignof--;
6893 ret.value = error_mark_node;
6894 ret.original_code = ERROR_MARK;
6895 ret.original_type = NULL;
6896 return ret;
6898 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6900 expr = c_parser_postfix_expression_after_paren_type (parser,
6901 type_name,
6902 loc);
6903 goto alignof_expr;
6905 /* alignof ( type-name ). */
6906 c_inhibit_evaluation_warnings--;
6907 in_alignof--;
6908 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
6909 NULL, NULL),
6910 false, is_c11_alignof, 1);
6911 ret.original_code = ERROR_MARK;
6912 ret.original_type = NULL;
6913 return ret;
6915 else
6917 struct c_expr ret;
6918 expr = c_parser_unary_expression (parser);
6919 alignof_expr:
6920 mark_exp_read (expr.value);
6921 c_inhibit_evaluation_warnings--;
6922 in_alignof--;
6923 pedwarn (loc, OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
6924 alignof_spelling);
6925 ret.value = c_alignof_expr (loc, expr.value);
6926 ret.original_code = ERROR_MARK;
6927 ret.original_type = NULL;
6928 return ret;
6932 /* Helper function to read arguments of builtins which are interfaces
6933 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
6934 others. The name of the builtin is passed using BNAME parameter.
6935 Function returns true if there were no errors while parsing and
6936 stores the arguments in CEXPR_LIST. */
6937 static bool
6938 c_parser_get_builtin_args (c_parser *parser, const char *bname,
6939 vec<c_expr_t, va_gc> **ret_cexpr_list,
6940 bool choose_expr_p)
6942 location_t loc = c_parser_peek_token (parser)->location;
6943 vec<c_expr_t, va_gc> *cexpr_list;
6944 c_expr_t expr;
6945 bool saved_force_folding_builtin_constant_p;
6947 *ret_cexpr_list = NULL;
6948 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
6950 error_at (loc, "cannot take address of %qs", bname);
6951 return false;
6954 c_parser_consume_token (parser);
6956 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6958 c_parser_consume_token (parser);
6959 return true;
6962 saved_force_folding_builtin_constant_p
6963 = force_folding_builtin_constant_p;
6964 force_folding_builtin_constant_p |= choose_expr_p;
6965 expr = c_parser_expr_no_commas (parser, NULL);
6966 force_folding_builtin_constant_p
6967 = saved_force_folding_builtin_constant_p;
6968 vec_alloc (cexpr_list, 1);
6969 vec_safe_push (cexpr_list, expr);
6970 while (c_parser_next_token_is (parser, CPP_COMMA))
6972 c_parser_consume_token (parser);
6973 expr = c_parser_expr_no_commas (parser, NULL);
6974 vec_safe_push (cexpr_list, expr);
6977 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6978 return false;
6980 *ret_cexpr_list = cexpr_list;
6981 return true;
6984 /* This represents a single generic-association. */
6986 struct c_generic_association
6988 /* The location of the starting token of the type. */
6989 location_t type_location;
6990 /* The association's type, or NULL_TREE for 'default'. */
6991 tree type;
6992 /* The association's expression. */
6993 struct c_expr expression;
6996 /* Parse a generic-selection. (C11 6.5.1.1).
6998 generic-selection:
6999 _Generic ( assignment-expression , generic-assoc-list )
7001 generic-assoc-list:
7002 generic-association
7003 generic-assoc-list , generic-association
7005 generic-association:
7006 type-name : assignment-expression
7007 default : assignment-expression
7010 static struct c_expr
7011 c_parser_generic_selection (c_parser *parser)
7013 vec<c_generic_association> associations = vNULL;
7014 struct c_expr selector, error_expr;
7015 tree selector_type;
7016 struct c_generic_association matched_assoc;
7017 bool match_found = false;
7018 location_t generic_loc, selector_loc;
7020 error_expr.original_code = ERROR_MARK;
7021 error_expr.original_type = NULL;
7022 error_expr.value = error_mark_node;
7023 matched_assoc.type_location = UNKNOWN_LOCATION;
7024 matched_assoc.type = NULL_TREE;
7025 matched_assoc.expression = error_expr;
7027 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7028 generic_loc = c_parser_peek_token (parser)->location;
7029 c_parser_consume_token (parser);
7030 if (flag_isoc99)
7031 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7032 "ISO C99 does not support %<_Generic%>");
7033 else
7034 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7035 "ISO C90 does not support %<_Generic%>");
7037 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7038 return error_expr;
7040 c_inhibit_evaluation_warnings++;
7041 selector_loc = c_parser_peek_token (parser)->location;
7042 selector = c_parser_expr_no_commas (parser, NULL);
7043 selector = default_function_array_conversion (selector_loc, selector);
7044 c_inhibit_evaluation_warnings--;
7046 if (selector.value == error_mark_node)
7048 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7049 return selector;
7051 selector_type = TREE_TYPE (selector.value);
7052 /* In ISO C terms, rvalues (including the controlling expression of
7053 _Generic) do not have qualified types. */
7054 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7055 selector_type = TYPE_MAIN_VARIANT (selector_type);
7056 /* In ISO C terms, _Noreturn is not part of the type of expressions
7057 such as &abort, but in GCC it is represented internally as a type
7058 qualifier. */
7059 if (FUNCTION_POINTER_TYPE_P (selector_type)
7060 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7061 selector_type
7062 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7064 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7066 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7067 return error_expr;
7070 while (1)
7072 struct c_generic_association assoc, *iter;
7073 unsigned int ix;
7074 c_token *token = c_parser_peek_token (parser);
7076 assoc.type_location = token->location;
7077 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7079 c_parser_consume_token (parser);
7080 assoc.type = NULL_TREE;
7082 else
7084 struct c_type_name *type_name;
7086 type_name = c_parser_type_name (parser);
7087 if (type_name == NULL)
7089 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7090 goto error_exit;
7092 assoc.type = groktypename (type_name, NULL, NULL);
7093 if (assoc.type == error_mark_node)
7095 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7096 goto error_exit;
7099 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7100 error_at (assoc.type_location,
7101 "%<_Generic%> association has function type");
7102 else if (!COMPLETE_TYPE_P (assoc.type))
7103 error_at (assoc.type_location,
7104 "%<_Generic%> association has incomplete type");
7106 if (variably_modified_type_p (assoc.type, NULL_TREE))
7107 error_at (assoc.type_location,
7108 "%<_Generic%> association has "
7109 "variable length type");
7112 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7114 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7115 goto error_exit;
7118 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7119 if (assoc.expression.value == error_mark_node)
7121 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7122 goto error_exit;
7125 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7127 if (assoc.type == NULL_TREE)
7129 if (iter->type == NULL_TREE)
7131 error_at (assoc.type_location,
7132 "duplicate %<default%> case in %<_Generic%>");
7133 inform (iter->type_location, "original %<default%> is here");
7136 else if (iter->type != NULL_TREE)
7138 if (comptypes (assoc.type, iter->type))
7140 error_at (assoc.type_location,
7141 "%<_Generic%> specifies two compatible types");
7142 inform (iter->type_location, "compatible type is here");
7147 if (assoc.type == NULL_TREE)
7149 if (!match_found)
7151 matched_assoc = assoc;
7152 match_found = true;
7155 else if (comptypes (assoc.type, selector_type))
7157 if (!match_found || matched_assoc.type == NULL_TREE)
7159 matched_assoc = assoc;
7160 match_found = true;
7162 else
7164 error_at (assoc.type_location,
7165 "%<_Generic> selector matches multiple associations");
7166 inform (matched_assoc.type_location,
7167 "other match is here");
7171 associations.safe_push (assoc);
7173 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7174 break;
7175 c_parser_consume_token (parser);
7178 associations.release ();
7180 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7182 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7183 return error_expr;
7186 if (!match_found)
7188 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7189 "compatible with any association",
7190 selector_type);
7191 return error_expr;
7194 return matched_assoc.expression;
7196 error_exit:
7197 associations.release ();
7198 return error_expr;
7201 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7203 postfix-expression:
7204 primary-expression
7205 postfix-expression [ expression ]
7206 postfix-expression ( argument-expression-list[opt] )
7207 postfix-expression . identifier
7208 postfix-expression -> identifier
7209 postfix-expression ++
7210 postfix-expression --
7211 ( type-name ) { initializer-list }
7212 ( type-name ) { initializer-list , }
7214 argument-expression-list:
7215 argument-expression
7216 argument-expression-list , argument-expression
7218 primary-expression:
7219 identifier
7220 constant
7221 string-literal
7222 ( expression )
7223 generic-selection
7225 GNU extensions:
7227 primary-expression:
7228 __func__
7229 (treated as a keyword in GNU C)
7230 __FUNCTION__
7231 __PRETTY_FUNCTION__
7232 ( compound-statement )
7233 __builtin_va_arg ( assignment-expression , type-name )
7234 __builtin_offsetof ( type-name , offsetof-member-designator )
7235 __builtin_choose_expr ( assignment-expression ,
7236 assignment-expression ,
7237 assignment-expression )
7238 __builtin_types_compatible_p ( type-name , type-name )
7239 __builtin_complex ( assignment-expression , assignment-expression )
7240 __builtin_shuffle ( assignment-expression , assignment-expression )
7241 __builtin_shuffle ( assignment-expression ,
7242 assignment-expression ,
7243 assignment-expression, )
7245 offsetof-member-designator:
7246 identifier
7247 offsetof-member-designator . identifier
7248 offsetof-member-designator [ expression ]
7250 Objective-C:
7252 primary-expression:
7253 [ objc-receiver objc-message-args ]
7254 @selector ( objc-selector-arg )
7255 @protocol ( identifier )
7256 @encode ( type-name )
7257 objc-string-literal
7258 Classname . identifier
7261 static struct c_expr
7262 c_parser_postfix_expression (c_parser *parser)
7264 struct c_expr expr, e1;
7265 struct c_type_name *t1, *t2;
7266 location_t loc = c_parser_peek_token (parser)->location;;
7267 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7268 expr.original_code = ERROR_MARK;
7269 expr.original_type = NULL;
7270 switch (c_parser_peek_token (parser)->type)
7272 case CPP_NUMBER:
7273 expr.value = c_parser_peek_token (parser)->value;
7274 set_c_expr_source_range (&expr, tok_range);
7275 loc = c_parser_peek_token (parser)->location;
7276 c_parser_consume_token (parser);
7277 if (TREE_CODE (expr.value) == FIXED_CST
7278 && !targetm.fixed_point_supported_p ())
7280 error_at (loc, "fixed-point types not supported for this target");
7281 expr.value = error_mark_node;
7283 break;
7284 case CPP_CHAR:
7285 case CPP_CHAR16:
7286 case CPP_CHAR32:
7287 case CPP_WCHAR:
7288 expr.value = c_parser_peek_token (parser)->value;
7289 set_c_expr_source_range (&expr, tok_range);
7290 c_parser_consume_token (parser);
7291 break;
7292 case CPP_STRING:
7293 case CPP_STRING16:
7294 case CPP_STRING32:
7295 case CPP_WSTRING:
7296 case CPP_UTF8STRING:
7297 expr.value = c_parser_peek_token (parser)->value;
7298 set_c_expr_source_range (&expr, tok_range);
7299 expr.original_code = STRING_CST;
7300 c_parser_consume_token (parser);
7301 break;
7302 case CPP_OBJC_STRING:
7303 gcc_assert (c_dialect_objc ());
7304 expr.value
7305 = objc_build_string_object (c_parser_peek_token (parser)->value);
7306 set_c_expr_source_range (&expr, tok_range);
7307 c_parser_consume_token (parser);
7308 break;
7309 case CPP_NAME:
7310 switch (c_parser_peek_token (parser)->id_kind)
7312 case C_ID_ID:
7314 tree id = c_parser_peek_token (parser)->value;
7315 c_parser_consume_token (parser);
7316 expr.value = build_external_ref (loc, id,
7317 (c_parser_peek_token (parser)->type
7318 == CPP_OPEN_PAREN),
7319 &expr.original_type);
7320 set_c_expr_source_range (&expr, tok_range);
7321 break;
7323 case C_ID_CLASSNAME:
7325 /* Here we parse the Objective-C 2.0 Class.name dot
7326 syntax. */
7327 tree class_name = c_parser_peek_token (parser)->value;
7328 tree component;
7329 c_parser_consume_token (parser);
7330 gcc_assert (c_dialect_objc ());
7331 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7333 expr.value = error_mark_node;
7334 break;
7336 if (c_parser_next_token_is_not (parser, CPP_NAME))
7338 c_parser_error (parser, "expected identifier");
7339 expr.value = error_mark_node;
7340 break;
7342 c_token *component_tok = c_parser_peek_token (parser);
7343 component = component_tok->value;
7344 location_t end_loc = component_tok->get_finish ();
7345 c_parser_consume_token (parser);
7346 expr.value = objc_build_class_component_ref (class_name,
7347 component);
7348 set_c_expr_source_range (&expr, loc, end_loc);
7349 break;
7351 default:
7352 c_parser_error (parser, "expected expression");
7353 expr.value = error_mark_node;
7354 break;
7356 break;
7357 case CPP_OPEN_PAREN:
7358 /* A parenthesized expression, statement expression or compound
7359 literal. */
7360 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7362 /* A statement expression. */
7363 tree stmt;
7364 location_t brace_loc;
7365 c_parser_consume_token (parser);
7366 brace_loc = c_parser_peek_token (parser)->location;
7367 c_parser_consume_token (parser);
7368 if (!building_stmt_list_p ())
7370 error_at (loc, "braced-group within expression allowed "
7371 "only inside a function");
7372 parser->error = true;
7373 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7374 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7375 expr.value = error_mark_node;
7376 break;
7378 stmt = c_begin_stmt_expr ();
7379 c_parser_compound_statement_nostart (parser);
7380 location_t close_loc = c_parser_peek_token (parser)->location;
7381 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7382 "expected %<)%>");
7383 pedwarn (loc, OPT_Wpedantic,
7384 "ISO C forbids braced-groups within expressions");
7385 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7386 set_c_expr_source_range (&expr, loc, close_loc);
7387 mark_exp_read (expr.value);
7389 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7391 /* A compound literal. ??? Can we actually get here rather
7392 than going directly to
7393 c_parser_postfix_expression_after_paren_type from
7394 elsewhere? */
7395 location_t loc;
7396 struct c_type_name *type_name;
7397 c_parser_consume_token (parser);
7398 loc = c_parser_peek_token (parser)->location;
7399 type_name = c_parser_type_name (parser);
7400 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7401 "expected %<)%>");
7402 if (type_name == NULL)
7404 expr.value = error_mark_node;
7406 else
7407 expr = c_parser_postfix_expression_after_paren_type (parser,
7408 type_name,
7409 loc);
7411 else
7413 /* A parenthesized expression. */
7414 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7415 c_parser_consume_token (parser);
7416 expr = c_parser_expression (parser);
7417 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7418 TREE_NO_WARNING (expr.value) = 1;
7419 if (expr.original_code != C_MAYBE_CONST_EXPR)
7420 expr.original_code = ERROR_MARK;
7421 /* Don't change EXPR.ORIGINAL_TYPE. */
7422 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7423 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7424 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7425 "expected %<)%>");
7427 break;
7428 case CPP_KEYWORD:
7429 switch (c_parser_peek_token (parser)->keyword)
7431 case RID_FUNCTION_NAME:
7432 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7433 "%<__FUNCTION__%> predefined identifier");
7434 expr.value = fname_decl (loc,
7435 c_parser_peek_token (parser)->keyword,
7436 c_parser_peek_token (parser)->value);
7437 set_c_expr_source_range (&expr, loc, loc);
7438 c_parser_consume_token (parser);
7439 break;
7440 case RID_PRETTY_FUNCTION_NAME:
7441 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7442 "%<__PRETTY_FUNCTION__%> predefined identifier");
7443 expr.value = fname_decl (loc,
7444 c_parser_peek_token (parser)->keyword,
7445 c_parser_peek_token (parser)->value);
7446 set_c_expr_source_range (&expr, loc, loc);
7447 c_parser_consume_token (parser);
7448 break;
7449 case RID_C99_FUNCTION_NAME:
7450 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7451 "%<__func__%> predefined identifier");
7452 expr.value = fname_decl (loc,
7453 c_parser_peek_token (parser)->keyword,
7454 c_parser_peek_token (parser)->value);
7455 set_c_expr_source_range (&expr, loc, loc);
7456 c_parser_consume_token (parser);
7457 break;
7458 case RID_VA_ARG:
7460 location_t start_loc = loc;
7461 c_parser_consume_token (parser);
7462 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7464 expr.value = error_mark_node;
7465 break;
7467 e1 = c_parser_expr_no_commas (parser, NULL);
7468 mark_exp_read (e1.value);
7469 e1.value = c_fully_fold (e1.value, false, NULL);
7470 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7472 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7473 expr.value = error_mark_node;
7474 break;
7476 loc = c_parser_peek_token (parser)->location;
7477 t1 = c_parser_type_name (parser);
7478 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7479 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7480 "expected %<)%>");
7481 if (t1 == NULL)
7483 expr.value = error_mark_node;
7485 else
7487 tree type_expr = NULL_TREE;
7488 expr.value = c_build_va_arg (loc, e1.value,
7489 groktypename (t1, &type_expr, NULL));
7490 if (type_expr)
7492 expr.value = build2 (C_MAYBE_CONST_EXPR,
7493 TREE_TYPE (expr.value), type_expr,
7494 expr.value);
7495 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7497 set_c_expr_source_range (&expr, start_loc, end_loc);
7500 break;
7501 case RID_OFFSETOF:
7502 c_parser_consume_token (parser);
7503 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7505 expr.value = error_mark_node;
7506 break;
7508 t1 = c_parser_type_name (parser);
7509 if (t1 == NULL)
7510 parser->error = true;
7511 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7512 gcc_assert (parser->error);
7513 if (parser->error)
7515 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7516 expr.value = error_mark_node;
7517 break;
7521 tree type = groktypename (t1, NULL, NULL);
7522 tree offsetof_ref;
7523 if (type == error_mark_node)
7524 offsetof_ref = error_mark_node;
7525 else
7527 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7528 SET_EXPR_LOCATION (offsetof_ref, loc);
7530 /* Parse the second argument to __builtin_offsetof. We
7531 must have one identifier, and beyond that we want to
7532 accept sub structure and sub array references. */
7533 if (c_parser_next_token_is (parser, CPP_NAME))
7535 offsetof_ref = build_component_ref
7536 (loc, offsetof_ref, c_parser_peek_token (parser)->value);
7537 c_parser_consume_token (parser);
7538 while (c_parser_next_token_is (parser, CPP_DOT)
7539 || c_parser_next_token_is (parser,
7540 CPP_OPEN_SQUARE)
7541 || c_parser_next_token_is (parser,
7542 CPP_DEREF))
7544 if (c_parser_next_token_is (parser, CPP_DEREF))
7546 loc = c_parser_peek_token (parser)->location;
7547 offsetof_ref = build_array_ref (loc,
7548 offsetof_ref,
7549 integer_zero_node);
7550 goto do_dot;
7552 else if (c_parser_next_token_is (parser, CPP_DOT))
7554 do_dot:
7555 c_parser_consume_token (parser);
7556 if (c_parser_next_token_is_not (parser,
7557 CPP_NAME))
7559 c_parser_error (parser, "expected identifier");
7560 break;
7562 offsetof_ref = build_component_ref
7563 (loc, offsetof_ref,
7564 c_parser_peek_token (parser)->value);
7565 c_parser_consume_token (parser);
7567 else
7569 struct c_expr ce;
7570 tree idx;
7571 loc = c_parser_peek_token (parser)->location;
7572 c_parser_consume_token (parser);
7573 ce = c_parser_expression (parser);
7574 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7575 idx = ce.value;
7576 idx = c_fully_fold (idx, false, NULL);
7577 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7578 "expected %<]%>");
7579 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7583 else
7584 c_parser_error (parser, "expected identifier");
7585 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7586 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7587 "expected %<)%>");
7588 expr.value = fold_offsetof (offsetof_ref);
7589 set_c_expr_source_range (&expr, loc, end_loc);
7591 break;
7592 case RID_CHOOSE_EXPR:
7594 vec<c_expr_t, va_gc> *cexpr_list;
7595 c_expr_t *e1_p, *e2_p, *e3_p;
7596 tree c;
7598 c_parser_consume_token (parser);
7599 if (!c_parser_get_builtin_args (parser,
7600 "__builtin_choose_expr",
7601 &cexpr_list, true))
7603 expr.value = error_mark_node;
7604 break;
7607 if (vec_safe_length (cexpr_list) != 3)
7609 error_at (loc, "wrong number of arguments to "
7610 "%<__builtin_choose_expr%>");
7611 expr.value = error_mark_node;
7612 break;
7615 e1_p = &(*cexpr_list)[0];
7616 e2_p = &(*cexpr_list)[1];
7617 e3_p = &(*cexpr_list)[2];
7619 c = e1_p->value;
7620 mark_exp_read (e2_p->value);
7621 mark_exp_read (e3_p->value);
7622 if (TREE_CODE (c) != INTEGER_CST
7623 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7624 error_at (loc,
7625 "first argument to %<__builtin_choose_expr%> not"
7626 " a constant");
7627 constant_expression_warning (c);
7628 expr = integer_zerop (c) ? *e3_p : *e2_p;
7629 break;
7631 case RID_TYPES_COMPATIBLE_P:
7632 c_parser_consume_token (parser);
7633 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7635 expr.value = error_mark_node;
7636 break;
7638 t1 = c_parser_type_name (parser);
7639 if (t1 == NULL)
7641 expr.value = error_mark_node;
7642 break;
7644 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7646 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7647 expr.value = error_mark_node;
7648 break;
7650 t2 = c_parser_type_name (parser);
7651 if (t2 == NULL)
7653 expr.value = error_mark_node;
7654 break;
7656 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7657 "expected %<)%>");
7659 tree e1, e2;
7660 e1 = groktypename (t1, NULL, NULL);
7661 e2 = groktypename (t2, NULL, NULL);
7662 if (e1 == error_mark_node || e2 == error_mark_node)
7664 expr.value = error_mark_node;
7665 break;
7668 e1 = TYPE_MAIN_VARIANT (e1);
7669 e2 = TYPE_MAIN_VARIANT (e2);
7671 expr.value
7672 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7674 break;
7675 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7677 vec<c_expr_t, va_gc> *cexpr_list;
7678 c_expr_t *e2_p;
7679 tree chain_value;
7681 c_parser_consume_token (parser);
7682 if (!c_parser_get_builtin_args (parser,
7683 "__builtin_call_with_static_chain",
7684 &cexpr_list, false))
7686 expr.value = error_mark_node;
7687 break;
7689 if (vec_safe_length (cexpr_list) != 2)
7691 error_at (loc, "wrong number of arguments to "
7692 "%<__builtin_call_with_static_chain%>");
7693 expr.value = error_mark_node;
7694 break;
7697 expr = (*cexpr_list)[0];
7698 e2_p = &(*cexpr_list)[1];
7699 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7700 chain_value = e2_p->value;
7701 mark_exp_read (chain_value);
7703 if (TREE_CODE (expr.value) != CALL_EXPR)
7704 error_at (loc, "first argument to "
7705 "%<__builtin_call_with_static_chain%> "
7706 "must be a call expression");
7707 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
7708 error_at (loc, "second argument to "
7709 "%<__builtin_call_with_static_chain%> "
7710 "must be a pointer type");
7711 else
7712 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
7713 break;
7715 case RID_BUILTIN_COMPLEX:
7717 vec<c_expr_t, va_gc> *cexpr_list;
7718 c_expr_t *e1_p, *e2_p;
7720 c_parser_consume_token (parser);
7721 if (!c_parser_get_builtin_args (parser,
7722 "__builtin_complex",
7723 &cexpr_list, false))
7725 expr.value = error_mark_node;
7726 break;
7729 if (vec_safe_length (cexpr_list) != 2)
7731 error_at (loc, "wrong number of arguments to "
7732 "%<__builtin_complex%>");
7733 expr.value = error_mark_node;
7734 break;
7737 e1_p = &(*cexpr_list)[0];
7738 e2_p = &(*cexpr_list)[1];
7740 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
7741 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
7742 e1_p->value = convert (TREE_TYPE (e1_p->value),
7743 TREE_OPERAND (e1_p->value, 0));
7744 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7745 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
7746 e2_p->value = convert (TREE_TYPE (e2_p->value),
7747 TREE_OPERAND (e2_p->value, 0));
7748 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7749 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
7750 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
7751 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
7753 error_at (loc, "%<__builtin_complex%> operand "
7754 "not of real binary floating-point type");
7755 expr.value = error_mark_node;
7756 break;
7758 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
7759 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
7761 error_at (loc,
7762 "%<__builtin_complex%> operands of different types");
7763 expr.value = error_mark_node;
7764 break;
7766 pedwarn_c90 (loc, OPT_Wpedantic,
7767 "ISO C90 does not support complex types");
7768 expr.value = build2 (COMPLEX_EXPR,
7769 build_complex_type
7770 (TYPE_MAIN_VARIANT
7771 (TREE_TYPE (e1_p->value))),
7772 e1_p->value, e2_p->value);
7773 break;
7775 case RID_BUILTIN_SHUFFLE:
7777 vec<c_expr_t, va_gc> *cexpr_list;
7778 unsigned int i;
7779 c_expr_t *p;
7781 c_parser_consume_token (parser);
7782 if (!c_parser_get_builtin_args (parser,
7783 "__builtin_shuffle",
7784 &cexpr_list, false))
7786 expr.value = error_mark_node;
7787 break;
7790 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
7791 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
7793 if (vec_safe_length (cexpr_list) == 2)
7794 expr.value =
7795 c_build_vec_perm_expr
7796 (loc, (*cexpr_list)[0].value,
7797 NULL_TREE, (*cexpr_list)[1].value);
7799 else if (vec_safe_length (cexpr_list) == 3)
7800 expr.value =
7801 c_build_vec_perm_expr
7802 (loc, (*cexpr_list)[0].value,
7803 (*cexpr_list)[1].value,
7804 (*cexpr_list)[2].value);
7805 else
7807 error_at (loc, "wrong number of arguments to "
7808 "%<__builtin_shuffle%>");
7809 expr.value = error_mark_node;
7811 break;
7813 case RID_AT_SELECTOR:
7814 gcc_assert (c_dialect_objc ());
7815 c_parser_consume_token (parser);
7816 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7818 expr.value = error_mark_node;
7819 break;
7822 tree sel = c_parser_objc_selector_arg (parser);
7823 location_t close_loc = c_parser_peek_token (parser)->location;
7824 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7825 "expected %<)%>");
7826 expr.value = objc_build_selector_expr (loc, sel);
7827 set_c_expr_source_range (&expr, loc, close_loc);
7829 break;
7830 case RID_AT_PROTOCOL:
7831 gcc_assert (c_dialect_objc ());
7832 c_parser_consume_token (parser);
7833 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7835 expr.value = error_mark_node;
7836 break;
7838 if (c_parser_next_token_is_not (parser, CPP_NAME))
7840 c_parser_error (parser, "expected identifier");
7841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7842 expr.value = error_mark_node;
7843 break;
7846 tree id = c_parser_peek_token (parser)->value;
7847 c_parser_consume_token (parser);
7848 location_t close_loc = c_parser_peek_token (parser)->location;
7849 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7850 "expected %<)%>");
7851 expr.value = objc_build_protocol_expr (id);
7852 set_c_expr_source_range (&expr, loc, close_loc);
7854 break;
7855 case RID_AT_ENCODE:
7856 /* Extension to support C-structures in the archiver. */
7857 gcc_assert (c_dialect_objc ());
7858 c_parser_consume_token (parser);
7859 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7861 expr.value = error_mark_node;
7862 break;
7864 t1 = c_parser_type_name (parser);
7865 if (t1 == NULL)
7867 expr.value = error_mark_node;
7868 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7869 break;
7872 location_t close_loc = c_parser_peek_token (parser)->location;
7873 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7874 "expected %<)%>");
7875 tree type = groktypename (t1, NULL, NULL);
7876 expr.value = objc_build_encode_expr (type);
7877 set_c_expr_source_range (&expr, loc, close_loc);
7879 break;
7880 case RID_GENERIC:
7881 expr = c_parser_generic_selection (parser);
7882 break;
7883 case RID_CILK_SPAWN:
7884 c_parser_consume_token (parser);
7885 if (!flag_cilkplus)
7887 error_at (loc, "-fcilkplus must be enabled to use "
7888 "%<_Cilk_spawn%>");
7889 expr = c_parser_postfix_expression (parser);
7890 expr.value = error_mark_node;
7892 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7894 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
7895 "are not permitted");
7896 /* Now flush out all the _Cilk_spawns. */
7897 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
7898 c_parser_consume_token (parser);
7899 expr = c_parser_postfix_expression (parser);
7901 else
7903 expr = c_parser_postfix_expression (parser);
7904 expr.value = build_cilk_spawn (loc, expr.value);
7906 break;
7907 default:
7908 c_parser_error (parser, "expected expression");
7909 expr.value = error_mark_node;
7910 break;
7912 break;
7913 case CPP_OPEN_SQUARE:
7914 if (c_dialect_objc ())
7916 tree receiver, args;
7917 c_parser_consume_token (parser);
7918 receiver = c_parser_objc_receiver (parser);
7919 args = c_parser_objc_message_args (parser);
7920 location_t close_loc = c_parser_peek_token (parser)->location;
7921 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7922 "expected %<]%>");
7923 expr.value = objc_build_message_expr (receiver, args);
7924 set_c_expr_source_range (&expr, loc, close_loc);
7925 break;
7927 /* Else fall through to report error. */
7928 default:
7929 c_parser_error (parser, "expected expression");
7930 expr.value = error_mark_node;
7931 break;
7933 return c_parser_postfix_expression_after_primary (parser, loc, expr);
7936 /* Parse a postfix expression after a parenthesized type name: the
7937 brace-enclosed initializer of a compound literal, possibly followed
7938 by some postfix operators. This is separate because it is not
7939 possible to tell until after the type name whether a cast
7940 expression has a cast or a compound literal, or whether the operand
7941 of sizeof is a parenthesized type name or starts with a compound
7942 literal. TYPE_LOC is the location where TYPE_NAME starts--the
7943 location of the first token after the parentheses around the type
7944 name. */
7946 static struct c_expr
7947 c_parser_postfix_expression_after_paren_type (c_parser *parser,
7948 struct c_type_name *type_name,
7949 location_t type_loc)
7951 tree type;
7952 struct c_expr init;
7953 bool non_const;
7954 struct c_expr expr;
7955 location_t start_loc;
7956 tree type_expr = NULL_TREE;
7957 bool type_expr_const = true;
7958 check_compound_literal_type (type_loc, type_name);
7959 start_init (NULL_TREE, NULL, 0);
7960 type = groktypename (type_name, &type_expr, &type_expr_const);
7961 start_loc = c_parser_peek_token (parser)->location;
7962 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
7964 error_at (type_loc, "compound literal has variable size");
7965 type = error_mark_node;
7967 init = c_parser_braced_init (parser, type, false);
7968 finish_init ();
7969 maybe_warn_string_init (type_loc, type, init);
7971 if (type != error_mark_node
7972 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
7973 && current_function_decl)
7975 error ("compound literal qualified by address-space qualifier");
7976 type = error_mark_node;
7979 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
7980 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
7981 ? CONSTRUCTOR_NON_CONST (init.value)
7982 : init.original_code == C_MAYBE_CONST_EXPR);
7983 non_const |= !type_expr_const;
7984 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
7985 set_c_expr_source_range (&expr, init.src_range);
7986 expr.original_code = ERROR_MARK;
7987 expr.original_type = NULL;
7988 if (type != error_mark_node && type_expr)
7990 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
7992 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
7993 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
7995 else
7997 gcc_assert (!non_const);
7998 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
7999 type_expr, expr.value);
8002 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8005 /* Callback function for sizeof_pointer_memaccess_warning to compare
8006 types. */
8008 static bool
8009 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8011 return comptypes (type1, type2) == 1;
8014 /* Parse a postfix expression after the initial primary or compound
8015 literal; that is, parse a series of postfix operators.
8017 EXPR_LOC is the location of the primary expression. */
8019 static struct c_expr
8020 c_parser_postfix_expression_after_primary (c_parser *parser,
8021 location_t expr_loc,
8022 struct c_expr expr)
8024 struct c_expr orig_expr;
8025 tree ident, idx;
8026 location_t sizeof_arg_loc[3];
8027 tree sizeof_arg[3];
8028 unsigned int literal_zero_mask;
8029 unsigned int i;
8030 vec<tree, va_gc> *exprlist;
8031 vec<tree, va_gc> *origtypes = NULL;
8032 vec<location_t> arg_loc = vNULL;
8033 location_t start;
8034 location_t finish;
8036 while (true)
8038 location_t op_loc = c_parser_peek_token (parser)->location;
8039 switch (c_parser_peek_token (parser)->type)
8041 case CPP_OPEN_SQUARE:
8042 /* Array reference. */
8043 c_parser_consume_token (parser);
8044 if (flag_cilkplus
8045 && c_parser_peek_token (parser)->type == CPP_COLON)
8046 /* If we are here, then we have something like this:
8047 Array [ : ]
8049 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8050 expr.value);
8051 else
8053 idx = c_parser_expression (parser).value;
8054 /* Here we have 3 options:
8055 1. Array [EXPR] -- Normal Array call.
8056 2. Array [EXPR : EXPR] -- Array notation without stride.
8057 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8059 For 1, we just handle it just like a normal array expression.
8060 For 2 and 3 we handle it like we handle array notations. The
8061 idx value we have above becomes the initial/start index.
8063 if (flag_cilkplus
8064 && c_parser_peek_token (parser)->type == CPP_COLON)
8065 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8066 expr.value);
8067 else
8069 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8070 "expected %<]%>");
8071 start = expr.get_start ();
8072 finish = parser->tokens_buf[0].location;
8073 expr.value = build_array_ref (op_loc, expr.value, idx);
8074 set_c_expr_source_range (&expr, start, finish);
8077 expr.original_code = ERROR_MARK;
8078 expr.original_type = NULL;
8079 break;
8080 case CPP_OPEN_PAREN:
8081 /* Function call. */
8082 c_parser_consume_token (parser);
8083 for (i = 0; i < 3; i++)
8085 sizeof_arg[i] = NULL_TREE;
8086 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8088 literal_zero_mask = 0;
8089 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8090 exprlist = NULL;
8091 else
8092 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8093 sizeof_arg_loc, sizeof_arg,
8094 &arg_loc, &literal_zero_mask);
8095 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8096 "expected %<)%>");
8097 orig_expr = expr;
8098 mark_exp_read (expr.value);
8099 if (warn_sizeof_pointer_memaccess)
8100 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8101 expr.value, exprlist,
8102 sizeof_arg,
8103 sizeof_ptr_memacc_comptypes);
8104 if (warn_memset_transposed_args
8105 && TREE_CODE (expr.value) == FUNCTION_DECL
8106 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8107 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8108 && vec_safe_length (exprlist) == 3
8109 && integer_zerop ((*exprlist)[2])
8110 && (literal_zero_mask & (1 << 2)) != 0
8111 && (!integer_zerop ((*exprlist)[1])
8112 || (literal_zero_mask & (1 << 1)) == 0))
8113 warning_at (expr_loc, OPT_Wmemset_transposed_args,
8114 "%<memset%> used with constant zero length parameter; "
8115 "this could be due to transposed parameters");
8117 start = expr.get_start ();
8118 finish = parser->tokens_buf[0].get_finish ();
8119 expr.value
8120 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8121 exprlist, origtypes);
8122 set_c_expr_source_range (&expr, start, finish);
8124 expr.original_code = ERROR_MARK;
8125 if (TREE_CODE (expr.value) == INTEGER_CST
8126 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8127 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8128 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8129 expr.original_code = C_MAYBE_CONST_EXPR;
8130 expr.original_type = NULL;
8131 if (exprlist)
8133 release_tree_vector (exprlist);
8134 release_tree_vector (origtypes);
8136 arg_loc.release ();
8137 break;
8138 case CPP_DOT:
8139 /* Structure element reference. */
8140 c_parser_consume_token (parser);
8141 expr = default_function_array_conversion (expr_loc, expr);
8142 if (c_parser_next_token_is (parser, CPP_NAME))
8143 ident = c_parser_peek_token (parser)->value;
8144 else
8146 c_parser_error (parser, "expected identifier");
8147 expr.value = error_mark_node;
8148 expr.original_code = ERROR_MARK;
8149 expr.original_type = NULL;
8150 return expr;
8152 start = expr.get_start ();
8153 finish = c_parser_peek_token (parser)->get_finish ();
8154 c_parser_consume_token (parser);
8155 expr.value = build_component_ref (op_loc, expr.value, ident);
8156 set_c_expr_source_range (&expr, start, finish);
8157 expr.original_code = ERROR_MARK;
8158 if (TREE_CODE (expr.value) != COMPONENT_REF)
8159 expr.original_type = NULL;
8160 else
8162 /* Remember the original type of a bitfield. */
8163 tree field = TREE_OPERAND (expr.value, 1);
8164 if (TREE_CODE (field) != FIELD_DECL)
8165 expr.original_type = NULL;
8166 else
8167 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8169 break;
8170 case CPP_DEREF:
8171 /* Structure element reference. */
8172 c_parser_consume_token (parser);
8173 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8174 if (c_parser_next_token_is (parser, CPP_NAME))
8175 ident = c_parser_peek_token (parser)->value;
8176 else
8178 c_parser_error (parser, "expected identifier");
8179 expr.value = error_mark_node;
8180 expr.original_code = ERROR_MARK;
8181 expr.original_type = NULL;
8182 return expr;
8184 start = expr.get_start ();
8185 finish = c_parser_peek_token (parser)->get_finish ();
8186 c_parser_consume_token (parser);
8187 expr.value = build_component_ref (op_loc,
8188 build_indirect_ref (op_loc,
8189 expr.value,
8190 RO_ARROW),
8191 ident);
8192 set_c_expr_source_range (&expr, start, finish);
8193 expr.original_code = ERROR_MARK;
8194 if (TREE_CODE (expr.value) != COMPONENT_REF)
8195 expr.original_type = NULL;
8196 else
8198 /* Remember the original type of a bitfield. */
8199 tree field = TREE_OPERAND (expr.value, 1);
8200 if (TREE_CODE (field) != FIELD_DECL)
8201 expr.original_type = NULL;
8202 else
8203 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8205 break;
8206 case CPP_PLUS_PLUS:
8207 /* Postincrement. */
8208 start = expr.get_start ();
8209 finish = c_parser_peek_token (parser)->get_finish ();
8210 c_parser_consume_token (parser);
8211 /* If the expressions have array notations, we expand them. */
8212 if (flag_cilkplus
8213 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8214 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8215 else
8217 expr = default_function_array_read_conversion (expr_loc, expr);
8218 expr.value = build_unary_op (op_loc,
8219 POSTINCREMENT_EXPR, expr.value, 0);
8221 set_c_expr_source_range (&expr, start, finish);
8222 expr.original_code = ERROR_MARK;
8223 expr.original_type = NULL;
8224 break;
8225 case CPP_MINUS_MINUS:
8226 /* Postdecrement. */
8227 start = expr.get_start ();
8228 finish = c_parser_peek_token (parser)->get_finish ();
8229 c_parser_consume_token (parser);
8230 /* If the expressions have array notations, we expand them. */
8231 if (flag_cilkplus
8232 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8233 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8234 else
8236 expr = default_function_array_read_conversion (expr_loc, expr);
8237 expr.value = build_unary_op (op_loc,
8238 POSTDECREMENT_EXPR, expr.value, 0);
8240 set_c_expr_source_range (&expr, start, finish);
8241 expr.original_code = ERROR_MARK;
8242 expr.original_type = NULL;
8243 break;
8244 default:
8245 return expr;
8250 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8252 expression:
8253 assignment-expression
8254 expression , assignment-expression
8257 static struct c_expr
8258 c_parser_expression (c_parser *parser)
8260 location_t tloc = c_parser_peek_token (parser)->location;
8261 struct c_expr expr;
8262 expr = c_parser_expr_no_commas (parser, NULL);
8263 if (c_parser_next_token_is (parser, CPP_COMMA))
8264 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8265 while (c_parser_next_token_is (parser, CPP_COMMA))
8267 struct c_expr next;
8268 tree lhsval;
8269 location_t loc = c_parser_peek_token (parser)->location;
8270 location_t expr_loc;
8271 c_parser_consume_token (parser);
8272 expr_loc = c_parser_peek_token (parser)->location;
8273 lhsval = expr.value;
8274 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8275 lhsval = TREE_OPERAND (lhsval, 1);
8276 if (DECL_P (lhsval) || handled_component_p (lhsval))
8277 mark_exp_read (lhsval);
8278 next = c_parser_expr_no_commas (parser, NULL);
8279 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8280 expr.value = build_compound_expr (loc, expr.value, next.value);
8281 expr.original_code = COMPOUND_EXPR;
8282 expr.original_type = next.original_type;
8284 return expr;
8287 /* Parse an expression and convert functions or arrays to pointers and
8288 lvalues to rvalues. */
8290 static struct c_expr
8291 c_parser_expression_conv (c_parser *parser)
8293 struct c_expr expr;
8294 location_t loc = c_parser_peek_token (parser)->location;
8295 expr = c_parser_expression (parser);
8296 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8297 return expr;
8300 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8301 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8303 static inline void
8304 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8305 unsigned int idx)
8307 if (idx >= HOST_BITS_PER_INT)
8308 return;
8310 c_token *tok = c_parser_peek_token (parser);
8311 switch (tok->type)
8313 case CPP_NUMBER:
8314 case CPP_CHAR:
8315 case CPP_WCHAR:
8316 case CPP_CHAR16:
8317 case CPP_CHAR32:
8318 /* If a parameter is literal zero alone, remember it
8319 for -Wmemset-transposed-args warning. */
8320 if (integer_zerop (tok->value)
8321 && !TREE_OVERFLOW (tok->value)
8322 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8323 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8324 *literal_zero_mask |= 1U << idx;
8325 default:
8326 break;
8330 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8331 functions and arrays to pointers and lvalues to rvalues. If
8332 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8333 locations of function arguments into this vector.
8335 nonempty-expr-list:
8336 assignment-expression
8337 nonempty-expr-list , assignment-expression
8340 static vec<tree, va_gc> *
8341 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8342 vec<tree, va_gc> **p_orig_types,
8343 location_t *sizeof_arg_loc, tree *sizeof_arg,
8344 vec<location_t> *locations,
8345 unsigned int *literal_zero_mask)
8347 vec<tree, va_gc> *ret;
8348 vec<tree, va_gc> *orig_types;
8349 struct c_expr expr;
8350 location_t loc = c_parser_peek_token (parser)->location;
8351 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8352 unsigned int idx = 0;
8354 ret = make_tree_vector ();
8355 if (p_orig_types == NULL)
8356 orig_types = NULL;
8357 else
8358 orig_types = make_tree_vector ();
8360 if (sizeof_arg != NULL
8361 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8362 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8363 if (literal_zero_mask)
8364 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8365 expr = c_parser_expr_no_commas (parser, NULL);
8366 if (convert_p)
8367 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8368 if (fold_p)
8369 expr.value = c_fully_fold (expr.value, false, NULL);
8370 ret->quick_push (expr.value);
8371 if (orig_types)
8372 orig_types->quick_push (expr.original_type);
8373 if (locations)
8374 locations->safe_push (loc);
8375 if (sizeof_arg != NULL
8376 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8377 && expr.original_code == SIZEOF_EXPR)
8379 sizeof_arg[0] = c_last_sizeof_arg;
8380 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8382 while (c_parser_next_token_is (parser, CPP_COMMA))
8384 c_parser_consume_token (parser);
8385 loc = c_parser_peek_token (parser)->location;
8386 if (sizeof_arg != NULL
8387 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8388 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8389 else
8390 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8391 if (literal_zero_mask)
8392 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8393 expr = c_parser_expr_no_commas (parser, NULL);
8394 if (convert_p)
8395 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8396 if (fold_p)
8397 expr.value = c_fully_fold (expr.value, false, NULL);
8398 vec_safe_push (ret, expr.value);
8399 if (orig_types)
8400 vec_safe_push (orig_types, expr.original_type);
8401 if (locations)
8402 locations->safe_push (loc);
8403 if (++idx < 3
8404 && sizeof_arg != NULL
8405 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8406 && expr.original_code == SIZEOF_EXPR)
8408 sizeof_arg[idx] = c_last_sizeof_arg;
8409 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8412 if (orig_types)
8413 *p_orig_types = orig_types;
8414 return ret;
8417 /* Parse Objective-C-specific constructs. */
8419 /* Parse an objc-class-definition.
8421 objc-class-definition:
8422 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8423 objc-class-instance-variables[opt] objc-methodprotolist @end
8424 @implementation identifier objc-superclass[opt]
8425 objc-class-instance-variables[opt]
8426 @interface identifier ( identifier ) objc-protocol-refs[opt]
8427 objc-methodprotolist @end
8428 @interface identifier ( ) objc-protocol-refs[opt]
8429 objc-methodprotolist @end
8430 @implementation identifier ( identifier )
8432 objc-superclass:
8433 : identifier
8435 "@interface identifier (" must start "@interface identifier (
8436 identifier ) ...": objc-methodprotolist in the first production may
8437 not start with a parenthesized identifier as a declarator of a data
8438 definition with no declaration specifiers if the objc-superclass,
8439 objc-protocol-refs and objc-class-instance-variables are omitted. */
8441 static void
8442 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8444 bool iface_p;
8445 tree id1;
8446 tree superclass;
8447 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8448 iface_p = true;
8449 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8450 iface_p = false;
8451 else
8452 gcc_unreachable ();
8454 c_parser_consume_token (parser);
8455 if (c_parser_next_token_is_not (parser, CPP_NAME))
8457 c_parser_error (parser, "expected identifier");
8458 return;
8460 id1 = c_parser_peek_token (parser)->value;
8461 c_parser_consume_token (parser);
8462 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8464 /* We have a category or class extension. */
8465 tree id2;
8466 tree proto = NULL_TREE;
8467 c_parser_consume_token (parser);
8468 if (c_parser_next_token_is_not (parser, CPP_NAME))
8470 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8472 /* We have a class extension. */
8473 id2 = NULL_TREE;
8475 else
8477 c_parser_error (parser, "expected identifier or %<)%>");
8478 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8479 return;
8482 else
8484 id2 = c_parser_peek_token (parser)->value;
8485 c_parser_consume_token (parser);
8487 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8488 if (!iface_p)
8490 objc_start_category_implementation (id1, id2);
8491 return;
8493 if (c_parser_next_token_is (parser, CPP_LESS))
8494 proto = c_parser_objc_protocol_refs (parser);
8495 objc_start_category_interface (id1, id2, proto, attributes);
8496 c_parser_objc_methodprotolist (parser);
8497 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8498 objc_finish_interface ();
8499 return;
8501 if (c_parser_next_token_is (parser, CPP_COLON))
8503 c_parser_consume_token (parser);
8504 if (c_parser_next_token_is_not (parser, CPP_NAME))
8506 c_parser_error (parser, "expected identifier");
8507 return;
8509 superclass = c_parser_peek_token (parser)->value;
8510 c_parser_consume_token (parser);
8512 else
8513 superclass = NULL_TREE;
8514 if (iface_p)
8516 tree proto = NULL_TREE;
8517 if (c_parser_next_token_is (parser, CPP_LESS))
8518 proto = c_parser_objc_protocol_refs (parser);
8519 objc_start_class_interface (id1, superclass, proto, attributes);
8521 else
8522 objc_start_class_implementation (id1, superclass);
8523 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8524 c_parser_objc_class_instance_variables (parser);
8525 if (iface_p)
8527 objc_continue_interface ();
8528 c_parser_objc_methodprotolist (parser);
8529 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8530 objc_finish_interface ();
8532 else
8534 objc_continue_implementation ();
8535 return;
8539 /* Parse objc-class-instance-variables.
8541 objc-class-instance-variables:
8542 { objc-instance-variable-decl-list[opt] }
8544 objc-instance-variable-decl-list:
8545 objc-visibility-spec
8546 objc-instance-variable-decl ;
8548 objc-instance-variable-decl-list objc-visibility-spec
8549 objc-instance-variable-decl-list objc-instance-variable-decl ;
8550 objc-instance-variable-decl-list ;
8552 objc-visibility-spec:
8553 @private
8554 @protected
8555 @public
8557 objc-instance-variable-decl:
8558 struct-declaration
8561 static void
8562 c_parser_objc_class_instance_variables (c_parser *parser)
8564 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8565 c_parser_consume_token (parser);
8566 while (c_parser_next_token_is_not (parser, CPP_EOF))
8568 tree decls;
8569 /* Parse any stray semicolon. */
8570 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8572 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8573 "extra semicolon");
8574 c_parser_consume_token (parser);
8575 continue;
8577 /* Stop if at the end of the instance variables. */
8578 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8580 c_parser_consume_token (parser);
8581 break;
8583 /* Parse any objc-visibility-spec. */
8584 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8586 c_parser_consume_token (parser);
8587 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8588 continue;
8590 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8592 c_parser_consume_token (parser);
8593 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8594 continue;
8596 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8598 c_parser_consume_token (parser);
8599 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8600 continue;
8602 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8604 c_parser_consume_token (parser);
8605 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8606 continue;
8608 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8610 c_parser_pragma (parser, pragma_external);
8611 continue;
8614 /* Parse some comma-separated declarations. */
8615 decls = c_parser_struct_declaration (parser);
8616 if (decls == NULL)
8618 /* There is a syntax error. We want to skip the offending
8619 tokens up to the next ';' (included) or '}'
8620 (excluded). */
8622 /* First, skip manually a ')' or ']'. This is because they
8623 reduce the nesting level, so c_parser_skip_until_found()
8624 wouldn't be able to skip past them. */
8625 c_token *token = c_parser_peek_token (parser);
8626 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8627 c_parser_consume_token (parser);
8629 /* Then, do the standard skipping. */
8630 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8632 /* We hopefully recovered. Start normal parsing again. */
8633 parser->error = false;
8634 continue;
8636 else
8638 /* Comma-separated instance variables are chained together
8639 in reverse order; add them one by one. */
8640 tree ivar = nreverse (decls);
8641 for (; ivar; ivar = DECL_CHAIN (ivar))
8642 objc_add_instance_variable (copy_node (ivar));
8644 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8648 /* Parse an objc-class-declaration.
8650 objc-class-declaration:
8651 @class identifier-list ;
8654 static void
8655 c_parser_objc_class_declaration (c_parser *parser)
8657 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8658 c_parser_consume_token (parser);
8659 /* Any identifiers, including those declared as type names, are OK
8660 here. */
8661 while (true)
8663 tree id;
8664 if (c_parser_next_token_is_not (parser, CPP_NAME))
8666 c_parser_error (parser, "expected identifier");
8667 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8668 parser->error = false;
8669 return;
8671 id = c_parser_peek_token (parser)->value;
8672 objc_declare_class (id);
8673 c_parser_consume_token (parser);
8674 if (c_parser_next_token_is (parser, CPP_COMMA))
8675 c_parser_consume_token (parser);
8676 else
8677 break;
8679 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8682 /* Parse an objc-alias-declaration.
8684 objc-alias-declaration:
8685 @compatibility_alias identifier identifier ;
8688 static void
8689 c_parser_objc_alias_declaration (c_parser *parser)
8691 tree id1, id2;
8692 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
8693 c_parser_consume_token (parser);
8694 if (c_parser_next_token_is_not (parser, CPP_NAME))
8696 c_parser_error (parser, "expected identifier");
8697 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8698 return;
8700 id1 = c_parser_peek_token (parser)->value;
8701 c_parser_consume_token (parser);
8702 if (c_parser_next_token_is_not (parser, CPP_NAME))
8704 c_parser_error (parser, "expected identifier");
8705 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8706 return;
8708 id2 = c_parser_peek_token (parser)->value;
8709 c_parser_consume_token (parser);
8710 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8711 objc_declare_alias (id1, id2);
8714 /* Parse an objc-protocol-definition.
8716 objc-protocol-definition:
8717 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
8718 @protocol identifier-list ;
8720 "@protocol identifier ;" should be resolved as "@protocol
8721 identifier-list ;": objc-methodprotolist may not start with a
8722 semicolon in the first alternative if objc-protocol-refs are
8723 omitted. */
8725 static void
8726 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
8728 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
8730 c_parser_consume_token (parser);
8731 if (c_parser_next_token_is_not (parser, CPP_NAME))
8733 c_parser_error (parser, "expected identifier");
8734 return;
8736 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8737 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
8739 /* Any identifiers, including those declared as type names, are
8740 OK here. */
8741 while (true)
8743 tree id;
8744 if (c_parser_next_token_is_not (parser, CPP_NAME))
8746 c_parser_error (parser, "expected identifier");
8747 break;
8749 id = c_parser_peek_token (parser)->value;
8750 objc_declare_protocol (id, attributes);
8751 c_parser_consume_token (parser);
8752 if (c_parser_next_token_is (parser, CPP_COMMA))
8753 c_parser_consume_token (parser);
8754 else
8755 break;
8757 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8759 else
8761 tree id = c_parser_peek_token (parser)->value;
8762 tree proto = NULL_TREE;
8763 c_parser_consume_token (parser);
8764 if (c_parser_next_token_is (parser, CPP_LESS))
8765 proto = c_parser_objc_protocol_refs (parser);
8766 parser->objc_pq_context = true;
8767 objc_start_protocol (id, proto, attributes);
8768 c_parser_objc_methodprotolist (parser);
8769 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8770 parser->objc_pq_context = false;
8771 objc_finish_interface ();
8775 /* Parse an objc-method-type.
8777 objc-method-type:
8781 Return true if it is a class method (+) and false if it is
8782 an instance method (-).
8784 static inline bool
8785 c_parser_objc_method_type (c_parser *parser)
8787 switch (c_parser_peek_token (parser)->type)
8789 case CPP_PLUS:
8790 c_parser_consume_token (parser);
8791 return true;
8792 case CPP_MINUS:
8793 c_parser_consume_token (parser);
8794 return false;
8795 default:
8796 gcc_unreachable ();
8800 /* Parse an objc-method-definition.
8802 objc-method-definition:
8803 objc-method-type objc-method-decl ;[opt] compound-statement
8806 static void
8807 c_parser_objc_method_definition (c_parser *parser)
8809 bool is_class_method = c_parser_objc_method_type (parser);
8810 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
8811 parser->objc_pq_context = true;
8812 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8813 &expr);
8814 if (decl == error_mark_node)
8815 return; /* Bail here. */
8817 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8819 c_parser_consume_token (parser);
8820 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8821 "extra semicolon in method definition specified");
8824 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8826 c_parser_error (parser, "expected %<{%>");
8827 return;
8830 parser->objc_pq_context = false;
8831 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
8833 add_stmt (c_parser_compound_statement (parser));
8834 objc_finish_method_definition (current_function_decl);
8836 else
8838 /* This code is executed when we find a method definition
8839 outside of an @implementation context (or invalid for other
8840 reasons). Parse the method (to keep going) but do not emit
8841 any code.
8843 c_parser_compound_statement (parser);
8847 /* Parse an objc-methodprotolist.
8849 objc-methodprotolist:
8850 empty
8851 objc-methodprotolist objc-methodproto
8852 objc-methodprotolist declaration
8853 objc-methodprotolist ;
8854 @optional
8855 @required
8857 The declaration is a data definition, which may be missing
8858 declaration specifiers under the same rules and diagnostics as
8859 other data definitions outside functions, and the stray semicolon
8860 is diagnosed the same way as a stray semicolon outside a
8861 function. */
8863 static void
8864 c_parser_objc_methodprotolist (c_parser *parser)
8866 while (true)
8868 /* The list is terminated by @end. */
8869 switch (c_parser_peek_token (parser)->type)
8871 case CPP_SEMICOLON:
8872 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8873 "ISO C does not allow extra %<;%> outside of a function");
8874 c_parser_consume_token (parser);
8875 break;
8876 case CPP_PLUS:
8877 case CPP_MINUS:
8878 c_parser_objc_methodproto (parser);
8879 break;
8880 case CPP_PRAGMA:
8881 c_parser_pragma (parser, pragma_external);
8882 break;
8883 case CPP_EOF:
8884 return;
8885 default:
8886 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
8887 return;
8888 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
8889 c_parser_objc_at_property_declaration (parser);
8890 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
8892 objc_set_method_opt (true);
8893 c_parser_consume_token (parser);
8895 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
8897 objc_set_method_opt (false);
8898 c_parser_consume_token (parser);
8900 else
8901 c_parser_declaration_or_fndef (parser, false, false, true,
8902 false, true, NULL, vNULL);
8903 break;
8908 /* Parse an objc-methodproto.
8910 objc-methodproto:
8911 objc-method-type objc-method-decl ;
8914 static void
8915 c_parser_objc_methodproto (c_parser *parser)
8917 bool is_class_method = c_parser_objc_method_type (parser);
8918 tree decl, attributes = NULL_TREE;
8920 /* Remember protocol qualifiers in prototypes. */
8921 parser->objc_pq_context = true;
8922 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
8923 NULL);
8924 /* Forget protocol qualifiers now. */
8925 parser->objc_pq_context = false;
8927 /* Do not allow the presence of attributes to hide an erroneous
8928 method implementation in the interface section. */
8929 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
8931 c_parser_error (parser, "expected %<;%>");
8932 return;
8935 if (decl != error_mark_node)
8936 objc_add_method_declaration (is_class_method, decl, attributes);
8938 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8941 /* If we are at a position that method attributes may be present, check that
8942 there are not any parsed already (a syntax error) and then collect any
8943 specified at the current location. Finally, if new attributes were present,
8944 check that the next token is legal ( ';' for decls and '{' for defs). */
8946 static bool
8947 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
8949 bool bad = false;
8950 if (*attributes)
8952 c_parser_error (parser,
8953 "method attributes must be specified at the end only");
8954 *attributes = NULL_TREE;
8955 bad = true;
8958 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
8959 *attributes = c_parser_attributes (parser);
8961 /* If there were no attributes here, just report any earlier error. */
8962 if (*attributes == NULL_TREE || bad)
8963 return bad;
8965 /* If the attributes are followed by a ; or {, then just report any earlier
8966 error. */
8967 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
8968 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8969 return bad;
8971 /* We've got attributes, but not at the end. */
8972 c_parser_error (parser,
8973 "expected %<;%> or %<{%> after method attribute definition");
8974 return true;
8977 /* Parse an objc-method-decl.
8979 objc-method-decl:
8980 ( objc-type-name ) objc-selector
8981 objc-selector
8982 ( objc-type-name ) objc-keyword-selector objc-optparmlist
8983 objc-keyword-selector objc-optparmlist
8984 attributes
8986 objc-keyword-selector:
8987 objc-keyword-decl
8988 objc-keyword-selector objc-keyword-decl
8990 objc-keyword-decl:
8991 objc-selector : ( objc-type-name ) identifier
8992 objc-selector : identifier
8993 : ( objc-type-name ) identifier
8994 : identifier
8996 objc-optparmlist:
8997 objc-optparms objc-optellipsis
8999 objc-optparms:
9000 empty
9001 objc-opt-parms , parameter-declaration
9003 objc-optellipsis:
9004 empty
9005 , ...
9008 static tree
9009 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9010 tree *attributes, tree *expr)
9012 tree type = NULL_TREE;
9013 tree sel;
9014 tree parms = NULL_TREE;
9015 bool ellipsis = false;
9016 bool attr_err = false;
9018 *attributes = NULL_TREE;
9019 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9021 c_parser_consume_token (parser);
9022 type = c_parser_objc_type_name (parser);
9023 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9025 sel = c_parser_objc_selector (parser);
9026 /* If there is no selector, or a colon follows, we have an
9027 objc-keyword-selector. If there is a selector, and a colon does
9028 not follow, that selector ends the objc-method-decl. */
9029 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9031 tree tsel = sel;
9032 tree list = NULL_TREE;
9033 while (true)
9035 tree atype = NULL_TREE, id, keyworddecl;
9036 tree param_attr = NULL_TREE;
9037 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9038 break;
9039 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9041 c_parser_consume_token (parser);
9042 atype = c_parser_objc_type_name (parser);
9043 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9044 "expected %<)%>");
9046 /* New ObjC allows attributes on method parameters. */
9047 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9048 param_attr = c_parser_attributes (parser);
9049 if (c_parser_next_token_is_not (parser, CPP_NAME))
9051 c_parser_error (parser, "expected identifier");
9052 return error_mark_node;
9054 id = c_parser_peek_token (parser)->value;
9055 c_parser_consume_token (parser);
9056 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9057 list = chainon (list, keyworddecl);
9058 tsel = c_parser_objc_selector (parser);
9059 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9060 break;
9063 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9065 /* Parse the optional parameter list. Optional Objective-C
9066 method parameters follow the C syntax, and may include '...'
9067 to denote a variable number of arguments. */
9068 parms = make_node (TREE_LIST);
9069 while (c_parser_next_token_is (parser, CPP_COMMA))
9071 struct c_parm *parm;
9072 c_parser_consume_token (parser);
9073 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9075 ellipsis = true;
9076 c_parser_consume_token (parser);
9077 attr_err |= c_parser_objc_maybe_method_attributes
9078 (parser, attributes) ;
9079 break;
9081 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9082 if (parm == NULL)
9083 break;
9084 parms = chainon (parms,
9085 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9087 sel = list;
9089 else
9090 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9092 if (sel == NULL)
9094 c_parser_error (parser, "objective-c method declaration is expected");
9095 return error_mark_node;
9098 if (attr_err)
9099 return error_mark_node;
9101 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9104 /* Parse an objc-type-name.
9106 objc-type-name:
9107 objc-type-qualifiers[opt] type-name
9108 objc-type-qualifiers[opt]
9110 objc-type-qualifiers:
9111 objc-type-qualifier
9112 objc-type-qualifiers objc-type-qualifier
9114 objc-type-qualifier: one of
9115 in out inout bycopy byref oneway
9118 static tree
9119 c_parser_objc_type_name (c_parser *parser)
9121 tree quals = NULL_TREE;
9122 struct c_type_name *type_name = NULL;
9123 tree type = NULL_TREE;
9124 while (true)
9126 c_token *token = c_parser_peek_token (parser);
9127 if (token->type == CPP_KEYWORD
9128 && (token->keyword == RID_IN
9129 || token->keyword == RID_OUT
9130 || token->keyword == RID_INOUT
9131 || token->keyword == RID_BYCOPY
9132 || token->keyword == RID_BYREF
9133 || token->keyword == RID_ONEWAY))
9135 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9136 c_parser_consume_token (parser);
9138 else
9139 break;
9141 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9142 type_name = c_parser_type_name (parser);
9143 if (type_name)
9144 type = groktypename (type_name, NULL, NULL);
9146 /* If the type is unknown, and error has already been produced and
9147 we need to recover from the error. In that case, use NULL_TREE
9148 for the type, as if no type had been specified; this will use the
9149 default type ('id') which is good for error recovery. */
9150 if (type == error_mark_node)
9151 type = NULL_TREE;
9153 return build_tree_list (quals, type);
9156 /* Parse objc-protocol-refs.
9158 objc-protocol-refs:
9159 < identifier-list >
9162 static tree
9163 c_parser_objc_protocol_refs (c_parser *parser)
9165 tree list = NULL_TREE;
9166 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9167 c_parser_consume_token (parser);
9168 /* Any identifiers, including those declared as type names, are OK
9169 here. */
9170 while (true)
9172 tree id;
9173 if (c_parser_next_token_is_not (parser, CPP_NAME))
9175 c_parser_error (parser, "expected identifier");
9176 break;
9178 id = c_parser_peek_token (parser)->value;
9179 list = chainon (list, build_tree_list (NULL_TREE, id));
9180 c_parser_consume_token (parser);
9181 if (c_parser_next_token_is (parser, CPP_COMMA))
9182 c_parser_consume_token (parser);
9183 else
9184 break;
9186 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9187 return list;
9190 /* Parse an objc-try-catch-finally-statement.
9192 objc-try-catch-finally-statement:
9193 @try compound-statement objc-catch-list[opt]
9194 @try compound-statement objc-catch-list[opt] @finally compound-statement
9196 objc-catch-list:
9197 @catch ( objc-catch-parameter-declaration ) compound-statement
9198 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9200 objc-catch-parameter-declaration:
9201 parameter-declaration
9202 '...'
9204 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9206 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9207 for C++. Keep them in sync. */
9209 static void
9210 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9212 location_t location;
9213 tree stmt;
9215 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9216 c_parser_consume_token (parser);
9217 location = c_parser_peek_token (parser)->location;
9218 objc_maybe_warn_exceptions (location);
9219 stmt = c_parser_compound_statement (parser);
9220 objc_begin_try_stmt (location, stmt);
9222 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9224 struct c_parm *parm;
9225 tree parameter_declaration = error_mark_node;
9226 bool seen_open_paren = false;
9228 c_parser_consume_token (parser);
9229 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9230 seen_open_paren = true;
9231 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9233 /* We have "@catch (...)" (where the '...' are literally
9234 what is in the code). Skip the '...'.
9235 parameter_declaration is set to NULL_TREE, and
9236 objc_being_catch_clauses() knows that that means
9237 '...'. */
9238 c_parser_consume_token (parser);
9239 parameter_declaration = NULL_TREE;
9241 else
9243 /* We have "@catch (NSException *exception)" or something
9244 like that. Parse the parameter declaration. */
9245 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9246 if (parm == NULL)
9247 parameter_declaration = error_mark_node;
9248 else
9249 parameter_declaration = grokparm (parm, NULL);
9251 if (seen_open_paren)
9252 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9253 else
9255 /* If there was no open parenthesis, we are recovering from
9256 an error, and we are trying to figure out what mistake
9257 the user has made. */
9259 /* If there is an immediate closing parenthesis, the user
9260 probably forgot the opening one (ie, they typed "@catch
9261 NSException *e)". Parse the closing parenthesis and keep
9262 going. */
9263 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9264 c_parser_consume_token (parser);
9266 /* If these is no immediate closing parenthesis, the user
9267 probably doesn't know that parenthesis are required at
9268 all (ie, they typed "@catch NSException *e"). So, just
9269 forget about the closing parenthesis and keep going. */
9271 objc_begin_catch_clause (parameter_declaration);
9272 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9273 c_parser_compound_statement_nostart (parser);
9274 objc_finish_catch_clause ();
9276 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9278 c_parser_consume_token (parser);
9279 location = c_parser_peek_token (parser)->location;
9280 stmt = c_parser_compound_statement (parser);
9281 objc_build_finally_clause (location, stmt);
9283 objc_finish_try_stmt ();
9286 /* Parse an objc-synchronized-statement.
9288 objc-synchronized-statement:
9289 @synchronized ( expression ) compound-statement
9292 static void
9293 c_parser_objc_synchronized_statement (c_parser *parser)
9295 location_t loc;
9296 tree expr, stmt;
9297 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9298 c_parser_consume_token (parser);
9299 loc = c_parser_peek_token (parser)->location;
9300 objc_maybe_warn_exceptions (loc);
9301 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9303 struct c_expr ce = c_parser_expression (parser);
9304 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9305 expr = ce.value;
9306 expr = c_fully_fold (expr, false, NULL);
9307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9309 else
9310 expr = error_mark_node;
9311 stmt = c_parser_compound_statement (parser);
9312 objc_build_synchronized (loc, expr, stmt);
9315 /* Parse an objc-selector; return NULL_TREE without an error if the
9316 next token is not an objc-selector.
9318 objc-selector:
9319 identifier
9320 one of
9321 enum struct union if else while do for switch case default
9322 break continue return goto asm sizeof typeof __alignof
9323 unsigned long const short volatile signed restrict _Complex
9324 in out inout bycopy byref oneway int char float double void _Bool
9325 _Atomic
9327 ??? Why this selection of keywords but not, for example, storage
9328 class specifiers? */
9330 static tree
9331 c_parser_objc_selector (c_parser *parser)
9333 c_token *token = c_parser_peek_token (parser);
9334 tree value = token->value;
9335 if (token->type == CPP_NAME)
9337 c_parser_consume_token (parser);
9338 return value;
9340 if (token->type != CPP_KEYWORD)
9341 return NULL_TREE;
9342 switch (token->keyword)
9344 case RID_ENUM:
9345 case RID_STRUCT:
9346 case RID_UNION:
9347 case RID_IF:
9348 case RID_ELSE:
9349 case RID_WHILE:
9350 case RID_DO:
9351 case RID_FOR:
9352 case RID_SWITCH:
9353 case RID_CASE:
9354 case RID_DEFAULT:
9355 case RID_BREAK:
9356 case RID_CONTINUE:
9357 case RID_RETURN:
9358 case RID_GOTO:
9359 case RID_ASM:
9360 case RID_SIZEOF:
9361 case RID_TYPEOF:
9362 case RID_ALIGNOF:
9363 case RID_UNSIGNED:
9364 case RID_LONG:
9365 case RID_CONST:
9366 case RID_SHORT:
9367 case RID_VOLATILE:
9368 case RID_SIGNED:
9369 case RID_RESTRICT:
9370 case RID_COMPLEX:
9371 case RID_IN:
9372 case RID_OUT:
9373 case RID_INOUT:
9374 case RID_BYCOPY:
9375 case RID_BYREF:
9376 case RID_ONEWAY:
9377 case RID_INT:
9378 case RID_CHAR:
9379 case RID_FLOAT:
9380 case RID_DOUBLE:
9381 case RID_VOID:
9382 case RID_BOOL:
9383 case RID_ATOMIC:
9384 case RID_AUTO_TYPE:
9385 case RID_INT_N_0:
9386 case RID_INT_N_1:
9387 case RID_INT_N_2:
9388 case RID_INT_N_3:
9389 c_parser_consume_token (parser);
9390 return value;
9391 default:
9392 return NULL_TREE;
9396 /* Parse an objc-selector-arg.
9398 objc-selector-arg:
9399 objc-selector
9400 objc-keywordname-list
9402 objc-keywordname-list:
9403 objc-keywordname
9404 objc-keywordname-list objc-keywordname
9406 objc-keywordname:
9407 objc-selector :
9411 static tree
9412 c_parser_objc_selector_arg (c_parser *parser)
9414 tree sel = c_parser_objc_selector (parser);
9415 tree list = NULL_TREE;
9416 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9417 return sel;
9418 while (true)
9420 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9421 return list;
9422 list = chainon (list, build_tree_list (sel, NULL_TREE));
9423 sel = c_parser_objc_selector (parser);
9424 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9425 break;
9427 return list;
9430 /* Parse an objc-receiver.
9432 objc-receiver:
9433 expression
9434 class-name
9435 type-name
9438 static tree
9439 c_parser_objc_receiver (c_parser *parser)
9441 location_t loc = c_parser_peek_token (parser)->location;
9443 if (c_parser_peek_token (parser)->type == CPP_NAME
9444 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9445 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9447 tree id = c_parser_peek_token (parser)->value;
9448 c_parser_consume_token (parser);
9449 return objc_get_class_reference (id);
9451 struct c_expr ce = c_parser_expression (parser);
9452 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9453 return c_fully_fold (ce.value, false, NULL);
9456 /* Parse objc-message-args.
9458 objc-message-args:
9459 objc-selector
9460 objc-keywordarg-list
9462 objc-keywordarg-list:
9463 objc-keywordarg
9464 objc-keywordarg-list objc-keywordarg
9466 objc-keywordarg:
9467 objc-selector : objc-keywordexpr
9468 : objc-keywordexpr
9471 static tree
9472 c_parser_objc_message_args (c_parser *parser)
9474 tree sel = c_parser_objc_selector (parser);
9475 tree list = NULL_TREE;
9476 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9477 return sel;
9478 while (true)
9480 tree keywordexpr;
9481 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9482 return error_mark_node;
9483 keywordexpr = c_parser_objc_keywordexpr (parser);
9484 list = chainon (list, build_tree_list (sel, keywordexpr));
9485 sel = c_parser_objc_selector (parser);
9486 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9487 break;
9489 return list;
9492 /* Parse an objc-keywordexpr.
9494 objc-keywordexpr:
9495 nonempty-expr-list
9498 static tree
9499 c_parser_objc_keywordexpr (c_parser *parser)
9501 tree ret;
9502 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9503 NULL, NULL, NULL, NULL);
9504 if (vec_safe_length (expr_list) == 1)
9506 /* Just return the expression, remove a level of
9507 indirection. */
9508 ret = (*expr_list)[0];
9510 else
9512 /* We have a comma expression, we will collapse later. */
9513 ret = build_tree_list_vec (expr_list);
9515 release_tree_vector (expr_list);
9516 return ret;
9519 /* A check, needed in several places, that ObjC interface, implementation or
9520 method definitions are not prefixed by incorrect items. */
9521 static bool
9522 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9523 struct c_declspecs *specs)
9525 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9526 || specs->typespec_kind != ctsk_none)
9528 c_parser_error (parser,
9529 "no type or storage class may be specified here,");
9530 c_parser_skip_to_end_of_block_or_statement (parser);
9531 return true;
9533 return false;
9536 /* Parse an Objective-C @property declaration. The syntax is:
9538 objc-property-declaration:
9539 '@property' objc-property-attributes[opt] struct-declaration ;
9541 objc-property-attributes:
9542 '(' objc-property-attribute-list ')'
9544 objc-property-attribute-list:
9545 objc-property-attribute
9546 objc-property-attribute-list, objc-property-attribute
9548 objc-property-attribute
9549 'getter' = identifier
9550 'setter' = identifier
9551 'readonly'
9552 'readwrite'
9553 'assign'
9554 'retain'
9555 'copy'
9556 'nonatomic'
9558 For example:
9559 @property NSString *name;
9560 @property (readonly) id object;
9561 @property (retain, nonatomic, getter=getTheName) id name;
9562 @property int a, b, c;
9564 PS: This function is identical to cp_parser_objc_at_propery_declaration
9565 for C++. Keep them in sync. */
9566 static void
9567 c_parser_objc_at_property_declaration (c_parser *parser)
9569 /* The following variables hold the attributes of the properties as
9570 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9571 seen. When we see an attribute, we set them to 'true' (if they
9572 are boolean properties) or to the identifier (if they have an
9573 argument, ie, for getter and setter). Note that here we only
9574 parse the list of attributes, check the syntax and accumulate the
9575 attributes that we find. objc_add_property_declaration() will
9576 then process the information. */
9577 bool property_assign = false;
9578 bool property_copy = false;
9579 tree property_getter_ident = NULL_TREE;
9580 bool property_nonatomic = false;
9581 bool property_readonly = false;
9582 bool property_readwrite = false;
9583 bool property_retain = false;
9584 tree property_setter_ident = NULL_TREE;
9586 /* 'properties' is the list of properties that we read. Usually a
9587 single one, but maybe more (eg, in "@property int a, b, c;" there
9588 are three). */
9589 tree properties;
9590 location_t loc;
9592 loc = c_parser_peek_token (parser)->location;
9593 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9595 c_parser_consume_token (parser); /* Eat '@property'. */
9597 /* Parse the optional attribute list... */
9598 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9600 /* Eat the '(' */
9601 c_parser_consume_token (parser);
9603 /* Property attribute keywords are valid now. */
9604 parser->objc_property_attr_context = true;
9606 while (true)
9608 bool syntax_error = false;
9609 c_token *token = c_parser_peek_token (parser);
9610 enum rid keyword;
9612 if (token->type != CPP_KEYWORD)
9614 if (token->type == CPP_CLOSE_PAREN)
9615 c_parser_error (parser, "expected identifier");
9616 else
9618 c_parser_consume_token (parser);
9619 c_parser_error (parser, "unknown property attribute");
9621 break;
9623 keyword = token->keyword;
9624 c_parser_consume_token (parser);
9625 switch (keyword)
9627 case RID_ASSIGN: property_assign = true; break;
9628 case RID_COPY: property_copy = true; break;
9629 case RID_NONATOMIC: property_nonatomic = true; break;
9630 case RID_READONLY: property_readonly = true; break;
9631 case RID_READWRITE: property_readwrite = true; break;
9632 case RID_RETAIN: property_retain = true; break;
9634 case RID_GETTER:
9635 case RID_SETTER:
9636 if (c_parser_next_token_is_not (parser, CPP_EQ))
9638 if (keyword == RID_GETTER)
9639 c_parser_error (parser,
9640 "missing %<=%> (after %<getter%> attribute)");
9641 else
9642 c_parser_error (parser,
9643 "missing %<=%> (after %<setter%> attribute)");
9644 syntax_error = true;
9645 break;
9647 c_parser_consume_token (parser); /* eat the = */
9648 if (c_parser_next_token_is_not (parser, CPP_NAME))
9650 c_parser_error (parser, "expected identifier");
9651 syntax_error = true;
9652 break;
9654 if (keyword == RID_SETTER)
9656 if (property_setter_ident != NULL_TREE)
9657 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9658 else
9659 property_setter_ident = c_parser_peek_token (parser)->value;
9660 c_parser_consume_token (parser);
9661 if (c_parser_next_token_is_not (parser, CPP_COLON))
9662 c_parser_error (parser, "setter name must terminate with %<:%>");
9663 else
9664 c_parser_consume_token (parser);
9666 else
9668 if (property_getter_ident != NULL_TREE)
9669 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9670 else
9671 property_getter_ident = c_parser_peek_token (parser)->value;
9672 c_parser_consume_token (parser);
9674 break;
9675 default:
9676 c_parser_error (parser, "unknown property attribute");
9677 syntax_error = true;
9678 break;
9681 if (syntax_error)
9682 break;
9684 if (c_parser_next_token_is (parser, CPP_COMMA))
9685 c_parser_consume_token (parser);
9686 else
9687 break;
9689 parser->objc_property_attr_context = false;
9690 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9692 /* ... and the property declaration(s). */
9693 properties = c_parser_struct_declaration (parser);
9695 if (properties == error_mark_node)
9697 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9698 parser->error = false;
9699 return;
9702 if (properties == NULL_TREE)
9703 c_parser_error (parser, "expected identifier");
9704 else
9706 /* Comma-separated properties are chained together in
9707 reverse order; add them one by one. */
9708 properties = nreverse (properties);
9710 for (; properties; properties = TREE_CHAIN (properties))
9711 objc_add_property_declaration (loc, copy_node (properties),
9712 property_readonly, property_readwrite,
9713 property_assign, property_retain,
9714 property_copy, property_nonatomic,
9715 property_getter_ident, property_setter_ident);
9718 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9719 parser->error = false;
9722 /* Parse an Objective-C @synthesize declaration. The syntax is:
9724 objc-synthesize-declaration:
9725 @synthesize objc-synthesize-identifier-list ;
9727 objc-synthesize-identifier-list:
9728 objc-synthesize-identifier
9729 objc-synthesize-identifier-list, objc-synthesize-identifier
9731 objc-synthesize-identifier
9732 identifier
9733 identifier = identifier
9735 For example:
9736 @synthesize MyProperty;
9737 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
9739 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
9740 for C++. Keep them in sync.
9742 static void
9743 c_parser_objc_at_synthesize_declaration (c_parser *parser)
9745 tree list = NULL_TREE;
9746 location_t loc;
9747 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
9748 loc = c_parser_peek_token (parser)->location;
9750 c_parser_consume_token (parser);
9751 while (true)
9753 tree property, ivar;
9754 if (c_parser_next_token_is_not (parser, CPP_NAME))
9756 c_parser_error (parser, "expected identifier");
9757 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9758 /* Once we find the semicolon, we can resume normal parsing.
9759 We have to reset parser->error manually because
9760 c_parser_skip_until_found() won't reset it for us if the
9761 next token is precisely a semicolon. */
9762 parser->error = false;
9763 return;
9765 property = c_parser_peek_token (parser)->value;
9766 c_parser_consume_token (parser);
9767 if (c_parser_next_token_is (parser, CPP_EQ))
9769 c_parser_consume_token (parser);
9770 if (c_parser_next_token_is_not (parser, CPP_NAME))
9772 c_parser_error (parser, "expected identifier");
9773 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9774 parser->error = false;
9775 return;
9777 ivar = c_parser_peek_token (parser)->value;
9778 c_parser_consume_token (parser);
9780 else
9781 ivar = NULL_TREE;
9782 list = chainon (list, build_tree_list (ivar, property));
9783 if (c_parser_next_token_is (parser, CPP_COMMA))
9784 c_parser_consume_token (parser);
9785 else
9786 break;
9788 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9789 objc_add_synthesize_declaration (loc, list);
9792 /* Parse an Objective-C @dynamic declaration. The syntax is:
9794 objc-dynamic-declaration:
9795 @dynamic identifier-list ;
9797 For example:
9798 @dynamic MyProperty;
9799 @dynamic MyProperty, AnotherProperty;
9801 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
9802 for C++. Keep them in sync.
9804 static void
9805 c_parser_objc_at_dynamic_declaration (c_parser *parser)
9807 tree list = NULL_TREE;
9808 location_t loc;
9809 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
9810 loc = c_parser_peek_token (parser)->location;
9812 c_parser_consume_token (parser);
9813 while (true)
9815 tree property;
9816 if (c_parser_next_token_is_not (parser, CPP_NAME))
9818 c_parser_error (parser, "expected identifier");
9819 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9820 parser->error = false;
9821 return;
9823 property = c_parser_peek_token (parser)->value;
9824 list = chainon (list, build_tree_list (NULL_TREE, property));
9825 c_parser_consume_token (parser);
9826 if (c_parser_next_token_is (parser, CPP_COMMA))
9827 c_parser_consume_token (parser);
9828 else
9829 break;
9831 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9832 objc_add_dynamic_declaration (loc, list);
9836 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
9837 should be considered, statements. ALLOW_STMT is true if we're within
9838 the context of a function and such pragmas are to be allowed. Returns
9839 true if we actually parsed such a pragma. */
9841 static bool
9842 c_parser_pragma (c_parser *parser, enum pragma_context context)
9844 unsigned int id;
9846 id = c_parser_peek_token (parser)->pragma_kind;
9847 gcc_assert (id != PRAGMA_NONE);
9849 switch (id)
9851 case PRAGMA_OACC_DECLARE:
9852 c_parser_oacc_declare (parser);
9853 return false;
9855 case PRAGMA_OACC_ENTER_DATA:
9856 c_parser_oacc_enter_exit_data (parser, true);
9857 return false;
9859 case PRAGMA_OACC_EXIT_DATA:
9860 c_parser_oacc_enter_exit_data (parser, false);
9861 return false;
9863 case PRAGMA_OACC_ROUTINE:
9864 c_parser_oacc_routine (parser, context);
9865 return false;
9867 case PRAGMA_OACC_UPDATE:
9868 if (context != pragma_compound)
9870 if (context == pragma_stmt)
9871 c_parser_error (parser, "%<#pragma acc update%> may only be "
9872 "used in compound statements");
9873 goto bad_stmt;
9875 c_parser_oacc_update (parser);
9876 return false;
9878 case PRAGMA_OMP_BARRIER:
9879 if (context != pragma_compound)
9881 if (context == pragma_stmt)
9882 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
9883 "used in compound statements");
9884 goto bad_stmt;
9886 c_parser_omp_barrier (parser);
9887 return false;
9889 case PRAGMA_OMP_FLUSH:
9890 if (context != pragma_compound)
9892 if (context == pragma_stmt)
9893 c_parser_error (parser, "%<#pragma omp flush%> may only be "
9894 "used in compound statements");
9895 goto bad_stmt;
9897 c_parser_omp_flush (parser);
9898 return false;
9900 case PRAGMA_OMP_TASKWAIT:
9901 if (context != pragma_compound)
9903 if (context == pragma_stmt)
9904 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
9905 "used in compound statements");
9906 goto bad_stmt;
9908 c_parser_omp_taskwait (parser);
9909 return false;
9911 case PRAGMA_OMP_TASKYIELD:
9912 if (context != pragma_compound)
9914 if (context == pragma_stmt)
9915 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
9916 "used in compound statements");
9917 goto bad_stmt;
9919 c_parser_omp_taskyield (parser);
9920 return false;
9922 case PRAGMA_OMP_CANCEL:
9923 if (context != pragma_compound)
9925 if (context == pragma_stmt)
9926 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
9927 "used in compound statements");
9928 goto bad_stmt;
9930 c_parser_omp_cancel (parser);
9931 return false;
9933 case PRAGMA_OMP_CANCELLATION_POINT:
9934 if (context != pragma_compound)
9936 if (context == pragma_stmt)
9937 c_parser_error (parser, "%<#pragma omp cancellation point%> may "
9938 "only be used in compound statements");
9939 goto bad_stmt;
9941 c_parser_omp_cancellation_point (parser);
9942 return false;
9944 case PRAGMA_OMP_THREADPRIVATE:
9945 c_parser_omp_threadprivate (parser);
9946 return false;
9948 case PRAGMA_OMP_TARGET:
9949 return c_parser_omp_target (parser, context);
9951 case PRAGMA_OMP_END_DECLARE_TARGET:
9952 c_parser_omp_end_declare_target (parser);
9953 return false;
9955 case PRAGMA_OMP_SECTION:
9956 error_at (c_parser_peek_token (parser)->location,
9957 "%<#pragma omp section%> may only be used in "
9958 "%<#pragma omp sections%> construct");
9959 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9960 return false;
9962 case PRAGMA_OMP_DECLARE_REDUCTION:
9963 c_parser_omp_declare (parser, context);
9964 return false;
9966 case PRAGMA_OMP_ORDERED:
9967 return c_parser_omp_ordered (parser, context);
9969 case PRAGMA_IVDEP:
9970 c_parser_consume_pragma (parser);
9971 c_parser_skip_to_pragma_eol (parser);
9972 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
9973 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
9974 && !c_parser_next_token_is_keyword (parser, RID_DO))
9976 c_parser_error (parser, "for, while or do statement expected");
9977 return false;
9979 if (c_parser_next_token_is_keyword (parser, RID_FOR))
9980 c_parser_for_statement (parser, true);
9981 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
9982 c_parser_while_statement (parser, true);
9983 else
9984 c_parser_do_statement (parser, true);
9985 return false;
9987 case PRAGMA_GCC_PCH_PREPROCESS:
9988 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
9989 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
9990 return false;
9992 case PRAGMA_CILK_SIMD:
9993 if (!c_parser_cilk_verify_simd (parser, context))
9994 return false;
9995 c_parser_consume_pragma (parser);
9996 c_parser_cilk_simd (parser);
9997 return false;
9998 case PRAGMA_CILK_GRAINSIZE:
9999 if (!flag_cilkplus)
10001 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10002 " enabled");
10003 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10004 return false;
10006 if (context == pragma_external)
10008 error_at (c_parser_peek_token (parser)->location,
10009 "%<#pragma grainsize%> must be inside a function");
10010 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10011 return false;
10013 c_parser_cilk_grainsize (parser);
10014 return false;
10016 default:
10017 if (id < PRAGMA_FIRST_EXTERNAL)
10019 if (context != pragma_stmt && context != pragma_compound)
10021 bad_stmt:
10022 c_parser_error (parser, "expected declaration specifiers");
10023 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10024 return false;
10026 c_parser_omp_construct (parser);
10027 return true;
10029 break;
10032 c_parser_consume_pragma (parser);
10033 c_invoke_pragma_handler (id);
10035 /* Skip to EOL, but suppress any error message. Those will have been
10036 generated by the handler routine through calling error, as opposed
10037 to calling c_parser_error. */
10038 parser->error = true;
10039 c_parser_skip_to_pragma_eol (parser);
10041 return false;
10044 /* The interface the pragma parsers have to the lexer. */
10046 enum cpp_ttype
10047 pragma_lex (tree *value, location_t *loc)
10049 c_token *tok = c_parser_peek_token (the_parser);
10050 enum cpp_ttype ret = tok->type;
10052 *value = tok->value;
10053 if (loc)
10054 *loc = tok->location;
10056 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10057 ret = CPP_EOF;
10058 else
10060 if (ret == CPP_KEYWORD)
10061 ret = CPP_NAME;
10062 c_parser_consume_token (the_parser);
10065 return ret;
10068 static void
10069 c_parser_pragma_pch_preprocess (c_parser *parser)
10071 tree name = NULL;
10073 c_parser_consume_pragma (parser);
10074 if (c_parser_next_token_is (parser, CPP_STRING))
10076 name = c_parser_peek_token (parser)->value;
10077 c_parser_consume_token (parser);
10079 else
10080 c_parser_error (parser, "expected string literal");
10081 c_parser_skip_to_pragma_eol (parser);
10083 if (name)
10084 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10087 /* OpenACC and OpenMP parsing routines. */
10089 /* Returns name of the next clause.
10090 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10091 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10092 returned and the token is consumed. */
10094 static pragma_omp_clause
10095 c_parser_omp_clause_name (c_parser *parser)
10097 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10099 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10100 result = PRAGMA_OACC_CLAUSE_AUTO;
10101 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10102 result = PRAGMA_OMP_CLAUSE_IF;
10103 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10104 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10105 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10106 result = PRAGMA_OMP_CLAUSE_FOR;
10107 else if (c_parser_next_token_is (parser, CPP_NAME))
10109 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10111 switch (p[0])
10113 case 'a':
10114 if (!strcmp ("aligned", p))
10115 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10116 else if (!strcmp ("async", p))
10117 result = PRAGMA_OACC_CLAUSE_ASYNC;
10118 break;
10119 case 'c':
10120 if (!strcmp ("collapse", p))
10121 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10122 else if (!strcmp ("copy", p))
10123 result = PRAGMA_OACC_CLAUSE_COPY;
10124 else if (!strcmp ("copyin", p))
10125 result = PRAGMA_OMP_CLAUSE_COPYIN;
10126 else if (!strcmp ("copyout", p))
10127 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10128 else if (!strcmp ("copyprivate", p))
10129 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10130 else if (!strcmp ("create", p))
10131 result = PRAGMA_OACC_CLAUSE_CREATE;
10132 break;
10133 case 'd':
10134 if (!strcmp ("defaultmap", p))
10135 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10136 else if (!strcmp ("delete", p))
10137 result = PRAGMA_OACC_CLAUSE_DELETE;
10138 else if (!strcmp ("depend", p))
10139 result = PRAGMA_OMP_CLAUSE_DEPEND;
10140 else if (!strcmp ("device", p))
10141 result = PRAGMA_OMP_CLAUSE_DEVICE;
10142 else if (!strcmp ("deviceptr", p))
10143 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10144 else if (!strcmp ("device_resident", p))
10145 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10146 else if (!strcmp ("dist_schedule", p))
10147 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10148 break;
10149 case 'f':
10150 if (!strcmp ("final", p))
10151 result = PRAGMA_OMP_CLAUSE_FINAL;
10152 else if (!strcmp ("firstprivate", p))
10153 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10154 else if (!strcmp ("from", p))
10155 result = PRAGMA_OMP_CLAUSE_FROM;
10156 break;
10157 case 'g':
10158 if (!strcmp ("gang", p))
10159 result = PRAGMA_OACC_CLAUSE_GANG;
10160 else if (!strcmp ("grainsize", p))
10161 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10162 break;
10163 case 'h':
10164 if (!strcmp ("hint", p))
10165 result = PRAGMA_OMP_CLAUSE_HINT;
10166 else if (!strcmp ("host", p))
10167 result = PRAGMA_OACC_CLAUSE_HOST;
10168 break;
10169 case 'i':
10170 if (!strcmp ("inbranch", p))
10171 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10172 else if (!strcmp ("independent", p))
10173 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10174 else if (!strcmp ("is_device_ptr", p))
10175 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10176 break;
10177 case 'l':
10178 if (!strcmp ("lastprivate", p))
10179 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10180 else if (!strcmp ("linear", p))
10181 result = PRAGMA_OMP_CLAUSE_LINEAR;
10182 else if (!strcmp ("link", p))
10183 result = PRAGMA_OMP_CLAUSE_LINK;
10184 break;
10185 case 'm':
10186 if (!strcmp ("map", p))
10187 result = PRAGMA_OMP_CLAUSE_MAP;
10188 else if (!strcmp ("mergeable", p))
10189 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10190 else if (flag_cilkplus && !strcmp ("mask", p))
10191 result = PRAGMA_CILK_CLAUSE_MASK;
10192 break;
10193 case 'n':
10194 if (!strcmp ("nogroup", p))
10195 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10196 else if (!strcmp ("notinbranch", p))
10197 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10198 else if (!strcmp ("nowait", p))
10199 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10200 else if (!strcmp ("num_gangs", p))
10201 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10202 else if (!strcmp ("num_tasks", p))
10203 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10204 else if (!strcmp ("num_teams", p))
10205 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10206 else if (!strcmp ("num_threads", p))
10207 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10208 else if (!strcmp ("num_workers", p))
10209 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10210 else if (flag_cilkplus && !strcmp ("nomask", p))
10211 result = PRAGMA_CILK_CLAUSE_NOMASK;
10212 break;
10213 case 'o':
10214 if (!strcmp ("ordered", p))
10215 result = PRAGMA_OMP_CLAUSE_ORDERED;
10216 break;
10217 case 'p':
10218 if (!strcmp ("parallel", p))
10219 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10220 else if (!strcmp ("present", p))
10221 result = PRAGMA_OACC_CLAUSE_PRESENT;
10222 else if (!strcmp ("present_or_copy", p)
10223 || !strcmp ("pcopy", p))
10224 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10225 else if (!strcmp ("present_or_copyin", p)
10226 || !strcmp ("pcopyin", p))
10227 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10228 else if (!strcmp ("present_or_copyout", p)
10229 || !strcmp ("pcopyout", p))
10230 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10231 else if (!strcmp ("present_or_create", p)
10232 || !strcmp ("pcreate", p))
10233 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10234 else if (!strcmp ("priority", p))
10235 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10236 else if (!strcmp ("private", p))
10237 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10238 else if (!strcmp ("proc_bind", p))
10239 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10240 break;
10241 case 'r':
10242 if (!strcmp ("reduction", p))
10243 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10244 break;
10245 case 's':
10246 if (!strcmp ("safelen", p))
10247 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10248 else if (!strcmp ("schedule", p))
10249 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10250 else if (!strcmp ("sections", p))
10251 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10252 else if (!strcmp ("seq", p))
10253 result = PRAGMA_OACC_CLAUSE_SEQ;
10254 else if (!strcmp ("shared", p))
10255 result = PRAGMA_OMP_CLAUSE_SHARED;
10256 else if (!strcmp ("simd", p))
10257 result = PRAGMA_OMP_CLAUSE_SIMD;
10258 else if (!strcmp ("simdlen", p))
10259 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10260 else if (!strcmp ("self", p))
10261 result = PRAGMA_OACC_CLAUSE_SELF;
10262 break;
10263 case 't':
10264 if (!strcmp ("taskgroup", p))
10265 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10266 else if (!strcmp ("thread_limit", p))
10267 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10268 else if (!strcmp ("threads", p))
10269 result = PRAGMA_OMP_CLAUSE_THREADS;
10270 else if (!strcmp ("tile", p))
10271 result = PRAGMA_OACC_CLAUSE_TILE;
10272 else if (!strcmp ("to", p))
10273 result = PRAGMA_OMP_CLAUSE_TO;
10274 break;
10275 case 'u':
10276 if (!strcmp ("uniform", p))
10277 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10278 else if (!strcmp ("untied", p))
10279 result = PRAGMA_OMP_CLAUSE_UNTIED;
10280 else if (!strcmp ("use_device_ptr", p))
10281 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10282 break;
10283 case 'v':
10284 if (!strcmp ("vector", p))
10285 result = PRAGMA_OACC_CLAUSE_VECTOR;
10286 else if (!strcmp ("vector_length", p))
10287 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10288 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10289 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10290 break;
10291 case 'w':
10292 if (!strcmp ("wait", p))
10293 result = PRAGMA_OACC_CLAUSE_WAIT;
10294 else if (!strcmp ("worker", p))
10295 result = PRAGMA_OACC_CLAUSE_WORKER;
10296 break;
10300 if (result != PRAGMA_OMP_CLAUSE_NONE)
10301 c_parser_consume_token (parser);
10303 return result;
10306 /* Validate that a clause of the given type does not already exist. */
10308 static void
10309 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10310 const char *name)
10312 tree c;
10314 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10315 if (OMP_CLAUSE_CODE (c) == code)
10317 location_t loc = OMP_CLAUSE_LOCATION (c);
10318 error_at (loc, "too many %qs clauses", name);
10319 break;
10323 /* OpenACC 2.0
10324 Parse wait clause or wait directive parameters. */
10326 static tree
10327 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10329 vec<tree, va_gc> *args;
10330 tree t, args_tree;
10332 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10333 return list;
10335 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10337 if (args->length () == 0)
10339 c_parser_error (parser, "expected integer expression before ')'");
10340 release_tree_vector (args);
10341 return list;
10344 args_tree = build_tree_list_vec (args);
10346 for (t = args_tree; t; t = TREE_CHAIN (t))
10348 tree targ = TREE_VALUE (t);
10350 if (targ != error_mark_node)
10352 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10354 c_parser_error (parser, "expression must be integral");
10355 targ = error_mark_node;
10357 else
10359 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10361 OMP_CLAUSE_DECL (c) = targ;
10362 OMP_CLAUSE_CHAIN (c) = list;
10363 list = c;
10368 release_tree_vector (args);
10369 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10370 return list;
10373 /* OpenACC 2.0, OpenMP 2.5:
10374 variable-list:
10375 identifier
10376 variable-list , identifier
10378 If KIND is nonzero, create the appropriate node and install the
10379 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10380 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10382 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10383 return the list created. */
10385 static tree
10386 c_parser_omp_variable_list (c_parser *parser,
10387 location_t clause_loc,
10388 enum omp_clause_code kind, tree list)
10390 if (c_parser_next_token_is_not (parser, CPP_NAME)
10391 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10392 c_parser_error (parser, "expected identifier");
10394 while (c_parser_next_token_is (parser, CPP_NAME)
10395 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10397 tree t = lookup_name (c_parser_peek_token (parser)->value);
10399 if (t == NULL_TREE)
10401 undeclared_variable (c_parser_peek_token (parser)->location,
10402 c_parser_peek_token (parser)->value);
10403 t = error_mark_node;
10406 c_parser_consume_token (parser);
10408 if (t == error_mark_node)
10410 else if (kind != 0)
10412 switch (kind)
10414 case OMP_CLAUSE__CACHE_:
10415 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10417 c_parser_error (parser, "expected %<[%>");
10418 t = error_mark_node;
10419 break;
10421 /* FALLTHROUGH */
10422 case OMP_CLAUSE_MAP:
10423 case OMP_CLAUSE_FROM:
10424 case OMP_CLAUSE_TO:
10425 while (c_parser_next_token_is (parser, CPP_DOT))
10427 location_t op_loc = c_parser_peek_token (parser)->location;
10428 c_parser_consume_token (parser);
10429 if (!c_parser_next_token_is (parser, CPP_NAME))
10431 c_parser_error (parser, "expected identifier");
10432 t = error_mark_node;
10433 break;
10435 tree ident = c_parser_peek_token (parser)->value;
10436 c_parser_consume_token (parser);
10437 t = build_component_ref (op_loc, t, ident);
10439 /* FALLTHROUGH */
10440 case OMP_CLAUSE_DEPEND:
10441 case OMP_CLAUSE_REDUCTION:
10442 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10444 tree low_bound = NULL_TREE, length = NULL_TREE;
10446 c_parser_consume_token (parser);
10447 if (!c_parser_next_token_is (parser, CPP_COLON))
10449 low_bound = c_parser_expression (parser).value;
10450 mark_exp_read (low_bound);
10452 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10453 length = integer_one_node;
10454 else
10456 /* Look for `:'. */
10457 if (!c_parser_require (parser, CPP_COLON,
10458 "expected %<:%>"))
10460 t = error_mark_node;
10461 break;
10463 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10465 length = c_parser_expression (parser).value;
10466 mark_exp_read (length);
10469 /* Look for the closing `]'. */
10470 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10471 "expected %<]%>"))
10473 t = error_mark_node;
10474 break;
10477 if (kind == OMP_CLAUSE__CACHE_)
10479 if (TREE_CODE (low_bound) != INTEGER_CST
10480 && !TREE_READONLY (low_bound))
10482 error_at (clause_loc,
10483 "%qD is not a constant", low_bound);
10484 t = error_mark_node;
10487 if (TREE_CODE (length) != INTEGER_CST
10488 && !TREE_READONLY (length))
10490 error_at (clause_loc,
10491 "%qD is not a constant", length);
10492 t = error_mark_node;
10496 t = tree_cons (low_bound, length, t);
10498 break;
10499 default:
10500 break;
10503 if (t != error_mark_node)
10505 tree u = build_omp_clause (clause_loc, kind);
10506 OMP_CLAUSE_DECL (u) = t;
10507 OMP_CLAUSE_CHAIN (u) = list;
10508 list = u;
10511 else
10512 list = tree_cons (t, NULL_TREE, list);
10514 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10515 break;
10517 c_parser_consume_token (parser);
10520 return list;
10523 /* Similarly, but expect leading and trailing parenthesis. This is a very
10524 common case for OpenACC and OpenMP clauses. */
10526 static tree
10527 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10528 tree list)
10530 /* The clauses location. */
10531 location_t loc = c_parser_peek_token (parser)->location;
10533 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10535 list = c_parser_omp_variable_list (parser, loc, kind, list);
10536 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10538 return list;
10541 /* OpenACC 2.0:
10542 copy ( variable-list )
10543 copyin ( variable-list )
10544 copyout ( variable-list )
10545 create ( variable-list )
10546 delete ( variable-list )
10547 present ( variable-list )
10548 present_or_copy ( variable-list )
10549 pcopy ( variable-list )
10550 present_or_copyin ( variable-list )
10551 pcopyin ( variable-list )
10552 present_or_copyout ( variable-list )
10553 pcopyout ( variable-list )
10554 present_or_create ( variable-list )
10555 pcreate ( variable-list ) */
10557 static tree
10558 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10559 tree list)
10561 enum gomp_map_kind kind;
10562 switch (c_kind)
10564 case PRAGMA_OACC_CLAUSE_COPY:
10565 kind = GOMP_MAP_FORCE_TOFROM;
10566 break;
10567 case PRAGMA_OACC_CLAUSE_COPYIN:
10568 kind = GOMP_MAP_FORCE_TO;
10569 break;
10570 case PRAGMA_OACC_CLAUSE_COPYOUT:
10571 kind = GOMP_MAP_FORCE_FROM;
10572 break;
10573 case PRAGMA_OACC_CLAUSE_CREATE:
10574 kind = GOMP_MAP_FORCE_ALLOC;
10575 break;
10576 case PRAGMA_OACC_CLAUSE_DELETE:
10577 kind = GOMP_MAP_FORCE_DEALLOC;
10578 break;
10579 case PRAGMA_OACC_CLAUSE_DEVICE:
10580 kind = GOMP_MAP_FORCE_TO;
10581 break;
10582 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
10583 kind = GOMP_MAP_DEVICE_RESIDENT;
10584 break;
10585 case PRAGMA_OACC_CLAUSE_HOST:
10586 case PRAGMA_OACC_CLAUSE_SELF:
10587 kind = GOMP_MAP_FORCE_FROM;
10588 break;
10589 case PRAGMA_OACC_CLAUSE_LINK:
10590 kind = GOMP_MAP_LINK;
10591 break;
10592 case PRAGMA_OACC_CLAUSE_PRESENT:
10593 kind = GOMP_MAP_FORCE_PRESENT;
10594 break;
10595 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10596 kind = GOMP_MAP_TOFROM;
10597 break;
10598 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10599 kind = GOMP_MAP_TO;
10600 break;
10601 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10602 kind = GOMP_MAP_FROM;
10603 break;
10604 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10605 kind = GOMP_MAP_ALLOC;
10606 break;
10607 default:
10608 gcc_unreachable ();
10610 tree nl, c;
10611 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10613 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10614 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10616 return nl;
10619 /* OpenACC 2.0:
10620 deviceptr ( variable-list ) */
10622 static tree
10623 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10625 location_t loc = c_parser_peek_token (parser)->location;
10626 tree vars, t;
10628 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10629 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10630 variable-list must only allow for pointer variables. */
10631 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10632 for (t = vars; t && t; t = TREE_CHAIN (t))
10634 tree v = TREE_PURPOSE (t);
10636 /* FIXME diagnostics: Ideally we should keep individual
10637 locations for all the variables in the var list to make the
10638 following errors more precise. Perhaps
10639 c_parser_omp_var_list_parens() should construct a list of
10640 locations to go along with the var list. */
10642 if (!VAR_P (v))
10643 error_at (loc, "%qD is not a variable", v);
10644 else if (TREE_TYPE (v) == error_mark_node)
10646 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10647 error_at (loc, "%qD is not a pointer variable", v);
10649 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10650 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10651 OMP_CLAUSE_DECL (u) = v;
10652 OMP_CLAUSE_CHAIN (u) = list;
10653 list = u;
10656 return list;
10659 /* OpenACC 2.0, OpenMP 3.0:
10660 collapse ( constant-expression ) */
10662 static tree
10663 c_parser_omp_clause_collapse (c_parser *parser, tree list)
10665 tree c, num = error_mark_node;
10666 HOST_WIDE_INT n;
10667 location_t loc;
10669 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
10671 loc = c_parser_peek_token (parser)->location;
10672 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10674 num = c_parser_expr_no_commas (parser, NULL).value;
10675 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10677 if (num == error_mark_node)
10678 return list;
10679 mark_exp_read (num);
10680 num = c_fully_fold (num, false, NULL);
10681 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
10682 || !tree_fits_shwi_p (num)
10683 || (n = tree_to_shwi (num)) <= 0
10684 || (int) n != n)
10686 error_at (loc,
10687 "collapse argument needs positive constant integer expression");
10688 return list;
10690 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
10691 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
10692 OMP_CLAUSE_CHAIN (c) = list;
10693 return c;
10696 /* OpenMP 2.5:
10697 copyin ( variable-list ) */
10699 static tree
10700 c_parser_omp_clause_copyin (c_parser *parser, tree list)
10702 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
10705 /* OpenMP 2.5:
10706 copyprivate ( variable-list ) */
10708 static tree
10709 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
10711 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
10714 /* OpenMP 2.5:
10715 default ( shared | none )
10717 OpenACC 2.0:
10718 default (none) */
10720 static tree
10721 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
10723 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
10724 location_t loc = c_parser_peek_token (parser)->location;
10725 tree c;
10727 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10728 return list;
10729 if (c_parser_next_token_is (parser, CPP_NAME))
10731 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10733 switch (p[0])
10735 case 'n':
10736 if (strcmp ("none", p) != 0)
10737 goto invalid_kind;
10738 kind = OMP_CLAUSE_DEFAULT_NONE;
10739 break;
10741 case 's':
10742 if (strcmp ("shared", p) != 0 || is_oacc)
10743 goto invalid_kind;
10744 kind = OMP_CLAUSE_DEFAULT_SHARED;
10745 break;
10747 default:
10748 goto invalid_kind;
10751 c_parser_consume_token (parser);
10753 else
10755 invalid_kind:
10756 if (is_oacc)
10757 c_parser_error (parser, "expected %<none%>");
10758 else
10759 c_parser_error (parser, "expected %<none%> or %<shared%>");
10761 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10763 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
10764 return list;
10766 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
10767 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
10768 OMP_CLAUSE_CHAIN (c) = list;
10769 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
10771 return c;
10774 /* OpenMP 2.5:
10775 firstprivate ( variable-list ) */
10777 static tree
10778 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
10780 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
10783 /* OpenMP 3.1:
10784 final ( expression ) */
10786 static tree
10787 c_parser_omp_clause_final (c_parser *parser, tree list)
10789 location_t loc = c_parser_peek_token (parser)->location;
10790 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10792 tree t = c_parser_paren_condition (parser);
10793 tree c;
10795 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
10797 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
10798 OMP_CLAUSE_FINAL_EXPR (c) = t;
10799 OMP_CLAUSE_CHAIN (c) = list;
10800 list = c;
10802 else
10803 c_parser_error (parser, "expected %<(%>");
10805 return list;
10808 /* OpenACC, OpenMP 2.5:
10809 if ( expression )
10811 OpenMP 4.5:
10812 if ( directive-name-modifier : expression )
10814 directive-name-modifier:
10815 parallel | task | taskloop | target data | target | target update
10816 | target enter data | target exit data */
10818 static tree
10819 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
10821 location_t location = c_parser_peek_token (parser)->location;
10822 enum tree_code if_modifier = ERROR_MARK;
10824 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10825 return list;
10827 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
10829 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10830 int n = 2;
10831 if (strcmp (p, "parallel") == 0)
10832 if_modifier = OMP_PARALLEL;
10833 else if (strcmp (p, "task") == 0)
10834 if_modifier = OMP_TASK;
10835 else if (strcmp (p, "taskloop") == 0)
10836 if_modifier = OMP_TASKLOOP;
10837 else if (strcmp (p, "target") == 0)
10839 if_modifier = OMP_TARGET;
10840 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
10842 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
10843 if (strcmp ("data", p) == 0)
10844 if_modifier = OMP_TARGET_DATA;
10845 else if (strcmp ("update", p) == 0)
10846 if_modifier = OMP_TARGET_UPDATE;
10847 else if (strcmp ("enter", p) == 0)
10848 if_modifier = OMP_TARGET_ENTER_DATA;
10849 else if (strcmp ("exit", p) == 0)
10850 if_modifier = OMP_TARGET_EXIT_DATA;
10851 if (if_modifier != OMP_TARGET)
10853 n = 3;
10854 c_parser_consume_token (parser);
10856 else
10858 location_t loc = c_parser_peek_2nd_token (parser)->location;
10859 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
10860 "or %<exit%>");
10861 if_modifier = ERROR_MARK;
10863 if (if_modifier == OMP_TARGET_ENTER_DATA
10864 || if_modifier == OMP_TARGET_EXIT_DATA)
10866 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
10868 p = IDENTIFIER_POINTER
10869 (c_parser_peek_2nd_token (parser)->value);
10870 if (strcmp ("data", p) == 0)
10871 n = 4;
10873 if (n == 4)
10874 c_parser_consume_token (parser);
10875 else
10877 location_t loc
10878 = c_parser_peek_2nd_token (parser)->location;
10879 error_at (loc, "expected %<data%>");
10880 if_modifier = ERROR_MARK;
10885 if (if_modifier != ERROR_MARK)
10887 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
10889 c_parser_consume_token (parser);
10890 c_parser_consume_token (parser);
10892 else
10894 if (n > 2)
10896 location_t loc = c_parser_peek_2nd_token (parser)->location;
10897 error_at (loc, "expected %<:%>");
10899 if_modifier = ERROR_MARK;
10904 tree t = c_parser_condition (parser), c;
10905 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10907 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
10908 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
10910 if (if_modifier != ERROR_MARK
10911 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
10913 const char *p = NULL;
10914 switch (if_modifier)
10916 case OMP_PARALLEL: p = "parallel"; break;
10917 case OMP_TASK: p = "task"; break;
10918 case OMP_TASKLOOP: p = "taskloop"; break;
10919 case OMP_TARGET_DATA: p = "target data"; break;
10920 case OMP_TARGET: p = "target"; break;
10921 case OMP_TARGET_UPDATE: p = "target update"; break;
10922 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
10923 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
10924 default: gcc_unreachable ();
10926 error_at (location, "too many %<if%> clauses with %qs modifier",
10928 return list;
10930 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
10932 if (!is_omp)
10933 error_at (location, "too many %<if%> clauses");
10934 else
10935 error_at (location, "too many %<if%> clauses without modifier");
10936 return list;
10938 else if (if_modifier == ERROR_MARK
10939 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
10941 error_at (location, "if any %<if%> clause has modifier, then all "
10942 "%<if%> clauses have to use modifier");
10943 return list;
10947 c = build_omp_clause (location, OMP_CLAUSE_IF);
10948 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
10949 OMP_CLAUSE_IF_EXPR (c) = t;
10950 OMP_CLAUSE_CHAIN (c) = list;
10951 return c;
10954 /* OpenMP 2.5:
10955 lastprivate ( variable-list ) */
10957 static tree
10958 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
10960 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
10963 /* OpenMP 3.1:
10964 mergeable */
10966 static tree
10967 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10969 tree c;
10971 /* FIXME: Should we allow duplicates? */
10972 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
10974 c = build_omp_clause (c_parser_peek_token (parser)->location,
10975 OMP_CLAUSE_MERGEABLE);
10976 OMP_CLAUSE_CHAIN (c) = list;
10978 return c;
10981 /* OpenMP 2.5:
10982 nowait */
10984 static tree
10985 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
10987 tree c;
10988 location_t loc = c_parser_peek_token (parser)->location;
10990 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
10992 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
10993 OMP_CLAUSE_CHAIN (c) = list;
10994 return c;
10997 /* OpenACC:
10998 num_gangs ( expression ) */
11000 static tree
11001 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11003 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11004 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11006 location_t expr_loc = c_parser_peek_token (parser)->location;
11007 tree c, t = c_parser_expression (parser).value;
11008 mark_exp_read (t);
11009 t = c_fully_fold (t, false, NULL);
11011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11013 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11015 c_parser_error (parser, "expected integer expression");
11016 return list;
11019 /* Attempt to statically determine when the number isn't positive. */
11020 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11021 build_int_cst (TREE_TYPE (t), 0));
11022 protected_set_expr_location (c, expr_loc);
11023 if (c == boolean_true_node)
11025 warning_at (expr_loc, 0,
11026 "%<num_gangs%> value must be positive");
11027 t = integer_one_node;
11030 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11032 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11033 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11034 OMP_CLAUSE_CHAIN (c) = list;
11035 list = c;
11038 return list;
11041 /* OpenMP 2.5:
11042 num_threads ( expression ) */
11044 static tree
11045 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11047 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11048 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11050 location_t expr_loc = c_parser_peek_token (parser)->location;
11051 tree c, t = c_parser_expression (parser).value;
11052 mark_exp_read (t);
11053 t = c_fully_fold (t, false, NULL);
11055 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11057 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11059 c_parser_error (parser, "expected integer expression");
11060 return list;
11063 /* Attempt to statically determine when the number isn't positive. */
11064 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11065 build_int_cst (TREE_TYPE (t), 0));
11066 protected_set_expr_location (c, expr_loc);
11067 if (c == boolean_true_node)
11069 warning_at (expr_loc, 0,
11070 "%<num_threads%> value must be positive");
11071 t = integer_one_node;
11074 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11076 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11077 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11078 OMP_CLAUSE_CHAIN (c) = list;
11079 list = c;
11082 return list;
11085 /* OpenMP 4.5:
11086 num_tasks ( expression ) */
11088 static tree
11089 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11091 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11092 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11094 location_t expr_loc = c_parser_peek_token (parser)->location;
11095 tree c, t = c_parser_expression (parser).value;
11096 mark_exp_read (t);
11097 t = c_fully_fold (t, false, NULL);
11099 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11101 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11103 c_parser_error (parser, "expected integer expression");
11104 return list;
11107 /* Attempt to statically determine when the number isn't positive. */
11108 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11109 build_int_cst (TREE_TYPE (t), 0));
11110 if (CAN_HAVE_LOCATION_P (c))
11111 SET_EXPR_LOCATION (c, expr_loc);
11112 if (c == boolean_true_node)
11114 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11115 t = integer_one_node;
11118 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11120 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11121 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11122 OMP_CLAUSE_CHAIN (c) = list;
11123 list = c;
11126 return list;
11129 /* OpenMP 4.5:
11130 grainsize ( expression ) */
11132 static tree
11133 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11135 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11136 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11138 location_t expr_loc = c_parser_peek_token (parser)->location;
11139 tree c, t = c_parser_expression (parser).value;
11140 mark_exp_read (t);
11141 t = c_fully_fold (t, false, NULL);
11143 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11145 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11147 c_parser_error (parser, "expected integer expression");
11148 return list;
11151 /* Attempt to statically determine when the number isn't positive. */
11152 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11153 build_int_cst (TREE_TYPE (t), 0));
11154 if (CAN_HAVE_LOCATION_P (c))
11155 SET_EXPR_LOCATION (c, expr_loc);
11156 if (c == boolean_true_node)
11158 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11159 t = integer_one_node;
11162 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11164 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11165 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11166 OMP_CLAUSE_CHAIN (c) = list;
11167 list = c;
11170 return list;
11173 /* OpenMP 4.5:
11174 priority ( expression ) */
11176 static tree
11177 c_parser_omp_clause_priority (c_parser *parser, tree list)
11179 location_t priority_loc = c_parser_peek_token (parser)->location;
11180 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11182 location_t expr_loc = c_parser_peek_token (parser)->location;
11183 tree c, t = c_parser_expression (parser).value;
11184 mark_exp_read (t);
11185 t = c_fully_fold (t, false, NULL);
11187 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11189 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11191 c_parser_error (parser, "expected integer expression");
11192 return list;
11195 /* Attempt to statically determine when the number isn't
11196 non-negative. */
11197 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11198 build_int_cst (TREE_TYPE (t), 0));
11199 if (CAN_HAVE_LOCATION_P (c))
11200 SET_EXPR_LOCATION (c, expr_loc);
11201 if (c == boolean_true_node)
11203 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11204 t = integer_one_node;
11207 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11209 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11210 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11211 OMP_CLAUSE_CHAIN (c) = list;
11212 list = c;
11215 return list;
11218 /* OpenMP 4.5:
11219 hint ( expression ) */
11221 static tree
11222 c_parser_omp_clause_hint (c_parser *parser, tree list)
11224 location_t hint_loc = c_parser_peek_token (parser)->location;
11225 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11227 tree c, t = c_parser_expression (parser).value;
11228 mark_exp_read (t);
11229 t = c_fully_fold (t, false, NULL);
11231 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11233 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11235 c_parser_error (parser, "expected integer expression");
11236 return list;
11239 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11241 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11242 OMP_CLAUSE_HINT_EXPR (c) = t;
11243 OMP_CLAUSE_CHAIN (c) = list;
11244 list = c;
11247 return list;
11250 /* OpenMP 4.5:
11251 defaultmap ( tofrom : scalar ) */
11253 static tree
11254 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11256 location_t loc = c_parser_peek_token (parser)->location;
11257 tree c;
11258 const char *p;
11260 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11261 return list;
11262 if (!c_parser_next_token_is (parser, CPP_NAME))
11264 c_parser_error (parser, "expected %<tofrom%>");
11265 goto out_err;
11267 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11268 if (strcmp (p, "tofrom") != 0)
11270 c_parser_error (parser, "expected %<tofrom%>");
11271 goto out_err;
11273 c_parser_consume_token (parser);
11274 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11275 goto out_err;
11276 if (!c_parser_next_token_is (parser, CPP_NAME))
11278 c_parser_error (parser, "expected %<scalar%>");
11279 goto out_err;
11281 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11282 if (strcmp (p, "scalar") != 0)
11284 c_parser_error (parser, "expected %<scalar%>");
11285 goto out_err;
11287 c_parser_consume_token (parser);
11288 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11289 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11290 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11291 OMP_CLAUSE_CHAIN (c) = list;
11292 return c;
11294 out_err:
11295 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11296 return list;
11299 /* OpenMP 4.5:
11300 use_device_ptr ( variable-list ) */
11302 static tree
11303 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11305 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11306 list);
11309 /* OpenMP 4.5:
11310 is_device_ptr ( variable-list ) */
11312 static tree
11313 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11315 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11318 /* OpenACC:
11319 num_workers ( expression ) */
11321 static tree
11322 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11324 location_t num_workers_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 protected_set_expr_location (c, expr_loc);
11344 if (c == boolean_true_node)
11346 warning_at (expr_loc, 0,
11347 "%<num_workers%> value must be positive");
11348 t = integer_one_node;
11351 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11353 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11354 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11355 OMP_CLAUSE_CHAIN (c) = list;
11356 list = c;
11359 return list;
11362 /* OpenACC:
11364 gang [( gang-arg-list )]
11365 worker [( [num:] int-expr )]
11366 vector [( [length:] int-expr )]
11368 where gang-arg is one of:
11370 [num:] int-expr
11371 static: size-expr
11373 and size-expr may be:
11376 int-expr
11379 static tree
11380 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11381 const char *str, tree list)
11383 const char *id = "num";
11384 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11385 location_t loc = c_parser_peek_token (parser)->location;
11387 if (kind == OMP_CLAUSE_VECTOR)
11388 id = "length";
11390 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11392 c_parser_consume_token (parser);
11396 c_token *next = c_parser_peek_token (parser);
11397 int idx = 0;
11399 /* Gang static argument. */
11400 if (kind == OMP_CLAUSE_GANG
11401 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11403 c_parser_consume_token (parser);
11405 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11406 goto cleanup_error;
11408 idx = 1;
11409 if (ops[idx] != NULL_TREE)
11411 c_parser_error (parser, "too many %<static%> arguments");
11412 goto cleanup_error;
11415 /* Check for the '*' argument. */
11416 if (c_parser_next_token_is (parser, CPP_MULT)
11417 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11418 || c_parser_peek_2nd_token (parser)->type
11419 == CPP_CLOSE_PAREN))
11421 c_parser_consume_token (parser);
11422 ops[idx] = integer_minus_one_node;
11424 if (c_parser_next_token_is (parser, CPP_COMMA))
11426 c_parser_consume_token (parser);
11427 continue;
11429 else
11430 break;
11433 /* Worker num: argument and vector length: arguments. */
11434 else if (c_parser_next_token_is (parser, CPP_NAME)
11435 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11436 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11438 c_parser_consume_token (parser); /* id */
11439 c_parser_consume_token (parser); /* ':' */
11442 /* Now collect the actual argument. */
11443 if (ops[idx] != NULL_TREE)
11445 c_parser_error (parser, "unexpected argument");
11446 goto cleanup_error;
11449 location_t expr_loc = c_parser_peek_token (parser)->location;
11450 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11451 if (expr == error_mark_node)
11452 goto cleanup_error;
11454 mark_exp_read (expr);
11455 expr = c_fully_fold (expr, false, NULL);
11457 /* Attempt to statically determine when the number isn't a
11458 positive integer. */
11460 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11462 c_parser_error (parser, "expected integer expression");
11463 return list;
11466 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11467 build_int_cst (TREE_TYPE (expr), 0));
11468 if (c == boolean_true_node)
11470 warning_at (loc, 0,
11471 "%<%s%> value must be positive", str);
11472 expr = integer_one_node;
11475 ops[idx] = expr;
11477 if (kind == OMP_CLAUSE_GANG
11478 && c_parser_next_token_is (parser, CPP_COMMA))
11480 c_parser_consume_token (parser);
11481 continue;
11483 break;
11485 while (1);
11487 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11488 goto cleanup_error;
11491 check_no_duplicate_clause (list, kind, str);
11493 c = build_omp_clause (loc, kind);
11495 if (ops[1])
11496 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11498 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11499 OMP_CLAUSE_CHAIN (c) = list;
11501 return c;
11503 cleanup_error:
11504 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11505 return list;
11508 /* OpenACC:
11509 auto
11510 independent
11511 nohost
11512 seq */
11514 static tree
11515 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11516 tree list)
11518 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11520 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11521 OMP_CLAUSE_CHAIN (c) = list;
11523 return c;
11526 /* OpenACC:
11527 async [( int-expr )] */
11529 static tree
11530 c_parser_oacc_clause_async (c_parser *parser, tree list)
11532 tree c, t;
11533 location_t loc = c_parser_peek_token (parser)->location;
11535 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11537 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11539 c_parser_consume_token (parser);
11541 t = c_parser_expression (parser).value;
11542 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11543 c_parser_error (parser, "expected integer expression");
11544 else if (t == error_mark_node
11545 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11546 return list;
11548 else
11549 t = c_fully_fold (t, false, NULL);
11551 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11553 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11554 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11555 OMP_CLAUSE_CHAIN (c) = list;
11556 list = c;
11558 return list;
11561 /* OpenACC 2.0:
11562 tile ( size-expr-list ) */
11564 static tree
11565 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11567 tree c, expr = error_mark_node;
11568 location_t loc, expr_loc;
11569 tree tile = NULL_TREE;
11571 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11573 loc = c_parser_peek_token (parser)->location;
11574 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11575 return list;
11579 if (c_parser_next_token_is (parser, CPP_MULT)
11580 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11581 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11583 c_parser_consume_token (parser);
11584 expr = integer_minus_one_node;
11586 else
11588 expr_loc = c_parser_peek_token (parser)->location;
11589 expr = c_parser_expr_no_commas (parser, NULL).value;
11591 if (expr == error_mark_node)
11593 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11594 "expected %<)%>");
11595 return list;
11598 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11600 c_parser_error (parser, "%<tile%> value must be integral");
11601 return list;
11604 mark_exp_read (expr);
11605 expr = c_fully_fold (expr, false, NULL);
11607 /* Attempt to statically determine when expr isn't positive. */
11608 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11609 build_int_cst (TREE_TYPE (expr), 0));
11610 protected_set_expr_location (c, expr_loc);
11611 if (c == boolean_true_node)
11613 warning_at (expr_loc, 0,"%<tile%> value must be positive");
11614 expr = integer_one_node;
11618 tile = tree_cons (NULL_TREE, expr, tile);
11619 if (c_parser_next_token_is (parser, CPP_COMMA))
11620 c_parser_consume_token (parser);
11622 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11624 /* Consume the trailing ')'. */
11625 c_parser_consume_token (parser);
11627 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11628 tile = nreverse (tile);
11629 OMP_CLAUSE_TILE_LIST (c) = tile;
11630 OMP_CLAUSE_CHAIN (c) = list;
11631 return c;
11634 /* OpenACC:
11635 wait ( int-expr-list ) */
11637 static tree
11638 c_parser_oacc_clause_wait (c_parser *parser, tree list)
11640 location_t clause_loc = c_parser_peek_token (parser)->location;
11642 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11643 list = c_parser_oacc_wait_list (parser, clause_loc, list);
11645 return list;
11648 /* OpenMP 2.5:
11649 ordered
11651 OpenMP 4.5:
11652 ordered ( constant-expression ) */
11654 static tree
11655 c_parser_omp_clause_ordered (c_parser *parser, tree list)
11657 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
11659 tree c, num = NULL_TREE;
11660 HOST_WIDE_INT n;
11661 location_t loc = c_parser_peek_token (parser)->location;
11662 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11664 c_parser_consume_token (parser);
11665 num = c_parser_expr_no_commas (parser, NULL).value;
11666 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11668 if (num == error_mark_node)
11669 return list;
11670 if (num)
11672 mark_exp_read (num);
11673 num = c_fully_fold (num, false, NULL);
11674 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11675 || !tree_fits_shwi_p (num)
11676 || (n = tree_to_shwi (num)) <= 0
11677 || (int) n != n)
11679 error_at (loc, "ordered argument needs positive "
11680 "constant integer expression");
11681 return list;
11684 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
11685 OMP_CLAUSE_ORDERED_EXPR (c) = num;
11686 OMP_CLAUSE_CHAIN (c) = list;
11687 return c;
11690 /* OpenMP 2.5:
11691 private ( variable-list ) */
11693 static tree
11694 c_parser_omp_clause_private (c_parser *parser, tree list)
11696 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
11699 /* OpenMP 2.5:
11700 reduction ( reduction-operator : variable-list )
11702 reduction-operator:
11703 One of: + * - & ^ | && ||
11705 OpenMP 3.1:
11707 reduction-operator:
11708 One of: + * - & ^ | && || max min
11710 OpenMP 4.0:
11712 reduction-operator:
11713 One of: + * - & ^ | && ||
11714 identifier */
11716 static tree
11717 c_parser_omp_clause_reduction (c_parser *parser, tree list)
11719 location_t clause_loc = c_parser_peek_token (parser)->location;
11720 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11722 enum tree_code code = ERROR_MARK;
11723 tree reduc_id = NULL_TREE;
11725 switch (c_parser_peek_token (parser)->type)
11727 case CPP_PLUS:
11728 code = PLUS_EXPR;
11729 break;
11730 case CPP_MULT:
11731 code = MULT_EXPR;
11732 break;
11733 case CPP_MINUS:
11734 code = MINUS_EXPR;
11735 break;
11736 case CPP_AND:
11737 code = BIT_AND_EXPR;
11738 break;
11739 case CPP_XOR:
11740 code = BIT_XOR_EXPR;
11741 break;
11742 case CPP_OR:
11743 code = BIT_IOR_EXPR;
11744 break;
11745 case CPP_AND_AND:
11746 code = TRUTH_ANDIF_EXPR;
11747 break;
11748 case CPP_OR_OR:
11749 code = TRUTH_ORIF_EXPR;
11750 break;
11751 case CPP_NAME:
11753 const char *p
11754 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11755 if (strcmp (p, "min") == 0)
11757 code = MIN_EXPR;
11758 break;
11760 if (strcmp (p, "max") == 0)
11762 code = MAX_EXPR;
11763 break;
11765 reduc_id = c_parser_peek_token (parser)->value;
11766 break;
11768 default:
11769 c_parser_error (parser,
11770 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
11771 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
11772 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11773 return list;
11775 c_parser_consume_token (parser);
11776 reduc_id = c_omp_reduction_id (code, reduc_id);
11777 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11779 tree nl, c;
11781 nl = c_parser_omp_variable_list (parser, clause_loc,
11782 OMP_CLAUSE_REDUCTION, list);
11783 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11785 tree d = OMP_CLAUSE_DECL (c), type;
11786 if (TREE_CODE (d) != TREE_LIST)
11787 type = TREE_TYPE (d);
11788 else
11790 int cnt = 0;
11791 tree t;
11792 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
11793 cnt++;
11794 type = TREE_TYPE (t);
11795 while (cnt > 0)
11797 if (TREE_CODE (type) != POINTER_TYPE
11798 && TREE_CODE (type) != ARRAY_TYPE)
11799 break;
11800 type = TREE_TYPE (type);
11801 cnt--;
11804 while (TREE_CODE (type) == ARRAY_TYPE)
11805 type = TREE_TYPE (type);
11806 OMP_CLAUSE_REDUCTION_CODE (c) = code;
11807 if (code == ERROR_MARK
11808 || !(INTEGRAL_TYPE_P (type)
11809 || TREE_CODE (type) == REAL_TYPE
11810 || TREE_CODE (type) == COMPLEX_TYPE))
11811 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
11812 = c_omp_reduction_lookup (reduc_id,
11813 TYPE_MAIN_VARIANT (type));
11816 list = nl;
11818 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11820 return list;
11823 /* OpenMP 2.5:
11824 schedule ( schedule-kind )
11825 schedule ( schedule-kind , expression )
11827 schedule-kind:
11828 static | dynamic | guided | runtime | auto
11830 OpenMP 4.5:
11831 schedule ( schedule-modifier : schedule-kind )
11832 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
11834 schedule-modifier:
11835 simd
11836 monotonic
11837 nonmonotonic */
11839 static tree
11840 c_parser_omp_clause_schedule (c_parser *parser, tree list)
11842 tree c, t;
11843 location_t loc = c_parser_peek_token (parser)->location;
11844 int modifiers = 0, nmodifiers = 0;
11846 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11847 return list;
11849 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
11851 while (c_parser_next_token_is (parser, CPP_NAME))
11853 tree kind = c_parser_peek_token (parser)->value;
11854 const char *p = IDENTIFIER_POINTER (kind);
11855 if (strcmp ("simd", p) == 0)
11856 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
11857 else if (strcmp ("monotonic", p) == 0)
11858 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
11859 else if (strcmp ("nonmonotonic", p) == 0)
11860 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
11861 else
11862 break;
11863 c_parser_consume_token (parser);
11864 if (nmodifiers++ == 0
11865 && c_parser_next_token_is (parser, CPP_COMMA))
11866 c_parser_consume_token (parser);
11867 else
11869 c_parser_require (parser, CPP_COLON, "expected %<:%>");
11870 break;
11874 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
11875 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
11876 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
11877 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
11879 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
11880 "specified");
11881 modifiers = 0;
11884 if (c_parser_next_token_is (parser, CPP_NAME))
11886 tree kind = c_parser_peek_token (parser)->value;
11887 const char *p = IDENTIFIER_POINTER (kind);
11889 switch (p[0])
11891 case 'd':
11892 if (strcmp ("dynamic", p) != 0)
11893 goto invalid_kind;
11894 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
11895 break;
11897 case 'g':
11898 if (strcmp ("guided", p) != 0)
11899 goto invalid_kind;
11900 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
11901 break;
11903 case 'r':
11904 if (strcmp ("runtime", p) != 0)
11905 goto invalid_kind;
11906 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
11907 break;
11909 default:
11910 goto invalid_kind;
11913 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
11914 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
11915 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11916 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
11917 else
11918 goto invalid_kind;
11920 c_parser_consume_token (parser);
11921 if (c_parser_next_token_is (parser, CPP_COMMA))
11923 location_t here;
11924 c_parser_consume_token (parser);
11926 here = c_parser_peek_token (parser)->location;
11927 t = c_parser_expr_no_commas (parser, NULL).value;
11928 mark_exp_read (t);
11929 t = c_fully_fold (t, false, NULL);
11931 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
11932 error_at (here, "schedule %<runtime%> does not take "
11933 "a %<chunk_size%> parameter");
11934 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
11935 error_at (here,
11936 "schedule %<auto%> does not take "
11937 "a %<chunk_size%> parameter");
11938 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
11939 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
11940 else
11941 c_parser_error (parser, "expected integer expression");
11943 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11945 else
11946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11947 "expected %<,%> or %<)%>");
11949 OMP_CLAUSE_SCHEDULE_KIND (c)
11950 = (enum omp_clause_schedule_kind)
11951 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
11953 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
11954 OMP_CLAUSE_CHAIN (c) = list;
11955 return c;
11957 invalid_kind:
11958 c_parser_error (parser, "invalid schedule kind");
11959 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11960 return list;
11963 /* OpenMP 2.5:
11964 shared ( variable-list ) */
11966 static tree
11967 c_parser_omp_clause_shared (c_parser *parser, tree list)
11969 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
11972 /* OpenMP 3.0:
11973 untied */
11975 static tree
11976 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11978 tree c;
11980 /* FIXME: Should we allow duplicates? */
11981 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
11983 c = build_omp_clause (c_parser_peek_token (parser)->location,
11984 OMP_CLAUSE_UNTIED);
11985 OMP_CLAUSE_CHAIN (c) = list;
11987 return c;
11990 /* OpenACC:
11991 vector_length ( expression ) */
11993 static tree
11994 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
11996 location_t vector_length_loc = c_parser_peek_token (parser)->location;
11997 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11999 location_t expr_loc = c_parser_peek_token (parser)->location;
12000 tree c, t = c_parser_expression (parser).value;
12001 mark_exp_read (t);
12002 t = c_fully_fold (t, false, NULL);
12004 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12006 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12008 c_parser_error (parser, "expected integer expression");
12009 return list;
12012 /* Attempt to statically determine when the number isn't positive. */
12013 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12014 build_int_cst (TREE_TYPE (t), 0));
12015 protected_set_expr_location (c, expr_loc);
12016 if (c == boolean_true_node)
12018 warning_at (expr_loc, 0,
12019 "%<vector_length%> value must be positive");
12020 t = integer_one_node;
12023 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12025 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12026 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12027 OMP_CLAUSE_CHAIN (c) = list;
12028 list = c;
12031 return list;
12034 /* OpenMP 4.0:
12035 inbranch
12036 notinbranch */
12038 static tree
12039 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12040 enum omp_clause_code code, tree list)
12042 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12044 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12045 OMP_CLAUSE_CHAIN (c) = list;
12047 return c;
12050 /* OpenMP 4.0:
12051 parallel
12053 sections
12054 taskgroup */
12056 static tree
12057 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12058 enum omp_clause_code code, tree list)
12060 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12061 OMP_CLAUSE_CHAIN (c) = list;
12063 return c;
12066 /* OpenMP 4.5:
12067 nogroup */
12069 static tree
12070 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12072 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12073 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12074 OMP_CLAUSE_NOGROUP);
12075 OMP_CLAUSE_CHAIN (c) = list;
12076 return c;
12079 /* OpenMP 4.5:
12080 simd
12081 threads */
12083 static tree
12084 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12085 enum omp_clause_code code, tree list)
12087 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12088 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12089 OMP_CLAUSE_CHAIN (c) = list;
12090 return c;
12093 /* OpenMP 4.0:
12094 num_teams ( expression ) */
12096 static tree
12097 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12099 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12100 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12102 location_t expr_loc = c_parser_peek_token (parser)->location;
12103 tree c, t = c_parser_expression (parser).value;
12104 mark_exp_read (t);
12105 t = c_fully_fold (t, false, NULL);
12107 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12109 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12111 c_parser_error (parser, "expected integer expression");
12112 return list;
12115 /* Attempt to statically determine when the number isn't positive. */
12116 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12117 build_int_cst (TREE_TYPE (t), 0));
12118 protected_set_expr_location (c, expr_loc);
12119 if (c == boolean_true_node)
12121 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12122 t = integer_one_node;
12125 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12127 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12128 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12129 OMP_CLAUSE_CHAIN (c) = list;
12130 list = c;
12133 return list;
12136 /* OpenMP 4.0:
12137 thread_limit ( expression ) */
12139 static tree
12140 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12142 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12143 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12145 location_t expr_loc = c_parser_peek_token (parser)->location;
12146 tree c, t = c_parser_expression (parser).value;
12147 mark_exp_read (t);
12148 t = c_fully_fold (t, false, NULL);
12150 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12152 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12154 c_parser_error (parser, "expected integer expression");
12155 return list;
12158 /* Attempt to statically determine when the number isn't positive. */
12159 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12160 build_int_cst (TREE_TYPE (t), 0));
12161 protected_set_expr_location (c, expr_loc);
12162 if (c == boolean_true_node)
12164 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12165 t = integer_one_node;
12168 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12169 "thread_limit");
12171 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12172 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12173 OMP_CLAUSE_CHAIN (c) = list;
12174 list = c;
12177 return list;
12180 /* OpenMP 4.0:
12181 aligned ( variable-list )
12182 aligned ( variable-list : constant-expression ) */
12184 static tree
12185 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12187 location_t clause_loc = c_parser_peek_token (parser)->location;
12188 tree nl, c;
12190 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12191 return list;
12193 nl = c_parser_omp_variable_list (parser, clause_loc,
12194 OMP_CLAUSE_ALIGNED, list);
12196 if (c_parser_next_token_is (parser, CPP_COLON))
12198 c_parser_consume_token (parser);
12199 tree alignment = c_parser_expr_no_commas (parser, NULL).value;
12200 mark_exp_read (alignment);
12201 alignment = c_fully_fold (alignment, false, NULL);
12202 if (TREE_CODE (alignment) != INTEGER_CST
12203 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12204 || tree_int_cst_sgn (alignment) != 1)
12206 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12207 "be positive constant integer expression");
12208 alignment = NULL_TREE;
12211 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12212 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12215 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12216 return nl;
12219 /* OpenMP 4.0:
12220 linear ( variable-list )
12221 linear ( variable-list : expression )
12223 OpenMP 4.5:
12224 linear ( modifier ( variable-list ) )
12225 linear ( modifier ( variable-list ) : expression ) */
12227 static tree
12228 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12230 location_t clause_loc = c_parser_peek_token (parser)->location;
12231 tree nl, c, step;
12232 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12234 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12235 return list;
12237 if (!is_cilk_simd_fn
12238 && c_parser_next_token_is (parser, CPP_NAME))
12240 c_token *tok = c_parser_peek_token (parser);
12241 const char *p = IDENTIFIER_POINTER (tok->value);
12242 if (strcmp ("val", p) == 0)
12243 kind = OMP_CLAUSE_LINEAR_VAL;
12244 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12245 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12246 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12248 c_parser_consume_token (parser);
12249 c_parser_consume_token (parser);
12253 nl = c_parser_omp_variable_list (parser, clause_loc,
12254 OMP_CLAUSE_LINEAR, list);
12256 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12257 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12259 if (c_parser_next_token_is (parser, CPP_COLON))
12261 c_parser_consume_token (parser);
12262 step = c_parser_expression (parser).value;
12263 mark_exp_read (step);
12264 step = c_fully_fold (step, false, NULL);
12265 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12267 sorry ("using parameters for %<linear%> step is not supported yet");
12268 step = integer_one_node;
12270 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12272 error_at (clause_loc, "%<linear%> clause step expression must "
12273 "be integral");
12274 step = integer_one_node;
12278 else
12279 step = integer_one_node;
12281 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12283 OMP_CLAUSE_LINEAR_STEP (c) = step;
12284 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12287 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12288 return nl;
12291 /* OpenMP 4.0:
12292 safelen ( constant-expression ) */
12294 static tree
12295 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12297 location_t clause_loc = c_parser_peek_token (parser)->location;
12298 tree c, t;
12300 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12301 return list;
12303 t = c_parser_expr_no_commas (parser, NULL).value;
12304 mark_exp_read (t);
12305 t = c_fully_fold (t, false, NULL);
12306 if (TREE_CODE (t) != INTEGER_CST
12307 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12308 || tree_int_cst_sgn (t) != 1)
12310 error_at (clause_loc, "%<safelen%> clause expression must "
12311 "be positive constant integer expression");
12312 t = NULL_TREE;
12315 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12316 if (t == NULL_TREE || t == error_mark_node)
12317 return list;
12319 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12321 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12322 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12323 OMP_CLAUSE_CHAIN (c) = list;
12324 return c;
12327 /* OpenMP 4.0:
12328 simdlen ( constant-expression ) */
12330 static tree
12331 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12333 location_t clause_loc = c_parser_peek_token (parser)->location;
12334 tree c, t;
12336 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12337 return list;
12339 t = c_parser_expr_no_commas (parser, NULL).value;
12340 mark_exp_read (t);
12341 t = c_fully_fold (t, false, NULL);
12342 if (TREE_CODE (t) != INTEGER_CST
12343 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12344 || tree_int_cst_sgn (t) != 1)
12346 error_at (clause_loc, "%<simdlen%> clause expression must "
12347 "be positive constant integer expression");
12348 t = NULL_TREE;
12351 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12352 if (t == NULL_TREE || t == error_mark_node)
12353 return list;
12355 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12357 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12358 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12359 OMP_CLAUSE_CHAIN (c) = list;
12360 return c;
12363 /* OpenMP 4.5:
12364 vec:
12365 identifier [+/- integer]
12366 vec , identifier [+/- integer]
12369 static tree
12370 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12371 tree list)
12373 tree vec = NULL;
12374 if (c_parser_next_token_is_not (parser, CPP_NAME)
12375 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12377 c_parser_error (parser, "expected identifier");
12378 return list;
12381 while (c_parser_next_token_is (parser, CPP_NAME)
12382 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12384 tree t = lookup_name (c_parser_peek_token (parser)->value);
12385 tree addend = NULL;
12387 if (t == NULL_TREE)
12389 undeclared_variable (c_parser_peek_token (parser)->location,
12390 c_parser_peek_token (parser)->value);
12391 t = error_mark_node;
12394 c_parser_consume_token (parser);
12396 bool neg = false;
12397 if (c_parser_next_token_is (parser, CPP_MINUS))
12398 neg = true;
12399 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12401 addend = integer_zero_node;
12402 neg = false;
12403 goto add_to_vector;
12405 c_parser_consume_token (parser);
12407 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12409 c_parser_error (parser, "expected integer");
12410 return list;
12413 addend = c_parser_peek_token (parser)->value;
12414 if (TREE_CODE (addend) != INTEGER_CST)
12416 c_parser_error (parser, "expected integer");
12417 return list;
12419 c_parser_consume_token (parser);
12421 add_to_vector:
12422 if (t != error_mark_node)
12424 vec = tree_cons (addend, t, vec);
12425 if (neg)
12426 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12429 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12430 break;
12432 c_parser_consume_token (parser);
12435 if (vec == NULL_TREE)
12436 return list;
12438 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12439 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12440 OMP_CLAUSE_DECL (u) = nreverse (vec);
12441 OMP_CLAUSE_CHAIN (u) = list;
12442 return u;
12445 /* OpenMP 4.0:
12446 depend ( depend-kind: variable-list )
12448 depend-kind:
12449 in | out | inout
12451 OpenMP 4.5:
12452 depend ( source )
12454 depend ( sink : vec ) */
12456 static tree
12457 c_parser_omp_clause_depend (c_parser *parser, tree list)
12459 location_t clause_loc = c_parser_peek_token (parser)->location;
12460 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12461 tree nl, c;
12463 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12464 return list;
12466 if (c_parser_next_token_is (parser, CPP_NAME))
12468 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12469 if (strcmp ("in", p) == 0)
12470 kind = OMP_CLAUSE_DEPEND_IN;
12471 else if (strcmp ("inout", p) == 0)
12472 kind = OMP_CLAUSE_DEPEND_INOUT;
12473 else if (strcmp ("out", p) == 0)
12474 kind = OMP_CLAUSE_DEPEND_OUT;
12475 else if (strcmp ("source", p) == 0)
12476 kind = OMP_CLAUSE_DEPEND_SOURCE;
12477 else if (strcmp ("sink", p) == 0)
12478 kind = OMP_CLAUSE_DEPEND_SINK;
12479 else
12480 goto invalid_kind;
12482 else
12483 goto invalid_kind;
12485 c_parser_consume_token (parser);
12487 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12489 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12490 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12491 OMP_CLAUSE_DECL (c) = NULL_TREE;
12492 OMP_CLAUSE_CHAIN (c) = list;
12493 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12494 return c;
12497 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12498 goto resync_fail;
12500 if (kind == OMP_CLAUSE_DEPEND_SINK)
12501 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12502 else
12504 nl = c_parser_omp_variable_list (parser, clause_loc,
12505 OMP_CLAUSE_DEPEND, list);
12507 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12508 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12511 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12512 return nl;
12514 invalid_kind:
12515 c_parser_error (parser, "invalid depend kind");
12516 resync_fail:
12517 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12518 return list;
12521 /* OpenMP 4.0:
12522 map ( map-kind: variable-list )
12523 map ( variable-list )
12525 map-kind:
12526 alloc | to | from | tofrom
12528 OpenMP 4.5:
12529 map-kind:
12530 alloc | to | from | tofrom | release | delete
12532 map ( always [,] map-kind: variable-list ) */
12534 static tree
12535 c_parser_omp_clause_map (c_parser *parser, tree list)
12537 location_t clause_loc = c_parser_peek_token (parser)->location;
12538 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12539 int always = 0;
12540 enum c_id_kind always_id_kind = C_ID_NONE;
12541 location_t always_loc = UNKNOWN_LOCATION;
12542 tree always_id = NULL_TREE;
12543 tree nl, c;
12545 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12546 return list;
12548 if (c_parser_next_token_is (parser, CPP_NAME))
12550 c_token *tok = c_parser_peek_token (parser);
12551 const char *p = IDENTIFIER_POINTER (tok->value);
12552 always_id_kind = tok->id_kind;
12553 always_loc = tok->location;
12554 always_id = tok->value;
12555 if (strcmp ("always", p) == 0)
12557 c_token *sectok = c_parser_peek_2nd_token (parser);
12558 if (sectok->type == CPP_COMMA)
12560 c_parser_consume_token (parser);
12561 c_parser_consume_token (parser);
12562 always = 2;
12564 else if (sectok->type == CPP_NAME)
12566 p = IDENTIFIER_POINTER (sectok->value);
12567 if (strcmp ("alloc", p) == 0
12568 || strcmp ("to", p) == 0
12569 || strcmp ("from", p) == 0
12570 || strcmp ("tofrom", p) == 0
12571 || strcmp ("release", p) == 0
12572 || strcmp ("delete", p) == 0)
12574 c_parser_consume_token (parser);
12575 always = 1;
12581 if (c_parser_next_token_is (parser, CPP_NAME)
12582 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12584 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12585 if (strcmp ("alloc", p) == 0)
12586 kind = GOMP_MAP_ALLOC;
12587 else if (strcmp ("to", p) == 0)
12588 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12589 else if (strcmp ("from", p) == 0)
12590 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12591 else if (strcmp ("tofrom", p) == 0)
12592 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12593 else if (strcmp ("release", p) == 0)
12594 kind = GOMP_MAP_RELEASE;
12595 else if (strcmp ("delete", p) == 0)
12596 kind = GOMP_MAP_DELETE;
12597 else
12599 c_parser_error (parser, "invalid map kind");
12600 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12601 "expected %<)%>");
12602 return list;
12604 c_parser_consume_token (parser);
12605 c_parser_consume_token (parser);
12607 else if (always)
12609 if (always_id_kind != C_ID_ID)
12611 c_parser_error (parser, "expected identifier");
12612 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12613 return list;
12616 tree t = lookup_name (always_id);
12617 if (t == NULL_TREE)
12619 undeclared_variable (always_loc, always_id);
12620 t = error_mark_node;
12622 if (t != error_mark_node)
12624 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12625 OMP_CLAUSE_DECL (u) = t;
12626 OMP_CLAUSE_CHAIN (u) = list;
12627 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12628 list = u;
12630 if (always == 1)
12632 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12633 return list;
12637 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
12639 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12640 OMP_CLAUSE_SET_MAP_KIND (c, kind);
12642 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12643 return nl;
12646 /* OpenMP 4.0:
12647 device ( expression ) */
12649 static tree
12650 c_parser_omp_clause_device (c_parser *parser, tree list)
12652 location_t clause_loc = c_parser_peek_token (parser)->location;
12653 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12655 tree c, t = c_parser_expr_no_commas (parser, NULL).value;
12656 mark_exp_read (t);
12657 t = c_fully_fold (t, false, NULL);
12659 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12661 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12663 c_parser_error (parser, "expected integer expression");
12664 return list;
12667 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
12669 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
12670 OMP_CLAUSE_DEVICE_ID (c) = t;
12671 OMP_CLAUSE_CHAIN (c) = list;
12672 list = c;
12675 return list;
12678 /* OpenMP 4.0:
12679 dist_schedule ( static )
12680 dist_schedule ( static , expression ) */
12682 static tree
12683 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
12685 tree c, t = NULL_TREE;
12686 location_t loc = c_parser_peek_token (parser)->location;
12688 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12689 return list;
12691 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
12693 c_parser_error (parser, "invalid dist_schedule kind");
12694 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12695 "expected %<)%>");
12696 return list;
12699 c_parser_consume_token (parser);
12700 if (c_parser_next_token_is (parser, CPP_COMMA))
12702 c_parser_consume_token (parser);
12704 t = c_parser_expr_no_commas (parser, NULL).value;
12705 mark_exp_read (t);
12706 t = c_fully_fold (t, false, NULL);
12707 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12709 else
12710 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12711 "expected %<,%> or %<)%>");
12713 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12714 if (t == error_mark_node)
12715 return list;
12717 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
12718 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
12719 OMP_CLAUSE_CHAIN (c) = list;
12720 return c;
12723 /* OpenMP 4.0:
12724 proc_bind ( proc-bind-kind )
12726 proc-bind-kind:
12727 master | close | spread */
12729 static tree
12730 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
12732 location_t clause_loc = c_parser_peek_token (parser)->location;
12733 enum omp_clause_proc_bind_kind kind;
12734 tree c;
12736 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12737 return list;
12739 if (c_parser_next_token_is (parser, CPP_NAME))
12741 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12742 if (strcmp ("master", p) == 0)
12743 kind = OMP_CLAUSE_PROC_BIND_MASTER;
12744 else if (strcmp ("close", p) == 0)
12745 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
12746 else if (strcmp ("spread", p) == 0)
12747 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
12748 else
12749 goto invalid_kind;
12751 else
12752 goto invalid_kind;
12754 c_parser_consume_token (parser);
12755 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12756 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
12757 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
12758 OMP_CLAUSE_CHAIN (c) = list;
12759 return c;
12761 invalid_kind:
12762 c_parser_error (parser, "invalid proc_bind kind");
12763 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12764 return list;
12767 /* OpenMP 4.0:
12768 to ( variable-list ) */
12770 static tree
12771 c_parser_omp_clause_to (c_parser *parser, tree list)
12773 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
12776 /* OpenMP 4.0:
12777 from ( variable-list ) */
12779 static tree
12780 c_parser_omp_clause_from (c_parser *parser, tree list)
12782 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
12785 /* OpenMP 4.0:
12786 uniform ( variable-list ) */
12788 static tree
12789 c_parser_omp_clause_uniform (c_parser *parser, tree list)
12791 /* The clauses location. */
12792 location_t loc = c_parser_peek_token (parser)->location;
12794 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12796 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
12797 list);
12798 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12800 return list;
12803 /* Parse all OpenACC clauses. The set clauses allowed by the directive
12804 is a bitmask in MASK. Return the list of clauses found. */
12806 static tree
12807 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
12808 const char *where, bool finish_p = true)
12810 tree clauses = NULL;
12811 bool first = true;
12813 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
12815 location_t here;
12816 pragma_omp_clause c_kind;
12817 const char *c_name;
12818 tree prev = clauses;
12820 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
12821 c_parser_consume_token (parser);
12823 here = c_parser_peek_token (parser)->location;
12824 c_kind = c_parser_omp_clause_name (parser);
12826 switch (c_kind)
12828 case PRAGMA_OACC_CLAUSE_ASYNC:
12829 clauses = c_parser_oacc_clause_async (parser, clauses);
12830 c_name = "async";
12831 break;
12832 case PRAGMA_OACC_CLAUSE_AUTO:
12833 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
12834 clauses);
12835 c_name = "auto";
12836 break;
12837 case PRAGMA_OACC_CLAUSE_COLLAPSE:
12838 clauses = c_parser_omp_clause_collapse (parser, clauses);
12839 c_name = "collapse";
12840 break;
12841 case PRAGMA_OACC_CLAUSE_COPY:
12842 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12843 c_name = "copy";
12844 break;
12845 case PRAGMA_OACC_CLAUSE_COPYIN:
12846 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12847 c_name = "copyin";
12848 break;
12849 case PRAGMA_OACC_CLAUSE_COPYOUT:
12850 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12851 c_name = "copyout";
12852 break;
12853 case PRAGMA_OACC_CLAUSE_CREATE:
12854 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12855 c_name = "create";
12856 break;
12857 case PRAGMA_OACC_CLAUSE_DELETE:
12858 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12859 c_name = "delete";
12860 break;
12861 case PRAGMA_OMP_CLAUSE_DEFAULT:
12862 clauses = c_parser_omp_clause_default (parser, clauses, true);
12863 c_name = "default";
12864 break;
12865 case PRAGMA_OACC_CLAUSE_DEVICE:
12866 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12867 c_name = "device";
12868 break;
12869 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
12870 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
12871 c_name = "deviceptr";
12872 break;
12873 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
12874 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12875 c_name = "device_resident";
12876 break;
12877 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
12878 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
12879 c_name = "firstprivate";
12880 break;
12881 case PRAGMA_OACC_CLAUSE_GANG:
12882 c_name = "gang";
12883 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
12884 c_name, clauses);
12885 break;
12886 case PRAGMA_OACC_CLAUSE_HOST:
12887 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12888 c_name = "host";
12889 break;
12890 case PRAGMA_OACC_CLAUSE_IF:
12891 clauses = c_parser_omp_clause_if (parser, clauses, false);
12892 c_name = "if";
12893 break;
12894 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
12895 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
12896 clauses);
12897 c_name = "independent";
12898 break;
12899 case PRAGMA_OACC_CLAUSE_LINK:
12900 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12901 c_name = "link";
12902 break;
12903 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
12904 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
12905 c_name = "num_gangs";
12906 break;
12907 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
12908 clauses = c_parser_omp_clause_num_workers (parser, clauses);
12909 c_name = "num_workers";
12910 break;
12911 case PRAGMA_OACC_CLAUSE_PRESENT:
12912 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12913 c_name = "present";
12914 break;
12915 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
12916 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12917 c_name = "present_or_copy";
12918 break;
12919 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
12920 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12921 c_name = "present_or_copyin";
12922 break;
12923 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
12924 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12925 c_name = "present_or_copyout";
12926 break;
12927 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
12928 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12929 c_name = "present_or_create";
12930 break;
12931 case PRAGMA_OACC_CLAUSE_PRIVATE:
12932 clauses = c_parser_omp_clause_private (parser, clauses);
12933 c_name = "private";
12934 break;
12935 case PRAGMA_OACC_CLAUSE_REDUCTION:
12936 clauses = c_parser_omp_clause_reduction (parser, clauses);
12937 c_name = "reduction";
12938 break;
12939 case PRAGMA_OACC_CLAUSE_SELF:
12940 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
12941 c_name = "self";
12942 break;
12943 case PRAGMA_OACC_CLAUSE_SEQ:
12944 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
12945 clauses);
12946 c_name = "seq";
12947 break;
12948 case PRAGMA_OACC_CLAUSE_TILE:
12949 clauses = c_parser_oacc_clause_tile (parser, clauses);
12950 c_name = "tile";
12951 break;
12952 case PRAGMA_OACC_CLAUSE_VECTOR:
12953 c_name = "vector";
12954 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
12955 c_name, clauses);
12956 break;
12957 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
12958 clauses = c_parser_omp_clause_vector_length (parser, clauses);
12959 c_name = "vector_length";
12960 break;
12961 case PRAGMA_OACC_CLAUSE_WAIT:
12962 clauses = c_parser_oacc_clause_wait (parser, clauses);
12963 c_name = "wait";
12964 break;
12965 case PRAGMA_OACC_CLAUSE_WORKER:
12966 c_name = "worker";
12967 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
12968 c_name, clauses);
12969 break;
12970 default:
12971 c_parser_error (parser, "expected %<#pragma acc%> clause");
12972 goto saw_error;
12975 first = false;
12977 if (((mask >> c_kind) & 1) == 0)
12979 /* Remove the invalid clause(s) from the list to avoid
12980 confusing the rest of the compiler. */
12981 clauses = prev;
12982 error_at (here, "%qs is not valid for %qs", c_name, where);
12986 saw_error:
12987 c_parser_skip_to_pragma_eol (parser);
12989 if (finish_p)
12990 return c_finish_omp_clauses (clauses, false);
12992 return clauses;
12995 /* Parse all OpenMP 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_omp_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, cilk_simd_fn = false;
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_OMP_CLAUSE_COLLAPSE:
13021 clauses = c_parser_omp_clause_collapse (parser, clauses);
13022 c_name = "collapse";
13023 break;
13024 case PRAGMA_OMP_CLAUSE_COPYIN:
13025 clauses = c_parser_omp_clause_copyin (parser, clauses);
13026 c_name = "copyin";
13027 break;
13028 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13029 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13030 c_name = "copyprivate";
13031 break;
13032 case PRAGMA_OMP_CLAUSE_DEFAULT:
13033 clauses = c_parser_omp_clause_default (parser, clauses, false);
13034 c_name = "default";
13035 break;
13036 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13037 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13038 c_name = "firstprivate";
13039 break;
13040 case PRAGMA_OMP_CLAUSE_FINAL:
13041 clauses = c_parser_omp_clause_final (parser, clauses);
13042 c_name = "final";
13043 break;
13044 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13045 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13046 c_name = "grainsize";
13047 break;
13048 case PRAGMA_OMP_CLAUSE_HINT:
13049 clauses = c_parser_omp_clause_hint (parser, clauses);
13050 c_name = "hint";
13051 break;
13052 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13053 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13054 c_name = "defaultmap";
13055 break;
13056 case PRAGMA_OMP_CLAUSE_IF:
13057 clauses = c_parser_omp_clause_if (parser, clauses, true);
13058 c_name = "if";
13059 break;
13060 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13061 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13062 c_name = "lastprivate";
13063 break;
13064 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13065 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13066 c_name = "mergeable";
13067 break;
13068 case PRAGMA_OMP_CLAUSE_NOWAIT:
13069 clauses = c_parser_omp_clause_nowait (parser, clauses);
13070 c_name = "nowait";
13071 break;
13072 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13073 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13074 c_name = "num_tasks";
13075 break;
13076 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13077 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13078 c_name = "num_threads";
13079 break;
13080 case PRAGMA_OMP_CLAUSE_ORDERED:
13081 clauses = c_parser_omp_clause_ordered (parser, clauses);
13082 c_name = "ordered";
13083 break;
13084 case PRAGMA_OMP_CLAUSE_PRIORITY:
13085 clauses = c_parser_omp_clause_priority (parser, clauses);
13086 c_name = "priority";
13087 break;
13088 case PRAGMA_OMP_CLAUSE_PRIVATE:
13089 clauses = c_parser_omp_clause_private (parser, clauses);
13090 c_name = "private";
13091 break;
13092 case PRAGMA_OMP_CLAUSE_REDUCTION:
13093 clauses = c_parser_omp_clause_reduction (parser, clauses);
13094 c_name = "reduction";
13095 break;
13096 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13097 clauses = c_parser_omp_clause_schedule (parser, clauses);
13098 c_name = "schedule";
13099 break;
13100 case PRAGMA_OMP_CLAUSE_SHARED:
13101 clauses = c_parser_omp_clause_shared (parser, clauses);
13102 c_name = "shared";
13103 break;
13104 case PRAGMA_OMP_CLAUSE_UNTIED:
13105 clauses = c_parser_omp_clause_untied (parser, clauses);
13106 c_name = "untied";
13107 break;
13108 case PRAGMA_OMP_CLAUSE_INBRANCH:
13109 case PRAGMA_CILK_CLAUSE_MASK:
13110 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13111 clauses);
13112 c_name = "inbranch";
13113 break;
13114 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13115 case PRAGMA_CILK_CLAUSE_NOMASK:
13116 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13117 clauses);
13118 c_name = "notinbranch";
13119 break;
13120 case PRAGMA_OMP_CLAUSE_PARALLEL:
13121 clauses
13122 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13123 clauses);
13124 c_name = "parallel";
13125 if (!first)
13127 clause_not_first:
13128 error_at (here, "%qs must be the first clause of %qs",
13129 c_name, where);
13130 clauses = prev;
13132 break;
13133 case PRAGMA_OMP_CLAUSE_FOR:
13134 clauses
13135 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13136 clauses);
13137 c_name = "for";
13138 if (!first)
13139 goto clause_not_first;
13140 break;
13141 case PRAGMA_OMP_CLAUSE_SECTIONS:
13142 clauses
13143 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13144 clauses);
13145 c_name = "sections";
13146 if (!first)
13147 goto clause_not_first;
13148 break;
13149 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13150 clauses
13151 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13152 clauses);
13153 c_name = "taskgroup";
13154 if (!first)
13155 goto clause_not_first;
13156 break;
13157 case PRAGMA_OMP_CLAUSE_LINK:
13158 clauses
13159 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13160 c_name = "link";
13161 break;
13162 case PRAGMA_OMP_CLAUSE_TO:
13163 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13164 clauses
13165 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13166 clauses);
13167 else
13168 clauses = c_parser_omp_clause_to (parser, clauses);
13169 c_name = "to";
13170 break;
13171 case PRAGMA_OMP_CLAUSE_FROM:
13172 clauses = c_parser_omp_clause_from (parser, clauses);
13173 c_name = "from";
13174 break;
13175 case PRAGMA_OMP_CLAUSE_UNIFORM:
13176 clauses = c_parser_omp_clause_uniform (parser, clauses);
13177 c_name = "uniform";
13178 break;
13179 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13180 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13181 c_name = "num_teams";
13182 break;
13183 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13184 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13185 c_name = "thread_limit";
13186 break;
13187 case PRAGMA_OMP_CLAUSE_ALIGNED:
13188 clauses = c_parser_omp_clause_aligned (parser, clauses);
13189 c_name = "aligned";
13190 break;
13191 case PRAGMA_OMP_CLAUSE_LINEAR:
13192 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13193 cilk_simd_fn = true;
13194 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13195 c_name = "linear";
13196 break;
13197 case PRAGMA_OMP_CLAUSE_DEPEND:
13198 clauses = c_parser_omp_clause_depend (parser, clauses);
13199 c_name = "depend";
13200 break;
13201 case PRAGMA_OMP_CLAUSE_MAP:
13202 clauses = c_parser_omp_clause_map (parser, clauses);
13203 c_name = "map";
13204 break;
13205 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13206 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13207 c_name = "use_device_ptr";
13208 break;
13209 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13210 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13211 c_name = "is_device_ptr";
13212 break;
13213 case PRAGMA_OMP_CLAUSE_DEVICE:
13214 clauses = c_parser_omp_clause_device (parser, clauses);
13215 c_name = "device";
13216 break;
13217 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13218 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13219 c_name = "dist_schedule";
13220 break;
13221 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13222 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13223 c_name = "proc_bind";
13224 break;
13225 case PRAGMA_OMP_CLAUSE_SAFELEN:
13226 clauses = c_parser_omp_clause_safelen (parser, clauses);
13227 c_name = "safelen";
13228 break;
13229 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13230 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13231 c_name = "simdlen";
13232 break;
13233 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13234 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13235 c_name = "simdlen";
13236 break;
13237 case PRAGMA_OMP_CLAUSE_NOGROUP:
13238 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13239 c_name = "nogroup";
13240 break;
13241 case PRAGMA_OMP_CLAUSE_THREADS:
13242 clauses
13243 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13244 clauses);
13245 c_name = "threads";
13246 break;
13247 case PRAGMA_OMP_CLAUSE_SIMD:
13248 clauses
13249 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13250 clauses);
13251 c_name = "simd";
13252 break;
13253 default:
13254 c_parser_error (parser, "expected %<#pragma omp%> clause");
13255 goto saw_error;
13258 first = false;
13260 if (((mask >> c_kind) & 1) == 0)
13262 /* Remove the invalid clause(s) from the list to avoid
13263 confusing the rest of the compiler. */
13264 clauses = prev;
13265 error_at (here, "%qs is not valid for %qs", c_name, where);
13269 saw_error:
13270 c_parser_skip_to_pragma_eol (parser);
13272 if (finish_p)
13274 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13275 return c_finish_omp_clauses (clauses, true, true);
13276 return c_finish_omp_clauses (clauses, true);
13279 return clauses;
13282 /* OpenACC 2.0, OpenMP 2.5:
13283 structured-block:
13284 statement
13286 In practice, we're also interested in adding the statement to an
13287 outer node. So it is convenient if we work around the fact that
13288 c_parser_statement calls add_stmt. */
13290 static tree
13291 c_parser_omp_structured_block (c_parser *parser)
13293 tree stmt = push_stmt_list ();
13294 c_parser_statement (parser);
13295 return pop_stmt_list (stmt);
13298 /* OpenACC 2.0:
13299 # pragma acc cache (variable-list) new-line
13301 LOC is the location of the #pragma token.
13304 static tree
13305 c_parser_oacc_cache (location_t loc, c_parser *parser)
13307 tree stmt, clauses;
13309 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13310 clauses = c_finish_omp_clauses (clauses, false);
13312 c_parser_skip_to_pragma_eol (parser);
13314 stmt = make_node (OACC_CACHE);
13315 TREE_TYPE (stmt) = void_type_node;
13316 OACC_CACHE_CLAUSES (stmt) = clauses;
13317 SET_EXPR_LOCATION (stmt, loc);
13318 add_stmt (stmt);
13320 return stmt;
13323 /* OpenACC 2.0:
13324 # pragma acc data oacc-data-clause[optseq] new-line
13325 structured-block
13327 LOC is the location of the #pragma token.
13330 #define OACC_DATA_CLAUSE_MASK \
13331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13343 static tree
13344 c_parser_oacc_data (location_t loc, c_parser *parser)
13346 tree stmt, clauses, block;
13348 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13349 "#pragma acc data");
13351 block = c_begin_omp_parallel ();
13352 add_stmt (c_parser_omp_structured_block (parser));
13354 stmt = c_finish_oacc_data (loc, clauses, block);
13356 return stmt;
13359 /* OpenACC 2.0:
13360 # pragma acc declare oacc-data-clause[optseq] new-line
13363 #define OACC_DECLARE_CLAUSE_MASK \
13364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13377 static void
13378 c_parser_oacc_declare (c_parser *parser)
13380 location_t pragma_loc = c_parser_peek_token (parser)->location;
13381 tree clauses, stmt, t, decl;
13383 bool error = false;
13385 c_parser_consume_pragma (parser);
13387 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13388 "#pragma acc declare");
13389 if (!clauses)
13391 error_at (pragma_loc,
13392 "no valid clauses specified in %<#pragma acc declare%>");
13393 return;
13396 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13398 location_t loc = OMP_CLAUSE_LOCATION (t);
13399 decl = OMP_CLAUSE_DECL (t);
13400 if (!DECL_P (decl))
13402 error_at (loc, "array section in %<#pragma acc declare%>");
13403 error = true;
13404 continue;
13407 switch (OMP_CLAUSE_MAP_KIND (t))
13409 case GOMP_MAP_FORCE_ALLOC:
13410 case GOMP_MAP_FORCE_TO:
13411 case GOMP_MAP_FORCE_DEVICEPTR:
13412 case GOMP_MAP_DEVICE_RESIDENT:
13413 break;
13415 case GOMP_MAP_POINTER:
13416 /* Generated by c_finish_omp_clauses from array sections;
13417 avoid spurious diagnostics. */
13418 break;
13420 case GOMP_MAP_LINK:
13421 if (!global_bindings_p ()
13422 && (TREE_STATIC (decl)
13423 || !DECL_EXTERNAL (decl)))
13425 error_at (loc,
13426 "%qD must be a global variable in"
13427 "%<#pragma acc declare link%>",
13428 decl);
13429 error = true;
13430 continue;
13432 break;
13434 default:
13435 if (global_bindings_p ())
13437 error_at (loc, "invalid OpenACC clause at file scope");
13438 error = true;
13439 continue;
13441 if (DECL_EXTERNAL (decl))
13443 error_at (loc,
13444 "invalid use of %<extern%> variable %qD "
13445 "in %<#pragma acc declare%>", decl);
13446 error = true;
13447 continue;
13449 else if (TREE_PUBLIC (decl))
13451 error_at (loc,
13452 "invalid use of %<global%> variable %qD "
13453 "in %<#pragma acc declare%>", decl);
13454 error = true;
13455 continue;
13457 break;
13460 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13461 || lookup_attribute ("omp declare target link",
13462 DECL_ATTRIBUTES (decl)))
13464 error_at (loc, "variable %qD used more than once with "
13465 "%<#pragma acc declare%>", decl);
13466 error = true;
13467 continue;
13470 if (!error)
13472 tree id;
13474 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13475 id = get_identifier ("omp declare target link");
13476 else
13477 id = get_identifier ("omp declare target");
13479 DECL_ATTRIBUTES (decl)
13480 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13482 if (global_bindings_p ())
13484 symtab_node *node = symtab_node::get (decl);
13485 if (node != NULL)
13487 node->offloadable = 1;
13488 if (ENABLE_OFFLOADING)
13490 g->have_offload = true;
13491 if (is_a <varpool_node *> (node))
13493 vec_safe_push (offload_vars, decl);
13494 node->force_output = 1;
13502 if (error || global_bindings_p ())
13503 return;
13505 stmt = make_node (OACC_DECLARE);
13506 TREE_TYPE (stmt) = void_type_node;
13507 OACC_DECLARE_CLAUSES (stmt) = clauses;
13508 SET_EXPR_LOCATION (stmt, pragma_loc);
13510 add_stmt (stmt);
13512 return;
13515 /* OpenACC 2.0:
13516 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13520 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13523 LOC is the location of the #pragma token.
13526 #define OACC_ENTER_DATA_CLAUSE_MASK \
13527 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13535 #define OACC_EXIT_DATA_CLAUSE_MASK \
13536 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13542 static void
13543 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13545 location_t loc = c_parser_peek_token (parser)->location;
13546 tree clauses, stmt;
13548 c_parser_consume_pragma (parser);
13550 if (!c_parser_next_token_is (parser, CPP_NAME))
13552 c_parser_error (parser, enter
13553 ? "expected %<data%> in %<#pragma acc enter data%>"
13554 : "expected %<data%> in %<#pragma acc exit data%>");
13555 c_parser_skip_to_pragma_eol (parser);
13556 return;
13559 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13560 if (strcmp (p, "data") != 0)
13562 c_parser_error (parser, "invalid pragma");
13563 c_parser_skip_to_pragma_eol (parser);
13564 return;
13567 c_parser_consume_token (parser);
13569 if (enter)
13570 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13571 "#pragma acc enter data");
13572 else
13573 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13574 "#pragma acc exit data");
13576 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13578 error_at (loc, enter
13579 ? "%<#pragma acc enter data%> has no data movement clause"
13580 : "%<#pragma acc exit data%> has no data movement clause");
13581 return;
13584 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13585 TREE_TYPE (stmt) = void_type_node;
13586 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13587 SET_EXPR_LOCATION (stmt, loc);
13588 add_stmt (stmt);
13592 /* OpenACC 2.0:
13594 # pragma acc loop oacc-loop-clause[optseq] new-line
13595 structured-block
13597 LOC is the location of the #pragma token.
13600 #define OACC_LOOP_CLAUSE_MASK \
13601 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
13609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
13611 static tree
13612 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
13613 omp_clause_mask mask, tree *cclauses)
13615 strcat (p_name, " loop");
13616 mask |= OACC_LOOP_CLAUSE_MASK;
13618 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
13619 cclauses == NULL);
13620 if (cclauses)
13622 clauses = c_oacc_split_loop_clauses (clauses, cclauses);
13623 if (*cclauses)
13624 c_finish_omp_clauses (*cclauses, false);
13625 if (clauses)
13626 c_finish_omp_clauses (clauses, false);
13629 tree block = c_begin_compound_stmt (true);
13630 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL);
13631 block = c_end_compound_stmt (loc, block, true);
13632 add_stmt (block);
13634 return stmt;
13637 /* OpenACC 2.0:
13638 # pragma acc kernels oacc-kernels-clause[optseq] new-line
13639 structured-block
13643 # pragma acc parallel oacc-parallel-clause[optseq] new-line
13644 structured-block
13646 LOC is the location of the #pragma token.
13649 #define OACC_KERNELS_CLAUSE_MASK \
13650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13665 #define OACC_PARALLEL_CLAUSE_MASK \
13666 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
13672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
13676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
13677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
13678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
13685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13687 static tree
13688 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
13689 enum pragma_kind p_kind, char *p_name)
13691 omp_clause_mask mask;
13692 enum tree_code code;
13693 switch (p_kind)
13695 case PRAGMA_OACC_KERNELS:
13696 strcat (p_name, " kernels");
13697 mask = OACC_KERNELS_CLAUSE_MASK;
13698 code = OACC_KERNELS;
13699 break;
13700 case PRAGMA_OACC_PARALLEL:
13701 strcat (p_name, " parallel");
13702 mask = OACC_PARALLEL_CLAUSE_MASK;
13703 code = OACC_PARALLEL;
13704 break;
13705 default:
13706 gcc_unreachable ();
13709 if (c_parser_next_token_is (parser, CPP_NAME))
13711 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13712 if (strcmp (p, "loop") == 0)
13714 c_parser_consume_token (parser);
13715 mask |= OACC_LOOP_CLAUSE_MASK;
13717 tree block = c_begin_omp_parallel ();
13718 tree clauses;
13719 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses);
13720 return c_finish_omp_construct (loc, code, block, clauses);
13724 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
13726 tree block = c_begin_omp_parallel ();
13727 add_stmt (c_parser_omp_structured_block (parser));
13729 return c_finish_omp_construct (loc, code, block, clauses);
13732 /* OpenACC 2.0:
13733 # pragma acc routine oacc-routine-clause[optseq] new-line
13734 function-definition
13736 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
13739 #define OACC_ROUTINE_CLAUSE_MASK \
13740 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
13745 /* Parse an OpenACC routine directive. For named directives, we apply
13746 immediately to the named function. For unnamed ones we then parse
13747 a declaration or definition, which must be for a function. */
13749 static void
13750 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
13752 tree decl = NULL_TREE;
13753 /* Create a dummy claue, to record location. */
13754 tree c_head = build_omp_clause (c_parser_peek_token (parser)->location,
13755 OMP_CLAUSE_SEQ);
13757 if (context != pragma_external)
13758 c_parser_error (parser, "%<#pragma acc routine%> not at file scope");
13760 c_parser_consume_pragma (parser);
13762 /* Scan for optional '( name )'. */
13763 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13765 c_parser_consume_token (parser);
13767 c_token *token = c_parser_peek_token (parser);
13769 if (token->type == CPP_NAME && (token->id_kind == C_ID_ID
13770 || token->id_kind == C_ID_TYPENAME))
13772 decl = lookup_name (token->value);
13773 if (!decl)
13775 error_at (token->location, "%qE has not been declared",
13776 token->value);
13777 decl = error_mark_node;
13780 else
13781 c_parser_error (parser, "expected function name");
13783 if (token->type != CPP_CLOSE_PAREN)
13784 c_parser_consume_token (parser);
13786 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13789 /* Build a chain of clauses. */
13790 parser->in_pragma = true;
13791 tree clauses = c_parser_oacc_all_clauses
13792 (parser, OACC_ROUTINE_CLAUSE_MASK, "#pragma acc routine");
13794 /* Force clauses to be non-null, by attaching context to it. */
13795 clauses = tree_cons (c_head, clauses, NULL_TREE);
13797 if (decl)
13798 c_finish_oacc_routine (parser, decl, clauses, true, true, false);
13799 else if (c_parser_peek_token (parser)->type == CPP_PRAGMA)
13800 /* This will emit an error. */
13801 c_finish_oacc_routine (parser, NULL_TREE, clauses, false, true, false);
13802 else
13803 c_parser_declaration_or_fndef (parser, true, false, false, false,
13804 true, NULL, vNULL, clauses);
13807 /* Finalize an OpenACC routine pragma, applying it to FNDECL. CLAUSES
13808 are the parsed clauses. IS_DEFN is true if we're applying it to
13809 the definition (so expect FNDEF to look somewhat defined. */
13811 static void
13812 c_finish_oacc_routine (c_parser *ARG_UNUSED (parser), tree fndecl,
13813 tree clauses, bool named, bool first, bool is_defn)
13815 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
13817 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL || !first)
13819 if (fndecl != error_mark_node)
13820 error_at (loc, "%<#pragma acc routine%> %s",
13821 named ? "does not refer to a function"
13822 : "not followed by single function");
13823 return;
13826 if (get_oacc_fn_attrib (fndecl))
13827 error_at (loc, "%<#pragma acc routine%> already applied to %D", fndecl);
13829 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
13830 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
13831 TREE_USED (fndecl) ? "use" : "definition");
13833 /* Process for function attrib */
13834 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
13835 replace_oacc_fn_attrib (fndecl, dims);
13837 /* Also attach as a declare. */
13838 DECL_ATTRIBUTES (fndecl)
13839 = tree_cons (get_identifier ("omp declare target"),
13840 clauses, DECL_ATTRIBUTES (fndecl));
13843 /* OpenACC 2.0:
13844 # pragma acc update oacc-update-clause[optseq] new-line
13847 #define OACC_UPDATE_CLAUSE_MASK \
13848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
13850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
13851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
13853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13855 static void
13856 c_parser_oacc_update (c_parser *parser)
13858 location_t loc = c_parser_peek_token (parser)->location;
13860 c_parser_consume_pragma (parser);
13862 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
13863 "#pragma acc update");
13864 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13866 error_at (loc,
13867 "%<#pragma acc update%> must contain at least one "
13868 "%<device%> or %<host%> or %<self%> clause");
13869 return;
13872 if (parser->error)
13873 return;
13875 tree stmt = make_node (OACC_UPDATE);
13876 TREE_TYPE (stmt) = void_type_node;
13877 OACC_UPDATE_CLAUSES (stmt) = clauses;
13878 SET_EXPR_LOCATION (stmt, loc);
13879 add_stmt (stmt);
13882 /* OpenACC 2.0:
13883 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
13885 LOC is the location of the #pragma token.
13888 #define OACC_WAIT_CLAUSE_MASK \
13889 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
13891 static tree
13892 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
13894 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
13896 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
13897 list = c_parser_oacc_wait_list (parser, loc, list);
13899 strcpy (p_name, " wait");
13900 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
13901 stmt = c_finish_oacc_wait (loc, list, clauses);
13903 return stmt;
13906 /* OpenMP 2.5:
13907 # pragma omp atomic new-line
13908 expression-stmt
13910 expression-stmt:
13911 x binop= expr | x++ | ++x | x-- | --x
13912 binop:
13913 +, *, -, /, &, ^, |, <<, >>
13915 where x is an lvalue expression with scalar type.
13917 OpenMP 3.1:
13918 # pragma omp atomic new-line
13919 update-stmt
13921 # pragma omp atomic read new-line
13922 read-stmt
13924 # pragma omp atomic write new-line
13925 write-stmt
13927 # pragma omp atomic update new-line
13928 update-stmt
13930 # pragma omp atomic capture new-line
13931 capture-stmt
13933 # pragma omp atomic capture new-line
13934 capture-block
13936 read-stmt:
13937 v = x
13938 write-stmt:
13939 x = expr
13940 update-stmt:
13941 expression-stmt | x = x binop expr
13942 capture-stmt:
13943 v = expression-stmt
13944 capture-block:
13945 { v = x; update-stmt; } | { update-stmt; v = x; }
13947 OpenMP 4.0:
13948 update-stmt:
13949 expression-stmt | x = x binop expr | x = expr binop x
13950 capture-stmt:
13951 v = update-stmt
13952 capture-block:
13953 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
13955 where x and v are lvalue expressions with scalar type.
13957 LOC is the location of the #pragma token. */
13959 static void
13960 c_parser_omp_atomic (location_t loc, c_parser *parser)
13962 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
13963 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
13964 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
13965 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
13966 struct c_expr expr;
13967 location_t eloc;
13968 bool structured_block = false;
13969 bool swapped = false;
13970 bool seq_cst = false;
13971 bool non_lvalue_p;
13973 if (c_parser_next_token_is (parser, CPP_NAME))
13975 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13976 if (!strcmp (p, "seq_cst"))
13978 seq_cst = true;
13979 c_parser_consume_token (parser);
13980 if (c_parser_next_token_is (parser, CPP_COMMA)
13981 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
13982 c_parser_consume_token (parser);
13985 if (c_parser_next_token_is (parser, CPP_NAME))
13987 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13989 if (!strcmp (p, "read"))
13990 code = OMP_ATOMIC_READ;
13991 else if (!strcmp (p, "write"))
13992 code = NOP_EXPR;
13993 else if (!strcmp (p, "update"))
13994 code = OMP_ATOMIC;
13995 else if (!strcmp (p, "capture"))
13996 code = OMP_ATOMIC_CAPTURE_NEW;
13997 else
13998 p = NULL;
13999 if (p)
14000 c_parser_consume_token (parser);
14002 if (!seq_cst)
14004 if (c_parser_next_token_is (parser, CPP_COMMA)
14005 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14006 c_parser_consume_token (parser);
14008 if (c_parser_next_token_is (parser, CPP_NAME))
14010 const char *p
14011 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14012 if (!strcmp (p, "seq_cst"))
14014 seq_cst = true;
14015 c_parser_consume_token (parser);
14019 c_parser_skip_to_pragma_eol (parser);
14021 switch (code)
14023 case OMP_ATOMIC_READ:
14024 case NOP_EXPR: /* atomic write */
14025 v = c_parser_cast_expression (parser, NULL).value;
14026 non_lvalue_p = !lvalue_p (v);
14027 v = c_fully_fold (v, false, NULL);
14028 if (v == error_mark_node)
14029 goto saw_error;
14030 if (non_lvalue_p)
14031 v = non_lvalue (v);
14032 loc = c_parser_peek_token (parser)->location;
14033 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14034 goto saw_error;
14035 if (code == NOP_EXPR)
14037 lhs = c_parser_expression (parser).value;
14038 lhs = c_fully_fold (lhs, false, NULL);
14039 if (lhs == error_mark_node)
14040 goto saw_error;
14042 else
14044 lhs = c_parser_cast_expression (parser, NULL).value;
14045 non_lvalue_p = !lvalue_p (lhs);
14046 lhs = c_fully_fold (lhs, false, NULL);
14047 if (lhs == error_mark_node)
14048 goto saw_error;
14049 if (non_lvalue_p)
14050 lhs = non_lvalue (lhs);
14052 if (code == NOP_EXPR)
14054 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14055 opcode. */
14056 code = OMP_ATOMIC;
14057 rhs = lhs;
14058 lhs = v;
14059 v = NULL_TREE;
14061 goto done;
14062 case OMP_ATOMIC_CAPTURE_NEW:
14063 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14065 c_parser_consume_token (parser);
14066 structured_block = true;
14068 else
14070 v = c_parser_cast_expression (parser, NULL).value;
14071 non_lvalue_p = !lvalue_p (v);
14072 v = c_fully_fold (v, false, NULL);
14073 if (v == error_mark_node)
14074 goto saw_error;
14075 if (non_lvalue_p)
14076 v = non_lvalue (v);
14077 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14078 goto saw_error;
14080 break;
14081 default:
14082 break;
14085 /* For structured_block case we don't know yet whether
14086 old or new x should be captured. */
14087 restart:
14088 eloc = c_parser_peek_token (parser)->location;
14089 expr = c_parser_cast_expression (parser, NULL);
14090 lhs = expr.value;
14091 expr = default_function_array_conversion (eloc, expr);
14092 unfolded_lhs = expr.value;
14093 lhs = c_fully_fold (lhs, false, NULL);
14094 orig_lhs = lhs;
14095 switch (TREE_CODE (lhs))
14097 case ERROR_MARK:
14098 saw_error:
14099 c_parser_skip_to_end_of_block_or_statement (parser);
14100 if (structured_block)
14102 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14103 c_parser_consume_token (parser);
14104 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14106 c_parser_skip_to_end_of_block_or_statement (parser);
14107 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14108 c_parser_consume_token (parser);
14111 return;
14113 case POSTINCREMENT_EXPR:
14114 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14115 code = OMP_ATOMIC_CAPTURE_OLD;
14116 /* FALLTHROUGH */
14117 case PREINCREMENT_EXPR:
14118 lhs = TREE_OPERAND (lhs, 0);
14119 unfolded_lhs = NULL_TREE;
14120 opcode = PLUS_EXPR;
14121 rhs = integer_one_node;
14122 break;
14124 case POSTDECREMENT_EXPR:
14125 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14126 code = OMP_ATOMIC_CAPTURE_OLD;
14127 /* FALLTHROUGH */
14128 case PREDECREMENT_EXPR:
14129 lhs = TREE_OPERAND (lhs, 0);
14130 unfolded_lhs = NULL_TREE;
14131 opcode = MINUS_EXPR;
14132 rhs = integer_one_node;
14133 break;
14135 case COMPOUND_EXPR:
14136 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14137 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14138 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14139 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14140 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14141 (TREE_OPERAND (lhs, 1), 0), 0)))
14142 == BOOLEAN_TYPE)
14143 /* Undo effects of boolean_increment for post {in,de}crement. */
14144 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14145 /* FALLTHRU */
14146 case MODIFY_EXPR:
14147 if (TREE_CODE (lhs) == MODIFY_EXPR
14148 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14150 /* Undo effects of boolean_increment. */
14151 if (integer_onep (TREE_OPERAND (lhs, 1)))
14153 /* This is pre or post increment. */
14154 rhs = TREE_OPERAND (lhs, 1);
14155 lhs = TREE_OPERAND (lhs, 0);
14156 unfolded_lhs = NULL_TREE;
14157 opcode = NOP_EXPR;
14158 if (code == OMP_ATOMIC_CAPTURE_NEW
14159 && !structured_block
14160 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14161 code = OMP_ATOMIC_CAPTURE_OLD;
14162 break;
14164 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14165 && TREE_OPERAND (lhs, 0)
14166 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14168 /* This is pre or post decrement. */
14169 rhs = TREE_OPERAND (lhs, 1);
14170 lhs = TREE_OPERAND (lhs, 0);
14171 unfolded_lhs = NULL_TREE;
14172 opcode = NOP_EXPR;
14173 if (code == OMP_ATOMIC_CAPTURE_NEW
14174 && !structured_block
14175 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14176 code = OMP_ATOMIC_CAPTURE_OLD;
14177 break;
14180 /* FALLTHRU */
14181 default:
14182 if (!lvalue_p (unfolded_lhs))
14183 lhs = non_lvalue (lhs);
14184 switch (c_parser_peek_token (parser)->type)
14186 case CPP_MULT_EQ:
14187 opcode = MULT_EXPR;
14188 break;
14189 case CPP_DIV_EQ:
14190 opcode = TRUNC_DIV_EXPR;
14191 break;
14192 case CPP_PLUS_EQ:
14193 opcode = PLUS_EXPR;
14194 break;
14195 case CPP_MINUS_EQ:
14196 opcode = MINUS_EXPR;
14197 break;
14198 case CPP_LSHIFT_EQ:
14199 opcode = LSHIFT_EXPR;
14200 break;
14201 case CPP_RSHIFT_EQ:
14202 opcode = RSHIFT_EXPR;
14203 break;
14204 case CPP_AND_EQ:
14205 opcode = BIT_AND_EXPR;
14206 break;
14207 case CPP_OR_EQ:
14208 opcode = BIT_IOR_EXPR;
14209 break;
14210 case CPP_XOR_EQ:
14211 opcode = BIT_XOR_EXPR;
14212 break;
14213 case CPP_EQ:
14214 c_parser_consume_token (parser);
14215 eloc = c_parser_peek_token (parser)->location;
14216 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14217 rhs1 = expr.value;
14218 switch (TREE_CODE (rhs1))
14220 case MULT_EXPR:
14221 case TRUNC_DIV_EXPR:
14222 case RDIV_EXPR:
14223 case PLUS_EXPR:
14224 case MINUS_EXPR:
14225 case LSHIFT_EXPR:
14226 case RSHIFT_EXPR:
14227 case BIT_AND_EXPR:
14228 case BIT_IOR_EXPR:
14229 case BIT_XOR_EXPR:
14230 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14232 opcode = TREE_CODE (rhs1);
14233 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14234 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14235 goto stmt_done;
14237 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14239 opcode = TREE_CODE (rhs1);
14240 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14241 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14242 swapped = !commutative_tree_code (opcode);
14243 goto stmt_done;
14245 break;
14246 case ERROR_MARK:
14247 goto saw_error;
14248 default:
14249 break;
14251 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14253 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14255 code = OMP_ATOMIC_CAPTURE_OLD;
14256 v = lhs;
14257 lhs = NULL_TREE;
14258 expr = default_function_array_read_conversion (eloc, expr);
14259 unfolded_lhs1 = expr.value;
14260 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14261 rhs1 = NULL_TREE;
14262 c_parser_consume_token (parser);
14263 goto restart;
14265 if (structured_block)
14267 opcode = NOP_EXPR;
14268 expr = default_function_array_read_conversion (eloc, expr);
14269 rhs = c_fully_fold (expr.value, false, NULL);
14270 rhs1 = NULL_TREE;
14271 goto stmt_done;
14274 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14275 goto saw_error;
14276 default:
14277 c_parser_error (parser,
14278 "invalid operator for %<#pragma omp atomic%>");
14279 goto saw_error;
14282 /* Arrange to pass the location of the assignment operator to
14283 c_finish_omp_atomic. */
14284 loc = c_parser_peek_token (parser)->location;
14285 c_parser_consume_token (parser);
14286 eloc = c_parser_peek_token (parser)->location;
14287 expr = c_parser_expression (parser);
14288 expr = default_function_array_read_conversion (eloc, expr);
14289 rhs = expr.value;
14290 rhs = c_fully_fold (rhs, false, NULL);
14291 break;
14293 stmt_done:
14294 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14296 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14297 goto saw_error;
14298 v = c_parser_cast_expression (parser, NULL).value;
14299 non_lvalue_p = !lvalue_p (v);
14300 v = c_fully_fold (v, false, NULL);
14301 if (v == error_mark_node)
14302 goto saw_error;
14303 if (non_lvalue_p)
14304 v = non_lvalue (v);
14305 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14306 goto saw_error;
14307 eloc = c_parser_peek_token (parser)->location;
14308 expr = c_parser_cast_expression (parser, NULL);
14309 lhs1 = expr.value;
14310 expr = default_function_array_read_conversion (eloc, expr);
14311 unfolded_lhs1 = expr.value;
14312 lhs1 = c_fully_fold (lhs1, false, NULL);
14313 if (lhs1 == error_mark_node)
14314 goto saw_error;
14315 if (!lvalue_p (unfolded_lhs1))
14316 lhs1 = non_lvalue (lhs1);
14318 if (structured_block)
14320 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14321 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14323 done:
14324 if (unfolded_lhs && unfolded_lhs1
14325 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14327 error ("%<#pragma omp atomic capture%> uses two different "
14328 "expressions for memory");
14329 stmt = error_mark_node;
14331 else
14332 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14333 swapped, seq_cst);
14334 if (stmt != error_mark_node)
14335 add_stmt (stmt);
14337 if (!structured_block)
14338 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14342 /* OpenMP 2.5:
14343 # pragma omp barrier new-line
14346 static void
14347 c_parser_omp_barrier (c_parser *parser)
14349 location_t loc = c_parser_peek_token (parser)->location;
14350 c_parser_consume_pragma (parser);
14351 c_parser_skip_to_pragma_eol (parser);
14353 c_finish_omp_barrier (loc);
14356 /* OpenMP 2.5:
14357 # pragma omp critical [(name)] new-line
14358 structured-block
14360 OpenMP 4.5:
14361 # pragma omp critical [(name) [hint(expression)]] new-line
14363 LOC is the location of the #pragma itself. */
14365 #define OMP_CRITICAL_CLAUSE_MASK \
14366 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14368 static tree
14369 c_parser_omp_critical (location_t loc, c_parser *parser)
14371 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14373 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14375 c_parser_consume_token (parser);
14376 if (c_parser_next_token_is (parser, CPP_NAME))
14378 name = c_parser_peek_token (parser)->value;
14379 c_parser_consume_token (parser);
14380 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14382 else
14383 c_parser_error (parser, "expected identifier");
14385 clauses = c_parser_omp_all_clauses (parser,
14386 OMP_CRITICAL_CLAUSE_MASK,
14387 "#pragma omp critical");
14389 else
14391 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14392 c_parser_error (parser, "expected %<(%> or end of line");
14393 c_parser_skip_to_pragma_eol (parser);
14396 stmt = c_parser_omp_structured_block (parser);
14397 return c_finish_omp_critical (loc, stmt, name, clauses);
14400 /* OpenMP 2.5:
14401 # pragma omp flush flush-vars[opt] new-line
14403 flush-vars:
14404 ( variable-list ) */
14406 static void
14407 c_parser_omp_flush (c_parser *parser)
14409 location_t loc = c_parser_peek_token (parser)->location;
14410 c_parser_consume_pragma (parser);
14411 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14412 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14413 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14414 c_parser_error (parser, "expected %<(%> or end of line");
14415 c_parser_skip_to_pragma_eol (parser);
14417 c_finish_omp_flush (loc);
14420 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14421 The real trick here is to determine the loop control variable early
14422 so that we can push a new decl if necessary to make it private.
14423 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14424 respectively. */
14426 static tree
14427 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14428 tree clauses, tree *cclauses)
14430 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14431 tree declv, condv, incrv, initv, ret = NULL_TREE;
14432 tree pre_body = NULL_TREE, this_pre_body;
14433 tree ordered_cl = NULL_TREE;
14434 bool fail = false, open_brace_parsed = false;
14435 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14436 location_t for_loc;
14437 vec<tree, va_gc> *for_block = make_tree_vector ();
14439 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14440 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14441 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14442 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14443 && OMP_CLAUSE_ORDERED_EXPR (cl))
14445 ordered_cl = cl;
14446 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14449 if (ordered && ordered < collapse)
14451 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14452 "%<ordered%> clause parameter is less than %<collapse%>");
14453 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14454 = build_int_cst (NULL_TREE, collapse);
14455 ordered = collapse;
14457 if (ordered)
14459 for (tree *pc = &clauses; *pc; )
14460 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14462 error_at (OMP_CLAUSE_LOCATION (*pc),
14463 "%<linear%> clause may not be specified together "
14464 "with %<ordered%> clause with a parameter");
14465 *pc = OMP_CLAUSE_CHAIN (*pc);
14467 else
14468 pc = &OMP_CLAUSE_CHAIN (*pc);
14471 gcc_assert (collapse >= 1 && ordered >= 0);
14472 count = ordered ? ordered : collapse;
14474 declv = make_tree_vec (count);
14475 initv = make_tree_vec (count);
14476 condv = make_tree_vec (count);
14477 incrv = make_tree_vec (count);
14479 if (code != CILK_FOR
14480 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14482 c_parser_error (parser, "for statement expected");
14483 return NULL;
14485 if (code == CILK_FOR
14486 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14488 c_parser_error (parser, "_Cilk_for statement expected");
14489 return NULL;
14491 for_loc = c_parser_peek_token (parser)->location;
14492 c_parser_consume_token (parser);
14494 for (i = 0; i < count; i++)
14496 int bracecount = 0;
14498 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14499 goto pop_scopes;
14501 /* Parse the initialization declaration or expression. */
14502 if (c_parser_next_tokens_start_declaration (parser))
14504 if (i > 0)
14505 vec_safe_push (for_block, c_begin_compound_stmt (true));
14506 this_pre_body = push_stmt_list ();
14507 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14508 NULL, vNULL);
14509 if (this_pre_body)
14511 this_pre_body = pop_stmt_list (this_pre_body);
14512 if (pre_body)
14514 tree t = pre_body;
14515 pre_body = push_stmt_list ();
14516 add_stmt (t);
14517 add_stmt (this_pre_body);
14518 pre_body = pop_stmt_list (pre_body);
14520 else
14521 pre_body = this_pre_body;
14523 decl = check_for_loop_decls (for_loc, flag_isoc99);
14524 if (decl == NULL)
14525 goto error_init;
14526 if (DECL_INITIAL (decl) == error_mark_node)
14527 decl = error_mark_node;
14528 init = decl;
14530 else if (c_parser_next_token_is (parser, CPP_NAME)
14531 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
14533 struct c_expr decl_exp;
14534 struct c_expr init_exp;
14535 location_t init_loc;
14537 decl_exp = c_parser_postfix_expression (parser);
14538 decl = decl_exp.value;
14540 c_parser_require (parser, CPP_EQ, "expected %<=%>");
14542 init_loc = c_parser_peek_token (parser)->location;
14543 init_exp = c_parser_expr_no_commas (parser, NULL);
14544 init_exp = default_function_array_read_conversion (init_loc,
14545 init_exp);
14546 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
14547 NOP_EXPR, init_loc, init_exp.value,
14548 init_exp.original_type);
14549 init = c_process_expr_stmt (init_loc, init);
14551 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14553 else
14555 error_init:
14556 c_parser_error (parser,
14557 "expected iteration declaration or initialization");
14558 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
14559 "expected %<)%>");
14560 fail = true;
14561 goto parse_next;
14564 /* Parse the loop condition. */
14565 cond = NULL_TREE;
14566 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
14568 location_t cond_loc = c_parser_peek_token (parser)->location;
14569 struct c_expr cond_expr
14570 = c_parser_binary_expression (parser, NULL, NULL_TREE);
14572 cond = cond_expr.value;
14573 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
14574 cond = c_fully_fold (cond, false, NULL);
14575 switch (cond_expr.original_code)
14577 case GT_EXPR:
14578 case GE_EXPR:
14579 case LT_EXPR:
14580 case LE_EXPR:
14581 break;
14582 case NE_EXPR:
14583 if (code == CILK_SIMD || code == CILK_FOR)
14584 break;
14585 /* FALLTHRU. */
14586 default:
14587 /* Can't be cond = error_mark_node, because we want to preserve
14588 the location until c_finish_omp_for. */
14589 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
14590 break;
14592 protected_set_expr_location (cond, cond_loc);
14594 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14596 /* Parse the increment expression. */
14597 incr = NULL_TREE;
14598 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
14600 location_t incr_loc = c_parser_peek_token (parser)->location;
14602 incr = c_process_expr_stmt (incr_loc,
14603 c_parser_expression (parser).value);
14605 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14607 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
14608 fail = true;
14609 else
14611 TREE_VEC_ELT (declv, i) = decl;
14612 TREE_VEC_ELT (initv, i) = init;
14613 TREE_VEC_ELT (condv, i) = cond;
14614 TREE_VEC_ELT (incrv, i) = incr;
14617 parse_next:
14618 if (i == count - 1)
14619 break;
14621 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
14622 in between the collapsed for loops to be still considered perfectly
14623 nested. Hopefully the final version clarifies this.
14624 For now handle (multiple) {'s and empty statements. */
14627 if (c_parser_next_token_is_keyword (parser, RID_FOR))
14629 c_parser_consume_token (parser);
14630 break;
14632 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14634 c_parser_consume_token (parser);
14635 bracecount++;
14637 else if (bracecount
14638 && c_parser_next_token_is (parser, CPP_SEMICOLON))
14639 c_parser_consume_token (parser);
14640 else
14642 c_parser_error (parser, "not enough perfectly nested loops");
14643 if (bracecount)
14645 open_brace_parsed = true;
14646 bracecount--;
14648 fail = true;
14649 count = 0;
14650 break;
14653 while (1);
14655 nbraces += bracecount;
14658 save_break = c_break_label;
14659 if (code == CILK_SIMD)
14660 c_break_label = build_int_cst (size_type_node, 2);
14661 else
14662 c_break_label = size_one_node;
14663 save_cont = c_cont_label;
14664 c_cont_label = NULL_TREE;
14665 body = push_stmt_list ();
14667 if (open_brace_parsed)
14669 location_t here = c_parser_peek_token (parser)->location;
14670 stmt = c_begin_compound_stmt (true);
14671 c_parser_compound_statement_nostart (parser);
14672 add_stmt (c_end_compound_stmt (here, stmt, true));
14674 else
14675 add_stmt (c_parser_c99_block_statement (parser));
14676 if (c_cont_label)
14678 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
14679 SET_EXPR_LOCATION (t, loc);
14680 add_stmt (t);
14683 body = pop_stmt_list (body);
14684 c_break_label = save_break;
14685 c_cont_label = save_cont;
14687 while (nbraces)
14689 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14691 c_parser_consume_token (parser);
14692 nbraces--;
14694 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
14695 c_parser_consume_token (parser);
14696 else
14698 c_parser_error (parser, "collapsed loops not perfectly nested");
14699 while (nbraces)
14701 location_t here = c_parser_peek_token (parser)->location;
14702 stmt = c_begin_compound_stmt (true);
14703 add_stmt (body);
14704 c_parser_compound_statement_nostart (parser);
14705 body = c_end_compound_stmt (here, stmt, true);
14706 nbraces--;
14708 goto pop_scopes;
14712 /* Only bother calling c_finish_omp_for if we haven't already generated
14713 an error from the initialization parsing. */
14714 if (!fail)
14716 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
14717 incrv, body, pre_body);
14719 /* Check for iterators appearing in lb, b or incr expressions. */
14720 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
14721 stmt = NULL_TREE;
14723 if (stmt)
14725 add_stmt (stmt);
14727 if (cclauses != NULL
14728 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
14730 tree *c;
14731 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
14732 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
14733 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
14734 c = &OMP_CLAUSE_CHAIN (*c);
14735 else
14737 for (i = 0; i < count; i++)
14738 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
14739 break;
14740 if (i == count)
14741 c = &OMP_CLAUSE_CHAIN (*c);
14742 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
14744 error_at (loc,
14745 "iteration variable %qD should not be firstprivate",
14746 OMP_CLAUSE_DECL (*c));
14747 *c = OMP_CLAUSE_CHAIN (*c);
14749 else
14751 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
14752 tree l = *c;
14753 *c = OMP_CLAUSE_CHAIN (*c);
14754 if (code == OMP_SIMD)
14756 OMP_CLAUSE_CHAIN (l)
14757 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14758 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
14760 else
14762 OMP_CLAUSE_CHAIN (l) = clauses;
14763 clauses = l;
14768 OMP_FOR_CLAUSES (stmt) = clauses;
14770 ret = stmt;
14772 pop_scopes:
14773 while (!for_block->is_empty ())
14775 /* FIXME diagnostics: LOC below should be the actual location of
14776 this particular for block. We need to build a list of
14777 locations to go along with FOR_BLOCK. */
14778 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
14779 add_stmt (stmt);
14781 release_tree_vector (for_block);
14782 return ret;
14785 /* Helper function for OpenMP parsing, split clauses and call
14786 finish_omp_clauses on each of the set of clauses afterwards. */
14788 static void
14789 omp_split_clauses (location_t loc, enum tree_code code,
14790 omp_clause_mask mask, tree clauses, tree *cclauses)
14792 int i;
14793 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
14794 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
14795 if (cclauses[i])
14796 cclauses[i] = c_finish_omp_clauses (cclauses[i], true);
14799 /* OpenMP 4.0:
14800 #pragma omp simd simd-clause[optseq] new-line
14801 for-loop
14803 LOC is the location of the #pragma token.
14806 #define OMP_SIMD_CLAUSE_MASK \
14807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
14808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
14809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
14810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
14811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
14813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
14816 static tree
14817 c_parser_omp_simd (location_t loc, c_parser *parser,
14818 char *p_name, omp_clause_mask mask, tree *cclauses)
14820 tree block, clauses, ret;
14822 strcat (p_name, " simd");
14823 mask |= OMP_SIMD_CLAUSE_MASK;
14825 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14826 if (cclauses)
14828 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
14829 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
14830 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
14831 OMP_CLAUSE_ORDERED);
14832 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
14834 error_at (OMP_CLAUSE_LOCATION (c),
14835 "%<ordered%> clause with parameter may not be specified "
14836 "on %qs construct", p_name);
14837 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
14841 block = c_begin_compound_stmt (true);
14842 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses);
14843 block = c_end_compound_stmt (loc, block, true);
14844 add_stmt (block);
14846 return ret;
14849 /* OpenMP 2.5:
14850 #pragma omp for for-clause[optseq] new-line
14851 for-loop
14853 OpenMP 4.0:
14854 #pragma omp for simd for-simd-clause[optseq] new-line
14855 for-loop
14857 LOC is the location of the #pragma token.
14860 #define OMP_FOR_CLAUSE_MASK \
14861 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
14862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
14863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
14864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
14865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
14866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
14867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
14868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
14869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
14871 static tree
14872 c_parser_omp_for (location_t loc, c_parser *parser,
14873 char *p_name, omp_clause_mask mask, tree *cclauses)
14875 tree block, clauses, ret;
14877 strcat (p_name, " for");
14878 mask |= OMP_FOR_CLAUSE_MASK;
14879 if (cclauses)
14880 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
14881 /* Composite distribute parallel for{, simd} disallows ordered clause. */
14882 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
14883 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
14885 if (c_parser_next_token_is (parser, CPP_NAME))
14887 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14889 if (strcmp (p, "simd") == 0)
14891 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
14892 if (cclauses == NULL)
14893 cclauses = cclauses_buf;
14895 c_parser_consume_token (parser);
14896 if (!flag_openmp) /* flag_openmp_simd */
14897 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
14898 block = c_begin_compound_stmt (true);
14899 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
14900 block = c_end_compound_stmt (loc, block, true);
14901 if (ret == NULL_TREE)
14902 return ret;
14903 ret = make_node (OMP_FOR);
14904 TREE_TYPE (ret) = void_type_node;
14905 OMP_FOR_BODY (ret) = block;
14906 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14907 SET_EXPR_LOCATION (ret, loc);
14908 add_stmt (ret);
14909 return ret;
14912 if (!flag_openmp) /* flag_openmp_simd */
14914 c_parser_skip_to_pragma_eol (parser, false);
14915 return NULL_TREE;
14918 /* Composite distribute parallel for disallows linear clause. */
14919 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
14920 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
14922 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
14923 if (cclauses)
14925 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
14926 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
14929 block = c_begin_compound_stmt (true);
14930 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses);
14931 block = c_end_compound_stmt (loc, block, true);
14932 add_stmt (block);
14934 return ret;
14937 /* OpenMP 2.5:
14938 # pragma omp master new-line
14939 structured-block
14941 LOC is the location of the #pragma token.
14944 static tree
14945 c_parser_omp_master (location_t loc, c_parser *parser)
14947 c_parser_skip_to_pragma_eol (parser);
14948 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser));
14951 /* OpenMP 2.5:
14952 # pragma omp ordered new-line
14953 structured-block
14955 OpenMP 4.5:
14956 # pragma omp ordered ordered-clauses new-line
14957 structured-block
14959 # pragma omp ordered depend-clauses new-line */
14961 #define OMP_ORDERED_CLAUSE_MASK \
14962 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
14963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
14965 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
14966 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
14968 static bool
14969 c_parser_omp_ordered (c_parser *parser, enum pragma_context context)
14971 location_t loc = c_parser_peek_token (parser)->location;
14972 c_parser_consume_pragma (parser);
14974 if (context != pragma_stmt && context != pragma_compound)
14976 c_parser_error (parser, "expected declaration specifiers");
14977 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
14978 return false;
14981 if (c_parser_next_token_is (parser, CPP_NAME))
14983 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14985 if (!strcmp ("depend", p))
14987 if (context == pragma_stmt)
14989 error_at (loc,
14990 "%<#pragma omp ordered%> with %<depend> clause may "
14991 "only be used in compound statements");
14992 c_parser_skip_to_pragma_eol (parser, false);
14993 return false;
14996 tree clauses
14997 = c_parser_omp_all_clauses (parser,
14998 OMP_ORDERED_DEPEND_CLAUSE_MASK,
14999 "#pragma omp ordered");
15000 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15001 return false;
15005 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15006 "#pragma omp ordered");
15007 c_finish_omp_ordered (loc, clauses,
15008 c_parser_omp_structured_block (parser));
15009 return true;
15012 /* OpenMP 2.5:
15014 section-scope:
15015 { section-sequence }
15017 section-sequence:
15018 section-directive[opt] structured-block
15019 section-sequence section-directive structured-block
15021 SECTIONS_LOC is the location of the #pragma omp sections. */
15023 static tree
15024 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15026 tree stmt, substmt;
15027 bool error_suppress = false;
15028 location_t loc;
15030 loc = c_parser_peek_token (parser)->location;
15031 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15033 /* Avoid skipping until the end of the block. */
15034 parser->error = false;
15035 return NULL_TREE;
15038 stmt = push_stmt_list ();
15040 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15042 substmt = c_parser_omp_structured_block (parser);
15043 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15044 SET_EXPR_LOCATION (substmt, loc);
15045 add_stmt (substmt);
15048 while (1)
15050 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15051 break;
15052 if (c_parser_next_token_is (parser, CPP_EOF))
15053 break;
15055 loc = c_parser_peek_token (parser)->location;
15056 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15058 c_parser_consume_pragma (parser);
15059 c_parser_skip_to_pragma_eol (parser);
15060 error_suppress = false;
15062 else if (!error_suppress)
15064 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15065 error_suppress = true;
15068 substmt = c_parser_omp_structured_block (parser);
15069 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15070 SET_EXPR_LOCATION (substmt, loc);
15071 add_stmt (substmt);
15073 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15074 "expected %<#pragma omp section%> or %<}%>");
15076 substmt = pop_stmt_list (stmt);
15078 stmt = make_node (OMP_SECTIONS);
15079 SET_EXPR_LOCATION (stmt, sections_loc);
15080 TREE_TYPE (stmt) = void_type_node;
15081 OMP_SECTIONS_BODY (stmt) = substmt;
15083 return add_stmt (stmt);
15086 /* OpenMP 2.5:
15087 # pragma omp sections sections-clause[optseq] newline
15088 sections-scope
15090 LOC is the location of the #pragma token.
15093 #define OMP_SECTIONS_CLAUSE_MASK \
15094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15100 static tree
15101 c_parser_omp_sections (location_t loc, c_parser *parser,
15102 char *p_name, omp_clause_mask mask, tree *cclauses)
15104 tree block, clauses, ret;
15106 strcat (p_name, " sections");
15107 mask |= OMP_SECTIONS_CLAUSE_MASK;
15108 if (cclauses)
15109 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15111 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15112 if (cclauses)
15114 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15115 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15118 block = c_begin_compound_stmt (true);
15119 ret = c_parser_omp_sections_scope (loc, parser);
15120 if (ret)
15121 OMP_SECTIONS_CLAUSES (ret) = clauses;
15122 block = c_end_compound_stmt (loc, block, true);
15123 add_stmt (block);
15125 return ret;
15128 /* OpenMP 2.5:
15129 # pragma omp parallel parallel-clause[optseq] new-line
15130 structured-block
15131 # pragma omp parallel for parallel-for-clause[optseq] new-line
15132 structured-block
15133 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15134 structured-block
15136 OpenMP 4.0:
15137 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15138 structured-block
15140 LOC is the location of the #pragma token.
15143 #define OMP_PARALLEL_CLAUSE_MASK \
15144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15154 static tree
15155 c_parser_omp_parallel (location_t loc, c_parser *parser,
15156 char *p_name, omp_clause_mask mask, tree *cclauses)
15158 tree stmt, clauses, block;
15160 strcat (p_name, " parallel");
15161 mask |= OMP_PARALLEL_CLAUSE_MASK;
15162 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15163 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15164 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15165 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15167 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15169 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15170 if (cclauses == NULL)
15171 cclauses = cclauses_buf;
15173 c_parser_consume_token (parser);
15174 if (!flag_openmp) /* flag_openmp_simd */
15175 return c_parser_omp_for (loc, parser, p_name, mask, cclauses);
15176 block = c_begin_omp_parallel ();
15177 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses);
15178 stmt
15179 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15180 block);
15181 if (ret == NULL_TREE)
15182 return ret;
15183 OMP_PARALLEL_COMBINED (stmt) = 1;
15184 return stmt;
15186 /* When combined with distribute, parallel has to be followed by for.
15187 #pragma omp target parallel is allowed though. */
15188 else if (cclauses
15189 && (mask & (OMP_CLAUSE_MASK_1
15190 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15192 error_at (loc, "expected %<for%> after %qs", p_name);
15193 c_parser_skip_to_pragma_eol (parser);
15194 return NULL_TREE;
15196 else if (!flag_openmp) /* flag_openmp_simd */
15198 c_parser_skip_to_pragma_eol (parser, false);
15199 return NULL_TREE;
15201 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15203 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15204 if (strcmp (p, "sections") == 0)
15206 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15207 if (cclauses == NULL)
15208 cclauses = cclauses_buf;
15210 c_parser_consume_token (parser);
15211 block = c_begin_omp_parallel ();
15212 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15213 stmt = c_finish_omp_parallel (loc,
15214 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15215 block);
15216 OMP_PARALLEL_COMBINED (stmt) = 1;
15217 return stmt;
15221 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15222 if (cclauses)
15224 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15225 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15228 block = c_begin_omp_parallel ();
15229 c_parser_statement (parser);
15230 stmt = c_finish_omp_parallel (loc, clauses, block);
15232 return stmt;
15235 /* OpenMP 2.5:
15236 # pragma omp single single-clause[optseq] new-line
15237 structured-block
15239 LOC is the location of the #pragma.
15242 #define OMP_SINGLE_CLAUSE_MASK \
15243 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15248 static tree
15249 c_parser_omp_single (location_t loc, c_parser *parser)
15251 tree stmt = make_node (OMP_SINGLE);
15252 SET_EXPR_LOCATION (stmt, loc);
15253 TREE_TYPE (stmt) = void_type_node;
15255 OMP_SINGLE_CLAUSES (stmt)
15256 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15257 "#pragma omp single");
15258 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser);
15260 return add_stmt (stmt);
15263 /* OpenMP 3.0:
15264 # pragma omp task task-clause[optseq] new-line
15266 LOC is the location of the #pragma.
15269 #define OMP_TASK_CLAUSE_MASK \
15270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15281 static tree
15282 c_parser_omp_task (location_t loc, c_parser *parser)
15284 tree clauses, block;
15286 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15287 "#pragma omp task");
15289 block = c_begin_omp_task ();
15290 c_parser_statement (parser);
15291 return c_finish_omp_task (loc, clauses, block);
15294 /* OpenMP 3.0:
15295 # pragma omp taskwait new-line
15298 static void
15299 c_parser_omp_taskwait (c_parser *parser)
15301 location_t loc = c_parser_peek_token (parser)->location;
15302 c_parser_consume_pragma (parser);
15303 c_parser_skip_to_pragma_eol (parser);
15305 c_finish_omp_taskwait (loc);
15308 /* OpenMP 3.1:
15309 # pragma omp taskyield new-line
15312 static void
15313 c_parser_omp_taskyield (c_parser *parser)
15315 location_t loc = c_parser_peek_token (parser)->location;
15316 c_parser_consume_pragma (parser);
15317 c_parser_skip_to_pragma_eol (parser);
15319 c_finish_omp_taskyield (loc);
15322 /* OpenMP 4.0:
15323 # pragma omp taskgroup new-line
15326 static tree
15327 c_parser_omp_taskgroup (c_parser *parser)
15329 location_t loc = c_parser_peek_token (parser)->location;
15330 c_parser_skip_to_pragma_eol (parser);
15331 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser));
15334 /* OpenMP 4.0:
15335 # pragma omp cancel cancel-clause[optseq] new-line
15337 LOC is the location of the #pragma.
15340 #define OMP_CANCEL_CLAUSE_MASK \
15341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15347 static void
15348 c_parser_omp_cancel (c_parser *parser)
15350 location_t loc = c_parser_peek_token (parser)->location;
15352 c_parser_consume_pragma (parser);
15353 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15354 "#pragma omp cancel");
15356 c_finish_omp_cancel (loc, clauses);
15359 /* OpenMP 4.0:
15360 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15362 LOC is the location of the #pragma.
15365 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15366 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15371 static void
15372 c_parser_omp_cancellation_point (c_parser *parser)
15374 location_t loc = c_parser_peek_token (parser)->location;
15375 tree clauses;
15376 bool point_seen = false;
15378 c_parser_consume_pragma (parser);
15379 if (c_parser_next_token_is (parser, CPP_NAME))
15381 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15382 if (strcmp (p, "point") == 0)
15384 c_parser_consume_token (parser);
15385 point_seen = true;
15388 if (!point_seen)
15390 c_parser_error (parser, "expected %<point%>");
15391 c_parser_skip_to_pragma_eol (parser);
15392 return;
15395 clauses
15396 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15397 "#pragma omp cancellation point");
15399 c_finish_omp_cancellation_point (loc, clauses);
15402 /* OpenMP 4.0:
15403 #pragma omp distribute distribute-clause[optseq] new-line
15404 for-loop */
15406 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15407 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15413 static tree
15414 c_parser_omp_distribute (location_t loc, c_parser *parser,
15415 char *p_name, omp_clause_mask mask, tree *cclauses)
15417 tree clauses, block, ret;
15419 strcat (p_name, " distribute");
15420 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15422 if (c_parser_next_token_is (parser, CPP_NAME))
15424 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15425 bool simd = false;
15426 bool parallel = false;
15428 if (strcmp (p, "simd") == 0)
15429 simd = true;
15430 else
15431 parallel = strcmp (p, "parallel") == 0;
15432 if (parallel || simd)
15434 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15435 if (cclauses == NULL)
15436 cclauses = cclauses_buf;
15437 c_parser_consume_token (parser);
15438 if (!flag_openmp) /* flag_openmp_simd */
15440 if (simd)
15441 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
15442 else
15443 return c_parser_omp_parallel (loc, parser, p_name, mask,
15444 cclauses);
15446 block = c_begin_compound_stmt (true);
15447 if (simd)
15448 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
15449 else
15450 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses);
15451 block = c_end_compound_stmt (loc, block, true);
15452 if (ret == NULL)
15453 return ret;
15454 ret = make_node (OMP_DISTRIBUTE);
15455 TREE_TYPE (ret) = void_type_node;
15456 OMP_FOR_BODY (ret) = block;
15457 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15458 SET_EXPR_LOCATION (ret, loc);
15459 add_stmt (ret);
15460 return ret;
15463 if (!flag_openmp) /* flag_openmp_simd */
15465 c_parser_skip_to_pragma_eol (parser, false);
15466 return NULL_TREE;
15469 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15470 if (cclauses)
15472 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15473 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15476 block = c_begin_compound_stmt (true);
15477 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL);
15478 block = c_end_compound_stmt (loc, block, true);
15479 add_stmt (block);
15481 return ret;
15484 /* OpenMP 4.0:
15485 # pragma omp teams teams-clause[optseq] new-line
15486 structured-block */
15488 #define OMP_TEAMS_CLAUSE_MASK \
15489 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
15497 static tree
15498 c_parser_omp_teams (location_t loc, c_parser *parser,
15499 char *p_name, omp_clause_mask mask, tree *cclauses)
15501 tree clauses, block, ret;
15503 strcat (p_name, " teams");
15504 mask |= OMP_TEAMS_CLAUSE_MASK;
15506 if (c_parser_next_token_is (parser, CPP_NAME))
15508 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15509 if (strcmp (p, "distribute") == 0)
15511 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15512 if (cclauses == NULL)
15513 cclauses = cclauses_buf;
15515 c_parser_consume_token (parser);
15516 if (!flag_openmp) /* flag_openmp_simd */
15517 return c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
15518 block = c_begin_compound_stmt (true);
15519 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses);
15520 block = c_end_compound_stmt (loc, block, true);
15521 if (ret == NULL)
15522 return ret;
15523 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15524 ret = make_node (OMP_TEAMS);
15525 TREE_TYPE (ret) = void_type_node;
15526 OMP_TEAMS_CLAUSES (ret) = clauses;
15527 OMP_TEAMS_BODY (ret) = block;
15528 OMP_TEAMS_COMBINED (ret) = 1;
15529 return add_stmt (ret);
15532 if (!flag_openmp) /* flag_openmp_simd */
15534 c_parser_skip_to_pragma_eol (parser, false);
15535 return NULL_TREE;
15538 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15539 if (cclauses)
15541 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
15542 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15545 tree stmt = make_node (OMP_TEAMS);
15546 TREE_TYPE (stmt) = void_type_node;
15547 OMP_TEAMS_CLAUSES (stmt) = clauses;
15548 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser);
15550 return add_stmt (stmt);
15553 /* OpenMP 4.0:
15554 # pragma omp target data target-data-clause[optseq] new-line
15555 structured-block */
15557 #define OMP_TARGET_DATA_CLAUSE_MASK \
15558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
15563 static tree
15564 c_parser_omp_target_data (location_t loc, c_parser *parser)
15566 tree clauses
15567 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
15568 "#pragma omp target data");
15569 int map_seen = 0;
15570 for (tree *pc = &clauses; *pc;)
15572 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15573 switch (OMP_CLAUSE_MAP_KIND (*pc))
15575 case GOMP_MAP_TO:
15576 case GOMP_MAP_ALWAYS_TO:
15577 case GOMP_MAP_FROM:
15578 case GOMP_MAP_ALWAYS_FROM:
15579 case GOMP_MAP_TOFROM:
15580 case GOMP_MAP_ALWAYS_TOFROM:
15581 case GOMP_MAP_ALLOC:
15582 map_seen = 3;
15583 break;
15584 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15585 case GOMP_MAP_ALWAYS_POINTER:
15586 break;
15587 default:
15588 map_seen |= 1;
15589 error_at (OMP_CLAUSE_LOCATION (*pc),
15590 "%<#pragma omp target data%> with map-type other "
15591 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
15592 "on %<map%> clause");
15593 *pc = OMP_CLAUSE_CHAIN (*pc);
15594 continue;
15596 pc = &OMP_CLAUSE_CHAIN (*pc);
15599 if (map_seen != 3)
15601 if (map_seen == 0)
15602 error_at (loc,
15603 "%<#pragma omp target data%> must contain at least "
15604 "one %<map%> clause");
15605 return NULL_TREE;
15608 tree stmt = make_node (OMP_TARGET_DATA);
15609 TREE_TYPE (stmt) = void_type_node;
15610 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
15611 keep_next_level ();
15612 tree block = c_begin_compound_stmt (true);
15613 add_stmt (c_parser_omp_structured_block (parser));
15614 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
15616 SET_EXPR_LOCATION (stmt, loc);
15617 return add_stmt (stmt);
15620 /* OpenMP 4.0:
15621 # pragma omp target update target-update-clause[optseq] new-line */
15623 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
15624 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
15625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
15626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15631 static bool
15632 c_parser_omp_target_update (location_t loc, c_parser *parser,
15633 enum pragma_context context)
15635 if (context == pragma_stmt)
15637 error_at (loc,
15638 "%<#pragma omp target update%> may only be "
15639 "used in compound statements");
15640 c_parser_skip_to_pragma_eol (parser, false);
15641 return false;
15644 tree clauses
15645 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
15646 "#pragma omp target update");
15647 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
15648 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
15650 error_at (loc,
15651 "%<#pragma omp target update%> must contain at least one "
15652 "%<from%> or %<to%> clauses");
15653 return false;
15656 tree stmt = make_node (OMP_TARGET_UPDATE);
15657 TREE_TYPE (stmt) = void_type_node;
15658 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
15659 SET_EXPR_LOCATION (stmt, loc);
15660 add_stmt (stmt);
15661 return false;
15664 /* OpenMP 4.5:
15665 # pragma omp target enter data target-data-clause[optseq] new-line */
15667 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
15668 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15674 static tree
15675 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
15676 enum pragma_context context)
15678 bool data_seen = false;
15679 if (c_parser_next_token_is (parser, CPP_NAME))
15681 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15682 if (strcmp (p, "data") == 0)
15684 c_parser_consume_token (parser);
15685 data_seen = true;
15688 if (!data_seen)
15690 c_parser_error (parser, "expected %<data%>");
15691 c_parser_skip_to_pragma_eol (parser);
15692 return NULL_TREE;
15695 if (context == pragma_stmt)
15697 error_at (loc,
15698 "%<#pragma omp target enter data%> may only be "
15699 "used in compound statements");
15700 c_parser_skip_to_pragma_eol (parser, false);
15701 return NULL_TREE;
15704 tree clauses
15705 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
15706 "#pragma omp target enter data");
15707 int map_seen = 0;
15708 for (tree *pc = &clauses; *pc;)
15710 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15711 switch (OMP_CLAUSE_MAP_KIND (*pc))
15713 case GOMP_MAP_TO:
15714 case GOMP_MAP_ALWAYS_TO:
15715 case GOMP_MAP_ALLOC:
15716 map_seen = 3;
15717 break;
15718 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15719 case GOMP_MAP_ALWAYS_POINTER:
15720 break;
15721 default:
15722 map_seen |= 1;
15723 error_at (OMP_CLAUSE_LOCATION (*pc),
15724 "%<#pragma omp target enter data%> with map-type other "
15725 "than %<to%> or %<alloc%> on %<map%> clause");
15726 *pc = OMP_CLAUSE_CHAIN (*pc);
15727 continue;
15729 pc = &OMP_CLAUSE_CHAIN (*pc);
15732 if (map_seen != 3)
15734 if (map_seen == 0)
15735 error_at (loc,
15736 "%<#pragma omp target enter data%> must contain at least "
15737 "one %<map%> clause");
15738 return NULL_TREE;
15741 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
15742 TREE_TYPE (stmt) = void_type_node;
15743 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
15744 SET_EXPR_LOCATION (stmt, loc);
15745 add_stmt (stmt);
15746 return stmt;
15749 /* OpenMP 4.5:
15750 # pragma omp target exit data target-data-clause[optseq] new-line */
15752 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
15753 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15759 static tree
15760 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
15761 enum pragma_context context)
15763 bool data_seen = false;
15764 if (c_parser_next_token_is (parser, CPP_NAME))
15766 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15767 if (strcmp (p, "data") == 0)
15769 c_parser_consume_token (parser);
15770 data_seen = true;
15773 if (!data_seen)
15775 c_parser_error (parser, "expected %<data%>");
15776 c_parser_skip_to_pragma_eol (parser);
15777 return NULL_TREE;
15780 if (context == pragma_stmt)
15782 error_at (loc,
15783 "%<#pragma omp target exit data%> may only be "
15784 "used in compound statements");
15785 c_parser_skip_to_pragma_eol (parser, false);
15786 return NULL_TREE;
15789 tree clauses
15790 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
15791 "#pragma omp target exit data");
15793 int map_seen = 0;
15794 for (tree *pc = &clauses; *pc;)
15796 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
15797 switch (OMP_CLAUSE_MAP_KIND (*pc))
15799 case GOMP_MAP_FROM:
15800 case GOMP_MAP_ALWAYS_FROM:
15801 case GOMP_MAP_RELEASE:
15802 case GOMP_MAP_DELETE:
15803 map_seen = 3;
15804 break;
15805 case GOMP_MAP_FIRSTPRIVATE_POINTER:
15806 case GOMP_MAP_ALWAYS_POINTER:
15807 break;
15808 default:
15809 map_seen |= 1;
15810 error_at (OMP_CLAUSE_LOCATION (*pc),
15811 "%<#pragma omp target exit data%> with map-type other "
15812 "than %<from%>, %<release> or %<delete%> on %<map%>"
15813 " clause");
15814 *pc = OMP_CLAUSE_CHAIN (*pc);
15815 continue;
15817 pc = &OMP_CLAUSE_CHAIN (*pc);
15820 if (map_seen != 3)
15822 if (map_seen == 0)
15823 error_at (loc,
15824 "%<#pragma omp target exit data%> must contain at least one "
15825 "%<map%> clause");
15826 return NULL_TREE;
15829 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
15830 TREE_TYPE (stmt) = void_type_node;
15831 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
15832 SET_EXPR_LOCATION (stmt, loc);
15833 add_stmt (stmt);
15834 return stmt;
15837 /* OpenMP 4.0:
15838 # pragma omp target target-clause[optseq] new-line
15839 structured-block */
15841 #define OMP_TARGET_CLAUSE_MASK \
15842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
15843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
15844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
15847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
15850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
15852 static bool
15853 c_parser_omp_target (c_parser *parser, enum pragma_context context)
15855 location_t loc = c_parser_peek_token (parser)->location;
15856 c_parser_consume_pragma (parser);
15857 tree *pc = NULL, stmt, block;
15859 if (context != pragma_stmt && context != pragma_compound)
15861 c_parser_error (parser, "expected declaration specifiers");
15862 c_parser_skip_to_pragma_eol (parser);
15863 return false;
15866 if (c_parser_next_token_is (parser, CPP_NAME))
15868 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15869 enum tree_code ccode = ERROR_MARK;
15871 if (strcmp (p, "teams") == 0)
15872 ccode = OMP_TEAMS;
15873 else if (strcmp (p, "parallel") == 0)
15874 ccode = OMP_PARALLEL;
15875 else if (strcmp (p, "simd") == 0)
15876 ccode = OMP_SIMD;
15877 if (ccode != ERROR_MARK)
15879 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
15880 char p_name[sizeof ("#pragma omp target teams distribute "
15881 "parallel for simd")];
15883 c_parser_consume_token (parser);
15884 strcpy (p_name, "#pragma omp target");
15885 if (!flag_openmp) /* flag_openmp_simd */
15887 tree stmt;
15888 switch (ccode)
15890 case OMP_TEAMS:
15891 stmt = c_parser_omp_teams (loc, parser, p_name,
15892 OMP_TARGET_CLAUSE_MASK,
15893 cclauses);
15894 break;
15895 case OMP_PARALLEL:
15896 stmt = c_parser_omp_parallel (loc, parser, p_name,
15897 OMP_TARGET_CLAUSE_MASK,
15898 cclauses);
15899 break;
15900 case OMP_SIMD:
15901 stmt = c_parser_omp_simd (loc, parser, p_name,
15902 OMP_TARGET_CLAUSE_MASK,
15903 cclauses);
15904 break;
15905 default:
15906 gcc_unreachable ();
15908 return stmt != NULL_TREE;
15910 keep_next_level ();
15911 tree block = c_begin_compound_stmt (true), ret;
15912 switch (ccode)
15914 case OMP_TEAMS:
15915 ret = c_parser_omp_teams (loc, parser, p_name,
15916 OMP_TARGET_CLAUSE_MASK, cclauses);
15917 break;
15918 case OMP_PARALLEL:
15919 ret = c_parser_omp_parallel (loc, parser, p_name,
15920 OMP_TARGET_CLAUSE_MASK, cclauses);
15921 break;
15922 case OMP_SIMD:
15923 ret = c_parser_omp_simd (loc, parser, p_name,
15924 OMP_TARGET_CLAUSE_MASK, cclauses);
15925 break;
15926 default:
15927 gcc_unreachable ();
15929 block = c_end_compound_stmt (loc, block, true);
15930 if (ret == NULL_TREE)
15931 return false;
15932 if (ccode == OMP_TEAMS)
15934 /* For combined target teams, ensure the num_teams and
15935 thread_limit clause expressions are evaluated on the host,
15936 before entering the target construct. */
15937 tree c;
15938 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
15939 c; c = OMP_CLAUSE_CHAIN (c))
15940 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
15941 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
15942 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
15944 tree expr = OMP_CLAUSE_OPERAND (c, 0);
15945 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
15946 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
15947 expr, NULL_TREE, NULL_TREE);
15948 add_stmt (expr);
15949 OMP_CLAUSE_OPERAND (c, 0) = expr;
15950 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
15951 OMP_CLAUSE_FIRSTPRIVATE);
15952 OMP_CLAUSE_DECL (tc) = tmp;
15953 OMP_CLAUSE_CHAIN (tc)
15954 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
15955 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
15958 tree stmt = make_node (OMP_TARGET);
15959 TREE_TYPE (stmt) = void_type_node;
15960 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
15961 OMP_TARGET_BODY (stmt) = block;
15962 OMP_TARGET_COMBINED (stmt) = 1;
15963 add_stmt (stmt);
15964 pc = &OMP_TARGET_CLAUSES (stmt);
15965 goto check_clauses;
15967 else if (!flag_openmp) /* flag_openmp_simd */
15969 c_parser_skip_to_pragma_eol (parser, false);
15970 return false;
15972 else if (strcmp (p, "data") == 0)
15974 c_parser_consume_token (parser);
15975 c_parser_omp_target_data (loc, parser);
15976 return true;
15978 else if (strcmp (p, "enter") == 0)
15980 c_parser_consume_token (parser);
15981 c_parser_omp_target_enter_data (loc, parser, context);
15982 return false;
15984 else if (strcmp (p, "exit") == 0)
15986 c_parser_consume_token (parser);
15987 c_parser_omp_target_exit_data (loc, parser, context);
15988 return false;
15990 else if (strcmp (p, "update") == 0)
15992 c_parser_consume_token (parser);
15993 return c_parser_omp_target_update (loc, parser, context);
15997 stmt = make_node (OMP_TARGET);
15998 TREE_TYPE (stmt) = void_type_node;
16000 OMP_TARGET_CLAUSES (stmt)
16001 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16002 "#pragma omp target");
16003 pc = &OMP_TARGET_CLAUSES (stmt);
16004 keep_next_level ();
16005 block = c_begin_compound_stmt (true);
16006 add_stmt (c_parser_omp_structured_block (parser));
16007 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16009 SET_EXPR_LOCATION (stmt, loc);
16010 add_stmt (stmt);
16012 check_clauses:
16013 while (*pc)
16015 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16016 switch (OMP_CLAUSE_MAP_KIND (*pc))
16018 case GOMP_MAP_TO:
16019 case GOMP_MAP_ALWAYS_TO:
16020 case GOMP_MAP_FROM:
16021 case GOMP_MAP_ALWAYS_FROM:
16022 case GOMP_MAP_TOFROM:
16023 case GOMP_MAP_ALWAYS_TOFROM:
16024 case GOMP_MAP_ALLOC:
16025 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16026 case GOMP_MAP_ALWAYS_POINTER:
16027 break;
16028 default:
16029 error_at (OMP_CLAUSE_LOCATION (*pc),
16030 "%<#pragma omp target%> with map-type other "
16031 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16032 "on %<map%> clause");
16033 *pc = OMP_CLAUSE_CHAIN (*pc);
16034 continue;
16036 pc = &OMP_CLAUSE_CHAIN (*pc);
16038 return true;
16041 /* OpenMP 4.0:
16042 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16044 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16045 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16052 static void
16053 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16055 vec<c_token> clauses = vNULL;
16056 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16058 c_token *token = c_parser_peek_token (parser);
16059 if (token->type == CPP_EOF)
16061 c_parser_skip_to_pragma_eol (parser);
16062 clauses.release ();
16063 return;
16065 clauses.safe_push (*token);
16066 c_parser_consume_token (parser);
16068 clauses.safe_push (*c_parser_peek_token (parser));
16069 c_parser_skip_to_pragma_eol (parser);
16071 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16073 if (c_parser_peek_token (parser)->pragma_kind
16074 != PRAGMA_OMP_DECLARE_REDUCTION
16075 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16076 || strcmp (IDENTIFIER_POINTER
16077 (c_parser_peek_2nd_token (parser)->value),
16078 "simd") != 0)
16080 c_parser_error (parser,
16081 "%<#pragma omp declare simd%> must be followed by "
16082 "function declaration or definition or another "
16083 "%<#pragma omp declare simd%>");
16084 clauses.release ();
16085 return;
16087 c_parser_consume_pragma (parser);
16088 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16090 c_token *token = c_parser_peek_token (parser);
16091 if (token->type == CPP_EOF)
16093 c_parser_skip_to_pragma_eol (parser);
16094 clauses.release ();
16095 return;
16097 clauses.safe_push (*token);
16098 c_parser_consume_token (parser);
16100 clauses.safe_push (*c_parser_peek_token (parser));
16101 c_parser_skip_to_pragma_eol (parser);
16104 /* Make sure nothing tries to read past the end of the tokens. */
16105 c_token eof_token;
16106 memset (&eof_token, 0, sizeof (eof_token));
16107 eof_token.type = CPP_EOF;
16108 clauses.safe_push (eof_token);
16109 clauses.safe_push (eof_token);
16111 switch (context)
16113 case pragma_external:
16114 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16115 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16117 int ext = disable_extension_diagnostics ();
16119 c_parser_consume_token (parser);
16120 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16121 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16122 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16123 NULL, clauses);
16124 restore_extension_diagnostics (ext);
16126 else
16127 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16128 NULL, clauses);
16129 break;
16130 case pragma_struct:
16131 case pragma_param:
16132 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16133 "function declaration or definition");
16134 break;
16135 case pragma_compound:
16136 case pragma_stmt:
16137 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16138 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16140 int ext = disable_extension_diagnostics ();
16142 c_parser_consume_token (parser);
16143 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16144 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16145 if (c_parser_next_tokens_start_declaration (parser))
16147 c_parser_declaration_or_fndef (parser, true, true, true, true,
16148 true, NULL, clauses);
16149 restore_extension_diagnostics (ext);
16150 break;
16152 restore_extension_diagnostics (ext);
16154 else if (c_parser_next_tokens_start_declaration (parser))
16156 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16157 NULL, clauses);
16158 break;
16160 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16161 "function declaration or definition");
16162 break;
16163 default:
16164 gcc_unreachable ();
16166 clauses.release ();
16169 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16170 and put that into "omp declare simd" attribute. */
16172 static void
16173 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16174 vec<c_token> clauses)
16176 if (flag_cilkplus
16177 && (clauses.exists ()
16178 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16179 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16181 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16182 "used in the same function marked as a Cilk Plus SIMD-enabled "
16183 "function");
16184 vec_free (parser->cilk_simd_fn_tokens);
16185 return;
16188 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16189 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16190 has already processed the tokens. */
16191 if (clauses.exists () && clauses[0].type == CPP_EOF)
16192 return;
16193 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16195 error ("%<#pragma omp declare simd%> not immediately followed by "
16196 "a function declaration or definition");
16197 clauses[0].type = CPP_EOF;
16198 return;
16200 if (clauses.exists () && clauses[0].type != CPP_NAME)
16202 error_at (DECL_SOURCE_LOCATION (fndecl),
16203 "%<#pragma omp declare simd%> not immediately followed by "
16204 "a single function declaration or definition");
16205 clauses[0].type = CPP_EOF;
16206 return;
16209 if (parms == NULL_TREE)
16210 parms = DECL_ARGUMENTS (fndecl);
16212 unsigned int tokens_avail = parser->tokens_avail;
16213 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16214 bool is_cilkplus_cilk_simd_fn = false;
16216 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16218 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16219 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16220 is_cilkplus_cilk_simd_fn = true;
16222 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16224 error_at (DECL_SOURCE_LOCATION (fndecl),
16225 "%<__simd__%> attribute cannot be used in the same "
16226 "function marked as a Cilk Plus SIMD-enabled function");
16227 vec_free (parser->cilk_simd_fn_tokens);
16228 return;
16232 else
16234 parser->tokens = clauses.address ();
16235 parser->tokens_avail = clauses.length ();
16238 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16239 while (parser->tokens_avail > 3)
16241 c_token *token = c_parser_peek_token (parser);
16242 if (!is_cilkplus_cilk_simd_fn)
16243 gcc_assert (token->type == CPP_NAME
16244 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16245 else
16246 gcc_assert (token->type == CPP_NAME
16247 && is_cilkplus_vector_p (token->value));
16248 c_parser_consume_token (parser);
16249 parser->in_pragma = true;
16251 tree c = NULL_TREE;
16252 if (is_cilkplus_cilk_simd_fn)
16253 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16254 "SIMD-enabled functions attribute");
16255 else
16256 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16257 "#pragma omp declare simd");
16258 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16259 if (c != NULL_TREE)
16260 c = tree_cons (NULL_TREE, c, NULL_TREE);
16261 if (is_cilkplus_cilk_simd_fn)
16263 tree k = build_tree_list (get_identifier ("cilk simd function"),
16264 NULL_TREE);
16265 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16266 DECL_ATTRIBUTES (fndecl) = k;
16268 c = build_tree_list (get_identifier ("omp declare simd"), c);
16269 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16270 DECL_ATTRIBUTES (fndecl) = c;
16273 parser->tokens = &parser->tokens_buf[0];
16274 parser->tokens_avail = tokens_avail;
16275 if (clauses.exists ())
16276 clauses[0].type = CPP_PRAGMA;
16278 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16279 vec_free (parser->cilk_simd_fn_tokens);
16283 /* OpenMP 4.0:
16284 # pragma omp declare target new-line
16285 declarations and definitions
16286 # pragma omp end declare target new-line
16288 OpenMP 4.5:
16289 # pragma omp declare target ( extended-list ) new-line
16291 # pragma omp declare target declare-target-clauses[seq] new-line */
16293 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16297 static void
16298 c_parser_omp_declare_target (c_parser *parser)
16300 location_t loc = c_parser_peek_token (parser)->location;
16301 tree clauses = NULL_TREE;
16302 if (c_parser_next_token_is (parser, CPP_NAME))
16303 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16304 "#pragma omp declare target");
16305 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16307 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16308 clauses);
16309 clauses = c_finish_omp_clauses (clauses, true);
16310 c_parser_skip_to_pragma_eol (parser);
16312 else
16314 c_parser_skip_to_pragma_eol (parser);
16315 current_omp_declare_target_attribute++;
16316 return;
16318 if (current_omp_declare_target_attribute)
16319 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16320 "%<#pragma omp declare target%> without clauses and "
16321 "%<#pragma omp end declare target%>");
16322 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16324 tree t = OMP_CLAUSE_DECL (c), id;
16325 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16326 tree at2 = lookup_attribute ("omp declare target link",
16327 DECL_ATTRIBUTES (t));
16328 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16330 id = get_identifier ("omp declare target link");
16331 std::swap (at1, at2);
16333 else
16334 id = get_identifier ("omp declare target");
16335 if (at2)
16337 error_at (OMP_CLAUSE_LOCATION (c),
16338 "%qD specified both in declare target %<link%> and %<to%>"
16339 " clauses", t);
16340 continue;
16342 if (!at1)
16344 symtab_node *node = symtab_node::get (t);
16345 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16346 if (node != NULL)
16348 node->offloadable = 1;
16349 if (ENABLE_OFFLOADING)
16351 g->have_offload = true;
16352 if (is_a <varpool_node *> (node))
16354 vec_safe_push (offload_vars, t);
16355 node->force_output = 1;
16363 static void
16364 c_parser_omp_end_declare_target (c_parser *parser)
16366 location_t loc = c_parser_peek_token (parser)->location;
16367 c_parser_consume_pragma (parser);
16368 if (c_parser_next_token_is (parser, CPP_NAME)
16369 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16370 "declare") == 0)
16372 c_parser_consume_token (parser);
16373 if (c_parser_next_token_is (parser, CPP_NAME)
16374 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16375 "target") == 0)
16376 c_parser_consume_token (parser);
16377 else
16379 c_parser_error (parser, "expected %<target%>");
16380 c_parser_skip_to_pragma_eol (parser);
16381 return;
16384 else
16386 c_parser_error (parser, "expected %<declare%>");
16387 c_parser_skip_to_pragma_eol (parser);
16388 return;
16390 c_parser_skip_to_pragma_eol (parser);
16391 if (!current_omp_declare_target_attribute)
16392 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16393 "%<#pragma omp declare target%>");
16394 else
16395 current_omp_declare_target_attribute--;
16399 /* OpenMP 4.0
16400 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16401 initializer-clause[opt] new-line
16403 initializer-clause:
16404 initializer (omp_priv = initializer)
16405 initializer (function-name (argument-list)) */
16407 static void
16408 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16410 unsigned int tokens_avail = 0, i;
16411 vec<tree> types = vNULL;
16412 vec<c_token> clauses = vNULL;
16413 enum tree_code reduc_code = ERROR_MARK;
16414 tree reduc_id = NULL_TREE;
16415 tree type;
16416 location_t rloc = c_parser_peek_token (parser)->location;
16418 if (context == pragma_struct || context == pragma_param)
16420 error ("%<#pragma omp declare reduction%> not at file or block scope");
16421 goto fail;
16424 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16425 goto fail;
16427 switch (c_parser_peek_token (parser)->type)
16429 case CPP_PLUS:
16430 reduc_code = PLUS_EXPR;
16431 break;
16432 case CPP_MULT:
16433 reduc_code = MULT_EXPR;
16434 break;
16435 case CPP_MINUS:
16436 reduc_code = MINUS_EXPR;
16437 break;
16438 case CPP_AND:
16439 reduc_code = BIT_AND_EXPR;
16440 break;
16441 case CPP_XOR:
16442 reduc_code = BIT_XOR_EXPR;
16443 break;
16444 case CPP_OR:
16445 reduc_code = BIT_IOR_EXPR;
16446 break;
16447 case CPP_AND_AND:
16448 reduc_code = TRUTH_ANDIF_EXPR;
16449 break;
16450 case CPP_OR_OR:
16451 reduc_code = TRUTH_ORIF_EXPR;
16452 break;
16453 case CPP_NAME:
16454 const char *p;
16455 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16456 if (strcmp (p, "min") == 0)
16458 reduc_code = MIN_EXPR;
16459 break;
16461 if (strcmp (p, "max") == 0)
16463 reduc_code = MAX_EXPR;
16464 break;
16466 reduc_id = c_parser_peek_token (parser)->value;
16467 break;
16468 default:
16469 c_parser_error (parser,
16470 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16471 "%<^%>, %<|%>, %<&&%>, %<||%>, %<min%> or identifier");
16472 goto fail;
16475 tree orig_reduc_id, reduc_decl;
16476 orig_reduc_id = reduc_id;
16477 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16478 reduc_decl = c_omp_reduction_decl (reduc_id);
16479 c_parser_consume_token (parser);
16481 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16482 goto fail;
16484 while (true)
16486 location_t loc = c_parser_peek_token (parser)->location;
16487 struct c_type_name *ctype = c_parser_type_name (parser);
16488 if (ctype != NULL)
16490 type = groktypename (ctype, NULL, NULL);
16491 if (type == error_mark_node)
16493 else if ((INTEGRAL_TYPE_P (type)
16494 || TREE_CODE (type) == REAL_TYPE
16495 || TREE_CODE (type) == COMPLEX_TYPE)
16496 && orig_reduc_id == NULL_TREE)
16497 error_at (loc, "predeclared arithmetic type in "
16498 "%<#pragma omp declare reduction%>");
16499 else if (TREE_CODE (type) == FUNCTION_TYPE
16500 || TREE_CODE (type) == ARRAY_TYPE)
16501 error_at (loc, "function or array type in "
16502 "%<#pragma omp declare reduction%>");
16503 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
16504 error_at (loc, "const, volatile or restrict qualified type in "
16505 "%<#pragma omp declare reduction%>");
16506 else
16508 tree t;
16509 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
16510 if (comptypes (TREE_PURPOSE (t), type))
16512 error_at (loc, "redeclaration of %qs "
16513 "%<#pragma omp declare reduction%> for "
16514 "type %qT",
16515 IDENTIFIER_POINTER (reduc_id)
16516 + sizeof ("omp declare reduction ") - 1,
16517 type);
16518 location_t ploc
16519 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
16520 0));
16521 error_at (ploc, "previous %<#pragma omp declare "
16522 "reduction%>");
16523 break;
16525 if (t == NULL_TREE)
16526 types.safe_push (type);
16528 if (c_parser_next_token_is (parser, CPP_COMMA))
16529 c_parser_consume_token (parser);
16530 else
16531 break;
16533 else
16534 break;
16537 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
16538 || types.is_empty ())
16540 fail:
16541 clauses.release ();
16542 types.release ();
16543 while (true)
16545 c_token *token = c_parser_peek_token (parser);
16546 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
16547 break;
16548 c_parser_consume_token (parser);
16550 c_parser_skip_to_pragma_eol (parser);
16551 return;
16554 if (types.length () > 1)
16556 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16558 c_token *token = c_parser_peek_token (parser);
16559 if (token->type == CPP_EOF)
16560 goto fail;
16561 clauses.safe_push (*token);
16562 c_parser_consume_token (parser);
16564 clauses.safe_push (*c_parser_peek_token (parser));
16565 c_parser_skip_to_pragma_eol (parser);
16567 /* Make sure nothing tries to read past the end of the tokens. */
16568 c_token eof_token;
16569 memset (&eof_token, 0, sizeof (eof_token));
16570 eof_token.type = CPP_EOF;
16571 clauses.safe_push (eof_token);
16572 clauses.safe_push (eof_token);
16575 int errs = errorcount;
16576 FOR_EACH_VEC_ELT (types, i, type)
16578 tokens_avail = parser->tokens_avail;
16579 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16580 if (!clauses.is_empty ())
16582 parser->tokens = clauses.address ();
16583 parser->tokens_avail = clauses.length ();
16584 parser->in_pragma = true;
16587 bool nested = current_function_decl != NULL_TREE;
16588 if (nested)
16589 c_push_function_context ();
16590 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
16591 reduc_id, default_function_type);
16592 current_function_decl = fndecl;
16593 allocate_struct_function (fndecl, true);
16594 push_scope ();
16595 tree stmt = push_stmt_list ();
16596 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
16597 warn about these. */
16598 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
16599 get_identifier ("omp_out"), type);
16600 DECL_ARTIFICIAL (omp_out) = 1;
16601 DECL_CONTEXT (omp_out) = fndecl;
16602 pushdecl (omp_out);
16603 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
16604 get_identifier ("omp_in"), type);
16605 DECL_ARTIFICIAL (omp_in) = 1;
16606 DECL_CONTEXT (omp_in) = fndecl;
16607 pushdecl (omp_in);
16608 struct c_expr combiner = c_parser_expression (parser);
16609 struct c_expr initializer;
16610 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
16611 bool bad = false;
16612 initializer.value = error_mark_node;
16613 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16614 bad = true;
16615 else if (c_parser_next_token_is (parser, CPP_NAME)
16616 && strcmp (IDENTIFIER_POINTER
16617 (c_parser_peek_token (parser)->value),
16618 "initializer") == 0)
16620 c_parser_consume_token (parser);
16621 pop_scope ();
16622 push_scope ();
16623 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
16624 get_identifier ("omp_priv"), type);
16625 DECL_ARTIFICIAL (omp_priv) = 1;
16626 DECL_INITIAL (omp_priv) = error_mark_node;
16627 DECL_CONTEXT (omp_priv) = fndecl;
16628 pushdecl (omp_priv);
16629 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
16630 get_identifier ("omp_orig"), type);
16631 DECL_ARTIFICIAL (omp_orig) = 1;
16632 DECL_CONTEXT (omp_orig) = fndecl;
16633 pushdecl (omp_orig);
16634 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16635 bad = true;
16636 else if (!c_parser_next_token_is (parser, CPP_NAME))
16638 c_parser_error (parser, "expected %<omp_priv%> or "
16639 "function-name");
16640 bad = true;
16642 else if (strcmp (IDENTIFIER_POINTER
16643 (c_parser_peek_token (parser)->value),
16644 "omp_priv") != 0)
16646 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
16647 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
16649 c_parser_error (parser, "expected function-name %<(%>");
16650 bad = true;
16652 else
16653 initializer = c_parser_postfix_expression (parser);
16654 if (initializer.value
16655 && TREE_CODE (initializer.value) == CALL_EXPR)
16657 int j;
16658 tree c = initializer.value;
16659 for (j = 0; j < call_expr_nargs (c); j++)
16661 tree a = CALL_EXPR_ARG (c, j);
16662 STRIP_NOPS (a);
16663 if (TREE_CODE (a) == ADDR_EXPR
16664 && TREE_OPERAND (a, 0) == omp_priv)
16665 break;
16667 if (j == call_expr_nargs (c))
16668 error ("one of the initializer call arguments should be "
16669 "%<&omp_priv%>");
16672 else
16674 c_parser_consume_token (parser);
16675 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
16676 bad = true;
16677 else
16679 tree st = push_stmt_list ();
16680 start_init (omp_priv, NULL_TREE, 0);
16681 location_t loc = c_parser_peek_token (parser)->location;
16682 struct c_expr init = c_parser_initializer (parser);
16683 finish_init ();
16684 finish_decl (omp_priv, loc, init.value,
16685 init.original_type, NULL_TREE);
16686 pop_stmt_list (st);
16689 if (!bad
16690 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
16691 bad = true;
16694 if (!bad)
16696 c_parser_skip_to_pragma_eol (parser);
16698 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
16699 DECL_INITIAL (reduc_decl));
16700 DECL_INITIAL (reduc_decl) = t;
16701 DECL_SOURCE_LOCATION (omp_out) = rloc;
16702 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
16703 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
16704 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
16705 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
16706 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
16707 if (omp_priv)
16709 DECL_SOURCE_LOCATION (omp_priv) = rloc;
16710 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
16711 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
16712 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
16713 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
16714 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16715 walk_tree (&DECL_INITIAL (omp_priv),
16716 c_check_omp_declare_reduction_r,
16717 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
16721 pop_stmt_list (stmt);
16722 pop_scope ();
16723 if (cfun->language != NULL)
16725 ggc_free (cfun->language);
16726 cfun->language = NULL;
16728 set_cfun (NULL);
16729 current_function_decl = NULL_TREE;
16730 if (nested)
16731 c_pop_function_context ();
16733 if (!clauses.is_empty ())
16735 parser->tokens = &parser->tokens_buf[0];
16736 parser->tokens_avail = tokens_avail;
16738 if (bad)
16739 goto fail;
16740 if (errs != errorcount)
16741 break;
16744 clauses.release ();
16745 types.release ();
16749 /* OpenMP 4.0
16750 #pragma omp declare simd declare-simd-clauses[optseq] new-line
16751 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16752 initializer-clause[opt] new-line
16753 #pragma omp declare target new-line */
16755 static void
16756 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
16758 c_parser_consume_pragma (parser);
16759 if (c_parser_next_token_is (parser, CPP_NAME))
16761 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16762 if (strcmp (p, "simd") == 0)
16764 /* c_parser_consume_token (parser); done in
16765 c_parser_omp_declare_simd. */
16766 c_parser_omp_declare_simd (parser, context);
16767 return;
16769 if (strcmp (p, "reduction") == 0)
16771 c_parser_consume_token (parser);
16772 c_parser_omp_declare_reduction (parser, context);
16773 return;
16775 if (!flag_openmp) /* flag_openmp_simd */
16777 c_parser_skip_to_pragma_eol (parser, false);
16778 return;
16780 if (strcmp (p, "target") == 0)
16782 c_parser_consume_token (parser);
16783 c_parser_omp_declare_target (parser);
16784 return;
16788 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
16789 "or %<target%>");
16790 c_parser_skip_to_pragma_eol (parser);
16793 /* OpenMP 4.5:
16794 #pragma omp taskloop taskloop-clause[optseq] new-line
16795 for-loop
16797 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
16798 for-loop */
16800 #define OMP_TASKLOOP_CLAUSE_MASK \
16801 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
16807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
16808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
16814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16816 static tree
16817 c_parser_omp_taskloop (location_t loc, c_parser *parser,
16818 char *p_name, omp_clause_mask mask, tree *cclauses)
16820 tree clauses, block, ret;
16822 strcat (p_name, " taskloop");
16823 mask |= OMP_TASKLOOP_CLAUSE_MASK;
16825 if (c_parser_next_token_is (parser, CPP_NAME))
16827 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16829 if (strcmp (p, "simd") == 0)
16831 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16832 if (cclauses == NULL)
16833 cclauses = cclauses_buf;
16834 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
16835 c_parser_consume_token (parser);
16836 if (!flag_openmp) /* flag_openmp_simd */
16837 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
16838 block = c_begin_compound_stmt (true);
16839 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses);
16840 block = c_end_compound_stmt (loc, block, true);
16841 if (ret == NULL)
16842 return ret;
16843 ret = make_node (OMP_TASKLOOP);
16844 TREE_TYPE (ret) = void_type_node;
16845 OMP_FOR_BODY (ret) = block;
16846 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
16847 SET_EXPR_LOCATION (ret, loc);
16848 add_stmt (ret);
16849 return ret;
16852 if (!flag_openmp) /* flag_openmp_simd */
16854 c_parser_skip_to_pragma_eol (parser, false);
16855 return NULL_TREE;
16858 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16859 if (cclauses)
16861 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
16862 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
16865 block = c_begin_compound_stmt (true);
16866 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL);
16867 block = c_end_compound_stmt (loc, block, true);
16868 add_stmt (block);
16870 return ret;
16873 /* Main entry point to parsing most OpenMP pragmas. */
16875 static void
16876 c_parser_omp_construct (c_parser *parser)
16878 enum pragma_kind p_kind;
16879 location_t loc;
16880 tree stmt;
16881 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
16882 omp_clause_mask mask (0);
16884 loc = c_parser_peek_token (parser)->location;
16885 p_kind = c_parser_peek_token (parser)->pragma_kind;
16886 c_parser_consume_pragma (parser);
16888 switch (p_kind)
16890 case PRAGMA_OACC_ATOMIC:
16891 c_parser_omp_atomic (loc, parser);
16892 return;
16893 case PRAGMA_OACC_CACHE:
16894 strcpy (p_name, "#pragma acc");
16895 stmt = c_parser_oacc_cache (loc, parser);
16896 break;
16897 case PRAGMA_OACC_DATA:
16898 stmt = c_parser_oacc_data (loc, parser);
16899 break;
16900 case PRAGMA_OACC_KERNELS:
16901 case PRAGMA_OACC_PARALLEL:
16902 strcpy (p_name, "#pragma acc");
16903 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name);
16904 break;
16905 case PRAGMA_OACC_LOOP:
16906 strcpy (p_name, "#pragma acc");
16907 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL);
16908 break;
16909 case PRAGMA_OACC_WAIT:
16910 strcpy (p_name, "#pragma wait");
16911 stmt = c_parser_oacc_wait (loc, parser, p_name);
16912 break;
16913 case PRAGMA_OMP_ATOMIC:
16914 c_parser_omp_atomic (loc, parser);
16915 return;
16916 case PRAGMA_OMP_CRITICAL:
16917 stmt = c_parser_omp_critical (loc, parser);
16918 break;
16919 case PRAGMA_OMP_DISTRIBUTE:
16920 strcpy (p_name, "#pragma omp");
16921 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL);
16922 break;
16923 case PRAGMA_OMP_FOR:
16924 strcpy (p_name, "#pragma omp");
16925 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL);
16926 break;
16927 case PRAGMA_OMP_MASTER:
16928 stmt = c_parser_omp_master (loc, parser);
16929 break;
16930 case PRAGMA_OMP_PARALLEL:
16931 strcpy (p_name, "#pragma omp");
16932 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL);
16933 break;
16934 case PRAGMA_OMP_SECTIONS:
16935 strcpy (p_name, "#pragma omp");
16936 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
16937 break;
16938 case PRAGMA_OMP_SIMD:
16939 strcpy (p_name, "#pragma omp");
16940 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL);
16941 break;
16942 case PRAGMA_OMP_SINGLE:
16943 stmt = c_parser_omp_single (loc, parser);
16944 break;
16945 case PRAGMA_OMP_TASK:
16946 stmt = c_parser_omp_task (loc, parser);
16947 break;
16948 case PRAGMA_OMP_TASKGROUP:
16949 stmt = c_parser_omp_taskgroup (parser);
16950 break;
16951 case PRAGMA_OMP_TASKLOOP:
16952 strcpy (p_name, "#pragma omp");
16953 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL);
16954 break;
16955 case PRAGMA_OMP_TEAMS:
16956 strcpy (p_name, "#pragma omp");
16957 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL);
16958 break;
16959 default:
16960 gcc_unreachable ();
16963 if (stmt)
16964 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
16968 /* OpenMP 2.5:
16969 # pragma omp threadprivate (variable-list) */
16971 static void
16972 c_parser_omp_threadprivate (c_parser *parser)
16974 tree vars, t;
16975 location_t loc;
16977 c_parser_consume_pragma (parser);
16978 loc = c_parser_peek_token (parser)->location;
16979 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
16981 /* Mark every variable in VARS to be assigned thread local storage. */
16982 for (t = vars; t; t = TREE_CHAIN (t))
16984 tree v = TREE_PURPOSE (t);
16986 /* FIXME diagnostics: Ideally we should keep individual
16987 locations for all the variables in the var list to make the
16988 following errors more precise. Perhaps
16989 c_parser_omp_var_list_parens() should construct a list of
16990 locations to go along with the var list. */
16992 /* If V had already been marked threadprivate, it doesn't matter
16993 whether it had been used prior to this point. */
16994 if (!VAR_P (v))
16995 error_at (loc, "%qD is not a variable", v);
16996 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
16997 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
16998 else if (! is_global_var (v))
16999 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17000 else if (TREE_TYPE (v) == error_mark_node)
17002 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17003 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17004 else
17006 if (! DECL_THREAD_LOCAL_P (v))
17008 set_decl_tls_model (v, decl_default_tls_model (v));
17009 /* If rtl has been already set for this var, call
17010 make_decl_rtl once again, so that encode_section_info
17011 has a chance to look at the new decl flags. */
17012 if (DECL_RTL_SET_P (v))
17013 make_decl_rtl (v);
17015 C_DECL_THREADPRIVATE_P (v) = 1;
17019 c_parser_skip_to_pragma_eol (parser);
17022 /* Cilk Plus <#pragma simd> parsing routines. */
17024 /* Helper function for c_parser_pragma. Perform some sanity checking
17025 for <#pragma simd> constructs. Returns FALSE if there was a
17026 problem. */
17028 static bool
17029 c_parser_cilk_verify_simd (c_parser *parser,
17030 enum pragma_context context)
17032 if (!flag_cilkplus)
17034 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17035 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17036 return false;
17038 if (context == pragma_external)
17040 c_parser_error (parser,"pragma simd must be inside a function");
17041 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17042 return false;
17044 return true;
17047 /* Cilk Plus:
17048 This function is shared by SIMD-enabled functions and #pragma simd.
17049 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17050 CLAUSES is unused. The main purpose of this function is to parse a
17051 vectorlength attribute or clause and check for parse errors.
17052 When IS_SIMD_FN is true then the function is merely caching the tokens
17053 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17054 cache is cleared since there is no reason to continue.
17055 Syntax:
17056 vectorlength ( constant-expression ) */
17058 static tree
17059 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17060 bool is_simd_fn)
17062 if (is_simd_fn)
17063 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17064 else
17065 /* The vectorlength clause behaves exactly like OpenMP's safelen
17066 clause. Represent it in OpenMP terms. */
17067 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17069 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17070 return clauses;
17072 location_t loc = c_parser_peek_token (parser)->location;
17073 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17074 expr = c_fully_fold (expr, false, NULL);
17076 /* If expr is an error_mark_node then the above function would have
17077 emitted an error. No reason to do it twice. */
17078 if (expr == error_mark_node)
17080 else if (!TREE_TYPE (expr)
17081 || !TREE_CONSTANT (expr)
17082 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17084 error_at (loc, "vectorlength must be an integer constant");
17085 else if (wi::exact_log2 (expr) == -1)
17086 error_at (loc, "vectorlength must be a power of 2");
17087 else
17089 if (is_simd_fn)
17091 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17092 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17093 OMP_CLAUSE_CHAIN (u) = clauses;
17094 clauses = u;
17096 else
17098 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17099 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17100 OMP_CLAUSE_CHAIN (u) = clauses;
17101 clauses = u;
17105 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17107 return clauses;
17110 /* Cilk Plus:
17111 linear ( simd-linear-variable-list )
17113 simd-linear-variable-list:
17114 simd-linear-variable
17115 simd-linear-variable-list , simd-linear-variable
17117 simd-linear-variable:
17118 id-expression
17119 id-expression : simd-linear-step
17121 simd-linear-step:
17122 conditional-expression */
17124 static tree
17125 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17127 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17128 return clauses;
17130 location_t loc = c_parser_peek_token (parser)->location;
17132 if (c_parser_next_token_is_not (parser, CPP_NAME)
17133 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17134 c_parser_error (parser, "expected identifier");
17136 while (c_parser_next_token_is (parser, CPP_NAME)
17137 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17139 tree var = lookup_name (c_parser_peek_token (parser)->value);
17141 if (var == NULL)
17143 undeclared_variable (c_parser_peek_token (parser)->location,
17144 c_parser_peek_token (parser)->value);
17145 c_parser_consume_token (parser);
17147 else if (var == error_mark_node)
17148 c_parser_consume_token (parser);
17149 else
17151 tree step = integer_one_node;
17153 /* Parse the linear step if present. */
17154 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17156 c_parser_consume_token (parser);
17157 c_parser_consume_token (parser);
17159 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17160 expr = c_fully_fold (expr, false, NULL);
17162 if (TREE_TYPE (expr)
17163 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17164 && (TREE_CONSTANT (expr)
17165 || DECL_P (expr)))
17166 step = expr;
17167 else
17168 c_parser_error (parser,
17169 "step size must be an integer constant "
17170 "expression or an integer variable");
17172 else
17173 c_parser_consume_token (parser);
17175 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17176 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17177 OMP_CLAUSE_DECL (u) = var;
17178 OMP_CLAUSE_LINEAR_STEP (u) = step;
17179 OMP_CLAUSE_CHAIN (u) = clauses;
17180 clauses = u;
17183 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17184 break;
17186 c_parser_consume_token (parser);
17189 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17191 return clauses;
17194 /* Returns the name of the next clause. If the clause is not
17195 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17196 not consumed. Otherwise, the appropriate pragma_simd_clause is
17197 returned and the token is consumed. */
17199 static pragma_omp_clause
17200 c_parser_cilk_clause_name (c_parser *parser)
17202 pragma_omp_clause result;
17203 c_token *token = c_parser_peek_token (parser);
17205 if (!token->value || token->type != CPP_NAME)
17206 return PRAGMA_CILK_CLAUSE_NONE;
17208 const char *p = IDENTIFIER_POINTER (token->value);
17210 if (!strcmp (p, "vectorlength"))
17211 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17212 else if (!strcmp (p, "linear"))
17213 result = PRAGMA_CILK_CLAUSE_LINEAR;
17214 else if (!strcmp (p, "private"))
17215 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17216 else if (!strcmp (p, "firstprivate"))
17217 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17218 else if (!strcmp (p, "lastprivate"))
17219 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17220 else if (!strcmp (p, "reduction"))
17221 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17222 else
17223 return PRAGMA_CILK_CLAUSE_NONE;
17225 c_parser_consume_token (parser);
17226 return result;
17229 /* Parse all #<pragma simd> clauses. Return the list of clauses
17230 found. */
17232 static tree
17233 c_parser_cilk_all_clauses (c_parser *parser)
17235 tree clauses = NULL;
17237 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17239 pragma_omp_clause c_kind;
17241 c_kind = c_parser_cilk_clause_name (parser);
17243 switch (c_kind)
17245 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17246 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17247 break;
17248 case PRAGMA_CILK_CLAUSE_LINEAR:
17249 clauses = c_parser_cilk_clause_linear (parser, clauses);
17250 break;
17251 case PRAGMA_CILK_CLAUSE_PRIVATE:
17252 /* Use the OpenMP counterpart. */
17253 clauses = c_parser_omp_clause_private (parser, clauses);
17254 break;
17255 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17256 /* Use the OpenMP counterpart. */
17257 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17258 break;
17259 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17260 /* Use the OpenMP counterpart. */
17261 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17262 break;
17263 case PRAGMA_CILK_CLAUSE_REDUCTION:
17264 /* Use the OpenMP counterpart. */
17265 clauses = c_parser_omp_clause_reduction (parser, clauses);
17266 break;
17267 default:
17268 c_parser_error (parser, "expected %<#pragma simd%> clause");
17269 goto saw_error;
17273 saw_error:
17274 c_parser_skip_to_pragma_eol (parser);
17275 return c_finish_cilk_clauses (clauses);
17278 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17279 Here is the correct syntax of this pragma:
17280 #pragma cilk grainsize = <EXP>
17283 static void
17284 c_parser_cilk_grainsize (c_parser *parser)
17286 extern tree convert_to_integer (tree, tree);
17288 /* consume the 'grainsize' keyword. */
17289 c_parser_consume_pragma (parser);
17291 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17293 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17294 if (g_expr.value == error_mark_node)
17296 c_parser_skip_to_pragma_eol (parser);
17297 return;
17299 tree grain = convert_to_integer (long_integer_type_node,
17300 c_fully_fold (g_expr.value, false,
17301 NULL));
17302 c_parser_skip_to_pragma_eol (parser);
17303 c_token *token = c_parser_peek_token (parser);
17304 if (token && token->type == CPP_KEYWORD
17305 && token->keyword == RID_CILK_FOR)
17307 if (grain == NULL_TREE || grain == error_mark_node)
17308 grain = integer_zero_node;
17309 c_parser_cilk_for (parser, grain);
17311 else
17312 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17313 "%<_Cilk_for%>");
17315 else
17316 c_parser_skip_to_pragma_eol (parser);
17319 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17321 static void
17322 c_parser_cilk_simd (c_parser *parser)
17324 tree clauses = c_parser_cilk_all_clauses (parser);
17325 tree block = c_begin_compound_stmt (true);
17326 location_t loc = c_parser_peek_token (parser)->location;
17327 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL);
17328 block = c_end_compound_stmt (loc, block, true);
17329 add_stmt (block);
17332 /* Create an artificial decl with TYPE and emit initialization of it with
17333 INIT. */
17335 static tree
17336 c_get_temp_regvar (tree type, tree init)
17338 location_t loc = EXPR_LOCATION (init);
17339 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17340 DECL_ARTIFICIAL (decl) = 1;
17341 DECL_IGNORED_P (decl) = 1;
17342 pushdecl (decl);
17343 tree t = build2 (INIT_EXPR, type, decl, init);
17344 add_stmt (t);
17345 return decl;
17348 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17349 GRAIN is the grain value passed in through pragma or 0. */
17351 static void
17352 c_parser_cilk_for (c_parser *parser, tree grain)
17354 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17355 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17356 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17357 clauses = c_finish_omp_clauses (clauses, false);
17359 tree block = c_begin_compound_stmt (true);
17360 tree sb = push_stmt_list ();
17361 location_t loc = c_parser_peek_token (parser)->location;
17362 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL);
17363 sb = pop_stmt_list (sb);
17365 if (omp_for)
17367 tree omp_par = make_node (OMP_PARALLEL);
17368 TREE_TYPE (omp_par) = void_type_node;
17369 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17370 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17371 TREE_SIDE_EFFECTS (bind) = 1;
17372 BIND_EXPR_BODY (bind) = sb;
17373 OMP_PARALLEL_BODY (omp_par) = bind;
17374 if (OMP_FOR_PRE_BODY (omp_for))
17376 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17377 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17379 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17380 tree decl = TREE_OPERAND (init, 0);
17381 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17382 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17383 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17384 if (TREE_CODE (t) != INTEGER_CST)
17386 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17387 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17388 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17389 OMP_CLAUSE_CHAIN (c) = clauses;
17390 clauses = c;
17392 if (TREE_CODE (incr) == MODIFY_EXPR)
17394 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17395 if (TREE_CODE (t) != INTEGER_CST)
17397 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17398 = c_get_temp_regvar (TREE_TYPE (t), t);
17399 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17400 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17401 OMP_CLAUSE_CHAIN (c) = clauses;
17402 clauses = c;
17405 t = TREE_OPERAND (init, 1);
17406 if (TREE_CODE (t) != INTEGER_CST)
17408 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17409 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17410 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17411 OMP_CLAUSE_CHAIN (c) = clauses;
17412 clauses = c;
17414 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17415 OMP_CLAUSE_DECL (c) = decl;
17416 OMP_CLAUSE_CHAIN (c) = clauses;
17417 clauses = c;
17418 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17419 OMP_CLAUSE_OPERAND (c, 0)
17420 = cilk_for_number_of_iterations (omp_for);
17421 OMP_CLAUSE_CHAIN (c) = clauses;
17422 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, true);
17423 add_stmt (omp_par);
17426 block = c_end_compound_stmt (loc, block, true);
17427 add_stmt (block);
17431 /* Parse a transaction attribute (GCC Extension).
17433 transaction-attribute:
17434 attributes
17435 [ [ any-word ] ]
17437 The transactional memory language description is written for C++,
17438 and uses the C++0x attribute syntax. For compatibility, allow the
17439 bracket style for transactions in C as well. */
17441 static tree
17442 c_parser_transaction_attributes (c_parser *parser)
17444 tree attr_name, attr = NULL;
17446 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17447 return c_parser_attributes (parser);
17449 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17450 return NULL_TREE;
17451 c_parser_consume_token (parser);
17452 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17453 goto error1;
17455 attr_name = c_parser_attribute_any_word (parser);
17456 if (attr_name)
17458 c_parser_consume_token (parser);
17459 attr = build_tree_list (attr_name, NULL_TREE);
17461 else
17462 c_parser_error (parser, "expected identifier");
17464 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17465 error1:
17466 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17467 return attr;
17470 /* Parse a __transaction_atomic or __transaction_relaxed statement
17471 (GCC Extension).
17473 transaction-statement:
17474 __transaction_atomic transaction-attribute[opt] compound-statement
17475 __transaction_relaxed compound-statement
17477 Note that the only valid attribute is: "outer".
17480 static tree
17481 c_parser_transaction (c_parser *parser, enum rid keyword)
17483 unsigned int old_in = parser->in_transaction;
17484 unsigned int this_in = 1, new_in;
17485 location_t loc = c_parser_peek_token (parser)->location;
17486 tree stmt, attrs;
17488 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17489 || keyword == RID_TRANSACTION_RELAXED)
17490 && c_parser_next_token_is_keyword (parser, keyword));
17491 c_parser_consume_token (parser);
17493 if (keyword == RID_TRANSACTION_RELAXED)
17494 this_in |= TM_STMT_ATTR_RELAXED;
17495 else
17497 attrs = c_parser_transaction_attributes (parser);
17498 if (attrs)
17499 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
17502 /* Keep track if we're in the lexical scope of an outer transaction. */
17503 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
17505 parser->in_transaction = new_in;
17506 stmt = c_parser_compound_statement (parser);
17507 parser->in_transaction = old_in;
17509 if (flag_tm)
17510 stmt = c_finish_transaction (loc, stmt, this_in);
17511 else
17512 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17513 "%<__transaction_atomic%> without transactional memory support enabled"
17514 : "%<__transaction_relaxed %> "
17515 "without transactional memory support enabled"));
17517 return stmt;
17520 /* Parse a __transaction_atomic or __transaction_relaxed expression
17521 (GCC Extension).
17523 transaction-expression:
17524 __transaction_atomic ( expression )
17525 __transaction_relaxed ( expression )
17528 static struct c_expr
17529 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
17531 struct c_expr ret;
17532 unsigned int old_in = parser->in_transaction;
17533 unsigned int this_in = 1;
17534 location_t loc = c_parser_peek_token (parser)->location;
17535 tree attrs;
17537 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17538 || keyword == RID_TRANSACTION_RELAXED)
17539 && c_parser_next_token_is_keyword (parser, keyword));
17540 c_parser_consume_token (parser);
17542 if (keyword == RID_TRANSACTION_RELAXED)
17543 this_in |= TM_STMT_ATTR_RELAXED;
17544 else
17546 attrs = c_parser_transaction_attributes (parser);
17547 if (attrs)
17548 this_in |= parse_tm_stmt_attr (attrs, 0);
17551 parser->in_transaction = this_in;
17552 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17554 tree expr = c_parser_expression (parser).value;
17555 ret.original_type = TREE_TYPE (expr);
17556 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
17557 if (this_in & TM_STMT_ATTR_RELAXED)
17558 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
17559 SET_EXPR_LOCATION (ret.value, loc);
17560 ret.original_code = TRANSACTION_EXPR;
17561 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17563 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
17564 goto error;
17567 else
17569 error:
17570 ret.value = error_mark_node;
17571 ret.original_code = ERROR_MARK;
17572 ret.original_type = NULL;
17574 parser->in_transaction = old_in;
17576 if (!flag_tm)
17577 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
17578 "%<__transaction_atomic%> without transactional memory support enabled"
17579 : "%<__transaction_relaxed %> "
17580 "without transactional memory support enabled"));
17582 set_c_expr_source_range (&ret, loc, loc);
17584 return ret;
17587 /* Parse a __transaction_cancel statement (GCC Extension).
17589 transaction-cancel-statement:
17590 __transaction_cancel transaction-attribute[opt] ;
17592 Note that the only valid attribute is "outer".
17595 static tree
17596 c_parser_transaction_cancel (c_parser *parser)
17598 location_t loc = c_parser_peek_token (parser)->location;
17599 tree attrs;
17600 bool is_outer = false;
17602 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
17603 c_parser_consume_token (parser);
17605 attrs = c_parser_transaction_attributes (parser);
17606 if (attrs)
17607 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
17609 if (!flag_tm)
17611 error_at (loc, "%<__transaction_cancel%> without "
17612 "transactional memory support enabled");
17613 goto ret_error;
17615 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
17617 error_at (loc, "%<__transaction_cancel%> within a "
17618 "%<__transaction_relaxed%>");
17619 goto ret_error;
17621 else if (is_outer)
17623 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
17624 && !is_tm_may_cancel_outer (current_function_decl))
17626 error_at (loc, "outer %<__transaction_cancel%> not "
17627 "within outer %<__transaction_atomic%>");
17628 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
17629 goto ret_error;
17632 else if (parser->in_transaction == 0)
17634 error_at (loc, "%<__transaction_cancel%> not within "
17635 "%<__transaction_atomic%>");
17636 goto ret_error;
17639 return add_stmt (build_tm_abort_call (loc, is_outer));
17641 ret_error:
17642 return build1 (NOP_EXPR, void_type_node, error_mark_node);
17645 /* Parse a single source file. */
17647 void
17648 c_parse_file (void)
17650 /* Use local storage to begin. If the first token is a pragma, parse it.
17651 If it is #pragma GCC pch_preprocess, then this will load a PCH file
17652 which will cause garbage collection. */
17653 c_parser tparser;
17655 memset (&tparser, 0, sizeof tparser);
17656 tparser.tokens = &tparser.tokens_buf[0];
17657 the_parser = &tparser;
17659 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
17660 c_parser_pragma_pch_preprocess (&tparser);
17662 the_parser = ggc_alloc<c_parser> ();
17663 *the_parser = tparser;
17664 if (tparser.tokens == &tparser.tokens_buf[0])
17665 the_parser->tokens = &the_parser->tokens_buf[0];
17667 /* Initialize EH, if we've been told to do so. */
17668 if (flag_exceptions)
17669 using_eh_for_cleanups ();
17671 c_parser_translation_unit (the_parser);
17672 the_parser = NULL;
17675 /* This function parses Cilk Plus array notation. The starting index is
17676 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
17677 return value of this function is a tree_node called VALUE_TREE of type
17678 ARRAY_NOTATION_REF. */
17680 static tree
17681 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
17682 tree array_value)
17684 c_token *token = NULL;
17685 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
17686 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
17687 tree array_type_domain = NULL_TREE;
17689 if (array_value == error_mark_node || initial_index == error_mark_node)
17691 /* No need to continue. If either of these 2 were true, then an error
17692 must be emitted already. Thus, no need to emit them twice. */
17693 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17694 return error_mark_node;
17697 array_type = TREE_TYPE (array_value);
17698 gcc_assert (array_type);
17699 if (TREE_CODE (array_type) != ARRAY_TYPE
17700 && TREE_CODE (array_type) != POINTER_TYPE)
17702 error_at (loc, "base of array section must be pointer or array type");
17703 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17704 return error_mark_node;
17706 type = TREE_TYPE (array_type);
17707 token = c_parser_peek_token (parser);
17709 if (token->type == CPP_EOF)
17711 c_parser_error (parser, "expected %<:%> or numeral");
17712 return value_tree;
17714 else if (token->type == CPP_COLON)
17716 if (!initial_index)
17718 /* If we are here, then we have a case like this A[:]. */
17719 c_parser_consume_token (parser);
17720 if (TREE_CODE (array_type) == POINTER_TYPE)
17722 error_at (loc, "start-index and length fields necessary for "
17723 "using array notations in pointers");
17724 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17725 return error_mark_node;
17727 if (TREE_CODE (array_type) == FUNCTION_TYPE)
17729 error_at (loc, "array notations cannot be used with function "
17730 "type");
17731 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17732 return error_mark_node;
17734 array_type_domain = TYPE_DOMAIN (array_type);
17736 if (!array_type_domain)
17738 error_at (loc, "start-index and length fields necessary for "
17739 "using array notations in dimensionless arrays");
17740 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17741 return error_mark_node;
17744 start_index = TYPE_MINVAL (array_type_domain);
17745 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
17746 start_index);
17747 if (!TYPE_MAXVAL (array_type_domain)
17748 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
17750 error_at (loc, "start-index and length fields necessary for "
17751 "using array notations in variable-length arrays");
17752 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17753 return error_mark_node;
17755 end_index = TYPE_MAXVAL (array_type_domain);
17756 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
17757 end_index, integer_one_node);
17758 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
17759 stride = build_int_cst (integer_type_node, 1);
17760 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
17762 else if (initial_index != error_mark_node)
17764 /* If we are here, then there should be 2 possibilities:
17765 1. Array [EXPR : EXPR]
17766 2. Array [EXPR : EXPR : EXPR]
17768 start_index = initial_index;
17770 if (TREE_CODE (array_type) == FUNCTION_TYPE)
17772 error_at (loc, "array notations cannot be used with function "
17773 "type");
17774 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
17775 return error_mark_node;
17777 c_parser_consume_token (parser); /* consume the ':' */
17778 struct c_expr ce = c_parser_expression (parser);
17779 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
17780 end_index = ce.value;
17781 if (!end_index || end_index == error_mark_node)
17783 c_parser_skip_to_end_of_block_or_statement (parser);
17784 return error_mark_node;
17786 if (c_parser_peek_token (parser)->type == CPP_COLON)
17788 c_parser_consume_token (parser);
17789 ce = c_parser_expression (parser);
17790 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
17791 stride = ce.value;
17792 if (!stride || stride == error_mark_node)
17794 c_parser_skip_to_end_of_block_or_statement (parser);
17795 return error_mark_node;
17799 else
17800 c_parser_error (parser, "expected array notation expression");
17802 else
17803 c_parser_error (parser, "expected array notation expression");
17805 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17807 value_tree = build_array_notation_ref (loc, array_value, start_index,
17808 end_index, stride, type);
17809 if (value_tree != error_mark_node)
17810 SET_EXPR_LOCATION (value_tree, loc);
17811 return value_tree;
17814 #include "gt-c-c-parser.h"