2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / c / c-parser.c
blobfa4e950c94f9aef65717ab5b0eb36e336a87426f
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;
211 /* Return a pointer to the Nth token in PARSERs tokens_buf. */
213 c_token *
214 c_parser_tokens_buf (c_parser *parser, unsigned n)
216 return &parser->tokens_buf[n];
219 /* Return the error state of PARSER. */
221 bool
222 c_parser_error (c_parser *parser)
224 return parser->error;
227 /* Set the error state of PARSER to ERR. */
229 void
230 c_parser_set_error (c_parser *parser, bool err)
232 parser->error = err;
236 /* The actual parser and external interface. ??? Does this need to be
237 garbage-collected? */
239 static GTY (()) c_parser *the_parser;
241 /* Read in and lex a single token, storing it in *TOKEN. */
243 static void
244 c_lex_one_token (c_parser *parser, c_token *token)
246 timevar_push (TV_LEX);
248 token->type = c_lex_with_flags (&token->value, &token->location,
249 &token->flags,
250 (parser->lex_untranslated_string
251 ? C_LEX_STRING_NO_TRANSLATE : 0));
252 token->id_kind = C_ID_NONE;
253 token->keyword = RID_MAX;
254 token->pragma_kind = PRAGMA_NONE;
256 switch (token->type)
258 case CPP_NAME:
260 tree decl;
262 bool objc_force_identifier = parser->objc_need_raw_identifier;
263 if (c_dialect_objc ())
264 parser->objc_need_raw_identifier = false;
266 if (C_IS_RESERVED_WORD (token->value))
268 enum rid rid_code = C_RID_CODE (token->value);
270 if (rid_code == RID_CXX_COMPAT_WARN)
272 warning_at (token->location,
273 OPT_Wc___compat,
274 "identifier %qE conflicts with C++ keyword",
275 token->value);
277 else if (rid_code >= RID_FIRST_ADDR_SPACE
278 && rid_code <= RID_LAST_ADDR_SPACE)
280 addr_space_t as;
281 as = (addr_space_t) (rid_code - RID_FIRST_ADDR_SPACE);
282 targetm.addr_space.diagnose_usage (as, token->location);
283 token->id_kind = C_ID_ADDRSPACE;
284 token->keyword = rid_code;
285 break;
287 else if (c_dialect_objc () && OBJC_IS_PQ_KEYWORD (rid_code))
289 /* We found an Objective-C "pq" keyword (in, out,
290 inout, bycopy, byref, oneway). They need special
291 care because the interpretation depends on the
292 context. */
293 if (parser->objc_pq_context)
295 token->type = CPP_KEYWORD;
296 token->keyword = rid_code;
297 break;
299 else if (parser->objc_could_be_foreach_context
300 && rid_code == RID_IN)
302 /* We are in Objective-C, inside a (potential)
303 foreach context (which means after having
304 parsed 'for (', but before having parsed ';'),
305 and we found 'in'. We consider it the keyword
306 which terminates the declaration at the
307 beginning of a foreach-statement. Note that
308 this means you can't use 'in' for anything else
309 in that context; in particular, in Objective-C
310 you can't use 'in' as the name of the running
311 variable in a C for loop. We could potentially
312 try to add code here to disambiguate, but it
313 seems a reasonable limitation. */
314 token->type = CPP_KEYWORD;
315 token->keyword = rid_code;
316 break;
318 /* Else, "pq" keywords outside of the "pq" context are
319 not keywords, and we fall through to the code for
320 normal tokens. */
322 else if (c_dialect_objc () && OBJC_IS_PATTR_KEYWORD (rid_code))
324 /* We found an Objective-C "property attribute"
325 keyword (getter, setter, readonly, etc). These are
326 only valid in the property context. */
327 if (parser->objc_property_attr_context)
329 token->type = CPP_KEYWORD;
330 token->keyword = rid_code;
331 break;
333 /* Else they are not special keywords.
336 else if (c_dialect_objc ()
337 && (OBJC_IS_AT_KEYWORD (rid_code)
338 || OBJC_IS_CXX_KEYWORD (rid_code)))
340 /* We found one of the Objective-C "@" keywords (defs,
341 selector, synchronized, etc) or one of the
342 Objective-C "cxx" keywords (class, private,
343 protected, public, try, catch, throw) without a
344 preceding '@' sign. Do nothing and fall through to
345 the code for normal tokens (in C++ we would still
346 consider the CXX ones keywords, but not in C). */
349 else
351 token->type = CPP_KEYWORD;
352 token->keyword = rid_code;
353 break;
357 decl = lookup_name (token->value);
358 if (decl)
360 if (TREE_CODE (decl) == TYPE_DECL)
362 token->id_kind = C_ID_TYPENAME;
363 break;
366 else if (c_dialect_objc ())
368 tree objc_interface_decl = objc_is_class_name (token->value);
369 /* Objective-C class names are in the same namespace as
370 variables and typedefs, and hence are shadowed by local
371 declarations. */
372 if (objc_interface_decl
373 && (!objc_force_identifier || global_bindings_p ()))
375 token->value = objc_interface_decl;
376 token->id_kind = C_ID_CLASSNAME;
377 break;
380 token->id_kind = C_ID_ID;
382 break;
383 case CPP_AT_NAME:
384 /* This only happens in Objective-C; it must be a keyword. */
385 token->type = CPP_KEYWORD;
386 switch (C_RID_CODE (token->value))
388 /* Replace 'class' with '@class', 'private' with '@private',
389 etc. This prevents confusion with the C++ keyword
390 'class', and makes the tokens consistent with other
391 Objective-C 'AT' keywords. For example '@class' is
392 reported as RID_AT_CLASS which is consistent with
393 '@synchronized', which is reported as
394 RID_AT_SYNCHRONIZED.
396 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
397 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
398 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
399 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
400 case RID_THROW: token->keyword = RID_AT_THROW; break;
401 case RID_TRY: token->keyword = RID_AT_TRY; break;
402 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
403 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
404 default: token->keyword = C_RID_CODE (token->value);
406 break;
407 case CPP_COLON:
408 case CPP_COMMA:
409 case CPP_CLOSE_PAREN:
410 case CPP_SEMICOLON:
411 /* These tokens may affect the interpretation of any identifiers
412 following, if doing Objective-C. */
413 if (c_dialect_objc ())
414 parser->objc_need_raw_identifier = false;
415 break;
416 case CPP_PRAGMA:
417 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
418 token->pragma_kind = (enum pragma_kind) TREE_INT_CST_LOW (token->value);
419 token->value = NULL;
420 break;
421 default:
422 break;
424 timevar_pop (TV_LEX);
427 /* Return a pointer to the next token from PARSER, reading it in if
428 necessary. */
430 c_token *
431 c_parser_peek_token (c_parser *parser)
433 if (parser->tokens_avail == 0)
435 c_lex_one_token (parser, &parser->tokens[0]);
436 parser->tokens_avail = 1;
438 return &parser->tokens[0];
441 /* Return a pointer to the next-but-one token from PARSER, reading it
442 in if necessary. The next token is already read in. */
444 c_token *
445 c_parser_peek_2nd_token (c_parser *parser)
447 if (parser->tokens_avail >= 2)
448 return &parser->tokens[1];
449 gcc_assert (parser->tokens_avail == 1);
450 gcc_assert (parser->tokens[0].type != CPP_EOF);
451 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL);
452 c_lex_one_token (parser, &parser->tokens[1]);
453 parser->tokens_avail = 2;
454 return &parser->tokens[1];
457 /* Return a pointer to the Nth token from PARSER, reading it
458 in if necessary. The N-1th token is already read in. */
460 c_token *
461 c_parser_peek_nth_token (c_parser *parser, unsigned int n)
463 /* N is 1-based, not zero-based. */
464 gcc_assert (n > 0);
466 if (parser->tokens_avail >= n)
467 return &parser->tokens[n - 1];
468 gcc_assert (parser->tokens_avail == n - 1);
469 c_lex_one_token (parser, &parser->tokens[n - 1]);
470 parser->tokens_avail = n;
471 return &parser->tokens[n - 1];
474 bool
475 c_keyword_starts_typename (enum rid keyword)
477 switch (keyword)
479 case RID_UNSIGNED:
480 case RID_LONG:
481 case RID_SHORT:
482 case RID_SIGNED:
483 case RID_COMPLEX:
484 case RID_INT:
485 case RID_CHAR:
486 case RID_FLOAT:
487 case RID_DOUBLE:
488 case RID_VOID:
489 case RID_DFLOAT32:
490 case RID_DFLOAT64:
491 case RID_DFLOAT128:
492 CASE_RID_FLOATN_NX:
493 case RID_BOOL:
494 case RID_ENUM:
495 case RID_STRUCT:
496 case RID_UNION:
497 case RID_TYPEOF:
498 case RID_CONST:
499 case RID_ATOMIC:
500 case RID_VOLATILE:
501 case RID_RESTRICT:
502 case RID_ATTRIBUTE:
503 case RID_FRACT:
504 case RID_ACCUM:
505 case RID_SAT:
506 case RID_AUTO_TYPE:
507 return true;
508 default:
509 if (keyword >= RID_FIRST_INT_N
510 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
511 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
512 return true;
513 return false;
517 /* Return true if TOKEN can start a type name,
518 false otherwise. */
519 bool
520 c_token_starts_typename (c_token *token)
522 switch (token->type)
524 case CPP_NAME:
525 switch (token->id_kind)
527 case C_ID_ID:
528 return false;
529 case C_ID_ADDRSPACE:
530 return true;
531 case C_ID_TYPENAME:
532 return true;
533 case C_ID_CLASSNAME:
534 gcc_assert (c_dialect_objc ());
535 return true;
536 default:
537 gcc_unreachable ();
539 case CPP_KEYWORD:
540 return c_keyword_starts_typename (token->keyword);
541 case CPP_LESS:
542 if (c_dialect_objc ())
543 return true;
544 return false;
545 default:
546 return false;
550 /* Return true if the next token from PARSER can start a type name,
551 false otherwise. LA specifies how to do lookahead in order to
552 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
554 static inline bool
555 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
557 c_token *token = c_parser_peek_token (parser);
558 if (c_token_starts_typename (token))
559 return true;
561 /* Try a bit harder to detect an unknown typename. */
562 if (la != cla_prefer_id
563 && token->type == CPP_NAME
564 && token->id_kind == C_ID_ID
566 /* Do not try too hard when we could have "object in array". */
567 && !parser->objc_could_be_foreach_context
569 && (la == cla_prefer_type
570 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
571 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
573 /* Only unknown identifiers. */
574 && !lookup_name (token->value))
575 return true;
577 return false;
580 /* Return true if TOKEN is a type qualifier, false otherwise. */
581 static bool
582 c_token_is_qualifier (c_token *token)
584 switch (token->type)
586 case CPP_NAME:
587 switch (token->id_kind)
589 case C_ID_ADDRSPACE:
590 return true;
591 default:
592 return false;
594 case CPP_KEYWORD:
595 switch (token->keyword)
597 case RID_CONST:
598 case RID_VOLATILE:
599 case RID_RESTRICT:
600 case RID_ATTRIBUTE:
601 case RID_ATOMIC:
602 return true;
603 default:
604 return false;
606 case CPP_LESS:
607 return false;
608 default:
609 gcc_unreachable ();
613 /* Return true if the next token from PARSER is a type qualifier,
614 false otherwise. */
615 static inline bool
616 c_parser_next_token_is_qualifier (c_parser *parser)
618 c_token *token = c_parser_peek_token (parser);
619 return c_token_is_qualifier (token);
622 /* Return true if TOKEN can start declaration specifiers, false
623 otherwise. */
624 static bool
625 c_token_starts_declspecs (c_token *token)
627 switch (token->type)
629 case CPP_NAME:
630 switch (token->id_kind)
632 case C_ID_ID:
633 return false;
634 case C_ID_ADDRSPACE:
635 return true;
636 case C_ID_TYPENAME:
637 return true;
638 case C_ID_CLASSNAME:
639 gcc_assert (c_dialect_objc ());
640 return true;
641 default:
642 gcc_unreachable ();
644 case CPP_KEYWORD:
645 switch (token->keyword)
647 case RID_STATIC:
648 case RID_EXTERN:
649 case RID_REGISTER:
650 case RID_TYPEDEF:
651 case RID_INLINE:
652 case RID_NORETURN:
653 case RID_AUTO:
654 case RID_THREAD:
655 case RID_UNSIGNED:
656 case RID_LONG:
657 case RID_SHORT:
658 case RID_SIGNED:
659 case RID_COMPLEX:
660 case RID_INT:
661 case RID_CHAR:
662 case RID_FLOAT:
663 case RID_DOUBLE:
664 case RID_VOID:
665 case RID_DFLOAT32:
666 case RID_DFLOAT64:
667 case RID_DFLOAT128:
668 CASE_RID_FLOATN_NX:
669 case RID_BOOL:
670 case RID_ENUM:
671 case RID_STRUCT:
672 case RID_UNION:
673 case RID_TYPEOF:
674 case RID_CONST:
675 case RID_VOLATILE:
676 case RID_RESTRICT:
677 case RID_ATTRIBUTE:
678 case RID_FRACT:
679 case RID_ACCUM:
680 case RID_SAT:
681 case RID_ALIGNAS:
682 case RID_ATOMIC:
683 case RID_AUTO_TYPE:
684 return true;
685 default:
686 if (token->keyword >= RID_FIRST_INT_N
687 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
688 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
689 return true;
690 return false;
692 case CPP_LESS:
693 if (c_dialect_objc ())
694 return true;
695 return false;
696 default:
697 return false;
702 /* Return true if TOKEN can start declaration specifiers or a static
703 assertion, false otherwise. */
704 static bool
705 c_token_starts_declaration (c_token *token)
707 if (c_token_starts_declspecs (token)
708 || token->keyword == RID_STATIC_ASSERT)
709 return true;
710 else
711 return false;
714 /* Return true if the next token from PARSER can start declaration
715 specifiers, false otherwise. */
716 bool
717 c_parser_next_token_starts_declspecs (c_parser *parser)
719 c_token *token = c_parser_peek_token (parser);
721 /* In Objective-C, a classname normally starts a declspecs unless it
722 is immediately followed by a dot. In that case, it is the
723 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
724 setter/getter on the class. c_token_starts_declspecs() can't
725 differentiate between the two cases because it only checks the
726 current token, so we have a special check here. */
727 if (c_dialect_objc ()
728 && token->type == CPP_NAME
729 && token->id_kind == C_ID_CLASSNAME
730 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
731 return false;
733 return c_token_starts_declspecs (token);
736 /* Return true if the next tokens from PARSER can start declaration
737 specifiers or a static assertion, false otherwise. */
738 bool
739 c_parser_next_tokens_start_declaration (c_parser *parser)
741 c_token *token = c_parser_peek_token (parser);
743 /* Same as above. */
744 if (c_dialect_objc ()
745 && token->type == CPP_NAME
746 && token->id_kind == C_ID_CLASSNAME
747 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
748 return false;
750 /* Labels do not start declarations. */
751 if (token->type == CPP_NAME
752 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
753 return false;
755 if (c_token_starts_declaration (token))
756 return true;
758 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
759 return true;
761 return false;
764 /* Consume the next token from PARSER. */
766 void
767 c_parser_consume_token (c_parser *parser)
769 gcc_assert (parser->tokens_avail >= 1);
770 gcc_assert (parser->tokens[0].type != CPP_EOF);
771 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
772 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
773 if (parser->tokens != &parser->tokens_buf[0])
774 parser->tokens++;
775 else if (parser->tokens_avail == 2)
776 parser->tokens[0] = parser->tokens[1];
777 parser->tokens_avail--;
780 /* Expect the current token to be a #pragma. Consume it and remember
781 that we've begun parsing a pragma. */
783 static void
784 c_parser_consume_pragma (c_parser *parser)
786 gcc_assert (!parser->in_pragma);
787 gcc_assert (parser->tokens_avail >= 1);
788 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
789 if (parser->tokens != &parser->tokens_buf[0])
790 parser->tokens++;
791 else if (parser->tokens_avail == 2)
792 parser->tokens[0] = parser->tokens[1];
793 parser->tokens_avail--;
794 parser->in_pragma = true;
797 /* Update the global input_location from TOKEN. */
798 static inline void
799 c_parser_set_source_position_from_token (c_token *token)
801 if (token->type != CPP_EOF)
803 input_location = token->location;
807 /* Helper function for c_parser_error.
808 Having peeked a token of kind TOK1_KIND that might signify
809 a conflict marker, peek successor tokens to determine
810 if we actually do have a conflict marker.
811 Specifically, we consider a run of 7 '<', '=' or '>' characters
812 at the start of a line as a conflict marker.
813 These come through the lexer as three pairs and a single,
814 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
815 If it returns true, *OUT_LOC is written to with the location/range
816 of the marker. */
818 static bool
819 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
820 location_t *out_loc)
822 c_token *token2 = c_parser_peek_2nd_token (parser);
823 if (token2->type != tok1_kind)
824 return false;
825 c_token *token3 = c_parser_peek_nth_token (parser, 3);
826 if (token3->type != tok1_kind)
827 return false;
828 c_token *token4 = c_parser_peek_nth_token (parser, 4);
829 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
830 return false;
832 /* It must be at the start of the line. */
833 location_t start_loc = c_parser_peek_token (parser)->location;
834 if (LOCATION_COLUMN (start_loc) != 1)
835 return false;
837 /* We have a conflict marker. Construct a location of the form:
838 <<<<<<<
839 ^~~~~~~
840 with start == caret, finishing at the end of the marker. */
841 location_t finish_loc = get_finish (token4->location);
842 *out_loc = make_location (start_loc, start_loc, finish_loc);
844 return true;
847 /* Issue a diagnostic of the form
848 FILE:LINE: MESSAGE before TOKEN
849 where TOKEN is the next token in the input stream of PARSER.
850 MESSAGE (specified by the caller) is usually of the form "expected
851 OTHER-TOKEN".
853 Do not issue a diagnostic if still recovering from an error.
855 ??? This is taken from the C++ parser, but building up messages in
856 this way is not i18n-friendly and some other approach should be
857 used. */
859 void
860 c_parser_error (c_parser *parser, const char *gmsgid)
862 c_token *token = c_parser_peek_token (parser);
863 if (parser->error)
864 return;
865 parser->error = true;
866 if (!gmsgid)
867 return;
869 /* If this is actually a conflict marker, report it as such. */
870 if (token->type == CPP_LSHIFT
871 || token->type == CPP_RSHIFT
872 || token->type == CPP_EQ_EQ)
874 location_t loc;
875 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
877 error_at (loc, "version control conflict marker in file");
878 return;
882 /* This diagnostic makes more sense if it is tagged to the line of
883 the token we just peeked at. */
884 c_parser_set_source_position_from_token (token);
885 c_parse_error (gmsgid,
886 /* Because c_parse_error does not understand
887 CPP_KEYWORD, keywords are treated like
888 identifiers. */
889 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
890 /* ??? The C parser does not save the cpp flags of a
891 token, we need to pass 0 here and we will not get
892 the source spelling of some tokens but rather the
893 canonical spelling. */
894 token->value, /*flags=*/0);
897 /* If the next token is of the indicated TYPE, consume it. Otherwise,
898 issue the error MSGID. If MSGID is NULL then a message has already
899 been produced and no message will be produced this time. Returns
900 true if found, false otherwise. */
902 bool
903 c_parser_require (c_parser *parser,
904 enum cpp_ttype type,
905 const char *msgid)
907 if (c_parser_next_token_is (parser, type))
909 c_parser_consume_token (parser);
910 return true;
912 else
914 c_parser_error (parser, msgid);
915 return false;
919 /* If the next token is the indicated keyword, consume it. Otherwise,
920 issue the error MSGID. Returns true if found, false otherwise. */
922 static bool
923 c_parser_require_keyword (c_parser *parser,
924 enum rid keyword,
925 const char *msgid)
927 if (c_parser_next_token_is_keyword (parser, keyword))
929 c_parser_consume_token (parser);
930 return true;
932 else
934 c_parser_error (parser, msgid);
935 return false;
939 /* Like c_parser_require, except that tokens will be skipped until the
940 desired token is found. An error message is still produced if the
941 next token is not as expected. If MSGID is NULL then a message has
942 already been produced and no message will be produced this
943 time. */
945 void
946 c_parser_skip_until_found (c_parser *parser,
947 enum cpp_ttype type,
948 const char *msgid)
950 unsigned nesting_depth = 0;
952 if (c_parser_require (parser, type, msgid))
953 return;
955 /* Skip tokens until the desired token is found. */
956 while (true)
958 /* Peek at the next token. */
959 c_token *token = c_parser_peek_token (parser);
960 /* If we've reached the token we want, consume it and stop. */
961 if (token->type == type && !nesting_depth)
963 c_parser_consume_token (parser);
964 break;
967 /* If we've run out of tokens, stop. */
968 if (token->type == CPP_EOF)
969 return;
970 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
971 return;
972 if (token->type == CPP_OPEN_BRACE
973 || token->type == CPP_OPEN_PAREN
974 || token->type == CPP_OPEN_SQUARE)
975 ++nesting_depth;
976 else if (token->type == CPP_CLOSE_BRACE
977 || token->type == CPP_CLOSE_PAREN
978 || token->type == CPP_CLOSE_SQUARE)
980 if (nesting_depth-- == 0)
981 break;
983 /* Consume this token. */
984 c_parser_consume_token (parser);
986 parser->error = false;
989 /* Skip tokens until the end of a parameter is found, but do not
990 consume the comma, semicolon or closing delimiter. */
992 static void
993 c_parser_skip_to_end_of_parameter (c_parser *parser)
995 unsigned nesting_depth = 0;
997 while (true)
999 c_token *token = c_parser_peek_token (parser);
1000 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1001 && !nesting_depth)
1002 break;
1003 /* If we've run out of tokens, stop. */
1004 if (token->type == CPP_EOF)
1005 return;
1006 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1007 return;
1008 if (token->type == CPP_OPEN_BRACE
1009 || token->type == CPP_OPEN_PAREN
1010 || token->type == CPP_OPEN_SQUARE)
1011 ++nesting_depth;
1012 else if (token->type == CPP_CLOSE_BRACE
1013 || token->type == CPP_CLOSE_PAREN
1014 || token->type == CPP_CLOSE_SQUARE)
1016 if (nesting_depth-- == 0)
1017 break;
1019 /* Consume this token. */
1020 c_parser_consume_token (parser);
1022 parser->error = false;
1025 /* Expect to be at the end of the pragma directive and consume an
1026 end of line marker. */
1028 static void
1029 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1031 gcc_assert (parser->in_pragma);
1032 parser->in_pragma = false;
1034 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1035 c_parser_error (parser, "expected end of line");
1037 cpp_ttype token_type;
1040 c_token *token = c_parser_peek_token (parser);
1041 token_type = token->type;
1042 if (token_type == CPP_EOF)
1043 break;
1044 c_parser_consume_token (parser);
1046 while (token_type != CPP_PRAGMA_EOL);
1048 parser->error = false;
1051 /* Skip tokens until we have consumed an entire block, or until we
1052 have consumed a non-nested ';'. */
1054 static void
1055 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1057 unsigned nesting_depth = 0;
1058 bool save_error = parser->error;
1060 while (true)
1062 c_token *token;
1064 /* Peek at the next token. */
1065 token = c_parser_peek_token (parser);
1067 switch (token->type)
1069 case CPP_EOF:
1070 return;
1072 case CPP_PRAGMA_EOL:
1073 if (parser->in_pragma)
1074 return;
1075 break;
1077 case CPP_SEMICOLON:
1078 /* If the next token is a ';', we have reached the
1079 end of the statement. */
1080 if (!nesting_depth)
1082 /* Consume the ';'. */
1083 c_parser_consume_token (parser);
1084 goto finished;
1086 break;
1088 case CPP_CLOSE_BRACE:
1089 /* If the next token is a non-nested '}', then we have
1090 reached the end of the current block. */
1091 if (nesting_depth == 0 || --nesting_depth == 0)
1093 c_parser_consume_token (parser);
1094 goto finished;
1096 break;
1098 case CPP_OPEN_BRACE:
1099 /* If it the next token is a '{', then we are entering a new
1100 block. Consume the entire block. */
1101 ++nesting_depth;
1102 break;
1104 case CPP_PRAGMA:
1105 /* If we see a pragma, consume the whole thing at once. We
1106 have some safeguards against consuming pragmas willy-nilly.
1107 Normally, we'd expect to be here with parser->error set,
1108 which disables these safeguards. But it's possible to get
1109 here for secondary error recovery, after parser->error has
1110 been cleared. */
1111 c_parser_consume_pragma (parser);
1112 c_parser_skip_to_pragma_eol (parser);
1113 parser->error = save_error;
1114 continue;
1116 default:
1117 break;
1120 c_parser_consume_token (parser);
1123 finished:
1124 parser->error = false;
1127 /* CPP's options (initialized by c-opts.c). */
1128 extern cpp_options *cpp_opts;
1130 /* Save the warning flags which are controlled by __extension__. */
1132 static inline int
1133 disable_extension_diagnostics (void)
1135 int ret = (pedantic
1136 | (warn_pointer_arith << 1)
1137 | (warn_traditional << 2)
1138 | (flag_iso << 3)
1139 | (warn_long_long << 4)
1140 | (warn_cxx_compat << 5)
1141 | (warn_overlength_strings << 6)
1142 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1143 play tricks to properly restore it. */
1144 | ((warn_c90_c99_compat == 1) << 7)
1145 | ((warn_c90_c99_compat == -1) << 8)
1146 /* Similarly for warn_c99_c11_compat. */
1147 | ((warn_c99_c11_compat == 1) << 9)
1148 | ((warn_c99_c11_compat == -1) << 10)
1150 cpp_opts->cpp_pedantic = pedantic = 0;
1151 warn_pointer_arith = 0;
1152 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1153 flag_iso = 0;
1154 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1155 warn_cxx_compat = 0;
1156 warn_overlength_strings = 0;
1157 warn_c90_c99_compat = 0;
1158 warn_c99_c11_compat = 0;
1159 return ret;
1162 /* Restore the warning flags which are controlled by __extension__.
1163 FLAGS is the return value from disable_extension_diagnostics. */
1165 static inline void
1166 restore_extension_diagnostics (int flags)
1168 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1169 warn_pointer_arith = (flags >> 1) & 1;
1170 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1171 flag_iso = (flags >> 3) & 1;
1172 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1173 warn_cxx_compat = (flags >> 5) & 1;
1174 warn_overlength_strings = (flags >> 6) & 1;
1175 /* See above for why is this needed. */
1176 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1177 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1180 /* Helper data structure for parsing #pragma acc routine. */
1181 struct oacc_routine_data {
1182 bool error_seen; /* Set if error has been reported. */
1183 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1184 tree clauses;
1185 location_t loc;
1188 static void c_parser_external_declaration (c_parser *);
1189 static void c_parser_asm_definition (c_parser *);
1190 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1191 bool, bool, tree *, vec<c_token>,
1192 struct oacc_routine_data * = NULL,
1193 bool * = NULL);
1194 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1195 static void c_parser_static_assert_declaration (c_parser *);
1196 static struct c_typespec c_parser_enum_specifier (c_parser *);
1197 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1198 static tree c_parser_struct_declaration (c_parser *);
1199 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1200 static tree c_parser_alignas_specifier (c_parser *);
1201 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1202 c_dtr_syn, bool *);
1203 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1204 bool,
1205 struct c_declarator *);
1206 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1207 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1208 tree);
1209 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1210 static tree c_parser_simple_asm_expr (c_parser *);
1211 static tree c_parser_attributes (c_parser *);
1212 static struct c_expr c_parser_initializer (c_parser *);
1213 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1214 struct obstack *);
1215 static void c_parser_initelt (c_parser *, struct obstack *);
1216 static void c_parser_initval (c_parser *, struct c_expr *,
1217 struct obstack *);
1218 static tree c_parser_compound_statement (c_parser *);
1219 static void c_parser_compound_statement_nostart (c_parser *);
1220 static void c_parser_label (c_parser *);
1221 static void c_parser_statement (c_parser *, bool *);
1222 static void c_parser_statement_after_labels (c_parser *, bool *,
1223 vec<tree> * = NULL);
1224 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1225 static void c_parser_switch_statement (c_parser *, bool *);
1226 static void c_parser_while_statement (c_parser *, bool, bool *);
1227 static void c_parser_do_statement (c_parser *, bool);
1228 static void c_parser_for_statement (c_parser *, bool, bool *);
1229 static tree c_parser_asm_statement (c_parser *);
1230 static tree c_parser_asm_operands (c_parser *);
1231 static tree c_parser_asm_goto_operands (c_parser *);
1232 static tree c_parser_asm_clobbers (c_parser *);
1233 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1234 tree = NULL_TREE);
1235 static struct c_expr c_parser_conditional_expression (c_parser *,
1236 struct c_expr *, tree);
1237 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1238 tree);
1239 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1240 static struct c_expr c_parser_unary_expression (c_parser *);
1241 static struct c_expr c_parser_sizeof_expression (c_parser *);
1242 static struct c_expr c_parser_alignof_expression (c_parser *);
1243 static struct c_expr c_parser_postfix_expression (c_parser *);
1244 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1245 struct c_type_name *,
1246 location_t);
1247 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1248 location_t loc,
1249 struct c_expr);
1250 static tree c_parser_transaction (c_parser *, enum rid);
1251 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1252 static tree c_parser_transaction_cancel (c_parser *);
1253 static struct c_expr c_parser_expression (c_parser *);
1254 static struct c_expr c_parser_expression_conv (c_parser *);
1255 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1256 vec<tree, va_gc> **, location_t *,
1257 tree *, vec<location_t> *,
1258 unsigned int * = NULL);
1259 static void c_parser_oacc_declare (c_parser *);
1260 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1261 static void c_parser_oacc_update (c_parser *);
1262 static void c_parser_omp_construct (c_parser *, bool *);
1263 static void c_parser_omp_threadprivate (c_parser *);
1264 static void c_parser_omp_barrier (c_parser *);
1265 static void c_parser_omp_flush (c_parser *);
1266 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1267 tree, tree *, bool *);
1268 static void c_parser_omp_taskwait (c_parser *);
1269 static void c_parser_omp_taskyield (c_parser *);
1270 static void c_parser_omp_cancel (c_parser *);
1272 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1273 pragma_stmt, pragma_compound };
1274 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1275 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1276 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1277 static void c_parser_omp_end_declare_target (c_parser *);
1278 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1279 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1280 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1282 /* These Objective-C parser functions are only ever called when
1283 compiling Objective-C. */
1284 static void c_parser_objc_class_definition (c_parser *, tree);
1285 static void c_parser_objc_class_instance_variables (c_parser *);
1286 static void c_parser_objc_class_declaration (c_parser *);
1287 static void c_parser_objc_alias_declaration (c_parser *);
1288 static void c_parser_objc_protocol_definition (c_parser *, tree);
1289 static bool c_parser_objc_method_type (c_parser *);
1290 static void c_parser_objc_method_definition (c_parser *);
1291 static void c_parser_objc_methodprotolist (c_parser *);
1292 static void c_parser_objc_methodproto (c_parser *);
1293 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1294 static tree c_parser_objc_type_name (c_parser *);
1295 static tree c_parser_objc_protocol_refs (c_parser *);
1296 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1297 static void c_parser_objc_synchronized_statement (c_parser *);
1298 static tree c_parser_objc_selector (c_parser *);
1299 static tree c_parser_objc_selector_arg (c_parser *);
1300 static tree c_parser_objc_receiver (c_parser *);
1301 static tree c_parser_objc_message_args (c_parser *);
1302 static tree c_parser_objc_keywordexpr (c_parser *);
1303 static void c_parser_objc_at_property_declaration (c_parser *);
1304 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1305 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1306 static bool c_parser_objc_diagnose_bad_element_prefix
1307 (c_parser *, struct c_declspecs *);
1309 /* Cilk Plus supporting routines. */
1310 static void c_parser_cilk_simd (c_parser *, bool *);
1311 static void c_parser_cilk_for (c_parser *, tree, bool *);
1312 static bool c_parser_cilk_verify_simd (c_parser *, enum pragma_context);
1313 static tree c_parser_array_notation (location_t, c_parser *, tree, tree);
1314 static tree c_parser_cilk_clause_vectorlength (c_parser *, tree, bool);
1315 static void c_parser_cilk_grainsize (c_parser *, bool *);
1317 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1319 /* Parse a translation unit (C90 6.7, C99 6.9).
1321 translation-unit:
1322 external-declarations
1324 external-declarations:
1325 external-declaration
1326 external-declarations external-declaration
1328 GNU extensions:
1330 translation-unit:
1331 empty
1334 static void
1335 c_parser_translation_unit (c_parser *parser)
1337 if (c_parser_next_token_is (parser, CPP_EOF))
1339 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1340 "ISO C forbids an empty translation unit");
1342 else
1344 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1345 mark_valid_location_for_stdc_pragma (false);
1348 ggc_collect ();
1349 c_parser_external_declaration (parser);
1350 obstack_free (&parser_obstack, obstack_position);
1352 while (c_parser_next_token_is_not (parser, CPP_EOF));
1355 unsigned int i;
1356 tree decl;
1357 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1358 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1359 error ("storage size of %q+D isn%'t known", decl);
1362 /* Parse an external declaration (C90 6.7, C99 6.9).
1364 external-declaration:
1365 function-definition
1366 declaration
1368 GNU extensions:
1370 external-declaration:
1371 asm-definition
1373 __extension__ external-declaration
1375 Objective-C:
1377 external-declaration:
1378 objc-class-definition
1379 objc-class-declaration
1380 objc-alias-declaration
1381 objc-protocol-definition
1382 objc-method-definition
1383 @end
1386 static void
1387 c_parser_external_declaration (c_parser *parser)
1389 int ext;
1390 switch (c_parser_peek_token (parser)->type)
1392 case CPP_KEYWORD:
1393 switch (c_parser_peek_token (parser)->keyword)
1395 case RID_EXTENSION:
1396 ext = disable_extension_diagnostics ();
1397 c_parser_consume_token (parser);
1398 c_parser_external_declaration (parser);
1399 restore_extension_diagnostics (ext);
1400 break;
1401 case RID_ASM:
1402 c_parser_asm_definition (parser);
1403 break;
1404 case RID_AT_INTERFACE:
1405 case RID_AT_IMPLEMENTATION:
1406 gcc_assert (c_dialect_objc ());
1407 c_parser_objc_class_definition (parser, NULL_TREE);
1408 break;
1409 case RID_AT_CLASS:
1410 gcc_assert (c_dialect_objc ());
1411 c_parser_objc_class_declaration (parser);
1412 break;
1413 case RID_AT_ALIAS:
1414 gcc_assert (c_dialect_objc ());
1415 c_parser_objc_alias_declaration (parser);
1416 break;
1417 case RID_AT_PROTOCOL:
1418 gcc_assert (c_dialect_objc ());
1419 c_parser_objc_protocol_definition (parser, NULL_TREE);
1420 break;
1421 case RID_AT_PROPERTY:
1422 gcc_assert (c_dialect_objc ());
1423 c_parser_objc_at_property_declaration (parser);
1424 break;
1425 case RID_AT_SYNTHESIZE:
1426 gcc_assert (c_dialect_objc ());
1427 c_parser_objc_at_synthesize_declaration (parser);
1428 break;
1429 case RID_AT_DYNAMIC:
1430 gcc_assert (c_dialect_objc ());
1431 c_parser_objc_at_dynamic_declaration (parser);
1432 break;
1433 case RID_AT_END:
1434 gcc_assert (c_dialect_objc ());
1435 c_parser_consume_token (parser);
1436 objc_finish_implementation ();
1437 break;
1438 default:
1439 goto decl_or_fndef;
1441 break;
1442 case CPP_SEMICOLON:
1443 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1444 "ISO C does not allow extra %<;%> outside of a function");
1445 c_parser_consume_token (parser);
1446 break;
1447 case CPP_PRAGMA:
1448 mark_valid_location_for_stdc_pragma (true);
1449 c_parser_pragma (parser, pragma_external, NULL);
1450 mark_valid_location_for_stdc_pragma (false);
1451 break;
1452 case CPP_PLUS:
1453 case CPP_MINUS:
1454 if (c_dialect_objc ())
1456 c_parser_objc_method_definition (parser);
1457 break;
1459 /* Else fall through, and yield a syntax error trying to parse
1460 as a declaration or function definition. */
1461 /* FALLTHRU */
1462 default:
1463 decl_or_fndef:
1464 /* A declaration or a function definition (or, in Objective-C,
1465 an @interface or @protocol with prefix attributes). We can
1466 only tell which after parsing the declaration specifiers, if
1467 any, and the first declarator. */
1468 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1469 NULL, vNULL);
1470 break;
1474 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1475 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1477 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1478 6.7, 6.9.1). If FNDEF_OK is true, a function definition is
1479 accepted; otherwise (old-style parameter declarations) only other
1480 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1481 assertion is accepted; otherwise (old-style parameter declarations)
1482 it is not. If NESTED is true, we are inside a function or parsing
1483 old-style parameter declarations; any functions encountered are
1484 nested functions and declaration specifiers are required; otherwise
1485 we are at top level and functions are normal functions and
1486 declaration specifiers may be optional. If EMPTY_OK is true, empty
1487 declarations are OK (subject to all other constraints); otherwise
1488 (old-style parameter declarations) they are diagnosed. If
1489 START_ATTR_OK is true, the declaration specifiers may start with
1490 attributes; otherwise they may not.
1491 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1492 declaration when parsing an Objective-C foreach statement.
1493 FALLTHRU_ATTR_P is used to signal whether this function parsed
1494 "__attribute__((fallthrough));".
1496 declaration:
1497 declaration-specifiers init-declarator-list[opt] ;
1498 static_assert-declaration
1500 function-definition:
1501 declaration-specifiers[opt] declarator declaration-list[opt]
1502 compound-statement
1504 declaration-list:
1505 declaration
1506 declaration-list declaration
1508 init-declarator-list:
1509 init-declarator
1510 init-declarator-list , init-declarator
1512 init-declarator:
1513 declarator simple-asm-expr[opt] attributes[opt]
1514 declarator simple-asm-expr[opt] attributes[opt] = initializer
1516 GNU extensions:
1518 nested-function-definition:
1519 declaration-specifiers declarator declaration-list[opt]
1520 compound-statement
1522 attribute ;
1524 Objective-C:
1525 attributes objc-class-definition
1526 attributes objc-category-definition
1527 attributes objc-protocol-definition
1529 The simple-asm-expr and attributes are GNU extensions.
1531 This function does not handle __extension__; that is handled in its
1532 callers. ??? Following the old parser, __extension__ may start
1533 external declarations, declarations in functions and declarations
1534 at the start of "for" loops, but not old-style parameter
1535 declarations.
1537 C99 requires declaration specifiers in a function definition; the
1538 absence is diagnosed through the diagnosis of implicit int. In GNU
1539 C we also allow but diagnose declarations without declaration
1540 specifiers, but only at top level (elsewhere they conflict with
1541 other syntax).
1543 In Objective-C, declarations of the looping variable in a foreach
1544 statement are exceptionally terminated by 'in' (for example, 'for
1545 (NSObject *object in array) { ... }').
1547 OpenMP:
1549 declaration:
1550 threadprivate-directive
1552 GIMPLE:
1554 gimple-function-definition:
1555 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1556 declaration-list[opt] compound-statement
1558 rtl-function-definition:
1559 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1560 declaration-list[opt] compound-statement */
1562 static void
1563 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1564 bool static_assert_ok, bool empty_ok,
1565 bool nested, bool start_attr_ok,
1566 tree *objc_foreach_object_declaration,
1567 vec<c_token> omp_declare_simd_clauses,
1568 struct oacc_routine_data *oacc_routine_data,
1569 bool *fallthru_attr_p)
1571 struct c_declspecs *specs;
1572 tree prefix_attrs;
1573 tree all_prefix_attrs;
1574 bool diagnosed_no_specs = false;
1575 location_t here = c_parser_peek_token (parser)->location;
1577 if (static_assert_ok
1578 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1580 c_parser_static_assert_declaration (parser);
1581 return;
1583 specs = build_null_declspecs ();
1585 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1586 if (c_parser_peek_token (parser)->type == CPP_NAME
1587 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1588 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1589 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1590 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1592 tree name = c_parser_peek_token (parser)->value;
1594 /* Issue a warning about NAME being an unknown type name, perhaps
1595 with some kind of hint.
1596 If the user forgot a "struct" etc, suggest inserting
1597 it. Otherwise, attempt to look for misspellings. */
1598 gcc_rich_location richloc (here);
1599 if (tag_exists_p (RECORD_TYPE, name))
1601 /* This is not C++ with its implicit typedef. */
1602 richloc.add_fixit_insert_before ("struct ");
1603 error_at_rich_loc (&richloc,
1604 "unknown type name %qE;"
1605 " use %<struct%> keyword to refer to the type",
1606 name);
1608 else if (tag_exists_p (UNION_TYPE, name))
1610 richloc.add_fixit_insert_before ("union ");
1611 error_at_rich_loc (&richloc,
1612 "unknown type name %qE;"
1613 " use %<union%> keyword to refer to the type",
1614 name);
1616 else if (tag_exists_p (ENUMERAL_TYPE, name))
1618 richloc.add_fixit_insert_before ("enum ");
1619 error_at_rich_loc (&richloc,
1620 "unknown type name %qE;"
1621 " use %<enum%> keyword to refer to the type",
1622 name);
1624 else
1626 const char *hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME);
1627 if (hint)
1629 richloc.add_fixit_replace (hint);
1630 error_at_rich_loc (&richloc,
1631 "unknown type name %qE; did you mean %qs?",
1632 name, hint);
1634 else
1635 error_at (here, "unknown type name %qE", name);
1638 /* Parse declspecs normally to get a correct pointer type, but avoid
1639 a further "fails to be a type name" error. Refuse nested functions
1640 since it is not how the user likely wants us to recover. */
1641 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1642 c_parser_peek_token (parser)->keyword = RID_VOID;
1643 c_parser_peek_token (parser)->value = error_mark_node;
1644 fndef_ok = !nested;
1647 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1648 true, true, cla_nonabstract_decl);
1649 if (parser->error)
1651 c_parser_skip_to_end_of_block_or_statement (parser);
1652 return;
1654 if (nested && !specs->declspecs_seen_p)
1656 c_parser_error (parser, "expected declaration specifiers");
1657 c_parser_skip_to_end_of_block_or_statement (parser);
1658 return;
1661 finish_declspecs (specs);
1662 bool auto_type_p = specs->typespec_word == cts_auto_type;
1663 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1665 if (auto_type_p)
1666 error_at (here, "%<__auto_type%> in empty declaration");
1667 else if (specs->typespec_kind == ctsk_none
1668 && attribute_fallthrough_p (specs->attrs))
1670 if (fallthru_attr_p != NULL)
1671 *fallthru_attr_p = true;
1672 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1673 void_type_node, 0);
1674 add_stmt (fn);
1676 else if (empty_ok)
1677 shadow_tag (specs);
1678 else
1680 shadow_tag_warned (specs, 1);
1681 pedwarn (here, 0, "empty declaration");
1683 c_parser_consume_token (parser);
1684 if (oacc_routine_data)
1685 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1686 return;
1689 /* Provide better error recovery. Note that a type name here is usually
1690 better diagnosed as a redeclaration. */
1691 if (empty_ok
1692 && specs->typespec_kind == ctsk_tagdef
1693 && c_parser_next_token_starts_declspecs (parser)
1694 && !c_parser_next_token_is (parser, CPP_NAME))
1696 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1697 parser->error = false;
1698 shadow_tag_warned (specs, 1);
1699 return;
1701 else if (c_dialect_objc () && !auto_type_p)
1703 /* Prefix attributes are an error on method decls. */
1704 switch (c_parser_peek_token (parser)->type)
1706 case CPP_PLUS:
1707 case CPP_MINUS:
1708 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1709 return;
1710 if (specs->attrs)
1712 warning_at (c_parser_peek_token (parser)->location,
1713 OPT_Wattributes,
1714 "prefix attributes are ignored for methods");
1715 specs->attrs = NULL_TREE;
1717 if (fndef_ok)
1718 c_parser_objc_method_definition (parser);
1719 else
1720 c_parser_objc_methodproto (parser);
1721 return;
1722 break;
1723 default:
1724 break;
1726 /* This is where we parse 'attributes @interface ...',
1727 'attributes @implementation ...', 'attributes @protocol ...'
1728 (where attributes could be, for example, __attribute__
1729 ((deprecated)).
1731 switch (c_parser_peek_token (parser)->keyword)
1733 case RID_AT_INTERFACE:
1735 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1736 return;
1737 c_parser_objc_class_definition (parser, specs->attrs);
1738 return;
1740 break;
1741 case RID_AT_IMPLEMENTATION:
1743 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1744 return;
1745 if (specs->attrs)
1747 warning_at (c_parser_peek_token (parser)->location,
1748 OPT_Wattributes,
1749 "prefix attributes are ignored for implementations");
1750 specs->attrs = NULL_TREE;
1752 c_parser_objc_class_definition (parser, NULL_TREE);
1753 return;
1755 break;
1756 case RID_AT_PROTOCOL:
1758 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1759 return;
1760 c_parser_objc_protocol_definition (parser, specs->attrs);
1761 return;
1763 break;
1764 case RID_AT_ALIAS:
1765 case RID_AT_CLASS:
1766 case RID_AT_END:
1767 case RID_AT_PROPERTY:
1768 if (specs->attrs)
1770 c_parser_error (parser, "unexpected attribute");
1771 specs->attrs = NULL;
1773 break;
1774 default:
1775 break;
1778 else if (attribute_fallthrough_p (specs->attrs))
1779 warning_at (here, OPT_Wattributes,
1780 "%<fallthrough%> attribute not followed by %<;%>");
1782 pending_xref_error ();
1783 prefix_attrs = specs->attrs;
1784 all_prefix_attrs = prefix_attrs;
1785 specs->attrs = NULL_TREE;
1786 while (true)
1788 struct c_declarator *declarator;
1789 bool dummy = false;
1790 timevar_id_t tv;
1791 tree fnbody = NULL_TREE;
1792 /* Declaring either one or more declarators (in which case we
1793 should diagnose if there were no declaration specifiers) or a
1794 function definition (in which case the diagnostic for
1795 implicit int suffices). */
1796 declarator = c_parser_declarator (parser,
1797 specs->typespec_kind != ctsk_none,
1798 C_DTR_NORMAL, &dummy);
1799 if (declarator == NULL)
1801 if (omp_declare_simd_clauses.exists ()
1802 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1803 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
1804 omp_declare_simd_clauses);
1805 if (oacc_routine_data)
1806 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1807 c_parser_skip_to_end_of_block_or_statement (parser);
1808 return;
1810 if (auto_type_p && declarator->kind != cdk_id)
1812 error_at (here,
1813 "%<__auto_type%> requires a plain identifier"
1814 " as declarator");
1815 c_parser_skip_to_end_of_block_or_statement (parser);
1816 return;
1818 if (c_parser_next_token_is (parser, CPP_EQ)
1819 || c_parser_next_token_is (parser, CPP_COMMA)
1820 || c_parser_next_token_is (parser, CPP_SEMICOLON)
1821 || c_parser_next_token_is_keyword (parser, RID_ASM)
1822 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
1823 || c_parser_next_token_is_keyword (parser, RID_IN))
1825 tree asm_name = NULL_TREE;
1826 tree postfix_attrs = NULL_TREE;
1827 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
1829 diagnosed_no_specs = true;
1830 pedwarn (here, 0, "data definition has no type or storage class");
1832 /* Having seen a data definition, there cannot now be a
1833 function definition. */
1834 fndef_ok = false;
1835 if (c_parser_next_token_is_keyword (parser, RID_ASM))
1836 asm_name = c_parser_simple_asm_expr (parser);
1837 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1839 postfix_attrs = c_parser_attributes (parser);
1840 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
1842 /* This means there is an attribute specifier after
1843 the declarator in a function definition. Provide
1844 some more information for the user. */
1845 error_at (here, "attributes should be specified before the "
1846 "declarator in a function definition");
1847 c_parser_skip_to_end_of_block_or_statement (parser);
1848 return;
1851 if (c_parser_next_token_is (parser, CPP_EQ))
1853 tree d;
1854 struct c_expr init;
1855 location_t init_loc;
1856 c_parser_consume_token (parser);
1857 if (auto_type_p)
1859 init_loc = c_parser_peek_token (parser)->location;
1860 rich_location richloc (line_table, init_loc);
1861 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
1862 init = c_parser_expr_no_commas (parser, NULL);
1863 if (TREE_CODE (init.value) == COMPONENT_REF
1864 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
1865 error_at (here,
1866 "%<__auto_type%> used with a bit-field"
1867 " initializer");
1868 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
1869 tree init_type = TREE_TYPE (init.value);
1870 /* As with typeof, remove all qualifiers from atomic types. */
1871 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
1872 init_type
1873 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
1874 bool vm_type = variably_modified_type_p (init_type,
1875 NULL_TREE);
1876 if (vm_type)
1877 init.value = c_save_expr (init.value);
1878 finish_init ();
1879 specs->typespec_kind = ctsk_typeof;
1880 specs->locations[cdw_typedef] = init_loc;
1881 specs->typedef_p = true;
1882 specs->type = init_type;
1883 if (vm_type)
1885 bool maybe_const = true;
1886 tree type_expr = c_fully_fold (init.value, false,
1887 &maybe_const);
1888 specs->expr_const_operands &= maybe_const;
1889 if (specs->expr)
1890 specs->expr = build2 (COMPOUND_EXPR,
1891 TREE_TYPE (type_expr),
1892 specs->expr, type_expr);
1893 else
1894 specs->expr = type_expr;
1896 d = start_decl (declarator, specs, true,
1897 chainon (postfix_attrs, all_prefix_attrs));
1898 if (!d)
1899 d = error_mark_node;
1900 if (omp_declare_simd_clauses.exists ()
1901 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1902 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1903 omp_declare_simd_clauses);
1905 else
1907 /* The declaration of the variable is in effect while
1908 its initializer is parsed. */
1909 d = start_decl (declarator, specs, true,
1910 chainon (postfix_attrs, all_prefix_attrs));
1911 if (!d)
1912 d = error_mark_node;
1913 if (omp_declare_simd_clauses.exists ()
1914 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1915 c_finish_omp_declare_simd (parser, d, NULL_TREE,
1916 omp_declare_simd_clauses);
1917 init_loc = c_parser_peek_token (parser)->location;
1918 rich_location richloc (line_table, init_loc);
1919 start_init (d, asm_name, global_bindings_p (), &richloc);
1920 init = c_parser_initializer (parser);
1921 finish_init ();
1923 if (oacc_routine_data)
1924 c_finish_oacc_routine (oacc_routine_data, d, false);
1925 if (d != error_mark_node)
1927 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
1928 finish_decl (d, init_loc, init.value,
1929 init.original_type, asm_name);
1932 else
1934 if (auto_type_p)
1936 error_at (here,
1937 "%<__auto_type%> requires an initialized "
1938 "data declaration");
1939 c_parser_skip_to_end_of_block_or_statement (parser);
1940 return;
1942 tree d = start_decl (declarator, specs, false,
1943 chainon (postfix_attrs,
1944 all_prefix_attrs));
1945 if (omp_declare_simd_clauses.exists ()
1946 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
1948 tree parms = NULL_TREE;
1949 if (d && TREE_CODE (d) == FUNCTION_DECL)
1951 struct c_declarator *ce = declarator;
1952 while (ce != NULL)
1953 if (ce->kind == cdk_function)
1955 parms = ce->u.arg_info->parms;
1956 break;
1958 else
1959 ce = ce->declarator;
1961 if (parms)
1962 temp_store_parm_decls (d, parms);
1963 c_finish_omp_declare_simd (parser, d, parms,
1964 omp_declare_simd_clauses);
1965 if (parms)
1966 temp_pop_parm_decls ();
1968 if (oacc_routine_data)
1969 c_finish_oacc_routine (oacc_routine_data, d, false);
1970 if (d)
1971 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
1972 NULL_TREE, asm_name);
1974 if (c_parser_next_token_is_keyword (parser, RID_IN))
1976 if (d)
1977 *objc_foreach_object_declaration = d;
1978 else
1979 *objc_foreach_object_declaration = error_mark_node;
1982 if (c_parser_next_token_is (parser, CPP_COMMA))
1984 if (auto_type_p)
1986 error_at (here,
1987 "%<__auto_type%> may only be used with"
1988 " a single declarator");
1989 c_parser_skip_to_end_of_block_or_statement (parser);
1990 return;
1992 c_parser_consume_token (parser);
1993 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
1994 all_prefix_attrs = chainon (c_parser_attributes (parser),
1995 prefix_attrs);
1996 else
1997 all_prefix_attrs = prefix_attrs;
1998 continue;
2000 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2002 c_parser_consume_token (parser);
2003 return;
2005 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2007 /* This can only happen in Objective-C: we found the
2008 'in' that terminates the declaration inside an
2009 Objective-C foreach statement. Do not consume the
2010 token, so that the caller can use it to determine
2011 that this indeed is a foreach context. */
2012 return;
2014 else
2016 c_parser_error (parser, "expected %<,%> or %<;%>");
2017 c_parser_skip_to_end_of_block_or_statement (parser);
2018 return;
2021 else if (auto_type_p)
2023 error_at (here,
2024 "%<__auto_type%> requires an initialized data declaration");
2025 c_parser_skip_to_end_of_block_or_statement (parser);
2026 return;
2028 else if (!fndef_ok)
2030 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2031 "%<asm%> or %<__attribute__%>");
2032 c_parser_skip_to_end_of_block_or_statement (parser);
2033 return;
2035 /* Function definition (nested or otherwise). */
2036 if (nested)
2038 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2039 c_push_function_context ();
2041 if (!start_function (specs, declarator, all_prefix_attrs))
2043 /* This can appear in many cases looking nothing like a
2044 function definition, so we don't give a more specific
2045 error suggesting there was one. */
2046 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2047 "or %<__attribute__%>");
2048 if (nested)
2049 c_pop_function_context ();
2050 break;
2053 if (DECL_DECLARED_INLINE_P (current_function_decl))
2054 tv = TV_PARSE_INLINE;
2055 else
2056 tv = TV_PARSE_FUNC;
2057 auto_timevar at (g_timer, tv);
2059 /* Parse old-style parameter declarations. ??? Attributes are
2060 not allowed to start declaration specifiers here because of a
2061 syntax conflict between a function declaration with attribute
2062 suffix and a function definition with an attribute prefix on
2063 first old-style parameter declaration. Following the old
2064 parser, they are not accepted on subsequent old-style
2065 parameter declarations either. However, there is no
2066 ambiguity after the first declaration, nor indeed on the
2067 first as long as we don't allow postfix attributes after a
2068 declarator with a nonempty identifier list in a definition;
2069 and postfix attributes have never been accepted here in
2070 function definitions either. */
2071 while (c_parser_next_token_is_not (parser, CPP_EOF)
2072 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2073 c_parser_declaration_or_fndef (parser, false, false, false,
2074 true, false, NULL, vNULL);
2075 store_parm_decls ();
2076 if (omp_declare_simd_clauses.exists ()
2077 || !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
2078 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2079 omp_declare_simd_clauses);
2080 if (oacc_routine_data)
2081 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2082 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2083 = c_parser_peek_token (parser)->location;
2085 /* If the definition was marked with __GIMPLE then parse the
2086 function body as GIMPLE. */
2087 if (specs->gimple_p)
2089 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2090 bool saved = in_late_binary_op;
2091 in_late_binary_op = true;
2092 c_parser_parse_gimple_body (parser);
2093 in_late_binary_op = saved;
2095 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2096 consuming the function body. */
2097 else if (specs->rtl_p)
2099 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2101 /* Normally, store_parm_decls sets next_is_function_body,
2102 anticipating a function body. We need a push_scope/pop_scope
2103 pair to flush out this state, or subsequent function parsing
2104 will go wrong. */
2105 push_scope ();
2106 pop_scope ();
2108 finish_function ();
2109 return;
2111 else
2113 fnbody = c_parser_compound_statement (parser);
2114 if (flag_cilkplus && contains_array_notation_expr (fnbody))
2115 fnbody = expand_array_notation_exprs (fnbody);
2117 tree fndecl = current_function_decl;
2118 if (nested)
2120 tree decl = current_function_decl;
2121 /* Mark nested functions as needing static-chain initially.
2122 lower_nested_functions will recompute it but the
2123 DECL_STATIC_CHAIN flag is also used before that happens,
2124 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2125 DECL_STATIC_CHAIN (decl) = 1;
2126 add_stmt (fnbody);
2127 finish_function ();
2128 c_pop_function_context ();
2129 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2131 else
2133 if (fnbody)
2134 add_stmt (fnbody);
2135 finish_function ();
2137 /* Get rid of the empty stmt list for GIMPLE. */
2138 if (specs->gimple_p)
2139 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2141 break;
2145 /* Parse an asm-definition (asm() outside a function body). This is a
2146 GNU extension.
2148 asm-definition:
2149 simple-asm-expr ;
2152 static void
2153 c_parser_asm_definition (c_parser *parser)
2155 tree asm_str = c_parser_simple_asm_expr (parser);
2156 if (asm_str)
2157 symtab->finalize_toplevel_asm (asm_str);
2158 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2161 /* Parse a static assertion (C11 6.7.10).
2163 static_assert-declaration:
2164 static_assert-declaration-no-semi ;
2167 static void
2168 c_parser_static_assert_declaration (c_parser *parser)
2170 c_parser_static_assert_declaration_no_semi (parser);
2171 if (parser->error
2172 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2173 c_parser_skip_to_end_of_block_or_statement (parser);
2176 /* Parse a static assertion (C11 6.7.10), without the trailing
2177 semicolon.
2179 static_assert-declaration-no-semi:
2180 _Static_assert ( constant-expression , string-literal )
2183 static void
2184 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2186 location_t assert_loc, value_loc;
2187 tree value;
2188 tree string;
2190 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2191 assert_loc = c_parser_peek_token (parser)->location;
2192 if (flag_isoc99)
2193 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2194 "ISO C99 does not support %<_Static_assert%>");
2195 else
2196 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2197 "ISO C90 does not support %<_Static_assert%>");
2198 c_parser_consume_token (parser);
2199 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2200 return;
2201 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2202 value = c_parser_expr_no_commas (parser, NULL).value;
2203 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2204 parser->lex_untranslated_string = true;
2205 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
2207 parser->lex_untranslated_string = false;
2208 return;
2210 switch (c_parser_peek_token (parser)->type)
2212 case CPP_STRING:
2213 case CPP_STRING16:
2214 case CPP_STRING32:
2215 case CPP_WSTRING:
2216 case CPP_UTF8STRING:
2217 string = c_parser_peek_token (parser)->value;
2218 c_parser_consume_token (parser);
2219 parser->lex_untranslated_string = false;
2220 break;
2221 default:
2222 c_parser_error (parser, "expected string literal");
2223 parser->lex_untranslated_string = false;
2224 return;
2226 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
2228 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2230 error_at (value_loc, "expression in static assertion is not an integer");
2231 return;
2233 if (TREE_CODE (value) != INTEGER_CST)
2235 value = c_fully_fold (value, false, NULL);
2236 /* Strip no-op conversions. */
2237 STRIP_TYPE_NOPS (value);
2238 if (TREE_CODE (value) == INTEGER_CST)
2239 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2240 "is not an integer constant expression");
2242 if (TREE_CODE (value) != INTEGER_CST)
2244 error_at (value_loc, "expression in static assertion is not constant");
2245 return;
2247 constant_expression_warning (value);
2248 if (integer_zerop (value))
2249 error_at (assert_loc, "static assertion failed: %E", string);
2252 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2253 6.7), adding them to SPECS (which may already include some).
2254 Storage class specifiers are accepted iff SCSPEC_OK; type
2255 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2256 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2257 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2259 declaration-specifiers:
2260 storage-class-specifier declaration-specifiers[opt]
2261 type-specifier declaration-specifiers[opt]
2262 type-qualifier declaration-specifiers[opt]
2263 function-specifier declaration-specifiers[opt]
2264 alignment-specifier declaration-specifiers[opt]
2266 Function specifiers (inline) are from C99, and are currently
2267 handled as storage class specifiers, as is __thread. Alignment
2268 specifiers are from C11.
2270 C90 6.5.1, C99 6.7.1:
2271 storage-class-specifier:
2272 typedef
2273 extern
2274 static
2275 auto
2276 register
2277 _Thread_local
2279 (_Thread_local is new in C11.)
2281 C99 6.7.4:
2282 function-specifier:
2283 inline
2284 _Noreturn
2286 (_Noreturn is new in C11.)
2288 C90 6.5.2, C99 6.7.2:
2289 type-specifier:
2290 void
2291 char
2292 short
2294 long
2295 float
2296 double
2297 signed
2298 unsigned
2299 _Bool
2300 _Complex
2301 [_Imaginary removed in C99 TC2]
2302 struct-or-union-specifier
2303 enum-specifier
2304 typedef-name
2305 atomic-type-specifier
2307 (_Bool and _Complex are new in C99.)
2308 (atomic-type-specifier is new in C11.)
2310 C90 6.5.3, C99 6.7.3:
2312 type-qualifier:
2313 const
2314 restrict
2315 volatile
2316 address-space-qualifier
2317 _Atomic
2319 (restrict is new in C99.)
2320 (_Atomic is new in C11.)
2322 GNU extensions:
2324 declaration-specifiers:
2325 attributes declaration-specifiers[opt]
2327 type-qualifier:
2328 address-space
2330 address-space:
2331 identifier recognized by the target
2333 storage-class-specifier:
2334 __thread
2336 type-specifier:
2337 typeof-specifier
2338 __auto_type
2339 __intN
2340 _Decimal32
2341 _Decimal64
2342 _Decimal128
2343 _Fract
2344 _Accum
2345 _Sat
2347 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2348 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2350 atomic-type-specifier
2351 _Atomic ( type-name )
2353 Objective-C:
2355 type-specifier:
2356 class-name objc-protocol-refs[opt]
2357 typedef-name objc-protocol-refs
2358 objc-protocol-refs
2361 void
2362 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2363 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2364 bool alignspec_ok, bool auto_type_ok,
2365 enum c_lookahead_kind la)
2367 bool attrs_ok = start_attr_ok;
2368 bool seen_type = specs->typespec_kind != ctsk_none;
2370 if (!typespec_ok)
2371 gcc_assert (la == cla_prefer_id);
2373 while (c_parser_next_token_is (parser, CPP_NAME)
2374 || c_parser_next_token_is (parser, CPP_KEYWORD)
2375 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2377 struct c_typespec t;
2378 tree attrs;
2379 tree align;
2380 location_t loc = c_parser_peek_token (parser)->location;
2382 /* If we cannot accept a type, exit if the next token must start
2383 one. Also, if we already have seen a tagged definition,
2384 a typename would be an error anyway and likely the user
2385 has simply forgotten a semicolon, so we exit. */
2386 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2387 && c_parser_next_tokens_start_typename (parser, la)
2388 && !c_parser_next_token_is_qualifier (parser))
2389 break;
2391 if (c_parser_next_token_is (parser, CPP_NAME))
2393 c_token *name_token = c_parser_peek_token (parser);
2394 tree value = name_token->value;
2395 c_id_kind kind = name_token->id_kind;
2397 if (kind == C_ID_ADDRSPACE)
2399 addr_space_t as
2400 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2401 declspecs_add_addrspace (name_token->location, specs, as);
2402 c_parser_consume_token (parser);
2403 attrs_ok = true;
2404 continue;
2407 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2409 /* If we cannot accept a type, and the next token must start one,
2410 exit. Do the same if we already have seen a tagged definition,
2411 since it would be an error anyway and likely the user has simply
2412 forgotten a semicolon. */
2413 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2414 break;
2416 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2417 a C_ID_CLASSNAME. */
2418 c_parser_consume_token (parser);
2419 seen_type = true;
2420 attrs_ok = true;
2421 if (kind == C_ID_ID)
2423 error_at (loc, "unknown type name %qE", value);
2424 t.kind = ctsk_typedef;
2425 t.spec = error_mark_node;
2427 else if (kind == C_ID_TYPENAME
2428 && (!c_dialect_objc ()
2429 || c_parser_next_token_is_not (parser, CPP_LESS)))
2431 t.kind = ctsk_typedef;
2432 /* For a typedef name, record the meaning, not the name.
2433 In case of 'foo foo, bar;'. */
2434 t.spec = lookup_name (value);
2436 else
2438 tree proto = NULL_TREE;
2439 gcc_assert (c_dialect_objc ());
2440 t.kind = ctsk_objc;
2441 if (c_parser_next_token_is (parser, CPP_LESS))
2442 proto = c_parser_objc_protocol_refs (parser);
2443 t.spec = objc_get_protocol_qualified_type (value, proto);
2445 t.expr = NULL_TREE;
2446 t.expr_const_operands = true;
2447 declspecs_add_type (name_token->location, specs, t);
2448 continue;
2450 if (c_parser_next_token_is (parser, CPP_LESS))
2452 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2453 nisse@lysator.liu.se. */
2454 tree proto;
2455 gcc_assert (c_dialect_objc ());
2456 if (!typespec_ok || seen_type)
2457 break;
2458 proto = c_parser_objc_protocol_refs (parser);
2459 t.kind = ctsk_objc;
2460 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2461 t.expr = NULL_TREE;
2462 t.expr_const_operands = true;
2463 declspecs_add_type (loc, specs, t);
2464 continue;
2466 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2467 switch (c_parser_peek_token (parser)->keyword)
2469 case RID_STATIC:
2470 case RID_EXTERN:
2471 case RID_REGISTER:
2472 case RID_TYPEDEF:
2473 case RID_INLINE:
2474 case RID_NORETURN:
2475 case RID_AUTO:
2476 case RID_THREAD:
2477 if (!scspec_ok)
2478 goto out;
2479 attrs_ok = true;
2480 /* TODO: Distinguish between function specifiers (inline, noreturn)
2481 and storage class specifiers, either here or in
2482 declspecs_add_scspec. */
2483 declspecs_add_scspec (loc, specs,
2484 c_parser_peek_token (parser)->value);
2485 c_parser_consume_token (parser);
2486 break;
2487 case RID_AUTO_TYPE:
2488 if (!auto_type_ok)
2489 goto out;
2490 /* Fall through. */
2491 case RID_UNSIGNED:
2492 case RID_LONG:
2493 case RID_SHORT:
2494 case RID_SIGNED:
2495 case RID_COMPLEX:
2496 case RID_INT:
2497 case RID_CHAR:
2498 case RID_FLOAT:
2499 case RID_DOUBLE:
2500 case RID_VOID:
2501 case RID_DFLOAT32:
2502 case RID_DFLOAT64:
2503 case RID_DFLOAT128:
2504 CASE_RID_FLOATN_NX:
2505 case RID_BOOL:
2506 case RID_FRACT:
2507 case RID_ACCUM:
2508 case RID_SAT:
2509 case RID_INT_N_0:
2510 case RID_INT_N_1:
2511 case RID_INT_N_2:
2512 case RID_INT_N_3:
2513 if (!typespec_ok)
2514 goto out;
2515 attrs_ok = true;
2516 seen_type = true;
2517 if (c_dialect_objc ())
2518 parser->objc_need_raw_identifier = true;
2519 t.kind = ctsk_resword;
2520 t.spec = c_parser_peek_token (parser)->value;
2521 t.expr = NULL_TREE;
2522 t.expr_const_operands = true;
2523 declspecs_add_type (loc, specs, t);
2524 c_parser_consume_token (parser);
2525 break;
2526 case RID_ENUM:
2527 if (!typespec_ok)
2528 goto out;
2529 attrs_ok = true;
2530 seen_type = true;
2531 t = c_parser_enum_specifier (parser);
2532 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2533 declspecs_add_type (loc, specs, t);
2534 break;
2535 case RID_STRUCT:
2536 case RID_UNION:
2537 if (!typespec_ok)
2538 goto out;
2539 attrs_ok = true;
2540 seen_type = true;
2541 t = c_parser_struct_or_union_specifier (parser);
2542 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2543 declspecs_add_type (loc, specs, t);
2544 break;
2545 case RID_TYPEOF:
2546 /* ??? The old parser rejected typeof after other type
2547 specifiers, but is a syntax error the best way of
2548 handling this? */
2549 if (!typespec_ok || seen_type)
2550 goto out;
2551 attrs_ok = true;
2552 seen_type = true;
2553 t = c_parser_typeof_specifier (parser);
2554 declspecs_add_type (loc, specs, t);
2555 break;
2556 case RID_ATOMIC:
2557 /* C parser handling of Objective-C constructs needs
2558 checking for correct lvalue-to-rvalue conversions, and
2559 the code in build_modify_expr handling various
2560 Objective-C cases, and that in build_unary_op handling
2561 Objective-C cases for increment / decrement, also needs
2562 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2563 and objc_types_are_equivalent may also need updates. */
2564 if (c_dialect_objc ())
2565 sorry ("%<_Atomic%> in Objective-C");
2566 if (flag_isoc99)
2567 pedwarn_c99 (loc, OPT_Wpedantic,
2568 "ISO C99 does not support the %<_Atomic%> qualifier");
2569 else
2570 pedwarn_c99 (loc, OPT_Wpedantic,
2571 "ISO C90 does not support the %<_Atomic%> qualifier");
2572 attrs_ok = true;
2573 tree value;
2574 value = c_parser_peek_token (parser)->value;
2575 c_parser_consume_token (parser);
2576 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2578 /* _Atomic ( type-name ). */
2579 seen_type = true;
2580 c_parser_consume_token (parser);
2581 struct c_type_name *type = c_parser_type_name (parser);
2582 t.kind = ctsk_typeof;
2583 t.spec = error_mark_node;
2584 t.expr = NULL_TREE;
2585 t.expr_const_operands = true;
2586 if (type != NULL)
2587 t.spec = groktypename (type, &t.expr,
2588 &t.expr_const_operands);
2589 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2590 "expected %<)%>");
2591 if (t.spec != error_mark_node)
2593 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2594 error_at (loc, "%<_Atomic%>-qualified array type");
2595 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2596 error_at (loc, "%<_Atomic%>-qualified function type");
2597 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2598 error_at (loc, "%<_Atomic%> applied to a qualified type");
2599 else
2600 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2602 declspecs_add_type (loc, specs, t);
2604 else
2605 declspecs_add_qual (loc, specs, value);
2606 break;
2607 case RID_CONST:
2608 case RID_VOLATILE:
2609 case RID_RESTRICT:
2610 attrs_ok = true;
2611 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2612 c_parser_consume_token (parser);
2613 break;
2614 case RID_ATTRIBUTE:
2615 if (!attrs_ok)
2616 goto out;
2617 attrs = c_parser_attributes (parser);
2618 declspecs_add_attrs (loc, specs, attrs);
2619 break;
2620 case RID_ALIGNAS:
2621 if (!alignspec_ok)
2622 goto out;
2623 align = c_parser_alignas_specifier (parser);
2624 declspecs_add_alignas (loc, specs, align);
2625 break;
2626 case RID_GIMPLE:
2627 if (! flag_gimple)
2628 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2629 c_parser_consume_token (parser);
2630 specs->gimple_p = true;
2631 specs->locations[cdw_gimple] = loc;
2632 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2633 break;
2634 case RID_RTL:
2635 c_parser_consume_token (parser);
2636 specs->rtl_p = true;
2637 specs->locations[cdw_rtl] = loc;
2638 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2639 break;
2640 default:
2641 goto out;
2644 out: ;
2647 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2).
2649 enum-specifier:
2650 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2651 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2652 enum attributes[opt] identifier
2654 The form with trailing comma is new in C99. The forms with
2655 attributes are GNU extensions. In GNU C, we accept any expression
2656 without commas in the syntax (assignment expressions, not just
2657 conditional expressions); assignment expressions will be diagnosed
2658 as non-constant.
2660 enumerator-list:
2661 enumerator
2662 enumerator-list , enumerator
2664 enumerator:
2665 enumeration-constant
2666 enumeration-constant = constant-expression
2668 GNU Extensions:
2670 enumerator:
2671 enumeration-constant attributes[opt]
2672 enumeration-constant attributes[opt] = constant-expression
2676 static struct c_typespec
2677 c_parser_enum_specifier (c_parser *parser)
2679 struct c_typespec ret;
2680 tree attrs;
2681 tree ident = NULL_TREE;
2682 location_t enum_loc;
2683 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2684 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2685 enum_loc = c_parser_peek_token (parser)->location;
2686 c_parser_consume_token (parser);
2687 attrs = c_parser_attributes (parser);
2688 enum_loc = c_parser_peek_token (parser)->location;
2689 /* Set the location in case we create a decl now. */
2690 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2691 if (c_parser_next_token_is (parser, CPP_NAME))
2693 ident = c_parser_peek_token (parser)->value;
2694 ident_loc = c_parser_peek_token (parser)->location;
2695 enum_loc = ident_loc;
2696 c_parser_consume_token (parser);
2698 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2700 /* Parse an enum definition. */
2701 struct c_enum_contents the_enum;
2702 tree type;
2703 tree postfix_attrs;
2704 /* We chain the enumerators in reverse order, then put them in
2705 forward order at the end. */
2706 tree values;
2707 timevar_push (TV_PARSE_ENUM);
2708 type = start_enum (enum_loc, &the_enum, ident);
2709 values = NULL_TREE;
2710 c_parser_consume_token (parser);
2711 while (true)
2713 tree enum_id;
2714 tree enum_value;
2715 tree enum_decl;
2716 bool seen_comma;
2717 c_token *token;
2718 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2719 location_t decl_loc, value_loc;
2720 if (c_parser_next_token_is_not (parser, CPP_NAME))
2722 /* Give a nicer error for "enum {}". */
2723 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2724 && !parser->error)
2726 error_at (c_parser_peek_token (parser)->location,
2727 "empty enum is invalid");
2728 parser->error = true;
2730 else
2731 c_parser_error (parser, "expected identifier");
2732 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2733 values = error_mark_node;
2734 break;
2736 token = c_parser_peek_token (parser);
2737 enum_id = token->value;
2738 /* Set the location in case we create a decl now. */
2739 c_parser_set_source_position_from_token (token);
2740 decl_loc = value_loc = token->location;
2741 c_parser_consume_token (parser);
2742 /* Parse any specified attributes. */
2743 tree enum_attrs = c_parser_attributes (parser);
2744 if (c_parser_next_token_is (parser, CPP_EQ))
2746 c_parser_consume_token (parser);
2747 value_loc = c_parser_peek_token (parser)->location;
2748 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2750 else
2751 enum_value = NULL_TREE;
2752 enum_decl = build_enumerator (decl_loc, value_loc,
2753 &the_enum, enum_id, enum_value);
2754 if (enum_attrs)
2755 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
2756 TREE_CHAIN (enum_decl) = values;
2757 values = enum_decl;
2758 seen_comma = false;
2759 if (c_parser_next_token_is (parser, CPP_COMMA))
2761 comma_loc = c_parser_peek_token (parser)->location;
2762 seen_comma = true;
2763 c_parser_consume_token (parser);
2765 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2767 if (seen_comma)
2768 pedwarn_c90 (comma_loc, OPT_Wpedantic,
2769 "comma at end of enumerator list");
2770 c_parser_consume_token (parser);
2771 break;
2773 if (!seen_comma)
2775 c_parser_error (parser, "expected %<,%> or %<}%>");
2776 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2777 values = error_mark_node;
2778 break;
2781 postfix_attrs = c_parser_attributes (parser);
2782 ret.spec = finish_enum (type, nreverse (values),
2783 chainon (attrs, postfix_attrs));
2784 ret.kind = ctsk_tagdef;
2785 ret.expr = NULL_TREE;
2786 ret.expr_const_operands = true;
2787 timevar_pop (TV_PARSE_ENUM);
2788 return ret;
2790 else if (!ident)
2792 c_parser_error (parser, "expected %<{%>");
2793 ret.spec = error_mark_node;
2794 ret.kind = ctsk_tagref;
2795 ret.expr = NULL_TREE;
2796 ret.expr_const_operands = true;
2797 return ret;
2799 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
2800 /* In ISO C, enumerated types can be referred to only if already
2801 defined. */
2802 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
2804 gcc_assert (ident);
2805 pedwarn (enum_loc, OPT_Wpedantic,
2806 "ISO C forbids forward references to %<enum%> types");
2808 return ret;
2811 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1).
2813 struct-or-union-specifier:
2814 struct-or-union attributes[opt] identifier[opt]
2815 { struct-contents } attributes[opt]
2816 struct-or-union attributes[opt] identifier
2818 struct-contents:
2819 struct-declaration-list
2821 struct-declaration-list:
2822 struct-declaration ;
2823 struct-declaration-list struct-declaration ;
2825 GNU extensions:
2827 struct-contents:
2828 empty
2829 struct-declaration
2830 struct-declaration-list struct-declaration
2832 struct-declaration-list:
2833 struct-declaration-list ;
2836 (Note that in the syntax here, unlike that in ISO C, the semicolons
2837 are included here rather than in struct-declaration, in order to
2838 describe the syntax with extra semicolons and missing semicolon at
2839 end.)
2841 Objective-C:
2843 struct-declaration-list:
2844 @defs ( class-name )
2846 (Note this does not include a trailing semicolon, but can be
2847 followed by further declarations, and gets a pedwarn-if-pedantic
2848 when followed by a semicolon.) */
2850 static struct c_typespec
2851 c_parser_struct_or_union_specifier (c_parser *parser)
2853 struct c_typespec ret;
2854 tree attrs;
2855 tree ident = NULL_TREE;
2856 location_t struct_loc;
2857 location_t ident_loc = UNKNOWN_LOCATION;
2858 enum tree_code code;
2859 switch (c_parser_peek_token (parser)->keyword)
2861 case RID_STRUCT:
2862 code = RECORD_TYPE;
2863 break;
2864 case RID_UNION:
2865 code = UNION_TYPE;
2866 break;
2867 default:
2868 gcc_unreachable ();
2870 struct_loc = c_parser_peek_token (parser)->location;
2871 c_parser_consume_token (parser);
2872 attrs = c_parser_attributes (parser);
2874 /* Set the location in case we create a decl now. */
2875 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2877 if (c_parser_next_token_is (parser, CPP_NAME))
2879 ident = c_parser_peek_token (parser)->value;
2880 ident_loc = c_parser_peek_token (parser)->location;
2881 struct_loc = ident_loc;
2882 c_parser_consume_token (parser);
2884 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2886 /* Parse a struct or union definition. Start the scope of the
2887 tag before parsing components. */
2888 struct c_struct_parse_info *struct_info;
2889 tree type = start_struct (struct_loc, code, ident, &struct_info);
2890 tree postfix_attrs;
2891 /* We chain the components in reverse order, then put them in
2892 forward order at the end. Each struct-declaration may
2893 declare multiple components (comma-separated), so we must use
2894 chainon to join them, although when parsing each
2895 struct-declaration we can use TREE_CHAIN directly.
2897 The theory behind all this is that there will be more
2898 semicolon separated fields than comma separated fields, and
2899 so we'll be minimizing the number of node traversals required
2900 by chainon. */
2901 tree contents;
2902 timevar_push (TV_PARSE_STRUCT);
2903 contents = NULL_TREE;
2904 c_parser_consume_token (parser);
2905 /* Handle the Objective-C @defs construct,
2906 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
2907 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
2909 tree name;
2910 gcc_assert (c_dialect_objc ());
2911 c_parser_consume_token (parser);
2912 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
2913 goto end_at_defs;
2914 if (c_parser_next_token_is (parser, CPP_NAME)
2915 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
2917 name = c_parser_peek_token (parser)->value;
2918 c_parser_consume_token (parser);
2920 else
2922 c_parser_error (parser, "expected class name");
2923 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
2924 goto end_at_defs;
2926 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2927 "expected %<)%>");
2928 contents = nreverse (objc_get_class_ivars (name));
2930 end_at_defs:
2931 /* Parse the struct-declarations and semicolons. Problems with
2932 semicolons are diagnosed here; empty structures are diagnosed
2933 elsewhere. */
2934 while (true)
2936 tree decls;
2937 /* Parse any stray semicolon. */
2938 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2940 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
2941 "extra semicolon in struct or union specified");
2942 c_parser_consume_token (parser);
2943 continue;
2945 /* Stop if at the end of the struct or union contents. */
2946 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2948 c_parser_consume_token (parser);
2949 break;
2951 /* Accept #pragmas at struct scope. */
2952 if (c_parser_next_token_is (parser, CPP_PRAGMA))
2954 c_parser_pragma (parser, pragma_struct, NULL);
2955 continue;
2957 /* Parse some comma-separated declarations, but not the
2958 trailing semicolon if any. */
2959 decls = c_parser_struct_declaration (parser);
2960 contents = chainon (decls, contents);
2961 /* If no semicolon follows, either we have a parse error or
2962 are at the end of the struct or union and should
2963 pedwarn. */
2964 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2965 c_parser_consume_token (parser);
2966 else
2968 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
2969 pedwarn (c_parser_peek_token (parser)->location, 0,
2970 "no semicolon at end of struct or union");
2971 else if (parser->error
2972 || !c_parser_next_token_starts_declspecs (parser))
2974 c_parser_error (parser, "expected %<;%>");
2975 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2976 break;
2979 /* If we come here, we have already emitted an error
2980 for an expected `;', identifier or `(', and we also
2981 recovered already. Go on with the next field. */
2984 postfix_attrs = c_parser_attributes (parser);
2985 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
2986 chainon (attrs, postfix_attrs), struct_info);
2987 ret.kind = ctsk_tagdef;
2988 ret.expr = NULL_TREE;
2989 ret.expr_const_operands = true;
2990 timevar_pop (TV_PARSE_STRUCT);
2991 return ret;
2993 else if (!ident)
2995 c_parser_error (parser, "expected %<{%>");
2996 ret.spec = error_mark_node;
2997 ret.kind = ctsk_tagref;
2998 ret.expr = NULL_TREE;
2999 ret.expr_const_operands = true;
3000 return ret;
3002 ret = parser_xref_tag (ident_loc, code, ident);
3003 return ret;
3006 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without*
3007 the trailing semicolon.
3009 struct-declaration:
3010 specifier-qualifier-list struct-declarator-list
3011 static_assert-declaration-no-semi
3013 specifier-qualifier-list:
3014 type-specifier specifier-qualifier-list[opt]
3015 type-qualifier specifier-qualifier-list[opt]
3016 attributes specifier-qualifier-list[opt]
3018 struct-declarator-list:
3019 struct-declarator
3020 struct-declarator-list , attributes[opt] struct-declarator
3022 struct-declarator:
3023 declarator attributes[opt]
3024 declarator[opt] : constant-expression attributes[opt]
3026 GNU extensions:
3028 struct-declaration:
3029 __extension__ struct-declaration
3030 specifier-qualifier-list
3032 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3033 of attributes where shown is a GNU extension. In GNU C, we accept
3034 any expression without commas in the syntax (assignment
3035 expressions, not just conditional expressions); assignment
3036 expressions will be diagnosed as non-constant. */
3038 static tree
3039 c_parser_struct_declaration (c_parser *parser)
3041 struct c_declspecs *specs;
3042 tree prefix_attrs;
3043 tree all_prefix_attrs;
3044 tree decls;
3045 location_t decl_loc;
3046 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3048 int ext;
3049 tree decl;
3050 ext = disable_extension_diagnostics ();
3051 c_parser_consume_token (parser);
3052 decl = c_parser_struct_declaration (parser);
3053 restore_extension_diagnostics (ext);
3054 return decl;
3056 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3058 c_parser_static_assert_declaration_no_semi (parser);
3059 return NULL_TREE;
3061 specs = build_null_declspecs ();
3062 decl_loc = c_parser_peek_token (parser)->location;
3063 /* Strictly by the standard, we shouldn't allow _Alignas here,
3064 but it appears to have been intended to allow it there, so
3065 we're keeping it as it is until WG14 reaches a conclusion
3066 of N1731.
3067 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3068 c_parser_declspecs (parser, specs, false, true, true,
3069 true, false, cla_nonabstract_decl);
3070 if (parser->error)
3071 return NULL_TREE;
3072 if (!specs->declspecs_seen_p)
3074 c_parser_error (parser, "expected specifier-qualifier-list");
3075 return NULL_TREE;
3077 finish_declspecs (specs);
3078 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3079 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3081 tree ret;
3082 if (specs->typespec_kind == ctsk_none)
3084 pedwarn (decl_loc, OPT_Wpedantic,
3085 "ISO C forbids member declarations with no members");
3086 shadow_tag_warned (specs, pedantic);
3087 ret = NULL_TREE;
3089 else
3091 /* Support for unnamed structs or unions as members of
3092 structs or unions (which is [a] useful and [b] supports
3093 MS P-SDK). */
3094 tree attrs = NULL;
3096 ret = grokfield (c_parser_peek_token (parser)->location,
3097 build_id_declarator (NULL_TREE), specs,
3098 NULL_TREE, &attrs);
3099 if (ret)
3100 decl_attributes (&ret, attrs, 0);
3102 return ret;
3105 /* Provide better error recovery. Note that a type name here is valid,
3106 and will be treated as a field name. */
3107 if (specs->typespec_kind == ctsk_tagdef
3108 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3109 && c_parser_next_token_starts_declspecs (parser)
3110 && !c_parser_next_token_is (parser, CPP_NAME))
3112 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3113 parser->error = false;
3114 return NULL_TREE;
3117 pending_xref_error ();
3118 prefix_attrs = specs->attrs;
3119 all_prefix_attrs = prefix_attrs;
3120 specs->attrs = NULL_TREE;
3121 decls = NULL_TREE;
3122 while (true)
3124 /* Declaring one or more declarators or un-named bit-fields. */
3125 struct c_declarator *declarator;
3126 bool dummy = false;
3127 if (c_parser_next_token_is (parser, CPP_COLON))
3128 declarator = build_id_declarator (NULL_TREE);
3129 else
3130 declarator = c_parser_declarator (parser,
3131 specs->typespec_kind != ctsk_none,
3132 C_DTR_NORMAL, &dummy);
3133 if (declarator == NULL)
3135 c_parser_skip_to_end_of_block_or_statement (parser);
3136 break;
3138 if (c_parser_next_token_is (parser, CPP_COLON)
3139 || c_parser_next_token_is (parser, CPP_COMMA)
3140 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3141 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3142 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3144 tree postfix_attrs = NULL_TREE;
3145 tree width = NULL_TREE;
3146 tree d;
3147 if (c_parser_next_token_is (parser, CPP_COLON))
3149 c_parser_consume_token (parser);
3150 width = c_parser_expr_no_commas (parser, NULL).value;
3152 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3153 postfix_attrs = c_parser_attributes (parser);
3154 d = grokfield (c_parser_peek_token (parser)->location,
3155 declarator, specs, width, &all_prefix_attrs);
3156 decl_attributes (&d, chainon (postfix_attrs,
3157 all_prefix_attrs), 0);
3158 DECL_CHAIN (d) = decls;
3159 decls = d;
3160 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3161 all_prefix_attrs = chainon (c_parser_attributes (parser),
3162 prefix_attrs);
3163 else
3164 all_prefix_attrs = prefix_attrs;
3165 if (c_parser_next_token_is (parser, CPP_COMMA))
3166 c_parser_consume_token (parser);
3167 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3168 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3170 /* Semicolon consumed in caller. */
3171 break;
3173 else
3175 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3176 break;
3179 else
3181 c_parser_error (parser,
3182 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3183 "%<__attribute__%>");
3184 break;
3187 return decls;
3190 /* Parse a typeof specifier (a GNU extension).
3192 typeof-specifier:
3193 typeof ( expression )
3194 typeof ( type-name )
3197 static struct c_typespec
3198 c_parser_typeof_specifier (c_parser *parser)
3200 struct c_typespec ret;
3201 ret.kind = ctsk_typeof;
3202 ret.spec = error_mark_node;
3203 ret.expr = NULL_TREE;
3204 ret.expr_const_operands = true;
3205 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3206 c_parser_consume_token (parser);
3207 c_inhibit_evaluation_warnings++;
3208 in_typeof++;
3209 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3211 c_inhibit_evaluation_warnings--;
3212 in_typeof--;
3213 return ret;
3215 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3217 struct c_type_name *type = c_parser_type_name (parser);
3218 c_inhibit_evaluation_warnings--;
3219 in_typeof--;
3220 if (type != NULL)
3222 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3223 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3226 else
3228 bool was_vm;
3229 location_t here = c_parser_peek_token (parser)->location;
3230 struct c_expr expr = c_parser_expression (parser);
3231 c_inhibit_evaluation_warnings--;
3232 in_typeof--;
3233 if (TREE_CODE (expr.value) == COMPONENT_REF
3234 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3235 error_at (here, "%<typeof%> applied to a bit-field");
3236 mark_exp_read (expr.value);
3237 ret.spec = TREE_TYPE (expr.value);
3238 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3239 /* This is returned with the type so that when the type is
3240 evaluated, this can be evaluated. */
3241 if (was_vm)
3242 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3243 pop_maybe_used (was_vm);
3244 /* For use in macros such as those in <stdatomic.h>, remove all
3245 qualifiers from atomic types. (const can be an issue for more macros
3246 using typeof than just the <stdatomic.h> ones.) */
3247 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3248 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3250 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3251 return ret;
3254 /* Parse an alignment-specifier.
3256 C11 6.7.5:
3258 alignment-specifier:
3259 _Alignas ( type-name )
3260 _Alignas ( constant-expression )
3263 static tree
3264 c_parser_alignas_specifier (c_parser * parser)
3266 tree ret = error_mark_node;
3267 location_t loc = c_parser_peek_token (parser)->location;
3268 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3269 c_parser_consume_token (parser);
3270 if (flag_isoc99)
3271 pedwarn_c99 (loc, OPT_Wpedantic,
3272 "ISO C99 does not support %<_Alignas%>");
3273 else
3274 pedwarn_c99 (loc, OPT_Wpedantic,
3275 "ISO C90 does not support %<_Alignas%>");
3276 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3277 return ret;
3278 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3280 struct c_type_name *type = c_parser_type_name (parser);
3281 if (type != NULL)
3282 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3283 false, true, 1);
3285 else
3286 ret = c_parser_expr_no_commas (parser, NULL).value;
3287 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
3288 return ret;
3291 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3292 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may
3293 be redeclared; otherwise it may not. KIND indicates which kind of
3294 declarator is wanted. Returns a valid declarator except in the
3295 case of a syntax error in which case NULL is returned. *SEEN_ID is
3296 set to true if an identifier being declared is seen; this is used
3297 to diagnose bad forms of abstract array declarators and to
3298 determine whether an identifier list is syntactically permitted.
3300 declarator:
3301 pointer[opt] direct-declarator
3303 direct-declarator:
3304 identifier
3305 ( attributes[opt] declarator )
3306 direct-declarator array-declarator
3307 direct-declarator ( parameter-type-list )
3308 direct-declarator ( identifier-list[opt] )
3310 pointer:
3311 * type-qualifier-list[opt]
3312 * type-qualifier-list[opt] pointer
3314 type-qualifier-list:
3315 type-qualifier
3316 attributes
3317 type-qualifier-list type-qualifier
3318 type-qualifier-list attributes
3320 array-declarator:
3321 [ type-qualifier-list[opt] assignment-expression[opt] ]
3322 [ static type-qualifier-list[opt] assignment-expression ]
3323 [ type-qualifier-list static assignment-expression ]
3324 [ type-qualifier-list[opt] * ]
3326 parameter-type-list:
3327 parameter-list
3328 parameter-list , ...
3330 parameter-list:
3331 parameter-declaration
3332 parameter-list , parameter-declaration
3334 parameter-declaration:
3335 declaration-specifiers declarator attributes[opt]
3336 declaration-specifiers abstract-declarator[opt] attributes[opt]
3338 identifier-list:
3339 identifier
3340 identifier-list , identifier
3342 abstract-declarator:
3343 pointer
3344 pointer[opt] direct-abstract-declarator
3346 direct-abstract-declarator:
3347 ( attributes[opt] abstract-declarator )
3348 direct-abstract-declarator[opt] array-declarator
3349 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3351 GNU extensions:
3353 direct-declarator:
3354 direct-declarator ( parameter-forward-declarations
3355 parameter-type-list[opt] )
3357 direct-abstract-declarator:
3358 direct-abstract-declarator[opt] ( parameter-forward-declarations
3359 parameter-type-list[opt] )
3361 parameter-forward-declarations:
3362 parameter-list ;
3363 parameter-forward-declarations parameter-list ;
3365 The uses of attributes shown above are GNU extensions.
3367 Some forms of array declarator are not included in C99 in the
3368 syntax for abstract declarators; these are disallowed elsewhere.
3369 This may be a defect (DR#289).
3371 This function also accepts an omitted abstract declarator as being
3372 an abstract declarator, although not part of the formal syntax. */
3374 struct c_declarator *
3375 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3376 bool *seen_id)
3378 /* Parse any initial pointer part. */
3379 if (c_parser_next_token_is (parser, CPP_MULT))
3381 struct c_declspecs *quals_attrs = build_null_declspecs ();
3382 struct c_declarator *inner;
3383 c_parser_consume_token (parser);
3384 c_parser_declspecs (parser, quals_attrs, false, false, true,
3385 false, false, cla_prefer_id);
3386 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3387 if (inner == NULL)
3388 return NULL;
3389 else
3390 return make_pointer_declarator (quals_attrs, inner);
3392 /* Now we have a direct declarator, direct abstract declarator or
3393 nothing (which counts as a direct abstract declarator here). */
3394 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3397 /* Parse a direct declarator or direct abstract declarator; arguments
3398 as c_parser_declarator. */
3400 static struct c_declarator *
3401 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3402 bool *seen_id)
3404 /* The direct declarator must start with an identifier (possibly
3405 omitted) or a parenthesized declarator (possibly abstract). In
3406 an ordinary declarator, initial parentheses must start a
3407 parenthesized declarator. In an abstract declarator or parameter
3408 declarator, they could start a parenthesized declarator or a
3409 parameter list. To tell which, the open parenthesis and any
3410 following attributes must be read. If a declaration specifier
3411 follows, then it is a parameter list; if the specifier is a
3412 typedef name, there might be an ambiguity about redeclaring it,
3413 which is resolved in the direction of treating it as a typedef
3414 name. If a close parenthesis follows, it is also an empty
3415 parameter list, as the syntax does not permit empty abstract
3416 declarators. Otherwise, it is a parenthesized declarator (in
3417 which case the analysis may be repeated inside it, recursively).
3419 ??? There is an ambiguity in a parameter declaration "int
3420 (__attribute__((foo)) x)", where x is not a typedef name: it
3421 could be an abstract declarator for a function, or declare x with
3422 parentheses. The proper resolution of this ambiguity needs
3423 documenting. At present we follow an accident of the old
3424 parser's implementation, whereby the first parameter must have
3425 some declaration specifiers other than just attributes. Thus as
3426 a parameter declaration it is treated as a parenthesized
3427 parameter named x, and as an abstract declarator it is
3428 rejected.
3430 ??? Also following the old parser, attributes inside an empty
3431 parameter list are ignored, making it a list not yielding a
3432 prototype, rather than giving an error or making it have one
3433 parameter with implicit type int.
3435 ??? Also following the old parser, typedef names may be
3436 redeclared in declarators, but not Objective-C class names. */
3438 if (kind != C_DTR_ABSTRACT
3439 && c_parser_next_token_is (parser, CPP_NAME)
3440 && ((type_seen_p
3441 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3442 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3443 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3445 struct c_declarator *inner
3446 = build_id_declarator (c_parser_peek_token (parser)->value);
3447 *seen_id = true;
3448 inner->id_loc = c_parser_peek_token (parser)->location;
3449 c_parser_consume_token (parser);
3450 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3453 if (kind != C_DTR_NORMAL
3454 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3456 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3457 inner->id_loc = c_parser_peek_token (parser)->location;
3458 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3461 /* Either we are at the end of an abstract declarator, or we have
3462 parentheses. */
3464 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3466 tree attrs;
3467 struct c_declarator *inner;
3468 c_parser_consume_token (parser);
3469 attrs = c_parser_attributes (parser);
3470 if (kind != C_DTR_NORMAL
3471 && (c_parser_next_token_starts_declspecs (parser)
3472 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3474 struct c_arg_info *args
3475 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3476 attrs);
3477 if (args == NULL)
3478 return NULL;
3479 else
3481 inner
3482 = build_function_declarator (args,
3483 build_id_declarator (NULL_TREE));
3484 return c_parser_direct_declarator_inner (parser, *seen_id,
3485 inner);
3488 /* A parenthesized declarator. */
3489 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3490 if (inner != NULL && attrs != NULL)
3491 inner = build_attrs_declarator (attrs, inner);
3492 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3494 c_parser_consume_token (parser);
3495 if (inner == NULL)
3496 return NULL;
3497 else
3498 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3500 else
3502 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3503 "expected %<)%>");
3504 return NULL;
3507 else
3509 if (kind == C_DTR_NORMAL)
3511 c_parser_error (parser, "expected identifier or %<(%>");
3512 return NULL;
3514 else
3515 return build_id_declarator (NULL_TREE);
3519 /* Parse part of a direct declarator or direct abstract declarator,
3520 given that some (in INNER) has already been parsed; ID_PRESENT is
3521 true if an identifier is present, false for an abstract
3522 declarator. */
3524 static struct c_declarator *
3525 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3526 struct c_declarator *inner)
3528 /* Parse a sequence of array declarators and parameter lists. */
3529 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3531 location_t brace_loc = c_parser_peek_token (parser)->location;
3532 struct c_declarator *declarator;
3533 struct c_declspecs *quals_attrs = build_null_declspecs ();
3534 bool static_seen;
3535 bool star_seen;
3536 struct c_expr dimen;
3537 dimen.value = NULL_TREE;
3538 dimen.original_code = ERROR_MARK;
3539 dimen.original_type = NULL_TREE;
3540 c_parser_consume_token (parser);
3541 c_parser_declspecs (parser, quals_attrs, false, false, true,
3542 false, false, cla_prefer_id);
3543 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3544 if (static_seen)
3545 c_parser_consume_token (parser);
3546 if (static_seen && !quals_attrs->declspecs_seen_p)
3547 c_parser_declspecs (parser, quals_attrs, false, false, true,
3548 false, false, cla_prefer_id);
3549 if (!quals_attrs->declspecs_seen_p)
3550 quals_attrs = NULL;
3551 /* If "static" is present, there must be an array dimension.
3552 Otherwise, there may be a dimension, "*", or no
3553 dimension. */
3554 if (static_seen)
3556 star_seen = false;
3557 dimen = c_parser_expr_no_commas (parser, NULL);
3559 else
3561 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3563 dimen.value = NULL_TREE;
3564 star_seen = false;
3566 else if (flag_cilkplus
3567 && c_parser_next_token_is (parser, CPP_COLON))
3569 dimen.value = error_mark_node;
3570 star_seen = false;
3571 error_at (c_parser_peek_token (parser)->location,
3572 "array notations cannot be used in declaration");
3573 c_parser_consume_token (parser);
3575 else if (c_parser_next_token_is (parser, CPP_MULT))
3577 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3579 dimen.value = NULL_TREE;
3580 star_seen = true;
3581 c_parser_consume_token (parser);
3583 else
3585 star_seen = false;
3586 dimen = c_parser_expr_no_commas (parser, NULL);
3589 else
3591 star_seen = false;
3592 dimen = c_parser_expr_no_commas (parser, NULL);
3595 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3596 c_parser_consume_token (parser);
3597 else if (flag_cilkplus
3598 && c_parser_next_token_is (parser, CPP_COLON))
3600 error_at (c_parser_peek_token (parser)->location,
3601 "array notations cannot be used in declaration");
3602 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
3603 return NULL;
3605 else
3607 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3608 "expected %<]%>");
3609 return NULL;
3611 if (dimen.value)
3612 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3613 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3614 static_seen, star_seen);
3615 if (declarator == NULL)
3616 return NULL;
3617 inner = set_array_declarator_inner (declarator, inner);
3618 return c_parser_direct_declarator_inner (parser, id_present, inner);
3620 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3622 tree attrs;
3623 struct c_arg_info *args;
3624 c_parser_consume_token (parser);
3625 attrs = c_parser_attributes (parser);
3626 args = c_parser_parms_declarator (parser, id_present, attrs);
3627 if (args == NULL)
3628 return NULL;
3629 else
3631 inner = build_function_declarator (args, inner);
3632 return c_parser_direct_declarator_inner (parser, id_present, inner);
3635 return inner;
3638 /* Parse a parameter list or identifier list, including the closing
3639 parenthesis but not the opening one. ATTRS are the attributes at
3640 the start of the list. ID_LIST_OK is true if an identifier list is
3641 acceptable; such a list must not have attributes at the start. */
3643 static struct c_arg_info *
3644 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3646 push_scope ();
3647 declare_parm_level ();
3648 /* If the list starts with an identifier, it is an identifier list.
3649 Otherwise, it is either a prototype list or an empty list. */
3650 if (id_list_ok
3651 && !attrs
3652 && c_parser_next_token_is (parser, CPP_NAME)
3653 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3655 /* Look ahead to detect typos in type names. */
3656 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3657 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3658 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3659 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3660 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3662 tree list = NULL_TREE, *nextp = &list;
3663 while (c_parser_next_token_is (parser, CPP_NAME)
3664 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3666 *nextp = build_tree_list (NULL_TREE,
3667 c_parser_peek_token (parser)->value);
3668 nextp = & TREE_CHAIN (*nextp);
3669 c_parser_consume_token (parser);
3670 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3671 break;
3672 c_parser_consume_token (parser);
3673 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3675 c_parser_error (parser, "expected identifier");
3676 break;
3679 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3681 struct c_arg_info *ret = build_arg_info ();
3682 ret->types = list;
3683 c_parser_consume_token (parser);
3684 pop_scope ();
3685 return ret;
3687 else
3689 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3690 "expected %<)%>");
3691 pop_scope ();
3692 return NULL;
3695 else
3697 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3698 NULL);
3699 pop_scope ();
3700 return ret;
3704 /* Parse a parameter list (possibly empty), including the closing
3705 parenthesis but not the opening one. ATTRS are the attributes at
3706 the start of the list. EXPR is NULL or an expression that needs to
3707 be evaluated for the side effects of array size expressions in the
3708 parameters. */
3710 static struct c_arg_info *
3711 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3713 bool bad_parm = false;
3715 /* ??? Following the old parser, forward parameter declarations may
3716 use abstract declarators, and if no real parameter declarations
3717 follow the forward declarations then this is not diagnosed. Also
3718 note as above that attributes are ignored as the only contents of
3719 the parentheses, or as the only contents after forward
3720 declarations. */
3721 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3723 struct c_arg_info *ret = build_arg_info ();
3724 c_parser_consume_token (parser);
3725 return ret;
3727 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3729 struct c_arg_info *ret = build_arg_info ();
3731 if (flag_allow_parameterless_variadic_functions)
3733 /* F (...) is allowed. */
3734 ret->types = NULL_TREE;
3736 else
3738 /* Suppress -Wold-style-definition for this case. */
3739 ret->types = error_mark_node;
3740 error_at (c_parser_peek_token (parser)->location,
3741 "ISO C requires a named argument before %<...%>");
3743 c_parser_consume_token (parser);
3744 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3746 c_parser_consume_token (parser);
3747 return ret;
3749 else
3751 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3752 "expected %<)%>");
3753 return NULL;
3756 /* Nonempty list of parameters, either terminated with semicolon
3757 (forward declarations; recurse) or with close parenthesis (normal
3758 function) or with ", ... )" (variadic function). */
3759 while (true)
3761 /* Parse a parameter. */
3762 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
3763 attrs = NULL_TREE;
3764 if (parm == NULL)
3765 bad_parm = true;
3766 else
3767 push_parm_decl (parm, &expr);
3768 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3770 tree new_attrs;
3771 c_parser_consume_token (parser);
3772 mark_forward_parm_decls ();
3773 new_attrs = c_parser_attributes (parser);
3774 return c_parser_parms_list_declarator (parser, new_attrs, expr);
3776 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3778 c_parser_consume_token (parser);
3779 if (bad_parm)
3780 return NULL;
3781 else
3782 return get_parm_info (false, expr);
3784 if (!c_parser_require (parser, CPP_COMMA,
3785 "expected %<;%>, %<,%> or %<)%>"))
3787 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3788 return NULL;
3790 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3792 c_parser_consume_token (parser);
3793 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3795 c_parser_consume_token (parser);
3796 if (bad_parm)
3797 return NULL;
3798 else
3799 return get_parm_info (true, expr);
3801 else
3803 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3804 "expected %<)%>");
3805 return NULL;
3811 /* Parse a parameter declaration. ATTRS are the attributes at the
3812 start of the declaration if it is the first parameter. */
3814 static struct c_parm *
3815 c_parser_parameter_declaration (c_parser *parser, tree attrs)
3817 struct c_declspecs *specs;
3818 struct c_declarator *declarator;
3819 tree prefix_attrs;
3820 tree postfix_attrs = NULL_TREE;
3821 bool dummy = false;
3823 /* Accept #pragmas between parameter declarations. */
3824 while (c_parser_next_token_is (parser, CPP_PRAGMA))
3825 c_parser_pragma (parser, pragma_param, NULL);
3827 if (!c_parser_next_token_starts_declspecs (parser))
3829 c_token *token = c_parser_peek_token (parser);
3830 if (parser->error)
3831 return NULL;
3832 c_parser_set_source_position_from_token (token);
3833 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
3835 const char *hint = lookup_name_fuzzy (token->value,
3836 FUZZY_LOOKUP_TYPENAME);
3837 if (hint)
3839 gcc_rich_location richloc (token->location);
3840 richloc.add_fixit_replace (hint);
3841 error_at_rich_loc (&richloc,
3842 "unknown type name %qE; did you mean %qs?",
3843 token->value, hint);
3845 else
3846 error_at (token->location, "unknown type name %qE", token->value);
3847 parser->error = true;
3849 /* ??? In some Objective-C cases '...' isn't applicable so there
3850 should be a different message. */
3851 else
3852 c_parser_error (parser,
3853 "expected declaration specifiers or %<...%>");
3854 c_parser_skip_to_end_of_parameter (parser);
3855 return NULL;
3857 specs = build_null_declspecs ();
3858 if (attrs)
3860 declspecs_add_attrs (input_location, specs, attrs);
3861 attrs = NULL_TREE;
3863 c_parser_declspecs (parser, specs, true, true, true, true, false,
3864 cla_nonabstract_decl);
3865 finish_declspecs (specs);
3866 pending_xref_error ();
3867 prefix_attrs = specs->attrs;
3868 specs->attrs = NULL_TREE;
3869 declarator = c_parser_declarator (parser,
3870 specs->typespec_kind != ctsk_none,
3871 C_DTR_PARM, &dummy);
3872 if (declarator == NULL)
3874 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
3875 return NULL;
3877 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3878 postfix_attrs = c_parser_attributes (parser);
3879 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
3880 declarator);
3883 /* Parse a string literal in an asm expression. It should not be
3884 translated, and wide string literals are an error although
3885 permitted by the syntax. This is a GNU extension.
3887 asm-string-literal:
3888 string-literal
3890 ??? At present, following the old parser, the caller needs to have
3891 set lex_untranslated_string to 1. It would be better to follow the
3892 C++ parser rather than using this kludge. */
3894 static tree
3895 c_parser_asm_string_literal (c_parser *parser)
3897 tree str;
3898 int save_flag = warn_overlength_strings;
3899 warn_overlength_strings = 0;
3900 if (c_parser_next_token_is (parser, CPP_STRING))
3902 str = c_parser_peek_token (parser)->value;
3903 c_parser_consume_token (parser);
3905 else if (c_parser_next_token_is (parser, CPP_WSTRING))
3907 error_at (c_parser_peek_token (parser)->location,
3908 "wide string literal in %<asm%>");
3909 str = build_string (1, "");
3910 c_parser_consume_token (parser);
3912 else
3914 c_parser_error (parser, "expected string literal");
3915 str = NULL_TREE;
3917 warn_overlength_strings = save_flag;
3918 return str;
3921 /* Parse a simple asm expression. This is used in restricted
3922 contexts, where a full expression with inputs and outputs does not
3923 make sense. This is a GNU extension.
3925 simple-asm-expr:
3926 asm ( asm-string-literal )
3929 static tree
3930 c_parser_simple_asm_expr (c_parser *parser)
3932 tree str;
3933 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
3934 /* ??? Follow the C++ parser rather than using the
3935 lex_untranslated_string kludge. */
3936 parser->lex_untranslated_string = true;
3937 c_parser_consume_token (parser);
3938 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
3940 parser->lex_untranslated_string = false;
3941 return NULL_TREE;
3943 str = c_parser_asm_string_literal (parser);
3944 parser->lex_untranslated_string = false;
3945 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
3947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3948 return NULL_TREE;
3950 return str;
3953 static tree
3954 c_parser_attribute_any_word (c_parser *parser)
3956 tree attr_name = NULL_TREE;
3958 if (c_parser_next_token_is (parser, CPP_KEYWORD))
3960 /* ??? See comment above about what keywords are accepted here. */
3961 bool ok;
3962 switch (c_parser_peek_token (parser)->keyword)
3964 case RID_STATIC:
3965 case RID_UNSIGNED:
3966 case RID_LONG:
3967 case RID_CONST:
3968 case RID_EXTERN:
3969 case RID_REGISTER:
3970 case RID_TYPEDEF:
3971 case RID_SHORT:
3972 case RID_INLINE:
3973 case RID_NORETURN:
3974 case RID_VOLATILE:
3975 case RID_SIGNED:
3976 case RID_AUTO:
3977 case RID_RESTRICT:
3978 case RID_COMPLEX:
3979 case RID_THREAD:
3980 case RID_INT:
3981 case RID_CHAR:
3982 case RID_FLOAT:
3983 case RID_DOUBLE:
3984 case RID_VOID:
3985 case RID_DFLOAT32:
3986 case RID_DFLOAT64:
3987 case RID_DFLOAT128:
3988 CASE_RID_FLOATN_NX:
3989 case RID_BOOL:
3990 case RID_FRACT:
3991 case RID_ACCUM:
3992 case RID_SAT:
3993 case RID_TRANSACTION_ATOMIC:
3994 case RID_TRANSACTION_CANCEL:
3995 case RID_ATOMIC:
3996 case RID_AUTO_TYPE:
3997 case RID_INT_N_0:
3998 case RID_INT_N_1:
3999 case RID_INT_N_2:
4000 case RID_INT_N_3:
4001 ok = true;
4002 break;
4003 default:
4004 ok = false;
4005 break;
4007 if (!ok)
4008 return NULL_TREE;
4010 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4011 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4013 else if (c_parser_next_token_is (parser, CPP_NAME))
4014 attr_name = c_parser_peek_token (parser)->value;
4016 return attr_name;
4019 #define CILK_SIMD_FN_CLAUSE_MASK \
4020 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
4021 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
4022 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
4023 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
4024 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
4026 /* Parses the vector attribute of SIMD enabled functions in Cilk Plus.
4027 VEC_TOKEN is the "vector" token that is replaced with "simd" and
4028 pushed into the token list.
4029 Syntax:
4030 vector
4031 vector (<vector attributes>). */
4033 static void
4034 c_parser_cilk_simd_fn_vector_attrs (c_parser *parser, c_token vec_token)
4036 gcc_assert (is_cilkplus_vector_p (vec_token.value));
4038 int paren_scope = 0;
4039 vec_safe_push (parser->cilk_simd_fn_tokens, vec_token);
4040 /* Consume the "vector" token. */
4041 c_parser_consume_token (parser);
4043 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
4045 c_parser_consume_token (parser);
4046 paren_scope++;
4048 while (paren_scope > 0)
4050 c_token *token = c_parser_peek_token (parser);
4051 if (token->type == CPP_OPEN_PAREN)
4052 paren_scope++;
4053 else if (token->type == CPP_CLOSE_PAREN)
4054 paren_scope--;
4055 /* Do not push the last ')' since we are not pushing the '('. */
4056 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
4057 vec_safe_push (parser->cilk_simd_fn_tokens, *token);
4058 c_parser_consume_token (parser);
4061 /* Since we are converting an attribute to a pragma, we need to end the
4062 attribute with PRAGMA_EOL. */
4063 c_token eol_token;
4064 memset (&eol_token, 0, sizeof (eol_token));
4065 eol_token.type = CPP_PRAGMA_EOL;
4066 vec_safe_push (parser->cilk_simd_fn_tokens, eol_token);
4069 /* Add 2 CPP_EOF at the end of PARSER->ELEM_FN_TOKENS vector. */
4071 static void
4072 c_finish_cilk_simd_fn_tokens (c_parser *parser)
4074 c_token last_token = parser->cilk_simd_fn_tokens->last ();
4076 /* c_parser_attributes is called in several places, so if these EOF
4077 tokens are already inserted, then don't do them again. */
4078 if (last_token.type == CPP_EOF)
4079 return;
4081 /* Two CPP_EOF token are added as a safety net since the normal C
4082 front-end has two token look-ahead. */
4083 c_token eof_token;
4084 eof_token.type = CPP_EOF;
4085 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4086 vec_safe_push (parser->cilk_simd_fn_tokens, eof_token);
4089 /* Parse (possibly empty) attributes. This is a GNU extension.
4091 attributes:
4092 empty
4093 attributes attribute
4095 attribute:
4096 __attribute__ ( ( attribute-list ) )
4098 attribute-list:
4099 attrib
4100 attribute_list , attrib
4102 attrib:
4103 empty
4104 any-word
4105 any-word ( identifier )
4106 any-word ( identifier , nonempty-expr-list )
4107 any-word ( expr-list )
4109 where the "identifier" must not be declared as a type, and
4110 "any-word" may be any identifier (including one declared as a
4111 type), a reserved word storage class specifier, type specifier or
4112 type qualifier. ??? This still leaves out most reserved keywords
4113 (following the old parser), shouldn't we include them, and why not
4114 allow identifiers declared as types to start the arguments? */
4116 static tree
4117 c_parser_attributes (c_parser *parser)
4119 tree attrs = NULL_TREE;
4120 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4122 /* ??? Follow the C++ parser rather than using the
4123 lex_untranslated_string kludge. */
4124 parser->lex_untranslated_string = true;
4125 /* Consume the `__attribute__' keyword. */
4126 c_parser_consume_token (parser);
4127 /* Look for the two `(' tokens. */
4128 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4130 parser->lex_untranslated_string = false;
4131 return attrs;
4133 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4135 parser->lex_untranslated_string = false;
4136 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4137 return attrs;
4139 /* Parse the attribute list. */
4140 while (c_parser_next_token_is (parser, CPP_COMMA)
4141 || c_parser_next_token_is (parser, CPP_NAME)
4142 || c_parser_next_token_is (parser, CPP_KEYWORD))
4144 tree attr, attr_name, attr_args;
4145 vec<tree, va_gc> *expr_list;
4146 if (c_parser_next_token_is (parser, CPP_COMMA))
4148 c_parser_consume_token (parser);
4149 continue;
4152 attr_name = c_parser_attribute_any_word (parser);
4153 if (attr_name == NULL)
4154 break;
4155 if (is_cilkplus_vector_p (attr_name))
4157 c_token *v_token = c_parser_peek_token (parser);
4158 c_parser_cilk_simd_fn_vector_attrs (parser, *v_token);
4159 /* If the next token isn't a comma, we're done. */
4160 if (!c_parser_next_token_is (parser, CPP_COMMA))
4161 break;
4162 continue;
4164 c_parser_consume_token (parser);
4165 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4167 attr = build_tree_list (attr_name, NULL_TREE);
4168 /* Add this attribute to the list. */
4169 attrs = chainon (attrs, attr);
4170 /* If the next token isn't a comma, we're done. */
4171 if (!c_parser_next_token_is (parser, CPP_COMMA))
4172 break;
4173 continue;
4175 c_parser_consume_token (parser);
4176 /* Parse the attribute contents. If they start with an
4177 identifier which is followed by a comma or close
4178 parenthesis, then the arguments start with that
4179 identifier; otherwise they are an expression list.
4180 In objective-c the identifier may be a classname. */
4181 if (c_parser_next_token_is (parser, CPP_NAME)
4182 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4183 || (c_dialect_objc ()
4184 && c_parser_peek_token (parser)->id_kind
4185 == C_ID_CLASSNAME))
4186 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4187 || (c_parser_peek_2nd_token (parser)->type
4188 == CPP_CLOSE_PAREN))
4189 && (attribute_takes_identifier_p (attr_name)
4190 || (c_dialect_objc ()
4191 && c_parser_peek_token (parser)->id_kind
4192 == C_ID_CLASSNAME)))
4194 tree arg1 = c_parser_peek_token (parser)->value;
4195 c_parser_consume_token (parser);
4196 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4197 attr_args = build_tree_list (NULL_TREE, arg1);
4198 else
4200 tree tree_list;
4201 c_parser_consume_token (parser);
4202 expr_list = c_parser_expr_list (parser, false, true,
4203 NULL, NULL, NULL, NULL);
4204 tree_list = build_tree_list_vec (expr_list);
4205 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4206 release_tree_vector (expr_list);
4209 else
4211 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4212 attr_args = NULL_TREE;
4213 else
4215 expr_list = c_parser_expr_list (parser, false, true,
4216 NULL, NULL, NULL, NULL);
4217 attr_args = build_tree_list_vec (expr_list);
4218 release_tree_vector (expr_list);
4221 attr = build_tree_list (attr_name, attr_args);
4222 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4223 c_parser_consume_token (parser);
4224 else
4226 parser->lex_untranslated_string = false;
4227 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4228 "expected %<)%>");
4229 return attrs;
4231 /* Add this attribute to the list. */
4232 attrs = chainon (attrs, attr);
4233 /* If the next token isn't a comma, we're done. */
4234 if (!c_parser_next_token_is (parser, CPP_COMMA))
4235 break;
4237 /* Look for the two `)' tokens. */
4238 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4239 c_parser_consume_token (parser);
4240 else
4242 parser->lex_untranslated_string = false;
4243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4244 "expected %<)%>");
4245 return attrs;
4247 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4248 c_parser_consume_token (parser);
4249 else
4251 parser->lex_untranslated_string = false;
4252 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4253 "expected %<)%>");
4254 return attrs;
4256 parser->lex_untranslated_string = false;
4259 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
4260 c_finish_cilk_simd_fn_tokens (parser);
4261 return attrs;
4264 /* Parse a type name (C90 6.5.5, C99 6.7.6).
4266 type-name:
4267 specifier-qualifier-list abstract-declarator[opt]
4270 struct c_type_name *
4271 c_parser_type_name (c_parser *parser)
4273 struct c_declspecs *specs = build_null_declspecs ();
4274 struct c_declarator *declarator;
4275 struct c_type_name *ret;
4276 bool dummy = false;
4277 c_parser_declspecs (parser, specs, false, true, true, false, false,
4278 cla_prefer_type);
4279 if (!specs->declspecs_seen_p)
4281 c_parser_error (parser, "expected specifier-qualifier-list");
4282 return NULL;
4284 if (specs->type != error_mark_node)
4286 pending_xref_error ();
4287 finish_declspecs (specs);
4289 declarator = c_parser_declarator (parser,
4290 specs->typespec_kind != ctsk_none,
4291 C_DTR_ABSTRACT, &dummy);
4292 if (declarator == NULL)
4293 return NULL;
4294 ret = XOBNEW (&parser_obstack, struct c_type_name);
4295 ret->specs = specs;
4296 ret->declarator = declarator;
4297 return ret;
4300 /* Parse an initializer (C90 6.5.7, C99 6.7.8).
4302 initializer:
4303 assignment-expression
4304 { initializer-list }
4305 { initializer-list , }
4307 initializer-list:
4308 designation[opt] initializer
4309 initializer-list , designation[opt] initializer
4311 designation:
4312 designator-list =
4314 designator-list:
4315 designator
4316 designator-list designator
4318 designator:
4319 array-designator
4320 . identifier
4322 array-designator:
4323 [ constant-expression ]
4325 GNU extensions:
4327 initializer:
4330 designation:
4331 array-designator
4332 identifier :
4334 array-designator:
4335 [ constant-expression ... constant-expression ]
4337 Any expression without commas is accepted in the syntax for the
4338 constant-expressions, with non-constant expressions rejected later.
4340 This function is only used for top-level initializers; for nested
4341 ones, see c_parser_initval. */
4343 static struct c_expr
4344 c_parser_initializer (c_parser *parser)
4346 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4347 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4348 else
4350 struct c_expr ret;
4351 location_t loc = c_parser_peek_token (parser)->location;
4352 ret = c_parser_expr_no_commas (parser, NULL);
4353 if (TREE_CODE (ret.value) != STRING_CST
4354 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4355 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4356 return ret;
4360 /* The location of the last comma within the current initializer list,
4361 or UNKNOWN_LOCATION if not within one. */
4363 location_t last_init_list_comma;
4365 /* Parse a braced initializer list. TYPE is the type specified for a
4366 compound literal, and NULL_TREE for other initializers and for
4367 nested braced lists. NESTED_P is true for nested braced lists,
4368 false for the list of a compound literal or the list that is the
4369 top-level initializer in a declaration. */
4371 static struct c_expr
4372 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4373 struct obstack *outer_obstack)
4375 struct c_expr ret;
4376 struct obstack braced_init_obstack;
4377 location_t brace_loc = c_parser_peek_token (parser)->location;
4378 gcc_obstack_init (&braced_init_obstack);
4379 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4380 c_parser_consume_token (parser);
4381 if (nested_p)
4383 finish_implicit_inits (brace_loc, outer_obstack);
4384 push_init_level (brace_loc, 0, &braced_init_obstack);
4386 else
4387 really_start_incremental_init (type);
4388 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4390 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4392 else
4394 /* Parse a non-empty initializer list, possibly with a trailing
4395 comma. */
4396 while (true)
4398 c_parser_initelt (parser, &braced_init_obstack);
4399 if (parser->error)
4400 break;
4401 if (c_parser_next_token_is (parser, CPP_COMMA))
4403 last_init_list_comma = c_parser_peek_token (parser)->location;
4404 c_parser_consume_token (parser);
4406 else
4407 break;
4408 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4409 break;
4412 c_token *next_tok = c_parser_peek_token (parser);
4413 if (next_tok->type != CPP_CLOSE_BRACE)
4415 ret.value = error_mark_node;
4416 ret.original_code = ERROR_MARK;
4417 ret.original_type = NULL;
4418 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>");
4419 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4420 obstack_free (&braced_init_obstack, NULL);
4421 return ret;
4423 location_t close_loc = next_tok->location;
4424 c_parser_consume_token (parser);
4425 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4426 obstack_free (&braced_init_obstack, NULL);
4427 set_c_expr_source_range (&ret, brace_loc, close_loc);
4428 return ret;
4431 /* Parse a nested initializer, including designators. */
4433 static void
4434 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4436 /* Parse any designator or designator list. A single array
4437 designator may have the subsequent "=" omitted in GNU C, but a
4438 longer list or a structure member designator may not. */
4439 if (c_parser_next_token_is (parser, CPP_NAME)
4440 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4442 /* Old-style structure member designator. */
4443 set_init_label (c_parser_peek_token (parser)->location,
4444 c_parser_peek_token (parser)->value,
4445 c_parser_peek_token (parser)->location,
4446 braced_init_obstack);
4447 /* Use the colon as the error location. */
4448 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4449 "obsolete use of designated initializer with %<:%>");
4450 c_parser_consume_token (parser);
4451 c_parser_consume_token (parser);
4453 else
4455 /* des_seen is 0 if there have been no designators, 1 if there
4456 has been a single array designator and 2 otherwise. */
4457 int des_seen = 0;
4458 /* Location of a designator. */
4459 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4460 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4461 || c_parser_next_token_is (parser, CPP_DOT))
4463 int des_prev = des_seen;
4464 if (!des_seen)
4465 des_loc = c_parser_peek_token (parser)->location;
4466 if (des_seen < 2)
4467 des_seen++;
4468 if (c_parser_next_token_is (parser, CPP_DOT))
4470 des_seen = 2;
4471 c_parser_consume_token (parser);
4472 if (c_parser_next_token_is (parser, CPP_NAME))
4474 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4475 c_parser_peek_token (parser)->location,
4476 braced_init_obstack);
4477 c_parser_consume_token (parser);
4479 else
4481 struct c_expr init;
4482 init.value = error_mark_node;
4483 init.original_code = ERROR_MARK;
4484 init.original_type = NULL;
4485 c_parser_error (parser, "expected identifier");
4486 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4487 process_init_element (input_location, init, false,
4488 braced_init_obstack);
4489 return;
4492 else
4494 tree first, second;
4495 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4496 location_t array_index_loc = UNKNOWN_LOCATION;
4497 /* ??? Following the old parser, [ objc-receiver
4498 objc-message-args ] is accepted as an initializer,
4499 being distinguished from a designator by what follows
4500 the first assignment expression inside the square
4501 brackets, but after a first array designator a
4502 subsequent square bracket is for Objective-C taken to
4503 start an expression, using the obsolete form of
4504 designated initializer without '=', rather than
4505 possibly being a second level of designation: in LALR
4506 terms, the '[' is shifted rather than reducing
4507 designator to designator-list. */
4508 if (des_prev == 1 && c_dialect_objc ())
4510 des_seen = des_prev;
4511 break;
4513 if (des_prev == 0 && c_dialect_objc ())
4515 /* This might be an array designator or an
4516 Objective-C message expression. If the former,
4517 continue parsing here; if the latter, parse the
4518 remainder of the initializer given the starting
4519 primary-expression. ??? It might make sense to
4520 distinguish when des_prev == 1 as well; see
4521 previous comment. */
4522 tree rec, args;
4523 struct c_expr mexpr;
4524 c_parser_consume_token (parser);
4525 if (c_parser_peek_token (parser)->type == CPP_NAME
4526 && ((c_parser_peek_token (parser)->id_kind
4527 == C_ID_TYPENAME)
4528 || (c_parser_peek_token (parser)->id_kind
4529 == C_ID_CLASSNAME)))
4531 /* Type name receiver. */
4532 tree id = c_parser_peek_token (parser)->value;
4533 c_parser_consume_token (parser);
4534 rec = objc_get_class_reference (id);
4535 goto parse_message_args;
4537 first = c_parser_expr_no_commas (parser, NULL).value;
4538 mark_exp_read (first);
4539 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4540 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4541 goto array_desig_after_first;
4542 /* Expression receiver. So far only one part
4543 without commas has been parsed; there might be
4544 more of the expression. */
4545 rec = first;
4546 while (c_parser_next_token_is (parser, CPP_COMMA))
4548 struct c_expr next;
4549 location_t comma_loc, exp_loc;
4550 comma_loc = c_parser_peek_token (parser)->location;
4551 c_parser_consume_token (parser);
4552 exp_loc = c_parser_peek_token (parser)->location;
4553 next = c_parser_expr_no_commas (parser, NULL);
4554 next = convert_lvalue_to_rvalue (exp_loc, next,
4555 true, true);
4556 rec = build_compound_expr (comma_loc, rec, next.value);
4558 parse_message_args:
4559 /* Now parse the objc-message-args. */
4560 args = c_parser_objc_message_args (parser);
4561 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4562 "expected %<]%>");
4563 mexpr.value
4564 = objc_build_message_expr (rec, args);
4565 mexpr.original_code = ERROR_MARK;
4566 mexpr.original_type = NULL;
4567 /* Now parse and process the remainder of the
4568 initializer, starting with this message
4569 expression as a primary-expression. */
4570 c_parser_initval (parser, &mexpr, braced_init_obstack);
4571 return;
4573 c_parser_consume_token (parser);
4574 array_index_loc = c_parser_peek_token (parser)->location;
4575 first = c_parser_expr_no_commas (parser, NULL).value;
4576 mark_exp_read (first);
4577 array_desig_after_first:
4578 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4580 ellipsis_loc = c_parser_peek_token (parser)->location;
4581 c_parser_consume_token (parser);
4582 second = c_parser_expr_no_commas (parser, NULL).value;
4583 mark_exp_read (second);
4585 else
4586 second = NULL_TREE;
4587 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4589 c_parser_consume_token (parser);
4590 set_init_index (array_index_loc, first, second,
4591 braced_init_obstack);
4592 if (second)
4593 pedwarn (ellipsis_loc, OPT_Wpedantic,
4594 "ISO C forbids specifying range of elements to initialize");
4596 else
4597 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4598 "expected %<]%>");
4601 if (des_seen >= 1)
4603 if (c_parser_next_token_is (parser, CPP_EQ))
4605 pedwarn_c90 (des_loc, OPT_Wpedantic,
4606 "ISO C90 forbids specifying subobject "
4607 "to initialize");
4608 c_parser_consume_token (parser);
4610 else
4612 if (des_seen == 1)
4613 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4614 "obsolete use of designated initializer without %<=%>");
4615 else
4617 struct c_expr init;
4618 init.value = error_mark_node;
4619 init.original_code = ERROR_MARK;
4620 init.original_type = NULL;
4621 c_parser_error (parser, "expected %<=%>");
4622 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4623 process_init_element (input_location, init, false,
4624 braced_init_obstack);
4625 return;
4630 c_parser_initval (parser, NULL, braced_init_obstack);
4633 /* Parse a nested initializer; as c_parser_initializer but parses
4634 initializers within braced lists, after any designators have been
4635 applied. If AFTER is not NULL then it is an Objective-C message
4636 expression which is the primary-expression starting the
4637 initializer. */
4639 static void
4640 c_parser_initval (c_parser *parser, struct c_expr *after,
4641 struct obstack * braced_init_obstack)
4643 struct c_expr init;
4644 gcc_assert (!after || c_dialect_objc ());
4645 location_t loc = c_parser_peek_token (parser)->location;
4647 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4648 init = c_parser_braced_init (parser, NULL_TREE, true,
4649 braced_init_obstack);
4650 else
4652 init = c_parser_expr_no_commas (parser, after);
4653 if (init.value != NULL_TREE
4654 && TREE_CODE (init.value) != STRING_CST
4655 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4656 init = convert_lvalue_to_rvalue (loc, init, true, true);
4658 process_init_element (loc, init, false, braced_init_obstack);
4661 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4662 C99 6.8.2).
4664 compound-statement:
4665 { block-item-list[opt] }
4666 { label-declarations block-item-list }
4668 block-item-list:
4669 block-item
4670 block-item-list block-item
4672 block-item:
4673 nested-declaration
4674 statement
4676 nested-declaration:
4677 declaration
4679 GNU extensions:
4681 compound-statement:
4682 { label-declarations block-item-list }
4684 nested-declaration:
4685 __extension__ nested-declaration
4686 nested-function-definition
4688 label-declarations:
4689 label-declaration
4690 label-declarations label-declaration
4692 label-declaration:
4693 __label__ identifier-list ;
4695 Allowing the mixing of declarations and code is new in C99. The
4696 GNU syntax also permits (not shown above) labels at the end of
4697 compound statements, which yield an error. We don't allow labels
4698 on declarations; this might seem like a natural extension, but
4699 there would be a conflict between attributes on the label and
4700 prefix attributes on the declaration. ??? The syntax follows the
4701 old parser in requiring something after label declarations.
4702 Although they are erroneous if the labels declared aren't defined,
4703 is it useful for the syntax to be this way?
4705 OpenACC:
4707 block-item:
4708 openacc-directive
4710 openacc-directive:
4711 update-directive
4713 OpenMP:
4715 block-item:
4716 openmp-directive
4718 openmp-directive:
4719 barrier-directive
4720 flush-directive
4721 taskwait-directive
4722 taskyield-directive
4723 cancel-directive
4724 cancellation-point-directive */
4726 static tree
4727 c_parser_compound_statement (c_parser *parser)
4729 tree stmt;
4730 location_t brace_loc;
4731 brace_loc = c_parser_peek_token (parser)->location;
4732 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4734 /* Ensure a scope is entered and left anyway to avoid confusion
4735 if we have just prepared to enter a function body. */
4736 stmt = c_begin_compound_stmt (true);
4737 c_end_compound_stmt (brace_loc, stmt, true);
4738 return error_mark_node;
4740 stmt = c_begin_compound_stmt (true);
4741 c_parser_compound_statement_nostart (parser);
4743 /* If the compound stmt contains array notations, then we expand them. */
4744 if (flag_cilkplus && contains_array_notation_expr (stmt))
4745 stmt = expand_array_notation_exprs (stmt);
4746 return c_end_compound_stmt (brace_loc, stmt, true);
4749 /* Parse a compound statement except for the opening brace. This is
4750 used for parsing both compound statements and statement expressions
4751 (which follow different paths to handling the opening). */
4753 static void
4754 c_parser_compound_statement_nostart (c_parser *parser)
4756 bool last_stmt = false;
4757 bool last_label = false;
4758 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4759 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4760 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4762 c_parser_consume_token (parser);
4763 return;
4765 mark_valid_location_for_stdc_pragma (true);
4766 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4768 /* Read zero or more forward-declarations for labels that nested
4769 functions can jump to. */
4770 mark_valid_location_for_stdc_pragma (false);
4771 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4773 label_loc = c_parser_peek_token (parser)->location;
4774 c_parser_consume_token (parser);
4775 /* Any identifiers, including those declared as type names,
4776 are OK here. */
4777 while (true)
4779 tree label;
4780 if (c_parser_next_token_is_not (parser, CPP_NAME))
4782 c_parser_error (parser, "expected identifier");
4783 break;
4785 label
4786 = declare_label (c_parser_peek_token (parser)->value);
4787 C_DECLARED_LABEL_FLAG (label) = 1;
4788 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4789 c_parser_consume_token (parser);
4790 if (c_parser_next_token_is (parser, CPP_COMMA))
4791 c_parser_consume_token (parser);
4792 else
4793 break;
4795 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4797 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4799 /* We must now have at least one statement, label or declaration. */
4800 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4802 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4803 c_parser_error (parser, "expected declaration or statement");
4804 c_parser_consume_token (parser);
4805 return;
4807 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
4809 location_t loc = c_parser_peek_token (parser)->location;
4810 if (c_parser_next_token_is_keyword (parser, RID_CASE)
4811 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4812 || (c_parser_next_token_is (parser, CPP_NAME)
4813 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4815 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4816 label_loc = c_parser_peek_2nd_token (parser)->location;
4817 else
4818 label_loc = c_parser_peek_token (parser)->location;
4819 last_label = true;
4820 last_stmt = false;
4821 mark_valid_location_for_stdc_pragma (false);
4822 c_parser_label (parser);
4824 else if (!last_label
4825 && c_parser_next_tokens_start_declaration (parser))
4827 last_label = false;
4828 mark_valid_location_for_stdc_pragma (false);
4829 bool fallthru_attr_p = false;
4830 c_parser_declaration_or_fndef (parser, true, true, true, true,
4831 true, NULL, vNULL, NULL,
4832 &fallthru_attr_p);
4833 if (last_stmt && !fallthru_attr_p)
4834 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4835 "ISO C90 forbids mixed declarations and code");
4836 last_stmt = fallthru_attr_p;
4838 else if (!last_label
4839 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
4841 /* __extension__ can start a declaration, but is also an
4842 unary operator that can start an expression. Consume all
4843 but the last of a possible series of __extension__ to
4844 determine which. */
4845 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
4846 && (c_parser_peek_2nd_token (parser)->keyword
4847 == RID_EXTENSION))
4848 c_parser_consume_token (parser);
4849 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
4851 int ext;
4852 ext = disable_extension_diagnostics ();
4853 c_parser_consume_token (parser);
4854 last_label = false;
4855 mark_valid_location_for_stdc_pragma (false);
4856 c_parser_declaration_or_fndef (parser, true, true, true, true,
4857 true, NULL, vNULL);
4858 /* Following the old parser, __extension__ does not
4859 disable this diagnostic. */
4860 restore_extension_diagnostics (ext);
4861 if (last_stmt)
4862 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
4863 "ISO C90 forbids mixed declarations and code");
4864 last_stmt = false;
4866 else
4867 goto statement;
4869 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
4871 /* External pragmas, and some omp pragmas, are not associated
4872 with regular c code, and so are not to be considered statements
4873 syntactically. This ensures that the user doesn't put them
4874 places that would turn into syntax errors if the directive
4875 were ignored. */
4876 if (c_parser_pragma (parser,
4877 last_label ? pragma_stmt : pragma_compound,
4878 NULL))
4879 last_label = false, last_stmt = true;
4881 else if (c_parser_next_token_is (parser, CPP_EOF))
4883 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4884 c_parser_error (parser, "expected declaration or statement");
4885 return;
4887 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
4889 if (parser->in_if_block)
4891 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4892 error_at (loc, """expected %<}%> before %<else%>");
4893 return;
4895 else
4897 error_at (loc, "%<else%> without a previous %<if%>");
4898 c_parser_consume_token (parser);
4899 continue;
4902 else
4904 statement:
4905 last_label = false;
4906 last_stmt = true;
4907 mark_valid_location_for_stdc_pragma (false);
4908 c_parser_statement_after_labels (parser, NULL);
4911 parser->error = false;
4913 if (last_label)
4914 error_at (label_loc, "label at end of compound statement");
4915 c_parser_consume_token (parser);
4916 /* Restore the value we started with. */
4917 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4920 /* Parse all consecutive labels. */
4922 static void
4923 c_parser_all_labels (c_parser *parser)
4925 while (c_parser_next_token_is_keyword (parser, RID_CASE)
4926 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
4927 || (c_parser_next_token_is (parser, CPP_NAME)
4928 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
4929 c_parser_label (parser);
4932 /* Parse a label (C90 6.6.1, C99 6.8.1).
4934 label:
4935 identifier : attributes[opt]
4936 case constant-expression :
4937 default :
4939 GNU extensions:
4941 label:
4942 case constant-expression ... constant-expression :
4944 The use of attributes on labels is a GNU extension. The syntax in
4945 GNU C accepts any expressions without commas, non-constant
4946 expressions being rejected later. */
4948 static void
4949 c_parser_label (c_parser *parser)
4951 location_t loc1 = c_parser_peek_token (parser)->location;
4952 tree label = NULL_TREE;
4954 /* Remember whether this case or a user-defined label is allowed to fall
4955 through to. */
4956 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
4958 if (c_parser_next_token_is_keyword (parser, RID_CASE))
4960 tree exp1, exp2;
4961 c_parser_consume_token (parser);
4962 exp1 = c_parser_expr_no_commas (parser, NULL).value;
4963 if (c_parser_next_token_is (parser, CPP_COLON))
4965 c_parser_consume_token (parser);
4966 label = do_case (loc1, exp1, NULL_TREE);
4968 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4970 c_parser_consume_token (parser);
4971 exp2 = c_parser_expr_no_commas (parser, NULL).value;
4972 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4973 label = do_case (loc1, exp1, exp2);
4975 else
4976 c_parser_error (parser, "expected %<:%> or %<...%>");
4978 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
4980 c_parser_consume_token (parser);
4981 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
4982 label = do_case (loc1, NULL_TREE, NULL_TREE);
4984 else
4986 tree name = c_parser_peek_token (parser)->value;
4987 tree tlab;
4988 tree attrs;
4989 location_t loc2 = c_parser_peek_token (parser)->location;
4990 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
4991 c_parser_consume_token (parser);
4992 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
4993 c_parser_consume_token (parser);
4994 attrs = c_parser_attributes (parser);
4995 tlab = define_label (loc2, name);
4996 if (tlab)
4998 decl_attributes (&tlab, attrs, 0);
4999 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5002 if (label)
5004 if (TREE_CODE (label) == LABEL_EXPR)
5005 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5006 else
5007 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5009 /* Allow '__attribute__((fallthrough));'. */
5010 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5012 location_t loc = c_parser_peek_token (parser)->location;
5013 tree attrs = c_parser_attributes (parser);
5014 if (attribute_fallthrough_p (attrs))
5016 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5018 tree fn = build_call_expr_internal_loc (loc,
5019 IFN_FALLTHROUGH,
5020 void_type_node, 0);
5021 add_stmt (fn);
5023 else
5024 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5025 "not followed by %<;%>");
5027 else if (attrs != NULL_TREE)
5028 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5029 " can be applied to a null statement");
5031 if (c_parser_next_tokens_start_declaration (parser))
5033 error_at (c_parser_peek_token (parser)->location,
5034 "a label can only be part of a statement and "
5035 "a declaration is not a statement");
5036 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5037 /*static_assert_ok*/ true,
5038 /*empty_ok*/ true, /*nested*/ true,
5039 /*start_attr_ok*/ true, NULL,
5040 vNULL);
5045 /* Parse a statement (C90 6.6, C99 6.8).
5047 statement:
5048 labeled-statement
5049 compound-statement
5050 expression-statement
5051 selection-statement
5052 iteration-statement
5053 jump-statement
5055 labeled-statement:
5056 label statement
5058 expression-statement:
5059 expression[opt] ;
5061 selection-statement:
5062 if-statement
5063 switch-statement
5065 iteration-statement:
5066 while-statement
5067 do-statement
5068 for-statement
5070 jump-statement:
5071 goto identifier ;
5072 continue ;
5073 break ;
5074 return expression[opt] ;
5076 GNU extensions:
5078 statement:
5079 asm-statement
5081 jump-statement:
5082 goto * expression ;
5084 expression-statement:
5085 attributes ;
5087 Objective-C:
5089 statement:
5090 objc-throw-statement
5091 objc-try-catch-statement
5092 objc-synchronized-statement
5094 objc-throw-statement:
5095 @throw expression ;
5096 @throw ;
5098 OpenACC:
5100 statement:
5101 openacc-construct
5103 openacc-construct:
5104 parallel-construct
5105 kernels-construct
5106 data-construct
5107 loop-construct
5109 parallel-construct:
5110 parallel-directive structured-block
5112 kernels-construct:
5113 kernels-directive structured-block
5115 data-construct:
5116 data-directive structured-block
5118 loop-construct:
5119 loop-directive structured-block
5121 OpenMP:
5123 statement:
5124 openmp-construct
5126 openmp-construct:
5127 parallel-construct
5128 for-construct
5129 simd-construct
5130 for-simd-construct
5131 sections-construct
5132 single-construct
5133 parallel-for-construct
5134 parallel-for-simd-construct
5135 parallel-sections-construct
5136 master-construct
5137 critical-construct
5138 atomic-construct
5139 ordered-construct
5141 parallel-construct:
5142 parallel-directive structured-block
5144 for-construct:
5145 for-directive iteration-statement
5147 simd-construct:
5148 simd-directive iteration-statements
5150 for-simd-construct:
5151 for-simd-directive iteration-statements
5153 sections-construct:
5154 sections-directive section-scope
5156 single-construct:
5157 single-directive structured-block
5159 parallel-for-construct:
5160 parallel-for-directive iteration-statement
5162 parallel-for-simd-construct:
5163 parallel-for-simd-directive iteration-statement
5165 parallel-sections-construct:
5166 parallel-sections-directive section-scope
5168 master-construct:
5169 master-directive structured-block
5171 critical-construct:
5172 critical-directive structured-block
5174 atomic-construct:
5175 atomic-directive expression-statement
5177 ordered-construct:
5178 ordered-directive structured-block
5180 Transactional Memory:
5182 statement:
5183 transaction-statement
5184 transaction-cancel-statement
5186 IF_P is used to track whether there's a (possibly labeled) if statement
5187 which is not enclosed in braces and has an else clause. This is used to
5188 implement -Wparentheses. */
5190 static void
5191 c_parser_statement (c_parser *parser, bool *if_p)
5193 c_parser_all_labels (parser);
5194 c_parser_statement_after_labels (parser, if_p, NULL);
5197 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5198 of if-else-if conditions.
5200 IF_P is used to track whether there's a (possibly labeled) if statement
5201 which is not enclosed in braces and has an else clause. This is used to
5202 implement -Wparentheses. */
5204 static void
5205 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5206 vec<tree> *chain)
5208 location_t loc = c_parser_peek_token (parser)->location;
5209 tree stmt = NULL_TREE;
5210 bool in_if_block = parser->in_if_block;
5211 parser->in_if_block = false;
5212 if (if_p != NULL)
5213 *if_p = false;
5214 switch (c_parser_peek_token (parser)->type)
5216 case CPP_OPEN_BRACE:
5217 add_stmt (c_parser_compound_statement (parser));
5218 break;
5219 case CPP_KEYWORD:
5220 switch (c_parser_peek_token (parser)->keyword)
5222 case RID_IF:
5223 c_parser_if_statement (parser, if_p, chain);
5224 break;
5225 case RID_SWITCH:
5226 c_parser_switch_statement (parser, if_p);
5227 break;
5228 case RID_WHILE:
5229 c_parser_while_statement (parser, false, if_p);
5230 break;
5231 case RID_DO:
5232 c_parser_do_statement (parser, false);
5233 break;
5234 case RID_FOR:
5235 c_parser_for_statement (parser, false, if_p);
5236 break;
5237 case RID_CILK_FOR:
5238 if (!flag_cilkplus)
5240 error_at (c_parser_peek_token (parser)->location,
5241 "-fcilkplus must be enabled to use %<_Cilk_for%>");
5242 c_parser_skip_to_end_of_block_or_statement (parser);
5244 else
5245 c_parser_cilk_for (parser, integer_zero_node, if_p);
5246 break;
5247 case RID_CILK_SYNC:
5248 c_parser_consume_token (parser);
5249 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5250 if (!flag_cilkplus)
5251 error_at (loc, "-fcilkplus must be enabled to use %<_Cilk_sync%>");
5252 else
5253 add_stmt (build_cilk_sync ());
5254 break;
5255 case RID_GOTO:
5256 c_parser_consume_token (parser);
5257 if (c_parser_next_token_is (parser, CPP_NAME))
5259 stmt = c_finish_goto_label (loc,
5260 c_parser_peek_token (parser)->value);
5261 c_parser_consume_token (parser);
5263 else if (c_parser_next_token_is (parser, CPP_MULT))
5265 struct c_expr val;
5267 c_parser_consume_token (parser);
5268 val = c_parser_expression (parser);
5269 if (check_no_cilk (val.value,
5270 "Cilk array notation cannot be used as a computed goto expression",
5271 "%<_Cilk_spawn%> statement cannot be used as a computed goto expression",
5272 loc))
5273 val.value = error_mark_node;
5274 val = convert_lvalue_to_rvalue (loc, val, false, true);
5275 stmt = c_finish_goto_ptr (loc, val.value);
5277 else
5278 c_parser_error (parser, "expected identifier or %<*%>");
5279 goto expect_semicolon;
5280 case RID_CONTINUE:
5281 c_parser_consume_token (parser);
5282 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5283 goto expect_semicolon;
5284 case RID_BREAK:
5285 c_parser_consume_token (parser);
5286 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5287 goto expect_semicolon;
5288 case RID_RETURN:
5289 c_parser_consume_token (parser);
5290 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5292 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5293 c_parser_consume_token (parser);
5295 else
5297 location_t xloc = c_parser_peek_token (parser)->location;
5298 struct c_expr expr = c_parser_expression_conv (parser);
5299 mark_exp_read (expr.value);
5300 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5301 expr.value, expr.original_type);
5302 goto expect_semicolon;
5304 break;
5305 case RID_ASM:
5306 stmt = c_parser_asm_statement (parser);
5307 break;
5308 case RID_TRANSACTION_ATOMIC:
5309 case RID_TRANSACTION_RELAXED:
5310 stmt = c_parser_transaction (parser,
5311 c_parser_peek_token (parser)->keyword);
5312 break;
5313 case RID_TRANSACTION_CANCEL:
5314 stmt = c_parser_transaction_cancel (parser);
5315 goto expect_semicolon;
5316 case RID_AT_THROW:
5317 gcc_assert (c_dialect_objc ());
5318 c_parser_consume_token (parser);
5319 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5321 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5322 c_parser_consume_token (parser);
5324 else
5326 struct c_expr expr = c_parser_expression (parser);
5327 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5328 if (check_no_cilk (expr.value,
5329 "Cilk array notation cannot be used for a throw expression",
5330 "%<_Cilk_spawn%> statement cannot be used for a throw expression"))
5331 expr.value = error_mark_node;
5332 else
5334 expr.value = c_fully_fold (expr.value, false, NULL);
5335 stmt = objc_build_throw_stmt (loc, expr.value);
5337 goto expect_semicolon;
5339 break;
5340 case RID_AT_TRY:
5341 gcc_assert (c_dialect_objc ());
5342 c_parser_objc_try_catch_finally_statement (parser);
5343 break;
5344 case RID_AT_SYNCHRONIZED:
5345 gcc_assert (c_dialect_objc ());
5346 c_parser_objc_synchronized_statement (parser);
5347 break;
5348 case RID_ATTRIBUTE:
5350 /* Allow '__attribute__((fallthrough));'. */
5351 tree attrs = c_parser_attributes (parser);
5352 if (attribute_fallthrough_p (attrs))
5354 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5356 tree fn = build_call_expr_internal_loc (loc,
5357 IFN_FALLTHROUGH,
5358 void_type_node, 0);
5359 add_stmt (fn);
5360 /* Eat the ';'. */
5361 c_parser_consume_token (parser);
5363 else
5364 warning_at (loc, OPT_Wattributes,
5365 "%<fallthrough%> attribute not followed "
5366 "by %<;%>");
5368 else if (attrs != NULL_TREE)
5369 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5370 " can be applied to a null statement");
5371 break;
5373 default:
5374 goto expr_stmt;
5376 break;
5377 case CPP_SEMICOLON:
5378 c_parser_consume_token (parser);
5379 break;
5380 case CPP_CLOSE_PAREN:
5381 case CPP_CLOSE_SQUARE:
5382 /* Avoid infinite loop in error recovery:
5383 c_parser_skip_until_found stops at a closing nesting
5384 delimiter without consuming it, but here we need to consume
5385 it to proceed further. */
5386 c_parser_error (parser, "expected statement");
5387 c_parser_consume_token (parser);
5388 break;
5389 case CPP_PRAGMA:
5390 c_parser_pragma (parser, pragma_stmt, if_p);
5391 break;
5392 default:
5393 expr_stmt:
5394 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5395 expect_semicolon:
5396 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5397 break;
5399 /* Two cases cannot and do not have line numbers associated: If stmt
5400 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5401 cannot hold line numbers. But that's OK because the statement
5402 will either be changed to a MODIFY_EXPR during gimplification of
5403 the statement expr, or discarded. If stmt was compound, but
5404 without new variables, we will have skipped the creation of a
5405 BIND and will have a bare STATEMENT_LIST. But that's OK because
5406 (recursively) all of the component statements should already have
5407 line numbers assigned. ??? Can we discard no-op statements
5408 earlier? */
5409 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5410 protected_set_expr_location (stmt, loc);
5412 parser->in_if_block = in_if_block;
5415 /* Parse the condition from an if, do, while or for statements. */
5417 static tree
5418 c_parser_condition (c_parser *parser)
5420 location_t loc = c_parser_peek_token (parser)->location;
5421 tree cond;
5422 cond = c_parser_expression_conv (parser).value;
5423 cond = c_objc_common_truthvalue_conversion (loc, cond);
5424 cond = c_fully_fold (cond, false, NULL);
5425 if (warn_sequence_point)
5426 verify_sequence_points (cond);
5427 return cond;
5430 /* Parse a parenthesized condition from an if, do or while statement.
5432 condition:
5433 ( expression )
5435 static tree
5436 c_parser_paren_condition (c_parser *parser)
5438 tree cond;
5439 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5440 return error_mark_node;
5441 cond = c_parser_condition (parser);
5442 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5443 return cond;
5446 /* Parse a statement which is a block in C99.
5448 IF_P is used to track whether there's a (possibly labeled) if statement
5449 which is not enclosed in braces and has an else clause. This is used to
5450 implement -Wparentheses. */
5452 static tree
5453 c_parser_c99_block_statement (c_parser *parser, bool *if_p)
5455 tree block = c_begin_compound_stmt (flag_isoc99);
5456 location_t loc = c_parser_peek_token (parser)->location;
5457 c_parser_statement (parser, if_p);
5458 return c_end_compound_stmt (loc, block, flag_isoc99);
5461 /* Parse the body of an if statement. This is just parsing a
5462 statement but (a) it is a block in C99, (b) we track whether the
5463 body is an if statement for the sake of -Wparentheses warnings, (c)
5464 we handle an empty body specially for the sake of -Wempty-body
5465 warnings, and (d) we call parser_compound_statement directly
5466 because c_parser_statement_after_labels resets
5467 parser->in_if_block.
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 tree
5474 c_parser_if_body (c_parser *parser, bool *if_p,
5475 const token_indent_info &if_tinfo)
5477 tree block = c_begin_compound_stmt (flag_isoc99);
5478 location_t body_loc = c_parser_peek_token (parser)->location;
5479 token_indent_info body_tinfo
5480 = get_token_indent_info (c_parser_peek_token (parser));
5482 c_parser_all_labels (parser);
5483 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5485 location_t loc = c_parser_peek_token (parser)->location;
5486 add_stmt (build_empty_stmt (loc));
5487 c_parser_consume_token (parser);
5488 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5489 warning_at (loc, OPT_Wempty_body,
5490 "suggest braces around empty body in an %<if%> statement");
5492 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5493 add_stmt (c_parser_compound_statement (parser));
5494 else
5495 c_parser_statement_after_labels (parser, if_p);
5497 token_indent_info next_tinfo
5498 = get_token_indent_info (c_parser_peek_token (parser));
5499 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5501 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5504 /* Parse the else body of an if statement. This is just parsing a
5505 statement but (a) it is a block in C99, (b) we handle an empty body
5506 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5507 of if-else-if conditions. */
5509 static tree
5510 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5511 vec<tree> *chain)
5513 location_t body_loc = c_parser_peek_token (parser)->location;
5514 tree block = c_begin_compound_stmt (flag_isoc99);
5515 token_indent_info body_tinfo
5516 = get_token_indent_info (c_parser_peek_token (parser));
5518 c_parser_all_labels (parser);
5519 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5521 location_t loc = c_parser_peek_token (parser)->location;
5522 warning_at (loc,
5523 OPT_Wempty_body,
5524 "suggest braces around empty body in an %<else%> statement");
5525 add_stmt (build_empty_stmt (loc));
5526 c_parser_consume_token (parser);
5528 else
5529 c_parser_statement_after_labels (parser, NULL, chain);
5531 token_indent_info next_tinfo
5532 = get_token_indent_info (c_parser_peek_token (parser));
5533 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5535 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5538 /* We might need to reclassify any previously-lexed identifier, e.g.
5539 when we've left a for loop with an if-statement without else in the
5540 body - we might have used a wrong scope for the token. See PR67784. */
5542 static void
5543 c_parser_maybe_reclassify_token (c_parser *parser)
5545 if (c_parser_next_token_is (parser, CPP_NAME))
5547 c_token *token = c_parser_peek_token (parser);
5549 if (token->id_kind != C_ID_CLASSNAME)
5551 tree decl = lookup_name (token->value);
5553 token->id_kind = C_ID_ID;
5554 if (decl)
5556 if (TREE_CODE (decl) == TYPE_DECL)
5557 token->id_kind = C_ID_TYPENAME;
5559 else if (c_dialect_objc ())
5561 tree objc_interface_decl = objc_is_class_name (token->value);
5562 /* Objective-C class names are in the same namespace as
5563 variables and typedefs, and hence are shadowed by local
5564 declarations. */
5565 if (objc_interface_decl)
5567 token->value = objc_interface_decl;
5568 token->id_kind = C_ID_CLASSNAME;
5575 /* Parse an if statement (C90 6.6.4, C99 6.8.4).
5577 if-statement:
5578 if ( expression ) statement
5579 if ( expression ) statement else statement
5581 CHAIN is a vector of if-else-if conditions.
5582 IF_P is used to track whether there's a (possibly labeled) if statement
5583 which is not enclosed in braces and has an else clause. This is used to
5584 implement -Wparentheses. */
5586 static void
5587 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5589 tree block;
5590 location_t loc;
5591 tree cond;
5592 bool nested_if = false;
5593 tree first_body, second_body;
5594 bool in_if_block;
5595 tree if_stmt;
5597 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5598 token_indent_info if_tinfo
5599 = get_token_indent_info (c_parser_peek_token (parser));
5600 c_parser_consume_token (parser);
5601 block = c_begin_compound_stmt (flag_isoc99);
5602 loc = c_parser_peek_token (parser)->location;
5603 cond = c_parser_paren_condition (parser);
5604 if (flag_cilkplus && contains_cilk_spawn_stmt (cond))
5606 error_at (loc, "if statement cannot contain %<Cilk_spawn%>");
5607 cond = error_mark_node;
5609 in_if_block = parser->in_if_block;
5610 parser->in_if_block = true;
5611 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5612 parser->in_if_block = in_if_block;
5614 if (warn_duplicated_cond)
5615 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5617 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5619 token_indent_info else_tinfo
5620 = get_token_indent_info (c_parser_peek_token (parser));
5621 c_parser_consume_token (parser);
5622 if (warn_duplicated_cond)
5624 if (c_parser_next_token_is_keyword (parser, RID_IF)
5625 && chain == NULL)
5627 /* We've got "if (COND) else if (COND2)". Start the
5628 condition chain and add COND as the first element. */
5629 chain = new vec<tree> ();
5630 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5631 chain->safe_push (cond);
5633 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5635 /* This is if-else without subsequent if. Zap the condition
5636 chain; we would have already warned at this point. */
5637 delete chain;
5638 chain = NULL;
5641 second_body = c_parser_else_body (parser, else_tinfo, chain);
5642 /* Set IF_P to true to indicate that this if statement has an
5643 else clause. This may trigger the Wparentheses warning
5644 below when we get back up to the parent if statement. */
5645 if (if_p != NULL)
5646 *if_p = true;
5648 else
5650 second_body = NULL_TREE;
5652 /* Diagnose an ambiguous else if if-then-else is nested inside
5653 if-then. */
5654 if (nested_if)
5655 warning_at (loc, OPT_Wdangling_else,
5656 "suggest explicit braces to avoid ambiguous %<else%>");
5658 if (warn_duplicated_cond)
5660 /* This if statement does not have an else clause. We don't
5661 need the condition chain anymore. */
5662 delete chain;
5663 chain = NULL;
5666 c_finish_if_stmt (loc, cond, first_body, second_body);
5667 if_stmt = c_end_compound_stmt (loc, block, flag_isoc99);
5669 /* If the if statement contains array notations, then we expand them. */
5670 if (flag_cilkplus && contains_array_notation_expr (if_stmt))
5671 if_stmt = fix_conditional_array_notations (if_stmt);
5672 add_stmt (if_stmt);
5673 c_parser_maybe_reclassify_token (parser);
5676 /* Parse a switch statement (C90 6.6.4, C99 6.8.4).
5678 switch-statement:
5679 switch (expression) statement
5682 static void
5683 c_parser_switch_statement (c_parser *parser, bool *if_p)
5685 struct c_expr ce;
5686 tree block, expr, body, save_break;
5687 location_t switch_loc = c_parser_peek_token (parser)->location;
5688 location_t switch_cond_loc;
5689 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5690 c_parser_consume_token (parser);
5691 block = c_begin_compound_stmt (flag_isoc99);
5692 bool explicit_cast_p = false;
5693 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5695 switch_cond_loc = c_parser_peek_token (parser)->location;
5696 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5697 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5698 explicit_cast_p = true;
5699 ce = c_parser_expression (parser);
5700 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5701 expr = ce.value;
5702 /* ??? expr has no valid location? */
5703 if (check_no_cilk (expr,
5704 "Cilk array notation cannot be used as a condition for switch statement",
5705 "%<_Cilk_spawn%> statement cannot be used as a condition for switch statement",
5706 switch_cond_loc))
5707 expr = error_mark_node;
5708 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
5710 else
5712 switch_cond_loc = UNKNOWN_LOCATION;
5713 expr = error_mark_node;
5714 ce.original_type = error_mark_node;
5716 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5717 save_break = c_break_label;
5718 c_break_label = NULL_TREE;
5719 body = c_parser_c99_block_statement (parser, if_p);
5720 c_finish_case (body, ce.original_type);
5721 if (c_break_label)
5723 location_t here = c_parser_peek_token (parser)->location;
5724 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5725 SET_EXPR_LOCATION (t, here);
5726 add_stmt (t);
5728 c_break_label = save_break;
5729 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5730 c_parser_maybe_reclassify_token (parser);
5733 /* Parse a while statement (C90 6.6.5, C99 6.8.5).
5735 while-statement:
5736 while (expression) statement
5738 IF_P is used to track whether there's a (possibly labeled) if statement
5739 which is not enclosed in braces and has an else clause. This is used to
5740 implement -Wparentheses. */
5742 static void
5743 c_parser_while_statement (c_parser *parser, bool ivdep, bool *if_p)
5745 tree block, cond, body, save_break, save_cont;
5746 location_t loc;
5747 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5748 token_indent_info while_tinfo
5749 = get_token_indent_info (c_parser_peek_token (parser));
5750 c_parser_consume_token (parser);
5751 block = c_begin_compound_stmt (flag_isoc99);
5752 loc = c_parser_peek_token (parser)->location;
5753 cond = c_parser_paren_condition (parser);
5754 if (check_no_cilk (cond,
5755 "Cilk array notation cannot be used as a condition for while statement",
5756 "%<_Cilk_spawn%> statement cannot be used as a condition for while statement"))
5757 cond = error_mark_node;
5758 if (ivdep && cond != error_mark_node)
5759 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5760 build_int_cst (integer_type_node,
5761 annot_expr_ivdep_kind));
5762 save_break = c_break_label;
5763 c_break_label = NULL_TREE;
5764 save_cont = c_cont_label;
5765 c_cont_label = NULL_TREE;
5767 token_indent_info body_tinfo
5768 = get_token_indent_info (c_parser_peek_token (parser));
5770 body = c_parser_c99_block_statement (parser, if_p);
5771 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5772 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5773 c_parser_maybe_reclassify_token (parser);
5775 token_indent_info next_tinfo
5776 = get_token_indent_info (c_parser_peek_token (parser));
5777 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5779 c_break_label = save_break;
5780 c_cont_label = save_cont;
5783 /* Parse a do statement (C90 6.6.5, C99 6.8.5).
5785 do-statement:
5786 do statement while ( expression ) ;
5789 static void
5790 c_parser_do_statement (c_parser *parser, bool ivdep)
5792 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5793 location_t loc;
5794 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5795 c_parser_consume_token (parser);
5796 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5797 warning_at (c_parser_peek_token (parser)->location,
5798 OPT_Wempty_body,
5799 "suggest braces around empty body in %<do%> statement");
5800 block = c_begin_compound_stmt (flag_isoc99);
5801 loc = c_parser_peek_token (parser)->location;
5802 save_break = c_break_label;
5803 c_break_label = NULL_TREE;
5804 save_cont = c_cont_label;
5805 c_cont_label = NULL_TREE;
5806 body = c_parser_c99_block_statement (parser, NULL);
5807 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5808 new_break = c_break_label;
5809 c_break_label = save_break;
5810 new_cont = c_cont_label;
5811 c_cont_label = save_cont;
5812 cond = c_parser_paren_condition (parser);
5813 if (check_no_cilk (cond,
5814 "Cilk array notation cannot be used as a condition for a do-while statement",
5815 "%<_Cilk_spawn%> statement cannot be used as a condition for a do-while statement"))
5816 cond = error_mark_node;
5817 if (ivdep && cond != error_mark_node)
5818 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5819 build_int_cst (integer_type_node,
5820 annot_expr_ivdep_kind));
5821 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
5822 c_parser_skip_to_end_of_block_or_statement (parser);
5823 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
5824 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5827 /* Parse a for statement (C90 6.6.5, C99 6.8.5).
5829 for-statement:
5830 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
5831 for ( nested-declaration expression[opt] ; expression[opt] ) statement
5833 The form with a declaration is new in C99.
5835 ??? In accordance with the old parser, the declaration may be a
5836 nested function, which is then rejected in check_for_loop_decls,
5837 but does it make any sense for this to be included in the grammar?
5838 Note in particular that the nested function does not include a
5839 trailing ';', whereas the "declaration" production includes one.
5840 Also, can we reject bad declarations earlier and cheaper than
5841 check_for_loop_decls?
5843 In Objective-C, there are two additional variants:
5845 foreach-statement:
5846 for ( expression in expresssion ) statement
5847 for ( declaration in expression ) statement
5849 This is inconsistent with C, because the second variant is allowed
5850 even if c99 is not enabled.
5852 The rest of the comment documents these Objective-C foreach-statement.
5854 Here is the canonical example of the first variant:
5855 for (object in array) { do something with object }
5856 we call the first expression ("object") the "object_expression" and
5857 the second expression ("array") the "collection_expression".
5858 object_expression must be an lvalue of type "id" (a generic Objective-C
5859 object) because the loop works by assigning to object_expression the
5860 various objects from the collection_expression. collection_expression
5861 must evaluate to something of type "id" which responds to the method
5862 countByEnumeratingWithState:objects:count:.
5864 The canonical example of the second variant is:
5865 for (id object in array) { do something with object }
5866 which is completely equivalent to
5868 id object;
5869 for (object in array) { do something with object }
5871 Note that initizializing 'object' in some way (eg, "for ((object =
5872 xxx) in array) { do something with object }") is possibly
5873 technically valid, but completely pointless as 'object' will be
5874 assigned to something else as soon as the loop starts. We should
5875 most likely reject it (TODO).
5877 The beginning of the Objective-C foreach-statement looks exactly
5878 like the beginning of the for-statement, and we can tell it is a
5879 foreach-statement only because the initial declaration or
5880 expression is terminated by 'in' instead of ';'.
5882 IF_P is used to track whether there's a (possibly labeled) if statement
5883 which is not enclosed in braces and has an else clause. This is used to
5884 implement -Wparentheses. */
5886 static void
5887 c_parser_for_statement (c_parser *parser, bool ivdep, bool *if_p)
5889 tree block, cond, incr, save_break, save_cont, body;
5890 /* The following are only used when parsing an ObjC foreach statement. */
5891 tree object_expression;
5892 /* Silence the bogus uninitialized warning. */
5893 tree collection_expression = NULL;
5894 location_t loc = c_parser_peek_token (parser)->location;
5895 location_t for_loc = c_parser_peek_token (parser)->location;
5896 bool is_foreach_statement = false;
5897 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
5898 token_indent_info for_tinfo
5899 = get_token_indent_info (c_parser_peek_token (parser));
5900 c_parser_consume_token (parser);
5901 /* Open a compound statement in Objective-C as well, just in case this is
5902 as foreach expression. */
5903 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
5904 cond = error_mark_node;
5905 incr = error_mark_node;
5906 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
5908 /* Parse the initialization declaration or expression. */
5909 object_expression = error_mark_node;
5910 parser->objc_could_be_foreach_context = c_dialect_objc ();
5911 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5913 parser->objc_could_be_foreach_context = false;
5914 c_parser_consume_token (parser);
5915 c_finish_expr_stmt (loc, NULL_TREE);
5917 else if (c_parser_next_tokens_start_declaration (parser))
5919 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
5920 &object_expression, vNULL);
5921 parser->objc_could_be_foreach_context = false;
5923 if (c_parser_next_token_is_keyword (parser, RID_IN))
5925 c_parser_consume_token (parser);
5926 is_foreach_statement = true;
5927 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5928 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5930 else
5931 check_for_loop_decls (for_loc, flag_isoc99);
5933 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5935 /* __extension__ can start a declaration, but is also an
5936 unary operator that can start an expression. Consume all
5937 but the last of a possible series of __extension__ to
5938 determine which. */
5939 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5940 && (c_parser_peek_2nd_token (parser)->keyword
5941 == RID_EXTENSION))
5942 c_parser_consume_token (parser);
5943 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5945 int ext;
5946 ext = disable_extension_diagnostics ();
5947 c_parser_consume_token (parser);
5948 c_parser_declaration_or_fndef (parser, true, true, true, true,
5949 true, &object_expression, vNULL);
5950 parser->objc_could_be_foreach_context = false;
5952 restore_extension_diagnostics (ext);
5953 if (c_parser_next_token_is_keyword (parser, RID_IN))
5955 c_parser_consume_token (parser);
5956 is_foreach_statement = true;
5957 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
5958 c_parser_error (parser, "multiple iterating variables in fast enumeration");
5960 else
5961 check_for_loop_decls (for_loc, flag_isoc99);
5963 else
5964 goto init_expr;
5966 else
5968 init_expr:
5970 struct c_expr ce;
5971 tree init_expression;
5972 ce = c_parser_expression (parser);
5973 /* In theory we could forbid _Cilk_spawn here, as the spec says "only in top
5974 level statement", but it works just fine, so allow it. */
5975 init_expression = ce.value;
5976 parser->objc_could_be_foreach_context = false;
5977 if (c_parser_next_token_is_keyword (parser, RID_IN))
5979 c_parser_consume_token (parser);
5980 is_foreach_statement = true;
5981 if (! lvalue_p (init_expression))
5982 c_parser_error (parser, "invalid iterating variable in fast enumeration");
5983 object_expression = c_fully_fold (init_expression, false, NULL);
5985 else
5987 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
5988 init_expression = ce.value;
5989 c_finish_expr_stmt (loc, init_expression);
5990 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5994 /* Parse the loop condition. In the case of a foreach
5995 statement, there is no loop condition. */
5996 gcc_assert (!parser->objc_could_be_foreach_context);
5997 if (!is_foreach_statement)
5999 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6001 if (ivdep)
6003 c_parser_error (parser, "missing loop condition in loop with "
6004 "%<GCC ivdep%> pragma");
6005 cond = error_mark_node;
6007 else
6009 c_parser_consume_token (parser);
6010 cond = NULL_TREE;
6013 else
6015 cond = c_parser_condition (parser);
6016 if (check_no_cilk (cond,
6017 "Cilk array notation cannot be used in a condition for a for-loop",
6018 "%<_Cilk_spawn%> statement cannot be used in a condition for a for-loop"))
6019 cond = error_mark_node;
6020 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6021 "expected %<;%>");
6023 if (ivdep && cond != error_mark_node)
6024 cond = build2 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6025 build_int_cst (integer_type_node,
6026 annot_expr_ivdep_kind));
6028 /* Parse the increment expression (the third expression in a
6029 for-statement). In the case of a foreach-statement, this is
6030 the expression that follows the 'in'. */
6031 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6033 if (is_foreach_statement)
6035 c_parser_error (parser, "missing collection in fast enumeration");
6036 collection_expression = error_mark_node;
6038 else
6039 incr = c_process_expr_stmt (loc, NULL_TREE);
6041 else
6043 if (is_foreach_statement)
6044 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6045 false, NULL);
6046 else
6048 struct c_expr ce = c_parser_expression (parser);
6049 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6050 incr = c_process_expr_stmt (loc, ce.value);
6053 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6055 save_break = c_break_label;
6056 c_break_label = NULL_TREE;
6057 save_cont = c_cont_label;
6058 c_cont_label = NULL_TREE;
6060 token_indent_info body_tinfo
6061 = get_token_indent_info (c_parser_peek_token (parser));
6063 body = c_parser_c99_block_statement (parser, if_p);
6065 if (is_foreach_statement)
6066 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6067 else
6068 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6069 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
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 (for_tinfo, body_tinfo, next_tinfo);
6076 c_break_label = save_break;
6077 c_cont_label = save_cont;
6080 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6081 statement with inputs, outputs, clobbers, and volatile tag
6082 allowed.
6084 asm-statement:
6085 asm type-qualifier[opt] ( asm-argument ) ;
6086 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6088 asm-argument:
6089 asm-string-literal
6090 asm-string-literal : asm-operands[opt]
6091 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6092 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6094 asm-goto-argument:
6095 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6096 : asm-goto-operands
6098 Qualifiers other than volatile are accepted in the syntax but
6099 warned for. */
6101 static tree
6102 c_parser_asm_statement (c_parser *parser)
6104 tree quals, str, outputs, inputs, clobbers, labels, ret;
6105 bool simple, is_goto;
6106 location_t asm_loc = c_parser_peek_token (parser)->location;
6107 int section, nsections;
6109 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6110 c_parser_consume_token (parser);
6111 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6113 quals = c_parser_peek_token (parser)->value;
6114 c_parser_consume_token (parser);
6116 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6117 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6119 warning_at (c_parser_peek_token (parser)->location,
6121 "%E qualifier ignored on asm",
6122 c_parser_peek_token (parser)->value);
6123 quals = NULL_TREE;
6124 c_parser_consume_token (parser);
6126 else
6127 quals = NULL_TREE;
6129 is_goto = false;
6130 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6132 c_parser_consume_token (parser);
6133 is_goto = true;
6136 /* ??? Follow the C++ parser rather than using the
6137 lex_untranslated_string kludge. */
6138 parser->lex_untranslated_string = true;
6139 ret = NULL;
6141 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6142 goto error;
6144 str = c_parser_asm_string_literal (parser);
6145 if (str == NULL_TREE)
6146 goto error_close_paren;
6148 simple = true;
6149 outputs = NULL_TREE;
6150 inputs = NULL_TREE;
6151 clobbers = NULL_TREE;
6152 labels = NULL_TREE;
6154 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6155 goto done_asm;
6157 /* Parse each colon-delimited section of operands. */
6158 nsections = 3 + is_goto;
6159 for (section = 0; section < nsections; ++section)
6161 if (!c_parser_require (parser, CPP_COLON,
6162 is_goto
6163 ? G_("expected %<:%>")
6164 : G_("expected %<:%> or %<)%>")))
6165 goto error_close_paren;
6167 /* Once past any colon, we're no longer a simple asm. */
6168 simple = false;
6170 if ((!c_parser_next_token_is (parser, CPP_COLON)
6171 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6172 || section == 3)
6173 switch (section)
6175 case 0:
6176 /* For asm goto, we don't allow output operands, but reserve
6177 the slot for a future extension that does allow them. */
6178 if (!is_goto)
6179 outputs = c_parser_asm_operands (parser);
6180 break;
6181 case 1:
6182 inputs = c_parser_asm_operands (parser);
6183 break;
6184 case 2:
6185 clobbers = c_parser_asm_clobbers (parser);
6186 break;
6187 case 3:
6188 labels = c_parser_asm_goto_operands (parser);
6189 break;
6190 default:
6191 gcc_unreachable ();
6194 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6195 goto done_asm;
6198 done_asm:
6199 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6201 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6202 goto error;
6205 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6206 c_parser_skip_to_end_of_block_or_statement (parser);
6208 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6209 clobbers, labels, simple));
6211 error:
6212 parser->lex_untranslated_string = false;
6213 return ret;
6215 error_close_paren:
6216 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6217 goto error;
6220 /* Parse asm operands, a GNU extension.
6222 asm-operands:
6223 asm-operand
6224 asm-operands , asm-operand
6226 asm-operand:
6227 asm-string-literal ( expression )
6228 [ identifier ] asm-string-literal ( expression )
6231 static tree
6232 c_parser_asm_operands (c_parser *parser)
6234 tree list = NULL_TREE;
6235 while (true)
6237 tree name, str;
6238 struct c_expr expr;
6239 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6241 c_parser_consume_token (parser);
6242 if (c_parser_next_token_is (parser, CPP_NAME))
6244 tree id = c_parser_peek_token (parser)->value;
6245 c_parser_consume_token (parser);
6246 name = build_string (IDENTIFIER_LENGTH (id),
6247 IDENTIFIER_POINTER (id));
6249 else
6251 c_parser_error (parser, "expected identifier");
6252 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6253 return NULL_TREE;
6255 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6256 "expected %<]%>");
6258 else
6259 name = NULL_TREE;
6260 str = c_parser_asm_string_literal (parser);
6261 if (str == NULL_TREE)
6262 return NULL_TREE;
6263 parser->lex_untranslated_string = false;
6264 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
6266 parser->lex_untranslated_string = true;
6267 return NULL_TREE;
6269 expr = c_parser_expression (parser);
6270 mark_exp_read (expr.value);
6271 parser->lex_untranslated_string = true;
6272 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
6274 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6275 return NULL_TREE;
6277 list = chainon (list, build_tree_list (build_tree_list (name, str),
6278 expr.value));
6279 if (c_parser_next_token_is (parser, CPP_COMMA))
6280 c_parser_consume_token (parser);
6281 else
6282 break;
6284 return list;
6287 /* Parse asm clobbers, a GNU extension.
6289 asm-clobbers:
6290 asm-string-literal
6291 asm-clobbers , asm-string-literal
6294 static tree
6295 c_parser_asm_clobbers (c_parser *parser)
6297 tree list = NULL_TREE;
6298 while (true)
6300 tree str = c_parser_asm_string_literal (parser);
6301 if (str)
6302 list = tree_cons (NULL_TREE, str, list);
6303 else
6304 return NULL_TREE;
6305 if (c_parser_next_token_is (parser, CPP_COMMA))
6306 c_parser_consume_token (parser);
6307 else
6308 break;
6310 return list;
6313 /* Parse asm goto labels, a GNU extension.
6315 asm-goto-operands:
6316 identifier
6317 asm-goto-operands , identifier
6320 static tree
6321 c_parser_asm_goto_operands (c_parser *parser)
6323 tree list = NULL_TREE;
6324 while (true)
6326 tree name, label;
6328 if (c_parser_next_token_is (parser, CPP_NAME))
6330 c_token *tok = c_parser_peek_token (parser);
6331 name = tok->value;
6332 label = lookup_label_for_goto (tok->location, name);
6333 c_parser_consume_token (parser);
6334 TREE_USED (label) = 1;
6336 else
6338 c_parser_error (parser, "expected identifier");
6339 return NULL_TREE;
6342 name = build_string (IDENTIFIER_LENGTH (name),
6343 IDENTIFIER_POINTER (name));
6344 list = tree_cons (name, label, list);
6345 if (c_parser_next_token_is (parser, CPP_COMMA))
6346 c_parser_consume_token (parser);
6347 else
6348 return nreverse (list);
6352 /* Parse an expression other than a compound expression; that is, an
6353 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not
6354 NULL then it is an Objective-C message expression which is the
6355 primary-expression starting the expression as an initializer.
6357 assignment-expression:
6358 conditional-expression
6359 unary-expression assignment-operator assignment-expression
6361 assignment-operator: one of
6362 = *= /= %= += -= <<= >>= &= ^= |=
6364 In GNU C we accept any conditional expression on the LHS and
6365 diagnose the invalid lvalue rather than producing a syntax
6366 error. */
6368 static struct c_expr
6369 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6370 tree omp_atomic_lhs)
6372 struct c_expr lhs, rhs, ret;
6373 enum tree_code code;
6374 location_t op_location, exp_location;
6375 gcc_assert (!after || c_dialect_objc ());
6376 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6377 op_location = c_parser_peek_token (parser)->location;
6378 switch (c_parser_peek_token (parser)->type)
6380 case CPP_EQ:
6381 code = NOP_EXPR;
6382 break;
6383 case CPP_MULT_EQ:
6384 code = MULT_EXPR;
6385 break;
6386 case CPP_DIV_EQ:
6387 code = TRUNC_DIV_EXPR;
6388 break;
6389 case CPP_MOD_EQ:
6390 code = TRUNC_MOD_EXPR;
6391 break;
6392 case CPP_PLUS_EQ:
6393 code = PLUS_EXPR;
6394 break;
6395 case CPP_MINUS_EQ:
6396 code = MINUS_EXPR;
6397 break;
6398 case CPP_LSHIFT_EQ:
6399 code = LSHIFT_EXPR;
6400 break;
6401 case CPP_RSHIFT_EQ:
6402 code = RSHIFT_EXPR;
6403 break;
6404 case CPP_AND_EQ:
6405 code = BIT_AND_EXPR;
6406 break;
6407 case CPP_XOR_EQ:
6408 code = BIT_XOR_EXPR;
6409 break;
6410 case CPP_OR_EQ:
6411 code = BIT_IOR_EXPR;
6412 break;
6413 default:
6414 return lhs;
6416 c_parser_consume_token (parser);
6417 exp_location = c_parser_peek_token (parser)->location;
6418 rhs = c_parser_expr_no_commas (parser, NULL);
6419 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6421 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6422 code, exp_location, rhs.value,
6423 rhs.original_type);
6424 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6425 if (code == NOP_EXPR)
6426 ret.original_code = MODIFY_EXPR;
6427 else
6429 TREE_NO_WARNING (ret.value) = 1;
6430 ret.original_code = ERROR_MARK;
6432 ret.original_type = NULL;
6433 return ret;
6436 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER
6437 is not NULL then it is an Objective-C message expression which is
6438 the primary-expression starting the expression as an initializer.
6440 conditional-expression:
6441 logical-OR-expression
6442 logical-OR-expression ? expression : conditional-expression
6444 GNU extensions:
6446 conditional-expression:
6447 logical-OR-expression ? : conditional-expression
6450 static struct c_expr
6451 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6452 tree omp_atomic_lhs)
6454 struct c_expr cond, exp1, exp2, ret;
6455 location_t start, cond_loc, colon_loc, middle_loc;
6457 gcc_assert (!after || c_dialect_objc ());
6459 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6461 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6462 return cond;
6463 if (cond.value != error_mark_node)
6464 start = cond.get_start ();
6465 else
6466 start = UNKNOWN_LOCATION;
6467 cond_loc = c_parser_peek_token (parser)->location;
6468 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6469 c_parser_consume_token (parser);
6470 if (c_parser_next_token_is (parser, CPP_COLON))
6472 tree eptype = NULL_TREE;
6474 middle_loc = c_parser_peek_token (parser)->location;
6475 pedwarn (middle_loc, OPT_Wpedantic,
6476 "ISO C forbids omitting the middle term of a ?: expression");
6477 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6479 eptype = TREE_TYPE (cond.value);
6480 cond.value = TREE_OPERAND (cond.value, 0);
6482 tree e = cond.value;
6483 while (TREE_CODE (e) == COMPOUND_EXPR)
6484 e = TREE_OPERAND (e, 1);
6485 warn_for_omitted_condop (middle_loc, e);
6486 /* Make sure first operand is calculated only once. */
6487 exp1.value = c_save_expr (default_conversion (cond.value));
6488 if (eptype)
6489 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6490 exp1.original_type = NULL;
6491 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6492 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6494 else
6496 cond.value
6497 = c_objc_common_truthvalue_conversion
6498 (cond_loc, default_conversion (cond.value));
6499 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6500 exp1 = c_parser_expression_conv (parser);
6501 mark_exp_read (exp1.value);
6502 c_inhibit_evaluation_warnings +=
6503 ((cond.value == truthvalue_true_node)
6504 - (cond.value == truthvalue_false_node));
6507 colon_loc = c_parser_peek_token (parser)->location;
6508 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6510 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6511 ret.value = error_mark_node;
6512 ret.original_code = ERROR_MARK;
6513 ret.original_type = NULL;
6514 return ret;
6517 location_t exp2_loc = c_parser_peek_token (parser)->location;
6518 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6519 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6521 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6522 ret.value = build_conditional_expr (colon_loc, cond.value,
6523 cond.original_code == C_MAYBE_CONST_EXPR,
6524 exp1.value, exp1.original_type,
6525 exp2.value, exp2.original_type);
6526 ret.original_code = ERROR_MARK;
6527 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6528 ret.original_type = NULL;
6529 else
6531 tree t1, t2;
6533 /* If both sides are enum type, the default conversion will have
6534 made the type of the result be an integer type. We want to
6535 remember the enum types we started with. */
6536 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6537 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6538 ret.original_type = ((t1 != error_mark_node
6539 && t2 != error_mark_node
6540 && (TYPE_MAIN_VARIANT (t1)
6541 == TYPE_MAIN_VARIANT (t2)))
6542 ? t1
6543 : NULL);
6545 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6546 return ret;
6549 /* Parse a binary expression; that is, a logical-OR-expression (C90
6550 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is
6551 an Objective-C message expression which is the primary-expression
6552 starting the expression as an initializer.
6554 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6555 when it should be the unfolded lhs. In a valid OpenMP source,
6556 one of the operands of the toplevel binary expression must be equal
6557 to it. In that case, just return a build2 created binary operation
6558 rather than result of parser_build_binary_op.
6560 multiplicative-expression:
6561 cast-expression
6562 multiplicative-expression * cast-expression
6563 multiplicative-expression / cast-expression
6564 multiplicative-expression % cast-expression
6566 additive-expression:
6567 multiplicative-expression
6568 additive-expression + multiplicative-expression
6569 additive-expression - multiplicative-expression
6571 shift-expression:
6572 additive-expression
6573 shift-expression << additive-expression
6574 shift-expression >> additive-expression
6576 relational-expression:
6577 shift-expression
6578 relational-expression < shift-expression
6579 relational-expression > shift-expression
6580 relational-expression <= shift-expression
6581 relational-expression >= shift-expression
6583 equality-expression:
6584 relational-expression
6585 equality-expression == relational-expression
6586 equality-expression != relational-expression
6588 AND-expression:
6589 equality-expression
6590 AND-expression & equality-expression
6592 exclusive-OR-expression:
6593 AND-expression
6594 exclusive-OR-expression ^ AND-expression
6596 inclusive-OR-expression:
6597 exclusive-OR-expression
6598 inclusive-OR-expression | exclusive-OR-expression
6600 logical-AND-expression:
6601 inclusive-OR-expression
6602 logical-AND-expression && inclusive-OR-expression
6604 logical-OR-expression:
6605 logical-AND-expression
6606 logical-OR-expression || logical-AND-expression
6609 static struct c_expr
6610 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6611 tree omp_atomic_lhs)
6613 /* A binary expression is parsed using operator-precedence parsing,
6614 with the operands being cast expressions. All the binary
6615 operators are left-associative. Thus a binary expression is of
6616 form:
6618 E0 op1 E1 op2 E2 ...
6620 which we represent on a stack. On the stack, the precedence
6621 levels are strictly increasing. When a new operator is
6622 encountered of higher precedence than that at the top of the
6623 stack, it is pushed; its LHS is the top expression, and its RHS
6624 is everything parsed until it is popped. When a new operator is
6625 encountered with precedence less than or equal to that at the top
6626 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6627 by the result of the operation until the operator at the top of
6628 the stack has lower precedence than the new operator or there is
6629 only one element on the stack; then the top expression is the LHS
6630 of the new operator. In the case of logical AND and OR
6631 expressions, we also need to adjust c_inhibit_evaluation_warnings
6632 as appropriate when the operators are pushed and popped. */
6634 struct {
6635 /* The expression at this stack level. */
6636 struct c_expr expr;
6637 /* The precedence of the operator on its left, PREC_NONE at the
6638 bottom of the stack. */
6639 enum c_parser_prec prec;
6640 /* The operation on its left. */
6641 enum tree_code op;
6642 /* The source location of this operation. */
6643 location_t loc;
6644 } stack[NUM_PRECS];
6645 int sp;
6646 /* Location of the binary operator. */
6647 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6648 #define POP \
6649 do { \
6650 switch (stack[sp].op) \
6652 case TRUTH_ANDIF_EXPR: \
6653 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6654 == truthvalue_false_node); \
6655 break; \
6656 case TRUTH_ORIF_EXPR: \
6657 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6658 == truthvalue_true_node); \
6659 break; \
6660 default: \
6661 break; \
6663 stack[sp - 1].expr \
6664 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6665 stack[sp - 1].expr, true, true); \
6666 stack[sp].expr \
6667 = convert_lvalue_to_rvalue (stack[sp].loc, \
6668 stack[sp].expr, true, true); \
6669 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6670 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6671 && ((1 << stack[sp].prec) \
6672 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6673 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6674 && stack[sp].op != TRUNC_MOD_EXPR \
6675 && stack[0].expr.value != error_mark_node \
6676 && stack[1].expr.value != error_mark_node \
6677 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6678 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6679 stack[0].expr.value \
6680 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6681 stack[0].expr.value, stack[1].expr.value); \
6682 else \
6683 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6684 stack[sp].op, \
6685 stack[sp - 1].expr, \
6686 stack[sp].expr); \
6687 sp--; \
6688 } while (0)
6689 gcc_assert (!after || c_dialect_objc ());
6690 stack[0].loc = c_parser_peek_token (parser)->location;
6691 stack[0].expr = c_parser_cast_expression (parser, after);
6692 stack[0].prec = PREC_NONE;
6693 sp = 0;
6694 while (true)
6696 enum c_parser_prec oprec;
6697 enum tree_code ocode;
6698 source_range src_range;
6699 if (parser->error)
6700 goto out;
6701 switch (c_parser_peek_token (parser)->type)
6703 case CPP_MULT:
6704 oprec = PREC_MULT;
6705 ocode = MULT_EXPR;
6706 break;
6707 case CPP_DIV:
6708 oprec = PREC_MULT;
6709 ocode = TRUNC_DIV_EXPR;
6710 break;
6711 case CPP_MOD:
6712 oprec = PREC_MULT;
6713 ocode = TRUNC_MOD_EXPR;
6714 break;
6715 case CPP_PLUS:
6716 oprec = PREC_ADD;
6717 ocode = PLUS_EXPR;
6718 break;
6719 case CPP_MINUS:
6720 oprec = PREC_ADD;
6721 ocode = MINUS_EXPR;
6722 break;
6723 case CPP_LSHIFT:
6724 oprec = PREC_SHIFT;
6725 ocode = LSHIFT_EXPR;
6726 break;
6727 case CPP_RSHIFT:
6728 oprec = PREC_SHIFT;
6729 ocode = RSHIFT_EXPR;
6730 break;
6731 case CPP_LESS:
6732 oprec = PREC_REL;
6733 ocode = LT_EXPR;
6734 break;
6735 case CPP_GREATER:
6736 oprec = PREC_REL;
6737 ocode = GT_EXPR;
6738 break;
6739 case CPP_LESS_EQ:
6740 oprec = PREC_REL;
6741 ocode = LE_EXPR;
6742 break;
6743 case CPP_GREATER_EQ:
6744 oprec = PREC_REL;
6745 ocode = GE_EXPR;
6746 break;
6747 case CPP_EQ_EQ:
6748 oprec = PREC_EQ;
6749 ocode = EQ_EXPR;
6750 break;
6751 case CPP_NOT_EQ:
6752 oprec = PREC_EQ;
6753 ocode = NE_EXPR;
6754 break;
6755 case CPP_AND:
6756 oprec = PREC_BITAND;
6757 ocode = BIT_AND_EXPR;
6758 break;
6759 case CPP_XOR:
6760 oprec = PREC_BITXOR;
6761 ocode = BIT_XOR_EXPR;
6762 break;
6763 case CPP_OR:
6764 oprec = PREC_BITOR;
6765 ocode = BIT_IOR_EXPR;
6766 break;
6767 case CPP_AND_AND:
6768 oprec = PREC_LOGAND;
6769 ocode = TRUTH_ANDIF_EXPR;
6770 break;
6771 case CPP_OR_OR:
6772 oprec = PREC_LOGOR;
6773 ocode = TRUTH_ORIF_EXPR;
6774 break;
6775 default:
6776 /* Not a binary operator, so end of the binary
6777 expression. */
6778 goto out;
6780 binary_loc = c_parser_peek_token (parser)->location;
6781 while (oprec <= stack[sp].prec)
6782 POP;
6783 c_parser_consume_token (parser);
6784 switch (ocode)
6786 case TRUTH_ANDIF_EXPR:
6787 src_range = stack[sp].expr.src_range;
6788 stack[sp].expr
6789 = convert_lvalue_to_rvalue (stack[sp].loc,
6790 stack[sp].expr, true, true);
6791 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6792 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6793 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6794 == truthvalue_false_node);
6795 set_c_expr_source_range (&stack[sp].expr, src_range);
6796 break;
6797 case TRUTH_ORIF_EXPR:
6798 src_range = stack[sp].expr.src_range;
6799 stack[sp].expr
6800 = convert_lvalue_to_rvalue (stack[sp].loc,
6801 stack[sp].expr, true, true);
6802 stack[sp].expr.value = c_objc_common_truthvalue_conversion
6803 (stack[sp].loc, default_conversion (stack[sp].expr.value));
6804 c_inhibit_evaluation_warnings += (stack[sp].expr.value
6805 == truthvalue_true_node);
6806 set_c_expr_source_range (&stack[sp].expr, src_range);
6807 break;
6808 default:
6809 break;
6811 sp++;
6812 stack[sp].loc = binary_loc;
6813 stack[sp].expr = c_parser_cast_expression (parser, NULL);
6814 stack[sp].prec = oprec;
6815 stack[sp].op = ocode;
6817 out:
6818 while (sp > 0)
6819 POP;
6820 return stack[0].expr;
6821 #undef POP
6824 /* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not
6825 NULL then it is an Objective-C message expression which is the
6826 primary-expression starting the expression as an initializer.
6828 cast-expression:
6829 unary-expression
6830 ( type-name ) unary-expression
6833 static struct c_expr
6834 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
6836 location_t cast_loc = c_parser_peek_token (parser)->location;
6837 gcc_assert (!after || c_dialect_objc ());
6838 if (after)
6839 return c_parser_postfix_expression_after_primary (parser,
6840 cast_loc, *after);
6841 /* If the expression begins with a parenthesized type name, it may
6842 be either a cast or a compound literal; we need to see whether
6843 the next character is '{' to tell the difference. If not, it is
6844 an unary expression. Full detection of unknown typenames here
6845 would require a 3-token lookahead. */
6846 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
6847 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
6849 struct c_type_name *type_name;
6850 struct c_expr ret;
6851 struct c_expr expr;
6852 c_parser_consume_token (parser);
6853 type_name = c_parser_type_name (parser);
6854 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
6855 if (type_name == NULL)
6857 ret.value = error_mark_node;
6858 ret.original_code = ERROR_MARK;
6859 ret.original_type = NULL;
6860 return ret;
6863 /* Save casted types in the function's used types hash table. */
6864 used_types_insert (type_name->specs->type);
6866 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
6867 return c_parser_postfix_expression_after_paren_type (parser, type_name,
6868 cast_loc);
6870 location_t expr_loc = c_parser_peek_token (parser)->location;
6871 expr = c_parser_cast_expression (parser, NULL);
6872 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
6874 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
6875 if (ret.value && expr.value)
6876 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
6877 ret.original_code = ERROR_MARK;
6878 ret.original_type = NULL;
6879 return ret;
6881 else
6882 return c_parser_unary_expression (parser);
6885 /* Parse an unary expression (C90 6.3.3, C99 6.5.3).
6887 unary-expression:
6888 postfix-expression
6889 ++ unary-expression
6890 -- unary-expression
6891 unary-operator cast-expression
6892 sizeof unary-expression
6893 sizeof ( type-name )
6895 unary-operator: one of
6896 & * + - ~ !
6898 GNU extensions:
6900 unary-expression:
6901 __alignof__ unary-expression
6902 __alignof__ ( type-name )
6903 && identifier
6905 (C11 permits _Alignof with type names only.)
6907 unary-operator: one of
6908 __extension__ __real__ __imag__
6910 Transactional Memory:
6912 unary-expression:
6913 transaction-expression
6915 In addition, the GNU syntax treats ++ and -- as unary operators, so
6916 they may be applied to cast expressions with errors for non-lvalues
6917 given later. */
6919 static struct c_expr
6920 c_parser_unary_expression (c_parser *parser)
6922 int ext;
6923 struct c_expr ret, op;
6924 location_t op_loc = c_parser_peek_token (parser)->location;
6925 location_t exp_loc;
6926 location_t finish;
6927 ret.original_code = ERROR_MARK;
6928 ret.original_type = NULL;
6929 switch (c_parser_peek_token (parser)->type)
6931 case CPP_PLUS_PLUS:
6932 c_parser_consume_token (parser);
6933 exp_loc = c_parser_peek_token (parser)->location;
6934 op = c_parser_cast_expression (parser, NULL);
6936 /* If there is array notations in op, we expand them. */
6937 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6938 return fix_array_notation_expr (exp_loc, PREINCREMENT_EXPR, op);
6939 else
6941 op = default_function_array_read_conversion (exp_loc, op);
6942 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
6944 case CPP_MINUS_MINUS:
6945 c_parser_consume_token (parser);
6946 exp_loc = c_parser_peek_token (parser)->location;
6947 op = c_parser_cast_expression (parser, NULL);
6949 /* If there is array notations in op, we expand them. */
6950 if (flag_cilkplus && TREE_CODE (op.value) == ARRAY_NOTATION_REF)
6951 return fix_array_notation_expr (exp_loc, PREDECREMENT_EXPR, op);
6952 else
6954 op = default_function_array_read_conversion (exp_loc, op);
6955 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
6957 case CPP_AND:
6958 c_parser_consume_token (parser);
6959 op = c_parser_cast_expression (parser, NULL);
6960 mark_exp_read (op.value);
6961 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
6962 case CPP_MULT:
6964 c_parser_consume_token (parser);
6965 exp_loc = c_parser_peek_token (parser)->location;
6966 op = c_parser_cast_expression (parser, NULL);
6967 finish = op.get_finish ();
6968 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6969 location_t combined_loc = make_location (op_loc, op_loc, finish);
6970 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
6971 ret.src_range.m_start = op_loc;
6972 ret.src_range.m_finish = finish;
6973 return ret;
6975 case CPP_PLUS:
6976 if (!c_dialect_objc () && !in_system_header_at (input_location))
6977 warning_at (op_loc,
6978 OPT_Wtraditional,
6979 "traditional C rejects the unary plus operator");
6980 c_parser_consume_token (parser);
6981 exp_loc = c_parser_peek_token (parser)->location;
6982 op = c_parser_cast_expression (parser, NULL);
6983 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6984 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
6985 case CPP_MINUS:
6986 c_parser_consume_token (parser);
6987 exp_loc = c_parser_peek_token (parser)->location;
6988 op = c_parser_cast_expression (parser, NULL);
6989 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6990 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
6991 case CPP_COMPL:
6992 c_parser_consume_token (parser);
6993 exp_loc = c_parser_peek_token (parser)->location;
6994 op = c_parser_cast_expression (parser, NULL);
6995 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
6996 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
6997 case CPP_NOT:
6998 c_parser_consume_token (parser);
6999 exp_loc = c_parser_peek_token (parser)->location;
7000 op = c_parser_cast_expression (parser, NULL);
7001 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7002 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7003 case CPP_AND_AND:
7004 /* Refer to the address of a label as a pointer. */
7005 c_parser_consume_token (parser);
7006 if (c_parser_next_token_is (parser, CPP_NAME))
7008 ret.value = finish_label_address_expr
7009 (c_parser_peek_token (parser)->value, op_loc);
7010 set_c_expr_source_range (&ret, op_loc,
7011 c_parser_peek_token (parser)->get_finish ());
7012 c_parser_consume_token (parser);
7014 else
7016 c_parser_error (parser, "expected identifier");
7017 ret.value = error_mark_node;
7019 return ret;
7020 case CPP_KEYWORD:
7021 switch (c_parser_peek_token (parser)->keyword)
7023 case RID_SIZEOF:
7024 return c_parser_sizeof_expression (parser);
7025 case RID_ALIGNOF:
7026 return c_parser_alignof_expression (parser);
7027 case RID_EXTENSION:
7028 c_parser_consume_token (parser);
7029 ext = disable_extension_diagnostics ();
7030 ret = c_parser_cast_expression (parser, NULL);
7031 restore_extension_diagnostics (ext);
7032 return ret;
7033 case RID_REALPART:
7034 c_parser_consume_token (parser);
7035 exp_loc = c_parser_peek_token (parser)->location;
7036 op = c_parser_cast_expression (parser, NULL);
7037 op = default_function_array_conversion (exp_loc, op);
7038 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7039 case RID_IMAGPART:
7040 c_parser_consume_token (parser);
7041 exp_loc = c_parser_peek_token (parser)->location;
7042 op = c_parser_cast_expression (parser, NULL);
7043 op = default_function_array_conversion (exp_loc, op);
7044 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7045 case RID_TRANSACTION_ATOMIC:
7046 case RID_TRANSACTION_RELAXED:
7047 return c_parser_transaction_expression (parser,
7048 c_parser_peek_token (parser)->keyword);
7049 default:
7050 return c_parser_postfix_expression (parser);
7052 default:
7053 return c_parser_postfix_expression (parser);
7057 /* Parse a sizeof expression. */
7059 static struct c_expr
7060 c_parser_sizeof_expression (c_parser *parser)
7062 struct c_expr expr;
7063 struct c_expr result;
7064 location_t expr_loc;
7065 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7067 location_t start;
7068 location_t finish = UNKNOWN_LOCATION;
7070 start = c_parser_peek_token (parser)->location;
7072 c_parser_consume_token (parser);
7073 c_inhibit_evaluation_warnings++;
7074 in_sizeof++;
7075 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7076 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7078 /* Either sizeof ( type-name ) or sizeof unary-expression
7079 starting with a compound literal. */
7080 struct c_type_name *type_name;
7081 c_parser_consume_token (parser);
7082 expr_loc = c_parser_peek_token (parser)->location;
7083 type_name = c_parser_type_name (parser);
7084 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7085 finish = parser->tokens_buf[0].location;
7086 if (type_name == NULL)
7088 struct c_expr ret;
7089 c_inhibit_evaluation_warnings--;
7090 in_sizeof--;
7091 ret.value = error_mark_node;
7092 ret.original_code = ERROR_MARK;
7093 ret.original_type = NULL;
7094 return ret;
7096 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7098 expr = c_parser_postfix_expression_after_paren_type (parser,
7099 type_name,
7100 expr_loc);
7101 finish = expr.get_finish ();
7102 goto sizeof_expr;
7104 /* sizeof ( type-name ). */
7105 c_inhibit_evaluation_warnings--;
7106 in_sizeof--;
7107 result = c_expr_sizeof_type (expr_loc, type_name);
7109 else
7111 expr_loc = c_parser_peek_token (parser)->location;
7112 expr = c_parser_unary_expression (parser);
7113 finish = expr.get_finish ();
7114 sizeof_expr:
7115 c_inhibit_evaluation_warnings--;
7116 in_sizeof--;
7117 mark_exp_read (expr.value);
7118 if (TREE_CODE (expr.value) == COMPONENT_REF
7119 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7120 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7121 result = c_expr_sizeof_expr (expr_loc, expr);
7123 if (finish != UNKNOWN_LOCATION)
7124 set_c_expr_source_range (&result, start, finish);
7125 return result;
7128 /* Parse an alignof expression. */
7130 static struct c_expr
7131 c_parser_alignof_expression (c_parser *parser)
7133 struct c_expr expr;
7134 location_t start_loc = c_parser_peek_token (parser)->location;
7135 location_t end_loc;
7136 tree alignof_spelling = c_parser_peek_token (parser)->value;
7137 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7138 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7139 "_Alignof") == 0;
7140 /* A diagnostic is not required for the use of this identifier in
7141 the implementation namespace; only diagnose it for the C11
7142 spelling because of existing code using the other spellings. */
7143 if (is_c11_alignof)
7145 if (flag_isoc99)
7146 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7147 alignof_spelling);
7148 else
7149 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7150 alignof_spelling);
7152 c_parser_consume_token (parser);
7153 c_inhibit_evaluation_warnings++;
7154 in_alignof++;
7155 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7156 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7158 /* Either __alignof__ ( type-name ) or __alignof__
7159 unary-expression starting with a compound literal. */
7160 location_t loc;
7161 struct c_type_name *type_name;
7162 struct c_expr ret;
7163 c_parser_consume_token (parser);
7164 loc = c_parser_peek_token (parser)->location;
7165 type_name = c_parser_type_name (parser);
7166 end_loc = c_parser_peek_token (parser)->location;
7167 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
7168 if (type_name == NULL)
7170 struct c_expr ret;
7171 c_inhibit_evaluation_warnings--;
7172 in_alignof--;
7173 ret.value = error_mark_node;
7174 ret.original_code = ERROR_MARK;
7175 ret.original_type = NULL;
7176 return ret;
7178 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7180 expr = c_parser_postfix_expression_after_paren_type (parser,
7181 type_name,
7182 loc);
7183 goto alignof_expr;
7185 /* alignof ( type-name ). */
7186 c_inhibit_evaluation_warnings--;
7187 in_alignof--;
7188 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7189 NULL, NULL),
7190 false, is_c11_alignof, 1);
7191 ret.original_code = ERROR_MARK;
7192 ret.original_type = NULL;
7193 set_c_expr_source_range (&ret, start_loc, end_loc);
7194 return ret;
7196 else
7198 struct c_expr ret;
7199 expr = c_parser_unary_expression (parser);
7200 end_loc = expr.src_range.m_finish;
7201 alignof_expr:
7202 mark_exp_read (expr.value);
7203 c_inhibit_evaluation_warnings--;
7204 in_alignof--;
7205 if (is_c11_alignof)
7206 pedwarn (start_loc,
7207 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7208 alignof_spelling);
7209 ret.value = c_alignof_expr (start_loc, expr.value);
7210 ret.original_code = ERROR_MARK;
7211 ret.original_type = NULL;
7212 set_c_expr_source_range (&ret, start_loc, end_loc);
7213 return ret;
7217 /* Helper function to read arguments of builtins which are interfaces
7218 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7219 others. The name of the builtin is passed using BNAME parameter.
7220 Function returns true if there were no errors while parsing and
7221 stores the arguments in CEXPR_LIST. If it returns true,
7222 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7223 parenthesis. */
7224 static bool
7225 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7226 vec<c_expr_t, va_gc> **ret_cexpr_list,
7227 bool choose_expr_p,
7228 location_t *out_close_paren_loc)
7230 location_t loc = c_parser_peek_token (parser)->location;
7231 vec<c_expr_t, va_gc> *cexpr_list;
7232 c_expr_t expr;
7233 bool saved_force_folding_builtin_constant_p;
7235 *ret_cexpr_list = NULL;
7236 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7238 error_at (loc, "cannot take address of %qs", bname);
7239 return false;
7242 c_parser_consume_token (parser);
7244 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7246 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7247 c_parser_consume_token (parser);
7248 return true;
7251 saved_force_folding_builtin_constant_p
7252 = force_folding_builtin_constant_p;
7253 force_folding_builtin_constant_p |= choose_expr_p;
7254 expr = c_parser_expr_no_commas (parser, NULL);
7255 force_folding_builtin_constant_p
7256 = saved_force_folding_builtin_constant_p;
7257 vec_alloc (cexpr_list, 1);
7258 vec_safe_push (cexpr_list, expr);
7259 while (c_parser_next_token_is (parser, CPP_COMMA))
7261 c_parser_consume_token (parser);
7262 expr = c_parser_expr_no_commas (parser, NULL);
7263 vec_safe_push (cexpr_list, expr);
7266 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7267 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7268 return false;
7270 *ret_cexpr_list = cexpr_list;
7271 return true;
7274 /* This represents a single generic-association. */
7276 struct c_generic_association
7278 /* The location of the starting token of the type. */
7279 location_t type_location;
7280 /* The association's type, or NULL_TREE for 'default'. */
7281 tree type;
7282 /* The association's expression. */
7283 struct c_expr expression;
7286 /* Parse a generic-selection. (C11 6.5.1.1).
7288 generic-selection:
7289 _Generic ( assignment-expression , generic-assoc-list )
7291 generic-assoc-list:
7292 generic-association
7293 generic-assoc-list , generic-association
7295 generic-association:
7296 type-name : assignment-expression
7297 default : assignment-expression
7300 static struct c_expr
7301 c_parser_generic_selection (c_parser *parser)
7303 struct c_expr selector, error_expr;
7304 tree selector_type;
7305 struct c_generic_association matched_assoc;
7306 bool match_found = false;
7307 location_t generic_loc, selector_loc;
7309 error_expr.original_code = ERROR_MARK;
7310 error_expr.original_type = NULL;
7311 error_expr.set_error ();
7312 matched_assoc.type_location = UNKNOWN_LOCATION;
7313 matched_assoc.type = NULL_TREE;
7314 matched_assoc.expression = error_expr;
7316 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7317 generic_loc = c_parser_peek_token (parser)->location;
7318 c_parser_consume_token (parser);
7319 if (flag_isoc99)
7320 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7321 "ISO C99 does not support %<_Generic%>");
7322 else
7323 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7324 "ISO C90 does not support %<_Generic%>");
7326 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7327 return error_expr;
7329 c_inhibit_evaluation_warnings++;
7330 selector_loc = c_parser_peek_token (parser)->location;
7331 selector = c_parser_expr_no_commas (parser, NULL);
7332 selector = default_function_array_conversion (selector_loc, selector);
7333 c_inhibit_evaluation_warnings--;
7335 if (selector.value == error_mark_node)
7337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7338 return selector;
7340 selector_type = TREE_TYPE (selector.value);
7341 /* In ISO C terms, rvalues (including the controlling expression of
7342 _Generic) do not have qualified types. */
7343 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7344 selector_type = TYPE_MAIN_VARIANT (selector_type);
7345 /* In ISO C terms, _Noreturn is not part of the type of expressions
7346 such as &abort, but in GCC it is represented internally as a type
7347 qualifier. */
7348 if (FUNCTION_POINTER_TYPE_P (selector_type)
7349 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7350 selector_type
7351 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7353 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7355 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7356 return error_expr;
7359 auto_vec<c_generic_association> associations;
7360 while (1)
7362 struct c_generic_association assoc, *iter;
7363 unsigned int ix;
7364 c_token *token = c_parser_peek_token (parser);
7366 assoc.type_location = token->location;
7367 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7369 c_parser_consume_token (parser);
7370 assoc.type = NULL_TREE;
7372 else
7374 struct c_type_name *type_name;
7376 type_name = c_parser_type_name (parser);
7377 if (type_name == NULL)
7379 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7380 return error_expr;
7382 assoc.type = groktypename (type_name, NULL, NULL);
7383 if (assoc.type == error_mark_node)
7385 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7386 return error_expr;
7389 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7390 error_at (assoc.type_location,
7391 "%<_Generic%> association has function type");
7392 else if (!COMPLETE_TYPE_P (assoc.type))
7393 error_at (assoc.type_location,
7394 "%<_Generic%> association has incomplete type");
7396 if (variably_modified_type_p (assoc.type, NULL_TREE))
7397 error_at (assoc.type_location,
7398 "%<_Generic%> association has "
7399 "variable length type");
7402 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7404 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7405 return error_expr;
7408 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7409 if (assoc.expression.value == error_mark_node)
7411 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7412 return error_expr;
7415 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7417 if (assoc.type == NULL_TREE)
7419 if (iter->type == NULL_TREE)
7421 error_at (assoc.type_location,
7422 "duplicate %<default%> case in %<_Generic%>");
7423 inform (iter->type_location, "original %<default%> is here");
7426 else if (iter->type != NULL_TREE)
7428 if (comptypes (assoc.type, iter->type))
7430 error_at (assoc.type_location,
7431 "%<_Generic%> specifies two compatible types");
7432 inform (iter->type_location, "compatible type is here");
7437 if (assoc.type == NULL_TREE)
7439 if (!match_found)
7441 matched_assoc = assoc;
7442 match_found = true;
7445 else if (comptypes (assoc.type, selector_type))
7447 if (!match_found || matched_assoc.type == NULL_TREE)
7449 matched_assoc = assoc;
7450 match_found = true;
7452 else
7454 error_at (assoc.type_location,
7455 "%<_Generic%> selector matches multiple associations");
7456 inform (matched_assoc.type_location,
7457 "other match is here");
7461 associations.safe_push (assoc);
7463 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7464 break;
7465 c_parser_consume_token (parser);
7468 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7470 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7471 return error_expr;
7474 if (!match_found)
7476 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7477 "compatible with any association",
7478 selector_type);
7479 return error_expr;
7482 return matched_assoc.expression;
7485 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2).
7487 postfix-expression:
7488 primary-expression
7489 postfix-expression [ expression ]
7490 postfix-expression ( argument-expression-list[opt] )
7491 postfix-expression . identifier
7492 postfix-expression -> identifier
7493 postfix-expression ++
7494 postfix-expression --
7495 ( type-name ) { initializer-list }
7496 ( type-name ) { initializer-list , }
7498 argument-expression-list:
7499 argument-expression
7500 argument-expression-list , argument-expression
7502 primary-expression:
7503 identifier
7504 constant
7505 string-literal
7506 ( expression )
7507 generic-selection
7509 GNU extensions:
7511 primary-expression:
7512 __func__
7513 (treated as a keyword in GNU C)
7514 __FUNCTION__
7515 __PRETTY_FUNCTION__
7516 ( compound-statement )
7517 __builtin_va_arg ( assignment-expression , type-name )
7518 __builtin_offsetof ( type-name , offsetof-member-designator )
7519 __builtin_choose_expr ( assignment-expression ,
7520 assignment-expression ,
7521 assignment-expression )
7522 __builtin_types_compatible_p ( type-name , type-name )
7523 __builtin_complex ( assignment-expression , assignment-expression )
7524 __builtin_shuffle ( assignment-expression , assignment-expression )
7525 __builtin_shuffle ( assignment-expression ,
7526 assignment-expression ,
7527 assignment-expression, )
7529 offsetof-member-designator:
7530 identifier
7531 offsetof-member-designator . identifier
7532 offsetof-member-designator [ expression ]
7534 Objective-C:
7536 primary-expression:
7537 [ objc-receiver objc-message-args ]
7538 @selector ( objc-selector-arg )
7539 @protocol ( identifier )
7540 @encode ( type-name )
7541 objc-string-literal
7542 Classname . identifier
7545 static struct c_expr
7546 c_parser_postfix_expression (c_parser *parser)
7548 struct c_expr expr, e1;
7549 struct c_type_name *t1, *t2;
7550 location_t loc = c_parser_peek_token (parser)->location;;
7551 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7552 expr.original_code = ERROR_MARK;
7553 expr.original_type = NULL;
7554 switch (c_parser_peek_token (parser)->type)
7556 case CPP_NUMBER:
7557 expr.value = c_parser_peek_token (parser)->value;
7558 set_c_expr_source_range (&expr, tok_range);
7559 loc = c_parser_peek_token (parser)->location;
7560 c_parser_consume_token (parser);
7561 if (TREE_CODE (expr.value) == FIXED_CST
7562 && !targetm.fixed_point_supported_p ())
7564 error_at (loc, "fixed-point types not supported for this target");
7565 expr.value = error_mark_node;
7567 break;
7568 case CPP_CHAR:
7569 case CPP_CHAR16:
7570 case CPP_CHAR32:
7571 case CPP_WCHAR:
7572 expr.value = c_parser_peek_token (parser)->value;
7573 /* For the purpose of warning when a pointer is compared with
7574 a zero character constant. */
7575 expr.original_type = char_type_node;
7576 set_c_expr_source_range (&expr, tok_range);
7577 c_parser_consume_token (parser);
7578 break;
7579 case CPP_STRING:
7580 case CPP_STRING16:
7581 case CPP_STRING32:
7582 case CPP_WSTRING:
7583 case CPP_UTF8STRING:
7584 expr.value = c_parser_peek_token (parser)->value;
7585 set_c_expr_source_range (&expr, tok_range);
7586 expr.original_code = STRING_CST;
7587 c_parser_consume_token (parser);
7588 break;
7589 case CPP_OBJC_STRING:
7590 gcc_assert (c_dialect_objc ());
7591 expr.value
7592 = objc_build_string_object (c_parser_peek_token (parser)->value);
7593 set_c_expr_source_range (&expr, tok_range);
7594 c_parser_consume_token (parser);
7595 break;
7596 case CPP_NAME:
7597 switch (c_parser_peek_token (parser)->id_kind)
7599 case C_ID_ID:
7601 tree id = c_parser_peek_token (parser)->value;
7602 c_parser_consume_token (parser);
7603 expr.value = build_external_ref (loc, id,
7604 (c_parser_peek_token (parser)->type
7605 == CPP_OPEN_PAREN),
7606 &expr.original_type);
7607 set_c_expr_source_range (&expr, tok_range);
7608 break;
7610 case C_ID_CLASSNAME:
7612 /* Here we parse the Objective-C 2.0 Class.name dot
7613 syntax. */
7614 tree class_name = c_parser_peek_token (parser)->value;
7615 tree component;
7616 c_parser_consume_token (parser);
7617 gcc_assert (c_dialect_objc ());
7618 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7620 expr.set_error ();
7621 break;
7623 if (c_parser_next_token_is_not (parser, CPP_NAME))
7625 c_parser_error (parser, "expected identifier");
7626 expr.set_error ();
7627 break;
7629 c_token *component_tok = c_parser_peek_token (parser);
7630 component = component_tok->value;
7631 location_t end_loc = component_tok->get_finish ();
7632 c_parser_consume_token (parser);
7633 expr.value = objc_build_class_component_ref (class_name,
7634 component);
7635 set_c_expr_source_range (&expr, loc, end_loc);
7636 break;
7638 default:
7639 c_parser_error (parser, "expected expression");
7640 expr.set_error ();
7641 break;
7643 break;
7644 case CPP_OPEN_PAREN:
7645 /* A parenthesized expression, statement expression or compound
7646 literal. */
7647 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7649 /* A statement expression. */
7650 tree stmt;
7651 location_t brace_loc;
7652 c_parser_consume_token (parser);
7653 brace_loc = c_parser_peek_token (parser)->location;
7654 c_parser_consume_token (parser);
7655 if (!building_stmt_list_p ())
7657 error_at (loc, "braced-group within expression allowed "
7658 "only inside a function");
7659 parser->error = true;
7660 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7661 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7662 expr.set_error ();
7663 break;
7665 stmt = c_begin_stmt_expr ();
7666 c_parser_compound_statement_nostart (parser);
7667 location_t close_loc = c_parser_peek_token (parser)->location;
7668 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7669 "expected %<)%>");
7670 pedwarn (loc, OPT_Wpedantic,
7671 "ISO C forbids braced-groups within expressions");
7672 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7673 set_c_expr_source_range (&expr, loc, close_loc);
7674 mark_exp_read (expr.value);
7676 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7678 /* A compound literal. ??? Can we actually get here rather
7679 than going directly to
7680 c_parser_postfix_expression_after_paren_type from
7681 elsewhere? */
7682 location_t loc;
7683 struct c_type_name *type_name;
7684 c_parser_consume_token (parser);
7685 loc = c_parser_peek_token (parser)->location;
7686 type_name = c_parser_type_name (parser);
7687 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7688 "expected %<)%>");
7689 if (type_name == NULL)
7691 expr.set_error ();
7693 else
7694 expr = c_parser_postfix_expression_after_paren_type (parser,
7695 type_name,
7696 loc);
7698 else
7700 /* A parenthesized expression. */
7701 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7702 c_parser_consume_token (parser);
7703 expr = c_parser_expression (parser);
7704 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7705 TREE_NO_WARNING (expr.value) = 1;
7706 if (expr.original_code != C_MAYBE_CONST_EXPR)
7707 expr.original_code = ERROR_MARK;
7708 /* Don't change EXPR.ORIGINAL_TYPE. */
7709 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7710 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7711 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7712 "expected %<)%>");
7714 break;
7715 case CPP_KEYWORD:
7716 switch (c_parser_peek_token (parser)->keyword)
7718 case RID_FUNCTION_NAME:
7719 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7720 "%<__FUNCTION__%> predefined identifier");
7721 expr.value = fname_decl (loc,
7722 c_parser_peek_token (parser)->keyword,
7723 c_parser_peek_token (parser)->value);
7724 set_c_expr_source_range (&expr, loc, loc);
7725 c_parser_consume_token (parser);
7726 break;
7727 case RID_PRETTY_FUNCTION_NAME:
7728 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
7729 "%<__PRETTY_FUNCTION__%> predefined identifier");
7730 expr.value = fname_decl (loc,
7731 c_parser_peek_token (parser)->keyword,
7732 c_parser_peek_token (parser)->value);
7733 set_c_expr_source_range (&expr, loc, loc);
7734 c_parser_consume_token (parser);
7735 break;
7736 case RID_C99_FUNCTION_NAME:
7737 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
7738 "%<__func__%> predefined identifier");
7739 expr.value = fname_decl (loc,
7740 c_parser_peek_token (parser)->keyword,
7741 c_parser_peek_token (parser)->value);
7742 set_c_expr_source_range (&expr, loc, loc);
7743 c_parser_consume_token (parser);
7744 break;
7745 case RID_VA_ARG:
7747 location_t start_loc = loc;
7748 c_parser_consume_token (parser);
7749 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7751 expr.set_error ();
7752 break;
7754 e1 = c_parser_expr_no_commas (parser, NULL);
7755 mark_exp_read (e1.value);
7756 e1.value = c_fully_fold (e1.value, false, NULL);
7757 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7759 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7760 expr.set_error ();
7761 break;
7763 loc = c_parser_peek_token (parser)->location;
7764 t1 = c_parser_type_name (parser);
7765 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7766 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7767 "expected %<)%>");
7768 if (t1 == NULL)
7770 expr.set_error ();
7772 else
7774 tree type_expr = NULL_TREE;
7775 expr.value = c_build_va_arg (start_loc, e1.value, loc,
7776 groktypename (t1, &type_expr, NULL));
7777 if (type_expr)
7779 expr.value = build2 (C_MAYBE_CONST_EXPR,
7780 TREE_TYPE (expr.value), type_expr,
7781 expr.value);
7782 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
7784 set_c_expr_source_range (&expr, start_loc, end_loc);
7787 break;
7788 case RID_OFFSETOF:
7789 c_parser_consume_token (parser);
7790 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7792 expr.set_error ();
7793 break;
7795 t1 = c_parser_type_name (parser);
7796 if (t1 == NULL)
7797 parser->error = true;
7798 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7799 gcc_assert (parser->error);
7800 if (parser->error)
7802 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7803 expr.set_error ();
7804 break;
7808 tree type = groktypename (t1, NULL, NULL);
7809 tree offsetof_ref;
7810 if (type == error_mark_node)
7811 offsetof_ref = error_mark_node;
7812 else
7814 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
7815 SET_EXPR_LOCATION (offsetof_ref, loc);
7817 /* Parse the second argument to __builtin_offsetof. We
7818 must have one identifier, and beyond that we want to
7819 accept sub structure and sub array references. */
7820 if (c_parser_next_token_is (parser, CPP_NAME))
7822 c_token *comp_tok = c_parser_peek_token (parser);
7823 offsetof_ref = build_component_ref
7824 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
7825 c_parser_consume_token (parser);
7826 while (c_parser_next_token_is (parser, CPP_DOT)
7827 || c_parser_next_token_is (parser,
7828 CPP_OPEN_SQUARE)
7829 || c_parser_next_token_is (parser,
7830 CPP_DEREF))
7832 if (c_parser_next_token_is (parser, CPP_DEREF))
7834 loc = c_parser_peek_token (parser)->location;
7835 offsetof_ref = build_array_ref (loc,
7836 offsetof_ref,
7837 integer_zero_node);
7838 goto do_dot;
7840 else if (c_parser_next_token_is (parser, CPP_DOT))
7842 do_dot:
7843 c_parser_consume_token (parser);
7844 if (c_parser_next_token_is_not (parser,
7845 CPP_NAME))
7847 c_parser_error (parser, "expected identifier");
7848 break;
7850 c_token *comp_tok = c_parser_peek_token (parser);
7851 offsetof_ref = build_component_ref
7852 (loc, offsetof_ref, comp_tok->value,
7853 comp_tok->location);
7854 c_parser_consume_token (parser);
7856 else
7858 struct c_expr ce;
7859 tree idx;
7860 loc = c_parser_peek_token (parser)->location;
7861 c_parser_consume_token (parser);
7862 ce = c_parser_expression (parser);
7863 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
7864 idx = ce.value;
7865 idx = c_fully_fold (idx, false, NULL);
7866 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
7867 "expected %<]%>");
7868 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
7872 else
7873 c_parser_error (parser, "expected identifier");
7874 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
7875 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7876 "expected %<)%>");
7877 expr.value = fold_offsetof (offsetof_ref);
7878 set_c_expr_source_range (&expr, loc, end_loc);
7880 break;
7881 case RID_CHOOSE_EXPR:
7883 vec<c_expr_t, va_gc> *cexpr_list;
7884 c_expr_t *e1_p, *e2_p, *e3_p;
7885 tree c;
7886 location_t close_paren_loc;
7888 c_parser_consume_token (parser);
7889 if (!c_parser_get_builtin_args (parser,
7890 "__builtin_choose_expr",
7891 &cexpr_list, true,
7892 &close_paren_loc))
7894 expr.set_error ();
7895 break;
7898 if (vec_safe_length (cexpr_list) != 3)
7900 error_at (loc, "wrong number of arguments to "
7901 "%<__builtin_choose_expr%>");
7902 expr.set_error ();
7903 break;
7906 e1_p = &(*cexpr_list)[0];
7907 e2_p = &(*cexpr_list)[1];
7908 e3_p = &(*cexpr_list)[2];
7910 c = e1_p->value;
7911 mark_exp_read (e2_p->value);
7912 mark_exp_read (e3_p->value);
7913 if (TREE_CODE (c) != INTEGER_CST
7914 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
7915 error_at (loc,
7916 "first argument to %<__builtin_choose_expr%> not"
7917 " a constant");
7918 constant_expression_warning (c);
7919 expr = integer_zerop (c) ? *e3_p : *e2_p;
7920 set_c_expr_source_range (&expr, loc, close_paren_loc);
7921 break;
7923 case RID_TYPES_COMPATIBLE_P:
7924 c_parser_consume_token (parser);
7925 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
7927 expr.set_error ();
7928 break;
7930 t1 = c_parser_type_name (parser);
7931 if (t1 == NULL)
7933 expr.set_error ();
7934 break;
7936 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7938 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7939 expr.set_error ();
7940 break;
7942 t2 = c_parser_type_name (parser);
7943 if (t2 == NULL)
7945 expr.set_error ();
7946 break;
7949 location_t close_paren_loc = c_parser_peek_token (parser)->location;
7950 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7951 "expected %<)%>");
7952 tree e1, e2;
7953 e1 = groktypename (t1, NULL, NULL);
7954 e2 = groktypename (t2, NULL, NULL);
7955 if (e1 == error_mark_node || e2 == error_mark_node)
7957 expr.set_error ();
7958 break;
7961 e1 = TYPE_MAIN_VARIANT (e1);
7962 e2 = TYPE_MAIN_VARIANT (e2);
7964 expr.value
7965 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
7966 set_c_expr_source_range (&expr, loc, close_paren_loc);
7968 break;
7969 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
7971 vec<c_expr_t, va_gc> *cexpr_list;
7972 c_expr_t *e2_p;
7973 tree chain_value;
7974 location_t close_paren_loc;
7976 c_parser_consume_token (parser);
7977 if (!c_parser_get_builtin_args (parser,
7978 "__builtin_call_with_static_chain",
7979 &cexpr_list, false,
7980 &close_paren_loc))
7982 expr.set_error ();
7983 break;
7985 if (vec_safe_length (cexpr_list) != 2)
7987 error_at (loc, "wrong number of arguments to "
7988 "%<__builtin_call_with_static_chain%>");
7989 expr.set_error ();
7990 break;
7993 expr = (*cexpr_list)[0];
7994 e2_p = &(*cexpr_list)[1];
7995 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
7996 chain_value = e2_p->value;
7997 mark_exp_read (chain_value);
7999 if (TREE_CODE (expr.value) != CALL_EXPR)
8000 error_at (loc, "first argument to "
8001 "%<__builtin_call_with_static_chain%> "
8002 "must be a call expression");
8003 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8004 error_at (loc, "second argument to "
8005 "%<__builtin_call_with_static_chain%> "
8006 "must be a pointer type");
8007 else
8008 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8009 set_c_expr_source_range (&expr, loc, close_paren_loc);
8010 break;
8012 case RID_BUILTIN_COMPLEX:
8014 vec<c_expr_t, va_gc> *cexpr_list;
8015 c_expr_t *e1_p, *e2_p;
8016 location_t close_paren_loc;
8018 c_parser_consume_token (parser);
8019 if (!c_parser_get_builtin_args (parser,
8020 "__builtin_complex",
8021 &cexpr_list, false,
8022 &close_paren_loc))
8024 expr.set_error ();
8025 break;
8028 if (vec_safe_length (cexpr_list) != 2)
8030 error_at (loc, "wrong number of arguments to "
8031 "%<__builtin_complex%>");
8032 expr.set_error ();
8033 break;
8036 e1_p = &(*cexpr_list)[0];
8037 e2_p = &(*cexpr_list)[1];
8039 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8040 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8041 e1_p->value = convert (TREE_TYPE (e1_p->value),
8042 TREE_OPERAND (e1_p->value, 0));
8043 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8044 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8045 e2_p->value = convert (TREE_TYPE (e2_p->value),
8046 TREE_OPERAND (e2_p->value, 0));
8047 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8048 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8049 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8050 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8052 error_at (loc, "%<__builtin_complex%> operand "
8053 "not of real binary floating-point type");
8054 expr.set_error ();
8055 break;
8057 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8058 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8060 error_at (loc,
8061 "%<__builtin_complex%> operands of different types");
8062 expr.set_error ();
8063 break;
8065 pedwarn_c90 (loc, OPT_Wpedantic,
8066 "ISO C90 does not support complex types");
8067 expr.value = build2_loc (loc, COMPLEX_EXPR,
8068 build_complex_type
8069 (TYPE_MAIN_VARIANT
8070 (TREE_TYPE (e1_p->value))),
8071 e1_p->value, e2_p->value);
8072 set_c_expr_source_range (&expr, loc, close_paren_loc);
8073 break;
8075 case RID_BUILTIN_SHUFFLE:
8077 vec<c_expr_t, va_gc> *cexpr_list;
8078 unsigned int i;
8079 c_expr_t *p;
8080 location_t close_paren_loc;
8082 c_parser_consume_token (parser);
8083 if (!c_parser_get_builtin_args (parser,
8084 "__builtin_shuffle",
8085 &cexpr_list, false,
8086 &close_paren_loc))
8088 expr.set_error ();
8089 break;
8092 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8093 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8095 if (vec_safe_length (cexpr_list) == 2)
8096 expr.value =
8097 c_build_vec_perm_expr
8098 (loc, (*cexpr_list)[0].value,
8099 NULL_TREE, (*cexpr_list)[1].value);
8101 else if (vec_safe_length (cexpr_list) == 3)
8102 expr.value =
8103 c_build_vec_perm_expr
8104 (loc, (*cexpr_list)[0].value,
8105 (*cexpr_list)[1].value,
8106 (*cexpr_list)[2].value);
8107 else
8109 error_at (loc, "wrong number of arguments to "
8110 "%<__builtin_shuffle%>");
8111 expr.set_error ();
8113 set_c_expr_source_range (&expr, loc, close_paren_loc);
8114 break;
8116 case RID_AT_SELECTOR:
8117 gcc_assert (c_dialect_objc ());
8118 c_parser_consume_token (parser);
8119 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8121 expr.set_error ();
8122 break;
8125 tree sel = c_parser_objc_selector_arg (parser);
8126 location_t close_loc = c_parser_peek_token (parser)->location;
8127 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8128 "expected %<)%>");
8129 expr.value = objc_build_selector_expr (loc, sel);
8130 set_c_expr_source_range (&expr, loc, close_loc);
8132 break;
8133 case RID_AT_PROTOCOL:
8134 gcc_assert (c_dialect_objc ());
8135 c_parser_consume_token (parser);
8136 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8138 expr.set_error ();
8139 break;
8141 if (c_parser_next_token_is_not (parser, CPP_NAME))
8143 c_parser_error (parser, "expected identifier");
8144 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8145 expr.set_error ();
8146 break;
8149 tree id = c_parser_peek_token (parser)->value;
8150 c_parser_consume_token (parser);
8151 location_t close_loc = c_parser_peek_token (parser)->location;
8152 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8153 "expected %<)%>");
8154 expr.value = objc_build_protocol_expr (id);
8155 set_c_expr_source_range (&expr, loc, close_loc);
8157 break;
8158 case RID_AT_ENCODE:
8159 /* Extension to support C-structures in the archiver. */
8160 gcc_assert (c_dialect_objc ());
8161 c_parser_consume_token (parser);
8162 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
8164 expr.set_error ();
8165 break;
8167 t1 = c_parser_type_name (parser);
8168 if (t1 == NULL)
8170 expr.set_error ();
8171 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8172 break;
8175 location_t close_loc = c_parser_peek_token (parser)->location;
8176 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8177 "expected %<)%>");
8178 tree type = groktypename (t1, NULL, NULL);
8179 expr.value = objc_build_encode_expr (type);
8180 set_c_expr_source_range (&expr, loc, close_loc);
8182 break;
8183 case RID_GENERIC:
8184 expr = c_parser_generic_selection (parser);
8185 break;
8186 case RID_CILK_SPAWN:
8187 c_parser_consume_token (parser);
8188 if (!flag_cilkplus)
8190 error_at (loc, "-fcilkplus must be enabled to use "
8191 "%<_Cilk_spawn%>");
8192 expr = c_parser_cast_expression (parser, NULL);
8193 expr.set_error ();
8195 else if (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8197 error_at (loc, "consecutive %<_Cilk_spawn%> keywords "
8198 "are not permitted");
8199 /* Now flush out all the _Cilk_spawns. */
8200 while (c_parser_peek_token (parser)->keyword == RID_CILK_SPAWN)
8201 c_parser_consume_token (parser);
8202 expr = c_parser_cast_expression (parser, NULL);
8204 else
8206 expr = c_parser_cast_expression (parser, NULL);
8207 expr.value = build_cilk_spawn (loc, expr.value);
8209 break;
8210 default:
8211 c_parser_error (parser, "expected expression");
8212 expr.set_error ();
8213 break;
8215 break;
8216 case CPP_OPEN_SQUARE:
8217 if (c_dialect_objc ())
8219 tree receiver, args;
8220 c_parser_consume_token (parser);
8221 receiver = c_parser_objc_receiver (parser);
8222 args = c_parser_objc_message_args (parser);
8223 location_t close_loc = c_parser_peek_token (parser)->location;
8224 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8225 "expected %<]%>");
8226 expr.value = objc_build_message_expr (receiver, args);
8227 set_c_expr_source_range (&expr, loc, close_loc);
8228 break;
8230 /* Else fall through to report error. */
8231 /* FALLTHRU */
8232 default:
8233 c_parser_error (parser, "expected expression");
8234 expr.set_error ();
8235 break;
8237 return c_parser_postfix_expression_after_primary
8238 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
8241 /* Parse a postfix expression after a parenthesized type name: the
8242 brace-enclosed initializer of a compound literal, possibly followed
8243 by some postfix operators. This is separate because it is not
8244 possible to tell until after the type name whether a cast
8245 expression has a cast or a compound literal, or whether the operand
8246 of sizeof is a parenthesized type name or starts with a compound
8247 literal. TYPE_LOC is the location where TYPE_NAME starts--the
8248 location of the first token after the parentheses around the type
8249 name. */
8251 static struct c_expr
8252 c_parser_postfix_expression_after_paren_type (c_parser *parser,
8253 struct c_type_name *type_name,
8254 location_t type_loc)
8256 tree type;
8257 struct c_expr init;
8258 bool non_const;
8259 struct c_expr expr;
8260 location_t start_loc;
8261 tree type_expr = NULL_TREE;
8262 bool type_expr_const = true;
8263 check_compound_literal_type (type_loc, type_name);
8264 rich_location richloc (line_table, type_loc);
8265 start_init (NULL_TREE, NULL, 0, &richloc);
8266 type = groktypename (type_name, &type_expr, &type_expr_const);
8267 start_loc = c_parser_peek_token (parser)->location;
8268 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
8270 error_at (type_loc, "compound literal has variable size");
8271 type = error_mark_node;
8273 init = c_parser_braced_init (parser, type, false, NULL);
8274 finish_init ();
8275 maybe_warn_string_init (type_loc, type, init);
8277 if (type != error_mark_node
8278 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
8279 && current_function_decl)
8281 error ("compound literal qualified by address-space qualifier");
8282 type = error_mark_node;
8285 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
8286 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
8287 ? CONSTRUCTOR_NON_CONST (init.value)
8288 : init.original_code == C_MAYBE_CONST_EXPR);
8289 non_const |= !type_expr_const;
8290 expr.value = build_compound_literal (start_loc, type, init.value, non_const);
8291 set_c_expr_source_range (&expr, init.src_range);
8292 expr.original_code = ERROR_MARK;
8293 expr.original_type = NULL;
8294 if (type != error_mark_node
8295 && expr.value != error_mark_node
8296 && type_expr)
8298 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
8300 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
8301 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
8303 else
8305 gcc_assert (!non_const);
8306 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
8307 type_expr, expr.value);
8310 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
8313 /* Callback function for sizeof_pointer_memaccess_warning to compare
8314 types. */
8316 static bool
8317 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
8319 return comptypes (type1, type2) == 1;
8322 /* Parse a postfix expression after the initial primary or compound
8323 literal; that is, parse a series of postfix operators.
8325 EXPR_LOC is the location of the primary expression. */
8327 static struct c_expr
8328 c_parser_postfix_expression_after_primary (c_parser *parser,
8329 location_t expr_loc,
8330 struct c_expr expr)
8332 struct c_expr orig_expr;
8333 tree ident, idx;
8334 location_t sizeof_arg_loc[3], comp_loc;
8335 tree sizeof_arg[3];
8336 unsigned int literal_zero_mask;
8337 unsigned int i;
8338 vec<tree, va_gc> *exprlist;
8339 vec<tree, va_gc> *origtypes = NULL;
8340 vec<location_t> arg_loc = vNULL;
8341 location_t start;
8342 location_t finish;
8344 while (true)
8346 location_t op_loc = c_parser_peek_token (parser)->location;
8347 switch (c_parser_peek_token (parser)->type)
8349 case CPP_OPEN_SQUARE:
8350 /* Array reference. */
8351 c_parser_consume_token (parser);
8352 if (flag_cilkplus
8353 && c_parser_peek_token (parser)->type == CPP_COLON)
8354 /* If we are here, then we have something like this:
8355 Array [ : ]
8357 expr.value = c_parser_array_notation (expr_loc, parser, NULL_TREE,
8358 expr.value);
8359 else
8361 idx = c_parser_expression (parser).value;
8362 /* Here we have 3 options:
8363 1. Array [EXPR] -- Normal Array call.
8364 2. Array [EXPR : EXPR] -- Array notation without stride.
8365 3. Array [EXPR : EXPR : EXPR] -- Array notation with stride.
8367 For 1, we just handle it just like a normal array expression.
8368 For 2 and 3 we handle it like we handle array notations. The
8369 idx value we have above becomes the initial/start index.
8371 if (flag_cilkplus
8372 && c_parser_peek_token (parser)->type == CPP_COLON)
8373 expr.value = c_parser_array_notation (expr_loc, parser, idx,
8374 expr.value);
8375 else
8377 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8378 "expected %<]%>");
8379 start = expr.get_start ();
8380 finish = parser->tokens_buf[0].location;
8381 expr.value = build_array_ref (op_loc, expr.value, idx);
8382 set_c_expr_source_range (&expr, start, finish);
8385 expr.original_code = ERROR_MARK;
8386 expr.original_type = NULL;
8387 break;
8388 case CPP_OPEN_PAREN:
8389 /* Function call. */
8390 c_parser_consume_token (parser);
8391 for (i = 0; i < 3; i++)
8393 sizeof_arg[i] = NULL_TREE;
8394 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
8396 literal_zero_mask = 0;
8397 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8398 exprlist = NULL;
8399 else
8400 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
8401 sizeof_arg_loc, sizeof_arg,
8402 &arg_loc, &literal_zero_mask);
8403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8404 "expected %<)%>");
8405 orig_expr = expr;
8406 mark_exp_read (expr.value);
8407 if (warn_sizeof_pointer_memaccess)
8408 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
8409 expr.value, exprlist,
8410 sizeof_arg,
8411 sizeof_ptr_memacc_comptypes);
8412 if (TREE_CODE (expr.value) == FUNCTION_DECL
8413 && DECL_BUILT_IN_CLASS (expr.value) == BUILT_IN_NORMAL
8414 && DECL_FUNCTION_CODE (expr.value) == BUILT_IN_MEMSET
8415 && vec_safe_length (exprlist) == 3)
8417 tree arg0 = (*exprlist)[0];
8418 tree arg2 = (*exprlist)[2];
8419 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
8422 start = expr.get_start ();
8423 finish = parser->tokens_buf[0].get_finish ();
8424 expr.value
8425 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
8426 exprlist, origtypes);
8427 set_c_expr_source_range (&expr, start, finish);
8429 expr.original_code = ERROR_MARK;
8430 if (TREE_CODE (expr.value) == INTEGER_CST
8431 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
8432 && DECL_BUILT_IN_CLASS (orig_expr.value) == BUILT_IN_NORMAL
8433 && DECL_FUNCTION_CODE (orig_expr.value) == BUILT_IN_CONSTANT_P)
8434 expr.original_code = C_MAYBE_CONST_EXPR;
8435 expr.original_type = NULL;
8436 if (exprlist)
8438 release_tree_vector (exprlist);
8439 release_tree_vector (origtypes);
8441 arg_loc.release ();
8442 break;
8443 case CPP_DOT:
8444 /* Structure element reference. */
8445 c_parser_consume_token (parser);
8446 expr = default_function_array_conversion (expr_loc, expr);
8447 if (c_parser_next_token_is (parser, CPP_NAME))
8449 c_token *comp_tok = c_parser_peek_token (parser);
8450 ident = comp_tok->value;
8451 comp_loc = comp_tok->location;
8453 else
8455 c_parser_error (parser, "expected identifier");
8456 expr.set_error ();
8457 expr.original_code = ERROR_MARK;
8458 expr.original_type = NULL;
8459 return expr;
8461 start = expr.get_start ();
8462 finish = c_parser_peek_token (parser)->get_finish ();
8463 c_parser_consume_token (parser);
8464 expr.value = build_component_ref (op_loc, expr.value, ident,
8465 comp_loc);
8466 set_c_expr_source_range (&expr, start, finish);
8467 expr.original_code = ERROR_MARK;
8468 if (TREE_CODE (expr.value) != COMPONENT_REF)
8469 expr.original_type = NULL;
8470 else
8472 /* Remember the original type of a bitfield. */
8473 tree field = TREE_OPERAND (expr.value, 1);
8474 if (TREE_CODE (field) != FIELD_DECL)
8475 expr.original_type = NULL;
8476 else
8477 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8479 break;
8480 case CPP_DEREF:
8481 /* Structure element reference. */
8482 c_parser_consume_token (parser);
8483 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
8484 if (c_parser_next_token_is (parser, CPP_NAME))
8486 c_token *comp_tok = c_parser_peek_token (parser);
8487 ident = comp_tok->value;
8488 comp_loc = comp_tok->location;
8490 else
8492 c_parser_error (parser, "expected identifier");
8493 expr.set_error ();
8494 expr.original_code = ERROR_MARK;
8495 expr.original_type = NULL;
8496 return expr;
8498 start = expr.get_start ();
8499 finish = c_parser_peek_token (parser)->get_finish ();
8500 c_parser_consume_token (parser);
8501 expr.value = build_component_ref (op_loc,
8502 build_indirect_ref (op_loc,
8503 expr.value,
8504 RO_ARROW),
8505 ident, comp_loc);
8506 set_c_expr_source_range (&expr, start, finish);
8507 expr.original_code = ERROR_MARK;
8508 if (TREE_CODE (expr.value) != COMPONENT_REF)
8509 expr.original_type = NULL;
8510 else
8512 /* Remember the original type of a bitfield. */
8513 tree field = TREE_OPERAND (expr.value, 1);
8514 if (TREE_CODE (field) != FIELD_DECL)
8515 expr.original_type = NULL;
8516 else
8517 expr.original_type = DECL_BIT_FIELD_TYPE (field);
8519 break;
8520 case CPP_PLUS_PLUS:
8521 /* Postincrement. */
8522 start = expr.get_start ();
8523 finish = c_parser_peek_token (parser)->get_finish ();
8524 c_parser_consume_token (parser);
8525 /* If the expressions have array notations, we expand them. */
8526 if (flag_cilkplus
8527 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8528 expr = fix_array_notation_expr (expr_loc, POSTINCREMENT_EXPR, expr);
8529 else
8531 expr = default_function_array_read_conversion (expr_loc, expr);
8532 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
8533 expr.value, false);
8535 set_c_expr_source_range (&expr, start, finish);
8536 expr.original_code = ERROR_MARK;
8537 expr.original_type = NULL;
8538 break;
8539 case CPP_MINUS_MINUS:
8540 /* Postdecrement. */
8541 start = expr.get_start ();
8542 finish = c_parser_peek_token (parser)->get_finish ();
8543 c_parser_consume_token (parser);
8544 /* If the expressions have array notations, we expand them. */
8545 if (flag_cilkplus
8546 && TREE_CODE (expr.value) == ARRAY_NOTATION_REF)
8547 expr = fix_array_notation_expr (expr_loc, POSTDECREMENT_EXPR, expr);
8548 else
8550 expr = default_function_array_read_conversion (expr_loc, expr);
8551 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
8552 expr.value, false);
8554 set_c_expr_source_range (&expr, start, finish);
8555 expr.original_code = ERROR_MARK;
8556 expr.original_type = NULL;
8557 break;
8558 default:
8559 return expr;
8564 /* Parse an expression (C90 6.3.17, C99 6.5.17).
8566 expression:
8567 assignment-expression
8568 expression , assignment-expression
8571 static struct c_expr
8572 c_parser_expression (c_parser *parser)
8574 location_t tloc = c_parser_peek_token (parser)->location;
8575 struct c_expr expr;
8576 expr = c_parser_expr_no_commas (parser, NULL);
8577 if (c_parser_next_token_is (parser, CPP_COMMA))
8578 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
8579 while (c_parser_next_token_is (parser, CPP_COMMA))
8581 struct c_expr next;
8582 tree lhsval;
8583 location_t loc = c_parser_peek_token (parser)->location;
8584 location_t expr_loc;
8585 c_parser_consume_token (parser);
8586 expr_loc = c_parser_peek_token (parser)->location;
8587 lhsval = expr.value;
8588 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
8589 lhsval = TREE_OPERAND (lhsval, 1);
8590 if (DECL_P (lhsval) || handled_component_p (lhsval))
8591 mark_exp_read (lhsval);
8592 next = c_parser_expr_no_commas (parser, NULL);
8593 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
8594 expr.value = build_compound_expr (loc, expr.value, next.value);
8595 expr.original_code = COMPOUND_EXPR;
8596 expr.original_type = next.original_type;
8598 return expr;
8601 /* Parse an expression and convert functions or arrays to pointers and
8602 lvalues to rvalues. */
8604 static struct c_expr
8605 c_parser_expression_conv (c_parser *parser)
8607 struct c_expr expr;
8608 location_t loc = c_parser_peek_token (parser)->location;
8609 expr = c_parser_expression (parser);
8610 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
8611 return expr;
8614 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
8615 argument is a literal zero alone and if so, set it in literal_zero_mask. */
8617 static inline void
8618 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
8619 unsigned int idx)
8621 if (idx >= HOST_BITS_PER_INT)
8622 return;
8624 c_token *tok = c_parser_peek_token (parser);
8625 switch (tok->type)
8627 case CPP_NUMBER:
8628 case CPP_CHAR:
8629 case CPP_WCHAR:
8630 case CPP_CHAR16:
8631 case CPP_CHAR32:
8632 /* If a parameter is literal zero alone, remember it
8633 for -Wmemset-transposed-args warning. */
8634 if (integer_zerop (tok->value)
8635 && !TREE_OVERFLOW (tok->value)
8636 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
8637 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
8638 *literal_zero_mask |= 1U << idx;
8639 default:
8640 break;
8644 /* Parse a non-empty list of expressions. If CONVERT_P, convert
8645 functions and arrays to pointers and lvalues to rvalues. If
8646 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
8647 locations of function arguments into this vector.
8649 nonempty-expr-list:
8650 assignment-expression
8651 nonempty-expr-list , assignment-expression
8654 static vec<tree, va_gc> *
8655 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
8656 vec<tree, va_gc> **p_orig_types,
8657 location_t *sizeof_arg_loc, tree *sizeof_arg,
8658 vec<location_t> *locations,
8659 unsigned int *literal_zero_mask)
8661 vec<tree, va_gc> *ret;
8662 vec<tree, va_gc> *orig_types;
8663 struct c_expr expr;
8664 location_t loc = c_parser_peek_token (parser)->location;
8665 location_t cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8666 unsigned int idx = 0;
8668 ret = make_tree_vector ();
8669 if (p_orig_types == NULL)
8670 orig_types = NULL;
8671 else
8672 orig_types = make_tree_vector ();
8674 if (sizeof_arg != NULL
8675 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8676 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8677 if (literal_zero_mask)
8678 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
8679 expr = c_parser_expr_no_commas (parser, NULL);
8680 if (convert_p)
8681 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8682 if (fold_p)
8683 expr.value = c_fully_fold (expr.value, false, NULL);
8684 ret->quick_push (expr.value);
8685 if (orig_types)
8686 orig_types->quick_push (expr.original_type);
8687 if (locations)
8688 locations->safe_push (loc);
8689 if (sizeof_arg != NULL
8690 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8691 && expr.original_code == SIZEOF_EXPR)
8693 sizeof_arg[0] = c_last_sizeof_arg;
8694 sizeof_arg_loc[0] = cur_sizeof_arg_loc;
8696 while (c_parser_next_token_is (parser, CPP_COMMA))
8698 c_parser_consume_token (parser);
8699 loc = c_parser_peek_token (parser)->location;
8700 if (sizeof_arg != NULL
8701 && c_parser_next_token_is_keyword (parser, RID_SIZEOF))
8702 cur_sizeof_arg_loc = c_parser_peek_2nd_token (parser)->location;
8703 else
8704 cur_sizeof_arg_loc = UNKNOWN_LOCATION;
8705 if (literal_zero_mask)
8706 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
8707 expr = c_parser_expr_no_commas (parser, NULL);
8708 if (convert_p)
8709 expr = convert_lvalue_to_rvalue (loc, expr, true, true);
8710 if (fold_p)
8711 expr.value = c_fully_fold (expr.value, false, NULL);
8712 vec_safe_push (ret, expr.value);
8713 if (orig_types)
8714 vec_safe_push (orig_types, expr.original_type);
8715 if (locations)
8716 locations->safe_push (loc);
8717 if (++idx < 3
8718 && sizeof_arg != NULL
8719 && cur_sizeof_arg_loc != UNKNOWN_LOCATION
8720 && expr.original_code == SIZEOF_EXPR)
8722 sizeof_arg[idx] = c_last_sizeof_arg;
8723 sizeof_arg_loc[idx] = cur_sizeof_arg_loc;
8726 if (orig_types)
8727 *p_orig_types = orig_types;
8728 return ret;
8731 /* Parse Objective-C-specific constructs. */
8733 /* Parse an objc-class-definition.
8735 objc-class-definition:
8736 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
8737 objc-class-instance-variables[opt] objc-methodprotolist @end
8738 @implementation identifier objc-superclass[opt]
8739 objc-class-instance-variables[opt]
8740 @interface identifier ( identifier ) objc-protocol-refs[opt]
8741 objc-methodprotolist @end
8742 @interface identifier ( ) objc-protocol-refs[opt]
8743 objc-methodprotolist @end
8744 @implementation identifier ( identifier )
8746 objc-superclass:
8747 : identifier
8749 "@interface identifier (" must start "@interface identifier (
8750 identifier ) ...": objc-methodprotolist in the first production may
8751 not start with a parenthesized identifier as a declarator of a data
8752 definition with no declaration specifiers if the objc-superclass,
8753 objc-protocol-refs and objc-class-instance-variables are omitted. */
8755 static void
8756 c_parser_objc_class_definition (c_parser *parser, tree attributes)
8758 bool iface_p;
8759 tree id1;
8760 tree superclass;
8761 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
8762 iface_p = true;
8763 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
8764 iface_p = false;
8765 else
8766 gcc_unreachable ();
8768 c_parser_consume_token (parser);
8769 if (c_parser_next_token_is_not (parser, CPP_NAME))
8771 c_parser_error (parser, "expected identifier");
8772 return;
8774 id1 = c_parser_peek_token (parser)->value;
8775 c_parser_consume_token (parser);
8776 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
8778 /* We have a category or class extension. */
8779 tree id2;
8780 tree proto = NULL_TREE;
8781 c_parser_consume_token (parser);
8782 if (c_parser_next_token_is_not (parser, CPP_NAME))
8784 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
8786 /* We have a class extension. */
8787 id2 = NULL_TREE;
8789 else
8791 c_parser_error (parser, "expected identifier or %<)%>");
8792 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8793 return;
8796 else
8798 id2 = c_parser_peek_token (parser)->value;
8799 c_parser_consume_token (parser);
8801 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
8802 if (!iface_p)
8804 objc_start_category_implementation (id1, id2);
8805 return;
8807 if (c_parser_next_token_is (parser, CPP_LESS))
8808 proto = c_parser_objc_protocol_refs (parser);
8809 objc_start_category_interface (id1, id2, proto, attributes);
8810 c_parser_objc_methodprotolist (parser);
8811 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8812 objc_finish_interface ();
8813 return;
8815 if (c_parser_next_token_is (parser, CPP_COLON))
8817 c_parser_consume_token (parser);
8818 if (c_parser_next_token_is_not (parser, CPP_NAME))
8820 c_parser_error (parser, "expected identifier");
8821 return;
8823 superclass = c_parser_peek_token (parser)->value;
8824 c_parser_consume_token (parser);
8826 else
8827 superclass = NULL_TREE;
8828 if (iface_p)
8830 tree proto = NULL_TREE;
8831 if (c_parser_next_token_is (parser, CPP_LESS))
8832 proto = c_parser_objc_protocol_refs (parser);
8833 objc_start_class_interface (id1, superclass, proto, attributes);
8835 else
8836 objc_start_class_implementation (id1, superclass);
8837 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
8838 c_parser_objc_class_instance_variables (parser);
8839 if (iface_p)
8841 objc_continue_interface ();
8842 c_parser_objc_methodprotolist (parser);
8843 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
8844 objc_finish_interface ();
8846 else
8848 objc_continue_implementation ();
8849 return;
8853 /* Parse objc-class-instance-variables.
8855 objc-class-instance-variables:
8856 { objc-instance-variable-decl-list[opt] }
8858 objc-instance-variable-decl-list:
8859 objc-visibility-spec
8860 objc-instance-variable-decl ;
8862 objc-instance-variable-decl-list objc-visibility-spec
8863 objc-instance-variable-decl-list objc-instance-variable-decl ;
8864 objc-instance-variable-decl-list ;
8866 objc-visibility-spec:
8867 @private
8868 @protected
8869 @public
8871 objc-instance-variable-decl:
8872 struct-declaration
8875 static void
8876 c_parser_objc_class_instance_variables (c_parser *parser)
8878 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
8879 c_parser_consume_token (parser);
8880 while (c_parser_next_token_is_not (parser, CPP_EOF))
8882 tree decls;
8883 /* Parse any stray semicolon. */
8884 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
8886 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
8887 "extra semicolon");
8888 c_parser_consume_token (parser);
8889 continue;
8891 /* Stop if at the end of the instance variables. */
8892 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
8894 c_parser_consume_token (parser);
8895 break;
8897 /* Parse any objc-visibility-spec. */
8898 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
8900 c_parser_consume_token (parser);
8901 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
8902 continue;
8904 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
8906 c_parser_consume_token (parser);
8907 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
8908 continue;
8910 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
8912 c_parser_consume_token (parser);
8913 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
8914 continue;
8916 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
8918 c_parser_consume_token (parser);
8919 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
8920 continue;
8922 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
8924 c_parser_pragma (parser, pragma_external, NULL);
8925 continue;
8928 /* Parse some comma-separated declarations. */
8929 decls = c_parser_struct_declaration (parser);
8930 if (decls == NULL)
8932 /* There is a syntax error. We want to skip the offending
8933 tokens up to the next ';' (included) or '}'
8934 (excluded). */
8936 /* First, skip manually a ')' or ']'. This is because they
8937 reduce the nesting level, so c_parser_skip_until_found()
8938 wouldn't be able to skip past them. */
8939 c_token *token = c_parser_peek_token (parser);
8940 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
8941 c_parser_consume_token (parser);
8943 /* Then, do the standard skipping. */
8944 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8946 /* We hopefully recovered. Start normal parsing again. */
8947 parser->error = false;
8948 continue;
8950 else
8952 /* Comma-separated instance variables are chained together
8953 in reverse order; add them one by one. */
8954 tree ivar = nreverse (decls);
8955 for (; ivar; ivar = DECL_CHAIN (ivar))
8956 objc_add_instance_variable (copy_node (ivar));
8958 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8962 /* Parse an objc-class-declaration.
8964 objc-class-declaration:
8965 @class identifier-list ;
8968 static void
8969 c_parser_objc_class_declaration (c_parser *parser)
8971 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
8972 c_parser_consume_token (parser);
8973 /* Any identifiers, including those declared as type names, are OK
8974 here. */
8975 while (true)
8977 tree id;
8978 if (c_parser_next_token_is_not (parser, CPP_NAME))
8980 c_parser_error (parser, "expected identifier");
8981 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
8982 parser->error = false;
8983 return;
8985 id = c_parser_peek_token (parser)->value;
8986 objc_declare_class (id);
8987 c_parser_consume_token (parser);
8988 if (c_parser_next_token_is (parser, CPP_COMMA))
8989 c_parser_consume_token (parser);
8990 else
8991 break;
8993 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
8996 /* Parse an objc-alias-declaration.
8998 objc-alias-declaration:
8999 @compatibility_alias identifier identifier ;
9002 static void
9003 c_parser_objc_alias_declaration (c_parser *parser)
9005 tree id1, id2;
9006 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9007 c_parser_consume_token (parser);
9008 if (c_parser_next_token_is_not (parser, CPP_NAME))
9010 c_parser_error (parser, "expected identifier");
9011 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9012 return;
9014 id1 = c_parser_peek_token (parser)->value;
9015 c_parser_consume_token (parser);
9016 if (c_parser_next_token_is_not (parser, CPP_NAME))
9018 c_parser_error (parser, "expected identifier");
9019 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9020 return;
9022 id2 = c_parser_peek_token (parser)->value;
9023 c_parser_consume_token (parser);
9024 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9025 objc_declare_alias (id1, id2);
9028 /* Parse an objc-protocol-definition.
9030 objc-protocol-definition:
9031 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9032 @protocol identifier-list ;
9034 "@protocol identifier ;" should be resolved as "@protocol
9035 identifier-list ;": objc-methodprotolist may not start with a
9036 semicolon in the first alternative if objc-protocol-refs are
9037 omitted. */
9039 static void
9040 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9042 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9044 c_parser_consume_token (parser);
9045 if (c_parser_next_token_is_not (parser, CPP_NAME))
9047 c_parser_error (parser, "expected identifier");
9048 return;
9050 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9051 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9053 /* Any identifiers, including those declared as type names, are
9054 OK here. */
9055 while (true)
9057 tree id;
9058 if (c_parser_next_token_is_not (parser, CPP_NAME))
9060 c_parser_error (parser, "expected identifier");
9061 break;
9063 id = c_parser_peek_token (parser)->value;
9064 objc_declare_protocol (id, attributes);
9065 c_parser_consume_token (parser);
9066 if (c_parser_next_token_is (parser, CPP_COMMA))
9067 c_parser_consume_token (parser);
9068 else
9069 break;
9071 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9073 else
9075 tree id = c_parser_peek_token (parser)->value;
9076 tree proto = NULL_TREE;
9077 c_parser_consume_token (parser);
9078 if (c_parser_next_token_is (parser, CPP_LESS))
9079 proto = c_parser_objc_protocol_refs (parser);
9080 parser->objc_pq_context = true;
9081 objc_start_protocol (id, proto, attributes);
9082 c_parser_objc_methodprotolist (parser);
9083 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9084 parser->objc_pq_context = false;
9085 objc_finish_interface ();
9089 /* Parse an objc-method-type.
9091 objc-method-type:
9095 Return true if it is a class method (+) and false if it is
9096 an instance method (-).
9098 static inline bool
9099 c_parser_objc_method_type (c_parser *parser)
9101 switch (c_parser_peek_token (parser)->type)
9103 case CPP_PLUS:
9104 c_parser_consume_token (parser);
9105 return true;
9106 case CPP_MINUS:
9107 c_parser_consume_token (parser);
9108 return false;
9109 default:
9110 gcc_unreachable ();
9114 /* Parse an objc-method-definition.
9116 objc-method-definition:
9117 objc-method-type objc-method-decl ;[opt] compound-statement
9120 static void
9121 c_parser_objc_method_definition (c_parser *parser)
9123 bool is_class_method = c_parser_objc_method_type (parser);
9124 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
9125 parser->objc_pq_context = true;
9126 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9127 &expr);
9128 if (decl == error_mark_node)
9129 return; /* Bail here. */
9131 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9133 c_parser_consume_token (parser);
9134 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9135 "extra semicolon in method definition specified");
9138 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9140 c_parser_error (parser, "expected %<{%>");
9141 return;
9144 parser->objc_pq_context = false;
9145 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
9147 add_stmt (c_parser_compound_statement (parser));
9148 objc_finish_method_definition (current_function_decl);
9150 else
9152 /* This code is executed when we find a method definition
9153 outside of an @implementation context (or invalid for other
9154 reasons). Parse the method (to keep going) but do not emit
9155 any code.
9157 c_parser_compound_statement (parser);
9161 /* Parse an objc-methodprotolist.
9163 objc-methodprotolist:
9164 empty
9165 objc-methodprotolist objc-methodproto
9166 objc-methodprotolist declaration
9167 objc-methodprotolist ;
9168 @optional
9169 @required
9171 The declaration is a data definition, which may be missing
9172 declaration specifiers under the same rules and diagnostics as
9173 other data definitions outside functions, and the stray semicolon
9174 is diagnosed the same way as a stray semicolon outside a
9175 function. */
9177 static void
9178 c_parser_objc_methodprotolist (c_parser *parser)
9180 while (true)
9182 /* The list is terminated by @end. */
9183 switch (c_parser_peek_token (parser)->type)
9185 case CPP_SEMICOLON:
9186 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9187 "ISO C does not allow extra %<;%> outside of a function");
9188 c_parser_consume_token (parser);
9189 break;
9190 case CPP_PLUS:
9191 case CPP_MINUS:
9192 c_parser_objc_methodproto (parser);
9193 break;
9194 case CPP_PRAGMA:
9195 c_parser_pragma (parser, pragma_external, NULL);
9196 break;
9197 case CPP_EOF:
9198 return;
9199 default:
9200 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
9201 return;
9202 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
9203 c_parser_objc_at_property_declaration (parser);
9204 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
9206 objc_set_method_opt (true);
9207 c_parser_consume_token (parser);
9209 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
9211 objc_set_method_opt (false);
9212 c_parser_consume_token (parser);
9214 else
9215 c_parser_declaration_or_fndef (parser, false, false, true,
9216 false, true, NULL, vNULL);
9217 break;
9222 /* Parse an objc-methodproto.
9224 objc-methodproto:
9225 objc-method-type objc-method-decl ;
9228 static void
9229 c_parser_objc_methodproto (c_parser *parser)
9231 bool is_class_method = c_parser_objc_method_type (parser);
9232 tree decl, attributes = NULL_TREE;
9234 /* Remember protocol qualifiers in prototypes. */
9235 parser->objc_pq_context = true;
9236 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
9237 NULL);
9238 /* Forget protocol qualifiers now. */
9239 parser->objc_pq_context = false;
9241 /* Do not allow the presence of attributes to hide an erroneous
9242 method implementation in the interface section. */
9243 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
9245 c_parser_error (parser, "expected %<;%>");
9246 return;
9249 if (decl != error_mark_node)
9250 objc_add_method_declaration (is_class_method, decl, attributes);
9252 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9255 /* If we are at a position that method attributes may be present, check that
9256 there are not any parsed already (a syntax error) and then collect any
9257 specified at the current location. Finally, if new attributes were present,
9258 check that the next token is legal ( ';' for decls and '{' for defs). */
9260 static bool
9261 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
9263 bool bad = false;
9264 if (*attributes)
9266 c_parser_error (parser,
9267 "method attributes must be specified at the end only");
9268 *attributes = NULL_TREE;
9269 bad = true;
9272 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9273 *attributes = c_parser_attributes (parser);
9275 /* If there were no attributes here, just report any earlier error. */
9276 if (*attributes == NULL_TREE || bad)
9277 return bad;
9279 /* If the attributes are followed by a ; or {, then just report any earlier
9280 error. */
9281 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
9282 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9283 return bad;
9285 /* We've got attributes, but not at the end. */
9286 c_parser_error (parser,
9287 "expected %<;%> or %<{%> after method attribute definition");
9288 return true;
9291 /* Parse an objc-method-decl.
9293 objc-method-decl:
9294 ( objc-type-name ) objc-selector
9295 objc-selector
9296 ( objc-type-name ) objc-keyword-selector objc-optparmlist
9297 objc-keyword-selector objc-optparmlist
9298 attributes
9300 objc-keyword-selector:
9301 objc-keyword-decl
9302 objc-keyword-selector objc-keyword-decl
9304 objc-keyword-decl:
9305 objc-selector : ( objc-type-name ) identifier
9306 objc-selector : identifier
9307 : ( objc-type-name ) identifier
9308 : identifier
9310 objc-optparmlist:
9311 objc-optparms objc-optellipsis
9313 objc-optparms:
9314 empty
9315 objc-opt-parms , parameter-declaration
9317 objc-optellipsis:
9318 empty
9319 , ...
9322 static tree
9323 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
9324 tree *attributes, tree *expr)
9326 tree type = NULL_TREE;
9327 tree sel;
9328 tree parms = NULL_TREE;
9329 bool ellipsis = false;
9330 bool attr_err = false;
9332 *attributes = NULL_TREE;
9333 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9335 c_parser_consume_token (parser);
9336 type = c_parser_objc_type_name (parser);
9337 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9339 sel = c_parser_objc_selector (parser);
9340 /* If there is no selector, or a colon follows, we have an
9341 objc-keyword-selector. If there is a selector, and a colon does
9342 not follow, that selector ends the objc-method-decl. */
9343 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
9345 tree tsel = sel;
9346 tree list = NULL_TREE;
9347 while (true)
9349 tree atype = NULL_TREE, id, keyworddecl;
9350 tree param_attr = NULL_TREE;
9351 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9352 break;
9353 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9355 c_parser_consume_token (parser);
9356 atype = c_parser_objc_type_name (parser);
9357 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9358 "expected %<)%>");
9360 /* New ObjC allows attributes on method parameters. */
9361 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
9362 param_attr = c_parser_attributes (parser);
9363 if (c_parser_next_token_is_not (parser, CPP_NAME))
9365 c_parser_error (parser, "expected identifier");
9366 return error_mark_node;
9368 id = c_parser_peek_token (parser)->value;
9369 c_parser_consume_token (parser);
9370 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
9371 list = chainon (list, keyworddecl);
9372 tsel = c_parser_objc_selector (parser);
9373 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
9374 break;
9377 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9379 /* Parse the optional parameter list. Optional Objective-C
9380 method parameters follow the C syntax, and may include '...'
9381 to denote a variable number of arguments. */
9382 parms = make_node (TREE_LIST);
9383 while (c_parser_next_token_is (parser, CPP_COMMA))
9385 struct c_parm *parm;
9386 c_parser_consume_token (parser);
9387 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9389 ellipsis = true;
9390 c_parser_consume_token (parser);
9391 attr_err |= c_parser_objc_maybe_method_attributes
9392 (parser, attributes) ;
9393 break;
9395 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9396 if (parm == NULL)
9397 break;
9398 parms = chainon (parms,
9399 build_tree_list (NULL_TREE, grokparm (parm, expr)));
9401 sel = list;
9403 else
9404 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
9406 if (sel == NULL)
9408 c_parser_error (parser, "objective-c method declaration is expected");
9409 return error_mark_node;
9412 if (attr_err)
9413 return error_mark_node;
9415 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
9418 /* Parse an objc-type-name.
9420 objc-type-name:
9421 objc-type-qualifiers[opt] type-name
9422 objc-type-qualifiers[opt]
9424 objc-type-qualifiers:
9425 objc-type-qualifier
9426 objc-type-qualifiers objc-type-qualifier
9428 objc-type-qualifier: one of
9429 in out inout bycopy byref oneway
9432 static tree
9433 c_parser_objc_type_name (c_parser *parser)
9435 tree quals = NULL_TREE;
9436 struct c_type_name *type_name = NULL;
9437 tree type = NULL_TREE;
9438 while (true)
9440 c_token *token = c_parser_peek_token (parser);
9441 if (token->type == CPP_KEYWORD
9442 && (token->keyword == RID_IN
9443 || token->keyword == RID_OUT
9444 || token->keyword == RID_INOUT
9445 || token->keyword == RID_BYCOPY
9446 || token->keyword == RID_BYREF
9447 || token->keyword == RID_ONEWAY))
9449 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
9450 c_parser_consume_token (parser);
9452 else
9453 break;
9455 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
9456 type_name = c_parser_type_name (parser);
9457 if (type_name)
9458 type = groktypename (type_name, NULL, NULL);
9460 /* If the type is unknown, and error has already been produced and
9461 we need to recover from the error. In that case, use NULL_TREE
9462 for the type, as if no type had been specified; this will use the
9463 default type ('id') which is good for error recovery. */
9464 if (type == error_mark_node)
9465 type = NULL_TREE;
9467 return build_tree_list (quals, type);
9470 /* Parse objc-protocol-refs.
9472 objc-protocol-refs:
9473 < identifier-list >
9476 static tree
9477 c_parser_objc_protocol_refs (c_parser *parser)
9479 tree list = NULL_TREE;
9480 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
9481 c_parser_consume_token (parser);
9482 /* Any identifiers, including those declared as type names, are OK
9483 here. */
9484 while (true)
9486 tree id;
9487 if (c_parser_next_token_is_not (parser, CPP_NAME))
9489 c_parser_error (parser, "expected identifier");
9490 break;
9492 id = c_parser_peek_token (parser)->value;
9493 list = chainon (list, build_tree_list (NULL_TREE, id));
9494 c_parser_consume_token (parser);
9495 if (c_parser_next_token_is (parser, CPP_COMMA))
9496 c_parser_consume_token (parser);
9497 else
9498 break;
9500 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
9501 return list;
9504 /* Parse an objc-try-catch-finally-statement.
9506 objc-try-catch-finally-statement:
9507 @try compound-statement objc-catch-list[opt]
9508 @try compound-statement objc-catch-list[opt] @finally compound-statement
9510 objc-catch-list:
9511 @catch ( objc-catch-parameter-declaration ) compound-statement
9512 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
9514 objc-catch-parameter-declaration:
9515 parameter-declaration
9516 '...'
9518 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
9520 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
9521 for C++. Keep them in sync. */
9523 static void
9524 c_parser_objc_try_catch_finally_statement (c_parser *parser)
9526 location_t location;
9527 tree stmt;
9529 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
9530 c_parser_consume_token (parser);
9531 location = c_parser_peek_token (parser)->location;
9532 objc_maybe_warn_exceptions (location);
9533 stmt = c_parser_compound_statement (parser);
9534 objc_begin_try_stmt (location, stmt);
9536 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
9538 struct c_parm *parm;
9539 tree parameter_declaration = error_mark_node;
9540 bool seen_open_paren = false;
9542 c_parser_consume_token (parser);
9543 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9544 seen_open_paren = true;
9545 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
9547 /* We have "@catch (...)" (where the '...' are literally
9548 what is in the code). Skip the '...'.
9549 parameter_declaration is set to NULL_TREE, and
9550 objc_being_catch_clauses() knows that that means
9551 '...'. */
9552 c_parser_consume_token (parser);
9553 parameter_declaration = NULL_TREE;
9555 else
9557 /* We have "@catch (NSException *exception)" or something
9558 like that. Parse the parameter declaration. */
9559 parm = c_parser_parameter_declaration (parser, NULL_TREE);
9560 if (parm == NULL)
9561 parameter_declaration = error_mark_node;
9562 else
9563 parameter_declaration = grokparm (parm, NULL);
9565 if (seen_open_paren)
9566 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9567 else
9569 /* If there was no open parenthesis, we are recovering from
9570 an error, and we are trying to figure out what mistake
9571 the user has made. */
9573 /* If there is an immediate closing parenthesis, the user
9574 probably forgot the opening one (ie, they typed "@catch
9575 NSException *e)". Parse the closing parenthesis and keep
9576 going. */
9577 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9578 c_parser_consume_token (parser);
9580 /* If these is no immediate closing parenthesis, the user
9581 probably doesn't know that parenthesis are required at
9582 all (ie, they typed "@catch NSException *e"). So, just
9583 forget about the closing parenthesis and keep going. */
9585 objc_begin_catch_clause (parameter_declaration);
9586 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
9587 c_parser_compound_statement_nostart (parser);
9588 objc_finish_catch_clause ();
9590 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
9592 c_parser_consume_token (parser);
9593 location = c_parser_peek_token (parser)->location;
9594 stmt = c_parser_compound_statement (parser);
9595 objc_build_finally_clause (location, stmt);
9597 objc_finish_try_stmt ();
9600 /* Parse an objc-synchronized-statement.
9602 objc-synchronized-statement:
9603 @synchronized ( expression ) compound-statement
9606 static void
9607 c_parser_objc_synchronized_statement (c_parser *parser)
9609 location_t loc;
9610 tree expr, stmt;
9611 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
9612 c_parser_consume_token (parser);
9613 loc = c_parser_peek_token (parser)->location;
9614 objc_maybe_warn_exceptions (loc);
9615 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
9617 struct c_expr ce = c_parser_expression (parser);
9618 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9619 expr = ce.value;
9620 expr = c_fully_fold (expr, false, NULL);
9621 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
9623 else
9624 expr = error_mark_node;
9625 stmt = c_parser_compound_statement (parser);
9626 objc_build_synchronized (loc, expr, stmt);
9629 /* Parse an objc-selector; return NULL_TREE without an error if the
9630 next token is not an objc-selector.
9632 objc-selector:
9633 identifier
9634 one of
9635 enum struct union if else while do for switch case default
9636 break continue return goto asm sizeof typeof __alignof
9637 unsigned long const short volatile signed restrict _Complex
9638 in out inout bycopy byref oneway int char float double void _Bool
9639 _Atomic
9641 ??? Why this selection of keywords but not, for example, storage
9642 class specifiers? */
9644 static tree
9645 c_parser_objc_selector (c_parser *parser)
9647 c_token *token = c_parser_peek_token (parser);
9648 tree value = token->value;
9649 if (token->type == CPP_NAME)
9651 c_parser_consume_token (parser);
9652 return value;
9654 if (token->type != CPP_KEYWORD)
9655 return NULL_TREE;
9656 switch (token->keyword)
9658 case RID_ENUM:
9659 case RID_STRUCT:
9660 case RID_UNION:
9661 case RID_IF:
9662 case RID_ELSE:
9663 case RID_WHILE:
9664 case RID_DO:
9665 case RID_FOR:
9666 case RID_SWITCH:
9667 case RID_CASE:
9668 case RID_DEFAULT:
9669 case RID_BREAK:
9670 case RID_CONTINUE:
9671 case RID_RETURN:
9672 case RID_GOTO:
9673 case RID_ASM:
9674 case RID_SIZEOF:
9675 case RID_TYPEOF:
9676 case RID_ALIGNOF:
9677 case RID_UNSIGNED:
9678 case RID_LONG:
9679 case RID_CONST:
9680 case RID_SHORT:
9681 case RID_VOLATILE:
9682 case RID_SIGNED:
9683 case RID_RESTRICT:
9684 case RID_COMPLEX:
9685 case RID_IN:
9686 case RID_OUT:
9687 case RID_INOUT:
9688 case RID_BYCOPY:
9689 case RID_BYREF:
9690 case RID_ONEWAY:
9691 case RID_INT:
9692 case RID_CHAR:
9693 case RID_FLOAT:
9694 case RID_DOUBLE:
9695 CASE_RID_FLOATN_NX:
9696 case RID_VOID:
9697 case RID_BOOL:
9698 case RID_ATOMIC:
9699 case RID_AUTO_TYPE:
9700 case RID_INT_N_0:
9701 case RID_INT_N_1:
9702 case RID_INT_N_2:
9703 case RID_INT_N_3:
9704 c_parser_consume_token (parser);
9705 return value;
9706 default:
9707 return NULL_TREE;
9711 /* Parse an objc-selector-arg.
9713 objc-selector-arg:
9714 objc-selector
9715 objc-keywordname-list
9717 objc-keywordname-list:
9718 objc-keywordname
9719 objc-keywordname-list objc-keywordname
9721 objc-keywordname:
9722 objc-selector :
9726 static tree
9727 c_parser_objc_selector_arg (c_parser *parser)
9729 tree sel = c_parser_objc_selector (parser);
9730 tree list = NULL_TREE;
9731 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9732 return sel;
9733 while (true)
9735 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9736 return list;
9737 list = chainon (list, build_tree_list (sel, NULL_TREE));
9738 sel = c_parser_objc_selector (parser);
9739 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9740 break;
9742 return list;
9745 /* Parse an objc-receiver.
9747 objc-receiver:
9748 expression
9749 class-name
9750 type-name
9753 static tree
9754 c_parser_objc_receiver (c_parser *parser)
9756 location_t loc = c_parser_peek_token (parser)->location;
9758 if (c_parser_peek_token (parser)->type == CPP_NAME
9759 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
9760 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
9762 tree id = c_parser_peek_token (parser)->value;
9763 c_parser_consume_token (parser);
9764 return objc_get_class_reference (id);
9766 struct c_expr ce = c_parser_expression (parser);
9767 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
9768 return c_fully_fold (ce.value, false, NULL);
9771 /* Parse objc-message-args.
9773 objc-message-args:
9774 objc-selector
9775 objc-keywordarg-list
9777 objc-keywordarg-list:
9778 objc-keywordarg
9779 objc-keywordarg-list objc-keywordarg
9781 objc-keywordarg:
9782 objc-selector : objc-keywordexpr
9783 : objc-keywordexpr
9786 static tree
9787 c_parser_objc_message_args (c_parser *parser)
9789 tree sel = c_parser_objc_selector (parser);
9790 tree list = NULL_TREE;
9791 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
9792 return sel;
9793 while (true)
9795 tree keywordexpr;
9796 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
9797 return error_mark_node;
9798 keywordexpr = c_parser_objc_keywordexpr (parser);
9799 list = chainon (list, build_tree_list (sel, keywordexpr));
9800 sel = c_parser_objc_selector (parser);
9801 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
9802 break;
9804 return list;
9807 /* Parse an objc-keywordexpr.
9809 objc-keywordexpr:
9810 nonempty-expr-list
9813 static tree
9814 c_parser_objc_keywordexpr (c_parser *parser)
9816 tree ret;
9817 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
9818 NULL, NULL, NULL, NULL);
9819 if (vec_safe_length (expr_list) == 1)
9821 /* Just return the expression, remove a level of
9822 indirection. */
9823 ret = (*expr_list)[0];
9825 else
9827 /* We have a comma expression, we will collapse later. */
9828 ret = build_tree_list_vec (expr_list);
9830 release_tree_vector (expr_list);
9831 return ret;
9834 /* A check, needed in several places, that ObjC interface, implementation or
9835 method definitions are not prefixed by incorrect items. */
9836 static bool
9837 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
9838 struct c_declspecs *specs)
9840 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
9841 || specs->typespec_kind != ctsk_none)
9843 c_parser_error (parser,
9844 "no type or storage class may be specified here,");
9845 c_parser_skip_to_end_of_block_or_statement (parser);
9846 return true;
9848 return false;
9851 /* Parse an Objective-C @property declaration. The syntax is:
9853 objc-property-declaration:
9854 '@property' objc-property-attributes[opt] struct-declaration ;
9856 objc-property-attributes:
9857 '(' objc-property-attribute-list ')'
9859 objc-property-attribute-list:
9860 objc-property-attribute
9861 objc-property-attribute-list, objc-property-attribute
9863 objc-property-attribute
9864 'getter' = identifier
9865 'setter' = identifier
9866 'readonly'
9867 'readwrite'
9868 'assign'
9869 'retain'
9870 'copy'
9871 'nonatomic'
9873 For example:
9874 @property NSString *name;
9875 @property (readonly) id object;
9876 @property (retain, nonatomic, getter=getTheName) id name;
9877 @property int a, b, c;
9879 PS: This function is identical to cp_parser_objc_at_propery_declaration
9880 for C++. Keep them in sync. */
9881 static void
9882 c_parser_objc_at_property_declaration (c_parser *parser)
9884 /* The following variables hold the attributes of the properties as
9885 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
9886 seen. When we see an attribute, we set them to 'true' (if they
9887 are boolean properties) or to the identifier (if they have an
9888 argument, ie, for getter and setter). Note that here we only
9889 parse the list of attributes, check the syntax and accumulate the
9890 attributes that we find. objc_add_property_declaration() will
9891 then process the information. */
9892 bool property_assign = false;
9893 bool property_copy = false;
9894 tree property_getter_ident = NULL_TREE;
9895 bool property_nonatomic = false;
9896 bool property_readonly = false;
9897 bool property_readwrite = false;
9898 bool property_retain = false;
9899 tree property_setter_ident = NULL_TREE;
9901 /* 'properties' is the list of properties that we read. Usually a
9902 single one, but maybe more (eg, in "@property int a, b, c;" there
9903 are three). */
9904 tree properties;
9905 location_t loc;
9907 loc = c_parser_peek_token (parser)->location;
9908 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
9910 c_parser_consume_token (parser); /* Eat '@property'. */
9912 /* Parse the optional attribute list... */
9913 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9915 /* Eat the '(' */
9916 c_parser_consume_token (parser);
9918 /* Property attribute keywords are valid now. */
9919 parser->objc_property_attr_context = true;
9921 while (true)
9923 bool syntax_error = false;
9924 c_token *token = c_parser_peek_token (parser);
9925 enum rid keyword;
9927 if (token->type != CPP_KEYWORD)
9929 if (token->type == CPP_CLOSE_PAREN)
9930 c_parser_error (parser, "expected identifier");
9931 else
9933 c_parser_consume_token (parser);
9934 c_parser_error (parser, "unknown property attribute");
9936 break;
9938 keyword = token->keyword;
9939 c_parser_consume_token (parser);
9940 switch (keyword)
9942 case RID_ASSIGN: property_assign = true; break;
9943 case RID_COPY: property_copy = true; break;
9944 case RID_NONATOMIC: property_nonatomic = true; break;
9945 case RID_READONLY: property_readonly = true; break;
9946 case RID_READWRITE: property_readwrite = true; break;
9947 case RID_RETAIN: property_retain = true; break;
9949 case RID_GETTER:
9950 case RID_SETTER:
9951 if (c_parser_next_token_is_not (parser, CPP_EQ))
9953 if (keyword == RID_GETTER)
9954 c_parser_error (parser,
9955 "missing %<=%> (after %<getter%> attribute)");
9956 else
9957 c_parser_error (parser,
9958 "missing %<=%> (after %<setter%> attribute)");
9959 syntax_error = true;
9960 break;
9962 c_parser_consume_token (parser); /* eat the = */
9963 if (c_parser_next_token_is_not (parser, CPP_NAME))
9965 c_parser_error (parser, "expected identifier");
9966 syntax_error = true;
9967 break;
9969 if (keyword == RID_SETTER)
9971 if (property_setter_ident != NULL_TREE)
9972 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
9973 else
9974 property_setter_ident = c_parser_peek_token (parser)->value;
9975 c_parser_consume_token (parser);
9976 if (c_parser_next_token_is_not (parser, CPP_COLON))
9977 c_parser_error (parser, "setter name must terminate with %<:%>");
9978 else
9979 c_parser_consume_token (parser);
9981 else
9983 if (property_getter_ident != NULL_TREE)
9984 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
9985 else
9986 property_getter_ident = c_parser_peek_token (parser)->value;
9987 c_parser_consume_token (parser);
9989 break;
9990 default:
9991 c_parser_error (parser, "unknown property attribute");
9992 syntax_error = true;
9993 break;
9996 if (syntax_error)
9997 break;
9999 if (c_parser_next_token_is (parser, CPP_COMMA))
10000 c_parser_consume_token (parser);
10001 else
10002 break;
10004 parser->objc_property_attr_context = false;
10005 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10007 /* ... and the property declaration(s). */
10008 properties = c_parser_struct_declaration (parser);
10010 if (properties == error_mark_node)
10012 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10013 parser->error = false;
10014 return;
10017 if (properties == NULL_TREE)
10018 c_parser_error (parser, "expected identifier");
10019 else
10021 /* Comma-separated properties are chained together in
10022 reverse order; add them one by one. */
10023 properties = nreverse (properties);
10025 for (; properties; properties = TREE_CHAIN (properties))
10026 objc_add_property_declaration (loc, copy_node (properties),
10027 property_readonly, property_readwrite,
10028 property_assign, property_retain,
10029 property_copy, property_nonatomic,
10030 property_getter_ident, property_setter_ident);
10033 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10034 parser->error = false;
10037 /* Parse an Objective-C @synthesize declaration. The syntax is:
10039 objc-synthesize-declaration:
10040 @synthesize objc-synthesize-identifier-list ;
10042 objc-synthesize-identifier-list:
10043 objc-synthesize-identifier
10044 objc-synthesize-identifier-list, objc-synthesize-identifier
10046 objc-synthesize-identifier
10047 identifier
10048 identifier = identifier
10050 For example:
10051 @synthesize MyProperty;
10052 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10054 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10055 for C++. Keep them in sync.
10057 static void
10058 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10060 tree list = NULL_TREE;
10061 location_t loc;
10062 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10063 loc = c_parser_peek_token (parser)->location;
10065 c_parser_consume_token (parser);
10066 while (true)
10068 tree property, ivar;
10069 if (c_parser_next_token_is_not (parser, CPP_NAME))
10071 c_parser_error (parser, "expected identifier");
10072 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10073 /* Once we find the semicolon, we can resume normal parsing.
10074 We have to reset parser->error manually because
10075 c_parser_skip_until_found() won't reset it for us if the
10076 next token is precisely a semicolon. */
10077 parser->error = false;
10078 return;
10080 property = c_parser_peek_token (parser)->value;
10081 c_parser_consume_token (parser);
10082 if (c_parser_next_token_is (parser, CPP_EQ))
10084 c_parser_consume_token (parser);
10085 if (c_parser_next_token_is_not (parser, CPP_NAME))
10087 c_parser_error (parser, "expected identifier");
10088 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10089 parser->error = false;
10090 return;
10092 ivar = c_parser_peek_token (parser)->value;
10093 c_parser_consume_token (parser);
10095 else
10096 ivar = NULL_TREE;
10097 list = chainon (list, build_tree_list (ivar, property));
10098 if (c_parser_next_token_is (parser, CPP_COMMA))
10099 c_parser_consume_token (parser);
10100 else
10101 break;
10103 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10104 objc_add_synthesize_declaration (loc, list);
10107 /* Parse an Objective-C @dynamic declaration. The syntax is:
10109 objc-dynamic-declaration:
10110 @dynamic identifier-list ;
10112 For example:
10113 @dynamic MyProperty;
10114 @dynamic MyProperty, AnotherProperty;
10116 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
10117 for C++. Keep them in sync.
10119 static void
10120 c_parser_objc_at_dynamic_declaration (c_parser *parser)
10122 tree list = NULL_TREE;
10123 location_t loc;
10124 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
10125 loc = c_parser_peek_token (parser)->location;
10127 c_parser_consume_token (parser);
10128 while (true)
10130 tree property;
10131 if (c_parser_next_token_is_not (parser, CPP_NAME))
10133 c_parser_error (parser, "expected identifier");
10134 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10135 parser->error = false;
10136 return;
10138 property = c_parser_peek_token (parser)->value;
10139 list = chainon (list, build_tree_list (NULL_TREE, property));
10140 c_parser_consume_token (parser);
10141 if (c_parser_next_token_is (parser, CPP_COMMA))
10142 c_parser_consume_token (parser);
10143 else
10144 break;
10146 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10147 objc_add_dynamic_declaration (loc, list);
10151 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
10152 should be considered, statements. ALLOW_STMT is true if we're within
10153 the context of a function and such pragmas are to be allowed. Returns
10154 true if we actually parsed such a pragma. */
10156 static bool
10157 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
10159 unsigned int id;
10161 id = c_parser_peek_token (parser)->pragma_kind;
10162 gcc_assert (id != PRAGMA_NONE);
10164 switch (id)
10166 case PRAGMA_OACC_DECLARE:
10167 c_parser_oacc_declare (parser);
10168 return false;
10170 case PRAGMA_OACC_ENTER_DATA:
10171 if (context != pragma_compound)
10173 if (context == pragma_stmt)
10174 c_parser_error (parser, "%<#pragma acc enter data%> may only be "
10175 "used in compound statements");
10176 goto bad_stmt;
10178 c_parser_oacc_enter_exit_data (parser, true);
10179 return false;
10181 case PRAGMA_OACC_EXIT_DATA:
10182 if (context != pragma_compound)
10184 if (context == pragma_stmt)
10185 c_parser_error (parser, "%<#pragma acc exit data%> may only be "
10186 "used in compound statements");
10187 goto bad_stmt;
10189 c_parser_oacc_enter_exit_data (parser, false);
10190 return false;
10192 case PRAGMA_OACC_ROUTINE:
10193 if (context != pragma_external)
10195 error_at (c_parser_peek_token (parser)->location,
10196 "%<#pragma acc routine%> must be at file scope");
10197 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10198 return false;
10200 c_parser_oacc_routine (parser, context);
10201 return false;
10203 case PRAGMA_OACC_UPDATE:
10204 if (context != pragma_compound)
10206 if (context == pragma_stmt)
10207 c_parser_error (parser, "%<#pragma acc update%> may only be "
10208 "used in compound statements");
10209 goto bad_stmt;
10211 c_parser_oacc_update (parser);
10212 return false;
10214 case PRAGMA_OMP_BARRIER:
10215 if (context != pragma_compound)
10217 if (context == pragma_stmt)
10218 c_parser_error (parser, "%<#pragma omp barrier%> may only be "
10219 "used in compound statements");
10220 goto bad_stmt;
10222 c_parser_omp_barrier (parser);
10223 return false;
10225 case PRAGMA_OMP_FLUSH:
10226 if (context != pragma_compound)
10228 if (context == pragma_stmt)
10229 c_parser_error (parser, "%<#pragma omp flush%> may only be "
10230 "used in compound statements");
10231 goto bad_stmt;
10233 c_parser_omp_flush (parser);
10234 return false;
10236 case PRAGMA_OMP_TASKWAIT:
10237 if (context != pragma_compound)
10239 if (context == pragma_stmt)
10240 c_parser_error (parser, "%<#pragma omp taskwait%> may only be "
10241 "used in compound statements");
10242 goto bad_stmt;
10244 c_parser_omp_taskwait (parser);
10245 return false;
10247 case PRAGMA_OMP_TASKYIELD:
10248 if (context != pragma_compound)
10250 if (context == pragma_stmt)
10251 c_parser_error (parser, "%<#pragma omp taskyield%> may only be "
10252 "used in compound statements");
10253 goto bad_stmt;
10255 c_parser_omp_taskyield (parser);
10256 return false;
10258 case PRAGMA_OMP_CANCEL:
10259 if (context != pragma_compound)
10261 if (context == pragma_stmt)
10262 c_parser_error (parser, "%<#pragma omp cancel%> may only be "
10263 "used in compound statements");
10264 goto bad_stmt;
10266 c_parser_omp_cancel (parser);
10267 return false;
10269 case PRAGMA_OMP_CANCELLATION_POINT:
10270 c_parser_omp_cancellation_point (parser, context);
10271 return false;
10273 case PRAGMA_OMP_THREADPRIVATE:
10274 c_parser_omp_threadprivate (parser);
10275 return false;
10277 case PRAGMA_OMP_TARGET:
10278 return c_parser_omp_target (parser, context, if_p);
10280 case PRAGMA_OMP_END_DECLARE_TARGET:
10281 c_parser_omp_end_declare_target (parser);
10282 return false;
10284 case PRAGMA_OMP_SECTION:
10285 error_at (c_parser_peek_token (parser)->location,
10286 "%<#pragma omp section%> may only be used in "
10287 "%<#pragma omp sections%> construct");
10288 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10289 return false;
10291 case PRAGMA_OMP_DECLARE:
10292 c_parser_omp_declare (parser, context);
10293 return false;
10295 case PRAGMA_OMP_ORDERED:
10296 return c_parser_omp_ordered (parser, context, if_p);
10298 case PRAGMA_IVDEP:
10299 c_parser_consume_pragma (parser);
10300 c_parser_skip_to_pragma_eol (parser);
10301 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
10302 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
10303 && !c_parser_next_token_is_keyword (parser, RID_DO))
10305 c_parser_error (parser, "for, while or do statement expected");
10306 return false;
10308 if (c_parser_next_token_is_keyword (parser, RID_FOR))
10309 c_parser_for_statement (parser, true, if_p);
10310 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
10311 c_parser_while_statement (parser, true, if_p);
10312 else
10313 c_parser_do_statement (parser, true);
10314 return false;
10316 case PRAGMA_GCC_PCH_PREPROCESS:
10317 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
10318 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10319 return false;
10321 case PRAGMA_CILK_SIMD:
10322 if (!c_parser_cilk_verify_simd (parser, context))
10323 return false;
10324 c_parser_consume_pragma (parser);
10325 c_parser_cilk_simd (parser, if_p);
10326 return false;
10327 case PRAGMA_CILK_GRAINSIZE:
10328 if (!flag_cilkplus)
10330 warning (0, "%<#pragma grainsize%> ignored because -fcilkplus is not"
10331 " enabled");
10332 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10333 return false;
10335 if (context == pragma_external)
10337 error_at (c_parser_peek_token (parser)->location,
10338 "%<#pragma grainsize%> must be inside a function");
10339 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10340 return false;
10342 c_parser_cilk_grainsize (parser, if_p);
10343 return false;
10345 case PRAGMA_OACC_WAIT:
10346 if (context != pragma_compound)
10348 if (context == pragma_stmt)
10349 c_parser_error (parser, "%<#pragma acc enter data%> may only be "
10350 "used in compound statements");
10351 goto bad_stmt;
10353 /* FALL THROUGH. */
10355 default:
10356 if (id < PRAGMA_FIRST_EXTERNAL)
10358 if (context != pragma_stmt && context != pragma_compound)
10360 bad_stmt:
10361 c_parser_error (parser, "expected declaration specifiers");
10362 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
10363 return false;
10365 c_parser_omp_construct (parser, if_p);
10366 return true;
10368 break;
10371 c_parser_consume_pragma (parser);
10372 c_invoke_pragma_handler (id);
10374 /* Skip to EOL, but suppress any error message. Those will have been
10375 generated by the handler routine through calling error, as opposed
10376 to calling c_parser_error. */
10377 parser->error = true;
10378 c_parser_skip_to_pragma_eol (parser);
10380 return false;
10383 /* The interface the pragma parsers have to the lexer. */
10385 enum cpp_ttype
10386 pragma_lex (tree *value, location_t *loc)
10388 c_token *tok = c_parser_peek_token (the_parser);
10389 enum cpp_ttype ret = tok->type;
10391 *value = tok->value;
10392 if (loc)
10393 *loc = tok->location;
10395 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
10396 ret = CPP_EOF;
10397 else
10399 if (ret == CPP_KEYWORD)
10400 ret = CPP_NAME;
10401 c_parser_consume_token (the_parser);
10404 return ret;
10407 static void
10408 c_parser_pragma_pch_preprocess (c_parser *parser)
10410 tree name = NULL;
10412 c_parser_consume_pragma (parser);
10413 if (c_parser_next_token_is (parser, CPP_STRING))
10415 name = c_parser_peek_token (parser)->value;
10416 c_parser_consume_token (parser);
10418 else
10419 c_parser_error (parser, "expected string literal");
10420 c_parser_skip_to_pragma_eol (parser);
10422 if (name)
10423 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
10426 /* OpenACC and OpenMP parsing routines. */
10428 /* Returns name of the next clause.
10429 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
10430 the token is not consumed. Otherwise appropriate pragma_omp_clause is
10431 returned and the token is consumed. */
10433 static pragma_omp_clause
10434 c_parser_omp_clause_name (c_parser *parser)
10436 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
10438 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
10439 result = PRAGMA_OACC_CLAUSE_AUTO;
10440 else if (c_parser_next_token_is_keyword (parser, RID_IF))
10441 result = PRAGMA_OMP_CLAUSE_IF;
10442 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
10443 result = PRAGMA_OMP_CLAUSE_DEFAULT;
10444 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
10445 result = PRAGMA_OMP_CLAUSE_FOR;
10446 else if (c_parser_next_token_is (parser, CPP_NAME))
10448 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
10450 switch (p[0])
10452 case 'a':
10453 if (!strcmp ("aligned", p))
10454 result = PRAGMA_OMP_CLAUSE_ALIGNED;
10455 else if (!strcmp ("async", p))
10456 result = PRAGMA_OACC_CLAUSE_ASYNC;
10457 break;
10458 case 'c':
10459 if (!strcmp ("collapse", p))
10460 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
10461 else if (!strcmp ("copy", p))
10462 result = PRAGMA_OACC_CLAUSE_COPY;
10463 else if (!strcmp ("copyin", p))
10464 result = PRAGMA_OMP_CLAUSE_COPYIN;
10465 else if (!strcmp ("copyout", p))
10466 result = PRAGMA_OACC_CLAUSE_COPYOUT;
10467 else if (!strcmp ("copyprivate", p))
10468 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
10469 else if (!strcmp ("create", p))
10470 result = PRAGMA_OACC_CLAUSE_CREATE;
10471 break;
10472 case 'd':
10473 if (!strcmp ("defaultmap", p))
10474 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
10475 else if (!strcmp ("delete", p))
10476 result = PRAGMA_OACC_CLAUSE_DELETE;
10477 else if (!strcmp ("depend", p))
10478 result = PRAGMA_OMP_CLAUSE_DEPEND;
10479 else if (!strcmp ("device", p))
10480 result = PRAGMA_OMP_CLAUSE_DEVICE;
10481 else if (!strcmp ("deviceptr", p))
10482 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
10483 else if (!strcmp ("device_resident", p))
10484 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
10485 else if (!strcmp ("dist_schedule", p))
10486 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
10487 break;
10488 case 'f':
10489 if (!strcmp ("final", p))
10490 result = PRAGMA_OMP_CLAUSE_FINAL;
10491 else if (!strcmp ("firstprivate", p))
10492 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
10493 else if (!strcmp ("from", p))
10494 result = PRAGMA_OMP_CLAUSE_FROM;
10495 break;
10496 case 'g':
10497 if (!strcmp ("gang", p))
10498 result = PRAGMA_OACC_CLAUSE_GANG;
10499 else if (!strcmp ("grainsize", p))
10500 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
10501 break;
10502 case 'h':
10503 if (!strcmp ("hint", p))
10504 result = PRAGMA_OMP_CLAUSE_HINT;
10505 else if (!strcmp ("host", p))
10506 result = PRAGMA_OACC_CLAUSE_HOST;
10507 break;
10508 case 'i':
10509 if (!strcmp ("inbranch", p))
10510 result = PRAGMA_OMP_CLAUSE_INBRANCH;
10511 else if (!strcmp ("independent", p))
10512 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
10513 else if (!strcmp ("is_device_ptr", p))
10514 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
10515 break;
10516 case 'l':
10517 if (!strcmp ("lastprivate", p))
10518 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
10519 else if (!strcmp ("linear", p))
10520 result = PRAGMA_OMP_CLAUSE_LINEAR;
10521 else if (!strcmp ("link", p))
10522 result = PRAGMA_OMP_CLAUSE_LINK;
10523 break;
10524 case 'm':
10525 if (!strcmp ("map", p))
10526 result = PRAGMA_OMP_CLAUSE_MAP;
10527 else if (!strcmp ("mergeable", p))
10528 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
10529 else if (flag_cilkplus && !strcmp ("mask", p))
10530 result = PRAGMA_CILK_CLAUSE_MASK;
10531 break;
10532 case 'n':
10533 if (!strcmp ("nogroup", p))
10534 result = PRAGMA_OMP_CLAUSE_NOGROUP;
10535 else if (!strcmp ("notinbranch", p))
10536 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
10537 else if (!strcmp ("nowait", p))
10538 result = PRAGMA_OMP_CLAUSE_NOWAIT;
10539 else if (!strcmp ("num_gangs", p))
10540 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
10541 else if (!strcmp ("num_tasks", p))
10542 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
10543 else if (!strcmp ("num_teams", p))
10544 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
10545 else if (!strcmp ("num_threads", p))
10546 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
10547 else if (!strcmp ("num_workers", p))
10548 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
10549 else if (flag_cilkplus && !strcmp ("nomask", p))
10550 result = PRAGMA_CILK_CLAUSE_NOMASK;
10551 break;
10552 case 'o':
10553 if (!strcmp ("ordered", p))
10554 result = PRAGMA_OMP_CLAUSE_ORDERED;
10555 break;
10556 case 'p':
10557 if (!strcmp ("parallel", p))
10558 result = PRAGMA_OMP_CLAUSE_PARALLEL;
10559 else if (!strcmp ("present", p))
10560 result = PRAGMA_OACC_CLAUSE_PRESENT;
10561 else if (!strcmp ("present_or_copy", p)
10562 || !strcmp ("pcopy", p))
10563 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
10564 else if (!strcmp ("present_or_copyin", p)
10565 || !strcmp ("pcopyin", p))
10566 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
10567 else if (!strcmp ("present_or_copyout", p)
10568 || !strcmp ("pcopyout", p))
10569 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
10570 else if (!strcmp ("present_or_create", p)
10571 || !strcmp ("pcreate", p))
10572 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
10573 else if (!strcmp ("priority", p))
10574 result = PRAGMA_OMP_CLAUSE_PRIORITY;
10575 else if (!strcmp ("private", p))
10576 result = PRAGMA_OMP_CLAUSE_PRIVATE;
10577 else if (!strcmp ("proc_bind", p))
10578 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
10579 break;
10580 case 'r':
10581 if (!strcmp ("reduction", p))
10582 result = PRAGMA_OMP_CLAUSE_REDUCTION;
10583 break;
10584 case 's':
10585 if (!strcmp ("safelen", p))
10586 result = PRAGMA_OMP_CLAUSE_SAFELEN;
10587 else if (!strcmp ("schedule", p))
10588 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
10589 else if (!strcmp ("sections", p))
10590 result = PRAGMA_OMP_CLAUSE_SECTIONS;
10591 else if (!strcmp ("seq", p))
10592 result = PRAGMA_OACC_CLAUSE_SEQ;
10593 else if (!strcmp ("shared", p))
10594 result = PRAGMA_OMP_CLAUSE_SHARED;
10595 else if (!strcmp ("simd", p))
10596 result = PRAGMA_OMP_CLAUSE_SIMD;
10597 else if (!strcmp ("simdlen", p))
10598 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
10599 else if (!strcmp ("self", p))
10600 result = PRAGMA_OACC_CLAUSE_SELF;
10601 break;
10602 case 't':
10603 if (!strcmp ("taskgroup", p))
10604 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
10605 else if (!strcmp ("thread_limit", p))
10606 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
10607 else if (!strcmp ("threads", p))
10608 result = PRAGMA_OMP_CLAUSE_THREADS;
10609 else if (!strcmp ("tile", p))
10610 result = PRAGMA_OACC_CLAUSE_TILE;
10611 else if (!strcmp ("to", p))
10612 result = PRAGMA_OMP_CLAUSE_TO;
10613 break;
10614 case 'u':
10615 if (!strcmp ("uniform", p))
10616 result = PRAGMA_OMP_CLAUSE_UNIFORM;
10617 else if (!strcmp ("untied", p))
10618 result = PRAGMA_OMP_CLAUSE_UNTIED;
10619 else if (!strcmp ("use_device", p))
10620 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
10621 else if (!strcmp ("use_device_ptr", p))
10622 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
10623 break;
10624 case 'v':
10625 if (!strcmp ("vector", p))
10626 result = PRAGMA_OACC_CLAUSE_VECTOR;
10627 else if (!strcmp ("vector_length", p))
10628 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
10629 else if (flag_cilkplus && !strcmp ("vectorlength", p))
10630 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
10631 break;
10632 case 'w':
10633 if (!strcmp ("wait", p))
10634 result = PRAGMA_OACC_CLAUSE_WAIT;
10635 else if (!strcmp ("worker", p))
10636 result = PRAGMA_OACC_CLAUSE_WORKER;
10637 break;
10641 if (result != PRAGMA_OMP_CLAUSE_NONE)
10642 c_parser_consume_token (parser);
10644 return result;
10647 /* Validate that a clause of the given type does not already exist. */
10649 static void
10650 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
10651 const char *name)
10653 tree c;
10655 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10656 if (OMP_CLAUSE_CODE (c) == code)
10658 location_t loc = OMP_CLAUSE_LOCATION (c);
10659 error_at (loc, "too many %qs clauses", name);
10660 break;
10664 /* OpenACC 2.0
10665 Parse wait clause or wait directive parameters. */
10667 static tree
10668 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
10670 vec<tree, va_gc> *args;
10671 tree t, args_tree;
10673 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10674 return list;
10676 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
10678 if (args->length () == 0)
10680 c_parser_error (parser, "expected integer expression before ')'");
10681 release_tree_vector (args);
10682 return list;
10685 args_tree = build_tree_list_vec (args);
10687 for (t = args_tree; t; t = TREE_CHAIN (t))
10689 tree targ = TREE_VALUE (t);
10691 if (targ != error_mark_node)
10693 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
10695 c_parser_error (parser, "expression must be integral");
10696 targ = error_mark_node;
10698 else
10700 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
10702 OMP_CLAUSE_DECL (c) = targ;
10703 OMP_CLAUSE_CHAIN (c) = list;
10704 list = c;
10709 release_tree_vector (args);
10710 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10711 return list;
10714 /* OpenACC 2.0, OpenMP 2.5:
10715 variable-list:
10716 identifier
10717 variable-list , identifier
10719 If KIND is nonzero, create the appropriate node and install the
10720 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
10721 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
10723 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
10724 return the list created. */
10726 static tree
10727 c_parser_omp_variable_list (c_parser *parser,
10728 location_t clause_loc,
10729 enum omp_clause_code kind, tree list)
10731 if (c_parser_next_token_is_not (parser, CPP_NAME)
10732 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
10733 c_parser_error (parser, "expected identifier");
10735 while (c_parser_next_token_is (parser, CPP_NAME)
10736 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
10738 tree t = lookup_name (c_parser_peek_token (parser)->value);
10740 if (t == NULL_TREE)
10742 undeclared_variable (c_parser_peek_token (parser)->location,
10743 c_parser_peek_token (parser)->value);
10744 t = error_mark_node;
10747 c_parser_consume_token (parser);
10749 if (t == error_mark_node)
10751 else if (kind != 0)
10753 switch (kind)
10755 case OMP_CLAUSE__CACHE_:
10756 /* The OpenACC cache directive explicitly only allows "array
10757 elements or subarrays". */
10758 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
10760 c_parser_error (parser, "expected %<[%>");
10761 t = error_mark_node;
10762 break;
10764 /* FALLTHROUGH */
10765 case OMP_CLAUSE_MAP:
10766 case OMP_CLAUSE_FROM:
10767 case OMP_CLAUSE_TO:
10768 while (c_parser_next_token_is (parser, CPP_DOT))
10770 location_t op_loc = c_parser_peek_token (parser)->location;
10771 c_parser_consume_token (parser);
10772 if (!c_parser_next_token_is (parser, CPP_NAME))
10774 c_parser_error (parser, "expected identifier");
10775 t = error_mark_node;
10776 break;
10779 c_token *comp_tok = c_parser_peek_token (parser);
10780 tree ident = comp_tok->value;
10781 location_t comp_loc = comp_tok->location;
10782 c_parser_consume_token (parser);
10783 t = build_component_ref (op_loc, t, ident, comp_loc);
10785 /* FALLTHROUGH */
10786 case OMP_CLAUSE_DEPEND:
10787 case OMP_CLAUSE_REDUCTION:
10788 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
10790 tree low_bound = NULL_TREE, length = NULL_TREE;
10792 c_parser_consume_token (parser);
10793 if (!c_parser_next_token_is (parser, CPP_COLON))
10795 location_t expr_loc
10796 = c_parser_peek_token (parser)->location;
10797 c_expr expr = c_parser_expression (parser);
10798 expr = convert_lvalue_to_rvalue (expr_loc, expr,
10799 false, true);
10800 low_bound = expr.value;
10802 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10803 length = integer_one_node;
10804 else
10806 /* Look for `:'. */
10807 if (!c_parser_require (parser, CPP_COLON,
10808 "expected %<:%>"))
10810 t = error_mark_node;
10811 break;
10813 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
10815 location_t expr_loc
10816 = c_parser_peek_token (parser)->location;
10817 c_expr expr = c_parser_expression (parser);
10818 expr = convert_lvalue_to_rvalue (expr_loc, expr,
10819 false, true);
10820 length = expr.value;
10823 /* Look for the closing `]'. */
10824 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
10825 "expected %<]%>"))
10827 t = error_mark_node;
10828 break;
10831 t = tree_cons (low_bound, length, t);
10833 break;
10834 default:
10835 break;
10838 if (t != error_mark_node)
10840 tree u = build_omp_clause (clause_loc, kind);
10841 OMP_CLAUSE_DECL (u) = t;
10842 OMP_CLAUSE_CHAIN (u) = list;
10843 list = u;
10846 else
10847 list = tree_cons (t, NULL_TREE, list);
10849 if (c_parser_next_token_is_not (parser, CPP_COMMA))
10850 break;
10852 c_parser_consume_token (parser);
10855 return list;
10858 /* Similarly, but expect leading and trailing parenthesis. This is a very
10859 common case for OpenACC and OpenMP clauses. */
10861 static tree
10862 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
10863 tree list)
10865 /* The clauses location. */
10866 location_t loc = c_parser_peek_token (parser)->location;
10868 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
10870 list = c_parser_omp_variable_list (parser, loc, kind, list);
10871 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
10873 return list;
10876 /* OpenACC 2.0:
10877 copy ( variable-list )
10878 copyin ( variable-list )
10879 copyout ( variable-list )
10880 create ( variable-list )
10881 delete ( variable-list )
10882 present ( variable-list )
10883 present_or_copy ( variable-list )
10884 pcopy ( variable-list )
10885 present_or_copyin ( variable-list )
10886 pcopyin ( variable-list )
10887 present_or_copyout ( variable-list )
10888 pcopyout ( variable-list )
10889 present_or_create ( variable-list )
10890 pcreate ( variable-list ) */
10892 static tree
10893 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
10894 tree list)
10896 enum gomp_map_kind kind;
10897 switch (c_kind)
10899 case PRAGMA_OACC_CLAUSE_COPY:
10900 kind = GOMP_MAP_FORCE_TOFROM;
10901 break;
10902 case PRAGMA_OACC_CLAUSE_COPYIN:
10903 kind = GOMP_MAP_FORCE_TO;
10904 break;
10905 case PRAGMA_OACC_CLAUSE_COPYOUT:
10906 kind = GOMP_MAP_FORCE_FROM;
10907 break;
10908 case PRAGMA_OACC_CLAUSE_CREATE:
10909 kind = GOMP_MAP_FORCE_ALLOC;
10910 break;
10911 case PRAGMA_OACC_CLAUSE_DELETE:
10912 kind = GOMP_MAP_DELETE;
10913 break;
10914 case PRAGMA_OACC_CLAUSE_DEVICE:
10915 kind = GOMP_MAP_FORCE_TO;
10916 break;
10917 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
10918 kind = GOMP_MAP_DEVICE_RESIDENT;
10919 break;
10920 case PRAGMA_OACC_CLAUSE_HOST:
10921 case PRAGMA_OACC_CLAUSE_SELF:
10922 kind = GOMP_MAP_FORCE_FROM;
10923 break;
10924 case PRAGMA_OACC_CLAUSE_LINK:
10925 kind = GOMP_MAP_LINK;
10926 break;
10927 case PRAGMA_OACC_CLAUSE_PRESENT:
10928 kind = GOMP_MAP_FORCE_PRESENT;
10929 break;
10930 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
10931 kind = GOMP_MAP_TOFROM;
10932 break;
10933 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
10934 kind = GOMP_MAP_TO;
10935 break;
10936 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
10937 kind = GOMP_MAP_FROM;
10938 break;
10939 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
10940 kind = GOMP_MAP_ALLOC;
10941 break;
10942 default:
10943 gcc_unreachable ();
10945 tree nl, c;
10946 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
10948 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
10949 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10951 return nl;
10954 /* OpenACC 2.0:
10955 deviceptr ( variable-list ) */
10957 static tree
10958 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
10960 location_t loc = c_parser_peek_token (parser)->location;
10961 tree vars, t;
10963 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
10964 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
10965 variable-list must only allow for pointer variables. */
10966 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
10967 for (t = vars; t && t; t = TREE_CHAIN (t))
10969 tree v = TREE_PURPOSE (t);
10971 /* FIXME diagnostics: Ideally we should keep individual
10972 locations for all the variables in the var list to make the
10973 following errors more precise. Perhaps
10974 c_parser_omp_var_list_parens() should construct a list of
10975 locations to go along with the var list. */
10977 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
10978 error_at (loc, "%qD is not a variable", v);
10979 else if (TREE_TYPE (v) == error_mark_node)
10981 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
10982 error_at (loc, "%qD is not a pointer variable", v);
10984 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
10985 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
10986 OMP_CLAUSE_DECL (u) = v;
10987 OMP_CLAUSE_CHAIN (u) = list;
10988 list = u;
10991 return list;
10994 /* OpenACC 2.0, OpenMP 3.0:
10995 collapse ( constant-expression ) */
10997 static tree
10998 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11000 tree c, num = error_mark_node;
11001 HOST_WIDE_INT n;
11002 location_t loc;
11004 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11005 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11007 loc = c_parser_peek_token (parser)->location;
11008 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11010 num = c_parser_expr_no_commas (parser, NULL).value;
11011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11013 if (num == error_mark_node)
11014 return list;
11015 mark_exp_read (num);
11016 num = c_fully_fold (num, false, NULL);
11017 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11018 || !tree_fits_shwi_p (num)
11019 || (n = tree_to_shwi (num)) <= 0
11020 || (int) n != n)
11022 error_at (loc,
11023 "collapse argument needs positive constant integer expression");
11024 return list;
11026 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11027 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11028 OMP_CLAUSE_CHAIN (c) = list;
11029 return c;
11032 /* OpenMP 2.5:
11033 copyin ( variable-list ) */
11035 static tree
11036 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11038 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11041 /* OpenMP 2.5:
11042 copyprivate ( variable-list ) */
11044 static tree
11045 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11047 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11050 /* OpenMP 2.5:
11051 default ( shared | none )
11053 OpenACC 2.0:
11054 default (none) */
11056 static tree
11057 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11059 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11060 location_t loc = c_parser_peek_token (parser)->location;
11061 tree c;
11063 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11064 return list;
11065 if (c_parser_next_token_is (parser, CPP_NAME))
11067 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11069 switch (p[0])
11071 case 'n':
11072 if (strcmp ("none", p) != 0)
11073 goto invalid_kind;
11074 kind = OMP_CLAUSE_DEFAULT_NONE;
11075 break;
11077 case 's':
11078 if (strcmp ("shared", p) != 0 || is_oacc)
11079 goto invalid_kind;
11080 kind = OMP_CLAUSE_DEFAULT_SHARED;
11081 break;
11083 default:
11084 goto invalid_kind;
11087 c_parser_consume_token (parser);
11089 else
11091 invalid_kind:
11092 if (is_oacc)
11093 c_parser_error (parser, "expected %<none%>");
11094 else
11095 c_parser_error (parser, "expected %<none%> or %<shared%>");
11097 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11099 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
11100 return list;
11102 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
11103 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
11104 OMP_CLAUSE_CHAIN (c) = list;
11105 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
11107 return c;
11110 /* OpenMP 2.5:
11111 firstprivate ( variable-list ) */
11113 static tree
11114 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
11116 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
11119 /* OpenMP 3.1:
11120 final ( expression ) */
11122 static tree
11123 c_parser_omp_clause_final (c_parser *parser, tree list)
11125 location_t loc = c_parser_peek_token (parser)->location;
11126 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11128 tree t = c_parser_paren_condition (parser);
11129 tree c;
11131 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
11133 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
11134 OMP_CLAUSE_FINAL_EXPR (c) = t;
11135 OMP_CLAUSE_CHAIN (c) = list;
11136 list = c;
11138 else
11139 c_parser_error (parser, "expected %<(%>");
11141 return list;
11144 /* OpenACC, OpenMP 2.5:
11145 if ( expression )
11147 OpenMP 4.5:
11148 if ( directive-name-modifier : expression )
11150 directive-name-modifier:
11151 parallel | task | taskloop | target data | target | target update
11152 | target enter data | target exit data */
11154 static tree
11155 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
11157 location_t location = c_parser_peek_token (parser)->location;
11158 enum tree_code if_modifier = ERROR_MARK;
11160 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11161 return list;
11163 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
11165 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11166 int n = 2;
11167 if (strcmp (p, "parallel") == 0)
11168 if_modifier = OMP_PARALLEL;
11169 else if (strcmp (p, "task") == 0)
11170 if_modifier = OMP_TASK;
11171 else if (strcmp (p, "taskloop") == 0)
11172 if_modifier = OMP_TASKLOOP;
11173 else if (strcmp (p, "target") == 0)
11175 if_modifier = OMP_TARGET;
11176 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11178 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
11179 if (strcmp ("data", p) == 0)
11180 if_modifier = OMP_TARGET_DATA;
11181 else if (strcmp ("update", p) == 0)
11182 if_modifier = OMP_TARGET_UPDATE;
11183 else if (strcmp ("enter", p) == 0)
11184 if_modifier = OMP_TARGET_ENTER_DATA;
11185 else if (strcmp ("exit", p) == 0)
11186 if_modifier = OMP_TARGET_EXIT_DATA;
11187 if (if_modifier != OMP_TARGET)
11189 n = 3;
11190 c_parser_consume_token (parser);
11192 else
11194 location_t loc = c_parser_peek_2nd_token (parser)->location;
11195 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
11196 "or %<exit%>");
11197 if_modifier = ERROR_MARK;
11199 if (if_modifier == OMP_TARGET_ENTER_DATA
11200 || if_modifier == OMP_TARGET_EXIT_DATA)
11202 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
11204 p = IDENTIFIER_POINTER
11205 (c_parser_peek_2nd_token (parser)->value);
11206 if (strcmp ("data", p) == 0)
11207 n = 4;
11209 if (n == 4)
11210 c_parser_consume_token (parser);
11211 else
11213 location_t loc
11214 = c_parser_peek_2nd_token (parser)->location;
11215 error_at (loc, "expected %<data%>");
11216 if_modifier = ERROR_MARK;
11221 if (if_modifier != ERROR_MARK)
11223 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11225 c_parser_consume_token (parser);
11226 c_parser_consume_token (parser);
11228 else
11230 if (n > 2)
11232 location_t loc = c_parser_peek_2nd_token (parser)->location;
11233 error_at (loc, "expected %<:%>");
11235 if_modifier = ERROR_MARK;
11240 tree t = c_parser_condition (parser), c;
11241 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11243 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
11244 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
11246 if (if_modifier != ERROR_MARK
11247 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11249 const char *p = NULL;
11250 switch (if_modifier)
11252 case OMP_PARALLEL: p = "parallel"; break;
11253 case OMP_TASK: p = "task"; break;
11254 case OMP_TASKLOOP: p = "taskloop"; break;
11255 case OMP_TARGET_DATA: p = "target data"; break;
11256 case OMP_TARGET: p = "target"; break;
11257 case OMP_TARGET_UPDATE: p = "target update"; break;
11258 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
11259 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
11260 default: gcc_unreachable ();
11262 error_at (location, "too many %<if%> clauses with %qs modifier",
11264 return list;
11266 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
11268 if (!is_omp)
11269 error_at (location, "too many %<if%> clauses");
11270 else
11271 error_at (location, "too many %<if%> clauses without modifier");
11272 return list;
11274 else if (if_modifier == ERROR_MARK
11275 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
11277 error_at (location, "if any %<if%> clause has modifier, then all "
11278 "%<if%> clauses have to use modifier");
11279 return list;
11283 c = build_omp_clause (location, OMP_CLAUSE_IF);
11284 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
11285 OMP_CLAUSE_IF_EXPR (c) = t;
11286 OMP_CLAUSE_CHAIN (c) = list;
11287 return c;
11290 /* OpenMP 2.5:
11291 lastprivate ( variable-list ) */
11293 static tree
11294 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
11296 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
11299 /* OpenMP 3.1:
11300 mergeable */
11302 static tree
11303 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11305 tree c;
11307 /* FIXME: Should we allow duplicates? */
11308 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
11310 c = build_omp_clause (c_parser_peek_token (parser)->location,
11311 OMP_CLAUSE_MERGEABLE);
11312 OMP_CLAUSE_CHAIN (c) = list;
11314 return c;
11317 /* OpenMP 2.5:
11318 nowait */
11320 static tree
11321 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
11323 tree c;
11324 location_t loc = c_parser_peek_token (parser)->location;
11326 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
11328 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
11329 OMP_CLAUSE_CHAIN (c) = list;
11330 return c;
11333 /* OpenACC:
11334 num_gangs ( expression ) */
11336 static tree
11337 c_parser_omp_clause_num_gangs (c_parser *parser, tree list)
11339 location_t num_gangs_loc = c_parser_peek_token (parser)->location;
11340 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11342 location_t expr_loc = c_parser_peek_token (parser)->location;
11343 c_expr expr = c_parser_expression (parser);
11344 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11345 tree c, t = expr.value;
11346 t = c_fully_fold (t, false, NULL);
11348 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11350 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11352 c_parser_error (parser, "expected integer expression");
11353 return list;
11356 /* Attempt to statically determine when the number isn't positive. */
11357 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11358 build_int_cst (TREE_TYPE (t), 0));
11359 protected_set_expr_location (c, expr_loc);
11360 if (c == boolean_true_node)
11362 warning_at (expr_loc, 0,
11363 "%<num_gangs%> value must be positive");
11364 t = integer_one_node;
11367 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs");
11369 c = build_omp_clause (num_gangs_loc, OMP_CLAUSE_NUM_GANGS);
11370 OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
11371 OMP_CLAUSE_CHAIN (c) = list;
11372 list = c;
11375 return list;
11378 /* OpenMP 2.5:
11379 num_threads ( expression ) */
11381 static tree
11382 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
11384 location_t num_threads_loc = c_parser_peek_token (parser)->location;
11385 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11387 location_t expr_loc = c_parser_peek_token (parser)->location;
11388 c_expr expr = c_parser_expression (parser);
11389 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11390 tree c, t = expr.value;
11391 t = c_fully_fold (t, false, NULL);
11393 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11395 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11397 c_parser_error (parser, "expected integer expression");
11398 return list;
11401 /* Attempt to statically determine when the number isn't positive. */
11402 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11403 build_int_cst (TREE_TYPE (t), 0));
11404 protected_set_expr_location (c, expr_loc);
11405 if (c == boolean_true_node)
11407 warning_at (expr_loc, 0,
11408 "%<num_threads%> value must be positive");
11409 t = integer_one_node;
11412 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
11414 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
11415 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
11416 OMP_CLAUSE_CHAIN (c) = list;
11417 list = c;
11420 return list;
11423 /* OpenMP 4.5:
11424 num_tasks ( expression ) */
11426 static tree
11427 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
11429 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
11430 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11432 location_t expr_loc = c_parser_peek_token (parser)->location;
11433 c_expr expr = c_parser_expression (parser);
11434 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11435 tree c, t = expr.value;
11436 t = c_fully_fold (t, false, NULL);
11438 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11440 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11442 c_parser_error (parser, "expected integer expression");
11443 return list;
11446 /* Attempt to statically determine when the number isn't positive. */
11447 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11448 build_int_cst (TREE_TYPE (t), 0));
11449 if (CAN_HAVE_LOCATION_P (c))
11450 SET_EXPR_LOCATION (c, expr_loc);
11451 if (c == boolean_true_node)
11453 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
11454 t = integer_one_node;
11457 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
11459 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
11460 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
11461 OMP_CLAUSE_CHAIN (c) = list;
11462 list = c;
11465 return list;
11468 /* OpenMP 4.5:
11469 grainsize ( expression ) */
11471 static tree
11472 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
11474 location_t grainsize_loc = c_parser_peek_token (parser)->location;
11475 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11477 location_t expr_loc = c_parser_peek_token (parser)->location;
11478 c_expr expr = c_parser_expression (parser);
11479 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11480 tree c, t = expr.value;
11481 t = c_fully_fold (t, false, NULL);
11483 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11485 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11487 c_parser_error (parser, "expected integer expression");
11488 return list;
11491 /* Attempt to statically determine when the number isn't positive. */
11492 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11493 build_int_cst (TREE_TYPE (t), 0));
11494 if (CAN_HAVE_LOCATION_P (c))
11495 SET_EXPR_LOCATION (c, expr_loc);
11496 if (c == boolean_true_node)
11498 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
11499 t = integer_one_node;
11502 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
11504 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
11505 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
11506 OMP_CLAUSE_CHAIN (c) = list;
11507 list = c;
11510 return list;
11513 /* OpenMP 4.5:
11514 priority ( expression ) */
11516 static tree
11517 c_parser_omp_clause_priority (c_parser *parser, tree list)
11519 location_t priority_loc = c_parser_peek_token (parser)->location;
11520 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11522 location_t expr_loc = c_parser_peek_token (parser)->location;
11523 c_expr expr = c_parser_expression (parser);
11524 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11525 tree c, t = expr.value;
11526 t = c_fully_fold (t, false, NULL);
11528 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11530 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11532 c_parser_error (parser, "expected integer expression");
11533 return list;
11536 /* Attempt to statically determine when the number isn't
11537 non-negative. */
11538 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
11539 build_int_cst (TREE_TYPE (t), 0));
11540 if (CAN_HAVE_LOCATION_P (c))
11541 SET_EXPR_LOCATION (c, expr_loc);
11542 if (c == boolean_true_node)
11544 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
11545 t = integer_one_node;
11548 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
11550 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
11551 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
11552 OMP_CLAUSE_CHAIN (c) = list;
11553 list = c;
11556 return list;
11559 /* OpenMP 4.5:
11560 hint ( expression ) */
11562 static tree
11563 c_parser_omp_clause_hint (c_parser *parser, tree list)
11565 location_t hint_loc = c_parser_peek_token (parser)->location;
11566 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11568 location_t expr_loc = c_parser_peek_token (parser)->location;
11569 c_expr expr = c_parser_expression (parser);
11570 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11571 tree c, t = expr.value;
11572 t = c_fully_fold (t, false, NULL);
11574 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11576 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11578 c_parser_error (parser, "expected integer expression");
11579 return list;
11582 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
11584 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
11585 OMP_CLAUSE_HINT_EXPR (c) = t;
11586 OMP_CLAUSE_CHAIN (c) = list;
11587 list = c;
11590 return list;
11593 /* OpenMP 4.5:
11594 defaultmap ( tofrom : scalar ) */
11596 static tree
11597 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
11599 location_t loc = c_parser_peek_token (parser)->location;
11600 tree c;
11601 const char *p;
11603 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11604 return list;
11605 if (!c_parser_next_token_is (parser, CPP_NAME))
11607 c_parser_error (parser, "expected %<tofrom%>");
11608 goto out_err;
11610 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11611 if (strcmp (p, "tofrom") != 0)
11613 c_parser_error (parser, "expected %<tofrom%>");
11614 goto out_err;
11616 c_parser_consume_token (parser);
11617 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11618 goto out_err;
11619 if (!c_parser_next_token_is (parser, CPP_NAME))
11621 c_parser_error (parser, "expected %<scalar%>");
11622 goto out_err;
11624 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11625 if (strcmp (p, "scalar") != 0)
11627 c_parser_error (parser, "expected %<scalar%>");
11628 goto out_err;
11630 c_parser_consume_token (parser);
11631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11632 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
11633 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
11634 OMP_CLAUSE_CHAIN (c) = list;
11635 return c;
11637 out_err:
11638 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11639 return list;
11642 /* OpenACC 2.0:
11643 use_device ( variable-list )
11645 OpenMP 4.5:
11646 use_device_ptr ( variable-list ) */
11648 static tree
11649 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
11651 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
11652 list);
11655 /* OpenMP 4.5:
11656 is_device_ptr ( variable-list ) */
11658 static tree
11659 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
11661 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
11664 /* OpenACC:
11665 num_workers ( expression ) */
11667 static tree
11668 c_parser_omp_clause_num_workers (c_parser *parser, tree list)
11670 location_t num_workers_loc = c_parser_peek_token (parser)->location;
11671 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11673 location_t expr_loc = c_parser_peek_token (parser)->location;
11674 c_expr expr = c_parser_expression (parser);
11675 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
11676 tree c, t = expr.value;
11677 t = c_fully_fold (t, false, NULL);
11679 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
11681 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11683 c_parser_error (parser, "expected integer expression");
11684 return list;
11687 /* Attempt to statically determine when the number isn't positive. */
11688 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
11689 build_int_cst (TREE_TYPE (t), 0));
11690 protected_set_expr_location (c, expr_loc);
11691 if (c == boolean_true_node)
11693 warning_at (expr_loc, 0,
11694 "%<num_workers%> value must be positive");
11695 t = integer_one_node;
11698 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_workers");
11700 c = build_omp_clause (num_workers_loc, OMP_CLAUSE_NUM_WORKERS);
11701 OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
11702 OMP_CLAUSE_CHAIN (c) = list;
11703 list = c;
11706 return list;
11709 /* OpenACC:
11711 gang [( gang-arg-list )]
11712 worker [( [num:] int-expr )]
11713 vector [( [length:] int-expr )]
11715 where gang-arg is one of:
11717 [num:] int-expr
11718 static: size-expr
11720 and size-expr may be:
11723 int-expr
11726 static tree
11727 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
11728 const char *str, tree list)
11730 const char *id = "num";
11731 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
11732 location_t loc = c_parser_peek_token (parser)->location;
11734 if (kind == OMP_CLAUSE_VECTOR)
11735 id = "length";
11737 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
11739 c_parser_consume_token (parser);
11743 c_token *next = c_parser_peek_token (parser);
11744 int idx = 0;
11746 /* Gang static argument. */
11747 if (kind == OMP_CLAUSE_GANG
11748 && c_parser_next_token_is_keyword (parser, RID_STATIC))
11750 c_parser_consume_token (parser);
11752 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
11753 goto cleanup_error;
11755 idx = 1;
11756 if (ops[idx] != NULL_TREE)
11758 c_parser_error (parser, "too many %<static%> arguments");
11759 goto cleanup_error;
11762 /* Check for the '*' argument. */
11763 if (c_parser_next_token_is (parser, CPP_MULT)
11764 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11765 || c_parser_peek_2nd_token (parser)->type
11766 == CPP_CLOSE_PAREN))
11768 c_parser_consume_token (parser);
11769 ops[idx] = integer_minus_one_node;
11771 if (c_parser_next_token_is (parser, CPP_COMMA))
11773 c_parser_consume_token (parser);
11774 continue;
11776 else
11777 break;
11780 /* Worker num: argument and vector length: arguments. */
11781 else if (c_parser_next_token_is (parser, CPP_NAME)
11782 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
11783 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
11785 c_parser_consume_token (parser); /* id */
11786 c_parser_consume_token (parser); /* ':' */
11789 /* Now collect the actual argument. */
11790 if (ops[idx] != NULL_TREE)
11792 c_parser_error (parser, "unexpected argument");
11793 goto cleanup_error;
11796 location_t expr_loc = c_parser_peek_token (parser)->location;
11797 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11798 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11799 tree expr = cexpr.value;
11800 if (expr == error_mark_node)
11801 goto cleanup_error;
11803 expr = c_fully_fold (expr, false, NULL);
11805 /* Attempt to statically determine when the number isn't a
11806 positive integer. */
11808 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
11810 c_parser_error (parser, "expected integer expression");
11811 return list;
11814 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
11815 build_int_cst (TREE_TYPE (expr), 0));
11816 if (c == boolean_true_node)
11818 warning_at (loc, 0,
11819 "%<%s%> value must be positive", str);
11820 expr = integer_one_node;
11823 ops[idx] = expr;
11825 if (kind == OMP_CLAUSE_GANG
11826 && c_parser_next_token_is (parser, CPP_COMMA))
11828 c_parser_consume_token (parser);
11829 continue;
11831 break;
11833 while (1);
11835 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11836 goto cleanup_error;
11839 check_no_duplicate_clause (list, kind, str);
11841 c = build_omp_clause (loc, kind);
11843 if (ops[1])
11844 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
11846 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
11847 OMP_CLAUSE_CHAIN (c) = list;
11849 return c;
11851 cleanup_error:
11852 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
11853 return list;
11856 /* OpenACC:
11857 auto
11858 independent
11859 nohost
11860 seq */
11862 static tree
11863 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
11864 tree list)
11866 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
11868 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
11869 OMP_CLAUSE_CHAIN (c) = list;
11871 return c;
11874 /* OpenACC:
11875 async [( int-expr )] */
11877 static tree
11878 c_parser_oacc_clause_async (c_parser *parser, tree list)
11880 tree c, t;
11881 location_t loc = c_parser_peek_token (parser)->location;
11883 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
11885 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11887 c_parser_consume_token (parser);
11889 t = c_parser_expression (parser).value;
11890 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
11891 c_parser_error (parser, "expected integer expression");
11892 else if (t == error_mark_node
11893 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
11894 return list;
11896 else
11897 t = c_fully_fold (t, false, NULL);
11899 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
11901 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
11902 OMP_CLAUSE_ASYNC_EXPR (c) = t;
11903 OMP_CLAUSE_CHAIN (c) = list;
11904 list = c;
11906 return list;
11909 /* OpenACC 2.0:
11910 tile ( size-expr-list ) */
11912 static tree
11913 c_parser_oacc_clause_tile (c_parser *parser, tree list)
11915 tree c, expr = error_mark_node;
11916 location_t loc;
11917 tree tile = NULL_TREE;
11919 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11920 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11922 loc = c_parser_peek_token (parser)->location;
11923 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
11924 return list;
11928 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
11929 return list;
11931 if (c_parser_next_token_is (parser, CPP_MULT)
11932 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
11933 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
11935 c_parser_consume_token (parser);
11936 expr = integer_zero_node;
11938 else
11940 location_t expr_loc = c_parser_peek_token (parser)->location;
11941 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
11942 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
11943 expr = cexpr.value;
11945 if (expr == error_mark_node)
11947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
11948 "expected %<)%>");
11949 return list;
11952 expr = c_fully_fold (expr, false, NULL);
11954 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
11955 || !tree_fits_shwi_p (expr)
11956 || tree_to_shwi (expr) <= 0)
11958 error_at (expr_loc, "%<tile%> argument needs positive"
11959 " integral constant");
11960 expr = integer_zero_node;
11964 tile = tree_cons (NULL_TREE, expr, tile);
11966 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
11968 /* Consume the trailing ')'. */
11969 c_parser_consume_token (parser);
11971 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
11972 tile = nreverse (tile);
11973 OMP_CLAUSE_TILE_LIST (c) = tile;
11974 OMP_CLAUSE_CHAIN (c) = list;
11975 return c;
11978 /* OpenACC:
11979 wait ( int-expr-list ) */
11981 static tree
11982 c_parser_oacc_clause_wait (c_parser *parser, tree list)
11984 location_t clause_loc = c_parser_peek_token (parser)->location;
11986 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
11987 list = c_parser_oacc_wait_list (parser, clause_loc, list);
11989 return list;
11992 /* OpenMP 2.5:
11993 ordered
11995 OpenMP 4.5:
11996 ordered ( constant-expression ) */
11998 static tree
11999 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12001 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12003 tree c, num = NULL_TREE;
12004 HOST_WIDE_INT n;
12005 location_t loc = c_parser_peek_token (parser)->location;
12006 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12008 c_parser_consume_token (parser);
12009 num = c_parser_expr_no_commas (parser, NULL).value;
12010 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12012 if (num == error_mark_node)
12013 return list;
12014 if (num)
12016 mark_exp_read (num);
12017 num = c_fully_fold (num, false, NULL);
12018 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12019 || !tree_fits_shwi_p (num)
12020 || (n = tree_to_shwi (num)) <= 0
12021 || (int) n != n)
12023 error_at (loc, "ordered argument needs positive "
12024 "constant integer expression");
12025 return list;
12028 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12029 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12030 OMP_CLAUSE_CHAIN (c) = list;
12031 return c;
12034 /* OpenMP 2.5:
12035 private ( variable-list ) */
12037 static tree
12038 c_parser_omp_clause_private (c_parser *parser, tree list)
12040 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12043 /* OpenMP 2.5:
12044 reduction ( reduction-operator : variable-list )
12046 reduction-operator:
12047 One of: + * - & ^ | && ||
12049 OpenMP 3.1:
12051 reduction-operator:
12052 One of: + * - & ^ | && || max min
12054 OpenMP 4.0:
12056 reduction-operator:
12057 One of: + * - & ^ | && ||
12058 identifier */
12060 static tree
12061 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12063 location_t clause_loc = c_parser_peek_token (parser)->location;
12064 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12066 enum tree_code code = ERROR_MARK;
12067 tree reduc_id = NULL_TREE;
12069 switch (c_parser_peek_token (parser)->type)
12071 case CPP_PLUS:
12072 code = PLUS_EXPR;
12073 break;
12074 case CPP_MULT:
12075 code = MULT_EXPR;
12076 break;
12077 case CPP_MINUS:
12078 code = MINUS_EXPR;
12079 break;
12080 case CPP_AND:
12081 code = BIT_AND_EXPR;
12082 break;
12083 case CPP_XOR:
12084 code = BIT_XOR_EXPR;
12085 break;
12086 case CPP_OR:
12087 code = BIT_IOR_EXPR;
12088 break;
12089 case CPP_AND_AND:
12090 code = TRUTH_ANDIF_EXPR;
12091 break;
12092 case CPP_OR_OR:
12093 code = TRUTH_ORIF_EXPR;
12094 break;
12095 case CPP_NAME:
12097 const char *p
12098 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12099 if (strcmp (p, "min") == 0)
12101 code = MIN_EXPR;
12102 break;
12104 if (strcmp (p, "max") == 0)
12106 code = MAX_EXPR;
12107 break;
12109 reduc_id = c_parser_peek_token (parser)->value;
12110 break;
12112 default:
12113 c_parser_error (parser,
12114 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
12115 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
12116 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12117 return list;
12119 c_parser_consume_token (parser);
12120 reduc_id = c_omp_reduction_id (code, reduc_id);
12121 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12123 tree nl, c;
12125 nl = c_parser_omp_variable_list (parser, clause_loc,
12126 OMP_CLAUSE_REDUCTION, list);
12127 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12129 tree d = OMP_CLAUSE_DECL (c), type;
12130 if (TREE_CODE (d) != TREE_LIST)
12131 type = TREE_TYPE (d);
12132 else
12134 int cnt = 0;
12135 tree t;
12136 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
12137 cnt++;
12138 type = TREE_TYPE (t);
12139 while (cnt > 0)
12141 if (TREE_CODE (type) != POINTER_TYPE
12142 && TREE_CODE (type) != ARRAY_TYPE)
12143 break;
12144 type = TREE_TYPE (type);
12145 cnt--;
12148 while (TREE_CODE (type) == ARRAY_TYPE)
12149 type = TREE_TYPE (type);
12150 OMP_CLAUSE_REDUCTION_CODE (c) = code;
12151 if (code == ERROR_MARK
12152 || !(INTEGRAL_TYPE_P (type)
12153 || TREE_CODE (type) == REAL_TYPE
12154 || TREE_CODE (type) == COMPLEX_TYPE))
12155 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
12156 = c_omp_reduction_lookup (reduc_id,
12157 TYPE_MAIN_VARIANT (type));
12160 list = nl;
12162 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12164 return list;
12167 /* OpenMP 2.5:
12168 schedule ( schedule-kind )
12169 schedule ( schedule-kind , expression )
12171 schedule-kind:
12172 static | dynamic | guided | runtime | auto
12174 OpenMP 4.5:
12175 schedule ( schedule-modifier : schedule-kind )
12176 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
12178 schedule-modifier:
12179 simd
12180 monotonic
12181 nonmonotonic */
12183 static tree
12184 c_parser_omp_clause_schedule (c_parser *parser, tree list)
12186 tree c, t;
12187 location_t loc = c_parser_peek_token (parser)->location;
12188 int modifiers = 0, nmodifiers = 0;
12190 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12191 return list;
12193 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
12195 while (c_parser_next_token_is (parser, CPP_NAME))
12197 tree kind = c_parser_peek_token (parser)->value;
12198 const char *p = IDENTIFIER_POINTER (kind);
12199 if (strcmp ("simd", p) == 0)
12200 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
12201 else if (strcmp ("monotonic", p) == 0)
12202 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
12203 else if (strcmp ("nonmonotonic", p) == 0)
12204 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
12205 else
12206 break;
12207 c_parser_consume_token (parser);
12208 if (nmodifiers++ == 0
12209 && c_parser_next_token_is (parser, CPP_COMMA))
12210 c_parser_consume_token (parser);
12211 else
12213 c_parser_require (parser, CPP_COLON, "expected %<:%>");
12214 break;
12218 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
12219 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12220 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
12221 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
12223 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
12224 "specified");
12225 modifiers = 0;
12228 if (c_parser_next_token_is (parser, CPP_NAME))
12230 tree kind = c_parser_peek_token (parser)->value;
12231 const char *p = IDENTIFIER_POINTER (kind);
12233 switch (p[0])
12235 case 'd':
12236 if (strcmp ("dynamic", p) != 0)
12237 goto invalid_kind;
12238 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
12239 break;
12241 case 'g':
12242 if (strcmp ("guided", p) != 0)
12243 goto invalid_kind;
12244 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
12245 break;
12247 case 'r':
12248 if (strcmp ("runtime", p) != 0)
12249 goto invalid_kind;
12250 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
12251 break;
12253 default:
12254 goto invalid_kind;
12257 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
12258 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
12259 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
12260 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
12261 else
12262 goto invalid_kind;
12264 c_parser_consume_token (parser);
12265 if (c_parser_next_token_is (parser, CPP_COMMA))
12267 location_t here;
12268 c_parser_consume_token (parser);
12270 here = c_parser_peek_token (parser)->location;
12271 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12272 expr = convert_lvalue_to_rvalue (here, expr, false, true);
12273 t = expr.value;
12274 t = c_fully_fold (t, false, NULL);
12276 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
12277 error_at (here, "schedule %<runtime%> does not take "
12278 "a %<chunk_size%> parameter");
12279 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
12280 error_at (here,
12281 "schedule %<auto%> does not take "
12282 "a %<chunk_size%> parameter");
12283 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
12285 /* Attempt to statically determine when the number isn't
12286 positive. */
12287 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
12288 build_int_cst (TREE_TYPE (t), 0));
12289 protected_set_expr_location (s, loc);
12290 if (s == boolean_true_node)
12292 warning_at (loc, 0,
12293 "chunk size value must be positive");
12294 t = integer_one_node;
12296 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
12298 else
12299 c_parser_error (parser, "expected integer expression");
12301 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12303 else
12304 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12305 "expected %<,%> or %<)%>");
12307 OMP_CLAUSE_SCHEDULE_KIND (c)
12308 = (enum omp_clause_schedule_kind)
12309 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
12311 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
12312 OMP_CLAUSE_CHAIN (c) = list;
12313 return c;
12315 invalid_kind:
12316 c_parser_error (parser, "invalid schedule kind");
12317 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12318 return list;
12321 /* OpenMP 2.5:
12322 shared ( variable-list ) */
12324 static tree
12325 c_parser_omp_clause_shared (c_parser *parser, tree list)
12327 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
12330 /* OpenMP 3.0:
12331 untied */
12333 static tree
12334 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12336 tree c;
12338 /* FIXME: Should we allow duplicates? */
12339 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
12341 c = build_omp_clause (c_parser_peek_token (parser)->location,
12342 OMP_CLAUSE_UNTIED);
12343 OMP_CLAUSE_CHAIN (c) = list;
12345 return c;
12348 /* OpenACC:
12349 vector_length ( expression ) */
12351 static tree
12352 c_parser_omp_clause_vector_length (c_parser *parser, tree list)
12354 location_t vector_length_loc = c_parser_peek_token (parser)->location;
12355 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12357 location_t expr_loc = c_parser_peek_token (parser)->location;
12358 c_expr expr = c_parser_expression (parser);
12359 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12360 tree c, t = expr.value;
12361 t = c_fully_fold (t, false, NULL);
12363 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12365 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12367 c_parser_error (parser, "expected integer expression");
12368 return list;
12371 /* Attempt to statically determine when the number isn't positive. */
12372 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12373 build_int_cst (TREE_TYPE (t), 0));
12374 protected_set_expr_location (c, expr_loc);
12375 if (c == boolean_true_node)
12377 warning_at (expr_loc, 0,
12378 "%<vector_length%> value must be positive");
12379 t = integer_one_node;
12382 check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length");
12384 c = build_omp_clause (vector_length_loc, OMP_CLAUSE_VECTOR_LENGTH);
12385 OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
12386 OMP_CLAUSE_CHAIN (c) = list;
12387 list = c;
12390 return list;
12393 /* OpenMP 4.0:
12394 inbranch
12395 notinbranch */
12397 static tree
12398 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
12399 enum omp_clause_code code, tree list)
12401 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12403 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12404 OMP_CLAUSE_CHAIN (c) = list;
12406 return c;
12409 /* OpenMP 4.0:
12410 parallel
12412 sections
12413 taskgroup */
12415 static tree
12416 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
12417 enum omp_clause_code code, tree list)
12419 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12420 OMP_CLAUSE_CHAIN (c) = list;
12422 return c;
12425 /* OpenMP 4.5:
12426 nogroup */
12428 static tree
12429 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12431 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
12432 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
12433 OMP_CLAUSE_NOGROUP);
12434 OMP_CLAUSE_CHAIN (c) = list;
12435 return c;
12438 /* OpenMP 4.5:
12439 simd
12440 threads */
12442 static tree
12443 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
12444 enum omp_clause_code code, tree list)
12446 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12447 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12448 OMP_CLAUSE_CHAIN (c) = list;
12449 return c;
12452 /* OpenMP 4.0:
12453 num_teams ( expression ) */
12455 static tree
12456 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
12458 location_t num_teams_loc = c_parser_peek_token (parser)->location;
12459 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12461 location_t expr_loc = c_parser_peek_token (parser)->location;
12462 c_expr expr = c_parser_expression (parser);
12463 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12464 tree c, t = expr.value;
12465 t = c_fully_fold (t, false, NULL);
12467 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12469 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12471 c_parser_error (parser, "expected integer expression");
12472 return list;
12475 /* Attempt to statically determine when the number isn't positive. */
12476 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12477 build_int_cst (TREE_TYPE (t), 0));
12478 protected_set_expr_location (c, expr_loc);
12479 if (c == boolean_true_node)
12481 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
12482 t = integer_one_node;
12485 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
12487 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
12488 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
12489 OMP_CLAUSE_CHAIN (c) = list;
12490 list = c;
12493 return list;
12496 /* OpenMP 4.0:
12497 thread_limit ( expression ) */
12499 static tree
12500 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
12502 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
12503 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12505 location_t expr_loc = c_parser_peek_token (parser)->location;
12506 c_expr expr = c_parser_expression (parser);
12507 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12508 tree c, t = expr.value;
12509 t = c_fully_fold (t, false, NULL);
12511 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12513 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12515 c_parser_error (parser, "expected integer expression");
12516 return list;
12519 /* Attempt to statically determine when the number isn't positive. */
12520 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12521 build_int_cst (TREE_TYPE (t), 0));
12522 protected_set_expr_location (c, expr_loc);
12523 if (c == boolean_true_node)
12525 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
12526 t = integer_one_node;
12529 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
12530 "thread_limit");
12532 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
12533 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
12534 OMP_CLAUSE_CHAIN (c) = list;
12535 list = c;
12538 return list;
12541 /* OpenMP 4.0:
12542 aligned ( variable-list )
12543 aligned ( variable-list : constant-expression ) */
12545 static tree
12546 c_parser_omp_clause_aligned (c_parser *parser, tree list)
12548 location_t clause_loc = c_parser_peek_token (parser)->location;
12549 tree nl, c;
12551 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12552 return list;
12554 nl = c_parser_omp_variable_list (parser, clause_loc,
12555 OMP_CLAUSE_ALIGNED, list);
12557 if (c_parser_next_token_is (parser, CPP_COLON))
12559 c_parser_consume_token (parser);
12560 location_t expr_loc = c_parser_peek_token (parser)->location;
12561 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12562 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12563 tree alignment = expr.value;
12564 alignment = c_fully_fold (alignment, false, NULL);
12565 if (TREE_CODE (alignment) != INTEGER_CST
12566 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
12567 || tree_int_cst_sgn (alignment) != 1)
12569 error_at (clause_loc, "%<aligned%> clause alignment expression must "
12570 "be positive constant integer expression");
12571 alignment = NULL_TREE;
12574 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12575 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
12578 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12579 return nl;
12582 /* OpenMP 4.0:
12583 linear ( variable-list )
12584 linear ( variable-list : expression )
12586 OpenMP 4.5:
12587 linear ( modifier ( variable-list ) )
12588 linear ( modifier ( variable-list ) : expression ) */
12590 static tree
12591 c_parser_omp_clause_linear (c_parser *parser, tree list, bool is_cilk_simd_fn)
12593 location_t clause_loc = c_parser_peek_token (parser)->location;
12594 tree nl, c, step;
12595 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
12597 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12598 return list;
12600 if (!is_cilk_simd_fn
12601 && c_parser_next_token_is (parser, CPP_NAME))
12603 c_token *tok = c_parser_peek_token (parser);
12604 const char *p = IDENTIFIER_POINTER (tok->value);
12605 if (strcmp ("val", p) == 0)
12606 kind = OMP_CLAUSE_LINEAR_VAL;
12607 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
12608 kind = OMP_CLAUSE_LINEAR_DEFAULT;
12609 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12611 c_parser_consume_token (parser);
12612 c_parser_consume_token (parser);
12616 nl = c_parser_omp_variable_list (parser, clause_loc,
12617 OMP_CLAUSE_LINEAR, list);
12619 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
12620 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12622 if (c_parser_next_token_is (parser, CPP_COLON))
12624 c_parser_consume_token (parser);
12625 location_t expr_loc = c_parser_peek_token (parser)->location;
12626 c_expr expr = c_parser_expression (parser);
12627 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12628 step = expr.value;
12629 step = c_fully_fold (step, false, NULL);
12630 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
12632 sorry ("using parameters for %<linear%> step is not supported yet");
12633 step = integer_one_node;
12635 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
12637 error_at (clause_loc, "%<linear%> clause step expression must "
12638 "be integral");
12639 step = integer_one_node;
12643 else
12644 step = integer_one_node;
12646 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12648 OMP_CLAUSE_LINEAR_STEP (c) = step;
12649 OMP_CLAUSE_LINEAR_KIND (c) = kind;
12652 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12653 return nl;
12656 /* OpenMP 4.0:
12657 safelen ( constant-expression ) */
12659 static tree
12660 c_parser_omp_clause_safelen (c_parser *parser, tree list)
12662 location_t clause_loc = c_parser_peek_token (parser)->location;
12663 tree c, t;
12665 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12666 return list;
12668 location_t expr_loc = c_parser_peek_token (parser)->location;
12669 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12670 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12671 t = expr.value;
12672 t = c_fully_fold (t, false, NULL);
12673 if (TREE_CODE (t) != INTEGER_CST
12674 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12675 || tree_int_cst_sgn (t) != 1)
12677 error_at (clause_loc, "%<safelen%> clause expression must "
12678 "be positive constant integer expression");
12679 t = NULL_TREE;
12682 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12683 if (t == NULL_TREE || t == error_mark_node)
12684 return list;
12686 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
12688 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
12689 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
12690 OMP_CLAUSE_CHAIN (c) = list;
12691 return c;
12694 /* OpenMP 4.0:
12695 simdlen ( constant-expression ) */
12697 static tree
12698 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
12700 location_t clause_loc = c_parser_peek_token (parser)->location;
12701 tree c, t;
12703 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12704 return list;
12706 location_t expr_loc = c_parser_peek_token (parser)->location;
12707 c_expr expr = c_parser_expr_no_commas (parser, NULL);
12708 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12709 t = expr.value;
12710 t = c_fully_fold (t, false, NULL);
12711 if (TREE_CODE (t) != INTEGER_CST
12712 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
12713 || tree_int_cst_sgn (t) != 1)
12715 error_at (clause_loc, "%<simdlen%> clause expression must "
12716 "be positive constant integer expression");
12717 t = NULL_TREE;
12720 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12721 if (t == NULL_TREE || t == error_mark_node)
12722 return list;
12724 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
12726 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
12727 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
12728 OMP_CLAUSE_CHAIN (c) = list;
12729 return c;
12732 /* OpenMP 4.5:
12733 vec:
12734 identifier [+/- integer]
12735 vec , identifier [+/- integer]
12738 static tree
12739 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
12740 tree list)
12742 tree vec = NULL;
12743 if (c_parser_next_token_is_not (parser, CPP_NAME)
12744 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
12746 c_parser_error (parser, "expected identifier");
12747 return list;
12750 while (c_parser_next_token_is (parser, CPP_NAME)
12751 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
12753 tree t = lookup_name (c_parser_peek_token (parser)->value);
12754 tree addend = NULL;
12756 if (t == NULL_TREE)
12758 undeclared_variable (c_parser_peek_token (parser)->location,
12759 c_parser_peek_token (parser)->value);
12760 t = error_mark_node;
12763 c_parser_consume_token (parser);
12765 bool neg = false;
12766 if (c_parser_next_token_is (parser, CPP_MINUS))
12767 neg = true;
12768 else if (!c_parser_next_token_is (parser, CPP_PLUS))
12770 addend = integer_zero_node;
12771 neg = false;
12772 goto add_to_vector;
12774 c_parser_consume_token (parser);
12776 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
12778 c_parser_error (parser, "expected integer");
12779 return list;
12782 addend = c_parser_peek_token (parser)->value;
12783 if (TREE_CODE (addend) != INTEGER_CST)
12785 c_parser_error (parser, "expected integer");
12786 return list;
12788 c_parser_consume_token (parser);
12790 add_to_vector:
12791 if (t != error_mark_node)
12793 vec = tree_cons (addend, t, vec);
12794 if (neg)
12795 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
12798 if (c_parser_next_token_is_not (parser, CPP_COMMA))
12799 break;
12801 c_parser_consume_token (parser);
12804 if (vec == NULL_TREE)
12805 return list;
12807 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12808 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
12809 OMP_CLAUSE_DECL (u) = nreverse (vec);
12810 OMP_CLAUSE_CHAIN (u) = list;
12811 return u;
12814 /* OpenMP 4.0:
12815 depend ( depend-kind: variable-list )
12817 depend-kind:
12818 in | out | inout
12820 OpenMP 4.5:
12821 depend ( source )
12823 depend ( sink : vec ) */
12825 static tree
12826 c_parser_omp_clause_depend (c_parser *parser, tree list)
12828 location_t clause_loc = c_parser_peek_token (parser)->location;
12829 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
12830 tree nl, c;
12832 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12833 return list;
12835 if (c_parser_next_token_is (parser, CPP_NAME))
12837 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12838 if (strcmp ("in", p) == 0)
12839 kind = OMP_CLAUSE_DEPEND_IN;
12840 else if (strcmp ("inout", p) == 0)
12841 kind = OMP_CLAUSE_DEPEND_INOUT;
12842 else if (strcmp ("out", p) == 0)
12843 kind = OMP_CLAUSE_DEPEND_OUT;
12844 else if (strcmp ("source", p) == 0)
12845 kind = OMP_CLAUSE_DEPEND_SOURCE;
12846 else if (strcmp ("sink", p) == 0)
12847 kind = OMP_CLAUSE_DEPEND_SINK;
12848 else
12849 goto invalid_kind;
12851 else
12852 goto invalid_kind;
12854 c_parser_consume_token (parser);
12856 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
12858 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
12859 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12860 OMP_CLAUSE_DECL (c) = NULL_TREE;
12861 OMP_CLAUSE_CHAIN (c) = list;
12862 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12863 return c;
12866 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12867 goto resync_fail;
12869 if (kind == OMP_CLAUSE_DEPEND_SINK)
12870 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
12871 else
12873 nl = c_parser_omp_variable_list (parser, clause_loc,
12874 OMP_CLAUSE_DEPEND, list);
12876 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
12877 OMP_CLAUSE_DEPEND_KIND (c) = kind;
12880 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12881 return nl;
12883 invalid_kind:
12884 c_parser_error (parser, "invalid depend kind");
12885 resync_fail:
12886 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12887 return list;
12890 /* OpenMP 4.0:
12891 map ( map-kind: variable-list )
12892 map ( variable-list )
12894 map-kind:
12895 alloc | to | from | tofrom
12897 OpenMP 4.5:
12898 map-kind:
12899 alloc | to | from | tofrom | release | delete
12901 map ( always [,] map-kind: variable-list ) */
12903 static tree
12904 c_parser_omp_clause_map (c_parser *parser, tree list)
12906 location_t clause_loc = c_parser_peek_token (parser)->location;
12907 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
12908 int always = 0;
12909 enum c_id_kind always_id_kind = C_ID_NONE;
12910 location_t always_loc = UNKNOWN_LOCATION;
12911 tree always_id = NULL_TREE;
12912 tree nl, c;
12914 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12915 return list;
12917 if (c_parser_next_token_is (parser, CPP_NAME))
12919 c_token *tok = c_parser_peek_token (parser);
12920 const char *p = IDENTIFIER_POINTER (tok->value);
12921 always_id_kind = tok->id_kind;
12922 always_loc = tok->location;
12923 always_id = tok->value;
12924 if (strcmp ("always", p) == 0)
12926 c_token *sectok = c_parser_peek_2nd_token (parser);
12927 if (sectok->type == CPP_COMMA)
12929 c_parser_consume_token (parser);
12930 c_parser_consume_token (parser);
12931 always = 2;
12933 else if (sectok->type == CPP_NAME)
12935 p = IDENTIFIER_POINTER (sectok->value);
12936 if (strcmp ("alloc", p) == 0
12937 || strcmp ("to", p) == 0
12938 || strcmp ("from", p) == 0
12939 || strcmp ("tofrom", p) == 0
12940 || strcmp ("release", p) == 0
12941 || strcmp ("delete", p) == 0)
12943 c_parser_consume_token (parser);
12944 always = 1;
12950 if (c_parser_next_token_is (parser, CPP_NAME)
12951 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12953 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12954 if (strcmp ("alloc", p) == 0)
12955 kind = GOMP_MAP_ALLOC;
12956 else if (strcmp ("to", p) == 0)
12957 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
12958 else if (strcmp ("from", p) == 0)
12959 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
12960 else if (strcmp ("tofrom", p) == 0)
12961 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
12962 else if (strcmp ("release", p) == 0)
12963 kind = GOMP_MAP_RELEASE;
12964 else if (strcmp ("delete", p) == 0)
12965 kind = GOMP_MAP_DELETE;
12966 else
12968 c_parser_error (parser, "invalid map kind");
12969 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12970 "expected %<)%>");
12971 return list;
12973 c_parser_consume_token (parser);
12974 c_parser_consume_token (parser);
12976 else if (always)
12978 if (always_id_kind != C_ID_ID)
12980 c_parser_error (parser, "expected identifier");
12981 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
12982 return list;
12985 tree t = lookup_name (always_id);
12986 if (t == NULL_TREE)
12988 undeclared_variable (always_loc, always_id);
12989 t = error_mark_node;
12991 if (t != error_mark_node)
12993 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
12994 OMP_CLAUSE_DECL (u) = t;
12995 OMP_CLAUSE_CHAIN (u) = list;
12996 OMP_CLAUSE_SET_MAP_KIND (u, kind);
12997 list = u;
12999 if (always == 1)
13001 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13002 return list;
13006 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13008 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13009 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13011 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13012 return nl;
13015 /* OpenMP 4.0:
13016 device ( expression ) */
13018 static tree
13019 c_parser_omp_clause_device (c_parser *parser, tree list)
13021 location_t clause_loc = c_parser_peek_token (parser)->location;
13022 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13024 location_t expr_loc = c_parser_peek_token (parser)->location;
13025 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13026 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13027 tree c, t = expr.value;
13028 t = c_fully_fold (t, false, NULL);
13030 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13032 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13034 c_parser_error (parser, "expected integer expression");
13035 return list;
13038 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13040 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13041 OMP_CLAUSE_DEVICE_ID (c) = t;
13042 OMP_CLAUSE_CHAIN (c) = list;
13043 list = c;
13046 return list;
13049 /* OpenMP 4.0:
13050 dist_schedule ( static )
13051 dist_schedule ( static , expression ) */
13053 static tree
13054 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13056 tree c, t = NULL_TREE;
13057 location_t loc = c_parser_peek_token (parser)->location;
13059 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13060 return list;
13062 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13064 c_parser_error (parser, "invalid dist_schedule kind");
13065 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13066 "expected %<)%>");
13067 return list;
13070 c_parser_consume_token (parser);
13071 if (c_parser_next_token_is (parser, CPP_COMMA))
13073 c_parser_consume_token (parser);
13075 location_t expr_loc = c_parser_peek_token (parser)->location;
13076 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13077 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13078 t = expr.value;
13079 t = c_fully_fold (t, false, NULL);
13080 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13082 else
13083 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13084 "expected %<,%> or %<)%>");
13086 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13087 if (t == error_mark_node)
13088 return list;
13090 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13091 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13092 OMP_CLAUSE_CHAIN (c) = list;
13093 return c;
13096 /* OpenMP 4.0:
13097 proc_bind ( proc-bind-kind )
13099 proc-bind-kind:
13100 master | close | spread */
13102 static tree
13103 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13105 location_t clause_loc = c_parser_peek_token (parser)->location;
13106 enum omp_clause_proc_bind_kind kind;
13107 tree c;
13109 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13110 return list;
13112 if (c_parser_next_token_is (parser, CPP_NAME))
13114 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13115 if (strcmp ("master", p) == 0)
13116 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13117 else if (strcmp ("close", p) == 0)
13118 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13119 else if (strcmp ("spread", p) == 0)
13120 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13121 else
13122 goto invalid_kind;
13124 else
13125 goto invalid_kind;
13127 c_parser_consume_token (parser);
13128 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13129 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13130 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13131 OMP_CLAUSE_CHAIN (c) = list;
13132 return c;
13134 invalid_kind:
13135 c_parser_error (parser, "invalid proc_bind kind");
13136 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13137 return list;
13140 /* OpenMP 4.0:
13141 to ( variable-list ) */
13143 static tree
13144 c_parser_omp_clause_to (c_parser *parser, tree list)
13146 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
13149 /* OpenMP 4.0:
13150 from ( variable-list ) */
13152 static tree
13153 c_parser_omp_clause_from (c_parser *parser, tree list)
13155 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
13158 /* OpenMP 4.0:
13159 uniform ( variable-list ) */
13161 static tree
13162 c_parser_omp_clause_uniform (c_parser *parser, tree list)
13164 /* The clauses location. */
13165 location_t loc = c_parser_peek_token (parser)->location;
13167 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
13169 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
13170 list);
13171 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
13173 return list;
13176 /* Parse all OpenACC clauses. The set clauses allowed by the directive
13177 is a bitmask in MASK. Return the list of clauses found. */
13179 static tree
13180 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
13181 const char *where, bool finish_p = true)
13183 tree clauses = NULL;
13184 bool first = true;
13186 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13188 location_t here;
13189 pragma_omp_clause c_kind;
13190 const char *c_name;
13191 tree prev = clauses;
13193 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13194 c_parser_consume_token (parser);
13196 here = c_parser_peek_token (parser)->location;
13197 c_kind = c_parser_omp_clause_name (parser);
13199 switch (c_kind)
13201 case PRAGMA_OACC_CLAUSE_ASYNC:
13202 clauses = c_parser_oacc_clause_async (parser, clauses);
13203 c_name = "async";
13204 break;
13205 case PRAGMA_OACC_CLAUSE_AUTO:
13206 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
13207 clauses);
13208 c_name = "auto";
13209 break;
13210 case PRAGMA_OACC_CLAUSE_COLLAPSE:
13211 clauses = c_parser_omp_clause_collapse (parser, clauses);
13212 c_name = "collapse";
13213 break;
13214 case PRAGMA_OACC_CLAUSE_COPY:
13215 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13216 c_name = "copy";
13217 break;
13218 case PRAGMA_OACC_CLAUSE_COPYIN:
13219 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13220 c_name = "copyin";
13221 break;
13222 case PRAGMA_OACC_CLAUSE_COPYOUT:
13223 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13224 c_name = "copyout";
13225 break;
13226 case PRAGMA_OACC_CLAUSE_CREATE:
13227 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13228 c_name = "create";
13229 break;
13230 case PRAGMA_OACC_CLAUSE_DELETE:
13231 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13232 c_name = "delete";
13233 break;
13234 case PRAGMA_OMP_CLAUSE_DEFAULT:
13235 clauses = c_parser_omp_clause_default (parser, clauses, true);
13236 c_name = "default";
13237 break;
13238 case PRAGMA_OACC_CLAUSE_DEVICE:
13239 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13240 c_name = "device";
13241 break;
13242 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
13243 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
13244 c_name = "deviceptr";
13245 break;
13246 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
13247 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13248 c_name = "device_resident";
13249 break;
13250 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
13251 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13252 c_name = "firstprivate";
13253 break;
13254 case PRAGMA_OACC_CLAUSE_GANG:
13255 c_name = "gang";
13256 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
13257 c_name, clauses);
13258 break;
13259 case PRAGMA_OACC_CLAUSE_HOST:
13260 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13261 c_name = "host";
13262 break;
13263 case PRAGMA_OACC_CLAUSE_IF:
13264 clauses = c_parser_omp_clause_if (parser, clauses, false);
13265 c_name = "if";
13266 break;
13267 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
13268 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
13269 clauses);
13270 c_name = "independent";
13271 break;
13272 case PRAGMA_OACC_CLAUSE_LINK:
13273 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13274 c_name = "link";
13275 break;
13276 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
13277 clauses = c_parser_omp_clause_num_gangs (parser, clauses);
13278 c_name = "num_gangs";
13279 break;
13280 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
13281 clauses = c_parser_omp_clause_num_workers (parser, clauses);
13282 c_name = "num_workers";
13283 break;
13284 case PRAGMA_OACC_CLAUSE_PRESENT:
13285 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13286 c_name = "present";
13287 break;
13288 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
13289 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13290 c_name = "present_or_copy";
13291 break;
13292 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
13293 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13294 c_name = "present_or_copyin";
13295 break;
13296 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
13297 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13298 c_name = "present_or_copyout";
13299 break;
13300 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
13301 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13302 c_name = "present_or_create";
13303 break;
13304 case PRAGMA_OACC_CLAUSE_PRIVATE:
13305 clauses = c_parser_omp_clause_private (parser, clauses);
13306 c_name = "private";
13307 break;
13308 case PRAGMA_OACC_CLAUSE_REDUCTION:
13309 clauses = c_parser_omp_clause_reduction (parser, clauses);
13310 c_name = "reduction";
13311 break;
13312 case PRAGMA_OACC_CLAUSE_SELF:
13313 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
13314 c_name = "self";
13315 break;
13316 case PRAGMA_OACC_CLAUSE_SEQ:
13317 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
13318 clauses);
13319 c_name = "seq";
13320 break;
13321 case PRAGMA_OACC_CLAUSE_TILE:
13322 clauses = c_parser_oacc_clause_tile (parser, clauses);
13323 c_name = "tile";
13324 break;
13325 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
13326 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13327 c_name = "use_device";
13328 break;
13329 case PRAGMA_OACC_CLAUSE_VECTOR:
13330 c_name = "vector";
13331 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
13332 c_name, clauses);
13333 break;
13334 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
13335 clauses = c_parser_omp_clause_vector_length (parser, clauses);
13336 c_name = "vector_length";
13337 break;
13338 case PRAGMA_OACC_CLAUSE_WAIT:
13339 clauses = c_parser_oacc_clause_wait (parser, clauses);
13340 c_name = "wait";
13341 break;
13342 case PRAGMA_OACC_CLAUSE_WORKER:
13343 c_name = "worker";
13344 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
13345 c_name, clauses);
13346 break;
13347 default:
13348 c_parser_error (parser, "expected %<#pragma acc%> clause");
13349 goto saw_error;
13352 first = false;
13354 if (((mask >> c_kind) & 1) == 0)
13356 /* Remove the invalid clause(s) from the list to avoid
13357 confusing the rest of the compiler. */
13358 clauses = prev;
13359 error_at (here, "%qs is not valid for %qs", c_name, where);
13363 saw_error:
13364 c_parser_skip_to_pragma_eol (parser);
13366 if (finish_p)
13367 return c_finish_omp_clauses (clauses, C_ORT_ACC);
13369 return clauses;
13372 /* Parse all OpenMP clauses. The set clauses allowed by the directive
13373 is a bitmask in MASK. Return the list of clauses found. */
13375 static tree
13376 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
13377 const char *where, bool finish_p = true)
13379 tree clauses = NULL;
13380 bool first = true, cilk_simd_fn = false;
13382 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
13384 location_t here;
13385 pragma_omp_clause c_kind;
13386 const char *c_name;
13387 tree prev = clauses;
13389 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
13390 c_parser_consume_token (parser);
13392 here = c_parser_peek_token (parser)->location;
13393 c_kind = c_parser_omp_clause_name (parser);
13395 switch (c_kind)
13397 case PRAGMA_OMP_CLAUSE_COLLAPSE:
13398 clauses = c_parser_omp_clause_collapse (parser, clauses);
13399 c_name = "collapse";
13400 break;
13401 case PRAGMA_OMP_CLAUSE_COPYIN:
13402 clauses = c_parser_omp_clause_copyin (parser, clauses);
13403 c_name = "copyin";
13404 break;
13405 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
13406 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
13407 c_name = "copyprivate";
13408 break;
13409 case PRAGMA_OMP_CLAUSE_DEFAULT:
13410 clauses = c_parser_omp_clause_default (parser, clauses, false);
13411 c_name = "default";
13412 break;
13413 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
13414 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
13415 c_name = "firstprivate";
13416 break;
13417 case PRAGMA_OMP_CLAUSE_FINAL:
13418 clauses = c_parser_omp_clause_final (parser, clauses);
13419 c_name = "final";
13420 break;
13421 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
13422 clauses = c_parser_omp_clause_grainsize (parser, clauses);
13423 c_name = "grainsize";
13424 break;
13425 case PRAGMA_OMP_CLAUSE_HINT:
13426 clauses = c_parser_omp_clause_hint (parser, clauses);
13427 c_name = "hint";
13428 break;
13429 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
13430 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
13431 c_name = "defaultmap";
13432 break;
13433 case PRAGMA_OMP_CLAUSE_IF:
13434 clauses = c_parser_omp_clause_if (parser, clauses, true);
13435 c_name = "if";
13436 break;
13437 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
13438 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
13439 c_name = "lastprivate";
13440 break;
13441 case PRAGMA_OMP_CLAUSE_MERGEABLE:
13442 clauses = c_parser_omp_clause_mergeable (parser, clauses);
13443 c_name = "mergeable";
13444 break;
13445 case PRAGMA_OMP_CLAUSE_NOWAIT:
13446 clauses = c_parser_omp_clause_nowait (parser, clauses);
13447 c_name = "nowait";
13448 break;
13449 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
13450 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
13451 c_name = "num_tasks";
13452 break;
13453 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
13454 clauses = c_parser_omp_clause_num_threads (parser, clauses);
13455 c_name = "num_threads";
13456 break;
13457 case PRAGMA_OMP_CLAUSE_ORDERED:
13458 clauses = c_parser_omp_clause_ordered (parser, clauses);
13459 c_name = "ordered";
13460 break;
13461 case PRAGMA_OMP_CLAUSE_PRIORITY:
13462 clauses = c_parser_omp_clause_priority (parser, clauses);
13463 c_name = "priority";
13464 break;
13465 case PRAGMA_OMP_CLAUSE_PRIVATE:
13466 clauses = c_parser_omp_clause_private (parser, clauses);
13467 c_name = "private";
13468 break;
13469 case PRAGMA_OMP_CLAUSE_REDUCTION:
13470 clauses = c_parser_omp_clause_reduction (parser, clauses);
13471 c_name = "reduction";
13472 break;
13473 case PRAGMA_OMP_CLAUSE_SCHEDULE:
13474 clauses = c_parser_omp_clause_schedule (parser, clauses);
13475 c_name = "schedule";
13476 break;
13477 case PRAGMA_OMP_CLAUSE_SHARED:
13478 clauses = c_parser_omp_clause_shared (parser, clauses);
13479 c_name = "shared";
13480 break;
13481 case PRAGMA_OMP_CLAUSE_UNTIED:
13482 clauses = c_parser_omp_clause_untied (parser, clauses);
13483 c_name = "untied";
13484 break;
13485 case PRAGMA_OMP_CLAUSE_INBRANCH:
13486 case PRAGMA_CILK_CLAUSE_MASK:
13487 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
13488 clauses);
13489 c_name = "inbranch";
13490 break;
13491 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
13492 case PRAGMA_CILK_CLAUSE_NOMASK:
13493 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
13494 clauses);
13495 c_name = "notinbranch";
13496 break;
13497 case PRAGMA_OMP_CLAUSE_PARALLEL:
13498 clauses
13499 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
13500 clauses);
13501 c_name = "parallel";
13502 if (!first)
13504 clause_not_first:
13505 error_at (here, "%qs must be the first clause of %qs",
13506 c_name, where);
13507 clauses = prev;
13509 break;
13510 case PRAGMA_OMP_CLAUSE_FOR:
13511 clauses
13512 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
13513 clauses);
13514 c_name = "for";
13515 if (!first)
13516 goto clause_not_first;
13517 break;
13518 case PRAGMA_OMP_CLAUSE_SECTIONS:
13519 clauses
13520 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
13521 clauses);
13522 c_name = "sections";
13523 if (!first)
13524 goto clause_not_first;
13525 break;
13526 case PRAGMA_OMP_CLAUSE_TASKGROUP:
13527 clauses
13528 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
13529 clauses);
13530 c_name = "taskgroup";
13531 if (!first)
13532 goto clause_not_first;
13533 break;
13534 case PRAGMA_OMP_CLAUSE_LINK:
13535 clauses
13536 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
13537 c_name = "link";
13538 break;
13539 case PRAGMA_OMP_CLAUSE_TO:
13540 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
13541 clauses
13542 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
13543 clauses);
13544 else
13545 clauses = c_parser_omp_clause_to (parser, clauses);
13546 c_name = "to";
13547 break;
13548 case PRAGMA_OMP_CLAUSE_FROM:
13549 clauses = c_parser_omp_clause_from (parser, clauses);
13550 c_name = "from";
13551 break;
13552 case PRAGMA_OMP_CLAUSE_UNIFORM:
13553 clauses = c_parser_omp_clause_uniform (parser, clauses);
13554 c_name = "uniform";
13555 break;
13556 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
13557 clauses = c_parser_omp_clause_num_teams (parser, clauses);
13558 c_name = "num_teams";
13559 break;
13560 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
13561 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
13562 c_name = "thread_limit";
13563 break;
13564 case PRAGMA_OMP_CLAUSE_ALIGNED:
13565 clauses = c_parser_omp_clause_aligned (parser, clauses);
13566 c_name = "aligned";
13567 break;
13568 case PRAGMA_OMP_CLAUSE_LINEAR:
13569 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
13570 cilk_simd_fn = true;
13571 clauses = c_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
13572 c_name = "linear";
13573 break;
13574 case PRAGMA_OMP_CLAUSE_DEPEND:
13575 clauses = c_parser_omp_clause_depend (parser, clauses);
13576 c_name = "depend";
13577 break;
13578 case PRAGMA_OMP_CLAUSE_MAP:
13579 clauses = c_parser_omp_clause_map (parser, clauses);
13580 c_name = "map";
13581 break;
13582 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
13583 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
13584 c_name = "use_device_ptr";
13585 break;
13586 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
13587 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
13588 c_name = "is_device_ptr";
13589 break;
13590 case PRAGMA_OMP_CLAUSE_DEVICE:
13591 clauses = c_parser_omp_clause_device (parser, clauses);
13592 c_name = "device";
13593 break;
13594 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
13595 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
13596 c_name = "dist_schedule";
13597 break;
13598 case PRAGMA_OMP_CLAUSE_PROC_BIND:
13599 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
13600 c_name = "proc_bind";
13601 break;
13602 case PRAGMA_OMP_CLAUSE_SAFELEN:
13603 clauses = c_parser_omp_clause_safelen (parser, clauses);
13604 c_name = "safelen";
13605 break;
13606 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
13607 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, true);
13608 c_name = "simdlen";
13609 break;
13610 case PRAGMA_OMP_CLAUSE_SIMDLEN:
13611 clauses = c_parser_omp_clause_simdlen (parser, clauses);
13612 c_name = "simdlen";
13613 break;
13614 case PRAGMA_OMP_CLAUSE_NOGROUP:
13615 clauses = c_parser_omp_clause_nogroup (parser, clauses);
13616 c_name = "nogroup";
13617 break;
13618 case PRAGMA_OMP_CLAUSE_THREADS:
13619 clauses
13620 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
13621 clauses);
13622 c_name = "threads";
13623 break;
13624 case PRAGMA_OMP_CLAUSE_SIMD:
13625 clauses
13626 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
13627 clauses);
13628 c_name = "simd";
13629 break;
13630 default:
13631 c_parser_error (parser, "expected %<#pragma omp%> clause");
13632 goto saw_error;
13635 first = false;
13637 if (((mask >> c_kind) & 1) == 0)
13639 /* Remove the invalid clause(s) from the list to avoid
13640 confusing the rest of the compiler. */
13641 clauses = prev;
13642 error_at (here, "%qs is not valid for %qs", c_name, where);
13646 saw_error:
13647 c_parser_skip_to_pragma_eol (parser);
13649 if (finish_p)
13651 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
13652 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
13653 return c_finish_omp_clauses (clauses, C_ORT_OMP);
13656 return clauses;
13659 /* OpenACC 2.0, OpenMP 2.5:
13660 structured-block:
13661 statement
13663 In practice, we're also interested in adding the statement to an
13664 outer node. So it is convenient if we work around the fact that
13665 c_parser_statement calls add_stmt. */
13667 static tree
13668 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
13670 tree stmt = push_stmt_list ();
13671 c_parser_statement (parser, if_p);
13672 return pop_stmt_list (stmt);
13675 /* OpenACC 2.0:
13676 # pragma acc cache (variable-list) new-line
13678 LOC is the location of the #pragma token.
13681 static tree
13682 c_parser_oacc_cache (location_t loc, c_parser *parser)
13684 tree stmt, clauses;
13686 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
13687 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
13689 c_parser_skip_to_pragma_eol (parser);
13691 stmt = make_node (OACC_CACHE);
13692 TREE_TYPE (stmt) = void_type_node;
13693 OACC_CACHE_CLAUSES (stmt) = clauses;
13694 SET_EXPR_LOCATION (stmt, loc);
13695 add_stmt (stmt);
13697 return stmt;
13700 /* OpenACC 2.0:
13701 # pragma acc data oacc-data-clause[optseq] new-line
13702 structured-block
13704 LOC is the location of the #pragma token.
13707 #define OACC_DATA_CLAUSE_MASK \
13708 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13720 static tree
13721 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
13723 tree stmt, clauses, block;
13725 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
13726 "#pragma acc data");
13728 block = c_begin_omp_parallel ();
13729 add_stmt (c_parser_omp_structured_block (parser, if_p));
13731 stmt = c_finish_oacc_data (loc, clauses, block);
13733 return stmt;
13736 /* OpenACC 2.0:
13737 # pragma acc declare oacc-data-clause[optseq] new-line
13740 #define OACC_DECLARE_CLAUSE_MASK \
13741 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
13742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
13746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
13747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
13748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
13749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
13750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
13752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) )
13754 static void
13755 c_parser_oacc_declare (c_parser *parser)
13757 location_t pragma_loc = c_parser_peek_token (parser)->location;
13758 tree clauses, stmt, t, decl;
13760 bool error = false;
13762 c_parser_consume_pragma (parser);
13764 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
13765 "#pragma acc declare");
13766 if (!clauses)
13768 error_at (pragma_loc,
13769 "no valid clauses specified in %<#pragma acc declare%>");
13770 return;
13773 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
13775 location_t loc = OMP_CLAUSE_LOCATION (t);
13776 decl = OMP_CLAUSE_DECL (t);
13777 if (!DECL_P (decl))
13779 error_at (loc, "array section in %<#pragma acc declare%>");
13780 error = true;
13781 continue;
13784 switch (OMP_CLAUSE_MAP_KIND (t))
13786 case GOMP_MAP_FIRSTPRIVATE_POINTER:
13787 case GOMP_MAP_FORCE_ALLOC:
13788 case GOMP_MAP_FORCE_TO:
13789 case GOMP_MAP_FORCE_DEVICEPTR:
13790 case GOMP_MAP_DEVICE_RESIDENT:
13791 break;
13793 case GOMP_MAP_LINK:
13794 if (!global_bindings_p ()
13795 && (TREE_STATIC (decl)
13796 || !DECL_EXTERNAL (decl)))
13798 error_at (loc,
13799 "%qD must be a global variable in "
13800 "%<#pragma acc declare link%>",
13801 decl);
13802 error = true;
13803 continue;
13805 break;
13807 default:
13808 if (global_bindings_p ())
13810 error_at (loc, "invalid OpenACC clause at file scope");
13811 error = true;
13812 continue;
13814 if (DECL_EXTERNAL (decl))
13816 error_at (loc,
13817 "invalid use of %<extern%> variable %qD "
13818 "in %<#pragma acc declare%>", decl);
13819 error = true;
13820 continue;
13822 else if (TREE_PUBLIC (decl))
13824 error_at (loc,
13825 "invalid use of %<global%> variable %qD "
13826 "in %<#pragma acc declare%>", decl);
13827 error = true;
13828 continue;
13830 break;
13833 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
13834 || lookup_attribute ("omp declare target link",
13835 DECL_ATTRIBUTES (decl)))
13837 error_at (loc, "variable %qD used more than once with "
13838 "%<#pragma acc declare%>", decl);
13839 error = true;
13840 continue;
13843 if (!error)
13845 tree id;
13847 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
13848 id = get_identifier ("omp declare target link");
13849 else
13850 id = get_identifier ("omp declare target");
13852 DECL_ATTRIBUTES (decl)
13853 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
13855 if (global_bindings_p ())
13857 symtab_node *node = symtab_node::get (decl);
13858 if (node != NULL)
13860 node->offloadable = 1;
13861 if (ENABLE_OFFLOADING)
13863 g->have_offload = true;
13864 if (is_a <varpool_node *> (node))
13865 vec_safe_push (offload_vars, decl);
13872 if (error || global_bindings_p ())
13873 return;
13875 stmt = make_node (OACC_DECLARE);
13876 TREE_TYPE (stmt) = void_type_node;
13877 OACC_DECLARE_CLAUSES (stmt) = clauses;
13878 SET_EXPR_LOCATION (stmt, pragma_loc);
13880 add_stmt (stmt);
13882 return;
13885 /* OpenACC 2.0:
13886 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
13890 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
13893 LOC is the location of the #pragma token.
13896 #define OACC_ENTER_DATA_CLAUSE_MASK \
13897 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13899 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
13900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
13901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
13902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
13903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13905 #define OACC_EXIT_DATA_CLAUSE_MASK \
13906 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
13907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
13908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
13909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
13910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
13912 static void
13913 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
13915 location_t loc = c_parser_peek_token (parser)->location;
13916 tree clauses, stmt;
13917 const char *p = "";
13919 c_parser_consume_pragma (parser);
13921 if (c_parser_next_token_is (parser, CPP_NAME))
13923 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13924 c_parser_consume_token (parser);
13927 if (strcmp (p, "data") != 0)
13929 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
13930 enter ? "enter" : "exit");
13931 parser->error = true;
13932 c_parser_skip_to_pragma_eol (parser);
13933 return;
13936 if (enter)
13937 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
13938 "#pragma acc enter data");
13939 else
13940 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
13941 "#pragma acc exit data");
13943 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
13945 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
13946 enter ? "enter" : "exit");
13947 return;
13950 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
13951 TREE_TYPE (stmt) = void_type_node;
13952 OMP_STANDALONE_CLAUSES (stmt) = clauses;
13953 SET_EXPR_LOCATION (stmt, loc);
13954 add_stmt (stmt);
13958 /* OpenACC 2.0:
13959 # pragma acc host_data oacc-data-clause[optseq] new-line
13960 structured-block
13963 #define OACC_HOST_DATA_CLAUSE_MASK \
13964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
13966 static tree
13967 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
13969 tree stmt, clauses, block;
13971 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
13972 "#pragma acc host_data");
13974 block = c_begin_omp_parallel ();
13975 add_stmt (c_parser_omp_structured_block (parser, if_p));
13976 stmt = c_finish_oacc_host_data (loc, clauses, block);
13977 return stmt;
13981 /* OpenACC 2.0:
13983 # pragma acc loop oacc-loop-clause[optseq] new-line
13984 structured-block
13986 LOC is the location of the #pragma token.
13989 #define OACC_LOOP_CLAUSE_MASK \
13990 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
13991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
13992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
13993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
13994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
13995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
13996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
13997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
13998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
13999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14000 static tree
14001 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14002 omp_clause_mask mask, tree *cclauses, bool *if_p)
14004 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14006 strcat (p_name, " loop");
14007 mask |= OACC_LOOP_CLAUSE_MASK;
14009 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14010 cclauses == NULL);
14011 if (cclauses)
14013 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14014 if (*cclauses)
14015 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14016 if (clauses)
14017 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14020 tree block = c_begin_compound_stmt (true);
14021 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14022 if_p);
14023 block = c_end_compound_stmt (loc, block, true);
14024 add_stmt (block);
14026 return stmt;
14029 /* OpenACC 2.0:
14030 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14031 structured-block
14035 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14036 structured-block
14038 LOC is the location of the #pragma token.
14041 #define OACC_KERNELS_CLAUSE_MASK \
14042 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14057 #define OACC_PARALLEL_CLAUSE_MASK \
14058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
14072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
14073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
14074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
14075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14079 static tree
14080 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14081 enum pragma_kind p_kind, char *p_name,
14082 bool *if_p)
14084 omp_clause_mask mask;
14085 enum tree_code code;
14086 switch (p_kind)
14088 case PRAGMA_OACC_KERNELS:
14089 strcat (p_name, " kernels");
14090 mask = OACC_KERNELS_CLAUSE_MASK;
14091 code = OACC_KERNELS;
14092 break;
14093 case PRAGMA_OACC_PARALLEL:
14094 strcat (p_name, " parallel");
14095 mask = OACC_PARALLEL_CLAUSE_MASK;
14096 code = OACC_PARALLEL;
14097 break;
14098 default:
14099 gcc_unreachable ();
14102 if (c_parser_next_token_is (parser, CPP_NAME))
14104 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14105 if (strcmp (p, "loop") == 0)
14107 c_parser_consume_token (parser);
14108 tree block = c_begin_omp_parallel ();
14109 tree clauses;
14110 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14111 return c_finish_omp_construct (loc, code, block, clauses);
14115 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14117 tree block = c_begin_omp_parallel ();
14118 add_stmt (c_parser_omp_structured_block (parser, if_p));
14120 return c_finish_omp_construct (loc, code, block, clauses);
14123 /* OpenACC 2.0:
14124 # pragma acc routine oacc-routine-clause[optseq] new-line
14125 function-definition
14127 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14130 #define OACC_ROUTINE_CLAUSE_MASK \
14131 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14136 /* Parse an OpenACC routine directive. For named directives, we apply
14137 immediately to the named function. For unnamed ones we then parse
14138 a declaration or definition, which must be for a function. */
14140 static void
14141 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14143 gcc_checking_assert (context == pragma_external);
14145 oacc_routine_data data;
14146 data.error_seen = false;
14147 data.fndecl_seen = false;
14148 data.clauses = NULL_TREE;
14149 data.loc = c_parser_peek_token (parser)->location;
14151 c_parser_consume_pragma (parser);
14153 /* Look for optional '( name )'. */
14154 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14156 c_parser_consume_token (parser); /* '(' */
14158 tree decl = NULL_TREE;
14159 c_token *name_token = c_parser_peek_token (parser);
14160 location_t name_loc = name_token->location;
14161 if (name_token->type == CPP_NAME
14162 && (name_token->id_kind == C_ID_ID
14163 || name_token->id_kind == C_ID_TYPENAME))
14165 decl = lookup_name (name_token->value);
14166 if (!decl)
14167 error_at (name_loc,
14168 "%qE has not been declared", name_token->value);
14169 c_parser_consume_token (parser);
14171 else
14172 c_parser_error (parser, "expected function name");
14174 if (!decl
14175 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
14177 c_parser_skip_to_pragma_eol (parser, false);
14178 return;
14181 data.clauses
14182 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14183 "#pragma acc routine");
14185 if (TREE_CODE (decl) != FUNCTION_DECL)
14187 error_at (name_loc, "%qD does not refer to a function", decl);
14188 return;
14191 c_finish_oacc_routine (&data, decl, false);
14193 else /* No optional '( name )'. */
14195 data.clauses
14196 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
14197 "#pragma acc routine");
14199 /* Emit a helpful diagnostic if there's another pragma following this
14200 one. Also don't allow a static assertion declaration, as in the
14201 following we'll just parse a *single* "declaration or function
14202 definition", and the static assertion counts an one. */
14203 if (c_parser_next_token_is (parser, CPP_PRAGMA)
14204 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
14206 error_at (data.loc,
14207 "%<#pragma acc routine%> not immediately followed by"
14208 " function declaration or definition");
14209 /* ..., and then just keep going. */
14210 return;
14213 /* We only have to consider the pragma_external case here. */
14214 if (c_parser_next_token_is (parser, CPP_KEYWORD)
14215 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
14217 int ext = disable_extension_diagnostics ();
14219 c_parser_consume_token (parser);
14220 while (c_parser_next_token_is (parser, CPP_KEYWORD)
14221 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
14222 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14223 NULL, vNULL, &data);
14224 restore_extension_diagnostics (ext);
14226 else
14227 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
14228 NULL, vNULL, &data);
14232 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
14233 IS_DEFN is true if we're applying it to the definition. */
14235 static void
14236 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
14237 bool is_defn)
14239 /* Keep going if we're in error reporting mode. */
14240 if (data->error_seen
14241 || fndecl == error_mark_node)
14242 return;
14244 if (data->fndecl_seen)
14246 error_at (data->loc,
14247 "%<#pragma acc routine%> not immediately followed by"
14248 " a single function declaration or definition");
14249 data->error_seen = true;
14250 return;
14252 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
14254 error_at (data->loc,
14255 "%<#pragma acc routine%> not immediately followed by"
14256 " function declaration or definition");
14257 data->error_seen = true;
14258 return;
14261 if (oacc_get_fn_attrib (fndecl))
14263 error_at (data->loc,
14264 "%<#pragma acc routine%> already applied to %qD", fndecl);
14265 data->error_seen = true;
14266 return;
14269 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
14271 error_at (data->loc,
14272 TREE_USED (fndecl)
14273 ? G_("%<#pragma acc routine%> must be applied before use")
14274 : G_("%<#pragma acc routine%> must be applied before "
14275 "definition"));
14276 data->error_seen = true;
14277 return;
14280 /* Process the routine's dimension clauses. */
14281 tree dims = oacc_build_routine_dims (data->clauses);
14282 oacc_replace_fn_attrib (fndecl, dims);
14284 /* Add an "omp declare target" attribute. */
14285 DECL_ATTRIBUTES (fndecl)
14286 = tree_cons (get_identifier ("omp declare target"),
14287 NULL_TREE, DECL_ATTRIBUTES (fndecl));
14289 /* Remember that we've used this "#pragma acc routine". */
14290 data->fndecl_seen = true;
14293 /* OpenACC 2.0:
14294 # pragma acc update oacc-update-clause[optseq] new-line
14297 #define OACC_UPDATE_CLAUSE_MASK \
14298 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
14300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
14301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
14303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14305 static void
14306 c_parser_oacc_update (c_parser *parser)
14308 location_t loc = c_parser_peek_token (parser)->location;
14310 c_parser_consume_pragma (parser);
14312 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
14313 "#pragma acc update");
14314 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14316 error_at (loc,
14317 "%<#pragma acc update%> must contain at least one "
14318 "%<device%> or %<host%> or %<self%> clause");
14319 return;
14322 if (parser->error)
14323 return;
14325 tree stmt = make_node (OACC_UPDATE);
14326 TREE_TYPE (stmt) = void_type_node;
14327 OACC_UPDATE_CLAUSES (stmt) = clauses;
14328 SET_EXPR_LOCATION (stmt, loc);
14329 add_stmt (stmt);
14332 /* OpenACC 2.0:
14333 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
14335 LOC is the location of the #pragma token.
14338 #define OACC_WAIT_CLAUSE_MASK \
14339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
14341 static tree
14342 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
14344 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
14346 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
14347 list = c_parser_oacc_wait_list (parser, loc, list);
14349 strcpy (p_name, " wait");
14350 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
14351 stmt = c_finish_oacc_wait (loc, list, clauses);
14352 add_stmt (stmt);
14354 return stmt;
14357 /* OpenMP 2.5:
14358 # pragma omp atomic new-line
14359 expression-stmt
14361 expression-stmt:
14362 x binop= expr | x++ | ++x | x-- | --x
14363 binop:
14364 +, *, -, /, &, ^, |, <<, >>
14366 where x is an lvalue expression with scalar type.
14368 OpenMP 3.1:
14369 # pragma omp atomic new-line
14370 update-stmt
14372 # pragma omp atomic read new-line
14373 read-stmt
14375 # pragma omp atomic write new-line
14376 write-stmt
14378 # pragma omp atomic update new-line
14379 update-stmt
14381 # pragma omp atomic capture new-line
14382 capture-stmt
14384 # pragma omp atomic capture new-line
14385 capture-block
14387 read-stmt:
14388 v = x
14389 write-stmt:
14390 x = expr
14391 update-stmt:
14392 expression-stmt | x = x binop expr
14393 capture-stmt:
14394 v = expression-stmt
14395 capture-block:
14396 { v = x; update-stmt; } | { update-stmt; v = x; }
14398 OpenMP 4.0:
14399 update-stmt:
14400 expression-stmt | x = x binop expr | x = expr binop x
14401 capture-stmt:
14402 v = update-stmt
14403 capture-block:
14404 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
14406 where x and v are lvalue expressions with scalar type.
14408 LOC is the location of the #pragma token. */
14410 static void
14411 c_parser_omp_atomic (location_t loc, c_parser *parser)
14413 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
14414 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
14415 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
14416 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
14417 struct c_expr expr;
14418 location_t eloc;
14419 bool structured_block = false;
14420 bool swapped = false;
14421 bool seq_cst = false;
14422 bool non_lvalue_p;
14424 if (c_parser_next_token_is (parser, CPP_NAME))
14426 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14427 if (!strcmp (p, "seq_cst"))
14429 seq_cst = true;
14430 c_parser_consume_token (parser);
14431 if (c_parser_next_token_is (parser, CPP_COMMA)
14432 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14433 c_parser_consume_token (parser);
14436 if (c_parser_next_token_is (parser, CPP_NAME))
14438 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14440 if (!strcmp (p, "read"))
14441 code = OMP_ATOMIC_READ;
14442 else if (!strcmp (p, "write"))
14443 code = NOP_EXPR;
14444 else if (!strcmp (p, "update"))
14445 code = OMP_ATOMIC;
14446 else if (!strcmp (p, "capture"))
14447 code = OMP_ATOMIC_CAPTURE_NEW;
14448 else
14449 p = NULL;
14450 if (p)
14451 c_parser_consume_token (parser);
14453 if (!seq_cst)
14455 if (c_parser_next_token_is (parser, CPP_COMMA)
14456 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
14457 c_parser_consume_token (parser);
14459 if (c_parser_next_token_is (parser, CPP_NAME))
14461 const char *p
14462 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14463 if (!strcmp (p, "seq_cst"))
14465 seq_cst = true;
14466 c_parser_consume_token (parser);
14470 c_parser_skip_to_pragma_eol (parser);
14472 switch (code)
14474 case OMP_ATOMIC_READ:
14475 case NOP_EXPR: /* atomic write */
14476 v = c_parser_cast_expression (parser, NULL).value;
14477 non_lvalue_p = !lvalue_p (v);
14478 v = c_fully_fold (v, false, NULL);
14479 if (v == error_mark_node)
14480 goto saw_error;
14481 if (non_lvalue_p)
14482 v = non_lvalue (v);
14483 loc = c_parser_peek_token (parser)->location;
14484 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14485 goto saw_error;
14486 if (code == NOP_EXPR)
14488 lhs = c_parser_expression (parser).value;
14489 lhs = c_fully_fold (lhs, false, NULL);
14490 if (lhs == error_mark_node)
14491 goto saw_error;
14493 else
14495 lhs = c_parser_cast_expression (parser, NULL).value;
14496 non_lvalue_p = !lvalue_p (lhs);
14497 lhs = c_fully_fold (lhs, false, NULL);
14498 if (lhs == error_mark_node)
14499 goto saw_error;
14500 if (non_lvalue_p)
14501 lhs = non_lvalue (lhs);
14503 if (code == NOP_EXPR)
14505 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
14506 opcode. */
14507 code = OMP_ATOMIC;
14508 rhs = lhs;
14509 lhs = v;
14510 v = NULL_TREE;
14512 goto done;
14513 case OMP_ATOMIC_CAPTURE_NEW:
14514 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
14516 c_parser_consume_token (parser);
14517 structured_block = true;
14519 else
14521 v = c_parser_cast_expression (parser, NULL).value;
14522 non_lvalue_p = !lvalue_p (v);
14523 v = c_fully_fold (v, false, NULL);
14524 if (v == error_mark_node)
14525 goto saw_error;
14526 if (non_lvalue_p)
14527 v = non_lvalue (v);
14528 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14529 goto saw_error;
14531 break;
14532 default:
14533 break;
14536 /* For structured_block case we don't know yet whether
14537 old or new x should be captured. */
14538 restart:
14539 eloc = c_parser_peek_token (parser)->location;
14540 expr = c_parser_cast_expression (parser, NULL);
14541 lhs = expr.value;
14542 expr = default_function_array_conversion (eloc, expr);
14543 unfolded_lhs = expr.value;
14544 lhs = c_fully_fold (lhs, false, NULL);
14545 orig_lhs = lhs;
14546 switch (TREE_CODE (lhs))
14548 case ERROR_MARK:
14549 saw_error:
14550 c_parser_skip_to_end_of_block_or_statement (parser);
14551 if (structured_block)
14553 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14554 c_parser_consume_token (parser);
14555 else if (code == OMP_ATOMIC_CAPTURE_NEW)
14557 c_parser_skip_to_end_of_block_or_statement (parser);
14558 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
14559 c_parser_consume_token (parser);
14562 return;
14564 case POSTINCREMENT_EXPR:
14565 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14566 code = OMP_ATOMIC_CAPTURE_OLD;
14567 /* FALLTHROUGH */
14568 case PREINCREMENT_EXPR:
14569 lhs = TREE_OPERAND (lhs, 0);
14570 unfolded_lhs = NULL_TREE;
14571 opcode = PLUS_EXPR;
14572 rhs = integer_one_node;
14573 break;
14575 case POSTDECREMENT_EXPR:
14576 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
14577 code = OMP_ATOMIC_CAPTURE_OLD;
14578 /* FALLTHROUGH */
14579 case PREDECREMENT_EXPR:
14580 lhs = TREE_OPERAND (lhs, 0);
14581 unfolded_lhs = NULL_TREE;
14582 opcode = MINUS_EXPR;
14583 rhs = integer_one_node;
14584 break;
14586 case COMPOUND_EXPR:
14587 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
14588 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
14589 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
14590 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
14591 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
14592 (TREE_OPERAND (lhs, 1), 0), 0)))
14593 == BOOLEAN_TYPE)
14594 /* Undo effects of boolean_increment for post {in,de}crement. */
14595 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
14596 /* FALLTHRU */
14597 case MODIFY_EXPR:
14598 if (TREE_CODE (lhs) == MODIFY_EXPR
14599 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
14601 /* Undo effects of boolean_increment. */
14602 if (integer_onep (TREE_OPERAND (lhs, 1)))
14604 /* This is pre or post increment. */
14605 rhs = TREE_OPERAND (lhs, 1);
14606 lhs = TREE_OPERAND (lhs, 0);
14607 unfolded_lhs = NULL_TREE;
14608 opcode = NOP_EXPR;
14609 if (code == OMP_ATOMIC_CAPTURE_NEW
14610 && !structured_block
14611 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14612 code = OMP_ATOMIC_CAPTURE_OLD;
14613 break;
14615 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
14616 && TREE_OPERAND (lhs, 0)
14617 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
14619 /* This is pre or post decrement. */
14620 rhs = TREE_OPERAND (lhs, 1);
14621 lhs = TREE_OPERAND (lhs, 0);
14622 unfolded_lhs = NULL_TREE;
14623 opcode = NOP_EXPR;
14624 if (code == OMP_ATOMIC_CAPTURE_NEW
14625 && !structured_block
14626 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
14627 code = OMP_ATOMIC_CAPTURE_OLD;
14628 break;
14631 /* FALLTHRU */
14632 default:
14633 if (!lvalue_p (unfolded_lhs))
14634 lhs = non_lvalue (lhs);
14635 switch (c_parser_peek_token (parser)->type)
14637 case CPP_MULT_EQ:
14638 opcode = MULT_EXPR;
14639 break;
14640 case CPP_DIV_EQ:
14641 opcode = TRUNC_DIV_EXPR;
14642 break;
14643 case CPP_PLUS_EQ:
14644 opcode = PLUS_EXPR;
14645 break;
14646 case CPP_MINUS_EQ:
14647 opcode = MINUS_EXPR;
14648 break;
14649 case CPP_LSHIFT_EQ:
14650 opcode = LSHIFT_EXPR;
14651 break;
14652 case CPP_RSHIFT_EQ:
14653 opcode = RSHIFT_EXPR;
14654 break;
14655 case CPP_AND_EQ:
14656 opcode = BIT_AND_EXPR;
14657 break;
14658 case CPP_OR_EQ:
14659 opcode = BIT_IOR_EXPR;
14660 break;
14661 case CPP_XOR_EQ:
14662 opcode = BIT_XOR_EXPR;
14663 break;
14664 case CPP_EQ:
14665 c_parser_consume_token (parser);
14666 eloc = c_parser_peek_token (parser)->location;
14667 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
14668 rhs1 = expr.value;
14669 switch (TREE_CODE (rhs1))
14671 case MULT_EXPR:
14672 case TRUNC_DIV_EXPR:
14673 case RDIV_EXPR:
14674 case PLUS_EXPR:
14675 case MINUS_EXPR:
14676 case LSHIFT_EXPR:
14677 case RSHIFT_EXPR:
14678 case BIT_AND_EXPR:
14679 case BIT_IOR_EXPR:
14680 case BIT_XOR_EXPR:
14681 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
14683 opcode = TREE_CODE (rhs1);
14684 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14685 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14686 goto stmt_done;
14688 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
14690 opcode = TREE_CODE (rhs1);
14691 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL);
14692 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL);
14693 swapped = !commutative_tree_code (opcode);
14694 goto stmt_done;
14696 break;
14697 case ERROR_MARK:
14698 goto saw_error;
14699 default:
14700 break;
14702 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
14704 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14706 code = OMP_ATOMIC_CAPTURE_OLD;
14707 v = lhs;
14708 lhs = NULL_TREE;
14709 expr = default_function_array_read_conversion (eloc, expr);
14710 unfolded_lhs1 = expr.value;
14711 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL);
14712 rhs1 = NULL_TREE;
14713 c_parser_consume_token (parser);
14714 goto restart;
14716 if (structured_block)
14718 opcode = NOP_EXPR;
14719 expr = default_function_array_read_conversion (eloc, expr);
14720 rhs = c_fully_fold (expr.value, false, NULL);
14721 rhs1 = NULL_TREE;
14722 goto stmt_done;
14725 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
14726 goto saw_error;
14727 default:
14728 c_parser_error (parser,
14729 "invalid operator for %<#pragma omp atomic%>");
14730 goto saw_error;
14733 /* Arrange to pass the location of the assignment operator to
14734 c_finish_omp_atomic. */
14735 loc = c_parser_peek_token (parser)->location;
14736 c_parser_consume_token (parser);
14737 eloc = c_parser_peek_token (parser)->location;
14738 expr = c_parser_expression (parser);
14739 expr = default_function_array_read_conversion (eloc, expr);
14740 rhs = expr.value;
14741 rhs = c_fully_fold (rhs, false, NULL);
14742 break;
14744 stmt_done:
14745 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
14747 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
14748 goto saw_error;
14749 v = c_parser_cast_expression (parser, NULL).value;
14750 non_lvalue_p = !lvalue_p (v);
14751 v = c_fully_fold (v, false, NULL);
14752 if (v == error_mark_node)
14753 goto saw_error;
14754 if (non_lvalue_p)
14755 v = non_lvalue (v);
14756 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
14757 goto saw_error;
14758 eloc = c_parser_peek_token (parser)->location;
14759 expr = c_parser_cast_expression (parser, NULL);
14760 lhs1 = expr.value;
14761 expr = default_function_array_read_conversion (eloc, expr);
14762 unfolded_lhs1 = expr.value;
14763 lhs1 = c_fully_fold (lhs1, false, NULL);
14764 if (lhs1 == error_mark_node)
14765 goto saw_error;
14766 if (!lvalue_p (unfolded_lhs1))
14767 lhs1 = non_lvalue (lhs1);
14769 if (structured_block)
14771 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14772 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
14774 done:
14775 if (unfolded_lhs && unfolded_lhs1
14776 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
14778 error ("%<#pragma omp atomic capture%> uses two different "
14779 "expressions for memory");
14780 stmt = error_mark_node;
14782 else
14783 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
14784 swapped, seq_cst);
14785 if (stmt != error_mark_node)
14786 add_stmt (stmt);
14788 if (!structured_block)
14789 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
14793 /* OpenMP 2.5:
14794 # pragma omp barrier new-line
14797 static void
14798 c_parser_omp_barrier (c_parser *parser)
14800 location_t loc = c_parser_peek_token (parser)->location;
14801 c_parser_consume_pragma (parser);
14802 c_parser_skip_to_pragma_eol (parser);
14804 c_finish_omp_barrier (loc);
14807 /* OpenMP 2.5:
14808 # pragma omp critical [(name)] new-line
14809 structured-block
14811 OpenMP 4.5:
14812 # pragma omp critical [(name) [hint(expression)]] new-line
14814 LOC is the location of the #pragma itself. */
14816 #define OMP_CRITICAL_CLAUSE_MASK \
14817 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
14819 static tree
14820 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
14822 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
14824 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14826 c_parser_consume_token (parser);
14827 if (c_parser_next_token_is (parser, CPP_NAME))
14829 name = c_parser_peek_token (parser)->value;
14830 c_parser_consume_token (parser);
14831 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
14833 else
14834 c_parser_error (parser, "expected identifier");
14836 clauses = c_parser_omp_all_clauses (parser,
14837 OMP_CRITICAL_CLAUSE_MASK,
14838 "#pragma omp critical");
14840 else
14842 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14843 c_parser_error (parser, "expected %<(%> or end of line");
14844 c_parser_skip_to_pragma_eol (parser);
14847 stmt = c_parser_omp_structured_block (parser, if_p);
14848 return c_finish_omp_critical (loc, stmt, name, clauses);
14851 /* OpenMP 2.5:
14852 # pragma omp flush flush-vars[opt] new-line
14854 flush-vars:
14855 ( variable-list ) */
14857 static void
14858 c_parser_omp_flush (c_parser *parser)
14860 location_t loc = c_parser_peek_token (parser)->location;
14861 c_parser_consume_pragma (parser);
14862 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14863 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
14864 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14865 c_parser_error (parser, "expected %<(%> or end of line");
14866 c_parser_skip_to_pragma_eol (parser);
14868 c_finish_omp_flush (loc);
14871 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
14872 The real trick here is to determine the loop control variable early
14873 so that we can push a new decl if necessary to make it private.
14874 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
14875 respectively. */
14877 static tree
14878 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
14879 tree clauses, tree *cclauses, bool *if_p)
14881 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
14882 tree declv, condv, incrv, initv, ret = NULL_TREE;
14883 tree pre_body = NULL_TREE, this_pre_body;
14884 tree ordered_cl = NULL_TREE;
14885 bool fail = false, open_brace_parsed = false;
14886 int i, collapse = 1, ordered = 0, count, nbraces = 0;
14887 location_t for_loc;
14888 bool tiling = false;
14889 vec<tree, va_gc> *for_block = make_tree_vector ();
14891 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
14892 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
14893 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
14894 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
14896 tiling = true;
14897 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
14899 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
14900 && OMP_CLAUSE_ORDERED_EXPR (cl))
14902 ordered_cl = cl;
14903 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
14906 if (ordered && ordered < collapse)
14908 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
14909 "%<ordered%> clause parameter is less than %<collapse%>");
14910 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
14911 = build_int_cst (NULL_TREE, collapse);
14912 ordered = collapse;
14914 if (ordered)
14916 for (tree *pc = &clauses; *pc; )
14917 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
14919 error_at (OMP_CLAUSE_LOCATION (*pc),
14920 "%<linear%> clause may not be specified together "
14921 "with %<ordered%> clause with a parameter");
14922 *pc = OMP_CLAUSE_CHAIN (*pc);
14924 else
14925 pc = &OMP_CLAUSE_CHAIN (*pc);
14928 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
14929 count = ordered ? ordered : collapse;
14931 declv = make_tree_vec (count);
14932 initv = make_tree_vec (count);
14933 condv = make_tree_vec (count);
14934 incrv = make_tree_vec (count);
14936 if (code != CILK_FOR
14937 && !c_parser_next_token_is_keyword (parser, RID_FOR))
14939 c_parser_error (parser, "for statement expected");
14940 return NULL;
14942 if (code == CILK_FOR
14943 && !c_parser_next_token_is_keyword (parser, RID_CILK_FOR))
14945 c_parser_error (parser, "_Cilk_for statement expected");
14946 return NULL;
14948 for_loc = c_parser_peek_token (parser)->location;
14949 c_parser_consume_token (parser);
14951 for (i = 0; i < count; i++)
14953 int bracecount = 0;
14955 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
14956 goto pop_scopes;
14958 /* Parse the initialization declaration or expression. */
14959 if (c_parser_next_tokens_start_declaration (parser))
14961 if (i > 0)
14962 vec_safe_push (for_block, c_begin_compound_stmt (true));
14963 this_pre_body = push_stmt_list ();
14964 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
14965 NULL, vNULL);
14966 if (this_pre_body)
14968 this_pre_body = pop_stmt_list (this_pre_body);
14969 if (pre_body)
14971 tree t = pre_body;
14972 pre_body = push_stmt_list ();
14973 add_stmt (t);
14974 add_stmt (this_pre_body);
14975 pre_body = pop_stmt_list (pre_body);
14977 else
14978 pre_body = this_pre_body;
14980 decl = check_for_loop_decls (for_loc, flag_isoc99);
14981 if (decl == NULL)
14982 goto error_init;
14983 if (DECL_INITIAL (decl) == error_mark_node)
14984 decl = error_mark_node;
14985 init = decl;
14987 else if (c_parser_next_token_is (parser, CPP_NAME)
14988 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
14990 struct c_expr decl_exp;
14991 struct c_expr init_exp;
14992 location_t init_loc;
14994 decl_exp = c_parser_postfix_expression (parser);
14995 decl = decl_exp.value;
14997 c_parser_require (parser, CPP_EQ, "expected %<=%>");
14999 init_loc = c_parser_peek_token (parser)->location;
15000 init_exp = c_parser_expr_no_commas (parser, NULL);
15001 init_exp = default_function_array_read_conversion (init_loc,
15002 init_exp);
15003 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15004 NOP_EXPR, init_loc, init_exp.value,
15005 init_exp.original_type);
15006 init = c_process_expr_stmt (init_loc, init);
15008 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15010 else
15012 error_init:
15013 c_parser_error (parser,
15014 "expected iteration declaration or initialization");
15015 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15016 "expected %<)%>");
15017 fail = true;
15018 goto parse_next;
15021 /* Parse the loop condition. */
15022 cond = NULL_TREE;
15023 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15025 location_t cond_loc = c_parser_peek_token (parser)->location;
15026 struct c_expr cond_expr
15027 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15029 cond = cond_expr.value;
15030 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15031 cond = c_fully_fold (cond, false, NULL);
15032 switch (cond_expr.original_code)
15034 case GT_EXPR:
15035 case GE_EXPR:
15036 case LT_EXPR:
15037 case LE_EXPR:
15038 break;
15039 case NE_EXPR:
15040 if (code == CILK_SIMD || code == CILK_FOR)
15041 break;
15042 /* FALLTHRU. */
15043 default:
15044 /* Can't be cond = error_mark_node, because we want to preserve
15045 the location until c_finish_omp_for. */
15046 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15047 break;
15049 protected_set_expr_location (cond, cond_loc);
15051 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15053 /* Parse the increment expression. */
15054 incr = NULL_TREE;
15055 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15057 location_t incr_loc = c_parser_peek_token (parser)->location;
15059 incr = c_process_expr_stmt (incr_loc,
15060 c_parser_expression (parser).value);
15062 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15064 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15065 fail = true;
15066 else
15068 TREE_VEC_ELT (declv, i) = decl;
15069 TREE_VEC_ELT (initv, i) = init;
15070 TREE_VEC_ELT (condv, i) = cond;
15071 TREE_VEC_ELT (incrv, i) = incr;
15074 parse_next:
15075 if (i == count - 1)
15076 break;
15078 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15079 in between the collapsed for loops to be still considered perfectly
15080 nested. Hopefully the final version clarifies this.
15081 For now handle (multiple) {'s and empty statements. */
15084 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15086 c_parser_consume_token (parser);
15087 break;
15089 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15091 c_parser_consume_token (parser);
15092 bracecount++;
15094 else if (bracecount
15095 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15096 c_parser_consume_token (parser);
15097 else
15099 c_parser_error (parser, "not enough perfectly nested loops");
15100 if (bracecount)
15102 open_brace_parsed = true;
15103 bracecount--;
15105 fail = true;
15106 count = 0;
15107 break;
15110 while (1);
15112 nbraces += bracecount;
15115 if (nbraces)
15116 if_p = NULL;
15118 save_break = c_break_label;
15119 if (code == CILK_SIMD)
15120 c_break_label = build_int_cst (size_type_node, 2);
15121 else
15122 c_break_label = size_one_node;
15123 save_cont = c_cont_label;
15124 c_cont_label = NULL_TREE;
15125 body = push_stmt_list ();
15127 if (open_brace_parsed)
15129 location_t here = c_parser_peek_token (parser)->location;
15130 stmt = c_begin_compound_stmt (true);
15131 c_parser_compound_statement_nostart (parser);
15132 add_stmt (c_end_compound_stmt (here, stmt, true));
15134 else
15135 add_stmt (c_parser_c99_block_statement (parser, if_p));
15136 if (c_cont_label)
15138 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15139 SET_EXPR_LOCATION (t, loc);
15140 add_stmt (t);
15143 body = pop_stmt_list (body);
15144 c_break_label = save_break;
15145 c_cont_label = save_cont;
15147 while (nbraces)
15149 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15151 c_parser_consume_token (parser);
15152 nbraces--;
15154 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15155 c_parser_consume_token (parser);
15156 else
15158 c_parser_error (parser, "collapsed loops not perfectly nested");
15159 while (nbraces)
15161 location_t here = c_parser_peek_token (parser)->location;
15162 stmt = c_begin_compound_stmt (true);
15163 add_stmt (body);
15164 c_parser_compound_statement_nostart (parser);
15165 body = c_end_compound_stmt (here, stmt, true);
15166 nbraces--;
15168 goto pop_scopes;
15172 /* Only bother calling c_finish_omp_for if we haven't already generated
15173 an error from the initialization parsing. */
15174 if (!fail)
15176 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
15177 incrv, body, pre_body);
15179 /* Check for iterators appearing in lb, b or incr expressions. */
15180 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
15181 stmt = NULL_TREE;
15183 if (stmt)
15185 add_stmt (stmt);
15187 if (cclauses != NULL
15188 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
15190 tree *c;
15191 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
15192 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
15193 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
15194 c = &OMP_CLAUSE_CHAIN (*c);
15195 else
15197 for (i = 0; i < count; i++)
15198 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
15199 break;
15200 if (i == count)
15201 c = &OMP_CLAUSE_CHAIN (*c);
15202 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
15204 error_at (loc,
15205 "iteration variable %qD should not be firstprivate",
15206 OMP_CLAUSE_DECL (*c));
15207 *c = OMP_CLAUSE_CHAIN (*c);
15209 else
15211 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15212 tree l = *c;
15213 *c = OMP_CLAUSE_CHAIN (*c);
15214 if (code == OMP_SIMD)
15216 OMP_CLAUSE_CHAIN (l)
15217 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15218 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
15220 else
15222 OMP_CLAUSE_CHAIN (l) = clauses;
15223 clauses = l;
15228 OMP_FOR_CLAUSES (stmt) = clauses;
15230 ret = stmt;
15232 pop_scopes:
15233 while (!for_block->is_empty ())
15235 /* FIXME diagnostics: LOC below should be the actual location of
15236 this particular for block. We need to build a list of
15237 locations to go along with FOR_BLOCK. */
15238 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
15239 add_stmt (stmt);
15241 release_tree_vector (for_block);
15242 return ret;
15245 /* Helper function for OpenMP parsing, split clauses and call
15246 finish_omp_clauses on each of the set of clauses afterwards. */
15248 static void
15249 omp_split_clauses (location_t loc, enum tree_code code,
15250 omp_clause_mask mask, tree clauses, tree *cclauses)
15252 int i;
15253 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
15254 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
15255 if (cclauses[i])
15256 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
15259 /* OpenMP 4.0:
15260 #pragma omp simd simd-clause[optseq] new-line
15261 for-loop
15263 LOC is the location of the #pragma token.
15266 #define OMP_SIMD_CLAUSE_MASK \
15267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
15268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
15269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
15271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15276 static tree
15277 c_parser_omp_simd (location_t loc, c_parser *parser,
15278 char *p_name, omp_clause_mask mask, tree *cclauses,
15279 bool *if_p)
15281 tree block, clauses, ret;
15283 strcat (p_name, " simd");
15284 mask |= OMP_SIMD_CLAUSE_MASK;
15286 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15287 if (cclauses)
15289 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
15290 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
15291 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
15292 OMP_CLAUSE_ORDERED);
15293 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
15295 error_at (OMP_CLAUSE_LOCATION (c),
15296 "%<ordered%> clause with parameter may not be specified "
15297 "on %qs construct", p_name);
15298 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
15302 block = c_begin_compound_stmt (true);
15303 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
15304 block = c_end_compound_stmt (loc, block, true);
15305 add_stmt (block);
15307 return ret;
15310 /* OpenMP 2.5:
15311 #pragma omp for for-clause[optseq] new-line
15312 for-loop
15314 OpenMP 4.0:
15315 #pragma omp for simd for-simd-clause[optseq] new-line
15316 for-loop
15318 LOC is the location of the #pragma token.
15321 #define OMP_FOR_CLAUSE_MASK \
15322 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
15326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
15328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
15329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
15330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15332 static tree
15333 c_parser_omp_for (location_t loc, c_parser *parser,
15334 char *p_name, omp_clause_mask mask, tree *cclauses,
15335 bool *if_p)
15337 tree block, clauses, ret;
15339 strcat (p_name, " for");
15340 mask |= OMP_FOR_CLAUSE_MASK;
15341 /* parallel for{, simd} disallows nowait clause, but for
15342 target {teams distribute ,}parallel for{, simd} it should be accepted. */
15343 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
15344 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15345 /* Composite distribute parallel for{, simd} disallows ordered clause. */
15346 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15347 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
15349 if (c_parser_next_token_is (parser, CPP_NAME))
15351 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15353 if (strcmp (p, "simd") == 0)
15355 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15356 if (cclauses == NULL)
15357 cclauses = cclauses_buf;
15359 c_parser_consume_token (parser);
15360 if (!flag_openmp) /* flag_openmp_simd */
15361 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15362 if_p);
15363 block = c_begin_compound_stmt (true);
15364 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
15365 block = c_end_compound_stmt (loc, block, true);
15366 if (ret == NULL_TREE)
15367 return ret;
15368 ret = make_node (OMP_FOR);
15369 TREE_TYPE (ret) = void_type_node;
15370 OMP_FOR_BODY (ret) = block;
15371 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15372 SET_EXPR_LOCATION (ret, loc);
15373 add_stmt (ret);
15374 return ret;
15377 if (!flag_openmp) /* flag_openmp_simd */
15379 c_parser_skip_to_pragma_eol (parser, false);
15380 return NULL_TREE;
15383 /* Composite distribute parallel for disallows linear clause. */
15384 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15385 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
15387 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15388 if (cclauses)
15390 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
15391 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
15394 block = c_begin_compound_stmt (true);
15395 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
15396 block = c_end_compound_stmt (loc, block, true);
15397 add_stmt (block);
15399 return ret;
15402 /* OpenMP 2.5:
15403 # pragma omp master new-line
15404 structured-block
15406 LOC is the location of the #pragma token.
15409 static tree
15410 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
15412 c_parser_skip_to_pragma_eol (parser);
15413 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
15414 if_p));
15417 /* OpenMP 2.5:
15418 # pragma omp ordered new-line
15419 structured-block
15421 OpenMP 4.5:
15422 # pragma omp ordered ordered-clauses new-line
15423 structured-block
15425 # pragma omp ordered depend-clauses new-line */
15427 #define OMP_ORDERED_CLAUSE_MASK \
15428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
15429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
15431 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
15432 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
15434 static bool
15435 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
15436 bool *if_p)
15438 location_t loc = c_parser_peek_token (parser)->location;
15439 c_parser_consume_pragma (parser);
15441 if (context != pragma_stmt && context != pragma_compound)
15443 c_parser_error (parser, "expected declaration specifiers");
15444 c_parser_skip_to_pragma_eol (parser, false);
15445 return false;
15448 if (c_parser_next_token_is (parser, CPP_NAME))
15450 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15452 if (!strcmp ("depend", p))
15454 if (context == pragma_stmt)
15456 error_at (loc,
15457 "%<#pragma omp ordered%> with %<depend%> clause may "
15458 "only be used in compound statements");
15459 c_parser_skip_to_pragma_eol (parser, false);
15460 return false;
15463 tree clauses
15464 = c_parser_omp_all_clauses (parser,
15465 OMP_ORDERED_DEPEND_CLAUSE_MASK,
15466 "#pragma omp ordered");
15467 c_finish_omp_ordered (loc, clauses, NULL_TREE);
15468 return false;
15472 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
15473 "#pragma omp ordered");
15474 c_finish_omp_ordered (loc, clauses,
15475 c_parser_omp_structured_block (parser, if_p));
15476 return true;
15479 /* OpenMP 2.5:
15481 section-scope:
15482 { section-sequence }
15484 section-sequence:
15485 section-directive[opt] structured-block
15486 section-sequence section-directive structured-block
15488 SECTIONS_LOC is the location of the #pragma omp sections. */
15490 static tree
15491 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
15493 tree stmt, substmt;
15494 bool error_suppress = false;
15495 location_t loc;
15497 loc = c_parser_peek_token (parser)->location;
15498 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
15500 /* Avoid skipping until the end of the block. */
15501 parser->error = false;
15502 return NULL_TREE;
15505 stmt = push_stmt_list ();
15507 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
15509 substmt = c_parser_omp_structured_block (parser, NULL);
15510 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15511 SET_EXPR_LOCATION (substmt, loc);
15512 add_stmt (substmt);
15515 while (1)
15517 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15518 break;
15519 if (c_parser_next_token_is (parser, CPP_EOF))
15520 break;
15522 loc = c_parser_peek_token (parser)->location;
15523 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
15525 c_parser_consume_pragma (parser);
15526 c_parser_skip_to_pragma_eol (parser);
15527 error_suppress = false;
15529 else if (!error_suppress)
15531 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
15532 error_suppress = true;
15535 substmt = c_parser_omp_structured_block (parser, NULL);
15536 substmt = build1 (OMP_SECTION, void_type_node, substmt);
15537 SET_EXPR_LOCATION (substmt, loc);
15538 add_stmt (substmt);
15540 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
15541 "expected %<#pragma omp section%> or %<}%>");
15543 substmt = pop_stmt_list (stmt);
15545 stmt = make_node (OMP_SECTIONS);
15546 SET_EXPR_LOCATION (stmt, sections_loc);
15547 TREE_TYPE (stmt) = void_type_node;
15548 OMP_SECTIONS_BODY (stmt) = substmt;
15550 return add_stmt (stmt);
15553 /* OpenMP 2.5:
15554 # pragma omp sections sections-clause[optseq] newline
15555 sections-scope
15557 LOC is the location of the #pragma token.
15560 #define OMP_SECTIONS_CLAUSE_MASK \
15561 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15567 static tree
15568 c_parser_omp_sections (location_t loc, c_parser *parser,
15569 char *p_name, omp_clause_mask mask, tree *cclauses)
15571 tree block, clauses, ret;
15573 strcat (p_name, " sections");
15574 mask |= OMP_SECTIONS_CLAUSE_MASK;
15575 if (cclauses)
15576 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
15578 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15579 if (cclauses)
15581 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
15582 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
15585 block = c_begin_compound_stmt (true);
15586 ret = c_parser_omp_sections_scope (loc, parser);
15587 if (ret)
15588 OMP_SECTIONS_CLAUSES (ret) = clauses;
15589 block = c_end_compound_stmt (loc, block, true);
15590 add_stmt (block);
15592 return ret;
15595 /* OpenMP 2.5:
15596 # pragma omp parallel parallel-clause[optseq] new-line
15597 structured-block
15598 # pragma omp parallel for parallel-for-clause[optseq] new-line
15599 structured-block
15600 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
15601 structured-block
15603 OpenMP 4.0:
15604 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
15605 structured-block
15607 LOC is the location of the #pragma token.
15610 #define OMP_PARALLEL_CLAUSE_MASK \
15611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
15617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
15619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
15621 static tree
15622 c_parser_omp_parallel (location_t loc, c_parser *parser,
15623 char *p_name, omp_clause_mask mask, tree *cclauses,
15624 bool *if_p)
15626 tree stmt, clauses, block;
15628 strcat (p_name, " parallel");
15629 mask |= OMP_PARALLEL_CLAUSE_MASK;
15630 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
15631 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
15632 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
15633 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
15635 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15637 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15638 if (cclauses == NULL)
15639 cclauses = cclauses_buf;
15641 c_parser_consume_token (parser);
15642 if (!flag_openmp) /* flag_openmp_simd */
15643 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15644 block = c_begin_omp_parallel ();
15645 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
15646 stmt
15647 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15648 block);
15649 if (ret == NULL_TREE)
15650 return ret;
15651 OMP_PARALLEL_COMBINED (stmt) = 1;
15652 return stmt;
15654 /* When combined with distribute, parallel has to be followed by for.
15655 #pragma omp target parallel is allowed though. */
15656 else if (cclauses
15657 && (mask & (OMP_CLAUSE_MASK_1
15658 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
15660 error_at (loc, "expected %<for%> after %qs", p_name);
15661 c_parser_skip_to_pragma_eol (parser);
15662 return NULL_TREE;
15664 else if (!flag_openmp) /* flag_openmp_simd */
15666 c_parser_skip_to_pragma_eol (parser, false);
15667 return NULL_TREE;
15669 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
15671 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15672 if (strcmp (p, "sections") == 0)
15674 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15675 if (cclauses == NULL)
15676 cclauses = cclauses_buf;
15678 c_parser_consume_token (parser);
15679 block = c_begin_omp_parallel ();
15680 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
15681 stmt = c_finish_omp_parallel (loc,
15682 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
15683 block);
15684 OMP_PARALLEL_COMBINED (stmt) = 1;
15685 return stmt;
15689 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15690 if (cclauses)
15692 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
15693 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
15696 block = c_begin_omp_parallel ();
15697 c_parser_statement (parser, if_p);
15698 stmt = c_finish_omp_parallel (loc, clauses, block);
15700 return stmt;
15703 /* OpenMP 2.5:
15704 # pragma omp single single-clause[optseq] new-line
15705 structured-block
15707 LOC is the location of the #pragma.
15710 #define OMP_SINGLE_CLAUSE_MASK \
15711 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
15714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
15716 static tree
15717 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
15719 tree stmt = make_node (OMP_SINGLE);
15720 SET_EXPR_LOCATION (stmt, loc);
15721 TREE_TYPE (stmt) = void_type_node;
15723 OMP_SINGLE_CLAUSES (stmt)
15724 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
15725 "#pragma omp single");
15726 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
15728 return add_stmt (stmt);
15731 /* OpenMP 3.0:
15732 # pragma omp task task-clause[optseq] new-line
15734 LOC is the location of the #pragma.
15737 #define OMP_TASK_CLAUSE_MASK \
15738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
15739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
15740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
15741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
15745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
15746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
15747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
15749 static tree
15750 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
15752 tree clauses, block;
15754 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
15755 "#pragma omp task");
15757 block = c_begin_omp_task ();
15758 c_parser_statement (parser, if_p);
15759 return c_finish_omp_task (loc, clauses, block);
15762 /* OpenMP 3.0:
15763 # pragma omp taskwait new-line
15766 static void
15767 c_parser_omp_taskwait (c_parser *parser)
15769 location_t loc = c_parser_peek_token (parser)->location;
15770 c_parser_consume_pragma (parser);
15771 c_parser_skip_to_pragma_eol (parser);
15773 c_finish_omp_taskwait (loc);
15776 /* OpenMP 3.1:
15777 # pragma omp taskyield new-line
15780 static void
15781 c_parser_omp_taskyield (c_parser *parser)
15783 location_t loc = c_parser_peek_token (parser)->location;
15784 c_parser_consume_pragma (parser);
15785 c_parser_skip_to_pragma_eol (parser);
15787 c_finish_omp_taskyield (loc);
15790 /* OpenMP 4.0:
15791 # pragma omp taskgroup new-line
15794 static tree
15795 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
15797 location_t loc = c_parser_peek_token (parser)->location;
15798 c_parser_skip_to_pragma_eol (parser);
15799 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
15800 if_p));
15803 /* OpenMP 4.0:
15804 # pragma omp cancel cancel-clause[optseq] new-line
15806 LOC is the location of the #pragma.
15809 #define OMP_CANCEL_CLAUSE_MASK \
15810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
15814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
15816 static void
15817 c_parser_omp_cancel (c_parser *parser)
15819 location_t loc = c_parser_peek_token (parser)->location;
15821 c_parser_consume_pragma (parser);
15822 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
15823 "#pragma omp cancel");
15825 c_finish_omp_cancel (loc, clauses);
15828 /* OpenMP 4.0:
15829 # pragma omp cancellation point cancelpt-clause[optseq] new-line
15831 LOC is the location of the #pragma.
15834 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
15835 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
15836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
15837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
15838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
15840 static void
15841 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
15843 location_t loc = c_parser_peek_token (parser)->location;
15844 tree clauses;
15845 bool point_seen = false;
15847 c_parser_consume_pragma (parser);
15848 if (c_parser_next_token_is (parser, CPP_NAME))
15850 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15851 if (strcmp (p, "point") == 0)
15853 c_parser_consume_token (parser);
15854 point_seen = true;
15857 if (!point_seen)
15859 c_parser_error (parser, "expected %<point%>");
15860 c_parser_skip_to_pragma_eol (parser);
15861 return;
15864 if (context != pragma_compound)
15866 if (context == pragma_stmt)
15867 error_at (loc, "%<#pragma omp cancellation point%> may only be used in"
15868 " compound statements");
15869 else
15870 c_parser_error (parser, "expected declaration specifiers");
15871 c_parser_skip_to_pragma_eol (parser, false);
15872 return;
15875 clauses
15876 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
15877 "#pragma omp cancellation point");
15879 c_finish_omp_cancellation_point (loc, clauses);
15882 /* OpenMP 4.0:
15883 #pragma omp distribute distribute-clause[optseq] new-line
15884 for-loop */
15886 #define OMP_DISTRIBUTE_CLAUSE_MASK \
15887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
15890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
15891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
15893 static tree
15894 c_parser_omp_distribute (location_t loc, c_parser *parser,
15895 char *p_name, omp_clause_mask mask, tree *cclauses,
15896 bool *if_p)
15898 tree clauses, block, ret;
15900 strcat (p_name, " distribute");
15901 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
15903 if (c_parser_next_token_is (parser, CPP_NAME))
15905 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15906 bool simd = false;
15907 bool parallel = false;
15909 if (strcmp (p, "simd") == 0)
15910 simd = true;
15911 else
15912 parallel = strcmp (p, "parallel") == 0;
15913 if (parallel || simd)
15915 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15916 if (cclauses == NULL)
15917 cclauses = cclauses_buf;
15918 c_parser_consume_token (parser);
15919 if (!flag_openmp) /* flag_openmp_simd */
15921 if (simd)
15922 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15923 if_p);
15924 else
15925 return c_parser_omp_parallel (loc, parser, p_name, mask,
15926 cclauses, if_p);
15928 block = c_begin_compound_stmt (true);
15929 if (simd)
15930 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
15931 if_p);
15932 else
15933 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
15934 if_p);
15935 block = c_end_compound_stmt (loc, block, true);
15936 if (ret == NULL)
15937 return ret;
15938 ret = make_node (OMP_DISTRIBUTE);
15939 TREE_TYPE (ret) = void_type_node;
15940 OMP_FOR_BODY (ret) = block;
15941 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15942 SET_EXPR_LOCATION (ret, loc);
15943 add_stmt (ret);
15944 return ret;
15947 if (!flag_openmp) /* flag_openmp_simd */
15949 c_parser_skip_to_pragma_eol (parser, false);
15950 return NULL_TREE;
15953 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
15954 if (cclauses)
15956 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
15957 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
15960 block = c_begin_compound_stmt (true);
15961 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
15962 if_p);
15963 block = c_end_compound_stmt (loc, block, true);
15964 add_stmt (block);
15966 return ret;
15969 /* OpenMP 4.0:
15970 # pragma omp teams teams-clause[optseq] new-line
15971 structured-block */
15973 #define OMP_TEAMS_CLAUSE_MASK \
15974 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
15975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
15976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
15977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
15978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
15979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
15980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
15982 static tree
15983 c_parser_omp_teams (location_t loc, c_parser *parser,
15984 char *p_name, omp_clause_mask mask, tree *cclauses,
15985 bool *if_p)
15987 tree clauses, block, ret;
15989 strcat (p_name, " teams");
15990 mask |= OMP_TEAMS_CLAUSE_MASK;
15992 if (c_parser_next_token_is (parser, CPP_NAME))
15994 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15995 if (strcmp (p, "distribute") == 0)
15997 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
15998 if (cclauses == NULL)
15999 cclauses = cclauses_buf;
16001 c_parser_consume_token (parser);
16002 if (!flag_openmp) /* flag_openmp_simd */
16003 return c_parser_omp_distribute (loc, parser, p_name, mask,
16004 cclauses, if_p);
16005 block = c_begin_compound_stmt (true);
16006 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16007 if_p);
16008 block = c_end_compound_stmt (loc, block, true);
16009 if (ret == NULL)
16010 return ret;
16011 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16012 ret = make_node (OMP_TEAMS);
16013 TREE_TYPE (ret) = void_type_node;
16014 OMP_TEAMS_CLAUSES (ret) = clauses;
16015 OMP_TEAMS_BODY (ret) = block;
16016 OMP_TEAMS_COMBINED (ret) = 1;
16017 return add_stmt (ret);
16020 if (!flag_openmp) /* flag_openmp_simd */
16022 c_parser_skip_to_pragma_eol (parser, false);
16023 return NULL_TREE;
16026 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16027 if (cclauses)
16029 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16030 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16033 tree stmt = make_node (OMP_TEAMS);
16034 TREE_TYPE (stmt) = void_type_node;
16035 OMP_TEAMS_CLAUSES (stmt) = clauses;
16036 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16038 return add_stmt (stmt);
16041 /* OpenMP 4.0:
16042 # pragma omp target data target-data-clause[optseq] new-line
16043 structured-block */
16045 #define OMP_TARGET_DATA_CLAUSE_MASK \
16046 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16051 static tree
16052 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16054 tree clauses
16055 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16056 "#pragma omp target data");
16057 int map_seen = 0;
16058 for (tree *pc = &clauses; *pc;)
16060 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16061 switch (OMP_CLAUSE_MAP_KIND (*pc))
16063 case GOMP_MAP_TO:
16064 case GOMP_MAP_ALWAYS_TO:
16065 case GOMP_MAP_FROM:
16066 case GOMP_MAP_ALWAYS_FROM:
16067 case GOMP_MAP_TOFROM:
16068 case GOMP_MAP_ALWAYS_TOFROM:
16069 case GOMP_MAP_ALLOC:
16070 map_seen = 3;
16071 break;
16072 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16073 case GOMP_MAP_ALWAYS_POINTER:
16074 break;
16075 default:
16076 map_seen |= 1;
16077 error_at (OMP_CLAUSE_LOCATION (*pc),
16078 "%<#pragma omp target data%> with map-type other "
16079 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16080 "on %<map%> clause");
16081 *pc = OMP_CLAUSE_CHAIN (*pc);
16082 continue;
16084 pc = &OMP_CLAUSE_CHAIN (*pc);
16087 if (map_seen != 3)
16089 if (map_seen == 0)
16090 error_at (loc,
16091 "%<#pragma omp target data%> must contain at least "
16092 "one %<map%> clause");
16093 return NULL_TREE;
16096 tree stmt = make_node (OMP_TARGET_DATA);
16097 TREE_TYPE (stmt) = void_type_node;
16098 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16099 keep_next_level ();
16100 tree block = c_begin_compound_stmt (true);
16101 add_stmt (c_parser_omp_structured_block (parser, if_p));
16102 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16104 SET_EXPR_LOCATION (stmt, loc);
16105 return add_stmt (stmt);
16108 /* OpenMP 4.0:
16109 # pragma omp target update target-update-clause[optseq] new-line */
16111 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16112 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16119 static bool
16120 c_parser_omp_target_update (location_t loc, c_parser *parser,
16121 enum pragma_context context)
16123 if (context == pragma_stmt)
16125 error_at (loc,
16126 "%<#pragma omp target update%> may only be "
16127 "used in compound statements");
16128 c_parser_skip_to_pragma_eol (parser, false);
16129 return false;
16132 tree clauses
16133 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16134 "#pragma omp target update");
16135 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16136 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16138 error_at (loc,
16139 "%<#pragma omp target update%> must contain at least one "
16140 "%<from%> or %<to%> clauses");
16141 return false;
16144 tree stmt = make_node (OMP_TARGET_UPDATE);
16145 TREE_TYPE (stmt) = void_type_node;
16146 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16147 SET_EXPR_LOCATION (stmt, loc);
16148 add_stmt (stmt);
16149 return false;
16152 /* OpenMP 4.5:
16153 # pragma omp target enter data target-data-clause[optseq] new-line */
16155 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
16156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16162 static tree
16163 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
16164 enum pragma_context context)
16166 bool data_seen = false;
16167 if (c_parser_next_token_is (parser, CPP_NAME))
16169 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16170 if (strcmp (p, "data") == 0)
16172 c_parser_consume_token (parser);
16173 data_seen = true;
16176 if (!data_seen)
16178 c_parser_error (parser, "expected %<data%>");
16179 c_parser_skip_to_pragma_eol (parser);
16180 return NULL_TREE;
16183 if (context == pragma_stmt)
16185 error_at (loc,
16186 "%<#pragma omp target enter data%> may only be "
16187 "used in compound statements");
16188 c_parser_skip_to_pragma_eol (parser, false);
16189 return NULL_TREE;
16192 tree clauses
16193 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
16194 "#pragma omp target enter data");
16195 int map_seen = 0;
16196 for (tree *pc = &clauses; *pc;)
16198 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16199 switch (OMP_CLAUSE_MAP_KIND (*pc))
16201 case GOMP_MAP_TO:
16202 case GOMP_MAP_ALWAYS_TO:
16203 case GOMP_MAP_ALLOC:
16204 map_seen = 3;
16205 break;
16206 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16207 case GOMP_MAP_ALWAYS_POINTER:
16208 break;
16209 default:
16210 map_seen |= 1;
16211 error_at (OMP_CLAUSE_LOCATION (*pc),
16212 "%<#pragma omp target enter data%> with map-type other "
16213 "than %<to%> or %<alloc%> on %<map%> clause");
16214 *pc = OMP_CLAUSE_CHAIN (*pc);
16215 continue;
16217 pc = &OMP_CLAUSE_CHAIN (*pc);
16220 if (map_seen != 3)
16222 if (map_seen == 0)
16223 error_at (loc,
16224 "%<#pragma omp target enter data%> must contain at least "
16225 "one %<map%> clause");
16226 return NULL_TREE;
16229 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
16230 TREE_TYPE (stmt) = void_type_node;
16231 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
16232 SET_EXPR_LOCATION (stmt, loc);
16233 add_stmt (stmt);
16234 return stmt;
16237 /* OpenMP 4.5:
16238 # pragma omp target exit data target-data-clause[optseq] new-line */
16240 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
16241 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16247 static tree
16248 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
16249 enum pragma_context context)
16251 bool data_seen = false;
16252 if (c_parser_next_token_is (parser, CPP_NAME))
16254 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16255 if (strcmp (p, "data") == 0)
16257 c_parser_consume_token (parser);
16258 data_seen = true;
16261 if (!data_seen)
16263 c_parser_error (parser, "expected %<data%>");
16264 c_parser_skip_to_pragma_eol (parser);
16265 return NULL_TREE;
16268 if (context == pragma_stmt)
16270 error_at (loc,
16271 "%<#pragma omp target exit data%> may only be "
16272 "used in compound statements");
16273 c_parser_skip_to_pragma_eol (parser, false);
16274 return NULL_TREE;
16277 tree clauses
16278 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
16279 "#pragma omp target exit data");
16281 int map_seen = 0;
16282 for (tree *pc = &clauses; *pc;)
16284 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16285 switch (OMP_CLAUSE_MAP_KIND (*pc))
16287 case GOMP_MAP_FROM:
16288 case GOMP_MAP_ALWAYS_FROM:
16289 case GOMP_MAP_RELEASE:
16290 case GOMP_MAP_DELETE:
16291 map_seen = 3;
16292 break;
16293 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16294 case GOMP_MAP_ALWAYS_POINTER:
16295 break;
16296 default:
16297 map_seen |= 1;
16298 error_at (OMP_CLAUSE_LOCATION (*pc),
16299 "%<#pragma omp target exit data%> with map-type other "
16300 "than %<from%>, %<release%> or %<delete%> on %<map%>"
16301 " clause");
16302 *pc = OMP_CLAUSE_CHAIN (*pc);
16303 continue;
16305 pc = &OMP_CLAUSE_CHAIN (*pc);
16308 if (map_seen != 3)
16310 if (map_seen == 0)
16311 error_at (loc,
16312 "%<#pragma omp target exit data%> must contain at least one "
16313 "%<map%> clause");
16314 return NULL_TREE;
16317 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
16318 TREE_TYPE (stmt) = void_type_node;
16319 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
16320 SET_EXPR_LOCATION (stmt, loc);
16321 add_stmt (stmt);
16322 return stmt;
16325 /* OpenMP 4.0:
16326 # pragma omp target target-clause[optseq] new-line
16327 structured-block */
16329 #define OMP_TARGET_CLAUSE_MASK \
16330 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
16335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
16338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
16340 static bool
16341 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
16343 location_t loc = c_parser_peek_token (parser)->location;
16344 c_parser_consume_pragma (parser);
16345 tree *pc = NULL, stmt, block;
16347 if (context != pragma_stmt && context != pragma_compound)
16349 c_parser_error (parser, "expected declaration specifiers");
16350 c_parser_skip_to_pragma_eol (parser);
16351 return false;
16354 if (c_parser_next_token_is (parser, CPP_NAME))
16356 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16357 enum tree_code ccode = ERROR_MARK;
16359 if (strcmp (p, "teams") == 0)
16360 ccode = OMP_TEAMS;
16361 else if (strcmp (p, "parallel") == 0)
16362 ccode = OMP_PARALLEL;
16363 else if (strcmp (p, "simd") == 0)
16364 ccode = OMP_SIMD;
16365 if (ccode != ERROR_MARK)
16367 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
16368 char p_name[sizeof ("#pragma omp target teams distribute "
16369 "parallel for simd")];
16371 c_parser_consume_token (parser);
16372 strcpy (p_name, "#pragma omp target");
16373 if (!flag_openmp) /* flag_openmp_simd */
16375 tree stmt;
16376 switch (ccode)
16378 case OMP_TEAMS:
16379 stmt = c_parser_omp_teams (loc, parser, p_name,
16380 OMP_TARGET_CLAUSE_MASK,
16381 cclauses, if_p);
16382 break;
16383 case OMP_PARALLEL:
16384 stmt = c_parser_omp_parallel (loc, parser, p_name,
16385 OMP_TARGET_CLAUSE_MASK,
16386 cclauses, if_p);
16387 break;
16388 case OMP_SIMD:
16389 stmt = c_parser_omp_simd (loc, parser, p_name,
16390 OMP_TARGET_CLAUSE_MASK,
16391 cclauses, if_p);
16392 break;
16393 default:
16394 gcc_unreachable ();
16396 return stmt != NULL_TREE;
16398 keep_next_level ();
16399 tree block = c_begin_compound_stmt (true), ret;
16400 switch (ccode)
16402 case OMP_TEAMS:
16403 ret = c_parser_omp_teams (loc, parser, p_name,
16404 OMP_TARGET_CLAUSE_MASK, cclauses,
16405 if_p);
16406 break;
16407 case OMP_PARALLEL:
16408 ret = c_parser_omp_parallel (loc, parser, p_name,
16409 OMP_TARGET_CLAUSE_MASK, cclauses,
16410 if_p);
16411 break;
16412 case OMP_SIMD:
16413 ret = c_parser_omp_simd (loc, parser, p_name,
16414 OMP_TARGET_CLAUSE_MASK, cclauses,
16415 if_p);
16416 break;
16417 default:
16418 gcc_unreachable ();
16420 block = c_end_compound_stmt (loc, block, true);
16421 if (ret == NULL_TREE)
16422 return false;
16423 if (ccode == OMP_TEAMS)
16425 /* For combined target teams, ensure the num_teams and
16426 thread_limit clause expressions are evaluated on the host,
16427 before entering the target construct. */
16428 tree c;
16429 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16430 c; c = OMP_CLAUSE_CHAIN (c))
16431 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16432 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16433 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16435 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16436 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
16437 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
16438 expr, NULL_TREE, NULL_TREE);
16439 add_stmt (expr);
16440 OMP_CLAUSE_OPERAND (c, 0) = expr;
16441 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16442 OMP_CLAUSE_FIRSTPRIVATE);
16443 OMP_CLAUSE_DECL (tc) = tmp;
16444 OMP_CLAUSE_CHAIN (tc)
16445 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16446 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
16449 tree stmt = make_node (OMP_TARGET);
16450 TREE_TYPE (stmt) = void_type_node;
16451 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
16452 OMP_TARGET_BODY (stmt) = block;
16453 OMP_TARGET_COMBINED (stmt) = 1;
16454 add_stmt (stmt);
16455 pc = &OMP_TARGET_CLAUSES (stmt);
16456 goto check_clauses;
16458 else if (!flag_openmp) /* flag_openmp_simd */
16460 c_parser_skip_to_pragma_eol (parser, false);
16461 return false;
16463 else if (strcmp (p, "data") == 0)
16465 c_parser_consume_token (parser);
16466 c_parser_omp_target_data (loc, parser, if_p);
16467 return true;
16469 else if (strcmp (p, "enter") == 0)
16471 c_parser_consume_token (parser);
16472 c_parser_omp_target_enter_data (loc, parser, context);
16473 return false;
16475 else if (strcmp (p, "exit") == 0)
16477 c_parser_consume_token (parser);
16478 c_parser_omp_target_exit_data (loc, parser, context);
16479 return false;
16481 else if (strcmp (p, "update") == 0)
16483 c_parser_consume_token (parser);
16484 return c_parser_omp_target_update (loc, parser, context);
16487 if (!flag_openmp) /* flag_openmp_simd */
16489 c_parser_skip_to_pragma_eol (parser, false);
16490 return false;
16493 stmt = make_node (OMP_TARGET);
16494 TREE_TYPE (stmt) = void_type_node;
16496 OMP_TARGET_CLAUSES (stmt)
16497 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
16498 "#pragma omp target");
16499 pc = &OMP_TARGET_CLAUSES (stmt);
16500 keep_next_level ();
16501 block = c_begin_compound_stmt (true);
16502 add_stmt (c_parser_omp_structured_block (parser, if_p));
16503 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16505 SET_EXPR_LOCATION (stmt, loc);
16506 add_stmt (stmt);
16508 check_clauses:
16509 while (*pc)
16511 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16512 switch (OMP_CLAUSE_MAP_KIND (*pc))
16514 case GOMP_MAP_TO:
16515 case GOMP_MAP_ALWAYS_TO:
16516 case GOMP_MAP_FROM:
16517 case GOMP_MAP_ALWAYS_FROM:
16518 case GOMP_MAP_TOFROM:
16519 case GOMP_MAP_ALWAYS_TOFROM:
16520 case GOMP_MAP_ALLOC:
16521 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16522 case GOMP_MAP_ALWAYS_POINTER:
16523 break;
16524 default:
16525 error_at (OMP_CLAUSE_LOCATION (*pc),
16526 "%<#pragma omp target%> with map-type other "
16527 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16528 "on %<map%> clause");
16529 *pc = OMP_CLAUSE_CHAIN (*pc);
16530 continue;
16532 pc = &OMP_CLAUSE_CHAIN (*pc);
16534 return true;
16537 /* OpenMP 4.0:
16538 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
16540 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
16541 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
16545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
16546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
16548 static void
16549 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
16551 auto_vec<c_token> clauses;
16552 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16554 c_token *token = c_parser_peek_token (parser);
16555 if (token->type == CPP_EOF)
16557 c_parser_skip_to_pragma_eol (parser);
16558 return;
16560 clauses.safe_push (*token);
16561 c_parser_consume_token (parser);
16563 clauses.safe_push (*c_parser_peek_token (parser));
16564 c_parser_skip_to_pragma_eol (parser);
16566 while (c_parser_next_token_is (parser, CPP_PRAGMA))
16568 if (c_parser_peek_token (parser)->pragma_kind
16569 != PRAGMA_OMP_DECLARE
16570 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
16571 || strcmp (IDENTIFIER_POINTER
16572 (c_parser_peek_2nd_token (parser)->value),
16573 "simd") != 0)
16575 c_parser_error (parser,
16576 "%<#pragma omp declare simd%> must be followed by "
16577 "function declaration or definition or another "
16578 "%<#pragma omp declare simd%>");
16579 return;
16581 c_parser_consume_pragma (parser);
16582 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
16584 c_token *token = c_parser_peek_token (parser);
16585 if (token->type == CPP_EOF)
16587 c_parser_skip_to_pragma_eol (parser);
16588 return;
16590 clauses.safe_push (*token);
16591 c_parser_consume_token (parser);
16593 clauses.safe_push (*c_parser_peek_token (parser));
16594 c_parser_skip_to_pragma_eol (parser);
16597 /* Make sure nothing tries to read past the end of the tokens. */
16598 c_token eof_token;
16599 memset (&eof_token, 0, sizeof (eof_token));
16600 eof_token.type = CPP_EOF;
16601 clauses.safe_push (eof_token);
16602 clauses.safe_push (eof_token);
16604 switch (context)
16606 case pragma_external:
16607 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16608 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16610 int ext = disable_extension_diagnostics ();
16612 c_parser_consume_token (parser);
16613 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16614 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16615 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16616 NULL, clauses);
16617 restore_extension_diagnostics (ext);
16619 else
16620 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
16621 NULL, clauses);
16622 break;
16623 case pragma_struct:
16624 case pragma_param:
16625 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16626 "function declaration or definition");
16627 break;
16628 case pragma_compound:
16629 case pragma_stmt:
16630 if (c_parser_next_token_is (parser, CPP_KEYWORD)
16631 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
16633 int ext = disable_extension_diagnostics ();
16635 c_parser_consume_token (parser);
16636 while (c_parser_next_token_is (parser, CPP_KEYWORD)
16637 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
16638 if (c_parser_next_tokens_start_declaration (parser))
16640 c_parser_declaration_or_fndef (parser, true, true, true, true,
16641 true, NULL, clauses);
16642 restore_extension_diagnostics (ext);
16643 break;
16645 restore_extension_diagnostics (ext);
16647 else if (c_parser_next_tokens_start_declaration (parser))
16649 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
16650 NULL, clauses);
16651 break;
16653 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
16654 "function declaration or definition");
16655 break;
16656 default:
16657 gcc_unreachable ();
16661 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
16662 and put that into "omp declare simd" attribute. */
16664 static void
16665 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
16666 vec<c_token> clauses)
16668 if (flag_cilkplus
16669 && (clauses.exists ()
16670 || lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)))
16671 && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16673 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
16674 "used in the same function marked as a Cilk Plus SIMD-enabled "
16675 "function");
16676 vec_free (parser->cilk_simd_fn_tokens);
16677 return;
16680 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
16681 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
16682 has already processed the tokens. */
16683 if (clauses.exists () && clauses[0].type == CPP_EOF)
16684 return;
16685 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
16687 error ("%<#pragma omp declare simd%> not immediately followed by "
16688 "a function declaration or definition");
16689 clauses[0].type = CPP_EOF;
16690 return;
16692 if (clauses.exists () && clauses[0].type != CPP_NAME)
16694 error_at (DECL_SOURCE_LOCATION (fndecl),
16695 "%<#pragma omp declare simd%> not immediately followed by "
16696 "a single function declaration or definition");
16697 clauses[0].type = CPP_EOF;
16698 return;
16701 if (parms == NULL_TREE)
16702 parms = DECL_ARGUMENTS (fndecl);
16704 unsigned int tokens_avail = parser->tokens_avail;
16705 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
16706 bool is_cilkplus_cilk_simd_fn = false;
16708 if (flag_cilkplus && !vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16710 parser->tokens = parser->cilk_simd_fn_tokens->address ();
16711 parser->tokens_avail = vec_safe_length (parser->cilk_simd_fn_tokens);
16712 is_cilkplus_cilk_simd_fn = true;
16714 if (lookup_attribute ("simd", DECL_ATTRIBUTES (fndecl)) != NULL)
16716 error_at (DECL_SOURCE_LOCATION (fndecl),
16717 "%<__simd__%> attribute cannot be used in the same "
16718 "function marked as a Cilk Plus SIMD-enabled function");
16719 vec_free (parser->cilk_simd_fn_tokens);
16720 return;
16724 else
16726 parser->tokens = clauses.address ();
16727 parser->tokens_avail = clauses.length ();
16730 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
16731 while (parser->tokens_avail > 3)
16733 c_token *token = c_parser_peek_token (parser);
16734 if (!is_cilkplus_cilk_simd_fn)
16735 gcc_assert (token->type == CPP_NAME
16736 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
16737 else
16738 gcc_assert (token->type == CPP_NAME
16739 && is_cilkplus_vector_p (token->value));
16740 c_parser_consume_token (parser);
16741 parser->in_pragma = true;
16743 tree c = NULL_TREE;
16744 if (is_cilkplus_cilk_simd_fn)
16745 c = c_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
16746 "SIMD-enabled functions attribute");
16747 else
16748 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
16749 "#pragma omp declare simd");
16750 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
16751 if (c != NULL_TREE)
16752 c = tree_cons (NULL_TREE, c, NULL_TREE);
16753 if (is_cilkplus_cilk_simd_fn)
16755 tree k = build_tree_list (get_identifier ("cilk simd function"),
16756 NULL_TREE);
16757 TREE_CHAIN (k) = DECL_ATTRIBUTES (fndecl);
16758 DECL_ATTRIBUTES (fndecl) = k;
16760 c = build_tree_list (get_identifier ("omp declare simd"), c);
16761 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
16762 DECL_ATTRIBUTES (fndecl) = c;
16765 parser->tokens = &parser->tokens_buf[0];
16766 parser->tokens_avail = tokens_avail;
16767 if (clauses.exists ())
16768 clauses[0].type = CPP_PRAGMA;
16770 if (!vec_safe_is_empty (parser->cilk_simd_fn_tokens))
16771 vec_free (parser->cilk_simd_fn_tokens);
16775 /* OpenMP 4.0:
16776 # pragma omp declare target new-line
16777 declarations and definitions
16778 # pragma omp end declare target new-line
16780 OpenMP 4.5:
16781 # pragma omp declare target ( extended-list ) new-line
16783 # pragma omp declare target declare-target-clauses[seq] new-line */
16785 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
16786 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
16789 static void
16790 c_parser_omp_declare_target (c_parser *parser)
16792 location_t loc = c_parser_peek_token (parser)->location;
16793 tree clauses = NULL_TREE;
16794 if (c_parser_next_token_is (parser, CPP_NAME))
16795 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
16796 "#pragma omp declare target");
16797 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
16799 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
16800 clauses);
16801 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
16802 c_parser_skip_to_pragma_eol (parser);
16804 else
16806 c_parser_skip_to_pragma_eol (parser);
16807 current_omp_declare_target_attribute++;
16808 return;
16810 if (current_omp_declare_target_attribute)
16811 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
16812 "%<#pragma omp declare target%> without clauses and "
16813 "%<#pragma omp end declare target%>");
16814 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
16816 tree t = OMP_CLAUSE_DECL (c), id;
16817 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
16818 tree at2 = lookup_attribute ("omp declare target link",
16819 DECL_ATTRIBUTES (t));
16820 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
16822 id = get_identifier ("omp declare target link");
16823 std::swap (at1, at2);
16825 else
16826 id = get_identifier ("omp declare target");
16827 if (at2)
16829 error_at (OMP_CLAUSE_LOCATION (c),
16830 "%qD specified both in declare target %<link%> and %<to%>"
16831 " clauses", t);
16832 continue;
16834 if (!at1)
16836 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
16837 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
16838 continue;
16840 symtab_node *node = symtab_node::get (t);
16841 if (node != NULL)
16843 node->offloadable = 1;
16844 if (ENABLE_OFFLOADING)
16846 g->have_offload = true;
16847 if (is_a <varpool_node *> (node))
16848 vec_safe_push (offload_vars, t);
16855 static void
16856 c_parser_omp_end_declare_target (c_parser *parser)
16858 location_t loc = c_parser_peek_token (parser)->location;
16859 c_parser_consume_pragma (parser);
16860 if (c_parser_next_token_is (parser, CPP_NAME)
16861 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16862 "declare") == 0)
16864 c_parser_consume_token (parser);
16865 if (c_parser_next_token_is (parser, CPP_NAME)
16866 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
16867 "target") == 0)
16868 c_parser_consume_token (parser);
16869 else
16871 c_parser_error (parser, "expected %<target%>");
16872 c_parser_skip_to_pragma_eol (parser);
16873 return;
16876 else
16878 c_parser_error (parser, "expected %<declare%>");
16879 c_parser_skip_to_pragma_eol (parser);
16880 return;
16882 c_parser_skip_to_pragma_eol (parser);
16883 if (!current_omp_declare_target_attribute)
16884 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
16885 "%<#pragma omp declare target%>");
16886 else
16887 current_omp_declare_target_attribute--;
16891 /* OpenMP 4.0
16892 #pragma omp declare reduction (reduction-id : typename-list : expression) \
16893 initializer-clause[opt] new-line
16895 initializer-clause:
16896 initializer (omp_priv = initializer)
16897 initializer (function-name (argument-list)) */
16899 static void
16900 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
16902 unsigned int tokens_avail = 0, i;
16903 vec<tree> types = vNULL;
16904 vec<c_token> clauses = vNULL;
16905 enum tree_code reduc_code = ERROR_MARK;
16906 tree reduc_id = NULL_TREE;
16907 tree type;
16908 location_t rloc = c_parser_peek_token (parser)->location;
16910 if (context == pragma_struct || context == pragma_param)
16912 error ("%<#pragma omp declare reduction%> not at file or block scope");
16913 goto fail;
16916 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
16917 goto fail;
16919 switch (c_parser_peek_token (parser)->type)
16921 case CPP_PLUS:
16922 reduc_code = PLUS_EXPR;
16923 break;
16924 case CPP_MULT:
16925 reduc_code = MULT_EXPR;
16926 break;
16927 case CPP_MINUS:
16928 reduc_code = MINUS_EXPR;
16929 break;
16930 case CPP_AND:
16931 reduc_code = BIT_AND_EXPR;
16932 break;
16933 case CPP_XOR:
16934 reduc_code = BIT_XOR_EXPR;
16935 break;
16936 case CPP_OR:
16937 reduc_code = BIT_IOR_EXPR;
16938 break;
16939 case CPP_AND_AND:
16940 reduc_code = TRUTH_ANDIF_EXPR;
16941 break;
16942 case CPP_OR_OR:
16943 reduc_code = TRUTH_ORIF_EXPR;
16944 break;
16945 case CPP_NAME:
16946 const char *p;
16947 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16948 if (strcmp (p, "min") == 0)
16950 reduc_code = MIN_EXPR;
16951 break;
16953 if (strcmp (p, "max") == 0)
16955 reduc_code = MAX_EXPR;
16956 break;
16958 reduc_id = c_parser_peek_token (parser)->value;
16959 break;
16960 default:
16961 c_parser_error (parser,
16962 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
16963 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
16964 goto fail;
16967 tree orig_reduc_id, reduc_decl;
16968 orig_reduc_id = reduc_id;
16969 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
16970 reduc_decl = c_omp_reduction_decl (reduc_id);
16971 c_parser_consume_token (parser);
16973 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
16974 goto fail;
16976 while (true)
16978 location_t loc = c_parser_peek_token (parser)->location;
16979 struct c_type_name *ctype = c_parser_type_name (parser);
16980 if (ctype != NULL)
16982 type = groktypename (ctype, NULL, NULL);
16983 if (type == error_mark_node)
16985 else if ((INTEGRAL_TYPE_P (type)
16986 || TREE_CODE (type) == REAL_TYPE
16987 || TREE_CODE (type) == COMPLEX_TYPE)
16988 && orig_reduc_id == NULL_TREE)
16989 error_at (loc, "predeclared arithmetic type in "
16990 "%<#pragma omp declare reduction%>");
16991 else if (TREE_CODE (type) == FUNCTION_TYPE
16992 || TREE_CODE (type) == ARRAY_TYPE)
16993 error_at (loc, "function or array type in "
16994 "%<#pragma omp declare reduction%>");
16995 else if (TYPE_ATOMIC (type))
16996 error_at (loc, "%<_Atomic%> qualified type in "
16997 "%<#pragma omp declare reduction%>");
16998 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
16999 error_at (loc, "const, volatile or restrict qualified type in "
17000 "%<#pragma omp declare reduction%>");
17001 else
17003 tree t;
17004 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17005 if (comptypes (TREE_PURPOSE (t), type))
17007 error_at (loc, "redeclaration of %qs "
17008 "%<#pragma omp declare reduction%> for "
17009 "type %qT",
17010 IDENTIFIER_POINTER (reduc_id)
17011 + sizeof ("omp declare reduction ") - 1,
17012 type);
17013 location_t ploc
17014 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17015 0));
17016 error_at (ploc, "previous %<#pragma omp declare "
17017 "reduction%>");
17018 break;
17020 if (t == NULL_TREE)
17021 types.safe_push (type);
17023 if (c_parser_next_token_is (parser, CPP_COMMA))
17024 c_parser_consume_token (parser);
17025 else
17026 break;
17028 else
17029 break;
17032 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17033 || types.is_empty ())
17035 fail:
17036 clauses.release ();
17037 types.release ();
17038 while (true)
17040 c_token *token = c_parser_peek_token (parser);
17041 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17042 break;
17043 c_parser_consume_token (parser);
17045 c_parser_skip_to_pragma_eol (parser);
17046 return;
17049 if (types.length () > 1)
17051 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17053 c_token *token = c_parser_peek_token (parser);
17054 if (token->type == CPP_EOF)
17055 goto fail;
17056 clauses.safe_push (*token);
17057 c_parser_consume_token (parser);
17059 clauses.safe_push (*c_parser_peek_token (parser));
17060 c_parser_skip_to_pragma_eol (parser);
17062 /* Make sure nothing tries to read past the end of the tokens. */
17063 c_token eof_token;
17064 memset (&eof_token, 0, sizeof (eof_token));
17065 eof_token.type = CPP_EOF;
17066 clauses.safe_push (eof_token);
17067 clauses.safe_push (eof_token);
17070 int errs = errorcount;
17071 FOR_EACH_VEC_ELT (types, i, type)
17073 tokens_avail = parser->tokens_avail;
17074 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17075 if (!clauses.is_empty ())
17077 parser->tokens = clauses.address ();
17078 parser->tokens_avail = clauses.length ();
17079 parser->in_pragma = true;
17082 bool nested = current_function_decl != NULL_TREE;
17083 if (nested)
17084 c_push_function_context ();
17085 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17086 reduc_id, default_function_type);
17087 current_function_decl = fndecl;
17088 allocate_struct_function (fndecl, true);
17089 push_scope ();
17090 tree stmt = push_stmt_list ();
17091 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17092 warn about these. */
17093 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17094 get_identifier ("omp_out"), type);
17095 DECL_ARTIFICIAL (omp_out) = 1;
17096 DECL_CONTEXT (omp_out) = fndecl;
17097 pushdecl (omp_out);
17098 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17099 get_identifier ("omp_in"), type);
17100 DECL_ARTIFICIAL (omp_in) = 1;
17101 DECL_CONTEXT (omp_in) = fndecl;
17102 pushdecl (omp_in);
17103 struct c_expr combiner = c_parser_expression (parser);
17104 struct c_expr initializer;
17105 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17106 bool bad = false;
17107 initializer.value = error_mark_node;
17108 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17109 bad = true;
17110 else if (c_parser_next_token_is (parser, CPP_NAME)
17111 && strcmp (IDENTIFIER_POINTER
17112 (c_parser_peek_token (parser)->value),
17113 "initializer") == 0)
17115 c_parser_consume_token (parser);
17116 pop_scope ();
17117 push_scope ();
17118 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17119 get_identifier ("omp_priv"), type);
17120 DECL_ARTIFICIAL (omp_priv) = 1;
17121 DECL_INITIAL (omp_priv) = error_mark_node;
17122 DECL_CONTEXT (omp_priv) = fndecl;
17123 pushdecl (omp_priv);
17124 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17125 get_identifier ("omp_orig"), type);
17126 DECL_ARTIFICIAL (omp_orig) = 1;
17127 DECL_CONTEXT (omp_orig) = fndecl;
17128 pushdecl (omp_orig);
17129 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17130 bad = true;
17131 else if (!c_parser_next_token_is (parser, CPP_NAME))
17133 c_parser_error (parser, "expected %<omp_priv%> or "
17134 "function-name");
17135 bad = true;
17137 else if (strcmp (IDENTIFIER_POINTER
17138 (c_parser_peek_token (parser)->value),
17139 "omp_priv") != 0)
17141 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17142 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17144 c_parser_error (parser, "expected function-name %<(%>");
17145 bad = true;
17147 else
17148 initializer = c_parser_postfix_expression (parser);
17149 if (initializer.value
17150 && TREE_CODE (initializer.value) == CALL_EXPR)
17152 int j;
17153 tree c = initializer.value;
17154 for (j = 0; j < call_expr_nargs (c); j++)
17156 tree a = CALL_EXPR_ARG (c, j);
17157 STRIP_NOPS (a);
17158 if (TREE_CODE (a) == ADDR_EXPR
17159 && TREE_OPERAND (a, 0) == omp_priv)
17160 break;
17162 if (j == call_expr_nargs (c))
17163 error ("one of the initializer call arguments should be "
17164 "%<&omp_priv%>");
17167 else
17169 c_parser_consume_token (parser);
17170 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17171 bad = true;
17172 else
17174 tree st = push_stmt_list ();
17175 location_t loc = c_parser_peek_token (parser)->location;
17176 rich_location richloc (line_table, loc);
17177 start_init (omp_priv, NULL_TREE, 0, &richloc);
17178 struct c_expr init = c_parser_initializer (parser);
17179 finish_init ();
17180 finish_decl (omp_priv, loc, init.value,
17181 init.original_type, NULL_TREE);
17182 pop_stmt_list (st);
17185 if (!bad
17186 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17187 bad = true;
17190 if (!bad)
17192 c_parser_skip_to_pragma_eol (parser);
17194 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17195 DECL_INITIAL (reduc_decl));
17196 DECL_INITIAL (reduc_decl) = t;
17197 DECL_SOURCE_LOCATION (omp_out) = rloc;
17198 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17199 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17200 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17201 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17202 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17203 if (omp_priv)
17205 DECL_SOURCE_LOCATION (omp_priv) = rloc;
17206 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
17207 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
17208 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
17209 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
17210 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17211 walk_tree (&DECL_INITIAL (omp_priv),
17212 c_check_omp_declare_reduction_r,
17213 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
17217 pop_stmt_list (stmt);
17218 pop_scope ();
17219 if (cfun->language != NULL)
17221 ggc_free (cfun->language);
17222 cfun->language = NULL;
17224 set_cfun (NULL);
17225 current_function_decl = NULL_TREE;
17226 if (nested)
17227 c_pop_function_context ();
17229 if (!clauses.is_empty ())
17231 parser->tokens = &parser->tokens_buf[0];
17232 parser->tokens_avail = tokens_avail;
17234 if (bad)
17235 goto fail;
17236 if (errs != errorcount)
17237 break;
17240 clauses.release ();
17241 types.release ();
17245 /* OpenMP 4.0
17246 #pragma omp declare simd declare-simd-clauses[optseq] new-line
17247 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17248 initializer-clause[opt] new-line
17249 #pragma omp declare target new-line */
17251 static void
17252 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
17254 c_parser_consume_pragma (parser);
17255 if (c_parser_next_token_is (parser, CPP_NAME))
17257 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17258 if (strcmp (p, "simd") == 0)
17260 /* c_parser_consume_token (parser); done in
17261 c_parser_omp_declare_simd. */
17262 c_parser_omp_declare_simd (parser, context);
17263 return;
17265 if (strcmp (p, "reduction") == 0)
17267 c_parser_consume_token (parser);
17268 c_parser_omp_declare_reduction (parser, context);
17269 return;
17271 if (!flag_openmp) /* flag_openmp_simd */
17273 c_parser_skip_to_pragma_eol (parser, false);
17274 return;
17276 if (strcmp (p, "target") == 0)
17278 c_parser_consume_token (parser);
17279 c_parser_omp_declare_target (parser);
17280 return;
17284 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
17285 "or %<target%>");
17286 c_parser_skip_to_pragma_eol (parser);
17289 /* OpenMP 4.5:
17290 #pragma omp taskloop taskloop-clause[optseq] new-line
17291 for-loop
17293 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
17294 for-loop */
17296 #define OMP_TASKLOOP_CLAUSE_MASK \
17297 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
17298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
17301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
17302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
17303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
17304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
17305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
17306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
17308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
17309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
17310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
17312 static tree
17313 c_parser_omp_taskloop (location_t loc, c_parser *parser,
17314 char *p_name, omp_clause_mask mask, tree *cclauses,
17315 bool *if_p)
17317 tree clauses, block, ret;
17319 strcat (p_name, " taskloop");
17320 mask |= OMP_TASKLOOP_CLAUSE_MASK;
17322 if (c_parser_next_token_is (parser, CPP_NAME))
17324 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17326 if (strcmp (p, "simd") == 0)
17328 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
17329 if (cclauses == NULL)
17330 cclauses = cclauses_buf;
17331 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
17332 c_parser_consume_token (parser);
17333 if (!flag_openmp) /* flag_openmp_simd */
17334 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
17335 if_p);
17336 block = c_begin_compound_stmt (true);
17337 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
17338 block = c_end_compound_stmt (loc, block, true);
17339 if (ret == NULL)
17340 return ret;
17341 ret = make_node (OMP_TASKLOOP);
17342 TREE_TYPE (ret) = void_type_node;
17343 OMP_FOR_BODY (ret) = block;
17344 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17345 SET_EXPR_LOCATION (ret, loc);
17346 add_stmt (ret);
17347 return ret;
17350 if (!flag_openmp) /* flag_openmp_simd */
17352 c_parser_skip_to_pragma_eol (parser, false);
17353 return NULL_TREE;
17356 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
17357 if (cclauses)
17359 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
17360 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
17363 block = c_begin_compound_stmt (true);
17364 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
17365 block = c_end_compound_stmt (loc, block, true);
17366 add_stmt (block);
17368 return ret;
17371 /* Main entry point to parsing most OpenMP pragmas. */
17373 static void
17374 c_parser_omp_construct (c_parser *parser, bool *if_p)
17376 enum pragma_kind p_kind;
17377 location_t loc;
17378 tree stmt;
17379 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
17380 omp_clause_mask mask (0);
17382 loc = c_parser_peek_token (parser)->location;
17383 p_kind = c_parser_peek_token (parser)->pragma_kind;
17384 c_parser_consume_pragma (parser);
17386 switch (p_kind)
17388 case PRAGMA_OACC_ATOMIC:
17389 c_parser_omp_atomic (loc, parser);
17390 return;
17391 case PRAGMA_OACC_CACHE:
17392 strcpy (p_name, "#pragma acc");
17393 stmt = c_parser_oacc_cache (loc, parser);
17394 break;
17395 case PRAGMA_OACC_DATA:
17396 stmt = c_parser_oacc_data (loc, parser, if_p);
17397 break;
17398 case PRAGMA_OACC_HOST_DATA:
17399 stmt = c_parser_oacc_host_data (loc, parser, if_p);
17400 break;
17401 case PRAGMA_OACC_KERNELS:
17402 case PRAGMA_OACC_PARALLEL:
17403 strcpy (p_name, "#pragma acc");
17404 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
17405 if_p);
17406 break;
17407 case PRAGMA_OACC_LOOP:
17408 strcpy (p_name, "#pragma acc");
17409 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
17410 break;
17411 case PRAGMA_OACC_WAIT:
17412 strcpy (p_name, "#pragma wait");
17413 stmt = c_parser_oacc_wait (loc, parser, p_name);
17414 break;
17415 case PRAGMA_OMP_ATOMIC:
17416 c_parser_omp_atomic (loc, parser);
17417 return;
17418 case PRAGMA_OMP_CRITICAL:
17419 stmt = c_parser_omp_critical (loc, parser, if_p);
17420 break;
17421 case PRAGMA_OMP_DISTRIBUTE:
17422 strcpy (p_name, "#pragma omp");
17423 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
17424 break;
17425 case PRAGMA_OMP_FOR:
17426 strcpy (p_name, "#pragma omp");
17427 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
17428 break;
17429 case PRAGMA_OMP_MASTER:
17430 stmt = c_parser_omp_master (loc, parser, if_p);
17431 break;
17432 case PRAGMA_OMP_PARALLEL:
17433 strcpy (p_name, "#pragma omp");
17434 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
17435 break;
17436 case PRAGMA_OMP_SECTIONS:
17437 strcpy (p_name, "#pragma omp");
17438 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
17439 break;
17440 case PRAGMA_OMP_SIMD:
17441 strcpy (p_name, "#pragma omp");
17442 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
17443 break;
17444 case PRAGMA_OMP_SINGLE:
17445 stmt = c_parser_omp_single (loc, parser, if_p);
17446 break;
17447 case PRAGMA_OMP_TASK:
17448 stmt = c_parser_omp_task (loc, parser, if_p);
17449 break;
17450 case PRAGMA_OMP_TASKGROUP:
17451 stmt = c_parser_omp_taskgroup (parser, if_p);
17452 break;
17453 case PRAGMA_OMP_TASKLOOP:
17454 strcpy (p_name, "#pragma omp");
17455 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
17456 break;
17457 case PRAGMA_OMP_TEAMS:
17458 strcpy (p_name, "#pragma omp");
17459 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
17460 break;
17461 default:
17462 gcc_unreachable ();
17465 if (stmt)
17466 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
17470 /* OpenMP 2.5:
17471 # pragma omp threadprivate (variable-list) */
17473 static void
17474 c_parser_omp_threadprivate (c_parser *parser)
17476 tree vars, t;
17477 location_t loc;
17479 c_parser_consume_pragma (parser);
17480 loc = c_parser_peek_token (parser)->location;
17481 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
17483 /* Mark every variable in VARS to be assigned thread local storage. */
17484 for (t = vars; t; t = TREE_CHAIN (t))
17486 tree v = TREE_PURPOSE (t);
17488 /* FIXME diagnostics: Ideally we should keep individual
17489 locations for all the variables in the var list to make the
17490 following errors more precise. Perhaps
17491 c_parser_omp_var_list_parens() should construct a list of
17492 locations to go along with the var list. */
17494 /* If V had already been marked threadprivate, it doesn't matter
17495 whether it had been used prior to this point. */
17496 if (!VAR_P (v))
17497 error_at (loc, "%qD is not a variable", v);
17498 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
17499 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
17500 else if (! is_global_var (v))
17501 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
17502 else if (TREE_TYPE (v) == error_mark_node)
17504 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
17505 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
17506 else
17508 if (! DECL_THREAD_LOCAL_P (v))
17510 set_decl_tls_model (v, decl_default_tls_model (v));
17511 /* If rtl has been already set for this var, call
17512 make_decl_rtl once again, so that encode_section_info
17513 has a chance to look at the new decl flags. */
17514 if (DECL_RTL_SET_P (v))
17515 make_decl_rtl (v);
17517 C_DECL_THREADPRIVATE_P (v) = 1;
17521 c_parser_skip_to_pragma_eol (parser);
17524 /* Cilk Plus <#pragma simd> parsing routines. */
17526 /* Helper function for c_parser_pragma. Perform some sanity checking
17527 for <#pragma simd> constructs. Returns FALSE if there was a
17528 problem. */
17530 static bool
17531 c_parser_cilk_verify_simd (c_parser *parser,
17532 enum pragma_context context)
17534 if (!flag_cilkplus)
17536 warning (0, "pragma simd ignored because -fcilkplus is not enabled");
17537 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17538 return false;
17540 if (context == pragma_external)
17542 c_parser_error (parser,"pragma simd must be inside a function");
17543 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
17544 return false;
17546 return true;
17549 /* Cilk Plus:
17550 This function is shared by SIMD-enabled functions and #pragma simd.
17551 If IS_SIMD_FN is true then it is parsing a SIMD-enabled function and
17552 CLAUSES is unused. The main purpose of this function is to parse a
17553 vectorlength attribute or clause and check for parse errors.
17554 When IS_SIMD_FN is true then the function is merely caching the tokens
17555 in PARSER->CILK_SIMD_FN_TOKENS. If errors are found then the token
17556 cache is cleared since there is no reason to continue.
17557 Syntax:
17558 vectorlength ( constant-expression ) */
17560 static tree
17561 c_parser_cilk_clause_vectorlength (c_parser *parser, tree clauses,
17562 bool is_simd_fn)
17564 if (is_simd_fn)
17565 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength");
17566 else
17567 /* The vectorlength clause behaves exactly like OpenMP's safelen
17568 clause. Represent it in OpenMP terms. */
17569 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength");
17571 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17572 return clauses;
17574 location_t loc = c_parser_peek_token (parser)->location;
17575 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17576 expr = c_fully_fold (expr, false, NULL);
17578 /* If expr is an error_mark_node then the above function would have
17579 emitted an error. No reason to do it twice. */
17580 if (expr == error_mark_node)
17582 else if (!TREE_TYPE (expr)
17583 || !TREE_CONSTANT (expr)
17584 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
17586 error_at (loc, "vectorlength must be an integer constant");
17587 else if (wi::exact_log2 (expr) == -1)
17588 error_at (loc, "vectorlength must be a power of 2");
17589 else
17591 if (is_simd_fn)
17593 tree u = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
17594 OMP_CLAUSE_SIMDLEN_EXPR (u) = expr;
17595 OMP_CLAUSE_CHAIN (u) = clauses;
17596 clauses = u;
17598 else
17600 tree u = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
17601 OMP_CLAUSE_SAFELEN_EXPR (u) = expr;
17602 OMP_CLAUSE_CHAIN (u) = clauses;
17603 clauses = u;
17607 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17609 return clauses;
17612 /* Cilk Plus:
17613 linear ( simd-linear-variable-list )
17615 simd-linear-variable-list:
17616 simd-linear-variable
17617 simd-linear-variable-list , simd-linear-variable
17619 simd-linear-variable:
17620 id-expression
17621 id-expression : simd-linear-step
17623 simd-linear-step:
17624 conditional-expression */
17626 static tree
17627 c_parser_cilk_clause_linear (c_parser *parser, tree clauses)
17629 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17630 return clauses;
17632 location_t loc = c_parser_peek_token (parser)->location;
17634 if (c_parser_next_token_is_not (parser, CPP_NAME)
17635 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17636 c_parser_error (parser, "expected identifier");
17638 while (c_parser_next_token_is (parser, CPP_NAME)
17639 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
17641 tree var = lookup_name (c_parser_peek_token (parser)->value);
17643 if (var == NULL)
17645 undeclared_variable (c_parser_peek_token (parser)->location,
17646 c_parser_peek_token (parser)->value);
17647 c_parser_consume_token (parser);
17649 else if (var == error_mark_node)
17650 c_parser_consume_token (parser);
17651 else
17653 tree step = integer_one_node;
17655 /* Parse the linear step if present. */
17656 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
17658 c_parser_consume_token (parser);
17659 c_parser_consume_token (parser);
17661 tree expr = c_parser_expr_no_commas (parser, NULL).value;
17662 expr = c_fully_fold (expr, false, NULL);
17664 if (TREE_TYPE (expr)
17665 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
17666 && (TREE_CONSTANT (expr)
17667 || DECL_P (expr)))
17668 step = expr;
17669 else
17670 c_parser_error (parser,
17671 "step size must be an integer constant "
17672 "expression or an integer variable");
17674 else
17675 c_parser_consume_token (parser);
17677 /* Use OMP_CLAUSE_LINEAR, which has the same semantics. */
17678 tree u = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
17679 OMP_CLAUSE_DECL (u) = var;
17680 OMP_CLAUSE_LINEAR_STEP (u) = step;
17681 OMP_CLAUSE_CHAIN (u) = clauses;
17682 clauses = u;
17685 if (c_parser_next_token_is_not (parser, CPP_COMMA))
17686 break;
17688 c_parser_consume_token (parser);
17691 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>");
17693 return clauses;
17696 /* Returns the name of the next clause. If the clause is not
17697 recognized SIMD_OMP_CLAUSE_NONE is returned and the next token is
17698 not consumed. Otherwise, the appropriate pragma_simd_clause is
17699 returned and the token is consumed. */
17701 static pragma_omp_clause
17702 c_parser_cilk_clause_name (c_parser *parser)
17704 pragma_omp_clause result;
17705 c_token *token = c_parser_peek_token (parser);
17707 if (!token->value || token->type != CPP_NAME)
17708 return PRAGMA_CILK_CLAUSE_NONE;
17710 const char *p = IDENTIFIER_POINTER (token->value);
17712 if (!strcmp (p, "vectorlength"))
17713 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
17714 else if (!strcmp (p, "linear"))
17715 result = PRAGMA_CILK_CLAUSE_LINEAR;
17716 else if (!strcmp (p, "private"))
17717 result = PRAGMA_CILK_CLAUSE_PRIVATE;
17718 else if (!strcmp (p, "firstprivate"))
17719 result = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
17720 else if (!strcmp (p, "lastprivate"))
17721 result = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
17722 else if (!strcmp (p, "reduction"))
17723 result = PRAGMA_CILK_CLAUSE_REDUCTION;
17724 else
17725 return PRAGMA_CILK_CLAUSE_NONE;
17727 c_parser_consume_token (parser);
17728 return result;
17731 /* Parse all #<pragma simd> clauses. Return the list of clauses
17732 found. */
17734 static tree
17735 c_parser_cilk_all_clauses (c_parser *parser)
17737 tree clauses = NULL;
17739 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17741 pragma_omp_clause c_kind;
17743 c_kind = c_parser_cilk_clause_name (parser);
17745 switch (c_kind)
17747 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
17748 clauses = c_parser_cilk_clause_vectorlength (parser, clauses, false);
17749 break;
17750 case PRAGMA_CILK_CLAUSE_LINEAR:
17751 clauses = c_parser_cilk_clause_linear (parser, clauses);
17752 break;
17753 case PRAGMA_CILK_CLAUSE_PRIVATE:
17754 /* Use the OpenMP counterpart. */
17755 clauses = c_parser_omp_clause_private (parser, clauses);
17756 break;
17757 case PRAGMA_CILK_CLAUSE_FIRSTPRIVATE:
17758 /* Use the OpenMP counterpart. */
17759 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
17760 break;
17761 case PRAGMA_CILK_CLAUSE_LASTPRIVATE:
17762 /* Use the OpenMP counterpart. */
17763 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
17764 break;
17765 case PRAGMA_CILK_CLAUSE_REDUCTION:
17766 /* Use the OpenMP counterpart. */
17767 clauses = c_parser_omp_clause_reduction (parser, clauses);
17768 break;
17769 default:
17770 c_parser_error (parser, "expected %<#pragma simd%> clause");
17771 goto saw_error;
17775 saw_error:
17776 c_parser_skip_to_pragma_eol (parser);
17777 return c_finish_omp_clauses (clauses, C_ORT_CILK);
17780 /* This function helps parse the grainsize pragma for a _Cilk_for statement.
17781 Here is the correct syntax of this pragma:
17782 #pragma cilk grainsize = <EXP>
17785 static void
17786 c_parser_cilk_grainsize (c_parser *parser, bool *if_p)
17788 extern tree convert_to_integer (tree, tree);
17790 /* consume the 'grainsize' keyword. */
17791 c_parser_consume_pragma (parser);
17793 if (c_parser_require (parser, CPP_EQ, "expected %<=%>") != 0)
17795 struct c_expr g_expr = c_parser_binary_expression (parser, NULL, NULL);
17796 if (g_expr.value == error_mark_node)
17798 c_parser_skip_to_pragma_eol (parser);
17799 return;
17801 tree grain = convert_to_integer (long_integer_type_node,
17802 c_fully_fold (g_expr.value, false,
17803 NULL));
17804 c_parser_skip_to_pragma_eol (parser);
17805 c_token *token = c_parser_peek_token (parser);
17806 if (token && token->type == CPP_KEYWORD
17807 && token->keyword == RID_CILK_FOR)
17809 if (grain == NULL_TREE || grain == error_mark_node)
17810 grain = integer_zero_node;
17811 c_parser_cilk_for (parser, grain, if_p);
17813 else
17814 warning (0, "%<#pragma cilk grainsize%> is not followed by "
17815 "%<_Cilk_for%>");
17817 else
17818 c_parser_skip_to_pragma_eol (parser);
17821 /* Main entry point for parsing Cilk Plus <#pragma simd> for loops. */
17823 static void
17824 c_parser_cilk_simd (c_parser *parser, bool *if_p)
17826 tree clauses = c_parser_cilk_all_clauses (parser);
17827 tree block = c_begin_compound_stmt (true);
17828 location_t loc = c_parser_peek_token (parser)->location;
17829 c_parser_omp_for_loop (loc, parser, CILK_SIMD, clauses, NULL, if_p);
17830 block = c_end_compound_stmt (loc, block, true);
17831 add_stmt (block);
17834 /* Create an artificial decl with TYPE and emit initialization of it with
17835 INIT. */
17837 static tree
17838 c_get_temp_regvar (tree type, tree init)
17840 location_t loc = EXPR_LOCATION (init);
17841 tree decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
17842 DECL_ARTIFICIAL (decl) = 1;
17843 DECL_IGNORED_P (decl) = 1;
17844 pushdecl (decl);
17845 tree t = build2 (INIT_EXPR, type, decl, init);
17846 add_stmt (t);
17847 return decl;
17850 /* Main entry point for parsing Cilk Plus _Cilk_for loops.
17851 GRAIN is the grain value passed in through pragma or 0. */
17853 static void
17854 c_parser_cilk_for (c_parser *parser, tree grain, bool *if_p)
17856 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
17857 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
17858 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
17859 clauses = c_finish_omp_clauses (clauses, C_ORT_CILK);
17861 tree block = c_begin_compound_stmt (true);
17862 tree sb = push_stmt_list ();
17863 location_t loc = c_parser_peek_token (parser)->location;
17864 tree omp_for = c_parser_omp_for_loop (loc, parser, CILK_FOR, clauses, NULL,
17865 if_p);
17866 sb = pop_stmt_list (sb);
17868 if (omp_for)
17870 tree omp_par = make_node (OMP_PARALLEL);
17871 TREE_TYPE (omp_par) = void_type_node;
17872 OMP_PARALLEL_CLAUSES (omp_par) = NULL_TREE;
17873 tree bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
17874 TREE_SIDE_EFFECTS (bind) = 1;
17875 BIND_EXPR_BODY (bind) = sb;
17876 OMP_PARALLEL_BODY (omp_par) = bind;
17877 if (OMP_FOR_PRE_BODY (omp_for))
17879 add_stmt (OMP_FOR_PRE_BODY (omp_for));
17880 OMP_FOR_PRE_BODY (omp_for) = NULL_TREE;
17882 tree init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0);
17883 tree decl = TREE_OPERAND (init, 0);
17884 tree cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), 0);
17885 tree incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
17886 tree t = TREE_OPERAND (cond, 1), c, clauses = NULL_TREE;
17887 if (TREE_CODE (t) != INTEGER_CST)
17889 TREE_OPERAND (cond, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17890 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17891 OMP_CLAUSE_DECL (c) = TREE_OPERAND (cond, 1);
17892 OMP_CLAUSE_CHAIN (c) = clauses;
17893 clauses = c;
17895 if (TREE_CODE (incr) == MODIFY_EXPR)
17897 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17898 if (TREE_CODE (t) != INTEGER_CST)
17900 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
17901 = c_get_temp_regvar (TREE_TYPE (t), t);
17902 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17903 OMP_CLAUSE_DECL (c) = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
17904 OMP_CLAUSE_CHAIN (c) = clauses;
17905 clauses = c;
17908 t = TREE_OPERAND (init, 1);
17909 if (TREE_CODE (t) != INTEGER_CST)
17911 TREE_OPERAND (init, 1) = c_get_temp_regvar (TREE_TYPE (t), t);
17912 c = build_omp_clause (input_location, OMP_CLAUSE_FIRSTPRIVATE);
17913 OMP_CLAUSE_DECL (c) = TREE_OPERAND (init, 1);
17914 OMP_CLAUSE_CHAIN (c) = clauses;
17915 clauses = c;
17917 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
17918 OMP_CLAUSE_DECL (c) = decl;
17919 OMP_CLAUSE_CHAIN (c) = clauses;
17920 clauses = c;
17921 c = build_omp_clause (input_location, OMP_CLAUSE__CILK_FOR_COUNT_);
17922 OMP_CLAUSE_OPERAND (c, 0)
17923 = cilk_for_number_of_iterations (omp_for);
17924 OMP_CLAUSE_CHAIN (c) = clauses;
17925 OMP_PARALLEL_CLAUSES (omp_par) = c_finish_omp_clauses (c, C_ORT_CILK);
17926 add_stmt (omp_par);
17929 block = c_end_compound_stmt (loc, block, true);
17930 add_stmt (block);
17934 /* Parse a transaction attribute (GCC Extension).
17936 transaction-attribute:
17937 attributes
17938 [ [ any-word ] ]
17940 The transactional memory language description is written for C++,
17941 and uses the C++0x attribute syntax. For compatibility, allow the
17942 bracket style for transactions in C as well. */
17944 static tree
17945 c_parser_transaction_attributes (c_parser *parser)
17947 tree attr_name, attr = NULL;
17949 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
17950 return c_parser_attributes (parser);
17952 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
17953 return NULL_TREE;
17954 c_parser_consume_token (parser);
17955 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
17956 goto error1;
17958 attr_name = c_parser_attribute_any_word (parser);
17959 if (attr_name)
17961 c_parser_consume_token (parser);
17962 attr = build_tree_list (attr_name, NULL_TREE);
17964 else
17965 c_parser_error (parser, "expected identifier");
17967 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17968 error1:
17969 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
17970 return attr;
17973 /* Parse a __transaction_atomic or __transaction_relaxed statement
17974 (GCC Extension).
17976 transaction-statement:
17977 __transaction_atomic transaction-attribute[opt] compound-statement
17978 __transaction_relaxed compound-statement
17980 Note that the only valid attribute is: "outer".
17983 static tree
17984 c_parser_transaction (c_parser *parser, enum rid keyword)
17986 unsigned int old_in = parser->in_transaction;
17987 unsigned int this_in = 1, new_in;
17988 location_t loc = c_parser_peek_token (parser)->location;
17989 tree stmt, attrs;
17991 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
17992 || keyword == RID_TRANSACTION_RELAXED)
17993 && c_parser_next_token_is_keyword (parser, keyword));
17994 c_parser_consume_token (parser);
17996 if (keyword == RID_TRANSACTION_RELAXED)
17997 this_in |= TM_STMT_ATTR_RELAXED;
17998 else
18000 attrs = c_parser_transaction_attributes (parser);
18001 if (attrs)
18002 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18005 /* Keep track if we're in the lexical scope of an outer transaction. */
18006 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18008 parser->in_transaction = new_in;
18009 stmt = c_parser_compound_statement (parser);
18010 parser->in_transaction = old_in;
18012 if (flag_tm)
18013 stmt = c_finish_transaction (loc, stmt, this_in);
18014 else
18015 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18016 "%<__transaction_atomic%> without transactional memory support enabled"
18017 : "%<__transaction_relaxed %> "
18018 "without transactional memory support enabled"));
18020 return stmt;
18023 /* Parse a __transaction_atomic or __transaction_relaxed expression
18024 (GCC Extension).
18026 transaction-expression:
18027 __transaction_atomic ( expression )
18028 __transaction_relaxed ( expression )
18031 static struct c_expr
18032 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18034 struct c_expr ret;
18035 unsigned int old_in = parser->in_transaction;
18036 unsigned int this_in = 1;
18037 location_t loc = c_parser_peek_token (parser)->location;
18038 tree attrs;
18040 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18041 || keyword == RID_TRANSACTION_RELAXED)
18042 && c_parser_next_token_is_keyword (parser, keyword));
18043 c_parser_consume_token (parser);
18045 if (keyword == RID_TRANSACTION_RELAXED)
18046 this_in |= TM_STMT_ATTR_RELAXED;
18047 else
18049 attrs = c_parser_transaction_attributes (parser);
18050 if (attrs)
18051 this_in |= parse_tm_stmt_attr (attrs, 0);
18054 parser->in_transaction = this_in;
18055 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18057 tree expr = c_parser_expression (parser).value;
18058 ret.original_type = TREE_TYPE (expr);
18059 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18060 if (this_in & TM_STMT_ATTR_RELAXED)
18061 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18062 SET_EXPR_LOCATION (ret.value, loc);
18063 ret.original_code = TRANSACTION_EXPR;
18064 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
18066 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18067 goto error;
18070 else
18072 error:
18073 ret.value = error_mark_node;
18074 ret.original_code = ERROR_MARK;
18075 ret.original_type = NULL;
18077 parser->in_transaction = old_in;
18079 if (!flag_tm)
18080 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18081 "%<__transaction_atomic%> without transactional memory support enabled"
18082 : "%<__transaction_relaxed %> "
18083 "without transactional memory support enabled"));
18085 set_c_expr_source_range (&ret, loc, loc);
18087 return ret;
18090 /* Parse a __transaction_cancel statement (GCC Extension).
18092 transaction-cancel-statement:
18093 __transaction_cancel transaction-attribute[opt] ;
18095 Note that the only valid attribute is "outer".
18098 static tree
18099 c_parser_transaction_cancel (c_parser *parser)
18101 location_t loc = c_parser_peek_token (parser)->location;
18102 tree attrs;
18103 bool is_outer = false;
18105 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18106 c_parser_consume_token (parser);
18108 attrs = c_parser_transaction_attributes (parser);
18109 if (attrs)
18110 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18112 if (!flag_tm)
18114 error_at (loc, "%<__transaction_cancel%> without "
18115 "transactional memory support enabled");
18116 goto ret_error;
18118 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18120 error_at (loc, "%<__transaction_cancel%> within a "
18121 "%<__transaction_relaxed%>");
18122 goto ret_error;
18124 else if (is_outer)
18126 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18127 && !is_tm_may_cancel_outer (current_function_decl))
18129 error_at (loc, "outer %<__transaction_cancel%> not "
18130 "within outer %<__transaction_atomic%>");
18131 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18132 goto ret_error;
18135 else if (parser->in_transaction == 0)
18137 error_at (loc, "%<__transaction_cancel%> not within "
18138 "%<__transaction_atomic%>");
18139 goto ret_error;
18142 return add_stmt (build_tm_abort_call (loc, is_outer));
18144 ret_error:
18145 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18148 /* Parse a single source file. */
18150 void
18151 c_parse_file (void)
18153 /* Use local storage to begin. If the first token is a pragma, parse it.
18154 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18155 which will cause garbage collection. */
18156 c_parser tparser;
18158 memset (&tparser, 0, sizeof tparser);
18159 tparser.tokens = &tparser.tokens_buf[0];
18160 the_parser = &tparser;
18162 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18163 c_parser_pragma_pch_preprocess (&tparser);
18165 the_parser = ggc_alloc<c_parser> ();
18166 *the_parser = tparser;
18167 if (tparser.tokens == &tparser.tokens_buf[0])
18168 the_parser->tokens = &the_parser->tokens_buf[0];
18170 /* Initialize EH, if we've been told to do so. */
18171 if (flag_exceptions)
18172 using_eh_for_cleanups ();
18174 c_parser_translation_unit (the_parser);
18175 the_parser = NULL;
18178 /* This function parses Cilk Plus array notation. The starting index is
18179 passed in INITIAL_INDEX and the array name is passes in ARRAY_VALUE. The
18180 return value of this function is a tree_node called VALUE_TREE of type
18181 ARRAY_NOTATION_REF. */
18183 static tree
18184 c_parser_array_notation (location_t loc, c_parser *parser, tree initial_index,
18185 tree array_value)
18187 c_token *token = NULL;
18188 tree start_index = NULL_TREE, end_index = NULL_TREE, stride = NULL_TREE;
18189 tree value_tree = NULL_TREE, type = NULL_TREE, array_type = NULL_TREE;
18190 tree array_type_domain = NULL_TREE;
18192 if (array_value == error_mark_node || initial_index == error_mark_node)
18194 /* No need to continue. If either of these 2 were true, then an error
18195 must be emitted already. Thus, no need to emit them twice. */
18196 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18197 return error_mark_node;
18200 array_type = TREE_TYPE (array_value);
18201 gcc_assert (array_type);
18202 if (TREE_CODE (array_type) != ARRAY_TYPE
18203 && TREE_CODE (array_type) != POINTER_TYPE)
18205 error_at (loc, "base of array section must be pointer or array type");
18206 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18207 return error_mark_node;
18209 type = TREE_TYPE (array_type);
18210 token = c_parser_peek_token (parser);
18212 if (token->type == CPP_EOF)
18214 c_parser_error (parser, "expected %<:%> or numeral");
18215 return value_tree;
18217 else if (token->type == CPP_COLON)
18219 if (!initial_index)
18221 /* If we are here, then we have a case like this A[:]. */
18222 c_parser_consume_token (parser);
18223 if (TREE_CODE (array_type) == POINTER_TYPE)
18225 error_at (loc, "start-index and length fields necessary for "
18226 "using array notations in pointers");
18227 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18228 return error_mark_node;
18230 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18232 error_at (loc, "array notations cannot be used with function "
18233 "type");
18234 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18235 return error_mark_node;
18237 array_type_domain = TYPE_DOMAIN (array_type);
18239 if (!array_type_domain)
18241 error_at (loc, "start-index and length fields necessary for "
18242 "using array notations in dimensionless arrays");
18243 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18244 return error_mark_node;
18247 start_index = TYPE_MINVAL (array_type_domain);
18248 start_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node,
18249 start_index);
18250 if (!TYPE_MAXVAL (array_type_domain)
18251 || !TREE_CONSTANT (TYPE_MAXVAL (array_type_domain)))
18253 error_at (loc, "start-index and length fields necessary for "
18254 "using array notations in variable-length arrays");
18255 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18256 return error_mark_node;
18258 end_index = TYPE_MAXVAL (array_type_domain);
18259 end_index = fold_build2 (PLUS_EXPR, TREE_TYPE (end_index),
18260 end_index, integer_one_node);
18261 end_index = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, end_index);
18262 stride = build_int_cst (integer_type_node, 1);
18263 stride = fold_build1 (CONVERT_EXPR, ptrdiff_type_node, stride);
18265 else if (initial_index != error_mark_node)
18267 /* If we are here, then there should be 2 possibilities:
18268 1. Array [EXPR : EXPR]
18269 2. Array [EXPR : EXPR : EXPR]
18271 start_index = initial_index;
18273 if (TREE_CODE (array_type) == FUNCTION_TYPE)
18275 error_at (loc, "array notations cannot be used with function "
18276 "type");
18277 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
18278 return error_mark_node;
18280 c_parser_consume_token (parser); /* consume the ':' */
18281 struct c_expr ce = c_parser_expression (parser);
18282 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18283 end_index = ce.value;
18284 if (!end_index || end_index == error_mark_node)
18286 c_parser_skip_to_end_of_block_or_statement (parser);
18287 return error_mark_node;
18289 if (c_parser_peek_token (parser)->type == CPP_COLON)
18291 c_parser_consume_token (parser);
18292 ce = c_parser_expression (parser);
18293 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
18294 stride = ce.value;
18295 if (!stride || stride == error_mark_node)
18297 c_parser_skip_to_end_of_block_or_statement (parser);
18298 return error_mark_node;
18302 else
18303 c_parser_error (parser, "expected array notation expression");
18305 else
18306 c_parser_error (parser, "expected array notation expression");
18308 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18310 value_tree = build_array_notation_ref (loc, array_value, start_index,
18311 end_index, stride, type);
18312 if (value_tree != error_mark_node)
18313 SET_EXPR_LOCATION (value_tree, loc);
18314 return value_tree;
18317 /* Parse the body of a function declaration marked with "__RTL".
18319 The RTL parser works on the level of characters read from a
18320 FILE *, whereas c_parser works at the level of tokens.
18321 Square this circle by consuming all of the tokens up to and
18322 including the closing brace, recording the start/end of the RTL
18323 fragment, and reopening the file and re-reading the relevant
18324 lines within the RTL parser.
18326 This requires the opening and closing braces of the C function
18327 to be on separate lines from the RTL they wrap.
18329 Take ownership of START_WITH_PASS, if non-NULL. */
18331 void
18332 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18334 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18336 free (start_with_pass);
18337 return;
18340 location_t start_loc = c_parser_peek_token (parser)->location;
18342 /* Consume all tokens, up to the closing brace, handling
18343 matching pairs of braces in the rtl dump. */
18344 int num_open_braces = 1;
18345 while (1)
18347 switch (c_parser_peek_token (parser)->type)
18349 case CPP_OPEN_BRACE:
18350 num_open_braces++;
18351 break;
18352 case CPP_CLOSE_BRACE:
18353 if (--num_open_braces == 0)
18354 goto found_closing_brace;
18355 break;
18356 case CPP_EOF:
18357 error_at (start_loc, "no closing brace");
18358 free (start_with_pass);
18359 return;
18360 default:
18361 break;
18363 c_parser_consume_token (parser);
18366 found_closing_brace:
18367 /* At the closing brace; record its location. */
18368 location_t end_loc = c_parser_peek_token (parser)->location;
18370 /* Consume the closing brace. */
18371 c_parser_consume_token (parser);
18373 /* Invoke the RTL parser. */
18374 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18376 free (start_with_pass);
18377 return;
18380 /* If a pass name was provided for START_WITH_PASS, run the backend
18381 accordingly now, on the cfun created above, transferring
18382 ownership of START_WITH_PASS. */
18383 if (start_with_pass)
18384 run_rtl_passes (start_with_pass);
18387 #include "gt-c-c-parser.h"