PR c/66618
[official-gcc.git] / gcc / c / c-parser.c
blobbd5dd5799a060c106807f7fa4f872722085a7d73
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 (&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 (&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 (&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 (&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 /* At this point we've consumed:
2245 declaration-specifiers declarator
2246 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2247 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2248 but the
2249 declaration-specifiers declarator
2250 aren't grokkable as a function definition, so we have
2251 an error. */
2252 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2253 if (c_parser_next_token_starts_declspecs (parser))
2255 /* If we have
2256 declaration-specifiers declarator decl-specs
2257 then assume we have a missing semicolon, which would
2258 give us:
2259 declaration-specifiers declarator decl-specs
2262 <~~~~~~~~~ declaration ~~~~~~~~~~>
2263 Use c_parser_require to get an error with a fix-it hint. */
2264 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2265 parser->error = false;
2267 else
2269 /* This can appear in many cases looking nothing like a
2270 function definition, so we don't give a more specific
2271 error suggesting there was one. */
2272 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2273 "or %<__attribute__%>");
2275 if (nested)
2276 c_pop_function_context ();
2277 break;
2280 if (DECL_DECLARED_INLINE_P (current_function_decl))
2281 tv = TV_PARSE_INLINE;
2282 else
2283 tv = TV_PARSE_FUNC;
2284 auto_timevar at (g_timer, tv);
2286 /* Parse old-style parameter declarations. ??? Attributes are
2287 not allowed to start declaration specifiers here because of a
2288 syntax conflict between a function declaration with attribute
2289 suffix and a function definition with an attribute prefix on
2290 first old-style parameter declaration. Following the old
2291 parser, they are not accepted on subsequent old-style
2292 parameter declarations either. However, there is no
2293 ambiguity after the first declaration, nor indeed on the
2294 first as long as we don't allow postfix attributes after a
2295 declarator with a nonempty identifier list in a definition;
2296 and postfix attributes have never been accepted here in
2297 function definitions either. */
2298 while (c_parser_next_token_is_not (parser, CPP_EOF)
2299 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2300 c_parser_declaration_or_fndef (parser, false, false, false,
2301 true, false, NULL, vNULL);
2302 store_parm_decls ();
2303 if (omp_declare_simd_clauses.exists ()
2304 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2305 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2306 omp_declare_simd_clauses);
2307 if (oacc_routine_data)
2308 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2309 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2310 = c_parser_peek_token (parser)->location;
2312 /* If the definition was marked with __GIMPLE then parse the
2313 function body as GIMPLE. */
2314 if (specs->gimple_p)
2316 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2317 bool saved = in_late_binary_op;
2318 in_late_binary_op = true;
2319 c_parser_parse_gimple_body (parser);
2320 in_late_binary_op = saved;
2322 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2323 consuming the function body. */
2324 else if (specs->rtl_p)
2326 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2328 /* Normally, store_parm_decls sets next_is_function_body,
2329 anticipating a function body. We need a push_scope/pop_scope
2330 pair to flush out this state, or subsequent function parsing
2331 will go wrong. */
2332 push_scope ();
2333 pop_scope ();
2335 finish_function ();
2336 return;
2338 else
2340 fnbody = c_parser_compound_statement (parser);
2341 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2342 fnbody = expand_array_notation_exprs (fnbody);
2344 tree fndecl = current_function_decl;
2345 if (nested)
2347 tree decl = current_function_decl;
2348 /* Mark nested functions as needing static-chain initially.
2349 lower_nested_functions will recompute it but the
2350 DECL_STATIC_CHAIN flag is also used before that happens,
2351 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2352 DECL_STATIC_CHAIN (decl) = 1;
2353 add_stmt (fnbody);
2354 finish_function ();
2355 c_pop_function_context ();
2356 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2358 else
2360 if (fnbody)
2361 add_stmt (fnbody);
2362 finish_function ();
2364 /* Get rid of the empty stmt list for GIMPLE. */
2365 if (specs->gimple_p)
2366 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2368 break;
2372 /* Parse an asm-definition (asm() outside a function body). This is a
2373 GNU extension.
2375 asm-definition:
2376 simple-asm-expr ;
2379 static void
2380 c_parser_asm_definition (c_parser *parser)
2382 tree asm_str = c_parser_simple_asm_expr (parser);
2383 if (asm_str)
2384 symtab->finalize_toplevel_asm (asm_str);
2385 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2388 /* Parse a static assertion (C11 6.7.10).
2390 static_assert-declaration:
2391 static_assert-declaration-no-semi ;
2394 static void
2395 c_parser_static_assert_declaration (c_parser *parser)
2397 c_parser_static_assert_declaration_no_semi (parser);
2398 if (parser->error
2399 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2400 c_parser_skip_to_end_of_block_or_statement (parser);
2403 /* Parse a static assertion (C11 6.7.10), without the trailing
2404 semicolon.
2406 static_assert-declaration-no-semi:
2407 _Static_assert ( constant-expression , string-literal )
2410 static void
2411 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2413 location_t assert_loc, value_loc;
2414 tree value;
2415 tree string;
2417 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2418 assert_loc = c_parser_peek_token (parser)->location;
2419 if (flag_isoc99)
2420 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2421 "ISO C99 does not support %<_Static_assert%>");
2422 else
2423 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2424 "ISO C90 does not support %<_Static_assert%>");
2425 c_parser_consume_token (parser);
2426 matching_parens parens;
2427 if (!parens.require_open (parser))
2428 return;
2429 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2430 value = c_parser_expr_no_commas (parser, NULL).value;
2431 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2432 parser->lex_untranslated_string = true;
2433 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2435 parser->lex_untranslated_string = false;
2436 return;
2438 switch (c_parser_peek_token (parser)->type)
2440 case CPP_STRING:
2441 case CPP_STRING16:
2442 case CPP_STRING32:
2443 case CPP_WSTRING:
2444 case CPP_UTF8STRING:
2445 string = c_parser_peek_token (parser)->value;
2446 c_parser_consume_token (parser);
2447 parser->lex_untranslated_string = false;
2448 break;
2449 default:
2450 c_parser_error (parser, "expected string literal");
2451 parser->lex_untranslated_string = false;
2452 return;
2454 parens.require_close (parser);
2456 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2458 error_at (value_loc, "expression in static assertion is not an integer");
2459 return;
2461 if (TREE_CODE (value) != INTEGER_CST)
2463 value = c_fully_fold (value, false, NULL);
2464 /* Strip no-op conversions. */
2465 STRIP_TYPE_NOPS (value);
2466 if (TREE_CODE (value) == INTEGER_CST)
2467 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2468 "is not an integer constant expression");
2470 if (TREE_CODE (value) != INTEGER_CST)
2472 error_at (value_loc, "expression in static assertion is not constant");
2473 return;
2475 constant_expression_warning (value);
2476 if (integer_zerop (value))
2477 error_at (assert_loc, "static assertion failed: %E", string);
2480 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2481 6.7, C11 6.7), adding them to SPECS (which may already include some).
2482 Storage class specifiers are accepted iff SCSPEC_OK; type
2483 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2484 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2485 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2487 declaration-specifiers:
2488 storage-class-specifier declaration-specifiers[opt]
2489 type-specifier declaration-specifiers[opt]
2490 type-qualifier declaration-specifiers[opt]
2491 function-specifier declaration-specifiers[opt]
2492 alignment-specifier declaration-specifiers[opt]
2494 Function specifiers (inline) are from C99, and are currently
2495 handled as storage class specifiers, as is __thread. Alignment
2496 specifiers are from C11.
2498 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2499 storage-class-specifier:
2500 typedef
2501 extern
2502 static
2503 auto
2504 register
2505 _Thread_local
2507 (_Thread_local is new in C11.)
2509 C99 6.7.4, C11 6.7.4:
2510 function-specifier:
2511 inline
2512 _Noreturn
2514 (_Noreturn is new in C11.)
2516 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2517 type-specifier:
2518 void
2519 char
2520 short
2522 long
2523 float
2524 double
2525 signed
2526 unsigned
2527 _Bool
2528 _Complex
2529 [_Imaginary removed in C99 TC2]
2530 struct-or-union-specifier
2531 enum-specifier
2532 typedef-name
2533 atomic-type-specifier
2535 (_Bool and _Complex are new in C99.)
2536 (atomic-type-specifier is new in C11.)
2538 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2540 type-qualifier:
2541 const
2542 restrict
2543 volatile
2544 address-space-qualifier
2545 _Atomic
2547 (restrict is new in C99.)
2548 (_Atomic is new in C11.)
2550 GNU extensions:
2552 declaration-specifiers:
2553 attributes declaration-specifiers[opt]
2555 type-qualifier:
2556 address-space
2558 address-space:
2559 identifier recognized by the target
2561 storage-class-specifier:
2562 __thread
2564 type-specifier:
2565 typeof-specifier
2566 __auto_type
2567 __intN
2568 _Decimal32
2569 _Decimal64
2570 _Decimal128
2571 _Fract
2572 _Accum
2573 _Sat
2575 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2576 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2578 atomic-type-specifier
2579 _Atomic ( type-name )
2581 Objective-C:
2583 type-specifier:
2584 class-name objc-protocol-refs[opt]
2585 typedef-name objc-protocol-refs
2586 objc-protocol-refs
2589 void
2590 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2591 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2592 bool alignspec_ok, bool auto_type_ok,
2593 enum c_lookahead_kind la)
2595 bool attrs_ok = start_attr_ok;
2596 bool seen_type = specs->typespec_kind != ctsk_none;
2598 if (!typespec_ok)
2599 gcc_assert (la == cla_prefer_id);
2601 while (c_parser_next_token_is (parser, CPP_NAME)
2602 || c_parser_next_token_is (parser, CPP_KEYWORD)
2603 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2605 struct c_typespec t;
2606 tree attrs;
2607 tree align;
2608 location_t loc = c_parser_peek_token (parser)->location;
2610 /* If we cannot accept a type, exit if the next token must start
2611 one. Also, if we already have seen a tagged definition,
2612 a typename would be an error anyway and likely the user
2613 has simply forgotten a semicolon, so we exit. */
2614 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2615 && c_parser_next_tokens_start_typename (parser, la)
2616 && !c_parser_next_token_is_qualifier (parser))
2617 break;
2619 if (c_parser_next_token_is (parser, CPP_NAME))
2621 c_token *name_token = c_parser_peek_token (parser);
2622 tree value = name_token->value;
2623 c_id_kind kind = name_token->id_kind;
2625 if (kind == C_ID_ADDRSPACE)
2627 addr_space_t as
2628 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2629 declspecs_add_addrspace (name_token->location, specs, as);
2630 c_parser_consume_token (parser);
2631 attrs_ok = true;
2632 continue;
2635 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2637 /* If we cannot accept a type, and the next token must start one,
2638 exit. Do the same if we already have seen a tagged definition,
2639 since it would be an error anyway and likely the user has simply
2640 forgotten a semicolon. */
2641 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2642 break;
2644 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2645 a C_ID_CLASSNAME. */
2646 c_parser_consume_token (parser);
2647 seen_type = true;
2648 attrs_ok = true;
2649 if (kind == C_ID_ID)
2651 error_at (loc, "unknown type name %qE", value);
2652 t.kind = ctsk_typedef;
2653 t.spec = error_mark_node;
2655 else if (kind == C_ID_TYPENAME
2656 && (!c_dialect_objc ()
2657 || c_parser_next_token_is_not (parser, CPP_LESS)))
2659 t.kind = ctsk_typedef;
2660 /* For a typedef name, record the meaning, not the name.
2661 In case of 'foo foo, bar;'. */
2662 t.spec = lookup_name (value);
2664 else
2666 tree proto = NULL_TREE;
2667 gcc_assert (c_dialect_objc ());
2668 t.kind = ctsk_objc;
2669 if (c_parser_next_token_is (parser, CPP_LESS))
2670 proto = c_parser_objc_protocol_refs (parser);
2671 t.spec = objc_get_protocol_qualified_type (value, proto);
2673 t.expr = NULL_TREE;
2674 t.expr_const_operands = true;
2675 declspecs_add_type (name_token->location, specs, t);
2676 continue;
2678 if (c_parser_next_token_is (parser, CPP_LESS))
2680 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2681 nisse@lysator.liu.se. */
2682 tree proto;
2683 gcc_assert (c_dialect_objc ());
2684 if (!typespec_ok || seen_type)
2685 break;
2686 proto = c_parser_objc_protocol_refs (parser);
2687 t.kind = ctsk_objc;
2688 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2689 t.expr = NULL_TREE;
2690 t.expr_const_operands = true;
2691 declspecs_add_type (loc, specs, t);
2692 continue;
2694 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2695 switch (c_parser_peek_token (parser)->keyword)
2697 case RID_STATIC:
2698 case RID_EXTERN:
2699 case RID_REGISTER:
2700 case RID_TYPEDEF:
2701 case RID_INLINE:
2702 case RID_NORETURN:
2703 case RID_AUTO:
2704 case RID_THREAD:
2705 if (!scspec_ok)
2706 goto out;
2707 attrs_ok = true;
2708 /* TODO: Distinguish between function specifiers (inline, noreturn)
2709 and storage class specifiers, either here or in
2710 declspecs_add_scspec. */
2711 declspecs_add_scspec (loc, specs,
2712 c_parser_peek_token (parser)->value);
2713 c_parser_consume_token (parser);
2714 break;
2715 case RID_AUTO_TYPE:
2716 if (!auto_type_ok)
2717 goto out;
2718 /* Fall through. */
2719 case RID_UNSIGNED:
2720 case RID_LONG:
2721 case RID_SHORT:
2722 case RID_SIGNED:
2723 case RID_COMPLEX:
2724 case RID_INT:
2725 case RID_CHAR:
2726 case RID_FLOAT:
2727 case RID_DOUBLE:
2728 case RID_VOID:
2729 case RID_DFLOAT32:
2730 case RID_DFLOAT64:
2731 case RID_DFLOAT128:
2732 CASE_RID_FLOATN_NX:
2733 case RID_BOOL:
2734 case RID_FRACT:
2735 case RID_ACCUM:
2736 case RID_SAT:
2737 case RID_INT_N_0:
2738 case RID_INT_N_1:
2739 case RID_INT_N_2:
2740 case RID_INT_N_3:
2741 if (!typespec_ok)
2742 goto out;
2743 attrs_ok = true;
2744 seen_type = true;
2745 if (c_dialect_objc ())
2746 parser->objc_need_raw_identifier = true;
2747 t.kind = ctsk_resword;
2748 t.spec = c_parser_peek_token (parser)->value;
2749 t.expr = NULL_TREE;
2750 t.expr_const_operands = true;
2751 declspecs_add_type (loc, specs, t);
2752 c_parser_consume_token (parser);
2753 break;
2754 case RID_ENUM:
2755 if (!typespec_ok)
2756 goto out;
2757 attrs_ok = true;
2758 seen_type = true;
2759 t = c_parser_enum_specifier (parser);
2760 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2761 declspecs_add_type (loc, specs, t);
2762 break;
2763 case RID_STRUCT:
2764 case RID_UNION:
2765 if (!typespec_ok)
2766 goto out;
2767 attrs_ok = true;
2768 seen_type = true;
2769 t = c_parser_struct_or_union_specifier (parser);
2770 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2771 declspecs_add_type (loc, specs, t);
2772 break;
2773 case RID_TYPEOF:
2774 /* ??? The old parser rejected typeof after other type
2775 specifiers, but is a syntax error the best way of
2776 handling this? */
2777 if (!typespec_ok || seen_type)
2778 goto out;
2779 attrs_ok = true;
2780 seen_type = true;
2781 t = c_parser_typeof_specifier (parser);
2782 declspecs_add_type (loc, specs, t);
2783 break;
2784 case RID_ATOMIC:
2785 /* C parser handling of Objective-C constructs needs
2786 checking for correct lvalue-to-rvalue conversions, and
2787 the code in build_modify_expr handling various
2788 Objective-C cases, and that in build_unary_op handling
2789 Objective-C cases for increment / decrement, also needs
2790 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2791 and objc_types_are_equivalent may also need updates. */
2792 if (c_dialect_objc ())
2793 sorry ("%<_Atomic%> in Objective-C");
2794 if (flag_isoc99)
2795 pedwarn_c99 (loc, OPT_Wpedantic,
2796 "ISO C99 does not support the %<_Atomic%> qualifier");
2797 else
2798 pedwarn_c99 (loc, OPT_Wpedantic,
2799 "ISO C90 does not support the %<_Atomic%> qualifier");
2800 attrs_ok = true;
2801 tree value;
2802 value = c_parser_peek_token (parser)->value;
2803 c_parser_consume_token (parser);
2804 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2806 /* _Atomic ( type-name ). */
2807 seen_type = true;
2808 c_parser_consume_token (parser);
2809 struct c_type_name *type = c_parser_type_name (parser);
2810 t.kind = ctsk_typeof;
2811 t.spec = error_mark_node;
2812 t.expr = NULL_TREE;
2813 t.expr_const_operands = true;
2814 if (type != NULL)
2815 t.spec = groktypename (type, &t.expr,
2816 &t.expr_const_operands);
2817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2818 "expected %<)%>");
2819 if (t.spec != error_mark_node)
2821 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2822 error_at (loc, "%<_Atomic%>-qualified array type");
2823 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2824 error_at (loc, "%<_Atomic%>-qualified function type");
2825 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2826 error_at (loc, "%<_Atomic%> applied to a qualified type");
2827 else
2828 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2830 declspecs_add_type (loc, specs, t);
2832 else
2833 declspecs_add_qual (loc, specs, value);
2834 break;
2835 case RID_CONST:
2836 case RID_VOLATILE:
2837 case RID_RESTRICT:
2838 attrs_ok = true;
2839 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2840 c_parser_consume_token (parser);
2841 break;
2842 case RID_ATTRIBUTE:
2843 if (!attrs_ok)
2844 goto out;
2845 attrs = c_parser_attributes (parser);
2846 declspecs_add_attrs (loc, specs, attrs);
2847 break;
2848 case RID_ALIGNAS:
2849 if (!alignspec_ok)
2850 goto out;
2851 align = c_parser_alignas_specifier (parser);
2852 declspecs_add_alignas (loc, specs, align);
2853 break;
2854 case RID_GIMPLE:
2855 if (! flag_gimple)
2856 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2857 c_parser_consume_token (parser);
2858 specs->gimple_p = true;
2859 specs->locations[cdw_gimple] = loc;
2860 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2861 break;
2862 case RID_RTL:
2863 c_parser_consume_token (parser);
2864 specs->rtl_p = true;
2865 specs->locations[cdw_rtl] = loc;
2866 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2867 break;
2868 default:
2869 goto out;
2872 out: ;
2875 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2877 enum-specifier:
2878 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2879 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2880 enum attributes[opt] identifier
2882 The form with trailing comma is new in C99. The forms with
2883 attributes are GNU extensions. In GNU C, we accept any expression
2884 without commas in the syntax (assignment expressions, not just
2885 conditional expressions); assignment expressions will be diagnosed
2886 as non-constant.
2888 enumerator-list:
2889 enumerator
2890 enumerator-list , enumerator
2892 enumerator:
2893 enumeration-constant
2894 enumeration-constant = constant-expression
2896 GNU Extensions:
2898 enumerator:
2899 enumeration-constant attributes[opt]
2900 enumeration-constant attributes[opt] = constant-expression
2904 static struct c_typespec
2905 c_parser_enum_specifier (c_parser *parser)
2907 struct c_typespec ret;
2908 tree attrs;
2909 tree ident = NULL_TREE;
2910 location_t enum_loc;
2911 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2912 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2913 c_parser_consume_token (parser);
2914 attrs = c_parser_attributes (parser);
2915 enum_loc = c_parser_peek_token (parser)->location;
2916 /* Set the location in case we create a decl now. */
2917 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2918 if (c_parser_next_token_is (parser, CPP_NAME))
2920 ident = c_parser_peek_token (parser)->value;
2921 ident_loc = c_parser_peek_token (parser)->location;
2922 enum_loc = ident_loc;
2923 c_parser_consume_token (parser);
2925 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2927 /* Parse an enum definition. */
2928 struct c_enum_contents the_enum;
2929 tree type;
2930 tree postfix_attrs;
2931 /* We chain the enumerators in reverse order, then put them in
2932 forward order at the end. */
2933 tree values;
2934 timevar_push (TV_PARSE_ENUM);
2935 type = start_enum (enum_loc, &the_enum, ident);
2936 values = NULL_TREE;
2937 c_parser_consume_token (parser);
2938 while (true)
2940 tree enum_id;
2941 tree enum_value;
2942 tree enum_decl;
2943 bool seen_comma;
2944 c_token *token;
2945 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2946 location_t decl_loc, value_loc;
2947 if (c_parser_next_token_is_not (parser, CPP_NAME))
2949 /* Give a nicer error for "enum {}". */
2950 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2951 && !parser->error)
2953 error_at (c_parser_peek_token (parser)->location,
2954 "empty enum is invalid");
2955 parser->error = true;
2957 else
2958 c_parser_error (parser, "expected identifier");
2959 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2960 values = error_mark_node;
2961 break;
2963 token = c_parser_peek_token (parser);
2964 enum_id = token->value;
2965 /* Set the location in case we create a decl now. */
2966 c_parser_set_source_position_from_token (token);
2967 decl_loc = value_loc = token->location;
2968 c_parser_consume_token (parser);
2969 /* Parse any specified attributes. */
2970 tree enum_attrs = c_parser_attributes (parser);
2971 if (c_parser_next_token_is (parser, CPP_EQ))
2973 c_parser_consume_token (parser);
2974 value_loc = c_parser_peek_token (parser)->location;
2975 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2977 else
2978 enum_value = NULL_TREE;
2979 enum_decl = build_enumerator (decl_loc, value_loc,
2980 &the_enum, enum_id, enum_value);
2981 if (enum_attrs)
2982 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2983 TREE_CHAIN (enum_decl) = values;
2984 values = enum_decl;
2985 seen_comma = false;
2986 if (c_parser_next_token_is (parser, CPP_COMMA))
2988 comma_loc = c_parser_peek_token (parser)->location;
2989 seen_comma = true;
2990 c_parser_consume_token (parser);
2992 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2994 if (seen_comma)
2995 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2996 "comma at end of enumerator list");
2997 c_parser_consume_token (parser);
2998 break;
3000 if (!seen_comma)
3002 c_parser_error (parser, "expected %<,%> or %<}%>");
3003 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3004 values = error_mark_node;
3005 break;
3008 postfix_attrs = c_parser_attributes (parser);
3009 ret.spec = finish_enum (type, nreverse (values),
3010 chainon (attrs, postfix_attrs));
3011 ret.kind = ctsk_tagdef;
3012 ret.expr = NULL_TREE;
3013 ret.expr_const_operands = true;
3014 timevar_pop (TV_PARSE_ENUM);
3015 return ret;
3017 else if (!ident)
3019 c_parser_error (parser, "expected %<{%>");
3020 ret.spec = error_mark_node;
3021 ret.kind = ctsk_tagref;
3022 ret.expr = NULL_TREE;
3023 ret.expr_const_operands = true;
3024 return ret;
3026 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3027 /* In ISO C, enumerated types can be referred to only if already
3028 defined. */
3029 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3031 gcc_assert (ident);
3032 pedwarn (enum_loc, OPT_Wpedantic,
3033 "ISO C forbids forward references to %<enum%> types");
3035 return ret;
3038 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3040 struct-or-union-specifier:
3041 struct-or-union attributes[opt] identifier[opt]
3042 { struct-contents } attributes[opt]
3043 struct-or-union attributes[opt] identifier
3045 struct-contents:
3046 struct-declaration-list
3048 struct-declaration-list:
3049 struct-declaration ;
3050 struct-declaration-list struct-declaration ;
3052 GNU extensions:
3054 struct-contents:
3055 empty
3056 struct-declaration
3057 struct-declaration-list struct-declaration
3059 struct-declaration-list:
3060 struct-declaration-list ;
3063 (Note that in the syntax here, unlike that in ISO C, the semicolons
3064 are included here rather than in struct-declaration, in order to
3065 describe the syntax with extra semicolons and missing semicolon at
3066 end.)
3068 Objective-C:
3070 struct-declaration-list:
3071 @defs ( class-name )
3073 (Note this does not include a trailing semicolon, but can be
3074 followed by further declarations, and gets a pedwarn-if-pedantic
3075 when followed by a semicolon.) */
3077 static struct c_typespec
3078 c_parser_struct_or_union_specifier (c_parser *parser)
3080 struct c_typespec ret;
3081 tree attrs;
3082 tree ident = NULL_TREE;
3083 location_t struct_loc;
3084 location_t ident_loc = UNKNOWN_LOCATION;
3085 enum tree_code code;
3086 switch (c_parser_peek_token (parser)->keyword)
3088 case RID_STRUCT:
3089 code = RECORD_TYPE;
3090 break;
3091 case RID_UNION:
3092 code = UNION_TYPE;
3093 break;
3094 default:
3095 gcc_unreachable ();
3097 struct_loc = c_parser_peek_token (parser)->location;
3098 c_parser_consume_token (parser);
3099 attrs = c_parser_attributes (parser);
3101 /* Set the location in case we create a decl now. */
3102 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3104 if (c_parser_next_token_is (parser, CPP_NAME))
3106 ident = c_parser_peek_token (parser)->value;
3107 ident_loc = c_parser_peek_token (parser)->location;
3108 struct_loc = ident_loc;
3109 c_parser_consume_token (parser);
3111 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3113 /* Parse a struct or union definition. Start the scope of the
3114 tag before parsing components. */
3115 struct c_struct_parse_info *struct_info;
3116 tree type = start_struct (struct_loc, code, ident, &struct_info);
3117 tree postfix_attrs;
3118 /* We chain the components in reverse order, then put them in
3119 forward order at the end. Each struct-declaration may
3120 declare multiple components (comma-separated), so we must use
3121 chainon to join them, although when parsing each
3122 struct-declaration we can use TREE_CHAIN directly.
3124 The theory behind all this is that there will be more
3125 semicolon separated fields than comma separated fields, and
3126 so we'll be minimizing the number of node traversals required
3127 by chainon. */
3128 tree contents;
3129 timevar_push (TV_PARSE_STRUCT);
3130 contents = NULL_TREE;
3131 c_parser_consume_token (parser);
3132 /* Handle the Objective-C @defs construct,
3133 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3134 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3136 tree name;
3137 gcc_assert (c_dialect_objc ());
3138 c_parser_consume_token (parser);
3139 matching_parens parens;
3140 if (!parens.require_open (parser))
3141 goto end_at_defs;
3142 if (c_parser_next_token_is (parser, CPP_NAME)
3143 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3145 name = c_parser_peek_token (parser)->value;
3146 c_parser_consume_token (parser);
3148 else
3150 c_parser_error (parser, "expected class name");
3151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3152 goto end_at_defs;
3154 parens.skip_until_found_close (parser);
3155 contents = nreverse (objc_get_class_ivars (name));
3157 end_at_defs:
3158 /* Parse the struct-declarations and semicolons. Problems with
3159 semicolons are diagnosed here; empty structures are diagnosed
3160 elsewhere. */
3161 while (true)
3163 tree decls;
3164 /* Parse any stray semicolon. */
3165 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3167 location_t semicolon_loc
3168 = c_parser_peek_token (parser)->location;
3169 gcc_rich_location richloc (semicolon_loc);
3170 richloc.add_fixit_remove ();
3171 pedwarn (&richloc, OPT_Wpedantic,
3172 "extra semicolon in struct or union specified");
3173 c_parser_consume_token (parser);
3174 continue;
3176 /* Stop if at the end of the struct or union contents. */
3177 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3179 c_parser_consume_token (parser);
3180 break;
3182 /* Accept #pragmas at struct scope. */
3183 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3185 c_parser_pragma (parser, pragma_struct, NULL);
3186 continue;
3188 /* Parse some comma-separated declarations, but not the
3189 trailing semicolon if any. */
3190 decls = c_parser_struct_declaration (parser);
3191 contents = chainon (decls, contents);
3192 /* If no semicolon follows, either we have a parse error or
3193 are at the end of the struct or union and should
3194 pedwarn. */
3195 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3196 c_parser_consume_token (parser);
3197 else
3199 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3200 pedwarn (c_parser_peek_token (parser)->location, 0,
3201 "no semicolon at end of struct or union");
3202 else if (parser->error
3203 || !c_parser_next_token_starts_declspecs (parser))
3205 c_parser_error (parser, "expected %<;%>");
3206 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3207 break;
3210 /* If we come here, we have already emitted an error
3211 for an expected `;', identifier or `(', and we also
3212 recovered already. Go on with the next field. */
3215 postfix_attrs = c_parser_attributes (parser);
3216 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3217 chainon (attrs, postfix_attrs), struct_info);
3218 ret.kind = ctsk_tagdef;
3219 ret.expr = NULL_TREE;
3220 ret.expr_const_operands = true;
3221 timevar_pop (TV_PARSE_STRUCT);
3222 return ret;
3224 else if (!ident)
3226 c_parser_error (parser, "expected %<{%>");
3227 ret.spec = error_mark_node;
3228 ret.kind = ctsk_tagref;
3229 ret.expr = NULL_TREE;
3230 ret.expr_const_operands = true;
3231 return ret;
3233 ret = parser_xref_tag (ident_loc, code, ident);
3234 return ret;
3237 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3238 *without* the trailing semicolon.
3240 struct-declaration:
3241 specifier-qualifier-list struct-declarator-list
3242 static_assert-declaration-no-semi
3244 specifier-qualifier-list:
3245 type-specifier specifier-qualifier-list[opt]
3246 type-qualifier specifier-qualifier-list[opt]
3247 attributes specifier-qualifier-list[opt]
3249 struct-declarator-list:
3250 struct-declarator
3251 struct-declarator-list , attributes[opt] struct-declarator
3253 struct-declarator:
3254 declarator attributes[opt]
3255 declarator[opt] : constant-expression attributes[opt]
3257 GNU extensions:
3259 struct-declaration:
3260 __extension__ struct-declaration
3261 specifier-qualifier-list
3263 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3264 of attributes where shown is a GNU extension. In GNU C, we accept
3265 any expression without commas in the syntax (assignment
3266 expressions, not just conditional expressions); assignment
3267 expressions will be diagnosed as non-constant. */
3269 static tree
3270 c_parser_struct_declaration (c_parser *parser)
3272 struct c_declspecs *specs;
3273 tree prefix_attrs;
3274 tree all_prefix_attrs;
3275 tree decls;
3276 location_t decl_loc;
3277 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3279 int ext;
3280 tree decl;
3281 ext = disable_extension_diagnostics ();
3282 c_parser_consume_token (parser);
3283 decl = c_parser_struct_declaration (parser);
3284 restore_extension_diagnostics (ext);
3285 return decl;
3287 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3289 c_parser_static_assert_declaration_no_semi (parser);
3290 return NULL_TREE;
3292 specs = build_null_declspecs ();
3293 decl_loc = c_parser_peek_token (parser)->location;
3294 /* Strictly by the standard, we shouldn't allow _Alignas here,
3295 but it appears to have been intended to allow it there, so
3296 we're keeping it as it is until WG14 reaches a conclusion
3297 of N1731.
3298 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3299 c_parser_declspecs (parser, specs, false, true, true,
3300 true, false, cla_nonabstract_decl);
3301 if (parser->error)
3302 return NULL_TREE;
3303 if (!specs->declspecs_seen_p)
3305 c_parser_error (parser, "expected specifier-qualifier-list");
3306 return NULL_TREE;
3308 finish_declspecs (specs);
3309 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3310 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3312 tree ret;
3313 if (specs->typespec_kind == ctsk_none)
3315 pedwarn (decl_loc, OPT_Wpedantic,
3316 "ISO C forbids member declarations with no members");
3317 shadow_tag_warned (specs, pedantic);
3318 ret = NULL_TREE;
3320 else
3322 /* Support for unnamed structs or unions as members of
3323 structs or unions (which is [a] useful and [b] supports
3324 MS P-SDK). */
3325 tree attrs = NULL;
3327 ret = grokfield (c_parser_peek_token (parser)->location,
3328 build_id_declarator (NULL_TREE), specs,
3329 NULL_TREE, &attrs);
3330 if (ret)
3331 decl_attributes (&ret, attrs, 0);
3333 return ret;
3336 /* Provide better error recovery. Note that a type name here is valid,
3337 and will be treated as a field name. */
3338 if (specs->typespec_kind == ctsk_tagdef
3339 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3340 && c_parser_next_token_starts_declspecs (parser)
3341 && !c_parser_next_token_is (parser, CPP_NAME))
3343 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3344 parser->error = false;
3345 return NULL_TREE;
3348 pending_xref_error ();
3349 prefix_attrs = specs->attrs;
3350 all_prefix_attrs = prefix_attrs;
3351 specs->attrs = NULL_TREE;
3352 decls = NULL_TREE;
3353 while (true)
3355 /* Declaring one or more declarators or un-named bit-fields. */
3356 struct c_declarator *declarator;
3357 bool dummy = false;
3358 if (c_parser_next_token_is (parser, CPP_COLON))
3359 declarator = build_id_declarator (NULL_TREE);
3360 else
3361 declarator = c_parser_declarator (parser,
3362 specs->typespec_kind != ctsk_none,
3363 C_DTR_NORMAL, &dummy);
3364 if (declarator == NULL)
3366 c_parser_skip_to_end_of_block_or_statement (parser);
3367 break;
3369 if (c_parser_next_token_is (parser, CPP_COLON)
3370 || c_parser_next_token_is (parser, CPP_COMMA)
3371 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3372 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3373 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3375 tree postfix_attrs = NULL_TREE;
3376 tree width = NULL_TREE;
3377 tree d;
3378 if (c_parser_next_token_is (parser, CPP_COLON))
3380 c_parser_consume_token (parser);
3381 width = c_parser_expr_no_commas (parser, NULL).value;
3383 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3384 postfix_attrs = c_parser_attributes (parser);
3385 d = grokfield (c_parser_peek_token (parser)->location,
3386 declarator, specs, width, &all_prefix_attrs);
3387 decl_attributes (&d, chainon (postfix_attrs,
3388 all_prefix_attrs), 0);
3389 DECL_CHAIN (d) = decls;
3390 decls = d;
3391 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3392 all_prefix_attrs = chainon (c_parser_attributes (parser),
3393 prefix_attrs);
3394 else
3395 all_prefix_attrs = prefix_attrs;
3396 if (c_parser_next_token_is (parser, CPP_COMMA))
3397 c_parser_consume_token (parser);
3398 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3399 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3401 /* Semicolon consumed in caller. */
3402 break;
3404 else
3406 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3407 break;
3410 else
3412 c_parser_error (parser,
3413 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3414 "%<__attribute__%>");
3415 break;
3418 return decls;
3421 /* Parse a typeof specifier (a GNU extension).
3423 typeof-specifier:
3424 typeof ( expression )
3425 typeof ( type-name )
3428 static struct c_typespec
3429 c_parser_typeof_specifier (c_parser *parser)
3431 struct c_typespec ret;
3432 ret.kind = ctsk_typeof;
3433 ret.spec = error_mark_node;
3434 ret.expr = NULL_TREE;
3435 ret.expr_const_operands = true;
3436 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3437 c_parser_consume_token (parser);
3438 c_inhibit_evaluation_warnings++;
3439 in_typeof++;
3440 matching_parens parens;
3441 if (!parens.require_open (parser))
3443 c_inhibit_evaluation_warnings--;
3444 in_typeof--;
3445 return ret;
3447 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3449 struct c_type_name *type = c_parser_type_name (parser);
3450 c_inhibit_evaluation_warnings--;
3451 in_typeof--;
3452 if (type != NULL)
3454 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3455 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3458 else
3460 bool was_vm;
3461 location_t here = c_parser_peek_token (parser)->location;
3462 struct c_expr expr = c_parser_expression (parser);
3463 c_inhibit_evaluation_warnings--;
3464 in_typeof--;
3465 if (TREE_CODE (expr.value) == COMPONENT_REF
3466 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3467 error_at (here, "%<typeof%> applied to a bit-field");
3468 mark_exp_read (expr.value);
3469 ret.spec = TREE_TYPE (expr.value);
3470 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3471 /* This is returned with the type so that when the type is
3472 evaluated, this can be evaluated. */
3473 if (was_vm)
3474 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3475 pop_maybe_used (was_vm);
3476 /* For use in macros such as those in <stdatomic.h>, remove all
3477 qualifiers from atomic types. (const can be an issue for more macros
3478 using typeof than just the <stdatomic.h> ones.) */
3479 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3480 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3482 parens.skip_until_found_close (parser);
3483 return ret;
3486 /* Parse an alignment-specifier.
3488 C11 6.7.5:
3490 alignment-specifier:
3491 _Alignas ( type-name )
3492 _Alignas ( constant-expression )
3495 static tree
3496 c_parser_alignas_specifier (c_parser * parser)
3498 tree ret = error_mark_node;
3499 location_t loc = c_parser_peek_token (parser)->location;
3500 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3501 c_parser_consume_token (parser);
3502 if (flag_isoc99)
3503 pedwarn_c99 (loc, OPT_Wpedantic,
3504 "ISO C99 does not support %<_Alignas%>");
3505 else
3506 pedwarn_c99 (loc, OPT_Wpedantic,
3507 "ISO C90 does not support %<_Alignas%>");
3508 matching_parens parens;
3509 if (!parens.require_open (parser))
3510 return ret;
3511 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3513 struct c_type_name *type = c_parser_type_name (parser);
3514 if (type != NULL)
3515 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3516 false, true, 1);
3518 else
3519 ret = c_parser_expr_no_commas (parser, NULL).value;
3520 parens.skip_until_found_close (parser);
3521 return ret;
3524 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3525 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3526 a typedef name may be redeclared; otherwise it may not. KIND
3527 indicates which kind of declarator is wanted. Returns a valid
3528 declarator except in the case of a syntax error in which case NULL is
3529 returned. *SEEN_ID is set to true if an identifier being declared is
3530 seen; this is used to diagnose bad forms of abstract array declarators
3531 and to determine whether an identifier list is syntactically permitted.
3533 declarator:
3534 pointer[opt] direct-declarator
3536 direct-declarator:
3537 identifier
3538 ( attributes[opt] declarator )
3539 direct-declarator array-declarator
3540 direct-declarator ( parameter-type-list )
3541 direct-declarator ( identifier-list[opt] )
3543 pointer:
3544 * type-qualifier-list[opt]
3545 * type-qualifier-list[opt] pointer
3547 type-qualifier-list:
3548 type-qualifier
3549 attributes
3550 type-qualifier-list type-qualifier
3551 type-qualifier-list attributes
3553 array-declarator:
3554 [ type-qualifier-list[opt] assignment-expression[opt] ]
3555 [ static type-qualifier-list[opt] assignment-expression ]
3556 [ type-qualifier-list static assignment-expression ]
3557 [ type-qualifier-list[opt] * ]
3559 parameter-type-list:
3560 parameter-list
3561 parameter-list , ...
3563 parameter-list:
3564 parameter-declaration
3565 parameter-list , parameter-declaration
3567 parameter-declaration:
3568 declaration-specifiers declarator attributes[opt]
3569 declaration-specifiers abstract-declarator[opt] attributes[opt]
3571 identifier-list:
3572 identifier
3573 identifier-list , identifier
3575 abstract-declarator:
3576 pointer
3577 pointer[opt] direct-abstract-declarator
3579 direct-abstract-declarator:
3580 ( attributes[opt] abstract-declarator )
3581 direct-abstract-declarator[opt] array-declarator
3582 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3584 GNU extensions:
3586 direct-declarator:
3587 direct-declarator ( parameter-forward-declarations
3588 parameter-type-list[opt] )
3590 direct-abstract-declarator:
3591 direct-abstract-declarator[opt] ( parameter-forward-declarations
3592 parameter-type-list[opt] )
3594 parameter-forward-declarations:
3595 parameter-list ;
3596 parameter-forward-declarations parameter-list ;
3598 The uses of attributes shown above are GNU extensions.
3600 Some forms of array declarator are not included in C99 in the
3601 syntax for abstract declarators; these are disallowed elsewhere.
3602 This may be a defect (DR#289).
3604 This function also accepts an omitted abstract declarator as being
3605 an abstract declarator, although not part of the formal syntax. */
3607 struct c_declarator *
3608 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3609 bool *seen_id)
3611 /* Parse any initial pointer part. */
3612 if (c_parser_next_token_is (parser, CPP_MULT))
3614 struct c_declspecs *quals_attrs = build_null_declspecs ();
3615 struct c_declarator *inner;
3616 c_parser_consume_token (parser);
3617 c_parser_declspecs (parser, quals_attrs, false, false, true,
3618 false, false, cla_prefer_id);
3619 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3620 if (inner == NULL)
3621 return NULL;
3622 else
3623 return make_pointer_declarator (quals_attrs, inner);
3625 /* Now we have a direct declarator, direct abstract declarator or
3626 nothing (which counts as a direct abstract declarator here). */
3627 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3630 /* Parse a direct declarator or direct abstract declarator; arguments
3631 as c_parser_declarator. */
3633 static struct c_declarator *
3634 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3635 bool *seen_id)
3637 /* The direct declarator must start with an identifier (possibly
3638 omitted) or a parenthesized declarator (possibly abstract). In
3639 an ordinary declarator, initial parentheses must start a
3640 parenthesized declarator. In an abstract declarator or parameter
3641 declarator, they could start a parenthesized declarator or a
3642 parameter list. To tell which, the open parenthesis and any
3643 following attributes must be read. If a declaration specifier
3644 follows, then it is a parameter list; if the specifier is a
3645 typedef name, there might be an ambiguity about redeclaring it,
3646 which is resolved in the direction of treating it as a typedef
3647 name. If a close parenthesis follows, it is also an empty
3648 parameter list, as the syntax does not permit empty abstract
3649 declarators. Otherwise, it is a parenthesized declarator (in
3650 which case the analysis may be repeated inside it, recursively).
3652 ??? There is an ambiguity in a parameter declaration "int
3653 (__attribute__((foo)) x)", where x is not a typedef name: it
3654 could be an abstract declarator for a function, or declare x with
3655 parentheses. The proper resolution of this ambiguity needs
3656 documenting. At present we follow an accident of the old
3657 parser's implementation, whereby the first parameter must have
3658 some declaration specifiers other than just attributes. Thus as
3659 a parameter declaration it is treated as a parenthesized
3660 parameter named x, and as an abstract declarator it is
3661 rejected.
3663 ??? Also following the old parser, attributes inside an empty
3664 parameter list are ignored, making it a list not yielding a
3665 prototype, rather than giving an error or making it have one
3666 parameter with implicit type int.
3668 ??? Also following the old parser, typedef names may be
3669 redeclared in declarators, but not Objective-C class names. */
3671 if (kind != C_DTR_ABSTRACT
3672 && c_parser_next_token_is (parser, CPP_NAME)
3673 && ((type_seen_p
3674 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3675 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3676 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3678 struct c_declarator *inner
3679 = build_id_declarator (c_parser_peek_token (parser)->value);
3680 *seen_id = true;
3681 inner->id_loc = c_parser_peek_token (parser)->location;
3682 c_parser_consume_token (parser);
3683 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3686 if (kind != C_DTR_NORMAL
3687 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3689 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3690 inner->id_loc = c_parser_peek_token (parser)->location;
3691 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3694 /* Either we are at the end of an abstract declarator, or we have
3695 parentheses. */
3697 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3699 tree attrs;
3700 struct c_declarator *inner;
3701 c_parser_consume_token (parser);
3702 attrs = c_parser_attributes (parser);
3703 if (kind != C_DTR_NORMAL
3704 && (c_parser_next_token_starts_declspecs (parser)
3705 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3707 struct c_arg_info *args
3708 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3709 attrs);
3710 if (args == NULL)
3711 return NULL;
3712 else
3714 inner
3715 = build_function_declarator (args,
3716 build_id_declarator (NULL_TREE));
3717 return c_parser_direct_declarator_inner (parser, *seen_id,
3718 inner);
3721 /* A parenthesized declarator. */
3722 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3723 if (inner != NULL && attrs != NULL)
3724 inner = build_attrs_declarator (attrs, inner);
3725 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3727 c_parser_consume_token (parser);
3728 if (inner == NULL)
3729 return NULL;
3730 else
3731 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3733 else
3735 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3736 "expected %<)%>");
3737 return NULL;
3740 else
3742 if (kind == C_DTR_NORMAL)
3744 c_parser_error (parser, "expected identifier or %<(%>");
3745 return NULL;
3747 else
3748 return build_id_declarator (NULL_TREE);
3752 /* Parse part of a direct declarator or direct abstract declarator,
3753 given that some (in INNER) has already been parsed; ID_PRESENT is
3754 true if an identifier is present, false for an abstract
3755 declarator. */
3757 static struct c_declarator *
3758 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3759 struct c_declarator *inner)
3761 /* Parse a sequence of array declarators and parameter lists. */
3762 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3764 location_t brace_loc = c_parser_peek_token (parser)->location;
3765 struct c_declarator *declarator;
3766 struct c_declspecs *quals_attrs = build_null_declspecs ();
3767 bool static_seen;
3768 bool star_seen;
3769 struct c_expr dimen;
3770 dimen.value = NULL_TREE;
3771 dimen.original_code = ERROR_MARK;
3772 dimen.original_type = NULL_TREE;
3773 c_parser_consume_token (parser);
3774 c_parser_declspecs (parser, quals_attrs, false, false, true,
3775 false, false, cla_prefer_id);
3776 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3777 if (static_seen)
3778 c_parser_consume_token (parser);
3779 if (static_seen && !quals_attrs->declspecs_seen_p)
3780 c_parser_declspecs (parser, quals_attrs, false, false, true,
3781 false, false, cla_prefer_id);
3782 if (!quals_attrs->declspecs_seen_p)
3783 quals_attrs = NULL;
3784 /* If "static" is present, there must be an array dimension.
3785 Otherwise, there may be a dimension, "*", or no
3786 dimension. */
3787 if (static_seen)
3789 star_seen = false;
3790 dimen = c_parser_expr_no_commas (parser, NULL);
3792 else
3794 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3796 dimen.value = NULL_TREE;
3797 star_seen = false;
3799 else if (flag_cilkplus
3800 && c_parser_next_token_is (parser, CPP_COLON))
3802 dimen.value = error_mark_node;
3803 star_seen = false;
3804 error_at (c_parser_peek_token (parser)->location,
3805 "array notations cannot be used in declaration");
3806 c_parser_consume_token (parser);
3808 else if (c_parser_next_token_is (parser, CPP_MULT))
3810 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3812 dimen.value = NULL_TREE;
3813 star_seen = true;
3814 c_parser_consume_token (parser);
3816 else
3818 star_seen = false;
3819 dimen = c_parser_expr_no_commas (parser, NULL);
3822 else
3824 star_seen = false;
3825 dimen = c_parser_expr_no_commas (parser, NULL);
3828 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3829 c_parser_consume_token (parser);
3830 else if (flag_cilkplus
3831 && c_parser_next_token_is (parser, CPP_COLON))
3833 error_at (c_parser_peek_token (parser)->location,
3834 "array notations cannot be used in declaration");
3835 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3836 return NULL;
3838 else
3840 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3841 "expected %<]%>");
3842 return NULL;
3844 if (dimen.value)
3845 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3846 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3847 static_seen, star_seen);
3848 if (declarator == NULL)
3849 return NULL;
3850 inner = set_array_declarator_inner (declarator, inner);
3851 return c_parser_direct_declarator_inner (parser, id_present, inner);
3853 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3855 tree attrs;
3856 struct c_arg_info *args;
3857 c_parser_consume_token (parser);
3858 attrs = c_parser_attributes (parser);
3859 args = c_parser_parms_declarator (parser, id_present, attrs);
3860 if (args == NULL)
3861 return NULL;
3862 else
3864 inner = build_function_declarator (args, inner);
3865 return c_parser_direct_declarator_inner (parser, id_present, inner);
3868 return inner;
3871 /* Parse a parameter list or identifier list, including the closing
3872 parenthesis but not the opening one. ATTRS are the attributes at
3873 the start of the list. ID_LIST_OK is true if an identifier list is
3874 acceptable; such a list must not have attributes at the start. */
3876 static struct c_arg_info *
3877 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3879 push_scope ();
3880 declare_parm_level ();
3881 /* If the list starts with an identifier, it is an identifier list.
3882 Otherwise, it is either a prototype list or an empty list. */
3883 if (id_list_ok
3884 && !attrs
3885 && c_parser_next_token_is (parser, CPP_NAME)
3886 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3888 /* Look ahead to detect typos in type names. */
3889 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3890 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3891 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3892 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3893 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3895 tree list = NULL_TREE, *nextp = &list;
3896 while (c_parser_next_token_is (parser, CPP_NAME)
3897 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3899 *nextp = build_tree_list (NULL_TREE,
3900 c_parser_peek_token (parser)->value);
3901 nextp = & TREE_CHAIN (*nextp);
3902 c_parser_consume_token (parser);
3903 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3904 break;
3905 c_parser_consume_token (parser);
3906 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3908 c_parser_error (parser, "expected identifier");
3909 break;
3912 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3914 struct c_arg_info *ret = build_arg_info ();
3915 ret->types = list;
3916 c_parser_consume_token (parser);
3917 pop_scope ();
3918 return ret;
3920 else
3922 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3923 "expected %<)%>");
3924 pop_scope ();
3925 return NULL;
3928 else
3930 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3931 NULL);
3932 pop_scope ();
3933 return ret;
3937 /* Parse a parameter list (possibly empty), including the closing
3938 parenthesis but not the opening one. ATTRS are the attributes at
3939 the start of the list. EXPR is NULL or an expression that needs to
3940 be evaluated for the side effects of array size expressions in the
3941 parameters. */
3943 static struct c_arg_info *
3944 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3946 bool bad_parm = false;
3948 /* ??? Following the old parser, forward parameter declarations may
3949 use abstract declarators, and if no real parameter declarations
3950 follow the forward declarations then this is not diagnosed. Also
3951 note as above that attributes are ignored as the only contents of
3952 the parentheses, or as the only contents after forward
3953 declarations. */
3954 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3956 struct c_arg_info *ret = build_arg_info ();
3957 c_parser_consume_token (parser);
3958 return ret;
3960 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3962 struct c_arg_info *ret = build_arg_info ();
3964 if (flag_allow_parameterless_variadic_functions)
3966 /* F (...) is allowed. */
3967 ret->types = NULL_TREE;
3969 else
3971 /* Suppress -Wold-style-definition for this case. */
3972 ret->types = error_mark_node;
3973 error_at (c_parser_peek_token (parser)->location,
3974 "ISO C requires a named argument before %<...%>");
3976 c_parser_consume_token (parser);
3977 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3979 c_parser_consume_token (parser);
3980 return ret;
3982 else
3984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3985 "expected %<)%>");
3986 return NULL;
3989 /* Nonempty list of parameters, either terminated with semicolon
3990 (forward declarations; recurse) or with close parenthesis (normal
3991 function) or with ", ... )" (variadic function). */
3992 while (true)
3994 /* Parse a parameter. */
3995 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3996 attrs = NULL_TREE;
3997 if (parm == NULL)
3998 bad_parm = true;
3999 else
4000 push_parm_decl (parm, &expr);
4001 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4003 tree new_attrs;
4004 c_parser_consume_token (parser);
4005 mark_forward_parm_decls ();
4006 new_attrs = c_parser_attributes (parser);
4007 return c_parser_parms_list_declarator (parser, new_attrs, expr);
4009 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4011 c_parser_consume_token (parser);
4012 if (bad_parm)
4013 return NULL;
4014 else
4015 return get_parm_info (false, expr);
4017 if (!c_parser_require (parser, CPP_COMMA,
4018 "expected %<;%>, %<,%> or %<)%>",
4019 UNKNOWN_LOCATION, false))
4021 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4022 return NULL;
4024 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4026 c_parser_consume_token (parser);
4027 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4029 c_parser_consume_token (parser);
4030 if (bad_parm)
4031 return NULL;
4032 else
4033 return get_parm_info (true, expr);
4035 else
4037 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4038 "expected %<)%>");
4039 return NULL;
4045 /* Parse a parameter declaration. ATTRS are the attributes at the
4046 start of the declaration if it is the first parameter. */
4048 static struct c_parm *
4049 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4051 struct c_declspecs *specs;
4052 struct c_declarator *declarator;
4053 tree prefix_attrs;
4054 tree postfix_attrs = NULL_TREE;
4055 bool dummy = false;
4057 /* Accept #pragmas between parameter declarations. */
4058 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4059 c_parser_pragma (parser, pragma_param, NULL);
4061 if (!c_parser_next_token_starts_declspecs (parser))
4063 c_token *token = c_parser_peek_token (parser);
4064 if (parser->error)
4065 return NULL;
4066 c_parser_set_source_position_from_token (token);
4067 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4069 const char *hint = lookup_name_fuzzy (token->value,
4070 FUZZY_LOOKUP_TYPENAME);
4071 if (hint)
4073 gcc_rich_location richloc (token->location);
4074 richloc.add_fixit_replace (hint);
4075 error_at (&richloc,
4076 "unknown type name %qE; did you mean %qs?",
4077 token->value, hint);
4079 else
4080 error_at (token->location, "unknown type name %qE", token->value);
4081 parser->error = true;
4083 /* ??? In some Objective-C cases '...' isn't applicable so there
4084 should be a different message. */
4085 else
4086 c_parser_error (parser,
4087 "expected declaration specifiers or %<...%>");
4088 c_parser_skip_to_end_of_parameter (parser);
4089 return NULL;
4092 location_t start_loc = c_parser_peek_token (parser)->location;
4094 specs = build_null_declspecs ();
4095 if (attrs)
4097 declspecs_add_attrs (input_location, specs, attrs);
4098 attrs = NULL_TREE;
4100 c_parser_declspecs (parser, specs, true, true, true, true, false,
4101 cla_nonabstract_decl);
4102 finish_declspecs (specs);
4103 pending_xref_error ();
4104 prefix_attrs = specs->attrs;
4105 specs->attrs = NULL_TREE;
4106 declarator = c_parser_declarator (parser,
4107 specs->typespec_kind != ctsk_none,
4108 C_DTR_PARM, &dummy);
4109 if (declarator == NULL)
4111 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4112 return NULL;
4114 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4115 postfix_attrs = c_parser_attributes (parser);
4117 /* Generate a location for the parameter, ranging from the start of the
4118 initial token to the end of the final token.
4120 If we have a identifier, then use it for the caret location, e.g.
4122 extern int callee (int one, int (*two)(int, int), float three);
4123 ~~~~~~^~~~~~~~~~~~~~
4125 otherwise, reuse the start location for the caret location e.g.:
4127 extern int callee (int one, int (*)(int, int), float three);
4128 ^~~~~~~~~~~~~~~~~
4130 location_t end_loc = parser->last_token_location;
4132 /* Find any cdk_id declarator; determine if we have an identifier. */
4133 c_declarator *id_declarator = declarator;
4134 while (id_declarator && id_declarator->kind != cdk_id)
4135 id_declarator = id_declarator->declarator;
4136 location_t caret_loc = (id_declarator->u.id
4137 ? id_declarator->id_loc
4138 : start_loc);
4139 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4141 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4142 declarator, param_loc);
4145 /* Parse a string literal in an asm expression. It should not be
4146 translated, and wide string literals are an error although
4147 permitted by the syntax. This is a GNU extension.
4149 asm-string-literal:
4150 string-literal
4152 ??? At present, following the old parser, the caller needs to have
4153 set lex_untranslated_string to 1. It would be better to follow the
4154 C++ parser rather than using this kludge. */
4156 static tree
4157 c_parser_asm_string_literal (c_parser *parser)
4159 tree str;
4160 int save_flag = warn_overlength_strings;
4161 warn_overlength_strings = 0;
4162 if (c_parser_next_token_is (parser, CPP_STRING))
4164 str = c_parser_peek_token (parser)->value;
4165 c_parser_consume_token (parser);
4167 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4169 error_at (c_parser_peek_token (parser)->location,
4170 "wide string literal in %<asm%>");
4171 str = build_string (1, "");
4172 c_parser_consume_token (parser);
4174 else
4176 c_parser_error (parser, "expected string literal");
4177 str = NULL_TREE;
4179 warn_overlength_strings = save_flag;
4180 return str;
4183 /* Parse a simple asm expression. This is used in restricted
4184 contexts, where a full expression with inputs and outputs does not
4185 make sense. This is a GNU extension.
4187 simple-asm-expr:
4188 asm ( asm-string-literal )
4191 static tree
4192 c_parser_simple_asm_expr (c_parser *parser)
4194 tree str;
4195 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4196 /* ??? Follow the C++ parser rather than using the
4197 lex_untranslated_string kludge. */
4198 parser->lex_untranslated_string = true;
4199 c_parser_consume_token (parser);
4200 matching_parens parens;
4201 if (!parens.require_open (parser))
4203 parser->lex_untranslated_string = false;
4204 return NULL_TREE;
4206 str = c_parser_asm_string_literal (parser);
4207 parser->lex_untranslated_string = false;
4208 if (!parens.require_close (parser))
4210 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4211 return NULL_TREE;
4213 return str;
4216 static tree
4217 c_parser_attribute_any_word (c_parser *parser)
4219 tree attr_name = NULL_TREE;
4221 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4223 /* ??? See comment above about what keywords are accepted here. */
4224 bool ok;
4225 switch (c_parser_peek_token (parser)->keyword)
4227 case RID_STATIC:
4228 case RID_UNSIGNED:
4229 case RID_LONG:
4230 case RID_CONST:
4231 case RID_EXTERN:
4232 case RID_REGISTER:
4233 case RID_TYPEDEF:
4234 case RID_SHORT:
4235 case RID_INLINE:
4236 case RID_NORETURN:
4237 case RID_VOLATILE:
4238 case RID_SIGNED:
4239 case RID_AUTO:
4240 case RID_RESTRICT:
4241 case RID_COMPLEX:
4242 case RID_THREAD:
4243 case RID_INT:
4244 case RID_CHAR:
4245 case RID_FLOAT:
4246 case RID_DOUBLE:
4247 case RID_VOID:
4248 case RID_DFLOAT32:
4249 case RID_DFLOAT64:
4250 case RID_DFLOAT128:
4251 CASE_RID_FLOATN_NX:
4252 case RID_BOOL:
4253 case RID_FRACT:
4254 case RID_ACCUM:
4255 case RID_SAT:
4256 case RID_TRANSACTION_ATOMIC:
4257 case RID_TRANSACTION_CANCEL:
4258 case RID_ATOMIC:
4259 case RID_AUTO_TYPE:
4260 case RID_INT_N_0:
4261 case RID_INT_N_1:
4262 case RID_INT_N_2:
4263 case RID_INT_N_3:
4264 ok = true;
4265 break;
4266 default:
4267 ok = false;
4268 break;
4270 if (!ok)
4271 return NULL_TREE;
4273 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4274 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4276 else if (c_parser_next_token_is (parser, CPP_NAME))
4277 attr_name = c_parser_peek_token (parser)->value;
4279 return attr_name;
4282 #define CILK_SIMD_FN_CLAUSE_MASK \
4283 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
4284 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
4285 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
4286 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
4287 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
4289 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
4290 VEC_TOKEN is the "vector" token that is replaced with "simd" and
4291 pushed into the token list.
4292 Syntax:
4293 vector
4294 vector (<vector attributes>). */
4296 static void
4297 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
4299 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4301 int paren_scope = 0;
4302 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4303 /* Consume the "vector" token. */
4304 c_parser_consume_token (parser);
4306 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4308 c_parser_consume_token (parser);
4309 paren_scope++;
4311 while (paren_scope > 0)
4313 c_token *token = c_parser_peek_token (parser);
4314 if (token->type == CPP_OPEN_PAREN)
4315 paren_scope++;
4316 else if (token->type == CPP_CLOSE_PAREN)
4317 paren_scope--;
4318 /* Do not push the last ')' since we are not pushing the '('. */
4319 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4320 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4321 c_parser_consume_token (parser);
4324 /* Since we are converting an attribute to a pragma, we need to end the
4325 attribute with PRAGMA_EOL. */
4326 c_token eol_token;
4327 memset (&eol_token, 0, sizeof (eol_token));
4328 eol_token.type = CPP_PRAGMA_EOL;
4329 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4332 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4334 static void
4335 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4337 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4339 /* c_parser_attributes is called in several places, so if these EOF
4340 tokens are already inserted, then don't do them again. */
4341 if (last_token.type == CPP_EOF)
4342 return;
4344 /* Two CPP_EOF token are added as a safety net since the normal C
4345 front-end has two token look-ahead. */
4346 c_token eof_token;
4347 eof_token.type = CPP_EOF;
4348 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4349 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4352 /* Parse (possibly empty) attributes. This is a GNU extension.
4354 attributes:
4355 empty
4356 attributes attribute
4358 attribute:
4359 __attribute__ ( ( attribute-list ) )
4361 attribute-list:
4362 attrib
4363 attribute_list , attrib
4365 attrib:
4366 empty
4367 any-word
4368 any-word ( identifier )
4369 any-word ( identifier , nonempty-expr-list )
4370 any-word ( expr-list )
4372 where the "identifier" must not be declared as a type, and
4373 "any-word" may be any identifier (including one declared as a
4374 type), a reserved word storage class specifier, type specifier or
4375 type qualifier. ??? This still leaves out most reserved keywords
4376 (following the old parser), shouldn't we include them, and why not
4377 allow identifiers declared as types to start the arguments? */
4379 static tree
4380 c_parser_attributes (c_parser *parser)
4382 tree attrs = NULL_TREE;
4383 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4385 /* ??? Follow the C++ parser rather than using the
4386 lex_untranslated_string kludge. */
4387 parser->lex_untranslated_string = true;
4388 /* Consume the `__attribute__' keyword. */
4389 c_parser_consume_token (parser);
4390 /* Look for the two `(' tokens. */
4391 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4393 parser->lex_untranslated_string = false;
4394 return attrs;
4396 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4398 parser->lex_untranslated_string = false;
4399 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4400 return attrs;
4402 /* Parse the attribute list. */
4403 while (c_parser_next_token_is (parser, CPP_COMMA)
4404 || c_parser_next_token_is (parser, CPP_NAME)
4405 || c_parser_next_token_is (parser, CPP_KEYWORD))
4407 tree attr, attr_name, attr_args;
4408 vec<tree, va_gc> *expr_list;
4409 if (c_parser_next_token_is (parser, CPP_COMMA))
4411 c_parser_consume_token (parser);
4412 continue;
4415 attr_name = c_parser_attribute_any_word (parser);
4416 if (attr_name == NULL)
4417 break;
4418 attr_name = canonicalize_attr_name (attr_name);
4419 if (is_cilkplus_vector_p (attr_name))
4421 c_token *v_token = c_parser_peek_token (parser);
4422 v_token->value = canonicalize_attr_name (v_token->value);
4423 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4424 /* If the next token isn't a comma, we're done. */
4425 if (!c_parser_next_token_is (parser, CPP_COMMA))
4426 break;
4427 continue;
4429 c_parser_consume_token (parser);
4430 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4432 attr = build_tree_list (attr_name, NULL_TREE);
4433 /* Add this attribute to the list. */
4434 attrs = chainon (attrs, attr);
4435 /* If the next token isn't a comma, we're done. */
4436 if (!c_parser_next_token_is (parser, CPP_COMMA))
4437 break;
4438 continue;
4440 c_parser_consume_token (parser);
4441 /* Parse the attribute contents. If they start with an
4442 identifier which is followed by a comma or close
4443 parenthesis, then the arguments start with that
4444 identifier; otherwise they are an expression list.
4445 In objective-c the identifier may be a classname. */
4446 if (c_parser_next_token_is (parser, CPP_NAME)
4447 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4448 || (c_dialect_objc ()
4449 && c_parser_peek_token (parser)->id_kind
4450 == C_ID_CLASSNAME))
4451 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4452 || (c_parser_peek_2nd_token (parser)->type
4453 == CPP_CLOSE_PAREN))
4454 && (attribute_takes_identifier_p (attr_name)
4455 || (c_dialect_objc ()
4456 && c_parser_peek_token (parser)->id_kind
4457 == C_ID_CLASSNAME)))
4459 tree arg1 = c_parser_peek_token (parser)->value;
4460 c_parser_consume_token (parser);
4461 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4462 attr_args = build_tree_list (NULL_TREE, arg1);
4463 else
4465 tree tree_list;
4466 c_parser_consume_token (parser);
4467 expr_list = c_parser_expr_list (parser, false, true,
4468 NULL, NULL, NULL, NULL);
4469 tree_list = build_tree_list_vec (expr_list);
4470 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4471 release_tree_vector (expr_list);
4474 else
4476 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4477 attr_args = NULL_TREE;
4478 else
4480 expr_list = c_parser_expr_list (parser, false, true,
4481 NULL, NULL, NULL, NULL);
4482 attr_args = build_tree_list_vec (expr_list);
4483 release_tree_vector (expr_list);
4487 attr = build_tree_list (attr_name, attr_args);
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 /* Add this attribute to the list. */
4498 attrs = chainon (attrs, attr);
4499 /* If the next token isn't a comma, we're done. */
4500 if (!c_parser_next_token_is (parser, CPP_COMMA))
4501 break;
4503 /* Look for the two `)' tokens. */
4504 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4505 c_parser_consume_token (parser);
4506 else
4508 parser->lex_untranslated_string = false;
4509 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4510 "expected %<)%>");
4511 return attrs;
4513 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4514 c_parser_consume_token (parser);
4515 else
4517 parser->lex_untranslated_string = false;
4518 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4519 "expected %<)%>");
4520 return attrs;
4522 parser->lex_untranslated_string = false;
4525 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4526 c_finish_cilk_simd_fn_tokens (parser);
4527 return attrs;
4530 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7).
4532 type-name:
4533 specifier-qualifier-list abstract-declarator[opt]
4536 struct c_type_name *
4537 c_parser_type_name (c_parser *parser)
4539 struct c_declspecs *specs = build_null_declspecs ();
4540 struct c_declarator *declarator;
4541 struct c_type_name *ret;
4542 bool dummy = false;
4543 c_parser_declspecs (parser, specs, false, true, true, false, false,
4544 cla_prefer_type);
4545 if (!specs->declspecs_seen_p)
4547 c_parser_error (parser, "expected specifier-qualifier-list");
4548 return NULL;
4550 if (specs->type != error_mark_node)
4552 pending_xref_error ();
4553 finish_declspecs (specs);
4555 declarator = c_parser_declarator (parser,
4556 specs->typespec_kind != ctsk_none,
4557 C_DTR_ABSTRACT, &dummy);
4558 if (declarator == NULL)
4559 return NULL;
4560 ret = XOBNEW (&parser_obstack, struct c_type_name);
4561 ret->specs = specs;
4562 ret->declarator = declarator;
4563 return ret;
4566 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4568 initializer:
4569 assignment-expression
4570 { initializer-list }
4571 { initializer-list , }
4573 initializer-list:
4574 designation[opt] initializer
4575 initializer-list , designation[opt] initializer
4577 designation:
4578 designator-list =
4580 designator-list:
4581 designator
4582 designator-list designator
4584 designator:
4585 array-designator
4586 . identifier
4588 array-designator:
4589 [ constant-expression ]
4591 GNU extensions:
4593 initializer:
4596 designation:
4597 array-designator
4598 identifier :
4600 array-designator:
4601 [ constant-expression ... constant-expression ]
4603 Any expression without commas is accepted in the syntax for the
4604 constant-expressions, with non-constant expressions rejected later.
4606 This function is only used for top-level initializers; for nested
4607 ones, see c_parser_initval. */
4609 static struct c_expr
4610 c_parser_initializer (c_parser *parser)
4612 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4613 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4614 else
4616 struct c_expr ret;
4617 location_t loc = c_parser_peek_token (parser)->location;
4618 ret = c_parser_expr_no_commas (parser, NULL);
4619 if (TREE_CODE (ret.value) != STRING_CST
4620 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4621 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4622 return ret;
4626 /* The location of the last comma within the current initializer list,
4627 or UNKNOWN_LOCATION if not within one. */
4629 location_t last_init_list_comma;
4631 /* Parse a braced initializer list. TYPE is the type specified for a
4632 compound literal, and NULL_TREE for other initializers and for
4633 nested braced lists. NESTED_P is true for nested braced lists,
4634 false for the list of a compound literal or the list that is the
4635 top-level initializer in a declaration. */
4637 static struct c_expr
4638 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4639 struct obstack *outer_obstack)
4641 struct c_expr ret;
4642 struct obstack braced_init_obstack;
4643 location_t brace_loc = c_parser_peek_token (parser)->location;
4644 gcc_obstack_init (&braced_init_obstack);
4645 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4646 matching_braces braces;
4647 braces.consume_open (parser);
4648 if (nested_p)
4650 finish_implicit_inits (brace_loc, outer_obstack);
4651 push_init_level (brace_loc, 0, &braced_init_obstack);
4653 else
4654 really_start_incremental_init (type);
4655 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4657 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4659 else
4661 /* Parse a non-empty initializer list, possibly with a trailing
4662 comma. */
4663 while (true)
4665 c_parser_initelt (parser, &braced_init_obstack);
4666 if (parser->error)
4667 break;
4668 if (c_parser_next_token_is (parser, CPP_COMMA))
4670 last_init_list_comma = c_parser_peek_token (parser)->location;
4671 c_parser_consume_token (parser);
4673 else
4674 break;
4675 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4676 break;
4679 c_token *next_tok = c_parser_peek_token (parser);
4680 if (next_tok->type != CPP_CLOSE_BRACE)
4682 ret.value = error_mark_node;
4683 ret.original_code = ERROR_MARK;
4684 ret.original_type = NULL;
4685 braces.skip_until_found_close (parser);
4686 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4687 obstack_free (&braced_init_obstack, NULL);
4688 return ret;
4690 location_t close_loc = next_tok->location;
4691 c_parser_consume_token (parser);
4692 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4693 obstack_free (&braced_init_obstack, NULL);
4694 set_c_expr_source_range (&ret, brace_loc, close_loc);
4695 return ret;
4698 /* Parse a nested initializer, including designators. */
4700 static void
4701 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4703 /* Parse any designator or designator list. A single array
4704 designator may have the subsequent "=" omitted in GNU C, but a
4705 longer list or a structure member designator may not. */
4706 if (c_parser_next_token_is (parser, CPP_NAME)
4707 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4709 /* Old-style structure member designator. */
4710 set_init_label (c_parser_peek_token (parser)->location,
4711 c_parser_peek_token (parser)->value,
4712 c_parser_peek_token (parser)->location,
4713 braced_init_obstack);
4714 /* Use the colon as the error location. */
4715 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4716 "obsolete use of designated initializer with %<:%>");
4717 c_parser_consume_token (parser);
4718 c_parser_consume_token (parser);
4720 else
4722 /* des_seen is 0 if there have been no designators, 1 if there
4723 has been a single array designator and 2 otherwise. */
4724 int des_seen = 0;
4725 /* Location of a designator. */
4726 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4727 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4728 || c_parser_next_token_is (parser, CPP_DOT))
4730 int des_prev = des_seen;
4731 if (!des_seen)
4732 des_loc = c_parser_peek_token (parser)->location;
4733 if (des_seen < 2)
4734 des_seen++;
4735 if (c_parser_next_token_is (parser, CPP_DOT))
4737 des_seen = 2;
4738 c_parser_consume_token (parser);
4739 if (c_parser_next_token_is (parser, CPP_NAME))
4741 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4742 c_parser_peek_token (parser)->location,
4743 braced_init_obstack);
4744 c_parser_consume_token (parser);
4746 else
4748 struct c_expr init;
4749 init.value = error_mark_node;
4750 init.original_code = ERROR_MARK;
4751 init.original_type = NULL;
4752 c_parser_error (parser, "expected identifier");
4753 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4754 process_init_element (input_location, init, false,
4755 braced_init_obstack);
4756 return;
4759 else
4761 tree first, second;
4762 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4763 location_t array_index_loc = UNKNOWN_LOCATION;
4764 /* ??? Following the old parser, [ objc-receiver
4765 objc-message-args ] is accepted as an initializer,
4766 being distinguished from a designator by what follows
4767 the first assignment expression inside the square
4768 brackets, but after a first array designator a
4769 subsequent square bracket is for Objective-C taken to
4770 start an expression, using the obsolete form of
4771 designated initializer without '=', rather than
4772 possibly being a second level of designation: in LALR
4773 terms, the '[' is shifted rather than reducing
4774 designator to designator-list. */
4775 if (des_prev == 1 && c_dialect_objc ())
4777 des_seen = des_prev;
4778 break;
4780 if (des_prev == 0 && c_dialect_objc ())
4782 /* This might be an array designator or an
4783 Objective-C message expression. If the former,
4784 continue parsing here; if the latter, parse the
4785 remainder of the initializer given the starting
4786 primary-expression. ??? It might make sense to
4787 distinguish when des_prev == 1 as well; see
4788 previous comment. */
4789 tree rec, args;
4790 struct c_expr mexpr;
4791 c_parser_consume_token (parser);
4792 if (c_parser_peek_token (parser)->type == CPP_NAME
4793 && ((c_parser_peek_token (parser)->id_kind
4794 == C_ID_TYPENAME)
4795 || (c_parser_peek_token (parser)->id_kind
4796 == C_ID_CLASSNAME)))
4798 /* Type name receiver. */
4799 tree id = c_parser_peek_token (parser)->value;
4800 c_parser_consume_token (parser);
4801 rec = objc_get_class_reference (id);
4802 goto parse_message_args;
4804 first = c_parser_expr_no_commas (parser, NULL).value;
4805 mark_exp_read (first);
4806 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4807 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4808 goto array_desig_after_first;
4809 /* Expression receiver. So far only one part
4810 without commas has been parsed; there might be
4811 more of the expression. */
4812 rec = first;
4813 while (c_parser_next_token_is (parser, CPP_COMMA))
4815 struct c_expr next;
4816 location_t comma_loc, exp_loc;
4817 comma_loc = c_parser_peek_token (parser)->location;
4818 c_parser_consume_token (parser);
4819 exp_loc = c_parser_peek_token (parser)->location;
4820 next = c_parser_expr_no_commas (parser, NULL);
4821 next = convert_lvalue_to_rvalue (exp_loc, next,
4822 true, true);
4823 rec = build_compound_expr (comma_loc, rec, next.value);
4825 parse_message_args:
4826 /* Now parse the objc-message-args. */
4827 args = c_parser_objc_message_args (parser);
4828 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4829 "expected %<]%>");
4830 mexpr.value
4831 = objc_build_message_expr (rec, args);
4832 mexpr.original_code = ERROR_MARK;
4833 mexpr.original_type = NULL;
4834 /* Now parse and process the remainder of the
4835 initializer, starting with this message
4836 expression as a primary-expression. */
4837 c_parser_initval (parser, &mexpr, braced_init_obstack);
4838 return;
4840 c_parser_consume_token (parser);
4841 array_index_loc = c_parser_peek_token (parser)->location;
4842 first = c_parser_expr_no_commas (parser, NULL).value;
4843 mark_exp_read (first);
4844 array_desig_after_first:
4845 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4847 ellipsis_loc = c_parser_peek_token (parser)->location;
4848 c_parser_consume_token (parser);
4849 second = c_parser_expr_no_commas (parser, NULL).value;
4850 mark_exp_read (second);
4852 else
4853 second = NULL_TREE;
4854 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4856 c_parser_consume_token (parser);
4857 set_init_index (array_index_loc, first, second,
4858 braced_init_obstack);
4859 if (second)
4860 pedwarn (ellipsis_loc, OPT_Wpedantic,
4861 "ISO C forbids specifying range of elements to initialize");
4863 else
4864 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4865 "expected %<]%>");
4868 if (des_seen >= 1)
4870 if (c_parser_next_token_is (parser, CPP_EQ))
4872 pedwarn_c90 (des_loc, OPT_Wpedantic,
4873 "ISO C90 forbids specifying subobject "
4874 "to initialize");
4875 c_parser_consume_token (parser);
4877 else
4879 if (des_seen == 1)
4880 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4881 "obsolete use of designated initializer without %<=%>");
4882 else
4884 struct c_expr init;
4885 init.value = error_mark_node;
4886 init.original_code = ERROR_MARK;
4887 init.original_type = NULL;
4888 c_parser_error (parser, "expected %<=%>");
4889 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4890 process_init_element (input_location, init, false,
4891 braced_init_obstack);
4892 return;
4897 c_parser_initval (parser, NULL, braced_init_obstack);
4900 /* Parse a nested initializer; as c_parser_initializer but parses
4901 initializers within braced lists, after any designators have been
4902 applied. If AFTER is not NULL then it is an Objective-C message
4903 expression which is the primary-expression starting the
4904 initializer. */
4906 static void
4907 c_parser_initval (c_parser *parser, struct c_expr *after,
4908 struct obstack * braced_init_obstack)
4910 struct c_expr init;
4911 gcc_assert (!after || c_dialect_objc ());
4912 location_t loc = c_parser_peek_token (parser)->location;
4914 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4915 init = c_parser_braced_init (parser, NULL_TREE, true,
4916 braced_init_obstack);
4917 else
4919 init = c_parser_expr_no_commas (parser, after);
4920 if (init.value != NULL_TREE
4921 && TREE_CODE (init.value) != STRING_CST
4922 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4923 init = convert_lvalue_to_rvalue (loc, init, true, true);
4925 process_init_element (loc, init, false, braced_init_obstack);
4928 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4929 C99 6.8.2, C11 6.8.2).
4931 compound-statement:
4932 { block-item-list[opt] }
4933 { label-declarations block-item-list }
4935 block-item-list:
4936 block-item
4937 block-item-list block-item
4939 block-item:
4940 nested-declaration
4941 statement
4943 nested-declaration:
4944 declaration
4946 GNU extensions:
4948 compound-statement:
4949 { label-declarations block-item-list }
4951 nested-declaration:
4952 __extension__ nested-declaration
4953 nested-function-definition
4955 label-declarations:
4956 label-declaration
4957 label-declarations label-declaration
4959 label-declaration:
4960 __label__ identifier-list ;
4962 Allowing the mixing of declarations and code is new in C99. The
4963 GNU syntax also permits (not shown above) labels at the end of
4964 compound statements, which yield an error. We don't allow labels
4965 on declarations; this might seem like a natural extension, but
4966 there would be a conflict between attributes on the label and
4967 prefix attributes on the declaration. ??? The syntax follows the
4968 old parser in requiring something after label declarations.
4969 Although they are erroneous if the labels declared aren't defined,
4970 is it useful for the syntax to be this way?
4972 OpenACC:
4974 block-item:
4975 openacc-directive
4977 openacc-directive:
4978 update-directive
4980 OpenMP:
4982 block-item:
4983 openmp-directive
4985 openmp-directive:
4986 barrier-directive
4987 flush-directive
4988 taskwait-directive
4989 taskyield-directive
4990 cancel-directive
4991 cancellation-point-directive */
4993 static tree
4994 c_parser_compound_statement (c_parser *parser)
4996 tree stmt;
4997 location_t brace_loc;
4998 brace_loc = c_parser_peek_token (parser)->location;
4999 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
5001 /* Ensure a scope is entered and left anyway to avoid confusion
5002 if we have just prepared to enter a function body. */
5003 stmt = c_begin_compound_stmt (true);
5004 c_end_compound_stmt (brace_loc, stmt, true);
5005 return error_mark_node;
5007 stmt = c_begin_compound_stmt (true);
5008 c_parser_compound_statement_nostart (parser);
5010 /* If the compound stmt contains array notations, then we expand them. */
5011 if (flag_cilkplus && contains_array_notation_expr (stmt))
5012 stmt = expand_array_notation_exprs (stmt);
5013 return c_end_compound_stmt (brace_loc, stmt, true);
5016 /* Parse a compound statement except for the opening brace. This is
5017 used for parsing both compound statements and statement expressions
5018 (which follow different paths to handling the opening). */
5020 static void
5021 c_parser_compound_statement_nostart (c_parser *parser)
5023 bool last_stmt = false;
5024 bool last_label = false;
5025 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
5026 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
5027 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5029 c_parser_consume_token (parser);
5030 return;
5032 mark_valid_location_for_stdc_pragma (true);
5033 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
5035 /* Read zero or more forward-declarations for labels that nested
5036 functions can jump to. */
5037 mark_valid_location_for_stdc_pragma (false);
5038 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
5040 label_loc = c_parser_peek_token (parser)->location;
5041 c_parser_consume_token (parser);
5042 /* Any identifiers, including those declared as type names,
5043 are OK here. */
5044 while (true)
5046 tree label;
5047 if (c_parser_next_token_is_not (parser, CPP_NAME))
5049 c_parser_error (parser, "expected identifier");
5050 break;
5052 label
5053 = declare_label (c_parser_peek_token (parser)->value);
5054 C_DECLARED_LABEL_FLAG (label) = 1;
5055 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
5056 c_parser_consume_token (parser);
5057 if (c_parser_next_token_is (parser, CPP_COMMA))
5058 c_parser_consume_token (parser);
5059 else
5060 break;
5062 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5064 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
5066 /* We must now have at least one statement, label or declaration. */
5067 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
5069 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5070 c_parser_error (parser, "expected declaration or statement");
5071 c_parser_consume_token (parser);
5072 return;
5074 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
5076 location_t loc = c_parser_peek_token (parser)->location;
5077 if (c_parser_next_token_is_keyword (parser, RID_CASE)
5078 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5079 || (c_parser_next_token_is (parser, CPP_NAME)
5080 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5082 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5083 label_loc = c_parser_peek_2nd_token (parser)->location;
5084 else
5085 label_loc = c_parser_peek_token (parser)->location;
5086 last_label = true;
5087 last_stmt = false;
5088 mark_valid_location_for_stdc_pragma (false);
5089 c_parser_label (parser);
5091 else if (!last_label
5092 && c_parser_next_tokens_start_declaration (parser))
5094 last_label = false;
5095 mark_valid_location_for_stdc_pragma (false);
5096 bool fallthru_attr_p = false;
5097 c_parser_declaration_or_fndef (parser, true, true, true, true,
5098 true, NULL, vNULL, NULL,
5099 &fallthru_attr_p);
5100 if (last_stmt && !fallthru_attr_p)
5101 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5102 "ISO C90 forbids mixed declarations and code");
5103 last_stmt = fallthru_attr_p;
5105 else if (!last_label
5106 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5108 /* __extension__ can start a declaration, but is also an
5109 unary operator that can start an expression. Consume all
5110 but the last of a possible series of __extension__ to
5111 determine which. */
5112 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5113 && (c_parser_peek_2nd_token (parser)->keyword
5114 == RID_EXTENSION))
5115 c_parser_consume_token (parser);
5116 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5118 int ext;
5119 ext = disable_extension_diagnostics ();
5120 c_parser_consume_token (parser);
5121 last_label = false;
5122 mark_valid_location_for_stdc_pragma (false);
5123 c_parser_declaration_or_fndef (parser, true, true, true, true,
5124 true, NULL, vNULL);
5125 /* Following the old parser, __extension__ does not
5126 disable this diagnostic. */
5127 restore_extension_diagnostics (ext);
5128 if (last_stmt)
5129 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5130 "ISO C90 forbids mixed declarations and code");
5131 last_stmt = false;
5133 else
5134 goto statement;
5136 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5138 /* External pragmas, and some omp pragmas, are not associated
5139 with regular c code, and so are not to be considered statements
5140 syntactically. This ensures that the user doesn't put them
5141 places that would turn into syntax errors if the directive
5142 were ignored. */
5143 if (c_parser_pragma (parser,
5144 last_label ? pragma_stmt : pragma_compound,
5145 NULL))
5146 last_label = false, last_stmt = true;
5148 else if (c_parser_next_token_is (parser, CPP_EOF))
5150 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5151 c_parser_error (parser, "expected declaration or statement");
5152 return;
5154 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5156 if (parser->in_if_block)
5158 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5159 error_at (loc, "expected %<}%> before %<else%>");
5160 return;
5162 else
5164 error_at (loc, "%<else%> without a previous %<if%>");
5165 c_parser_consume_token (parser);
5166 continue;
5169 else
5171 statement:
5172 last_label = false;
5173 last_stmt = true;
5174 mark_valid_location_for_stdc_pragma (false);
5175 c_parser_statement_after_labels (parser, NULL);
5178 parser->error = false;
5180 if (last_label)
5181 error_at (label_loc, "label at end of compound statement");
5182 c_parser_consume_token (parser);
5183 /* Restore the value we started with. */
5184 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5187 /* Parse all consecutive labels. */
5189 static void
5190 c_parser_all_labels (c_parser *parser)
5192 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5193 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5194 || (c_parser_next_token_is (parser, CPP_NAME)
5195 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5196 c_parser_label (parser);
5199 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5201 label:
5202 identifier : attributes[opt]
5203 case constant-expression :
5204 default :
5206 GNU extensions:
5208 label:
5209 case constant-expression ... constant-expression :
5211 The use of attributes on labels is a GNU extension. The syntax in
5212 GNU C accepts any expressions without commas, non-constant
5213 expressions being rejected later. */
5215 static void
5216 c_parser_label (c_parser *parser)
5218 location_t loc1 = c_parser_peek_token (parser)->location;
5219 tree label = NULL_TREE;
5221 /* Remember whether this case or a user-defined label is allowed to fall
5222 through to. */
5223 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5225 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5227 tree exp1, exp2;
5228 c_parser_consume_token (parser);
5229 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5230 if (c_parser_next_token_is (parser, CPP_COLON))
5232 c_parser_consume_token (parser);
5233 label = do_case (loc1, exp1, NULL_TREE);
5235 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5237 c_parser_consume_token (parser);
5238 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5239 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5240 label = do_case (loc1, exp1, exp2);
5242 else
5243 c_parser_error (parser, "expected %<:%> or %<...%>");
5245 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5247 c_parser_consume_token (parser);
5248 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5249 label = do_case (loc1, NULL_TREE, NULL_TREE);
5251 else
5253 tree name = c_parser_peek_token (parser)->value;
5254 tree tlab;
5255 tree attrs;
5256 location_t loc2 = c_parser_peek_token (parser)->location;
5257 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5258 c_parser_consume_token (parser);
5259 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5260 c_parser_consume_token (parser);
5261 attrs = c_parser_attributes (parser);
5262 tlab = define_label (loc2, name);
5263 if (tlab)
5265 decl_attributes (&tlab, attrs, 0);
5266 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5269 if (label)
5271 if (TREE_CODE (label) == LABEL_EXPR)
5272 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5273 else
5274 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5276 /* Allow '__attribute__((fallthrough));'. */
5277 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5279 location_t loc = c_parser_peek_token (parser)->location;
5280 tree attrs = c_parser_attributes (parser);
5281 if (attribute_fallthrough_p (attrs))
5283 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5285 tree fn = build_call_expr_internal_loc (loc,
5286 IFN_FALLTHROUGH,
5287 void_type_node, 0);
5288 add_stmt (fn);
5290 else
5291 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5292 "not followed by %<;%>");
5294 else if (attrs != NULL_TREE)
5295 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5296 " can be applied to a null statement");
5298 if (c_parser_next_tokens_start_declaration (parser))
5300 error_at (c_parser_peek_token (parser)->location,
5301 "a label can only be part of a statement and "
5302 "a declaration is not a statement");
5303 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5304 /*static_assert_ok*/ true,
5305 /*empty_ok*/ true, /*nested*/ true,
5306 /*start_attr_ok*/ true, NULL,
5307 vNULL);
5312 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5314 statement:
5315 labeled-statement
5316 compound-statement
5317 expression-statement
5318 selection-statement
5319 iteration-statement
5320 jump-statement
5322 labeled-statement:
5323 label statement
5325 expression-statement:
5326 expression[opt] ;
5328 selection-statement:
5329 if-statement
5330 switch-statement
5332 iteration-statement:
5333 while-statement
5334 do-statement
5335 for-statement
5337 jump-statement:
5338 goto identifier ;
5339 continue ;
5340 break ;
5341 return expression[opt] ;
5343 GNU extensions:
5345 statement:
5346 asm-statement
5348 jump-statement:
5349 goto * expression ;
5351 expression-statement:
5352 attributes ;
5354 Objective-C:
5356 statement:
5357 objc-throw-statement
5358 objc-try-catch-statement
5359 objc-synchronized-statement
5361 objc-throw-statement:
5362 @throw expression ;
5363 @throw ;
5365 OpenACC:
5367 statement:
5368 openacc-construct
5370 openacc-construct:
5371 parallel-construct
5372 kernels-construct
5373 data-construct
5374 loop-construct
5376 parallel-construct:
5377 parallel-directive structured-block
5379 kernels-construct:
5380 kernels-directive structured-block
5382 data-construct:
5383 data-directive structured-block
5385 loop-construct:
5386 loop-directive structured-block
5388 OpenMP:
5390 statement:
5391 openmp-construct
5393 openmp-construct:
5394 parallel-construct
5395 for-construct
5396 simd-construct
5397 for-simd-construct
5398 sections-construct
5399 single-construct
5400 parallel-for-construct
5401 parallel-for-simd-construct
5402 parallel-sections-construct
5403 master-construct
5404 critical-construct
5405 atomic-construct
5406 ordered-construct
5408 parallel-construct:
5409 parallel-directive structured-block
5411 for-construct:
5412 for-directive iteration-statement
5414 simd-construct:
5415 simd-directive iteration-statements
5417 for-simd-construct:
5418 for-simd-directive iteration-statements
5420 sections-construct:
5421 sections-directive section-scope
5423 single-construct:
5424 single-directive structured-block
5426 parallel-for-construct:
5427 parallel-for-directive iteration-statement
5429 parallel-for-simd-construct:
5430 parallel-for-simd-directive iteration-statement
5432 parallel-sections-construct:
5433 parallel-sections-directive section-scope
5435 master-construct:
5436 master-directive structured-block
5438 critical-construct:
5439 critical-directive structured-block
5441 atomic-construct:
5442 atomic-directive expression-statement
5444 ordered-construct:
5445 ordered-directive structured-block
5447 Transactional Memory:
5449 statement:
5450 transaction-statement
5451 transaction-cancel-statement
5453 IF_P is used to track whether there's a (possibly labeled) if statement
5454 which is not enclosed in braces and has an else clause. This is used to
5455 implement -Wparentheses. */
5457 static void
5458 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5460 c_parser_all_labels (parser);
5461 if (loc_after_labels)
5462 *loc_after_labels = c_parser_peek_token (parser)->location;
5463 c_parser_statement_after_labels (parser, if_p, NULL);
5466 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5467 of if-else-if conditions.
5469 IF_P is used to track whether there's a (possibly labeled) if statement
5470 which is not enclosed in braces and has an else clause. This is used to
5471 implement -Wparentheses. */
5473 static void
5474 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5475 vec<tree> *chain)
5477 location_t loc = c_parser_peek_token (parser)->location;
5478 tree stmt = NULL_TREE;
5479 bool in_if_block = parser->in_if_block;
5480 parser->in_if_block = false;
5481 if (if_p != NULL)
5482 *if_p = false;
5483 switch (c_parser_peek_token (parser)->type)
5485 case CPP_OPEN_BRACE:
5486 add_stmt (c_parser_compound_statement (parser));
5487 break;
5488 case CPP_KEYWORD:
5489 switch (c_parser_peek_token (parser)->keyword)
5491 case RID_IF:
5492 c_parser_if_statement (parser, if_p, chain);
5493 break;
5494 case RID_SWITCH:
5495 c_parser_switch_statement (parser, if_p);
5496 break;
5497 case RID_WHILE:
5498 c_parser_while_statement (parser, false, if_p);
5499 break;
5500 case RID_DO:
5501 c_parser_do_statement (parser, false);
5502 break;
5503 case RID_FOR:
5504 c_parser_for_statement (parser, false, if_p);
5505 break;
5506 case RID_CILK_FOR:
5507 if (!flag_cilkplus)
5509 error_at (c_parser_peek_token (parser)->location,
5510 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5511 c_parser_skip_to_end_of_block_or_statement (parser);
5513 else
5514 c_parser_cilk_for (parser, integer_zero_node, if_p);
5515 break;
5516 case RID_CILK_SYNC:
5517 c_parser_consume_token (parser);
5518 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5519 if (!flag_cilkplus)
5520 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5521 else
5522 add_stmt (build_cilk_sync ());
5523 break;
5524 case RID_GOTO:
5525 c_parser_consume_token (parser);
5526 if (c_parser_next_token_is (parser, CPP_NAME))
5528 stmt = c_finish_goto_label (loc,
5529 c_parser_peek_token (parser)->value);
5530 c_parser_consume_token (parser);
5532 else if (c_parser_next_token_is (parser, CPP_MULT))
5534 struct c_expr val;
5536 c_parser_consume_token (parser);
5537 val = c_parser_expression (parser);
5538 if (check_no_cilk (val.value,
5539 "Cilk array notation cannot be used as a computed goto expression",
5540 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5541 loc))
5542 val.value = error_mark_node;
5543 val = convert_lvalue_to_rvalue (loc, val, false, true);
5544 stmt = c_finish_goto_ptr (loc, val.value);
5546 else
5547 c_parser_error (parser, "expected identifier or %<*%>");
5548 goto expect_semicolon;
5549 case RID_CONTINUE:
5550 c_parser_consume_token (parser);
5551 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5552 goto expect_semicolon;
5553 case RID_BREAK:
5554 c_parser_consume_token (parser);
5555 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5556 goto expect_semicolon;
5557 case RID_RETURN:
5558 c_parser_consume_token (parser);
5559 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5561 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5562 c_parser_consume_token (parser);
5564 else
5566 location_t xloc = c_parser_peek_token (parser)->location;
5567 struct c_expr expr = c_parser_expression_conv (parser);
5568 mark_exp_read (expr.value);
5569 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5570 expr.value, expr.original_type);
5571 goto expect_semicolon;
5573 break;
5574 case RID_ASM:
5575 stmt = c_parser_asm_statement (parser);
5576 break;
5577 case RID_TRANSACTION_ATOMIC:
5578 case RID_TRANSACTION_RELAXED:
5579 stmt = c_parser_transaction (parser,
5580 c_parser_peek_token (parser)->keyword);
5581 break;
5582 case RID_TRANSACTION_CANCEL:
5583 stmt = c_parser_transaction_cancel (parser);
5584 goto expect_semicolon;
5585 case RID_AT_THROW:
5586 gcc_assert (c_dialect_objc ());
5587 c_parser_consume_token (parser);
5588 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5590 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5591 c_parser_consume_token (parser);
5593 else
5595 struct c_expr expr = c_parser_expression (parser);
5596 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5597 if (check_no_cilk (expr.value,
5598 "Cilk array notation cannot be used for a throw expression",
5599 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5600 expr.value = error_mark_node;
5601 else
5603 expr.value = c_fully_fold (expr.value, false, NULL);
5604 stmt = objc_build_throw_stmt (loc, expr.value);
5606 goto expect_semicolon;
5608 break;
5609 case RID_AT_TRY:
5610 gcc_assert (c_dialect_objc ());
5611 c_parser_objc_try_catch_finally_statement (parser);
5612 break;
5613 case RID_AT_SYNCHRONIZED:
5614 gcc_assert (c_dialect_objc ());
5615 c_parser_objc_synchronized_statement (parser);
5616 break;
5617 case RID_ATTRIBUTE:
5619 /* Allow '__attribute__((fallthrough));'. */
5620 tree attrs = c_parser_attributes (parser);
5621 if (attribute_fallthrough_p (attrs))
5623 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5625 tree fn = build_call_expr_internal_loc (loc,
5626 IFN_FALLTHROUGH,
5627 void_type_node, 0);
5628 add_stmt (fn);
5629 /* Eat the ';'. */
5630 c_parser_consume_token (parser);
5632 else
5633 warning_at (loc, OPT_Wattributes,
5634 "%<fallthrough%> attribute not followed "
5635 "by %<;%>");
5637 else if (attrs != NULL_TREE)
5638 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5639 " can be applied to a null statement");
5640 break;
5642 default:
5643 goto expr_stmt;
5645 break;
5646 case CPP_SEMICOLON:
5647 c_parser_consume_token (parser);
5648 break;
5649 case CPP_CLOSE_PAREN:
5650 case CPP_CLOSE_SQUARE:
5651 /* Avoid infinite loop in error recovery:
5652 c_parser_skip_until_found stops at a closing nesting
5653 delimiter without consuming it, but here we need to consume
5654 it to proceed further. */
5655 c_parser_error (parser, "expected statement");
5656 c_parser_consume_token (parser);
5657 break;
5658 case CPP_PRAGMA:
5659 c_parser_pragma (parser, pragma_stmt, if_p);
5660 break;
5661 default:
5662 expr_stmt:
5663 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5664 expect_semicolon:
5665 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5666 break;
5668 /* Two cases cannot and do not have line numbers associated: If stmt
5669 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5670 cannot hold line numbers. But that's OK because the statement
5671 will either be changed to a MODIFY_EXPR during gimplification of
5672 the statement expr, or discarded. If stmt was compound, but
5673 without new variables, we will have skipped the creation of a
5674 BIND and will have a bare STATEMENT_LIST. But that's OK because
5675 (recursively) all of the component statements should already have
5676 line numbers assigned. ??? Can we discard no-op statements
5677 earlier? */
5678 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5679 protected_set_expr_location (stmt, loc);
5681 parser->in_if_block = in_if_block;
5684 /* Parse the condition from an if, do, while or for statements. */
5686 static tree
5687 c_parser_condition (c_parser *parser)
5689 location_t loc = c_parser_peek_token (parser)->location;
5690 tree cond;
5691 cond = c_parser_expression_conv (parser).value;
5692 cond = c_objc_common_truthvalue_conversion (loc, cond);
5693 cond = c_fully_fold (cond, false, NULL);
5694 if (warn_sequence_point)
5695 verify_sequence_points (cond);
5696 return cond;
5699 /* Parse a parenthesized condition from an if, do or while statement.
5701 condition:
5702 ( expression )
5704 static tree
5705 c_parser_paren_condition (c_parser *parser)
5707 tree cond;
5708 matching_parens parens;
5709 if (!parens.require_open (parser))
5710 return error_mark_node;
5711 cond = c_parser_condition (parser);
5712 parens.skip_until_found_close (parser);
5713 return cond;
5716 /* Parse a statement which is a block in C99.
5718 IF_P is used to track whether there's a (possibly labeled) if statement
5719 which is not enclosed in braces and has an else clause. This is used to
5720 implement -Wparentheses. */
5722 static tree
5723 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5724 location_t *loc_after_labels)
5726 tree block = c_begin_compound_stmt (flag_isoc99);
5727 location_t loc = c_parser_peek_token (parser)->location;
5728 c_parser_statement (parser, if_p, loc_after_labels);
5729 return c_end_compound_stmt (loc, block, flag_isoc99);
5732 /* Parse the body of an if statement. This is just parsing a
5733 statement but (a) it is a block in C99, (b) we track whether the
5734 body is an if statement for the sake of -Wparentheses warnings, (c)
5735 we handle an empty body specially for the sake of -Wempty-body
5736 warnings, and (d) we call parser_compound_statement directly
5737 because c_parser_statement_after_labels resets
5738 parser->in_if_block.
5740 IF_P is used to track whether there's a (possibly labeled) if statement
5741 which is not enclosed in braces and has an else clause. This is used to
5742 implement -Wparentheses. */
5744 static tree
5745 c_parser_if_body (c_parser *parser, bool *if_p,
5746 const token_indent_info &if_tinfo)
5748 tree block = c_begin_compound_stmt (flag_isoc99);
5749 location_t body_loc = c_parser_peek_token (parser)->location;
5750 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5751 token_indent_info body_tinfo
5752 = get_token_indent_info (c_parser_peek_token (parser));
5754 c_parser_all_labels (parser);
5755 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5757 location_t loc = c_parser_peek_token (parser)->location;
5758 add_stmt (build_empty_stmt (loc));
5759 c_parser_consume_token (parser);
5760 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5761 warning_at (loc, OPT_Wempty_body,
5762 "suggest braces around empty body in an %<if%> statement");
5764 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5765 add_stmt (c_parser_compound_statement (parser));
5766 else
5768 body_loc_after_labels = c_parser_peek_token (parser)->location;
5769 c_parser_statement_after_labels (parser, if_p);
5772 token_indent_info next_tinfo
5773 = get_token_indent_info (c_parser_peek_token (parser));
5774 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5775 if (body_loc_after_labels != UNKNOWN_LOCATION
5776 && next_tinfo.type != CPP_SEMICOLON)
5777 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5778 if_tinfo.location, RID_IF);
5780 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5783 /* Parse the else body of an if statement. This is just parsing a
5784 statement but (a) it is a block in C99, (b) we handle an empty body
5785 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5786 of if-else-if conditions. */
5788 static tree
5789 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5790 vec<tree> *chain)
5792 location_t body_loc = c_parser_peek_token (parser)->location;
5793 tree block = c_begin_compound_stmt (flag_isoc99);
5794 token_indent_info body_tinfo
5795 = get_token_indent_info (c_parser_peek_token (parser));
5796 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5798 c_parser_all_labels (parser);
5799 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5801 location_t loc = c_parser_peek_token (parser)->location;
5802 warning_at (loc,
5803 OPT_Wempty_body,
5804 "suggest braces around empty body in an %<else%> statement");
5805 add_stmt (build_empty_stmt (loc));
5806 c_parser_consume_token (parser);
5808 else
5810 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5811 body_loc_after_labels = c_parser_peek_token (parser)->location;
5812 c_parser_statement_after_labels (parser, NULL, chain);
5815 token_indent_info next_tinfo
5816 = get_token_indent_info (c_parser_peek_token (parser));
5817 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5818 if (body_loc_after_labels != UNKNOWN_LOCATION
5819 && next_tinfo.type != CPP_SEMICOLON)
5820 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5821 else_tinfo.location, RID_ELSE);
5823 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5826 /* We might need to reclassify any previously-lexed identifier, e.g.
5827 when we've left a for loop with an if-statement without else in the
5828 body - we might have used a wrong scope for the token. See PR67784. */
5830 static void
5831 c_parser_maybe_reclassify_token (c_parser *parser)
5833 if (c_parser_next_token_is (parser, CPP_NAME))
5835 c_token *token = c_parser_peek_token (parser);
5837 if (token->id_kind != C_ID_CLASSNAME)
5839 tree decl = lookup_name (token->value);
5841 token->id_kind = C_ID_ID;
5842 if (decl)
5844 if (TREE_CODE (decl) == TYPE_DECL)
5845 token->id_kind = C_ID_TYPENAME;
5847 else if (c_dialect_objc ())
5849 tree objc_interface_decl = objc_is_class_name (token->value);
5850 /* Objective-C class names are in the same namespace as
5851 variables and typedefs, and hence are shadowed by local
5852 declarations. */
5853 if (objc_interface_decl)
5855 token->value = objc_interface_decl;
5856 token->id_kind = C_ID_CLASSNAME;
5863 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5865 if-statement:
5866 if ( expression ) statement
5867 if ( expression ) statement else statement
5869 CHAIN is a vector of if-else-if conditions.
5870 IF_P is used to track whether there's a (possibly labeled) if statement
5871 which is not enclosed in braces and has an else clause. This is used to
5872 implement -Wparentheses. */
5874 static void
5875 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5877 tree block;
5878 location_t loc;
5879 tree cond;
5880 bool nested_if = false;
5881 tree first_body, second_body;
5882 bool in_if_block;
5883 tree if_stmt;
5885 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5886 token_indent_info if_tinfo
5887 = get_token_indent_info (c_parser_peek_token (parser));
5888 c_parser_consume_token (parser);
5889 block = c_begin_compound_stmt (flag_isoc99);
5890 loc = c_parser_peek_token (parser)->location;
5891 cond = c_parser_paren_condition (parser);
5892 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5894 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5895 cond = error_mark_node;
5897 in_if_block = parser->in_if_block;
5898 parser->in_if_block = true;
5899 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5900 parser->in_if_block = in_if_block;
5902 if (warn_duplicated_cond)
5903 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5905 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5907 token_indent_info else_tinfo
5908 = get_token_indent_info (c_parser_peek_token (parser));
5909 c_parser_consume_token (parser);
5910 if (warn_duplicated_cond)
5912 if (c_parser_next_token_is_keyword (parser, RID_IF)
5913 && chain == NULL)
5915 /* We've got "if (COND) else if (COND2)". Start the
5916 condition chain and add COND as the first element. */
5917 chain = new vec<tree> ();
5918 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5919 chain->safe_push (cond);
5921 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5923 /* This is if-else without subsequent if. Zap the condition
5924 chain; we would have already warned at this point. */
5925 delete chain;
5926 chain = NULL;
5929 second_body = c_parser_else_body (parser, else_tinfo, chain);
5930 /* Set IF_P to true to indicate that this if statement has an
5931 else clause. This may trigger the Wparentheses warning
5932 below when we get back up to the parent if statement. */
5933 if (if_p != NULL)
5934 *if_p = true;
5936 else
5938 second_body = NULL_TREE;
5940 /* Diagnose an ambiguous else if if-then-else is nested inside
5941 if-then. */
5942 if (nested_if)
5943 warning_at (loc, OPT_Wdangling_else,
5944 "suggest explicit braces to avoid ambiguous %<else%>");
5946 if (warn_duplicated_cond)
5948 /* This if statement does not have an else clause. We don't
5949 need the condition chain anymore. */
5950 delete chain;
5951 chain = NULL;
5954 c_finish_if_stmt (loc, cond, first_body, second_body);
5955 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5957 /* If the if statement contains array notations, then we expand them. */
5958 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5959 if_stmt = fix_conditional_array_notations (if_stmt);
5960 add_stmt (if_stmt);
5961 c_parser_maybe_reclassify_token (parser);
5964 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5966 switch-statement:
5967 switch (expression) statement
5970 static void
5971 c_parser_switch_statement (c_parser *parser, bool *if_p)
5973 struct c_expr ce;
5974 tree block, expr, body, save_break;
5975 location_t switch_loc = c_parser_peek_token (parser)->location;
5976 location_t switch_cond_loc;
5977 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5978 c_parser_consume_token (parser);
5979 block = c_begin_compound_stmt (flag_isoc99);
5980 bool explicit_cast_p = false;
5981 matching_parens parens;
5982 if (parens.require_open (parser))
5984 switch_cond_loc = c_parser_peek_token (parser)->location;
5985 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5986 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5987 explicit_cast_p = true;
5988 ce = c_parser_expression (parser);
5989 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5990 expr = ce.value;
5991 /* ??? expr has no valid location? */
5992 if (check_no_cilk (expr,
5993 "Cilk array notation cannot be used as a condition for switch statement",
5994 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5995 switch_cond_loc))
5996 expr = error_mark_node;
5997 parens.skip_until_found_close (parser);
5999 else
6001 switch_cond_loc = UNKNOWN_LOCATION;
6002 expr = error_mark_node;
6003 ce.original_type = error_mark_node;
6005 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
6006 save_break = c_break_label;
6007 c_break_label = NULL_TREE;
6008 location_t loc_after_labels;
6009 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
6010 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6011 location_t next_loc = c_parser_peek_token (parser)->location;
6012 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
6013 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
6014 RID_SWITCH);
6015 c_finish_case (body, ce.original_type);
6016 if (c_break_label)
6018 location_t here = c_parser_peek_token (parser)->location;
6019 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
6020 SET_EXPR_LOCATION (t, here);
6021 add_stmt (t);
6023 c_break_label = save_break;
6024 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
6025 c_parser_maybe_reclassify_token (parser);
6028 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6030 while-statement:
6031 while (expression) statement
6033 IF_P is used to track whether there's a (possibly labeled) if statement
6034 which is not enclosed in braces and has an else clause. This is used to
6035 implement -Wparentheses. */
6037 static void
6038 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
6040 tree block, cond, body, save_break, save_cont;
6041 location_t loc;
6042 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
6043 token_indent_info while_tinfo
6044 = get_token_indent_info (c_parser_peek_token (parser));
6045 c_parser_consume_token (parser);
6046 block = c_begin_compound_stmt (flag_isoc99);
6047 loc = c_parser_peek_token (parser)->location;
6048 cond = c_parser_paren_condition (parser);
6049 if (check_no_cilk (cond,
6050 "Cilk array notation cannot be used as a condition for while statement",
6051 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
6052 cond = error_mark_node;
6053 if (ivdep && cond != error_mark_node)
6054 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6055 build_int_cst (integer_type_node,
6056 annot_expr_ivdep_kind));
6057 save_break = c_break_label;
6058 c_break_label = NULL_TREE;
6059 save_cont = c_cont_label;
6060 c_cont_label = NULL_TREE;
6062 token_indent_info body_tinfo
6063 = get_token_indent_info (c_parser_peek_token (parser));
6065 location_t loc_after_labels;
6066 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6067 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6068 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
6069 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6070 c_parser_maybe_reclassify_token (parser);
6072 token_indent_info next_tinfo
6073 = get_token_indent_info (c_parser_peek_token (parser));
6074 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
6076 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6077 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6078 while_tinfo.location, RID_WHILE);
6080 c_break_label = save_break;
6081 c_cont_label = save_cont;
6084 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6086 do-statement:
6087 do statement while ( expression ) ;
6090 static void
6091 c_parser_do_statement (c_parser *parser, bool ivdep)
6093 tree block, cond, body, save_break, save_cont, new_break, new_cont;
6094 location_t loc;
6095 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
6096 c_parser_consume_token (parser);
6097 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6098 warning_at (c_parser_peek_token (parser)->location,
6099 OPT_Wempty_body,
6100 "suggest braces around empty body in %<do%> statement");
6101 block = c_begin_compound_stmt (flag_isoc99);
6102 loc = c_parser_peek_token (parser)->location;
6103 save_break = c_break_label;
6104 c_break_label = NULL_TREE;
6105 save_cont = c_cont_label;
6106 c_cont_label = NULL_TREE;
6107 body = c_parser_c99_block_statement (parser, NULL);
6108 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
6109 new_break = c_break_label;
6110 c_break_label = save_break;
6111 new_cont = c_cont_label;
6112 c_cont_label = save_cont;
6113 cond = c_parser_paren_condition (parser);
6114 if (check_no_cilk (cond,
6115 "Cilk array notation cannot be used as a condition for a do-while statement",
6116 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
6117 cond = error_mark_node;
6118 if (ivdep && cond != error_mark_node)
6119 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6120 build_int_cst (integer_type_node,
6121 annot_expr_ivdep_kind));
6122 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6123 c_parser_skip_to_end_of_block_or_statement (parser);
6124 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
6125 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6128 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6130 for-statement:
6131 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6132 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6134 The form with a declaration is new in C99.
6136 ??? In accordance with the old parser, the declaration may be a
6137 nested function, which is then rejected in check_for_loop_decls,
6138 but does it make any sense for this to be included in the grammar?
6139 Note in particular that the nested function does not include a
6140 trailing ';', whereas the "declaration" production includes one.
6141 Also, can we reject bad declarations earlier and cheaper than
6142 check_for_loop_decls?
6144 In Objective-C, there are two additional variants:
6146 foreach-statement:
6147 for ( expression in expresssion ) statement
6148 for ( declaration in expression ) statement
6150 This is inconsistent with C, because the second variant is allowed
6151 even if c99 is not enabled.
6153 The rest of the comment documents these Objective-C foreach-statement.
6155 Here is the canonical example of the first variant:
6156 for (object in array) { do something with object }
6157 we call the first expression ("object") the "object_expression" and
6158 the second expression ("array") the "collection_expression".
6159 object_expression must be an lvalue of type "id" (a generic Objective-C
6160 object) because the loop works by assigning to object_expression the
6161 various objects from the collection_expression. collection_expression
6162 must evaluate to something of type "id" which responds to the method
6163 countByEnumeratingWithState:objects:count:.
6165 The canonical example of the second variant is:
6166 for (id object in array) { do something with object }
6167 which is completely equivalent to
6169 id object;
6170 for (object in array) { do something with object }
6172 Note that initizializing 'object' in some way (eg, "for ((object =
6173 xxx) in array) { do something with object }") is possibly
6174 technically valid, but completely pointless as 'object' will be
6175 assigned to something else as soon as the loop starts. We should
6176 most likely reject it (TODO).
6178 The beginning of the Objective-C foreach-statement looks exactly
6179 like the beginning of the for-statement, and we can tell it is a
6180 foreach-statement only because the initial declaration or
6181 expression is terminated by 'in' instead of ';'.
6183 IF_P is used to track whether there's a (possibly labeled) if statement
6184 which is not enclosed in braces and has an else clause. This is used to
6185 implement -Wparentheses. */
6187 static void
6188 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
6190 tree block, cond, incr, save_break, save_cont, body;
6191 /* The following are only used when parsing an ObjC foreach statement. */
6192 tree object_expression;
6193 /* Silence the bogus uninitialized warning. */
6194 tree collection_expression = NULL;
6195 location_t loc = c_parser_peek_token (parser)->location;
6196 location_t for_loc = c_parser_peek_token (parser)->location;
6197 bool is_foreach_statement = false;
6198 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6199 token_indent_info for_tinfo
6200 = get_token_indent_info (c_parser_peek_token (parser));
6201 c_parser_consume_token (parser);
6202 /* Open a compound statement in Objective-C as well, just in case this is
6203 as foreach expression. */
6204 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6205 cond = error_mark_node;
6206 incr = error_mark_node;
6207 matching_parens parens;
6208 if (parens.require_open (parser))
6210 /* Parse the initialization declaration or expression. */
6211 object_expression = error_mark_node;
6212 parser->objc_could_be_foreach_context = c_dialect_objc ();
6213 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6215 parser->objc_could_be_foreach_context = false;
6216 c_parser_consume_token (parser);
6217 c_finish_expr_stmt (loc, NULL_TREE);
6219 else if (c_parser_next_tokens_start_declaration (parser))
6221 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6222 &object_expression, vNULL);
6223 parser->objc_could_be_foreach_context = false;
6225 if (c_parser_next_token_is_keyword (parser, RID_IN))
6227 c_parser_consume_token (parser);
6228 is_foreach_statement = true;
6229 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6230 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6232 else
6233 check_for_loop_decls (for_loc, flag_isoc99);
6235 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6237 /* __extension__ can start a declaration, but is also an
6238 unary operator that can start an expression. Consume all
6239 but the last of a possible series of __extension__ to
6240 determine which. */
6241 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6242 && (c_parser_peek_2nd_token (parser)->keyword
6243 == RID_EXTENSION))
6244 c_parser_consume_token (parser);
6245 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6247 int ext;
6248 ext = disable_extension_diagnostics ();
6249 c_parser_consume_token (parser);
6250 c_parser_declaration_or_fndef (parser, true, true, true, true,
6251 true, &object_expression, vNULL);
6252 parser->objc_could_be_foreach_context = false;
6254 restore_extension_diagnostics (ext);
6255 if (c_parser_next_token_is_keyword (parser, RID_IN))
6257 c_parser_consume_token (parser);
6258 is_foreach_statement = true;
6259 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6260 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6262 else
6263 check_for_loop_decls (for_loc, flag_isoc99);
6265 else
6266 goto init_expr;
6268 else
6270 init_expr:
6272 struct c_expr ce;
6273 tree init_expression;
6274 ce = c_parser_expression (parser);
6275 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
6276 level statement", but it works just fine, so allow it. */
6277 init_expression = ce.value;
6278 parser->objc_could_be_foreach_context = false;
6279 if (c_parser_next_token_is_keyword (parser, RID_IN))
6281 c_parser_consume_token (parser);
6282 is_foreach_statement = true;
6283 if (! lvalue_p (init_expression))
6284 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6285 object_expression = c_fully_fold (init_expression, false, NULL);
6287 else
6289 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6290 init_expression = ce.value;
6291 c_finish_expr_stmt (loc, init_expression);
6292 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6296 /* Parse the loop condition. In the case of a foreach
6297 statement, there is no loop condition. */
6298 gcc_assert (!parser->objc_could_be_foreach_context);
6299 if (!is_foreach_statement)
6301 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6303 if (ivdep)
6305 c_parser_error (parser, "missing loop condition in loop with "
6306 "%<GCC ivdep%> pragma");
6307 cond = error_mark_node;
6309 else
6311 c_parser_consume_token (parser);
6312 cond = NULL_TREE;
6315 else
6317 cond = c_parser_condition (parser);
6318 if (check_no_cilk (cond,
6319 "Cilk array notation cannot be used in a condition for a for-loop",
6320 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
6321 cond = error_mark_node;
6322 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6323 "expected %<;%>");
6325 if (ivdep && cond != error_mark_node)
6326 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6327 build_int_cst (integer_type_node,
6328 annot_expr_ivdep_kind));
6330 /* Parse the increment expression (the third expression in a
6331 for-statement). In the case of a foreach-statement, this is
6332 the expression that follows the 'in'. */
6333 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6335 if (is_foreach_statement)
6337 c_parser_error (parser, "missing collection in fast enumeration");
6338 collection_expression = error_mark_node;
6340 else
6341 incr = c_process_expr_stmt (loc, NULL_TREE);
6343 else
6345 if (is_foreach_statement)
6346 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6347 false, NULL);
6348 else
6350 struct c_expr ce = c_parser_expression (parser);
6351 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6352 incr = c_process_expr_stmt (loc, ce.value);
6355 parens.skip_until_found_close (parser);
6357 save_break = c_break_label;
6358 c_break_label = NULL_TREE;
6359 save_cont = c_cont_label;
6360 c_cont_label = NULL_TREE;
6362 token_indent_info body_tinfo
6363 = get_token_indent_info (c_parser_peek_token (parser));
6365 location_t loc_after_labels;
6366 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6367 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6369 if (is_foreach_statement)
6370 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6371 else
6372 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6373 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6374 c_parser_maybe_reclassify_token (parser);
6376 token_indent_info next_tinfo
6377 = get_token_indent_info (c_parser_peek_token (parser));
6378 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6380 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6381 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6382 for_tinfo.location, RID_FOR);
6384 c_break_label = save_break;
6385 c_cont_label = save_cont;
6388 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6389 statement with inputs, outputs, clobbers, and volatile tag
6390 allowed.
6392 asm-statement:
6393 asm type-qualifier[opt] ( asm-argument ) ;
6394 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6396 asm-argument:
6397 asm-string-literal
6398 asm-string-literal : asm-operands[opt]
6399 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6400 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6402 asm-goto-argument:
6403 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6404 : asm-goto-operands
6406 Qualifiers other than volatile are accepted in the syntax but
6407 warned for. */
6409 static tree
6410 c_parser_asm_statement (c_parser *parser)
6412 tree quals, str, outputs, inputs, clobbers, labels, ret;
6413 bool simple, is_goto;
6414 location_t asm_loc = c_parser_peek_token (parser)->location;
6415 int section, nsections;
6417 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6418 c_parser_consume_token (parser);
6419 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6421 quals = c_parser_peek_token (parser)->value;
6422 c_parser_consume_token (parser);
6424 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6425 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6427 warning_at (c_parser_peek_token (parser)->location,
6429 "%E qualifier ignored on asm",
6430 c_parser_peek_token (parser)->value);
6431 quals = NULL_TREE;
6432 c_parser_consume_token (parser);
6434 else
6435 quals = NULL_TREE;
6437 is_goto = false;
6438 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6440 c_parser_consume_token (parser);
6441 is_goto = true;
6444 /* ??? Follow the C++ parser rather than using the
6445 lex_untranslated_string kludge. */
6446 parser->lex_untranslated_string = true;
6447 ret = NULL;
6449 matching_parens parens;
6450 if (!parens.require_open (parser))
6451 goto error;
6453 str = c_parser_asm_string_literal (parser);
6454 if (str == NULL_TREE)
6455 goto error_close_paren;
6457 simple = true;
6458 outputs = NULL_TREE;
6459 inputs = NULL_TREE;
6460 clobbers = NULL_TREE;
6461 labels = NULL_TREE;
6463 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6464 goto done_asm;
6466 /* Parse each colon-delimited section of operands. */
6467 nsections = 3 + is_goto;
6468 for (section = 0; section < nsections; ++section)
6470 if (!c_parser_require (parser, CPP_COLON,
6471 is_goto
6472 ? G_("expected %<:%>")
6473 : G_("expected %<:%> or %<)%>"),
6474 UNKNOWN_LOCATION, is_goto))
6475 goto error_close_paren;
6477 /* Once past any colon, we're no longer a simple asm. */
6478 simple = false;
6480 if ((!c_parser_next_token_is (parser, CPP_COLON)
6481 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6482 || section == 3)
6483 switch (section)
6485 case 0:
6486 /* For asm goto, we don't allow output operands, but reserve
6487 the slot for a future extension that does allow them. */
6488 if (!is_goto)
6489 outputs = c_parser_asm_operands (parser);
6490 break;
6491 case 1:
6492 inputs = c_parser_asm_operands (parser);
6493 break;
6494 case 2:
6495 clobbers = c_parser_asm_clobbers (parser);
6496 break;
6497 case 3:
6498 labels = c_parser_asm_goto_operands (parser);
6499 break;
6500 default:
6501 gcc_unreachable ();
6504 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6505 goto done_asm;
6508 done_asm:
6509 if (!parens.require_close (parser))
6511 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6512 goto error;
6515 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6516 c_parser_skip_to_end_of_block_or_statement (parser);
6518 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6519 clobbers, labels, simple));
6521 error:
6522 parser->lex_untranslated_string = false;
6523 return ret;
6525 error_close_paren:
6526 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6527 goto error;
6530 /* Parse asm operands, a GNU extension.
6532 asm-operands:
6533 asm-operand
6534 asm-operands , asm-operand
6536 asm-operand:
6537 asm-string-literal ( expression )
6538 [ identifier ] asm-string-literal ( expression )
6541 static tree
6542 c_parser_asm_operands (c_parser *parser)
6544 tree list = NULL_TREE;
6545 while (true)
6547 tree name, str;
6548 struct c_expr expr;
6549 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6551 c_parser_consume_token (parser);
6552 if (c_parser_next_token_is (parser, CPP_NAME))
6554 tree id = c_parser_peek_token (parser)->value;
6555 c_parser_consume_token (parser);
6556 name = build_string (IDENTIFIER_LENGTH (id),
6557 IDENTIFIER_POINTER (id));
6559 else
6561 c_parser_error (parser, "expected identifier");
6562 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6563 return NULL_TREE;
6565 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6566 "expected %<]%>");
6568 else
6569 name = NULL_TREE;
6570 str = c_parser_asm_string_literal (parser);
6571 if (str == NULL_TREE)
6572 return NULL_TREE;
6573 parser->lex_untranslated_string = false;
6574 matching_parens parens;
6575 if (!parens.require_open (parser))
6577 parser->lex_untranslated_string = true;
6578 return NULL_TREE;
6580 expr = c_parser_expression (parser);
6581 mark_exp_read (expr.value);
6582 parser->lex_untranslated_string = true;
6583 if (!parens.require_close (parser))
6585 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6586 return NULL_TREE;
6588 list = chainon (list, build_tree_list (build_tree_list (name, str),
6589 expr.value));
6590 if (c_parser_next_token_is (parser, CPP_COMMA))
6591 c_parser_consume_token (parser);
6592 else
6593 break;
6595 return list;
6598 /* Parse asm clobbers, a GNU extension.
6600 asm-clobbers:
6601 asm-string-literal
6602 asm-clobbers , asm-string-literal
6605 static tree
6606 c_parser_asm_clobbers (c_parser *parser)
6608 tree list = NULL_TREE;
6609 while (true)
6611 tree str = c_parser_asm_string_literal (parser);
6612 if (str)
6613 list = tree_cons (NULL_TREE, str, list);
6614 else
6615 return NULL_TREE;
6616 if (c_parser_next_token_is (parser, CPP_COMMA))
6617 c_parser_consume_token (parser);
6618 else
6619 break;
6621 return list;
6624 /* Parse asm goto labels, a GNU extension.
6626 asm-goto-operands:
6627 identifier
6628 asm-goto-operands , identifier
6631 static tree
6632 c_parser_asm_goto_operands (c_parser *parser)
6634 tree list = NULL_TREE;
6635 while (true)
6637 tree name, label;
6639 if (c_parser_next_token_is (parser, CPP_NAME))
6641 c_token *tok = c_parser_peek_token (parser);
6642 name = tok->value;
6643 label = lookup_label_for_goto (tok->location, name);
6644 c_parser_consume_token (parser);
6645 TREE_USED (label) = 1;
6647 else
6649 c_parser_error (parser, "expected identifier");
6650 return NULL_TREE;
6653 name = build_string (IDENTIFIER_LENGTH (name),
6654 IDENTIFIER_POINTER (name));
6655 list = tree_cons (name, label, list);
6656 if (c_parser_next_token_is (parser, CPP_COMMA))
6657 c_parser_consume_token (parser);
6658 else
6659 return nreverse (list);
6663 /* Parse an expression other than a compound expression; that is, an
6664 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6665 AFTER is not NULL then it is an Objective-C message expression which
6666 is the primary-expression starting the expression as an initializer.
6668 assignment-expression:
6669 conditional-expression
6670 unary-expression assignment-operator assignment-expression
6672 assignment-operator: one of
6673 = *= /= %= += -= <<= >>= &= ^= |=
6675 In GNU C we accept any conditional expression on the LHS and
6676 diagnose the invalid lvalue rather than producing a syntax
6677 error. */
6679 static struct c_expr
6680 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6681 tree omp_atomic_lhs)
6683 struct c_expr lhs, rhs, ret;
6684 enum tree_code code;
6685 location_t op_location, exp_location;
6686 gcc_assert (!after || c_dialect_objc ());
6687 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6688 op_location = c_parser_peek_token (parser)->location;
6689 switch (c_parser_peek_token (parser)->type)
6691 case CPP_EQ:
6692 code = NOP_EXPR;
6693 break;
6694 case CPP_MULT_EQ:
6695 code = MULT_EXPR;
6696 break;
6697 case CPP_DIV_EQ:
6698 code = TRUNC_DIV_EXPR;
6699 break;
6700 case CPP_MOD_EQ:
6701 code = TRUNC_MOD_EXPR;
6702 break;
6703 case CPP_PLUS_EQ:
6704 code = PLUS_EXPR;
6705 break;
6706 case CPP_MINUS_EQ:
6707 code = MINUS_EXPR;
6708 break;
6709 case CPP_LSHIFT_EQ:
6710 code = LSHIFT_EXPR;
6711 break;
6712 case CPP_RSHIFT_EQ:
6713 code = RSHIFT_EXPR;
6714 break;
6715 case CPP_AND_EQ:
6716 code = BIT_AND_EXPR;
6717 break;
6718 case CPP_XOR_EQ:
6719 code = BIT_XOR_EXPR;
6720 break;
6721 case CPP_OR_EQ:
6722 code = BIT_IOR_EXPR;
6723 break;
6724 default:
6725 return lhs;
6727 c_parser_consume_token (parser);
6728 exp_location = c_parser_peek_token (parser)->location;
6729 rhs = c_parser_expr_no_commas (parser, NULL);
6730 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6732 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6733 code, exp_location, rhs.value,
6734 rhs.original_type);
6735 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6736 if (code == NOP_EXPR)
6737 ret.original_code = MODIFY_EXPR;
6738 else
6740 TREE_NO_WARNING (ret.value) = 1;
6741 ret.original_code = ERROR_MARK;
6743 ret.original_type = NULL;
6744 return ret;
6747 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6748 AFTER is not NULL then it is an Objective-C message expression which is
6749 the primary-expression starting the expression as an initializer.
6751 conditional-expression:
6752 logical-OR-expression
6753 logical-OR-expression ? expression : conditional-expression
6755 GNU extensions:
6757 conditional-expression:
6758 logical-OR-expression ? : conditional-expression
6761 static struct c_expr
6762 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6763 tree omp_atomic_lhs)
6765 struct c_expr cond, exp1, exp2, ret;
6766 location_t start, cond_loc, colon_loc;
6768 gcc_assert (!after || c_dialect_objc ());
6770 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6772 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6773 return cond;
6774 if (cond.value != error_mark_node)
6775 start = cond.get_start ();
6776 else
6777 start = UNKNOWN_LOCATION;
6778 cond_loc = c_parser_peek_token (parser)->location;
6779 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6780 c_parser_consume_token (parser);
6781 if (c_parser_next_token_is (parser, CPP_COLON))
6783 tree eptype = NULL_TREE;
6785 location_t middle_loc = c_parser_peek_token (parser)->location;
6786 pedwarn (middle_loc, OPT_Wpedantic,
6787 "ISO C forbids omitting the middle term of a ?: expression");
6788 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6790 eptype = TREE_TYPE (cond.value);
6791 cond.value = TREE_OPERAND (cond.value, 0);
6793 tree e = cond.value;
6794 while (TREE_CODE (e) == COMPOUND_EXPR)
6795 e = TREE_OPERAND (e, 1);
6796 warn_for_omitted_condop (middle_loc, e);
6797 /* Make sure first operand is calculated only once. */
6798 exp1.value = save_expr (default_conversion (cond.value));
6799 if (eptype)
6800 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6801 exp1.original_type = NULL;
6802 exp1.src_range = cond.src_range;
6803 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6804 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6806 else
6808 cond.value
6809 = c_objc_common_truthvalue_conversion
6810 (cond_loc, default_conversion (cond.value));
6811 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6812 exp1 = c_parser_expression_conv (parser);
6813 mark_exp_read (exp1.value);
6814 c_inhibit_evaluation_warnings +=
6815 ((cond.value == truthvalue_true_node)
6816 - (cond.value == truthvalue_false_node));
6819 colon_loc = c_parser_peek_token (parser)->location;
6820 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6822 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6823 ret.value = error_mark_node;
6824 ret.original_code = ERROR_MARK;
6825 ret.original_type = NULL;
6826 return ret;
6829 location_t exp2_loc = c_parser_peek_token (parser)->location;
6830 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6831 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6833 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6834 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6835 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6836 ret.value = build_conditional_expr (colon_loc, cond.value,
6837 cond.original_code == C_MAYBE_CONST_EXPR,
6838 exp1.value, exp1.original_type, loc1,
6839 exp2.value, exp2.original_type, loc2);
6840 ret.original_code = ERROR_MARK;
6841 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6842 ret.original_type = NULL;
6843 else
6845 tree t1, t2;
6847 /* If both sides are enum type, the default conversion will have
6848 made the type of the result be an integer type. We want to
6849 remember the enum types we started with. */
6850 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6851 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6852 ret.original_type = ((t1 != error_mark_node
6853 && t2 != error_mark_node
6854 && (TYPE_MAIN_VARIANT (t1)
6855 == TYPE_MAIN_VARIANT (t2)))
6856 ? t1
6857 : NULL);
6859 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6860 return ret;
6863 /* Parse a binary expression; that is, a logical-OR-expression (C90
6864 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6865 NULL then it is an Objective-C message expression which is the
6866 primary-expression starting the expression as an initializer.
6868 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6869 when it should be the unfolded lhs. In a valid OpenMP source,
6870 one of the operands of the toplevel binary expression must be equal
6871 to it. In that case, just return a build2 created binary operation
6872 rather than result of parser_build_binary_op.
6874 multiplicative-expression:
6875 cast-expression
6876 multiplicative-expression * cast-expression
6877 multiplicative-expression / cast-expression
6878 multiplicative-expression % cast-expression
6880 additive-expression:
6881 multiplicative-expression
6882 additive-expression + multiplicative-expression
6883 additive-expression - multiplicative-expression
6885 shift-expression:
6886 additive-expression
6887 shift-expression << additive-expression
6888 shift-expression >> additive-expression
6890 relational-expression:
6891 shift-expression
6892 relational-expression < shift-expression
6893 relational-expression > shift-expression
6894 relational-expression <= shift-expression
6895 relational-expression >= shift-expression
6897 equality-expression:
6898 relational-expression
6899 equality-expression == relational-expression
6900 equality-expression != relational-expression
6902 AND-expression:
6903 equality-expression
6904 AND-expression & equality-expression
6906 exclusive-OR-expression:
6907 AND-expression
6908 exclusive-OR-expression ^ AND-expression
6910 inclusive-OR-expression:
6911 exclusive-OR-expression
6912 inclusive-OR-expression | exclusive-OR-expression
6914 logical-AND-expression:
6915 inclusive-OR-expression
6916 logical-AND-expression && inclusive-OR-expression
6918 logical-OR-expression:
6919 logical-AND-expression
6920 logical-OR-expression || logical-AND-expression
6923 static struct c_expr
6924 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6925 tree omp_atomic_lhs)
6927 /* A binary expression is parsed using operator-precedence parsing,
6928 with the operands being cast expressions. All the binary
6929 operators are left-associative. Thus a binary expression is of
6930 form:
6932 E0 op1 E1 op2 E2 ...
6934 which we represent on a stack. On the stack, the precedence
6935 levels are strictly increasing. When a new operator is
6936 encountered of higher precedence than that at the top of the
6937 stack, it is pushed; its LHS is the top expression, and its RHS
6938 is everything parsed until it is popped. When a new operator is
6939 encountered with precedence less than or equal to that at the top
6940 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6941 by the result of the operation until the operator at the top of
6942 the stack has lower precedence than the new operator or there is
6943 only one element on the stack; then the top expression is the LHS
6944 of the new operator. In the case of logical AND and OR
6945 expressions, we also need to adjust c_inhibit_evaluation_warnings
6946 as appropriate when the operators are pushed and popped. */
6948 struct {
6949 /* The expression at this stack level. */
6950 struct c_expr expr;
6951 /* The precedence of the operator on its left, PREC_NONE at the
6952 bottom of the stack. */
6953 enum c_parser_prec prec;
6954 /* The operation on its left. */
6955 enum tree_code op;
6956 /* The source location of this operation. */
6957 location_t loc;
6958 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6959 tree sizeof_arg;
6960 } stack[NUM_PRECS];
6961 int sp;
6962 /* Location of the binary operator. */
6963 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6964 #define POP \
6965 do { \
6966 switch (stack[sp].op) \
6968 case TRUTH_ANDIF_EXPR: \
6969 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6970 == truthvalue_false_node); \
6971 break; \
6972 case TRUTH_ORIF_EXPR: \
6973 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6974 == truthvalue_true_node); \
6975 break; \
6976 case TRUNC_DIV_EXPR: \
6977 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6978 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6980 tree type0 = stack[sp - 1].sizeof_arg; \
6981 tree type1 = stack[sp].sizeof_arg; \
6982 tree first_arg = type0; \
6983 if (!TYPE_P (type0)) \
6984 type0 = TREE_TYPE (type0); \
6985 if (!TYPE_P (type1)) \
6986 type1 = TREE_TYPE (type1); \
6987 if (POINTER_TYPE_P (type0) \
6988 && comptypes (TREE_TYPE (type0), type1) \
6989 && !(TREE_CODE (first_arg) == PARM_DECL \
6990 && C_ARRAY_PARAMETER (first_arg) \
6991 && warn_sizeof_array_argument)) \
6992 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6993 "division %<sizeof (%T) / sizeof (%T)%> does " \
6994 "not compute the number of array elements", \
6995 type0, type1)) \
6996 if (DECL_P (first_arg)) \
6997 inform (DECL_SOURCE_LOCATION (first_arg), \
6998 "first %<sizeof%> operand was declared here"); \
7000 break; \
7001 default: \
7002 break; \
7004 stack[sp - 1].expr \
7005 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
7006 stack[sp - 1].expr, true, true); \
7007 stack[sp].expr \
7008 = convert_lvalue_to_rvalue (stack[sp].loc, \
7009 stack[sp].expr, true, true); \
7010 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
7011 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
7012 && ((1 << stack[sp].prec) \
7013 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
7014 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
7015 && stack[sp].op != TRUNC_MOD_EXPR \
7016 && stack[0].expr.value != error_mark_node \
7017 && stack[1].expr.value != error_mark_node \
7018 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
7019 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
7020 stack[0].expr.value \
7021 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
7022 stack[0].expr.value, stack[1].expr.value); \
7023 else \
7024 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
7025 stack[sp].op, \
7026 stack[sp - 1].expr, \
7027 stack[sp].expr); \
7028 sp--; \
7029 } while (0)
7030 gcc_assert (!after || c_dialect_objc ());
7031 stack[0].loc = c_parser_peek_token (parser)->location;
7032 stack[0].expr = c_parser_cast_expression (parser, after);
7033 stack[0].prec = PREC_NONE;
7034 stack[0].sizeof_arg = c_last_sizeof_arg;
7035 sp = 0;
7036 while (true)
7038 enum c_parser_prec oprec;
7039 enum tree_code ocode;
7040 source_range src_range;
7041 if (parser->error)
7042 goto out;
7043 switch (c_parser_peek_token (parser)->type)
7045 case CPP_MULT:
7046 oprec = PREC_MULT;
7047 ocode = MULT_EXPR;
7048 break;
7049 case CPP_DIV:
7050 oprec = PREC_MULT;
7051 ocode = TRUNC_DIV_EXPR;
7052 break;
7053 case CPP_MOD:
7054 oprec = PREC_MULT;
7055 ocode = TRUNC_MOD_EXPR;
7056 break;
7057 case CPP_PLUS:
7058 oprec = PREC_ADD;
7059 ocode = PLUS_EXPR;
7060 break;
7061 case CPP_MINUS:
7062 oprec = PREC_ADD;
7063 ocode = MINUS_EXPR;
7064 break;
7065 case CPP_LSHIFT:
7066 oprec = PREC_SHIFT;
7067 ocode = LSHIFT_EXPR;
7068 break;
7069 case CPP_RSHIFT:
7070 oprec = PREC_SHIFT;
7071 ocode = RSHIFT_EXPR;
7072 break;
7073 case CPP_LESS:
7074 oprec = PREC_REL;
7075 ocode = LT_EXPR;
7076 break;
7077 case CPP_GREATER:
7078 oprec = PREC_REL;
7079 ocode = GT_EXPR;
7080 break;
7081 case CPP_LESS_EQ:
7082 oprec = PREC_REL;
7083 ocode = LE_EXPR;
7084 break;
7085 case CPP_GREATER_EQ:
7086 oprec = PREC_REL;
7087 ocode = GE_EXPR;
7088 break;
7089 case CPP_EQ_EQ:
7090 oprec = PREC_EQ;
7091 ocode = EQ_EXPR;
7092 break;
7093 case CPP_NOT_EQ:
7094 oprec = PREC_EQ;
7095 ocode = NE_EXPR;
7096 break;
7097 case CPP_AND:
7098 oprec = PREC_BITAND;
7099 ocode = BIT_AND_EXPR;
7100 break;
7101 case CPP_XOR:
7102 oprec = PREC_BITXOR;
7103 ocode = BIT_XOR_EXPR;
7104 break;
7105 case CPP_OR:
7106 oprec = PREC_BITOR;
7107 ocode = BIT_IOR_EXPR;
7108 break;
7109 case CPP_AND_AND:
7110 oprec = PREC_LOGAND;
7111 ocode = TRUTH_ANDIF_EXPR;
7112 break;
7113 case CPP_OR_OR:
7114 oprec = PREC_LOGOR;
7115 ocode = TRUTH_ORIF_EXPR;
7116 break;
7117 default:
7118 /* Not a binary operator, so end of the binary
7119 expression. */
7120 goto out;
7122 binary_loc = c_parser_peek_token (parser)->location;
7123 while (oprec <= stack[sp].prec)
7124 POP;
7125 c_parser_consume_token (parser);
7126 switch (ocode)
7128 case TRUTH_ANDIF_EXPR:
7129 src_range = stack[sp].expr.src_range;
7130 stack[sp].expr
7131 = convert_lvalue_to_rvalue (stack[sp].loc,
7132 stack[sp].expr, true, true);
7133 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7134 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7135 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7136 == truthvalue_false_node);
7137 set_c_expr_source_range (&stack[sp].expr, src_range);
7138 break;
7139 case TRUTH_ORIF_EXPR:
7140 src_range = stack[sp].expr.src_range;
7141 stack[sp].expr
7142 = convert_lvalue_to_rvalue (stack[sp].loc,
7143 stack[sp].expr, true, true);
7144 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7145 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7146 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7147 == truthvalue_true_node);
7148 set_c_expr_source_range (&stack[sp].expr, src_range);
7149 break;
7150 default:
7151 break;
7153 sp++;
7154 stack[sp].loc = binary_loc;
7155 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7156 stack[sp].prec = oprec;
7157 stack[sp].op = ocode;
7158 stack[sp].sizeof_arg = c_last_sizeof_arg;
7160 out:
7161 while (sp > 0)
7162 POP;
7163 return stack[0].expr;
7164 #undef POP
7167 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7168 is not NULL then it is an Objective-C message expression which is the
7169 primary-expression starting the expression as an initializer.
7171 cast-expression:
7172 unary-expression
7173 ( type-name ) unary-expression
7176 static struct c_expr
7177 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7179 location_t cast_loc = c_parser_peek_token (parser)->location;
7180 gcc_assert (!after || c_dialect_objc ());
7181 if (after)
7182 return c_parser_postfix_expression_after_primary (parser,
7183 cast_loc, *after);
7184 /* If the expression begins with a parenthesized type name, it may
7185 be either a cast or a compound literal; we need to see whether
7186 the next character is '{' to tell the difference. If not, it is
7187 an unary expression. Full detection of unknown typenames here
7188 would require a 3-token lookahead. */
7189 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7190 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7192 struct c_type_name *type_name;
7193 struct c_expr ret;
7194 struct c_expr expr;
7195 matching_parens parens;
7196 parens.consume_open (parser);
7197 type_name = c_parser_type_name (parser);
7198 parens.skip_until_found_close (parser);
7199 if (type_name == NULL)
7201 ret.value = error_mark_node;
7202 ret.original_code = ERROR_MARK;
7203 ret.original_type = NULL;
7204 return ret;
7207 /* Save casted types in the function's used types hash table. */
7208 used_types_insert (type_name->specs->type);
7210 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7211 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7212 cast_loc);
7214 location_t expr_loc = c_parser_peek_token (parser)->location;
7215 expr = c_parser_cast_expression (parser, NULL);
7216 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7218 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7219 if (ret.value && expr.value)
7220 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7221 ret.original_code = ERROR_MARK;
7222 ret.original_type = NULL;
7223 return ret;
7225 else
7226 return c_parser_unary_expression (parser);
7229 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7231 unary-expression:
7232 postfix-expression
7233 ++ unary-expression
7234 -- unary-expression
7235 unary-operator cast-expression
7236 sizeof unary-expression
7237 sizeof ( type-name )
7239 unary-operator: one of
7240 & * + - ~ !
7242 GNU extensions:
7244 unary-expression:
7245 __alignof__ unary-expression
7246 __alignof__ ( type-name )
7247 && identifier
7249 (C11 permits _Alignof with type names only.)
7251 unary-operator: one of
7252 __extension__ __real__ __imag__
7254 Transactional Memory:
7256 unary-expression:
7257 transaction-expression
7259 In addition, the GNU syntax treats ++ and -- as unary operators, so
7260 they may be applied to cast expressions with errors for non-lvalues
7261 given later. */
7263 static struct c_expr
7264 c_parser_unary_expression (c_parser *parser)
7266 int ext;
7267 struct c_expr ret, op;
7268 location_t op_loc = c_parser_peek_token (parser)->location;
7269 location_t exp_loc;
7270 location_t finish;
7271 ret.original_code = ERROR_MARK;
7272 ret.original_type = NULL;
7273 switch (c_parser_peek_token (parser)->type)
7275 case CPP_PLUS_PLUS:
7276 c_parser_consume_token (parser);
7277 exp_loc = c_parser_peek_token (parser)->location;
7278 op = c_parser_cast_expression (parser, NULL);
7280 /* If there is array notations in op, we expand them. */
7281 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
7282 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
7283 else
7285 op = default_function_array_read_conversion (exp_loc, op);
7286 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7288 case CPP_MINUS_MINUS:
7289 c_parser_consume_token (parser);
7290 exp_loc = c_parser_peek_token (parser)->location;
7291 op = c_parser_cast_expression (parser, NULL);
7293 /* If there is array notations in op, we expand them. */
7294 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
7295 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
7296 else
7298 op = default_function_array_read_conversion (exp_loc, op);
7299 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7301 case CPP_AND:
7302 c_parser_consume_token (parser);
7303 op = c_parser_cast_expression (parser, NULL);
7304 mark_exp_read (op.value);
7305 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7306 case CPP_MULT:
7308 c_parser_consume_token (parser);
7309 exp_loc = c_parser_peek_token (parser)->location;
7310 op = c_parser_cast_expression (parser, NULL);
7311 finish = op.get_finish ();
7312 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7313 location_t combined_loc = make_location (op_loc, op_loc, finish);
7314 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7315 ret.src_range.m_start = op_loc;
7316 ret.src_range.m_finish = finish;
7317 return ret;
7319 case CPP_PLUS:
7320 if (!c_dialect_objc () && !in_system_header_at (input_location))
7321 warning_at (op_loc,
7322 OPT_Wtraditional,
7323 "traditional C rejects the unary plus operator");
7324 c_parser_consume_token (parser);
7325 exp_loc = c_parser_peek_token (parser)->location;
7326 op = c_parser_cast_expression (parser, NULL);
7327 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7328 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7329 case CPP_MINUS:
7330 c_parser_consume_token (parser);
7331 exp_loc = c_parser_peek_token (parser)->location;
7332 op = c_parser_cast_expression (parser, NULL);
7333 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7334 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7335 case CPP_COMPL:
7336 c_parser_consume_token (parser);
7337 exp_loc = c_parser_peek_token (parser)->location;
7338 op = c_parser_cast_expression (parser, NULL);
7339 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7340 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7341 case CPP_NOT:
7342 c_parser_consume_token (parser);
7343 exp_loc = c_parser_peek_token (parser)->location;
7344 op = c_parser_cast_expression (parser, NULL);
7345 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7346 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7347 case CPP_AND_AND:
7348 /* Refer to the address of a label as a pointer. */
7349 c_parser_consume_token (parser);
7350 if (c_parser_next_token_is (parser, CPP_NAME))
7352 ret.value = finish_label_address_expr
7353 (c_parser_peek_token (parser)->value, op_loc);
7354 set_c_expr_source_range (&ret, op_loc,
7355 c_parser_peek_token (parser)->get_finish ());
7356 c_parser_consume_token (parser);
7358 else
7360 c_parser_error (parser, "expected identifier");
7361 ret.set_error ();
7363 return ret;
7364 case CPP_KEYWORD:
7365 switch (c_parser_peek_token (parser)->keyword)
7367 case RID_SIZEOF:
7368 return c_parser_sizeof_expression (parser);
7369 case RID_ALIGNOF:
7370 return c_parser_alignof_expression (parser);
7371 case RID_EXTENSION:
7372 c_parser_consume_token (parser);
7373 ext = disable_extension_diagnostics ();
7374 ret = c_parser_cast_expression (parser, NULL);
7375 restore_extension_diagnostics (ext);
7376 return ret;
7377 case RID_REALPART:
7378 c_parser_consume_token (parser);
7379 exp_loc = c_parser_peek_token (parser)->location;
7380 op = c_parser_cast_expression (parser, NULL);
7381 op = default_function_array_conversion (exp_loc, op);
7382 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7383 case RID_IMAGPART:
7384 c_parser_consume_token (parser);
7385 exp_loc = c_parser_peek_token (parser)->location;
7386 op = c_parser_cast_expression (parser, NULL);
7387 op = default_function_array_conversion (exp_loc, op);
7388 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7389 case RID_TRANSACTION_ATOMIC:
7390 case RID_TRANSACTION_RELAXED:
7391 return c_parser_transaction_expression (parser,
7392 c_parser_peek_token (parser)->keyword);
7393 default:
7394 return c_parser_postfix_expression (parser);
7396 default:
7397 return c_parser_postfix_expression (parser);
7401 /* Parse a sizeof expression. */
7403 static struct c_expr
7404 c_parser_sizeof_expression (c_parser *parser)
7406 struct c_expr expr;
7407 struct c_expr result;
7408 location_t expr_loc;
7409 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7411 location_t start;
7412 location_t finish = UNKNOWN_LOCATION;
7414 start = c_parser_peek_token (parser)->location;
7416 c_parser_consume_token (parser);
7417 c_inhibit_evaluation_warnings++;
7418 in_sizeof++;
7419 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7420 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7422 /* Either sizeof ( type-name ) or sizeof unary-expression
7423 starting with a compound literal. */
7424 struct c_type_name *type_name;
7425 matching_parens parens;
7426 parens.consume_open (parser);
7427 expr_loc = c_parser_peek_token (parser)->location;
7428 type_name = c_parser_type_name (parser);
7429 parens.skip_until_found_close (parser);
7430 finish = parser->tokens_buf[0].location;
7431 if (type_name == NULL)
7433 struct c_expr ret;
7434 c_inhibit_evaluation_warnings--;
7435 in_sizeof--;
7436 ret.value = error_mark_node;
7437 ret.original_code = ERROR_MARK;
7438 ret.original_type = NULL;
7439 return ret;
7441 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7443 expr = c_parser_postfix_expression_after_paren_type (parser,
7444 type_name,
7445 expr_loc);
7446 finish = expr.get_finish ();
7447 goto sizeof_expr;
7449 /* sizeof ( type-name ). */
7450 c_inhibit_evaluation_warnings--;
7451 in_sizeof--;
7452 result = c_expr_sizeof_type (expr_loc, type_name);
7454 else
7456 expr_loc = c_parser_peek_token (parser)->location;
7457 expr = c_parser_unary_expression (parser);
7458 finish = expr.get_finish ();
7459 sizeof_expr:
7460 c_inhibit_evaluation_warnings--;
7461 in_sizeof--;
7462 mark_exp_read (expr.value);
7463 if (TREE_CODE (expr.value) == COMPONENT_REF
7464 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7465 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7466 result = c_expr_sizeof_expr (expr_loc, expr);
7468 if (finish != UNKNOWN_LOCATION)
7469 set_c_expr_source_range (&result, start, finish);
7470 return result;
7473 /* Parse an alignof expression. */
7475 static struct c_expr
7476 c_parser_alignof_expression (c_parser *parser)
7478 struct c_expr expr;
7479 location_t start_loc = c_parser_peek_token (parser)->location;
7480 location_t end_loc;
7481 tree alignof_spelling = c_parser_peek_token (parser)->value;
7482 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7483 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7484 "_Alignof") == 0;
7485 /* A diagnostic is not required for the use of this identifier in
7486 the implementation namespace; only diagnose it for the C11
7487 spelling because of existing code using the other spellings. */
7488 if (is_c11_alignof)
7490 if (flag_isoc99)
7491 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7492 alignof_spelling);
7493 else
7494 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7495 alignof_spelling);
7497 c_parser_consume_token (parser);
7498 c_inhibit_evaluation_warnings++;
7499 in_alignof++;
7500 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7501 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7503 /* Either __alignof__ ( type-name ) or __alignof__
7504 unary-expression starting with a compound literal. */
7505 location_t loc;
7506 struct c_type_name *type_name;
7507 struct c_expr ret;
7508 matching_parens parens;
7509 parens.consume_open (parser);
7510 loc = c_parser_peek_token (parser)->location;
7511 type_name = c_parser_type_name (parser);
7512 end_loc = c_parser_peek_token (parser)->location;
7513 parens.skip_until_found_close (parser);
7514 if (type_name == NULL)
7516 struct c_expr ret;
7517 c_inhibit_evaluation_warnings--;
7518 in_alignof--;
7519 ret.value = error_mark_node;
7520 ret.original_code = ERROR_MARK;
7521 ret.original_type = NULL;
7522 return ret;
7524 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7526 expr = c_parser_postfix_expression_after_paren_type (parser,
7527 type_name,
7528 loc);
7529 goto alignof_expr;
7531 /* alignof ( type-name ). */
7532 c_inhibit_evaluation_warnings--;
7533 in_alignof--;
7534 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7535 NULL, NULL),
7536 false, is_c11_alignof, 1);
7537 ret.original_code = ERROR_MARK;
7538 ret.original_type = NULL;
7539 set_c_expr_source_range (&ret, start_loc, end_loc);
7540 return ret;
7542 else
7544 struct c_expr ret;
7545 expr = c_parser_unary_expression (parser);
7546 end_loc = expr.src_range.m_finish;
7547 alignof_expr:
7548 mark_exp_read (expr.value);
7549 c_inhibit_evaluation_warnings--;
7550 in_alignof--;
7551 if (is_c11_alignof)
7552 pedwarn (start_loc,
7553 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7554 alignof_spelling);
7555 ret.value = c_alignof_expr (start_loc, expr.value);
7556 ret.original_code = ERROR_MARK;
7557 ret.original_type = NULL;
7558 set_c_expr_source_range (&ret, start_loc, end_loc);
7559 return ret;
7563 /* Helper function to read arguments of builtins which are interfaces
7564 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7565 others. The name of the builtin is passed using BNAME parameter.
7566 Function returns true if there were no errors while parsing and
7567 stores the arguments in CEXPR_LIST. If it returns true,
7568 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7569 parenthesis. */
7570 static bool
7571 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7572 vec<c_expr_t, va_gc> **ret_cexpr_list,
7573 bool choose_expr_p,
7574 location_t *out_close_paren_loc)
7576 location_t loc = c_parser_peek_token (parser)->location;
7577 vec<c_expr_t, va_gc> *cexpr_list;
7578 c_expr_t expr;
7579 bool saved_force_folding_builtin_constant_p;
7581 *ret_cexpr_list = NULL;
7582 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7584 error_at (loc, "cannot take address of %qs", bname);
7585 return false;
7588 c_parser_consume_token (parser);
7590 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7592 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7593 c_parser_consume_token (parser);
7594 return true;
7597 saved_force_folding_builtin_constant_p
7598 = force_folding_builtin_constant_p;
7599 force_folding_builtin_constant_p |= choose_expr_p;
7600 expr = c_parser_expr_no_commas (parser, NULL);
7601 force_folding_builtin_constant_p
7602 = saved_force_folding_builtin_constant_p;
7603 vec_alloc (cexpr_list, 1);
7604 vec_safe_push (cexpr_list, expr);
7605 while (c_parser_next_token_is (parser, CPP_COMMA))
7607 c_parser_consume_token (parser);
7608 expr = c_parser_expr_no_commas (parser, NULL);
7609 vec_safe_push (cexpr_list, expr);
7612 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7613 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7614 return false;
7616 *ret_cexpr_list = cexpr_list;
7617 return true;
7620 /* This represents a single generic-association. */
7622 struct c_generic_association
7624 /* The location of the starting token of the type. */
7625 location_t type_location;
7626 /* The association's type, or NULL_TREE for 'default'. */
7627 tree type;
7628 /* The association's expression. */
7629 struct c_expr expression;
7632 /* Parse a generic-selection. (C11 6.5.1.1).
7634 generic-selection:
7635 _Generic ( assignment-expression , generic-assoc-list )
7637 generic-assoc-list:
7638 generic-association
7639 generic-assoc-list , generic-association
7641 generic-association:
7642 type-name : assignment-expression
7643 default : assignment-expression
7646 static struct c_expr
7647 c_parser_generic_selection (c_parser *parser)
7649 struct c_expr selector, error_expr;
7650 tree selector_type;
7651 struct c_generic_association matched_assoc;
7652 bool match_found = false;
7653 location_t generic_loc, selector_loc;
7655 error_expr.original_code = ERROR_MARK;
7656 error_expr.original_type = NULL;
7657 error_expr.set_error ();
7658 matched_assoc.type_location = UNKNOWN_LOCATION;
7659 matched_assoc.type = NULL_TREE;
7660 matched_assoc.expression = error_expr;
7662 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7663 generic_loc = c_parser_peek_token (parser)->location;
7664 c_parser_consume_token (parser);
7665 if (flag_isoc99)
7666 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7667 "ISO C99 does not support %<_Generic%>");
7668 else
7669 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7670 "ISO C90 does not support %<_Generic%>");
7672 matching_parens parens;
7673 if (!parens.require_open (parser))
7674 return error_expr;
7676 c_inhibit_evaluation_warnings++;
7677 selector_loc = c_parser_peek_token (parser)->location;
7678 selector = c_parser_expr_no_commas (parser, NULL);
7679 selector = default_function_array_conversion (selector_loc, selector);
7680 c_inhibit_evaluation_warnings--;
7682 if (selector.value == error_mark_node)
7684 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7685 return selector;
7687 selector_type = TREE_TYPE (selector.value);
7688 /* In ISO C terms, rvalues (including the controlling expression of
7689 _Generic) do not have qualified types. */
7690 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7691 selector_type = TYPE_MAIN_VARIANT (selector_type);
7692 /* In ISO C terms, _Noreturn is not part of the type of expressions
7693 such as &abort, but in GCC it is represented internally as a type
7694 qualifier. */
7695 if (FUNCTION_POINTER_TYPE_P (selector_type)
7696 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7697 selector_type
7698 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7700 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7702 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7703 return error_expr;
7706 auto_vec<c_generic_association> associations;
7707 while (1)
7709 struct c_generic_association assoc, *iter;
7710 unsigned int ix;
7711 c_token *token = c_parser_peek_token (parser);
7713 assoc.type_location = token->location;
7714 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7716 c_parser_consume_token (parser);
7717 assoc.type = NULL_TREE;
7719 else
7721 struct c_type_name *type_name;
7723 type_name = c_parser_type_name (parser);
7724 if (type_name == NULL)
7726 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7727 return error_expr;
7729 assoc.type = groktypename (type_name, NULL, NULL);
7730 if (assoc.type == error_mark_node)
7732 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7733 return error_expr;
7736 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7737 error_at (assoc.type_location,
7738 "%<_Generic%> association has function type");
7739 else if (!COMPLETE_TYPE_P (assoc.type))
7740 error_at (assoc.type_location,
7741 "%<_Generic%> association has incomplete type");
7743 if (variably_modified_type_p (assoc.type, NULL_TREE))
7744 error_at (assoc.type_location,
7745 "%<_Generic%> association has "
7746 "variable length type");
7749 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7752 return error_expr;
7755 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7756 if (assoc.expression.value == error_mark_node)
7758 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7759 return error_expr;
7762 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7764 if (assoc.type == NULL_TREE)
7766 if (iter->type == NULL_TREE)
7768 error_at (assoc.type_location,
7769 "duplicate %<default%> case in %<_Generic%>");
7770 inform (iter->type_location, "original %<default%> is here");
7773 else if (iter->type != NULL_TREE)
7775 if (comptypes (assoc.type, iter->type))
7777 error_at (assoc.type_location,
7778 "%<_Generic%> specifies two compatible types");
7779 inform (iter->type_location, "compatible type is here");
7784 if (assoc.type == NULL_TREE)
7786 if (!match_found)
7788 matched_assoc = assoc;
7789 match_found = true;
7792 else if (comptypes (assoc.type, selector_type))
7794 if (!match_found || matched_assoc.type == NULL_TREE)
7796 matched_assoc = assoc;
7797 match_found = true;
7799 else
7801 error_at (assoc.type_location,
7802 "%<_Generic%> selector matches multiple associations");
7803 inform (matched_assoc.type_location,
7804 "other match is here");
7808 associations.safe_push (assoc);
7810 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7811 break;
7812 c_parser_consume_token (parser);
7815 if (!parens.require_close (parser))
7817 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7818 return error_expr;
7821 if (!match_found)
7823 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7824 "compatible with any association",
7825 selector_type);
7826 return error_expr;
7829 return matched_assoc.expression;
7832 /* Check the validity of a function pointer argument *EXPR (argument
7833 position POS) to __builtin_tgmath. Return the number of function
7834 arguments if possibly valid; return 0 having reported an error if
7835 not valid. */
7837 static unsigned int
7838 check_tgmath_function (c_expr *expr, unsigned int pos)
7840 tree type = TREE_TYPE (expr->value);
7841 if (!FUNCTION_POINTER_TYPE_P (type))
7843 error_at (expr->get_location (),
7844 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7845 pos);
7846 return 0;
7848 type = TREE_TYPE (type);
7849 if (!prototype_p (type))
7851 error_at (expr->get_location (),
7852 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7853 return 0;
7855 if (stdarg_p (type))
7857 error_at (expr->get_location (),
7858 "argument %u of %<__builtin_tgmath%> has variable arguments",
7859 pos);
7860 return 0;
7862 unsigned int nargs = 0;
7863 function_args_iterator iter;
7864 tree t;
7865 FOREACH_FUNCTION_ARGS (type, t, iter)
7867 if (t == void_type_node)
7868 break;
7869 nargs++;
7871 if (nargs == 0)
7873 error_at (expr->get_location (),
7874 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7875 return 0;
7877 return nargs;
7880 /* Ways in which a parameter or return value of a type-generic macro
7881 may vary between the different functions the macro may call. */
7882 enum tgmath_parm_kind
7884 tgmath_fixed, tgmath_real, tgmath_complex
7887 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7888 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7889 call c_parser_postfix_expression_after_paren_type on encountering them.
7891 postfix-expression:
7892 primary-expression
7893 postfix-expression [ expression ]
7894 postfix-expression ( argument-expression-list[opt] )
7895 postfix-expression . identifier
7896 postfix-expression -> identifier
7897 postfix-expression ++
7898 postfix-expression --
7899 ( type-name ) { initializer-list }
7900 ( type-name ) { initializer-list , }
7902 argument-expression-list:
7903 argument-expression
7904 argument-expression-list , argument-expression
7906 primary-expression:
7907 identifier
7908 constant
7909 string-literal
7910 ( expression )
7911 generic-selection
7913 GNU extensions:
7915 primary-expression:
7916 __func__
7917 (treated as a keyword in GNU C)
7918 __FUNCTION__
7919 __PRETTY_FUNCTION__
7920 ( compound-statement )
7921 __builtin_va_arg ( assignment-expression , type-name )
7922 __builtin_offsetof ( type-name , offsetof-member-designator )
7923 __builtin_choose_expr ( assignment-expression ,
7924 assignment-expression ,
7925 assignment-expression )
7926 __builtin_types_compatible_p ( type-name , type-name )
7927 __builtin_tgmath ( expr-list )
7928 __builtin_complex ( assignment-expression , assignment-expression )
7929 __builtin_shuffle ( assignment-expression , assignment-expression )
7930 __builtin_shuffle ( assignment-expression ,
7931 assignment-expression ,
7932 assignment-expression, )
7934 offsetof-member-designator:
7935 identifier
7936 offsetof-member-designator . identifier
7937 offsetof-member-designator [ expression ]
7939 Objective-C:
7941 primary-expression:
7942 [ objc-receiver objc-message-args ]
7943 @selector ( objc-selector-arg )
7944 @protocol ( identifier )
7945 @encode ( type-name )
7946 objc-string-literal
7947 Classname . identifier
7950 static struct c_expr
7951 c_parser_postfix_expression (c_parser *parser)
7953 struct c_expr expr, e1;
7954 struct c_type_name *t1, *t2;
7955 location_t loc = c_parser_peek_token (parser)->location;;
7956 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7957 expr.original_code = ERROR_MARK;
7958 expr.original_type = NULL;
7959 switch (c_parser_peek_token (parser)->type)
7961 case CPP_NUMBER:
7962 expr.value = c_parser_peek_token (parser)->value;
7963 set_c_expr_source_range (&expr, tok_range);
7964 loc = c_parser_peek_token (parser)->location;
7965 c_parser_consume_token (parser);
7966 if (TREE_CODE (expr.value) == FIXED_CST
7967 && !targetm.fixed_point_supported_p ())
7969 error_at (loc, "fixed-point types not supported for this target");
7970 expr.value = error_mark_node;
7972 break;
7973 case CPP_CHAR:
7974 case CPP_CHAR16:
7975 case CPP_CHAR32:
7976 case CPP_WCHAR:
7977 expr.value = c_parser_peek_token (parser)->value;
7978 /* For the purpose of warning when a pointer is compared with
7979 a zero character constant. */
7980 expr.original_type = char_type_node;
7981 set_c_expr_source_range (&expr, tok_range);
7982 c_parser_consume_token (parser);
7983 break;
7984 case CPP_STRING:
7985 case CPP_STRING16:
7986 case CPP_STRING32:
7987 case CPP_WSTRING:
7988 case CPP_UTF8STRING:
7989 expr.value = c_parser_peek_token (parser)->value;
7990 set_c_expr_source_range (&expr, tok_range);
7991 expr.original_code = STRING_CST;
7992 c_parser_consume_token (parser);
7993 break;
7994 case CPP_OBJC_STRING:
7995 gcc_assert (c_dialect_objc ());
7996 expr.value
7997 = objc_build_string_object (c_parser_peek_token (parser)->value);
7998 set_c_expr_source_range (&expr, tok_range);
7999 c_parser_consume_token (parser);
8000 break;
8001 case CPP_NAME:
8002 switch (c_parser_peek_token (parser)->id_kind)
8004 case C_ID_ID:
8006 tree id = c_parser_peek_token (parser)->value;
8007 c_parser_consume_token (parser);
8008 expr.value = build_external_ref (loc, id,
8009 (c_parser_peek_token (parser)->type
8010 == CPP_OPEN_PAREN),
8011 &expr.original_type);
8012 set_c_expr_source_range (&expr, tok_range);
8013 break;
8015 case C_ID_CLASSNAME:
8017 /* Here we parse the Objective-C 2.0 Class.name dot
8018 syntax. */
8019 tree class_name = c_parser_peek_token (parser)->value;
8020 tree component;
8021 c_parser_consume_token (parser);
8022 gcc_assert (c_dialect_objc ());
8023 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
8025 expr.set_error ();
8026 break;
8028 if (c_parser_next_token_is_not (parser, CPP_NAME))
8030 c_parser_error (parser, "expected identifier");
8031 expr.set_error ();
8032 break;
8034 c_token *component_tok = c_parser_peek_token (parser);
8035 component = component_tok->value;
8036 location_t end_loc = component_tok->get_finish ();
8037 c_parser_consume_token (parser);
8038 expr.value = objc_build_class_component_ref (class_name,
8039 component);
8040 set_c_expr_source_range (&expr, loc, end_loc);
8041 break;
8043 default:
8044 c_parser_error (parser, "expected expression");
8045 expr.set_error ();
8046 break;
8048 break;
8049 case CPP_OPEN_PAREN:
8050 /* A parenthesized expression, statement expression or compound
8051 literal. */
8052 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
8054 /* A statement expression. */
8055 tree stmt;
8056 location_t brace_loc;
8057 c_parser_consume_token (parser);
8058 brace_loc = c_parser_peek_token (parser)->location;
8059 c_parser_consume_token (parser);
8060 if (!building_stmt_list_p ())
8062 error_at (loc, "braced-group within expression allowed "
8063 "only inside a function");
8064 parser->error = true;
8065 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
8066 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8067 expr.set_error ();
8068 break;
8070 stmt = c_begin_stmt_expr ();
8071 c_parser_compound_statement_nostart (parser);
8072 location_t close_loc = c_parser_peek_token (parser)->location;
8073 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8074 "expected %<)%>");
8075 pedwarn (loc, OPT_Wpedantic,
8076 "ISO C forbids braced-groups within expressions");
8077 expr.value = c_finish_stmt_expr (brace_loc, stmt);
8078 set_c_expr_source_range (&expr, loc, close_loc);
8079 mark_exp_read (expr.value);
8081 else
8083 /* A parenthesized expression. */
8084 location_t loc_open_paren = c_parser_peek_token (parser)->location;
8085 c_parser_consume_token (parser);
8086 expr = c_parser_expression (parser);
8087 if (TREE_CODE (expr.value) == MODIFY_EXPR)
8088 TREE_NO_WARNING (expr.value) = 1;
8089 if (expr.original_code != C_MAYBE_CONST_EXPR
8090 && expr.original_code != SIZEOF_EXPR)
8091 expr.original_code = ERROR_MARK;
8092 /* Don't change EXPR.ORIGINAL_TYPE. */
8093 location_t loc_close_paren = c_parser_peek_token (parser)->location;
8094 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
8095 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8096 "expected %<)%>", loc_open_paren);
8098 break;
8099 case CPP_KEYWORD:
8100 switch (c_parser_peek_token (parser)->keyword)
8102 case RID_FUNCTION_NAME:
8103 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8104 "%<__FUNCTION__%> predefined identifier");
8105 expr.value = fname_decl (loc,
8106 c_parser_peek_token (parser)->keyword,
8107 c_parser_peek_token (parser)->value);
8108 set_c_expr_source_range (&expr, loc, loc);
8109 c_parser_consume_token (parser);
8110 break;
8111 case RID_PRETTY_FUNCTION_NAME:
8112 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8113 "%<__PRETTY_FUNCTION__%> predefined identifier");
8114 expr.value = fname_decl (loc,
8115 c_parser_peek_token (parser)->keyword,
8116 c_parser_peek_token (parser)->value);
8117 set_c_expr_source_range (&expr, loc, loc);
8118 c_parser_consume_token (parser);
8119 break;
8120 case RID_C99_FUNCTION_NAME:
8121 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8122 "%<__func__%> predefined identifier");
8123 expr.value = fname_decl (loc,
8124 c_parser_peek_token (parser)->keyword,
8125 c_parser_peek_token (parser)->value);
8126 set_c_expr_source_range (&expr, loc, loc);
8127 c_parser_consume_token (parser);
8128 break;
8129 case RID_VA_ARG:
8131 location_t start_loc = loc;
8132 c_parser_consume_token (parser);
8133 matching_parens parens;
8134 if (!parens.require_open (parser))
8136 expr.set_error ();
8137 break;
8139 e1 = c_parser_expr_no_commas (parser, NULL);
8140 mark_exp_read (e1.value);
8141 e1.value = c_fully_fold (e1.value, false, NULL);
8142 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8144 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8145 expr.set_error ();
8146 break;
8148 loc = c_parser_peek_token (parser)->location;
8149 t1 = c_parser_type_name (parser);
8150 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8151 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8152 "expected %<)%>");
8153 if (t1 == NULL)
8155 expr.set_error ();
8157 else
8159 tree type_expr = NULL_TREE;
8160 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8161 groktypename (t1, &type_expr, NULL));
8162 if (type_expr)
8164 expr.value = build2 (C_MAYBE_CONST_EXPR,
8165 TREE_TYPE (expr.value), type_expr,
8166 expr.value);
8167 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8169 set_c_expr_source_range (&expr, start_loc, end_loc);
8172 break;
8173 case RID_OFFSETOF:
8175 c_parser_consume_token (parser);
8176 matching_parens parens;
8177 if (!parens.require_open (parser))
8179 expr.set_error ();
8180 break;
8182 t1 = c_parser_type_name (parser);
8183 if (t1 == NULL)
8184 parser->error = true;
8185 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8186 gcc_assert (parser->error);
8187 if (parser->error)
8189 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8190 expr.set_error ();
8191 break;
8193 tree type = groktypename (t1, NULL, NULL);
8194 tree offsetof_ref;
8195 if (type == error_mark_node)
8196 offsetof_ref = error_mark_node;
8197 else
8199 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8200 SET_EXPR_LOCATION (offsetof_ref, loc);
8202 /* Parse the second argument to __builtin_offsetof. We
8203 must have one identifier, and beyond that we want to
8204 accept sub structure and sub array references. */
8205 if (c_parser_next_token_is (parser, CPP_NAME))
8207 c_token *comp_tok = c_parser_peek_token (parser);
8208 offsetof_ref = build_component_ref
8209 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8210 c_parser_consume_token (parser);
8211 while (c_parser_next_token_is (parser, CPP_DOT)
8212 || c_parser_next_token_is (parser,
8213 CPP_OPEN_SQUARE)
8214 || c_parser_next_token_is (parser,
8215 CPP_DEREF))
8217 if (c_parser_next_token_is (parser, CPP_DEREF))
8219 loc = c_parser_peek_token (parser)->location;
8220 offsetof_ref = build_array_ref (loc,
8221 offsetof_ref,
8222 integer_zero_node);
8223 goto do_dot;
8225 else if (c_parser_next_token_is (parser, CPP_DOT))
8227 do_dot:
8228 c_parser_consume_token (parser);
8229 if (c_parser_next_token_is_not (parser,
8230 CPP_NAME))
8232 c_parser_error (parser, "expected identifier");
8233 break;
8235 c_token *comp_tok = c_parser_peek_token (parser);
8236 offsetof_ref = build_component_ref
8237 (loc, offsetof_ref, comp_tok->value,
8238 comp_tok->location);
8239 c_parser_consume_token (parser);
8241 else
8243 struct c_expr ce;
8244 tree idx;
8245 loc = c_parser_peek_token (parser)->location;
8246 c_parser_consume_token (parser);
8247 ce = c_parser_expression (parser);
8248 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8249 idx = ce.value;
8250 idx = c_fully_fold (idx, false, NULL);
8251 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8252 "expected %<]%>");
8253 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8257 else
8258 c_parser_error (parser, "expected identifier");
8259 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8260 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8261 "expected %<)%>");
8262 expr.value = fold_offsetof (offsetof_ref);
8263 set_c_expr_source_range (&expr, loc, end_loc);
8265 break;
8266 case RID_CHOOSE_EXPR:
8268 vec<c_expr_t, va_gc> *cexpr_list;
8269 c_expr_t *e1_p, *e2_p, *e3_p;
8270 tree c;
8271 location_t close_paren_loc;
8273 c_parser_consume_token (parser);
8274 if (!c_parser_get_builtin_args (parser,
8275 "__builtin_choose_expr",
8276 &cexpr_list, true,
8277 &close_paren_loc))
8279 expr.set_error ();
8280 break;
8283 if (vec_safe_length (cexpr_list) != 3)
8285 error_at (loc, "wrong number of arguments to "
8286 "%<__builtin_choose_expr%>");
8287 expr.set_error ();
8288 break;
8291 e1_p = &(*cexpr_list)[0];
8292 e2_p = &(*cexpr_list)[1];
8293 e3_p = &(*cexpr_list)[2];
8295 c = e1_p->value;
8296 mark_exp_read (e2_p->value);
8297 mark_exp_read (e3_p->value);
8298 if (TREE_CODE (c) != INTEGER_CST
8299 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8300 error_at (loc,
8301 "first argument to %<__builtin_choose_expr%> not"
8302 " a constant");
8303 constant_expression_warning (c);
8304 expr = integer_zerop (c) ? *e3_p : *e2_p;
8305 set_c_expr_source_range (&expr, loc, close_paren_loc);
8306 break;
8308 case RID_TYPES_COMPATIBLE_P:
8310 c_parser_consume_token (parser);
8311 matching_parens parens;
8312 if (!parens.require_open (parser))
8314 expr.set_error ();
8315 break;
8317 t1 = c_parser_type_name (parser);
8318 if (t1 == NULL)
8320 expr.set_error ();
8321 break;
8323 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8325 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8326 expr.set_error ();
8327 break;
8329 t2 = c_parser_type_name (parser);
8330 if (t2 == NULL)
8332 expr.set_error ();
8333 break;
8335 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8336 parens.skip_until_found_close (parser);
8337 tree e1, e2;
8338 e1 = groktypename (t1, NULL, NULL);
8339 e2 = groktypename (t2, NULL, NULL);
8340 if (e1 == error_mark_node || e2 == error_mark_node)
8342 expr.set_error ();
8343 break;
8346 e1 = TYPE_MAIN_VARIANT (e1);
8347 e2 = TYPE_MAIN_VARIANT (e2);
8349 expr.value
8350 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8351 set_c_expr_source_range (&expr, loc, close_paren_loc);
8353 break;
8354 case RID_BUILTIN_TGMATH:
8356 vec<c_expr_t, va_gc> *cexpr_list;
8357 location_t close_paren_loc;
8359 c_parser_consume_token (parser);
8360 if (!c_parser_get_builtin_args (parser,
8361 "__builtin_tgmath",
8362 &cexpr_list, false,
8363 &close_paren_loc))
8365 expr.set_error ();
8366 break;
8369 if (vec_safe_length (cexpr_list) < 3)
8371 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8372 expr.set_error ();
8373 break;
8376 unsigned int i;
8377 c_expr_t *p;
8378 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8379 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8380 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8381 if (nargs == 0)
8383 expr.set_error ();
8384 break;
8386 if (vec_safe_length (cexpr_list) < nargs)
8388 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8389 expr.set_error ();
8390 break;
8392 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8393 if (num_functions < 2)
8395 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8396 expr.set_error ();
8397 break;
8400 /* The first NUM_FUNCTIONS expressions are the function
8401 pointers. The remaining NARGS expressions are the
8402 arguments that are to be passed to one of those
8403 functions, chosen following <tgmath.h> rules. */
8404 for (unsigned int j = 1; j < num_functions; j++)
8406 unsigned int this_nargs
8407 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8408 if (this_nargs == 0)
8410 expr.set_error ();
8411 goto out;
8413 if (this_nargs != nargs)
8415 error_at ((*cexpr_list)[j].get_location (),
8416 "argument %u of %<__builtin_tgmath%> has "
8417 "wrong number of arguments", j + 1);
8418 expr.set_error ();
8419 goto out;
8423 /* The functions all have the same number of arguments.
8424 Determine whether arguments and return types vary in
8425 ways permitted for <tgmath.h> functions. */
8426 /* The first entry in each of these vectors is for the
8427 return type, subsequent entries for parameter
8428 types. */
8429 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8430 auto_vec<tree> parm_first (nargs + 1);
8431 auto_vec<bool> parm_complex (nargs + 1);
8432 auto_vec<bool> parm_varies (nargs + 1);
8433 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8434 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8435 parm_first.quick_push (first_ret);
8436 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8437 parm_varies.quick_push (false);
8438 function_args_iterator iter;
8439 tree t;
8440 unsigned int argpos;
8441 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8443 if (t == void_type_node)
8444 break;
8445 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8446 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8447 parm_varies.quick_push (false);
8449 for (unsigned int j = 1; j < num_functions; j++)
8451 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8452 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8453 if (ret != parm_first[0])
8455 parm_varies[0] = true;
8456 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8457 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8459 error_at ((*cexpr_list)[0].get_location (),
8460 "invalid type-generic return type for "
8461 "argument %u of %<__builtin_tgmath%>",
8463 expr.set_error ();
8464 goto out;
8466 if (!SCALAR_FLOAT_TYPE_P (ret)
8467 && !COMPLEX_FLOAT_TYPE_P (ret))
8469 error_at ((*cexpr_list)[j].get_location (),
8470 "invalid type-generic return type for "
8471 "argument %u of %<__builtin_tgmath%>",
8472 j + 1);
8473 expr.set_error ();
8474 goto out;
8477 if (TREE_CODE (ret) == COMPLEX_TYPE)
8478 parm_complex[0] = true;
8479 argpos = 1;
8480 FOREACH_FUNCTION_ARGS (type, t, iter)
8482 if (t == void_type_node)
8483 break;
8484 t = TYPE_MAIN_VARIANT (t);
8485 if (t != parm_first[argpos])
8487 parm_varies[argpos] = true;
8488 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8489 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8491 error_at ((*cexpr_list)[0].get_location (),
8492 "invalid type-generic type for "
8493 "argument %u of argument %u of "
8494 "%<__builtin_tgmath%>", argpos, 1);
8495 expr.set_error ();
8496 goto out;
8498 if (!SCALAR_FLOAT_TYPE_P (t)
8499 && !COMPLEX_FLOAT_TYPE_P (t))
8501 error_at ((*cexpr_list)[j].get_location (),
8502 "invalid type-generic type for "
8503 "argument %u of argument %u of "
8504 "%<__builtin_tgmath%>", argpos, j + 1);
8505 expr.set_error ();
8506 goto out;
8509 if (TREE_CODE (t) == COMPLEX_TYPE)
8510 parm_complex[argpos] = true;
8511 argpos++;
8514 enum tgmath_parm_kind max_variation = tgmath_fixed;
8515 for (unsigned int j = 0; j <= nargs; j++)
8517 enum tgmath_parm_kind this_kind;
8518 if (parm_varies[j])
8520 if (parm_complex[j])
8521 max_variation = this_kind = tgmath_complex;
8522 else
8524 this_kind = tgmath_real;
8525 if (max_variation != tgmath_complex)
8526 max_variation = tgmath_real;
8529 else
8530 this_kind = tgmath_fixed;
8531 parm_kind.quick_push (this_kind);
8533 if (max_variation == tgmath_fixed)
8535 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8536 "all have the same type");
8537 expr.set_error ();
8538 break;
8541 /* Identify a parameter (not the return type) that varies,
8542 including with complex types if any variation includes
8543 complex types; there must be at least one such
8544 parameter. */
8545 unsigned int tgarg = 0;
8546 for (unsigned int j = 1; j <= nargs; j++)
8547 if (parm_kind[j] == max_variation)
8549 tgarg = j;
8550 break;
8552 if (tgarg == 0)
8554 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8555 "lack type-generic parameter");
8556 expr.set_error ();
8557 break;
8560 /* Determine the type of the relevant parameter for each
8561 function. */
8562 auto_vec<tree> tg_type (num_functions);
8563 for (unsigned int j = 0; j < num_functions; j++)
8565 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8566 argpos = 1;
8567 FOREACH_FUNCTION_ARGS (type, t, iter)
8569 if (argpos == tgarg)
8571 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8572 break;
8574 argpos++;
8578 /* Verify that the corresponding types are different for
8579 all the listed functions. Also determine whether all
8580 the types are complex, whether all the types are
8581 standard or binary, and whether all the types are
8582 decimal. */
8583 bool all_complex = true;
8584 bool all_binary = true;
8585 bool all_decimal = true;
8586 hash_set<tree> tg_types;
8587 FOR_EACH_VEC_ELT (tg_type, i, t)
8589 if (TREE_CODE (t) == COMPLEX_TYPE)
8590 all_decimal = false;
8591 else
8593 all_complex = false;
8594 if (DECIMAL_FLOAT_TYPE_P (t))
8595 all_binary = false;
8596 else
8597 all_decimal = false;
8599 if (tg_types.add (t))
8601 error_at ((*cexpr_list)[i].get_location (),
8602 "duplicate type-generic parameter type for "
8603 "function argument %u of %<__builtin_tgmath%>",
8604 i + 1);
8605 expr.set_error ();
8606 goto out;
8610 /* Verify that other parameters and the return type whose
8611 types vary have their types varying in the correct
8612 way. */
8613 for (unsigned int j = 0; j < num_functions; j++)
8615 tree exp_type = tg_type[j];
8616 tree exp_real_type = exp_type;
8617 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8618 exp_real_type = TREE_TYPE (exp_type);
8619 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8620 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8621 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8622 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8624 error_at ((*cexpr_list)[j].get_location (),
8625 "bad return type for function argument %u "
8626 "of %<__builtin_tgmath%>", j + 1);
8627 expr.set_error ();
8628 goto out;
8630 argpos = 1;
8631 FOREACH_FUNCTION_ARGS (type, t, iter)
8633 if (t == void_type_node)
8634 break;
8635 t = TYPE_MAIN_VARIANT (t);
8636 if ((parm_kind[argpos] == tgmath_complex
8637 && t != exp_type)
8638 || (parm_kind[argpos] == tgmath_real
8639 && t != exp_real_type))
8641 error_at ((*cexpr_list)[j].get_location (),
8642 "bad type for argument %u of "
8643 "function argument %u of "
8644 "%<__builtin_tgmath%>", argpos, j + 1);
8645 expr.set_error ();
8646 goto out;
8648 argpos++;
8652 /* The functions listed are a valid set of functions for a
8653 <tgmath.h> macro to select between. Identify the
8654 matching function, if any. First, the argument types
8655 must be combined following <tgmath.h> rules. Integer
8656 types are treated as _Decimal64 if any type-generic
8657 argument is decimal, or if the only alternatives for
8658 type-generic arguments are of decimal types, and are
8659 otherwise treated as double (or _Complex double for
8660 complex integer types). After that adjustment, types
8661 are combined following the usual arithmetic
8662 conversions. If the function only accepts complex
8663 arguments, a complex type is produced. */
8664 bool arg_complex = all_complex;
8665 bool arg_binary = all_binary;
8666 bool arg_int_decimal = all_decimal;
8667 for (unsigned int j = 1; j <= nargs; j++)
8669 if (parm_kind[j] == tgmath_fixed)
8670 continue;
8671 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8672 tree type = TREE_TYPE (ce->value);
8673 if (!INTEGRAL_TYPE_P (type)
8674 && !SCALAR_FLOAT_TYPE_P (type)
8675 && TREE_CODE (type) != COMPLEX_TYPE)
8677 error_at (ce->get_location (),
8678 "invalid type of argument %u of type-generic "
8679 "function", j);
8680 expr.set_error ();
8681 goto out;
8683 if (DECIMAL_FLOAT_TYPE_P (type))
8685 arg_int_decimal = true;
8686 if (all_complex)
8688 error_at (ce->get_location (),
8689 "decimal floating-point argument %u to "
8690 "complex-only type-generic function", j);
8691 expr.set_error ();
8692 goto out;
8694 else if (all_binary)
8696 error_at (ce->get_location (),
8697 "decimal floating-point argument %u to "
8698 "binary-only type-generic function", j);
8699 expr.set_error ();
8700 goto out;
8702 else if (arg_complex)
8704 error_at (ce->get_location (),
8705 "both complex and decimal floating-point "
8706 "arguments to type-generic function");
8707 expr.set_error ();
8708 goto out;
8710 else if (arg_binary)
8712 error_at (ce->get_location (),
8713 "both binary and decimal floating-point "
8714 "arguments to type-generic function");
8715 expr.set_error ();
8716 goto out;
8719 else if (TREE_CODE (type) == COMPLEX_TYPE)
8721 arg_complex = true;
8722 if (COMPLEX_FLOAT_TYPE_P (type))
8723 arg_binary = true;
8724 if (all_decimal)
8726 error_at (ce->get_location (),
8727 "complex argument %u to "
8728 "decimal-only type-generic function", j);
8729 expr.set_error ();
8730 goto out;
8732 else if (arg_int_decimal)
8734 error_at (ce->get_location (),
8735 "both complex and decimal floating-point "
8736 "arguments to type-generic function");
8737 expr.set_error ();
8738 goto out;
8741 else if (SCALAR_FLOAT_TYPE_P (type))
8743 arg_binary = true;
8744 if (all_decimal)
8746 error_at (ce->get_location (),
8747 "binary argument %u to "
8748 "decimal-only type-generic function", j);
8749 expr.set_error ();
8750 goto out;
8752 else if (arg_int_decimal)
8754 error_at (ce->get_location (),
8755 "both binary and decimal floating-point "
8756 "arguments to type-generic function");
8757 expr.set_error ();
8758 goto out;
8762 tree arg_real = NULL_TREE;
8763 for (unsigned int j = 1; j <= nargs; j++)
8765 if (parm_kind[j] == tgmath_fixed)
8766 continue;
8767 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8768 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8769 if (TREE_CODE (type) == COMPLEX_TYPE)
8770 type = TREE_TYPE (type);
8771 if (INTEGRAL_TYPE_P (type))
8772 type = (arg_int_decimal
8773 ? dfloat64_type_node
8774 : double_type_node);
8775 if (arg_real == NULL_TREE)
8776 arg_real = type;
8777 else
8778 arg_real = common_type (arg_real, type);
8779 if (arg_real == error_mark_node)
8781 expr.set_error ();
8782 goto out;
8785 tree arg_type = (arg_complex
8786 ? build_complex_type (arg_real)
8787 : arg_real);
8789 /* Look for a function to call with type-generic parameter
8790 type ARG_TYPE. */
8791 c_expr_t *fn = NULL;
8792 for (unsigned int j = 0; j < num_functions; j++)
8794 if (tg_type[j] == arg_type)
8796 fn = &(*cexpr_list)[j];
8797 break;
8800 if (fn == NULL
8801 && parm_kind[0] == tgmath_fixed
8802 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8804 /* Presume this is a macro that rounds its result to a
8805 narrower type, and look for the first function with
8806 at least the range and precision of the argument
8807 type. */
8808 for (unsigned int j = 0; j < num_functions; j++)
8810 if (arg_complex
8811 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8812 continue;
8813 tree real_tg_type = (arg_complex
8814 ? TREE_TYPE (tg_type[j])
8815 : tg_type[j]);
8816 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8817 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8818 continue;
8819 scalar_float_mode arg_mode
8820 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8821 scalar_float_mode tg_mode
8822 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8823 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8824 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8825 if (arg_fmt->b == tg_fmt->b
8826 && arg_fmt->p <= tg_fmt->p
8827 && arg_fmt->emax <= tg_fmt->emax
8828 && (arg_fmt->emin - arg_fmt->p
8829 >= tg_fmt->emin - tg_fmt->p))
8831 fn = &(*cexpr_list)[j];
8832 break;
8836 if (fn == NULL)
8838 error_at (loc, "no matching function for type-generic call");
8839 expr.set_error ();
8840 break;
8843 /* Construct a call to FN. */
8844 vec<tree, va_gc> *args;
8845 vec_alloc (args, nargs);
8846 vec<tree, va_gc> *origtypes;
8847 vec_alloc (origtypes, nargs);
8848 auto_vec<location_t> arg_loc (nargs);
8849 for (unsigned int j = 0; j < nargs; j++)
8851 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8852 args->quick_push (ce->value);
8853 arg_loc.quick_push (ce->get_location ());
8854 origtypes->quick_push (ce->original_type);
8856 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8857 args, origtypes);
8858 set_c_expr_source_range (&expr, loc, close_paren_loc);
8859 break;
8861 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8863 vec<c_expr_t, va_gc> *cexpr_list;
8864 c_expr_t *e2_p;
8865 tree chain_value;
8866 location_t close_paren_loc;
8868 c_parser_consume_token (parser);
8869 if (!c_parser_get_builtin_args (parser,
8870 "__builtin_call_with_static_chain",
8871 &cexpr_list, false,
8872 &close_paren_loc))
8874 expr.set_error ();
8875 break;
8877 if (vec_safe_length (cexpr_list) != 2)
8879 error_at (loc, "wrong number of arguments to "
8880 "%<__builtin_call_with_static_chain%>");
8881 expr.set_error ();
8882 break;
8885 expr = (*cexpr_list)[0];
8886 e2_p = &(*cexpr_list)[1];
8887 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8888 chain_value = e2_p->value;
8889 mark_exp_read (chain_value);
8891 if (TREE_CODE (expr.value) != CALL_EXPR)
8892 error_at (loc, "first argument to "
8893 "%<__builtin_call_with_static_chain%> "
8894 "must be a call expression");
8895 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8896 error_at (loc, "second argument to "
8897 "%<__builtin_call_with_static_chain%> "
8898 "must be a pointer type");
8899 else
8900 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8901 set_c_expr_source_range (&expr, loc, close_paren_loc);
8902 break;
8904 case RID_BUILTIN_COMPLEX:
8906 vec<c_expr_t, va_gc> *cexpr_list;
8907 c_expr_t *e1_p, *e2_p;
8908 location_t close_paren_loc;
8910 c_parser_consume_token (parser);
8911 if (!c_parser_get_builtin_args (parser,
8912 "__builtin_complex",
8913 &cexpr_list, false,
8914 &close_paren_loc))
8916 expr.set_error ();
8917 break;
8920 if (vec_safe_length (cexpr_list) != 2)
8922 error_at (loc, "wrong number of arguments to "
8923 "%<__builtin_complex%>");
8924 expr.set_error ();
8925 break;
8928 e1_p = &(*cexpr_list)[0];
8929 e2_p = &(*cexpr_list)[1];
8931 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8932 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8933 e1_p->value = convert (TREE_TYPE (e1_p->value),
8934 TREE_OPERAND (e1_p->value, 0));
8935 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8936 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8937 e2_p->value = convert (TREE_TYPE (e2_p->value),
8938 TREE_OPERAND (e2_p->value, 0));
8939 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8940 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8941 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8942 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8944 error_at (loc, "%<__builtin_complex%> operand "
8945 "not of real binary floating-point type");
8946 expr.set_error ();
8947 break;
8949 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8950 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8952 error_at (loc,
8953 "%<__builtin_complex%> operands of different types");
8954 expr.set_error ();
8955 break;
8957 pedwarn_c90 (loc, OPT_Wpedantic,
8958 "ISO C90 does not support complex types");
8959 expr.value = build2_loc (loc, COMPLEX_EXPR,
8960 build_complex_type
8961 (TYPE_MAIN_VARIANT
8962 (TREE_TYPE (e1_p->value))),
8963 e1_p->value, e2_p->value);
8964 set_c_expr_source_range (&expr, loc, close_paren_loc);
8965 break;
8967 case RID_BUILTIN_SHUFFLE:
8969 vec<c_expr_t, va_gc> *cexpr_list;
8970 unsigned int i;
8971 c_expr_t *p;
8972 location_t close_paren_loc;
8974 c_parser_consume_token (parser);
8975 if (!c_parser_get_builtin_args (parser,
8976 "__builtin_shuffle",
8977 &cexpr_list, false,
8978 &close_paren_loc))
8980 expr.set_error ();
8981 break;
8984 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8985 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8987 if (vec_safe_length (cexpr_list) == 2)
8988 expr.value =
8989 c_build_vec_perm_expr
8990 (loc, (*cexpr_list)[0].value,
8991 NULL_TREE, (*cexpr_list)[1].value);
8993 else if (vec_safe_length (cexpr_list) == 3)
8994 expr.value =
8995 c_build_vec_perm_expr
8996 (loc, (*cexpr_list)[0].value,
8997 (*cexpr_list)[1].value,
8998 (*cexpr_list)[2].value);
8999 else
9001 error_at (loc, "wrong number of arguments to "
9002 "%<__builtin_shuffle%>");
9003 expr.set_error ();
9005 set_c_expr_source_range (&expr, loc, close_paren_loc);
9006 break;
9008 case RID_AT_SELECTOR:
9010 gcc_assert (c_dialect_objc ());
9011 c_parser_consume_token (parser);
9012 matching_parens parens;
9013 if (!parens.require_open (parser))
9015 expr.set_error ();
9016 break;
9018 tree sel = c_parser_objc_selector_arg (parser);
9019 location_t close_loc = c_parser_peek_token (parser)->location;
9020 parens.skip_until_found_close (parser);
9021 expr.value = objc_build_selector_expr (loc, sel);
9022 set_c_expr_source_range (&expr, loc, close_loc);
9024 break;
9025 case RID_AT_PROTOCOL:
9027 gcc_assert (c_dialect_objc ());
9028 c_parser_consume_token (parser);
9029 matching_parens parens;
9030 if (!parens.require_open (parser))
9032 expr.set_error ();
9033 break;
9035 if (c_parser_next_token_is_not (parser, CPP_NAME))
9037 c_parser_error (parser, "expected identifier");
9038 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9039 expr.set_error ();
9040 break;
9042 tree id = c_parser_peek_token (parser)->value;
9043 c_parser_consume_token (parser);
9044 location_t close_loc = c_parser_peek_token (parser)->location;
9045 parens.skip_until_found_close (parser);
9046 expr.value = objc_build_protocol_expr (id);
9047 set_c_expr_source_range (&expr, loc, close_loc);
9049 break;
9050 case RID_AT_ENCODE:
9052 /* Extension to support C-structures in the archiver. */
9053 gcc_assert (c_dialect_objc ());
9054 c_parser_consume_token (parser);
9055 matching_parens parens;
9056 if (!parens.require_open (parser))
9058 expr.set_error ();
9059 break;
9061 t1 = c_parser_type_name (parser);
9062 if (t1 == NULL)
9064 expr.set_error ();
9065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9066 break;
9068 location_t close_loc = c_parser_peek_token (parser)->location;
9069 parens.skip_until_found_close (parser);
9070 tree type = groktypename (t1, NULL, NULL);
9071 expr.value = objc_build_encode_expr (type);
9072 set_c_expr_source_range (&expr, loc, close_loc);
9074 break;
9075 case RID_GENERIC:
9076 expr = c_parser_generic_selection (parser);
9077 break;
9078 case RID_CILK_SPAWN:
9079 c_parser_consume_token (parser);
9080 if (!flag_cilkplus)
9082 error_at (loc, "-fcilkplus must be enabled to use "
9083 "%<_Cilk_spawn%>");
9084 expr = c_parser_cast_expression (parser, NULL);
9085 expr.set_error ();
9087 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
9089 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
9090 "are not permitted");
9091 /* Now flush out all the _Cilk_spawns. */
9092 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
9093 c_parser_consume_token (parser);
9094 expr = c_parser_cast_expression (parser, NULL);
9096 else
9098 expr = c_parser_cast_expression (parser, NULL);
9099 expr.value = build_cilk_spawn (loc, expr.value);
9101 break;
9102 default:
9103 c_parser_error (parser, "expected expression");
9104 expr.set_error ();
9105 break;
9107 break;
9108 case CPP_OPEN_SQUARE:
9109 if (c_dialect_objc ())
9111 tree receiver, args;
9112 c_parser_consume_token (parser);
9113 receiver = c_parser_objc_receiver (parser);
9114 args = c_parser_objc_message_args (parser);
9115 location_t close_loc = c_parser_peek_token (parser)->location;
9116 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9117 "expected %<]%>");
9118 expr.value = objc_build_message_expr (receiver, args);
9119 set_c_expr_source_range (&expr, loc, close_loc);
9120 break;
9122 /* Else fall through to report error. */
9123 /* FALLTHRU */
9124 default:
9125 c_parser_error (parser, "expected expression");
9126 expr.set_error ();
9127 break;
9129 out:
9130 return c_parser_postfix_expression_after_primary
9131 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9134 /* Parse a postfix expression after a parenthesized type name: the
9135 brace-enclosed initializer of a compound literal, possibly followed
9136 by some postfix operators. This is separate because it is not
9137 possible to tell until after the type name whether a cast
9138 expression has a cast or a compound literal, or whether the operand
9139 of sizeof is a parenthesized type name or starts with a compound
9140 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9141 location of the first token after the parentheses around the type
9142 name. */
9144 static struct c_expr
9145 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9146 struct c_type_name *type_name,
9147 location_t type_loc)
9149 tree type;
9150 struct c_expr init;
9151 bool non_const;
9152 struct c_expr expr;
9153 location_t start_loc;
9154 tree type_expr = NULL_TREE;
9155 bool type_expr_const = true;
9156 check_compound_literal_type (type_loc, type_name);
9157 rich_location richloc (line_table, type_loc);
9158 start_init (NULL_TREE, NULL, 0, &richloc);
9159 type = groktypename (type_name, &type_expr, &type_expr_const);
9160 start_loc = c_parser_peek_token (parser)->location;
9161 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9163 error_at (type_loc, "compound literal has variable size");
9164 type = error_mark_node;
9166 init = c_parser_braced_init (parser, type, false, NULL);
9167 finish_init ();
9168 maybe_warn_string_init (type_loc, type, init);
9170 if (type != error_mark_node
9171 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9172 && current_function_decl)
9174 error ("compound literal qualified by address-space qualifier");
9175 type = error_mark_node;
9178 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9179 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9180 ? CONSTRUCTOR_NON_CONST (init.value)
9181 : init.original_code == C_MAYBE_CONST_EXPR);
9182 non_const |= !type_expr_const;
9183 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
9184 set_c_expr_source_range (&expr, init.src_range);
9185 expr.original_code = ERROR_MARK;
9186 expr.original_type = NULL;
9187 if (type != error_mark_node
9188 && expr.value != error_mark_node
9189 && type_expr)
9191 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9193 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9194 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9196 else
9198 gcc_assert (!non_const);
9199 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9200 type_expr, expr.value);
9203 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9206 /* Callback function for sizeof_pointer_memaccess_warning to compare
9207 types. */
9209 static bool
9210 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9212 return comptypes (type1, type2) == 1;
9215 /* Parse a postfix expression after the initial primary or compound
9216 literal; that is, parse a series of postfix operators.
9218 EXPR_LOC is the location of the primary expression. */
9220 static struct c_expr
9221 c_parser_postfix_expression_after_primary (c_parser *parser,
9222 location_t expr_loc,
9223 struct c_expr expr)
9225 struct c_expr orig_expr;
9226 tree ident, idx;
9227 location_t sizeof_arg_loc[3], comp_loc;
9228 tree sizeof_arg[3];
9229 unsigned int literal_zero_mask;
9230 unsigned int i;
9231 vec<tree, va_gc> *exprlist;
9232 vec<tree, va_gc> *origtypes = NULL;
9233 vec<location_t> arg_loc = vNULL;
9234 location_t start;
9235 location_t finish;
9237 while (true)
9239 location_t op_loc = c_parser_peek_token (parser)->location;
9240 switch (c_parser_peek_token (parser)->type)
9242 case CPP_OPEN_SQUARE:
9243 /* Array reference. */
9244 c_parser_consume_token (parser);
9245 if (flag_cilkplus
9246 && c_parser_peek_token (parser)->type == CPP_COLON)
9247 /* If we are here, then we have something like this:
9248 Array [ : ]
9250 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
9251 expr.value);
9252 else
9254 idx = c_parser_expression (parser).value;
9255 /* Here we have 3 options:
9256 1. Array [EXPR] -- Normal Array call.
9257 2. Array [EXPR : EXPR] -- Array notation without stride.
9258 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
9260 For 1, we just handle it just like a normal array expression.
9261 For 2 and 3 we handle it like we handle array notations. The
9262 idx value we have above becomes the initial/start index.
9264 if (flag_cilkplus
9265 && c_parser_peek_token (parser)->type == CPP_COLON)
9266 expr.value = c_parser_array_notation (expr_loc, parser, idx,
9267 expr.value);
9268 else
9270 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9271 "expected %<]%>");
9272 start = expr.get_start ();
9273 finish = parser->tokens_buf[0].location;
9274 expr.value = build_array_ref (op_loc, expr.value, idx);
9275 set_c_expr_source_range (&expr, start, finish);
9278 expr.original_code = ERROR_MARK;
9279 expr.original_type = NULL;
9280 break;
9281 case CPP_OPEN_PAREN:
9282 /* Function call. */
9283 c_parser_consume_token (parser);
9284 for (i = 0; i < 3; i++)
9286 sizeof_arg[i] = NULL_TREE;
9287 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9289 literal_zero_mask = 0;
9290 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9291 exprlist = NULL;
9292 else
9293 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9294 sizeof_arg_loc, sizeof_arg,
9295 &arg_loc, &literal_zero_mask);
9296 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9297 "expected %<)%>");
9298 orig_expr = expr;
9299 mark_exp_read (expr.value);
9300 if (warn_sizeof_pointer_memaccess)
9301 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9302 expr.value, exprlist,
9303 sizeof_arg,
9304 sizeof_ptr_memacc_comptypes);
9305 if (TREE_CODE (expr.value) == FUNCTION_DECL
9306 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
9307 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
9308 && vec_safe_length (exprlist) == 3)
9310 tree arg0 = (*exprlist)[0];
9311 tree arg2 = (*exprlist)[2];
9312 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9315 start = expr.get_start ();
9316 finish = parser->tokens_buf[0].get_finish ();
9317 expr.value
9318 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9319 exprlist, origtypes);
9320 set_c_expr_source_range (&expr, start, finish);
9322 expr.original_code = ERROR_MARK;
9323 if (TREE_CODE (expr.value) == INTEGER_CST
9324 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9325 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
9326 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
9327 expr.original_code = C_MAYBE_CONST_EXPR;
9328 expr.original_type = NULL;
9329 if (exprlist)
9331 release_tree_vector (exprlist);
9332 release_tree_vector (origtypes);
9334 arg_loc.release ();
9335 break;
9336 case CPP_DOT:
9337 /* Structure element reference. */
9338 c_parser_consume_token (parser);
9339 expr = default_function_array_conversion (expr_loc, expr);
9340 if (c_parser_next_token_is (parser, CPP_NAME))
9342 c_token *comp_tok = c_parser_peek_token (parser);
9343 ident = comp_tok->value;
9344 comp_loc = comp_tok->location;
9346 else
9348 c_parser_error (parser, "expected identifier");
9349 expr.set_error ();
9350 expr.original_code = ERROR_MARK;
9351 expr.original_type = NULL;
9352 return expr;
9354 start = expr.get_start ();
9355 finish = c_parser_peek_token (parser)->get_finish ();
9356 c_parser_consume_token (parser);
9357 expr.value = build_component_ref (op_loc, expr.value, ident,
9358 comp_loc);
9359 set_c_expr_source_range (&expr, start, finish);
9360 expr.original_code = ERROR_MARK;
9361 if (TREE_CODE (expr.value) != COMPONENT_REF)
9362 expr.original_type = NULL;
9363 else
9365 /* Remember the original type of a bitfield. */
9366 tree field = TREE_OPERAND (expr.value, 1);
9367 if (TREE_CODE (field) != FIELD_DECL)
9368 expr.original_type = NULL;
9369 else
9370 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9372 break;
9373 case CPP_DEREF:
9374 /* Structure element reference. */
9375 c_parser_consume_token (parser);
9376 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9377 if (c_parser_next_token_is (parser, CPP_NAME))
9379 c_token *comp_tok = c_parser_peek_token (parser);
9380 ident = comp_tok->value;
9381 comp_loc = comp_tok->location;
9383 else
9385 c_parser_error (parser, "expected identifier");
9386 expr.set_error ();
9387 expr.original_code = ERROR_MARK;
9388 expr.original_type = NULL;
9389 return expr;
9391 start = expr.get_start ();
9392 finish = c_parser_peek_token (parser)->get_finish ();
9393 c_parser_consume_token (parser);
9394 expr.value = build_component_ref (op_loc,
9395 build_indirect_ref (op_loc,
9396 expr.value,
9397 RO_ARROW),
9398 ident, comp_loc);
9399 set_c_expr_source_range (&expr, start, finish);
9400 expr.original_code = ERROR_MARK;
9401 if (TREE_CODE (expr.value) != COMPONENT_REF)
9402 expr.original_type = NULL;
9403 else
9405 /* Remember the original type of a bitfield. */
9406 tree field = TREE_OPERAND (expr.value, 1);
9407 if (TREE_CODE (field) != FIELD_DECL)
9408 expr.original_type = NULL;
9409 else
9410 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9412 break;
9413 case CPP_PLUS_PLUS:
9414 /* Postincrement. */
9415 start = expr.get_start ();
9416 finish = c_parser_peek_token (parser)->get_finish ();
9417 c_parser_consume_token (parser);
9418 /* If the expressions have array notations, we expand them. */
9419 if (flag_cilkplus
9420 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
9421 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
9422 else
9424 expr = default_function_array_read_conversion (expr_loc, expr);
9425 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9426 expr.value, false);
9428 set_c_expr_source_range (&expr, start, finish);
9429 expr.original_code = ERROR_MARK;
9430 expr.original_type = NULL;
9431 break;
9432 case CPP_MINUS_MINUS:
9433 /* Postdecrement. */
9434 start = expr.get_start ();
9435 finish = c_parser_peek_token (parser)->get_finish ();
9436 c_parser_consume_token (parser);
9437 /* If the expressions have array notations, we expand them. */
9438 if (flag_cilkplus
9439 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
9440 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
9441 else
9443 expr = default_function_array_read_conversion (expr_loc, expr);
9444 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9445 expr.value, false);
9447 set_c_expr_source_range (&expr, start, finish);
9448 expr.original_code = ERROR_MARK;
9449 expr.original_type = NULL;
9450 break;
9451 default:
9452 return expr;
9457 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9459 expression:
9460 assignment-expression
9461 expression , assignment-expression
9464 static struct c_expr
9465 c_parser_expression (c_parser *parser)
9467 location_t tloc = c_parser_peek_token (parser)->location;
9468 struct c_expr expr;
9469 expr = c_parser_expr_no_commas (parser, NULL);
9470 if (c_parser_next_token_is (parser, CPP_COMMA))
9471 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9472 while (c_parser_next_token_is (parser, CPP_COMMA))
9474 struct c_expr next;
9475 tree lhsval;
9476 location_t loc = c_parser_peek_token (parser)->location;
9477 location_t expr_loc;
9478 c_parser_consume_token (parser);
9479 expr_loc = c_parser_peek_token (parser)->location;
9480 lhsval = expr.value;
9481 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9482 lhsval = TREE_OPERAND (lhsval, 1);
9483 if (DECL_P (lhsval) || handled_component_p (lhsval))
9484 mark_exp_read (lhsval);
9485 next = c_parser_expr_no_commas (parser, NULL);
9486 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9487 expr.value = build_compound_expr (loc, expr.value, next.value);
9488 expr.original_code = COMPOUND_EXPR;
9489 expr.original_type = next.original_type;
9491 return expr;
9494 /* Parse an expression and convert functions or arrays to pointers and
9495 lvalues to rvalues. */
9497 static struct c_expr
9498 c_parser_expression_conv (c_parser *parser)
9500 struct c_expr expr;
9501 location_t loc = c_parser_peek_token (parser)->location;
9502 expr = c_parser_expression (parser);
9503 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9504 return expr;
9507 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9508 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9510 static inline void
9511 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9512 unsigned int idx)
9514 if (idx >= HOST_BITS_PER_INT)
9515 return;
9517 c_token *tok = c_parser_peek_token (parser);
9518 switch (tok->type)
9520 case CPP_NUMBER:
9521 case CPP_CHAR:
9522 case CPP_WCHAR:
9523 case CPP_CHAR16:
9524 case CPP_CHAR32:
9525 /* If a parameter is literal zero alone, remember it
9526 for -Wmemset-transposed-args warning. */
9527 if (integer_zerop (tok->value)
9528 && !TREE_OVERFLOW (tok->value)
9529 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9530 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9531 *literal_zero_mask |= 1U << idx;
9532 default:
9533 break;
9537 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9538 functions and arrays to pointers and lvalues to rvalues. If
9539 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9540 locations of function arguments into this vector.
9542 nonempty-expr-list:
9543 assignment-expression
9544 nonempty-expr-list , assignment-expression
9547 static vec<tree, va_gc> *
9548 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9549 vec<tree, va_gc> **p_orig_types,
9550 location_t *sizeof_arg_loc, tree *sizeof_arg,
9551 vec<location_t> *locations,
9552 unsigned int *literal_zero_mask)
9554 vec<tree, va_gc> *ret;
9555 vec<tree, va_gc> *orig_types;
9556 struct c_expr expr;
9557 unsigned int idx = 0;
9559 ret = make_tree_vector ();
9560 if (p_orig_types == NULL)
9561 orig_types = NULL;
9562 else
9563 orig_types = make_tree_vector ();
9565 if (literal_zero_mask)
9566 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9567 expr = c_parser_expr_no_commas (parser, NULL);
9568 if (convert_p)
9569 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9570 if (fold_p)
9571 expr.value = c_fully_fold (expr.value, false, NULL);
9572 ret->quick_push (expr.value);
9573 if (orig_types)
9574 orig_types->quick_push (expr.original_type);
9575 if (locations)
9576 locations->safe_push (expr.get_location ());
9577 if (sizeof_arg != NULL
9578 && expr.original_code == SIZEOF_EXPR)
9580 sizeof_arg[0] = c_last_sizeof_arg;
9581 sizeof_arg_loc[0] = c_last_sizeof_loc;
9583 while (c_parser_next_token_is (parser, CPP_COMMA))
9585 c_parser_consume_token (parser);
9586 if (literal_zero_mask)
9587 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9588 expr = c_parser_expr_no_commas (parser, NULL);
9589 if (convert_p)
9590 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9591 true);
9592 if (fold_p)
9593 expr.value = c_fully_fold (expr.value, false, NULL);
9594 vec_safe_push (ret, expr.value);
9595 if (orig_types)
9596 vec_safe_push (orig_types, expr.original_type);
9597 if (locations)
9598 locations->safe_push (expr.get_location ());
9599 if (++idx < 3
9600 && sizeof_arg != NULL
9601 && expr.original_code == SIZEOF_EXPR)
9603 sizeof_arg[idx] = c_last_sizeof_arg;
9604 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9607 if (orig_types)
9608 *p_orig_types = orig_types;
9609 return ret;
9612 /* Parse Objective-C-specific constructs. */
9614 /* Parse an objc-class-definition.
9616 objc-class-definition:
9617 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9618 objc-class-instance-variables[opt] objc-methodprotolist @end
9619 @implementation identifier objc-superclass[opt]
9620 objc-class-instance-variables[opt]
9621 @interface identifier ( identifier ) objc-protocol-refs[opt]
9622 objc-methodprotolist @end
9623 @interface identifier ( ) objc-protocol-refs[opt]
9624 objc-methodprotolist @end
9625 @implementation identifier ( identifier )
9627 objc-superclass:
9628 : identifier
9630 "@interface identifier (" must start "@interface identifier (
9631 identifier ) ...": objc-methodprotolist in the first production may
9632 not start with a parenthesized identifier as a declarator of a data
9633 definition with no declaration specifiers if the objc-superclass,
9634 objc-protocol-refs and objc-class-instance-variables are omitted. */
9636 static void
9637 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9639 bool iface_p;
9640 tree id1;
9641 tree superclass;
9642 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9643 iface_p = true;
9644 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9645 iface_p = false;
9646 else
9647 gcc_unreachable ();
9649 c_parser_consume_token (parser);
9650 if (c_parser_next_token_is_not (parser, CPP_NAME))
9652 c_parser_error (parser, "expected identifier");
9653 return;
9655 id1 = c_parser_peek_token (parser)->value;
9656 c_parser_consume_token (parser);
9657 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9659 /* We have a category or class extension. */
9660 tree id2;
9661 tree proto = NULL_TREE;
9662 matching_parens parens;
9663 parens.consume_open (parser);
9664 if (c_parser_next_token_is_not (parser, CPP_NAME))
9666 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9668 /* We have a class extension. */
9669 id2 = NULL_TREE;
9671 else
9673 c_parser_error (parser, "expected identifier or %<)%>");
9674 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9675 return;
9678 else
9680 id2 = c_parser_peek_token (parser)->value;
9681 c_parser_consume_token (parser);
9683 parens.skip_until_found_close (parser);
9684 if (!iface_p)
9686 objc_start_category_implementation (id1, id2);
9687 return;
9689 if (c_parser_next_token_is (parser, CPP_LESS))
9690 proto = c_parser_objc_protocol_refs (parser);
9691 objc_start_category_interface (id1, id2, proto, attributes);
9692 c_parser_objc_methodprotolist (parser);
9693 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9694 objc_finish_interface ();
9695 return;
9697 if (c_parser_next_token_is (parser, CPP_COLON))
9699 c_parser_consume_token (parser);
9700 if (c_parser_next_token_is_not (parser, CPP_NAME))
9702 c_parser_error (parser, "expected identifier");
9703 return;
9705 superclass = c_parser_peek_token (parser)->value;
9706 c_parser_consume_token (parser);
9708 else
9709 superclass = NULL_TREE;
9710 if (iface_p)
9712 tree proto = NULL_TREE;
9713 if (c_parser_next_token_is (parser, CPP_LESS))
9714 proto = c_parser_objc_protocol_refs (parser);
9715 objc_start_class_interface (id1, superclass, proto, attributes);
9717 else
9718 objc_start_class_implementation (id1, superclass);
9719 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9720 c_parser_objc_class_instance_variables (parser);
9721 if (iface_p)
9723 objc_continue_interface ();
9724 c_parser_objc_methodprotolist (parser);
9725 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9726 objc_finish_interface ();
9728 else
9730 objc_continue_implementation ();
9731 return;
9735 /* Parse objc-class-instance-variables.
9737 objc-class-instance-variables:
9738 { objc-instance-variable-decl-list[opt] }
9740 objc-instance-variable-decl-list:
9741 objc-visibility-spec
9742 objc-instance-variable-decl ;
9744 objc-instance-variable-decl-list objc-visibility-spec
9745 objc-instance-variable-decl-list objc-instance-variable-decl ;
9746 objc-instance-variable-decl-list ;
9748 objc-visibility-spec:
9749 @private
9750 @protected
9751 @public
9753 objc-instance-variable-decl:
9754 struct-declaration
9757 static void
9758 c_parser_objc_class_instance_variables (c_parser *parser)
9760 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9761 c_parser_consume_token (parser);
9762 while (c_parser_next_token_is_not (parser, CPP_EOF))
9764 tree decls;
9765 /* Parse any stray semicolon. */
9766 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9768 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9769 "extra semicolon");
9770 c_parser_consume_token (parser);
9771 continue;
9773 /* Stop if at the end of the instance variables. */
9774 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9776 c_parser_consume_token (parser);
9777 break;
9779 /* Parse any objc-visibility-spec. */
9780 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9782 c_parser_consume_token (parser);
9783 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9784 continue;
9786 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9788 c_parser_consume_token (parser);
9789 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9790 continue;
9792 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9794 c_parser_consume_token (parser);
9795 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9796 continue;
9798 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9800 c_parser_consume_token (parser);
9801 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9802 continue;
9804 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9806 c_parser_pragma (parser, pragma_external, NULL);
9807 continue;
9810 /* Parse some comma-separated declarations. */
9811 decls = c_parser_struct_declaration (parser);
9812 if (decls == NULL)
9814 /* There is a syntax error. We want to skip the offending
9815 tokens up to the next ';' (included) or '}'
9816 (excluded). */
9818 /* First, skip manually a ')' or ']'. This is because they
9819 reduce the nesting level, so c_parser_skip_until_found()
9820 wouldn't be able to skip past them. */
9821 c_token *token = c_parser_peek_token (parser);
9822 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9823 c_parser_consume_token (parser);
9825 /* Then, do the standard skipping. */
9826 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9828 /* We hopefully recovered. Start normal parsing again. */
9829 parser->error = false;
9830 continue;
9832 else
9834 /* Comma-separated instance variables are chained together
9835 in reverse order; add them one by one. */
9836 tree ivar = nreverse (decls);
9837 for (; ivar; ivar = DECL_CHAIN (ivar))
9838 objc_add_instance_variable (copy_node (ivar));
9840 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9844 /* Parse an objc-class-declaration.
9846 objc-class-declaration:
9847 @class identifier-list ;
9850 static void
9851 c_parser_objc_class_declaration (c_parser *parser)
9853 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9854 c_parser_consume_token (parser);
9855 /* Any identifiers, including those declared as type names, are OK
9856 here. */
9857 while (true)
9859 tree id;
9860 if (c_parser_next_token_is_not (parser, CPP_NAME))
9862 c_parser_error (parser, "expected identifier");
9863 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9864 parser->error = false;
9865 return;
9867 id = c_parser_peek_token (parser)->value;
9868 objc_declare_class (id);
9869 c_parser_consume_token (parser);
9870 if (c_parser_next_token_is (parser, CPP_COMMA))
9871 c_parser_consume_token (parser);
9872 else
9873 break;
9875 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9878 /* Parse an objc-alias-declaration.
9880 objc-alias-declaration:
9881 @compatibility_alias identifier identifier ;
9884 static void
9885 c_parser_objc_alias_declaration (c_parser *parser)
9887 tree id1, id2;
9888 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9889 c_parser_consume_token (parser);
9890 if (c_parser_next_token_is_not (parser, CPP_NAME))
9892 c_parser_error (parser, "expected identifier");
9893 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9894 return;
9896 id1 = c_parser_peek_token (parser)->value;
9897 c_parser_consume_token (parser);
9898 if (c_parser_next_token_is_not (parser, CPP_NAME))
9900 c_parser_error (parser, "expected identifier");
9901 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9902 return;
9904 id2 = c_parser_peek_token (parser)->value;
9905 c_parser_consume_token (parser);
9906 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9907 objc_declare_alias (id1, id2);
9910 /* Parse an objc-protocol-definition.
9912 objc-protocol-definition:
9913 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9914 @protocol identifier-list ;
9916 "@protocol identifier ;" should be resolved as "@protocol
9917 identifier-list ;": objc-methodprotolist may not start with a
9918 semicolon in the first alternative if objc-protocol-refs are
9919 omitted. */
9921 static void
9922 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9924 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9926 c_parser_consume_token (parser);
9927 if (c_parser_next_token_is_not (parser, CPP_NAME))
9929 c_parser_error (parser, "expected identifier");
9930 return;
9932 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9933 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9935 /* Any identifiers, including those declared as type names, are
9936 OK here. */
9937 while (true)
9939 tree id;
9940 if (c_parser_next_token_is_not (parser, CPP_NAME))
9942 c_parser_error (parser, "expected identifier");
9943 break;
9945 id = c_parser_peek_token (parser)->value;
9946 objc_declare_protocol (id, attributes);
9947 c_parser_consume_token (parser);
9948 if (c_parser_next_token_is (parser, CPP_COMMA))
9949 c_parser_consume_token (parser);
9950 else
9951 break;
9953 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9955 else
9957 tree id = c_parser_peek_token (parser)->value;
9958 tree proto = NULL_TREE;
9959 c_parser_consume_token (parser);
9960 if (c_parser_next_token_is (parser, CPP_LESS))
9961 proto = c_parser_objc_protocol_refs (parser);
9962 parser->objc_pq_context = true;
9963 objc_start_protocol (id, proto, attributes);
9964 c_parser_objc_methodprotolist (parser);
9965 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9966 parser->objc_pq_context = false;
9967 objc_finish_interface ();
9971 /* Parse an objc-method-type.
9973 objc-method-type:
9977 Return true if it is a class method (+) and false if it is
9978 an instance method (-).
9980 static inline bool
9981 c_parser_objc_method_type (c_parser *parser)
9983 switch (c_parser_peek_token (parser)->type)
9985 case CPP_PLUS:
9986 c_parser_consume_token (parser);
9987 return true;
9988 case CPP_MINUS:
9989 c_parser_consume_token (parser);
9990 return false;
9991 default:
9992 gcc_unreachable ();
9996 /* Parse an objc-method-definition.
9998 objc-method-definition:
9999 objc-method-type objc-method-decl ;[opt] compound-statement
10002 static void
10003 c_parser_objc_method_definition (c_parser *parser)
10005 bool is_class_method = c_parser_objc_method_type (parser);
10006 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
10007 parser->objc_pq_context = true;
10008 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10009 &expr);
10010 if (decl == error_mark_node)
10011 return; /* Bail here. */
10013 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10015 c_parser_consume_token (parser);
10016 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10017 "extra semicolon in method definition specified");
10020 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10022 c_parser_error (parser, "expected %<{%>");
10023 return;
10026 parser->objc_pq_context = false;
10027 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
10029 add_stmt (c_parser_compound_statement (parser));
10030 objc_finish_method_definition (current_function_decl);
10032 else
10034 /* This code is executed when we find a method definition
10035 outside of an @implementation context (or invalid for other
10036 reasons). Parse the method (to keep going) but do not emit
10037 any code.
10039 c_parser_compound_statement (parser);
10043 /* Parse an objc-methodprotolist.
10045 objc-methodprotolist:
10046 empty
10047 objc-methodprotolist objc-methodproto
10048 objc-methodprotolist declaration
10049 objc-methodprotolist ;
10050 @optional
10051 @required
10053 The declaration is a data definition, which may be missing
10054 declaration specifiers under the same rules and diagnostics as
10055 other data definitions outside functions, and the stray semicolon
10056 is diagnosed the same way as a stray semicolon outside a
10057 function. */
10059 static void
10060 c_parser_objc_methodprotolist (c_parser *parser)
10062 while (true)
10064 /* The list is terminated by @end. */
10065 switch (c_parser_peek_token (parser)->type)
10067 case CPP_SEMICOLON:
10068 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10069 "ISO C does not allow extra %<;%> outside of a function");
10070 c_parser_consume_token (parser);
10071 break;
10072 case CPP_PLUS:
10073 case CPP_MINUS:
10074 c_parser_objc_methodproto (parser);
10075 break;
10076 case CPP_PRAGMA:
10077 c_parser_pragma (parser, pragma_external, NULL);
10078 break;
10079 case CPP_EOF:
10080 return;
10081 default:
10082 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
10083 return;
10084 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
10085 c_parser_objc_at_property_declaration (parser);
10086 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
10088 objc_set_method_opt (true);
10089 c_parser_consume_token (parser);
10091 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
10093 objc_set_method_opt (false);
10094 c_parser_consume_token (parser);
10096 else
10097 c_parser_declaration_or_fndef (parser, false, false, true,
10098 false, true, NULL, vNULL);
10099 break;
10104 /* Parse an objc-methodproto.
10106 objc-methodproto:
10107 objc-method-type objc-method-decl ;
10110 static void
10111 c_parser_objc_methodproto (c_parser *parser)
10113 bool is_class_method = c_parser_objc_method_type (parser);
10114 tree decl, attributes = NULL_TREE;
10116 /* Remember protocol qualifiers in prototypes. */
10117 parser->objc_pq_context = true;
10118 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10119 NULL);
10120 /* Forget protocol qualifiers now. */
10121 parser->objc_pq_context = false;
10123 /* Do not allow the presence of attributes to hide an erroneous
10124 method implementation in the interface section. */
10125 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
10127 c_parser_error (parser, "expected %<;%>");
10128 return;
10131 if (decl != error_mark_node)
10132 objc_add_method_declaration (is_class_method, decl, attributes);
10134 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10137 /* If we are at a position that method attributes may be present, check that
10138 there are not any parsed already (a syntax error) and then collect any
10139 specified at the current location. Finally, if new attributes were present,
10140 check that the next token is legal ( ';' for decls and '{' for defs). */
10142 static bool
10143 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
10145 bool bad = false;
10146 if (*attributes)
10148 c_parser_error (parser,
10149 "method attributes must be specified at the end only");
10150 *attributes = NULL_TREE;
10151 bad = true;
10154 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10155 *attributes = c_parser_attributes (parser);
10157 /* If there were no attributes here, just report any earlier error. */
10158 if (*attributes == NULL_TREE || bad)
10159 return bad;
10161 /* If the attributes are followed by a ; or {, then just report any earlier
10162 error. */
10163 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10164 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10165 return bad;
10167 /* We've got attributes, but not at the end. */
10168 c_parser_error (parser,
10169 "expected %<;%> or %<{%> after method attribute definition");
10170 return true;
10173 /* Parse an objc-method-decl.
10175 objc-method-decl:
10176 ( objc-type-name ) objc-selector
10177 objc-selector
10178 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10179 objc-keyword-selector objc-optparmlist
10180 attributes
10182 objc-keyword-selector:
10183 objc-keyword-decl
10184 objc-keyword-selector objc-keyword-decl
10186 objc-keyword-decl:
10187 objc-selector : ( objc-type-name ) identifier
10188 objc-selector : identifier
10189 : ( objc-type-name ) identifier
10190 : identifier
10192 objc-optparmlist:
10193 objc-optparms objc-optellipsis
10195 objc-optparms:
10196 empty
10197 objc-opt-parms , parameter-declaration
10199 objc-optellipsis:
10200 empty
10201 , ...
10204 static tree
10205 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10206 tree *attributes, tree *expr)
10208 tree type = NULL_TREE;
10209 tree sel;
10210 tree parms = NULL_TREE;
10211 bool ellipsis = false;
10212 bool attr_err = false;
10214 *attributes = NULL_TREE;
10215 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10217 matching_parens parens;
10218 parens.consume_open (parser);
10219 type = c_parser_objc_type_name (parser);
10220 parens.skip_until_found_close (parser);
10222 sel = c_parser_objc_selector (parser);
10223 /* If there is no selector, or a colon follows, we have an
10224 objc-keyword-selector. If there is a selector, and a colon does
10225 not follow, that selector ends the objc-method-decl. */
10226 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10228 tree tsel = sel;
10229 tree list = NULL_TREE;
10230 while (true)
10232 tree atype = NULL_TREE, id, keyworddecl;
10233 tree param_attr = NULL_TREE;
10234 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10235 break;
10236 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10238 c_parser_consume_token (parser);
10239 atype = c_parser_objc_type_name (parser);
10240 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10241 "expected %<)%>");
10243 /* New ObjC allows attributes on method parameters. */
10244 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10245 param_attr = c_parser_attributes (parser);
10246 if (c_parser_next_token_is_not (parser, CPP_NAME))
10248 c_parser_error (parser, "expected identifier");
10249 return error_mark_node;
10251 id = c_parser_peek_token (parser)->value;
10252 c_parser_consume_token (parser);
10253 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10254 list = chainon (list, keyworddecl);
10255 tsel = c_parser_objc_selector (parser);
10256 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10257 break;
10260 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10262 /* Parse the optional parameter list. Optional Objective-C
10263 method parameters follow the C syntax, and may include '...'
10264 to denote a variable number of arguments. */
10265 parms = make_node (TREE_LIST);
10266 while (c_parser_next_token_is (parser, CPP_COMMA))
10268 struct c_parm *parm;
10269 c_parser_consume_token (parser);
10270 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10272 ellipsis = true;
10273 c_parser_consume_token (parser);
10274 attr_err |= c_parser_objc_maybe_method_attributes
10275 (parser, attributes) ;
10276 break;
10278 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10279 if (parm == NULL)
10280 break;
10281 parms = chainon (parms,
10282 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10284 sel = list;
10286 else
10287 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10289 if (sel == NULL)
10291 c_parser_error (parser, "objective-c method declaration is expected");
10292 return error_mark_node;
10295 if (attr_err)
10296 return error_mark_node;
10298 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10301 /* Parse an objc-type-name.
10303 objc-type-name:
10304 objc-type-qualifiers[opt] type-name
10305 objc-type-qualifiers[opt]
10307 objc-type-qualifiers:
10308 objc-type-qualifier
10309 objc-type-qualifiers objc-type-qualifier
10311 objc-type-qualifier: one of
10312 in out inout bycopy byref oneway
10315 static tree
10316 c_parser_objc_type_name (c_parser *parser)
10318 tree quals = NULL_TREE;
10319 struct c_type_name *type_name = NULL;
10320 tree type = NULL_TREE;
10321 while (true)
10323 c_token *token = c_parser_peek_token (parser);
10324 if (token->type == CPP_KEYWORD
10325 && (token->keyword == RID_IN
10326 || token->keyword == RID_OUT
10327 || token->keyword == RID_INOUT
10328 || token->keyword == RID_BYCOPY
10329 || token->keyword == RID_BYREF
10330 || token->keyword == RID_ONEWAY))
10332 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10333 c_parser_consume_token (parser);
10335 else
10336 break;
10338 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10339 type_name = c_parser_type_name (parser);
10340 if (type_name)
10341 type = groktypename (type_name, NULL, NULL);
10343 /* If the type is unknown, and error has already been produced and
10344 we need to recover from the error. In that case, use NULL_TREE
10345 for the type, as if no type had been specified; this will use the
10346 default type ('id') which is good for error recovery. */
10347 if (type == error_mark_node)
10348 type = NULL_TREE;
10350 return build_tree_list (quals, type);
10353 /* Parse objc-protocol-refs.
10355 objc-protocol-refs:
10356 < identifier-list >
10359 static tree
10360 c_parser_objc_protocol_refs (c_parser *parser)
10362 tree list = NULL_TREE;
10363 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10364 c_parser_consume_token (parser);
10365 /* Any identifiers, including those declared as type names, are OK
10366 here. */
10367 while (true)
10369 tree id;
10370 if (c_parser_next_token_is_not (parser, CPP_NAME))
10372 c_parser_error (parser, "expected identifier");
10373 break;
10375 id = c_parser_peek_token (parser)->value;
10376 list = chainon (list, build_tree_list (NULL_TREE, id));
10377 c_parser_consume_token (parser);
10378 if (c_parser_next_token_is (parser, CPP_COMMA))
10379 c_parser_consume_token (parser);
10380 else
10381 break;
10383 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10384 return list;
10387 /* Parse an objc-try-catch-finally-statement.
10389 objc-try-catch-finally-statement:
10390 @try compound-statement objc-catch-list[opt]
10391 @try compound-statement objc-catch-list[opt] @finally compound-statement
10393 objc-catch-list:
10394 @catch ( objc-catch-parameter-declaration ) compound-statement
10395 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10397 objc-catch-parameter-declaration:
10398 parameter-declaration
10399 '...'
10401 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10403 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10404 for C++. Keep them in sync. */
10406 static void
10407 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10409 location_t location;
10410 tree stmt;
10412 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10413 c_parser_consume_token (parser);
10414 location = c_parser_peek_token (parser)->location;
10415 objc_maybe_warn_exceptions (location);
10416 stmt = c_parser_compound_statement (parser);
10417 objc_begin_try_stmt (location, stmt);
10419 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10421 struct c_parm *parm;
10422 tree parameter_declaration = error_mark_node;
10423 bool seen_open_paren = false;
10425 c_parser_consume_token (parser);
10426 matching_parens parens;
10427 if (!parens.require_open (parser))
10428 seen_open_paren = true;
10429 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10431 /* We have "@catch (...)" (where the '...' are literally
10432 what is in the code). Skip the '...'.
10433 parameter_declaration is set to NULL_TREE, and
10434 objc_being_catch_clauses() knows that that means
10435 '...'. */
10436 c_parser_consume_token (parser);
10437 parameter_declaration = NULL_TREE;
10439 else
10441 /* We have "@catch (NSException *exception)" or something
10442 like that. Parse the parameter declaration. */
10443 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10444 if (parm == NULL)
10445 parameter_declaration = error_mark_node;
10446 else
10447 parameter_declaration = grokparm (parm, NULL);
10449 if (seen_open_paren)
10450 parens.require_close (parser);
10451 else
10453 /* If there was no open parenthesis, we are recovering from
10454 an error, and we are trying to figure out what mistake
10455 the user has made. */
10457 /* If there is an immediate closing parenthesis, the user
10458 probably forgot the opening one (ie, they typed "@catch
10459 NSException *e)". Parse the closing parenthesis and keep
10460 going. */
10461 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10462 c_parser_consume_token (parser);
10464 /* If these is no immediate closing parenthesis, the user
10465 probably doesn't know that parenthesis are required at
10466 all (ie, they typed "@catch NSException *e"). So, just
10467 forget about the closing parenthesis and keep going. */
10469 objc_begin_catch_clause (parameter_declaration);
10470 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10471 c_parser_compound_statement_nostart (parser);
10472 objc_finish_catch_clause ();
10474 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10476 c_parser_consume_token (parser);
10477 location = c_parser_peek_token (parser)->location;
10478 stmt = c_parser_compound_statement (parser);
10479 objc_build_finally_clause (location, stmt);
10481 objc_finish_try_stmt ();
10484 /* Parse an objc-synchronized-statement.
10486 objc-synchronized-statement:
10487 @synchronized ( expression ) compound-statement
10490 static void
10491 c_parser_objc_synchronized_statement (c_parser *parser)
10493 location_t loc;
10494 tree expr, stmt;
10495 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10496 c_parser_consume_token (parser);
10497 loc = c_parser_peek_token (parser)->location;
10498 objc_maybe_warn_exceptions (loc);
10499 matching_parens parens;
10500 if (parens.require_open (parser))
10502 struct c_expr ce = c_parser_expression (parser);
10503 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10504 expr = ce.value;
10505 expr = c_fully_fold (expr, false, NULL);
10506 parens.skip_until_found_close (parser);
10508 else
10509 expr = error_mark_node;
10510 stmt = c_parser_compound_statement (parser);
10511 objc_build_synchronized (loc, expr, stmt);
10514 /* Parse an objc-selector; return NULL_TREE without an error if the
10515 next token is not an objc-selector.
10517 objc-selector:
10518 identifier
10519 one of
10520 enum struct union if else while do for switch case default
10521 break continue return goto asm sizeof typeof __alignof
10522 unsigned long const short volatile signed restrict _Complex
10523 in out inout bycopy byref oneway int char float double void _Bool
10524 _Atomic
10526 ??? Why this selection of keywords but not, for example, storage
10527 class specifiers? */
10529 static tree
10530 c_parser_objc_selector (c_parser *parser)
10532 c_token *token = c_parser_peek_token (parser);
10533 tree value = token->value;
10534 if (token->type == CPP_NAME)
10536 c_parser_consume_token (parser);
10537 return value;
10539 if (token->type != CPP_KEYWORD)
10540 return NULL_TREE;
10541 switch (token->keyword)
10543 case RID_ENUM:
10544 case RID_STRUCT:
10545 case RID_UNION:
10546 case RID_IF:
10547 case RID_ELSE:
10548 case RID_WHILE:
10549 case RID_DO:
10550 case RID_FOR:
10551 case RID_SWITCH:
10552 case RID_CASE:
10553 case RID_DEFAULT:
10554 case RID_BREAK:
10555 case RID_CONTINUE:
10556 case RID_RETURN:
10557 case RID_GOTO:
10558 case RID_ASM:
10559 case RID_SIZEOF:
10560 case RID_TYPEOF:
10561 case RID_ALIGNOF:
10562 case RID_UNSIGNED:
10563 case RID_LONG:
10564 case RID_CONST:
10565 case RID_SHORT:
10566 case RID_VOLATILE:
10567 case RID_SIGNED:
10568 case RID_RESTRICT:
10569 case RID_COMPLEX:
10570 case RID_IN:
10571 case RID_OUT:
10572 case RID_INOUT:
10573 case RID_BYCOPY:
10574 case RID_BYREF:
10575 case RID_ONEWAY:
10576 case RID_INT:
10577 case RID_CHAR:
10578 case RID_FLOAT:
10579 case RID_DOUBLE:
10580 CASE_RID_FLOATN_NX:
10581 case RID_VOID:
10582 case RID_BOOL:
10583 case RID_ATOMIC:
10584 case RID_AUTO_TYPE:
10585 case RID_INT_N_0:
10586 case RID_INT_N_1:
10587 case RID_INT_N_2:
10588 case RID_INT_N_3:
10589 c_parser_consume_token (parser);
10590 return value;
10591 default:
10592 return NULL_TREE;
10596 /* Parse an objc-selector-arg.
10598 objc-selector-arg:
10599 objc-selector
10600 objc-keywordname-list
10602 objc-keywordname-list:
10603 objc-keywordname
10604 objc-keywordname-list objc-keywordname
10606 objc-keywordname:
10607 objc-selector :
10611 static tree
10612 c_parser_objc_selector_arg (c_parser *parser)
10614 tree sel = c_parser_objc_selector (parser);
10615 tree list = NULL_TREE;
10616 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10617 return sel;
10618 while (true)
10620 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10621 return list;
10622 list = chainon (list, build_tree_list (sel, NULL_TREE));
10623 sel = c_parser_objc_selector (parser);
10624 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10625 break;
10627 return list;
10630 /* Parse an objc-receiver.
10632 objc-receiver:
10633 expression
10634 class-name
10635 type-name
10638 static tree
10639 c_parser_objc_receiver (c_parser *parser)
10641 location_t loc = c_parser_peek_token (parser)->location;
10643 if (c_parser_peek_token (parser)->type == CPP_NAME
10644 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10645 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10647 tree id = c_parser_peek_token (parser)->value;
10648 c_parser_consume_token (parser);
10649 return objc_get_class_reference (id);
10651 struct c_expr ce = c_parser_expression (parser);
10652 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10653 return c_fully_fold (ce.value, false, NULL);
10656 /* Parse objc-message-args.
10658 objc-message-args:
10659 objc-selector
10660 objc-keywordarg-list
10662 objc-keywordarg-list:
10663 objc-keywordarg
10664 objc-keywordarg-list objc-keywordarg
10666 objc-keywordarg:
10667 objc-selector : objc-keywordexpr
10668 : objc-keywordexpr
10671 static tree
10672 c_parser_objc_message_args (c_parser *parser)
10674 tree sel = c_parser_objc_selector (parser);
10675 tree list = NULL_TREE;
10676 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10677 return sel;
10678 while (true)
10680 tree keywordexpr;
10681 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10682 return error_mark_node;
10683 keywordexpr = c_parser_objc_keywordexpr (parser);
10684 list = chainon (list, build_tree_list (sel, keywordexpr));
10685 sel = c_parser_objc_selector (parser);
10686 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10687 break;
10689 return list;
10692 /* Parse an objc-keywordexpr.
10694 objc-keywordexpr:
10695 nonempty-expr-list
10698 static tree
10699 c_parser_objc_keywordexpr (c_parser *parser)
10701 tree ret;
10702 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10703 NULL, NULL, NULL, NULL);
10704 if (vec_safe_length (expr_list) == 1)
10706 /* Just return the expression, remove a level of
10707 indirection. */
10708 ret = (*expr_list)[0];
10710 else
10712 /* We have a comma expression, we will collapse later. */
10713 ret = build_tree_list_vec (expr_list);
10715 release_tree_vector (expr_list);
10716 return ret;
10719 /* A check, needed in several places, that ObjC interface, implementation or
10720 method definitions are not prefixed by incorrect items. */
10721 static bool
10722 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10723 struct c_declspecs *specs)
10725 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10726 || specs->typespec_kind != ctsk_none)
10728 c_parser_error (parser,
10729 "no type or storage class may be specified here,");
10730 c_parser_skip_to_end_of_block_or_statement (parser);
10731 return true;
10733 return false;
10736 /* Parse an Objective-C @property declaration. The syntax is:
10738 objc-property-declaration:
10739 '@property' objc-property-attributes[opt] struct-declaration ;
10741 objc-property-attributes:
10742 '(' objc-property-attribute-list ')'
10744 objc-property-attribute-list:
10745 objc-property-attribute
10746 objc-property-attribute-list, objc-property-attribute
10748 objc-property-attribute
10749 'getter' = identifier
10750 'setter' = identifier
10751 'readonly'
10752 'readwrite'
10753 'assign'
10754 'retain'
10755 'copy'
10756 'nonatomic'
10758 For example:
10759 @property NSString *name;
10760 @property (readonly) id object;
10761 @property (retain, nonatomic, getter=getTheName) id name;
10762 @property int a, b, c;
10764 PS: This function is identical to cp_parser_objc_at_propery_declaration
10765 for C++. Keep them in sync. */
10766 static void
10767 c_parser_objc_at_property_declaration (c_parser *parser)
10769 /* The following variables hold the attributes of the properties as
10770 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10771 seen. When we see an attribute, we set them to 'true' (if they
10772 are boolean properties) or to the identifier (if they have an
10773 argument, ie, for getter and setter). Note that here we only
10774 parse the list of attributes, check the syntax and accumulate the
10775 attributes that we find. objc_add_property_declaration() will
10776 then process the information. */
10777 bool property_assign = false;
10778 bool property_copy = false;
10779 tree property_getter_ident = NULL_TREE;
10780 bool property_nonatomic = false;
10781 bool property_readonly = false;
10782 bool property_readwrite = false;
10783 bool property_retain = false;
10784 tree property_setter_ident = NULL_TREE;
10786 /* 'properties' is the list of properties that we read. Usually a
10787 single one, but maybe more (eg, in "@property int a, b, c;" there
10788 are three). */
10789 tree properties;
10790 location_t loc;
10792 loc = c_parser_peek_token (parser)->location;
10793 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10795 c_parser_consume_token (parser); /* Eat '@property'. */
10797 /* Parse the optional attribute list... */
10798 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10800 matching_parens parens;
10802 /* Eat the '(' */
10803 parens.consume_open (parser);
10805 /* Property attribute keywords are valid now. */
10806 parser->objc_property_attr_context = true;
10808 while (true)
10810 bool syntax_error = false;
10811 c_token *token = c_parser_peek_token (parser);
10812 enum rid keyword;
10814 if (token->type != CPP_KEYWORD)
10816 if (token->type == CPP_CLOSE_PAREN)
10817 c_parser_error (parser, "expected identifier");
10818 else
10820 c_parser_consume_token (parser);
10821 c_parser_error (parser, "unknown property attribute");
10823 break;
10825 keyword = token->keyword;
10826 c_parser_consume_token (parser);
10827 switch (keyword)
10829 case RID_ASSIGN: property_assign = true; break;
10830 case RID_COPY: property_copy = true; break;
10831 case RID_NONATOMIC: property_nonatomic = true; break;
10832 case RID_READONLY: property_readonly = true; break;
10833 case RID_READWRITE: property_readwrite = true; break;
10834 case RID_RETAIN: property_retain = true; break;
10836 case RID_GETTER:
10837 case RID_SETTER:
10838 if (c_parser_next_token_is_not (parser, CPP_EQ))
10840 if (keyword == RID_GETTER)
10841 c_parser_error (parser,
10842 "missing %<=%> (after %<getter%> attribute)");
10843 else
10844 c_parser_error (parser,
10845 "missing %<=%> (after %<setter%> attribute)");
10846 syntax_error = true;
10847 break;
10849 c_parser_consume_token (parser); /* eat the = */
10850 if (c_parser_next_token_is_not (parser, CPP_NAME))
10852 c_parser_error (parser, "expected identifier");
10853 syntax_error = true;
10854 break;
10856 if (keyword == RID_SETTER)
10858 if (property_setter_ident != NULL_TREE)
10859 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10860 else
10861 property_setter_ident = c_parser_peek_token (parser)->value;
10862 c_parser_consume_token (parser);
10863 if (c_parser_next_token_is_not (parser, CPP_COLON))
10864 c_parser_error (parser, "setter name must terminate with %<:%>");
10865 else
10866 c_parser_consume_token (parser);
10868 else
10870 if (property_getter_ident != NULL_TREE)
10871 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10872 else
10873 property_getter_ident = c_parser_peek_token (parser)->value;
10874 c_parser_consume_token (parser);
10876 break;
10877 default:
10878 c_parser_error (parser, "unknown property attribute");
10879 syntax_error = true;
10880 break;
10883 if (syntax_error)
10884 break;
10886 if (c_parser_next_token_is (parser, CPP_COMMA))
10887 c_parser_consume_token (parser);
10888 else
10889 break;
10891 parser->objc_property_attr_context = false;
10892 parens.skip_until_found_close (parser);
10894 /* ... and the property declaration(s). */
10895 properties = c_parser_struct_declaration (parser);
10897 if (properties == error_mark_node)
10899 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10900 parser->error = false;
10901 return;
10904 if (properties == NULL_TREE)
10905 c_parser_error (parser, "expected identifier");
10906 else
10908 /* Comma-separated properties are chained together in
10909 reverse order; add them one by one. */
10910 properties = nreverse (properties);
10912 for (; properties; properties = TREE_CHAIN (properties))
10913 objc_add_property_declaration (loc, copy_node (properties),
10914 property_readonly, property_readwrite,
10915 property_assign, property_retain,
10916 property_copy, property_nonatomic,
10917 property_getter_ident, property_setter_ident);
10920 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10921 parser->error = false;
10924 /* Parse an Objective-C @synthesize declaration. The syntax is:
10926 objc-synthesize-declaration:
10927 @synthesize objc-synthesize-identifier-list ;
10929 objc-synthesize-identifier-list:
10930 objc-synthesize-identifier
10931 objc-synthesize-identifier-list, objc-synthesize-identifier
10933 objc-synthesize-identifier
10934 identifier
10935 identifier = identifier
10937 For example:
10938 @synthesize MyProperty;
10939 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10941 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10942 for C++. Keep them in sync.
10944 static void
10945 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10947 tree list = NULL_TREE;
10948 location_t loc;
10949 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10950 loc = c_parser_peek_token (parser)->location;
10952 c_parser_consume_token (parser);
10953 while (true)
10955 tree property, ivar;
10956 if (c_parser_next_token_is_not (parser, CPP_NAME))
10958 c_parser_error (parser, "expected identifier");
10959 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10960 /* Once we find the semicolon, we can resume normal parsing.
10961 We have to reset parser->error manually because
10962 c_parser_skip_until_found() won't reset it for us if the
10963 next token is precisely a semicolon. */
10964 parser->error = false;
10965 return;
10967 property = c_parser_peek_token (parser)->value;
10968 c_parser_consume_token (parser);
10969 if (c_parser_next_token_is (parser, CPP_EQ))
10971 c_parser_consume_token (parser);
10972 if (c_parser_next_token_is_not (parser, CPP_NAME))
10974 c_parser_error (parser, "expected identifier");
10975 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10976 parser->error = false;
10977 return;
10979 ivar = c_parser_peek_token (parser)->value;
10980 c_parser_consume_token (parser);
10982 else
10983 ivar = NULL_TREE;
10984 list = chainon (list, build_tree_list (ivar, property));
10985 if (c_parser_next_token_is (parser, CPP_COMMA))
10986 c_parser_consume_token (parser);
10987 else
10988 break;
10990 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10991 objc_add_synthesize_declaration (loc, list);
10994 /* Parse an Objective-C @dynamic declaration. The syntax is:
10996 objc-dynamic-declaration:
10997 @dynamic identifier-list ;
10999 For example:
11000 @dynamic MyProperty;
11001 @dynamic MyProperty, AnotherProperty;
11003 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
11004 for C++. Keep them in sync.
11006 static void
11007 c_parser_objc_at_dynamic_declaration (c_parser *parser)
11009 tree list = NULL_TREE;
11010 location_t loc;
11011 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
11012 loc = c_parser_peek_token (parser)->location;
11014 c_parser_consume_token (parser);
11015 while (true)
11017 tree property;
11018 if (c_parser_next_token_is_not (parser, CPP_NAME))
11020 c_parser_error (parser, "expected identifier");
11021 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11022 parser->error = false;
11023 return;
11025 property = c_parser_peek_token (parser)->value;
11026 list = chainon (list, build_tree_list (NULL_TREE, property));
11027 c_parser_consume_token (parser);
11028 if (c_parser_next_token_is (parser, CPP_COMMA))
11029 c_parser_consume_token (parser);
11030 else
11031 break;
11033 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11034 objc_add_dynamic_declaration (loc, list);
11038 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
11039 should be considered, statements. ALLOW_STMT is true if we're within
11040 the context of a function and such pragmas are to be allowed. Returns
11041 true if we actually parsed such a pragma. */
11043 static bool
11044 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
11046 unsigned int id;
11047 const char *construct = NULL;
11049 id = c_parser_peek_token (parser)->pragma_kind;
11050 gcc_assert (id != PRAGMA_NONE);
11052 switch (id)
11054 case PRAGMA_OACC_DECLARE:
11055 c_parser_oacc_declare (parser);
11056 return false;
11058 case PRAGMA_OACC_ENTER_DATA:
11059 if (context != pragma_compound)
11061 construct = "acc enter data";
11062 in_compound:
11063 if (context == pragma_stmt)
11065 error_at (c_parser_peek_token (parser)->location,
11066 "%<#pragma %s%> may only be used in compound "
11067 "statements", construct);
11068 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11069 return false;
11071 goto bad_stmt;
11073 c_parser_oacc_enter_exit_data (parser, true);
11074 return false;
11076 case PRAGMA_OACC_EXIT_DATA:
11077 if (context != pragma_compound)
11079 construct = "acc exit data";
11080 goto in_compound;
11082 c_parser_oacc_enter_exit_data (parser, false);
11083 return false;
11085 case PRAGMA_OACC_ROUTINE:
11086 if (context != pragma_external)
11088 error_at (c_parser_peek_token (parser)->location,
11089 "%<#pragma acc routine%> must be at file scope");
11090 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11091 return false;
11093 c_parser_oacc_routine (parser, context);
11094 return false;
11096 case PRAGMA_OACC_UPDATE:
11097 if (context != pragma_compound)
11099 construct = "acc update";
11100 goto in_compound;
11102 c_parser_oacc_update (parser);
11103 return false;
11105 case PRAGMA_OMP_BARRIER:
11106 if (context != pragma_compound)
11108 construct = "omp barrier";
11109 goto in_compound;
11111 c_parser_omp_barrier (parser);
11112 return false;
11114 case PRAGMA_OMP_FLUSH:
11115 if (context != pragma_compound)
11117 construct = "omp flush";
11118 goto in_compound;
11120 c_parser_omp_flush (parser);
11121 return false;
11123 case PRAGMA_OMP_TASKWAIT:
11124 if (context != pragma_compound)
11126 construct = "omp taskwait";
11127 goto in_compound;
11129 c_parser_omp_taskwait (parser);
11130 return false;
11132 case PRAGMA_OMP_TASKYIELD:
11133 if (context != pragma_compound)
11135 construct = "omp taskyield";
11136 goto in_compound;
11138 c_parser_omp_taskyield (parser);
11139 return false;
11141 case PRAGMA_OMP_CANCEL:
11142 if (context != pragma_compound)
11144 construct = "omp cancel";
11145 goto in_compound;
11147 c_parser_omp_cancel (parser);
11148 return false;
11150 case PRAGMA_OMP_CANCELLATION_POINT:
11151 c_parser_omp_cancellation_point (parser, context);
11152 return false;
11154 case PRAGMA_OMP_THREADPRIVATE:
11155 c_parser_omp_threadprivate (parser);
11156 return false;
11158 case PRAGMA_OMP_TARGET:
11159 return c_parser_omp_target (parser, context, if_p);
11161 case PRAGMA_OMP_END_DECLARE_TARGET:
11162 c_parser_omp_end_declare_target (parser);
11163 return false;
11165 case PRAGMA_OMP_SECTION:
11166 error_at (c_parser_peek_token (parser)->location,
11167 "%<#pragma omp section%> may only be used in "
11168 "%<#pragma omp sections%> construct");
11169 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11170 return false;
11172 case PRAGMA_OMP_DECLARE:
11173 c_parser_omp_declare (parser, context);
11174 return false;
11176 case PRAGMA_OMP_ORDERED:
11177 return c_parser_omp_ordered (parser, context, if_p);
11179 case PRAGMA_IVDEP:
11180 c_parser_consume_pragma (parser);
11181 c_parser_skip_to_pragma_eol (parser);
11182 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11183 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11184 && !c_parser_next_token_is_keyword (parser, RID_DO))
11186 c_parser_error (parser, "for, while or do statement expected");
11187 return false;
11189 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11190 c_parser_for_statement (parser, true, if_p);
11191 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11192 c_parser_while_statement (parser, true, if_p);
11193 else
11194 c_parser_do_statement (parser, true);
11195 return false;
11197 case PRAGMA_GCC_PCH_PREPROCESS:
11198 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11199 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11200 return false;
11202 case PRAGMA_CILK_SIMD:
11203 if (!c_parser_cilk_verify_simd (parser, context))
11204 return false;
11205 c_parser_consume_pragma (parser);
11206 c_parser_cilk_simd (parser, if_p);
11207 return false;
11208 case PRAGMA_CILK_GRAINSIZE:
11209 if (!flag_cilkplus)
11211 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
11212 " enabled");
11213 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11214 return false;
11216 if (context == pragma_external)
11218 error_at (c_parser_peek_token (parser)->location,
11219 "%<#pragma grainsize%> must be inside a function");
11220 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11221 return false;
11223 c_parser_cilk_grainsize (parser, if_p);
11224 return false;
11226 case PRAGMA_OACC_WAIT:
11227 if (context != pragma_compound)
11229 construct = "acc wait";
11230 goto in_compound;
11232 /* FALL THROUGH. */
11234 default:
11235 if (id < PRAGMA_FIRST_EXTERNAL)
11237 if (context != pragma_stmt && context != pragma_compound)
11239 bad_stmt:
11240 c_parser_error (parser, "expected declaration specifiers");
11241 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11242 return false;
11244 c_parser_omp_construct (parser, if_p);
11245 return true;
11247 break;
11250 c_parser_consume_pragma (parser);
11251 c_invoke_pragma_handler (id);
11253 /* Skip to EOL, but suppress any error message. Those will have been
11254 generated by the handler routine through calling error, as opposed
11255 to calling c_parser_error. */
11256 parser->error = true;
11257 c_parser_skip_to_pragma_eol (parser);
11259 return false;
11262 /* The interface the pragma parsers have to the lexer. */
11264 enum cpp_ttype
11265 pragma_lex (tree *value, location_t *loc)
11267 c_token *tok = c_parser_peek_token (the_parser);
11268 enum cpp_ttype ret = tok->type;
11270 *value = tok->value;
11271 if (loc)
11272 *loc = tok->location;
11274 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11275 ret = CPP_EOF;
11276 else
11278 if (ret == CPP_KEYWORD)
11279 ret = CPP_NAME;
11280 c_parser_consume_token (the_parser);
11283 return ret;
11286 static void
11287 c_parser_pragma_pch_preprocess (c_parser *parser)
11289 tree name = NULL;
11291 c_parser_consume_pragma (parser);
11292 if (c_parser_next_token_is (parser, CPP_STRING))
11294 name = c_parser_peek_token (parser)->value;
11295 c_parser_consume_token (parser);
11297 else
11298 c_parser_error (parser, "expected string literal");
11299 c_parser_skip_to_pragma_eol (parser);
11301 if (name)
11302 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11305 /* OpenACC and OpenMP parsing routines. */
11307 /* Returns name of the next clause.
11308 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11309 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11310 returned and the token is consumed. */
11312 static pragma_omp_clause
11313 c_parser_omp_clause_name (c_parser *parser)
11315 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11317 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11318 result = PRAGMA_OACC_CLAUSE_AUTO;
11319 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11320 result = PRAGMA_OMP_CLAUSE_IF;
11321 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11322 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11323 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11324 result = PRAGMA_OMP_CLAUSE_FOR;
11325 else if (c_parser_next_token_is (parser, CPP_NAME))
11327 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11329 switch (p[0])
11331 case 'a':
11332 if (!strcmp ("aligned", p))
11333 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11334 else if (!strcmp ("async", p))
11335 result = PRAGMA_OACC_CLAUSE_ASYNC;
11336 break;
11337 case 'c':
11338 if (!strcmp ("collapse", p))
11339 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11340 else if (!strcmp ("copy", p))
11341 result = PRAGMA_OACC_CLAUSE_COPY;
11342 else if (!strcmp ("copyin", p))
11343 result = PRAGMA_OMP_CLAUSE_COPYIN;
11344 else if (!strcmp ("copyout", p))
11345 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11346 else if (!strcmp ("copyprivate", p))
11347 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11348 else if (!strcmp ("create", p))
11349 result = PRAGMA_OACC_CLAUSE_CREATE;
11350 break;
11351 case 'd':
11352 if (!strcmp ("defaultmap", p))
11353 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11354 else if (!strcmp ("delete", p))
11355 result = PRAGMA_OACC_CLAUSE_DELETE;
11356 else if (!strcmp ("depend", p))
11357 result = PRAGMA_OMP_CLAUSE_DEPEND;
11358 else if (!strcmp ("device", p))
11359 result = PRAGMA_OMP_CLAUSE_DEVICE;
11360 else if (!strcmp ("deviceptr", p))
11361 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11362 else if (!strcmp ("device_resident", p))
11363 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11364 else if (!strcmp ("dist_schedule", p))
11365 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11366 break;
11367 case 'f':
11368 if (!strcmp ("final", p))
11369 result = PRAGMA_OMP_CLAUSE_FINAL;
11370 else if (!strcmp ("firstprivate", p))
11371 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11372 else if (!strcmp ("from", p))
11373 result = PRAGMA_OMP_CLAUSE_FROM;
11374 break;
11375 case 'g':
11376 if (!strcmp ("gang", p))
11377 result = PRAGMA_OACC_CLAUSE_GANG;
11378 else if (!strcmp ("grainsize", p))
11379 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11380 break;
11381 case 'h':
11382 if (!strcmp ("hint", p))
11383 result = PRAGMA_OMP_CLAUSE_HINT;
11384 else if (!strcmp ("host", p))
11385 result = PRAGMA_OACC_CLAUSE_HOST;
11386 break;
11387 case 'i':
11388 if (!strcmp ("inbranch", p))
11389 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11390 else if (!strcmp ("independent", p))
11391 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11392 else if (!strcmp ("is_device_ptr", p))
11393 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11394 break;
11395 case 'l':
11396 if (!strcmp ("lastprivate", p))
11397 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11398 else if (!strcmp ("linear", p))
11399 result = PRAGMA_OMP_CLAUSE_LINEAR;
11400 else if (!strcmp ("link", p))
11401 result = PRAGMA_OMP_CLAUSE_LINK;
11402 break;
11403 case 'm':
11404 if (!strcmp ("map", p))
11405 result = PRAGMA_OMP_CLAUSE_MAP;
11406 else if (!strcmp ("mergeable", p))
11407 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11408 else if (flag_cilkplus && !strcmp ("mask", p))
11409 result = PRAGMA_CILK_CLAUSE_MASK;
11410 break;
11411 case 'n':
11412 if (!strcmp ("nogroup", p))
11413 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11414 else if (!strcmp ("notinbranch", p))
11415 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11416 else if (!strcmp ("nowait", p))
11417 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11418 else if (!strcmp ("num_gangs", p))
11419 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11420 else if (!strcmp ("num_tasks", p))
11421 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11422 else if (!strcmp ("num_teams", p))
11423 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11424 else if (!strcmp ("num_threads", p))
11425 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11426 else if (!strcmp ("num_workers", p))
11427 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11428 else if (flag_cilkplus && !strcmp ("nomask", p))
11429 result = PRAGMA_CILK_CLAUSE_NOMASK;
11430 break;
11431 case 'o':
11432 if (!strcmp ("ordered", p))
11433 result = PRAGMA_OMP_CLAUSE_ORDERED;
11434 break;
11435 case 'p':
11436 if (!strcmp ("parallel", p))
11437 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11438 else if (!strcmp ("present", p))
11439 result = PRAGMA_OACC_CLAUSE_PRESENT;
11440 else if (!strcmp ("present_or_copy", p)
11441 || !strcmp ("pcopy", p))
11442 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
11443 else if (!strcmp ("present_or_copyin", p)
11444 || !strcmp ("pcopyin", p))
11445 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
11446 else if (!strcmp ("present_or_copyout", p)
11447 || !strcmp ("pcopyout", p))
11448 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
11449 else if (!strcmp ("present_or_create", p)
11450 || !strcmp ("pcreate", p))
11451 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
11452 else if (!strcmp ("priority", p))
11453 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11454 else if (!strcmp ("private", p))
11455 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11456 else if (!strcmp ("proc_bind", p))
11457 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11458 break;
11459 case 'r':
11460 if (!strcmp ("reduction", p))
11461 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11462 break;
11463 case 's':
11464 if (!strcmp ("safelen", p))
11465 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11466 else if (!strcmp ("schedule", p))
11467 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11468 else if (!strcmp ("sections", p))
11469 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11470 else if (!strcmp ("seq", p))
11471 result = PRAGMA_OACC_CLAUSE_SEQ;
11472 else if (!strcmp ("shared", p))
11473 result = PRAGMA_OMP_CLAUSE_SHARED;
11474 else if (!strcmp ("simd", p))
11475 result = PRAGMA_OMP_CLAUSE_SIMD;
11476 else if (!strcmp ("simdlen", p))
11477 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11478 else if (!strcmp ("self", p))
11479 result = PRAGMA_OACC_CLAUSE_SELF;
11480 break;
11481 case 't':
11482 if (!strcmp ("taskgroup", p))
11483 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11484 else if (!strcmp ("thread_limit", p))
11485 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11486 else if (!strcmp ("threads", p))
11487 result = PRAGMA_OMP_CLAUSE_THREADS;
11488 else if (!strcmp ("tile", p))
11489 result = PRAGMA_OACC_CLAUSE_TILE;
11490 else if (!strcmp ("to", p))
11491 result = PRAGMA_OMP_CLAUSE_TO;
11492 break;
11493 case 'u':
11494 if (!strcmp ("uniform", p))
11495 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11496 else if (!strcmp ("untied", p))
11497 result = PRAGMA_OMP_CLAUSE_UNTIED;
11498 else if (!strcmp ("use_device", p))
11499 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11500 else if (!strcmp ("use_device_ptr", p))
11501 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11502 break;
11503 case 'v':
11504 if (!strcmp ("vector", p))
11505 result = PRAGMA_OACC_CLAUSE_VECTOR;
11506 else if (!strcmp ("vector_length", p))
11507 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11508 else if (flag_cilkplus && !strcmp ("vectorlength", p))
11509 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
11510 break;
11511 case 'w':
11512 if (!strcmp ("wait", p))
11513 result = PRAGMA_OACC_CLAUSE_WAIT;
11514 else if (!strcmp ("worker", p))
11515 result = PRAGMA_OACC_CLAUSE_WORKER;
11516 break;
11520 if (result != PRAGMA_OMP_CLAUSE_NONE)
11521 c_parser_consume_token (parser);
11523 return result;
11526 /* Validate that a clause of the given type does not already exist. */
11528 static void
11529 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11530 const char *name)
11532 tree c;
11534 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11535 if (OMP_CLAUSE_CODE (c) == code)
11537 location_t loc = OMP_CLAUSE_LOCATION (c);
11538 error_at (loc, "too many %qs clauses", name);
11539 break;
11543 /* OpenACC 2.0
11544 Parse wait clause or wait directive parameters. */
11546 static tree
11547 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11549 vec<tree, va_gc> *args;
11550 tree t, args_tree;
11552 matching_parens parens;
11553 if (!parens.require_open (parser))
11554 return list;
11556 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11558 if (args->length () == 0)
11560 c_parser_error (parser, "expected integer expression before ')'");
11561 release_tree_vector (args);
11562 return list;
11565 args_tree = build_tree_list_vec (args);
11567 for (t = args_tree; t; t = TREE_CHAIN (t))
11569 tree targ = TREE_VALUE (t);
11571 if (targ != error_mark_node)
11573 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11575 c_parser_error (parser, "expression must be integral");
11576 targ = error_mark_node;
11578 else
11580 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11582 OMP_CLAUSE_DECL (c) = targ;
11583 OMP_CLAUSE_CHAIN (c) = list;
11584 list = c;
11589 release_tree_vector (args);
11590 parens.require_close (parser);
11591 return list;
11594 /* OpenACC 2.0, OpenMP 2.5:
11595 variable-list:
11596 identifier
11597 variable-list , identifier
11599 If KIND is nonzero, create the appropriate node and install the
11600 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11601 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11603 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11604 return the list created. */
11606 static tree
11607 c_parser_omp_variable_list (c_parser *parser,
11608 location_t clause_loc,
11609 enum omp_clause_code kind, tree list)
11611 if (c_parser_next_token_is_not (parser, CPP_NAME)
11612 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11613 c_parser_error (parser, "expected identifier");
11615 while (c_parser_next_token_is (parser, CPP_NAME)
11616 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11618 tree t = lookup_name (c_parser_peek_token (parser)->value);
11620 if (t == NULL_TREE)
11622 undeclared_variable (c_parser_peek_token (parser)->location,
11623 c_parser_peek_token (parser)->value);
11624 t = error_mark_node;
11627 c_parser_consume_token (parser);
11629 if (t == error_mark_node)
11631 else if (kind != 0)
11633 switch (kind)
11635 case OMP_CLAUSE__CACHE_:
11636 /* The OpenACC cache directive explicitly only allows "array
11637 elements or subarrays". */
11638 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11640 c_parser_error (parser, "expected %<[%>");
11641 t = error_mark_node;
11642 break;
11644 /* FALLTHROUGH */
11645 case OMP_CLAUSE_MAP:
11646 case OMP_CLAUSE_FROM:
11647 case OMP_CLAUSE_TO:
11648 while (c_parser_next_token_is (parser, CPP_DOT))
11650 location_t op_loc = c_parser_peek_token (parser)->location;
11651 c_parser_consume_token (parser);
11652 if (!c_parser_next_token_is (parser, CPP_NAME))
11654 c_parser_error (parser, "expected identifier");
11655 t = error_mark_node;
11656 break;
11659 c_token *comp_tok = c_parser_peek_token (parser);
11660 tree ident = comp_tok->value;
11661 location_t comp_loc = comp_tok->location;
11662 c_parser_consume_token (parser);
11663 t = build_component_ref (op_loc, t, ident, comp_loc);
11665 /* FALLTHROUGH */
11666 case OMP_CLAUSE_DEPEND:
11667 case OMP_CLAUSE_REDUCTION:
11668 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11670 tree low_bound = NULL_TREE, length = NULL_TREE;
11672 c_parser_consume_token (parser);
11673 if (!c_parser_next_token_is (parser, CPP_COLON))
11675 location_t expr_loc
11676 = c_parser_peek_token (parser)->location;
11677 c_expr expr = c_parser_expression (parser);
11678 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11679 false, true);
11680 low_bound = expr.value;
11682 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11683 length = integer_one_node;
11684 else
11686 /* Look for `:'. */
11687 if (!c_parser_require (parser, CPP_COLON,
11688 "expected %<:%>"))
11690 t = error_mark_node;
11691 break;
11693 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11695 location_t expr_loc
11696 = c_parser_peek_token (parser)->location;
11697 c_expr expr = c_parser_expression (parser);
11698 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11699 false, true);
11700 length = expr.value;
11703 /* Look for the closing `]'. */
11704 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11705 "expected %<]%>"))
11707 t = error_mark_node;
11708 break;
11711 t = tree_cons (low_bound, length, t);
11713 break;
11714 default:
11715 break;
11718 if (t != error_mark_node)
11720 tree u = build_omp_clause (clause_loc, kind);
11721 OMP_CLAUSE_DECL (u) = t;
11722 OMP_CLAUSE_CHAIN (u) = list;
11723 list = u;
11726 else
11727 list = tree_cons (t, NULL_TREE, list);
11729 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11730 break;
11732 c_parser_consume_token (parser);
11735 return list;
11738 /* Similarly, but expect leading and trailing parenthesis. This is a very
11739 common case for OpenACC and OpenMP clauses. */
11741 static tree
11742 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11743 tree list)
11745 /* The clauses location. */
11746 location_t loc = c_parser_peek_token (parser)->location;
11748 matching_parens parens;
11749 if (parens.require_open (parser))
11751 list = c_parser_omp_variable_list (parser, loc, kind, list);
11752 parens.skip_until_found_close (parser);
11754 return list;
11757 /* OpenACC 2.0:
11758 copy ( variable-list )
11759 copyin ( variable-list )
11760 copyout ( variable-list )
11761 create ( variable-list )
11762 delete ( variable-list )
11763 present ( variable-list )
11764 present_or_copy ( variable-list )
11765 pcopy ( variable-list )
11766 present_or_copyin ( variable-list )
11767 pcopyin ( variable-list )
11768 present_or_copyout ( variable-list )
11769 pcopyout ( variable-list )
11770 present_or_create ( variable-list )
11771 pcreate ( variable-list ) */
11773 static tree
11774 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11775 tree list)
11777 enum gomp_map_kind kind;
11778 switch (c_kind)
11780 case PRAGMA_OACC_CLAUSE_COPY:
11781 kind = GOMP_MAP_FORCE_TOFROM;
11782 break;
11783 case PRAGMA_OACC_CLAUSE_COPYIN:
11784 kind = GOMP_MAP_FORCE_TO;
11785 break;
11786 case PRAGMA_OACC_CLAUSE_COPYOUT:
11787 kind = GOMP_MAP_FORCE_FROM;
11788 break;
11789 case PRAGMA_OACC_CLAUSE_CREATE:
11790 kind = GOMP_MAP_FORCE_ALLOC;
11791 break;
11792 case PRAGMA_OACC_CLAUSE_DELETE:
11793 kind = GOMP_MAP_DELETE;
11794 break;
11795 case PRAGMA_OACC_CLAUSE_DEVICE:
11796 kind = GOMP_MAP_FORCE_TO;
11797 break;
11798 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11799 kind = GOMP_MAP_DEVICE_RESIDENT;
11800 break;
11801 case PRAGMA_OACC_CLAUSE_HOST:
11802 case PRAGMA_OACC_CLAUSE_SELF:
11803 kind = GOMP_MAP_FORCE_FROM;
11804 break;
11805 case PRAGMA_OACC_CLAUSE_LINK:
11806 kind = GOMP_MAP_LINK;
11807 break;
11808 case PRAGMA_OACC_CLAUSE_PRESENT:
11809 kind = GOMP_MAP_FORCE_PRESENT;
11810 break;
11811 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
11812 kind = GOMP_MAP_TOFROM;
11813 break;
11814 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
11815 kind = GOMP_MAP_TO;
11816 break;
11817 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
11818 kind = GOMP_MAP_FROM;
11819 break;
11820 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
11821 kind = GOMP_MAP_ALLOC;
11822 break;
11823 default:
11824 gcc_unreachable ();
11826 tree nl, c;
11827 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11829 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11830 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11832 return nl;
11835 /* OpenACC 2.0:
11836 deviceptr ( variable-list ) */
11838 static tree
11839 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11841 location_t loc = c_parser_peek_token (parser)->location;
11842 tree vars, t;
11844 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11845 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11846 variable-list must only allow for pointer variables. */
11847 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11848 for (t = vars; t && t; t = TREE_CHAIN (t))
11850 tree v = TREE_PURPOSE (t);
11852 /* FIXME diagnostics: Ideally we should keep individual
11853 locations for all the variables in the var list to make the
11854 following errors more precise. Perhaps
11855 c_parser_omp_var_list_parens() should construct a list of
11856 locations to go along with the var list. */
11858 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11859 error_at (loc, "%qD is not a variable", v);
11860 else if (TREE_TYPE (v) == error_mark_node)
11862 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11863 error_at (loc, "%qD is not a pointer variable", v);
11865 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11866 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11867 OMP_CLAUSE_DECL (u) = v;
11868 OMP_CLAUSE_CHAIN (u) = list;
11869 list = u;
11872 return list;
11875 /* OpenACC 2.0, OpenMP 3.0:
11876 collapse ( constant-expression ) */
11878 static tree
11879 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11881 tree c, num = error_mark_node;
11882 HOST_WIDE_INT n;
11883 location_t loc;
11885 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11886 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11888 loc = c_parser_peek_token (parser)->location;
11889 matching_parens parens;
11890 if (parens.require_open (parser))
11892 num = c_parser_expr_no_commas (parser, NULL).value;
11893 parens.skip_until_found_close (parser);
11895 if (num == error_mark_node)
11896 return list;
11897 mark_exp_read (num);
11898 num = c_fully_fold (num, false, NULL);
11899 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11900 || !tree_fits_shwi_p (num)
11901 || (n = tree_to_shwi (num)) <= 0
11902 || (int) n != n)
11904 error_at (loc,
11905 "collapse argument needs positive constant integer expression");
11906 return list;
11908 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11909 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11910 OMP_CLAUSE_CHAIN (c) = list;
11911 return c;
11914 /* OpenMP 2.5:
11915 copyin ( variable-list ) */
11917 static tree
11918 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11920 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11923 /* OpenMP 2.5:
11924 copyprivate ( variable-list ) */
11926 static tree
11927 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11929 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11932 /* OpenMP 2.5:
11933 default ( none | shared )
11935 OpenACC:
11936 default ( none | present ) */
11938 static tree
11939 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11941 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11942 location_t loc = c_parser_peek_token (parser)->location;
11943 tree c;
11945 matching_parens parens;
11946 if (!parens.require_open (parser))
11947 return list;
11948 if (c_parser_next_token_is (parser, CPP_NAME))
11950 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11952 switch (p[0])
11954 case 'n':
11955 if (strcmp ("none", p) != 0)
11956 goto invalid_kind;
11957 kind = OMP_CLAUSE_DEFAULT_NONE;
11958 break;
11960 case 'p':
11961 if (strcmp ("present", p) != 0 || !is_oacc)
11962 goto invalid_kind;
11963 kind = OMP_CLAUSE_DEFAULT_PRESENT;
11964 break;
11966 case 's':
11967 if (strcmp ("shared", p) != 0 || is_oacc)
11968 goto invalid_kind;
11969 kind = OMP_CLAUSE_DEFAULT_SHARED;
11970 break;
11972 default:
11973 goto invalid_kind;
11976 c_parser_consume_token (parser);
11978 else
11980 invalid_kind:
11981 if (is_oacc)
11982 c_parser_error (parser, "expected %<none%> or %<present%>");
11983 else
11984 c_parser_error (parser, "expected %<none%> or %<shared%>");
11986 parens.skip_until_found_close (parser);
11988 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11989 return list;
11991 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11992 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11993 OMP_CLAUSE_CHAIN (c) = list;
11994 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11996 return c;
11999 /* OpenMP 2.5:
12000 firstprivate ( variable-list ) */
12002 static tree
12003 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
12005 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
12008 /* OpenMP 3.1:
12009 final ( expression ) */
12011 static tree
12012 c_parser_omp_clause_final (c_parser *parser, tree list)
12014 location_t loc = c_parser_peek_token (parser)->location;
12015 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12017 tree t = c_parser_paren_condition (parser);
12018 tree c;
12020 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
12022 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
12023 OMP_CLAUSE_FINAL_EXPR (c) = t;
12024 OMP_CLAUSE_CHAIN (c) = list;
12025 list = c;
12027 else
12028 c_parser_error (parser, "expected %<(%>");
12030 return list;
12033 /* OpenACC, OpenMP 2.5:
12034 if ( expression )
12036 OpenMP 4.5:
12037 if ( directive-name-modifier : expression )
12039 directive-name-modifier:
12040 parallel | task | taskloop | target data | target | target update
12041 | target enter data | target exit data */
12043 static tree
12044 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
12046 location_t location = c_parser_peek_token (parser)->location;
12047 enum tree_code if_modifier = ERROR_MARK;
12049 matching_parens parens;
12050 if (!parens.require_open (parser))
12051 return list;
12053 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
12055 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12056 int n = 2;
12057 if (strcmp (p, "parallel") == 0)
12058 if_modifier = OMP_PARALLEL;
12059 else if (strcmp (p, "task") == 0)
12060 if_modifier = OMP_TASK;
12061 else if (strcmp (p, "taskloop") == 0)
12062 if_modifier = OMP_TASKLOOP;
12063 else if (strcmp (p, "target") == 0)
12065 if_modifier = OMP_TARGET;
12066 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12068 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
12069 if (strcmp ("data", p) == 0)
12070 if_modifier = OMP_TARGET_DATA;
12071 else if (strcmp ("update", p) == 0)
12072 if_modifier = OMP_TARGET_UPDATE;
12073 else if (strcmp ("enter", p) == 0)
12074 if_modifier = OMP_TARGET_ENTER_DATA;
12075 else if (strcmp ("exit", p) == 0)
12076 if_modifier = OMP_TARGET_EXIT_DATA;
12077 if (if_modifier != OMP_TARGET)
12079 n = 3;
12080 c_parser_consume_token (parser);
12082 else
12084 location_t loc = c_parser_peek_2nd_token (parser)->location;
12085 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
12086 "or %<exit%>");
12087 if_modifier = ERROR_MARK;
12089 if (if_modifier == OMP_TARGET_ENTER_DATA
12090 || if_modifier == OMP_TARGET_EXIT_DATA)
12092 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12094 p = IDENTIFIER_POINTER
12095 (c_parser_peek_2nd_token (parser)->value);
12096 if (strcmp ("data", p) == 0)
12097 n = 4;
12099 if (n == 4)
12100 c_parser_consume_token (parser);
12101 else
12103 location_t loc
12104 = c_parser_peek_2nd_token (parser)->location;
12105 error_at (loc, "expected %<data%>");
12106 if_modifier = ERROR_MARK;
12111 if (if_modifier != ERROR_MARK)
12113 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12115 c_parser_consume_token (parser);
12116 c_parser_consume_token (parser);
12118 else
12120 if (n > 2)
12122 location_t loc = c_parser_peek_2nd_token (parser)->location;
12123 error_at (loc, "expected %<:%>");
12125 if_modifier = ERROR_MARK;
12130 tree t = c_parser_condition (parser), c;
12131 parens.skip_until_found_close (parser);
12133 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12134 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12136 if (if_modifier != ERROR_MARK
12137 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12139 const char *p = NULL;
12140 switch (if_modifier)
12142 case OMP_PARALLEL: p = "parallel"; break;
12143 case OMP_TASK: p = "task"; break;
12144 case OMP_TASKLOOP: p = "taskloop"; break;
12145 case OMP_TARGET_DATA: p = "target data"; break;
12146 case OMP_TARGET: p = "target"; break;
12147 case OMP_TARGET_UPDATE: p = "target update"; break;
12148 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12149 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12150 default: gcc_unreachable ();
12152 error_at (location, "too many %<if%> clauses with %qs modifier",
12154 return list;
12156 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12158 if (!is_omp)
12159 error_at (location, "too many %<if%> clauses");
12160 else
12161 error_at (location, "too many %<if%> clauses without modifier");
12162 return list;
12164 else if (if_modifier == ERROR_MARK
12165 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12167 error_at (location, "if any %<if%> clause has modifier, then all "
12168 "%<if%> clauses have to use modifier");
12169 return list;
12173 c = build_omp_clause (location, OMP_CLAUSE_IF);
12174 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12175 OMP_CLAUSE_IF_EXPR (c) = t;
12176 OMP_CLAUSE_CHAIN (c) = list;
12177 return c;
12180 /* OpenMP 2.5:
12181 lastprivate ( variable-list ) */
12183 static tree
12184 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12186 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12189 /* OpenMP 3.1:
12190 mergeable */
12192 static tree
12193 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12195 tree c;
12197 /* FIXME: Should we allow duplicates? */
12198 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12200 c = build_omp_clause (c_parser_peek_token (parser)->location,
12201 OMP_CLAUSE_MERGEABLE);
12202 OMP_CLAUSE_CHAIN (c) = list;
12204 return c;
12207 /* OpenMP 2.5:
12208 nowait */
12210 static tree
12211 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12213 tree c;
12214 location_t loc = c_parser_peek_token (parser)->location;
12216 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12218 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12219 OMP_CLAUSE_CHAIN (c) = list;
12220 return c;
12223 /* OpenMP 2.5:
12224 num_threads ( expression ) */
12226 static tree
12227 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12229 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12230 matching_parens parens;
12231 if (parens.require_open (parser))
12233 location_t expr_loc = c_parser_peek_token (parser)->location;
12234 c_expr expr = c_parser_expression (parser);
12235 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12236 tree c, t = expr.value;
12237 t = c_fully_fold (t, false, NULL);
12239 parens.skip_until_found_close (parser);
12241 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12243 c_parser_error (parser, "expected integer expression");
12244 return list;
12247 /* Attempt to statically determine when the number isn't positive. */
12248 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12249 build_int_cst (TREE_TYPE (t), 0));
12250 protected_set_expr_location (c, expr_loc);
12251 if (c == boolean_true_node)
12253 warning_at (expr_loc, 0,
12254 "%<num_threads%> value must be positive");
12255 t = integer_one_node;
12258 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12260 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12261 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12262 OMP_CLAUSE_CHAIN (c) = list;
12263 list = c;
12266 return list;
12269 /* OpenMP 4.5:
12270 num_tasks ( expression ) */
12272 static tree
12273 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12275 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12276 matching_parens parens;
12277 if (parens.require_open (parser))
12279 location_t expr_loc = c_parser_peek_token (parser)->location;
12280 c_expr expr = c_parser_expression (parser);
12281 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12282 tree c, t = expr.value;
12283 t = c_fully_fold (t, false, NULL);
12285 parens.skip_until_found_close (parser);
12287 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12289 c_parser_error (parser, "expected integer expression");
12290 return list;
12293 /* Attempt to statically determine when the number isn't positive. */
12294 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12295 build_int_cst (TREE_TYPE (t), 0));
12296 if (CAN_HAVE_LOCATION_P (c))
12297 SET_EXPR_LOCATION (c, expr_loc);
12298 if (c == boolean_true_node)
12300 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12301 t = integer_one_node;
12304 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12306 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12307 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12308 OMP_CLAUSE_CHAIN (c) = list;
12309 list = c;
12312 return list;
12315 /* OpenMP 4.5:
12316 grainsize ( expression ) */
12318 static tree
12319 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12321 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12322 matching_parens parens;
12323 if (parens.require_open (parser))
12325 location_t expr_loc = c_parser_peek_token (parser)->location;
12326 c_expr expr = c_parser_expression (parser);
12327 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12328 tree c, t = expr.value;
12329 t = c_fully_fold (t, false, NULL);
12331 parens.skip_until_found_close (parser);
12333 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12335 c_parser_error (parser, "expected integer expression");
12336 return list;
12339 /* Attempt to statically determine when the number isn't positive. */
12340 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12341 build_int_cst (TREE_TYPE (t), 0));
12342 if (CAN_HAVE_LOCATION_P (c))
12343 SET_EXPR_LOCATION (c, expr_loc);
12344 if (c == boolean_true_node)
12346 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12347 t = integer_one_node;
12350 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12352 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12353 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12354 OMP_CLAUSE_CHAIN (c) = list;
12355 list = c;
12358 return list;
12361 /* OpenMP 4.5:
12362 priority ( expression ) */
12364 static tree
12365 c_parser_omp_clause_priority (c_parser *parser, tree list)
12367 location_t priority_loc = c_parser_peek_token (parser)->location;
12368 matching_parens parens;
12369 if (parens.require_open (parser))
12371 location_t expr_loc = c_parser_peek_token (parser)->location;
12372 c_expr expr = c_parser_expression (parser);
12373 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12374 tree c, t = expr.value;
12375 t = c_fully_fold (t, false, NULL);
12377 parens.skip_until_found_close (parser);
12379 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12381 c_parser_error (parser, "expected integer expression");
12382 return list;
12385 /* Attempt to statically determine when the number isn't
12386 non-negative. */
12387 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12388 build_int_cst (TREE_TYPE (t), 0));
12389 if (CAN_HAVE_LOCATION_P (c))
12390 SET_EXPR_LOCATION (c, expr_loc);
12391 if (c == boolean_true_node)
12393 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12394 t = integer_one_node;
12397 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12399 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12400 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12401 OMP_CLAUSE_CHAIN (c) = list;
12402 list = c;
12405 return list;
12408 /* OpenMP 4.5:
12409 hint ( expression ) */
12411 static tree
12412 c_parser_omp_clause_hint (c_parser *parser, tree list)
12414 location_t hint_loc = c_parser_peek_token (parser)->location;
12415 matching_parens parens;
12416 if (parens.require_open (parser))
12418 location_t expr_loc = c_parser_peek_token (parser)->location;
12419 c_expr expr = c_parser_expression (parser);
12420 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12421 tree c, t = expr.value;
12422 t = c_fully_fold (t, false, NULL);
12424 parens.skip_until_found_close (parser);
12426 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12428 c_parser_error (parser, "expected integer expression");
12429 return list;
12432 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12434 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12435 OMP_CLAUSE_HINT_EXPR (c) = t;
12436 OMP_CLAUSE_CHAIN (c) = list;
12437 list = c;
12440 return list;
12443 /* OpenMP 4.5:
12444 defaultmap ( tofrom : scalar ) */
12446 static tree
12447 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12449 location_t loc = c_parser_peek_token (parser)->location;
12450 tree c;
12451 const char *p;
12453 matching_parens parens;
12454 if (!parens.require_open (parser))
12455 return list;
12456 if (!c_parser_next_token_is (parser, CPP_NAME))
12458 c_parser_error (parser, "expected %<tofrom%>");
12459 goto out_err;
12461 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12462 if (strcmp (p, "tofrom") != 0)
12464 c_parser_error (parser, "expected %<tofrom%>");
12465 goto out_err;
12467 c_parser_consume_token (parser);
12468 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12469 goto out_err;
12470 if (!c_parser_next_token_is (parser, CPP_NAME))
12472 c_parser_error (parser, "expected %<scalar%>");
12473 goto out_err;
12475 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12476 if (strcmp (p, "scalar") != 0)
12478 c_parser_error (parser, "expected %<scalar%>");
12479 goto out_err;
12481 c_parser_consume_token (parser);
12482 parens.skip_until_found_close (parser);
12483 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12484 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12485 OMP_CLAUSE_CHAIN (c) = list;
12486 return c;
12488 out_err:
12489 parens.skip_until_found_close (parser);
12490 return list;
12493 /* OpenACC 2.0:
12494 use_device ( variable-list )
12496 OpenMP 4.5:
12497 use_device_ptr ( variable-list ) */
12499 static tree
12500 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12502 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12503 list);
12506 /* OpenMP 4.5:
12507 is_device_ptr ( variable-list ) */
12509 static tree
12510 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12512 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12515 /* OpenACC:
12516 num_gangs ( expression )
12517 num_workers ( expression )
12518 vector_length ( expression ) */
12520 static tree
12521 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12522 tree list)
12524 location_t loc = c_parser_peek_token (parser)->location;
12526 matching_parens parens;
12527 if (!parens.require_open (parser))
12528 return list;
12530 location_t expr_loc = c_parser_peek_token (parser)->location;
12531 c_expr expr = c_parser_expression (parser);
12532 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12533 tree c, t = expr.value;
12534 t = c_fully_fold (t, false, NULL);
12536 parens.skip_until_found_close (parser);
12538 if (t == error_mark_node)
12539 return list;
12540 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12542 error_at (expr_loc, "%qs expression must be integral",
12543 omp_clause_code_name[code]);
12544 return list;
12547 /* Attempt to statically determine when the number isn't positive. */
12548 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12549 build_int_cst (TREE_TYPE (t), 0));
12550 protected_set_expr_location (c, expr_loc);
12551 if (c == boolean_true_node)
12553 warning_at (expr_loc, 0,
12554 "%qs value must be positive",
12555 omp_clause_code_name[code]);
12556 t = integer_one_node;
12559 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12561 c = build_omp_clause (loc, code);
12562 OMP_CLAUSE_OPERAND (c, 0) = t;
12563 OMP_CLAUSE_CHAIN (c) = list;
12564 return c;
12567 /* OpenACC:
12569 gang [( gang-arg-list )]
12570 worker [( [num:] int-expr )]
12571 vector [( [length:] int-expr )]
12573 where gang-arg is one of:
12575 [num:] int-expr
12576 static: size-expr
12578 and size-expr may be:
12581 int-expr
12584 static tree
12585 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12586 const char *str, tree list)
12588 const char *id = "num";
12589 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12590 location_t loc = c_parser_peek_token (parser)->location;
12592 if (kind == OMP_CLAUSE_VECTOR)
12593 id = "length";
12595 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12597 c_parser_consume_token (parser);
12601 c_token *next = c_parser_peek_token (parser);
12602 int idx = 0;
12604 /* Gang static argument. */
12605 if (kind == OMP_CLAUSE_GANG
12606 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12608 c_parser_consume_token (parser);
12610 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12611 goto cleanup_error;
12613 idx = 1;
12614 if (ops[idx] != NULL_TREE)
12616 c_parser_error (parser, "too many %<static%> arguments");
12617 goto cleanup_error;
12620 /* Check for the '*' argument. */
12621 if (c_parser_next_token_is (parser, CPP_MULT)
12622 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12623 || c_parser_peek_2nd_token (parser)->type
12624 == CPP_CLOSE_PAREN))
12626 c_parser_consume_token (parser);
12627 ops[idx] = integer_minus_one_node;
12629 if (c_parser_next_token_is (parser, CPP_COMMA))
12631 c_parser_consume_token (parser);
12632 continue;
12634 else
12635 break;
12638 /* Worker num: argument and vector length: arguments. */
12639 else if (c_parser_next_token_is (parser, CPP_NAME)
12640 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12641 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12643 c_parser_consume_token (parser); /* id */
12644 c_parser_consume_token (parser); /* ':' */
12647 /* Now collect the actual argument. */
12648 if (ops[idx] != NULL_TREE)
12650 c_parser_error (parser, "unexpected argument");
12651 goto cleanup_error;
12654 location_t expr_loc = c_parser_peek_token (parser)->location;
12655 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12656 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12657 tree expr = cexpr.value;
12658 if (expr == error_mark_node)
12659 goto cleanup_error;
12661 expr = c_fully_fold (expr, false, NULL);
12663 /* Attempt to statically determine when the number isn't a
12664 positive integer. */
12666 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12668 c_parser_error (parser, "expected integer expression");
12669 return list;
12672 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12673 build_int_cst (TREE_TYPE (expr), 0));
12674 if (c == boolean_true_node)
12676 warning_at (loc, 0,
12677 "%qs value must be positive", str);
12678 expr = integer_one_node;
12681 ops[idx] = expr;
12683 if (kind == OMP_CLAUSE_GANG
12684 && c_parser_next_token_is (parser, CPP_COMMA))
12686 c_parser_consume_token (parser);
12687 continue;
12689 break;
12691 while (1);
12693 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12694 goto cleanup_error;
12697 check_no_duplicate_clause (list, kind, str);
12699 c = build_omp_clause (loc, kind);
12701 if (ops[1])
12702 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12704 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12705 OMP_CLAUSE_CHAIN (c) = list;
12707 return c;
12709 cleanup_error:
12710 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12711 return list;
12714 /* OpenACC:
12715 auto
12716 independent
12717 nohost
12718 seq */
12720 static tree
12721 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12722 tree list)
12724 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12726 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12727 OMP_CLAUSE_CHAIN (c) = list;
12729 return c;
12732 /* OpenACC:
12733 async [( int-expr )] */
12735 static tree
12736 c_parser_oacc_clause_async (c_parser *parser, tree list)
12738 tree c, t;
12739 location_t loc = c_parser_peek_token (parser)->location;
12741 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12743 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12745 c_parser_consume_token (parser);
12747 t = c_parser_expression (parser).value;
12748 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12749 c_parser_error (parser, "expected integer expression");
12750 else if (t == error_mark_node
12751 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12752 return list;
12754 else
12755 t = c_fully_fold (t, false, NULL);
12757 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12759 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12760 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12761 OMP_CLAUSE_CHAIN (c) = list;
12762 list = c;
12764 return list;
12767 /* OpenACC 2.0:
12768 tile ( size-expr-list ) */
12770 static tree
12771 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12773 tree c, expr = error_mark_node;
12774 location_t loc;
12775 tree tile = NULL_TREE;
12777 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12778 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12780 loc = c_parser_peek_token (parser)->location;
12781 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12782 return list;
12786 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12787 return list;
12789 if (c_parser_next_token_is (parser, CPP_MULT)
12790 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12791 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12793 c_parser_consume_token (parser);
12794 expr = integer_zero_node;
12796 else
12798 location_t expr_loc = c_parser_peek_token (parser)->location;
12799 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12800 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12801 expr = cexpr.value;
12803 if (expr == error_mark_node)
12805 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12806 "expected %<)%>");
12807 return list;
12810 expr = c_fully_fold (expr, false, NULL);
12812 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12813 || !tree_fits_shwi_p (expr)
12814 || tree_to_shwi (expr) <= 0)
12816 error_at (expr_loc, "%<tile%> argument needs positive"
12817 " integral constant");
12818 expr = integer_zero_node;
12822 tile = tree_cons (NULL_TREE, expr, tile);
12824 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12826 /* Consume the trailing ')'. */
12827 c_parser_consume_token (parser);
12829 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12830 tile = nreverse (tile);
12831 OMP_CLAUSE_TILE_LIST (c) = tile;
12832 OMP_CLAUSE_CHAIN (c) = list;
12833 return c;
12836 /* OpenACC:
12837 wait ( int-expr-list ) */
12839 static tree
12840 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12842 location_t clause_loc = c_parser_peek_token (parser)->location;
12844 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12845 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12847 return list;
12850 /* OpenMP 2.5:
12851 ordered
12853 OpenMP 4.5:
12854 ordered ( constant-expression ) */
12856 static tree
12857 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12859 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12861 tree c, num = NULL_TREE;
12862 HOST_WIDE_INT n;
12863 location_t loc = c_parser_peek_token (parser)->location;
12864 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12866 matching_parens parens;
12867 parens.consume_open (parser);
12868 num = c_parser_expr_no_commas (parser, NULL).value;
12869 parens.skip_until_found_close (parser);
12871 if (num == error_mark_node)
12872 return list;
12873 if (num)
12875 mark_exp_read (num);
12876 num = c_fully_fold (num, false, NULL);
12877 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12878 || !tree_fits_shwi_p (num)
12879 || (n = tree_to_shwi (num)) <= 0
12880 || (int) n != n)
12882 error_at (loc, "ordered argument needs positive "
12883 "constant integer expression");
12884 return list;
12887 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12888 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12889 OMP_CLAUSE_CHAIN (c) = list;
12890 return c;
12893 /* OpenMP 2.5:
12894 private ( variable-list ) */
12896 static tree
12897 c_parser_omp_clause_private (c_parser *parser, tree list)
12899 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12902 /* OpenMP 2.5:
12903 reduction ( reduction-operator : variable-list )
12905 reduction-operator:
12906 One of: + * - & ^ | && ||
12908 OpenMP 3.1:
12910 reduction-operator:
12911 One of: + * - & ^ | && || max min
12913 OpenMP 4.0:
12915 reduction-operator:
12916 One of: + * - & ^ | && ||
12917 identifier */
12919 static tree
12920 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12922 location_t clause_loc = c_parser_peek_token (parser)->location;
12923 matching_parens parens;
12924 if (parens.require_open (parser))
12926 enum tree_code code = ERROR_MARK;
12927 tree reduc_id = NULL_TREE;
12929 switch (c_parser_peek_token (parser)->type)
12931 case CPP_PLUS:
12932 code = PLUS_EXPR;
12933 break;
12934 case CPP_MULT:
12935 code = MULT_EXPR;
12936 break;
12937 case CPP_MINUS:
12938 code = MINUS_EXPR;
12939 break;
12940 case CPP_AND:
12941 code = BIT_AND_EXPR;
12942 break;
12943 case CPP_XOR:
12944 code = BIT_XOR_EXPR;
12945 break;
12946 case CPP_OR:
12947 code = BIT_IOR_EXPR;
12948 break;
12949 case CPP_AND_AND:
12950 code = TRUTH_ANDIF_EXPR;
12951 break;
12952 case CPP_OR_OR:
12953 code = TRUTH_ORIF_EXPR;
12954 break;
12955 case CPP_NAME:
12957 const char *p
12958 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12959 if (strcmp (p, "min") == 0)
12961 code = MIN_EXPR;
12962 break;
12964 if (strcmp (p, "max") == 0)
12966 code = MAX_EXPR;
12967 break;
12969 reduc_id = c_parser_peek_token (parser)->value;
12970 break;
12972 default:
12973 c_parser_error (parser,
12974 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12975 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12976 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12977 return list;
12979 c_parser_consume_token (parser);
12980 reduc_id = c_omp_reduction_id (code, reduc_id);
12981 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12983 tree nl, c;
12985 nl = c_parser_omp_variable_list (parser, clause_loc,
12986 OMP_CLAUSE_REDUCTION, list);
12987 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12989 tree d = OMP_CLAUSE_DECL (c), type;
12990 if (TREE_CODE (d) != TREE_LIST)
12991 type = TREE_TYPE (d);
12992 else
12994 int cnt = 0;
12995 tree t;
12996 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12997 cnt++;
12998 type = TREE_TYPE (t);
12999 while (cnt > 0)
13001 if (TREE_CODE (type) != POINTER_TYPE
13002 && TREE_CODE (type) != ARRAY_TYPE)
13003 break;
13004 type = TREE_TYPE (type);
13005 cnt--;
13008 while (TREE_CODE (type) == ARRAY_TYPE)
13009 type = TREE_TYPE (type);
13010 OMP_CLAUSE_REDUCTION_CODE (c) = code;
13011 if (code == ERROR_MARK
13012 || !(INTEGRAL_TYPE_P (type)
13013 || TREE_CODE (type) == REAL_TYPE
13014 || TREE_CODE (type) == COMPLEX_TYPE))
13015 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
13016 = c_omp_reduction_lookup (reduc_id,
13017 TYPE_MAIN_VARIANT (type));
13020 list = nl;
13022 parens.skip_until_found_close (parser);
13024 return list;
13027 /* OpenMP 2.5:
13028 schedule ( schedule-kind )
13029 schedule ( schedule-kind , expression )
13031 schedule-kind:
13032 static | dynamic | guided | runtime | auto
13034 OpenMP 4.5:
13035 schedule ( schedule-modifier : schedule-kind )
13036 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
13038 schedule-modifier:
13039 simd
13040 monotonic
13041 nonmonotonic */
13043 static tree
13044 c_parser_omp_clause_schedule (c_parser *parser, tree list)
13046 tree c, t;
13047 location_t loc = c_parser_peek_token (parser)->location;
13048 int modifiers = 0, nmodifiers = 0;
13050 matching_parens parens;
13051 if (!parens.require_open (parser))
13052 return list;
13054 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
13056 while (c_parser_next_token_is (parser, CPP_NAME))
13058 tree kind = c_parser_peek_token (parser)->value;
13059 const char *p = IDENTIFIER_POINTER (kind);
13060 if (strcmp ("simd", p) == 0)
13061 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
13062 else if (strcmp ("monotonic", p) == 0)
13063 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
13064 else if (strcmp ("nonmonotonic", p) == 0)
13065 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
13066 else
13067 break;
13068 c_parser_consume_token (parser);
13069 if (nmodifiers++ == 0
13070 && c_parser_next_token_is (parser, CPP_COMMA))
13071 c_parser_consume_token (parser);
13072 else
13074 c_parser_require (parser, CPP_COLON, "expected %<:%>");
13075 break;
13079 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
13080 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13081 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
13082 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13084 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
13085 "specified");
13086 modifiers = 0;
13089 if (c_parser_next_token_is (parser, CPP_NAME))
13091 tree kind = c_parser_peek_token (parser)->value;
13092 const char *p = IDENTIFIER_POINTER (kind);
13094 switch (p[0])
13096 case 'd':
13097 if (strcmp ("dynamic", p) != 0)
13098 goto invalid_kind;
13099 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
13100 break;
13102 case 'g':
13103 if (strcmp ("guided", p) != 0)
13104 goto invalid_kind;
13105 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
13106 break;
13108 case 'r':
13109 if (strcmp ("runtime", p) != 0)
13110 goto invalid_kind;
13111 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
13112 break;
13114 default:
13115 goto invalid_kind;
13118 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13119 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13120 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13121 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13122 else
13123 goto invalid_kind;
13125 c_parser_consume_token (parser);
13126 if (c_parser_next_token_is (parser, CPP_COMMA))
13128 location_t here;
13129 c_parser_consume_token (parser);
13131 here = c_parser_peek_token (parser)->location;
13132 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13133 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13134 t = expr.value;
13135 t = c_fully_fold (t, false, NULL);
13137 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13138 error_at (here, "schedule %<runtime%> does not take "
13139 "a %<chunk_size%> parameter");
13140 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13141 error_at (here,
13142 "schedule %<auto%> does not take "
13143 "a %<chunk_size%> parameter");
13144 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13146 /* Attempt to statically determine when the number isn't
13147 positive. */
13148 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13149 build_int_cst (TREE_TYPE (t), 0));
13150 protected_set_expr_location (s, loc);
13151 if (s == boolean_true_node)
13153 warning_at (loc, 0,
13154 "chunk size value must be positive");
13155 t = integer_one_node;
13157 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13159 else
13160 c_parser_error (parser, "expected integer expression");
13162 parens.skip_until_found_close (parser);
13164 else
13165 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13166 "expected %<,%> or %<)%>");
13168 OMP_CLAUSE_SCHEDULE_KIND (c)
13169 = (enum omp_clause_schedule_kind)
13170 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13172 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13173 OMP_CLAUSE_CHAIN (c) = list;
13174 return c;
13176 invalid_kind:
13177 c_parser_error (parser, "invalid schedule kind");
13178 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13179 return list;
13182 /* OpenMP 2.5:
13183 shared ( variable-list ) */
13185 static tree
13186 c_parser_omp_clause_shared (c_parser *parser, tree list)
13188 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13191 /* OpenMP 3.0:
13192 untied */
13194 static tree
13195 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13197 tree c;
13199 /* FIXME: Should we allow duplicates? */
13200 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13202 c = build_omp_clause (c_parser_peek_token (parser)->location,
13203 OMP_CLAUSE_UNTIED);
13204 OMP_CLAUSE_CHAIN (c) = list;
13206 return c;
13209 /* OpenMP 4.0:
13210 inbranch
13211 notinbranch */
13213 static tree
13214 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13215 enum omp_clause_code code, tree list)
13217 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13219 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13220 OMP_CLAUSE_CHAIN (c) = list;
13222 return c;
13225 /* OpenMP 4.0:
13226 parallel
13228 sections
13229 taskgroup */
13231 static tree
13232 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13233 enum omp_clause_code code, tree list)
13235 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13236 OMP_CLAUSE_CHAIN (c) = list;
13238 return c;
13241 /* OpenMP 4.5:
13242 nogroup */
13244 static tree
13245 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13247 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13248 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13249 OMP_CLAUSE_NOGROUP);
13250 OMP_CLAUSE_CHAIN (c) = list;
13251 return c;
13254 /* OpenMP 4.5:
13255 simd
13256 threads */
13258 static tree
13259 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13260 enum omp_clause_code code, tree list)
13262 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13263 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13264 OMP_CLAUSE_CHAIN (c) = list;
13265 return c;
13268 /* OpenMP 4.0:
13269 num_teams ( expression ) */
13271 static tree
13272 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13274 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13275 matching_parens parens;
13276 if (parens.require_open (parser))
13278 location_t expr_loc = c_parser_peek_token (parser)->location;
13279 c_expr expr = c_parser_expression (parser);
13280 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13281 tree c, t = expr.value;
13282 t = c_fully_fold (t, false, NULL);
13284 parens.skip_until_found_close (parser);
13286 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13288 c_parser_error (parser, "expected integer expression");
13289 return list;
13292 /* Attempt to statically determine when the number isn't positive. */
13293 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13294 build_int_cst (TREE_TYPE (t), 0));
13295 protected_set_expr_location (c, expr_loc);
13296 if (c == boolean_true_node)
13298 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13299 t = integer_one_node;
13302 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13304 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13305 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13306 OMP_CLAUSE_CHAIN (c) = list;
13307 list = c;
13310 return list;
13313 /* OpenMP 4.0:
13314 thread_limit ( expression ) */
13316 static tree
13317 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13319 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13320 matching_parens parens;
13321 if (parens.require_open (parser))
13323 location_t expr_loc = c_parser_peek_token (parser)->location;
13324 c_expr expr = c_parser_expression (parser);
13325 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13326 tree c, t = expr.value;
13327 t = c_fully_fold (t, false, NULL);
13329 parens.skip_until_found_close (parser);
13331 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13333 c_parser_error (parser, "expected integer expression");
13334 return list;
13337 /* Attempt to statically determine when the number isn't positive. */
13338 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13339 build_int_cst (TREE_TYPE (t), 0));
13340 protected_set_expr_location (c, expr_loc);
13341 if (c == boolean_true_node)
13343 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13344 t = integer_one_node;
13347 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13348 "thread_limit");
13350 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13351 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13352 OMP_CLAUSE_CHAIN (c) = list;
13353 list = c;
13356 return list;
13359 /* OpenMP 4.0:
13360 aligned ( variable-list )
13361 aligned ( variable-list : constant-expression ) */
13363 static tree
13364 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13366 location_t clause_loc = c_parser_peek_token (parser)->location;
13367 tree nl, c;
13369 matching_parens parens;
13370 if (!parens.require_open (parser))
13371 return list;
13373 nl = c_parser_omp_variable_list (parser, clause_loc,
13374 OMP_CLAUSE_ALIGNED, list);
13376 if (c_parser_next_token_is (parser, CPP_COLON))
13378 c_parser_consume_token (parser);
13379 location_t expr_loc = c_parser_peek_token (parser)->location;
13380 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13381 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13382 tree alignment = expr.value;
13383 alignment = c_fully_fold (alignment, false, NULL);
13384 if (TREE_CODE (alignment) != INTEGER_CST
13385 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13386 || tree_int_cst_sgn (alignment) != 1)
13388 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13389 "be positive constant integer expression");
13390 alignment = NULL_TREE;
13393 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13394 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13397 parens.skip_until_found_close (parser);
13398 return nl;
13401 /* OpenMP 4.0:
13402 linear ( variable-list )
13403 linear ( variable-list : expression )
13405 OpenMP 4.5:
13406 linear ( modifier ( variable-list ) )
13407 linear ( modifier ( variable-list ) : expression ) */
13409 static tree
13410 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
13412 location_t clause_loc = c_parser_peek_token (parser)->location;
13413 tree nl, c, step;
13414 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13416 matching_parens parens;
13417 if (!parens.require_open (parser))
13418 return list;
13420 if (!is_cilk_simd_fn
13421 && c_parser_next_token_is (parser, CPP_NAME))
13423 c_token *tok = c_parser_peek_token (parser);
13424 const char *p = IDENTIFIER_POINTER (tok->value);
13425 if (strcmp ("val", p) == 0)
13426 kind = OMP_CLAUSE_LINEAR_VAL;
13427 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13428 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13429 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13431 c_parser_consume_token (parser);
13432 c_parser_consume_token (parser);
13436 nl = c_parser_omp_variable_list (parser, clause_loc,
13437 OMP_CLAUSE_LINEAR, list);
13439 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13440 parens.skip_until_found_close (parser);
13442 if (c_parser_next_token_is (parser, CPP_COLON))
13444 c_parser_consume_token (parser);
13445 location_t expr_loc = c_parser_peek_token (parser)->location;
13446 c_expr expr = c_parser_expression (parser);
13447 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13448 step = expr.value;
13449 step = c_fully_fold (step, false, NULL);
13450 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
13452 sorry ("using parameters for %<linear%> step is not supported yet");
13453 step = integer_one_node;
13455 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13457 error_at (clause_loc, "%<linear%> clause step expression must "
13458 "be integral");
13459 step = integer_one_node;
13463 else
13464 step = integer_one_node;
13466 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13468 OMP_CLAUSE_LINEAR_STEP (c) = step;
13469 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13472 parens.skip_until_found_close (parser);
13473 return nl;
13476 /* OpenMP 4.0:
13477 safelen ( constant-expression ) */
13479 static tree
13480 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13482 location_t clause_loc = c_parser_peek_token (parser)->location;
13483 tree c, t;
13485 matching_parens parens;
13486 if (!parens.require_open (parser))
13487 return list;
13489 location_t expr_loc = c_parser_peek_token (parser)->location;
13490 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13491 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13492 t = expr.value;
13493 t = c_fully_fold (t, false, NULL);
13494 if (TREE_CODE (t) != INTEGER_CST
13495 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13496 || tree_int_cst_sgn (t) != 1)
13498 error_at (clause_loc, "%<safelen%> clause expression must "
13499 "be positive constant integer expression");
13500 t = NULL_TREE;
13503 parens.skip_until_found_close (parser);
13504 if (t == NULL_TREE || t == error_mark_node)
13505 return list;
13507 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13509 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13510 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13511 OMP_CLAUSE_CHAIN (c) = list;
13512 return c;
13515 /* OpenMP 4.0:
13516 simdlen ( constant-expression ) */
13518 static tree
13519 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13521 location_t clause_loc = c_parser_peek_token (parser)->location;
13522 tree c, t;
13524 matching_parens parens;
13525 if (!parens.require_open (parser))
13526 return list;
13528 location_t expr_loc = c_parser_peek_token (parser)->location;
13529 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13530 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13531 t = expr.value;
13532 t = c_fully_fold (t, false, NULL);
13533 if (TREE_CODE (t) != INTEGER_CST
13534 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13535 || tree_int_cst_sgn (t) != 1)
13537 error_at (clause_loc, "%<simdlen%> clause expression must "
13538 "be positive constant integer expression");
13539 t = NULL_TREE;
13542 parens.skip_until_found_close (parser);
13543 if (t == NULL_TREE || t == error_mark_node)
13544 return list;
13546 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13548 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13549 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13550 OMP_CLAUSE_CHAIN (c) = list;
13551 return c;
13554 /* OpenMP 4.5:
13555 vec:
13556 identifier [+/- integer]
13557 vec , identifier [+/- integer]
13560 static tree
13561 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13562 tree list)
13564 tree vec = NULL;
13565 if (c_parser_next_token_is_not (parser, CPP_NAME)
13566 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13568 c_parser_error (parser, "expected identifier");
13569 return list;
13572 while (c_parser_next_token_is (parser, CPP_NAME)
13573 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13575 tree t = lookup_name (c_parser_peek_token (parser)->value);
13576 tree addend = NULL;
13578 if (t == NULL_TREE)
13580 undeclared_variable (c_parser_peek_token (parser)->location,
13581 c_parser_peek_token (parser)->value);
13582 t = error_mark_node;
13585 c_parser_consume_token (parser);
13587 bool neg = false;
13588 if (c_parser_next_token_is (parser, CPP_MINUS))
13589 neg = true;
13590 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13592 addend = integer_zero_node;
13593 neg = false;
13594 goto add_to_vector;
13596 c_parser_consume_token (parser);
13598 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13600 c_parser_error (parser, "expected integer");
13601 return list;
13604 addend = c_parser_peek_token (parser)->value;
13605 if (TREE_CODE (addend) != INTEGER_CST)
13607 c_parser_error (parser, "expected integer");
13608 return list;
13610 c_parser_consume_token (parser);
13612 add_to_vector:
13613 if (t != error_mark_node)
13615 vec = tree_cons (addend, t, vec);
13616 if (neg)
13617 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13620 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13621 break;
13623 c_parser_consume_token (parser);
13626 if (vec == NULL_TREE)
13627 return list;
13629 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13630 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13631 OMP_CLAUSE_DECL (u) = nreverse (vec);
13632 OMP_CLAUSE_CHAIN (u) = list;
13633 return u;
13636 /* OpenMP 4.0:
13637 depend ( depend-kind: variable-list )
13639 depend-kind:
13640 in | out | inout
13642 OpenMP 4.5:
13643 depend ( source )
13645 depend ( sink : vec ) */
13647 static tree
13648 c_parser_omp_clause_depend (c_parser *parser, tree list)
13650 location_t clause_loc = c_parser_peek_token (parser)->location;
13651 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13652 tree nl, c;
13654 matching_parens parens;
13655 if (!parens.require_open (parser))
13656 return list;
13658 if (c_parser_next_token_is (parser, CPP_NAME))
13660 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13661 if (strcmp ("in", p) == 0)
13662 kind = OMP_CLAUSE_DEPEND_IN;
13663 else if (strcmp ("inout", p) == 0)
13664 kind = OMP_CLAUSE_DEPEND_INOUT;
13665 else if (strcmp ("out", p) == 0)
13666 kind = OMP_CLAUSE_DEPEND_OUT;
13667 else if (strcmp ("source", p) == 0)
13668 kind = OMP_CLAUSE_DEPEND_SOURCE;
13669 else if (strcmp ("sink", p) == 0)
13670 kind = OMP_CLAUSE_DEPEND_SINK;
13671 else
13672 goto invalid_kind;
13674 else
13675 goto invalid_kind;
13677 c_parser_consume_token (parser);
13679 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13681 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13682 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13683 OMP_CLAUSE_DECL (c) = NULL_TREE;
13684 OMP_CLAUSE_CHAIN (c) = list;
13685 parens.skip_until_found_close (parser);
13686 return c;
13689 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13690 goto resync_fail;
13692 if (kind == OMP_CLAUSE_DEPEND_SINK)
13693 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13694 else
13696 nl = c_parser_omp_variable_list (parser, clause_loc,
13697 OMP_CLAUSE_DEPEND, list);
13699 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13700 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13703 parens.skip_until_found_close (parser);
13704 return nl;
13706 invalid_kind:
13707 c_parser_error (parser, "invalid depend kind");
13708 resync_fail:
13709 parens.skip_until_found_close (parser);
13710 return list;
13713 /* OpenMP 4.0:
13714 map ( map-kind: variable-list )
13715 map ( variable-list )
13717 map-kind:
13718 alloc | to | from | tofrom
13720 OpenMP 4.5:
13721 map-kind:
13722 alloc | to | from | tofrom | release | delete
13724 map ( always [,] map-kind: variable-list ) */
13726 static tree
13727 c_parser_omp_clause_map (c_parser *parser, tree list)
13729 location_t clause_loc = c_parser_peek_token (parser)->location;
13730 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13731 int always = 0;
13732 enum c_id_kind always_id_kind = C_ID_NONE;
13733 location_t always_loc = UNKNOWN_LOCATION;
13734 tree always_id = NULL_TREE;
13735 tree nl, c;
13737 matching_parens parens;
13738 if (!parens.require_open (parser))
13739 return list;
13741 if (c_parser_next_token_is (parser, CPP_NAME))
13743 c_token *tok = c_parser_peek_token (parser);
13744 const char *p = IDENTIFIER_POINTER (tok->value);
13745 always_id_kind = tok->id_kind;
13746 always_loc = tok->location;
13747 always_id = tok->value;
13748 if (strcmp ("always", p) == 0)
13750 c_token *sectok = c_parser_peek_2nd_token (parser);
13751 if (sectok->type == CPP_COMMA)
13753 c_parser_consume_token (parser);
13754 c_parser_consume_token (parser);
13755 always = 2;
13757 else if (sectok->type == CPP_NAME)
13759 p = IDENTIFIER_POINTER (sectok->value);
13760 if (strcmp ("alloc", p) == 0
13761 || strcmp ("to", p) == 0
13762 || strcmp ("from", p) == 0
13763 || strcmp ("tofrom", p) == 0
13764 || strcmp ("release", p) == 0
13765 || strcmp ("delete", p) == 0)
13767 c_parser_consume_token (parser);
13768 always = 1;
13774 if (c_parser_next_token_is (parser, CPP_NAME)
13775 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13777 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13778 if (strcmp ("alloc", p) == 0)
13779 kind = GOMP_MAP_ALLOC;
13780 else if (strcmp ("to", p) == 0)
13781 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13782 else if (strcmp ("from", p) == 0)
13783 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13784 else if (strcmp ("tofrom", p) == 0)
13785 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13786 else if (strcmp ("release", p) == 0)
13787 kind = GOMP_MAP_RELEASE;
13788 else if (strcmp ("delete", p) == 0)
13789 kind = GOMP_MAP_DELETE;
13790 else
13792 c_parser_error (parser, "invalid map kind");
13793 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13794 "expected %<)%>");
13795 return list;
13797 c_parser_consume_token (parser);
13798 c_parser_consume_token (parser);
13800 else if (always)
13802 if (always_id_kind != C_ID_ID)
13804 c_parser_error (parser, "expected identifier");
13805 parens.skip_until_found_close (parser);
13806 return list;
13809 tree t = lookup_name (always_id);
13810 if (t == NULL_TREE)
13812 undeclared_variable (always_loc, always_id);
13813 t = error_mark_node;
13815 if (t != error_mark_node)
13817 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13818 OMP_CLAUSE_DECL (u) = t;
13819 OMP_CLAUSE_CHAIN (u) = list;
13820 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13821 list = u;
13823 if (always == 1)
13825 parens.skip_until_found_close (parser);
13826 return list;
13830 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13832 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13833 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13835 parens.skip_until_found_close (parser);
13836 return nl;
13839 /* OpenMP 4.0:
13840 device ( expression ) */
13842 static tree
13843 c_parser_omp_clause_device (c_parser *parser, tree list)
13845 location_t clause_loc = c_parser_peek_token (parser)->location;
13846 matching_parens parens;
13847 if (parens.require_open (parser))
13849 location_t expr_loc = c_parser_peek_token (parser)->location;
13850 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13851 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13852 tree c, t = expr.value;
13853 t = c_fully_fold (t, false, NULL);
13855 parens.skip_until_found_close (parser);
13857 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13859 c_parser_error (parser, "expected integer expression");
13860 return list;
13863 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13865 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13866 OMP_CLAUSE_DEVICE_ID (c) = t;
13867 OMP_CLAUSE_CHAIN (c) = list;
13868 list = c;
13871 return list;
13874 /* OpenMP 4.0:
13875 dist_schedule ( static )
13876 dist_schedule ( static , expression ) */
13878 static tree
13879 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13881 tree c, t = NULL_TREE;
13882 location_t loc = c_parser_peek_token (parser)->location;
13884 matching_parens parens;
13885 if (!parens.require_open (parser))
13886 return list;
13888 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13890 c_parser_error (parser, "invalid dist_schedule kind");
13891 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13892 "expected %<)%>");
13893 return list;
13896 c_parser_consume_token (parser);
13897 if (c_parser_next_token_is (parser, CPP_COMMA))
13899 c_parser_consume_token (parser);
13901 location_t expr_loc = c_parser_peek_token (parser)->location;
13902 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13903 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13904 t = expr.value;
13905 t = c_fully_fold (t, false, NULL);
13906 parens.skip_until_found_close (parser);
13908 else
13909 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13910 "expected %<,%> or %<)%>");
13912 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13913 if (t == error_mark_node)
13914 return list;
13916 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13917 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13918 OMP_CLAUSE_CHAIN (c) = list;
13919 return c;
13922 /* OpenMP 4.0:
13923 proc_bind ( proc-bind-kind )
13925 proc-bind-kind:
13926 master | close | spread */
13928 static tree
13929 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13931 location_t clause_loc = c_parser_peek_token (parser)->location;
13932 enum omp_clause_proc_bind_kind kind;
13933 tree c;
13935 matching_parens parens;
13936 if (!parens.require_open (parser))
13937 return list;
13939 if (c_parser_next_token_is (parser, CPP_NAME))
13941 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13942 if (strcmp ("master", p) == 0)
13943 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13944 else if (strcmp ("close", p) == 0)
13945 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13946 else if (strcmp ("spread", p) == 0)
13947 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13948 else
13949 goto invalid_kind;
13951 else
13952 goto invalid_kind;
13954 c_parser_consume_token (parser);
13955 parens.skip_until_found_close (parser);
13956 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13957 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13958 OMP_CLAUSE_CHAIN (c) = list;
13959 return c;
13961 invalid_kind:
13962 c_parser_error (parser, "invalid proc_bind kind");
13963 parens.skip_until_found_close (parser);
13964 return list;
13967 /* OpenMP 4.0:
13968 to ( variable-list ) */
13970 static tree
13971 c_parser_omp_clause_to (c_parser *parser, tree list)
13973 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13976 /* OpenMP 4.0:
13977 from ( variable-list ) */
13979 static tree
13980 c_parser_omp_clause_from (c_parser *parser, tree list)
13982 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13985 /* OpenMP 4.0:
13986 uniform ( variable-list ) */
13988 static tree
13989 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13991 /* The clauses location. */
13992 location_t loc = c_parser_peek_token (parser)->location;
13994 matching_parens parens;
13995 if (parens.require_open (parser))
13997 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13998 list);
13999 parens.skip_until_found_close (parser);
14001 return list;
14004 /* Parse all OpenACC clauses. The set clauses allowed by the directive
14005 is a bitmask in MASK. Return the list of clauses found. */
14007 static tree
14008 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
14009 const char *where, bool finish_p = true)
14011 tree clauses = NULL;
14012 bool first = true;
14014 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14016 location_t here;
14017 pragma_omp_clause c_kind;
14018 const char *c_name;
14019 tree prev = clauses;
14021 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14022 c_parser_consume_token (parser);
14024 here = c_parser_peek_token (parser)->location;
14025 c_kind = c_parser_omp_clause_name (parser);
14027 switch (c_kind)
14029 case PRAGMA_OACC_CLAUSE_ASYNC:
14030 clauses = c_parser_oacc_clause_async (parser, clauses);
14031 c_name = "async";
14032 break;
14033 case PRAGMA_OACC_CLAUSE_AUTO:
14034 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
14035 clauses);
14036 c_name = "auto";
14037 break;
14038 case PRAGMA_OACC_CLAUSE_COLLAPSE:
14039 clauses = c_parser_omp_clause_collapse (parser, clauses);
14040 c_name = "collapse";
14041 break;
14042 case PRAGMA_OACC_CLAUSE_COPY:
14043 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14044 c_name = "copy";
14045 break;
14046 case PRAGMA_OACC_CLAUSE_COPYIN:
14047 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14048 c_name = "copyin";
14049 break;
14050 case PRAGMA_OACC_CLAUSE_COPYOUT:
14051 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14052 c_name = "copyout";
14053 break;
14054 case PRAGMA_OACC_CLAUSE_CREATE:
14055 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14056 c_name = "create";
14057 break;
14058 case PRAGMA_OACC_CLAUSE_DELETE:
14059 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14060 c_name = "delete";
14061 break;
14062 case PRAGMA_OMP_CLAUSE_DEFAULT:
14063 clauses = c_parser_omp_clause_default (parser, clauses, true);
14064 c_name = "default";
14065 break;
14066 case PRAGMA_OACC_CLAUSE_DEVICE:
14067 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14068 c_name = "device";
14069 break;
14070 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
14071 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
14072 c_name = "deviceptr";
14073 break;
14074 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
14075 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14076 c_name = "device_resident";
14077 break;
14078 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
14079 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14080 c_name = "firstprivate";
14081 break;
14082 case PRAGMA_OACC_CLAUSE_GANG:
14083 c_name = "gang";
14084 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
14085 c_name, clauses);
14086 break;
14087 case PRAGMA_OACC_CLAUSE_HOST:
14088 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14089 c_name = "host";
14090 break;
14091 case PRAGMA_OACC_CLAUSE_IF:
14092 clauses = c_parser_omp_clause_if (parser, clauses, false);
14093 c_name = "if";
14094 break;
14095 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
14096 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
14097 clauses);
14098 c_name = "independent";
14099 break;
14100 case PRAGMA_OACC_CLAUSE_LINK:
14101 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14102 c_name = "link";
14103 break;
14104 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
14105 clauses = c_parser_oacc_single_int_clause (parser,
14106 OMP_CLAUSE_NUM_GANGS,
14107 clauses);
14108 c_name = "num_gangs";
14109 break;
14110 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
14111 clauses = c_parser_oacc_single_int_clause (parser,
14112 OMP_CLAUSE_NUM_WORKERS,
14113 clauses);
14114 c_name = "num_workers";
14115 break;
14116 case PRAGMA_OACC_CLAUSE_PRESENT:
14117 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14118 c_name = "present";
14119 break;
14120 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
14121 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14122 c_name = "present_or_copy";
14123 break;
14124 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
14125 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14126 c_name = "present_or_copyin";
14127 break;
14128 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
14129 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14130 c_name = "present_or_copyout";
14131 break;
14132 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
14133 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14134 c_name = "present_or_create";
14135 break;
14136 case PRAGMA_OACC_CLAUSE_PRIVATE:
14137 clauses = c_parser_omp_clause_private (parser, clauses);
14138 c_name = "private";
14139 break;
14140 case PRAGMA_OACC_CLAUSE_REDUCTION:
14141 clauses = c_parser_omp_clause_reduction (parser, clauses);
14142 c_name = "reduction";
14143 break;
14144 case PRAGMA_OACC_CLAUSE_SELF:
14145 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14146 c_name = "self";
14147 break;
14148 case PRAGMA_OACC_CLAUSE_SEQ:
14149 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14150 clauses);
14151 c_name = "seq";
14152 break;
14153 case PRAGMA_OACC_CLAUSE_TILE:
14154 clauses = c_parser_oacc_clause_tile (parser, clauses);
14155 c_name = "tile";
14156 break;
14157 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14158 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14159 c_name = "use_device";
14160 break;
14161 case PRAGMA_OACC_CLAUSE_VECTOR:
14162 c_name = "vector";
14163 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14164 c_name, clauses);
14165 break;
14166 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14167 clauses = c_parser_oacc_single_int_clause (parser,
14168 OMP_CLAUSE_VECTOR_LENGTH,
14169 clauses);
14170 c_name = "vector_length";
14171 break;
14172 case PRAGMA_OACC_CLAUSE_WAIT:
14173 clauses = c_parser_oacc_clause_wait (parser, clauses);
14174 c_name = "wait";
14175 break;
14176 case PRAGMA_OACC_CLAUSE_WORKER:
14177 c_name = "worker";
14178 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14179 c_name, clauses);
14180 break;
14181 default:
14182 c_parser_error (parser, "expected %<#pragma acc%> clause");
14183 goto saw_error;
14186 first = false;
14188 if (((mask >> c_kind) & 1) == 0)
14190 /* Remove the invalid clause(s) from the list to avoid
14191 confusing the rest of the compiler. */
14192 clauses = prev;
14193 error_at (here, "%qs is not valid for %qs", c_name, where);
14197 saw_error:
14198 c_parser_skip_to_pragma_eol (parser);
14200 if (finish_p)
14201 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14203 return clauses;
14206 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14207 is a bitmask in MASK. Return the list of clauses found. */
14209 static tree
14210 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14211 const char *where, bool finish_p = true)
14213 tree clauses = NULL;
14214 bool first = true, cilk_simd_fn = false;
14216 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14218 location_t here;
14219 pragma_omp_clause c_kind;
14220 const char *c_name;
14221 tree prev = clauses;
14223 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14224 c_parser_consume_token (parser);
14226 here = c_parser_peek_token (parser)->location;
14227 c_kind = c_parser_omp_clause_name (parser);
14229 switch (c_kind)
14231 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14232 clauses = c_parser_omp_clause_collapse (parser, clauses);
14233 c_name = "collapse";
14234 break;
14235 case PRAGMA_OMP_CLAUSE_COPYIN:
14236 clauses = c_parser_omp_clause_copyin (parser, clauses);
14237 c_name = "copyin";
14238 break;
14239 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14240 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14241 c_name = "copyprivate";
14242 break;
14243 case PRAGMA_OMP_CLAUSE_DEFAULT:
14244 clauses = c_parser_omp_clause_default (parser, clauses, false);
14245 c_name = "default";
14246 break;
14247 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14248 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14249 c_name = "firstprivate";
14250 break;
14251 case PRAGMA_OMP_CLAUSE_FINAL:
14252 clauses = c_parser_omp_clause_final (parser, clauses);
14253 c_name = "final";
14254 break;
14255 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14256 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14257 c_name = "grainsize";
14258 break;
14259 case PRAGMA_OMP_CLAUSE_HINT:
14260 clauses = c_parser_omp_clause_hint (parser, clauses);
14261 c_name = "hint";
14262 break;
14263 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14264 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14265 c_name = "defaultmap";
14266 break;
14267 case PRAGMA_OMP_CLAUSE_IF:
14268 clauses = c_parser_omp_clause_if (parser, clauses, true);
14269 c_name = "if";
14270 break;
14271 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14272 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14273 c_name = "lastprivate";
14274 break;
14275 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14276 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14277 c_name = "mergeable";
14278 break;
14279 case PRAGMA_OMP_CLAUSE_NOWAIT:
14280 clauses = c_parser_omp_clause_nowait (parser, clauses);
14281 c_name = "nowait";
14282 break;
14283 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14284 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14285 c_name = "num_tasks";
14286 break;
14287 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14288 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14289 c_name = "num_threads";
14290 break;
14291 case PRAGMA_OMP_CLAUSE_ORDERED:
14292 clauses = c_parser_omp_clause_ordered (parser, clauses);
14293 c_name = "ordered";
14294 break;
14295 case PRAGMA_OMP_CLAUSE_PRIORITY:
14296 clauses = c_parser_omp_clause_priority (parser, clauses);
14297 c_name = "priority";
14298 break;
14299 case PRAGMA_OMP_CLAUSE_PRIVATE:
14300 clauses = c_parser_omp_clause_private (parser, clauses);
14301 c_name = "private";
14302 break;
14303 case PRAGMA_OMP_CLAUSE_REDUCTION:
14304 clauses = c_parser_omp_clause_reduction (parser, clauses);
14305 c_name = "reduction";
14306 break;
14307 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14308 clauses = c_parser_omp_clause_schedule (parser, clauses);
14309 c_name = "schedule";
14310 break;
14311 case PRAGMA_OMP_CLAUSE_SHARED:
14312 clauses = c_parser_omp_clause_shared (parser, clauses);
14313 c_name = "shared";
14314 break;
14315 case PRAGMA_OMP_CLAUSE_UNTIED:
14316 clauses = c_parser_omp_clause_untied (parser, clauses);
14317 c_name = "untied";
14318 break;
14319 case PRAGMA_OMP_CLAUSE_INBRANCH:
14320 case PRAGMA_CILK_CLAUSE_MASK:
14321 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14322 clauses);
14323 c_name = "inbranch";
14324 break;
14325 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14326 case PRAGMA_CILK_CLAUSE_NOMASK:
14327 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14328 clauses);
14329 c_name = "notinbranch";
14330 break;
14331 case PRAGMA_OMP_CLAUSE_PARALLEL:
14332 clauses
14333 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14334 clauses);
14335 c_name = "parallel";
14336 if (!first)
14338 clause_not_first:
14339 error_at (here, "%qs must be the first clause of %qs",
14340 c_name, where);
14341 clauses = prev;
14343 break;
14344 case PRAGMA_OMP_CLAUSE_FOR:
14345 clauses
14346 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14347 clauses);
14348 c_name = "for";
14349 if (!first)
14350 goto clause_not_first;
14351 break;
14352 case PRAGMA_OMP_CLAUSE_SECTIONS:
14353 clauses
14354 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14355 clauses);
14356 c_name = "sections";
14357 if (!first)
14358 goto clause_not_first;
14359 break;
14360 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14361 clauses
14362 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14363 clauses);
14364 c_name = "taskgroup";
14365 if (!first)
14366 goto clause_not_first;
14367 break;
14368 case PRAGMA_OMP_CLAUSE_LINK:
14369 clauses
14370 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14371 c_name = "link";
14372 break;
14373 case PRAGMA_OMP_CLAUSE_TO:
14374 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14375 clauses
14376 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14377 clauses);
14378 else
14379 clauses = c_parser_omp_clause_to (parser, clauses);
14380 c_name = "to";
14381 break;
14382 case PRAGMA_OMP_CLAUSE_FROM:
14383 clauses = c_parser_omp_clause_from (parser, clauses);
14384 c_name = "from";
14385 break;
14386 case PRAGMA_OMP_CLAUSE_UNIFORM:
14387 clauses = c_parser_omp_clause_uniform (parser, clauses);
14388 c_name = "uniform";
14389 break;
14390 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14391 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14392 c_name = "num_teams";
14393 break;
14394 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14395 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14396 c_name = "thread_limit";
14397 break;
14398 case PRAGMA_OMP_CLAUSE_ALIGNED:
14399 clauses = c_parser_omp_clause_aligned (parser, clauses);
14400 c_name = "aligned";
14401 break;
14402 case PRAGMA_OMP_CLAUSE_LINEAR:
14403 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
14404 cilk_simd_fn = true;
14405 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
14406 c_name = "linear";
14407 break;
14408 case PRAGMA_OMP_CLAUSE_DEPEND:
14409 clauses = c_parser_omp_clause_depend (parser, clauses);
14410 c_name = "depend";
14411 break;
14412 case PRAGMA_OMP_CLAUSE_MAP:
14413 clauses = c_parser_omp_clause_map (parser, clauses);
14414 c_name = "map";
14415 break;
14416 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14417 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14418 c_name = "use_device_ptr";
14419 break;
14420 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14421 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14422 c_name = "is_device_ptr";
14423 break;
14424 case PRAGMA_OMP_CLAUSE_DEVICE:
14425 clauses = c_parser_omp_clause_device (parser, clauses);
14426 c_name = "device";
14427 break;
14428 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14429 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14430 c_name = "dist_schedule";
14431 break;
14432 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14433 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14434 c_name = "proc_bind";
14435 break;
14436 case PRAGMA_OMP_CLAUSE_SAFELEN:
14437 clauses = c_parser_omp_clause_safelen (parser, clauses);
14438 c_name = "safelen";
14439 break;
14440 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
14441 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
14442 c_name = "simdlen";
14443 break;
14444 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14445 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14446 c_name = "simdlen";
14447 break;
14448 case PRAGMA_OMP_CLAUSE_NOGROUP:
14449 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14450 c_name = "nogroup";
14451 break;
14452 case PRAGMA_OMP_CLAUSE_THREADS:
14453 clauses
14454 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14455 clauses);
14456 c_name = "threads";
14457 break;
14458 case PRAGMA_OMP_CLAUSE_SIMD:
14459 clauses
14460 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14461 clauses);
14462 c_name = "simd";
14463 break;
14464 default:
14465 c_parser_error (parser, "expected %<#pragma omp%> clause");
14466 goto saw_error;
14469 first = false;
14471 if (((mask >> c_kind) & 1) == 0)
14473 /* Remove the invalid clause(s) from the list to avoid
14474 confusing the rest of the compiler. */
14475 clauses = prev;
14476 error_at (here, "%qs is not valid for %qs", c_name, where);
14480 saw_error:
14481 c_parser_skip_to_pragma_eol (parser);
14483 if (finish_p)
14485 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14486 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14487 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14490 return clauses;
14493 /* OpenACC 2.0, OpenMP 2.5:
14494 structured-block:
14495 statement
14497 In practice, we're also interested in adding the statement to an
14498 outer node. So it is convenient if we work around the fact that
14499 c_parser_statement calls add_stmt. */
14501 static tree
14502 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14504 tree stmt = push_stmt_list ();
14505 c_parser_statement (parser, if_p);
14506 return pop_stmt_list (stmt);
14509 /* OpenACC 2.0:
14510 # pragma acc cache (variable-list) new-line
14512 LOC is the location of the #pragma token.
14515 static tree
14516 c_parser_oacc_cache (location_t loc, c_parser *parser)
14518 tree stmt, clauses;
14520 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14521 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14523 c_parser_skip_to_pragma_eol (parser);
14525 stmt = make_node (OACC_CACHE);
14526 TREE_TYPE (stmt) = void_type_node;
14527 OACC_CACHE_CLAUSES (stmt) = clauses;
14528 SET_EXPR_LOCATION (stmt, loc);
14529 add_stmt (stmt);
14531 return stmt;
14534 /* OpenACC 2.0:
14535 # pragma acc data oacc-data-clause[optseq] new-line
14536 structured-block
14538 LOC is the location of the #pragma token.
14541 #define OACC_DATA_CLAUSE_MASK \
14542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14554 static tree
14555 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14557 tree stmt, clauses, block;
14559 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14560 "#pragma acc data");
14562 block = c_begin_omp_parallel ();
14563 add_stmt (c_parser_omp_structured_block (parser, if_p));
14565 stmt = c_finish_oacc_data (loc, clauses, block);
14567 return stmt;
14570 /* OpenACC 2.0:
14571 # pragma acc declare oacc-data-clause[optseq] new-line
14574 #define OACC_DECLARE_CLAUSE_MASK \
14575 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
14588 static void
14589 c_parser_oacc_declare (c_parser *parser)
14591 location_t pragma_loc = c_parser_peek_token (parser)->location;
14592 tree clauses, stmt, t, decl;
14594 bool error = false;
14596 c_parser_consume_pragma (parser);
14598 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14599 "#pragma acc declare");
14600 if (!clauses)
14602 error_at (pragma_loc,
14603 "no valid clauses specified in %<#pragma acc declare%>");
14604 return;
14607 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14609 location_t loc = OMP_CLAUSE_LOCATION (t);
14610 decl = OMP_CLAUSE_DECL (t);
14611 if (!DECL_P (decl))
14613 error_at (loc, "array section in %<#pragma acc declare%>");
14614 error = true;
14615 continue;
14618 switch (OMP_CLAUSE_MAP_KIND (t))
14620 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14621 case GOMP_MAP_FORCE_ALLOC:
14622 case GOMP_MAP_FORCE_TO:
14623 case GOMP_MAP_FORCE_DEVICEPTR:
14624 case GOMP_MAP_DEVICE_RESIDENT:
14625 break;
14627 case GOMP_MAP_LINK:
14628 if (!global_bindings_p ()
14629 && (TREE_STATIC (decl)
14630 || !DECL_EXTERNAL (decl)))
14632 error_at (loc,
14633 "%qD must be a global variable in "
14634 "%<#pragma acc declare link%>",
14635 decl);
14636 error = true;
14637 continue;
14639 break;
14641 default:
14642 if (global_bindings_p ())
14644 error_at (loc, "invalid OpenACC clause at file scope");
14645 error = true;
14646 continue;
14648 if (DECL_EXTERNAL (decl))
14650 error_at (loc,
14651 "invalid use of %<extern%> variable %qD "
14652 "in %<#pragma acc declare%>", decl);
14653 error = true;
14654 continue;
14656 else if (TREE_PUBLIC (decl))
14658 error_at (loc,
14659 "invalid use of %<global%> variable %qD "
14660 "in %<#pragma acc declare%>", decl);
14661 error = true;
14662 continue;
14664 break;
14667 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14668 || lookup_attribute ("omp declare target link",
14669 DECL_ATTRIBUTES (decl)))
14671 error_at (loc, "variable %qD used more than once with "
14672 "%<#pragma acc declare%>", decl);
14673 error = true;
14674 continue;
14677 if (!error)
14679 tree id;
14681 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14682 id = get_identifier ("omp declare target link");
14683 else
14684 id = get_identifier ("omp declare target");
14686 DECL_ATTRIBUTES (decl)
14687 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14689 if (global_bindings_p ())
14691 symtab_node *node = symtab_node::get (decl);
14692 if (node != NULL)
14694 node->offloadable = 1;
14695 if (ENABLE_OFFLOADING)
14697 g->have_offload = true;
14698 if (is_a <varpool_node *> (node))
14699 vec_safe_push (offload_vars, decl);
14706 if (error || global_bindings_p ())
14707 return;
14709 stmt = make_node (OACC_DECLARE);
14710 TREE_TYPE (stmt) = void_type_node;
14711 OACC_DECLARE_CLAUSES (stmt) = clauses;
14712 SET_EXPR_LOCATION (stmt, pragma_loc);
14714 add_stmt (stmt);
14716 return;
14719 /* OpenACC 2.0:
14720 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14724 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14727 LOC is the location of the #pragma token.
14730 #define OACC_ENTER_DATA_CLAUSE_MASK \
14731 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14739 #define OACC_EXIT_DATA_CLAUSE_MASK \
14740 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14746 static void
14747 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14749 location_t loc = c_parser_peek_token (parser)->location;
14750 tree clauses, stmt;
14751 const char *p = "";
14753 c_parser_consume_pragma (parser);
14755 if (c_parser_next_token_is (parser, CPP_NAME))
14757 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14758 c_parser_consume_token (parser);
14761 if (strcmp (p, "data") != 0)
14763 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14764 enter ? "enter" : "exit");
14765 parser->error = true;
14766 c_parser_skip_to_pragma_eol (parser);
14767 return;
14770 if (enter)
14771 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14772 "#pragma acc enter data");
14773 else
14774 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14775 "#pragma acc exit data");
14777 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14779 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14780 enter ? "enter" : "exit");
14781 return;
14784 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14785 TREE_TYPE (stmt) = void_type_node;
14786 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14787 SET_EXPR_LOCATION (stmt, loc);
14788 add_stmt (stmt);
14792 /* OpenACC 2.0:
14793 # pragma acc host_data oacc-data-clause[optseq] new-line
14794 structured-block
14797 #define OACC_HOST_DATA_CLAUSE_MASK \
14798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14800 static tree
14801 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14803 tree stmt, clauses, block;
14805 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14806 "#pragma acc host_data");
14808 block = c_begin_omp_parallel ();
14809 add_stmt (c_parser_omp_structured_block (parser, if_p));
14810 stmt = c_finish_oacc_host_data (loc, clauses, block);
14811 return stmt;
14815 /* OpenACC 2.0:
14817 # pragma acc loop oacc-loop-clause[optseq] new-line
14818 structured-block
14820 LOC is the location of the #pragma token.
14823 #define OACC_LOOP_CLAUSE_MASK \
14824 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14834 static tree
14835 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14836 omp_clause_mask mask, tree *cclauses, bool *if_p)
14838 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14840 strcat (p_name, " loop");
14841 mask |= OACC_LOOP_CLAUSE_MASK;
14843 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14844 cclauses == NULL);
14845 if (cclauses)
14847 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14848 if (*cclauses)
14849 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14850 if (clauses)
14851 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14854 tree block = c_begin_compound_stmt (true);
14855 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14856 if_p);
14857 block = c_end_compound_stmt (loc, block, true);
14858 add_stmt (block);
14860 return stmt;
14863 /* OpenACC 2.0:
14864 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14865 structured-block
14869 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14870 structured-block
14872 LOC is the location of the #pragma token.
14875 #define OACC_KERNELS_CLAUSE_MASK \
14876 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14894 #define OACC_PARALLEL_CLAUSE_MASK \
14895 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14916 static tree
14917 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14918 enum pragma_kind p_kind, char *p_name,
14919 bool *if_p)
14921 omp_clause_mask mask;
14922 enum tree_code code;
14923 switch (p_kind)
14925 case PRAGMA_OACC_KERNELS:
14926 strcat (p_name, " kernels");
14927 mask = OACC_KERNELS_CLAUSE_MASK;
14928 code = OACC_KERNELS;
14929 break;
14930 case PRAGMA_OACC_PARALLEL:
14931 strcat (p_name, " parallel");
14932 mask = OACC_PARALLEL_CLAUSE_MASK;
14933 code = OACC_PARALLEL;
14934 break;
14935 default:
14936 gcc_unreachable ();
14939 if (c_parser_next_token_is (parser, CPP_NAME))
14941 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14942 if (strcmp (p, "loop") == 0)
14944 c_parser_consume_token (parser);
14945 tree block = c_begin_omp_parallel ();
14946 tree clauses;
14947 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14948 return c_finish_omp_construct (loc, code, block, clauses);
14952 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14954 tree block = c_begin_omp_parallel ();
14955 add_stmt (c_parser_omp_structured_block (parser, if_p));
14957 return c_finish_omp_construct (loc, code, block, clauses);
14960 /* OpenACC 2.0:
14961 # pragma acc routine oacc-routine-clause[optseq] new-line
14962 function-definition
14964 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14967 #define OACC_ROUTINE_CLAUSE_MASK \
14968 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14973 /* Parse an OpenACC routine directive. For named directives, we apply
14974 immediately to the named function. For unnamed ones we then parse
14975 a declaration or definition, which must be for a function. */
14977 static void
14978 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14980 gcc_checking_assert (context == pragma_external);
14982 oacc_routine_data data;
14983 data.error_seen = false;
14984 data.fndecl_seen = false;
14985 data.clauses = NULL_TREE;
14986 data.loc = c_parser_peek_token (parser)->location;
14988 c_parser_consume_pragma (parser);
14990 /* Look for optional '( name )'. */
14991 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14993 c_parser_consume_token (parser); /* '(' */
14995 tree decl = NULL_TREE;
14996 c_token *name_token = c_parser_peek_token (parser);
14997 location_t name_loc = name_token->location;
14998 if (name_token->type == CPP_NAME
14999 && (name_token->id_kind == C_ID_ID
15000 || name_token->id_kind == C_ID_TYPENAME))
15002 decl = lookup_name (name_token->value);
15003 if (!decl)
15004 error_at (name_loc,
15005 "%qE has not been declared", name_token->value);
15006 c_parser_consume_token (parser);
15008 else
15009 c_parser_error (parser, "expected function name");
15011 if (!decl
15012 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15014 c_parser_skip_to_pragma_eol (parser, false);
15015 return;
15018 data.clauses
15019 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15020 "#pragma acc routine");
15022 if (TREE_CODE (decl) != FUNCTION_DECL)
15024 error_at (name_loc, "%qD does not refer to a function", decl);
15025 return;
15028 c_finish_oacc_routine (&data, decl, false);
15030 else /* No optional '( name )'. */
15032 data.clauses
15033 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15034 "#pragma acc routine");
15036 /* Emit a helpful diagnostic if there's another pragma following this
15037 one. Also don't allow a static assertion declaration, as in the
15038 following we'll just parse a *single* "declaration or function
15039 definition", and the static assertion counts an one. */
15040 if (c_parser_next_token_is (parser, CPP_PRAGMA)
15041 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
15043 error_at (data.loc,
15044 "%<#pragma acc routine%> not immediately followed by"
15045 " function declaration or definition");
15046 /* ..., and then just keep going. */
15047 return;
15050 /* We only have to consider the pragma_external case here. */
15051 if (c_parser_next_token_is (parser, CPP_KEYWORD)
15052 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
15054 int ext = disable_extension_diagnostics ();
15056 c_parser_consume_token (parser);
15057 while (c_parser_next_token_is (parser, CPP_KEYWORD)
15058 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
15059 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15060 NULL, vNULL, &data);
15061 restore_extension_diagnostics (ext);
15063 else
15064 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15065 NULL, vNULL, &data);
15069 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
15070 IS_DEFN is true if we're applying it to the definition. */
15072 static void
15073 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
15074 bool is_defn)
15076 /* Keep going if we're in error reporting mode. */
15077 if (data->error_seen
15078 || fndecl == error_mark_node)
15079 return;
15081 if (data->fndecl_seen)
15083 error_at (data->loc,
15084 "%<#pragma acc routine%> not immediately followed by"
15085 " a single function declaration or definition");
15086 data->error_seen = true;
15087 return;
15089 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
15091 error_at (data->loc,
15092 "%<#pragma acc routine%> not immediately followed by"
15093 " function declaration or definition");
15094 data->error_seen = true;
15095 return;
15098 if (oacc_get_fn_attrib (fndecl))
15100 error_at (data->loc,
15101 "%<#pragma acc routine%> already applied to %qD", fndecl);
15102 data->error_seen = true;
15103 return;
15106 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
15108 error_at (data->loc,
15109 TREE_USED (fndecl)
15110 ? G_("%<#pragma acc routine%> must be applied before use")
15111 : G_("%<#pragma acc routine%> must be applied before "
15112 "definition"));
15113 data->error_seen = true;
15114 return;
15117 /* Process the routine's dimension clauses. */
15118 tree dims = oacc_build_routine_dims (data->clauses);
15119 oacc_replace_fn_attrib (fndecl, dims);
15121 /* Add an "omp declare target" attribute. */
15122 DECL_ATTRIBUTES (fndecl)
15123 = tree_cons (get_identifier ("omp declare target"),
15124 NULL_TREE, DECL_ATTRIBUTES (fndecl));
15126 /* Remember that we've used this "#pragma acc routine". */
15127 data->fndecl_seen = true;
15130 /* OpenACC 2.0:
15131 # pragma acc update oacc-update-clause[optseq] new-line
15134 #define OACC_UPDATE_CLAUSE_MASK \
15135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
15137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
15138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
15140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15142 static void
15143 c_parser_oacc_update (c_parser *parser)
15145 location_t loc = c_parser_peek_token (parser)->location;
15147 c_parser_consume_pragma (parser);
15149 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15150 "#pragma acc update");
15151 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15153 error_at (loc,
15154 "%<#pragma acc update%> must contain at least one "
15155 "%<device%> or %<host%> or %<self%> clause");
15156 return;
15159 if (parser->error)
15160 return;
15162 tree stmt = make_node (OACC_UPDATE);
15163 TREE_TYPE (stmt) = void_type_node;
15164 OACC_UPDATE_CLAUSES (stmt) = clauses;
15165 SET_EXPR_LOCATION (stmt, loc);
15166 add_stmt (stmt);
15169 /* OpenACC 2.0:
15170 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15172 LOC is the location of the #pragma token.
15175 #define OACC_WAIT_CLAUSE_MASK \
15176 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15178 static tree
15179 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15181 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15183 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15184 list = c_parser_oacc_wait_list (parser, loc, list);
15186 strcpy (p_name, " wait");
15187 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15188 stmt = c_finish_oacc_wait (loc, list, clauses);
15189 add_stmt (stmt);
15191 return stmt;
15194 /* OpenMP 2.5:
15195 # pragma omp atomic new-line
15196 expression-stmt
15198 expression-stmt:
15199 x binop= expr | x++ | ++x | x-- | --x
15200 binop:
15201 +, *, -, /, &, ^, |, <<, >>
15203 where x is an lvalue expression with scalar type.
15205 OpenMP 3.1:
15206 # pragma omp atomic new-line
15207 update-stmt
15209 # pragma omp atomic read new-line
15210 read-stmt
15212 # pragma omp atomic write new-line
15213 write-stmt
15215 # pragma omp atomic update new-line
15216 update-stmt
15218 # pragma omp atomic capture new-line
15219 capture-stmt
15221 # pragma omp atomic capture new-line
15222 capture-block
15224 read-stmt:
15225 v = x
15226 write-stmt:
15227 x = expr
15228 update-stmt:
15229 expression-stmt | x = x binop expr
15230 capture-stmt:
15231 v = expression-stmt
15232 capture-block:
15233 { v = x; update-stmt; } | { update-stmt; v = x; }
15235 OpenMP 4.0:
15236 update-stmt:
15237 expression-stmt | x = x binop expr | x = expr binop x
15238 capture-stmt:
15239 v = update-stmt
15240 capture-block:
15241 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15243 where x and v are lvalue expressions with scalar type.
15245 LOC is the location of the #pragma token. */
15247 static void
15248 c_parser_omp_atomic (location_t loc, c_parser *parser)
15250 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15251 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15252 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15253 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15254 struct c_expr expr;
15255 location_t eloc;
15256 bool structured_block = false;
15257 bool swapped = false;
15258 bool seq_cst = false;
15259 bool non_lvalue_p;
15261 if (c_parser_next_token_is (parser, CPP_NAME))
15263 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15264 if (!strcmp (p, "seq_cst"))
15266 seq_cst = true;
15267 c_parser_consume_token (parser);
15268 if (c_parser_next_token_is (parser, CPP_COMMA)
15269 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15270 c_parser_consume_token (parser);
15273 if (c_parser_next_token_is (parser, CPP_NAME))
15275 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15277 if (!strcmp (p, "read"))
15278 code = OMP_ATOMIC_READ;
15279 else if (!strcmp (p, "write"))
15280 code = NOP_EXPR;
15281 else if (!strcmp (p, "update"))
15282 code = OMP_ATOMIC;
15283 else if (!strcmp (p, "capture"))
15284 code = OMP_ATOMIC_CAPTURE_NEW;
15285 else
15286 p = NULL;
15287 if (p)
15288 c_parser_consume_token (parser);
15290 if (!seq_cst)
15292 if (c_parser_next_token_is (parser, CPP_COMMA)
15293 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15294 c_parser_consume_token (parser);
15296 if (c_parser_next_token_is (parser, CPP_NAME))
15298 const char *p
15299 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15300 if (!strcmp (p, "seq_cst"))
15302 seq_cst = true;
15303 c_parser_consume_token (parser);
15307 c_parser_skip_to_pragma_eol (parser);
15309 switch (code)
15311 case OMP_ATOMIC_READ:
15312 case NOP_EXPR: /* atomic write */
15313 v = c_parser_cast_expression (parser, NULL).value;
15314 non_lvalue_p = !lvalue_p (v);
15315 v = c_fully_fold (v, false, NULL, true);
15316 if (v == error_mark_node)
15317 goto saw_error;
15318 if (non_lvalue_p)
15319 v = non_lvalue (v);
15320 loc = c_parser_peek_token (parser)->location;
15321 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15322 goto saw_error;
15323 if (code == NOP_EXPR)
15325 lhs = c_parser_expression (parser).value;
15326 lhs = c_fully_fold (lhs, false, NULL);
15327 if (lhs == error_mark_node)
15328 goto saw_error;
15330 else
15332 lhs = c_parser_cast_expression (parser, NULL).value;
15333 non_lvalue_p = !lvalue_p (lhs);
15334 lhs = c_fully_fold (lhs, false, NULL, true);
15335 if (lhs == error_mark_node)
15336 goto saw_error;
15337 if (non_lvalue_p)
15338 lhs = non_lvalue (lhs);
15340 if (code == NOP_EXPR)
15342 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15343 opcode. */
15344 code = OMP_ATOMIC;
15345 rhs = lhs;
15346 lhs = v;
15347 v = NULL_TREE;
15349 goto done;
15350 case OMP_ATOMIC_CAPTURE_NEW:
15351 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15353 c_parser_consume_token (parser);
15354 structured_block = true;
15356 else
15358 v = c_parser_cast_expression (parser, NULL).value;
15359 non_lvalue_p = !lvalue_p (v);
15360 v = c_fully_fold (v, false, NULL, true);
15361 if (v == error_mark_node)
15362 goto saw_error;
15363 if (non_lvalue_p)
15364 v = non_lvalue (v);
15365 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15366 goto saw_error;
15368 break;
15369 default:
15370 break;
15373 /* For structured_block case we don't know yet whether
15374 old or new x should be captured. */
15375 restart:
15376 eloc = c_parser_peek_token (parser)->location;
15377 expr = c_parser_cast_expression (parser, NULL);
15378 lhs = expr.value;
15379 expr = default_function_array_conversion (eloc, expr);
15380 unfolded_lhs = expr.value;
15381 lhs = c_fully_fold (lhs, false, NULL, true);
15382 orig_lhs = lhs;
15383 switch (TREE_CODE (lhs))
15385 case ERROR_MARK:
15386 saw_error:
15387 c_parser_skip_to_end_of_block_or_statement (parser);
15388 if (structured_block)
15390 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15391 c_parser_consume_token (parser);
15392 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15394 c_parser_skip_to_end_of_block_or_statement (parser);
15395 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15396 c_parser_consume_token (parser);
15399 return;
15401 case POSTINCREMENT_EXPR:
15402 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15403 code = OMP_ATOMIC_CAPTURE_OLD;
15404 /* FALLTHROUGH */
15405 case PREINCREMENT_EXPR:
15406 lhs = TREE_OPERAND (lhs, 0);
15407 unfolded_lhs = NULL_TREE;
15408 opcode = PLUS_EXPR;
15409 rhs = integer_one_node;
15410 break;
15412 case POSTDECREMENT_EXPR:
15413 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15414 code = OMP_ATOMIC_CAPTURE_OLD;
15415 /* FALLTHROUGH */
15416 case PREDECREMENT_EXPR:
15417 lhs = TREE_OPERAND (lhs, 0);
15418 unfolded_lhs = NULL_TREE;
15419 opcode = MINUS_EXPR;
15420 rhs = integer_one_node;
15421 break;
15423 case COMPOUND_EXPR:
15424 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15425 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15426 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15427 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15428 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15429 (TREE_OPERAND (lhs, 1), 0), 0)))
15430 == BOOLEAN_TYPE)
15431 /* Undo effects of boolean_increment for post {in,de}crement. */
15432 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15433 /* FALLTHRU */
15434 case MODIFY_EXPR:
15435 if (TREE_CODE (lhs) == MODIFY_EXPR
15436 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15438 /* Undo effects of boolean_increment. */
15439 if (integer_onep (TREE_OPERAND (lhs, 1)))
15441 /* This is pre or post increment. */
15442 rhs = TREE_OPERAND (lhs, 1);
15443 lhs = TREE_OPERAND (lhs, 0);
15444 unfolded_lhs = NULL_TREE;
15445 opcode = NOP_EXPR;
15446 if (code == OMP_ATOMIC_CAPTURE_NEW
15447 && !structured_block
15448 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15449 code = OMP_ATOMIC_CAPTURE_OLD;
15450 break;
15452 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15453 && TREE_OPERAND (lhs, 0)
15454 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15456 /* This is pre or post decrement. */
15457 rhs = TREE_OPERAND (lhs, 1);
15458 lhs = TREE_OPERAND (lhs, 0);
15459 unfolded_lhs = NULL_TREE;
15460 opcode = NOP_EXPR;
15461 if (code == OMP_ATOMIC_CAPTURE_NEW
15462 && !structured_block
15463 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15464 code = OMP_ATOMIC_CAPTURE_OLD;
15465 break;
15468 /* FALLTHRU */
15469 default:
15470 if (!lvalue_p (unfolded_lhs))
15471 lhs = non_lvalue (lhs);
15472 switch (c_parser_peek_token (parser)->type)
15474 case CPP_MULT_EQ:
15475 opcode = MULT_EXPR;
15476 break;
15477 case CPP_DIV_EQ:
15478 opcode = TRUNC_DIV_EXPR;
15479 break;
15480 case CPP_PLUS_EQ:
15481 opcode = PLUS_EXPR;
15482 break;
15483 case CPP_MINUS_EQ:
15484 opcode = MINUS_EXPR;
15485 break;
15486 case CPP_LSHIFT_EQ:
15487 opcode = LSHIFT_EXPR;
15488 break;
15489 case CPP_RSHIFT_EQ:
15490 opcode = RSHIFT_EXPR;
15491 break;
15492 case CPP_AND_EQ:
15493 opcode = BIT_AND_EXPR;
15494 break;
15495 case CPP_OR_EQ:
15496 opcode = BIT_IOR_EXPR;
15497 break;
15498 case CPP_XOR_EQ:
15499 opcode = BIT_XOR_EXPR;
15500 break;
15501 case CPP_EQ:
15502 c_parser_consume_token (parser);
15503 eloc = c_parser_peek_token (parser)->location;
15504 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15505 rhs1 = expr.value;
15506 switch (TREE_CODE (rhs1))
15508 case MULT_EXPR:
15509 case TRUNC_DIV_EXPR:
15510 case RDIV_EXPR:
15511 case PLUS_EXPR:
15512 case MINUS_EXPR:
15513 case LSHIFT_EXPR:
15514 case RSHIFT_EXPR:
15515 case BIT_AND_EXPR:
15516 case BIT_IOR_EXPR:
15517 case BIT_XOR_EXPR:
15518 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15520 opcode = TREE_CODE (rhs1);
15521 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15522 true);
15523 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15524 true);
15525 goto stmt_done;
15527 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15529 opcode = TREE_CODE (rhs1);
15530 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15531 true);
15532 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15533 true);
15534 swapped = !commutative_tree_code (opcode);
15535 goto stmt_done;
15537 break;
15538 case ERROR_MARK:
15539 goto saw_error;
15540 default:
15541 break;
15543 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15545 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15547 code = OMP_ATOMIC_CAPTURE_OLD;
15548 v = lhs;
15549 lhs = NULL_TREE;
15550 expr = default_function_array_read_conversion (eloc, expr);
15551 unfolded_lhs1 = expr.value;
15552 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15553 rhs1 = NULL_TREE;
15554 c_parser_consume_token (parser);
15555 goto restart;
15557 if (structured_block)
15559 opcode = NOP_EXPR;
15560 expr = default_function_array_read_conversion (eloc, expr);
15561 rhs = c_fully_fold (expr.value, false, NULL, true);
15562 rhs1 = NULL_TREE;
15563 goto stmt_done;
15566 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15567 goto saw_error;
15568 default:
15569 c_parser_error (parser,
15570 "invalid operator for %<#pragma omp atomic%>");
15571 goto saw_error;
15574 /* Arrange to pass the location of the assignment operator to
15575 c_finish_omp_atomic. */
15576 loc = c_parser_peek_token (parser)->location;
15577 c_parser_consume_token (parser);
15578 eloc = c_parser_peek_token (parser)->location;
15579 expr = c_parser_expression (parser);
15580 expr = default_function_array_read_conversion (eloc, expr);
15581 rhs = expr.value;
15582 rhs = c_fully_fold (rhs, false, NULL, true);
15583 break;
15585 stmt_done:
15586 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15588 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15589 goto saw_error;
15590 v = c_parser_cast_expression (parser, NULL).value;
15591 non_lvalue_p = !lvalue_p (v);
15592 v = c_fully_fold (v, false, NULL, true);
15593 if (v == error_mark_node)
15594 goto saw_error;
15595 if (non_lvalue_p)
15596 v = non_lvalue (v);
15597 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15598 goto saw_error;
15599 eloc = c_parser_peek_token (parser)->location;
15600 expr = c_parser_cast_expression (parser, NULL);
15601 lhs1 = expr.value;
15602 expr = default_function_array_read_conversion (eloc, expr);
15603 unfolded_lhs1 = expr.value;
15604 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15605 if (lhs1 == error_mark_node)
15606 goto saw_error;
15607 if (!lvalue_p (unfolded_lhs1))
15608 lhs1 = non_lvalue (lhs1);
15610 if (structured_block)
15612 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15613 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15615 done:
15616 if (unfolded_lhs && unfolded_lhs1
15617 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15619 error ("%<#pragma omp atomic capture%> uses two different "
15620 "expressions for memory");
15621 stmt = error_mark_node;
15623 else
15624 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15625 swapped, seq_cst);
15626 if (stmt != error_mark_node)
15627 add_stmt (stmt);
15629 if (!structured_block)
15630 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15634 /* OpenMP 2.5:
15635 # pragma omp barrier new-line
15638 static void
15639 c_parser_omp_barrier (c_parser *parser)
15641 location_t loc = c_parser_peek_token (parser)->location;
15642 c_parser_consume_pragma (parser);
15643 c_parser_skip_to_pragma_eol (parser);
15645 c_finish_omp_barrier (loc);
15648 /* OpenMP 2.5:
15649 # pragma omp critical [(name)] new-line
15650 structured-block
15652 OpenMP 4.5:
15653 # pragma omp critical [(name) [hint(expression)]] new-line
15655 LOC is the location of the #pragma itself. */
15657 #define OMP_CRITICAL_CLAUSE_MASK \
15658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15660 static tree
15661 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15663 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15665 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15667 c_parser_consume_token (parser);
15668 if (c_parser_next_token_is (parser, CPP_NAME))
15670 name = c_parser_peek_token (parser)->value;
15671 c_parser_consume_token (parser);
15672 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15674 else
15675 c_parser_error (parser, "expected identifier");
15677 clauses = c_parser_omp_all_clauses (parser,
15678 OMP_CRITICAL_CLAUSE_MASK,
15679 "#pragma omp critical");
15681 else
15683 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15684 c_parser_error (parser, "expected %<(%> or end of line");
15685 c_parser_skip_to_pragma_eol (parser);
15688 stmt = c_parser_omp_structured_block (parser, if_p);
15689 return c_finish_omp_critical (loc, stmt, name, clauses);
15692 /* OpenMP 2.5:
15693 # pragma omp flush flush-vars[opt] new-line
15695 flush-vars:
15696 ( variable-list ) */
15698 static void
15699 c_parser_omp_flush (c_parser *parser)
15701 location_t loc = c_parser_peek_token (parser)->location;
15702 c_parser_consume_pragma (parser);
15703 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15704 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15705 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15706 c_parser_error (parser, "expected %<(%> or end of line");
15707 c_parser_skip_to_pragma_eol (parser);
15709 c_finish_omp_flush (loc);
15712 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15713 The real trick here is to determine the loop control variable early
15714 so that we can push a new decl if necessary to make it private.
15715 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15716 respectively. */
15718 static tree
15719 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15720 tree clauses, tree *cclauses, bool *if_p)
15722 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15723 tree declv, condv, incrv, initv, ret = NULL_TREE;
15724 tree pre_body = NULL_TREE, this_pre_body;
15725 tree ordered_cl = NULL_TREE;
15726 bool fail = false, open_brace_parsed = false;
15727 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15728 location_t for_loc;
15729 bool tiling = false;
15730 vec<tree, va_gc> *for_block = make_tree_vector ();
15732 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15733 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15734 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15735 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15737 tiling = true;
15738 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15740 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15741 && OMP_CLAUSE_ORDERED_EXPR (cl))
15743 ordered_cl = cl;
15744 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15747 if (ordered && ordered < collapse)
15749 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15750 "%<ordered%> clause parameter is less than %<collapse%>");
15751 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15752 = build_int_cst (NULL_TREE, collapse);
15753 ordered = collapse;
15755 if (ordered)
15757 for (tree *pc = &clauses; *pc; )
15758 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15760 error_at (OMP_CLAUSE_LOCATION (*pc),
15761 "%<linear%> clause may not be specified together "
15762 "with %<ordered%> clause with a parameter");
15763 *pc = OMP_CLAUSE_CHAIN (*pc);
15765 else
15766 pc = &OMP_CLAUSE_CHAIN (*pc);
15769 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15770 count = ordered ? ordered : collapse;
15772 declv = make_tree_vec (count);
15773 initv = make_tree_vec (count);
15774 condv = make_tree_vec (count);
15775 incrv = make_tree_vec (count);
15777 if (code != CILK_FOR
15778 && !c_parser_next_token_is_keyword (parser, RID_FOR))
15780 c_parser_error (parser, "for statement expected");
15781 return NULL;
15783 if (code == CILK_FOR
15784 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
15786 c_parser_error (parser, "_Cilk_for statement expected");
15787 return NULL;
15789 for_loc = c_parser_peek_token (parser)->location;
15790 c_parser_consume_token (parser);
15792 for (i = 0; i < count; i++)
15794 int bracecount = 0;
15796 matching_parens parens;
15797 if (!parens.require_open (parser))
15798 goto pop_scopes;
15800 /* Parse the initialization declaration or expression. */
15801 if (c_parser_next_tokens_start_declaration (parser))
15803 if (i > 0)
15804 vec_safe_push (for_block, c_begin_compound_stmt (true));
15805 this_pre_body = push_stmt_list ();
15806 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15807 NULL, vNULL);
15808 if (this_pre_body)
15810 this_pre_body = pop_stmt_list (this_pre_body);
15811 if (pre_body)
15813 tree t = pre_body;
15814 pre_body = push_stmt_list ();
15815 add_stmt (t);
15816 add_stmt (this_pre_body);
15817 pre_body = pop_stmt_list (pre_body);
15819 else
15820 pre_body = this_pre_body;
15822 decl = check_for_loop_decls (for_loc, flag_isoc99);
15823 if (decl == NULL)
15824 goto error_init;
15825 if (DECL_INITIAL (decl) == error_mark_node)
15826 decl = error_mark_node;
15827 init = decl;
15829 else if (c_parser_next_token_is (parser, CPP_NAME)
15830 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15832 struct c_expr decl_exp;
15833 struct c_expr init_exp;
15834 location_t init_loc;
15836 decl_exp = c_parser_postfix_expression (parser);
15837 decl = decl_exp.value;
15839 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15841 init_loc = c_parser_peek_token (parser)->location;
15842 init_exp = c_parser_expr_no_commas (parser, NULL);
15843 init_exp = default_function_array_read_conversion (init_loc,
15844 init_exp);
15845 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15846 NOP_EXPR, init_loc, init_exp.value,
15847 init_exp.original_type);
15848 init = c_process_expr_stmt (init_loc, init);
15850 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15852 else
15854 error_init:
15855 c_parser_error (parser,
15856 "expected iteration declaration or initialization");
15857 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15858 "expected %<)%>");
15859 fail = true;
15860 goto parse_next;
15863 /* Parse the loop condition. */
15864 cond = NULL_TREE;
15865 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15867 location_t cond_loc = c_parser_peek_token (parser)->location;
15868 struct c_expr cond_expr
15869 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15871 cond = cond_expr.value;
15872 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15873 if (COMPARISON_CLASS_P (cond))
15875 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15876 op0 = c_fully_fold (op0, false, NULL);
15877 op1 = c_fully_fold (op1, false, NULL);
15878 TREE_OPERAND (cond, 0) = op0;
15879 TREE_OPERAND (cond, 1) = op1;
15881 switch (cond_expr.original_code)
15883 case GT_EXPR:
15884 case GE_EXPR:
15885 case LT_EXPR:
15886 case LE_EXPR:
15887 break;
15888 case NE_EXPR:
15889 if (code == CILK_SIMD || code == CILK_FOR)
15890 break;
15891 /* FALLTHRU. */
15892 default:
15893 /* Can't be cond = error_mark_node, because we want to preserve
15894 the location until c_finish_omp_for. */
15895 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15896 break;
15898 protected_set_expr_location (cond, cond_loc);
15900 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15902 /* Parse the increment expression. */
15903 incr = NULL_TREE;
15904 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15906 location_t incr_loc = c_parser_peek_token (parser)->location;
15908 incr = c_process_expr_stmt (incr_loc,
15909 c_parser_expression (parser).value);
15911 parens.skip_until_found_close (parser);
15913 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15914 fail = true;
15915 else
15917 TREE_VEC_ELT (declv, i) = decl;
15918 TREE_VEC_ELT (initv, i) = init;
15919 TREE_VEC_ELT (condv, i) = cond;
15920 TREE_VEC_ELT (incrv, i) = incr;
15923 parse_next:
15924 if (i == count - 1)
15925 break;
15927 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15928 in between the collapsed for loops to be still considered perfectly
15929 nested. Hopefully the final version clarifies this.
15930 For now handle (multiple) {'s and empty statements. */
15933 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15935 c_parser_consume_token (parser);
15936 break;
15938 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15940 c_parser_consume_token (parser);
15941 bracecount++;
15943 else if (bracecount
15944 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15945 c_parser_consume_token (parser);
15946 else
15948 c_parser_error (parser, "not enough perfectly nested loops");
15949 if (bracecount)
15951 open_brace_parsed = true;
15952 bracecount--;
15954 fail = true;
15955 count = 0;
15956 break;
15959 while (1);
15961 nbraces += bracecount;
15964 if (nbraces)
15965 if_p = NULL;
15967 save_break = c_break_label;
15968 if (code == CILK_SIMD)
15969 c_break_label = build_int_cst (size_type_node, 2);
15970 else
15971 c_break_label = size_one_node;
15972 save_cont = c_cont_label;
15973 c_cont_label = NULL_TREE;
15974 body = push_stmt_list ();
15976 if (open_brace_parsed)
15978 location_t here = c_parser_peek_token (parser)->location;
15979 stmt = c_begin_compound_stmt (true);
15980 c_parser_compound_statement_nostart (parser);
15981 add_stmt (c_end_compound_stmt (here, stmt, true));
15983 else
15984 add_stmt (c_parser_c99_block_statement (parser, if_p));
15985 if (c_cont_label)
15987 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15988 SET_EXPR_LOCATION (t, loc);
15989 add_stmt (t);
15992 body = pop_stmt_list (body);
15993 c_break_label = save_break;
15994 c_cont_label = save_cont;
15996 while (nbraces)
15998 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16000 c_parser_consume_token (parser);
16001 nbraces--;
16003 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
16004 c_parser_consume_token (parser);
16005 else
16007 c_parser_error (parser, "collapsed loops not perfectly nested");
16008 while (nbraces)
16010 location_t here = c_parser_peek_token (parser)->location;
16011 stmt = c_begin_compound_stmt (true);
16012 add_stmt (body);
16013 c_parser_compound_statement_nostart (parser);
16014 body = c_end_compound_stmt (here, stmt, true);
16015 nbraces--;
16017 goto pop_scopes;
16021 /* Only bother calling c_finish_omp_for if we haven't already generated
16022 an error from the initialization parsing. */
16023 if (!fail)
16025 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
16026 incrv, body, pre_body);
16028 /* Check for iterators appearing in lb, b or incr expressions. */
16029 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
16030 stmt = NULL_TREE;
16032 if (stmt)
16034 add_stmt (stmt);
16036 if (cclauses != NULL
16037 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
16039 tree *c;
16040 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
16041 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
16042 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
16043 c = &OMP_CLAUSE_CHAIN (*c);
16044 else
16046 for (i = 0; i < count; i++)
16047 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
16048 break;
16049 if (i == count)
16050 c = &OMP_CLAUSE_CHAIN (*c);
16051 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
16053 error_at (loc,
16054 "iteration variable %qD should not be firstprivate",
16055 OMP_CLAUSE_DECL (*c));
16056 *c = OMP_CLAUSE_CHAIN (*c);
16058 else
16060 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16061 tree l = *c;
16062 *c = OMP_CLAUSE_CHAIN (*c);
16063 if (code == OMP_SIMD)
16065 OMP_CLAUSE_CHAIN (l)
16066 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16067 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
16069 else
16071 OMP_CLAUSE_CHAIN (l) = clauses;
16072 clauses = l;
16077 OMP_FOR_CLAUSES (stmt) = clauses;
16079 ret = stmt;
16081 pop_scopes:
16082 while (!for_block->is_empty ())
16084 /* FIXME diagnostics: LOC below should be the actual location of
16085 this particular for block. We need to build a list of
16086 locations to go along with FOR_BLOCK. */
16087 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
16088 add_stmt (stmt);
16090 release_tree_vector (for_block);
16091 return ret;
16094 /* Helper function for OpenMP parsing, split clauses and call
16095 finish_omp_clauses on each of the set of clauses afterwards. */
16097 static void
16098 omp_split_clauses (location_t loc, enum tree_code code,
16099 omp_clause_mask mask, tree clauses, tree *cclauses)
16101 int i;
16102 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
16103 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
16104 if (cclauses[i])
16105 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
16108 /* OpenMP 4.0:
16109 #pragma omp simd simd-clause[optseq] new-line
16110 for-loop
16112 LOC is the location of the #pragma token.
16115 #define OMP_SIMD_CLAUSE_MASK \
16116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
16117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16125 static tree
16126 c_parser_omp_simd (location_t loc, c_parser *parser,
16127 char *p_name, omp_clause_mask mask, tree *cclauses,
16128 bool *if_p)
16130 tree block, clauses, ret;
16132 strcat (p_name, " simd");
16133 mask |= OMP_SIMD_CLAUSE_MASK;
16135 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16136 if (cclauses)
16138 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
16139 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
16140 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
16141 OMP_CLAUSE_ORDERED);
16142 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
16144 error_at (OMP_CLAUSE_LOCATION (c),
16145 "%<ordered%> clause with parameter may not be specified "
16146 "on %qs construct", p_name);
16147 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
16151 block = c_begin_compound_stmt (true);
16152 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
16153 block = c_end_compound_stmt (loc, block, true);
16154 add_stmt (block);
16156 return ret;
16159 /* OpenMP 2.5:
16160 #pragma omp for for-clause[optseq] new-line
16161 for-loop
16163 OpenMP 4.0:
16164 #pragma omp for simd for-simd-clause[optseq] new-line
16165 for-loop
16167 LOC is the location of the #pragma token.
16170 #define OMP_FOR_CLAUSE_MASK \
16171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
16177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
16178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16181 static tree
16182 c_parser_omp_for (location_t loc, c_parser *parser,
16183 char *p_name, omp_clause_mask mask, tree *cclauses,
16184 bool *if_p)
16186 tree block, clauses, ret;
16188 strcat (p_name, " for");
16189 mask |= OMP_FOR_CLAUSE_MASK;
16190 /* parallel for{, simd} disallows nowait clause, but for
16191 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16192 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16193 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16194 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16195 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16196 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16198 if (c_parser_next_token_is (parser, CPP_NAME))
16200 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16202 if (strcmp (p, "simd") == 0)
16204 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16205 if (cclauses == NULL)
16206 cclauses = cclauses_buf;
16208 c_parser_consume_token (parser);
16209 if (!flag_openmp) /* flag_openmp_simd */
16210 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16211 if_p);
16212 block = c_begin_compound_stmt (true);
16213 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16214 block = c_end_compound_stmt (loc, block, true);
16215 if (ret == NULL_TREE)
16216 return ret;
16217 ret = make_node (OMP_FOR);
16218 TREE_TYPE (ret) = void_type_node;
16219 OMP_FOR_BODY (ret) = block;
16220 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16221 SET_EXPR_LOCATION (ret, loc);
16222 add_stmt (ret);
16223 return ret;
16226 if (!flag_openmp) /* flag_openmp_simd */
16228 c_parser_skip_to_pragma_eol (parser, false);
16229 return NULL_TREE;
16232 /* Composite distribute parallel for disallows linear clause. */
16233 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16234 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16236 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16237 if (cclauses)
16239 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16240 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16243 block = c_begin_compound_stmt (true);
16244 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16245 block = c_end_compound_stmt (loc, block, true);
16246 add_stmt (block);
16248 return ret;
16251 /* OpenMP 2.5:
16252 # pragma omp master new-line
16253 structured-block
16255 LOC is the location of the #pragma token.
16258 static tree
16259 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16261 c_parser_skip_to_pragma_eol (parser);
16262 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16263 if_p));
16266 /* OpenMP 2.5:
16267 # pragma omp ordered new-line
16268 structured-block
16270 OpenMP 4.5:
16271 # pragma omp ordered ordered-clauses new-line
16272 structured-block
16274 # pragma omp ordered depend-clauses new-line */
16276 #define OMP_ORDERED_CLAUSE_MASK \
16277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16280 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16281 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16283 static bool
16284 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16285 bool *if_p)
16287 location_t loc = c_parser_peek_token (parser)->location;
16288 c_parser_consume_pragma (parser);
16290 if (context != pragma_stmt && context != pragma_compound)
16292 c_parser_error (parser, "expected declaration specifiers");
16293 c_parser_skip_to_pragma_eol (parser, false);
16294 return false;
16297 if (c_parser_next_token_is (parser, CPP_NAME))
16299 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16301 if (!strcmp ("depend", p))
16303 if (!flag_openmp) /* flag_openmp_simd */
16305 c_parser_skip_to_pragma_eol (parser, false);
16306 return false;
16308 if (context == pragma_stmt)
16310 error_at (loc,
16311 "%<#pragma omp ordered%> with %<depend%> clause may "
16312 "only be used in compound statements");
16313 c_parser_skip_to_pragma_eol (parser, false);
16314 return false;
16317 tree clauses
16318 = c_parser_omp_all_clauses (parser,
16319 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16320 "#pragma omp ordered");
16321 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16322 return false;
16326 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16327 "#pragma omp ordered");
16329 if (!flag_openmp /* flag_openmp_simd */
16330 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16331 return false;
16333 c_finish_omp_ordered (loc, clauses,
16334 c_parser_omp_structured_block (parser, if_p));
16335 return true;
16338 /* OpenMP 2.5:
16340 section-scope:
16341 { section-sequence }
16343 section-sequence:
16344 section-directive[opt] structured-block
16345 section-sequence section-directive structured-block
16347 SECTIONS_LOC is the location of the #pragma omp sections. */
16349 static tree
16350 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16352 tree stmt, substmt;
16353 bool error_suppress = false;
16354 location_t loc;
16356 loc = c_parser_peek_token (parser)->location;
16357 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16359 /* Avoid skipping until the end of the block. */
16360 parser->error = false;
16361 return NULL_TREE;
16364 stmt = push_stmt_list ();
16366 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16368 substmt = c_parser_omp_structured_block (parser, NULL);
16369 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16370 SET_EXPR_LOCATION (substmt, loc);
16371 add_stmt (substmt);
16374 while (1)
16376 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16377 break;
16378 if (c_parser_next_token_is (parser, CPP_EOF))
16379 break;
16381 loc = c_parser_peek_token (parser)->location;
16382 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16384 c_parser_consume_pragma (parser);
16385 c_parser_skip_to_pragma_eol (parser);
16386 error_suppress = false;
16388 else if (!error_suppress)
16390 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16391 error_suppress = true;
16394 substmt = c_parser_omp_structured_block (parser, NULL);
16395 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16396 SET_EXPR_LOCATION (substmt, loc);
16397 add_stmt (substmt);
16399 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16400 "expected %<#pragma omp section%> or %<}%>");
16402 substmt = pop_stmt_list (stmt);
16404 stmt = make_node (OMP_SECTIONS);
16405 SET_EXPR_LOCATION (stmt, sections_loc);
16406 TREE_TYPE (stmt) = void_type_node;
16407 OMP_SECTIONS_BODY (stmt) = substmt;
16409 return add_stmt (stmt);
16412 /* OpenMP 2.5:
16413 # pragma omp sections sections-clause[optseq] newline
16414 sections-scope
16416 LOC is the location of the #pragma token.
16419 #define OMP_SECTIONS_CLAUSE_MASK \
16420 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16426 static tree
16427 c_parser_omp_sections (location_t loc, c_parser *parser,
16428 char *p_name, omp_clause_mask mask, tree *cclauses)
16430 tree block, clauses, ret;
16432 strcat (p_name, " sections");
16433 mask |= OMP_SECTIONS_CLAUSE_MASK;
16434 if (cclauses)
16435 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16437 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16438 if (cclauses)
16440 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16441 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16444 block = c_begin_compound_stmt (true);
16445 ret = c_parser_omp_sections_scope (loc, parser);
16446 if (ret)
16447 OMP_SECTIONS_CLAUSES (ret) = clauses;
16448 block = c_end_compound_stmt (loc, block, true);
16449 add_stmt (block);
16451 return ret;
16454 /* OpenMP 2.5:
16455 # pragma omp parallel parallel-clause[optseq] new-line
16456 structured-block
16457 # pragma omp parallel for parallel-for-clause[optseq] new-line
16458 structured-block
16459 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16460 structured-block
16462 OpenMP 4.0:
16463 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16464 structured-block
16466 LOC is the location of the #pragma token.
16469 #define OMP_PARALLEL_CLAUSE_MASK \
16470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16480 static tree
16481 c_parser_omp_parallel (location_t loc, c_parser *parser,
16482 char *p_name, omp_clause_mask mask, tree *cclauses,
16483 bool *if_p)
16485 tree stmt, clauses, block;
16487 strcat (p_name, " parallel");
16488 mask |= OMP_PARALLEL_CLAUSE_MASK;
16489 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16490 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16491 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16492 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16494 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16496 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16497 if (cclauses == NULL)
16498 cclauses = cclauses_buf;
16500 c_parser_consume_token (parser);
16501 if (!flag_openmp) /* flag_openmp_simd */
16502 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16503 block = c_begin_omp_parallel ();
16504 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16505 stmt
16506 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16507 block);
16508 if (ret == NULL_TREE)
16509 return ret;
16510 OMP_PARALLEL_COMBINED (stmt) = 1;
16511 return stmt;
16513 /* When combined with distribute, parallel has to be followed by for.
16514 #pragma omp target parallel is allowed though. */
16515 else if (cclauses
16516 && (mask & (OMP_CLAUSE_MASK_1
16517 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16519 error_at (loc, "expected %<for%> after %qs", p_name);
16520 c_parser_skip_to_pragma_eol (parser);
16521 return NULL_TREE;
16523 else if (!flag_openmp) /* flag_openmp_simd */
16525 c_parser_skip_to_pragma_eol (parser, false);
16526 return NULL_TREE;
16528 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16530 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16531 if (strcmp (p, "sections") == 0)
16533 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16534 if (cclauses == NULL)
16535 cclauses = cclauses_buf;
16537 c_parser_consume_token (parser);
16538 block = c_begin_omp_parallel ();
16539 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16540 stmt = c_finish_omp_parallel (loc,
16541 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16542 block);
16543 OMP_PARALLEL_COMBINED (stmt) = 1;
16544 return stmt;
16548 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16549 if (cclauses)
16551 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16552 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16555 block = c_begin_omp_parallel ();
16556 c_parser_statement (parser, if_p);
16557 stmt = c_finish_omp_parallel (loc, clauses, block);
16559 return stmt;
16562 /* OpenMP 2.5:
16563 # pragma omp single single-clause[optseq] new-line
16564 structured-block
16566 LOC is the location of the #pragma.
16569 #define OMP_SINGLE_CLAUSE_MASK \
16570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16575 static tree
16576 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16578 tree stmt = make_node (OMP_SINGLE);
16579 SET_EXPR_LOCATION (stmt, loc);
16580 TREE_TYPE (stmt) = void_type_node;
16582 OMP_SINGLE_CLAUSES (stmt)
16583 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16584 "#pragma omp single");
16585 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16587 return add_stmt (stmt);
16590 /* OpenMP 3.0:
16591 # pragma omp task task-clause[optseq] new-line
16593 LOC is the location of the #pragma.
16596 #define OMP_TASK_CLAUSE_MASK \
16597 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16608 static tree
16609 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16611 tree clauses, block;
16613 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16614 "#pragma omp task");
16616 block = c_begin_omp_task ();
16617 c_parser_statement (parser, if_p);
16618 return c_finish_omp_task (loc, clauses, block);
16621 /* OpenMP 3.0:
16622 # pragma omp taskwait new-line
16625 static void
16626 c_parser_omp_taskwait (c_parser *parser)
16628 location_t loc = c_parser_peek_token (parser)->location;
16629 c_parser_consume_pragma (parser);
16630 c_parser_skip_to_pragma_eol (parser);
16632 c_finish_omp_taskwait (loc);
16635 /* OpenMP 3.1:
16636 # pragma omp taskyield new-line
16639 static void
16640 c_parser_omp_taskyield (c_parser *parser)
16642 location_t loc = c_parser_peek_token (parser)->location;
16643 c_parser_consume_pragma (parser);
16644 c_parser_skip_to_pragma_eol (parser);
16646 c_finish_omp_taskyield (loc);
16649 /* OpenMP 4.0:
16650 # pragma omp taskgroup new-line
16653 static tree
16654 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16656 location_t loc = c_parser_peek_token (parser)->location;
16657 c_parser_skip_to_pragma_eol (parser);
16658 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16659 if_p));
16662 /* OpenMP 4.0:
16663 # pragma omp cancel cancel-clause[optseq] new-line
16665 LOC is the location of the #pragma.
16668 #define OMP_CANCEL_CLAUSE_MASK \
16669 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16675 static void
16676 c_parser_omp_cancel (c_parser *parser)
16678 location_t loc = c_parser_peek_token (parser)->location;
16680 c_parser_consume_pragma (parser);
16681 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16682 "#pragma omp cancel");
16684 c_finish_omp_cancel (loc, clauses);
16687 /* OpenMP 4.0:
16688 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16690 LOC is the location of the #pragma.
16693 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16694 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16699 static void
16700 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16702 location_t loc = c_parser_peek_token (parser)->location;
16703 tree clauses;
16704 bool point_seen = false;
16706 c_parser_consume_pragma (parser);
16707 if (c_parser_next_token_is (parser, CPP_NAME))
16709 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16710 if (strcmp (p, "point") == 0)
16712 c_parser_consume_token (parser);
16713 point_seen = true;
16716 if (!point_seen)
16718 c_parser_error (parser, "expected %<point%>");
16719 c_parser_skip_to_pragma_eol (parser);
16720 return;
16723 if (context != pragma_compound)
16725 if (context == pragma_stmt)
16726 error_at (loc,
16727 "%<#pragma %s%> may only be used in compound statements",
16728 "omp cancellation point");
16729 else
16730 c_parser_error (parser, "expected declaration specifiers");
16731 c_parser_skip_to_pragma_eol (parser, false);
16732 return;
16735 clauses
16736 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16737 "#pragma omp cancellation point");
16739 c_finish_omp_cancellation_point (loc, clauses);
16742 /* OpenMP 4.0:
16743 #pragma omp distribute distribute-clause[optseq] new-line
16744 for-loop */
16746 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16753 static tree
16754 c_parser_omp_distribute (location_t loc, c_parser *parser,
16755 char *p_name, omp_clause_mask mask, tree *cclauses,
16756 bool *if_p)
16758 tree clauses, block, ret;
16760 strcat (p_name, " distribute");
16761 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16763 if (c_parser_next_token_is (parser, CPP_NAME))
16765 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16766 bool simd = false;
16767 bool parallel = false;
16769 if (strcmp (p, "simd") == 0)
16770 simd = true;
16771 else
16772 parallel = strcmp (p, "parallel") == 0;
16773 if (parallel || simd)
16775 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16776 if (cclauses == NULL)
16777 cclauses = cclauses_buf;
16778 c_parser_consume_token (parser);
16779 if (!flag_openmp) /* flag_openmp_simd */
16781 if (simd)
16782 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16783 if_p);
16784 else
16785 return c_parser_omp_parallel (loc, parser, p_name, mask,
16786 cclauses, if_p);
16788 block = c_begin_compound_stmt (true);
16789 if (simd)
16790 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16791 if_p);
16792 else
16793 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16794 if_p);
16795 block = c_end_compound_stmt (loc, block, true);
16796 if (ret == NULL)
16797 return ret;
16798 ret = make_node (OMP_DISTRIBUTE);
16799 TREE_TYPE (ret) = void_type_node;
16800 OMP_FOR_BODY (ret) = block;
16801 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16802 SET_EXPR_LOCATION (ret, loc);
16803 add_stmt (ret);
16804 return ret;
16807 if (!flag_openmp) /* flag_openmp_simd */
16809 c_parser_skip_to_pragma_eol (parser, false);
16810 return NULL_TREE;
16813 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16814 if (cclauses)
16816 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16817 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16820 block = c_begin_compound_stmt (true);
16821 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16822 if_p);
16823 block = c_end_compound_stmt (loc, block, true);
16824 add_stmt (block);
16826 return ret;
16829 /* OpenMP 4.0:
16830 # pragma omp teams teams-clause[optseq] new-line
16831 structured-block */
16833 #define OMP_TEAMS_CLAUSE_MASK \
16834 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16842 static tree
16843 c_parser_omp_teams (location_t loc, c_parser *parser,
16844 char *p_name, omp_clause_mask mask, tree *cclauses,
16845 bool *if_p)
16847 tree clauses, block, ret;
16849 strcat (p_name, " teams");
16850 mask |= OMP_TEAMS_CLAUSE_MASK;
16852 if (c_parser_next_token_is (parser, CPP_NAME))
16854 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16855 if (strcmp (p, "distribute") == 0)
16857 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16858 if (cclauses == NULL)
16859 cclauses = cclauses_buf;
16861 c_parser_consume_token (parser);
16862 if (!flag_openmp) /* flag_openmp_simd */
16863 return c_parser_omp_distribute (loc, parser, p_name, mask,
16864 cclauses, if_p);
16865 block = c_begin_compound_stmt (true);
16866 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16867 if_p);
16868 block = c_end_compound_stmt (loc, block, true);
16869 if (ret == NULL)
16870 return ret;
16871 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16872 ret = make_node (OMP_TEAMS);
16873 TREE_TYPE (ret) = void_type_node;
16874 OMP_TEAMS_CLAUSES (ret) = clauses;
16875 OMP_TEAMS_BODY (ret) = block;
16876 OMP_TEAMS_COMBINED (ret) = 1;
16877 return add_stmt (ret);
16880 if (!flag_openmp) /* flag_openmp_simd */
16882 c_parser_skip_to_pragma_eol (parser, false);
16883 return NULL_TREE;
16886 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16887 if (cclauses)
16889 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16890 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16893 tree stmt = make_node (OMP_TEAMS);
16894 TREE_TYPE (stmt) = void_type_node;
16895 OMP_TEAMS_CLAUSES (stmt) = clauses;
16896 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16898 return add_stmt (stmt);
16901 /* OpenMP 4.0:
16902 # pragma omp target data target-data-clause[optseq] new-line
16903 structured-block */
16905 #define OMP_TARGET_DATA_CLAUSE_MASK \
16906 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16911 static tree
16912 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16914 tree clauses
16915 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16916 "#pragma omp target data");
16917 int map_seen = 0;
16918 for (tree *pc = &clauses; *pc;)
16920 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16921 switch (OMP_CLAUSE_MAP_KIND (*pc))
16923 case GOMP_MAP_TO:
16924 case GOMP_MAP_ALWAYS_TO:
16925 case GOMP_MAP_FROM:
16926 case GOMP_MAP_ALWAYS_FROM:
16927 case GOMP_MAP_TOFROM:
16928 case GOMP_MAP_ALWAYS_TOFROM:
16929 case GOMP_MAP_ALLOC:
16930 map_seen = 3;
16931 break;
16932 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16933 case GOMP_MAP_ALWAYS_POINTER:
16934 break;
16935 default:
16936 map_seen |= 1;
16937 error_at (OMP_CLAUSE_LOCATION (*pc),
16938 "%<#pragma omp target data%> with map-type other "
16939 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16940 "on %<map%> clause");
16941 *pc = OMP_CLAUSE_CHAIN (*pc);
16942 continue;
16944 pc = &OMP_CLAUSE_CHAIN (*pc);
16947 if (map_seen != 3)
16949 if (map_seen == 0)
16950 error_at (loc,
16951 "%<#pragma omp target data%> must contain at least "
16952 "one %<map%> clause");
16953 return NULL_TREE;
16956 tree stmt = make_node (OMP_TARGET_DATA);
16957 TREE_TYPE (stmt) = void_type_node;
16958 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16959 keep_next_level ();
16960 tree block = c_begin_compound_stmt (true);
16961 add_stmt (c_parser_omp_structured_block (parser, if_p));
16962 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16964 SET_EXPR_LOCATION (stmt, loc);
16965 return add_stmt (stmt);
16968 /* OpenMP 4.0:
16969 # pragma omp target update target-update-clause[optseq] new-line */
16971 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16972 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16979 static bool
16980 c_parser_omp_target_update (location_t loc, c_parser *parser,
16981 enum pragma_context context)
16983 if (context == pragma_stmt)
16985 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16986 "omp target update");
16987 c_parser_skip_to_pragma_eol (parser, false);
16988 return false;
16991 tree clauses
16992 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16993 "#pragma omp target update");
16994 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16995 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16997 error_at (loc,
16998 "%<#pragma omp target update%> must contain at least one "
16999 "%<from%> or %<to%> clauses");
17000 return false;
17003 tree stmt = make_node (OMP_TARGET_UPDATE);
17004 TREE_TYPE (stmt) = void_type_node;
17005 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
17006 SET_EXPR_LOCATION (stmt, loc);
17007 add_stmt (stmt);
17008 return false;
17011 /* OpenMP 4.5:
17012 # pragma omp target enter data target-data-clause[optseq] new-line */
17014 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
17015 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17021 static tree
17022 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
17023 enum pragma_context context)
17025 bool data_seen = false;
17026 if (c_parser_next_token_is (parser, CPP_NAME))
17028 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17029 if (strcmp (p, "data") == 0)
17031 c_parser_consume_token (parser);
17032 data_seen = true;
17035 if (!data_seen)
17037 c_parser_error (parser, "expected %<data%>");
17038 c_parser_skip_to_pragma_eol (parser);
17039 return NULL_TREE;
17042 if (context == pragma_stmt)
17044 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17045 "omp target enter data");
17046 c_parser_skip_to_pragma_eol (parser, false);
17047 return NULL_TREE;
17050 tree clauses
17051 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
17052 "#pragma omp target enter data");
17053 int map_seen = 0;
17054 for (tree *pc = &clauses; *pc;)
17056 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17057 switch (OMP_CLAUSE_MAP_KIND (*pc))
17059 case GOMP_MAP_TO:
17060 case GOMP_MAP_ALWAYS_TO:
17061 case GOMP_MAP_ALLOC:
17062 map_seen = 3;
17063 break;
17064 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17065 case GOMP_MAP_ALWAYS_POINTER:
17066 break;
17067 default:
17068 map_seen |= 1;
17069 error_at (OMP_CLAUSE_LOCATION (*pc),
17070 "%<#pragma omp target enter data%> with map-type other "
17071 "than %<to%> or %<alloc%> on %<map%> clause");
17072 *pc = OMP_CLAUSE_CHAIN (*pc);
17073 continue;
17075 pc = &OMP_CLAUSE_CHAIN (*pc);
17078 if (map_seen != 3)
17080 if (map_seen == 0)
17081 error_at (loc,
17082 "%<#pragma omp target enter data%> must contain at least "
17083 "one %<map%> clause");
17084 return NULL_TREE;
17087 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
17088 TREE_TYPE (stmt) = void_type_node;
17089 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
17090 SET_EXPR_LOCATION (stmt, loc);
17091 add_stmt (stmt);
17092 return stmt;
17095 /* OpenMP 4.5:
17096 # pragma omp target exit data target-data-clause[optseq] new-line */
17098 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
17099 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17105 static tree
17106 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
17107 enum pragma_context context)
17109 bool data_seen = false;
17110 if (c_parser_next_token_is (parser, CPP_NAME))
17112 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17113 if (strcmp (p, "data") == 0)
17115 c_parser_consume_token (parser);
17116 data_seen = true;
17119 if (!data_seen)
17121 c_parser_error (parser, "expected %<data%>");
17122 c_parser_skip_to_pragma_eol (parser);
17123 return NULL_TREE;
17126 if (context == pragma_stmt)
17128 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17129 "omp target exit data");
17130 c_parser_skip_to_pragma_eol (parser, false);
17131 return NULL_TREE;
17134 tree clauses
17135 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
17136 "#pragma omp target exit data");
17138 int map_seen = 0;
17139 for (tree *pc = &clauses; *pc;)
17141 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17142 switch (OMP_CLAUSE_MAP_KIND (*pc))
17144 case GOMP_MAP_FROM:
17145 case GOMP_MAP_ALWAYS_FROM:
17146 case GOMP_MAP_RELEASE:
17147 case GOMP_MAP_DELETE:
17148 map_seen = 3;
17149 break;
17150 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17151 case GOMP_MAP_ALWAYS_POINTER:
17152 break;
17153 default:
17154 map_seen |= 1;
17155 error_at (OMP_CLAUSE_LOCATION (*pc),
17156 "%<#pragma omp target exit data%> with map-type other "
17157 "than %<from%>, %<release%> or %<delete%> on %<map%>"
17158 " clause");
17159 *pc = OMP_CLAUSE_CHAIN (*pc);
17160 continue;
17162 pc = &OMP_CLAUSE_CHAIN (*pc);
17165 if (map_seen != 3)
17167 if (map_seen == 0)
17168 error_at (loc,
17169 "%<#pragma omp target exit data%> must contain at least one "
17170 "%<map%> clause");
17171 return NULL_TREE;
17174 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17175 TREE_TYPE (stmt) = void_type_node;
17176 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17177 SET_EXPR_LOCATION (stmt, loc);
17178 add_stmt (stmt);
17179 return stmt;
17182 /* OpenMP 4.0:
17183 # pragma omp target target-clause[optseq] new-line
17184 structured-block */
17186 #define OMP_TARGET_CLAUSE_MASK \
17187 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17197 static bool
17198 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17200 location_t loc = c_parser_peek_token (parser)->location;
17201 c_parser_consume_pragma (parser);
17202 tree *pc = NULL, stmt, block;
17204 if (context != pragma_stmt && context != pragma_compound)
17206 c_parser_error (parser, "expected declaration specifiers");
17207 c_parser_skip_to_pragma_eol (parser);
17208 return false;
17211 if (c_parser_next_token_is (parser, CPP_NAME))
17213 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17214 enum tree_code ccode = ERROR_MARK;
17216 if (strcmp (p, "teams") == 0)
17217 ccode = OMP_TEAMS;
17218 else if (strcmp (p, "parallel") == 0)
17219 ccode = OMP_PARALLEL;
17220 else if (strcmp (p, "simd") == 0)
17221 ccode = OMP_SIMD;
17222 if (ccode != ERROR_MARK)
17224 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17225 char p_name[sizeof ("#pragma omp target teams distribute "
17226 "parallel for simd")];
17228 c_parser_consume_token (parser);
17229 strcpy (p_name, "#pragma omp target");
17230 if (!flag_openmp) /* flag_openmp_simd */
17232 tree stmt;
17233 switch (ccode)
17235 case OMP_TEAMS:
17236 stmt = c_parser_omp_teams (loc, parser, p_name,
17237 OMP_TARGET_CLAUSE_MASK,
17238 cclauses, if_p);
17239 break;
17240 case OMP_PARALLEL:
17241 stmt = c_parser_omp_parallel (loc, parser, p_name,
17242 OMP_TARGET_CLAUSE_MASK,
17243 cclauses, if_p);
17244 break;
17245 case OMP_SIMD:
17246 stmt = c_parser_omp_simd (loc, parser, p_name,
17247 OMP_TARGET_CLAUSE_MASK,
17248 cclauses, if_p);
17249 break;
17250 default:
17251 gcc_unreachable ();
17253 return stmt != NULL_TREE;
17255 keep_next_level ();
17256 tree block = c_begin_compound_stmt (true), ret;
17257 switch (ccode)
17259 case OMP_TEAMS:
17260 ret = c_parser_omp_teams (loc, parser, p_name,
17261 OMP_TARGET_CLAUSE_MASK, cclauses,
17262 if_p);
17263 break;
17264 case OMP_PARALLEL:
17265 ret = c_parser_omp_parallel (loc, parser, p_name,
17266 OMP_TARGET_CLAUSE_MASK, cclauses,
17267 if_p);
17268 break;
17269 case OMP_SIMD:
17270 ret = c_parser_omp_simd (loc, parser, p_name,
17271 OMP_TARGET_CLAUSE_MASK, cclauses,
17272 if_p);
17273 break;
17274 default:
17275 gcc_unreachable ();
17277 block = c_end_compound_stmt (loc, block, true);
17278 if (ret == NULL_TREE)
17279 return false;
17280 if (ccode == OMP_TEAMS)
17282 /* For combined target teams, ensure the num_teams and
17283 thread_limit clause expressions are evaluated on the host,
17284 before entering the target construct. */
17285 tree c;
17286 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17287 c; c = OMP_CLAUSE_CHAIN (c))
17288 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17289 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17290 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17292 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17293 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17294 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17295 expr, NULL_TREE, NULL_TREE);
17296 add_stmt (expr);
17297 OMP_CLAUSE_OPERAND (c, 0) = expr;
17298 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17299 OMP_CLAUSE_FIRSTPRIVATE);
17300 OMP_CLAUSE_DECL (tc) = tmp;
17301 OMP_CLAUSE_CHAIN (tc)
17302 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17303 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17306 tree stmt = make_node (OMP_TARGET);
17307 TREE_TYPE (stmt) = void_type_node;
17308 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17309 OMP_TARGET_BODY (stmt) = block;
17310 OMP_TARGET_COMBINED (stmt) = 1;
17311 add_stmt (stmt);
17312 pc = &OMP_TARGET_CLAUSES (stmt);
17313 goto check_clauses;
17315 else if (!flag_openmp) /* flag_openmp_simd */
17317 c_parser_skip_to_pragma_eol (parser, false);
17318 return false;
17320 else if (strcmp (p, "data") == 0)
17322 c_parser_consume_token (parser);
17323 c_parser_omp_target_data (loc, parser, if_p);
17324 return true;
17326 else if (strcmp (p, "enter") == 0)
17328 c_parser_consume_token (parser);
17329 c_parser_omp_target_enter_data (loc, parser, context);
17330 return false;
17332 else if (strcmp (p, "exit") == 0)
17334 c_parser_consume_token (parser);
17335 c_parser_omp_target_exit_data (loc, parser, context);
17336 return false;
17338 else if (strcmp (p, "update") == 0)
17340 c_parser_consume_token (parser);
17341 return c_parser_omp_target_update (loc, parser, context);
17344 if (!flag_openmp) /* flag_openmp_simd */
17346 c_parser_skip_to_pragma_eol (parser, false);
17347 return false;
17350 stmt = make_node (OMP_TARGET);
17351 TREE_TYPE (stmt) = void_type_node;
17353 OMP_TARGET_CLAUSES (stmt)
17354 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17355 "#pragma omp target");
17356 pc = &OMP_TARGET_CLAUSES (stmt);
17357 keep_next_level ();
17358 block = c_begin_compound_stmt (true);
17359 add_stmt (c_parser_omp_structured_block (parser, if_p));
17360 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17362 SET_EXPR_LOCATION (stmt, loc);
17363 add_stmt (stmt);
17365 check_clauses:
17366 while (*pc)
17368 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17369 switch (OMP_CLAUSE_MAP_KIND (*pc))
17371 case GOMP_MAP_TO:
17372 case GOMP_MAP_ALWAYS_TO:
17373 case GOMP_MAP_FROM:
17374 case GOMP_MAP_ALWAYS_FROM:
17375 case GOMP_MAP_TOFROM:
17376 case GOMP_MAP_ALWAYS_TOFROM:
17377 case GOMP_MAP_ALLOC:
17378 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17379 case GOMP_MAP_ALWAYS_POINTER:
17380 break;
17381 default:
17382 error_at (OMP_CLAUSE_LOCATION (*pc),
17383 "%<#pragma omp target%> with map-type other "
17384 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17385 "on %<map%> clause");
17386 *pc = OMP_CLAUSE_CHAIN (*pc);
17387 continue;
17389 pc = &OMP_CLAUSE_CHAIN (*pc);
17391 return true;
17394 /* OpenMP 4.0:
17395 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17397 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17398 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17405 static void
17406 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17408 auto_vec<c_token> clauses;
17409 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17411 c_token *token = c_parser_peek_token (parser);
17412 if (token->type == CPP_EOF)
17414 c_parser_skip_to_pragma_eol (parser);
17415 return;
17417 clauses.safe_push (*token);
17418 c_parser_consume_token (parser);
17420 clauses.safe_push (*c_parser_peek_token (parser));
17421 c_parser_skip_to_pragma_eol (parser);
17423 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17425 if (c_parser_peek_token (parser)->pragma_kind
17426 != PRAGMA_OMP_DECLARE
17427 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17428 || strcmp (IDENTIFIER_POINTER
17429 (c_parser_peek_2nd_token (parser)->value),
17430 "simd") != 0)
17432 c_parser_error (parser,
17433 "%<#pragma omp declare simd%> must be followed by "
17434 "function declaration or definition or another "
17435 "%<#pragma omp declare simd%>");
17436 return;
17438 c_parser_consume_pragma (parser);
17439 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17441 c_token *token = c_parser_peek_token (parser);
17442 if (token->type == CPP_EOF)
17444 c_parser_skip_to_pragma_eol (parser);
17445 return;
17447 clauses.safe_push (*token);
17448 c_parser_consume_token (parser);
17450 clauses.safe_push (*c_parser_peek_token (parser));
17451 c_parser_skip_to_pragma_eol (parser);
17454 /* Make sure nothing tries to read past the end of the tokens. */
17455 c_token eof_token;
17456 memset (&eof_token, 0, sizeof (eof_token));
17457 eof_token.type = CPP_EOF;
17458 clauses.safe_push (eof_token);
17459 clauses.safe_push (eof_token);
17461 switch (context)
17463 case pragma_external:
17464 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17465 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17467 int ext = disable_extension_diagnostics ();
17469 c_parser_consume_token (parser);
17470 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17471 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17472 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17473 NULL, clauses);
17474 restore_extension_diagnostics (ext);
17476 else
17477 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17478 NULL, clauses);
17479 break;
17480 case pragma_struct:
17481 case pragma_param:
17482 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17483 "function declaration or definition");
17484 break;
17485 case pragma_compound:
17486 case pragma_stmt:
17487 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17488 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17490 int ext = disable_extension_diagnostics ();
17492 c_parser_consume_token (parser);
17493 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17494 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17495 if (c_parser_next_tokens_start_declaration (parser))
17497 c_parser_declaration_or_fndef (parser, true, true, true, true,
17498 true, NULL, clauses);
17499 restore_extension_diagnostics (ext);
17500 break;
17502 restore_extension_diagnostics (ext);
17504 else if (c_parser_next_tokens_start_declaration (parser))
17506 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17507 NULL, clauses);
17508 break;
17510 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17511 "function declaration or definition");
17512 break;
17513 default:
17514 gcc_unreachable ();
17518 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17519 and put that into "omp declare simd" attribute. */
17521 static void
17522 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17523 vec<c_token> clauses)
17525 if (flag_cilkplus
17526 && (clauses.exists ()
17527 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
17528 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
17530 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
17531 "used in the same function marked as a Cilk Plus SIMD-enabled "
17532 "function");
17533 vec_free (parser->cilk_simd_fn_tokens);
17534 return;
17537 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17538 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17539 has already processed the tokens. */
17540 if (clauses.exists () && clauses[0].type == CPP_EOF)
17541 return;
17542 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17544 error ("%<#pragma omp declare simd%> not immediately followed by "
17545 "a function declaration or definition");
17546 clauses[0].type = CPP_EOF;
17547 return;
17549 if (clauses.exists () && clauses[0].type != CPP_NAME)
17551 error_at (DECL_SOURCE_LOCATION (fndecl),
17552 "%<#pragma omp declare simd%> not immediately followed by "
17553 "a single function declaration or definition");
17554 clauses[0].type = CPP_EOF;
17555 return;
17558 if (parms == NULL_TREE)
17559 parms = DECL_ARGUMENTS (fndecl);
17561 unsigned int tokens_avail = parser->tokens_avail;
17562 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17563 bool is_cilkplus_cilk_simd_fn = false;
17565 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
17567 parser->tokens = parser->cilk_simd_fn_tokens->address ();
17568 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
17569 is_cilkplus_cilk_simd_fn = true;
17571 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
17573 error_at (DECL_SOURCE_LOCATION (fndecl),
17574 "%<__simd__%> attribute cannot be used in the same "
17575 "function marked as a Cilk Plus SIMD-enabled function");
17576 vec_free (parser->cilk_simd_fn_tokens);
17577 return;
17581 else
17583 parser->tokens = clauses.address ();
17584 parser->tokens_avail = clauses.length ();
17587 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17588 while (parser->tokens_avail > 3)
17590 c_token *token = c_parser_peek_token (parser);
17591 if (!is_cilkplus_cilk_simd_fn)
17592 gcc_assert (token->type == CPP_NAME
17593 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17594 else
17595 gcc_assert (token->type == CPP_NAME
17596 && is_cilkplus_vector_p (token->value));
17597 c_parser_consume_token (parser);
17598 parser->in_pragma = true;
17600 tree c = NULL_TREE;
17601 if (is_cilkplus_cilk_simd_fn)
17602 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
17603 "SIMD-enabled functions attribute");
17604 else
17605 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17606 "#pragma omp declare simd");
17607 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17608 if (c != NULL_TREE)
17609 c = tree_cons (NULL_TREE, c, NULL_TREE);
17610 if (is_cilkplus_cilk_simd_fn)
17612 tree k = build_tree_list (get_identifier ("cilk simd function"),
17613 NULL_TREE);
17614 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
17615 DECL_ATTRIBUTES (fndecl) = k;
17617 c = build_tree_list (get_identifier ("omp declare simd"), c);
17618 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17619 DECL_ATTRIBUTES (fndecl) = c;
17622 parser->tokens = &parser->tokens_buf[0];
17623 parser->tokens_avail = tokens_avail;
17624 if (clauses.exists ())
17625 clauses[0].type = CPP_PRAGMA;
17627 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
17628 vec_free (parser->cilk_simd_fn_tokens);
17632 /* OpenMP 4.0:
17633 # pragma omp declare target new-line
17634 declarations and definitions
17635 # pragma omp end declare target new-line
17637 OpenMP 4.5:
17638 # pragma omp declare target ( extended-list ) new-line
17640 # pragma omp declare target declare-target-clauses[seq] new-line */
17642 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17643 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17646 static void
17647 c_parser_omp_declare_target (c_parser *parser)
17649 location_t loc = c_parser_peek_token (parser)->location;
17650 tree clauses = NULL_TREE;
17651 if (c_parser_next_token_is (parser, CPP_NAME))
17652 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17653 "#pragma omp declare target");
17654 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17656 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17657 clauses);
17658 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17659 c_parser_skip_to_pragma_eol (parser);
17661 else
17663 c_parser_skip_to_pragma_eol (parser);
17664 current_omp_declare_target_attribute++;
17665 return;
17667 if (current_omp_declare_target_attribute)
17668 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17669 "%<#pragma omp declare target%> without clauses and "
17670 "%<#pragma omp end declare target%>");
17671 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17673 tree t = OMP_CLAUSE_DECL (c), id;
17674 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17675 tree at2 = lookup_attribute ("omp declare target link",
17676 DECL_ATTRIBUTES (t));
17677 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17679 id = get_identifier ("omp declare target link");
17680 std::swap (at1, at2);
17682 else
17683 id = get_identifier ("omp declare target");
17684 if (at2)
17686 error_at (OMP_CLAUSE_LOCATION (c),
17687 "%qD specified both in declare target %<link%> and %<to%>"
17688 " clauses", t);
17689 continue;
17691 if (!at1)
17693 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17694 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17695 continue;
17697 symtab_node *node = symtab_node::get (t);
17698 if (node != NULL)
17700 node->offloadable = 1;
17701 if (ENABLE_OFFLOADING)
17703 g->have_offload = true;
17704 if (is_a <varpool_node *> (node))
17705 vec_safe_push (offload_vars, t);
17712 static void
17713 c_parser_omp_end_declare_target (c_parser *parser)
17715 location_t loc = c_parser_peek_token (parser)->location;
17716 c_parser_consume_pragma (parser);
17717 if (c_parser_next_token_is (parser, CPP_NAME)
17718 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17719 "declare") == 0)
17721 c_parser_consume_token (parser);
17722 if (c_parser_next_token_is (parser, CPP_NAME)
17723 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17724 "target") == 0)
17725 c_parser_consume_token (parser);
17726 else
17728 c_parser_error (parser, "expected %<target%>");
17729 c_parser_skip_to_pragma_eol (parser);
17730 return;
17733 else
17735 c_parser_error (parser, "expected %<declare%>");
17736 c_parser_skip_to_pragma_eol (parser);
17737 return;
17739 c_parser_skip_to_pragma_eol (parser);
17740 if (!current_omp_declare_target_attribute)
17741 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17742 "%<#pragma omp declare target%>");
17743 else
17744 current_omp_declare_target_attribute--;
17748 /* OpenMP 4.0
17749 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17750 initializer-clause[opt] new-line
17752 initializer-clause:
17753 initializer (omp_priv = initializer)
17754 initializer (function-name (argument-list)) */
17756 static void
17757 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17759 unsigned int tokens_avail = 0, i;
17760 vec<tree> types = vNULL;
17761 vec<c_token> clauses = vNULL;
17762 enum tree_code reduc_code = ERROR_MARK;
17763 tree reduc_id = NULL_TREE;
17764 tree type;
17765 location_t rloc = c_parser_peek_token (parser)->location;
17767 if (context == pragma_struct || context == pragma_param)
17769 error ("%<#pragma omp declare reduction%> not at file or block scope");
17770 goto fail;
17773 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17774 goto fail;
17776 switch (c_parser_peek_token (parser)->type)
17778 case CPP_PLUS:
17779 reduc_code = PLUS_EXPR;
17780 break;
17781 case CPP_MULT:
17782 reduc_code = MULT_EXPR;
17783 break;
17784 case CPP_MINUS:
17785 reduc_code = MINUS_EXPR;
17786 break;
17787 case CPP_AND:
17788 reduc_code = BIT_AND_EXPR;
17789 break;
17790 case CPP_XOR:
17791 reduc_code = BIT_XOR_EXPR;
17792 break;
17793 case CPP_OR:
17794 reduc_code = BIT_IOR_EXPR;
17795 break;
17796 case CPP_AND_AND:
17797 reduc_code = TRUTH_ANDIF_EXPR;
17798 break;
17799 case CPP_OR_OR:
17800 reduc_code = TRUTH_ORIF_EXPR;
17801 break;
17802 case CPP_NAME:
17803 const char *p;
17804 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17805 if (strcmp (p, "min") == 0)
17807 reduc_code = MIN_EXPR;
17808 break;
17810 if (strcmp (p, "max") == 0)
17812 reduc_code = MAX_EXPR;
17813 break;
17815 reduc_id = c_parser_peek_token (parser)->value;
17816 break;
17817 default:
17818 c_parser_error (parser,
17819 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17820 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17821 goto fail;
17824 tree orig_reduc_id, reduc_decl;
17825 orig_reduc_id = reduc_id;
17826 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17827 reduc_decl = c_omp_reduction_decl (reduc_id);
17828 c_parser_consume_token (parser);
17830 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17831 goto fail;
17833 while (true)
17835 location_t loc = c_parser_peek_token (parser)->location;
17836 struct c_type_name *ctype = c_parser_type_name (parser);
17837 if (ctype != NULL)
17839 type = groktypename (ctype, NULL, NULL);
17840 if (type == error_mark_node)
17842 else if ((INTEGRAL_TYPE_P (type)
17843 || TREE_CODE (type) == REAL_TYPE
17844 || TREE_CODE (type) == COMPLEX_TYPE)
17845 && orig_reduc_id == NULL_TREE)
17846 error_at (loc, "predeclared arithmetic type in "
17847 "%<#pragma omp declare reduction%>");
17848 else if (TREE_CODE (type) == FUNCTION_TYPE
17849 || TREE_CODE (type) == ARRAY_TYPE)
17850 error_at (loc, "function or array type in "
17851 "%<#pragma omp declare reduction%>");
17852 else if (TYPE_ATOMIC (type))
17853 error_at (loc, "%<_Atomic%> qualified type in "
17854 "%<#pragma omp declare reduction%>");
17855 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17856 error_at (loc, "const, volatile or restrict qualified type in "
17857 "%<#pragma omp declare reduction%>");
17858 else
17860 tree t;
17861 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17862 if (comptypes (TREE_PURPOSE (t), type))
17864 error_at (loc, "redeclaration of %qs "
17865 "%<#pragma omp declare reduction%> for "
17866 "type %qT",
17867 IDENTIFIER_POINTER (reduc_id)
17868 + sizeof ("omp declare reduction ") - 1,
17869 type);
17870 location_t ploc
17871 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17872 0));
17873 error_at (ploc, "previous %<#pragma omp declare "
17874 "reduction%>");
17875 break;
17877 if (t == NULL_TREE)
17878 types.safe_push (type);
17880 if (c_parser_next_token_is (parser, CPP_COMMA))
17881 c_parser_consume_token (parser);
17882 else
17883 break;
17885 else
17886 break;
17889 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17890 || types.is_empty ())
17892 fail:
17893 clauses.release ();
17894 types.release ();
17895 while (true)
17897 c_token *token = c_parser_peek_token (parser);
17898 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17899 break;
17900 c_parser_consume_token (parser);
17902 c_parser_skip_to_pragma_eol (parser);
17903 return;
17906 if (types.length () > 1)
17908 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17910 c_token *token = c_parser_peek_token (parser);
17911 if (token->type == CPP_EOF)
17912 goto fail;
17913 clauses.safe_push (*token);
17914 c_parser_consume_token (parser);
17916 clauses.safe_push (*c_parser_peek_token (parser));
17917 c_parser_skip_to_pragma_eol (parser);
17919 /* Make sure nothing tries to read past the end of the tokens. */
17920 c_token eof_token;
17921 memset (&eof_token, 0, sizeof (eof_token));
17922 eof_token.type = CPP_EOF;
17923 clauses.safe_push (eof_token);
17924 clauses.safe_push (eof_token);
17927 int errs = errorcount;
17928 FOR_EACH_VEC_ELT (types, i, type)
17930 tokens_avail = parser->tokens_avail;
17931 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17932 if (!clauses.is_empty ())
17934 parser->tokens = clauses.address ();
17935 parser->tokens_avail = clauses.length ();
17936 parser->in_pragma = true;
17939 bool nested = current_function_decl != NULL_TREE;
17940 if (nested)
17941 c_push_function_context ();
17942 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17943 reduc_id, default_function_type);
17944 current_function_decl = fndecl;
17945 allocate_struct_function (fndecl, true);
17946 push_scope ();
17947 tree stmt = push_stmt_list ();
17948 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17949 warn about these. */
17950 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17951 get_identifier ("omp_out"), type);
17952 DECL_ARTIFICIAL (omp_out) = 1;
17953 DECL_CONTEXT (omp_out) = fndecl;
17954 pushdecl (omp_out);
17955 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17956 get_identifier ("omp_in"), type);
17957 DECL_ARTIFICIAL (omp_in) = 1;
17958 DECL_CONTEXT (omp_in) = fndecl;
17959 pushdecl (omp_in);
17960 struct c_expr combiner = c_parser_expression (parser);
17961 struct c_expr initializer;
17962 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17963 bool bad = false;
17964 initializer.value = error_mark_node;
17965 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17966 bad = true;
17967 else if (c_parser_next_token_is (parser, CPP_NAME)
17968 && strcmp (IDENTIFIER_POINTER
17969 (c_parser_peek_token (parser)->value),
17970 "initializer") == 0)
17972 c_parser_consume_token (parser);
17973 pop_scope ();
17974 push_scope ();
17975 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17976 get_identifier ("omp_priv"), type);
17977 DECL_ARTIFICIAL (omp_priv) = 1;
17978 DECL_INITIAL (omp_priv) = error_mark_node;
17979 DECL_CONTEXT (omp_priv) = fndecl;
17980 pushdecl (omp_priv);
17981 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17982 get_identifier ("omp_orig"), type);
17983 DECL_ARTIFICIAL (omp_orig) = 1;
17984 DECL_CONTEXT (omp_orig) = fndecl;
17985 pushdecl (omp_orig);
17986 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17987 bad = true;
17988 else if (!c_parser_next_token_is (parser, CPP_NAME))
17990 c_parser_error (parser, "expected %<omp_priv%> or "
17991 "function-name");
17992 bad = true;
17994 else if (strcmp (IDENTIFIER_POINTER
17995 (c_parser_peek_token (parser)->value),
17996 "omp_priv") != 0)
17998 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17999 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
18001 c_parser_error (parser, "expected function-name %<(%>");
18002 bad = true;
18004 else
18005 initializer = c_parser_postfix_expression (parser);
18006 if (initializer.value
18007 && TREE_CODE (initializer.value) == CALL_EXPR)
18009 int j;
18010 tree c = initializer.value;
18011 for (j = 0; j < call_expr_nargs (c); j++)
18013 tree a = CALL_EXPR_ARG (c, j);
18014 STRIP_NOPS (a);
18015 if (TREE_CODE (a) == ADDR_EXPR
18016 && TREE_OPERAND (a, 0) == omp_priv)
18017 break;
18019 if (j == call_expr_nargs (c))
18020 error ("one of the initializer call arguments should be "
18021 "%<&omp_priv%>");
18024 else
18026 c_parser_consume_token (parser);
18027 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
18028 bad = true;
18029 else
18031 tree st = push_stmt_list ();
18032 location_t loc = c_parser_peek_token (parser)->location;
18033 rich_location richloc (line_table, loc);
18034 start_init (omp_priv, NULL_TREE, 0, &richloc);
18035 struct c_expr init = c_parser_initializer (parser);
18036 finish_init ();
18037 finish_decl (omp_priv, loc, init.value,
18038 init.original_type, NULL_TREE);
18039 pop_stmt_list (st);
18042 if (!bad
18043 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
18044 bad = true;
18047 if (!bad)
18049 c_parser_skip_to_pragma_eol (parser);
18051 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
18052 DECL_INITIAL (reduc_decl));
18053 DECL_INITIAL (reduc_decl) = t;
18054 DECL_SOURCE_LOCATION (omp_out) = rloc;
18055 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
18056 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
18057 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
18058 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
18059 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
18060 if (omp_priv)
18062 DECL_SOURCE_LOCATION (omp_priv) = rloc;
18063 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
18064 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
18065 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
18066 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
18067 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
18068 walk_tree (&DECL_INITIAL (omp_priv),
18069 c_check_omp_declare_reduction_r,
18070 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
18074 pop_stmt_list (stmt);
18075 pop_scope ();
18076 if (cfun->language != NULL)
18078 ggc_free (cfun->language);
18079 cfun->language = NULL;
18081 set_cfun (NULL);
18082 current_function_decl = NULL_TREE;
18083 if (nested)
18084 c_pop_function_context ();
18086 if (!clauses.is_empty ())
18088 parser->tokens = &parser->tokens_buf[0];
18089 parser->tokens_avail = tokens_avail;
18091 if (bad)
18092 goto fail;
18093 if (errs != errorcount)
18094 break;
18097 clauses.release ();
18098 types.release ();
18102 /* OpenMP 4.0
18103 #pragma omp declare simd declare-simd-clauses[optseq] new-line
18104 #pragma omp declare reduction (reduction-id : typename-list : expression) \
18105 initializer-clause[opt] new-line
18106 #pragma omp declare target new-line */
18108 static void
18109 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
18111 c_parser_consume_pragma (parser);
18112 if (c_parser_next_token_is (parser, CPP_NAME))
18114 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18115 if (strcmp (p, "simd") == 0)
18117 /* c_parser_consume_token (parser); done in
18118 c_parser_omp_declare_simd. */
18119 c_parser_omp_declare_simd (parser, context);
18120 return;
18122 if (strcmp (p, "reduction") == 0)
18124 c_parser_consume_token (parser);
18125 c_parser_omp_declare_reduction (parser, context);
18126 return;
18128 if (!flag_openmp) /* flag_openmp_simd */
18130 c_parser_skip_to_pragma_eol (parser, false);
18131 return;
18133 if (strcmp (p, "target") == 0)
18135 c_parser_consume_token (parser);
18136 c_parser_omp_declare_target (parser);
18137 return;
18141 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
18142 "or %<target%>");
18143 c_parser_skip_to_pragma_eol (parser);
18146 /* OpenMP 4.5:
18147 #pragma omp taskloop taskloop-clause[optseq] new-line
18148 for-loop
18150 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
18151 for-loop */
18153 #define OMP_TASKLOOP_CLAUSE_MASK \
18154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
18155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
18159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
18160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
18161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
18163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
18165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
18166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
18167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
18169 static tree
18170 c_parser_omp_taskloop (location_t loc, c_parser *parser,
18171 char *p_name, omp_clause_mask mask, tree *cclauses,
18172 bool *if_p)
18174 tree clauses, block, ret;
18176 strcat (p_name, " taskloop");
18177 mask |= OMP_TASKLOOP_CLAUSE_MASK;
18179 if (c_parser_next_token_is (parser, CPP_NAME))
18181 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18183 if (strcmp (p, "simd") == 0)
18185 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18186 if (cclauses == NULL)
18187 cclauses = cclauses_buf;
18188 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
18189 c_parser_consume_token (parser);
18190 if (!flag_openmp) /* flag_openmp_simd */
18191 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
18192 if_p);
18193 block = c_begin_compound_stmt (true);
18194 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
18195 block = c_end_compound_stmt (loc, block, true);
18196 if (ret == NULL)
18197 return ret;
18198 ret = make_node (OMP_TASKLOOP);
18199 TREE_TYPE (ret) = void_type_node;
18200 OMP_FOR_BODY (ret) = block;
18201 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18202 SET_EXPR_LOCATION (ret, loc);
18203 add_stmt (ret);
18204 return ret;
18207 if (!flag_openmp) /* flag_openmp_simd */
18209 c_parser_skip_to_pragma_eol (parser, false);
18210 return NULL_TREE;
18213 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18214 if (cclauses)
18216 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
18217 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18220 block = c_begin_compound_stmt (true);
18221 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
18222 block = c_end_compound_stmt (loc, block, true);
18223 add_stmt (block);
18225 return ret;
18228 /* Main entry point to parsing most OpenMP pragmas. */
18230 static void
18231 c_parser_omp_construct (c_parser *parser, bool *if_p)
18233 enum pragma_kind p_kind;
18234 location_t loc;
18235 tree stmt;
18236 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18237 omp_clause_mask mask (0);
18239 loc = c_parser_peek_token (parser)->location;
18240 p_kind = c_parser_peek_token (parser)->pragma_kind;
18241 c_parser_consume_pragma (parser);
18243 switch (p_kind)
18245 case PRAGMA_OACC_ATOMIC:
18246 c_parser_omp_atomic (loc, parser);
18247 return;
18248 case PRAGMA_OACC_CACHE:
18249 strcpy (p_name, "#pragma acc");
18250 stmt = c_parser_oacc_cache (loc, parser);
18251 break;
18252 case PRAGMA_OACC_DATA:
18253 stmt = c_parser_oacc_data (loc, parser, if_p);
18254 break;
18255 case PRAGMA_OACC_HOST_DATA:
18256 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18257 break;
18258 case PRAGMA_OACC_KERNELS:
18259 case PRAGMA_OACC_PARALLEL:
18260 strcpy (p_name, "#pragma acc");
18261 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18262 if_p);
18263 break;
18264 case PRAGMA_OACC_LOOP:
18265 strcpy (p_name, "#pragma acc");
18266 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18267 break;
18268 case PRAGMA_OACC_WAIT:
18269 strcpy (p_name, "#pragma wait");
18270 stmt = c_parser_oacc_wait (loc, parser, p_name);
18271 break;
18272 case PRAGMA_OMP_ATOMIC:
18273 c_parser_omp_atomic (loc, parser);
18274 return;
18275 case PRAGMA_OMP_CRITICAL:
18276 stmt = c_parser_omp_critical (loc, parser, if_p);
18277 break;
18278 case PRAGMA_OMP_DISTRIBUTE:
18279 strcpy (p_name, "#pragma omp");
18280 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18281 break;
18282 case PRAGMA_OMP_FOR:
18283 strcpy (p_name, "#pragma omp");
18284 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18285 break;
18286 case PRAGMA_OMP_MASTER:
18287 stmt = c_parser_omp_master (loc, parser, if_p);
18288 break;
18289 case PRAGMA_OMP_PARALLEL:
18290 strcpy (p_name, "#pragma omp");
18291 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18292 break;
18293 case PRAGMA_OMP_SECTIONS:
18294 strcpy (p_name, "#pragma omp");
18295 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18296 break;
18297 case PRAGMA_OMP_SIMD:
18298 strcpy (p_name, "#pragma omp");
18299 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18300 break;
18301 case PRAGMA_OMP_SINGLE:
18302 stmt = c_parser_omp_single (loc, parser, if_p);
18303 break;
18304 case PRAGMA_OMP_TASK:
18305 stmt = c_parser_omp_task (loc, parser, if_p);
18306 break;
18307 case PRAGMA_OMP_TASKGROUP:
18308 stmt = c_parser_omp_taskgroup (parser, if_p);
18309 break;
18310 case PRAGMA_OMP_TASKLOOP:
18311 strcpy (p_name, "#pragma omp");
18312 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18313 break;
18314 case PRAGMA_OMP_TEAMS:
18315 strcpy (p_name, "#pragma omp");
18316 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18317 break;
18318 default:
18319 gcc_unreachable ();
18322 if (stmt)
18323 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18327 /* OpenMP 2.5:
18328 # pragma omp threadprivate (variable-list) */
18330 static void
18331 c_parser_omp_threadprivate (c_parser *parser)
18333 tree vars, t;
18334 location_t loc;
18336 c_parser_consume_pragma (parser);
18337 loc = c_parser_peek_token (parser)->location;
18338 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18340 /* Mark every variable in VARS to be assigned thread local storage. */
18341 for (t = vars; t; t = TREE_CHAIN (t))
18343 tree v = TREE_PURPOSE (t);
18345 /* FIXME diagnostics: Ideally we should keep individual
18346 locations for all the variables in the var list to make the
18347 following errors more precise. Perhaps
18348 c_parser_omp_var_list_parens() should construct a list of
18349 locations to go along with the var list. */
18351 /* If V had already been marked threadprivate, it doesn't matter
18352 whether it had been used prior to this point. */
18353 if (!VAR_P (v))
18354 error_at (loc, "%qD is not a variable", v);
18355 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18356 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18357 else if (! is_global_var (v))
18358 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18359 else if (TREE_TYPE (v) == error_mark_node)
18361 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18362 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18363 else
18365 if (! DECL_THREAD_LOCAL_P (v))
18367 set_decl_tls_model (v, decl_default_tls_model (v));
18368 /* If rtl has been already set for this var, call
18369 make_decl_rtl once again, so that encode_section_info
18370 has a chance to look at the new decl flags. */
18371 if (DECL_RTL_SET_P (v))
18372 make_decl_rtl (v);
18374 C_DECL_THREADPRIVATE_P (v) = 1;
18378 c_parser_skip_to_pragma_eol (parser);
18381 /* Cilk Plus <#pragma simd> parsing routines. */
18383 /* Helper function for c_parser_pragma. Perform some sanity checking
18384 for <#pragma simd> constructs. Returns FALSE if there was a
18385 problem. */
18387 static bool
18388 c_parser_cilk_verify_simd (c_parser *parser,
18389 enum pragma_context context)
18391 if (!flag_cilkplus)
18393 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
18394 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
18395 return false;
18397 if (context == pragma_external)
18399 c_parser_error (parser,"pragma simd must be inside a function");
18400 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
18401 return false;
18403 return true;
18406 /* Cilk Plus:
18407 This function is shared by SIMD-enabled functions and #pragma simd.
18408 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
18409 CLAUSES is unused. The main purpose of this function is to parse a
18410 vectorlength attribute or clause and check for parse errors.
18411 When IS_SIMD_FN is true then the function is merely caching the tokens
18412 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
18413 cache is cleared since there is no reason to continue.
18414 Syntax:
18415 vectorlength ( constant-expression ) */
18417 static tree
18418 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
18419 bool is_simd_fn)
18421 if (is_simd_fn)
18422 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
18423 else
18424 /* The vectorlength clause behaves exactly like OpenMP's safelen
18425 clause. Represent it in OpenMP terms. */
18426 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
18428 matching_parens parens;
18429 if (!parens.require_open (parser))
18430 return clauses;
18432 location_t loc = c_parser_peek_token (parser)->location;
18433 tree expr = c_parser_expr_no_commas (parser, NULL).value;
18434 expr = c_fully_fold (expr, false, NULL);
18436 /* If expr is an error_mark_node then the above function would have
18437 emitted an error. No reason to do it twice. */
18438 if (expr == error_mark_node)
18440 else if (!TREE_TYPE (expr)
18441 || !TREE_CONSTANT (expr)
18442 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
18444 error_at (loc, "vectorlength must be an integer constant");
18445 else if (wi::exact_log2 (wi::to_wide (expr)) == -1)
18446 error_at (loc, "vectorlength must be a power of 2");
18447 else
18449 if (is_simd_fn)
18451 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
18452 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
18453 OMP_CLAUSE_CHAIN (u) = clauses;
18454 clauses = u;
18456 else
18458 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
18459 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
18460 OMP_CLAUSE_CHAIN (u) = clauses;
18461 clauses = u;
18465 parens.require_close (parser);
18467 return clauses;
18470 /* Cilk Plus:
18471 linear ( simd-linear-variable-list )
18473 simd-linear-variable-list:
18474 simd-linear-variable
18475 simd-linear-variable-list , simd-linear-variable
18477 simd-linear-variable:
18478 id-expression
18479 id-expression : simd-linear-step
18481 simd-linear-step:
18482 conditional-expression */
18484 static tree
18485 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
18487 matching_parens parens;
18488 if (!parens.require_open (parser))
18489 return clauses;
18491 location_t loc = c_parser_peek_token (parser)->location;
18493 if (c_parser_next_token_is_not (parser, CPP_NAME)
18494 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
18495 c_parser_error (parser, "expected identifier");
18497 while (c_parser_next_token_is (parser, CPP_NAME)
18498 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
18500 tree var = lookup_name (c_parser_peek_token (parser)->value);
18502 if (var == NULL)
18504 undeclared_variable (c_parser_peek_token (parser)->location,
18505 c_parser_peek_token (parser)->value);
18506 c_parser_consume_token (parser);
18508 else if (var == error_mark_node)
18509 c_parser_consume_token (parser);
18510 else
18512 tree step = integer_one_node;
18514 /* Parse the linear step if present. */
18515 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
18517 c_parser_consume_token (parser);
18518 c_parser_consume_token (parser);
18520 tree expr = c_parser_expr_no_commas (parser, NULL).value;
18521 expr = c_fully_fold (expr, false, NULL);
18523 if (TREE_TYPE (expr)
18524 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
18525 && (TREE_CONSTANT (expr)
18526 || DECL_P (expr)))
18527 step = expr;
18528 else
18529 c_parser_error (parser,
18530 "step size must be an integer constant "
18531 "expression or an integer variable");
18533 else
18534 c_parser_consume_token (parser);
18536 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
18537 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
18538 OMP_CLAUSE_DECL (u) = var;
18539 OMP_CLAUSE_LINEAR_STEP (u) = step;
18540 OMP_CLAUSE_CHAIN (u) = clauses;
18541 clauses = u;
18544 if (c_parser_next_token_is_not (parser, CPP_COMMA))
18545 break;
18547 c_parser_consume_token (parser);
18550 parens.skip_until_found_close (parser);
18552 return clauses;
18555 /* Returns the name of the next clause. If the clause is not
18556 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
18557 not consumed. Otherwise, the appropriate pragma_simd_clause is
18558 returned and the token is consumed. */
18560 static pragma_omp_clause
18561 c_parser_cilk_clause_name (c_parser *parser)
18563 pragma_omp_clause result;
18564 c_token *token = c_parser_peek_token (parser);
18566 if (!token->value || token->type != CPP_NAME)
18567 return PRAGMA_CILK_CLAUSE_NONE;
18569 const char *p = IDENTIFIER_POINTER (token->value);
18571 if (!strcmp (p, "vectorlength"))
18572 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
18573 else if (!strcmp (p, "linear"))
18574 result = PRAGMA_CILK_CLAUSE_LINEAR;
18575 else if (!strcmp (p, "private"))
18576 result = PRAGMA_CILK_CLAUSE_PRIVATE;
18577 else if (!strcmp (p, "firstprivate"))
18578 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
18579 else if (!strcmp (p, "lastprivate"))
18580 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
18581 else if (!strcmp (p, "reduction"))
18582 result = PRAGMA_CILK_CLAUSE_REDUCTION;
18583 else
18584 return PRAGMA_CILK_CLAUSE_NONE;
18586 c_parser_consume_token (parser);
18587 return result;
18590 /* Parse all #<pragma simd> clauses. Return the list of clauses
18591 found. */
18593 static tree
18594 c_parser_cilk_all_clauses (c_parser *parser)
18596 tree clauses = NULL;
18598 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
18600 pragma_omp_clause c_kind;
18602 c_kind = c_parser_cilk_clause_name (parser);
18604 switch (c_kind)
18606 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
18607 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
18608 break;
18609 case PRAGMA_CILK_CLAUSE_LINEAR:
18610 clauses = c_parser_cilk_clause_linear (parser, clauses);
18611 break;
18612 case PRAGMA_CILK_CLAUSE_PRIVATE:
18613 /* Use the OpenMP counterpart. */
18614 clauses = c_parser_omp_clause_private (parser, clauses);
18615 break;
18616 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
18617 /* Use the OpenMP counterpart. */
18618 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
18619 break;
18620 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
18621 /* Use the OpenMP counterpart. */
18622 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
18623 break;
18624 case PRAGMA_CILK_CLAUSE_REDUCTION:
18625 /* Use the OpenMP counterpart. */
18626 clauses = c_parser_omp_clause_reduction (parser, clauses);
18627 break;
18628 default:
18629 c_parser_error (parser, "expected %<#pragma simd%> clause");
18630 goto saw_error;
18634 saw_error:
18635 c_parser_skip_to_pragma_eol (parser);
18636 return c_finish_omp_clauses (clauses, C_ORT_CILK);
18639 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
18640 Here is the correct syntax of this pragma:
18641 #pragma cilk grainsize = <EXP>
18644 static void
18645 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
18647 extern tree convert_to_integer (tree, tree);
18649 /* consume the 'grainsize' keyword. */
18650 c_parser_consume_pragma (parser);
18652 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
18654 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
18655 if (g_expr.value == error_mark_node)
18657 c_parser_skip_to_pragma_eol (parser);
18658 return;
18660 tree grain = convert_to_integer (long_integer_type_node,
18661 c_fully_fold (g_expr.value, false,
18662 NULL));
18663 c_parser_skip_to_pragma_eol (parser);
18664 c_token *token = c_parser_peek_token (parser);
18665 if (token && token->type == CPP_KEYWORD
18666 && token->keyword == RID_CILK_FOR)
18668 if (grain == NULL_TREE || grain == error_mark_node)
18669 grain = integer_zero_node;
18670 c_parser_cilk_for (parser, grain, if_p);
18672 else
18673 warning (0, "%<#pragma cilk grainsize%> is not followed by "
18674 "%<_Cilk_for%>");
18676 else
18677 c_parser_skip_to_pragma_eol (parser);
18680 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
18682 static void
18683 c_parser_cilk_simd (c_parser *parser, bool *if_p)
18685 tree clauses = c_parser_cilk_all_clauses (parser);
18686 tree block = c_begin_compound_stmt (true);
18687 location_t loc = c_parser_peek_token (parser)->location;
18688 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
18689 block = c_end_compound_stmt (loc, block, true);
18690 add_stmt (block);
18693 /* Create an artificial decl with TYPE and emit initialization of it with
18694 INIT. */
18696 static tree
18697 c_get_temp_regvar (tree type, tree init)
18699 location_t loc = EXPR_LOCATION (init);
18700 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
18701 DECL_ARTIFICIAL (decl) = 1;
18702 DECL_IGNORED_P (decl) = 1;
18703 pushdecl (decl);
18704 tree t = build2 (INIT_EXPR, type, decl, init);
18705 add_stmt (t);
18706 return decl;
18709 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
18710 GRAIN is the grain value passed in through pragma or 0. */
18712 static void
18713 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
18715 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
18716 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
18717 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
18718 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
18720 tree block = c_begin_compound_stmt (true);
18721 tree sb = push_stmt_list ();
18722 location_t loc = c_parser_peek_token (parser)->location;
18723 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
18724 if_p);
18725 sb = pop_stmt_list (sb);
18727 if (omp_for)
18729 tree omp_par = make_node (OMP_PARALLEL);
18730 TREE_TYPE (omp_par) = void_type_node;
18731 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
18732 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
18733 TREE_SIDE_EFFECTS (bind) = 1;
18734 BIND_EXPR_BODY (bind) = sb;
18735 OMP_PARALLEL_BODY (omp_par) = bind;
18736 if (OMP_FOR_PRE_BODY (omp_for))
18738 add_stmt (OMP_FOR_PRE_BODY (omp_for));
18739 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
18741 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
18742 tree decl = TREE_OPERAND (init, 0);
18743 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
18744 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
18745 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
18746 if (TREE_CODE (t) != INTEGER_CST)
18748 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
18749 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18750 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
18751 OMP_CLAUSE_CHAIN (c) = clauses;
18752 clauses = c;
18754 if (TREE_CODE (incr) == MODIFY_EXPR)
18756 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
18757 if (TREE_CODE (t) != INTEGER_CST)
18759 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
18760 = c_get_temp_regvar (TREE_TYPE (t), t);
18761 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18762 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
18763 OMP_CLAUSE_CHAIN (c) = clauses;
18764 clauses = c;
18767 t = TREE_OPERAND (init, 1);
18768 if (TREE_CODE (t) != INTEGER_CST)
18770 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
18771 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
18772 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
18773 OMP_CLAUSE_CHAIN (c) = clauses;
18774 clauses = c;
18776 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
18777 OMP_CLAUSE_DECL (c) = decl;
18778 OMP_CLAUSE_CHAIN (c) = clauses;
18779 clauses = c;
18780 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
18781 OMP_CLAUSE_OPERAND (c, 0)
18782 = cilk_for_number_of_iterations (omp_for);
18783 OMP_CLAUSE_CHAIN (c) = clauses;
18784 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
18785 add_stmt (omp_par);
18788 block = c_end_compound_stmt (loc, block, true);
18789 add_stmt (block);
18793 /* Parse a transaction attribute (GCC Extension).
18795 transaction-attribute:
18796 attributes
18797 [ [ any-word ] ]
18799 The transactional memory language description is written for C++,
18800 and uses the C++0x attribute syntax. For compatibility, allow the
18801 bracket style for transactions in C as well. */
18803 static tree
18804 c_parser_transaction_attributes (c_parser *parser)
18806 tree attr_name, attr = NULL;
18808 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18809 return c_parser_attributes (parser);
18811 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18812 return NULL_TREE;
18813 c_parser_consume_token (parser);
18814 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18815 goto error1;
18817 attr_name = c_parser_attribute_any_word (parser);
18818 if (attr_name)
18820 c_parser_consume_token (parser);
18821 attr = build_tree_list (attr_name, NULL_TREE);
18823 else
18824 c_parser_error (parser, "expected identifier");
18826 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18827 error1:
18828 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18829 return attr;
18832 /* Parse a __transaction_atomic or __transaction_relaxed statement
18833 (GCC Extension).
18835 transaction-statement:
18836 __transaction_atomic transaction-attribute[opt] compound-statement
18837 __transaction_relaxed compound-statement
18839 Note that the only valid attribute is: "outer".
18842 static tree
18843 c_parser_transaction (c_parser *parser, enum rid keyword)
18845 unsigned int old_in = parser->in_transaction;
18846 unsigned int this_in = 1, new_in;
18847 location_t loc = c_parser_peek_token (parser)->location;
18848 tree stmt, attrs;
18850 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18851 || keyword == RID_TRANSACTION_RELAXED)
18852 && c_parser_next_token_is_keyword (parser, keyword));
18853 c_parser_consume_token (parser);
18855 if (keyword == RID_TRANSACTION_RELAXED)
18856 this_in |= TM_STMT_ATTR_RELAXED;
18857 else
18859 attrs = c_parser_transaction_attributes (parser);
18860 if (attrs)
18861 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18864 /* Keep track if we're in the lexical scope of an outer transaction. */
18865 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18867 parser->in_transaction = new_in;
18868 stmt = c_parser_compound_statement (parser);
18869 parser->in_transaction = old_in;
18871 if (flag_tm)
18872 stmt = c_finish_transaction (loc, stmt, this_in);
18873 else
18874 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18875 "%<__transaction_atomic%> without transactional memory support enabled"
18876 : "%<__transaction_relaxed %> "
18877 "without transactional memory support enabled"));
18879 return stmt;
18882 /* Parse a __transaction_atomic or __transaction_relaxed expression
18883 (GCC Extension).
18885 transaction-expression:
18886 __transaction_atomic ( expression )
18887 __transaction_relaxed ( expression )
18890 static struct c_expr
18891 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18893 struct c_expr ret;
18894 unsigned int old_in = parser->in_transaction;
18895 unsigned int this_in = 1;
18896 location_t loc = c_parser_peek_token (parser)->location;
18897 tree attrs;
18899 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18900 || keyword == RID_TRANSACTION_RELAXED)
18901 && c_parser_next_token_is_keyword (parser, keyword));
18902 c_parser_consume_token (parser);
18904 if (keyword == RID_TRANSACTION_RELAXED)
18905 this_in |= TM_STMT_ATTR_RELAXED;
18906 else
18908 attrs = c_parser_transaction_attributes (parser);
18909 if (attrs)
18910 this_in |= parse_tm_stmt_attr (attrs, 0);
18913 parser->in_transaction = this_in;
18914 matching_parens parens;
18915 if (parens.require_open (parser))
18917 tree expr = c_parser_expression (parser).value;
18918 ret.original_type = TREE_TYPE (expr);
18919 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18920 if (this_in & TM_STMT_ATTR_RELAXED)
18921 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18922 SET_EXPR_LOCATION (ret.value, loc);
18923 ret.original_code = TRANSACTION_EXPR;
18924 if (!parens.require_close (parser))
18926 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18927 goto error;
18930 else
18932 error:
18933 ret.value = error_mark_node;
18934 ret.original_code = ERROR_MARK;
18935 ret.original_type = NULL;
18937 parser->in_transaction = old_in;
18939 if (!flag_tm)
18940 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18941 "%<__transaction_atomic%> without transactional memory support enabled"
18942 : "%<__transaction_relaxed %> "
18943 "without transactional memory support enabled"));
18945 set_c_expr_source_range (&ret, loc, loc);
18947 return ret;
18950 /* Parse a __transaction_cancel statement (GCC Extension).
18952 transaction-cancel-statement:
18953 __transaction_cancel transaction-attribute[opt] ;
18955 Note that the only valid attribute is "outer".
18958 static tree
18959 c_parser_transaction_cancel (c_parser *parser)
18961 location_t loc = c_parser_peek_token (parser)->location;
18962 tree attrs;
18963 bool is_outer = false;
18965 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18966 c_parser_consume_token (parser);
18968 attrs = c_parser_transaction_attributes (parser);
18969 if (attrs)
18970 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18972 if (!flag_tm)
18974 error_at (loc, "%<__transaction_cancel%> without "
18975 "transactional memory support enabled");
18976 goto ret_error;
18978 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18980 error_at (loc, "%<__transaction_cancel%> within a "
18981 "%<__transaction_relaxed%>");
18982 goto ret_error;
18984 else if (is_outer)
18986 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18987 && !is_tm_may_cancel_outer (current_function_decl))
18989 error_at (loc, "outer %<__transaction_cancel%> not "
18990 "within outer %<__transaction_atomic%>");
18991 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18992 goto ret_error;
18995 else if (parser->in_transaction == 0)
18997 error_at (loc, "%<__transaction_cancel%> not within "
18998 "%<__transaction_atomic%>");
18999 goto ret_error;
19002 return add_stmt (build_tm_abort_call (loc, is_outer));
19004 ret_error:
19005 return build1 (NOP_EXPR, void_type_node, error_mark_node);
19008 /* Parse a single source file. */
19010 void
19011 c_parse_file (void)
19013 /* Use local storage to begin. If the first token is a pragma, parse it.
19014 If it is #pragma GCC pch_preprocess, then this will load a PCH file
19015 which will cause garbage collection. */
19016 c_parser tparser;
19018 memset (&tparser, 0, sizeof tparser);
19019 tparser.tokens = &tparser.tokens_buf[0];
19020 the_parser = &tparser;
19022 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
19023 c_parser_pragma_pch_preprocess (&tparser);
19025 the_parser = ggc_alloc<c_parser> ();
19026 *the_parser = tparser;
19027 if (tparser.tokens == &tparser.tokens_buf[0])
19028 the_parser->tokens = &the_parser->tokens_buf[0];
19030 /* Initialize EH, if we've been told to do so. */
19031 if (flag_exceptions)
19032 using_eh_for_cleanups ();
19034 c_parser_translation_unit (the_parser);
19035 the_parser = NULL;
19038 /* This function parses Cilk Plus array notation. The starting index is
19039 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
19040 return value of this function is a tree_node called VALUE_TREE of type
19041 ARRAY_NOTATION_REF. */
19043 static tree
19044 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
19045 tree array_value)
19047 c_token *token = NULL;
19048 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
19049 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
19050 tree array_type_domain = NULL_TREE;
19052 if (array_value == error_mark_node || initial_index == error_mark_node)
19054 /* No need to continue. If either of these 2 were true, then an error
19055 must be emitted already. Thus, no need to emit them twice. */
19056 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19057 return error_mark_node;
19060 array_type = TREE_TYPE (array_value);
19061 gcc_assert (array_type);
19062 if (TREE_CODE (array_type) != ARRAY_TYPE
19063 && TREE_CODE (array_type) != POINTER_TYPE)
19065 error_at (loc, "base of array section must be pointer or array type");
19066 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19067 return error_mark_node;
19069 type = TREE_TYPE (array_type);
19070 token = c_parser_peek_token (parser);
19072 if (token->type == CPP_EOF)
19074 c_parser_error (parser, "expected %<:%> or numeral");
19075 return value_tree;
19077 else if (token->type == CPP_COLON)
19079 if (!initial_index)
19081 /* If we are here, then we have a case like this A[:]. */
19082 c_parser_consume_token (parser);
19083 if (TREE_CODE (array_type) == POINTER_TYPE)
19085 error_at (loc, "start-index and length fields necessary for "
19086 "using array notations in pointers");
19087 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19088 return error_mark_node;
19090 if (TREE_CODE (array_type) == FUNCTION_TYPE)
19092 error_at (loc, "array notations cannot be used with function "
19093 "type");
19094 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19095 return error_mark_node;
19097 array_type_domain = TYPE_DOMAIN (array_type);
19099 if (!array_type_domain)
19101 error_at (loc, "start-index and length fields necessary for "
19102 "using array notations in dimensionless arrays");
19103 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19104 return error_mark_node;
19107 start_index = TYPE_MIN_VALUE (array_type_domain);
19108 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
19109 start_index);
19110 if (!TYPE_MAX_VALUE (array_type_domain)
19111 || !TREE_CONSTANT (TYPE_MAX_VALUE (array_type_domain)))
19113 error_at (loc, "start-index and length fields necessary for "
19114 "using array notations in variable-length arrays");
19115 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19116 return error_mark_node;
19118 end_index = TYPE_MAX_VALUE (array_type_domain);
19119 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
19120 end_index, integer_one_node);
19121 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
19122 stride = build_int_cst (integer_type_node, 1);
19123 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
19125 else if (initial_index != error_mark_node)
19127 /* If we are here, then there should be 2 possibilities:
19128 1. Array [EXPR : EXPR]
19129 2. Array [EXPR : EXPR : EXPR]
19131 start_index = initial_index;
19133 if (TREE_CODE (array_type) == FUNCTION_TYPE)
19135 error_at (loc, "array notations cannot be used with function "
19136 "type");
19137 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
19138 return error_mark_node;
19140 c_parser_consume_token (parser); /* consume the ':' */
19141 struct c_expr ce = c_parser_expression (parser);
19142 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
19143 end_index = ce.value;
19144 if (!end_index || end_index == error_mark_node)
19146 c_parser_skip_to_end_of_block_or_statement (parser);
19147 return error_mark_node;
19149 if (c_parser_peek_token (parser)->type == CPP_COLON)
19151 c_parser_consume_token (parser);
19152 ce = c_parser_expression (parser);
19153 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
19154 stride = ce.value;
19155 if (!stride || stride == error_mark_node)
19157 c_parser_skip_to_end_of_block_or_statement (parser);
19158 return error_mark_node;
19162 else
19163 c_parser_error (parser, "expected array notation expression");
19165 else
19166 c_parser_error (parser, "expected array notation expression");
19168 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
19170 value_tree = build_array_notation_ref (loc, array_value, start_index,
19171 end_index, stride, type);
19172 if (value_tree != error_mark_node)
19173 SET_EXPR_LOCATION (value_tree, loc);
19174 return value_tree;
19177 /* Parse the body of a function declaration marked with "__RTL".
19179 The RTL parser works on the level of characters read from a
19180 FILE *, whereas c_parser works at the level of tokens.
19181 Square this circle by consuming all of the tokens up to and
19182 including the closing brace, recording the start/end of the RTL
19183 fragment, and reopening the file and re-reading the relevant
19184 lines within the RTL parser.
19186 This requires the opening and closing braces of the C function
19187 to be on separate lines from the RTL they wrap.
19189 Take ownership of START_WITH_PASS, if non-NULL. */
19191 void
19192 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
19194 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
19196 free (start_with_pass);
19197 return;
19200 location_t start_loc = c_parser_peek_token (parser)->location;
19202 /* Consume all tokens, up to the closing brace, handling
19203 matching pairs of braces in the rtl dump. */
19204 int num_open_braces = 1;
19205 while (1)
19207 switch (c_parser_peek_token (parser)->type)
19209 case CPP_OPEN_BRACE:
19210 num_open_braces++;
19211 break;
19212 case CPP_CLOSE_BRACE:
19213 if (--num_open_braces == 0)
19214 goto found_closing_brace;
19215 break;
19216 case CPP_EOF:
19217 error_at (start_loc, "no closing brace");
19218 free (start_with_pass);
19219 return;
19220 default:
19221 break;
19223 c_parser_consume_token (parser);
19226 found_closing_brace:
19227 /* At the closing brace; record its location. */
19228 location_t end_loc = c_parser_peek_token (parser)->location;
19230 /* Consume the closing brace. */
19231 c_parser_consume_token (parser);
19233 /* Invoke the RTL parser. */
19234 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
19236 free (start_with_pass);
19237 return;
19240 /* If a pass name was provided for START_WITH_PASS, run the backend
19241 accordingly now, on the cfun created above, transferring
19242 ownership of START_WITH_PASS. */
19243 if (start_with_pass)
19244 run_rtl_passes (start_with_pass);
19247 #include "gt-c-c-parser.h"