Fix build on sparc64-linux-gnu.
[official-gcc.git] / gcc / c / c-parser.c
blobb36fca9a330acdfc7bb1d9ab5f32c7974c5383bf
1 /* Parser for C and Objective-C.
2 Copyright (C) 1987-2018 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 #define INCLUDE_UNIQUE_PTR
40 #include "system.h"
41 #include "coretypes.h"
42 #include "target.h"
43 #include "function.h"
44 #include "c-tree.h"
45 #include "timevar.h"
46 #include "stringpool.h"
47 #include "cgraph.h"
48 #include "attribs.h"
49 #include "stor-layout.h"
50 #include "varasm.h"
51 #include "trans-mem.h"
52 #include "c-family/c-pragma.h"
53 #include "c-lang.h"
54 #include "c-family/c-objc.h"
55 #include "plugin.h"
56 #include "omp-general.h"
57 #include "omp-offload.h"
58 #include "builtins.h"
59 #include "gomp-constants.h"
60 #include "c-family/c-indentation.h"
61 #include "gimple-expr.h"
62 #include "context.h"
63 #include "gcc-rich-location.h"
64 #include "c-parser.h"
65 #include "gimple-parser.h"
66 #include "read-rtl-function.h"
67 #include "run-rtl-passes.h"
68 #include "intl.h"
69 #include "c-family/name-hint.h"
70 #include "tree-iterator.h"
72 /* We need to walk over decls with incomplete struct/union/enum types
73 after parsing the whole translation unit.
74 In finish_decl(), if the decl is static, has incomplete
75 struct/union/enum type, it is appeneded to incomplete_record_decls.
76 In c_parser_translation_unit(), we iterate over incomplete_record_decls
77 and report error if any of the decls are still incomplete. */
79 vec<tree> incomplete_record_decls;
81 void
82 set_c_expr_source_range (c_expr *expr,
83 location_t start, location_t finish)
85 expr->src_range.m_start = start;
86 expr->src_range.m_finish = finish;
87 if (expr->value)
88 set_source_range (expr->value, start, finish);
91 void
92 set_c_expr_source_range (c_expr *expr,
93 source_range src_range)
95 expr->src_range = src_range;
96 if (expr->value)
97 set_source_range (expr->value, src_range);
101 /* Initialization routine for this file. */
103 void
104 c_parse_init (void)
106 /* The only initialization required is of the reserved word
107 identifiers. */
108 unsigned int i;
109 tree id;
110 int mask = 0;
112 /* Make sure RID_MAX hasn't grown past the 8 bits used to hold the keyword in
113 the c_token structure. */
114 gcc_assert (RID_MAX <= 255);
116 mask |= D_CXXONLY;
117 if (!flag_isoc99)
118 mask |= D_C99;
119 if (flag_no_asm)
121 mask |= D_ASM | D_EXT;
122 if (!flag_isoc99)
123 mask |= D_EXT89;
125 if (!c_dialect_objc ())
126 mask |= D_OBJC | D_CXX_OBJC;
128 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
129 for (i = 0; i < num_c_common_reswords; i++)
131 /* If a keyword is disabled, do not enter it into the table
132 and so create a canonical spelling that isn't a keyword. */
133 if (c_common_reswords[i].disable & mask)
135 if (warn_cxx_compat
136 && (c_common_reswords[i].disable & D_CXXWARN))
138 id = get_identifier (c_common_reswords[i].word);
139 C_SET_RID_CODE (id, RID_CXX_COMPAT_WARN);
140 C_IS_RESERVED_WORD (id) = 1;
142 continue;
145 id = get_identifier (c_common_reswords[i].word);
146 C_SET_RID_CODE (id, c_common_reswords[i].rid);
147 C_IS_RESERVED_WORD (id) = 1;
148 ridpointers [(int) c_common_reswords[i].rid] = id;
151 for (i = 0; i < NUM_INT_N_ENTS; i++)
153 /* We always create the symbols but they aren't always supported. */
154 char name[50];
155 sprintf (name, "__int%d", int_n_data[i].bitsize);
156 id = get_identifier (name);
157 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
158 C_IS_RESERVED_WORD (id) = 1;
162 /* A parser structure recording information about the state and
163 context of parsing. Includes lexer information with up to two
164 tokens of look-ahead; more are not needed for C. */
165 struct GTY(()) c_parser {
166 /* The look-ahead tokens. */
167 c_token * GTY((skip)) tokens;
168 /* Buffer for look-ahead tokens. */
169 c_token tokens_buf[4];
170 /* How many look-ahead tokens are available (0 - 4, or
171 more if parsing from pre-lexed tokens). */
172 unsigned int tokens_avail;
173 /* True if a syntax error is being recovered from; false otherwise.
174 c_parser_error sets this flag. It should clear this flag when
175 enough tokens have been consumed to recover from the error. */
176 BOOL_BITFIELD error : 1;
177 /* True if we're processing a pragma, and shouldn't automatically
178 consume CPP_PRAGMA_EOL. */
179 BOOL_BITFIELD in_pragma : 1;
180 /* True if we're parsing the outermost block of an if statement. */
181 BOOL_BITFIELD in_if_block : 1;
182 /* True if we want to lex an untranslated string. */
183 BOOL_BITFIELD lex_untranslated_string : 1;
185 /* Objective-C specific parser/lexer information. */
187 /* True if we are in a context where the Objective-C "PQ" keywords
188 are considered keywords. */
189 BOOL_BITFIELD objc_pq_context : 1;
190 /* True if we are parsing a (potential) Objective-C foreach
191 statement. This is set to true after we parsed 'for (' and while
192 we wait for 'in' or ';' to decide if it's a standard C for loop or an
193 Objective-C foreach loop. */
194 BOOL_BITFIELD objc_could_be_foreach_context : 1;
195 /* The following flag is needed to contextualize Objective-C lexical
196 analysis. In some cases (e.g., 'int NSObject;'), it is
197 undesirable to bind an identifier to an Objective-C class, even
198 if a class with that name exists. */
199 BOOL_BITFIELD objc_need_raw_identifier : 1;
200 /* Nonzero if we're processing a __transaction statement. The value
201 is 1 | TM_STMT_ATTR_*. */
202 unsigned int in_transaction : 4;
203 /* True if we are in a context where the Objective-C "Property attribute"
204 keywords are valid. */
205 BOOL_BITFIELD objc_property_attr_context : 1;
207 /* Location of the last consumed token. */
208 location_t last_token_location;
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 case RID_ALIGNAS:
508 return true;
509 default:
510 if (keyword >= RID_FIRST_INT_N
511 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
512 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
513 return true;
514 return false;
518 /* Return true if TOKEN can start a type name,
519 false otherwise. */
520 bool
521 c_token_starts_typename (c_token *token)
523 switch (token->type)
525 case CPP_NAME:
526 switch (token->id_kind)
528 case C_ID_ID:
529 return false;
530 case C_ID_ADDRSPACE:
531 return true;
532 case C_ID_TYPENAME:
533 return true;
534 case C_ID_CLASSNAME:
535 gcc_assert (c_dialect_objc ());
536 return true;
537 default:
538 gcc_unreachable ();
540 case CPP_KEYWORD:
541 return c_keyword_starts_typename (token->keyword);
542 case CPP_LESS:
543 if (c_dialect_objc ())
544 return true;
545 return false;
546 default:
547 return false;
551 /* Return true if the next token from PARSER can start a type name,
552 false otherwise. LA specifies how to do lookahead in order to
553 detect unknown type names. If unsure, pick CLA_PREFER_ID. */
555 static inline bool
556 c_parser_next_tokens_start_typename (c_parser *parser, enum c_lookahead_kind la)
558 c_token *token = c_parser_peek_token (parser);
559 if (c_token_starts_typename (token))
560 return true;
562 /* Try a bit harder to detect an unknown typename. */
563 if (la != cla_prefer_id
564 && token->type == CPP_NAME
565 && token->id_kind == C_ID_ID
567 /* Do not try too hard when we could have "object in array". */
568 && !parser->objc_could_be_foreach_context
570 && (la == cla_prefer_type
571 || c_parser_peek_2nd_token (parser)->type == CPP_NAME
572 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
574 /* Only unknown identifiers. */
575 && !lookup_name (token->value))
576 return true;
578 return false;
581 /* Return true if TOKEN is a type qualifier, false otherwise. */
582 static bool
583 c_token_is_qualifier (c_token *token)
585 switch (token->type)
587 case CPP_NAME:
588 switch (token->id_kind)
590 case C_ID_ADDRSPACE:
591 return true;
592 default:
593 return false;
595 case CPP_KEYWORD:
596 switch (token->keyword)
598 case RID_CONST:
599 case RID_VOLATILE:
600 case RID_RESTRICT:
601 case RID_ATTRIBUTE:
602 case RID_ATOMIC:
603 return true;
604 default:
605 return false;
607 case CPP_LESS:
608 return false;
609 default:
610 gcc_unreachable ();
614 /* Return true if the next token from PARSER is a type qualifier,
615 false otherwise. */
616 static inline bool
617 c_parser_next_token_is_qualifier (c_parser *parser)
619 c_token *token = c_parser_peek_token (parser);
620 return c_token_is_qualifier (token);
623 /* Return true if TOKEN can start declaration specifiers, false
624 otherwise. */
625 static bool
626 c_token_starts_declspecs (c_token *token)
628 switch (token->type)
630 case CPP_NAME:
631 switch (token->id_kind)
633 case C_ID_ID:
634 return false;
635 case C_ID_ADDRSPACE:
636 return true;
637 case C_ID_TYPENAME:
638 return true;
639 case C_ID_CLASSNAME:
640 gcc_assert (c_dialect_objc ());
641 return true;
642 default:
643 gcc_unreachable ();
645 case CPP_KEYWORD:
646 switch (token->keyword)
648 case RID_STATIC:
649 case RID_EXTERN:
650 case RID_REGISTER:
651 case RID_TYPEDEF:
652 case RID_INLINE:
653 case RID_NORETURN:
654 case RID_AUTO:
655 case RID_THREAD:
656 case RID_UNSIGNED:
657 case RID_LONG:
658 case RID_SHORT:
659 case RID_SIGNED:
660 case RID_COMPLEX:
661 case RID_INT:
662 case RID_CHAR:
663 case RID_FLOAT:
664 case RID_DOUBLE:
665 case RID_VOID:
666 case RID_DFLOAT32:
667 case RID_DFLOAT64:
668 case RID_DFLOAT128:
669 CASE_RID_FLOATN_NX:
670 case RID_BOOL:
671 case RID_ENUM:
672 case RID_STRUCT:
673 case RID_UNION:
674 case RID_TYPEOF:
675 case RID_CONST:
676 case RID_VOLATILE:
677 case RID_RESTRICT:
678 case RID_ATTRIBUTE:
679 case RID_FRACT:
680 case RID_ACCUM:
681 case RID_SAT:
682 case RID_ALIGNAS:
683 case RID_ATOMIC:
684 case RID_AUTO_TYPE:
685 return true;
686 default:
687 if (token->keyword >= RID_FIRST_INT_N
688 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
689 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
690 return true;
691 return false;
693 case CPP_LESS:
694 if (c_dialect_objc ())
695 return true;
696 return false;
697 default:
698 return false;
703 /* Return true if TOKEN can start declaration specifiers or a static
704 assertion, false otherwise. */
705 static bool
706 c_token_starts_declaration (c_token *token)
708 if (c_token_starts_declspecs (token)
709 || token->keyword == RID_STATIC_ASSERT)
710 return true;
711 else
712 return false;
715 /* Return true if the next token from PARSER can start declaration
716 specifiers, false otherwise. */
717 bool
718 c_parser_next_token_starts_declspecs (c_parser *parser)
720 c_token *token = c_parser_peek_token (parser);
722 /* In Objective-C, a classname normally starts a declspecs unless it
723 is immediately followed by a dot. In that case, it is the
724 Objective-C 2.0 "dot-syntax" for class objects, ie, calls the
725 setter/getter on the class. c_token_starts_declspecs() can't
726 differentiate between the two cases because it only checks the
727 current token, so we have a special check here. */
728 if (c_dialect_objc ()
729 && token->type == CPP_NAME
730 && token->id_kind == C_ID_CLASSNAME
731 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
732 return false;
734 return c_token_starts_declspecs (token);
737 /* Return true if the next tokens from PARSER can start declaration
738 specifiers or a static assertion, false otherwise. */
739 bool
740 c_parser_next_tokens_start_declaration (c_parser *parser)
742 c_token *token = c_parser_peek_token (parser);
744 /* Same as above. */
745 if (c_dialect_objc ()
746 && token->type == CPP_NAME
747 && token->id_kind == C_ID_CLASSNAME
748 && c_parser_peek_2nd_token (parser)->type == CPP_DOT)
749 return false;
751 /* Labels do not start declarations. */
752 if (token->type == CPP_NAME
753 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
754 return false;
756 if (c_token_starts_declaration (token))
757 return true;
759 if (c_parser_next_tokens_start_typename (parser, cla_nonabstract_decl))
760 return true;
762 return false;
765 /* Consume the next token from PARSER. */
767 void
768 c_parser_consume_token (c_parser *parser)
770 gcc_assert (parser->tokens_avail >= 1);
771 gcc_assert (parser->tokens[0].type != CPP_EOF);
772 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL);
773 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA);
774 parser->last_token_location = parser->tokens[0].location;
775 if (parser->tokens != &parser->tokens_buf[0])
776 parser->tokens++;
777 else if (parser->tokens_avail == 2)
778 parser->tokens[0] = parser->tokens[1];
779 parser->tokens_avail--;
782 /* Expect the current token to be a #pragma. Consume it and remember
783 that we've begun parsing a pragma. */
785 static void
786 c_parser_consume_pragma (c_parser *parser)
788 gcc_assert (!parser->in_pragma);
789 gcc_assert (parser->tokens_avail >= 1);
790 gcc_assert (parser->tokens[0].type == CPP_PRAGMA);
791 if (parser->tokens != &parser->tokens_buf[0])
792 parser->tokens++;
793 else if (parser->tokens_avail == 2)
794 parser->tokens[0] = parser->tokens[1];
795 parser->tokens_avail--;
796 parser->in_pragma = true;
799 /* Update the global input_location from TOKEN. */
800 static inline void
801 c_parser_set_source_position_from_token (c_token *token)
803 if (token->type != CPP_EOF)
805 input_location = token->location;
809 /* Helper function for c_parser_error.
810 Having peeked a token of kind TOK1_KIND that might signify
811 a conflict marker, peek successor tokens to determine
812 if we actually do have a conflict marker.
813 Specifically, we consider a run of 7 '<', '=' or '>' characters
814 at the start of a line as a conflict marker.
815 These come through the lexer as three pairs and a single,
816 e.g. three CPP_LSHIFT ("<<") and a CPP_LESS ('<').
817 If it returns true, *OUT_LOC is written to with the location/range
818 of the marker. */
820 static bool
821 c_parser_peek_conflict_marker (c_parser *parser, enum cpp_ttype tok1_kind,
822 location_t *out_loc)
824 c_token *token2 = c_parser_peek_2nd_token (parser);
825 if (token2->type != tok1_kind)
826 return false;
827 c_token *token3 = c_parser_peek_nth_token (parser, 3);
828 if (token3->type != tok1_kind)
829 return false;
830 c_token *token4 = c_parser_peek_nth_token (parser, 4);
831 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
832 return false;
834 /* It must be at the start of the line. */
835 location_t start_loc = c_parser_peek_token (parser)->location;
836 if (LOCATION_COLUMN (start_loc) != 1)
837 return false;
839 /* We have a conflict marker. Construct a location of the form:
840 <<<<<<<
841 ^~~~~~~
842 with start == caret, finishing at the end of the marker. */
843 location_t finish_loc = get_finish (token4->location);
844 *out_loc = make_location (start_loc, start_loc, finish_loc);
846 return true;
849 /* Issue a diagnostic of the form
850 FILE:LINE: MESSAGE before TOKEN
851 where TOKEN is the next token in the input stream of PARSER.
852 MESSAGE (specified by the caller) is usually of the form "expected
853 OTHER-TOKEN".
855 Use RICHLOC as the location of the diagnostic.
857 Do not issue a diagnostic if still recovering from an error.
859 Return true iff an error was actually emitted.
861 ??? This is taken from the C++ parser, but building up messages in
862 this way is not i18n-friendly and some other approach should be
863 used. */
865 static bool
866 c_parser_error_richloc (c_parser *parser, const char *gmsgid,
867 rich_location *richloc)
869 c_token *token = c_parser_peek_token (parser);
870 if (parser->error)
871 return false;
872 parser->error = true;
873 if (!gmsgid)
874 return false;
876 /* If this is actually a conflict marker, report it as such. */
877 if (token->type == CPP_LSHIFT
878 || token->type == CPP_RSHIFT
879 || token->type == CPP_EQ_EQ)
881 location_t loc;
882 if (c_parser_peek_conflict_marker (parser, token->type, &loc))
884 error_at (loc, "version control conflict marker in file");
885 return true;
889 c_parse_error (gmsgid,
890 /* Because c_parse_error does not understand
891 CPP_KEYWORD, keywords are treated like
892 identifiers. */
893 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
894 /* ??? The C parser does not save the cpp flags of a
895 token, we need to pass 0 here and we will not get
896 the source spelling of some tokens but rather the
897 canonical spelling. */
898 token->value, /*flags=*/0, richloc);
899 return true;
902 /* As c_parser_error_richloc, but issue the message at the
903 location of PARSER's next token, or at input_location
904 if the next token is EOF. */
906 bool
907 c_parser_error (c_parser *parser, const char *gmsgid)
909 c_token *token = c_parser_peek_token (parser);
910 c_parser_set_source_position_from_token (token);
911 rich_location richloc (line_table, input_location);
912 return c_parser_error_richloc (parser, gmsgid, &richloc);
915 /* Some tokens naturally come in pairs e.g.'(' and ')'.
916 This class is for tracking such a matching pair of symbols.
917 In particular, it tracks the location of the first token,
918 so that if the second token is missing, we can highlight the
919 location of the first token when notifying the user about the
920 problem. */
922 template <typename traits_t>
923 class token_pair
925 public:
926 /* token_pair's ctor. */
927 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
929 /* If the next token is the opening symbol for this pair, consume it and
930 return true.
931 Otherwise, issue an error and return false.
932 In either case, record the location of the opening token. */
934 bool require_open (c_parser *parser)
936 c_token *token = c_parser_peek_token (parser);
937 if (token)
938 m_open_loc = token->location;
940 return c_parser_require (parser, traits_t::open_token_type,
941 traits_t::open_gmsgid);
944 /* Consume the next token from PARSER, recording its location as
945 that of the opening token within the pair. */
947 void consume_open (c_parser *parser)
949 c_token *token = c_parser_peek_token (parser);
950 gcc_assert (token->type == traits_t::open_token_type);
951 m_open_loc = token->location;
952 c_parser_consume_token (parser);
955 /* If the next token is the closing symbol for this pair, consume it
956 and return true.
957 Otherwise, issue an error, highlighting the location of the
958 corresponding opening token, and return false. */
960 bool require_close (c_parser *parser) const
962 return c_parser_require (parser, traits_t::close_token_type,
963 traits_t::close_gmsgid, m_open_loc);
966 /* Like token_pair::require_close, except that tokens will be skipped
967 until the desired token is found. An error message is still produced
968 if the next token is not as expected. */
970 void skip_until_found_close (c_parser *parser) const
972 c_parser_skip_until_found (parser, traits_t::close_token_type,
973 traits_t::close_gmsgid, m_open_loc);
976 private:
977 location_t m_open_loc;
980 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
982 struct matching_paren_traits
984 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
985 static const char * const open_gmsgid;
986 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
987 static const char * const close_gmsgid;
990 const char * const matching_paren_traits::open_gmsgid = "expected %<(%>";
991 const char * const matching_paren_traits::close_gmsgid = "expected %<)%>";
993 /* "matching_parens" is a token_pair<T> class for tracking matching
994 pairs of parentheses. */
996 typedef token_pair<matching_paren_traits> matching_parens;
998 /* Traits for token_pair<T> for tracking matching pairs of braces. */
1000 struct matching_brace_traits
1002 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
1003 static const char * const open_gmsgid;
1004 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
1005 static const char * const close_gmsgid;
1008 const char * const matching_brace_traits::open_gmsgid = "expected %<{%>";
1009 const char * const matching_brace_traits::close_gmsgid = "expected %<}%>";
1011 /* "matching_braces" is a token_pair<T> class for tracking matching
1012 pairs of braces. */
1014 typedef token_pair<matching_brace_traits> matching_braces;
1016 /* Get a description of the matching symbol to TYPE e.g. "(" for
1017 CPP_CLOSE_PAREN. */
1019 static const char *
1020 get_matching_symbol (enum cpp_ttype type)
1022 switch (type)
1024 default:
1025 gcc_unreachable ();
1026 return "";
1027 case CPP_CLOSE_PAREN:
1028 return "(";
1029 case CPP_CLOSE_BRACE:
1030 return "{";
1034 /* If the next token is of the indicated TYPE, consume it. Otherwise,
1035 issue the error MSGID. If MSGID is NULL then a message has already
1036 been produced and no message will be produced this time. Returns
1037 true if found, false otherwise.
1039 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1040 within any error as the location of an "opening" token matching
1041 the close token TYPE (e.g. the location of the '(' when TYPE is
1042 CPP_CLOSE_PAREN).
1044 If TYPE_IS_UNIQUE is true (the default) then msgid describes exactly
1045 one type (e.g. "expected %<)%>") and thus it may be reasonable to
1046 attempt to generate a fix-it hint for the problem.
1047 Otherwise msgid describes multiple token types (e.g.
1048 "expected %<;%>, %<,%> or %<)%>"), and thus we shouldn't attempt to
1049 generate a fix-it hint. */
1051 bool
1052 c_parser_require (c_parser *parser,
1053 enum cpp_ttype type,
1054 const char *msgid,
1055 location_t matching_location,
1056 bool type_is_unique)
1058 if (c_parser_next_token_is (parser, type))
1060 c_parser_consume_token (parser);
1061 return true;
1063 else
1065 location_t next_token_loc = c_parser_peek_token (parser)->location;
1066 gcc_rich_location richloc (next_token_loc);
1068 /* Potentially supply a fix-it hint, suggesting to add the
1069 missing token immediately after the *previous* token.
1070 This may move the primary location within richloc. */
1071 if (!parser->error && type_is_unique)
1072 maybe_suggest_missing_token_insertion (&richloc, type,
1073 parser->last_token_location);
1075 /* If matching_location != UNKNOWN_LOCATION, highlight it.
1076 Attempt to consolidate diagnostics by printing it as a
1077 secondary range within the main diagnostic. */
1078 bool added_matching_location = false;
1079 if (matching_location != UNKNOWN_LOCATION)
1080 added_matching_location
1081 = richloc.add_location_if_nearby (matching_location);
1083 if (c_parser_error_richloc (parser, msgid, &richloc))
1084 /* If we weren't able to consolidate matching_location, then
1085 print it as a secondary diagnostic. */
1086 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
1087 inform (matching_location, "to match this %qs",
1088 get_matching_symbol (type));
1090 return false;
1094 /* If the next token is the indicated keyword, consume it. Otherwise,
1095 issue the error MSGID. Returns true if found, false otherwise. */
1097 static bool
1098 c_parser_require_keyword (c_parser *parser,
1099 enum rid keyword,
1100 const char *msgid)
1102 if (c_parser_next_token_is_keyword (parser, keyword))
1104 c_parser_consume_token (parser);
1105 return true;
1107 else
1109 c_parser_error (parser, msgid);
1110 return false;
1114 /* Like c_parser_require, except that tokens will be skipped until the
1115 desired token is found. An error message is still produced if the
1116 next token is not as expected. If MSGID is NULL then a message has
1117 already been produced and no message will be produced this
1118 time.
1120 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
1121 within any error as the location of an "opening" token matching
1122 the close token TYPE (e.g. the location of the '(' when TYPE is
1123 CPP_CLOSE_PAREN). */
1125 void
1126 c_parser_skip_until_found (c_parser *parser,
1127 enum cpp_ttype type,
1128 const char *msgid,
1129 location_t matching_location)
1131 unsigned nesting_depth = 0;
1133 if (c_parser_require (parser, type, msgid, matching_location))
1134 return;
1136 /* Skip tokens until the desired token is found. */
1137 while (true)
1139 /* Peek at the next token. */
1140 c_token *token = c_parser_peek_token (parser);
1141 /* If we've reached the token we want, consume it and stop. */
1142 if (token->type == type && !nesting_depth)
1144 c_parser_consume_token (parser);
1145 break;
1148 /* If we've run out of tokens, stop. */
1149 if (token->type == CPP_EOF)
1150 return;
1151 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1152 return;
1153 if (token->type == CPP_OPEN_BRACE
1154 || token->type == CPP_OPEN_PAREN
1155 || token->type == CPP_OPEN_SQUARE)
1156 ++nesting_depth;
1157 else if (token->type == CPP_CLOSE_BRACE
1158 || token->type == CPP_CLOSE_PAREN
1159 || token->type == CPP_CLOSE_SQUARE)
1161 if (nesting_depth-- == 0)
1162 break;
1164 /* Consume this token. */
1165 c_parser_consume_token (parser);
1167 parser->error = false;
1170 /* Skip tokens until the end of a parameter is found, but do not
1171 consume the comma, semicolon or closing delimiter. */
1173 static void
1174 c_parser_skip_to_end_of_parameter (c_parser *parser)
1176 unsigned nesting_depth = 0;
1178 while (true)
1180 c_token *token = c_parser_peek_token (parser);
1181 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON)
1182 && !nesting_depth)
1183 break;
1184 /* If we've run out of tokens, stop. */
1185 if (token->type == CPP_EOF)
1186 return;
1187 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma)
1188 return;
1189 if (token->type == CPP_OPEN_BRACE
1190 || token->type == CPP_OPEN_PAREN
1191 || token->type == CPP_OPEN_SQUARE)
1192 ++nesting_depth;
1193 else if (token->type == CPP_CLOSE_BRACE
1194 || token->type == CPP_CLOSE_PAREN
1195 || token->type == CPP_CLOSE_SQUARE)
1197 if (nesting_depth-- == 0)
1198 break;
1200 /* Consume this token. */
1201 c_parser_consume_token (parser);
1203 parser->error = false;
1206 /* Expect to be at the end of the pragma directive and consume an
1207 end of line marker. */
1209 static void
1210 c_parser_skip_to_pragma_eol (c_parser *parser, bool error_if_not_eol = true)
1212 gcc_assert (parser->in_pragma);
1213 parser->in_pragma = false;
1215 if (error_if_not_eol && c_parser_peek_token (parser)->type != CPP_PRAGMA_EOL)
1216 c_parser_error (parser, "expected end of line");
1218 cpp_ttype token_type;
1221 c_token *token = c_parser_peek_token (parser);
1222 token_type = token->type;
1223 if (token_type == CPP_EOF)
1224 break;
1225 c_parser_consume_token (parser);
1227 while (token_type != CPP_PRAGMA_EOL);
1229 parser->error = false;
1232 /* Skip tokens until we have consumed an entire block, or until we
1233 have consumed a non-nested ';'. */
1235 static void
1236 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
1238 unsigned nesting_depth = 0;
1239 bool save_error = parser->error;
1241 while (true)
1243 c_token *token;
1245 /* Peek at the next token. */
1246 token = c_parser_peek_token (parser);
1248 switch (token->type)
1250 case CPP_EOF:
1251 return;
1253 case CPP_PRAGMA_EOL:
1254 if (parser->in_pragma)
1255 return;
1256 break;
1258 case CPP_SEMICOLON:
1259 /* If the next token is a ';', we have reached the
1260 end of the statement. */
1261 if (!nesting_depth)
1263 /* Consume the ';'. */
1264 c_parser_consume_token (parser);
1265 goto finished;
1267 break;
1269 case CPP_CLOSE_BRACE:
1270 /* If the next token is a non-nested '}', then we have
1271 reached the end of the current block. */
1272 if (nesting_depth == 0 || --nesting_depth == 0)
1274 c_parser_consume_token (parser);
1275 goto finished;
1277 break;
1279 case CPP_OPEN_BRACE:
1280 /* If it the next token is a '{', then we are entering a new
1281 block. Consume the entire block. */
1282 ++nesting_depth;
1283 break;
1285 case CPP_PRAGMA:
1286 /* If we see a pragma, consume the whole thing at once. We
1287 have some safeguards against consuming pragmas willy-nilly.
1288 Normally, we'd expect to be here with parser->error set,
1289 which disables these safeguards. But it's possible to get
1290 here for secondary error recovery, after parser->error has
1291 been cleared. */
1292 c_parser_consume_pragma (parser);
1293 c_parser_skip_to_pragma_eol (parser);
1294 parser->error = save_error;
1295 continue;
1297 default:
1298 break;
1301 c_parser_consume_token (parser);
1304 finished:
1305 parser->error = false;
1308 /* CPP's options (initialized by c-opts.c). */
1309 extern cpp_options *cpp_opts;
1311 /* Save the warning flags which are controlled by __extension__. */
1313 static inline int
1314 disable_extension_diagnostics (void)
1316 int ret = (pedantic
1317 | (warn_pointer_arith << 1)
1318 | (warn_traditional << 2)
1319 | (flag_iso << 3)
1320 | (warn_long_long << 4)
1321 | (warn_cxx_compat << 5)
1322 | (warn_overlength_strings << 6)
1323 /* warn_c90_c99_compat has three states: -1/0/1, so we must
1324 play tricks to properly restore it. */
1325 | ((warn_c90_c99_compat == 1) << 7)
1326 | ((warn_c90_c99_compat == -1) << 8)
1327 /* Similarly for warn_c99_c11_compat. */
1328 | ((warn_c99_c11_compat == 1) << 9)
1329 | ((warn_c99_c11_compat == -1) << 10)
1330 /* Similarly for warn_c11_c2x_compat. */
1331 | ((warn_c11_c2x_compat == 1) << 11)
1332 | ((warn_c11_c2x_compat == -1) << 12)
1334 cpp_opts->cpp_pedantic = pedantic = 0;
1335 warn_pointer_arith = 0;
1336 cpp_opts->cpp_warn_traditional = warn_traditional = 0;
1337 flag_iso = 0;
1338 cpp_opts->cpp_warn_long_long = warn_long_long = 0;
1339 warn_cxx_compat = 0;
1340 warn_overlength_strings = 0;
1341 warn_c90_c99_compat = 0;
1342 warn_c99_c11_compat = 0;
1343 warn_c11_c2x_compat = 0;
1344 return ret;
1347 /* Restore the warning flags which are controlled by __extension__.
1348 FLAGS is the return value from disable_extension_diagnostics. */
1350 static inline void
1351 restore_extension_diagnostics (int flags)
1353 cpp_opts->cpp_pedantic = pedantic = flags & 1;
1354 warn_pointer_arith = (flags >> 1) & 1;
1355 cpp_opts->cpp_warn_traditional = warn_traditional = (flags >> 2) & 1;
1356 flag_iso = (flags >> 3) & 1;
1357 cpp_opts->cpp_warn_long_long = warn_long_long = (flags >> 4) & 1;
1358 warn_cxx_compat = (flags >> 5) & 1;
1359 warn_overlength_strings = (flags >> 6) & 1;
1360 /* See above for why is this needed. */
1361 warn_c90_c99_compat = (flags >> 7) & 1 ? 1 : ((flags >> 8) & 1 ? -1 : 0);
1362 warn_c99_c11_compat = (flags >> 9) & 1 ? 1 : ((flags >> 10) & 1 ? -1 : 0);
1363 warn_c11_c2x_compat = (flags >> 11) & 1 ? 1 : ((flags >> 12) & 1 ? -1 : 0);
1366 /* Helper data structure for parsing #pragma acc routine. */
1367 struct oacc_routine_data {
1368 bool error_seen; /* Set if error has been reported. */
1369 bool fndecl_seen; /* Set if one fn decl/definition has been seen already. */
1370 tree clauses;
1371 location_t loc;
1374 static void c_parser_external_declaration (c_parser *);
1375 static void c_parser_asm_definition (c_parser *);
1376 static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
1377 bool, bool, tree *, vec<c_token>,
1378 struct oacc_routine_data * = NULL,
1379 bool * = NULL);
1380 static void c_parser_static_assert_declaration_no_semi (c_parser *);
1381 static void c_parser_static_assert_declaration (c_parser *);
1382 static struct c_typespec c_parser_enum_specifier (c_parser *);
1383 static struct c_typespec c_parser_struct_or_union_specifier (c_parser *);
1384 static tree c_parser_struct_declaration (c_parser *);
1385 static struct c_typespec c_parser_typeof_specifier (c_parser *);
1386 static tree c_parser_alignas_specifier (c_parser *);
1387 static struct c_declarator *c_parser_direct_declarator (c_parser *, bool,
1388 c_dtr_syn, bool *);
1389 static struct c_declarator *c_parser_direct_declarator_inner (c_parser *,
1390 bool,
1391 struct c_declarator *);
1392 static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree);
1393 static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree,
1394 tree);
1395 static struct c_parm *c_parser_parameter_declaration (c_parser *, tree);
1396 static tree c_parser_simple_asm_expr (c_parser *);
1397 static tree c_parser_attributes (c_parser *);
1398 static struct c_expr c_parser_initializer (c_parser *);
1399 static struct c_expr c_parser_braced_init (c_parser *, tree, bool,
1400 struct obstack *);
1401 static void c_parser_initelt (c_parser *, struct obstack *);
1402 static void c_parser_initval (c_parser *, struct c_expr *,
1403 struct obstack *);
1404 static tree c_parser_compound_statement (c_parser *);
1405 static void c_parser_compound_statement_nostart (c_parser *);
1406 static void c_parser_label (c_parser *);
1407 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
1408 static void c_parser_statement_after_labels (c_parser *, bool *,
1409 vec<tree> * = NULL);
1410 static tree c_parser_c99_block_statement (c_parser *, bool *,
1411 location_t * = NULL);
1412 static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
1413 static void c_parser_switch_statement (c_parser *, bool *);
1414 static void c_parser_while_statement (c_parser *, bool, unsigned short, bool *);
1415 static void c_parser_do_statement (c_parser *, bool, unsigned short);
1416 static void c_parser_for_statement (c_parser *, bool, unsigned short, bool *);
1417 static tree c_parser_asm_statement (c_parser *);
1418 static tree c_parser_asm_operands (c_parser *);
1419 static tree c_parser_asm_goto_operands (c_parser *);
1420 static tree c_parser_asm_clobbers (c_parser *);
1421 static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *,
1422 tree = NULL_TREE);
1423 static struct c_expr c_parser_conditional_expression (c_parser *,
1424 struct c_expr *, tree);
1425 static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *,
1426 tree);
1427 static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *);
1428 static struct c_expr c_parser_unary_expression (c_parser *);
1429 static struct c_expr c_parser_sizeof_expression (c_parser *);
1430 static struct c_expr c_parser_alignof_expression (c_parser *);
1431 static struct c_expr c_parser_postfix_expression (c_parser *);
1432 static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *,
1433 struct c_type_name *,
1434 location_t);
1435 static struct c_expr c_parser_postfix_expression_after_primary (c_parser *,
1436 location_t loc,
1437 struct c_expr);
1438 static tree c_parser_transaction (c_parser *, enum rid);
1439 static struct c_expr c_parser_transaction_expression (c_parser *, enum rid);
1440 static tree c_parser_transaction_cancel (c_parser *);
1441 static struct c_expr c_parser_expression (c_parser *);
1442 static struct c_expr c_parser_expression_conv (c_parser *);
1443 static vec<tree, va_gc> *c_parser_expr_list (c_parser *, bool, bool,
1444 vec<tree, va_gc> **, location_t *,
1445 tree *, vec<location_t> *,
1446 unsigned int * = NULL);
1447 static void c_parser_oacc_declare (c_parser *);
1448 static void c_parser_oacc_enter_exit_data (c_parser *, bool);
1449 static void c_parser_oacc_update (c_parser *);
1450 static void c_parser_omp_construct (c_parser *, bool *);
1451 static void c_parser_omp_threadprivate (c_parser *);
1452 static void c_parser_omp_barrier (c_parser *);
1453 static void c_parser_omp_flush (c_parser *);
1454 static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
1455 tree, tree *, bool *);
1456 static void c_parser_omp_taskwait (c_parser *);
1457 static void c_parser_omp_taskyield (c_parser *);
1458 static void c_parser_omp_cancel (c_parser *);
1460 enum pragma_context { pragma_external, pragma_struct, pragma_param,
1461 pragma_stmt, pragma_compound };
1462 static bool c_parser_pragma (c_parser *, enum pragma_context, bool *);
1463 static void c_parser_omp_cancellation_point (c_parser *, enum pragma_context);
1464 static bool c_parser_omp_target (c_parser *, enum pragma_context, bool *);
1465 static void c_parser_omp_end_declare_target (c_parser *);
1466 static void c_parser_omp_declare (c_parser *, enum pragma_context);
1467 static bool c_parser_omp_ordered (c_parser *, enum pragma_context, bool *);
1468 static void c_parser_oacc_routine (c_parser *, enum pragma_context);
1470 /* These Objective-C parser functions are only ever called when
1471 compiling Objective-C. */
1472 static void c_parser_objc_class_definition (c_parser *, tree);
1473 static void c_parser_objc_class_instance_variables (c_parser *);
1474 static void c_parser_objc_class_declaration (c_parser *);
1475 static void c_parser_objc_alias_declaration (c_parser *);
1476 static void c_parser_objc_protocol_definition (c_parser *, tree);
1477 static bool c_parser_objc_method_type (c_parser *);
1478 static void c_parser_objc_method_definition (c_parser *);
1479 static void c_parser_objc_methodprotolist (c_parser *);
1480 static void c_parser_objc_methodproto (c_parser *);
1481 static tree c_parser_objc_method_decl (c_parser *, bool, tree *, tree *);
1482 static tree c_parser_objc_type_name (c_parser *);
1483 static tree c_parser_objc_protocol_refs (c_parser *);
1484 static void c_parser_objc_try_catch_finally_statement (c_parser *);
1485 static void c_parser_objc_synchronized_statement (c_parser *);
1486 static tree c_parser_objc_selector (c_parser *);
1487 static tree c_parser_objc_selector_arg (c_parser *);
1488 static tree c_parser_objc_receiver (c_parser *);
1489 static tree c_parser_objc_message_args (c_parser *);
1490 static tree c_parser_objc_keywordexpr (c_parser *);
1491 static void c_parser_objc_at_property_declaration (c_parser *);
1492 static void c_parser_objc_at_synthesize_declaration (c_parser *);
1493 static void c_parser_objc_at_dynamic_declaration (c_parser *);
1494 static bool c_parser_objc_diagnose_bad_element_prefix
1495 (c_parser *, struct c_declspecs *);
1497 static void c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass);
1499 /* Parse a translation unit (C90 6.7, C99 6.9, C11 6.9).
1501 translation-unit:
1502 external-declarations
1504 external-declarations:
1505 external-declaration
1506 external-declarations external-declaration
1508 GNU extensions:
1510 translation-unit:
1511 empty
1514 static void
1515 c_parser_translation_unit (c_parser *parser)
1517 if (c_parser_next_token_is (parser, CPP_EOF))
1519 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1520 "ISO C forbids an empty translation unit");
1522 else
1524 void *obstack_position = obstack_alloc (&parser_obstack, 0);
1525 mark_valid_location_for_stdc_pragma (false);
1528 ggc_collect ();
1529 c_parser_external_declaration (parser);
1530 obstack_free (&parser_obstack, obstack_position);
1532 while (c_parser_next_token_is_not (parser, CPP_EOF));
1535 unsigned int i;
1536 tree decl;
1537 FOR_EACH_VEC_ELT (incomplete_record_decls, i, decl)
1538 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node)
1539 error ("storage size of %q+D isn%'t known", decl);
1542 /* Parse an external declaration (C90 6.7, C99 6.9, C11 6.9).
1544 external-declaration:
1545 function-definition
1546 declaration
1548 GNU extensions:
1550 external-declaration:
1551 asm-definition
1553 __extension__ external-declaration
1555 Objective-C:
1557 external-declaration:
1558 objc-class-definition
1559 objc-class-declaration
1560 objc-alias-declaration
1561 objc-protocol-definition
1562 objc-method-definition
1563 @end
1566 static void
1567 c_parser_external_declaration (c_parser *parser)
1569 int ext;
1570 switch (c_parser_peek_token (parser)->type)
1572 case CPP_KEYWORD:
1573 switch (c_parser_peek_token (parser)->keyword)
1575 case RID_EXTENSION:
1576 ext = disable_extension_diagnostics ();
1577 c_parser_consume_token (parser);
1578 c_parser_external_declaration (parser);
1579 restore_extension_diagnostics (ext);
1580 break;
1581 case RID_ASM:
1582 c_parser_asm_definition (parser);
1583 break;
1584 case RID_AT_INTERFACE:
1585 case RID_AT_IMPLEMENTATION:
1586 gcc_assert (c_dialect_objc ());
1587 c_parser_objc_class_definition (parser, NULL_TREE);
1588 break;
1589 case RID_AT_CLASS:
1590 gcc_assert (c_dialect_objc ());
1591 c_parser_objc_class_declaration (parser);
1592 break;
1593 case RID_AT_ALIAS:
1594 gcc_assert (c_dialect_objc ());
1595 c_parser_objc_alias_declaration (parser);
1596 break;
1597 case RID_AT_PROTOCOL:
1598 gcc_assert (c_dialect_objc ());
1599 c_parser_objc_protocol_definition (parser, NULL_TREE);
1600 break;
1601 case RID_AT_PROPERTY:
1602 gcc_assert (c_dialect_objc ());
1603 c_parser_objc_at_property_declaration (parser);
1604 break;
1605 case RID_AT_SYNTHESIZE:
1606 gcc_assert (c_dialect_objc ());
1607 c_parser_objc_at_synthesize_declaration (parser);
1608 break;
1609 case RID_AT_DYNAMIC:
1610 gcc_assert (c_dialect_objc ());
1611 c_parser_objc_at_dynamic_declaration (parser);
1612 break;
1613 case RID_AT_END:
1614 gcc_assert (c_dialect_objc ());
1615 c_parser_consume_token (parser);
1616 objc_finish_implementation ();
1617 break;
1618 default:
1619 goto decl_or_fndef;
1621 break;
1622 case CPP_SEMICOLON:
1623 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
1624 "ISO C does not allow extra %<;%> outside of a function");
1625 c_parser_consume_token (parser);
1626 break;
1627 case CPP_PRAGMA:
1628 mark_valid_location_for_stdc_pragma (true);
1629 c_parser_pragma (parser, pragma_external, NULL);
1630 mark_valid_location_for_stdc_pragma (false);
1631 break;
1632 case CPP_PLUS:
1633 case CPP_MINUS:
1634 if (c_dialect_objc ())
1636 c_parser_objc_method_definition (parser);
1637 break;
1639 /* Else fall through, and yield a syntax error trying to parse
1640 as a declaration or function definition. */
1641 /* FALLTHRU */
1642 default:
1643 decl_or_fndef:
1644 /* A declaration or a function definition (or, in Objective-C,
1645 an @interface or @protocol with prefix attributes). We can
1646 only tell which after parsing the declaration specifiers, if
1647 any, and the first declarator. */
1648 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
1649 NULL, vNULL);
1650 break;
1654 static void c_finish_omp_declare_simd (c_parser *, tree, tree, vec<c_token>);
1655 static void c_finish_oacc_routine (struct oacc_routine_data *, tree, bool);
1657 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
1659 static void
1660 add_debug_begin_stmt (location_t loc)
1662 /* Don't add DEBUG_BEGIN_STMTs outside of functions, see PR84721. */
1663 if (!MAY_HAVE_DEBUG_MARKER_STMTS || !building_stmt_list_p ())
1664 return;
1666 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
1667 SET_EXPR_LOCATION (stmt, loc);
1668 add_stmt (stmt);
1671 /* Parse a declaration or function definition (C90 6.5, 6.7.1, C99
1672 6.7, 6.9.1, C11 6.7, 6.9.1). If FNDEF_OK is true, a function definition
1673 is accepted; otherwise (old-style parameter declarations) only other
1674 declarations are accepted. If STATIC_ASSERT_OK is true, a static
1675 assertion is accepted; otherwise (old-style parameter declarations)
1676 it is not. If NESTED is true, we are inside a function or parsing
1677 old-style parameter declarations; any functions encountered are
1678 nested functions and declaration specifiers are required; otherwise
1679 we are at top level and functions are normal functions and
1680 declaration specifiers may be optional. If EMPTY_OK is true, empty
1681 declarations are OK (subject to all other constraints); otherwise
1682 (old-style parameter declarations) they are diagnosed. If
1683 START_ATTR_OK is true, the declaration specifiers may start with
1684 attributes; otherwise they may not.
1685 OBJC_FOREACH_OBJECT_DECLARATION can be used to get back the parsed
1686 declaration when parsing an Objective-C foreach statement.
1687 FALLTHRU_ATTR_P is used to signal whether this function parsed
1688 "__attribute__((fallthrough));".
1690 declaration:
1691 declaration-specifiers init-declarator-list[opt] ;
1692 static_assert-declaration
1694 function-definition:
1695 declaration-specifiers[opt] declarator declaration-list[opt]
1696 compound-statement
1698 declaration-list:
1699 declaration
1700 declaration-list declaration
1702 init-declarator-list:
1703 init-declarator
1704 init-declarator-list , init-declarator
1706 init-declarator:
1707 declarator simple-asm-expr[opt] attributes[opt]
1708 declarator simple-asm-expr[opt] attributes[opt] = initializer
1710 GNU extensions:
1712 nested-function-definition:
1713 declaration-specifiers declarator declaration-list[opt]
1714 compound-statement
1716 attribute ;
1718 Objective-C:
1719 attributes objc-class-definition
1720 attributes objc-category-definition
1721 attributes objc-protocol-definition
1723 The simple-asm-expr and attributes are GNU extensions.
1725 This function does not handle __extension__; that is handled in its
1726 callers. ??? Following the old parser, __extension__ may start
1727 external declarations, declarations in functions and declarations
1728 at the start of "for" loops, but not old-style parameter
1729 declarations.
1731 C99 requires declaration specifiers in a function definition; the
1732 absence is diagnosed through the diagnosis of implicit int. In GNU
1733 C we also allow but diagnose declarations without declaration
1734 specifiers, but only at top level (elsewhere they conflict with
1735 other syntax).
1737 In Objective-C, declarations of the looping variable in a foreach
1738 statement are exceptionally terminated by 'in' (for example, 'for
1739 (NSObject *object in array) { ... }').
1741 OpenMP:
1743 declaration:
1744 threadprivate-directive
1746 GIMPLE:
1748 gimple-function-definition:
1749 declaration-specifiers[opt] __GIMPLE (gimple-or-rtl-pass-list) declarator
1750 declaration-list[opt] compound-statement
1752 rtl-function-definition:
1753 declaration-specifiers[opt] __RTL (gimple-or-rtl-pass-list) declarator
1754 declaration-list[opt] compound-statement */
1756 static void
1757 c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok,
1758 bool static_assert_ok, bool empty_ok,
1759 bool nested, bool start_attr_ok,
1760 tree *objc_foreach_object_declaration,
1761 vec<c_token> omp_declare_simd_clauses,
1762 struct oacc_routine_data *oacc_routine_data,
1763 bool *fallthru_attr_p)
1765 struct c_declspecs *specs;
1766 tree prefix_attrs;
1767 tree all_prefix_attrs;
1768 bool diagnosed_no_specs = false;
1769 location_t here = c_parser_peek_token (parser)->location;
1771 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
1773 if (static_assert_ok
1774 && c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
1776 c_parser_static_assert_declaration (parser);
1777 return;
1779 specs = build_null_declspecs ();
1781 /* Try to detect an unknown type name when we have "A B" or "A *B". */
1782 if (c_parser_peek_token (parser)->type == CPP_NAME
1783 && c_parser_peek_token (parser)->id_kind == C_ID_ID
1784 && (c_parser_peek_2nd_token (parser)->type == CPP_NAME
1785 || c_parser_peek_2nd_token (parser)->type == CPP_MULT)
1786 && (!nested || !lookup_name (c_parser_peek_token (parser)->value)))
1788 tree name = c_parser_peek_token (parser)->value;
1790 /* Issue a warning about NAME being an unknown type name, perhaps
1791 with some kind of hint.
1792 If the user forgot a "struct" etc, suggest inserting
1793 it. Otherwise, attempt to look for misspellings. */
1794 gcc_rich_location richloc (here);
1795 if (tag_exists_p (RECORD_TYPE, name))
1797 /* This is not C++ with its implicit typedef. */
1798 richloc.add_fixit_insert_before ("struct ");
1799 error_at (&richloc,
1800 "unknown type name %qE;"
1801 " use %<struct%> keyword to refer to the type",
1802 name);
1804 else if (tag_exists_p (UNION_TYPE, name))
1806 richloc.add_fixit_insert_before ("union ");
1807 error_at (&richloc,
1808 "unknown type name %qE;"
1809 " use %<union%> keyword to refer to the type",
1810 name);
1812 else if (tag_exists_p (ENUMERAL_TYPE, name))
1814 richloc.add_fixit_insert_before ("enum ");
1815 error_at (&richloc,
1816 "unknown type name %qE;"
1817 " use %<enum%> keyword to refer to the type",
1818 name);
1820 else
1822 auto_diagnostic_group d;
1823 name_hint hint = lookup_name_fuzzy (name, FUZZY_LOOKUP_TYPENAME,
1824 here);
1825 if (const char *suggestion = hint.suggestion ())
1827 richloc.add_fixit_replace (suggestion);
1828 error_at (&richloc,
1829 "unknown type name %qE; did you mean %qs?",
1830 name, suggestion);
1832 else
1833 error_at (here, "unknown type name %qE", name);
1836 /* Parse declspecs normally to get a correct pointer type, but avoid
1837 a further "fails to be a type name" error. Refuse nested functions
1838 since it is not how the user likely wants us to recover. */
1839 c_parser_peek_token (parser)->type = CPP_KEYWORD;
1840 c_parser_peek_token (parser)->keyword = RID_VOID;
1841 c_parser_peek_token (parser)->value = error_mark_node;
1842 fndef_ok = !nested;
1845 c_parser_declspecs (parser, specs, true, true, start_attr_ok,
1846 true, true, cla_nonabstract_decl);
1847 if (parser->error)
1849 c_parser_skip_to_end_of_block_or_statement (parser);
1850 return;
1852 if (nested && !specs->declspecs_seen_p)
1854 c_parser_error (parser, "expected declaration specifiers");
1855 c_parser_skip_to_end_of_block_or_statement (parser);
1856 return;
1859 finish_declspecs (specs);
1860 bool auto_type_p = specs->typespec_word == cts_auto_type;
1861 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
1863 if (auto_type_p)
1864 error_at (here, "%<__auto_type%> in empty declaration");
1865 else if (specs->typespec_kind == ctsk_none
1866 && attribute_fallthrough_p (specs->attrs))
1868 if (fallthru_attr_p != NULL)
1869 *fallthru_attr_p = true;
1870 tree fn = build_call_expr_internal_loc (here, IFN_FALLTHROUGH,
1871 void_type_node, 0);
1872 add_stmt (fn);
1874 else if (empty_ok)
1875 shadow_tag (specs);
1876 else
1878 shadow_tag_warned (specs, 1);
1879 pedwarn (here, 0, "empty declaration");
1881 c_parser_consume_token (parser);
1882 if (oacc_routine_data)
1883 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
1884 return;
1887 /* Provide better error recovery. Note that a type name here is usually
1888 better diagnosed as a redeclaration. */
1889 if (empty_ok
1890 && specs->typespec_kind == ctsk_tagdef
1891 && c_parser_next_token_starts_declspecs (parser)
1892 && !c_parser_next_token_is (parser, CPP_NAME))
1894 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
1895 parser->error = false;
1896 shadow_tag_warned (specs, 1);
1897 return;
1899 else if (c_dialect_objc () && !auto_type_p)
1901 /* Prefix attributes are an error on method decls. */
1902 switch (c_parser_peek_token (parser)->type)
1904 case CPP_PLUS:
1905 case CPP_MINUS:
1906 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1907 return;
1908 if (specs->attrs)
1910 warning_at (c_parser_peek_token (parser)->location,
1911 OPT_Wattributes,
1912 "prefix attributes are ignored for methods");
1913 specs->attrs = NULL_TREE;
1915 if (fndef_ok)
1916 c_parser_objc_method_definition (parser);
1917 else
1918 c_parser_objc_methodproto (parser);
1919 return;
1920 break;
1921 default:
1922 break;
1924 /* This is where we parse 'attributes @interface ...',
1925 'attributes @implementation ...', 'attributes @protocol ...'
1926 (where attributes could be, for example, __attribute__
1927 ((deprecated)).
1929 switch (c_parser_peek_token (parser)->keyword)
1931 case RID_AT_INTERFACE:
1933 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1934 return;
1935 c_parser_objc_class_definition (parser, specs->attrs);
1936 return;
1938 break;
1939 case RID_AT_IMPLEMENTATION:
1941 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1942 return;
1943 if (specs->attrs)
1945 warning_at (c_parser_peek_token (parser)->location,
1946 OPT_Wattributes,
1947 "prefix attributes are ignored for implementations");
1948 specs->attrs = NULL_TREE;
1950 c_parser_objc_class_definition (parser, NULL_TREE);
1951 return;
1953 break;
1954 case RID_AT_PROTOCOL:
1956 if (c_parser_objc_diagnose_bad_element_prefix (parser, specs))
1957 return;
1958 c_parser_objc_protocol_definition (parser, specs->attrs);
1959 return;
1961 break;
1962 case RID_AT_ALIAS:
1963 case RID_AT_CLASS:
1964 case RID_AT_END:
1965 case RID_AT_PROPERTY:
1966 if (specs->attrs)
1968 c_parser_error (parser, "unexpected attribute");
1969 specs->attrs = NULL;
1971 break;
1972 default:
1973 break;
1976 else if (attribute_fallthrough_p (specs->attrs))
1977 warning_at (here, OPT_Wattributes,
1978 "%<fallthrough%> attribute not followed by %<;%>");
1980 pending_xref_error ();
1981 prefix_attrs = specs->attrs;
1982 all_prefix_attrs = prefix_attrs;
1983 specs->attrs = NULL_TREE;
1984 while (true)
1986 struct c_declarator *declarator;
1987 bool dummy = false;
1988 timevar_id_t tv;
1989 tree fnbody = NULL_TREE;
1990 /* Declaring either one or more declarators (in which case we
1991 should diagnose if there were no declaration specifiers) or a
1992 function definition (in which case the diagnostic for
1993 implicit int suffices). */
1994 declarator = c_parser_declarator (parser,
1995 specs->typespec_kind != ctsk_none,
1996 C_DTR_NORMAL, &dummy);
1997 if (declarator == NULL)
1999 if (omp_declare_simd_clauses.exists ())
2000 c_finish_omp_declare_simd (parser, NULL_TREE, NULL_TREE,
2001 omp_declare_simd_clauses);
2002 if (oacc_routine_data)
2003 c_finish_oacc_routine (oacc_routine_data, NULL_TREE, false);
2004 c_parser_skip_to_end_of_block_or_statement (parser);
2005 return;
2007 if (auto_type_p && declarator->kind != cdk_id)
2009 error_at (here,
2010 "%<__auto_type%> requires a plain identifier"
2011 " as declarator");
2012 c_parser_skip_to_end_of_block_or_statement (parser);
2013 return;
2015 if (c_parser_next_token_is (parser, CPP_EQ)
2016 || c_parser_next_token_is (parser, CPP_COMMA)
2017 || c_parser_next_token_is (parser, CPP_SEMICOLON)
2018 || c_parser_next_token_is_keyword (parser, RID_ASM)
2019 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)
2020 || c_parser_next_token_is_keyword (parser, RID_IN))
2022 tree asm_name = NULL_TREE;
2023 tree postfix_attrs = NULL_TREE;
2024 if (!diagnosed_no_specs && !specs->declspecs_seen_p)
2026 diagnosed_no_specs = true;
2027 pedwarn (here, 0, "data definition has no type or storage class");
2029 /* Having seen a data definition, there cannot now be a
2030 function definition. */
2031 fndef_ok = false;
2032 if (c_parser_next_token_is_keyword (parser, RID_ASM))
2033 asm_name = c_parser_simple_asm_expr (parser);
2034 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2036 postfix_attrs = c_parser_attributes (parser);
2037 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2039 /* This means there is an attribute specifier after
2040 the declarator in a function definition. Provide
2041 some more information for the user. */
2042 error_at (here, "attributes should be specified before the "
2043 "declarator in a function definition");
2044 c_parser_skip_to_end_of_block_or_statement (parser);
2045 return;
2048 if (c_parser_next_token_is (parser, CPP_EQ))
2050 tree d;
2051 struct c_expr init;
2052 location_t init_loc;
2053 c_parser_consume_token (parser);
2054 if (auto_type_p)
2056 init_loc = c_parser_peek_token (parser)->location;
2057 rich_location richloc (line_table, init_loc);
2058 start_init (NULL_TREE, asm_name, global_bindings_p (), &richloc);
2059 /* A parameter is initialized, which is invalid. Don't
2060 attempt to instrument the initializer. */
2061 int flag_sanitize_save = flag_sanitize;
2062 if (nested && !empty_ok)
2063 flag_sanitize = 0;
2064 init = c_parser_expr_no_commas (parser, NULL);
2065 flag_sanitize = flag_sanitize_save;
2066 if (TREE_CODE (init.value) == COMPONENT_REF
2067 && DECL_C_BIT_FIELD (TREE_OPERAND (init.value, 1)))
2068 error_at (here,
2069 "%<__auto_type%> used with a bit-field"
2070 " initializer");
2071 init = convert_lvalue_to_rvalue (init_loc, init, true, true);
2072 tree init_type = TREE_TYPE (init.value);
2073 /* As with typeof, remove all qualifiers from atomic types. */
2074 if (init_type != error_mark_node && TYPE_ATOMIC (init_type))
2075 init_type
2076 = c_build_qualified_type (init_type, TYPE_UNQUALIFIED);
2077 bool vm_type = variably_modified_type_p (init_type,
2078 NULL_TREE);
2079 if (vm_type)
2080 init.value = save_expr (init.value);
2081 finish_init ();
2082 specs->typespec_kind = ctsk_typeof;
2083 specs->locations[cdw_typedef] = init_loc;
2084 specs->typedef_p = true;
2085 specs->type = init_type;
2086 if (vm_type)
2088 bool maybe_const = true;
2089 tree type_expr = c_fully_fold (init.value, false,
2090 &maybe_const);
2091 specs->expr_const_operands &= maybe_const;
2092 if (specs->expr)
2093 specs->expr = build2 (COMPOUND_EXPR,
2094 TREE_TYPE (type_expr),
2095 specs->expr, type_expr);
2096 else
2097 specs->expr = type_expr;
2099 d = start_decl (declarator, specs, true,
2100 chainon (postfix_attrs, all_prefix_attrs));
2101 if (!d)
2102 d = error_mark_node;
2103 if (omp_declare_simd_clauses.exists ())
2104 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2105 omp_declare_simd_clauses);
2107 else
2109 /* The declaration of the variable is in effect while
2110 its initializer is parsed. */
2111 d = start_decl (declarator, specs, true,
2112 chainon (postfix_attrs, all_prefix_attrs));
2113 if (!d)
2114 d = error_mark_node;
2115 if (omp_declare_simd_clauses.exists ())
2116 c_finish_omp_declare_simd (parser, d, NULL_TREE,
2117 omp_declare_simd_clauses);
2118 init_loc = c_parser_peek_token (parser)->location;
2119 rich_location richloc (line_table, init_loc);
2120 start_init (d, asm_name, global_bindings_p (), &richloc);
2121 /* A parameter is initialized, which is invalid. Don't
2122 attempt to instrument the initializer. */
2123 int flag_sanitize_save = flag_sanitize;
2124 if (TREE_CODE (d) == PARM_DECL)
2125 flag_sanitize = 0;
2126 init = c_parser_initializer (parser);
2127 flag_sanitize = flag_sanitize_save;
2128 finish_init ();
2130 if (oacc_routine_data)
2131 c_finish_oacc_routine (oacc_routine_data, d, false);
2132 if (d != error_mark_node)
2134 maybe_warn_string_init (init_loc, TREE_TYPE (d), init);
2135 finish_decl (d, init_loc, init.value,
2136 init.original_type, asm_name);
2139 else
2141 if (auto_type_p)
2143 error_at (here,
2144 "%<__auto_type%> requires an initialized "
2145 "data declaration");
2146 c_parser_skip_to_end_of_block_or_statement (parser);
2147 return;
2149 tree d = start_decl (declarator, specs, false,
2150 chainon (postfix_attrs,
2151 all_prefix_attrs));
2152 if (d && TREE_CODE (d) == FUNCTION_DECL)
2153 if (declarator->kind == cdk_function)
2154 if (DECL_ARGUMENTS (d) == NULL_TREE)
2155 DECL_ARGUMENTS (d) = declarator->u.arg_info->parms;
2156 if (omp_declare_simd_clauses.exists ())
2158 tree parms = NULL_TREE;
2159 if (d && TREE_CODE (d) == FUNCTION_DECL)
2161 struct c_declarator *ce = declarator;
2162 while (ce != NULL)
2163 if (ce->kind == cdk_function)
2165 parms = ce->u.arg_info->parms;
2166 break;
2168 else
2169 ce = ce->declarator;
2171 if (parms)
2172 temp_store_parm_decls (d, parms);
2173 c_finish_omp_declare_simd (parser, d, parms,
2174 omp_declare_simd_clauses);
2175 if (parms)
2176 temp_pop_parm_decls ();
2178 if (oacc_routine_data)
2179 c_finish_oacc_routine (oacc_routine_data, d, false);
2180 if (d)
2181 finish_decl (d, UNKNOWN_LOCATION, NULL_TREE,
2182 NULL_TREE, asm_name);
2184 if (c_parser_next_token_is_keyword (parser, RID_IN))
2186 if (d)
2187 *objc_foreach_object_declaration = d;
2188 else
2189 *objc_foreach_object_declaration = error_mark_node;
2192 if (c_parser_next_token_is (parser, CPP_COMMA))
2194 if (auto_type_p)
2196 error_at (here,
2197 "%<__auto_type%> may only be used with"
2198 " a single declarator");
2199 c_parser_skip_to_end_of_block_or_statement (parser);
2200 return;
2202 c_parser_consume_token (parser);
2203 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
2204 all_prefix_attrs = chainon (c_parser_attributes (parser),
2205 prefix_attrs);
2206 else
2207 all_prefix_attrs = prefix_attrs;
2208 continue;
2210 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
2212 c_parser_consume_token (parser);
2213 return;
2215 else if (c_parser_next_token_is_keyword (parser, RID_IN))
2217 /* This can only happen in Objective-C: we found the
2218 'in' that terminates the declaration inside an
2219 Objective-C foreach statement. Do not consume the
2220 token, so that the caller can use it to determine
2221 that this indeed is a foreach context. */
2222 return;
2224 else
2226 c_parser_error (parser, "expected %<,%> or %<;%>");
2227 c_parser_skip_to_end_of_block_or_statement (parser);
2228 return;
2231 else if (auto_type_p)
2233 error_at (here,
2234 "%<__auto_type%> requires an initialized data declaration");
2235 c_parser_skip_to_end_of_block_or_statement (parser);
2236 return;
2238 else if (!fndef_ok)
2240 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, "
2241 "%<asm%> or %<__attribute__%>");
2242 c_parser_skip_to_end_of_block_or_statement (parser);
2243 return;
2245 /* Function definition (nested or otherwise). */
2246 if (nested)
2248 pedwarn (here, OPT_Wpedantic, "ISO C forbids nested functions");
2249 c_push_function_context ();
2251 if (!start_function (specs, declarator, all_prefix_attrs))
2253 /* At this point we've consumed:
2254 declaration-specifiers declarator
2255 and the next token isn't CPP_EQ, CPP_COMMA, CPP_SEMICOLON,
2256 RID_ASM, RID_ATTRIBUTE, or RID_IN,
2257 but the
2258 declaration-specifiers declarator
2259 aren't grokkable as a function definition, so we have
2260 an error. */
2261 gcc_assert (!c_parser_next_token_is (parser, CPP_SEMICOLON));
2262 if (c_parser_next_token_starts_declspecs (parser))
2264 /* If we have
2265 declaration-specifiers declarator decl-specs
2266 then assume we have a missing semicolon, which would
2267 give us:
2268 declaration-specifiers declarator decl-specs
2271 <~~~~~~~~~ declaration ~~~~~~~~~~>
2272 Use c_parser_require to get an error with a fix-it hint. */
2273 c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>");
2274 parser->error = false;
2276 else
2278 /* This can appear in many cases looking nothing like a
2279 function definition, so we don't give a more specific
2280 error suggesting there was one. */
2281 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> "
2282 "or %<__attribute__%>");
2284 if (nested)
2285 c_pop_function_context ();
2286 break;
2289 if (DECL_DECLARED_INLINE_P (current_function_decl))
2290 tv = TV_PARSE_INLINE;
2291 else
2292 tv = TV_PARSE_FUNC;
2293 auto_timevar at (g_timer, tv);
2295 /* Parse old-style parameter declarations. ??? Attributes are
2296 not allowed to start declaration specifiers here because of a
2297 syntax conflict between a function declaration with attribute
2298 suffix and a function definition with an attribute prefix on
2299 first old-style parameter declaration. Following the old
2300 parser, they are not accepted on subsequent old-style
2301 parameter declarations either. However, there is no
2302 ambiguity after the first declaration, nor indeed on the
2303 first as long as we don't allow postfix attributes after a
2304 declarator with a nonempty identifier list in a definition;
2305 and postfix attributes have never been accepted here in
2306 function definitions either. */
2307 while (c_parser_next_token_is_not (parser, CPP_EOF)
2308 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE))
2309 c_parser_declaration_or_fndef (parser, false, false, false,
2310 true, false, NULL, vNULL);
2311 store_parm_decls ();
2312 if (omp_declare_simd_clauses.exists ())
2313 c_finish_omp_declare_simd (parser, current_function_decl, NULL_TREE,
2314 omp_declare_simd_clauses);
2315 if (oacc_routine_data)
2316 c_finish_oacc_routine (oacc_routine_data, current_function_decl, true);
2317 DECL_STRUCT_FUNCTION (current_function_decl)->function_start_locus
2318 = c_parser_peek_token (parser)->location;
2320 /* If the definition was marked with __GIMPLE then parse the
2321 function body as GIMPLE. */
2322 if (specs->gimple_p)
2324 cfun->pass_startwith = specs->gimple_or_rtl_pass;
2325 bool saved = in_late_binary_op;
2326 in_late_binary_op = true;
2327 c_parser_parse_gimple_body (parser);
2328 in_late_binary_op = saved;
2330 /* Similarly, if it was marked with __RTL, use the RTL parser now,
2331 consuming the function body. */
2332 else if (specs->rtl_p)
2334 c_parser_parse_rtl_body (parser, specs->gimple_or_rtl_pass);
2336 /* Normally, store_parm_decls sets next_is_function_body,
2337 anticipating a function body. We need a push_scope/pop_scope
2338 pair to flush out this state, or subsequent function parsing
2339 will go wrong. */
2340 push_scope ();
2341 pop_scope ();
2343 finish_function ();
2344 return;
2346 else
2347 fnbody = c_parser_compound_statement (parser);
2348 tree fndecl = current_function_decl;
2349 if (nested)
2351 tree decl = current_function_decl;
2352 /* Mark nested functions as needing static-chain initially.
2353 lower_nested_functions will recompute it but the
2354 DECL_STATIC_CHAIN flag is also used before that happens,
2355 by initializer_constant_valid_p. See gcc.dg/nested-fn-2.c. */
2356 DECL_STATIC_CHAIN (decl) = 1;
2357 add_stmt (fnbody);
2358 finish_function ();
2359 c_pop_function_context ();
2360 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
2362 else
2364 if (fnbody)
2365 add_stmt (fnbody);
2366 finish_function ();
2368 /* Get rid of the empty stmt list for GIMPLE. */
2369 if (specs->gimple_p)
2370 DECL_SAVED_TREE (fndecl) = NULL_TREE;
2372 break;
2376 /* Parse an asm-definition (asm() outside a function body). This is a
2377 GNU extension.
2379 asm-definition:
2380 simple-asm-expr ;
2383 static void
2384 c_parser_asm_definition (c_parser *parser)
2386 tree asm_str = c_parser_simple_asm_expr (parser);
2387 if (asm_str)
2388 symtab->finalize_toplevel_asm (asm_str);
2389 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
2392 /* Parse a static assertion (C11 6.7.10).
2394 static_assert-declaration:
2395 static_assert-declaration-no-semi ;
2398 static void
2399 c_parser_static_assert_declaration (c_parser *parser)
2401 c_parser_static_assert_declaration_no_semi (parser);
2402 if (parser->error
2403 || !c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
2404 c_parser_skip_to_end_of_block_or_statement (parser);
2407 /* Parse a static assertion (C11 6.7.10), without the trailing
2408 semicolon.
2410 static_assert-declaration-no-semi:
2411 _Static_assert ( constant-expression , string-literal )
2413 C2X:
2414 static_assert-declaration-no-semi:
2415 _Static_assert ( constant-expression )
2418 static void
2419 c_parser_static_assert_declaration_no_semi (c_parser *parser)
2421 location_t assert_loc, value_loc;
2422 tree value;
2423 tree string = NULL_TREE;
2425 gcc_assert (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT));
2426 assert_loc = c_parser_peek_token (parser)->location;
2427 if (flag_isoc99)
2428 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2429 "ISO C99 does not support %<_Static_assert%>");
2430 else
2431 pedwarn_c99 (assert_loc, OPT_Wpedantic,
2432 "ISO C90 does not support %<_Static_assert%>");
2433 c_parser_consume_token (parser);
2434 matching_parens parens;
2435 if (!parens.require_open (parser))
2436 return;
2437 location_t value_tok_loc = c_parser_peek_token (parser)->location;
2438 value = c_parser_expr_no_commas (parser, NULL).value;
2439 value_loc = EXPR_LOC_OR_LOC (value, value_tok_loc);
2440 parser->lex_untranslated_string = true;
2441 if (c_parser_next_token_is (parser, CPP_COMMA))
2443 c_parser_consume_token (parser);
2444 switch (c_parser_peek_token (parser)->type)
2446 case CPP_STRING:
2447 case CPP_STRING16:
2448 case CPP_STRING32:
2449 case CPP_WSTRING:
2450 case CPP_UTF8STRING:
2451 string = c_parser_peek_token (parser)->value;
2452 c_parser_consume_token (parser);
2453 parser->lex_untranslated_string = false;
2454 break;
2455 default:
2456 c_parser_error (parser, "expected string literal");
2457 parser->lex_untranslated_string = false;
2458 return;
2461 else if (flag_isoc11)
2462 /* If pedantic for pre-C11, the use of _Static_assert itself will
2463 have been diagnosed, so do not also diagnose the use of this
2464 new C2X feature of _Static_assert. */
2465 pedwarn_c11 (assert_loc, OPT_Wpedantic,
2466 "ISO C11 does not support omitting the string in "
2467 "%<_Static_assert%>");
2468 parens.require_close (parser);
2470 if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
2472 error_at (value_loc, "expression in static assertion is not an integer");
2473 return;
2475 if (TREE_CODE (value) != INTEGER_CST)
2477 value = c_fully_fold (value, false, NULL);
2478 /* Strip no-op conversions. */
2479 STRIP_TYPE_NOPS (value);
2480 if (TREE_CODE (value) == INTEGER_CST)
2481 pedwarn (value_loc, OPT_Wpedantic, "expression in static assertion "
2482 "is not an integer constant expression");
2484 if (TREE_CODE (value) != INTEGER_CST)
2486 error_at (value_loc, "expression in static assertion is not constant");
2487 return;
2489 constant_expression_warning (value);
2490 if (integer_zerop (value))
2492 if (string)
2493 error_at (assert_loc, "static assertion failed: %E", string);
2494 else
2495 error_at (assert_loc, "static assertion failed");
2499 /* Parse some declaration specifiers (possibly none) (C90 6.5, C99
2500 6.7, C11 6.7), adding them to SPECS (which may already include some).
2501 Storage class specifiers are accepted iff SCSPEC_OK; type
2502 specifiers are accepted iff TYPESPEC_OK; alignment specifiers are
2503 accepted iff ALIGNSPEC_OK; attributes are accepted at the start
2504 iff START_ATTR_OK; __auto_type is accepted iff AUTO_TYPE_OK.
2506 declaration-specifiers:
2507 storage-class-specifier declaration-specifiers[opt]
2508 type-specifier declaration-specifiers[opt]
2509 type-qualifier declaration-specifiers[opt]
2510 function-specifier declaration-specifiers[opt]
2511 alignment-specifier declaration-specifiers[opt]
2513 Function specifiers (inline) are from C99, and are currently
2514 handled as storage class specifiers, as is __thread. Alignment
2515 specifiers are from C11.
2517 C90 6.5.1, C99 6.7.1, C11 6.7.1:
2518 storage-class-specifier:
2519 typedef
2520 extern
2521 static
2522 auto
2523 register
2524 _Thread_local
2526 (_Thread_local is new in C11.)
2528 C99 6.7.4, C11 6.7.4:
2529 function-specifier:
2530 inline
2531 _Noreturn
2533 (_Noreturn is new in C11.)
2535 C90 6.5.2, C99 6.7.2, C11 6.7.2:
2536 type-specifier:
2537 void
2538 char
2539 short
2541 long
2542 float
2543 double
2544 signed
2545 unsigned
2546 _Bool
2547 _Complex
2548 [_Imaginary removed in C99 TC2]
2549 struct-or-union-specifier
2550 enum-specifier
2551 typedef-name
2552 atomic-type-specifier
2554 (_Bool and _Complex are new in C99.)
2555 (atomic-type-specifier is new in C11.)
2557 C90 6.5.3, C99 6.7.3, C11 6.7.3:
2559 type-qualifier:
2560 const
2561 restrict
2562 volatile
2563 address-space-qualifier
2564 _Atomic
2566 (restrict is new in C99.)
2567 (_Atomic is new in C11.)
2569 GNU extensions:
2571 declaration-specifiers:
2572 attributes declaration-specifiers[opt]
2574 type-qualifier:
2575 address-space
2577 address-space:
2578 identifier recognized by the target
2580 storage-class-specifier:
2581 __thread
2583 type-specifier:
2584 typeof-specifier
2585 __auto_type
2586 __intN
2587 _Decimal32
2588 _Decimal64
2589 _Decimal128
2590 _Fract
2591 _Accum
2592 _Sat
2594 (_Fract, _Accum, and _Sat are new from ISO/IEC DTR 18037:
2595 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
2597 atomic-type-specifier
2598 _Atomic ( type-name )
2600 Objective-C:
2602 type-specifier:
2603 class-name objc-protocol-refs[opt]
2604 typedef-name objc-protocol-refs
2605 objc-protocol-refs
2608 void
2609 c_parser_declspecs (c_parser *parser, struct c_declspecs *specs,
2610 bool scspec_ok, bool typespec_ok, bool start_attr_ok,
2611 bool alignspec_ok, bool auto_type_ok,
2612 enum c_lookahead_kind la)
2614 bool attrs_ok = start_attr_ok;
2615 bool seen_type = specs->typespec_kind != ctsk_none;
2617 if (!typespec_ok)
2618 gcc_assert (la == cla_prefer_id);
2620 while (c_parser_next_token_is (parser, CPP_NAME)
2621 || c_parser_next_token_is (parser, CPP_KEYWORD)
2622 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS)))
2624 struct c_typespec t;
2625 tree attrs;
2626 tree align;
2627 location_t loc = c_parser_peek_token (parser)->location;
2629 /* If we cannot accept a type, exit if the next token must start
2630 one. Also, if we already have seen a tagged definition,
2631 a typename would be an error anyway and likely the user
2632 has simply forgotten a semicolon, so we exit. */
2633 if ((!typespec_ok || specs->typespec_kind == ctsk_tagdef)
2634 && c_parser_next_tokens_start_typename (parser, la)
2635 && !c_parser_next_token_is_qualifier (parser)
2636 && !c_parser_next_token_is_keyword (parser, RID_ALIGNAS))
2637 break;
2639 if (c_parser_next_token_is (parser, CPP_NAME))
2641 c_token *name_token = c_parser_peek_token (parser);
2642 tree value = name_token->value;
2643 c_id_kind kind = name_token->id_kind;
2645 if (kind == C_ID_ADDRSPACE)
2647 addr_space_t as
2648 = name_token->keyword - RID_FIRST_ADDR_SPACE;
2649 declspecs_add_addrspace (name_token->location, specs, as);
2650 c_parser_consume_token (parser);
2651 attrs_ok = true;
2652 continue;
2655 gcc_assert (!c_parser_next_token_is_qualifier (parser));
2657 /* If we cannot accept a type, and the next token must start one,
2658 exit. Do the same if we already have seen a tagged definition,
2659 since it would be an error anyway and likely the user has simply
2660 forgotten a semicolon. */
2661 if (seen_type || !c_parser_next_tokens_start_typename (parser, la))
2662 break;
2664 /* Now at an unknown typename (C_ID_ID), a C_ID_TYPENAME or
2665 a C_ID_CLASSNAME. */
2666 c_parser_consume_token (parser);
2667 seen_type = true;
2668 attrs_ok = true;
2669 if (kind == C_ID_ID)
2671 error_at (loc, "unknown type name %qE", value);
2672 t.kind = ctsk_typedef;
2673 t.spec = error_mark_node;
2675 else if (kind == C_ID_TYPENAME
2676 && (!c_dialect_objc ()
2677 || c_parser_next_token_is_not (parser, CPP_LESS)))
2679 t.kind = ctsk_typedef;
2680 /* For a typedef name, record the meaning, not the name.
2681 In case of 'foo foo, bar;'. */
2682 t.spec = lookup_name (value);
2684 else
2686 tree proto = NULL_TREE;
2687 gcc_assert (c_dialect_objc ());
2688 t.kind = ctsk_objc;
2689 if (c_parser_next_token_is (parser, CPP_LESS))
2690 proto = c_parser_objc_protocol_refs (parser);
2691 t.spec = objc_get_protocol_qualified_type (value, proto);
2693 t.expr = NULL_TREE;
2694 t.expr_const_operands = true;
2695 declspecs_add_type (name_token->location, specs, t);
2696 continue;
2698 if (c_parser_next_token_is (parser, CPP_LESS))
2700 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" -
2701 nisse@lysator.liu.se. */
2702 tree proto;
2703 gcc_assert (c_dialect_objc ());
2704 if (!typespec_ok || seen_type)
2705 break;
2706 proto = c_parser_objc_protocol_refs (parser);
2707 t.kind = ctsk_objc;
2708 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto);
2709 t.expr = NULL_TREE;
2710 t.expr_const_operands = true;
2711 declspecs_add_type (loc, specs, t);
2712 continue;
2714 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD));
2715 switch (c_parser_peek_token (parser)->keyword)
2717 case RID_STATIC:
2718 case RID_EXTERN:
2719 case RID_REGISTER:
2720 case RID_TYPEDEF:
2721 case RID_INLINE:
2722 case RID_NORETURN:
2723 case RID_AUTO:
2724 case RID_THREAD:
2725 if (!scspec_ok)
2726 goto out;
2727 attrs_ok = true;
2728 /* TODO: Distinguish between function specifiers (inline, noreturn)
2729 and storage class specifiers, either here or in
2730 declspecs_add_scspec. */
2731 declspecs_add_scspec (loc, specs,
2732 c_parser_peek_token (parser)->value);
2733 c_parser_consume_token (parser);
2734 break;
2735 case RID_AUTO_TYPE:
2736 if (!auto_type_ok)
2737 goto out;
2738 /* Fall through. */
2739 case RID_UNSIGNED:
2740 case RID_LONG:
2741 case RID_SHORT:
2742 case RID_SIGNED:
2743 case RID_COMPLEX:
2744 case RID_INT:
2745 case RID_CHAR:
2746 case RID_FLOAT:
2747 case RID_DOUBLE:
2748 case RID_VOID:
2749 case RID_DFLOAT32:
2750 case RID_DFLOAT64:
2751 case RID_DFLOAT128:
2752 CASE_RID_FLOATN_NX:
2753 case RID_BOOL:
2754 case RID_FRACT:
2755 case RID_ACCUM:
2756 case RID_SAT:
2757 case RID_INT_N_0:
2758 case RID_INT_N_1:
2759 case RID_INT_N_2:
2760 case RID_INT_N_3:
2761 if (!typespec_ok)
2762 goto out;
2763 attrs_ok = true;
2764 seen_type = true;
2765 if (c_dialect_objc ())
2766 parser->objc_need_raw_identifier = true;
2767 t.kind = ctsk_resword;
2768 t.spec = c_parser_peek_token (parser)->value;
2769 t.expr = NULL_TREE;
2770 t.expr_const_operands = true;
2771 declspecs_add_type (loc, specs, t);
2772 c_parser_consume_token (parser);
2773 break;
2774 case RID_ENUM:
2775 if (!typespec_ok)
2776 goto out;
2777 attrs_ok = true;
2778 seen_type = true;
2779 t = c_parser_enum_specifier (parser);
2780 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2781 declspecs_add_type (loc, specs, t);
2782 break;
2783 case RID_STRUCT:
2784 case RID_UNION:
2785 if (!typespec_ok)
2786 goto out;
2787 attrs_ok = true;
2788 seen_type = true;
2789 t = c_parser_struct_or_union_specifier (parser);
2790 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, t.spec);
2791 declspecs_add_type (loc, specs, t);
2792 break;
2793 case RID_TYPEOF:
2794 /* ??? The old parser rejected typeof after other type
2795 specifiers, but is a syntax error the best way of
2796 handling this? */
2797 if (!typespec_ok || seen_type)
2798 goto out;
2799 attrs_ok = true;
2800 seen_type = true;
2801 t = c_parser_typeof_specifier (parser);
2802 declspecs_add_type (loc, specs, t);
2803 break;
2804 case RID_ATOMIC:
2805 /* C parser handling of Objective-C constructs needs
2806 checking for correct lvalue-to-rvalue conversions, and
2807 the code in build_modify_expr handling various
2808 Objective-C cases, and that in build_unary_op handling
2809 Objective-C cases for increment / decrement, also needs
2810 updating; uses of TYPE_MAIN_VARIANT in objc_compare_types
2811 and objc_types_are_equivalent may also need updates. */
2812 if (c_dialect_objc ())
2813 sorry ("%<_Atomic%> in Objective-C");
2814 if (flag_isoc99)
2815 pedwarn_c99 (loc, OPT_Wpedantic,
2816 "ISO C99 does not support the %<_Atomic%> qualifier");
2817 else
2818 pedwarn_c99 (loc, OPT_Wpedantic,
2819 "ISO C90 does not support the %<_Atomic%> qualifier");
2820 attrs_ok = true;
2821 tree value;
2822 value = c_parser_peek_token (parser)->value;
2823 c_parser_consume_token (parser);
2824 if (typespec_ok && c_parser_next_token_is (parser, CPP_OPEN_PAREN))
2826 /* _Atomic ( type-name ). */
2827 seen_type = true;
2828 c_parser_consume_token (parser);
2829 struct c_type_name *type = c_parser_type_name (parser);
2830 t.kind = ctsk_typeof;
2831 t.spec = error_mark_node;
2832 t.expr = NULL_TREE;
2833 t.expr_const_operands = true;
2834 if (type != NULL)
2835 t.spec = groktypename (type, &t.expr,
2836 &t.expr_const_operands);
2837 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
2838 "expected %<)%>");
2839 if (t.spec != error_mark_node)
2841 if (TREE_CODE (t.spec) == ARRAY_TYPE)
2842 error_at (loc, "%<_Atomic%>-qualified array type");
2843 else if (TREE_CODE (t.spec) == FUNCTION_TYPE)
2844 error_at (loc, "%<_Atomic%>-qualified function type");
2845 else if (TYPE_QUALS (t.spec) != TYPE_UNQUALIFIED)
2846 error_at (loc, "%<_Atomic%> applied to a qualified type");
2847 else
2848 t.spec = c_build_qualified_type (t.spec, TYPE_QUAL_ATOMIC);
2850 declspecs_add_type (loc, specs, t);
2852 else
2853 declspecs_add_qual (loc, specs, value);
2854 break;
2855 case RID_CONST:
2856 case RID_VOLATILE:
2857 case RID_RESTRICT:
2858 attrs_ok = true;
2859 declspecs_add_qual (loc, specs, c_parser_peek_token (parser)->value);
2860 c_parser_consume_token (parser);
2861 break;
2862 case RID_ATTRIBUTE:
2863 if (!attrs_ok)
2864 goto out;
2865 attrs = c_parser_attributes (parser);
2866 declspecs_add_attrs (loc, specs, attrs);
2867 break;
2868 case RID_ALIGNAS:
2869 if (!alignspec_ok)
2870 goto out;
2871 align = c_parser_alignas_specifier (parser);
2872 declspecs_add_alignas (loc, specs, align);
2873 break;
2874 case RID_GIMPLE:
2875 if (! flag_gimple)
2876 error_at (loc, "%<__GIMPLE%> only valid with -fgimple");
2877 c_parser_consume_token (parser);
2878 specs->gimple_p = true;
2879 specs->locations[cdw_gimple] = loc;
2880 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2881 break;
2882 case RID_RTL:
2883 c_parser_consume_token (parser);
2884 specs->rtl_p = true;
2885 specs->locations[cdw_rtl] = loc;
2886 specs->gimple_or_rtl_pass = c_parser_gimple_or_rtl_pass_list (parser);
2887 break;
2888 default:
2889 goto out;
2892 out: ;
2895 /* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2, C11 6.7.2.2).
2897 enum-specifier:
2898 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt]
2899 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt]
2900 enum attributes[opt] identifier
2902 The form with trailing comma is new in C99. The forms with
2903 attributes are GNU extensions. In GNU C, we accept any expression
2904 without commas in the syntax (assignment expressions, not just
2905 conditional expressions); assignment expressions will be diagnosed
2906 as non-constant.
2908 enumerator-list:
2909 enumerator
2910 enumerator-list , enumerator
2912 enumerator:
2913 enumeration-constant
2914 enumeration-constant = constant-expression
2916 GNU Extensions:
2918 enumerator:
2919 enumeration-constant attributes[opt]
2920 enumeration-constant attributes[opt] = constant-expression
2924 static struct c_typespec
2925 c_parser_enum_specifier (c_parser *parser)
2927 struct c_typespec ret;
2928 tree attrs;
2929 tree ident = NULL_TREE;
2930 location_t enum_loc;
2931 location_t ident_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2932 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM));
2933 c_parser_consume_token (parser);
2934 attrs = c_parser_attributes (parser);
2935 enum_loc = c_parser_peek_token (parser)->location;
2936 /* Set the location in case we create a decl now. */
2937 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
2938 if (c_parser_next_token_is (parser, CPP_NAME))
2940 ident = c_parser_peek_token (parser)->value;
2941 ident_loc = c_parser_peek_token (parser)->location;
2942 enum_loc = ident_loc;
2943 c_parser_consume_token (parser);
2945 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
2947 /* Parse an enum definition. */
2948 struct c_enum_contents the_enum;
2949 tree type;
2950 tree postfix_attrs;
2951 /* We chain the enumerators in reverse order, then put them in
2952 forward order at the end. */
2953 tree values;
2954 timevar_push (TV_PARSE_ENUM);
2955 type = start_enum (enum_loc, &the_enum, ident);
2956 values = NULL_TREE;
2957 c_parser_consume_token (parser);
2958 while (true)
2960 tree enum_id;
2961 tree enum_value;
2962 tree enum_decl;
2963 bool seen_comma;
2964 c_token *token;
2965 location_t comma_loc = UNKNOWN_LOCATION; /* Quiet warning. */
2966 location_t decl_loc, value_loc;
2967 if (c_parser_next_token_is_not (parser, CPP_NAME))
2969 /* Give a nicer error for "enum {}". */
2970 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
2971 && !parser->error)
2973 error_at (c_parser_peek_token (parser)->location,
2974 "empty enum is invalid");
2975 parser->error = true;
2977 else
2978 c_parser_error (parser, "expected identifier");
2979 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
2980 values = error_mark_node;
2981 break;
2983 token = c_parser_peek_token (parser);
2984 enum_id = token->value;
2985 /* Set the location in case we create a decl now. */
2986 c_parser_set_source_position_from_token (token);
2987 decl_loc = value_loc = token->location;
2988 c_parser_consume_token (parser);
2989 /* Parse any specified attributes. */
2990 tree enum_attrs = c_parser_attributes (parser);
2991 if (c_parser_next_token_is (parser, CPP_EQ))
2993 c_parser_consume_token (parser);
2994 value_loc = c_parser_peek_token (parser)->location;
2995 enum_value = c_parser_expr_no_commas (parser, NULL).value;
2997 else
2998 enum_value = NULL_TREE;
2999 enum_decl = build_enumerator (decl_loc, value_loc,
3000 &the_enum, enum_id, enum_value);
3001 if (enum_attrs)
3002 decl_attributes (&TREE_PURPOSE (enum_decl), enum_attrs, 0);
3003 TREE_CHAIN (enum_decl) = values;
3004 values = enum_decl;
3005 seen_comma = false;
3006 if (c_parser_next_token_is (parser, CPP_COMMA))
3008 comma_loc = c_parser_peek_token (parser)->location;
3009 seen_comma = true;
3010 c_parser_consume_token (parser);
3012 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3014 if (seen_comma)
3015 pedwarn_c90 (comma_loc, OPT_Wpedantic,
3016 "comma at end of enumerator list");
3017 c_parser_consume_token (parser);
3018 break;
3020 if (!seen_comma)
3022 c_parser_error (parser, "expected %<,%> or %<}%>");
3023 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3024 values = error_mark_node;
3025 break;
3028 postfix_attrs = c_parser_attributes (parser);
3029 ret.spec = finish_enum (type, nreverse (values),
3030 chainon (attrs, postfix_attrs));
3031 ret.kind = ctsk_tagdef;
3032 ret.expr = NULL_TREE;
3033 ret.expr_const_operands = true;
3034 timevar_pop (TV_PARSE_ENUM);
3035 return ret;
3037 else if (!ident)
3039 c_parser_error (parser, "expected %<{%>");
3040 ret.spec = error_mark_node;
3041 ret.kind = ctsk_tagref;
3042 ret.expr = NULL_TREE;
3043 ret.expr_const_operands = true;
3044 return ret;
3046 ret = parser_xref_tag (ident_loc, ENUMERAL_TYPE, ident);
3047 /* In ISO C, enumerated types can be referred to only if already
3048 defined. */
3049 if (pedantic && !COMPLETE_TYPE_P (ret.spec))
3051 gcc_assert (ident);
3052 pedwarn (enum_loc, OPT_Wpedantic,
3053 "ISO C forbids forward references to %<enum%> types");
3055 return ret;
3058 /* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1).
3060 struct-or-union-specifier:
3061 struct-or-union attributes[opt] identifier[opt]
3062 { struct-contents } attributes[opt]
3063 struct-or-union attributes[opt] identifier
3065 struct-contents:
3066 struct-declaration-list
3068 struct-declaration-list:
3069 struct-declaration ;
3070 struct-declaration-list struct-declaration ;
3072 GNU extensions:
3074 struct-contents:
3075 empty
3076 struct-declaration
3077 struct-declaration-list struct-declaration
3079 struct-declaration-list:
3080 struct-declaration-list ;
3083 (Note that in the syntax here, unlike that in ISO C, the semicolons
3084 are included here rather than in struct-declaration, in order to
3085 describe the syntax with extra semicolons and missing semicolon at
3086 end.)
3088 Objective-C:
3090 struct-declaration-list:
3091 @defs ( class-name )
3093 (Note this does not include a trailing semicolon, but can be
3094 followed by further declarations, and gets a pedwarn-if-pedantic
3095 when followed by a semicolon.) */
3097 static struct c_typespec
3098 c_parser_struct_or_union_specifier (c_parser *parser)
3100 struct c_typespec ret;
3101 tree attrs;
3102 tree ident = NULL_TREE;
3103 location_t struct_loc;
3104 location_t ident_loc = UNKNOWN_LOCATION;
3105 enum tree_code code;
3106 switch (c_parser_peek_token (parser)->keyword)
3108 case RID_STRUCT:
3109 code = RECORD_TYPE;
3110 break;
3111 case RID_UNION:
3112 code = UNION_TYPE;
3113 break;
3114 default:
3115 gcc_unreachable ();
3117 struct_loc = c_parser_peek_token (parser)->location;
3118 c_parser_consume_token (parser);
3119 attrs = c_parser_attributes (parser);
3121 /* Set the location in case we create a decl now. */
3122 c_parser_set_source_position_from_token (c_parser_peek_token (parser));
3124 if (c_parser_next_token_is (parser, CPP_NAME))
3126 ident = c_parser_peek_token (parser)->value;
3127 ident_loc = c_parser_peek_token (parser)->location;
3128 struct_loc = ident_loc;
3129 c_parser_consume_token (parser);
3131 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
3133 /* Parse a struct or union definition. Start the scope of the
3134 tag before parsing components. */
3135 struct c_struct_parse_info *struct_info;
3136 tree type = start_struct (struct_loc, code, ident, &struct_info);
3137 tree postfix_attrs;
3138 /* We chain the components in reverse order, then put them in
3139 forward order at the end. Each struct-declaration may
3140 declare multiple components (comma-separated), so we must use
3141 chainon to join them, although when parsing each
3142 struct-declaration we can use TREE_CHAIN directly.
3144 The theory behind all this is that there will be more
3145 semicolon separated fields than comma separated fields, and
3146 so we'll be minimizing the number of node traversals required
3147 by chainon. */
3148 tree contents;
3149 timevar_push (TV_PARSE_STRUCT);
3150 contents = NULL_TREE;
3151 c_parser_consume_token (parser);
3152 /* Handle the Objective-C @defs construct,
3153 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */
3154 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS))
3156 tree name;
3157 gcc_assert (c_dialect_objc ());
3158 c_parser_consume_token (parser);
3159 matching_parens parens;
3160 if (!parens.require_open (parser))
3161 goto end_at_defs;
3162 if (c_parser_next_token_is (parser, CPP_NAME)
3163 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)
3165 name = c_parser_peek_token (parser)->value;
3166 c_parser_consume_token (parser);
3168 else
3170 c_parser_error (parser, "expected class name");
3171 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
3172 goto end_at_defs;
3174 parens.skip_until_found_close (parser);
3175 contents = nreverse (objc_get_class_ivars (name));
3177 end_at_defs:
3178 /* Parse the struct-declarations and semicolons. Problems with
3179 semicolons are diagnosed here; empty structures are diagnosed
3180 elsewhere. */
3181 while (true)
3183 tree decls;
3184 /* Parse any stray semicolon. */
3185 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3187 location_t semicolon_loc
3188 = c_parser_peek_token (parser)->location;
3189 gcc_rich_location richloc (semicolon_loc);
3190 richloc.add_fixit_remove ();
3191 pedwarn (&richloc, OPT_Wpedantic,
3192 "extra semicolon in struct or union specified");
3193 c_parser_consume_token (parser);
3194 continue;
3196 /* Stop if at the end of the struct or union contents. */
3197 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3199 c_parser_consume_token (parser);
3200 break;
3202 /* Accept #pragmas at struct scope. */
3203 if (c_parser_next_token_is (parser, CPP_PRAGMA))
3205 c_parser_pragma (parser, pragma_struct, NULL);
3206 continue;
3208 /* Parse some comma-separated declarations, but not the
3209 trailing semicolon if any. */
3210 decls = c_parser_struct_declaration (parser);
3211 contents = chainon (decls, contents);
3212 /* If no semicolon follows, either we have a parse error or
3213 are at the end of the struct or union and should
3214 pedwarn. */
3215 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
3216 c_parser_consume_token (parser);
3217 else
3219 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3220 pedwarn (c_parser_peek_token (parser)->location, 0,
3221 "no semicolon at end of struct or union");
3222 else if (parser->error
3223 || !c_parser_next_token_starts_declspecs (parser))
3225 c_parser_error (parser, "expected %<;%>");
3226 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
3227 break;
3230 /* If we come here, we have already emitted an error
3231 for an expected `;', identifier or `(', and we also
3232 recovered already. Go on with the next field. */
3235 postfix_attrs = c_parser_attributes (parser);
3236 ret.spec = finish_struct (struct_loc, type, nreverse (contents),
3237 chainon (attrs, postfix_attrs), struct_info);
3238 ret.kind = ctsk_tagdef;
3239 ret.expr = NULL_TREE;
3240 ret.expr_const_operands = true;
3241 timevar_pop (TV_PARSE_STRUCT);
3242 return ret;
3244 else if (!ident)
3246 c_parser_error (parser, "expected %<{%>");
3247 ret.spec = error_mark_node;
3248 ret.kind = ctsk_tagref;
3249 ret.expr = NULL_TREE;
3250 ret.expr_const_operands = true;
3251 return ret;
3253 ret = parser_xref_tag (ident_loc, code, ident);
3254 return ret;
3257 /* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1, C11 6.7.2.1),
3258 *without* the trailing semicolon.
3260 struct-declaration:
3261 specifier-qualifier-list struct-declarator-list
3262 static_assert-declaration-no-semi
3264 specifier-qualifier-list:
3265 type-specifier specifier-qualifier-list[opt]
3266 type-qualifier specifier-qualifier-list[opt]
3267 alignment-specifier specifier-qualifier-list[opt]
3268 attributes specifier-qualifier-list[opt]
3270 struct-declarator-list:
3271 struct-declarator
3272 struct-declarator-list , attributes[opt] struct-declarator
3274 struct-declarator:
3275 declarator attributes[opt]
3276 declarator[opt] : constant-expression attributes[opt]
3278 GNU extensions:
3280 struct-declaration:
3281 __extension__ struct-declaration
3282 specifier-qualifier-list
3284 Unlike the ISO C syntax, semicolons are handled elsewhere. The use
3285 of attributes where shown is a GNU extension. In GNU C, we accept
3286 any expression without commas in the syntax (assignment
3287 expressions, not just conditional expressions); assignment
3288 expressions will be diagnosed as non-constant. */
3290 static tree
3291 c_parser_struct_declaration (c_parser *parser)
3293 struct c_declspecs *specs;
3294 tree prefix_attrs;
3295 tree all_prefix_attrs;
3296 tree decls;
3297 location_t decl_loc;
3298 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
3300 int ext;
3301 tree decl;
3302 ext = disable_extension_diagnostics ();
3303 c_parser_consume_token (parser);
3304 decl = c_parser_struct_declaration (parser);
3305 restore_extension_diagnostics (ext);
3306 return decl;
3308 if (c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
3310 c_parser_static_assert_declaration_no_semi (parser);
3311 return NULL_TREE;
3313 specs = build_null_declspecs ();
3314 decl_loc = c_parser_peek_token (parser)->location;
3315 /* Strictly by the standard, we shouldn't allow _Alignas here,
3316 but it appears to have been intended to allow it there, so
3317 we're keeping it as it is until WG14 reaches a conclusion
3318 of N1731.
3319 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1731.pdf> */
3320 c_parser_declspecs (parser, specs, false, true, true,
3321 true, false, cla_nonabstract_decl);
3322 if (parser->error)
3323 return NULL_TREE;
3324 if (!specs->declspecs_seen_p)
3326 c_parser_error (parser, "expected specifier-qualifier-list");
3327 return NULL_TREE;
3329 finish_declspecs (specs);
3330 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3331 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3333 tree ret;
3334 if (specs->typespec_kind == ctsk_none)
3336 pedwarn (decl_loc, OPT_Wpedantic,
3337 "ISO C forbids member declarations with no members");
3338 shadow_tag_warned (specs, pedantic);
3339 ret = NULL_TREE;
3341 else
3343 /* Support for unnamed structs or unions as members of
3344 structs or unions (which is [a] useful and [b] supports
3345 MS P-SDK). */
3346 tree attrs = NULL;
3348 ret = grokfield (c_parser_peek_token (parser)->location,
3349 build_id_declarator (NULL_TREE), specs,
3350 NULL_TREE, &attrs);
3351 if (ret)
3352 decl_attributes (&ret, attrs, 0);
3354 return ret;
3357 /* Provide better error recovery. Note that a type name here is valid,
3358 and will be treated as a field name. */
3359 if (specs->typespec_kind == ctsk_tagdef
3360 && TREE_CODE (specs->type) != ENUMERAL_TYPE
3361 && c_parser_next_token_starts_declspecs (parser)
3362 && !c_parser_next_token_is (parser, CPP_NAME))
3364 c_parser_error (parser, "expected %<;%>, identifier or %<(%>");
3365 parser->error = false;
3366 return NULL_TREE;
3369 pending_xref_error ();
3370 prefix_attrs = specs->attrs;
3371 all_prefix_attrs = prefix_attrs;
3372 specs->attrs = NULL_TREE;
3373 decls = NULL_TREE;
3374 while (true)
3376 /* Declaring one or more declarators or un-named bit-fields. */
3377 struct c_declarator *declarator;
3378 bool dummy = false;
3379 if (c_parser_next_token_is (parser, CPP_COLON))
3380 declarator = build_id_declarator (NULL_TREE);
3381 else
3382 declarator = c_parser_declarator (parser,
3383 specs->typespec_kind != ctsk_none,
3384 C_DTR_NORMAL, &dummy);
3385 if (declarator == NULL)
3387 c_parser_skip_to_end_of_block_or_statement (parser);
3388 break;
3390 if (c_parser_next_token_is (parser, CPP_COLON)
3391 || c_parser_next_token_is (parser, CPP_COMMA)
3392 || c_parser_next_token_is (parser, CPP_SEMICOLON)
3393 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)
3394 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3396 tree postfix_attrs = NULL_TREE;
3397 tree width = NULL_TREE;
3398 tree d;
3399 if (c_parser_next_token_is (parser, CPP_COLON))
3401 c_parser_consume_token (parser);
3402 width = c_parser_expr_no_commas (parser, NULL).value;
3404 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3405 postfix_attrs = c_parser_attributes (parser);
3406 d = grokfield (c_parser_peek_token (parser)->location,
3407 declarator, specs, width, &all_prefix_attrs);
3408 decl_attributes (&d, chainon (postfix_attrs,
3409 all_prefix_attrs), 0);
3410 DECL_CHAIN (d) = decls;
3411 decls = d;
3412 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
3413 all_prefix_attrs = chainon (c_parser_attributes (parser),
3414 prefix_attrs);
3415 else
3416 all_prefix_attrs = prefix_attrs;
3417 if (c_parser_next_token_is (parser, CPP_COMMA))
3418 c_parser_consume_token (parser);
3419 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)
3420 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
3422 /* Semicolon consumed in caller. */
3423 break;
3425 else
3427 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>");
3428 break;
3431 else
3433 c_parser_error (parser,
3434 "expected %<:%>, %<,%>, %<;%>, %<}%> or "
3435 "%<__attribute__%>");
3436 break;
3439 return decls;
3442 /* Parse a typeof specifier (a GNU extension).
3444 typeof-specifier:
3445 typeof ( expression )
3446 typeof ( type-name )
3449 static struct c_typespec
3450 c_parser_typeof_specifier (c_parser *parser)
3452 struct c_typespec ret;
3453 ret.kind = ctsk_typeof;
3454 ret.spec = error_mark_node;
3455 ret.expr = NULL_TREE;
3456 ret.expr_const_operands = true;
3457 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF));
3458 c_parser_consume_token (parser);
3459 c_inhibit_evaluation_warnings++;
3460 in_typeof++;
3461 matching_parens parens;
3462 if (!parens.require_open (parser))
3464 c_inhibit_evaluation_warnings--;
3465 in_typeof--;
3466 return ret;
3468 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3470 struct c_type_name *type = c_parser_type_name (parser);
3471 c_inhibit_evaluation_warnings--;
3472 in_typeof--;
3473 if (type != NULL)
3475 ret.spec = groktypename (type, &ret.expr, &ret.expr_const_operands);
3476 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE));
3479 else
3481 bool was_vm;
3482 location_t here = c_parser_peek_token (parser)->location;
3483 struct c_expr expr = c_parser_expression (parser);
3484 c_inhibit_evaluation_warnings--;
3485 in_typeof--;
3486 if (TREE_CODE (expr.value) == COMPONENT_REF
3487 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
3488 error_at (here, "%<typeof%> applied to a bit-field");
3489 mark_exp_read (expr.value);
3490 ret.spec = TREE_TYPE (expr.value);
3491 was_vm = variably_modified_type_p (ret.spec, NULL_TREE);
3492 /* This is returned with the type so that when the type is
3493 evaluated, this can be evaluated. */
3494 if (was_vm)
3495 ret.expr = c_fully_fold (expr.value, false, &ret.expr_const_operands);
3496 pop_maybe_used (was_vm);
3497 /* For use in macros such as those in <stdatomic.h>, remove all
3498 qualifiers from atomic types. (const can be an issue for more macros
3499 using typeof than just the <stdatomic.h> ones.) */
3500 if (ret.spec != error_mark_node && TYPE_ATOMIC (ret.spec))
3501 ret.spec = c_build_qualified_type (ret.spec, TYPE_UNQUALIFIED);
3503 parens.skip_until_found_close (parser);
3504 return ret;
3507 /* Parse an alignment-specifier.
3509 C11 6.7.5:
3511 alignment-specifier:
3512 _Alignas ( type-name )
3513 _Alignas ( constant-expression )
3516 static tree
3517 c_parser_alignas_specifier (c_parser * parser)
3519 tree ret = error_mark_node;
3520 location_t loc = c_parser_peek_token (parser)->location;
3521 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNAS));
3522 c_parser_consume_token (parser);
3523 if (flag_isoc99)
3524 pedwarn_c99 (loc, OPT_Wpedantic,
3525 "ISO C99 does not support %<_Alignas%>");
3526 else
3527 pedwarn_c99 (loc, OPT_Wpedantic,
3528 "ISO C90 does not support %<_Alignas%>");
3529 matching_parens parens;
3530 if (!parens.require_open (parser))
3531 return ret;
3532 if (c_parser_next_tokens_start_typename (parser, cla_prefer_id))
3534 struct c_type_name *type = c_parser_type_name (parser);
3535 if (type != NULL)
3536 ret = c_sizeof_or_alignof_type (loc, groktypename (type, NULL, NULL),
3537 false, true, 1);
3539 else
3540 ret = c_parser_expr_no_commas (parser, NULL).value;
3541 parens.skip_until_found_close (parser);
3542 return ret;
3545 /* Parse a declarator, possibly an abstract declarator (C90 6.5.4,
3546 6.5.5, C99 6.7.5, 6.7.6, C11 6.7.6, 6.7.7). If TYPE_SEEN_P then
3547 a typedef name may be redeclared; otherwise it may not. KIND
3548 indicates which kind of declarator is wanted. Returns a valid
3549 declarator except in the case of a syntax error in which case NULL is
3550 returned. *SEEN_ID is set to true if an identifier being declared is
3551 seen; this is used to diagnose bad forms of abstract array declarators
3552 and to determine whether an identifier list is syntactically permitted.
3554 declarator:
3555 pointer[opt] direct-declarator
3557 direct-declarator:
3558 identifier
3559 ( attributes[opt] declarator )
3560 direct-declarator array-declarator
3561 direct-declarator ( parameter-type-list )
3562 direct-declarator ( identifier-list[opt] )
3564 pointer:
3565 * type-qualifier-list[opt]
3566 * type-qualifier-list[opt] pointer
3568 type-qualifier-list:
3569 type-qualifier
3570 attributes
3571 type-qualifier-list type-qualifier
3572 type-qualifier-list attributes
3574 array-declarator:
3575 [ type-qualifier-list[opt] assignment-expression[opt] ]
3576 [ static type-qualifier-list[opt] assignment-expression ]
3577 [ type-qualifier-list static assignment-expression ]
3578 [ type-qualifier-list[opt] * ]
3580 parameter-type-list:
3581 parameter-list
3582 parameter-list , ...
3584 parameter-list:
3585 parameter-declaration
3586 parameter-list , parameter-declaration
3588 parameter-declaration:
3589 declaration-specifiers declarator attributes[opt]
3590 declaration-specifiers abstract-declarator[opt] attributes[opt]
3592 identifier-list:
3593 identifier
3594 identifier-list , identifier
3596 abstract-declarator:
3597 pointer
3598 pointer[opt] direct-abstract-declarator
3600 direct-abstract-declarator:
3601 ( attributes[opt] abstract-declarator )
3602 direct-abstract-declarator[opt] array-declarator
3603 direct-abstract-declarator[opt] ( parameter-type-list[opt] )
3605 GNU extensions:
3607 direct-declarator:
3608 direct-declarator ( parameter-forward-declarations
3609 parameter-type-list[opt] )
3611 direct-abstract-declarator:
3612 direct-abstract-declarator[opt] ( parameter-forward-declarations
3613 parameter-type-list[opt] )
3615 parameter-forward-declarations:
3616 parameter-list ;
3617 parameter-forward-declarations parameter-list ;
3619 The uses of attributes shown above are GNU extensions.
3621 Some forms of array declarator are not included in C99 in the
3622 syntax for abstract declarators; these are disallowed elsewhere.
3623 This may be a defect (DR#289).
3625 This function also accepts an omitted abstract declarator as being
3626 an abstract declarator, although not part of the formal syntax. */
3628 struct c_declarator *
3629 c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3630 bool *seen_id)
3632 /* Parse any initial pointer part. */
3633 if (c_parser_next_token_is (parser, CPP_MULT))
3635 struct c_declspecs *quals_attrs = build_null_declspecs ();
3636 struct c_declarator *inner;
3637 c_parser_consume_token (parser);
3638 c_parser_declspecs (parser, quals_attrs, false, false, true,
3639 false, false, cla_prefer_id);
3640 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3641 if (inner == NULL)
3642 return NULL;
3643 else
3644 return make_pointer_declarator (quals_attrs, inner);
3646 /* Now we have a direct declarator, direct abstract declarator or
3647 nothing (which counts as a direct abstract declarator here). */
3648 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id);
3651 /* Parse a direct declarator or direct abstract declarator; arguments
3652 as c_parser_declarator. */
3654 static struct c_declarator *
3655 c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind,
3656 bool *seen_id)
3658 /* The direct declarator must start with an identifier (possibly
3659 omitted) or a parenthesized declarator (possibly abstract). In
3660 an ordinary declarator, initial parentheses must start a
3661 parenthesized declarator. In an abstract declarator or parameter
3662 declarator, they could start a parenthesized declarator or a
3663 parameter list. To tell which, the open parenthesis and any
3664 following attributes must be read. If a declaration specifier
3665 follows, then it is a parameter list; if the specifier is a
3666 typedef name, there might be an ambiguity about redeclaring it,
3667 which is resolved in the direction of treating it as a typedef
3668 name. If a close parenthesis follows, it is also an empty
3669 parameter list, as the syntax does not permit empty abstract
3670 declarators. Otherwise, it is a parenthesized declarator (in
3671 which case the analysis may be repeated inside it, recursively).
3673 ??? There is an ambiguity in a parameter declaration "int
3674 (__attribute__((foo)) x)", where x is not a typedef name: it
3675 could be an abstract declarator for a function, or declare x with
3676 parentheses. The proper resolution of this ambiguity needs
3677 documenting. At present we follow an accident of the old
3678 parser's implementation, whereby the first parameter must have
3679 some declaration specifiers other than just attributes. Thus as
3680 a parameter declaration it is treated as a parenthesized
3681 parameter named x, and as an abstract declarator it is
3682 rejected.
3684 ??? Also following the old parser, attributes inside an empty
3685 parameter list are ignored, making it a list not yielding a
3686 prototype, rather than giving an error or making it have one
3687 parameter with implicit type int.
3689 ??? Also following the old parser, typedef names may be
3690 redeclared in declarators, but not Objective-C class names. */
3692 if (kind != C_DTR_ABSTRACT
3693 && c_parser_next_token_is (parser, CPP_NAME)
3694 && ((type_seen_p
3695 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
3696 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
3697 || c_parser_peek_token (parser)->id_kind == C_ID_ID))
3699 struct c_declarator *inner
3700 = build_id_declarator (c_parser_peek_token (parser)->value);
3701 *seen_id = true;
3702 inner->id_loc = c_parser_peek_token (parser)->location;
3703 c_parser_consume_token (parser);
3704 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3707 if (kind != C_DTR_NORMAL
3708 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3710 struct c_declarator *inner = build_id_declarator (NULL_TREE);
3711 inner->id_loc = c_parser_peek_token (parser)->location;
3712 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3715 /* Either we are at the end of an abstract declarator, or we have
3716 parentheses. */
3718 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3720 tree attrs;
3721 struct c_declarator *inner;
3722 c_parser_consume_token (parser);
3723 attrs = c_parser_attributes (parser);
3724 if (kind != C_DTR_NORMAL
3725 && (c_parser_next_token_starts_declspecs (parser)
3726 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)))
3728 struct c_arg_info *args
3729 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL,
3730 attrs);
3731 if (args == NULL)
3732 return NULL;
3733 else
3735 inner
3736 = build_function_declarator (args,
3737 build_id_declarator (NULL_TREE));
3738 return c_parser_direct_declarator_inner (parser, *seen_id,
3739 inner);
3742 /* A parenthesized declarator. */
3743 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id);
3744 if (inner != NULL && attrs != NULL)
3745 inner = build_attrs_declarator (attrs, inner);
3746 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3748 c_parser_consume_token (parser);
3749 if (inner == NULL)
3750 return NULL;
3751 else
3752 return c_parser_direct_declarator_inner (parser, *seen_id, inner);
3754 else
3756 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3757 "expected %<)%>");
3758 return NULL;
3761 else
3763 if (kind == C_DTR_NORMAL)
3765 c_parser_error (parser, "expected identifier or %<(%>");
3766 return NULL;
3768 else
3769 return build_id_declarator (NULL_TREE);
3773 /* Parse part of a direct declarator or direct abstract declarator,
3774 given that some (in INNER) has already been parsed; ID_PRESENT is
3775 true if an identifier is present, false for an abstract
3776 declarator. */
3778 static struct c_declarator *
3779 c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
3780 struct c_declarator *inner)
3782 /* Parse a sequence of array declarators and parameter lists. */
3783 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
3785 location_t brace_loc = c_parser_peek_token (parser)->location;
3786 struct c_declarator *declarator;
3787 struct c_declspecs *quals_attrs = build_null_declspecs ();
3788 bool static_seen;
3789 bool star_seen;
3790 struct c_expr dimen;
3791 dimen.value = NULL_TREE;
3792 dimen.original_code = ERROR_MARK;
3793 dimen.original_type = NULL_TREE;
3794 c_parser_consume_token (parser);
3795 c_parser_declspecs (parser, quals_attrs, false, false, true,
3796 false, false, cla_prefer_id);
3797 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
3798 if (static_seen)
3799 c_parser_consume_token (parser);
3800 if (static_seen && !quals_attrs->declspecs_seen_p)
3801 c_parser_declspecs (parser, quals_attrs, false, false, true,
3802 false, false, cla_prefer_id);
3803 if (!quals_attrs->declspecs_seen_p)
3804 quals_attrs = NULL;
3805 /* If "static" is present, there must be an array dimension.
3806 Otherwise, there may be a dimension, "*", or no
3807 dimension. */
3808 if (static_seen)
3810 star_seen = false;
3811 dimen = c_parser_expr_no_commas (parser, NULL);
3813 else
3815 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3817 dimen.value = NULL_TREE;
3818 star_seen = false;
3820 else if (c_parser_next_token_is (parser, CPP_MULT))
3822 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
3824 dimen.value = NULL_TREE;
3825 star_seen = true;
3826 c_parser_consume_token (parser);
3828 else
3830 star_seen = false;
3831 dimen = c_parser_expr_no_commas (parser, NULL);
3834 else
3836 star_seen = false;
3837 dimen = c_parser_expr_no_commas (parser, NULL);
3840 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
3841 c_parser_consume_token (parser);
3842 else
3844 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
3845 "expected %<]%>");
3846 return NULL;
3848 if (dimen.value)
3849 dimen = convert_lvalue_to_rvalue (brace_loc, dimen, true, true);
3850 declarator = build_array_declarator (brace_loc, dimen.value, quals_attrs,
3851 static_seen, star_seen);
3852 if (declarator == NULL)
3853 return NULL;
3854 inner = set_array_declarator_inner (declarator, inner);
3855 return c_parser_direct_declarator_inner (parser, id_present, inner);
3857 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
3859 tree attrs;
3860 struct c_arg_info *args;
3861 c_parser_consume_token (parser);
3862 attrs = c_parser_attributes (parser);
3863 args = c_parser_parms_declarator (parser, id_present, attrs);
3864 if (args == NULL)
3865 return NULL;
3866 else
3868 inner = build_function_declarator (args, inner);
3869 return c_parser_direct_declarator_inner (parser, id_present, inner);
3872 return inner;
3875 /* Parse a parameter list or identifier list, including the closing
3876 parenthesis but not the opening one. ATTRS are the attributes at
3877 the start of the list. ID_LIST_OK is true if an identifier list is
3878 acceptable; such a list must not have attributes at the start. */
3880 static struct c_arg_info *
3881 c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs)
3883 push_scope ();
3884 declare_parm_level ();
3885 /* If the list starts with an identifier, it is an identifier list.
3886 Otherwise, it is either a prototype list or an empty list. */
3887 if (id_list_ok
3888 && !attrs
3889 && c_parser_next_token_is (parser, CPP_NAME)
3890 && c_parser_peek_token (parser)->id_kind == C_ID_ID
3892 /* Look ahead to detect typos in type names. */
3893 && c_parser_peek_2nd_token (parser)->type != CPP_NAME
3894 && c_parser_peek_2nd_token (parser)->type != CPP_MULT
3895 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
3896 && c_parser_peek_2nd_token (parser)->type != CPP_OPEN_SQUARE
3897 && c_parser_peek_2nd_token (parser)->type != CPP_KEYWORD)
3899 tree list = NULL_TREE, *nextp = &list;
3900 while (c_parser_next_token_is (parser, CPP_NAME)
3901 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
3903 *nextp = build_tree_list (NULL_TREE,
3904 c_parser_peek_token (parser)->value);
3905 nextp = & TREE_CHAIN (*nextp);
3906 c_parser_consume_token (parser);
3907 if (c_parser_next_token_is_not (parser, CPP_COMMA))
3908 break;
3909 c_parser_consume_token (parser);
3910 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3912 c_parser_error (parser, "expected identifier");
3913 break;
3916 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3918 struct c_arg_info *ret = build_arg_info ();
3919 ret->types = list;
3920 c_parser_consume_token (parser);
3921 pop_scope ();
3922 return ret;
3924 else
3926 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3927 "expected %<)%>");
3928 pop_scope ();
3929 return NULL;
3932 else
3934 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs,
3935 NULL);
3936 pop_scope ();
3937 return ret;
3941 /* Parse a parameter list (possibly empty), including the closing
3942 parenthesis but not the opening one. ATTRS are the attributes at
3943 the start of the list. EXPR is NULL or an expression that needs to
3944 be evaluated for the side effects of array size expressions in the
3945 parameters. */
3947 static struct c_arg_info *
3948 c_parser_parms_list_declarator (c_parser *parser, tree attrs, tree expr)
3950 bool bad_parm = false;
3952 /* ??? Following the old parser, forward parameter declarations may
3953 use abstract declarators, and if no real parameter declarations
3954 follow the forward declarations then this is not diagnosed. Also
3955 note as above that attributes are ignored as the only contents of
3956 the parentheses, or as the only contents after forward
3957 declarations. */
3958 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3960 struct c_arg_info *ret = build_arg_info ();
3961 c_parser_consume_token (parser);
3962 return ret;
3964 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
3966 struct c_arg_info *ret = build_arg_info ();
3968 if (flag_allow_parameterless_variadic_functions)
3970 /* F (...) is allowed. */
3971 ret->types = NULL_TREE;
3973 else
3975 /* Suppress -Wold-style-definition for this case. */
3976 ret->types = error_mark_node;
3977 error_at (c_parser_peek_token (parser)->location,
3978 "ISO C requires a named argument before %<...%>");
3980 c_parser_consume_token (parser);
3981 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
3983 c_parser_consume_token (parser);
3984 return ret;
3986 else
3988 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
3989 "expected %<)%>");
3990 return NULL;
3993 /* Nonempty list of parameters, either terminated with semicolon
3994 (forward declarations; recurse) or with close parenthesis (normal
3995 function) or with ", ... )" (variadic function). */
3996 while (true)
3998 /* Parse a parameter. */
3999 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs);
4000 attrs = NULL_TREE;
4001 if (parm == NULL)
4002 bad_parm = true;
4003 else
4004 push_parm_decl (parm, &expr);
4005 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
4007 tree new_attrs;
4008 c_parser_consume_token (parser);
4009 mark_forward_parm_decls ();
4010 new_attrs = c_parser_attributes (parser);
4011 return c_parser_parms_list_declarator (parser, new_attrs, expr);
4013 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4015 c_parser_consume_token (parser);
4016 if (bad_parm)
4017 return NULL;
4018 else
4019 return get_parm_info (false, expr);
4021 if (!c_parser_require (parser, CPP_COMMA,
4022 "expected %<;%>, %<,%> or %<)%>",
4023 UNKNOWN_LOCATION, false))
4025 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4026 return NULL;
4028 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4030 c_parser_consume_token (parser);
4031 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4033 c_parser_consume_token (parser);
4034 if (bad_parm)
4035 return NULL;
4036 else
4037 return get_parm_info (true, expr);
4039 else
4041 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4042 "expected %<)%>");
4043 return NULL;
4049 /* Parse a parameter declaration. ATTRS are the attributes at the
4050 start of the declaration if it is the first parameter. */
4052 static struct c_parm *
4053 c_parser_parameter_declaration (c_parser *parser, tree attrs)
4055 struct c_declspecs *specs;
4056 struct c_declarator *declarator;
4057 tree prefix_attrs;
4058 tree postfix_attrs = NULL_TREE;
4059 bool dummy = false;
4061 /* Accept #pragmas between parameter declarations. */
4062 while (c_parser_next_token_is (parser, CPP_PRAGMA))
4063 c_parser_pragma (parser, pragma_param, NULL);
4065 if (!c_parser_next_token_starts_declspecs (parser))
4067 c_token *token = c_parser_peek_token (parser);
4068 if (parser->error)
4069 return NULL;
4070 c_parser_set_source_position_from_token (token);
4071 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
4073 auto_diagnostic_group d;
4074 name_hint hint = lookup_name_fuzzy (token->value,
4075 FUZZY_LOOKUP_TYPENAME,
4076 token->location);
4077 if (const char *suggestion = hint.suggestion ())
4079 gcc_rich_location richloc (token->location);
4080 richloc.add_fixit_replace (suggestion);
4081 error_at (&richloc,
4082 "unknown type name %qE; did you mean %qs?",
4083 token->value, suggestion);
4085 else
4086 error_at (token->location, "unknown type name %qE", token->value);
4087 parser->error = true;
4089 /* ??? In some Objective-C cases '...' isn't applicable so there
4090 should be a different message. */
4091 else
4092 c_parser_error (parser,
4093 "expected declaration specifiers or %<...%>");
4094 c_parser_skip_to_end_of_parameter (parser);
4095 return NULL;
4098 location_t start_loc = c_parser_peek_token (parser)->location;
4100 specs = build_null_declspecs ();
4101 if (attrs)
4103 declspecs_add_attrs (input_location, specs, attrs);
4104 attrs = NULL_TREE;
4106 c_parser_declspecs (parser, specs, true, true, true, true, false,
4107 cla_nonabstract_decl);
4108 finish_declspecs (specs);
4109 pending_xref_error ();
4110 prefix_attrs = specs->attrs;
4111 specs->attrs = NULL_TREE;
4112 declarator = c_parser_declarator (parser,
4113 specs->typespec_kind != ctsk_none,
4114 C_DTR_PARM, &dummy);
4115 if (declarator == NULL)
4117 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4118 return NULL;
4120 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4121 postfix_attrs = c_parser_attributes (parser);
4123 /* Generate a location for the parameter, ranging from the start of the
4124 initial token to the end of the final token.
4126 If we have a identifier, then use it for the caret location, e.g.
4128 extern int callee (int one, int (*two)(int, int), float three);
4129 ~~~~~~^~~~~~~~~~~~~~
4131 otherwise, reuse the start location for the caret location e.g.:
4133 extern int callee (int one, int (*)(int, int), float three);
4134 ^~~~~~~~~~~~~~~~~
4136 location_t end_loc = parser->last_token_location;
4138 /* Find any cdk_id declarator; determine if we have an identifier. */
4139 c_declarator *id_declarator = declarator;
4140 while (id_declarator && id_declarator->kind != cdk_id)
4141 id_declarator = id_declarator->declarator;
4142 location_t caret_loc = (id_declarator->u.id
4143 ? id_declarator->id_loc
4144 : start_loc);
4145 location_t param_loc = make_location (caret_loc, start_loc, end_loc);
4147 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs),
4148 declarator, param_loc);
4151 /* Parse a string literal in an asm expression. It should not be
4152 translated, and wide string literals are an error although
4153 permitted by the syntax. This is a GNU extension.
4155 asm-string-literal:
4156 string-literal
4158 ??? At present, following the old parser, the caller needs to have
4159 set lex_untranslated_string to 1. It would be better to follow the
4160 C++ parser rather than using this kludge. */
4162 static tree
4163 c_parser_asm_string_literal (c_parser *parser)
4165 tree str;
4166 int save_flag = warn_overlength_strings;
4167 warn_overlength_strings = 0;
4168 if (c_parser_next_token_is (parser, CPP_STRING))
4170 str = c_parser_peek_token (parser)->value;
4171 c_parser_consume_token (parser);
4173 else if (c_parser_next_token_is (parser, CPP_WSTRING))
4175 error_at (c_parser_peek_token (parser)->location,
4176 "wide string literal in %<asm%>");
4177 str = build_string (1, "");
4178 c_parser_consume_token (parser);
4180 else
4182 c_parser_error (parser, "expected string literal");
4183 str = NULL_TREE;
4185 warn_overlength_strings = save_flag;
4186 return str;
4189 /* Parse a simple asm expression. This is used in restricted
4190 contexts, where a full expression with inputs and outputs does not
4191 make sense. This is a GNU extension.
4193 simple-asm-expr:
4194 asm ( asm-string-literal )
4197 static tree
4198 c_parser_simple_asm_expr (c_parser *parser)
4200 tree str;
4201 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
4202 /* ??? Follow the C++ parser rather than using the
4203 lex_untranslated_string kludge. */
4204 parser->lex_untranslated_string = true;
4205 c_parser_consume_token (parser);
4206 matching_parens parens;
4207 if (!parens.require_open (parser))
4209 parser->lex_untranslated_string = false;
4210 return NULL_TREE;
4212 str = c_parser_asm_string_literal (parser);
4213 parser->lex_untranslated_string = false;
4214 if (!parens.require_close (parser))
4216 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4217 return NULL_TREE;
4219 return str;
4222 static tree
4223 c_parser_attribute_any_word (c_parser *parser)
4225 tree attr_name = NULL_TREE;
4227 if (c_parser_next_token_is (parser, CPP_KEYWORD))
4229 /* ??? See comment above about what keywords are accepted here. */
4230 bool ok;
4231 switch (c_parser_peek_token (parser)->keyword)
4233 case RID_STATIC:
4234 case RID_UNSIGNED:
4235 case RID_LONG:
4236 case RID_CONST:
4237 case RID_EXTERN:
4238 case RID_REGISTER:
4239 case RID_TYPEDEF:
4240 case RID_SHORT:
4241 case RID_INLINE:
4242 case RID_NORETURN:
4243 case RID_VOLATILE:
4244 case RID_SIGNED:
4245 case RID_AUTO:
4246 case RID_RESTRICT:
4247 case RID_COMPLEX:
4248 case RID_THREAD:
4249 case RID_INT:
4250 case RID_CHAR:
4251 case RID_FLOAT:
4252 case RID_DOUBLE:
4253 case RID_VOID:
4254 case RID_DFLOAT32:
4255 case RID_DFLOAT64:
4256 case RID_DFLOAT128:
4257 CASE_RID_FLOATN_NX:
4258 case RID_BOOL:
4259 case RID_FRACT:
4260 case RID_ACCUM:
4261 case RID_SAT:
4262 case RID_TRANSACTION_ATOMIC:
4263 case RID_TRANSACTION_CANCEL:
4264 case RID_ATOMIC:
4265 case RID_AUTO_TYPE:
4266 case RID_INT_N_0:
4267 case RID_INT_N_1:
4268 case RID_INT_N_2:
4269 case RID_INT_N_3:
4270 ok = true;
4271 break;
4272 default:
4273 ok = false;
4274 break;
4276 if (!ok)
4277 return NULL_TREE;
4279 /* Accept __attribute__((__const)) as __attribute__((const)) etc. */
4280 attr_name = ridpointers[(int) c_parser_peek_token (parser)->keyword];
4282 else if (c_parser_next_token_is (parser, CPP_NAME))
4283 attr_name = c_parser_peek_token (parser)->value;
4285 return attr_name;
4288 /* Parse (possibly empty) attributes. This is a GNU extension.
4290 attributes:
4291 empty
4292 attributes attribute
4294 attribute:
4295 __attribute__ ( ( attribute-list ) )
4297 attribute-list:
4298 attrib
4299 attribute_list , attrib
4301 attrib:
4302 empty
4303 any-word
4304 any-word ( identifier )
4305 any-word ( identifier , nonempty-expr-list )
4306 any-word ( expr-list )
4308 where the "identifier" must not be declared as a type, and
4309 "any-word" may be any identifier (including one declared as a
4310 type), a reserved word storage class specifier, type specifier or
4311 type qualifier. ??? This still leaves out most reserved keywords
4312 (following the old parser), shouldn't we include them, and why not
4313 allow identifiers declared as types to start the arguments? */
4315 static tree
4316 c_parser_attributes (c_parser *parser)
4318 tree attrs = NULL_TREE;
4319 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
4321 /* ??? Follow the C++ parser rather than using the
4322 lex_untranslated_string kludge. */
4323 parser->lex_untranslated_string = true;
4324 /* Consume the `__attribute__' keyword. */
4325 c_parser_consume_token (parser);
4326 /* Look for the two `(' tokens. */
4327 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4329 parser->lex_untranslated_string = false;
4330 return attrs;
4332 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
4334 parser->lex_untranslated_string = false;
4335 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
4336 return attrs;
4338 /* Parse the attribute list. */
4339 while (c_parser_next_token_is (parser, CPP_COMMA)
4340 || c_parser_next_token_is (parser, CPP_NAME)
4341 || c_parser_next_token_is (parser, CPP_KEYWORD))
4343 tree attr, attr_name, attr_args;
4344 vec<tree, va_gc> *expr_list;
4345 if (c_parser_next_token_is (parser, CPP_COMMA))
4347 c_parser_consume_token (parser);
4348 continue;
4351 attr_name = c_parser_attribute_any_word (parser);
4352 if (attr_name == NULL)
4353 break;
4354 attr_name = canonicalize_attr_name (attr_name);
4355 c_parser_consume_token (parser);
4356 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
4358 attr = build_tree_list (attr_name, NULL_TREE);
4359 /* Add this attribute to the list. */
4360 attrs = chainon (attrs, attr);
4361 /* If the next token isn't a comma, we're done. */
4362 if (!c_parser_next_token_is (parser, CPP_COMMA))
4363 break;
4364 continue;
4366 c_parser_consume_token (parser);
4367 /* Parse the attribute contents. If they start with an
4368 identifier which is followed by a comma or close
4369 parenthesis, then the arguments start with that
4370 identifier; otherwise they are an expression list.
4371 In objective-c the identifier may be a classname. */
4372 if (c_parser_next_token_is (parser, CPP_NAME)
4373 && (c_parser_peek_token (parser)->id_kind == C_ID_ID
4374 || (c_dialect_objc ()
4375 && c_parser_peek_token (parser)->id_kind
4376 == C_ID_CLASSNAME))
4377 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA)
4378 || (c_parser_peek_2nd_token (parser)->type
4379 == CPP_CLOSE_PAREN))
4380 && (attribute_takes_identifier_p (attr_name)
4381 || (c_dialect_objc ()
4382 && c_parser_peek_token (parser)->id_kind
4383 == C_ID_CLASSNAME)))
4385 tree arg1 = c_parser_peek_token (parser)->value;
4386 c_parser_consume_token (parser);
4387 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4388 attr_args = build_tree_list (NULL_TREE, arg1);
4389 else
4391 tree tree_list;
4392 c_parser_consume_token (parser);
4393 expr_list = c_parser_expr_list (parser, false, true,
4394 NULL, NULL, NULL, NULL);
4395 tree_list = build_tree_list_vec (expr_list);
4396 attr_args = tree_cons (NULL_TREE, arg1, tree_list);
4397 release_tree_vector (expr_list);
4400 else
4402 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4403 attr_args = NULL_TREE;
4404 else
4406 expr_list = c_parser_expr_list (parser, false, true,
4407 NULL, NULL, NULL, NULL);
4408 attr_args = build_tree_list_vec (expr_list);
4409 release_tree_vector (expr_list);
4413 attr = build_tree_list (attr_name, attr_args);
4414 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4415 c_parser_consume_token (parser);
4416 else
4418 parser->lex_untranslated_string = false;
4419 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4420 "expected %<)%>");
4421 return attrs;
4423 /* Add this attribute to the list. */
4424 attrs = chainon (attrs, attr);
4425 /* If the next token isn't a comma, we're done. */
4426 if (!c_parser_next_token_is (parser, CPP_COMMA))
4427 break;
4429 /* Look for the two `)' tokens. */
4430 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4431 c_parser_consume_token (parser);
4432 else
4434 parser->lex_untranslated_string = false;
4435 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4436 "expected %<)%>");
4437 return attrs;
4439 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
4440 c_parser_consume_token (parser);
4441 else
4443 parser->lex_untranslated_string = false;
4444 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
4445 "expected %<)%>");
4446 return attrs;
4448 parser->lex_untranslated_string = false;
4451 return attrs;
4454 /* Parse a type name (C90 6.5.5, C99 6.7.6, C11 6.7.7). ALIGNAS_OK
4455 says whether alignment specifiers are OK (only in cases that might
4456 be the type name of a compound literal).
4458 type-name:
4459 specifier-qualifier-list abstract-declarator[opt]
4462 struct c_type_name *
4463 c_parser_type_name (c_parser *parser, bool alignas_ok)
4465 struct c_declspecs *specs = build_null_declspecs ();
4466 struct c_declarator *declarator;
4467 struct c_type_name *ret;
4468 bool dummy = false;
4469 c_parser_declspecs (parser, specs, false, true, true, alignas_ok, false,
4470 cla_prefer_type);
4471 if (!specs->declspecs_seen_p)
4473 c_parser_error (parser, "expected specifier-qualifier-list");
4474 return NULL;
4476 if (specs->type != error_mark_node)
4478 pending_xref_error ();
4479 finish_declspecs (specs);
4481 declarator = c_parser_declarator (parser,
4482 specs->typespec_kind != ctsk_none,
4483 C_DTR_ABSTRACT, &dummy);
4484 if (declarator == NULL)
4485 return NULL;
4486 ret = XOBNEW (&parser_obstack, struct c_type_name);
4487 ret->specs = specs;
4488 ret->declarator = declarator;
4489 return ret;
4492 /* Parse an initializer (C90 6.5.7, C99 6.7.8, C11 6.7.9).
4494 initializer:
4495 assignment-expression
4496 { initializer-list }
4497 { initializer-list , }
4499 initializer-list:
4500 designation[opt] initializer
4501 initializer-list , designation[opt] initializer
4503 designation:
4504 designator-list =
4506 designator-list:
4507 designator
4508 designator-list designator
4510 designator:
4511 array-designator
4512 . identifier
4514 array-designator:
4515 [ constant-expression ]
4517 GNU extensions:
4519 initializer:
4522 designation:
4523 array-designator
4524 identifier :
4526 array-designator:
4527 [ constant-expression ... constant-expression ]
4529 Any expression without commas is accepted in the syntax for the
4530 constant-expressions, with non-constant expressions rejected later.
4532 This function is only used for top-level initializers; for nested
4533 ones, see c_parser_initval. */
4535 static struct c_expr
4536 c_parser_initializer (c_parser *parser)
4538 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
4539 return c_parser_braced_init (parser, NULL_TREE, false, NULL);
4540 else
4542 struct c_expr ret;
4543 location_t loc = c_parser_peek_token (parser)->location;
4544 ret = c_parser_expr_no_commas (parser, NULL);
4545 if (TREE_CODE (ret.value) != STRING_CST
4546 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR)
4547 ret = convert_lvalue_to_rvalue (loc, ret, true, true);
4548 return ret;
4552 /* The location of the last comma within the current initializer list,
4553 or UNKNOWN_LOCATION if not within one. */
4555 location_t last_init_list_comma;
4557 /* Parse a braced initializer list. TYPE is the type specified for a
4558 compound literal, and NULL_TREE for other initializers and for
4559 nested braced lists. NESTED_P is true for nested braced lists,
4560 false for the list of a compound literal or the list that is the
4561 top-level initializer in a declaration. */
4563 static struct c_expr
4564 c_parser_braced_init (c_parser *parser, tree type, bool nested_p,
4565 struct obstack *outer_obstack)
4567 struct c_expr ret;
4568 struct obstack braced_init_obstack;
4569 location_t brace_loc = c_parser_peek_token (parser)->location;
4570 gcc_obstack_init (&braced_init_obstack);
4571 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
4572 matching_braces braces;
4573 braces.consume_open (parser);
4574 if (nested_p)
4576 finish_implicit_inits (brace_loc, outer_obstack);
4577 push_init_level (brace_loc, 0, &braced_init_obstack);
4579 else
4580 really_start_incremental_init (type);
4581 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4583 pedwarn (brace_loc, OPT_Wpedantic, "ISO C forbids empty initializer braces");
4585 else
4587 /* Parse a non-empty initializer list, possibly with a trailing
4588 comma. */
4589 while (true)
4591 c_parser_initelt (parser, &braced_init_obstack);
4592 if (parser->error)
4593 break;
4594 if (c_parser_next_token_is (parser, CPP_COMMA))
4596 last_init_list_comma = c_parser_peek_token (parser)->location;
4597 c_parser_consume_token (parser);
4599 else
4600 break;
4601 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4602 break;
4605 c_token *next_tok = c_parser_peek_token (parser);
4606 if (next_tok->type != CPP_CLOSE_BRACE)
4608 ret.set_error ();
4609 ret.original_code = ERROR_MARK;
4610 ret.original_type = NULL;
4611 braces.skip_until_found_close (parser);
4612 pop_init_level (brace_loc, 0, &braced_init_obstack, last_init_list_comma);
4613 obstack_free (&braced_init_obstack, NULL);
4614 return ret;
4616 location_t close_loc = next_tok->location;
4617 c_parser_consume_token (parser);
4618 ret = pop_init_level (brace_loc, 0, &braced_init_obstack, close_loc);
4619 obstack_free (&braced_init_obstack, NULL);
4620 set_c_expr_source_range (&ret, brace_loc, close_loc);
4621 return ret;
4624 /* Parse a nested initializer, including designators. */
4626 static void
4627 c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
4629 /* Parse any designator or designator list. A single array
4630 designator may have the subsequent "=" omitted in GNU C, but a
4631 longer list or a structure member designator may not. */
4632 if (c_parser_next_token_is (parser, CPP_NAME)
4633 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
4635 /* Old-style structure member designator. */
4636 set_init_label (c_parser_peek_token (parser)->location,
4637 c_parser_peek_token (parser)->value,
4638 c_parser_peek_token (parser)->location,
4639 braced_init_obstack);
4640 /* Use the colon as the error location. */
4641 pedwarn (c_parser_peek_2nd_token (parser)->location, OPT_Wpedantic,
4642 "obsolete use of designated initializer with %<:%>");
4643 c_parser_consume_token (parser);
4644 c_parser_consume_token (parser);
4646 else
4648 /* des_seen is 0 if there have been no designators, 1 if there
4649 has been a single array designator and 2 otherwise. */
4650 int des_seen = 0;
4651 /* Location of a designator. */
4652 location_t des_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4653 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)
4654 || c_parser_next_token_is (parser, CPP_DOT))
4656 int des_prev = des_seen;
4657 if (!des_seen)
4658 des_loc = c_parser_peek_token (parser)->location;
4659 if (des_seen < 2)
4660 des_seen++;
4661 if (c_parser_next_token_is (parser, CPP_DOT))
4663 des_seen = 2;
4664 c_parser_consume_token (parser);
4665 if (c_parser_next_token_is (parser, CPP_NAME))
4667 set_init_label (des_loc, c_parser_peek_token (parser)->value,
4668 c_parser_peek_token (parser)->location,
4669 braced_init_obstack);
4670 c_parser_consume_token (parser);
4672 else
4674 struct c_expr init;
4675 init.set_error ();
4676 init.original_code = ERROR_MARK;
4677 init.original_type = NULL;
4678 c_parser_error (parser, "expected identifier");
4679 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4680 process_init_element (input_location, init, false,
4681 braced_init_obstack);
4682 return;
4685 else
4687 tree first, second;
4688 location_t ellipsis_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4689 location_t array_index_loc = UNKNOWN_LOCATION;
4690 /* ??? Following the old parser, [ objc-receiver
4691 objc-message-args ] is accepted as an initializer,
4692 being distinguished from a designator by what follows
4693 the first assignment expression inside the square
4694 brackets, but after a first array designator a
4695 subsequent square bracket is for Objective-C taken to
4696 start an expression, using the obsolete form of
4697 designated initializer without '=', rather than
4698 possibly being a second level of designation: in LALR
4699 terms, the '[' is shifted rather than reducing
4700 designator to designator-list. */
4701 if (des_prev == 1 && c_dialect_objc ())
4703 des_seen = des_prev;
4704 break;
4706 if (des_prev == 0 && c_dialect_objc ())
4708 /* This might be an array designator or an
4709 Objective-C message expression. If the former,
4710 continue parsing here; if the latter, parse the
4711 remainder of the initializer given the starting
4712 primary-expression. ??? It might make sense to
4713 distinguish when des_prev == 1 as well; see
4714 previous comment. */
4715 tree rec, args;
4716 struct c_expr mexpr;
4717 c_parser_consume_token (parser);
4718 if (c_parser_peek_token (parser)->type == CPP_NAME
4719 && ((c_parser_peek_token (parser)->id_kind
4720 == C_ID_TYPENAME)
4721 || (c_parser_peek_token (parser)->id_kind
4722 == C_ID_CLASSNAME)))
4724 /* Type name receiver. */
4725 tree id = c_parser_peek_token (parser)->value;
4726 c_parser_consume_token (parser);
4727 rec = objc_get_class_reference (id);
4728 goto parse_message_args;
4730 first = c_parser_expr_no_commas (parser, NULL).value;
4731 mark_exp_read (first);
4732 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)
4733 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4734 goto array_desig_after_first;
4735 /* Expression receiver. So far only one part
4736 without commas has been parsed; there might be
4737 more of the expression. */
4738 rec = first;
4739 while (c_parser_next_token_is (parser, CPP_COMMA))
4741 struct c_expr next;
4742 location_t comma_loc, exp_loc;
4743 comma_loc = c_parser_peek_token (parser)->location;
4744 c_parser_consume_token (parser);
4745 exp_loc = c_parser_peek_token (parser)->location;
4746 next = c_parser_expr_no_commas (parser, NULL);
4747 next = convert_lvalue_to_rvalue (exp_loc, next,
4748 true, true);
4749 rec = build_compound_expr (comma_loc, rec, next.value);
4751 parse_message_args:
4752 /* Now parse the objc-message-args. */
4753 args = c_parser_objc_message_args (parser);
4754 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4755 "expected %<]%>");
4756 mexpr.value
4757 = objc_build_message_expr (rec, args);
4758 mexpr.original_code = ERROR_MARK;
4759 mexpr.original_type = NULL;
4760 /* Now parse and process the remainder of the
4761 initializer, starting with this message
4762 expression as a primary-expression. */
4763 c_parser_initval (parser, &mexpr, braced_init_obstack);
4764 return;
4766 c_parser_consume_token (parser);
4767 array_index_loc = c_parser_peek_token (parser)->location;
4768 first = c_parser_expr_no_commas (parser, NULL).value;
4769 mark_exp_read (first);
4770 array_desig_after_first:
4771 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
4773 ellipsis_loc = c_parser_peek_token (parser)->location;
4774 c_parser_consume_token (parser);
4775 second = c_parser_expr_no_commas (parser, NULL).value;
4776 mark_exp_read (second);
4778 else
4779 second = NULL_TREE;
4780 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
4782 c_parser_consume_token (parser);
4783 set_init_index (array_index_loc, first, second,
4784 braced_init_obstack);
4785 if (second)
4786 pedwarn (ellipsis_loc, OPT_Wpedantic,
4787 "ISO C forbids specifying range of elements to initialize");
4789 else
4790 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
4791 "expected %<]%>");
4794 if (des_seen >= 1)
4796 if (c_parser_next_token_is (parser, CPP_EQ))
4798 pedwarn_c90 (des_loc, OPT_Wpedantic,
4799 "ISO C90 forbids specifying subobject "
4800 "to initialize");
4801 c_parser_consume_token (parser);
4803 else
4805 if (des_seen == 1)
4806 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
4807 "obsolete use of designated initializer without %<=%>");
4808 else
4810 struct c_expr init;
4811 init.set_error ();
4812 init.original_code = ERROR_MARK;
4813 init.original_type = NULL;
4814 c_parser_error (parser, "expected %<=%>");
4815 c_parser_skip_until_found (parser, CPP_COMMA, NULL);
4816 process_init_element (input_location, init, false,
4817 braced_init_obstack);
4818 return;
4823 c_parser_initval (parser, NULL, braced_init_obstack);
4826 /* Parse a nested initializer; as c_parser_initializer but parses
4827 initializers within braced lists, after any designators have been
4828 applied. If AFTER is not NULL then it is an Objective-C message
4829 expression which is the primary-expression starting the
4830 initializer. */
4832 static void
4833 c_parser_initval (c_parser *parser, struct c_expr *after,
4834 struct obstack * braced_init_obstack)
4836 struct c_expr init;
4837 gcc_assert (!after || c_dialect_objc ());
4838 location_t loc = c_parser_peek_token (parser)->location;
4840 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after)
4841 init = c_parser_braced_init (parser, NULL_TREE, true,
4842 braced_init_obstack);
4843 else
4845 init = c_parser_expr_no_commas (parser, after);
4846 if (init.value != NULL_TREE
4847 && TREE_CODE (init.value) != STRING_CST
4848 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
4849 init = convert_lvalue_to_rvalue (loc, init, true, true);
4851 process_init_element (loc, init, false, braced_init_obstack);
4854 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
4855 C99 6.8.2, C11 6.8.2).
4857 compound-statement:
4858 { block-item-list[opt] }
4859 { label-declarations block-item-list }
4861 block-item-list:
4862 block-item
4863 block-item-list block-item
4865 block-item:
4866 nested-declaration
4867 statement
4869 nested-declaration:
4870 declaration
4872 GNU extensions:
4874 compound-statement:
4875 { label-declarations block-item-list }
4877 nested-declaration:
4878 __extension__ nested-declaration
4879 nested-function-definition
4881 label-declarations:
4882 label-declaration
4883 label-declarations label-declaration
4885 label-declaration:
4886 __label__ identifier-list ;
4888 Allowing the mixing of declarations and code is new in C99. The
4889 GNU syntax also permits (not shown above) labels at the end of
4890 compound statements, which yield an error. We don't allow labels
4891 on declarations; this might seem like a natural extension, but
4892 there would be a conflict between attributes on the label and
4893 prefix attributes on the declaration. ??? The syntax follows the
4894 old parser in requiring something after label declarations.
4895 Although they are erroneous if the labels declared aren't defined,
4896 is it useful for the syntax to be this way?
4898 OpenACC:
4900 block-item:
4901 openacc-directive
4903 openacc-directive:
4904 update-directive
4906 OpenMP:
4908 block-item:
4909 openmp-directive
4911 openmp-directive:
4912 barrier-directive
4913 flush-directive
4914 taskwait-directive
4915 taskyield-directive
4916 cancel-directive
4917 cancellation-point-directive */
4919 static tree
4920 c_parser_compound_statement (c_parser *parser)
4922 tree stmt;
4923 location_t brace_loc;
4924 brace_loc = c_parser_peek_token (parser)->location;
4925 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
4927 /* Ensure a scope is entered and left anyway to avoid confusion
4928 if we have just prepared to enter a function body. */
4929 stmt = c_begin_compound_stmt (true);
4930 c_end_compound_stmt (brace_loc, stmt, true);
4931 return error_mark_node;
4933 stmt = c_begin_compound_stmt (true);
4934 c_parser_compound_statement_nostart (parser);
4936 return c_end_compound_stmt (brace_loc, stmt, true);
4939 /* Parse a compound statement except for the opening brace. This is
4940 used for parsing both compound statements and statement expressions
4941 (which follow different paths to handling the opening). */
4943 static void
4944 c_parser_compound_statement_nostart (c_parser *parser)
4946 bool last_stmt = false;
4947 bool last_label = false;
4948 bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
4949 location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
4950 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4952 add_debug_begin_stmt (c_parser_peek_token (parser)->location);
4953 c_parser_consume_token (parser);
4954 return;
4956 mark_valid_location_for_stdc_pragma (true);
4957 if (c_parser_next_token_is_keyword (parser, RID_LABEL))
4959 /* Read zero or more forward-declarations for labels that nested
4960 functions can jump to. */
4961 mark_valid_location_for_stdc_pragma (false);
4962 while (c_parser_next_token_is_keyword (parser, RID_LABEL))
4964 label_loc = c_parser_peek_token (parser)->location;
4965 c_parser_consume_token (parser);
4966 /* Any identifiers, including those declared as type names,
4967 are OK here. */
4968 while (true)
4970 tree label;
4971 if (c_parser_next_token_is_not (parser, CPP_NAME))
4973 c_parser_error (parser, "expected identifier");
4974 break;
4976 label
4977 = declare_label (c_parser_peek_token (parser)->value);
4978 C_DECLARED_LABEL_FLAG (label) = 1;
4979 add_stmt (build_stmt (label_loc, DECL_EXPR, label));
4980 c_parser_consume_token (parser);
4981 if (c_parser_next_token_is (parser, CPP_COMMA))
4982 c_parser_consume_token (parser);
4983 else
4984 break;
4986 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
4988 pedwarn (label_loc, OPT_Wpedantic, "ISO C forbids label declarations");
4990 /* We must now have at least one statement, label or declaration. */
4991 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
4993 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
4994 c_parser_error (parser, "expected declaration or statement");
4995 c_parser_consume_token (parser);
4996 return;
4998 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE))
5000 location_t loc = c_parser_peek_token (parser)->location;
5001 loc = expansion_point_location_if_in_system_header (loc);
5002 if (c_parser_next_token_is_keyword (parser, RID_CASE)
5003 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5004 || (c_parser_next_token_is (parser, CPP_NAME)
5005 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5007 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5008 label_loc = c_parser_peek_2nd_token (parser)->location;
5009 else
5010 label_loc = c_parser_peek_token (parser)->location;
5011 last_label = true;
5012 last_stmt = false;
5013 mark_valid_location_for_stdc_pragma (false);
5014 c_parser_label (parser);
5016 else if (!last_label
5017 && c_parser_next_tokens_start_declaration (parser))
5019 last_label = false;
5020 mark_valid_location_for_stdc_pragma (false);
5021 bool fallthru_attr_p = false;
5022 c_parser_declaration_or_fndef (parser, true, true, true, true,
5023 true, NULL, vNULL, NULL,
5024 &fallthru_attr_p);
5025 if (last_stmt && !fallthru_attr_p)
5026 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5027 "ISO C90 forbids mixed declarations and code");
5028 last_stmt = fallthru_attr_p;
5030 else if (!last_label
5031 && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
5033 /* __extension__ can start a declaration, but is also an
5034 unary operator that can start an expression. Consume all
5035 but the last of a possible series of __extension__ to
5036 determine which. */
5037 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
5038 && (c_parser_peek_2nd_token (parser)->keyword
5039 == RID_EXTENSION))
5040 c_parser_consume_token (parser);
5041 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
5043 int ext;
5044 ext = disable_extension_diagnostics ();
5045 c_parser_consume_token (parser);
5046 last_label = false;
5047 mark_valid_location_for_stdc_pragma (false);
5048 c_parser_declaration_or_fndef (parser, true, true, true, true,
5049 true, NULL, vNULL);
5050 /* Following the old parser, __extension__ does not
5051 disable this diagnostic. */
5052 restore_extension_diagnostics (ext);
5053 if (last_stmt)
5054 pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
5055 "ISO C90 forbids mixed declarations and code");
5056 last_stmt = false;
5058 else
5059 goto statement;
5061 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
5063 /* External pragmas, and some omp pragmas, are not associated
5064 with regular c code, and so are not to be considered statements
5065 syntactically. This ensures that the user doesn't put them
5066 places that would turn into syntax errors if the directive
5067 were ignored. */
5068 if (c_parser_pragma (parser,
5069 last_label ? pragma_stmt : pragma_compound,
5070 NULL))
5071 last_label = false, last_stmt = true;
5073 else if (c_parser_next_token_is (parser, CPP_EOF))
5075 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5076 c_parser_error (parser, "expected declaration or statement");
5077 return;
5079 else if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5081 if (parser->in_if_block)
5083 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5084 error_at (loc, "expected %<}%> before %<else%>");
5085 return;
5087 else
5089 error_at (loc, "%<else%> without a previous %<if%>");
5090 c_parser_consume_token (parser);
5091 continue;
5094 else
5096 statement:
5097 last_label = false;
5098 last_stmt = true;
5099 mark_valid_location_for_stdc_pragma (false);
5100 c_parser_statement_after_labels (parser, NULL);
5103 parser->error = false;
5105 if (last_label)
5106 error_at (label_loc, "label at end of compound statement");
5107 c_parser_consume_token (parser);
5108 /* Restore the value we started with. */
5109 mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
5112 /* Parse all consecutive labels. */
5114 static void
5115 c_parser_all_labels (c_parser *parser)
5117 while (c_parser_next_token_is_keyword (parser, RID_CASE)
5118 || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
5119 || (c_parser_next_token_is (parser, CPP_NAME)
5120 && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
5121 c_parser_label (parser);
5124 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
5126 label:
5127 identifier : attributes[opt]
5128 case constant-expression :
5129 default :
5131 GNU extensions:
5133 label:
5134 case constant-expression ... constant-expression :
5136 The use of attributes on labels is a GNU extension. The syntax in
5137 GNU C accepts any expressions without commas, non-constant
5138 expressions being rejected later. */
5140 static void
5141 c_parser_label (c_parser *parser)
5143 location_t loc1 = c_parser_peek_token (parser)->location;
5144 tree label = NULL_TREE;
5146 /* Remember whether this case or a user-defined label is allowed to fall
5147 through to. */
5148 bool fallthrough_p = c_parser_peek_token (parser)->flags & PREV_FALLTHROUGH;
5150 if (c_parser_next_token_is_keyword (parser, RID_CASE))
5152 tree exp1, exp2;
5153 c_parser_consume_token (parser);
5154 exp1 = c_parser_expr_no_commas (parser, NULL).value;
5155 if (c_parser_next_token_is (parser, CPP_COLON))
5157 c_parser_consume_token (parser);
5158 label = do_case (loc1, exp1, NULL_TREE);
5160 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
5162 c_parser_consume_token (parser);
5163 exp2 = c_parser_expr_no_commas (parser, NULL).value;
5164 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5165 label = do_case (loc1, exp1, exp2);
5167 else
5168 c_parser_error (parser, "expected %<:%> or %<...%>");
5170 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
5172 c_parser_consume_token (parser);
5173 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
5174 label = do_case (loc1, NULL_TREE, NULL_TREE);
5176 else
5178 tree name = c_parser_peek_token (parser)->value;
5179 tree tlab;
5180 tree attrs;
5181 location_t loc2 = c_parser_peek_token (parser)->location;
5182 gcc_assert (c_parser_next_token_is (parser, CPP_NAME));
5183 c_parser_consume_token (parser);
5184 gcc_assert (c_parser_next_token_is (parser, CPP_COLON));
5185 c_parser_consume_token (parser);
5186 attrs = c_parser_attributes (parser);
5187 tlab = define_label (loc2, name);
5188 if (tlab)
5190 decl_attributes (&tlab, attrs, 0);
5191 label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
5194 if (label)
5196 if (TREE_CODE (label) == LABEL_EXPR)
5197 FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
5198 else
5199 FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
5201 /* Allow '__attribute__((fallthrough));'. */
5202 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
5204 location_t loc = c_parser_peek_token (parser)->location;
5205 tree attrs = c_parser_attributes (parser);
5206 if (attribute_fallthrough_p (attrs))
5208 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5210 tree fn = build_call_expr_internal_loc (loc,
5211 IFN_FALLTHROUGH,
5212 void_type_node, 0);
5213 add_stmt (fn);
5215 else
5216 warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
5217 "not followed by %<;%>");
5219 else if (attrs != NULL_TREE)
5220 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5221 " can be applied to a null statement");
5223 if (c_parser_next_tokens_start_declaration (parser))
5225 error_at (c_parser_peek_token (parser)->location,
5226 "a label can only be part of a statement and "
5227 "a declaration is not a statement");
5228 c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
5229 /*static_assert_ok*/ true,
5230 /*empty_ok*/ true, /*nested*/ true,
5231 /*start_attr_ok*/ true, NULL,
5232 vNULL);
5237 /* Parse a statement (C90 6.6, C99 6.8, C11 6.8).
5239 statement:
5240 labeled-statement
5241 compound-statement
5242 expression-statement
5243 selection-statement
5244 iteration-statement
5245 jump-statement
5247 labeled-statement:
5248 label statement
5250 expression-statement:
5251 expression[opt] ;
5253 selection-statement:
5254 if-statement
5255 switch-statement
5257 iteration-statement:
5258 while-statement
5259 do-statement
5260 for-statement
5262 jump-statement:
5263 goto identifier ;
5264 continue ;
5265 break ;
5266 return expression[opt] ;
5268 GNU extensions:
5270 statement:
5271 asm-statement
5273 jump-statement:
5274 goto * expression ;
5276 expression-statement:
5277 attributes ;
5279 Objective-C:
5281 statement:
5282 objc-throw-statement
5283 objc-try-catch-statement
5284 objc-synchronized-statement
5286 objc-throw-statement:
5287 @throw expression ;
5288 @throw ;
5290 OpenACC:
5292 statement:
5293 openacc-construct
5295 openacc-construct:
5296 parallel-construct
5297 kernels-construct
5298 data-construct
5299 loop-construct
5301 parallel-construct:
5302 parallel-directive structured-block
5304 kernels-construct:
5305 kernels-directive structured-block
5307 data-construct:
5308 data-directive structured-block
5310 loop-construct:
5311 loop-directive structured-block
5313 OpenMP:
5315 statement:
5316 openmp-construct
5318 openmp-construct:
5319 parallel-construct
5320 for-construct
5321 simd-construct
5322 for-simd-construct
5323 sections-construct
5324 single-construct
5325 parallel-for-construct
5326 parallel-for-simd-construct
5327 parallel-sections-construct
5328 master-construct
5329 critical-construct
5330 atomic-construct
5331 ordered-construct
5333 parallel-construct:
5334 parallel-directive structured-block
5336 for-construct:
5337 for-directive iteration-statement
5339 simd-construct:
5340 simd-directive iteration-statements
5342 for-simd-construct:
5343 for-simd-directive iteration-statements
5345 sections-construct:
5346 sections-directive section-scope
5348 single-construct:
5349 single-directive structured-block
5351 parallel-for-construct:
5352 parallel-for-directive iteration-statement
5354 parallel-for-simd-construct:
5355 parallel-for-simd-directive iteration-statement
5357 parallel-sections-construct:
5358 parallel-sections-directive section-scope
5360 master-construct:
5361 master-directive structured-block
5363 critical-construct:
5364 critical-directive structured-block
5366 atomic-construct:
5367 atomic-directive expression-statement
5369 ordered-construct:
5370 ordered-directive structured-block
5372 Transactional Memory:
5374 statement:
5375 transaction-statement
5376 transaction-cancel-statement
5378 IF_P is used to track whether there's a (possibly labeled) if statement
5379 which is not enclosed in braces and has an else clause. This is used to
5380 implement -Wparentheses. */
5382 static void
5383 c_parser_statement (c_parser *parser, bool *if_p, location_t *loc_after_labels)
5385 c_parser_all_labels (parser);
5386 if (loc_after_labels)
5387 *loc_after_labels = c_parser_peek_token (parser)->location;
5388 c_parser_statement_after_labels (parser, if_p, NULL);
5391 /* Parse a statement, other than a labeled statement. CHAIN is a vector
5392 of if-else-if conditions.
5394 IF_P is used to track whether there's a (possibly labeled) if statement
5395 which is not enclosed in braces and has an else clause. This is used to
5396 implement -Wparentheses. */
5398 static void
5399 c_parser_statement_after_labels (c_parser *parser, bool *if_p,
5400 vec<tree> *chain)
5402 location_t loc = c_parser_peek_token (parser)->location;
5403 tree stmt = NULL_TREE;
5404 bool in_if_block = parser->in_if_block;
5405 parser->in_if_block = false;
5406 if (if_p != NULL)
5407 *if_p = false;
5409 if (c_parser_peek_token (parser)->type != CPP_OPEN_BRACE)
5410 add_debug_begin_stmt (loc);
5412 switch (c_parser_peek_token (parser)->type)
5414 case CPP_OPEN_BRACE:
5415 add_stmt (c_parser_compound_statement (parser));
5416 break;
5417 case CPP_KEYWORD:
5418 switch (c_parser_peek_token (parser)->keyword)
5420 case RID_IF:
5421 c_parser_if_statement (parser, if_p, chain);
5422 break;
5423 case RID_SWITCH:
5424 c_parser_switch_statement (parser, if_p);
5425 break;
5426 case RID_WHILE:
5427 c_parser_while_statement (parser, false, 0, if_p);
5428 break;
5429 case RID_DO:
5430 c_parser_do_statement (parser, 0, false);
5431 break;
5432 case RID_FOR:
5433 c_parser_for_statement (parser, false, 0, if_p);
5434 break;
5435 case RID_GOTO:
5436 c_parser_consume_token (parser);
5437 if (c_parser_next_token_is (parser, CPP_NAME))
5439 stmt = c_finish_goto_label (loc,
5440 c_parser_peek_token (parser)->value);
5441 c_parser_consume_token (parser);
5443 else if (c_parser_next_token_is (parser, CPP_MULT))
5445 struct c_expr val;
5447 c_parser_consume_token (parser);
5448 val = c_parser_expression (parser);
5449 val = convert_lvalue_to_rvalue (loc, val, false, true);
5450 stmt = c_finish_goto_ptr (loc, val.value);
5452 else
5453 c_parser_error (parser, "expected identifier or %<*%>");
5454 goto expect_semicolon;
5455 case RID_CONTINUE:
5456 c_parser_consume_token (parser);
5457 stmt = c_finish_bc_stmt (loc, &c_cont_label, false);
5458 goto expect_semicolon;
5459 case RID_BREAK:
5460 c_parser_consume_token (parser);
5461 stmt = c_finish_bc_stmt (loc, &c_break_label, true);
5462 goto expect_semicolon;
5463 case RID_RETURN:
5464 c_parser_consume_token (parser);
5465 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5467 stmt = c_finish_return (loc, NULL_TREE, NULL_TREE);
5468 c_parser_consume_token (parser);
5470 else
5472 location_t xloc = c_parser_peek_token (parser)->location;
5473 struct c_expr expr = c_parser_expression_conv (parser);
5474 mark_exp_read (expr.value);
5475 stmt = c_finish_return (EXPR_LOC_OR_LOC (expr.value, xloc),
5476 expr.value, expr.original_type);
5477 goto expect_semicolon;
5479 break;
5480 case RID_ASM:
5481 stmt = c_parser_asm_statement (parser);
5482 break;
5483 case RID_TRANSACTION_ATOMIC:
5484 case RID_TRANSACTION_RELAXED:
5485 stmt = c_parser_transaction (parser,
5486 c_parser_peek_token (parser)->keyword);
5487 break;
5488 case RID_TRANSACTION_CANCEL:
5489 stmt = c_parser_transaction_cancel (parser);
5490 goto expect_semicolon;
5491 case RID_AT_THROW:
5492 gcc_assert (c_dialect_objc ());
5493 c_parser_consume_token (parser);
5494 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5496 stmt = objc_build_throw_stmt (loc, NULL_TREE);
5497 c_parser_consume_token (parser);
5499 else
5501 struct c_expr expr = c_parser_expression (parser);
5502 expr = convert_lvalue_to_rvalue (loc, expr, false, false);
5503 expr.value = c_fully_fold (expr.value, false, NULL);
5504 stmt = objc_build_throw_stmt (loc, expr.value);
5505 goto expect_semicolon;
5507 break;
5508 case RID_AT_TRY:
5509 gcc_assert (c_dialect_objc ());
5510 c_parser_objc_try_catch_finally_statement (parser);
5511 break;
5512 case RID_AT_SYNCHRONIZED:
5513 gcc_assert (c_dialect_objc ());
5514 c_parser_objc_synchronized_statement (parser);
5515 break;
5516 case RID_ATTRIBUTE:
5518 /* Allow '__attribute__((fallthrough));'. */
5519 tree attrs = c_parser_attributes (parser);
5520 if (attribute_fallthrough_p (attrs))
5522 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5524 tree fn = build_call_expr_internal_loc (loc,
5525 IFN_FALLTHROUGH,
5526 void_type_node, 0);
5527 add_stmt (fn);
5528 /* Eat the ';'. */
5529 c_parser_consume_token (parser);
5531 else
5532 warning_at (loc, OPT_Wattributes,
5533 "%<fallthrough%> attribute not followed "
5534 "by %<;%>");
5536 else if (attrs != NULL_TREE)
5537 warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
5538 " can be applied to a null statement");
5539 break;
5541 default:
5542 goto expr_stmt;
5544 break;
5545 case CPP_SEMICOLON:
5546 c_parser_consume_token (parser);
5547 break;
5548 case CPP_CLOSE_PAREN:
5549 case CPP_CLOSE_SQUARE:
5550 /* Avoid infinite loop in error recovery:
5551 c_parser_skip_until_found stops at a closing nesting
5552 delimiter without consuming it, but here we need to consume
5553 it to proceed further. */
5554 c_parser_error (parser, "expected statement");
5555 c_parser_consume_token (parser);
5556 break;
5557 case CPP_PRAGMA:
5558 c_parser_pragma (parser, pragma_stmt, if_p);
5559 break;
5560 default:
5561 expr_stmt:
5562 stmt = c_finish_expr_stmt (loc, c_parser_expression_conv (parser).value);
5563 expect_semicolon:
5564 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
5565 break;
5567 /* Two cases cannot and do not have line numbers associated: If stmt
5568 is degenerate, such as "2;", then stmt is an INTEGER_CST, which
5569 cannot hold line numbers. But that's OK because the statement
5570 will either be changed to a MODIFY_EXPR during gimplification of
5571 the statement expr, or discarded. If stmt was compound, but
5572 without new variables, we will have skipped the creation of a
5573 BIND and will have a bare STATEMENT_LIST. But that's OK because
5574 (recursively) all of the component statements should already have
5575 line numbers assigned. ??? Can we discard no-op statements
5576 earlier? */
5577 if (EXPR_LOCATION (stmt) == UNKNOWN_LOCATION)
5578 protected_set_expr_location (stmt, loc);
5580 parser->in_if_block = in_if_block;
5583 /* Parse the condition from an if, do, while or for statements. */
5585 static tree
5586 c_parser_condition (c_parser *parser)
5588 location_t loc = c_parser_peek_token (parser)->location;
5589 tree cond;
5590 cond = c_parser_expression_conv (parser).value;
5591 cond = c_objc_common_truthvalue_conversion (loc, cond);
5592 cond = c_fully_fold (cond, false, NULL);
5593 if (warn_sequence_point)
5594 verify_sequence_points (cond);
5595 return cond;
5598 /* Parse a parenthesized condition from an if, do or while statement.
5600 condition:
5601 ( expression )
5603 static tree
5604 c_parser_paren_condition (c_parser *parser)
5606 tree cond;
5607 matching_parens parens;
5608 if (!parens.require_open (parser))
5609 return error_mark_node;
5610 cond = c_parser_condition (parser);
5611 parens.skip_until_found_close (parser);
5612 return cond;
5615 /* Parse a statement which is a block in C99.
5617 IF_P is used to track whether there's a (possibly labeled) if statement
5618 which is not enclosed in braces and has an else clause. This is used to
5619 implement -Wparentheses. */
5621 static tree
5622 c_parser_c99_block_statement (c_parser *parser, bool *if_p,
5623 location_t *loc_after_labels)
5625 tree block = c_begin_compound_stmt (flag_isoc99);
5626 location_t loc = c_parser_peek_token (parser)->location;
5627 c_parser_statement (parser, if_p, loc_after_labels);
5628 return c_end_compound_stmt (loc, block, flag_isoc99);
5631 /* Parse the body of an if statement. This is just parsing a
5632 statement but (a) it is a block in C99, (b) we track whether the
5633 body is an if statement for the sake of -Wparentheses warnings, (c)
5634 we handle an empty body specially for the sake of -Wempty-body
5635 warnings, and (d) we call parser_compound_statement directly
5636 because c_parser_statement_after_labels resets
5637 parser->in_if_block.
5639 IF_P is used to track whether there's a (possibly labeled) if statement
5640 which is not enclosed in braces and has an else clause. This is used to
5641 implement -Wparentheses. */
5643 static tree
5644 c_parser_if_body (c_parser *parser, bool *if_p,
5645 const token_indent_info &if_tinfo)
5647 tree block = c_begin_compound_stmt (flag_isoc99);
5648 location_t body_loc = c_parser_peek_token (parser)->location;
5649 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5650 token_indent_info body_tinfo
5651 = get_token_indent_info (c_parser_peek_token (parser));
5653 c_parser_all_labels (parser);
5654 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5656 location_t loc = c_parser_peek_token (parser)->location;
5657 add_stmt (build_empty_stmt (loc));
5658 c_parser_consume_token (parser);
5659 if (!c_parser_next_token_is_keyword (parser, RID_ELSE))
5660 warning_at (loc, OPT_Wempty_body,
5661 "suggest braces around empty body in an %<if%> statement");
5663 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5664 add_stmt (c_parser_compound_statement (parser));
5665 else
5667 body_loc_after_labels = c_parser_peek_token (parser)->location;
5668 c_parser_statement_after_labels (parser, if_p);
5671 token_indent_info next_tinfo
5672 = get_token_indent_info (c_parser_peek_token (parser));
5673 warn_for_misleading_indentation (if_tinfo, body_tinfo, next_tinfo);
5674 if (body_loc_after_labels != UNKNOWN_LOCATION
5675 && next_tinfo.type != CPP_SEMICOLON)
5676 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5677 if_tinfo.location, RID_IF);
5679 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5682 /* Parse the else body of an if statement. This is just parsing a
5683 statement but (a) it is a block in C99, (b) we handle an empty body
5684 specially for the sake of -Wempty-body warnings. CHAIN is a vector
5685 of if-else-if conditions. */
5687 static tree
5688 c_parser_else_body (c_parser *parser, const token_indent_info &else_tinfo,
5689 vec<tree> *chain)
5691 location_t body_loc = c_parser_peek_token (parser)->location;
5692 tree block = c_begin_compound_stmt (flag_isoc99);
5693 token_indent_info body_tinfo
5694 = get_token_indent_info (c_parser_peek_token (parser));
5695 location_t body_loc_after_labels = UNKNOWN_LOCATION;
5697 c_parser_all_labels (parser);
5698 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5700 location_t loc = c_parser_peek_token (parser)->location;
5701 warning_at (loc,
5702 OPT_Wempty_body,
5703 "suggest braces around empty body in an %<else%> statement");
5704 add_stmt (build_empty_stmt (loc));
5705 c_parser_consume_token (parser);
5707 else
5709 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
5710 body_loc_after_labels = c_parser_peek_token (parser)->location;
5711 c_parser_statement_after_labels (parser, NULL, chain);
5714 token_indent_info next_tinfo
5715 = get_token_indent_info (c_parser_peek_token (parser));
5716 warn_for_misleading_indentation (else_tinfo, body_tinfo, next_tinfo);
5717 if (body_loc_after_labels != UNKNOWN_LOCATION
5718 && next_tinfo.type != CPP_SEMICOLON)
5719 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
5720 else_tinfo.location, RID_ELSE);
5722 return c_end_compound_stmt (body_loc, block, flag_isoc99);
5725 /* We might need to reclassify any previously-lexed identifier, e.g.
5726 when we've left a for loop with an if-statement without else in the
5727 body - we might have used a wrong scope for the token. See PR67784. */
5729 static void
5730 c_parser_maybe_reclassify_token (c_parser *parser)
5732 if (c_parser_next_token_is (parser, CPP_NAME))
5734 c_token *token = c_parser_peek_token (parser);
5736 if (token->id_kind != C_ID_CLASSNAME)
5738 tree decl = lookup_name (token->value);
5740 token->id_kind = C_ID_ID;
5741 if (decl)
5743 if (TREE_CODE (decl) == TYPE_DECL)
5744 token->id_kind = C_ID_TYPENAME;
5746 else if (c_dialect_objc ())
5748 tree objc_interface_decl = objc_is_class_name (token->value);
5749 /* Objective-C class names are in the same namespace as
5750 variables and typedefs, and hence are shadowed by local
5751 declarations. */
5752 if (objc_interface_decl)
5754 token->value = objc_interface_decl;
5755 token->id_kind = C_ID_CLASSNAME;
5762 /* Parse an if statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5764 if-statement:
5765 if ( expression ) statement
5766 if ( expression ) statement else statement
5768 CHAIN is a vector of if-else-if conditions.
5769 IF_P is used to track whether there's a (possibly labeled) if statement
5770 which is not enclosed in braces and has an else clause. This is used to
5771 implement -Wparentheses. */
5773 static void
5774 c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
5776 tree block;
5777 location_t loc;
5778 tree cond;
5779 bool nested_if = false;
5780 tree first_body, second_body;
5781 bool in_if_block;
5783 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF));
5784 token_indent_info if_tinfo
5785 = get_token_indent_info (c_parser_peek_token (parser));
5786 c_parser_consume_token (parser);
5787 block = c_begin_compound_stmt (flag_isoc99);
5788 loc = c_parser_peek_token (parser)->location;
5789 cond = c_parser_paren_condition (parser);
5790 in_if_block = parser->in_if_block;
5791 parser->in_if_block = true;
5792 first_body = c_parser_if_body (parser, &nested_if, if_tinfo);
5793 parser->in_if_block = in_if_block;
5795 if (warn_duplicated_cond)
5796 warn_duplicated_cond_add_or_warn (EXPR_LOCATION (cond), cond, &chain);
5798 if (c_parser_next_token_is_keyword (parser, RID_ELSE))
5800 token_indent_info else_tinfo
5801 = get_token_indent_info (c_parser_peek_token (parser));
5802 c_parser_consume_token (parser);
5803 if (warn_duplicated_cond)
5805 if (c_parser_next_token_is_keyword (parser, RID_IF)
5806 && chain == NULL)
5808 /* We've got "if (COND) else if (COND2)". Start the
5809 condition chain and add COND as the first element. */
5810 chain = new vec<tree> ();
5811 if (!CONSTANT_CLASS_P (cond) && !TREE_SIDE_EFFECTS (cond))
5812 chain->safe_push (cond);
5814 else if (!c_parser_next_token_is_keyword (parser, RID_IF))
5816 /* This is if-else without subsequent if. Zap the condition
5817 chain; we would have already warned at this point. */
5818 delete chain;
5819 chain = NULL;
5822 second_body = c_parser_else_body (parser, else_tinfo, chain);
5823 /* Set IF_P to true to indicate that this if statement has an
5824 else clause. This may trigger the Wparentheses warning
5825 below when we get back up to the parent if statement. */
5826 if (if_p != NULL)
5827 *if_p = true;
5829 else
5831 second_body = NULL_TREE;
5833 /* Diagnose an ambiguous else if if-then-else is nested inside
5834 if-then. */
5835 if (nested_if)
5836 warning_at (loc, OPT_Wdangling_else,
5837 "suggest explicit braces to avoid ambiguous %<else%>");
5839 if (warn_duplicated_cond)
5841 /* This if statement does not have an else clause. We don't
5842 need the condition chain anymore. */
5843 delete chain;
5844 chain = NULL;
5847 c_finish_if_stmt (loc, cond, first_body, second_body);
5848 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5850 c_parser_maybe_reclassify_token (parser);
5853 /* Parse a switch statement (C90 6.6.4, C99 6.8.4, C11 6.8.4).
5855 switch-statement:
5856 switch (expression) statement
5859 static void
5860 c_parser_switch_statement (c_parser *parser, bool *if_p)
5862 struct c_expr ce;
5863 tree block, expr, body, save_break;
5864 location_t switch_loc = c_parser_peek_token (parser)->location;
5865 location_t switch_cond_loc;
5866 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH));
5867 c_parser_consume_token (parser);
5868 block = c_begin_compound_stmt (flag_isoc99);
5869 bool explicit_cast_p = false;
5870 matching_parens parens;
5871 if (parens.require_open (parser))
5873 switch_cond_loc = c_parser_peek_token (parser)->location;
5874 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
5875 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
5876 explicit_cast_p = true;
5877 ce = c_parser_expression (parser);
5878 ce = convert_lvalue_to_rvalue (switch_cond_loc, ce, true, false);
5879 expr = ce.value;
5880 /* ??? expr has no valid location? */
5881 parens.skip_until_found_close (parser);
5883 else
5885 switch_cond_loc = UNKNOWN_LOCATION;
5886 expr = error_mark_node;
5887 ce.original_type = error_mark_node;
5889 c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
5890 save_break = c_break_label;
5891 c_break_label = NULL_TREE;
5892 location_t loc_after_labels;
5893 bool open_brace_p = c_parser_peek_token (parser)->type == CPP_OPEN_BRACE;
5894 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5895 location_t next_loc = c_parser_peek_token (parser)->location;
5896 if (!open_brace_p && c_parser_peek_token (parser)->type != CPP_SEMICOLON)
5897 warn_for_multistatement_macros (loc_after_labels, next_loc, switch_loc,
5898 RID_SWITCH);
5899 if (c_break_label)
5901 location_t here = c_parser_peek_token (parser)->location;
5902 tree t = build1 (LABEL_EXPR, void_type_node, c_break_label);
5903 SET_EXPR_LOCATION (t, here);
5904 SWITCH_BREAK_LABEL_P (c_break_label) = 1;
5905 append_to_statement_list_force (t, &body);
5907 c_finish_case (body, ce.original_type);
5908 c_break_label = save_break;
5909 add_stmt (c_end_compound_stmt (switch_loc, block, flag_isoc99));
5910 c_parser_maybe_reclassify_token (parser);
5913 /* Parse a while statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5915 while-statement:
5916 while (expression) statement
5918 IF_P is used to track whether there's a (possibly labeled) if statement
5919 which is not enclosed in braces and has an else clause. This is used to
5920 implement -Wparentheses. */
5922 static void
5923 c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
5924 bool *if_p)
5926 tree block, cond, body, save_break, save_cont;
5927 location_t loc;
5928 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
5929 token_indent_info while_tinfo
5930 = get_token_indent_info (c_parser_peek_token (parser));
5931 c_parser_consume_token (parser);
5932 block = c_begin_compound_stmt (flag_isoc99);
5933 loc = c_parser_peek_token (parser)->location;
5934 cond = c_parser_paren_condition (parser);
5935 if (ivdep && cond != error_mark_node)
5936 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5937 build_int_cst (integer_type_node,
5938 annot_expr_ivdep_kind),
5939 integer_zero_node);
5940 if (unroll && cond != error_mark_node)
5941 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
5942 build_int_cst (integer_type_node,
5943 annot_expr_unroll_kind),
5944 build_int_cst (integer_type_node, unroll));
5945 save_break = c_break_label;
5946 c_break_label = NULL_TREE;
5947 save_cont = c_cont_label;
5948 c_cont_label = NULL_TREE;
5950 token_indent_info body_tinfo
5951 = get_token_indent_info (c_parser_peek_token (parser));
5953 location_t loc_after_labels;
5954 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
5955 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
5956 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true);
5957 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
5958 c_parser_maybe_reclassify_token (parser);
5960 token_indent_info next_tinfo
5961 = get_token_indent_info (c_parser_peek_token (parser));
5962 warn_for_misleading_indentation (while_tinfo, body_tinfo, next_tinfo);
5964 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
5965 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
5966 while_tinfo.location, RID_WHILE);
5968 c_break_label = save_break;
5969 c_cont_label = save_cont;
5972 /* Parse a do statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
5974 do-statement:
5975 do statement while ( expression ) ;
5978 static void
5979 c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
5981 tree block, cond, body, save_break, save_cont, new_break, new_cont;
5982 location_t loc;
5983 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
5984 c_parser_consume_token (parser);
5985 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
5986 warning_at (c_parser_peek_token (parser)->location,
5987 OPT_Wempty_body,
5988 "suggest braces around empty body in %<do%> statement");
5989 block = c_begin_compound_stmt (flag_isoc99);
5990 loc = c_parser_peek_token (parser)->location;
5991 save_break = c_break_label;
5992 c_break_label = NULL_TREE;
5993 save_cont = c_cont_label;
5994 c_cont_label = NULL_TREE;
5995 body = c_parser_c99_block_statement (parser, NULL);
5996 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>");
5997 new_break = c_break_label;
5998 c_break_label = save_break;
5999 new_cont = c_cont_label;
6000 c_cont_label = save_cont;
6001 cond = c_parser_paren_condition (parser);
6002 if (ivdep && cond != error_mark_node)
6003 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6004 build_int_cst (integer_type_node,
6005 annot_expr_ivdep_kind),
6006 integer_zero_node);
6007 if (unroll && cond != error_mark_node)
6008 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6009 build_int_cst (integer_type_node,
6010 annot_expr_unroll_kind),
6011 build_int_cst (integer_type_node, unroll));
6012 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6013 c_parser_skip_to_end_of_block_or_statement (parser);
6014 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false);
6015 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
6018 /* Parse a for statement (C90 6.6.5, C99 6.8.5, C11 6.8.5).
6020 for-statement:
6021 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
6022 for ( nested-declaration expression[opt] ; expression[opt] ) statement
6024 The form with a declaration is new in C99.
6026 ??? In accordance with the old parser, the declaration may be a
6027 nested function, which is then rejected in check_for_loop_decls,
6028 but does it make any sense for this to be included in the grammar?
6029 Note in particular that the nested function does not include a
6030 trailing ';', whereas the "declaration" production includes one.
6031 Also, can we reject bad declarations earlier and cheaper than
6032 check_for_loop_decls?
6034 In Objective-C, there are two additional variants:
6036 foreach-statement:
6037 for ( expression in expresssion ) statement
6038 for ( declaration in expression ) statement
6040 This is inconsistent with C, because the second variant is allowed
6041 even if c99 is not enabled.
6043 The rest of the comment documents these Objective-C foreach-statement.
6045 Here is the canonical example of the first variant:
6046 for (object in array) { do something with object }
6047 we call the first expression ("object") the "object_expression" and
6048 the second expression ("array") the "collection_expression".
6049 object_expression must be an lvalue of type "id" (a generic Objective-C
6050 object) because the loop works by assigning to object_expression the
6051 various objects from the collection_expression. collection_expression
6052 must evaluate to something of type "id" which responds to the method
6053 countByEnumeratingWithState:objects:count:.
6055 The canonical example of the second variant is:
6056 for (id object in array) { do something with object }
6057 which is completely equivalent to
6059 id object;
6060 for (object in array) { do something with object }
6062 Note that initizializing 'object' in some way (eg, "for ((object =
6063 xxx) in array) { do something with object }") is possibly
6064 technically valid, but completely pointless as 'object' will be
6065 assigned to something else as soon as the loop starts. We should
6066 most likely reject it (TODO).
6068 The beginning of the Objective-C foreach-statement looks exactly
6069 like the beginning of the for-statement, and we can tell it is a
6070 foreach-statement only because the initial declaration or
6071 expression is terminated by 'in' instead of ';'.
6073 IF_P is used to track whether there's a (possibly labeled) if statement
6074 which is not enclosed in braces and has an else clause. This is used to
6075 implement -Wparentheses. */
6077 static void
6078 c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
6079 bool *if_p)
6081 tree block, cond, incr, save_break, save_cont, body;
6082 /* The following are only used when parsing an ObjC foreach statement. */
6083 tree object_expression;
6084 /* Silence the bogus uninitialized warning. */
6085 tree collection_expression = NULL;
6086 location_t loc = c_parser_peek_token (parser)->location;
6087 location_t for_loc = c_parser_peek_token (parser)->location;
6088 bool is_foreach_statement = false;
6089 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
6090 token_indent_info for_tinfo
6091 = get_token_indent_info (c_parser_peek_token (parser));
6092 c_parser_consume_token (parser);
6093 /* Open a compound statement in Objective-C as well, just in case this is
6094 as foreach expression. */
6095 block = c_begin_compound_stmt (flag_isoc99 || c_dialect_objc ());
6096 cond = error_mark_node;
6097 incr = error_mark_node;
6098 matching_parens parens;
6099 if (parens.require_open (parser))
6101 /* Parse the initialization declaration or expression. */
6102 object_expression = error_mark_node;
6103 parser->objc_could_be_foreach_context = c_dialect_objc ();
6104 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6106 parser->objc_could_be_foreach_context = false;
6107 c_parser_consume_token (parser);
6108 c_finish_expr_stmt (loc, NULL_TREE);
6110 else if (c_parser_next_tokens_start_declaration (parser))
6112 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
6113 &object_expression, vNULL);
6114 parser->objc_could_be_foreach_context = false;
6116 if (c_parser_next_token_is_keyword (parser, RID_IN))
6118 c_parser_consume_token (parser);
6119 is_foreach_statement = true;
6120 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6121 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6123 else
6124 check_for_loop_decls (for_loc, flag_isoc99);
6126 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
6128 /* __extension__ can start a declaration, but is also an
6129 unary operator that can start an expression. Consume all
6130 but the last of a possible series of __extension__ to
6131 determine which. */
6132 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD
6133 && (c_parser_peek_2nd_token (parser)->keyword
6134 == RID_EXTENSION))
6135 c_parser_consume_token (parser);
6136 if (c_token_starts_declaration (c_parser_peek_2nd_token (parser)))
6138 int ext;
6139 ext = disable_extension_diagnostics ();
6140 c_parser_consume_token (parser);
6141 c_parser_declaration_or_fndef (parser, true, true, true, true,
6142 true, &object_expression, vNULL);
6143 parser->objc_could_be_foreach_context = false;
6145 restore_extension_diagnostics (ext);
6146 if (c_parser_next_token_is_keyword (parser, RID_IN))
6148 c_parser_consume_token (parser);
6149 is_foreach_statement = true;
6150 if (check_for_loop_decls (for_loc, true) == NULL_TREE)
6151 c_parser_error (parser, "multiple iterating variables in fast enumeration");
6153 else
6154 check_for_loop_decls (for_loc, flag_isoc99);
6156 else
6157 goto init_expr;
6159 else
6161 init_expr:
6163 struct c_expr ce;
6164 tree init_expression;
6165 ce = c_parser_expression (parser);
6166 init_expression = ce.value;
6167 parser->objc_could_be_foreach_context = false;
6168 if (c_parser_next_token_is_keyword (parser, RID_IN))
6170 c_parser_consume_token (parser);
6171 is_foreach_statement = true;
6172 if (! lvalue_p (init_expression))
6173 c_parser_error (parser, "invalid iterating variable in fast enumeration");
6174 object_expression = c_fully_fold (init_expression, false, NULL);
6176 else
6178 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6179 init_expression = ce.value;
6180 c_finish_expr_stmt (loc, init_expression);
6181 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
6185 /* Parse the loop condition. In the case of a foreach
6186 statement, there is no loop condition. */
6187 gcc_assert (!parser->objc_could_be_foreach_context);
6188 if (!is_foreach_statement)
6190 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
6192 if (ivdep)
6194 c_parser_error (parser, "missing loop condition in loop with "
6195 "%<GCC ivdep%> pragma");
6196 cond = error_mark_node;
6198 else if (unroll)
6200 c_parser_error (parser, "missing loop condition in loop with "
6201 "%<GCC unroll%> pragma");
6202 cond = error_mark_node;
6204 else
6206 c_parser_consume_token (parser);
6207 cond = NULL_TREE;
6210 else
6212 cond = c_parser_condition (parser);
6213 c_parser_skip_until_found (parser, CPP_SEMICOLON,
6214 "expected %<;%>");
6216 if (ivdep && cond != error_mark_node)
6217 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6218 build_int_cst (integer_type_node,
6219 annot_expr_ivdep_kind),
6220 integer_zero_node);
6221 if (unroll && cond != error_mark_node)
6222 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
6223 build_int_cst (integer_type_node,
6224 annot_expr_unroll_kind),
6225 build_int_cst (integer_type_node, unroll));
6227 /* Parse the increment expression (the third expression in a
6228 for-statement). In the case of a foreach-statement, this is
6229 the expression that follows the 'in'. */
6230 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6232 if (is_foreach_statement)
6234 c_parser_error (parser, "missing collection in fast enumeration");
6235 collection_expression = error_mark_node;
6237 else
6238 incr = c_process_expr_stmt (loc, NULL_TREE);
6240 else
6242 if (is_foreach_statement)
6243 collection_expression = c_fully_fold (c_parser_expression (parser).value,
6244 false, NULL);
6245 else
6247 struct c_expr ce = c_parser_expression (parser);
6248 ce = convert_lvalue_to_rvalue (loc, ce, true, false);
6249 incr = c_process_expr_stmt (loc, ce.value);
6252 parens.skip_until_found_close (parser);
6254 save_break = c_break_label;
6255 c_break_label = NULL_TREE;
6256 save_cont = c_cont_label;
6257 c_cont_label = NULL_TREE;
6259 token_indent_info body_tinfo
6260 = get_token_indent_info (c_parser_peek_token (parser));
6262 location_t loc_after_labels;
6263 bool open_brace = c_parser_next_token_is (parser, CPP_OPEN_BRACE);
6264 body = c_parser_c99_block_statement (parser, if_p, &loc_after_labels);
6266 if (is_foreach_statement)
6267 objc_finish_foreach_loop (loc, object_expression, collection_expression, body, c_break_label, c_cont_label);
6268 else
6269 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true);
6270 add_stmt (c_end_compound_stmt (loc, block, flag_isoc99 || c_dialect_objc ()));
6271 c_parser_maybe_reclassify_token (parser);
6273 token_indent_info next_tinfo
6274 = get_token_indent_info (c_parser_peek_token (parser));
6275 warn_for_misleading_indentation (for_tinfo, body_tinfo, next_tinfo);
6277 if (next_tinfo.type != CPP_SEMICOLON && !open_brace)
6278 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
6279 for_tinfo.location, RID_FOR);
6281 c_break_label = save_break;
6282 c_cont_label = save_cont;
6285 /* Parse an asm statement, a GNU extension. This is a full-blown asm
6286 statement with inputs, outputs, clobbers, and volatile tag
6287 allowed.
6289 asm-statement:
6290 asm type-qualifier[opt] ( asm-argument ) ;
6291 asm type-qualifier[opt] goto ( asm-goto-argument ) ;
6293 asm-argument:
6294 asm-string-literal
6295 asm-string-literal : asm-operands[opt]
6296 asm-string-literal : asm-operands[opt] : asm-operands[opt]
6297 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers[opt]
6299 asm-goto-argument:
6300 asm-string-literal : : asm-operands[opt] : asm-clobbers[opt] \
6301 : asm-goto-operands
6303 Qualifiers other than volatile are accepted in the syntax but
6304 warned for. */
6306 static tree
6307 c_parser_asm_statement (c_parser *parser)
6309 tree quals, str, outputs, inputs, clobbers, labels, ret;
6310 bool simple, is_goto;
6311 location_t asm_loc = c_parser_peek_token (parser)->location;
6312 int section, nsections;
6314 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM));
6315 c_parser_consume_token (parser);
6316 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE))
6318 quals = c_parser_peek_token (parser)->value;
6319 c_parser_consume_token (parser);
6321 else if (c_parser_next_token_is_keyword (parser, RID_CONST)
6322 || c_parser_next_token_is_keyword (parser, RID_RESTRICT))
6324 warning_at (c_parser_peek_token (parser)->location,
6326 "%E qualifier ignored on asm",
6327 c_parser_peek_token (parser)->value);
6328 quals = NULL_TREE;
6329 c_parser_consume_token (parser);
6331 else
6332 quals = NULL_TREE;
6334 is_goto = false;
6335 if (c_parser_next_token_is_keyword (parser, RID_GOTO))
6337 c_parser_consume_token (parser);
6338 is_goto = true;
6341 /* ??? Follow the C++ parser rather than using the
6342 lex_untranslated_string kludge. */
6343 parser->lex_untranslated_string = true;
6344 ret = NULL;
6346 matching_parens parens;
6347 if (!parens.require_open (parser))
6348 goto error;
6350 str = c_parser_asm_string_literal (parser);
6351 if (str == NULL_TREE)
6352 goto error_close_paren;
6354 simple = true;
6355 outputs = NULL_TREE;
6356 inputs = NULL_TREE;
6357 clobbers = NULL_TREE;
6358 labels = NULL_TREE;
6360 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6361 goto done_asm;
6363 /* Parse each colon-delimited section of operands. */
6364 nsections = 3 + is_goto;
6365 for (section = 0; section < nsections; ++section)
6367 if (!c_parser_require (parser, CPP_COLON,
6368 is_goto
6369 ? G_("expected %<:%>")
6370 : G_("expected %<:%> or %<)%>"),
6371 UNKNOWN_LOCATION, is_goto))
6372 goto error_close_paren;
6374 /* Once past any colon, we're no longer a simple asm. */
6375 simple = false;
6377 if ((!c_parser_next_token_is (parser, CPP_COLON)
6378 && !c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
6379 || section == 3)
6380 switch (section)
6382 case 0:
6383 /* For asm goto, we don't allow output operands, but reserve
6384 the slot for a future extension that does allow them. */
6385 if (!is_goto)
6386 outputs = c_parser_asm_operands (parser);
6387 break;
6388 case 1:
6389 inputs = c_parser_asm_operands (parser);
6390 break;
6391 case 2:
6392 clobbers = c_parser_asm_clobbers (parser);
6393 break;
6394 case 3:
6395 labels = c_parser_asm_goto_operands (parser);
6396 break;
6397 default:
6398 gcc_unreachable ();
6401 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN) && !is_goto)
6402 goto done_asm;
6405 done_asm:
6406 if (!parens.require_close (parser))
6408 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6409 goto error;
6412 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
6413 c_parser_skip_to_end_of_block_or_statement (parser);
6415 ret = build_asm_stmt (quals, build_asm_expr (asm_loc, str, outputs, inputs,
6416 clobbers, labels, simple));
6418 error:
6419 parser->lex_untranslated_string = false;
6420 return ret;
6422 error_close_paren:
6423 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6424 goto error;
6427 /* Parse asm operands, a GNU extension.
6429 asm-operands:
6430 asm-operand
6431 asm-operands , asm-operand
6433 asm-operand:
6434 asm-string-literal ( expression )
6435 [ identifier ] asm-string-literal ( expression )
6438 static tree
6439 c_parser_asm_operands (c_parser *parser)
6441 tree list = NULL_TREE;
6442 while (true)
6444 tree name, str;
6445 struct c_expr expr;
6446 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
6448 c_parser_consume_token (parser);
6449 if (c_parser_next_token_is (parser, CPP_NAME))
6451 tree id = c_parser_peek_token (parser)->value;
6452 c_parser_consume_token (parser);
6453 name = build_string (IDENTIFIER_LENGTH (id),
6454 IDENTIFIER_POINTER (id));
6456 else
6458 c_parser_error (parser, "expected identifier");
6459 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL);
6460 return NULL_TREE;
6462 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
6463 "expected %<]%>");
6465 else
6466 name = NULL_TREE;
6467 str = c_parser_asm_string_literal (parser);
6468 if (str == NULL_TREE)
6469 return NULL_TREE;
6470 parser->lex_untranslated_string = false;
6471 matching_parens parens;
6472 if (!parens.require_open (parser))
6474 parser->lex_untranslated_string = true;
6475 return NULL_TREE;
6477 expr = c_parser_expression (parser);
6478 mark_exp_read (expr.value);
6479 parser->lex_untranslated_string = true;
6480 if (!parens.require_close (parser))
6482 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
6483 return NULL_TREE;
6485 list = chainon (list, build_tree_list (build_tree_list (name, str),
6486 expr.value));
6487 if (c_parser_next_token_is (parser, CPP_COMMA))
6488 c_parser_consume_token (parser);
6489 else
6490 break;
6492 return list;
6495 /* Parse asm clobbers, a GNU extension.
6497 asm-clobbers:
6498 asm-string-literal
6499 asm-clobbers , asm-string-literal
6502 static tree
6503 c_parser_asm_clobbers (c_parser *parser)
6505 tree list = NULL_TREE;
6506 while (true)
6508 tree str = c_parser_asm_string_literal (parser);
6509 if (str)
6510 list = tree_cons (NULL_TREE, str, list);
6511 else
6512 return NULL_TREE;
6513 if (c_parser_next_token_is (parser, CPP_COMMA))
6514 c_parser_consume_token (parser);
6515 else
6516 break;
6518 return list;
6521 /* Parse asm goto labels, a GNU extension.
6523 asm-goto-operands:
6524 identifier
6525 asm-goto-operands , identifier
6528 static tree
6529 c_parser_asm_goto_operands (c_parser *parser)
6531 tree list = NULL_TREE;
6532 while (true)
6534 tree name, label;
6536 if (c_parser_next_token_is (parser, CPP_NAME))
6538 c_token *tok = c_parser_peek_token (parser);
6539 name = tok->value;
6540 label = lookup_label_for_goto (tok->location, name);
6541 c_parser_consume_token (parser);
6542 TREE_USED (label) = 1;
6544 else
6546 c_parser_error (parser, "expected identifier");
6547 return NULL_TREE;
6550 name = build_string (IDENTIFIER_LENGTH (name),
6551 IDENTIFIER_POINTER (name));
6552 list = tree_cons (name, label, list);
6553 if (c_parser_next_token_is (parser, CPP_COMMA))
6554 c_parser_consume_token (parser);
6555 else
6556 return nreverse (list);
6560 /* Parse an expression other than a compound expression; that is, an
6561 assignment expression (C90 6.3.16, C99 6.5.16, C11 6.5.16). If
6562 AFTER is not NULL then it is an Objective-C message expression which
6563 is the primary-expression starting the expression as an initializer.
6565 assignment-expression:
6566 conditional-expression
6567 unary-expression assignment-operator assignment-expression
6569 assignment-operator: one of
6570 = *= /= %= += -= <<= >>= &= ^= |=
6572 In GNU C we accept any conditional expression on the LHS and
6573 diagnose the invalid lvalue rather than producing a syntax
6574 error. */
6576 static struct c_expr
6577 c_parser_expr_no_commas (c_parser *parser, struct c_expr *after,
6578 tree omp_atomic_lhs)
6580 struct c_expr lhs, rhs, ret;
6581 enum tree_code code;
6582 location_t op_location, exp_location;
6583 gcc_assert (!after || c_dialect_objc ());
6584 lhs = c_parser_conditional_expression (parser, after, omp_atomic_lhs);
6585 op_location = c_parser_peek_token (parser)->location;
6586 switch (c_parser_peek_token (parser)->type)
6588 case CPP_EQ:
6589 code = NOP_EXPR;
6590 break;
6591 case CPP_MULT_EQ:
6592 code = MULT_EXPR;
6593 break;
6594 case CPP_DIV_EQ:
6595 code = TRUNC_DIV_EXPR;
6596 break;
6597 case CPP_MOD_EQ:
6598 code = TRUNC_MOD_EXPR;
6599 break;
6600 case CPP_PLUS_EQ:
6601 code = PLUS_EXPR;
6602 break;
6603 case CPP_MINUS_EQ:
6604 code = MINUS_EXPR;
6605 break;
6606 case CPP_LSHIFT_EQ:
6607 code = LSHIFT_EXPR;
6608 break;
6609 case CPP_RSHIFT_EQ:
6610 code = RSHIFT_EXPR;
6611 break;
6612 case CPP_AND_EQ:
6613 code = BIT_AND_EXPR;
6614 break;
6615 case CPP_XOR_EQ:
6616 code = BIT_XOR_EXPR;
6617 break;
6618 case CPP_OR_EQ:
6619 code = BIT_IOR_EXPR;
6620 break;
6621 default:
6622 return lhs;
6624 c_parser_consume_token (parser);
6625 exp_location = c_parser_peek_token (parser)->location;
6626 rhs = c_parser_expr_no_commas (parser, NULL);
6627 rhs = convert_lvalue_to_rvalue (exp_location, rhs, true, true);
6629 ret.value = build_modify_expr (op_location, lhs.value, lhs.original_type,
6630 code, exp_location, rhs.value,
6631 rhs.original_type);
6632 set_c_expr_source_range (&ret, lhs.get_start (), rhs.get_finish ());
6633 if (code == NOP_EXPR)
6634 ret.original_code = MODIFY_EXPR;
6635 else
6637 TREE_NO_WARNING (ret.value) = 1;
6638 ret.original_code = ERROR_MARK;
6640 ret.original_type = NULL;
6641 return ret;
6644 /* Parse a conditional expression (C90 6.3.15, C99 6.5.15, C11 6.5.15). If
6645 AFTER is not NULL then it is an Objective-C message expression which is
6646 the primary-expression starting the expression as an initializer.
6648 conditional-expression:
6649 logical-OR-expression
6650 logical-OR-expression ? expression : conditional-expression
6652 GNU extensions:
6654 conditional-expression:
6655 logical-OR-expression ? : conditional-expression
6658 static struct c_expr
6659 c_parser_conditional_expression (c_parser *parser, struct c_expr *after,
6660 tree omp_atomic_lhs)
6662 struct c_expr cond, exp1, exp2, ret;
6663 location_t start, cond_loc, colon_loc;
6665 gcc_assert (!after || c_dialect_objc ());
6667 cond = c_parser_binary_expression (parser, after, omp_atomic_lhs);
6669 if (c_parser_next_token_is_not (parser, CPP_QUERY))
6670 return cond;
6671 if (cond.value != error_mark_node)
6672 start = cond.get_start ();
6673 else
6674 start = UNKNOWN_LOCATION;
6675 cond_loc = c_parser_peek_token (parser)->location;
6676 cond = convert_lvalue_to_rvalue (cond_loc, cond, true, true);
6677 c_parser_consume_token (parser);
6678 if (c_parser_next_token_is (parser, CPP_COLON))
6680 tree eptype = NULL_TREE;
6682 location_t middle_loc = c_parser_peek_token (parser)->location;
6683 pedwarn (middle_loc, OPT_Wpedantic,
6684 "ISO C forbids omitting the middle term of a ?: expression");
6685 if (TREE_CODE (cond.value) == EXCESS_PRECISION_EXPR)
6687 eptype = TREE_TYPE (cond.value);
6688 cond.value = TREE_OPERAND (cond.value, 0);
6690 tree e = cond.value;
6691 while (TREE_CODE (e) == COMPOUND_EXPR)
6692 e = TREE_OPERAND (e, 1);
6693 warn_for_omitted_condop (middle_loc, e);
6694 /* Make sure first operand is calculated only once. */
6695 exp1.value = save_expr (default_conversion (cond.value));
6696 if (eptype)
6697 exp1.value = build1 (EXCESS_PRECISION_EXPR, eptype, exp1.value);
6698 exp1.original_type = NULL;
6699 exp1.src_range = cond.src_range;
6700 cond.value = c_objc_common_truthvalue_conversion (cond_loc, exp1.value);
6701 c_inhibit_evaluation_warnings += cond.value == truthvalue_true_node;
6703 else
6705 cond.value
6706 = c_objc_common_truthvalue_conversion
6707 (cond_loc, default_conversion (cond.value));
6708 c_inhibit_evaluation_warnings += cond.value == truthvalue_false_node;
6709 exp1 = c_parser_expression_conv (parser);
6710 mark_exp_read (exp1.value);
6711 c_inhibit_evaluation_warnings +=
6712 ((cond.value == truthvalue_true_node)
6713 - (cond.value == truthvalue_false_node));
6716 colon_loc = c_parser_peek_token (parser)->location;
6717 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
6719 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6720 ret.set_error ();
6721 ret.original_code = ERROR_MARK;
6722 ret.original_type = NULL;
6723 return ret;
6726 location_t exp2_loc = c_parser_peek_token (parser)->location;
6727 exp2 = c_parser_conditional_expression (parser, NULL, NULL_TREE);
6728 exp2 = convert_lvalue_to_rvalue (exp2_loc, exp2, true, true);
6730 c_inhibit_evaluation_warnings -= cond.value == truthvalue_true_node;
6731 location_t loc1 = make_location (exp1.get_start (), exp1.src_range);
6732 location_t loc2 = make_location (exp2.get_start (), exp2.src_range);
6733 ret.value = build_conditional_expr (colon_loc, cond.value,
6734 cond.original_code == C_MAYBE_CONST_EXPR,
6735 exp1.value, exp1.original_type, loc1,
6736 exp2.value, exp2.original_type, loc2);
6737 ret.original_code = ERROR_MARK;
6738 if (exp1.value == error_mark_node || exp2.value == error_mark_node)
6739 ret.original_type = NULL;
6740 else
6742 tree t1, t2;
6744 /* If both sides are enum type, the default conversion will have
6745 made the type of the result be an integer type. We want to
6746 remember the enum types we started with. */
6747 t1 = exp1.original_type ? exp1.original_type : TREE_TYPE (exp1.value);
6748 t2 = exp2.original_type ? exp2.original_type : TREE_TYPE (exp2.value);
6749 ret.original_type = ((t1 != error_mark_node
6750 && t2 != error_mark_node
6751 && (TYPE_MAIN_VARIANT (t1)
6752 == TYPE_MAIN_VARIANT (t2)))
6753 ? t1
6754 : NULL);
6756 set_c_expr_source_range (&ret, start, exp2.get_finish ());
6757 return ret;
6760 /* Parse a binary expression; that is, a logical-OR-expression (C90
6761 6.3.5-6.3.14, C99 6.5.5-6.5.14, C11 6.5.5-6.5.14). If AFTER is not
6762 NULL then it is an Objective-C message expression which is the
6763 primary-expression starting the expression as an initializer.
6765 OMP_ATOMIC_LHS is NULL, unless parsing OpenMP #pragma omp atomic,
6766 when it should be the unfolded lhs. In a valid OpenMP source,
6767 one of the operands of the toplevel binary expression must be equal
6768 to it. In that case, just return a build2 created binary operation
6769 rather than result of parser_build_binary_op.
6771 multiplicative-expression:
6772 cast-expression
6773 multiplicative-expression * cast-expression
6774 multiplicative-expression / cast-expression
6775 multiplicative-expression % cast-expression
6777 additive-expression:
6778 multiplicative-expression
6779 additive-expression + multiplicative-expression
6780 additive-expression - multiplicative-expression
6782 shift-expression:
6783 additive-expression
6784 shift-expression << additive-expression
6785 shift-expression >> additive-expression
6787 relational-expression:
6788 shift-expression
6789 relational-expression < shift-expression
6790 relational-expression > shift-expression
6791 relational-expression <= shift-expression
6792 relational-expression >= shift-expression
6794 equality-expression:
6795 relational-expression
6796 equality-expression == relational-expression
6797 equality-expression != relational-expression
6799 AND-expression:
6800 equality-expression
6801 AND-expression & equality-expression
6803 exclusive-OR-expression:
6804 AND-expression
6805 exclusive-OR-expression ^ AND-expression
6807 inclusive-OR-expression:
6808 exclusive-OR-expression
6809 inclusive-OR-expression | exclusive-OR-expression
6811 logical-AND-expression:
6812 inclusive-OR-expression
6813 logical-AND-expression && inclusive-OR-expression
6815 logical-OR-expression:
6816 logical-AND-expression
6817 logical-OR-expression || logical-AND-expression
6820 static struct c_expr
6821 c_parser_binary_expression (c_parser *parser, struct c_expr *after,
6822 tree omp_atomic_lhs)
6824 /* A binary expression is parsed using operator-precedence parsing,
6825 with the operands being cast expressions. All the binary
6826 operators are left-associative. Thus a binary expression is of
6827 form:
6829 E0 op1 E1 op2 E2 ...
6831 which we represent on a stack. On the stack, the precedence
6832 levels are strictly increasing. When a new operator is
6833 encountered of higher precedence than that at the top of the
6834 stack, it is pushed; its LHS is the top expression, and its RHS
6835 is everything parsed until it is popped. When a new operator is
6836 encountered with precedence less than or equal to that at the top
6837 of the stack, triples E[i-1] op[i] E[i] are popped and replaced
6838 by the result of the operation until the operator at the top of
6839 the stack has lower precedence than the new operator or there is
6840 only one element on the stack; then the top expression is the LHS
6841 of the new operator. In the case of logical AND and OR
6842 expressions, we also need to adjust c_inhibit_evaluation_warnings
6843 as appropriate when the operators are pushed and popped. */
6845 struct {
6846 /* The expression at this stack level. */
6847 struct c_expr expr;
6848 /* The precedence of the operator on its left, PREC_NONE at the
6849 bottom of the stack. */
6850 enum c_parser_prec prec;
6851 /* The operation on its left. */
6852 enum tree_code op;
6853 /* The source location of this operation. */
6854 location_t loc;
6855 /* The sizeof argument if expr.original_code == SIZEOF_EXPR. */
6856 tree sizeof_arg;
6857 } stack[NUM_PRECS];
6858 int sp;
6859 /* Location of the binary operator. */
6860 location_t binary_loc = UNKNOWN_LOCATION; /* Quiet warning. */
6861 #define POP \
6862 do { \
6863 switch (stack[sp].op) \
6865 case TRUTH_ANDIF_EXPR: \
6866 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6867 == truthvalue_false_node); \
6868 break; \
6869 case TRUTH_ORIF_EXPR: \
6870 c_inhibit_evaluation_warnings -= (stack[sp - 1].expr.value \
6871 == truthvalue_true_node); \
6872 break; \
6873 case TRUNC_DIV_EXPR: \
6874 if (stack[sp - 1].expr.original_code == SIZEOF_EXPR \
6875 && stack[sp].expr.original_code == SIZEOF_EXPR) \
6877 tree type0 = stack[sp - 1].sizeof_arg; \
6878 tree type1 = stack[sp].sizeof_arg; \
6879 tree first_arg = type0; \
6880 if (!TYPE_P (type0)) \
6881 type0 = TREE_TYPE (type0); \
6882 if (!TYPE_P (type1)) \
6883 type1 = TREE_TYPE (type1); \
6884 if (POINTER_TYPE_P (type0) \
6885 && comptypes (TREE_TYPE (type0), type1) \
6886 && !(TREE_CODE (first_arg) == PARM_DECL \
6887 && C_ARRAY_PARAMETER (first_arg) \
6888 && warn_sizeof_array_argument)) \
6890 auto_diagnostic_group d; \
6891 if (warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div, \
6892 "division %<sizeof (%T) / sizeof (%T)%> " \
6893 "does not compute the number of array " \
6894 "elements", \
6895 type0, type1)) \
6896 if (DECL_P (first_arg)) \
6897 inform (DECL_SOURCE_LOCATION (first_arg), \
6898 "first %<sizeof%> operand was declared here"); \
6901 break; \
6902 default: \
6903 break; \
6905 stack[sp - 1].expr \
6906 = convert_lvalue_to_rvalue (stack[sp - 1].loc, \
6907 stack[sp - 1].expr, true, true); \
6908 stack[sp].expr \
6909 = convert_lvalue_to_rvalue (stack[sp].loc, \
6910 stack[sp].expr, true, true); \
6911 if (__builtin_expect (omp_atomic_lhs != NULL_TREE, 0) && sp == 1 \
6912 && c_parser_peek_token (parser)->type == CPP_SEMICOLON \
6913 && ((1 << stack[sp].prec) \
6914 & ((1 << PREC_BITOR) | (1 << PREC_BITXOR) | (1 << PREC_BITAND) \
6915 | (1 << PREC_SHIFT) | (1 << PREC_ADD) | (1 << PREC_MULT))) \
6916 && stack[sp].op != TRUNC_MOD_EXPR \
6917 && stack[0].expr.value != error_mark_node \
6918 && stack[1].expr.value != error_mark_node \
6919 && (c_tree_equal (stack[0].expr.value, omp_atomic_lhs) \
6920 || c_tree_equal (stack[1].expr.value, omp_atomic_lhs))) \
6921 stack[0].expr.value \
6922 = build2 (stack[1].op, TREE_TYPE (stack[0].expr.value), \
6923 stack[0].expr.value, stack[1].expr.value); \
6924 else \
6925 stack[sp - 1].expr = parser_build_binary_op (stack[sp].loc, \
6926 stack[sp].op, \
6927 stack[sp - 1].expr, \
6928 stack[sp].expr); \
6929 sp--; \
6930 } while (0)
6931 gcc_assert (!after || c_dialect_objc ());
6932 stack[0].loc = c_parser_peek_token (parser)->location;
6933 stack[0].expr = c_parser_cast_expression (parser, after);
6934 stack[0].prec = PREC_NONE;
6935 stack[0].sizeof_arg = c_last_sizeof_arg;
6936 sp = 0;
6937 while (true)
6939 enum c_parser_prec oprec;
6940 enum tree_code ocode;
6941 source_range src_range;
6942 if (parser->error)
6943 goto out;
6944 switch (c_parser_peek_token (parser)->type)
6946 case CPP_MULT:
6947 oprec = PREC_MULT;
6948 ocode = MULT_EXPR;
6949 break;
6950 case CPP_DIV:
6951 oprec = PREC_MULT;
6952 ocode = TRUNC_DIV_EXPR;
6953 break;
6954 case CPP_MOD:
6955 oprec = PREC_MULT;
6956 ocode = TRUNC_MOD_EXPR;
6957 break;
6958 case CPP_PLUS:
6959 oprec = PREC_ADD;
6960 ocode = PLUS_EXPR;
6961 break;
6962 case CPP_MINUS:
6963 oprec = PREC_ADD;
6964 ocode = MINUS_EXPR;
6965 break;
6966 case CPP_LSHIFT:
6967 oprec = PREC_SHIFT;
6968 ocode = LSHIFT_EXPR;
6969 break;
6970 case CPP_RSHIFT:
6971 oprec = PREC_SHIFT;
6972 ocode = RSHIFT_EXPR;
6973 break;
6974 case CPP_LESS:
6975 oprec = PREC_REL;
6976 ocode = LT_EXPR;
6977 break;
6978 case CPP_GREATER:
6979 oprec = PREC_REL;
6980 ocode = GT_EXPR;
6981 break;
6982 case CPP_LESS_EQ:
6983 oprec = PREC_REL;
6984 ocode = LE_EXPR;
6985 break;
6986 case CPP_GREATER_EQ:
6987 oprec = PREC_REL;
6988 ocode = GE_EXPR;
6989 break;
6990 case CPP_EQ_EQ:
6991 oprec = PREC_EQ;
6992 ocode = EQ_EXPR;
6993 break;
6994 case CPP_NOT_EQ:
6995 oprec = PREC_EQ;
6996 ocode = NE_EXPR;
6997 break;
6998 case CPP_AND:
6999 oprec = PREC_BITAND;
7000 ocode = BIT_AND_EXPR;
7001 break;
7002 case CPP_XOR:
7003 oprec = PREC_BITXOR;
7004 ocode = BIT_XOR_EXPR;
7005 break;
7006 case CPP_OR:
7007 oprec = PREC_BITOR;
7008 ocode = BIT_IOR_EXPR;
7009 break;
7010 case CPP_AND_AND:
7011 oprec = PREC_LOGAND;
7012 ocode = TRUTH_ANDIF_EXPR;
7013 break;
7014 case CPP_OR_OR:
7015 oprec = PREC_LOGOR;
7016 ocode = TRUTH_ORIF_EXPR;
7017 break;
7018 default:
7019 /* Not a binary operator, so end of the binary
7020 expression. */
7021 goto out;
7023 binary_loc = c_parser_peek_token (parser)->location;
7024 while (oprec <= stack[sp].prec)
7025 POP;
7026 c_parser_consume_token (parser);
7027 switch (ocode)
7029 case TRUTH_ANDIF_EXPR:
7030 src_range = stack[sp].expr.src_range;
7031 stack[sp].expr
7032 = convert_lvalue_to_rvalue (stack[sp].loc,
7033 stack[sp].expr, true, true);
7034 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7035 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7036 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7037 == truthvalue_false_node);
7038 set_c_expr_source_range (&stack[sp].expr, src_range);
7039 break;
7040 case TRUTH_ORIF_EXPR:
7041 src_range = stack[sp].expr.src_range;
7042 stack[sp].expr
7043 = convert_lvalue_to_rvalue (stack[sp].loc,
7044 stack[sp].expr, true, true);
7045 stack[sp].expr.value = c_objc_common_truthvalue_conversion
7046 (stack[sp].loc, default_conversion (stack[sp].expr.value));
7047 c_inhibit_evaluation_warnings += (stack[sp].expr.value
7048 == truthvalue_true_node);
7049 set_c_expr_source_range (&stack[sp].expr, src_range);
7050 break;
7051 default:
7052 break;
7054 sp++;
7055 stack[sp].loc = binary_loc;
7056 stack[sp].expr = c_parser_cast_expression (parser, NULL);
7057 stack[sp].prec = oprec;
7058 stack[sp].op = ocode;
7059 stack[sp].sizeof_arg = c_last_sizeof_arg;
7061 out:
7062 while (sp > 0)
7063 POP;
7064 return stack[0].expr;
7065 #undef POP
7068 /* Parse a cast expression (C90 6.3.4, C99 6.5.4, C11 6.5.4). If AFTER
7069 is not NULL then it is an Objective-C message expression which is the
7070 primary-expression starting the expression as an initializer.
7072 cast-expression:
7073 unary-expression
7074 ( type-name ) unary-expression
7077 static struct c_expr
7078 c_parser_cast_expression (c_parser *parser, struct c_expr *after)
7080 location_t cast_loc = c_parser_peek_token (parser)->location;
7081 gcc_assert (!after || c_dialect_objc ());
7082 if (after)
7083 return c_parser_postfix_expression_after_primary (parser,
7084 cast_loc, *after);
7085 /* If the expression begins with a parenthesized type name, it may
7086 be either a cast or a compound literal; we need to see whether
7087 the next character is '{' to tell the difference. If not, it is
7088 an unary expression. Full detection of unknown typenames here
7089 would require a 3-token lookahead. */
7090 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7091 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7093 struct c_type_name *type_name;
7094 struct c_expr ret;
7095 struct c_expr expr;
7096 matching_parens parens;
7097 parens.consume_open (parser);
7098 type_name = c_parser_type_name (parser, true);
7099 parens.skip_until_found_close (parser);
7100 if (type_name == NULL)
7102 ret.set_error ();
7103 ret.original_code = ERROR_MARK;
7104 ret.original_type = NULL;
7105 return ret;
7108 /* Save casted types in the function's used types hash table. */
7109 used_types_insert (type_name->specs->type);
7111 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7112 return c_parser_postfix_expression_after_paren_type (parser, type_name,
7113 cast_loc);
7114 if (type_name->specs->alignas_p)
7115 error_at (type_name->specs->locations[cdw_alignas],
7116 "alignment specified for type name in cast");
7118 location_t expr_loc = c_parser_peek_token (parser)->location;
7119 expr = c_parser_cast_expression (parser, NULL);
7120 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, true);
7122 ret.value = c_cast_expr (cast_loc, type_name, expr.value);
7123 if (ret.value && expr.value)
7124 set_c_expr_source_range (&ret, cast_loc, expr.get_finish ());
7125 ret.original_code = ERROR_MARK;
7126 ret.original_type = NULL;
7127 return ret;
7129 else
7130 return c_parser_unary_expression (parser);
7133 /* Parse an unary expression (C90 6.3.3, C99 6.5.3, C11 6.5.3).
7135 unary-expression:
7136 postfix-expression
7137 ++ unary-expression
7138 -- unary-expression
7139 unary-operator cast-expression
7140 sizeof unary-expression
7141 sizeof ( type-name )
7143 unary-operator: one of
7144 & * + - ~ !
7146 GNU extensions:
7148 unary-expression:
7149 __alignof__ unary-expression
7150 __alignof__ ( type-name )
7151 && identifier
7153 (C11 permits _Alignof with type names only.)
7155 unary-operator: one of
7156 __extension__ __real__ __imag__
7158 Transactional Memory:
7160 unary-expression:
7161 transaction-expression
7163 In addition, the GNU syntax treats ++ and -- as unary operators, so
7164 they may be applied to cast expressions with errors for non-lvalues
7165 given later. */
7167 static struct c_expr
7168 c_parser_unary_expression (c_parser *parser)
7170 int ext;
7171 struct c_expr ret, op;
7172 location_t op_loc = c_parser_peek_token (parser)->location;
7173 location_t exp_loc;
7174 location_t finish;
7175 ret.original_code = ERROR_MARK;
7176 ret.original_type = NULL;
7177 switch (c_parser_peek_token (parser)->type)
7179 case CPP_PLUS_PLUS:
7180 c_parser_consume_token (parser);
7181 exp_loc = c_parser_peek_token (parser)->location;
7182 op = c_parser_cast_expression (parser, NULL);
7184 op = default_function_array_read_conversion (exp_loc, op);
7185 return parser_build_unary_op (op_loc, PREINCREMENT_EXPR, op);
7186 case CPP_MINUS_MINUS:
7187 c_parser_consume_token (parser);
7188 exp_loc = c_parser_peek_token (parser)->location;
7189 op = c_parser_cast_expression (parser, NULL);
7191 op = default_function_array_read_conversion (exp_loc, op);
7192 return parser_build_unary_op (op_loc, PREDECREMENT_EXPR, op);
7193 case CPP_AND:
7194 c_parser_consume_token (parser);
7195 op = c_parser_cast_expression (parser, NULL);
7196 mark_exp_read (op.value);
7197 return parser_build_unary_op (op_loc, ADDR_EXPR, op);
7198 case CPP_MULT:
7200 c_parser_consume_token (parser);
7201 exp_loc = c_parser_peek_token (parser)->location;
7202 op = c_parser_cast_expression (parser, NULL);
7203 finish = op.get_finish ();
7204 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7205 location_t combined_loc = make_location (op_loc, op_loc, finish);
7206 ret.value = build_indirect_ref (combined_loc, op.value, RO_UNARY_STAR);
7207 ret.src_range.m_start = op_loc;
7208 ret.src_range.m_finish = finish;
7209 return ret;
7211 case CPP_PLUS:
7212 if (!c_dialect_objc () && !in_system_header_at (input_location))
7213 warning_at (op_loc,
7214 OPT_Wtraditional,
7215 "traditional C rejects the unary plus operator");
7216 c_parser_consume_token (parser);
7217 exp_loc = c_parser_peek_token (parser)->location;
7218 op = c_parser_cast_expression (parser, NULL);
7219 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7220 return parser_build_unary_op (op_loc, CONVERT_EXPR, op);
7221 case CPP_MINUS:
7222 c_parser_consume_token (parser);
7223 exp_loc = c_parser_peek_token (parser)->location;
7224 op = c_parser_cast_expression (parser, NULL);
7225 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7226 return parser_build_unary_op (op_loc, NEGATE_EXPR, op);
7227 case CPP_COMPL:
7228 c_parser_consume_token (parser);
7229 exp_loc = c_parser_peek_token (parser)->location;
7230 op = c_parser_cast_expression (parser, NULL);
7231 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7232 return parser_build_unary_op (op_loc, BIT_NOT_EXPR, op);
7233 case CPP_NOT:
7234 c_parser_consume_token (parser);
7235 exp_loc = c_parser_peek_token (parser)->location;
7236 op = c_parser_cast_expression (parser, NULL);
7237 op = convert_lvalue_to_rvalue (exp_loc, op, true, true);
7238 return parser_build_unary_op (op_loc, TRUTH_NOT_EXPR, op);
7239 case CPP_AND_AND:
7240 /* Refer to the address of a label as a pointer. */
7241 c_parser_consume_token (parser);
7242 if (c_parser_next_token_is (parser, CPP_NAME))
7244 ret.value = finish_label_address_expr
7245 (c_parser_peek_token (parser)->value, op_loc);
7246 set_c_expr_source_range (&ret, op_loc,
7247 c_parser_peek_token (parser)->get_finish ());
7248 c_parser_consume_token (parser);
7250 else
7252 c_parser_error (parser, "expected identifier");
7253 ret.set_error ();
7255 return ret;
7256 case CPP_KEYWORD:
7257 switch (c_parser_peek_token (parser)->keyword)
7259 case RID_SIZEOF:
7260 return c_parser_sizeof_expression (parser);
7261 case RID_ALIGNOF:
7262 return c_parser_alignof_expression (parser);
7263 case RID_EXTENSION:
7264 c_parser_consume_token (parser);
7265 ext = disable_extension_diagnostics ();
7266 ret = c_parser_cast_expression (parser, NULL);
7267 restore_extension_diagnostics (ext);
7268 return ret;
7269 case RID_REALPART:
7270 c_parser_consume_token (parser);
7271 exp_loc = c_parser_peek_token (parser)->location;
7272 op = c_parser_cast_expression (parser, NULL);
7273 op = default_function_array_conversion (exp_loc, op);
7274 return parser_build_unary_op (op_loc, REALPART_EXPR, op);
7275 case RID_IMAGPART:
7276 c_parser_consume_token (parser);
7277 exp_loc = c_parser_peek_token (parser)->location;
7278 op = c_parser_cast_expression (parser, NULL);
7279 op = default_function_array_conversion (exp_loc, op);
7280 return parser_build_unary_op (op_loc, IMAGPART_EXPR, op);
7281 case RID_TRANSACTION_ATOMIC:
7282 case RID_TRANSACTION_RELAXED:
7283 return c_parser_transaction_expression (parser,
7284 c_parser_peek_token (parser)->keyword);
7285 default:
7286 return c_parser_postfix_expression (parser);
7288 default:
7289 return c_parser_postfix_expression (parser);
7293 /* Parse a sizeof expression. */
7295 static struct c_expr
7296 c_parser_sizeof_expression (c_parser *parser)
7298 struct c_expr expr;
7299 struct c_expr result;
7300 location_t expr_loc;
7301 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF));
7303 location_t start;
7304 location_t finish = UNKNOWN_LOCATION;
7306 start = c_parser_peek_token (parser)->location;
7308 c_parser_consume_token (parser);
7309 c_inhibit_evaluation_warnings++;
7310 in_sizeof++;
7311 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7312 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7314 /* Either sizeof ( type-name ) or sizeof unary-expression
7315 starting with a compound literal. */
7316 struct c_type_name *type_name;
7317 matching_parens parens;
7318 parens.consume_open (parser);
7319 expr_loc = c_parser_peek_token (parser)->location;
7320 type_name = c_parser_type_name (parser, true);
7321 parens.skip_until_found_close (parser);
7322 finish = parser->tokens_buf[0].location;
7323 if (type_name == NULL)
7325 struct c_expr ret;
7326 c_inhibit_evaluation_warnings--;
7327 in_sizeof--;
7328 ret.set_error ();
7329 ret.original_code = ERROR_MARK;
7330 ret.original_type = NULL;
7331 return ret;
7333 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7335 expr = c_parser_postfix_expression_after_paren_type (parser,
7336 type_name,
7337 expr_loc);
7338 finish = expr.get_finish ();
7339 goto sizeof_expr;
7341 /* sizeof ( type-name ). */
7342 if (type_name->specs->alignas_p)
7343 error_at (type_name->specs->locations[cdw_alignas],
7344 "alignment specified for type name in %<sizeof%>");
7345 c_inhibit_evaluation_warnings--;
7346 in_sizeof--;
7347 result = c_expr_sizeof_type (expr_loc, type_name);
7349 else
7351 expr_loc = c_parser_peek_token (parser)->location;
7352 expr = c_parser_unary_expression (parser);
7353 finish = expr.get_finish ();
7354 sizeof_expr:
7355 c_inhibit_evaluation_warnings--;
7356 in_sizeof--;
7357 mark_exp_read (expr.value);
7358 if (TREE_CODE (expr.value) == COMPONENT_REF
7359 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1)))
7360 error_at (expr_loc, "%<sizeof%> applied to a bit-field");
7361 result = c_expr_sizeof_expr (expr_loc, expr);
7363 if (finish != UNKNOWN_LOCATION)
7364 set_c_expr_source_range (&result, start, finish);
7365 return result;
7368 /* Parse an alignof expression. */
7370 static struct c_expr
7371 c_parser_alignof_expression (c_parser *parser)
7373 struct c_expr expr;
7374 location_t start_loc = c_parser_peek_token (parser)->location;
7375 location_t end_loc;
7376 tree alignof_spelling = c_parser_peek_token (parser)->value;
7377 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF));
7378 bool is_c11_alignof = strcmp (IDENTIFIER_POINTER (alignof_spelling),
7379 "_Alignof") == 0;
7380 /* A diagnostic is not required for the use of this identifier in
7381 the implementation namespace; only diagnose it for the C11
7382 spelling because of existing code using the other spellings. */
7383 if (is_c11_alignof)
7385 if (flag_isoc99)
7386 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C99 does not support %qE",
7387 alignof_spelling);
7388 else
7389 pedwarn_c99 (start_loc, OPT_Wpedantic, "ISO C90 does not support %qE",
7390 alignof_spelling);
7392 c_parser_consume_token (parser);
7393 c_inhibit_evaluation_warnings++;
7394 in_alignof++;
7395 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)
7396 && c_token_starts_typename (c_parser_peek_2nd_token (parser)))
7398 /* Either __alignof__ ( type-name ) or __alignof__
7399 unary-expression starting with a compound literal. */
7400 location_t loc;
7401 struct c_type_name *type_name;
7402 struct c_expr ret;
7403 matching_parens parens;
7404 parens.consume_open (parser);
7405 loc = c_parser_peek_token (parser)->location;
7406 type_name = c_parser_type_name (parser, true);
7407 end_loc = c_parser_peek_token (parser)->location;
7408 parens.skip_until_found_close (parser);
7409 if (type_name == NULL)
7411 struct c_expr ret;
7412 c_inhibit_evaluation_warnings--;
7413 in_alignof--;
7414 ret.set_error ();
7415 ret.original_code = ERROR_MARK;
7416 ret.original_type = NULL;
7417 return ret;
7419 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
7421 expr = c_parser_postfix_expression_after_paren_type (parser,
7422 type_name,
7423 loc);
7424 goto alignof_expr;
7426 /* alignof ( type-name ). */
7427 if (type_name->specs->alignas_p)
7428 error_at (type_name->specs->locations[cdw_alignas],
7429 "alignment specified for type name in %qE",
7430 alignof_spelling);
7431 c_inhibit_evaluation_warnings--;
7432 in_alignof--;
7433 ret.value = c_sizeof_or_alignof_type (loc, groktypename (type_name,
7434 NULL, NULL),
7435 false, is_c11_alignof, 1);
7436 ret.original_code = ERROR_MARK;
7437 ret.original_type = NULL;
7438 set_c_expr_source_range (&ret, start_loc, end_loc);
7439 return ret;
7441 else
7443 struct c_expr ret;
7444 expr = c_parser_unary_expression (parser);
7445 end_loc = expr.src_range.m_finish;
7446 alignof_expr:
7447 mark_exp_read (expr.value);
7448 c_inhibit_evaluation_warnings--;
7449 in_alignof--;
7450 if (is_c11_alignof)
7451 pedwarn (start_loc,
7452 OPT_Wpedantic, "ISO C does not allow %<%E (expression)%>",
7453 alignof_spelling);
7454 ret.value = c_alignof_expr (start_loc, expr.value);
7455 ret.original_code = ERROR_MARK;
7456 ret.original_type = NULL;
7457 set_c_expr_source_range (&ret, start_loc, end_loc);
7458 return ret;
7462 /* Helper function to read arguments of builtins which are interfaces
7463 for the middle-end nodes like COMPLEX_EXPR, VEC_PERM_EXPR and
7464 others. The name of the builtin is passed using BNAME parameter.
7465 Function returns true if there were no errors while parsing and
7466 stores the arguments in CEXPR_LIST. If it returns true,
7467 *OUT_CLOSE_PAREN_LOC is written to with the location of the closing
7468 parenthesis. */
7469 static bool
7470 c_parser_get_builtin_args (c_parser *parser, const char *bname,
7471 vec<c_expr_t, va_gc> **ret_cexpr_list,
7472 bool choose_expr_p,
7473 location_t *out_close_paren_loc)
7475 location_t loc = c_parser_peek_token (parser)->location;
7476 vec<c_expr_t, va_gc> *cexpr_list;
7477 c_expr_t expr;
7478 bool saved_force_folding_builtin_constant_p;
7480 *ret_cexpr_list = NULL;
7481 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN))
7483 error_at (loc, "cannot take address of %qs", bname);
7484 return false;
7487 c_parser_consume_token (parser);
7489 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
7491 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7492 c_parser_consume_token (parser);
7493 return true;
7496 saved_force_folding_builtin_constant_p
7497 = force_folding_builtin_constant_p;
7498 force_folding_builtin_constant_p |= choose_expr_p;
7499 expr = c_parser_expr_no_commas (parser, NULL);
7500 force_folding_builtin_constant_p
7501 = saved_force_folding_builtin_constant_p;
7502 vec_alloc (cexpr_list, 1);
7503 vec_safe_push (cexpr_list, expr);
7504 while (c_parser_next_token_is (parser, CPP_COMMA))
7506 c_parser_consume_token (parser);
7507 expr = c_parser_expr_no_commas (parser, NULL);
7508 vec_safe_push (cexpr_list, expr);
7511 *out_close_paren_loc = c_parser_peek_token (parser)->location;
7512 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
7513 return false;
7515 *ret_cexpr_list = cexpr_list;
7516 return true;
7519 /* This represents a single generic-association. */
7521 struct c_generic_association
7523 /* The location of the starting token of the type. */
7524 location_t type_location;
7525 /* The association's type, or NULL_TREE for 'default'. */
7526 tree type;
7527 /* The association's expression. */
7528 struct c_expr expression;
7531 /* Parse a generic-selection. (C11 6.5.1.1).
7533 generic-selection:
7534 _Generic ( assignment-expression , generic-assoc-list )
7536 generic-assoc-list:
7537 generic-association
7538 generic-assoc-list , generic-association
7540 generic-association:
7541 type-name : assignment-expression
7542 default : assignment-expression
7545 static struct c_expr
7546 c_parser_generic_selection (c_parser *parser)
7548 struct c_expr selector, error_expr;
7549 tree selector_type;
7550 struct c_generic_association matched_assoc;
7551 bool match_found = false;
7552 location_t generic_loc, selector_loc;
7554 error_expr.original_code = ERROR_MARK;
7555 error_expr.original_type = NULL;
7556 error_expr.set_error ();
7557 matched_assoc.type_location = UNKNOWN_LOCATION;
7558 matched_assoc.type = NULL_TREE;
7559 matched_assoc.expression = error_expr;
7561 gcc_assert (c_parser_next_token_is_keyword (parser, RID_GENERIC));
7562 generic_loc = c_parser_peek_token (parser)->location;
7563 c_parser_consume_token (parser);
7564 if (flag_isoc99)
7565 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7566 "ISO C99 does not support %<_Generic%>");
7567 else
7568 pedwarn_c99 (generic_loc, OPT_Wpedantic,
7569 "ISO C90 does not support %<_Generic%>");
7571 matching_parens parens;
7572 if (!parens.require_open (parser))
7573 return error_expr;
7575 c_inhibit_evaluation_warnings++;
7576 selector_loc = c_parser_peek_token (parser)->location;
7577 selector = c_parser_expr_no_commas (parser, NULL);
7578 selector = default_function_array_conversion (selector_loc, selector);
7579 c_inhibit_evaluation_warnings--;
7581 if (selector.value == error_mark_node)
7583 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7584 return selector;
7586 selector_type = TREE_TYPE (selector.value);
7587 /* In ISO C terms, rvalues (including the controlling expression of
7588 _Generic) do not have qualified types. */
7589 if (TREE_CODE (selector_type) != ARRAY_TYPE)
7590 selector_type = TYPE_MAIN_VARIANT (selector_type);
7591 /* In ISO C terms, _Noreturn is not part of the type of expressions
7592 such as &abort, but in GCC it is represented internally as a type
7593 qualifier. */
7594 if (FUNCTION_POINTER_TYPE_P (selector_type)
7595 && TYPE_QUALS (TREE_TYPE (selector_type)) != TYPE_UNQUALIFIED)
7596 selector_type
7597 = build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (selector_type)));
7599 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
7601 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7602 return error_expr;
7605 auto_vec<c_generic_association> associations;
7606 while (1)
7608 struct c_generic_association assoc, *iter;
7609 unsigned int ix;
7610 c_token *token = c_parser_peek_token (parser);
7612 assoc.type_location = token->location;
7613 if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
7615 c_parser_consume_token (parser);
7616 assoc.type = NULL_TREE;
7618 else
7620 struct c_type_name *type_name;
7622 type_name = c_parser_type_name (parser);
7623 if (type_name == NULL)
7625 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7626 return error_expr;
7628 assoc.type = groktypename (type_name, NULL, NULL);
7629 if (assoc.type == error_mark_node)
7631 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7632 return error_expr;
7635 if (TREE_CODE (assoc.type) == FUNCTION_TYPE)
7636 error_at (assoc.type_location,
7637 "%<_Generic%> association has function type");
7638 else if (!COMPLETE_TYPE_P (assoc.type))
7639 error_at (assoc.type_location,
7640 "%<_Generic%> association has incomplete type");
7642 if (variably_modified_type_p (assoc.type, NULL_TREE))
7643 error_at (assoc.type_location,
7644 "%<_Generic%> association has "
7645 "variable length type");
7648 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
7650 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7651 return error_expr;
7654 assoc.expression = c_parser_expr_no_commas (parser, NULL);
7655 if (assoc.expression.value == error_mark_node)
7657 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7658 return error_expr;
7661 for (ix = 0; associations.iterate (ix, &iter); ++ix)
7663 if (assoc.type == NULL_TREE)
7665 if (iter->type == NULL_TREE)
7667 error_at (assoc.type_location,
7668 "duplicate %<default%> case in %<_Generic%>");
7669 inform (iter->type_location, "original %<default%> is here");
7672 else if (iter->type != NULL_TREE)
7674 if (comptypes (assoc.type, iter->type))
7676 error_at (assoc.type_location,
7677 "%<_Generic%> specifies two compatible types");
7678 inform (iter->type_location, "compatible type is here");
7683 if (assoc.type == NULL_TREE)
7685 if (!match_found)
7687 matched_assoc = assoc;
7688 match_found = true;
7691 else if (comptypes (assoc.type, selector_type))
7693 if (!match_found || matched_assoc.type == NULL_TREE)
7695 matched_assoc = assoc;
7696 match_found = true;
7698 else
7700 error_at (assoc.type_location,
7701 "%<_Generic%> selector matches multiple associations");
7702 inform (matched_assoc.type_location,
7703 "other match is here");
7707 associations.safe_push (assoc);
7709 if (c_parser_peek_token (parser)->type != CPP_COMMA)
7710 break;
7711 c_parser_consume_token (parser);
7714 if (!parens.require_close (parser))
7716 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7717 return error_expr;
7720 if (!match_found)
7722 error_at (selector_loc, "%<_Generic%> selector of type %qT is not "
7723 "compatible with any association",
7724 selector_type);
7725 return error_expr;
7728 return matched_assoc.expression;
7731 /* Check the validity of a function pointer argument *EXPR (argument
7732 position POS) to __builtin_tgmath. Return the number of function
7733 arguments if possibly valid; return 0 having reported an error if
7734 not valid. */
7736 static unsigned int
7737 check_tgmath_function (c_expr *expr, unsigned int pos)
7739 tree type = TREE_TYPE (expr->value);
7740 if (!FUNCTION_POINTER_TYPE_P (type))
7742 error_at (expr->get_location (),
7743 "argument %u of %<__builtin_tgmath%> is not a function pointer",
7744 pos);
7745 return 0;
7747 type = TREE_TYPE (type);
7748 if (!prototype_p (type))
7750 error_at (expr->get_location (),
7751 "argument %u of %<__builtin_tgmath%> is unprototyped", pos);
7752 return 0;
7754 if (stdarg_p (type))
7756 error_at (expr->get_location (),
7757 "argument %u of %<__builtin_tgmath%> has variable arguments",
7758 pos);
7759 return 0;
7761 unsigned int nargs = 0;
7762 function_args_iterator iter;
7763 tree t;
7764 FOREACH_FUNCTION_ARGS (type, t, iter)
7766 if (t == void_type_node)
7767 break;
7768 nargs++;
7770 if (nargs == 0)
7772 error_at (expr->get_location (),
7773 "argument %u of %<__builtin_tgmath%> has no arguments", pos);
7774 return 0;
7776 return nargs;
7779 /* Ways in which a parameter or return value of a type-generic macro
7780 may vary between the different functions the macro may call. */
7781 enum tgmath_parm_kind
7783 tgmath_fixed, tgmath_real, tgmath_complex
7786 /* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2,
7787 C11 6.5.1-6.5.2). Compound literals aren't handled here; callers have to
7788 call c_parser_postfix_expression_after_paren_type on encountering them.
7790 postfix-expression:
7791 primary-expression
7792 postfix-expression [ expression ]
7793 postfix-expression ( argument-expression-list[opt] )
7794 postfix-expression . identifier
7795 postfix-expression -> identifier
7796 postfix-expression ++
7797 postfix-expression --
7798 ( type-name ) { initializer-list }
7799 ( type-name ) { initializer-list , }
7801 argument-expression-list:
7802 argument-expression
7803 argument-expression-list , argument-expression
7805 primary-expression:
7806 identifier
7807 constant
7808 string-literal
7809 ( expression )
7810 generic-selection
7812 GNU extensions:
7814 primary-expression:
7815 __func__
7816 (treated as a keyword in GNU C)
7817 __FUNCTION__
7818 __PRETTY_FUNCTION__
7819 ( compound-statement )
7820 __builtin_va_arg ( assignment-expression , type-name )
7821 __builtin_offsetof ( type-name , offsetof-member-designator )
7822 __builtin_choose_expr ( assignment-expression ,
7823 assignment-expression ,
7824 assignment-expression )
7825 __builtin_types_compatible_p ( type-name , type-name )
7826 __builtin_tgmath ( expr-list )
7827 __builtin_complex ( assignment-expression , assignment-expression )
7828 __builtin_shuffle ( assignment-expression , assignment-expression )
7829 __builtin_shuffle ( assignment-expression ,
7830 assignment-expression ,
7831 assignment-expression, )
7833 offsetof-member-designator:
7834 identifier
7835 offsetof-member-designator . identifier
7836 offsetof-member-designator [ expression ]
7838 Objective-C:
7840 primary-expression:
7841 [ objc-receiver objc-message-args ]
7842 @selector ( objc-selector-arg )
7843 @protocol ( identifier )
7844 @encode ( type-name )
7845 objc-string-literal
7846 Classname . identifier
7849 static struct c_expr
7850 c_parser_postfix_expression (c_parser *parser)
7852 struct c_expr expr, e1;
7853 struct c_type_name *t1, *t2;
7854 location_t loc = c_parser_peek_token (parser)->location;
7855 source_range tok_range = c_parser_peek_token (parser)->get_range ();
7856 expr.original_code = ERROR_MARK;
7857 expr.original_type = NULL;
7858 switch (c_parser_peek_token (parser)->type)
7860 case CPP_NUMBER:
7861 expr.value = c_parser_peek_token (parser)->value;
7862 set_c_expr_source_range (&expr, tok_range);
7863 loc = c_parser_peek_token (parser)->location;
7864 c_parser_consume_token (parser);
7865 if (TREE_CODE (expr.value) == FIXED_CST
7866 && !targetm.fixed_point_supported_p ())
7868 error_at (loc, "fixed-point types not supported for this target");
7869 expr.set_error ();
7871 break;
7872 case CPP_CHAR:
7873 case CPP_CHAR16:
7874 case CPP_CHAR32:
7875 case CPP_WCHAR:
7876 expr.value = c_parser_peek_token (parser)->value;
7877 /* For the purpose of warning when a pointer is compared with
7878 a zero character constant. */
7879 expr.original_type = char_type_node;
7880 set_c_expr_source_range (&expr, tok_range);
7881 c_parser_consume_token (parser);
7882 break;
7883 case CPP_STRING:
7884 case CPP_STRING16:
7885 case CPP_STRING32:
7886 case CPP_WSTRING:
7887 case CPP_UTF8STRING:
7888 expr.value = c_parser_peek_token (parser)->value;
7889 set_c_expr_source_range (&expr, tok_range);
7890 expr.original_code = STRING_CST;
7891 c_parser_consume_token (parser);
7892 break;
7893 case CPP_OBJC_STRING:
7894 gcc_assert (c_dialect_objc ());
7895 expr.value
7896 = objc_build_string_object (c_parser_peek_token (parser)->value);
7897 set_c_expr_source_range (&expr, tok_range);
7898 c_parser_consume_token (parser);
7899 break;
7900 case CPP_NAME:
7901 switch (c_parser_peek_token (parser)->id_kind)
7903 case C_ID_ID:
7905 tree id = c_parser_peek_token (parser)->value;
7906 c_parser_consume_token (parser);
7907 expr.value = build_external_ref (loc, id,
7908 (c_parser_peek_token (parser)->type
7909 == CPP_OPEN_PAREN),
7910 &expr.original_type);
7911 set_c_expr_source_range (&expr, tok_range);
7912 break;
7914 case C_ID_CLASSNAME:
7916 /* Here we parse the Objective-C 2.0 Class.name dot
7917 syntax. */
7918 tree class_name = c_parser_peek_token (parser)->value;
7919 tree component;
7920 c_parser_consume_token (parser);
7921 gcc_assert (c_dialect_objc ());
7922 if (!c_parser_require (parser, CPP_DOT, "expected %<.%>"))
7924 expr.set_error ();
7925 break;
7927 if (c_parser_next_token_is_not (parser, CPP_NAME))
7929 c_parser_error (parser, "expected identifier");
7930 expr.set_error ();
7931 break;
7933 c_token *component_tok = c_parser_peek_token (parser);
7934 component = component_tok->value;
7935 location_t end_loc = component_tok->get_finish ();
7936 c_parser_consume_token (parser);
7937 expr.value = objc_build_class_component_ref (class_name,
7938 component);
7939 set_c_expr_source_range (&expr, loc, end_loc);
7940 break;
7942 default:
7943 c_parser_error (parser, "expected expression");
7944 expr.set_error ();
7945 break;
7947 break;
7948 case CPP_OPEN_PAREN:
7949 /* A parenthesized expression, statement expression or compound
7950 literal. */
7951 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE)
7953 /* A statement expression. */
7954 tree stmt;
7955 location_t brace_loc;
7956 c_parser_consume_token (parser);
7957 brace_loc = c_parser_peek_token (parser)->location;
7958 c_parser_consume_token (parser);
7959 /* If we've not yet started the current function's statement list,
7960 or we're in the parameter scope of an old-style function
7961 declaration, statement expressions are not allowed. */
7962 if (!building_stmt_list_p () || old_style_parameter_scope ())
7964 error_at (loc, "braced-group within expression allowed "
7965 "only inside a function");
7966 parser->error = true;
7967 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL);
7968 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
7969 expr.set_error ();
7970 break;
7972 stmt = c_begin_stmt_expr ();
7973 c_parser_compound_statement_nostart (parser);
7974 location_t close_loc = c_parser_peek_token (parser)->location;
7975 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7976 "expected %<)%>");
7977 pedwarn (loc, OPT_Wpedantic,
7978 "ISO C forbids braced-groups within expressions");
7979 expr.value = c_finish_stmt_expr (brace_loc, stmt);
7980 set_c_expr_source_range (&expr, loc, close_loc);
7981 mark_exp_read (expr.value);
7983 else
7985 /* A parenthesized expression. */
7986 location_t loc_open_paren = c_parser_peek_token (parser)->location;
7987 c_parser_consume_token (parser);
7988 expr = c_parser_expression (parser);
7989 if (TREE_CODE (expr.value) == MODIFY_EXPR)
7990 TREE_NO_WARNING (expr.value) = 1;
7991 if (expr.original_code != C_MAYBE_CONST_EXPR
7992 && expr.original_code != SIZEOF_EXPR)
7993 expr.original_code = ERROR_MARK;
7994 /* Don't change EXPR.ORIGINAL_TYPE. */
7995 location_t loc_close_paren = c_parser_peek_token (parser)->location;
7996 set_c_expr_source_range (&expr, loc_open_paren, loc_close_paren);
7997 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
7998 "expected %<)%>", loc_open_paren);
8000 break;
8001 case CPP_KEYWORD:
8002 switch (c_parser_peek_token (parser)->keyword)
8004 case RID_FUNCTION_NAME:
8005 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8006 "%<__FUNCTION__%> predefined identifier");
8007 expr.value = fname_decl (loc,
8008 c_parser_peek_token (parser)->keyword,
8009 c_parser_peek_token (parser)->value);
8010 set_c_expr_source_range (&expr, loc, loc);
8011 c_parser_consume_token (parser);
8012 break;
8013 case RID_PRETTY_FUNCTION_NAME:
8014 pedwarn (loc, OPT_Wpedantic, "ISO C does not support "
8015 "%<__PRETTY_FUNCTION__%> predefined identifier");
8016 expr.value = fname_decl (loc,
8017 c_parser_peek_token (parser)->keyword,
8018 c_parser_peek_token (parser)->value);
8019 set_c_expr_source_range (&expr, loc, loc);
8020 c_parser_consume_token (parser);
8021 break;
8022 case RID_C99_FUNCTION_NAME:
8023 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support "
8024 "%<__func__%> predefined identifier");
8025 expr.value = fname_decl (loc,
8026 c_parser_peek_token (parser)->keyword,
8027 c_parser_peek_token (parser)->value);
8028 set_c_expr_source_range (&expr, loc, loc);
8029 c_parser_consume_token (parser);
8030 break;
8031 case RID_VA_ARG:
8033 location_t start_loc = loc;
8034 c_parser_consume_token (parser);
8035 matching_parens parens;
8036 if (!parens.require_open (parser))
8038 expr.set_error ();
8039 break;
8041 e1 = c_parser_expr_no_commas (parser, NULL);
8042 mark_exp_read (e1.value);
8043 e1.value = c_fully_fold (e1.value, false, NULL);
8044 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8046 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8047 expr.set_error ();
8048 break;
8050 loc = c_parser_peek_token (parser)->location;
8051 t1 = c_parser_type_name (parser);
8052 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8053 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8054 "expected %<)%>");
8055 if (t1 == NULL)
8057 expr.set_error ();
8059 else
8061 tree type_expr = NULL_TREE;
8062 expr.value = c_build_va_arg (start_loc, e1.value, loc,
8063 groktypename (t1, &type_expr, NULL));
8064 if (type_expr)
8066 expr.value = build2 (C_MAYBE_CONST_EXPR,
8067 TREE_TYPE (expr.value), type_expr,
8068 expr.value);
8069 C_MAYBE_CONST_EXPR_NON_CONST (expr.value) = true;
8071 set_c_expr_source_range (&expr, start_loc, end_loc);
8074 break;
8075 case RID_OFFSETOF:
8077 c_parser_consume_token (parser);
8078 matching_parens parens;
8079 if (!parens.require_open (parser))
8081 expr.set_error ();
8082 break;
8084 t1 = c_parser_type_name (parser);
8085 if (t1 == NULL)
8086 parser->error = true;
8087 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8088 gcc_assert (parser->error);
8089 if (parser->error)
8091 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8092 expr.set_error ();
8093 break;
8095 tree type = groktypename (t1, NULL, NULL);
8096 tree offsetof_ref;
8097 if (type == error_mark_node)
8098 offsetof_ref = error_mark_node;
8099 else
8101 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node);
8102 SET_EXPR_LOCATION (offsetof_ref, loc);
8104 /* Parse the second argument to __builtin_offsetof. We
8105 must have one identifier, and beyond that we want to
8106 accept sub structure and sub array references. */
8107 if (c_parser_next_token_is (parser, CPP_NAME))
8109 c_token *comp_tok = c_parser_peek_token (parser);
8110 offsetof_ref = build_component_ref
8111 (loc, offsetof_ref, comp_tok->value, comp_tok->location);
8112 c_parser_consume_token (parser);
8113 while (c_parser_next_token_is (parser, CPP_DOT)
8114 || c_parser_next_token_is (parser,
8115 CPP_OPEN_SQUARE)
8116 || c_parser_next_token_is (parser,
8117 CPP_DEREF))
8119 if (c_parser_next_token_is (parser, CPP_DEREF))
8121 loc = c_parser_peek_token (parser)->location;
8122 offsetof_ref = build_array_ref (loc,
8123 offsetof_ref,
8124 integer_zero_node);
8125 goto do_dot;
8127 else if (c_parser_next_token_is (parser, CPP_DOT))
8129 do_dot:
8130 c_parser_consume_token (parser);
8131 if (c_parser_next_token_is_not (parser,
8132 CPP_NAME))
8134 c_parser_error (parser, "expected identifier");
8135 break;
8137 c_token *comp_tok = c_parser_peek_token (parser);
8138 offsetof_ref = build_component_ref
8139 (loc, offsetof_ref, comp_tok->value,
8140 comp_tok->location);
8141 c_parser_consume_token (parser);
8143 else
8145 struct c_expr ce;
8146 tree idx;
8147 loc = c_parser_peek_token (parser)->location;
8148 c_parser_consume_token (parser);
8149 ce = c_parser_expression (parser);
8150 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
8151 idx = ce.value;
8152 idx = c_fully_fold (idx, false, NULL);
8153 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
8154 "expected %<]%>");
8155 offsetof_ref = build_array_ref (loc, offsetof_ref, idx);
8159 else
8160 c_parser_error (parser, "expected identifier");
8161 location_t end_loc = c_parser_peek_token (parser)->get_finish ();
8162 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
8163 "expected %<)%>");
8164 expr.value = fold_offsetof (offsetof_ref);
8165 set_c_expr_source_range (&expr, loc, end_loc);
8167 break;
8168 case RID_CHOOSE_EXPR:
8170 vec<c_expr_t, va_gc> *cexpr_list;
8171 c_expr_t *e1_p, *e2_p, *e3_p;
8172 tree c;
8173 location_t close_paren_loc;
8175 c_parser_consume_token (parser);
8176 if (!c_parser_get_builtin_args (parser,
8177 "__builtin_choose_expr",
8178 &cexpr_list, true,
8179 &close_paren_loc))
8181 expr.set_error ();
8182 break;
8185 if (vec_safe_length (cexpr_list) != 3)
8187 error_at (loc, "wrong number of arguments to "
8188 "%<__builtin_choose_expr%>");
8189 expr.set_error ();
8190 break;
8193 e1_p = &(*cexpr_list)[0];
8194 e2_p = &(*cexpr_list)[1];
8195 e3_p = &(*cexpr_list)[2];
8197 c = e1_p->value;
8198 mark_exp_read (e2_p->value);
8199 mark_exp_read (e3_p->value);
8200 if (TREE_CODE (c) != INTEGER_CST
8201 || !INTEGRAL_TYPE_P (TREE_TYPE (c)))
8202 error_at (loc,
8203 "first argument to %<__builtin_choose_expr%> not"
8204 " a constant");
8205 constant_expression_warning (c);
8206 expr = integer_zerop (c) ? *e3_p : *e2_p;
8207 set_c_expr_source_range (&expr, loc, close_paren_loc);
8208 break;
8210 case RID_TYPES_COMPATIBLE_P:
8212 c_parser_consume_token (parser);
8213 matching_parens parens;
8214 if (!parens.require_open (parser))
8216 expr.set_error ();
8217 break;
8219 t1 = c_parser_type_name (parser);
8220 if (t1 == NULL)
8222 expr.set_error ();
8223 break;
8225 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
8227 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8228 expr.set_error ();
8229 break;
8231 t2 = c_parser_type_name (parser);
8232 if (t2 == NULL)
8234 expr.set_error ();
8235 break;
8237 location_t close_paren_loc = c_parser_peek_token (parser)->location;
8238 parens.skip_until_found_close (parser);
8239 tree e1, e2;
8240 e1 = groktypename (t1, NULL, NULL);
8241 e2 = groktypename (t2, NULL, NULL);
8242 if (e1 == error_mark_node || e2 == error_mark_node)
8244 expr.set_error ();
8245 break;
8248 e1 = TYPE_MAIN_VARIANT (e1);
8249 e2 = TYPE_MAIN_VARIANT (e2);
8251 expr.value
8252 = comptypes (e1, e2) ? integer_one_node : integer_zero_node;
8253 set_c_expr_source_range (&expr, loc, close_paren_loc);
8255 break;
8256 case RID_BUILTIN_TGMATH:
8258 vec<c_expr_t, va_gc> *cexpr_list;
8259 location_t close_paren_loc;
8261 c_parser_consume_token (parser);
8262 if (!c_parser_get_builtin_args (parser,
8263 "__builtin_tgmath",
8264 &cexpr_list, false,
8265 &close_paren_loc))
8267 expr.set_error ();
8268 break;
8271 if (vec_safe_length (cexpr_list) < 3)
8273 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8274 expr.set_error ();
8275 break;
8278 unsigned int i;
8279 c_expr_t *p;
8280 FOR_EACH_VEC_ELT (*cexpr_list, i, p)
8281 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8282 unsigned int nargs = check_tgmath_function (&(*cexpr_list)[0], 1);
8283 if (nargs == 0)
8285 expr.set_error ();
8286 break;
8288 if (vec_safe_length (cexpr_list) < nargs)
8290 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8291 expr.set_error ();
8292 break;
8294 unsigned int num_functions = vec_safe_length (cexpr_list) - nargs;
8295 if (num_functions < 2)
8297 error_at (loc, "too few arguments to %<__builtin_tgmath%>");
8298 expr.set_error ();
8299 break;
8302 /* The first NUM_FUNCTIONS expressions are the function
8303 pointers. The remaining NARGS expressions are the
8304 arguments that are to be passed to one of those
8305 functions, chosen following <tgmath.h> rules. */
8306 for (unsigned int j = 1; j < num_functions; j++)
8308 unsigned int this_nargs
8309 = check_tgmath_function (&(*cexpr_list)[j], j + 1);
8310 if (this_nargs == 0)
8312 expr.set_error ();
8313 goto out;
8315 if (this_nargs != nargs)
8317 error_at ((*cexpr_list)[j].get_location (),
8318 "argument %u of %<__builtin_tgmath%> has "
8319 "wrong number of arguments", j + 1);
8320 expr.set_error ();
8321 goto out;
8325 /* The functions all have the same number of arguments.
8326 Determine whether arguments and return types vary in
8327 ways permitted for <tgmath.h> functions. */
8328 /* The first entry in each of these vectors is for the
8329 return type, subsequent entries for parameter
8330 types. */
8331 auto_vec<enum tgmath_parm_kind> parm_kind (nargs + 1);
8332 auto_vec<tree> parm_first (nargs + 1);
8333 auto_vec<bool> parm_complex (nargs + 1);
8334 auto_vec<bool> parm_varies (nargs + 1);
8335 tree first_type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[0].value));
8336 tree first_ret = TYPE_MAIN_VARIANT (TREE_TYPE (first_type));
8337 parm_first.quick_push (first_ret);
8338 parm_complex.quick_push (TREE_CODE (first_ret) == COMPLEX_TYPE);
8339 parm_varies.quick_push (false);
8340 function_args_iterator iter;
8341 tree t;
8342 unsigned int argpos;
8343 FOREACH_FUNCTION_ARGS (first_type, t, iter)
8345 if (t == void_type_node)
8346 break;
8347 parm_first.quick_push (TYPE_MAIN_VARIANT (t));
8348 parm_complex.quick_push (TREE_CODE (t) == COMPLEX_TYPE);
8349 parm_varies.quick_push (false);
8351 for (unsigned int j = 1; j < num_functions; j++)
8353 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8354 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8355 if (ret != parm_first[0])
8357 parm_varies[0] = true;
8358 if (!SCALAR_FLOAT_TYPE_P (parm_first[0])
8359 && !COMPLEX_FLOAT_TYPE_P (parm_first[0]))
8361 error_at ((*cexpr_list)[0].get_location (),
8362 "invalid type-generic return type for "
8363 "argument %u of %<__builtin_tgmath%>",
8365 expr.set_error ();
8366 goto out;
8368 if (!SCALAR_FLOAT_TYPE_P (ret)
8369 && !COMPLEX_FLOAT_TYPE_P (ret))
8371 error_at ((*cexpr_list)[j].get_location (),
8372 "invalid type-generic return type for "
8373 "argument %u of %<__builtin_tgmath%>",
8374 j + 1);
8375 expr.set_error ();
8376 goto out;
8379 if (TREE_CODE (ret) == COMPLEX_TYPE)
8380 parm_complex[0] = true;
8381 argpos = 1;
8382 FOREACH_FUNCTION_ARGS (type, t, iter)
8384 if (t == void_type_node)
8385 break;
8386 t = TYPE_MAIN_VARIANT (t);
8387 if (t != parm_first[argpos])
8389 parm_varies[argpos] = true;
8390 if (!SCALAR_FLOAT_TYPE_P (parm_first[argpos])
8391 && !COMPLEX_FLOAT_TYPE_P (parm_first[argpos]))
8393 error_at ((*cexpr_list)[0].get_location (),
8394 "invalid type-generic type for "
8395 "argument %u of argument %u of "
8396 "%<__builtin_tgmath%>", argpos, 1);
8397 expr.set_error ();
8398 goto out;
8400 if (!SCALAR_FLOAT_TYPE_P (t)
8401 && !COMPLEX_FLOAT_TYPE_P (t))
8403 error_at ((*cexpr_list)[j].get_location (),
8404 "invalid type-generic type for "
8405 "argument %u of argument %u of "
8406 "%<__builtin_tgmath%>", argpos, j + 1);
8407 expr.set_error ();
8408 goto out;
8411 if (TREE_CODE (t) == COMPLEX_TYPE)
8412 parm_complex[argpos] = true;
8413 argpos++;
8416 enum tgmath_parm_kind max_variation = tgmath_fixed;
8417 for (unsigned int j = 0; j <= nargs; j++)
8419 enum tgmath_parm_kind this_kind;
8420 if (parm_varies[j])
8422 if (parm_complex[j])
8423 max_variation = this_kind = tgmath_complex;
8424 else
8426 this_kind = tgmath_real;
8427 if (max_variation != tgmath_complex)
8428 max_variation = tgmath_real;
8431 else
8432 this_kind = tgmath_fixed;
8433 parm_kind.quick_push (this_kind);
8435 if (max_variation == tgmath_fixed)
8437 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8438 "all have the same type");
8439 expr.set_error ();
8440 break;
8443 /* Identify a parameter (not the return type) that varies,
8444 including with complex types if any variation includes
8445 complex types; there must be at least one such
8446 parameter. */
8447 unsigned int tgarg = 0;
8448 for (unsigned int j = 1; j <= nargs; j++)
8449 if (parm_kind[j] == max_variation)
8451 tgarg = j;
8452 break;
8454 if (tgarg == 0)
8456 error_at (loc, "function arguments of %<__builtin_tgmath%> "
8457 "lack type-generic parameter");
8458 expr.set_error ();
8459 break;
8462 /* Determine the type of the relevant parameter for each
8463 function. */
8464 auto_vec<tree> tg_type (num_functions);
8465 for (unsigned int j = 0; j < num_functions; j++)
8467 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8468 argpos = 1;
8469 FOREACH_FUNCTION_ARGS (type, t, iter)
8471 if (argpos == tgarg)
8473 tg_type.quick_push (TYPE_MAIN_VARIANT (t));
8474 break;
8476 argpos++;
8480 /* Verify that the corresponding types are different for
8481 all the listed functions. Also determine whether all
8482 the types are complex, whether all the types are
8483 standard or binary, and whether all the types are
8484 decimal. */
8485 bool all_complex = true;
8486 bool all_binary = true;
8487 bool all_decimal = true;
8488 hash_set<tree> tg_types;
8489 FOR_EACH_VEC_ELT (tg_type, i, t)
8491 if (TREE_CODE (t) == COMPLEX_TYPE)
8492 all_decimal = false;
8493 else
8495 all_complex = false;
8496 if (DECIMAL_FLOAT_TYPE_P (t))
8497 all_binary = false;
8498 else
8499 all_decimal = false;
8501 if (tg_types.add (t))
8503 error_at ((*cexpr_list)[i].get_location (),
8504 "duplicate type-generic parameter type for "
8505 "function argument %u of %<__builtin_tgmath%>",
8506 i + 1);
8507 expr.set_error ();
8508 goto out;
8512 /* Verify that other parameters and the return type whose
8513 types vary have their types varying in the correct
8514 way. */
8515 for (unsigned int j = 0; j < num_functions; j++)
8517 tree exp_type = tg_type[j];
8518 tree exp_real_type = exp_type;
8519 if (TREE_CODE (exp_type) == COMPLEX_TYPE)
8520 exp_real_type = TREE_TYPE (exp_type);
8521 tree type = TREE_TYPE (TREE_TYPE ((*cexpr_list)[j].value));
8522 tree ret = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8523 if ((parm_kind[0] == tgmath_complex && ret != exp_type)
8524 || (parm_kind[0] == tgmath_real && ret != exp_real_type))
8526 error_at ((*cexpr_list)[j].get_location (),
8527 "bad return type for function argument %u "
8528 "of %<__builtin_tgmath%>", j + 1);
8529 expr.set_error ();
8530 goto out;
8532 argpos = 1;
8533 FOREACH_FUNCTION_ARGS (type, t, iter)
8535 if (t == void_type_node)
8536 break;
8537 t = TYPE_MAIN_VARIANT (t);
8538 if ((parm_kind[argpos] == tgmath_complex
8539 && t != exp_type)
8540 || (parm_kind[argpos] == tgmath_real
8541 && t != exp_real_type))
8543 error_at ((*cexpr_list)[j].get_location (),
8544 "bad type for argument %u of "
8545 "function argument %u of "
8546 "%<__builtin_tgmath%>", argpos, j + 1);
8547 expr.set_error ();
8548 goto out;
8550 argpos++;
8554 /* The functions listed are a valid set of functions for a
8555 <tgmath.h> macro to select between. Identify the
8556 matching function, if any. First, the argument types
8557 must be combined following <tgmath.h> rules. Integer
8558 types are treated as _Decimal64 if any type-generic
8559 argument is decimal, or if the only alternatives for
8560 type-generic arguments are of decimal types, and are
8561 otherwise treated as double (or _Complex double for
8562 complex integer types, or _Float64 or _Complex _Float64
8563 if all the return types are the same _FloatN or
8564 _FloatNx type). After that adjustment, types are
8565 combined following the usual arithmetic conversions.
8566 If the function only accepts complex arguments, a
8567 complex type is produced. */
8568 bool arg_complex = all_complex;
8569 bool arg_binary = all_binary;
8570 bool arg_int_decimal = all_decimal;
8571 for (unsigned int j = 1; j <= nargs; j++)
8573 if (parm_kind[j] == tgmath_fixed)
8574 continue;
8575 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8576 tree type = TREE_TYPE (ce->value);
8577 if (!INTEGRAL_TYPE_P (type)
8578 && !SCALAR_FLOAT_TYPE_P (type)
8579 && TREE_CODE (type) != COMPLEX_TYPE)
8581 error_at (ce->get_location (),
8582 "invalid type of argument %u of type-generic "
8583 "function", j);
8584 expr.set_error ();
8585 goto out;
8587 if (DECIMAL_FLOAT_TYPE_P (type))
8589 arg_int_decimal = true;
8590 if (all_complex)
8592 error_at (ce->get_location (),
8593 "decimal floating-point argument %u to "
8594 "complex-only type-generic function", j);
8595 expr.set_error ();
8596 goto out;
8598 else if (all_binary)
8600 error_at (ce->get_location (),
8601 "decimal floating-point argument %u to "
8602 "binary-only type-generic function", j);
8603 expr.set_error ();
8604 goto out;
8606 else if (arg_complex)
8608 error_at (ce->get_location (),
8609 "both complex and decimal floating-point "
8610 "arguments to type-generic function");
8611 expr.set_error ();
8612 goto out;
8614 else if (arg_binary)
8616 error_at (ce->get_location (),
8617 "both binary and decimal floating-point "
8618 "arguments to type-generic function");
8619 expr.set_error ();
8620 goto out;
8623 else if (TREE_CODE (type) == COMPLEX_TYPE)
8625 arg_complex = true;
8626 if (COMPLEX_FLOAT_TYPE_P (type))
8627 arg_binary = true;
8628 if (all_decimal)
8630 error_at (ce->get_location (),
8631 "complex argument %u to "
8632 "decimal-only type-generic function", j);
8633 expr.set_error ();
8634 goto out;
8636 else if (arg_int_decimal)
8638 error_at (ce->get_location (),
8639 "both complex and decimal floating-point "
8640 "arguments to type-generic function");
8641 expr.set_error ();
8642 goto out;
8645 else if (SCALAR_FLOAT_TYPE_P (type))
8647 arg_binary = true;
8648 if (all_decimal)
8650 error_at (ce->get_location (),
8651 "binary argument %u to "
8652 "decimal-only type-generic function", j);
8653 expr.set_error ();
8654 goto out;
8656 else if (arg_int_decimal)
8658 error_at (ce->get_location (),
8659 "both binary and decimal floating-point "
8660 "arguments to type-generic function");
8661 expr.set_error ();
8662 goto out;
8666 /* For a macro rounding its result to a narrower type, map
8667 integer types to _Float64 not double if the return type
8668 is a _FloatN or _FloatNx type. */
8669 bool arg_int_float64 = false;
8670 if (parm_kind[0] == tgmath_fixed
8671 && SCALAR_FLOAT_TYPE_P (parm_first[0])
8672 && float64_type_node != NULL_TREE)
8673 for (unsigned int j = 0; j < NUM_FLOATN_NX_TYPES; j++)
8674 if (parm_first[0] == FLOATN_TYPE_NODE (j))
8676 arg_int_float64 = true;
8677 break;
8679 tree arg_real = NULL_TREE;
8680 for (unsigned int j = 1; j <= nargs; j++)
8682 if (parm_kind[j] == tgmath_fixed)
8683 continue;
8684 c_expr_t *ce = &(*cexpr_list)[num_functions + j - 1];
8685 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (ce->value));
8686 if (TREE_CODE (type) == COMPLEX_TYPE)
8687 type = TREE_TYPE (type);
8688 if (INTEGRAL_TYPE_P (type))
8689 type = (arg_int_decimal
8690 ? dfloat64_type_node
8691 : arg_int_float64
8692 ? float64_type_node
8693 : double_type_node);
8694 if (arg_real == NULL_TREE)
8695 arg_real = type;
8696 else
8697 arg_real = common_type (arg_real, type);
8698 if (arg_real == error_mark_node)
8700 expr.set_error ();
8701 goto out;
8704 tree arg_type = (arg_complex
8705 ? build_complex_type (arg_real)
8706 : arg_real);
8708 /* Look for a function to call with type-generic parameter
8709 type ARG_TYPE. */
8710 c_expr_t *fn = NULL;
8711 for (unsigned int j = 0; j < num_functions; j++)
8713 if (tg_type[j] == arg_type)
8715 fn = &(*cexpr_list)[j];
8716 break;
8719 if (fn == NULL
8720 && parm_kind[0] == tgmath_fixed
8721 && SCALAR_FLOAT_TYPE_P (parm_first[0]))
8723 /* Presume this is a macro that rounds its result to a
8724 narrower type, and look for the first function with
8725 at least the range and precision of the argument
8726 type. */
8727 for (unsigned int j = 0; j < num_functions; j++)
8729 if (arg_complex
8730 != (TREE_CODE (tg_type[j]) == COMPLEX_TYPE))
8731 continue;
8732 tree real_tg_type = (arg_complex
8733 ? TREE_TYPE (tg_type[j])
8734 : tg_type[j]);
8735 if (DECIMAL_FLOAT_TYPE_P (arg_real)
8736 != DECIMAL_FLOAT_TYPE_P (real_tg_type))
8737 continue;
8738 scalar_float_mode arg_mode
8739 = SCALAR_FLOAT_TYPE_MODE (arg_real);
8740 scalar_float_mode tg_mode
8741 = SCALAR_FLOAT_TYPE_MODE (real_tg_type);
8742 const real_format *arg_fmt = REAL_MODE_FORMAT (arg_mode);
8743 const real_format *tg_fmt = REAL_MODE_FORMAT (tg_mode);
8744 if (arg_fmt->b == tg_fmt->b
8745 && arg_fmt->p <= tg_fmt->p
8746 && arg_fmt->emax <= tg_fmt->emax
8747 && (arg_fmt->emin - arg_fmt->p
8748 >= tg_fmt->emin - tg_fmt->p))
8750 fn = &(*cexpr_list)[j];
8751 break;
8755 if (fn == NULL)
8757 error_at (loc, "no matching function for type-generic call");
8758 expr.set_error ();
8759 break;
8762 /* Construct a call to FN. */
8763 vec<tree, va_gc> *args;
8764 vec_alloc (args, nargs);
8765 vec<tree, va_gc> *origtypes;
8766 vec_alloc (origtypes, nargs);
8767 auto_vec<location_t> arg_loc (nargs);
8768 for (unsigned int j = 0; j < nargs; j++)
8770 c_expr_t *ce = &(*cexpr_list)[num_functions + j];
8771 args->quick_push (ce->value);
8772 arg_loc.quick_push (ce->get_location ());
8773 origtypes->quick_push (ce->original_type);
8775 expr.value = c_build_function_call_vec (loc, arg_loc, fn->value,
8776 args, origtypes);
8777 set_c_expr_source_range (&expr, loc, close_paren_loc);
8778 break;
8780 case RID_BUILTIN_CALL_WITH_STATIC_CHAIN:
8782 vec<c_expr_t, va_gc> *cexpr_list;
8783 c_expr_t *e2_p;
8784 tree chain_value;
8785 location_t close_paren_loc;
8787 c_parser_consume_token (parser);
8788 if (!c_parser_get_builtin_args (parser,
8789 "__builtin_call_with_static_chain",
8790 &cexpr_list, false,
8791 &close_paren_loc))
8793 expr.set_error ();
8794 break;
8796 if (vec_safe_length (cexpr_list) != 2)
8798 error_at (loc, "wrong number of arguments to "
8799 "%<__builtin_call_with_static_chain%>");
8800 expr.set_error ();
8801 break;
8804 expr = (*cexpr_list)[0];
8805 e2_p = &(*cexpr_list)[1];
8806 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8807 chain_value = e2_p->value;
8808 mark_exp_read (chain_value);
8810 if (TREE_CODE (expr.value) != CALL_EXPR)
8811 error_at (loc, "first argument to "
8812 "%<__builtin_call_with_static_chain%> "
8813 "must be a call expression");
8814 else if (TREE_CODE (TREE_TYPE (chain_value)) != POINTER_TYPE)
8815 error_at (loc, "second argument to "
8816 "%<__builtin_call_with_static_chain%> "
8817 "must be a pointer type");
8818 else
8819 CALL_EXPR_STATIC_CHAIN (expr.value) = chain_value;
8820 set_c_expr_source_range (&expr, loc, close_paren_loc);
8821 break;
8823 case RID_BUILTIN_COMPLEX:
8825 vec<c_expr_t, va_gc> *cexpr_list;
8826 c_expr_t *e1_p, *e2_p;
8827 location_t close_paren_loc;
8829 c_parser_consume_token (parser);
8830 if (!c_parser_get_builtin_args (parser,
8831 "__builtin_complex",
8832 &cexpr_list, false,
8833 &close_paren_loc))
8835 expr.set_error ();
8836 break;
8839 if (vec_safe_length (cexpr_list) != 2)
8841 error_at (loc, "wrong number of arguments to "
8842 "%<__builtin_complex%>");
8843 expr.set_error ();
8844 break;
8847 e1_p = &(*cexpr_list)[0];
8848 e2_p = &(*cexpr_list)[1];
8850 *e1_p = convert_lvalue_to_rvalue (loc, *e1_p, true, true);
8851 if (TREE_CODE (e1_p->value) == EXCESS_PRECISION_EXPR)
8852 e1_p->value = convert (TREE_TYPE (e1_p->value),
8853 TREE_OPERAND (e1_p->value, 0));
8854 *e2_p = convert_lvalue_to_rvalue (loc, *e2_p, true, true);
8855 if (TREE_CODE (e2_p->value) == EXCESS_PRECISION_EXPR)
8856 e2_p->value = convert (TREE_TYPE (e2_p->value),
8857 TREE_OPERAND (e2_p->value, 0));
8858 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8859 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e1_p->value))
8860 || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (e2_p->value))
8861 || DECIMAL_FLOAT_TYPE_P (TREE_TYPE (e2_p->value)))
8863 error_at (loc, "%<__builtin_complex%> operand "
8864 "not of real binary floating-point type");
8865 expr.set_error ();
8866 break;
8868 if (TYPE_MAIN_VARIANT (TREE_TYPE (e1_p->value))
8869 != TYPE_MAIN_VARIANT (TREE_TYPE (e2_p->value)))
8871 error_at (loc,
8872 "%<__builtin_complex%> operands of different types");
8873 expr.set_error ();
8874 break;
8876 pedwarn_c90 (loc, OPT_Wpedantic,
8877 "ISO C90 does not support complex types");
8878 expr.value = build2_loc (loc, COMPLEX_EXPR,
8879 build_complex_type
8880 (TYPE_MAIN_VARIANT
8881 (TREE_TYPE (e1_p->value))),
8882 e1_p->value, e2_p->value);
8883 set_c_expr_source_range (&expr, loc, close_paren_loc);
8884 break;
8886 case RID_BUILTIN_SHUFFLE:
8888 vec<c_expr_t, va_gc> *cexpr_list;
8889 unsigned int i;
8890 c_expr_t *p;
8891 location_t close_paren_loc;
8893 c_parser_consume_token (parser);
8894 if (!c_parser_get_builtin_args (parser,
8895 "__builtin_shuffle",
8896 &cexpr_list, false,
8897 &close_paren_loc))
8899 expr.set_error ();
8900 break;
8903 FOR_EACH_VEC_SAFE_ELT (cexpr_list, i, p)
8904 *p = convert_lvalue_to_rvalue (loc, *p, true, true);
8906 if (vec_safe_length (cexpr_list) == 2)
8907 expr.value =
8908 c_build_vec_perm_expr
8909 (loc, (*cexpr_list)[0].value,
8910 NULL_TREE, (*cexpr_list)[1].value);
8912 else if (vec_safe_length (cexpr_list) == 3)
8913 expr.value =
8914 c_build_vec_perm_expr
8915 (loc, (*cexpr_list)[0].value,
8916 (*cexpr_list)[1].value,
8917 (*cexpr_list)[2].value);
8918 else
8920 error_at (loc, "wrong number of arguments to "
8921 "%<__builtin_shuffle%>");
8922 expr.set_error ();
8924 set_c_expr_source_range (&expr, loc, close_paren_loc);
8925 break;
8927 case RID_AT_SELECTOR:
8929 gcc_assert (c_dialect_objc ());
8930 c_parser_consume_token (parser);
8931 matching_parens parens;
8932 if (!parens.require_open (parser))
8934 expr.set_error ();
8935 break;
8937 tree sel = c_parser_objc_selector_arg (parser);
8938 location_t close_loc = c_parser_peek_token (parser)->location;
8939 parens.skip_until_found_close (parser);
8940 expr.value = objc_build_selector_expr (loc, sel);
8941 set_c_expr_source_range (&expr, loc, close_loc);
8943 break;
8944 case RID_AT_PROTOCOL:
8946 gcc_assert (c_dialect_objc ());
8947 c_parser_consume_token (parser);
8948 matching_parens parens;
8949 if (!parens.require_open (parser))
8951 expr.set_error ();
8952 break;
8954 if (c_parser_next_token_is_not (parser, CPP_NAME))
8956 c_parser_error (parser, "expected identifier");
8957 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8958 expr.set_error ();
8959 break;
8961 tree id = c_parser_peek_token (parser)->value;
8962 c_parser_consume_token (parser);
8963 location_t close_loc = c_parser_peek_token (parser)->location;
8964 parens.skip_until_found_close (parser);
8965 expr.value = objc_build_protocol_expr (id);
8966 set_c_expr_source_range (&expr, loc, close_loc);
8968 break;
8969 case RID_AT_ENCODE:
8971 /* Extension to support C-structures in the archiver. */
8972 gcc_assert (c_dialect_objc ());
8973 c_parser_consume_token (parser);
8974 matching_parens parens;
8975 if (!parens.require_open (parser))
8977 expr.set_error ();
8978 break;
8980 t1 = c_parser_type_name (parser);
8981 if (t1 == NULL)
8983 expr.set_error ();
8984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
8985 break;
8987 location_t close_loc = c_parser_peek_token (parser)->location;
8988 parens.skip_until_found_close (parser);
8989 tree type = groktypename (t1, NULL, NULL);
8990 expr.value = objc_build_encode_expr (type);
8991 set_c_expr_source_range (&expr, loc, close_loc);
8993 break;
8994 case RID_GENERIC:
8995 expr = c_parser_generic_selection (parser);
8996 break;
8997 default:
8998 c_parser_error (parser, "expected expression");
8999 expr.set_error ();
9000 break;
9002 break;
9003 case CPP_OPEN_SQUARE:
9004 if (c_dialect_objc ())
9006 tree receiver, args;
9007 c_parser_consume_token (parser);
9008 receiver = c_parser_objc_receiver (parser);
9009 args = c_parser_objc_message_args (parser);
9010 location_t close_loc = c_parser_peek_token (parser)->location;
9011 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9012 "expected %<]%>");
9013 expr.value = objc_build_message_expr (receiver, args);
9014 set_c_expr_source_range (&expr, loc, close_loc);
9015 break;
9017 /* Else fall through to report error. */
9018 /* FALLTHRU */
9019 default:
9020 c_parser_error (parser, "expected expression");
9021 expr.set_error ();
9022 break;
9024 out:
9025 return c_parser_postfix_expression_after_primary
9026 (parser, EXPR_LOC_OR_LOC (expr.value, loc), expr);
9029 /* Parse a postfix expression after a parenthesized type name: the
9030 brace-enclosed initializer of a compound literal, possibly followed
9031 by some postfix operators. This is separate because it is not
9032 possible to tell until after the type name whether a cast
9033 expression has a cast or a compound literal, or whether the operand
9034 of sizeof is a parenthesized type name or starts with a compound
9035 literal. TYPE_LOC is the location where TYPE_NAME starts--the
9036 location of the first token after the parentheses around the type
9037 name. */
9039 static struct c_expr
9040 c_parser_postfix_expression_after_paren_type (c_parser *parser,
9041 struct c_type_name *type_name,
9042 location_t type_loc)
9044 tree type;
9045 struct c_expr init;
9046 bool non_const;
9047 struct c_expr expr;
9048 location_t start_loc;
9049 tree type_expr = NULL_TREE;
9050 bool type_expr_const = true;
9051 check_compound_literal_type (type_loc, type_name);
9052 rich_location richloc (line_table, type_loc);
9053 start_init (NULL_TREE, NULL, 0, &richloc);
9054 type = groktypename (type_name, &type_expr, &type_expr_const);
9055 start_loc = c_parser_peek_token (parser)->location;
9056 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type))
9058 error_at (type_loc, "compound literal has variable size");
9059 type = error_mark_node;
9061 init = c_parser_braced_init (parser, type, false, NULL);
9062 finish_init ();
9063 maybe_warn_string_init (type_loc, type, init);
9065 if (type != error_mark_node
9066 && !ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (type))
9067 && current_function_decl)
9069 error ("compound literal qualified by address-space qualifier");
9070 type = error_mark_node;
9073 pedwarn_c90 (start_loc, OPT_Wpedantic, "ISO C90 forbids compound literals");
9074 non_const = ((init.value && TREE_CODE (init.value) == CONSTRUCTOR)
9075 ? CONSTRUCTOR_NON_CONST (init.value)
9076 : init.original_code == C_MAYBE_CONST_EXPR);
9077 non_const |= !type_expr_const;
9078 unsigned int alignas_align = 0;
9079 if (type != error_mark_node
9080 && type_name->specs->align_log != -1)
9082 alignas_align = 1U << type_name->specs->align_log;
9083 if (alignas_align < min_align_of_type (type))
9085 error_at (type_name->specs->locations[cdw_alignas],
9086 "%<_Alignas%> specifiers cannot reduce "
9087 "alignment of compound literal");
9088 alignas_align = 0;
9091 expr.value = build_compound_literal (start_loc, type, init.value, non_const,
9092 alignas_align);
9093 set_c_expr_source_range (&expr, init.src_range);
9094 expr.original_code = ERROR_MARK;
9095 expr.original_type = NULL;
9096 if (type != error_mark_node
9097 && expr.value != error_mark_node
9098 && type_expr)
9100 if (TREE_CODE (expr.value) == C_MAYBE_CONST_EXPR)
9102 gcc_assert (C_MAYBE_CONST_EXPR_PRE (expr.value) == NULL_TREE);
9103 C_MAYBE_CONST_EXPR_PRE (expr.value) = type_expr;
9105 else
9107 gcc_assert (!non_const);
9108 expr.value = build2 (C_MAYBE_CONST_EXPR, type,
9109 type_expr, expr.value);
9112 return c_parser_postfix_expression_after_primary (parser, start_loc, expr);
9115 /* Callback function for sizeof_pointer_memaccess_warning to compare
9116 types. */
9118 static bool
9119 sizeof_ptr_memacc_comptypes (tree type1, tree type2)
9121 return comptypes (type1, type2) == 1;
9124 /* Warn for patterns where abs-like function appears to be used incorrectly,
9125 gracefully ignore any non-abs-like function. The warning location should
9126 be LOC. FNDECL is the declaration of called function, it must be a
9127 BUILT_IN_NORMAL function. ARG is the first and only argument of the
9128 call. */
9130 static void
9131 warn_for_abs (location_t loc, tree fndecl, tree arg)
9133 tree atype = TREE_TYPE (arg);
9135 /* Casts from pointers (and thus arrays and fndecls) will generate
9136 -Wint-conversion warnings. Most other wrong types hopefully lead to type
9137 mismatch errors. TODO: Think about what to do with FIXED_POINT_TYPE_P
9138 types and possibly other exotic types. */
9139 if (!INTEGRAL_TYPE_P (atype)
9140 && !SCALAR_FLOAT_TYPE_P (atype)
9141 && TREE_CODE (atype) != COMPLEX_TYPE)
9142 return;
9144 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
9146 switch (fcode)
9148 case BUILT_IN_ABS:
9149 case BUILT_IN_LABS:
9150 case BUILT_IN_LLABS:
9151 case BUILT_IN_IMAXABS:
9152 if (!INTEGRAL_TYPE_P (atype))
9154 if (SCALAR_FLOAT_TYPE_P (atype))
9155 warning_at (loc, OPT_Wabsolute_value,
9156 "using integer absolute value function %qD when "
9157 "argument is of floating point type %qT",
9158 fndecl, atype);
9159 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9160 warning_at (loc, OPT_Wabsolute_value,
9161 "using integer absolute value function %qD when "
9162 "argument is of complex type %qT", fndecl, atype);
9163 else
9164 gcc_unreachable ();
9165 return;
9167 if (TYPE_UNSIGNED (atype))
9168 warning_at (loc, OPT_Wabsolute_value,
9169 "taking the absolute value of unsigned type %qT "
9170 "has no effect", atype);
9171 break;
9173 CASE_FLT_FN (BUILT_IN_FABS):
9174 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
9175 if (!SCALAR_FLOAT_TYPE_P (atype)
9176 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (atype)))
9178 if (INTEGRAL_TYPE_P (atype))
9179 warning_at (loc, OPT_Wabsolute_value,
9180 "using floating point absolute value function %qD "
9181 "when argument is of integer type %qT", fndecl, atype);
9182 else if (DECIMAL_FLOAT_TYPE_P (atype))
9183 warning_at (loc, OPT_Wabsolute_value,
9184 "using floating point absolute value function %qD "
9185 "when argument is of decimal floating point type %qT",
9186 fndecl, atype);
9187 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9188 warning_at (loc, OPT_Wabsolute_value,
9189 "using floating point absolute value function %qD when "
9190 "argument is of complex type %qT", fndecl, atype);
9191 else
9192 gcc_unreachable ();
9193 return;
9195 break;
9197 CASE_FLT_FN (BUILT_IN_CABS):
9198 if (TREE_CODE (atype) != COMPLEX_TYPE)
9200 if (INTEGRAL_TYPE_P (atype))
9201 warning_at (loc, OPT_Wabsolute_value,
9202 "using complex absolute value function %qD when "
9203 "argument is of integer type %qT", fndecl, atype);
9204 else if (SCALAR_FLOAT_TYPE_P (atype))
9205 warning_at (loc, OPT_Wabsolute_value,
9206 "using complex absolute value function %qD when "
9207 "argument is of floating point type %qT",
9208 fndecl, atype);
9209 else
9210 gcc_unreachable ();
9212 return;
9214 break;
9216 case BUILT_IN_FABSD32:
9217 case BUILT_IN_FABSD64:
9218 case BUILT_IN_FABSD128:
9219 if (!DECIMAL_FLOAT_TYPE_P (atype))
9221 if (INTEGRAL_TYPE_P (atype))
9222 warning_at (loc, OPT_Wabsolute_value,
9223 "using decimal floating point absolute value "
9224 "function %qD when argument is of integer type %qT",
9225 fndecl, atype);
9226 else if (SCALAR_FLOAT_TYPE_P (atype))
9227 warning_at (loc, OPT_Wabsolute_value,
9228 "using decimal floating point absolute value "
9229 "function %qD when argument is of floating point "
9230 "type %qT", fndecl, atype);
9231 else if (TREE_CODE (atype) == COMPLEX_TYPE)
9232 warning_at (loc, OPT_Wabsolute_value,
9233 "using decimal floating point absolute value "
9234 "function %qD when argument is of complex type %qT",
9235 fndecl, atype);
9236 else
9237 gcc_unreachable ();
9238 return;
9240 break;
9242 default:
9243 return;
9246 if (!TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
9247 return;
9249 tree ftype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
9250 if (TREE_CODE (atype) == COMPLEX_TYPE)
9252 gcc_assert (TREE_CODE (ftype) == COMPLEX_TYPE);
9253 atype = TREE_TYPE (atype);
9254 ftype = TREE_TYPE (ftype);
9257 if (TYPE_PRECISION (ftype) < TYPE_PRECISION (atype))
9258 warning_at (loc, OPT_Wabsolute_value,
9259 "absolute value function %qD given an argument of type %qT "
9260 "but has parameter of type %qT which may cause truncation "
9261 "of value", fndecl, atype, ftype);
9265 /* Parse a postfix expression after the initial primary or compound
9266 literal; that is, parse a series of postfix operators.
9268 EXPR_LOC is the location of the primary expression. */
9270 static struct c_expr
9271 c_parser_postfix_expression_after_primary (c_parser *parser,
9272 location_t expr_loc,
9273 struct c_expr expr)
9275 struct c_expr orig_expr;
9276 tree ident, idx;
9277 location_t sizeof_arg_loc[3], comp_loc;
9278 tree sizeof_arg[3];
9279 unsigned int literal_zero_mask;
9280 unsigned int i;
9281 vec<tree, va_gc> *exprlist;
9282 vec<tree, va_gc> *origtypes = NULL;
9283 vec<location_t> arg_loc = vNULL;
9284 location_t start;
9285 location_t finish;
9287 while (true)
9289 location_t op_loc = c_parser_peek_token (parser)->location;
9290 switch (c_parser_peek_token (parser)->type)
9292 case CPP_OPEN_SQUARE:
9293 /* Array reference. */
9294 c_parser_consume_token (parser);
9295 idx = c_parser_expression (parser).value;
9296 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE,
9297 "expected %<]%>");
9298 start = expr.get_start ();
9299 finish = parser->tokens_buf[0].location;
9300 expr.value = build_array_ref (op_loc, expr.value, idx);
9301 set_c_expr_source_range (&expr, start, finish);
9302 expr.original_code = ERROR_MARK;
9303 expr.original_type = NULL;
9304 break;
9305 case CPP_OPEN_PAREN:
9306 /* Function call. */
9307 c_parser_consume_token (parser);
9308 for (i = 0; i < 3; i++)
9310 sizeof_arg[i] = NULL_TREE;
9311 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
9313 literal_zero_mask = 0;
9314 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9315 exprlist = NULL;
9316 else
9317 exprlist = c_parser_expr_list (parser, true, false, &origtypes,
9318 sizeof_arg_loc, sizeof_arg,
9319 &arg_loc, &literal_zero_mask);
9320 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
9321 "expected %<)%>");
9322 orig_expr = expr;
9323 mark_exp_read (expr.value);
9324 if (warn_sizeof_pointer_memaccess)
9325 sizeof_pointer_memaccess_warning (sizeof_arg_loc,
9326 expr.value, exprlist,
9327 sizeof_arg,
9328 sizeof_ptr_memacc_comptypes);
9329 if (TREE_CODE (expr.value) == FUNCTION_DECL)
9331 if (fndecl_built_in_p (expr.value, BUILT_IN_MEMSET)
9332 && vec_safe_length (exprlist) == 3)
9334 tree arg0 = (*exprlist)[0];
9335 tree arg2 = (*exprlist)[2];
9336 warn_for_memset (expr_loc, arg0, arg2, literal_zero_mask);
9338 if (warn_absolute_value
9339 && fndecl_built_in_p (expr.value, BUILT_IN_NORMAL)
9340 && vec_safe_length (exprlist) == 1)
9341 warn_for_abs (expr_loc, expr.value, (*exprlist)[0]);
9344 start = expr.get_start ();
9345 finish = parser->tokens_buf[0].get_finish ();
9346 expr.value
9347 = c_build_function_call_vec (expr_loc, arg_loc, expr.value,
9348 exprlist, origtypes);
9349 set_c_expr_source_range (&expr, start, finish);
9351 expr.original_code = ERROR_MARK;
9352 if (TREE_CODE (expr.value) == INTEGER_CST
9353 && TREE_CODE (orig_expr.value) == FUNCTION_DECL
9354 && fndecl_built_in_p (orig_expr.value, BUILT_IN_CONSTANT_P))
9355 expr.original_code = C_MAYBE_CONST_EXPR;
9356 expr.original_type = NULL;
9357 if (exprlist)
9359 release_tree_vector (exprlist);
9360 release_tree_vector (origtypes);
9362 arg_loc.release ();
9363 break;
9364 case CPP_DOT:
9365 /* Structure element reference. */
9366 c_parser_consume_token (parser);
9367 expr = default_function_array_conversion (expr_loc, expr);
9368 if (c_parser_next_token_is (parser, CPP_NAME))
9370 c_token *comp_tok = c_parser_peek_token (parser);
9371 ident = comp_tok->value;
9372 comp_loc = comp_tok->location;
9374 else
9376 c_parser_error (parser, "expected identifier");
9377 expr.set_error ();
9378 expr.original_code = ERROR_MARK;
9379 expr.original_type = NULL;
9380 return expr;
9382 start = expr.get_start ();
9383 finish = c_parser_peek_token (parser)->get_finish ();
9384 c_parser_consume_token (parser);
9385 expr.value = build_component_ref (op_loc, expr.value, ident,
9386 comp_loc);
9387 set_c_expr_source_range (&expr, start, finish);
9388 expr.original_code = ERROR_MARK;
9389 if (TREE_CODE (expr.value) != COMPONENT_REF)
9390 expr.original_type = NULL;
9391 else
9393 /* Remember the original type of a bitfield. */
9394 tree field = TREE_OPERAND (expr.value, 1);
9395 if (TREE_CODE (field) != FIELD_DECL)
9396 expr.original_type = NULL;
9397 else
9398 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9400 break;
9401 case CPP_DEREF:
9402 /* Structure element reference. */
9403 c_parser_consume_token (parser);
9404 expr = convert_lvalue_to_rvalue (expr_loc, expr, true, false);
9405 if (c_parser_next_token_is (parser, CPP_NAME))
9407 c_token *comp_tok = c_parser_peek_token (parser);
9408 ident = comp_tok->value;
9409 comp_loc = comp_tok->location;
9411 else
9413 c_parser_error (parser, "expected identifier");
9414 expr.set_error ();
9415 expr.original_code = ERROR_MARK;
9416 expr.original_type = NULL;
9417 return expr;
9419 start = expr.get_start ();
9420 finish = c_parser_peek_token (parser)->get_finish ();
9421 c_parser_consume_token (parser);
9422 expr.value = build_component_ref (op_loc,
9423 build_indirect_ref (op_loc,
9424 expr.value,
9425 RO_ARROW),
9426 ident, comp_loc);
9427 set_c_expr_source_range (&expr, start, finish);
9428 expr.original_code = ERROR_MARK;
9429 if (TREE_CODE (expr.value) != COMPONENT_REF)
9430 expr.original_type = NULL;
9431 else
9433 /* Remember the original type of a bitfield. */
9434 tree field = TREE_OPERAND (expr.value, 1);
9435 if (TREE_CODE (field) != FIELD_DECL)
9436 expr.original_type = NULL;
9437 else
9438 expr.original_type = DECL_BIT_FIELD_TYPE (field);
9440 break;
9441 case CPP_PLUS_PLUS:
9442 /* Postincrement. */
9443 start = expr.get_start ();
9444 finish = c_parser_peek_token (parser)->get_finish ();
9445 c_parser_consume_token (parser);
9446 expr = default_function_array_read_conversion (expr_loc, expr);
9447 expr.value = build_unary_op (op_loc, POSTINCREMENT_EXPR,
9448 expr.value, false);
9449 set_c_expr_source_range (&expr, start, finish);
9450 expr.original_code = ERROR_MARK;
9451 expr.original_type = NULL;
9452 break;
9453 case CPP_MINUS_MINUS:
9454 /* Postdecrement. */
9455 start = expr.get_start ();
9456 finish = c_parser_peek_token (parser)->get_finish ();
9457 c_parser_consume_token (parser);
9458 expr = default_function_array_read_conversion (expr_loc, expr);
9459 expr.value = build_unary_op (op_loc, POSTDECREMENT_EXPR,
9460 expr.value, false);
9461 set_c_expr_source_range (&expr, start, finish);
9462 expr.original_code = ERROR_MARK;
9463 expr.original_type = NULL;
9464 break;
9465 default:
9466 return expr;
9471 /* Parse an expression (C90 6.3.17, C99 6.5.17, C11 6.5.17).
9473 expression:
9474 assignment-expression
9475 expression , assignment-expression
9478 static struct c_expr
9479 c_parser_expression (c_parser *parser)
9481 location_t tloc = c_parser_peek_token (parser)->location;
9482 struct c_expr expr;
9483 expr = c_parser_expr_no_commas (parser, NULL);
9484 if (c_parser_next_token_is (parser, CPP_COMMA))
9485 expr = convert_lvalue_to_rvalue (tloc, expr, true, false);
9486 while (c_parser_next_token_is (parser, CPP_COMMA))
9488 struct c_expr next;
9489 tree lhsval;
9490 location_t loc = c_parser_peek_token (parser)->location;
9491 location_t expr_loc;
9492 c_parser_consume_token (parser);
9493 expr_loc = c_parser_peek_token (parser)->location;
9494 lhsval = expr.value;
9495 while (TREE_CODE (lhsval) == COMPOUND_EXPR)
9496 lhsval = TREE_OPERAND (lhsval, 1);
9497 if (DECL_P (lhsval) || handled_component_p (lhsval))
9498 mark_exp_read (lhsval);
9499 next = c_parser_expr_no_commas (parser, NULL);
9500 next = convert_lvalue_to_rvalue (expr_loc, next, true, false);
9501 expr.value = build_compound_expr (loc, expr.value, next.value);
9502 expr.original_code = COMPOUND_EXPR;
9503 expr.original_type = next.original_type;
9505 return expr;
9508 /* Parse an expression and convert functions or arrays to pointers and
9509 lvalues to rvalues. */
9511 static struct c_expr
9512 c_parser_expression_conv (c_parser *parser)
9514 struct c_expr expr;
9515 location_t loc = c_parser_peek_token (parser)->location;
9516 expr = c_parser_expression (parser);
9517 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
9518 return expr;
9521 /* Helper function of c_parser_expr_list. Check if IDXth (0 based)
9522 argument is a literal zero alone and if so, set it in literal_zero_mask. */
9524 static inline void
9525 c_parser_check_literal_zero (c_parser *parser, unsigned *literal_zero_mask,
9526 unsigned int idx)
9528 if (idx >= HOST_BITS_PER_INT)
9529 return;
9531 c_token *tok = c_parser_peek_token (parser);
9532 switch (tok->type)
9534 case CPP_NUMBER:
9535 case CPP_CHAR:
9536 case CPP_WCHAR:
9537 case CPP_CHAR16:
9538 case CPP_CHAR32:
9539 /* If a parameter is literal zero alone, remember it
9540 for -Wmemset-transposed-args warning. */
9541 if (integer_zerop (tok->value)
9542 && !TREE_OVERFLOW (tok->value)
9543 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9544 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
9545 *literal_zero_mask |= 1U << idx;
9546 default:
9547 break;
9551 /* Parse a non-empty list of expressions. If CONVERT_P, convert
9552 functions and arrays to pointers and lvalues to rvalues. If
9553 FOLD_P, fold the expressions. If LOCATIONS is non-NULL, save the
9554 locations of function arguments into this vector.
9556 nonempty-expr-list:
9557 assignment-expression
9558 nonempty-expr-list , assignment-expression
9561 static vec<tree, va_gc> *
9562 c_parser_expr_list (c_parser *parser, bool convert_p, bool fold_p,
9563 vec<tree, va_gc> **p_orig_types,
9564 location_t *sizeof_arg_loc, tree *sizeof_arg,
9565 vec<location_t> *locations,
9566 unsigned int *literal_zero_mask)
9568 vec<tree, va_gc> *ret;
9569 vec<tree, va_gc> *orig_types;
9570 struct c_expr expr;
9571 unsigned int idx = 0;
9573 ret = make_tree_vector ();
9574 if (p_orig_types == NULL)
9575 orig_types = NULL;
9576 else
9577 orig_types = make_tree_vector ();
9579 if (literal_zero_mask)
9580 c_parser_check_literal_zero (parser, literal_zero_mask, 0);
9581 expr = c_parser_expr_no_commas (parser, NULL);
9582 if (convert_p)
9583 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true, true);
9584 if (fold_p)
9585 expr.value = c_fully_fold (expr.value, false, NULL);
9586 ret->quick_push (expr.value);
9587 if (orig_types)
9588 orig_types->quick_push (expr.original_type);
9589 if (locations)
9590 locations->safe_push (expr.get_location ());
9591 if (sizeof_arg != NULL
9592 && expr.original_code == SIZEOF_EXPR)
9594 sizeof_arg[0] = c_last_sizeof_arg;
9595 sizeof_arg_loc[0] = c_last_sizeof_loc;
9597 while (c_parser_next_token_is (parser, CPP_COMMA))
9599 c_parser_consume_token (parser);
9600 if (literal_zero_mask)
9601 c_parser_check_literal_zero (parser, literal_zero_mask, idx + 1);
9602 expr = c_parser_expr_no_commas (parser, NULL);
9603 if (convert_p)
9604 expr = convert_lvalue_to_rvalue (expr.get_location (), expr, true,
9605 true);
9606 if (fold_p)
9607 expr.value = c_fully_fold (expr.value, false, NULL);
9608 vec_safe_push (ret, expr.value);
9609 if (orig_types)
9610 vec_safe_push (orig_types, expr.original_type);
9611 if (locations)
9612 locations->safe_push (expr.get_location ());
9613 if (++idx < 3
9614 && sizeof_arg != NULL
9615 && expr.original_code == SIZEOF_EXPR)
9617 sizeof_arg[idx] = c_last_sizeof_arg;
9618 sizeof_arg_loc[idx] = c_last_sizeof_loc;
9621 if (orig_types)
9622 *p_orig_types = orig_types;
9623 return ret;
9626 /* Parse Objective-C-specific constructs. */
9628 /* Parse an objc-class-definition.
9630 objc-class-definition:
9631 @interface identifier objc-superclass[opt] objc-protocol-refs[opt]
9632 objc-class-instance-variables[opt] objc-methodprotolist @end
9633 @implementation identifier objc-superclass[opt]
9634 objc-class-instance-variables[opt]
9635 @interface identifier ( identifier ) objc-protocol-refs[opt]
9636 objc-methodprotolist @end
9637 @interface identifier ( ) objc-protocol-refs[opt]
9638 objc-methodprotolist @end
9639 @implementation identifier ( identifier )
9641 objc-superclass:
9642 : identifier
9644 "@interface identifier (" must start "@interface identifier (
9645 identifier ) ...": objc-methodprotolist in the first production may
9646 not start with a parenthesized identifier as a declarator of a data
9647 definition with no declaration specifiers if the objc-superclass,
9648 objc-protocol-refs and objc-class-instance-variables are omitted. */
9650 static void
9651 c_parser_objc_class_definition (c_parser *parser, tree attributes)
9653 bool iface_p;
9654 tree id1;
9655 tree superclass;
9656 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE))
9657 iface_p = true;
9658 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION))
9659 iface_p = false;
9660 else
9661 gcc_unreachable ();
9663 c_parser_consume_token (parser);
9664 if (c_parser_next_token_is_not (parser, CPP_NAME))
9666 c_parser_error (parser, "expected identifier");
9667 return;
9669 id1 = c_parser_peek_token (parser)->value;
9670 c_parser_consume_token (parser);
9671 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
9673 /* We have a category or class extension. */
9674 tree id2;
9675 tree proto = NULL_TREE;
9676 matching_parens parens;
9677 parens.consume_open (parser);
9678 if (c_parser_next_token_is_not (parser, CPP_NAME))
9680 if (iface_p && c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
9682 /* We have a class extension. */
9683 id2 = NULL_TREE;
9685 else
9687 c_parser_error (parser, "expected identifier or %<)%>");
9688 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
9689 return;
9692 else
9694 id2 = c_parser_peek_token (parser)->value;
9695 c_parser_consume_token (parser);
9697 parens.skip_until_found_close (parser);
9698 if (!iface_p)
9700 objc_start_category_implementation (id1, id2);
9701 return;
9703 if (c_parser_next_token_is (parser, CPP_LESS))
9704 proto = c_parser_objc_protocol_refs (parser);
9705 objc_start_category_interface (id1, id2, proto, attributes);
9706 c_parser_objc_methodprotolist (parser);
9707 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9708 objc_finish_interface ();
9709 return;
9711 if (c_parser_next_token_is (parser, CPP_COLON))
9713 c_parser_consume_token (parser);
9714 if (c_parser_next_token_is_not (parser, CPP_NAME))
9716 c_parser_error (parser, "expected identifier");
9717 return;
9719 superclass = c_parser_peek_token (parser)->value;
9720 c_parser_consume_token (parser);
9722 else
9723 superclass = NULL_TREE;
9724 if (iface_p)
9726 tree proto = NULL_TREE;
9727 if (c_parser_next_token_is (parser, CPP_LESS))
9728 proto = c_parser_objc_protocol_refs (parser);
9729 objc_start_class_interface (id1, superclass, proto, attributes);
9731 else
9732 objc_start_class_implementation (id1, superclass);
9733 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
9734 c_parser_objc_class_instance_variables (parser);
9735 if (iface_p)
9737 objc_continue_interface ();
9738 c_parser_objc_methodprotolist (parser);
9739 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9740 objc_finish_interface ();
9742 else
9744 objc_continue_implementation ();
9745 return;
9749 /* Parse objc-class-instance-variables.
9751 objc-class-instance-variables:
9752 { objc-instance-variable-decl-list[opt] }
9754 objc-instance-variable-decl-list:
9755 objc-visibility-spec
9756 objc-instance-variable-decl ;
9758 objc-instance-variable-decl-list objc-visibility-spec
9759 objc-instance-variable-decl-list objc-instance-variable-decl ;
9760 objc-instance-variable-decl-list ;
9762 objc-visibility-spec:
9763 @private
9764 @protected
9765 @public
9767 objc-instance-variable-decl:
9768 struct-declaration
9771 static void
9772 c_parser_objc_class_instance_variables (c_parser *parser)
9774 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE));
9775 c_parser_consume_token (parser);
9776 while (c_parser_next_token_is_not (parser, CPP_EOF))
9778 tree decls;
9779 /* Parse any stray semicolon. */
9780 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
9782 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
9783 "extra semicolon");
9784 c_parser_consume_token (parser);
9785 continue;
9787 /* Stop if at the end of the instance variables. */
9788 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
9790 c_parser_consume_token (parser);
9791 break;
9793 /* Parse any objc-visibility-spec. */
9794 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE))
9796 c_parser_consume_token (parser);
9797 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
9798 continue;
9800 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED))
9802 c_parser_consume_token (parser);
9803 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
9804 continue;
9806 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC))
9808 c_parser_consume_token (parser);
9809 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
9810 continue;
9812 else if (c_parser_next_token_is_keyword (parser, RID_AT_PACKAGE))
9814 c_parser_consume_token (parser);
9815 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
9816 continue;
9818 else if (c_parser_next_token_is (parser, CPP_PRAGMA))
9820 c_parser_pragma (parser, pragma_external, NULL);
9821 continue;
9824 /* Parse some comma-separated declarations. */
9825 decls = c_parser_struct_declaration (parser);
9826 if (decls == NULL)
9828 /* There is a syntax error. We want to skip the offending
9829 tokens up to the next ';' (included) or '}'
9830 (excluded). */
9832 /* First, skip manually a ')' or ']'. This is because they
9833 reduce the nesting level, so c_parser_skip_until_found()
9834 wouldn't be able to skip past them. */
9835 c_token *token = c_parser_peek_token (parser);
9836 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_CLOSE_SQUARE)
9837 c_parser_consume_token (parser);
9839 /* Then, do the standard skipping. */
9840 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9842 /* We hopefully recovered. Start normal parsing again. */
9843 parser->error = false;
9844 continue;
9846 else
9848 /* Comma-separated instance variables are chained together
9849 in reverse order; add them one by one. */
9850 tree ivar = nreverse (decls);
9851 for (; ivar; ivar = DECL_CHAIN (ivar))
9852 objc_add_instance_variable (copy_node (ivar));
9854 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9858 /* Parse an objc-class-declaration.
9860 objc-class-declaration:
9861 @class identifier-list ;
9864 static void
9865 c_parser_objc_class_declaration (c_parser *parser)
9867 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS));
9868 c_parser_consume_token (parser);
9869 /* Any identifiers, including those declared as type names, are OK
9870 here. */
9871 while (true)
9873 tree id;
9874 if (c_parser_next_token_is_not (parser, CPP_NAME))
9876 c_parser_error (parser, "expected identifier");
9877 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9878 parser->error = false;
9879 return;
9881 id = c_parser_peek_token (parser)->value;
9882 objc_declare_class (id);
9883 c_parser_consume_token (parser);
9884 if (c_parser_next_token_is (parser, CPP_COMMA))
9885 c_parser_consume_token (parser);
9886 else
9887 break;
9889 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9892 /* Parse an objc-alias-declaration.
9894 objc-alias-declaration:
9895 @compatibility_alias identifier identifier ;
9898 static void
9899 c_parser_objc_alias_declaration (c_parser *parser)
9901 tree id1, id2;
9902 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS));
9903 c_parser_consume_token (parser);
9904 if (c_parser_next_token_is_not (parser, CPP_NAME))
9906 c_parser_error (parser, "expected identifier");
9907 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9908 return;
9910 id1 = c_parser_peek_token (parser)->value;
9911 c_parser_consume_token (parser);
9912 if (c_parser_next_token_is_not (parser, CPP_NAME))
9914 c_parser_error (parser, "expected identifier");
9915 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
9916 return;
9918 id2 = c_parser_peek_token (parser)->value;
9919 c_parser_consume_token (parser);
9920 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9921 objc_declare_alias (id1, id2);
9924 /* Parse an objc-protocol-definition.
9926 objc-protocol-definition:
9927 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end
9928 @protocol identifier-list ;
9930 "@protocol identifier ;" should be resolved as "@protocol
9931 identifier-list ;": objc-methodprotolist may not start with a
9932 semicolon in the first alternative if objc-protocol-refs are
9933 omitted. */
9935 static void
9936 c_parser_objc_protocol_definition (c_parser *parser, tree attributes)
9938 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL));
9940 c_parser_consume_token (parser);
9941 if (c_parser_next_token_is_not (parser, CPP_NAME))
9943 c_parser_error (parser, "expected identifier");
9944 return;
9946 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
9947 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON)
9949 /* Any identifiers, including those declared as type names, are
9950 OK here. */
9951 while (true)
9953 tree id;
9954 if (c_parser_next_token_is_not (parser, CPP_NAME))
9956 c_parser_error (parser, "expected identifier");
9957 break;
9959 id = c_parser_peek_token (parser)->value;
9960 objc_declare_protocol (id, attributes);
9961 c_parser_consume_token (parser);
9962 if (c_parser_next_token_is (parser, CPP_COMMA))
9963 c_parser_consume_token (parser);
9964 else
9965 break;
9967 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
9969 else
9971 tree id = c_parser_peek_token (parser)->value;
9972 tree proto = NULL_TREE;
9973 c_parser_consume_token (parser);
9974 if (c_parser_next_token_is (parser, CPP_LESS))
9975 proto = c_parser_objc_protocol_refs (parser);
9976 parser->objc_pq_context = true;
9977 objc_start_protocol (id, proto, attributes);
9978 c_parser_objc_methodprotolist (parser);
9979 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>");
9980 parser->objc_pq_context = false;
9981 objc_finish_interface ();
9985 /* Parse an objc-method-type.
9987 objc-method-type:
9991 Return true if it is a class method (+) and false if it is
9992 an instance method (-).
9994 static inline bool
9995 c_parser_objc_method_type (c_parser *parser)
9997 switch (c_parser_peek_token (parser)->type)
9999 case CPP_PLUS:
10000 c_parser_consume_token (parser);
10001 return true;
10002 case CPP_MINUS:
10003 c_parser_consume_token (parser);
10004 return false;
10005 default:
10006 gcc_unreachable ();
10010 /* Parse an objc-method-definition.
10012 objc-method-definition:
10013 objc-method-type objc-method-decl ;[opt] compound-statement
10016 static void
10017 c_parser_objc_method_definition (c_parser *parser)
10019 bool is_class_method = c_parser_objc_method_type (parser);
10020 tree decl, attributes = NULL_TREE, expr = NULL_TREE;
10021 parser->objc_pq_context = true;
10022 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10023 &expr);
10024 if (decl == error_mark_node)
10025 return; /* Bail here. */
10027 if (c_parser_next_token_is (parser, CPP_SEMICOLON))
10029 c_parser_consume_token (parser);
10030 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10031 "extra semicolon in method definition specified");
10034 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10036 c_parser_error (parser, "expected %<{%>");
10037 return;
10040 parser->objc_pq_context = false;
10041 if (objc_start_method_definition (is_class_method, decl, attributes, expr))
10043 add_stmt (c_parser_compound_statement (parser));
10044 objc_finish_method_definition (current_function_decl);
10046 else
10048 /* This code is executed when we find a method definition
10049 outside of an @implementation context (or invalid for other
10050 reasons). Parse the method (to keep going) but do not emit
10051 any code.
10053 c_parser_compound_statement (parser);
10057 /* Parse an objc-methodprotolist.
10059 objc-methodprotolist:
10060 empty
10061 objc-methodprotolist objc-methodproto
10062 objc-methodprotolist declaration
10063 objc-methodprotolist ;
10064 @optional
10065 @required
10067 The declaration is a data definition, which may be missing
10068 declaration specifiers under the same rules and diagnostics as
10069 other data definitions outside functions, and the stray semicolon
10070 is diagnosed the same way as a stray semicolon outside a
10071 function. */
10073 static void
10074 c_parser_objc_methodprotolist (c_parser *parser)
10076 while (true)
10078 /* The list is terminated by @end. */
10079 switch (c_parser_peek_token (parser)->type)
10081 case CPP_SEMICOLON:
10082 pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic,
10083 "ISO C does not allow extra %<;%> outside of a function");
10084 c_parser_consume_token (parser);
10085 break;
10086 case CPP_PLUS:
10087 case CPP_MINUS:
10088 c_parser_objc_methodproto (parser);
10089 break;
10090 case CPP_PRAGMA:
10091 c_parser_pragma (parser, pragma_external, NULL);
10092 break;
10093 case CPP_EOF:
10094 return;
10095 default:
10096 if (c_parser_next_token_is_keyword (parser, RID_AT_END))
10097 return;
10098 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY))
10099 c_parser_objc_at_property_declaration (parser);
10100 else if (c_parser_next_token_is_keyword (parser, RID_AT_OPTIONAL))
10102 objc_set_method_opt (true);
10103 c_parser_consume_token (parser);
10105 else if (c_parser_next_token_is_keyword (parser, RID_AT_REQUIRED))
10107 objc_set_method_opt (false);
10108 c_parser_consume_token (parser);
10110 else
10111 c_parser_declaration_or_fndef (parser, false, false, true,
10112 false, true, NULL, vNULL);
10113 break;
10118 /* Parse an objc-methodproto.
10120 objc-methodproto:
10121 objc-method-type objc-method-decl ;
10124 static void
10125 c_parser_objc_methodproto (c_parser *parser)
10127 bool is_class_method = c_parser_objc_method_type (parser);
10128 tree decl, attributes = NULL_TREE;
10130 /* Remember protocol qualifiers in prototypes. */
10131 parser->objc_pq_context = true;
10132 decl = c_parser_objc_method_decl (parser, is_class_method, &attributes,
10133 NULL);
10134 /* Forget protocol qualifiers now. */
10135 parser->objc_pq_context = false;
10137 /* Do not allow the presence of attributes to hide an erroneous
10138 method implementation in the interface section. */
10139 if (!c_parser_next_token_is (parser, CPP_SEMICOLON))
10141 c_parser_error (parser, "expected %<;%>");
10142 return;
10145 if (decl != error_mark_node)
10146 objc_add_method_declaration (is_class_method, decl, attributes);
10148 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10151 /* If we are at a position that method attributes may be present, check that
10152 there are not any parsed already (a syntax error) and then collect any
10153 specified at the current location. Finally, if new attributes were present,
10154 check that the next token is legal ( ';' for decls and '{' for defs). */
10156 static bool
10157 c_parser_objc_maybe_method_attributes (c_parser* parser, tree* attributes)
10159 bool bad = false;
10160 if (*attributes)
10162 c_parser_error (parser,
10163 "method attributes must be specified at the end only");
10164 *attributes = NULL_TREE;
10165 bad = true;
10168 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10169 *attributes = c_parser_attributes (parser);
10171 /* If there were no attributes here, just report any earlier error. */
10172 if (*attributes == NULL_TREE || bad)
10173 return bad;
10175 /* If the attributes are followed by a ; or {, then just report any earlier
10176 error. */
10177 if (c_parser_next_token_is (parser, CPP_SEMICOLON)
10178 || c_parser_next_token_is (parser, CPP_OPEN_BRACE))
10179 return bad;
10181 /* We've got attributes, but not at the end. */
10182 c_parser_error (parser,
10183 "expected %<;%> or %<{%> after method attribute definition");
10184 return true;
10187 /* Parse an objc-method-decl.
10189 objc-method-decl:
10190 ( objc-type-name ) objc-selector
10191 objc-selector
10192 ( objc-type-name ) objc-keyword-selector objc-optparmlist
10193 objc-keyword-selector objc-optparmlist
10194 attributes
10196 objc-keyword-selector:
10197 objc-keyword-decl
10198 objc-keyword-selector objc-keyword-decl
10200 objc-keyword-decl:
10201 objc-selector : ( objc-type-name ) identifier
10202 objc-selector : identifier
10203 : ( objc-type-name ) identifier
10204 : identifier
10206 objc-optparmlist:
10207 objc-optparms objc-optellipsis
10209 objc-optparms:
10210 empty
10211 objc-opt-parms , parameter-declaration
10213 objc-optellipsis:
10214 empty
10215 , ...
10218 static tree
10219 c_parser_objc_method_decl (c_parser *parser, bool is_class_method,
10220 tree *attributes, tree *expr)
10222 tree type = NULL_TREE;
10223 tree sel;
10224 tree parms = NULL_TREE;
10225 bool ellipsis = false;
10226 bool attr_err = false;
10228 *attributes = NULL_TREE;
10229 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10231 matching_parens parens;
10232 parens.consume_open (parser);
10233 type = c_parser_objc_type_name (parser);
10234 parens.skip_until_found_close (parser);
10236 sel = c_parser_objc_selector (parser);
10237 /* If there is no selector, or a colon follows, we have an
10238 objc-keyword-selector. If there is a selector, and a colon does
10239 not follow, that selector ends the objc-method-decl. */
10240 if (!sel || c_parser_next_token_is (parser, CPP_COLON))
10242 tree tsel = sel;
10243 tree list = NULL_TREE;
10244 while (true)
10246 tree atype = NULL_TREE, id, keyworddecl;
10247 tree param_attr = NULL_TREE;
10248 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10249 break;
10250 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10252 c_parser_consume_token (parser);
10253 atype = c_parser_objc_type_name (parser);
10254 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
10255 "expected %<)%>");
10257 /* New ObjC allows attributes on method parameters. */
10258 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
10259 param_attr = c_parser_attributes (parser);
10260 if (c_parser_next_token_is_not (parser, CPP_NAME))
10262 c_parser_error (parser, "expected identifier");
10263 return error_mark_node;
10265 id = c_parser_peek_token (parser)->value;
10266 c_parser_consume_token (parser);
10267 keyworddecl = objc_build_keyword_decl (tsel, atype, id, param_attr);
10268 list = chainon (list, keyworddecl);
10269 tsel = c_parser_objc_selector (parser);
10270 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON))
10271 break;
10274 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10276 /* Parse the optional parameter list. Optional Objective-C
10277 method parameters follow the C syntax, and may include '...'
10278 to denote a variable number of arguments. */
10279 parms = make_node (TREE_LIST);
10280 while (c_parser_next_token_is (parser, CPP_COMMA))
10282 struct c_parm *parm;
10283 c_parser_consume_token (parser);
10284 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10286 ellipsis = true;
10287 c_parser_consume_token (parser);
10288 attr_err |= c_parser_objc_maybe_method_attributes
10289 (parser, attributes) ;
10290 break;
10292 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10293 if (parm == NULL)
10294 break;
10295 parms = chainon (parms,
10296 build_tree_list (NULL_TREE, grokparm (parm, expr)));
10298 sel = list;
10300 else
10301 attr_err |= c_parser_objc_maybe_method_attributes (parser, attributes) ;
10303 if (sel == NULL)
10305 c_parser_error (parser, "objective-c method declaration is expected");
10306 return error_mark_node;
10309 if (attr_err)
10310 return error_mark_node;
10312 return objc_build_method_signature (is_class_method, type, sel, parms, ellipsis);
10315 /* Parse an objc-type-name.
10317 objc-type-name:
10318 objc-type-qualifiers[opt] type-name
10319 objc-type-qualifiers[opt]
10321 objc-type-qualifiers:
10322 objc-type-qualifier
10323 objc-type-qualifiers objc-type-qualifier
10325 objc-type-qualifier: one of
10326 in out inout bycopy byref oneway
10329 static tree
10330 c_parser_objc_type_name (c_parser *parser)
10332 tree quals = NULL_TREE;
10333 struct c_type_name *type_name = NULL;
10334 tree type = NULL_TREE;
10335 while (true)
10337 c_token *token = c_parser_peek_token (parser);
10338 if (token->type == CPP_KEYWORD
10339 && (token->keyword == RID_IN
10340 || token->keyword == RID_OUT
10341 || token->keyword == RID_INOUT
10342 || token->keyword == RID_BYCOPY
10343 || token->keyword == RID_BYREF
10344 || token->keyword == RID_ONEWAY))
10346 quals = chainon (build_tree_list (NULL_TREE, token->value), quals);
10347 c_parser_consume_token (parser);
10349 else
10350 break;
10352 if (c_parser_next_tokens_start_typename (parser, cla_prefer_type))
10353 type_name = c_parser_type_name (parser);
10354 if (type_name)
10355 type = groktypename (type_name, NULL, NULL);
10357 /* If the type is unknown, and error has already been produced and
10358 we need to recover from the error. In that case, use NULL_TREE
10359 for the type, as if no type had been specified; this will use the
10360 default type ('id') which is good for error recovery. */
10361 if (type == error_mark_node)
10362 type = NULL_TREE;
10364 return build_tree_list (quals, type);
10367 /* Parse objc-protocol-refs.
10369 objc-protocol-refs:
10370 < identifier-list >
10373 static tree
10374 c_parser_objc_protocol_refs (c_parser *parser)
10376 tree list = NULL_TREE;
10377 gcc_assert (c_parser_next_token_is (parser, CPP_LESS));
10378 c_parser_consume_token (parser);
10379 /* Any identifiers, including those declared as type names, are OK
10380 here. */
10381 while (true)
10383 tree id;
10384 if (c_parser_next_token_is_not (parser, CPP_NAME))
10386 c_parser_error (parser, "expected identifier");
10387 break;
10389 id = c_parser_peek_token (parser)->value;
10390 list = chainon (list, build_tree_list (NULL_TREE, id));
10391 c_parser_consume_token (parser);
10392 if (c_parser_next_token_is (parser, CPP_COMMA))
10393 c_parser_consume_token (parser);
10394 else
10395 break;
10397 c_parser_require (parser, CPP_GREATER, "expected %<>%>");
10398 return list;
10401 /* Parse an objc-try-catch-finally-statement.
10403 objc-try-catch-finally-statement:
10404 @try compound-statement objc-catch-list[opt]
10405 @try compound-statement objc-catch-list[opt] @finally compound-statement
10407 objc-catch-list:
10408 @catch ( objc-catch-parameter-declaration ) compound-statement
10409 objc-catch-list @catch ( objc-catch-parameter-declaration ) compound-statement
10411 objc-catch-parameter-declaration:
10412 parameter-declaration
10413 '...'
10415 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
10417 PS: This function is identical to cp_parser_objc_try_catch_finally_statement
10418 for C++. Keep them in sync. */
10420 static void
10421 c_parser_objc_try_catch_finally_statement (c_parser *parser)
10423 location_t location;
10424 tree stmt;
10426 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY));
10427 c_parser_consume_token (parser);
10428 location = c_parser_peek_token (parser)->location;
10429 objc_maybe_warn_exceptions (location);
10430 stmt = c_parser_compound_statement (parser);
10431 objc_begin_try_stmt (location, stmt);
10433 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH))
10435 struct c_parm *parm;
10436 tree parameter_declaration = error_mark_node;
10437 bool seen_open_paren = false;
10439 c_parser_consume_token (parser);
10440 matching_parens parens;
10441 if (!parens.require_open (parser))
10442 seen_open_paren = true;
10443 if (c_parser_next_token_is (parser, CPP_ELLIPSIS))
10445 /* We have "@catch (...)" (where the '...' are literally
10446 what is in the code). Skip the '...'.
10447 parameter_declaration is set to NULL_TREE, and
10448 objc_being_catch_clauses() knows that that means
10449 '...'. */
10450 c_parser_consume_token (parser);
10451 parameter_declaration = NULL_TREE;
10453 else
10455 /* We have "@catch (NSException *exception)" or something
10456 like that. Parse the parameter declaration. */
10457 parm = c_parser_parameter_declaration (parser, NULL_TREE);
10458 if (parm == NULL)
10459 parameter_declaration = error_mark_node;
10460 else
10461 parameter_declaration = grokparm (parm, NULL);
10463 if (seen_open_paren)
10464 parens.require_close (parser);
10465 else
10467 /* If there was no open parenthesis, we are recovering from
10468 an error, and we are trying to figure out what mistake
10469 the user has made. */
10471 /* If there is an immediate closing parenthesis, the user
10472 probably forgot the opening one (ie, they typed "@catch
10473 NSException *e)". Parse the closing parenthesis and keep
10474 going. */
10475 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN))
10476 c_parser_consume_token (parser);
10478 /* If these is no immediate closing parenthesis, the user
10479 probably doesn't know that parenthesis are required at
10480 all (ie, they typed "@catch NSException *e"). So, just
10481 forget about the closing parenthesis and keep going. */
10483 objc_begin_catch_clause (parameter_declaration);
10484 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
10485 c_parser_compound_statement_nostart (parser);
10486 objc_finish_catch_clause ();
10488 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY))
10490 c_parser_consume_token (parser);
10491 location = c_parser_peek_token (parser)->location;
10492 stmt = c_parser_compound_statement (parser);
10493 objc_build_finally_clause (location, stmt);
10495 objc_finish_try_stmt ();
10498 /* Parse an objc-synchronized-statement.
10500 objc-synchronized-statement:
10501 @synchronized ( expression ) compound-statement
10504 static void
10505 c_parser_objc_synchronized_statement (c_parser *parser)
10507 location_t loc;
10508 tree expr, stmt;
10509 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED));
10510 c_parser_consume_token (parser);
10511 loc = c_parser_peek_token (parser)->location;
10512 objc_maybe_warn_exceptions (loc);
10513 matching_parens parens;
10514 if (parens.require_open (parser))
10516 struct c_expr ce = c_parser_expression (parser);
10517 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10518 expr = ce.value;
10519 expr = c_fully_fold (expr, false, NULL);
10520 parens.skip_until_found_close (parser);
10522 else
10523 expr = error_mark_node;
10524 stmt = c_parser_compound_statement (parser);
10525 objc_build_synchronized (loc, expr, stmt);
10528 /* Parse an objc-selector; return NULL_TREE without an error if the
10529 next token is not an objc-selector.
10531 objc-selector:
10532 identifier
10533 one of
10534 enum struct union if else while do for switch case default
10535 break continue return goto asm sizeof typeof __alignof
10536 unsigned long const short volatile signed restrict _Complex
10537 in out inout bycopy byref oneway int char float double void _Bool
10538 _Atomic
10540 ??? Why this selection of keywords but not, for example, storage
10541 class specifiers? */
10543 static tree
10544 c_parser_objc_selector (c_parser *parser)
10546 c_token *token = c_parser_peek_token (parser);
10547 tree value = token->value;
10548 if (token->type == CPP_NAME)
10550 c_parser_consume_token (parser);
10551 return value;
10553 if (token->type != CPP_KEYWORD)
10554 return NULL_TREE;
10555 switch (token->keyword)
10557 case RID_ENUM:
10558 case RID_STRUCT:
10559 case RID_UNION:
10560 case RID_IF:
10561 case RID_ELSE:
10562 case RID_WHILE:
10563 case RID_DO:
10564 case RID_FOR:
10565 case RID_SWITCH:
10566 case RID_CASE:
10567 case RID_DEFAULT:
10568 case RID_BREAK:
10569 case RID_CONTINUE:
10570 case RID_RETURN:
10571 case RID_GOTO:
10572 case RID_ASM:
10573 case RID_SIZEOF:
10574 case RID_TYPEOF:
10575 case RID_ALIGNOF:
10576 case RID_UNSIGNED:
10577 case RID_LONG:
10578 case RID_CONST:
10579 case RID_SHORT:
10580 case RID_VOLATILE:
10581 case RID_SIGNED:
10582 case RID_RESTRICT:
10583 case RID_COMPLEX:
10584 case RID_IN:
10585 case RID_OUT:
10586 case RID_INOUT:
10587 case RID_BYCOPY:
10588 case RID_BYREF:
10589 case RID_ONEWAY:
10590 case RID_INT:
10591 case RID_CHAR:
10592 case RID_FLOAT:
10593 case RID_DOUBLE:
10594 CASE_RID_FLOATN_NX:
10595 case RID_VOID:
10596 case RID_BOOL:
10597 case RID_ATOMIC:
10598 case RID_AUTO_TYPE:
10599 case RID_INT_N_0:
10600 case RID_INT_N_1:
10601 case RID_INT_N_2:
10602 case RID_INT_N_3:
10603 c_parser_consume_token (parser);
10604 return value;
10605 default:
10606 return NULL_TREE;
10610 /* Parse an objc-selector-arg.
10612 objc-selector-arg:
10613 objc-selector
10614 objc-keywordname-list
10616 objc-keywordname-list:
10617 objc-keywordname
10618 objc-keywordname-list objc-keywordname
10620 objc-keywordname:
10621 objc-selector :
10625 static tree
10626 c_parser_objc_selector_arg (c_parser *parser)
10628 tree sel = c_parser_objc_selector (parser);
10629 tree list = NULL_TREE;
10630 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10631 return sel;
10632 while (true)
10634 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10635 return list;
10636 list = chainon (list, build_tree_list (sel, NULL_TREE));
10637 sel = c_parser_objc_selector (parser);
10638 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10639 break;
10641 return list;
10644 /* Parse an objc-receiver.
10646 objc-receiver:
10647 expression
10648 class-name
10649 type-name
10652 static tree
10653 c_parser_objc_receiver (c_parser *parser)
10655 location_t loc = c_parser_peek_token (parser)->location;
10657 if (c_parser_peek_token (parser)->type == CPP_NAME
10658 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME
10659 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME))
10661 tree id = c_parser_peek_token (parser)->value;
10662 c_parser_consume_token (parser);
10663 return objc_get_class_reference (id);
10665 struct c_expr ce = c_parser_expression (parser);
10666 ce = convert_lvalue_to_rvalue (loc, ce, false, false);
10667 return c_fully_fold (ce.value, false, NULL);
10670 /* Parse objc-message-args.
10672 objc-message-args:
10673 objc-selector
10674 objc-keywordarg-list
10676 objc-keywordarg-list:
10677 objc-keywordarg
10678 objc-keywordarg-list objc-keywordarg
10680 objc-keywordarg:
10681 objc-selector : objc-keywordexpr
10682 : objc-keywordexpr
10685 static tree
10686 c_parser_objc_message_args (c_parser *parser)
10688 tree sel = c_parser_objc_selector (parser);
10689 tree list = NULL_TREE;
10690 if (sel && c_parser_next_token_is_not (parser, CPP_COLON))
10691 return sel;
10692 while (true)
10694 tree keywordexpr;
10695 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
10696 return error_mark_node;
10697 keywordexpr = c_parser_objc_keywordexpr (parser);
10698 list = chainon (list, build_tree_list (sel, keywordexpr));
10699 sel = c_parser_objc_selector (parser);
10700 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON))
10701 break;
10703 return list;
10706 /* Parse an objc-keywordexpr.
10708 objc-keywordexpr:
10709 nonempty-expr-list
10712 static tree
10713 c_parser_objc_keywordexpr (c_parser *parser)
10715 tree ret;
10716 vec<tree, va_gc> *expr_list = c_parser_expr_list (parser, true, true,
10717 NULL, NULL, NULL, NULL);
10718 if (vec_safe_length (expr_list) == 1)
10720 /* Just return the expression, remove a level of
10721 indirection. */
10722 ret = (*expr_list)[0];
10724 else
10726 /* We have a comma expression, we will collapse later. */
10727 ret = build_tree_list_vec (expr_list);
10729 release_tree_vector (expr_list);
10730 return ret;
10733 /* A check, needed in several places, that ObjC interface, implementation or
10734 method definitions are not prefixed by incorrect items. */
10735 static bool
10736 c_parser_objc_diagnose_bad_element_prefix (c_parser *parser,
10737 struct c_declspecs *specs)
10739 if (!specs->declspecs_seen_p || specs->non_sc_seen_p
10740 || specs->typespec_kind != ctsk_none)
10742 c_parser_error (parser,
10743 "no type or storage class may be specified here,");
10744 c_parser_skip_to_end_of_block_or_statement (parser);
10745 return true;
10747 return false;
10750 /* Parse an Objective-C @property declaration. The syntax is:
10752 objc-property-declaration:
10753 '@property' objc-property-attributes[opt] struct-declaration ;
10755 objc-property-attributes:
10756 '(' objc-property-attribute-list ')'
10758 objc-property-attribute-list:
10759 objc-property-attribute
10760 objc-property-attribute-list, objc-property-attribute
10762 objc-property-attribute
10763 'getter' = identifier
10764 'setter' = identifier
10765 'readonly'
10766 'readwrite'
10767 'assign'
10768 'retain'
10769 'copy'
10770 'nonatomic'
10772 For example:
10773 @property NSString *name;
10774 @property (readonly) id object;
10775 @property (retain, nonatomic, getter=getTheName) id name;
10776 @property int a, b, c;
10778 PS: This function is identical to cp_parser_objc_at_propery_declaration
10779 for C++. Keep them in sync. */
10780 static void
10781 c_parser_objc_at_property_declaration (c_parser *parser)
10783 /* The following variables hold the attributes of the properties as
10784 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
10785 seen. When we see an attribute, we set them to 'true' (if they
10786 are boolean properties) or to the identifier (if they have an
10787 argument, ie, for getter and setter). Note that here we only
10788 parse the list of attributes, check the syntax and accumulate the
10789 attributes that we find. objc_add_property_declaration() will
10790 then process the information. */
10791 bool property_assign = false;
10792 bool property_copy = false;
10793 tree property_getter_ident = NULL_TREE;
10794 bool property_nonatomic = false;
10795 bool property_readonly = false;
10796 bool property_readwrite = false;
10797 bool property_retain = false;
10798 tree property_setter_ident = NULL_TREE;
10800 /* 'properties' is the list of properties that we read. Usually a
10801 single one, but maybe more (eg, in "@property int a, b, c;" there
10802 are three). */
10803 tree properties;
10804 location_t loc;
10806 loc = c_parser_peek_token (parser)->location;
10807 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROPERTY));
10809 c_parser_consume_token (parser); /* Eat '@property'. */
10811 /* Parse the optional attribute list... */
10812 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
10814 matching_parens parens;
10816 /* Eat the '(' */
10817 parens.consume_open (parser);
10819 /* Property attribute keywords are valid now. */
10820 parser->objc_property_attr_context = true;
10822 while (true)
10824 bool syntax_error = false;
10825 c_token *token = c_parser_peek_token (parser);
10826 enum rid keyword;
10828 if (token->type != CPP_KEYWORD)
10830 if (token->type == CPP_CLOSE_PAREN)
10831 c_parser_error (parser, "expected identifier");
10832 else
10834 c_parser_consume_token (parser);
10835 c_parser_error (parser, "unknown property attribute");
10837 break;
10839 keyword = token->keyword;
10840 c_parser_consume_token (parser);
10841 switch (keyword)
10843 case RID_ASSIGN: property_assign = true; break;
10844 case RID_COPY: property_copy = true; break;
10845 case RID_NONATOMIC: property_nonatomic = true; break;
10846 case RID_READONLY: property_readonly = true; break;
10847 case RID_READWRITE: property_readwrite = true; break;
10848 case RID_RETAIN: property_retain = true; break;
10850 case RID_GETTER:
10851 case RID_SETTER:
10852 if (c_parser_next_token_is_not (parser, CPP_EQ))
10854 if (keyword == RID_GETTER)
10855 c_parser_error (parser,
10856 "missing %<=%> (after %<getter%> attribute)");
10857 else
10858 c_parser_error (parser,
10859 "missing %<=%> (after %<setter%> attribute)");
10860 syntax_error = true;
10861 break;
10863 c_parser_consume_token (parser); /* eat the = */
10864 if (c_parser_next_token_is_not (parser, CPP_NAME))
10866 c_parser_error (parser, "expected identifier");
10867 syntax_error = true;
10868 break;
10870 if (keyword == RID_SETTER)
10872 if (property_setter_ident != NULL_TREE)
10873 c_parser_error (parser, "the %<setter%> attribute may only be specified once");
10874 else
10875 property_setter_ident = c_parser_peek_token (parser)->value;
10876 c_parser_consume_token (parser);
10877 if (c_parser_next_token_is_not (parser, CPP_COLON))
10878 c_parser_error (parser, "setter name must terminate with %<:%>");
10879 else
10880 c_parser_consume_token (parser);
10882 else
10884 if (property_getter_ident != NULL_TREE)
10885 c_parser_error (parser, "the %<getter%> attribute may only be specified once");
10886 else
10887 property_getter_ident = c_parser_peek_token (parser)->value;
10888 c_parser_consume_token (parser);
10890 break;
10891 default:
10892 c_parser_error (parser, "unknown property attribute");
10893 syntax_error = true;
10894 break;
10897 if (syntax_error)
10898 break;
10900 if (c_parser_next_token_is (parser, CPP_COMMA))
10901 c_parser_consume_token (parser);
10902 else
10903 break;
10905 parser->objc_property_attr_context = false;
10906 parens.skip_until_found_close (parser);
10908 /* ... and the property declaration(s). */
10909 properties = c_parser_struct_declaration (parser);
10911 if (properties == error_mark_node)
10913 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10914 parser->error = false;
10915 return;
10918 if (properties == NULL_TREE)
10919 c_parser_error (parser, "expected identifier");
10920 else
10922 /* Comma-separated properties are chained together in
10923 reverse order; add them one by one. */
10924 properties = nreverse (properties);
10926 for (; properties; properties = TREE_CHAIN (properties))
10927 objc_add_property_declaration (loc, copy_node (properties),
10928 property_readonly, property_readwrite,
10929 property_assign, property_retain,
10930 property_copy, property_nonatomic,
10931 property_getter_ident, property_setter_ident);
10934 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
10935 parser->error = false;
10938 /* Parse an Objective-C @synthesize declaration. The syntax is:
10940 objc-synthesize-declaration:
10941 @synthesize objc-synthesize-identifier-list ;
10943 objc-synthesize-identifier-list:
10944 objc-synthesize-identifier
10945 objc-synthesize-identifier-list, objc-synthesize-identifier
10947 objc-synthesize-identifier
10948 identifier
10949 identifier = identifier
10951 For example:
10952 @synthesize MyProperty;
10953 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
10955 PS: This function is identical to cp_parser_objc_at_synthesize_declaration
10956 for C++. Keep them in sync.
10958 static void
10959 c_parser_objc_at_synthesize_declaration (c_parser *parser)
10961 tree list = NULL_TREE;
10962 location_t loc;
10963 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNTHESIZE));
10964 loc = c_parser_peek_token (parser)->location;
10966 c_parser_consume_token (parser);
10967 while (true)
10969 tree property, ivar;
10970 if (c_parser_next_token_is_not (parser, CPP_NAME))
10972 c_parser_error (parser, "expected identifier");
10973 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10974 /* Once we find the semicolon, we can resume normal parsing.
10975 We have to reset parser->error manually because
10976 c_parser_skip_until_found() won't reset it for us if the
10977 next token is precisely a semicolon. */
10978 parser->error = false;
10979 return;
10981 property = c_parser_peek_token (parser)->value;
10982 c_parser_consume_token (parser);
10983 if (c_parser_next_token_is (parser, CPP_EQ))
10985 c_parser_consume_token (parser);
10986 if (c_parser_next_token_is_not (parser, CPP_NAME))
10988 c_parser_error (parser, "expected identifier");
10989 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
10990 parser->error = false;
10991 return;
10993 ivar = c_parser_peek_token (parser)->value;
10994 c_parser_consume_token (parser);
10996 else
10997 ivar = NULL_TREE;
10998 list = chainon (list, build_tree_list (ivar, property));
10999 if (c_parser_next_token_is (parser, CPP_COMMA))
11000 c_parser_consume_token (parser);
11001 else
11002 break;
11004 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11005 objc_add_synthesize_declaration (loc, list);
11008 /* Parse an Objective-C @dynamic declaration. The syntax is:
11010 objc-dynamic-declaration:
11011 @dynamic identifier-list ;
11013 For example:
11014 @dynamic MyProperty;
11015 @dynamic MyProperty, AnotherProperty;
11017 PS: This function is identical to cp_parser_objc_at_dynamic_declaration
11018 for C++. Keep them in sync.
11020 static void
11021 c_parser_objc_at_dynamic_declaration (c_parser *parser)
11023 tree list = NULL_TREE;
11024 location_t loc;
11025 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_DYNAMIC));
11026 loc = c_parser_peek_token (parser)->location;
11028 c_parser_consume_token (parser);
11029 while (true)
11031 tree property;
11032 if (c_parser_next_token_is_not (parser, CPP_NAME))
11034 c_parser_error (parser, "expected identifier");
11035 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL);
11036 parser->error = false;
11037 return;
11039 property = c_parser_peek_token (parser)->value;
11040 list = chainon (list, build_tree_list (NULL_TREE, property));
11041 c_parser_consume_token (parser);
11042 if (c_parser_next_token_is (parser, CPP_COMMA))
11043 c_parser_consume_token (parser);
11044 else
11045 break;
11047 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
11048 objc_add_dynamic_declaration (loc, list);
11052 /* Parse a pragma GCC ivdep. */
11054 static bool
11055 c_parse_pragma_ivdep (c_parser *parser)
11057 c_parser_consume_pragma (parser);
11058 c_parser_skip_to_pragma_eol (parser);
11059 return true;
11062 /* Parse a pragma GCC unroll. */
11064 static unsigned short
11065 c_parser_pragma_unroll (c_parser *parser)
11067 unsigned short unroll;
11068 c_parser_consume_pragma (parser);
11069 location_t location = c_parser_peek_token (parser)->location;
11070 tree expr = c_parser_expr_no_commas (parser, NULL).value;
11071 mark_exp_read (expr);
11072 expr = c_fully_fold (expr, false, NULL);
11073 HOST_WIDE_INT lunroll = 0;
11074 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
11075 || TREE_CODE (expr) != INTEGER_CST
11076 || (lunroll = tree_to_shwi (expr)) < 0
11077 || lunroll >= USHRT_MAX)
11079 error_at (location, "%<#pragma GCC unroll%> requires an"
11080 " assignment-expression that evaluates to a non-negative"
11081 " integral constant less than %u", USHRT_MAX);
11082 unroll = 0;
11084 else
11086 unroll = (unsigned short)lunroll;
11087 if (unroll == 0)
11088 unroll = 1;
11091 c_parser_skip_to_pragma_eol (parser);
11092 return unroll;
11095 /* Handle pragmas. Some OpenMP pragmas are associated with, and therefore
11096 should be considered, statements. ALLOW_STMT is true if we're within
11097 the context of a function and such pragmas are to be allowed. Returns
11098 true if we actually parsed such a pragma. */
11100 static bool
11101 c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
11103 unsigned int id;
11104 const char *construct = NULL;
11106 id = c_parser_peek_token (parser)->pragma_kind;
11107 gcc_assert (id != PRAGMA_NONE);
11109 switch (id)
11111 case PRAGMA_OACC_DECLARE:
11112 c_parser_oacc_declare (parser);
11113 return false;
11115 case PRAGMA_OACC_ENTER_DATA:
11116 if (context != pragma_compound)
11118 construct = "acc enter data";
11119 in_compound:
11120 if (context == pragma_stmt)
11122 error_at (c_parser_peek_token (parser)->location,
11123 "%<#pragma %s%> may only be used in compound "
11124 "statements", construct);
11125 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11126 return false;
11128 goto bad_stmt;
11130 c_parser_oacc_enter_exit_data (parser, true);
11131 return false;
11133 case PRAGMA_OACC_EXIT_DATA:
11134 if (context != pragma_compound)
11136 construct = "acc exit data";
11137 goto in_compound;
11139 c_parser_oacc_enter_exit_data (parser, false);
11140 return false;
11142 case PRAGMA_OACC_ROUTINE:
11143 if (context != pragma_external)
11145 error_at (c_parser_peek_token (parser)->location,
11146 "%<#pragma acc routine%> must be at file scope");
11147 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11148 return false;
11150 c_parser_oacc_routine (parser, context);
11151 return false;
11153 case PRAGMA_OACC_UPDATE:
11154 if (context != pragma_compound)
11156 construct = "acc update";
11157 goto in_compound;
11159 c_parser_oacc_update (parser);
11160 return false;
11162 case PRAGMA_OMP_BARRIER:
11163 if (context != pragma_compound)
11165 construct = "omp barrier";
11166 goto in_compound;
11168 c_parser_omp_barrier (parser);
11169 return false;
11171 case PRAGMA_OMP_FLUSH:
11172 if (context != pragma_compound)
11174 construct = "omp flush";
11175 goto in_compound;
11177 c_parser_omp_flush (parser);
11178 return false;
11180 case PRAGMA_OMP_TASKWAIT:
11181 if (context != pragma_compound)
11183 construct = "omp taskwait";
11184 goto in_compound;
11186 c_parser_omp_taskwait (parser);
11187 return false;
11189 case PRAGMA_OMP_TASKYIELD:
11190 if (context != pragma_compound)
11192 construct = "omp taskyield";
11193 goto in_compound;
11195 c_parser_omp_taskyield (parser);
11196 return false;
11198 case PRAGMA_OMP_CANCEL:
11199 if (context != pragma_compound)
11201 construct = "omp cancel";
11202 goto in_compound;
11204 c_parser_omp_cancel (parser);
11205 return false;
11207 case PRAGMA_OMP_CANCELLATION_POINT:
11208 c_parser_omp_cancellation_point (parser, context);
11209 return false;
11211 case PRAGMA_OMP_THREADPRIVATE:
11212 c_parser_omp_threadprivate (parser);
11213 return false;
11215 case PRAGMA_OMP_TARGET:
11216 return c_parser_omp_target (parser, context, if_p);
11218 case PRAGMA_OMP_END_DECLARE_TARGET:
11219 c_parser_omp_end_declare_target (parser);
11220 return false;
11222 case PRAGMA_OMP_SECTION:
11223 error_at (c_parser_peek_token (parser)->location,
11224 "%<#pragma omp section%> may only be used in "
11225 "%<#pragma omp sections%> construct");
11226 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11227 return false;
11229 case PRAGMA_OMP_DECLARE:
11230 c_parser_omp_declare (parser, context);
11231 return false;
11233 case PRAGMA_OMP_ORDERED:
11234 return c_parser_omp_ordered (parser, context, if_p);
11236 case PRAGMA_IVDEP:
11238 const bool ivdep = c_parse_pragma_ivdep (parser);
11239 unsigned short unroll;
11240 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_UNROLL)
11241 unroll = c_parser_pragma_unroll (parser);
11242 else
11243 unroll = 0;
11244 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11245 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11246 && !c_parser_next_token_is_keyword (parser, RID_DO))
11248 c_parser_error (parser, "for, while or do statement expected");
11249 return false;
11251 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11252 c_parser_for_statement (parser, ivdep, unroll, if_p);
11253 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11254 c_parser_while_statement (parser, ivdep, unroll, if_p);
11255 else
11256 c_parser_do_statement (parser, ivdep, unroll);
11258 return false;
11260 case PRAGMA_UNROLL:
11262 unsigned short unroll = c_parser_pragma_unroll (parser);
11263 bool ivdep;
11264 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_IVDEP)
11265 ivdep = c_parse_pragma_ivdep (parser);
11266 else
11267 ivdep = false;
11268 if (!c_parser_next_token_is_keyword (parser, RID_FOR)
11269 && !c_parser_next_token_is_keyword (parser, RID_WHILE)
11270 && !c_parser_next_token_is_keyword (parser, RID_DO))
11272 c_parser_error (parser, "for, while or do statement expected");
11273 return false;
11275 if (c_parser_next_token_is_keyword (parser, RID_FOR))
11276 c_parser_for_statement (parser, ivdep, unroll, if_p);
11277 else if (c_parser_next_token_is_keyword (parser, RID_WHILE))
11278 c_parser_while_statement (parser, ivdep, unroll, if_p);
11279 else
11280 c_parser_do_statement (parser, ivdep, unroll);
11282 return false;
11284 case PRAGMA_GCC_PCH_PREPROCESS:
11285 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first");
11286 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11287 return false;
11289 case PRAGMA_OACC_WAIT:
11290 if (context != pragma_compound)
11292 construct = "acc wait";
11293 goto in_compound;
11295 /* FALL THROUGH. */
11297 default:
11298 if (id < PRAGMA_FIRST_EXTERNAL)
11300 if (context != pragma_stmt && context != pragma_compound)
11302 bad_stmt:
11303 c_parser_error (parser, "expected declaration specifiers");
11304 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
11305 return false;
11307 c_parser_omp_construct (parser, if_p);
11308 return true;
11310 break;
11313 c_parser_consume_pragma (parser);
11314 c_invoke_pragma_handler (id);
11316 /* Skip to EOL, but suppress any error message. Those will have been
11317 generated by the handler routine through calling error, as opposed
11318 to calling c_parser_error. */
11319 parser->error = true;
11320 c_parser_skip_to_pragma_eol (parser);
11322 return false;
11325 /* The interface the pragma parsers have to the lexer. */
11327 enum cpp_ttype
11328 pragma_lex (tree *value, location_t *loc)
11330 c_token *tok = c_parser_peek_token (the_parser);
11331 enum cpp_ttype ret = tok->type;
11333 *value = tok->value;
11334 if (loc)
11335 *loc = tok->location;
11337 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
11338 ret = CPP_EOF;
11339 else
11341 if (ret == CPP_KEYWORD)
11342 ret = CPP_NAME;
11343 c_parser_consume_token (the_parser);
11346 return ret;
11349 static void
11350 c_parser_pragma_pch_preprocess (c_parser *parser)
11352 tree name = NULL;
11354 c_parser_consume_pragma (parser);
11355 if (c_parser_next_token_is (parser, CPP_STRING))
11357 name = c_parser_peek_token (parser)->value;
11358 c_parser_consume_token (parser);
11360 else
11361 c_parser_error (parser, "expected string literal");
11362 c_parser_skip_to_pragma_eol (parser);
11364 if (name)
11365 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
11368 /* OpenACC and OpenMP parsing routines. */
11370 /* Returns name of the next clause.
11371 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
11372 the token is not consumed. Otherwise appropriate pragma_omp_clause is
11373 returned and the token is consumed. */
11375 static pragma_omp_clause
11376 c_parser_omp_clause_name (c_parser *parser)
11378 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
11380 if (c_parser_next_token_is_keyword (parser, RID_AUTO))
11381 result = PRAGMA_OACC_CLAUSE_AUTO;
11382 else if (c_parser_next_token_is_keyword (parser, RID_IF))
11383 result = PRAGMA_OMP_CLAUSE_IF;
11384 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT))
11385 result = PRAGMA_OMP_CLAUSE_DEFAULT;
11386 else if (c_parser_next_token_is_keyword (parser, RID_FOR))
11387 result = PRAGMA_OMP_CLAUSE_FOR;
11388 else if (c_parser_next_token_is (parser, CPP_NAME))
11390 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11392 switch (p[0])
11394 case 'a':
11395 if (!strcmp ("aligned", p))
11396 result = PRAGMA_OMP_CLAUSE_ALIGNED;
11397 else if (!strcmp ("async", p))
11398 result = PRAGMA_OACC_CLAUSE_ASYNC;
11399 break;
11400 case 'c':
11401 if (!strcmp ("collapse", p))
11402 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
11403 else if (!strcmp ("copy", p))
11404 result = PRAGMA_OACC_CLAUSE_COPY;
11405 else if (!strcmp ("copyin", p))
11406 result = PRAGMA_OMP_CLAUSE_COPYIN;
11407 else if (!strcmp ("copyout", p))
11408 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11409 else if (!strcmp ("copyprivate", p))
11410 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
11411 else if (!strcmp ("create", p))
11412 result = PRAGMA_OACC_CLAUSE_CREATE;
11413 break;
11414 case 'd':
11415 if (!strcmp ("defaultmap", p))
11416 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
11417 else if (!strcmp ("delete", p))
11418 result = PRAGMA_OACC_CLAUSE_DELETE;
11419 else if (!strcmp ("depend", p))
11420 result = PRAGMA_OMP_CLAUSE_DEPEND;
11421 else if (!strcmp ("device", p))
11422 result = PRAGMA_OMP_CLAUSE_DEVICE;
11423 else if (!strcmp ("deviceptr", p))
11424 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
11425 else if (!strcmp ("device_resident", p))
11426 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
11427 else if (!strcmp ("dist_schedule", p))
11428 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
11429 break;
11430 case 'f':
11431 if (!strcmp ("final", p))
11432 result = PRAGMA_OMP_CLAUSE_FINAL;
11433 else if (!strcmp ("finalize", p))
11434 result = PRAGMA_OACC_CLAUSE_FINALIZE;
11435 else if (!strcmp ("firstprivate", p))
11436 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
11437 else if (!strcmp ("from", p))
11438 result = PRAGMA_OMP_CLAUSE_FROM;
11439 break;
11440 case 'g':
11441 if (!strcmp ("gang", p))
11442 result = PRAGMA_OACC_CLAUSE_GANG;
11443 else if (!strcmp ("grainsize", p))
11444 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
11445 break;
11446 case 'h':
11447 if (!strcmp ("hint", p))
11448 result = PRAGMA_OMP_CLAUSE_HINT;
11449 else if (!strcmp ("host", p))
11450 result = PRAGMA_OACC_CLAUSE_HOST;
11451 break;
11452 case 'i':
11453 if (!strcmp ("if_present", p))
11454 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
11455 else if (!strcmp ("inbranch", p))
11456 result = PRAGMA_OMP_CLAUSE_INBRANCH;
11457 else if (!strcmp ("independent", p))
11458 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
11459 else if (!strcmp ("is_device_ptr", p))
11460 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
11461 break;
11462 case 'l':
11463 if (!strcmp ("lastprivate", p))
11464 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
11465 else if (!strcmp ("linear", p))
11466 result = PRAGMA_OMP_CLAUSE_LINEAR;
11467 else if (!strcmp ("link", p))
11468 result = PRAGMA_OMP_CLAUSE_LINK;
11469 break;
11470 case 'm':
11471 if (!strcmp ("map", p))
11472 result = PRAGMA_OMP_CLAUSE_MAP;
11473 else if (!strcmp ("mergeable", p))
11474 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
11475 break;
11476 case 'n':
11477 if (!strcmp ("nogroup", p))
11478 result = PRAGMA_OMP_CLAUSE_NOGROUP;
11479 else if (!strcmp ("notinbranch", p))
11480 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
11481 else if (!strcmp ("nowait", p))
11482 result = PRAGMA_OMP_CLAUSE_NOWAIT;
11483 else if (!strcmp ("num_gangs", p))
11484 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
11485 else if (!strcmp ("num_tasks", p))
11486 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
11487 else if (!strcmp ("num_teams", p))
11488 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
11489 else if (!strcmp ("num_threads", p))
11490 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
11491 else if (!strcmp ("num_workers", p))
11492 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
11493 break;
11494 case 'o':
11495 if (!strcmp ("ordered", p))
11496 result = PRAGMA_OMP_CLAUSE_ORDERED;
11497 break;
11498 case 'p':
11499 if (!strcmp ("parallel", p))
11500 result = PRAGMA_OMP_CLAUSE_PARALLEL;
11501 else if (!strcmp ("present", p))
11502 result = PRAGMA_OACC_CLAUSE_PRESENT;
11503 /* As of OpenACC 2.5, these are now aliases of the non-present_or
11504 clauses. */
11505 else if (!strcmp ("present_or_copy", p)
11506 || !strcmp ("pcopy", p))
11507 result = PRAGMA_OACC_CLAUSE_COPY;
11508 else if (!strcmp ("present_or_copyin", p)
11509 || !strcmp ("pcopyin", p))
11510 result = PRAGMA_OACC_CLAUSE_COPYIN;
11511 else if (!strcmp ("present_or_copyout", p)
11512 || !strcmp ("pcopyout", p))
11513 result = PRAGMA_OACC_CLAUSE_COPYOUT;
11514 else if (!strcmp ("present_or_create", p)
11515 || !strcmp ("pcreate", p))
11516 result = PRAGMA_OACC_CLAUSE_CREATE;
11517 else if (!strcmp ("priority", p))
11518 result = PRAGMA_OMP_CLAUSE_PRIORITY;
11519 else if (!strcmp ("private", p))
11520 result = PRAGMA_OMP_CLAUSE_PRIVATE;
11521 else if (!strcmp ("proc_bind", p))
11522 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
11523 break;
11524 case 'r':
11525 if (!strcmp ("reduction", p))
11526 result = PRAGMA_OMP_CLAUSE_REDUCTION;
11527 break;
11528 case 's':
11529 if (!strcmp ("safelen", p))
11530 result = PRAGMA_OMP_CLAUSE_SAFELEN;
11531 else if (!strcmp ("schedule", p))
11532 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
11533 else if (!strcmp ("sections", p))
11534 result = PRAGMA_OMP_CLAUSE_SECTIONS;
11535 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
11536 result = PRAGMA_OACC_CLAUSE_HOST;
11537 else if (!strcmp ("seq", p))
11538 result = PRAGMA_OACC_CLAUSE_SEQ;
11539 else if (!strcmp ("shared", p))
11540 result = PRAGMA_OMP_CLAUSE_SHARED;
11541 else if (!strcmp ("simd", p))
11542 result = PRAGMA_OMP_CLAUSE_SIMD;
11543 else if (!strcmp ("simdlen", p))
11544 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
11545 break;
11546 case 't':
11547 if (!strcmp ("taskgroup", p))
11548 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
11549 else if (!strcmp ("thread_limit", p))
11550 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
11551 else if (!strcmp ("threads", p))
11552 result = PRAGMA_OMP_CLAUSE_THREADS;
11553 else if (!strcmp ("tile", p))
11554 result = PRAGMA_OACC_CLAUSE_TILE;
11555 else if (!strcmp ("to", p))
11556 result = PRAGMA_OMP_CLAUSE_TO;
11557 break;
11558 case 'u':
11559 if (!strcmp ("uniform", p))
11560 result = PRAGMA_OMP_CLAUSE_UNIFORM;
11561 else if (!strcmp ("untied", p))
11562 result = PRAGMA_OMP_CLAUSE_UNTIED;
11563 else if (!strcmp ("use_device", p))
11564 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
11565 else if (!strcmp ("use_device_ptr", p))
11566 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
11567 break;
11568 case 'v':
11569 if (!strcmp ("vector", p))
11570 result = PRAGMA_OACC_CLAUSE_VECTOR;
11571 else if (!strcmp ("vector_length", p))
11572 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
11573 break;
11574 case 'w':
11575 if (!strcmp ("wait", p))
11576 result = PRAGMA_OACC_CLAUSE_WAIT;
11577 else if (!strcmp ("worker", p))
11578 result = PRAGMA_OACC_CLAUSE_WORKER;
11579 break;
11583 if (result != PRAGMA_OMP_CLAUSE_NONE)
11584 c_parser_consume_token (parser);
11586 return result;
11589 /* Validate that a clause of the given type does not already exist. */
11591 static void
11592 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
11593 const char *name)
11595 tree c;
11597 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11598 if (OMP_CLAUSE_CODE (c) == code)
11600 location_t loc = OMP_CLAUSE_LOCATION (c);
11601 error_at (loc, "too many %qs clauses", name);
11602 break;
11606 /* OpenACC 2.0
11607 Parse wait clause or wait directive parameters. */
11609 static tree
11610 c_parser_oacc_wait_list (c_parser *parser, location_t clause_loc, tree list)
11612 vec<tree, va_gc> *args;
11613 tree t, args_tree;
11615 matching_parens parens;
11616 if (!parens.require_open (parser))
11617 return list;
11619 args = c_parser_expr_list (parser, false, true, NULL, NULL, NULL, NULL);
11621 if (args->length () == 0)
11623 c_parser_error (parser, "expected integer expression before ')'");
11624 release_tree_vector (args);
11625 return list;
11628 args_tree = build_tree_list_vec (args);
11630 for (t = args_tree; t; t = TREE_CHAIN (t))
11632 tree targ = TREE_VALUE (t);
11634 if (targ != error_mark_node)
11636 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
11638 c_parser_error (parser, "expression must be integral");
11639 targ = error_mark_node;
11641 else
11643 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
11645 OMP_CLAUSE_DECL (c) = targ;
11646 OMP_CLAUSE_CHAIN (c) = list;
11647 list = c;
11652 release_tree_vector (args);
11653 parens.require_close (parser);
11654 return list;
11657 /* OpenACC 2.0, OpenMP 2.5:
11658 variable-list:
11659 identifier
11660 variable-list , identifier
11662 If KIND is nonzero, create the appropriate node and install the
11663 decl in OMP_CLAUSE_DECL and add the node to the head of the list.
11664 If KIND is nonzero, CLAUSE_LOC is the location of the clause.
11666 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
11667 return the list created. */
11669 static tree
11670 c_parser_omp_variable_list (c_parser *parser,
11671 location_t clause_loc,
11672 enum omp_clause_code kind, tree list)
11674 if (c_parser_next_token_is_not (parser, CPP_NAME)
11675 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
11676 c_parser_error (parser, "expected identifier");
11678 while (c_parser_next_token_is (parser, CPP_NAME)
11679 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
11681 tree t = lookup_name (c_parser_peek_token (parser)->value);
11683 if (t == NULL_TREE)
11685 undeclared_variable (c_parser_peek_token (parser)->location,
11686 c_parser_peek_token (parser)->value);
11687 t = error_mark_node;
11690 c_parser_consume_token (parser);
11692 if (t == error_mark_node)
11694 else if (kind != 0)
11696 switch (kind)
11698 case OMP_CLAUSE__CACHE_:
11699 /* The OpenACC cache directive explicitly only allows "array
11700 elements or subarrays". */
11701 if (c_parser_peek_token (parser)->type != CPP_OPEN_SQUARE)
11703 c_parser_error (parser, "expected %<[%>");
11704 t = error_mark_node;
11705 break;
11707 /* FALLTHROUGH */
11708 case OMP_CLAUSE_MAP:
11709 case OMP_CLAUSE_FROM:
11710 case OMP_CLAUSE_TO:
11711 while (c_parser_next_token_is (parser, CPP_DOT))
11713 location_t op_loc = c_parser_peek_token (parser)->location;
11714 c_parser_consume_token (parser);
11715 if (!c_parser_next_token_is (parser, CPP_NAME))
11717 c_parser_error (parser, "expected identifier");
11718 t = error_mark_node;
11719 break;
11722 c_token *comp_tok = c_parser_peek_token (parser);
11723 tree ident = comp_tok->value;
11724 location_t comp_loc = comp_tok->location;
11725 c_parser_consume_token (parser);
11726 t = build_component_ref (op_loc, t, ident, comp_loc);
11728 /* FALLTHROUGH */
11729 case OMP_CLAUSE_DEPEND:
11730 case OMP_CLAUSE_REDUCTION:
11731 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
11733 tree low_bound = NULL_TREE, length = NULL_TREE;
11735 c_parser_consume_token (parser);
11736 if (!c_parser_next_token_is (parser, CPP_COLON))
11738 location_t expr_loc
11739 = c_parser_peek_token (parser)->location;
11740 c_expr expr = c_parser_expression (parser);
11741 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11742 false, true);
11743 low_bound = expr.value;
11745 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11746 length = integer_one_node;
11747 else
11749 /* Look for `:'. */
11750 if (!c_parser_require (parser, CPP_COLON,
11751 "expected %<:%>"))
11753 t = error_mark_node;
11754 break;
11756 if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
11758 location_t expr_loc
11759 = c_parser_peek_token (parser)->location;
11760 c_expr expr = c_parser_expression (parser);
11761 expr = convert_lvalue_to_rvalue (expr_loc, expr,
11762 false, true);
11763 length = expr.value;
11766 /* Look for the closing `]'. */
11767 if (!c_parser_require (parser, CPP_CLOSE_SQUARE,
11768 "expected %<]%>"))
11770 t = error_mark_node;
11771 break;
11774 t = tree_cons (low_bound, length, t);
11776 break;
11777 default:
11778 break;
11781 if (t != error_mark_node)
11783 tree u = build_omp_clause (clause_loc, kind);
11784 OMP_CLAUSE_DECL (u) = t;
11785 OMP_CLAUSE_CHAIN (u) = list;
11786 list = u;
11789 else
11790 list = tree_cons (t, NULL_TREE, list);
11792 if (c_parser_next_token_is_not (parser, CPP_COMMA))
11793 break;
11795 c_parser_consume_token (parser);
11798 return list;
11801 /* Similarly, but expect leading and trailing parenthesis. This is a very
11802 common case for OpenACC and OpenMP clauses. */
11804 static tree
11805 c_parser_omp_var_list_parens (c_parser *parser, enum omp_clause_code kind,
11806 tree list)
11808 /* The clauses location. */
11809 location_t loc = c_parser_peek_token (parser)->location;
11811 matching_parens parens;
11812 if (parens.require_open (parser))
11814 list = c_parser_omp_variable_list (parser, loc, kind, list);
11815 parens.skip_until_found_close (parser);
11817 return list;
11820 /* OpenACC 2.0:
11821 copy ( variable-list )
11822 copyin ( variable-list )
11823 copyout ( variable-list )
11824 create ( variable-list )
11825 delete ( variable-list )
11826 present ( variable-list ) */
11828 static tree
11829 c_parser_oacc_data_clause (c_parser *parser, pragma_omp_clause c_kind,
11830 tree list)
11832 enum gomp_map_kind kind;
11833 switch (c_kind)
11835 case PRAGMA_OACC_CLAUSE_COPY:
11836 kind = GOMP_MAP_TOFROM;
11837 break;
11838 case PRAGMA_OACC_CLAUSE_COPYIN:
11839 kind = GOMP_MAP_TO;
11840 break;
11841 case PRAGMA_OACC_CLAUSE_COPYOUT:
11842 kind = GOMP_MAP_FROM;
11843 break;
11844 case PRAGMA_OACC_CLAUSE_CREATE:
11845 kind = GOMP_MAP_ALLOC;
11846 break;
11847 case PRAGMA_OACC_CLAUSE_DELETE:
11848 kind = GOMP_MAP_RELEASE;
11849 break;
11850 case PRAGMA_OACC_CLAUSE_DEVICE:
11851 kind = GOMP_MAP_FORCE_TO;
11852 break;
11853 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
11854 kind = GOMP_MAP_DEVICE_RESIDENT;
11855 break;
11856 case PRAGMA_OACC_CLAUSE_HOST:
11857 kind = GOMP_MAP_FORCE_FROM;
11858 break;
11859 case PRAGMA_OACC_CLAUSE_LINK:
11860 kind = GOMP_MAP_LINK;
11861 break;
11862 case PRAGMA_OACC_CLAUSE_PRESENT:
11863 kind = GOMP_MAP_FORCE_PRESENT;
11864 break;
11865 default:
11866 gcc_unreachable ();
11868 tree nl, c;
11869 nl = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_MAP, list);
11871 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
11872 OMP_CLAUSE_SET_MAP_KIND (c, kind);
11874 return nl;
11877 /* OpenACC 2.0:
11878 deviceptr ( variable-list ) */
11880 static tree
11881 c_parser_oacc_data_clause_deviceptr (c_parser *parser, tree list)
11883 location_t loc = c_parser_peek_token (parser)->location;
11884 tree vars, t;
11886 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
11887 c_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
11888 variable-list must only allow for pointer variables. */
11889 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
11890 for (t = vars; t && t; t = TREE_CHAIN (t))
11892 tree v = TREE_PURPOSE (t);
11894 /* FIXME diagnostics: Ideally we should keep individual
11895 locations for all the variables in the var list to make the
11896 following errors more precise. Perhaps
11897 c_parser_omp_var_list_parens() should construct a list of
11898 locations to go along with the var list. */
11900 if (!VAR_P (v) && TREE_CODE (v) != PARM_DECL)
11901 error_at (loc, "%qD is not a variable", v);
11902 else if (TREE_TYPE (v) == error_mark_node)
11904 else if (!POINTER_TYPE_P (TREE_TYPE (v)))
11905 error_at (loc, "%qD is not a pointer variable", v);
11907 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
11908 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
11909 OMP_CLAUSE_DECL (u) = v;
11910 OMP_CLAUSE_CHAIN (u) = list;
11911 list = u;
11914 return list;
11917 /* OpenACC 2.0, OpenMP 3.0:
11918 collapse ( constant-expression ) */
11920 static tree
11921 c_parser_omp_clause_collapse (c_parser *parser, tree list)
11923 tree c, num = error_mark_node;
11924 HOST_WIDE_INT n;
11925 location_t loc;
11927 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
11928 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
11930 loc = c_parser_peek_token (parser)->location;
11931 matching_parens parens;
11932 if (parens.require_open (parser))
11934 num = c_parser_expr_no_commas (parser, NULL).value;
11935 parens.skip_until_found_close (parser);
11937 if (num == error_mark_node)
11938 return list;
11939 mark_exp_read (num);
11940 num = c_fully_fold (num, false, NULL);
11941 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
11942 || !tree_fits_shwi_p (num)
11943 || (n = tree_to_shwi (num)) <= 0
11944 || (int) n != n)
11946 error_at (loc,
11947 "collapse argument needs positive constant integer expression");
11948 return list;
11950 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
11951 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
11952 OMP_CLAUSE_CHAIN (c) = list;
11953 return c;
11956 /* OpenMP 2.5:
11957 copyin ( variable-list ) */
11959 static tree
11960 c_parser_omp_clause_copyin (c_parser *parser, tree list)
11962 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list);
11965 /* OpenMP 2.5:
11966 copyprivate ( variable-list ) */
11968 static tree
11969 c_parser_omp_clause_copyprivate (c_parser *parser, tree list)
11971 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list);
11974 /* OpenMP 2.5:
11975 default ( none | shared )
11977 OpenACC:
11978 default ( none | present ) */
11980 static tree
11981 c_parser_omp_clause_default (c_parser *parser, tree list, bool is_oacc)
11983 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
11984 location_t loc = c_parser_peek_token (parser)->location;
11985 tree c;
11987 matching_parens parens;
11988 if (!parens.require_open (parser))
11989 return list;
11990 if (c_parser_next_token_is (parser, CPP_NAME))
11992 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
11994 switch (p[0])
11996 case 'n':
11997 if (strcmp ("none", p) != 0)
11998 goto invalid_kind;
11999 kind = OMP_CLAUSE_DEFAULT_NONE;
12000 break;
12002 case 'p':
12003 if (strcmp ("present", p) != 0 || !is_oacc)
12004 goto invalid_kind;
12005 kind = OMP_CLAUSE_DEFAULT_PRESENT;
12006 break;
12008 case 's':
12009 if (strcmp ("shared", p) != 0 || is_oacc)
12010 goto invalid_kind;
12011 kind = OMP_CLAUSE_DEFAULT_SHARED;
12012 break;
12014 default:
12015 goto invalid_kind;
12018 c_parser_consume_token (parser);
12020 else
12022 invalid_kind:
12023 if (is_oacc)
12024 c_parser_error (parser, "expected %<none%> or %<present%>");
12025 else
12026 c_parser_error (parser, "expected %<none%> or %<shared%>");
12028 parens.skip_until_found_close (parser);
12030 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
12031 return list;
12033 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
12034 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULT);
12035 OMP_CLAUSE_CHAIN (c) = list;
12036 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
12038 return c;
12041 /* OpenMP 2.5:
12042 firstprivate ( variable-list ) */
12044 static tree
12045 c_parser_omp_clause_firstprivate (c_parser *parser, tree list)
12047 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list);
12050 /* OpenMP 3.1:
12051 final ( expression ) */
12053 static tree
12054 c_parser_omp_clause_final (c_parser *parser, tree list)
12056 location_t loc = c_parser_peek_token (parser)->location;
12057 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12059 tree t = c_parser_paren_condition (parser);
12060 tree c;
12062 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final");
12064 c = build_omp_clause (loc, OMP_CLAUSE_FINAL);
12065 OMP_CLAUSE_FINAL_EXPR (c) = t;
12066 OMP_CLAUSE_CHAIN (c) = list;
12067 list = c;
12069 else
12070 c_parser_error (parser, "expected %<(%>");
12072 return list;
12075 /* OpenACC, OpenMP 2.5:
12076 if ( expression )
12078 OpenMP 4.5:
12079 if ( directive-name-modifier : expression )
12081 directive-name-modifier:
12082 parallel | task | taskloop | target data | target | target update
12083 | target enter data | target exit data */
12085 static tree
12086 c_parser_omp_clause_if (c_parser *parser, tree list, bool is_omp)
12088 location_t location = c_parser_peek_token (parser)->location;
12089 enum tree_code if_modifier = ERROR_MARK;
12091 matching_parens parens;
12092 if (!parens.require_open (parser))
12093 return list;
12095 if (is_omp && c_parser_next_token_is (parser, CPP_NAME))
12097 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12098 int n = 2;
12099 if (strcmp (p, "parallel") == 0)
12100 if_modifier = OMP_PARALLEL;
12101 else if (strcmp (p, "task") == 0)
12102 if_modifier = OMP_TASK;
12103 else if (strcmp (p, "taskloop") == 0)
12104 if_modifier = OMP_TASKLOOP;
12105 else if (strcmp (p, "target") == 0)
12107 if_modifier = OMP_TARGET;
12108 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12110 p = IDENTIFIER_POINTER (c_parser_peek_2nd_token (parser)->value);
12111 if (strcmp ("data", p) == 0)
12112 if_modifier = OMP_TARGET_DATA;
12113 else if (strcmp ("update", p) == 0)
12114 if_modifier = OMP_TARGET_UPDATE;
12115 else if (strcmp ("enter", p) == 0)
12116 if_modifier = OMP_TARGET_ENTER_DATA;
12117 else if (strcmp ("exit", p) == 0)
12118 if_modifier = OMP_TARGET_EXIT_DATA;
12119 if (if_modifier != OMP_TARGET)
12121 n = 3;
12122 c_parser_consume_token (parser);
12124 else
12126 location_t loc = c_parser_peek_2nd_token (parser)->location;
12127 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
12128 "or %<exit%>");
12129 if_modifier = ERROR_MARK;
12131 if (if_modifier == OMP_TARGET_ENTER_DATA
12132 || if_modifier == OMP_TARGET_EXIT_DATA)
12134 if (c_parser_peek_2nd_token (parser)->type == CPP_NAME)
12136 p = IDENTIFIER_POINTER
12137 (c_parser_peek_2nd_token (parser)->value);
12138 if (strcmp ("data", p) == 0)
12139 n = 4;
12141 if (n == 4)
12142 c_parser_consume_token (parser);
12143 else
12145 location_t loc
12146 = c_parser_peek_2nd_token (parser)->location;
12147 error_at (loc, "expected %<data%>");
12148 if_modifier = ERROR_MARK;
12153 if (if_modifier != ERROR_MARK)
12155 if (c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12157 c_parser_consume_token (parser);
12158 c_parser_consume_token (parser);
12160 else
12162 if (n > 2)
12164 location_t loc = c_parser_peek_2nd_token (parser)->location;
12165 error_at (loc, "expected %<:%>");
12167 if_modifier = ERROR_MARK;
12172 tree t = c_parser_condition (parser), c;
12173 parens.skip_until_found_close (parser);
12175 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
12176 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
12178 if (if_modifier != ERROR_MARK
12179 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12181 const char *p = NULL;
12182 switch (if_modifier)
12184 case OMP_PARALLEL: p = "parallel"; break;
12185 case OMP_TASK: p = "task"; break;
12186 case OMP_TASKLOOP: p = "taskloop"; break;
12187 case OMP_TARGET_DATA: p = "target data"; break;
12188 case OMP_TARGET: p = "target"; break;
12189 case OMP_TARGET_UPDATE: p = "target update"; break;
12190 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
12191 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
12192 default: gcc_unreachable ();
12194 error_at (location, "too many %<if%> clauses with %qs modifier",
12196 return list;
12198 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
12200 if (!is_omp)
12201 error_at (location, "too many %<if%> clauses");
12202 else
12203 error_at (location, "too many %<if%> clauses without modifier");
12204 return list;
12206 else if (if_modifier == ERROR_MARK
12207 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
12209 error_at (location, "if any %<if%> clause has modifier, then all "
12210 "%<if%> clauses have to use modifier");
12211 return list;
12215 c = build_omp_clause (location, OMP_CLAUSE_IF);
12216 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
12217 OMP_CLAUSE_IF_EXPR (c) = t;
12218 OMP_CLAUSE_CHAIN (c) = list;
12219 return c;
12222 /* OpenMP 2.5:
12223 lastprivate ( variable-list ) */
12225 static tree
12226 c_parser_omp_clause_lastprivate (c_parser *parser, tree list)
12228 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list);
12231 /* OpenMP 3.1:
12232 mergeable */
12234 static tree
12235 c_parser_omp_clause_mergeable (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12237 tree c;
12239 /* FIXME: Should we allow duplicates? */
12240 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable");
12242 c = build_omp_clause (c_parser_peek_token (parser)->location,
12243 OMP_CLAUSE_MERGEABLE);
12244 OMP_CLAUSE_CHAIN (c) = list;
12246 return c;
12249 /* OpenMP 2.5:
12250 nowait */
12252 static tree
12253 c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list)
12255 tree c;
12256 location_t loc = c_parser_peek_token (parser)->location;
12258 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
12260 c = build_omp_clause (loc, OMP_CLAUSE_NOWAIT);
12261 OMP_CLAUSE_CHAIN (c) = list;
12262 return c;
12265 /* OpenMP 2.5:
12266 num_threads ( expression ) */
12268 static tree
12269 c_parser_omp_clause_num_threads (c_parser *parser, tree list)
12271 location_t num_threads_loc = c_parser_peek_token (parser)->location;
12272 matching_parens parens;
12273 if (parens.require_open (parser))
12275 location_t expr_loc = c_parser_peek_token (parser)->location;
12276 c_expr expr = c_parser_expression (parser);
12277 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12278 tree c, t = expr.value;
12279 t = c_fully_fold (t, false, NULL);
12281 parens.skip_until_found_close (parser);
12283 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12285 c_parser_error (parser, "expected integer expression");
12286 return list;
12289 /* Attempt to statically determine when the number isn't positive. */
12290 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12291 build_int_cst (TREE_TYPE (t), 0));
12292 protected_set_expr_location (c, expr_loc);
12293 if (c == boolean_true_node)
12295 warning_at (expr_loc, 0,
12296 "%<num_threads%> value must be positive");
12297 t = integer_one_node;
12300 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
12302 c = build_omp_clause (num_threads_loc, OMP_CLAUSE_NUM_THREADS);
12303 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
12304 OMP_CLAUSE_CHAIN (c) = list;
12305 list = c;
12308 return list;
12311 /* OpenMP 4.5:
12312 num_tasks ( expression ) */
12314 static tree
12315 c_parser_omp_clause_num_tasks (c_parser *parser, tree list)
12317 location_t num_tasks_loc = c_parser_peek_token (parser)->location;
12318 matching_parens parens;
12319 if (parens.require_open (parser))
12321 location_t expr_loc = c_parser_peek_token (parser)->location;
12322 c_expr expr = c_parser_expression (parser);
12323 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12324 tree c, t = expr.value;
12325 t = c_fully_fold (t, false, NULL);
12327 parens.skip_until_found_close (parser);
12329 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12331 c_parser_error (parser, "expected integer expression");
12332 return list;
12335 /* Attempt to statically determine when the number isn't positive. */
12336 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12337 build_int_cst (TREE_TYPE (t), 0));
12338 if (CAN_HAVE_LOCATION_P (c))
12339 SET_EXPR_LOCATION (c, expr_loc);
12340 if (c == boolean_true_node)
12342 warning_at (expr_loc, 0, "%<num_tasks%> value must be positive");
12343 t = integer_one_node;
12346 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS, "num_tasks");
12348 c = build_omp_clause (num_tasks_loc, OMP_CLAUSE_NUM_TASKS);
12349 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
12350 OMP_CLAUSE_CHAIN (c) = list;
12351 list = c;
12354 return list;
12357 /* OpenMP 4.5:
12358 grainsize ( expression ) */
12360 static tree
12361 c_parser_omp_clause_grainsize (c_parser *parser, tree list)
12363 location_t grainsize_loc = c_parser_peek_token (parser)->location;
12364 matching_parens parens;
12365 if (parens.require_open (parser))
12367 location_t expr_loc = c_parser_peek_token (parser)->location;
12368 c_expr expr = c_parser_expression (parser);
12369 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12370 tree c, t = expr.value;
12371 t = c_fully_fold (t, false, NULL);
12373 parens.skip_until_found_close (parser);
12375 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12377 c_parser_error (parser, "expected integer expression");
12378 return list;
12381 /* Attempt to statically determine when the number isn't positive. */
12382 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12383 build_int_cst (TREE_TYPE (t), 0));
12384 if (CAN_HAVE_LOCATION_P (c))
12385 SET_EXPR_LOCATION (c, expr_loc);
12386 if (c == boolean_true_node)
12388 warning_at (expr_loc, 0, "%<grainsize%> value must be positive");
12389 t = integer_one_node;
12392 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE, "grainsize");
12394 c = build_omp_clause (grainsize_loc, OMP_CLAUSE_GRAINSIZE);
12395 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
12396 OMP_CLAUSE_CHAIN (c) = list;
12397 list = c;
12400 return list;
12403 /* OpenMP 4.5:
12404 priority ( expression ) */
12406 static tree
12407 c_parser_omp_clause_priority (c_parser *parser, tree list)
12409 location_t priority_loc = c_parser_peek_token (parser)->location;
12410 matching_parens parens;
12411 if (parens.require_open (parser))
12413 location_t expr_loc = c_parser_peek_token (parser)->location;
12414 c_expr expr = c_parser_expression (parser);
12415 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12416 tree c, t = expr.value;
12417 t = c_fully_fold (t, false, NULL);
12419 parens.skip_until_found_close (parser);
12421 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12423 c_parser_error (parser, "expected integer expression");
12424 return list;
12427 /* Attempt to statically determine when the number isn't
12428 non-negative. */
12429 c = fold_build2_loc (expr_loc, LT_EXPR, boolean_type_node, t,
12430 build_int_cst (TREE_TYPE (t), 0));
12431 if (CAN_HAVE_LOCATION_P (c))
12432 SET_EXPR_LOCATION (c, expr_loc);
12433 if (c == boolean_true_node)
12435 warning_at (expr_loc, 0, "%<priority%> value must be non-negative");
12436 t = integer_one_node;
12439 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY, "priority");
12441 c = build_omp_clause (priority_loc, OMP_CLAUSE_PRIORITY);
12442 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
12443 OMP_CLAUSE_CHAIN (c) = list;
12444 list = c;
12447 return list;
12450 /* OpenMP 4.5:
12451 hint ( expression ) */
12453 static tree
12454 c_parser_omp_clause_hint (c_parser *parser, tree list)
12456 location_t hint_loc = c_parser_peek_token (parser)->location;
12457 matching_parens parens;
12458 if (parens.require_open (parser))
12460 location_t expr_loc = c_parser_peek_token (parser)->location;
12461 c_expr expr = c_parser_expression (parser);
12462 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12463 tree c, t = expr.value;
12464 t = c_fully_fold (t, false, NULL);
12466 parens.skip_until_found_close (parser);
12468 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12470 c_parser_error (parser, "expected integer expression");
12471 return list;
12474 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint");
12476 c = build_omp_clause (hint_loc, OMP_CLAUSE_HINT);
12477 OMP_CLAUSE_HINT_EXPR (c) = t;
12478 OMP_CLAUSE_CHAIN (c) = list;
12479 list = c;
12482 return list;
12485 /* OpenMP 4.5:
12486 defaultmap ( tofrom : scalar ) */
12488 static tree
12489 c_parser_omp_clause_defaultmap (c_parser *parser, tree list)
12491 location_t loc = c_parser_peek_token (parser)->location;
12492 tree c;
12493 const char *p;
12495 matching_parens parens;
12496 if (!parens.require_open (parser))
12497 return list;
12498 if (!c_parser_next_token_is (parser, CPP_NAME))
12500 c_parser_error (parser, "expected %<tofrom%>");
12501 goto out_err;
12503 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12504 if (strcmp (p, "tofrom") != 0)
12506 c_parser_error (parser, "expected %<tofrom%>");
12507 goto out_err;
12509 c_parser_consume_token (parser);
12510 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12511 goto out_err;
12512 if (!c_parser_next_token_is (parser, CPP_NAME))
12514 c_parser_error (parser, "expected %<scalar%>");
12515 goto out_err;
12517 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
12518 if (strcmp (p, "scalar") != 0)
12520 c_parser_error (parser, "expected %<scalar%>");
12521 goto out_err;
12523 c_parser_consume_token (parser);
12524 parens.skip_until_found_close (parser);
12525 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap");
12526 c = build_omp_clause (loc, OMP_CLAUSE_DEFAULTMAP);
12527 OMP_CLAUSE_CHAIN (c) = list;
12528 return c;
12530 out_err:
12531 parens.skip_until_found_close (parser);
12532 return list;
12535 /* OpenACC 2.0:
12536 use_device ( variable-list )
12538 OpenMP 4.5:
12539 use_device_ptr ( variable-list ) */
12541 static tree
12542 c_parser_omp_clause_use_device_ptr (c_parser *parser, tree list)
12544 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_USE_DEVICE_PTR,
12545 list);
12548 /* OpenMP 4.5:
12549 is_device_ptr ( variable-list ) */
12551 static tree
12552 c_parser_omp_clause_is_device_ptr (c_parser *parser, tree list)
12554 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_IS_DEVICE_PTR, list);
12557 /* OpenACC:
12558 num_gangs ( expression )
12559 num_workers ( expression )
12560 vector_length ( expression ) */
12562 static tree
12563 c_parser_oacc_single_int_clause (c_parser *parser, omp_clause_code code,
12564 tree list)
12566 location_t loc = c_parser_peek_token (parser)->location;
12568 matching_parens parens;
12569 if (!parens.require_open (parser))
12570 return list;
12572 location_t expr_loc = c_parser_peek_token (parser)->location;
12573 c_expr expr = c_parser_expression (parser);
12574 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
12575 tree c, t = expr.value;
12576 t = c_fully_fold (t, false, NULL);
12578 parens.skip_until_found_close (parser);
12580 if (t == error_mark_node)
12581 return list;
12582 else if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12584 error_at (expr_loc, "%qs expression must be integral",
12585 omp_clause_code_name[code]);
12586 return list;
12589 /* Attempt to statically determine when the number isn't positive. */
12590 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
12591 build_int_cst (TREE_TYPE (t), 0));
12592 protected_set_expr_location (c, expr_loc);
12593 if (c == boolean_true_node)
12595 warning_at (expr_loc, 0,
12596 "%qs value must be positive",
12597 omp_clause_code_name[code]);
12598 t = integer_one_node;
12601 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12603 c = build_omp_clause (loc, code);
12604 OMP_CLAUSE_OPERAND (c, 0) = t;
12605 OMP_CLAUSE_CHAIN (c) = list;
12606 return c;
12609 /* OpenACC:
12611 gang [( gang-arg-list )]
12612 worker [( [num:] int-expr )]
12613 vector [( [length:] int-expr )]
12615 where gang-arg is one of:
12617 [num:] int-expr
12618 static: size-expr
12620 and size-expr may be:
12623 int-expr
12626 static tree
12627 c_parser_oacc_shape_clause (c_parser *parser, omp_clause_code kind,
12628 const char *str, tree list)
12630 const char *id = "num";
12631 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
12632 location_t loc = c_parser_peek_token (parser)->location;
12634 if (kind == OMP_CLAUSE_VECTOR)
12635 id = "length";
12637 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12639 c_parser_consume_token (parser);
12643 c_token *next = c_parser_peek_token (parser);
12644 int idx = 0;
12646 /* Gang static argument. */
12647 if (kind == OMP_CLAUSE_GANG
12648 && c_parser_next_token_is_keyword (parser, RID_STATIC))
12650 c_parser_consume_token (parser);
12652 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
12653 goto cleanup_error;
12655 idx = 1;
12656 if (ops[idx] != NULL_TREE)
12658 c_parser_error (parser, "too many %<static%> arguments");
12659 goto cleanup_error;
12662 /* Check for the '*' argument. */
12663 if (c_parser_next_token_is (parser, CPP_MULT)
12664 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12665 || c_parser_peek_2nd_token (parser)->type
12666 == CPP_CLOSE_PAREN))
12668 c_parser_consume_token (parser);
12669 ops[idx] = integer_minus_one_node;
12671 if (c_parser_next_token_is (parser, CPP_COMMA))
12673 c_parser_consume_token (parser);
12674 continue;
12676 else
12677 break;
12680 /* Worker num: argument and vector length: arguments. */
12681 else if (c_parser_next_token_is (parser, CPP_NAME)
12682 && strcmp (id, IDENTIFIER_POINTER (next->value)) == 0
12683 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
12685 c_parser_consume_token (parser); /* id */
12686 c_parser_consume_token (parser); /* ':' */
12689 /* Now collect the actual argument. */
12690 if (ops[idx] != NULL_TREE)
12692 c_parser_error (parser, "unexpected argument");
12693 goto cleanup_error;
12696 location_t expr_loc = c_parser_peek_token (parser)->location;
12697 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12698 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12699 tree expr = cexpr.value;
12700 if (expr == error_mark_node)
12701 goto cleanup_error;
12703 expr = c_fully_fold (expr, false, NULL);
12705 /* Attempt to statically determine when the number isn't a
12706 positive integer. */
12708 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr)))
12710 c_parser_error (parser, "expected integer expression");
12711 return list;
12714 tree c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, expr,
12715 build_int_cst (TREE_TYPE (expr), 0));
12716 if (c == boolean_true_node)
12718 warning_at (loc, 0,
12719 "%qs value must be positive", str);
12720 expr = integer_one_node;
12723 ops[idx] = expr;
12725 if (kind == OMP_CLAUSE_GANG
12726 && c_parser_next_token_is (parser, CPP_COMMA))
12728 c_parser_consume_token (parser);
12729 continue;
12731 break;
12733 while (1);
12735 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12736 goto cleanup_error;
12739 check_no_duplicate_clause (list, kind, str);
12741 c = build_omp_clause (loc, kind);
12743 if (ops[1])
12744 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
12746 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
12747 OMP_CLAUSE_CHAIN (c) = list;
12749 return c;
12751 cleanup_error:
12752 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
12753 return list;
12756 /* OpenACC 2.5:
12757 auto
12758 finalize
12759 independent
12760 nohost
12761 seq */
12763 static tree
12764 c_parser_oacc_simple_clause (c_parser *parser, enum omp_clause_code code,
12765 tree list)
12767 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
12769 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
12770 OMP_CLAUSE_CHAIN (c) = list;
12772 return c;
12775 /* OpenACC:
12776 async [( int-expr )] */
12778 static tree
12779 c_parser_oacc_clause_async (c_parser *parser, tree list)
12781 tree c, t;
12782 location_t loc = c_parser_peek_token (parser)->location;
12784 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
12786 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12788 c_parser_consume_token (parser);
12790 t = c_parser_expression (parser).value;
12791 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
12792 c_parser_error (parser, "expected integer expression");
12793 else if (t == error_mark_node
12794 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
12795 return list;
12797 else
12798 t = c_fully_fold (t, false, NULL);
12800 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async");
12802 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
12803 OMP_CLAUSE_ASYNC_EXPR (c) = t;
12804 OMP_CLAUSE_CHAIN (c) = list;
12805 list = c;
12807 return list;
12810 /* OpenACC 2.0:
12811 tile ( size-expr-list ) */
12813 static tree
12814 c_parser_oacc_clause_tile (c_parser *parser, tree list)
12816 tree c, expr = error_mark_node;
12817 location_t loc;
12818 tree tile = NULL_TREE;
12820 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile");
12821 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse");
12823 loc = c_parser_peek_token (parser)->location;
12824 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
12825 return list;
12829 if (tile && !c_parser_require (parser, CPP_COMMA, "expected %<,%>"))
12830 return list;
12832 if (c_parser_next_token_is (parser, CPP_MULT)
12833 && (c_parser_peek_2nd_token (parser)->type == CPP_COMMA
12834 || c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_PAREN))
12836 c_parser_consume_token (parser);
12837 expr = integer_zero_node;
12839 else
12841 location_t expr_loc = c_parser_peek_token (parser)->location;
12842 c_expr cexpr = c_parser_expr_no_commas (parser, NULL);
12843 cexpr = convert_lvalue_to_rvalue (expr_loc, cexpr, false, true);
12844 expr = cexpr.value;
12846 if (expr == error_mark_node)
12848 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
12849 "expected %<)%>");
12850 return list;
12853 expr = c_fully_fold (expr, false, NULL);
12855 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
12856 || !tree_fits_shwi_p (expr)
12857 || tree_to_shwi (expr) <= 0)
12859 error_at (expr_loc, "%<tile%> argument needs positive"
12860 " integral constant");
12861 expr = integer_zero_node;
12865 tile = tree_cons (NULL_TREE, expr, tile);
12867 while (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN));
12869 /* Consume the trailing ')'. */
12870 c_parser_consume_token (parser);
12872 c = build_omp_clause (loc, OMP_CLAUSE_TILE);
12873 tile = nreverse (tile);
12874 OMP_CLAUSE_TILE_LIST (c) = tile;
12875 OMP_CLAUSE_CHAIN (c) = list;
12876 return c;
12879 /* OpenACC:
12880 wait ( int-expr-list ) */
12882 static tree
12883 c_parser_oacc_clause_wait (c_parser *parser, tree list)
12885 location_t clause_loc = c_parser_peek_token (parser)->location;
12887 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
12888 list = c_parser_oacc_wait_list (parser, clause_loc, list);
12890 return list;
12893 /* OpenMP 2.5:
12894 ordered
12896 OpenMP 4.5:
12897 ordered ( constant-expression ) */
12899 static tree
12900 c_parser_omp_clause_ordered (c_parser *parser, tree list)
12902 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
12904 tree c, num = NULL_TREE;
12905 HOST_WIDE_INT n;
12906 location_t loc = c_parser_peek_token (parser)->location;
12907 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
12909 matching_parens parens;
12910 parens.consume_open (parser);
12911 num = c_parser_expr_no_commas (parser, NULL).value;
12912 parens.skip_until_found_close (parser);
12914 if (num == error_mark_node)
12915 return list;
12916 if (num)
12918 mark_exp_read (num);
12919 num = c_fully_fold (num, false, NULL);
12920 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
12921 || !tree_fits_shwi_p (num)
12922 || (n = tree_to_shwi (num)) <= 0
12923 || (int) n != n)
12925 error_at (loc, "ordered argument needs positive "
12926 "constant integer expression");
12927 return list;
12930 c = build_omp_clause (loc, OMP_CLAUSE_ORDERED);
12931 OMP_CLAUSE_ORDERED_EXPR (c) = num;
12932 OMP_CLAUSE_CHAIN (c) = list;
12933 return c;
12936 /* OpenMP 2.5:
12937 private ( variable-list ) */
12939 static tree
12940 c_parser_omp_clause_private (c_parser *parser, tree list)
12942 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list);
12945 /* OpenMP 2.5:
12946 reduction ( reduction-operator : variable-list )
12948 reduction-operator:
12949 One of: + * - & ^ | && ||
12951 OpenMP 3.1:
12953 reduction-operator:
12954 One of: + * - & ^ | && || max min
12956 OpenMP 4.0:
12958 reduction-operator:
12959 One of: + * - & ^ | && ||
12960 identifier */
12962 static tree
12963 c_parser_omp_clause_reduction (c_parser *parser, tree list)
12965 location_t clause_loc = c_parser_peek_token (parser)->location;
12966 matching_parens parens;
12967 if (parens.require_open (parser))
12969 enum tree_code code = ERROR_MARK;
12970 tree reduc_id = NULL_TREE;
12972 switch (c_parser_peek_token (parser)->type)
12974 case CPP_PLUS:
12975 code = PLUS_EXPR;
12976 break;
12977 case CPP_MULT:
12978 code = MULT_EXPR;
12979 break;
12980 case CPP_MINUS:
12981 code = MINUS_EXPR;
12982 break;
12983 case CPP_AND:
12984 code = BIT_AND_EXPR;
12985 break;
12986 case CPP_XOR:
12987 code = BIT_XOR_EXPR;
12988 break;
12989 case CPP_OR:
12990 code = BIT_IOR_EXPR;
12991 break;
12992 case CPP_AND_AND:
12993 code = TRUTH_ANDIF_EXPR;
12994 break;
12995 case CPP_OR_OR:
12996 code = TRUTH_ORIF_EXPR;
12997 break;
12998 case CPP_NAME:
13000 const char *p
13001 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13002 if (strcmp (p, "min") == 0)
13004 code = MIN_EXPR;
13005 break;
13007 if (strcmp (p, "max") == 0)
13009 code = MAX_EXPR;
13010 break;
13012 reduc_id = c_parser_peek_token (parser)->value;
13013 break;
13015 default:
13016 c_parser_error (parser,
13017 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
13018 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
13019 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13020 return list;
13022 c_parser_consume_token (parser);
13023 reduc_id = c_omp_reduction_id (code, reduc_id);
13024 if (c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13026 tree nl, c;
13028 nl = c_parser_omp_variable_list (parser, clause_loc,
13029 OMP_CLAUSE_REDUCTION, list);
13030 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13032 tree d = OMP_CLAUSE_DECL (c), type;
13033 if (TREE_CODE (d) != TREE_LIST)
13034 type = TREE_TYPE (d);
13035 else
13037 int cnt = 0;
13038 tree t;
13039 for (t = d; TREE_CODE (t) == TREE_LIST; t = TREE_CHAIN (t))
13040 cnt++;
13041 type = TREE_TYPE (t);
13042 while (cnt > 0)
13044 if (TREE_CODE (type) != POINTER_TYPE
13045 && TREE_CODE (type) != ARRAY_TYPE)
13046 break;
13047 type = TREE_TYPE (type);
13048 cnt--;
13051 while (TREE_CODE (type) == ARRAY_TYPE)
13052 type = TREE_TYPE (type);
13053 OMP_CLAUSE_REDUCTION_CODE (c) = code;
13054 if (code == ERROR_MARK
13055 || !(INTEGRAL_TYPE_P (type)
13056 || TREE_CODE (type) == REAL_TYPE
13057 || TREE_CODE (type) == COMPLEX_TYPE))
13058 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c)
13059 = c_omp_reduction_lookup (reduc_id,
13060 TYPE_MAIN_VARIANT (type));
13063 list = nl;
13065 parens.skip_until_found_close (parser);
13067 return list;
13070 /* OpenMP 2.5:
13071 schedule ( schedule-kind )
13072 schedule ( schedule-kind , expression )
13074 schedule-kind:
13075 static | dynamic | guided | runtime | auto
13077 OpenMP 4.5:
13078 schedule ( schedule-modifier : schedule-kind )
13079 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
13081 schedule-modifier:
13082 simd
13083 monotonic
13084 nonmonotonic */
13086 static tree
13087 c_parser_omp_clause_schedule (c_parser *parser, tree list)
13089 tree c, t;
13090 location_t loc = c_parser_peek_token (parser)->location;
13091 int modifiers = 0, nmodifiers = 0;
13093 matching_parens parens;
13094 if (!parens.require_open (parser))
13095 return list;
13097 c = build_omp_clause (loc, OMP_CLAUSE_SCHEDULE);
13099 while (c_parser_next_token_is (parser, CPP_NAME))
13101 tree kind = c_parser_peek_token (parser)->value;
13102 const char *p = IDENTIFIER_POINTER (kind);
13103 if (strcmp ("simd", p) == 0)
13104 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
13105 else if (strcmp ("monotonic", p) == 0)
13106 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
13107 else if (strcmp ("nonmonotonic", p) == 0)
13108 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
13109 else
13110 break;
13111 c_parser_consume_token (parser);
13112 if (nmodifiers++ == 0
13113 && c_parser_next_token_is (parser, CPP_COMMA))
13114 c_parser_consume_token (parser);
13115 else
13117 c_parser_require (parser, CPP_COLON, "expected %<:%>");
13118 break;
13122 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
13123 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13124 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
13125 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13127 error_at (loc, "both %<monotonic%> and %<nonmonotonic%> modifiers "
13128 "specified");
13129 modifiers = 0;
13132 if (c_parser_next_token_is (parser, CPP_NAME))
13134 tree kind = c_parser_peek_token (parser)->value;
13135 const char *p = IDENTIFIER_POINTER (kind);
13137 switch (p[0])
13139 case 'd':
13140 if (strcmp ("dynamic", p) != 0)
13141 goto invalid_kind;
13142 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
13143 break;
13145 case 'g':
13146 if (strcmp ("guided", p) != 0)
13147 goto invalid_kind;
13148 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
13149 break;
13151 case 'r':
13152 if (strcmp ("runtime", p) != 0)
13153 goto invalid_kind;
13154 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
13155 break;
13157 default:
13158 goto invalid_kind;
13161 else if (c_parser_next_token_is_keyword (parser, RID_STATIC))
13162 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
13163 else if (c_parser_next_token_is_keyword (parser, RID_AUTO))
13164 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
13165 else
13166 goto invalid_kind;
13168 c_parser_consume_token (parser);
13169 if (c_parser_next_token_is (parser, CPP_COMMA))
13171 location_t here;
13172 c_parser_consume_token (parser);
13174 here = c_parser_peek_token (parser)->location;
13175 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13176 expr = convert_lvalue_to_rvalue (here, expr, false, true);
13177 t = expr.value;
13178 t = c_fully_fold (t, false, NULL);
13180 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
13181 error_at (here, "schedule %<runtime%> does not take "
13182 "a %<chunk_size%> parameter");
13183 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
13184 error_at (here,
13185 "schedule %<auto%> does not take "
13186 "a %<chunk_size%> parameter");
13187 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE)
13189 /* Attempt to statically determine when the number isn't
13190 positive. */
13191 tree s = fold_build2_loc (loc, LE_EXPR, boolean_type_node, t,
13192 build_int_cst (TREE_TYPE (t), 0));
13193 protected_set_expr_location (s, loc);
13194 if (s == boolean_true_node)
13196 warning_at (loc, 0,
13197 "chunk size value must be positive");
13198 t = integer_one_node;
13200 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
13202 else
13203 c_parser_error (parser, "expected integer expression");
13205 parens.skip_until_found_close (parser);
13207 else
13208 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13209 "expected %<,%> or %<)%>");
13211 OMP_CLAUSE_SCHEDULE_KIND (c)
13212 = (enum omp_clause_schedule_kind)
13213 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
13215 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13216 OMP_CLAUSE_CHAIN (c) = list;
13217 return c;
13219 invalid_kind:
13220 c_parser_error (parser, "invalid schedule kind");
13221 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0);
13222 return list;
13225 /* OpenMP 2.5:
13226 shared ( variable-list ) */
13228 static tree
13229 c_parser_omp_clause_shared (c_parser *parser, tree list)
13231 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list);
13234 /* OpenMP 3.0:
13235 untied */
13237 static tree
13238 c_parser_omp_clause_untied (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13240 tree c;
13242 /* FIXME: Should we allow duplicates? */
13243 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied");
13245 c = build_omp_clause (c_parser_peek_token (parser)->location,
13246 OMP_CLAUSE_UNTIED);
13247 OMP_CLAUSE_CHAIN (c) = list;
13249 return c;
13252 /* OpenMP 4.0:
13253 inbranch
13254 notinbranch */
13256 static tree
13257 c_parser_omp_clause_branch (c_parser *parser ATTRIBUTE_UNUSED,
13258 enum omp_clause_code code, tree list)
13260 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13262 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13263 OMP_CLAUSE_CHAIN (c) = list;
13265 return c;
13268 /* OpenMP 4.0:
13269 parallel
13271 sections
13272 taskgroup */
13274 static tree
13275 c_parser_omp_clause_cancelkind (c_parser *parser ATTRIBUTE_UNUSED,
13276 enum omp_clause_code code, tree list)
13278 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13279 OMP_CLAUSE_CHAIN (c) = list;
13281 return c;
13284 /* OpenMP 4.5:
13285 nogroup */
13287 static tree
13288 c_parser_omp_clause_nogroup (c_parser *parser ATTRIBUTE_UNUSED, tree list)
13290 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup");
13291 tree c = build_omp_clause (c_parser_peek_token (parser)->location,
13292 OMP_CLAUSE_NOGROUP);
13293 OMP_CLAUSE_CHAIN (c) = list;
13294 return c;
13297 /* OpenMP 4.5:
13298 simd
13299 threads */
13301 static tree
13302 c_parser_omp_clause_orderedkind (c_parser *parser ATTRIBUTE_UNUSED,
13303 enum omp_clause_code code, tree list)
13305 check_no_duplicate_clause (list, code, omp_clause_code_name[code]);
13306 tree c = build_omp_clause (c_parser_peek_token (parser)->location, code);
13307 OMP_CLAUSE_CHAIN (c) = list;
13308 return c;
13311 /* OpenMP 4.0:
13312 num_teams ( expression ) */
13314 static tree
13315 c_parser_omp_clause_num_teams (c_parser *parser, tree list)
13317 location_t num_teams_loc = c_parser_peek_token (parser)->location;
13318 matching_parens parens;
13319 if (parens.require_open (parser))
13321 location_t expr_loc = c_parser_peek_token (parser)->location;
13322 c_expr expr = c_parser_expression (parser);
13323 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13324 tree c, t = expr.value;
13325 t = c_fully_fold (t, false, NULL);
13327 parens.skip_until_found_close (parser);
13329 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13331 c_parser_error (parser, "expected integer expression");
13332 return list;
13335 /* Attempt to statically determine when the number isn't positive. */
13336 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13337 build_int_cst (TREE_TYPE (t), 0));
13338 protected_set_expr_location (c, expr_loc);
13339 if (c == boolean_true_node)
13341 warning_at (expr_loc, 0, "%<num_teams%> value must be positive");
13342 t = integer_one_node;
13345 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS, "num_teams");
13347 c = build_omp_clause (num_teams_loc, OMP_CLAUSE_NUM_TEAMS);
13348 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
13349 OMP_CLAUSE_CHAIN (c) = list;
13350 list = c;
13353 return list;
13356 /* OpenMP 4.0:
13357 thread_limit ( expression ) */
13359 static tree
13360 c_parser_omp_clause_thread_limit (c_parser *parser, tree list)
13362 location_t num_thread_limit_loc = c_parser_peek_token (parser)->location;
13363 matching_parens parens;
13364 if (parens.require_open (parser))
13366 location_t expr_loc = c_parser_peek_token (parser)->location;
13367 c_expr expr = c_parser_expression (parser);
13368 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13369 tree c, t = expr.value;
13370 t = c_fully_fold (t, false, NULL);
13372 parens.skip_until_found_close (parser);
13374 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13376 c_parser_error (parser, "expected integer expression");
13377 return list;
13380 /* Attempt to statically determine when the number isn't positive. */
13381 c = fold_build2_loc (expr_loc, LE_EXPR, boolean_type_node, t,
13382 build_int_cst (TREE_TYPE (t), 0));
13383 protected_set_expr_location (c, expr_loc);
13384 if (c == boolean_true_node)
13386 warning_at (expr_loc, 0, "%<thread_limit%> value must be positive");
13387 t = integer_one_node;
13390 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
13391 "thread_limit");
13393 c = build_omp_clause (num_thread_limit_loc, OMP_CLAUSE_THREAD_LIMIT);
13394 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
13395 OMP_CLAUSE_CHAIN (c) = list;
13396 list = c;
13399 return list;
13402 /* OpenMP 4.0:
13403 aligned ( variable-list )
13404 aligned ( variable-list : constant-expression ) */
13406 static tree
13407 c_parser_omp_clause_aligned (c_parser *parser, tree list)
13409 location_t clause_loc = c_parser_peek_token (parser)->location;
13410 tree nl, c;
13412 matching_parens parens;
13413 if (!parens.require_open (parser))
13414 return list;
13416 nl = c_parser_omp_variable_list (parser, clause_loc,
13417 OMP_CLAUSE_ALIGNED, list);
13419 if (c_parser_next_token_is (parser, CPP_COLON))
13421 c_parser_consume_token (parser);
13422 location_t expr_loc = c_parser_peek_token (parser)->location;
13423 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13424 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13425 tree alignment = expr.value;
13426 alignment = c_fully_fold (alignment, false, NULL);
13427 if (TREE_CODE (alignment) != INTEGER_CST
13428 || !INTEGRAL_TYPE_P (TREE_TYPE (alignment))
13429 || tree_int_cst_sgn (alignment) != 1)
13431 error_at (clause_loc, "%<aligned%> clause alignment expression must "
13432 "be positive constant integer expression");
13433 alignment = NULL_TREE;
13436 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13437 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
13440 parens.skip_until_found_close (parser);
13441 return nl;
13444 /* OpenMP 4.0:
13445 linear ( variable-list )
13446 linear ( variable-list : expression )
13448 OpenMP 4.5:
13449 linear ( modifier ( variable-list ) )
13450 linear ( modifier ( variable-list ) : expression ) */
13452 static tree
13453 c_parser_omp_clause_linear (c_parser *parser, tree list)
13455 location_t clause_loc = c_parser_peek_token (parser)->location;
13456 tree nl, c, step;
13457 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
13459 matching_parens parens;
13460 if (!parens.require_open (parser))
13461 return list;
13463 if (c_parser_next_token_is (parser, CPP_NAME))
13465 c_token *tok = c_parser_peek_token (parser);
13466 const char *p = IDENTIFIER_POINTER (tok->value);
13467 if (strcmp ("val", p) == 0)
13468 kind = OMP_CLAUSE_LINEAR_VAL;
13469 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN)
13470 kind = OMP_CLAUSE_LINEAR_DEFAULT;
13471 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13473 c_parser_consume_token (parser);
13474 c_parser_consume_token (parser);
13478 nl = c_parser_omp_variable_list (parser, clause_loc,
13479 OMP_CLAUSE_LINEAR, list);
13481 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
13482 parens.skip_until_found_close (parser);
13484 if (c_parser_next_token_is (parser, CPP_COLON))
13486 c_parser_consume_token (parser);
13487 location_t expr_loc = c_parser_peek_token (parser)->location;
13488 c_expr expr = c_parser_expression (parser);
13489 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13490 step = expr.value;
13491 step = c_fully_fold (step, false, NULL);
13492 if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
13494 error_at (clause_loc, "%<linear%> clause step expression must "
13495 "be integral");
13496 step = integer_one_node;
13500 else
13501 step = integer_one_node;
13503 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13505 OMP_CLAUSE_LINEAR_STEP (c) = step;
13506 OMP_CLAUSE_LINEAR_KIND (c) = kind;
13509 parens.skip_until_found_close (parser);
13510 return nl;
13513 /* OpenMP 4.0:
13514 safelen ( constant-expression ) */
13516 static tree
13517 c_parser_omp_clause_safelen (c_parser *parser, tree list)
13519 location_t clause_loc = c_parser_peek_token (parser)->location;
13520 tree c, t;
13522 matching_parens parens;
13523 if (!parens.require_open (parser))
13524 return list;
13526 location_t expr_loc = c_parser_peek_token (parser)->location;
13527 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13528 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13529 t = expr.value;
13530 t = c_fully_fold (t, false, NULL);
13531 if (TREE_CODE (t) != INTEGER_CST
13532 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13533 || tree_int_cst_sgn (t) != 1)
13535 error_at (clause_loc, "%<safelen%> clause expression must "
13536 "be positive constant integer expression");
13537 t = NULL_TREE;
13540 parens.skip_until_found_close (parser);
13541 if (t == NULL_TREE || t == error_mark_node)
13542 return list;
13544 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen");
13546 c = build_omp_clause (clause_loc, OMP_CLAUSE_SAFELEN);
13547 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
13548 OMP_CLAUSE_CHAIN (c) = list;
13549 return c;
13552 /* OpenMP 4.0:
13553 simdlen ( constant-expression ) */
13555 static tree
13556 c_parser_omp_clause_simdlen (c_parser *parser, tree list)
13558 location_t clause_loc = c_parser_peek_token (parser)->location;
13559 tree c, t;
13561 matching_parens parens;
13562 if (!parens.require_open (parser))
13563 return list;
13565 location_t expr_loc = c_parser_peek_token (parser)->location;
13566 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13567 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13568 t = expr.value;
13569 t = c_fully_fold (t, false, NULL);
13570 if (TREE_CODE (t) != INTEGER_CST
13571 || !INTEGRAL_TYPE_P (TREE_TYPE (t))
13572 || tree_int_cst_sgn (t) != 1)
13574 error_at (clause_loc, "%<simdlen%> clause expression must "
13575 "be positive constant integer expression");
13576 t = NULL_TREE;
13579 parens.skip_until_found_close (parser);
13580 if (t == NULL_TREE || t == error_mark_node)
13581 return list;
13583 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen");
13585 c = build_omp_clause (clause_loc, OMP_CLAUSE_SIMDLEN);
13586 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
13587 OMP_CLAUSE_CHAIN (c) = list;
13588 return c;
13591 /* OpenMP 4.5:
13592 vec:
13593 identifier [+/- integer]
13594 vec , identifier [+/- integer]
13597 static tree
13598 c_parser_omp_clause_depend_sink (c_parser *parser, location_t clause_loc,
13599 tree list)
13601 tree vec = NULL;
13602 if (c_parser_next_token_is_not (parser, CPP_NAME)
13603 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
13605 c_parser_error (parser, "expected identifier");
13606 return list;
13609 while (c_parser_next_token_is (parser, CPP_NAME)
13610 && c_parser_peek_token (parser)->id_kind == C_ID_ID)
13612 tree t = lookup_name (c_parser_peek_token (parser)->value);
13613 tree addend = NULL;
13615 if (t == NULL_TREE)
13617 undeclared_variable (c_parser_peek_token (parser)->location,
13618 c_parser_peek_token (parser)->value);
13619 t = error_mark_node;
13622 c_parser_consume_token (parser);
13624 bool neg = false;
13625 if (c_parser_next_token_is (parser, CPP_MINUS))
13626 neg = true;
13627 else if (!c_parser_next_token_is (parser, CPP_PLUS))
13629 addend = integer_zero_node;
13630 neg = false;
13631 goto add_to_vector;
13633 c_parser_consume_token (parser);
13635 if (c_parser_next_token_is_not (parser, CPP_NUMBER))
13637 c_parser_error (parser, "expected integer");
13638 return list;
13641 addend = c_parser_peek_token (parser)->value;
13642 if (TREE_CODE (addend) != INTEGER_CST)
13644 c_parser_error (parser, "expected integer");
13645 return list;
13647 c_parser_consume_token (parser);
13649 add_to_vector:
13650 if (t != error_mark_node)
13652 vec = tree_cons (addend, t, vec);
13653 if (neg)
13654 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
13657 if (c_parser_next_token_is_not (parser, CPP_COMMA))
13658 break;
13660 c_parser_consume_token (parser);
13663 if (vec == NULL_TREE)
13664 return list;
13666 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13667 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
13668 OMP_CLAUSE_DECL (u) = nreverse (vec);
13669 OMP_CLAUSE_CHAIN (u) = list;
13670 return u;
13673 /* OpenMP 4.0:
13674 depend ( depend-kind: variable-list )
13676 depend-kind:
13677 in | out | inout
13679 OpenMP 4.5:
13680 depend ( source )
13682 depend ( sink : vec ) */
13684 static tree
13685 c_parser_omp_clause_depend (c_parser *parser, tree list)
13687 location_t clause_loc = c_parser_peek_token (parser)->location;
13688 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
13689 tree nl, c;
13691 matching_parens parens;
13692 if (!parens.require_open (parser))
13693 return list;
13695 if (c_parser_next_token_is (parser, CPP_NAME))
13697 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13698 if (strcmp ("in", p) == 0)
13699 kind = OMP_CLAUSE_DEPEND_IN;
13700 else if (strcmp ("inout", p) == 0)
13701 kind = OMP_CLAUSE_DEPEND_INOUT;
13702 else if (strcmp ("out", p) == 0)
13703 kind = OMP_CLAUSE_DEPEND_OUT;
13704 else if (strcmp ("source", p) == 0)
13705 kind = OMP_CLAUSE_DEPEND_SOURCE;
13706 else if (strcmp ("sink", p) == 0)
13707 kind = OMP_CLAUSE_DEPEND_SINK;
13708 else
13709 goto invalid_kind;
13711 else
13712 goto invalid_kind;
13714 c_parser_consume_token (parser);
13716 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
13718 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
13719 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13720 OMP_CLAUSE_DECL (c) = NULL_TREE;
13721 OMP_CLAUSE_CHAIN (c) = list;
13722 parens.skip_until_found_close (parser);
13723 return c;
13726 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
13727 goto resync_fail;
13729 if (kind == OMP_CLAUSE_DEPEND_SINK)
13730 nl = c_parser_omp_clause_depend_sink (parser, clause_loc, list);
13731 else
13733 nl = c_parser_omp_variable_list (parser, clause_loc,
13734 OMP_CLAUSE_DEPEND, list);
13736 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13737 OMP_CLAUSE_DEPEND_KIND (c) = kind;
13740 parens.skip_until_found_close (parser);
13741 return nl;
13743 invalid_kind:
13744 c_parser_error (parser, "invalid depend kind");
13745 resync_fail:
13746 parens.skip_until_found_close (parser);
13747 return list;
13750 /* OpenMP 4.0:
13751 map ( map-kind: variable-list )
13752 map ( variable-list )
13754 map-kind:
13755 alloc | to | from | tofrom
13757 OpenMP 4.5:
13758 map-kind:
13759 alloc | to | from | tofrom | release | delete
13761 map ( always [,] map-kind: variable-list ) */
13763 static tree
13764 c_parser_omp_clause_map (c_parser *parser, tree list)
13766 location_t clause_loc = c_parser_peek_token (parser)->location;
13767 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
13768 int always = 0;
13769 enum c_id_kind always_id_kind = C_ID_NONE;
13770 location_t always_loc = UNKNOWN_LOCATION;
13771 tree always_id = NULL_TREE;
13772 tree nl, c;
13774 matching_parens parens;
13775 if (!parens.require_open (parser))
13776 return list;
13778 if (c_parser_next_token_is (parser, CPP_NAME))
13780 c_token *tok = c_parser_peek_token (parser);
13781 const char *p = IDENTIFIER_POINTER (tok->value);
13782 always_id_kind = tok->id_kind;
13783 always_loc = tok->location;
13784 always_id = tok->value;
13785 if (strcmp ("always", p) == 0)
13787 c_token *sectok = c_parser_peek_2nd_token (parser);
13788 if (sectok->type == CPP_COMMA)
13790 c_parser_consume_token (parser);
13791 c_parser_consume_token (parser);
13792 always = 2;
13794 else if (sectok->type == CPP_NAME)
13796 p = IDENTIFIER_POINTER (sectok->value);
13797 if (strcmp ("alloc", p) == 0
13798 || strcmp ("to", p) == 0
13799 || strcmp ("from", p) == 0
13800 || strcmp ("tofrom", p) == 0
13801 || strcmp ("release", p) == 0
13802 || strcmp ("delete", p) == 0)
13804 c_parser_consume_token (parser);
13805 always = 1;
13811 if (c_parser_next_token_is (parser, CPP_NAME)
13812 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)
13814 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13815 if (strcmp ("alloc", p) == 0)
13816 kind = GOMP_MAP_ALLOC;
13817 else if (strcmp ("to", p) == 0)
13818 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
13819 else if (strcmp ("from", p) == 0)
13820 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
13821 else if (strcmp ("tofrom", p) == 0)
13822 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
13823 else if (strcmp ("release", p) == 0)
13824 kind = GOMP_MAP_RELEASE;
13825 else if (strcmp ("delete", p) == 0)
13826 kind = GOMP_MAP_DELETE;
13827 else
13829 c_parser_error (parser, "invalid map kind");
13830 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13831 "expected %<)%>");
13832 return list;
13834 c_parser_consume_token (parser);
13835 c_parser_consume_token (parser);
13837 else if (always)
13839 if (always_id_kind != C_ID_ID)
13841 c_parser_error (parser, "expected identifier");
13842 parens.skip_until_found_close (parser);
13843 return list;
13846 tree t = lookup_name (always_id);
13847 if (t == NULL_TREE)
13849 undeclared_variable (always_loc, always_id);
13850 t = error_mark_node;
13852 if (t != error_mark_node)
13854 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_MAP);
13855 OMP_CLAUSE_DECL (u) = t;
13856 OMP_CLAUSE_CHAIN (u) = list;
13857 OMP_CLAUSE_SET_MAP_KIND (u, kind);
13858 list = u;
13860 if (always == 1)
13862 parens.skip_until_found_close (parser);
13863 return list;
13867 nl = c_parser_omp_variable_list (parser, clause_loc, OMP_CLAUSE_MAP, list);
13869 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
13870 OMP_CLAUSE_SET_MAP_KIND (c, kind);
13872 parens.skip_until_found_close (parser);
13873 return nl;
13876 /* OpenMP 4.0:
13877 device ( expression ) */
13879 static tree
13880 c_parser_omp_clause_device (c_parser *parser, tree list)
13882 location_t clause_loc = c_parser_peek_token (parser)->location;
13883 matching_parens parens;
13884 if (parens.require_open (parser))
13886 location_t expr_loc = c_parser_peek_token (parser)->location;
13887 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13888 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13889 tree c, t = expr.value;
13890 t = c_fully_fold (t, false, NULL);
13892 parens.skip_until_found_close (parser);
13894 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13896 c_parser_error (parser, "expected integer expression");
13897 return list;
13900 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE, "device");
13902 c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE);
13903 OMP_CLAUSE_DEVICE_ID (c) = t;
13904 OMP_CLAUSE_CHAIN (c) = list;
13905 list = c;
13908 return list;
13911 /* OpenMP 4.0:
13912 dist_schedule ( static )
13913 dist_schedule ( static , expression ) */
13915 static tree
13916 c_parser_omp_clause_dist_schedule (c_parser *parser, tree list)
13918 tree c, t = NULL_TREE;
13919 location_t loc = c_parser_peek_token (parser)->location;
13921 matching_parens parens;
13922 if (!parens.require_open (parser))
13923 return list;
13925 if (!c_parser_next_token_is_keyword (parser, RID_STATIC))
13927 c_parser_error (parser, "invalid dist_schedule kind");
13928 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13929 "expected %<)%>");
13930 return list;
13933 c_parser_consume_token (parser);
13934 if (c_parser_next_token_is (parser, CPP_COMMA))
13936 c_parser_consume_token (parser);
13938 location_t expr_loc = c_parser_peek_token (parser)->location;
13939 c_expr expr = c_parser_expr_no_commas (parser, NULL);
13940 expr = convert_lvalue_to_rvalue (expr_loc, expr, false, true);
13941 t = expr.value;
13942 t = c_fully_fold (t, false, NULL);
13943 parens.skip_until_found_close (parser);
13945 else
13946 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
13947 "expected %<,%> or %<)%>");
13949 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
13950 if (t == error_mark_node)
13951 return list;
13953 c = build_omp_clause (loc, OMP_CLAUSE_DIST_SCHEDULE);
13954 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
13955 OMP_CLAUSE_CHAIN (c) = list;
13956 return c;
13959 /* OpenMP 4.0:
13960 proc_bind ( proc-bind-kind )
13962 proc-bind-kind:
13963 master | close | spread */
13965 static tree
13966 c_parser_omp_clause_proc_bind (c_parser *parser, tree list)
13968 location_t clause_loc = c_parser_peek_token (parser)->location;
13969 enum omp_clause_proc_bind_kind kind;
13970 tree c;
13972 matching_parens parens;
13973 if (!parens.require_open (parser))
13974 return list;
13976 if (c_parser_next_token_is (parser, CPP_NAME))
13978 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
13979 if (strcmp ("master", p) == 0)
13980 kind = OMP_CLAUSE_PROC_BIND_MASTER;
13981 else if (strcmp ("close", p) == 0)
13982 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
13983 else if (strcmp ("spread", p) == 0)
13984 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
13985 else
13986 goto invalid_kind;
13988 else
13989 goto invalid_kind;
13991 c_parser_consume_token (parser);
13992 parens.skip_until_found_close (parser);
13993 c = build_omp_clause (clause_loc, OMP_CLAUSE_PROC_BIND);
13994 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
13995 OMP_CLAUSE_CHAIN (c) = list;
13996 return c;
13998 invalid_kind:
13999 c_parser_error (parser, "invalid proc_bind kind");
14000 parens.skip_until_found_close (parser);
14001 return list;
14004 /* OpenMP 4.0:
14005 to ( variable-list ) */
14007 static tree
14008 c_parser_omp_clause_to (c_parser *parser, tree list)
14010 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO, list);
14013 /* OpenMP 4.0:
14014 from ( variable-list ) */
14016 static tree
14017 c_parser_omp_clause_from (c_parser *parser, tree list)
14019 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FROM, list);
14022 /* OpenMP 4.0:
14023 uniform ( variable-list ) */
14025 static tree
14026 c_parser_omp_clause_uniform (c_parser *parser, tree list)
14028 /* The clauses location. */
14029 location_t loc = c_parser_peek_token (parser)->location;
14031 matching_parens parens;
14032 if (parens.require_open (parser))
14034 list = c_parser_omp_variable_list (parser, loc, OMP_CLAUSE_UNIFORM,
14035 list);
14036 parens.skip_until_found_close (parser);
14038 return list;
14041 /* Parse all OpenACC clauses. The set clauses allowed by the directive
14042 is a bitmask in MASK. Return the list of clauses found. */
14044 static tree
14045 c_parser_oacc_all_clauses (c_parser *parser, omp_clause_mask mask,
14046 const char *where, bool finish_p = true)
14048 tree clauses = NULL;
14049 bool first = true;
14051 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14053 location_t here;
14054 pragma_omp_clause c_kind;
14055 const char *c_name;
14056 tree prev = clauses;
14058 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14059 c_parser_consume_token (parser);
14061 here = c_parser_peek_token (parser)->location;
14062 c_kind = c_parser_omp_clause_name (parser);
14064 switch (c_kind)
14066 case PRAGMA_OACC_CLAUSE_ASYNC:
14067 clauses = c_parser_oacc_clause_async (parser, clauses);
14068 c_name = "async";
14069 break;
14070 case PRAGMA_OACC_CLAUSE_AUTO:
14071 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
14072 clauses);
14073 c_name = "auto";
14074 break;
14075 case PRAGMA_OACC_CLAUSE_COLLAPSE:
14076 clauses = c_parser_omp_clause_collapse (parser, clauses);
14077 c_name = "collapse";
14078 break;
14079 case PRAGMA_OACC_CLAUSE_COPY:
14080 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14081 c_name = "copy";
14082 break;
14083 case PRAGMA_OACC_CLAUSE_COPYIN:
14084 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14085 c_name = "copyin";
14086 break;
14087 case PRAGMA_OACC_CLAUSE_COPYOUT:
14088 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14089 c_name = "copyout";
14090 break;
14091 case PRAGMA_OACC_CLAUSE_CREATE:
14092 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14093 c_name = "create";
14094 break;
14095 case PRAGMA_OACC_CLAUSE_DELETE:
14096 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14097 c_name = "delete";
14098 break;
14099 case PRAGMA_OMP_CLAUSE_DEFAULT:
14100 clauses = c_parser_omp_clause_default (parser, clauses, true);
14101 c_name = "default";
14102 break;
14103 case PRAGMA_OACC_CLAUSE_DEVICE:
14104 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14105 c_name = "device";
14106 break;
14107 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
14108 clauses = c_parser_oacc_data_clause_deviceptr (parser, clauses);
14109 c_name = "deviceptr";
14110 break;
14111 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
14112 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14113 c_name = "device_resident";
14114 break;
14115 case PRAGMA_OACC_CLAUSE_FINALIZE:
14116 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
14117 clauses);
14118 c_name = "finalize";
14119 break;
14120 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
14121 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14122 c_name = "firstprivate";
14123 break;
14124 case PRAGMA_OACC_CLAUSE_GANG:
14125 c_name = "gang";
14126 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
14127 c_name, clauses);
14128 break;
14129 case PRAGMA_OACC_CLAUSE_HOST:
14130 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14131 c_name = "host";
14132 break;
14133 case PRAGMA_OACC_CLAUSE_IF:
14134 clauses = c_parser_omp_clause_if (parser, clauses, false);
14135 c_name = "if";
14136 break;
14137 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
14138 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_IF_PRESENT,
14139 clauses);
14140 c_name = "if_present";
14141 break;
14142 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
14143 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_INDEPENDENT,
14144 clauses);
14145 c_name = "independent";
14146 break;
14147 case PRAGMA_OACC_CLAUSE_LINK:
14148 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14149 c_name = "link";
14150 break;
14151 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
14152 clauses = c_parser_oacc_single_int_clause (parser,
14153 OMP_CLAUSE_NUM_GANGS,
14154 clauses);
14155 c_name = "num_gangs";
14156 break;
14157 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
14158 clauses = c_parser_oacc_single_int_clause (parser,
14159 OMP_CLAUSE_NUM_WORKERS,
14160 clauses);
14161 c_name = "num_workers";
14162 break;
14163 case PRAGMA_OACC_CLAUSE_PRESENT:
14164 clauses = c_parser_oacc_data_clause (parser, c_kind, clauses);
14165 c_name = "present";
14166 break;
14167 case PRAGMA_OACC_CLAUSE_PRIVATE:
14168 clauses = c_parser_omp_clause_private (parser, clauses);
14169 c_name = "private";
14170 break;
14171 case PRAGMA_OACC_CLAUSE_REDUCTION:
14172 clauses = c_parser_omp_clause_reduction (parser, clauses);
14173 c_name = "reduction";
14174 break;
14175 case PRAGMA_OACC_CLAUSE_SEQ:
14176 clauses = c_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
14177 clauses);
14178 c_name = "seq";
14179 break;
14180 case PRAGMA_OACC_CLAUSE_TILE:
14181 clauses = c_parser_oacc_clause_tile (parser, clauses);
14182 c_name = "tile";
14183 break;
14184 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
14185 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14186 c_name = "use_device";
14187 break;
14188 case PRAGMA_OACC_CLAUSE_VECTOR:
14189 c_name = "vector";
14190 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
14191 c_name, clauses);
14192 break;
14193 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
14194 clauses = c_parser_oacc_single_int_clause (parser,
14195 OMP_CLAUSE_VECTOR_LENGTH,
14196 clauses);
14197 c_name = "vector_length";
14198 break;
14199 case PRAGMA_OACC_CLAUSE_WAIT:
14200 clauses = c_parser_oacc_clause_wait (parser, clauses);
14201 c_name = "wait";
14202 break;
14203 case PRAGMA_OACC_CLAUSE_WORKER:
14204 c_name = "worker";
14205 clauses = c_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
14206 c_name, clauses);
14207 break;
14208 default:
14209 c_parser_error (parser, "expected %<#pragma acc%> clause");
14210 goto saw_error;
14213 first = false;
14215 if (((mask >> c_kind) & 1) == 0)
14217 /* Remove the invalid clause(s) from the list to avoid
14218 confusing the rest of the compiler. */
14219 clauses = prev;
14220 error_at (here, "%qs is not valid for %qs", c_name, where);
14224 saw_error:
14225 c_parser_skip_to_pragma_eol (parser);
14227 if (finish_p)
14228 return c_finish_omp_clauses (clauses, C_ORT_ACC);
14230 return clauses;
14233 /* Parse all OpenMP clauses. The set clauses allowed by the directive
14234 is a bitmask in MASK. Return the list of clauses found. */
14236 static tree
14237 c_parser_omp_all_clauses (c_parser *parser, omp_clause_mask mask,
14238 const char *where, bool finish_p = true)
14240 tree clauses = NULL;
14241 bool first = true;
14243 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
14245 location_t here;
14246 pragma_omp_clause c_kind;
14247 const char *c_name;
14248 tree prev = clauses;
14250 if (!first && c_parser_next_token_is (parser, CPP_COMMA))
14251 c_parser_consume_token (parser);
14253 here = c_parser_peek_token (parser)->location;
14254 c_kind = c_parser_omp_clause_name (parser);
14256 switch (c_kind)
14258 case PRAGMA_OMP_CLAUSE_COLLAPSE:
14259 clauses = c_parser_omp_clause_collapse (parser, clauses);
14260 c_name = "collapse";
14261 break;
14262 case PRAGMA_OMP_CLAUSE_COPYIN:
14263 clauses = c_parser_omp_clause_copyin (parser, clauses);
14264 c_name = "copyin";
14265 break;
14266 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
14267 clauses = c_parser_omp_clause_copyprivate (parser, clauses);
14268 c_name = "copyprivate";
14269 break;
14270 case PRAGMA_OMP_CLAUSE_DEFAULT:
14271 clauses = c_parser_omp_clause_default (parser, clauses, false);
14272 c_name = "default";
14273 break;
14274 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
14275 clauses = c_parser_omp_clause_firstprivate (parser, clauses);
14276 c_name = "firstprivate";
14277 break;
14278 case PRAGMA_OMP_CLAUSE_FINAL:
14279 clauses = c_parser_omp_clause_final (parser, clauses);
14280 c_name = "final";
14281 break;
14282 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
14283 clauses = c_parser_omp_clause_grainsize (parser, clauses);
14284 c_name = "grainsize";
14285 break;
14286 case PRAGMA_OMP_CLAUSE_HINT:
14287 clauses = c_parser_omp_clause_hint (parser, clauses);
14288 c_name = "hint";
14289 break;
14290 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
14291 clauses = c_parser_omp_clause_defaultmap (parser, clauses);
14292 c_name = "defaultmap";
14293 break;
14294 case PRAGMA_OMP_CLAUSE_IF:
14295 clauses = c_parser_omp_clause_if (parser, clauses, true);
14296 c_name = "if";
14297 break;
14298 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
14299 clauses = c_parser_omp_clause_lastprivate (parser, clauses);
14300 c_name = "lastprivate";
14301 break;
14302 case PRAGMA_OMP_CLAUSE_MERGEABLE:
14303 clauses = c_parser_omp_clause_mergeable (parser, clauses);
14304 c_name = "mergeable";
14305 break;
14306 case PRAGMA_OMP_CLAUSE_NOWAIT:
14307 clauses = c_parser_omp_clause_nowait (parser, clauses);
14308 c_name = "nowait";
14309 break;
14310 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
14311 clauses = c_parser_omp_clause_num_tasks (parser, clauses);
14312 c_name = "num_tasks";
14313 break;
14314 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
14315 clauses = c_parser_omp_clause_num_threads (parser, clauses);
14316 c_name = "num_threads";
14317 break;
14318 case PRAGMA_OMP_CLAUSE_ORDERED:
14319 clauses = c_parser_omp_clause_ordered (parser, clauses);
14320 c_name = "ordered";
14321 break;
14322 case PRAGMA_OMP_CLAUSE_PRIORITY:
14323 clauses = c_parser_omp_clause_priority (parser, clauses);
14324 c_name = "priority";
14325 break;
14326 case PRAGMA_OMP_CLAUSE_PRIVATE:
14327 clauses = c_parser_omp_clause_private (parser, clauses);
14328 c_name = "private";
14329 break;
14330 case PRAGMA_OMP_CLAUSE_REDUCTION:
14331 clauses = c_parser_omp_clause_reduction (parser, clauses);
14332 c_name = "reduction";
14333 break;
14334 case PRAGMA_OMP_CLAUSE_SCHEDULE:
14335 clauses = c_parser_omp_clause_schedule (parser, clauses);
14336 c_name = "schedule";
14337 break;
14338 case PRAGMA_OMP_CLAUSE_SHARED:
14339 clauses = c_parser_omp_clause_shared (parser, clauses);
14340 c_name = "shared";
14341 break;
14342 case PRAGMA_OMP_CLAUSE_UNTIED:
14343 clauses = c_parser_omp_clause_untied (parser, clauses);
14344 c_name = "untied";
14345 break;
14346 case PRAGMA_OMP_CLAUSE_INBRANCH:
14347 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
14348 clauses);
14349 c_name = "inbranch";
14350 break;
14351 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
14352 clauses = c_parser_omp_clause_branch (parser, OMP_CLAUSE_NOTINBRANCH,
14353 clauses);
14354 c_name = "notinbranch";
14355 break;
14356 case PRAGMA_OMP_CLAUSE_PARALLEL:
14357 clauses
14358 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
14359 clauses);
14360 c_name = "parallel";
14361 if (!first)
14363 clause_not_first:
14364 error_at (here, "%qs must be the first clause of %qs",
14365 c_name, where);
14366 clauses = prev;
14368 break;
14369 case PRAGMA_OMP_CLAUSE_FOR:
14370 clauses
14371 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
14372 clauses);
14373 c_name = "for";
14374 if (!first)
14375 goto clause_not_first;
14376 break;
14377 case PRAGMA_OMP_CLAUSE_SECTIONS:
14378 clauses
14379 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
14380 clauses);
14381 c_name = "sections";
14382 if (!first)
14383 goto clause_not_first;
14384 break;
14385 case PRAGMA_OMP_CLAUSE_TASKGROUP:
14386 clauses
14387 = c_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
14388 clauses);
14389 c_name = "taskgroup";
14390 if (!first)
14391 goto clause_not_first;
14392 break;
14393 case PRAGMA_OMP_CLAUSE_LINK:
14394 clauses
14395 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LINK, clauses);
14396 c_name = "link";
14397 break;
14398 case PRAGMA_OMP_CLAUSE_TO:
14399 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
14400 clauses
14401 = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
14402 clauses);
14403 else
14404 clauses = c_parser_omp_clause_to (parser, clauses);
14405 c_name = "to";
14406 break;
14407 case PRAGMA_OMP_CLAUSE_FROM:
14408 clauses = c_parser_omp_clause_from (parser, clauses);
14409 c_name = "from";
14410 break;
14411 case PRAGMA_OMP_CLAUSE_UNIFORM:
14412 clauses = c_parser_omp_clause_uniform (parser, clauses);
14413 c_name = "uniform";
14414 break;
14415 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
14416 clauses = c_parser_omp_clause_num_teams (parser, clauses);
14417 c_name = "num_teams";
14418 break;
14419 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
14420 clauses = c_parser_omp_clause_thread_limit (parser, clauses);
14421 c_name = "thread_limit";
14422 break;
14423 case PRAGMA_OMP_CLAUSE_ALIGNED:
14424 clauses = c_parser_omp_clause_aligned (parser, clauses);
14425 c_name = "aligned";
14426 break;
14427 case PRAGMA_OMP_CLAUSE_LINEAR:
14428 clauses = c_parser_omp_clause_linear (parser, clauses);
14429 c_name = "linear";
14430 break;
14431 case PRAGMA_OMP_CLAUSE_DEPEND:
14432 clauses = c_parser_omp_clause_depend (parser, clauses);
14433 c_name = "depend";
14434 break;
14435 case PRAGMA_OMP_CLAUSE_MAP:
14436 clauses = c_parser_omp_clause_map (parser, clauses);
14437 c_name = "map";
14438 break;
14439 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
14440 clauses = c_parser_omp_clause_use_device_ptr (parser, clauses);
14441 c_name = "use_device_ptr";
14442 break;
14443 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
14444 clauses = c_parser_omp_clause_is_device_ptr (parser, clauses);
14445 c_name = "is_device_ptr";
14446 break;
14447 case PRAGMA_OMP_CLAUSE_DEVICE:
14448 clauses = c_parser_omp_clause_device (parser, clauses);
14449 c_name = "device";
14450 break;
14451 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
14452 clauses = c_parser_omp_clause_dist_schedule (parser, clauses);
14453 c_name = "dist_schedule";
14454 break;
14455 case PRAGMA_OMP_CLAUSE_PROC_BIND:
14456 clauses = c_parser_omp_clause_proc_bind (parser, clauses);
14457 c_name = "proc_bind";
14458 break;
14459 case PRAGMA_OMP_CLAUSE_SAFELEN:
14460 clauses = c_parser_omp_clause_safelen (parser, clauses);
14461 c_name = "safelen";
14462 break;
14463 case PRAGMA_OMP_CLAUSE_SIMDLEN:
14464 clauses = c_parser_omp_clause_simdlen (parser, clauses);
14465 c_name = "simdlen";
14466 break;
14467 case PRAGMA_OMP_CLAUSE_NOGROUP:
14468 clauses = c_parser_omp_clause_nogroup (parser, clauses);
14469 c_name = "nogroup";
14470 break;
14471 case PRAGMA_OMP_CLAUSE_THREADS:
14472 clauses
14473 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
14474 clauses);
14475 c_name = "threads";
14476 break;
14477 case PRAGMA_OMP_CLAUSE_SIMD:
14478 clauses
14479 = c_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
14480 clauses);
14481 c_name = "simd";
14482 break;
14483 default:
14484 c_parser_error (parser, "expected %<#pragma omp%> clause");
14485 goto saw_error;
14488 first = false;
14490 if (((mask >> c_kind) & 1) == 0)
14492 /* Remove the invalid clause(s) from the list to avoid
14493 confusing the rest of the compiler. */
14494 clauses = prev;
14495 error_at (here, "%qs is not valid for %qs", c_name, where);
14499 saw_error:
14500 c_parser_skip_to_pragma_eol (parser);
14502 if (finish_p)
14504 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
14505 return c_finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
14506 return c_finish_omp_clauses (clauses, C_ORT_OMP);
14509 return clauses;
14512 /* OpenACC 2.0, OpenMP 2.5:
14513 structured-block:
14514 statement
14516 In practice, we're also interested in adding the statement to an
14517 outer node. So it is convenient if we work around the fact that
14518 c_parser_statement calls add_stmt. */
14520 static tree
14521 c_parser_omp_structured_block (c_parser *parser, bool *if_p)
14523 tree stmt = push_stmt_list ();
14524 c_parser_statement (parser, if_p);
14525 return pop_stmt_list (stmt);
14528 /* OpenACC 2.0:
14529 # pragma acc cache (variable-list) new-line
14531 LOC is the location of the #pragma token.
14534 static tree
14535 c_parser_oacc_cache (location_t loc, c_parser *parser)
14537 tree stmt, clauses;
14539 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE__CACHE_, NULL);
14540 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14542 c_parser_skip_to_pragma_eol (parser);
14544 stmt = make_node (OACC_CACHE);
14545 TREE_TYPE (stmt) = void_type_node;
14546 OACC_CACHE_CLAUSES (stmt) = clauses;
14547 SET_EXPR_LOCATION (stmt, loc);
14548 add_stmt (stmt);
14550 return stmt;
14553 /* OpenACC 2.0:
14554 # pragma acc data oacc-data-clause[optseq] new-line
14555 structured-block
14557 LOC is the location of the #pragma token.
14560 #define OACC_DATA_CLAUSE_MASK \
14561 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
14569 static tree
14570 c_parser_oacc_data (location_t loc, c_parser *parser, bool *if_p)
14572 tree stmt, clauses, block;
14574 clauses = c_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
14575 "#pragma acc data");
14577 block = c_begin_omp_parallel ();
14578 add_stmt (c_parser_omp_structured_block (parser, if_p));
14580 stmt = c_finish_oacc_data (loc, clauses, block);
14582 return stmt;
14585 /* OpenACC 2.0:
14586 # pragma acc declare oacc-data-clause[optseq] new-line
14589 #define OACC_DECLARE_CLAUSE_MASK \
14590 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
14596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
14597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT))
14599 static void
14600 c_parser_oacc_declare (c_parser *parser)
14602 location_t pragma_loc = c_parser_peek_token (parser)->location;
14603 tree clauses, stmt, t, decl;
14605 bool error = false;
14607 c_parser_consume_pragma (parser);
14609 clauses = c_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
14610 "#pragma acc declare");
14611 if (!clauses)
14613 error_at (pragma_loc,
14614 "no valid clauses specified in %<#pragma acc declare%>");
14615 return;
14618 for (t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
14620 location_t loc = OMP_CLAUSE_LOCATION (t);
14621 decl = OMP_CLAUSE_DECL (t);
14622 if (!DECL_P (decl))
14624 error_at (loc, "array section in %<#pragma acc declare%>");
14625 error = true;
14626 continue;
14629 switch (OMP_CLAUSE_MAP_KIND (t))
14631 case GOMP_MAP_FIRSTPRIVATE_POINTER:
14632 case GOMP_MAP_ALLOC:
14633 case GOMP_MAP_TO:
14634 case GOMP_MAP_FORCE_DEVICEPTR:
14635 case GOMP_MAP_DEVICE_RESIDENT:
14636 break;
14638 case GOMP_MAP_LINK:
14639 if (!global_bindings_p ()
14640 && (TREE_STATIC (decl)
14641 || !DECL_EXTERNAL (decl)))
14643 error_at (loc,
14644 "%qD must be a global variable in "
14645 "%<#pragma acc declare link%>",
14646 decl);
14647 error = true;
14648 continue;
14650 break;
14652 default:
14653 if (global_bindings_p ())
14655 error_at (loc, "invalid OpenACC clause at file scope");
14656 error = true;
14657 continue;
14659 if (DECL_EXTERNAL (decl))
14661 error_at (loc,
14662 "invalid use of %<extern%> variable %qD "
14663 "in %<#pragma acc declare%>", decl);
14664 error = true;
14665 continue;
14667 else if (TREE_PUBLIC (decl))
14669 error_at (loc,
14670 "invalid use of %<global%> variable %qD "
14671 "in %<#pragma acc declare%>", decl);
14672 error = true;
14673 continue;
14675 break;
14678 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
14679 || lookup_attribute ("omp declare target link",
14680 DECL_ATTRIBUTES (decl)))
14682 error_at (loc, "variable %qD used more than once with "
14683 "%<#pragma acc declare%>", decl);
14684 error = true;
14685 continue;
14688 if (!error)
14690 tree id;
14692 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
14693 id = get_identifier ("omp declare target link");
14694 else
14695 id = get_identifier ("omp declare target");
14697 DECL_ATTRIBUTES (decl)
14698 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
14700 if (global_bindings_p ())
14702 symtab_node *node = symtab_node::get (decl);
14703 if (node != NULL)
14705 node->offloadable = 1;
14706 if (ENABLE_OFFLOADING)
14708 g->have_offload = true;
14709 if (is_a <varpool_node *> (node))
14710 vec_safe_push (offload_vars, decl);
14717 if (error || global_bindings_p ())
14718 return;
14720 stmt = make_node (OACC_DECLARE);
14721 TREE_TYPE (stmt) = void_type_node;
14722 OACC_DECLARE_CLAUSES (stmt) = clauses;
14723 SET_EXPR_LOCATION (stmt, pragma_loc);
14725 add_stmt (stmt);
14727 return;
14730 /* OpenACC 2.0:
14731 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
14735 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
14738 LOC is the location of the #pragma token.
14741 #define OACC_ENTER_DATA_CLAUSE_MASK \
14742 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14748 #define OACC_EXIT_DATA_CLAUSE_MASK \
14749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
14753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
14754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14756 static void
14757 c_parser_oacc_enter_exit_data (c_parser *parser, bool enter)
14759 location_t loc = c_parser_peek_token (parser)->location;
14760 tree clauses, stmt;
14761 const char *p = "";
14763 c_parser_consume_pragma (parser);
14765 if (c_parser_next_token_is (parser, CPP_NAME))
14767 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14768 c_parser_consume_token (parser);
14771 if (strcmp (p, "data") != 0)
14773 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
14774 enter ? "enter" : "exit");
14775 parser->error = true;
14776 c_parser_skip_to_pragma_eol (parser);
14777 return;
14780 if (enter)
14781 clauses = c_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
14782 "#pragma acc enter data");
14783 else
14784 clauses = c_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
14785 "#pragma acc exit data");
14787 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
14789 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
14790 enter ? "enter" : "exit");
14791 return;
14794 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
14795 TREE_TYPE (stmt) = void_type_node;
14796 OMP_STANDALONE_CLAUSES (stmt) = clauses;
14797 SET_EXPR_LOCATION (stmt, loc);
14798 add_stmt (stmt);
14802 /* OpenACC 2.0:
14803 # pragma acc host_data oacc-data-clause[optseq] new-line
14804 structured-block
14807 #define OACC_HOST_DATA_CLAUSE_MASK \
14808 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
14810 static tree
14811 c_parser_oacc_host_data (location_t loc, c_parser *parser, bool *if_p)
14813 tree stmt, clauses, block;
14815 clauses = c_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
14816 "#pragma acc host_data");
14818 block = c_begin_omp_parallel ();
14819 add_stmt (c_parser_omp_structured_block (parser, if_p));
14820 stmt = c_finish_oacc_host_data (loc, clauses, block);
14821 return stmt;
14825 /* OpenACC 2.0:
14827 # pragma acc loop oacc-loop-clause[optseq] new-line
14828 structured-block
14830 LOC is the location of the #pragma token.
14833 #define OACC_LOOP_CLAUSE_MASK \
14834 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
14835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
14841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
14842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
14843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE) )
14844 static tree
14845 c_parser_oacc_loop (location_t loc, c_parser *parser, char *p_name,
14846 omp_clause_mask mask, tree *cclauses, bool *if_p)
14848 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
14850 strcat (p_name, " loop");
14851 mask |= OACC_LOOP_CLAUSE_MASK;
14853 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name,
14854 cclauses == NULL);
14855 if (cclauses)
14857 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
14858 if (*cclauses)
14859 *cclauses = c_finish_omp_clauses (*cclauses, C_ORT_ACC);
14860 if (clauses)
14861 clauses = c_finish_omp_clauses (clauses, C_ORT_ACC);
14864 tree block = c_begin_compound_stmt (true);
14865 tree stmt = c_parser_omp_for_loop (loc, parser, OACC_LOOP, clauses, NULL,
14866 if_p);
14867 block = c_end_compound_stmt (loc, block, true);
14868 add_stmt (block);
14870 return stmt;
14873 /* OpenACC 2.0:
14874 # pragma acc kernels oacc-kernels-clause[optseq] new-line
14875 structured-block
14879 # pragma acc parallel oacc-parallel-clause[optseq] new-line
14880 structured-block
14882 LOC is the location of the #pragma token.
14885 #define OACC_KERNELS_CLAUSE_MASK \
14886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14898 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14900 #define OACC_PARALLEL_CLAUSE_MASK \
14901 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
14902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
14903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
14904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
14905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
14906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
14907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
14908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
14909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
14910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
14911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
14912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
14913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
14914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
14915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
14916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
14918 static tree
14919 c_parser_oacc_kernels_parallel (location_t loc, c_parser *parser,
14920 enum pragma_kind p_kind, char *p_name,
14921 bool *if_p)
14923 omp_clause_mask mask;
14924 enum tree_code code;
14925 switch (p_kind)
14927 case PRAGMA_OACC_KERNELS:
14928 strcat (p_name, " kernels");
14929 mask = OACC_KERNELS_CLAUSE_MASK;
14930 code = OACC_KERNELS;
14931 break;
14932 case PRAGMA_OACC_PARALLEL:
14933 strcat (p_name, " parallel");
14934 mask = OACC_PARALLEL_CLAUSE_MASK;
14935 code = OACC_PARALLEL;
14936 break;
14937 default:
14938 gcc_unreachable ();
14941 if (c_parser_next_token_is (parser, CPP_NAME))
14943 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
14944 if (strcmp (p, "loop") == 0)
14946 c_parser_consume_token (parser);
14947 tree block = c_begin_omp_parallel ();
14948 tree clauses;
14949 c_parser_oacc_loop (loc, parser, p_name, mask, &clauses, if_p);
14950 return c_finish_omp_construct (loc, code, block, clauses);
14954 tree clauses = c_parser_oacc_all_clauses (parser, mask, p_name);
14956 tree block = c_begin_omp_parallel ();
14957 add_stmt (c_parser_omp_structured_block (parser, if_p));
14959 return c_finish_omp_construct (loc, code, block, clauses);
14962 /* OpenACC 2.0:
14963 # pragma acc routine oacc-routine-clause[optseq] new-line
14964 function-definition
14966 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
14969 #define OACC_ROUTINE_CLAUSE_MASK \
14970 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
14971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
14972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
14973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) )
14975 /* Parse an OpenACC routine directive. For named directives, we apply
14976 immediately to the named function. For unnamed ones we then parse
14977 a declaration or definition, which must be for a function. */
14979 static void
14980 c_parser_oacc_routine (c_parser *parser, enum pragma_context context)
14982 gcc_checking_assert (context == pragma_external);
14984 oacc_routine_data data;
14985 data.error_seen = false;
14986 data.fndecl_seen = false;
14987 data.clauses = NULL_TREE;
14988 data.loc = c_parser_peek_token (parser)->location;
14990 c_parser_consume_pragma (parser);
14992 /* Look for optional '( name )'. */
14993 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
14995 c_parser_consume_token (parser); /* '(' */
14997 tree decl = NULL_TREE;
14998 c_token *name_token = c_parser_peek_token (parser);
14999 location_t name_loc = name_token->location;
15000 if (name_token->type == CPP_NAME
15001 && (name_token->id_kind == C_ID_ID
15002 || name_token->id_kind == C_ID_TYPENAME))
15004 decl = lookup_name (name_token->value);
15005 if (!decl)
15006 error_at (name_loc,
15007 "%qE has not been declared", name_token->value);
15008 c_parser_consume_token (parser);
15010 else
15011 c_parser_error (parser, "expected function name");
15013 if (!decl
15014 || !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
15016 c_parser_skip_to_pragma_eol (parser, false);
15017 return;
15020 data.clauses
15021 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15022 "#pragma acc routine");
15024 if (TREE_CODE (decl) != FUNCTION_DECL)
15026 error_at (name_loc, "%qD does not refer to a function", decl);
15027 return;
15030 c_finish_oacc_routine (&data, decl, false);
15032 else /* No optional '( name )'. */
15034 data.clauses
15035 = c_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
15036 "#pragma acc routine");
15038 /* Emit a helpful diagnostic if there's another pragma following this
15039 one. Also don't allow a static assertion declaration, as in the
15040 following we'll just parse a *single* "declaration or function
15041 definition", and the static assertion counts an one. */
15042 if (c_parser_next_token_is (parser, CPP_PRAGMA)
15043 || c_parser_next_token_is_keyword (parser, RID_STATIC_ASSERT))
15045 error_at (data.loc,
15046 "%<#pragma acc routine%> not immediately followed by"
15047 " function declaration or definition");
15048 /* ..., and then just keep going. */
15049 return;
15052 /* We only have to consider the pragma_external case here. */
15053 if (c_parser_next_token_is (parser, CPP_KEYWORD)
15054 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
15056 int ext = disable_extension_diagnostics ();
15058 c_parser_consume_token (parser);
15059 while (c_parser_next_token_is (parser, CPP_KEYWORD)
15060 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
15061 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15062 NULL, vNULL, &data);
15063 restore_extension_diagnostics (ext);
15065 else
15066 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
15067 NULL, vNULL, &data);
15071 /* Finalize an OpenACC routine pragma, applying it to FNDECL.
15072 IS_DEFN is true if we're applying it to the definition. */
15074 static void
15075 c_finish_oacc_routine (struct oacc_routine_data *data, tree fndecl,
15076 bool is_defn)
15078 /* Keep going if we're in error reporting mode. */
15079 if (data->error_seen
15080 || fndecl == error_mark_node)
15081 return;
15083 if (data->fndecl_seen)
15085 error_at (data->loc,
15086 "%<#pragma acc routine%> not immediately followed by"
15087 " a single function declaration or definition");
15088 data->error_seen = true;
15089 return;
15091 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
15093 error_at (data->loc,
15094 "%<#pragma acc routine%> not immediately followed by"
15095 " function declaration or definition");
15096 data->error_seen = true;
15097 return;
15100 if (oacc_get_fn_attrib (fndecl))
15102 error_at (data->loc,
15103 "%<#pragma acc routine%> already applied to %qD", fndecl);
15104 data->error_seen = true;
15105 return;
15108 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
15110 error_at (data->loc,
15111 TREE_USED (fndecl)
15112 ? G_("%<#pragma acc routine%> must be applied before use")
15113 : G_("%<#pragma acc routine%> must be applied before "
15114 "definition"));
15115 data->error_seen = true;
15116 return;
15119 /* Process the routine's dimension clauses. */
15120 tree dims = oacc_build_routine_dims (data->clauses);
15121 oacc_replace_fn_attrib (fndecl, dims);
15123 /* Add an "omp declare target" attribute. */
15124 DECL_ATTRIBUTES (fndecl)
15125 = tree_cons (get_identifier ("omp declare target"),
15126 NULL_TREE, DECL_ATTRIBUTES (fndecl));
15128 /* Remember that we've used this "#pragma acc routine". */
15129 data->fndecl_seen = true;
15132 /* OpenACC 2.0:
15133 # pragma acc update oacc-update-clause[optseq] new-line
15136 #define OACC_UPDATE_CLAUSE_MASK \
15137 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
15138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
15139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
15140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
15141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
15142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
15144 static void
15145 c_parser_oacc_update (c_parser *parser)
15147 location_t loc = c_parser_peek_token (parser)->location;
15149 c_parser_consume_pragma (parser);
15151 tree clauses = c_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
15152 "#pragma acc update");
15153 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
15155 error_at (loc,
15156 "%<#pragma acc update%> must contain at least one "
15157 "%<device%> or %<host%> or %<self%> clause");
15158 return;
15161 if (parser->error)
15162 return;
15164 tree stmt = make_node (OACC_UPDATE);
15165 TREE_TYPE (stmt) = void_type_node;
15166 OACC_UPDATE_CLAUSES (stmt) = clauses;
15167 SET_EXPR_LOCATION (stmt, loc);
15168 add_stmt (stmt);
15171 /* OpenACC 2.0:
15172 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
15174 LOC is the location of the #pragma token.
15177 #define OACC_WAIT_CLAUSE_MASK \
15178 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) )
15180 static tree
15181 c_parser_oacc_wait (location_t loc, c_parser *parser, char *p_name)
15183 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
15185 if (c_parser_peek_token (parser)->type == CPP_OPEN_PAREN)
15186 list = c_parser_oacc_wait_list (parser, loc, list);
15188 strcpy (p_name, " wait");
15189 clauses = c_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK, p_name);
15190 stmt = c_finish_oacc_wait (loc, list, clauses);
15191 add_stmt (stmt);
15193 return stmt;
15196 /* OpenMP 2.5:
15197 # pragma omp atomic new-line
15198 expression-stmt
15200 expression-stmt:
15201 x binop= expr | x++ | ++x | x-- | --x
15202 binop:
15203 +, *, -, /, &, ^, |, <<, >>
15205 where x is an lvalue expression with scalar type.
15207 OpenMP 3.1:
15208 # pragma omp atomic new-line
15209 update-stmt
15211 # pragma omp atomic read new-line
15212 read-stmt
15214 # pragma omp atomic write new-line
15215 write-stmt
15217 # pragma omp atomic update new-line
15218 update-stmt
15220 # pragma omp atomic capture new-line
15221 capture-stmt
15223 # pragma omp atomic capture new-line
15224 capture-block
15226 read-stmt:
15227 v = x
15228 write-stmt:
15229 x = expr
15230 update-stmt:
15231 expression-stmt | x = x binop expr
15232 capture-stmt:
15233 v = expression-stmt
15234 capture-block:
15235 { v = x; update-stmt; } | { update-stmt; v = x; }
15237 OpenMP 4.0:
15238 update-stmt:
15239 expression-stmt | x = x binop expr | x = expr binop x
15240 capture-stmt:
15241 v = update-stmt
15242 capture-block:
15243 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
15245 where x and v are lvalue expressions with scalar type.
15247 LOC is the location of the #pragma token. */
15249 static void
15250 c_parser_omp_atomic (location_t loc, c_parser *parser)
15252 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE;
15253 tree lhs1 = NULL_TREE, rhs1 = NULL_TREE;
15254 tree stmt, orig_lhs, unfolded_lhs = NULL_TREE, unfolded_lhs1 = NULL_TREE;
15255 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
15256 struct c_expr expr;
15257 location_t eloc;
15258 bool structured_block = false;
15259 bool swapped = false;
15260 bool seq_cst = false;
15261 bool non_lvalue_p;
15263 if (c_parser_next_token_is (parser, CPP_NAME))
15265 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15266 if (!strcmp (p, "seq_cst"))
15268 seq_cst = true;
15269 c_parser_consume_token (parser);
15270 if (c_parser_next_token_is (parser, CPP_COMMA)
15271 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15272 c_parser_consume_token (parser);
15275 if (c_parser_next_token_is (parser, CPP_NAME))
15277 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15279 if (!strcmp (p, "read"))
15280 code = OMP_ATOMIC_READ;
15281 else if (!strcmp (p, "write"))
15282 code = NOP_EXPR;
15283 else if (!strcmp (p, "update"))
15284 code = OMP_ATOMIC;
15285 else if (!strcmp (p, "capture"))
15286 code = OMP_ATOMIC_CAPTURE_NEW;
15287 else
15288 p = NULL;
15289 if (p)
15290 c_parser_consume_token (parser);
15292 if (!seq_cst)
15294 if (c_parser_next_token_is (parser, CPP_COMMA)
15295 && c_parser_peek_2nd_token (parser)->type == CPP_NAME)
15296 c_parser_consume_token (parser);
15298 if (c_parser_next_token_is (parser, CPP_NAME))
15300 const char *p
15301 = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
15302 if (!strcmp (p, "seq_cst"))
15304 seq_cst = true;
15305 c_parser_consume_token (parser);
15309 c_parser_skip_to_pragma_eol (parser);
15311 switch (code)
15313 case OMP_ATOMIC_READ:
15314 case NOP_EXPR: /* atomic write */
15315 v = c_parser_cast_expression (parser, NULL).value;
15316 non_lvalue_p = !lvalue_p (v);
15317 v = c_fully_fold (v, false, NULL, true);
15318 if (v == error_mark_node)
15319 goto saw_error;
15320 if (non_lvalue_p)
15321 v = non_lvalue (v);
15322 loc = c_parser_peek_token (parser)->location;
15323 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15324 goto saw_error;
15325 if (code == NOP_EXPR)
15327 lhs = c_parser_expression (parser).value;
15328 lhs = c_fully_fold (lhs, false, NULL);
15329 if (lhs == error_mark_node)
15330 goto saw_error;
15332 else
15334 lhs = c_parser_cast_expression (parser, NULL).value;
15335 non_lvalue_p = !lvalue_p (lhs);
15336 lhs = c_fully_fold (lhs, false, NULL, true);
15337 if (lhs == error_mark_node)
15338 goto saw_error;
15339 if (non_lvalue_p)
15340 lhs = non_lvalue (lhs);
15342 if (code == NOP_EXPR)
15344 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
15345 opcode. */
15346 code = OMP_ATOMIC;
15347 rhs = lhs;
15348 lhs = v;
15349 v = NULL_TREE;
15351 goto done;
15352 case OMP_ATOMIC_CAPTURE_NEW:
15353 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15355 c_parser_consume_token (parser);
15356 structured_block = true;
15358 else
15360 v = c_parser_cast_expression (parser, NULL).value;
15361 non_lvalue_p = !lvalue_p (v);
15362 v = c_fully_fold (v, false, NULL, true);
15363 if (v == error_mark_node)
15364 goto saw_error;
15365 if (non_lvalue_p)
15366 v = non_lvalue (v);
15367 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15368 goto saw_error;
15370 break;
15371 default:
15372 break;
15375 /* For structured_block case we don't know yet whether
15376 old or new x should be captured. */
15377 restart:
15378 eloc = c_parser_peek_token (parser)->location;
15379 expr = c_parser_cast_expression (parser, NULL);
15380 lhs = expr.value;
15381 expr = default_function_array_conversion (eloc, expr);
15382 unfolded_lhs = expr.value;
15383 lhs = c_fully_fold (lhs, false, NULL, true);
15384 orig_lhs = lhs;
15385 switch (TREE_CODE (lhs))
15387 case ERROR_MARK:
15388 saw_error:
15389 c_parser_skip_to_end_of_block_or_statement (parser);
15390 if (structured_block)
15392 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15393 c_parser_consume_token (parser);
15394 else if (code == OMP_ATOMIC_CAPTURE_NEW)
15396 c_parser_skip_to_end_of_block_or_statement (parser);
15397 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15398 c_parser_consume_token (parser);
15401 return;
15403 case POSTINCREMENT_EXPR:
15404 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15405 code = OMP_ATOMIC_CAPTURE_OLD;
15406 /* FALLTHROUGH */
15407 case PREINCREMENT_EXPR:
15408 lhs = TREE_OPERAND (lhs, 0);
15409 unfolded_lhs = NULL_TREE;
15410 opcode = PLUS_EXPR;
15411 rhs = integer_one_node;
15412 break;
15414 case POSTDECREMENT_EXPR:
15415 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
15416 code = OMP_ATOMIC_CAPTURE_OLD;
15417 /* FALLTHROUGH */
15418 case PREDECREMENT_EXPR:
15419 lhs = TREE_OPERAND (lhs, 0);
15420 unfolded_lhs = NULL_TREE;
15421 opcode = MINUS_EXPR;
15422 rhs = integer_one_node;
15423 break;
15425 case COMPOUND_EXPR:
15426 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
15427 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
15428 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
15429 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
15430 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
15431 (TREE_OPERAND (lhs, 1), 0), 0)))
15432 == BOOLEAN_TYPE)
15433 /* Undo effects of boolean_increment for post {in,de}crement. */
15434 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
15435 /* FALLTHRU */
15436 case MODIFY_EXPR:
15437 if (TREE_CODE (lhs) == MODIFY_EXPR
15438 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
15440 /* Undo effects of boolean_increment. */
15441 if (integer_onep (TREE_OPERAND (lhs, 1)))
15443 /* This is pre or post increment. */
15444 rhs = TREE_OPERAND (lhs, 1);
15445 lhs = TREE_OPERAND (lhs, 0);
15446 unfolded_lhs = NULL_TREE;
15447 opcode = NOP_EXPR;
15448 if (code == OMP_ATOMIC_CAPTURE_NEW
15449 && !structured_block
15450 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15451 code = OMP_ATOMIC_CAPTURE_OLD;
15452 break;
15454 if (TREE_CODE (TREE_OPERAND (lhs, 1)) == TRUTH_NOT_EXPR
15455 && TREE_OPERAND (lhs, 0)
15456 == TREE_OPERAND (TREE_OPERAND (lhs, 1), 0))
15458 /* This is pre or post decrement. */
15459 rhs = TREE_OPERAND (lhs, 1);
15460 lhs = TREE_OPERAND (lhs, 0);
15461 unfolded_lhs = NULL_TREE;
15462 opcode = NOP_EXPR;
15463 if (code == OMP_ATOMIC_CAPTURE_NEW
15464 && !structured_block
15465 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
15466 code = OMP_ATOMIC_CAPTURE_OLD;
15467 break;
15470 /* FALLTHRU */
15471 default:
15472 if (!lvalue_p (unfolded_lhs))
15473 lhs = non_lvalue (lhs);
15474 switch (c_parser_peek_token (parser)->type)
15476 case CPP_MULT_EQ:
15477 opcode = MULT_EXPR;
15478 break;
15479 case CPP_DIV_EQ:
15480 opcode = TRUNC_DIV_EXPR;
15481 break;
15482 case CPP_PLUS_EQ:
15483 opcode = PLUS_EXPR;
15484 break;
15485 case CPP_MINUS_EQ:
15486 opcode = MINUS_EXPR;
15487 break;
15488 case CPP_LSHIFT_EQ:
15489 opcode = LSHIFT_EXPR;
15490 break;
15491 case CPP_RSHIFT_EQ:
15492 opcode = RSHIFT_EXPR;
15493 break;
15494 case CPP_AND_EQ:
15495 opcode = BIT_AND_EXPR;
15496 break;
15497 case CPP_OR_EQ:
15498 opcode = BIT_IOR_EXPR;
15499 break;
15500 case CPP_XOR_EQ:
15501 opcode = BIT_XOR_EXPR;
15502 break;
15503 case CPP_EQ:
15504 c_parser_consume_token (parser);
15505 eloc = c_parser_peek_token (parser)->location;
15506 expr = c_parser_expr_no_commas (parser, NULL, unfolded_lhs);
15507 rhs1 = expr.value;
15508 switch (TREE_CODE (rhs1))
15510 case MULT_EXPR:
15511 case TRUNC_DIV_EXPR:
15512 case RDIV_EXPR:
15513 case PLUS_EXPR:
15514 case MINUS_EXPR:
15515 case LSHIFT_EXPR:
15516 case RSHIFT_EXPR:
15517 case BIT_AND_EXPR:
15518 case BIT_IOR_EXPR:
15519 case BIT_XOR_EXPR:
15520 if (c_tree_equal (TREE_OPERAND (rhs1, 0), unfolded_lhs))
15522 opcode = TREE_CODE (rhs1);
15523 rhs = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15524 true);
15525 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15526 true);
15527 goto stmt_done;
15529 if (c_tree_equal (TREE_OPERAND (rhs1, 1), unfolded_lhs))
15531 opcode = TREE_CODE (rhs1);
15532 rhs = c_fully_fold (TREE_OPERAND (rhs1, 0), false, NULL,
15533 true);
15534 rhs1 = c_fully_fold (TREE_OPERAND (rhs1, 1), false, NULL,
15535 true);
15536 swapped = !commutative_tree_code (opcode);
15537 goto stmt_done;
15539 break;
15540 case ERROR_MARK:
15541 goto saw_error;
15542 default:
15543 break;
15545 if (c_parser_peek_token (parser)->type == CPP_SEMICOLON)
15547 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15549 code = OMP_ATOMIC_CAPTURE_OLD;
15550 v = lhs;
15551 lhs = NULL_TREE;
15552 expr = default_function_array_read_conversion (eloc, expr);
15553 unfolded_lhs1 = expr.value;
15554 lhs1 = c_fully_fold (unfolded_lhs1, false, NULL, true);
15555 rhs1 = NULL_TREE;
15556 c_parser_consume_token (parser);
15557 goto restart;
15559 if (structured_block)
15561 opcode = NOP_EXPR;
15562 expr = default_function_array_read_conversion (eloc, expr);
15563 rhs = c_fully_fold (expr.value, false, NULL, true);
15564 rhs1 = NULL_TREE;
15565 goto stmt_done;
15568 c_parser_error (parser, "invalid form of %<#pragma omp atomic%>");
15569 goto saw_error;
15570 default:
15571 c_parser_error (parser,
15572 "invalid operator for %<#pragma omp atomic%>");
15573 goto saw_error;
15576 /* Arrange to pass the location of the assignment operator to
15577 c_finish_omp_atomic. */
15578 loc = c_parser_peek_token (parser)->location;
15579 c_parser_consume_token (parser);
15580 eloc = c_parser_peek_token (parser)->location;
15581 expr = c_parser_expression (parser);
15582 expr = default_function_array_read_conversion (eloc, expr);
15583 rhs = expr.value;
15584 rhs = c_fully_fold (rhs, false, NULL, true);
15585 break;
15587 stmt_done:
15588 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
15590 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>"))
15591 goto saw_error;
15592 v = c_parser_cast_expression (parser, NULL).value;
15593 non_lvalue_p = !lvalue_p (v);
15594 v = c_fully_fold (v, false, NULL, true);
15595 if (v == error_mark_node)
15596 goto saw_error;
15597 if (non_lvalue_p)
15598 v = non_lvalue (v);
15599 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
15600 goto saw_error;
15601 eloc = c_parser_peek_token (parser)->location;
15602 expr = c_parser_cast_expression (parser, NULL);
15603 lhs1 = expr.value;
15604 expr = default_function_array_read_conversion (eloc, expr);
15605 unfolded_lhs1 = expr.value;
15606 lhs1 = c_fully_fold (lhs1, false, NULL, true);
15607 if (lhs1 == error_mark_node)
15608 goto saw_error;
15609 if (!lvalue_p (unfolded_lhs1))
15610 lhs1 = non_lvalue (lhs1);
15612 if (structured_block)
15614 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15615 c_parser_require (parser, CPP_CLOSE_BRACE, "expected %<}%>");
15617 done:
15618 if (unfolded_lhs && unfolded_lhs1
15619 && !c_tree_equal (unfolded_lhs, unfolded_lhs1))
15621 error ("%<#pragma omp atomic capture%> uses two different "
15622 "expressions for memory");
15623 stmt = error_mark_node;
15625 else
15626 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs, v, lhs1, rhs1,
15627 swapped, seq_cst);
15628 if (stmt != error_mark_node)
15629 add_stmt (stmt);
15631 if (!structured_block)
15632 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15636 /* OpenMP 2.5:
15637 # pragma omp barrier new-line
15640 static void
15641 c_parser_omp_barrier (c_parser *parser)
15643 location_t loc = c_parser_peek_token (parser)->location;
15644 c_parser_consume_pragma (parser);
15645 c_parser_skip_to_pragma_eol (parser);
15647 c_finish_omp_barrier (loc);
15650 /* OpenMP 2.5:
15651 # pragma omp critical [(name)] new-line
15652 structured-block
15654 OpenMP 4.5:
15655 # pragma omp critical [(name) [hint(expression)]] new-line
15657 LOC is the location of the #pragma itself. */
15659 #define OMP_CRITICAL_CLAUSE_MASK \
15660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
15662 static tree
15663 c_parser_omp_critical (location_t loc, c_parser *parser, bool *if_p)
15665 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
15667 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15669 c_parser_consume_token (parser);
15670 if (c_parser_next_token_is (parser, CPP_NAME))
15672 name = c_parser_peek_token (parser)->value;
15673 c_parser_consume_token (parser);
15674 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>");
15676 else
15677 c_parser_error (parser, "expected identifier");
15679 clauses = c_parser_omp_all_clauses (parser,
15680 OMP_CRITICAL_CLAUSE_MASK,
15681 "#pragma omp critical");
15683 else
15685 if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15686 c_parser_error (parser, "expected %<(%> or end of line");
15687 c_parser_skip_to_pragma_eol (parser);
15690 stmt = c_parser_omp_structured_block (parser, if_p);
15691 return c_finish_omp_critical (loc, stmt, name, clauses);
15694 /* OpenMP 2.5:
15695 # pragma omp flush flush-vars[opt] new-line
15697 flush-vars:
15698 ( variable-list ) */
15700 static void
15701 c_parser_omp_flush (c_parser *parser)
15703 location_t loc = c_parser_peek_token (parser)->location;
15704 c_parser_consume_pragma (parser);
15705 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
15706 c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
15707 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
15708 c_parser_error (parser, "expected %<(%> or end of line");
15709 c_parser_skip_to_pragma_eol (parser);
15711 c_finish_omp_flush (loc);
15714 /* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
15715 The real trick here is to determine the loop control variable early
15716 so that we can push a new decl if necessary to make it private.
15717 LOC is the location of the "acc" or "omp" in "#pragma acc" or "#pragma omp",
15718 respectively. */
15720 static tree
15721 c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
15722 tree clauses, tree *cclauses, bool *if_p)
15724 tree decl, cond, incr, save_break, save_cont, body, init, stmt, cl;
15725 tree declv, condv, incrv, initv, ret = NULL_TREE;
15726 tree pre_body = NULL_TREE, this_pre_body;
15727 tree ordered_cl = NULL_TREE;
15728 bool fail = false, open_brace_parsed = false;
15729 int i, collapse = 1, ordered = 0, count, nbraces = 0;
15730 location_t for_loc;
15731 bool tiling = false;
15732 vec<tree, va_gc> *for_block = make_tree_vector ();
15734 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
15735 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
15736 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
15737 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
15739 tiling = true;
15740 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
15742 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
15743 && OMP_CLAUSE_ORDERED_EXPR (cl))
15745 ordered_cl = cl;
15746 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
15749 if (ordered && ordered < collapse)
15751 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
15752 "%<ordered%> clause parameter is less than %<collapse%>");
15753 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
15754 = build_int_cst (NULL_TREE, collapse);
15755 ordered = collapse;
15757 if (ordered)
15759 for (tree *pc = &clauses; *pc; )
15760 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
15762 error_at (OMP_CLAUSE_LOCATION (*pc),
15763 "%<linear%> clause may not be specified together "
15764 "with %<ordered%> clause with a parameter");
15765 *pc = OMP_CLAUSE_CHAIN (*pc);
15767 else
15768 pc = &OMP_CLAUSE_CHAIN (*pc);
15771 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
15772 count = ordered ? ordered : collapse;
15774 declv = make_tree_vec (count);
15775 initv = make_tree_vec (count);
15776 condv = make_tree_vec (count);
15777 incrv = make_tree_vec (count);
15779 if (!c_parser_next_token_is_keyword (parser, RID_FOR))
15781 c_parser_error (parser, "for statement expected");
15782 return NULL;
15784 for_loc = c_parser_peek_token (parser)->location;
15785 c_parser_consume_token (parser);
15787 for (i = 0; i < count; i++)
15789 int bracecount = 0;
15791 matching_parens parens;
15792 if (!parens.require_open (parser))
15793 goto pop_scopes;
15795 /* Parse the initialization declaration or expression. */
15796 if (c_parser_next_tokens_start_declaration (parser))
15798 if (i > 0)
15799 vec_safe_push (for_block, c_begin_compound_stmt (true));
15800 this_pre_body = push_stmt_list ();
15801 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
15802 NULL, vNULL);
15803 if (this_pre_body)
15805 this_pre_body = pop_stmt_list (this_pre_body);
15806 if (pre_body)
15808 tree t = pre_body;
15809 pre_body = push_stmt_list ();
15810 add_stmt (t);
15811 add_stmt (this_pre_body);
15812 pre_body = pop_stmt_list (pre_body);
15814 else
15815 pre_body = this_pre_body;
15817 decl = check_for_loop_decls (for_loc, flag_isoc99);
15818 if (decl == NULL)
15819 goto error_init;
15820 if (DECL_INITIAL (decl) == error_mark_node)
15821 decl = error_mark_node;
15822 init = decl;
15824 else if (c_parser_next_token_is (parser, CPP_NAME)
15825 && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
15827 struct c_expr decl_exp;
15828 struct c_expr init_exp;
15829 location_t init_loc;
15831 decl_exp = c_parser_postfix_expression (parser);
15832 decl = decl_exp.value;
15834 c_parser_require (parser, CPP_EQ, "expected %<=%>");
15836 init_loc = c_parser_peek_token (parser)->location;
15837 init_exp = c_parser_expr_no_commas (parser, NULL);
15838 init_exp = default_function_array_read_conversion (init_loc,
15839 init_exp);
15840 init = build_modify_expr (init_loc, decl, decl_exp.original_type,
15841 NOP_EXPR, init_loc, init_exp.value,
15842 init_exp.original_type);
15843 init = c_process_expr_stmt (init_loc, init);
15845 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15847 else
15849 error_init:
15850 c_parser_error (parser,
15851 "expected iteration declaration or initialization");
15852 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
15853 "expected %<)%>");
15854 fail = true;
15855 goto parse_next;
15858 /* Parse the loop condition. */
15859 cond = NULL_TREE;
15860 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
15862 location_t cond_loc = c_parser_peek_token (parser)->location;
15863 struct c_expr cond_expr
15864 = c_parser_binary_expression (parser, NULL, NULL_TREE);
15866 cond = cond_expr.value;
15867 cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
15868 if (COMPARISON_CLASS_P (cond))
15870 tree op0 = TREE_OPERAND (cond, 0), op1 = TREE_OPERAND (cond, 1);
15871 op0 = c_fully_fold (op0, false, NULL);
15872 op1 = c_fully_fold (op1, false, NULL);
15873 TREE_OPERAND (cond, 0) = op0;
15874 TREE_OPERAND (cond, 1) = op1;
15876 switch (cond_expr.original_code)
15878 case GT_EXPR:
15879 case GE_EXPR:
15880 case LT_EXPR:
15881 case LE_EXPR:
15882 break;
15883 default:
15884 /* Can't be cond = error_mark_node, because we want to preserve
15885 the location until c_finish_omp_for. */
15886 cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
15887 break;
15889 protected_set_expr_location (cond, cond_loc);
15891 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
15893 /* Parse the increment expression. */
15894 incr = NULL_TREE;
15895 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
15897 location_t incr_loc = c_parser_peek_token (parser)->location;
15899 incr = c_process_expr_stmt (incr_loc,
15900 c_parser_expression (parser).value);
15902 parens.skip_until_found_close (parser);
15904 if (decl == NULL || decl == error_mark_node || init == error_mark_node)
15905 fail = true;
15906 else
15908 TREE_VEC_ELT (declv, i) = decl;
15909 TREE_VEC_ELT (initv, i) = init;
15910 TREE_VEC_ELT (condv, i) = cond;
15911 TREE_VEC_ELT (incrv, i) = incr;
15914 parse_next:
15915 if (i == count - 1)
15916 break;
15918 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
15919 in between the collapsed for loops to be still considered perfectly
15920 nested. Hopefully the final version clarifies this.
15921 For now handle (multiple) {'s and empty statements. */
15924 if (c_parser_next_token_is_keyword (parser, RID_FOR))
15926 c_parser_consume_token (parser);
15927 break;
15929 else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
15931 c_parser_consume_token (parser);
15932 bracecount++;
15934 else if (bracecount
15935 && c_parser_next_token_is (parser, CPP_SEMICOLON))
15936 c_parser_consume_token (parser);
15937 else
15939 c_parser_error (parser, "not enough perfectly nested loops");
15940 if (bracecount)
15942 open_brace_parsed = true;
15943 bracecount--;
15945 fail = true;
15946 count = 0;
15947 break;
15950 while (1);
15952 nbraces += bracecount;
15955 if (nbraces)
15956 if_p = NULL;
15958 save_break = c_break_label;
15959 c_break_label = size_one_node;
15960 save_cont = c_cont_label;
15961 c_cont_label = NULL_TREE;
15962 body = push_stmt_list ();
15964 if (open_brace_parsed)
15966 location_t here = c_parser_peek_token (parser)->location;
15967 stmt = c_begin_compound_stmt (true);
15968 c_parser_compound_statement_nostart (parser);
15969 add_stmt (c_end_compound_stmt (here, stmt, true));
15971 else
15972 add_stmt (c_parser_c99_block_statement (parser, if_p));
15973 if (c_cont_label)
15975 tree t = build1 (LABEL_EXPR, void_type_node, c_cont_label);
15976 SET_EXPR_LOCATION (t, loc);
15977 add_stmt (t);
15980 body = pop_stmt_list (body);
15981 c_break_label = save_break;
15982 c_cont_label = save_cont;
15984 while (nbraces)
15986 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
15988 c_parser_consume_token (parser);
15989 nbraces--;
15991 else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
15992 c_parser_consume_token (parser);
15993 else
15995 c_parser_error (parser, "collapsed loops not perfectly nested");
15996 while (nbraces)
15998 location_t here = c_parser_peek_token (parser)->location;
15999 stmt = c_begin_compound_stmt (true);
16000 add_stmt (body);
16001 c_parser_compound_statement_nostart (parser);
16002 body = c_end_compound_stmt (here, stmt, true);
16003 nbraces--;
16005 goto pop_scopes;
16009 /* Only bother calling c_finish_omp_for if we haven't already generated
16010 an error from the initialization parsing. */
16011 if (!fail)
16013 stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
16014 incrv, body, pre_body);
16016 /* Check for iterators appearing in lb, b or incr expressions. */
16017 if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
16018 stmt = NULL_TREE;
16020 if (stmt)
16022 add_stmt (stmt);
16024 if (cclauses != NULL
16025 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL)
16027 tree *c;
16028 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
16029 if (OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_FIRSTPRIVATE
16030 && OMP_CLAUSE_CODE (*c) != OMP_CLAUSE_LASTPRIVATE)
16031 c = &OMP_CLAUSE_CHAIN (*c);
16032 else
16034 for (i = 0; i < count; i++)
16035 if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
16036 break;
16037 if (i == count)
16038 c = &OMP_CLAUSE_CHAIN (*c);
16039 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE)
16041 error_at (loc,
16042 "iteration variable %qD should not be firstprivate",
16043 OMP_CLAUSE_DECL (*c));
16044 *c = OMP_CLAUSE_CHAIN (*c);
16046 else
16048 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16049 tree l = *c;
16050 *c = OMP_CLAUSE_CHAIN (*c);
16051 if (code == OMP_SIMD)
16053 OMP_CLAUSE_CHAIN (l)
16054 = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16055 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
16057 else
16059 OMP_CLAUSE_CHAIN (l) = clauses;
16060 clauses = l;
16065 OMP_FOR_CLAUSES (stmt) = clauses;
16067 ret = stmt;
16069 pop_scopes:
16070 while (!for_block->is_empty ())
16072 /* FIXME diagnostics: LOC below should be the actual location of
16073 this particular for block. We need to build a list of
16074 locations to go along with FOR_BLOCK. */
16075 stmt = c_end_compound_stmt (loc, for_block->pop (), true);
16076 add_stmt (stmt);
16078 release_tree_vector (for_block);
16079 return ret;
16082 /* Helper function for OpenMP parsing, split clauses and call
16083 finish_omp_clauses on each of the set of clauses afterwards. */
16085 static void
16086 omp_split_clauses (location_t loc, enum tree_code code,
16087 omp_clause_mask mask, tree clauses, tree *cclauses)
16089 int i;
16090 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
16091 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
16092 if (cclauses[i])
16093 cclauses[i] = c_finish_omp_clauses (cclauses[i], C_ORT_OMP);
16096 /* OpenMP 4.0:
16097 #pragma omp simd simd-clause[optseq] new-line
16098 for-loop
16100 LOC is the location of the #pragma token.
16103 #define OMP_SIMD_CLAUSE_MASK \
16104 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
16105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
16106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
16108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16113 static tree
16114 c_parser_omp_simd (location_t loc, c_parser *parser,
16115 char *p_name, omp_clause_mask mask, tree *cclauses,
16116 bool *if_p)
16118 tree block, clauses, ret;
16120 strcat (p_name, " simd");
16121 mask |= OMP_SIMD_CLAUSE_MASK;
16123 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16124 if (cclauses)
16126 omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
16127 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
16128 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
16129 OMP_CLAUSE_ORDERED);
16130 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
16132 error_at (OMP_CLAUSE_LOCATION (c),
16133 "%<ordered%> clause with parameter may not be specified "
16134 "on %qs construct", p_name);
16135 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
16139 block = c_begin_compound_stmt (true);
16140 ret = c_parser_omp_for_loop (loc, parser, OMP_SIMD, clauses, cclauses, if_p);
16141 block = c_end_compound_stmt (loc, block, true);
16142 add_stmt (block);
16144 return ret;
16147 /* OpenMP 2.5:
16148 #pragma omp for for-clause[optseq] new-line
16149 for-loop
16151 OpenMP 4.0:
16152 #pragma omp for simd for-simd-clause[optseq] new-line
16153 for-loop
16155 LOC is the location of the #pragma token.
16158 #define OMP_FOR_CLAUSE_MASK \
16159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
16163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
16165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
16166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
16167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16169 static tree
16170 c_parser_omp_for (location_t loc, c_parser *parser,
16171 char *p_name, omp_clause_mask mask, tree *cclauses,
16172 bool *if_p)
16174 tree block, clauses, ret;
16176 strcat (p_name, " for");
16177 mask |= OMP_FOR_CLAUSE_MASK;
16178 /* parallel for{, simd} disallows nowait clause, but for
16179 target {teams distribute ,}parallel for{, simd} it should be accepted. */
16180 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
16181 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16182 /* Composite distribute parallel for{, simd} disallows ordered clause. */
16183 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16184 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
16186 if (c_parser_next_token_is (parser, CPP_NAME))
16188 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16190 if (strcmp (p, "simd") == 0)
16192 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16193 if (cclauses == NULL)
16194 cclauses = cclauses_buf;
16196 c_parser_consume_token (parser);
16197 if (!flag_openmp) /* flag_openmp_simd */
16198 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16199 if_p);
16200 block = c_begin_compound_stmt (true);
16201 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
16202 block = c_end_compound_stmt (loc, block, true);
16203 if (ret == NULL_TREE)
16204 return ret;
16205 ret = make_node (OMP_FOR);
16206 TREE_TYPE (ret) = void_type_node;
16207 OMP_FOR_BODY (ret) = block;
16208 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16209 SET_EXPR_LOCATION (ret, loc);
16210 add_stmt (ret);
16211 return ret;
16214 if (!flag_openmp) /* flag_openmp_simd */
16216 c_parser_skip_to_pragma_eol (parser, false);
16217 return NULL_TREE;
16220 /* Composite distribute parallel for disallows linear clause. */
16221 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16222 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
16224 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16225 if (cclauses)
16227 omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
16228 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
16231 block = c_begin_compound_stmt (true);
16232 ret = c_parser_omp_for_loop (loc, parser, OMP_FOR, clauses, cclauses, if_p);
16233 block = c_end_compound_stmt (loc, block, true);
16234 add_stmt (block);
16236 return ret;
16239 /* OpenMP 2.5:
16240 # pragma omp master new-line
16241 structured-block
16243 LOC is the location of the #pragma token.
16246 static tree
16247 c_parser_omp_master (location_t loc, c_parser *parser, bool *if_p)
16249 c_parser_skip_to_pragma_eol (parser);
16250 return c_finish_omp_master (loc, c_parser_omp_structured_block (parser,
16251 if_p));
16254 /* OpenMP 2.5:
16255 # pragma omp ordered new-line
16256 structured-block
16258 OpenMP 4.5:
16259 # pragma omp ordered ordered-clauses new-line
16260 structured-block
16262 # pragma omp ordered depend-clauses new-line */
16264 #define OMP_ORDERED_CLAUSE_MASK \
16265 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
16266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
16268 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
16269 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
16271 static bool
16272 c_parser_omp_ordered (c_parser *parser, enum pragma_context context,
16273 bool *if_p)
16275 location_t loc = c_parser_peek_token (parser)->location;
16276 c_parser_consume_pragma (parser);
16278 if (context != pragma_stmt && context != pragma_compound)
16280 c_parser_error (parser, "expected declaration specifiers");
16281 c_parser_skip_to_pragma_eol (parser, false);
16282 return false;
16285 if (c_parser_next_token_is (parser, CPP_NAME))
16287 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16289 if (!strcmp ("depend", p))
16291 if (!flag_openmp) /* flag_openmp_simd */
16293 c_parser_skip_to_pragma_eol (parser, false);
16294 return false;
16296 if (context == pragma_stmt)
16298 error_at (loc,
16299 "%<#pragma omp ordered%> with %<depend%> clause may "
16300 "only be used in compound statements");
16301 c_parser_skip_to_pragma_eol (parser, false);
16302 return false;
16305 tree clauses
16306 = c_parser_omp_all_clauses (parser,
16307 OMP_ORDERED_DEPEND_CLAUSE_MASK,
16308 "#pragma omp ordered");
16309 c_finish_omp_ordered (loc, clauses, NULL_TREE);
16310 return false;
16314 tree clauses = c_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
16315 "#pragma omp ordered");
16317 if (!flag_openmp /* flag_openmp_simd */
16318 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
16319 return false;
16321 c_finish_omp_ordered (loc, clauses,
16322 c_parser_omp_structured_block (parser, if_p));
16323 return true;
16326 /* OpenMP 2.5:
16328 section-scope:
16329 { section-sequence }
16331 section-sequence:
16332 section-directive[opt] structured-block
16333 section-sequence section-directive structured-block
16335 SECTIONS_LOC is the location of the #pragma omp sections. */
16337 static tree
16338 c_parser_omp_sections_scope (location_t sections_loc, c_parser *parser)
16340 tree stmt, substmt;
16341 bool error_suppress = false;
16342 location_t loc;
16344 loc = c_parser_peek_token (parser)->location;
16345 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
16347 /* Avoid skipping until the end of the block. */
16348 parser->error = false;
16349 return NULL_TREE;
16352 stmt = push_stmt_list ();
16354 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION)
16356 substmt = c_parser_omp_structured_block (parser, NULL);
16357 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16358 SET_EXPR_LOCATION (substmt, loc);
16359 add_stmt (substmt);
16362 while (1)
16364 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
16365 break;
16366 if (c_parser_next_token_is (parser, CPP_EOF))
16367 break;
16369 loc = c_parser_peek_token (parser)->location;
16370 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION)
16372 c_parser_consume_pragma (parser);
16373 c_parser_skip_to_pragma_eol (parser);
16374 error_suppress = false;
16376 else if (!error_suppress)
16378 error_at (loc, "expected %<#pragma omp section%> or %<}%>");
16379 error_suppress = true;
16382 substmt = c_parser_omp_structured_block (parser, NULL);
16383 substmt = build1 (OMP_SECTION, void_type_node, substmt);
16384 SET_EXPR_LOCATION (substmt, loc);
16385 add_stmt (substmt);
16387 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE,
16388 "expected %<#pragma omp section%> or %<}%>");
16390 substmt = pop_stmt_list (stmt);
16392 stmt = make_node (OMP_SECTIONS);
16393 SET_EXPR_LOCATION (stmt, sections_loc);
16394 TREE_TYPE (stmt) = void_type_node;
16395 OMP_SECTIONS_BODY (stmt) = substmt;
16397 return add_stmt (stmt);
16400 /* OpenMP 2.5:
16401 # pragma omp sections sections-clause[optseq] newline
16402 sections-scope
16404 LOC is the location of the #pragma token.
16407 #define OMP_SECTIONS_CLAUSE_MASK \
16408 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16414 static tree
16415 c_parser_omp_sections (location_t loc, c_parser *parser,
16416 char *p_name, omp_clause_mask mask, tree *cclauses)
16418 tree block, clauses, ret;
16420 strcat (p_name, " sections");
16421 mask |= OMP_SECTIONS_CLAUSE_MASK;
16422 if (cclauses)
16423 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
16425 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16426 if (cclauses)
16428 omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
16429 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
16432 block = c_begin_compound_stmt (true);
16433 ret = c_parser_omp_sections_scope (loc, parser);
16434 if (ret)
16435 OMP_SECTIONS_CLAUSES (ret) = clauses;
16436 block = c_end_compound_stmt (loc, block, true);
16437 add_stmt (block);
16439 return ret;
16442 /* OpenMP 2.5:
16443 # pragma omp parallel parallel-clause[optseq] new-line
16444 structured-block
16445 # pragma omp parallel for parallel-for-clause[optseq] new-line
16446 structured-block
16447 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
16448 structured-block
16450 OpenMP 4.0:
16451 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
16452 structured-block
16454 LOC is the location of the #pragma token.
16457 #define OMP_PARALLEL_CLAUSE_MASK \
16458 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
16464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
16466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
16468 static tree
16469 c_parser_omp_parallel (location_t loc, c_parser *parser,
16470 char *p_name, omp_clause_mask mask, tree *cclauses,
16471 bool *if_p)
16473 tree stmt, clauses, block;
16475 strcat (p_name, " parallel");
16476 mask |= OMP_PARALLEL_CLAUSE_MASK;
16477 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
16478 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
16479 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
16480 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
16482 if (c_parser_next_token_is_keyword (parser, RID_FOR))
16484 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16485 if (cclauses == NULL)
16486 cclauses = cclauses_buf;
16488 c_parser_consume_token (parser);
16489 if (!flag_openmp) /* flag_openmp_simd */
16490 return c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16491 block = c_begin_omp_parallel ();
16492 tree ret = c_parser_omp_for (loc, parser, p_name, mask, cclauses, if_p);
16493 stmt
16494 = c_finish_omp_parallel (loc, cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16495 block);
16496 if (ret == NULL_TREE)
16497 return ret;
16498 OMP_PARALLEL_COMBINED (stmt) = 1;
16499 return stmt;
16501 /* When combined with distribute, parallel has to be followed by for.
16502 #pragma omp target parallel is allowed though. */
16503 else if (cclauses
16504 && (mask & (OMP_CLAUSE_MASK_1
16505 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
16507 error_at (loc, "expected %<for%> after %qs", p_name);
16508 c_parser_skip_to_pragma_eol (parser);
16509 return NULL_TREE;
16511 else if (!flag_openmp) /* flag_openmp_simd */
16513 c_parser_skip_to_pragma_eol (parser, false);
16514 return NULL_TREE;
16516 else if (cclauses == NULL && c_parser_next_token_is (parser, CPP_NAME))
16518 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16519 if (strcmp (p, "sections") == 0)
16521 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16522 if (cclauses == NULL)
16523 cclauses = cclauses_buf;
16525 c_parser_consume_token (parser);
16526 block = c_begin_omp_parallel ();
16527 c_parser_omp_sections (loc, parser, p_name, mask, cclauses);
16528 stmt = c_finish_omp_parallel (loc,
16529 cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
16530 block);
16531 OMP_PARALLEL_COMBINED (stmt) = 1;
16532 return stmt;
16536 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16537 if (cclauses)
16539 omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
16540 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
16543 block = c_begin_omp_parallel ();
16544 c_parser_statement (parser, if_p);
16545 stmt = c_finish_omp_parallel (loc, clauses, block);
16547 return stmt;
16550 /* OpenMP 2.5:
16551 # pragma omp single single-clause[optseq] new-line
16552 structured-block
16554 LOC is the location of the #pragma.
16557 #define OMP_SINGLE_CLAUSE_MASK \
16558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
16561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16563 static tree
16564 c_parser_omp_single (location_t loc, c_parser *parser, bool *if_p)
16566 tree stmt = make_node (OMP_SINGLE);
16567 SET_EXPR_LOCATION (stmt, loc);
16568 TREE_TYPE (stmt) = void_type_node;
16570 OMP_SINGLE_CLAUSES (stmt)
16571 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
16572 "#pragma omp single");
16573 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16575 return add_stmt (stmt);
16578 /* OpenMP 3.0:
16579 # pragma omp task task-clause[optseq] new-line
16581 LOC is the location of the #pragma.
16584 #define OMP_TASK_CLAUSE_MASK \
16585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
16587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
16588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
16592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
16593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
16596 static tree
16597 c_parser_omp_task (location_t loc, c_parser *parser, bool *if_p)
16599 tree clauses, block;
16601 clauses = c_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
16602 "#pragma omp task");
16604 block = c_begin_omp_task ();
16605 c_parser_statement (parser, if_p);
16606 return c_finish_omp_task (loc, clauses, block);
16609 /* OpenMP 3.0:
16610 # pragma omp taskwait new-line
16613 static void
16614 c_parser_omp_taskwait (c_parser *parser)
16616 location_t loc = c_parser_peek_token (parser)->location;
16617 c_parser_consume_pragma (parser);
16618 c_parser_skip_to_pragma_eol (parser);
16620 c_finish_omp_taskwait (loc);
16623 /* OpenMP 3.1:
16624 # pragma omp taskyield new-line
16627 static void
16628 c_parser_omp_taskyield (c_parser *parser)
16630 location_t loc = c_parser_peek_token (parser)->location;
16631 c_parser_consume_pragma (parser);
16632 c_parser_skip_to_pragma_eol (parser);
16634 c_finish_omp_taskyield (loc);
16637 /* OpenMP 4.0:
16638 # pragma omp taskgroup new-line
16641 static tree
16642 c_parser_omp_taskgroup (c_parser *parser, bool *if_p)
16644 location_t loc = c_parser_peek_token (parser)->location;
16645 c_parser_skip_to_pragma_eol (parser);
16646 return c_finish_omp_taskgroup (loc, c_parser_omp_structured_block (parser,
16647 if_p));
16650 /* OpenMP 4.0:
16651 # pragma omp cancel cancel-clause[optseq] new-line
16653 LOC is the location of the #pragma.
16656 #define OMP_CANCEL_CLAUSE_MASK \
16657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
16661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
16663 static void
16664 c_parser_omp_cancel (c_parser *parser)
16666 location_t loc = c_parser_peek_token (parser)->location;
16668 c_parser_consume_pragma (parser);
16669 tree clauses = c_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
16670 "#pragma omp cancel");
16672 c_finish_omp_cancel (loc, clauses);
16675 /* OpenMP 4.0:
16676 # pragma omp cancellation point cancelpt-clause[optseq] new-line
16678 LOC is the location of the #pragma.
16681 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
16682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
16683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
16684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
16685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
16687 static void
16688 c_parser_omp_cancellation_point (c_parser *parser, enum pragma_context context)
16690 location_t loc = c_parser_peek_token (parser)->location;
16691 tree clauses;
16692 bool point_seen = false;
16694 c_parser_consume_pragma (parser);
16695 if (c_parser_next_token_is (parser, CPP_NAME))
16697 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16698 if (strcmp (p, "point") == 0)
16700 c_parser_consume_token (parser);
16701 point_seen = true;
16704 if (!point_seen)
16706 c_parser_error (parser, "expected %<point%>");
16707 c_parser_skip_to_pragma_eol (parser);
16708 return;
16711 if (context != pragma_compound)
16713 if (context == pragma_stmt)
16714 error_at (loc,
16715 "%<#pragma %s%> may only be used in compound statements",
16716 "omp cancellation point");
16717 else
16718 c_parser_error (parser, "expected declaration specifiers");
16719 c_parser_skip_to_pragma_eol (parser, false);
16720 return;
16723 clauses
16724 = c_parser_omp_all_clauses (parser, OMP_CANCELLATION_POINT_CLAUSE_MASK,
16725 "#pragma omp cancellation point");
16727 c_finish_omp_cancellation_point (loc, clauses);
16730 /* OpenMP 4.0:
16731 #pragma omp distribute distribute-clause[optseq] new-line
16732 for-loop */
16734 #define OMP_DISTRIBUTE_CLAUSE_MASK \
16735 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
16738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
16739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
16741 static tree
16742 c_parser_omp_distribute (location_t loc, c_parser *parser,
16743 char *p_name, omp_clause_mask mask, tree *cclauses,
16744 bool *if_p)
16746 tree clauses, block, ret;
16748 strcat (p_name, " distribute");
16749 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
16751 if (c_parser_next_token_is (parser, CPP_NAME))
16753 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16754 bool simd = false;
16755 bool parallel = false;
16757 if (strcmp (p, "simd") == 0)
16758 simd = true;
16759 else
16760 parallel = strcmp (p, "parallel") == 0;
16761 if (parallel || simd)
16763 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16764 if (cclauses == NULL)
16765 cclauses = cclauses_buf;
16766 c_parser_consume_token (parser);
16767 if (!flag_openmp) /* flag_openmp_simd */
16769 if (simd)
16770 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16771 if_p);
16772 else
16773 return c_parser_omp_parallel (loc, parser, p_name, mask,
16774 cclauses, if_p);
16776 block = c_begin_compound_stmt (true);
16777 if (simd)
16778 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
16779 if_p);
16780 else
16781 ret = c_parser_omp_parallel (loc, parser, p_name, mask, cclauses,
16782 if_p);
16783 block = c_end_compound_stmt (loc, block, true);
16784 if (ret == NULL)
16785 return ret;
16786 ret = make_node (OMP_DISTRIBUTE);
16787 TREE_TYPE (ret) = void_type_node;
16788 OMP_FOR_BODY (ret) = block;
16789 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16790 SET_EXPR_LOCATION (ret, loc);
16791 add_stmt (ret);
16792 return ret;
16795 if (!flag_openmp) /* flag_openmp_simd */
16797 c_parser_skip_to_pragma_eol (parser, false);
16798 return NULL_TREE;
16801 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16802 if (cclauses)
16804 omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
16805 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
16808 block = c_begin_compound_stmt (true);
16809 ret = c_parser_omp_for_loop (loc, parser, OMP_DISTRIBUTE, clauses, NULL,
16810 if_p);
16811 block = c_end_compound_stmt (loc, block, true);
16812 add_stmt (block);
16814 return ret;
16817 /* OpenMP 4.0:
16818 # pragma omp teams teams-clause[optseq] new-line
16819 structured-block */
16821 #define OMP_TEAMS_CLAUSE_MASK \
16822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
16823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
16824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
16825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
16826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
16827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
16828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
16830 static tree
16831 c_parser_omp_teams (location_t loc, c_parser *parser,
16832 char *p_name, omp_clause_mask mask, tree *cclauses,
16833 bool *if_p)
16835 tree clauses, block, ret;
16837 strcat (p_name, " teams");
16838 mask |= OMP_TEAMS_CLAUSE_MASK;
16840 if (c_parser_next_token_is (parser, CPP_NAME))
16842 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
16843 if (strcmp (p, "distribute") == 0)
16845 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
16846 if (cclauses == NULL)
16847 cclauses = cclauses_buf;
16849 c_parser_consume_token (parser);
16850 if (!flag_openmp) /* flag_openmp_simd */
16851 return c_parser_omp_distribute (loc, parser, p_name, mask,
16852 cclauses, if_p);
16853 block = c_begin_compound_stmt (true);
16854 ret = c_parser_omp_distribute (loc, parser, p_name, mask, cclauses,
16855 if_p);
16856 block = c_end_compound_stmt (loc, block, true);
16857 if (ret == NULL)
16858 return ret;
16859 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16860 ret = make_node (OMP_TEAMS);
16861 TREE_TYPE (ret) = void_type_node;
16862 OMP_TEAMS_CLAUSES (ret) = clauses;
16863 OMP_TEAMS_BODY (ret) = block;
16864 OMP_TEAMS_COMBINED (ret) = 1;
16865 return add_stmt (ret);
16868 if (!flag_openmp) /* flag_openmp_simd */
16870 c_parser_skip_to_pragma_eol (parser, false);
16871 return NULL_TREE;
16874 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
16875 if (cclauses)
16877 omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
16878 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
16881 tree stmt = make_node (OMP_TEAMS);
16882 TREE_TYPE (stmt) = void_type_node;
16883 OMP_TEAMS_CLAUSES (stmt) = clauses;
16884 OMP_TEAMS_BODY (stmt) = c_parser_omp_structured_block (parser, if_p);
16886 return add_stmt (stmt);
16889 /* OpenMP 4.0:
16890 # pragma omp target data target-data-clause[optseq] new-line
16891 structured-block */
16893 #define OMP_TARGET_DATA_CLAUSE_MASK \
16894 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
16896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
16899 static tree
16900 c_parser_omp_target_data (location_t loc, c_parser *parser, bool *if_p)
16902 tree clauses
16903 = c_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
16904 "#pragma omp target data");
16905 int map_seen = 0;
16906 for (tree *pc = &clauses; *pc;)
16908 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
16909 switch (OMP_CLAUSE_MAP_KIND (*pc))
16911 case GOMP_MAP_TO:
16912 case GOMP_MAP_ALWAYS_TO:
16913 case GOMP_MAP_FROM:
16914 case GOMP_MAP_ALWAYS_FROM:
16915 case GOMP_MAP_TOFROM:
16916 case GOMP_MAP_ALWAYS_TOFROM:
16917 case GOMP_MAP_ALLOC:
16918 map_seen = 3;
16919 break;
16920 case GOMP_MAP_FIRSTPRIVATE_POINTER:
16921 case GOMP_MAP_ALWAYS_POINTER:
16922 break;
16923 default:
16924 map_seen |= 1;
16925 error_at (OMP_CLAUSE_LOCATION (*pc),
16926 "%<#pragma omp target data%> with map-type other "
16927 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
16928 "on %<map%> clause");
16929 *pc = OMP_CLAUSE_CHAIN (*pc);
16930 continue;
16932 pc = &OMP_CLAUSE_CHAIN (*pc);
16935 if (map_seen != 3)
16937 if (map_seen == 0)
16938 error_at (loc,
16939 "%<#pragma omp target data%> must contain at least "
16940 "one %<map%> clause");
16941 return NULL_TREE;
16944 tree stmt = make_node (OMP_TARGET_DATA);
16945 TREE_TYPE (stmt) = void_type_node;
16946 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
16947 keep_next_level ();
16948 tree block = c_begin_compound_stmt (true);
16949 add_stmt (c_parser_omp_structured_block (parser, if_p));
16950 OMP_TARGET_DATA_BODY (stmt) = c_end_compound_stmt (loc, block, true);
16952 SET_EXPR_LOCATION (stmt, loc);
16953 return add_stmt (stmt);
16956 /* OpenMP 4.0:
16957 # pragma omp target update target-update-clause[optseq] new-line */
16959 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
16960 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
16961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
16962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
16963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
16964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
16965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
16967 static bool
16968 c_parser_omp_target_update (location_t loc, c_parser *parser,
16969 enum pragma_context context)
16971 if (context == pragma_stmt)
16973 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
16974 "omp target update");
16975 c_parser_skip_to_pragma_eol (parser, false);
16976 return false;
16979 tree clauses
16980 = c_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
16981 "#pragma omp target update");
16982 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
16983 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
16985 error_at (loc,
16986 "%<#pragma omp target update%> must contain at least one "
16987 "%<from%> or %<to%> clauses");
16988 return false;
16991 tree stmt = make_node (OMP_TARGET_UPDATE);
16992 TREE_TYPE (stmt) = void_type_node;
16993 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
16994 SET_EXPR_LOCATION (stmt, loc);
16995 add_stmt (stmt);
16996 return false;
16999 /* OpenMP 4.5:
17000 # pragma omp target enter data target-data-clause[optseq] new-line */
17002 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
17003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17009 static tree
17010 c_parser_omp_target_enter_data (location_t loc, c_parser *parser,
17011 enum pragma_context context)
17013 bool data_seen = false;
17014 if (c_parser_next_token_is (parser, CPP_NAME))
17016 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17017 if (strcmp (p, "data") == 0)
17019 c_parser_consume_token (parser);
17020 data_seen = true;
17023 if (!data_seen)
17025 c_parser_error (parser, "expected %<data%>");
17026 c_parser_skip_to_pragma_eol (parser);
17027 return NULL_TREE;
17030 if (context == pragma_stmt)
17032 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17033 "omp target enter data");
17034 c_parser_skip_to_pragma_eol (parser, false);
17035 return NULL_TREE;
17038 tree clauses
17039 = c_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
17040 "#pragma omp target enter data");
17041 int map_seen = 0;
17042 for (tree *pc = &clauses; *pc;)
17044 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17045 switch (OMP_CLAUSE_MAP_KIND (*pc))
17047 case GOMP_MAP_TO:
17048 case GOMP_MAP_ALWAYS_TO:
17049 case GOMP_MAP_ALLOC:
17050 map_seen = 3;
17051 break;
17052 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17053 case GOMP_MAP_ALWAYS_POINTER:
17054 break;
17055 default:
17056 map_seen |= 1;
17057 error_at (OMP_CLAUSE_LOCATION (*pc),
17058 "%<#pragma omp target enter data%> with map-type other "
17059 "than %<to%> or %<alloc%> on %<map%> clause");
17060 *pc = OMP_CLAUSE_CHAIN (*pc);
17061 continue;
17063 pc = &OMP_CLAUSE_CHAIN (*pc);
17066 if (map_seen != 3)
17068 if (map_seen == 0)
17069 error_at (loc,
17070 "%<#pragma omp target enter data%> must contain at least "
17071 "one %<map%> clause");
17072 return NULL_TREE;
17075 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
17076 TREE_TYPE (stmt) = void_type_node;
17077 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
17078 SET_EXPR_LOCATION (stmt, loc);
17079 add_stmt (stmt);
17080 return stmt;
17083 /* OpenMP 4.5:
17084 # pragma omp target exit data target-data-clause[optseq] new-line */
17086 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
17087 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
17093 static tree
17094 c_parser_omp_target_exit_data (location_t loc, c_parser *parser,
17095 enum pragma_context context)
17097 bool data_seen = false;
17098 if (c_parser_next_token_is (parser, CPP_NAME))
17100 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17101 if (strcmp (p, "data") == 0)
17103 c_parser_consume_token (parser);
17104 data_seen = true;
17107 if (!data_seen)
17109 c_parser_error (parser, "expected %<data%>");
17110 c_parser_skip_to_pragma_eol (parser);
17111 return NULL_TREE;
17114 if (context == pragma_stmt)
17116 error_at (loc, "%<#pragma %s%> may only be used in compound statements",
17117 "omp target exit data");
17118 c_parser_skip_to_pragma_eol (parser, false);
17119 return NULL_TREE;
17122 tree clauses
17123 = c_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
17124 "#pragma omp target exit data");
17126 int map_seen = 0;
17127 for (tree *pc = &clauses; *pc;)
17129 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17130 switch (OMP_CLAUSE_MAP_KIND (*pc))
17132 case GOMP_MAP_FROM:
17133 case GOMP_MAP_ALWAYS_FROM:
17134 case GOMP_MAP_RELEASE:
17135 case GOMP_MAP_DELETE:
17136 map_seen = 3;
17137 break;
17138 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17139 case GOMP_MAP_ALWAYS_POINTER:
17140 break;
17141 default:
17142 map_seen |= 1;
17143 error_at (OMP_CLAUSE_LOCATION (*pc),
17144 "%<#pragma omp target exit data%> with map-type other "
17145 "than %<from%>, %<release%> or %<delete%> on %<map%>"
17146 " clause");
17147 *pc = OMP_CLAUSE_CHAIN (*pc);
17148 continue;
17150 pc = &OMP_CLAUSE_CHAIN (*pc);
17153 if (map_seen != 3)
17155 if (map_seen == 0)
17156 error_at (loc,
17157 "%<#pragma omp target exit data%> must contain at least one "
17158 "%<map%> clause");
17159 return NULL_TREE;
17162 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
17163 TREE_TYPE (stmt) = void_type_node;
17164 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
17165 SET_EXPR_LOCATION (stmt, loc);
17166 add_stmt (stmt);
17167 return stmt;
17170 /* OpenMP 4.0:
17171 # pragma omp target target-clause[optseq] new-line
17172 structured-block */
17174 #define OMP_TARGET_CLAUSE_MASK \
17175 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
17176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
17177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
17178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
17179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
17180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
17181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
17182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
17183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
17185 static bool
17186 c_parser_omp_target (c_parser *parser, enum pragma_context context, bool *if_p)
17188 location_t loc = c_parser_peek_token (parser)->location;
17189 c_parser_consume_pragma (parser);
17190 tree *pc = NULL, stmt, block;
17192 if (context != pragma_stmt && context != pragma_compound)
17194 c_parser_error (parser, "expected declaration specifiers");
17195 c_parser_skip_to_pragma_eol (parser);
17196 return false;
17199 if (c_parser_next_token_is (parser, CPP_NAME))
17201 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17202 enum tree_code ccode = ERROR_MARK;
17204 if (strcmp (p, "teams") == 0)
17205 ccode = OMP_TEAMS;
17206 else if (strcmp (p, "parallel") == 0)
17207 ccode = OMP_PARALLEL;
17208 else if (strcmp (p, "simd") == 0)
17209 ccode = OMP_SIMD;
17210 if (ccode != ERROR_MARK)
17212 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
17213 char p_name[sizeof ("#pragma omp target teams distribute "
17214 "parallel for simd")];
17216 c_parser_consume_token (parser);
17217 strcpy (p_name, "#pragma omp target");
17218 if (!flag_openmp) /* flag_openmp_simd */
17220 tree stmt;
17221 switch (ccode)
17223 case OMP_TEAMS:
17224 stmt = c_parser_omp_teams (loc, parser, p_name,
17225 OMP_TARGET_CLAUSE_MASK,
17226 cclauses, if_p);
17227 break;
17228 case OMP_PARALLEL:
17229 stmt = c_parser_omp_parallel (loc, parser, p_name,
17230 OMP_TARGET_CLAUSE_MASK,
17231 cclauses, if_p);
17232 break;
17233 case OMP_SIMD:
17234 stmt = c_parser_omp_simd (loc, parser, p_name,
17235 OMP_TARGET_CLAUSE_MASK,
17236 cclauses, if_p);
17237 break;
17238 default:
17239 gcc_unreachable ();
17241 return stmt != NULL_TREE;
17243 keep_next_level ();
17244 tree block = c_begin_compound_stmt (true), ret;
17245 switch (ccode)
17247 case OMP_TEAMS:
17248 ret = c_parser_omp_teams (loc, parser, p_name,
17249 OMP_TARGET_CLAUSE_MASK, cclauses,
17250 if_p);
17251 break;
17252 case OMP_PARALLEL:
17253 ret = c_parser_omp_parallel (loc, parser, p_name,
17254 OMP_TARGET_CLAUSE_MASK, cclauses,
17255 if_p);
17256 break;
17257 case OMP_SIMD:
17258 ret = c_parser_omp_simd (loc, parser, p_name,
17259 OMP_TARGET_CLAUSE_MASK, cclauses,
17260 if_p);
17261 break;
17262 default:
17263 gcc_unreachable ();
17265 block = c_end_compound_stmt (loc, block, true);
17266 if (ret == NULL_TREE)
17267 return false;
17268 if (ccode == OMP_TEAMS)
17270 /* For combined target teams, ensure the num_teams and
17271 thread_limit clause expressions are evaluated on the host,
17272 before entering the target construct. */
17273 tree c;
17274 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
17275 c; c = OMP_CLAUSE_CHAIN (c))
17276 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17277 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17278 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17280 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17281 tree tmp = create_tmp_var_raw (TREE_TYPE (expr));
17282 expr = build4 (TARGET_EXPR, TREE_TYPE (expr), tmp,
17283 expr, NULL_TREE, NULL_TREE);
17284 add_stmt (expr);
17285 OMP_CLAUSE_OPERAND (c, 0) = expr;
17286 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17287 OMP_CLAUSE_FIRSTPRIVATE);
17288 OMP_CLAUSE_DECL (tc) = tmp;
17289 OMP_CLAUSE_CHAIN (tc)
17290 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17291 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
17294 tree stmt = make_node (OMP_TARGET);
17295 TREE_TYPE (stmt) = void_type_node;
17296 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
17297 OMP_TARGET_BODY (stmt) = block;
17298 OMP_TARGET_COMBINED (stmt) = 1;
17299 add_stmt (stmt);
17300 pc = &OMP_TARGET_CLAUSES (stmt);
17301 goto check_clauses;
17303 else if (!flag_openmp) /* flag_openmp_simd */
17305 c_parser_skip_to_pragma_eol (parser, false);
17306 return false;
17308 else if (strcmp (p, "data") == 0)
17310 c_parser_consume_token (parser);
17311 c_parser_omp_target_data (loc, parser, if_p);
17312 return true;
17314 else if (strcmp (p, "enter") == 0)
17316 c_parser_consume_token (parser);
17317 c_parser_omp_target_enter_data (loc, parser, context);
17318 return false;
17320 else if (strcmp (p, "exit") == 0)
17322 c_parser_consume_token (parser);
17323 c_parser_omp_target_exit_data (loc, parser, context);
17324 return false;
17326 else if (strcmp (p, "update") == 0)
17328 c_parser_consume_token (parser);
17329 return c_parser_omp_target_update (loc, parser, context);
17332 if (!flag_openmp) /* flag_openmp_simd */
17334 c_parser_skip_to_pragma_eol (parser, false);
17335 return false;
17338 stmt = make_node (OMP_TARGET);
17339 TREE_TYPE (stmt) = void_type_node;
17341 OMP_TARGET_CLAUSES (stmt)
17342 = c_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
17343 "#pragma omp target");
17344 pc = &OMP_TARGET_CLAUSES (stmt);
17345 keep_next_level ();
17346 block = c_begin_compound_stmt (true);
17347 add_stmt (c_parser_omp_structured_block (parser, if_p));
17348 OMP_TARGET_BODY (stmt) = c_end_compound_stmt (loc, block, true);
17350 SET_EXPR_LOCATION (stmt, loc);
17351 add_stmt (stmt);
17353 check_clauses:
17354 while (*pc)
17356 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
17357 switch (OMP_CLAUSE_MAP_KIND (*pc))
17359 case GOMP_MAP_TO:
17360 case GOMP_MAP_ALWAYS_TO:
17361 case GOMP_MAP_FROM:
17362 case GOMP_MAP_ALWAYS_FROM:
17363 case GOMP_MAP_TOFROM:
17364 case GOMP_MAP_ALWAYS_TOFROM:
17365 case GOMP_MAP_ALLOC:
17366 case GOMP_MAP_FIRSTPRIVATE_POINTER:
17367 case GOMP_MAP_ALWAYS_POINTER:
17368 break;
17369 default:
17370 error_at (OMP_CLAUSE_LOCATION (*pc),
17371 "%<#pragma omp target%> with map-type other "
17372 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
17373 "on %<map%> clause");
17374 *pc = OMP_CLAUSE_CHAIN (*pc);
17375 continue;
17377 pc = &OMP_CLAUSE_CHAIN (*pc);
17379 return true;
17382 /* OpenMP 4.0:
17383 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
17385 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
17386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
17387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
17388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
17389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
17390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
17391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
17393 static void
17394 c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
17396 auto_vec<c_token> clauses;
17397 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17399 c_token *token = c_parser_peek_token (parser);
17400 if (token->type == CPP_EOF)
17402 c_parser_skip_to_pragma_eol (parser);
17403 return;
17405 clauses.safe_push (*token);
17406 c_parser_consume_token (parser);
17408 clauses.safe_push (*c_parser_peek_token (parser));
17409 c_parser_skip_to_pragma_eol (parser);
17411 while (c_parser_next_token_is (parser, CPP_PRAGMA))
17413 if (c_parser_peek_token (parser)->pragma_kind
17414 != PRAGMA_OMP_DECLARE
17415 || c_parser_peek_2nd_token (parser)->type != CPP_NAME
17416 || strcmp (IDENTIFIER_POINTER
17417 (c_parser_peek_2nd_token (parser)->value),
17418 "simd") != 0)
17420 c_parser_error (parser,
17421 "%<#pragma omp declare simd%> must be followed by "
17422 "function declaration or definition or another "
17423 "%<#pragma omp declare simd%>");
17424 return;
17426 c_parser_consume_pragma (parser);
17427 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17429 c_token *token = c_parser_peek_token (parser);
17430 if (token->type == CPP_EOF)
17432 c_parser_skip_to_pragma_eol (parser);
17433 return;
17435 clauses.safe_push (*token);
17436 c_parser_consume_token (parser);
17438 clauses.safe_push (*c_parser_peek_token (parser));
17439 c_parser_skip_to_pragma_eol (parser);
17442 /* Make sure nothing tries to read past the end of the tokens. */
17443 c_token eof_token;
17444 memset (&eof_token, 0, sizeof (eof_token));
17445 eof_token.type = CPP_EOF;
17446 clauses.safe_push (eof_token);
17447 clauses.safe_push (eof_token);
17449 switch (context)
17451 case pragma_external:
17452 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17453 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17455 int ext = disable_extension_diagnostics ();
17457 c_parser_consume_token (parser);
17458 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17459 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17460 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17461 NULL, clauses);
17462 restore_extension_diagnostics (ext);
17464 else
17465 c_parser_declaration_or_fndef (parser, true, true, true, false, true,
17466 NULL, clauses);
17467 break;
17468 case pragma_struct:
17469 case pragma_param:
17470 case pragma_stmt:
17471 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17472 "function declaration or definition");
17473 break;
17474 case pragma_compound:
17475 if (c_parser_next_token_is (parser, CPP_KEYWORD)
17476 && c_parser_peek_token (parser)->keyword == RID_EXTENSION)
17478 int ext = disable_extension_diagnostics ();
17480 c_parser_consume_token (parser);
17481 while (c_parser_next_token_is (parser, CPP_KEYWORD)
17482 && c_parser_peek_token (parser)->keyword == RID_EXTENSION);
17483 if (c_parser_next_tokens_start_declaration (parser))
17485 c_parser_declaration_or_fndef (parser, true, true, true, true,
17486 true, NULL, clauses);
17487 restore_extension_diagnostics (ext);
17488 break;
17490 restore_extension_diagnostics (ext);
17492 else if (c_parser_next_tokens_start_declaration (parser))
17494 c_parser_declaration_or_fndef (parser, true, true, true, true, true,
17495 NULL, clauses);
17496 break;
17498 c_parser_error (parser, "%<#pragma omp declare simd%> must be followed by "
17499 "function declaration or definition");
17500 break;
17501 default:
17502 gcc_unreachable ();
17506 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
17507 and put that into "omp declare simd" attribute. */
17509 static void
17510 c_finish_omp_declare_simd (c_parser *parser, tree fndecl, tree parms,
17511 vec<c_token> clauses)
17513 /* Normally first token is CPP_NAME "simd". CPP_EOF there indicates
17514 error has been reported and CPP_PRAGMA that c_finish_omp_declare_simd
17515 has already processed the tokens. */
17516 if (clauses.exists () && clauses[0].type == CPP_EOF)
17517 return;
17518 if (fndecl == NULL_TREE || TREE_CODE (fndecl) != FUNCTION_DECL)
17520 error ("%<#pragma omp declare simd%> not immediately followed by "
17521 "a function declaration or definition");
17522 clauses[0].type = CPP_EOF;
17523 return;
17525 if (clauses.exists () && clauses[0].type != CPP_NAME)
17527 error_at (DECL_SOURCE_LOCATION (fndecl),
17528 "%<#pragma omp declare simd%> not immediately followed by "
17529 "a single function declaration or definition");
17530 clauses[0].type = CPP_EOF;
17531 return;
17534 if (parms == NULL_TREE)
17535 parms = DECL_ARGUMENTS (fndecl);
17537 unsigned int tokens_avail = parser->tokens_avail;
17538 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17541 parser->tokens = clauses.address ();
17542 parser->tokens_avail = clauses.length ();
17544 /* c_parser_omp_declare_simd pushed 2 extra CPP_EOF tokens at the end. */
17545 while (parser->tokens_avail > 3)
17547 c_token *token = c_parser_peek_token (parser);
17548 gcc_assert (token->type == CPP_NAME
17549 && strcmp (IDENTIFIER_POINTER (token->value), "simd") == 0);
17550 c_parser_consume_token (parser);
17551 parser->in_pragma = true;
17553 tree c = NULL_TREE;
17554 c = c_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
17555 "#pragma omp declare simd");
17556 c = c_omp_declare_simd_clauses_to_numbers (parms, c);
17557 if (c != NULL_TREE)
17558 c = tree_cons (NULL_TREE, c, NULL_TREE);
17559 c = build_tree_list (get_identifier ("omp declare simd"), c);
17560 TREE_CHAIN (c) = DECL_ATTRIBUTES (fndecl);
17561 DECL_ATTRIBUTES (fndecl) = c;
17564 parser->tokens = &parser->tokens_buf[0];
17565 parser->tokens_avail = tokens_avail;
17566 if (clauses.exists ())
17567 clauses[0].type = CPP_PRAGMA;
17571 /* OpenMP 4.0:
17572 # pragma omp declare target new-line
17573 declarations and definitions
17574 # pragma omp end declare target new-line
17576 OpenMP 4.5:
17577 # pragma omp declare target ( extended-list ) new-line
17579 # pragma omp declare target declare-target-clauses[seq] new-line */
17581 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
17582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
17583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
17585 static void
17586 c_parser_omp_declare_target (c_parser *parser)
17588 location_t loc = c_parser_peek_token (parser)->location;
17589 tree clauses = NULL_TREE;
17590 if (c_parser_next_token_is (parser, CPP_NAME))
17591 clauses = c_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
17592 "#pragma omp declare target");
17593 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
17595 clauses = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_TO_DECLARE,
17596 clauses);
17597 clauses = c_finish_omp_clauses (clauses, C_ORT_OMP);
17598 c_parser_skip_to_pragma_eol (parser);
17600 else
17602 c_parser_skip_to_pragma_eol (parser);
17603 current_omp_declare_target_attribute++;
17604 return;
17606 if (current_omp_declare_target_attribute)
17607 error_at (loc, "%<#pragma omp declare target%> with clauses in between "
17608 "%<#pragma omp declare target%> without clauses and "
17609 "%<#pragma omp end declare target%>");
17610 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
17612 tree t = OMP_CLAUSE_DECL (c), id;
17613 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
17614 tree at2 = lookup_attribute ("omp declare target link",
17615 DECL_ATTRIBUTES (t));
17616 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
17618 id = get_identifier ("omp declare target link");
17619 std::swap (at1, at2);
17621 else
17622 id = get_identifier ("omp declare target");
17623 if (at2)
17625 error_at (OMP_CLAUSE_LOCATION (c),
17626 "%qD specified both in declare target %<link%> and %<to%>"
17627 " clauses", t);
17628 continue;
17630 if (!at1)
17632 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
17633 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
17634 continue;
17636 symtab_node *node = symtab_node::get (t);
17637 if (node != NULL)
17639 node->offloadable = 1;
17640 if (ENABLE_OFFLOADING)
17642 g->have_offload = true;
17643 if (is_a <varpool_node *> (node))
17644 vec_safe_push (offload_vars, t);
17651 static void
17652 c_parser_omp_end_declare_target (c_parser *parser)
17654 location_t loc = c_parser_peek_token (parser)->location;
17655 c_parser_consume_pragma (parser);
17656 if (c_parser_next_token_is (parser, CPP_NAME)
17657 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17658 "declare") == 0)
17660 c_parser_consume_token (parser);
17661 if (c_parser_next_token_is (parser, CPP_NAME)
17662 && strcmp (IDENTIFIER_POINTER (c_parser_peek_token (parser)->value),
17663 "target") == 0)
17664 c_parser_consume_token (parser);
17665 else
17667 c_parser_error (parser, "expected %<target%>");
17668 c_parser_skip_to_pragma_eol (parser);
17669 return;
17672 else
17674 c_parser_error (parser, "expected %<declare%>");
17675 c_parser_skip_to_pragma_eol (parser);
17676 return;
17678 c_parser_skip_to_pragma_eol (parser);
17679 if (!current_omp_declare_target_attribute)
17680 error_at (loc, "%<#pragma omp end declare target%> without corresponding "
17681 "%<#pragma omp declare target%>");
17682 else
17683 current_omp_declare_target_attribute--;
17687 /* OpenMP 4.0
17688 #pragma omp declare reduction (reduction-id : typename-list : expression) \
17689 initializer-clause[opt] new-line
17691 initializer-clause:
17692 initializer (omp_priv = initializer)
17693 initializer (function-name (argument-list)) */
17695 static void
17696 c_parser_omp_declare_reduction (c_parser *parser, enum pragma_context context)
17698 unsigned int tokens_avail = 0, i;
17699 vec<tree> types = vNULL;
17700 vec<c_token> clauses = vNULL;
17701 enum tree_code reduc_code = ERROR_MARK;
17702 tree reduc_id = NULL_TREE;
17703 tree type;
17704 location_t rloc = c_parser_peek_token (parser)->location;
17706 if (context == pragma_struct || context == pragma_param)
17708 error ("%<#pragma omp declare reduction%> not at file or block scope");
17709 goto fail;
17712 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17713 goto fail;
17715 switch (c_parser_peek_token (parser)->type)
17717 case CPP_PLUS:
17718 reduc_code = PLUS_EXPR;
17719 break;
17720 case CPP_MULT:
17721 reduc_code = MULT_EXPR;
17722 break;
17723 case CPP_MINUS:
17724 reduc_code = MINUS_EXPR;
17725 break;
17726 case CPP_AND:
17727 reduc_code = BIT_AND_EXPR;
17728 break;
17729 case CPP_XOR:
17730 reduc_code = BIT_XOR_EXPR;
17731 break;
17732 case CPP_OR:
17733 reduc_code = BIT_IOR_EXPR;
17734 break;
17735 case CPP_AND_AND:
17736 reduc_code = TRUTH_ANDIF_EXPR;
17737 break;
17738 case CPP_OR_OR:
17739 reduc_code = TRUTH_ORIF_EXPR;
17740 break;
17741 case CPP_NAME:
17742 const char *p;
17743 p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
17744 if (strcmp (p, "min") == 0)
17746 reduc_code = MIN_EXPR;
17747 break;
17749 if (strcmp (p, "max") == 0)
17751 reduc_code = MAX_EXPR;
17752 break;
17754 reduc_id = c_parser_peek_token (parser)->value;
17755 break;
17756 default:
17757 c_parser_error (parser,
17758 "expected %<+%>, %<*%>, %<-%>, %<&%>, "
17759 "%<^%>, %<|%>, %<&&%>, %<||%> or identifier");
17760 goto fail;
17763 tree orig_reduc_id, reduc_decl;
17764 orig_reduc_id = reduc_id;
17765 reduc_id = c_omp_reduction_id (reduc_code, reduc_id);
17766 reduc_decl = c_omp_reduction_decl (reduc_id);
17767 c_parser_consume_token (parser);
17769 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>"))
17770 goto fail;
17772 while (true)
17774 location_t loc = c_parser_peek_token (parser)->location;
17775 struct c_type_name *ctype = c_parser_type_name (parser);
17776 if (ctype != NULL)
17778 type = groktypename (ctype, NULL, NULL);
17779 if (type == error_mark_node)
17781 else if ((INTEGRAL_TYPE_P (type)
17782 || TREE_CODE (type) == REAL_TYPE
17783 || TREE_CODE (type) == COMPLEX_TYPE)
17784 && orig_reduc_id == NULL_TREE)
17785 error_at (loc, "predeclared arithmetic type in "
17786 "%<#pragma omp declare reduction%>");
17787 else if (TREE_CODE (type) == FUNCTION_TYPE
17788 || TREE_CODE (type) == ARRAY_TYPE)
17789 error_at (loc, "function or array type in "
17790 "%<#pragma omp declare reduction%>");
17791 else if (TYPE_ATOMIC (type))
17792 error_at (loc, "%<_Atomic%> qualified type in "
17793 "%<#pragma omp declare reduction%>");
17794 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
17795 error_at (loc, "const, volatile or restrict qualified type in "
17796 "%<#pragma omp declare reduction%>");
17797 else
17799 tree t;
17800 for (t = DECL_INITIAL (reduc_decl); t; t = TREE_CHAIN (t))
17801 if (comptypes (TREE_PURPOSE (t), type))
17803 error_at (loc, "redeclaration of %qs "
17804 "%<#pragma omp declare reduction%> for "
17805 "type %qT",
17806 IDENTIFIER_POINTER (reduc_id)
17807 + sizeof ("omp declare reduction ") - 1,
17808 type);
17809 location_t ploc
17810 = DECL_SOURCE_LOCATION (TREE_VEC_ELT (TREE_VALUE (t),
17811 0));
17812 error_at (ploc, "previous %<#pragma omp declare "
17813 "reduction%>");
17814 break;
17816 if (t == NULL_TREE)
17817 types.safe_push (type);
17819 if (c_parser_next_token_is (parser, CPP_COMMA))
17820 c_parser_consume_token (parser);
17821 else
17822 break;
17824 else
17825 break;
17828 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")
17829 || types.is_empty ())
17831 fail:
17832 clauses.release ();
17833 types.release ();
17834 while (true)
17836 c_token *token = c_parser_peek_token (parser);
17837 if (token->type == CPP_EOF || token->type == CPP_PRAGMA_EOL)
17838 break;
17839 c_parser_consume_token (parser);
17841 c_parser_skip_to_pragma_eol (parser);
17842 return;
17845 if (types.length () > 1)
17847 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL))
17849 c_token *token = c_parser_peek_token (parser);
17850 if (token->type == CPP_EOF)
17851 goto fail;
17852 clauses.safe_push (*token);
17853 c_parser_consume_token (parser);
17855 clauses.safe_push (*c_parser_peek_token (parser));
17856 c_parser_skip_to_pragma_eol (parser);
17858 /* Make sure nothing tries to read past the end of the tokens. */
17859 c_token eof_token;
17860 memset (&eof_token, 0, sizeof (eof_token));
17861 eof_token.type = CPP_EOF;
17862 clauses.safe_push (eof_token);
17863 clauses.safe_push (eof_token);
17866 int errs = errorcount;
17867 FOR_EACH_VEC_ELT (types, i, type)
17869 tokens_avail = parser->tokens_avail;
17870 gcc_assert (parser->tokens == &parser->tokens_buf[0]);
17871 if (!clauses.is_empty ())
17873 parser->tokens = clauses.address ();
17874 parser->tokens_avail = clauses.length ();
17875 parser->in_pragma = true;
17878 bool nested = current_function_decl != NULL_TREE;
17879 if (nested)
17880 c_push_function_context ();
17881 tree fndecl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
17882 reduc_id, default_function_type);
17883 current_function_decl = fndecl;
17884 allocate_struct_function (fndecl, true);
17885 push_scope ();
17886 tree stmt = push_stmt_list ();
17887 /* Intentionally BUILTINS_LOCATION, so that -Wshadow doesn't
17888 warn about these. */
17889 tree omp_out = build_decl (BUILTINS_LOCATION, VAR_DECL,
17890 get_identifier ("omp_out"), type);
17891 DECL_ARTIFICIAL (omp_out) = 1;
17892 DECL_CONTEXT (omp_out) = fndecl;
17893 pushdecl (omp_out);
17894 tree omp_in = build_decl (BUILTINS_LOCATION, VAR_DECL,
17895 get_identifier ("omp_in"), type);
17896 DECL_ARTIFICIAL (omp_in) = 1;
17897 DECL_CONTEXT (omp_in) = fndecl;
17898 pushdecl (omp_in);
17899 struct c_expr combiner = c_parser_expression (parser);
17900 struct c_expr initializer;
17901 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE;
17902 bool bad = false;
17903 initializer.set_error ();
17904 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17905 bad = true;
17906 else if (c_parser_next_token_is (parser, CPP_NAME)
17907 && strcmp (IDENTIFIER_POINTER
17908 (c_parser_peek_token (parser)->value),
17909 "initializer") == 0)
17911 c_parser_consume_token (parser);
17912 pop_scope ();
17913 push_scope ();
17914 omp_priv = build_decl (BUILTINS_LOCATION, VAR_DECL,
17915 get_identifier ("omp_priv"), type);
17916 DECL_ARTIFICIAL (omp_priv) = 1;
17917 DECL_INITIAL (omp_priv) = error_mark_node;
17918 DECL_CONTEXT (omp_priv) = fndecl;
17919 pushdecl (omp_priv);
17920 omp_orig = build_decl (BUILTINS_LOCATION, VAR_DECL,
17921 get_identifier ("omp_orig"), type);
17922 DECL_ARTIFICIAL (omp_orig) = 1;
17923 DECL_CONTEXT (omp_orig) = fndecl;
17924 pushdecl (omp_orig);
17925 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
17926 bad = true;
17927 else if (!c_parser_next_token_is (parser, CPP_NAME))
17929 c_parser_error (parser, "expected %<omp_priv%> or "
17930 "function-name");
17931 bad = true;
17933 else if (strcmp (IDENTIFIER_POINTER
17934 (c_parser_peek_token (parser)->value),
17935 "omp_priv") != 0)
17937 if (c_parser_peek_2nd_token (parser)->type != CPP_OPEN_PAREN
17938 || c_parser_peek_token (parser)->id_kind != C_ID_ID)
17940 c_parser_error (parser, "expected function-name %<(%>");
17941 bad = true;
17943 else
17944 initializer = c_parser_postfix_expression (parser);
17945 if (initializer.value
17946 && TREE_CODE (initializer.value) == CALL_EXPR)
17948 int j;
17949 tree c = initializer.value;
17950 for (j = 0; j < call_expr_nargs (c); j++)
17952 tree a = CALL_EXPR_ARG (c, j);
17953 STRIP_NOPS (a);
17954 if (TREE_CODE (a) == ADDR_EXPR
17955 && TREE_OPERAND (a, 0) == omp_priv)
17956 break;
17958 if (j == call_expr_nargs (c))
17959 error ("one of the initializer call arguments should be "
17960 "%<&omp_priv%>");
17963 else
17965 c_parser_consume_token (parser);
17966 if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
17967 bad = true;
17968 else
17970 tree st = push_stmt_list ();
17971 location_t loc = c_parser_peek_token (parser)->location;
17972 rich_location richloc (line_table, loc);
17973 start_init (omp_priv, NULL_TREE, 0, &richloc);
17974 struct c_expr init = c_parser_initializer (parser);
17975 finish_init ();
17976 finish_decl (omp_priv, loc, init.value,
17977 init.original_type, NULL_TREE);
17978 pop_stmt_list (st);
17981 if (!bad
17982 && !c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"))
17983 bad = true;
17986 if (!bad)
17988 c_parser_skip_to_pragma_eol (parser);
17990 tree t = tree_cons (type, make_tree_vec (omp_priv ? 6 : 3),
17991 DECL_INITIAL (reduc_decl));
17992 DECL_INITIAL (reduc_decl) = t;
17993 DECL_SOURCE_LOCATION (omp_out) = rloc;
17994 TREE_VEC_ELT (TREE_VALUE (t), 0) = omp_out;
17995 TREE_VEC_ELT (TREE_VALUE (t), 1) = omp_in;
17996 TREE_VEC_ELT (TREE_VALUE (t), 2) = combiner.value;
17997 walk_tree (&combiner.value, c_check_omp_declare_reduction_r,
17998 &TREE_VEC_ELT (TREE_VALUE (t), 0), NULL);
17999 if (omp_priv)
18001 DECL_SOURCE_LOCATION (omp_priv) = rloc;
18002 TREE_VEC_ELT (TREE_VALUE (t), 3) = omp_priv;
18003 TREE_VEC_ELT (TREE_VALUE (t), 4) = omp_orig;
18004 TREE_VEC_ELT (TREE_VALUE (t), 5) = initializer.value;
18005 walk_tree (&initializer.value, c_check_omp_declare_reduction_r,
18006 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
18007 walk_tree (&DECL_INITIAL (omp_priv),
18008 c_check_omp_declare_reduction_r,
18009 &TREE_VEC_ELT (TREE_VALUE (t), 3), NULL);
18013 pop_stmt_list (stmt);
18014 pop_scope ();
18015 if (cfun->language != NULL)
18017 ggc_free (cfun->language);
18018 cfun->language = NULL;
18020 set_cfun (NULL);
18021 current_function_decl = NULL_TREE;
18022 if (nested)
18023 c_pop_function_context ();
18025 if (!clauses.is_empty ())
18027 parser->tokens = &parser->tokens_buf[0];
18028 parser->tokens_avail = tokens_avail;
18030 if (bad)
18031 goto fail;
18032 if (errs != errorcount)
18033 break;
18036 clauses.release ();
18037 types.release ();
18041 /* OpenMP 4.0
18042 #pragma omp declare simd declare-simd-clauses[optseq] new-line
18043 #pragma omp declare reduction (reduction-id : typename-list : expression) \
18044 initializer-clause[opt] new-line
18045 #pragma omp declare target new-line */
18047 static void
18048 c_parser_omp_declare (c_parser *parser, enum pragma_context context)
18050 c_parser_consume_pragma (parser);
18051 if (c_parser_next_token_is (parser, CPP_NAME))
18053 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18054 if (strcmp (p, "simd") == 0)
18056 /* c_parser_consume_token (parser); done in
18057 c_parser_omp_declare_simd. */
18058 c_parser_omp_declare_simd (parser, context);
18059 return;
18061 if (strcmp (p, "reduction") == 0)
18063 c_parser_consume_token (parser);
18064 c_parser_omp_declare_reduction (parser, context);
18065 return;
18067 if (!flag_openmp) /* flag_openmp_simd */
18069 c_parser_skip_to_pragma_eol (parser, false);
18070 return;
18072 if (strcmp (p, "target") == 0)
18074 c_parser_consume_token (parser);
18075 c_parser_omp_declare_target (parser);
18076 return;
18080 c_parser_error (parser, "expected %<simd%> or %<reduction%> "
18081 "or %<target%>");
18082 c_parser_skip_to_pragma_eol (parser);
18085 /* OpenMP 4.5:
18086 #pragma omp taskloop taskloop-clause[optseq] new-line
18087 for-loop
18089 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
18090 for-loop */
18092 #define OMP_TASKLOOP_CLAUSE_MASK \
18093 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
18094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
18095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
18096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
18097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
18098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
18099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
18100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
18101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
18102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
18103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
18104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
18105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
18106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
18108 static tree
18109 c_parser_omp_taskloop (location_t loc, c_parser *parser,
18110 char *p_name, omp_clause_mask mask, tree *cclauses,
18111 bool *if_p)
18113 tree clauses, block, ret;
18115 strcat (p_name, " taskloop");
18116 mask |= OMP_TASKLOOP_CLAUSE_MASK;
18118 if (c_parser_next_token_is (parser, CPP_NAME))
18120 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
18122 if (strcmp (p, "simd") == 0)
18124 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
18125 if (cclauses == NULL)
18126 cclauses = cclauses_buf;
18127 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION);
18128 c_parser_consume_token (parser);
18129 if (!flag_openmp) /* flag_openmp_simd */
18130 return c_parser_omp_simd (loc, parser, p_name, mask, cclauses,
18131 if_p);
18132 block = c_begin_compound_stmt (true);
18133 ret = c_parser_omp_simd (loc, parser, p_name, mask, cclauses, if_p);
18134 block = c_end_compound_stmt (loc, block, true);
18135 if (ret == NULL)
18136 return ret;
18137 ret = make_node (OMP_TASKLOOP);
18138 TREE_TYPE (ret) = void_type_node;
18139 OMP_FOR_BODY (ret) = block;
18140 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18141 SET_EXPR_LOCATION (ret, loc);
18142 add_stmt (ret);
18143 return ret;
18146 if (!flag_openmp) /* flag_openmp_simd */
18148 c_parser_skip_to_pragma_eol (parser, false);
18149 return NULL_TREE;
18152 clauses = c_parser_omp_all_clauses (parser, mask, p_name, cclauses == NULL);
18153 if (cclauses)
18155 omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
18156 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
18159 block = c_begin_compound_stmt (true);
18160 ret = c_parser_omp_for_loop (loc, parser, OMP_TASKLOOP, clauses, NULL, if_p);
18161 block = c_end_compound_stmt (loc, block, true);
18162 add_stmt (block);
18164 return ret;
18167 /* Main entry point to parsing most OpenMP pragmas. */
18169 static void
18170 c_parser_omp_construct (c_parser *parser, bool *if_p)
18172 enum pragma_kind p_kind;
18173 location_t loc;
18174 tree stmt;
18175 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
18176 omp_clause_mask mask (0);
18178 loc = c_parser_peek_token (parser)->location;
18179 p_kind = c_parser_peek_token (parser)->pragma_kind;
18180 c_parser_consume_pragma (parser);
18182 switch (p_kind)
18184 case PRAGMA_OACC_ATOMIC:
18185 c_parser_omp_atomic (loc, parser);
18186 return;
18187 case PRAGMA_OACC_CACHE:
18188 strcpy (p_name, "#pragma acc");
18189 stmt = c_parser_oacc_cache (loc, parser);
18190 break;
18191 case PRAGMA_OACC_DATA:
18192 stmt = c_parser_oacc_data (loc, parser, if_p);
18193 break;
18194 case PRAGMA_OACC_HOST_DATA:
18195 stmt = c_parser_oacc_host_data (loc, parser, if_p);
18196 break;
18197 case PRAGMA_OACC_KERNELS:
18198 case PRAGMA_OACC_PARALLEL:
18199 strcpy (p_name, "#pragma acc");
18200 stmt = c_parser_oacc_kernels_parallel (loc, parser, p_kind, p_name,
18201 if_p);
18202 break;
18203 case PRAGMA_OACC_LOOP:
18204 strcpy (p_name, "#pragma acc");
18205 stmt = c_parser_oacc_loop (loc, parser, p_name, mask, NULL, if_p);
18206 break;
18207 case PRAGMA_OACC_WAIT:
18208 strcpy (p_name, "#pragma wait");
18209 stmt = c_parser_oacc_wait (loc, parser, p_name);
18210 break;
18211 case PRAGMA_OMP_ATOMIC:
18212 c_parser_omp_atomic (loc, parser);
18213 return;
18214 case PRAGMA_OMP_CRITICAL:
18215 stmt = c_parser_omp_critical (loc, parser, if_p);
18216 break;
18217 case PRAGMA_OMP_DISTRIBUTE:
18218 strcpy (p_name, "#pragma omp");
18219 stmt = c_parser_omp_distribute (loc, parser, p_name, mask, NULL, if_p);
18220 break;
18221 case PRAGMA_OMP_FOR:
18222 strcpy (p_name, "#pragma omp");
18223 stmt = c_parser_omp_for (loc, parser, p_name, mask, NULL, if_p);
18224 break;
18225 case PRAGMA_OMP_MASTER:
18226 stmt = c_parser_omp_master (loc, parser, if_p);
18227 break;
18228 case PRAGMA_OMP_PARALLEL:
18229 strcpy (p_name, "#pragma omp");
18230 stmt = c_parser_omp_parallel (loc, parser, p_name, mask, NULL, if_p);
18231 break;
18232 case PRAGMA_OMP_SECTIONS:
18233 strcpy (p_name, "#pragma omp");
18234 stmt = c_parser_omp_sections (loc, parser, p_name, mask, NULL);
18235 break;
18236 case PRAGMA_OMP_SIMD:
18237 strcpy (p_name, "#pragma omp");
18238 stmt = c_parser_omp_simd (loc, parser, p_name, mask, NULL, if_p);
18239 break;
18240 case PRAGMA_OMP_SINGLE:
18241 stmt = c_parser_omp_single (loc, parser, if_p);
18242 break;
18243 case PRAGMA_OMP_TASK:
18244 stmt = c_parser_omp_task (loc, parser, if_p);
18245 break;
18246 case PRAGMA_OMP_TASKGROUP:
18247 stmt = c_parser_omp_taskgroup (parser, if_p);
18248 break;
18249 case PRAGMA_OMP_TASKLOOP:
18250 strcpy (p_name, "#pragma omp");
18251 stmt = c_parser_omp_taskloop (loc, parser, p_name, mask, NULL, if_p);
18252 break;
18253 case PRAGMA_OMP_TEAMS:
18254 strcpy (p_name, "#pragma omp");
18255 stmt = c_parser_omp_teams (loc, parser, p_name, mask, NULL, if_p);
18256 break;
18257 default:
18258 gcc_unreachable ();
18261 if (stmt)
18262 gcc_assert (EXPR_LOCATION (stmt) != UNKNOWN_LOCATION);
18266 /* OpenMP 2.5:
18267 # pragma omp threadprivate (variable-list) */
18269 static void
18270 c_parser_omp_threadprivate (c_parser *parser)
18272 tree vars, t;
18273 location_t loc;
18275 c_parser_consume_pragma (parser);
18276 loc = c_parser_peek_token (parser)->location;
18277 vars = c_parser_omp_var_list_parens (parser, OMP_CLAUSE_ERROR, NULL);
18279 /* Mark every variable in VARS to be assigned thread local storage. */
18280 for (t = vars; t; t = TREE_CHAIN (t))
18282 tree v = TREE_PURPOSE (t);
18284 /* FIXME diagnostics: Ideally we should keep individual
18285 locations for all the variables in the var list to make the
18286 following errors more precise. Perhaps
18287 c_parser_omp_var_list_parens() should construct a list of
18288 locations to go along with the var list. */
18290 /* If V had already been marked threadprivate, it doesn't matter
18291 whether it had been used prior to this point. */
18292 if (!VAR_P (v))
18293 error_at (loc, "%qD is not a variable", v);
18294 else if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v))
18295 error_at (loc, "%qE declared %<threadprivate%> after first use", v);
18296 else if (! is_global_var (v))
18297 error_at (loc, "automatic variable %qE cannot be %<threadprivate%>", v);
18298 else if (TREE_TYPE (v) == error_mark_node)
18300 else if (! COMPLETE_TYPE_P (TREE_TYPE (v)))
18301 error_at (loc, "%<threadprivate%> %qE has incomplete type", v);
18302 else
18304 if (! DECL_THREAD_LOCAL_P (v))
18306 set_decl_tls_model (v, decl_default_tls_model (v));
18307 /* If rtl has been already set for this var, call
18308 make_decl_rtl once again, so that encode_section_info
18309 has a chance to look at the new decl flags. */
18310 if (DECL_RTL_SET_P (v))
18311 make_decl_rtl (v);
18313 C_DECL_THREADPRIVATE_P (v) = 1;
18317 c_parser_skip_to_pragma_eol (parser);
18320 /* Parse a transaction attribute (GCC Extension).
18322 transaction-attribute:
18323 attributes
18324 [ [ any-word ] ]
18326 The transactional memory language description is written for C++,
18327 and uses the C++0x attribute syntax. For compatibility, allow the
18328 bracket style for transactions in C as well. */
18330 static tree
18331 c_parser_transaction_attributes (c_parser *parser)
18333 tree attr_name, attr = NULL;
18335 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
18336 return c_parser_attributes (parser);
18338 if (!c_parser_next_token_is (parser, CPP_OPEN_SQUARE))
18339 return NULL_TREE;
18340 c_parser_consume_token (parser);
18341 if (!c_parser_require (parser, CPP_OPEN_SQUARE, "expected %<[%>"))
18342 goto error1;
18344 attr_name = c_parser_attribute_any_word (parser);
18345 if (attr_name)
18347 c_parser_consume_token (parser);
18348 attr = build_tree_list (attr_name, NULL_TREE);
18350 else
18351 c_parser_error (parser, "expected identifier");
18353 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18354 error1:
18355 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, "expected %<]%>");
18356 return attr;
18359 /* Parse a __transaction_atomic or __transaction_relaxed statement
18360 (GCC Extension).
18362 transaction-statement:
18363 __transaction_atomic transaction-attribute[opt] compound-statement
18364 __transaction_relaxed compound-statement
18366 Note that the only valid attribute is: "outer".
18369 static tree
18370 c_parser_transaction (c_parser *parser, enum rid keyword)
18372 unsigned int old_in = parser->in_transaction;
18373 unsigned int this_in = 1, new_in;
18374 location_t loc = c_parser_peek_token (parser)->location;
18375 tree stmt, attrs;
18377 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18378 || keyword == RID_TRANSACTION_RELAXED)
18379 && c_parser_next_token_is_keyword (parser, keyword));
18380 c_parser_consume_token (parser);
18382 if (keyword == RID_TRANSACTION_RELAXED)
18383 this_in |= TM_STMT_ATTR_RELAXED;
18384 else
18386 attrs = c_parser_transaction_attributes (parser);
18387 if (attrs)
18388 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
18391 /* Keep track if we're in the lexical scope of an outer transaction. */
18392 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
18394 parser->in_transaction = new_in;
18395 stmt = c_parser_compound_statement (parser);
18396 parser->in_transaction = old_in;
18398 if (flag_tm)
18399 stmt = c_finish_transaction (loc, stmt, this_in);
18400 else
18401 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18402 "%<__transaction_atomic%> without transactional memory support enabled"
18403 : "%<__transaction_relaxed %> "
18404 "without transactional memory support enabled"));
18406 return stmt;
18409 /* Parse a __transaction_atomic or __transaction_relaxed expression
18410 (GCC Extension).
18412 transaction-expression:
18413 __transaction_atomic ( expression )
18414 __transaction_relaxed ( expression )
18417 static struct c_expr
18418 c_parser_transaction_expression (c_parser *parser, enum rid keyword)
18420 struct c_expr ret;
18421 unsigned int old_in = parser->in_transaction;
18422 unsigned int this_in = 1;
18423 location_t loc = c_parser_peek_token (parser)->location;
18424 tree attrs;
18426 gcc_assert ((keyword == RID_TRANSACTION_ATOMIC
18427 || keyword == RID_TRANSACTION_RELAXED)
18428 && c_parser_next_token_is_keyword (parser, keyword));
18429 c_parser_consume_token (parser);
18431 if (keyword == RID_TRANSACTION_RELAXED)
18432 this_in |= TM_STMT_ATTR_RELAXED;
18433 else
18435 attrs = c_parser_transaction_attributes (parser);
18436 if (attrs)
18437 this_in |= parse_tm_stmt_attr (attrs, 0);
18440 parser->in_transaction = this_in;
18441 matching_parens parens;
18442 if (parens.require_open (parser))
18444 tree expr = c_parser_expression (parser).value;
18445 ret.original_type = TREE_TYPE (expr);
18446 ret.value = build1 (TRANSACTION_EXPR, ret.original_type, expr);
18447 if (this_in & TM_STMT_ATTR_RELAXED)
18448 TRANSACTION_EXPR_RELAXED (ret.value) = 1;
18449 SET_EXPR_LOCATION (ret.value, loc);
18450 ret.original_code = TRANSACTION_EXPR;
18451 if (!parens.require_close (parser))
18453 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL);
18454 goto error;
18457 else
18459 error:
18460 ret.set_error ();
18461 ret.original_code = ERROR_MARK;
18462 ret.original_type = NULL;
18464 parser->in_transaction = old_in;
18466 if (!flag_tm)
18467 error_at (loc, (keyword == RID_TRANSACTION_ATOMIC ?
18468 "%<__transaction_atomic%> without transactional memory support enabled"
18469 : "%<__transaction_relaxed %> "
18470 "without transactional memory support enabled"));
18472 set_c_expr_source_range (&ret, loc, loc);
18474 return ret;
18477 /* Parse a __transaction_cancel statement (GCC Extension).
18479 transaction-cancel-statement:
18480 __transaction_cancel transaction-attribute[opt] ;
18482 Note that the only valid attribute is "outer".
18485 static tree
18486 c_parser_transaction_cancel (c_parser *parser)
18488 location_t loc = c_parser_peek_token (parser)->location;
18489 tree attrs;
18490 bool is_outer = false;
18492 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TRANSACTION_CANCEL));
18493 c_parser_consume_token (parser);
18495 attrs = c_parser_transaction_attributes (parser);
18496 if (attrs)
18497 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
18499 if (!flag_tm)
18501 error_at (loc, "%<__transaction_cancel%> without "
18502 "transactional memory support enabled");
18503 goto ret_error;
18505 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
18507 error_at (loc, "%<__transaction_cancel%> within a "
18508 "%<__transaction_relaxed%>");
18509 goto ret_error;
18511 else if (is_outer)
18513 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
18514 && !is_tm_may_cancel_outer (current_function_decl))
18516 error_at (loc, "outer %<__transaction_cancel%> not "
18517 "within outer %<__transaction_atomic%>");
18518 error_at (loc, " or a %<transaction_may_cancel_outer%> function");
18519 goto ret_error;
18522 else if (parser->in_transaction == 0)
18524 error_at (loc, "%<__transaction_cancel%> not within "
18525 "%<__transaction_atomic%>");
18526 goto ret_error;
18529 return add_stmt (build_tm_abort_call (loc, is_outer));
18531 ret_error:
18532 return build1 (NOP_EXPR, void_type_node, error_mark_node);
18535 /* Parse a single source file. */
18537 void
18538 c_parse_file (void)
18540 /* Use local storage to begin. If the first token is a pragma, parse it.
18541 If it is #pragma GCC pch_preprocess, then this will load a PCH file
18542 which will cause garbage collection. */
18543 c_parser tparser;
18545 memset (&tparser, 0, sizeof tparser);
18546 tparser.tokens = &tparser.tokens_buf[0];
18547 the_parser = &tparser;
18549 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS)
18550 c_parser_pragma_pch_preprocess (&tparser);
18552 the_parser = ggc_alloc<c_parser> ();
18553 *the_parser = tparser;
18554 if (tparser.tokens == &tparser.tokens_buf[0])
18555 the_parser->tokens = &the_parser->tokens_buf[0];
18557 /* Initialize EH, if we've been told to do so. */
18558 if (flag_exceptions)
18559 using_eh_for_cleanups ();
18561 c_parser_translation_unit (the_parser);
18562 the_parser = NULL;
18565 /* Parse the body of a function declaration marked with "__RTL".
18567 The RTL parser works on the level of characters read from a
18568 FILE *, whereas c_parser works at the level of tokens.
18569 Square this circle by consuming all of the tokens up to and
18570 including the closing brace, recording the start/end of the RTL
18571 fragment, and reopening the file and re-reading the relevant
18572 lines within the RTL parser.
18574 This requires the opening and closing braces of the C function
18575 to be on separate lines from the RTL they wrap.
18577 Take ownership of START_WITH_PASS, if non-NULL. */
18579 void
18580 c_parser_parse_rtl_body (c_parser *parser, char *start_with_pass)
18582 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>"))
18584 free (start_with_pass);
18585 return;
18588 location_t start_loc = c_parser_peek_token (parser)->location;
18590 /* Consume all tokens, up to the closing brace, handling
18591 matching pairs of braces in the rtl dump. */
18592 int num_open_braces = 1;
18593 while (1)
18595 switch (c_parser_peek_token (parser)->type)
18597 case CPP_OPEN_BRACE:
18598 num_open_braces++;
18599 break;
18600 case CPP_CLOSE_BRACE:
18601 if (--num_open_braces == 0)
18602 goto found_closing_brace;
18603 break;
18604 case CPP_EOF:
18605 error_at (start_loc, "no closing brace");
18606 free (start_with_pass);
18607 return;
18608 default:
18609 break;
18611 c_parser_consume_token (parser);
18614 found_closing_brace:
18615 /* At the closing brace; record its location. */
18616 location_t end_loc = c_parser_peek_token (parser)->location;
18618 /* Consume the closing brace. */
18619 c_parser_consume_token (parser);
18621 /* Invoke the RTL parser. */
18622 if (!read_rtl_function_body_from_file_range (start_loc, end_loc))
18624 free (start_with_pass);
18625 return;
18628 /* If a pass name was provided for START_WITH_PASS, run the backend
18629 accordingly now, on the cfun created above, transferring
18630 ownership of START_WITH_PASS. */
18631 if (start_with_pass)
18632 run_rtl_passes (start_with_pass);
18635 #include "gt-c-c-parser.h"