debug/dwarf: support 64-bit DWARF in byte order check
[official-gcc.git] / gcc / c / c-parser.c
blob6b843247911e84f94c1c775a00d616379c600241
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Parser actions based on the old Bison parser; structure somewhat
5 influenced by and fragments based on the C++ parser.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO:
25 Make sure all relevant comments, and all relevant code from all
26 actions, brought over from old parser. Verify exact correspondence
27 of syntax accepted.
29 Add testcases covering every input symbol in every state in old and
30 new parsers.
32 Include full syntax for GNU C, including erroneous cases accepted
33 with error messages, in syntax productions in comments.
35 Make more diagnostics in the front end generally take an explicit
36 location rather than implicitly using input_location. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "target.h"
42 #include "function.h"
43 #include "c-tree.h"
44 #include "timevar.h"
45 #include "stringpool.h"
46 #include "cgraph.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "varasm.h"
50 #include "trans-mem.h"
51 #include "c-family/c-pragma.h"
52 #include "c-lang.h"
53 #include "c-family/c-objc.h"
54 #include "plugin.h"
55 #include "omp-general.h"
56 #include "omp-offload.h"
57 #include "builtins.h"
58 #include "gomp-constants.h"
59 #include "c-family/c-indentation.h"
60 #include "gimple-expr.h"
61 #include "context.h"
62 #include "gcc-rich-location.h"
63 #include "c-parser.h"
64 #include "gimple-parser.h"
65 #include "read-rtl-function.h"
66 #include "run-rtl-passes.h"
67 #include "intl.h"
69 /* We need to walk over decls with incomplete struct/union/enum types
70 after parsing the whole translation unit.
71 In finish_decl(), if the decl is static, has incomplete
72 struct/union/enum type, it is appeneded to incomplete_record_decls.
73 In c_parser_translation_unit(), we iterate over incomplete_record_decls
74 and report error if any of the decls are still incomplete. */
76 vec<tree> incomplete_record_decls;
78 void
79 set_c_expr_source_range (c_expr *expr,
80 location_t start, location_t finish)
82 expr->src_range.m_start = start;
83 expr->src_range.m_finish = finish;
84 if (expr->value)
85 set_source_range (expr->value, start, finish);
88 void
89 set_c_expr_source_range (c_expr *expr,
90 source_range src_range)
92 expr->src_range = src_range;
93 if (expr->value)
94 set_source_range (expr->value, src_range);
98 /* Initialization routine for this file. */
100 void
101 c_parse_init (void)
103 /* The only initialization required is of the reserved word
104 identifiers. */
105 unsigned int i;
106 tree id;
107 int mask = 0;
109 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
110 the c_token structure. */
111 gcc_assert (RID_MAX <= 255);
113 mask |= D_CXXONLY;
114 if (!flag_isoc99)
115 mask |= D_C99;
116 if (flag_no_asm)
118 mask |= D_ASM | D_EXT;
119 if (!flag_isoc99)
120 mask |= D_EXT89;
122 if (!c_dialect_objc ())
123 mask |= D_OBJC | D_CXX_OBJC;
125 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
126 for (i = 0; i < num_c_common_reswords; i++)
128 /* If a keyword is disabled, do not enter it into the table
129 and so create a canonical spelling that isn't a keyword. */
130 if (c_common_reswords[i].disable & mask)
132 if (warn_cxx_compat
133 && (c_common_reswords[i].disable & D_CXXWARN))
135 id = get_identifier (c_common_reswords[i].word);
136 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
137 C_IS_RESERVED_WORD (id) = 1;
139 continue;
142 id = get_identifier (c_common_reswords[i].word);
143 C_SET_RID_CODE (id, c_common_reswords[i].rid);
144 C_IS_RESERVED_WORD (id) = 1;
145 ridpointers [(int) c_common_reswords[i].rid] = id;
148 for (i = 0; i < NUM_INT_N_ENTS; i++)
150 /* We always create the symbols but they aren't always supported. */
151 char name[50];
152 sprintf (name, "__int%d", int_n_data[i].bitsize);
153 id = get_identifier (name);
154 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
155 C_IS_RESERVED_WORD (id) = 1;
159 /* A parser structure recording information about the state and
160 context of parsing. Includes lexer information with up to two
161 tokens of look-ahead; more are not needed for C. */
162 struct GTY(()) c_parser {
163 /* The look-ahead tokens. */
164 c_token * GTY((skip)) tokens;
165 /* Buffer for look-ahead tokens. */
166 c_token tokens_buf[4];
167 /* How many look-ahead tokens are available (0 - 4, or
168 more if parsing from pre-lexed tokens). */
169 unsigned int tokens_avail;
170 /* True if a syntax error is being recovered from; false otherwise.
171 c_parser_error sets this flag. It should clear this flag when
172 enough tokens have been consumed to recover from the error. */
173 BOOL_BITFIELD error : 1;
174 /* True if we're processing a pragma, and shouldn't automatically
175 consume CPP_PRAGMA_EOL. */
176 BOOL_BITFIELD in_pragma : 1;
177 /* True if we're parsing the outermost block of an if statement. */
178 BOOL_BITFIELD in_if_block : 1;
179 /* True if we want to lex an untranslated string. */
180 BOOL_BITFIELD lex_untranslated_string : 1;
182 /* Objective-C specific parser/lexer information. */
184 /* True if we are in a context where the Objective-C "PQ" keywords
185 are considered keywords. */
186 BOOL_BITFIELD objc_pq_context : 1;
187 /* True if we are parsing a (potential) Objective-C foreach
188 statement. This is set to true after we parsed 'for (' and while
189 we wait for 'in' or ';' to decide if it's a standard C for loop or an
190 Objective-C foreach loop. */
191 BOOL_BITFIELD objc_could_be_foreach_context : 1;
192 /* The following flag is needed to contextualize Objective-C lexical
193 analysis. In some cases (e.g., 'int NSObject;'), it is
194 undesirable to bind an identifier to an Objective-C class, even
195 if a class with that name exists. */
196 BOOL_BITFIELD objc_need_raw_identifier : 1;
197 /* Nonzero if we're processing a __transaction statement. The value
198 is 1 | TM_STMT_ATTR_*. */
199 unsigned int in_transaction : 4;
200 /* True if we are in a context where the Objective-C "Property attribute"
201 keywords are valid. */
202 BOOL_BITFIELD objc_property_attr_context : 1;
204 /* Cilk Plus specific parser/lexer information. */
206 /* Buffer to hold all the tokens from parsing the vector attribute for the
207 SIMD-enabled functions (formerly known as elemental functions). */
208 vec <c_token, va_gc> *cilk_simd_fn_tokens;
210 /* Location of the last consumed token. */
211 location_t last_token_location;
214 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
216 c_token *
217 c_parser_tokens_buf (c_parser *parser, unsigned n)
219 return &parser->tokens_buf[n];
222 /* Return the error state of PARSER. */
224 bool
225 c_parser_error (c_parser *parser)
227 return parser->error;
230 /* Set the error state of PARSER to ERR. */
232 void
233 c_parser_set_error (c_parser *parser, bool err)
235 parser->error = err;
239 /* The actual parser and external interface. ??? Does this need to be
240 garbage-collected? */
242 static GTY (()) c_parser *the_parser;
244 /* Read in and lex a single token, storing it in *TOKEN. */
246 static void
247 c_lex_one_token (c_parser *parser, c_token *token)
249 timevar_push (TV_LEX);
251 token->type = c_lex_with_flags (&token->value, &token->location,
252 &token->flags,
253 (parser->lex_untranslated_string
254 ? C_LEX_STRING_NO_TRANSLATE : 0));
255 token->id_kind = C_ID_NONE;
256 token->keyword = RID_MAX;
257 token->pragma_kind = PRAGMA_NONE;
259 switch (token->type)
261 case CPP_NAME:
263 tree decl;
265 bool objc_force_identifier = parser->objc_need_raw_identifier;
266 if (c_dialect_objc ())
267 parser->objc_need_raw_identifier = false;
269 if (C_IS_RESERVED_WORD (token->value))
271 enum rid rid_code = C_RID_CODE (token->value);
273 if (rid_code == RID_CXX_COMPAT_WARN)
275 warning_at (token->location,
276 OPT_Wc___compat,
277 "identifier %qE conflicts with C++ keyword",
278 token->value);
280 else if (rid_code >= RID_FIRST_ADDR_SPACE
281 && rid_code <= RID_LAST_ADDR_SPACE)
283 addr_space_t as;
284 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
285 targetm.addr_space.diagnose_usage (as, token->location);
286 token->id_kind = C_ID_ADDRSPACE;
287 token->keyword = rid_code;
288 break;
290 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
292 /* We found an Objective-C "pq" keyword (in, out,
293 inout, bycopy, byref, oneway). They need special
294 care because the interpretation depends on the
295 context. */
296 if (parser->objc_pq_context)
298 token->type = CPP_KEYWORD;
299 token->keyword = rid_code;
300 break;
302 else if (parser->objc_could_be_foreach_context
303 && rid_code == RID_IN)
305 /* We are in Objective-C, inside a (potential)
306 foreach context (which means after having
307 parsed 'for (', but before having parsed ';'),
308 and we found 'in'. We consider it the keyword
309 which terminates the declaration at the
310 beginning of a foreach-statement. Note that
311 this means you can't use 'in' for anything else
312 in that context; in particular, in Objective-C
313 you can't use 'in' as the name of the running
314 variable in a C for loop. We could potentially
315 try to add code here to disambiguate, but it
316 seems a reasonable limitation. */
317 token->type = CPP_KEYWORD;
318 token->keyword = rid_code;
319 break;
321 /* Else, "pq" keywords outside of the "pq" context are
322 not keywords, and we fall through to the code for
323 normal tokens. */
325 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
327 /* We found an Objective-C "property attribute"
328 keyword (getter, setter, readonly, etc). These are
329 only valid in the property context. */
330 if (parser->objc_property_attr_context)
332 token->type = CPP_KEYWORD;
333 token->keyword = rid_code;
334 break;
336 /* Else they are not special keywords.
339 else if (c_dialect_objc ()
340 && (OBJC_IS_AT_KEYWORD (rid_code)
341 || OBJC_IS_CXX_KEYWORD (rid_code)))
343 /* We found one of the Objective-C "@" keywords (defs,
344 selector, synchronized, etc) or one of the
345 Objective-C "cxx" keywords (class, private,
346 protected, public, try, catch, throw) without a
347 preceding '@' sign. Do nothing and fall through to
348 the code for normal tokens (in C++ we would still
349 consider the CXX ones keywords, but not in C). */
352 else
354 token->type = CPP_KEYWORD;
355 token->keyword = rid_code;
356 break;
360 decl = lookup_name (token->value);
361 if (decl)
363 if (TREE_CODE (decl) == TYPE_DECL)
365 token->id_kind = C_ID_TYPENAME;
366 break;
369 else if (c_dialect_objc ())
371 tree objc_interface_decl = objc_is_class_name (token->value);
372 /* Objective-C class names are in the same namespace as
373 variables and typedefs, and hence are shadowed by local
374 declarations. */
375 if (objc_interface_decl
376 && (!objc_force_identifier || global_bindings_p ()))
378 token->value = objc_interface_decl;
379 token->id_kind = C_ID_CLASSNAME;
380 break;
383 token->id_kind = C_ID_ID;
385 break;
386 case CPP_AT_NAME:
387 /* This only happens in Objective-C; it must be a keyword. */
388 token->type = CPP_KEYWORD;
389 switch (C_RID_CODE (token->value))
391 /* Replace 'class' with '@class', 'private' with '@private',
392 etc. This prevents confusion with the C++ keyword
393 'class', and makes the tokens consistent with other
394 Objective-C 'AT' keywords. For example '@class' is
395 reported as RID_AT_CLASS which is consistent with
396 '@synchronized', which is reported as
397 RID_AT_SYNCHRONIZED.
399 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
400 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
401 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
402 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
403 case RID_THROW: token->keyword = RID_AT_THROW; break;
404 case RID_TRY: token->keyword = RID_AT_TRY; break;
405 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
406 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
407 default: token->keyword = C_RID_CODE (token->value);
409 break;
410 case CPP_COLON:
411 case CPP_COMMA:
412 case CPP_CLOSE_PAREN:
413 case CPP_SEMICOLON:
414 /* These tokens may affect the interpretation of any identifiers
415 following, if doing Objective-C. */
416 if (c_dialect_objc ())
417 parser->objc_need_raw_identifier = false;
418 break;
419 case CPP_PRAGMA:
420 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
421 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
422 token->value = NULL;
423 break;
424 default:
425 break;
427 timevar_pop (TV_LEX);
430 /* Return a pointer to the next token from PARSER, reading it in if
431 necessary. */
433 c_token *
434 c_parser_peek_token (c_parser *parser)
436 if (parser->tokens_avail == 0)
438 c_lex_one_token (parser, &parser->tokens[0]);
439 parser->tokens_avail = 1;
441 return &parser->tokens[0];
444 /* Return a pointer to the next-but-one token from PARSER, reading it
445 in if necessary. The next token is already read in. */
447 c_token *
448 c_parser_peek_2nd_token (c_parser *parser)
450 if (parser->tokens_avail >= 2)
451 return &parser->tokens[1];
452 gcc_assert (parser->tokens_avail == 1);
453 gcc_assert (parser->tokens[0].type != CPP_EOF);
454 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
455 c_lex_one_token (parser, &parser->tokens[1]);
456 parser->tokens_avail = 2;
457 return &parser->tokens[1];
460 /* Return a pointer to the Nth token from PARSER, reading it
461 in if necessary. The N-1th token is already read in. */
463 c_token *
464 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
466 /* N is 1-based, not zero-based. */
467 gcc_assert (n > 0);
469 if (parser->tokens_avail >= n)
470 return &parser->tokens[n - 1];
471 gcc_assert (parser->tokens_avail == n - 1);
472 c_lex_one_token (parser, &parser->tokens[n - 1]);
473 parser->tokens_avail = n;
474 return &parser->tokens[n - 1];
477 bool
478 c_keyword_starts_typename (enum rid keyword)
480 switch (keyword)
482 case RID_UNSIGNED:
483 case RID_LONG:
484 case RID_SHORT:
485 case RID_SIGNED:
486 case RID_COMPLEX:
487 case RID_INT:
488 case RID_CHAR:
489 case RID_FLOAT:
490 case RID_DOUBLE:
491 case RID_VOID:
492 case RID_DFLOAT32:
493 case RID_DFLOAT64:
494 case RID_DFLOAT128:
495 CASE_RID_FLOATN_NX:
496 case RID_BOOL:
497 case RID_ENUM:
498 case RID_STRUCT:
499 case RID_UNION:
500 case RID_TYPEOF:
501 case RID_CONST:
502 case RID_ATOMIC:
503 case RID_VOLATILE:
504 case RID_RESTRICT:
505 case RID_ATTRIBUTE:
506 case RID_FRACT:
507 case RID_ACCUM:
508 case RID_SAT:
509 case RID_AUTO_TYPE:
510 return true;
511 default:
512 if (keyword >= RID_FIRST_INT_N
513 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
514 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
515 return true;
516 return false;
520 /* Return true if TOKEN can start a type name,
521 false otherwise. */
522 bool
523 c_token_starts_typename (c_token *token)
525 switch (token->type)
527 case CPP_NAME:
528 switch (token->id_kind)
530 case C_ID_ID:
531 return false;
532 case C_ID_ADDRSPACE:
533 return true;
534 case C_ID_TYPENAME:
535 return true;
536 case C_ID_CLASSNAME:
537 gcc_assert (c_dialect_objc ());
538 return true;
539 default:
540 gcc_unreachable ();
542 case CPP_KEYWORD:
543 return c_keyword_starts_typename (token->keyword);
544 case CPP_LESS:
545 if (c_dialect_objc ())
546 return true;
547 return false;
548 default:
549 return false;
553 /* Return true if the next token from PARSER can start a type name,
554 false otherwise. LA specifies how to do lookahead in order to
555 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
557 static inline bool
558 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
560 c_token *token = c_parser_peek_token (parser);
561 if (c_token_starts_typename (token))
562 return true;
564 /* Try a bit harder to detect an unknown typename. */
565 if (la != cla_prefer_id
566 && token->type == CPP_NAME
567 && token->id_kind == C_ID_ID
569 /* Do not try too hard when we could have "object in array". */
570 && !parser->objc_could_be_foreach_context
572 && (la == cla_prefer_type
573 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
574 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
576 /* Only unknown identifiers. */
577 && !lookup_name (token->value))
578 return true;
580 return false;
583 /* Return true if TOKEN is a type qualifier, false otherwise. */
584 static bool
585 c_token_is_qualifier (c_token *token)
587 switch (token->type)
589 case CPP_NAME:
590 switch (token->id_kind)
592 case C_ID_ADDRSPACE:
593 return true;
594 default:
595 return false;
597 case CPP_KEYWORD:
598 switch (token->keyword)
600 case RID_CONST:
601 case RID_VOLATILE:
602 case RID_RESTRICT:
603 case RID_ATTRIBUTE:
604 case RID_ATOMIC:
605 return true;
606 default:
607 return false;
609 case CPP_LESS:
610 return false;
611 default:
612 gcc_unreachable ();
616 /* Return true if the next token from PARSER is a type qualifier,
617 false otherwise. */
618 static inline bool
619 c_parser_next_token_is_qualifier (c_parser *parser)
621 c_token *token = c_parser_peek_token (parser);
622 return c_token_is_qualifier (token);
625 /* Return true if TOKEN can start declaration specifiers, false
626 otherwise. */
627 static bool
628 c_token_starts_declspecs (c_token *token)
630 switch (token->type)
632 case CPP_NAME:
633 switch (token->id_kind)
635 case C_ID_ID:
636 return false;
637 case C_ID_ADDRSPACE:
638 return true;
639 case C_ID_TYPENAME:
640 return true;
641 case C_ID_CLASSNAME:
642 gcc_assert (c_dialect_objc ());
643 return true;
644 default:
645 gcc_unreachable ();
647 case CPP_KEYWORD:
648 switch (token->keyword)
650 case RID_STATIC:
651 case RID_EXTERN:
652 case RID_REGISTER:
653 case RID_TYPEDEF:
654 case RID_INLINE:
655 case RID_NORETURN:
656 case RID_AUTO:
657 case RID_THREAD:
658 case RID_UNSIGNED:
659 case RID_LONG:
660 case RID_SHORT:
661 case RID_SIGNED:
662 case RID_COMPLEX:
663 case RID_INT:
664 case RID_CHAR:
665 case RID_FLOAT:
666 case RID_DOUBLE:
667 case RID_VOID:
668 case RID_DFLOAT32:
669 case RID_DFLOAT64:
670 case RID_DFLOAT128:
671 CASE_RID_FLOATN_NX:
672 case RID_BOOL:
673 case RID_ENUM:
674 case RID_STRUCT:
675 case RID_UNION:
676 case RID_TYPEOF:
677 case RID_CONST:
678 case RID_VOLATILE:
679 case RID_RESTRICT:
680 case RID_ATTRIBUTE:
681 case RID_FRACT:
682 case RID_ACCUM:
683 case RID_SAT:
684 case RID_ALIGNAS:
685 case RID_ATOMIC:
686 case RID_AUTO_TYPE:
687 return true;
688 default:
689 if (token->keyword >= RID_FIRST_INT_N
690 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
691 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
692 return true;
693 return false;
695 case CPP_LESS:
696 if (c_dialect_objc ())
697 return true;
698 return false;
699 default:
700 return false;
705 /* Return true if TOKEN can start declaration specifiers or a static
706 assertion, false otherwise. */
707 static bool
708 c_token_starts_declaration (c_token *token)
710 if (c_token_starts_declspecs (token)
711 || token->keyword == RID_STATIC_ASSERT)
712 return true;
713 else
714 return false;
717 /* Return true if the next token from PARSER can start declaration
718 specifiers, false otherwise. */
719 bool
720 c_parser_next_token_starts_declspecs (c_parser *parser)
722 c_token *token = c_parser_peek_token (parser);
724 /* In Objective-C, a classname normally starts a declspecs unless it
725 is immediately followed by a dot. In that case, it is the
726 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
727 setter/getter on the class. c_token_starts_declspecs() can't
728 differentiate between the two cases because it only checks the
729 current token, so we have a special check here. */
730 if (c_dialect_objc ()
731 && token->type == CPP_NAME
732 && token->id_kind == C_ID_CLASSNAME
733 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
734 return false;
736 return c_token_starts_declspecs (token);
739 /* Return true if the next tokens from PARSER can start declaration
740 specifiers or a static assertion, false otherwise. */
741 bool
742 c_parser_next_tokens_start_declaration (c_parser *parser)
744 c_token *token = c_parser_peek_token (parser);
746 /* Same as above. */
747 if (c_dialect_objc ()
748 && token->type == CPP_NAME
749 && token->id_kind == C_ID_CLASSNAME
750 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
751 return false;
753 /* Labels do not start declarations. */
754 if (token->type == CPP_NAME
755 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
756 return false;
758 if (c_token_starts_declaration (token))
759 return true;
761 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
762 return true;
764 return false;
767 /* Consume the next token from PARSER. */
769 void
770 c_parser_consume_token (c_parser *parser)
772 gcc_assert (parser->tokens_avail >= 1);
773 gcc_assert (parser->tokens[0].type != CPP_EOF);
774 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
775 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
776 parser->last_token_location = parser->tokens[0].location;
777 if (parser->tokens != &parser->tokens_buf[0])
778 parser->tokens++;
779 else if (parser->tokens_avail == 2)
780 parser->tokens[0] = parser->tokens[1];
781 parser->tokens_avail--;
784 /* Expect the current token to be a #pragma. Consume it and remember
785 that we've begun parsing a pragma. */
787 static void
788 c_parser_consume_pragma (c_parser *parser)
790 gcc_assert (!parser->in_pragma);
791 gcc_assert (parser->tokens_avail >= 1);
792 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
793 if (parser->tokens != &parser->tokens_buf[0])
794 parser->tokens++;
795 else if (parser->tokens_avail == 2)
796 parser->tokens[0] = parser->tokens[1];
797 parser->tokens_avail--;
798 parser->in_pragma = true;
801 /* Update the global input_location from TOKEN. */
802 static inline void
803 c_parser_set_source_position_from_token (c_token *token)
805 if (token->type != CPP_EOF)
807 input_location = token->location;
811 /* Helper function for c_parser_error.
812 Having peeked a token of kind TOK1_KIND that might signify
813 a conflict marker, peek successor tokens to determine
814 if we actually do have a conflict marker.
815 Specifically, we consider a run of 7 '<', '=' or '>' characters
816 at the start of a line as a conflict marker.
817 These come through the lexer as three pairs and a single,
818 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
819 If it returns true, *OUT_LOC is written to with the location/range
820 of the marker. */
822 static bool
823 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
824 location_t *out_loc)
826 c_token *token2 = c_parser_peek_2nd_token (parser);
827 if (token2->type != tok1_kind)
828 return false;
829 c_token *token3 = c_parser_peek_nth_token (parser, 3);
830 if (token3->type != tok1_kind)
831 return false;
832 c_token *token4 = c_parser_peek_nth_token (parser, 4);
833 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
834 return false;
836 /* It must be at the start of the line. */
837 location_t start_loc = c_parser_peek_token (parser)->location;
838 if (LOCATION_COLUMN (start_loc) != 1)
839 return false;
841 /* We have a conflict marker. Construct a location of the form:
842 <<<<<<<
843 ^~~~~~~
844 with start == caret, finishing at the end of the marker. */
845 location_t finish_loc = get_finish (token4->location);
846 *out_loc = make_location (start_loc, start_loc, finish_loc);
848 return true;
851 /* Issue a diagnostic of the form
852 FILE:LINE: MESSAGE before TOKEN
853 where TOKEN is the next token in the input stream of PARSER.
854 MESSAGE (specified by the caller) is usually of the form "expected
855 OTHER-TOKEN".
857 Use RICHLOC as the location of the diagnostic.
859 Do not issue a diagnostic if still recovering from an error.
861 Return true iff an error was actually emitted.
863 ??? This is taken from the C++ parser, but building up messages in
864 this way is not i18n-friendly and some other approach should be
865 used. */
867 static bool
868 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
869 rich_location *richloc)
871 c_token *token = c_parser_peek_token (parser);
872 if (parser->error)
873 return false;
874 parser->error = true;
875 if (!gmsgid)
876 return false;
878 /* If this is actually a conflict marker, report it as such. */
879 if (token->type == CPP_LSHIFT
880 || token->type == CPP_RSHIFT
881 || token->type == CPP_EQ_EQ)
883 location_t loc;
884 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
886 error_at (loc, "version control conflict marker in file");
887 return true;
891 c_parse_error (gmsgid,
892 /* Because c_parse_error does not understand
893 CPP_KEYWORD, keywords are treated like
894 identifiers. */
895 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
896 /* ??? The C parser does not save the cpp flags of a
897 token, we need to pass 0 here and we will not get
898 the source spelling of some tokens but rather the
899 canonical spelling. */
900 token->value, /*flags=*/0, richloc);
901 return true;
904 /* As c_parser_error_richloc, but issue the message at the
905 location of PARSER's next token, or at input_location
906 if the next token is EOF. */
908 bool
909 c_parser_error (c_parser *parser, const char *gmsgid)
911 c_token *token = c_parser_peek_token (parser);
912 c_parser_set_source_position_from_token (token);
913 rich_location richloc (line_table, input_location);
914 return c_parser_error_richloc (parser, gmsgid, &richloc);
917 /* Some tokens naturally come in pairs e.g.'(' and ')'.
918 This class is for tracking such a matching pair of symbols.
919 In particular, it tracks the location of the first token,
920 so that if the second token is missing, we can highlight the
921 location of the first token when notifying the user about the
922 problem. */
924 template <typename traits_t>
925 class token_pair
927 public:
928 /* token_pair's ctor. */
929 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
931 /* If the next token is the opening symbol for this pair, consume it and
932 return true.
933 Otherwise, issue an error and return false.
934 In either case, record the location of the opening token. */
936 bool require_open (c_parser *parser)
938 c_token *token = c_parser_peek_token (parser);
939 if (token)
940 m_open_loc = token->location;
942 return c_parser_require (parser, traits_t::open_token_type,
943 traits_t::open_gmsgid);
946 /* Consume the next token from PARSER, recording its location as
947 that of the opening token within the pair. */
949 void consume_open (c_parser *parser)
951 c_token *token = c_parser_peek_token (parser);
952 gcc_assert (token->type == traits_t::open_token_type);
953 m_open_loc = token->location;
954 c_parser_consume_token (parser);
957 /* If the next token is the closing symbol for this pair, consume it
958 and return true.
959 Otherwise, issue an error, highlighting the location of the
960 corresponding opening token, and return false. */
962 bool require_close (c_parser *parser) const
964 return c_parser_require (parser, traits_t::close_token_type,
965 traits_t::close_gmsgid, m_open_loc);
968 /* Like token_pair::require_close, except that tokens will be skipped
969 until the desired token is found. An error message is still produced
970 if the next token is not as expected. */
972 void skip_until_found_close (c_parser *parser) const
974 c_parser_skip_until_found (parser, traits_t::close_token_type,
975 traits_t::close_gmsgid, m_open_loc);
978 private:
979 location_t m_open_loc;
982 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
984 struct matching_paren_traits
986 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
987 static const char * const open_gmsgid;
988 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
989 static const char * const close_gmsgid;
992 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
993 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
995 /* "matching_parens" is a token_pair<T> class for tracking matching
996 pairs of parentheses. */
998 typedef token_pair<matching_paren_traits> matching_parens;
1000 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1002 struct matching_brace_traits
1004 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1005 static const char * const open_gmsgid;
1006 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1007 static const char * const close_gmsgid;
1010 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1011 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1013 /* "matching_braces" is a token_pair<T> class for tracking matching
1014 pairs of braces. */
1016 typedef token_pair<matching_brace_traits> matching_braces;
1018 /* Get a description of the matching symbol to TYPE e.g. "(" for
1019 CPP_CLOSE_PAREN. */
1021 static const char *
1022 get_matching_symbol (enum cpp_ttype type)
1024 switch (type)
1026 default:
1027 gcc_unreachable ();
1028 return "";
1029 case CPP_CLOSE_PAREN:
1030 return "(";
1031 case CPP_CLOSE_BRACE:
1032 return "{";
1036 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1037 issue the error MSGID. If MSGID is NULL then a message has already
1038 been produced and no message will be produced this time. Returns
1039 true if found, false otherwise.
1041 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1042 within any error as the location of an "opening" token matching
1043 the close token TYPE (e.g. the location of the '(' when TYPE is
1044 CPP_CLOSE_PAREN).
1046 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1047 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1048 attempt to generate a fix-it hint for the problem.
1049 Otherwise msgid describes multiple token types (e.g.
1050 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1051 generate a fix-it hint. */
1053 bool
1054 c_parser_require (c_parser *parser,
1055 enum cpp_ttype type,
1056 const char *msgid,
1057 location_t matching_location,
1058 bool type_is_unique)
1060 if (c_parser_next_token_is (parser, type))
1062 c_parser_consume_token (parser);
1063 return true;
1065 else
1067 location_t next_token_loc = c_parser_peek_token (parser)->location;
1068 gcc_rich_location richloc (next_token_loc);
1070 /* Potentially supply a fix-it hint, suggesting to add the
1071 missing token immediately after the *previous* token.
1072 This may move the primary location within richloc. */
1073 if (!parser->error && type_is_unique)
1074 maybe_suggest_missing_token_insertion (&richloc, type,
1075 parser->last_token_location);
1077 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1078 Attempt to consolidate diagnostics by printing it as a
1079 secondary range within the main diagnostic. */
1080 bool added_matching_location = false;
1081 if (matching_location != UNKNOWN_LOCATION)
1082 added_matching_location
1083 = richloc.add_location_if_nearby (matching_location);
1085 if (c_parser_error_richloc (parser, msgid, &richloc))
1086 /* If we weren't able to consolidate matching_location, then
1087 print it as a secondary diagnostic. */
1088 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1089 inform (matching_location, "to match this %qs",
1090 get_matching_symbol (type));
1092 return false;
1096 /* If the next token is the indicated keyword, consume it. Otherwise,
1097 issue the error MSGID. Returns true if found, false otherwise. */
1099 static bool
1100 c_parser_require_keyword (c_parser *parser,
1101 enum rid keyword,
1102 const char *msgid)
1104 if (c_parser_next_token_is_keyword (parser, keyword))
1106 c_parser_consume_token (parser);
1107 return true;
1109 else
1111 c_parser_error (parser, msgid);
1112 return false;
1116 /* Like c_parser_require, except that tokens will be skipped until the
1117 desired token is found. An error message is still produced if the
1118 next token is not as expected. If MSGID is NULL then a message has
1119 already been produced and no message will be produced this
1120 time.
1122 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1123 within any error as the location of an "opening" token matching
1124 the close token TYPE (e.g. the location of the '(' when TYPE is
1125 CPP_CLOSE_PAREN). */
1127 void
1128 c_parser_skip_until_found (c_parser *parser,
1129 enum cpp_ttype type,
1130 const char *msgid,
1131 location_t matching_location)
1133 unsigned nesting_depth = 0;
1135 if (c_parser_require (parser, type, msgid, matching_location))
1136 return;
1138 /* Skip tokens until the desired token is found. */
1139 while (true)
1141 /* Peek at the next token. */
1142 c_token *token = c_parser_peek_token (parser);
1143 /* If we've reached the token we want, consume it and stop. */
1144 if (token->type == type && !nesting_depth)
1146 c_parser_consume_token (parser);
1147 break;
1150 /* If we've run out of tokens, stop. */
1151 if (token->type == CPP_EOF)
1152 return;
1153 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1154 return;
1155 if (token->type == CPP_OPEN_BRACE
1156 || token->type == CPP_OPEN_PAREN
1157 || token->type == CPP_OPEN_SQUARE)
1158 ++nesting_depth;
1159 else if (token->type == CPP_CLOSE_BRACE
1160 || token->type == CPP_CLOSE_PAREN
1161 || token->type == CPP_CLOSE_SQUARE)
1163 if (nesting_depth-- == 0)
1164 break;
1166 /* Consume this token. */
1167 c_parser_consume_token (parser);
1169 parser->error = false;
1172 /* Skip tokens until the end of a parameter is found, but do not
1173 consume the comma, semicolon or closing delimiter. */
1175 static void
1176 c_parser_skip_to_end_of_parameter (c_parser *parser)
1178 unsigned nesting_depth = 0;
1180 while (true)
1182 c_token *token = c_parser_peek_token (parser);
1183 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1184 && !nesting_depth)
1185 break;
1186 /* If we've run out of tokens, stop. */
1187 if (token->type == CPP_EOF)
1188 return;
1189 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1190 return;
1191 if (token->type == CPP_OPEN_BRACE
1192 || token->type == CPP_OPEN_PAREN
1193 || token->type == CPP_OPEN_SQUARE)
1194 ++nesting_depth;
1195 else if (token->type == CPP_CLOSE_BRACE
1196 || token->type == CPP_CLOSE_PAREN
1197 || token->type == CPP_CLOSE_SQUARE)
1199 if (nesting_depth-- == 0)
1200 break;
1202 /* Consume this token. */
1203 c_parser_consume_token (parser);
1205 parser->error = false;
1208 /* Expect to be at the end of the pragma directive and consume an
1209 end of line marker. */
1211 static void
1212 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1214 gcc_assert (parser->in_pragma);
1215 parser->in_pragma = false;
1217 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1218 c_parser_error (parser, "expected end of line");
1220 cpp_ttype token_type;
1223 c_token *token = c_parser_peek_token (parser);
1224 token_type = token->type;
1225 if (token_type == CPP_EOF)
1226 break;
1227 c_parser_consume_token (parser);
1229 while (token_type != CPP_PRAGMA_EOL);
1231 parser->error = false;
1234 /* Skip tokens until we have consumed an entire block, or until we
1235 have consumed a non-nested ';'. */
1237 static void
1238 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1240 unsigned nesting_depth = 0;
1241 bool save_error = parser->error;
1243 while (true)
1245 c_token *token;
1247 /* Peek at the next token. */
1248 token = c_parser_peek_token (parser);
1250 switch (token->type)
1252 case CPP_EOF:
1253 return;
1255 case CPP_PRAGMA_EOL:
1256 if (parser->in_pragma)
1257 return;
1258 break;
1260 case CPP_SEMICOLON:
1261 /* If the next token is a ';', we have reached the
1262 end of the statement. */
1263 if (!nesting_depth)
1265 /* Consume the ';'. */
1266 c_parser_consume_token (parser);
1267 goto finished;
1269 break;
1271 case CPP_CLOSE_BRACE:
1272 /* If the next token is a non-nested '}', then we have
1273 reached the end of the current block. */
1274 if (nesting_depth == 0 || --nesting_depth == 0)
1276 c_parser_consume_token (parser);
1277 goto finished;
1279 break;
1281 case CPP_OPEN_BRACE:
1282 /* If it the next token is a '{', then we are entering a new
1283 block. Consume the entire block. */
1284 ++nesting_depth;
1285 break;
1287 case CPP_PRAGMA:
1288 /* If we see a pragma, consume the whole thing at once. We
1289 have some safeguards against consuming pragmas willy-nilly.
1290 Normally, we'd expect to be here with parser->error set,
1291 which disables these safeguards. But it's possible to get
1292 here for secondary error recovery, after parser->error has
1293 been cleared. */
1294 c_parser_consume_pragma (parser);
1295 c_parser_skip_to_pragma_eol (parser);
1296 parser->error = save_error;
1297 continue;
1299 default:
1300 break;
1303 c_parser_consume_token (parser);
1306 finished:
1307 parser->error = false;
1310 /* CPP's options (initialized by c-opts.c). */
1311 extern cpp_options *cpp_opts;
1313 /* Save the warning flags which are controlled by __extension__. */
1315 static inline int
1316 disable_extension_diagnostics (void)
1318 int ret = (pedantic
1319 | (warn_pointer_arith << 1)
1320 | (warn_traditional << 2)
1321 | (flag_iso << 3)
1322 | (warn_long_long << 4)
1323 | (warn_cxx_compat << 5)
1324 | (warn_overlength_strings << 6)
1325 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1326 play tricks to properly restore it. */
1327 | ((warn_c90_c99_compat == 1) << 7)
1328 | ((warn_c90_c99_compat == -1) << 8)
1329 /* Similarly for warn_c99_c11_compat. */
1330 | ((warn_c99_c11_compat == 1) << 9)
1331 | ((warn_c99_c11_compat == -1) << 10)
1333 cpp_opts->cpp_pedantic = pedantic = 0;
1334 warn_pointer_arith = 0;
1335 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1336 flag_iso = 0;
1337 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1338 warn_cxx_compat = 0;
1339 warn_overlength_strings = 0;
1340 warn_c90_c99_compat = 0;
1341 warn_c99_c11_compat = 0;
1342 return ret;
1345 /* Restore the warning flags which are controlled by __extension__.
1346 FLAGS is the return value from disable_extension_diagnostics. */
1348 static inline void
1349 restore_extension_diagnostics (int flags)
1351 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1352 warn_pointer_arith = (flags >> 1) & 1;
1353 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1354 flag_iso = (flags >> 3) & 1;
1355 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1356 warn_cxx_compat = (flags >> 5) & 1;
1357 warn_overlength_strings = (flags >> 6) & 1;
1358 /* See above for why is this needed. */
1359 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1360 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1363 /* Helper data structure for parsing #pragma acc routine. */
1364 struct oacc_routine_data {
1365 bool error_seen; /* Set if error has been reported. */
1366 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1367 tree clauses;
1368 location_t loc;
1371 static void c_parser_external_declaration (c_parser *);
1372 static void c_parser_asm_definition (c_parser *);
1373 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1374 bool, bool, tree *, vec<c_token>,
1375 struct oacc_routine_data * = NULL,
1376 bool * = NULL);
1377 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1378 static void c_parser_static_assert_declaration (c_parser *);
1379 static struct c_typespec c_parser_enum_specifier (c_parser *);
1380 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1381 static tree c_parser_struct_declaration (c_parser *);
1382 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1383 static tree c_parser_alignas_specifier (c_parser *);
1384 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1385 c_dtr_syn, bool *);
1386 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1387 bool,
1388 struct c_declarator *);
1389 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1390 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1391 tree);
1392 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1393 static tree c_parser_simple_asm_expr (c_parser *);
1394 static tree c_parser_attributes (c_parser *);
1395 static struct c_expr c_parser_initializer (c_parser *);
1396 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1397 struct obstack *);
1398 static void c_parser_initelt (c_parser *, struct obstack *);
1399 static void c_parser_initval (c_parser *, struct c_expr *,
1400 struct obstack *);
1401 static tree c_parser_compound_statement (c_parser *);
1402 static void c_parser_compound_statement_nostart (c_parser *);
1403 static void c_parser_label (c_parser *);
1404 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1405 static void c_parser_statement_after_labels (c_parser *, bool *,
1406 vec<tree> * = NULL);
1407 static tree c_parser_c99_block_statement (c_parser *, bool *,
1408 location_t * = NULL);
1409 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1410 static void c_parser_switch_statement (c_parser *, bool *);
1411 static void c_parser_while_statement (c_parser *, bool, bool *);
1412 static void c_parser_do_statement (c_parser *, bool);
1413 static void c_parser_for_statement (c_parser *, bool, bool *);
1414 static tree c_parser_asm_statement (c_parser *);
1415 static tree c_parser_asm_operands (c_parser *);
1416 static tree c_parser_asm_goto_operands (c_parser *);
1417 static tree c_parser_asm_clobbers (c_parser *);
1418 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1419 tree = NULL_TREE);
1420 static struct c_expr c_parser_conditional_expression (c_parser *,
1421 struct c_expr *, tree);
1422 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1423 tree);
1424 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1425 static struct c_expr c_parser_unary_expression (c_parser *);
1426 static struct c_expr c_parser_sizeof_expression (c_parser *);
1427 static struct c_expr c_parser_alignof_expression (c_parser *);
1428 static struct c_expr c_parser_postfix_expression (c_parser *);
1429 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1430 struct c_type_name *,
1431 location_t);
1432 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1433 location_t loc,
1434 struct c_expr);
1435 static tree c_parser_transaction (c_parser *, enum rid);
1436 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1437 static tree c_parser_transaction_cancel (c_parser *);
1438 static struct c_expr c_parser_expression (c_parser *);
1439 static struct c_expr c_parser_expression_conv (c_parser *);
1440 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1441 vec<tree, va_gc> **, location_t *,
1442 tree *, vec<location_t> *,
1443 unsigned int * = NULL);
1444 static void c_parser_oacc_declare (c_parser *);
1445 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1446 static void c_parser_oacc_update (c_parser *);
1447 static void c_parser_omp_construct (c_parser *, bool *);
1448 static void c_parser_omp_threadprivate (c_parser *);
1449 static void c_parser_omp_barrier (c_parser *);
1450 static void c_parser_omp_flush (c_parser *);
1451 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1452 tree, tree *, bool *);
1453 static void c_parser_omp_taskwait (c_parser *);
1454 static void c_parser_omp_taskyield (c_parser *);
1455 static void c_parser_omp_cancel (c_parser *);
1457 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1458 pragma_stmt, pragma_compound };
1459 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1460 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1461 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1462 static void c_parser_omp_end_declare_target (c_parser *);
1463 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1464 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1465 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1467 /* These Objective-C parser functions are only ever called when
1468 compiling Objective-C. */
1469 static void c_parser_objc_class_definition (c_parser *, tree);
1470 static void c_parser_objc_class_instance_variables (c_parser *);
1471 static void c_parser_objc_class_declaration (c_parser *);
1472 static void c_parser_objc_alias_declaration (c_parser *);
1473 static void c_parser_objc_protocol_definition (c_parser *, tree);
1474 static bool c_parser_objc_method_type (c_parser *);
1475 static void c_parser_objc_method_definition (c_parser *);
1476 static void c_parser_objc_methodprotolist (c_parser *);
1477 static void c_parser_objc_methodproto (c_parser *);
1478 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1479 static tree c_parser_objc_type_name (c_parser *);
1480 static tree c_parser_objc_protocol_refs (c_parser *);
1481 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1482 static void c_parser_objc_synchronized_statement (c_parser *);
1483 static tree c_parser_objc_selector (c_parser *);
1484 static tree c_parser_objc_selector_arg (c_parser *);
1485 static tree c_parser_objc_receiver (c_parser *);
1486 static tree c_parser_objc_message_args (c_parser *);
1487 static tree c_parser_objc_keywordexpr (c_parser *);
1488 static void c_parser_objc_at_property_declaration (c_parser *);
1489 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1490 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1491 static bool c_parser_objc_diagnose_bad_element_prefix
1492 (c_parser *, struct c_declspecs *);
1494 /* Cilk Plus supporting routines. */
1495 static void c_parser_cilk_simd (c_parser *, bool *);
1496 static void c_parser_cilk_for (c_parser *, tree, bool *);
1497 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1498 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1499 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1500 static void c_parser_cilk_grainsize (c_parser *, bool *);
1502 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1504 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1506 translation-unit:
1507 external-declarations
1509 external-declarations:
1510 external-declaration
1511 external-declarations external-declaration
1513 GNU extensions:
1515 translation-unit:
1516 empty
1519 static void
1520 c_parser_translation_unit (c_parser *parser)
1522 if (c_parser_next_token_is (parser, CPP_EOF))
1524 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1525 "ISO C forbids an empty translation unit");
1527 else
1529 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1530 mark_valid_location_for_stdc_pragma (false);
1533 ggc_collect ();
1534 c_parser_external_declaration (parser);
1535 obstack_free (&parser_obstack, obstack_position);
1537 while (c_parser_next_token_is_not (parser, CPP_EOF));
1540 unsigned int i;
1541 tree decl;
1542 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1543 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1544 error ("storage size of %q+D isn%'t known", decl);
1547 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1549 external-declaration:
1550 function-definition
1551 declaration
1553 GNU extensions:
1555 external-declaration:
1556 asm-definition
1558 __extension__ external-declaration
1560 Objective-C:
1562 external-declaration:
1563 objc-class-definition
1564 objc-class-declaration
1565 objc-alias-declaration
1566 objc-protocol-definition
1567 objc-method-definition
1568 @end
1571 static void
1572 c_parser_external_declaration (c_parser *parser)
1574 int ext;
1575 switch (c_parser_peek_token (parser)->type)
1577 case CPP_KEYWORD:
1578 switch (c_parser_peek_token (parser)->keyword)
1580 case RID_EXTENSION:
1581 ext = disable_extension_diagnostics ();
1582 c_parser_consume_token (parser);
1583 c_parser_external_declaration (parser);
1584 restore_extension_diagnostics (ext);
1585 break;
1586 case RID_ASM:
1587 c_parser_asm_definition (parser);
1588 break;
1589 case RID_AT_INTERFACE:
1590 case RID_AT_IMPLEMENTATION:
1591 gcc_assert (c_dialect_objc ());
1592 c_parser_objc_class_definition (parser, NULL_TREE);
1593 break;
1594 case RID_AT_CLASS:
1595 gcc_assert (c_dialect_objc ());
1596 c_parser_objc_class_declaration (parser);
1597 break;
1598 case RID_AT_ALIAS:
1599 gcc_assert (c_dialect_objc ());
1600 c_parser_objc_alias_declaration (parser);
1601 break;
1602 case RID_AT_PROTOCOL:
1603 gcc_assert (c_dialect_objc ());
1604 c_parser_objc_protocol_definition (parser, NULL_TREE);
1605 break;
1606 case RID_AT_PROPERTY:
1607 gcc_assert (c_dialect_objc ());
1608 c_parser_objc_at_property_declaration (parser);
1609 break;
1610 case RID_AT_SYNTHESIZE:
1611 gcc_assert (c_dialect_objc ());
1612 c_parser_objc_at_synthesize_declaration (parser);
1613 break;
1614 case RID_AT_DYNAMIC:
1615 gcc_assert (c_dialect_objc ());
1616 c_parser_objc_at_dynamic_declaration (parser);
1617 break;
1618 case RID_AT_END:
1619 gcc_assert (c_dialect_objc ());
1620 c_parser_consume_token (parser);
1621 objc_finish_implementation ();
1622 break;
1623 default:
1624 goto decl_or_fndef;
1626 break;
1627 case CPP_SEMICOLON:
1628 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1629 "ISO C does not allow extra %<;%> outside of a function");
1630 c_parser_consume_token (parser);
1631 break;
1632 case CPP_PRAGMA:
1633 mark_valid_location_for_stdc_pragma (true);
1634 c_parser_pragma (parser, pragma_external, NULL);
1635 mark_valid_location_for_stdc_pragma (false);
1636 break;
1637 case CPP_PLUS:
1638 case CPP_MINUS:
1639 if (c_dialect_objc ())
1641 c_parser_objc_method_definition (parser);
1642 break;
1644 /* Else fall through, and yield a syntax error trying to parse
1645 as a declaration or function definition. */
1646 /* FALLTHRU */
1647 default:
1648 decl_or_fndef:
1649 /* A declaration or a function definition (or, in Objective-C,
1650 an @interface or @protocol with prefix attributes). We can
1651 only tell which after parsing the declaration specifiers, if
1652 any, and the first declarator. */
1653 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1654 NULL, vNULL);
1655 break;
1659 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1660 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1662 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1663 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1664 is accepted; otherwise (old-style parameter declarations) only other
1665 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1666 assertion is accepted; otherwise (old-style parameter declarations)
1667 it is not. If NESTED is true, we are inside a function or parsing
1668 old-style parameter declarations; any functions encountered are
1669 nested functions and declaration specifiers are required; otherwise
1670 we are at top level and functions are normal functions and
1671 declaration specifiers may be optional. If EMPTY_OK is true, empty
1672 declarations are OK (subject to all other constraints); otherwise
1673 (old-style parameter declarations) they are diagnosed. If
1674 START_ATTR_OK is true, the declaration specifiers may start with
1675 attributes; otherwise they may not.
1676 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1677 declaration when parsing an Objective-C foreach statement.
1678 FALLTHRU_ATTR_P is used to signal whether this function parsed
1679 "__attribute__((fallthrough));".
1681 declaration:
1682 declaration-specifiers init-declarator-list[opt] ;
1683 static_assert-declaration
1685 function-definition:
1686 declaration-specifiers[opt] declarator declaration-list[opt]
1687 compound-statement
1689 declaration-list:
1690 declaration
1691 declaration-list declaration
1693 init-declarator-list:
1694 init-declarator
1695 init-declarator-list , init-declarator
1697 init-declarator:
1698 declarator simple-asm-expr[opt] attributes[opt]
1699 declarator simple-asm-expr[opt] attributes[opt] = initializer
1701 GNU extensions:
1703 nested-function-definition:
1704 declaration-specifiers declarator declaration-list[opt]
1705 compound-statement
1707 attribute ;
1709 Objective-C:
1710 attributes objc-class-definition
1711 attributes objc-category-definition
1712 attributes objc-protocol-definition
1714 The simple-asm-expr and attributes are GNU extensions.
1716 This function does not handle __extension__; that is handled in its
1717 callers. ??? Following the old parser, __extension__ may start
1718 external declarations, declarations in functions and declarations
1719 at the start of "for" loops, but not old-style parameter
1720 declarations.
1722 C99 requires declaration specifiers in a function definition; the
1723 absence is diagnosed through the diagnosis of implicit int. In GNU
1724 C we also allow but diagnose declarations without declaration
1725 specifiers, but only at top level (elsewhere they conflict with
1726 other syntax).
1728 In Objective-C, declarations of the looping variable in a foreach
1729 statement are exceptionally terminated by 'in' (for example, 'for
1730 (NSObject *object in array) { ... }').
1732 OpenMP:
1734 declaration:
1735 threadprivate-directive
1737 GIMPLE:
1739 gimple-function-definition:
1740 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1741 declaration-list[opt] compound-statement
1743 rtl-function-definition:
1744 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1745 declaration-list[opt] compound-statement */
1747 static void
1748 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1749 bool static_assert_ok, bool empty_ok,
1750 bool nested, bool start_attr_ok,
1751 tree *objc_foreach_object_declaration,
1752 vec<c_token> omp_declare_simd_clauses,
1753 struct oacc_routine_data *oacc_routine_data,
1754 bool *fallthru_attr_p)
1756 struct c_declspecs *specs;
1757 tree prefix_attrs;
1758 tree all_prefix_attrs;
1759 bool diagnosed_no_specs = false;
1760 location_t here = c_parser_peek_token (parser)->location;
1762 if (static_assert_ok
1763 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1765 c_parser_static_assert_declaration (parser);
1766 return;
1768 specs = build_null_declspecs ();
1770 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1771 if (c_parser_peek_token (parser)->type == CPP_NAME
1772 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1773 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1774 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1775 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1777 tree name = c_parser_peek_token (parser)->value;
1779 /* Issue a warning about NAME being an unknown type name, perhaps
1780 with some kind of hint.
1781 If the user forgot a "struct" etc, suggest inserting
1782 it. Otherwise, attempt to look for misspellings. */
1783 gcc_rich_location richloc (here);
1784 if (tag_exists_p (RECORD_TYPE, name))
1786 /* This is not C++ with its implicit typedef. */
1787 richloc.add_fixit_insert_before ("struct ");
1788 error_at_rich_loc (&richloc,
1789 "unknown type name %qE;"
1790 " use %<struct%> keyword to refer to the type",
1791 name);
1793 else if (tag_exists_p (UNION_TYPE, name))
1795 richloc.add_fixit_insert_before ("union ");
1796 error_at_rich_loc (&richloc,
1797 "unknown type name %qE;"
1798 " use %<union%> keyword to refer to the type",
1799 name);
1801 else if (tag_exists_p (ENUMERAL_TYPE, name))
1803 richloc.add_fixit_insert_before ("enum ");
1804 error_at_rich_loc (&richloc,
1805 "unknown type name %qE;"
1806 " use %<enum%> keyword to refer to the type",
1807 name);
1809 else
1811 const char *hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME);
1812 if (hint)
1814 richloc.add_fixit_replace (hint);
1815 error_at_rich_loc (&richloc,
1816 "unknown type name %qE; did you mean %qs?",
1817 name, hint);
1819 else
1820 error_at (here, "unknown type name %qE", name);
1823 /* Parse declspecs normally to get a correct pointer type, but avoid
1824 a further "fails to be a type name" error. Refuse nested functions
1825 since it is not how the user likely wants us to recover. */
1826 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1827 c_parser_peek_token (parser)->keyword = RID_VOID;
1828 c_parser_peek_token (parser)->value = error_mark_node;
1829 fndef_ok = !nested;
1832 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1833 true, true, cla_nonabstract_decl);
1834 if (parser->error)
1836 c_parser_skip_to_end_of_block_or_statement (parser);
1837 return;
1839 if (nested && !specs->declspecs_seen_p)
1841 c_parser_error (parser, "expected declaration specifiers");
1842 c_parser_skip_to_end_of_block_or_statement (parser);
1843 return;
1846 finish_declspecs (specs);
1847 bool auto_type_p = specs->typespec_word == cts_auto_type;
1848 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1850 if (auto_type_p)
1851 error_at (here, "%<__auto_type%> in empty declaration");
1852 else if (specs->typespec_kind == ctsk_none
1853 && attribute_fallthrough_p (specs->attrs))
1855 if (fallthru_attr_p != NULL)
1856 *fallthru_attr_p = true;
1857 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1858 void_type_node, 0);
1859 add_stmt (fn);
1861 else if (empty_ok)
1862 shadow_tag (specs);
1863 else
1865 shadow_tag_warned (specs, 1);
1866 pedwarn (here, 0, "empty declaration");
1868 c_parser_consume_token (parser);
1869 if (oacc_routine_data)
1870 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1871 return;
1874 /* Provide better error recovery. Note that a type name here is usually
1875 better diagnosed as a redeclaration. */
1876 if (empty_ok
1877 && specs->typespec_kind == ctsk_tagdef
1878 && c_parser_next_token_starts_declspecs (parser)
1879 && !c_parser_next_token_is (parser, CPP_NAME))
1881 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1882 parser->error = false;
1883 shadow_tag_warned (specs, 1);
1884 return;
1886 else if (c_dialect_objc () && !auto_type_p)
1888 /* Prefix attributes are an error on method decls. */
1889 switch (c_parser_peek_token (parser)->type)
1891 case CPP_PLUS:
1892 case CPP_MINUS:
1893 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1894 return;
1895 if (specs->attrs)
1897 warning_at (c_parser_peek_token (parser)->location,
1898 OPT_Wattributes,
1899 "prefix attributes are ignored for methods");
1900 specs->attrs = NULL_TREE;
1902 if (fndef_ok)
1903 c_parser_objc_method_definition (parser);
1904 else
1905 c_parser_objc_methodproto (parser);
1906 return;
1907 break;
1908 default:
1909 break;
1911 /* This is where we parse 'attributes @interface ...',
1912 'attributes @implementation ...', 'attributes @protocol ...'
1913 (where attributes could be, for example, __attribute__
1914 ((deprecated)).
1916 switch (c_parser_peek_token (parser)->keyword)
1918 case RID_AT_INTERFACE:
1920 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1921 return;
1922 c_parser_objc_class_definition (parser, specs->attrs);
1923 return;
1925 break;
1926 case RID_AT_IMPLEMENTATION:
1928 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1929 return;
1930 if (specs->attrs)
1932 warning_at (c_parser_peek_token (parser)->location,
1933 OPT_Wattributes,
1934 "prefix attributes are ignored for implementations");
1935 specs->attrs = NULL_TREE;
1937 c_parser_objc_class_definition (parser, NULL_TREE);
1938 return;
1940 break;
1941 case RID_AT_PROTOCOL:
1943 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1944 return;
1945 c_parser_objc_protocol_definition (parser, specs->attrs);
1946 return;
1948 break;
1949 case RID_AT_ALIAS:
1950 case RID_AT_CLASS:
1951 case RID_AT_END:
1952 case RID_AT_PROPERTY:
1953 if (specs->attrs)
1955 c_parser_error (parser, "unexpected attribute");
1956 specs->attrs = NULL;
1958 break;
1959 default:
1960 break;
1963 else if (attribute_fallthrough_p (specs->attrs))
1964 warning_at (here, OPT_Wattributes,
1965 "%<fallthrough%> attribute not followed by %<;%>");
1967 pending_xref_error ();
1968 prefix_attrs = specs->attrs;
1969 all_prefix_attrs = prefix_attrs;
1970 specs->attrs = NULL_TREE;
1971 while (true)
1973 struct c_declarator *declarator;
1974 bool dummy = false;
1975 timevar_id_t tv;
1976 tree fnbody = NULL_TREE;
1977 /* Declaring either one or more declarators (in which case we
1978 should diagnose if there were no declaration specifiers) or a
1979 function definition (in which case the diagnostic for
1980 implicit int suffices). */
1981 declarator = c_parser_declarator (parser,
1982 specs->typespec_kind != ctsk_none,
1983 C_DTR_NORMAL, &dummy);
1984 if (declarator == NULL)
1986 if (omp_declare_simd_clauses.exists ()
1987 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1988 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1989 omp_declare_simd_clauses);
1990 if (oacc_routine_data)
1991 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1992 c_parser_skip_to_end_of_block_or_statement (parser);
1993 return;
1995 if (auto_type_p && declarator->kind != cdk_id)
1997 error_at (here,
1998 "%<__auto_type%> requires a plain identifier"
1999 " as declarator");
2000 c_parser_skip_to_end_of_block_or_statement (parser);
2001 return;
2003 if (c_parser_next_token_is (parser, CPP_EQ)
2004 || c_parser_next_token_is (parser, CPP_COMMA)
2005 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2006 || c_parser_next_token_is_keyword (parser, RID_ASM)
2007 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2008 || c_parser_next_token_is_keyword (parser, RID_IN))
2010 tree asm_name = NULL_TREE;
2011 tree postfix_attrs = NULL_TREE;
2012 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2014 diagnosed_no_specs = true;
2015 pedwarn (here, 0, "data definition has no type or storage class");
2017 /* Having seen a data definition, there cannot now be a
2018 function definition. */
2019 fndef_ok = false;
2020 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2021 asm_name = c_parser_simple_asm_expr (parser);
2022 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2024 postfix_attrs = c_parser_attributes (parser);
2025 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2027 /* This means there is an attribute specifier after
2028 the declarator in a function definition. Provide
2029 some more information for the user. */
2030 error_at (here, "attributes should be specified before the "
2031 "declarator in a function definition");
2032 c_parser_skip_to_end_of_block_or_statement (parser);
2033 return;
2036 if (c_parser_next_token_is (parser, CPP_EQ))
2038 tree d;
2039 struct c_expr init;
2040 location_t init_loc;
2041 c_parser_consume_token (parser);
2042 if (auto_type_p)
2044 init_loc = c_parser_peek_token (parser)->location;
2045 rich_location richloc (line_table, init_loc);
2046 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2047 /* A parameter is initialized, which is invalid. Don't
2048 attempt to instrument the initializer. */
2049 int flag_sanitize_save = flag_sanitize;
2050 if (nested && !empty_ok)
2051 flag_sanitize = 0;
2052 init = c_parser_expr_no_commas (parser, NULL);
2053 flag_sanitize = flag_sanitize_save;
2054 if (TREE_CODE (init.value) == COMPONENT_REF
2055 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2056 error_at (here,
2057 "%<__auto_type%> used with a bit-field"
2058 " initializer");
2059 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2060 tree init_type = TREE_TYPE (init.value);
2061 /* As with typeof, remove all qualifiers from atomic types. */
2062 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2063 init_type
2064 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2065 bool vm_type = variably_modified_type_p (init_type,
2066 NULL_TREE);
2067 if (vm_type)
2068 init.value = save_expr (init.value);
2069 finish_init ();
2070 specs->typespec_kind = ctsk_typeof;
2071 specs->locations[cdw_typedef] = init_loc;
2072 specs->typedef_p = true;
2073 specs->type = init_type;
2074 if (vm_type)
2076 bool maybe_const = true;
2077 tree type_expr = c_fully_fold (init.value, false,
2078 &maybe_const);
2079 specs->expr_const_operands &= maybe_const;
2080 if (specs->expr)
2081 specs->expr = build2 (COMPOUND_EXPR,
2082 TREE_TYPE (type_expr),
2083 specs->expr, type_expr);
2084 else
2085 specs->expr = type_expr;
2087 d = start_decl (declarator, specs, true,
2088 chainon (postfix_attrs, all_prefix_attrs));
2089 if (!d)
2090 d = error_mark_node;
2091 if (omp_declare_simd_clauses.exists ()
2092 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2093 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2094 omp_declare_simd_clauses);
2096 else
2098 /* The declaration of the variable is in effect while
2099 its initializer is parsed. */
2100 d = start_decl (declarator, specs, true,
2101 chainon (postfix_attrs, all_prefix_attrs));
2102 if (!d)
2103 d = error_mark_node;
2104 if (omp_declare_simd_clauses.exists ()
2105 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2106 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2107 omp_declare_simd_clauses);
2108 init_loc = c_parser_peek_token (parser)->location;
2109 rich_location richloc (line_table, init_loc);
2110 start_init (d, asm_name, global_bindings_p (), &richloc);
2111 /* A parameter is initialized, which is invalid. Don't
2112 attempt to instrument the initializer. */
2113 int flag_sanitize_save = flag_sanitize;
2114 if (TREE_CODE (d) == PARM_DECL)
2115 flag_sanitize = 0;
2116 init = c_parser_initializer (parser);
2117 flag_sanitize = flag_sanitize_save;
2118 finish_init ();
2120 if (oacc_routine_data)
2121 c_finish_oacc_routine (oacc_routine_data, d, false);
2122 if (d != error_mark_node)
2124 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2125 finish_decl (d, init_loc, init.value,
2126 init.original_type, asm_name);
2129 else
2131 if (auto_type_p)
2133 error_at (here,
2134 "%<__auto_type%> requires an initialized "
2135 "data declaration");
2136 c_parser_skip_to_end_of_block_or_statement (parser);
2137 return;
2139 tree d = start_decl (declarator, specs, false,
2140 chainon (postfix_attrs,
2141 all_prefix_attrs));
2142 if (d && TREE_CODE (d) == FUNCTION_DECL)
2143 if (declarator->kind == cdk_function)
2144 if (DECL_ARGUMENTS (d) == NULL_TREE)
2145 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2146 if (omp_declare_simd_clauses.exists ()
2147 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2149 tree parms = NULL_TREE;
2150 if (d && TREE_CODE (d) == FUNCTION_DECL)
2152 struct c_declarator *ce = declarator;
2153 while (ce != NULL)
2154 if (ce->kind == cdk_function)
2156 parms = ce->u.arg_info->parms;
2157 break;
2159 else
2160 ce = ce->declarator;
2162 if (parms)
2163 temp_store_parm_decls (d, parms);
2164 c_finish_omp_declare_simd (parser, d, parms,
2165 omp_declare_simd_clauses);
2166 if (parms)
2167 temp_pop_parm_decls ();
2169 if (oacc_routine_data)
2170 c_finish_oacc_routine (oacc_routine_data, d, false);
2171 if (d)
2172 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2173 NULL_TREE, asm_name);
2175 if (c_parser_next_token_is_keyword (parser, RID_IN))
2177 if (d)
2178 *objc_foreach_object_declaration = d;
2179 else
2180 *objc_foreach_object_declaration = error_mark_node;
2183 if (c_parser_next_token_is (parser, CPP_COMMA))
2185 if (auto_type_p)
2187 error_at (here,
2188 "%<__auto_type%> may only be used with"
2189 " a single declarator");
2190 c_parser_skip_to_end_of_block_or_statement (parser);
2191 return;
2193 c_parser_consume_token (parser);
2194 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2195 all_prefix_attrs = chainon (c_parser_attributes (parser),
2196 prefix_attrs);
2197 else
2198 all_prefix_attrs = prefix_attrs;
2199 continue;
2201 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2203 c_parser_consume_token (parser);
2204 return;
2206 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2208 /* This can only happen in Objective-C: we found the
2209 'in' that terminates the declaration inside an
2210 Objective-C foreach statement. Do not consume the
2211 token, so that the caller can use it to determine
2212 that this indeed is a foreach context. */
2213 return;
2215 else
2217 c_parser_error (parser, "expected %<,%> or %<;%>");
2218 c_parser_skip_to_end_of_block_or_statement (parser);
2219 return;
2222 else if (auto_type_p)
2224 error_at (here,
2225 "%<__auto_type%> requires an initialized data declaration");
2226 c_parser_skip_to_end_of_block_or_statement (parser);
2227 return;
2229 else if (!fndef_ok)
2231 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2232 "%<asm%> or %<__attribute__%>");
2233 c_parser_skip_to_end_of_block_or_statement (parser);
2234 return;
2236 /* Function definition (nested or otherwise). */
2237 if (nested)
2239 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2240 c_push_function_context ();
2242 if (!start_function (specs, declarator, all_prefix_attrs))
2244 /* This can appear in many cases looking nothing like a
2245 function definition, so we don't give a more specific
2246 error suggesting there was one. */
2247 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2248 "or %<__attribute__%>");
2249 if (nested)
2250 c_pop_function_context ();
2251 break;
2254 if (DECL_DECLARED_INLINE_P (current_function_decl))
2255 tv = TV_PARSE_INLINE;
2256 else
2257 tv = TV_PARSE_FUNC;
2258 auto_timevar at (g_timer, tv);
2260 /* Parse old-style parameter declarations. ??? Attributes are
2261 not allowed to start declaration specifiers here because of a
2262 syntax conflict between a function declaration with attribute
2263 suffix and a function definition with an attribute prefix on
2264 first old-style parameter declaration. Following the old
2265 parser, they are not accepted on subsequent old-style
2266 parameter declarations either. However, there is no
2267 ambiguity after the first declaration, nor indeed on the
2268 first as long as we don't allow postfix attributes after a
2269 declarator with a nonempty identifier list in a definition;
2270 and postfix attributes have never been accepted here in
2271 function definitions either. */
2272 while (c_parser_next_token_is_not (parser, CPP_EOF)
2273 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2274 c_parser_declaration_or_fndef (parser, false, false, false,
2275 true, false, NULL, vNULL);
2276 store_parm_decls ();
2277 if (omp_declare_simd_clauses.exists ()
2278 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2279 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2280 omp_declare_simd_clauses);
2281 if (oacc_routine_data)
2282 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2283 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2284 = c_parser_peek_token (parser)->location;
2286 /* If the definition was marked with __GIMPLE then parse the
2287 function body as GIMPLE. */
2288 if (specs->gimple_p)
2290 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2291 bool saved = in_late_binary_op;
2292 in_late_binary_op = true;
2293 c_parser_parse_gimple_body (parser);
2294 in_late_binary_op = saved;
2296 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2297 consuming the function body. */
2298 else if (specs->rtl_p)
2300 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2302 /* Normally, store_parm_decls sets next_is_function_body,
2303 anticipating a function body. We need a push_scope/pop_scope
2304 pair to flush out this state, or subsequent function parsing
2305 will go wrong. */
2306 push_scope ();
2307 pop_scope ();
2309 finish_function ();
2310 return;
2312 else
2314 fnbody = c_parser_compound_statement (parser);
2315 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2316 fnbody = expand_array_notation_exprs (fnbody);
2318 tree fndecl = current_function_decl;
2319 if (nested)
2321 tree decl = current_function_decl;
2322 /* Mark nested functions as needing static-chain initially.
2323 lower_nested_functions will recompute it but the
2324 DECL_STATIC_CHAIN flag is also used before that happens,
2325 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2326 DECL_STATIC_CHAIN (decl) = 1;
2327 add_stmt (fnbody);
2328 finish_function ();
2329 c_pop_function_context ();
2330 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2332 else
2334 if (fnbody)
2335 add_stmt (fnbody);
2336 finish_function ();
2338 /* Get rid of the empty stmt list for GIMPLE. */
2339 if (specs->gimple_p)
2340 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2342 break;
2346 /* Parse an asm-definition (asm() outside a function body). This is a
2347 GNU extension.
2349 asm-definition:
2350 simple-asm-expr ;
2353 static void
2354 c_parser_asm_definition (c_parser *parser)
2356 tree asm_str = c_parser_simple_asm_expr (parser);
2357 if (asm_str)
2358 symtab->finalize_toplevel_asm (asm_str);
2359 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2362 /* Parse a static assertion (C11 6.7.10).
2364 static_assert-declaration:
2365 static_assert-declaration-no-semi ;
2368 static void
2369 c_parser_static_assert_declaration (c_parser *parser)
2371 c_parser_static_assert_declaration_no_semi (parser);
2372 if (parser->error
2373 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2374 c_parser_skip_to_end_of_block_or_statement (parser);
2377 /* Parse a static assertion (C11 6.7.10), without the trailing
2378 semicolon.
2380 static_assert-declaration-no-semi:
2381 _Static_assert ( constant-expression , string-literal )
2384 static void
2385 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2387 location_t assert_loc, value_loc;
2388 tree value;
2389 tree string;
2391 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2392 assert_loc = c_parser_peek_token (parser)->location;
2393 if (flag_isoc99)
2394 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2395 "ISO C99 does not support %<_Static_assert%>");
2396 else
2397 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2398 "ISO C90 does not support %<_Static_assert%>");
2399 c_parser_consume_token (parser);
2400 matching_parens parens;
2401 if (!parens.require_open (parser))
2402 return;
2403 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2404 value = c_parser_expr_no_commas (parser, NULL).value;
2405 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2406 parser->lex_untranslated_string = true;
2407 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2409 parser->lex_untranslated_string = false;
2410 return;
2412 switch (c_parser_peek_token (parser)->type)
2414 case CPP_STRING:
2415 case CPP_STRING16:
2416 case CPP_STRING32:
2417 case CPP_WSTRING:
2418 case CPP_UTF8STRING:
2419 string = c_parser_peek_token (parser)->value;
2420 c_parser_consume_token (parser);
2421 parser->lex_untranslated_string = false;
2422 break;
2423 default:
2424 c_parser_error (parser, "expected string literal");
2425 parser->lex_untranslated_string = false;
2426 return;
2428 parens.require_close (parser);
2430 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2432 error_at (value_loc, "expression in static assertion is not an integer");
2433 return;
2435 if (TREE_CODE (value) != INTEGER_CST)
2437 value = c_fully_fold (value, false, NULL);
2438 /* Strip no-op conversions. */
2439 STRIP_TYPE_NOPS (value);
2440 if (TREE_CODE (value) == INTEGER_CST)
2441 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2442 "is not an integer constant expression");
2444 if (TREE_CODE (value) != INTEGER_CST)
2446 error_at (value_loc, "expression in static assertion is not constant");
2447 return;
2449 constant_expression_warning (value);
2450 if (integer_zerop (value))
2451 error_at (assert_loc, "static assertion failed: %E", string);
2454 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2455 6.7, C11 6.7), adding them to SPECS (which may already include some).
2456 Storage class specifiers are accepted iff SCSPEC_OK; type
2457 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2458 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2459 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2461 declaration-specifiers:
2462 storage-class-specifier declaration-specifiers[opt]
2463 type-specifier declaration-specifiers[opt]
2464 type-qualifier declaration-specifiers[opt]
2465 function-specifier declaration-specifiers[opt]
2466 alignment-specifier declaration-specifiers[opt]
2468 Function specifiers (inline) are from C99, and are currently
2469 handled as storage class specifiers, as is __thread. Alignment
2470 specifiers are from C11.
2472 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2473 storage-class-specifier:
2474 typedef
2475 extern
2476 static
2477 auto
2478 register
2479 _Thread_local
2481 (_Thread_local is new in C11.)
2483 C99 6.7.4, C11 6.7.4:
2484 function-specifier:
2485 inline
2486 _Noreturn
2488 (_Noreturn is new in C11.)
2490 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2491 type-specifier:
2492 void
2493 char
2494 short
2496 long
2497 float
2498 double
2499 signed
2500 unsigned
2501 _Bool
2502 _Complex
2503 [_Imaginary removed in C99 TC2]
2504 struct-or-union-specifier
2505 enum-specifier
2506 typedef-name
2507 atomic-type-specifier
2509 (_Bool and _Complex are new in C99.)
2510 (atomic-type-specifier is new in C11.)
2512 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2514 type-qualifier:
2515 const
2516 restrict
2517 volatile
2518 address-space-qualifier
2519 _Atomic
2521 (restrict is new in C99.)
2522 (_Atomic is new in C11.)
2524 GNU extensions:
2526 declaration-specifiers:
2527 attributes declaration-specifiers[opt]
2529 type-qualifier:
2530 address-space
2532 address-space:
2533 identifier recognized by the target
2535 storage-class-specifier:
2536 __thread
2538 type-specifier:
2539 typeof-specifier
2540 __auto_type
2541 __intN
2542 _Decimal32
2543 _Decimal64
2544 _Decimal128
2545 _Fract
2546 _Accum
2547 _Sat
2549 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2550 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2552 atomic-type-specifier
2553 _Atomic ( type-name )
2555 Objective-C:
2557 type-specifier:
2558 class-name objc-protocol-refs[opt]
2559 typedef-name objc-protocol-refs
2560 objc-protocol-refs
2563 void
2564 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2565 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2566 bool alignspec_ok, bool auto_type_ok,
2567 enum c_lookahead_kind la)
2569 bool attrs_ok = start_attr_ok;
2570 bool seen_type = specs->typespec_kind != ctsk_none;
2572 if (!typespec_ok)
2573 gcc_assert (la == cla_prefer_id);
2575 while (c_parser_next_token_is (parser, CPP_NAME)
2576 || c_parser_next_token_is (parser, CPP_KEYWORD)
2577 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2579 struct c_typespec t;
2580 tree attrs;
2581 tree align;
2582 location_t loc = c_parser_peek_token (parser)->location;
2584 /* If we cannot accept a type, exit if the next token must start
2585 one. Also, if we already have seen a tagged definition,
2586 a typename would be an error anyway and likely the user
2587 has simply forgotten a semicolon, so we exit. */
2588 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2589 && c_parser_next_tokens_start_typename (parser, la)
2590 && !c_parser_next_token_is_qualifier (parser))
2591 break;
2593 if (c_parser_next_token_is (parser, CPP_NAME))
2595 c_token *name_token = c_parser_peek_token (parser);
2596 tree value = name_token->value;
2597 c_id_kind kind = name_token->id_kind;
2599 if (kind == C_ID_ADDRSPACE)
2601 addr_space_t as
2602 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2603 declspecs_add_addrspace (name_token->location, specs, as);
2604 c_parser_consume_token (parser);
2605 attrs_ok = true;
2606 continue;
2609 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2611 /* If we cannot accept a type, and the next token must start one,
2612 exit. Do the same if we already have seen a tagged definition,
2613 since it would be an error anyway and likely the user has simply
2614 forgotten a semicolon. */
2615 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2616 break;
2618 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2619 a C_ID_CLASSNAME. */
2620 c_parser_consume_token (parser);
2621 seen_type = true;
2622 attrs_ok = true;
2623 if (kind == C_ID_ID)
2625 error_at (loc, "unknown type name %qE", value);
2626 t.kind = ctsk_typedef;
2627 t.spec = error_mark_node;
2629 else if (kind == C_ID_TYPENAME
2630 && (!c_dialect_objc ()
2631 || c_parser_next_token_is_not (parser, CPP_LESS)))
2633 t.kind = ctsk_typedef;
2634 /* For a typedef name, record the meaning, not the name.
2635 In case of 'foo foo, bar;'. */
2636 t.spec = lookup_name (value);
2638 else
2640 tree proto = NULL_TREE;
2641 gcc_assert (c_dialect_objc ());
2642 t.kind = ctsk_objc;
2643 if (c_parser_next_token_is (parser, CPP_LESS))
2644 proto = c_parser_objc_protocol_refs (parser);
2645 t.spec = objc_get_protocol_qualified_type (value, proto);
2647 t.expr = NULL_TREE;
2648 t.expr_const_operands = true;
2649 declspecs_add_type (name_token->location, specs, t);
2650 continue;
2652 if (c_parser_next_token_is (parser, CPP_LESS))
2654 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2655 nisse@lysator.liu.se. */
2656 tree proto;
2657 gcc_assert (c_dialect_objc ());
2658 if (!typespec_ok || seen_type)
2659 break;
2660 proto = c_parser_objc_protocol_refs (parser);
2661 t.kind = ctsk_objc;
2662 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2663 t.expr = NULL_TREE;
2664 t.expr_const_operands = true;
2665 declspecs_add_type (loc, specs, t);
2666 continue;
2668 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2669 switch (c_parser_peek_token (parser)->keyword)
2671 case RID_STATIC:
2672 case RID_EXTERN:
2673 case RID_REGISTER:
2674 case RID_TYPEDEF:
2675 case RID_INLINE:
2676 case RID_NORETURN:
2677 case RID_AUTO:
2678 case RID_THREAD:
2679 if (!scspec_ok)
2680 goto out;
2681 attrs_ok = true;
2682 /* TODO: Distinguish between function specifiers (inline, noreturn)
2683 and storage class specifiers, either here or in
2684 declspecs_add_scspec. */
2685 declspecs_add_scspec (loc, specs,
2686 c_parser_peek_token (parser)->value);
2687 c_parser_consume_token (parser);
2688 break;
2689 case RID_AUTO_TYPE:
2690 if (!auto_type_ok)
2691 goto out;
2692 /* Fall through. */
2693 case RID_UNSIGNED:
2694 case RID_LONG:
2695 case RID_SHORT:
2696 case RID_SIGNED:
2697 case RID_COMPLEX:
2698 case RID_INT:
2699 case RID_CHAR:
2700 case RID_FLOAT:
2701 case RID_DOUBLE:
2702 case RID_VOID:
2703 case RID_DFLOAT32:
2704 case RID_DFLOAT64:
2705 case RID_DFLOAT128:
2706 CASE_RID_FLOATN_NX:
2707 case RID_BOOL:
2708 case RID_FRACT:
2709 case RID_ACCUM:
2710 case RID_SAT:
2711 case RID_INT_N_0:
2712 case RID_INT_N_1:
2713 case RID_INT_N_2:
2714 case RID_INT_N_3:
2715 if (!typespec_ok)
2716 goto out;
2717 attrs_ok = true;
2718 seen_type = true;
2719 if (c_dialect_objc ())
2720 parser->objc_need_raw_identifier = true;
2721 t.kind = ctsk_resword;
2722 t.spec = c_parser_peek_token (parser)->value;
2723 t.expr = NULL_TREE;
2724 t.expr_const_operands = true;
2725 declspecs_add_type (loc, specs, t);
2726 c_parser_consume_token (parser);
2727 break;
2728 case RID_ENUM:
2729 if (!typespec_ok)
2730 goto out;
2731 attrs_ok = true;
2732 seen_type = true;
2733 t = c_parser_enum_specifier (parser);
2734 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2735 declspecs_add_type (loc, specs, t);
2736 break;
2737 case RID_STRUCT:
2738 case RID_UNION:
2739 if (!typespec_ok)
2740 goto out;
2741 attrs_ok = true;
2742 seen_type = true;
2743 t = c_parser_struct_or_union_specifier (parser);
2744 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2745 declspecs_add_type (loc, specs, t);
2746 break;
2747 case RID_TYPEOF:
2748 /* ??? The old parser rejected typeof after other type
2749 specifiers, but is a syntax error the best way of
2750 handling this? */
2751 if (!typespec_ok || seen_type)
2752 goto out;
2753 attrs_ok = true;
2754 seen_type = true;
2755 t = c_parser_typeof_specifier (parser);
2756 declspecs_add_type (loc, specs, t);
2757 break;
2758 case RID_ATOMIC:
2759 /* C parser handling of Objective-C constructs needs
2760 checking for correct lvalue-to-rvalue conversions, and
2761 the code in build_modify_expr handling various
2762 Objective-C cases, and that in build_unary_op handling
2763 Objective-C cases for increment / decrement, also needs
2764 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2765 and objc_types_are_equivalent may also need updates. */
2766 if (c_dialect_objc ())
2767 sorry ("%<_Atomic%> in Objective-C");
2768 if (flag_isoc99)
2769 pedwarn_c99 (loc, OPT_Wpedantic,
2770 "ISO C99 does not support the %<_Atomic%> qualifier");
2771 else
2772 pedwarn_c99 (loc, OPT_Wpedantic,
2773 "ISO C90 does not support the %<_Atomic%> qualifier");
2774 attrs_ok = true;
2775 tree value;
2776 value = c_parser_peek_token (parser)->value;
2777 c_parser_consume_token (parser);
2778 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2780 /* _Atomic ( type-name ). */
2781 seen_type = true;
2782 c_parser_consume_token (parser);
2783 struct c_type_name *type = c_parser_type_name (parser);
2784 t.kind = ctsk_typeof;
2785 t.spec = error_mark_node;
2786 t.expr = NULL_TREE;
2787 t.expr_const_operands = true;
2788 if (type != NULL)
2789 t.spec = groktypename (type, &t.expr,
2790 &t.expr_const_operands);
2791 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2792 "expected %<)%>");
2793 if (t.spec != error_mark_node)
2795 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2796 error_at (loc, "%<_Atomic%>-qualified array type");
2797 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2798 error_at (loc, "%<_Atomic%>-qualified function type");
2799 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2800 error_at (loc, "%<_Atomic%> applied to a qualified type");
2801 else
2802 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2804 declspecs_add_type (loc, specs, t);
2806 else
2807 declspecs_add_qual (loc, specs, value);
2808 break;
2809 case RID_CONST:
2810 case RID_VOLATILE:
2811 case RID_RESTRICT:
2812 attrs_ok = true;
2813 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2814 c_parser_consume_token (parser);
2815 break;
2816 case RID_ATTRIBUTE:
2817 if (!attrs_ok)
2818 goto out;
2819 attrs = c_parser_attributes (parser);
2820 declspecs_add_attrs (loc, specs, attrs);
2821 break;
2822 case RID_ALIGNAS:
2823 if (!alignspec_ok)
2824 goto out;
2825 align = c_parser_alignas_specifier (parser);
2826 declspecs_add_alignas (loc, specs, align);
2827 break;
2828 case RID_GIMPLE:
2829 if (! flag_gimple)
2830 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2831 c_parser_consume_token (parser);
2832 specs->gimple_p = true;
2833 specs->locations[cdw_gimple] = loc;
2834 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2835 break;
2836 case RID_RTL:
2837 c_parser_consume_token (parser);
2838 specs->rtl_p = true;
2839 specs->locations[cdw_rtl] = loc;
2840 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2841 break;
2842 default:
2843 goto out;
2846 out: ;
2849 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2851 enum-specifier:
2852 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2853 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2854 enum attributes[opt] identifier
2856 The form with trailing comma is new in C99. The forms with
2857 attributes are GNU extensions. In GNU C, we accept any expression
2858 without commas in the syntax (assignment expressions, not just
2859 conditional expressions); assignment expressions will be diagnosed
2860 as non-constant.
2862 enumerator-list:
2863 enumerator
2864 enumerator-list , enumerator
2866 enumerator:
2867 enumeration-constant
2868 enumeration-constant = constant-expression
2870 GNU Extensions:
2872 enumerator:
2873 enumeration-constant attributes[opt]
2874 enumeration-constant attributes[opt] = constant-expression
2878 static struct c_typespec
2879 c_parser_enum_specifier (c_parser *parser)
2881 struct c_typespec ret;
2882 tree attrs;
2883 tree ident = NULL_TREE;
2884 location_t enum_loc;
2885 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2886 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2887 c_parser_consume_token (parser);
2888 attrs = c_parser_attributes (parser);
2889 enum_loc = c_parser_peek_token (parser)->location;
2890 /* Set the location in case we create a decl now. */
2891 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2892 if (c_parser_next_token_is (parser, CPP_NAME))
2894 ident = c_parser_peek_token (parser)->value;
2895 ident_loc = c_parser_peek_token (parser)->location;
2896 enum_loc = ident_loc;
2897 c_parser_consume_token (parser);
2899 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2901 /* Parse an enum definition. */
2902 struct c_enum_contents the_enum;
2903 tree type;
2904 tree postfix_attrs;
2905 /* We chain the enumerators in reverse order, then put them in
2906 forward order at the end. */
2907 tree values;
2908 timevar_push (TV_PARSE_ENUM);
2909 type = start_enum (enum_loc, &the_enum, ident);
2910 values = NULL_TREE;
2911 c_parser_consume_token (parser);
2912 while (true)
2914 tree enum_id;
2915 tree enum_value;
2916 tree enum_decl;
2917 bool seen_comma;
2918 c_token *token;
2919 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2920 location_t decl_loc, value_loc;
2921 if (c_parser_next_token_is_not (parser, CPP_NAME))
2923 /* Give a nicer error for "enum {}". */
2924 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2925 && !parser->error)
2927 error_at (c_parser_peek_token (parser)->location,
2928 "empty enum is invalid");
2929 parser->error = true;
2931 else
2932 c_parser_error (parser, "expected identifier");
2933 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2934 values = error_mark_node;
2935 break;
2937 token = c_parser_peek_token (parser);
2938 enum_id = token->value;
2939 /* Set the location in case we create a decl now. */
2940 c_parser_set_source_position_from_token (token);
2941 decl_loc = value_loc = token->location;
2942 c_parser_consume_token (parser);
2943 /* Parse any specified attributes. */
2944 tree enum_attrs = c_parser_attributes (parser);
2945 if (c_parser_next_token_is (parser, CPP_EQ))
2947 c_parser_consume_token (parser);
2948 value_loc = c_parser_peek_token (parser)->location;
2949 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2951 else
2952 enum_value = NULL_TREE;
2953 enum_decl = build_enumerator (decl_loc, value_loc,
2954 &the_enum, enum_id, enum_value);
2955 if (enum_attrs)
2956 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2957 TREE_CHAIN (enum_decl) = values;
2958 values = enum_decl;
2959 seen_comma = false;
2960 if (c_parser_next_token_is (parser, CPP_COMMA))
2962 comma_loc = c_parser_peek_token (parser)->location;
2963 seen_comma = true;
2964 c_parser_consume_token (parser);
2966 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2968 if (seen_comma)
2969 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2970 "comma at end of enumerator list");
2971 c_parser_consume_token (parser);
2972 break;
2974 if (!seen_comma)
2976 c_parser_error (parser, "expected %<,%> or %<}%>");
2977 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2978 values = error_mark_node;
2979 break;
2982 postfix_attrs = c_parser_attributes (parser);
2983 ret.spec = finish_enum (type, nreverse (values),
2984 chainon (attrs, postfix_attrs));
2985 ret.kind = ctsk_tagdef;
2986 ret.expr = NULL_TREE;
2987 ret.expr_const_operands = true;
2988 timevar_pop (TV_PARSE_ENUM);
2989 return ret;
2991 else if (!ident)
2993 c_parser_error (parser, "expected %<{%>");
2994 ret.spec = error_mark_node;
2995 ret.kind = ctsk_tagref;
2996 ret.expr = NULL_TREE;
2997 ret.expr_const_operands = true;
2998 return ret;
3000 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3001 /* In ISO C, enumerated types can be referred to only if already
3002 defined. */
3003 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3005 gcc_assert (ident);
3006 pedwarn (enum_loc, OPT_Wpedantic,
3007 "ISO C forbids forward references to %<enum%> types");
3009 return ret;
3012 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3014 struct-or-union-specifier:
3015 struct-or-union attributes[opt] identifier[opt]
3016 { struct-contents } attributes[opt]
3017 struct-or-union attributes[opt] identifier
3019 struct-contents:
3020 struct-declaration-list
3022 struct-declaration-list:
3023 struct-declaration ;
3024 struct-declaration-list struct-declaration ;
3026 GNU extensions:
3028 struct-contents:
3029 empty
3030 struct-declaration
3031 struct-declaration-list struct-declaration
3033 struct-declaration-list:
3034 struct-declaration-list ;
3037 (Note that in the syntax here, unlike that in ISO C, the semicolons
3038 are included here rather than in struct-declaration, in order to
3039 describe the syntax with extra semicolons and missing semicolon at
3040 end.)
3042 Objective-C:
3044 struct-declaration-list:
3045 @defs ( class-name )
3047 (Note this does not include a trailing semicolon, but can be
3048 followed by further declarations, and gets a pedwarn-if-pedantic
3049 when followed by a semicolon.) */
3051 static struct c_typespec
3052 c_parser_struct_or_union_specifier (c_parser *parser)
3054 struct c_typespec ret;
3055 tree attrs;
3056 tree ident = NULL_TREE;
3057 location_t struct_loc;
3058 location_t ident_loc = UNKNOWN_LOCATION;
3059 enum tree_code code;
3060 switch (c_parser_peek_token (parser)->keyword)
3062 case RID_STRUCT:
3063 code = RECORD_TYPE;
3064 break;
3065 case RID_UNION:
3066 code = UNION_TYPE;
3067 break;
3068 default:
3069 gcc_unreachable ();
3071 struct_loc = c_parser_peek_token (parser)->location;
3072 c_parser_consume_token (parser);
3073 attrs = c_parser_attributes (parser);
3075 /* Set the location in case we create a decl now. */
3076 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3078 if (c_parser_next_token_is (parser, CPP_NAME))
3080 ident = c_parser_peek_token (parser)->value;
3081 ident_loc = c_parser_peek_token (parser)->location;
3082 struct_loc = ident_loc;
3083 c_parser_consume_token (parser);
3085 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3087 /* Parse a struct or union definition. Start the scope of the
3088 tag before parsing components. */
3089 struct c_struct_parse_info *struct_info;
3090 tree type = start_struct (struct_loc, code, ident, &struct_info);
3091 tree postfix_attrs;
3092 /* We chain the components in reverse order, then put them in
3093 forward order at the end. Each struct-declaration may
3094 declare multiple components (comma-separated), so we must use
3095 chainon to join them, although when parsing each
3096 struct-declaration we can use TREE_CHAIN directly.
3098 The theory behind all this is that there will be more
3099 semicolon separated fields than comma separated fields, and
3100 so we'll be minimizing the number of node traversals required
3101 by chainon. */
3102 tree contents;
3103 timevar_push (TV_PARSE_STRUCT);
3104 contents = NULL_TREE;
3105 c_parser_consume_token (parser);
3106 /* Handle the Objective-C @defs construct,
3107 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3108 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3110 tree name;
3111 gcc_assert (c_dialect_objc ());
3112 c_parser_consume_token (parser);
3113 matching_parens parens;
3114 if (!parens.require_open (parser))
3115 goto end_at_defs;
3116 if (c_parser_next_token_is (parser, CPP_NAME)
3117 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3119 name = c_parser_peek_token (parser)->value;
3120 c_parser_consume_token (parser);
3122 else
3124 c_parser_error (parser, "expected class name");
3125 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3126 goto end_at_defs;
3128 parens.skip_until_found_close (parser);
3129 contents = nreverse (objc_get_class_ivars (name));
3131 end_at_defs:
3132 /* Parse the struct-declarations and semicolons. Problems with
3133 semicolons are diagnosed here; empty structures are diagnosed
3134 elsewhere. */
3135 while (true)
3137 tree decls;
3138 /* Parse any stray semicolon. */
3139 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3141 location_t semicolon_loc
3142 = c_parser_peek_token (parser)->location;
3143 gcc_rich_location richloc (semicolon_loc);
3144 richloc.add_fixit_remove ();
3145 pedwarn_at_rich_loc
3146 (&richloc, OPT_Wpedantic,
3147 "extra semicolon in struct or union specified");
3148 c_parser_consume_token (parser);
3149 continue;
3151 /* Stop if at the end of the struct or union contents. */
3152 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3154 c_parser_consume_token (parser);
3155 break;
3157 /* Accept #pragmas at struct scope. */
3158 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3160 c_parser_pragma (parser, pragma_struct, NULL);
3161 continue;
3163 /* Parse some comma-separated declarations, but not the
3164 trailing semicolon if any. */
3165 decls = c_parser_struct_declaration (parser);
3166 contents = chainon (decls, contents);
3167 /* If no semicolon follows, either we have a parse error or
3168 are at the end of the struct or union and should
3169 pedwarn. */
3170 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3171 c_parser_consume_token (parser);
3172 else
3174 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3175 pedwarn (c_parser_peek_token (parser)->location, 0,
3176 "no semicolon at end of struct or union");
3177 else if (parser->error
3178 || !c_parser_next_token_starts_declspecs (parser))
3180 c_parser_error (parser, "expected %<;%>");
3181 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3182 break;
3185 /* If we come here, we have already emitted an error
3186 for an expected `;', identifier or `(', and we also
3187 recovered already. Go on with the next field. */
3190 postfix_attrs = c_parser_attributes (parser);
3191 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3192 chainon (attrs, postfix_attrs), struct_info);
3193 ret.kind = ctsk_tagdef;
3194 ret.expr = NULL_TREE;
3195 ret.expr_const_operands = true;
3196 timevar_pop (TV_PARSE_STRUCT);
3197 return ret;
3199 else if (!ident)
3201 c_parser_error (parser, "expected %<{%>");
3202 ret.spec = error_mark_node;
3203 ret.kind = ctsk_tagref;
3204 ret.expr = NULL_TREE;
3205 ret.expr_const_operands = true;
3206 return ret;
3208 ret = parser_xref_tag (ident_loc, code, ident);
3209 return ret;
3212 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3213 *without* the trailing semicolon.
3215 struct-declaration:
3216 specifier-qualifier-list struct-declarator-list
3217 static_assert-declaration-no-semi
3219 specifier-qualifier-list:
3220 type-specifier specifier-qualifier-list[opt]
3221 type-qualifier specifier-qualifier-list[opt]
3222 attributes specifier-qualifier-list[opt]
3224 struct-declarator-list:
3225 struct-declarator
3226 struct-declarator-list , attributes[opt] struct-declarator
3228 struct-declarator:
3229 declarator attributes[opt]
3230 declarator[opt] : constant-expression attributes[opt]
3232 GNU extensions:
3234 struct-declaration:
3235 __extension__ struct-declaration
3236 specifier-qualifier-list
3238 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3239 of attributes where shown is a GNU extension. In GNU C, we accept
3240 any expression without commas in the syntax (assignment
3241 expressions, not just conditional expressions); assignment
3242 expressions will be diagnosed as non-constant. */
3244 static tree
3245 c_parser_struct_declaration (c_parser *parser)
3247 struct c_declspecs *specs;
3248 tree prefix_attrs;
3249 tree all_prefix_attrs;
3250 tree decls;
3251 location_t decl_loc;
3252 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3254 int ext;
3255 tree decl;
3256 ext = disable_extension_diagnostics ();
3257 c_parser_consume_token (parser);
3258 decl = c_parser_struct_declaration (parser);
3259 restore_extension_diagnostics (ext);
3260 return decl;
3262 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3264 c_parser_static_assert_declaration_no_semi (parser);
3265 return NULL_TREE;
3267 specs = build_null_declspecs ();
3268 decl_loc = c_parser_peek_token (parser)->location;
3269 /* Strictly by the standard, we shouldn't allow _Alignas here,
3270 but it appears to have been intended to allow it there, so
3271 we're keeping it as it is until WG14 reaches a conclusion
3272 of N1731.
3273 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3274 c_parser_declspecs (parser, specs, false, true, true,
3275 true, false, cla_nonabstract_decl);
3276 if (parser->error)
3277 return NULL_TREE;
3278 if (!specs->declspecs_seen_p)
3280 c_parser_error (parser, "expected specifier-qualifier-list");
3281 return NULL_TREE;
3283 finish_declspecs (specs);
3284 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3285 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3287 tree ret;
3288 if (specs->typespec_kind == ctsk_none)
3290 pedwarn (decl_loc, OPT_Wpedantic,
3291 "ISO C forbids member declarations with no members");
3292 shadow_tag_warned (specs, pedantic);
3293 ret = NULL_TREE;
3295 else
3297 /* Support for unnamed structs or unions as members of
3298 structs or unions (which is [a] useful and [b] supports
3299 MS P-SDK). */
3300 tree attrs = NULL;
3302 ret = grokfield (c_parser_peek_token (parser)->location,
3303 build_id_declarator (NULL_TREE), specs,
3304 NULL_TREE, &attrs);
3305 if (ret)
3306 decl_attributes (&ret, attrs, 0);
3308 return ret;
3311 /* Provide better error recovery. Note that a type name here is valid,
3312 and will be treated as a field name. */
3313 if (specs->typespec_kind == ctsk_tagdef
3314 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3315 && c_parser_next_token_starts_declspecs (parser)
3316 && !c_parser_next_token_is (parser, CPP_NAME))
3318 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3319 parser->error = false;
3320 return NULL_TREE;
3323 pending_xref_error ();
3324 prefix_attrs = specs->attrs;
3325 all_prefix_attrs = prefix_attrs;
3326 specs->attrs = NULL_TREE;
3327 decls = NULL_TREE;
3328 while (true)
3330 /* Declaring one or more declarators or un-named bit-fields. */
3331 struct c_declarator *declarator;
3332 bool dummy = false;
3333 if (c_parser_next_token_is (parser, CPP_COLON))
3334 declarator = build_id_declarator (NULL_TREE);
3335 else
3336 declarator = c_parser_declarator (parser,
3337 specs->typespec_kind != ctsk_none,
3338 C_DTR_NORMAL, &dummy);
3339 if (declarator == NULL)
3341 c_parser_skip_to_end_of_block_or_statement (parser);
3342 break;
3344 if (c_parser_next_token_is (parser, CPP_COLON)
3345 || c_parser_next_token_is (parser, CPP_COMMA)
3346 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3347 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3348 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3350 tree postfix_attrs = NULL_TREE;
3351 tree width = NULL_TREE;
3352 tree d;
3353 if (c_parser_next_token_is (parser, CPP_COLON))
3355 c_parser_consume_token (parser);
3356 width = c_parser_expr_no_commas (parser, NULL).value;
3358 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3359 postfix_attrs = c_parser_attributes (parser);
3360 d = grokfield (c_parser_peek_token (parser)->location,
3361 declarator, specs, width, &all_prefix_attrs);
3362 decl_attributes (&d, chainon (postfix_attrs,
3363 all_prefix_attrs), 0);
3364 DECL_CHAIN (d) = decls;
3365 decls = d;
3366 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3367 all_prefix_attrs = chainon (c_parser_attributes (parser),
3368 prefix_attrs);
3369 else
3370 all_prefix_attrs = prefix_attrs;
3371 if (c_parser_next_token_is (parser, CPP_COMMA))
3372 c_parser_consume_token (parser);
3373 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3374 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3376 /* Semicolon consumed in caller. */
3377 break;
3379 else
3381 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3382 break;
3385 else
3387 c_parser_error (parser,
3388 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3389 "%<__attribute__%>");
3390 break;
3393 return decls;
3396 /* Parse a typeof specifier (a GNU extension).
3398 typeof-specifier:
3399 typeof ( expression )
3400 typeof ( type-name )
3403 static struct c_typespec
3404 c_parser_typeof_specifier (c_parser *parser)
3406 struct c_typespec ret;
3407 ret.kind = ctsk_typeof;
3408 ret.spec = error_mark_node;
3409 ret.expr = NULL_TREE;
3410 ret.expr_const_operands = true;
3411 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3412 c_parser_consume_token (parser);
3413 c_inhibit_evaluation_warnings++;
3414 in_typeof++;
3415 matching_parens parens;
3416 if (!parens.require_open (parser))
3418 c_inhibit_evaluation_warnings--;
3419 in_typeof--;
3420 return ret;
3422 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3424 struct c_type_name *type = c_parser_type_name (parser);
3425 c_inhibit_evaluation_warnings--;
3426 in_typeof--;
3427 if (type != NULL)
3429 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3430 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3433 else
3435 bool was_vm;
3436 location_t here = c_parser_peek_token (parser)->location;
3437 struct c_expr expr = c_parser_expression (parser);
3438 c_inhibit_evaluation_warnings--;
3439 in_typeof--;
3440 if (TREE_CODE (expr.value) == COMPONENT_REF
3441 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3442 error_at (here, "%<typeof%> applied to a bit-field");
3443 mark_exp_read (expr.value);
3444 ret.spec = TREE_TYPE (expr.value);
3445 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3446 /* This is returned with the type so that when the type is
3447 evaluated, this can be evaluated. */
3448 if (was_vm)
3449 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3450 pop_maybe_used (was_vm);
3451 /* For use in macros such as those in <stdatomic.h>, remove all
3452 qualifiers from atomic types. (const can be an issue for more macros
3453 using typeof than just the <stdatomic.h> ones.) */
3454 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3455 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3457 parens.skip_until_found_close (parser);
3458 return ret;
3461 /* Parse an alignment-specifier.
3463 C11 6.7.5:
3465 alignment-specifier:
3466 _Alignas ( type-name )
3467 _Alignas ( constant-expression )
3470 static tree
3471 c_parser_alignas_specifier (c_parser * parser)
3473 tree ret = error_mark_node;
3474 location_t loc = c_parser_peek_token (parser)->location;
3475 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3476 c_parser_consume_token (parser);
3477 if (flag_isoc99)
3478 pedwarn_c99 (loc, OPT_Wpedantic,
3479 "ISO C99 does not support %<_Alignas%>");
3480 else
3481 pedwarn_c99 (loc, OPT_Wpedantic,
3482 "ISO C90 does not support %<_Alignas%>");
3483 matching_parens parens;
3484 if (!parens.require_open (parser))
3485 return ret;
3486 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3488 struct c_type_name *type = c_parser_type_name (parser);
3489 if (type != NULL)
3490 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3491 false, true, 1);
3493 else
3494 ret = c_parser_expr_no_commas (parser, NULL).value;
3495 parens.skip_until_found_close (parser);
3496 return ret;
3499 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3500 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3501 a typedef name may be redeclared; otherwise it may not. KIND
3502 indicates which kind of declarator is wanted. Returns a valid
3503 declarator except in the case of a syntax error in which case NULL is
3504 returned. *SEEN_ID is set to true if an identifier being declared is
3505 seen; this is used to diagnose bad forms of abstract array declarators
3506 and to determine whether an identifier list is syntactically permitted.
3508 declarator:
3509 pointer[opt] direct-declarator
3511 direct-declarator:
3512 identifier
3513 ( attributes[opt] declarator )
3514 direct-declarator array-declarator
3515 direct-declarator ( parameter-type-list )
3516 direct-declarator ( identifier-list[opt] )
3518 pointer:
3519 * type-qualifier-list[opt]
3520 * type-qualifier-list[opt] pointer
3522 type-qualifier-list:
3523 type-qualifier
3524 attributes
3525 type-qualifier-list type-qualifier
3526 type-qualifier-list attributes
3528 array-declarator:
3529 [ type-qualifier-list[opt] assignment-expression[opt] ]
3530 [ static type-qualifier-list[opt] assignment-expression ]
3531 [ type-qualifier-list static assignment-expression ]
3532 [ type-qualifier-list[opt] * ]
3534 parameter-type-list:
3535 parameter-list
3536 parameter-list , ...
3538 parameter-list:
3539 parameter-declaration
3540 parameter-list , parameter-declaration
3542 parameter-declaration:
3543 declaration-specifiers declarator attributes[opt]
3544 declaration-specifiers abstract-declarator[opt] attributes[opt]
3546 identifier-list:
3547 identifier
3548 identifier-list , identifier
3550 abstract-declarator:
3551 pointer
3552 pointer[opt] direct-abstract-declarator
3554 direct-abstract-declarator:
3555 ( attributes[opt] abstract-declarator )
3556 direct-abstract-declarator[opt] array-declarator
3557 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3559 GNU extensions:
3561 direct-declarator:
3562 direct-declarator ( parameter-forward-declarations
3563 parameter-type-list[opt] )
3565 direct-abstract-declarator:
3566 direct-abstract-declarator[opt] ( parameter-forward-declarations
3567 parameter-type-list[opt] )
3569 parameter-forward-declarations:
3570 parameter-list ;
3571 parameter-forward-declarations parameter-list ;
3573 The uses of attributes shown above are GNU extensions.
3575 Some forms of array declarator are not included in C99 in the
3576 syntax for abstract declarators; these are disallowed elsewhere.
3577 This may be a defect (DR#289).
3579 This function also accepts an omitted abstract declarator as being
3580 an abstract declarator, although not part of the formal syntax. */
3582 struct c_declarator *
3583 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3584 bool *seen_id)
3586 /* Parse any initial pointer part. */
3587 if (c_parser_next_token_is (parser, CPP_MULT))
3589 struct c_declspecs *quals_attrs = build_null_declspecs ();
3590 struct c_declarator *inner;
3591 c_parser_consume_token (parser);
3592 c_parser_declspecs (parser, quals_attrs, false, false, true,
3593 false, false, cla_prefer_id);
3594 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3595 if (inner == NULL)
3596 return NULL;
3597 else
3598 return make_pointer_declarator (quals_attrs, inner);
3600 /* Now we have a direct declarator, direct abstract declarator or
3601 nothing (which counts as a direct abstract declarator here). */
3602 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3605 /* Parse a direct declarator or direct abstract declarator; arguments
3606 as c_parser_declarator. */
3608 static struct c_declarator *
3609 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3610 bool *seen_id)
3612 /* The direct declarator must start with an identifier (possibly
3613 omitted) or a parenthesized declarator (possibly abstract). In
3614 an ordinary declarator, initial parentheses must start a
3615 parenthesized declarator. In an abstract declarator or parameter
3616 declarator, they could start a parenthesized declarator or a
3617 parameter list. To tell which, the open parenthesis and any
3618 following attributes must be read. If a declaration specifier
3619 follows, then it is a parameter list; if the specifier is a
3620 typedef name, there might be an ambiguity about redeclaring it,
3621 which is resolved in the direction of treating it as a typedef
3622 name. If a close parenthesis follows, it is also an empty
3623 parameter list, as the syntax does not permit empty abstract
3624 declarators. Otherwise, it is a parenthesized declarator (in
3625 which case the analysis may be repeated inside it, recursively).
3627 ??? There is an ambiguity in a parameter declaration "int
3628 (__attribute__((foo)) x)", where x is not a typedef name: it
3629 could be an abstract declarator for a function, or declare x with
3630 parentheses. The proper resolution of this ambiguity needs
3631 documenting. At present we follow an accident of the old
3632 parser's implementation, whereby the first parameter must have
3633 some declaration specifiers other than just attributes. Thus as
3634 a parameter declaration it is treated as a parenthesized
3635 parameter named x, and as an abstract declarator it is
3636 rejected.
3638 ??? Also following the old parser, attributes inside an empty
3639 parameter list are ignored, making it a list not yielding a
3640 prototype, rather than giving an error or making it have one
3641 parameter with implicit type int.
3643 ??? Also following the old parser, typedef names may be
3644 redeclared in declarators, but not Objective-C class names. */
3646 if (kind != C_DTR_ABSTRACT
3647 && c_parser_next_token_is (parser, CPP_NAME)
3648 && ((type_seen_p
3649 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3650 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3651 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3653 struct c_declarator *inner
3654 = build_id_declarator (c_parser_peek_token (parser)->value);
3655 *seen_id = true;
3656 inner->id_loc = c_parser_peek_token (parser)->location;
3657 c_parser_consume_token (parser);
3658 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3661 if (kind != C_DTR_NORMAL
3662 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3664 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3665 inner->id_loc = c_parser_peek_token (parser)->location;
3666 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3669 /* Either we are at the end of an abstract declarator, or we have
3670 parentheses. */
3672 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3674 tree attrs;
3675 struct c_declarator *inner;
3676 c_parser_consume_token (parser);
3677 attrs = c_parser_attributes (parser);
3678 if (kind != C_DTR_NORMAL
3679 && (c_parser_next_token_starts_declspecs (parser)
3680 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3682 struct c_arg_info *args
3683 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3684 attrs);
3685 if (args == NULL)
3686 return NULL;
3687 else
3689 inner
3690 = build_function_declarator (args,
3691 build_id_declarator (NULL_TREE));
3692 return c_parser_direct_declarator_inner (parser, *seen_id,
3693 inner);
3696 /* A parenthesized declarator. */
3697 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3698 if (inner != NULL && attrs != NULL)
3699 inner = build_attrs_declarator (attrs, inner);
3700 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3702 c_parser_consume_token (parser);
3703 if (inner == NULL)
3704 return NULL;
3705 else
3706 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3708 else
3710 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3711 "expected %<)%>");
3712 return NULL;
3715 else
3717 if (kind == C_DTR_NORMAL)
3719 c_parser_error (parser, "expected identifier or %<(%>");
3720 return NULL;
3722 else
3723 return build_id_declarator (NULL_TREE);
3727 /* Parse part of a direct declarator or direct abstract declarator,
3728 given that some (in INNER) has already been parsed; ID_PRESENT is
3729 true if an identifier is present, false for an abstract
3730 declarator. */
3732 static struct c_declarator *
3733 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3734 struct c_declarator *inner)
3736 /* Parse a sequence of array declarators and parameter lists. */
3737 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3739 location_t brace_loc = c_parser_peek_token (parser)->location;
3740 struct c_declarator *declarator;
3741 struct c_declspecs *quals_attrs = build_null_declspecs ();
3742 bool static_seen;
3743 bool star_seen;
3744 struct c_expr dimen;
3745 dimen.value = NULL_TREE;
3746 dimen.original_code = ERROR_MARK;
3747 dimen.original_type = NULL_TREE;
3748 c_parser_consume_token (parser);
3749 c_parser_declspecs (parser, quals_attrs, false, false, true,
3750 false, false, cla_prefer_id);
3751 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3752 if (static_seen)
3753 c_parser_consume_token (parser);
3754 if (static_seen && !quals_attrs->declspecs_seen_p)
3755 c_parser_declspecs (parser, quals_attrs, false, false, true,
3756 false, false, cla_prefer_id);
3757 if (!quals_attrs->declspecs_seen_p)
3758 quals_attrs = NULL;
3759 /* If "static" is present, there must be an array dimension.
3760 Otherwise, there may be a dimension, "*", or no
3761 dimension. */
3762 if (static_seen)
3764 star_seen = false;
3765 dimen = c_parser_expr_no_commas (parser, NULL);
3767 else
3769 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3771 dimen.value = NULL_TREE;
3772 star_seen = false;
3774 else if (flag_cilkplus
3775 && c_parser_next_token_is (parser, CPP_COLON))
3777 dimen.value = error_mark_node;
3778 star_seen = false;
3779 error_at (c_parser_peek_token (parser)->location,
3780 "array notations cannot be used in declaration");
3781 c_parser_consume_token (parser);
3783 else if (c_parser_next_token_is (parser, CPP_MULT))
3785 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3787 dimen.value = NULL_TREE;
3788 star_seen = true;
3789 c_parser_consume_token (parser);
3791 else
3793 star_seen = false;
3794 dimen = c_parser_expr_no_commas (parser, NULL);
3797 else
3799 star_seen = false;
3800 dimen = c_parser_expr_no_commas (parser, NULL);
3803 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3804 c_parser_consume_token (parser);
3805 else if (flag_cilkplus
3806 && c_parser_next_token_is (parser, CPP_COLON))
3808 error_at (c_parser_peek_token (parser)->location,
3809 "array notations cannot be used in declaration");
3810 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3811 return NULL;
3813 else
3815 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3816 "expected %<]%>");
3817 return NULL;
3819 if (dimen.value)
3820 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3821 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3822 static_seen, star_seen);
3823 if (declarator == NULL)
3824 return NULL;
3825 inner = set_array_declarator_inner (declarator, inner);
3826 return c_parser_direct_declarator_inner (parser, id_present, inner);
3828 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3830 tree attrs;
3831 struct c_arg_info *args;
3832 c_parser_consume_token (parser);
3833 attrs = c_parser_attributes (parser);
3834 args = c_parser_parms_declarator (parser, id_present, attrs);
3835 if (args == NULL)
3836 return NULL;
3837 else
3839 inner = build_function_declarator (args, inner);
3840 return c_parser_direct_declarator_inner (parser, id_present, inner);
3843 return inner;
3846 /* Parse a parameter list or identifier list, including the closing
3847 parenthesis but not the opening one. ATTRS are the attributes at
3848 the start of the list. ID_LIST_OK is true if an identifier list is
3849 acceptable; such a list must not have attributes at the start. */
3851 static struct c_arg_info *
3852 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3854 push_scope ();
3855 declare_parm_level ();
3856 /* If the list starts with an identifier, it is an identifier list.
3857 Otherwise, it is either a prototype list or an empty list. */
3858 if (id_list_ok
3859 && !attrs
3860 && c_parser_next_token_is (parser, CPP_NAME)
3861 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3863 /* Look ahead to detect typos in type names. */
3864 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3865 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3866 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3867 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3868 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3870 tree list = NULL_TREE, *nextp = &list;
3871 while (c_parser_next_token_is (parser, CPP_NAME)
3872 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3874 *nextp = build_tree_list (NULL_TREE,
3875 c_parser_peek_token (parser)->value);
3876 nextp = & TREE_CHAIN (*nextp);
3877 c_parser_consume_token (parser);
3878 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3879 break;
3880 c_parser_consume_token (parser);
3881 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3883 c_parser_error (parser, "expected identifier");
3884 break;
3887 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3889 struct c_arg_info *ret = build_arg_info ();
3890 ret->types = list;
3891 c_parser_consume_token (parser);
3892 pop_scope ();
3893 return ret;
3895 else
3897 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3898 "expected %<)%>");
3899 pop_scope ();
3900 return NULL;
3903 else
3905 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3906 NULL);
3907 pop_scope ();
3908 return ret;
3912 /* Parse a parameter list (possibly empty), including the closing
3913 parenthesis but not the opening one. ATTRS are the attributes at
3914 the start of the list. EXPR is NULL or an expression that needs to
3915 be evaluated for the side effects of array size expressions in the
3916 parameters. */
3918 static struct c_arg_info *
3919 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3921 bool bad_parm = false;
3923 /* ??? Following the old parser, forward parameter declarations may
3924 use abstract declarators, and if no real parameter declarations
3925 follow the forward declarations then this is not diagnosed. Also
3926 note as above that attributes are ignored as the only contents of
3927 the parentheses, or as the only contents after forward
3928 declarations. */
3929 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3931 struct c_arg_info *ret = build_arg_info ();
3932 c_parser_consume_token (parser);
3933 return ret;
3935 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3937 struct c_arg_info *ret = build_arg_info ();
3939 if (flag_allow_parameterless_variadic_functions)
3941 /* F (...) is allowed. */
3942 ret->types = NULL_TREE;
3944 else
3946 /* Suppress -Wold-style-definition for this case. */
3947 ret->types = error_mark_node;
3948 error_at (c_parser_peek_token (parser)->location,
3949 "ISO C requires a named argument before %<...%>");
3951 c_parser_consume_token (parser);
3952 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3954 c_parser_consume_token (parser);
3955 return ret;
3957 else
3959 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3960 "expected %<)%>");
3961 return NULL;
3964 /* Nonempty list of parameters, either terminated with semicolon
3965 (forward declarations; recurse) or with close parenthesis (normal
3966 function) or with ", ... )" (variadic function). */
3967 while (true)
3969 /* Parse a parameter. */
3970 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3971 attrs = NULL_TREE;
3972 if (parm == NULL)
3973 bad_parm = true;
3974 else
3975 push_parm_decl (parm, &expr);
3976 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3978 tree new_attrs;
3979 c_parser_consume_token (parser);
3980 mark_forward_parm_decls ();
3981 new_attrs = c_parser_attributes (parser);
3982 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3984 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3986 c_parser_consume_token (parser);
3987 if (bad_parm)
3988 return NULL;
3989 else
3990 return get_parm_info (false, expr);
3992 if (!c_parser_require (parser, CPP_COMMA,
3993 "expected %<;%>, %<,%> or %<)%>",
3994 UNKNOWN_LOCATION, false))
3996 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3997 return NULL;
3999 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4001 c_parser_consume_token (parser);
4002 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4004 c_parser_consume_token (parser);
4005 if (bad_parm)
4006 return NULL;
4007 else
4008 return get_parm_info (true, expr);
4010 else
4012 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4013 "expected %<)%>");
4014 return NULL;
4020 /* Parse a parameter declaration. ATTRS are the attributes at the
4021 start of the declaration if it is the first parameter. */
4023 static struct c_parm *
4024 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4026 struct c_declspecs *specs;
4027 struct c_declarator *declarator;
4028 tree prefix_attrs;
4029 tree postfix_attrs = NULL_TREE;
4030 bool dummy = false;
4032 /* Accept #pragmas between parameter declarations. */
4033 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4034 c_parser_pragma (parser, pragma_param, NULL);
4036 if (!c_parser_next_token_starts_declspecs (parser))
4038 c_token *token = c_parser_peek_token (parser);
4039 if (parser->error)
4040 return NULL;
4041 c_parser_set_source_position_from_token (token);
4042 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4044 const char *hint = lookup_name_fuzzy (token->value,
4045 FUZZY_LOOKUP_TYPENAME);
4046 if (hint)
4048 gcc_rich_location richloc (token->location);
4049 richloc.add_fixit_replace (hint);
4050 error_at_rich_loc (&richloc,
4051 "unknown type name %qE; did you mean %qs?",
4052 token->value, hint);
4054 else
4055 error_at (token->location, "unknown type name %qE", token->value);
4056 parser->error = true;
4058 /* ??? In some Objective-C cases '...' isn't applicable so there
4059 should be a different message. */
4060 else
4061 c_parser_error (parser,
4062 "expected declaration specifiers or %<...%>");
4063 c_parser_skip_to_end_of_parameter (parser);
4064 return NULL;
4067 location_t start_loc = c_parser_peek_token (parser)->location;
4069 specs = build_null_declspecs ();
4070 if (attrs)
4072 declspecs_add_attrs (input_location, specs, attrs);
4073 attrs = NULL_TREE;
4075 c_parser_declspecs (parser, specs, true, true, true, true, false,
4076 cla_nonabstract_decl);
4077 finish_declspecs (specs);
4078 pending_xref_error ();
4079 prefix_attrs = specs->attrs;
4080 specs->attrs = NULL_TREE;
4081 declarator = c_parser_declarator (parser,
4082 specs->typespec_kind != ctsk_none,
4083 C_DTR_PARM, &dummy);
4084 if (declarator == NULL)
4086 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4087 return NULL;
4089 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4090 postfix_attrs = c_parser_attributes (parser);
4092 /* Generate a location for the parameter, ranging from the start of the
4093 initial token to the end of the final token.
4095 If we have a identifier, then use it for the caret location, e.g.
4097 extern int callee (int one, int (*two)(int, int), float three);
4098 ~~~~~~^~~~~~~~~~~~~~
4100 otherwise, reuse the start location for the caret location e.g.:
4102 extern int callee (int one, int (*)(int, int), float three);
4103 ^~~~~~~~~~~~~~~~~
4105 location_t end_loc = parser->last_token_location;
4107 /* Find any cdk_id declarator; determine if we have an identifier. */
4108 c_declarator *id_declarator = declarator;
4109 while (id_declarator && id_declarator->kind != cdk_id)
4110 id_declarator = id_declarator->declarator;
4111 location_t caret_loc = (id_declarator->u.id
4112 ? id_declarator->id_loc
4113 : start_loc);
4114 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4116 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4117 declarator, param_loc);
4120 /* Parse a string literal in an asm expression. It should not be
4121 translated, and wide string literals are an error although
4122 permitted by the syntax. This is a GNU extension.
4124 asm-string-literal:
4125 string-literal
4127 ??? At present, following the old parser, the caller needs to have
4128 set lex_untranslated_string to 1. It would be better to follow the
4129 C++ parser rather than using this kludge. */
4131 static tree
4132 c_parser_asm_string_literal (c_parser *parser)
4134 tree str;
4135 int save_flag = warn_overlength_strings;
4136 warn_overlength_strings = 0;
4137 if (c_parser_next_token_is (parser, CPP_STRING))
4139 str = c_parser_peek_token (parser)->value;
4140 c_parser_consume_token (parser);
4142 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4144 error_at (c_parser_peek_token (parser)->location,
4145 "wide string literal in %<asm%>");
4146 str = build_string (1, "");
4147 c_parser_consume_token (parser);
4149 else
4151 c_parser_error (parser, "expected string literal");
4152 str = NULL_TREE;
4154 warn_overlength_strings = save_flag;
4155 return str;
4158 /* Parse a simple asm expression. This is used in restricted
4159 contexts, where a full expression with inputs and outputs does not
4160 make sense. This is a GNU extension.
4162 simple-asm-expr:
4163 asm ( asm-string-literal )
4166 static tree
4167 c_parser_simple_asm_expr (c_parser *parser)
4169 tree str;
4170 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4171 /* ??? Follow the C++ parser rather than using the
4172 lex_untranslated_string kludge. */
4173 parser->lex_untranslated_string = true;
4174 c_parser_consume_token (parser);
4175 matching_parens parens;
4176 if (!parens.require_open (parser))
4178 parser->lex_untranslated_string = false;
4179 return NULL_TREE;
4181 str = c_parser_asm_string_literal (parser);
4182 parser->lex_untranslated_string = false;
4183 if (!parens.require_close (parser))
4185 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4186 return NULL_TREE;
4188 return str;
4191 static tree
4192 c_parser_attribute_any_word (c_parser *parser)
4194 tree attr_name = NULL_TREE;
4196 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4198 /* ??? See comment above about what keywords are accepted here. */
4199 bool ok;
4200 switch (c_parser_peek_token (parser)->keyword)
4202 case RID_STATIC:
4203 case RID_UNSIGNED:
4204 case RID_LONG:
4205 case RID_CONST:
4206 case RID_EXTERN:
4207 case RID_REGISTER:
4208 case RID_TYPEDEF:
4209 case RID_SHORT:
4210 case RID_INLINE:
4211 case RID_NORETURN:
4212 case RID_VOLATILE:
4213 case RID_SIGNED:
4214 case RID_AUTO:
4215 case RID_RESTRICT:
4216 case RID_COMPLEX:
4217 case RID_THREAD:
4218 case RID_INT:
4219 case RID_CHAR:
4220 case RID_FLOAT:
4221 case RID_DOUBLE:
4222 case RID_VOID:
4223 case RID_DFLOAT32:
4224 case RID_DFLOAT64:
4225 case RID_DFLOAT128:
4226 CASE_RID_FLOATN_NX:
4227 case RID_BOOL:
4228 case RID_FRACT:
4229 case RID_ACCUM:
4230 case RID_SAT:
4231 case RID_TRANSACTION_ATOMIC:
4232 case RID_TRANSACTION_CANCEL:
4233 case RID_ATOMIC:
4234 case RID_AUTO_TYPE:
4235 case RID_INT_N_0:
4236 case RID_INT_N_1:
4237 case RID_INT_N_2:
4238 case RID_INT_N_3:
4239 ok = true;
4240 break;
4241 default:
4242 ok = false;
4243 break;
4245 if (!ok)
4246 return NULL_TREE;
4248 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4249 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4251 else if (c_parser_next_token_is (parser, CPP_NAME))
4252 attr_name = c_parser_peek_token (parser)->value;
4254 return attr_name;
4257 #define CILK_SIMD_FN_CLAUSE_MASK \
4258 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
4259 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
4260 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
4261 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
4262 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
4264 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
4265 VEC_TOKEN is the "vector" token that is replaced with "simd" and
4266 pushed into the token list.
4267 Syntax:
4268 vector
4269 vector (<vector attributes>). */
4271 static void
4272 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
4274 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4276 int paren_scope = 0;
4277 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4278 /* Consume the "vector" token. */
4279 c_parser_consume_token (parser);
4281 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4283 c_parser_consume_token (parser);
4284 paren_scope++;
4286 while (paren_scope > 0)
4288 c_token *token = c_parser_peek_token (parser);
4289 if (token->type == CPP_OPEN_PAREN)
4290 paren_scope++;
4291 else if (token->type == CPP_CLOSE_PAREN)
4292 paren_scope--;
4293 /* Do not push the last ')' since we are not pushing the '('. */
4294 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4295 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4296 c_parser_consume_token (parser);
4299 /* Since we are converting an attribute to a pragma, we need to end the
4300 attribute with PRAGMA_EOL. */
4301 c_token eol_token;
4302 memset (&eol_token, 0, sizeof (eol_token));
4303 eol_token.type = CPP_PRAGMA_EOL;
4304 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4307 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4309 static void
4310 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4312 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4314 /* c_parser_attributes is called in several places, so if these EOF
4315 tokens are already inserted, then don't do them again. */
4316 if (last_token.type == CPP_EOF)
4317 return;
4319 /* Two CPP_EOF token are added as a safety net since the normal C
4320 front-end has two token look-ahead. */
4321 c_token eof_token;
4322 eof_token.type = CPP_EOF;
4323 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4324 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4327 /* Parse (possibly empty) attributes. This is a GNU extension.
4329 attributes:
4330 empty
4331 attributes attribute
4333 attribute:
4334 __attribute__ ( ( attribute-list ) )
4336 attribute-list:
4337 attrib
4338 attribute_list , attrib
4340 attrib:
4341 empty
4342 any-word
4343 any-word ( identifier )
4344 any-word ( identifier , nonempty-expr-list )
4345 any-word ( expr-list )
4347 where the "identifier" must not be declared as a type, and
4348 "any-word" may be any identifier (including one declared as a
4349 type), a reserved word storage class specifier, type specifier or
4350 type qualifier. ??? This still leaves out most reserved keywords
4351 (following the old parser), shouldn't we include them, and why not
4352 allow identifiers declared as types to start the arguments? */
4354 static tree
4355 c_parser_attributes (c_parser *parser)
4357 tree attrs = NULL_TREE;
4358 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4360 /* ??? Follow the C++ parser rather than using the
4361 lex_untranslated_string kludge. */
4362 parser->lex_untranslated_string = true;
4363 /* Consume the `__attribute__' keyword. */
4364 c_parser_consume_token (parser);
4365 /* Look for the two `(' tokens. */
4366 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4368 parser->lex_untranslated_string = false;
4369 return attrs;
4371 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4373 parser->lex_untranslated_string = false;
4374 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4375 return attrs;
4377 /* Parse the attribute list. */
4378 while (c_parser_next_token_is (parser, CPP_COMMA)
4379 || c_parser_next_token_is (parser, CPP_NAME)
4380 || c_parser_next_token_is (parser, CPP_KEYWORD))
4382 tree attr, attr_name, attr_args;
4383 vec<tree, va_gc> *expr_list;
4384 if (c_parser_next_token_is (parser, CPP_COMMA))
4386 c_parser_consume_token (parser);
4387 continue;
4390 attr_name = c_parser_attribute_any_word (parser);
4391 if (attr_name == NULL)
4392 break;
4393 attr_name = canonicalize_attr_name (attr_name);
4394 if (is_cilkplus_vector_p (attr_name))
4396 c_token *v_token = c_parser_peek_token (parser);
4397 v_token->value = canonicalize_attr_name (v_token->value);
4398 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4399 /* If the next token isn't a comma, we're done. */
4400 if (!c_parser_next_token_is (parser, CPP_COMMA))
4401 break;
4402 continue;
4404 c_parser_consume_token (parser);
4405 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4407 attr = build_tree_list (attr_name, NULL_TREE);
4408 /* Add this attribute to the list. */
4409 attrs = chainon (attrs, attr);
4410 /* If the next token isn't a comma, we're done. */
4411 if (!c_parser_next_token_is (parser, CPP_COMMA))
4412 break;
4413 continue;
4415 c_parser_consume_token (parser);
4416 /* Parse the attribute contents. If they start with an
4417 identifier which is followed by a comma or close
4418 parenthesis, then the arguments start with that
4419 identifier; otherwise they are an expression list.
4420 In objective-c the identifier may be a classname. */
4421 if (c_parser_next_token_is (parser, CPP_NAME)
4422 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4423 || (c_dialect_objc ()
4424 && c_parser_peek_token (parser)->id_kind
4425 == C_ID_CLASSNAME))
4426 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4427 || (c_parser_peek_2nd_token (parser)->type
4428 == CPP_CLOSE_PAREN))
4429 && (attribute_takes_identifier_p (attr_name)
4430 || (c_dialect_objc ()
4431 && c_parser_peek_token (parser)->id_kind
4432 == C_ID_CLASSNAME)))
4434 tree arg1 = c_parser_peek_token (parser)->value;
4435 c_parser_consume_token (parser);
4436 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4437 attr_args = build_tree_list (NULL_TREE, arg1);
4438 else
4440 tree tree_list;
4441 c_parser_consume_token (parser);
4442 expr_list = c_parser_expr_list (parser, false, true,
4443 NULL, NULL, NULL, NULL);
4444 tree_list = build_tree_list_vec (expr_list);
4445 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4446 release_tree_vector (expr_list);
4449 else
4451 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4452 attr_args = NULL_TREE;
4453 else
4455 expr_list = c_parser_expr_list (parser, false, true,
4456 NULL, NULL, NULL, NULL);
4457 attr_args = build_tree_list_vec (expr_list);
4458 release_tree_vector (expr_list);
4462 attr = build_tree_list (attr_name, attr_args);
4463 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4464 c_parser_consume_token (parser);
4465 else
4467 parser->lex_untranslated_string = false;
4468 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4469 "expected %<)%>");
4470 return attrs;
4472 /* Add this attribute to the list. */
4473 attrs = chainon (attrs, attr);
4474 /* If the next token isn't a comma, we're done. */
4475 if (!c_parser_next_token_is (parser, CPP_COMMA))
4476 break;
4478 /* Look for the two `)' tokens. */
4479 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4480 c_parser_consume_token (parser);
4481 else
4483 parser->lex_untranslated_string = false;
4484 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4485 "expected %<)%>");
4486 return attrs;
4488 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4489 c_parser_consume_token (parser);
4490 else
4492 parser->lex_untranslated_string = false;
4493 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4494 "expected %<)%>");
4495 return attrs;
4497 parser->lex_untranslated_string = false;
4500 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4501 c_finish_cilk_simd_fn_tokens (parser);
4502 return attrs;
4505 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7).
4507 type-name:
4508 specifier-qualifier-list abstract-declarator[opt]
4511 struct c_type_name *
4512 c_parser_type_name (c_parser *parser)
4514 struct c_declspecs *specs = build_null_declspecs ();
4515 struct c_declarator *declarator;
4516 struct c_type_name *ret;
4517 bool dummy = false;
4518 c_parser_declspecs (parser, specs, false, true, true, false, false,
4519 cla_prefer_type);
4520 if (!specs->declspecs_seen_p)
4522 c_parser_error (parser, "expected specifier-qualifier-list");
4523 return NULL;
4525 if (specs->type != error_mark_node)
4527 pending_xref_error ();
4528 finish_declspecs (specs);
4530 declarator = c_parser_declarator (parser,
4531 specs->typespec_kind != ctsk_none,
4532 C_DTR_ABSTRACT, &dummy);
4533 if (declarator == NULL)
4534 return NULL;
4535 ret = XOBNEW (&parser_obstack, struct c_type_name);
4536 ret->specs = specs;
4537 ret->declarator = declarator;
4538 return ret;
4541 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4543 initializer:
4544 assignment-expression
4545 { initializer-list }
4546 { initializer-list , }
4548 initializer-list:
4549 designation[opt] initializer
4550 initializer-list , designation[opt] initializer
4552 designation:
4553 designator-list =
4555 designator-list:
4556 designator
4557 designator-list designator
4559 designator:
4560 array-designator
4561 . identifier
4563 array-designator:
4564 [ constant-expression ]
4566 GNU extensions:
4568 initializer:
4571 designation:
4572 array-designator
4573 identifier :
4575 array-designator:
4576 [ constant-expression ... constant-expression ]
4578 Any expression without commas is accepted in the syntax for the
4579 constant-expressions, with non-constant expressions rejected later.
4581 This function is only used for top-level initializers; for nested
4582 ones, see c_parser_initval. */
4584 static struct c_expr
4585 c_parser_initializer (c_parser *parser)
4587 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4588 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4589 else
4591 struct c_expr ret;
4592 location_t loc = c_parser_peek_token (parser)->location;
4593 ret = c_parser_expr_no_commas (parser, NULL);
4594 if (TREE_CODE (ret.value) != STRING_CST
4595 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4596 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4597 return ret;
4601 /* The location of the last comma within the current initializer list,
4602 or UNKNOWN_LOCATION if not within one. */
4604 location_t last_init_list_comma;
4606 /* Parse a braced initializer list. TYPE is the type specified for a
4607 compound literal, and NULL_TREE for other initializers and for
4608 nested braced lists. NESTED_P is true for nested braced lists,
4609 false for the list of a compound literal or the list that is the
4610 top-level initializer in a declaration. */
4612 static struct c_expr
4613 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4614 struct obstack *outer_obstack)
4616 struct c_expr ret;
4617 struct obstack braced_init_obstack;
4618 location_t brace_loc = c_parser_peek_token (parser)->location;
4619 gcc_obstack_init (&braced_init_obstack);
4620 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4621 matching_braces braces;
4622 braces.consume_open (parser);
4623 if (nested_p)
4625 finish_implicit_inits (brace_loc, outer_obstack);
4626 push_init_level (brace_loc, 0, &braced_init_obstack);
4628 else
4629 really_start_incremental_init (type);
4630 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4632 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4634 else
4636 /* Parse a non-empty initializer list, possibly with a trailing
4637 comma. */
4638 while (true)
4640 c_parser_initelt (parser, &braced_init_obstack);
4641 if (parser->error)
4642 break;
4643 if (c_parser_next_token_is (parser, CPP_COMMA))
4645 last_init_list_comma = c_parser_peek_token (parser)->location;
4646 c_parser_consume_token (parser);
4648 else
4649 break;
4650 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4651 break;
4654 c_token *next_tok = c_parser_peek_token (parser);
4655 if (next_tok->type != CPP_CLOSE_BRACE)
4657 ret.value = error_mark_node;
4658 ret.original_code = ERROR_MARK;
4659 ret.original_type = NULL;
4660 braces.skip_until_found_close (parser);
4661 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4662 obstack_free (&braced_init_obstack, NULL);
4663 return ret;
4665 location_t close_loc = next_tok->location;
4666 c_parser_consume_token (parser);
4667 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4668 obstack_free (&braced_init_obstack, NULL);
4669 set_c_expr_source_range (&ret, brace_loc, close_loc);
4670 return ret;
4673 /* Parse a nested initializer, including designators. */
4675 static void
4676 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4678 /* Parse any designator or designator list. A single array
4679 designator may have the subsequent "=" omitted in GNU C, but a
4680 longer list or a structure member designator may not. */
4681 if (c_parser_next_token_is (parser, CPP_NAME)
4682 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4684 /* Old-style structure member designator. */
4685 set_init_label (c_parser_peek_token (parser)->location,
4686 c_parser_peek_token (parser)->value,
4687 c_parser_peek_token (parser)->location,
4688 braced_init_obstack);
4689 /* Use the colon as the error location. */
4690 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4691 "obsolete use of designated initializer with %<:%>");
4692 c_parser_consume_token (parser);
4693 c_parser_consume_token (parser);
4695 else
4697 /* des_seen is 0 if there have been no designators, 1 if there
4698 has been a single array designator and 2 otherwise. */
4699 int des_seen = 0;
4700 /* Location of a designator. */
4701 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4702 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4703 || c_parser_next_token_is (parser, CPP_DOT))
4705 int des_prev = des_seen;
4706 if (!des_seen)
4707 des_loc = c_parser_peek_token (parser)->location;
4708 if (des_seen < 2)
4709 des_seen++;
4710 if (c_parser_next_token_is (parser, CPP_DOT))
4712 des_seen = 2;
4713 c_parser_consume_token (parser);
4714 if (c_parser_next_token_is (parser, CPP_NAME))
4716 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4717 c_parser_peek_token (parser)->location,
4718 braced_init_obstack);
4719 c_parser_consume_token (parser);
4721 else
4723 struct c_expr init;
4724 init.value = error_mark_node;
4725 init.original_code = ERROR_MARK;
4726 init.original_type = NULL;
4727 c_parser_error (parser, "expected identifier");
4728 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4729 process_init_element (input_location, init, false,
4730 braced_init_obstack);
4731 return;
4734 else
4736 tree first, second;
4737 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4738 location_t array_index_loc = UNKNOWN_LOCATION;
4739 /* ??? Following the old parser, [ objc-receiver
4740 objc-message-args ] is accepted as an initializer,
4741 being distinguished from a designator by what follows
4742 the first assignment expression inside the square
4743 brackets, but after a first array designator a
4744 subsequent square bracket is for Objective-C taken to
4745 start an expression, using the obsolete form of
4746 designated initializer without '=', rather than
4747 possibly being a second level of designation: in LALR
4748 terms, the '[' is shifted rather than reducing
4749 designator to designator-list. */
4750 if (des_prev == 1 && c_dialect_objc ())
4752 des_seen = des_prev;
4753 break;
4755 if (des_prev == 0 && c_dialect_objc ())
4757 /* This might be an array designator or an
4758 Objective-C message expression. If the former,
4759 continue parsing here; if the latter, parse the
4760 remainder of the initializer given the starting
4761 primary-expression. ??? It might make sense to
4762 distinguish when des_prev == 1 as well; see
4763 previous comment. */
4764 tree rec, args;
4765 struct c_expr mexpr;
4766 c_parser_consume_token (parser);
4767 if (c_parser_peek_token (parser)->type == CPP_NAME
4768 && ((c_parser_peek_token (parser)->id_kind
4769 == C_ID_TYPENAME)
4770 || (c_parser_peek_token (parser)->id_kind
4771 == C_ID_CLASSNAME)))
4773 /* Type name receiver. */
4774 tree id = c_parser_peek_token (parser)->value;
4775 c_parser_consume_token (parser);
4776 rec = objc_get_class_reference (id);
4777 goto parse_message_args;
4779 first = c_parser_expr_no_commas (parser, NULL).value;
4780 mark_exp_read (first);
4781 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4782 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4783 goto array_desig_after_first;
4784 /* Expression receiver. So far only one part
4785 without commas has been parsed; there might be
4786 more of the expression. */
4787 rec = first;
4788 while (c_parser_next_token_is (parser, CPP_COMMA))
4790 struct c_expr next;
4791 location_t comma_loc, exp_loc;
4792 comma_loc = c_parser_peek_token (parser)->location;
4793 c_parser_consume_token (parser);
4794 exp_loc = c_parser_peek_token (parser)->location;
4795 next = c_parser_expr_no_commas (parser, NULL);
4796 next = convert_lvalue_to_rvalue (exp_loc, next,
4797 true, true);
4798 rec = build_compound_expr (comma_loc, rec, next.value);
4800 parse_message_args:
4801 /* Now parse the objc-message-args. */
4802 args = c_parser_objc_message_args (parser);
4803 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4804 "expected %<]%>");
4805 mexpr.value
4806 = objc_build_message_expr (rec, args);
4807 mexpr.original_code = ERROR_MARK;
4808 mexpr.original_type = NULL;
4809 /* Now parse and process the remainder of the
4810 initializer, starting with this message
4811 expression as a primary-expression. */
4812 c_parser_initval (parser, &mexpr, braced_init_obstack);
4813 return;
4815 c_parser_consume_token (parser);
4816 array_index_loc = c_parser_peek_token (parser)->location;
4817 first = c_parser_expr_no_commas (parser, NULL).value;
4818 mark_exp_read (first);
4819 array_desig_after_first:
4820 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4822 ellipsis_loc = c_parser_peek_token (parser)->location;
4823 c_parser_consume_token (parser);
4824 second = c_parser_expr_no_commas (parser, NULL).value;
4825 mark_exp_read (second);
4827 else
4828 second = NULL_TREE;
4829 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4831 c_parser_consume_token (parser);
4832 set_init_index (array_index_loc, first, second,
4833 braced_init_obstack);
4834 if (second)
4835 pedwarn (ellipsis_loc, OPT_Wpedantic,
4836 "ISO C forbids specifying range of elements to initialize");
4838 else
4839 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4840 "expected %<]%>");
4843 if (des_seen >= 1)
4845 if (c_parser_next_token_is (parser, CPP_EQ))
4847 pedwarn_c90 (des_loc, OPT_Wpedantic,
4848 "ISO C90 forbids specifying subobject "
4849 "to initialize");
4850 c_parser_consume_token (parser);
4852 else
4854 if (des_seen == 1)
4855 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4856 "obsolete use of designated initializer without %<=%>");
4857 else
4859 struct c_expr init;
4860 init.value = error_mark_node;
4861 init.original_code = ERROR_MARK;
4862 init.original_type = NULL;
4863 c_parser_error (parser, "expected %<=%>");
4864 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4865 process_init_element (input_location, init, false,
4866 braced_init_obstack);
4867 return;
4872 c_parser_initval (parser, NULL, braced_init_obstack);
4875 /* Parse a nested initializer; as c_parser_initializer but parses
4876 initializers within braced lists, after any designators have been
4877 applied. If AFTER is not NULL then it is an Objective-C message
4878 expression which is the primary-expression starting the
4879 initializer. */
4881 static void
4882 c_parser_initval (c_parser *parser, struct c_expr *after,
4883 struct obstack * braced_init_obstack)
4885 struct c_expr init;
4886 gcc_assert (!after || c_dialect_objc ());
4887 location_t loc = c_parser_peek_token (parser)->location;
4889 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4890 init = c_parser_braced_init (parser, NULL_TREE, true,
4891 braced_init_obstack);
4892 else
4894 init = c_parser_expr_no_commas (parser, after);
4895 if (init.value != NULL_TREE
4896 && TREE_CODE (init.value) != STRING_CST
4897 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4898 init = convert_lvalue_to_rvalue (loc, init, true, true);
4900 process_init_element (loc, init, false, braced_init_obstack);
4903 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4904 C99 6.8.2, C11 6.8.2).
4906 compound-statement:
4907 { block-item-list[opt] }
4908 { label-declarations block-item-list }
4910 block-item-list:
4911 block-item
4912 block-item-list block-item
4914 block-item:
4915 nested-declaration
4916 statement
4918 nested-declaration:
4919 declaration
4921 GNU extensions:
4923 compound-statement:
4924 { label-declarations block-item-list }
4926 nested-declaration:
4927 __extension__ nested-declaration
4928 nested-function-definition
4930 label-declarations:
4931 label-declaration
4932 label-declarations label-declaration
4934 label-declaration:
4935 __label__ identifier-list ;
4937 Allowing the mixing of declarations and code is new in C99. The
4938 GNU syntax also permits (not shown above) labels at the end of
4939 compound statements, which yield an error. We don't allow labels
4940 on declarations; this might seem like a natural extension, but
4941 there would be a conflict between attributes on the label and
4942 prefix attributes on the declaration. ??? The syntax follows the
4943 old parser in requiring something after label declarations.
4944 Although they are erroneous if the labels declared aren't defined,
4945 is it useful for the syntax to be this way?
4947 OpenACC:
4949 block-item:
4950 openacc-directive
4952 openacc-directive:
4953 update-directive
4955 OpenMP:
4957 block-item:
4958 openmp-directive
4960 openmp-directive:
4961 barrier-directive
4962 flush-directive
4963 taskwait-directive
4964 taskyield-directive
4965 cancel-directive
4966 cancellation-point-directive */
4968 static tree
4969 c_parser_compound_statement (c_parser *parser)
4971 tree stmt;
4972 location_t brace_loc;
4973 brace_loc = c_parser_peek_token (parser)->location;
4974 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4976 /* Ensure a scope is entered and left anyway to avoid confusion
4977 if we have just prepared to enter a function body. */
4978 stmt = c_begin_compound_stmt (true);
4979 c_end_compound_stmt (brace_loc, stmt, true);
4980 return error_mark_node;
4982 stmt = c_begin_compound_stmt (true);
4983 c_parser_compound_statement_nostart (parser);
4985 /* If the compound stmt contains array notations, then we expand them. */
4986 if (flag_cilkplus && contains_array_notation_expr (stmt))
4987 stmt = expand_array_notation_exprs (stmt);
4988 return c_end_compound_stmt (brace_loc, stmt, true);
4991 /* Parse a compound statement except for the opening brace. This is
4992 used for parsing both compound statements and statement expressions
4993 (which follow different paths to handling the opening). */
4995 static void
4996 c_parser_compound_statement_nostart (c_parser *parser)
4998 bool last_stmt = false;
4999 bool last_label = false;
5000 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
5001 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5002 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5004 c_parser_consume_token (parser);
5005 return;
5007 mark_valid_location_for_stdc_pragma (true);
5008 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
5010 /* Read zero or more forward-declarations for labels that nested
5011 functions can jump to. */
5012 mark_valid_location_for_stdc_pragma (false);
5013 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
5015 label_loc = c_parser_peek_token (parser)->location;
5016 c_parser_consume_token (parser);
5017 /* Any identifiers, including those declared as type names,
5018 are OK here. */
5019 while (true)
5021 tree label;
5022 if (c_parser_next_token_is_not (parser, CPP_NAME))
5024 c_parser_error (parser, "expected identifier");
5025 break;
5027 label
5028 = declare_label (c_parser_peek_token (parser)->value);
5029 C_DECLARED_LABEL_FLAG (label) = 1;
5030 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
5031 c_parser_consume_token (parser);
5032 if (c_parser_next_token_is (parser, CPP_COMMA))
5033 c_parser_consume_token (parser);
5034 else
5035 break;
5037 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5039 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
5041 /* We must now have at least one statement, label or declaration. */
5042 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5044 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5045 c_parser_error (parser, "expected declaration or statement");
5046 c_parser_consume_token (parser);
5047 return;
5049 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
5051 location_t loc = c_parser_peek_token (parser)->location;
5052 if (c_parser_next_token_is_keyword (parser, RID_CASE)
5053 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5054 || (c_parser_next_token_is (parser, CPP_NAME)
5055 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5057 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5058 label_loc = c_parser_peek_2nd_token (parser)->location;
5059 else
5060 label_loc = c_parser_peek_token (parser)->location;
5061 last_label = true;
5062 last_stmt = false;
5063 mark_valid_location_for_stdc_pragma (false);
5064 c_parser_label (parser);
5066 else if (!last_label
5067 && c_parser_next_tokens_start_declaration (parser))
5069 last_label = false;
5070 mark_valid_location_for_stdc_pragma (false);
5071 bool fallthru_attr_p = false;
5072 c_parser_declaration_or_fndef (parser, true, true, true, true,
5073 true, NULL, vNULL, NULL,
5074 &fallthru_attr_p);
5075 if (last_stmt && !fallthru_attr_p)
5076 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5077 "ISO C90 forbids mixed declarations and code");
5078 last_stmt = fallthru_attr_p;
5080 else if (!last_label
5081 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5083 /* __extension__ can start a declaration, but is also an
5084 unary operator that can start an expression. Consume all
5085 but the last of a possible series of __extension__ to
5086 determine which. */
5087 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5088 && (c_parser_peek_2nd_token (parser)->keyword
5089 == RID_EXTENSION))
5090 c_parser_consume_token (parser);
5091 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5093 int ext;
5094 ext = disable_extension_diagnostics ();
5095 c_parser_consume_token (parser);
5096 last_label = false;
5097 mark_valid_location_for_stdc_pragma (false);
5098 c_parser_declaration_or_fndef (parser, true, true, true, true,
5099 true, NULL, vNULL);
5100 /* Following the old parser, __extension__ does not
5101 disable this diagnostic. */
5102 restore_extension_diagnostics (ext);
5103 if (last_stmt)
5104 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5105 "ISO C90 forbids mixed declarations and code");
5106 last_stmt = false;
5108 else
5109 goto statement;
5111 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5113 /* External pragmas, and some omp pragmas, are not associated
5114 with regular c code, and so are not to be considered statements
5115 syntactically. This ensures that the user doesn't put them
5116 places that would turn into syntax errors if the directive
5117 were ignored. */
5118 if (c_parser_pragma (parser,
5119 last_label ? pragma_stmt : pragma_compound,
5120 NULL))
5121 last_label = false, last_stmt = true;
5123 else if (c_parser_next_token_is (parser, CPP_EOF))
5125 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5126 c_parser_error (parser, "expected declaration or statement");
5127 return;
5129 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5131 if (parser->in_if_block)
5133 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5134 error_at (loc, "expected %<}%> before %<else%>");
5135 return;
5137 else
5139 error_at (loc, "%<else%> without a previous %<if%>");
5140 c_parser_consume_token (parser);
5141 continue;
5144 else
5146 statement:
5147 last_label = false;
5148 last_stmt = true;
5149 mark_valid_location_for_stdc_pragma (false);
5150 c_parser_statement_after_labels (parser, NULL);
5153 parser->error = false;
5155 if (last_label)
5156 error_at (label_loc, "label at end of compound statement");
5157 c_parser_consume_token (parser);
5158 /* Restore the value we started with. */
5159 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5162 /* Parse all consecutive labels. */
5164 static void
5165 c_parser_all_labels (c_parser *parser)
5167 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5168 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5169 || (c_parser_next_token_is (parser, CPP_NAME)
5170 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5171 c_parser_label (parser);
5174 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5176 label:
5177 identifier : attributes[opt]
5178 case constant-expression :
5179 default :
5181 GNU extensions:
5183 label:
5184 case constant-expression ... constant-expression :
5186 The use of attributes on labels is a GNU extension. The syntax in
5187 GNU C accepts any expressions without commas, non-constant
5188 expressions being rejected later. */
5190 static void
5191 c_parser_label (c_parser *parser)
5193 location_t loc1 = c_parser_peek_token (parser)->location;
5194 tree label = NULL_TREE;
5196 /* Remember whether this case or a user-defined label is allowed to fall
5197 through to. */
5198 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5200 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5202 tree exp1, exp2;
5203 c_parser_consume_token (parser);
5204 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5205 if (c_parser_next_token_is (parser, CPP_COLON))
5207 c_parser_consume_token (parser);
5208 label = do_case (loc1, exp1, NULL_TREE);
5210 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5212 c_parser_consume_token (parser);
5213 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5214 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5215 label = do_case (loc1, exp1, exp2);
5217 else
5218 c_parser_error (parser, "expected %<:%> or %<...%>");
5220 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5222 c_parser_consume_token (parser);
5223 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5224 label = do_case (loc1, NULL_TREE, NULL_TREE);
5226 else
5228 tree name = c_parser_peek_token (parser)->value;
5229 tree tlab;
5230 tree attrs;
5231 location_t loc2 = c_parser_peek_token (parser)->location;
5232 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5233 c_parser_consume_token (parser);
5234 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5235 c_parser_consume_token (parser);
5236 attrs = c_parser_attributes (parser);
5237 tlab = define_label (loc2, name);
5238 if (tlab)
5240 decl_attributes (&tlab, attrs, 0);
5241 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5244 if (label)
5246 if (TREE_CODE (label) == LABEL_EXPR)
5247 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5248 else
5249 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5251 /* Allow '__attribute__((fallthrough));'. */
5252 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5254 location_t loc = c_parser_peek_token (parser)->location;
5255 tree attrs = c_parser_attributes (parser);
5256 if (attribute_fallthrough_p (attrs))
5258 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5260 tree fn = build_call_expr_internal_loc (loc,
5261 IFN_FALLTHROUGH,
5262 void_type_node, 0);
5263 add_stmt (fn);
5265 else
5266 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5267 "not followed by %<;%>");
5269 else if (attrs != NULL_TREE)
5270 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5271 " can be applied to a null statement");
5273 if (c_parser_next_tokens_start_declaration (parser))
5275 error_at (c_parser_peek_token (parser)->location,
5276 "a label can only be part of a statement and "
5277 "a declaration is not a statement");
5278 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5279 /*static_assert_ok*/ true,
5280 /*empty_ok*/ true, /*nested*/ true,
5281 /*start_attr_ok*/ true, NULL,
5282 vNULL);
5287 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5289 statement:
5290 labeled-statement
5291 compound-statement
5292 expression-statement
5293 selection-statement
5294 iteration-statement
5295 jump-statement
5297 labeled-statement:
5298 label statement
5300 expression-statement:
5301 expression[opt] ;
5303 selection-statement:
5304 if-statement
5305 switch-statement
5307 iteration-statement:
5308 while-statement
5309 do-statement
5310 for-statement
5312 jump-statement:
5313 goto identifier ;
5314 continue ;
5315 break ;
5316 return expression[opt] ;
5318 GNU extensions:
5320 statement:
5321 asm-statement
5323 jump-statement:
5324 goto * expression ;
5326 expression-statement:
5327 attributes ;
5329 Objective-C:
5331 statement:
5332 objc-throw-statement
5333 objc-try-catch-statement
5334 objc-synchronized-statement
5336 objc-throw-statement:
5337 @throw expression ;
5338 @throw ;
5340 OpenACC:
5342 statement:
5343 openacc-construct
5345 openacc-construct:
5346 parallel-construct
5347 kernels-construct
5348 data-construct
5349 loop-construct
5351 parallel-construct:
5352 parallel-directive structured-block
5354 kernels-construct:
5355 kernels-directive structured-block
5357 data-construct:
5358 data-directive structured-block
5360 loop-construct:
5361 loop-directive structured-block
5363 OpenMP:
5365 statement:
5366 openmp-construct
5368 openmp-construct:
5369 parallel-construct
5370 for-construct
5371 simd-construct
5372 for-simd-construct
5373 sections-construct
5374 single-construct
5375 parallel-for-construct
5376 parallel-for-simd-construct
5377 parallel-sections-construct
5378 master-construct
5379 critical-construct
5380 atomic-construct
5381 ordered-construct
5383 parallel-construct:
5384 parallel-directive structured-block
5386 for-construct:
5387 for-directive iteration-statement
5389 simd-construct:
5390 simd-directive iteration-statements
5392 for-simd-construct:
5393 for-simd-directive iteration-statements
5395 sections-construct:
5396 sections-directive section-scope
5398 single-construct:
5399 single-directive structured-block
5401 parallel-for-construct:
5402 parallel-for-directive iteration-statement
5404 parallel-for-simd-construct:
5405 parallel-for-simd-directive iteration-statement
5407 parallel-sections-construct:
5408 parallel-sections-directive section-scope
5410 master-construct:
5411 master-directive structured-block
5413 critical-construct:
5414 critical-directive structured-block
5416 atomic-construct:
5417 atomic-directive expression-statement
5419 ordered-construct:
5420 ordered-directive structured-block
5422 Transactional Memory:
5424 statement:
5425 transaction-statement
5426 transaction-cancel-statement
5428 IF_P is used to track whether there's a (possibly labeled) if statement
5429 which is not enclosed in braces and has an else clause. This is used to
5430 implement -Wparentheses. */
5432 static void
5433 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5435 c_parser_all_labels (parser);
5436 if (loc_after_labels)
5437 *loc_after_labels = c_parser_peek_token (parser)->location;
5438 c_parser_statement_after_labels (parser, if_p, NULL);
5441 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5442 of if-else-if conditions.
5444 IF_P is used to track whether there's a (possibly labeled) if statement
5445 which is not enclosed in braces and has an else clause. This is used to
5446 implement -Wparentheses. */
5448 static void
5449 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5450 vec<tree> *chain)
5452 location_t loc = c_parser_peek_token (parser)->location;
5453 tree stmt = NULL_TREE;
5454 bool in_if_block = parser->in_if_block;
5455 parser->in_if_block = false;
5456 if (if_p != NULL)
5457 *if_p = false;
5458 switch (c_parser_peek_token (parser)->type)
5460 case CPP_OPEN_BRACE:
5461 add_stmt (c_parser_compound_statement (parser));
5462 break;
5463 case CPP_KEYWORD:
5464 switch (c_parser_peek_token (parser)->keyword)
5466 case RID_IF:
5467 c_parser_if_statement (parser, if_p, chain);
5468 break;
5469 case RID_SWITCH:
5470 c_parser_switch_statement (parser, if_p);
5471 break;
5472 case RID_WHILE:
5473 c_parser_while_statement (parser, false, if_p);
5474 break;
5475 case RID_DO:
5476 c_parser_do_statement (parser, false);
5477 break;
5478 case RID_FOR:
5479 c_parser_for_statement (parser, false, if_p);
5480 break;
5481 case RID_CILK_FOR:
5482 if (!flag_cilkplus)
5484 error_at (c_parser_peek_token (parser)->location,
5485 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5486 c_parser_skip_to_end_of_block_or_statement (parser);
5488 else
5489 c_parser_cilk_for (parser, integer_zero_node, if_p);
5490 break;
5491 case RID_CILK_SYNC:
5492 c_parser_consume_token (parser);
5493 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5494 if (!flag_cilkplus)
5495 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5496 else
5497 add_stmt (build_cilk_sync ());
5498 break;
5499 case RID_GOTO:
5500 c_parser_consume_token (parser);
5501 if (c_parser_next_token_is (parser, CPP_NAME))
5503 stmt = c_finish_goto_label (loc,
5504 c_parser_peek_token (parser)->value);
5505 c_parser_consume_token (parser);
5507 else if (c_parser_next_token_is (parser, CPP_MULT))
5509 struct c_expr val;
5511 c_parser_consume_token (parser);
5512 val = c_parser_expression (parser);
5513 if (check_no_cilk (val.value,
5514 "Cilk array notation cannot be used as a computed goto expression",
5515 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5516 loc))
5517 val.value = error_mark_node;
5518 val = convert_lvalue_to_rvalue (loc, val, false, true);
5519 stmt = c_finish_goto_ptr (loc, val.value);
5521 else
5522 c_parser_error (parser, "expected identifier or %<*%>");
5523 goto expect_semicolon;
5524 case RID_CONTINUE:
5525 c_parser_consume_token (parser);
5526 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5527 goto expect_semicolon;
5528 case RID_BREAK:
5529 c_parser_consume_token (parser);
5530 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5531 goto expect_semicolon;
5532 case RID_RETURN:
5533 c_parser_consume_token (parser);
5534 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5536 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5537 c_parser_consume_token (parser);
5539 else
5541 location_t xloc = c_parser_peek_token (parser)->location;
5542 struct c_expr expr = c_parser_expression_conv (parser);
5543 mark_exp_read (expr.value);
5544 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5545 expr.value, expr.original_type);
5546 goto expect_semicolon;
5548 break;
5549 case RID_ASM:
5550 stmt = c_parser_asm_statement (parser);
5551 break;
5552 case RID_TRANSACTION_ATOMIC:
5553 case RID_TRANSACTION_RELAXED:
5554 stmt = c_parser_transaction (parser,
5555 c_parser_peek_token (parser)->keyword);
5556 break;
5557 case RID_TRANSACTION_CANCEL:
5558 stmt = c_parser_transaction_cancel (parser);
5559 goto expect_semicolon;
5560 case RID_AT_THROW:
5561 gcc_assert (c_dialect_objc ());
5562 c_parser_consume_token (parser);
5563 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5565 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5566 c_parser_consume_token (parser);
5568 else
5570 struct c_expr expr = c_parser_expression (parser);
5571 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5572 if (check_no_cilk (expr.value,
5573 "Cilk array notation cannot be used for a throw expression",
5574 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5575 expr.value = error_mark_node;
5576 else
5578 expr.value = c_fully_fold (expr.value, false, NULL);
5579 stmt = objc_build_throw_stmt (loc, expr.value);
5581 goto expect_semicolon;
5583 break;
5584 case RID_AT_TRY:
5585 gcc_assert (c_dialect_objc ());
5586 c_parser_objc_try_catch_finally_statement (parser);
5587 break;
5588 case RID_AT_SYNCHRONIZED:
5589 gcc_assert (c_dialect_objc ());
5590 c_parser_objc_synchronized_statement (parser);
5591 break;
5592 case RID_ATTRIBUTE:
5594 /* Allow '__attribute__((fallthrough));'. */
5595 tree attrs = c_parser_attributes (parser);
5596 if (attribute_fallthrough_p (attrs))
5598 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5600 tree fn = build_call_expr_internal_loc (loc,
5601 IFN_FALLTHROUGH,
5602 void_type_node, 0);
5603 add_stmt (fn);
5604 /* Eat the ';'. */
5605 c_parser_consume_token (parser);
5607 else
5608 warning_at (loc, OPT_Wattributes,
5609 "%<fallthrough%> attribute not followed "
5610 "by %<;%>");
5612 else if (attrs != NULL_TREE)
5613 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5614 " can be applied to a null statement");
5615 break;
5617 default:
5618 goto expr_stmt;
5620 break;
5621 case CPP_SEMICOLON:
5622 c_parser_consume_token (parser);
5623 break;
5624 case CPP_CLOSE_PAREN:
5625 case CPP_CLOSE_SQUARE:
5626 /* Avoid infinite loop in error recovery:
5627 c_parser_skip_until_found stops at a closing nesting
5628 delimiter without consuming it, but here we need to consume
5629 it to proceed further. */
5630 c_parser_error (parser, "expected statement");
5631 c_parser_consume_token (parser);
5632 break;
5633 case CPP_PRAGMA:
5634 c_parser_pragma (parser, pragma_stmt, if_p);
5635 break;
5636 default:
5637 expr_stmt:
5638 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5639 expect_semicolon:
5640 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5641 break;
5643 /* Two cases cannot and do not have line numbers associated: If stmt
5644 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5645 cannot hold line numbers. But that's OK because the statement
5646 will either be changed to a MODIFY_EXPR during gimplification of
5647 the statement expr, or discarded. If stmt was compound, but
5648 without new variables, we will have skipped the creation of a
5649 BIND and will have a bare STATEMENT_LIST. But that's OK because
5650 (recursively) all of the component statements should already have
5651 line numbers assigned. ??? Can we discard no-op statements
5652 earlier? */
5653 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5654 protected_set_expr_location (stmt, loc);
5656 parser->in_if_block = in_if_block;
5659 /* Parse the condition from an if, do, while or for statements. */
5661 static tree
5662 c_parser_condition (c_parser *parser)
5664 location_t loc = c_parser_peek_token (parser)->location;
5665 tree cond;
5666 cond = c_parser_expression_conv (parser).value;
5667 cond = c_objc_common_truthvalue_conversion (loc, cond);
5668 cond = c_fully_fold (cond, false, NULL);
5669 if (warn_sequence_point)
5670 verify_sequence_points (cond);
5671 return cond;
5674 /* Parse a parenthesized condition from an if, do or while statement.
5676 condition:
5677 ( expression )
5679 static tree
5680 c_parser_paren_condition (c_parser *parser)
5682 tree cond;
5683 matching_parens parens;
5684 if (!parens.require_open (parser))
5685 return error_mark_node;
5686 cond = c_parser_condition (parser);
5687 parens.skip_until_found_close (parser);
5688 return cond;
5691 /* Parse a statement which is a block in C99.
5693 IF_P is used to track whether there's a (possibly labeled) if statement
5694 which is not enclosed in braces and has an else clause. This is used to
5695 implement -Wparentheses. */
5697 static tree
5698 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5699 location_t *loc_after_labels)
5701 tree block = c_begin_compound_stmt (flag_isoc99);
5702 location_t loc = c_parser_peek_token (parser)->location;
5703 c_parser_statement (parser, if_p, loc_after_labels);
5704 return c_end_compound_stmt (loc, block, flag_isoc99);
5707 /* Parse the body of an if statement. This is just parsing a
5708 statement but (a) it is a block in C99, (b) we track whether the
5709 body is an if statement for the sake of -Wparentheses warnings, (c)
5710 we handle an empty body specially for the sake of -Wempty-body
5711 warnings, and (d) we call parser_compound_statement directly
5712 because c_parser_statement_after_labels resets
5713 parser->in_if_block.
5715 IF_P is used to track whether there's a (possibly labeled) if statement
5716 which is not enclosed in braces and has an else clause. This is used to
5717 implement -Wparentheses. */
5719 static tree
5720 c_parser_if_body (c_parser *parser, bool *if_p,
5721 const token_indent_info &if_tinfo)
5723 tree block = c_begin_compound_stmt (flag_isoc99);
5724 location_t body_loc = c_parser_peek_token (parser)->location;
5725 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5726 token_indent_info body_tinfo
5727 = get_token_indent_info (c_parser_peek_token (parser));
5729 c_parser_all_labels (parser);
5730 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5732 location_t loc = c_parser_peek_token (parser)->location;
5733 add_stmt (build_empty_stmt (loc));
5734 c_parser_consume_token (parser);
5735 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5736 warning_at (loc, OPT_Wempty_body,
5737 "suggest braces around empty body in an %<if%> statement");
5739 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5740 add_stmt (c_parser_compound_statement (parser));
5741 else
5743 body_loc_after_labels = c_parser_peek_token (parser)->location;
5744 c_parser_statement_after_labels (parser, if_p);
5747 token_indent_info next_tinfo
5748 = get_token_indent_info (c_parser_peek_token (parser));
5749 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5750 if (body_loc_after_labels != UNKNOWN_LOCATION
5751 && next_tinfo.type != CPP_SEMICOLON)
5752 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5753 if_tinfo.location, RID_IF);
5755 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5758 /* Parse the else body of an if statement. This is just parsing a
5759 statement but (a) it is a block in C99, (b) we handle an empty body
5760 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5761 of if-else-if conditions. */
5763 static tree
5764 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5765 vec<tree> *chain)
5767 location_t body_loc = c_parser_peek_token (parser)->location;
5768 tree block = c_begin_compound_stmt (flag_isoc99);
5769 token_indent_info body_tinfo
5770 = get_token_indent_info (c_parser_peek_token (parser));
5771 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5773 c_parser_all_labels (parser);
5774 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5776 location_t loc = c_parser_peek_token (parser)->location;
5777 warning_at (loc,
5778 OPT_Wempty_body,
5779 "suggest braces around empty body in an %<else%> statement");
5780 add_stmt (build_empty_stmt (loc));
5781 c_parser_consume_token (parser);
5783 else
5785 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5786 body_loc_after_labels = c_parser_peek_token (parser)->location;
5787 c_parser_statement_after_labels (parser, NULL, chain);
5790 token_indent_info next_tinfo
5791 = get_token_indent_info (c_parser_peek_token (parser));
5792 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5793 if (body_loc_after_labels != UNKNOWN_LOCATION
5794 && next_tinfo.type != CPP_SEMICOLON)
5795 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5796 else_tinfo.location, RID_ELSE);
5798 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5801 /* We might need to reclassify any previously-lexed identifier, e.g.
5802 when we've left a for loop with an if-statement without else in the
5803 body - we might have used a wrong scope for the token. See PR67784. */
5805 static void
5806 c_parser_maybe_reclassify_token (c_parser *parser)
5808 if (c_parser_next_token_is (parser, CPP_NAME))
5810 c_token *token = c_parser_peek_token (parser);
5812 if (token->id_kind != C_ID_CLASSNAME)
5814 tree decl = lookup_name (token->value);
5816 token->id_kind = C_ID_ID;
5817 if (decl)
5819 if (TREE_CODE (decl) == TYPE_DECL)
5820 token->id_kind = C_ID_TYPENAME;
5822 else if (c_dialect_objc ())
5824 tree objc_interface_decl = objc_is_class_name (token->value);
5825 /* Objective-C class names are in the same namespace as
5826 variables and typedefs, and hence are shadowed by local
5827 declarations. */
5828 if (objc_interface_decl)
5830 token->value = objc_interface_decl;
5831 token->id_kind = C_ID_CLASSNAME;
5838 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5840 if-statement:
5841 if ( expression ) statement
5842 if ( expression ) statement else statement
5844 CHAIN is a vector of if-else-if conditions.
5845 IF_P is used to track whether there's a (possibly labeled) if statement
5846 which is not enclosed in braces and has an else clause. This is used to
5847 implement -Wparentheses. */
5849 static void
5850 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5852 tree block;
5853 location_t loc;
5854 tree cond;
5855 bool nested_if = false;
5856 tree first_body, second_body;
5857 bool in_if_block;
5858 tree if_stmt;
5860 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5861 token_indent_info if_tinfo
5862 = get_token_indent_info (c_parser_peek_token (parser));
5863 c_parser_consume_token (parser);
5864 block = c_begin_compound_stmt (flag_isoc99);
5865 loc = c_parser_peek_token (parser)->location;
5866 cond = c_parser_paren_condition (parser);
5867 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5869 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5870 cond = error_mark_node;
5872 in_if_block = parser->in_if_block;
5873 parser->in_if_block = true;
5874 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5875 parser->in_if_block = in_if_block;
5877 if (warn_duplicated_cond)
5878 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5880 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5882 token_indent_info else_tinfo
5883 = get_token_indent_info (c_parser_peek_token (parser));
5884 c_parser_consume_token (parser);
5885 if (warn_duplicated_cond)
5887 if (c_parser_next_token_is_keyword (parser, RID_IF)
5888 && chain == NULL)
5890 /* We've got "if (COND) else if (COND2)". Start the
5891 condition chain and add COND as the first element. */
5892 chain = new vec<tree> ();
5893 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5894 chain->safe_push (cond);
5896 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5898 /* This is if-else without subsequent if. Zap the condition
5899 chain; we would have already warned at this point. */
5900 delete chain;
5901 chain = NULL;
5904 second_body = c_parser_else_body (parser, else_tinfo, chain);
5905 /* Set IF_P to true to indicate that this if statement has an
5906 else clause. This may trigger the Wparentheses warning
5907 below when we get back up to the parent if statement. */
5908 if (if_p != NULL)
5909 *if_p = true;
5911 else
5913 second_body = NULL_TREE;
5915 /* Diagnose an ambiguous else if if-then-else is nested inside
5916 if-then. */
5917 if (nested_if)
5918 warning_at (loc, OPT_Wdangling_else,
5919 "suggest explicit braces to avoid ambiguous %<else%>");
5921 if (warn_duplicated_cond)
5923 /* This if statement does not have an else clause. We don't
5924 need the condition chain anymore. */
5925 delete chain;
5926 chain = NULL;
5929 c_finish_if_stmt (loc, cond, first_body, second_body);
5930 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5932 /* If the if statement contains array notations, then we expand them. */
5933 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5934 if_stmt = fix_conditional_array_notations (if_stmt);
5935 add_stmt (if_stmt);
5936 c_parser_maybe_reclassify_token (parser);
5939 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5941 switch-statement:
5942 switch (expression) statement
5945 static void
5946 c_parser_switch_statement (c_parser *parser, bool *if_p)
5948 struct c_expr ce;
5949 tree block, expr, body, save_break;
5950 location_t switch_loc = c_parser_peek_token (parser)->location;
5951 location_t switch_cond_loc;
5952 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5953 c_parser_consume_token (parser);
5954 block = c_begin_compound_stmt (flag_isoc99);
5955 bool explicit_cast_p = false;
5956 matching_parens parens;
5957 if (parens.require_open (parser))
5959 switch_cond_loc = c_parser_peek_token (parser)->location;
5960 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5961 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5962 explicit_cast_p = true;
5963 ce = c_parser_expression (parser);
5964 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5965 expr = ce.value;
5966 /* ??? expr has no valid location? */
5967 if (check_no_cilk (expr,
5968 "Cilk array notation cannot be used as a condition for switch statement",
5969 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5970 switch_cond_loc))
5971 expr = error_mark_node;
5972 parens.skip_until_found_close (parser);
5974 else
5976 switch_cond_loc = UNKNOWN_LOCATION;
5977 expr = error_mark_node;
5978 ce.original_type = error_mark_node;
5980 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5981 save_break = c_break_label;
5982 c_break_label = NULL_TREE;
5983 location_t loc_after_labels;
5984 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5985 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5986 location_t next_loc = c_parser_peek_token (parser)->location;
5987 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5988 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5989 RID_SWITCH);
5990 c_finish_case (body, ce.original_type);
5991 if (c_break_label)
5993 location_t here = c_parser_peek_token (parser)->location;
5994 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5995 SET_EXPR_LOCATION (t, here);
5996 add_stmt (t);
5998 c_break_label = save_break;
5999 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
6000 c_parser_maybe_reclassify_token (parser);
6003 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6005 while-statement:
6006 while (expression) statement
6008 IF_P is used to track whether there's a (possibly labeled) if statement
6009 which is not enclosed in braces and has an else clause. This is used to
6010 implement -Wparentheses. */
6012 static void
6013 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
6015 tree block, cond, body, save_break, save_cont;
6016 location_t loc;
6017 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
6018 token_indent_info while_tinfo
6019 = get_token_indent_info (c_parser_peek_token (parser));
6020 c_parser_consume_token (parser);
6021 block = c_begin_compound_stmt (flag_isoc99);
6022 loc = c_parser_peek_token (parser)->location;
6023 cond = c_parser_paren_condition (parser);
6024 if (check_no_cilk (cond,
6025 "Cilk array notation cannot be used as a condition for while statement",
6026 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
6027 cond = error_mark_node;
6028 if (ivdep && cond != error_mark_node)
6029 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6030 build_int_cst (integer_type_node,
6031 annot_expr_ivdep_kind));
6032 save_break = c_break_label;
6033 c_break_label = NULL_TREE;
6034 save_cont = c_cont_label;
6035 c_cont_label = NULL_TREE;
6037 token_indent_info body_tinfo
6038 = get_token_indent_info (c_parser_peek_token (parser));
6040 location_t loc_after_labels;
6041 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6042 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6043 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
6044 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6045 c_parser_maybe_reclassify_token (parser);
6047 token_indent_info next_tinfo
6048 = get_token_indent_info (c_parser_peek_token (parser));
6049 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
6051 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6052 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6053 while_tinfo.location, RID_WHILE);
6055 c_break_label = save_break;
6056 c_cont_label = save_cont;
6059 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6061 do-statement:
6062 do statement while ( expression ) ;
6065 static void
6066 c_parser_do_statement (c_parser *parser, bool ivdep)
6068 tree block, cond, body, save_break, save_cont, new_break, new_cont;
6069 location_t loc;
6070 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
6071 c_parser_consume_token (parser);
6072 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6073 warning_at (c_parser_peek_token (parser)->location,
6074 OPT_Wempty_body,
6075 "suggest braces around empty body in %<do%> statement");
6076 block = c_begin_compound_stmt (flag_isoc99);
6077 loc = c_parser_peek_token (parser)->location;
6078 save_break = c_break_label;
6079 c_break_label = NULL_TREE;
6080 save_cont = c_cont_label;
6081 c_cont_label = NULL_TREE;
6082 body = c_parser_c99_block_statement (parser, NULL);
6083 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
6084 new_break = c_break_label;
6085 c_break_label = save_break;
6086 new_cont = c_cont_label;
6087 c_cont_label = save_cont;
6088 cond = c_parser_paren_condition (parser);
6089 if (check_no_cilk (cond,
6090 "Cilk array notation cannot be used as a condition for a do-while statement",
6091 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
6092 cond = error_mark_node;
6093 if (ivdep && cond != error_mark_node)
6094 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6095 build_int_cst (integer_type_node,
6096 annot_expr_ivdep_kind));
6097 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6098 c_parser_skip_to_end_of_block_or_statement (parser);
6099 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
6100 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6103 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6105 for-statement:
6106 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6107 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6109 The form with a declaration is new in C99.
6111 ??? In accordance with the old parser, the declaration may be a
6112 nested function, which is then rejected in check_for_loop_decls,
6113 but does it make any sense for this to be included in the grammar?
6114 Note in particular that the nested function does not include a
6115 trailing ';', whereas the "declaration" production includes one.
6116 Also, can we reject bad declarations earlier and cheaper than
6117 check_for_loop_decls?
6119 In Objective-C, there are two additional variants:
6121 foreach-statement:
6122 for ( expression in expresssion ) statement
6123 for ( declaration in expression ) statement
6125 This is inconsistent with C, because the second variant is allowed
6126 even if c99 is not enabled.
6128 The rest of the comment documents these Objective-C foreach-statement.
6130 Here is the canonical example of the first variant:
6131 for (object in array) { do something with object }
6132 we call the first expression ("object") the "object_expression" and
6133 the second expression ("array") the "collection_expression".
6134 object_expression must be an lvalue of type "id" (a generic Objective-C
6135 object) because the loop works by assigning to object_expression the
6136 various objects from the collection_expression. collection_expression
6137 must evaluate to something of type "id" which responds to the method
6138 countByEnumeratingWithState:objects:count:.
6140 The canonical example of the second variant is:
6141 for (id object in array) { do something with object }
6142 which is completely equivalent to
6144 id object;
6145 for (object in array) { do something with object }
6147 Note that initizializing 'object' in some way (eg, "for ((object =
6148 xxx) in array) { do something with object }") is possibly
6149 technically valid, but completely pointless as 'object' will be
6150 assigned to something else as soon as the loop starts. We should
6151 most likely reject it (TODO).
6153 The beginning of the Objective-C foreach-statement looks exactly
6154 like the beginning of the for-statement, and we can tell it is a
6155 foreach-statement only because the initial declaration or
6156 expression is terminated by 'in' instead of ';'.
6158 IF_P is used to track whether there's a (possibly labeled) if statement
6159 which is not enclosed in braces and has an else clause. This is used to
6160 implement -Wparentheses. */
6162 static void
6163 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
6165 tree block, cond, incr, save_break, save_cont, body;
6166 /* The following are only used when parsing an ObjC foreach statement. */
6167 tree object_expression;
6168 /* Silence the bogus uninitialized warning. */
6169 tree collection_expression = NULL;
6170 location_t loc = c_parser_peek_token (parser)->location;
6171 location_t for_loc = c_parser_peek_token (parser)->location;
6172 bool is_foreach_statement = false;
6173 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6174 token_indent_info for_tinfo
6175 = get_token_indent_info (c_parser_peek_token (parser));
6176 c_parser_consume_token (parser);
6177 /* Open a compound statement in Objective-C as well, just in case this is
6178 as foreach expression. */
6179 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6180 cond = error_mark_node;
6181 incr = error_mark_node;
6182 matching_parens parens;
6183 if (parens.require_open (parser))
6185 /* Parse the initialization declaration or expression. */
6186 object_expression = error_mark_node;
6187 parser->objc_could_be_foreach_context = c_dialect_objc ();
6188 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6190 parser->objc_could_be_foreach_context = false;
6191 c_parser_consume_token (parser);
6192 c_finish_expr_stmt (loc, NULL_TREE);
6194 else if (c_parser_next_tokens_start_declaration (parser))
6196 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6197 &object_expression, vNULL);
6198 parser->objc_could_be_foreach_context = false;
6200 if (c_parser_next_token_is_keyword (parser, RID_IN))
6202 c_parser_consume_token (parser);
6203 is_foreach_statement = true;
6204 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6205 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6207 else
6208 check_for_loop_decls (for_loc, flag_isoc99);
6210 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6212 /* __extension__ can start a declaration, but is also an
6213 unary operator that can start an expression. Consume all
6214 but the last of a possible series of __extension__ to
6215 determine which. */
6216 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6217 && (c_parser_peek_2nd_token (parser)->keyword
6218 == RID_EXTENSION))
6219 c_parser_consume_token (parser);
6220 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6222 int ext;
6223 ext = disable_extension_diagnostics ();
6224 c_parser_consume_token (parser);
6225 c_parser_declaration_or_fndef (parser, true, true, true, true,
6226 true, &object_expression, vNULL);
6227 parser->objc_could_be_foreach_context = false;
6229 restore_extension_diagnostics (ext);
6230 if (c_parser_next_token_is_keyword (parser, RID_IN))
6232 c_parser_consume_token (parser);
6233 is_foreach_statement = true;
6234 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6235 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6237 else
6238 check_for_loop_decls (for_loc, flag_isoc99);
6240 else
6241 goto init_expr;
6243 else
6245 init_expr:
6247 struct c_expr ce;
6248 tree init_expression;
6249 ce = c_parser_expression (parser);
6250 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
6251 level statement", but it works just fine, so allow it. */
6252 init_expression = ce.value;
6253 parser->objc_could_be_foreach_context = false;
6254 if (c_parser_next_token_is_keyword (parser, RID_IN))
6256 c_parser_consume_token (parser);
6257 is_foreach_statement = true;
6258 if (! lvalue_p (init_expression))
6259 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6260 object_expression = c_fully_fold (init_expression, false, NULL);
6262 else
6264 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6265 init_expression = ce.value;
6266 c_finish_expr_stmt (loc, init_expression);
6267 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6271 /* Parse the loop condition. In the case of a foreach
6272 statement, there is no loop condition. */
6273 gcc_assert (!parser->objc_could_be_foreach_context);
6274 if (!is_foreach_statement)
6276 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6278 if (ivdep)
6280 c_parser_error (parser, "missing loop condition in loop with "
6281 "%<GCC ivdep%> pragma");
6282 cond = error_mark_node;
6284 else
6286 c_parser_consume_token (parser);
6287 cond = NULL_TREE;
6290 else
6292 cond = c_parser_condition (parser);
6293 if (check_no_cilk (cond,
6294 "Cilk array notation cannot be used in a condition for a for-loop",
6295 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
6296 cond = error_mark_node;
6297 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6298 "expected %<;%>");
6300 if (ivdep && cond != error_mark_node)
6301 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6302 build_int_cst (integer_type_node,
6303 annot_expr_ivdep_kind));
6305 /* Parse the increment expression (the third expression in a
6306 for-statement). In the case of a foreach-statement, this is
6307 the expression that follows the 'in'. */
6308 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6310 if (is_foreach_statement)
6312 c_parser_error (parser, "missing collection in fast enumeration");
6313 collection_expression = error_mark_node;
6315 else
6316 incr = c_process_expr_stmt (loc, NULL_TREE);
6318 else
6320 if (is_foreach_statement)
6321 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6322 false, NULL);
6323 else
6325 struct c_expr ce = c_parser_expression (parser);
6326 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6327 incr = c_process_expr_stmt (loc, ce.value);
6330 parens.skip_until_found_close (parser);
6332 save_break = c_break_label;
6333 c_break_label = NULL_TREE;
6334 save_cont = c_cont_label;
6335 c_cont_label = NULL_TREE;
6337 token_indent_info body_tinfo
6338 = get_token_indent_info (c_parser_peek_token (parser));
6340 location_t loc_after_labels;
6341 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6342 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6344 if (is_foreach_statement)
6345 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6346 else
6347 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6348 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6349 c_parser_maybe_reclassify_token (parser);
6351 token_indent_info next_tinfo
6352 = get_token_indent_info (c_parser_peek_token (parser));
6353 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6355 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6356 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6357 for_tinfo.location, RID_FOR);
6359 c_break_label = save_break;
6360 c_cont_label = save_cont;
6363 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6364 statement with inputs, outputs, clobbers, and volatile tag
6365 allowed.
6367 asm-statement:
6368 asm type-qualifier[opt] ( asm-argument ) ;
6369 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6371 asm-argument:
6372 asm-string-literal
6373 asm-string-literal : asm-operands[opt]
6374 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6375 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6377 asm-goto-argument:
6378 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6379 : asm-goto-operands
6381 Qualifiers other than volatile are accepted in the syntax but
6382 warned for. */
6384 static tree
6385 c_parser_asm_statement (c_parser *parser)
6387 tree quals, str, outputs, inputs, clobbers, labels, ret;
6388 bool simple, is_goto;
6389 location_t asm_loc = c_parser_peek_token (parser)->location;
6390 int section, nsections;
6392 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6393 c_parser_consume_token (parser);
6394 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6396 quals = c_parser_peek_token (parser)->value;
6397 c_parser_consume_token (parser);
6399 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6400 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6402 warning_at (c_parser_peek_token (parser)->location,
6404 "%E qualifier ignored on asm",
6405 c_parser_peek_token (parser)->value);
6406 quals = NULL_TREE;
6407 c_parser_consume_token (parser);
6409 else
6410 quals = NULL_TREE;
6412 is_goto = false;
6413 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6415 c_parser_consume_token (parser);
6416 is_goto = true;
6419 /* ??? Follow the C++ parser rather than using the
6420 lex_untranslated_string kludge. */
6421 parser->lex_untranslated_string = true;
6422 ret = NULL;
6424 matching_parens parens;
6425 if (!parens.require_open (parser))
6426 goto error;
6428 str = c_parser_asm_string_literal (parser);
6429 if (str == NULL_TREE)
6430 goto error_close_paren;
6432 simple = true;
6433 outputs = NULL_TREE;
6434 inputs = NULL_TREE;
6435 clobbers = NULL_TREE;
6436 labels = NULL_TREE;
6438 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6439 goto done_asm;
6441 /* Parse each colon-delimited section of operands. */
6442 nsections = 3 + is_goto;
6443 for (section = 0; section < nsections; ++section)
6445 if (!c_parser_require (parser, CPP_COLON,
6446 is_goto
6447 ? G_("expected %<:%>")
6448 : G_("expected %<:%> or %<)%>"),
6449 UNKNOWN_LOCATION, is_goto))
6450 goto error_close_paren;
6452 /* Once past any colon, we're no longer a simple asm. */
6453 simple = false;
6455 if ((!c_parser_next_token_is (parser, CPP_COLON)
6456 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6457 || section == 3)
6458 switch (section)
6460 case 0:
6461 /* For asm goto, we don't allow output operands, but reserve
6462 the slot for a future extension that does allow them. */
6463 if (!is_goto)
6464 outputs = c_parser_asm_operands (parser);
6465 break;
6466 case 1:
6467 inputs = c_parser_asm_operands (parser);
6468 break;
6469 case 2:
6470 clobbers = c_parser_asm_clobbers (parser);
6471 break;
6472 case 3:
6473 labels = c_parser_asm_goto_operands (parser);
6474 break;
6475 default:
6476 gcc_unreachable ();
6479 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6480 goto done_asm;
6483 done_asm:
6484 if (!parens.require_close (parser))
6486 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6487 goto error;
6490 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6491 c_parser_skip_to_end_of_block_or_statement (parser);
6493 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6494 clobbers, labels, simple));
6496 error:
6497 parser->lex_untranslated_string = false;
6498 return ret;
6500 error_close_paren:
6501 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6502 goto error;
6505 /* Parse asm operands, a GNU extension.
6507 asm-operands:
6508 asm-operand
6509 asm-operands , asm-operand
6511 asm-operand:
6512 asm-string-literal ( expression )
6513 [ identifier ] asm-string-literal ( expression )
6516 static tree
6517 c_parser_asm_operands (c_parser *parser)
6519 tree list = NULL_TREE;
6520 while (true)
6522 tree name, str;
6523 struct c_expr expr;
6524 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6526 c_parser_consume_token (parser);
6527 if (c_parser_next_token_is (parser, CPP_NAME))
6529 tree id = c_parser_peek_token (parser)->value;
6530 c_parser_consume_token (parser);
6531 name = build_string (IDENTIFIER_LENGTH (id),
6532 IDENTIFIER_POINTER (id));
6534 else
6536 c_parser_error (parser, "expected identifier");
6537 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6538 return NULL_TREE;
6540 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6541 "expected %<]%>");
6543 else
6544 name = NULL_TREE;
6545 str = c_parser_asm_string_literal (parser);
6546 if (str == NULL_TREE)
6547 return NULL_TREE;
6548 parser->lex_untranslated_string = false;
6549 matching_parens parens;
6550 if (!parens.require_open (parser))
6552 parser->lex_untranslated_string = true;
6553 return NULL_TREE;
6555 expr = c_parser_expression (parser);
6556 mark_exp_read (expr.value);
6557 parser->lex_untranslated_string = true;
6558 if (!parens.require_close (parser))
6560 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6561 return NULL_TREE;
6563 list = chainon (list, build_tree_list (build_tree_list (name, str),
6564 expr.value));
6565 if (c_parser_next_token_is (parser, CPP_COMMA))
6566 c_parser_consume_token (parser);
6567 else
6568 break;
6570 return list;
6573 /* Parse asm clobbers, a GNU extension.
6575 asm-clobbers:
6576 asm-string-literal
6577 asm-clobbers , asm-string-literal
6580 static tree
6581 c_parser_asm_clobbers (c_parser *parser)
6583 tree list = NULL_TREE;
6584 while (true)
6586 tree str = c_parser_asm_string_literal (parser);
6587 if (str)
6588 list = tree_cons (NULL_TREE, str, list);
6589 else
6590 return NULL_TREE;
6591 if (c_parser_next_token_is (parser, CPP_COMMA))
6592 c_parser_consume_token (parser);
6593 else
6594 break;
6596 return list;
6599 /* Parse asm goto labels, a GNU extension.
6601 asm-goto-operands:
6602 identifier
6603 asm-goto-operands , identifier
6606 static tree
6607 c_parser_asm_goto_operands (c_parser *parser)
6609 tree list = NULL_TREE;
6610 while (true)
6612 tree name, label;
6614 if (c_parser_next_token_is (parser, CPP_NAME))
6616 c_token *tok = c_parser_peek_token (parser);
6617 name = tok->value;
6618 label = lookup_label_for_goto (tok->location, name);
6619 c_parser_consume_token (parser);
6620 TREE_USED (label) = 1;
6622 else
6624 c_parser_error (parser, "expected identifier");
6625 return NULL_TREE;
6628 name = build_string (IDENTIFIER_LENGTH (name),
6629 IDENTIFIER_POINTER (name));
6630 list = tree_cons (name, label, list);
6631 if (c_parser_next_token_is (parser, CPP_COMMA))
6632 c_parser_consume_token (parser);
6633 else
6634 return nreverse (list);
6638 /* Parse an expression other than a compound expression; that is, an
6639 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6640 AFTER is not NULL then it is an Objective-C message expression which
6641 is the primary-expression starting the expression as an initializer.
6643 assignment-expression:
6644 conditional-expression
6645 unary-expression assignment-operator assignment-expression
6647 assignment-operator: one of
6648 = *= /= %= += -= <<= >>= &= ^= |=
6650 In GNU C we accept any conditional expression on the LHS and
6651 diagnose the invalid lvalue rather than producing a syntax
6652 error. */
6654 static struct c_expr
6655 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6656 tree omp_atomic_lhs)
6658 struct c_expr lhs, rhs, ret;
6659 enum tree_code code;
6660 location_t op_location, exp_location;
6661 gcc_assert (!after || c_dialect_objc ());
6662 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6663 op_location = c_parser_peek_token (parser)->location;
6664 switch (c_parser_peek_token (parser)->type)
6666 case CPP_EQ:
6667 code = NOP_EXPR;
6668 break;
6669 case CPP_MULT_EQ:
6670 code = MULT_EXPR;
6671 break;
6672 case CPP_DIV_EQ:
6673 code = TRUNC_DIV_EXPR;
6674 break;
6675 case CPP_MOD_EQ:
6676 code = TRUNC_MOD_EXPR;
6677 break;
6678 case CPP_PLUS_EQ:
6679 code = PLUS_EXPR;
6680 break;
6681 case CPP_MINUS_EQ:
6682 code = MINUS_EXPR;
6683 break;
6684 case CPP_LSHIFT_EQ:
6685 code = LSHIFT_EXPR;
6686 break;
6687 case CPP_RSHIFT_EQ:
6688 code = RSHIFT_EXPR;
6689 break;
6690 case CPP_AND_EQ:
6691 code = BIT_AND_EXPR;
6692 break;
6693 case CPP_XOR_EQ:
6694 code = BIT_XOR_EXPR;
6695 break;
6696 case CPP_OR_EQ:
6697 code = BIT_IOR_EXPR;
6698 break;
6699 default:
6700 return lhs;
6702 c_parser_consume_token (parser);
6703 exp_location = c_parser_peek_token (parser)->location;
6704 rhs = c_parser_expr_no_commas (parser, NULL);
6705 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6707 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6708 code, exp_location, rhs.value,
6709 rhs.original_type);
6710 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6711 if (code == NOP_EXPR)
6712 ret.original_code = MODIFY_EXPR;
6713 else
6715 TREE_NO_WARNING (ret.value) = 1;
6716 ret.original_code = ERROR_MARK;
6718 ret.original_type = NULL;
6719 return ret;
6722 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6723 AFTER is not NULL then it is an Objective-C message expression which is
6724 the primary-expression starting the expression as an initializer.
6726 conditional-expression:
6727 logical-OR-expression
6728 logical-OR-expression ? expression : conditional-expression
6730 GNU extensions:
6732 conditional-expression:
6733 logical-OR-expression ? : conditional-expression
6736 static struct c_expr
6737 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6738 tree omp_atomic_lhs)
6740 struct c_expr cond, exp1, exp2, ret;
6741 location_t start, cond_loc, colon_loc;
6743 gcc_assert (!after || c_dialect_objc ());
6745 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6747 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6748 return cond;
6749 if (cond.value != error_mark_node)
6750 start = cond.get_start ();
6751 else
6752 start = UNKNOWN_LOCATION;
6753 cond_loc = c_parser_peek_token (parser)->location;
6754 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6755 c_parser_consume_token (parser);
6756 if (c_parser_next_token_is (parser, CPP_COLON))
6758 tree eptype = NULL_TREE;
6760 location_t middle_loc = c_parser_peek_token (parser)->location;
6761 pedwarn (middle_loc, OPT_Wpedantic,
6762 "ISO C forbids omitting the middle term of a ?: expression");
6763 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6765 eptype = TREE_TYPE (cond.value);
6766 cond.value = TREE_OPERAND (cond.value, 0);
6768 tree e = cond.value;
6769 while (TREE_CODE (e) == COMPOUND_EXPR)
6770 e = TREE_OPERAND (e, 1);
6771 warn_for_omitted_condop (middle_loc, e);
6772 /* Make sure first operand is calculated only once. */
6773 exp1.value = save_expr (default_conversion (cond.value));
6774 if (eptype)
6775 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6776 exp1.original_type = NULL;
6777 exp1.src_range = cond.src_range;
6778 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6779 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6781 else
6783 cond.value
6784 = c_objc_common_truthvalue_conversion
6785 (cond_loc, default_conversion (cond.value));
6786 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6787 exp1 = c_parser_expression_conv (parser);
6788 mark_exp_read (exp1.value);
6789 c_inhibit_evaluation_warnings +=
6790 ((cond.value == truthvalue_true_node)
6791 - (cond.value == truthvalue_false_node));
6794 colon_loc = c_parser_peek_token (parser)->location;
6795 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6797 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6798 ret.value = error_mark_node;
6799 ret.original_code = ERROR_MARK;
6800 ret.original_type = NULL;
6801 return ret;
6804 location_t exp2_loc = c_parser_peek_token (parser)->location;
6805 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6806 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6808 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6809 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6810 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6811 ret.value = build_conditional_expr (colon_loc, cond.value,
6812 cond.original_code == C_MAYBE_CONST_EXPR,
6813 exp1.value, exp1.original_type, loc1,
6814 exp2.value, exp2.original_type, loc2);
6815 ret.original_code = ERROR_MARK;
6816 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6817 ret.original_type = NULL;
6818 else
6820 tree t1, t2;
6822 /* If both sides are enum type, the default conversion will have
6823 made the type of the result be an integer type. We want to
6824 remember the enum types we started with. */
6825 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6826 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6827 ret.original_type = ((t1 != error_mark_node
6828 && t2 != error_mark_node
6829 && (TYPE_MAIN_VARIANT (t1)
6830 == TYPE_MAIN_VARIANT (t2)))
6831 ? t1
6832 : NULL);
6834 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6835 return ret;
6838 /* Parse a binary expression; that is, a logical-OR-expression (C90
6839 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6840 NULL then it is an Objective-C message expression which is the
6841 primary-expression starting the expression as an initializer.
6843 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6844 when it should be the unfolded lhs. In a valid OpenMP source,
6845 one of the operands of the toplevel binary expression must be equal
6846 to it. In that case, just return a build2 created binary operation
6847 rather than result of parser_build_binary_op.
6849 multiplicative-expression:
6850 cast-expression
6851 multiplicative-expression * cast-expression
6852 multiplicative-expression / cast-expression
6853 multiplicative-expression % cast-expression
6855 additive-expression:
6856 multiplicative-expression
6857 additive-expression + multiplicative-expression
6858 additive-expression - multiplicative-expression
6860 shift-expression:
6861 additive-expression
6862 shift-expression << additive-expression
6863 shift-expression >> additive-expression
6865 relational-expression:
6866 shift-expression
6867 relational-expression < shift-expression
6868 relational-expression > shift-expression
6869 relational-expression <= shift-expression
6870 relational-expression >= shift-expression
6872 equality-expression:
6873 relational-expression
6874 equality-expression == relational-expression
6875 equality-expression != relational-expression
6877 AND-expression:
6878 equality-expression
6879 AND-expression & equality-expression
6881 exclusive-OR-expression:
6882 AND-expression
6883 exclusive-OR-expression ^ AND-expression
6885 inclusive-OR-expression:
6886 exclusive-OR-expression
6887 inclusive-OR-expression | exclusive-OR-expression
6889 logical-AND-expression:
6890 inclusive-OR-expression
6891 logical-AND-expression && inclusive-OR-expression
6893 logical-OR-expression:
6894 logical-AND-expression
6895 logical-OR-expression || logical-AND-expression
6898 static struct c_expr
6899 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6900 tree omp_atomic_lhs)
6902 /* A binary expression is parsed using operator-precedence parsing,
6903 with the operands being cast expressions. All the binary
6904 operators are left-associative. Thus a binary expression is of
6905 form:
6907 E0 op1 E1 op2 E2 ...
6909 which we represent on a stack. On the stack, the precedence
6910 levels are strictly increasing. When a new operator is
6911 encountered of higher precedence than that at the top of the
6912 stack, it is pushed; its LHS is the top expression, and its RHS
6913 is everything parsed until it is popped. When a new operator is
6914 encountered with precedence less than or equal to that at the top
6915 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6916 by the result of the operation until the operator at the top of
6917 the stack has lower precedence than the new operator or there is
6918 only one element on the stack; then the top expression is the LHS
6919 of the new operator. In the case of logical AND and OR
6920 expressions, we also need to adjust c_inhibit_evaluation_warnings
6921 as appropriate when the operators are pushed and popped. */
6923 struct {
6924 /* The expression at this stack level. */
6925 struct c_expr expr;
6926 /* The precedence of the operator on its left, PREC_NONE at the
6927 bottom of the stack. */
6928 enum c_parser_prec prec;
6929 /* The operation on its left. */
6930 enum tree_code op;
6931 /* The source location of this operation. */
6932 location_t loc;
6933 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6934 tree sizeof_arg;
6935 } stack[NUM_PRECS];
6936 int sp;
6937 /* Location of the binary operator. */
6938 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6939 #define POP \
6940 do { \
6941 switch (stack[sp].op) \
6943 case TRUTH_ANDIF_EXPR: \
6944 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6945 == truthvalue_false_node); \
6946 break; \
6947 case TRUTH_ORIF_EXPR: \
6948 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6949 == truthvalue_true_node); \
6950 break; \
6951 case TRUNC_DIV_EXPR: \
6952 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6953 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6955 tree type0 = stack[sp - 1].sizeof_arg; \
6956 tree type1 = stack[sp].sizeof_arg; \
6957 tree first_arg = type0; \
6958 if (!TYPE_P (type0)) \
6959 type0 = TREE_TYPE (type0); \
6960 if (!TYPE_P (type1)) \
6961 type1 = TREE_TYPE (type1); \
6962 if (POINTER_TYPE_P (type0) \
6963 && comptypes (TREE_TYPE (type0), type1) \
6964 && !(TREE_CODE (first_arg) == PARM_DECL \
6965 && C_ARRAY_PARAMETER (first_arg) \
6966 && warn_sizeof_array_argument)) \
6967 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6968 "division %<sizeof (%T) / sizeof (%T)%> does " \
6969 "not compute the number of array elements", \
6970 type0, type1)) \
6971 if (DECL_P (first_arg)) \
6972 inform (DECL_SOURCE_LOCATION (first_arg), \
6973 "first %<sizeof%> operand was declared here"); \
6975 break; \
6976 default: \
6977 break; \
6979 stack[sp - 1].expr \
6980 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6981 stack[sp - 1].expr, true, true); \
6982 stack[sp].expr \
6983 = convert_lvalue_to_rvalue (stack[sp].loc, \
6984 stack[sp].expr, true, true); \
6985 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6986 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6987 && ((1 << stack[sp].prec) \
6988 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6989 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6990 && stack[sp].op != TRUNC_MOD_EXPR \
6991 && stack[0].expr.value != error_mark_node \
6992 && stack[1].expr.value != error_mark_node \
6993 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6994 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6995 stack[0].expr.value \
6996 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6997 stack[0].expr.value, stack[1].expr.value); \
6998 else \
6999 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
7000 stack[sp].op, \
7001 stack[sp - 1].expr, \
7002 stack[sp].expr); \
7003 sp--; \
7004 } while (0)
7005 gcc_assert (!after || c_dialect_objc ());
7006 stack[0].loc = c_parser_peek_token (parser)->location;
7007 stack[0].expr = c_parser_cast_expression (parser, after);
7008 stack[0].prec = PREC_NONE;
7009 stack[0].sizeof_arg = c_last_sizeof_arg;
7010 sp = 0;
7011 while (true)
7013 enum c_parser_prec oprec;
7014 enum tree_code ocode;
7015 source_range src_range;
7016 if (parser->error)
7017 goto out;
7018 switch (c_parser_peek_token (parser)->type)
7020 case CPP_MULT:
7021 oprec = PREC_MULT;
7022 ocode = MULT_EXPR;
7023 break;
7024 case CPP_DIV:
7025 oprec = PREC_MULT;
7026 ocode = TRUNC_DIV_EXPR;
7027 break;
7028 case CPP_MOD:
7029 oprec = PREC_MULT;
7030 ocode = TRUNC_MOD_EXPR;
7031 break;
7032 case CPP_PLUS:
7033 oprec = PREC_ADD;
7034 ocode = PLUS_EXPR;
7035 break;
7036 case CPP_MINUS:
7037 oprec = PREC_ADD;
7038 ocode = MINUS_EXPR;
7039 break;
7040 case CPP_LSHIFT:
7041 oprec = PREC_SHIFT;
7042 ocode = LSHIFT_EXPR;
7043 break;
7044 case CPP_RSHIFT:
7045 oprec = PREC_SHIFT;
7046 ocode = RSHIFT_EXPR;
7047 break;
7048 case CPP_LESS:
7049 oprec = PREC_REL;
7050 ocode = LT_EXPR;
7051 break;
7052 case CPP_GREATER:
7053 oprec = PREC_REL;
7054 ocode = GT_EXPR;
7055 break;
7056 case CPP_LESS_EQ:
7057 oprec = PREC_REL;
7058 ocode = LE_EXPR;
7059 break;
7060 case CPP_GREATER_EQ:
7061 oprec = PREC_REL;
7062 ocode = GE_EXPR;
7063 break;
7064 case CPP_EQ_EQ:
7065 oprec = PREC_EQ;
7066 ocode = EQ_EXPR;
7067 break;
7068 case CPP_NOT_EQ:
7069 oprec = PREC_EQ;
7070 ocode = NE_EXPR;
7071 break;
7072 case CPP_AND:
7073 oprec = PREC_BITAND;
7074 ocode = BIT_AND_EXPR;
7075 break;
7076 case CPP_XOR:
7077 oprec = PREC_BITXOR;
7078 ocode = BIT_XOR_EXPR;
7079 break;
7080 case CPP_OR:
7081 oprec = PREC_BITOR;
7082 ocode = BIT_IOR_EXPR;
7083 break;
7084 case CPP_AND_AND:
7085 oprec = PREC_LOGAND;
7086 ocode = TRUTH_ANDIF_EXPR;
7087 break;
7088 case CPP_OR_OR:
7089 oprec = PREC_LOGOR;
7090 ocode = TRUTH_ORIF_EXPR;
7091 break;
7092 default:
7093 /* Not a binary operator, so end of the binary
7094 expression. */
7095 goto out;
7097 binary_loc = c_parser_peek_token (parser)->location;
7098 while (oprec <= stack[sp].prec)
7099 POP;
7100 c_parser_consume_token (parser);
7101 switch (ocode)
7103 case TRUTH_ANDIF_EXPR:
7104 src_range = stack[sp].expr.src_range;
7105 stack[sp].expr
7106 = convert_lvalue_to_rvalue (stack[sp].loc,
7107 stack[sp].expr, true, true);
7108 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7109 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7110 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7111 == truthvalue_false_node);
7112 set_c_expr_source_range (&stack[sp].expr, src_range);
7113 break;
7114 case TRUTH_ORIF_EXPR:
7115 src_range = stack[sp].expr.src_range;
7116 stack[sp].expr
7117 = convert_lvalue_to_rvalue (stack[sp].loc,
7118 stack[sp].expr, true, true);
7119 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7120 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7121 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7122 == truthvalue_true_node);
7123 set_c_expr_source_range (&stack[sp].expr, src_range);
7124 break;
7125 default:
7126 break;
7128 sp++;
7129 stack[sp].loc = binary_loc;
7130 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7131 stack[sp].prec = oprec;
7132 stack[sp].op = ocode;
7133 stack[sp].sizeof_arg = c_last_sizeof_arg;
7135 out:
7136 while (sp > 0)
7137 POP;
7138 return stack[0].expr;
7139 #undef POP
7142 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7143 is not NULL then it is an Objective-C message expression which is the
7144 primary-expression starting the expression as an initializer.
7146 cast-expression:
7147 unary-expression
7148 ( type-name ) unary-expression
7151 static struct c_expr
7152 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7154 location_t cast_loc = c_parser_peek_token (parser)->location;
7155 gcc_assert (!after || c_dialect_objc ());
7156 if (after)
7157 return c_parser_postfix_expression_after_primary (parser,
7158 cast_loc, *after);
7159 /* If the expression begins with a parenthesized type name, it may
7160 be either a cast or a compound literal; we need to see whether
7161 the next character is '{' to tell the difference. If not, it is
7162 an unary expression. Full detection of unknown typenames here
7163 would require a 3-token lookahead. */
7164 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7165 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7167 struct c_type_name *type_name;
7168 struct c_expr ret;
7169 struct c_expr expr;
7170 matching_parens parens;
7171 parens.consume_open (parser);
7172 type_name = c_parser_type_name (parser);
7173 parens.skip_until_found_close (parser);
7174 if (type_name == NULL)
7176 ret.value = error_mark_node;
7177 ret.original_code = ERROR_MARK;
7178 ret.original_type = NULL;
7179 return ret;
7182 /* Save casted types in the function's used types hash table. */
7183 used_types_insert (type_name->specs->type);
7185 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7186 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7187 cast_loc);
7189 location_t expr_loc = c_parser_peek_token (parser)->location;
7190 expr = c_parser_cast_expression (parser, NULL);
7191 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7193 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7194 if (ret.value && expr.value)
7195 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7196 ret.original_code = ERROR_MARK;
7197 ret.original_type = NULL;
7198 return ret;
7200 else
7201 return c_parser_unary_expression (parser);
7204 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7206 unary-expression:
7207 postfix-expression
7208 ++ unary-expression
7209 -- unary-expression
7210 unary-operator cast-expression
7211 sizeof unary-expression
7212 sizeof ( type-name )
7214 unary-operator: one of
7215 & * + - ~ !
7217 GNU extensions:
7219 unary-expression:
7220 __alignof__ unary-expression
7221 __alignof__ ( type-name )
7222 && identifier
7224 (C11 permits _Alignof with type names only.)
7226 unary-operator: one of
7227 __extension__ __real__ __imag__
7229 Transactional Memory:
7231 unary-expression:
7232 transaction-expression
7234 In addition, the GNU syntax treats ++ and -- as unary operators, so
7235 they may be applied to cast expressions with errors for non-lvalues
7236 given later. */
7238 static struct c_expr
7239 c_parser_unary_expression (c_parser *parser)
7241 int ext;
7242 struct c_expr ret, op;
7243 location_t op_loc = c_parser_peek_token (parser)->location;
7244 location_t exp_loc;
7245 location_t finish;
7246 ret.original_code = ERROR_MARK;
7247 ret.original_type = NULL;
7248 switch (c_parser_peek_token (parser)->type)
7250 case CPP_PLUS_PLUS:
7251 c_parser_consume_token (parser);
7252 exp_loc = c_parser_peek_token (parser)->location;
7253 op = c_parser_cast_expression (parser, NULL);
7255 /* If there is array notations in op, we expand them. */
7256 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
7257 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
7258 else
7260 op = default_function_array_read_conversion (exp_loc, op);
7261 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7263 case CPP_MINUS_MINUS:
7264 c_parser_consume_token (parser);
7265 exp_loc = c_parser_peek_token (parser)->location;
7266 op = c_parser_cast_expression (parser, NULL);
7268 /* If there is array notations in op, we expand them. */
7269 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
7270 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
7271 else
7273 op = default_function_array_read_conversion (exp_loc, op);
7274 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7276 case CPP_AND:
7277 c_parser_consume_token (parser);
7278 op = c_parser_cast_expression (parser, NULL);
7279 mark_exp_read (op.value);
7280 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7281 case CPP_MULT:
7283 c_parser_consume_token (parser);
7284 exp_loc = c_parser_peek_token (parser)->location;
7285 op = c_parser_cast_expression (parser, NULL);
7286 finish = op.get_finish ();
7287 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7288 location_t combined_loc = make_location (op_loc, op_loc, finish);
7289 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7290 ret.src_range.m_start = op_loc;
7291 ret.src_range.m_finish = finish;
7292 return ret;
7294 case CPP_PLUS:
7295 if (!c_dialect_objc () && !in_system_header_at (input_location))
7296 warning_at (op_loc,
7297 OPT_Wtraditional,
7298 "traditional C rejects the unary plus operator");
7299 c_parser_consume_token (parser);
7300 exp_loc = c_parser_peek_token (parser)->location;
7301 op = c_parser_cast_expression (parser, NULL);
7302 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7303 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7304 case CPP_MINUS:
7305 c_parser_consume_token (parser);
7306 exp_loc = c_parser_peek_token (parser)->location;
7307 op = c_parser_cast_expression (parser, NULL);
7308 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7309 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7310 case CPP_COMPL:
7311 c_parser_consume_token (parser);
7312 exp_loc = c_parser_peek_token (parser)->location;
7313 op = c_parser_cast_expression (parser, NULL);
7314 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7315 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7316 case CPP_NOT:
7317 c_parser_consume_token (parser);
7318 exp_loc = c_parser_peek_token (parser)->location;
7319 op = c_parser_cast_expression (parser, NULL);
7320 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7321 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7322 case CPP_AND_AND:
7323 /* Refer to the address of a label as a pointer. */
7324 c_parser_consume_token (parser);
7325 if (c_parser_next_token_is (parser, CPP_NAME))
7327 ret.value = finish_label_address_expr
7328 (c_parser_peek_token (parser)->value, op_loc);
7329 set_c_expr_source_range (&ret, op_loc,
7330 c_parser_peek_token (parser)->get_finish ());
7331 c_parser_consume_token (parser);
7333 else
7335 c_parser_error (parser, "expected identifier");
7336 ret.set_error ();
7338 return ret;
7339 case CPP_KEYWORD:
7340 switch (c_parser_peek_token (parser)->keyword)
7342 case RID_SIZEOF:
7343 return c_parser_sizeof_expression (parser);
7344 case RID_ALIGNOF:
7345 return c_parser_alignof_expression (parser);
7346 case RID_EXTENSION:
7347 c_parser_consume_token (parser);
7348 ext = disable_extension_diagnostics ();
7349 ret = c_parser_cast_expression (parser, NULL);
7350 restore_extension_diagnostics (ext);
7351 return ret;
7352 case RID_REALPART:
7353 c_parser_consume_token (parser);
7354 exp_loc = c_parser_peek_token (parser)->location;
7355 op = c_parser_cast_expression (parser, NULL);
7356 op = default_function_array_conversion (exp_loc, op);
7357 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7358 case RID_IMAGPART:
7359 c_parser_consume_token (parser);
7360 exp_loc = c_parser_peek_token (parser)->location;
7361 op = c_parser_cast_expression (parser, NULL);
7362 op = default_function_array_conversion (exp_loc, op);
7363 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7364 case RID_TRANSACTION_ATOMIC:
7365 case RID_TRANSACTION_RELAXED:
7366 return c_parser_transaction_expression (parser,
7367 c_parser_peek_token (parser)->keyword);
7368 default:
7369 return c_parser_postfix_expression (parser);
7371 default:
7372 return c_parser_postfix_expression (parser);
7376 /* Parse a sizeof expression. */
7378 static struct c_expr
7379 c_parser_sizeof_expression (c_parser *parser)
7381 struct c_expr expr;
7382 struct c_expr result;
7383 location_t expr_loc;
7384 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7386 location_t start;
7387 location_t finish = UNKNOWN_LOCATION;
7389 start = c_parser_peek_token (parser)->location;
7391 c_parser_consume_token (parser);
7392 c_inhibit_evaluation_warnings++;
7393 in_sizeof++;
7394 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7395 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7397 /* Either sizeof ( type-name ) or sizeof unary-expression
7398 starting with a compound literal. */
7399 struct c_type_name *type_name;
7400 matching_parens parens;
7401 parens.consume_open (parser);
7402 expr_loc = c_parser_peek_token (parser)->location;
7403 type_name = c_parser_type_name (parser);
7404 parens.skip_until_found_close (parser);
7405 finish = parser->tokens_buf[0].location;
7406 if (type_name == NULL)
7408 struct c_expr ret;
7409 c_inhibit_evaluation_warnings--;
7410 in_sizeof--;
7411 ret.value = error_mark_node;
7412 ret.original_code = ERROR_MARK;
7413 ret.original_type = NULL;
7414 return ret;
7416 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7418 expr = c_parser_postfix_expression_after_paren_type (parser,
7419 type_name,
7420 expr_loc);
7421 finish = expr.get_finish ();
7422 goto sizeof_expr;
7424 /* sizeof ( type-name ). */
7425 c_inhibit_evaluation_warnings--;
7426 in_sizeof--;
7427 result = c_expr_sizeof_type (expr_loc, type_name);
7429 else
7431 expr_loc = c_parser_peek_token (parser)->location;
7432 expr = c_parser_unary_expression (parser);
7433 finish = expr.get_finish ();
7434 sizeof_expr:
7435 c_inhibit_evaluation_warnings--;
7436 in_sizeof--;
7437 mark_exp_read (expr.value);
7438 if (TREE_CODE (expr.value) == COMPONENT_REF
7439 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7440 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7441 result = c_expr_sizeof_expr (expr_loc, expr);
7443 if (finish != UNKNOWN_LOCATION)
7444 set_c_expr_source_range (&result, start, finish);
7445 return result;
7448 /* Parse an alignof expression. */
7450 static struct c_expr
7451 c_parser_alignof_expression (c_parser *parser)
7453 struct c_expr expr;
7454 location_t start_loc = c_parser_peek_token (parser)->location;
7455 location_t end_loc;
7456 tree alignof_spelling = c_parser_peek_token (parser)->value;
7457 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7458 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7459 "_Alignof") == 0;
7460 /* A diagnostic is not required for the use of this identifier in
7461 the implementation namespace; only diagnose it for the C11
7462 spelling because of existing code using the other spellings. */
7463 if (is_c11_alignof)
7465 if (flag_isoc99)
7466 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7467 alignof_spelling);
7468 else
7469 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7470 alignof_spelling);
7472 c_parser_consume_token (parser);
7473 c_inhibit_evaluation_warnings++;
7474 in_alignof++;
7475 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7476 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7478 /* Either __alignof__ ( type-name ) or __alignof__
7479 unary-expression starting with a compound literal. */
7480 location_t loc;
7481 struct c_type_name *type_name;
7482 struct c_expr ret;
7483 matching_parens parens;
7484 parens.consume_open (parser);
7485 loc = c_parser_peek_token (parser)->location;
7486 type_name = c_parser_type_name (parser);
7487 end_loc = c_parser_peek_token (parser)->location;
7488 parens.skip_until_found_close (parser);
7489 if (type_name == NULL)
7491 struct c_expr ret;
7492 c_inhibit_evaluation_warnings--;
7493 in_alignof--;
7494 ret.value = error_mark_node;
7495 ret.original_code = ERROR_MARK;
7496 ret.original_type = NULL;
7497 return ret;
7499 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7501 expr = c_parser_postfix_expression_after_paren_type (parser,
7502 type_name,
7503 loc);
7504 goto alignof_expr;
7506 /* alignof ( type-name ). */
7507 c_inhibit_evaluation_warnings--;
7508 in_alignof--;
7509 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7510 NULL, NULL),
7511 false, is_c11_alignof, 1);
7512 ret.original_code = ERROR_MARK;
7513 ret.original_type = NULL;
7514 set_c_expr_source_range (&ret, start_loc, end_loc);
7515 return ret;
7517 else
7519 struct c_expr ret;
7520 expr = c_parser_unary_expression (parser);
7521 end_loc = expr.src_range.m_finish;
7522 alignof_expr:
7523 mark_exp_read (expr.value);
7524 c_inhibit_evaluation_warnings--;
7525 in_alignof--;
7526 if (is_c11_alignof)
7527 pedwarn (start_loc,
7528 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7529 alignof_spelling);
7530 ret.value = c_alignof_expr (start_loc, expr.value);
7531 ret.original_code = ERROR_MARK;
7532 ret.original_type = NULL;
7533 set_c_expr_source_range (&ret, start_loc, end_loc);
7534 return ret;
7538 /* Helper function to read arguments of builtins which are interfaces
7539 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7540 others. The name of the builtin is passed using BNAME parameter.
7541 Function returns true if there were no errors while parsing and
7542 stores the arguments in CEXPR_LIST. If it returns true,
7543 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7544 parenthesis. */
7545 static bool
7546 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7547 vec<c_expr_t, va_gc> **ret_cexpr_list,
7548 bool choose_expr_p,
7549 location_t *out_close_paren_loc)
7551 location_t loc = c_parser_peek_token (parser)->location;
7552 vec<c_expr_t, va_gc> *cexpr_list;
7553 c_expr_t expr;
7554 bool saved_force_folding_builtin_constant_p;
7556 *ret_cexpr_list = NULL;
7557 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7559 error_at (loc, "cannot take address of %qs", bname);
7560 return false;
7563 c_parser_consume_token (parser);
7565 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7567 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7568 c_parser_consume_token (parser);
7569 return true;
7572 saved_force_folding_builtin_constant_p
7573 = force_folding_builtin_constant_p;
7574 force_folding_builtin_constant_p |= choose_expr_p;
7575 expr = c_parser_expr_no_commas (parser, NULL);
7576 force_folding_builtin_constant_p
7577 = saved_force_folding_builtin_constant_p;
7578 vec_alloc (cexpr_list, 1);
7579 vec_safe_push (cexpr_list, expr);
7580 while (c_parser_next_token_is (parser, CPP_COMMA))
7582 c_parser_consume_token (parser);
7583 expr = c_parser_expr_no_commas (parser, NULL);
7584 vec_safe_push (cexpr_list, expr);
7587 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7588 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7589 return false;
7591 *ret_cexpr_list = cexpr_list;
7592 return true;
7595 /* This represents a single generic-association. */
7597 struct c_generic_association
7599 /* The location of the starting token of the type. */
7600 location_t type_location;
7601 /* The association's type, or NULL_TREE for 'default'. */
7602 tree type;
7603 /* The association's expression. */
7604 struct c_expr expression;
7607 /* Parse a generic-selection. (C11 6.5.1.1).
7609 generic-selection:
7610 _Generic ( assignment-expression , generic-assoc-list )
7612 generic-assoc-list:
7613 generic-association
7614 generic-assoc-list , generic-association
7616 generic-association:
7617 type-name : assignment-expression
7618 default : assignment-expression
7621 static struct c_expr
7622 c_parser_generic_selection (c_parser *parser)
7624 struct c_expr selector, error_expr;
7625 tree selector_type;
7626 struct c_generic_association matched_assoc;
7627 bool match_found = false;
7628 location_t generic_loc, selector_loc;
7630 error_expr.original_code = ERROR_MARK;
7631 error_expr.original_type = NULL;
7632 error_expr.set_error ();
7633 matched_assoc.type_location = UNKNOWN_LOCATION;
7634 matched_assoc.type = NULL_TREE;
7635 matched_assoc.expression = error_expr;
7637 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7638 generic_loc = c_parser_peek_token (parser)->location;
7639 c_parser_consume_token (parser);
7640 if (flag_isoc99)
7641 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7642 "ISO C99 does not support %<_Generic%>");
7643 else
7644 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7645 "ISO C90 does not support %<_Generic%>");
7647 matching_parens parens;
7648 if (!parens.require_open (parser))
7649 return error_expr;
7651 c_inhibit_evaluation_warnings++;
7652 selector_loc = c_parser_peek_token (parser)->location;
7653 selector = c_parser_expr_no_commas (parser, NULL);
7654 selector = default_function_array_conversion (selector_loc, selector);
7655 c_inhibit_evaluation_warnings--;
7657 if (selector.value == error_mark_node)
7659 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7660 return selector;
7662 selector_type = TREE_TYPE (selector.value);
7663 /* In ISO C terms, rvalues (including the controlling expression of
7664 _Generic) do not have qualified types. */
7665 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7666 selector_type = TYPE_MAIN_VARIANT (selector_type);
7667 /* In ISO C terms, _Noreturn is not part of the type of expressions
7668 such as &abort, but in GCC it is represented internally as a type
7669 qualifier. */
7670 if (FUNCTION_POINTER_TYPE_P (selector_type)
7671 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7672 selector_type
7673 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7675 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7677 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7678 return error_expr;
7681 auto_vec<c_generic_association> associations;
7682 while (1)
7684 struct c_generic_association assoc, *iter;
7685 unsigned int ix;
7686 c_token *token = c_parser_peek_token (parser);
7688 assoc.type_location = token->location;
7689 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7691 c_parser_consume_token (parser);
7692 assoc.type = NULL_TREE;
7694 else
7696 struct c_type_name *type_name;
7698 type_name = c_parser_type_name (parser);
7699 if (type_name == NULL)
7701 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7702 return error_expr;
7704 assoc.type = groktypename (type_name, NULL, NULL);
7705 if (assoc.type == error_mark_node)
7707 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7708 return error_expr;
7711 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7712 error_at (assoc.type_location,
7713 "%<_Generic%> association has function type");
7714 else if (!COMPLETE_TYPE_P (assoc.type))
7715 error_at (assoc.type_location,
7716 "%<_Generic%> association has incomplete type");
7718 if (variably_modified_type_p (assoc.type, NULL_TREE))
7719 error_at (assoc.type_location,
7720 "%<_Generic%> association has "
7721 "variable length type");
7724 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7726 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7727 return error_expr;
7730 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7731 if (assoc.expression.value == error_mark_node)
7733 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7734 return error_expr;
7737 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7739 if (assoc.type == NULL_TREE)
7741 if (iter->type == NULL_TREE)
7743 error_at (assoc.type_location,
7744 "duplicate %<default%> case in %<_Generic%>");
7745 inform (iter->type_location, "original %<default%> is here");
7748 else if (iter->type != NULL_TREE)
7750 if (comptypes (assoc.type, iter->type))
7752 error_at (assoc.type_location,
7753 "%<_Generic%> specifies two compatible types");
7754 inform (iter->type_location, "compatible type is here");
7759 if (assoc.type == NULL_TREE)
7761 if (!match_found)
7763 matched_assoc = assoc;
7764 match_found = true;
7767 else if (comptypes (assoc.type, selector_type))
7769 if (!match_found || matched_assoc.type == NULL_TREE)
7771 matched_assoc = assoc;
7772 match_found = true;
7774 else
7776 error_at (assoc.type_location,
7777 "%<_Generic%> selector matches multiple associations");
7778 inform (matched_assoc.type_location,
7779 "other match is here");
7783 associations.safe_push (assoc);
7785 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7786 break;
7787 c_parser_consume_token (parser);
7790 if (!parens.require_close (parser))
7792 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7793 return error_expr;
7796 if (!match_found)
7798 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7799 "compatible with any association",
7800 selector_type);
7801 return error_expr;
7804 return matched_assoc.expression;
7807 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7808 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7809 call c_parser_postfix_expression_after_paren_type on encountering them.
7811 postfix-expression:
7812 primary-expression
7813 postfix-expression [ expression ]
7814 postfix-expression ( argument-expression-list[opt] )
7815 postfix-expression . identifier
7816 postfix-expression -> identifier
7817 postfix-expression ++
7818 postfix-expression --
7819 ( type-name ) { initializer-list }
7820 ( type-name ) { initializer-list , }
7822 argument-expression-list:
7823 argument-expression
7824 argument-expression-list , argument-expression
7826 primary-expression:
7827 identifier
7828 constant
7829 string-literal
7830 ( expression )
7831 generic-selection
7833 GNU extensions:
7835 primary-expression:
7836 __func__
7837 (treated as a keyword in GNU C)
7838 __FUNCTION__
7839 __PRETTY_FUNCTION__
7840 ( compound-statement )
7841 __builtin_va_arg ( assignment-expression , type-name )
7842 __builtin_offsetof ( type-name , offsetof-member-designator )
7843 __builtin_choose_expr ( assignment-expression ,
7844 assignment-expression ,
7845 assignment-expression )
7846 __builtin_types_compatible_p ( type-name , type-name )
7847 __builtin_complex ( assignment-expression , assignment-expression )
7848 __builtin_shuffle ( assignment-expression , assignment-expression )
7849 __builtin_shuffle ( assignment-expression ,
7850 assignment-expression ,
7851 assignment-expression, )
7853 offsetof-member-designator:
7854 identifier
7855 offsetof-member-designator . identifier
7856 offsetof-member-designator [ expression ]
7858 Objective-C:
7860 primary-expression:
7861 [ objc-receiver objc-message-args ]
7862 @selector ( objc-selector-arg )
7863 @protocol ( identifier )
7864 @encode ( type-name )
7865 objc-string-literal
7866 Classname . identifier
7869 static struct c_expr
7870 c_parser_postfix_expression (c_parser *parser)
7872 struct c_expr expr, e1;
7873 struct c_type_name *t1, *t2;
7874 location_t loc = c_parser_peek_token (parser)->location;;
7875 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7876 expr.original_code = ERROR_MARK;
7877 expr.original_type = NULL;
7878 switch (c_parser_peek_token (parser)->type)
7880 case CPP_NUMBER:
7881 expr.value = c_parser_peek_token (parser)->value;
7882 set_c_expr_source_range (&expr, tok_range);
7883 loc = c_parser_peek_token (parser)->location;
7884 c_parser_consume_token (parser);
7885 if (TREE_CODE (expr.value) == FIXED_CST
7886 && !targetm.fixed_point_supported_p ())
7888 error_at (loc, "fixed-point types not supported for this target");
7889 expr.value = error_mark_node;
7891 break;
7892 case CPP_CHAR:
7893 case CPP_CHAR16:
7894 case CPP_CHAR32:
7895 case CPP_WCHAR:
7896 expr.value = c_parser_peek_token (parser)->value;
7897 /* For the purpose of warning when a pointer is compared with
7898 a zero character constant. */
7899 expr.original_type = char_type_node;
7900 set_c_expr_source_range (&expr, tok_range);
7901 c_parser_consume_token (parser);
7902 break;
7903 case CPP_STRING:
7904 case CPP_STRING16:
7905 case CPP_STRING32:
7906 case CPP_WSTRING:
7907 case CPP_UTF8STRING:
7908 expr.value = c_parser_peek_token (parser)->value;
7909 set_c_expr_source_range (&expr, tok_range);
7910 expr.original_code = STRING_CST;
7911 c_parser_consume_token (parser);
7912 break;
7913 case CPP_OBJC_STRING:
7914 gcc_assert (c_dialect_objc ());
7915 expr.value
7916 = objc_build_string_object (c_parser_peek_token (parser)->value);
7917 set_c_expr_source_range (&expr, tok_range);
7918 c_parser_consume_token (parser);
7919 break;
7920 case CPP_NAME:
7921 switch (c_parser_peek_token (parser)->id_kind)
7923 case C_ID_ID:
7925 tree id = c_parser_peek_token (parser)->value;
7926 c_parser_consume_token (parser);
7927 expr.value = build_external_ref (loc, id,
7928 (c_parser_peek_token (parser)->type
7929 == CPP_OPEN_PAREN),
7930 &expr.original_type);
7931 set_c_expr_source_range (&expr, tok_range);
7932 break;
7934 case C_ID_CLASSNAME:
7936 /* Here we parse the Objective-C 2.0 Class.name dot
7937 syntax. */
7938 tree class_name = c_parser_peek_token (parser)->value;
7939 tree component;
7940 c_parser_consume_token (parser);
7941 gcc_assert (c_dialect_objc ());
7942 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7944 expr.set_error ();
7945 break;
7947 if (c_parser_next_token_is_not (parser, CPP_NAME))
7949 c_parser_error (parser, "expected identifier");
7950 expr.set_error ();
7951 break;
7953 c_token *component_tok = c_parser_peek_token (parser);
7954 component = component_tok->value;
7955 location_t end_loc = component_tok->get_finish ();
7956 c_parser_consume_token (parser);
7957 expr.value = objc_build_class_component_ref (class_name,
7958 component);
7959 set_c_expr_source_range (&expr, loc, end_loc);
7960 break;
7962 default:
7963 c_parser_error (parser, "expected expression");
7964 expr.set_error ();
7965 break;
7967 break;
7968 case CPP_OPEN_PAREN:
7969 /* A parenthesized expression, statement expression or compound
7970 literal. */
7971 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7973 /* A statement expression. */
7974 tree stmt;
7975 location_t brace_loc;
7976 c_parser_consume_token (parser);
7977 brace_loc = c_parser_peek_token (parser)->location;
7978 c_parser_consume_token (parser);
7979 if (!building_stmt_list_p ())
7981 error_at (loc, "braced-group within expression allowed "
7982 "only inside a function");
7983 parser->error = true;
7984 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7985 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7986 expr.set_error ();
7987 break;
7989 stmt = c_begin_stmt_expr ();
7990 c_parser_compound_statement_nostart (parser);
7991 location_t close_loc = c_parser_peek_token (parser)->location;
7992 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7993 "expected %<)%>");
7994 pedwarn (loc, OPT_Wpedantic,
7995 "ISO C forbids braced-groups within expressions");
7996 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7997 set_c_expr_source_range (&expr, loc, close_loc);
7998 mark_exp_read (expr.value);
8000 else
8002 /* A parenthesized expression. */
8003 location_t loc_open_paren = c_parser_peek_token (parser)->location;
8004 c_parser_consume_token (parser);
8005 expr = c_parser_expression (parser);
8006 if (TREE_CODE (expr.value) == MODIFY_EXPR)
8007 TREE_NO_WARNING (expr.value) = 1;
8008 if (expr.original_code != C_MAYBE_CONST_EXPR
8009 && expr.original_code != SIZEOF_EXPR)
8010 expr.original_code = ERROR_MARK;
8011 /* Don't change EXPR.ORIGINAL_TYPE. */
8012 location_t loc_close_paren = c_parser_peek_token (parser)->location;
8013 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
8014 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8015 "expected %<)%>", loc_open_paren);
8017 break;
8018 case CPP_KEYWORD:
8019 switch (c_parser_peek_token (parser)->keyword)
8021 case RID_FUNCTION_NAME:
8022 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8023 "%<__FUNCTION__%> predefined identifier");
8024 expr.value = fname_decl (loc,
8025 c_parser_peek_token (parser)->keyword,
8026 c_parser_peek_token (parser)->value);
8027 set_c_expr_source_range (&expr, loc, loc);
8028 c_parser_consume_token (parser);
8029 break;
8030 case RID_PRETTY_FUNCTION_NAME:
8031 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8032 "%<__PRETTY_FUNCTION__%> predefined identifier");
8033 expr.value = fname_decl (loc,
8034 c_parser_peek_token (parser)->keyword,
8035 c_parser_peek_token (parser)->value);
8036 set_c_expr_source_range (&expr, loc, loc);
8037 c_parser_consume_token (parser);
8038 break;
8039 case RID_C99_FUNCTION_NAME:
8040 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8041 "%<__func__%> predefined identifier");
8042 expr.value = fname_decl (loc,
8043 c_parser_peek_token (parser)->keyword,
8044 c_parser_peek_token (parser)->value);
8045 set_c_expr_source_range (&expr, loc, loc);
8046 c_parser_consume_token (parser);
8047 break;
8048 case RID_VA_ARG:
8050 location_t start_loc = loc;
8051 c_parser_consume_token (parser);
8052 matching_parens parens;
8053 if (!parens.require_open (parser))
8055 expr.set_error ();
8056 break;
8058 e1 = c_parser_expr_no_commas (parser, NULL);
8059 mark_exp_read (e1.value);
8060 e1.value = c_fully_fold (e1.value, false, NULL);
8061 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8063 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8064 expr.set_error ();
8065 break;
8067 loc = c_parser_peek_token (parser)->location;
8068 t1 = c_parser_type_name (parser);
8069 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8070 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8071 "expected %<)%>");
8072 if (t1 == NULL)
8074 expr.set_error ();
8076 else
8078 tree type_expr = NULL_TREE;
8079 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8080 groktypename (t1, &type_expr, NULL));
8081 if (type_expr)
8083 expr.value = build2 (C_MAYBE_CONST_EXPR,
8084 TREE_TYPE (expr.value), type_expr,
8085 expr.value);
8086 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8088 set_c_expr_source_range (&expr, start_loc, end_loc);
8091 break;
8092 case RID_OFFSETOF:
8094 c_parser_consume_token (parser);
8095 matching_parens parens;
8096 if (!parens.require_open (parser))
8098 expr.set_error ();
8099 break;
8101 t1 = c_parser_type_name (parser);
8102 if (t1 == NULL)
8103 parser->error = true;
8104 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8105 gcc_assert (parser->error);
8106 if (parser->error)
8108 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8109 expr.set_error ();
8110 break;
8112 tree type = groktypename (t1, NULL, NULL);
8113 tree offsetof_ref;
8114 if (type == error_mark_node)
8115 offsetof_ref = error_mark_node;
8116 else
8118 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8119 SET_EXPR_LOCATION (offsetof_ref, loc);
8121 /* Parse the second argument to __builtin_offsetof. We
8122 must have one identifier, and beyond that we want to
8123 accept sub structure and sub array references. */
8124 if (c_parser_next_token_is (parser, CPP_NAME))
8126 c_token *comp_tok = c_parser_peek_token (parser);
8127 offsetof_ref = build_component_ref
8128 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8129 c_parser_consume_token (parser);
8130 while (c_parser_next_token_is (parser, CPP_DOT)
8131 || c_parser_next_token_is (parser,
8132 CPP_OPEN_SQUARE)
8133 || c_parser_next_token_is (parser,
8134 CPP_DEREF))
8136 if (c_parser_next_token_is (parser, CPP_DEREF))
8138 loc = c_parser_peek_token (parser)->location;
8139 offsetof_ref = build_array_ref (loc,
8140 offsetof_ref,
8141 integer_zero_node);
8142 goto do_dot;
8144 else if (c_parser_next_token_is (parser, CPP_DOT))
8146 do_dot:
8147 c_parser_consume_token (parser);
8148 if (c_parser_next_token_is_not (parser,
8149 CPP_NAME))
8151 c_parser_error (parser, "expected identifier");
8152 break;
8154 c_token *comp_tok = c_parser_peek_token (parser);
8155 offsetof_ref = build_component_ref
8156 (loc, offsetof_ref, comp_tok->value,
8157 comp_tok->location);
8158 c_parser_consume_token (parser);
8160 else
8162 struct c_expr ce;
8163 tree idx;
8164 loc = c_parser_peek_token (parser)->location;
8165 c_parser_consume_token (parser);
8166 ce = c_parser_expression (parser);
8167 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8168 idx = ce.value;
8169 idx = c_fully_fold (idx, false, NULL);
8170 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8171 "expected %<]%>");
8172 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8176 else
8177 c_parser_error (parser, "expected identifier");
8178 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8179 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8180 "expected %<)%>");
8181 expr.value = fold_offsetof (offsetof_ref);
8182 set_c_expr_source_range (&expr, loc, end_loc);
8184 break;
8185 case RID_CHOOSE_EXPR:
8187 vec<c_expr_t, va_gc> *cexpr_list;
8188 c_expr_t *e1_p, *e2_p, *e3_p;
8189 tree c;
8190 location_t close_paren_loc;
8192 c_parser_consume_token (parser);
8193 if (!c_parser_get_builtin_args (parser,
8194 "__builtin_choose_expr",
8195 &cexpr_list, true,
8196 &close_paren_loc))
8198 expr.set_error ();
8199 break;
8202 if (vec_safe_length (cexpr_list) != 3)
8204 error_at (loc, "wrong number of arguments to "
8205 "%<__builtin_choose_expr%>");
8206 expr.set_error ();
8207 break;
8210 e1_p = &(*cexpr_list)[0];
8211 e2_p = &(*cexpr_list)[1];
8212 e3_p = &(*cexpr_list)[2];
8214 c = e1_p->value;
8215 mark_exp_read (e2_p->value);
8216 mark_exp_read (e3_p->value);
8217 if (TREE_CODE (c) != INTEGER_CST
8218 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8219 error_at (loc,
8220 "first argument to %<__builtin_choose_expr%> not"
8221 " a constant");
8222 constant_expression_warning (c);
8223 expr = integer_zerop (c) ? *e3_p : *e2_p;
8224 set_c_expr_source_range (&expr, loc, close_paren_loc);
8225 break;
8227 case RID_TYPES_COMPATIBLE_P:
8229 c_parser_consume_token (parser);
8230 matching_parens parens;
8231 if (!parens.require_open (parser))
8233 expr.set_error ();
8234 break;
8236 t1 = c_parser_type_name (parser);
8237 if (t1 == NULL)
8239 expr.set_error ();
8240 break;
8242 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8244 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8245 expr.set_error ();
8246 break;
8248 t2 = c_parser_type_name (parser);
8249 if (t2 == NULL)
8251 expr.set_error ();
8252 break;
8254 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8255 parens.skip_until_found_close (parser);
8256 tree e1, e2;
8257 e1 = groktypename (t1, NULL, NULL);
8258 e2 = groktypename (t2, NULL, NULL);
8259 if (e1 == error_mark_node || e2 == error_mark_node)
8261 expr.set_error ();
8262 break;
8265 e1 = TYPE_MAIN_VARIANT (e1);
8266 e2 = TYPE_MAIN_VARIANT (e2);
8268 expr.value
8269 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8270 set_c_expr_source_range (&expr, loc, close_paren_loc);
8272 break;
8273 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8275 vec<c_expr_t, va_gc> *cexpr_list;
8276 c_expr_t *e2_p;
8277 tree chain_value;
8278 location_t close_paren_loc;
8280 c_parser_consume_token (parser);
8281 if (!c_parser_get_builtin_args (parser,
8282 "__builtin_call_with_static_chain",
8283 &cexpr_list, false,
8284 &close_paren_loc))
8286 expr.set_error ();
8287 break;
8289 if (vec_safe_length (cexpr_list) != 2)
8291 error_at (loc, "wrong number of arguments to "
8292 "%<__builtin_call_with_static_chain%>");
8293 expr.set_error ();
8294 break;
8297 expr = (*cexpr_list)[0];
8298 e2_p = &(*cexpr_list)[1];
8299 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8300 chain_value = e2_p->value;
8301 mark_exp_read (chain_value);
8303 if (TREE_CODE (expr.value) != CALL_EXPR)
8304 error_at (loc, "first argument to "
8305 "%<__builtin_call_with_static_chain%> "
8306 "must be a call expression");
8307 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8308 error_at (loc, "second argument to "
8309 "%<__builtin_call_with_static_chain%> "
8310 "must be a pointer type");
8311 else
8312 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8313 set_c_expr_source_range (&expr, loc, close_paren_loc);
8314 break;
8316 case RID_BUILTIN_COMPLEX:
8318 vec<c_expr_t, va_gc> *cexpr_list;
8319 c_expr_t *e1_p, *e2_p;
8320 location_t close_paren_loc;
8322 c_parser_consume_token (parser);
8323 if (!c_parser_get_builtin_args (parser,
8324 "__builtin_complex",
8325 &cexpr_list, false,
8326 &close_paren_loc))
8328 expr.set_error ();
8329 break;
8332 if (vec_safe_length (cexpr_list) != 2)
8334 error_at (loc, "wrong number of arguments to "
8335 "%<__builtin_complex%>");
8336 expr.set_error ();
8337 break;
8340 e1_p = &(*cexpr_list)[0];
8341 e2_p = &(*cexpr_list)[1];
8343 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8344 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8345 e1_p->value = convert (TREE_TYPE (e1_p->value),
8346 TREE_OPERAND (e1_p->value, 0));
8347 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8348 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8349 e2_p->value = convert (TREE_TYPE (e2_p->value),
8350 TREE_OPERAND (e2_p->value, 0));
8351 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8352 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8353 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8354 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8356 error_at (loc, "%<__builtin_complex%> operand "
8357 "not of real binary floating-point type");
8358 expr.set_error ();
8359 break;
8361 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8362 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8364 error_at (loc,
8365 "%<__builtin_complex%> operands of different types");
8366 expr.set_error ();
8367 break;
8369 pedwarn_c90 (loc, OPT_Wpedantic,
8370 "ISO C90 does not support complex types");
8371 expr.value = build2_loc (loc, COMPLEX_EXPR,
8372 build_complex_type
8373 (TYPE_MAIN_VARIANT
8374 (TREE_TYPE (e1_p->value))),
8375 e1_p->value, e2_p->value);
8376 set_c_expr_source_range (&expr, loc, close_paren_loc);
8377 break;
8379 case RID_BUILTIN_SHUFFLE:
8381 vec<c_expr_t, va_gc> *cexpr_list;
8382 unsigned int i;
8383 c_expr_t *p;
8384 location_t close_paren_loc;
8386 c_parser_consume_token (parser);
8387 if (!c_parser_get_builtin_args (parser,
8388 "__builtin_shuffle",
8389 &cexpr_list, false,
8390 &close_paren_loc))
8392 expr.set_error ();
8393 break;
8396 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8397 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8399 if (vec_safe_length (cexpr_list) == 2)
8400 expr.value =
8401 c_build_vec_perm_expr
8402 (loc, (*cexpr_list)[0].value,
8403 NULL_TREE, (*cexpr_list)[1].value);
8405 else if (vec_safe_length (cexpr_list) == 3)
8406 expr.value =
8407 c_build_vec_perm_expr
8408 (loc, (*cexpr_list)[0].value,
8409 (*cexpr_list)[1].value,
8410 (*cexpr_list)[2].value);
8411 else
8413 error_at (loc, "wrong number of arguments to "
8414 "%<__builtin_shuffle%>");
8415 expr.set_error ();
8417 set_c_expr_source_range (&expr, loc, close_paren_loc);
8418 break;
8420 case RID_AT_SELECTOR:
8422 gcc_assert (c_dialect_objc ());
8423 c_parser_consume_token (parser);
8424 matching_parens parens;
8425 if (!parens.require_open (parser))
8427 expr.set_error ();
8428 break;
8430 tree sel = c_parser_objc_selector_arg (parser);
8431 location_t close_loc = c_parser_peek_token (parser)->location;
8432 parens.skip_until_found_close (parser);
8433 expr.value = objc_build_selector_expr (loc, sel);
8434 set_c_expr_source_range (&expr, loc, close_loc);
8436 break;
8437 case RID_AT_PROTOCOL:
8439 gcc_assert (c_dialect_objc ());
8440 c_parser_consume_token (parser);
8441 matching_parens parens;
8442 if (!parens.require_open (parser))
8444 expr.set_error ();
8445 break;
8447 if (c_parser_next_token_is_not (parser, CPP_NAME))
8449 c_parser_error (parser, "expected identifier");
8450 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8451 expr.set_error ();
8452 break;
8454 tree id = c_parser_peek_token (parser)->value;
8455 c_parser_consume_token (parser);
8456 location_t close_loc = c_parser_peek_token (parser)->location;
8457 parens.skip_until_found_close (parser);
8458 expr.value = objc_build_protocol_expr (id);
8459 set_c_expr_source_range (&expr, loc, close_loc);
8461 break;
8462 case RID_AT_ENCODE:
8464 /* Extension to support C-structures in the archiver. */
8465 gcc_assert (c_dialect_objc ());
8466 c_parser_consume_token (parser);
8467 matching_parens parens;
8468 if (!parens.require_open (parser))
8470 expr.set_error ();
8471 break;
8473 t1 = c_parser_type_name (parser);
8474 if (t1 == NULL)
8476 expr.set_error ();
8477 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8478 break;
8480 location_t close_loc = c_parser_peek_token (parser)->location;
8481 parens.skip_until_found_close (parser);
8482 tree type = groktypename (t1, NULL, NULL);
8483 expr.value = objc_build_encode_expr (type);
8484 set_c_expr_source_range (&expr, loc, close_loc);
8486 break;
8487 case RID_GENERIC:
8488 expr = c_parser_generic_selection (parser);
8489 break;
8490 case RID_CILK_SPAWN:
8491 c_parser_consume_token (parser);
8492 if (!flag_cilkplus)
8494 error_at (loc, "-fcilkplus must be enabled to use "
8495 "%<_Cilk_spawn%>");
8496 expr = c_parser_cast_expression (parser, NULL);
8497 expr.set_error ();
8499 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8501 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
8502 "are not permitted");
8503 /* Now flush out all the _Cilk_spawns. */
8504 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8505 c_parser_consume_token (parser);
8506 expr = c_parser_cast_expression (parser, NULL);
8508 else
8510 expr = c_parser_cast_expression (parser, NULL);
8511 expr.value = build_cilk_spawn (loc, expr.value);
8513 break;
8514 default:
8515 c_parser_error (parser, "expected expression");
8516 expr.set_error ();
8517 break;
8519 break;
8520 case CPP_OPEN_SQUARE:
8521 if (c_dialect_objc ())
8523 tree receiver, args;
8524 c_parser_consume_token (parser);
8525 receiver = c_parser_objc_receiver (parser);
8526 args = c_parser_objc_message_args (parser);
8527 location_t close_loc = c_parser_peek_token (parser)->location;
8528 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8529 "expected %<]%>");
8530 expr.value = objc_build_message_expr (receiver, args);
8531 set_c_expr_source_range (&expr, loc, close_loc);
8532 break;
8534 /* Else fall through to report error. */
8535 /* FALLTHRU */
8536 default:
8537 c_parser_error (parser, "expected expression");
8538 expr.set_error ();
8539 break;
8541 return c_parser_postfix_expression_after_primary
8542 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8545 /* Parse a postfix expression after a parenthesized type name: the
8546 brace-enclosed initializer of a compound literal, possibly followed
8547 by some postfix operators. This is separate because it is not
8548 possible to tell until after the type name whether a cast
8549 expression has a cast or a compound literal, or whether the operand
8550 of sizeof is a parenthesized type name or starts with a compound
8551 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8552 location of the first token after the parentheses around the type
8553 name. */
8555 static struct c_expr
8556 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8557 struct c_type_name *type_name,
8558 location_t type_loc)
8560 tree type;
8561 struct c_expr init;
8562 bool non_const;
8563 struct c_expr expr;
8564 location_t start_loc;
8565 tree type_expr = NULL_TREE;
8566 bool type_expr_const = true;
8567 check_compound_literal_type (type_loc, type_name);
8568 rich_location richloc (line_table, type_loc);
8569 start_init (NULL_TREE, NULL, 0, &richloc);
8570 type = groktypename (type_name, &type_expr, &type_expr_const);
8571 start_loc = c_parser_peek_token (parser)->location;
8572 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8574 error_at (type_loc, "compound literal has variable size");
8575 type = error_mark_node;
8577 init = c_parser_braced_init (parser, type, false, NULL);
8578 finish_init ();
8579 maybe_warn_string_init (type_loc, type, init);
8581 if (type != error_mark_node
8582 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8583 && current_function_decl)
8585 error ("compound literal qualified by address-space qualifier");
8586 type = error_mark_node;
8589 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8590 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8591 ? CONSTRUCTOR_NON_CONST (init.value)
8592 : init.original_code == C_MAYBE_CONST_EXPR);
8593 non_const |= !type_expr_const;
8594 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8595 set_c_expr_source_range (&expr, init.src_range);
8596 expr.original_code = ERROR_MARK;
8597 expr.original_type = NULL;
8598 if (type != error_mark_node
8599 && expr.value != error_mark_node
8600 && type_expr)
8602 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8604 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8605 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8607 else
8609 gcc_assert (!non_const);
8610 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8611 type_expr, expr.value);
8614 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8617 /* Callback function for sizeof_pointer_memaccess_warning to compare
8618 types. */
8620 static bool
8621 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8623 return comptypes (type1, type2) == 1;
8626 /* Parse a postfix expression after the initial primary or compound
8627 literal; that is, parse a series of postfix operators.
8629 EXPR_LOC is the location of the primary expression. */
8631 static struct c_expr
8632 c_parser_postfix_expression_after_primary (c_parser *parser,
8633 location_t expr_loc,
8634 struct c_expr expr)
8636 struct c_expr orig_expr;
8637 tree ident, idx;
8638 location_t sizeof_arg_loc[3], comp_loc;
8639 tree sizeof_arg[3];
8640 unsigned int literal_zero_mask;
8641 unsigned int i;
8642 vec<tree, va_gc> *exprlist;
8643 vec<tree, va_gc> *origtypes = NULL;
8644 vec<location_t> arg_loc = vNULL;
8645 location_t start;
8646 location_t finish;
8648 while (true)
8650 location_t op_loc = c_parser_peek_token (parser)->location;
8651 switch (c_parser_peek_token (parser)->type)
8653 case CPP_OPEN_SQUARE:
8654 /* Array reference. */
8655 c_parser_consume_token (parser);
8656 if (flag_cilkplus
8657 && c_parser_peek_token (parser)->type == CPP_COLON)
8658 /* If we are here, then we have something like this:
8659 Array [ : ]
8661 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8662 expr.value);
8663 else
8665 idx = c_parser_expression (parser).value;
8666 /* Here we have 3 options:
8667 1. Array [EXPR] -- Normal Array call.
8668 2. Array [EXPR : EXPR] -- Array notation without stride.
8669 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8671 For 1, we just handle it just like a normal array expression.
8672 For 2 and 3 we handle it like we handle array notations. The
8673 idx value we have above becomes the initial/start index.
8675 if (flag_cilkplus
8676 && c_parser_peek_token (parser)->type == CPP_COLON)
8677 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8678 expr.value);
8679 else
8681 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8682 "expected %<]%>");
8683 start = expr.get_start ();
8684 finish = parser->tokens_buf[0].location;
8685 expr.value = build_array_ref (op_loc, expr.value, idx);
8686 set_c_expr_source_range (&expr, start, finish);
8689 expr.original_code = ERROR_MARK;
8690 expr.original_type = NULL;
8691 break;
8692 case CPP_OPEN_PAREN:
8693 /* Function call. */
8694 c_parser_consume_token (parser);
8695 for (i = 0; i < 3; i++)
8697 sizeof_arg[i] = NULL_TREE;
8698 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8700 literal_zero_mask = 0;
8701 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8702 exprlist = NULL;
8703 else
8704 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8705 sizeof_arg_loc, sizeof_arg,
8706 &arg_loc, &literal_zero_mask);
8707 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8708 "expected %<)%>");
8709 orig_expr = expr;
8710 mark_exp_read (expr.value);
8711 if (warn_sizeof_pointer_memaccess)
8712 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8713 expr.value, exprlist,
8714 sizeof_arg,
8715 sizeof_ptr_memacc_comptypes);
8716 if (TREE_CODE (expr.value) == FUNCTION_DECL
8717 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8718 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8719 && vec_safe_length (exprlist) == 3)
8721 tree arg0 = (*exprlist)[0];
8722 tree arg2 = (*exprlist)[2];
8723 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
8726 start = expr.get_start ();
8727 finish = parser->tokens_buf[0].get_finish ();
8728 expr.value
8729 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8730 exprlist, origtypes);
8731 set_c_expr_source_range (&expr, start, finish);
8733 expr.original_code = ERROR_MARK;
8734 if (TREE_CODE (expr.value) == INTEGER_CST
8735 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8736 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8737 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8738 expr.original_code = C_MAYBE_CONST_EXPR;
8739 expr.original_type = NULL;
8740 if (exprlist)
8742 release_tree_vector (exprlist);
8743 release_tree_vector (origtypes);
8745 arg_loc.release ();
8746 break;
8747 case CPP_DOT:
8748 /* Structure element reference. */
8749 c_parser_consume_token (parser);
8750 expr = default_function_array_conversion (expr_loc, expr);
8751 if (c_parser_next_token_is (parser, CPP_NAME))
8753 c_token *comp_tok = c_parser_peek_token (parser);
8754 ident = comp_tok->value;
8755 comp_loc = comp_tok->location;
8757 else
8759 c_parser_error (parser, "expected identifier");
8760 expr.set_error ();
8761 expr.original_code = ERROR_MARK;
8762 expr.original_type = NULL;
8763 return expr;
8765 start = expr.get_start ();
8766 finish = c_parser_peek_token (parser)->get_finish ();
8767 c_parser_consume_token (parser);
8768 expr.value = build_component_ref (op_loc, expr.value, ident,
8769 comp_loc);
8770 set_c_expr_source_range (&expr, start, finish);
8771 expr.original_code = ERROR_MARK;
8772 if (TREE_CODE (expr.value) != COMPONENT_REF)
8773 expr.original_type = NULL;
8774 else
8776 /* Remember the original type of a bitfield. */
8777 tree field = TREE_OPERAND (expr.value, 1);
8778 if (TREE_CODE (field) != FIELD_DECL)
8779 expr.original_type = NULL;
8780 else
8781 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8783 break;
8784 case CPP_DEREF:
8785 /* Structure element reference. */
8786 c_parser_consume_token (parser);
8787 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8788 if (c_parser_next_token_is (parser, CPP_NAME))
8790 c_token *comp_tok = c_parser_peek_token (parser);
8791 ident = comp_tok->value;
8792 comp_loc = comp_tok->location;
8794 else
8796 c_parser_error (parser, "expected identifier");
8797 expr.set_error ();
8798 expr.original_code = ERROR_MARK;
8799 expr.original_type = NULL;
8800 return expr;
8802 start = expr.get_start ();
8803 finish = c_parser_peek_token (parser)->get_finish ();
8804 c_parser_consume_token (parser);
8805 expr.value = build_component_ref (op_loc,
8806 build_indirect_ref (op_loc,
8807 expr.value,
8808 RO_ARROW),
8809 ident, comp_loc);
8810 set_c_expr_source_range (&expr, start, finish);
8811 expr.original_code = ERROR_MARK;
8812 if (TREE_CODE (expr.value) != COMPONENT_REF)
8813 expr.original_type = NULL;
8814 else
8816 /* Remember the original type of a bitfield. */
8817 tree field = TREE_OPERAND (expr.value, 1);
8818 if (TREE_CODE (field) != FIELD_DECL)
8819 expr.original_type = NULL;
8820 else
8821 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8823 break;
8824 case CPP_PLUS_PLUS:
8825 /* Postincrement. */
8826 start = expr.get_start ();
8827 finish = c_parser_peek_token (parser)->get_finish ();
8828 c_parser_consume_token (parser);
8829 /* If the expressions have array notations, we expand them. */
8830 if (flag_cilkplus
8831 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8832 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8833 else
8835 expr = default_function_array_read_conversion (expr_loc, expr);
8836 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
8837 expr.value, false);
8839 set_c_expr_source_range (&expr, start, finish);
8840 expr.original_code = ERROR_MARK;
8841 expr.original_type = NULL;
8842 break;
8843 case CPP_MINUS_MINUS:
8844 /* Postdecrement. */
8845 start = expr.get_start ();
8846 finish = c_parser_peek_token (parser)->get_finish ();
8847 c_parser_consume_token (parser);
8848 /* If the expressions have array notations, we expand them. */
8849 if (flag_cilkplus
8850 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8851 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8852 else
8854 expr = default_function_array_read_conversion (expr_loc, expr);
8855 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
8856 expr.value, false);
8858 set_c_expr_source_range (&expr, start, finish);
8859 expr.original_code = ERROR_MARK;
8860 expr.original_type = NULL;
8861 break;
8862 default:
8863 return expr;
8868 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
8870 expression:
8871 assignment-expression
8872 expression , assignment-expression
8875 static struct c_expr
8876 c_parser_expression (c_parser *parser)
8878 location_t tloc = c_parser_peek_token (parser)->location;
8879 struct c_expr expr;
8880 expr = c_parser_expr_no_commas (parser, NULL);
8881 if (c_parser_next_token_is (parser, CPP_COMMA))
8882 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8883 while (c_parser_next_token_is (parser, CPP_COMMA))
8885 struct c_expr next;
8886 tree lhsval;
8887 location_t loc = c_parser_peek_token (parser)->location;
8888 location_t expr_loc;
8889 c_parser_consume_token (parser);
8890 expr_loc = c_parser_peek_token (parser)->location;
8891 lhsval = expr.value;
8892 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8893 lhsval = TREE_OPERAND (lhsval, 1);
8894 if (DECL_P (lhsval) || handled_component_p (lhsval))
8895 mark_exp_read (lhsval);
8896 next = c_parser_expr_no_commas (parser, NULL);
8897 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8898 expr.value = build_compound_expr (loc, expr.value, next.value);
8899 expr.original_code = COMPOUND_EXPR;
8900 expr.original_type = next.original_type;
8902 return expr;
8905 /* Parse an expression and convert functions or arrays to pointers and
8906 lvalues to rvalues. */
8908 static struct c_expr
8909 c_parser_expression_conv (c_parser *parser)
8911 struct c_expr expr;
8912 location_t loc = c_parser_peek_token (parser)->location;
8913 expr = c_parser_expression (parser);
8914 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8915 return expr;
8918 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8919 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8921 static inline void
8922 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8923 unsigned int idx)
8925 if (idx >= HOST_BITS_PER_INT)
8926 return;
8928 c_token *tok = c_parser_peek_token (parser);
8929 switch (tok->type)
8931 case CPP_NUMBER:
8932 case CPP_CHAR:
8933 case CPP_WCHAR:
8934 case CPP_CHAR16:
8935 case CPP_CHAR32:
8936 /* If a parameter is literal zero alone, remember it
8937 for -Wmemset-transposed-args warning. */
8938 if (integer_zerop (tok->value)
8939 && !TREE_OVERFLOW (tok->value)
8940 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8941 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8942 *literal_zero_mask |= 1U << idx;
8943 default:
8944 break;
8948 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8949 functions and arrays to pointers and lvalues to rvalues. If
8950 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8951 locations of function arguments into this vector.
8953 nonempty-expr-list:
8954 assignment-expression
8955 nonempty-expr-list , assignment-expression
8958 static vec<tree, va_gc> *
8959 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8960 vec<tree, va_gc> **p_orig_types,
8961 location_t *sizeof_arg_loc, tree *sizeof_arg,
8962 vec<location_t> *locations,
8963 unsigned int *literal_zero_mask)
8965 vec<tree, va_gc> *ret;
8966 vec<tree, va_gc> *orig_types;
8967 struct c_expr expr;
8968 unsigned int idx = 0;
8970 ret = make_tree_vector ();
8971 if (p_orig_types == NULL)
8972 orig_types = NULL;
8973 else
8974 orig_types = make_tree_vector ();
8976 if (literal_zero_mask)
8977 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8978 expr = c_parser_expr_no_commas (parser, NULL);
8979 if (convert_p)
8980 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
8981 if (fold_p)
8982 expr.value = c_fully_fold (expr.value, false, NULL);
8983 ret->quick_push (expr.value);
8984 if (orig_types)
8985 orig_types->quick_push (expr.original_type);
8986 if (locations)
8987 locations->safe_push (expr.get_location ());
8988 if (sizeof_arg != NULL
8989 && expr.original_code == SIZEOF_EXPR)
8991 sizeof_arg[0] = c_last_sizeof_arg;
8992 sizeof_arg_loc[0] = c_last_sizeof_loc;
8994 while (c_parser_next_token_is (parser, CPP_COMMA))
8996 c_parser_consume_token (parser);
8997 if (literal_zero_mask)
8998 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8999 expr = c_parser_expr_no_commas (parser, NULL);
9000 if (convert_p)
9001 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9002 true);
9003 if (fold_p)
9004 expr.value = c_fully_fold (expr.value, false, NULL);
9005 vec_safe_push (ret, expr.value);
9006 if (orig_types)
9007 vec_safe_push (orig_types, expr.original_type);
9008 if (locations)
9009 locations->safe_push (expr.get_location ());
9010 if (++idx < 3
9011 && sizeof_arg != NULL
9012 && expr.original_code == SIZEOF_EXPR)
9014 sizeof_arg[idx] = c_last_sizeof_arg;
9015 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9018 if (orig_types)
9019 *p_orig_types = orig_types;
9020 return ret;
9023 /* Parse Objective-C-specific constructs. */
9025 /* Parse an objc-class-definition.
9027 objc-class-definition:
9028 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9029 objc-class-instance-variables[opt] objc-methodprotolist @end
9030 @implementation identifier objc-superclass[opt]
9031 objc-class-instance-variables[opt]
9032 @interface identifier ( identifier ) objc-protocol-refs[opt]
9033 objc-methodprotolist @end
9034 @interface identifier ( ) objc-protocol-refs[opt]
9035 objc-methodprotolist @end
9036 @implementation identifier ( identifier )
9038 objc-superclass:
9039 : identifier
9041 "@interface identifier (" must start "@interface identifier (
9042 identifier ) ...": objc-methodprotolist in the first production may
9043 not start with a parenthesized identifier as a declarator of a data
9044 definition with no declaration specifiers if the objc-superclass,
9045 objc-protocol-refs and objc-class-instance-variables are omitted. */
9047 static void
9048 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9050 bool iface_p;
9051 tree id1;
9052 tree superclass;
9053 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9054 iface_p = true;
9055 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9056 iface_p = false;
9057 else
9058 gcc_unreachable ();
9060 c_parser_consume_token (parser);
9061 if (c_parser_next_token_is_not (parser, CPP_NAME))
9063 c_parser_error (parser, "expected identifier");
9064 return;
9066 id1 = c_parser_peek_token (parser)->value;
9067 c_parser_consume_token (parser);
9068 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9070 /* We have a category or class extension. */
9071 tree id2;
9072 tree proto = NULL_TREE;
9073 matching_parens parens;
9074 parens.consume_open (parser);
9075 if (c_parser_next_token_is_not (parser, CPP_NAME))
9077 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9079 /* We have a class extension. */
9080 id2 = NULL_TREE;
9082 else
9084 c_parser_error (parser, "expected identifier or %<)%>");
9085 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9086 return;
9089 else
9091 id2 = c_parser_peek_token (parser)->value;
9092 c_parser_consume_token (parser);
9094 parens.skip_until_found_close (parser);
9095 if (!iface_p)
9097 objc_start_category_implementation (id1, id2);
9098 return;
9100 if (c_parser_next_token_is (parser, CPP_LESS))
9101 proto = c_parser_objc_protocol_refs (parser);
9102 objc_start_category_interface (id1, id2, proto, attributes);
9103 c_parser_objc_methodprotolist (parser);
9104 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9105 objc_finish_interface ();
9106 return;
9108 if (c_parser_next_token_is (parser, CPP_COLON))
9110 c_parser_consume_token (parser);
9111 if (c_parser_next_token_is_not (parser, CPP_NAME))
9113 c_parser_error (parser, "expected identifier");
9114 return;
9116 superclass = c_parser_peek_token (parser)->value;
9117 c_parser_consume_token (parser);
9119 else
9120 superclass = NULL_TREE;
9121 if (iface_p)
9123 tree proto = NULL_TREE;
9124 if (c_parser_next_token_is (parser, CPP_LESS))
9125 proto = c_parser_objc_protocol_refs (parser);
9126 objc_start_class_interface (id1, superclass, proto, attributes);
9128 else
9129 objc_start_class_implementation (id1, superclass);
9130 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9131 c_parser_objc_class_instance_variables (parser);
9132 if (iface_p)
9134 objc_continue_interface ();
9135 c_parser_objc_methodprotolist (parser);
9136 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9137 objc_finish_interface ();
9139 else
9141 objc_continue_implementation ();
9142 return;
9146 /* Parse objc-class-instance-variables.
9148 objc-class-instance-variables:
9149 { objc-instance-variable-decl-list[opt] }
9151 objc-instance-variable-decl-list:
9152 objc-visibility-spec
9153 objc-instance-variable-decl ;
9155 objc-instance-variable-decl-list objc-visibility-spec
9156 objc-instance-variable-decl-list objc-instance-variable-decl ;
9157 objc-instance-variable-decl-list ;
9159 objc-visibility-spec:
9160 @private
9161 @protected
9162 @public
9164 objc-instance-variable-decl:
9165 struct-declaration
9168 static void
9169 c_parser_objc_class_instance_variables (c_parser *parser)
9171 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9172 c_parser_consume_token (parser);
9173 while (c_parser_next_token_is_not (parser, CPP_EOF))
9175 tree decls;
9176 /* Parse any stray semicolon. */
9177 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9179 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9180 "extra semicolon");
9181 c_parser_consume_token (parser);
9182 continue;
9184 /* Stop if at the end of the instance variables. */
9185 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9187 c_parser_consume_token (parser);
9188 break;
9190 /* Parse any objc-visibility-spec. */
9191 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9193 c_parser_consume_token (parser);
9194 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9195 continue;
9197 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9199 c_parser_consume_token (parser);
9200 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9201 continue;
9203 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9205 c_parser_consume_token (parser);
9206 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9207 continue;
9209 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9211 c_parser_consume_token (parser);
9212 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9213 continue;
9215 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9217 c_parser_pragma (parser, pragma_external, NULL);
9218 continue;
9221 /* Parse some comma-separated declarations. */
9222 decls = c_parser_struct_declaration (parser);
9223 if (decls == NULL)
9225 /* There is a syntax error. We want to skip the offending
9226 tokens up to the next ';' (included) or '}'
9227 (excluded). */
9229 /* First, skip manually a ')' or ']'. This is because they
9230 reduce the nesting level, so c_parser_skip_until_found()
9231 wouldn't be able to skip past them. */
9232 c_token *token = c_parser_peek_token (parser);
9233 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9234 c_parser_consume_token (parser);
9236 /* Then, do the standard skipping. */
9237 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9239 /* We hopefully recovered. Start normal parsing again. */
9240 parser->error = false;
9241 continue;
9243 else
9245 /* Comma-separated instance variables are chained together
9246 in reverse order; add them one by one. */
9247 tree ivar = nreverse (decls);
9248 for (; ivar; ivar = DECL_CHAIN (ivar))
9249 objc_add_instance_variable (copy_node (ivar));
9251 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9255 /* Parse an objc-class-declaration.
9257 objc-class-declaration:
9258 @class identifier-list ;
9261 static void
9262 c_parser_objc_class_declaration (c_parser *parser)
9264 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9265 c_parser_consume_token (parser);
9266 /* Any identifiers, including those declared as type names, are OK
9267 here. */
9268 while (true)
9270 tree id;
9271 if (c_parser_next_token_is_not (parser, CPP_NAME))
9273 c_parser_error (parser, "expected identifier");
9274 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9275 parser->error = false;
9276 return;
9278 id = c_parser_peek_token (parser)->value;
9279 objc_declare_class (id);
9280 c_parser_consume_token (parser);
9281 if (c_parser_next_token_is (parser, CPP_COMMA))
9282 c_parser_consume_token (parser);
9283 else
9284 break;
9286 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9289 /* Parse an objc-alias-declaration.
9291 objc-alias-declaration:
9292 @compatibility_alias identifier identifier ;
9295 static void
9296 c_parser_objc_alias_declaration (c_parser *parser)
9298 tree id1, id2;
9299 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9300 c_parser_consume_token (parser);
9301 if (c_parser_next_token_is_not (parser, CPP_NAME))
9303 c_parser_error (parser, "expected identifier");
9304 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9305 return;
9307 id1 = c_parser_peek_token (parser)->value;
9308 c_parser_consume_token (parser);
9309 if (c_parser_next_token_is_not (parser, CPP_NAME))
9311 c_parser_error (parser, "expected identifier");
9312 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9313 return;
9315 id2 = c_parser_peek_token (parser)->value;
9316 c_parser_consume_token (parser);
9317 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9318 objc_declare_alias (id1, id2);
9321 /* Parse an objc-protocol-definition.
9323 objc-protocol-definition:
9324 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9325 @protocol identifier-list ;
9327 "@protocol identifier ;" should be resolved as "@protocol
9328 identifier-list ;": objc-methodprotolist may not start with a
9329 semicolon in the first alternative if objc-protocol-refs are
9330 omitted. */
9332 static void
9333 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9335 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9337 c_parser_consume_token (parser);
9338 if (c_parser_next_token_is_not (parser, CPP_NAME))
9340 c_parser_error (parser, "expected identifier");
9341 return;
9343 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9344 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9346 /* Any identifiers, including those declared as type names, are
9347 OK here. */
9348 while (true)
9350 tree id;
9351 if (c_parser_next_token_is_not (parser, CPP_NAME))
9353 c_parser_error (parser, "expected identifier");
9354 break;
9356 id = c_parser_peek_token (parser)->value;
9357 objc_declare_protocol (id, attributes);
9358 c_parser_consume_token (parser);
9359 if (c_parser_next_token_is (parser, CPP_COMMA))
9360 c_parser_consume_token (parser);
9361 else
9362 break;
9364 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9366 else
9368 tree id = c_parser_peek_token (parser)->value;
9369 tree proto = NULL_TREE;
9370 c_parser_consume_token (parser);
9371 if (c_parser_next_token_is (parser, CPP_LESS))
9372 proto = c_parser_objc_protocol_refs (parser);
9373 parser->objc_pq_context = true;
9374 objc_start_protocol (id, proto, attributes);
9375 c_parser_objc_methodprotolist (parser);
9376 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9377 parser->objc_pq_context = false;
9378 objc_finish_interface ();
9382 /* Parse an objc-method-type.
9384 objc-method-type:
9388 Return true if it is a class method (+) and false if it is
9389 an instance method (-).
9391 static inline bool
9392 c_parser_objc_method_type (c_parser *parser)
9394 switch (c_parser_peek_token (parser)->type)
9396 case CPP_PLUS:
9397 c_parser_consume_token (parser);
9398 return true;
9399 case CPP_MINUS:
9400 c_parser_consume_token (parser);
9401 return false;
9402 default:
9403 gcc_unreachable ();
9407 /* Parse an objc-method-definition.
9409 objc-method-definition:
9410 objc-method-type objc-method-decl ;[opt] compound-statement
9413 static void
9414 c_parser_objc_method_definition (c_parser *parser)
9416 bool is_class_method = c_parser_objc_method_type (parser);
9417 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9418 parser->objc_pq_context = true;
9419 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9420 &expr);
9421 if (decl == error_mark_node)
9422 return; /* Bail here. */
9424 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9426 c_parser_consume_token (parser);
9427 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9428 "extra semicolon in method definition specified");
9431 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9433 c_parser_error (parser, "expected %<{%>");
9434 return;
9437 parser->objc_pq_context = false;
9438 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9440 add_stmt (c_parser_compound_statement (parser));
9441 objc_finish_method_definition (current_function_decl);
9443 else
9445 /* This code is executed when we find a method definition
9446 outside of an @implementation context (or invalid for other
9447 reasons). Parse the method (to keep going) but do not emit
9448 any code.
9450 c_parser_compound_statement (parser);
9454 /* Parse an objc-methodprotolist.
9456 objc-methodprotolist:
9457 empty
9458 objc-methodprotolist objc-methodproto
9459 objc-methodprotolist declaration
9460 objc-methodprotolist ;
9461 @optional
9462 @required
9464 The declaration is a data definition, which may be missing
9465 declaration specifiers under the same rules and diagnostics as
9466 other data definitions outside functions, and the stray semicolon
9467 is diagnosed the same way as a stray semicolon outside a
9468 function. */
9470 static void
9471 c_parser_objc_methodprotolist (c_parser *parser)
9473 while (true)
9475 /* The list is terminated by @end. */
9476 switch (c_parser_peek_token (parser)->type)
9478 case CPP_SEMICOLON:
9479 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9480 "ISO C does not allow extra %<;%> outside of a function");
9481 c_parser_consume_token (parser);
9482 break;
9483 case CPP_PLUS:
9484 case CPP_MINUS:
9485 c_parser_objc_methodproto (parser);
9486 break;
9487 case CPP_PRAGMA:
9488 c_parser_pragma (parser, pragma_external, NULL);
9489 break;
9490 case CPP_EOF:
9491 return;
9492 default:
9493 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9494 return;
9495 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9496 c_parser_objc_at_property_declaration (parser);
9497 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9499 objc_set_method_opt (true);
9500 c_parser_consume_token (parser);
9502 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9504 objc_set_method_opt (false);
9505 c_parser_consume_token (parser);
9507 else
9508 c_parser_declaration_or_fndef (parser, false, false, true,
9509 false, true, NULL, vNULL);
9510 break;
9515 /* Parse an objc-methodproto.
9517 objc-methodproto:
9518 objc-method-type objc-method-decl ;
9521 static void
9522 c_parser_objc_methodproto (c_parser *parser)
9524 bool is_class_method = c_parser_objc_method_type (parser);
9525 tree decl, attributes = NULL_TREE;
9527 /* Remember protocol qualifiers in prototypes. */
9528 parser->objc_pq_context = true;
9529 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9530 NULL);
9531 /* Forget protocol qualifiers now. */
9532 parser->objc_pq_context = false;
9534 /* Do not allow the presence of attributes to hide an erroneous
9535 method implementation in the interface section. */
9536 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9538 c_parser_error (parser, "expected %<;%>");
9539 return;
9542 if (decl != error_mark_node)
9543 objc_add_method_declaration (is_class_method, decl, attributes);
9545 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9548 /* If we are at a position that method attributes may be present, check that
9549 there are not any parsed already (a syntax error) and then collect any
9550 specified at the current location. Finally, if new attributes were present,
9551 check that the next token is legal ( ';' for decls and '{' for defs). */
9553 static bool
9554 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9556 bool bad = false;
9557 if (*attributes)
9559 c_parser_error (parser,
9560 "method attributes must be specified at the end only");
9561 *attributes = NULL_TREE;
9562 bad = true;
9565 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9566 *attributes = c_parser_attributes (parser);
9568 /* If there were no attributes here, just report any earlier error. */
9569 if (*attributes == NULL_TREE || bad)
9570 return bad;
9572 /* If the attributes are followed by a ; or {, then just report any earlier
9573 error. */
9574 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9575 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9576 return bad;
9578 /* We've got attributes, but not at the end. */
9579 c_parser_error (parser,
9580 "expected %<;%> or %<{%> after method attribute definition");
9581 return true;
9584 /* Parse an objc-method-decl.
9586 objc-method-decl:
9587 ( objc-type-name ) objc-selector
9588 objc-selector
9589 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9590 objc-keyword-selector objc-optparmlist
9591 attributes
9593 objc-keyword-selector:
9594 objc-keyword-decl
9595 objc-keyword-selector objc-keyword-decl
9597 objc-keyword-decl:
9598 objc-selector : ( objc-type-name ) identifier
9599 objc-selector : identifier
9600 : ( objc-type-name ) identifier
9601 : identifier
9603 objc-optparmlist:
9604 objc-optparms objc-optellipsis
9606 objc-optparms:
9607 empty
9608 objc-opt-parms , parameter-declaration
9610 objc-optellipsis:
9611 empty
9612 , ...
9615 static tree
9616 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9617 tree *attributes, tree *expr)
9619 tree type = NULL_TREE;
9620 tree sel;
9621 tree parms = NULL_TREE;
9622 bool ellipsis = false;
9623 bool attr_err = false;
9625 *attributes = NULL_TREE;
9626 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9628 matching_parens parens;
9629 parens.consume_open (parser);
9630 type = c_parser_objc_type_name (parser);
9631 parens.skip_until_found_close (parser);
9633 sel = c_parser_objc_selector (parser);
9634 /* If there is no selector, or a colon follows, we have an
9635 objc-keyword-selector. If there is a selector, and a colon does
9636 not follow, that selector ends the objc-method-decl. */
9637 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9639 tree tsel = sel;
9640 tree list = NULL_TREE;
9641 while (true)
9643 tree atype = NULL_TREE, id, keyworddecl;
9644 tree param_attr = NULL_TREE;
9645 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9646 break;
9647 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9649 c_parser_consume_token (parser);
9650 atype = c_parser_objc_type_name (parser);
9651 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9652 "expected %<)%>");
9654 /* New ObjC allows attributes on method parameters. */
9655 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9656 param_attr = c_parser_attributes (parser);
9657 if (c_parser_next_token_is_not (parser, CPP_NAME))
9659 c_parser_error (parser, "expected identifier");
9660 return error_mark_node;
9662 id = c_parser_peek_token (parser)->value;
9663 c_parser_consume_token (parser);
9664 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9665 list = chainon (list, keyworddecl);
9666 tsel = c_parser_objc_selector (parser);
9667 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9668 break;
9671 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9673 /* Parse the optional parameter list. Optional Objective-C
9674 method parameters follow the C syntax, and may include '...'
9675 to denote a variable number of arguments. */
9676 parms = make_node (TREE_LIST);
9677 while (c_parser_next_token_is (parser, CPP_COMMA))
9679 struct c_parm *parm;
9680 c_parser_consume_token (parser);
9681 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9683 ellipsis = true;
9684 c_parser_consume_token (parser);
9685 attr_err |= c_parser_objc_maybe_method_attributes
9686 (parser, attributes) ;
9687 break;
9689 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9690 if (parm == NULL)
9691 break;
9692 parms = chainon (parms,
9693 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9695 sel = list;
9697 else
9698 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9700 if (sel == NULL)
9702 c_parser_error (parser, "objective-c method declaration is expected");
9703 return error_mark_node;
9706 if (attr_err)
9707 return error_mark_node;
9709 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9712 /* Parse an objc-type-name.
9714 objc-type-name:
9715 objc-type-qualifiers[opt] type-name
9716 objc-type-qualifiers[opt]
9718 objc-type-qualifiers:
9719 objc-type-qualifier
9720 objc-type-qualifiers objc-type-qualifier
9722 objc-type-qualifier: one of
9723 in out inout bycopy byref oneway
9726 static tree
9727 c_parser_objc_type_name (c_parser *parser)
9729 tree quals = NULL_TREE;
9730 struct c_type_name *type_name = NULL;
9731 tree type = NULL_TREE;
9732 while (true)
9734 c_token *token = c_parser_peek_token (parser);
9735 if (token->type == CPP_KEYWORD
9736 && (token->keyword == RID_IN
9737 || token->keyword == RID_OUT
9738 || token->keyword == RID_INOUT
9739 || token->keyword == RID_BYCOPY
9740 || token->keyword == RID_BYREF
9741 || token->keyword == RID_ONEWAY))
9743 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9744 c_parser_consume_token (parser);
9746 else
9747 break;
9749 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9750 type_name = c_parser_type_name (parser);
9751 if (type_name)
9752 type = groktypename (type_name, NULL, NULL);
9754 /* If the type is unknown, and error has already been produced and
9755 we need to recover from the error. In that case, use NULL_TREE
9756 for the type, as if no type had been specified; this will use the
9757 default type ('id') which is good for error recovery. */
9758 if (type == error_mark_node)
9759 type = NULL_TREE;
9761 return build_tree_list (quals, type);
9764 /* Parse objc-protocol-refs.
9766 objc-protocol-refs:
9767 < identifier-list >
9770 static tree
9771 c_parser_objc_protocol_refs (c_parser *parser)
9773 tree list = NULL_TREE;
9774 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9775 c_parser_consume_token (parser);
9776 /* Any identifiers, including those declared as type names, are OK
9777 here. */
9778 while (true)
9780 tree id;
9781 if (c_parser_next_token_is_not (parser, CPP_NAME))
9783 c_parser_error (parser, "expected identifier");
9784 break;
9786 id = c_parser_peek_token (parser)->value;
9787 list = chainon (list, build_tree_list (NULL_TREE, id));
9788 c_parser_consume_token (parser);
9789 if (c_parser_next_token_is (parser, CPP_COMMA))
9790 c_parser_consume_token (parser);
9791 else
9792 break;
9794 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9795 return list;
9798 /* Parse an objc-try-catch-finally-statement.
9800 objc-try-catch-finally-statement:
9801 @try compound-statement objc-catch-list[opt]
9802 @try compound-statement objc-catch-list[opt] @finally compound-statement
9804 objc-catch-list:
9805 @catch ( objc-catch-parameter-declaration ) compound-statement
9806 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9808 objc-catch-parameter-declaration:
9809 parameter-declaration
9810 '...'
9812 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9814 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9815 for C++. Keep them in sync. */
9817 static void
9818 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9820 location_t location;
9821 tree stmt;
9823 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9824 c_parser_consume_token (parser);
9825 location = c_parser_peek_token (parser)->location;
9826 objc_maybe_warn_exceptions (location);
9827 stmt = c_parser_compound_statement (parser);
9828 objc_begin_try_stmt (location, stmt);
9830 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9832 struct c_parm *parm;
9833 tree parameter_declaration = error_mark_node;
9834 bool seen_open_paren = false;
9836 c_parser_consume_token (parser);
9837 matching_parens parens;
9838 if (!parens.require_open (parser))
9839 seen_open_paren = true;
9840 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9842 /* We have "@catch (...)" (where the '...' are literally
9843 what is in the code). Skip the '...'.
9844 parameter_declaration is set to NULL_TREE, and
9845 objc_being_catch_clauses() knows that that means
9846 '...'. */
9847 c_parser_consume_token (parser);
9848 parameter_declaration = NULL_TREE;
9850 else
9852 /* We have "@catch (NSException *exception)" or something
9853 like that. Parse the parameter declaration. */
9854 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9855 if (parm == NULL)
9856 parameter_declaration = error_mark_node;
9857 else
9858 parameter_declaration = grokparm (parm, NULL);
9860 if (seen_open_paren)
9861 parens.require_close (parser);
9862 else
9864 /* If there was no open parenthesis, we are recovering from
9865 an error, and we are trying to figure out what mistake
9866 the user has made. */
9868 /* If there is an immediate closing parenthesis, the user
9869 probably forgot the opening one (ie, they typed "@catch
9870 NSException *e)". Parse the closing parenthesis and keep
9871 going. */
9872 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9873 c_parser_consume_token (parser);
9875 /* If these is no immediate closing parenthesis, the user
9876 probably doesn't know that parenthesis are required at
9877 all (ie, they typed "@catch NSException *e"). So, just
9878 forget about the closing parenthesis and keep going. */
9880 objc_begin_catch_clause (parameter_declaration);
9881 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9882 c_parser_compound_statement_nostart (parser);
9883 objc_finish_catch_clause ();
9885 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9887 c_parser_consume_token (parser);
9888 location = c_parser_peek_token (parser)->location;
9889 stmt = c_parser_compound_statement (parser);
9890 objc_build_finally_clause (location, stmt);
9892 objc_finish_try_stmt ();
9895 /* Parse an objc-synchronized-statement.
9897 objc-synchronized-statement:
9898 @synchronized ( expression ) compound-statement
9901 static void
9902 c_parser_objc_synchronized_statement (c_parser *parser)
9904 location_t loc;
9905 tree expr, stmt;
9906 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9907 c_parser_consume_token (parser);
9908 loc = c_parser_peek_token (parser)->location;
9909 objc_maybe_warn_exceptions (loc);
9910 matching_parens parens;
9911 if (parens.require_open (parser))
9913 struct c_expr ce = c_parser_expression (parser);
9914 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9915 expr = ce.value;
9916 expr = c_fully_fold (expr, false, NULL);
9917 parens.skip_until_found_close (parser);
9919 else
9920 expr = error_mark_node;
9921 stmt = c_parser_compound_statement (parser);
9922 objc_build_synchronized (loc, expr, stmt);
9925 /* Parse an objc-selector; return NULL_TREE without an error if the
9926 next token is not an objc-selector.
9928 objc-selector:
9929 identifier
9930 one of
9931 enum struct union if else while do for switch case default
9932 break continue return goto asm sizeof typeof __alignof
9933 unsigned long const short volatile signed restrict _Complex
9934 in out inout bycopy byref oneway int char float double void _Bool
9935 _Atomic
9937 ??? Why this selection of keywords but not, for example, storage
9938 class specifiers? */
9940 static tree
9941 c_parser_objc_selector (c_parser *parser)
9943 c_token *token = c_parser_peek_token (parser);
9944 tree value = token->value;
9945 if (token->type == CPP_NAME)
9947 c_parser_consume_token (parser);
9948 return value;
9950 if (token->type != CPP_KEYWORD)
9951 return NULL_TREE;
9952 switch (token->keyword)
9954 case RID_ENUM:
9955 case RID_STRUCT:
9956 case RID_UNION:
9957 case RID_IF:
9958 case RID_ELSE:
9959 case RID_WHILE:
9960 case RID_DO:
9961 case RID_FOR:
9962 case RID_SWITCH:
9963 case RID_CASE:
9964 case RID_DEFAULT:
9965 case RID_BREAK:
9966 case RID_CONTINUE:
9967 case RID_RETURN:
9968 case RID_GOTO:
9969 case RID_ASM:
9970 case RID_SIZEOF:
9971 case RID_TYPEOF:
9972 case RID_ALIGNOF:
9973 case RID_UNSIGNED:
9974 case RID_LONG:
9975 case RID_CONST:
9976 case RID_SHORT:
9977 case RID_VOLATILE:
9978 case RID_SIGNED:
9979 case RID_RESTRICT:
9980 case RID_COMPLEX:
9981 case RID_IN:
9982 case RID_OUT:
9983 case RID_INOUT:
9984 case RID_BYCOPY:
9985 case RID_BYREF:
9986 case RID_ONEWAY:
9987 case RID_INT:
9988 case RID_CHAR:
9989 case RID_FLOAT:
9990 case RID_DOUBLE:
9991 CASE_RID_FLOATN_NX:
9992 case RID_VOID:
9993 case RID_BOOL:
9994 case RID_ATOMIC:
9995 case RID_AUTO_TYPE:
9996 case RID_INT_N_0:
9997 case RID_INT_N_1:
9998 case RID_INT_N_2:
9999 case RID_INT_N_3:
10000 c_parser_consume_token (parser);
10001 return value;
10002 default:
10003 return NULL_TREE;
10007 /* Parse an objc-selector-arg.
10009 objc-selector-arg:
10010 objc-selector
10011 objc-keywordname-list
10013 objc-keywordname-list:
10014 objc-keywordname
10015 objc-keywordname-list objc-keywordname
10017 objc-keywordname:
10018 objc-selector :
10022 static tree
10023 c_parser_objc_selector_arg (c_parser *parser)
10025 tree sel = c_parser_objc_selector (parser);
10026 tree list = NULL_TREE;
10027 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10028 return sel;
10029 while (true)
10031 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10032 return list;
10033 list = chainon (list, build_tree_list (sel, NULL_TREE));
10034 sel = c_parser_objc_selector (parser);
10035 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10036 break;
10038 return list;
10041 /* Parse an objc-receiver.
10043 objc-receiver:
10044 expression
10045 class-name
10046 type-name
10049 static tree
10050 c_parser_objc_receiver (c_parser *parser)
10052 location_t loc = c_parser_peek_token (parser)->location;
10054 if (c_parser_peek_token (parser)->type == CPP_NAME
10055 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10056 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10058 tree id = c_parser_peek_token (parser)->value;
10059 c_parser_consume_token (parser);
10060 return objc_get_class_reference (id);
10062 struct c_expr ce = c_parser_expression (parser);
10063 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10064 return c_fully_fold (ce.value, false, NULL);
10067 /* Parse objc-message-args.
10069 objc-message-args:
10070 objc-selector
10071 objc-keywordarg-list
10073 objc-keywordarg-list:
10074 objc-keywordarg
10075 objc-keywordarg-list objc-keywordarg
10077 objc-keywordarg:
10078 objc-selector : objc-keywordexpr
10079 : objc-keywordexpr
10082 static tree
10083 c_parser_objc_message_args (c_parser *parser)
10085 tree sel = c_parser_objc_selector (parser);
10086 tree list = NULL_TREE;
10087 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10088 return sel;
10089 while (true)
10091 tree keywordexpr;
10092 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10093 return error_mark_node;
10094 keywordexpr = c_parser_objc_keywordexpr (parser);
10095 list = chainon (list, build_tree_list (sel, keywordexpr));
10096 sel = c_parser_objc_selector (parser);
10097 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10098 break;
10100 return list;
10103 /* Parse an objc-keywordexpr.
10105 objc-keywordexpr:
10106 nonempty-expr-list
10109 static tree
10110 c_parser_objc_keywordexpr (c_parser *parser)
10112 tree ret;
10113 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10114 NULL, NULL, NULL, NULL);
10115 if (vec_safe_length (expr_list) == 1)
10117 /* Just return the expression, remove a level of
10118 indirection. */
10119 ret = (*expr_list)[0];
10121 else
10123 /* We have a comma expression, we will collapse later. */
10124 ret = build_tree_list_vec (expr_list);
10126 release_tree_vector (expr_list);
10127 return ret;
10130 /* A check, needed in several places, that ObjC interface, implementation or
10131 method definitions are not prefixed by incorrect items. */
10132 static bool
10133 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10134 struct c_declspecs *specs)
10136 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10137 || specs->typespec_kind != ctsk_none)
10139 c_parser_error (parser,
10140 "no type or storage class may be specified here,");
10141 c_parser_skip_to_end_of_block_or_statement (parser);
10142 return true;
10144 return false;
10147 /* Parse an Objective-C @property declaration. The syntax is:
10149 objc-property-declaration:
10150 '@property' objc-property-attributes[opt] struct-declaration ;
10152 objc-property-attributes:
10153 '(' objc-property-attribute-list ')'
10155 objc-property-attribute-list:
10156 objc-property-attribute
10157 objc-property-attribute-list, objc-property-attribute
10159 objc-property-attribute
10160 'getter' = identifier
10161 'setter' = identifier
10162 'readonly'
10163 'readwrite'
10164 'assign'
10165 'retain'
10166 'copy'
10167 'nonatomic'
10169 For example:
10170 @property NSString *name;
10171 @property (readonly) id object;
10172 @property (retain, nonatomic, getter=getTheName) id name;
10173 @property int a, b, c;
10175 PS: This function is identical to cp_parser_objc_at_propery_declaration
10176 for C++. Keep them in sync. */
10177 static void
10178 c_parser_objc_at_property_declaration (c_parser *parser)
10180 /* The following variables hold the attributes of the properties as
10181 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10182 seen. When we see an attribute, we set them to 'true' (if they
10183 are boolean properties) or to the identifier (if they have an
10184 argument, ie, for getter and setter). Note that here we only
10185 parse the list of attributes, check the syntax and accumulate the
10186 attributes that we find. objc_add_property_declaration() will
10187 then process the information. */
10188 bool property_assign = false;
10189 bool property_copy = false;
10190 tree property_getter_ident = NULL_TREE;
10191 bool property_nonatomic = false;
10192 bool property_readonly = false;
10193 bool property_readwrite = false;
10194 bool property_retain = false;
10195 tree property_setter_ident = NULL_TREE;
10197 /* 'properties' is the list of properties that we read. Usually a
10198 single one, but maybe more (eg, in "@property int a, b, c;" there
10199 are three). */
10200 tree properties;
10201 location_t loc;
10203 loc = c_parser_peek_token (parser)->location;
10204 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10206 c_parser_consume_token (parser); /* Eat '@property'. */
10208 /* Parse the optional attribute list... */
10209 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10211 matching_parens parens;
10213 /* Eat the '(' */
10214 parens.consume_open (parser);
10216 /* Property attribute keywords are valid now. */
10217 parser->objc_property_attr_context = true;
10219 while (true)
10221 bool syntax_error = false;
10222 c_token *token = c_parser_peek_token (parser);
10223 enum rid keyword;
10225 if (token->type != CPP_KEYWORD)
10227 if (token->type == CPP_CLOSE_PAREN)
10228 c_parser_error (parser, "expected identifier");
10229 else
10231 c_parser_consume_token (parser);
10232 c_parser_error (parser, "unknown property attribute");
10234 break;
10236 keyword = token->keyword;
10237 c_parser_consume_token (parser);
10238 switch (keyword)
10240 case RID_ASSIGN: property_assign = true; break;
10241 case RID_COPY: property_copy = true; break;
10242 case RID_NONATOMIC: property_nonatomic = true; break;
10243 case RID_READONLY: property_readonly = true; break;
10244 case RID_READWRITE: property_readwrite = true; break;
10245 case RID_RETAIN: property_retain = true; break;
10247 case RID_GETTER:
10248 case RID_SETTER:
10249 if (c_parser_next_token_is_not (parser, CPP_EQ))
10251 if (keyword == RID_GETTER)
10252 c_parser_error (parser,
10253 "missing %<=%> (after %<getter%> attribute)");
10254 else
10255 c_parser_error (parser,
10256 "missing %<=%> (after %<setter%> attribute)");
10257 syntax_error = true;
10258 break;
10260 c_parser_consume_token (parser); /* eat the = */
10261 if (c_parser_next_token_is_not (parser, CPP_NAME))
10263 c_parser_error (parser, "expected identifier");
10264 syntax_error = true;
10265 break;
10267 if (keyword == RID_SETTER)
10269 if (property_setter_ident != NULL_TREE)
10270 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10271 else
10272 property_setter_ident = c_parser_peek_token (parser)->value;
10273 c_parser_consume_token (parser);
10274 if (c_parser_next_token_is_not (parser, CPP_COLON))
10275 c_parser_error (parser, "setter name must terminate with %<:%>");
10276 else
10277 c_parser_consume_token (parser);
10279 else
10281 if (property_getter_ident != NULL_TREE)
10282 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10283 else
10284 property_getter_ident = c_parser_peek_token (parser)->value;
10285 c_parser_consume_token (parser);
10287 break;
10288 default:
10289 c_parser_error (parser, "unknown property attribute");
10290 syntax_error = true;
10291 break;
10294 if (syntax_error)
10295 break;
10297 if (c_parser_next_token_is (parser, CPP_COMMA))
10298 c_parser_consume_token (parser);
10299 else
10300 break;
10302 parser->objc_property_attr_context = false;
10303 parens.skip_until_found_close (parser);
10305 /* ... and the property declaration(s). */
10306 properties = c_parser_struct_declaration (parser);
10308 if (properties == error_mark_node)
10310 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10311 parser->error = false;
10312 return;
10315 if (properties == NULL_TREE)
10316 c_parser_error (parser, "expected identifier");
10317 else
10319 /* Comma-separated properties are chained together in
10320 reverse order; add them one by one. */
10321 properties = nreverse (properties);
10323 for (; properties; properties = TREE_CHAIN (properties))
10324 objc_add_property_declaration (loc, copy_node (properties),
10325 property_readonly, property_readwrite,
10326 property_assign, property_retain,
10327 property_copy, property_nonatomic,
10328 property_getter_ident, property_setter_ident);
10331 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10332 parser->error = false;
10335 /* Parse an Objective-C @synthesize declaration. The syntax is:
10337 objc-synthesize-declaration:
10338 @synthesize objc-synthesize-identifier-list ;
10340 objc-synthesize-identifier-list:
10341 objc-synthesize-identifier
10342 objc-synthesize-identifier-list, objc-synthesize-identifier
10344 objc-synthesize-identifier
10345 identifier
10346 identifier = identifier
10348 For example:
10349 @synthesize MyProperty;
10350 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10352 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10353 for C++. Keep them in sync.
10355 static void
10356 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10358 tree list = NULL_TREE;
10359 location_t loc;
10360 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10361 loc = c_parser_peek_token (parser)->location;
10363 c_parser_consume_token (parser);
10364 while (true)
10366 tree property, ivar;
10367 if (c_parser_next_token_is_not (parser, CPP_NAME))
10369 c_parser_error (parser, "expected identifier");
10370 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10371 /* Once we find the semicolon, we can resume normal parsing.
10372 We have to reset parser->error manually because
10373 c_parser_skip_until_found() won't reset it for us if the
10374 next token is precisely a semicolon. */
10375 parser->error = false;
10376 return;
10378 property = c_parser_peek_token (parser)->value;
10379 c_parser_consume_token (parser);
10380 if (c_parser_next_token_is (parser, CPP_EQ))
10382 c_parser_consume_token (parser);
10383 if (c_parser_next_token_is_not (parser, CPP_NAME))
10385 c_parser_error (parser, "expected identifier");
10386 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10387 parser->error = false;
10388 return;
10390 ivar = c_parser_peek_token (parser)->value;
10391 c_parser_consume_token (parser);
10393 else
10394 ivar = NULL_TREE;
10395 list = chainon (list, build_tree_list (ivar, property));
10396 if (c_parser_next_token_is (parser, CPP_COMMA))
10397 c_parser_consume_token (parser);
10398 else
10399 break;
10401 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10402 objc_add_synthesize_declaration (loc, list);
10405 /* Parse an Objective-C @dynamic declaration. The syntax is:
10407 objc-dynamic-declaration:
10408 @dynamic identifier-list ;
10410 For example:
10411 @dynamic MyProperty;
10412 @dynamic MyProperty, AnotherProperty;
10414 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10415 for C++. Keep them in sync.
10417 static void
10418 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10420 tree list = NULL_TREE;
10421 location_t loc;
10422 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10423 loc = c_parser_peek_token (parser)->location;
10425 c_parser_consume_token (parser);
10426 while (true)
10428 tree property;
10429 if (c_parser_next_token_is_not (parser, CPP_NAME))
10431 c_parser_error (parser, "expected identifier");
10432 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10433 parser->error = false;
10434 return;
10436 property = c_parser_peek_token (parser)->value;
10437 list = chainon (list, build_tree_list (NULL_TREE, property));
10438 c_parser_consume_token (parser);
10439 if (c_parser_next_token_is (parser, CPP_COMMA))
10440 c_parser_consume_token (parser);
10441 else
10442 break;
10444 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10445 objc_add_dynamic_declaration (loc, list);
10449 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10450 should be considered, statements. ALLOW_STMT is true if we're within
10451 the context of a function and such pragmas are to be allowed. Returns
10452 true if we actually parsed such a pragma. */
10454 static bool
10455 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10457 unsigned int id;
10458 const char *construct = NULL;
10460 id = c_parser_peek_token (parser)->pragma_kind;
10461 gcc_assert (id != PRAGMA_NONE);
10463 switch (id)
10465 case PRAGMA_OACC_DECLARE:
10466 c_parser_oacc_declare (parser);
10467 return false;
10469 case PRAGMA_OACC_ENTER_DATA:
10470 if (context != pragma_compound)
10472 construct = "acc enter data";
10473 in_compound:
10474 if (context == pragma_stmt)
10476 error_at (c_parser_peek_token (parser)->location,
10477 "%<#pragma %s%> may only be used in compound "
10478 "statements", construct);
10479 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10480 return false;
10482 goto bad_stmt;
10484 c_parser_oacc_enter_exit_data (parser, true);
10485 return false;
10487 case PRAGMA_OACC_EXIT_DATA:
10488 if (context != pragma_compound)
10490 construct = "acc exit data";
10491 goto in_compound;
10493 c_parser_oacc_enter_exit_data (parser, false);
10494 return false;
10496 case PRAGMA_OACC_ROUTINE:
10497 if (context != pragma_external)
10499 error_at (c_parser_peek_token (parser)->location,
10500 "%<#pragma acc routine%> must be at file scope");
10501 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10502 return false;
10504 c_parser_oacc_routine (parser, context);
10505 return false;
10507 case PRAGMA_OACC_UPDATE:
10508 if (context != pragma_compound)
10510 construct = "acc update";
10511 goto in_compound;
10513 c_parser_oacc_update (parser);
10514 return false;
10516 case PRAGMA_OMP_BARRIER:
10517 if (context != pragma_compound)
10519 construct = "omp barrier";
10520 goto in_compound;
10522 c_parser_omp_barrier (parser);
10523 return false;
10525 case PRAGMA_OMP_FLUSH:
10526 if (context != pragma_compound)
10528 construct = "omp flush";
10529 goto in_compound;
10531 c_parser_omp_flush (parser);
10532 return false;
10534 case PRAGMA_OMP_TASKWAIT:
10535 if (context != pragma_compound)
10537 construct = "omp taskwait";
10538 goto in_compound;
10540 c_parser_omp_taskwait (parser);
10541 return false;
10543 case PRAGMA_OMP_TASKYIELD:
10544 if (context != pragma_compound)
10546 construct = "omp taskyield";
10547 goto in_compound;
10549 c_parser_omp_taskyield (parser);
10550 return false;
10552 case PRAGMA_OMP_CANCEL:
10553 if (context != pragma_compound)
10555 construct = "omp cancel";
10556 goto in_compound;
10558 c_parser_omp_cancel (parser);
10559 return false;
10561 case PRAGMA_OMP_CANCELLATION_POINT:
10562 c_parser_omp_cancellation_point (parser, context);
10563 return false;
10565 case PRAGMA_OMP_THREADPRIVATE:
10566 c_parser_omp_threadprivate (parser);
10567 return false;
10569 case PRAGMA_OMP_TARGET:
10570 return c_parser_omp_target (parser, context, if_p);
10572 case PRAGMA_OMP_END_DECLARE_TARGET:
10573 c_parser_omp_end_declare_target (parser);
10574 return false;
10576 case PRAGMA_OMP_SECTION:
10577 error_at (c_parser_peek_token (parser)->location,
10578 "%<#pragma omp section%> may only be used in "
10579 "%<#pragma omp sections%> construct");
10580 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10581 return false;
10583 case PRAGMA_OMP_DECLARE:
10584 c_parser_omp_declare (parser, context);
10585 return false;
10587 case PRAGMA_OMP_ORDERED:
10588 return c_parser_omp_ordered (parser, context, if_p);
10590 case PRAGMA_IVDEP:
10591 c_parser_consume_pragma (parser);
10592 c_parser_skip_to_pragma_eol (parser);
10593 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10594 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10595 && !c_parser_next_token_is_keyword (parser, RID_DO))
10597 c_parser_error (parser, "for, while or do statement expected");
10598 return false;
10600 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10601 c_parser_for_statement (parser, true, if_p);
10602 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10603 c_parser_while_statement (parser, true, if_p);
10604 else
10605 c_parser_do_statement (parser, true);
10606 return false;
10608 case PRAGMA_GCC_PCH_PREPROCESS:
10609 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10610 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10611 return false;
10613 case PRAGMA_CILK_SIMD:
10614 if (!c_parser_cilk_verify_simd (parser, context))
10615 return false;
10616 c_parser_consume_pragma (parser);
10617 c_parser_cilk_simd (parser, if_p);
10618 return false;
10619 case PRAGMA_CILK_GRAINSIZE:
10620 if (!flag_cilkplus)
10622 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10623 " enabled");
10624 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10625 return false;
10627 if (context == pragma_external)
10629 error_at (c_parser_peek_token (parser)->location,
10630 "%<#pragma grainsize%> must be inside a function");
10631 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10632 return false;
10634 c_parser_cilk_grainsize (parser, if_p);
10635 return false;
10637 case PRAGMA_OACC_WAIT:
10638 if (context != pragma_compound)
10640 construct = "acc wait";
10641 goto in_compound;
10643 /* FALL THROUGH. */
10645 default:
10646 if (id < PRAGMA_FIRST_EXTERNAL)
10648 if (context != pragma_stmt && context != pragma_compound)
10650 bad_stmt:
10651 c_parser_error (parser, "expected declaration specifiers");
10652 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10653 return false;
10655 c_parser_omp_construct (parser, if_p);
10656 return true;
10658 break;
10661 c_parser_consume_pragma (parser);
10662 c_invoke_pragma_handler (id);
10664 /* Skip to EOL, but suppress any error message. Those will have been
10665 generated by the handler routine through calling error, as opposed
10666 to calling c_parser_error. */
10667 parser->error = true;
10668 c_parser_skip_to_pragma_eol (parser);
10670 return false;
10673 /* The interface the pragma parsers have to the lexer. */
10675 enum cpp_ttype
10676 pragma_lex (tree *value, location_t *loc)
10678 c_token *tok = c_parser_peek_token (the_parser);
10679 enum cpp_ttype ret = tok->type;
10681 *value = tok->value;
10682 if (loc)
10683 *loc = tok->location;
10685 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10686 ret = CPP_EOF;
10687 else
10689 if (ret == CPP_KEYWORD)
10690 ret = CPP_NAME;
10691 c_parser_consume_token (the_parser);
10694 return ret;
10697 static void
10698 c_parser_pragma_pch_preprocess (c_parser *parser)
10700 tree name = NULL;
10702 c_parser_consume_pragma (parser);
10703 if (c_parser_next_token_is (parser, CPP_STRING))
10705 name = c_parser_peek_token (parser)->value;
10706 c_parser_consume_token (parser);
10708 else
10709 c_parser_error (parser, "expected string literal");
10710 c_parser_skip_to_pragma_eol (parser);
10712 if (name)
10713 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10716 /* OpenACC and OpenMP parsing routines. */
10718 /* Returns name of the next clause.
10719 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10720 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10721 returned and the token is consumed. */
10723 static pragma_omp_clause
10724 c_parser_omp_clause_name (c_parser *parser)
10726 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10728 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10729 result = PRAGMA_OACC_CLAUSE_AUTO;
10730 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10731 result = PRAGMA_OMP_CLAUSE_IF;
10732 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10733 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10734 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10735 result = PRAGMA_OMP_CLAUSE_FOR;
10736 else if (c_parser_next_token_is (parser, CPP_NAME))
10738 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10740 switch (p[0])
10742 case 'a':
10743 if (!strcmp ("aligned", p))
10744 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10745 else if (!strcmp ("async", p))
10746 result = PRAGMA_OACC_CLAUSE_ASYNC;
10747 break;
10748 case 'c':
10749 if (!strcmp ("collapse", p))
10750 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10751 else if (!strcmp ("copy", p))
10752 result = PRAGMA_OACC_CLAUSE_COPY;
10753 else if (!strcmp ("copyin", p))
10754 result = PRAGMA_OMP_CLAUSE_COPYIN;
10755 else if (!strcmp ("copyout", p))
10756 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10757 else if (!strcmp ("copyprivate", p))
10758 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10759 else if (!strcmp ("create", p))
10760 result = PRAGMA_OACC_CLAUSE_CREATE;
10761 break;
10762 case 'd':
10763 if (!strcmp ("defaultmap", p))
10764 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10765 else if (!strcmp ("delete", p))
10766 result = PRAGMA_OACC_CLAUSE_DELETE;
10767 else if (!strcmp ("depend", p))
10768 result = PRAGMA_OMP_CLAUSE_DEPEND;
10769 else if (!strcmp ("device", p))
10770 result = PRAGMA_OMP_CLAUSE_DEVICE;
10771 else if (!strcmp ("deviceptr", p))
10772 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10773 else if (!strcmp ("device_resident", p))
10774 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10775 else if (!strcmp ("dist_schedule", p))
10776 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10777 break;
10778 case 'f':
10779 if (!strcmp ("final", p))
10780 result = PRAGMA_OMP_CLAUSE_FINAL;
10781 else if (!strcmp ("firstprivate", p))
10782 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10783 else if (!strcmp ("from", p))
10784 result = PRAGMA_OMP_CLAUSE_FROM;
10785 break;
10786 case 'g':
10787 if (!strcmp ("gang", p))
10788 result = PRAGMA_OACC_CLAUSE_GANG;
10789 else if (!strcmp ("grainsize", p))
10790 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10791 break;
10792 case 'h':
10793 if (!strcmp ("hint", p))
10794 result = PRAGMA_OMP_CLAUSE_HINT;
10795 else if (!strcmp ("host", p))
10796 result = PRAGMA_OACC_CLAUSE_HOST;
10797 break;
10798 case 'i':
10799 if (!strcmp ("inbranch", p))
10800 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10801 else if (!strcmp ("independent", p))
10802 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10803 else if (!strcmp ("is_device_ptr", p))
10804 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10805 break;
10806 case 'l':
10807 if (!strcmp ("lastprivate", p))
10808 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10809 else if (!strcmp ("linear", p))
10810 result = PRAGMA_OMP_CLAUSE_LINEAR;
10811 else if (!strcmp ("link", p))
10812 result = PRAGMA_OMP_CLAUSE_LINK;
10813 break;
10814 case 'm':
10815 if (!strcmp ("map", p))
10816 result = PRAGMA_OMP_CLAUSE_MAP;
10817 else if (!strcmp ("mergeable", p))
10818 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10819 else if (flag_cilkplus && !strcmp ("mask", p))
10820 result = PRAGMA_CILK_CLAUSE_MASK;
10821 break;
10822 case 'n':
10823 if (!strcmp ("nogroup", p))
10824 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10825 else if (!strcmp ("notinbranch", p))
10826 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10827 else if (!strcmp ("nowait", p))
10828 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10829 else if (!strcmp ("num_gangs", p))
10830 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10831 else if (!strcmp ("num_tasks", p))
10832 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10833 else if (!strcmp ("num_teams", p))
10834 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10835 else if (!strcmp ("num_threads", p))
10836 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10837 else if (!strcmp ("num_workers", p))
10838 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10839 else if (flag_cilkplus && !strcmp ("nomask", p))
10840 result = PRAGMA_CILK_CLAUSE_NOMASK;
10841 break;
10842 case 'o':
10843 if (!strcmp ("ordered", p))
10844 result = PRAGMA_OMP_CLAUSE_ORDERED;
10845 break;
10846 case 'p':
10847 if (!strcmp ("parallel", p))
10848 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10849 else if (!strcmp ("present", p))
10850 result = PRAGMA_OACC_CLAUSE_PRESENT;
10851 else if (!strcmp ("present_or_copy", p)
10852 || !strcmp ("pcopy", p))
10853 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10854 else if (!strcmp ("present_or_copyin", p)
10855 || !strcmp ("pcopyin", p))
10856 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10857 else if (!strcmp ("present_or_copyout", p)
10858 || !strcmp ("pcopyout", p))
10859 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10860 else if (!strcmp ("present_or_create", p)
10861 || !strcmp ("pcreate", p))
10862 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10863 else if (!strcmp ("priority", p))
10864 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10865 else if (!strcmp ("private", p))
10866 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10867 else if (!strcmp ("proc_bind", p))
10868 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10869 break;
10870 case 'r':
10871 if (!strcmp ("reduction", p))
10872 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10873 break;
10874 case 's':
10875 if (!strcmp ("safelen", p))
10876 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10877 else if (!strcmp ("schedule", p))
10878 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10879 else if (!strcmp ("sections", p))
10880 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10881 else if (!strcmp ("seq", p))
10882 result = PRAGMA_OACC_CLAUSE_SEQ;
10883 else if (!strcmp ("shared", p))
10884 result = PRAGMA_OMP_CLAUSE_SHARED;
10885 else if (!strcmp ("simd", p))
10886 result = PRAGMA_OMP_CLAUSE_SIMD;
10887 else if (!strcmp ("simdlen", p))
10888 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10889 else if (!strcmp ("self", p))
10890 result = PRAGMA_OACC_CLAUSE_SELF;
10891 break;
10892 case 't':
10893 if (!strcmp ("taskgroup", p))
10894 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10895 else if (!strcmp ("thread_limit", p))
10896 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10897 else if (!strcmp ("threads", p))
10898 result = PRAGMA_OMP_CLAUSE_THREADS;
10899 else if (!strcmp ("tile", p))
10900 result = PRAGMA_OACC_CLAUSE_TILE;
10901 else if (!strcmp ("to", p))
10902 result = PRAGMA_OMP_CLAUSE_TO;
10903 break;
10904 case 'u':
10905 if (!strcmp ("uniform", p))
10906 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10907 else if (!strcmp ("untied", p))
10908 result = PRAGMA_OMP_CLAUSE_UNTIED;
10909 else if (!strcmp ("use_device", p))
10910 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10911 else if (!strcmp ("use_device_ptr", p))
10912 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10913 break;
10914 case 'v':
10915 if (!strcmp ("vector", p))
10916 result = PRAGMA_OACC_CLAUSE_VECTOR;
10917 else if (!strcmp ("vector_length", p))
10918 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10919 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10920 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10921 break;
10922 case 'w':
10923 if (!strcmp ("wait", p))
10924 result = PRAGMA_OACC_CLAUSE_WAIT;
10925 else if (!strcmp ("worker", p))
10926 result = PRAGMA_OACC_CLAUSE_WORKER;
10927 break;
10931 if (result != PRAGMA_OMP_CLAUSE_NONE)
10932 c_parser_consume_token (parser);
10934 return result;
10937 /* Validate that a clause of the given type does not already exist. */
10939 static void
10940 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10941 const char *name)
10943 tree c;
10945 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10946 if (OMP_CLAUSE_CODE (c) == code)
10948 location_t loc = OMP_CLAUSE_LOCATION (c);
10949 error_at (loc, "too many %qs clauses", name);
10950 break;
10954 /* OpenACC 2.0
10955 Parse wait clause or wait directive parameters. */
10957 static tree
10958 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10960 vec<tree, va_gc> *args;
10961 tree t, args_tree;
10963 matching_parens parens;
10964 if (!parens.require_open (parser))
10965 return list;
10967 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10969 if (args->length () == 0)
10971 c_parser_error (parser, "expected integer expression before ')'");
10972 release_tree_vector (args);
10973 return list;
10976 args_tree = build_tree_list_vec (args);
10978 for (t = args_tree; t; t = TREE_CHAIN (t))
10980 tree targ = TREE_VALUE (t);
10982 if (targ != error_mark_node)
10984 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10986 c_parser_error (parser, "expression must be integral");
10987 targ = error_mark_node;
10989 else
10991 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10993 OMP_CLAUSE_DECL (c) = targ;
10994 OMP_CLAUSE_CHAIN (c) = list;
10995 list = c;
11000 release_tree_vector (args);
11001 parens.require_close (parser);
11002 return list;
11005 /* OpenACC 2.0, OpenMP 2.5:
11006 variable-list:
11007 identifier
11008 variable-list , identifier
11010 If KIND is nonzero, create the appropriate node and install the
11011 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11012 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11014 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11015 return the list created. */
11017 static tree
11018 c_parser_omp_variable_list (c_parser *parser,
11019 location_t clause_loc,
11020 enum omp_clause_code kind, tree list)
11022 if (c_parser_next_token_is_not (parser, CPP_NAME)
11023 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11024 c_parser_error (parser, "expected identifier");
11026 while (c_parser_next_token_is (parser, CPP_NAME)
11027 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11029 tree t = lookup_name (c_parser_peek_token (parser)->value);
11031 if (t == NULL_TREE)
11033 undeclared_variable (c_parser_peek_token (parser)->location,
11034 c_parser_peek_token (parser)->value);
11035 t = error_mark_node;
11038 c_parser_consume_token (parser);
11040 if (t == error_mark_node)
11042 else if (kind != 0)
11044 switch (kind)
11046 case OMP_CLAUSE__CACHE_:
11047 /* The OpenACC cache directive explicitly only allows "array
11048 elements or subarrays". */
11049 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11051 c_parser_error (parser, "expected %<[%>");
11052 t = error_mark_node;
11053 break;
11055 /* FALLTHROUGH */
11056 case OMP_CLAUSE_MAP:
11057 case OMP_CLAUSE_FROM:
11058 case OMP_CLAUSE_TO:
11059 while (c_parser_next_token_is (parser, CPP_DOT))
11061 location_t op_loc = c_parser_peek_token (parser)->location;
11062 c_parser_consume_token (parser);
11063 if (!c_parser_next_token_is (parser, CPP_NAME))
11065 c_parser_error (parser, "expected identifier");
11066 t = error_mark_node;
11067 break;
11070 c_token *comp_tok = c_parser_peek_token (parser);
11071 tree ident = comp_tok->value;
11072 location_t comp_loc = comp_tok->location;
11073 c_parser_consume_token (parser);
11074 t = build_component_ref (op_loc, t, ident, comp_loc);
11076 /* FALLTHROUGH */
11077 case OMP_CLAUSE_DEPEND:
11078 case OMP_CLAUSE_REDUCTION:
11079 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11081 tree low_bound = NULL_TREE, length = NULL_TREE;
11083 c_parser_consume_token (parser);
11084 if (!c_parser_next_token_is (parser, CPP_COLON))
11086 location_t expr_loc
11087 = c_parser_peek_token (parser)->location;
11088 c_expr expr = c_parser_expression (parser);
11089 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11090 false, true);
11091 low_bound = expr.value;
11093 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11094 length = integer_one_node;
11095 else
11097 /* Look for `:'. */
11098 if (!c_parser_require (parser, CPP_COLON,
11099 "expected %<:%>"))
11101 t = error_mark_node;
11102 break;
11104 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11106 location_t expr_loc
11107 = c_parser_peek_token (parser)->location;
11108 c_expr expr = c_parser_expression (parser);
11109 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11110 false, true);
11111 length = expr.value;
11114 /* Look for the closing `]'. */
11115 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11116 "expected %<]%>"))
11118 t = error_mark_node;
11119 break;
11122 t = tree_cons (low_bound, length, t);
11124 break;
11125 default:
11126 break;
11129 if (t != error_mark_node)
11131 tree u = build_omp_clause (clause_loc, kind);
11132 OMP_CLAUSE_DECL (u) = t;
11133 OMP_CLAUSE_CHAIN (u) = list;
11134 list = u;
11137 else
11138 list = tree_cons (t, NULL_TREE, list);
11140 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11141 break;
11143 c_parser_consume_token (parser);
11146 return list;
11149 /* Similarly, but expect leading and trailing parenthesis. This is a very
11150 common case for OpenACC and OpenMP clauses. */
11152 static tree
11153 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11154 tree list)
11156 /* The clauses location. */
11157 location_t loc = c_parser_peek_token (parser)->location;
11159 matching_parens parens;
11160 if (parens.require_open (parser))
11162 list = c_parser_omp_variable_list (parser, loc, kind, list);
11163 parens.skip_until_found_close (parser);
11165 return list;
11168 /* OpenACC 2.0:
11169 copy ( variable-list )
11170 copyin ( variable-list )
11171 copyout ( variable-list )
11172 create ( variable-list )
11173 delete ( variable-list )
11174 present ( variable-list )
11175 present_or_copy ( variable-list )
11176 pcopy ( variable-list )
11177 present_or_copyin ( variable-list )
11178 pcopyin ( variable-list )
11179 present_or_copyout ( variable-list )
11180 pcopyout ( variable-list )
11181 present_or_create ( variable-list )
11182 pcreate ( variable-list ) */
11184 static tree
11185 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11186 tree list)
11188 enum gomp_map_kind kind;
11189 switch (c_kind)
11191 case PRAGMA_OACC_CLAUSE_COPY:
11192 kind = GOMP_MAP_FORCE_TOFROM;
11193 break;
11194 case PRAGMA_OACC_CLAUSE_COPYIN:
11195 kind = GOMP_MAP_FORCE_TO;
11196 break;
11197 case PRAGMA_OACC_CLAUSE_COPYOUT:
11198 kind = GOMP_MAP_FORCE_FROM;
11199 break;
11200 case PRAGMA_OACC_CLAUSE_CREATE:
11201 kind = GOMP_MAP_FORCE_ALLOC;
11202 break;
11203 case PRAGMA_OACC_CLAUSE_DELETE:
11204 kind = GOMP_MAP_DELETE;
11205 break;
11206 case PRAGMA_OACC_CLAUSE_DEVICE:
11207 kind = GOMP_MAP_FORCE_TO;
11208 break;
11209 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11210 kind = GOMP_MAP_DEVICE_RESIDENT;
11211 break;
11212 case PRAGMA_OACC_CLAUSE_HOST:
11213 case PRAGMA_OACC_CLAUSE_SELF:
11214 kind = GOMP_MAP_FORCE_FROM;
11215 break;
11216 case PRAGMA_OACC_CLAUSE_LINK:
11217 kind = GOMP_MAP_LINK;
11218 break;
11219 case PRAGMA_OACC_CLAUSE_PRESENT:
11220 kind = GOMP_MAP_FORCE_PRESENT;
11221 break;
11222 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11223 kind = GOMP_MAP_TOFROM;
11224 break;
11225 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11226 kind = GOMP_MAP_TO;
11227 break;
11228 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11229 kind = GOMP_MAP_FROM;
11230 break;
11231 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11232 kind = GOMP_MAP_ALLOC;
11233 break;
11234 default:
11235 gcc_unreachable ();
11237 tree nl, c;
11238 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11240 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11241 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11243 return nl;
11246 /* OpenACC 2.0:
11247 deviceptr ( variable-list ) */
11249 static tree
11250 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11252 location_t loc = c_parser_peek_token (parser)->location;
11253 tree vars, t;
11255 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11256 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11257 variable-list must only allow for pointer variables. */
11258 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11259 for (t = vars; t && t; t = TREE_CHAIN (t))
11261 tree v = TREE_PURPOSE (t);
11263 /* FIXME diagnostics: Ideally we should keep individual
11264 locations for all the variables in the var list to make the
11265 following errors more precise. Perhaps
11266 c_parser_omp_var_list_parens() should construct a list of
11267 locations to go along with the var list. */
11269 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11270 error_at (loc, "%qD is not a variable", v);
11271 else if (TREE_TYPE (v) == error_mark_node)
11273 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11274 error_at (loc, "%qD is not a pointer variable", v);
11276 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11277 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11278 OMP_CLAUSE_DECL (u) = v;
11279 OMP_CLAUSE_CHAIN (u) = list;
11280 list = u;
11283 return list;
11286 /* OpenACC 2.0, OpenMP 3.0:
11287 collapse ( constant-expression ) */
11289 static tree
11290 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11292 tree c, num = error_mark_node;
11293 HOST_WIDE_INT n;
11294 location_t loc;
11296 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11297 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11299 loc = c_parser_peek_token (parser)->location;
11300 matching_parens parens;
11301 if (parens.require_open (parser))
11303 num = c_parser_expr_no_commas (parser, NULL).value;
11304 parens.skip_until_found_close (parser);
11306 if (num == error_mark_node)
11307 return list;
11308 mark_exp_read (num);
11309 num = c_fully_fold (num, false, NULL);
11310 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11311 || !tree_fits_shwi_p (num)
11312 || (n = tree_to_shwi (num)) <= 0
11313 || (int) n != n)
11315 error_at (loc,
11316 "collapse argument needs positive constant integer expression");
11317 return list;
11319 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11320 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11321 OMP_CLAUSE_CHAIN (c) = list;
11322 return c;
11325 /* OpenMP 2.5:
11326 copyin ( variable-list ) */
11328 static tree
11329 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11331 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11334 /* OpenMP 2.5:
11335 copyprivate ( variable-list ) */
11337 static tree
11338 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11340 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11343 /* OpenMP 2.5:
11344 default ( none | shared )
11346 OpenACC:
11347 default ( none | present ) */
11349 static tree
11350 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11352 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11353 location_t loc = c_parser_peek_token (parser)->location;
11354 tree c;
11356 matching_parens parens;
11357 if (!parens.require_open (parser))
11358 return list;
11359 if (c_parser_next_token_is (parser, CPP_NAME))
11361 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11363 switch (p[0])
11365 case 'n':
11366 if (strcmp ("none", p) != 0)
11367 goto invalid_kind;
11368 kind = OMP_CLAUSE_DEFAULT_NONE;
11369 break;
11371 case 'p':
11372 if (strcmp ("present", p) != 0 || !is_oacc)
11373 goto invalid_kind;
11374 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11375 break;
11377 case 's':
11378 if (strcmp ("shared", p) != 0 || is_oacc)
11379 goto invalid_kind;
11380 kind = OMP_CLAUSE_DEFAULT_SHARED;
11381 break;
11383 default:
11384 goto invalid_kind;
11387 c_parser_consume_token (parser);
11389 else
11391 invalid_kind:
11392 if (is_oacc)
11393 c_parser_error (parser, "expected %<none%> or %<present%>");
11394 else
11395 c_parser_error (parser, "expected %<none%> or %<shared%>");
11397 parens.skip_until_found_close (parser);
11399 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11400 return list;
11402 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11403 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11404 OMP_CLAUSE_CHAIN (c) = list;
11405 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11407 return c;
11410 /* OpenMP 2.5:
11411 firstprivate ( variable-list ) */
11413 static tree
11414 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11416 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11419 /* OpenMP 3.1:
11420 final ( expression ) */
11422 static tree
11423 c_parser_omp_clause_final (c_parser *parser, tree list)
11425 location_t loc = c_parser_peek_token (parser)->location;
11426 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11428 tree t = c_parser_paren_condition (parser);
11429 tree c;
11431 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11433 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11434 OMP_CLAUSE_FINAL_EXPR (c) = t;
11435 OMP_CLAUSE_CHAIN (c) = list;
11436 list = c;
11438 else
11439 c_parser_error (parser, "expected %<(%>");
11441 return list;
11444 /* OpenACC, OpenMP 2.5:
11445 if ( expression )
11447 OpenMP 4.5:
11448 if ( directive-name-modifier : expression )
11450 directive-name-modifier:
11451 parallel | task | taskloop | target data | target | target update
11452 | target enter data | target exit data */
11454 static tree
11455 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11457 location_t location = c_parser_peek_token (parser)->location;
11458 enum tree_code if_modifier = ERROR_MARK;
11460 matching_parens parens;
11461 if (!parens.require_open (parser))
11462 return list;
11464 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11466 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11467 int n = 2;
11468 if (strcmp (p, "parallel") == 0)
11469 if_modifier = OMP_PARALLEL;
11470 else if (strcmp (p, "task") == 0)
11471 if_modifier = OMP_TASK;
11472 else if (strcmp (p, "taskloop") == 0)
11473 if_modifier = OMP_TASKLOOP;
11474 else if (strcmp (p, "target") == 0)
11476 if_modifier = OMP_TARGET;
11477 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11479 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11480 if (strcmp ("data", p) == 0)
11481 if_modifier = OMP_TARGET_DATA;
11482 else if (strcmp ("update", p) == 0)
11483 if_modifier = OMP_TARGET_UPDATE;
11484 else if (strcmp ("enter", p) == 0)
11485 if_modifier = OMP_TARGET_ENTER_DATA;
11486 else if (strcmp ("exit", p) == 0)
11487 if_modifier = OMP_TARGET_EXIT_DATA;
11488 if (if_modifier != OMP_TARGET)
11490 n = 3;
11491 c_parser_consume_token (parser);
11493 else
11495 location_t loc = c_parser_peek_2nd_token (parser)->location;
11496 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11497 "or %<exit%>");
11498 if_modifier = ERROR_MARK;
11500 if (if_modifier == OMP_TARGET_ENTER_DATA
11501 || if_modifier == OMP_TARGET_EXIT_DATA)
11503 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11505 p = IDENTIFIER_POINTER
11506 (c_parser_peek_2nd_token (parser)->value);
11507 if (strcmp ("data", p) == 0)
11508 n = 4;
11510 if (n == 4)
11511 c_parser_consume_token (parser);
11512 else
11514 location_t loc
11515 = c_parser_peek_2nd_token (parser)->location;
11516 error_at (loc, "expected %<data%>");
11517 if_modifier = ERROR_MARK;
11522 if (if_modifier != ERROR_MARK)
11524 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11526 c_parser_consume_token (parser);
11527 c_parser_consume_token (parser);
11529 else
11531 if (n > 2)
11533 location_t loc = c_parser_peek_2nd_token (parser)->location;
11534 error_at (loc, "expected %<:%>");
11536 if_modifier = ERROR_MARK;
11541 tree t = c_parser_condition (parser), c;
11542 parens.skip_until_found_close (parser);
11544 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11545 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11547 if (if_modifier != ERROR_MARK
11548 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11550 const char *p = NULL;
11551 switch (if_modifier)
11553 case OMP_PARALLEL: p = "parallel"; break;
11554 case OMP_TASK: p = "task"; break;
11555 case OMP_TASKLOOP: p = "taskloop"; break;
11556 case OMP_TARGET_DATA: p = "target data"; break;
11557 case OMP_TARGET: p = "target"; break;
11558 case OMP_TARGET_UPDATE: p = "target update"; break;
11559 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11560 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11561 default: gcc_unreachable ();
11563 error_at (location, "too many %<if%> clauses with %qs modifier",
11565 return list;
11567 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11569 if (!is_omp)
11570 error_at (location, "too many %<if%> clauses");
11571 else
11572 error_at (location, "too many %<if%> clauses without modifier");
11573 return list;
11575 else if (if_modifier == ERROR_MARK
11576 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11578 error_at (location, "if any %<if%> clause has modifier, then all "
11579 "%<if%> clauses have to use modifier");
11580 return list;
11584 c = build_omp_clause (location, OMP_CLAUSE_IF);
11585 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11586 OMP_CLAUSE_IF_EXPR (c) = t;
11587 OMP_CLAUSE_CHAIN (c) = list;
11588 return c;
11591 /* OpenMP 2.5:
11592 lastprivate ( variable-list ) */
11594 static tree
11595 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11597 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11600 /* OpenMP 3.1:
11601 mergeable */
11603 static tree
11604 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11606 tree c;
11608 /* FIXME: Should we allow duplicates? */
11609 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11611 c = build_omp_clause (c_parser_peek_token (parser)->location,
11612 OMP_CLAUSE_MERGEABLE);
11613 OMP_CLAUSE_CHAIN (c) = list;
11615 return c;
11618 /* OpenMP 2.5:
11619 nowait */
11621 static tree
11622 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11624 tree c;
11625 location_t loc = c_parser_peek_token (parser)->location;
11627 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11629 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11630 OMP_CLAUSE_CHAIN (c) = list;
11631 return c;
11634 /* OpenMP 2.5:
11635 num_threads ( expression ) */
11637 static tree
11638 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11640 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11641 matching_parens parens;
11642 if (parens.require_open (parser))
11644 location_t expr_loc = c_parser_peek_token (parser)->location;
11645 c_expr expr = c_parser_expression (parser);
11646 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11647 tree c, t = expr.value;
11648 t = c_fully_fold (t, false, NULL);
11650 parens.skip_until_found_close (parser);
11652 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11654 c_parser_error (parser, "expected integer expression");
11655 return list;
11658 /* Attempt to statically determine when the number isn't positive. */
11659 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11660 build_int_cst (TREE_TYPE (t), 0));
11661 protected_set_expr_location (c, expr_loc);
11662 if (c == boolean_true_node)
11664 warning_at (expr_loc, 0,
11665 "%<num_threads%> value must be positive");
11666 t = integer_one_node;
11669 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11671 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11672 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11673 OMP_CLAUSE_CHAIN (c) = list;
11674 list = c;
11677 return list;
11680 /* OpenMP 4.5:
11681 num_tasks ( expression ) */
11683 static tree
11684 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11686 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11687 matching_parens parens;
11688 if (parens.require_open (parser))
11690 location_t expr_loc = c_parser_peek_token (parser)->location;
11691 c_expr expr = c_parser_expression (parser);
11692 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11693 tree c, t = expr.value;
11694 t = c_fully_fold (t, false, NULL);
11696 parens.skip_until_found_close (parser);
11698 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11700 c_parser_error (parser, "expected integer expression");
11701 return list;
11704 /* Attempt to statically determine when the number isn't positive. */
11705 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11706 build_int_cst (TREE_TYPE (t), 0));
11707 if (CAN_HAVE_LOCATION_P (c))
11708 SET_EXPR_LOCATION (c, expr_loc);
11709 if (c == boolean_true_node)
11711 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11712 t = integer_one_node;
11715 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11717 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11718 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11719 OMP_CLAUSE_CHAIN (c) = list;
11720 list = c;
11723 return list;
11726 /* OpenMP 4.5:
11727 grainsize ( expression ) */
11729 static tree
11730 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11732 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11733 matching_parens parens;
11734 if (parens.require_open (parser))
11736 location_t expr_loc = c_parser_peek_token (parser)->location;
11737 c_expr expr = c_parser_expression (parser);
11738 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11739 tree c, t = expr.value;
11740 t = c_fully_fold (t, false, NULL);
11742 parens.skip_until_found_close (parser);
11744 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11746 c_parser_error (parser, "expected integer expression");
11747 return list;
11750 /* Attempt to statically determine when the number isn't positive. */
11751 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11752 build_int_cst (TREE_TYPE (t), 0));
11753 if (CAN_HAVE_LOCATION_P (c))
11754 SET_EXPR_LOCATION (c, expr_loc);
11755 if (c == boolean_true_node)
11757 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11758 t = integer_one_node;
11761 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11763 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11764 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11765 OMP_CLAUSE_CHAIN (c) = list;
11766 list = c;
11769 return list;
11772 /* OpenMP 4.5:
11773 priority ( expression ) */
11775 static tree
11776 c_parser_omp_clause_priority (c_parser *parser, tree list)
11778 location_t priority_loc = c_parser_peek_token (parser)->location;
11779 matching_parens parens;
11780 if (parens.require_open (parser))
11782 location_t expr_loc = c_parser_peek_token (parser)->location;
11783 c_expr expr = c_parser_expression (parser);
11784 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11785 tree c, t = expr.value;
11786 t = c_fully_fold (t, false, NULL);
11788 parens.skip_until_found_close (parser);
11790 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11792 c_parser_error (parser, "expected integer expression");
11793 return list;
11796 /* Attempt to statically determine when the number isn't
11797 non-negative. */
11798 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11799 build_int_cst (TREE_TYPE (t), 0));
11800 if (CAN_HAVE_LOCATION_P (c))
11801 SET_EXPR_LOCATION (c, expr_loc);
11802 if (c == boolean_true_node)
11804 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11805 t = integer_one_node;
11808 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11810 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11811 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11812 OMP_CLAUSE_CHAIN (c) = list;
11813 list = c;
11816 return list;
11819 /* OpenMP 4.5:
11820 hint ( expression ) */
11822 static tree
11823 c_parser_omp_clause_hint (c_parser *parser, tree list)
11825 location_t hint_loc = c_parser_peek_token (parser)->location;
11826 matching_parens parens;
11827 if (parens.require_open (parser))
11829 location_t expr_loc = c_parser_peek_token (parser)->location;
11830 c_expr expr = c_parser_expression (parser);
11831 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11832 tree c, t = expr.value;
11833 t = c_fully_fold (t, false, NULL);
11835 parens.skip_until_found_close (parser);
11837 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11839 c_parser_error (parser, "expected integer expression");
11840 return list;
11843 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11845 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11846 OMP_CLAUSE_HINT_EXPR (c) = t;
11847 OMP_CLAUSE_CHAIN (c) = list;
11848 list = c;
11851 return list;
11854 /* OpenMP 4.5:
11855 defaultmap ( tofrom : scalar ) */
11857 static tree
11858 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11860 location_t loc = c_parser_peek_token (parser)->location;
11861 tree c;
11862 const char *p;
11864 matching_parens parens;
11865 if (!parens.require_open (parser))
11866 return list;
11867 if (!c_parser_next_token_is (parser, CPP_NAME))
11869 c_parser_error (parser, "expected %<tofrom%>");
11870 goto out_err;
11872 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11873 if (strcmp (p, "tofrom") != 0)
11875 c_parser_error (parser, "expected %<tofrom%>");
11876 goto out_err;
11878 c_parser_consume_token (parser);
11879 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11880 goto out_err;
11881 if (!c_parser_next_token_is (parser, CPP_NAME))
11883 c_parser_error (parser, "expected %<scalar%>");
11884 goto out_err;
11886 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11887 if (strcmp (p, "scalar") != 0)
11889 c_parser_error (parser, "expected %<scalar%>");
11890 goto out_err;
11892 c_parser_consume_token (parser);
11893 parens.skip_until_found_close (parser);
11894 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11895 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11896 OMP_CLAUSE_CHAIN (c) = list;
11897 return c;
11899 out_err:
11900 parens.skip_until_found_close (parser);
11901 return list;
11904 /* OpenACC 2.0:
11905 use_device ( variable-list )
11907 OpenMP 4.5:
11908 use_device_ptr ( variable-list ) */
11910 static tree
11911 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11913 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11914 list);
11917 /* OpenMP 4.5:
11918 is_device_ptr ( variable-list ) */
11920 static tree
11921 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11923 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11926 /* OpenACC:
11927 num_gangs ( expression )
11928 num_workers ( expression )
11929 vector_length ( expression ) */
11931 static tree
11932 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
11933 tree list)
11935 location_t loc = c_parser_peek_token (parser)->location;
11937 matching_parens parens;
11938 if (!parens.require_open (parser))
11939 return list;
11941 location_t expr_loc = c_parser_peek_token (parser)->location;
11942 c_expr expr = c_parser_expression (parser);
11943 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11944 tree c, t = expr.value;
11945 t = c_fully_fold (t, false, NULL);
11947 parens.skip_until_found_close (parser);
11949 if (t == error_mark_node)
11950 return list;
11951 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11953 error_at (expr_loc, "%qs expression must be integral",
11954 omp_clause_code_name[code]);
11955 return list;
11958 /* Attempt to statically determine when the number isn't positive. */
11959 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11960 build_int_cst (TREE_TYPE (t), 0));
11961 protected_set_expr_location (c, expr_loc);
11962 if (c == boolean_true_node)
11964 warning_at (expr_loc, 0,
11965 "%qs value must be positive",
11966 omp_clause_code_name[code]);
11967 t = integer_one_node;
11970 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11972 c = build_omp_clause (loc, code);
11973 OMP_CLAUSE_OPERAND (c, 0) = t;
11974 OMP_CLAUSE_CHAIN (c) = list;
11975 return c;
11978 /* OpenACC:
11980 gang [( gang-arg-list )]
11981 worker [( [num:] int-expr )]
11982 vector [( [length:] int-expr )]
11984 where gang-arg is one of:
11986 [num:] int-expr
11987 static: size-expr
11989 and size-expr may be:
11992 int-expr
11995 static tree
11996 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11997 const char *str, tree list)
11999 const char *id = "num";
12000 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12001 location_t loc = c_parser_peek_token (parser)->location;
12003 if (kind == OMP_CLAUSE_VECTOR)
12004 id = "length";
12006 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12008 c_parser_consume_token (parser);
12012 c_token *next = c_parser_peek_token (parser);
12013 int idx = 0;
12015 /* Gang static argument. */
12016 if (kind == OMP_CLAUSE_GANG
12017 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12019 c_parser_consume_token (parser);
12021 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12022 goto cleanup_error;
12024 idx = 1;
12025 if (ops[idx] != NULL_TREE)
12027 c_parser_error (parser, "too many %<static%> arguments");
12028 goto cleanup_error;
12031 /* Check for the '*' argument. */
12032 if (c_parser_next_token_is (parser, CPP_MULT)
12033 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12034 || c_parser_peek_2nd_token (parser)->type
12035 == CPP_CLOSE_PAREN))
12037 c_parser_consume_token (parser);
12038 ops[idx] = integer_minus_one_node;
12040 if (c_parser_next_token_is (parser, CPP_COMMA))
12042 c_parser_consume_token (parser);
12043 continue;
12045 else
12046 break;
12049 /* Worker num: argument and vector length: arguments. */
12050 else if (c_parser_next_token_is (parser, CPP_NAME)
12051 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12052 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12054 c_parser_consume_token (parser); /* id */
12055 c_parser_consume_token (parser); /* ':' */
12058 /* Now collect the actual argument. */
12059 if (ops[idx] != NULL_TREE)
12061 c_parser_error (parser, "unexpected argument");
12062 goto cleanup_error;
12065 location_t expr_loc = c_parser_peek_token (parser)->location;
12066 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12067 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12068 tree expr = cexpr.value;
12069 if (expr == error_mark_node)
12070 goto cleanup_error;
12072 expr = c_fully_fold (expr, false, NULL);
12074 /* Attempt to statically determine when the number isn't a
12075 positive integer. */
12077 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12079 c_parser_error (parser, "expected integer expression");
12080 return list;
12083 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12084 build_int_cst (TREE_TYPE (expr), 0));
12085 if (c == boolean_true_node)
12087 warning_at (loc, 0,
12088 "%qs value must be positive", str);
12089 expr = integer_one_node;
12092 ops[idx] = expr;
12094 if (kind == OMP_CLAUSE_GANG
12095 && c_parser_next_token_is (parser, CPP_COMMA))
12097 c_parser_consume_token (parser);
12098 continue;
12100 break;
12102 while (1);
12104 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12105 goto cleanup_error;
12108 check_no_duplicate_clause (list, kind, str);
12110 c = build_omp_clause (loc, kind);
12112 if (ops[1])
12113 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12115 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12116 OMP_CLAUSE_CHAIN (c) = list;
12118 return c;
12120 cleanup_error:
12121 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12122 return list;
12125 /* OpenACC:
12126 auto
12127 independent
12128 nohost
12129 seq */
12131 static tree
12132 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12133 tree list)
12135 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12137 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12138 OMP_CLAUSE_CHAIN (c) = list;
12140 return c;
12143 /* OpenACC:
12144 async [( int-expr )] */
12146 static tree
12147 c_parser_oacc_clause_async (c_parser *parser, tree list)
12149 tree c, t;
12150 location_t loc = c_parser_peek_token (parser)->location;
12152 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12154 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12156 c_parser_consume_token (parser);
12158 t = c_parser_expression (parser).value;
12159 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12160 c_parser_error (parser, "expected integer expression");
12161 else if (t == error_mark_node
12162 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12163 return list;
12165 else
12166 t = c_fully_fold (t, false, NULL);
12168 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12170 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12171 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12172 OMP_CLAUSE_CHAIN (c) = list;
12173 list = c;
12175 return list;
12178 /* OpenACC 2.0:
12179 tile ( size-expr-list ) */
12181 static tree
12182 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12184 tree c, expr = error_mark_node;
12185 location_t loc;
12186 tree tile = NULL_TREE;
12188 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12189 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12191 loc = c_parser_peek_token (parser)->location;
12192 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12193 return list;
12197 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12198 return list;
12200 if (c_parser_next_token_is (parser, CPP_MULT)
12201 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12202 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12204 c_parser_consume_token (parser);
12205 expr = integer_zero_node;
12207 else
12209 location_t expr_loc = c_parser_peek_token (parser)->location;
12210 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12211 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12212 expr = cexpr.value;
12214 if (expr == error_mark_node)
12216 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12217 "expected %<)%>");
12218 return list;
12221 expr = c_fully_fold (expr, false, NULL);
12223 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12224 || !tree_fits_shwi_p (expr)
12225 || tree_to_shwi (expr) <= 0)
12227 error_at (expr_loc, "%<tile%> argument needs positive"
12228 " integral constant");
12229 expr = integer_zero_node;
12233 tile = tree_cons (NULL_TREE, expr, tile);
12235 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12237 /* Consume the trailing ')'. */
12238 c_parser_consume_token (parser);
12240 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12241 tile = nreverse (tile);
12242 OMP_CLAUSE_TILE_LIST (c) = tile;
12243 OMP_CLAUSE_CHAIN (c) = list;
12244 return c;
12247 /* OpenACC:
12248 wait ( int-expr-list ) */
12250 static tree
12251 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12253 location_t clause_loc = c_parser_peek_token (parser)->location;
12255 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12256 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12258 return list;
12261 /* OpenMP 2.5:
12262 ordered
12264 OpenMP 4.5:
12265 ordered ( constant-expression ) */
12267 static tree
12268 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12270 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12272 tree c, num = NULL_TREE;
12273 HOST_WIDE_INT n;
12274 location_t loc = c_parser_peek_token (parser)->location;
12275 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12277 matching_parens parens;
12278 parens.consume_open (parser);
12279 num = c_parser_expr_no_commas (parser, NULL).value;
12280 parens.skip_until_found_close (parser);
12282 if (num == error_mark_node)
12283 return list;
12284 if (num)
12286 mark_exp_read (num);
12287 num = c_fully_fold (num, false, NULL);
12288 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12289 || !tree_fits_shwi_p (num)
12290 || (n = tree_to_shwi (num)) <= 0
12291 || (int) n != n)
12293 error_at (loc, "ordered argument needs positive "
12294 "constant integer expression");
12295 return list;
12298 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12299 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12300 OMP_CLAUSE_CHAIN (c) = list;
12301 return c;
12304 /* OpenMP 2.5:
12305 private ( variable-list ) */
12307 static tree
12308 c_parser_omp_clause_private (c_parser *parser, tree list)
12310 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12313 /* OpenMP 2.5:
12314 reduction ( reduction-operator : variable-list )
12316 reduction-operator:
12317 One of: + * - & ^ | && ||
12319 OpenMP 3.1:
12321 reduction-operator:
12322 One of: + * - & ^ | && || max min
12324 OpenMP 4.0:
12326 reduction-operator:
12327 One of: + * - & ^ | && ||
12328 identifier */
12330 static tree
12331 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12333 location_t clause_loc = c_parser_peek_token (parser)->location;
12334 matching_parens parens;
12335 if (parens.require_open (parser))
12337 enum tree_code code = ERROR_MARK;
12338 tree reduc_id = NULL_TREE;
12340 switch (c_parser_peek_token (parser)->type)
12342 case CPP_PLUS:
12343 code = PLUS_EXPR;
12344 break;
12345 case CPP_MULT:
12346 code = MULT_EXPR;
12347 break;
12348 case CPP_MINUS:
12349 code = MINUS_EXPR;
12350 break;
12351 case CPP_AND:
12352 code = BIT_AND_EXPR;
12353 break;
12354 case CPP_XOR:
12355 code = BIT_XOR_EXPR;
12356 break;
12357 case CPP_OR:
12358 code = BIT_IOR_EXPR;
12359 break;
12360 case CPP_AND_AND:
12361 code = TRUTH_ANDIF_EXPR;
12362 break;
12363 case CPP_OR_OR:
12364 code = TRUTH_ORIF_EXPR;
12365 break;
12366 case CPP_NAME:
12368 const char *p
12369 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12370 if (strcmp (p, "min") == 0)
12372 code = MIN_EXPR;
12373 break;
12375 if (strcmp (p, "max") == 0)
12377 code = MAX_EXPR;
12378 break;
12380 reduc_id = c_parser_peek_token (parser)->value;
12381 break;
12383 default:
12384 c_parser_error (parser,
12385 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12386 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12387 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12388 return list;
12390 c_parser_consume_token (parser);
12391 reduc_id = c_omp_reduction_id (code, reduc_id);
12392 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12394 tree nl, c;
12396 nl = c_parser_omp_variable_list (parser, clause_loc,
12397 OMP_CLAUSE_REDUCTION, list);
12398 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12400 tree d = OMP_CLAUSE_DECL (c), type;
12401 if (TREE_CODE (d) != TREE_LIST)
12402 type = TREE_TYPE (d);
12403 else
12405 int cnt = 0;
12406 tree t;
12407 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12408 cnt++;
12409 type = TREE_TYPE (t);
12410 while (cnt > 0)
12412 if (TREE_CODE (type) != POINTER_TYPE
12413 && TREE_CODE (type) != ARRAY_TYPE)
12414 break;
12415 type = TREE_TYPE (type);
12416 cnt--;
12419 while (TREE_CODE (type) == ARRAY_TYPE)
12420 type = TREE_TYPE (type);
12421 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12422 if (code == ERROR_MARK
12423 || !(INTEGRAL_TYPE_P (type)
12424 || TREE_CODE (type) == REAL_TYPE
12425 || TREE_CODE (type) == COMPLEX_TYPE))
12426 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12427 = c_omp_reduction_lookup (reduc_id,
12428 TYPE_MAIN_VARIANT (type));
12431 list = nl;
12433 parens.skip_until_found_close (parser);
12435 return list;
12438 /* OpenMP 2.5:
12439 schedule ( schedule-kind )
12440 schedule ( schedule-kind , expression )
12442 schedule-kind:
12443 static | dynamic | guided | runtime | auto
12445 OpenMP 4.5:
12446 schedule ( schedule-modifier : schedule-kind )
12447 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12449 schedule-modifier:
12450 simd
12451 monotonic
12452 nonmonotonic */
12454 static tree
12455 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12457 tree c, t;
12458 location_t loc = c_parser_peek_token (parser)->location;
12459 int modifiers = 0, nmodifiers = 0;
12461 matching_parens parens;
12462 if (!parens.require_open (parser))
12463 return list;
12465 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12467 while (c_parser_next_token_is (parser, CPP_NAME))
12469 tree kind = c_parser_peek_token (parser)->value;
12470 const char *p = IDENTIFIER_POINTER (kind);
12471 if (strcmp ("simd", p) == 0)
12472 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12473 else if (strcmp ("monotonic", p) == 0)
12474 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12475 else if (strcmp ("nonmonotonic", p) == 0)
12476 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12477 else
12478 break;
12479 c_parser_consume_token (parser);
12480 if (nmodifiers++ == 0
12481 && c_parser_next_token_is (parser, CPP_COMMA))
12482 c_parser_consume_token (parser);
12483 else
12485 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12486 break;
12490 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12491 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12492 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12493 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12495 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12496 "specified");
12497 modifiers = 0;
12500 if (c_parser_next_token_is (parser, CPP_NAME))
12502 tree kind = c_parser_peek_token (parser)->value;
12503 const char *p = IDENTIFIER_POINTER (kind);
12505 switch (p[0])
12507 case 'd':
12508 if (strcmp ("dynamic", p) != 0)
12509 goto invalid_kind;
12510 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12511 break;
12513 case 'g':
12514 if (strcmp ("guided", p) != 0)
12515 goto invalid_kind;
12516 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12517 break;
12519 case 'r':
12520 if (strcmp ("runtime", p) != 0)
12521 goto invalid_kind;
12522 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12523 break;
12525 default:
12526 goto invalid_kind;
12529 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12530 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12531 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12532 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12533 else
12534 goto invalid_kind;
12536 c_parser_consume_token (parser);
12537 if (c_parser_next_token_is (parser, CPP_COMMA))
12539 location_t here;
12540 c_parser_consume_token (parser);
12542 here = c_parser_peek_token (parser)->location;
12543 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12544 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12545 t = expr.value;
12546 t = c_fully_fold (t, false, NULL);
12548 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12549 error_at (here, "schedule %<runtime%> does not take "
12550 "a %<chunk_size%> parameter");
12551 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12552 error_at (here,
12553 "schedule %<auto%> does not take "
12554 "a %<chunk_size%> parameter");
12555 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12557 /* Attempt to statically determine when the number isn't
12558 positive. */
12559 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12560 build_int_cst (TREE_TYPE (t), 0));
12561 protected_set_expr_location (s, loc);
12562 if (s == boolean_true_node)
12564 warning_at (loc, 0,
12565 "chunk size value must be positive");
12566 t = integer_one_node;
12568 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12570 else
12571 c_parser_error (parser, "expected integer expression");
12573 parens.skip_until_found_close (parser);
12575 else
12576 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12577 "expected %<,%> or %<)%>");
12579 OMP_CLAUSE_SCHEDULE_KIND (c)
12580 = (enum omp_clause_schedule_kind)
12581 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12583 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12584 OMP_CLAUSE_CHAIN (c) = list;
12585 return c;
12587 invalid_kind:
12588 c_parser_error (parser, "invalid schedule kind");
12589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12590 return list;
12593 /* OpenMP 2.5:
12594 shared ( variable-list ) */
12596 static tree
12597 c_parser_omp_clause_shared (c_parser *parser, tree list)
12599 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12602 /* OpenMP 3.0:
12603 untied */
12605 static tree
12606 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12608 tree c;
12610 /* FIXME: Should we allow duplicates? */
12611 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12613 c = build_omp_clause (c_parser_peek_token (parser)->location,
12614 OMP_CLAUSE_UNTIED);
12615 OMP_CLAUSE_CHAIN (c) = list;
12617 return c;
12620 /* OpenMP 4.0:
12621 inbranch
12622 notinbranch */
12624 static tree
12625 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12626 enum omp_clause_code code, tree list)
12628 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12630 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12631 OMP_CLAUSE_CHAIN (c) = list;
12633 return c;
12636 /* OpenMP 4.0:
12637 parallel
12639 sections
12640 taskgroup */
12642 static tree
12643 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12644 enum omp_clause_code code, tree list)
12646 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12647 OMP_CLAUSE_CHAIN (c) = list;
12649 return c;
12652 /* OpenMP 4.5:
12653 nogroup */
12655 static tree
12656 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12658 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12659 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12660 OMP_CLAUSE_NOGROUP);
12661 OMP_CLAUSE_CHAIN (c) = list;
12662 return c;
12665 /* OpenMP 4.5:
12666 simd
12667 threads */
12669 static tree
12670 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12671 enum omp_clause_code code, tree list)
12673 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12674 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12675 OMP_CLAUSE_CHAIN (c) = list;
12676 return c;
12679 /* OpenMP 4.0:
12680 num_teams ( expression ) */
12682 static tree
12683 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12685 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12686 matching_parens parens;
12687 if (parens.require_open (parser))
12689 location_t expr_loc = c_parser_peek_token (parser)->location;
12690 c_expr expr = c_parser_expression (parser);
12691 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12692 tree c, t = expr.value;
12693 t = c_fully_fold (t, false, NULL);
12695 parens.skip_until_found_close (parser);
12697 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12699 c_parser_error (parser, "expected integer expression");
12700 return list;
12703 /* Attempt to statically determine when the number isn't positive. */
12704 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12705 build_int_cst (TREE_TYPE (t), 0));
12706 protected_set_expr_location (c, expr_loc);
12707 if (c == boolean_true_node)
12709 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12710 t = integer_one_node;
12713 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12715 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12716 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12717 OMP_CLAUSE_CHAIN (c) = list;
12718 list = c;
12721 return list;
12724 /* OpenMP 4.0:
12725 thread_limit ( expression ) */
12727 static tree
12728 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12730 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12731 matching_parens parens;
12732 if (parens.require_open (parser))
12734 location_t expr_loc = c_parser_peek_token (parser)->location;
12735 c_expr expr = c_parser_expression (parser);
12736 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12737 tree c, t = expr.value;
12738 t = c_fully_fold (t, false, NULL);
12740 parens.skip_until_found_close (parser);
12742 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12744 c_parser_error (parser, "expected integer expression");
12745 return list;
12748 /* Attempt to statically determine when the number isn't positive. */
12749 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12750 build_int_cst (TREE_TYPE (t), 0));
12751 protected_set_expr_location (c, expr_loc);
12752 if (c == boolean_true_node)
12754 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12755 t = integer_one_node;
12758 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12759 "thread_limit");
12761 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12762 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12763 OMP_CLAUSE_CHAIN (c) = list;
12764 list = c;
12767 return list;
12770 /* OpenMP 4.0:
12771 aligned ( variable-list )
12772 aligned ( variable-list : constant-expression ) */
12774 static tree
12775 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12777 location_t clause_loc = c_parser_peek_token (parser)->location;
12778 tree nl, c;
12780 matching_parens parens;
12781 if (!parens.require_open (parser))
12782 return list;
12784 nl = c_parser_omp_variable_list (parser, clause_loc,
12785 OMP_CLAUSE_ALIGNED, list);
12787 if (c_parser_next_token_is (parser, CPP_COLON))
12789 c_parser_consume_token (parser);
12790 location_t expr_loc = c_parser_peek_token (parser)->location;
12791 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12792 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12793 tree alignment = expr.value;
12794 alignment = c_fully_fold (alignment, false, NULL);
12795 if (TREE_CODE (alignment) != INTEGER_CST
12796 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12797 || tree_int_cst_sgn (alignment) != 1)
12799 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12800 "be positive constant integer expression");
12801 alignment = NULL_TREE;
12804 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12805 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12808 parens.skip_until_found_close (parser);
12809 return nl;
12812 /* OpenMP 4.0:
12813 linear ( variable-list )
12814 linear ( variable-list : expression )
12816 OpenMP 4.5:
12817 linear ( modifier ( variable-list ) )
12818 linear ( modifier ( variable-list ) : expression ) */
12820 static tree
12821 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12823 location_t clause_loc = c_parser_peek_token (parser)->location;
12824 tree nl, c, step;
12825 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12827 matching_parens parens;
12828 if (!parens.require_open (parser))
12829 return list;
12831 if (!is_cilk_simd_fn
12832 && c_parser_next_token_is (parser, CPP_NAME))
12834 c_token *tok = c_parser_peek_token (parser);
12835 const char *p = IDENTIFIER_POINTER (tok->value);
12836 if (strcmp ("val", p) == 0)
12837 kind = OMP_CLAUSE_LINEAR_VAL;
12838 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12839 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12840 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12842 c_parser_consume_token (parser);
12843 c_parser_consume_token (parser);
12847 nl = c_parser_omp_variable_list (parser, clause_loc,
12848 OMP_CLAUSE_LINEAR, list);
12850 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12851 parens.skip_until_found_close (parser);
12853 if (c_parser_next_token_is (parser, CPP_COLON))
12855 c_parser_consume_token (parser);
12856 location_t expr_loc = c_parser_peek_token (parser)->location;
12857 c_expr expr = c_parser_expression (parser);
12858 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12859 step = expr.value;
12860 step = c_fully_fold (step, false, NULL);
12861 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12863 sorry ("using parameters for %<linear%> step is not supported yet");
12864 step = integer_one_node;
12866 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12868 error_at (clause_loc, "%<linear%> clause step expression must "
12869 "be integral");
12870 step = integer_one_node;
12874 else
12875 step = integer_one_node;
12877 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12879 OMP_CLAUSE_LINEAR_STEP (c) = step;
12880 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12883 parens.skip_until_found_close (parser);
12884 return nl;
12887 /* OpenMP 4.0:
12888 safelen ( constant-expression ) */
12890 static tree
12891 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12893 location_t clause_loc = c_parser_peek_token (parser)->location;
12894 tree c, t;
12896 matching_parens parens;
12897 if (!parens.require_open (parser))
12898 return list;
12900 location_t expr_loc = c_parser_peek_token (parser)->location;
12901 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12902 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12903 t = expr.value;
12904 t = c_fully_fold (t, false, NULL);
12905 if (TREE_CODE (t) != INTEGER_CST
12906 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12907 || tree_int_cst_sgn (t) != 1)
12909 error_at (clause_loc, "%<safelen%> clause expression must "
12910 "be positive constant integer expression");
12911 t = NULL_TREE;
12914 parens.skip_until_found_close (parser);
12915 if (t == NULL_TREE || t == error_mark_node)
12916 return list;
12918 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12920 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12921 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12922 OMP_CLAUSE_CHAIN (c) = list;
12923 return c;
12926 /* OpenMP 4.0:
12927 simdlen ( constant-expression ) */
12929 static tree
12930 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12932 location_t clause_loc = c_parser_peek_token (parser)->location;
12933 tree c, t;
12935 matching_parens parens;
12936 if (!parens.require_open (parser))
12937 return list;
12939 location_t expr_loc = c_parser_peek_token (parser)->location;
12940 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12941 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12942 t = expr.value;
12943 t = c_fully_fold (t, false, NULL);
12944 if (TREE_CODE (t) != INTEGER_CST
12945 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12946 || tree_int_cst_sgn (t) != 1)
12948 error_at (clause_loc, "%<simdlen%> clause expression must "
12949 "be positive constant integer expression");
12950 t = NULL_TREE;
12953 parens.skip_until_found_close (parser);
12954 if (t == NULL_TREE || t == error_mark_node)
12955 return list;
12957 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12959 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12960 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12961 OMP_CLAUSE_CHAIN (c) = list;
12962 return c;
12965 /* OpenMP 4.5:
12966 vec:
12967 identifier [+/- integer]
12968 vec , identifier [+/- integer]
12971 static tree
12972 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12973 tree list)
12975 tree vec = NULL;
12976 if (c_parser_next_token_is_not (parser, CPP_NAME)
12977 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12979 c_parser_error (parser, "expected identifier");
12980 return list;
12983 while (c_parser_next_token_is (parser, CPP_NAME)
12984 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12986 tree t = lookup_name (c_parser_peek_token (parser)->value);
12987 tree addend = NULL;
12989 if (t == NULL_TREE)
12991 undeclared_variable (c_parser_peek_token (parser)->location,
12992 c_parser_peek_token (parser)->value);
12993 t = error_mark_node;
12996 c_parser_consume_token (parser);
12998 bool neg = false;
12999 if (c_parser_next_token_is (parser, CPP_MINUS))
13000 neg = true;
13001 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13003 addend = integer_zero_node;
13004 neg = false;
13005 goto add_to_vector;
13007 c_parser_consume_token (parser);
13009 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13011 c_parser_error (parser, "expected integer");
13012 return list;
13015 addend = c_parser_peek_token (parser)->value;
13016 if (TREE_CODE (addend) != INTEGER_CST)
13018 c_parser_error (parser, "expected integer");
13019 return list;
13021 c_parser_consume_token (parser);
13023 add_to_vector:
13024 if (t != error_mark_node)
13026 vec = tree_cons (addend, t, vec);
13027 if (neg)
13028 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13031 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13032 break;
13034 c_parser_consume_token (parser);
13037 if (vec == NULL_TREE)
13038 return list;
13040 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13041 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13042 OMP_CLAUSE_DECL (u) = nreverse (vec);
13043 OMP_CLAUSE_CHAIN (u) = list;
13044 return u;
13047 /* OpenMP 4.0:
13048 depend ( depend-kind: variable-list )
13050 depend-kind:
13051 in | out | inout
13053 OpenMP 4.5:
13054 depend ( source )
13056 depend ( sink : vec ) */
13058 static tree
13059 c_parser_omp_clause_depend (c_parser *parser, tree list)
13061 location_t clause_loc = c_parser_peek_token (parser)->location;
13062 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13063 tree nl, c;
13065 matching_parens parens;
13066 if (!parens.require_open (parser))
13067 return list;
13069 if (c_parser_next_token_is (parser, CPP_NAME))
13071 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13072 if (strcmp ("in", p) == 0)
13073 kind = OMP_CLAUSE_DEPEND_IN;
13074 else if (strcmp ("inout", p) == 0)
13075 kind = OMP_CLAUSE_DEPEND_INOUT;
13076 else if (strcmp ("out", p) == 0)
13077 kind = OMP_CLAUSE_DEPEND_OUT;
13078 else if (strcmp ("source", p) == 0)
13079 kind = OMP_CLAUSE_DEPEND_SOURCE;
13080 else if (strcmp ("sink", p) == 0)
13081 kind = OMP_CLAUSE_DEPEND_SINK;
13082 else
13083 goto invalid_kind;
13085 else
13086 goto invalid_kind;
13088 c_parser_consume_token (parser);
13090 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13092 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13093 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13094 OMP_CLAUSE_DECL (c) = NULL_TREE;
13095 OMP_CLAUSE_CHAIN (c) = list;
13096 parens.skip_until_found_close (parser);
13097 return c;
13100 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13101 goto resync_fail;
13103 if (kind == OMP_CLAUSE_DEPEND_SINK)
13104 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13105 else
13107 nl = c_parser_omp_variable_list (parser, clause_loc,
13108 OMP_CLAUSE_DEPEND, list);
13110 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13111 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13114 parens.skip_until_found_close (parser);
13115 return nl;
13117 invalid_kind:
13118 c_parser_error (parser, "invalid depend kind");
13119 resync_fail:
13120 parens.skip_until_found_close (parser);
13121 return list;
13124 /* OpenMP 4.0:
13125 map ( map-kind: variable-list )
13126 map ( variable-list )
13128 map-kind:
13129 alloc | to | from | tofrom
13131 OpenMP 4.5:
13132 map-kind:
13133 alloc | to | from | tofrom | release | delete
13135 map ( always [,] map-kind: variable-list ) */
13137 static tree
13138 c_parser_omp_clause_map (c_parser *parser, tree list)
13140 location_t clause_loc = c_parser_peek_token (parser)->location;
13141 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13142 int always = 0;
13143 enum c_id_kind always_id_kind = C_ID_NONE;
13144 location_t always_loc = UNKNOWN_LOCATION;
13145 tree always_id = NULL_TREE;
13146 tree nl, c;
13148 matching_parens parens;
13149 if (!parens.require_open (parser))
13150 return list;
13152 if (c_parser_next_token_is (parser, CPP_NAME))
13154 c_token *tok = c_parser_peek_token (parser);
13155 const char *p = IDENTIFIER_POINTER (tok->value);
13156 always_id_kind = tok->id_kind;
13157 always_loc = tok->location;
13158 always_id = tok->value;
13159 if (strcmp ("always", p) == 0)
13161 c_token *sectok = c_parser_peek_2nd_token (parser);
13162 if (sectok->type == CPP_COMMA)
13164 c_parser_consume_token (parser);
13165 c_parser_consume_token (parser);
13166 always = 2;
13168 else if (sectok->type == CPP_NAME)
13170 p = IDENTIFIER_POINTER (sectok->value);
13171 if (strcmp ("alloc", p) == 0
13172 || strcmp ("to", p) == 0
13173 || strcmp ("from", p) == 0
13174 || strcmp ("tofrom", p) == 0
13175 || strcmp ("release", p) == 0
13176 || strcmp ("delete", p) == 0)
13178 c_parser_consume_token (parser);
13179 always = 1;
13185 if (c_parser_next_token_is (parser, CPP_NAME)
13186 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13188 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13189 if (strcmp ("alloc", p) == 0)
13190 kind = GOMP_MAP_ALLOC;
13191 else if (strcmp ("to", p) == 0)
13192 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13193 else if (strcmp ("from", p) == 0)
13194 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13195 else if (strcmp ("tofrom", p) == 0)
13196 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13197 else if (strcmp ("release", p) == 0)
13198 kind = GOMP_MAP_RELEASE;
13199 else if (strcmp ("delete", p) == 0)
13200 kind = GOMP_MAP_DELETE;
13201 else
13203 c_parser_error (parser, "invalid map kind");
13204 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13205 "expected %<)%>");
13206 return list;
13208 c_parser_consume_token (parser);
13209 c_parser_consume_token (parser);
13211 else if (always)
13213 if (always_id_kind != C_ID_ID)
13215 c_parser_error (parser, "expected identifier");
13216 parens.skip_until_found_close (parser);
13217 return list;
13220 tree t = lookup_name (always_id);
13221 if (t == NULL_TREE)
13223 undeclared_variable (always_loc, always_id);
13224 t = error_mark_node;
13226 if (t != error_mark_node)
13228 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13229 OMP_CLAUSE_DECL (u) = t;
13230 OMP_CLAUSE_CHAIN (u) = list;
13231 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13232 list = u;
13234 if (always == 1)
13236 parens.skip_until_found_close (parser);
13237 return list;
13241 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13243 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13244 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13246 parens.skip_until_found_close (parser);
13247 return nl;
13250 /* OpenMP 4.0:
13251 device ( expression ) */
13253 static tree
13254 c_parser_omp_clause_device (c_parser *parser, tree list)
13256 location_t clause_loc = c_parser_peek_token (parser)->location;
13257 matching_parens parens;
13258 if (parens.require_open (parser))
13260 location_t expr_loc = c_parser_peek_token (parser)->location;
13261 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13262 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13263 tree c, t = expr.value;
13264 t = c_fully_fold (t, false, NULL);
13266 parens.skip_until_found_close (parser);
13268 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13270 c_parser_error (parser, "expected integer expression");
13271 return list;
13274 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13276 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13277 OMP_CLAUSE_DEVICE_ID (c) = t;
13278 OMP_CLAUSE_CHAIN (c) = list;
13279 list = c;
13282 return list;
13285 /* OpenMP 4.0:
13286 dist_schedule ( static )
13287 dist_schedule ( static , expression ) */
13289 static tree
13290 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13292 tree c, t = NULL_TREE;
13293 location_t loc = c_parser_peek_token (parser)->location;
13295 matching_parens parens;
13296 if (!parens.require_open (parser))
13297 return list;
13299 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13301 c_parser_error (parser, "invalid dist_schedule kind");
13302 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13303 "expected %<)%>");
13304 return list;
13307 c_parser_consume_token (parser);
13308 if (c_parser_next_token_is (parser, CPP_COMMA))
13310 c_parser_consume_token (parser);
13312 location_t expr_loc = c_parser_peek_token (parser)->location;
13313 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13314 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13315 t = expr.value;
13316 t = c_fully_fold (t, false, NULL);
13317 parens.skip_until_found_close (parser);
13319 else
13320 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13321 "expected %<,%> or %<)%>");
13323 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13324 if (t == error_mark_node)
13325 return list;
13327 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13328 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13329 OMP_CLAUSE_CHAIN (c) = list;
13330 return c;
13333 /* OpenMP 4.0:
13334 proc_bind ( proc-bind-kind )
13336 proc-bind-kind:
13337 master | close | spread */
13339 static tree
13340 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13342 location_t clause_loc = c_parser_peek_token (parser)->location;
13343 enum omp_clause_proc_bind_kind kind;
13344 tree c;
13346 matching_parens parens;
13347 if (!parens.require_open (parser))
13348 return list;
13350 if (c_parser_next_token_is (parser, CPP_NAME))
13352 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13353 if (strcmp ("master", p) == 0)
13354 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13355 else if (strcmp ("close", p) == 0)
13356 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13357 else if (strcmp ("spread", p) == 0)
13358 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13359 else
13360 goto invalid_kind;
13362 else
13363 goto invalid_kind;
13365 c_parser_consume_token (parser);
13366 parens.skip_until_found_close (parser);
13367 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13368 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13369 OMP_CLAUSE_CHAIN (c) = list;
13370 return c;
13372 invalid_kind:
13373 c_parser_error (parser, "invalid proc_bind kind");
13374 parens.skip_until_found_close (parser);
13375 return list;
13378 /* OpenMP 4.0:
13379 to ( variable-list ) */
13381 static tree
13382 c_parser_omp_clause_to (c_parser *parser, tree list)
13384 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13387 /* OpenMP 4.0:
13388 from ( variable-list ) */
13390 static tree
13391 c_parser_omp_clause_from (c_parser *parser, tree list)
13393 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13396 /* OpenMP 4.0:
13397 uniform ( variable-list ) */
13399 static tree
13400 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13402 /* The clauses location. */
13403 location_t loc = c_parser_peek_token (parser)->location;
13405 matching_parens parens;
13406 if (parens.require_open (parser))
13408 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13409 list);
13410 parens.skip_until_found_close (parser);
13412 return list;
13415 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13416 is a bitmask in MASK. Return the list of clauses found. */
13418 static tree
13419 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13420 const char *where, bool finish_p = true)
13422 tree clauses = NULL;
13423 bool first = true;
13425 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13427 location_t here;
13428 pragma_omp_clause c_kind;
13429 const char *c_name;
13430 tree prev = clauses;
13432 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13433 c_parser_consume_token (parser);
13435 here = c_parser_peek_token (parser)->location;
13436 c_kind = c_parser_omp_clause_name (parser);
13438 switch (c_kind)
13440 case PRAGMA_OACC_CLAUSE_ASYNC:
13441 clauses = c_parser_oacc_clause_async (parser, clauses);
13442 c_name = "async";
13443 break;
13444 case PRAGMA_OACC_CLAUSE_AUTO:
13445 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13446 clauses);
13447 c_name = "auto";
13448 break;
13449 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13450 clauses = c_parser_omp_clause_collapse (parser, clauses);
13451 c_name = "collapse";
13452 break;
13453 case PRAGMA_OACC_CLAUSE_COPY:
13454 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13455 c_name = "copy";
13456 break;
13457 case PRAGMA_OACC_CLAUSE_COPYIN:
13458 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13459 c_name = "copyin";
13460 break;
13461 case PRAGMA_OACC_CLAUSE_COPYOUT:
13462 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13463 c_name = "copyout";
13464 break;
13465 case PRAGMA_OACC_CLAUSE_CREATE:
13466 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13467 c_name = "create";
13468 break;
13469 case PRAGMA_OACC_CLAUSE_DELETE:
13470 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13471 c_name = "delete";
13472 break;
13473 case PRAGMA_OMP_CLAUSE_DEFAULT:
13474 clauses = c_parser_omp_clause_default (parser, clauses, true);
13475 c_name = "default";
13476 break;
13477 case PRAGMA_OACC_CLAUSE_DEVICE:
13478 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13479 c_name = "device";
13480 break;
13481 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13482 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13483 c_name = "deviceptr";
13484 break;
13485 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13486 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13487 c_name = "device_resident";
13488 break;
13489 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13490 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13491 c_name = "firstprivate";
13492 break;
13493 case PRAGMA_OACC_CLAUSE_GANG:
13494 c_name = "gang";
13495 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13496 c_name, clauses);
13497 break;
13498 case PRAGMA_OACC_CLAUSE_HOST:
13499 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13500 c_name = "host";
13501 break;
13502 case PRAGMA_OACC_CLAUSE_IF:
13503 clauses = c_parser_omp_clause_if (parser, clauses, false);
13504 c_name = "if";
13505 break;
13506 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13507 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13508 clauses);
13509 c_name = "independent";
13510 break;
13511 case PRAGMA_OACC_CLAUSE_LINK:
13512 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13513 c_name = "link";
13514 break;
13515 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13516 clauses = c_parser_oacc_single_int_clause (parser,
13517 OMP_CLAUSE_NUM_GANGS,
13518 clauses);
13519 c_name = "num_gangs";
13520 break;
13521 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13522 clauses = c_parser_oacc_single_int_clause (parser,
13523 OMP_CLAUSE_NUM_WORKERS,
13524 clauses);
13525 c_name = "num_workers";
13526 break;
13527 case PRAGMA_OACC_CLAUSE_PRESENT:
13528 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13529 c_name = "present";
13530 break;
13531 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13532 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13533 c_name = "present_or_copy";
13534 break;
13535 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13536 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13537 c_name = "present_or_copyin";
13538 break;
13539 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13540 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13541 c_name = "present_or_copyout";
13542 break;
13543 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13544 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13545 c_name = "present_or_create";
13546 break;
13547 case PRAGMA_OACC_CLAUSE_PRIVATE:
13548 clauses = c_parser_omp_clause_private (parser, clauses);
13549 c_name = "private";
13550 break;
13551 case PRAGMA_OACC_CLAUSE_REDUCTION:
13552 clauses = c_parser_omp_clause_reduction (parser, clauses);
13553 c_name = "reduction";
13554 break;
13555 case PRAGMA_OACC_CLAUSE_SELF:
13556 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13557 c_name = "self";
13558 break;
13559 case PRAGMA_OACC_CLAUSE_SEQ:
13560 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13561 clauses);
13562 c_name = "seq";
13563 break;
13564 case PRAGMA_OACC_CLAUSE_TILE:
13565 clauses = c_parser_oacc_clause_tile (parser, clauses);
13566 c_name = "tile";
13567 break;
13568 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13569 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13570 c_name = "use_device";
13571 break;
13572 case PRAGMA_OACC_CLAUSE_VECTOR:
13573 c_name = "vector";
13574 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13575 c_name, clauses);
13576 break;
13577 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13578 clauses = c_parser_oacc_single_int_clause (parser,
13579 OMP_CLAUSE_VECTOR_LENGTH,
13580 clauses);
13581 c_name = "vector_length";
13582 break;
13583 case PRAGMA_OACC_CLAUSE_WAIT:
13584 clauses = c_parser_oacc_clause_wait (parser, clauses);
13585 c_name = "wait";
13586 break;
13587 case PRAGMA_OACC_CLAUSE_WORKER:
13588 c_name = "worker";
13589 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13590 c_name, clauses);
13591 break;
13592 default:
13593 c_parser_error (parser, "expected %<#pragma acc%> clause");
13594 goto saw_error;
13597 first = false;
13599 if (((mask >> c_kind) & 1) == 0)
13601 /* Remove the invalid clause(s) from the list to avoid
13602 confusing the rest of the compiler. */
13603 clauses = prev;
13604 error_at (here, "%qs is not valid for %qs", c_name, where);
13608 saw_error:
13609 c_parser_skip_to_pragma_eol (parser);
13611 if (finish_p)
13612 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13614 return clauses;
13617 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13618 is a bitmask in MASK. Return the list of clauses found. */
13620 static tree
13621 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13622 const char *where, bool finish_p = true)
13624 tree clauses = NULL;
13625 bool first = true, cilk_simd_fn = false;
13627 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13629 location_t here;
13630 pragma_omp_clause c_kind;
13631 const char *c_name;
13632 tree prev = clauses;
13634 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13635 c_parser_consume_token (parser);
13637 here = c_parser_peek_token (parser)->location;
13638 c_kind = c_parser_omp_clause_name (parser);
13640 switch (c_kind)
13642 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13643 clauses = c_parser_omp_clause_collapse (parser, clauses);
13644 c_name = "collapse";
13645 break;
13646 case PRAGMA_OMP_CLAUSE_COPYIN:
13647 clauses = c_parser_omp_clause_copyin (parser, clauses);
13648 c_name = "copyin";
13649 break;
13650 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13651 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13652 c_name = "copyprivate";
13653 break;
13654 case PRAGMA_OMP_CLAUSE_DEFAULT:
13655 clauses = c_parser_omp_clause_default (parser, clauses, false);
13656 c_name = "default";
13657 break;
13658 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13659 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13660 c_name = "firstprivate";
13661 break;
13662 case PRAGMA_OMP_CLAUSE_FINAL:
13663 clauses = c_parser_omp_clause_final (parser, clauses);
13664 c_name = "final";
13665 break;
13666 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13667 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13668 c_name = "grainsize";
13669 break;
13670 case PRAGMA_OMP_CLAUSE_HINT:
13671 clauses = c_parser_omp_clause_hint (parser, clauses);
13672 c_name = "hint";
13673 break;
13674 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13675 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13676 c_name = "defaultmap";
13677 break;
13678 case PRAGMA_OMP_CLAUSE_IF:
13679 clauses = c_parser_omp_clause_if (parser, clauses, true);
13680 c_name = "if";
13681 break;
13682 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13683 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13684 c_name = "lastprivate";
13685 break;
13686 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13687 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13688 c_name = "mergeable";
13689 break;
13690 case PRAGMA_OMP_CLAUSE_NOWAIT:
13691 clauses = c_parser_omp_clause_nowait (parser, clauses);
13692 c_name = "nowait";
13693 break;
13694 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13695 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13696 c_name = "num_tasks";
13697 break;
13698 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13699 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13700 c_name = "num_threads";
13701 break;
13702 case PRAGMA_OMP_CLAUSE_ORDERED:
13703 clauses = c_parser_omp_clause_ordered (parser, clauses);
13704 c_name = "ordered";
13705 break;
13706 case PRAGMA_OMP_CLAUSE_PRIORITY:
13707 clauses = c_parser_omp_clause_priority (parser, clauses);
13708 c_name = "priority";
13709 break;
13710 case PRAGMA_OMP_CLAUSE_PRIVATE:
13711 clauses = c_parser_omp_clause_private (parser, clauses);
13712 c_name = "private";
13713 break;
13714 case PRAGMA_OMP_CLAUSE_REDUCTION:
13715 clauses = c_parser_omp_clause_reduction (parser, clauses);
13716 c_name = "reduction";
13717 break;
13718 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13719 clauses = c_parser_omp_clause_schedule (parser, clauses);
13720 c_name = "schedule";
13721 break;
13722 case PRAGMA_OMP_CLAUSE_SHARED:
13723 clauses = c_parser_omp_clause_shared (parser, clauses);
13724 c_name = "shared";
13725 break;
13726 case PRAGMA_OMP_CLAUSE_UNTIED:
13727 clauses = c_parser_omp_clause_untied (parser, clauses);
13728 c_name = "untied";
13729 break;
13730 case PRAGMA_OMP_CLAUSE_INBRANCH:
13731 case PRAGMA_CILK_CLAUSE_MASK:
13732 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13733 clauses);
13734 c_name = "inbranch";
13735 break;
13736 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13737 case PRAGMA_CILK_CLAUSE_NOMASK:
13738 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13739 clauses);
13740 c_name = "notinbranch";
13741 break;
13742 case PRAGMA_OMP_CLAUSE_PARALLEL:
13743 clauses
13744 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13745 clauses);
13746 c_name = "parallel";
13747 if (!first)
13749 clause_not_first:
13750 error_at (here, "%qs must be the first clause of %qs",
13751 c_name, where);
13752 clauses = prev;
13754 break;
13755 case PRAGMA_OMP_CLAUSE_FOR:
13756 clauses
13757 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13758 clauses);
13759 c_name = "for";
13760 if (!first)
13761 goto clause_not_first;
13762 break;
13763 case PRAGMA_OMP_CLAUSE_SECTIONS:
13764 clauses
13765 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13766 clauses);
13767 c_name = "sections";
13768 if (!first)
13769 goto clause_not_first;
13770 break;
13771 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13772 clauses
13773 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13774 clauses);
13775 c_name = "taskgroup";
13776 if (!first)
13777 goto clause_not_first;
13778 break;
13779 case PRAGMA_OMP_CLAUSE_LINK:
13780 clauses
13781 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13782 c_name = "link";
13783 break;
13784 case PRAGMA_OMP_CLAUSE_TO:
13785 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13786 clauses
13787 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13788 clauses);
13789 else
13790 clauses = c_parser_omp_clause_to (parser, clauses);
13791 c_name = "to";
13792 break;
13793 case PRAGMA_OMP_CLAUSE_FROM:
13794 clauses = c_parser_omp_clause_from (parser, clauses);
13795 c_name = "from";
13796 break;
13797 case PRAGMA_OMP_CLAUSE_UNIFORM:
13798 clauses = c_parser_omp_clause_uniform (parser, clauses);
13799 c_name = "uniform";
13800 break;
13801 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13802 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13803 c_name = "num_teams";
13804 break;
13805 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13806 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13807 c_name = "thread_limit";
13808 break;
13809 case PRAGMA_OMP_CLAUSE_ALIGNED:
13810 clauses = c_parser_omp_clause_aligned (parser, clauses);
13811 c_name = "aligned";
13812 break;
13813 case PRAGMA_OMP_CLAUSE_LINEAR:
13814 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13815 cilk_simd_fn = true;
13816 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13817 c_name = "linear";
13818 break;
13819 case PRAGMA_OMP_CLAUSE_DEPEND:
13820 clauses = c_parser_omp_clause_depend (parser, clauses);
13821 c_name = "depend";
13822 break;
13823 case PRAGMA_OMP_CLAUSE_MAP:
13824 clauses = c_parser_omp_clause_map (parser, clauses);
13825 c_name = "map";
13826 break;
13827 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13828 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13829 c_name = "use_device_ptr";
13830 break;
13831 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13832 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13833 c_name = "is_device_ptr";
13834 break;
13835 case PRAGMA_OMP_CLAUSE_DEVICE:
13836 clauses = c_parser_omp_clause_device (parser, clauses);
13837 c_name = "device";
13838 break;
13839 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13840 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13841 c_name = "dist_schedule";
13842 break;
13843 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13844 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13845 c_name = "proc_bind";
13846 break;
13847 case PRAGMA_OMP_CLAUSE_SAFELEN:
13848 clauses = c_parser_omp_clause_safelen (parser, clauses);
13849 c_name = "safelen";
13850 break;
13851 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13852 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13853 c_name = "simdlen";
13854 break;
13855 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13856 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13857 c_name = "simdlen";
13858 break;
13859 case PRAGMA_OMP_CLAUSE_NOGROUP:
13860 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13861 c_name = "nogroup";
13862 break;
13863 case PRAGMA_OMP_CLAUSE_THREADS:
13864 clauses
13865 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13866 clauses);
13867 c_name = "threads";
13868 break;
13869 case PRAGMA_OMP_CLAUSE_SIMD:
13870 clauses
13871 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13872 clauses);
13873 c_name = "simd";
13874 break;
13875 default:
13876 c_parser_error (parser, "expected %<#pragma omp%> clause");
13877 goto saw_error;
13880 first = false;
13882 if (((mask >> c_kind) & 1) == 0)
13884 /* Remove the invalid clause(s) from the list to avoid
13885 confusing the rest of the compiler. */
13886 clauses = prev;
13887 error_at (here, "%qs is not valid for %qs", c_name, where);
13891 saw_error:
13892 c_parser_skip_to_pragma_eol (parser);
13894 if (finish_p)
13896 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13897 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
13898 return c_finish_omp_clauses (clauses, C_ORT_OMP);
13901 return clauses;
13904 /* OpenACC 2.0, OpenMP 2.5:
13905 structured-block:
13906 statement
13908 In practice, we're also interested in adding the statement to an
13909 outer node. So it is convenient if we work around the fact that
13910 c_parser_statement calls add_stmt. */
13912 static tree
13913 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13915 tree stmt = push_stmt_list ();
13916 c_parser_statement (parser, if_p);
13917 return pop_stmt_list (stmt);
13920 /* OpenACC 2.0:
13921 # pragma acc cache (variable-list) new-line
13923 LOC is the location of the #pragma token.
13926 static tree
13927 c_parser_oacc_cache (location_t loc, c_parser *parser)
13929 tree stmt, clauses;
13931 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13932 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13934 c_parser_skip_to_pragma_eol (parser);
13936 stmt = make_node (OACC_CACHE);
13937 TREE_TYPE (stmt) = void_type_node;
13938 OACC_CACHE_CLAUSES (stmt) = clauses;
13939 SET_EXPR_LOCATION (stmt, loc);
13940 add_stmt (stmt);
13942 return stmt;
13945 /* OpenACC 2.0:
13946 # pragma acc data oacc-data-clause[optseq] new-line
13947 structured-block
13949 LOC is the location of the #pragma token.
13952 #define OACC_DATA_CLAUSE_MASK \
13953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13965 static tree
13966 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13968 tree stmt, clauses, block;
13970 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13971 "#pragma acc data");
13973 block = c_begin_omp_parallel ();
13974 add_stmt (c_parser_omp_structured_block (parser, if_p));
13976 stmt = c_finish_oacc_data (loc, clauses, block);
13978 return stmt;
13981 /* OpenACC 2.0:
13982 # pragma acc declare oacc-data-clause[optseq] new-line
13985 #define OACC_DECLARE_CLAUSE_MASK \
13986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13999 static void
14000 c_parser_oacc_declare (c_parser *parser)
14002 location_t pragma_loc = c_parser_peek_token (parser)->location;
14003 tree clauses, stmt, t, decl;
14005 bool error = false;
14007 c_parser_consume_pragma (parser);
14009 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14010 "#pragma acc declare");
14011 if (!clauses)
14013 error_at (pragma_loc,
14014 "no valid clauses specified in %<#pragma acc declare%>");
14015 return;
14018 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14020 location_t loc = OMP_CLAUSE_LOCATION (t);
14021 decl = OMP_CLAUSE_DECL (t);
14022 if (!DECL_P (decl))
14024 error_at (loc, "array section in %<#pragma acc declare%>");
14025 error = true;
14026 continue;
14029 switch (OMP_CLAUSE_MAP_KIND (t))
14031 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14032 case GOMP_MAP_FORCE_ALLOC:
14033 case GOMP_MAP_FORCE_TO:
14034 case GOMP_MAP_FORCE_DEVICEPTR:
14035 case GOMP_MAP_DEVICE_RESIDENT:
14036 break;
14038 case GOMP_MAP_LINK:
14039 if (!global_bindings_p ()
14040 && (TREE_STATIC (decl)
14041 || !DECL_EXTERNAL (decl)))
14043 error_at (loc,
14044 "%qD must be a global variable in "
14045 "%<#pragma acc declare link%>",
14046 decl);
14047 error = true;
14048 continue;
14050 break;
14052 default:
14053 if (global_bindings_p ())
14055 error_at (loc, "invalid OpenACC clause at file scope");
14056 error = true;
14057 continue;
14059 if (DECL_EXTERNAL (decl))
14061 error_at (loc,
14062 "invalid use of %<extern%> variable %qD "
14063 "in %<#pragma acc declare%>", decl);
14064 error = true;
14065 continue;
14067 else if (TREE_PUBLIC (decl))
14069 error_at (loc,
14070 "invalid use of %<global%> variable %qD "
14071 "in %<#pragma acc declare%>", decl);
14072 error = true;
14073 continue;
14075 break;
14078 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14079 || lookup_attribute ("omp declare target link",
14080 DECL_ATTRIBUTES (decl)))
14082 error_at (loc, "variable %qD used more than once with "
14083 "%<#pragma acc declare%>", decl);
14084 error = true;
14085 continue;
14088 if (!error)
14090 tree id;
14092 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14093 id = get_identifier ("omp declare target link");
14094 else
14095 id = get_identifier ("omp declare target");
14097 DECL_ATTRIBUTES (decl)
14098 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14100 if (global_bindings_p ())
14102 symtab_node *node = symtab_node::get (decl);
14103 if (node != NULL)
14105 node->offloadable = 1;
14106 if (ENABLE_OFFLOADING)
14108 g->have_offload = true;
14109 if (is_a <varpool_node *> (node))
14110 vec_safe_push (offload_vars, decl);
14117 if (error || global_bindings_p ())
14118 return;
14120 stmt = make_node (OACC_DECLARE);
14121 TREE_TYPE (stmt) = void_type_node;
14122 OACC_DECLARE_CLAUSES (stmt) = clauses;
14123 SET_EXPR_LOCATION (stmt, pragma_loc);
14125 add_stmt (stmt);
14127 return;
14130 /* OpenACC 2.0:
14131 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14135 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14138 LOC is the location of the #pragma token.
14141 #define OACC_ENTER_DATA_CLAUSE_MASK \
14142 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14150 #define OACC_EXIT_DATA_CLAUSE_MASK \
14151 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14157 static void
14158 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14160 location_t loc = c_parser_peek_token (parser)->location;
14161 tree clauses, stmt;
14162 const char *p = "";
14164 c_parser_consume_pragma (parser);
14166 if (c_parser_next_token_is (parser, CPP_NAME))
14168 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14169 c_parser_consume_token (parser);
14172 if (strcmp (p, "data") != 0)
14174 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14175 enter ? "enter" : "exit");
14176 parser->error = true;
14177 c_parser_skip_to_pragma_eol (parser);
14178 return;
14181 if (enter)
14182 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14183 "#pragma acc enter data");
14184 else
14185 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14186 "#pragma acc exit data");
14188 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14190 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14191 enter ? "enter" : "exit");
14192 return;
14195 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14196 TREE_TYPE (stmt) = void_type_node;
14197 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14198 SET_EXPR_LOCATION (stmt, loc);
14199 add_stmt (stmt);
14203 /* OpenACC 2.0:
14204 # pragma acc host_data oacc-data-clause[optseq] new-line
14205 structured-block
14208 #define OACC_HOST_DATA_CLAUSE_MASK \
14209 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14211 static tree
14212 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14214 tree stmt, clauses, block;
14216 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14217 "#pragma acc host_data");
14219 block = c_begin_omp_parallel ();
14220 add_stmt (c_parser_omp_structured_block (parser, if_p));
14221 stmt = c_finish_oacc_host_data (loc, clauses, block);
14222 return stmt;
14226 /* OpenACC 2.0:
14228 # pragma acc loop oacc-loop-clause[optseq] new-line
14229 structured-block
14231 LOC is the location of the #pragma token.
14234 #define OACC_LOOP_CLAUSE_MASK \
14235 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14245 static tree
14246 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14247 omp_clause_mask mask, tree *cclauses, bool *if_p)
14249 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14251 strcat (p_name, " loop");
14252 mask |= OACC_LOOP_CLAUSE_MASK;
14254 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14255 cclauses == NULL);
14256 if (cclauses)
14258 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14259 if (*cclauses)
14260 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14261 if (clauses)
14262 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14265 tree block = c_begin_compound_stmt (true);
14266 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14267 if_p);
14268 block = c_end_compound_stmt (loc, block, true);
14269 add_stmt (block);
14271 return stmt;
14274 /* OpenACC 2.0:
14275 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14276 structured-block
14280 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14281 structured-block
14283 LOC is the location of the #pragma token.
14286 #define OACC_KERNELS_CLAUSE_MASK \
14287 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14305 #define OACC_PARALLEL_CLAUSE_MASK \
14306 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14327 static tree
14328 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14329 enum pragma_kind p_kind, char *p_name,
14330 bool *if_p)
14332 omp_clause_mask mask;
14333 enum tree_code code;
14334 switch (p_kind)
14336 case PRAGMA_OACC_KERNELS:
14337 strcat (p_name, " kernels");
14338 mask = OACC_KERNELS_CLAUSE_MASK;
14339 code = OACC_KERNELS;
14340 break;
14341 case PRAGMA_OACC_PARALLEL:
14342 strcat (p_name, " parallel");
14343 mask = OACC_PARALLEL_CLAUSE_MASK;
14344 code = OACC_PARALLEL;
14345 break;
14346 default:
14347 gcc_unreachable ();
14350 if (c_parser_next_token_is (parser, CPP_NAME))
14352 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14353 if (strcmp (p, "loop") == 0)
14355 c_parser_consume_token (parser);
14356 tree block = c_begin_omp_parallel ();
14357 tree clauses;
14358 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14359 return c_finish_omp_construct (loc, code, block, clauses);
14363 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14365 tree block = c_begin_omp_parallel ();
14366 add_stmt (c_parser_omp_structured_block (parser, if_p));
14368 return c_finish_omp_construct (loc, code, block, clauses);
14371 /* OpenACC 2.0:
14372 # pragma acc routine oacc-routine-clause[optseq] new-line
14373 function-definition
14375 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14378 #define OACC_ROUTINE_CLAUSE_MASK \
14379 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14384 /* Parse an OpenACC routine directive. For named directives, we apply
14385 immediately to the named function. For unnamed ones we then parse
14386 a declaration or definition, which must be for a function. */
14388 static void
14389 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14391 gcc_checking_assert (context == pragma_external);
14393 oacc_routine_data data;
14394 data.error_seen = false;
14395 data.fndecl_seen = false;
14396 data.clauses = NULL_TREE;
14397 data.loc = c_parser_peek_token (parser)->location;
14399 c_parser_consume_pragma (parser);
14401 /* Look for optional '( name )'. */
14402 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14404 c_parser_consume_token (parser); /* '(' */
14406 tree decl = NULL_TREE;
14407 c_token *name_token = c_parser_peek_token (parser);
14408 location_t name_loc = name_token->location;
14409 if (name_token->type == CPP_NAME
14410 && (name_token->id_kind == C_ID_ID
14411 || name_token->id_kind == C_ID_TYPENAME))
14413 decl = lookup_name (name_token->value);
14414 if (!decl)
14415 error_at (name_loc,
14416 "%qE has not been declared", name_token->value);
14417 c_parser_consume_token (parser);
14419 else
14420 c_parser_error (parser, "expected function name");
14422 if (!decl
14423 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14425 c_parser_skip_to_pragma_eol (parser, false);
14426 return;
14429 data.clauses
14430 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14431 "#pragma acc routine");
14433 if (TREE_CODE (decl) != FUNCTION_DECL)
14435 error_at (name_loc, "%qD does not refer to a function", decl);
14436 return;
14439 c_finish_oacc_routine (&data, decl, false);
14441 else /* No optional '( name )'. */
14443 data.clauses
14444 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14445 "#pragma acc routine");
14447 /* Emit a helpful diagnostic if there's another pragma following this
14448 one. Also don't allow a static assertion declaration, as in the
14449 following we'll just parse a *single* "declaration or function
14450 definition", and the static assertion counts an one. */
14451 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14452 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14454 error_at (data.loc,
14455 "%<#pragma acc routine%> not immediately followed by"
14456 " function declaration or definition");
14457 /* ..., and then just keep going. */
14458 return;
14461 /* We only have to consider the pragma_external case here. */
14462 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14463 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14465 int ext = disable_extension_diagnostics ();
14467 c_parser_consume_token (parser);
14468 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14469 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14470 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14471 NULL, vNULL, &data);
14472 restore_extension_diagnostics (ext);
14474 else
14475 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14476 NULL, vNULL, &data);
14480 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14481 IS_DEFN is true if we're applying it to the definition. */
14483 static void
14484 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14485 bool is_defn)
14487 /* Keep going if we're in error reporting mode. */
14488 if (data->error_seen
14489 || fndecl == error_mark_node)
14490 return;
14492 if (data->fndecl_seen)
14494 error_at (data->loc,
14495 "%<#pragma acc routine%> not immediately followed by"
14496 " a single function declaration or definition");
14497 data->error_seen = true;
14498 return;
14500 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14502 error_at (data->loc,
14503 "%<#pragma acc routine%> not immediately followed by"
14504 " function declaration or definition");
14505 data->error_seen = true;
14506 return;
14509 if (oacc_get_fn_attrib (fndecl))
14511 error_at (data->loc,
14512 "%<#pragma acc routine%> already applied to %qD", fndecl);
14513 data->error_seen = true;
14514 return;
14517 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14519 error_at (data->loc,
14520 TREE_USED (fndecl)
14521 ? G_("%<#pragma acc routine%> must be applied before use")
14522 : G_("%<#pragma acc routine%> must be applied before "
14523 "definition"));
14524 data->error_seen = true;
14525 return;
14528 /* Process the routine's dimension clauses. */
14529 tree dims = oacc_build_routine_dims (data->clauses);
14530 oacc_replace_fn_attrib (fndecl, dims);
14532 /* Add an "omp declare target" attribute. */
14533 DECL_ATTRIBUTES (fndecl)
14534 = tree_cons (get_identifier ("omp declare target"),
14535 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14537 /* Remember that we've used this "#pragma acc routine". */
14538 data->fndecl_seen = true;
14541 /* OpenACC 2.0:
14542 # pragma acc update oacc-update-clause[optseq] new-line
14545 #define OACC_UPDATE_CLAUSE_MASK \
14546 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14553 static void
14554 c_parser_oacc_update (c_parser *parser)
14556 location_t loc = c_parser_peek_token (parser)->location;
14558 c_parser_consume_pragma (parser);
14560 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14561 "#pragma acc update");
14562 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14564 error_at (loc,
14565 "%<#pragma acc update%> must contain at least one "
14566 "%<device%> or %<host%> or %<self%> clause");
14567 return;
14570 if (parser->error)
14571 return;
14573 tree stmt = make_node (OACC_UPDATE);
14574 TREE_TYPE (stmt) = void_type_node;
14575 OACC_UPDATE_CLAUSES (stmt) = clauses;
14576 SET_EXPR_LOCATION (stmt, loc);
14577 add_stmt (stmt);
14580 /* OpenACC 2.0:
14581 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14583 LOC is the location of the #pragma token.
14586 #define OACC_WAIT_CLAUSE_MASK \
14587 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14589 static tree
14590 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14592 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14594 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14595 list = c_parser_oacc_wait_list (parser, loc, list);
14597 strcpy (p_name, " wait");
14598 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14599 stmt = c_finish_oacc_wait (loc, list, clauses);
14600 add_stmt (stmt);
14602 return stmt;
14605 /* OpenMP 2.5:
14606 # pragma omp atomic new-line
14607 expression-stmt
14609 expression-stmt:
14610 x binop= expr | x++ | ++x | x-- | --x
14611 binop:
14612 +, *, -, /, &, ^, |, <<, >>
14614 where x is an lvalue expression with scalar type.
14616 OpenMP 3.1:
14617 # pragma omp atomic new-line
14618 update-stmt
14620 # pragma omp atomic read new-line
14621 read-stmt
14623 # pragma omp atomic write new-line
14624 write-stmt
14626 # pragma omp atomic update new-line
14627 update-stmt
14629 # pragma omp atomic capture new-line
14630 capture-stmt
14632 # pragma omp atomic capture new-line
14633 capture-block
14635 read-stmt:
14636 v = x
14637 write-stmt:
14638 x = expr
14639 update-stmt:
14640 expression-stmt | x = x binop expr
14641 capture-stmt:
14642 v = expression-stmt
14643 capture-block:
14644 { v = x; update-stmt; } | { update-stmt; v = x; }
14646 OpenMP 4.0:
14647 update-stmt:
14648 expression-stmt | x = x binop expr | x = expr binop x
14649 capture-stmt:
14650 v = update-stmt
14651 capture-block:
14652 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14654 where x and v are lvalue expressions with scalar type.
14656 LOC is the location of the #pragma token. */
14658 static void
14659 c_parser_omp_atomic (location_t loc, c_parser *parser)
14661 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14662 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14663 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14664 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14665 struct c_expr expr;
14666 location_t eloc;
14667 bool structured_block = false;
14668 bool swapped = false;
14669 bool seq_cst = false;
14670 bool non_lvalue_p;
14672 if (c_parser_next_token_is (parser, CPP_NAME))
14674 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14675 if (!strcmp (p, "seq_cst"))
14677 seq_cst = true;
14678 c_parser_consume_token (parser);
14679 if (c_parser_next_token_is (parser, CPP_COMMA)
14680 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14681 c_parser_consume_token (parser);
14684 if (c_parser_next_token_is (parser, CPP_NAME))
14686 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14688 if (!strcmp (p, "read"))
14689 code = OMP_ATOMIC_READ;
14690 else if (!strcmp (p, "write"))
14691 code = NOP_EXPR;
14692 else if (!strcmp (p, "update"))
14693 code = OMP_ATOMIC;
14694 else if (!strcmp (p, "capture"))
14695 code = OMP_ATOMIC_CAPTURE_NEW;
14696 else
14697 p = NULL;
14698 if (p)
14699 c_parser_consume_token (parser);
14701 if (!seq_cst)
14703 if (c_parser_next_token_is (parser, CPP_COMMA)
14704 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14705 c_parser_consume_token (parser);
14707 if (c_parser_next_token_is (parser, CPP_NAME))
14709 const char *p
14710 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14711 if (!strcmp (p, "seq_cst"))
14713 seq_cst = true;
14714 c_parser_consume_token (parser);
14718 c_parser_skip_to_pragma_eol (parser);
14720 switch (code)
14722 case OMP_ATOMIC_READ:
14723 case NOP_EXPR: /* atomic write */
14724 v = c_parser_cast_expression (parser, NULL).value;
14725 non_lvalue_p = !lvalue_p (v);
14726 v = c_fully_fold (v, false, NULL);
14727 if (v == error_mark_node)
14728 goto saw_error;
14729 if (non_lvalue_p)
14730 v = non_lvalue (v);
14731 loc = c_parser_peek_token (parser)->location;
14732 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14733 goto saw_error;
14734 if (code == NOP_EXPR)
14736 lhs = c_parser_expression (parser).value;
14737 lhs = c_fully_fold (lhs, false, NULL);
14738 if (lhs == error_mark_node)
14739 goto saw_error;
14741 else
14743 lhs = c_parser_cast_expression (parser, NULL).value;
14744 non_lvalue_p = !lvalue_p (lhs);
14745 lhs = c_fully_fold (lhs, false, NULL);
14746 if (lhs == error_mark_node)
14747 goto saw_error;
14748 if (non_lvalue_p)
14749 lhs = non_lvalue (lhs);
14751 if (code == NOP_EXPR)
14753 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14754 opcode. */
14755 code = OMP_ATOMIC;
14756 rhs = lhs;
14757 lhs = v;
14758 v = NULL_TREE;
14760 goto done;
14761 case OMP_ATOMIC_CAPTURE_NEW:
14762 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14764 c_parser_consume_token (parser);
14765 structured_block = true;
14767 else
14769 v = c_parser_cast_expression (parser, NULL).value;
14770 non_lvalue_p = !lvalue_p (v);
14771 v = c_fully_fold (v, false, NULL);
14772 if (v == error_mark_node)
14773 goto saw_error;
14774 if (non_lvalue_p)
14775 v = non_lvalue (v);
14776 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14777 goto saw_error;
14779 break;
14780 default:
14781 break;
14784 /* For structured_block case we don't know yet whether
14785 old or new x should be captured. */
14786 restart:
14787 eloc = c_parser_peek_token (parser)->location;
14788 expr = c_parser_cast_expression (parser, NULL);
14789 lhs = expr.value;
14790 expr = default_function_array_conversion (eloc, expr);
14791 unfolded_lhs = expr.value;
14792 lhs = c_fully_fold (lhs, false, NULL);
14793 orig_lhs = lhs;
14794 switch (TREE_CODE (lhs))
14796 case ERROR_MARK:
14797 saw_error:
14798 c_parser_skip_to_end_of_block_or_statement (parser);
14799 if (structured_block)
14801 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14802 c_parser_consume_token (parser);
14803 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14805 c_parser_skip_to_end_of_block_or_statement (parser);
14806 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14807 c_parser_consume_token (parser);
14810 return;
14812 case POSTINCREMENT_EXPR:
14813 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14814 code = OMP_ATOMIC_CAPTURE_OLD;
14815 /* FALLTHROUGH */
14816 case PREINCREMENT_EXPR:
14817 lhs = TREE_OPERAND (lhs, 0);
14818 unfolded_lhs = NULL_TREE;
14819 opcode = PLUS_EXPR;
14820 rhs = integer_one_node;
14821 break;
14823 case POSTDECREMENT_EXPR:
14824 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14825 code = OMP_ATOMIC_CAPTURE_OLD;
14826 /* FALLTHROUGH */
14827 case PREDECREMENT_EXPR:
14828 lhs = TREE_OPERAND (lhs, 0);
14829 unfolded_lhs = NULL_TREE;
14830 opcode = MINUS_EXPR;
14831 rhs = integer_one_node;
14832 break;
14834 case COMPOUND_EXPR:
14835 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14836 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14837 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14838 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14839 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14840 (TREE_OPERAND (lhs, 1), 0), 0)))
14841 == BOOLEAN_TYPE)
14842 /* Undo effects of boolean_increment for post {in,de}crement. */
14843 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14844 /* FALLTHRU */
14845 case MODIFY_EXPR:
14846 if (TREE_CODE (lhs) == MODIFY_EXPR
14847 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14849 /* Undo effects of boolean_increment. */
14850 if (integer_onep (TREE_OPERAND (lhs, 1)))
14852 /* This is pre or post increment. */
14853 rhs = TREE_OPERAND (lhs, 1);
14854 lhs = TREE_OPERAND (lhs, 0);
14855 unfolded_lhs = NULL_TREE;
14856 opcode = NOP_EXPR;
14857 if (code == OMP_ATOMIC_CAPTURE_NEW
14858 && !structured_block
14859 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14860 code = OMP_ATOMIC_CAPTURE_OLD;
14861 break;
14863 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14864 && TREE_OPERAND (lhs, 0)
14865 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14867 /* This is pre or post decrement. */
14868 rhs = TREE_OPERAND (lhs, 1);
14869 lhs = TREE_OPERAND (lhs, 0);
14870 unfolded_lhs = NULL_TREE;
14871 opcode = NOP_EXPR;
14872 if (code == OMP_ATOMIC_CAPTURE_NEW
14873 && !structured_block
14874 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14875 code = OMP_ATOMIC_CAPTURE_OLD;
14876 break;
14879 /* FALLTHRU */
14880 default:
14881 if (!lvalue_p (unfolded_lhs))
14882 lhs = non_lvalue (lhs);
14883 switch (c_parser_peek_token (parser)->type)
14885 case CPP_MULT_EQ:
14886 opcode = MULT_EXPR;
14887 break;
14888 case CPP_DIV_EQ:
14889 opcode = TRUNC_DIV_EXPR;
14890 break;
14891 case CPP_PLUS_EQ:
14892 opcode = PLUS_EXPR;
14893 break;
14894 case CPP_MINUS_EQ:
14895 opcode = MINUS_EXPR;
14896 break;
14897 case CPP_LSHIFT_EQ:
14898 opcode = LSHIFT_EXPR;
14899 break;
14900 case CPP_RSHIFT_EQ:
14901 opcode = RSHIFT_EXPR;
14902 break;
14903 case CPP_AND_EQ:
14904 opcode = BIT_AND_EXPR;
14905 break;
14906 case CPP_OR_EQ:
14907 opcode = BIT_IOR_EXPR;
14908 break;
14909 case CPP_XOR_EQ:
14910 opcode = BIT_XOR_EXPR;
14911 break;
14912 case CPP_EQ:
14913 c_parser_consume_token (parser);
14914 eloc = c_parser_peek_token (parser)->location;
14915 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14916 rhs1 = expr.value;
14917 switch (TREE_CODE (rhs1))
14919 case MULT_EXPR:
14920 case TRUNC_DIV_EXPR:
14921 case RDIV_EXPR:
14922 case PLUS_EXPR:
14923 case MINUS_EXPR:
14924 case LSHIFT_EXPR:
14925 case RSHIFT_EXPR:
14926 case BIT_AND_EXPR:
14927 case BIT_IOR_EXPR:
14928 case BIT_XOR_EXPR:
14929 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14931 opcode = TREE_CODE (rhs1);
14932 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14933 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14934 goto stmt_done;
14936 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14938 opcode = TREE_CODE (rhs1);
14939 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14940 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14941 swapped = !commutative_tree_code (opcode);
14942 goto stmt_done;
14944 break;
14945 case ERROR_MARK:
14946 goto saw_error;
14947 default:
14948 break;
14950 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14952 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14954 code = OMP_ATOMIC_CAPTURE_OLD;
14955 v = lhs;
14956 lhs = NULL_TREE;
14957 expr = default_function_array_read_conversion (eloc, expr);
14958 unfolded_lhs1 = expr.value;
14959 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14960 rhs1 = NULL_TREE;
14961 c_parser_consume_token (parser);
14962 goto restart;
14964 if (structured_block)
14966 opcode = NOP_EXPR;
14967 expr = default_function_array_read_conversion (eloc, expr);
14968 rhs = c_fully_fold (expr.value, false, NULL);
14969 rhs1 = NULL_TREE;
14970 goto stmt_done;
14973 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14974 goto saw_error;
14975 default:
14976 c_parser_error (parser,
14977 "invalid operator for %<#pragma omp atomic%>");
14978 goto saw_error;
14981 /* Arrange to pass the location of the assignment operator to
14982 c_finish_omp_atomic. */
14983 loc = c_parser_peek_token (parser)->location;
14984 c_parser_consume_token (parser);
14985 eloc = c_parser_peek_token (parser)->location;
14986 expr = c_parser_expression (parser);
14987 expr = default_function_array_read_conversion (eloc, expr);
14988 rhs = expr.value;
14989 rhs = c_fully_fold (rhs, false, NULL);
14990 break;
14992 stmt_done:
14993 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14995 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14996 goto saw_error;
14997 v = c_parser_cast_expression (parser, NULL).value;
14998 non_lvalue_p = !lvalue_p (v);
14999 v = c_fully_fold (v, false, NULL);
15000 if (v == error_mark_node)
15001 goto saw_error;
15002 if (non_lvalue_p)
15003 v = non_lvalue (v);
15004 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15005 goto saw_error;
15006 eloc = c_parser_peek_token (parser)->location;
15007 expr = c_parser_cast_expression (parser, NULL);
15008 lhs1 = expr.value;
15009 expr = default_function_array_read_conversion (eloc, expr);
15010 unfolded_lhs1 = expr.value;
15011 lhs1 = c_fully_fold (lhs1, false, NULL);
15012 if (lhs1 == error_mark_node)
15013 goto saw_error;
15014 if (!lvalue_p (unfolded_lhs1))
15015 lhs1 = non_lvalue (lhs1);
15017 if (structured_block)
15019 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15020 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15022 done:
15023 if (unfolded_lhs && unfolded_lhs1
15024 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15026 error ("%<#pragma omp atomic capture%> uses two different "
15027 "expressions for memory");
15028 stmt = error_mark_node;
15030 else
15031 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15032 swapped, seq_cst);
15033 if (stmt != error_mark_node)
15034 add_stmt (stmt);
15036 if (!structured_block)
15037 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15041 /* OpenMP 2.5:
15042 # pragma omp barrier new-line
15045 static void
15046 c_parser_omp_barrier (c_parser *parser)
15048 location_t loc = c_parser_peek_token (parser)->location;
15049 c_parser_consume_pragma (parser);
15050 c_parser_skip_to_pragma_eol (parser);
15052 c_finish_omp_barrier (loc);
15055 /* OpenMP 2.5:
15056 # pragma omp critical [(name)] new-line
15057 structured-block
15059 OpenMP 4.5:
15060 # pragma omp critical [(name) [hint(expression)]] new-line
15062 LOC is the location of the #pragma itself. */
15064 #define OMP_CRITICAL_CLAUSE_MASK \
15065 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15067 static tree
15068 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15070 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15072 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15074 c_parser_consume_token (parser);
15075 if (c_parser_next_token_is (parser, CPP_NAME))
15077 name = c_parser_peek_token (parser)->value;
15078 c_parser_consume_token (parser);
15079 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15081 else
15082 c_parser_error (parser, "expected identifier");
15084 clauses = c_parser_omp_all_clauses (parser,
15085 OMP_CRITICAL_CLAUSE_MASK,
15086 "#pragma omp critical");
15088 else
15090 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15091 c_parser_error (parser, "expected %<(%> or end of line");
15092 c_parser_skip_to_pragma_eol (parser);
15095 stmt = c_parser_omp_structured_block (parser, if_p);
15096 return c_finish_omp_critical (loc, stmt, name, clauses);
15099 /* OpenMP 2.5:
15100 # pragma omp flush flush-vars[opt] new-line
15102 flush-vars:
15103 ( variable-list ) */
15105 static void
15106 c_parser_omp_flush (c_parser *parser)
15108 location_t loc = c_parser_peek_token (parser)->location;
15109 c_parser_consume_pragma (parser);
15110 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15111 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15112 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15113 c_parser_error (parser, "expected %<(%> or end of line");
15114 c_parser_skip_to_pragma_eol (parser);
15116 c_finish_omp_flush (loc);
15119 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15120 The real trick here is to determine the loop control variable early
15121 so that we can push a new decl if necessary to make it private.
15122 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15123 respectively. */
15125 static tree
15126 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15127 tree clauses, tree *cclauses, bool *if_p)
15129 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15130 tree declv, condv, incrv, initv, ret = NULL_TREE;
15131 tree pre_body = NULL_TREE, this_pre_body;
15132 tree ordered_cl = NULL_TREE;
15133 bool fail = false, open_brace_parsed = false;
15134 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15135 location_t for_loc;
15136 bool tiling = false;
15137 vec<tree, va_gc> *for_block = make_tree_vector ();
15139 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15140 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15141 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15142 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15144 tiling = true;
15145 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15147 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15148 && OMP_CLAUSE_ORDERED_EXPR (cl))
15150 ordered_cl = cl;
15151 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15154 if (ordered && ordered < collapse)
15156 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15157 "%<ordered%> clause parameter is less than %<collapse%>");
15158 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15159 = build_int_cst (NULL_TREE, collapse);
15160 ordered = collapse;
15162 if (ordered)
15164 for (tree *pc = &clauses; *pc; )
15165 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15167 error_at (OMP_CLAUSE_LOCATION (*pc),
15168 "%<linear%> clause may not be specified together "
15169 "with %<ordered%> clause with a parameter");
15170 *pc = OMP_CLAUSE_CHAIN (*pc);
15172 else
15173 pc = &OMP_CLAUSE_CHAIN (*pc);
15176 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15177 count = ordered ? ordered : collapse;
15179 declv = make_tree_vec (count);
15180 initv = make_tree_vec (count);
15181 condv = make_tree_vec (count);
15182 incrv = make_tree_vec (count);
15184 if (code != CILK_FOR
15185 && !c_parser_next_token_is_keyword (parser, RID_FOR))
15187 c_parser_error (parser, "for statement expected");
15188 return NULL;
15190 if (code == CILK_FOR
15191 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
15193 c_parser_error (parser, "_Cilk_for statement expected");
15194 return NULL;
15196 for_loc = c_parser_peek_token (parser)->location;
15197 c_parser_consume_token (parser);
15199 for (i = 0; i < count; i++)
15201 int bracecount = 0;
15203 matching_parens parens;
15204 if (!parens.require_open (parser))
15205 goto pop_scopes;
15207 /* Parse the initialization declaration or expression. */
15208 if (c_parser_next_tokens_start_declaration (parser))
15210 if (i > 0)
15211 vec_safe_push (for_block, c_begin_compound_stmt (true));
15212 this_pre_body = push_stmt_list ();
15213 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15214 NULL, vNULL);
15215 if (this_pre_body)
15217 this_pre_body = pop_stmt_list (this_pre_body);
15218 if (pre_body)
15220 tree t = pre_body;
15221 pre_body = push_stmt_list ();
15222 add_stmt (t);
15223 add_stmt (this_pre_body);
15224 pre_body = pop_stmt_list (pre_body);
15226 else
15227 pre_body = this_pre_body;
15229 decl = check_for_loop_decls (for_loc, flag_isoc99);
15230 if (decl == NULL)
15231 goto error_init;
15232 if (DECL_INITIAL (decl) == error_mark_node)
15233 decl = error_mark_node;
15234 init = decl;
15236 else if (c_parser_next_token_is (parser, CPP_NAME)
15237 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15239 struct c_expr decl_exp;
15240 struct c_expr init_exp;
15241 location_t init_loc;
15243 decl_exp = c_parser_postfix_expression (parser);
15244 decl = decl_exp.value;
15246 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15248 init_loc = c_parser_peek_token (parser)->location;
15249 init_exp = c_parser_expr_no_commas (parser, NULL);
15250 init_exp = default_function_array_read_conversion (init_loc,
15251 init_exp);
15252 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15253 NOP_EXPR, init_loc, init_exp.value,
15254 init_exp.original_type);
15255 init = c_process_expr_stmt (init_loc, init);
15257 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15259 else
15261 error_init:
15262 c_parser_error (parser,
15263 "expected iteration declaration or initialization");
15264 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15265 "expected %<)%>");
15266 fail = true;
15267 goto parse_next;
15270 /* Parse the loop condition. */
15271 cond = NULL_TREE;
15272 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15274 location_t cond_loc = c_parser_peek_token (parser)->location;
15275 struct c_expr cond_expr
15276 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15278 cond = cond_expr.value;
15279 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15280 if (COMPARISON_CLASS_P (cond))
15282 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15283 op0 = c_fully_fold (op0, false, NULL);
15284 op1 = c_fully_fold (op1, false, NULL);
15285 TREE_OPERAND (cond, 0) = op0;
15286 TREE_OPERAND (cond, 1) = op1;
15288 switch (cond_expr.original_code)
15290 case GT_EXPR:
15291 case GE_EXPR:
15292 case LT_EXPR:
15293 case LE_EXPR:
15294 break;
15295 case NE_EXPR:
15296 if (code == CILK_SIMD || code == CILK_FOR)
15297 break;
15298 /* FALLTHRU. */
15299 default:
15300 /* Can't be cond = error_mark_node, because we want to preserve
15301 the location until c_finish_omp_for. */
15302 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15303 break;
15305 protected_set_expr_location (cond, cond_loc);
15307 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15309 /* Parse the increment expression. */
15310 incr = NULL_TREE;
15311 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15313 location_t incr_loc = c_parser_peek_token (parser)->location;
15315 incr = c_process_expr_stmt (incr_loc,
15316 c_parser_expression (parser).value);
15318 parens.skip_until_found_close (parser);
15320 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15321 fail = true;
15322 else
15324 TREE_VEC_ELT (declv, i) = decl;
15325 TREE_VEC_ELT (initv, i) = init;
15326 TREE_VEC_ELT (condv, i) = cond;
15327 TREE_VEC_ELT (incrv, i) = incr;
15330 parse_next:
15331 if (i == count - 1)
15332 break;
15334 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15335 in between the collapsed for loops to be still considered perfectly
15336 nested. Hopefully the final version clarifies this.
15337 For now handle (multiple) {'s and empty statements. */
15340 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15342 c_parser_consume_token (parser);
15343 break;
15345 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15347 c_parser_consume_token (parser);
15348 bracecount++;
15350 else if (bracecount
15351 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15352 c_parser_consume_token (parser);
15353 else
15355 c_parser_error (parser, "not enough perfectly nested loops");
15356 if (bracecount)
15358 open_brace_parsed = true;
15359 bracecount--;
15361 fail = true;
15362 count = 0;
15363 break;
15366 while (1);
15368 nbraces += bracecount;
15371 if (nbraces)
15372 if_p = NULL;
15374 save_break = c_break_label;
15375 if (code == CILK_SIMD)
15376 c_break_label = build_int_cst (size_type_node, 2);
15377 else
15378 c_break_label = size_one_node;
15379 save_cont = c_cont_label;
15380 c_cont_label = NULL_TREE;
15381 body = push_stmt_list ();
15383 if (open_brace_parsed)
15385 location_t here = c_parser_peek_token (parser)->location;
15386 stmt = c_begin_compound_stmt (true);
15387 c_parser_compound_statement_nostart (parser);
15388 add_stmt (c_end_compound_stmt (here, stmt, true));
15390 else
15391 add_stmt (c_parser_c99_block_statement (parser, if_p));
15392 if (c_cont_label)
15394 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15395 SET_EXPR_LOCATION (t, loc);
15396 add_stmt (t);
15399 body = pop_stmt_list (body);
15400 c_break_label = save_break;
15401 c_cont_label = save_cont;
15403 while (nbraces)
15405 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15407 c_parser_consume_token (parser);
15408 nbraces--;
15410 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15411 c_parser_consume_token (parser);
15412 else
15414 c_parser_error (parser, "collapsed loops not perfectly nested");
15415 while (nbraces)
15417 location_t here = c_parser_peek_token (parser)->location;
15418 stmt = c_begin_compound_stmt (true);
15419 add_stmt (body);
15420 c_parser_compound_statement_nostart (parser);
15421 body = c_end_compound_stmt (here, stmt, true);
15422 nbraces--;
15424 goto pop_scopes;
15428 /* Only bother calling c_finish_omp_for if we haven't already generated
15429 an error from the initialization parsing. */
15430 if (!fail)
15432 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15433 incrv, body, pre_body);
15435 /* Check for iterators appearing in lb, b or incr expressions. */
15436 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15437 stmt = NULL_TREE;
15439 if (stmt)
15441 add_stmt (stmt);
15443 if (cclauses != NULL
15444 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15446 tree *c;
15447 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15448 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15449 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15450 c = &OMP_CLAUSE_CHAIN (*c);
15451 else
15453 for (i = 0; i < count; i++)
15454 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15455 break;
15456 if (i == count)
15457 c = &OMP_CLAUSE_CHAIN (*c);
15458 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15460 error_at (loc,
15461 "iteration variable %qD should not be firstprivate",
15462 OMP_CLAUSE_DECL (*c));
15463 *c = OMP_CLAUSE_CHAIN (*c);
15465 else
15467 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15468 tree l = *c;
15469 *c = OMP_CLAUSE_CHAIN (*c);
15470 if (code == OMP_SIMD)
15472 OMP_CLAUSE_CHAIN (l)
15473 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15474 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15476 else
15478 OMP_CLAUSE_CHAIN (l) = clauses;
15479 clauses = l;
15484 OMP_FOR_CLAUSES (stmt) = clauses;
15486 ret = stmt;
15488 pop_scopes:
15489 while (!for_block->is_empty ())
15491 /* FIXME diagnostics: LOC below should be the actual location of
15492 this particular for block. We need to build a list of
15493 locations to go along with FOR_BLOCK. */
15494 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15495 add_stmt (stmt);
15497 release_tree_vector (for_block);
15498 return ret;
15501 /* Helper function for OpenMP parsing, split clauses and call
15502 finish_omp_clauses on each of the set of clauses afterwards. */
15504 static void
15505 omp_split_clauses (location_t loc, enum tree_code code,
15506 omp_clause_mask mask, tree clauses, tree *cclauses)
15508 int i;
15509 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15510 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15511 if (cclauses[i])
15512 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15515 /* OpenMP 4.0:
15516 #pragma omp simd simd-clause[optseq] new-line
15517 for-loop
15519 LOC is the location of the #pragma token.
15522 #define OMP_SIMD_CLAUSE_MASK \
15523 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15532 static tree
15533 c_parser_omp_simd (location_t loc, c_parser *parser,
15534 char *p_name, omp_clause_mask mask, tree *cclauses,
15535 bool *if_p)
15537 tree block, clauses, ret;
15539 strcat (p_name, " simd");
15540 mask |= OMP_SIMD_CLAUSE_MASK;
15542 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15543 if (cclauses)
15545 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15546 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15547 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15548 OMP_CLAUSE_ORDERED);
15549 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15551 error_at (OMP_CLAUSE_LOCATION (c),
15552 "%<ordered%> clause with parameter may not be specified "
15553 "on %qs construct", p_name);
15554 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15558 block = c_begin_compound_stmt (true);
15559 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15560 block = c_end_compound_stmt (loc, block, true);
15561 add_stmt (block);
15563 return ret;
15566 /* OpenMP 2.5:
15567 #pragma omp for for-clause[optseq] new-line
15568 for-loop
15570 OpenMP 4.0:
15571 #pragma omp for simd for-simd-clause[optseq] new-line
15572 for-loop
15574 LOC is the location of the #pragma token.
15577 #define OMP_FOR_CLAUSE_MASK \
15578 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15588 static tree
15589 c_parser_omp_for (location_t loc, c_parser *parser,
15590 char *p_name, omp_clause_mask mask, tree *cclauses,
15591 bool *if_p)
15593 tree block, clauses, ret;
15595 strcat (p_name, " for");
15596 mask |= OMP_FOR_CLAUSE_MASK;
15597 /* parallel for{, simd} disallows nowait clause, but for
15598 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15599 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15600 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15601 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15602 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15603 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15605 if (c_parser_next_token_is (parser, CPP_NAME))
15607 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15609 if (strcmp (p, "simd") == 0)
15611 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15612 if (cclauses == NULL)
15613 cclauses = cclauses_buf;
15615 c_parser_consume_token (parser);
15616 if (!flag_openmp) /* flag_openmp_simd */
15617 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15618 if_p);
15619 block = c_begin_compound_stmt (true);
15620 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15621 block = c_end_compound_stmt (loc, block, true);
15622 if (ret == NULL_TREE)
15623 return ret;
15624 ret = make_node (OMP_FOR);
15625 TREE_TYPE (ret) = void_type_node;
15626 OMP_FOR_BODY (ret) = block;
15627 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15628 SET_EXPR_LOCATION (ret, loc);
15629 add_stmt (ret);
15630 return ret;
15633 if (!flag_openmp) /* flag_openmp_simd */
15635 c_parser_skip_to_pragma_eol (parser, false);
15636 return NULL_TREE;
15639 /* Composite distribute parallel for disallows linear clause. */
15640 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15641 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15643 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15644 if (cclauses)
15646 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15647 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15650 block = c_begin_compound_stmt (true);
15651 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15652 block = c_end_compound_stmt (loc, block, true);
15653 add_stmt (block);
15655 return ret;
15658 /* OpenMP 2.5:
15659 # pragma omp master new-line
15660 structured-block
15662 LOC is the location of the #pragma token.
15665 static tree
15666 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15668 c_parser_skip_to_pragma_eol (parser);
15669 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15670 if_p));
15673 /* OpenMP 2.5:
15674 # pragma omp ordered new-line
15675 structured-block
15677 OpenMP 4.5:
15678 # pragma omp ordered ordered-clauses new-line
15679 structured-block
15681 # pragma omp ordered depend-clauses new-line */
15683 #define OMP_ORDERED_CLAUSE_MASK \
15684 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15687 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15688 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15690 static bool
15691 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15692 bool *if_p)
15694 location_t loc = c_parser_peek_token (parser)->location;
15695 c_parser_consume_pragma (parser);
15697 if (context != pragma_stmt && context != pragma_compound)
15699 c_parser_error (parser, "expected declaration specifiers");
15700 c_parser_skip_to_pragma_eol (parser, false);
15701 return false;
15704 if (c_parser_next_token_is (parser, CPP_NAME))
15706 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15708 if (!strcmp ("depend", p))
15710 if (!flag_openmp) /* flag_openmp_simd */
15712 c_parser_skip_to_pragma_eol (parser, false);
15713 return false;
15715 if (context == pragma_stmt)
15717 error_at (loc,
15718 "%<#pragma omp ordered%> with %<depend%> clause may "
15719 "only be used in compound statements");
15720 c_parser_skip_to_pragma_eol (parser, false);
15721 return false;
15724 tree clauses
15725 = c_parser_omp_all_clauses (parser,
15726 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15727 "#pragma omp ordered");
15728 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15729 return false;
15733 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15734 "#pragma omp ordered");
15736 if (!flag_openmp /* flag_openmp_simd */
15737 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
15738 return false;
15740 c_finish_omp_ordered (loc, clauses,
15741 c_parser_omp_structured_block (parser, if_p));
15742 return true;
15745 /* OpenMP 2.5:
15747 section-scope:
15748 { section-sequence }
15750 section-sequence:
15751 section-directive[opt] structured-block
15752 section-sequence section-directive structured-block
15754 SECTIONS_LOC is the location of the #pragma omp sections. */
15756 static tree
15757 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15759 tree stmt, substmt;
15760 bool error_suppress = false;
15761 location_t loc;
15763 loc = c_parser_peek_token (parser)->location;
15764 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15766 /* Avoid skipping until the end of the block. */
15767 parser->error = false;
15768 return NULL_TREE;
15771 stmt = push_stmt_list ();
15773 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15775 substmt = c_parser_omp_structured_block (parser, NULL);
15776 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15777 SET_EXPR_LOCATION (substmt, loc);
15778 add_stmt (substmt);
15781 while (1)
15783 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15784 break;
15785 if (c_parser_next_token_is (parser, CPP_EOF))
15786 break;
15788 loc = c_parser_peek_token (parser)->location;
15789 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15791 c_parser_consume_pragma (parser);
15792 c_parser_skip_to_pragma_eol (parser);
15793 error_suppress = false;
15795 else if (!error_suppress)
15797 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15798 error_suppress = true;
15801 substmt = c_parser_omp_structured_block (parser, NULL);
15802 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15803 SET_EXPR_LOCATION (substmt, loc);
15804 add_stmt (substmt);
15806 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15807 "expected %<#pragma omp section%> or %<}%>");
15809 substmt = pop_stmt_list (stmt);
15811 stmt = make_node (OMP_SECTIONS);
15812 SET_EXPR_LOCATION (stmt, sections_loc);
15813 TREE_TYPE (stmt) = void_type_node;
15814 OMP_SECTIONS_BODY (stmt) = substmt;
15816 return add_stmt (stmt);
15819 /* OpenMP 2.5:
15820 # pragma omp sections sections-clause[optseq] newline
15821 sections-scope
15823 LOC is the location of the #pragma token.
15826 #define OMP_SECTIONS_CLAUSE_MASK \
15827 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15833 static tree
15834 c_parser_omp_sections (location_t loc, c_parser *parser,
15835 char *p_name, omp_clause_mask mask, tree *cclauses)
15837 tree block, clauses, ret;
15839 strcat (p_name, " sections");
15840 mask |= OMP_SECTIONS_CLAUSE_MASK;
15841 if (cclauses)
15842 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15844 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15845 if (cclauses)
15847 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15848 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15851 block = c_begin_compound_stmt (true);
15852 ret = c_parser_omp_sections_scope (loc, parser);
15853 if (ret)
15854 OMP_SECTIONS_CLAUSES (ret) = clauses;
15855 block = c_end_compound_stmt (loc, block, true);
15856 add_stmt (block);
15858 return ret;
15861 /* OpenMP 2.5:
15862 # pragma omp parallel parallel-clause[optseq] new-line
15863 structured-block
15864 # pragma omp parallel for parallel-for-clause[optseq] new-line
15865 structured-block
15866 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15867 structured-block
15869 OpenMP 4.0:
15870 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15871 structured-block
15873 LOC is the location of the #pragma token.
15876 #define OMP_PARALLEL_CLAUSE_MASK \
15877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15887 static tree
15888 c_parser_omp_parallel (location_t loc, c_parser *parser,
15889 char *p_name, omp_clause_mask mask, tree *cclauses,
15890 bool *if_p)
15892 tree stmt, clauses, block;
15894 strcat (p_name, " parallel");
15895 mask |= OMP_PARALLEL_CLAUSE_MASK;
15896 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15897 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15898 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15899 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15901 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15903 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15904 if (cclauses == NULL)
15905 cclauses = cclauses_buf;
15907 c_parser_consume_token (parser);
15908 if (!flag_openmp) /* flag_openmp_simd */
15909 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15910 block = c_begin_omp_parallel ();
15911 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15912 stmt
15913 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15914 block);
15915 if (ret == NULL_TREE)
15916 return ret;
15917 OMP_PARALLEL_COMBINED (stmt) = 1;
15918 return stmt;
15920 /* When combined with distribute, parallel has to be followed by for.
15921 #pragma omp target parallel is allowed though. */
15922 else if (cclauses
15923 && (mask & (OMP_CLAUSE_MASK_1
15924 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15926 error_at (loc, "expected %<for%> after %qs", p_name);
15927 c_parser_skip_to_pragma_eol (parser);
15928 return NULL_TREE;
15930 else if (!flag_openmp) /* flag_openmp_simd */
15932 c_parser_skip_to_pragma_eol (parser, false);
15933 return NULL_TREE;
15935 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15937 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15938 if (strcmp (p, "sections") == 0)
15940 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15941 if (cclauses == NULL)
15942 cclauses = cclauses_buf;
15944 c_parser_consume_token (parser);
15945 block = c_begin_omp_parallel ();
15946 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15947 stmt = c_finish_omp_parallel (loc,
15948 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15949 block);
15950 OMP_PARALLEL_COMBINED (stmt) = 1;
15951 return stmt;
15955 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15956 if (cclauses)
15958 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15959 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15962 block = c_begin_omp_parallel ();
15963 c_parser_statement (parser, if_p);
15964 stmt = c_finish_omp_parallel (loc, clauses, block);
15966 return stmt;
15969 /* OpenMP 2.5:
15970 # pragma omp single single-clause[optseq] new-line
15971 structured-block
15973 LOC is the location of the #pragma.
15976 #define OMP_SINGLE_CLAUSE_MASK \
15977 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15982 static tree
15983 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15985 tree stmt = make_node (OMP_SINGLE);
15986 SET_EXPR_LOCATION (stmt, loc);
15987 TREE_TYPE (stmt) = void_type_node;
15989 OMP_SINGLE_CLAUSES (stmt)
15990 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15991 "#pragma omp single");
15992 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15994 return add_stmt (stmt);
15997 /* OpenMP 3.0:
15998 # pragma omp task task-clause[optseq] new-line
16000 LOC is the location of the #pragma.
16003 #define OMP_TASK_CLAUSE_MASK \
16004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16015 static tree
16016 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16018 tree clauses, block;
16020 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16021 "#pragma omp task");
16023 block = c_begin_omp_task ();
16024 c_parser_statement (parser, if_p);
16025 return c_finish_omp_task (loc, clauses, block);
16028 /* OpenMP 3.0:
16029 # pragma omp taskwait new-line
16032 static void
16033 c_parser_omp_taskwait (c_parser *parser)
16035 location_t loc = c_parser_peek_token (parser)->location;
16036 c_parser_consume_pragma (parser);
16037 c_parser_skip_to_pragma_eol (parser);
16039 c_finish_omp_taskwait (loc);
16042 /* OpenMP 3.1:
16043 # pragma omp taskyield new-line
16046 static void
16047 c_parser_omp_taskyield (c_parser *parser)
16049 location_t loc = c_parser_peek_token (parser)->location;
16050 c_parser_consume_pragma (parser);
16051 c_parser_skip_to_pragma_eol (parser);
16053 c_finish_omp_taskyield (loc);
16056 /* OpenMP 4.0:
16057 # pragma omp taskgroup new-line
16060 static tree
16061 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16063 location_t loc = c_parser_peek_token (parser)->location;
16064 c_parser_skip_to_pragma_eol (parser);
16065 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16066 if_p));
16069 /* OpenMP 4.0:
16070 # pragma omp cancel cancel-clause[optseq] new-line
16072 LOC is the location of the #pragma.
16075 #define OMP_CANCEL_CLAUSE_MASK \
16076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16082 static void
16083 c_parser_omp_cancel (c_parser *parser)
16085 location_t loc = c_parser_peek_token (parser)->location;
16087 c_parser_consume_pragma (parser);
16088 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16089 "#pragma omp cancel");
16091 c_finish_omp_cancel (loc, clauses);
16094 /* OpenMP 4.0:
16095 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16097 LOC is the location of the #pragma.
16100 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16101 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16106 static void
16107 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16109 location_t loc = c_parser_peek_token (parser)->location;
16110 tree clauses;
16111 bool point_seen = false;
16113 c_parser_consume_pragma (parser);
16114 if (c_parser_next_token_is (parser, CPP_NAME))
16116 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16117 if (strcmp (p, "point") == 0)
16119 c_parser_consume_token (parser);
16120 point_seen = true;
16123 if (!point_seen)
16125 c_parser_error (parser, "expected %<point%>");
16126 c_parser_skip_to_pragma_eol (parser);
16127 return;
16130 if (context != pragma_compound)
16132 if (context == pragma_stmt)
16133 error_at (loc,
16134 "%<#pragma %s%> may only be used in compound statements",
16135 "omp cancellation point");
16136 else
16137 c_parser_error (parser, "expected declaration specifiers");
16138 c_parser_skip_to_pragma_eol (parser, false);
16139 return;
16142 clauses
16143 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16144 "#pragma omp cancellation point");
16146 c_finish_omp_cancellation_point (loc, clauses);
16149 /* OpenMP 4.0:
16150 #pragma omp distribute distribute-clause[optseq] new-line
16151 for-loop */
16153 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16160 static tree
16161 c_parser_omp_distribute (location_t loc, c_parser *parser,
16162 char *p_name, omp_clause_mask mask, tree *cclauses,
16163 bool *if_p)
16165 tree clauses, block, ret;
16167 strcat (p_name, " distribute");
16168 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16170 if (c_parser_next_token_is (parser, CPP_NAME))
16172 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16173 bool simd = false;
16174 bool parallel = false;
16176 if (strcmp (p, "simd") == 0)
16177 simd = true;
16178 else
16179 parallel = strcmp (p, "parallel") == 0;
16180 if (parallel || simd)
16182 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16183 if (cclauses == NULL)
16184 cclauses = cclauses_buf;
16185 c_parser_consume_token (parser);
16186 if (!flag_openmp) /* flag_openmp_simd */
16188 if (simd)
16189 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16190 if_p);
16191 else
16192 return c_parser_omp_parallel (loc, parser, p_name, mask,
16193 cclauses, if_p);
16195 block = c_begin_compound_stmt (true);
16196 if (simd)
16197 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16198 if_p);
16199 else
16200 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16201 if_p);
16202 block = c_end_compound_stmt (loc, block, true);
16203 if (ret == NULL)
16204 return ret;
16205 ret = make_node (OMP_DISTRIBUTE);
16206 TREE_TYPE (ret) = void_type_node;
16207 OMP_FOR_BODY (ret) = block;
16208 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16209 SET_EXPR_LOCATION (ret, loc);
16210 add_stmt (ret);
16211 return ret;
16214 if (!flag_openmp) /* flag_openmp_simd */
16216 c_parser_skip_to_pragma_eol (parser, false);
16217 return NULL_TREE;
16220 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16221 if (cclauses)
16223 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16224 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16227 block = c_begin_compound_stmt (true);
16228 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16229 if_p);
16230 block = c_end_compound_stmt (loc, block, true);
16231 add_stmt (block);
16233 return ret;
16236 /* OpenMP 4.0:
16237 # pragma omp teams teams-clause[optseq] new-line
16238 structured-block */
16240 #define OMP_TEAMS_CLAUSE_MASK \
16241 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16247 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16249 static tree
16250 c_parser_omp_teams (location_t loc, c_parser *parser,
16251 char *p_name, omp_clause_mask mask, tree *cclauses,
16252 bool *if_p)
16254 tree clauses, block, ret;
16256 strcat (p_name, " teams");
16257 mask |= OMP_TEAMS_CLAUSE_MASK;
16259 if (c_parser_next_token_is (parser, CPP_NAME))
16261 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16262 if (strcmp (p, "distribute") == 0)
16264 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16265 if (cclauses == NULL)
16266 cclauses = cclauses_buf;
16268 c_parser_consume_token (parser);
16269 if (!flag_openmp) /* flag_openmp_simd */
16270 return c_parser_omp_distribute (loc, parser, p_name, mask,
16271 cclauses, if_p);
16272 block = c_begin_compound_stmt (true);
16273 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16274 if_p);
16275 block = c_end_compound_stmt (loc, block, true);
16276 if (ret == NULL)
16277 return ret;
16278 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16279 ret = make_node (OMP_TEAMS);
16280 TREE_TYPE (ret) = void_type_node;
16281 OMP_TEAMS_CLAUSES (ret) = clauses;
16282 OMP_TEAMS_BODY (ret) = block;
16283 OMP_TEAMS_COMBINED (ret) = 1;
16284 return add_stmt (ret);
16287 if (!flag_openmp) /* flag_openmp_simd */
16289 c_parser_skip_to_pragma_eol (parser, false);
16290 return NULL_TREE;
16293 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16294 if (cclauses)
16296 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16297 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16300 tree stmt = make_node (OMP_TEAMS);
16301 TREE_TYPE (stmt) = void_type_node;
16302 OMP_TEAMS_CLAUSES (stmt) = clauses;
16303 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16305 return add_stmt (stmt);
16308 /* OpenMP 4.0:
16309 # pragma omp target data target-data-clause[optseq] new-line
16310 structured-block */
16312 #define OMP_TARGET_DATA_CLAUSE_MASK \
16313 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16318 static tree
16319 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16321 tree clauses
16322 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16323 "#pragma omp target data");
16324 int map_seen = 0;
16325 for (tree *pc = &clauses; *pc;)
16327 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16328 switch (OMP_CLAUSE_MAP_KIND (*pc))
16330 case GOMP_MAP_TO:
16331 case GOMP_MAP_ALWAYS_TO:
16332 case GOMP_MAP_FROM:
16333 case GOMP_MAP_ALWAYS_FROM:
16334 case GOMP_MAP_TOFROM:
16335 case GOMP_MAP_ALWAYS_TOFROM:
16336 case GOMP_MAP_ALLOC:
16337 map_seen = 3;
16338 break;
16339 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16340 case GOMP_MAP_ALWAYS_POINTER:
16341 break;
16342 default:
16343 map_seen |= 1;
16344 error_at (OMP_CLAUSE_LOCATION (*pc),
16345 "%<#pragma omp target data%> with map-type other "
16346 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16347 "on %<map%> clause");
16348 *pc = OMP_CLAUSE_CHAIN (*pc);
16349 continue;
16351 pc = &OMP_CLAUSE_CHAIN (*pc);
16354 if (map_seen != 3)
16356 if (map_seen == 0)
16357 error_at (loc,
16358 "%<#pragma omp target data%> must contain at least "
16359 "one %<map%> clause");
16360 return NULL_TREE;
16363 tree stmt = make_node (OMP_TARGET_DATA);
16364 TREE_TYPE (stmt) = void_type_node;
16365 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16366 keep_next_level ();
16367 tree block = c_begin_compound_stmt (true);
16368 add_stmt (c_parser_omp_structured_block (parser, if_p));
16369 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16371 SET_EXPR_LOCATION (stmt, loc);
16372 return add_stmt (stmt);
16375 /* OpenMP 4.0:
16376 # pragma omp target update target-update-clause[optseq] new-line */
16378 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16379 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16386 static bool
16387 c_parser_omp_target_update (location_t loc, c_parser *parser,
16388 enum pragma_context context)
16390 if (context == pragma_stmt)
16392 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16393 "omp target update");
16394 c_parser_skip_to_pragma_eol (parser, false);
16395 return false;
16398 tree clauses
16399 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16400 "#pragma omp target update");
16401 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16402 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16404 error_at (loc,
16405 "%<#pragma omp target update%> must contain at least one "
16406 "%<from%> or %<to%> clauses");
16407 return false;
16410 tree stmt = make_node (OMP_TARGET_UPDATE);
16411 TREE_TYPE (stmt) = void_type_node;
16412 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16413 SET_EXPR_LOCATION (stmt, loc);
16414 add_stmt (stmt);
16415 return false;
16418 /* OpenMP 4.5:
16419 # pragma omp target enter data target-data-clause[optseq] new-line */
16421 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16422 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16428 static tree
16429 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16430 enum pragma_context context)
16432 bool data_seen = false;
16433 if (c_parser_next_token_is (parser, CPP_NAME))
16435 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16436 if (strcmp (p, "data") == 0)
16438 c_parser_consume_token (parser);
16439 data_seen = true;
16442 if (!data_seen)
16444 c_parser_error (parser, "expected %<data%>");
16445 c_parser_skip_to_pragma_eol (parser);
16446 return NULL_TREE;
16449 if (context == pragma_stmt)
16451 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16452 "omp target enter data");
16453 c_parser_skip_to_pragma_eol (parser, false);
16454 return NULL_TREE;
16457 tree clauses
16458 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16459 "#pragma omp target enter data");
16460 int map_seen = 0;
16461 for (tree *pc = &clauses; *pc;)
16463 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16464 switch (OMP_CLAUSE_MAP_KIND (*pc))
16466 case GOMP_MAP_TO:
16467 case GOMP_MAP_ALWAYS_TO:
16468 case GOMP_MAP_ALLOC:
16469 map_seen = 3;
16470 break;
16471 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16472 case GOMP_MAP_ALWAYS_POINTER:
16473 break;
16474 default:
16475 map_seen |= 1;
16476 error_at (OMP_CLAUSE_LOCATION (*pc),
16477 "%<#pragma omp target enter data%> with map-type other "
16478 "than %<to%> or %<alloc%> on %<map%> clause");
16479 *pc = OMP_CLAUSE_CHAIN (*pc);
16480 continue;
16482 pc = &OMP_CLAUSE_CHAIN (*pc);
16485 if (map_seen != 3)
16487 if (map_seen == 0)
16488 error_at (loc,
16489 "%<#pragma omp target enter data%> must contain at least "
16490 "one %<map%> clause");
16491 return NULL_TREE;
16494 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16495 TREE_TYPE (stmt) = void_type_node;
16496 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16497 SET_EXPR_LOCATION (stmt, loc);
16498 add_stmt (stmt);
16499 return stmt;
16502 /* OpenMP 4.5:
16503 # pragma omp target exit data target-data-clause[optseq] new-line */
16505 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16506 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16512 static tree
16513 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16514 enum pragma_context context)
16516 bool data_seen = false;
16517 if (c_parser_next_token_is (parser, CPP_NAME))
16519 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16520 if (strcmp (p, "data") == 0)
16522 c_parser_consume_token (parser);
16523 data_seen = true;
16526 if (!data_seen)
16528 c_parser_error (parser, "expected %<data%>");
16529 c_parser_skip_to_pragma_eol (parser);
16530 return NULL_TREE;
16533 if (context == pragma_stmt)
16535 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16536 "omp target exit data");
16537 c_parser_skip_to_pragma_eol (parser, false);
16538 return NULL_TREE;
16541 tree clauses
16542 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16543 "#pragma omp target exit data");
16545 int map_seen = 0;
16546 for (tree *pc = &clauses; *pc;)
16548 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16549 switch (OMP_CLAUSE_MAP_KIND (*pc))
16551 case GOMP_MAP_FROM:
16552 case GOMP_MAP_ALWAYS_FROM:
16553 case GOMP_MAP_RELEASE:
16554 case GOMP_MAP_DELETE:
16555 map_seen = 3;
16556 break;
16557 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16558 case GOMP_MAP_ALWAYS_POINTER:
16559 break;
16560 default:
16561 map_seen |= 1;
16562 error_at (OMP_CLAUSE_LOCATION (*pc),
16563 "%<#pragma omp target exit data%> with map-type other "
16564 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16565 " clause");
16566 *pc = OMP_CLAUSE_CHAIN (*pc);
16567 continue;
16569 pc = &OMP_CLAUSE_CHAIN (*pc);
16572 if (map_seen != 3)
16574 if (map_seen == 0)
16575 error_at (loc,
16576 "%<#pragma omp target exit data%> must contain at least one "
16577 "%<map%> clause");
16578 return NULL_TREE;
16581 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16582 TREE_TYPE (stmt) = void_type_node;
16583 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16584 SET_EXPR_LOCATION (stmt, loc);
16585 add_stmt (stmt);
16586 return stmt;
16589 /* OpenMP 4.0:
16590 # pragma omp target target-clause[optseq] new-line
16591 structured-block */
16593 #define OMP_TARGET_CLAUSE_MASK \
16594 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16604 static bool
16605 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16607 location_t loc = c_parser_peek_token (parser)->location;
16608 c_parser_consume_pragma (parser);
16609 tree *pc = NULL, stmt, block;
16611 if (context != pragma_stmt && context != pragma_compound)
16613 c_parser_error (parser, "expected declaration specifiers");
16614 c_parser_skip_to_pragma_eol (parser);
16615 return false;
16618 if (c_parser_next_token_is (parser, CPP_NAME))
16620 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16621 enum tree_code ccode = ERROR_MARK;
16623 if (strcmp (p, "teams") == 0)
16624 ccode = OMP_TEAMS;
16625 else if (strcmp (p, "parallel") == 0)
16626 ccode = OMP_PARALLEL;
16627 else if (strcmp (p, "simd") == 0)
16628 ccode = OMP_SIMD;
16629 if (ccode != ERROR_MARK)
16631 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16632 char p_name[sizeof ("#pragma omp target teams distribute "
16633 "parallel for simd")];
16635 c_parser_consume_token (parser);
16636 strcpy (p_name, "#pragma omp target");
16637 if (!flag_openmp) /* flag_openmp_simd */
16639 tree stmt;
16640 switch (ccode)
16642 case OMP_TEAMS:
16643 stmt = c_parser_omp_teams (loc, parser, p_name,
16644 OMP_TARGET_CLAUSE_MASK,
16645 cclauses, if_p);
16646 break;
16647 case OMP_PARALLEL:
16648 stmt = c_parser_omp_parallel (loc, parser, p_name,
16649 OMP_TARGET_CLAUSE_MASK,
16650 cclauses, if_p);
16651 break;
16652 case OMP_SIMD:
16653 stmt = c_parser_omp_simd (loc, parser, p_name,
16654 OMP_TARGET_CLAUSE_MASK,
16655 cclauses, if_p);
16656 break;
16657 default:
16658 gcc_unreachable ();
16660 return stmt != NULL_TREE;
16662 keep_next_level ();
16663 tree block = c_begin_compound_stmt (true), ret;
16664 switch (ccode)
16666 case OMP_TEAMS:
16667 ret = c_parser_omp_teams (loc, parser, p_name,
16668 OMP_TARGET_CLAUSE_MASK, cclauses,
16669 if_p);
16670 break;
16671 case OMP_PARALLEL:
16672 ret = c_parser_omp_parallel (loc, parser, p_name,
16673 OMP_TARGET_CLAUSE_MASK, cclauses,
16674 if_p);
16675 break;
16676 case OMP_SIMD:
16677 ret = c_parser_omp_simd (loc, parser, p_name,
16678 OMP_TARGET_CLAUSE_MASK, cclauses,
16679 if_p);
16680 break;
16681 default:
16682 gcc_unreachable ();
16684 block = c_end_compound_stmt (loc, block, true);
16685 if (ret == NULL_TREE)
16686 return false;
16687 if (ccode == OMP_TEAMS)
16689 /* For combined target teams, ensure the num_teams and
16690 thread_limit clause expressions are evaluated on the host,
16691 before entering the target construct. */
16692 tree c;
16693 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16694 c; c = OMP_CLAUSE_CHAIN (c))
16695 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16696 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16697 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16699 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16700 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16701 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16702 expr, NULL_TREE, NULL_TREE);
16703 add_stmt (expr);
16704 OMP_CLAUSE_OPERAND (c, 0) = expr;
16705 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16706 OMP_CLAUSE_FIRSTPRIVATE);
16707 OMP_CLAUSE_DECL (tc) = tmp;
16708 OMP_CLAUSE_CHAIN (tc)
16709 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16710 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16713 tree stmt = make_node (OMP_TARGET);
16714 TREE_TYPE (stmt) = void_type_node;
16715 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16716 OMP_TARGET_BODY (stmt) = block;
16717 OMP_TARGET_COMBINED (stmt) = 1;
16718 add_stmt (stmt);
16719 pc = &OMP_TARGET_CLAUSES (stmt);
16720 goto check_clauses;
16722 else if (!flag_openmp) /* flag_openmp_simd */
16724 c_parser_skip_to_pragma_eol (parser, false);
16725 return false;
16727 else if (strcmp (p, "data") == 0)
16729 c_parser_consume_token (parser);
16730 c_parser_omp_target_data (loc, parser, if_p);
16731 return true;
16733 else if (strcmp (p, "enter") == 0)
16735 c_parser_consume_token (parser);
16736 c_parser_omp_target_enter_data (loc, parser, context);
16737 return false;
16739 else if (strcmp (p, "exit") == 0)
16741 c_parser_consume_token (parser);
16742 c_parser_omp_target_exit_data (loc, parser, context);
16743 return false;
16745 else if (strcmp (p, "update") == 0)
16747 c_parser_consume_token (parser);
16748 return c_parser_omp_target_update (loc, parser, context);
16751 if (!flag_openmp) /* flag_openmp_simd */
16753 c_parser_skip_to_pragma_eol (parser, false);
16754 return false;
16757 stmt = make_node (OMP_TARGET);
16758 TREE_TYPE (stmt) = void_type_node;
16760 OMP_TARGET_CLAUSES (stmt)
16761 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16762 "#pragma omp target");
16763 pc = &OMP_TARGET_CLAUSES (stmt);
16764 keep_next_level ();
16765 block = c_begin_compound_stmt (true);
16766 add_stmt (c_parser_omp_structured_block (parser, if_p));
16767 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16769 SET_EXPR_LOCATION (stmt, loc);
16770 add_stmt (stmt);
16772 check_clauses:
16773 while (*pc)
16775 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16776 switch (OMP_CLAUSE_MAP_KIND (*pc))
16778 case GOMP_MAP_TO:
16779 case GOMP_MAP_ALWAYS_TO:
16780 case GOMP_MAP_FROM:
16781 case GOMP_MAP_ALWAYS_FROM:
16782 case GOMP_MAP_TOFROM:
16783 case GOMP_MAP_ALWAYS_TOFROM:
16784 case GOMP_MAP_ALLOC:
16785 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16786 case GOMP_MAP_ALWAYS_POINTER:
16787 break;
16788 default:
16789 error_at (OMP_CLAUSE_LOCATION (*pc),
16790 "%<#pragma omp target%> with map-type other "
16791 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16792 "on %<map%> clause");
16793 *pc = OMP_CLAUSE_CHAIN (*pc);
16794 continue;
16796 pc = &OMP_CLAUSE_CHAIN (*pc);
16798 return true;
16801 /* OpenMP 4.0:
16802 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16804 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16805 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16812 static void
16813 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16815 auto_vec<c_token> clauses;
16816 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16818 c_token *token = c_parser_peek_token (parser);
16819 if (token->type == CPP_EOF)
16821 c_parser_skip_to_pragma_eol (parser);
16822 return;
16824 clauses.safe_push (*token);
16825 c_parser_consume_token (parser);
16827 clauses.safe_push (*c_parser_peek_token (parser));
16828 c_parser_skip_to_pragma_eol (parser);
16830 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16832 if (c_parser_peek_token (parser)->pragma_kind
16833 != PRAGMA_OMP_DECLARE
16834 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16835 || strcmp (IDENTIFIER_POINTER
16836 (c_parser_peek_2nd_token (parser)->value),
16837 "simd") != 0)
16839 c_parser_error (parser,
16840 "%<#pragma omp declare simd%> must be followed by "
16841 "function declaration or definition or another "
16842 "%<#pragma omp declare simd%>");
16843 return;
16845 c_parser_consume_pragma (parser);
16846 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16848 c_token *token = c_parser_peek_token (parser);
16849 if (token->type == CPP_EOF)
16851 c_parser_skip_to_pragma_eol (parser);
16852 return;
16854 clauses.safe_push (*token);
16855 c_parser_consume_token (parser);
16857 clauses.safe_push (*c_parser_peek_token (parser));
16858 c_parser_skip_to_pragma_eol (parser);
16861 /* Make sure nothing tries to read past the end of the tokens. */
16862 c_token eof_token;
16863 memset (&eof_token, 0, sizeof (eof_token));
16864 eof_token.type = CPP_EOF;
16865 clauses.safe_push (eof_token);
16866 clauses.safe_push (eof_token);
16868 switch (context)
16870 case pragma_external:
16871 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16872 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16874 int ext = disable_extension_diagnostics ();
16876 c_parser_consume_token (parser);
16877 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16878 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16879 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16880 NULL, clauses);
16881 restore_extension_diagnostics (ext);
16883 else
16884 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16885 NULL, clauses);
16886 break;
16887 case pragma_struct:
16888 case pragma_param:
16889 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16890 "function declaration or definition");
16891 break;
16892 case pragma_compound:
16893 case pragma_stmt:
16894 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16895 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16897 int ext = disable_extension_diagnostics ();
16899 c_parser_consume_token (parser);
16900 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16901 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16902 if (c_parser_next_tokens_start_declaration (parser))
16904 c_parser_declaration_or_fndef (parser, true, true, true, true,
16905 true, NULL, clauses);
16906 restore_extension_diagnostics (ext);
16907 break;
16909 restore_extension_diagnostics (ext);
16911 else if (c_parser_next_tokens_start_declaration (parser))
16913 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16914 NULL, clauses);
16915 break;
16917 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16918 "function declaration or definition");
16919 break;
16920 default:
16921 gcc_unreachable ();
16925 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16926 and put that into "omp declare simd" attribute. */
16928 static void
16929 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16930 vec<c_token> clauses)
16932 if (flag_cilkplus
16933 && (clauses.exists ()
16934 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16935 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16937 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16938 "used in the same function marked as a Cilk Plus SIMD-enabled "
16939 "function");
16940 vec_free (parser->cilk_simd_fn_tokens);
16941 return;
16944 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16945 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16946 has already processed the tokens. */
16947 if (clauses.exists () && clauses[0].type == CPP_EOF)
16948 return;
16949 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16951 error ("%<#pragma omp declare simd%> not immediately followed by "
16952 "a function declaration or definition");
16953 clauses[0].type = CPP_EOF;
16954 return;
16956 if (clauses.exists () && clauses[0].type != CPP_NAME)
16958 error_at (DECL_SOURCE_LOCATION (fndecl),
16959 "%<#pragma omp declare simd%> not immediately followed by "
16960 "a single function declaration or definition");
16961 clauses[0].type = CPP_EOF;
16962 return;
16965 if (parms == NULL_TREE)
16966 parms = DECL_ARGUMENTS (fndecl);
16968 unsigned int tokens_avail = parser->tokens_avail;
16969 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16970 bool is_cilkplus_cilk_simd_fn = false;
16972 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16974 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16975 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16976 is_cilkplus_cilk_simd_fn = true;
16978 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16980 error_at (DECL_SOURCE_LOCATION (fndecl),
16981 "%<__simd__%> attribute cannot be used in the same "
16982 "function marked as a Cilk Plus SIMD-enabled function");
16983 vec_free (parser->cilk_simd_fn_tokens);
16984 return;
16988 else
16990 parser->tokens = clauses.address ();
16991 parser->tokens_avail = clauses.length ();
16994 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16995 while (parser->tokens_avail > 3)
16997 c_token *token = c_parser_peek_token (parser);
16998 if (!is_cilkplus_cilk_simd_fn)
16999 gcc_assert (token->type == CPP_NAME
17000 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17001 else
17002 gcc_assert (token->type == CPP_NAME
17003 && is_cilkplus_vector_p (token->value));
17004 c_parser_consume_token (parser);
17005 parser->in_pragma = true;
17007 tree c = NULL_TREE;
17008 if (is_cilkplus_cilk_simd_fn)
17009 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
17010 "SIMD-enabled functions attribute");
17011 else
17012 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17013 "#pragma omp declare simd");
17014 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17015 if (c != NULL_TREE)
17016 c = tree_cons (NULL_TREE, c, NULL_TREE);
17017 if (is_cilkplus_cilk_simd_fn)
17019 tree k = build_tree_list (get_identifier ("cilk simd function"),
17020 NULL_TREE);
17021 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
17022 DECL_ATTRIBUTES (fndecl) = k;
17024 c = build_tree_list (get_identifier ("omp declare simd"), c);
17025 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17026 DECL_ATTRIBUTES (fndecl) = c;
17029 parser->tokens = &parser->tokens_buf[0];
17030 parser->tokens_avail = tokens_avail;
17031 if (clauses.exists ())
17032 clauses[0].type = CPP_PRAGMA;
17034 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
17035 vec_free (parser->cilk_simd_fn_tokens);
17039 /* OpenMP 4.0:
17040 # pragma omp declare target new-line
17041 declarations and definitions
17042 # pragma omp end declare target new-line
17044 OpenMP 4.5:
17045 # pragma omp declare target ( extended-list ) new-line
17047 # pragma omp declare target declare-target-clauses[seq] new-line */
17049 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17053 static void
17054 c_parser_omp_declare_target (c_parser *parser)
17056 location_t loc = c_parser_peek_token (parser)->location;
17057 tree clauses = NULL_TREE;
17058 if (c_parser_next_token_is (parser, CPP_NAME))
17059 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17060 "#pragma omp declare target");
17061 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17063 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17064 clauses);
17065 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17066 c_parser_skip_to_pragma_eol (parser);
17068 else
17070 c_parser_skip_to_pragma_eol (parser);
17071 current_omp_declare_target_attribute++;
17072 return;
17074 if (current_omp_declare_target_attribute)
17075 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17076 "%<#pragma omp declare target%> without clauses and "
17077 "%<#pragma omp end declare target%>");
17078 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17080 tree t = OMP_CLAUSE_DECL (c), id;
17081 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17082 tree at2 = lookup_attribute ("omp declare target link",
17083 DECL_ATTRIBUTES (t));
17084 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17086 id = get_identifier ("omp declare target link");
17087 std::swap (at1, at2);
17089 else
17090 id = get_identifier ("omp declare target");
17091 if (at2)
17093 error_at (OMP_CLAUSE_LOCATION (c),
17094 "%qD specified both in declare target %<link%> and %<to%>"
17095 " clauses", t);
17096 continue;
17098 if (!at1)
17100 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17101 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17102 continue;
17104 symtab_node *node = symtab_node::get (t);
17105 if (node != NULL)
17107 node->offloadable = 1;
17108 if (ENABLE_OFFLOADING)
17110 g->have_offload = true;
17111 if (is_a <varpool_node *> (node))
17112 vec_safe_push (offload_vars, t);
17119 static void
17120 c_parser_omp_end_declare_target (c_parser *parser)
17122 location_t loc = c_parser_peek_token (parser)->location;
17123 c_parser_consume_pragma (parser);
17124 if (c_parser_next_token_is (parser, CPP_NAME)
17125 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17126 "declare") == 0)
17128 c_parser_consume_token (parser);
17129 if (c_parser_next_token_is (parser, CPP_NAME)
17130 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17131 "target") == 0)
17132 c_parser_consume_token (parser);
17133 else
17135 c_parser_error (parser, "expected %<target%>");
17136 c_parser_skip_to_pragma_eol (parser);
17137 return;
17140 else
17142 c_parser_error (parser, "expected %<declare%>");
17143 c_parser_skip_to_pragma_eol (parser);
17144 return;
17146 c_parser_skip_to_pragma_eol (parser);
17147 if (!current_omp_declare_target_attribute)
17148 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17149 "%<#pragma omp declare target%>");
17150 else
17151 current_omp_declare_target_attribute--;
17155 /* OpenMP 4.0
17156 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17157 initializer-clause[opt] new-line
17159 initializer-clause:
17160 initializer (omp_priv = initializer)
17161 initializer (function-name (argument-list)) */
17163 static void
17164 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17166 unsigned int tokens_avail = 0, i;
17167 vec<tree> types = vNULL;
17168 vec<c_token> clauses = vNULL;
17169 enum tree_code reduc_code = ERROR_MARK;
17170 tree reduc_id = NULL_TREE;
17171 tree type;
17172 location_t rloc = c_parser_peek_token (parser)->location;
17174 if (context == pragma_struct || context == pragma_param)
17176 error ("%<#pragma omp declare reduction%> not at file or block scope");
17177 goto fail;
17180 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17181 goto fail;
17183 switch (c_parser_peek_token (parser)->type)
17185 case CPP_PLUS:
17186 reduc_code = PLUS_EXPR;
17187 break;
17188 case CPP_MULT:
17189 reduc_code = MULT_EXPR;
17190 break;
17191 case CPP_MINUS:
17192 reduc_code = MINUS_EXPR;
17193 break;
17194 case CPP_AND:
17195 reduc_code = BIT_AND_EXPR;
17196 break;
17197 case CPP_XOR:
17198 reduc_code = BIT_XOR_EXPR;
17199 break;
17200 case CPP_OR:
17201 reduc_code = BIT_IOR_EXPR;
17202 break;
17203 case CPP_AND_AND:
17204 reduc_code = TRUTH_ANDIF_EXPR;
17205 break;
17206 case CPP_OR_OR:
17207 reduc_code = TRUTH_ORIF_EXPR;
17208 break;
17209 case CPP_NAME:
17210 const char *p;
17211 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17212 if (strcmp (p, "min") == 0)
17214 reduc_code = MIN_EXPR;
17215 break;
17217 if (strcmp (p, "max") == 0)
17219 reduc_code = MAX_EXPR;
17220 break;
17222 reduc_id = c_parser_peek_token (parser)->value;
17223 break;
17224 default:
17225 c_parser_error (parser,
17226 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17227 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17228 goto fail;
17231 tree orig_reduc_id, reduc_decl;
17232 orig_reduc_id = reduc_id;
17233 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17234 reduc_decl = c_omp_reduction_decl (reduc_id);
17235 c_parser_consume_token (parser);
17237 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17238 goto fail;
17240 while (true)
17242 location_t loc = c_parser_peek_token (parser)->location;
17243 struct c_type_name *ctype = c_parser_type_name (parser);
17244 if (ctype != NULL)
17246 type = groktypename (ctype, NULL, NULL);
17247 if (type == error_mark_node)
17249 else if ((INTEGRAL_TYPE_P (type)
17250 || TREE_CODE (type) == REAL_TYPE
17251 || TREE_CODE (type) == COMPLEX_TYPE)
17252 && orig_reduc_id == NULL_TREE)
17253 error_at (loc, "predeclared arithmetic type in "
17254 "%<#pragma omp declare reduction%>");
17255 else if (TREE_CODE (type) == FUNCTION_TYPE
17256 || TREE_CODE (type) == ARRAY_TYPE)
17257 error_at (loc, "function or array type in "
17258 "%<#pragma omp declare reduction%>");
17259 else if (TYPE_ATOMIC (type))
17260 error_at (loc, "%<_Atomic%> qualified type in "
17261 "%<#pragma omp declare reduction%>");
17262 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17263 error_at (loc, "const, volatile or restrict qualified type in "
17264 "%<#pragma omp declare reduction%>");
17265 else
17267 tree t;
17268 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17269 if (comptypes (TREE_PURPOSE (t), type))
17271 error_at (loc, "redeclaration of %qs "
17272 "%<#pragma omp declare reduction%> for "
17273 "type %qT",
17274 IDENTIFIER_POINTER (reduc_id)
17275 + sizeof ("omp declare reduction ") - 1,
17276 type);
17277 location_t ploc
17278 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17279 0));
17280 error_at (ploc, "previous %<#pragma omp declare "
17281 "reduction%>");
17282 break;
17284 if (t == NULL_TREE)
17285 types.safe_push (type);
17287 if (c_parser_next_token_is (parser, CPP_COMMA))
17288 c_parser_consume_token (parser);
17289 else
17290 break;
17292 else
17293 break;
17296 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17297 || types.is_empty ())
17299 fail:
17300 clauses.release ();
17301 types.release ();
17302 while (true)
17304 c_token *token = c_parser_peek_token (parser);
17305 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17306 break;
17307 c_parser_consume_token (parser);
17309 c_parser_skip_to_pragma_eol (parser);
17310 return;
17313 if (types.length () > 1)
17315 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17317 c_token *token = c_parser_peek_token (parser);
17318 if (token->type == CPP_EOF)
17319 goto fail;
17320 clauses.safe_push (*token);
17321 c_parser_consume_token (parser);
17323 clauses.safe_push (*c_parser_peek_token (parser));
17324 c_parser_skip_to_pragma_eol (parser);
17326 /* Make sure nothing tries to read past the end of the tokens. */
17327 c_token eof_token;
17328 memset (&eof_token, 0, sizeof (eof_token));
17329 eof_token.type = CPP_EOF;
17330 clauses.safe_push (eof_token);
17331 clauses.safe_push (eof_token);
17334 int errs = errorcount;
17335 FOR_EACH_VEC_ELT (types, i, type)
17337 tokens_avail = parser->tokens_avail;
17338 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17339 if (!clauses.is_empty ())
17341 parser->tokens = clauses.address ();
17342 parser->tokens_avail = clauses.length ();
17343 parser->in_pragma = true;
17346 bool nested = current_function_decl != NULL_TREE;
17347 if (nested)
17348 c_push_function_context ();
17349 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17350 reduc_id, default_function_type);
17351 current_function_decl = fndecl;
17352 allocate_struct_function (fndecl, true);
17353 push_scope ();
17354 tree stmt = push_stmt_list ();
17355 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17356 warn about these. */
17357 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17358 get_identifier ("omp_out"), type);
17359 DECL_ARTIFICIAL (omp_out) = 1;
17360 DECL_CONTEXT (omp_out) = fndecl;
17361 pushdecl (omp_out);
17362 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17363 get_identifier ("omp_in"), type);
17364 DECL_ARTIFICIAL (omp_in) = 1;
17365 DECL_CONTEXT (omp_in) = fndecl;
17366 pushdecl (omp_in);
17367 struct c_expr combiner = c_parser_expression (parser);
17368 struct c_expr initializer;
17369 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17370 bool bad = false;
17371 initializer.value = error_mark_node;
17372 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17373 bad = true;
17374 else if (c_parser_next_token_is (parser, CPP_NAME)
17375 && strcmp (IDENTIFIER_POINTER
17376 (c_parser_peek_token (parser)->value),
17377 "initializer") == 0)
17379 c_parser_consume_token (parser);
17380 pop_scope ();
17381 push_scope ();
17382 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17383 get_identifier ("omp_priv"), type);
17384 DECL_ARTIFICIAL (omp_priv) = 1;
17385 DECL_INITIAL (omp_priv) = error_mark_node;
17386 DECL_CONTEXT (omp_priv) = fndecl;
17387 pushdecl (omp_priv);
17388 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17389 get_identifier ("omp_orig"), type);
17390 DECL_ARTIFICIAL (omp_orig) = 1;
17391 DECL_CONTEXT (omp_orig) = fndecl;
17392 pushdecl (omp_orig);
17393 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17394 bad = true;
17395 else if (!c_parser_next_token_is (parser, CPP_NAME))
17397 c_parser_error (parser, "expected %<omp_priv%> or "
17398 "function-name");
17399 bad = true;
17401 else if (strcmp (IDENTIFIER_POINTER
17402 (c_parser_peek_token (parser)->value),
17403 "omp_priv") != 0)
17405 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17406 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17408 c_parser_error (parser, "expected function-name %<(%>");
17409 bad = true;
17411 else
17412 initializer = c_parser_postfix_expression (parser);
17413 if (initializer.value
17414 && TREE_CODE (initializer.value) == CALL_EXPR)
17416 int j;
17417 tree c = initializer.value;
17418 for (j = 0; j < call_expr_nargs (c); j++)
17420 tree a = CALL_EXPR_ARG (c, j);
17421 STRIP_NOPS (a);
17422 if (TREE_CODE (a) == ADDR_EXPR
17423 && TREE_OPERAND (a, 0) == omp_priv)
17424 break;
17426 if (j == call_expr_nargs (c))
17427 error ("one of the initializer call arguments should be "
17428 "%<&omp_priv%>");
17431 else
17433 c_parser_consume_token (parser);
17434 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17435 bad = true;
17436 else
17438 tree st = push_stmt_list ();
17439 location_t loc = c_parser_peek_token (parser)->location;
17440 rich_location richloc (line_table, loc);
17441 start_init (omp_priv, NULL_TREE, 0, &richloc);
17442 struct c_expr init = c_parser_initializer (parser);
17443 finish_init ();
17444 finish_decl (omp_priv, loc, init.value,
17445 init.original_type, NULL_TREE);
17446 pop_stmt_list (st);
17449 if (!bad
17450 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17451 bad = true;
17454 if (!bad)
17456 c_parser_skip_to_pragma_eol (parser);
17458 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17459 DECL_INITIAL (reduc_decl));
17460 DECL_INITIAL (reduc_decl) = t;
17461 DECL_SOURCE_LOCATION (omp_out) = rloc;
17462 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17463 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17464 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17465 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17466 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17467 if (omp_priv)
17469 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17470 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17471 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17472 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17473 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17474 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17475 walk_tree (&DECL_INITIAL (omp_priv),
17476 c_check_omp_declare_reduction_r,
17477 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17481 pop_stmt_list (stmt);
17482 pop_scope ();
17483 if (cfun->language != NULL)
17485 ggc_free (cfun->language);
17486 cfun->language = NULL;
17488 set_cfun (NULL);
17489 current_function_decl = NULL_TREE;
17490 if (nested)
17491 c_pop_function_context ();
17493 if (!clauses.is_empty ())
17495 parser->tokens = &parser->tokens_buf[0];
17496 parser->tokens_avail = tokens_avail;
17498 if (bad)
17499 goto fail;
17500 if (errs != errorcount)
17501 break;
17504 clauses.release ();
17505 types.release ();
17509 /* OpenMP 4.0
17510 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17511 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17512 initializer-clause[opt] new-line
17513 #pragma omp declare target new-line */
17515 static void
17516 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17518 c_parser_consume_pragma (parser);
17519 if (c_parser_next_token_is (parser, CPP_NAME))
17521 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17522 if (strcmp (p, "simd") == 0)
17524 /* c_parser_consume_token (parser); done in
17525 c_parser_omp_declare_simd. */
17526 c_parser_omp_declare_simd (parser, context);
17527 return;
17529 if (strcmp (p, "reduction") == 0)
17531 c_parser_consume_token (parser);
17532 c_parser_omp_declare_reduction (parser, context);
17533 return;
17535 if (!flag_openmp) /* flag_openmp_simd */
17537 c_parser_skip_to_pragma_eol (parser, false);
17538 return;
17540 if (strcmp (p, "target") == 0)
17542 c_parser_consume_token (parser);
17543 c_parser_omp_declare_target (parser);
17544 return;
17548 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17549 "or %<target%>");
17550 c_parser_skip_to_pragma_eol (parser);
17553 /* OpenMP 4.5:
17554 #pragma omp taskloop taskloop-clause[optseq] new-line
17555 for-loop
17557 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17558 for-loop */
17560 #define OMP_TASKLOOP_CLAUSE_MASK \
17561 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17576 static tree
17577 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17578 char *p_name, omp_clause_mask mask, tree *cclauses,
17579 bool *if_p)
17581 tree clauses, block, ret;
17583 strcat (p_name, " taskloop");
17584 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17586 if (c_parser_next_token_is (parser, CPP_NAME))
17588 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17590 if (strcmp (p, "simd") == 0)
17592 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17593 if (cclauses == NULL)
17594 cclauses = cclauses_buf;
17595 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17596 c_parser_consume_token (parser);
17597 if (!flag_openmp) /* flag_openmp_simd */
17598 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17599 if_p);
17600 block = c_begin_compound_stmt (true);
17601 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17602 block = c_end_compound_stmt (loc, block, true);
17603 if (ret == NULL)
17604 return ret;
17605 ret = make_node (OMP_TASKLOOP);
17606 TREE_TYPE (ret) = void_type_node;
17607 OMP_FOR_BODY (ret) = block;
17608 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17609 SET_EXPR_LOCATION (ret, loc);
17610 add_stmt (ret);
17611 return ret;
17614 if (!flag_openmp) /* flag_openmp_simd */
17616 c_parser_skip_to_pragma_eol (parser, false);
17617 return NULL_TREE;
17620 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17621 if (cclauses)
17623 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17624 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17627 block = c_begin_compound_stmt (true);
17628 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17629 block = c_end_compound_stmt (loc, block, true);
17630 add_stmt (block);
17632 return ret;
17635 /* Main entry point to parsing most OpenMP pragmas. */
17637 static void
17638 c_parser_omp_construct (c_parser *parser, bool *if_p)
17640 enum pragma_kind p_kind;
17641 location_t loc;
17642 tree stmt;
17643 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17644 omp_clause_mask mask (0);
17646 loc = c_parser_peek_token (parser)->location;
17647 p_kind = c_parser_peek_token (parser)->pragma_kind;
17648 c_parser_consume_pragma (parser);
17650 switch (p_kind)
17652 case PRAGMA_OACC_ATOMIC:
17653 c_parser_omp_atomic (loc, parser);
17654 return;
17655 case PRAGMA_OACC_CACHE:
17656 strcpy (p_name, "#pragma acc");
17657 stmt = c_parser_oacc_cache (loc, parser);
17658 break;
17659 case PRAGMA_OACC_DATA:
17660 stmt = c_parser_oacc_data (loc, parser, if_p);
17661 break;
17662 case PRAGMA_OACC_HOST_DATA:
17663 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17664 break;
17665 case PRAGMA_OACC_KERNELS:
17666 case PRAGMA_OACC_PARALLEL:
17667 strcpy (p_name, "#pragma acc");
17668 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17669 if_p);
17670 break;
17671 case PRAGMA_OACC_LOOP:
17672 strcpy (p_name, "#pragma acc");
17673 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17674 break;
17675 case PRAGMA_OACC_WAIT:
17676 strcpy (p_name, "#pragma wait");
17677 stmt = c_parser_oacc_wait (loc, parser, p_name);
17678 break;
17679 case PRAGMA_OMP_ATOMIC:
17680 c_parser_omp_atomic (loc, parser);
17681 return;
17682 case PRAGMA_OMP_CRITICAL:
17683 stmt = c_parser_omp_critical (loc, parser, if_p);
17684 break;
17685 case PRAGMA_OMP_DISTRIBUTE:
17686 strcpy (p_name, "#pragma omp");
17687 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17688 break;
17689 case PRAGMA_OMP_FOR:
17690 strcpy (p_name, "#pragma omp");
17691 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17692 break;
17693 case PRAGMA_OMP_MASTER:
17694 stmt = c_parser_omp_master (loc, parser, if_p);
17695 break;
17696 case PRAGMA_OMP_PARALLEL:
17697 strcpy (p_name, "#pragma omp");
17698 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17699 break;
17700 case PRAGMA_OMP_SECTIONS:
17701 strcpy (p_name, "#pragma omp");
17702 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17703 break;
17704 case PRAGMA_OMP_SIMD:
17705 strcpy (p_name, "#pragma omp");
17706 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17707 break;
17708 case PRAGMA_OMP_SINGLE:
17709 stmt = c_parser_omp_single (loc, parser, if_p);
17710 break;
17711 case PRAGMA_OMP_TASK:
17712 stmt = c_parser_omp_task (loc, parser, if_p);
17713 break;
17714 case PRAGMA_OMP_TASKGROUP:
17715 stmt = c_parser_omp_taskgroup (parser, if_p);
17716 break;
17717 case PRAGMA_OMP_TASKLOOP:
17718 strcpy (p_name, "#pragma omp");
17719 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17720 break;
17721 case PRAGMA_OMP_TEAMS:
17722 strcpy (p_name, "#pragma omp");
17723 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17724 break;
17725 default:
17726 gcc_unreachable ();
17729 if (stmt)
17730 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17734 /* OpenMP 2.5:
17735 # pragma omp threadprivate (variable-list) */
17737 static void
17738 c_parser_omp_threadprivate (c_parser *parser)
17740 tree vars, t;
17741 location_t loc;
17743 c_parser_consume_pragma (parser);
17744 loc = c_parser_peek_token (parser)->location;
17745 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17747 /* Mark every variable in VARS to be assigned thread local storage. */
17748 for (t = vars; t; t = TREE_CHAIN (t))
17750 tree v = TREE_PURPOSE (t);
17752 /* FIXME diagnostics: Ideally we should keep individual
17753 locations for all the variables in the var list to make the
17754 following errors more precise. Perhaps
17755 c_parser_omp_var_list_parens() should construct a list of
17756 locations to go along with the var list. */
17758 /* If V had already been marked threadprivate, it doesn't matter
17759 whether it had been used prior to this point. */
17760 if (!VAR_P (v))
17761 error_at (loc, "%qD is not a variable", v);
17762 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17763 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17764 else if (! is_global_var (v))
17765 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17766 else if (TREE_TYPE (v) == error_mark_node)
17768 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17769 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17770 else
17772 if (! DECL_THREAD_LOCAL_P (v))
17774 set_decl_tls_model (v, decl_default_tls_model (v));
17775 /* If rtl has been already set for this var, call
17776 make_decl_rtl once again, so that encode_section_info
17777 has a chance to look at the new decl flags. */
17778 if (DECL_RTL_SET_P (v))
17779 make_decl_rtl (v);
17781 C_DECL_THREADPRIVATE_P (v) = 1;
17785 c_parser_skip_to_pragma_eol (parser);
17788 /* Cilk Plus <#pragma simd> parsing routines. */
17790 /* Helper function for c_parser_pragma. Perform some sanity checking
17791 for <#pragma simd> constructs. Returns FALSE if there was a
17792 problem. */
17794 static bool
17795 c_parser_cilk_verify_simd (c_parser *parser,
17796 enum pragma_context context)
17798 if (!flag_cilkplus)
17800 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17801 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17802 return false;
17804 if (context == pragma_external)
17806 c_parser_error (parser,"pragma simd must be inside a function");
17807 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17808 return false;
17810 return true;
17813 /* Cilk Plus:
17814 This function is shared by SIMD-enabled functions and #pragma simd.
17815 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17816 CLAUSES is unused. The main purpose of this function is to parse a
17817 vectorlength attribute or clause and check for parse errors.
17818 When IS_SIMD_FN is true then the function is merely caching the tokens
17819 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17820 cache is cleared since there is no reason to continue.
17821 Syntax:
17822 vectorlength ( constant-expression ) */
17824 static tree
17825 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17826 bool is_simd_fn)
17828 if (is_simd_fn)
17829 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17830 else
17831 /* The vectorlength clause behaves exactly like OpenMP's safelen
17832 clause. Represent it in OpenMP terms. */
17833 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17835 matching_parens parens;
17836 if (!parens.require_open (parser))
17837 return clauses;
17839 location_t loc = c_parser_peek_token (parser)->location;
17840 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17841 expr = c_fully_fold (expr, false, NULL);
17843 /* If expr is an error_mark_node then the above function would have
17844 emitted an error. No reason to do it twice. */
17845 if (expr == error_mark_node)
17847 else if (!TREE_TYPE (expr)
17848 || !TREE_CONSTANT (expr)
17849 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17851 error_at (loc, "vectorlength must be an integer constant");
17852 else if (wi::exact_log2 (wi::to_wide (expr)) == -1)
17853 error_at (loc, "vectorlength must be a power of 2");
17854 else
17856 if (is_simd_fn)
17858 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17859 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17860 OMP_CLAUSE_CHAIN (u) = clauses;
17861 clauses = u;
17863 else
17865 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17866 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17867 OMP_CLAUSE_CHAIN (u) = clauses;
17868 clauses = u;
17872 parens.require_close (parser);
17874 return clauses;
17877 /* Cilk Plus:
17878 linear ( simd-linear-variable-list )
17880 simd-linear-variable-list:
17881 simd-linear-variable
17882 simd-linear-variable-list , simd-linear-variable
17884 simd-linear-variable:
17885 id-expression
17886 id-expression : simd-linear-step
17888 simd-linear-step:
17889 conditional-expression */
17891 static tree
17892 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17894 matching_parens parens;
17895 if (!parens.require_open (parser))
17896 return clauses;
17898 location_t loc = c_parser_peek_token (parser)->location;
17900 if (c_parser_next_token_is_not (parser, CPP_NAME)
17901 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17902 c_parser_error (parser, "expected identifier");
17904 while (c_parser_next_token_is (parser, CPP_NAME)
17905 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17907 tree var = lookup_name (c_parser_peek_token (parser)->value);
17909 if (var == NULL)
17911 undeclared_variable (c_parser_peek_token (parser)->location,
17912 c_parser_peek_token (parser)->value);
17913 c_parser_consume_token (parser);
17915 else if (var == error_mark_node)
17916 c_parser_consume_token (parser);
17917 else
17919 tree step = integer_one_node;
17921 /* Parse the linear step if present. */
17922 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17924 c_parser_consume_token (parser);
17925 c_parser_consume_token (parser);
17927 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17928 expr = c_fully_fold (expr, false, NULL);
17930 if (TREE_TYPE (expr)
17931 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17932 && (TREE_CONSTANT (expr)
17933 || DECL_P (expr)))
17934 step = expr;
17935 else
17936 c_parser_error (parser,
17937 "step size must be an integer constant "
17938 "expression or an integer variable");
17940 else
17941 c_parser_consume_token (parser);
17943 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17944 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17945 OMP_CLAUSE_DECL (u) = var;
17946 OMP_CLAUSE_LINEAR_STEP (u) = step;
17947 OMP_CLAUSE_CHAIN (u) = clauses;
17948 clauses = u;
17951 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17952 break;
17954 c_parser_consume_token (parser);
17957 parens.skip_until_found_close (parser);
17959 return clauses;
17962 /* Returns the name of the next clause. If the clause is not
17963 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17964 not consumed. Otherwise, the appropriate pragma_simd_clause is
17965 returned and the token is consumed. */
17967 static pragma_omp_clause
17968 c_parser_cilk_clause_name (c_parser *parser)
17970 pragma_omp_clause result;
17971 c_token *token = c_parser_peek_token (parser);
17973 if (!token->value || token->type != CPP_NAME)
17974 return PRAGMA_CILK_CLAUSE_NONE;
17976 const char *p = IDENTIFIER_POINTER (token->value);
17978 if (!strcmp (p, "vectorlength"))
17979 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17980 else if (!strcmp (p, "linear"))
17981 result = PRAGMA_CILK_CLAUSE_LINEAR;
17982 else if (!strcmp (p, "private"))
17983 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17984 else if (!strcmp (p, "firstprivate"))
17985 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17986 else if (!strcmp (p, "lastprivate"))
17987 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17988 else if (!strcmp (p, "reduction"))
17989 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17990 else
17991 return PRAGMA_CILK_CLAUSE_NONE;
17993 c_parser_consume_token (parser);
17994 return result;
17997 /* Parse all #<pragma simd> clauses. Return the list of clauses
17998 found. */
18000 static tree
18001 c_parser_cilk_all_clauses (c_parser *parser)
18003 tree clauses = NULL;
18005 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18007 pragma_omp_clause c_kind;
18009 c_kind = c_parser_cilk_clause_name (parser);
18011 switch (c_kind)
18013 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
18014 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
18015 break;
18016 case PRAGMA_CILK_CLAUSE_LINEAR:
18017 clauses = c_parser_cilk_clause_linear (parser, clauses);
18018 break;
18019 case PRAGMA_CILK_CLAUSE_PRIVATE:
18020 /* Use the OpenMP counterpart. */
18021 clauses = c_parser_omp_clause_private (parser, clauses);
18022 break;
18023 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
18024 /* Use the OpenMP counterpart. */
18025 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
18026 break;
18027 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
18028 /* Use the OpenMP counterpart. */
18029 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
18030 break;
18031 case PRAGMA_CILK_CLAUSE_REDUCTION:
18032 /* Use the OpenMP counterpart. */
18033 clauses = c_parser_omp_clause_reduction (parser, clauses);
18034 break;
18035 default:
18036 c_parser_error (parser, "expected %<#pragma simd%> clause");
18037 goto saw_error;
18041 saw_error:
18042 c_parser_skip_to_pragma_eol (parser);
18043 return c_finish_omp_clauses (clauses, C_ORT_CILK);
18046 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
18047 Here is the correct syntax of this pragma:
18048 #pragma cilk grainsize = <EXP>
18051 static void
18052 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
18054 extern tree convert_to_integer (tree, tree);
18056 /* consume the 'grainsize' keyword. */
18057 c_parser_consume_pragma (parser);
18059 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
18061 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
18062 if (g_expr.value == error_mark_node)
18064 c_parser_skip_to_pragma_eol (parser);
18065 return;
18067 tree grain = convert_to_integer (long_integer_type_node,
18068 c_fully_fold (g_expr.value, false,
18069 NULL));
18070 c_parser_skip_to_pragma_eol (parser);
18071 c_token *token = c_parser_peek_token (parser);
18072 if (token && token->type == CPP_KEYWORD
18073 && token->keyword == RID_CILK_FOR)
18075 if (grain == NULL_TREE || grain == error_mark_node)
18076 grain = integer_zero_node;
18077 c_parser_cilk_for (parser, grain, if_p);
18079 else
18080 warning (0, "%<#pragma cilk grainsize%> is not followed by "
18081 "%<_Cilk_for%>");
18083 else
18084 c_parser_skip_to_pragma_eol (parser);
18087 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
18089 static void
18090 c_parser_cilk_simd (c_parser *parser, bool *if_p)
18092 tree clauses = c_parser_cilk_all_clauses (parser);
18093 tree block = c_begin_compound_stmt (true);
18094 location_t loc = c_parser_peek_token (parser)->location;
18095 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
18096 block = c_end_compound_stmt (loc, block, true);
18097 add_stmt (block);
18100 /* Create an artificial decl with TYPE and emit initialization of it with
18101 INIT. */
18103 static tree
18104 c_get_temp_regvar (tree type, tree init)
18106 location_t loc = EXPR_LOCATION (init);
18107 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
18108 DECL_ARTIFICIAL (decl) = 1;
18109 DECL_IGNORED_P (decl) = 1;
18110 pushdecl (decl);
18111 tree t = build2 (INIT_EXPR, type, decl, init);
18112 add_stmt (t);
18113 return decl;
18116 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
18117 GRAIN is the grain value passed in through pragma or 0. */
18119 static void
18120 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
18122 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
18123 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
18124 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
18125 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
18127 tree block = c_begin_compound_stmt (true);
18128 tree sb = push_stmt_list ();
18129 location_t loc = c_parser_peek_token (parser)->location;
18130 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
18131 if_p);
18132 sb = pop_stmt_list (sb);
18134 if (omp_for)
18136 tree omp_par = make_node (OMP_PARALLEL);
18137 TREE_TYPE (omp_par) = void_type_node;
18138 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
18139 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
18140 TREE_SIDE_EFFECTS (bind) = 1;
18141 BIND_EXPR_BODY (bind) = sb;
18142 OMP_PARALLEL_BODY (omp_par) = bind;
18143 if (OMP_FOR_PRE_BODY (omp_for))
18145 add_stmt (OMP_FOR_PRE_BODY (omp_for));
18146 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
18148 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
18149 tree decl = TREE_OPERAND (init, 0);
18150 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
18151 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
18152 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
18153 if (TREE_CODE (t) != INTEGER_CST)
18155 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
18156 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18157 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
18158 OMP_CLAUSE_CHAIN (c) = clauses;
18159 clauses = c;
18161 if (TREE_CODE (incr) == MODIFY_EXPR)
18163 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
18164 if (TREE_CODE (t) != INTEGER_CST)
18166 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
18167 = c_get_temp_regvar (TREE_TYPE (t), t);
18168 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18169 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
18170 OMP_CLAUSE_CHAIN (c) = clauses;
18171 clauses = c;
18174 t = TREE_OPERAND (init, 1);
18175 if (TREE_CODE (t) != INTEGER_CST)
18177 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
18178 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18179 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
18180 OMP_CLAUSE_CHAIN (c) = clauses;
18181 clauses = c;
18183 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
18184 OMP_CLAUSE_DECL (c) = decl;
18185 OMP_CLAUSE_CHAIN (c) = clauses;
18186 clauses = c;
18187 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
18188 OMP_CLAUSE_OPERAND (c, 0)
18189 = cilk_for_number_of_iterations (omp_for);
18190 OMP_CLAUSE_CHAIN (c) = clauses;
18191 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
18192 add_stmt (omp_par);
18195 block = c_end_compound_stmt (loc, block, true);
18196 add_stmt (block);
18200 /* Parse a transaction attribute (GCC Extension).
18202 transaction-attribute:
18203 attributes
18204 [ [ any-word ] ]
18206 The transactional memory language description is written for C++,
18207 and uses the C++0x attribute syntax. For compatibility, allow the
18208 bracket style for transactions in C as well. */
18210 static tree
18211 c_parser_transaction_attributes (c_parser *parser)
18213 tree attr_name, attr = NULL;
18215 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18216 return c_parser_attributes (parser);
18218 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18219 return NULL_TREE;
18220 c_parser_consume_token (parser);
18221 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18222 goto error1;
18224 attr_name = c_parser_attribute_any_word (parser);
18225 if (attr_name)
18227 c_parser_consume_token (parser);
18228 attr = build_tree_list (attr_name, NULL_TREE);
18230 else
18231 c_parser_error (parser, "expected identifier");
18233 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18234 error1:
18235 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18236 return attr;
18239 /* Parse a __transaction_atomic or __transaction_relaxed statement
18240 (GCC Extension).
18242 transaction-statement:
18243 __transaction_atomic transaction-attribute[opt] compound-statement
18244 __transaction_relaxed compound-statement
18246 Note that the only valid attribute is: "outer".
18249 static tree
18250 c_parser_transaction (c_parser *parser, enum rid keyword)
18252 unsigned int old_in = parser->in_transaction;
18253 unsigned int this_in = 1, new_in;
18254 location_t loc = c_parser_peek_token (parser)->location;
18255 tree stmt, attrs;
18257 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18258 || keyword == RID_TRANSACTION_RELAXED)
18259 && c_parser_next_token_is_keyword (parser, keyword));
18260 c_parser_consume_token (parser);
18262 if (keyword == RID_TRANSACTION_RELAXED)
18263 this_in |= TM_STMT_ATTR_RELAXED;
18264 else
18266 attrs = c_parser_transaction_attributes (parser);
18267 if (attrs)
18268 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18271 /* Keep track if we're in the lexical scope of an outer transaction. */
18272 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18274 parser->in_transaction = new_in;
18275 stmt = c_parser_compound_statement (parser);
18276 parser->in_transaction = old_in;
18278 if (flag_tm)
18279 stmt = c_finish_transaction (loc, stmt, this_in);
18280 else
18281 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18282 "%<__transaction_atomic%> without transactional memory support enabled"
18283 : "%<__transaction_relaxed %> "
18284 "without transactional memory support enabled"));
18286 return stmt;
18289 /* Parse a __transaction_atomic or __transaction_relaxed expression
18290 (GCC Extension).
18292 transaction-expression:
18293 __transaction_atomic ( expression )
18294 __transaction_relaxed ( expression )
18297 static struct c_expr
18298 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18300 struct c_expr ret;
18301 unsigned int old_in = parser->in_transaction;
18302 unsigned int this_in = 1;
18303 location_t loc = c_parser_peek_token (parser)->location;
18304 tree attrs;
18306 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18307 || keyword == RID_TRANSACTION_RELAXED)
18308 && c_parser_next_token_is_keyword (parser, keyword));
18309 c_parser_consume_token (parser);
18311 if (keyword == RID_TRANSACTION_RELAXED)
18312 this_in |= TM_STMT_ATTR_RELAXED;
18313 else
18315 attrs = c_parser_transaction_attributes (parser);
18316 if (attrs)
18317 this_in |= parse_tm_stmt_attr (attrs, 0);
18320 parser->in_transaction = this_in;
18321 matching_parens parens;
18322 if (parens.require_open (parser))
18324 tree expr = c_parser_expression (parser).value;
18325 ret.original_type = TREE_TYPE (expr);
18326 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18327 if (this_in & TM_STMT_ATTR_RELAXED)
18328 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18329 SET_EXPR_LOCATION (ret.value, loc);
18330 ret.original_code = TRANSACTION_EXPR;
18331 if (!parens.require_close (parser))
18333 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18334 goto error;
18337 else
18339 error:
18340 ret.value = error_mark_node;
18341 ret.original_code = ERROR_MARK;
18342 ret.original_type = NULL;
18344 parser->in_transaction = old_in;
18346 if (!flag_tm)
18347 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18348 "%<__transaction_atomic%> without transactional memory support enabled"
18349 : "%<__transaction_relaxed %> "
18350 "without transactional memory support enabled"));
18352 set_c_expr_source_range (&ret, loc, loc);
18354 return ret;
18357 /* Parse a __transaction_cancel statement (GCC Extension).
18359 transaction-cancel-statement:
18360 __transaction_cancel transaction-attribute[opt] ;
18362 Note that the only valid attribute is "outer".
18365 static tree
18366 c_parser_transaction_cancel (c_parser *parser)
18368 location_t loc = c_parser_peek_token (parser)->location;
18369 tree attrs;
18370 bool is_outer = false;
18372 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18373 c_parser_consume_token (parser);
18375 attrs = c_parser_transaction_attributes (parser);
18376 if (attrs)
18377 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18379 if (!flag_tm)
18381 error_at (loc, "%<__transaction_cancel%> without "
18382 "transactional memory support enabled");
18383 goto ret_error;
18385 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18387 error_at (loc, "%<__transaction_cancel%> within a "
18388 "%<__transaction_relaxed%>");
18389 goto ret_error;
18391 else if (is_outer)
18393 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18394 && !is_tm_may_cancel_outer (current_function_decl))
18396 error_at (loc, "outer %<__transaction_cancel%> not "
18397 "within outer %<__transaction_atomic%>");
18398 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18399 goto ret_error;
18402 else if (parser->in_transaction == 0)
18404 error_at (loc, "%<__transaction_cancel%> not within "
18405 "%<__transaction_atomic%>");
18406 goto ret_error;
18409 return add_stmt (build_tm_abort_call (loc, is_outer));
18411 ret_error:
18412 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18415 /* Parse a single source file. */
18417 void
18418 c_parse_file (void)
18420 /* Use local storage to begin. If the first token is a pragma, parse it.
18421 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18422 which will cause garbage collection. */
18423 c_parser tparser;
18425 memset (&tparser, 0, sizeof tparser);
18426 tparser.tokens = &tparser.tokens_buf[0];
18427 the_parser = &tparser;
18429 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18430 c_parser_pragma_pch_preprocess (&tparser);
18432 the_parser = ggc_alloc<c_parser> ();
18433 *the_parser = tparser;
18434 if (tparser.tokens == &tparser.tokens_buf[0])
18435 the_parser->tokens = &the_parser->tokens_buf[0];
18437 /* Initialize EH, if we've been told to do so. */
18438 if (flag_exceptions)
18439 using_eh_for_cleanups ();
18441 c_parser_translation_unit (the_parser);
18442 the_parser = NULL;
18445 /* This function parses Cilk Plus array notation. The starting index is
18446 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
18447 return value of this function is a tree_node called VALUE_TREE of type
18448 ARRAY_NOTATION_REF. */
18450 static tree
18451 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
18452 tree array_value)
18454 c_token *token = NULL;
18455 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
18456 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
18457 tree array_type_domain = NULL_TREE;
18459 if (array_value == error_mark_node || initial_index == error_mark_node)
18461 /* No need to continue. If either of these 2 were true, then an error
18462 must be emitted already. Thus, no need to emit them twice. */
18463 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18464 return error_mark_node;
18467 array_type = TREE_TYPE (array_value);
18468 gcc_assert (array_type);
18469 if (TREE_CODE (array_type) != ARRAY_TYPE
18470 && TREE_CODE (array_type) != POINTER_TYPE)
18472 error_at (loc, "base of array section must be pointer or array type");
18473 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18474 return error_mark_node;
18476 type = TREE_TYPE (array_type);
18477 token = c_parser_peek_token (parser);
18479 if (token->type == CPP_EOF)
18481 c_parser_error (parser, "expected %<:%> or numeral");
18482 return value_tree;
18484 else if (token->type == CPP_COLON)
18486 if (!initial_index)
18488 /* If we are here, then we have a case like this A[:]. */
18489 c_parser_consume_token (parser);
18490 if (TREE_CODE (array_type) == POINTER_TYPE)
18492 error_at (loc, "start-index and length fields necessary for "
18493 "using array notations in pointers");
18494 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18495 return error_mark_node;
18497 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18499 error_at (loc, "array notations cannot be used with function "
18500 "type");
18501 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18502 return error_mark_node;
18504 array_type_domain = TYPE_DOMAIN (array_type);
18506 if (!array_type_domain)
18508 error_at (loc, "start-index and length fields necessary for "
18509 "using array notations in dimensionless arrays");
18510 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18511 return error_mark_node;
18514 start_index = TYPE_MIN_VALUE (array_type_domain);
18515 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
18516 start_index);
18517 if (!TYPE_MAX_VALUE (array_type_domain)
18518 || !TREE_CONSTANT (TYPE_MAX_VALUE (array_type_domain)))
18520 error_at (loc, "start-index and length fields necessary for "
18521 "using array notations in variable-length arrays");
18522 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18523 return error_mark_node;
18525 end_index = TYPE_MAX_VALUE (array_type_domain);
18526 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18527 end_index, integer_one_node);
18528 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18529 stride = build_int_cst (integer_type_node, 1);
18530 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18532 else if (initial_index != error_mark_node)
18534 /* If we are here, then there should be 2 possibilities:
18535 1. Array [EXPR : EXPR]
18536 2. Array [EXPR : EXPR : EXPR]
18538 start_index = initial_index;
18540 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18542 error_at (loc, "array notations cannot be used with function "
18543 "type");
18544 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18545 return error_mark_node;
18547 c_parser_consume_token (parser); /* consume the ':' */
18548 struct c_expr ce = c_parser_expression (parser);
18549 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18550 end_index = ce.value;
18551 if (!end_index || end_index == error_mark_node)
18553 c_parser_skip_to_end_of_block_or_statement (parser);
18554 return error_mark_node;
18556 if (c_parser_peek_token (parser)->type == CPP_COLON)
18558 c_parser_consume_token (parser);
18559 ce = c_parser_expression (parser);
18560 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18561 stride = ce.value;
18562 if (!stride || stride == error_mark_node)
18564 c_parser_skip_to_end_of_block_or_statement (parser);
18565 return error_mark_node;
18569 else
18570 c_parser_error (parser, "expected array notation expression");
18572 else
18573 c_parser_error (parser, "expected array notation expression");
18575 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18577 value_tree = build_array_notation_ref (loc, array_value, start_index,
18578 end_index, stride, type);
18579 if (value_tree != error_mark_node)
18580 SET_EXPR_LOCATION (value_tree, loc);
18581 return value_tree;
18584 /* Parse the body of a function declaration marked with "__RTL".
18586 The RTL parser works on the level of characters read from a
18587 FILE *, whereas c_parser works at the level of tokens.
18588 Square this circle by consuming all of the tokens up to and
18589 including the closing brace, recording the start/end of the RTL
18590 fragment, and reopening the file and re-reading the relevant
18591 lines within the RTL parser.
18593 This requires the opening and closing braces of the C function
18594 to be on separate lines from the RTL they wrap.
18596 Take ownership of START_WITH_PASS, if non-NULL. */
18598 void
18599 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18601 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18603 free (start_with_pass);
18604 return;
18607 location_t start_loc = c_parser_peek_token (parser)->location;
18609 /* Consume all tokens, up to the closing brace, handling
18610 matching pairs of braces in the rtl dump. */
18611 int num_open_braces = 1;
18612 while (1)
18614 switch (c_parser_peek_token (parser)->type)
18616 case CPP_OPEN_BRACE:
18617 num_open_braces++;
18618 break;
18619 case CPP_CLOSE_BRACE:
18620 if (--num_open_braces == 0)
18621 goto found_closing_brace;
18622 break;
18623 case CPP_EOF:
18624 error_at (start_loc, "no closing brace");
18625 free (start_with_pass);
18626 return;
18627 default:
18628 break;
18630 c_parser_consume_token (parser);
18633 found_closing_brace:
18634 /* At the closing brace; record its location. */
18635 location_t end_loc = c_parser_peek_token (parser)->location;
18637 /* Consume the closing brace. */
18638 c_parser_consume_token (parser);
18640 /* Invoke the RTL parser. */
18641 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18643 free (start_with_pass);
18644 return;
18647 /* If a pass name was provided for START_WITH_PASS, run the backend
18648 accordingly now, on the cfun created above, transferring
18649 ownership of START_WITH_PASS. */
18650 if (start_with_pass)
18651 run_rtl_passes (start_with_pass);
18654 #include "gt-c-c-parser.h"